Gops
Gops

Reputation: 45

Determining if a packet is ipv4 or ipv6 from sk_buff's network header

I have the SKB of type 'struct sk_buff' and I was trying to determine if the packet is of type IPv4 or IPv6. Basically I need the source IP address, but dont know, for sure how to check the 'version' field inside iph or ipv6h, or dont know if it is a reliable approach to check 'version' value.

Network header inside the sk_buff implementation on my machine is:

union {
        struct iphdr    *iph;
        struct ipv6hdr  *ipv6h;
        struct arphdr   *arph;
        unsigned char   *raw;
} nh;

And the iphdr and ipv6hdr have usual definitions.

How to determine the IP version from the IP network header in sk_buff?

Upvotes: 1

Views: 3080

Answers (2)

Mi Bear
Mi Bear

Reputation: 11

nf_ct_l3num(skb->nfct) can tell you whether it's IPv4 or IPv6, after conntrack for this skb is initialized.

Upvotes: 1

caskey
caskey

Reputation: 12695

The IP version number is encoded in the first 4 bits of the packet whether it is ipv4 or ipv6 for just this reason. Use the ipv4 pointer (ipv) and examine the version field.

Upvotes: 3

Related Questions