]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/vjcomp.c
Decouple pap & chap output routines from the corresponding
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / vjcomp.c
1 /*
2  *             Input/Output VJ Compressed packets
3  *
4  *          Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5  *
6  *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the Internet Initiative Japan, Inc.  The name of the
14  * IIJ may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * $Id: vjcomp.c,v 1.24 1999/01/28 01:56:34 brian Exp $
21  *
22  *  TODO:
23  */
24 #include <sys/param.h>
25 #include <netinet/in.h>
26 #include <netinet/in_systm.h>
27 #include <netinet/ip.h>
28 #include <sys/un.h>
29
30 #include <stdio.h>
31
32 #include "mbuf.h"
33 #include "log.h"
34 #include "timer.h"
35 #include "fsm.h"
36 #include "lcpproto.h"
37 #include "slcompress.h"
38 #include "lqr.h"
39 #include "hdlc.h"
40 #include "defs.h"
41 #include "iplist.h"
42 #include "throughput.h"
43 #include "ipcp.h"
44 #include "lcp.h"
45 #include "ccp.h"
46 #include "link.h"
47 #include "filter.h"
48 #include "descriptor.h"
49 #include "mp.h"
50 #ifndef NORADIUS
51 #include "radius.h"
52 #endif
53 #include "bundle.h"
54 #include "vjcomp.h"
55
56 #define MAX_VJHEADER 16         /* Maximum size of compressed header */
57
58 void
59 vj_SendFrame(struct link *l, struct mbuf * bp, struct bundle *bundle)
60 {
61   int type;
62   u_short proto;
63   u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
64
65   log_Printf(LogDEBUG, "vj_SendFrame: COMPPROTO = %x\n",
66             bundle->ncp.ipcp.peer_compproto);
67   if (((struct ip *) MBUF_CTOP(bp))->ip_p == IPPROTO_TCP
68       && cproto == PROTO_VJCOMP) {
69     type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp),
70                            &bundle->ncp.ipcp.vj.cslc,
71                            &bundle->ncp.ipcp.vj.slstat,
72                            bundle->ncp.ipcp.peer_compproto & 0xff);
73     log_Printf(LogDEBUG, "vj_SendFrame: type = %x\n", type);
74     switch (type) {
75     case TYPE_IP:
76       proto = PROTO_IP;
77       break;
78     case TYPE_UNCOMPRESSED_TCP:
79       proto = PROTO_VJUNCOMP;
80       break;
81     case TYPE_COMPRESSED_TCP:
82       proto = PROTO_VJCOMP;
83       break;
84     default:
85       log_Printf(LogALERT, "Unknown frame type %x\n", type);
86       mbuf_Free(bp);
87       return;
88     }
89   } else
90     proto = PROTO_IP;
91
92   if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, proto, bp))
93     hdlc_Output(l, PRI_NORMAL, proto, bp);
94 }
95
96 static struct mbuf *
97 VjUncompressTcp(struct ipcp *ipcp, struct mbuf * bp, u_char type)
98 {
99   u_char *bufp;
100   int len, olen, rlen;
101   struct mbuf *nbp;
102   u_char work[MAX_HDR + MAX_VJHEADER];  /* enough to hold TCP/IP header */
103
104   olen = len = mbuf_Length(bp);
105   if (type == TYPE_UNCOMPRESSED_TCP) {
106
107     /*
108      * Uncompressed packet does NOT change its size, so that we can use mbuf
109      * space for uncompression job.
110      */
111     bufp = MBUF_CTOP(bp);
112     len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
113                             (ipcp->my_compproto >> 8) & 255);
114     if (len <= 0) {
115       mbuf_Free(bp);
116       bp = NULL;
117     }
118     return (bp);
119   }
120
121   /*
122    * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
123    * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
124    * Copy unread data info there.
125    */
126   if (len > MAX_VJHEADER)
127     len = MAX_VJHEADER;
128   rlen = len;
129   bufp = work + MAX_HDR;
130   bp = mbuf_Read(bp, bufp, rlen);
131   len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
132                           (ipcp->my_compproto >> 8) & 255);
133   if (len <= 0) {
134     mbuf_Free(bp);
135     return NULL;
136   }
137   len -= olen;
138   len += rlen;
139   nbp = mbuf_Alloc(len, MB_VJCOMP);
140   memcpy(MBUF_CTOP(nbp), bufp, len);
141   nbp->next = bp;
142   return (nbp);
143 }
144
145 struct mbuf *
146 vj_Input(struct ipcp *ipcp, struct mbuf *bp, int proto)
147 {
148   u_char type;
149
150   log_Printf(LogDEBUG, "vj_Input: proto %02x\n", proto);
151   log_DumpBp(LogDEBUG, "Raw packet info:", bp);
152
153   switch (proto) {
154   case PROTO_VJCOMP:
155     type = TYPE_COMPRESSED_TCP;
156     break;
157   case PROTO_VJUNCOMP:
158     type = TYPE_UNCOMPRESSED_TCP;
159     break;
160   default:
161     log_Printf(LogWARN, "vj_Input...???\n");
162     return (bp);
163   }
164   bp = VjUncompressTcp(ipcp, bp, type);
165   return (bp);
166 }
167
168 const char *
169 vj2asc(u_int32_t val)
170 {
171   static char asc[50];          /* The return value is used immediately */
172
173   if (val)
174     snprintf(asc, sizeof asc, "%d VJ slots %s slot compression",
175             (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
176   else
177     strcpy(asc, "VJ disabled");
178   return asc;
179 }