Merge remote-tracking branch 'remotes/powerpc/topic/xive' into kvm-ppc-next
[linux/fpc-iii.git] / net / netfilter / nft_meta.c
blobe1f5ca9b423b5ffda43ec5519d4c8832ce695899
1 /*
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/)
9 */
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>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/ipv6.h>
20 #include <linux/smp.h>
21 #include <linux/static_key.h>
22 #include <net/dst.h>
23 #include <net/sock.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);
40 struct sock *sk;
41 u32 *dest = &regs->data[priv->dreg];
43 switch (priv->key) {
44 case NFT_META_LEN:
45 *dest = skb->len;
46 break;
47 case NFT_META_PROTOCOL:
48 *dest = 0;
49 *(__be16 *)dest = skb->protocol;
50 break;
51 case NFT_META_NFPROTO:
52 *dest = nft_pf(pkt);
53 break;
54 case NFT_META_L4PROTO:
55 if (!pkt->tprot_set)
56 goto err;
57 *dest = pkt->tprot;
58 break;
59 case NFT_META_PRIORITY:
60 *dest = skb->priority;
61 break;
62 case NFT_META_MARK:
63 *dest = skb->mark;
64 break;
65 case NFT_META_IIF:
66 if (in == NULL)
67 goto err;
68 *dest = in->ifindex;
69 break;
70 case NFT_META_OIF:
71 if (out == NULL)
72 goto err;
73 *dest = out->ifindex;
74 break;
75 case NFT_META_IIFNAME:
76 if (in == NULL)
77 goto err;
78 strncpy((char *)dest, in->name, IFNAMSIZ);
79 break;
80 case NFT_META_OIFNAME:
81 if (out == NULL)
82 goto err;
83 strncpy((char *)dest, out->name, IFNAMSIZ);
84 break;
85 case NFT_META_IIFTYPE:
86 if (in == NULL)
87 goto err;
88 *dest = 0;
89 *(u16 *)dest = in->type;
90 break;
91 case NFT_META_OIFTYPE:
92 if (out == NULL)
93 goto err;
94 *dest = 0;
95 *(u16 *)dest = out->type;
96 break;
97 case NFT_META_SKUID:
98 sk = skb_to_full_sk(skb);
99 if (!sk || !sk_fullsock(sk))
100 goto err;
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);
106 goto err;
109 *dest = from_kuid_munged(&init_user_ns,
110 sk->sk_socket->file->f_cred->fsuid);
111 read_unlock_bh(&sk->sk_callback_lock);
112 break;
113 case NFT_META_SKGID:
114 sk = skb_to_full_sk(skb);
115 if (!sk || !sk_fullsock(sk))
116 goto err;
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);
122 goto err;
124 *dest = from_kgid_munged(&init_user_ns,
125 sk->sk_socket->file->f_cred->fsgid);
126 read_unlock_bh(&sk->sk_callback_lock);
127 break;
128 #ifdef CONFIG_IP_ROUTE_CLASSID
129 case NFT_META_RTCLASSID: {
130 const struct dst_entry *dst = skb_dst(skb);
132 if (dst == NULL)
133 goto err;
134 *dest = dst->tclassid;
135 break;
137 #endif
138 #ifdef CONFIG_NETWORK_SECMARK
139 case NFT_META_SECMARK:
140 *dest = skb->secmark;
141 break;
142 #endif
143 case NFT_META_PKTTYPE:
144 if (skb->pkt_type != PACKET_LOOPBACK) {
145 *dest = skb->pkt_type;
146 break;
149 switch (nft_pf(pkt)) {
150 case NFPROTO_IPV4:
151 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
152 *dest = PACKET_MULTICAST;
153 else
154 *dest = PACKET_BROADCAST;
155 break;
156 case NFPROTO_IPV6:
157 *dest = PACKET_MULTICAST;
158 break;
159 case NFPROTO_NETDEV:
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);
167 if (!iph)
168 goto err;
170 if (ipv4_is_multicast(iph->daddr))
171 *dest = PACKET_MULTICAST;
172 else
173 *dest = PACKET_BROADCAST;
175 break;
177 case htons(ETH_P_IPV6):
178 *dest = PACKET_MULTICAST;
179 break;
180 default:
181 WARN_ON_ONCE(1);
182 goto err;
184 break;
185 default:
186 WARN_ON_ONCE(1);
187 goto err;
189 break;
190 case NFT_META_CPU:
191 *dest = raw_smp_processor_id();
192 break;
193 case NFT_META_IIFGROUP:
194 if (in == NULL)
195 goto err;
196 *dest = in->group;
197 break;
198 case NFT_META_OIFGROUP:
199 if (out == NULL)
200 goto err;
201 *dest = out->group;
202 break;
203 #ifdef CONFIG_CGROUP_NET_CLASSID
204 case NFT_META_CGROUP:
205 sk = skb_to_full_sk(skb);
206 if (!sk || !sk_fullsock(sk))
207 goto err;
208 *dest = sock_cgroup_classid(&sk->sk_cgrp_data);
209 break;
210 #endif
211 case NFT_META_PRANDOM: {
212 struct rnd_state *state = this_cpu_ptr(&nft_prandom_state);
213 *dest = prandom_u32_state(state);
214 break;
216 default:
217 WARN_ON(1);
218 goto err;
220 return;
222 err:
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];
235 switch (meta->key) {
236 case NFT_META_MARK:
237 skb->mark = value;
238 break;
239 case NFT_META_PRIORITY:
240 skb->priority = value;
241 break;
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;
246 break;
247 case NFT_META_NFTRACE:
248 skb->nf_trace = !!value;
249 break;
250 default:
251 WARN_ON(1);
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);
268 unsigned int len;
270 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
271 switch (priv->key) {
272 case NFT_META_PROTOCOL:
273 case NFT_META_IIFTYPE:
274 case NFT_META_OIFTYPE:
275 len = sizeof(u16);
276 break;
277 case NFT_META_NFPROTO:
278 case NFT_META_L4PROTO:
279 case NFT_META_LEN:
280 case NFT_META_PRIORITY:
281 case NFT_META_MARK:
282 case NFT_META_IIF:
283 case NFT_META_OIF:
284 case NFT_META_SKUID:
285 case NFT_META_SKGID:
286 #ifdef CONFIG_IP_ROUTE_CLASSID
287 case NFT_META_RTCLASSID:
288 #endif
289 #ifdef CONFIG_NETWORK_SECMARK
290 case NFT_META_SECMARK:
291 #endif
292 case NFT_META_PKTTYPE:
293 case NFT_META_CPU:
294 case NFT_META_IIFGROUP:
295 case NFT_META_OIFGROUP:
296 #ifdef CONFIG_CGROUP_NET_CLASSID
297 case NFT_META_CGROUP:
298 #endif
299 len = sizeof(u32);
300 break;
301 case NFT_META_IIFNAME:
302 case NFT_META_OIFNAME:
303 len = IFNAMSIZ;
304 break;
305 case NFT_META_PRANDOM:
306 prandom_init_once(&nft_prandom_state);
307 len = sizeof(u32);
308 break;
309 default:
310 return -EOPNOTSUPP;
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);
324 unsigned int hooks;
326 if (priv->key != NFT_META_PKTTYPE)
327 return 0;
329 switch (ctx->afi->family) {
330 case NFPROTO_BRIDGE:
331 hooks = 1 << NF_BR_PRE_ROUTING;
332 break;
333 case NFPROTO_NETDEV:
334 hooks = 1 << NF_NETDEV_INGRESS;
335 break;
336 case NFPROTO_IPV4:
337 case NFPROTO_IPV6:
338 case NFPROTO_INET:
339 hooks = 1 << NF_INET_PRE_ROUTING;
340 break;
341 default:
342 return -EOPNOTSUPP;
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);
354 unsigned int len;
355 int err;
357 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
358 switch (priv->key) {
359 case NFT_META_MARK:
360 case NFT_META_PRIORITY:
361 len = sizeof(u32);
362 break;
363 case NFT_META_NFTRACE:
364 len = sizeof(u8);
365 break;
366 case NFT_META_PKTTYPE:
367 len = sizeof(u8);
368 break;
369 default:
370 return -EOPNOTSUPP;
373 err = nft_meta_set_validate(ctx, expr, NULL);
374 if (err < 0)
375 return err;
377 priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
378 err = nft_validate_register_load(priv->sreg, len);
379 if (err < 0)
380 return err;
382 if (priv->key == NFT_META_NFTRACE)
383 static_branch_inc(&nft_trace_enabled);
385 return 0;
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;
398 return 0;
400 nla_put_failure:
401 return -1;
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;
415 return 0;
417 nla_put_failure:
418 return -1;
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 = {
471 .name = "meta",
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");