]>
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> |
e639f7ab | 21 | #include <linux/static_key.h> |
96518518 PM |
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> | |
e639f7ab | 26 | #include <net/netfilter/nf_tables_core.h> |
aa45660c | 27 | #include <net/netfilter/nft_meta.h> |
96518518 | 28 | |
b4aae759 FW |
29 | #include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */ |
30 | ||
b07edbe1 FW |
31 | static DEFINE_PER_CPU(struct rnd_state, nft_prandom_state); |
32 | ||
aa45660c | 33 | void nft_meta_get_eval(const struct nft_expr *expr, |
a55e22e9 | 34 | struct nft_regs *regs, |
aa45660c | 35 | const struct nft_pktinfo *pkt) |
96518518 PM |
36 | { |
37 | const struct nft_meta *priv = nft_expr_priv(expr); | |
38 | const struct sk_buff *skb = pkt->skb; | |
0e5a1c7e | 39 | const struct net_device *in = nft_in(pkt), *out = nft_out(pkt); |
3aed8225 | 40 | struct sock *sk; |
49499c3e | 41 | u32 *dest = ®s->data[priv->dreg]; |
96518518 PM |
42 | |
43 | switch (priv->key) { | |
44 | case NFT_META_LEN: | |
fad136ea | 45 | *dest = skb->len; |
96518518 PM |
46 | break; |
47 | case NFT_META_PROTOCOL: | |
10596608 | 48 | nft_reg_store16(dest, (__force u16)skb->protocol); |
96518518 | 49 | break; |
124edfa9 | 50 | case NFT_META_NFPROTO: |
10596608 | 51 | nft_reg_store8(dest, nft_pf(pkt)); |
124edfa9 | 52 | break; |
4566bf27 | 53 | case NFT_META_L4PROTO: |
beac5afa PNA |
54 | if (!pkt->tprot_set) |
55 | goto err; | |
10596608 | 56 | nft_reg_store8(dest, pkt->tprot); |
4566bf27 | 57 | break; |
96518518 | 58 | case NFT_META_PRIORITY: |
fad136ea | 59 | *dest = skb->priority; |
96518518 PM |
60 | break; |
61 | case NFT_META_MARK: | |
fad136ea | 62 | *dest = skb->mark; |
96518518 PM |
63 | break; |
64 | case NFT_META_IIF: | |
65 | if (in == NULL) | |
66 | goto err; | |
fad136ea | 67 | *dest = in->ifindex; |
96518518 PM |
68 | break; |
69 | case NFT_META_OIF: | |
70 | if (out == NULL) | |
71 | goto err; | |
fad136ea | 72 | *dest = out->ifindex; |
96518518 PM |
73 | break; |
74 | case NFT_META_IIFNAME: | |
75 | if (in == NULL) | |
76 | goto err; | |
fad136ea | 77 | strncpy((char *)dest, in->name, IFNAMSIZ); |
96518518 PM |
78 | break; |
79 | case NFT_META_OIFNAME: | |
80 | if (out == NULL) | |
81 | goto err; | |
fad136ea | 82 | strncpy((char *)dest, out->name, IFNAMSIZ); |
96518518 PM |
83 | break; |
84 | case NFT_META_IIFTYPE: | |
85 | if (in == NULL) | |
86 | goto err; | |
10596608 | 87 | nft_reg_store16(dest, in->type); |
96518518 PM |
88 | break; |
89 | case NFT_META_OIFTYPE: | |
90 | if (out == NULL) | |
91 | goto err; | |
10596608 | 92 | nft_reg_store16(dest, out->type); |
96518518 PM |
93 | break; |
94 | case NFT_META_SKUID: | |
3aed8225 ED |
95 | sk = skb_to_full_sk(skb); |
96 | if (!sk || !sk_fullsock(sk)) | |
96518518 PM |
97 | goto err; |
98 | ||
3aed8225 ED |
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); | |
96518518 PM |
103 | goto err; |
104 | } | |
105 | ||
fad136ea | 106 | *dest = from_kuid_munged(&init_user_ns, |
3aed8225 ED |
107 | sk->sk_socket->file->f_cred->fsuid); |
108 | read_unlock_bh(&sk->sk_callback_lock); | |
96518518 PM |
109 | break; |
110 | case NFT_META_SKGID: | |
3aed8225 ED |
111 | sk = skb_to_full_sk(skb); |
112 | if (!sk || !sk_fullsock(sk)) | |
96518518 PM |
113 | goto err; |
114 | ||
3aed8225 ED |
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); | |
96518518 PM |
119 | goto err; |
120 | } | |
fad136ea | 121 | *dest = from_kgid_munged(&init_user_ns, |
3aed8225 ED |
122 | sk->sk_socket->file->f_cred->fsgid); |
123 | read_unlock_bh(&sk->sk_callback_lock); | |
96518518 | 124 | break; |
06efbd6d | 125 | #ifdef CONFIG_IP_ROUTE_CLASSID |
96518518 PM |
126 | case NFT_META_RTCLASSID: { |
127 | const struct dst_entry *dst = skb_dst(skb); | |
128 | ||
129 | if (dst == NULL) | |
130 | goto err; | |
fad136ea | 131 | *dest = dst->tclassid; |
96518518 PM |
132 | break; |
133 | } | |
134 | #endif | |
135 | #ifdef CONFIG_NETWORK_SECMARK | |
136 | case NFT_META_SECMARK: | |
fad136ea | 137 | *dest = skb->secmark; |
96518518 PM |
138 | break; |
139 | #endif | |
e2a093ff AR |
140 | case NFT_META_PKTTYPE: |
141 | if (skb->pkt_type != PACKET_LOOPBACK) { | |
10596608 | 142 | nft_reg_store8(dest, skb->pkt_type); |
e2a093ff AR |
143 | break; |
144 | } | |
145 | ||
0e5a1c7e | 146 | switch (nft_pf(pkt)) { |
e2a093ff AR |
147 | case NFPROTO_IPV4: |
148 | if (ipv4_is_multicast(ip_hdr(skb)->daddr)) | |
10596608 | 149 | nft_reg_store8(dest, PACKET_MULTICAST); |
e2a093ff | 150 | else |
10596608 | 151 | nft_reg_store8(dest, PACKET_BROADCAST); |
e2a093ff AR |
152 | break; |
153 | case NFPROTO_IPV6: | |
10596608 | 154 | nft_reg_store8(dest, PACKET_MULTICAST); |
e2a093ff | 155 | break; |
f169fd69 LZ |
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; | |
161 | ||
162 | iph = skb_header_pointer(skb, noff, | |
163 | sizeof(_iph), &_iph); | |
164 | if (!iph) | |
165 | goto err; | |
166 | ||
167 | if (ipv4_is_multicast(iph->daddr)) | |
10596608 | 168 | nft_reg_store8(dest, PACKET_MULTICAST); |
f169fd69 | 169 | else |
10596608 | 170 | nft_reg_store8(dest, PACKET_BROADCAST); |
f169fd69 LZ |
171 | |
172 | break; | |
173 | } | |
174 | case htons(ETH_P_IPV6): | |
10596608 | 175 | nft_reg_store8(dest, PACKET_MULTICAST); |
f169fd69 LZ |
176 | break; |
177 | default: | |
178 | WARN_ON_ONCE(1); | |
179 | goto err; | |
180 | } | |
181 | break; | |
e2a093ff | 182 | default: |
f169fd69 | 183 | WARN_ON_ONCE(1); |
e2a093ff AR |
184 | goto err; |
185 | } | |
186 | break; | |
afc5be30 | 187 | case NFT_META_CPU: |
fad136ea | 188 | *dest = raw_smp_processor_id(); |
afc5be30 | 189 | break; |
3045d760 AR |
190 | case NFT_META_IIFGROUP: |
191 | if (in == NULL) | |
192 | goto err; | |
fad136ea | 193 | *dest = in->group; |
3045d760 AR |
194 | break; |
195 | case NFT_META_OIFGROUP: | |
196 | if (out == NULL) | |
197 | goto err; | |
fad136ea | 198 | *dest = out->group; |
3045d760 | 199 | break; |
e181a543 | 200 | #ifdef CONFIG_CGROUP_NET_CLASSID |
ce674173 | 201 | case NFT_META_CGROUP: |
3aed8225 ED |
202 | sk = skb_to_full_sk(skb); |
203 | if (!sk || !sk_fullsock(sk)) | |
c5035c77 | 204 | goto err; |
2a56a1fe | 205 | *dest = sock_cgroup_classid(&sk->sk_cgrp_data); |
ce674173 | 206 | break; |
e181a543 | 207 | #endif |
b07edbe1 FW |
208 | case NFT_META_PRANDOM: { |
209 | struct rnd_state *state = this_cpu_ptr(&nft_prandom_state); | |
210 | *dest = prandom_u32_state(state); | |
211 | break; | |
212 | } | |
96518518 PM |
213 | default: |
214 | WARN_ON(1); | |
215 | goto err; | |
216 | } | |
217 | return; | |
218 | ||
219 | err: | |
a55e22e9 | 220 | regs->verdict.code = NFT_BREAK; |
96518518 | 221 | } |
aa45660c | 222 | EXPORT_SYMBOL_GPL(nft_meta_get_eval); |
96518518 | 223 | |
aa45660c | 224 | void nft_meta_set_eval(const struct nft_expr *expr, |
a55e22e9 | 225 | struct nft_regs *regs, |
aa45660c | 226 | const struct nft_pktinfo *pkt) |
e035b77a ABG |
227 | { |
228 | const struct nft_meta *meta = nft_expr_priv(expr); | |
229 | struct sk_buff *skb = pkt->skb; | |
10596608 LZ |
230 | u32 *sreg = ®s->data[meta->sreg]; |
231 | u32 value = *sreg; | |
232 | u8 pkt_type; | |
e035b77a ABG |
233 | |
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; | |
b4aae759 | 241 | case NFT_META_PKTTYPE: |
10596608 LZ |
242 | pkt_type = nft_reg_load8(sreg); |
243 | ||
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; | |
b4aae759 | 248 | break; |
e035b77a | 249 | case NFT_META_NFTRACE: |
62131e5d | 250 | skb->nf_trace = !!value; |
e035b77a ABG |
251 | break; |
252 | default: | |
253 | WARN_ON(1); | |
254 | } | |
255 | } | |
aa45660c | 256 | EXPORT_SYMBOL_GPL(nft_meta_set_eval); |
e035b77a | 257 | |
aa45660c | 258 | const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = { |
96518518 PM |
259 | [NFTA_META_DREG] = { .type = NLA_U32 }, |
260 | [NFTA_META_KEY] = { .type = NLA_U32 }, | |
e035b77a | 261 | [NFTA_META_SREG] = { .type = NLA_U32 }, |
96518518 | 262 | }; |
aa45660c | 263 | EXPORT_SYMBOL_GPL(nft_meta_policy); |
96518518 | 264 | |
aa45660c TB |
265 | int nft_meta_get_init(const struct nft_ctx *ctx, |
266 | const struct nft_expr *expr, | |
267 | const struct nlattr * const tb[]) | |
96518518 | 268 | { |
d2caa696 | 269 | struct nft_meta *priv = nft_expr_priv(expr); |
45d9bcda | 270 | unsigned int len; |
96518518 | 271 | |
d2caa696 PM |
272 | priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); |
273 | switch (priv->key) { | |
96518518 | 274 | case NFT_META_PROTOCOL: |
45d9bcda PM |
275 | case NFT_META_IIFTYPE: |
276 | case NFT_META_OIFTYPE: | |
277 | len = sizeof(u16); | |
278 | break; | |
124edfa9 | 279 | case NFT_META_NFPROTO: |
4566bf27 | 280 | case NFT_META_L4PROTO: |
45d9bcda | 281 | case NFT_META_LEN: |
96518518 PM |
282 | case NFT_META_PRIORITY: |
283 | case NFT_META_MARK: | |
284 | case NFT_META_IIF: | |
285 | case NFT_META_OIF: | |
96518518 PM |
286 | case NFT_META_SKUID: |
287 | case NFT_META_SKGID: | |
06efbd6d | 288 | #ifdef CONFIG_IP_ROUTE_CLASSID |
96518518 PM |
289 | case NFT_META_RTCLASSID: |
290 | #endif | |
291 | #ifdef CONFIG_NETWORK_SECMARK | |
292 | case NFT_META_SECMARK: | |
293 | #endif | |
e2a093ff | 294 | case NFT_META_PKTTYPE: |
afc5be30 | 295 | case NFT_META_CPU: |
3045d760 AR |
296 | case NFT_META_IIFGROUP: |
297 | case NFT_META_OIFGROUP: | |
e181a543 | 298 | #ifdef CONFIG_CGROUP_NET_CLASSID |
ce674173 | 299 | case NFT_META_CGROUP: |
e181a543 | 300 | #endif |
45d9bcda PM |
301 | len = sizeof(u32); |
302 | break; | |
303 | case NFT_META_IIFNAME: | |
304 | case NFT_META_OIFNAME: | |
305 | len = IFNAMSIZ; | |
d2caa696 | 306 | break; |
b07edbe1 FW |
307 | case NFT_META_PRANDOM: |
308 | prandom_init_once(&nft_prandom_state); | |
309 | len = sizeof(u32); | |
310 | break; | |
96518518 PM |
311 | default: |
312 | return -EOPNOTSUPP; | |
313 | } | |
314 | ||
b1c96ed3 | 315 | priv->dreg = nft_parse_register(tb[NFTA_META_DREG]); |
27e6d201 PM |
316 | return nft_validate_register_store(ctx, priv->dreg, NULL, |
317 | NFT_DATA_VALUE, len); | |
e035b77a | 318 | } |
aa45660c | 319 | EXPORT_SYMBOL_GPL(nft_meta_get_init); |
e035b77a | 320 | |
960fa72f LZ |
321 | int nft_meta_set_validate(const struct nft_ctx *ctx, |
322 | const struct nft_expr *expr, | |
323 | const struct nft_data **data) | |
b4aae759 | 324 | { |
960fa72f | 325 | struct nft_meta *priv = nft_expr_priv(expr); |
b4aae759 FW |
326 | unsigned int hooks; |
327 | ||
960fa72f LZ |
328 | if (priv->key != NFT_META_PKTTYPE) |
329 | return 0; | |
330 | ||
b4aae759 FW |
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; | |
96d9f2a7 LZ |
338 | case NFPROTO_IPV4: |
339 | case NFPROTO_IPV6: | |
340 | case NFPROTO_INET: | |
341 | hooks = 1 << NF_INET_PRE_ROUTING; | |
342 | break; | |
b4aae759 FW |
343 | default: |
344 | return -EOPNOTSUPP; | |
345 | } | |
346 | ||
347 | return nft_chain_validate_hooks(ctx->chain, hooks); | |
348 | } | |
960fa72f | 349 | EXPORT_SYMBOL_GPL(nft_meta_set_validate); |
b4aae759 | 350 | |
aa45660c TB |
351 | int nft_meta_set_init(const struct nft_ctx *ctx, |
352 | const struct nft_expr *expr, | |
353 | const struct nlattr * const tb[]) | |
e035b77a ABG |
354 | { |
355 | struct nft_meta *priv = nft_expr_priv(expr); | |
d07db988 | 356 | unsigned int len; |
e035b77a ABG |
357 | int err; |
358 | ||
359 | priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); | |
d2caa696 PM |
360 | switch (priv->key) { |
361 | case NFT_META_MARK: | |
362 | case NFT_META_PRIORITY: | |
d07db988 PM |
363 | len = sizeof(u32); |
364 | break; | |
d2caa696 | 365 | case NFT_META_NFTRACE: |
d07db988 | 366 | len = sizeof(u8); |
d2caa696 | 367 | break; |
b4aae759 | 368 | case NFT_META_PKTTYPE: |
b4aae759 FW |
369 | len = sizeof(u8); |
370 | break; | |
d2caa696 PM |
371 | default: |
372 | return -EOPNOTSUPP; | |
e035b77a ABG |
373 | } |
374 | ||
b1c96ed3 | 375 | priv->sreg = nft_parse_register(tb[NFTA_META_SREG]); |
d07db988 | 376 | err = nft_validate_register_load(priv->sreg, len); |
b38895c5 PNA |
377 | if (err < 0) |
378 | return err; | |
e035b77a | 379 | |
e639f7ab FW |
380 | if (priv->key == NFT_META_NFTRACE) |
381 | static_branch_inc(&nft_trace_enabled); | |
382 | ||
e035b77a | 383 | return 0; |
96518518 | 384 | } |
aa45660c | 385 | EXPORT_SYMBOL_GPL(nft_meta_set_init); |
96518518 | 386 | |
aa45660c TB |
387 | int nft_meta_get_dump(struct sk_buff *skb, |
388 | const struct nft_expr *expr) | |
96518518 PM |
389 | { |
390 | const struct nft_meta *priv = nft_expr_priv(expr); | |
391 | ||
e035b77a ABG |
392 | if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key))) |
393 | goto nla_put_failure; | |
b1c96ed3 | 394 | if (nft_dump_register(skb, NFTA_META_DREG, priv->dreg)) |
96518518 | 395 | goto nla_put_failure; |
e035b77a ABG |
396 | return 0; |
397 | ||
398 | nla_put_failure: | |
399 | return -1; | |
400 | } | |
aa45660c | 401 | EXPORT_SYMBOL_GPL(nft_meta_get_dump); |
e035b77a | 402 | |
aa45660c TB |
403 | int nft_meta_set_dump(struct sk_buff *skb, |
404 | const struct nft_expr *expr) | |
e035b77a ABG |
405 | { |
406 | const struct nft_meta *priv = nft_expr_priv(expr); | |
407 | ||
96518518 PM |
408 | if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key))) |
409 | goto nla_put_failure; | |
b1c96ed3 | 410 | if (nft_dump_register(skb, NFTA_META_SREG, priv->sreg)) |
e035b77a ABG |
411 | goto nla_put_failure; |
412 | ||
96518518 PM |
413 | return 0; |
414 | ||
415 | nla_put_failure: | |
416 | return -1; | |
417 | } | |
aa45660c | 418 | EXPORT_SYMBOL_GPL(nft_meta_set_dump); |
96518518 | 419 | |
e639f7ab FW |
420 | void nft_meta_set_destroy(const struct nft_ctx *ctx, |
421 | const struct nft_expr *expr) | |
422 | { | |
423 | const struct nft_meta *priv = nft_expr_priv(expr); | |
424 | ||
425 | if (priv->key == NFT_META_NFTRACE) | |
426 | static_branch_dec(&nft_trace_enabled); | |
427 | } | |
428 | EXPORT_SYMBOL_GPL(nft_meta_set_destroy); | |
429 | ||
ef1f7df9 | 430 | static struct nft_expr_type nft_meta_type; |
e035b77a | 431 | static const struct nft_expr_ops nft_meta_get_ops = { |
ef1f7df9 | 432 | .type = &nft_meta_type, |
96518518 | 433 | .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)), |
e035b77a | 434 | .eval = nft_meta_get_eval, |
d2caa696 | 435 | .init = nft_meta_get_init, |
e035b77a | 436 | .dump = nft_meta_get_dump, |
ef1f7df9 PM |
437 | }; |
438 | ||
e035b77a ABG |
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, | |
d2caa696 | 443 | .init = nft_meta_set_init, |
e639f7ab | 444 | .destroy = nft_meta_set_destroy, |
e035b77a | 445 | .dump = nft_meta_set_dump, |
960fa72f | 446 | .validate = nft_meta_set_validate, |
e035b77a ABG |
447 | }; |
448 | ||
449 | static const struct nft_expr_ops * | |
450 | nft_meta_select_ops(const struct nft_ctx *ctx, | |
451 | const struct nlattr * const tb[]) | |
452 | { | |
453 | if (tb[NFTA_META_KEY] == NULL) | |
454 | return ERR_PTR(-EINVAL); | |
455 | ||
456 | if (tb[NFTA_META_DREG] && tb[NFTA_META_SREG]) | |
457 | return ERR_PTR(-EINVAL); | |
458 | ||
459 | if (tb[NFTA_META_DREG]) | |
460 | return &nft_meta_get_ops; | |
461 | ||
462 | if (tb[NFTA_META_SREG]) | |
463 | return &nft_meta_set_ops; | |
464 | ||
465 | return ERR_PTR(-EINVAL); | |
466 | } | |
467 | ||
ef1f7df9 PM |
468 | static struct nft_expr_type nft_meta_type __read_mostly = { |
469 | .name = "meta", | |
d4ef3835 | 470 | .select_ops = nft_meta_select_ops, |
96518518 PM |
471 | .policy = nft_meta_policy, |
472 | .maxattr = NFTA_META_MAX, | |
ef1f7df9 | 473 | .owner = THIS_MODULE, |
96518518 PM |
474 | }; |
475 | ||
476 | static int __init nft_meta_module_init(void) | |
477 | { | |
ef1f7df9 | 478 | return nft_register_expr(&nft_meta_type); |
96518518 PM |
479 | } |
480 | ||
481 | static void __exit nft_meta_module_exit(void) | |
482 | { | |
ef1f7df9 | 483 | nft_unregister_expr(&nft_meta_type); |
96518518 PM |
484 | } |
485 | ||
486 | module_init(nft_meta_module_init); | |
487 | module_exit(nft_meta_module_exit); | |
488 | ||
489 | MODULE_LICENSE("GPL"); | |
490 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
491 | MODULE_ALIAS_NFT_EXPR("meta"); |