[NETFILTER]: ipt annotations
[pandora-kernel.git] / net / ipv4 / netfilter / ipt_TCPMSS.c
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
14 #include <linux/ip.h>
15 #include <net/tcp.h>
16
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <linux/netfilter_ipv4/ipt_TCPMSS.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
22 MODULE_DESCRIPTION("iptables TCP MSS modification module");
23
24 static inline unsigned int
25 optlen(const u_int8_t *opt, unsigned int offset)
26 {
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];
32 }
33
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)
41 {
42         const struct ipt_tcpmss_info *tcpmssinfo = targinfo;
43         struct tcphdr *tcph;
44         struct iphdr *iph;
45         u_int16_t tcplen, newmss;
46         __be16 newtotlen, oldval;
47         unsigned int i;
48         u_int8_t *opt;
49
50         if (!skb_make_writable(pskb, (*pskb)->len))
51                 return NF_DROP;
52
53         iph = (*pskb)->nh.iph;
54         tcplen = (*pskb)->len - iph->ihl*4;
55         tcph = (void *)iph + iph->ihl*4;
56
57         /* Since it passed flags test in tcp match, we know it is is
58            not a fragment, and has data >= tcp header length.  SYN
59            packets should not contain data: if they did, then we risk
60            running over MTU, sending Frag Needed and breaking things
61            badly. --RR */
62         if (tcplen != tcph->doff*4) {
63                 if (net_ratelimit())
64                         printk(KERN_ERR
65                                "ipt_tcpmss_target: bad length (%d bytes)\n",
66                                (*pskb)->len);
67                 return NF_DROP;
68         }
69
70         if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU) {
71                 if (dst_mtu((*pskb)->dst) <= sizeof(struct iphdr) +
72                                              sizeof(struct tcphdr)) {
73                         if (net_ratelimit())
74                                 printk(KERN_ERR "ipt_tcpmss_target: "
75                                        "unknown or invalid path-MTU (%d)\n",
76                                        dst_mtu((*pskb)->dst));
77                         return NF_DROP; /* or IPT_CONTINUE ?? */
78                 }
79
80                 newmss = dst_mtu((*pskb)->dst) - sizeof(struct iphdr) -
81                                                  sizeof(struct tcphdr);
82         } else
83                 newmss = tcpmssinfo->mss;
84
85         opt = (u_int8_t *)tcph;
86         for (i = sizeof(struct tcphdr); i < tcph->doff*4; i += optlen(opt, i)) {
87                 if (opt[i] == TCPOPT_MSS && tcph->doff*4 - i >= TCPOLEN_MSS &&
88                     opt[i+1] == TCPOLEN_MSS) {
89                         u_int16_t oldmss;
90
91                         oldmss = (opt[i+2] << 8) | opt[i+3];
92
93                         if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU &&
94                             oldmss <= newmss)
95                                 return IPT_CONTINUE;
96
97                         opt[i+2] = (newmss & 0xff00) >> 8;
98                         opt[i+3] = (newmss & 0x00ff);
99
100                         tcph->check = nf_proto_csum_update(*pskb,
101                                                            htons(oldmss)^htons(0xFFFF),
102                                                            htons(newmss),
103                                                            tcph->check, 0);
104                         return IPT_CONTINUE;
105                 }
106         }
107
108         /*
109          * MSS Option not found ?! add it..
110          */
111         if (skb_tailroom((*pskb)) < TCPOLEN_MSS) {
112                 struct sk_buff *newskb;
113
114                 newskb = skb_copy_expand(*pskb, skb_headroom(*pskb),
115                                          TCPOLEN_MSS, GFP_ATOMIC);
116                 if (!newskb)
117                         return NF_DROP;
118                 kfree_skb(*pskb);
119                 *pskb = newskb;
120                 iph = (*pskb)->nh.iph;
121                 tcph = (void *)iph + iph->ihl*4;
122         }
123
124         skb_put((*pskb), TCPOLEN_MSS);
125
126         opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
127         memmove(opt + TCPOLEN_MSS, opt, tcplen - sizeof(struct tcphdr));
128
129         tcph->check = nf_proto_csum_update(*pskb,
130                                            htons(tcplen) ^ htons(0xFFFF),
131                                            htons(tcplen + TCPOLEN_MSS),
132                                            tcph->check, 1);
133         opt[0] = TCPOPT_MSS;
134         opt[1] = TCPOLEN_MSS;
135         opt[2] = (newmss & 0xff00) >> 8;
136         opt[3] = (newmss & 0x00ff);
137
138         tcph->check = nf_proto_csum_update(*pskb, htonl(~0), *((__be32 *)opt),
139                                            tcph->check, 0);
140
141         oldval = ((__be16 *)tcph)[6];
142         tcph->doff += TCPOLEN_MSS/4;
143         tcph->check = nf_proto_csum_update(*pskb,
144                                            oldval ^ htons(0xFFFF),
145                                            ((__be16 *)tcph)[6],
146                                            tcph->check, 0);
147
148         newtotlen = htons(ntohs(iph->tot_len) + TCPOLEN_MSS);
149         iph->check = nf_csum_update(iph->tot_len ^ htons(0xFFFF),
150                                     newtotlen, iph->check);
151         iph->tot_len = newtotlen;
152         return IPT_CONTINUE;
153 }
154
155 #define TH_SYN 0x02
156
157 static inline int find_syn_match(const struct ipt_entry_match *m)
158 {
159         const struct ipt_tcp *tcpinfo = (const struct ipt_tcp *)m->data;
160
161         if (strcmp(m->u.kernel.match->name, "tcp") == 0 &&
162             tcpinfo->flg_cmp & TH_SYN &&
163             !(tcpinfo->invflags & IPT_TCP_INV_FLAGS))
164                 return 1;
165
166         return 0;
167 }
168
169 /* Must specify -p tcp --syn/--tcp-flags SYN */
170 static int
171 ipt_tcpmss_checkentry(const char *tablename,
172                       const void *e_void,
173                       const struct xt_target *target,
174                       void *targinfo,
175                       unsigned int hook_mask)
176 {
177         const struct ipt_tcpmss_info *tcpmssinfo = targinfo;
178         const struct ipt_entry *e = e_void;
179
180         if (tcpmssinfo->mss == IPT_TCPMSS_CLAMP_PMTU &&
181             (hook_mask & ~((1 << NF_IP_FORWARD) |
182                            (1 << NF_IP_LOCAL_OUT) |
183                            (1 << NF_IP_POST_ROUTING))) != 0) {
184                 printk("TCPMSS: path-MTU clamping only supported in "
185                        "FORWARD, OUTPUT and POSTROUTING hooks\n");
186                 return 0;
187         }
188
189         if (IPT_MATCH_ITERATE(e, find_syn_match))
190                 return 1;
191         printk("TCPMSS: Only works on TCP SYN packets\n");
192         return 0;
193 }
194
195 static struct ipt_target ipt_tcpmss_reg = {
196         .name           = "TCPMSS",
197         .target         = ipt_tcpmss_target,
198         .targetsize     = sizeof(struct ipt_tcpmss_info),
199         .proto          = IPPROTO_TCP,
200         .checkentry     = ipt_tcpmss_checkentry,
201         .me             = THIS_MODULE,
202 };
203
204 static int __init ipt_tcpmss_init(void)
205 {
206         return ipt_register_target(&ipt_tcpmss_reg);
207 }
208
209 static void __exit ipt_tcpmss_fini(void)
210 {
211         ipt_unregister_target(&ipt_tcpmss_reg);
212 }
213
214 module_init(ipt_tcpmss_init);
215 module_exit(ipt_tcpmss_fini);