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 struct xt_match *match,
78 const void *matchinfo,
83 struct tcphdr _tcph, *th;
84 const struct xt_tcp *tcpinfo = matchinfo;
89 Don't allow a fragment of TCP 8 bytes in. Nobody normal
90 causes this. Its a cracker trying to break in by doing a
91 flag overwrite to pass the direction checks.
94 duprintf("Dropping evil TCP offset=1 frag.\n");
97 /* Must not be a fragment. */
101 #define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg))
103 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
105 /* We've been asked to examine this packet, and we
106 can't. Hence, no choice but to drop. */
107 duprintf("Dropping evil TCP offset=0 tinygram.\n");
112 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
114 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
116 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
118 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
120 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
124 if (tcpinfo->option) {
125 if (th->doff * 4 < sizeof(_tcph)) {
129 if (!tcp_find_option(tcpinfo->option, skb, protoff,
130 th->doff*4 - sizeof(_tcph),
131 tcpinfo->invflags & XT_TCP_INV_OPTION,
138 /* Called when user tries to insert an entry of this type. */
140 tcp_checkentry(const char *tablename,
142 const struct xt_match *match,
144 unsigned int matchsize,
145 unsigned int hook_mask)
147 const struct xt_tcp *tcpinfo = matchinfo;
149 /* Must specify no unknown invflags */
150 return !(tcpinfo->invflags & ~XT_TCP_INV_MASK);
154 udp_match(const struct sk_buff *skb,
155 const struct net_device *in,
156 const struct net_device *out,
157 const struct xt_match *match,
158 const void *matchinfo,
160 unsigned int protoff,
163 struct udphdr _udph, *uh;
164 const struct xt_udp *udpinfo = matchinfo;
166 /* Must not be a fragment. */
170 uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);
172 /* We've been asked to examine this packet, and we
173 can't. Hence, no choice but to drop. */
174 duprintf("Dropping evil UDP tinygram.\n");
179 return port_match(udpinfo->spts[0], udpinfo->spts[1],
181 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
182 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
184 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
187 /* Called when user tries to insert an entry of this type. */
189 udp_checkentry(const char *tablename,
191 const struct xt_match *match,
193 unsigned int matchsize,
194 unsigned int hook_mask)
196 const struct xt_tcp *udpinfo = matchinfo;
198 /* Must specify no unknown invflags */
199 return !(udpinfo->invflags & ~XT_UDP_INV_MASK);
202 static struct xt_match tcp_matchstruct = {
205 .matchsize = sizeof(struct xt_tcp),
206 .proto = IPPROTO_TCP,
208 .checkentry = tcp_checkentry,
212 static struct xt_match tcp6_matchstruct = {
215 .matchsize = sizeof(struct xt_tcp),
216 .proto = IPPROTO_TCP,
218 .checkentry = tcp_checkentry,
222 static struct xt_match udp_matchstruct = {
225 .matchsize = sizeof(struct xt_udp),
226 .proto = IPPROTO_UDP,
228 .checkentry = udp_checkentry,
231 static struct xt_match udp6_matchstruct = {
234 .matchsize = sizeof(struct xt_udp),
235 .proto = IPPROTO_UDP,
237 .checkentry = udp_checkentry,
241 static int __init xt_tcpudp_init(void)
244 ret = xt_register_match(&tcp_matchstruct);
248 ret = xt_register_match(&tcp6_matchstruct);
252 ret = xt_register_match(&udp_matchstruct);
256 ret = xt_register_match(&udp6_matchstruct);
263 xt_unregister_match(&tcp_matchstruct);
265 xt_unregister_match(&tcp6_matchstruct);
267 xt_unregister_match(&tcp_matchstruct);
271 static void __exit xt_tcpudp_fini(void)
273 xt_unregister_match(&udp6_matchstruct);
274 xt_unregister_match(&udp_matchstruct);
275 xt_unregister_match(&tcp6_matchstruct);
276 xt_unregister_match(&tcp_matchstruct);
279 module_init(xt_tcpudp_init);
280 module_exit(xt_tcpudp_fini);