在IP hook点获取MAC,IP,TCP/UDP指针

2820阅读 0评论2016-09-12 Rayree1993
分类:LINUX

最近用到了IP hook钩子点抓包分析。需要获取2,3,4指针。
其中内核中有几个函数可以帮助获取指针:
中:

点击(此处)折叠或打开

  1. static inline struct ethhdr *eth_hdr(const struct sk_buff *skb)
  2. {
  3.     return (struct ethhdr *)skb_mac_header(skb);
  4. }
该函数获取skb中的mac头指针。

中:

点击(此处)折叠或打开

  1. static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
  2. {
  3.     return (struct iphdr *)skb_network_header(skb);
  4. }
该函数获取skb中的ip头指针。

中:

点击(此处)折叠或打开

  1. static inline struct tcphdr *tcp_hdr(const struct sk_buff *skb)
  2. {
  3.     return (struct tcphdr *)skb_transport_header(skb);
  4. }
该函数获取skb中的tcp头指针。

中:

点击(此处)折叠或打开

  1. static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
  2. {
  3.     return (struct udphdr *)skb_transport_header(skb);
  4. }
该函数获取skb中的udp头指针。

但是当我在使用IP HOOK时,使用tcp_hdr函数时获取到的tcp头指针计算tcp数据长度时是错误的,让我很疑惑。最后看到了一篇帖子才明白了其中的原由:


原来时IP hook钩子函数都在 IP 层。
skb 有指向 MAC 头和 IP 头的成员,都是有效的,可以直接使用接口API获取。

而获取TCP/UDP头时,skb中的成员是无效的,所以需要自己实现获取API:
tcph = (struct tcphdr *)((unsigned char *)iph + iph->ihl * 4)

上一篇:移植正则表达式到内核态
下一篇:从程序员的角度看ASCII, GB2312, UNICODE, UTF-8