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 <linux/netfilter_bridge/ebtables.h>
23 #include <linux/netfilter_arp/arp_tables.h>
24 #include <net/netfilter/nf_tables.h>
27 struct list_head head
;
28 struct nft_expr_ops ops
;
31 /* Unlike other expressions, ops doesn't have static storage duration.
32 * nft core assumes they do. We use kfree_rcu so that nft core can
33 * can check expr->ops->size even after nft_compat->destroy() frees
34 * the nft_xt struct that holds the ops structure.
36 struct rcu_head rcu_head
;
39 /* Used for matches where *info is larger than X byte */
40 #define NFT_MATCH_LARGE_THRESH 192
42 struct nft_xt_match_priv
{
46 static bool nft_xt_put(struct nft_xt
*xt
)
48 if (--xt
->refcnt
== 0) {
50 kfree_rcu(xt
, rcu_head
);
57 static int nft_compat_chain_validate_dependency(const char *tablename
,
58 const struct nft_chain
*chain
)
60 const struct nft_base_chain
*basechain
;
63 !nft_is_base_chain(chain
))
66 basechain
= nft_base_chain(chain
);
67 if (strcmp(tablename
, "nat") == 0 &&
68 basechain
->type
->type
!= NFT_CHAIN_T_NAT
)
78 struct arpt_entry arp
;
82 nft_compat_set_par(struct xt_action_param
*par
, void *xt
, const void *xt_info
)
85 par
->targinfo
= xt_info
;
89 static void nft_target_eval_xt(const struct nft_expr
*expr
,
90 struct nft_regs
*regs
,
91 const struct nft_pktinfo
*pkt
)
93 void *info
= nft_expr_priv(expr
);
94 struct xt_target
*target
= expr
->ops
->data
;
95 struct sk_buff
*skb
= pkt
->skb
;
98 nft_compat_set_par((struct xt_action_param
*)&pkt
->xt
, target
, info
);
100 ret
= target
->target(skb
, &pkt
->xt
);
107 regs
->verdict
.code
= NFT_CONTINUE
;
110 regs
->verdict
.code
= ret
;
115 static void nft_target_eval_bridge(const struct nft_expr
*expr
,
116 struct nft_regs
*regs
,
117 const struct nft_pktinfo
*pkt
)
119 void *info
= nft_expr_priv(expr
);
120 struct xt_target
*target
= expr
->ops
->data
;
121 struct sk_buff
*skb
= pkt
->skb
;
124 nft_compat_set_par((struct xt_action_param
*)&pkt
->xt
, target
, info
);
126 ret
= target
->target(skb
, &pkt
->xt
);
133 regs
->verdict
.code
= NF_ACCEPT
;
136 regs
->verdict
.code
= NF_DROP
;
139 regs
->verdict
.code
= NFT_CONTINUE
;
142 regs
->verdict
.code
= NFT_RETURN
;
145 regs
->verdict
.code
= ret
;
150 static const struct nla_policy nft_target_policy
[NFTA_TARGET_MAX
+ 1] = {
151 [NFTA_TARGET_NAME
] = { .type
= NLA_NUL_STRING
},
152 [NFTA_TARGET_REV
] = { .type
= NLA_U32
},
153 [NFTA_TARGET_INFO
] = { .type
= NLA_BINARY
},
157 nft_target_set_tgchk_param(struct xt_tgchk_param
*par
,
158 const struct nft_ctx
*ctx
,
159 struct xt_target
*target
, void *info
,
160 union nft_entry
*entry
, u16 proto
, bool inv
)
163 par
->table
= ctx
->table
->name
;
164 switch (ctx
->family
) {
166 entry
->e4
.ip
.proto
= proto
;
167 entry
->e4
.ip
.invflags
= inv
? IPT_INV_PROTO
: 0;
171 entry
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
173 entry
->e6
.ipv6
.proto
= proto
;
174 entry
->e6
.ipv6
.invflags
= inv
? IP6T_INV_PROTO
: 0;
177 entry
->ebt
.ethproto
= (__force __be16
)proto
;
178 entry
->ebt
.invflags
= inv
? EBT_IPROTO
: 0;
183 par
->entryinfo
= entry
;
184 par
->target
= target
;
185 par
->targinfo
= info
;
186 if (nft_is_base_chain(ctx
->chain
)) {
187 const struct nft_base_chain
*basechain
=
188 nft_base_chain(ctx
->chain
);
189 const struct nf_hook_ops
*ops
= &basechain
->ops
;
191 par
->hook_mask
= 1 << ops
->hooknum
;
195 par
->family
= ctx
->family
;
196 par
->nft_compat
= true;
199 static void target_compat_from_user(struct xt_target
*t
, void *in
, void *out
)
203 memcpy(out
, in
, t
->targetsize
);
204 pad
= XT_ALIGN(t
->targetsize
) - t
->targetsize
;
206 memset(out
+ t
->targetsize
, 0, pad
);
209 static const struct nla_policy nft_rule_compat_policy
[NFTA_RULE_COMPAT_MAX
+ 1] = {
210 [NFTA_RULE_COMPAT_PROTO
] = { .type
= NLA_U32
},
211 [NFTA_RULE_COMPAT_FLAGS
] = { .type
= NLA_U32
},
214 static int nft_parse_compat(const struct nlattr
*attr
, u16
*proto
, bool *inv
)
216 struct nlattr
*tb
[NFTA_RULE_COMPAT_MAX
+1];
220 err
= nla_parse_nested(tb
, NFTA_RULE_COMPAT_MAX
, attr
,
221 nft_rule_compat_policy
, NULL
);
225 if (!tb
[NFTA_RULE_COMPAT_PROTO
] || !tb
[NFTA_RULE_COMPAT_FLAGS
])
228 flags
= ntohl(nla_get_be32(tb
[NFTA_RULE_COMPAT_FLAGS
]));
229 if (flags
& ~NFT_RULE_COMPAT_F_MASK
)
231 if (flags
& NFT_RULE_COMPAT_F_INV
)
234 *proto
= ntohl(nla_get_be32(tb
[NFTA_RULE_COMPAT_PROTO
]));
239 nft_target_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
240 const struct nlattr
* const tb
[])
242 void *info
= nft_expr_priv(expr
);
243 struct xt_target
*target
= expr
->ops
->data
;
244 struct xt_tgchk_param par
;
245 size_t size
= XT_ALIGN(nla_len(tb
[NFTA_TARGET_INFO
]));
246 struct nft_xt
*nft_xt
;
249 union nft_entry e
= {};
252 target_compat_from_user(target
, nla_data(tb
[NFTA_TARGET_INFO
]), info
);
254 if (ctx
->nla
[NFTA_RULE_COMPAT
]) {
255 ret
= nft_parse_compat(ctx
->nla
[NFTA_RULE_COMPAT
], &proto
, &inv
);
260 nft_target_set_tgchk_param(&par
, ctx
, target
, info
, &e
, proto
, inv
);
262 ret
= xt_check_target(&par
, size
, proto
, inv
);
266 /* The standard target cannot be used */
270 nft_xt
= container_of(expr
->ops
, struct nft_xt
, ops
);
276 nft_target_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
278 struct xt_target
*target
= expr
->ops
->data
;
279 void *info
= nft_expr_priv(expr
);
280 struct xt_tgdtor_param par
;
285 par
.family
= ctx
->family
;
286 if (par
.target
->destroy
!= NULL
)
287 par
.target
->destroy(&par
);
289 if (nft_xt_put(container_of(expr
->ops
, struct nft_xt
, ops
)))
290 module_put(target
->me
);
293 static int nft_extension_dump_info(struct sk_buff
*skb
, int attr
,
295 unsigned int size
, unsigned int user_size
)
297 unsigned int info_size
, aligned_size
= XT_ALIGN(size
);
300 nla
= nla_reserve(skb
, attr
, aligned_size
);
304 info_size
= user_size
? : size
;
305 memcpy(nla_data(nla
), info
, info_size
);
306 memset(nla_data(nla
) + info_size
, 0, aligned_size
- info_size
);
311 static int nft_target_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
313 const struct xt_target
*target
= expr
->ops
->data
;
314 void *info
= nft_expr_priv(expr
);
316 if (nla_put_string(skb
, NFTA_TARGET_NAME
, target
->name
) ||
317 nla_put_be32(skb
, NFTA_TARGET_REV
, htonl(target
->revision
)) ||
318 nft_extension_dump_info(skb
, NFTA_TARGET_INFO
, info
,
319 target
->targetsize
, target
->usersize
))
320 goto nla_put_failure
;
328 static int nft_target_validate(const struct nft_ctx
*ctx
,
329 const struct nft_expr
*expr
,
330 const struct nft_data
**data
)
332 struct xt_target
*target
= expr
->ops
->data
;
333 unsigned int hook_mask
= 0;
336 if (nft_is_base_chain(ctx
->chain
)) {
337 const struct nft_base_chain
*basechain
=
338 nft_base_chain(ctx
->chain
);
339 const struct nf_hook_ops
*ops
= &basechain
->ops
;
341 hook_mask
= 1 << ops
->hooknum
;
342 if (target
->hooks
&& !(hook_mask
& target
->hooks
))
345 ret
= nft_compat_chain_validate_dependency(target
->table
,
353 static void __nft_match_eval(const struct nft_expr
*expr
,
354 struct nft_regs
*regs
,
355 const struct nft_pktinfo
*pkt
,
358 struct xt_match
*match
= expr
->ops
->data
;
359 struct sk_buff
*skb
= pkt
->skb
;
362 nft_compat_set_par((struct xt_action_param
*)&pkt
->xt
, match
, info
);
364 ret
= match
->match(skb
, (struct xt_action_param
*)&pkt
->xt
);
366 if (pkt
->xt
.hotdrop
) {
367 regs
->verdict
.code
= NF_DROP
;
371 switch (ret
? 1 : 0) {
373 regs
->verdict
.code
= NFT_CONTINUE
;
376 regs
->verdict
.code
= NFT_BREAK
;
381 static void nft_match_large_eval(const struct nft_expr
*expr
,
382 struct nft_regs
*regs
,
383 const struct nft_pktinfo
*pkt
)
385 struct nft_xt_match_priv
*priv
= nft_expr_priv(expr
);
387 __nft_match_eval(expr
, regs
, pkt
, priv
->info
);
390 static void nft_match_eval(const struct nft_expr
*expr
,
391 struct nft_regs
*regs
,
392 const struct nft_pktinfo
*pkt
)
394 __nft_match_eval(expr
, regs
, pkt
, nft_expr_priv(expr
));
397 static const struct nla_policy nft_match_policy
[NFTA_MATCH_MAX
+ 1] = {
398 [NFTA_MATCH_NAME
] = { .type
= NLA_NUL_STRING
},
399 [NFTA_MATCH_REV
] = { .type
= NLA_U32
},
400 [NFTA_MATCH_INFO
] = { .type
= NLA_BINARY
},
403 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
405 nft_match_set_mtchk_param(struct xt_mtchk_param
*par
, const struct nft_ctx
*ctx
,
406 struct xt_match
*match
, void *info
,
407 union nft_entry
*entry
, u16 proto
, bool inv
)
410 par
->table
= ctx
->table
->name
;
411 switch (ctx
->family
) {
413 entry
->e4
.ip
.proto
= proto
;
414 entry
->e4
.ip
.invflags
= inv
? IPT_INV_PROTO
: 0;
418 entry
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
420 entry
->e6
.ipv6
.proto
= proto
;
421 entry
->e6
.ipv6
.invflags
= inv
? IP6T_INV_PROTO
: 0;
424 entry
->ebt
.ethproto
= (__force __be16
)proto
;
425 entry
->ebt
.invflags
= inv
? EBT_IPROTO
: 0;
430 par
->entryinfo
= entry
;
432 par
->matchinfo
= info
;
433 if (nft_is_base_chain(ctx
->chain
)) {
434 const struct nft_base_chain
*basechain
=
435 nft_base_chain(ctx
->chain
);
436 const struct nf_hook_ops
*ops
= &basechain
->ops
;
438 par
->hook_mask
= 1 << ops
->hooknum
;
442 par
->family
= ctx
->family
;
443 par
->nft_compat
= true;
446 static void match_compat_from_user(struct xt_match
*m
, void *in
, void *out
)
450 memcpy(out
, in
, m
->matchsize
);
451 pad
= XT_ALIGN(m
->matchsize
) - m
->matchsize
;
453 memset(out
+ m
->matchsize
, 0, pad
);
457 __nft_match_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
458 const struct nlattr
* const tb
[],
461 struct xt_match
*match
= expr
->ops
->data
;
462 struct xt_mtchk_param par
;
463 size_t size
= XT_ALIGN(nla_len(tb
[NFTA_MATCH_INFO
]));
464 struct nft_xt
*nft_xt
;
467 union nft_entry e
= {};
470 match_compat_from_user(match
, nla_data(tb
[NFTA_MATCH_INFO
]), info
);
472 if (ctx
->nla
[NFTA_RULE_COMPAT
]) {
473 ret
= nft_parse_compat(ctx
->nla
[NFTA_RULE_COMPAT
], &proto
, &inv
);
478 nft_match_set_mtchk_param(&par
, ctx
, match
, info
, &e
, proto
, inv
);
480 ret
= xt_check_match(&par
, size
, proto
, inv
);
484 nft_xt
= container_of(expr
->ops
, struct nft_xt
, ops
);
490 nft_match_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
491 const struct nlattr
* const tb
[])
493 return __nft_match_init(ctx
, expr
, tb
, nft_expr_priv(expr
));
497 nft_match_large_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
498 const struct nlattr
* const tb
[])
500 struct nft_xt_match_priv
*priv
= nft_expr_priv(expr
);
501 struct xt_match
*m
= expr
->ops
->data
;
504 priv
->info
= kmalloc(XT_ALIGN(m
->matchsize
), GFP_KERNEL
);
508 ret
= __nft_match_init(ctx
, expr
, tb
, priv
->info
);
515 __nft_match_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
518 struct xt_match
*match
= expr
->ops
->data
;
519 struct xt_mtdtor_param par
;
523 par
.matchinfo
= info
;
524 par
.family
= ctx
->family
;
525 if (par
.match
->destroy
!= NULL
)
526 par
.match
->destroy(&par
);
528 if (nft_xt_put(container_of(expr
->ops
, struct nft_xt
, ops
)))
529 module_put(match
->me
);
533 nft_match_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
535 __nft_match_destroy(ctx
, expr
, nft_expr_priv(expr
));
539 nft_match_large_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
541 struct nft_xt_match_priv
*priv
= nft_expr_priv(expr
);
543 __nft_match_destroy(ctx
, expr
, priv
->info
);
547 static int __nft_match_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
,
550 struct xt_match
*match
= expr
->ops
->data
;
552 if (nla_put_string(skb
, NFTA_MATCH_NAME
, match
->name
) ||
553 nla_put_be32(skb
, NFTA_MATCH_REV
, htonl(match
->revision
)) ||
554 nft_extension_dump_info(skb
, NFTA_MATCH_INFO
, info
,
555 match
->matchsize
, match
->usersize
))
556 goto nla_put_failure
;
564 static int nft_match_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
566 return __nft_match_dump(skb
, expr
, nft_expr_priv(expr
));
569 static int nft_match_large_dump(struct sk_buff
*skb
, const struct nft_expr
*e
)
571 struct nft_xt_match_priv
*priv
= nft_expr_priv(e
);
573 return __nft_match_dump(skb
, e
, priv
->info
);
576 static int nft_match_validate(const struct nft_ctx
*ctx
,
577 const struct nft_expr
*expr
,
578 const struct nft_data
**data
)
580 struct xt_match
*match
= expr
->ops
->data
;
581 unsigned int hook_mask
= 0;
584 if (nft_is_base_chain(ctx
->chain
)) {
585 const struct nft_base_chain
*basechain
=
586 nft_base_chain(ctx
->chain
);
587 const struct nf_hook_ops
*ops
= &basechain
->ops
;
589 hook_mask
= 1 << ops
->hooknum
;
590 if (match
->hooks
&& !(hook_mask
& match
->hooks
))
593 ret
= nft_compat_chain_validate_dependency(match
->table
,
602 nfnl_compat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
603 int event
, u16 family
, const char *name
,
606 struct nlmsghdr
*nlh
;
607 struct nfgenmsg
*nfmsg
;
608 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
610 event
= nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT
, event
);
611 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
615 nfmsg
= nlmsg_data(nlh
);
616 nfmsg
->nfgen_family
= family
;
617 nfmsg
->version
= NFNETLINK_V0
;
620 if (nla_put_string(skb
, NFTA_COMPAT_NAME
, name
) ||
621 nla_put_be32(skb
, NFTA_COMPAT_REV
, htonl(rev
)) ||
622 nla_put_be32(skb
, NFTA_COMPAT_TYPE
, htonl(target
)))
623 goto nla_put_failure
;
630 nlmsg_cancel(skb
, nlh
);
634 static int nfnl_compat_get_rcu(struct net
*net
, struct sock
*nfnl
,
635 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
636 const struct nlattr
* const tb
[],
637 struct netlink_ext_ack
*extack
)
640 struct nfgenmsg
*nfmsg
;
644 struct sk_buff
*skb2
;
646 if (tb
[NFTA_COMPAT_NAME
] == NULL
||
647 tb
[NFTA_COMPAT_REV
] == NULL
||
648 tb
[NFTA_COMPAT_TYPE
] == NULL
)
651 name
= nla_data(tb
[NFTA_COMPAT_NAME
]);
652 rev
= ntohl(nla_get_be32(tb
[NFTA_COMPAT_REV
]));
653 target
= ntohl(nla_get_be32(tb
[NFTA_COMPAT_TYPE
]));
655 nfmsg
= nlmsg_data(nlh
);
657 switch(nfmsg
->nfgen_family
) {
671 pr_err("nft_compat: unsupported protocol %d\n",
672 nfmsg
->nfgen_family
);
676 if (!try_module_get(THIS_MODULE
))
680 try_then_request_module(xt_find_revision(nfmsg
->nfgen_family
, name
,
686 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
692 /* include the best revision for this extension in the message */
693 if (nfnl_compat_fill_info(skb2
, NETLINK_CB(skb
).portid
,
695 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
698 name
, ret
, target
) <= 0) {
703 ret
= netlink_unicast(nfnl
, skb2
, NETLINK_CB(skb
).portid
,
709 module_put(THIS_MODULE
);
710 return ret
== -EAGAIN
? -ENOBUFS
: ret
;
713 static const struct nla_policy nfnl_compat_policy_get
[NFTA_COMPAT_MAX
+1] = {
714 [NFTA_COMPAT_NAME
] = { .type
= NLA_NUL_STRING
,
715 .len
= NFT_COMPAT_NAME_MAX
-1 },
716 [NFTA_COMPAT_REV
] = { .type
= NLA_U32
},
717 [NFTA_COMPAT_TYPE
] = { .type
= NLA_U32
},
720 static const struct nfnl_callback nfnl_nft_compat_cb
[NFNL_MSG_COMPAT_MAX
] = {
721 [NFNL_MSG_COMPAT_GET
] = { .call_rcu
= nfnl_compat_get_rcu
,
722 .attr_count
= NFTA_COMPAT_MAX
,
723 .policy
= nfnl_compat_policy_get
},
726 static const struct nfnetlink_subsystem nfnl_compat_subsys
= {
727 .name
= "nft-compat",
728 .subsys_id
= NFNL_SUBSYS_NFT_COMPAT
,
729 .cb_count
= NFNL_MSG_COMPAT_MAX
,
730 .cb
= nfnl_nft_compat_cb
,
733 static LIST_HEAD(nft_match_list
);
735 static struct nft_expr_type nft_match_type
;
737 static bool nft_match_cmp(const struct xt_match
*match
,
738 const char *name
, u32 rev
, u32 family
)
740 return strcmp(match
->name
, name
) == 0 && match
->revision
== rev
&&
741 (match
->family
== NFPROTO_UNSPEC
|| match
->family
== family
);
744 static const struct nft_expr_ops
*
745 nft_match_select_ops(const struct nft_ctx
*ctx
,
746 const struct nlattr
* const tb
[])
748 struct nft_xt
*nft_match
;
749 struct xt_match
*match
;
750 unsigned int matchsize
;
755 if (tb
[NFTA_MATCH_NAME
] == NULL
||
756 tb
[NFTA_MATCH_REV
] == NULL
||
757 tb
[NFTA_MATCH_INFO
] == NULL
)
758 return ERR_PTR(-EINVAL
);
760 mt_name
= nla_data(tb
[NFTA_MATCH_NAME
]);
761 rev
= ntohl(nla_get_be32(tb
[NFTA_MATCH_REV
]));
762 family
= ctx
->family
;
764 /* Re-use the existing match if it's already loaded. */
765 list_for_each_entry(nft_match
, &nft_match_list
, head
) {
766 struct xt_match
*match
= nft_match
->ops
.data
;
768 if (nft_match_cmp(match
, mt_name
, rev
, family
))
769 return &nft_match
->ops
;
772 match
= xt_request_find_match(family
, mt_name
, rev
);
774 return ERR_PTR(-ENOENT
);
776 if (match
->matchsize
> nla_len(tb
[NFTA_MATCH_INFO
])) {
781 /* This is the first time we use this match, allocate operations */
782 nft_match
= kzalloc(sizeof(struct nft_xt
), GFP_KERNEL
);
783 if (nft_match
== NULL
) {
788 nft_match
->refcnt
= 0;
789 nft_match
->ops
.type
= &nft_match_type
;
790 nft_match
->ops
.eval
= nft_match_eval
;
791 nft_match
->ops
.init
= nft_match_init
;
792 nft_match
->ops
.destroy
= nft_match_destroy
;
793 nft_match
->ops
.dump
= nft_match_dump
;
794 nft_match
->ops
.validate
= nft_match_validate
;
795 nft_match
->ops
.data
= match
;
797 matchsize
= NFT_EXPR_SIZE(XT_ALIGN(match
->matchsize
));
798 if (matchsize
> NFT_MATCH_LARGE_THRESH
) {
799 matchsize
= NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv
));
801 nft_match
->ops
.eval
= nft_match_large_eval
;
802 nft_match
->ops
.init
= nft_match_large_init
;
803 nft_match
->ops
.destroy
= nft_match_large_destroy
;
804 nft_match
->ops
.dump
= nft_match_large_dump
;
807 nft_match
->ops
.size
= matchsize
;
809 list_add(&nft_match
->head
, &nft_match_list
);
811 return &nft_match
->ops
;
813 module_put(match
->me
);
817 static struct nft_expr_type nft_match_type __read_mostly
= {
819 .select_ops
= nft_match_select_ops
,
820 .policy
= nft_match_policy
,
821 .maxattr
= NFTA_MATCH_MAX
,
822 .owner
= THIS_MODULE
,
825 static LIST_HEAD(nft_target_list
);
827 static struct nft_expr_type nft_target_type
;
829 static bool nft_target_cmp(const struct xt_target
*tg
,
830 const char *name
, u32 rev
, u32 family
)
832 return strcmp(tg
->name
, name
) == 0 && tg
->revision
== rev
&&
833 (tg
->family
== NFPROTO_UNSPEC
|| tg
->family
== family
);
836 static const struct nft_expr_ops
*
837 nft_target_select_ops(const struct nft_ctx
*ctx
,
838 const struct nlattr
* const tb
[])
840 struct nft_xt
*nft_target
;
841 struct xt_target
*target
;
846 if (tb
[NFTA_TARGET_NAME
] == NULL
||
847 tb
[NFTA_TARGET_REV
] == NULL
||
848 tb
[NFTA_TARGET_INFO
] == NULL
)
849 return ERR_PTR(-EINVAL
);
851 tg_name
= nla_data(tb
[NFTA_TARGET_NAME
]);
852 rev
= ntohl(nla_get_be32(tb
[NFTA_TARGET_REV
]));
853 family
= ctx
->family
;
855 if (strcmp(tg_name
, XT_ERROR_TARGET
) == 0 ||
856 strcmp(tg_name
, XT_STANDARD_TARGET
) == 0 ||
857 strcmp(tg_name
, "standard") == 0)
858 return ERR_PTR(-EINVAL
);
860 /* Re-use the existing target if it's already loaded. */
861 list_for_each_entry(nft_target
, &nft_target_list
, head
) {
862 struct xt_target
*target
= nft_target
->ops
.data
;
867 if (nft_target_cmp(target
, tg_name
, rev
, family
))
868 return &nft_target
->ops
;
871 target
= xt_request_find_target(family
, tg_name
, rev
);
873 return ERR_PTR(-ENOENT
);
875 if (!target
->target
) {
880 if (target
->targetsize
> nla_len(tb
[NFTA_TARGET_INFO
])) {
885 /* This is the first time we use this target, allocate operations */
886 nft_target
= kzalloc(sizeof(struct nft_xt
), GFP_KERNEL
);
887 if (nft_target
== NULL
) {
892 nft_target
->refcnt
= 0;
893 nft_target
->ops
.type
= &nft_target_type
;
894 nft_target
->ops
.size
= NFT_EXPR_SIZE(XT_ALIGN(target
->targetsize
));
895 nft_target
->ops
.init
= nft_target_init
;
896 nft_target
->ops
.destroy
= nft_target_destroy
;
897 nft_target
->ops
.dump
= nft_target_dump
;
898 nft_target
->ops
.validate
= nft_target_validate
;
899 nft_target
->ops
.data
= target
;
901 if (family
== NFPROTO_BRIDGE
)
902 nft_target
->ops
.eval
= nft_target_eval_bridge
;
904 nft_target
->ops
.eval
= nft_target_eval_xt
;
906 list_add(&nft_target
->head
, &nft_target_list
);
908 return &nft_target
->ops
;
910 module_put(target
->me
);
914 static struct nft_expr_type nft_target_type __read_mostly
= {
916 .select_ops
= nft_target_select_ops
,
917 .policy
= nft_target_policy
,
918 .maxattr
= NFTA_TARGET_MAX
,
919 .owner
= THIS_MODULE
,
922 static int __init
nft_compat_module_init(void)
926 ret
= nft_register_expr(&nft_match_type
);
930 ret
= nft_register_expr(&nft_target_type
);
934 ret
= nfnetlink_subsys_register(&nfnl_compat_subsys
);
936 pr_err("nft_compat: cannot register with nfnetlink.\n");
943 nft_unregister_expr(&nft_target_type
);
945 nft_unregister_expr(&nft_match_type
);
949 static void __exit
nft_compat_module_exit(void)
951 struct nft_xt
*xt
, *next
;
953 /* list should be empty here, it can be non-empty only in case there
954 * was an error that caused nft_xt expr to not be initialized fully
955 * and noone else requested the same expression later.
957 * In this case, the lists contain 0-refcount entries that still
958 * hold module reference.
960 list_for_each_entry_safe(xt
, next
, &nft_target_list
, head
) {
961 struct xt_target
*target
= xt
->ops
.data
;
963 if (WARN_ON_ONCE(xt
->refcnt
))
965 module_put(target
->me
);
969 list_for_each_entry_safe(xt
, next
, &nft_match_list
, head
) {
970 struct xt_match
*match
= xt
->ops
.data
;
972 if (WARN_ON_ONCE(xt
->refcnt
))
974 module_put(match
->me
);
977 nfnetlink_subsys_unregister(&nfnl_compat_subsys
);
978 nft_unregister_expr(&nft_target_type
);
979 nft_unregister_expr(&nft_match_type
);
982 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT
);
984 module_init(nft_compat_module_init
);
985 module_exit(nft_compat_module_exit
);
987 MODULE_LICENSE("GPL");
988 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
989 MODULE_ALIAS_NFT_EXPR("match");
990 MODULE_ALIAS_NFT_EXPR("target");