2 * This is a module which is used for setting the MSS option in TCP packets.
4 * Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
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.
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
14 #include <linux/ipv6.h>
15 #include <linux/tcp.h>
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>
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");
31 static inline unsigned int
32 optlen(const u_int8_t *opt, unsigned int offset)
34 /* Beware zero-length options: make finite progress */
35 if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0)
42 tcpmss_mangle_packet(struct sk_buff *skb,
43 const struct xt_tcpmss_info *info,
48 unsigned int tcplen, i;
53 if (!skb_make_writable(skb, skb->len))
56 tcplen = skb->len - tcphoff;
57 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
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
64 if (tcplen != tcph->doff*4) {
66 printk(KERN_ERR "xt_TCPMSS: bad length (%u bytes)\n",
71 if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
72 if (dst_mtu(skb->dst) <= minlen) {
74 printk(KERN_ERR "xt_TCPMSS: "
75 "unknown or invalid path-MTU (%u)\n",
79 newmss = dst_mtu(skb->dst) - minlen;
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) {
89 oldmss = (opt[i+2] << 8) | opt[i+3];
91 if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
95 opt[i+2] = (newmss & 0xff00) >> 8;
96 opt[i+3] = newmss & 0x00ff;
98 inet_proto_csum_replace2(&tcph->check, skb,
99 htons(oldmss), htons(newmss),
106 * MSS Option not found ?! add it..
108 if (skb_tailroom(skb) < TCPOLEN_MSS) {
109 if (pskb_expand_head(skb, 0,
110 TCPOLEN_MSS - skb_tailroom(skb),
113 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
116 skb_put(skb, TCPOLEN_MSS);
118 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
119 memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr));
121 inet_proto_csum_replace2(&tcph->check, skb,
122 htons(tcplen), htons(tcplen + TCPOLEN_MSS), 1);
124 opt[1] = TCPOLEN_MSS;
125 opt[2] = (newmss & 0xff00) >> 8;
126 opt[3] = newmss & 0x00ff;
128 inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), 0);
130 oldval = ((__be16 *)tcph)[6];
131 tcph->doff += TCPOLEN_MSS/4;
132 inet_proto_csum_replace2(&tcph->check, skb,
133 oldval, ((__be16 *)tcph)[6], 0);
138 xt_tcpmss_target4(struct sk_buff *skb,
139 const struct net_device *in,
140 const struct net_device *out,
141 unsigned int hooknum,
142 const struct xt_target *target,
143 const void *targinfo)
145 struct iphdr *iph = ip_hdr(skb);
149 ret = tcpmss_mangle_packet(skb, targinfo, iph->ihl * 4,
150 sizeof(*iph) + sizeof(struct tcphdr));
155 newlen = htons(ntohs(iph->tot_len) + ret);
156 csum_replace2(&iph->check, iph->tot_len, newlen);
157 iph->tot_len = newlen;
162 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
164 xt_tcpmss_target6(struct sk_buff *skb,
165 const struct net_device *in,
166 const struct net_device *out,
167 unsigned int hooknum,
168 const struct xt_target *target,
169 const void *targinfo)
171 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
176 nexthdr = ipv6h->nexthdr;
177 tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr);
180 ret = tcpmss_mangle_packet(skb, targinfo, tcphoff,
181 sizeof(*ipv6h) + sizeof(struct tcphdr));
185 ipv6h = ipv6_hdr(skb);
186 ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret);
194 /* Must specify -p tcp --syn */
195 static inline bool find_syn_match(const struct xt_entry_match *m)
197 const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data;
199 if (strcmp(m->u.kernel.match->name, "tcp") == 0 &&
200 tcpinfo->flg_cmp & TH_SYN &&
201 !(tcpinfo->invflags & XT_TCP_INV_FLAGS))
208 xt_tcpmss_checkentry4(const char *tablename,
210 const struct xt_target *target,
212 unsigned int hook_mask)
214 const struct xt_tcpmss_info *info = targinfo;
215 const struct ipt_entry *e = entry;
217 if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
218 (hook_mask & ~((1 << NF_INET_FORWARD) |
219 (1 << NF_INET_LOCAL_OUT) |
220 (1 << NF_INET_POST_ROUTING))) != 0) {
221 printk("xt_TCPMSS: path-MTU clamping only supported in "
222 "FORWARD, OUTPUT and POSTROUTING hooks\n");
225 if (IPT_MATCH_ITERATE(e, find_syn_match))
227 printk("xt_TCPMSS: Only works on TCP SYN packets\n");
231 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
233 xt_tcpmss_checkentry6(const char *tablename,
235 const struct xt_target *target,
237 unsigned int hook_mask)
239 const struct xt_tcpmss_info *info = targinfo;
240 const struct ip6t_entry *e = entry;
242 if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
243 (hook_mask & ~((1 << NF_INET_FORWARD) |
244 (1 << NF_INET_LOCAL_OUT) |
245 (1 << NF_INET_POST_ROUTING))) != 0) {
246 printk("xt_TCPMSS: path-MTU clamping only supported in "
247 "FORWARD, OUTPUT and POSTROUTING hooks\n");
250 if (IP6T_MATCH_ITERATE(e, find_syn_match))
252 printk("xt_TCPMSS: Only works on TCP SYN packets\n");
257 static struct xt_target xt_tcpmss_reg[] __read_mostly = {
261 .checkentry = xt_tcpmss_checkentry4,
262 .target = xt_tcpmss_target4,
263 .targetsize = sizeof(struct xt_tcpmss_info),
264 .proto = IPPROTO_TCP,
267 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
271 .checkentry = xt_tcpmss_checkentry6,
272 .target = xt_tcpmss_target6,
273 .targetsize = sizeof(struct xt_tcpmss_info),
274 .proto = IPPROTO_TCP,
280 static int __init xt_tcpmss_init(void)
282 return xt_register_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg));
285 static void __exit xt_tcpmss_fini(void)
287 xt_unregister_targets(xt_tcpmss_reg, ARRAY_SIZE(xt_tcpmss_reg));
290 module_init(xt_tcpmss_init);
291 module_exit(xt_tcpmss_fini);