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
->afi
->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
[0];
191 par
->hook_mask
= 1 << ops
->hooknum
;
195 par
->family
= ctx
->afi
->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 module
*me
= target
->me
;
281 struct xt_tgdtor_param par
;
286 par
.family
= ctx
->afi
->family
;
287 if (par
.target
->destroy
!= NULL
)
288 par
.target
->destroy(&par
);
290 if (nft_xt_put(container_of(expr
->ops
, struct nft_xt
, ops
)))
294 static int nft_target_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
296 const struct xt_target
*target
= expr
->ops
->data
;
297 void *info
= nft_expr_priv(expr
);
299 if (nla_put_string(skb
, NFTA_TARGET_NAME
, target
->name
) ||
300 nla_put_be32(skb
, NFTA_TARGET_REV
, htonl(target
->revision
)) ||
301 nla_put(skb
, NFTA_TARGET_INFO
, XT_ALIGN(target
->targetsize
), info
))
302 goto nla_put_failure
;
310 static int nft_target_validate(const struct nft_ctx
*ctx
,
311 const struct nft_expr
*expr
,
312 const struct nft_data
**data
)
314 struct xt_target
*target
= expr
->ops
->data
;
315 unsigned int hook_mask
= 0;
318 if (nft_is_base_chain(ctx
->chain
)) {
319 const struct nft_base_chain
*basechain
=
320 nft_base_chain(ctx
->chain
);
321 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
323 hook_mask
= 1 << ops
->hooknum
;
324 if (target
->hooks
&& !(hook_mask
& target
->hooks
))
327 ret
= nft_compat_chain_validate_dependency(target
->table
,
335 static void __nft_match_eval(const struct nft_expr
*expr
,
336 struct nft_regs
*regs
,
337 const struct nft_pktinfo
*pkt
,
340 struct xt_match
*match
= expr
->ops
->data
;
341 struct sk_buff
*skb
= pkt
->skb
;
344 nft_compat_set_par((struct xt_action_param
*)&pkt
->xt
, match
, info
);
346 ret
= match
->match(skb
, (struct xt_action_param
*)&pkt
->xt
);
348 if (pkt
->xt
.hotdrop
) {
349 regs
->verdict
.code
= NF_DROP
;
353 switch (ret
? 1 : 0) {
355 regs
->verdict
.code
= NFT_CONTINUE
;
358 regs
->verdict
.code
= NFT_BREAK
;
363 static void nft_match_large_eval(const struct nft_expr
*expr
,
364 struct nft_regs
*regs
,
365 const struct nft_pktinfo
*pkt
)
367 struct nft_xt_match_priv
*priv
= nft_expr_priv(expr
);
369 __nft_match_eval(expr
, regs
, pkt
, priv
->info
);
372 static void nft_match_eval(const struct nft_expr
*expr
,
373 struct nft_regs
*regs
,
374 const struct nft_pktinfo
*pkt
)
376 __nft_match_eval(expr
, regs
, pkt
, nft_expr_priv(expr
));
379 static const struct nla_policy nft_match_policy
[NFTA_MATCH_MAX
+ 1] = {
380 [NFTA_MATCH_NAME
] = { .type
= NLA_NUL_STRING
},
381 [NFTA_MATCH_REV
] = { .type
= NLA_U32
},
382 [NFTA_MATCH_INFO
] = { .type
= NLA_BINARY
},
385 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
387 nft_match_set_mtchk_param(struct xt_mtchk_param
*par
, const struct nft_ctx
*ctx
,
388 struct xt_match
*match
, void *info
,
389 union nft_entry
*entry
, u16 proto
, bool inv
)
392 par
->table
= ctx
->table
->name
;
393 switch (ctx
->afi
->family
) {
395 entry
->e4
.ip
.proto
= proto
;
396 entry
->e4
.ip
.invflags
= inv
? IPT_INV_PROTO
: 0;
400 entry
->e6
.ipv6
.flags
|= IP6T_F_PROTO
;
402 entry
->e6
.ipv6
.proto
= proto
;
403 entry
->e6
.ipv6
.invflags
= inv
? IP6T_INV_PROTO
: 0;
406 entry
->ebt
.ethproto
= (__force __be16
)proto
;
407 entry
->ebt
.invflags
= inv
? EBT_IPROTO
: 0;
412 par
->entryinfo
= entry
;
414 par
->matchinfo
= info
;
415 if (nft_is_base_chain(ctx
->chain
)) {
416 const struct nft_base_chain
*basechain
=
417 nft_base_chain(ctx
->chain
);
418 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
420 par
->hook_mask
= 1 << ops
->hooknum
;
424 par
->family
= ctx
->afi
->family
;
425 par
->nft_compat
= true;
428 static void match_compat_from_user(struct xt_match
*m
, void *in
, void *out
)
432 memcpy(out
, in
, m
->matchsize
);
433 pad
= XT_ALIGN(m
->matchsize
) - m
->matchsize
;
435 memset(out
+ m
->matchsize
, 0, pad
);
439 __nft_match_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
440 const struct nlattr
* const tb
[],
443 struct xt_match
*match
= expr
->ops
->data
;
444 struct xt_mtchk_param par
;
445 size_t size
= XT_ALIGN(nla_len(tb
[NFTA_MATCH_INFO
]));
446 struct nft_xt
*nft_xt
;
449 union nft_entry e
= {};
452 match_compat_from_user(match
, nla_data(tb
[NFTA_MATCH_INFO
]), info
);
454 if (ctx
->nla
[NFTA_RULE_COMPAT
]) {
455 ret
= nft_parse_compat(ctx
->nla
[NFTA_RULE_COMPAT
], &proto
, &inv
);
460 nft_match_set_mtchk_param(&par
, ctx
, match
, info
, &e
, proto
, inv
);
462 ret
= xt_check_match(&par
, size
, proto
, inv
);
466 nft_xt
= container_of(expr
->ops
, struct nft_xt
, ops
);
472 nft_match_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
473 const struct nlattr
* const tb
[])
475 return __nft_match_init(ctx
, expr
, tb
, nft_expr_priv(expr
));
479 nft_match_large_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
480 const struct nlattr
* const tb
[])
482 struct nft_xt_match_priv
*priv
= nft_expr_priv(expr
);
483 struct xt_match
*m
= expr
->ops
->data
;
486 priv
->info
= kmalloc(XT_ALIGN(m
->matchsize
), GFP_KERNEL
);
490 ret
= __nft_match_init(ctx
, expr
, tb
, priv
->info
);
497 __nft_match_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
500 struct xt_match
*match
= expr
->ops
->data
;
501 struct module
*me
= match
->me
;
502 struct xt_mtdtor_param par
;
506 par
.matchinfo
= info
;
507 par
.family
= ctx
->afi
->family
;
508 if (par
.match
->destroy
!= NULL
)
509 par
.match
->destroy(&par
);
511 if (nft_xt_put(container_of(expr
->ops
, struct nft_xt
, ops
)))
516 nft_match_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
518 __nft_match_destroy(ctx
, expr
, nft_expr_priv(expr
));
522 nft_match_large_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
524 struct nft_xt_match_priv
*priv
= nft_expr_priv(expr
);
526 __nft_match_destroy(ctx
, expr
, priv
->info
);
530 static int __nft_match_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
,
533 struct xt_match
*match
= expr
->ops
->data
;
535 if (nla_put_string(skb
, NFTA_MATCH_NAME
, match
->name
) ||
536 nla_put_be32(skb
, NFTA_MATCH_REV
, htonl(match
->revision
)) ||
537 nla_put(skb
, NFTA_MATCH_INFO
, XT_ALIGN(match
->matchsize
), info
))
538 goto nla_put_failure
;
546 static int nft_match_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
548 return __nft_match_dump(skb
, expr
, nft_expr_priv(expr
));
551 static int nft_match_large_dump(struct sk_buff
*skb
, const struct nft_expr
*e
)
553 struct nft_xt_match_priv
*priv
= nft_expr_priv(e
);
555 return __nft_match_dump(skb
, e
, priv
->info
);
558 static int nft_match_validate(const struct nft_ctx
*ctx
,
559 const struct nft_expr
*expr
,
560 const struct nft_data
**data
)
562 struct xt_match
*match
= expr
->ops
->data
;
563 unsigned int hook_mask
= 0;
566 if (nft_is_base_chain(ctx
->chain
)) {
567 const struct nft_base_chain
*basechain
=
568 nft_base_chain(ctx
->chain
);
569 const struct nf_hook_ops
*ops
= &basechain
->ops
[0];
571 hook_mask
= 1 << ops
->hooknum
;
572 if (match
->hooks
&& !(hook_mask
& match
->hooks
))
575 ret
= nft_compat_chain_validate_dependency(match
->table
,
584 nfnl_compat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
585 int event
, u16 family
, const char *name
,
588 struct nlmsghdr
*nlh
;
589 struct nfgenmsg
*nfmsg
;
590 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
592 event
= nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT
, event
);
593 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
597 nfmsg
= nlmsg_data(nlh
);
598 nfmsg
->nfgen_family
= family
;
599 nfmsg
->version
= NFNETLINK_V0
;
602 if (nla_put_string(skb
, NFTA_COMPAT_NAME
, name
) ||
603 nla_put_be32(skb
, NFTA_COMPAT_REV
, htonl(rev
)) ||
604 nla_put_be32(skb
, NFTA_COMPAT_TYPE
, htonl(target
)))
605 goto nla_put_failure
;
612 nlmsg_cancel(skb
, nlh
);
616 static int nfnl_compat_get(struct net
*net
, struct sock
*nfnl
,
617 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
618 const struct nlattr
* const tb
[],
619 struct netlink_ext_ack
*extack
)
622 struct nfgenmsg
*nfmsg
;
626 struct sk_buff
*skb2
;
628 if (tb
[NFTA_COMPAT_NAME
] == NULL
||
629 tb
[NFTA_COMPAT_REV
] == NULL
||
630 tb
[NFTA_COMPAT_TYPE
] == NULL
)
633 name
= nla_data(tb
[NFTA_COMPAT_NAME
]);
634 rev
= ntohl(nla_get_be32(tb
[NFTA_COMPAT_REV
]));
635 target
= ntohl(nla_get_be32(tb
[NFTA_COMPAT_TYPE
]));
637 nfmsg
= nlmsg_data(nlh
);
639 switch(nfmsg
->nfgen_family
) {
653 pr_err("nft_compat: unsupported protocol %d\n",
654 nfmsg
->nfgen_family
);
658 try_then_request_module(xt_find_revision(nfmsg
->nfgen_family
, name
,
665 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
669 /* include the best revision for this extension in the message */
670 if (nfnl_compat_fill_info(skb2
, NETLINK_CB(skb
).portid
,
672 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
675 name
, ret
, target
) <= 0) {
680 ret
= netlink_unicast(nfnl
, skb2
, NETLINK_CB(skb
).portid
,
685 return ret
== -EAGAIN
? -ENOBUFS
: ret
;
688 static const struct nla_policy nfnl_compat_policy_get
[NFTA_COMPAT_MAX
+1] = {
689 [NFTA_COMPAT_NAME
] = { .type
= NLA_NUL_STRING
,
690 .len
= NFT_COMPAT_NAME_MAX
-1 },
691 [NFTA_COMPAT_REV
] = { .type
= NLA_U32
},
692 [NFTA_COMPAT_TYPE
] = { .type
= NLA_U32
},
695 static const struct nfnl_callback nfnl_nft_compat_cb
[NFNL_MSG_COMPAT_MAX
] = {
696 [NFNL_MSG_COMPAT_GET
] = { .call
= nfnl_compat_get
,
697 .attr_count
= NFTA_COMPAT_MAX
,
698 .policy
= nfnl_compat_policy_get
},
701 static const struct nfnetlink_subsystem nfnl_compat_subsys
= {
702 .name
= "nft-compat",
703 .subsys_id
= NFNL_SUBSYS_NFT_COMPAT
,
704 .cb_count
= NFNL_MSG_COMPAT_MAX
,
705 .cb
= nfnl_nft_compat_cb
,
708 static LIST_HEAD(nft_match_list
);
710 static struct nft_expr_type nft_match_type
;
712 static bool nft_match_cmp(const struct xt_match
*match
,
713 const char *name
, u32 rev
, u32 family
)
715 return strcmp(match
->name
, name
) == 0 && match
->revision
== rev
&&
716 (match
->family
== NFPROTO_UNSPEC
|| match
->family
== family
);
719 static const struct nft_expr_ops
*
720 nft_match_select_ops(const struct nft_ctx
*ctx
,
721 const struct nlattr
* const tb
[])
723 struct nft_xt
*nft_match
;
724 struct xt_match
*match
;
725 unsigned int matchsize
;
730 if (tb
[NFTA_MATCH_NAME
] == NULL
||
731 tb
[NFTA_MATCH_REV
] == NULL
||
732 tb
[NFTA_MATCH_INFO
] == NULL
)
733 return ERR_PTR(-EINVAL
);
735 mt_name
= nla_data(tb
[NFTA_MATCH_NAME
]);
736 rev
= ntohl(nla_get_be32(tb
[NFTA_MATCH_REV
]));
737 family
= ctx
->afi
->family
;
739 /* Re-use the existing match if it's already loaded. */
740 list_for_each_entry(nft_match
, &nft_match_list
, head
) {
741 struct xt_match
*match
= nft_match
->ops
.data
;
743 if (nft_match_cmp(match
, mt_name
, rev
, family
))
744 return &nft_match
->ops
;
747 match
= xt_request_find_match(family
, mt_name
, rev
);
749 return ERR_PTR(-ENOENT
);
751 if (match
->matchsize
> nla_len(tb
[NFTA_MATCH_INFO
])) {
756 /* This is the first time we use this match, allocate operations */
757 nft_match
= kzalloc(sizeof(struct nft_xt
), GFP_KERNEL
);
758 if (nft_match
== NULL
) {
763 nft_match
->refcnt
= 0;
764 nft_match
->ops
.type
= &nft_match_type
;
765 nft_match
->ops
.eval
= nft_match_eval
;
766 nft_match
->ops
.init
= nft_match_init
;
767 nft_match
->ops
.destroy
= nft_match_destroy
;
768 nft_match
->ops
.dump
= nft_match_dump
;
769 nft_match
->ops
.validate
= nft_match_validate
;
770 nft_match
->ops
.data
= match
;
772 matchsize
= NFT_EXPR_SIZE(XT_ALIGN(match
->matchsize
));
773 if (matchsize
> NFT_MATCH_LARGE_THRESH
) {
774 matchsize
= NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv
));
776 nft_match
->ops
.eval
= nft_match_large_eval
;
777 nft_match
->ops
.init
= nft_match_large_init
;
778 nft_match
->ops
.destroy
= nft_match_large_destroy
;
779 nft_match
->ops
.dump
= nft_match_large_dump
;
782 nft_match
->ops
.size
= matchsize
;
784 list_add(&nft_match
->head
, &nft_match_list
);
786 return &nft_match
->ops
;
788 module_put(match
->me
);
792 static struct nft_expr_type nft_match_type __read_mostly
= {
794 .select_ops
= nft_match_select_ops
,
795 .policy
= nft_match_policy
,
796 .maxattr
= NFTA_MATCH_MAX
,
797 .owner
= THIS_MODULE
,
800 static LIST_HEAD(nft_target_list
);
802 static struct nft_expr_type nft_target_type
;
804 static bool nft_target_cmp(const struct xt_target
*tg
,
805 const char *name
, u32 rev
, u32 family
)
807 return strcmp(tg
->name
, name
) == 0 && tg
->revision
== rev
&&
808 (tg
->family
== NFPROTO_UNSPEC
|| tg
->family
== family
);
811 static const struct nft_expr_ops
*
812 nft_target_select_ops(const struct nft_ctx
*ctx
,
813 const struct nlattr
* const tb
[])
815 struct nft_xt
*nft_target
;
816 struct xt_target
*target
;
821 if (tb
[NFTA_TARGET_NAME
] == NULL
||
822 tb
[NFTA_TARGET_REV
] == NULL
||
823 tb
[NFTA_TARGET_INFO
] == NULL
)
824 return ERR_PTR(-EINVAL
);
826 tg_name
= nla_data(tb
[NFTA_TARGET_NAME
]);
827 rev
= ntohl(nla_get_be32(tb
[NFTA_TARGET_REV
]));
828 family
= ctx
->afi
->family
;
830 if (strcmp(tg_name
, XT_ERROR_TARGET
) == 0 ||
831 strcmp(tg_name
, XT_STANDARD_TARGET
) == 0 ||
832 strcmp(tg_name
, "standard") == 0)
833 return ERR_PTR(-EINVAL
);
835 /* Re-use the existing target if it's already loaded. */
836 list_for_each_entry(nft_target
, &nft_target_list
, head
) {
837 struct xt_target
*target
= nft_target
->ops
.data
;
842 if (nft_target_cmp(target
, tg_name
, rev
, family
))
843 return &nft_target
->ops
;
846 target
= xt_request_find_target(family
, tg_name
, rev
);
848 return ERR_PTR(-ENOENT
);
850 if (!target
->target
) {
855 if (target
->targetsize
> nla_len(tb
[NFTA_TARGET_INFO
])) {
860 /* This is the first time we use this target, allocate operations */
861 nft_target
= kzalloc(sizeof(struct nft_xt
), GFP_KERNEL
);
862 if (nft_target
== NULL
) {
867 nft_target
->refcnt
= 0;
868 nft_target
->ops
.type
= &nft_target_type
;
869 nft_target
->ops
.size
= NFT_EXPR_SIZE(XT_ALIGN(target
->targetsize
));
870 nft_target
->ops
.init
= nft_target_init
;
871 nft_target
->ops
.destroy
= nft_target_destroy
;
872 nft_target
->ops
.dump
= nft_target_dump
;
873 nft_target
->ops
.validate
= nft_target_validate
;
874 nft_target
->ops
.data
= target
;
876 if (family
== NFPROTO_BRIDGE
)
877 nft_target
->ops
.eval
= nft_target_eval_bridge
;
879 nft_target
->ops
.eval
= nft_target_eval_xt
;
881 list_add(&nft_target
->head
, &nft_target_list
);
883 return &nft_target
->ops
;
885 module_put(target
->me
);
889 static struct nft_expr_type nft_target_type __read_mostly
= {
891 .select_ops
= nft_target_select_ops
,
892 .policy
= nft_target_policy
,
893 .maxattr
= NFTA_TARGET_MAX
,
894 .owner
= THIS_MODULE
,
897 static int __init
nft_compat_module_init(void)
901 ret
= nft_register_expr(&nft_match_type
);
905 ret
= nft_register_expr(&nft_target_type
);
909 ret
= nfnetlink_subsys_register(&nfnl_compat_subsys
);
911 pr_err("nft_compat: cannot register with nfnetlink.\n");
915 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
920 nft_unregister_expr(&nft_target_type
);
922 nft_unregister_expr(&nft_match_type
);
926 static void __exit
nft_compat_module_exit(void)
928 struct nft_xt
*xt
, *next
;
930 /* list should be empty here, it can be non-empty only in case there
931 * was an error that caused nft_xt expr to not be initialized fully
932 * and noone else requested the same expression later.
934 * In this case, the lists contain 0-refcount entries that still
935 * hold module reference.
937 list_for_each_entry_safe(xt
, next
, &nft_target_list
, head
) {
938 struct xt_target
*target
= xt
->ops
.data
;
940 if (WARN_ON_ONCE(xt
->refcnt
))
942 module_put(target
->me
);
946 list_for_each_entry_safe(xt
, next
, &nft_match_list
, head
) {
947 struct xt_match
*match
= xt
->ops
.data
;
949 if (WARN_ON_ONCE(xt
->refcnt
))
951 module_put(match
->me
);
954 nfnetlink_subsys_unregister(&nfnl_compat_subsys
);
955 nft_unregister_expr(&nft_target_type
);
956 nft_unregister_expr(&nft_match_type
);
959 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT
);
961 module_init(nft_compat_module_init
);
962 module_exit(nft_compat_module_exit
);
964 MODULE_LICENSE("GPL");
965 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
966 MODULE_ALIAS_NFT_EXPR("match");
967 MODULE_ALIAS_NFT_EXPR("target");