Chinaunix

标题: linux内核3.0.0-通过sk_buff读取tcp端口的问题!急!谢! [打印本页]

作者: lanqiucoco    时间: 2012-04-30 15:25
标题: linux内核3.0.0-通过sk_buff读取tcp端口的问题!急!谢!
我现在用的是ubuntu 11.10,内核是3.0.0-17
问题是我最近想写一个基于netfilter框架的流量程序。现在struct sk_buff结构变了

struct sk_buff {
  .
  .
  sk_buff_data_t transport_header;
  sk_buff_data_t network_header;
  sk_buff_data_t mac_header;
  .
  .
}

现在我在框架中挂一个钩子,怎么运用这个sk_buff结构读出包的目的端口啊?急!!谢谢!
  1. #include <linux/module.h>
  2. #include <linux/tcp.h>
  3. #include <linux/kernel.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/ip.h>
  6. #include <linux/netfilter.h>
  7. #include <linux/netfilter_ipv4.h>

  8. MODULE_LICENSE("GPL");

  9. unsigned char *deny_port = "\x00\x16";

  10. struct nf_hook_ops hookops;
  11. struct tcphdr *tcpth;

  12. unsigned int hook_func(unsigned int hooknum,
  13.                            struct sk_buff *skb,
  14.                            const struct net_device *in,
  15.                            const struct net_device *out,
  16.                            int (*okfn)(struct sk_buff *))
  17. {
  18.     tcpth = NULL;
  19.     tcpth = tcp_hdr(skb);
  20.     printk("source port = %s\n",tcpth->source);
  21.     printk("dest port = %d\n",tcpth->dest);
  22.     printk("len = %d\n",skb->len);
  23.     return NF_ACCEPT;
  24. }

  25. static int example_init(void)
  26. {
  27.    hookops.hook = hook_func;
  28.    hookops.hooknum = NF_INET_LOCAL_IN;
  29.    hookops.pf = PF_INET;
  30.    hookops.priority = NF_IP_PRI_FIRST;

  31.    nf_register_hook(&hookops);
  32.    printk("example module install into kernel!\n");
  33.    return 0;
  34. }

  35. static void example_exit(void)
  36. {
  37.    nf_unregister_hook(&hookops);
  38.    printk("example module removed from kernel!\n");
  39. }

  40. module_init(example_init);
  41. module_exit(example_exit);
复制代码
我输出的dest值一直都是69,source也不确定值,会改变?不知道问什么?是我tcp头地址读错了吗?谢谢

还有一个问题为什么在钩子函数中输出skb->data_len的值是0?

作者: lanqiucoco    时间: 2012-04-30 15:28
额,那个printk("source port = %s\n",tcpth->source);打错了,是%d的。最后这个的值一直都是69,不管是什么包?求解,谢谢!
作者: lanqiucoco    时间: 2012-04-30 23:00
额,没人理我

实验发现和以前的版本差不多...




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2