2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012-2013 Pablo Neira Ayuso <pablo@netfilter.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
12 #include <linux/init.h>
13 #include <linux/module.h>
15 #include <linux/netfilter_ipv4.h>
16 #include <net/netfilter/nf_tables.h>
17 #include <net/net_namespace.h>
19 #include <net/netfilter/nf_tables_ipv4.h>
21 static unsigned int nft_do_chain_ipv4(const struct nf_hook_ops
*ops
,
23 const struct nf_hook_state
*state
)
25 struct nft_pktinfo pkt
;
27 nft_set_pktinfo_ipv4(&pkt
, ops
, skb
, state
);
29 return nft_do_chain(&pkt
, ops
);
32 static unsigned int nft_ipv4_output(const struct nf_hook_ops
*ops
,
34 const struct nf_hook_state
*state
)
36 if (unlikely(skb
->len
< sizeof(struct iphdr
) ||
37 ip_hdr(skb
)->ihl
< sizeof(struct iphdr
) / 4)) {
39 pr_info("nf_tables_ipv4: ignoring short SOCK_RAW "
44 return nft_do_chain_ipv4(ops
, skb
, state
);
47 struct nft_af_info nft_af_ipv4 __read_mostly
= {
48 .family
= NFPROTO_IPV4
,
49 .nhooks
= NF_INET_NUMHOOKS
,
53 [NF_INET_LOCAL_IN
] = nft_do_chain_ipv4
,
54 [NF_INET_LOCAL_OUT
] = nft_ipv4_output
,
55 [NF_INET_FORWARD
] = nft_do_chain_ipv4
,
56 [NF_INET_PRE_ROUTING
] = nft_do_chain_ipv4
,
57 [NF_INET_POST_ROUTING
] = nft_do_chain_ipv4
,
60 EXPORT_SYMBOL_GPL(nft_af_ipv4
);
62 static int nf_tables_ipv4_init_net(struct net
*net
)
64 net
->nft
.ipv4
= kmalloc(sizeof(struct nft_af_info
), GFP_KERNEL
);
65 if (net
->nft
.ipv4
== NULL
)
68 memcpy(net
->nft
.ipv4
, &nft_af_ipv4
, sizeof(nft_af_ipv4
));
70 if (nft_register_afinfo(net
, net
->nft
.ipv4
) < 0)
79 static void nf_tables_ipv4_exit_net(struct net
*net
)
81 nft_unregister_afinfo(net
->nft
.ipv4
);
85 static struct pernet_operations nf_tables_ipv4_net_ops
= {
86 .init
= nf_tables_ipv4_init_net
,
87 .exit
= nf_tables_ipv4_exit_net
,
90 static const struct nf_chain_type filter_ipv4
= {
92 .type
= NFT_CHAIN_T_DEFAULT
,
93 .family
= NFPROTO_IPV4
,
95 .hook_mask
= (1 << NF_INET_LOCAL_IN
) |
96 (1 << NF_INET_LOCAL_OUT
) |
97 (1 << NF_INET_FORWARD
) |
98 (1 << NF_INET_PRE_ROUTING
) |
99 (1 << NF_INET_POST_ROUTING
),
102 static int __init
nf_tables_ipv4_init(void)
106 nft_register_chain_type(&filter_ipv4
);
107 ret
= register_pernet_subsys(&nf_tables_ipv4_net_ops
);
109 nft_unregister_chain_type(&filter_ipv4
);
114 static void __exit
nf_tables_ipv4_exit(void)
116 unregister_pernet_subsys(&nf_tables_ipv4_net_ops
);
117 nft_unregister_chain_type(&filter_ipv4
);
120 module_init(nf_tables_ipv4_init
);
121 module_exit(nf_tables_ipv4_exit
);
123 MODULE_LICENSE("GPL");
124 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
125 MODULE_ALIAS_NFT_FAMILY(AF_INET
);