在通过ifconfig eth0 xxx 修改ip地址的时候内核流程为:
fib_inetaddr_notifier{}:fib_inetaddr_event->fib_add_ifaddr()
在此我们已ifconfig eth0 192.168.11.103为例子看一下fib_add_ifaddr的执行过程
//ifconfig eth0 192.168.11.103
void fib_add_ifaddr(struct in_ifaddr *ifa)
{
struct in_device *in_dev = ifa->ifa_dev;
struct net_device *dev = in_dev->dev;
struct in_ifaddr *prim = ifa;
__be32 mask = ifa->ifa_mask;//00ffffff
__be32 addr = ifa->ifa_local;//670ba8c0 =ifa_address
__be32 prefix = ifa->ifa_address&mask;//00ffffff & 670ba8c0=000ba8c0
//如果flag参数指定了配置IP对象为从属设备或者临时设备
if (ifa->ifa_flags&IFA_F_SECONDARY)
{
//但是根据索引找不到该设备,返回错误
prim = inet_ifa_byprefix(in_dev, prefix, mask);
if (prim == NULL)
{
printk(KERN_WARNING "fib_add_ifaddr: bug: prim == NULL\n");
return;
}
}
//通过ifconfig eth0 192.168.11.103 将192.168.11.103加入local表中
//在local表中添加路由表项
fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
//设备是否在运行
if (!(dev->flags & IFF_UP))
return;
/* Add broadcast address, if it is explicitly assigned. */
//通过ifconfig eth0 192.168.11.103
//广播地址为ifa->ifa_broadcast=ff0ba8c0
if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
//htonl(0xff000000) & 000ba8c0 == 0为假
if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
(prefix != addr || ifa->ifa_prefixlen < 32))
{
//将192.168.11.0/24加入到MAIN表中
fib_magic(RTM_NEWROUTE,
dev->flags&IFF_LOOPBACK ? RTN_LOCAL: RTN_UNICAST , //RTN_UNICAST
prefix, //192.168.11.0
ifa->ifa_prefixlen, //24
prim);
/* Add network specific broadcasts, when it takes a sense */
//ifconfig eth0 192.168.11.103 掩码长度24
if (ifa->ifa_prefixlen < 31)
{
fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);//192.168.11.0 加入到local表
fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim);
} // 000ba8c0 | 11000000 =192.168.11.255加入到local表
}
}
fib_add_ifaddr()调用之前main表和local表
执行ifconfig eth1 192.168.1.2后路由表main表和local表的变化内核通过fib_add_ifaddr()将路由项加入路由表中
————————————————
版权声明:本文为CSDN博主「yldfree」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yldfree/article/details/82911271
阅读(1911) | 评论(0) | 转发(0) |