免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3512 | 回复: 3

[网络子系统] netfilter hook函数的hooknum值的问题? [复制链接]

论坛徽章:
7
IT运维版块每日发帖之星
日期:2016-05-27 06:20:00IT运维版块每日发帖之星
日期:2016-06-09 06:20:00操作系统版块每日发帖之星
日期:2016-06-12 06:20:00程序设计版块每日发帖之星
日期:2016-06-12 06:20:00操作系统版块每日发帖之星
日期:2016-06-13 06:20:00IT运维版块每日发帖之星
日期:2016-06-17 06:20:002015-2016NBA季后赛纪念章
日期:2016-06-28 17:42:27
发表于 2017-11-11 15:47 |显示全部楼层
如下代码中,打印hooknum发现,其值是一个很大的值,为什么会跑到我的注册函数中?这个值为什么不是我注册的几个值中的一个呢?

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/netfilter.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/ip.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/if_ether.h>
  9. #include <linux/if_packet.h>
  10. #include <net/tcp.h>
  11. #include <net/udp.h>
  12. #include <net/icmp.h>
  13. #include <linux/netfilter_ipv4.h>

  14. #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
  15. #define MAC_ARG(x) (((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5])

  16. MODULE_LICENSE("GPL");
  17. MODULE_AUTHOR("kenthy@163.com");

  18. const char *hooks[] = {
  19.         "NF_INET_PRE_ROUTING",
  20.         "NF_INET_LOCAL_IN",
  21.         "NF_INET_FORWARD",
  22.         "NF_INET_LOCAL_OUT",
  23.         "NF_INET_POST_ROUTING",
  24. };

  25. void print_ipproto(int proto) {
  26.                 switch(proto) {
  27.                         case IPPROTO_ICMP:
  28.                                 printk("%s\n", "IPPROTO_ICMP");
  29.                                 break;
  30.                         case IPPROTO_TCP:
  31.                                 printk("%s\n", "IPPROTO_TCP");
  32.                                 break;
  33.                         case IPPROTO_UDP:
  34.                                 printk("%s\n", "IPPROTO_UDP");
  35.                                 break;
  36.                         default:
  37.                                 printk("%s\n", "other IPPROTO");
  38.                                 break;
  39.                 }
  40. }

  41. void print_mac(struct ethhdr *eth) {
  42.                 if (NULL == eth)
  43.                         return;
  44.                 if (eth->h_source != NULL)
  45.                         printk("SOURCE:"MAC_FMT"\n", MAC_ARG(eth->h_source));
  46.                 if (eth->h_dest != NULL)
  47.                         printk("SOURCE:"MAC_FMT"\n", MAC_ARG(eth->h_dest));
  48. }

  49. unsigned int
  50. mac (unsigned int hooknum,
  51.         struct sk_buff* skb,
  52.         const struct net_device *in,
  53.         const struct net_device *out,
  54.         int (*okfn)(struct sk_buff*))
  55. {
  56.         struct sk_buff *nskb;
  57.         struct iphdr *iph = NULL;
  58.         struct ethhdr *eth;
  59.        
  60.         nskb = skb;
  61.         if (nskb == NULL) {
  62.                 printk("%s\n", "*skb is NULL");
  63.                 return NF_ACCEPT;
  64.         }
  65.        
  66.         if (nskb->len < sizeof(struct iphdr)+sizeof(struct ethhdr)) {
  67.                 printk("not valid ip\n");
  68.                 return NF_ACCEPT;
  69.         }
  70.         iph = ip_hdr(nskb);
  71.         if (iph == NULL) {
  72.                 printk("%s\n", "*iph is NULL");
  73.                 return NF_ACCEPT;
  74.         }
  75.        
  76.         if(hooknum > 4) {
  77.                 printk("hooknum=%u\n", hooknum);
  78.                 printk("hooknum=%u, %u\n", ntohl(hooknum), htonl(hooknum));
  79.                 return NF_ACCEPT;
  80.         }
  81.         printk("hooknum %u\n", hooknum);
  82.         printk("-------begin %s -------\n", hooks[hooknum]);
  83.         print_ipproto(iph->protocol);
  84.         printk("len is %d, data len is %d\n", nskb->len, nskb->data_len);
  85.         if (nskb->mac_len > 0) {
  86.                 eth = (struct ethhdr *)skb_mac_header(skb);
  87.                 print_mac(eth);
  88.         }
  89.         else {
  90.                 printk("%s", "mac is NULL");
  91.         }
  92.        
  93.         printk("-------end %s -------\n", hooks[hooknum]);
  94.         return NF_ACCEPT;
  95. }

  96. static struct nf_hook_ops mac_ops[] = {
  97.                 {
  98.                         .hook = mac,
  99.                         .owner = THIS_MODULE,
  100.                         .pf = PF_INET,
  101.                         .hooknum = NF_INET_PRE_ROUTING,
  102.                         .priority = NF_IP_PRI_FIRST,
  103.                 },
  104.                 {
  105.                         .hook = mac,
  106.                         .owner = THIS_MODULE,
  107.                         .pf = PF_INET,
  108.                         .hooknum = NF_INET_LOCAL_IN,
  109.                         .priority = NF_IP_PRI_FIRST,
  110.                 },
  111.                 {
  112.                         .hook = mac,
  113.                         .owner = THIS_MODULE,
  114.                         .pf = PF_INET,
  115.                         .hooknum = NF_INET_FORWARD,
  116.                         .priority = NF_IP_PRI_FIRST,
  117.                 },
  118.                 {
  119.                         .hook = mac,
  120.                         .owner = THIS_MODULE,
  121.                         .pf = PF_INET,
  122.                         .hooknum = NF_INET_LOCAL_OUT,
  123.                         .priority = NF_IP_PRI_FIRST,
  124.                 },
  125.                 {
  126.                         .hook = mac,
  127.                         .owner = THIS_MODULE,
  128.                         .pf = PF_INET,
  129.                         .hooknum = NF_INET_POST_ROUTING,
  130.                         .priority = NF_IP_PRI_FIRST,
  131.                 },
  132. };

  133. static int __init init(void) {
  134.         int ret;
  135.         ret = nf_register_hooks(mac_ops, ARRAY_SIZE(mac_ops));
  136.         if (ret < 0) {
  137.                 printk("http detect: can't register mac_ops detect hook!\n");
  138.                 return ret;
  139.         }
  140.        
  141.         printk("insmod mac_ops detect module\n");
  142.         return 0;
  143. }

  144. static void __exit fini(void) {
  145.         nf_unregister_hooks(mac_ops, ARRAY_SIZE(mac_ops));
  146.         printk("remove mac_ops detect module.\n");
  147. }

  148. module_init(init);
  149. module_exit(fini);
