def phrx_ip4(self, ip4_packet_rx): """ Handle inbound IP packets """ self.logger.debug(f"{ip4_packet_rx.tracker} - {ip4_packet_rx}") # Check if received packet has been sent to us directly or by unicast/broadcast, allow any destination if no unicast address is configured (for DHCP client) if self.stack_ip4_unicast and ip4_packet_rx.ip4_dst not in {*self.stack_ip4_unicast, *self.stack_ip4_multicast, *self.stack_ip4_broadcast}: self.logger.debug(f"{ip4_packet_rx.tracker} - IP packet not destined for this stack, droping") return # Validate IP header checksum if not ip4_packet_rx.validate_cksum(): self.logger.debug(f"{ip4_packet_rx.tracker} - IP packet has invalid checksum, droping") return # Check if packet is a fragment, and if so process it accrdingly ip4_packet_rx = handle_ip4_fragmentation(ip4_packet_rx) if not ip4_packet_rx: return if ip4_packet_rx.ip4_proto == ps_ip4.IP4_PROTO_ICMP4 and ps_icmp4.preliminary_sanity_check(ip4_packet_rx.raw_data, ip4_packet_rx.tracker, self.logger): self.phrx_icmp4(ip4_packet_rx, ps_icmp4.Icmp4Packet(ip4_packet_rx)) return if ip4_packet_rx.ip4_proto == ps_ip4.IP4_PROTO_UDP and ps_udp.preliminary_sanity_check(ip4_packet_rx.raw_data, ip4_packet_rx.tracker, self.logger): self.phrx_udp(ip4_packet_rx, ps_udp.UdpPacket(ip4_packet_rx)) return if ip4_packet_rx.ip4_proto == ps_ip4.IP4_PROTO_TCP and ps_tcp.preliminary_sanity_check(ip4_packet_rx.raw_data, ip4_packet_rx.tracker, self.logger): self.phrx_tcp(ip4_packet_rx, ps_tcp.TcpPacket(ip4_packet_rx)) return
def phrx_ip6(self, ip6_packet_rx): """ Handle inbound IP packets """ self.logger.debug(f"{ip6_packet_rx.tracker} - {ip6_packet_rx}") # Check if received packet has been sent to us directly or by unicast or multicast if ip6_packet_rx.ip6_dst not in { *self.stack_ip6_unicast, *self.stack_ip6_multicast }: self.logger.debug( f"{ip6_packet_rx.tracker} - IP packet not destined for this stack, droping" ) return if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_ICMP6 and ps_icmp6.preliminary_sanity_check( ip6_packet_rx.raw_data, ip6_packet_rx.tracker, self.logger): self.phrx_icmp6(ip6_packet_rx, ps_icmp6.Icmp6Packet(ip6_packet_rx)) return if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_UDP and ps_udp.preliminary_sanity_check( ip6_packet_rx.raw_data, ip6_packet_rx.tracker, self.logger): self.phrx_udp(ip6_packet_rx, ps_udp.UdpPacket(ip6_packet_rx)) return if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_TCP and ps_tcp.preliminary_sanity_check( ip6_packet_rx.raw_data, ip6_packet_rx.tracker, self.logger): self.phrx_tcp(ip6_packet_rx, ps_tcp.TcpPacket(ip6_packet_rx)) return
def phrx_ip6(self, ip6_packet_rx): """ Handle inbound IP packets """ # Validate IPv6 packet sanity if ip6_packet_rx.sanity_check_failed: return self.logger.debug(f"{ip6_packet_rx.tracker} - {ip6_packet_rx}") # Check if received packet has been sent to us directly or by unicast or multicast if ip6_packet_rx.ip6_dst not in {*self.ip6_unicast, *self.ip6_multicast}: self.logger.debug( f"{ip6_packet_rx.tracker} - IP packet not destined for this stack, droping" ) return if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_ICMP6: self.phrx_icmp6(ip6_packet_rx, ps_icmp6.Icmp6Packet(ip6_packet_rx)) return if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_UDP: self.phrx_udp(ip6_packet_rx, ps_udp.UdpPacket(ip6_packet_rx)) return if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_TCP: self.phrx_tcp(ip6_packet_rx, ps_tcp.TcpPacket(ip6_packet_rx)) return
def main(): while True: raw_packet_rx = raw_socket.recv(2048) ether_packet_rx = ps_ether.EtherPacket(raw_packet_rx) if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_ARP: arp_packet_rx = ps_arp.ArpPacket(ether_packet_rx) print("-" * 160) print(ether_packet_rx) print(arp_packet_rx) print("-" * 160) continue if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_IP6: ipv6_packet_rx = ps_ipv6.Ip6Packet(ether_packet_rx) if ipv6_packet_rx.ipv6_next == ps_ipv6.IP6_NEXT_HEADER_ICMP6: icmpv6_packet_rx = ps_icmpv6.Icmp6Packet(ipv6_packet_rx) print("-" * 160) print(ether_packet_rx) print(ipv6_packet_rx) print(icmpv6_packet_rx) print("-" * 160) continue print("-" * 160) print(ether_packet_rx) print(ipv6_packet_rx) continue if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_IP6: ipv4_packet_rx = ps_ipv4.Ip4Packet(ether_packet_rx) if ipv4_packet_rx.ipv4_proto == ps_ipv4.IP4_PROTO_ICMP4: icmpv4_packet_rx = ps_icmpv4.Icmp4Packet(ipv4_packet_rx) print("-" * 160) print(ether_packet_rx) print(ipv4_packet_rx) print(icmpv4_packet_rx) print("-" * 160) continue if ipv4_packet_rx.ipv4_proto == ps_ipv4.IP4_PROTO_UDP: udp_packet_rx = ps_udp.UdpPacket(ipv4_packet_rx) print("-" * 160) print(ether_packet_rx) print(ipv4_packet_rx) print(udp_packet_rx) print("-" * 160) continue if ipv4_packet_rx.ipv4_proto == ps_ipv4.IP4_PROTO_TCP: tcp_packet_rx = ps_tcp.TcpPacket(ipv4_packet_rx) if 22 in {tcp_packet_rx.tcp_dport, tcp_packet_rx.tcp_sport}: continue print("-" * 160) print(ether_packet_rx) print(ipv4_packet_rx) print(tcp_packet_rx) print("-" * 160) continue print("-" * 160) print(ether_packet_rx) print(ipv4_packet_rx) print("-" * 160) continue print("-" * 160) print(ether_packet_rx)