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