复制代码


打印出的值为:

[66920.605991] insmod mac_ops detect module
[66923.279828] hooknum=2688819368
[66923.279831] hooknum=2820687008, 2820687008
[66923.279842] hooknum=2688819424
[66923.279843] hooknum=3760211104, 3760211104
[66923.279853] hooknum=2688819200
[66923.279853] hooknum=2114720, 2114720
[66923.279855] hooknum=2688819256
[66923.279855] hooknum=941638816, 941638816
[66923.279903] hooknum=2688819368
[66923.279904] hooknum=2820687008, 2820687008
[66923.279906] hooknum=2688819424
[66923.279907] hooknum=3760211104, 3760211104
[66923.283333] hooknum=2688819200
[66923.283337] hooknum=2114720, 2114720
[66923.283347] hooknum=2688819256
[66923.283348] hooknum=941638816, 941638816
[66923.283409] hooknum=2688819368
[66923.283410] hooknum=2820687008, 2820687008
[66923.283413] hooknum=2688819424
[66923.283443] hooknum=3760211104, 3760211104
[66923.283451] hooknum=2688819200
[66923.283452] hooknum=2114720, 2114720
[66923.283453] hooknum=2688819256
[66923.283454] hooknum=941638816, 941638816
[66923.283987] hooknum=2688819368
[66923.283989] hooknum=2820687008, 2820687008
[66923.283995] hooknum=2688819424
[66923.283996] hooknum=3760211104, 3760211104
[66923.294197] hooknum=2688819200
[66923.294201] hooknum=2114720, 2114720
[66923.294215] hooknum=2688819256
[66923.294216] hooknum=941638816, 941638816
[66923.298107] hooknum=2688819368
[66923.298117] hooknum=2820687008, 2820687008
[66923.298148] hooknum=2688819424
[66923.298149] hooknum=3760211104, 3760211104
[66923.298180] hooknum=2688819200
[66923.298181] hooknum=2114720, 2114720
[66923.298183] hooknum=2688819256
[66923.298184] hooknum=941638816, 941638816
[66923.298354] hooknum=2688819368
[66923.298357] hooknum=2820687008, 2820687008
[66923.298367] hooknum=2688819424
[66923.298367] hooknum=3760211104, 3760211104
[66923.311544] hooknum=2688819200
[66923.311549] hooknum=2114720, 2114720
[66923.311568] hooknum=2688819256
[66923.311569] hooknum=941638816, 941638816
[66923.311655] hooknum=2688819368
[66923.311657] hooknum=2820687008, 2820687008
[66923.311662] hooknum=2688819424
[66923.311663] hooknum=3760211104, 3760211104
[66923.311672] hooknum=2688819200
[66923.311673] hooknum=2114720, 2114720
[66923.311675] hooknum=2688819256
[66923.311676] hooknum=941638816, 941638816
[66924.285636] hooknum=2688819368
[66924.285643] hooknum=2820687008, 2820687008
[66924.285656] hooknum=2688819424
[66924.285657] hooknum=3760211104, 3760211104
[66924.298939] hooknum=2688819200
[66924.298943] hooknum=2114720, 2114720
[66924.298959] hooknum=2688819256
[66924.298961] hooknum=941638816, 941638816
[66927.460909] remove mac_ops detect module.


