ºìÁªLinuxÃÅ»§
Linux°ïÖú

netfilter£ºLinux ·À»ðǽÔÚÄÚºËÖеÄʵÏÖ

·¢²¼Ê±¼ä:2006-06-29 16:47:07À´Ô´:ºìÁª×÷Õß:wide
×÷ÕߣºLinux/Free Software ¶ÀÁ¢¼¼Êõ¹ËÎÊ

±¾ÎĽéÉÜ Linux µÄ·À»ðǽ¼¼Êõ netfilter/iptables ÔÚ Linux ÄÚºËÖеľßÌåʵÏÖ¡£

netfilter ºÍ Linux ·À»ðǽ½éÉÜ

Linux µÄ·À»ðǽ¼¼Êõ¾­ÀúÁËÈô¸É´úµÄÑظһ²½²½µÄ·¢Õ¹¶øÀ´¡£×ʼµÄ ipfwadm ÊÇ Alan Cox ÔÚ Linux kernel ·¢Õ¹µÄ³õÆÚ£¬´Ó FreeBSD µÄÄں˴úÂëÖÐÒÆÖ²¹ýÀ´µÄ¡£ºóÀ´¾­ÀúÁË ipchains£¬ÔÙ¾­ÓÉ Paul Russell ÔÚ Linux kernel 2.3 ϵÁеĿª·¢¹ý³ÌÖз¢Õ¹ÁË netfilter Õâ¸ö¼Ü¹¹¡£¶øÓû§¿Õ¼äµÄ·À»ðǽ¹ÜÀí¹¤¾ß£¬Ò²ÏàÓ¦µÄ·¢Õ¹Îª iptables¡£netfilter/iptables Õâ¸ö×éºÏÄ¿Ç°Ï൱µÄÁîÈËÂúÒâ¡£ÔÚ¾­ÀúÁË Linux kernel 2.4 ºÍ 2.5 µÄ·¢Õ¹ÒԺ󣬵ÄÈ·¿ÉÒÔ˵£¬netfilter/iptables ¾­ÊÜסÁË´óÁ¿Óû§¹ã·ºÊ¹ÓõĿ¼Ñé¡£

±¾ÎIJ¢²»´òËã½éÉÜ Linux ·À»ðǽÔÚÓû§¿Õ¼äµÄ¹ÜÀí³ÌÐò iptables µÄʹÓá£ÖÁÓÚÈçºÎÀûÓà netfilter/iptables »úÖƴһ¸ö¿É¿¿µÄ Internet ·À»ðǽ£¬ÕâÒ²²»ÊDZ¾ÎĸÐÐËȤµÄ»°Ìâ¡£¹ØÓÚ iptables µÄʹÓ㬶ÁÕßÅóÓÑÃÇ¿ÉÒԲο¼ man iptables µÄÊֲᣬҲ¿ÉÒԲο¼ netfilter µÄºËÐÄ¿ª·¢Õß Paul Russell дµÄ Packet Filtering HOW-TO ºÍ NAT HOW-TO¡£Ïà¹ØµÄÁ´½Ó£¬Çë²Î¼ûÎĺóËùÁеIJο¼×ÊÁÏĿ¼¡£¶ÁÕßÅóÓÑÃÇÔÚÔĶÁ±¾ÎÄ֮ǰ£¬×îºÃÄܹ»¶Ô iptables µÄʹÓÃÓÐÒ»¶¨µÄÁ˽⡣

±¾ÎĽéÉÜ netfilter ÔÚ Linux kernel ÖеÄʵÏÖ¡£Èç¹ûÌõ¼þÔÊÐíµÄ»°£¬ÎÒÃÇ¿ÉÄÜÔÚºóÐøµÄÎÄÕÂÖн«Òª½øÒ»²½ËµÃ÷ÈçºÎ±àд×Ô¼ºµÄ kernel modules ²¢½«ÆäÏâǶÔÚ netfilter µÄ¼Ü¹¹ÖУ¬ÒÔʵÏÖ×Ô¼ºµÄ¶¨ÖÆ·À»ðǽ¹¦ÄÜ¡£ÖµµÃÖ¸³öµÄÊÇ£¬ÔÚ netfilter µÄÍøÕ¾ÉÏ£¬¿ÉÒÔ¿´µ½ netfilter µÄÒ»¸ö×ÓÏîÄ¿ patch-o-matic£¬ÆäÖÐÊÕ¼ÁË´óÁ¿µÄ¸÷ÖÖ¶¨ÖÆ kernel modules£¬ÕâЩ modules ¸ø¶ÁÕßÅóÓÑÃÇ¿ª·¢×Ô¼ºµÄ kernel modules£¬ÌṩÁ˷dz£¶àµÄ¡¢ºÜºÃµÄÀý×Ó¡£

