下面是防火墙模块的初始化函数:
- int init_firewall()
- {
- (&ip_allowed_in_node_head)->next = NULL;
- (&ip_denied_in_node_head)->next = NULL;
- (&port_allowed_in_node_head)->next = NULL;
- (&port_denied_in_node_head)->next = NULL;
- (&ip_allowed_out_node_head)->next = NULL;
- (&ip_denied_out_node_head)->next = NULL;
- (&port_allowed_out_node_head)->next = NULL;
- (&port_denied_out_node_head)->next = NULL;
- switch(work_mode)
- {
- case MODE_IP_ONLY_ALLOWED_IN:
- open_ip_cfg_file(ALLOWED_IN_IP_CONF_FILE_DIR,ALLOWED_IP_IN);
- break;
- case MODE_IP_ONLY_DENIED_IN:
- open_ip_cfg_file(DENIED_IN_IP_CONF_FILE_DIR,DENIED_IP_IN);
- break;
- case MODE_IP_PORT_ALLOWED_IN:
- open_port_cfg_file(ALLOWED_IN_PORT_CONF_FILE_DIR,ALLOWED_PORT_IN);
- open_ip_cfg_file(ALLOWED_IN_IP_CONF_FILE_DIR,ALLOWED_IP_IN);
- break;
- case MODE_IP_PORT_DENIED_IN:
- open_port_cfg_file(DENIED_IN_PORT_CONF_FILE_DIR,DENIED_PORT_IN);
- open_ip_cfg_file(ALLOWED_IN_IP_CONF_FILE_DIR,ALLOWED_IP_IN);
- break;
- case MODE_IP_ONLY_ALLOWED_OUT:
- open_ip_cfg_file(ALLOWED_OUT_IP_CONF_FILE_DIR,ALLOWED_IP_OUT);
- break;
- case MODE_IP_ONLY_DENIED_OUT:
- open_ip_cfg_file(DENIED_OUT_IP_CONF_FILE_DIR,DENIED_IP_OUT);
- break;
- case MODE_IP_PORT_ALLOWED_OUT:
- open_port_cfg_file(ALLOWED_OUT_PORT_CONF_FILE_DIR,ALLOWED_PORT_OUT);
- open_ip_cfg_file(ALLOWED_OUT_IP_CONF_FILE_DIR,ALLOWED_IP_OUT);
- break;
- case MODE_IP_PORT_DENIED_OUT:
- open_port_cfg_file(DENIED_OUT_PORT_CONF_FILE_DIR,DENIED_PORT_OUT);
- open_ip_cfg_file(ALLOWED_OUT_IP_CONF_FILE_DIR,ALLOWED_IP_OUT);
- break;
- default:break;
- }
- //open_port_cfg_file(DENIED_PORT_CONF_FILE,DENIED_PORT);
- nf_register_hook(&my_netfilter[0]);
- nf_register_hook(&my_netfilter[1]);
- printk("INIT my firewall OK!\n");
- return 0;
- }
下图是Netfilter的IPV4下的结构

上述的两个函数挂载位置NF_INET_LOCAL_IN和NF_INET_LOCAL_OUT,分别处理从本机发出和到达本机的数据包。
