【发布时间】:2015-12-11 16:20:30
【问题描述】:
我正在编写一个 netfilter 模块来根据目标 IP 检查丢弃数据包。
我注册了一个 nf 钩子
nfho_out.hook = hook_func_out;
nfho_out.hooknum = NF_INET_LOCAL_OUT;
nfho_out.pf = PF_INET;
nfho_out.priority = NF_IP_PRI_FIRST;
nf_register_hook(&nfho_out);
这是我的 nf 钩子函数
unsigned int hook_func_out(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)) {
struct iphdr *ip_header = (struct iphdr *)skb_network_header(skb);
struct udphdr *udp_header;
struct tcphdr *tcp_header;
struct list_head *p;
unsigned int src_ip = (unsigned int)ip_header->saddr;
unsigned int dest_ip = (unsigned int)ip_header->daddr;
unsigned int src_port = 0;
unsigned int dest_port = 0;
if (ip_header->protocol==17) {
udp_header = (struct udphdr *)skb_transport_header(skb);
src_port = (unsigned int)ntohs(udp_header->source);
} else if (ip_header->protocol == 6) {
tcp_header = (struct tcphdr *)skb_transport_header(skb);
src_port = (unsigned int)ntohs(tcp_header->source);
dest_port = (unsigned int)ntohs(tcp_header->dest);
}
printk(KERN_INFO "OUT packet info: src ip: %u, src port: %u; dest ip: %u, dest port: %u; proto: %u\n", src_ip, src_port, dest_ip, dest_port, ip_header->protocol);
printk(KERN_DEBUG "IP addres = %pI4 DEST = %pI4\n", &src_ip, &dest_ip);
....
....
}
我在网络浏览器中打开了 google.com,但我无法获得 google.com 解析到的实际目标 IP 地址。问题是我总是得到一些奇怪的 IP 地址(因为我怀疑代理或 dns 服务器)而不是实际的 IP 地址。
如何在nf钩子函数中获取实际的目的地址?
【问题讨论】:
-
您确定您的网络浏览器连接到您认为的 IP 吗? Google 因使用奇怪的地址而闻名(除了 8.8.8.8)...
-
我总是将目标地址设为 10.169.95.255。我的 IP 地址是 10.169.95.73
-
无论我浏览什么网站,我都会从 IP 标头中获得 10.169.95.255 作为目标 IP
-
尝试直接连接:"telnet some_wbesite.com 80" 然后"get/http/1.0"
-
也许看看
iptablessources 可能会有所帮助。
标签: linux linux-kernel linux-device-driver