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>
19 #include <linux/ipv6.h>
20 #include <linux/smp.h>
21 #include <linux/static_key.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 */
31 enum nft_meta_keys key
:8;
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"
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
);
52 u32
*dest
= ®s
->data
[priv
->dreg
];
53 #ifdef CONFIG_NF_TABLES_BRIDGE
54 const struct net_bridge_port
*p
;
61 case NFT_META_PROTOCOL
:
62 nft_reg_store16(dest
, (__force u16
)skb
->protocol
);
64 case NFT_META_NFPROTO
:
65 nft_reg_store8(dest
, nft_pf(pkt
));
67 case NFT_META_L4PROTO
:
70 nft_reg_store8(dest
, pkt
->tprot
);
72 case NFT_META_PRIORITY
:
73 *dest
= skb
->priority
;
88 case NFT_META_IIFNAME
:
91 strncpy((char *)dest
, in
->name
, IFNAMSIZ
);
93 case NFT_META_OIFNAME
:
96 strncpy((char *)dest
, out
->name
, IFNAMSIZ
);
98 case NFT_META_IIFTYPE
:
101 nft_reg_store16(dest
, in
->type
);
103 case NFT_META_OIFTYPE
:
106 nft_reg_store16(dest
, out
->type
);
109 sk
= skb_to_full_sk(skb
);
110 if (!sk
|| !sk_fullsock(sk
) ||
111 !net_eq(nft_net(pkt
), sock_net(sk
)))
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
);
121 *dest
= from_kuid_munged(&init_user_ns
,
122 sk
->sk_socket
->file
->f_cred
->fsuid
);
123 read_unlock_bh(&sk
->sk_callback_lock
);
126 sk
= skb_to_full_sk(skb
);
127 if (!sk
|| !sk_fullsock(sk
) ||
128 !net_eq(nft_net(pkt
), sock_net(sk
)))
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
);
137 *dest
= from_kgid_munged(&init_user_ns
,
138 sk
->sk_socket
->file
->f_cred
->fsgid
);
139 read_unlock_bh(&sk
->sk_callback_lock
);
141 #ifdef CONFIG_IP_ROUTE_CLASSID
142 case NFT_META_RTCLASSID
: {
143 const struct dst_entry
*dst
= skb_dst(skb
);
147 *dest
= dst
->tclassid
;
151 #ifdef CONFIG_NETWORK_SECMARK
152 case NFT_META_SECMARK
:
153 *dest
= skb
->secmark
;
156 case NFT_META_PKTTYPE
:
157 if (skb
->pkt_type
!= PACKET_LOOPBACK
) {
158 nft_reg_store8(dest
, skb
->pkt_type
);
162 switch (nft_pf(pkt
)) {
164 if (ipv4_is_multicast(ip_hdr(skb
)->daddr
))
165 nft_reg_store8(dest
, PACKET_MULTICAST
);
167 nft_reg_store8(dest
, PACKET_BROADCAST
);
170 nft_reg_store8(dest
, PACKET_MULTICAST
);
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
);
183 if (ipv4_is_multicast(iph
->daddr
))
184 nft_reg_store8(dest
, PACKET_MULTICAST
);
186 nft_reg_store8(dest
, PACKET_BROADCAST
);
190 case htons(ETH_P_IPV6
):
191 nft_reg_store8(dest
, PACKET_MULTICAST
);
204 *dest
= raw_smp_processor_id();
206 case NFT_META_IIFGROUP
:
211 case NFT_META_OIFGROUP
:
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
)))
222 *dest
= sock_cgroup_classid(&sk
->sk_cgrp_data
);
225 case NFT_META_PRANDOM
: {
226 struct rnd_state
*state
= this_cpu_ptr(&nft_prandom_state
);
227 *dest
= prandom_u32_state(state
);
231 case NFT_META_SECPATH
:
232 nft_reg_store8(dest
, !!skb
->sp
);
235 #ifdef CONFIG_NF_TABLES_BRIDGE
236 case NFT_META_BRI_IIFNAME
:
237 if (in
== NULL
|| (p
= br_port_get_rcu(in
)) == NULL
)
239 strncpy((char *)dest
, p
->br
->dev
->name
, IFNAMSIZ
);
241 case NFT_META_BRI_OIFNAME
:
242 if (out
== NULL
|| (p
= br_port_get_rcu(out
)) == NULL
)
244 strncpy((char *)dest
, p
->br
->dev
->name
, IFNAMSIZ
);
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
= ®s
->data
[meta
->sreg
];
271 case NFT_META_PRIORITY
:
272 skb
->priority
= value
;
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
;
282 case NFT_META_NFTRACE
:
283 value8
= nft_reg_load8(sreg
);
285 skb
->nf_trace
= !!value8
;
287 #ifdef CONFIG_NETWORK_SECMARK
288 case NFT_META_SECMARK
:
289 skb
->secmark
= value
;
297 static const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
298 [NFTA_META_DREG
] = { .type
= NLA_U32
},
299 [NFTA_META_KEY
] = { .type
= NLA_U32
},
300 [NFTA_META_SREG
] = { .type
= NLA_U32
},
303 static int nft_meta_get_init(const struct nft_ctx
*ctx
,
304 const struct nft_expr
*expr
,
305 const struct nlattr
* const tb
[])
307 struct nft_meta
*priv
= nft_expr_priv(expr
);
310 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
312 case NFT_META_PROTOCOL
:
313 case NFT_META_IIFTYPE
:
314 case NFT_META_OIFTYPE
:
317 case NFT_META_NFPROTO
:
318 case NFT_META_L4PROTO
:
320 case NFT_META_PRIORITY
:
326 #ifdef CONFIG_IP_ROUTE_CLASSID
327 case NFT_META_RTCLASSID
:
329 #ifdef CONFIG_NETWORK_SECMARK
330 case NFT_META_SECMARK
:
332 case NFT_META_PKTTYPE
:
334 case NFT_META_IIFGROUP
:
335 case NFT_META_OIFGROUP
:
336 #ifdef CONFIG_CGROUP_NET_CLASSID
337 case NFT_META_CGROUP
:
341 case NFT_META_IIFNAME
:
342 case NFT_META_OIFNAME
:
345 case NFT_META_PRANDOM
:
346 prandom_init_once(&nft_prandom_state
);
350 case NFT_META_SECPATH
:
354 #ifdef CONFIG_NF_TABLES_BRIDGE
355 case NFT_META_BRI_IIFNAME
:
356 case NFT_META_BRI_OIFNAME
:
357 if (ctx
->family
!= NFPROTO_BRIDGE
)
366 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
367 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
368 NFT_DATA_VALUE
, len
);
371 static int nft_meta_get_validate(const struct nft_ctx
*ctx
,
372 const struct nft_expr
*expr
,
373 const struct nft_data
**data
)
376 const struct nft_meta
*priv
= nft_expr_priv(expr
);
379 if (priv
->key
!= NFT_META_SECPATH
)
382 switch (ctx
->family
) {
384 hooks
= 1 << NF_NETDEV_INGRESS
;
389 hooks
= (1 << NF_INET_PRE_ROUTING
) |
390 (1 << NF_INET_LOCAL_IN
) |
391 (1 << NF_INET_FORWARD
);
397 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
403 static int nft_meta_set_validate(const struct nft_ctx
*ctx
,
404 const struct nft_expr
*expr
,
405 const struct nft_data
**data
)
407 struct nft_meta
*priv
= nft_expr_priv(expr
);
410 if (priv
->key
!= NFT_META_PKTTYPE
)
413 switch (ctx
->family
) {
415 hooks
= 1 << NF_BR_PRE_ROUTING
;
418 hooks
= 1 << NF_NETDEV_INGRESS
;
423 hooks
= 1 << NF_INET_PRE_ROUTING
;
429 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
432 static int nft_meta_set_init(const struct nft_ctx
*ctx
,
433 const struct nft_expr
*expr
,
434 const struct nlattr
* const tb
[])
436 struct nft_meta
*priv
= nft_expr_priv(expr
);
440 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
443 case NFT_META_PRIORITY
:
444 #ifdef CONFIG_NETWORK_SECMARK
445 case NFT_META_SECMARK
:
449 case NFT_META_NFTRACE
:
452 case NFT_META_PKTTYPE
:
459 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
460 err
= nft_validate_register_load(priv
->sreg
, len
);
464 if (priv
->key
== NFT_META_NFTRACE
)
465 static_branch_inc(&nft_trace_enabled
);
470 static int nft_meta_get_dump(struct sk_buff
*skb
,
471 const struct nft_expr
*expr
)
473 const struct nft_meta
*priv
= nft_expr_priv(expr
);
475 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
476 goto nla_put_failure
;
477 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
478 goto nla_put_failure
;
485 static int nft_meta_set_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
487 const struct nft_meta
*priv
= nft_expr_priv(expr
);
489 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
490 goto nla_put_failure
;
491 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
492 goto nla_put_failure
;
500 static void nft_meta_set_destroy(const struct nft_ctx
*ctx
,
501 const struct nft_expr
*expr
)
503 const struct nft_meta
*priv
= nft_expr_priv(expr
);
505 if (priv
->key
== NFT_META_NFTRACE
)
506 static_branch_dec(&nft_trace_enabled
);
509 static const struct nft_expr_ops nft_meta_get_ops
= {
510 .type
= &nft_meta_type
,
511 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
512 .eval
= nft_meta_get_eval
,
513 .init
= nft_meta_get_init
,
514 .dump
= nft_meta_get_dump
,
515 .validate
= nft_meta_get_validate
,
518 static const struct nft_expr_ops nft_meta_set_ops
= {
519 .type
= &nft_meta_type
,
520 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
521 .eval
= nft_meta_set_eval
,
522 .init
= nft_meta_set_init
,
523 .destroy
= nft_meta_set_destroy
,
524 .dump
= nft_meta_set_dump
,
525 .validate
= nft_meta_set_validate
,
528 static const struct nft_expr_ops
*
529 nft_meta_select_ops(const struct nft_ctx
*ctx
,
530 const struct nlattr
* const tb
[])
532 if (tb
[NFTA_META_KEY
] == NULL
)
533 return ERR_PTR(-EINVAL
);
535 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
536 return ERR_PTR(-EINVAL
);
538 if (tb
[NFTA_META_DREG
])
539 return &nft_meta_get_ops
;
541 if (tb
[NFTA_META_SREG
])
542 return &nft_meta_set_ops
;
544 return ERR_PTR(-EINVAL
);
547 struct nft_expr_type nft_meta_type __read_mostly
= {
549 .select_ops
= nft_meta_select_ops
,
550 .policy
= nft_meta_policy
,
551 .maxattr
= NFTA_META_MAX
,
552 .owner
= THIS_MODULE
,
555 #ifdef CONFIG_NETWORK_SECMARK
561 static const struct nla_policy nft_secmark_policy
[NFTA_SECMARK_MAX
+ 1] = {
562 [NFTA_SECMARK_CTX
] = { .type
= NLA_STRING
, .len
= NFT_SECMARK_CTX_MAXLEN
},
565 static int nft_secmark_compute_secid(struct nft_secmark
*priv
)
570 err
= security_secctx_to_secid(priv
->ctx
, strlen(priv
->ctx
), &tmp_secid
);
577 err
= security_secmark_relabel_packet(tmp_secid
);
581 priv
->secid
= tmp_secid
;
585 static void nft_secmark_obj_eval(struct nft_object
*obj
, struct nft_regs
*regs
,
586 const struct nft_pktinfo
*pkt
)
588 const struct nft_secmark
*priv
= nft_obj_data(obj
);
589 struct sk_buff
*skb
= pkt
->skb
;
591 skb
->secmark
= priv
->secid
;
594 static int nft_secmark_obj_init(const struct nft_ctx
*ctx
,
595 const struct nlattr
* const tb
[],
596 struct nft_object
*obj
)
598 struct nft_secmark
*priv
= nft_obj_data(obj
);
601 if (tb
[NFTA_SECMARK_CTX
] == NULL
)
604 priv
->ctx
= nla_strdup(tb
[NFTA_SECMARK_CTX
], GFP_KERNEL
);
608 err
= nft_secmark_compute_secid(priv
);
614 security_secmark_refcount_inc();
619 static int nft_secmark_obj_dump(struct sk_buff
*skb
, struct nft_object
*obj
,
622 struct nft_secmark
*priv
= nft_obj_data(obj
);
625 if (nla_put_string(skb
, NFTA_SECMARK_CTX
, priv
->ctx
))
629 err
= nft_secmark_compute_secid(priv
);
637 static void nft_secmark_obj_destroy(const struct nft_ctx
*ctx
, struct nft_object
*obj
)
639 struct nft_secmark
*priv
= nft_obj_data(obj
);
641 security_secmark_refcount_dec();
646 static const struct nft_object_ops nft_secmark_obj_ops
= {
647 .type
= &nft_secmark_obj_type
,
648 .size
= sizeof(struct nft_secmark
),
649 .init
= nft_secmark_obj_init
,
650 .eval
= nft_secmark_obj_eval
,
651 .dump
= nft_secmark_obj_dump
,
652 .destroy
= nft_secmark_obj_destroy
,
654 struct nft_object_type nft_secmark_obj_type __read_mostly
= {
655 .type
= NFT_OBJECT_SECMARK
,
656 .ops
= &nft_secmark_obj_ops
,
657 .maxattr
= NFTA_SECMARK_MAX
,
658 .policy
= nft_secmark_policy
,
659 .owner
= THIS_MODULE
,
661 #endif /* CONFIG_NETWORK_SECMARK */