1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 5 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 6 * Internet Initiative Japan, Inc (IIJ) 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 #include <sys/param.h> 34 #include <netinet/in.h> 35 #include <netinet/in_systm.h> 36 #include <netinet/ip.h> 37 #include <sys/socket.h> 38 #include <sys/un.h> 39 40 #include <stdio.h> 41 #include <string.h> /* strlen/memcpy */ 42 #include <termios.h> 43 44 #include "layer.h" 45 #include "mbuf.h" 46 #include "log.h" 47 #include "timer.h" 48 #include "fsm.h" 49 #include "proto.h" 50 #include "slcompress.h" 51 #include "lqr.h" 52 #include "hdlc.h" 53 #include "defs.h" 54 #include "iplist.h" 55 #include "throughput.h" 56 #include "ncpaddr.h" 57 #include "ipcp.h" 58 #include "lcp.h" 59 #include "ccp.h" 60 #include "link.h" 61 #include "filter.h" 62 #include "descriptor.h" 63 #include "mp.h" 64 #ifndef NORADIUS 65 #include "radius.h" 66 #endif 67 #include "ipv6cp.h" 68 #include "ncp.h" 69 #include "bundle.h" 70 #include "vjcomp.h" 71 72 #define MAX_VJHEADER 16 /* Maximum size of compressed header */ 73 74 static struct mbuf * 75 vj_LayerPush(struct bundle *bundle, struct link *l __unused, struct mbuf *bp, 76 int pri __unused, u_short *proto) 77 { 78 int type; 79 struct ip *pip; 80 u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16; 81 82 bp = m_pullup(bp); 83 pip = (struct ip *)MBUF_CTOP(bp); 84 if (*proto == PROTO_IP && pip->ip_p == IPPROTO_TCP && 85 cproto == PROTO_VJCOMP) { 86 type = sl_compress_tcp(bp, pip, &bundle->ncp.ipcp.vj.cslc, 87 &bundle->ncp.ipcp.vj.slstat, 88 bundle->ncp.ipcp.peer_compproto & 0xff); 89 log_Printf(LogDEBUG, "vj_LayerWrite: type = %x\n", type); 90 switch (type) { 91 case TYPE_IP: 92 break; 93 94 case TYPE_UNCOMPRESSED_TCP: 95 *proto = PROTO_VJUNCOMP; 96 log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n"); 97 m_settype(bp, MB_VJOUT); 98 break; 99 100 case TYPE_COMPRESSED_TCP: 101 *proto = PROTO_VJCOMP; 102 log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n"); 103 m_settype(bp, MB_VJOUT); 104 break; 105 106 default: 107 log_Printf(LogERROR, "vj_LayerPush: Unknown frame type %x\n", type); 108 m_freem(bp); 109 return NULL; 110 } 111 } 112 113 return bp; 114 } 115 116 static struct mbuf * 117 VjUncompressTcp(struct ipcp *ipcp, struct mbuf *bp, u_char type) 118 { 119 u_char *bufp; 120 int len, olen, rlen; 121 u_char work[MAX_HDR + MAX_VJHEADER]; /* enough to hold TCP/IP header */ 122 123 bp = m_pullup(bp); 124 olen = len = m_length(bp); 125 if (type == TYPE_UNCOMPRESSED_TCP) { 126 /* 127 * Uncompressed packet does NOT change its size, so that we can use mbuf 128 * space for uncompression job. 129 */ 130 bufp = MBUF_CTOP(bp); 131 len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat, 132 (ipcp->my_compproto >> 8) & 255); 133 if (len <= 0) { 134 m_freem(bp); 135 bp = NULL; 136 } else 137 m_settype(bp, MB_VJIN); 138 return bp; 139 } 140 141 /* 142 * Handle compressed packet. 1) Read up to MAX_VJHEADER bytes into work 143 * space. 2) Try to uncompress it. 3) Compute amount of necessary space. 4) 144 * Copy unread data info there. 145 */ 146 if (len > MAX_VJHEADER) 147 len = MAX_VJHEADER; 148 rlen = len; 149 bufp = work + MAX_HDR; 150 bp = mbuf_Read(bp, bufp, rlen); 151 len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat, 152 (ipcp->my_compproto >> 8) & 255); 153 if (len <= 0) { 154 m_freem(bp); 155 return NULL; 156 } 157 len -= olen; 158 len += rlen; 159 160 bp = m_prepend(bp, bufp, len, 0); 161 m_settype(bp, MB_VJIN); 162 163 return bp; 164 } 165 166 static struct mbuf * 167 vj_LayerPull(struct bundle *bundle, struct link *l __unused, struct mbuf *bp, 168 u_short *proto) 169 { 170 u_char type; 171 172 switch (*proto) { 173 case PROTO_VJCOMP: 174 type = TYPE_COMPRESSED_TCP; 175 log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJCOMP -> PROTO_IP\n"); 176 break; 177 case PROTO_VJUNCOMP: 178 type = TYPE_UNCOMPRESSED_TCP; 179 log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJUNCOMP -> PROTO_IP\n"); 180 break; 181 default: 182 return bp; 183 } 184 185 *proto = PROTO_IP; 186 return VjUncompressTcp(&bundle->ncp.ipcp, bp, type); 187 } 188 189 const char * 190 vj2asc(u_int32_t val) 191 { 192 static char asc[50]; /* The return value is used immediately */ 193 194 if (val) 195 snprintf(asc, sizeof asc, "%d VJ slots with%s slot compression", 196 (int)((val>>8)&15)+1, val & 1 ? "" : "out"); 197 else 198 strcpy(asc, "VJ disabled"); 199 return asc; 200 } 201 202 struct layer vjlayer = { LAYER_VJ, "vj", vj_LayerPush, vj_LayerPull }; 203