IPv4 ´úÂëÖÐ netfilter µÄ½Ó¿Ú

netfilter ÔÚ Linux kernel ÖÐµÄ IPv4¡¢IPv6 ºÍ DECnet µÈÍøÂçЭÒéÕ»Öж¼ÓÐÏàÓ¦µÄʵÏÖ¡£±¾ÎÄÏÞÓÚƪ·ù£¬½«Ö»½éÉÜÆäÖÐ×îÈôó¶àÊý¶ÁÕßÅóÓÑÃǸÐÐËȤµÄ IPv4 ЭÒéÕ»É쵀 netfilter µÄʵÏÖ¡£

ÎÒÃÇÔÚ±àÒë Linux kernel µÄ¹ý³ÌÖÐÒ»¶¨»á×¢Òâµ½£¬netfilter ÊÇÒ»¸öÔÚ±àÒë¹ý³ÌÖпÉÑ¡µÄ²¿¼þ¡£Ò²¾ÍÊÇ˵£¬Óû§ÔÚ±àÒëÄں˵Ĺý³ÌÖУ¬¿ÉÒÔ°´ÕÕ×Ô¼ºµÄÐèÒª£¬¾ö¶¨ÊÇ·ñÒªÔÚ×Ô¼ºµÄÄÚºËÖбàÒë½øÈ¥ netfilter µÄ kernel Ö§³Ö¡£Õâ¾Í´ø¸øÎÒÃÇÒ»¸öÌáʾ£¬ÊµÏÖ netfilter µÄ´úÂë¶ÔÓÚʵÏÖ IPv4 ЭÒéÕ»µÄ´úÂëµÄÓ°ÏìÓ¦¸Ã»áÊǾ¡Á¿µÄС£¬²»ÄÇôÒýÈËעĿ²Å¶Ô¡£·ñÔòµÄ»°£¬IPv4 ЭÒéÕ»µÄ´úÂëά»¤¹¤×÷¾Í²»µÃ²»ºÍʵÏÖ netfilter µÄ´úÂëµÄά»¤¹¤×÷½ÁÔÚÒ»Æð£¬ÈÃÈËÍ·ÌÛÁË¡£

ÊÂʵҲµÄÈ·Èç´Ë£¬IPv4 ЭÒéջΪÁËʵÏÖ¶Ô netfilter ¼Ü¹¹µÄÖ§³Ö£¬ÔÚ IP packet ÔÚ IPv4 ЭÒéÕ»ÉϵÄÓÎÀú·ÏßÖ®ÖУ¬×ÐϸѡÔñÁËÎå¸ö²Î¿¼µã¡£ÔÚÕâÎå¸ö²Î¿¼µãÉÏ£¬¸÷ÒýÈëÁËÒ»ÐÐ¶Ô NF_HOOK() ºêº¯ÊýµÄÒ»¸öÏàÓ¦µÄµ÷Óá£ÕâÎå¸ö²Î¿¼µã±»·Ö±ðÃüÃûΪ PREROUTING£¬LOCAL-IN£¬FORWARD£¬LOCAL-OUT ºÍ POSTROUTING¡£¹ØÓÚÕâÎå¸ö²Î¿¼µãµÄº¬Ò壬ÔÚ iptables µÄʹÓÃ˵Ã÷ÖÐÓÐ׼ȷµÄÐðÊö£¬ÏàÐŶÁÕßÅóÓÑÃǶ¼Ó¦¸ÃÁ˽âÁË¡£´ÓÈçÏ嵀 grep Êä³ö£¬ÎÒÃÇ¿ÉÒÔ¿´µ½ IPv4 ЭÒéջʵÏÖ´úÂë¶Ô NF_HOOK() ºêº¯ÊýµÄµ÷Óãº


