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 <netinet/in_systm.h> 32 #include <netinet/in.h> 33 #include <netinet/ip.h> 34 #include <netinet/tcp.h> 35 #include <sys/un.h> 36 37 #include <termios.h> 38 39 #include "layer.h" 40 #include "defs.h" 41 #include "log.h" 42 #include "timer.h" 43 #include "fsm.h" 44 #include "mbuf.h" 45 #include "throughput.h" 46 #include "lqr.h" 47 #include "hdlc.h" 48 #include "lcp.h" 49 #include "ccp.h" 50 #include "link.h" 51 #include "iplist.h" 52 #include "slcompress.h" 53 #include "ipcp.h" 54 #include "filter.h" 55 #include "descriptor.h" 56 #include "mp.h" 57 #ifndef NORADIUS 58 #include "radius.h" 59 #endif 60 #include "bundle.h" 61 62 63 /*- 64 * We are in a liberal position about MSS 65 * (RFC 879, section 7). 66 */ 67 #define MAXMSS(mtu) (mtu - sizeof(struct ip) - sizeof(struct tcphdr)) 68 69 70 static void MSSFixup(struct tcphdr *, ssize_t, u_int16_t); 71 72 73 static struct mbuf * 74 tcpmss_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp, 75 int pri, u_short *proto) 76 { 77 struct ip *pip; 78 int hlen, plen; 79 80 if (!Enabled(bundle, OPT_TCPMSSFIXUP)) 81 return bp; 82 83 bp = m_pullup(bp); 84 plen = m_length(bp); 85 pip = (struct ip *)MBUF_CTOP(bp); 86 hlen = pip->ip_hl << 2; 87 88 /* 89 * Check for MSS option only for TCP packets with zero fragment offsets 90 * and correct total and header lengths. 91 */ 92 if (pip->ip_p == IPPROTO_TCP && (ntohs(pip->ip_off) & IP_OFFMASK) == 0 && 93 ntohs(pip->ip_len) == plen && hlen <= plen && 94 plen - hlen >= sizeof(struct tcphdr)) 95 MSSFixup((struct tcphdr *)(MBUF_CTOP(bp) + hlen), plen - hlen, 96 MAXMSS(bundle->mtu)); 97 98 return bp; 99 } 100 101 /*- 102 * The following macro is used to update an 103 * internet checksum. "acc" is a 32-bit 104 * accumulation of all the changes to the 105 * checksum (adding in old 16-bit words and 106 * subtracting out new words), and "cksum" 107 * is the checksum value to be updated. 108 */ 109 #define ADJUST_CHECKSUM(acc, cksum) { \ 110 acc += cksum; \ 111 if (acc < 0) { \ 112 acc = -acc; \ 113 acc = (acc >> 16) + (acc & 0xffff); \ 114 acc += acc >> 16; \ 115 cksum = (u_short) ~acc; \ 116 } else { \ 117 acc = (acc >> 16) + (acc & 0xffff); \ 118 acc += acc >> 16; \ 119 cksum = (u_short) acc; \ 120 } \ 121 } 122 123 static void 124 MSSFixup(struct tcphdr *tc, ssize_t pktlen, u_int16_t maxmss) 125 { 126 int hlen, olen, optlen; 127 u_char *opt; 128 u_int16_t *mss; 129 int accumulate; 130 131 hlen = tc->th_off << 2; 132 133 /* Invalid header length or header without options. */ 134 if (hlen <= sizeof(struct tcphdr) || hlen > pktlen) 135 return; 136 137 /* MSS option only allowed within SYN packets. */ 138 if (!(tc->th_flags & TH_SYN)) 139 return; 140 141 for (olen = hlen - sizeof(struct tcphdr), opt = (u_char *)(tc + 1); 142 olen > 0; olen -= optlen, opt += optlen) { 143 if (*opt == TCPOPT_EOL) 144 break; 145 else if (*opt == TCPOPT_NOP) 146 optlen = 1; 147 else { 148 optlen = *(opt + 1); 149 if (optlen <= 0 || optlen > olen) 150 break; 151 if (*opt == TCPOPT_MAXSEG) { 152 if (optlen != TCPOLEN_MAXSEG) 153 continue; 154 mss = (u_int16_t *)(opt + 2); 155 if (ntohs(*mss) > maxmss) { 156 log_Printf(LogDEBUG, "MSS: %u -> %u\n", 157 ntohs(*mss), maxmss); 158 accumulate = *mss; 159 *mss = htons(maxmss); 160 accumulate -= *mss; 161 ADJUST_CHECKSUM(accumulate, tc->th_sum); 162 } 163 } 164 } 165 } 166 } 167 168 struct layer tcpmsslayer = { LAYER_PROTO, "tcpmss", tcpmss_LayerPush, NULL }; 169