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