2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Carnegie Mellon University
22 * Pittsburgh, PA 15213
27 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
29 * Copyright (c) 1987 Regents of the University of California.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms are permitted
33 * provided that the above copyright notice and this paragraph are
34 * duplicated in all such forms and that any documentation,
35 * advertising materials, and other materials related to such
36 * distribution and use acknowledge that the software was developed
37 * by the University of California, Berkeley. The name of the
38 * University may not be used to endorse or promote products derived
39 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
44 * Serial Line interface
47 * Center for Seismic Studies
48 * 1300 N 17th Street, Suite 1450
49 * Arlington, Virginia 22209
54 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
55 * Converted to 4.3BSD Beta by Chris Torek.
56 * Other changes made at Berkeley, based in part on code by Kirk Smith.
58 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59 * Added VJ tcp header compression; more unified ioctls
61 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
62 * Cleaned up a lot of the mbuf-related code to fix bugs that
63 * caused system crashes and packet corruption. Changed pppstart
64 * so that it doesn't just give up with a collision if the whole
65 * packet doesn't fit in the output ring buffer.
67 * Added priority queueing for interactive IP packets, following
68 * the model of if_sl.c, plus hooks for bpf.
69 * Paul Mackerras (paulus@cs.anu.edu.au).
72 /* $Id: if_ppp.c,v 1.10 1996/10/08 04:38:03 paulus Exp $ */
73 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
74 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
82 #include <sys/param.h>
83 #include <sys/systm.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/kernel.h>
90 #include <sys/malloc.h>
93 #include <net/if_types.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/in_var.h>
101 #include <netinet/ip.h>
104 #include "bpfilter.h"
110 #include <net/pppcompress.h>
113 #include <net/ppp_defs.h>
114 #include <net/if_ppp.h>
115 #include <net/if_pppvar.h>
116 #include <machine/cpu.h>
118 #define splsoftnet splnet
121 /* This definition should be moved to net/netisr.h */
122 #define NETISR_PPP 26 /* PPP software interrupt */
126 #define PACKETPTR struct mbuf *
127 #include <net/ppp-comp.h>
130 static void ppp_requeue __P((struct ppp_softc *));
131 static void ppp_outpkt __P((struct ppp_softc *));
132 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
133 static void ppp_ccp_closed __P((struct ppp_softc *));
134 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
135 static void pppdumpm __P((struct mbuf *m0));
138 * Some useful mbuf macros not in mbuf.h.
140 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
142 #define M_DATASTART(m) \
143 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
144 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
146 #define M_DATASIZE(m) \
147 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
148 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
151 * We steal two bits in the mbuf m_flags, to mark high-priority packets
152 * for output, and received packets following lost/corrupted packets.
154 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
155 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
160 * List of compressors we know about.
161 * We leave some space so maybe we can modload compressors.
164 extern struct compressor ppp_bsd_compress;
165 extern struct compressor ppp_deflate;
167 struct compressor *ppp_compressors[8] = {
176 #endif /* PPP_COMPRESS */
178 TEXT_SET(pseudo_set, pppattach);
181 * Called from boot code to establish ppp interfaces.
186 register struct ppp_softc *sc;
188 extern void (*netisrs[])__P((void));
190 for (sc = ppp_softc; i < NPPP; sc++) {
191 sc->sc_if.if_name = "ppp";
192 sc->sc_if.if_unit = i++;
193 sc->sc_if.if_mtu = PPP_MTU;
194 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
195 sc->sc_if.if_type = IFT_PPP;
196 sc->sc_if.if_hdrlen = PPP_HDRLEN;
197 sc->sc_if.if_ioctl = pppsioctl;
198 sc->sc_if.if_output = pppoutput;
199 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
200 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
201 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
202 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
203 if_attach(&sc->sc_if);
205 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
208 netisrs[NETISR_PPP] = pppintr;
212 * Allocate a ppp interface unit and initialize it.
219 struct ppp_softc *sc;
221 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
222 if (sc->sc_xfer == pid) {
226 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
227 if (sc->sc_devp == NULL)
233 sc->sc_mru = PPP_MRU;
234 sc->sc_relinq = NULL;
235 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
237 MALLOC(sc->sc_comp, struct vjcompress *, sizeof(struct vjcompress),
240 vj_compress_init(sc->sc_comp, -1);
243 sc->sc_xc_state = NULL;
244 sc->sc_rc_state = NULL;
245 #endif /* PPP_COMPRESS */
246 for (i = 0; i < NUM_NP; ++i)
247 sc->sc_npmode[i] = NPMODE_ERROR;
248 sc->sc_npqueue = NULL;
249 sc->sc_npqtail = &sc->sc_npqueue;
250 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
256 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
260 struct ppp_softc *sc;
265 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
269 IF_DEQUEUE(&sc->sc_rawq, m);
275 IF_DEQUEUE(&sc->sc_inq, m);
281 IF_DEQUEUE(&sc->sc_fastq, m);
286 while ((m = sc->sc_npqueue) != NULL) {
287 sc->sc_npqueue = m->m_nextpkt;
290 if (sc->sc_togo != NULL) {
291 m_freem(sc->sc_togo);
296 sc->sc_xc_state = NULL;
297 sc->sc_rc_state = NULL;
298 #endif /* PPP_COMPRESS */
300 if (sc->sc_comp != 0) {
301 FREE(sc->sc_comp, M_DEVBUF);
308 * Ioctl routine for generic ppp devices.
311 pppioctl(sc, cmd, data, flag, p)
312 struct ppp_softc *sc;
318 int s, error, flags, mru, nb, npx;
319 struct ppp_option_data *odp;
320 struct compressor **cp;
324 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
329 *(int *)data = sc->sc_inq.ifq_len;
333 *(int *)data = sc->sc_if.if_unit;
337 *(u_int *)data = sc->sc_flags;
341 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
343 flags = *(int *)data & SC_MASK;
346 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
350 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
355 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
358 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
363 *(int *)data = sc->sc_mru;
368 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
372 vj_compress_init(sc->sc_comp, *(int *)data);
379 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
381 sc->sc_xfer = p->p_pid;
385 case PPPIOCSCOMPRESS:
386 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
388 odp = (struct ppp_option_data *) data;
390 if (nb > sizeof(ccp_option))
391 nb = sizeof(ccp_option);
392 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
394 if (ccp_option[1] < 2) /* preliminary check on the length byte */
396 for (cp = ppp_compressors; *cp != NULL; ++cp)
397 if ((*cp)->compress_proto == ccp_option[0]) {
399 * Found a handler for the protocol - try to allocate
400 * a compressor or decompressor.
405 if (sc->sc_xc_state != NULL)
406 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
408 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
409 if (sc->sc_xc_state == NULL) {
410 if (sc->sc_flags & SC_DEBUG)
411 printf("ppp%d: comp_alloc failed\n",
416 sc->sc_flags &= ~SC_COMP_RUN;
420 if (sc->sc_rc_state != NULL)
421 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
423 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
424 if (sc->sc_rc_state == NULL) {
425 if (sc->sc_flags & SC_DEBUG)
426 printf("ppp%d: decomp_alloc failed\n",
431 sc->sc_flags &= ~SC_DECOMP_RUN;
436 if (sc->sc_flags & SC_DEBUG)
437 printf("ppp%d: no compressor for [%x %x %x], %x\n",
438 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
440 return (EINVAL); /* no handler found */
441 #endif /* PPP_COMPRESS */
445 npi = (struct npioctl *) data;
446 switch (npi->protocol) {
453 if (cmd == PPPIOCGNPMODE) {
454 npi->mode = sc->sc_npmode[npx];
456 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
458 if (npi->mode != sc->sc_npmode[npx]) {
460 sc->sc_npmode[npx] = npi->mode;
461 if (npi->mode != NPMODE_QUEUE) {
473 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
474 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
485 * Process an ioctl request to the ppp network interface.
488 pppsioctl(ifp, cmd, data)
489 register struct ifnet *ifp;
493 struct proc *p = curproc; /* XXX */
494 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
495 register struct ifaddr *ifa = (struct ifaddr *)data;
496 register struct ifreq *ifr = (struct ifreq *)data;
497 struct ppp_stats *psp;
499 struct ppp_comp_stats *pcp;
501 int s = splimp(), error = 0;
505 if ((ifp->if_flags & IFF_RUNNING) == 0)
506 ifp->if_flags &= ~IFF_UP;
510 if (ifa->ifa_addr->sa_family != AF_INET)
511 error = EAFNOSUPPORT;
515 if (ifa->ifa_addr->sa_family != AF_INET)
516 error = EAFNOSUPPORT;
520 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
522 sc->sc_if.if_mtu = ifr->ifr_mtu;
526 ifr->ifr_mtu = sc->sc_if.if_mtu;
532 error = EAFNOSUPPORT;
535 switch(ifr->ifr_addr.sa_family) {
541 error = EAFNOSUPPORT;
547 psp = &((struct ifpppstatsreq *) data)->stats;
548 bzero(psp, sizeof(*psp));
549 psp->p = sc->sc_stats;
550 #if defined(VJC) && !defined(SL_NO_STATS)
552 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
553 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
554 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
555 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
556 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
557 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
558 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
559 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
566 pcp = &((struct ifpppcstatsreq *) data)->stats;
567 bzero(pcp, sizeof(*pcp));
568 if (sc->sc_xc_state != NULL)
569 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
570 if (sc->sc_rc_state != NULL)
571 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
573 #endif /* PPP_COMPRESS */
583 * Queue a packet. Start transmission if not active.
584 * Packet is placed in Information field of PPP frame.
587 pppoutput(ifp, m0, dst, rtp)
590 struct sockaddr *dst;
593 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
594 int protocol, address, control;
603 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
604 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
605 error = ENETDOWN; /* sort of */
610 * Compute PPP header.
612 m0->m_flags &= ~M_HIGHPRI;
613 switch (dst->sa_family) {
616 address = PPP_ALLSTATIONS;
619 mode = sc->sc_npmode[NP_IP];
622 * If this packet has the "low delay" bit set in the IP header,
623 * put it on the fastq instead.
625 ip = mtod(m0, struct ip *);
626 if (ip->ip_tos & IPTOS_LOWDELAY)
627 m0->m_flags |= M_HIGHPRI;
631 address = PPP_ADDRESS(dst->sa_data);
632 control = PPP_CONTROL(dst->sa_data);
633 protocol = PPP_PROTOCOL(dst->sa_data);
637 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
638 error = EAFNOSUPPORT;
643 * Drop this packet, or return an error, if necessary.
645 if (mode == NPMODE_ERROR) {
649 if (mode == NPMODE_DROP) {
655 * Add PPP header. If no space in first mbuf, allocate another.
656 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
658 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
659 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
666 m0->m_data -= PPP_HDRLEN;
668 cp = mtod(m0, u_char *);
671 *cp++ = protocol >> 8;
672 *cp++ = protocol & 0xff;
673 m0->m_len += PPP_HDRLEN;
676 for (m = m0; m != 0; m = m->m_next)
679 if (sc->sc_flags & SC_LOG_OUTPKT) {
680 printf("ppp%d output: ", ifp->if_unit);
684 if ((protocol & 0x8000) == 0) {
686 * Update the time we sent the most recent data packet.
688 sc->sc_last_sent = time.tv_sec;
693 * See if bpf wants to look at the packet.
696 bpf_mtap(sc->sc_bpf, m0);
700 * Put the packet on the appropriate queue.
703 if (mode == NPMODE_QUEUE) {
704 /* XXX we should limit the number of packets on this queue */
705 *sc->sc_npqtail = m0;
706 m0->m_nextpkt = NULL;
707 sc->sc_npqtail = &m0->m_nextpkt;
709 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
710 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
713 sc->sc_if.if_oerrors++;
714 sc->sc_stats.ppp_oerrors++;
721 ifp->if_lastchange = time;
723 ifp->if_obytes += len;
734 * After a change in the NPmode for some NP, move packets from the
735 * npqueue to the send queue or the fast queue as appropriate.
736 * Should be called at splsoftnet.
740 struct ppp_softc *sc;
742 struct mbuf *m, **mpp;
746 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
747 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
749 mode = sc->sc_npmode[NP_IP];
758 * This packet can now go on one of the queues to be sent.
762 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
765 sc->sc_if.if_oerrors++;
766 sc->sc_stats.ppp_oerrors++;
782 sc->sc_npqtail = mpp;
786 * Transmitter has finished outputting some stuff;
787 * remember to call sc->sc_start later at splsoftnet.
791 struct ppp_softc *sc;
795 sc->sc_flags &= ~SC_TBUSY;
796 schednetisr(NETISR_PPP);
801 * Get a packet to send. This procedure is intended to be called at
802 * splsoftnet, since it may involve time-consuming operations such as
803 * applying VJ compression, packet compression, address/control and/or
804 * protocol field compression to the packet.
808 struct ppp_softc *sc;
812 int address, control, protocol;
816 * Grab a packet to send: first try the fast queue, then the
819 IF_DEQUEUE(&sc->sc_fastq, m);
821 IF_DEQUEUE(&sc->sc_if.if_snd, m);
825 ++sc->sc_stats.ppp_opackets;
828 * Extract the ppp header of the new packet.
829 * The ppp header will be in one mbuf.
831 cp = mtod(m, u_char *);
832 address = PPP_ADDRESS(cp);
833 control = PPP_CONTROL(cp);
834 protocol = PPP_PROTOCOL(cp);
840 * If the packet is a TCP/IP packet, see if we can compress it.
842 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
847 ip = (struct ip *) (cp + PPP_HDRLEN);
848 if (mp->m_len <= PPP_HDRLEN) {
852 ip = mtod(mp, struct ip *);
854 /* this code assumes the IP/TCP header is in one non-shared mbuf */
855 if (ip->ip_p == IPPROTO_TCP) {
856 type = vj_compress_tcp(mp, ip, sc->sc_comp,
857 !(sc->sc_flags & SC_NO_TCP_CCID));
859 case TYPE_UNCOMPRESSED_TCP:
860 protocol = PPP_VJC_UNCOMP;
862 case TYPE_COMPRESSED_TCP:
863 protocol = PPP_VJC_COMP;
864 cp = mtod(m, u_char *);
865 cp[0] = address; /* header has moved */
870 cp[3] = protocol; /* update protocol in PPP header */
880 #endif /* PPP_COMPRESS */
884 if (protocol != PPP_LCP && protocol != PPP_CCP
885 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
886 struct mbuf *mcomp = NULL;
890 for (mp = m; mp != NULL; mp = mp->m_next)
892 clen = (*sc->sc_xcomp->compress)
893 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
895 if (sc->sc_flags & SC_CCP_UP) {
896 /* Send the compressed packet instead of the original. */
899 cp = mtod(m, u_char *);
902 /* Can't transmit compressed packets until CCP is up. */
907 #endif /* PPP_COMPRESS */
910 * Compress the address/control and protocol, if possible.
912 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
913 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
914 protocol != PPP_LCP) {
915 /* can compress address/control */
919 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
920 /* can compress protocol */
921 if (mtod(m, u_char *) == cp) {
922 cp[2] = cp[1]; /* move address/control up */
933 * Software interrupt routine, called at splsoftnet.
938 struct ppp_softc *sc;
944 for (i = 0; i < NPPP; ++i, ++sc) {
945 if (!(sc->sc_flags & SC_TBUSY)
946 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
948 sc->sc_flags |= SC_TBUSY;
954 IF_DEQUEUE(&sc->sc_rawq, m);
966 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
967 * 0 if it is about to be transmitted.
971 struct ppp_softc *sc;
980 * Get a pointer to the data after the PPP header.
982 if (m->m_len <= PPP_HDRLEN) {
986 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
989 dp = mtod(mp, u_char *) + PPP_HDRLEN;
992 ep = mtod(mp, u_char *) + mp->m_len;
993 if (dp + CCP_HDRLEN > ep)
995 slen = CCP_LENGTH(dp);
996 if (dp + slen > ep) {
997 if (sc->sc_flags & SC_DEBUG)
998 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
999 dp, slen, mtod(mp, u_char *), mp->m_len);
1003 switch (CCP_CODE(dp)) {
1007 /* CCP must be going down - disable compression */
1008 if (sc->sc_flags & SC_CCP_UP) {
1010 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1016 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1017 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1018 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1020 /* we're agreeing to send compressed packets. */
1021 if (sc->sc_xc_state != NULL
1022 && (*sc->sc_xcomp->comp_init)
1023 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1024 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1026 sc->sc_flags |= SC_COMP_RUN;
1030 /* peer is agreeing to send compressed packets. */
1031 if (sc->sc_rc_state != NULL
1032 && (*sc->sc_rcomp->decomp_init)
1033 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1034 sc->sc_if.if_unit, 0, sc->sc_mru,
1035 sc->sc_flags & SC_DEBUG)) {
1037 sc->sc_flags |= SC_DECOMP_RUN;
1038 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1046 if (sc->sc_flags & SC_CCP_UP) {
1048 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1049 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1051 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1052 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1054 sc->sc_flags &= ~SC_DC_ERROR;
1064 * CCP is down; free (de)compressor state if necessary.
1068 struct ppp_softc *sc;
1070 if (sc->sc_xc_state) {
1071 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1072 sc->sc_xc_state = NULL;
1074 if (sc->sc_rc_state) {
1075 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1076 sc->sc_rc_state = NULL;
1079 #endif /* PPP_COMPRESS */
1082 * PPP packet input routine.
1083 * The caller has checked and removed the FCS and has inserted
1084 * the address/control bytes and the protocol high byte if they
1088 ppppktin(sc, m, lost)
1089 struct ppp_softc *sc;
1096 m->m_flags |= M_ERRMARK;
1097 IF_ENQUEUE(&sc->sc_rawq, m);
1098 schednetisr(NETISR_PPP);
1103 * Process a received PPP packet, doing decompression as necessary.
1104 * Should be called at splsoftnet.
1106 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1107 TYPE_UNCOMPRESSED_TCP)
1111 struct ppp_softc *sc;
1114 struct ifnet *ifp = &sc->sc_if;
1115 struct ifqueue *inq;
1116 int s, ilen, xlen, proto, rv;
1117 u_char *cp, adrs, ctrl;
1118 struct mbuf *mp, *dmp = NULL;
1122 sc->sc_stats.ppp_ipackets++;
1124 if (sc->sc_flags & SC_LOG_INPKT) {
1126 for (mp = m; mp != NULL; mp = mp->m_next)
1128 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1132 cp = mtod(m, u_char *);
1133 adrs = PPP_ADDRESS(cp);
1134 ctrl = PPP_CONTROL(cp);
1135 proto = PPP_PROTOCOL(cp);
1137 if (m->m_flags & M_ERRMARK) {
1138 m->m_flags &= ~M_ERRMARK;
1140 sc->sc_flags |= SC_VJ_RESET;
1146 * Decompress this packet if necessary, update the receiver's
1147 * dictionary, or take appropriate action on a CCP packet.
1149 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1150 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1151 /* decompress this packet */
1152 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1153 if (rv == DECOMP_OK) {
1156 /* no error, but no decompressed packet produced */
1160 cp = mtod(m, u_char *);
1161 proto = PPP_PROTOCOL(cp);
1165 * An error has occurred in decompression.
1166 * Pass the compressed packet up to pppd, which may take
1167 * CCP down or issue a Reset-Req.
1169 if (sc->sc_flags & SC_DEBUG)
1170 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1172 sc->sc_flags |= SC_VJ_RESET;
1173 if (rv == DECOMP_ERROR)
1174 sc->sc_flags |= SC_DC_ERROR;
1176 sc->sc_flags |= SC_DC_FERROR;
1181 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1182 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1184 if (proto == PPP_CCP) {
1191 for (mp = m; mp != NULL; mp = mp->m_next)
1195 if (sc->sc_flags & SC_VJ_RESET) {
1197 * If we've missed a packet, we must toss subsequent compressed
1198 * packets which don't have an explicit connection ID.
1201 vj_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1203 sc->sc_flags &= ~SC_VJ_RESET;
1208 * See if we have a VJ-compressed packet to uncompress.
1210 if (proto == PPP_VJC_COMP) {
1211 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1214 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1215 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1216 sc->sc_comp, &iphdr, &hlen);
1219 if (sc->sc_flags & SC_DEBUG)
1220 printf("ppp%d: VJ uncompress failed on type comp\n",
1225 /* Copy the PPP and IP headers into a new mbuf. */
1226 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1231 if (hlen + PPP_HDRLEN > MHLEN) {
1232 MCLGET(mp, M_DONTWAIT);
1233 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1235 goto bad; /* lose if big headers and no clusters */
1238 cp = mtod(mp, u_char *);
1244 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1245 mp->m_len = hlen + PPP_HDRLEN;
1248 * Trim the PPP and VJ headers off the old mbuf
1249 * and stick the new and old mbufs together.
1251 m->m_data += PPP_HDRLEN + xlen;
1252 m->m_len -= PPP_HDRLEN + xlen;
1253 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1254 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1255 mp->m_len += m->m_len;
1256 MFREE(m, mp->m_next);
1260 ilen += hlen - xlen;
1262 } else if (proto == PPP_VJC_UNCOMP) {
1263 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1266 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1267 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1268 sc->sc_comp, &iphdr, &hlen);
1271 if (sc->sc_flags & SC_DEBUG)
1272 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1283 * If the packet will fit in a header mbuf, don't waste a
1284 * whole cluster on it.
1286 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1287 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1289 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1295 m->m_pkthdr.len = ilen;
1296 m->m_pkthdr.rcvif = ifp;
1299 * Record the time that we received this packet.
1301 if ((proto & 0x8000) == 0) {
1302 sc->sc_last_recv = time.tv_sec;
1306 /* See if bpf wants to look at the packet. */
1308 bpf_mtap(sc->sc_bpf, m);
1316 * IP packet - take off the ppp header and pass it up to IP.
1318 if ((ifp->if_flags & IFF_UP) == 0
1319 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1320 /* interface is down - drop the packet. */
1324 m->m_pkthdr.len -= PPP_HDRLEN;
1325 m->m_data += PPP_HDRLEN;
1326 m->m_len -= PPP_HDRLEN;
1327 schednetisr(NETISR_IP);
1334 * Some other protocol - place on input queue for read().
1342 * Put the packet on the appropriate input queue.
1345 if (IF_QFULL(inq)) {
1348 if (sc->sc_flags & SC_DEBUG)
1349 printf("ppp%d: input queue full\n", ifp->if_unit);
1356 ifp->if_ibytes += ilen;
1357 ifp->if_lastchange = time;
1366 sc->sc_if.if_ierrors++;
1367 sc->sc_stats.ppp_ierrors++;
1370 #define MAX_DUMP_BYTES 128
1376 char buf[3*MAX_DUMP_BYTES+4];
1379 static char digits[] = "0123456789abcdef";
1381 for (m = m0; m; m = m->m_next) {
1383 u_char *rptr = (u_char *)m->m_data;
1386 if (bp > buf + sizeof(buf) - 4)
1388 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1389 *bp++ = digits[*rptr++ & 0xf];
1393 if (bp > buf + sizeof(buf) - 3)
1403 printf("%s\n", buf);
1406 #endif /* NPPP > 0 */