首页 技术 正文
技术 2022年11月14日
0 收藏 488 点赞 4,458 浏览 4490 个字

参考 cs89x0.c

1.网卡驱动程序与网络驱动程序的区别
网卡驱动程序:网络驱动程序中最底层的驱动,
主要工作:把上面发下来的数据发送出去,收到数据后构造一个包抛给上层。有收发能力就可以了。

2.网卡设备驱动框架

app:  socket
--------------------------------------------------
---------------
--------------- 若干层网络协议--纯软件
---------------
---------------
ndo_start_xmit || /\
\/ || netif_rx sk_buff
---------------
硬件相关的驱动程序(要提供hard_start_xmit, 有数据时要用netif_rx上报)
--------------------------------------------------
硬件

(1).分配net_device结构
(2).设置
  ①提供发包函数:netdev_tx_t net_send_packet(struct sk_buff *skb, struct net_device *dev);
  ②提供收包函数:int netif_rx(struct sk_buff *skb);
(3).注册:register_netdevice
(4).硬件相关操作

由上可知,网卡驱动与上层软件之间的数据交换是通过struct sk_buff进行的,上层传下来一个sk_buff给到网卡驱动,网卡发出去;网卡驱动收到数据后构造一个sk_buff包发给上层软件。

3.当应用程序ping自己网卡的ip的时候,根本就不会调用到网卡驱动的ndo_start_xmit和netif_rx函数,在网卡驱动的上层就已经返回了。这也是为什么只注册一个网卡设备结构体就能ping通自己的原因。也就表明了Ip是个纯软件的概念,此时ifconfig列举出的RX/TX统计信息是不会变化的。当ping其它网卡的Ip地址的时候,上层软件才会把数据包丢给驱动。

4.当有多个网卡的时候,不同的网卡最好不要处于同一个网段中,因为若存在于同一个网段中,主机根本就不知道应该从哪个网卡中把数据发送出去。主机会优先选择与目的Ip处于同一网段的网卡。

5.可以在ndo_start_xmit()加dump_stack()来查调试上层软件的发包机制。

6.示例驱动
伪造一个目的ip网卡进行应答,使能ping通其它Ip的网卡

/*
* 参考 drivers\net\cs89x0.c
*/#include <linux/module.h>
#include <linux/printk.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/jiffies.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/gfp.h>
#include <linux/io.h>
#include <linux/ip.h>#include <asm/irq.h>
#include <linux/atomic.h>static struct net_device *vnet_dev;/*
iph = ip_hdr(skb);
static inline struct ethhdr *eth_hdr(const struct sk_buff *skb)
*/static void emulator_rx_packet(struct sk_buff *skb, struct net_device *dev)
{
/* 参考LDD3 */
unsigned char *type;
struct iphdr *ih = ip_hdr(skb);
__be32 tmp;
unsigned char tmp_dev_addr[ETH_ALEN];
struct ethhdr *ethhdr = eth_hdr(skb); struct sk_buff *rx_skb; // 从硬件读出/保存数据
/* 对调"源/目的"的mac地址 */
//ethhdr = (struct ethhdr *)skb->data;
memcpy(tmp_dev_addr, ethhdr->h_dest, ETH_ALEN);
memcpy(ethhdr->h_dest, ethhdr->h_source, ETH_ALEN);
memcpy(ethhdr->h_source, tmp_dev_addr, ETH_ALEN); /* 对调"源/目的"的ip地址 */
//ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
tmp = ih->saddr;;
ih->saddr = ih->daddr;
ih->daddr = tmp; //((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */
//((u8 *)daddr)[2] ^= 1;
type = skb->data + sizeof(struct ethhdr) + sizeof(struct iphdr);
//printk("tx package type = %02x\n", *type);
/* 修改类型, 原来0x8表示ping, 0表示reply */
*type = ; ih->check = ; /* and rebuild the checksum (ip needs it) */
ih->check = ip_fast_csum((unsigned char *)ih, ih->ihl); /*here oops: Unable to handle kernel paging request at virtual address ffff800600000000*/ /* 构造一个sk_buff */
rx_skb = dev_alloc_skb(skb->len + );
skb_reserve(rx_skb, ); /* align IP on 16B boundary */
memcpy(skb_put(rx_skb, skb->len), skb->data, skb->len); /* Write metadata, and then pass to the receive level */
rx_skb->dev = dev;
rx_skb->protocol = eth_type_trans(rx_skb, dev);
rx_skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */ /* 假装接收到数据更新统计信息 */
dev->stats.rx_packets++;
dev->stats.rx_bytes += skb->len; /* 提交sk_buff */
netif_rx(rx_skb);
}static int virt_net_send_packet(struct sk_buff *skb, struct net_device *dev)
{
static int cnt = ;
printk("virt_net_send_packet cnt = %d\n", ++cnt); /* 对于真实的网卡, 把skb里的数据通过网卡发送出去 */
netif_stop_queue(dev); /* 停止该网卡的队列 */
/* ...... */ /* 把skb的数据写入网卡 */ /* 构造一个假的sk_buff,上报,类型回环,使ping其它Ip也能成功*/
emulator_rx_packet(skb, dev); /*tmp commit out!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ netif_wake_queue(dev); /* 数据全部发送出去后,唤醒网卡的队列,实际的网卡是在发送完后产生中断,在中断服务函数中唤醒队列*/ /* 更新统计信息 这些统计信息ifconfig可以看到 */
dev->stats.tx_packets++;
dev->stats.tx_bytes += skb->len; dev_kfree_skb (skb); /* 释放skb */ return ;
}static const struct net_device_ops virt_net_ndops = {
.ndo_start_xmit = virt_net_send_packet,
};static int virt_net_init(void)
{
/* 1. 分配一个net_device结构体 */
vnet_dev = alloc_netdev(, "vnet%d", NET_NAME_UNKNOWN, ether_setup); /* alloc_etherdev */ /* 2. 设置 */
vnet_dev->netdev_ops = &virt_net_ndops; /*上层软件通过这个函数把sk_buff丢给网卡驱动*/ /* 设置MAC地址 */
vnet_dev->dev_addr[] = 0x11; /*vnet_dev->dev_addr在alloc_netdev时已经分配了空间的*/
vnet_dev->dev_addr[] = 0x22;
vnet_dev->dev_addr[] = 0x33;
vnet_dev->dev_addr[] = 0x44;
vnet_dev->dev_addr[] = 0x55;
vnet_dev->dev_addr[] = 0x66; /* 设置下面两项才能ping通 why? */
vnet_dev->flags |= IFF_NOARP;
//vnet_dev->features |= NETIF_F_NO_CSUM; /* 3. 注册 */
//register_netdevice(vnet_dev);
register_netdev(vnet_dev); return ;
}static void virt_net_exit(void)
{
unregister_netdev(vnet_dev);
free_netdev(vnet_dev);
}module_init(virt_net_init);
module_exit(virt_net_exit);MODULE_AUTHOR("thisway.diy@163.com,17653039@qq.com");
MODULE_LICENSE("GPL");
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,945
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,471
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,284
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,100
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,731
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,767