网络子系统62_路由子系统处理设备事件
// 监听设备事件 // 在ip_rt_init->devinet_init中注册 1.1 static struct notifier_block ip_netdev_notifier = { .notifier_call =inetdev_event, }; // 路由子系统对网络设备事件的处理 // 与事件相关的设备需要有inet配置信息 // 函数主要任务: // 1.开启设备,加入多播组,为回环设备配置ip地址 // 2.关闭设备,设备退出多播组 // 3.设备注销,删除该设备的配置信息 // 4.设备更名,更新设备的配置信息 // 注:路由子系统不处理设备注册事件,因为设备注册时,还没有inet配置信息。 1.2 static int inetdev_event(struct notifier_block *this, unsigned long event, void *ptr) { struct net_device *dev = ptr; struct in_device *in_dev = __in_dev_get(dev); //只处理有inet配置信息的网络设备 if (!in_dev) goto out; switch (event) { //设备注册,说明有bug,因为设备注册时,不会有inet配置信息,已经返回 case NETDEV_REGISTER: dev->ip_ptr = NULL; break; //设备开启 case NETDEV_UP: //ip要求mtu至少68 if (dev->mtu < 68) break; //由路由子系统处理回环设备的inet配置信息 if (dev == &loopback_dev) { struct in_ifaddr *ifa; //分配ip地址描述符 if ((ifa = inet_alloc_ifa()) != NULL) { //初始化ip地址,掩码 ifa->ifa_local = ifa->ifa_address = htonl(INADDR_LOOPBACK); ifa->ifa_prefixlen = 8; ifa->ifa_mask = inet_make_mask(8); in_dev_hold(in_dev); ifa->ifa_dev = in_dev; //ip地址的scope为host,表示此ip只在本机内部有效 ifa->ifa_scope = RT_SCOPE_HOST; memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); //将ifa插入到in_dev->ifa_list inet_insert_ifa(ifa); } in_dev->cnf.no_xfrm = 1; in_dev->cnf.no_policy = 1; } //加入多播组 ip_mc_up(in_dev); break; case NETDEV_DOWN: //离开多播组 ip_mc_down(in_dev); break; case NETDEV_CHANGEMTU: if (dev->mtu >= 68) break; case NETDEV_UNREGISTER: inetdev_destroy(in_dev); break; case NETDEV_CHANGENAME: inetdev_changename(dev, in_dev); break; } out: return NOTIFY_DONE; } // 删除设备的配置信息 // 函数主要任务: // 1.删除设备配置每个ip地址 // 2.删除设备调整邻居协议的参数 // 3.通过邻居协议设备配置信息不再使用 1.3 static void inetdev_destroy(struct in_device *in_dev) { struct in_ifaddr *ifa; struct net_device *dev; //表示配置信息不再被使用 in_dev->dead = 1; //遍历设备配置的地址 while ((ifa = in_dev->ifa_list) != NULL) { //配置信息中删除该地址 inet_del_ifa(in_dev, &in_dev->ifa_list, 0); inet_free_ifa(ifa); } dev = in_dev->dev; dev->ip_ptr = NULL; //每个设备用于调整邻居协议的参数,通过table->params链接在一起 neigh_parms_release(&arp_tbl, in_dev->arp_parms); //通知邻居协议与此设备相关的邻居项失效 arp_ifdown(dev); call_rcu(&in_dev->rcu_head, in_dev_rcu_put); }
最后更新:2017-04-03 14:53:45