blob: 4791c7cbe5a9eadd750e142f9d6d822925534423 [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>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080023MODULE_DESCRIPTION("Xtables: TCP MSS match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080024MODULE_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
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080028tcpmss_mt(const struct sk_buff *skb, const struct net_device *in,
29 const struct net_device *out, const struct xt_match *match,
30 const void *matchinfo, int offset, unsigned int protoff,
31 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080032{
Patrick McHardyce556b32006-08-22 00:44:14 -070033 const struct xt_tcpmss_match_info *info = matchinfo;
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020034 const struct tcphdr *th;
35 struct tcphdr _tcph;
Harald Welte2e4e6a12006-01-12 13:30:04 -080036 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020037 const u_int8_t *op;
38 u8 _opt[15 * 4 - sizeof(_tcph)];
Harald Welte2e4e6a12006-01-12 13:30:04 -080039 unsigned int i, optlen;
40
41 /* If we don't have the whole header, drop packet. */
42 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
43 if (th == NULL)
44 goto dropit;
45
46 /* Malformed. */
47 if (th->doff*4 < sizeof(*th))
48 goto dropit;
49
50 optlen = th->doff*4 - sizeof(*th);
51 if (!optlen)
52 goto out;
53
54 /* Truncated options. */
55 op = skb_header_pointer(skb, protoff + sizeof(*th), optlen, _opt);
56 if (op == NULL)
57 goto dropit;
58
59 for (i = 0; i < optlen; ) {
60 if (op[i] == TCPOPT_MSS
61 && (optlen - i) >= TCPOLEN_MSS
62 && op[i+1] == TCPOLEN_MSS) {
63 u_int16_t mssval;
64
65 mssval = (op[i+2] << 8) | op[i+3];
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080066
Patrick McHardyce556b32006-08-22 00:44:14 -070067 return (mssval >= info->mss_min &&
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080068 mssval <= info->mss_max) ^ info->invert;
Harald Welte2e4e6a12006-01-12 13:30:04 -080069 }
Patrick McHardyce556b32006-08-22 00:44:14 -070070 if (op[i] < 2)
71 i++;
72 else
73 i += op[i+1] ? : 1;
Harald Welte2e4e6a12006-01-12 13:30:04 -080074 }
75out:
Patrick McHardyce556b32006-08-22 00:44:14 -070076 return info->invert;
Harald Welte2e4e6a12006-01-12 13:30:04 -080077
Patrick McHardyce556b32006-08-22 00:44:14 -070078dropit:
Jan Engelhardtcff533a2007-07-07 22:15:12 -070079 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070080 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080081}
82
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080083static struct xt_match tcpmss_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070084 {
85 .name = "tcpmss",
Jan Engelhardtee999d82008-10-08 11:35:01 +020086 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080087 .match = tcpmss_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070088 .matchsize = sizeof(struct xt_tcpmss_match_info),
89 .proto = IPPROTO_TCP,
90 .me = THIS_MODULE,
91 },
92 {
93 .name = "tcpmss",
Jan Engelhardtee999d82008-10-08 11:35:01 +020094 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080095 .match = tcpmss_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070096 .matchsize = sizeof(struct xt_tcpmss_match_info),
97 .proto = IPPROTO_TCP,
98 .me = THIS_MODULE,
99 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800100};
101
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800102static int __init tcpmss_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800103{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800104 return xt_register_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800105}
106
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800107static void __exit tcpmss_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800108{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800109 xt_unregister_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800110}
111
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800112module_init(tcpmss_mt_init);
113module_exit(tcpmss_mt_exit);