media: dvb_frontend: be sure to init dvb_frontend_handle_ioctl() return code
[linux/fpc-iii.git] / net / netfilter / nft_meta.c
blob5a60eb23a7ed8cc82cc2500c7f8c116ee6df2f30
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 nft_reg_store16(dest, (__force u16)skb->protocol);
49 break;
50 case NFT_META_NFPROTO:
51 nft_reg_store8(dest, nft_pf(pkt));
52 break;
53 case NFT_META_L4PROTO:
54 if (!pkt->tprot_set)
55 goto err;
56 nft_reg_store8(dest, pkt->tprot);
57 break;
58 case NFT_META_PRIORITY:
59 *dest = skb->priority;
60 break;
61 case NFT_META_MARK:
62 *dest = skb->mark;
63 break;
64 case NFT_META_IIF:
65 if (in == NULL)
66 goto err;
67 *dest = in->ifindex;
68 break;
69 case NFT_META_OIF:
70 if (out == NULL)
71 goto err;
72 *dest = out->ifindex;
73 break;
74 case NFT_META_IIFNAME:
75 if (in == NULL)
76 goto err;
77 strncpy((char *)dest, in->name, IFNAMSIZ);
78 break;
79 case NFT_META_OIFNAME:
80 if (out == NULL)
81 goto err;
82 strncpy((char *)dest, out->name, IFNAMSIZ);
83 break;
84 case NFT_META_IIFTYPE:
85 if (in == NULL)
86 goto err;
87 nft_reg_store16(dest, in->type);
88 break;
89 case NFT_META_OIFTYPE:
90 if (out == NULL)
91 goto err;
92 nft_reg_store16(dest, out->type);
93 break;
94 case NFT_META_SKUID:
95 sk = skb_to_full_sk(skb);
96 if (!sk || !sk_fullsock(sk))
97 goto err;
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);
103 goto err;
106 *dest = from_kuid_munged(&init_user_ns,
107 sk->sk_socket->file->f_cred->fsuid);
108 read_unlock_bh(&sk->sk_callback_lock);
109 break;
110 case NFT_META_SKGID:
111 sk = skb_to_full_sk(skb);
112 if (!sk || !sk_fullsock(sk))
113 goto err;
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);
119 goto err;
121 *dest = from_kgid_munged(&init_user_ns,
122 sk->sk_socket->file->f_cred->fsgid);
123 read_unlock_bh(&sk->sk_callback_lock);
124 break;
125 #ifdef CONFIG_IP_ROUTE_CLASSID
126 case NFT_META_RTCLASSID: {
127 const struct dst_entry *dst = skb_dst(skb);
129 if (dst == NULL)
130 goto err;
131 *dest = dst->tclassid;
132 break;
134 #endif
135 #ifdef CONFIG_NETWORK_SECMARK
136 case NFT_META_SECMARK:
137 *dest = skb->secmark;
138 break;
139 #endif
140 case NFT_META_PKTTYPE:
141 if (skb->pkt_type != PACKET_LOOPBACK) {
142 nft_reg_store8(dest, skb->pkt_type);
143 break;
146 switch (nft_pf(pkt)) {
147 case NFPROTO_IPV4:
148 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
149 nft_reg_store8(dest, PACKET_MULTICAST);
150 else
151 nft_reg_store8(dest, PACKET_BROADCAST);
152 break;
153 case NFPROTO_IPV6:
154 nft_reg_store8(dest, PACKET_MULTICAST);
155 break;
156 case NFPROTO_NETDEV:
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);
164 if (!iph)
165 goto err;
167 if (ipv4_is_multicast(iph->daddr))
168 nft_reg_store8(dest, PACKET_MULTICAST);
169 else
170 nft_reg_store8(dest, PACKET_BROADCAST);
172 break;
174 case htons(ETH_P_IPV6):
175 nft_reg_store8(dest, PACKET_MULTICAST);
176 break;
177 default:
178 WARN_ON_ONCE(1);
179 goto err;
181 break;
182 default:
183 WARN_ON_ONCE(1);
184 goto err;
186 break;
187 case NFT_META_CPU:
188 *dest = raw_smp_processor_id();
189 break;
190 case NFT_META_IIFGROUP:
191 if (in == NULL)
192 goto err;
193 *dest = in->group;
194 break;
195 case NFT_META_OIFGROUP:
196 if (out == NULL)
197 goto err;
198 *dest = out->group;
199 break;
200 #ifdef CONFIG_CGROUP_NET_CLASSID
201 case NFT_META_CGROUP:
202 sk = skb_to_full_sk(skb);
203 if (!sk || !sk_fullsock(sk))
204 goto err;
205 *dest = sock_cgroup_classid(&sk->sk_cgrp_data);
206 break;
207 #endif
208 case NFT_META_PRANDOM: {
209 struct rnd_state *state = this_cpu_ptr(&nft_prandom_state);
210 *dest = prandom_u32_state(state);
211 break;
213 default:
214 WARN_ON(1);
215 goto err;
217 return;
219 err:
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 = &regs->data[meta->sreg];
231 u32 value = *sreg;
232 u8 pkt_type;
234 switch (meta->key) {
235 case NFT_META_MARK:
236 skb->mark = value;
237 break;
238 case NFT_META_PRIORITY:
239 skb->priority = value;
240 break;
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;
248 break;
249 case NFT_META_NFTRACE:
250 skb->nf_trace = !!value;
251 break;
252 default:
253 WARN_ON(1);
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);
270 unsigned int len;
272 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
273 switch (priv->key) {
274 case NFT_META_PROTOCOL:
275 case NFT_META_IIFTYPE:
276 case NFT_META_OIFTYPE:
277 len = sizeof(u16);
278 break;
279 case NFT_META_NFPROTO:
280 case NFT_META_L4PROTO:
281 case NFT_META_LEN:
282 case NFT_META_PRIORITY:
283 case NFT_META_MARK:
284 case NFT_META_IIF:
285 case NFT_META_OIF:
286 case NFT_META_SKUID:
287 case NFT_META_SKGID:
288 #ifdef CONFIG_IP_ROUTE_CLASSID
289 case NFT_META_RTCLASSID:
290 #endif
291 #ifdef CONFIG_NETWORK_SECMARK
292 case NFT_META_SECMARK:
293 #endif
294 case NFT_META_PKTTYPE:
295 case NFT_META_CPU:
296 case NFT_META_IIFGROUP:
297 case NFT_META_OIFGROUP:
298 #ifdef CONFIG_CGROUP_NET_CLASSID
299 case NFT_META_CGROUP:
300 #endif
301 len = sizeof(u32);
302 break;
303 case NFT_META_IIFNAME:
304 case NFT_META_OIFNAME:
305 len = IFNAMSIZ;
306 break;
307 case NFT_META_PRANDOM:
308 prandom_init_once(&nft_prandom_state);
309 len = sizeof(u32);
310 break;
311 default:
312 return -EOPNOTSUPP;
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);
326 unsigned int hooks;
328 if (priv->key != NFT_META_PKTTYPE)
329 return 0;
331 switch (ctx->afi->family) {
332 case NFPROTO_BRIDGE:
333 hooks = 1 << NF_BR_PRE_ROUTING;
334 break;
335 case NFPROTO_NETDEV:
336 hooks = 1 << NF_NETDEV_INGRESS;
337 break;
338 case NFPROTO_IPV4:
339 case NFPROTO_IPV6:
340 case NFPROTO_INET:
341 hooks = 1 << NF_INET_PRE_ROUTING;
342 break;
343 default:
344 return -EOPNOTSUPP;
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);
356 unsigned int len;
357 int err;
359 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
360 switch (priv->key) {
361 case NFT_META_MARK:
362 case NFT_META_PRIORITY:
363 len = sizeof(u32);
364 break;
365 case NFT_META_NFTRACE:
366 len = sizeof(u8);
367 break;
368 case NFT_META_PKTTYPE:
369 len = sizeof(u8);
370 break;
371 default:
372 return -EOPNOTSUPP;
375 priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
376 err = nft_validate_register_load(priv->sreg, len);
377 if (err < 0)
378 return err;
380 if (priv->key == NFT_META_NFTRACE)
381 static_branch_inc(&nft_trace_enabled);
383 return 0;
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;
396 return 0;
398 nla_put_failure:
399 return -1;
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;
413 return 0;
415 nla_put_failure:
416 return -1;
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 = {
469 .name = "meta",
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");