]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/bridge/netfilter/nf_tables_bridge.c
netfilter: Pass priv instead of nf_hook_ops to netfilter hooks
[mirror_ubuntu-artful-kernel.git] / net / bridge / netfilter / nf_tables_bridge.c
CommitLineData
96518518
PM
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
46413825 3 * Copyright (c) 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/netfilter_bridge.h>
15#include <net/netfilter/nf_tables.h>
68b0faa8
AN
16#include <net/netfilter/nf_tables_bridge.h>
17#include <linux/ip.h>
18#include <linux/ipv6.h>
1b63d4b9
AN
19#include <net/netfilter/nf_tables_ipv4.h>
20#include <net/netfilter/nf_tables_ipv6.h>
68b0faa8
AN
21
22int 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}
45EXPORT_SYMBOL_GPL(nft_bridge_iphdr_validate);
46
47int 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}
65EXPORT_SYMBOL_GPL(nft_bridge_ip6hdr_validate);
96518518 66
1b63d4b9 67static inline void nft_bridge_set_pktinfo_ipv4(struct nft_pktinfo *pkt,
1b63d4b9 68 struct sk_buff *skb,
073bfd56 69 const struct nf_hook_state *state)
1b63d4b9
AN
70{
71 if (nft_bridge_iphdr_validate(skb))
6aa187f2 72 nft_set_pktinfo_ipv4(pkt, skb, state);
1b63d4b9 73 else
6aa187f2 74 nft_set_pktinfo(pkt, skb, state);
1b63d4b9
AN
75}
76
77static inline void nft_bridge_set_pktinfo_ipv6(struct nft_pktinfo *pkt,
073bfd56
DM
78 struct sk_buff *skb,
79 const struct nf_hook_state *state)
1b63d4b9
AN
80{
81#if IS_ENABLED(CONFIG_IPV6)
82 if (nft_bridge_ip6hdr_validate(skb) &&
6aa187f2 83 nft_set_pktinfo_ipv6(pkt, skb, state) == 0)
1b63d4b9
AN
84 return;
85#endif
6aa187f2 86 nft_set_pktinfo(pkt, skb, state);
1b63d4b9
AN
87}
88
3b088c4b 89static unsigned int
06198b34 90nft_do_chain_bridge(void *priv,
3b088c4b 91 struct sk_buff *skb,
238e54c9 92 const struct nf_hook_state *state)
3b088c4b
PM
93{
94 struct nft_pktinfo pkt;
95
1b63d4b9
AN
96 switch (eth_hdr(skb)->h_proto) {
97 case htons(ETH_P_IP):
6aa187f2 98 nft_bridge_set_pktinfo_ipv4(&pkt, skb, state);
1b63d4b9
AN
99 break;
100 case htons(ETH_P_IPV6):
6aa187f2 101 nft_bridge_set_pktinfo_ipv6(&pkt, skb, state);
1b63d4b9
AN
102 break;
103 default:
6aa187f2 104 nft_set_pktinfo(&pkt, skb, state);
1b63d4b9
AN
105 break;
106 }
3b088c4b 107
06198b34 108 return nft_do_chain(&pkt, priv);
3b088c4b
PM
109}
110
96518518
PM
111static struct nft_af_info nft_af_bridge __read_mostly = {
112 .family = NFPROTO_BRIDGE,
113 .nhooks = NF_BR_NUMHOOKS,
114 .owner = THIS_MODULE,
115a60b1 115 .nops = 1,
3b088c4b 116 .hooks = {
36d2af59 117 [NF_BR_PRE_ROUTING] = nft_do_chain_bridge,
3b088c4b
PM
118 [NF_BR_LOCAL_IN] = nft_do_chain_bridge,
119 [NF_BR_FORWARD] = nft_do_chain_bridge,
120 [NF_BR_LOCAL_OUT] = nft_do_chain_bridge,
36d2af59 121 [NF_BR_POST_ROUTING] = nft_do_chain_bridge,
3b088c4b 122 },
96518518
PM
123};
124
99633ab2
PNA
125static int nf_tables_bridge_init_net(struct net *net)
126{
127 net->nft.bridge = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
128 if (net->nft.bridge == NULL)
129 return -ENOMEM;
130
131 memcpy(net->nft.bridge, &nft_af_bridge, sizeof(nft_af_bridge));
132
133 if (nft_register_afinfo(net, net->nft.bridge) < 0)
134 goto err;
135
136 return 0;
137err:
138 kfree(net->nft.bridge);
139 return -ENOMEM;
140}
141
142static void nf_tables_bridge_exit_net(struct net *net)
143{
144 nft_unregister_afinfo(net->nft.bridge);
145 kfree(net->nft.bridge);
146}
147
148static struct pernet_operations nf_tables_bridge_net_ops = {
149 .init = nf_tables_bridge_init_net,
150 .exit = nf_tables_bridge_exit_net,
151};
152
2a37d755 153static const struct nf_chain_type filter_bridge = {
46413825
PNA
154 .name = "filter",
155 .type = NFT_CHAIN_T_DEFAULT,
fa2c1de0
PM
156 .family = NFPROTO_BRIDGE,
157 .owner = THIS_MODULE,
4d87716c
PNA
158 .hook_mask = (1 << NF_BR_PRE_ROUTING) |
159 (1 << NF_BR_LOCAL_IN) |
46413825 160 (1 << NF_BR_FORWARD) |
4d87716c
PNA
161 (1 << NF_BR_LOCAL_OUT) |
162 (1 << NF_BR_POST_ROUTING),
46413825
PNA
163};
164
96518518
PM
165static int __init nf_tables_bridge_init(void)
166{
46413825
PNA
167 int ret;
168
169 nft_register_chain_type(&filter_bridge);
170 ret = register_pernet_subsys(&nf_tables_bridge_net_ops);
171 if (ret < 0)
172 nft_unregister_chain_type(&filter_bridge);
173
174 return ret;
96518518
PM
175}
176
177static void __exit nf_tables_bridge_exit(void)
178{
46413825
PNA
179 unregister_pernet_subsys(&nf_tables_bridge_net_ops);
180 nft_unregister_chain_type(&filter_bridge);
96518518
PM
181}
182
183module_init(nf_tables_bridge_init);
184module_exit(nf_tables_bridge_exit);
185
186MODULE_LICENSE("GPL");
187MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
188MODULE_ALIAS_NFT_FAMILY(AF_BRIDGE);