1 // SPDX-License-Identifier: GPL-2.0-only
3 * Xtables module for matching the value of the IPv4/IPv6 and TCP ECN bits
5 * (C) 2002 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2011 Patrick McHardy <kaber@trash.net>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/tcp.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_ecn.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv6/ip6_tables.h>
21 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
22 MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag match");
23 MODULE_LICENSE("GPL");
24 MODULE_ALIAS("ipt_ecn");
25 MODULE_ALIAS("ip6t_ecn");
27 static bool match_tcp(const struct sk_buff
*skb
, struct xt_action_param
*par
)
29 const struct xt_ecn_info
*einfo
= par
->matchinfo
;
31 const struct tcphdr
*th
;
33 /* In practice, TCP match does this, so can't fail. But let's
36 th
= skb_header_pointer(skb
, par
->thoff
, sizeof(_tcph
), &_tcph
);
40 if (einfo
->operation
& XT_ECN_OP_MATCH_ECE
) {
41 if (einfo
->invert
& XT_ECN_OP_MATCH_ECE
) {
50 if (einfo
->operation
& XT_ECN_OP_MATCH_CWR
) {
51 if (einfo
->invert
& XT_ECN_OP_MATCH_CWR
) {
63 static inline bool match_ip(const struct sk_buff
*skb
,
64 const struct xt_ecn_info
*einfo
)
66 return ((ip_hdr(skb
)->tos
& XT_ECN_IP_MASK
) == einfo
->ip_ect
) ^
67 !!(einfo
->invert
& XT_ECN_OP_MATCH_IP
);
70 static bool ecn_mt4(const struct sk_buff
*skb
, struct xt_action_param
*par
)
72 const struct xt_ecn_info
*info
= par
->matchinfo
;
74 if (info
->operation
& XT_ECN_OP_MATCH_IP
&& !match_ip(skb
, info
))
77 if (info
->operation
& (XT_ECN_OP_MATCH_ECE
| XT_ECN_OP_MATCH_CWR
) &&
84 static int ecn_mt_check4(const struct xt_mtchk_param
*par
)
86 const struct xt_ecn_info
*info
= par
->matchinfo
;
87 const struct ipt_ip
*ip
= par
->entryinfo
;
89 if (info
->operation
& XT_ECN_OP_MATCH_MASK
)
92 if (info
->invert
& XT_ECN_OP_MATCH_MASK
)
95 if (info
->operation
& (XT_ECN_OP_MATCH_ECE
| XT_ECN_OP_MATCH_CWR
) &&
96 (ip
->proto
!= IPPROTO_TCP
|| ip
->invflags
& IPT_INV_PROTO
)) {
97 pr_info_ratelimited("cannot match TCP bits for non-tcp packets\n");
104 static inline bool match_ipv6(const struct sk_buff
*skb
,
105 const struct xt_ecn_info
*einfo
)
107 return (((ipv6_hdr(skb
)->flow_lbl
[0] >> 4) & XT_ECN_IP_MASK
) ==
109 !!(einfo
->invert
& XT_ECN_OP_MATCH_IP
);
112 static bool ecn_mt6(const struct sk_buff
*skb
, struct xt_action_param
*par
)
114 const struct xt_ecn_info
*info
= par
->matchinfo
;
116 if (info
->operation
& XT_ECN_OP_MATCH_IP
&& !match_ipv6(skb
, info
))
119 if (info
->operation
& (XT_ECN_OP_MATCH_ECE
| XT_ECN_OP_MATCH_CWR
) &&
120 !match_tcp(skb
, par
))
126 static int ecn_mt_check6(const struct xt_mtchk_param
*par
)
128 const struct xt_ecn_info
*info
= par
->matchinfo
;
129 const struct ip6t_ip6
*ip
= par
->entryinfo
;
131 if (info
->operation
& XT_ECN_OP_MATCH_MASK
)
134 if (info
->invert
& XT_ECN_OP_MATCH_MASK
)
137 if (info
->operation
& (XT_ECN_OP_MATCH_ECE
| XT_ECN_OP_MATCH_CWR
) &&
138 (ip
->proto
!= IPPROTO_TCP
|| ip
->invflags
& IP6T_INV_PROTO
)) {
139 pr_info_ratelimited("cannot match TCP bits for non-tcp packets\n");
146 static struct xt_match ecn_mt_reg
[] __read_mostly
= {
149 .family
= NFPROTO_IPV4
,
151 .matchsize
= sizeof(struct xt_ecn_info
),
152 .checkentry
= ecn_mt_check4
,
157 .family
= NFPROTO_IPV6
,
159 .matchsize
= sizeof(struct xt_ecn_info
),
160 .checkentry
= ecn_mt_check6
,
165 static int __init
ecn_mt_init(void)
167 return xt_register_matches(ecn_mt_reg
, ARRAY_SIZE(ecn_mt_reg
));
170 static void __exit
ecn_mt_exit(void)
172 xt_unregister_matches(ecn_mt_reg
, ARRAY_SIZE(ecn_mt_reg
));
175 module_init(ecn_mt_init
);
176 module_exit(ecn_mt_exit
);