 |
|
Packet Sniffer SDK VCL Edition
BPF example: IP level | | Previous Next |
To filter a protocol encapsulated into IP you should check EthType field for
ETHERTYPE_IP = 0x0800, and then analyze Protocol field in the IP packet. ICMP packets filter example:IPPROTO_ICMP = 1 // ICMP protocol
IPPROTO_TCP = 6 // TCP protocol
IPPROTO_UDP = 17 // UDP protocol See also files winsock.h/winsock2.h (Microsoft Platform SDK). BpfFilter.AddCmd(BPF_LD+BPF_H+BPF_ABS, 12); // EthType
BpfFilter.AddJmp(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 3); // EthType == ETHERTYPE_IP
BpfFilter.AddCmd(BPF_LD+BPF_B+BPF_ABS, 23); // Get Protocol field value
BpfFilter.AddJmp(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_ICMP, 0, 1); // Protocol == ICMP
BpfFilter.AddCmd(BPF_RET+BPF_K, (UINT)-1); // TRUE (Accept)
BpfFilter.AddCmd(BPF_RET+BPF_K, 0); // FALSE (Reject) #define IP_PROTO 0x800
#define IPPROTO_ICMP 0x1
ld P[12:2] // A = WORD offset 12 (protocol in the Ethernet header)
jeq IP_PROTO, 0, Exit // If A <> 0x800 (IP), exit
ld P[23] // A = BYTE offset 23 (protocol in the IP header)
jeq IPPROTO_ICMP, 0, Exit // If A <> 6 (ICMP), exit
ret -1 // It is an ICMP packet, exit and return TRUE
Exit:
ret 0 // It is not an ICMP packet, exit and return FALSEIPv4 Datagram header Format: IP header fields:VersionVersion of the IP protocol which determines how to interpret the header. Currently the only
permitted values are 4 (0100) or 6 (0110). The header format shown here is valid for IPv4 only.IHLLength of a header as a number of 32-bit wordsType of serviceThis field is often ignored by current routers but is meant to allow traffic to be prioritised
(among other things).Total LengthThe length of the entire datagram including the header and data: maximum permitted it 65,535
bytes or 64K.Identification, Flags and Fragment OffsetThese values allow datagrams to be fragmented for transmission and reassembled at the
destination.Time to liveAn integer which is decremented at each router "hop"; supposed to be interpreted as a number
of seconds but more often treated as a "hop count". If the value reaches zero the datagram is
discarded and an ICMP message is sent to the source host.ProtocolIdentifies the transport-layer protocol which will interpret the Data section. This will
typically be TCP or UDP but other values are possible. Protocols are identified by a unique number as
listed in an online database at http://www.iana.org.Header checksumThis is used to verify the header, and is recalculated at each router hop. This field is left
out of IPv6 which relies on the transport layer for verification.Addresses and Options
These are 32-bit IP addresses which identify the network and host address. Routing
requirements can also be specified in the Options field, along with options to do with security
and debugging.
|