2 * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
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.
8 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
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 <net/netfilter/nf_tables.h>
24 static int nft_compat_chain_validate_dependency(const char *tablename
,
25 const struct nft_chain
*chain
)
27 const struct nft_base_chain
*basechain
;
29 if (!tablename
|| !(chain
->flags
& NFT_BASE_CHAIN
))
32 basechain
= nft_base_chain(chain
);
33 if (strcmp(tablename
, "nat") == 0 &&
34 basechain
->type
->type
!= NFT_CHAIN_T_NAT
)
46 nft_compat_set_par(struct xt_action_param
*par
, void *xt
, const void *xt_info
)
49 par
->targinfo
= xt_info
;
53 static void nft_target_eval(const struct nft_expr
*expr
,
54 struct nft_data data
[NFT_REG_MAX
+ 1],
55 const struct nft_pktinfo
*pkt
)
57 void *info
= nft_expr_priv(expr
);
58 struct xt_target
*target
= expr
->ops
->data
;
59 struct sk_buff
*skb
= pkt
->skb
;
62 nft_compat_set_par((struct xt_action_param
*)&pkt
->xt
, target
, info
);
64 ret
= target
->target(skb
, &pkt
->xt
);
71 data
[NFT_REG_VERDICT
].verdict
= NFT_CONTINUE
;
74 data
[NFT_REG_VERDICT
].verdict
= ret
;
80 static const struct nla_policy nft_target_policy
[NFTA_TARGET_MAX
+ 1] = {
81 [NFTA_TARGET_NAME
] = { .type
= NLA_NUL_STRING
},
82 [NFTA_TARGET_REV
] = { .type
= NLA_U32
},
83 [NFTA_TARGET_INFO
] = { .type
= NLA_BINARY
},
87 nft_target_set_tgchk_param(struct xt_tgchk_param
*par
,
88 const struct nft_ctx
*ctx
,
89 struct xt_target
*target
, void *info
,
90 union nft_entry
*entry
, u8 proto
, bool inv
)
93 par
->table
= ctx
->table
->name
;
94 switch (ctx
->afi
->family
) {
96 entry
->e4
.ip
.proto
= proto
;
97 entry
->e4
.ip
.invflags
= inv
? IPT_INV_PROTO
: 0;
100 entry
->e6
.ipv6
.proto
= proto
;
101 entry
->e6
.ipv6
.invflags
= inv
? IP6T_INV_PROTO
: 0;
104 par
->entryinfo
= entry
;
105 par
->target
= target
;
106 par
->targinfo
= info
;
107 if (ctx
->chain
->flags
& NFT_BASE_CHAIN
) {
108 const struct nft_base_chain
*basechain
=
109 nft_base_chain(ctx
->chain
);
110 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
112 par
->hook_mask
= 1 << ops
->hooknum
;
116 par
->family
= ctx
->afi
->family
;
119 static void target_compat_from_user(struct xt_target
*t
, void *in
, void *out
)
123 memcpy(out
, in
, t
->targetsize
);
124 pad
= XT_ALIGN(t
->targetsize
) - t
->targetsize
;
126 memset(out
+ t
->targetsize
, 0, pad
);
129 static const struct nla_policy nft_rule_compat_policy
[NFTA_RULE_COMPAT_MAX
+ 1] = {
130 [NFTA_RULE_COMPAT_PROTO
] = { .type
= NLA_U32
},
131 [NFTA_RULE_COMPAT_FLAGS
] = { .type
= NLA_U32
},
134 static int nft_parse_compat(const struct nlattr
*attr
, u8
*proto
, bool *inv
)
136 struct nlattr
*tb
[NFTA_RULE_COMPAT_MAX
+1];
140 err
= nla_parse_nested(tb
, NFTA_RULE_COMPAT_MAX
, attr
,
141 nft_rule_compat_policy
);
145 if (!tb
[NFTA_RULE_COMPAT_PROTO
] || !tb
[NFTA_RULE_COMPAT_FLAGS
])
148 flags
= ntohl(nla_get_be32(tb
[NFTA_RULE_COMPAT_FLAGS
]));
149 if (flags
& ~NFT_RULE_COMPAT_F_MASK
)
151 if (flags
& NFT_RULE_COMPAT_F_INV
)
154 *proto
= ntohl(nla_get_be32(tb
[NFTA_RULE_COMPAT_PROTO
]));
159 nft_target_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
160 const struct nlattr
* const tb
[])
162 void *info
= nft_expr_priv(expr
);
163 struct xt_target
*target
= expr
->ops
->data
;
164 struct xt_tgchk_param par
;
165 size_t size
= XT_ALIGN(nla_len(tb
[NFTA_TARGET_INFO
]));
168 union nft_entry e
= {};
171 ret
= nft_compat_chain_validate_dependency(target
->table
, ctx
->chain
);
175 target_compat_from_user(target
, nla_data(tb
[NFTA_TARGET_INFO
]), info
);
177 if (ctx
->nla
[NFTA_RULE_COMPAT
]) {
178 ret
= nft_parse_compat(ctx
->nla
[NFTA_RULE_COMPAT
], &proto
, &inv
);
183 nft_target_set_tgchk_param(&par
, ctx
, target
, info
, &e
, proto
, inv
);
185 ret
= xt_check_target(&par
, size
, proto
, inv
);
189 /* The standard target cannot be used */
190 if (target
->target
== NULL
) {
197 module_put(target
->me
);
202 nft_target_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
204 struct xt_target
*target
= expr
->ops
->data
;
205 void *info
= nft_expr_priv(expr
);
206 struct xt_tgdtor_param par
;
211 par
.family
= ctx
->afi
->family
;
212 if (par
.target
->destroy
!= NULL
)
213 par
.target
->destroy(&par
);
215 module_put(target
->me
);
218 static int nft_target_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
220 const struct xt_target
*target
= expr
->ops
->data
;
221 void *info
= nft_expr_priv(expr
);
223 if (nla_put_string(skb
, NFTA_TARGET_NAME
, target
->name
) ||
224 nla_put_be32(skb
, NFTA_TARGET_REV
, htonl(target
->revision
)) ||
225 nla_put(skb
, NFTA_TARGET_INFO
, XT_ALIGN(target
->targetsize
), info
))
226 goto nla_put_failure
;
234 static int nft_target_validate(const struct nft_ctx
*ctx
,
235 const struct nft_expr
*expr
,
236 const struct nft_data
**data
)
238 struct xt_target
*target
= expr
->ops
->data
;
239 unsigned int hook_mask
= 0;
242 if (ctx
->chain
->flags
& NFT_BASE_CHAIN
) {
243 const struct nft_base_chain
*basechain
=
244 nft_base_chain(ctx
->chain
);
245 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
247 hook_mask
= 1 << ops
->hooknum
;
248 if (!(hook_mask
& target
->hooks
))
251 ret
= nft_compat_chain_validate_dependency(target
->table
,
259 static void nft_match_eval(const struct nft_expr
*expr
,
260 struct nft_data data
[NFT_REG_MAX
+ 1],
261 const struct nft_pktinfo
*pkt
)
263 void *info
= nft_expr_priv(expr
);
264 struct xt_match
*match
= expr
->ops
->data
;
265 struct sk_buff
*skb
= pkt
->skb
;
268 nft_compat_set_par((struct xt_action_param
*)&pkt
->xt
, match
, info
);
270 ret
= match
->match(skb
, (struct xt_action_param
*)&pkt
->xt
);
272 if (pkt
->xt
.hotdrop
) {
273 data
[NFT_REG_VERDICT
].verdict
= NF_DROP
;
279 data
[NFT_REG_VERDICT
].verdict
= NFT_CONTINUE
;
282 data
[NFT_REG_VERDICT
].verdict
= NFT_BREAK
;
287 static const struct nla_policy nft_match_policy
[NFTA_MATCH_MAX
+ 1] = {
288 [NFTA_MATCH_NAME
] = { .type
= NLA_NUL_STRING
},
289 [NFTA_MATCH_REV
] = { .type
= NLA_U32
},
290 [NFTA_MATCH_INFO
] = { .type
= NLA_BINARY
},
293 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
295 nft_match_set_mtchk_param(struct xt_mtchk_param
*par
, const struct nft_ctx
*ctx
,
296 struct xt_match
*match
, void *info
,
297 union nft_entry
*entry
, u8 proto
, bool inv
)
300 par
->table
= ctx
->table
->name
;
301 switch (ctx
->afi
->family
) {
303 entry
->e4
.ip
.proto
= proto
;
304 entry
->e4
.ip
.invflags
= inv
? IPT_INV_PROTO
: 0;
307 entry
->e6
.ipv6
.proto
= proto
;
308 entry
->e6
.ipv6
.invflags
= inv
? IP6T_INV_PROTO
: 0;
311 par
->entryinfo
= entry
;
313 par
->matchinfo
= info
;
314 if (ctx
->chain
->flags
& NFT_BASE_CHAIN
) {
315 const struct nft_base_chain
*basechain
=
316 nft_base_chain(ctx
->chain
);
317 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
319 par
->hook_mask
= 1 << ops
->hooknum
;
323 par
->family
= ctx
->afi
->family
;
326 static void match_compat_from_user(struct xt_match
*m
, void *in
, void *out
)
330 memcpy(out
, in
, m
->matchsize
);
331 pad
= XT_ALIGN(m
->matchsize
) - m
->matchsize
;
333 memset(out
+ m
->matchsize
, 0, pad
);
337 nft_match_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
338 const struct nlattr
* const tb
[])
340 void *info
= nft_expr_priv(expr
);
341 struct xt_match
*match
= expr
->ops
->data
;
342 struct xt_mtchk_param par
;
343 size_t size
= XT_ALIGN(nla_len(tb
[NFTA_MATCH_INFO
]));
346 union nft_entry e
= {};
349 ret
= nft_compat_chain_validate_dependency(match
->table
, ctx
->chain
);
353 match_compat_from_user(match
, nla_data(tb
[NFTA_MATCH_INFO
]), info
);
355 if (ctx
->nla
[NFTA_RULE_COMPAT
]) {
356 ret
= nft_parse_compat(ctx
->nla
[NFTA_RULE_COMPAT
], &proto
, &inv
);
361 nft_match_set_mtchk_param(&par
, ctx
, match
, info
, &e
, proto
, inv
);
363 ret
= xt_check_match(&par
, size
, proto
, inv
);
369 module_put(match
->me
);
374 nft_match_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
376 struct xt_match
*match
= expr
->ops
->data
;
377 void *info
= nft_expr_priv(expr
);
378 struct xt_mtdtor_param par
;
382 par
.matchinfo
= info
;
383 par
.family
= ctx
->afi
->family
;
384 if (par
.match
->destroy
!= NULL
)
385 par
.match
->destroy(&par
);
387 module_put(match
->me
);
390 static int nft_match_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
392 void *info
= nft_expr_priv(expr
);
393 struct xt_match
*match
= expr
->ops
->data
;
395 if (nla_put_string(skb
, NFTA_MATCH_NAME
, match
->name
) ||
396 nla_put_be32(skb
, NFTA_MATCH_REV
, htonl(match
->revision
)) ||
397 nla_put(skb
, NFTA_MATCH_INFO
, XT_ALIGN(match
->matchsize
), info
))
398 goto nla_put_failure
;
406 static int nft_match_validate(const struct nft_ctx
*ctx
,
407 const struct nft_expr
*expr
,
408 const struct nft_data
**data
)
410 struct xt_match
*match
= expr
->ops
->data
;
411 unsigned int hook_mask
= 0;
414 if (ctx
->chain
->flags
& NFT_BASE_CHAIN
) {
415 const struct nft_base_chain
*basechain
=
416 nft_base_chain(ctx
->chain
);
417 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
419 hook_mask
= 1 << ops
->hooknum
;
420 if (!(hook_mask
& match
->hooks
))
423 ret
= nft_compat_chain_validate_dependency(match
->table
,
432 nfnl_compat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
433 int event
, u16 family
, const char *name
,
436 struct nlmsghdr
*nlh
;
437 struct nfgenmsg
*nfmsg
;
438 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
440 event
|= NFNL_SUBSYS_NFT_COMPAT
<< 8;
441 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
445 nfmsg
= nlmsg_data(nlh
);
446 nfmsg
->nfgen_family
= family
;
447 nfmsg
->version
= NFNETLINK_V0
;
450 if (nla_put_string(skb
, NFTA_COMPAT_NAME
, name
) ||
451 nla_put_be32(skb
, NFTA_COMPAT_REV
, htonl(rev
)) ||
452 nla_put_be32(skb
, NFTA_COMPAT_TYPE
, htonl(target
)))
453 goto nla_put_failure
;
460 nlmsg_cancel(skb
, nlh
);
465 nfnl_compat_get(struct sock
*nfnl
, struct sk_buff
*skb
,
466 const struct nlmsghdr
*nlh
, const struct nlattr
* const tb
[])
469 struct nfgenmsg
*nfmsg
;
473 struct sk_buff
*skb2
;
475 if (tb
[NFTA_COMPAT_NAME
] == NULL
||
476 tb
[NFTA_COMPAT_REV
] == NULL
||
477 tb
[NFTA_COMPAT_TYPE
] == NULL
)
480 name
= nla_data(tb
[NFTA_COMPAT_NAME
]);
481 rev
= ntohl(nla_get_be32(tb
[NFTA_COMPAT_REV
]));
482 target
= ntohl(nla_get_be32(tb
[NFTA_COMPAT_TYPE
]));
484 nfmsg
= nlmsg_data(nlh
);
486 switch(nfmsg
->nfgen_family
) {
494 pr_err("nft_compat: unsupported protocol %d\n",
495 nfmsg
->nfgen_family
);
499 try_then_request_module(xt_find_revision(nfmsg
->nfgen_family
, name
,
506 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
510 /* include the best revision for this extension in the message */
511 if (nfnl_compat_fill_info(skb2
, NETLINK_CB(skb
).portid
,
513 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
516 name
, ret
, target
) <= 0) {
521 ret
= netlink_unicast(nfnl
, skb2
, NETLINK_CB(skb
).portid
,
526 return ret
== -EAGAIN
? -ENOBUFS
: ret
;
529 static const struct nla_policy nfnl_compat_policy_get
[NFTA_COMPAT_MAX
+1] = {
530 [NFTA_COMPAT_NAME
] = { .type
= NLA_NUL_STRING
,
531 .len
= NFT_COMPAT_NAME_MAX
-1 },
532 [NFTA_COMPAT_REV
] = { .type
= NLA_U32
},
533 [NFTA_COMPAT_TYPE
] = { .type
= NLA_U32
},
536 static const struct nfnl_callback nfnl_nft_compat_cb
[NFNL_MSG_COMPAT_MAX
] = {
537 [NFNL_MSG_COMPAT_GET
] = { .call
= nfnl_compat_get
,
538 .attr_count
= NFTA_COMPAT_MAX
,
539 .policy
= nfnl_compat_policy_get
},
542 static const struct nfnetlink_subsystem nfnl_compat_subsys
= {
543 .name
= "nft-compat",
544 .subsys_id
= NFNL_SUBSYS_NFT_COMPAT
,
545 .cb_count
= NFNL_MSG_COMPAT_MAX
,
546 .cb
= nfnl_nft_compat_cb
,
549 static LIST_HEAD(nft_match_list
);
552 struct list_head head
;
553 struct nft_expr_ops ops
;
556 static struct nft_expr_type nft_match_type
;
558 static const struct nft_expr_ops
*
559 nft_match_select_ops(const struct nft_ctx
*ctx
,
560 const struct nlattr
* const tb
[])
562 struct nft_xt
*nft_match
;
563 struct xt_match
*match
;
567 if (tb
[NFTA_MATCH_NAME
] == NULL
||
568 tb
[NFTA_MATCH_REV
] == NULL
||
569 tb
[NFTA_MATCH_INFO
] == NULL
)
570 return ERR_PTR(-EINVAL
);
572 mt_name
= nla_data(tb
[NFTA_MATCH_NAME
]);
573 rev
= ntohl(nla_get_be32(tb
[NFTA_MATCH_REV
]));
574 family
= ctx
->afi
->family
;
576 /* Re-use the existing match if it's already loaded. */
577 list_for_each_entry(nft_match
, &nft_match_list
, head
) {
578 struct xt_match
*match
= nft_match
->ops
.data
;
580 if (strcmp(match
->name
, mt_name
) == 0 &&
581 match
->revision
== rev
&& match
->family
== family
)
582 return &nft_match
->ops
;
585 match
= xt_request_find_match(family
, mt_name
, rev
);
587 return ERR_PTR(-ENOENT
);
589 /* This is the first time we use this match, allocate operations */
590 nft_match
= kzalloc(sizeof(struct nft_xt
), GFP_KERNEL
);
591 if (nft_match
== NULL
)
592 return ERR_PTR(-ENOMEM
);
594 nft_match
->ops
.type
= &nft_match_type
;
595 nft_match
->ops
.size
= NFT_EXPR_SIZE(XT_ALIGN(match
->matchsize
));
596 nft_match
->ops
.eval
= nft_match_eval
;
597 nft_match
->ops
.init
= nft_match_init
;
598 nft_match
->ops
.destroy
= nft_match_destroy
;
599 nft_match
->ops
.dump
= nft_match_dump
;
600 nft_match
->ops
.validate
= nft_match_validate
;
601 nft_match
->ops
.data
= match
;
603 list_add(&nft_match
->head
, &nft_match_list
);
605 return &nft_match
->ops
;
608 static void nft_match_release(void)
610 struct nft_xt
*nft_match
, *tmp
;
612 list_for_each_entry_safe(nft_match
, tmp
, &nft_match_list
, head
)
616 static struct nft_expr_type nft_match_type __read_mostly
= {
618 .select_ops
= nft_match_select_ops
,
619 .policy
= nft_match_policy
,
620 .maxattr
= NFTA_MATCH_MAX
,
621 .owner
= THIS_MODULE
,
624 static LIST_HEAD(nft_target_list
);
626 static struct nft_expr_type nft_target_type
;
628 static const struct nft_expr_ops
*
629 nft_target_select_ops(const struct nft_ctx
*ctx
,
630 const struct nlattr
* const tb
[])
632 struct nft_xt
*nft_target
;
633 struct xt_target
*target
;
637 if (tb
[NFTA_TARGET_NAME
] == NULL
||
638 tb
[NFTA_TARGET_REV
] == NULL
||
639 tb
[NFTA_TARGET_INFO
] == NULL
)
640 return ERR_PTR(-EINVAL
);
642 tg_name
= nla_data(tb
[NFTA_TARGET_NAME
]);
643 rev
= ntohl(nla_get_be32(tb
[NFTA_TARGET_REV
]));
644 family
= ctx
->afi
->family
;
646 /* Re-use the existing target if it's already loaded. */
647 list_for_each_entry(nft_target
, &nft_target_list
, head
) {
648 struct xt_target
*target
= nft_target
->ops
.data
;
650 if (strcmp(target
->name
, tg_name
) == 0 &&
651 target
->revision
== rev
&& target
->family
== family
)
652 return &nft_target
->ops
;
655 target
= xt_request_find_target(family
, tg_name
, rev
);
657 return ERR_PTR(-ENOENT
);
659 /* This is the first time we use this target, allocate operations */
660 nft_target
= kzalloc(sizeof(struct nft_xt
), GFP_KERNEL
);
661 if (nft_target
== NULL
)
662 return ERR_PTR(-ENOMEM
);
664 nft_target
->ops
.type
= &nft_target_type
;
665 nft_target
->ops
.size
= NFT_EXPR_SIZE(XT_ALIGN(target
->targetsize
));
666 nft_target
->ops
.eval
= nft_target_eval
;
667 nft_target
->ops
.init
= nft_target_init
;
668 nft_target
->ops
.destroy
= nft_target_destroy
;
669 nft_target
->ops
.dump
= nft_target_dump
;
670 nft_target
->ops
.validate
= nft_target_validate
;
671 nft_target
->ops
.data
= target
;
673 list_add(&nft_target
->head
, &nft_target_list
);
675 return &nft_target
->ops
;
678 static void nft_target_release(void)
680 struct nft_xt
*nft_target
, *tmp
;
682 list_for_each_entry_safe(nft_target
, tmp
, &nft_target_list
, head
)
686 static struct nft_expr_type nft_target_type __read_mostly
= {
688 .select_ops
= nft_target_select_ops
,
689 .policy
= nft_target_policy
,
690 .maxattr
= NFTA_TARGET_MAX
,
691 .owner
= THIS_MODULE
,
694 static int __init
nft_compat_module_init(void)
698 ret
= nft_register_expr(&nft_match_type
);
702 ret
= nft_register_expr(&nft_target_type
);
706 ret
= nfnetlink_subsys_register(&nfnl_compat_subsys
);
708 pr_err("nft_compat: cannot register with nfnetlink.\n");
712 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
717 nft_unregister_expr(&nft_target_type
);
719 nft_unregister_expr(&nft_match_type
);
723 static void __exit
nft_compat_module_exit(void)
725 nfnetlink_subsys_unregister(&nfnl_compat_subsys
);
726 nft_unregister_expr(&nft_target_type
);
727 nft_unregister_expr(&nft_match_type
);
729 nft_target_release();
732 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT
);
734 module_init(nft_compat_module_init
);
735 module_exit(nft_compat_module_exit
);
737 MODULE_LICENSE("GPL");
738 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
739 MODULE_ALIAS_NFT_EXPR("match");
740 MODULE_ALIAS_NFT_EXPR("target");