blob: 9e204d9d0cdf7da1457044c213fe4ab8999b7fdf [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * ppp_comp.c - STREAMS module for kernel-level compression and CCP support.
3 *
4 * Copyright (c) 1994 Paul Mackerras. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
21 *
22 * 4. Redistributions of any form whatsoever must retain the following
23 * acknowledgment:
24 * "This product includes software developed by Paul Mackerras
25 * <paulus@samba.org>".
26 *
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
lh9ed821d2023-04-07 01:36:19 -070034 */
35
36/*
37 * This file is used under SVR4, Solaris 2, SunOS 4, and Digital UNIX.
38 */
39
40#include <sys/types.h>
41#include <sys/param.h>
42#include <sys/errno.h>
43#include <sys/stream.h>
44
45#ifdef SVR4
46#include <sys/conf.h>
47#include <sys/cmn_err.h>
48#include <sys/ddi.h>
49#else
50#include <sys/user.h>
51#ifdef __osf__
52#include <sys/cmn_err.h>
53#endif
54#endif /* SVR4 */
55
56#include <net/ppp_defs.h>
57#include <net/pppio.h>
58#include "ppp_mod.h"
59
60#ifdef __osf__
61#include <sys/mbuf.h>
62#include <sys/protosw.h>
63#endif
64
65#include <netinet/in.h>
66#include <netinet/in_systm.h>
67#include <netinet/ip.h>
68#include <net/vjcompress.h>
69
70#define PACKETPTR mblk_t *
71#include <net/ppp-comp.h>
72
73MOD_OPEN_DECL(ppp_comp_open);
74MOD_CLOSE_DECL(ppp_comp_close);
xf.li84027492024-04-09 00:17:51 -070075static int ppp_comp_rput(queue_t *, mblk_t *);
76static int ppp_comp_rsrv(queue_t *);
77static int ppp_comp_wput(queue_t *, mblk_t *);
78static int ppp_comp_wsrv(queue_t *);
79static void ppp_comp_ccp(queue_t *, mblk_t *, int);
80static int msg_byte(mblk_t *, unsigned int);
lh9ed821d2023-04-07 01:36:19 -070081
82/* Extract byte i of message mp. */
83#define MSG_BYTE(mp, i) ((i) < (mp)->b_wptr - (mp)->b_rptr? (mp)->b_rptr[i]: \
84 msg_byte((mp), (i)))
85
86/* Is this LCP packet one we have to transmit using LCP defaults? */
87#define LCP_USE_DFLT(mp) (1 <= (code = MSG_BYTE((mp), 4)) && code <= 7)
88
89#define PPP_COMP_ID 0xbadf
90static struct module_info minfo = {
91#ifdef PRIOQ
92 PPP_COMP_ID, "ppp_comp", 0, INFPSZ, 16512, 16384,
93#else
94 PPP_COMP_ID, "ppp_comp", 0, INFPSZ, 16384, 4096,
95#endif
96};
97
98static struct qinit r_init = {
99 ppp_comp_rput, ppp_comp_rsrv, ppp_comp_open, ppp_comp_close,
100 NULL, &minfo, NULL
101};
102
103static struct qinit w_init = {
104 ppp_comp_wput, ppp_comp_wsrv, NULL, NULL, NULL, &minfo, NULL
105};
106
107#if defined(SVR4) && !defined(SOL2)
108int pcmpdevflag = 0;
109#define ppp_compinfo pcmpinfo
110#endif
111struct streamtab ppp_compinfo = {
112 &r_init, &w_init, NULL, NULL
113};
114
115int ppp_comp_count; /* number of module instances in use */
116
117#ifdef __osf__
118
xf.li84027492024-04-09 00:17:51 -0700119static void ppp_comp_alloc(comp_state_t *);
lh9ed821d2023-04-07 01:36:19 -0700120typedef struct memreq {
121 unsigned char comp_opts[20];
122 int cmd;
123 int thread_status;
124 char *returned_mem;
125} memreq_t;
126
127#endif
128
129typedef struct comp_state {
130 int flags;
131 int mru;
132 int mtu;
133 int unit;
134 struct compressor *xcomp;
135 void *xstate;
136 struct compressor *rcomp;
137 void *rstate;
138 struct vjcompress vj_comp;
139 int vj_last_ierrors;
140 struct pppstat stats;
141#ifdef __osf__
142 memreq_t memreq;
143 thread_t thread;
144#endif
145} comp_state_t;
146
147
148#ifdef __osf__
149extern task_t first_task;
150#endif
151
152/* Bits in flags are as defined in pppio.h. */
153#define CCP_ERR (CCP_ERROR | CCP_FATALERROR)
154#define LAST_MOD 0x1000000 /* no ppp modules below us */
155#define DBGLOG 0x2000000 /* log debugging stuff */
156
157#define MAX_IPHDR 128 /* max TCP/IP header size */
158#define MAX_VJHDR 20 /* max VJ compressed header size (?) */
159
160#undef MIN /* just in case */
161#define MIN(a, b) ((a) < (b)? (a): (b))
162
163/*
164 * List of compressors we know about.
165 */
166
167#if DO_BSD_COMPRESS
168extern struct compressor ppp_bsd_compress;
169#endif
170#if DO_DEFLATE
171extern struct compressor ppp_deflate, ppp_deflate_draft;
172#endif
173
174struct compressor *ppp_compressors[] = {
175#if DO_BSD_COMPRESS
176 &ppp_bsd_compress,
177#endif
178#if DO_DEFLATE
179 &ppp_deflate,
180 &ppp_deflate_draft,
181#endif
182 NULL
183};
184
185/*
186 * STREAMS module entry points.
187 */
188MOD_OPEN(ppp_comp_open)
189{
190 comp_state_t *cp;
191#ifdef __osf__
192 thread_t thread;
193#endif
194
195 if (q->q_ptr == NULL) {
196 cp = (comp_state_t *) ALLOC_SLEEP(sizeof(comp_state_t));
197 if (cp == NULL)
198 OPEN_ERROR(ENOSR);
199 bzero((caddr_t)cp, sizeof(comp_state_t));
200 WR(q)->q_ptr = q->q_ptr = (caddr_t) cp;
201 cp->mru = PPP_MRU;
202 cp->mtu = PPP_MTU;
203 cp->xstate = NULL;
204 cp->rstate = NULL;
205 vj_compress_init(&cp->vj_comp, -1);
206#ifdef __osf__
207 if (!(thread = kernel_thread_w_arg(first_task, ppp_comp_alloc, (void *)cp)))
208 OPEN_ERROR(ENOSR);
209 cp->thread = thread;
210#endif
211 ++ppp_comp_count;
212 qprocson(q);
213 }
214 return 0;
215}
216
217MOD_CLOSE(ppp_comp_close)
218{
219 comp_state_t *cp;
220
221 qprocsoff(q);
222 cp = (comp_state_t *) q->q_ptr;
223 if (cp != NULL) {
224 if (cp->xstate != NULL)
225 (*cp->xcomp->comp_free)(cp->xstate);
226 if (cp->rstate != NULL)
227 (*cp->rcomp->decomp_free)(cp->rstate);
228#ifdef __osf__
229 if (!cp->thread)
230 printf("ppp_comp_close: NULL thread!\n");
231 else
232 thread_terminate(cp->thread);
233#endif
234 FREE(cp, sizeof(comp_state_t));
235 q->q_ptr = NULL;
236 OTHERQ(q)->q_ptr = NULL;
237 --ppp_comp_count;
238 }
239 return 0;
240}
241
242#ifdef __osf__
243
244/* thread for calling back to a compressor's memory allocator
245 * Needed for Digital UNIX since it's VM can't handle requests
246 * for large amounts of memory without blocking. The thread
247 * provides a context in which we can call a memory allocator
248 * that may block.
249 */
250static void
251ppp_comp_alloc(comp_state_t *cp)
252{
253 int len, cmd;
254 unsigned char *compressor_options;
255 thread_t thread;
256 void *(*comp_allocator)();
257
258
259#if defined(MAJOR_VERSION) && (MAJOR_VERSION <= 2)
260
261 /* In 2.x and earlier the argument gets passed
262 * in the thread structure itself. Yuck.
263 */
264 thread = current_thread();
265 cp = thread->reply_port;
266 thread->reply_port = PORT_NULL;
267
268#endif
269
270 for (;;) {
271 assert_wait((vm_offset_t)&cp->memreq.thread_status, TRUE);
272 thread_block();
273
274 if (thread_should_halt(current_thread()))
275 thread_halt_self();
276 cmd = cp->memreq.cmd;
277 compressor_options = &cp->memreq.comp_opts[0];
278 len = compressor_options[1];
279 if (cmd == PPPIO_XCOMP) {
280 cp->memreq.returned_mem = cp->xcomp->comp_alloc(compressor_options, len);
281 if (!cp->memreq.returned_mem) {
282 cp->memreq.thread_status = ENOSR;
283 } else {
284 cp->memreq.thread_status = 0;
285 }
286 } else {
287 cp->memreq.returned_mem = cp->rcomp->decomp_alloc(compressor_options, len);
288 if (!cp->memreq.returned_mem) {
289 cp->memreq.thread_status = ENOSR;
290 } else {
291 cp->memreq.thread_status = 0;
292 }
293 }
294 }
295}
296
297#endif /* __osf__ */
298
299/* here's the deal with memory allocation under Digital UNIX.
300 * Some other may also benefit from this...
301 * We can't ask for huge chunks of memory in a context where
302 * the caller can't be put to sleep (like, here.) The alloc
303 * is likely to fail. Instead we do this: the first time we
304 * get called, kick off a thread to do the allocation. Return
305 * immediately to the caller with EAGAIN, as an indication that
306 * they should send down the ioctl again. By the time the
307 * second call comes in it's likely that the memory allocation
308 * thread will have returned with the requested memory. We will
309 * continue to return EAGAIN however until the thread has completed.
310 * When it has, we return zero (and the memory) if the allocator
311 * was successful and ENOSR otherwise.
312 *
313 * Callers of the RCOMP and XCOMP ioctls are encouraged (but not
314 * required) to loop for some number of iterations with a small
315 * delay in the loop body (for instance a 1/10-th second "sleep"
316 * via select.)
317 */
318static int
319ppp_comp_wput(q, mp)
320 queue_t *q;
321 mblk_t *mp;
322{
323 struct iocblk *iop;
324 comp_state_t *cp;
325 int error, len, n;
326 int flags, mask;
327 mblk_t *np;
328 struct compressor **comp;
329 struct ppp_stats *psp;
330 struct ppp_comp_stats *csp;
331 unsigned char *opt_data;
332 int nxslots, nrslots;
333
334 cp = (comp_state_t *) q->q_ptr;
335 if (cp == 0) {
336 DPRINT("cp == 0 in ppp_comp_wput\n");
337 freemsg(mp);
338 return 0;
339 }
340
341 switch (mp->b_datap->db_type) {
342
343 case M_DATA:
344 putq(q, mp);
345 break;
346
347 case M_IOCTL:
348 iop = (struct iocblk *) mp->b_rptr;
349 error = EINVAL;
350 switch (iop->ioc_cmd) {
351
352 case PPPIO_CFLAGS:
353 /* set/get CCP state */
354 if (iop->ioc_count != 2 * sizeof(int))
355 break;
356 if (mp->b_cont == 0) {
357 DPRINT1("ppp_comp_wput/%d: PPPIO_CFLAGS b_cont = 0!\n", cp->unit);
358 break;
359 }
360 flags = ((int *) mp->b_cont->b_rptr)[0];
361 mask = ((int *) mp->b_cont->b_rptr)[1];
362 cp->flags = (cp->flags & ~mask) | (flags & mask);
363 if ((mask & CCP_ISOPEN) && (flags & CCP_ISOPEN) == 0) {
364 if (cp->xstate != NULL) {
365 (*cp->xcomp->comp_free)(cp->xstate);
366 cp->xstate = NULL;
367 }
368 if (cp->rstate != NULL) {
369 (*cp->rcomp->decomp_free)(cp->rstate);
370 cp->rstate = NULL;
371 }
372 cp->flags &= ~CCP_ISUP;
373 }
374 error = 0;
375 iop->ioc_count = sizeof(int);
376 ((int *) mp->b_cont->b_rptr)[0] = cp->flags;
377 mp->b_cont->b_wptr = mp->b_cont->b_rptr + sizeof(int);
378 break;
379
380 case PPPIO_VJINIT:
381 /*
382 * Initialize VJ compressor/decompressor
383 */
384 if (iop->ioc_count != 2)
385 break;
386 if (mp->b_cont == 0) {
387 DPRINT1("ppp_comp_wput/%d: PPPIO_VJINIT b_cont = 0!\n", cp->unit);
388 break;
389 }
390 nxslots = mp->b_cont->b_rptr[0] + 1;
391 nrslots = mp->b_cont->b_rptr[1] + 1;
392 if (nxslots > MAX_STATES || nrslots > MAX_STATES)
393 break;
394 vj_compress_init(&cp->vj_comp, nxslots);
395 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
396 error = 0;
397 iop->ioc_count = 0;
398 break;
399
400 case PPPIO_XCOMP:
401 case PPPIO_RCOMP:
402 if (iop->ioc_count <= 0)
403 break;
404 if (mp->b_cont == 0) {
405 DPRINT1("ppp_comp_wput/%d: PPPIO_[XR]COMP b_cont = 0!\n", cp->unit);
406 break;
407 }
408 opt_data = mp->b_cont->b_rptr;
409 len = mp->b_cont->b_wptr - opt_data;
410 if (len > iop->ioc_count)
411 len = iop->ioc_count;
412 if (opt_data[1] < 2 || opt_data[1] > len)
413 break;
414 for (comp = ppp_compressors; *comp != NULL; ++comp)
415 if ((*comp)->compress_proto == opt_data[0]) {
416 /* here's the handler! */
417 error = 0;
418#ifndef __osf__
419 if (iop->ioc_cmd == PPPIO_XCOMP) {
420 /* A previous call may have fetched memory for a compressor
421 * that's now being retired or reset. Free it using it's
422 * mechanism for freeing stuff.
423 */
424 if (cp->xstate != NULL) {
425 (*cp->xcomp->comp_free)(cp->xstate);
426 cp->xstate = NULL;
427 }
428 cp->xcomp = *comp;
429 cp->xstate = (*comp)->comp_alloc(opt_data, len);
430 if (cp->xstate == NULL)
431 error = ENOSR;
432 } else {
433 if (cp->rstate != NULL) {
434 (*cp->rcomp->decomp_free)(cp->rstate);
435 cp->rstate = NULL;
436 }
437 cp->rcomp = *comp;
438 cp->rstate = (*comp)->decomp_alloc(opt_data, len);
439 if (cp->rstate == NULL)
440 error = ENOSR;
441 }
442#else
443 if ((error = cp->memreq.thread_status) != EAGAIN)
444 if (iop->ioc_cmd == PPPIO_XCOMP) {
445 if (cp->xstate) {
446 (*cp->xcomp->comp_free)(cp->xstate);
447 cp->xstate = 0;
448 }
449 /* sanity check for compressor options
450 */
451 if (sizeof (cp->memreq.comp_opts) < len) {
452 printf("can't handle options for compressor %d (%d)\n", opt_data[0],
453 opt_data[1]);
454 cp->memreq.thread_status = ENOSR;
455 cp->memreq.returned_mem = 0;
456 }
457 /* fill in request for the thread and kick it off
458 */
459 if (cp->memreq.thread_status == 0 && !cp->memreq.returned_mem) {
460 bcopy(opt_data, cp->memreq.comp_opts, len);
461 cp->memreq.cmd = PPPIO_XCOMP;
462 cp->xcomp = *comp;
463 error = cp->memreq.thread_status = EAGAIN;
464 thread_wakeup((vm_offset_t)&cp->memreq.thread_status);
465 } else {
466 cp->xstate = cp->memreq.returned_mem;
467 cp->memreq.returned_mem = 0;
468 cp->memreq.thread_status = 0;
469 }
470 } else {
471 if (cp->rstate) {
472 (*cp->rcomp->decomp_free)(cp->rstate);
473 cp->rstate = NULL;
474 }
475 if (sizeof (cp->memreq.comp_opts) < len) {
476 printf("can't handle options for compressor %d (%d)\n", opt_data[0],
477 opt_data[1]);
478 cp->memreq.thread_status = ENOSR;
479 cp->memreq.returned_mem = 0;
480 }
481 if (cp->memreq.thread_status == 0 && !cp->memreq.returned_mem) {
482 bcopy(opt_data, cp->memreq.comp_opts, len);
483 cp->memreq.cmd = PPPIO_RCOMP;
484 cp->rcomp = *comp;
485 error = cp->memreq.thread_status = EAGAIN;
486 thread_wakeup((vm_offset_t)&cp->memreq.thread_status);
487 } else {
488 cp->rstate = cp->memreq.returned_mem;
489 cp->memreq.returned_mem = 0;
490 cp->memreq.thread_status = 0;
491 }
492 }
493#endif
494 break;
495 }
496 iop->ioc_count = 0;
497 break;
498
499 case PPPIO_GETSTAT:
500 if ((cp->flags & LAST_MOD) == 0) {
501 error = -1; /* let the ppp_ahdl module handle it */
502 break;
503 }
504 np = allocb(sizeof(struct ppp_stats), BPRI_HI);
505 if (np == 0) {
506 error = ENOSR;
507 break;
508 }
509 if (mp->b_cont != 0)
510 freemsg(mp->b_cont);
511 mp->b_cont = np;
512 psp = (struct ppp_stats *) np->b_wptr;
513 np->b_wptr += sizeof(struct ppp_stats);
514 iop->ioc_count = sizeof(struct ppp_stats);
515 psp->p = cp->stats;
516 psp->vj = cp->vj_comp.stats;
517 error = 0;
518 break;
519
520 case PPPIO_GETCSTAT:
521 np = allocb(sizeof(struct ppp_comp_stats), BPRI_HI);
522 if (np == 0) {
523 error = ENOSR;
524 break;
525 }
526 if (mp->b_cont != 0)
527 freemsg(mp->b_cont);
528 mp->b_cont = np;
529 csp = (struct ppp_comp_stats *) np->b_wptr;
530 np->b_wptr += sizeof(struct ppp_comp_stats);
531 iop->ioc_count = sizeof(struct ppp_comp_stats);
532 bzero((caddr_t)csp, sizeof(struct ppp_comp_stats));
533 if (cp->xstate != 0)
534 (*cp->xcomp->comp_stat)(cp->xstate, &csp->c);
535 if (cp->rstate != 0)
536 (*cp->rcomp->decomp_stat)(cp->rstate, &csp->d);
537 error = 0;
538 break;
539
540 case PPPIO_DEBUG:
541 if (iop->ioc_count != sizeof(int))
542 break;
543 if (mp->b_cont == 0) {
544 DPRINT1("ppp_comp_wput/%d: PPPIO_DEBUG b_cont = 0!\n", cp->unit);
545 break;
546 }
547 n = *(int *)mp->b_cont->b_rptr;
548 if (n == PPPDBG_LOG + PPPDBG_COMP) {
549 DPRINT1("ppp_comp%d: debug log enabled\n", cp->unit);
550 cp->flags |= DBGLOG;
551 error = 0;
552 iop->ioc_count = 0;
553 } else {
554 error = -1;
555 }
556 break;
557
558 case PPPIO_LASTMOD:
559 cp->flags |= LAST_MOD;
560 error = 0;
561 break;
562
563 default:
564 error = -1;
565 break;
566 }
567
568 if (error < 0)
569 putnext(q, mp);
570 else if (error == 0) {
571 mp->b_datap->db_type = M_IOCACK;
572 qreply(q, mp);
573 } else {
574 mp->b_datap->db_type = M_IOCNAK;
575 iop->ioc_error = error;
576 iop->ioc_count = 0;
577 qreply(q, mp);
578 }
579 break;
580
581 case M_CTL:
582 switch (*mp->b_rptr) {
583 case PPPCTL_MTU:
584 cp->mtu = ((unsigned short *)mp->b_rptr)[1];
585 break;
586 case PPPCTL_MRU:
587 cp->mru = ((unsigned short *)mp->b_rptr)[1];
588 break;
589 case PPPCTL_UNIT:
590 cp->unit = mp->b_rptr[1];
591 break;
592 }
593 putnext(q, mp);
594 break;
595
596 default:
597 putnext(q, mp);
598 }
599
600 return 0;
601}
602
603static int
604ppp_comp_wsrv(q)
605 queue_t *q;
606{
607 mblk_t *mp, *cmp = NULL;
608 comp_state_t *cp;
609 int len, proto, type, hlen, code;
610 struct ip *ip;
611 unsigned char *vjhdr, *dp;
612
613 cp = (comp_state_t *) q->q_ptr;
614 if (cp == 0) {
615 DPRINT("cp == 0 in ppp_comp_wsrv\n");
616 return 0;
617 }
618
619 while ((mp = getq(q)) != 0) {
620 /* assert(mp->b_datap->db_type == M_DATA) */
621#ifdef PRIOQ
622 if (!bcanputnext(q,mp->b_band))
623#else
624 if (!canputnext(q))
625#endif /* PRIOQ */
626 {
627 putbq(q, mp);
628 break;
629 }
630
631 /*
632 * First check the packet length and work out what the protocol is.
633 */
634 len = msgdsize(mp);
635 if (len < PPP_HDRLEN) {
636 DPRINT1("ppp_comp_wsrv: bogus short packet (%d)\n", len);
637 freemsg(mp);
638 cp->stats.ppp_oerrors++;
639 putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
640 continue;
641 }
642 proto = (MSG_BYTE(mp, 2) << 8) + MSG_BYTE(mp, 3);
643
644 /*
645 * Make sure we've got enough data in the first mblk
646 * and that we are its only user.
647 */
648 if (proto == PPP_CCP)
649 hlen = len;
650 else if (proto == PPP_IP)
651 hlen = PPP_HDRLEN + MAX_IPHDR;
652 else
653 hlen = PPP_HDRLEN;
654 if (hlen > len)
655 hlen = len;
656 if (mp->b_wptr < mp->b_rptr + hlen || mp->b_datap->db_ref > 1) {
657 PULLUP(mp, hlen);
658 if (mp == 0) {
659 DPRINT1("ppp_comp_wsrv: pullup failed (%d)\n", hlen);
660 cp->stats.ppp_oerrors++;
661 putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
662 continue;
663 }
664 }
665
666 /*
667 * Do VJ compression if requested.
668 */
669 if (proto == PPP_IP && (cp->flags & COMP_VJC)) {
670 ip = (struct ip *) (mp->b_rptr + PPP_HDRLEN);
671 if (ip->ip_p == IPPROTO_TCP) {
672 type = vj_compress_tcp(ip, len - PPP_HDRLEN, &cp->vj_comp,
673 (cp->flags & COMP_VJCCID), &vjhdr);
674 switch (type) {
675 case TYPE_UNCOMPRESSED_TCP:
676 mp->b_rptr[3] = proto = PPP_VJC_UNCOMP;
677 break;
678 case TYPE_COMPRESSED_TCP:
679 dp = vjhdr - PPP_HDRLEN;
680 dp[1] = mp->b_rptr[1]; /* copy control field */
681 dp[0] = mp->b_rptr[0]; /* copy address field */
682 dp[2] = 0; /* set protocol field */
683 dp[3] = proto = PPP_VJC_COMP;
684 mp->b_rptr = dp;
685 break;
686 }
687 }
688 }
689
690 /*
691 * Do packet compression if enabled.
692 */
693 if (proto == PPP_CCP)
694 ppp_comp_ccp(q, mp, 0);
695 else if (proto != PPP_LCP && (cp->flags & CCP_COMP_RUN)
696 && cp->xstate != NULL) {
697 len = msgdsize(mp);
698 (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
699 (cp->flags & CCP_ISUP? cp->mtu + PPP_HDRLEN: 0));
700 if (cmp != NULL) {
701#ifdef PRIOQ
702 cmp->b_band=mp->b_band;
703#endif /* PRIOQ */
704 freemsg(mp);
705 mp = cmp;
706 }
707 }
708
709 /*
710 * Do address/control and protocol compression if enabled.
711 */
712 if ((cp->flags & COMP_AC)
713 && !(proto == PPP_LCP && LCP_USE_DFLT(mp))) {
714 mp->b_rptr += 2; /* drop the address & ctrl fields */
715 if (proto < 0x100 && (cp->flags & COMP_PROT))
716 ++mp->b_rptr; /* drop the high protocol byte */
717 } else if (proto < 0x100 && (cp->flags & COMP_PROT)) {
718 /* shuffle up the address & ctrl fields */
719 mp->b_rptr[2] = mp->b_rptr[1];
720 mp->b_rptr[1] = mp->b_rptr[0];
721 ++mp->b_rptr;
722 }
723
724 cp->stats.ppp_opackets++;
725 cp->stats.ppp_obytes += msgdsize(mp);
726 putnext(q, mp);
727 }
728
729 return 0;
730}
731
732static int
733ppp_comp_rput(q, mp)
734 queue_t *q;
735 mblk_t *mp;
736{
737 comp_state_t *cp;
738 struct iocblk *iop;
739 struct ppp_stats *psp;
740
741 cp = (comp_state_t *) q->q_ptr;
742 if (cp == 0) {
743 DPRINT("cp == 0 in ppp_comp_rput\n");
744 freemsg(mp);
745 return 0;
746 }
747
748 switch (mp->b_datap->db_type) {
749
750 case M_DATA:
751 putq(q, mp);
752 break;
753
754 case M_IOCACK:
755 iop = (struct iocblk *) mp->b_rptr;
756 switch (iop->ioc_cmd) {
757 case PPPIO_GETSTAT:
758 /*
759 * Catch this on the way back from the ppp_ahdl module
760 * so we can fill in the VJ stats.
761 */
762 if (mp->b_cont == 0 || iop->ioc_count != sizeof(struct ppp_stats))
763 break;
764 psp = (struct ppp_stats *) mp->b_cont->b_rptr;
765 psp->vj = cp->vj_comp.stats;
766 break;
767 }
768 putnext(q, mp);
769 break;
770
771 case M_CTL:
772 switch (mp->b_rptr[0]) {
773 case PPPCTL_IERROR:
774 ++cp->stats.ppp_ierrors;
775 break;
776 case PPPCTL_OERROR:
777 ++cp->stats.ppp_oerrors;
778 break;
779 }
780 putnext(q, mp);
781 break;
782
783 default:
784 putnext(q, mp);
785 }
786
787 return 0;
788}
789
790static int
791ppp_comp_rsrv(q)
792 queue_t *q;
793{
794 int proto, rv, i;
795 mblk_t *mp, *dmp = NULL, *np;
796 uchar_t *dp, *iphdr;
797 comp_state_t *cp;
798 int len, hlen, vjlen;
799 u_int iphlen;
800
801 cp = (comp_state_t *) q->q_ptr;
802 if (cp == 0) {
803 DPRINT("cp == 0 in ppp_comp_rsrv\n");
804 return 0;
805 }
806
807 while ((mp = getq(q)) != 0) {
808 /* assert(mp->b_datap->db_type == M_DATA) */
809 if (!canputnext(q)) {
810 putbq(q, mp);
811 break;
812 }
813
814 len = msgdsize(mp);
815 cp->stats.ppp_ibytes += len;
816 cp->stats.ppp_ipackets++;
817
818 /*
819 * First work out the protocol and where the PPP header ends.
820 */
821 i = 0;
822 proto = MSG_BYTE(mp, 0);
823 if (proto == PPP_ALLSTATIONS) {
824 i = 2;
825 proto = MSG_BYTE(mp, 2);
826 }
827 if ((proto & 1) == 0) {
828 ++i;
829 proto = (proto << 8) + MSG_BYTE(mp, i);
830 }
831 hlen = i + 1;
832
833 /*
834 * Now reconstruct a complete, contiguous PPP header at the
835 * start of the packet.
836 */
837 if (hlen < ((cp->flags & DECOMP_AC)? 0: 2)
838 + ((cp->flags & DECOMP_PROT)? 1: 2)) {
839 /* count these? */
840 goto bad;
841 }
842 if (mp->b_rptr + hlen > mp->b_wptr) {
843 adjmsg(mp, hlen); /* XXX check this call */
844 hlen = 0;
845 }
846 if (hlen != PPP_HDRLEN) {
847 /*
848 * We need to put some bytes on the front of the packet
849 * to make a full-length PPP header.
850 * If we can put them in *mp, we do, otherwise we
851 * tack another mblk on the front.
852 * XXX we really shouldn't need to carry around
853 * the address and control at this stage.
854 */
855 dp = mp->b_rptr + hlen - PPP_HDRLEN;
856 if (dp < mp->b_datap->db_base || mp->b_datap->db_ref > 1) {
857 np = allocb(PPP_HDRLEN, BPRI_MED);
858 if (np == 0)
859 goto bad;
860 np->b_cont = mp;
861 mp->b_rptr += hlen;
862 mp = np;
863 dp = mp->b_wptr;
864 mp->b_wptr += PPP_HDRLEN;
865 } else
866 mp->b_rptr = dp;
867
868 dp[0] = PPP_ALLSTATIONS;
869 dp[1] = PPP_UI;
870 dp[2] = proto >> 8;
871 dp[3] = proto;
872 }
873
874 /*
875 * Now see if we have a compressed packet to decompress,
876 * or a CCP packet to take notice of.
877 */
878 proto = PPP_PROTOCOL(mp->b_rptr);
879 if (proto == PPP_CCP) {
880 len = msgdsize(mp);
881 if (mp->b_wptr < mp->b_rptr + len) {
882 PULLUP(mp, len);
883 if (mp == 0)
884 goto bad;
885 }
886 ppp_comp_ccp(q, mp, 1);
887 } else if (proto == PPP_COMP) {
888 if ((cp->flags & CCP_ISUP)
889 && (cp->flags & CCP_DECOMP_RUN) && cp->rstate
890 && (cp->flags & CCP_ERR) == 0) {
891 rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
892 switch (rv) {
893 case DECOMP_OK:
894 freemsg(mp);
895 mp = dmp;
896 if (mp == NULL) {
897 /* no error, but no packet returned either. */
898 continue;
899 }
900 break;
901 case DECOMP_ERROR:
902 cp->flags |= CCP_ERROR;
903 ++cp->stats.ppp_ierrors;
904 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
905 break;
906 case DECOMP_FATALERROR:
907 cp->flags |= CCP_FATALERROR;
908 ++cp->stats.ppp_ierrors;
909 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
910 break;
911 }
912 }
913 } else if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
914 (*cp->rcomp->incomp)(cp->rstate, mp);
915 }
916
917 /*
918 * Now do VJ decompression.
919 */
920 proto = PPP_PROTOCOL(mp->b_rptr);
921 if (proto == PPP_VJC_COMP || proto == PPP_VJC_UNCOMP) {
922 len = msgdsize(mp) - PPP_HDRLEN;
923 if ((cp->flags & DECOMP_VJC) == 0 || len <= 0)
924 goto bad;
925
926 /*
927 * Advance past the ppp header.
928 * Here we assume that the whole PPP header is in the first mblk.
929 */
930 np = mp;
931 dp = np->b_rptr + PPP_HDRLEN;
932 if (dp >= mp->b_wptr) {
933 np = np->b_cont;
934 dp = np->b_rptr;
935 }
936
937 /*
938 * Make sure we have sufficient contiguous data at this point.
939 */
940 hlen = (proto == PPP_VJC_COMP)? MAX_VJHDR: MAX_IPHDR;
941 if (hlen > len)
942 hlen = len;
943 if (np->b_wptr < dp + hlen || np->b_datap->db_ref > 1) {
944 PULLUP(mp, hlen + PPP_HDRLEN);
945 if (mp == 0)
946 goto bad;
947 np = mp;
948 dp = np->b_rptr + PPP_HDRLEN;
949 }
950
951 if (proto == PPP_VJC_COMP) {
952 /*
953 * Decompress VJ-compressed packet.
954 * First reset compressor if an input error has occurred.
955 */
956 if (cp->stats.ppp_ierrors != cp->vj_last_ierrors) {
957 if (cp->flags & DBGLOG)
958 DPRINT1("ppp%d: resetting VJ\n", cp->unit);
959 vj_uncompress_err(&cp->vj_comp);
960 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
961 }
962
963 vjlen = vj_uncompress_tcp(dp, np->b_wptr - dp, len,
964 &cp->vj_comp, &iphdr, &iphlen);
965 if (vjlen < 0) {
966 if (cp->flags & DBGLOG)
967 DPRINT2("ppp%d: vj_uncomp_tcp failed, pkt len %d\n",
968 cp->unit, len);
969 ++cp->vj_last_ierrors; /* so we don't reset next time */
970 goto bad;
971 }
972
973 /* drop ppp and vj headers off */
974 if (mp != np) {
975 freeb(mp);
976 mp = np;
977 }
978 mp->b_rptr = dp + vjlen;
979
980 /* allocate a new mblk for the ppp and ip headers */
981 if ((np = allocb(iphlen + PPP_HDRLEN + 4, BPRI_MED)) == 0)
982 goto bad;
983 dp = np->b_rptr; /* prepend mblk with TCP/IP hdr */
984 dp[0] = PPP_ALLSTATIONS; /* reconstruct PPP header */
985 dp[1] = PPP_UI;
986 dp[2] = PPP_IP >> 8;
987 dp[3] = PPP_IP;
988 bcopy((caddr_t)iphdr, (caddr_t)dp + PPP_HDRLEN, iphlen);
989 np->b_wptr = dp + iphlen + PPP_HDRLEN;
990 np->b_cont = mp;
991
992 /* XXX there seems to be a bug which causes panics in strread
993 if we make an mbuf with only the IP header in it :-( */
994 if (mp->b_wptr - mp->b_rptr > 4) {
995 bcopy((caddr_t)mp->b_rptr, (caddr_t)np->b_wptr, 4);
996 mp->b_rptr += 4;
997 np->b_wptr += 4;
998 } else {
999 bcopy((caddr_t)mp->b_rptr, (caddr_t)np->b_wptr,
1000 mp->b_wptr - mp->b_rptr);
1001 np->b_wptr += mp->b_wptr - mp->b_rptr;
1002 np->b_cont = mp->b_cont;
1003 freeb(mp);
1004 }
1005
1006 mp = np;
1007
1008 } else {
1009 /*
1010 * "Decompress" a VJ-uncompressed packet.
1011 */
1012 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
1013 if (!vj_uncompress_uncomp(dp, hlen, &cp->vj_comp)) {
1014 if (cp->flags & DBGLOG)
1015 DPRINT2("ppp%d: vj_uncomp_uncomp failed, pkt len %d\n",
1016 cp->unit, len);
1017 ++cp->vj_last_ierrors; /* don't need to reset next time */
1018 goto bad;
1019 }
1020 mp->b_rptr[3] = PPP_IP; /* fix up the PPP protocol field */
1021 }
1022 }
1023
1024 putnext(q, mp);
1025 continue;
1026
1027 bad:
1028 if (mp != 0)
1029 freemsg(mp);
1030 cp->stats.ppp_ierrors++;
1031 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
1032 }
1033
1034 return 0;
1035}
1036
1037/*
1038 * Handle a CCP packet being sent or received.
1039 * Here all the data in the packet is in a single mbuf.
1040 */
1041static void
1042ppp_comp_ccp(q, mp, rcvd)
1043 queue_t *q;
1044 mblk_t *mp;
1045 int rcvd;
1046{
1047 int len, clen;
1048 comp_state_t *cp;
1049 unsigned char *dp;
1050
1051 len = msgdsize(mp);
1052 if (len < PPP_HDRLEN + CCP_HDRLEN)
1053 return;
1054
1055 cp = (comp_state_t *) q->q_ptr;
1056 dp = mp->b_rptr + PPP_HDRLEN;
1057 len -= PPP_HDRLEN;
1058 clen = CCP_LENGTH(dp);
1059 if (clen > len)
1060 return;
1061
1062 switch (CCP_CODE(dp)) {
1063 case CCP_CONFREQ:
1064 case CCP_TERMREQ:
1065 case CCP_TERMACK:
1066 cp->flags &= ~CCP_ISUP;
1067 break;
1068
1069 case CCP_CONFACK:
1070 if ((cp->flags & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
1071 && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
1072 && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
1073 if (!rcvd) {
1074 if (cp->xstate != NULL
1075 && (*cp->xcomp->comp_init)
1076 (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
1077 cp->unit, 0, ((cp->flags & DBGLOG) != 0)))
1078 cp->flags |= CCP_COMP_RUN;
1079 } else {
1080 if (cp->rstate != NULL
1081 && (*cp->rcomp->decomp_init)
1082 (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
1083 cp->unit, 0, cp->mru, ((cp->flags & DBGLOG) != 0)))
1084 cp->flags = (cp->flags & ~CCP_ERR) | CCP_DECOMP_RUN;
1085 }
1086 }
1087 break;
1088
1089 case CCP_RESETACK:
1090 if (cp->flags & CCP_ISUP) {
1091 if (!rcvd) {
1092 if (cp->xstate && (cp->flags & CCP_COMP_RUN))
1093 (*cp->xcomp->comp_reset)(cp->xstate);
1094 } else {
1095 if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
1096 (*cp->rcomp->decomp_reset)(cp->rstate);
1097 cp->flags &= ~CCP_ERROR;
1098 }
1099 }
1100 }
1101 break;
1102 }
1103}
1104
1105#if 0
1106dump_msg(mp)
1107 mblk_t *mp;
1108{
1109 dblk_t *db;
1110
1111 while (mp != 0) {
1112 db = mp->b_datap;
1113 DPRINT2("mp=%x cont=%x ", mp, mp->b_cont);
1114 DPRINT3("rptr=%x wptr=%x datap=%x\n", mp->b_rptr, mp->b_wptr, db);
1115 DPRINT2(" base=%x lim=%x", db->db_base, db->db_lim);
1116 DPRINT2(" ref=%d type=%d\n", db->db_ref, db->db_type);
1117 mp = mp->b_cont;
1118 }
1119}
1120#endif
1121
1122static int
1123msg_byte(mp, i)
1124 mblk_t *mp;
1125 unsigned int i;
1126{
1127 while (mp != 0 && i >= mp->b_wptr - mp->b_rptr)
1128 mp = mp->b_cont;
1129 if (mp == 0)
1130 return -1;
1131 return mp->b_rptr[i];
1132}