[NETFILTER]: ipt_TCPMSS: misc cleanup
[linux-2.6.22.y-op.git] / net / ipv4 / netfilter / ipt_TCPMSS.c
blob4246c4321e5bb74333bc9a1ed1a3fde6929b6510
1 /*
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.
9 */
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
14 #include <linux/ip.h>
15 #include <net/tcp.h>
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <linux/netfilter_ipv4/ipt_TCPMSS.h>
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
22 MODULE_DESCRIPTION("iptables TCP MSS modification module");
24 static inline unsigned int
25 optlen(const u_int8_t *opt, unsigned int offset)
27 /* Beware zero-length options: make finite progress */
28 if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0)
29 return 1;
30 else
31 return opt[offset+1];
34 static unsigned int
35 ipt_tcpmss_target(struct sk_buff **pskb,
36 const struct net_device *in,
37 const struct net_device *out,
38 unsigned int hooknum,
39 const struct xt_target *target,
40 const void *targinfo)
42 const struct ipt_tcpmss_info *tcpmssinfo = targinfo;
43 struct tcphdr *tcph;
44 struct iphdr *iph;
45 u_int16_t tcplen, newtotlen, oldval, newmss;
46 unsigned int i;
47 u_int8_t *opt;
49 if (!skb_make_writable(pskb, (*pskb)->len))
50 return NF_DROP;
52 iph = (*pskb)->nh.iph;
53 tcplen = (*pskb)->len - iph->ihl*4;
54 tcph = (void *)iph + iph->ihl*4;
56 /* Since it passed flags test in tcp match, we know it is is
57 not a fragment, and has data >= tcp header length. SYN
58 packets should not contain data: if they did, then we risk
59 running over MTU, sending Frag Needed and breaking things
60 badly. --RR */
61 if (tcplen != tcph->doff*4) {
62 if (net_ratelimit())
63 printk(KERN_ERR
64 "ipt_tcpmss_target: bad length (%d bytes)\n",
65 (*pskb)->len);
66 return NF_DROP;
69 if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU) {
70 if (dst_mtu((*pskb)->dst) <= sizeof(struct iphdr) +
71 sizeof(struct tcphdr)) {
72 if (net_ratelimit())
73 printk(KERN_ERR "ipt_tcpmss_target: "
74 "unknown or invalid path-MTU (%d)\n",
75 dst_mtu((*pskb)->dst));
76 return NF_DROP; /* or IPT_CONTINUE ?? */
79 newmss = dst_mtu((*pskb)->dst) - sizeof(struct iphdr) -
80 sizeof(struct tcphdr);
81 } else
82 newmss = tcpmssinfo->mss;
84 opt = (u_int8_t *)tcph;
85 for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)) {
86 if (opt[i] == TCPOPT_MSS && tcph->doff*4 - i >= TCPOLEN_MSS &&
87 opt[i+1] == TCPOLEN_MSS) {
88 u_int16_t oldmss;
90 oldmss = (opt[i+2] << 8) | opt[i+3];
92 if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU &&
93 oldmss <= newmss)
94 return IPT_CONTINUE;
96 opt[i+2] = (newmss & 0xff00) >> 8;
97 opt[i+3] = (newmss & 0x00ff);
99 tcph->check = nf_proto_csum_update(*pskb,
100 htons(oldmss)^0xFFFF,
101 htons(newmss),
102 tcph->check, 0);
103 return IPT_CONTINUE;
108 * MSS Option not found ?! add it..
110 if (skb_tailroom((*pskb)) < TCPOLEN_MSS) {
111 struct sk_buff *newskb;
113 newskb = skb_copy_expand(*pskb, skb_headroom(*pskb),
114 TCPOLEN_MSS, GFP_ATOMIC);
115 if (!newskb)
116 return NF_DROP;
117 kfree_skb(*pskb);
118 *pskb = newskb;
119 iph = (*pskb)->nh.iph;
120 tcph = (void *)iph + iph->ihl*4;
123 skb_put((*pskb), TCPOLEN_MSS);
125 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
126 memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr));
128 tcph->check = nf_proto_csum_update(*pskb,
129 htons(tcplen) ^ 0xFFFF,
130 htons(tcplen + TCPOLEN_MSS),
131 tcph->check, 1);
132 opt[0] = TCPOPT_MSS;
133 opt[1] = TCPOLEN_MSS;
134 opt[2] = (newmss & 0xff00) >> 8;
135 opt[3] = (newmss & 0x00ff);
137 tcph->check = nf_proto_csum_update(*pskb, ~0, *((u_int32_t *)opt),
138 tcph->check, 0);
140 oldval = ((u_int16_t *)tcph)[6];
141 tcph->doff += TCPOLEN_MSS/4;
142 tcph->check = nf_proto_csum_update(*pskb,
143 oldval ^ 0xFFFF,
144 ((u_int16_t *)tcph)[6],
145 tcph->check, 0);
147 newtotlen = htons(ntohs(iph->tot_len) + TCPOLEN_MSS);
148 iph->check = nf_csum_update(iph->tot_len ^ 0xFFFF,
149 newtotlen, iph->check);
150 iph->tot_len = newtotlen;
151 return IPT_CONTINUE;
154 #define TH_SYN 0x02
156 static inline int find_syn_match(const struct ipt_entry_match *m)
158 const struct ipt_tcp *tcpinfo = (const struct ipt_tcp *)m->data;
160 if (strcmp(m->u.kernel.match->name, "tcp") == 0 &&
161 tcpinfo->flg_cmp & TH_SYN &&
162 !(tcpinfo->invflags & IPT_TCP_INV_FLAGS))
163 return 1;
165 return 0;
168 /* Must specify -p tcp --syn/--tcp-flags SYN */
169 static int
170 ipt_tcpmss_checkentry(const char *tablename,
171 const void *e_void,
172 const struct xt_target *target,
173 void *targinfo,
174 unsigned int hook_mask)
176 const struct ipt_tcpmss_info *tcpmssinfo = targinfo;
177 const struct ipt_entry *e = e_void;
179 if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU &&
180 (hook_mask & ~((1 << NF_IP_FORWARD) |
181 (1 << NF_IP_LOCAL_OUT) |
182 (1 << NF_IP_POST_ROUTING))) != 0) {
183 printk("TCPMSS: path-MTU clamping only supported in "
184 "FORWARD, OUTPUT and POSTROUTING hooks\n");
185 return 0;
188 if (IPT_MATCH_ITERATE(e, find_syn_match))
189 return 1;
190 printk("TCPMSS: Only works on TCP SYN packets\n");
191 return 0;
194 static struct ipt_target ipt_tcpmss_reg = {
195 .name = "TCPMSS",
196 .target = ipt_tcpmss_target,
197 .targetsize = sizeof(struct ipt_tcpmss_info),
198 .proto = IPPROTO_TCP,
199 .checkentry = ipt_tcpmss_checkentry,
200 .me = THIS_MODULE,
203 static int __init ipt_tcpmss_init(void)
205 return ipt_register_target(&ipt_tcpmss_reg);
208 static void __exit ipt_tcpmss_fini(void)
210 ipt_unregister_target(&ipt_tcpmss_reg);
213 module_init(ipt_tcpmss_init);
214 module_exit(ipt_tcpmss_fini);