1 #include <linux/types.h>
2 #include <linux/module.h>
4 #include <linux/ipv6.h>
8 #include <linux/netfilter/x_tables.h>
9 #include <linux/netfilter/xt_tcpudp.h>
10 #include <linux/netfilter_ipv4/ip_tables.h>
11 #include <linux/netfilter_ipv6/ip6_tables.h>
13 MODULE_DESCRIPTION("x_tables match for TCP and UDP, supports IPv4 and IPv6");
14 MODULE_LICENSE("GPL");
15 MODULE_ALIAS("xt_tcp");
16 MODULE_ALIAS("xt_udp");
17 MODULE_ALIAS("ipt_udp");
18 MODULE_ALIAS("ipt_tcp");
19 MODULE_ALIAS("ip6t_udp");
20 MODULE_ALIAS("ip6t_tcp");
22 #ifdef DEBUG_IP_FIREWALL_USER
23 #define duprintf(format, args...) printk(format , ## args)
25 #define duprintf(format, args...)
29 /* Returns 1 if the port is matched by the range, 0 otherwise */
31 port_match(u_int16_t min
, u_int16_t max
, u_int16_t port
, int invert
)
35 ret
= (port
>= min
&& port
<= max
) ^ invert
;
40 tcp_find_option(u_int8_t option
,
41 const struct sk_buff
*skb
,
47 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
48 u_int8_t _opt
[60 - sizeof(struct tcphdr
)], *op
;
51 duprintf("tcp_match: finding option\n");
56 /* If we don't have the whole header, drop packet. */
57 op
= skb_header_pointer(skb
, protoff
+ sizeof(struct tcphdr
),
64 for (i
= 0; i
< optlen
; ) {
65 if (op
[i
] == option
) return !invert
;
74 tcp_match(const struct sk_buff
*skb
,
75 const struct net_device
*in
,
76 const struct net_device
*out
,
77 const void *matchinfo
,
82 struct tcphdr _tcph
, *th
;
83 const struct xt_tcp
*tcpinfo
= matchinfo
;
88 Don't allow a fragment of TCP 8 bytes in. Nobody normal
89 causes this. Its a cracker trying to break in by doing a
90 flag overwrite to pass the direction checks.
93 duprintf("Dropping evil TCP offset=1 frag.\n");
96 /* Must not be a fragment. */
100 #define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg))
102 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
104 /* We've been asked to examine this packet, and we
105 can't. Hence, no choice but to drop. */
106 duprintf("Dropping evil TCP offset=0 tinygram.\n");
111 if (!port_match(tcpinfo
->spts
[0], tcpinfo
->spts
[1],
113 !!(tcpinfo
->invflags
& XT_TCP_INV_SRCPT
)))
115 if (!port_match(tcpinfo
->dpts
[0], tcpinfo
->dpts
[1],
117 !!(tcpinfo
->invflags
& XT_TCP_INV_DSTPT
)))
119 if (!FWINVTCP((((unsigned char *)th
)[13] & tcpinfo
->flg_mask
)
123 if (tcpinfo
->option
) {
124 if (th
->doff
* 4 < sizeof(_tcph
)) {
128 if (!tcp_find_option(tcpinfo
->option
, skb
, protoff
,
129 th
->doff
*4 - sizeof(_tcph
),
130 tcpinfo
->invflags
& XT_TCP_INV_OPTION
,
137 /* Called when user tries to insert an entry of this type. */
139 tcp_checkentry(const char *tablename
,
142 unsigned int matchsize
,
143 unsigned int hook_mask
)
145 const struct ipt_ip
*ip
= info
;
146 const struct xt_tcp
*tcpinfo
= matchinfo
;
148 /* Must specify proto == TCP, and no unknown invflags */
149 return ip
->proto
== IPPROTO_TCP
150 && !(ip
->invflags
& XT_INV_PROTO
)
151 && matchsize
== XT_ALIGN(sizeof(struct xt_tcp
))
152 && !(tcpinfo
->invflags
& ~XT_TCP_INV_MASK
);
155 /* Called when user tries to insert an entry of this type. */
157 tcp6_checkentry(const char *tablename
,
160 unsigned int matchsize
,
161 unsigned int hook_mask
)
163 const struct ip6t_ip6
*ipv6
= entry
;
164 const struct xt_tcp
*tcpinfo
= matchinfo
;
166 /* Must specify proto == TCP, and no unknown invflags */
167 return ipv6
->proto
== IPPROTO_TCP
168 && !(ipv6
->invflags
& XT_INV_PROTO
)
169 && matchsize
== XT_ALIGN(sizeof(struct xt_tcp
))
170 && !(tcpinfo
->invflags
& ~XT_TCP_INV_MASK
);
175 udp_match(const struct sk_buff
*skb
,
176 const struct net_device
*in
,
177 const struct net_device
*out
,
178 const void *matchinfo
,
180 unsigned int protoff
,
183 struct udphdr _udph
, *uh
;
184 const struct xt_udp
*udpinfo
= matchinfo
;
186 /* Must not be a fragment. */
190 uh
= skb_header_pointer(skb
, protoff
, sizeof(_udph
), &_udph
);
192 /* We've been asked to examine this packet, and we
193 can't. Hence, no choice but to drop. */
194 duprintf("Dropping evil UDP tinygram.\n");
199 return port_match(udpinfo
->spts
[0], udpinfo
->spts
[1],
201 !!(udpinfo
->invflags
& XT_UDP_INV_SRCPT
))
202 && port_match(udpinfo
->dpts
[0], udpinfo
->dpts
[1],
204 !!(udpinfo
->invflags
& XT_UDP_INV_DSTPT
));
207 /* Called when user tries to insert an entry of this type. */
209 udp_checkentry(const char *tablename
,
212 unsigned int matchinfosize
,
213 unsigned int hook_mask
)
215 const struct ipt_ip
*ip
= info
;
216 const struct xt_udp
*udpinfo
= matchinfo
;
218 /* Must specify proto == UDP, and no unknown invflags */
219 if (ip
->proto
!= IPPROTO_UDP
|| (ip
->invflags
& XT_INV_PROTO
)) {
220 duprintf("ipt_udp: Protocol %u != %u\n", ip
->proto
,
224 if (matchinfosize
!= XT_ALIGN(sizeof(struct xt_udp
))) {
225 duprintf("ipt_udp: matchsize %u != %u\n",
226 matchinfosize
, XT_ALIGN(sizeof(struct xt_udp
)));
229 if (udpinfo
->invflags
& ~XT_UDP_INV_MASK
) {
230 duprintf("ipt_udp: unknown flags %X\n",
238 /* Called when user tries to insert an entry of this type. */
240 udp6_checkentry(const char *tablename
,
243 unsigned int matchinfosize
,
244 unsigned int hook_mask
)
246 const struct ip6t_ip6
*ipv6
= entry
;
247 const struct xt_udp
*udpinfo
= matchinfo
;
249 /* Must specify proto == UDP, and no unknown invflags */
250 if (ipv6
->proto
!= IPPROTO_UDP
|| (ipv6
->invflags
& XT_INV_PROTO
)) {
251 duprintf("ip6t_udp: Protocol %u != %u\n", ipv6
->proto
,
255 if (matchinfosize
!= XT_ALIGN(sizeof(struct xt_udp
))) {
256 duprintf("ip6t_udp: matchsize %u != %u\n",
257 matchinfosize
, XT_ALIGN(sizeof(struct xt_udp
)));
260 if (udpinfo
->invflags
& ~XT_UDP_INV_MASK
) {
261 duprintf("ip6t_udp: unknown flags %X\n",
269 static struct xt_match tcp_matchstruct
= {
272 .checkentry
= &tcp_checkentry
,
275 static struct xt_match tcp6_matchstruct
= {
278 .checkentry
= &tcp6_checkentry
,
282 static struct xt_match udp_matchstruct
= {
285 .checkentry
= &udp_checkentry
,
288 static struct xt_match udp6_matchstruct
= {
291 .checkentry
= &udp6_checkentry
,
295 static int __init
init(void)
298 ret
= xt_register_match(AF_INET
, &tcp_matchstruct
);
302 ret
= xt_register_match(AF_INET6
, &tcp6_matchstruct
);
306 ret
= xt_register_match(AF_INET
, &udp_matchstruct
);
310 ret
= xt_register_match(AF_INET6
, &udp6_matchstruct
);
317 xt_unregister_match(AF_INET
, &tcp_matchstruct
);
319 xt_unregister_match(AF_INET6
, &tcp6_matchstruct
);
321 xt_unregister_match(AF_INET
, &tcp_matchstruct
);
325 static void __exit
fini(void)
327 xt_unregister_match(AF_INET6
, &udp6_matchstruct
);
328 xt_unregister_match(AF_INET
, &udp_matchstruct
);
329 xt_unregister_match(AF_INET6
, &tcp6_matchstruct
);
330 xt_unregister_match(AF_INET
, &tcp_matchstruct
);