Commit | Line | Data |
---|---|---|
96518518 | 1 | /* |
ef1f7df9 | 2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
96518518 PM |
3 | * |
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. | |
7 | * | |
8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
9 | */ | |
10 | ||
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> | |
e2a093ff AR |
17 | #include <linux/in.h> |
18 | #include <linux/ip.h> | |
19 | #include <linux/ipv6.h> | |
afc5be30 | 20 | #include <linux/smp.h> |
96518518 PM |
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> | |
aa45660c | 25 | #include <net/netfilter/nft_meta.h> |
96518518 | 26 | |
aa45660c | 27 | void nft_meta_get_eval(const struct nft_expr *expr, |
a55e22e9 | 28 | struct nft_regs *regs, |
aa45660c | 29 | const struct nft_pktinfo *pkt) |
96518518 PM |
30 | { |
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; | |
49499c3e | 34 | u32 *dest = ®s->data[priv->dreg]; |
96518518 PM |
35 | |
36 | switch (priv->key) { | |
37 | case NFT_META_LEN: | |
fad136ea | 38 | *dest = skb->len; |
96518518 PM |
39 | break; |
40 | case NFT_META_PROTOCOL: | |
49499c3e | 41 | *dest = 0; |
fad136ea | 42 | *(__be16 *)dest = skb->protocol; |
96518518 | 43 | break; |
124edfa9 | 44 | case NFT_META_NFPROTO: |
6aa187f2 | 45 | *dest = pkt->pf; |
124edfa9 | 46 | break; |
4566bf27 | 47 | case NFT_META_L4PROTO: |
fad136ea | 48 | *dest = pkt->tprot; |
4566bf27 | 49 | break; |
96518518 | 50 | case NFT_META_PRIORITY: |
fad136ea | 51 | *dest = skb->priority; |
96518518 PM |
52 | break; |
53 | case NFT_META_MARK: | |
fad136ea | 54 | *dest = skb->mark; |
96518518 PM |
55 | break; |
56 | case NFT_META_IIF: | |
57 | if (in == NULL) | |
58 | goto err; | |
fad136ea | 59 | *dest = in->ifindex; |
96518518 PM |
60 | break; |
61 | case NFT_META_OIF: | |
62 | if (out == NULL) | |
63 | goto err; | |
fad136ea | 64 | *dest = out->ifindex; |
96518518 PM |
65 | break; |
66 | case NFT_META_IIFNAME: | |
67 | if (in == NULL) | |
68 | goto err; | |
fad136ea | 69 | strncpy((char *)dest, in->name, IFNAMSIZ); |
96518518 PM |
70 | break; |
71 | case NFT_META_OIFNAME: | |
72 | if (out == NULL) | |
73 | goto err; | |
fad136ea | 74 | strncpy((char *)dest, out->name, IFNAMSIZ); |
96518518 PM |
75 | break; |
76 | case NFT_META_IIFTYPE: | |
77 | if (in == NULL) | |
78 | goto err; | |
49499c3e | 79 | *dest = 0; |
fad136ea | 80 | *(u16 *)dest = in->type; |
96518518 PM |
81 | break; |
82 | case NFT_META_OIFTYPE: | |
83 | if (out == NULL) | |
84 | goto err; | |
49499c3e | 85 | *dest = 0; |
fad136ea | 86 | *(u16 *)dest = out->type; |
96518518 PM |
87 | break; |
88 | case NFT_META_SKUID: | |
a8399231 | 89 | if (skb->sk == NULL || !sk_fullsock(skb->sk)) |
96518518 PM |
90 | goto err; |
91 | ||
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; | |
97 | } | |
98 | ||
fad136ea | 99 | *dest = from_kuid_munged(&init_user_ns, |
96518518 PM |
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: | |
a8399231 | 104 | if (skb->sk == NULL || !sk_fullsock(skb->sk)) |
96518518 PM |
105 | goto err; |
106 | ||
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; | |
112 | } | |
fad136ea | 113 | *dest = from_kgid_munged(&init_user_ns, |
96518518 PM |
114 | skb->sk->sk_socket->file->f_cred->fsgid); |
115 | read_unlock_bh(&skb->sk->sk_callback_lock); | |
116 | break; | |
06efbd6d | 117 | #ifdef CONFIG_IP_ROUTE_CLASSID |
96518518 PM |
118 | case NFT_META_RTCLASSID: { |
119 | const struct dst_entry *dst = skb_dst(skb); | |
120 | ||
121 | if (dst == NULL) | |
122 | goto err; | |
fad136ea | 123 | *dest = dst->tclassid; |
96518518 PM |
124 | break; |
125 | } | |
126 | #endif | |
127 | #ifdef CONFIG_NETWORK_SECMARK | |
128 | case NFT_META_SECMARK: | |
fad136ea | 129 | *dest = skb->secmark; |
96518518 PM |
130 | break; |
131 | #endif | |
e2a093ff AR |
132 | case NFT_META_PKTTYPE: |
133 | if (skb->pkt_type != PACKET_LOOPBACK) { | |
fad136ea | 134 | *dest = skb->pkt_type; |
e2a093ff AR |
135 | break; |
136 | } | |
137 | ||
6aa187f2 | 138 | switch (pkt->pf) { |
e2a093ff AR |
139 | case NFPROTO_IPV4: |
140 | if (ipv4_is_multicast(ip_hdr(skb)->daddr)) | |
fad136ea | 141 | *dest = PACKET_MULTICAST; |
e2a093ff | 142 | else |
fad136ea | 143 | *dest = PACKET_BROADCAST; |
e2a093ff AR |
144 | break; |
145 | case NFPROTO_IPV6: | |
146 | if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF) | |
fad136ea | 147 | *dest = PACKET_MULTICAST; |
e2a093ff | 148 | else |
fad136ea | 149 | *dest = PACKET_BROADCAST; |
e2a093ff AR |
150 | break; |
151 | default: | |
152 | WARN_ON(1); | |
153 | goto err; | |
154 | } | |
155 | break; | |
afc5be30 | 156 | case NFT_META_CPU: |
fad136ea | 157 | *dest = raw_smp_processor_id(); |
afc5be30 | 158 | break; |
3045d760 AR |
159 | case NFT_META_IIFGROUP: |
160 | if (in == NULL) | |
161 | goto err; | |
fad136ea | 162 | *dest = in->group; |
3045d760 AR |
163 | break; |
164 | case NFT_META_OIFGROUP: | |
165 | if (out == NULL) | |
166 | goto err; | |
fad136ea | 167 | *dest = out->group; |
3045d760 | 168 | break; |
e181a543 | 169 | #ifdef CONFIG_CGROUP_NET_CLASSID |
ce674173 | 170 | case NFT_META_CGROUP: |
c5035c77 PNA |
171 | if (skb->sk == NULL || !sk_fullsock(skb->sk)) |
172 | goto err; | |
fad136ea | 173 | *dest = skb->sk->sk_classid; |
ce674173 | 174 | break; |
e181a543 | 175 | #endif |
96518518 PM |
176 | default: |
177 | WARN_ON(1); | |
178 | goto err; | |
179 | } | |
180 | return; | |
181 | ||
182 | err: | |
a55e22e9 | 183 | regs->verdict.code = NFT_BREAK; |
96518518 | 184 | } |
aa45660c | 185 | EXPORT_SYMBOL_GPL(nft_meta_get_eval); |
96518518 | 186 | |
aa45660c | 187 | void nft_meta_set_eval(const struct nft_expr *expr, |
a55e22e9 | 188 | struct nft_regs *regs, |
aa45660c | 189 | const struct nft_pktinfo *pkt) |
e035b77a ABG |
190 | { |
191 | const struct nft_meta *meta = nft_expr_priv(expr); | |
192 | struct sk_buff *skb = pkt->skb; | |
49499c3e | 193 | u32 value = regs->data[meta->sreg]; |
e035b77a ABG |
194 | |
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); | |
207 | } | |
208 | } | |
aa45660c | 209 | EXPORT_SYMBOL_GPL(nft_meta_set_eval); |
e035b77a | 210 | |
aa45660c | 211 | const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = { |
96518518 PM |
212 | [NFTA_META_DREG] = { .type = NLA_U32 }, |
213 | [NFTA_META_KEY] = { .type = NLA_U32 }, | |
e035b77a | 214 | [NFTA_META_SREG] = { .type = NLA_U32 }, |
96518518 | 215 | }; |
aa45660c | 216 | EXPORT_SYMBOL_GPL(nft_meta_policy); |
96518518 | 217 | |
aa45660c TB |
218 | int nft_meta_get_init(const struct nft_ctx *ctx, |
219 | const struct nft_expr *expr, | |
220 | const struct nlattr * const tb[]) | |
96518518 | 221 | { |
d2caa696 | 222 | struct nft_meta *priv = nft_expr_priv(expr); |
45d9bcda | 223 | unsigned int len; |
96518518 | 224 | |
d2caa696 PM |
225 | priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); |
226 | switch (priv->key) { | |
96518518 | 227 | case NFT_META_PROTOCOL: |
45d9bcda PM |
228 | case NFT_META_IIFTYPE: |
229 | case NFT_META_OIFTYPE: | |
230 | len = sizeof(u16); | |
231 | break; | |
124edfa9 | 232 | case NFT_META_NFPROTO: |
4566bf27 | 233 | case NFT_META_L4PROTO: |
45d9bcda | 234 | case NFT_META_LEN: |
96518518 PM |
235 | case NFT_META_PRIORITY: |
236 | case NFT_META_MARK: | |
237 | case NFT_META_IIF: | |
238 | case NFT_META_OIF: | |
96518518 PM |
239 | case NFT_META_SKUID: |
240 | case NFT_META_SKGID: | |
06efbd6d | 241 | #ifdef CONFIG_IP_ROUTE_CLASSID |
96518518 PM |
242 | case NFT_META_RTCLASSID: |
243 | #endif | |
244 | #ifdef CONFIG_NETWORK_SECMARK | |
245 | case NFT_META_SECMARK: | |
246 | #endif | |
e2a093ff | 247 | case NFT_META_PKTTYPE: |
afc5be30 | 248 | case NFT_META_CPU: |
3045d760 AR |
249 | case NFT_META_IIFGROUP: |
250 | case NFT_META_OIFGROUP: | |
e181a543 | 251 | #ifdef CONFIG_CGROUP_NET_CLASSID |
ce674173 | 252 | case NFT_META_CGROUP: |
e181a543 | 253 | #endif |
45d9bcda PM |
254 | len = sizeof(u32); |
255 | break; | |
256 | case NFT_META_IIFNAME: | |
257 | case NFT_META_OIFNAME: | |
258 | len = IFNAMSIZ; | |
d2caa696 | 259 | break; |
96518518 PM |
260 | default: |
261 | return -EOPNOTSUPP; | |
262 | } | |
263 | ||
b1c96ed3 | 264 | priv->dreg = nft_parse_register(tb[NFTA_META_DREG]); |
27e6d201 PM |
265 | return nft_validate_register_store(ctx, priv->dreg, NULL, |
266 | NFT_DATA_VALUE, len); | |
e035b77a | 267 | } |
aa45660c | 268 | EXPORT_SYMBOL_GPL(nft_meta_get_init); |
e035b77a | 269 | |
aa45660c TB |
270 | int nft_meta_set_init(const struct nft_ctx *ctx, |
271 | const struct nft_expr *expr, | |
272 | const struct nlattr * const tb[]) | |
e035b77a ABG |
273 | { |
274 | struct nft_meta *priv = nft_expr_priv(expr); | |
d07db988 | 275 | unsigned int len; |
e035b77a ABG |
276 | int err; |
277 | ||
278 | priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); | |
d2caa696 PM |
279 | switch (priv->key) { |
280 | case NFT_META_MARK: | |
281 | case NFT_META_PRIORITY: | |
d07db988 PM |
282 | len = sizeof(u32); |
283 | break; | |
d2caa696 | 284 | case NFT_META_NFTRACE: |
d07db988 | 285 | len = sizeof(u8); |
d2caa696 PM |
286 | break; |
287 | default: | |
288 | return -EOPNOTSUPP; | |
e035b77a ABG |
289 | } |
290 | ||
b1c96ed3 | 291 | priv->sreg = nft_parse_register(tb[NFTA_META_SREG]); |
d07db988 | 292 | err = nft_validate_register_load(priv->sreg, len); |
b38895c5 PNA |
293 | if (err < 0) |
294 | return err; | |
e035b77a ABG |
295 | |
296 | return 0; | |
96518518 | 297 | } |
aa45660c | 298 | EXPORT_SYMBOL_GPL(nft_meta_set_init); |
96518518 | 299 | |
aa45660c TB |
300 | int nft_meta_get_dump(struct sk_buff *skb, |
301 | const struct nft_expr *expr) | |
96518518 PM |
302 | { |
303 | const struct nft_meta *priv = nft_expr_priv(expr); | |
304 | ||
e035b77a ABG |
305 | if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key))) |
306 | goto nla_put_failure; | |
b1c96ed3 | 307 | if (nft_dump_register(skb, NFTA_META_DREG, priv->dreg)) |
96518518 | 308 | goto nla_put_failure; |
e035b77a ABG |
309 | return 0; |
310 | ||
311 | nla_put_failure: | |
312 | return -1; | |
313 | } | |
aa45660c | 314 | EXPORT_SYMBOL_GPL(nft_meta_get_dump); |
e035b77a | 315 | |
aa45660c TB |
316 | int nft_meta_set_dump(struct sk_buff *skb, |
317 | const struct nft_expr *expr) | |
e035b77a ABG |
318 | { |
319 | const struct nft_meta *priv = nft_expr_priv(expr); | |
320 | ||
96518518 PM |
321 | if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key))) |
322 | goto nla_put_failure; | |
b1c96ed3 | 323 | if (nft_dump_register(skb, NFTA_META_SREG, priv->sreg)) |
e035b77a ABG |
324 | goto nla_put_failure; |
325 | ||
96518518 PM |
326 | return 0; |
327 | ||
328 | nla_put_failure: | |
329 | return -1; | |
330 | } | |
aa45660c | 331 | EXPORT_SYMBOL_GPL(nft_meta_set_dump); |
96518518 | 332 | |
ef1f7df9 | 333 | static struct nft_expr_type nft_meta_type; |
e035b77a | 334 | static const struct nft_expr_ops nft_meta_get_ops = { |
ef1f7df9 | 335 | .type = &nft_meta_type, |
96518518 | 336 | .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)), |
e035b77a | 337 | .eval = nft_meta_get_eval, |
d2caa696 | 338 | .init = nft_meta_get_init, |
e035b77a | 339 | .dump = nft_meta_get_dump, |
ef1f7df9 PM |
340 | }; |
341 | ||
e035b77a ABG |
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, | |
d2caa696 | 346 | .init = nft_meta_set_init, |
e035b77a ABG |
347 | .dump = nft_meta_set_dump, |
348 | }; | |
349 | ||
350 | static const struct nft_expr_ops * | |
351 | nft_meta_select_ops(const struct nft_ctx *ctx, | |
352 | const struct nlattr * const tb[]) | |
353 | { | |
354 | if (tb[NFTA_META_KEY] == NULL) | |
355 | return ERR_PTR(-EINVAL); | |
356 | ||
357 | if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG]) | |
358 | return ERR_PTR(-EINVAL); | |
359 | ||
360 | if (tb[NFTA_META_DREG]) | |
361 | return &nft_meta_get_ops; | |
362 | ||
363 | if (tb[NFTA_META_SREG]) | |
364 | return &nft_meta_set_ops; | |
365 | ||
366 | return ERR_PTR(-EINVAL); | |
367 | } | |
368 | ||
ef1f7df9 PM |
369 | static struct nft_expr_type nft_meta_type __read_mostly = { |
370 | .name = "meta", | |
e035b77a | 371 | .select_ops = &nft_meta_select_ops, |
96518518 PM |
372 | .policy = nft_meta_policy, |
373 | .maxattr = NFTA_META_MAX, | |
ef1f7df9 | 374 | .owner = THIS_MODULE, |
96518518 PM |
375 | }; |
376 | ||
377 | static int __init nft_meta_module_init(void) | |
378 | { | |
ef1f7df9 | 379 | return nft_register_expr(&nft_meta_type); |
96518518 PM |
380 | } |
381 | ||
382 | static void __exit nft_meta_module_exit(void) | |
383 | { | |
ef1f7df9 | 384 | nft_unregister_expr(&nft_meta_type); |
96518518 PM |
385 | } |
386 | ||
387 | module_init(nft_meta_module_init); | |
388 | module_exit(nft_meta_module_exit); | |
389 | ||
390 | MODULE_LICENSE("GPL"); | |
391 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
392 | MODULE_ALIAS_NFT_EXPR("meta"); |