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(-Lite), 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, bool invert)
33 return (port >= min && port <= max) ^ invert;
37 tcp_find_option(u_int8_t option,
38 const struct sk_buff *skb,
44 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
45 u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
48 duprintf("tcp_match: finding option\n");
53 /* If we don't have the whole header, drop packet. */
54 op = skb_header_pointer(skb, protoff + sizeof(struct tcphdr),
61 for (i = 0; i < optlen; ) {
62 if (op[i] == option) return !invert;
71 tcp_match(const struct sk_buff *skb,
72 const struct net_device *in,
73 const struct net_device *out,
74 const struct xt_match *match,
75 const void *matchinfo,
80 struct tcphdr _tcph, *th;
81 const struct xt_tcp *tcpinfo = matchinfo;
86 Don't allow a fragment of TCP 8 bytes in. Nobody normal
87 causes this. Its a cracker trying to break in by doing a
88 flag overwrite to pass the direction checks.
91 duprintf("Dropping evil TCP offset=1 frag.\n");
94 /* Must not be a fragment. */
98 #define FWINVTCP(bool, invflg) ((bool) ^ !!(tcpinfo->invflags & (invflg)))
100 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
102 /* We've been asked to examine this packet, and we
103 can't. Hence, no choice but to drop. */
104 duprintf("Dropping evil TCP offset=0 tinygram.\n");
109 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
111 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
113 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
115 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
117 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
121 if (tcpinfo->option) {
122 if (th->doff * 4 < sizeof(_tcph)) {
126 if (!tcp_find_option(tcpinfo->option, skb, protoff,
127 th->doff*4 - sizeof(_tcph),
128 tcpinfo->invflags & XT_TCP_INV_OPTION,
135 /* Called when user tries to insert an entry of this type. */
137 tcp_checkentry(const char *tablename,
139 const struct xt_match *match,
141 unsigned int hook_mask)
143 const struct xt_tcp *tcpinfo = matchinfo;
145 /* Must specify no unknown invflags */
146 return !(tcpinfo->invflags & ~XT_TCP_INV_MASK);
150 udp_match(const struct sk_buff *skb,
151 const struct net_device *in,
152 const struct net_device *out,
153 const struct xt_match *match,
154 const void *matchinfo,
156 unsigned int protoff,
159 struct udphdr _udph, *uh;
160 const struct xt_udp *udpinfo = matchinfo;
162 /* Must not be a fragment. */
166 uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);
168 /* We've been asked to examine this packet, and we
169 can't. Hence, no choice but to drop. */
170 duprintf("Dropping evil UDP tinygram.\n");
175 return port_match(udpinfo->spts[0], udpinfo->spts[1],
177 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
178 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
180 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
183 /* Called when user tries to insert an entry of this type. */
185 udp_checkentry(const char *tablename,
187 const struct xt_match *match,
189 unsigned int hook_mask)
191 const struct xt_udp *udpinfo = matchinfo;
193 /* Must specify no unknown invflags */
194 return !(udpinfo->invflags & ~XT_UDP_INV_MASK);
197 static struct xt_match xt_tcpudp_match[] __read_mostly = {
201 .checkentry = tcp_checkentry,
203 .matchsize = sizeof(struct xt_tcp),
204 .proto = IPPROTO_TCP,
210 .checkentry = tcp_checkentry,
212 .matchsize = sizeof(struct xt_tcp),
213 .proto = IPPROTO_TCP,
219 .checkentry = udp_checkentry,
221 .matchsize = sizeof(struct xt_udp),
222 .proto = IPPROTO_UDP,
228 .checkentry = udp_checkentry,
230 .matchsize = sizeof(struct xt_udp),
231 .proto = IPPROTO_UDP,
237 .checkentry = udp_checkentry,
239 .matchsize = sizeof(struct xt_udp),
240 .proto = IPPROTO_UDPLITE,
246 .checkentry = udp_checkentry,
248 .matchsize = sizeof(struct xt_udp),
249 .proto = IPPROTO_UDPLITE,
254 static int __init xt_tcpudp_init(void)
256 return xt_register_matches(xt_tcpudp_match,
257 ARRAY_SIZE(xt_tcpudp_match));
260 static void __exit xt_tcpudp_fini(void)
262 xt_unregister_matches(xt_tcpudp_match, ARRAY_SIZE(xt_tcpudp_match));
265 module_init(xt_tcpudp_init);
266 module_exit(xt_tcpudp_fini);