]>
Commit | Line | Data |
---|---|---|
0ca743a5 PNA |
1 | /* |
2 | * (C) 2012-2013 by Pablo Neira Ayuso <[email protected]> | |
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 | * This software has been sponsored by Sophos Astaro <http://www.sophos.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/nfnetlink.h> | |
17 | #include <linux/netfilter/nf_tables.h> | |
18 | #include <linux/netfilter/nf_tables_compat.h> | |
19 | #include <linux/netfilter/x_tables.h> | |
20 | #include <linux/netfilter_ipv4/ip_tables.h> | |
21 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
5191f4d8 | 22 | #include <linux/netfilter_bridge/ebtables.h> |
5f158939 | 23 | #include <linux/netfilter_arp/arp_tables.h> |
0ca743a5 PNA |
24 | #include <net/netfilter/nf_tables.h> |
25 | ||
4b512e1c LZ |
26 | struct nft_xt { |
27 | struct list_head head; | |
28 | struct nft_expr_ops ops; | |
29 | unsigned int refcnt; | |
30 | }; | |
31 | ||
32 | static void nft_xt_put(struct nft_xt *xt) | |
33 | { | |
34 | if (--xt->refcnt == 0) { | |
35 | list_del(&xt->head); | |
36 | kfree(xt); | |
37 | } | |
38 | } | |
39 | ||
f3f5dded PNA |
40 | static int nft_compat_chain_validate_dependency(const char *tablename, |
41 | const struct nft_chain *chain) | |
42 | { | |
f3f5dded PNA |
43 | const struct nft_base_chain *basechain; |
44 | ||
f323d954 PNA |
45 | if (!tablename || |
46 | !nft_is_base_chain(chain)) | |
f3f5dded PNA |
47 | return 0; |
48 | ||
f3f5dded | 49 | basechain = nft_base_chain(chain); |
c918687f PNA |
50 | if (strcmp(tablename, "nat") == 0 && |
51 | basechain->type->type != NFT_CHAIN_T_NAT) | |
f3f5dded PNA |
52 | return -EINVAL; |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
0ca743a5 PNA |
57 | union nft_entry { |
58 | struct ipt_entry e4; | |
59 | struct ip6t_entry e6; | |
5191f4d8 | 60 | struct ebt_entry ebt; |
5f158939 | 61 | struct arpt_entry arp; |
0ca743a5 PNA |
62 | }; |
63 | ||
64 | static inline void | |
65 | nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info) | |
66 | { | |
67 | par->target = xt; | |
68 | par->targinfo = xt_info; | |
69 | par->hotdrop = false; | |
70 | } | |
71 | ||
5191f4d8 | 72 | static void nft_target_eval_xt(const struct nft_expr *expr, |
a55e22e9 | 73 | struct nft_regs *regs, |
5191f4d8 | 74 | const struct nft_pktinfo *pkt) |
0ca743a5 PNA |
75 | { |
76 | void *info = nft_expr_priv(expr); | |
77 | struct xt_target *target = expr->ops->data; | |
78 | struct sk_buff *skb = pkt->skb; | |
79 | int ret; | |
80 | ||
81 | nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info); | |
82 | ||
83 | ret = target->target(skb, &pkt->xt); | |
84 | ||
85 | if (pkt->xt.hotdrop) | |
86 | ret = NF_DROP; | |
87 | ||
5191f4d8 | 88 | switch (ret) { |
0ca743a5 | 89 | case XT_CONTINUE: |
a55e22e9 | 90 | regs->verdict.code = NFT_CONTINUE; |
0ca743a5 PNA |
91 | break; |
92 | default: | |
a55e22e9 | 93 | regs->verdict.code = ret; |
0ca743a5 PNA |
94 | break; |
95 | } | |
5191f4d8 AB |
96 | } |
97 | ||
98 | static void nft_target_eval_bridge(const struct nft_expr *expr, | |
a55e22e9 | 99 | struct nft_regs *regs, |
5191f4d8 AB |
100 | const struct nft_pktinfo *pkt) |
101 | { | |
102 | void *info = nft_expr_priv(expr); | |
103 | struct xt_target *target = expr->ops->data; | |
104 | struct sk_buff *skb = pkt->skb; | |
105 | int ret; | |
106 | ||
107 | nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info); | |
108 | ||
109 | ret = target->target(skb, &pkt->xt); | |
110 | ||
111 | if (pkt->xt.hotdrop) | |
112 | ret = NF_DROP; | |
113 | ||
114 | switch (ret) { | |
115 | case EBT_ACCEPT: | |
a55e22e9 | 116 | regs->verdict.code = NF_ACCEPT; |
5191f4d8 AB |
117 | break; |
118 | case EBT_DROP: | |
a55e22e9 | 119 | regs->verdict.code = NF_DROP; |
5191f4d8 AB |
120 | break; |
121 | case EBT_CONTINUE: | |
a55e22e9 | 122 | regs->verdict.code = NFT_CONTINUE; |
5191f4d8 AB |
123 | break; |
124 | case EBT_RETURN: | |
a55e22e9 | 125 | regs->verdict.code = NFT_RETURN; |
5191f4d8 AB |
126 | break; |
127 | default: | |
a55e22e9 | 128 | regs->verdict.code = ret; |
5191f4d8 AB |
129 | break; |
130 | } | |
0ca743a5 PNA |
131 | } |
132 | ||
133 | static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = { | |
134 | [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING }, | |
135 | [NFTA_TARGET_REV] = { .type = NLA_U32 }, | |
136 | [NFTA_TARGET_INFO] = { .type = NLA_BINARY }, | |
137 | }; | |
138 | ||
139 | static void | |
140 | nft_target_set_tgchk_param(struct xt_tgchk_param *par, | |
141 | const struct nft_ctx *ctx, | |
142 | struct xt_target *target, void *info, | |
2156d321 | 143 | union nft_entry *entry, u16 proto, bool inv) |
0ca743a5 | 144 | { |
2daf1b4d | 145 | par->net = ctx->net; |
0ca743a5 | 146 | par->table = ctx->table->name; |
36596dad | 147 | switch (ctx->family) { |
0ca743a5 PNA |
148 | case AF_INET: |
149 | entry->e4.ip.proto = proto; | |
150 | entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0; | |
151 | break; | |
152 | case AF_INET6: | |
749177cc PNA |
153 | if (proto) |
154 | entry->e6.ipv6.flags |= IP6T_F_PROTO; | |
155 | ||
0ca743a5 PNA |
156 | entry->e6.ipv6.proto = proto; |
157 | entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0; | |
158 | break; | |
5191f4d8 | 159 | case NFPROTO_BRIDGE: |
2156d321 | 160 | entry->ebt.ethproto = (__force __be16)proto; |
5191f4d8 AB |
161 | entry->ebt.invflags = inv ? EBT_IPROTO : 0; |
162 | break; | |
5f158939 AB |
163 | case NFPROTO_ARP: |
164 | break; | |
0ca743a5 PNA |
165 | } |
166 | par->entryinfo = entry; | |
167 | par->target = target; | |
168 | par->targinfo = info; | |
f323d954 | 169 | if (nft_is_base_chain(ctx->chain)) { |
0ca743a5 PNA |
170 | const struct nft_base_chain *basechain = |
171 | nft_base_chain(ctx->chain); | |
c974a3a3 | 172 | const struct nf_hook_ops *ops = &basechain->ops; |
0ca743a5 PNA |
173 | |
174 | par->hook_mask = 1 << ops->hooknum; | |
493618a9 PNA |
175 | } else { |
176 | par->hook_mask = 0; | |
0ca743a5 | 177 | } |
36596dad | 178 | par->family = ctx->family; |
55917a21 | 179 | par->nft_compat = true; |
0ca743a5 PNA |
180 | } |
181 | ||
182 | static void target_compat_from_user(struct xt_target *t, void *in, void *out) | |
183 | { | |
756c1b1a | 184 | int pad; |
0ca743a5 | 185 | |
756c1b1a PNA |
186 | memcpy(out, in, t->targetsize); |
187 | pad = XT_ALIGN(t->targetsize) - t->targetsize; | |
188 | if (pad > 0) | |
189 | memset(out + t->targetsize, 0, pad); | |
0ca743a5 PNA |
190 | } |
191 | ||
192 | static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = { | |
193 | [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 }, | |
194 | [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 }, | |
195 | }; | |
196 | ||
2156d321 | 197 | static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv) |
0ca743a5 PNA |
198 | { |
199 | struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1]; | |
200 | u32 flags; | |
201 | int err; | |
202 | ||
203 | err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr, | |
fceb6435 | 204 | nft_rule_compat_policy, NULL); |
0ca743a5 PNA |
205 | if (err < 0) |
206 | return err; | |
207 | ||
208 | if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS]) | |
209 | return -EINVAL; | |
210 | ||
211 | flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS])); | |
212 | if (flags & ~NFT_RULE_COMPAT_F_MASK) | |
213 | return -EINVAL; | |
214 | if (flags & NFT_RULE_COMPAT_F_INV) | |
215 | *inv = true; | |
216 | ||
8691a9a3 PNA |
217 | *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO])); |
218 | return 0; | |
0ca743a5 PNA |
219 | } |
220 | ||
221 | static int | |
222 | nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
223 | const struct nlattr * const tb[]) | |
224 | { | |
225 | void *info = nft_expr_priv(expr); | |
226 | struct xt_target *target = expr->ops->data; | |
227 | struct xt_tgchk_param par; | |
228 | size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO])); | |
2156d321 | 229 | u16 proto = 0; |
0ca743a5 PNA |
230 | bool inv = false; |
231 | union nft_entry e = {}; | |
232 | int ret; | |
233 | ||
234 | target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info); | |
235 | ||
8691a9a3 PNA |
236 | if (ctx->nla[NFTA_RULE_COMPAT]) { |
237 | ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv); | |
238 | if (ret < 0) | |
239 | goto err; | |
240 | } | |
0ca743a5 PNA |
241 | |
242 | nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv); | |
243 | ||
244 | ret = xt_check_target(&par, size, proto, inv); | |
245 | if (ret < 0) | |
246 | goto err; | |
247 | ||
248 | /* The standard target cannot be used */ | |
249 | if (target->target == NULL) { | |
250 | ret = -EINVAL; | |
251 | goto err; | |
252 | } | |
253 | ||
254 | return 0; | |
255 | err: | |
256 | module_put(target->me); | |
257 | return ret; | |
258 | } | |
259 | ||
260 | static void | |
62472bce | 261 | nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
0ca743a5 PNA |
262 | { |
263 | struct xt_target *target = expr->ops->data; | |
3d9b1421 PNA |
264 | void *info = nft_expr_priv(expr); |
265 | struct xt_tgdtor_param par; | |
266 | ||
267 | par.net = ctx->net; | |
268 | par.target = target; | |
269 | par.targinfo = info; | |
36596dad | 270 | par.family = ctx->family; |
3d9b1421 PNA |
271 | if (par.target->destroy != NULL) |
272 | par.target->destroy(&par); | |
0ca743a5 | 273 | |
4b512e1c | 274 | nft_xt_put(container_of(expr->ops, struct nft_xt, ops)); |
0ca743a5 PNA |
275 | module_put(target->me); |
276 | } | |
277 | ||
0ca743a5 PNA |
278 | static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr) |
279 | { | |
280 | const struct xt_target *target = expr->ops->data; | |
281 | void *info = nft_expr_priv(expr); | |
282 | ||
283 | if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) || | |
284 | nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) || | |
756c1b1a | 285 | nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(target->targetsize), info)) |
0ca743a5 PNA |
286 | goto nla_put_failure; |
287 | ||
288 | return 0; | |
289 | ||
290 | nla_put_failure: | |
291 | return -1; | |
292 | } | |
293 | ||
294 | static int nft_target_validate(const struct nft_ctx *ctx, | |
295 | const struct nft_expr *expr, | |
296 | const struct nft_data **data) | |
297 | { | |
298 | struct xt_target *target = expr->ops->data; | |
299 | unsigned int hook_mask = 0; | |
f3f5dded | 300 | int ret; |
0ca743a5 | 301 | |
f323d954 | 302 | if (nft_is_base_chain(ctx->chain)) { |
0ca743a5 PNA |
303 | const struct nft_base_chain *basechain = |
304 | nft_base_chain(ctx->chain); | |
c974a3a3 | 305 | const struct nf_hook_ops *ops = &basechain->ops; |
0ca743a5 PNA |
306 | |
307 | hook_mask = 1 << ops->hooknum; | |
f7fb77fc | 308 | if (target->hooks && !(hook_mask & target->hooks)) |
f3f5dded | 309 | return -EINVAL; |
0ca743a5 | 310 | |
f3f5dded PNA |
311 | ret = nft_compat_chain_validate_dependency(target->table, |
312 | ctx->chain); | |
313 | if (ret < 0) | |
314 | return ret; | |
0ca743a5 PNA |
315 | } |
316 | return 0; | |
317 | } | |
318 | ||
319 | static void nft_match_eval(const struct nft_expr *expr, | |
a55e22e9 | 320 | struct nft_regs *regs, |
0ca743a5 PNA |
321 | const struct nft_pktinfo *pkt) |
322 | { | |
323 | void *info = nft_expr_priv(expr); | |
324 | struct xt_match *match = expr->ops->data; | |
325 | struct sk_buff *skb = pkt->skb; | |
326 | bool ret; | |
327 | ||
328 | nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info); | |
329 | ||
330 | ret = match->match(skb, (struct xt_action_param *)&pkt->xt); | |
331 | ||
332 | if (pkt->xt.hotdrop) { | |
a55e22e9 | 333 | regs->verdict.code = NF_DROP; |
0ca743a5 PNA |
334 | return; |
335 | } | |
336 | ||
c1f86676 DM |
337 | switch (ret ? 1 : 0) { |
338 | case 1: | |
a55e22e9 | 339 | regs->verdict.code = NFT_CONTINUE; |
0ca743a5 | 340 | break; |
c1f86676 | 341 | case 0: |
a55e22e9 | 342 | regs->verdict.code = NFT_BREAK; |
0ca743a5 PNA |
343 | break; |
344 | } | |
345 | } | |
346 | ||
347 | static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = { | |
348 | [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING }, | |
349 | [NFTA_MATCH_REV] = { .type = NLA_U32 }, | |
350 | [NFTA_MATCH_INFO] = { .type = NLA_BINARY }, | |
351 | }; | |
352 | ||
353 | /* struct xt_mtchk_param and xt_tgchk_param look very similar */ | |
354 | static void | |
355 | nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx, | |
356 | struct xt_match *match, void *info, | |
2156d321 | 357 | union nft_entry *entry, u16 proto, bool inv) |
0ca743a5 | 358 | { |
2daf1b4d | 359 | par->net = ctx->net; |
0ca743a5 | 360 | par->table = ctx->table->name; |
36596dad | 361 | switch (ctx->family) { |
0ca743a5 PNA |
362 | case AF_INET: |
363 | entry->e4.ip.proto = proto; | |
364 | entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0; | |
365 | break; | |
366 | case AF_INET6: | |
749177cc PNA |
367 | if (proto) |
368 | entry->e6.ipv6.flags |= IP6T_F_PROTO; | |
369 | ||
0ca743a5 PNA |
370 | entry->e6.ipv6.proto = proto; |
371 | entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0; | |
372 | break; | |
5191f4d8 | 373 | case NFPROTO_BRIDGE: |
2156d321 | 374 | entry->ebt.ethproto = (__force __be16)proto; |
5191f4d8 AB |
375 | entry->ebt.invflags = inv ? EBT_IPROTO : 0; |
376 | break; | |
5f158939 AB |
377 | case NFPROTO_ARP: |
378 | break; | |
0ca743a5 PNA |
379 | } |
380 | par->entryinfo = entry; | |
381 | par->match = match; | |
382 | par->matchinfo = info; | |
f323d954 | 383 | if (nft_is_base_chain(ctx->chain)) { |
0ca743a5 PNA |
384 | const struct nft_base_chain *basechain = |
385 | nft_base_chain(ctx->chain); | |
c974a3a3 | 386 | const struct nf_hook_ops *ops = &basechain->ops; |
0ca743a5 PNA |
387 | |
388 | par->hook_mask = 1 << ops->hooknum; | |
493618a9 PNA |
389 | } else { |
390 | par->hook_mask = 0; | |
0ca743a5 | 391 | } |
36596dad | 392 | par->family = ctx->family; |
55917a21 | 393 | par->nft_compat = true; |
0ca743a5 PNA |
394 | } |
395 | ||
396 | static void match_compat_from_user(struct xt_match *m, void *in, void *out) | |
397 | { | |
756c1b1a PNA |
398 | int pad; |
399 | ||
400 | memcpy(out, in, m->matchsize); | |
401 | pad = XT_ALIGN(m->matchsize) - m->matchsize; | |
402 | if (pad > 0) | |
403 | memset(out + m->matchsize, 0, pad); | |
0ca743a5 PNA |
404 | } |
405 | ||
406 | static int | |
407 | nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
408 | const struct nlattr * const tb[]) | |
409 | { | |
410 | void *info = nft_expr_priv(expr); | |
411 | struct xt_match *match = expr->ops->data; | |
412 | struct xt_mtchk_param par; | |
413 | size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO])); | |
2156d321 | 414 | u16 proto = 0; |
0ca743a5 PNA |
415 | bool inv = false; |
416 | union nft_entry e = {}; | |
417 | int ret; | |
418 | ||
419 | match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info); | |
420 | ||
8691a9a3 PNA |
421 | if (ctx->nla[NFTA_RULE_COMPAT]) { |
422 | ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv); | |
423 | if (ret < 0) | |
424 | goto err; | |
425 | } | |
0ca743a5 PNA |
426 | |
427 | nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv); | |
428 | ||
429 | ret = xt_check_match(&par, size, proto, inv); | |
430 | if (ret < 0) | |
431 | goto err; | |
432 | ||
433 | return 0; | |
434 | err: | |
435 | module_put(match->me); | |
436 | return ret; | |
437 | } | |
438 | ||
439 | static void | |
62472bce | 440 | nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
0ca743a5 PNA |
441 | { |
442 | struct xt_match *match = expr->ops->data; | |
3d9b1421 PNA |
443 | void *info = nft_expr_priv(expr); |
444 | struct xt_mtdtor_param par; | |
445 | ||
446 | par.net = ctx->net; | |
447 | par.match = match; | |
448 | par.matchinfo = info; | |
36596dad | 449 | par.family = ctx->family; |
3d9b1421 PNA |
450 | if (par.match->destroy != NULL) |
451 | par.match->destroy(&par); | |
0ca743a5 | 452 | |
4b512e1c | 453 | nft_xt_put(container_of(expr->ops, struct nft_xt, ops)); |
0ca743a5 PNA |
454 | module_put(match->me); |
455 | } | |
456 | ||
0ca743a5 PNA |
457 | static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr) |
458 | { | |
459 | void *info = nft_expr_priv(expr); | |
460 | struct xt_match *match = expr->ops->data; | |
461 | ||
462 | if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) || | |
463 | nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) || | |
756c1b1a | 464 | nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(match->matchsize), info)) |
0ca743a5 PNA |
465 | goto nla_put_failure; |
466 | ||
467 | return 0; | |
468 | ||
469 | nla_put_failure: | |
470 | return -1; | |
471 | } | |
472 | ||
473 | static int nft_match_validate(const struct nft_ctx *ctx, | |
474 | const struct nft_expr *expr, | |
475 | const struct nft_data **data) | |
476 | { | |
477 | struct xt_match *match = expr->ops->data; | |
478 | unsigned int hook_mask = 0; | |
f3f5dded | 479 | int ret; |
0ca743a5 | 480 | |
f323d954 | 481 | if (nft_is_base_chain(ctx->chain)) { |
0ca743a5 PNA |
482 | const struct nft_base_chain *basechain = |
483 | nft_base_chain(ctx->chain); | |
c974a3a3 | 484 | const struct nf_hook_ops *ops = &basechain->ops; |
0ca743a5 PNA |
485 | |
486 | hook_mask = 1 << ops->hooknum; | |
f7fb77fc | 487 | if (match->hooks && !(hook_mask & match->hooks)) |
f3f5dded | 488 | return -EINVAL; |
0ca743a5 | 489 | |
afefb6f9 | 490 | ret = nft_compat_chain_validate_dependency(match->table, |
f3f5dded PNA |
491 | ctx->chain); |
492 | if (ret < 0) | |
493 | return ret; | |
0ca743a5 PNA |
494 | } |
495 | return 0; | |
496 | } | |
497 | ||
498 | static int | |
499 | nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, | |
500 | int event, u16 family, const char *name, | |
501 | int rev, int target) | |
502 | { | |
503 | struct nlmsghdr *nlh; | |
504 | struct nfgenmsg *nfmsg; | |
505 | unsigned int flags = portid ? NLM_F_MULTI : 0; | |
506 | ||
dedb67c4 | 507 | event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event); |
0ca743a5 PNA |
508 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
509 | if (nlh == NULL) | |
510 | goto nlmsg_failure; | |
511 | ||
512 | nfmsg = nlmsg_data(nlh); | |
513 | nfmsg->nfgen_family = family; | |
514 | nfmsg->version = NFNETLINK_V0; | |
515 | nfmsg->res_id = 0; | |
516 | ||
517 | if (nla_put_string(skb, NFTA_COMPAT_NAME, name) || | |
518 | nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) || | |
519 | nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target))) | |
520 | goto nla_put_failure; | |
521 | ||
522 | nlmsg_end(skb, nlh); | |
523 | return skb->len; | |
524 | ||
525 | nlmsg_failure: | |
526 | nla_put_failure: | |
527 | nlmsg_cancel(skb, nlh); | |
528 | return -1; | |
529 | } | |
530 | ||
7b8002a1 PNA |
531 | static int nfnl_compat_get(struct net *net, struct sock *nfnl, |
532 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
533 | const struct nlattr * const tb[], |
534 | struct netlink_ext_ack *extack) | |
0ca743a5 PNA |
535 | { |
536 | int ret = 0, target; | |
537 | struct nfgenmsg *nfmsg; | |
538 | const char *fmt; | |
539 | const char *name; | |
540 | u32 rev; | |
541 | struct sk_buff *skb2; | |
542 | ||
543 | if (tb[NFTA_COMPAT_NAME] == NULL || | |
544 | tb[NFTA_COMPAT_REV] == NULL || | |
545 | tb[NFTA_COMPAT_TYPE] == NULL) | |
546 | return -EINVAL; | |
547 | ||
548 | name = nla_data(tb[NFTA_COMPAT_NAME]); | |
549 | rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV])); | |
550 | target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE])); | |
551 | ||
552 | nfmsg = nlmsg_data(nlh); | |
553 | ||
554 | switch(nfmsg->nfgen_family) { | |
555 | case AF_INET: | |
556 | fmt = "ipt_%s"; | |
557 | break; | |
558 | case AF_INET6: | |
559 | fmt = "ip6t_%s"; | |
560 | break; | |
5191f4d8 AB |
561 | case NFPROTO_BRIDGE: |
562 | fmt = "ebt_%s"; | |
563 | break; | |
5f158939 AB |
564 | case NFPROTO_ARP: |
565 | fmt = "arpt_%s"; | |
566 | break; | |
0ca743a5 PNA |
567 | default: |
568 | pr_err("nft_compat: unsupported protocol %d\n", | |
569 | nfmsg->nfgen_family); | |
570 | return -EINVAL; | |
571 | } | |
572 | ||
573 | try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name, | |
574 | rev, target, &ret), | |
575 | fmt, name); | |
576 | ||
577 | if (ret < 0) | |
578 | return ret; | |
579 | ||
580 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
581 | if (skb2 == NULL) | |
582 | return -ENOMEM; | |
583 | ||
584 | /* include the best revision for this extension in the message */ | |
585 | if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid, | |
586 | nlh->nlmsg_seq, | |
587 | NFNL_MSG_TYPE(nlh->nlmsg_type), | |
588 | NFNL_MSG_COMPAT_GET, | |
589 | nfmsg->nfgen_family, | |
590 | name, ret, target) <= 0) { | |
591 | kfree_skb(skb2); | |
592 | return -ENOSPC; | |
593 | } | |
594 | ||
595 | ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid, | |
596 | MSG_DONTWAIT); | |
597 | if (ret > 0) | |
598 | ret = 0; | |
599 | ||
600 | return ret == -EAGAIN ? -ENOBUFS : ret; | |
601 | } | |
602 | ||
603 | static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = { | |
604 | [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING, | |
605 | .len = NFT_COMPAT_NAME_MAX-1 }, | |
606 | [NFTA_COMPAT_REV] = { .type = NLA_U32 }, | |
607 | [NFTA_COMPAT_TYPE] = { .type = NLA_U32 }, | |
608 | }; | |
609 | ||
610 | static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = { | |
611 | [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get, | |
612 | .attr_count = NFTA_COMPAT_MAX, | |
613 | .policy = nfnl_compat_policy_get }, | |
614 | }; | |
615 | ||
616 | static const struct nfnetlink_subsystem nfnl_compat_subsys = { | |
617 | .name = "nft-compat", | |
618 | .subsys_id = NFNL_SUBSYS_NFT_COMPAT, | |
619 | .cb_count = NFNL_MSG_COMPAT_MAX, | |
620 | .cb = nfnl_nft_compat_cb, | |
621 | }; | |
622 | ||
623 | static LIST_HEAD(nft_match_list); | |
624 | ||
0ca743a5 PNA |
625 | static struct nft_expr_type nft_match_type; |
626 | ||
ba378ca9 PNA |
627 | static bool nft_match_cmp(const struct xt_match *match, |
628 | const char *name, u32 rev, u32 family) | |
629 | { | |
630 | return strcmp(match->name, name) == 0 && match->revision == rev && | |
631 | (match->family == NFPROTO_UNSPEC || match->family == family); | |
632 | } | |
633 | ||
0ca743a5 PNA |
634 | static const struct nft_expr_ops * |
635 | nft_match_select_ops(const struct nft_ctx *ctx, | |
636 | const struct nlattr * const tb[]) | |
637 | { | |
638 | struct nft_xt *nft_match; | |
639 | struct xt_match *match; | |
640 | char *mt_name; | |
ba378ca9 | 641 | u32 rev, family; |
2bf4fade | 642 | int err; |
0ca743a5 PNA |
643 | |
644 | if (tb[NFTA_MATCH_NAME] == NULL || | |
645 | tb[NFTA_MATCH_REV] == NULL || | |
646 | tb[NFTA_MATCH_INFO] == NULL) | |
647 | return ERR_PTR(-EINVAL); | |
648 | ||
649 | mt_name = nla_data(tb[NFTA_MATCH_NAME]); | |
650 | rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV])); | |
36596dad | 651 | family = ctx->family; |
0ca743a5 PNA |
652 | |
653 | /* Re-use the existing match if it's already loaded. */ | |
654 | list_for_each_entry(nft_match, &nft_match_list, head) { | |
655 | struct xt_match *match = nft_match->ops.data; | |
656 | ||
ba378ca9 | 657 | if (nft_match_cmp(match, mt_name, rev, family)) { |
520aa741 PNA |
658 | if (!try_module_get(match->me)) |
659 | return ERR_PTR(-ENOENT); | |
660 | ||
4b512e1c | 661 | nft_match->refcnt++; |
0ca743a5 | 662 | return &nft_match->ops; |
520aa741 | 663 | } |
0ca743a5 PNA |
664 | } |
665 | ||
666 | match = xt_request_find_match(family, mt_name, rev); | |
667 | if (IS_ERR(match)) | |
668 | return ERR_PTR(-ENOENT); | |
669 | ||
2bf4fade LZ |
670 | if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) { |
671 | err = -EINVAL; | |
672 | goto err; | |
673 | } | |
f0716cd6 | 674 | |
0ca743a5 PNA |
675 | /* This is the first time we use this match, allocate operations */ |
676 | nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL); | |
2bf4fade LZ |
677 | if (nft_match == NULL) { |
678 | err = -ENOMEM; | |
679 | goto err; | |
680 | } | |
0ca743a5 | 681 | |
4b512e1c | 682 | nft_match->refcnt = 1; |
0ca743a5 | 683 | nft_match->ops.type = &nft_match_type; |
756c1b1a | 684 | nft_match->ops.size = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize)); |
0ca743a5 PNA |
685 | nft_match->ops.eval = nft_match_eval; |
686 | nft_match->ops.init = nft_match_init; | |
687 | nft_match->ops.destroy = nft_match_destroy; | |
688 | nft_match->ops.dump = nft_match_dump; | |
689 | nft_match->ops.validate = nft_match_validate; | |
690 | nft_match->ops.data = match; | |
691 | ||
692 | list_add(&nft_match->head, &nft_match_list); | |
693 | ||
694 | return &nft_match->ops; | |
2bf4fade LZ |
695 | err: |
696 | module_put(match->me); | |
697 | return ERR_PTR(err); | |
0ca743a5 PNA |
698 | } |
699 | ||
0ca743a5 PNA |
700 | static struct nft_expr_type nft_match_type __read_mostly = { |
701 | .name = "match", | |
702 | .select_ops = nft_match_select_ops, | |
703 | .policy = nft_match_policy, | |
704 | .maxattr = NFTA_MATCH_MAX, | |
705 | .owner = THIS_MODULE, | |
706 | }; | |
707 | ||
708 | static LIST_HEAD(nft_target_list); | |
709 | ||
710 | static struct nft_expr_type nft_target_type; | |
711 | ||
ba378ca9 PNA |
712 | static bool nft_target_cmp(const struct xt_target *tg, |
713 | const char *name, u32 rev, u32 family) | |
714 | { | |
715 | return strcmp(tg->name, name) == 0 && tg->revision == rev && | |
716 | (tg->family == NFPROTO_UNSPEC || tg->family == family); | |
717 | } | |
718 | ||
0ca743a5 PNA |
719 | static const struct nft_expr_ops * |
720 | nft_target_select_ops(const struct nft_ctx *ctx, | |
721 | const struct nlattr * const tb[]) | |
722 | { | |
723 | struct nft_xt *nft_target; | |
724 | struct xt_target *target; | |
725 | char *tg_name; | |
ba378ca9 | 726 | u32 rev, family; |
2bf4fade | 727 | int err; |
0ca743a5 PNA |
728 | |
729 | if (tb[NFTA_TARGET_NAME] == NULL || | |
730 | tb[NFTA_TARGET_REV] == NULL || | |
731 | tb[NFTA_TARGET_INFO] == NULL) | |
732 | return ERR_PTR(-EINVAL); | |
733 | ||
734 | tg_name = nla_data(tb[NFTA_TARGET_NAME]); | |
735 | rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV])); | |
36596dad | 736 | family = ctx->family; |
0ca743a5 PNA |
737 | |
738 | /* Re-use the existing target if it's already loaded. */ | |
7965ee93 | 739 | list_for_each_entry(nft_target, &nft_target_list, head) { |
0ca743a5 PNA |
740 | struct xt_target *target = nft_target->ops.data; |
741 | ||
ba378ca9 | 742 | if (nft_target_cmp(target, tg_name, rev, family)) { |
520aa741 PNA |
743 | if (!try_module_get(target->me)) |
744 | return ERR_PTR(-ENOENT); | |
745 | ||
4b512e1c | 746 | nft_target->refcnt++; |
0ca743a5 | 747 | return &nft_target->ops; |
520aa741 | 748 | } |
0ca743a5 PNA |
749 | } |
750 | ||
751 | target = xt_request_find_target(family, tg_name, rev); | |
752 | if (IS_ERR(target)) | |
753 | return ERR_PTR(-ENOENT); | |
754 | ||
2bf4fade LZ |
755 | if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) { |
756 | err = -EINVAL; | |
757 | goto err; | |
758 | } | |
f0716cd6 | 759 | |
0ca743a5 PNA |
760 | /* This is the first time we use this target, allocate operations */ |
761 | nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL); | |
2bf4fade LZ |
762 | if (nft_target == NULL) { |
763 | err = -ENOMEM; | |
764 | goto err; | |
765 | } | |
0ca743a5 | 766 | |
4b512e1c | 767 | nft_target->refcnt = 1; |
0ca743a5 | 768 | nft_target->ops.type = &nft_target_type; |
756c1b1a | 769 | nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize)); |
0ca743a5 PNA |
770 | nft_target->ops.init = nft_target_init; |
771 | nft_target->ops.destroy = nft_target_destroy; | |
772 | nft_target->ops.dump = nft_target_dump; | |
773 | nft_target->ops.validate = nft_target_validate; | |
774 | nft_target->ops.data = target; | |
775 | ||
5191f4d8 AB |
776 | if (family == NFPROTO_BRIDGE) |
777 | nft_target->ops.eval = nft_target_eval_bridge; | |
778 | else | |
779 | nft_target->ops.eval = nft_target_eval_xt; | |
780 | ||
0ca743a5 PNA |
781 | list_add(&nft_target->head, &nft_target_list); |
782 | ||
783 | return &nft_target->ops; | |
2bf4fade LZ |
784 | err: |
785 | module_put(target->me); | |
786 | return ERR_PTR(err); | |
0ca743a5 PNA |
787 | } |
788 | ||
0ca743a5 PNA |
789 | static struct nft_expr_type nft_target_type __read_mostly = { |
790 | .name = "target", | |
791 | .select_ops = nft_target_select_ops, | |
792 | .policy = nft_target_policy, | |
793 | .maxattr = NFTA_TARGET_MAX, | |
794 | .owner = THIS_MODULE, | |
795 | }; | |
796 | ||
797 | static int __init nft_compat_module_init(void) | |
798 | { | |
799 | int ret; | |
800 | ||
801 | ret = nft_register_expr(&nft_match_type); | |
802 | if (ret < 0) | |
803 | return ret; | |
804 | ||
805 | ret = nft_register_expr(&nft_target_type); | |
806 | if (ret < 0) | |
807 | goto err_match; | |
808 | ||
809 | ret = nfnetlink_subsys_register(&nfnl_compat_subsys); | |
810 | if (ret < 0) { | |
811 | pr_err("nft_compat: cannot register with nfnetlink.\n"); | |
812 | goto err_target; | |
813 | } | |
814 | ||
0ca743a5 PNA |
815 | return ret; |
816 | ||
817 | err_target: | |
818 | nft_unregister_expr(&nft_target_type); | |
819 | err_match: | |
820 | nft_unregister_expr(&nft_match_type); | |
821 | return ret; | |
822 | } | |
823 | ||
824 | static void __exit nft_compat_module_exit(void) | |
825 | { | |
826 | nfnetlink_subsys_unregister(&nfnl_compat_subsys); | |
827 | nft_unregister_expr(&nft_target_type); | |
828 | nft_unregister_expr(&nft_match_type); | |
0ca743a5 PNA |
829 | } |
830 | ||
831 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT); | |
832 | ||
833 | module_init(nft_compat_module_init); | |
834 | module_exit(nft_compat_module_exit); | |
835 | ||
836 | MODULE_LICENSE("GPL"); | |
837 | MODULE_AUTHOR("Pablo Neira Ayuso <[email protected]>"); | |
838 | MODULE_ALIAS_NFT_EXPR("match"); | |
839 | MODULE_ALIAS_NFT_EXPR("target"); |