网卡混杂模式的设置可以通过下面的命令来进行设置:
root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:1e:4f:e7:81:16
inet addr:10.0.16.111 Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::21e:4fff:fee7:8116/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1678 errors:0 dropped:0 overruns:0 frame:0
TX packets:63 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:164865 (161.0 KB) TX bytes:8362 (8.1 KB)
Base address:0xecc0 Memory:fe9e0000-fea00000
root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0 promisc
root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:1e:4f:e7:81:16
inet addr:10.0.16.111 Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::21e:4fff:fee7:8116/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:1751 errors:0 dropped:0 overruns:0 frame:0
TX packets:63 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:169849 (165.8 KB) TX bytes:8362 (8.1 KB)
Base address:0xecc0 Memory:fe9e0000-fea00000
root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0 -promisc
root@dell-desktop:/home/dell/libppf/bin# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:1e:4f:e7:81:16
inet addr:10.0.16.111 Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::21e:4fff:fee7:8116/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1815 errors:0 dropped:0 overruns:0 frame:0
TX packets:64 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:176114 (171.9 KB) TX bytes:8458 (8.2 KB)
Base address:0xecc0 Memory:fe9e0000-fea00000
程序可以通过IOCTL来实现:
int Set_Promisc(char *interface, int sock ) {
struct ifreq ifr;
strncpy(ifr.ifr_name, interface,strnlen(interface)+1);
if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) {
/*Could not retrieve flags for the interface*/
perror("Could not retrive flags for the interface");
exit(0);
}
printf("The interface is ::: %s\n", interface);
perror("Retrieved flags from interface successfully");
/*now that the flags have been retrieved*/
/* set the flags to PROMISC */
ifr.ifr_flags |= IFF_PROMISC;
if (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1 ) {
/*Could not set the flags on the interface */
perror("Could not set the PROMISC flag:");
exit(0);
}
printf("Setting interface ::: %s ::: to promisc", interface);
return(0);
}
分析ifconfig promisc的实现:
调用 ioctl(skfd, SIOCSIFFLAGS, &ifr)
net/core/dev.c net/core/dev_mcast.c
int dev_ioctl(unsigned int cmd, void __user *arg)
int dev_change_flags(struct net_device *dev, unsigned flags)
void dev_set_promiscuity(struct net_device *dev, int inc)
void dev_mc_upload(struct net_device *dev)
dev->set_multicast_list(dev)
调驱动的 void smc_set_multicast_list(struct net_device *dev)并操作硬件。
参考:
http://www.cnitblog.com/darkstax/articles/18219.html
阅读(2091) | 评论(0) | 转发(1) |