赞
踩
- struct nf_hook_ops {
- struct list_head list;
-
- /* User fills in from here down. */
- nf_hookfn *hook; //hook处理函数
- struct net_device *dev;
- void *priv;
- u_int8_t pf; //协议类型
- unsigned int hooknum; //hook点
- /* Hooks are ordered in ascending priority. */
- int priority; //优先级
- };
- /* Bridge Hooks */
- /* After promisc drops, checksum checks. */
- #define NF_BR_PRE_ROUTING 0
- /* If the packet is destined for this box. */
- #define NF_BR_LOCAL_IN 1
- /* If the packet is destined for another interface. */
- #define NF_BR_FORWARD 2
- /* Packets coming from a local process. */
- #define NF_BR_LOCAL_OUT 3
- /* Packets about to hit the wire. */
- #define NF_BR_POST_ROUTING 4
- enum nf_ip_hook_priorities {
- NF_IP_PRI_FIRST = INT_MIN,
- NF_IP_PRI_CONNTRACK_DEFRAG = -400,
- NF_IP_PRI_RAW = -300,
- NF_IP_PRI_SELINUX_FIRST = -225,
- NF_IP_PRI_CONNTRACK = -200,
- NF_IP_PRI_MANGLE = -150,
- NF_IP_PRI_NAT_DST = -100,
- NF_IP_PRI_FILTER = 0,
- NF_IP_PRI_SECURITY = 50,
- NF_IP_PRI_NAT_SRC = 100,
- NF_IP_PRI_SELINUX_LAST = 225,
- NF_IP_PRI_CONNTRACK_HELPER = 300,
- NF_IP_PRI_CONNTRACK_CONFIRM = INT_MAX,
- NF_IP_PRI_LAST = INT_MAX,
- };
- unsigned int nf_hookfn(void *priv,
- struct sk_buff *skb,
- const struct nf_hook_state *state);
每监控到一条数据包,就会调用一次次函数,数据包信息存储在sk_buff结构中,参考sk_buff相关介绍。这个函数的返回值决定了函数结束后此数据包的走向,有如下几种返回值:- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/netfilter.h>
- #include <linux/netfilter_bridge.h>
- #include <linux/netfilter_ipv4.h>
- #include <linux/ip.h>
-
- static void
- IP2Str(char *ipaddr, int size, uint32_t ip)
- {
- snprintf(ipaddr, size, "%d.%d.%d.%d", ( ip >> 24 ) & 0xff
- , ( ip >> 16 ) & 0xff
- , ( ip >> 8 ) & 0xff
- , ip & 0xff);
- }
-
- unsigned int
- my_hook_fun(void *priv, struct sk_buff *skb, const struct nf_hook_state *state)
- {
- struct iphdr *iph;
- char ipaddr[17];
-
- if( unlikely(!skb) ) {
- return NF_ACCEPT;
- }
-
- iph = ip_hdr(skb);
- if( unlikely(!iph) ) {
- return NF_ACCEPT;
- }
-
- if( likely(iph->protocol != IPPROTO_ICMP) ) {
- return NF_ACCEPT;
- }
-
- memset(ipaddr, 0, sizeof(ipaddr));
- IP2Str(ipaddr, sizeof(ipaddr), ntohl(iph->saddr));
- if( strcmp(ipaddr, "192.168.31.4") == 0 ) {
- printk(KERN_INFO "receive ping from 192.168.31.4\n");
- }
-
- return NF_ACCEPT;
- }
-
- static struct nf_hook_ops my_hook_ops = {
- .hook = my_hook_fun, //hook处理函数
- .pf = PF_INET, //协议类型
- .hooknum = NF_BR_PRE_ROUTING, //hook注册点
- .priority = NF_IP_PRI_FIRST, //优先级
- };
-
- static void
- hello_cleanup(void)
- {
- nf_unregister_hook(&my_hook_ops);
- }
-
- static __init int hello_init(void)
- {
-
- if ( nf_register_hook(&my_hook_ops) != 0 ) {
- printk(KERN_WARNING "register hook error!\n");
- goto err;
- }
- printk(KERN_ALERT "hello init success!\n");
- return 0;
-
- err:
- hello_cleanup();
- return -1;
- }
-
- static __exit void hello_exit(void)
- {
- hello_cleanup();
- printk(KERN_WARNING "helloworld exit!\n");
- }
-
- module_init(hello_init);
- module_exit(hello_exit);
-
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Stone");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。