2 * ppp_comp.c - STREAMS module for kernel-level compression and CCP support.
4 * Copyright (c) 1994 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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
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.
22 * 4. Redistributions of any form whatsoever must retain the following
24 * "This product includes software developed by Paul Mackerras
25 * <paulus@samba.org>".
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.
35 * $Id: ppp_comp.c,v 1.3 2004/01/17 05:47:55 carlsonj Exp $
39 * This file is used under SVR4, Solaris 2, SunOS 4, and Digital UNIX.
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/errno.h>
45 #include <sys/stream.h>
49 #include <sys/cmn_err.h>
54 #include <sys/cmn_err.h>
58 #include <net/ppp_defs.h>
59 #include <net/pppio.h>
64 #include <sys/protosw.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <net/vjcompress.h>
72 #define PACKETPTR mblk_t *
73 #include <net/ppp-comp.h>
75 MOD_OPEN_DECL(ppp_comp_open
);
76 MOD_CLOSE_DECL(ppp_comp_close
);
77 static int ppp_comp_rput
__P((queue_t
*, mblk_t
*));
78 static int ppp_comp_rsrv
__P((queue_t
*));
79 static int ppp_comp_wput
__P((queue_t
*, mblk_t
*));
80 static int ppp_comp_wsrv
__P((queue_t
*));
81 static void ppp_comp_ccp
__P((queue_t
*, mblk_t
*, int));
82 static int msg_byte
__P((mblk_t
*, unsigned int));
84 /* Extract byte i of message mp. */
85 #define MSG_BYTE(mp, i) ((i) < (mp)->b_wptr - (mp)->b_rptr? (mp)->b_rptr[i]: \
88 /* Is this LCP packet one we have to transmit using LCP defaults? */
89 #define LCP_USE_DFLT(mp) (1 <= (code = MSG_BYTE((mp), 4)) && code <= 7)
91 #define PPP_COMP_ID 0xbadf
92 static struct module_info minfo
= {
94 PPP_COMP_ID
, "ppp_comp", 0, INFPSZ
, 16512, 16384,
96 PPP_COMP_ID
, "ppp_comp", 0, INFPSZ
, 16384, 4096,
100 static struct qinit r_init
= {
101 ppp_comp_rput
, ppp_comp_rsrv
, ppp_comp_open
, ppp_comp_close
,
105 static struct qinit w_init
= {
106 ppp_comp_wput
, ppp_comp_wsrv
, NULL
, NULL
, NULL
, &minfo
, NULL
109 #if defined(SVR4) && !defined(SOL2)
111 #define ppp_compinfo pcmpinfo
113 struct streamtab ppp_compinfo
= {
114 &r_init
, &w_init
, NULL
, NULL
117 int ppp_comp_count
; /* number of module instances in use */
121 static void ppp_comp_alloc
__P((comp_state_t
*));
122 typedef struct memreq
{
123 unsigned char comp_opts
[20];
131 typedef struct comp_state
{
136 struct compressor
*xcomp
;
138 struct compressor
*rcomp
;
140 struct vjcompress vj_comp
;
142 struct pppstat stats
;
151 extern task_t first_task
;
154 /* Bits in flags are as defined in pppio.h. */
155 #define CCP_ERR (CCP_ERROR | CCP_FATALERROR)
156 #define LAST_MOD 0x1000000 /* no ppp modules below us */
157 #define DBGLOG 0x2000000 /* log debugging stuff */
159 #define MAX_IPHDR 128 /* max TCP/IP header size */
160 #define MAX_VJHDR 20 /* max VJ compressed header size (?) */
162 #undef MIN /* just in case */
163 #define MIN(a, b) ((a) < (b)? (a): (b))
166 * List of compressors we know about.
170 extern struct compressor ppp_bsd_compress
;
173 extern struct compressor ppp_deflate
, ppp_deflate_draft
;
176 struct compressor
*ppp_compressors
[] = {
188 * STREAMS module entry points.
190 MOD_OPEN(ppp_comp_open
)
197 if (q
->q_ptr
== NULL
) {
198 cp
= (comp_state_t
*) ALLOC_SLEEP(sizeof(comp_state_t
));
201 bzero((caddr_t
)cp
, sizeof(comp_state_t
));
202 WR(q
)->q_ptr
= q
->q_ptr
= (caddr_t
) cp
;
207 vj_compress_init(&cp
->vj_comp
, -1);
209 if (!(thread
= kernel_thread_w_arg(first_task
, ppp_comp_alloc
, (void *)cp
)))
219 MOD_CLOSE(ppp_comp_close
)
224 cp
= (comp_state_t
*) q
->q_ptr
;
226 if (cp
->xstate
!= NULL
)
227 (*cp
->xcomp
->comp_free
)(cp
->xstate
);
228 if (cp
->rstate
!= NULL
)
229 (*cp
->rcomp
->decomp_free
)(cp
->rstate
);
232 printf("ppp_comp_close: NULL thread!\n");
234 thread_terminate(cp
->thread
);
236 FREE(cp
, sizeof(comp_state_t
));
238 OTHERQ(q
)->q_ptr
= NULL
;
246 /* thread for calling back to a compressor's memory allocator
247 * Needed for Digital UNIX since it's VM can't handle requests
248 * for large amounts of memory without blocking. The thread
249 * provides a context in which we can call a memory allocator
253 ppp_comp_alloc(comp_state_t
*cp
)
256 unsigned char *compressor_options
;
258 void *(*comp_allocator
)();
261 #if defined(MAJOR_VERSION) && (MAJOR_VERSION <= 2)
263 /* In 2.x and earlier the argument gets passed
264 * in the thread structure itself. Yuck.
266 thread
= current_thread();
267 cp
= thread
->reply_port
;
268 thread
->reply_port
= PORT_NULL
;
273 assert_wait((vm_offset_t
)&cp
->memreq
.thread_status
, TRUE
);
276 if (thread_should_halt(current_thread()))
278 cmd
= cp
->memreq
.cmd
;
279 compressor_options
= &cp
->memreq
.comp_opts
[0];
280 len
= compressor_options
[1];
281 if (cmd
== PPPIO_XCOMP
) {
282 cp
->memreq
.returned_mem
= cp
->xcomp
->comp_alloc(compressor_options
, len
);
283 if (!cp
->memreq
.returned_mem
) {
284 cp
->memreq
.thread_status
= ENOSR
;
286 cp
->memreq
.thread_status
= 0;
289 cp
->memreq
.returned_mem
= cp
->rcomp
->decomp_alloc(compressor_options
, len
);
290 if (!cp
->memreq
.returned_mem
) {
291 cp
->memreq
.thread_status
= ENOSR
;
293 cp
->memreq
.thread_status
= 0;
301 /* here's the deal with memory allocation under Digital UNIX.
302 * Some other may also benefit from this...
303 * We can't ask for huge chunks of memory in a context where
304 * the caller can't be put to sleep (like, here.) The alloc
305 * is likely to fail. Instead we do this: the first time we
306 * get called, kick off a thread to do the allocation. Return
307 * immediately to the caller with EAGAIN, as an indication that
308 * they should send down the ioctl again. By the time the
309 * second call comes in it's likely that the memory allocation
310 * thread will have returned with the requested memory. We will
311 * continue to return EAGAIN however until the thread has completed.
312 * When it has, we return zero (and the memory) if the allocator
313 * was successful and ENOSR otherwise.
315 * Callers of the RCOMP and XCOMP ioctls are encouraged (but not
316 * required) to loop for some number of iterations with a small
317 * delay in the loop body (for instance a 1/10-th second "sleep"
330 struct compressor
**comp
;
331 struct ppp_stats
*psp
;
332 struct ppp_comp_stats
*csp
;
333 unsigned char *opt_data
;
334 int nxslots
, nrslots
;
336 cp
= (comp_state_t
*) q
->q_ptr
;
338 DPRINT("cp == 0 in ppp_comp_wput\n");
343 switch (mp
->b_datap
->db_type
) {
350 iop
= (struct iocblk
*) mp
->b_rptr
;
352 switch (iop
->ioc_cmd
) {
355 /* set/get CCP state */
356 if (iop
->ioc_count
!= 2 * sizeof(int))
358 if (mp
->b_cont
== 0) {
359 DPRINT1("ppp_comp_wput/%d: PPPIO_CFLAGS b_cont = 0!\n", cp
->unit
);
362 flags
= ((int *) mp
->b_cont
->b_rptr
)[0];
363 mask
= ((int *) mp
->b_cont
->b_rptr
)[1];
364 cp
->flags
= (cp
->flags
& ~mask
) | (flags
& mask
);
365 if ((mask
& CCP_ISOPEN
) && (flags
& CCP_ISOPEN
) == 0) {
366 if (cp
->xstate
!= NULL
) {
367 (*cp
->xcomp
->comp_free
)(cp
->xstate
);
370 if (cp
->rstate
!= NULL
) {
371 (*cp
->rcomp
->decomp_free
)(cp
->rstate
);
374 cp
->flags
&= ~CCP_ISUP
;
377 iop
->ioc_count
= sizeof(int);
378 ((int *) mp
->b_cont
->b_rptr
)[0] = cp
->flags
;
379 mp
->b_cont
->b_wptr
= mp
->b_cont
->b_rptr
+ sizeof(int);
384 * Initialize VJ compressor/decompressor
386 if (iop
->ioc_count
!= 2)
388 if (mp
->b_cont
== 0) {
389 DPRINT1("ppp_comp_wput/%d: PPPIO_VJINIT b_cont = 0!\n", cp
->unit
);
392 nxslots
= mp
->b_cont
->b_rptr
[0] + 1;
393 nrslots
= mp
->b_cont
->b_rptr
[1] + 1;
394 if (nxslots
> MAX_STATES
|| nrslots
> MAX_STATES
)
396 vj_compress_init(&cp
->vj_comp
, nxslots
);
397 cp
->vj_last_ierrors
= cp
->stats
.ppp_ierrors
;
404 if (iop
->ioc_count
<= 0)
406 if (mp
->b_cont
== 0) {
407 DPRINT1("ppp_comp_wput/%d: PPPIO_[XR]COMP b_cont = 0!\n", cp
->unit
);
410 opt_data
= mp
->b_cont
->b_rptr
;
411 len
= mp
->b_cont
->b_wptr
- opt_data
;
412 if (len
> iop
->ioc_count
)
413 len
= iop
->ioc_count
;
414 if (opt_data
[1] < 2 || opt_data
[1] > len
)
416 for (comp
= ppp_compressors
; *comp
!= NULL
; ++comp
)
417 if ((*comp
)->compress_proto
== opt_data
[0]) {
418 /* here's the handler! */
421 if (iop
->ioc_cmd
== PPPIO_XCOMP
) {
422 /* A previous call may have fetched memory for a compressor
423 * that's now being retired or reset. Free it using it's
424 * mechanism for freeing stuff.
426 if (cp
->xstate
!= NULL
) {
427 (*cp
->xcomp
->comp_free
)(cp
->xstate
);
431 cp
->xstate
= (*comp
)->comp_alloc(opt_data
, len
);
432 if (cp
->xstate
== NULL
)
435 if (cp
->rstate
!= NULL
) {
436 (*cp
->rcomp
->decomp_free
)(cp
->rstate
);
440 cp
->rstate
= (*comp
)->decomp_alloc(opt_data
, len
);
441 if (cp
->rstate
== NULL
)
445 if ((error
= cp
->memreq
.thread_status
) != EAGAIN
)
446 if (iop
->ioc_cmd
== PPPIO_XCOMP
) {
448 (*cp
->xcomp
->comp_free
)(cp
->xstate
);
451 /* sanity check for compressor options
453 if (sizeof (cp
->memreq
.comp_opts
) < len
) {
454 printf("can't handle options for compressor %d (%d)\n", opt_data
[0],
456 cp
->memreq
.thread_status
= ENOSR
;
457 cp
->memreq
.returned_mem
= 0;
459 /* fill in request for the thread and kick it off
461 if (cp
->memreq
.thread_status
== 0 && !cp
->memreq
.returned_mem
) {
462 bcopy(opt_data
, cp
->memreq
.comp_opts
, len
);
463 cp
->memreq
.cmd
= PPPIO_XCOMP
;
465 error
= cp
->memreq
.thread_status
= EAGAIN
;
466 thread_wakeup((vm_offset_t
)&cp
->memreq
.thread_status
);
468 cp
->xstate
= cp
->memreq
.returned_mem
;
469 cp
->memreq
.returned_mem
= 0;
470 cp
->memreq
.thread_status
= 0;
474 (*cp
->rcomp
->decomp_free
)(cp
->rstate
);
477 if (sizeof (cp
->memreq
.comp_opts
) < len
) {
478 printf("can't handle options for compressor %d (%d)\n", opt_data
[0],
480 cp
->memreq
.thread_status
= ENOSR
;
481 cp
->memreq
.returned_mem
= 0;
483 if (cp
->memreq
.thread_status
== 0 && !cp
->memreq
.returned_mem
) {
484 bcopy(opt_data
, cp
->memreq
.comp_opts
, len
);
485 cp
->memreq
.cmd
= PPPIO_RCOMP
;
487 error
= cp
->memreq
.thread_status
= EAGAIN
;
488 thread_wakeup((vm_offset_t
)&cp
->memreq
.thread_status
);
490 cp
->rstate
= cp
->memreq
.returned_mem
;
491 cp
->memreq
.returned_mem
= 0;
492 cp
->memreq
.thread_status
= 0;
502 if ((cp
->flags
& LAST_MOD
) == 0) {
503 error
= -1; /* let the ppp_ahdl module handle it */
506 np
= allocb(sizeof(struct ppp_stats
), BPRI_HI
);
514 psp
= (struct ppp_stats
*) np
->b_wptr
;
515 np
->b_wptr
+= sizeof(struct ppp_stats
);
516 iop
->ioc_count
= sizeof(struct ppp_stats
);
518 psp
->vj
= cp
->vj_comp
.stats
;
523 np
= allocb(sizeof(struct ppp_comp_stats
), BPRI_HI
);
531 csp
= (struct ppp_comp_stats
*) np
->b_wptr
;
532 np
->b_wptr
+= sizeof(struct ppp_comp_stats
);
533 iop
->ioc_count
= sizeof(struct ppp_comp_stats
);
534 bzero((caddr_t
)csp
, sizeof(struct ppp_comp_stats
));
536 (*cp
->xcomp
->comp_stat
)(cp
->xstate
, &csp
->c
);
538 (*cp
->rcomp
->decomp_stat
)(cp
->rstate
, &csp
->d
);
543 if (iop
->ioc_count
!= sizeof(int))
545 if (mp
->b_cont
== 0) {
546 DPRINT1("ppp_comp_wput/%d: PPPIO_DEBUG b_cont = 0!\n", cp
->unit
);
549 n
= *(int *)mp
->b_cont
->b_rptr
;
550 if (n
== PPPDBG_LOG
+ PPPDBG_COMP
) {
551 DPRINT1("ppp_comp%d: debug log enabled\n", cp
->unit
);
561 cp
->flags
|= LAST_MOD
;
572 else if (error
== 0) {
573 mp
->b_datap
->db_type
= M_IOCACK
;
576 mp
->b_datap
->db_type
= M_IOCNAK
;
577 iop
->ioc_error
= error
;
584 switch (*mp
->b_rptr
) {
586 cp
->mtu
= ((unsigned short *)mp
->b_rptr
)[1];
589 cp
->mru
= ((unsigned short *)mp
->b_rptr
)[1];
592 cp
->unit
= mp
->b_rptr
[1];
609 mblk_t
*mp
, *cmp
= NULL
;
611 int len
, proto
, type
, hlen
, code
;
613 unsigned char *vjhdr
, *dp
;
615 cp
= (comp_state_t
*) q
->q_ptr
;
617 DPRINT("cp == 0 in ppp_comp_wsrv\n");
621 while ((mp
= getq(q
)) != 0) {
622 /* assert(mp->b_datap->db_type == M_DATA) */
624 if (!bcanputnext(q
,mp
->b_band
))
634 * First check the packet length and work out what the protocol is.
637 if (len
< PPP_HDRLEN
) {
638 DPRINT1("ppp_comp_wsrv: bogus short packet (%d)\n", len
);
640 cp
->stats
.ppp_oerrors
++;
641 putctl1(RD(q
)->q_next
, M_CTL
, PPPCTL_OERROR
);
644 proto
= (MSG_BYTE(mp
, 2) << 8) + MSG_BYTE(mp
, 3);
647 * Make sure we've got enough data in the first mblk
648 * and that we are its only user.
650 if (proto
== PPP_CCP
)
652 else if (proto
== PPP_IP
)
653 hlen
= PPP_HDRLEN
+ MAX_IPHDR
;
658 if (mp
->b_wptr
< mp
->b_rptr
+ hlen
|| mp
->b_datap
->db_ref
> 1) {
661 DPRINT1("ppp_comp_wsrv: pullup failed (%d)\n", hlen
);
662 cp
->stats
.ppp_oerrors
++;
663 putctl1(RD(q
)->q_next
, M_CTL
, PPPCTL_OERROR
);
669 * Do VJ compression if requested.
671 if (proto
== PPP_IP
&& (cp
->flags
& COMP_VJC
)) {
672 ip
= (struct ip
*) (mp
->b_rptr
+ PPP_HDRLEN
);
673 if (ip
->ip_p
== IPPROTO_TCP
) {
674 type
= vj_compress_tcp(ip
, len
- PPP_HDRLEN
, &cp
->vj_comp
,
675 (cp
->flags
& COMP_VJCCID
), &vjhdr
);
677 case TYPE_UNCOMPRESSED_TCP
:
678 mp
->b_rptr
[3] = proto
= PPP_VJC_UNCOMP
;
680 case TYPE_COMPRESSED_TCP
:
681 dp
= vjhdr
- PPP_HDRLEN
;
682 dp
[1] = mp
->b_rptr
[1]; /* copy control field */
683 dp
[0] = mp
->b_rptr
[0]; /* copy address field */
684 dp
[2] = 0; /* set protocol field */
685 dp
[3] = proto
= PPP_VJC_COMP
;
693 * Do packet compression if enabled.
695 if (proto
== PPP_CCP
)
696 ppp_comp_ccp(q
, mp
, 0);
697 else if (proto
!= PPP_LCP
&& (cp
->flags
& CCP_COMP_RUN
)
698 && cp
->xstate
!= NULL
) {
700 (*cp
->xcomp
->compress
)(cp
->xstate
, &cmp
, mp
, len
,
701 (cp
->flags
& CCP_ISUP
? cp
->mtu
+ PPP_HDRLEN
: 0));
704 cmp
->b_band
=mp
->b_band
;
712 * Do address/control and protocol compression if enabled.
714 if ((cp
->flags
& COMP_AC
)
715 && !(proto
== PPP_LCP
&& LCP_USE_DFLT(mp
))) {
716 mp
->b_rptr
+= 2; /* drop the address & ctrl fields */
717 if (proto
< 0x100 && (cp
->flags
& COMP_PROT
))
718 ++mp
->b_rptr
; /* drop the high protocol byte */
719 } else if (proto
< 0x100 && (cp
->flags
& COMP_PROT
)) {
720 /* shuffle up the address & ctrl fields */
721 mp
->b_rptr
[2] = mp
->b_rptr
[1];
722 mp
->b_rptr
[1] = mp
->b_rptr
[0];
726 cp
->stats
.ppp_opackets
++;
727 cp
->stats
.ppp_obytes
+= msgdsize(mp
);
741 struct ppp_stats
*psp
;
743 cp
= (comp_state_t
*) q
->q_ptr
;
745 DPRINT("cp == 0 in ppp_comp_rput\n");
750 switch (mp
->b_datap
->db_type
) {
757 iop
= (struct iocblk
*) mp
->b_rptr
;
758 switch (iop
->ioc_cmd
) {
761 * Catch this on the way back from the ppp_ahdl module
762 * so we can fill in the VJ stats.
764 if (mp
->b_cont
== 0 || iop
->ioc_count
!= sizeof(struct ppp_stats
))
766 psp
= (struct ppp_stats
*) mp
->b_cont
->b_rptr
;
767 psp
->vj
= cp
->vj_comp
.stats
;
774 switch (mp
->b_rptr
[0]) {
776 ++cp
->stats
.ppp_ierrors
;
779 ++cp
->stats
.ppp_oerrors
;
797 mblk_t
*mp
, *dmp
= NULL
, *np
;
800 int len
, hlen
, vjlen
;
803 cp
= (comp_state_t
*) q
->q_ptr
;
805 DPRINT("cp == 0 in ppp_comp_rsrv\n");
809 while ((mp
= getq(q
)) != 0) {
810 /* assert(mp->b_datap->db_type == M_DATA) */
811 if (!canputnext(q
)) {
817 cp
->stats
.ppp_ibytes
+= len
;
818 cp
->stats
.ppp_ipackets
++;
821 * First work out the protocol and where the PPP header ends.
824 proto
= MSG_BYTE(mp
, 0);
825 if (proto
== PPP_ALLSTATIONS
) {
827 proto
= MSG_BYTE(mp
, 2);
829 if ((proto
& 1) == 0) {
831 proto
= (proto
<< 8) + MSG_BYTE(mp
, i
);
836 * Now reconstruct a complete, contiguous PPP header at the
837 * start of the packet.
839 if (hlen
< ((cp
->flags
& DECOMP_AC
)? 0: 2)
840 + ((cp
->flags
& DECOMP_PROT
)? 1: 2)) {
844 if (mp
->b_rptr
+ hlen
> mp
->b_wptr
) {
845 adjmsg(mp
, hlen
); /* XXX check this call */
848 if (hlen
!= PPP_HDRLEN
) {
850 * We need to put some bytes on the front of the packet
851 * to make a full-length PPP header.
852 * If we can put them in *mp, we do, otherwise we
853 * tack another mblk on the front.
854 * XXX we really shouldn't need to carry around
855 * the address and control at this stage.
857 dp
= mp
->b_rptr
+ hlen
- PPP_HDRLEN
;
858 if (dp
< mp
->b_datap
->db_base
|| mp
->b_datap
->db_ref
> 1) {
859 np
= allocb(PPP_HDRLEN
, BPRI_MED
);
866 mp
->b_wptr
+= PPP_HDRLEN
;
870 dp
[0] = PPP_ALLSTATIONS
;
877 * Now see if we have a compressed packet to decompress,
878 * or a CCP packet to take notice of.
880 proto
= PPP_PROTOCOL(mp
->b_rptr
);
881 if (proto
== PPP_CCP
) {
883 if (mp
->b_wptr
< mp
->b_rptr
+ len
) {
888 ppp_comp_ccp(q
, mp
, 1);
889 } else if (proto
== PPP_COMP
) {
890 if ((cp
->flags
& CCP_ISUP
)
891 && (cp
->flags
& CCP_DECOMP_RUN
) && cp
->rstate
892 && (cp
->flags
& CCP_ERR
) == 0) {
893 rv
= (*cp
->rcomp
->decompress
)(cp
->rstate
, mp
, &dmp
);
899 /* no error, but no packet returned either. */
904 cp
->flags
|= CCP_ERROR
;
905 ++cp
->stats
.ppp_ierrors
;
906 putctl1(q
->q_next
, M_CTL
, PPPCTL_IERROR
);
908 case DECOMP_FATALERROR
:
909 cp
->flags
|= CCP_FATALERROR
;
910 ++cp
->stats
.ppp_ierrors
;
911 putctl1(q
->q_next
, M_CTL
, PPPCTL_IERROR
);
915 } else if (cp
->rstate
&& (cp
->flags
& CCP_DECOMP_RUN
)) {
916 (*cp
->rcomp
->incomp
)(cp
->rstate
, mp
);
920 * Now do VJ decompression.
922 proto
= PPP_PROTOCOL(mp
->b_rptr
);
923 if (proto
== PPP_VJC_COMP
|| proto
== PPP_VJC_UNCOMP
) {
924 len
= msgdsize(mp
) - PPP_HDRLEN
;
925 if ((cp
->flags
& DECOMP_VJC
) == 0 || len
<= 0)
929 * Advance past the ppp header.
930 * Here we assume that the whole PPP header is in the first mblk.
933 dp
= np
->b_rptr
+ PPP_HDRLEN
;
934 if (dp
>= mp
->b_wptr
) {
940 * Make sure we have sufficient contiguous data at this point.
942 hlen
= (proto
== PPP_VJC_COMP
)? MAX_VJHDR
: MAX_IPHDR
;
945 if (np
->b_wptr
< dp
+ hlen
|| np
->b_datap
->db_ref
> 1) {
946 PULLUP(mp
, hlen
+ PPP_HDRLEN
);
950 dp
= np
->b_rptr
+ PPP_HDRLEN
;
953 if (proto
== PPP_VJC_COMP
) {
955 * Decompress VJ-compressed packet.
956 * First reset compressor if an input error has occurred.
958 if (cp
->stats
.ppp_ierrors
!= cp
->vj_last_ierrors
) {
959 if (cp
->flags
& DBGLOG
)
960 DPRINT1("ppp%d: resetting VJ\n", cp
->unit
);
961 vj_uncompress_err(&cp
->vj_comp
);
962 cp
->vj_last_ierrors
= cp
->stats
.ppp_ierrors
;
965 vjlen
= vj_uncompress_tcp(dp
, np
->b_wptr
- dp
, len
,
966 &cp
->vj_comp
, &iphdr
, &iphlen
);
968 if (cp
->flags
& DBGLOG
)
969 DPRINT2("ppp%d: vj_uncomp_tcp failed, pkt len %d\n",
971 ++cp
->vj_last_ierrors
; /* so we don't reset next time */
975 /* drop ppp and vj headers off */
980 mp
->b_rptr
= dp
+ vjlen
;
982 /* allocate a new mblk for the ppp and ip headers */
983 if ((np
= allocb(iphlen
+ PPP_HDRLEN
+ 4, BPRI_MED
)) == 0)
985 dp
= np
->b_rptr
; /* prepend mblk with TCP/IP hdr */
986 dp
[0] = PPP_ALLSTATIONS
; /* reconstruct PPP header */
990 bcopy((caddr_t
)iphdr
, (caddr_t
)dp
+ PPP_HDRLEN
, iphlen
);
991 np
->b_wptr
= dp
+ iphlen
+ PPP_HDRLEN
;
994 /* XXX there seems to be a bug which causes panics in strread
995 if we make an mbuf with only the IP header in it :-( */
996 if (mp
->b_wptr
- mp
->b_rptr
> 4) {
997 bcopy((caddr_t
)mp
->b_rptr
, (caddr_t
)np
->b_wptr
, 4);
1001 bcopy((caddr_t
)mp
->b_rptr
, (caddr_t
)np
->b_wptr
,
1002 mp
->b_wptr
- mp
->b_rptr
);
1003 np
->b_wptr
+= mp
->b_wptr
- mp
->b_rptr
;
1004 np
->b_cont
= mp
->b_cont
;
1012 * "Decompress" a VJ-uncompressed packet.
1014 cp
->vj_last_ierrors
= cp
->stats
.ppp_ierrors
;
1015 if (!vj_uncompress_uncomp(dp
, hlen
, &cp
->vj_comp
)) {
1016 if (cp
->flags
& DBGLOG
)
1017 DPRINT2("ppp%d: vj_uncomp_uncomp failed, pkt len %d\n",
1019 ++cp
->vj_last_ierrors
; /* don't need to reset next time */
1022 mp
->b_rptr
[3] = PPP_IP
; /* fix up the PPP protocol field */
1032 cp
->stats
.ppp_ierrors
++;
1033 putctl1(q
->q_next
, M_CTL
, PPPCTL_IERROR
);
1040 * Handle a CCP packet being sent or received.
1041 * Here all the data in the packet is in a single mbuf.
1044 ppp_comp_ccp(q
, mp
, rcvd
)
1054 if (len
< PPP_HDRLEN
+ CCP_HDRLEN
)
1057 cp
= (comp_state_t
*) q
->q_ptr
;
1058 dp
= mp
->b_rptr
+ PPP_HDRLEN
;
1060 clen
= CCP_LENGTH(dp
);
1064 switch (CCP_CODE(dp
)) {
1068 cp
->flags
&= ~CCP_ISUP
;
1072 if ((cp
->flags
& (CCP_ISOPEN
| CCP_ISUP
)) == CCP_ISOPEN
1073 && clen
>= CCP_HDRLEN
+ CCP_OPT_MINLEN
1074 && clen
>= CCP_HDRLEN
+ CCP_OPT_LENGTH(dp
+ CCP_HDRLEN
)) {
1076 if (cp
->xstate
!= NULL
1077 && (*cp
->xcomp
->comp_init
)
1078 (cp
->xstate
, dp
+ CCP_HDRLEN
, clen
- CCP_HDRLEN
,
1079 cp
->unit
, 0, ((cp
->flags
& DBGLOG
) != 0)))
1080 cp
->flags
|= CCP_COMP_RUN
;
1082 if (cp
->rstate
!= NULL
1083 && (*cp
->rcomp
->decomp_init
)
1084 (cp
->rstate
, dp
+ CCP_HDRLEN
, clen
- CCP_HDRLEN
,
1085 cp
->unit
, 0, cp
->mru
, ((cp
->flags
& DBGLOG
) != 0)))
1086 cp
->flags
= (cp
->flags
& ~CCP_ERR
) | CCP_DECOMP_RUN
;
1092 if (cp
->flags
& CCP_ISUP
) {
1094 if (cp
->xstate
&& (cp
->flags
& CCP_COMP_RUN
))
1095 (*cp
->xcomp
->comp_reset
)(cp
->xstate
);
1097 if (cp
->rstate
&& (cp
->flags
& CCP_DECOMP_RUN
)) {
1098 (*cp
->rcomp
->decomp_reset
)(cp
->rstate
);
1099 cp
->flags
&= ~CCP_ERROR
;
1115 DPRINT2("mp=%x cont=%x ", mp
, mp
->b_cont
);
1116 DPRINT3("rptr=%x wptr=%x datap=%x\n", mp
->b_rptr
, mp
->b_wptr
, db
);
1117 DPRINT2(" base=%x lim=%x", db
->db_base
, db
->db_lim
);
1118 DPRINT2(" ref=%d type=%d\n", db
->db_ref
, db
->db_type
);
1129 while (mp
!= 0 && i
>= mp
->b_wptr
- mp
->b_rptr
)
1133 return mp
->b_rptr
[i
];