zhaoway@qhq ~/linux-2.4.19/net/ipv4 $ grep -n NF_HOOK *.c
arp.c:591:NF_HOOK(NF_ARP, NF_ARP_OUT, skb, NULL, dev, dev_queue_xmit);
arp.c:871:return NF_HOOK(NF_ARP, NF_ARP_IN, skb, dev, NULL, arp_process);
igmp.c:187:/* Don't just hand NF_HOOK skb->dst->output, in case netfilter hook
igmp.c:252:return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
ip_forward.c:145:return NF_HOOK(PF_INET, NF_IP_FORWARD, skb, skb->dev, dev2,
ip_gre.c:668:/* Need this wrapper because NF_HOOK takes the function address */
ip_input.c:302:return NF_HOOK(PF_INET, NF_IP_LOCAL_IN, skb, skb->dev, NULL,
ip_input.c:437:return NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, dev, NULL,
ip_output.c:111:/* Don't just hand NF_HOOK skb->dst->output, in case netfilter hook
ip_output.c:156:return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
ip_output.c:191:return NF_HOOK(PF_INET, NF_IP_POST_ROUTING, skb, NULL, dev,
ip_output.c:233:NF_HOOK(PF_INET, NF_IP_POST_ROUTING, newskb, NULL,
ip_output.c:249:NF_HOOK(PF_INET, NF_IP_POST_ROUTING, newskb, NULL,
ip_output.c:400:return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
ip_output.c:603:err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL,
ip_output.c:714:err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
ipip.c:516:/* Need this wrapper because NF_HOOK takes the function address */
ipmr.c:1211:NF_HOOK(PF_INET, NF_IP_FORWARD, skb2, skb->dev, dev,
zhaoway@qhq ~/linux-2.4.19/net/ipv4 $

NF_HOOK() Õâ¸öºêº¯Êý£¬¶¨ÒåÔÚ linux-2.4.19/include/linux/netfilter.h ÀïÃæ¡£µ± #ifdef CONFIG_NETFILTER ±»¶¨ÒåµÄʱºò£¬¾Íתȥµ÷Óà nf_hook_slow() º¯Êý£»Èç¹û CONFIG_NETFILTER ûÓỶ¨Ò壬Ôò´Ó netfilter Ä£¿éת»Øµ½ IPv4 ЭÒéÕ»£¬¼ÌÐøÍùÏ´¦Àí¡£ÕâÑù¾Í¸øÁËÓû§ÔÚ±àÒë kernel µÄʱºòÒ»¸öÑ¡Ï¿ÉÒÔͨ¹ý¶¨Òå CONFIG_NETFILTER Óë·ñÀ´¾ö¶¨ÊÇ·ñ°Ñ netfilter Ö§³Ö´úÂë±àÒë½øÄںˡ£´ÓÕâ¸öº¯ÊýµÄÃû³Æ£¬ÎÒÃÇÒ²¿ÉÒԲµ½£¬¿ÉÒÔ°Ñ IPv4 ЭÒéÕ»ÉϵÄÕâÎå¸ö²Î¿¼µã£¬ÐÎÏóµÄ¿´³ÉÊÇÎå¸ö¹³×Ó¡£IP packet ÔÚ IPv4 ЭÒéÕ»ÉÏÓÎÀúµÄʱºò£¬Í¾¾­ÕâÎå¸ö¹³×Ó£¬¾Í»á±» netfilter Ä£¿éµöÉÏÀ´£¬Éó²éÒ»·¬£¬²¢¾ÝÉó²éµÄ½á¹û£¬¾ö¶¨ packet µÄÏÂÒ»²½ÃüÔË£ºÊDZ»Ô­·â²»¶¯µÄ·Å»Ø IPv4 ЭÒéÕ»£¬¼ÌÐøÓÎÀú£»»¹ÊǾ­¹ýһЩÐ޸ģ¬ÔÙ·Å»ØÈ¥£»»¹ÊǸɴඪÆúµôËãÁË£¿

netfilter µÄºËÐÄÄ£¿é

¡°Óã¹³¡±ºÍ¡°´¹µöµã¡±

IP packet ±» NF_HOOK() ´Ó IPv4 ЭÒéÕ»Éϵö³öÀ´ÒԺ󣬾ͽøÈë linux-2.4.19/net/core/netfilter.c ÖÐµÄ nf_hook_slow() º¯Êý½øÐд¦Àí¡£Õâ¸öº¯Êý¸ÉµÄÖ÷ÒªÊÂÇ飬¾ÍÊǸù¾Ý nf_hooks[] Êý×飬¿ªÊ¼´¦Àí packet¡£×¼È·µØ˵À´£¬ÉÏÒ»¶Î½²µ½µÄ IPv4 ЭÒéÕ»ÉϵÄÎå¸ö²Î¿¼µã£¬²¢²»ÊÇ¡°µöÓãµÄ¹³×Ó¡±£¬¶øÊÇ¡°ÔÊÐí´¹µöµÄµØµã¡±¡£»»¾ä»°Ëµ£¬IPv4 ЭÒéÕ»É϶¨ÒåÁËÎå¸ö¡°ÔÊÐí´¹µöµã¡±¡£ÔÚÿһ¸ö¡°´¹µöµã¡±£¬¶¼¿ÉÒÔÈà netfilter ·ÅÖÃÒ»¸ö¡°Óã¹³¡±£¬°Ñ¾­¹ýµÄ packet µöÉÏÀ´¡£ÄÇô netfiler µÄ¡°Óã¹³¡±¶¼·ÅÔÚʲôµØ·½£¿¾Í·ÅÔÚ nf_hooks[][] Êý×éÀïÃæ¡£Õâ¸ö¡°Óã¹³¡±Óà linux-2.4.19/include/linux/netfilter.h Öж¨ÒåµÄÈçÏ struct ÓèÒÔÃèÊö£º


struct nf_hook_ops
{
struct list_head list;
nf_hookfn *hook;
int pf;
int hooknum;
int priority;
};

ÎÒÃÇ¿´µ½£¬¡°Óã¹³¡±µÄ±¾ÖÊ£¬ÊÇÒ»¸ö nf_hookfn º¯Êý¡£Õâ¸öº¯Êý½«¶Ô±»µöÉÏÀ´µÄ IP packet ½øÐгõ²½µÄ´¦Àí¡£ÄÇô£¬ÕâЩ¡°Óã¹³¡±ÊÇÓÉË­À´·ÅÖõ½ nf_hooks[][] Êý×éÀïÃæµÄÄØ£¿´ð°¸ÊÇ£¬¸÷¸ö table¡£ÊìϤ iptables ¹ÜÀí¹¤¾ßµÄ¶ÁÕßÅóÓÑÃÇÓ¦¸ÃÁ˽⣬һ¸ö table ¾ÍÊÇÒ»×éÀàËƵķÀ»ðǽ rules µÄ¼¯ºÏ¡£iptables ÀïÃæĬÈ϶¨ÒåÁËÈý¸ö table£ºfilter£¬mangle£¬ºÍ nat¡£¾Ù filter table ΪÀý£¬ËüÊÇÔÚ linux-2.4.19/net/ipv4/netfilter/iptable_filter.c ÖÐʵÏÖµÄÒ»¸ö kernel module¡£ÔÚÕâ¸ö module µÄ³õʼ»¯¹ý³ÌÖУ¬Ëü»áµ÷Óà nf_register_hook() Ïò netfilter µÄºËÐÄ´úÂë×¢²áÒ»×é¡°Óã¹³¡±¡£Õâ¸ö×¢²á¹ý³Ì£¬Êµ¼ÊÉÏ£¬Ò²¾ÍÊÇ°Ñ¡°Óã¹³¡±·Åµ½¡°´¹µöµã¡±µÄ¹ý³Ì¡£¡°´¹µöµã¡±µÄ¾ßÌåλÖã¬ÓÉ nf_hooks[][] Êý×éµÄϱê¾ßÌå˵Ã÷¡£

ipt_do_table()

ÎÒÃǾßÌå¿´µ½ linux-2.4.19/net/ipv4/netfilter/iptable_filter.c Ò²¾ÍÊÇ filter table µÄʵÏÖ´úÂ룬¾Í·¢ÏÖ filter table Öеġ°Óã¹³¡±É쵀 nf_hookfn º¯Êý£¬Ö÷ÒªÊÇÔÚµ÷Óà ipt_do_table() º¯Êý¡£ÕâÊÇÒ»¸ö¶¨ÒåÔÚ linux-2.4.19/net/ipv4/netfilter/ip_tables.c Öеĺ¯Êý¡£Ç°ÃæÌáµ½¹ý£¬Ò»¸ö table ¾ÍÊÇÒ»×é·À»ðǽ rules µÄ¼¯ºÏ¡£ÏÔÈ»£¬ipt_do_table() º¯Êý½«Òª×öµÄÊÂÇ飬¾ÍÊÇ°´ÕÕ table Öд洢µÄÒ»ÌõÓÖÒ»ÌõµÄ rules À´´¦Àí±»¡°µö¡±ÉÏÀ´µÄ IP packet¡£

table ÀïÃæ´æ·ÅÁËÕâ¸ö table ÖÐËùÓеķÀ»ðǽ rules¡£µ«ÊDz¢²»ÊÇËùÓÐµÄ rules ¶¼ÒªÄùýÀ´£¬°´ÕÕËüÉó²éÒ»ÏÂÕâ¸ö packet¡£ÊÂʵÉÏ£¬Õâ¸ö packet ÊÇ´ÓÄĸö¡°Óã¹³¡±Éϱ»µöÉÏÀ´µÄ£¬¾ÍÖ»ÓкÍÄǸö¡°Óã¹³¡±Ïà¹ØµÄ rules ²Å±»ÄùýÀ´£¬ÓÃÀ´Éó²éÕâ¸ö packet¡£Õâ¸ö»úÖÆ£¬¾ÍΪÿ¸ö table ʵÏÖÁ˶à¸ö chain£¬¶øÿ¸ö chain ÉÏÓÖÓжà¸ö rules¡£¶øÇÒ£¬ÎÒÃÇÁ¢¿Ì¿´µ½£¬Ò»¸ö chain ÊÇºÍ IPv4 ЭÒéÕ»ÉϵÄÒ»¸ö¡°´¹µöµã¡±Ïà¶ÔÓ¦µÄ¡£ÊìϤ iptables Óû§¿Õ¼ä¹ÜÀí¹¤¾ßµÄʹÓõĶÁÕßÅóÓÑÃÇÓ¦¸ÃÁ¢¿Ì¾Í»á×¢Òâµ½ÕâÒ»µãÁË¡£

ÔÚ linux-2.4.19/include/linux/netfilter_ipv4/ip_tables.h Öж¨ÒåÁË table ÖÐµÄ rule µÄ´æ·Å¸ñʽ£¬ÈçÏ£º


/* This structure defines each of the firewall rules. Consists of 3
parts which are 1) general IP header stuff 2) match specific
stuff 3) the target to perform if the rule matches */
struct ipt_entry
{
struct ipt_ip ip;
/* Mark with fields that we care about. */
unsigned int nfcache;
/* Size of ipt_entry + matches */
u_int16_t target_offset;
/* Size of ipt_entry + matches + target */
u_int16_t next_offset;
/* Back pointer */
unsigned int comefrom;
/* Packet and byte counters. */
struct ipt_counters counters;
/* The matches (if any), then the target. */
unsigned char elems[0];
};

Ò»¸ö entry ¾ÍÊÇÒ»¸ö rule¡£Ò»¸ö entry Ö÷ÒªÓÉÁ½²¿·Ö×é³É¡£Ò»²¿·Ö£¬ÊÇһϵÁÐµÄ matches£»ÁíÒ»²¿·Ö£¬ÊÇÒ»¸ö target¡£ÕâÈô¸É¸ö match ËùÒª»Ø´ðµÄÎÊÌ⣬ÊÇÏà¹ØµÄ packet ºÍ±¾Ìõ rule ÊÇ·ñÆ¥Åä¡£¶øÕâ¸ö target ËùÒª»Ø´ðµÄÎÊÌ⣬ÊÇÒ»µ© packet Æ¥ÅäÉÏÒԺ󣬸ÃÄÃÕâ¸ö packet Ôõô°ì£¿Ò²¾ÍÊÇÒªÓÉ target À´¾ö¶¨Õâ¸öÆ¥ÅäµÄ packet ½ñºóµÄÃüÔËÁË¡£¿ªÍ·µÄ struct ipt_ip µÄ¶¨ÒåÈçÏ£º


struct ipt_ip {
/* Source and destination IP addr */
struct in_addr src, dst;
/* Mask for src and dest IP addr */
struct in_addr smsk, dmsk;
char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
/* Protocol, 0 = ANY */
u_int16_t proto;
/* Flags word */
u_int8_t flags;
/* Inverse flags */
u_int8_t invflags;
};

ÎÒÃÇÁ¢¿Ì¿ÉÒÔ¿´³öÀ´£¬ÔÚ struct ipt_ip ÀïÃæ¼Ç¼Á˹ØÓÚÕâ¸ö rule ËùҪƥÅ䣨match£©µÄ packet µÄһЩÌØÕ÷¡£

match ºÍ target

netfilter ºËÐIJ¿·ÖÌṩÁËÒ»¸ö·ÖÎö¡¢´¦Öà packet µÄ¼Ü¹¹£¬µ«ÊǺËÐIJ¿·Ö´úÂë²¢²»¾ßÌåµÄÈ¥·ÖÎö¡¢´¦Öà packet¡£Õâ¸ö¾ßÌåµÄ·ÖÎö¡¢´¦ÖõÄÈÎÎñ±»½»¸øÆäËüµÄ module À´Íê³É¡£ºËÐIJ¿·Ö´úÂë¿ÉÒÔ¸ù¾Ý table ÖмǼµÄ rules ÐÅÏ¢£¬À´°Ñ packet ½»¸øÄܹ»´¦ÀíÏàÓ¦µÄ rules µÄ module ´úÂë¡£ÄÇô£¬ºËÐÄ´úÂëÈçºÎÁ˽âÄÄÒ»¸ö module ¿ÉÒÔ´¦ÀíÄÄÒ»ÀàµÄ rules µÄÄØ£¿ÕâÒªÓɸ÷¸öÏàÓ¦µÄ modules Æ𶯵Äʱºò£¬Ö÷¶¯È¥ÏòºËÐÄ´úÂë×¢²á£¬ipt_register_target() »òÕßÊÇ ipt_register_match()¡£Õâ¸ö×¢²á¹ý³Ì£¬Ö÷Òª¾ÍÊÇ֪ͨºËÐÄ´úÂ룬±¾ module ÓÐÒ»¸ö target() º¯Êý£¬¿ÉÒÔ¾ö¶¨ packet µÄÃüÔË£»»òÕßÊÇ£¬±¾ module ÓÐÒ»¸ö match() º¯Êý£¬¿ÉÒÔÅж¨Ò»¸ö packet ÊÇ·ñ·ûºÏ rules µÄÆ¥ÅäÒªÇó¡£

Õâ¾ÍÌáʾÎÒÃÇ£¬Èç¹ûҪд×Ô¼ºµÄ·À»ðǽģ¿é£¬ÏâǶÔÚ netfilter µÄ¼Ü¹¹ÖеĻ°£¬ÎÒÃÇÖ÷ÒªÒª×öµÄÈÎÎñ£¬¾ÍÊÇÏò netfilter ºËÐÄ×¢²á ipt_register_target() »òÕß ipt_register_match()¡£

iptables ¹ÜÀí¹¤¾ß

×îºó£¬ÒªËµÃ÷µÄÊÇ iptables£¬Õâ¸öλÓÚÓû§¿Õ¼äµÄ¹ÜÀí¹¤¾ß¡£Ç°ÃæÎÒÃÇ¿´µ½ÁË£¬netfilter ÔÚÄں˿ռäµÄ´úÂë¸ù¾Ý table ÖÐµÄ rules£¬Íê³É¶Ô packet µÄ·ÖÎöºÍ´¦Öᣵ«ÊÇÕâЩ table ÖеľßÌåµÄ·À»ðǽ rules£¬»¹ÊDZØÐëÓÉϵͳ¹ÜÀíÔ±Ç××Ô±àд¡£kernel ÖÐµÄ netfilter Ö»ÊÇÌṩÁËÒ»¸ö»úÖÆ£¬Ëü²¢²»ÖªµÀ¸ÃÔõÑùÀûÓÃÕâ¸ö»úÖÆ£¬Ð´³öºÏÊ浀 rules£¬À´ÊµÏÖÒ»¸öÍøÂç·À»ðǽ¡£ÄÇô£¬ÏµÍ³¹ÜÀíÔ±±àдµÄ rules£¬ÔõÑù½øÈëλÓÚ kernel ¿Õ¼äÖÐµÄ netfilter ά»¤µÄ table ÖÐÈ¥ÄØ£¿

Õâ¸öÈÎÎñÊÇÓÉ iptables Õâ¸ö¹¤¾ßÀ´Íê³ÉµÄ¡£Ëü¾­¹ý getsockopt() ÒÔ¼° setsockopt() Á½¸öϵͳµ÷Ó㬽øÈë kernel ¿Õ¼ä¡£ÕâÁ½¸öµ÷ÓÃÊÇ BSD Socket ½Ó¿ÚµÄÒ»²¿·Ö¡£ÕâÀïÃæµÄÎÊÌâÊÇ IPv4 ÔÚ½Óµ½¹ØÓÚij¸ö sock µÄ²»ÈÏʶµÄ opt µÄʱºò£¬Ó¦¸ÃÔõô´¦Àí£¿netfilter ÒªÇóËüÔÚ linux-2.4.19/net/ipv4/ip_sockglue.c ÎļþÖд¦Àí getsockopt() ºÍ setsockopt() ϵͳµ÷ÓÃµÄ ip_sockopt() º¯ÊýÖÐÊʵ±µÄµØ·½µ÷Óà nf_sockopt()¡£ÕâÑù£¬Óû§¿Õ¼ä¾Í¿ÉÒÔºÍ netfilter ºËÐIJ¿·Ö½øÐн»Á÷£¬¿ÉÒÔά»¤ table ÖеķÀ»ðǽ rules ÁË¡£

С½á

netfilter ¶ÔÓÚ IPv4 µÄÐ޸ķdz£Ð¡£¬Ò»ÊÇÔÚÈô¸É¸öµØ·½µ÷ÓÃÁË NF_HOOK()£¬¶þÊÇÔÚ ip_sockopt() Öе÷ÓÃÁË nf_sockopt()¡£netfilter µÄºËÐIJ¿·Ö´úÂëÖ»ÊÇά»¤ table£¬½âÊÍ table µÄÈÎÎñÔÚÓÚÆäËüµÄ kernel module¡£netfilter »á°Ñ´Ó hook ¡°µö¡±ÆðÀ´µÄ packet ÒÔ¼° table ÀïÃæµÄÏà¹ØÄÚÈÝ·¢¸ø×¢²áÁ赀 module£¬¾ö¶¨ packet µÄÃüÔË¡£

²Î¿¼×ÊÁÏ

1 netfilter/iptables Ö÷Õ¾µãÔÚ http://www.netfilter.org/ »òÕß http://www.iptables.org/ ÔÚÕâ¸öÕ¾µãÉÏ£¬¿ÉÒÔÕÒµ½ netfilter ºËÐÄ¿ª·¢Õß Paul Russell дµÄ Linux Packet Filtering HOW-TO£¬Linux NAT HOW-TO µÈ¹ØÓÚÔõÑù²¿Êð Linux netfilter ·À»ðǽµÄ¼¼ÊõÎÄÕ¡£

2 Linux ÄÚºËÔ´´úÂëµÄÔÚÏß½»²æË÷ÒýÔÚ http://lxr.linux.no/ Õâ¸öÕ¾µã¿ÉÒÔ°ïÖú¶ÁÕßÅóÓÑÃǸü¼Ó·½±ãµÄÔĶÁ Linux kernel µÄÔ´³ÌÐò¡£

¹ØÓÚ×÷Õß

ÕÔε£¬×¡ÔÚÄϾ©ÊÐµÄ Linux/Free Software ¶ÀÁ¢¼¼Êõ¹ËÎÊ¡£ÔÚ IBM developerWorks ·¢±í¹ý¶àƪÖÐÎÄÎÄÕ¡£¹ØÓÚËûÔÚÍøÂçÉÏ·¢±íµÄ¼¼ÊõÎÄÕµÄÒ»·ÝÇåµ¥£¬¿ÉÒÔÔÚ http://www.advogato.org/person/zhaoway/ ÕÒµ½¡£Ëû¾­³£¹â¹ËÄϾ©´óѧС°ÙºÏ BBS É쵀 LinuxUnix °æ¡£ÄÏ´óС°ÙºÏµÄÍøÖ·ÔÚ http://bbs.nju.edu.cn/ ÕÔεÔÚÄÏ´óС°ÙºÏÉÏÃæµÄ ID ÊÇ iloveqhq »¶Ó­¶ÁÕßÅóÓÑÃǺÍËûÌÖÂÛ Linux/Free Software Ïà¹ØµÄ¸÷ÖÖ¼¼ÊõÎÊÌâ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