Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This is a module which is used for setting the MSS option in TCP packets. |
| 3 | * |
| 4 | * Copyright (C) 2000 Marc Boucher <marc@mbsi.ca> |
| 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 <linux/ip.h> |
| 14 | #include <linux/ipv6.h> |
| 15 | #include <linux/tcp.h> |
| 16 | #include <net/ipv6.h> |
| 17 | #include <net/tcp.h> |
| 18 | |
| 19 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 20 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 21 | #include <linux/netfilter/x_tables.h> |
| 22 | #include <linux/netfilter/xt_tcpudp.h> |
| 23 | #include <linux/netfilter/xt_TCPMSS.h> |
| 24 | |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); |
| 27 | MODULE_DESCRIPTION("x_tables TCP MSS modification module"); |
| 28 | MODULE_ALIAS("ipt_TCPMSS"); |
| 29 | MODULE_ALIAS("ip6t_TCPMSS"); |
| 30 | |
| 31 | static inline unsigned int |
| 32 | optlen(const u_int8_t *opt, unsigned int offset) |
| 33 | { |
| 34 | /* Beware zero-length options: make finite progress */ |
| 35 | if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0) |
| 36 | return 1; |
| 37 | else |
| 38 | return opt[offset+1]; |
| 39 | } |
| 40 | |
| 41 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 42 | tcpmss_mangle_packet(struct sk_buff *skb, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 43 | const struct xt_tcpmss_info *info, |
| 44 | unsigned int tcphoff, |
| 45 | unsigned int minlen) |
| 46 | { |
| 47 | struct tcphdr *tcph; |
| 48 | unsigned int tcplen, i; |
| 49 | __be16 oldval; |
| 50 | u16 newmss; |
| 51 | u8 *opt; |
| 52 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 53 | if (!skb_make_writable(skb, skb->len)) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 54 | return -1; |
| 55 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 56 | tcplen = skb->len - tcphoff; |
| 57 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 58 | |
| 59 | /* Since it passed flags test in tcp match, we know it is is |
| 60 | not a fragment, and has data >= tcp header length. SYN |
| 61 | packets should not contain data: if they did, then we risk |
| 62 | running over MTU, sending Frag Needed and breaking things |
| 63 | badly. --RR */ |
| 64 | if (tcplen != tcph->doff*4) { |
| 65 | if (net_ratelimit()) |
| 66 | printk(KERN_ERR "xt_TCPMSS: bad length (%u bytes)\n", |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 67 | skb->len); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if (info->mss == XT_TCPMSS_CLAMP_PMTU) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 72 | if (dst_mtu(skb->dst) <= minlen) { |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 73 | if (net_ratelimit()) |
| 74 | printk(KERN_ERR "xt_TCPMSS: " |
| 75 | "unknown or invalid path-MTU (%u)\n", |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 76 | dst_mtu(skb->dst)); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 77 | return -1; |
| 78 | } |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 79 | newmss = dst_mtu(skb->dst) - minlen; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 80 | } else |
| 81 | newmss = info->mss; |
| 82 | |
| 83 | opt = (u_int8_t *)tcph; |
| 84 | for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)) { |
| 85 | if (opt[i] == TCPOPT_MSS && tcph->doff*4 - i >= TCPOLEN_MSS && |
| 86 | opt[i+1] == TCPOLEN_MSS) { |
| 87 | u_int16_t oldmss; |
| 88 | |
| 89 | oldmss = (opt[i+2] << 8) | opt[i+3]; |
| 90 | |
Benjamin LaHaise | 1700806 | 2007-12-17 22:27:36 -0800 | [diff] [blame^] | 91 | /* Never increase MSS, even when setting it, as |
| 92 | * doing so results in problems for hosts that rely |
| 93 | * on MSS being set correctly. |
| 94 | */ |
| 95 | if (oldmss <= newmss) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 96 | return 0; |
| 97 | |
| 98 | opt[i+2] = (newmss & 0xff00) >> 8; |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 99 | opt[i+3] = newmss & 0x00ff; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 100 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 101 | inet_proto_csum_replace2(&tcph->check, skb, |
| 102 | htons(oldmss), htons(newmss), |
| 103 | 0); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * MSS Option not found ?! add it.. |
| 110 | */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 111 | if (skb_tailroom(skb) < TCPOLEN_MSS) { |
| 112 | if (pskb_expand_head(skb, 0, |
| 113 | TCPOLEN_MSS - skb_tailroom(skb), |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 114 | GFP_ATOMIC)) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 115 | return -1; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 116 | tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 119 | skb_put(skb, TCPOLEN_MSS); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 120 | |
| 121 | opt = (u_int8_t *)tcph + sizeof(struct tcphdr); |
| 122 | memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr)); |
| 123 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 124 | inet_proto_csum_replace2(&tcph->check, skb, |
| 125 | htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 126 | opt[0] = TCPOPT_MSS; |
| 127 | opt[1] = TCPOLEN_MSS; |
| 128 | opt[2] = (newmss & 0xff00) >> 8; |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 129 | opt[3] = newmss & 0x00ff; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 130 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 131 | inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), 0); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 132 | |
| 133 | oldval = ((__be16 *)tcph)[6]; |
| 134 | tcph->doff += TCPOLEN_MSS/4; |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 135 | inet_proto_csum_replace2(&tcph->check, skb, |
| 136 | oldval, ((__be16 *)tcph)[6], 0); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 137 | return TCPOLEN_MSS; |
| 138 | } |
| 139 | |
| 140 | static unsigned int |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 141 | tcpmss_tg4(struct sk_buff *skb, const struct net_device *in, |
| 142 | const struct net_device *out, unsigned int hooknum, |
| 143 | const struct xt_target *target, const void *targinfo) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 144 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 145 | struct iphdr *iph = ip_hdr(skb); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 146 | __be16 newlen; |
| 147 | int ret; |
| 148 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 149 | ret = tcpmss_mangle_packet(skb, targinfo, iph->ihl * 4, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 150 | sizeof(*iph) + sizeof(struct tcphdr)); |
| 151 | if (ret < 0) |
| 152 | return NF_DROP; |
| 153 | if (ret > 0) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 154 | iph = ip_hdr(skb); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 155 | newlen = htons(ntohs(iph->tot_len) + ret); |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 156 | csum_replace2(&iph->check, iph->tot_len, newlen); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 157 | iph->tot_len = newlen; |
| 158 | } |
| 159 | return XT_CONTINUE; |
| 160 | } |
| 161 | |
| 162 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
| 163 | static unsigned int |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 164 | tcpmss_tg6(struct sk_buff *skb, const struct net_device *in, |
| 165 | const struct net_device *out, unsigned int hooknum, |
| 166 | const struct xt_target *target, const void *targinfo) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 167 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 168 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 169 | u8 nexthdr; |
| 170 | int tcphoff; |
| 171 | int ret; |
| 172 | |
| 173 | nexthdr = ipv6h->nexthdr; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 174 | tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr); |
Patrick McHardy | 9dc0564 | 2007-11-30 23:58:03 +1100 | [diff] [blame] | 175 | if (tcphoff < 0) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 176 | return NF_DROP; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 177 | ret = tcpmss_mangle_packet(skb, targinfo, tcphoff, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 178 | sizeof(*ipv6h) + sizeof(struct tcphdr)); |
| 179 | if (ret < 0) |
| 180 | return NF_DROP; |
| 181 | if (ret > 0) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 182 | ipv6h = ipv6_hdr(skb); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 183 | ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret); |
| 184 | } |
| 185 | return XT_CONTINUE; |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | #define TH_SYN 0x02 |
| 190 | |
| 191 | /* Must specify -p tcp --syn */ |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 192 | static inline bool find_syn_match(const struct xt_entry_match *m) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 193 | { |
| 194 | const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data; |
| 195 | |
| 196 | if (strcmp(m->u.kernel.match->name, "tcp") == 0 && |
| 197 | tcpinfo->flg_cmp & TH_SYN && |
| 198 | !(tcpinfo->invflags & XT_TCP_INV_FLAGS)) |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 199 | return true; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 200 | |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 201 | return false; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 204 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 205 | tcpmss_tg4_check(const char *tablename, const void *entry, |
| 206 | const struct xt_target *target, void *targinfo, |
| 207 | unsigned int hook_mask) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 208 | { |
| 209 | const struct xt_tcpmss_info *info = targinfo; |
| 210 | const struct ipt_entry *e = entry; |
| 211 | |
| 212 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 213 | (hook_mask & ~((1 << NF_INET_FORWARD) | |
| 214 | (1 << NF_INET_LOCAL_OUT) | |
| 215 | (1 << NF_INET_POST_ROUTING))) != 0) { |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 216 | printk("xt_TCPMSS: path-MTU clamping only supported in " |
| 217 | "FORWARD, OUTPUT and POSTROUTING hooks\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 218 | return false; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 219 | } |
| 220 | if (IPT_MATCH_ITERATE(e, find_syn_match)) |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 221 | return true; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 222 | printk("xt_TCPMSS: Only works on TCP SYN packets\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 223 | return false; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 227 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 228 | tcpmss_tg6_check(const char *tablename, const void *entry, |
| 229 | const struct xt_target *target, void *targinfo, |
| 230 | unsigned int hook_mask) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 231 | { |
| 232 | const struct xt_tcpmss_info *info = targinfo; |
| 233 | const struct ip6t_entry *e = entry; |
| 234 | |
| 235 | if (info->mss == XT_TCPMSS_CLAMP_PMTU && |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 236 | (hook_mask & ~((1 << NF_INET_FORWARD) | |
| 237 | (1 << NF_INET_LOCAL_OUT) | |
| 238 | (1 << NF_INET_POST_ROUTING))) != 0) { |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 239 | printk("xt_TCPMSS: path-MTU clamping only supported in " |
| 240 | "FORWARD, OUTPUT and POSTROUTING hooks\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 241 | return false; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 242 | } |
| 243 | if (IP6T_MATCH_ITERATE(e, find_syn_match)) |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 244 | return true; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 245 | printk("xt_TCPMSS: Only works on TCP SYN packets\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 246 | return false; |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 247 | } |
| 248 | #endif |
| 249 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 250 | static struct xt_target tcpmss_tg_reg[] __read_mostly = { |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 251 | { |
| 252 | .family = AF_INET, |
| 253 | .name = "TCPMSS", |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 254 | .checkentry = tcpmss_tg4_check, |
| 255 | .target = tcpmss_tg4, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 256 | .targetsize = sizeof(struct xt_tcpmss_info), |
| 257 | .proto = IPPROTO_TCP, |
| 258 | .me = THIS_MODULE, |
| 259 | }, |
| 260 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
| 261 | { |
| 262 | .family = AF_INET6, |
| 263 | .name = "TCPMSS", |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 264 | .checkentry = tcpmss_tg6_check, |
| 265 | .target = tcpmss_tg6, |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 266 | .targetsize = sizeof(struct xt_tcpmss_info), |
| 267 | .proto = IPPROTO_TCP, |
| 268 | .me = THIS_MODULE, |
| 269 | }, |
| 270 | #endif |
| 271 | }; |
| 272 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 273 | static int __init tcpmss_tg_init(void) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 274 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 275 | return xt_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg)); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 278 | static void __exit tcpmss_tg_exit(void) |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 279 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 280 | xt_unregister_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg)); |
Patrick McHardy | cdd289a | 2007-02-07 15:09:46 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 283 | module_init(tcpmss_tg_init); |
| 284 | module_exit(tcpmss_tg_exit); |