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
= pkt
->in
, *out
= pkt
->out
;
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
:
57 case NFT_META_PRIORITY
:
58 *dest
= skb
->priority
;
73 case NFT_META_IIFNAME
:
76 strncpy((char *)dest
, in
->name
, IFNAMSIZ
);
78 case NFT_META_OIFNAME
:
81 strncpy((char *)dest
, out
->name
, IFNAMSIZ
);
83 case NFT_META_IIFTYPE
:
87 *(u16
*)dest
= in
->type
;
89 case NFT_META_OIFTYPE
:
93 *(u16
*)dest
= out
->type
;
96 sk
= skb_to_full_sk(skb
);
97 if (!sk
|| !sk_fullsock(sk
))
100 read_lock_bh(&sk
->sk_callback_lock
);
101 if (sk
->sk_socket
== NULL
||
102 sk
->sk_socket
->file
== NULL
) {
103 read_unlock_bh(&sk
->sk_callback_lock
);
107 *dest
= from_kuid_munged(&init_user_ns
,
108 sk
->sk_socket
->file
->f_cred
->fsuid
);
109 read_unlock_bh(&sk
->sk_callback_lock
);
112 sk
= skb_to_full_sk(skb
);
113 if (!sk
|| !sk_fullsock(sk
))
116 read_lock_bh(&sk
->sk_callback_lock
);
117 if (sk
->sk_socket
== NULL
||
118 sk
->sk_socket
->file
== NULL
) {
119 read_unlock_bh(&sk
->sk_callback_lock
);
122 *dest
= from_kgid_munged(&init_user_ns
,
123 sk
->sk_socket
->file
->f_cred
->fsgid
);
124 read_unlock_bh(&sk
->sk_callback_lock
);
126 #ifdef CONFIG_IP_ROUTE_CLASSID
127 case NFT_META_RTCLASSID
: {
128 const struct dst_entry
*dst
= skb_dst(skb
);
132 *dest
= dst
->tclassid
;
136 #ifdef CONFIG_NETWORK_SECMARK
137 case NFT_META_SECMARK
:
138 *dest
= skb
->secmark
;
141 case NFT_META_PKTTYPE
:
142 if (skb
->pkt_type
!= PACKET_LOOPBACK
) {
143 *dest
= skb
->pkt_type
;
149 if (ipv4_is_multicast(ip_hdr(skb
)->daddr
))
150 *dest
= PACKET_MULTICAST
;
152 *dest
= PACKET_BROADCAST
;
155 if (ipv6_hdr(skb
)->daddr
.s6_addr
[0] == 0xFF)
156 *dest
= PACKET_MULTICAST
;
158 *dest
= PACKET_BROADCAST
;
166 *dest
= raw_smp_processor_id();
168 case NFT_META_IIFGROUP
:
173 case NFT_META_OIFGROUP
:
178 #ifdef CONFIG_CGROUP_NET_CLASSID
179 case NFT_META_CGROUP
:
180 sk
= skb_to_full_sk(skb
);
181 if (!sk
|| !sk_fullsock(sk
))
183 *dest
= sock_cgroup_classid(&sk
->sk_cgrp_data
);
186 case NFT_META_PRANDOM
: {
187 struct rnd_state
*state
= this_cpu_ptr(&nft_prandom_state
);
188 *dest
= prandom_u32_state(state
);
198 regs
->verdict
.code
= NFT_BREAK
;
200 EXPORT_SYMBOL_GPL(nft_meta_get_eval
);
202 void nft_meta_set_eval(const struct nft_expr
*expr
,
203 struct nft_regs
*regs
,
204 const struct nft_pktinfo
*pkt
)
206 const struct nft_meta
*meta
= nft_expr_priv(expr
);
207 struct sk_buff
*skb
= pkt
->skb
;
208 u32 value
= regs
->data
[meta
->sreg
];
214 case NFT_META_PRIORITY
:
215 skb
->priority
= value
;
217 case NFT_META_PKTTYPE
:
218 if (skb
->pkt_type
!= value
&&
219 skb_pkt_type_ok(value
) && skb_pkt_type_ok(skb
->pkt_type
))
220 skb
->pkt_type
= value
;
222 case NFT_META_NFTRACE
:
223 skb
->nf_trace
= !!value
;
229 EXPORT_SYMBOL_GPL(nft_meta_set_eval
);
231 const struct nla_policy nft_meta_policy
[NFTA_META_MAX
+ 1] = {
232 [NFTA_META_DREG
] = { .type
= NLA_U32
},
233 [NFTA_META_KEY
] = { .type
= NLA_U32
},
234 [NFTA_META_SREG
] = { .type
= NLA_U32
},
236 EXPORT_SYMBOL_GPL(nft_meta_policy
);
238 int nft_meta_get_init(const struct nft_ctx
*ctx
,
239 const struct nft_expr
*expr
,
240 const struct nlattr
* const tb
[])
242 struct nft_meta
*priv
= nft_expr_priv(expr
);
245 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
247 case NFT_META_PROTOCOL
:
248 case NFT_META_IIFTYPE
:
249 case NFT_META_OIFTYPE
:
252 case NFT_META_NFPROTO
:
253 case NFT_META_L4PROTO
:
255 case NFT_META_PRIORITY
:
261 #ifdef CONFIG_IP_ROUTE_CLASSID
262 case NFT_META_RTCLASSID
:
264 #ifdef CONFIG_NETWORK_SECMARK
265 case NFT_META_SECMARK
:
267 case NFT_META_PKTTYPE
:
269 case NFT_META_IIFGROUP
:
270 case NFT_META_OIFGROUP
:
271 #ifdef CONFIG_CGROUP_NET_CLASSID
272 case NFT_META_CGROUP
:
276 case NFT_META_IIFNAME
:
277 case NFT_META_OIFNAME
:
280 case NFT_META_PRANDOM
:
281 prandom_init_once(&nft_prandom_state
);
288 priv
->dreg
= nft_parse_register(tb
[NFTA_META_DREG
]);
289 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
290 NFT_DATA_VALUE
, len
);
292 EXPORT_SYMBOL_GPL(nft_meta_get_init
);
294 static int nft_meta_set_init_pkttype(const struct nft_ctx
*ctx
)
298 switch (ctx
->afi
->family
) {
300 hooks
= 1 << NF_BR_PRE_ROUTING
;
303 hooks
= 1 << NF_NETDEV_INGRESS
;
309 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
312 int nft_meta_set_init(const struct nft_ctx
*ctx
,
313 const struct nft_expr
*expr
,
314 const struct nlattr
* const tb
[])
316 struct nft_meta
*priv
= nft_expr_priv(expr
);
320 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_META_KEY
]));
323 case NFT_META_PRIORITY
:
326 case NFT_META_NFTRACE
:
329 case NFT_META_PKTTYPE
:
330 err
= nft_meta_set_init_pkttype(ctx
);
339 priv
->sreg
= nft_parse_register(tb
[NFTA_META_SREG
]);
340 err
= nft_validate_register_load(priv
->sreg
, len
);
344 if (priv
->key
== NFT_META_NFTRACE
)
345 static_branch_inc(&nft_trace_enabled
);
349 EXPORT_SYMBOL_GPL(nft_meta_set_init
);
351 int nft_meta_get_dump(struct sk_buff
*skb
,
352 const struct nft_expr
*expr
)
354 const struct nft_meta
*priv
= nft_expr_priv(expr
);
356 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
357 goto nla_put_failure
;
358 if (nft_dump_register(skb
, NFTA_META_DREG
, priv
->dreg
))
359 goto nla_put_failure
;
365 EXPORT_SYMBOL_GPL(nft_meta_get_dump
);
367 int nft_meta_set_dump(struct sk_buff
*skb
,
368 const struct nft_expr
*expr
)
370 const struct nft_meta
*priv
= nft_expr_priv(expr
);
372 if (nla_put_be32(skb
, NFTA_META_KEY
, htonl(priv
->key
)))
373 goto nla_put_failure
;
374 if (nft_dump_register(skb
, NFTA_META_SREG
, priv
->sreg
))
375 goto nla_put_failure
;
382 EXPORT_SYMBOL_GPL(nft_meta_set_dump
);
384 void nft_meta_set_destroy(const struct nft_ctx
*ctx
,
385 const struct nft_expr
*expr
)
387 const struct nft_meta
*priv
= nft_expr_priv(expr
);
389 if (priv
->key
== NFT_META_NFTRACE
)
390 static_branch_dec(&nft_trace_enabled
);
392 EXPORT_SYMBOL_GPL(nft_meta_set_destroy
);
394 static struct nft_expr_type nft_meta_type
;
395 static const struct nft_expr_ops nft_meta_get_ops
= {
396 .type
= &nft_meta_type
,
397 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
398 .eval
= nft_meta_get_eval
,
399 .init
= nft_meta_get_init
,
400 .dump
= nft_meta_get_dump
,
403 static const struct nft_expr_ops nft_meta_set_ops
= {
404 .type
= &nft_meta_type
,
405 .size
= NFT_EXPR_SIZE(sizeof(struct nft_meta
)),
406 .eval
= nft_meta_set_eval
,
407 .init
= nft_meta_set_init
,
408 .destroy
= nft_meta_set_destroy
,
409 .dump
= nft_meta_set_dump
,
412 static const struct nft_expr_ops
*
413 nft_meta_select_ops(const struct nft_ctx
*ctx
,
414 const struct nlattr
* const tb
[])
416 if (tb
[NFTA_META_KEY
] == NULL
)
417 return ERR_PTR(-EINVAL
);
419 if (tb
[NFTA_META_DREG
] && tb
[NFTA_META_SREG
])
420 return ERR_PTR(-EINVAL
);
422 if (tb
[NFTA_META_DREG
])
423 return &nft_meta_get_ops
;
425 if (tb
[NFTA_META_SREG
])
426 return &nft_meta_set_ops
;
428 return ERR_PTR(-EINVAL
);
431 static struct nft_expr_type nft_meta_type __read_mostly
= {
433 .select_ops
= &nft_meta_select_ops
,
434 .policy
= nft_meta_policy
,
435 .maxattr
= NFTA_META_MAX
,
436 .owner
= THIS_MODULE
,
439 static int __init
nft_meta_module_init(void)
441 return nft_register_expr(&nft_meta_type
);
444 static void __exit
nft_meta_module_exit(void)
446 nft_unregister_expr(&nft_meta_type
);
449 module_init(nft_meta_module_init
);
450 module_exit(nft_meta_module_exit
);
452 MODULE_LICENSE("GPL");
453 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
454 MODULE_ALIAS_NFT_EXPR("meta");