xref: /freebsd/usr.sbin/ppp/tcpmss.c (revision b3e76948)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
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 #ifndef NOINET6
37 #include <netinet/ip6.h>
38 #endif
39 #include <netinet/tcp.h>
40 #include <sys/un.h>
41 
42 #include <termios.h>
43 
44 #include "layer.h"
45 #include "defs.h"
46 #include "log.h"
47 #include "timer.h"
48 #include "fsm.h"
49 #include "mbuf.h"
50 #include "throughput.h"
51 #include "lqr.h"
52 #include "hdlc.h"
53 #include "lcp.h"
54 #include "ccp.h"
55 #include "link.h"
56 #include "iplist.h"
57 #include "slcompress.h"
58 #include "ncpaddr.h"
59 #include "ipcp.h"
60 #include "filter.h"
61 #include "descriptor.h"
62 #include "mp.h"
63 #include "iface.h"
64 #ifndef NORADIUS
65 #include "radius.h"
66 #endif
67 #include "ipv6cp.h"
68 #include "ncp.h"
69 #include "bundle.h"
70 
71 
72 /*-
73  * Compute the MSS as described in RFC 6691.
74  */
75 #define MAXMSS4(mtu) ((mtu) - sizeof(struct ip) - sizeof(struct tcphdr))
76 #ifndef NOINET6
77 #define MAXMSS6(mtu) ((mtu) - sizeof(struct ip6_hdr) - sizeof(struct tcphdr))
78 #endif
79 
80 
81 /*-
82  * The following macro is used to update an
83  * internet checksum.  "acc" is a 32-bit
84  * accumulation of all the changes to the
85  * checksum (adding in old 16-bit words and
86  * subtracting out new words), and "cksum"
87  * is the checksum value to be updated.
88  */
89 #define ADJUST_CHECKSUM(acc, cksum) { \
90   acc += cksum; \
91   if (acc < 0) { \
92     acc = -acc; \
93     acc = (acc >> 16) + (acc & 0xffff); \
94     acc += acc >> 16; \
95     cksum = (u_short) ~acc; \
96   } else { \
97     acc = (acc >> 16) + (acc & 0xffff); \
98     acc += acc >> 16; \
99     cksum = (u_short) acc; \
100   } \
101 }
102 
103 static void
MSSFixup(struct tcphdr * tc,size_t pktlen,u_int16_t maxmss)104 MSSFixup(struct tcphdr *tc, size_t pktlen, u_int16_t maxmss)
105 {
106   size_t hlen, olen, optlen;
107   u_char *opt;
108   u_int16_t *mss;
109   int accumulate;
110 
111   hlen = tc->th_off << 2;
112 
113   /* Invalid header length or header without options. */
114   if (hlen <= sizeof(struct tcphdr) || hlen > pktlen)
115     return;
116 
117   /* MSS option only allowed within SYN packets. */
118   if (!(tc->th_flags & TH_SYN))
119     return;
120 
121   for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1);
122        olen > 0; olen -= optlen, opt += optlen) {
123     if (*opt == TCPOPT_EOL)
124       break;
125     else if (*opt == TCPOPT_NOP)
126       optlen = 1;
127     else {
128       optlen = *(opt + 1);
129       if (optlen <= 0 || optlen > olen)
130         break;
131       if (*opt == TCPOPT_MAXSEG) {
132         if (optlen != TCPOLEN_MAXSEG)
133           continue;
134         mss = (u_int16_t *)(opt + 2);
135         if (ntohs(*mss) > maxmss) {
136           log_Printf(LogDEBUG, "MSS: %u -> %u\n",
137                ntohs(*mss), maxmss);
138           accumulate = *mss;
139           *mss = htons(maxmss);
140           accumulate -= *mss;
141           ADJUST_CHECKSUM(accumulate, tc->th_sum);
142         }
143       }
144     }
145   }
146 }
147 
148 static struct mbuf *
tcpmss_Check(struct bundle * bundle,struct mbuf * bp)149 tcpmss_Check(struct bundle *bundle, struct mbuf *bp)
150 {
151   struct ip *pip;
152 #ifndef NOINET6
153   struct ip6_hdr *pip6;
154   struct ip6_frag *pfrag;
155 #endif
156   size_t hlen, plen;
157 
158   if (!Enabled(bundle, OPT_TCPMSSFIXUP))
159     return bp;
160 
161   bp = m_pullup(bp);
162   plen = m_length(bp);
163   if (plen < sizeof(struct ip))
164     return bp;
165   pip = (struct ip *)MBUF_CTOP(bp);
166 
167   switch (pip->ip_v) {
168   case IPVERSION:
169     /*
170      * Check for MSS option only for TCP packets with zero fragment offsets
171      * and correct total and header lengths.
172      */
173     hlen = pip->ip_hl << 2;
174     if (pip->ip_p == IPPROTO_TCP && (ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
175         ntohs(pip->ip_len) == plen && hlen <= plen &&
176         plen >= sizeof(struct tcphdr) + hlen)
177       MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
178                MAXMSS4(bundle->iface->mtu));
179     break;
180 #ifndef NOINET6
181   case IPV6_VERSION >> 4:
182     /*
183      * Check for MSS option only for TCP packets with no extension headers
184      * or a single extension header which is a fragmentation header with
185      * offset 0. Furthermore require that the length field is correct.
186      */
187     if (plen < sizeof(struct ip6_hdr))
188       break;
189     pip6 = (struct ip6_hdr *)MBUF_CTOP(bp);
190     if (ntohs(pip6->ip6_plen) + sizeof(struct ip6_hdr) != plen)
191       break;
192     hlen = 0;
193     switch (pip6->ip6_nxt) {
194     case IPPROTO_TCP:
195       hlen = sizeof(struct ip6_hdr);
196       break;
197     case IPPROTO_FRAGMENT:
198       if (plen >= sizeof(struct ip6_frag) + sizeof(struct ip6_hdr)) {
199         pfrag = (struct ip6_frag *)(MBUF_CTOP(bp) + sizeof(struct ip6_hdr));
200         if (pfrag->ip6f_nxt == IPPROTO_TCP &&
201             ntohs(pfrag->ip6f_offlg & IP6F_OFF_MASK) == 0)
202           hlen = sizeof(struct ip6_hdr)+ sizeof(struct ip6_frag);
203       }
204       break;
205     }
206     if (hlen > 0 && plen >= sizeof(struct tcphdr) + hlen)
207       MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen,
208                MAXMSS6(bundle->iface->mtu));
209     break;
210 #endif
211   default:
212     log_Printf(LogDEBUG, "tcpmss_Check: Unknown IP family %u\n", pip->ip_v);
213     break;
214   }
215   return bp;
216 }
217 
218 static struct mbuf *
tcpmss_LayerPush(struct bundle * bundle,struct link * l __unused,struct mbuf * bp,int pri __unused,u_short * proto __unused)219 tcpmss_LayerPush(struct bundle *bundle, struct link *l __unused,
220 		 struct mbuf *bp, int pri __unused, u_short *proto __unused)
221 {
222 	return tcpmss_Check(bundle, bp);
223 }
224 
225 static struct mbuf *
tcpmss_LayerPull(struct bundle * bundle,struct link * l __unused,struct mbuf * bp,u_short * proto __unused)226 tcpmss_LayerPull(struct bundle *bundle, struct link *l __unused,
227 		 struct mbuf *bp, u_short *proto __unused)
228 {
229 	return tcpmss_Check(bundle, bp);
230 }
231 
232 struct layer tcpmsslayer =
233   { LAYER_PROTO, "tcpmss", tcpmss_LayerPush, tcpmss_LayerPull };
234