Commit | Line | Data |
---|---|---|
96518518 PM |
1 | /* |
2 | * Copyright (c) 2008 Patrick McHardy <kaber@trash.net> | |
9370761c | 3 | * Copyright (c) 2012-2013 Pablo Neira Ayuso <pablo@netfilter.org> |
96518518 PM |
4 | * |
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. | |
8 | * | |
9 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
10 | */ | |
11 | ||
12 | #include <linux/init.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/ip.h> | |
15 | #include <linux/netfilter_ipv4.h> | |
16 | #include <net/netfilter/nf_tables.h> | |
99633ab2 | 17 | #include <net/net_namespace.h> |
96518518 | 18 | #include <net/ip.h> |
0ca743a5 | 19 | #include <net/netfilter/nf_tables_ipv4.h> |
96518518 | 20 | |
3b088c4b PM |
21 | static unsigned int nft_do_chain_ipv4(const struct nf_hook_ops *ops, |
22 | struct sk_buff *skb, | |
23 | const struct net_device *in, | |
24 | const struct net_device *out, | |
25 | int (*okfn)(struct sk_buff *)) | |
26 | { | |
27 | struct nft_pktinfo pkt; | |
28 | ||
29 | nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out); | |
30 | ||
31 | return nft_do_chain_pktinfo(&pkt, ops); | |
32 | } | |
33 | ||
96518518 PM |
34 | static unsigned int nft_ipv4_output(const struct nf_hook_ops *ops, |
35 | struct sk_buff *skb, | |
36 | const struct net_device *in, | |
37 | const struct net_device *out, | |
38 | int (*okfn)(struct sk_buff *)) | |
39 | { | |
40 | if (unlikely(skb->len < sizeof(struct iphdr) || | |
41 | ip_hdr(skb)->ihl < sizeof(struct iphdr) / 4)) { | |
42 | if (net_ratelimit()) | |
43 | pr_info("nf_tables_ipv4: ignoring short SOCK_RAW " | |
44 | "packet\n"); | |
45 | return NF_ACCEPT; | |
46 | } | |
47 | ||
3b088c4b | 48 | return nft_do_chain_ipv4(ops, skb, in, out, okfn); |
96518518 PM |
49 | } |
50 | ||
1d49144c | 51 | struct nft_af_info nft_af_ipv4 __read_mostly = { |
96518518 PM |
52 | .family = NFPROTO_IPV4, |
53 | .nhooks = NF_INET_NUMHOOKS, | |
54 | .owner = THIS_MODULE, | |
115a60b1 | 55 | .nops = 1, |
96518518 | 56 | .hooks = { |
3b088c4b | 57 | [NF_INET_LOCAL_IN] = nft_do_chain_ipv4, |
96518518 | 58 | [NF_INET_LOCAL_OUT] = nft_ipv4_output, |
3b088c4b PM |
59 | [NF_INET_FORWARD] = nft_do_chain_ipv4, |
60 | [NF_INET_PRE_ROUTING] = nft_do_chain_ipv4, | |
61 | [NF_INET_POST_ROUTING] = nft_do_chain_ipv4, | |
96518518 PM |
62 | }, |
63 | }; | |
1d49144c | 64 | EXPORT_SYMBOL_GPL(nft_af_ipv4); |
96518518 | 65 | |
99633ab2 PNA |
66 | static int nf_tables_ipv4_init_net(struct net *net) |
67 | { | |
68 | net->nft.ipv4 = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL); | |
69 | if (net->nft.ipv4 == NULL) | |
70 | return -ENOMEM; | |
71 | ||
72 | memcpy(net->nft.ipv4, &nft_af_ipv4, sizeof(nft_af_ipv4)); | |
73 | ||
74 | if (nft_register_afinfo(net, net->nft.ipv4) < 0) | |
75 | goto err; | |
76 | ||
77 | return 0; | |
78 | err: | |
79 | kfree(net->nft.ipv4); | |
80 | return -ENOMEM; | |
81 | } | |
82 | ||
83 | static void nf_tables_ipv4_exit_net(struct net *net) | |
84 | { | |
85 | nft_unregister_afinfo(net->nft.ipv4); | |
86 | kfree(net->nft.ipv4); | |
87 | } | |
88 | ||
89 | static struct pernet_operations nf_tables_ipv4_net_ops = { | |
90 | .init = nf_tables_ipv4_init_net, | |
91 | .exit = nf_tables_ipv4_exit_net, | |
92 | }; | |
0ca743a5 | 93 | |
9370761c PNA |
94 | static struct nf_chain_type filter_ipv4 = { |
95 | .family = NFPROTO_IPV4, | |
96 | .name = "filter", | |
97 | .type = NFT_CHAIN_T_DEFAULT, | |
88ce65a7 | 98 | .me = THIS_MODULE, |
9370761c PNA |
99 | .hook_mask = (1 << NF_INET_LOCAL_IN) | |
100 | (1 << NF_INET_LOCAL_OUT) | | |
101 | (1 << NF_INET_FORWARD) | | |
102 | (1 << NF_INET_PRE_ROUTING) | | |
103 | (1 << NF_INET_POST_ROUTING), | |
9370761c PNA |
104 | }; |
105 | ||
96518518 PM |
106 | static int __init nf_tables_ipv4_init(void) |
107 | { | |
9370761c | 108 | nft_register_chain_type(&filter_ipv4); |
99633ab2 | 109 | return register_pernet_subsys(&nf_tables_ipv4_net_ops); |
96518518 PM |
110 | } |
111 | ||
112 | static void __exit nf_tables_ipv4_exit(void) | |
113 | { | |
99633ab2 | 114 | unregister_pernet_subsys(&nf_tables_ipv4_net_ops); |
9370761c | 115 | nft_unregister_chain_type(&filter_ipv4); |
96518518 PM |
116 | } |
117 | ||
118 | module_init(nf_tables_ipv4_init); | |
119 | module_exit(nf_tables_ipv4_exit); | |
120 | ||
121 | MODULE_LICENSE("GPL"); | |
122 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
123 | MODULE_ALIAS_NFT_FAMILY(AF_INET); |