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
:
48 nft_reg_store16(dest
, (__force u16
)skb
->protocol
);
50 case NFT_META_NFPROTO
:
51 nft_reg_store8(dest
, nft_pf(pkt
));
53 case NFT_META_L4PROTO
:
56 nft_reg_store8(dest
, pkt
->tprot
);
58 case NFT_META_PRIORITY
:
59 *dest
= skb
->priority
;
74 case NFT_META_IIFNAME
:
77 strncpy((char *)dest
, in
->name
, IFNAMSIZ
);
79 case NFT_META_OIFNAME
:
82 strncpy((char *)dest
, out
->name
, IFNAMSIZ
);
84 case NFT_META_IIFTYPE
:
87 nft_reg_store16(dest
, in
->type
);
89 case NFT_META_OIFTYPE
:
92 nft_reg_store16(dest
, out
->type
);
95 sk
= skb_to_full_sk(skb
);
96 if (!sk
|| !sk_fullsock(sk
))
99 read_lock_bh(&sk
->sk_callback_lock
);
100 if (sk
->sk_socket
== NULL
||
101 sk
->sk_socket
->file
== NULL
) {
102 read_unlock_bh(&sk
->sk_callback_lock
);
106 *dest
= from_kuid_munged(&init_user_ns
,
107 sk
->sk_socket
->file
->f_cred
->fsuid
);
108 read_unlock_bh(&sk
->sk_callback_lock
);
111 sk
= skb_to_full_sk(skb
);
112 if (!sk
|| !sk_fullsock(sk
))
115 read_lock_bh(&sk
->sk_callback_lock
);
116 if (sk
->sk_socket
== NULL
||
117 sk
->sk_socket
->file
== NULL
) {
118 read_unlock_bh(&sk
->sk_callback_lock
);
121 *dest
= from_kgid_munged(&init_user_ns
,
122 sk
->sk_socket
->file
->f_cred
->fsgid
);
123 read_unlock_bh(&sk
->sk_callback_lock
);
125 #ifdef CONFIG_IP_ROUTE_CLASSID
126 case NFT_META_RTCLASSID
: {
127 const struct dst_entry
*dst
= skb_dst(skb
);
131 *dest
= dst
->tclassid
;
135 #ifdef CONFIG_NETWORK_SECMARK
136 case NFT_META_SECMARK
:
137 *dest
= skb
->secmark
;
140 case NFT_META_PKTTYPE
:
141 if (skb
->pkt_type
!= PACKET_LOOPBACK
) {
142 nft_reg_store8(dest
, skb
->pkt_type
);
146 switch (nft_pf(pkt
)) {
148 if (ipv4_is_multicast(ip_hdr(skb
)->daddr
))
149 nft_reg_store8(dest
, PACKET_MULTICAST
);
151 nft_reg_store8(dest
, PACKET_BROADCAST
);
154 nft_reg_store8(dest
, PACKET_MULTICAST
);
157 switch (skb
->protocol
) {
158 case htons(ETH_P_IP
): {
159 int noff
= skb_network_offset(skb
);
160 struct iphdr
*iph
, _iph
;
162 iph
= skb_header_pointer(skb
, noff
,
163 sizeof(_iph
), &_iph
);
167 if (ipv4_is_multicast(iph
->daddr
))
168 nft_reg_store8(dest
, PACKET_MULTICAST
);
170 nft_reg_store8(dest
, PACKET_BROADCAST
);
174 case htons(ETH_P_IPV6
):
175 nft_reg_store8(dest
, PACKET_MULTICAST
);
188 *dest
= raw_smp_processor_id();
190 case NFT_META_IIFGROUP
:
195 case NFT_META_OIFGROUP
:
200 #ifdef CONFIG_CGROUP_NET_CLASSID
201 case NFT_META_CGROUP
:
202 sk
= skb_to_full_sk(skb
);
203 if (!sk
|| !sk_fullsock(sk
))
205 *dest
= sock_cgroup_classid(&sk
->sk_cgrp_data
);
208 case NFT_META_PRANDOM
: {
209 struct rnd_state
*state
= this_cpu_ptr(&nft_prandom_state
);
210 *dest
= prandom_u32_state(state
);
214 case NFT_META_SECPATH
:
215 nft_reg_store8(dest
, !!skb
->sp
);
225 regs
->verdict
.code
= NFT_BREAK
;
227 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
229 void nft_meta_set_eval(const struct nft_expr
*expr
,
230 struct nft_regs
*regs
,
231 const struct nft_pktinfo
*pkt
)
233 const struct nft_meta
*meta
= nft_expr_priv(expr
);
234 struct sk_buff
*skb
= pkt
->skb
;
235 u32
*sreg
= ®s
->data
[meta
->sreg
];
243 case NFT_META_PRIORITY
:
244 skb
->priority
= value
;
246 case NFT_META_PKTTYPE
:
247 pkt_type
= nft_reg_load8(sreg
);
249 if (skb
->pkt_type
!= pkt_type
&&
250 skb_pkt_type_ok(pkt_type
) &&
251 skb_pkt_type_ok(skb
->pkt_type
))
252 skb
->pkt_type
= pkt_type
;
254 case NFT_META_NFTRACE
:
255 skb
->nf_trace
= !!value
;
261 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
263 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
264 [NFTA_META_DREG
] = { .type
= NLA_U32
},
265 [NFTA_META_KEY
] = { .type
= NLA_U32
},
266 [NFTA_META_SREG
] = { .type
= NLA_U32
},
268 EXPORT_SYMBOL_GPL(nft_meta_policy
);
270 int nft_meta_get_init(const struct nft_ctx
*ctx
,
271 const struct nft_expr
*expr
,
272 const struct nlattr
* const tb
[])
274 struct nft_meta
*priv
= nft_expr_priv(expr
);
277 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
279 case NFT_META_PROTOCOL
:
280 case NFT_META_IIFTYPE
:
281 case NFT_META_OIFTYPE
:
284 case NFT_META_NFPROTO
:
285 case NFT_META_L4PROTO
:
287 case NFT_META_PRIORITY
:
293 #ifdef CONFIG_IP_ROUTE_CLASSID
294 case NFT_META_RTCLASSID
:
296 #ifdef CONFIG_NETWORK_SECMARK
297 case NFT_META_SECMARK
:
299 case NFT_META_PKTTYPE
:
301 case NFT_META_IIFGROUP
:
302 case NFT_META_OIFGROUP
:
303 #ifdef CONFIG_CGROUP_NET_CLASSID
304 case NFT_META_CGROUP
:
308 case NFT_META_IIFNAME
:
309 case NFT_META_OIFNAME
:
312 case NFT_META_PRANDOM
:
313 prandom_init_once(&nft_prandom_state
);
317 case NFT_META_SECPATH
:
325 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
326 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
327 NFT_DATA_VALUE
, len
);
329 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
331 static int nft_meta_get_validate(const struct nft_ctx
*ctx
,
332 const struct nft_expr
*expr
,
333 const struct nft_data
**data
)
336 const struct nft_meta
*priv
= nft_expr_priv(expr
);
339 if (priv
->key
!= NFT_META_SECPATH
)
342 switch (ctx
->family
) {
344 hooks
= 1 << NF_NETDEV_INGRESS
;
349 hooks
= (1 << NF_INET_PRE_ROUTING
) |
350 (1 << NF_INET_LOCAL_IN
) |
351 (1 << NF_INET_FORWARD
);
357 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
363 int nft_meta_set_validate(const struct nft_ctx
*ctx
,
364 const struct nft_expr
*expr
,
365 const struct nft_data
**data
)
367 struct nft_meta
*priv
= nft_expr_priv(expr
);
370 if (priv
->key
!= NFT_META_PKTTYPE
)
373 switch (ctx
->family
) {
375 hooks
= 1 << NF_BR_PRE_ROUTING
;
378 hooks
= 1 << NF_NETDEV_INGRESS
;
383 hooks
= 1 << NF_INET_PRE_ROUTING
;
389 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
391 EXPORT_SYMBOL_GPL(nft_meta_set_validate
);
393 int nft_meta_set_init(const struct nft_ctx
*ctx
,
394 const struct nft_expr
*expr
,
395 const struct nlattr
* const tb
[])
397 struct nft_meta
*priv
= nft_expr_priv(expr
);
401 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
404 case NFT_META_PRIORITY
:
407 case NFT_META_NFTRACE
:
410 case NFT_META_PKTTYPE
:
417 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
418 err
= nft_validate_register_load(priv
->sreg
, len
);
422 if (priv
->key
== NFT_META_NFTRACE
)
423 static_branch_inc(&nft_trace_enabled
);
427 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
429 int nft_meta_get_dump(struct sk_buff
*skb
,
430 const struct nft_expr
*expr
)
432 const struct nft_meta
*priv
= nft_expr_priv(expr
);
434 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
435 goto nla_put_failure
;
436 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
437 goto nla_put_failure
;
443 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
445 int nft_meta_set_dump(struct sk_buff
*skb
,
446 const struct nft_expr
*expr
)
448 const struct nft_meta
*priv
= nft_expr_priv(expr
);
450 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
451 goto nla_put_failure
;
452 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
453 goto nla_put_failure
;
460 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
462 void nft_meta_set_destroy(const struct nft_ctx
*ctx
,
463 const struct nft_expr
*expr
)
465 const struct nft_meta
*priv
= nft_expr_priv(expr
);
467 if (priv
->key
== NFT_META_NFTRACE
)
468 static_branch_dec(&nft_trace_enabled
);
470 EXPORT_SYMBOL_GPL(nft_meta_set_destroy
);
472 static struct nft_expr_type nft_meta_type
;
473 static const struct nft_expr_ops nft_meta_get_ops
= {
474 .type
= &nft_meta_type
,
475 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
476 .eval
= nft_meta_get_eval
,
477 .init
= nft_meta_get_init
,
478 .dump
= nft_meta_get_dump
,
479 .validate
= nft_meta_get_validate
,
482 static const struct nft_expr_ops nft_meta_set_ops
= {
483 .type
= &nft_meta_type
,
484 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
485 .eval
= nft_meta_set_eval
,
486 .init
= nft_meta_set_init
,
487 .destroy
= nft_meta_set_destroy
,
488 .dump
= nft_meta_set_dump
,
489 .validate
= nft_meta_set_validate
,
492 static const struct nft_expr_ops
*
493 nft_meta_select_ops(const struct nft_ctx
*ctx
,
494 const struct nlattr
* const tb
[])
496 if (tb
[NFTA_META_KEY
] == NULL
)
497 return ERR_PTR(-EINVAL
);
499 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
500 return ERR_PTR(-EINVAL
);
502 if (tb
[NFTA_META_DREG
])
503 return &nft_meta_get_ops
;
505 if (tb
[NFTA_META_SREG
])
506 return &nft_meta_set_ops
;
508 return ERR_PTR(-EINVAL
);
511 static struct nft_expr_type nft_meta_type __read_mostly
= {
513 .select_ops
= nft_meta_select_ops
,
514 .policy
= nft_meta_policy
,
515 .maxattr
= NFTA_META_MAX
,
516 .owner
= THIS_MODULE
,
519 static int __init
nft_meta_module_init(void)
521 return nft_register_expr(&nft_meta_type
);
524 static void __exit
nft_meta_module_exit(void)
526 nft_unregister_expr(&nft_meta_type
);
529 module_init(nft_meta_module_init
);
530 module_exit(nft_meta_module_exit
);
532 MODULE_LICENSE("GPL");
533 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
534 MODULE_ALIAS_NFT_EXPR("meta");