问题
I am trying to write a netfilter module and want to access the fields of the IPHeader that denotes the DF and MF fields. I can access most other fields as desired but I think I have to extract the DF and MF fields from the ipheader in the struct and manipulate them say for example I want to set or unset the DF bit depending on the type of packet I receive.
The below structure has 'frag_off' how do I access/rewrite IP_DF and IP_MF from this?
struct iphdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u8 ihl:4,
version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
__u8 version:4,
ihl:4;
#else
#error "Please fix <asm/byteorder.h>"
#endif
__u8 tos;
__u16 tot_len;
__u16 id;
__u16 frag_off;
__u8 ttl;
__u8 protocol;
__u16 check;
__u32 saddr;
__u32 daddr;
/*The options start here. */
};
#define IP_MF 0x2000 /* Flag: "More Fragments" */
#define IP_OFFSET 0x1FFF /* "Fragment Offset" part */
#define IP_DF 0x4000 /* dont fragment flag */
printk(KERN_INFO "IP_FRAG_OFF : %d", (iph->frag_off & IP_OFFSET));
printk(KERN_INFO "MF: %d", (iph->frag_off & IP_MF));
回答1:
The field frag_off is 16 bits. The first 3 are the flags, the rest 13 bits is the offset.
The flags bits are: first is reserved and must be 0. second is DF and third is MF. So, to access the DF you should isolate the second bit by "frag_off & 0x4000", and IP_DF declared as 0x4000, so you can do "iph->frag_off & IP_DF".
Same for MF, 0x2000, and for the offset IP_OFFSET, 0x1FFF
来源:https://stackoverflow.com/questions/51699201/how-to-access-ip-df-and-ip-mf-from-iph-frag-offset-when-using-netfilter