Merge tag 'for-linus' of git://git.code.sf.net/p/openipmi/linux-ipmi
[cascardo/linux.git] / net / bridge / netfilter / nf_tables_bridge.c
1 /*
2  * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3  * Copyright (c) 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/netfilter_bridge.h>
15 #include <net/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables_bridge.h>
17 #include <linux/ip.h>
18 #include <linux/ipv6.h>
19 #include <net/netfilter/nf_tables_ipv4.h>
20 #include <net/netfilter/nf_tables_ipv6.h>
21
22 int nft_bridge_iphdr_validate(struct sk_buff *skb)
23 {
24         struct iphdr *iph;
25         u32 len;
26
27         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
28                 return 0;
29
30         iph = ip_hdr(skb);
31         if (iph->ihl < 5 || iph->version != 4)
32                 return 0;
33
34         len = ntohs(iph->tot_len);
35         if (skb->len < len)
36                 return 0;
37         else if (len < (iph->ihl*4))
38                 return 0;
39
40         if (!pskb_may_pull(skb, iph->ihl*4))
41                 return 0;
42
43         return 1;
44 }
45 EXPORT_SYMBOL_GPL(nft_bridge_iphdr_validate);
46
47 int nft_bridge_ip6hdr_validate(struct sk_buff *skb)
48 {
49         struct ipv6hdr *hdr;
50         u32 pkt_len;
51
52         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
53                 return 0;
54
55         hdr = ipv6_hdr(skb);
56         if (hdr->version != 6)
57                 return 0;
58
59         pkt_len = ntohs(hdr->payload_len);
60         if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
61                 return 0;
62
63         return 1;
64 }
65 EXPORT_SYMBOL_GPL(nft_bridge_ip6hdr_validate);
66
67 static inline void nft_bridge_set_pktinfo_ipv4(struct nft_pktinfo *pkt,
68                                                const struct nf_hook_ops *ops,
69                                                struct sk_buff *skb,
70                                                const struct net_device *in,
71                                                const struct net_device *out)
72 {
73         if (nft_bridge_iphdr_validate(skb))
74                 nft_set_pktinfo_ipv4(pkt, ops, skb, in, out);
75         else
76                 nft_set_pktinfo(pkt, ops, skb, in, out);
77 }
78
79 static inline void nft_bridge_set_pktinfo_ipv6(struct nft_pktinfo *pkt,
80                                               const struct nf_hook_ops *ops,
81                                               struct sk_buff *skb,
82                                               const struct net_device *in,
83                                               const struct net_device *out)
84 {
85 #if IS_ENABLED(CONFIG_IPV6)
86         if (nft_bridge_ip6hdr_validate(skb) &&
87             nft_set_pktinfo_ipv6(pkt, ops, skb, in, out) == 0)
88                 return;
89 #endif
90         nft_set_pktinfo(pkt, ops, skb, in, out);
91 }
92
93 static unsigned int
94 nft_do_chain_bridge(const struct nf_hook_ops *ops,
95                     struct sk_buff *skb,
96                     const struct net_device *in,
97                     const struct net_device *out,
98                     int (*okfn)(struct sk_buff *))
99 {
100         struct nft_pktinfo pkt;
101
102         switch (eth_hdr(skb)->h_proto) {
103         case htons(ETH_P_IP):
104                 nft_bridge_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
105                 break;
106         case htons(ETH_P_IPV6):
107                 nft_bridge_set_pktinfo_ipv6(&pkt, ops, skb, in, out);
108                 break;
109         default:
110                 nft_set_pktinfo(&pkt, ops, skb, in, out);
111                 break;
112         }
113
114         return nft_do_chain(&pkt, ops);
115 }
116
117 static struct nft_af_info nft_af_bridge __read_mostly = {
118         .family         = NFPROTO_BRIDGE,
119         .nhooks         = NF_BR_NUMHOOKS,
120         .owner          = THIS_MODULE,
121         .nops           = 1,
122         .hooks          = {
123                 [NF_BR_PRE_ROUTING]     = nft_do_chain_bridge,
124                 [NF_BR_LOCAL_IN]        = nft_do_chain_bridge,
125                 [NF_BR_FORWARD]         = nft_do_chain_bridge,
126                 [NF_BR_LOCAL_OUT]       = nft_do_chain_bridge,
127                 [NF_BR_POST_ROUTING]    = nft_do_chain_bridge,
128         },
129 };
130
131 static int nf_tables_bridge_init_net(struct net *net)
132 {
133         net->nft.bridge = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
134         if (net->nft.bridge == NULL)
135                 return -ENOMEM;
136
137         memcpy(net->nft.bridge, &nft_af_bridge, sizeof(nft_af_bridge));
138
139         if (nft_register_afinfo(net, net->nft.bridge) < 0)
140                 goto err;
141
142         return 0;
143 err:
144         kfree(net->nft.bridge);
145         return -ENOMEM;
146 }
147
148 static void nf_tables_bridge_exit_net(struct net *net)
149 {
150         nft_unregister_afinfo(net->nft.bridge);
151         kfree(net->nft.bridge);
152 }
153
154 static struct pernet_operations nf_tables_bridge_net_ops = {
155         .init   = nf_tables_bridge_init_net,
156         .exit   = nf_tables_bridge_exit_net,
157 };
158
159 static const struct nf_chain_type filter_bridge = {
160         .name           = "filter",
161         .type           = NFT_CHAIN_T_DEFAULT,
162         .family         = NFPROTO_BRIDGE,
163         .owner          = THIS_MODULE,
164         .hook_mask      = (1 << NF_BR_PRE_ROUTING) |
165                           (1 << NF_BR_LOCAL_IN) |
166                           (1 << NF_BR_FORWARD) |
167                           (1 << NF_BR_LOCAL_OUT) |
168                           (1 << NF_BR_POST_ROUTING),
169 };
170
171 static int __init nf_tables_bridge_init(void)
172 {
173         int ret;
174
175         nft_register_chain_type(&filter_bridge);
176         ret = register_pernet_subsys(&nf_tables_bridge_net_ops);
177         if (ret < 0)
178                 nft_unregister_chain_type(&filter_bridge);
179
180         return ret;
181 }
182
183 static void __exit nf_tables_bridge_exit(void)
184 {
185         unregister_pernet_subsys(&nf_tables_bridge_net_ops);
186         nft_unregister_chain_type(&filter_bridge);
187 }
188
189 module_init(nf_tables_bridge_init);
190 module_exit(nf_tables_bridge_exit);
191
192 MODULE_LICENSE("GPL");
193 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
194 MODULE_ALIAS_NFT_FAMILY(AF_BRIDGE);