Chinaunix首页 | 论坛 | 博客
  • 博客访问: 789717
  • 博文数量: 264
  • 博客积分: 592
  • 博客等级: 中士
  • 技术积分: 1574
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-24 22:02
文章分类

全部博文(264)

文章存档

2019年(2)

2018年(1)

2017年(1)

2016年(4)

2015年(14)

2014年(57)

2013年(88)

2012年(97)

分类: LINUX

2014-02-12 15:58:07

转:http://blog.csdn.net/nerdx/article/details/12839651
  1. //  监听设备事件  
  2. //      在ip_rt_init->devinet_init中注册  
  3. 1.1 static struct notifier_block ip_netdev_notifier = {  
  4.     .notifier_call =inetdev_event,  
  5. };  
  6.   
  7. //  路由子系统对网络设备事件的处理  
  8. //      与事件相关的设备需要有inet配置信息  
  9.   
  10. //  函数主要任务:  
  11. //      1.开启设备,加入多播组,为回环设备配置ip地址  
  12. //      2.关闭设备,设备退出多播组  
  13. //      3.设备注销,删除该设备的配置信息  
  14. //      4.设备更名,更新设备的配置信息  
  15.   
  16. //  注:路由子系统不处理设备注册事件,因为设备注册时,还没有inet配置信息。  
  17. 1.2 static int inetdev_event(struct notifier_block *thisunsigned long event,  
  18.              void *ptr)  
  19. {  
  20.     struct net_device *dev = ptr;  
  21.     struct in_device *in_dev = __in_dev_get(dev);  
  22.     //只处理有inet配置信息的网络设备  
  23.     if (!in_dev)  
  24.         goto out;  
  25.   
  26.     switch (event) {  
  27.     //设备注册,说明有bug,因为设备注册时,不会有inet配置信息,已经返回  
  28.     case NETDEV_REGISTER:  
  29.         dev->ip_ptr = NULL;  
  30.         break;  
  31.     //设备开启  
  32.     case NETDEV_UP:  
  33.         //ip要求mtu至少68  
  34.         if (dev->mtu < 68)  
  35.             break;  
  36.         //由路由子系统处理回环设备的inet配置信息  
  37.         if (dev == &loopback_dev) {  
  38.             struct in_ifaddr *ifa;  
  39.             //分配ip地址描述符  
  40.             if ((ifa = inet_alloc_ifa()) != NULL) {  
  41.                 //初始化ip地址,掩码  
  42.                 ifa->ifa_local =  
  43.                   ifa->ifa_address = htonl(INADDR_LOOPBACK);  
  44.                 ifa->ifa_prefixlen = 8;  
  45.                 ifa->ifa_mask = inet_make_mask(8);  
  46.                 in_dev_hold(in_dev);  
  47.                 ifa->ifa_dev = in_dev;  
  48.                 //ip地址的scope为host,表示此ip只在本机内部有效  
  49.                 ifa->ifa_scope = RT_SCOPE_HOST;  
  50.                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);  
  51.                 //将ifa插入到in_dev->ifa_list  
  52.                 inet_insert_ifa(ifa);  
  53.             }  
  54.             in_dev->cnf.no_xfrm = 1;  
  55.             in_dev->cnf.no_policy = 1;  
  56.         }  
  57.         //加入多播组  
  58.         ip_mc_up(in_dev);  
  59.         break;  
  60.     case NETDEV_DOWN:  
  61.         //离开多播组  
  62.         ip_mc_down(in_dev);  
  63.         break;  
  64.     case NETDEV_CHANGEMTU:  
  65.         if (dev->mtu >= 68)  
  66.             break;  
  67.     case NETDEV_UNREGISTER:  
  68.         inetdev_destroy(in_dev);  
  69.         break;  
  70.     case NETDEV_CHANGENAME:  
  71.         inetdev_changename(dev, in_dev);  
  72.         break;  
  73.     }  
  74. out:  
  75.     return NOTIFY_DONE;  
  76. }  
  77.   
  78. //  删除设备的配置信息  
  79. //  函数主要任务:  
  80. //      1.删除设备配置每个ip地址  
  81. //      2.删除设备调整邻居协议的参数  
  82. //      3.通过邻居协议设备配置信息不再使用  
  83. 1.3 static void inetdev_destroy(struct in_device *in_dev)  
  84. {  
  85.     struct in_ifaddr *ifa;  
  86.     struct net_device *dev;  
  87.     //表示配置信息不再被使用  
  88.     in_dev->dead = 1;  
  89.     //遍历设备配置的地址  
  90.     while ((ifa = in_dev->ifa_list) != NULL) {  
  91.         //配置信息中删除该地址  
  92.         inet_del_ifa(in_dev, &in_dev->ifa_list, 0);  
  93.         inet_free_ifa(ifa);  
  94.     }  
  95.   
  96.     dev = in_dev->dev;  
  97.     dev->ip_ptr = NULL;  
  98.     //每个设备用于调整邻居协议的参数,通过table->params链接在一起  
  99.     neigh_parms_release(&arp_tbl, in_dev->arp_parms);  
  100.     //通知邻居协议与此设备相关的邻居项失效  
  101.     arp_ifdown(dev);  
  102.     call_rcu(&in_dev->rcu_head, in_dev_rcu_put);  
  103. }  
阅读(706) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~