你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux内核--基于Netfilter的内核级包过滤防火墙实现

[日期:2012-05-29] 来源:Linux社区  作者:闫明 [字体:大 中 小]
这里配置文件中不仅支持具体的IP地址,还支持IP地址网段,例如

192.168.1.1

192.168.1.*

192.168.*.*

192.*.*.*

下面是处理函数

  1. /************************************************ 
  2. str:The IP Address like 192.168.1.1 
  3. start:The pointer to the start IP 
  4. end:The pointer to the end IP 
  5. ***********************************************/  
  6. static int ip_to_unsigned(const char * str,unsigned int * start,unsigned int * end)  
  7. {  
  8.     char cache[4][4];  
  9.     /*split the IP address*/  
  10.     int i;  
  11.     int k = 0;  
  12.     int j = 0;  
  13.     for(i = 0;str[i] != '\0';i++)  
  14.     {  
  15.         cache[j][k] = str[i];  
  16.         if(str[i] == '.')  
  17.         {  
  18.             cache[j][k] = '\0';  
  19.             k = 0;  
  20.             j++;  
  21.               
  22.         }  
  23.         else k++;  
  24.         if(j > 3) return NO;  
  25.     }  
  26.     cache[3][k] = '\0';  
  27.   
  28.     short int a[4];  
  29.     for(i = 0;i < 4;i++)  
  30.     {  
  31.         if(cache[i][0] != '*')  
  32.         {  
  33.             a[i] = (short)simple_strtol(cache[i],NULL,0);  
  34.             if(a[i] < 0 || a[i] > 255) return NO;  
  35.         }  
  36.         else  
  37.         {  
  38.             break;  
  39.         }  
  40.     }  
  41.   
  42.     switch(i)  
  43.     {  
  44.         case 4:/*Specific IP Address eg.  192.168.1.1   */  
  45.             *start = *end = (a[0]<<24) + (a[1]<<16) + (a[2]<<8 )+a[3];  
  46.             break;  
  47.         case 3:/*  eg. 192.168.1.*   */  
  48.             *start = (a[0]<<24) + (a[1]<<16) + (a[2]<<8);  
  49.             *end = *start + (1<<8) - 1;  
  50.             break;  
  51.         case 2:/*  eg. 192.168.*.*   */  
  52.             *start = (a[0]<<24) + (a[1]<<16);  
  53.             *end = *start + (1<<16) - 1;  
  54.             break;  
  55.         case 1:/*  eg. 192.*.*.*    */  
  56.             *start = (a[0]<<24);  
  57.             *end = *start + (1<<24) - 1;  
  58.             break;  
  59.         default:  
  60.             *start = 0;  
  61.             *end = (1<<32) - 1;  
  62.             break;  
  63.     }  
  64.   
  65.     return  YES;  
  66. }  
模块的移除函数
  1. void remove_firewall()  
  2. {  
  3.     free_ip_list(&ip_allowed_in_node_head);  
  4.     free_ip_list(&ip_denied_in_node_head);  
  5.   
  6.     free_ip_list(&ip_allowed_out_node_head);  
  7.     free_ip_list(&ip_denied_out_node_head);  
  8.   
  9.     free_port_list(&port_allowed_in_node_head);  
  10.     free_port_list(&port_denied_in_node_head);  
  11.   
  12.     free_port_list(&port_allowed_out_node_head);  
  13.     free_port_list(&port_denied_out_node_head);  
  14.   
  15.     nf_unregister_hook(&my_netfilter[0]);  
  16.     nf_unregister_hook(&my_netfilter[1]);  
  17.     printk("CLEAN up my firewall OK!\n");  
  18. }  
  1. MODULE_LICENSE("Dual BSD/GPL");  
  2. MODULE_AUTHOR("yming0221@gmail.com");  
该防火墙支持命令参数设置,根据参数设置防火墙的工作模式,只需定义和声明
  1. static int work_mode = 0;//default mode   
  2. module_param(work_mode,int,S_IRUGO);  
目前测试,防火墙正常工作。

可以看到数据包能到达网络层,但由于防火墙启用的相应检查规则,浏览器等应用层软件无法联网,数据包被丢弃。

linux
相关资讯       Linux内核编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款