Chinaunix首页 | 论坛 | 博客
  • 博客访问: 417044
  • 博文数量: 124
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 872
  • 用 户 组: 普通用户
  • 注册时间: 2018-03-29 14:38
个人简介

默默的一块石头

文章分类

全部博文(124)

文章存档

2022年(26)

2021年(10)

2020年(28)

2019年(60)

我的朋友

分类: LINUX

2019-10-18 15:51:09

1.net_class_attrs(net-sysfs.c)
static struct attribute *net_class_attrs[] = {
&dev_attr_netdev_group.attr,
&dev_attr_type.attr,
&dev_attr_dev_id.attr,
&dev_attr_iflink.attr,
&dev_attr_ifindex.attr,
&dev_attr_addr_assign_type.attr,
&dev_attr_addr_len.attr,
&dev_attr_link_mode.attr,
&dev_attr_address.attr,
&dev_attr_broadcast.attr,
......
NULL,
};
ATTRIBUTE_GROUPS(net_class);

2.address attribute(net-sysfs.c)
/* use same locking rules as GIFHWADDR ioctl's */
static ssize_t address_show(struct device *dev, struct device_attribute *attr,char *buf)
{
struct net_device *net = to_net_dev(dev);
ssize_t ret = -EINVAL;

read_lock(&dev_base_lock);
if (dev_isalive(net))
ret = sysfs_format_mac(buf, net->dev_addr, net->addr_len);
read_unlock(&dev_base_lock);
return ret;
}
static DEVICE_ATTR_RO(address);



3.ARP MAC地址的获取来源
static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
{
......
/*
首先,每台主机都会在自己的ARP缓冲区中建立一个 ARP列表,以表示I P地址和MAC地址的对应关系。当源主机需要将一个数据包要发送到目的主机时,会首先检查自己 ARP列表中是否存在该 I P地址对应的MAC地址,如果有,就直接将数据包发送到这个MAC地址;如果没有,就向本地网段发起一个ARP请求的广播包,查询此目的主机对应的MAC地址。此ARP请求数据包里包括源主机的IP地址、硬件地址、以及目的主机的I P地址
*/
arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
dst_hw, dev->dev_addr, NULL);//saddr:IP 地址 dev->dev_addr:MAC地址
}

struct arphdr {
__be16 ar_hrd; /* format of hardware address */
__be16 ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
__be16 ar_op; /* ARP opcode (command) */
};

static inline int dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  unsigned short type,
  const void *daddr, const void *saddr,
  unsigned int len)
{
if (!dev->header_ops || !dev->header_ops->create)
return 0;


return dev->header_ops->create(skb, dev, type, daddr, saddr, len);
}


/* Interface to link layer: send routine and receive handler.*/

/* Create an arp packet. If (dest_hw == NULL), we create a broadcast message. */
struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
   struct net_device *dev, __be32 src_ip,
   const unsigned char *dest_hw,
   const unsigned char *src_hw,
   const unsigned char *target_hw)
{
struct sk_buff *skb;
struct arphdr *arp;
 unsigned char *arp_ptr;

 ......
if (src_hw == NULL)
src_hw = dev->dev_addr;

/* Fill the device header for the ARP frame */
if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
goto out;
......
arp_ptr = (unsigned char *)(arp + 1);

memcpy(arp_ptr, src_hw, dev->addr_len);
arp_ptr += dev->addr_len;
memcpy(arp_ptr, &src_ip, 4);
arp_ptr += 4;

......
}

4.struct neighbour 
struct neighbour {
unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];//MAC地址
 .....
u8 primary_key[0];//IP地址
};

阅读(1568) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~