]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match TCP MSS values. */ |
2 | ||
3 | /* Copyright (C) 2000 Marc Boucher <marc@mbsi.ca> | |
2e4e6a17 | 4 | * Portions (C) 2005 by Harald Welte <laforge@netfilter.org> |
1da177e4 LT |
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 | ||
2e4e6a17 HW |
15 | #include <linux/netfilter/xt_tcpmss.h> |
16 | #include <linux/netfilter/x_tables.h> | |
17 | ||
1da177e4 | 18 | #include <linux/netfilter_ipv4/ip_tables.h> |
2e4e6a17 | 19 | #include <linux/netfilter_ipv6/ip6_tables.h> |
1da177e4 | 20 | |
1da177e4 LT |
21 | MODULE_LICENSE("GPL"); |
22 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); | |
2ae15b64 | 23 | MODULE_DESCRIPTION("Xtables: TCP MSS match"); |
2e4e6a17 | 24 | MODULE_ALIAS("ipt_tcpmss"); |
73aaf935 | 25 | MODULE_ALIAS("ip6t_tcpmss"); |
1da177e4 | 26 | |
1d93a9cb | 27 | static bool |
d3c5ee6d JE |
28 | tcpmss_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) | |
1da177e4 | 32 | { |
ce556b3a | 33 | const struct xt_tcpmss_match_info *info = matchinfo; |
1da177e4 LT |
34 | struct tcphdr _tcph, *th; |
35 | /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */ | |
36 | u8 _opt[15 * 4 - sizeof(_tcph)], *op; | |
37 | unsigned int i, optlen; | |
38 | ||
39 | /* If we don't have the whole header, drop packet. */ | |
2e4e6a17 | 40 | th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph); |
1da177e4 LT |
41 | if (th == NULL) |
42 | goto dropit; | |
43 | ||
44 | /* Malformed. */ | |
45 | if (th->doff*4 < sizeof(*th)) | |
46 | goto dropit; | |
47 | ||
48 | optlen = th->doff*4 - sizeof(*th); | |
49 | if (!optlen) | |
50 | goto out; | |
51 | ||
52 | /* Truncated options. */ | |
2e4e6a17 | 53 | op = skb_header_pointer(skb, protoff + sizeof(*th), optlen, _opt); |
1da177e4 LT |
54 | if (op == NULL) |
55 | goto dropit; | |
56 | ||
57 | for (i = 0; i < optlen; ) { | |
58 | if (op[i] == TCPOPT_MSS | |
59 | && (optlen - i) >= TCPOLEN_MSS | |
60 | && op[i+1] == TCPOLEN_MSS) { | |
61 | u_int16_t mssval; | |
62 | ||
63 | mssval = (op[i+2] << 8) | op[i+3]; | |
601e68e1 | 64 | |
ce556b3a | 65 | return (mssval >= info->mss_min && |
601e68e1 | 66 | mssval <= info->mss_max) ^ info->invert; |
1da177e4 | 67 | } |
ce556b3a PM |
68 | if (op[i] < 2) |
69 | i++; | |
70 | else | |
71 | i += op[i+1] ? : 1; | |
1da177e4 LT |
72 | } |
73 | out: | |
ce556b3a | 74 | return info->invert; |
1da177e4 | 75 | |
ce556b3a | 76 | dropit: |
cff533ac | 77 | *hotdrop = true; |
1d93a9cb | 78 | return false; |
1da177e4 LT |
79 | } |
80 | ||
d3c5ee6d | 81 | static struct xt_match tcpmss_mt_reg[] __read_mostly = { |
4470bbc7 PM |
82 | { |
83 | .name = "tcpmss", | |
84 | .family = AF_INET, | |
d3c5ee6d | 85 | .match = tcpmss_mt, |
4470bbc7 PM |
86 | .matchsize = sizeof(struct xt_tcpmss_match_info), |
87 | .proto = IPPROTO_TCP, | |
88 | .me = THIS_MODULE, | |
89 | }, | |
90 | { | |
91 | .name = "tcpmss", | |
92 | .family = AF_INET6, | |
d3c5ee6d | 93 | .match = tcpmss_mt, |
4470bbc7 PM |
94 | .matchsize = sizeof(struct xt_tcpmss_match_info), |
95 | .proto = IPPROTO_TCP, | |
96 | .me = THIS_MODULE, | |
97 | }, | |
1da177e4 LT |
98 | }; |
99 | ||
d3c5ee6d | 100 | static int __init tcpmss_mt_init(void) |
1da177e4 | 101 | { |
d3c5ee6d | 102 | return xt_register_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg)); |
1da177e4 LT |
103 | } |
104 | ||
d3c5ee6d | 105 | static void __exit tcpmss_mt_exit(void) |
1da177e4 | 106 | { |
d3c5ee6d | 107 | xt_unregister_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg)); |
1da177e4 LT |
108 | } |
109 | ||
d3c5ee6d JE |
110 | module_init(tcpmss_mt_init); |
111 | module_exit(tcpmss_mt_exit); |