nfnetfilter_queue 模块的工作原理

来源:互联网 发布:java httpget下载文件 编辑:程序博客网 时间:2024/04/27 23:12

// ip包输入流(net/ipv4/ip_input.cz):

ip_rcv() -> HF_HOOK(NF_INET_PRE_ROUTING) -> ip_route_input -> NF_HOOK(NF_INET_LOCAL_IN) -> NF_HOOK_THRESH() -> nf_hook_thresh() ->nf_hook_slow() -> nf_queue() -> __nf_queue() -> [nf_queue_handler.outfn()] ->nfqnl_enqueue_packet() -> nfnetlink_unicast() & __enqueue_entry()**


// 注册nfnetlink_queue模块 (net/netfilter/netfilter_queue.c):

nfnetlink_queue_init() -> nfnetlink_subsys_register(nfnetlink_subsystem)


// nfnetlick消息接收 (net/netfilter/nfnetlink.c):

nfnetlink_rcv() -> netlink_rcv_skb() -> [nfnetlink_subsystem.cb()] ->nfqnl_recv_config() | nfqnl_recv_verdict() -> nf_register_queue_handler() | nf_reinject()**


// iptable_filter表注册xt_table,将xt_table保存到每个net的net.xt.tables[af],并将net.ipv4.iptable_filter的值指向xt_table,net/ipv4/netfilter/iptable_filter.c:

iptable_filter_init() -> register_pernet_subsys() -> register_pernet_operations() ->pernet_operations.init(net) -> iptable_filter_net_init(net) ->ipt_register_table(net,xt_table, ipt_replace) ->xt_register_table(net, xt_table, ipt_replace) -> xt_register_table(net, xt_table, xt_table_info)


// iptable_filter注册HOOK,net/ipv4/netfilter/iptable_filter.c:

nf_register_hooks() -> ipt_local_out_hook()


// iptable_filter HOOK函数回调,net/ipv4/ip_tables.c:

ipt_do_table(net.ipv4.iptable_filter) ->ip_packet_match(iphdr)、IPT_MATCH_ITERATE(do_match(ipt_entry_match)) -> xt_match_param -> xt_match.match()、ipt_entry_target -> xt_target_param -> xt_target.target()


// match的注册,net/netfilter/xt_owner.c:

owner_mt_init() -> xt_register_matches() -> xt_register_match() -> xt[af].match


// 用户层iptc与内核层iptable的通信,内核层注册SOCK_RAW sockopt, net/ipv4/netfilter/ip_tables.c:

ip_tables_init() -> nf_register_sockopt() -> nf_sockopts添加struct nf_sockopt_ops


// 用户层iptc与内核层iptable的通信,内核调用过程, net/ipv4/raw.c

struct proto raw_prot.setsockopt -> raw_setsockopt(struct sock) -> ip_setsockopt(struct sock) ->nf_setsockopt(struct sock) ->  nf_sockopt(struct sock) -> nf_sockopt_find