2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
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>
27 #include <net/netfilter/nft_meta.h>
29 #include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */
31 static DEFINE_PER_CPU(struct rnd_state
, nft_prandom_state
);
33 void nft_meta_get_eval(const struct nft_expr
*expr
,
34 struct nft_regs
*regs
,
35 const struct nft_pktinfo
*pkt
)
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
);
41 u32
*dest
= ®s
->data
[priv
->dreg
];
47 case NFT_META_PROTOCOL
:
49 *(__be16
*)dest
= skb
->protocol
;
51 case NFT_META_NFPROTO
:
54 case NFT_META_L4PROTO
:
59 case NFT_META_PRIORITY
:
60 *dest
= skb
->priority
;
75 case NFT_META_IIFNAME
:
78 strncpy((char *)dest
, in
->name
, IFNAMSIZ
);
80 case NFT_META_OIFNAME
:
83 strncpy((char *)dest
, out
->name
, IFNAMSIZ
);
85 case NFT_META_IIFTYPE
:
89 *(u16
*)dest
= in
->type
;
91 case NFT_META_OIFTYPE
:
95 *(u16
*)dest
= out
->type
;
98 sk
= skb_to_full_sk(skb
);
99 if (!sk
|| !sk_fullsock(sk
))
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
);
109 *dest
= from_kuid_munged(&init_user_ns
,
110 sk
->sk_socket
->file
->f_cred
->fsuid
);
111 read_unlock_bh(&sk
->sk_callback_lock
);
114 sk
= skb_to_full_sk(skb
);
115 if (!sk
|| !sk_fullsock(sk
))
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
);
124 *dest
= from_kgid_munged(&init_user_ns
,
125 sk
->sk_socket
->file
->f_cred
->fsgid
);
126 read_unlock_bh(&sk
->sk_callback_lock
);
128 #ifdef CONFIG_IP_ROUTE_CLASSID
129 case NFT_META_RTCLASSID
: {
130 const struct dst_entry
*dst
= skb_dst(skb
);
134 *dest
= dst
->tclassid
;
138 #ifdef CONFIG_NETWORK_SECMARK
139 case NFT_META_SECMARK
:
140 *dest
= skb
->secmark
;
143 case NFT_META_PKTTYPE
:
144 if (skb
->pkt_type
!= PACKET_LOOPBACK
) {
145 *dest
= skb
->pkt_type
;
149 switch (nft_pf(pkt
)) {
151 if (ipv4_is_multicast(ip_hdr(skb
)->daddr
))
152 *dest
= PACKET_MULTICAST
;
154 *dest
= PACKET_BROADCAST
;
157 *dest
= PACKET_MULTICAST
;
160 switch (skb
->protocol
) {
161 case htons(ETH_P_IP
): {
162 int noff
= skb_network_offset(skb
);
163 struct iphdr
*iph
, _iph
;
165 iph
= skb_header_pointer(skb
, noff
,
166 sizeof(_iph
), &_iph
);
170 if (ipv4_is_multicast(iph
->daddr
))
171 *dest
= PACKET_MULTICAST
;
173 *dest
= PACKET_BROADCAST
;
177 case htons(ETH_P_IPV6
):
178 *dest
= PACKET_MULTICAST
;
191 *dest
= raw_smp_processor_id();
193 case NFT_META_IIFGROUP
:
198 case NFT_META_OIFGROUP
:
203 #ifdef CONFIG_CGROUP_NET_CLASSID
204 case NFT_META_CGROUP
:
205 sk
= skb_to_full_sk(skb
);
206 if (!sk
|| !sk_fullsock(sk
))
208 *dest
= sock_cgroup_classid(&sk
->sk_cgrp_data
);
211 case NFT_META_PRANDOM
: {
212 struct rnd_state
*state
= this_cpu_ptr(&nft_prandom_state
);
213 *dest
= prandom_u32_state(state
);
223 regs
->verdict
.code
= NFT_BREAK
;
225 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
227 void nft_meta_set_eval(const struct nft_expr
*expr
,
228 struct nft_regs
*regs
,
229 const struct nft_pktinfo
*pkt
)
231 const struct nft_meta
*meta
= nft_expr_priv(expr
);
232 struct sk_buff
*skb
= pkt
->skb
;
233 u32 value
= regs
->data
[meta
->sreg
];
239 case NFT_META_PRIORITY
:
240 skb
->priority
= value
;
242 case NFT_META_PKTTYPE
:
243 if (skb
->pkt_type
!= value
&&
244 skb_pkt_type_ok(value
) && skb_pkt_type_ok(skb
->pkt_type
))
245 skb
->pkt_type
= value
;
247 case NFT_META_NFTRACE
:
248 skb
->nf_trace
= !!value
;
254 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
256 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
257 [NFTA_META_DREG
] = { .type
= NLA_U32
},
258 [NFTA_META_KEY
] = { .type
= NLA_U32
},
259 [NFTA_META_SREG
] = { .type
= NLA_U32
},
261 EXPORT_SYMBOL_GPL(nft_meta_policy
);
263 int nft_meta_get_init(const struct nft_ctx
*ctx
,
264 const struct nft_expr
*expr
,
265 const struct nlattr
* const tb
[])
267 struct nft_meta
*priv
= nft_expr_priv(expr
);
270 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
272 case NFT_META_PROTOCOL
:
273 case NFT_META_IIFTYPE
:
274 case NFT_META_OIFTYPE
:
277 case NFT_META_NFPROTO
:
278 case NFT_META_L4PROTO
:
280 case NFT_META_PRIORITY
:
286 #ifdef CONFIG_IP_ROUTE_CLASSID
287 case NFT_META_RTCLASSID
:
289 #ifdef CONFIG_NETWORK_SECMARK
290 case NFT_META_SECMARK
:
292 case NFT_META_PKTTYPE
:
294 case NFT_META_IIFGROUP
:
295 case NFT_META_OIFGROUP
:
296 #ifdef CONFIG_CGROUP_NET_CLASSID
297 case NFT_META_CGROUP
:
301 case NFT_META_IIFNAME
:
302 case NFT_META_OIFNAME
:
305 case NFT_META_PRANDOM
:
306 prandom_init_once(&nft_prandom_state
);
313 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
314 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
315 NFT_DATA_VALUE
, len
);
317 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
319 int nft_meta_set_validate(const struct nft_ctx
*ctx
,
320 const struct nft_expr
*expr
,
321 const struct nft_data
**data
)
323 struct nft_meta
*priv
= nft_expr_priv(expr
);
326 if (priv
->key
!= NFT_META_PKTTYPE
)
329 switch (ctx
->afi
->family
) {
331 hooks
= 1 << NF_BR_PRE_ROUTING
;
334 hooks
= 1 << NF_NETDEV_INGRESS
;
339 hooks
= 1 << NF_INET_PRE_ROUTING
;
345 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
347 EXPORT_SYMBOL_GPL(nft_meta_set_validate
);
349 int nft_meta_set_init(const struct nft_ctx
*ctx
,
350 const struct nft_expr
*expr
,
351 const struct nlattr
* const tb
[])
353 struct nft_meta
*priv
= nft_expr_priv(expr
);
357 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
360 case NFT_META_PRIORITY
:
363 case NFT_META_NFTRACE
:
366 case NFT_META_PKTTYPE
:
373 err
= nft_meta_set_validate(ctx
, expr
, NULL
);
377 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
378 err
= nft_validate_register_load(priv
->sreg
, len
);
382 if (priv
->key
== NFT_META_NFTRACE
)
383 static_branch_inc(&nft_trace_enabled
);
387 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
389 int nft_meta_get_dump(struct sk_buff
*skb
,
390 const struct nft_expr
*expr
)
392 const struct nft_meta
*priv
= nft_expr_priv(expr
);
394 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
395 goto nla_put_failure
;
396 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
397 goto nla_put_failure
;
403 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
405 int nft_meta_set_dump(struct sk_buff
*skb
,
406 const struct nft_expr
*expr
)
408 const struct nft_meta
*priv
= nft_expr_priv(expr
);
410 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
411 goto nla_put_failure
;
412 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
413 goto nla_put_failure
;
420 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
422 void nft_meta_set_destroy(const struct nft_ctx
*ctx
,
423 const struct nft_expr
*expr
)
425 const struct nft_meta
*priv
= nft_expr_priv(expr
);
427 if (priv
->key
== NFT_META_NFTRACE
)
428 static_branch_dec(&nft_trace_enabled
);
430 EXPORT_SYMBOL_GPL(nft_meta_set_destroy
);
432 static struct nft_expr_type nft_meta_type
;
433 static const struct nft_expr_ops nft_meta_get_ops
= {
434 .type
= &nft_meta_type
,
435 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
436 .eval
= nft_meta_get_eval
,
437 .init
= nft_meta_get_init
,
438 .dump
= nft_meta_get_dump
,
441 static const struct nft_expr_ops nft_meta_set_ops
= {
442 .type
= &nft_meta_type
,
443 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
444 .eval
= nft_meta_set_eval
,
445 .init
= nft_meta_set_init
,
446 .destroy
= nft_meta_set_destroy
,
447 .dump
= nft_meta_set_dump
,
448 .validate
= nft_meta_set_validate
,
451 static const struct nft_expr_ops
*
452 nft_meta_select_ops(const struct nft_ctx
*ctx
,
453 const struct nlattr
* const tb
[])
455 if (tb
[NFTA_META_KEY
] == NULL
)
456 return ERR_PTR(-EINVAL
);
458 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
459 return ERR_PTR(-EINVAL
);
461 if (tb
[NFTA_META_DREG
])
462 return &nft_meta_get_ops
;
464 if (tb
[NFTA_META_SREG
])
465 return &nft_meta_set_ops
;
467 return ERR_PTR(-EINVAL
);
470 static struct nft_expr_type nft_meta_type __read_mostly
= {
472 .select_ops
= &nft_meta_select_ops
,
473 .policy
= nft_meta_policy
,
474 .maxattr
= NFTA_META_MAX
,
475 .owner
= THIS_MODULE
,
478 static int __init
nft_meta_module_init(void)
480 return nft_register_expr(&nft_meta_type
);
483 static void __exit
nft_meta_module_exit(void)
485 nft_unregister_expr(&nft_meta_type
);
488 module_init(nft_meta_module_init
);
489 module_exit(nft_meta_module_exit
);
491 MODULE_LICENSE("GPL");
492 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
493 MODULE_ALIAS_NFT_EXPR("meta");