1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/types.h>
4 #include <linux/module.h>
6 #include <linux/ipv6.h>
10 #include <linux/netfilter/x_tables.h>
11 #include <linux/netfilter/xt_tcpudp.h>
12 #include <linux/netfilter_ipv4/ip_tables.h>
13 #include <linux/netfilter_ipv6/ip6_tables.h>
15 MODULE_DESCRIPTION("Xtables: TCP, UDP and UDP-Lite match");
16 MODULE_LICENSE("GPL");
17 MODULE_ALIAS("xt_tcp");
18 MODULE_ALIAS("xt_udp");
19 MODULE_ALIAS("ipt_udp");
20 MODULE_ALIAS("ipt_tcp");
21 MODULE_ALIAS("ip6t_udp");
22 MODULE_ALIAS("ip6t_tcp");
24 /* Returns 1 if the port is matched by the range, 0 otherwise */
26 port_match(u_int16_t min
, u_int16_t max
, u_int16_t port
, bool invert
)
28 return (port
>= min
&& port
<= max
) ^ invert
;
32 tcp_find_option(u_int8_t option
,
33 const struct sk_buff
*skb
,
39 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
41 u_int8_t _opt
[60 - sizeof(struct tcphdr
)];
44 pr_debug("finding option\n");
49 /* If we don't have the whole header, drop packet. */
50 op
= skb_header_pointer(skb
, protoff
+ sizeof(struct tcphdr
),
57 for (i
= 0; i
< optlen
; ) {
58 if (op
[i
] == option
) return !invert
;
66 static bool tcp_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
68 const struct tcphdr
*th
;
70 const struct xt_tcp
*tcpinfo
= par
->matchinfo
;
72 if (par
->fragoff
!= 0) {
75 Don't allow a fragment of TCP 8 bytes in. Nobody normal
76 causes this. Its a cracker trying to break in by doing a
77 flag overwrite to pass the direction checks.
79 if (par
->fragoff
== 1) {
80 pr_debug("Dropping evil TCP offset=1 frag.\n");
83 /* Must not be a fragment. */
87 th
= skb_header_pointer(skb
, par
->thoff
, sizeof(_tcph
), &_tcph
);
89 /* We've been asked to examine this packet, and we
90 can't. Hence, no choice but to drop. */
91 pr_debug("Dropping evil TCP offset=0 tinygram.\n");
96 if (!port_match(tcpinfo
->spts
[0], tcpinfo
->spts
[1],
98 !!(tcpinfo
->invflags
& XT_TCP_INV_SRCPT
)))
100 if (!port_match(tcpinfo
->dpts
[0], tcpinfo
->dpts
[1],
102 !!(tcpinfo
->invflags
& XT_TCP_INV_DSTPT
)))
104 if (!NF_INVF(tcpinfo
, XT_TCP_INV_FLAGS
,
105 (((unsigned char *)th
)[13] & tcpinfo
->flg_mask
) == tcpinfo
->flg_cmp
))
107 if (tcpinfo
->option
) {
108 if (th
->doff
* 4 < sizeof(_tcph
)) {
112 if (!tcp_find_option(tcpinfo
->option
, skb
, par
->thoff
,
113 th
->doff
*4 - sizeof(_tcph
),
114 tcpinfo
->invflags
& XT_TCP_INV_OPTION
,
121 static int tcp_mt_check(const struct xt_mtchk_param
*par
)
123 const struct xt_tcp
*tcpinfo
= par
->matchinfo
;
125 /* Must specify no unknown invflags */
126 return (tcpinfo
->invflags
& ~XT_TCP_INV_MASK
) ? -EINVAL
: 0;
129 static bool udp_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
131 const struct udphdr
*uh
;
133 const struct xt_udp
*udpinfo
= par
->matchinfo
;
135 /* Must not be a fragment. */
136 if (par
->fragoff
!= 0)
139 uh
= skb_header_pointer(skb
, par
->thoff
, sizeof(_udph
), &_udph
);
141 /* We've been asked to examine this packet, and we
142 can't. Hence, no choice but to drop. */
143 pr_debug("Dropping evil UDP tinygram.\n");
148 return port_match(udpinfo
->spts
[0], udpinfo
->spts
[1],
150 !!(udpinfo
->invflags
& XT_UDP_INV_SRCPT
))
151 && port_match(udpinfo
->dpts
[0], udpinfo
->dpts
[1],
153 !!(udpinfo
->invflags
& XT_UDP_INV_DSTPT
));
156 static int udp_mt_check(const struct xt_mtchk_param
*par
)
158 const struct xt_udp
*udpinfo
= par
->matchinfo
;
160 /* Must specify no unknown invflags */
161 return (udpinfo
->invflags
& ~XT_UDP_INV_MASK
) ? -EINVAL
: 0;
164 static struct xt_match tcpudp_mt_reg
[] __read_mostly
= {
167 .family
= NFPROTO_IPV4
,
168 .checkentry
= tcp_mt_check
,
170 .matchsize
= sizeof(struct xt_tcp
),
171 .proto
= IPPROTO_TCP
,
176 .family
= NFPROTO_IPV6
,
177 .checkentry
= tcp_mt_check
,
179 .matchsize
= sizeof(struct xt_tcp
),
180 .proto
= IPPROTO_TCP
,
185 .family
= NFPROTO_IPV4
,
186 .checkentry
= udp_mt_check
,
188 .matchsize
= sizeof(struct xt_udp
),
189 .proto
= IPPROTO_UDP
,
194 .family
= NFPROTO_IPV6
,
195 .checkentry
= udp_mt_check
,
197 .matchsize
= sizeof(struct xt_udp
),
198 .proto
= IPPROTO_UDP
,
203 .family
= NFPROTO_IPV4
,
204 .checkentry
= udp_mt_check
,
206 .matchsize
= sizeof(struct xt_udp
),
207 .proto
= IPPROTO_UDPLITE
,
212 .family
= NFPROTO_IPV6
,
213 .checkentry
= udp_mt_check
,
215 .matchsize
= sizeof(struct xt_udp
),
216 .proto
= IPPROTO_UDPLITE
,
221 static int __init
tcpudp_mt_init(void)
223 return xt_register_matches(tcpudp_mt_reg
, ARRAY_SIZE(tcpudp_mt_reg
));
226 static void __exit
tcpudp_mt_exit(void)
228 xt_unregister_matches(tcpudp_mt_reg
, ARRAY_SIZE(tcpudp_mt_reg
));
231 module_init(tcpudp_mt_init
);
232 module_exit(tcpudp_mt_exit
);