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
);
220 regs
->verdict
.code
= NFT_BREAK
;
222 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
224 void nft_meta_set_eval(const struct nft_expr
*expr
,
225 struct nft_regs
*regs
,
226 const struct nft_pktinfo
*pkt
)
228 const struct nft_meta
*meta
= nft_expr_priv(expr
);
229 struct sk_buff
*skb
= pkt
->skb
;
230 u32
*sreg
= ®s
->data
[meta
->sreg
];
238 case NFT_META_PRIORITY
:
239 skb
->priority
= value
;
241 case NFT_META_PKTTYPE
:
242 pkt_type
= nft_reg_load8(sreg
);
244 if (skb
->pkt_type
!= pkt_type
&&
245 skb_pkt_type_ok(pkt_type
) &&
246 skb_pkt_type_ok(skb
->pkt_type
))
247 skb
->pkt_type
= pkt_type
;
249 case NFT_META_NFTRACE
:
250 skb
->nf_trace
= !!value
;
256 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
258 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
259 [NFTA_META_DREG
] = { .type
= NLA_U32
},
260 [NFTA_META_KEY
] = { .type
= NLA_U32
},
261 [NFTA_META_SREG
] = { .type
= NLA_U32
},
263 EXPORT_SYMBOL_GPL(nft_meta_policy
);
265 int nft_meta_get_init(const struct nft_ctx
*ctx
,
266 const struct nft_expr
*expr
,
267 const struct nlattr
* const tb
[])
269 struct nft_meta
*priv
= nft_expr_priv(expr
);
272 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
274 case NFT_META_PROTOCOL
:
275 case NFT_META_IIFTYPE
:
276 case NFT_META_OIFTYPE
:
279 case NFT_META_NFPROTO
:
280 case NFT_META_L4PROTO
:
282 case NFT_META_PRIORITY
:
288 #ifdef CONFIG_IP_ROUTE_CLASSID
289 case NFT_META_RTCLASSID
:
291 #ifdef CONFIG_NETWORK_SECMARK
292 case NFT_META_SECMARK
:
294 case NFT_META_PKTTYPE
:
296 case NFT_META_IIFGROUP
:
297 case NFT_META_OIFGROUP
:
298 #ifdef CONFIG_CGROUP_NET_CLASSID
299 case NFT_META_CGROUP
:
303 case NFT_META_IIFNAME
:
304 case NFT_META_OIFNAME
:
307 case NFT_META_PRANDOM
:
308 prandom_init_once(&nft_prandom_state
);
315 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
316 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
317 NFT_DATA_VALUE
, len
);
319 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
321 int nft_meta_set_validate(const struct nft_ctx
*ctx
,
322 const struct nft_expr
*expr
,
323 const struct nft_data
**data
)
325 struct nft_meta
*priv
= nft_expr_priv(expr
);
328 if (priv
->key
!= NFT_META_PKTTYPE
)
331 switch (ctx
->afi
->family
) {
333 hooks
= 1 << NF_BR_PRE_ROUTING
;
336 hooks
= 1 << NF_NETDEV_INGRESS
;
341 hooks
= 1 << NF_INET_PRE_ROUTING
;
347 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
349 EXPORT_SYMBOL_GPL(nft_meta_set_validate
);
351 int nft_meta_set_init(const struct nft_ctx
*ctx
,
352 const struct nft_expr
*expr
,
353 const struct nlattr
* const tb
[])
355 struct nft_meta
*priv
= nft_expr_priv(expr
);
359 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
362 case NFT_META_PRIORITY
:
365 case NFT_META_NFTRACE
:
368 case NFT_META_PKTTYPE
:
375 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
376 err
= nft_validate_register_load(priv
->sreg
, len
);
380 if (priv
->key
== NFT_META_NFTRACE
)
381 static_branch_inc(&nft_trace_enabled
);
385 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
387 int nft_meta_get_dump(struct sk_buff
*skb
,
388 const struct nft_expr
*expr
)
390 const struct nft_meta
*priv
= nft_expr_priv(expr
);
392 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
393 goto nla_put_failure
;
394 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
395 goto nla_put_failure
;
401 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
403 int nft_meta_set_dump(struct sk_buff
*skb
,
404 const struct nft_expr
*expr
)
406 const struct nft_meta
*priv
= nft_expr_priv(expr
);
408 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
409 goto nla_put_failure
;
410 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
411 goto nla_put_failure
;
418 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
420 void nft_meta_set_destroy(const struct nft_ctx
*ctx
,
421 const struct nft_expr
*expr
)
423 const struct nft_meta
*priv
= nft_expr_priv(expr
);
425 if (priv
->key
== NFT_META_NFTRACE
)
426 static_branch_dec(&nft_trace_enabled
);
428 EXPORT_SYMBOL_GPL(nft_meta_set_destroy
);
430 static struct nft_expr_type nft_meta_type
;
431 static const struct nft_expr_ops nft_meta_get_ops
= {
432 .type
= &nft_meta_type
,
433 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
434 .eval
= nft_meta_get_eval
,
435 .init
= nft_meta_get_init
,
436 .dump
= nft_meta_get_dump
,
439 static const struct nft_expr_ops nft_meta_set_ops
= {
440 .type
= &nft_meta_type
,
441 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
442 .eval
= nft_meta_set_eval
,
443 .init
= nft_meta_set_init
,
444 .destroy
= nft_meta_set_destroy
,
445 .dump
= nft_meta_set_dump
,
446 .validate
= nft_meta_set_validate
,
449 static const struct nft_expr_ops
*
450 nft_meta_select_ops(const struct nft_ctx
*ctx
,
451 const struct nlattr
* const tb
[])
453 if (tb
[NFTA_META_KEY
] == NULL
)
454 return ERR_PTR(-EINVAL
);
456 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
457 return ERR_PTR(-EINVAL
);
459 if (tb
[NFTA_META_DREG
])
460 return &nft_meta_get_ops
;
462 if (tb
[NFTA_META_SREG
])
463 return &nft_meta_set_ops
;
465 return ERR_PTR(-EINVAL
);
468 static struct nft_expr_type nft_meta_type __read_mostly
= {
470 .select_ops
= nft_meta_select_ops
,
471 .policy
= nft_meta_policy
,
472 .maxattr
= NFTA_META_MAX
,
473 .owner
= THIS_MODULE
,
476 static int __init
nft_meta_module_init(void)
478 return nft_register_expr(&nft_meta_type
);
481 static void __exit
nft_meta_module_exit(void)
483 nft_unregister_expr(&nft_meta_type
);
486 module_init(nft_meta_module_init
);
487 module_exit(nft_meta_module_exit
);
489 MODULE_LICENSE("GPL");
490 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
491 MODULE_ALIAS_NFT_EXPR("meta");