]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - net/netfilter/nft_meta.c
netfilter: nf_tables: bump set->ndeact on set flush
[mirror_ubuntu-zesty-kernel.git] / net / netfilter / nft_meta.c
1 /*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/netlink.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/nf_tables.h>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/ipv6.h>
20 #include <linux/smp.h>
21 #include <linux/static_key.h>
22 #include <net/dst.h>
23 #include <net/sock.h>
24 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
25 #include <net/netfilter/nf_tables.h>
26 #include <net/netfilter/nf_tables_core.h>
27 #include <net/netfilter/nft_meta.h>
28
29 #include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */
30
31 static DEFINE_PER_CPU(struct rnd_state, nft_prandom_state);
32
33 void nft_meta_get_eval(const struct nft_expr *expr,
34 struct nft_regs *regs,
35 const struct nft_pktinfo *pkt)
36 {
37 const struct nft_meta *priv = nft_expr_priv(expr);
38 const struct sk_buff *skb = pkt->skb;
39 const struct net_device *in = nft_in(pkt), *out = nft_out(pkt);
40 struct sock *sk;
41 u32 *dest = &regs->data[priv->dreg];
42
43 switch (priv->key) {
44 case NFT_META_LEN:
45 *dest = skb->len;
46 break;
47 case NFT_META_PROTOCOL:
48 *dest = 0;
49 *(__be16 *)dest = skb->protocol;
50 break;
51 case NFT_META_NFPROTO:
52 *dest = nft_pf(pkt);
53 break;
54 case NFT_META_L4PROTO:
55 if (!pkt->tprot_set)
56 goto err;
57 *dest = pkt->tprot;
58 break;
59 case NFT_META_PRIORITY:
60 *dest = skb->priority;
61 break;
62 case NFT_META_MARK:
63 *dest = skb->mark;
64 break;
65 case NFT_META_IIF:
66 if (in == NULL)
67 goto err;
68 *dest = in->ifindex;
69 break;
70 case NFT_META_OIF:
71 if (out == NULL)
72 goto err;
73 *dest = out->ifindex;
74 break;
75 case NFT_META_IIFNAME:
76 if (in == NULL)
77 goto err;
78 strncpy((char *)dest, in->name, IFNAMSIZ);
79 break;
80 case NFT_META_OIFNAME:
81 if (out == NULL)
82 goto err;
83 strncpy((char *)dest, out->name, IFNAMSIZ);
84 break;
85 case NFT_META_IIFTYPE:
86 if (in == NULL)
87 goto err;
88 *dest = 0;
89 *(u16 *)dest = in->type;
90 break;
91 case NFT_META_OIFTYPE:
92 if (out == NULL)
93 goto err;
94 *dest = 0;
95 *(u16 *)dest = out->type;
96 break;
97 case NFT_META_SKUID:
98 sk = skb_to_full_sk(skb);
99 if (!sk || !sk_fullsock(sk))
100 goto err;
101
102 read_lock_bh(&sk->sk_callback_lock);
103 if (sk->sk_socket == NULL ||
104 sk->sk_socket->file == NULL) {
105 read_unlock_bh(&sk->sk_callback_lock);
106 goto err;
107 }
108
109 *dest = from_kuid_munged(&init_user_ns,
110 sk->sk_socket->file->f_cred->fsuid);
111 read_unlock_bh(&sk->sk_callback_lock);
112 break;
113 case NFT_META_SKGID:
114 sk = skb_to_full_sk(skb);
115 if (!sk || !sk_fullsock(sk))
116 goto err;
117
118 read_lock_bh(&sk->sk_callback_lock);
119 if (sk->sk_socket == NULL ||
120 sk->sk_socket->file == NULL) {
121 read_unlock_bh(&sk->sk_callback_lock);
122 goto err;
123 }
124 *dest = from_kgid_munged(&init_user_ns,
125 sk->sk_socket->file->f_cred->fsgid);
126 read_unlock_bh(&sk->sk_callback_lock);
127 break;
128 #ifdef CONFIG_IP_ROUTE_CLASSID
129 case NFT_META_RTCLASSID: {
130 const struct dst_entry *dst = skb_dst(skb);
131
132 if (dst == NULL)
133 goto err;
134 *dest = dst->tclassid;
135 break;
136 }
137 #endif
138 #ifdef CONFIG_NETWORK_SECMARK
139 case NFT_META_SECMARK:
140 *dest = skb->secmark;
141 break;
142 #endif
143 case NFT_META_PKTTYPE:
144 if (skb->pkt_type != PACKET_LOOPBACK) {
145 *dest = skb->pkt_type;
146 break;
147 }
148
149 switch (nft_pf(pkt)) {
150 case NFPROTO_IPV4:
151 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
152 *dest = PACKET_MULTICAST;
153 else
154 *dest = PACKET_BROADCAST;
155 break;
156 case NFPROTO_IPV6:
157 if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
158 *dest = PACKET_MULTICAST;
159 else
160 *dest = PACKET_BROADCAST;
161 break;
162 default:
163 WARN_ON(1);
164 goto err;
165 }
166 break;
167 case NFT_META_CPU:
168 *dest = raw_smp_processor_id();
169 break;
170 case NFT_META_IIFGROUP:
171 if (in == NULL)
172 goto err;
173 *dest = in->group;
174 break;
175 case NFT_META_OIFGROUP:
176 if (out == NULL)
177 goto err;
178 *dest = out->group;
179 break;
180 #ifdef CONFIG_CGROUP_NET_CLASSID
181 case NFT_META_CGROUP:
182 sk = skb_to_full_sk(skb);
183 if (!sk || !sk_fullsock(sk))
184 goto err;
185 *dest = sock_cgroup_classid(&sk->sk_cgrp_data);
186 break;
187 #endif
188 case NFT_META_PRANDOM: {
189 struct rnd_state *state = this_cpu_ptr(&nft_prandom_state);
190 *dest = prandom_u32_state(state);
191 break;
192 }
193 default:
194 WARN_ON(1);
195 goto err;
196 }
197 return;
198
199 err:
200 regs->verdict.code = NFT_BREAK;
201 }
202 EXPORT_SYMBOL_GPL(nft_meta_get_eval);
203
204 void nft_meta_set_eval(const struct nft_expr *expr,
205 struct nft_regs *regs,
206 const struct nft_pktinfo *pkt)
207 {
208 const struct nft_meta *meta = nft_expr_priv(expr);
209 struct sk_buff *skb = pkt->skb;
210 u32 value = regs->data[meta->sreg];
211
212 switch (meta->key) {
213 case NFT_META_MARK:
214 skb->mark = value;
215 break;
216 case NFT_META_PRIORITY:
217 skb->priority = value;
218 break;
219 case NFT_META_PKTTYPE:
220 if (skb->pkt_type != value &&
221 skb_pkt_type_ok(value) && skb_pkt_type_ok(skb->pkt_type))
222 skb->pkt_type = value;
223 break;
224 case NFT_META_NFTRACE:
225 skb->nf_trace = !!value;
226 break;
227 default:
228 WARN_ON(1);
229 }
230 }
231 EXPORT_SYMBOL_GPL(nft_meta_set_eval);
232
233 const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
234 [NFTA_META_DREG] = { .type = NLA_U32 },
235 [NFTA_META_KEY] = { .type = NLA_U32 },
236 [NFTA_META_SREG] = { .type = NLA_U32 },
237 };
238 EXPORT_SYMBOL_GPL(nft_meta_policy);
239
240 int nft_meta_get_init(const struct nft_ctx *ctx,
241 const struct nft_expr *expr,
242 const struct nlattr * const tb[])
243 {
244 struct nft_meta *priv = nft_expr_priv(expr);
245 unsigned int len;
246
247 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
248 switch (priv->key) {
249 case NFT_META_PROTOCOL:
250 case NFT_META_IIFTYPE:
251 case NFT_META_OIFTYPE:
252 len = sizeof(u16);
253 break;
254 case NFT_META_NFPROTO:
255 case NFT_META_L4PROTO:
256 case NFT_META_LEN:
257 case NFT_META_PRIORITY:
258 case NFT_META_MARK:
259 case NFT_META_IIF:
260 case NFT_META_OIF:
261 case NFT_META_SKUID:
262 case NFT_META_SKGID:
263 #ifdef CONFIG_IP_ROUTE_CLASSID
264 case NFT_META_RTCLASSID:
265 #endif
266 #ifdef CONFIG_NETWORK_SECMARK
267 case NFT_META_SECMARK:
268 #endif
269 case NFT_META_PKTTYPE:
270 case NFT_META_CPU:
271 case NFT_META_IIFGROUP:
272 case NFT_META_OIFGROUP:
273 #ifdef CONFIG_CGROUP_NET_CLASSID
274 case NFT_META_CGROUP:
275 #endif
276 len = sizeof(u32);
277 break;
278 case NFT_META_IIFNAME:
279 case NFT_META_OIFNAME:
280 len = IFNAMSIZ;
281 break;
282 case NFT_META_PRANDOM:
283 prandom_init_once(&nft_prandom_state);
284 len = sizeof(u32);
285 break;
286 default:
287 return -EOPNOTSUPP;
288 }
289
290 priv->dreg = nft_parse_register(tb[NFTA_META_DREG]);
291 return nft_validate_register_store(ctx, priv->dreg, NULL,
292 NFT_DATA_VALUE, len);
293 }
294 EXPORT_SYMBOL_GPL(nft_meta_get_init);
295
296 int nft_meta_set_validate(const struct nft_ctx *ctx,
297 const struct nft_expr *expr,
298 const struct nft_data **data)
299 {
300 struct nft_meta *priv = nft_expr_priv(expr);
301 unsigned int hooks;
302
303 if (priv->key != NFT_META_PKTTYPE)
304 return 0;
305
306 switch (ctx->afi->family) {
307 case NFPROTO_BRIDGE:
308 hooks = 1 << NF_BR_PRE_ROUTING;
309 break;
310 case NFPROTO_NETDEV:
311 hooks = 1 << NF_NETDEV_INGRESS;
312 break;
313 case NFPROTO_IPV4:
314 case NFPROTO_IPV6:
315 case NFPROTO_INET:
316 hooks = 1 << NF_INET_PRE_ROUTING;
317 break;
318 default:
319 return -EOPNOTSUPP;
320 }
321
322 return nft_chain_validate_hooks(ctx->chain, hooks);
323 }
324 EXPORT_SYMBOL_GPL(nft_meta_set_validate);
325
326 int nft_meta_set_init(const struct nft_ctx *ctx,
327 const struct nft_expr *expr,
328 const struct nlattr * const tb[])
329 {
330 struct nft_meta *priv = nft_expr_priv(expr);
331 unsigned int len;
332 int err;
333
334 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
335 switch (priv->key) {
336 case NFT_META_MARK:
337 case NFT_META_PRIORITY:
338 len = sizeof(u32);
339 break;
340 case NFT_META_NFTRACE:
341 len = sizeof(u8);
342 break;
343 case NFT_META_PKTTYPE:
344 len = sizeof(u8);
345 break;
346 default:
347 return -EOPNOTSUPP;
348 }
349
350 err = nft_meta_set_validate(ctx, expr, NULL);
351 if (err < 0)
352 return err;
353
354 priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
355 err = nft_validate_register_load(priv->sreg, len);
356 if (err < 0)
357 return err;
358
359 if (priv->key == NFT_META_NFTRACE)
360 static_branch_inc(&nft_trace_enabled);
361
362 return 0;
363 }
364 EXPORT_SYMBOL_GPL(nft_meta_set_init);
365
366 int nft_meta_get_dump(struct sk_buff *skb,
367 const struct nft_expr *expr)
368 {
369 const struct nft_meta *priv = nft_expr_priv(expr);
370
371 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
372 goto nla_put_failure;
373 if (nft_dump_register(skb, NFTA_META_DREG, priv->dreg))
374 goto nla_put_failure;
375 return 0;
376
377 nla_put_failure:
378 return -1;
379 }
380 EXPORT_SYMBOL_GPL(nft_meta_get_dump);
381
382 int nft_meta_set_dump(struct sk_buff *skb,
383 const struct nft_expr *expr)
384 {
385 const struct nft_meta *priv = nft_expr_priv(expr);
386
387 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
388 goto nla_put_failure;
389 if (nft_dump_register(skb, NFTA_META_SREG, priv->sreg))
390 goto nla_put_failure;
391
392 return 0;
393
394 nla_put_failure:
395 return -1;
396 }
397 EXPORT_SYMBOL_GPL(nft_meta_set_dump);
398
399 void nft_meta_set_destroy(const struct nft_ctx *ctx,
400 const struct nft_expr *expr)
401 {
402 const struct nft_meta *priv = nft_expr_priv(expr);
403
404 if (priv->key == NFT_META_NFTRACE)
405 static_branch_dec(&nft_trace_enabled);
406 }
407 EXPORT_SYMBOL_GPL(nft_meta_set_destroy);
408
409 static struct nft_expr_type nft_meta_type;
410 static const struct nft_expr_ops nft_meta_get_ops = {
411 .type = &nft_meta_type,
412 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
413 .eval = nft_meta_get_eval,
414 .init = nft_meta_get_init,
415 .dump = nft_meta_get_dump,
416 };
417
418 static const struct nft_expr_ops nft_meta_set_ops = {
419 .type = &nft_meta_type,
420 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
421 .eval = nft_meta_set_eval,
422 .init = nft_meta_set_init,
423 .destroy = nft_meta_set_destroy,
424 .dump = nft_meta_set_dump,
425 .validate = nft_meta_set_validate,
426 };
427
428 static const struct nft_expr_ops *
429 nft_meta_select_ops(const struct nft_ctx *ctx,
430 const struct nlattr * const tb[])
431 {
432 if (tb[NFTA_META_KEY] == NULL)
433 return ERR_PTR(-EINVAL);
434
435 if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
436 return ERR_PTR(-EINVAL);
437
438 if (tb[NFTA_META_DREG])
439 return &nft_meta_get_ops;
440
441 if (tb[NFTA_META_SREG])
442 return &nft_meta_set_ops;
443
444 return ERR_PTR(-EINVAL);
445 }
446
447 static struct nft_expr_type nft_meta_type __read_mostly = {
448 .name = "meta",
449 .select_ops = &nft_meta_select_ops,
450 .policy = nft_meta_policy,
451 .maxattr = NFTA_META_MAX,
452 .owner = THIS_MODULE,
453 };
454
455 static int __init nft_meta_module_init(void)
456 {
457 return nft_register_expr(&nft_meta_type);
458 }
459
460 static void __exit nft_meta_module_exit(void)
461 {
462 nft_unregister_expr(&nft_meta_type);
463 }
464
465 module_init(nft_meta_module_init);
466 module_exit(nft_meta_module_exit);
467
468 MODULE_LICENSE("GPL");
469 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
470 MODULE_ALIAS_NFT_EXPR("meta");