Chinaunix首页 | 论坛 | 博客
  • 博客访问: 790155
  • 博文数量: 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 16:01:44

转:http://blog.csdn.net/nerdx/article/details/12843327
  1. //  策略路由链表  
  2. //      系统中所有策略路由通过fib_rules链接在一起  
  3.   
  4. static struct fib_rule default_rule = {  
  5.     .r_clntref =    ATOMIC_INIT(2),//引用计数  
  6.     .r_preference = 0x7FFF,//路由规则优先级  
  7.     .r_table =  RT_TABLE_DEFAULT,//路由表标示符  
  8.     .r_action = RTN_UNICAST,//路由类型,间接定义了当路由查找匹配时应采取的动作  
  9. };  
  10.   
  11. static struct fib_rule main_rule = {  
  12.     .r_next =   &default_rule,  
  13.     .r_clntref =    ATOMIC_INIT(2),  
  14.     .r_preference = 0x7FFE,  
  15.     .r_table =  RT_TABLE_MAIN,  
  16.     .r_action = RTN_UNICAST,  
  17. };  
  18.   
  19. static struct fib_rule local_rule = {  
  20.     .r_next =   &main_rule,  
  21.     .r_clntref =    ATOMIC_INIT(2),  
  22.     .r_table =  RT_TABLE_LOCAL,  
  23.     .r_action = RTN_UNICAST,  
  24. };  
  25.   
  26. static struct fib_rule *fib_rules = &local_rule;  
  27.   
  28. //  策略路由初始化  
  29. //      向netdev_chain注册监听块  
  30. //  调用路径:ip_rt_init->ip_fib_init->fib_rules_init  
  31. 2.1 void __init fib_rules_init(void)  
  32. {  
  33.     register_netdevice_notifier(&fib_rules_notifier);  
  34. }  
  35.   
  36. 2.1 static struct notifier_block fib_rules_notifier = {  
  37.     .notifier_call =fib_rules_event,  
  38. };  
  39.   
  40. //  策略路由对设备事件的处理  
  41. //      只处理设备注册,注销事件  
  42. static int fib_rules_event(struct notifier_block *thisunsigned long event, void *ptr)  
  43. {  
  44.     struct net_device *dev = ptr;  
  45.     //设备注销,删除与设备相关的策略路由  
  46.     if (event == NETDEV_UNREGISTER)  
  47.         fib_rules_detach(dev);  
  48.     else if (event == NETDEV_REGISTER)  
  49.         fib_rules_attach(dev);//添加与设备相关的策略  
  50.     return NOTIFY_DONE;  
  51. }  
  52.   
  53. //  禁止与设备相关的策略路由  
  54. //      将策略路由r_ifindex=-1,禁止策略路由  
  55. 2.2 static void fib_rules_detach(struct net_device *dev)  
  56. {  
  57.     struct fib_rule *r;  
  58.     //遍历系统中所有的策略路由  
  59.     for (r=fib_rules; r; r=r->r_next) {  
  60.         if (r->r_ifindex == dev->ifindex) {//策略路由使用的设备index  
  61.             write_lock_bh(&fib_rules_lock);  
  62.             r->r_ifindex = -1;//禁止该策略路由  
  63.             write_unlock_bh(&fib_rules_lock);  
  64.         }  
  65.     }  
  66. }  
  67.   
  68. //  激活设备的策略路由  
  69. //      fib_rule->r_ifname表示策略应用的设备  
  70. 2.3 static void fib_rules_attach(struct net_device *dev)  
  71. {  
  72.     struct fib_rule *r;  
  73.   
  74.     for (r=fib_rules; r; r=r->r_next) {  
  75.         if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {  
  76.             write_lock_bh(&fib_rules_lock);  
  77.             r->r_ifindex = dev->ifindex;  
  78.             write_unlock_bh(&fib_rules_lock);  
  79.         }  
  80.     }  
  81. }  
阅读(785) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~