blob: 84d401bfafad2be0fe528f9d3cd1996c15aa71df [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/* Kernel module to match TCP MSS values. */
2
3/* Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
4 * Portions (C) 2005 by Harald Welte <laforge@netfilter.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <net/tcp.h>
14
15#include <linux/netfilter/xt_tcpmss.h>
16#include <linux/netfilter/x_tables.h>
17
18#include <linux/netfilter_ipv4/ip_tables.h>
19#include <linux/netfilter_ipv6/ip6_tables.h>
20
Harald Welte2e4e6a12006-01-12 13:30:04 -080021MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
23MODULE_DESCRIPTION("iptables TCP MSS match module");
24MODULE_ALIAS("ipt_tcpmss");
Jan Engelhardt73aaf932007-10-11 14:36:40 -070025MODULE_ALIAS("ip6t_tcpmss");
Harald Welte2e4e6a12006-01-12 13:30:04 -080026
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070027static bool
Patrick McHardyce556b32006-08-22 00:44:14 -070028match(const struct sk_buff *skb,
29 const struct net_device *in,
30 const struct net_device *out,
31 const struct xt_match *match,
32 const void *matchinfo,
33 int offset,
34 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070035 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080036{
Patrick McHardyce556b32006-08-22 00:44:14 -070037 const struct xt_tcpmss_match_info *info = matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -080038 struct tcphdr _tcph, *th;
39 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
40 u8 _opt[15 * 4 - sizeof(_tcph)], *op;
41 unsigned int i, optlen;
42
43 /* If we don't have the whole header, drop packet. */
44 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
45 if (th == NULL)
46 goto dropit;
47
48 /* Malformed. */
49 if (th->doff*4 < sizeof(*th))
50 goto dropit;
51
52 optlen = th->doff*4 - sizeof(*th);
53 if (!optlen)
54 goto out;
55
56 /* Truncated options. */
57 op = skb_header_pointer(skb, protoff + sizeof(*th), optlen, _opt);
58 if (op == NULL)
59 goto dropit;
60
61 for (i = 0; i < optlen; ) {
62 if (op[i] == TCPOPT_MSS
63 && (optlen - i) >= TCPOLEN_MSS
64 && op[i+1] == TCPOLEN_MSS) {
65 u_int16_t mssval;
66
67 mssval = (op[i+2] << 8) | op[i+3];
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080068
Patrick McHardyce556b32006-08-22 00:44:14 -070069 return (mssval >= info->mss_min &&
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080070 mssval <= info->mss_max) ^ info->invert;
Harald Welte2e4e6a12006-01-12 13:30:04 -080071 }
Patrick McHardyce556b32006-08-22 00:44:14 -070072 if (op[i] < 2)
73 i++;
74 else
75 i += op[i+1] ? : 1;
Harald Welte2e4e6a12006-01-12 13:30:04 -080076 }
77out:
Patrick McHardyce556b32006-08-22 00:44:14 -070078 return info->invert;
Harald Welte2e4e6a12006-01-12 13:30:04 -080079
Patrick McHardyce556b32006-08-22 00:44:14 -070080dropit:
Jan Engelhardtcff533a2007-07-07 22:15:12 -070081 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070082 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080083}
84
Patrick McHardy9f15c532007-07-07 22:22:02 -070085static struct xt_match xt_tcpmss_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070086 {
87 .name = "tcpmss",
88 .family = AF_INET,
89 .match = match,
90 .matchsize = sizeof(struct xt_tcpmss_match_info),
91 .proto = IPPROTO_TCP,
92 .me = THIS_MODULE,
93 },
94 {
95 .name = "tcpmss",
96 .family = AF_INET6,
97 .match = match,
98 .matchsize = sizeof(struct xt_tcpmss_match_info),
99 .proto = IPPROTO_TCP,
100 .me = THIS_MODULE,
101 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800102};
103
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800104static int __init xt_tcpmss_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800105{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700106 return xt_register_matches(xt_tcpmss_match,
107 ARRAY_SIZE(xt_tcpmss_match));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800108}
109
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800110static void __exit xt_tcpmss_fini(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800111{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700112 xt_unregister_matches(xt_tcpmss_match, ARRAY_SIZE(xt_tcpmss_match));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800113}
114
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800115module_init(xt_tcpmss_init);
116module_exit(xt_tcpmss_fini);