1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 * Copyright (c) 2014 Intel Corporation
5 * Author: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
7 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 #include <linux/kernel.h>
11 #include <linux/netlink.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter/nf_tables.h>
16 #include <linux/ipv6.h>
17 #include <linux/smp.h>
18 #include <linux/static_key.h>
21 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
22 #include <net/netfilter/nf_tables.h>
23 #include <net/netfilter/nf_tables_core.h>
24 #include <net/netfilter/nft_meta.h>
25 #include <net/netfilter/nf_tables_offload.h>
27 #include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */
29 static DEFINE_PER_CPU(struct rnd_state
, nft_prandom_state
);
31 void nft_meta_get_eval(const struct nft_expr
*expr
,
32 struct nft_regs
*regs
,
33 const struct nft_pktinfo
*pkt
)
35 const struct nft_meta
*priv
= nft_expr_priv(expr
);
36 const struct sk_buff
*skb
= pkt
->skb
;
37 const struct net_device
*in
= nft_in(pkt
), *out
= nft_out(pkt
);
39 u32
*dest
= ®s
->data
[priv
->dreg
];
45 case NFT_META_PROTOCOL
:
46 nft_reg_store16(dest
, (__force u16
)skb
->protocol
);
48 case NFT_META_NFPROTO
:
49 nft_reg_store8(dest
, nft_pf(pkt
));
51 case NFT_META_L4PROTO
:
54 nft_reg_store8(dest
, pkt
->tprot
);
56 case NFT_META_PRIORITY
:
57 *dest
= skb
->priority
;
63 *dest
= in
? in
->ifindex
: 0;
66 *dest
= out
? out
->ifindex
: 0;
68 case NFT_META_IIFNAME
:
69 strncpy((char *)dest
, in
? in
->name
: "", IFNAMSIZ
);
71 case NFT_META_OIFNAME
:
72 strncpy((char *)dest
, out
? out
->name
: "", IFNAMSIZ
);
74 case NFT_META_IIFTYPE
:
77 nft_reg_store16(dest
, in
->type
);
79 case NFT_META_OIFTYPE
:
82 nft_reg_store16(dest
, out
->type
);
85 sk
= skb_to_full_sk(skb
);
86 if (!sk
|| !sk_fullsock(sk
) ||
87 !net_eq(nft_net(pkt
), sock_net(sk
)))
90 read_lock_bh(&sk
->sk_callback_lock
);
91 if (sk
->sk_socket
== NULL
||
92 sk
->sk_socket
->file
== NULL
) {
93 read_unlock_bh(&sk
->sk_callback_lock
);
97 *dest
= from_kuid_munged(&init_user_ns
,
98 sk
->sk_socket
->file
->f_cred
->fsuid
);
99 read_unlock_bh(&sk
->sk_callback_lock
);
102 sk
= skb_to_full_sk(skb
);
103 if (!sk
|| !sk_fullsock(sk
) ||
104 !net_eq(nft_net(pkt
), sock_net(sk
)))
107 read_lock_bh(&sk
->sk_callback_lock
);
108 if (sk
->sk_socket
== NULL
||
109 sk
->sk_socket
->file
== NULL
) {
110 read_unlock_bh(&sk
->sk_callback_lock
);
113 *dest
= from_kgid_munged(&init_user_ns
,
114 sk
->sk_socket
->file
->f_cred
->fsgid
);
115 read_unlock_bh(&sk
->sk_callback_lock
);
117 #ifdef CONFIG_IP_ROUTE_CLASSID
118 case NFT_META_RTCLASSID
: {
119 const struct dst_entry
*dst
= skb_dst(skb
);
123 *dest
= dst
->tclassid
;
127 #ifdef CONFIG_NETWORK_SECMARK
128 case NFT_META_SECMARK
:
129 *dest
= skb
->secmark
;
132 case NFT_META_PKTTYPE
:
133 if (skb
->pkt_type
!= PACKET_LOOPBACK
) {
134 nft_reg_store8(dest
, skb
->pkt_type
);
138 switch (nft_pf(pkt
)) {
140 if (ipv4_is_multicast(ip_hdr(skb
)->daddr
))
141 nft_reg_store8(dest
, PACKET_MULTICAST
);
143 nft_reg_store8(dest
, PACKET_BROADCAST
);
146 nft_reg_store8(dest
, PACKET_MULTICAST
);
149 switch (skb
->protocol
) {
150 case htons(ETH_P_IP
): {
151 int noff
= skb_network_offset(skb
);
152 struct iphdr
*iph
, _iph
;
154 iph
= skb_header_pointer(skb
, noff
,
155 sizeof(_iph
), &_iph
);
159 if (ipv4_is_multicast(iph
->daddr
))
160 nft_reg_store8(dest
, PACKET_MULTICAST
);
162 nft_reg_store8(dest
, PACKET_BROADCAST
);
166 case htons(ETH_P_IPV6
):
167 nft_reg_store8(dest
, PACKET_MULTICAST
);
180 *dest
= raw_smp_processor_id();
182 case NFT_META_IIFGROUP
:
187 case NFT_META_OIFGROUP
:
192 #ifdef CONFIG_CGROUP_NET_CLASSID
193 case NFT_META_CGROUP
:
194 sk
= skb_to_full_sk(skb
);
195 if (!sk
|| !sk_fullsock(sk
) ||
196 !net_eq(nft_net(pkt
), sock_net(sk
)))
198 *dest
= sock_cgroup_classid(&sk
->sk_cgrp_data
);
201 case NFT_META_PRANDOM
: {
202 struct rnd_state
*state
= this_cpu_ptr(&nft_prandom_state
);
203 *dest
= prandom_u32_state(state
);
207 case NFT_META_SECPATH
:
208 nft_reg_store8(dest
, secpath_exists(skb
));
211 case NFT_META_IIFKIND
:
212 if (in
== NULL
|| in
->rtnl_link_ops
== NULL
)
214 strncpy((char *)dest
, in
->rtnl_link_ops
->kind
, IFNAMSIZ
);
216 case NFT_META_OIFKIND
:
217 if (out
== NULL
|| out
->rtnl_link_ops
== NULL
)
219 strncpy((char *)dest
, out
->rtnl_link_ops
->kind
, IFNAMSIZ
);
228 regs
->verdict
.code
= NFT_BREAK
;
230 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
232 void nft_meta_set_eval(const struct nft_expr
*expr
,
233 struct nft_regs
*regs
,
234 const struct nft_pktinfo
*pkt
)
236 const struct nft_meta
*meta
= nft_expr_priv(expr
);
237 struct sk_buff
*skb
= pkt
->skb
;
238 u32
*sreg
= ®s
->data
[meta
->sreg
];
246 case NFT_META_PRIORITY
:
247 skb
->priority
= value
;
249 case NFT_META_PKTTYPE
:
250 value8
= nft_reg_load8(sreg
);
252 if (skb
->pkt_type
!= value8
&&
253 skb_pkt_type_ok(value8
) &&
254 skb_pkt_type_ok(skb
->pkt_type
))
255 skb
->pkt_type
= value8
;
257 case NFT_META_NFTRACE
:
258 value8
= nft_reg_load8(sreg
);
260 skb
->nf_trace
= !!value8
;
262 #ifdef CONFIG_NETWORK_SECMARK
263 case NFT_META_SECMARK
:
264 skb
->secmark
= value
;
271 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
273 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
274 [NFTA_META_DREG
] = { .type
= NLA_U32
},
275 [NFTA_META_KEY
] = { .type
= NLA_U32
},
276 [NFTA_META_SREG
] = { .type
= NLA_U32
},
278 EXPORT_SYMBOL_GPL(nft_meta_policy
);
280 int nft_meta_get_init(const struct nft_ctx
*ctx
,
281 const struct nft_expr
*expr
,
282 const struct nlattr
* const tb
[])
284 struct nft_meta
*priv
= nft_expr_priv(expr
);
287 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
289 case NFT_META_PROTOCOL
:
290 case NFT_META_IIFTYPE
:
291 case NFT_META_OIFTYPE
:
294 case NFT_META_NFPROTO
:
295 case NFT_META_L4PROTO
:
297 case NFT_META_PRIORITY
:
303 #ifdef CONFIG_IP_ROUTE_CLASSID
304 case NFT_META_RTCLASSID
:
306 #ifdef CONFIG_NETWORK_SECMARK
307 case NFT_META_SECMARK
:
309 case NFT_META_PKTTYPE
:
311 case NFT_META_IIFGROUP
:
312 case NFT_META_OIFGROUP
:
313 #ifdef CONFIG_CGROUP_NET_CLASSID
314 case NFT_META_CGROUP
:
318 case NFT_META_IIFNAME
:
319 case NFT_META_OIFNAME
:
320 case NFT_META_IIFKIND
:
321 case NFT_META_OIFKIND
:
324 case NFT_META_PRANDOM
:
325 prandom_init_once(&nft_prandom_state
);
329 case NFT_META_SECPATH
:
337 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
338 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
339 NFT_DATA_VALUE
, len
);
341 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
343 static int nft_meta_get_validate(const struct nft_ctx
*ctx
,
344 const struct nft_expr
*expr
,
345 const struct nft_data
**data
)
348 const struct nft_meta
*priv
= nft_expr_priv(expr
);
351 if (priv
->key
!= NFT_META_SECPATH
)
354 switch (ctx
->family
) {
356 hooks
= 1 << NF_NETDEV_INGRESS
;
361 hooks
= (1 << NF_INET_PRE_ROUTING
) |
362 (1 << NF_INET_LOCAL_IN
) |
363 (1 << NF_INET_FORWARD
);
369 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
375 int nft_meta_set_validate(const struct nft_ctx
*ctx
,
376 const struct nft_expr
*expr
,
377 const struct nft_data
**data
)
379 struct nft_meta
*priv
= nft_expr_priv(expr
);
382 if (priv
->key
!= NFT_META_PKTTYPE
)
385 switch (ctx
->family
) {
387 hooks
= 1 << NF_BR_PRE_ROUTING
;
390 hooks
= 1 << NF_NETDEV_INGRESS
;
395 hooks
= 1 << NF_INET_PRE_ROUTING
;
401 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
403 EXPORT_SYMBOL_GPL(nft_meta_set_validate
);
405 int nft_meta_set_init(const struct nft_ctx
*ctx
,
406 const struct nft_expr
*expr
,
407 const struct nlattr
* const tb
[])
409 struct nft_meta
*priv
= nft_expr_priv(expr
);
413 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
416 case NFT_META_PRIORITY
:
417 #ifdef CONFIG_NETWORK_SECMARK
418 case NFT_META_SECMARK
:
422 case NFT_META_NFTRACE
:
425 case NFT_META_PKTTYPE
:
432 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
433 err
= nft_validate_register_load(priv
->sreg
, len
);
437 if (priv
->key
== NFT_META_NFTRACE
)
438 static_branch_inc(&nft_trace_enabled
);
442 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
444 int nft_meta_get_dump(struct sk_buff
*skb
,
445 const struct nft_expr
*expr
)
447 const struct nft_meta
*priv
= nft_expr_priv(expr
);
449 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
450 goto nla_put_failure
;
451 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
452 goto nla_put_failure
;
458 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
460 int nft_meta_set_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
462 const struct nft_meta
*priv
= nft_expr_priv(expr
);
464 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
465 goto nla_put_failure
;
466 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
467 goto nla_put_failure
;
474 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
476 void nft_meta_set_destroy(const struct nft_ctx
*ctx
,
477 const struct nft_expr
*expr
)
479 const struct nft_meta
*priv
= nft_expr_priv(expr
);
481 if (priv
->key
== NFT_META_NFTRACE
)
482 static_branch_dec(&nft_trace_enabled
);
484 EXPORT_SYMBOL_GPL(nft_meta_set_destroy
);
486 static int nft_meta_get_offload(struct nft_offload_ctx
*ctx
,
487 struct nft_flow_rule
*flow
,
488 const struct nft_expr
*expr
)
490 const struct nft_meta
*priv
= nft_expr_priv(expr
);
491 struct nft_offload_reg
*reg
= &ctx
->regs
[priv
->dreg
];
494 case NFT_META_PROTOCOL
:
495 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC
, basic
, n_proto
,
497 nft_offload_set_dependency(ctx
, NFT_OFFLOAD_DEP_NETWORK
);
499 case NFT_META_L4PROTO
:
500 NFT_OFFLOAD_MATCH(FLOW_DISSECTOR_KEY_BASIC
, basic
, ip_proto
,
502 nft_offload_set_dependency(ctx
, NFT_OFFLOAD_DEP_TRANSPORT
);
511 static const struct nft_expr_ops nft_meta_get_ops
= {
512 .type
= &nft_meta_type
,
513 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
514 .eval
= nft_meta_get_eval
,
515 .init
= nft_meta_get_init
,
516 .dump
= nft_meta_get_dump
,
517 .validate
= nft_meta_get_validate
,
518 .offload
= nft_meta_get_offload
,
521 static const struct nft_expr_ops nft_meta_set_ops
= {
522 .type
= &nft_meta_type
,
523 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
524 .eval
= nft_meta_set_eval
,
525 .init
= nft_meta_set_init
,
526 .destroy
= nft_meta_set_destroy
,
527 .dump
= nft_meta_set_dump
,
528 .validate
= nft_meta_set_validate
,
531 static const struct nft_expr_ops
*
532 nft_meta_select_ops(const struct nft_ctx
*ctx
,
533 const struct nlattr
* const tb
[])
535 if (tb
[NFTA_META_KEY
] == NULL
)
536 return ERR_PTR(-EINVAL
);
538 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
539 return ERR_PTR(-EINVAL
);
541 #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE) && IS_MODULE(CONFIG_NFT_BRIDGE_META)
542 if (ctx
->family
== NFPROTO_BRIDGE
)
543 return ERR_PTR(-EAGAIN
);
545 if (tb
[NFTA_META_DREG
])
546 return &nft_meta_get_ops
;
548 if (tb
[NFTA_META_SREG
])
549 return &nft_meta_set_ops
;
551 return ERR_PTR(-EINVAL
);
554 struct nft_expr_type nft_meta_type __read_mostly
= {
556 .select_ops
= nft_meta_select_ops
,
557 .policy
= nft_meta_policy
,
558 .maxattr
= NFTA_META_MAX
,
559 .owner
= THIS_MODULE
,
562 #ifdef CONFIG_NETWORK_SECMARK
568 static const struct nla_policy nft_secmark_policy
[NFTA_SECMARK_MAX
+ 1] = {
569 [NFTA_SECMARK_CTX
] = { .type
= NLA_STRING
, .len
= NFT_SECMARK_CTX_MAXLEN
},
572 static int nft_secmark_compute_secid(struct nft_secmark
*priv
)
577 err
= security_secctx_to_secid(priv
->ctx
, strlen(priv
->ctx
), &tmp_secid
);
584 err
= security_secmark_relabel_packet(tmp_secid
);
588 priv
->secid
= tmp_secid
;
592 static void nft_secmark_obj_eval(struct nft_object
*obj
, struct nft_regs
*regs
,
593 const struct nft_pktinfo
*pkt
)
595 const struct nft_secmark
*priv
= nft_obj_data(obj
);
596 struct sk_buff
*skb
= pkt
->skb
;
598 skb
->secmark
= priv
->secid
;
601 static int nft_secmark_obj_init(const struct nft_ctx
*ctx
,
602 const struct nlattr
* const tb
[],
603 struct nft_object
*obj
)
605 struct nft_secmark
*priv
= nft_obj_data(obj
);
608 if (tb
[NFTA_SECMARK_CTX
] == NULL
)
611 priv
->ctx
= nla_strdup(tb
[NFTA_SECMARK_CTX
], GFP_KERNEL
);
615 err
= nft_secmark_compute_secid(priv
);
621 security_secmark_refcount_inc();
626 static int nft_secmark_obj_dump(struct sk_buff
*skb
, struct nft_object
*obj
,
629 struct nft_secmark
*priv
= nft_obj_data(obj
);
632 if (nla_put_string(skb
, NFTA_SECMARK_CTX
, priv
->ctx
))
636 err
= nft_secmark_compute_secid(priv
);
644 static void nft_secmark_obj_destroy(const struct nft_ctx
*ctx
, struct nft_object
*obj
)
646 struct nft_secmark
*priv
= nft_obj_data(obj
);
648 security_secmark_refcount_dec();
653 static const struct nft_object_ops nft_secmark_obj_ops
= {
654 .type
= &nft_secmark_obj_type
,
655 .size
= sizeof(struct nft_secmark
),
656 .init
= nft_secmark_obj_init
,
657 .eval
= nft_secmark_obj_eval
,
658 .dump
= nft_secmark_obj_dump
,
659 .destroy
= nft_secmark_obj_destroy
,
661 struct nft_object_type nft_secmark_obj_type __read_mostly
= {
662 .type
= NFT_OBJECT_SECMARK
,
663 .ops
= &nft_secmark_obj_ops
,
664 .maxattr
= NFTA_SECMARK_MAX
,
665 .policy
= nft_secmark_policy
,
666 .owner
= THIS_MODULE
,
668 #endif /* CONFIG_NETWORK_SECMARK */