Chinaunix首页 | 论坛 | 博客
  • 博客访问: 125780
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 354
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-01 15:34
个人简介

不晓得说啥子

文章分类

全部博文(42)

文章存档

2015年(41)

2014年(1)

我的朋友

分类: LINUX

2015-04-06 14:36:34

还是一filter表为例,在注册filter表之前需要将其初始化,初始化会用到一个repl结构,这个结构是根据filter表来初始化的。

1、repl结构
struct ipt_replace {
     /* Which table. */
     char name[XT_TABLE_MAXNAMELEN];

     /* Which hook entry points are valid: bitmask.  You can't
           change this. */
     unsigned int valid_hooks;

     /* Number of entries */
     unsigned int num_entries;

     /* Total size of new entries */
     unsigned int size;

     /* Hook entry points. */
     unsigned int hook_entry[NF_INET_NUMHOOKS];

     /* Underflow points. */
     unsigned int underflow[NF_INET_NUMHOOKS];

     /* Information about old entries: */
     /* Number of counters (must be equal to current number of entries). */
     unsigned int num_counters;
     /* The old entries' counters. */
     struct xt_counters __user *counters;

     /* The entries (hang off end: not really an array). */  可变长的数组
     struct ipt_entry entries[0];
};


2、首先简单的初始化filter表,给其中部分成员赋值
     static const struct xt_table packet_filter = {
     .name          = "filter",
     .valid_hooks     = FILTER_VALID_HOOKS,
     .me          = THIS_MODULE,
     .af          = NFPROTO_IPV4,
     .priority     = NF_IP_PRI_FILTER,
     };

3、根据packet_filter的值生成一个struct ipt_replace结构repl,这部分是用宏来实现的

     #define xt_alloc_initial_table(type, typ2) ({ \
     unsigned int hook_mask = info->valid_hooks; \
     unsigned int nhooks = hweight32(hook_mask); \
     unsigned int bytes = 0, hooknum = 0, i = 0; \
     struct { \                                                                                        //该宏最后返回的是这样的一个结构,将该结构复制给repl,由于repl结构的最后一个元素是可变长的柔性数组,所以这里才可以这样赋值。
          struct type##_replace repl; \
          struct type##_standard entries[nhooks]; \
          struct type##_error term; \
     } *tbl = kzalloc(sizeof(*tbl), GFP_KERNEL); \
     if (tbl == NULL) \
          return NULL; \
     strncpy(tbl->repl.name, info->name, sizeof(tbl->repl.name)); \
     tbl->term = (struct type##_error)typ2##_ERROR_INIT;  \
     tbl->repl.valid_hooks = hook_mask; \
     tbl->repl.num_entries = nhooks + 1; \
     tbl->repl.size = nhooks * sizeof(struct type##_standard) + \
                      sizeof(struct type##_error); \
     for (; hook_mask != 0; hook_mask >>= 1, ++hooknum) { \
          if (!(hook_mask & 1)) \
               continue; \
          tbl->repl.hook_entry[hooknum] = bytes; \
          tbl->repl.underflow[hooknum]  = bytes; \
          tbl->entries[i++] = (struct type##_standard) \
               typ2##_STANDARD_INIT(NF_ACCEPT); \
          bytes += sizeof(struct type##_standard); \
     } \
     tbl; \
})

4、最后生成的repl结构如下图所示

阅读(1625) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~