SUMMARY
There is a struct in_device __rcu *ip_ptr
under every net_device
.
which stores all the IPv4 related information for this device.
Each IPv4 address is stored a struct in_ifaddr
,
all the IPv4 address of a net device are stored into a list(netdev->ip_ptr->ifa_list).
相关结构体
net_device
和in_device
1 | 1075 struct net_device { |
1 | 55 struct in_device { |
ifa结构体struct in_ifaddr
1 | 161 struct in_ifaddr { |
注册处理增删IP地址
命令的入口函数
1 | 2284 void __init devinet_init(void) |
内核如何处理增加ip地址
calltrace
1 | > inet_rtm_newaddr |
NOTE:
inetaddr_chain
will be shown in next blog.
处理增加IP地址
命令的入口函数
inet_rtm_newaddr
- 创建:函数
rtm_to_ifaddr
根据netlink 消息,生成一个 ifa实例 - 查找:使用函数
find_matching_ifa
查找是否有相同的ifa - 插入:如果没有找到, 则通过``__inet_insert_ifa`添加ifa到内核里。
1 | 807 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) |
rtm_to_ifaddr
根据netlink消息生成 ifa
- 查找并确认netdev 网口名字: 查找 ip命令指定的网口下,是否有这个IP地址,而不是在整个ns下查
- 分配一个ifa实例
- 根据 netlink 消息内容初始化 ifa
Q: 同一个IP地址(掩码也相同)可以配置的多个网口上吗?或者说多个网口可以配置相同的ip地址(ip 和掩码都相同)?
A: 可以
1 | 708 static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh, |
把 ifa 插入到对应的两个链表里。
- 插入到网口下的 ip 地址列表
- 插入同 ns 下的 hash 链表
- 发送 netlink 更新消息
- 发送
blocking_notifier_call_chain
:待学习
1 | 426 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh, |