netfilter: Make nf_hookfn use nf_hook_state.
[deliverable/linux.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
AN
67static 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
79static 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
3b088c4b
PM
93static unsigned int
94nft_do_chain_bridge(const struct nf_hook_ops *ops,
95 struct sk_buff *skb,
238e54c9 96 const struct nf_hook_state *state)
3b088c4b
PM
97{
98 struct nft_pktinfo pkt;
99
1b63d4b9
AN
100 switch (eth_hdr(skb)->h_proto) {
101 case htons(ETH_P_IP):
238e54c9 102 nft_bridge_set_pktinfo_ipv4(&pkt, ops, skb, state->in, state->out);
1b63d4b9
AN
103 break;
104 case htons(ETH_P_IPV6):
238e54c9 105 nft_bridge_set_pktinfo_ipv6(&pkt, ops, skb, state->in, state->out);
1b63d4b9
AN
106 break;
107 default:
238e54c9 108 nft_set_pktinfo(&pkt, ops, skb, state->in, state->out);
1b63d4b9
AN
109 break;
110 }
3b088c4b 111
3876d22d 112 return nft_do_chain(&pkt, ops);
3b088c4b
PM
113}
114
96518518
PM
115static struct nft_af_info nft_af_bridge __read_mostly = {
116 .family = NFPROTO_BRIDGE,
117 .nhooks = NF_BR_NUMHOOKS,
118 .owner = THIS_MODULE,
115a60b1 119 .nops = 1,
3b088c4b 120 .hooks = {
36d2af59 121 [NF_BR_PRE_ROUTING] = nft_do_chain_bridge,
3b088c4b
PM
122 [NF_BR_LOCAL_IN] = nft_do_chain_bridge,
123 [NF_BR_FORWARD] = nft_do_chain_bridge,
124 [NF_BR_LOCAL_OUT] = nft_do_chain_bridge,
36d2af59 125 [NF_BR_POST_ROUTING] = nft_do_chain_bridge,
3b088c4b 126 },
96518518
PM
127};
128
99633ab2
PNA
129static int nf_tables_bridge_init_net(struct net *net)
130{
131 net->nft.bridge = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
132 if (net->nft.bridge == NULL)
133 return -ENOMEM;
134
135 memcpy(net->nft.bridge, &nft_af_bridge, sizeof(nft_af_bridge));
136
137 if (nft_register_afinfo(net, net->nft.bridge) < 0)
138 goto err;
139
140 return 0;
141err:
142 kfree(net->nft.bridge);
143 return -ENOMEM;
144}
145
146static void nf_tables_bridge_exit_net(struct net *net)
147{
148 nft_unregister_afinfo(net->nft.bridge);
149 kfree(net->nft.bridge);
150}
151
152static struct pernet_operations nf_tables_bridge_net_ops = {
153 .init = nf_tables_bridge_init_net,
154 .exit = nf_tables_bridge_exit_net,
155};
156
2a37d755 157static const struct nf_chain_type filter_bridge = {
46413825
PNA
158 .name = "filter",
159 .type = NFT_CHAIN_T_DEFAULT,
fa2c1de0
PM
160 .family = NFPROTO_BRIDGE,
161 .owner = THIS_MODULE,
4d87716c
PNA
162 .hook_mask = (1 << NF_BR_PRE_ROUTING) |
163 (1 << NF_BR_LOCAL_IN) |
46413825 164 (1 << NF_BR_FORWARD) |
4d87716c
PNA
165 (1 << NF_BR_LOCAL_OUT) |
166 (1 << NF_BR_POST_ROUTING),
46413825
PNA
167};
168
96518518
PM
169static int __init nf_tables_bridge_init(void)
170{
46413825
PNA
171 int ret;
172
173 nft_register_chain_type(&filter_bridge);
174 ret = register_pernet_subsys(&nf_tables_bridge_net_ops);
175 if (ret < 0)
176 nft_unregister_chain_type(&filter_bridge);
177
178 return ret;
96518518
PM
179}
180
181static void __exit nf_tables_bridge_exit(void)
182{
46413825
PNA
183 unregister_pernet_subsys(&nf_tables_bridge_net_ops);
184 nft_unregister_chain_type(&filter_bridge);
96518518
PM
185}
186
187module_init(nf_tables_bridge_init);
188module_exit(nf_tables_bridge_exit);
189
190MODULE_LICENSE("GPL");
191MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
192MODULE_ALIAS_NFT_FAMILY(AF_BRIDGE);
This page took 0.098772 seconds and 5 git commands to generate.