使用netfilter时如何从iph-> frag_offset访问IP_DF和IP_MF [英] How to access IP_DF and IP_MF from iph->frag_offset when using netfilter

查看:286
本文介绍了使用netfilter时如何从iph-> frag_offset访问IP_DF和IP_MF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个netfilter模块,并希望访问IPHeader的字段,该字段表示DF和MF字段.我可以根据需要访问大多数其他字段,但我认为我必须从结构中的ipheader提取DF和MF字段并对其进行操作,例如,我想根据接收到的数据包的类型来设置或取消设置DF位.

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.

以下结构具有"frag_off",我如何从中访问/重写IP_DF和IP_MF?

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));

推荐答案

字段frag_off为16位. 前3个是标志,其余13位是偏移量.

The field frag_off is 16 bits. The first 3 are the flags, the rest 13 bits is the offset.

标志位是:第一个是保留的,必须为0.第二个是DF,第三个是MF. 因此,要访问DF,您应该使用"frag_off& 0x4000"和IP_DF声明为0x4000来隔离第二位,以便执行"iph-> frag_off& IP_DF".

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".

与MF相同,0x2000,对于偏移IP_OFFSET,为0x1FFF

Same for MF, 0x2000, and for the offset IP_OFFSET, 0x1FFF

这篇关于使用netfilter时如何从iph-> frag_offset访问IP_DF和IP_MF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