1 // SPDX-License-Identifier: GPL-2.0-only
3 * xt_ipvs - kernel module to match IPVS connection properties
5 * Author: Hannes Eder <heder@google.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/spinlock.h>
13 #include <linux/skbuff.h>
14 #ifdef CONFIG_IP_VS_IPV6
17 #include <linux/ip_vs.h>
18 #include <linux/types.h>
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter/xt_ipvs.h>
21 #include <net/netfilter/nf_conntrack.h>
23 #include <net/ip_vs.h>
25 MODULE_AUTHOR("Hannes Eder <heder@google.com>");
26 MODULE_DESCRIPTION("Xtables: match IPVS connection properties");
27 MODULE_LICENSE("GPL");
28 MODULE_ALIAS("ipt_ipvs");
29 MODULE_ALIAS("ip6t_ipvs");
31 /* borrowed from xt_conntrack */
32 static bool ipvs_mt_addrcmp(const union nf_inet_addr
*kaddr
,
33 const union nf_inet_addr
*uaddr
,
34 const union nf_inet_addr
*umask
,
37 if (l3proto
== NFPROTO_IPV4
)
38 return ((kaddr
->ip
^ uaddr
->ip
) & umask
->ip
) == 0;
39 #ifdef CONFIG_IP_VS_IPV6
40 else if (l3proto
== NFPROTO_IPV6
)
41 return ipv6_masked_addr_cmp(&kaddr
->in6
, &umask
->in6
,
49 ipvs_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
51 const struct xt_ipvs_mtinfo
*data
= par
->matchinfo
;
52 struct netns_ipvs
*ipvs
= net_ipvs(xt_net(par
));
53 /* ipvs_mt_check ensures that family is only NFPROTO_IPV[46]. */
54 const u_int8_t family
= xt_family(par
);
55 struct ip_vs_iphdr iph
;
56 struct ip_vs_protocol
*pp
;
57 struct ip_vs_conn
*cp
;
60 if (data
->bitmask
== XT_IPVS_IPVS_PROPERTY
) {
61 match
= skb
->ipvs_property
^
62 !!(data
->invert
& XT_IPVS_IPVS_PROPERTY
);
66 /* other flags than XT_IPVS_IPVS_PROPERTY are set */
67 if (!skb
->ipvs_property
) {
72 ip_vs_fill_iph_skb(family
, skb
, true, &iph
);
74 if (data
->bitmask
& XT_IPVS_PROTO
)
75 if ((iph
.protocol
== data
->l4proto
) ^
76 !(data
->invert
& XT_IPVS_PROTO
)) {
81 pp
= ip_vs_proto_get(iph
.protocol
);
88 * Check if the packet belongs to an existing entry
90 cp
= pp
->conn_out_get(ipvs
, family
, skb
, &iph
);
91 if (unlikely(cp
== NULL
)) {
97 * We found a connection, i.e. ct != 0, make sure to call
98 * __ip_vs_conn_put before returning. In our case jump to out_put_con.
101 if (data
->bitmask
& XT_IPVS_VPORT
)
102 if ((cp
->vport
== data
->vport
) ^
103 !(data
->invert
& XT_IPVS_VPORT
)) {
108 if (data
->bitmask
& XT_IPVS_VPORTCTL
)
109 if ((cp
->control
!= NULL
&&
110 cp
->control
->vport
== data
->vportctl
) ^
111 !(data
->invert
& XT_IPVS_VPORTCTL
)) {
116 if (data
->bitmask
& XT_IPVS_DIR
) {
117 enum ip_conntrack_info ctinfo
;
118 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
125 if ((ctinfo
>= IP_CT_IS_REPLY
) ^
126 !!(data
->invert
& XT_IPVS_DIR
)) {
132 if (data
->bitmask
& XT_IPVS_METHOD
)
133 if (((cp
->flags
& IP_VS_CONN_F_FWD_MASK
) == data
->fwd_method
) ^
134 !(data
->invert
& XT_IPVS_METHOD
)) {
139 if (data
->bitmask
& XT_IPVS_VADDR
) {
140 if (ipvs_mt_addrcmp(&cp
->vaddr
, &data
->vaddr
,
141 &data
->vmask
, family
) ^
142 !(data
->invert
& XT_IPVS_VADDR
)) {
149 __ip_vs_conn_put(cp
);
151 pr_debug("match=%d\n", match
);
155 static int ipvs_mt_check(const struct xt_mtchk_param
*par
)
157 if (par
->family
!= NFPROTO_IPV4
158 #ifdef CONFIG_IP_VS_IPV6
159 && par
->family
!= NFPROTO_IPV6
162 pr_info_ratelimited("protocol family %u not supported\n",
170 static struct xt_match xt_ipvs_mt_reg __read_mostly
= {
173 .family
= NFPROTO_UNSPEC
,
175 .checkentry
= ipvs_mt_check
,
176 .matchsize
= XT_ALIGN(sizeof(struct xt_ipvs_mtinfo
)),
180 static int __init
ipvs_mt_init(void)
182 return xt_register_match(&xt_ipvs_mt_reg
);
185 static void __exit
ipvs_mt_exit(void)
187 xt_unregister_match(&xt_ipvs_mt_reg
);
190 module_init(ipvs_mt_init
);
191 module_exit(ipvs_mt_exit
);