Linux 4.3
[linux/fpc-iii.git] / net / netfilter / nft_meta.c
blobcb2f13ebb5a66cdf5fd9498ddfe8949f38741803
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 u32 *dest = &regs->data[priv->dreg];
36 switch (priv->key) {
37 case NFT_META_LEN:
38 *dest = skb->len;
39 break;
40 case NFT_META_PROTOCOL:
41 *dest = 0;
42 *(__be16 *)dest = skb->protocol;
43 break;
44 case NFT_META_NFPROTO:
45 *dest = pkt->ops->pf;
46 break;
47 case NFT_META_L4PROTO:
48 *dest = pkt->tprot;
49 break;
50 case NFT_META_PRIORITY:
51 *dest = skb->priority;
52 break;
53 case NFT_META_MARK:
54 *dest = skb->mark;
55 break;
56 case NFT_META_IIF:
57 if (in == NULL)
58 goto err;
59 *dest = in->ifindex;
60 break;
61 case NFT_META_OIF:
62 if (out == NULL)
63 goto err;
64 *dest = out->ifindex;
65 break;
66 case NFT_META_IIFNAME:
67 if (in == NULL)
68 goto err;
69 strncpy((char *)dest, in->name, IFNAMSIZ);
70 break;
71 case NFT_META_OIFNAME:
72 if (out == NULL)
73 goto err;
74 strncpy((char *)dest, out->name, IFNAMSIZ);
75 break;
76 case NFT_META_IIFTYPE:
77 if (in == NULL)
78 goto err;
79 *dest = 0;
80 *(u16 *)dest = in->type;
81 break;
82 case NFT_META_OIFTYPE:
83 if (out == NULL)
84 goto err;
85 *dest = 0;
86 *(u16 *)dest = out->type;
87 break;
88 case NFT_META_SKUID:
89 if (skb->sk == NULL || !sk_fullsock(skb->sk))
90 goto err;
92 read_lock_bh(&skb->sk->sk_callback_lock);
93 if (skb->sk->sk_socket == NULL ||
94 skb->sk->sk_socket->file == NULL) {
95 read_unlock_bh(&skb->sk->sk_callback_lock);
96 goto err;
99 *dest = from_kuid_munged(&init_user_ns,
100 skb->sk->sk_socket->file->f_cred->fsuid);
101 read_unlock_bh(&skb->sk->sk_callback_lock);
102 break;
103 case NFT_META_SKGID:
104 if (skb->sk == NULL || !sk_fullsock(skb->sk))
105 goto err;
107 read_lock_bh(&skb->sk->sk_callback_lock);
108 if (skb->sk->sk_socket == NULL ||
109 skb->sk->sk_socket->file == NULL) {
110 read_unlock_bh(&skb->sk->sk_callback_lock);
111 goto err;
113 *dest = from_kgid_munged(&init_user_ns,
114 skb->sk->sk_socket->file->f_cred->fsgid);
115 read_unlock_bh(&skb->sk->sk_callback_lock);
116 break;
117 #ifdef CONFIG_IP_ROUTE_CLASSID
118 case NFT_META_RTCLASSID: {
119 const struct dst_entry *dst = skb_dst(skb);
121 if (dst == NULL)
122 goto err;
123 *dest = dst->tclassid;
124 break;
126 #endif
127 #ifdef CONFIG_NETWORK_SECMARK
128 case NFT_META_SECMARK:
129 *dest = skb->secmark;
130 break;
131 #endif
132 case NFT_META_PKTTYPE:
133 if (skb->pkt_type != PACKET_LOOPBACK) {
134 *dest = skb->pkt_type;
135 break;
138 switch (pkt->ops->pf) {
139 case NFPROTO_IPV4:
140 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
141 *dest = PACKET_MULTICAST;
142 else
143 *dest = PACKET_BROADCAST;
144 break;
145 case NFPROTO_IPV6:
146 if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
147 *dest = PACKET_MULTICAST;
148 else
149 *dest = PACKET_BROADCAST;
150 break;
151 default:
152 WARN_ON(1);
153 goto err;
155 break;
156 case NFT_META_CPU:
157 *dest = raw_smp_processor_id();
158 break;
159 case NFT_META_IIFGROUP:
160 if (in == NULL)
161 goto err;
162 *dest = in->group;
163 break;
164 case NFT_META_OIFGROUP:
165 if (out == NULL)
166 goto err;
167 *dest = out->group;
168 break;
169 #ifdef CONFIG_CGROUP_NET_CLASSID
170 case NFT_META_CGROUP:
171 if (skb->sk == NULL || !sk_fullsock(skb->sk))
172 goto err;
173 *dest = skb->sk->sk_classid;
174 break;
175 #endif
176 default:
177 WARN_ON(1);
178 goto err;
180 return;
182 err:
183 regs->verdict.code = NFT_BREAK;
185 EXPORT_SYMBOL_GPL(nft_meta_get_eval);
187 void nft_meta_set_eval(const struct nft_expr *expr,
188 struct nft_regs *regs,
189 const struct nft_pktinfo *pkt)
191 const struct nft_meta *meta = nft_expr_priv(expr);
192 struct sk_buff *skb = pkt->skb;
193 u32 value = regs->data[meta->sreg];
195 switch (meta->key) {
196 case NFT_META_MARK:
197 skb->mark = value;
198 break;
199 case NFT_META_PRIORITY:
200 skb->priority = value;
201 break;
202 case NFT_META_NFTRACE:
203 skb->nf_trace = 1;
204 break;
205 default:
206 WARN_ON(1);
209 EXPORT_SYMBOL_GPL(nft_meta_set_eval);
211 const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
212 [NFTA_META_DREG] = { .type = NLA_U32 },
213 [NFTA_META_KEY] = { .type = NLA_U32 },
214 [NFTA_META_SREG] = { .type = NLA_U32 },
216 EXPORT_SYMBOL_GPL(nft_meta_policy);
218 int nft_meta_get_init(const struct nft_ctx *ctx,
219 const struct nft_expr *expr,
220 const struct nlattr * const tb[])
222 struct nft_meta *priv = nft_expr_priv(expr);
223 unsigned int len;
225 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
226 switch (priv->key) {
227 case NFT_META_PROTOCOL:
228 case NFT_META_IIFTYPE:
229 case NFT_META_OIFTYPE:
230 len = sizeof(u16);
231 break;
232 case NFT_META_NFPROTO:
233 case NFT_META_L4PROTO:
234 case NFT_META_LEN:
235 case NFT_META_PRIORITY:
236 case NFT_META_MARK:
237 case NFT_META_IIF:
238 case NFT_META_OIF:
239 case NFT_META_SKUID:
240 case NFT_META_SKGID:
241 #ifdef CONFIG_IP_ROUTE_CLASSID
242 case NFT_META_RTCLASSID:
243 #endif
244 #ifdef CONFIG_NETWORK_SECMARK
245 case NFT_META_SECMARK:
246 #endif
247 case NFT_META_PKTTYPE:
248 case NFT_META_CPU:
249 case NFT_META_IIFGROUP:
250 case NFT_META_OIFGROUP:
251 #ifdef CONFIG_CGROUP_NET_CLASSID
252 case NFT_META_CGROUP:
253 #endif
254 len = sizeof(u32);
255 break;
256 case NFT_META_IIFNAME:
257 case NFT_META_OIFNAME:
258 len = IFNAMSIZ;
259 break;
260 default:
261 return -EOPNOTSUPP;
264 priv->dreg = nft_parse_register(tb[NFTA_META_DREG]);
265 return nft_validate_register_store(ctx, priv->dreg, NULL,
266 NFT_DATA_VALUE, len);
268 EXPORT_SYMBOL_GPL(nft_meta_get_init);
270 int nft_meta_set_init(const struct nft_ctx *ctx,
271 const struct nft_expr *expr,
272 const struct nlattr * const tb[])
274 struct nft_meta *priv = nft_expr_priv(expr);
275 unsigned int len;
276 int err;
278 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
279 switch (priv->key) {
280 case NFT_META_MARK:
281 case NFT_META_PRIORITY:
282 len = sizeof(u32);
283 break;
284 case NFT_META_NFTRACE:
285 len = sizeof(u8);
286 break;
287 default:
288 return -EOPNOTSUPP;
291 priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
292 err = nft_validate_register_load(priv->sreg, len);
293 if (err < 0)
294 return err;
296 return 0;
298 EXPORT_SYMBOL_GPL(nft_meta_set_init);
300 int nft_meta_get_dump(struct sk_buff *skb,
301 const struct nft_expr *expr)
303 const struct nft_meta *priv = nft_expr_priv(expr);
305 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
306 goto nla_put_failure;
307 if (nft_dump_register(skb, NFTA_META_DREG, priv->dreg))
308 goto nla_put_failure;
309 return 0;
311 nla_put_failure:
312 return -1;
314 EXPORT_SYMBOL_GPL(nft_meta_get_dump);
316 int nft_meta_set_dump(struct sk_buff *skb,
317 const struct nft_expr *expr)
319 const struct nft_meta *priv = nft_expr_priv(expr);
321 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
322 goto nla_put_failure;
323 if (nft_dump_register(skb, NFTA_META_SREG, priv->sreg))
324 goto nla_put_failure;
326 return 0;
328 nla_put_failure:
329 return -1;
331 EXPORT_SYMBOL_GPL(nft_meta_set_dump);
333 static struct nft_expr_type nft_meta_type;
334 static const struct nft_expr_ops nft_meta_get_ops = {
335 .type = &nft_meta_type,
336 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
337 .eval = nft_meta_get_eval,
338 .init = nft_meta_get_init,
339 .dump = nft_meta_get_dump,
342 static const struct nft_expr_ops nft_meta_set_ops = {
343 .type = &nft_meta_type,
344 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
345 .eval = nft_meta_set_eval,
346 .init = nft_meta_set_init,
347 .dump = nft_meta_set_dump,
350 static const struct nft_expr_ops *
351 nft_meta_select_ops(const struct nft_ctx *ctx,
352 const struct nlattr * const tb[])
354 if (tb[NFTA_META_KEY] == NULL)
355 return ERR_PTR(-EINVAL);
357 if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG])
358 return ERR_PTR(-EINVAL);
360 if (tb[NFTA_META_DREG])
361 return &nft_meta_get_ops;
363 if (tb[NFTA_META_SREG])
364 return &nft_meta_set_ops;
366 return ERR_PTR(-EINVAL);
369 static struct nft_expr_type nft_meta_type __read_mostly = {
370 .name = "meta",
371 .select_ops = &nft_meta_select_ops,
372 .policy = nft_meta_policy,
373 .maxattr = NFTA_META_MAX,
374 .owner = THIS_MODULE,
377 static int __init nft_meta_module_init(void)
379 return nft_register_expr(&nft_meta_type);
382 static void __exit nft_meta_module_exit(void)
384 nft_unregister_expr(&nft_meta_type);
387 module_init(nft_meta_module_init);
388 module_exit(nft_meta_module_exit);
390 MODULE_LICENSE("GPL");
391 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
392 MODULE_ALIAS_NFT_EXPR("meta");