我制作了一个模块,旨在使用来自用户空间(应用程序)的ioctl命令发送ping数据包。但是我不确定它是否发送ping数据包,因为当我尝试通过挂钩函数接收数据包时,没有回复它。我正在将ping数据包发送到路由器(192.168.1.1)。以下是我的代码,它编译并运行w / o错误:
int device_ioctl(struct net_device *dev,struct ifreq *ifr, int cmd)
{
int len;
//printk("%s , %d",__func__,__LINE__);
switch(cmd)
{
case SIOCDEVPRIVATE:
printk("here it is : %s: %d\n",__func__,__LINE__);
break;
case WLAN_TRANSMIT :
icmp.type = ICMP_ECHO;
icmp.code = 0;
icmp.un.echo.sequence = i++;
icmp.un.echo.id = current->pid & 0xFFFF;
printk(KERN_ALERT"ID::%X\n",icmp.un.echo.id);
ip4.protocol = 0x01; //for icmp protocol
ip4.tos = 0x00;
ip4.frag_off = 0;
ip4.daddr = in_aton(procfs_buffer);
ip4.saddr = in_aton(ifr->ifr_addr.sa_data);
len = sizeof(data);
skb = dev_alloc_skb(1500);
skb->dev = __dev_get_by_name(&init_net,"wlan0");
skb_reserve(skb,NET_IP_ALIGN); //header of 2 bytes; increments tail and data pointer
skb->data = skb_put(skb,sizeof(len)); // increments all pointer or adds data
memcpy(data,skb->data,len);
wdev = skb->dev;
skb->transport_header =skb_push(skb,sizeof(icmp));
memset(skb->transport_header,0,sizeof(struct icmphdr));
memcpy(skb->transport_header,&icmp,sizeof(struct icmphdr));
skb->network_header=skb_push(skb,sizeof(ip4));
memset(skb->network_header,0,sizeof(struct iphdr));
memcpy(skb->network_header,&ip4,sizeof(struct iphdr));
// printk("i::%d\n",i);
// skb->mac_header = skb_push(skb,6*sizeof(0xFF));
// memset(skb->mac_header,0xFF,6*sizeof(0xFF));
dev_queue_xmit(skb);
// kfree(skb);
}
return 0;
}
unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb1, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))
{
struct sk_buff *sock_buff;
struct iphdr *ip1;
struct icmphdr *icmp1;
sock_buff = skb1;
//printk("ihere we are::%s,%d\n",__func__,__LINE__);
ip1 = (struct iphdr *)skb_network_header(sock_buff);
//printk(KERN_ALERT"proto:%d,addr:%X::%X\n",ip1->protocol,ip1->saddr,ip1->daddr);
if(ip1->protocol==1)
{
printk(KERN_ALERT"proto:%d,addr:%X::%X\n",ip1->protocol,ip1->saddr,ip1->daddr);
icmp1 = (struct icmphdr *)skb_transport_header(sock_buff);
printk(KERN_ALERT"reply type: %c,,seq : %04X\n",icmp1->type,icmp1->un.echo.id);
}
// kfree(sock_buff);
return NF_ACCEPT;
}
钩子函数工作正常,但不确定如何发送ping数据包(有疑问,因为没有得到回复数据包)。
问候
卡兰
答案 0 :(得分:1)
您可以使用以下命令检查ping是否已经开箱即用:root:
# tcpdump -i eth0 icmp
当然,将eth0
替换为您机器的真实接口名称。
此命令将转储该接口中的所有ICMP流量,因此您应该能够检查发生了什么。