如何将 raw_socket 绑定到内核空间中的特定接口?
我的问题是,当我用 PF_PACKET 监听时,我也会收到我的盒子发送的数据包,这是不希望的。
err_create = sock_create(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL), sock);
我尝试了 ETH_P_IP,而不是 ETH_P_ALL,但随后我只收到发送到我的主机的数据包。所以我的想法是将套接字绑定到接口。使用“sockaddr_ll”我可以定义 ifindex。但我发现没有函数来获取我的接口的索引。ioctl 在内核空间中不起作用。
..
memset(&my_addr, 0, sizeof(struct sockaddr_ll));
my_addr.sll_family = PF_PACKET;
my_addr.sll_protocol = htons(ETH_P_ALL);
// my_addr.sll_ifindex = 2; //I tried different numbers.. but then I got no packets
err_bind = sock->ops->bind(sock, (struct sockaddr_ll *)&my_addr, sizeof(my_addr));
任何帮助将不胜感激
My problem is, when I listen with PF_PACKET I also get the packets my box sends and which is undesirable.
err_create = sock_create(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL), sock);
Instead of ETH_P_ALL I tried ETH_P_IP, but then I only get packets send to my host. So my idea was to bind the socket to an interface. With 'sockaddr_ll' I can define the ifindex. But I found no function to get the index of my interface.. ioctl does not work in kernelspace.
..
memset(&my_addr, 0, sizeof(struct sockaddr_ll));
my_addr.sll_family = PF_PACKET;
my_addr.sll_protocol = htons(ETH_P_ALL);
// my_addr.sll_ifindex = 2; //I tried different numbers.. but then I got no packets
err_bind = sock->ops->bind(sock, (struct sockaddr_ll *)&my_addr, sizeof(my_addr));
Any help would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您要做什么,但您可以通过 dev_get_by_name() 在内核中查找网络设备。但是为什么你首先要尝试在内核中捕获数据包呢?这就是 libpcap 在用户空间中的用途。
I'm not sure about what you're trying to do, but you can look up a network device in the kernel via dev_get_by_name(). But why are you trying to catch packets in the kernel in the first place? That's what libpcap is for in userspace.