📄 linux防火墙程序设计.htm
字号:
<BR>利用这个参数可以向内核传递信息,如重定向时的端口号。 <BR>* 参数pskb
<BR>此参数是指向sk_buff结构指针的指针。在Linux中,所有网络数据的发送和接收都用sk_buff数据结构表示。在sk_buff数据结构中包含有对应设备结构的device地址、传输层、网络层、链路层协议头地址等。关于sk_buff的定义可参见skbuff.h头文件。
<BR>3、防火墙程序示例
<BR>下面给出一个简单防火墙程序。在这里假设读者对以太协议、IP协议、TCP协议等常用协议有一定的了解。用命令行\"gcc -Wall -O2
-c
MyFirewall.c\"进行编译,再用insmod命令加载程序后,系统将只响应外部网络用TCP协议的80端口所进行的访问。要让系统恢复原有功能,则可用rmmod命令卸载该程序,源代码见网站www.pccomputing.com.cn上的同名文章。
<BR><BR><BR>// MyFirewall.c 2000年3月7日编写 <BR>#ifndef __KERNEL__ <BR>#
define __KERNEL__ //按内核模块编译 <BR>#endif <BR>#ifndef MODULE <BR># define
MODULE //按设备驱动程序模块编译 <BR>#endif <BR>#include <LINUX
module.h>//最基本的内核模块头文件 <BR>#include <LINUX sched.h><BR>#include <LINUX
kernel.h>//最基本的内核模块头文件 <BR>#include <LINUX netdevice.h><BR>#include
<LINUX ip.h><BR>#include <LINUX tcp.h><BR>#include <LINUX
skbuff.h><BR>#include <LINUX proc_fs.h><BR>#include <LINUX
if.h><BR>#include <LINUX in.h><BR>#include <LINUX firewall.h><BR>#define
SOL_ICMP 1 <BR>#define PERMIT_PORT 80 //只允许访问TCP的80端口 <BR><BR>int
zzl_input(struct firewall_ops *this,int pf,struct device *dev, <BR>void
*phdr,void *arg,struct sk_buff **pskb) <BR>{//每当收到一个网络报时,此函数将被内核调用
<BR>struct tcphdr *tcph; //TCP的头指针 <BR>struct iphdr *iph; //IP头指针
<BR>struct sk_buff *skb=*pskb; <BR>if
(skb->protocol==htons(ETH_P_ARP)){ <BR>printk(\"\\nPermit a ARP
Packet\"); <BR>return FW_ACCEPT;//允许地址解析协议报 <BR>}
<BR>if(skb->protocol==htons(ETH_P_RARP)){ <BR>printk(\"\\nPermit a
RARP Packet\"); <BR>return FW_ACCEPT;//允许反向地址解析协议报 <BR>}
<BR>if(skb->protocol==htons(ETH_P_IP)) <BR>{ <BR>iph=skb->nh.iph;
<BR>if (iph->protocol==SOL_ICMP) <BR>{ <BR>printk(\"\\nPermit a ICMP
Packet\"); <BR>return FW_ACCEPT;//允许网络控制报 <BR>}
<BR>if(iph->protocol==SOL_TCP){ <BR>tcph=skb->h.th;
<BR>if(tcph->dest==PERMIT_PORT){ <BR>printk(\"\\nPermit a valid
access\"); <BR>return FW_ACCEPT;//允许对TCP端口80的访问 <BR>} <BR>} <BR>}
<BR>return FW_REJECT;//禁止对本计算机的所有其它访问 <BR>} <BR>int zzl_output(struct
firewall_ops *this,int pf,struct device *dev, <BR>void *phdr,void
*arg,struct sk_buff **pskb) <BR>{//程序编写方法同zzl_input函数模块
<BR>printk(\"\\nzzl_output is called \"); <BR>return FW_SKIP; <BR>}
<BR>int zzl_foreward(struct firewall_ops *this,int pf,struct device
*dev, <BR>void *phdr,void *arg,struct sk_buff **pskb)
<BR>{//程序编写方法同zzl_input函数模块 <BR>printk(\"\\nzzl_foreward is called \");
<BR>return FW_SKIP; <BR>} <BR>struct firewall_ops zzl_ops= <BR>{
<BR>NULL, <BR>zzl_foreward, <BR>zzl_input, <BR>zzl_output, <BR>PF_INET,
<BR>01 <BR>}; <BR>int init_module(void) <BR>{
<BR>if(register_firewall(PF_INET,&zzl_ops)!=0) <BR>{
<BR>printk(\"\\nunable register firewall\"); <BR>return -1; <BR>}
<BR>printk(\"\\nzzl_ops=%p\",&zzl_ops); <BR>return 0; <BR>} <BR>void
cleanup_module(void) <BR>{ <BR>printk(\"unload\\n\");
<BR>unregister_firewall(PF_INET,&zzl_ops); <BR>}
<BR><BR>(作者地址:北京市北三环中路四号750房 100720 收稿日期:2000.3.13)
<BR><BR><BR><BR>——摘自:游侠部落
<DIV align=right>发布人:netbull 来自:蓝森林 </DIV><BR></UL><IMG
src="Linux防火墙程序设计.files/line.jpg"><BR>
<FORM action=post.php?skin=reart&ID=58 method=post>
<UL>-- 发表评论 --<BR> 昵称:<INPUT name=name> Email:<INPUT
name=email><BR> 内容:<BR><TEXTAREA name=content rows=5 cols=56></TEXTAREA><BR> <INPUT type=submit value=确定回复> <INPUT type=reset value=清除></UL></FORM><IMG
src="Linux防火墙程序设计.files/line.jpg"><BR>
<UL>
<LI>以下是对此文的评论:<BR><BR></LI></UL></TD><!--第三列-->
<TD vAlign=top align=left width="25%"> <IMG
src="Linux防火墙程序设计.files/online.jpg" border=0>
<UL>
<LI><A href="http://www.linuxbyte.net/addnews.php"
target=_blank>新闻发布</A><BR><BR>
<LI><A href="http://www.linuxbyte.net/addart.php"
target=_blank>文献发布</A><BR><BR>
<LI><A href="http://www.linuxbyte.net/addsoft.php"
target=_blank>软件发布</A><BR><BR>
<LI><A href="http://www.linuxbyte.net/manger.php"
target=_blank>软件管理</A><BR></LI></UL><IMG height=5
src="Linux防火墙程序设计.files/tabledi2.jpg" width="100%"><!--下载排行--> <IMG
src="Linux防火墙程序设计.files/download.jpg" border=0> <BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=676">Red Hat
Linux</A> (81310)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=219">Oracle9i
Enterprise</A> (40541)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=93">星际译王1.31版</A> (37768)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=785">kylix</A> (36519)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=1642">Lindows OS
4.5</A> (29616)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=287">AOL
server</A> (27385)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=969">RedHat
中文环境</A> (26129)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=647">Chinput</A> (24366)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=1041">中软Linux</A> (21925)<BR> <A
href="http://www.linuxbyte.net/view.php?skin=soft&id=946">RedHat Linux
7.1正式</A> (21608)<BR><IMG height=5
src="Linux防火墙程序设计.files/tabledi2.jpg" width="100%"> <!--文摘分类--> <IMG
src="Linux防火墙程序设计.files/artstyle.jpg" border=0> <BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=1">内核分析</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=2">网络技术及应用</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=3">应用编程</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=4">硬件应用</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=5">软件应用</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=6">系统管理</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=7">数据库应用</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=8">系统安全</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=9">Linux中文化</A><BR> <A
href="http://www.linuxbyte.net/arttype.php?dno=10">市场与观点</A><BR><IMG
height=5 src="Linux防火墙程序设计.files/tabledi2.jpg" width="100%"> <BR><!--合作伙伴--> <IMG src="Linux防火墙程序设计.files/friends.jpg" border=0>
<BR> <A href="http://www.5ilinux.com/"
target=_blank>我爱linux</A><BR> <A
href="http://www.heblinux.org/"
target=_blank>河北LINUX协会</A><BR> <A
href="http://www.xteamlinux.com.cn/"
target=_blank>冲浪软件下载中心</A><BR> <A
href="http://cosoft.org.cn/html/"
target=_blank>共创联盟</A><BR> <A
href="http://www.coventive.com.cn/"
target=_blank>XLinux</A><BR> <A
href="http://www.it365.net/"
target=_blank>诺金软件电脑网络</A><BR> <A
href="http://www.ch2000.com.cn/"
target=_blank>中文2000软件</A><BR> <A
href="http://www.csuu.com/"
target=_blank>中国Unix联盟</A><BR> <A
href="http://freewares.cn/"
target=_blank>自由软件在中国</A><BR> <A
href="http://www.pconline.com.cn/pcedu"
target=_blank>太平洋电脑信息网</A><BR> <A
href="http://www.linuxaid.com.cn/"
target=_blank>LinuxAid</A><BR> <A
href="http://www.cosoft.org.cn/"
target=_blank>共创软件联盟</A><BR> <A
href="http://wsdn.org/"
target=_blank>WEB程序开发网络</A><BR> <A
href="http://linux.softhouse.com.cn/"
target=_blank>软件屋Linux之家</A><BR> <A
href="http://ljb.vpnet.cn/"
target=_blank>随意网络</A><BR> <A
href="http://linux.ccidnet.com/"
target=_blank>赛迪网Linux专区</A><BR></TD></TR></TBODY></TABLE>
<CENTER>Completed in 0.0548579692841 seconds</CENTER>
<CENTER>COPYRIGHT 2002-2003 <FONT color=#9b2626>LinuxByte.net</FONT> <A
href="mailto:oneteam@mail.linuxbyte.net">联系本站</A></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -