xref: /freebsd/usr.sbin/ppp/tcpmss.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1 /*-
2  * Copyright (c) 2000 Ruslan Ermilov and Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 
31 #include <sys/socket.h>
32 #include <net/route.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
36 #include <netinet/tcp.h>
37 #include <sys/un.h>
38 
39 #include <termios.h>
40 
41 #include "layer.h"
42 #include "defs.h"
43 #include "log.h"
44 #include "timer.h"
45 #include "fsm.h"
46 #include "mbuf.h"
47 #include "throughput.h"
48 #include "lqr.h"
49 #include "hdlc.h"
50 #include "lcp.h"
51 #include "ccp.h"
52 #include "link.h"
53 #include "iplist.h"
54 #include "slcompress.h"
55 #include "ncpaddr.h"
56 #include "ipcp.h"
57 #include "filter.h"
58 #include "descriptor.h"
59 #include "mp.h"
60 #include "iface.h"
61 #ifndef NORADIUS
62 #include "radius.h"
63 #endif
64 #include "ipv6cp.h"
65 #include "ncp.h"
66 #include "bundle.h"
67 
68 
69 /*-
70  * We are in a liberal position about MSS
71  * (RFC 879, section 7).
72  */
73 #define MAXMSS(mtu) ((mtu) - sizeof(struct ip) - sizeof(struct tcphdr) - 12)
74 
75 
76 /*-
77  * The following macro is used to update an
78  * internet checksum.  "acc" is a 32-bit
79  * accumulation of all the changes to the
80  * checksum (adding in old 16-bit words and
81  * subtracting out new words), and "cksum"
82  * is the checksum value to be updated.
83  */
84 #define ADJUST_CHECKSUM(acc, cksum) { \
85   acc += cksum; \
86   if (acc < 0) { \
87     acc = -acc; \
88     acc = (acc >> 16) + (acc & 0xffff); \
89     acc += acc >> 16; \
90     cksum = (u_short) ~acc; \
91   } else { \
92     acc = (acc >> 16) + (acc & 0xffff); \
93     acc += acc >> 16; \
94     cksum = (u_short) acc; \
95   } \
96 }
97 
98 static void
99 MSSFixup(struct tcphdr *tc, size_t pktlen, u_int16_t maxmss)
100 {
101   size_t hlen, olen, optlen;
102   u_char *opt;
103   u_int16_t *mss;
104   int accumulate;
105 
106   hlen = tc->th_off << 2;
107 
108   /* Invalid header length or header without options. */
109   if (hlen <= sizeof(struct tcphdr) || hlen > pktlen)
110     return;
111 
112   /* MSS option only allowed within SYN packets. */
113   if (!(tc->th_flags & TH_SYN))
114     return;
115 
116   for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1);
117        olen > 0; olen -= optlen, opt += optlen) {
118     if (*opt == TCPOPT_EOL)
119       break;
120     else if (*opt == TCPOPT_NOP)
121       optlen = 1;
122     else {
123       optlen = *(opt + 1);
124       if (optlen <= 0 || optlen > olen)
125         break;
126       if (*opt == TCPOPT_MAXSEG) {
127         if (optlen != TCPOLEN_MAXSEG)
128           continue;
129         mss = (u_int16_t *)(opt + 2);
130         if (ntohs(*mss) > maxmss) {
131           log_Printf(LogDEBUG, "MSS: %u -> %u\n",
132                ntohs(*mss), maxmss);
133           accumulate = *mss;
134           *mss = htons(maxmss);
135           accumulate -= *mss;
136           ADJUST_CHECKSUM(accumulate, tc->th_sum);
137         }
138       }
139     }
140   }
141 }
142 
143 static struct mbuf *
144 tcpmss_Check(struct bundle *bundle, struct mbuf *bp)
145 {
146   struct ip *pip;
147   size_t hlen, plen;
148 
149   if (!Enabled(bundle, OPT_TCPMSSFIXUP))
150     return bp;
151 
152   bp = m_pullup(bp);
153   plen = m_length(bp);
154   pip = (struct ip *)MBUF_CTOP(bp);
155   hlen = pip->ip_hl << 2;
156 
157   /*
158    * Check for MSS option only for TCP packets with zero fragment offsets
159    * and correct total and header lengths.
160    */
161   if (pip->ip_p == IPPROTO_TCP && (ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
162       ntohs(pip->ip_len) == plen && hlen <= plen &&
163       plen >= sizeof(struct tcphdr) + hlen)
164     MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
165              MAXMSS(bundle->iface->mtu));
166 
167   return bp;
168 }
169 
170 static struct mbuf *
171 tcpmss_LayerPush(struct bundle *bundle, struct link *l __unused,
172 		 struct mbuf *bp, int pri __unused, u_short *proto __unused)
173 {
174 	return tcpmss_Check(bundle, bp);
175 }
176 
177 static struct mbuf *
178 tcpmss_LayerPull(struct bundle *bundle, struct link *l __unused,
179 		 struct mbuf *bp, u_short *proto __unused)
180 {
181 	return tcpmss_Check(bundle, bp);
182 }
183 
184 struct layer tcpmsslayer =
185   { LAYER_PROTO, "tcpmss", tcpmss_LayerPush, tcpmss_LayerPull };
186