dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / net / netfilter / nft_meta.c
bloba97a5bf716be1970a1dddee51788aeb3c7739fe7
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 <net/dst.h>
22 #include <net/sock.h>
23 #include <net/tcp_states.h> /* for TCP_TIME_WAIT */
24 #include <net/netfilter/nf_tables.h>
25 #include <net/netfilter/nft_meta.h>
27 void nft_meta_get_eval(const struct nft_expr *expr,
28 struct nft_regs *regs,
29 const struct nft_pktinfo *pkt)
31 const struct nft_meta *priv = nft_expr_priv(expr);
32 const struct sk_buff *skb = pkt->skb;
33 const struct net_device *in = pkt->in, *out = pkt->out;
34 struct sock *sk;
35 u32 *dest = &regs->data[priv->dreg];
37 switch (priv->key) {
38 case NFT_META_LEN:
39 *dest = skb->len;
40 break;
41 case NFT_META_PROTOCOL:
42 *dest = 0;
43 *(__be16 *)dest = skb->protocol;
44 break;
45 case NFT_META_NFPROTO:
46 *dest = pkt->pf;
47 break;
48 case NFT_META_L4PROTO:
49 *dest = pkt->tprot;
50 break;
51 case NFT_META_PRIORITY:
52 *dest = skb->priority;
53 break;
54 case NFT_META_MARK:
55 *dest = skb->mark;
56 break;
57 case NFT_META_IIF:
58 if (in == NULL)
59 goto err;
60 *dest = in->ifindex;
61 break;
62 case NFT_META_OIF:
63 if (out == NULL)
64 goto err;
65 *dest = out->ifindex;
66 break;
67 case NFT_META_IIFNAME:
68 if (in == NULL)
69 goto err;
70 strncpy((char *)dest, in->name, IFNAMSIZ);
71 break;
72 case NFT_META_OIFNAME:
73 if (out == NULL)
74 goto err;
75 strncpy((char *)dest, out->name, IFNAMSIZ);
76 break;
77 case NFT_META_IIFTYPE:
78 if (in == NULL)
79 goto err;
80 *dest = 0;
81 *(u16 *)dest = in->type;
82 break;
83 case NFT_META_OIFTYPE:
84 if (out == NULL)
85 goto err;
86 *dest = 0;
87 *(u16 *)dest = out->type;
88 break;
89 case NFT_META_SKUID:
90 sk = skb_to_full_sk(skb);
91 if (!sk || !sk_fullsock(sk))
92 goto err;
94 read_lock_bh(&sk->sk_callback_lock);
95 if (sk->sk_socket == NULL ||
96 sk->sk_socket->file == NULL) {
97 read_unlock_bh(&sk->sk_callback_lock);
98 goto err;
101 *dest = from_kuid_munged(&init_user_ns,
102 sk->sk_socket->file->f_cred->fsuid);
103 read_unlock_bh(&sk->sk_callback_lock);
104 break;
105 case NFT_META_SKGID:
106 sk = skb_to_full_sk(skb);
107 if (!sk || !sk_fullsock(sk))
108 goto err;
110 read_lock_bh(&sk->sk_callback_lock);
111 if (sk->sk_socket == NULL ||
112 sk->sk_socket->file == NULL) {
113 read_unlock_bh(&sk->sk_callback_lock);
114 goto err;
116 *dest = from_kgid_munged(&init_user_ns,
117 sk->sk_socket->file->f_cred->fsgid);
118 read_unlock_bh(&sk->sk_callback_lock);
119 break;
120 #ifdef CONFIG_IP_ROUTE_CLASSID
121 case NFT_META_RTCLASSID: {
122 const struct dst_entry *dst = skb_dst(skb);
124 if (dst == NULL)
125 goto err;
126 *dest = dst->tclassid;
127 break;
129 #endif
130 #ifdef CONFIG_NETWORK_SECMARK
131 case NFT_META_SECMARK:
132 *dest = skb->secmark;
133 break;
134 #endif
135 case NFT_META_PKTTYPE:
136 if (skb->pkt_type != PACKET_LOOPBACK) {
137 *dest = skb->pkt_type;
138 break;
141 switch (pkt->pf) {
142 case NFPROTO_IPV4:
143 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
144 *dest = PACKET_MULTICAST;
145 else
146 *dest = PACKET_BROADCAST;
147 break;
148 case NFPROTO_IPV6:
149 if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
150 *dest = PACKET_MULTICAST;
151 else
152 *dest = PACKET_BROADCAST;
153 break;
154 case NFPROTO_NETDEV:
155 switch (skb->protocol) {
156 case htons(ETH_P_IP): {
157 int noff = skb_network_offset(skb);
158 struct iphdr *iph, _iph;
160 iph = skb_header_pointer(skb, noff,
161 sizeof(_iph), &_iph);
162 if (!iph)
163 goto err;
165 if (ipv4_is_multicast(iph->daddr))
166 *dest = PACKET_MULTICAST;
167 else
168 *dest = PACKET_BROADCAST;
170 break;
172 case htons(ETH_P_IPV6):
173 *dest = PACKET_MULTICAST;
174 break;
175 default:
176 WARN_ON_ONCE(1);
177 goto err;
179 break;
180 default:
181 WARN_ON_ONCE(1);
182 goto err;
184 break;
185 case NFT_META_CPU:
186 *dest = raw_smp_processor_id();
187 break;
188 case NFT_META_IIFGROUP:
189 if (in == NULL)
190 goto err;
191 *dest = in->group;
192 break;
193 case NFT_META_OIFGROUP:
194 if (out == NULL)
195 goto err;
196 *dest = out->group;
197 break;
198 #ifdef CONFIG_CGROUP_NET_CLASSID
199 case NFT_META_CGROUP:
200 sk = skb_to_full_sk(skb);
201 if (!sk || !sk_fullsock(sk))
202 goto err;
203 *dest = sk->sk_classid;
204 break;
205 #endif
206 default:
207 WARN_ON(1);
208 goto err;
210 return;
212 err:
213 regs->verdict.code = NFT_BREAK;
215 EXPORT_SYMBOL_GPL(nft_meta_get_eval);
217 void nft_meta_set_eval(const struct nft_expr *expr,
218 struct nft_regs *regs,
219 const struct nft_pktinfo *pkt)
221 const struct nft_meta *meta = nft_expr_priv(expr);
222 struct sk_buff *skb = pkt->skb;
223 u32 value = regs->data[meta->sreg];
225 switch (meta->key) {
226 case NFT_META_MARK:
227 skb->mark = value;
228 break;
229 case NFT_META_PRIORITY:
230 skb->priority = value;
231 break;
232 case NFT_META_NFTRACE:
233 skb->nf_trace = 1;
234 break;
235 default:
236 WARN_ON(1);
239 EXPORT_SYMBOL_GPL(nft_meta_set_eval);
241 const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
242 [NFTA_META_DREG] = { .type = NLA_U32 },
243 [NFTA_META_KEY] = { .type = NLA_U32 },
244 [NFTA_META_SREG] = { .type = NLA_U32 },
246 EXPORT_SYMBOL_GPL(nft_meta_policy);
248 int nft_meta_get_init(const struct nft_ctx *ctx,
249 const struct nft_expr *expr,
250 const struct nlattr * const tb[])
252 struct nft_meta *priv = nft_expr_priv(expr);
253 unsigned int len;
255 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
256 switch (priv->key) {
257 case NFT_META_PROTOCOL:
258 case NFT_META_IIFTYPE:
259 case NFT_META_OIFTYPE:
260 len = sizeof(u16);
261 break;
262 case NFT_META_NFPROTO:
263 case NFT_META_L4PROTO:
264 case NFT_META_LEN:
265 case NFT_META_PRIORITY:
266 case NFT_META_MARK:
267 case NFT_META_IIF:
268 case NFT_META_OIF:
269 case NFT_META_SKUID:
270 case NFT_META_SKGID:
271 #ifdef CONFIG_IP_ROUTE_CLASSID
272 case NFT_META_RTCLASSID:
273 #endif
274 #ifdef CONFIG_NETWORK_SECMARK
275 case NFT_META_SECMARK:
276 #endif
277 case NFT_META_PKTTYPE:
278 case NFT_META_CPU:
279 case NFT_META_IIFGROUP:
280 case NFT_META_OIFGROUP:
281 #ifdef CONFIG_CGROUP_NET_CLASSID
282 case NFT_META_CGROUP:
283 #endif
284 len = sizeof(u32);
285 break;
286 case NFT_META_IIFNAME:
287 case NFT_META_OIFNAME:
288 len = IFNAMSIZ;
289 break;
290 default:
291 return -EOPNOTSUPP;
294 priv->dreg = nft_parse_register(tb[NFTA_META_DREG]);
295 return nft_validate_register_store(ctx, priv->dreg, NULL,
296 NFT_DATA_VALUE, len);
298 EXPORT_SYMBOL_GPL(nft_meta_get_init);
300 int nft_meta_set_init(const struct nft_ctx *ctx,
301 const struct nft_expr *expr,
302 const struct nlattr * const tb[])
304 struct nft_meta *priv = nft_expr_priv(expr);
305 unsigned int len;
306 int err;
308 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
309 switch (priv->key) {
310 case NFT_META_MARK:
311 case NFT_META_PRIORITY:
312 len = sizeof(u32);
313 break;
314 case NFT_META_NFTRACE:
315 len = sizeof(u8);
316 break;
317 default:
318 return -EOPNOTSUPP;
321 priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
322 err = nft_validate_register_load(priv->sreg, len);
323 if (err < 0)
324 return err;
326 return 0;
328 EXPORT_SYMBOL_GPL(nft_meta_set_init);
330 int nft_meta_get_dump(struct sk_buff *skb,
331 const struct nft_expr *expr)
333 const struct nft_meta *priv = nft_expr_priv(expr);
335 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
336 goto nla_put_failure;
337 if (nft_dump_register(skb, NFTA_META_DREG, priv->dreg))
338 goto nla_put_failure;
339 return 0;
341 nla_put_failure:
342 return -1;
344 EXPORT_SYMBOL_GPL(nft_meta_get_dump);
346 int nft_meta_set_dump(struct sk_buff *skb,
347 const struct nft_expr *expr)
349 const struct nft_meta *priv = nft_expr_priv(expr);
351 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
352 goto nla_put_failure;
353 if (nft_dump_register(skb, NFTA_META_SREG, priv->sreg))
354 goto nla_put_failure;
356 return 0;
358 nla_put_failure:
359 return -1;
361 EXPORT_SYMBOL_GPL(nft_meta_set_dump);
363 static struct nft_expr_type nft_meta_type;
364 static const struct nft_expr_ops nft_meta_get_ops = {
365 .type = &nft_meta_type,
366 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
367 .eval = nft_meta_get_eval,
368 .init = nft_meta_get_init,
369 .dump = nft_meta_get_dump,
372 static const struct nft_expr_ops nft_meta_set_ops = {
373 .type = &nft_meta_type,
374 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
375 .eval = nft_meta_set_eval,
376 .init = nft_meta_set_init,
377 .dump = nft_meta_set_dump,
380 static const struct nft_expr_ops *
381 nft_meta_select_ops(const struct nft_ctx *ctx,
382 const struct nlattr * const tb[])
384 if (tb[NFTA_META_KEY] == NULL)
385 return ERR_PTR(-EINVAL);
387 if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
388 return ERR_PTR(-EINVAL);
390 if (tb[NFTA_META_DREG])
391 return &nft_meta_get_ops;
393 if (tb[NFTA_META_SREG])
394 return &nft_meta_set_ops;
396 return ERR_PTR(-EINVAL);
399 static struct nft_expr_type nft_meta_type __read_mostly = {
400 .name = "meta",
401 .select_ops = &nft_meta_select_ops,
402 .policy = nft_meta_policy,
403 .maxattr = NFTA_META_MAX,
404 .owner = THIS_MODULE,
407 static int __init nft_meta_module_init(void)
409 return nft_register_expr(&nft_meta_type);
412 static void __exit nft_meta_module_exit(void)
414 nft_unregister_expr(&nft_meta_type);
417 module_init(nft_meta_module_init);
418 module_exit(nft_meta_module_exit);
420 MODULE_LICENSE("GPL");
421 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
422 MODULE_ALIAS_NFT_EXPR("meta");