1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2 #include <linux/types.h>
3 #include <linux/module.h>
5 #include <linux/ipv6.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/xt_tcpudp.h>
11 #include <linux/netfilter_ipv4/ip_tables.h>
12 #include <linux/netfilter_ipv6/ip6_tables.h>
14 MODULE_DESCRIPTION("Xtables: TCP, UDP and UDP-Lite match");
15 MODULE_LICENSE("GPL");
16 MODULE_ALIAS("xt_tcp");
17 MODULE_ALIAS("xt_udp");
18 MODULE_ALIAS("ipt_udp");
19 MODULE_ALIAS("ipt_tcp");
20 MODULE_ALIAS("ip6t_udp");
21 MODULE_ALIAS("ip6t_tcp");
23 /* Returns 1 if the port is matched by the range, 0 otherwise */
25 port_match(u_int16_t min, u_int16_t max, u_int16_t port, bool invert)
27 return (port >= min && port <= max) ^ invert;
31 tcp_find_option(u_int8_t option,
32 const struct sk_buff *skb,
38 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
40 u_int8_t _opt[60 - sizeof(struct tcphdr)];
43 pr_debug("finding option\n");
48 /* If we don't have the whole header, drop packet. */
49 op = skb_header_pointer(skb, protoff + sizeof(struct tcphdr),
56 for (i = 0; i < optlen; ) {
57 if (op[i] == option) return !invert;
65 static bool tcp_mt(const struct sk_buff *skb, struct xt_action_param *par)
67 const struct tcphdr *th;
69 const struct xt_tcp *tcpinfo = par->matchinfo;
71 if (par->fragoff != 0) {
74 Don't allow a fragment of TCP 8 bytes in. Nobody normal
75 causes this. Its a cracker trying to break in by doing a
76 flag overwrite to pass the direction checks.
78 if (par->fragoff == 1) {
79 pr_debug("Dropping evil TCP offset=1 frag.\n");
82 /* Must not be a fragment. */
86 th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
88 /* We've been asked to examine this packet, and we
89 can't. Hence, no choice but to drop. */
90 pr_debug("Dropping evil TCP offset=0 tinygram.\n");
95 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
97 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
99 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
101 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
103 if (!NF_INVF(tcpinfo, XT_TCP_INV_FLAGS,
104 (((unsigned char *)th)[13] & tcpinfo->flg_mask) == tcpinfo->flg_cmp))
106 if (tcpinfo->option) {
107 if (th->doff * 4 < sizeof(_tcph)) {
111 if (!tcp_find_option(tcpinfo->option, skb, par->thoff,
112 th->doff*4 - sizeof(_tcph),
113 tcpinfo->invflags & XT_TCP_INV_OPTION,
120 static int tcp_mt_check(const struct xt_mtchk_param *par)
122 const struct xt_tcp *tcpinfo = par->matchinfo;
124 /* Must specify no unknown invflags */
125 return (tcpinfo->invflags & ~XT_TCP_INV_MASK) ? -EINVAL : 0;
128 static bool udp_mt(const struct sk_buff *skb, struct xt_action_param *par)
130 const struct udphdr *uh;
132 const struct xt_udp *udpinfo = par->matchinfo;
134 /* Must not be a fragment. */
135 if (par->fragoff != 0)
138 uh = skb_header_pointer(skb, par->thoff, sizeof(_udph), &_udph);
140 /* We've been asked to examine this packet, and we
141 can't. Hence, no choice but to drop. */
142 pr_debug("Dropping evil UDP tinygram.\n");
147 return port_match(udpinfo->spts[0], udpinfo->spts[1],
149 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
150 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
152 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
155 static int udp_mt_check(const struct xt_mtchk_param *par)
157 const struct xt_udp *udpinfo = par->matchinfo;
159 /* Must specify no unknown invflags */
160 return (udpinfo->invflags & ~XT_UDP_INV_MASK) ? -EINVAL : 0;
163 static struct xt_match tcpudp_mt_reg[] __read_mostly = {
166 .family = NFPROTO_IPV4,
167 .checkentry = tcp_mt_check,
169 .matchsize = sizeof(struct xt_tcp),
170 .proto = IPPROTO_TCP,
175 .family = NFPROTO_IPV6,
176 .checkentry = tcp_mt_check,
178 .matchsize = sizeof(struct xt_tcp),
179 .proto = IPPROTO_TCP,
184 .family = NFPROTO_IPV4,
185 .checkentry = udp_mt_check,
187 .matchsize = sizeof(struct xt_udp),
188 .proto = IPPROTO_UDP,
193 .family = NFPROTO_IPV6,
194 .checkentry = udp_mt_check,
196 .matchsize = sizeof(struct xt_udp),
197 .proto = IPPROTO_UDP,
202 .family = NFPROTO_IPV4,
203 .checkentry = udp_mt_check,
205 .matchsize = sizeof(struct xt_udp),
206 .proto = IPPROTO_UDPLITE,
211 .family = NFPROTO_IPV6,
212 .checkentry = udp_mt_check,
214 .matchsize = sizeof(struct xt_udp),
215 .proto = IPPROTO_UDPLITE,
220 static int __init tcpudp_mt_init(void)
222 return xt_register_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
225 static void __exit tcpudp_mt_exit(void)
227 xt_unregister_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
230 module_init(tcpudp_mt_init);
231 module_exit(tcpudp_mt_exit);