我们正在尝试为 linux 内核实现一个 NAT 模块。面临的问题是,对于传入挂钩的 TCP 数据包,预期指向目标端口的指针不这样做。传出钩子的片段:
unsigned int incoming_hook(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
struct iphdr *iph;
struct tcphdr *tcph;
unsigned int dst_addr;
unsigned short dst_port;
unsigned short *dpptr;
printk(KERN_ALERT "****Incoming hook starts****\n");
if (!skb)return NF_ACCEPT;
iph = ip_hdr(skb);
if (!iph)return NF_ACCEPT;
dst_addr = ntohl(iph->daddr); // works correct
printk(KERN_ALERT " dst addr is %d \n", dst_addr);
if (iph->protocol==IPPROTO_TCP)
{
tcph = tcp_hdr(skb);
if (!tcph)return NF_ACCEPT;
dst_port = ntohs(tcph->dest); // gives incorrect result (unexpected.)
// dpptr = (unsigned short *)((char *)iph + 22); // just a try. does not work either
printk(KERN_ALERT " dst port is %d \n", *dpptr);
dst_port = ntohs(*dpptr);
// continues with the NAT code
然而,同样的事情也适用于改变源地址和源端口的传出钩子上的数据包。以下适用于传出数据包
tcph = tcp_hdr(skb);
if (!tcph)return NF_ACCEPT;
src_port = ntohs(tcph->source);
传入数据包的 IP 标头大小是否有问题,或者 struct tcphdr 有错误或 tcphdr->dest 指针有问题?还是可能出了问题?
需要帮忙。谢谢。