这个是什么原因导致的呢?

论坛徽章:
7
IT运维版块每日发帖之星
日期:2016-05-27 06:20:00IT运维版块每日发帖之星
日期:2016-06-09 06:20:00操作系统版块每日发帖之星
日期:2016-06-12 06:20:00程序设计版块每日发帖之星
日期:2016-06-12 06:20:00操作系统版块每日发帖之星
日期:2016-06-13 06:20:00IT运维版块每日发帖之星
日期:2016-06-17 06:20:002015-2016NBA季后赛纪念章
日期:2016-06-28 17:42:27
发表于 2017-11-11 21:59 |显示全部楼层
本帖最后由 qianguozheng 于 2017-11-12 10:48 编辑

没有用过hooknum吗? 传进来的不应该是我在结构体设置的hooknum吗

论坛徽章:
7
IT运维版块每日发帖之星
日期:2016-05-27 06:20:00IT运维版块每日发帖之星
日期:2016-06-09 06:20:00操作系统版块每日发帖之星
日期:2016-06-12 06:20:00程序设计版块每日发帖之星
日期:2016-06-12 06:20:00操作系统版块每日发帖之星
日期:2016-06-13 06:20:00IT运维版块每日发帖之星
日期:2016-06-17 06:20:002015-2016NBA季后赛纪念章
日期:2016-06-28 17:42:27
发表于 2017-11-13 11:02 |显示全部楼层
@Godbach 这个例子来自你的修改skb的例子,现在传递过来的hooknum有什么变化吗? 我在ubuntu 4.4的实体机器上面测试发现hooknum一直是0.

论坛徽章:
7
IT运维版块每日发帖之星
日期:2016-05-27 06:20:00IT运维版块每日发帖之星
日期:2016-06-09 06:20:00操作系统版块每日发帖之星
日期:2016-06-12 06:20:00程序设计版块每日发帖之星
日期:2016-06-12 06:20:00操作系统版块每日发帖之星
日期:2016-06-13 06:20:00IT运维版块每日发帖之星
日期:2016-06-17 06:20:002015-2016NBA季后赛纪念章
日期:2016-06-28 17:42:27
发表于 2017-11-13 19:01 |显示全部楼层
回调函数变化导致的问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP