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