Linux 4.19.133
[linux/fpc-iii.git] / net / netfilter / nft_meta.c
blob297fe7d97c182ffbcbfb15c94f10bdec633f1149
1 /*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2014 Intel Corporation
4 * Author: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Development of this code funded by Astaro AG (http://www.astaro.com/)
13 #include <linux/kernel.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>
28 #include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */
30 struct nft_meta {
31 enum nft_meta_keys key:8;
32 union {
33 enum nft_registers dreg:8;
34 enum nft_registers sreg:8;
38 static DEFINE_PER_CPU(struct rnd_state, nft_prandom_state);
40 #ifdef CONFIG_NF_TABLES_BRIDGE
41 #include "../bridge/br_private.h"
42 #endif
44 void nft_meta_get_eval(const struct nft_expr *expr,
45 struct nft_regs *regs,
46 const struct nft_pktinfo *pkt)
48 const struct nft_meta *priv = nft_expr_priv(expr);
49 const struct sk_buff *skb = pkt->skb;
50 const struct net_device *in = nft_in(pkt), *out = nft_out(pkt);
51 struct sock *sk;
52 u32 *dest = &regs->data[priv->dreg];
53 #ifdef CONFIG_NF_TABLES_BRIDGE
54 const struct net_bridge_port *p;
55 #endif
57 switch (priv->key) {
58 case NFT_META_LEN:
59 *dest = skb->len;
60 break;
61 case NFT_META_PROTOCOL:
62 nft_reg_store16(dest, (__force u16)skb->protocol);
63 break;
64 case NFT_META_NFPROTO:
65 nft_reg_store8(dest, nft_pf(pkt));
66 break;
67 case NFT_META_L4PROTO:
68 if (!pkt->tprot_set)
69 goto err;
70 nft_reg_store8(dest, pkt->tprot);
71 break;
72 case NFT_META_PRIORITY:
73 *dest = skb->priority;
74 break;
75 case NFT_META_MARK:
76 *dest = skb->mark;
77 break;
78 case NFT_META_IIF:
79 if (in == NULL)
80 goto err;
81 *dest = in->ifindex;
82 break;
83 case NFT_META_OIF:
84 if (out == NULL)
85 goto err;
86 *dest = out->ifindex;
87 break;
88 case NFT_META_IIFNAME:
89 if (in == NULL)
90 goto err;
91 strncpy((char *)dest, in->name, IFNAMSIZ);
92 break;
93 case NFT_META_OIFNAME:
94 if (out == NULL)
95 goto err;
96 strncpy((char *)dest, out->name, IFNAMSIZ);
97 break;
98 case NFT_META_IIFTYPE:
99 if (in == NULL)
100 goto err;
101 nft_reg_store16(dest, in->type);
102 break;
103 case NFT_META_OIFTYPE:
104 if (out == NULL)
105 goto err;
106 nft_reg_store16(dest, out->type);
107 break;
108 case NFT_META_SKUID:
109 sk = skb_to_full_sk(skb);
110 if (!sk || !sk_fullsock(sk) ||
111 !net_eq(nft_net(pkt), sock_net(sk)))
112 goto err;
114 read_lock_bh(&sk->sk_callback_lock);
115 if (sk->sk_socket == NULL ||
116 sk->sk_socket->file == NULL) {
117 read_unlock_bh(&sk->sk_callback_lock);
118 goto err;
121 *dest = from_kuid_munged(&init_user_ns,
122 sk->sk_socket->file->f_cred->fsuid);
123 read_unlock_bh(&sk->sk_callback_lock);
124 break;
125 case NFT_META_SKGID:
126 sk = skb_to_full_sk(skb);
127 if (!sk || !sk_fullsock(sk) ||
128 !net_eq(nft_net(pkt), sock_net(sk)))
129 goto err;
131 read_lock_bh(&sk->sk_callback_lock);
132 if (sk->sk_socket == NULL ||
133 sk->sk_socket->file == NULL) {
134 read_unlock_bh(&sk->sk_callback_lock);
135 goto err;
137 *dest = from_kgid_munged(&init_user_ns,
138 sk->sk_socket->file->f_cred->fsgid);
139 read_unlock_bh(&sk->sk_callback_lock);
140 break;
141 #ifdef CONFIG_IP_ROUTE_CLASSID
142 case NFT_META_RTCLASSID: {
143 const struct dst_entry *dst = skb_dst(skb);
145 if (dst == NULL)
146 goto err;
147 *dest = dst->tclassid;
148 break;
150 #endif
151 #ifdef CONFIG_NETWORK_SECMARK
152 case NFT_META_SECMARK:
153 *dest = skb->secmark;
154 break;
155 #endif
156 case NFT_META_PKTTYPE:
157 if (skb->pkt_type != PACKET_LOOPBACK) {
158 nft_reg_store8(dest, skb->pkt_type);
159 break;
162 switch (nft_pf(pkt)) {
163 case NFPROTO_IPV4:
164 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
165 nft_reg_store8(dest, PACKET_MULTICAST);
166 else
167 nft_reg_store8(dest, PACKET_BROADCAST);
168 break;
169 case NFPROTO_IPV6:
170 nft_reg_store8(dest, PACKET_MULTICAST);
171 break;
172 case NFPROTO_NETDEV:
173 switch (skb->protocol) {
174 case htons(ETH_P_IP): {
175 int noff = skb_network_offset(skb);
176 struct iphdr *iph, _iph;
178 iph = skb_header_pointer(skb, noff,
179 sizeof(_iph), &_iph);
180 if (!iph)
181 goto err;
183 if (ipv4_is_multicast(iph->daddr))
184 nft_reg_store8(dest, PACKET_MULTICAST);
185 else
186 nft_reg_store8(dest, PACKET_BROADCAST);
188 break;
190 case htons(ETH_P_IPV6):
191 nft_reg_store8(dest, PACKET_MULTICAST);
192 break;
193 default:
194 WARN_ON_ONCE(1);
195 goto err;
197 break;
198 default:
199 WARN_ON_ONCE(1);
200 goto err;
202 break;
203 case NFT_META_CPU:
204 *dest = raw_smp_processor_id();
205 break;
206 case NFT_META_IIFGROUP:
207 if (in == NULL)
208 goto err;
209 *dest = in->group;
210 break;
211 case NFT_META_OIFGROUP:
212 if (out == NULL)
213 goto err;
214 *dest = out->group;
215 break;
216 #ifdef CONFIG_CGROUP_NET_CLASSID
217 case NFT_META_CGROUP:
218 sk = skb_to_full_sk(skb);
219 if (!sk || !sk_fullsock(sk) ||
220 !net_eq(nft_net(pkt), sock_net(sk)))
221 goto err;
222 *dest = sock_cgroup_classid(&sk->sk_cgrp_data);
223 break;
224 #endif
225 case NFT_META_PRANDOM: {
226 struct rnd_state *state = this_cpu_ptr(&nft_prandom_state);
227 *dest = prandom_u32_state(state);
228 break;
230 #ifdef CONFIG_XFRM
231 case NFT_META_SECPATH:
232 nft_reg_store8(dest, !!skb->sp);
233 break;
234 #endif
235 #ifdef CONFIG_NF_TABLES_BRIDGE
236 case NFT_META_BRI_IIFNAME:
237 if (in == NULL || (p = br_port_get_rcu(in)) == NULL)
238 goto err;
239 strncpy((char *)dest, p->br->dev->name, IFNAMSIZ);
240 return;
241 case NFT_META_BRI_OIFNAME:
242 if (out == NULL || (p = br_port_get_rcu(out)) == NULL)
243 goto err;
244 strncpy((char *)dest, p->br->dev->name, IFNAMSIZ);
245 return;
246 #endif
247 default:
248 WARN_ON(1);
249 goto err;
251 return;
253 err:
254 regs->verdict.code = NFT_BREAK;
257 static void nft_meta_set_eval(const struct nft_expr *expr,
258 struct nft_regs *regs,
259 const struct nft_pktinfo *pkt)
261 const struct nft_meta *meta = nft_expr_priv(expr);
262 struct sk_buff *skb = pkt->skb;
263 u32 *sreg = &regs->data[meta->sreg];
264 u32 value = *sreg;
265 u8 value8;
267 switch (meta->key) {
268 case NFT_META_MARK:
269 skb->mark = value;
270 break;
271 case NFT_META_PRIORITY:
272 skb->priority = value;
273 break;
274 case NFT_META_PKTTYPE:
275 value8 = nft_reg_load8(sreg);
277 if (skb->pkt_type != value8 &&
278 skb_pkt_type_ok(value8) &&
279 skb_pkt_type_ok(skb->pkt_type))
280 skb->pkt_type = value8;
281 break;
282 case NFT_META_NFTRACE:
283 value8 = nft_reg_load8(sreg);
285 skb->nf_trace = !!value8;
286 break;
287 default:
288 WARN_ON(1);
292 static const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
293 [NFTA_META_DREG] = { .type = NLA_U32 },
294 [NFTA_META_KEY] = { .type = NLA_U32 },
295 [NFTA_META_SREG] = { .type = NLA_U32 },
298 static int nft_meta_get_init(const struct nft_ctx *ctx,
299 const struct nft_expr *expr,
300 const struct nlattr * const tb[])
302 struct nft_meta *priv = nft_expr_priv(expr);
303 unsigned int len;
305 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
306 switch (priv->key) {
307 case NFT_META_PROTOCOL:
308 case NFT_META_IIFTYPE:
309 case NFT_META_OIFTYPE:
310 len = sizeof(u16);
311 break;
312 case NFT_META_NFPROTO:
313 case NFT_META_L4PROTO:
314 case NFT_META_LEN:
315 case NFT_META_PRIORITY:
316 case NFT_META_MARK:
317 case NFT_META_IIF:
318 case NFT_META_OIF:
319 case NFT_META_SKUID:
320 case NFT_META_SKGID:
321 #ifdef CONFIG_IP_ROUTE_CLASSID
322 case NFT_META_RTCLASSID:
323 #endif
324 #ifdef CONFIG_NETWORK_SECMARK
325 case NFT_META_SECMARK:
326 #endif
327 case NFT_META_PKTTYPE:
328 case NFT_META_CPU:
329 case NFT_META_IIFGROUP:
330 case NFT_META_OIFGROUP:
331 #ifdef CONFIG_CGROUP_NET_CLASSID
332 case NFT_META_CGROUP:
333 #endif
334 len = sizeof(u32);
335 break;
336 case NFT_META_IIFNAME:
337 case NFT_META_OIFNAME:
338 len = IFNAMSIZ;
339 break;
340 case NFT_META_PRANDOM:
341 prandom_init_once(&nft_prandom_state);
342 len = sizeof(u32);
343 break;
344 #ifdef CONFIG_XFRM
345 case NFT_META_SECPATH:
346 len = sizeof(u8);
347 break;
348 #endif
349 #ifdef CONFIG_NF_TABLES_BRIDGE
350 case NFT_META_BRI_IIFNAME:
351 case NFT_META_BRI_OIFNAME:
352 if (ctx->family != NFPROTO_BRIDGE)
353 return -EOPNOTSUPP;
354 len = IFNAMSIZ;
355 break;
356 #endif
357 default:
358 return -EOPNOTSUPP;
361 priv->dreg = nft_parse_register(tb[NFTA_META_DREG]);
362 return nft_validate_register_store(ctx, priv->dreg, NULL,
363 NFT_DATA_VALUE, len);
366 static int nft_meta_get_validate(const struct nft_ctx *ctx,
367 const struct nft_expr *expr,
368 const struct nft_data **data)
370 #ifdef CONFIG_XFRM
371 const struct nft_meta *priv = nft_expr_priv(expr);
372 unsigned int hooks;
374 if (priv->key != NFT_META_SECPATH)
375 return 0;
377 switch (ctx->family) {
378 case NFPROTO_NETDEV:
379 hooks = 1 << NF_NETDEV_INGRESS;
380 break;
381 case NFPROTO_IPV4:
382 case NFPROTO_IPV6:
383 case NFPROTO_INET:
384 hooks = (1 << NF_INET_PRE_ROUTING) |
385 (1 << NF_INET_LOCAL_IN) |
386 (1 << NF_INET_FORWARD);
387 break;
388 default:
389 return -EOPNOTSUPP;
392 return nft_chain_validate_hooks(ctx->chain, hooks);
393 #else
394 return 0;
395 #endif
398 static int nft_meta_set_validate(const struct nft_ctx *ctx,
399 const struct nft_expr *expr,
400 const struct nft_data **data)
402 struct nft_meta *priv = nft_expr_priv(expr);
403 unsigned int hooks;
405 if (priv->key != NFT_META_PKTTYPE)
406 return 0;
408 switch (ctx->family) {
409 case NFPROTO_BRIDGE:
410 hooks = 1 << NF_BR_PRE_ROUTING;
411 break;
412 case NFPROTO_NETDEV:
413 hooks = 1 << NF_NETDEV_INGRESS;
414 break;
415 case NFPROTO_IPV4:
416 case NFPROTO_IPV6:
417 case NFPROTO_INET:
418 hooks = 1 << NF_INET_PRE_ROUTING;
419 break;
420 default:
421 return -EOPNOTSUPP;
424 return nft_chain_validate_hooks(ctx->chain, hooks);
427 static int nft_meta_set_init(const struct nft_ctx *ctx,
428 const struct nft_expr *expr,
429 const struct nlattr * const tb[])
431 struct nft_meta *priv = nft_expr_priv(expr);
432 unsigned int len;
433 int err;
435 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
436 switch (priv->key) {
437 case NFT_META_MARK:
438 case NFT_META_PRIORITY:
439 len = sizeof(u32);
440 break;
441 case NFT_META_NFTRACE:
442 len = sizeof(u8);
443 break;
444 case NFT_META_PKTTYPE:
445 len = sizeof(u8);
446 break;
447 default:
448 return -EOPNOTSUPP;
451 priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
452 err = nft_validate_register_load(priv->sreg, len);
453 if (err < 0)
454 return err;
456 if (priv->key == NFT_META_NFTRACE)
457 static_branch_inc(&nft_trace_enabled);
459 return 0;
462 static int nft_meta_get_dump(struct sk_buff *skb,
463 const struct nft_expr *expr)
465 const struct nft_meta *priv = nft_expr_priv(expr);
467 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
468 goto nla_put_failure;
469 if (nft_dump_register(skb, NFTA_META_DREG, priv->dreg))
470 goto nla_put_failure;
471 return 0;
473 nla_put_failure:
474 return -1;
477 static int nft_meta_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
479 const struct nft_meta *priv = nft_expr_priv(expr);
481 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
482 goto nla_put_failure;
483 if (nft_dump_register(skb, NFTA_META_SREG, priv->sreg))
484 goto nla_put_failure;
486 return 0;
488 nla_put_failure:
489 return -1;
492 static void nft_meta_set_destroy(const struct nft_ctx *ctx,
493 const struct nft_expr *expr)
495 const struct nft_meta *priv = nft_expr_priv(expr);
497 if (priv->key == NFT_META_NFTRACE)
498 static_branch_dec(&nft_trace_enabled);
501 static const struct nft_expr_ops nft_meta_get_ops = {
502 .type = &nft_meta_type,
503 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
504 .eval = nft_meta_get_eval,
505 .init = nft_meta_get_init,
506 .dump = nft_meta_get_dump,
507 .validate = nft_meta_get_validate,
510 static const struct nft_expr_ops nft_meta_set_ops = {
511 .type = &nft_meta_type,
512 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
513 .eval = nft_meta_set_eval,
514 .init = nft_meta_set_init,
515 .destroy = nft_meta_set_destroy,
516 .dump = nft_meta_set_dump,
517 .validate = nft_meta_set_validate,
520 static const struct nft_expr_ops *
521 nft_meta_select_ops(const struct nft_ctx *ctx,
522 const struct nlattr * const tb[])
524 if (tb[NFTA_META_KEY] == NULL)
525 return ERR_PTR(-EINVAL);
527 if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
528 return ERR_PTR(-EINVAL);
530 if (tb[NFTA_META_DREG])
531 return &nft_meta_get_ops;
533 if (tb[NFTA_META_SREG])
534 return &nft_meta_set_ops;
536 return ERR_PTR(-EINVAL);
539 struct nft_expr_type nft_meta_type __read_mostly = {
540 .name = "meta",
541 .select_ops = nft_meta_select_ops,
542 .policy = nft_meta_policy,
543 .maxattr = NFTA_META_MAX,
544 .owner = THIS_MODULE,