Merge remote-tracking branch 'pstore/for-next/pstore'
[deliverable/linux.git] / net / netfilter / nf_tables_netdev.c
1 /*
2 * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <net/netfilter/nf_tables.h>
13 #include <linux/ip.h>
14 #include <linux/ipv6.h>
15 #include <net/netfilter/nf_tables_ipv4.h>
16 #include <net/netfilter/nf_tables_ipv6.h>
17
18 static inline void
19 nft_netdev_set_pktinfo_ipv4(struct nft_pktinfo *pkt,
20 struct sk_buff *skb,
21 const struct nf_hook_state *state)
22 {
23 struct iphdr *iph, _iph;
24 u32 len, thoff;
25
26 nft_set_pktinfo(pkt, skb, state);
27
28 iph = skb_header_pointer(skb, skb_network_offset(skb), sizeof(*iph),
29 &_iph);
30 if (!iph)
31 return;
32
33 if (iph->ihl < 5 || iph->version != 4)
34 return;
35
36 len = ntohs(iph->tot_len);
37 thoff = iph->ihl * 4;
38 if (skb->len < len)
39 return;
40 else if (len < thoff)
41 return;
42
43 pkt->tprot = iph->protocol;
44 pkt->xt.thoff = thoff;
45 pkt->xt.fragoff = ntohs(iph->frag_off) & IP_OFFSET;
46 }
47
48 static inline void
49 __nft_netdev_set_pktinfo_ipv6(struct nft_pktinfo *pkt,
50 struct sk_buff *skb,
51 const struct nf_hook_state *state)
52 {
53 #if IS_ENABLED(CONFIG_IPV6)
54 struct ipv6hdr *ip6h, _ip6h;
55 unsigned int thoff = 0;
56 unsigned short frag_off;
57 int protohdr;
58 u32 pkt_len;
59
60 ip6h = skb_header_pointer(skb, skb_network_offset(skb), sizeof(*ip6h),
61 &_ip6h);
62 if (!ip6h)
63 return;
64
65 if (ip6h->version != 6)
66 return;
67
68 pkt_len = ntohs(ip6h->payload_len);
69 if (pkt_len + sizeof(*ip6h) > skb->len)
70 return;
71
72 protohdr = ipv6_find_hdr(pkt->skb, &thoff, -1, &frag_off, NULL);
73 if (protohdr < 0)
74 return;
75
76 pkt->tprot = protohdr;
77 pkt->xt.thoff = thoff;
78 pkt->xt.fragoff = frag_off;
79 #endif
80 }
81
82 static inline void nft_netdev_set_pktinfo_ipv6(struct nft_pktinfo *pkt,
83 struct sk_buff *skb,
84 const struct nf_hook_state *state)
85 {
86 nft_set_pktinfo(pkt, skb, state);
87 __nft_netdev_set_pktinfo_ipv6(pkt, skb, state);
88 }
89
90 static unsigned int
91 nft_do_chain_netdev(void *priv, struct sk_buff *skb,
92 const struct nf_hook_state *state)
93 {
94 struct nft_pktinfo pkt;
95
96 switch (skb->protocol) {
97 case htons(ETH_P_IP):
98 nft_netdev_set_pktinfo_ipv4(&pkt, skb, state);
99 break;
100 case htons(ETH_P_IPV6):
101 nft_netdev_set_pktinfo_ipv6(&pkt, skb, state);
102 break;
103 default:
104 nft_set_pktinfo(&pkt, skb, state);
105 break;
106 }
107
108 return nft_do_chain(&pkt, priv);
109 }
110
111 static struct nft_af_info nft_af_netdev __read_mostly = {
112 .family = NFPROTO_NETDEV,
113 .nhooks = NF_NETDEV_NUMHOOKS,
114 .owner = THIS_MODULE,
115 .flags = NFT_AF_NEEDS_DEV,
116 .nops = 1,
117 .hooks = {
118 [NF_NETDEV_INGRESS] = nft_do_chain_netdev,
119 },
120 };
121
122 static int nf_tables_netdev_init_net(struct net *net)
123 {
124 net->nft.netdev = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
125 if (net->nft.netdev == NULL)
126 return -ENOMEM;
127
128 memcpy(net->nft.netdev, &nft_af_netdev, sizeof(nft_af_netdev));
129
130 if (nft_register_afinfo(net, net->nft.netdev) < 0)
131 goto err;
132
133 return 0;
134 err:
135 kfree(net->nft.netdev);
136 return -ENOMEM;
137 }
138
139 static void nf_tables_netdev_exit_net(struct net *net)
140 {
141 nft_unregister_afinfo(net, net->nft.netdev);
142 kfree(net->nft.netdev);
143 }
144
145 static struct pernet_operations nf_tables_netdev_net_ops = {
146 .init = nf_tables_netdev_init_net,
147 .exit = nf_tables_netdev_exit_net,
148 };
149
150 static const struct nf_chain_type nft_filter_chain_netdev = {
151 .name = "filter",
152 .type = NFT_CHAIN_T_DEFAULT,
153 .family = NFPROTO_NETDEV,
154 .owner = THIS_MODULE,
155 .hook_mask = (1 << NF_NETDEV_INGRESS),
156 };
157
158 static void nft_netdev_event(unsigned long event, struct net_device *dev,
159 struct nft_ctx *ctx)
160 {
161 struct nft_base_chain *basechain = nft_base_chain(ctx->chain);
162
163 switch (event) {
164 case NETDEV_UNREGISTER:
165 if (strcmp(basechain->dev_name, dev->name) != 0)
166 return;
167
168 __nft_release_basechain(ctx);
169 break;
170 case NETDEV_CHANGENAME:
171 if (dev->ifindex != basechain->ops[0].dev->ifindex)
172 return;
173
174 strncpy(basechain->dev_name, dev->name, IFNAMSIZ);
175 break;
176 }
177 }
178
179 static int nf_tables_netdev_event(struct notifier_block *this,
180 unsigned long event, void *ptr)
181 {
182 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
183 struct nft_af_info *afi;
184 struct nft_table *table;
185 struct nft_chain *chain, *nr;
186 struct nft_ctx ctx = {
187 .net = dev_net(dev),
188 };
189
190 if (event != NETDEV_UNREGISTER &&
191 event != NETDEV_CHANGENAME)
192 return NOTIFY_DONE;
193
194 nfnl_lock(NFNL_SUBSYS_NFTABLES);
195 list_for_each_entry(afi, &dev_net(dev)->nft.af_info, list) {
196 ctx.afi = afi;
197 if (afi->family != NFPROTO_NETDEV)
198 continue;
199
200 list_for_each_entry(table, &afi->tables, list) {
201 ctx.table = table;
202 list_for_each_entry_safe(chain, nr, &table->chains, list) {
203 if (!(chain->flags & NFT_BASE_CHAIN))
204 continue;
205
206 ctx.chain = chain;
207 nft_netdev_event(event, dev, &ctx);
208 }
209 }
210 }
211 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
212
213 return NOTIFY_DONE;
214 }
215
216 static struct notifier_block nf_tables_netdev_notifier = {
217 .notifier_call = nf_tables_netdev_event,
218 };
219
220 static int __init nf_tables_netdev_init(void)
221 {
222 int ret;
223
224 nft_register_chain_type(&nft_filter_chain_netdev);
225 ret = register_pernet_subsys(&nf_tables_netdev_net_ops);
226 if (ret < 0) {
227 nft_unregister_chain_type(&nft_filter_chain_netdev);
228 return ret;
229 }
230 register_netdevice_notifier(&nf_tables_netdev_notifier);
231 return 0;
232 }
233
234 static void __exit nf_tables_netdev_exit(void)
235 {
236 unregister_netdevice_notifier(&nf_tables_netdev_notifier);
237 unregister_pernet_subsys(&nf_tables_netdev_net_ops);
238 nft_unregister_chain_type(&nft_filter_chain_netdev);
239 }
240
241 module_init(nf_tables_netdev_init);
242 module_exit(nf_tables_netdev_exit);
243
244 MODULE_LICENSE("GPL");
245 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
246 MODULE_ALIAS_NFT_FAMILY(5); /* NFPROTO_NETDEV */
This page took 0.038411 seconds and 5 git commands to generate.