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
;
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
);
305 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
307 case NFT_META_PROTOCOL
:
308 case NFT_META_IIFTYPE
:
309 case NFT_META_OIFTYPE
:
312 case NFT_META_NFPROTO
:
313 case NFT_META_L4PROTO
:
315 case NFT_META_PRIORITY
:
321 #ifdef CONFIG_IP_ROUTE_CLASSID
322 case NFT_META_RTCLASSID
:
324 #ifdef CONFIG_NETWORK_SECMARK
325 case NFT_META_SECMARK
:
327 case NFT_META_PKTTYPE
:
329 case NFT_META_IIFGROUP
:
330 case NFT_META_OIFGROUP
:
331 #ifdef CONFIG_CGROUP_NET_CLASSID
332 case NFT_META_CGROUP
:
336 case NFT_META_IIFNAME
:
337 case NFT_META_OIFNAME
:
340 case NFT_META_PRANDOM
:
341 prandom_init_once(&nft_prandom_state
);
345 case NFT_META_SECPATH
:
349 #ifdef CONFIG_NF_TABLES_BRIDGE
350 case NFT_META_BRI_IIFNAME
:
351 case NFT_META_BRI_OIFNAME
:
352 if (ctx
->family
!= NFPROTO_BRIDGE
)
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
)
371 const struct nft_meta
*priv
= nft_expr_priv(expr
);
374 if (priv
->key
!= NFT_META_SECPATH
)
377 switch (ctx
->family
) {
379 hooks
= 1 << NF_NETDEV_INGRESS
;
384 hooks
= (1 << NF_INET_PRE_ROUTING
) |
385 (1 << NF_INET_LOCAL_IN
) |
386 (1 << NF_INET_FORWARD
);
392 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
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
);
405 if (priv
->key
!= NFT_META_PKTTYPE
)
408 switch (ctx
->family
) {
410 hooks
= 1 << NF_BR_PRE_ROUTING
;
413 hooks
= 1 << NF_NETDEV_INGRESS
;
418 hooks
= 1 << NF_INET_PRE_ROUTING
;
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
);
435 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
438 case NFT_META_PRIORITY
:
441 case NFT_META_NFTRACE
:
444 case NFT_META_PKTTYPE
:
451 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
452 err
= nft_validate_register_load(priv
->sreg
, len
);
456 if (priv
->key
== NFT_META_NFTRACE
)
457 static_branch_inc(&nft_trace_enabled
);
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
;
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
;
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
= {
541 .select_ops
= nft_meta_select_ops
,
542 .policy
= nft_meta_policy
,
543 .maxattr
= NFTA_META_MAX
,
544 .owner
= THIS_MODULE
,