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