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