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.2 1995/04/28 06:19:34 paulus Exp $ */
73 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
81 #include <sys/param.h>
82 #include <sys/systm.h>
85 #include <sys/socket.h>
86 #include <sys/ioctl.h>
87 #include <sys/kernel.h>
90 #include <net/if_types.h>
91 #include <net/netisr.h>
92 #include <net/route.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
101 #include "bpfilter.h"
103 #include <sys/time.h>
108 #include <net/pppcompress.h>
111 #include <net/ppp_defs.h>
112 #include <net/if_ppp.h>
113 #include <net/if_pppvar.h>
114 #include <machine/cpu.h>
117 /* This definition should be moved to net/netisr.h */
118 #define NETISR_PPP 26 /* PPP software interrupt */
122 #define PACKETPTR struct mbuf *
123 #include <net/ppp-comp.h>
126 void pppattach __P((void));
127 int pppioctl __P((struct ppp_softc *sc, int cmd, caddr_t data, int flag,
129 int pppoutput __P((struct ifnet *ifp, struct mbuf *m0,
130 struct sockaddr *dst, struct rtentry *rtp));
131 int pppsioctl __P((struct ifnet *ifp, int cmd, caddr_t data));
132 void pppintr __P((void));
134 static void ppp_requeue __P((struct ppp_softc *));
135 static void ppp_outpkt __P((struct ppp_softc *));
136 static int ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
137 static void ppp_ccp_closed __P((struct ppp_softc *));
138 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
139 static void pppdumpm __P((struct mbuf *m0));
142 * Some useful mbuf macros not in mbuf.h.
144 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
146 #define M_DATASTART(m) \
147 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
148 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
150 #define M_DATASIZE(m) \
151 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
152 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
155 * We steal two bits in the mbuf m_flags, to mark high-priority packets
156 * for output, and received packets following lost/corrupted packets.
158 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
159 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
164 * List of compressors we know about.
165 * We leave some space so maybe we can modload compressors.
168 extern struct compressor ppp_bsd_compress;
170 struct compressor *ppp_compressors[8] = {
174 #endif /* PPP_COMPRESS */
176 TEXT_SET(pseudo_set, pppattach);
179 * Called from boot code to establish ppp interfaces.
184 register struct ppp_softc *sc;
186 extern void (*netisrs[])__P((void));
188 for (sc = ppp_softc; i < NPPP; sc++) {
189 sc->sc_if.if_name = "ppp";
190 sc->sc_if.if_unit = i++;
191 sc->sc_if.if_mtu = PPP_MTU;
192 sc->sc_if.if_flags = IFF_POINTOPOINT;
193 sc->sc_if.if_type = IFT_PPP;
194 sc->sc_if.if_hdrlen = PPP_HDRLEN;
195 sc->sc_if.if_ioctl = pppsioctl;
196 sc->sc_if.if_output = pppoutput;
197 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
198 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
199 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
200 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
201 if_attach(&sc->sc_if);
203 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
206 netisrs[NETISR_PPP] = ppp_intr;
210 * Allocate a ppp interface unit and initialize it.
217 struct ppp_softc *sc;
219 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
220 if (sc->sc_xfer == pid) {
224 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
225 if (sc->sc_devp == NULL)
231 sc->sc_mru = PPP_MRU;
232 sc->sc_relinq = NULL;
234 vj_compress_init(&sc->sc_comp, -1);
237 sc->sc_xc_state = NULL;
238 sc->sc_rc_state = NULL;
239 #endif /* PPP_COMPRESS */
240 for (i = 0; i < NUM_NP; ++i)
241 sc->sc_npmode[i] = NPMODE_ERROR;
242 sc->sc_npqueue = NULL;
243 sc->sc_npqtail = &sc->sc_npqueue;
244 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
250 * Deallocate a ppp unit. Must be called at splnet or higher.
254 struct ppp_softc *sc;
259 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
263 IF_DEQUEUE(&sc->sc_rawq, m);
269 IF_DEQUEUE(&sc->sc_inq, m);
275 IF_DEQUEUE(&sc->sc_fastq, m);
280 while ((m = sc->sc_npqueue) != NULL) {
281 sc->sc_npqueue = m->m_nextpkt;
284 if (sc->sc_togo != NULL) {
285 m_freem(sc->sc_togo);
290 sc->sc_xc_state = NULL;
291 sc->sc_rc_state = NULL;
292 #endif /* PPP_COMPRESS */
296 * Ioctl routine for generic ppp devices.
299 pppioctl(sc, cmd, data, flag, p)
300 struct ppp_softc *sc;
305 int s, error, flags, mru, nb, npx;
306 struct ppp_option_data *odp;
307 struct compressor **cp;
311 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
316 *(int *)data = sc->sc_inq.ifq_len;
320 *(int *)data = sc->sc_if.if_unit;
324 *(u_int *)data = sc->sc_flags;
328 if (error = suser(p->p_ucred, &p->p_acflag))
330 flags = *(int *)data & SC_MASK;
333 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
337 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
342 if (error = suser(p->p_ucred, &p->p_acflag))
345 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
350 *(int *)data = sc->sc_mru;
355 if (error = suser(p->p_ucred, &p->p_acflag))
358 vj_compress_init(&sc->sc_comp, *(int *)data);
364 if (error = suser(p->p_ucred, &p->p_acflag))
366 sc->sc_xfer = p->p_pid;
370 case PPPIOCSCOMPRESS:
371 if (error = suser(p->p_ucred, &p->p_acflag))
373 odp = (struct ppp_option_data *) data;
375 if (nb > sizeof(ccp_option))
376 nb = sizeof(ccp_option);
377 if (error = copyin(odp->ptr, ccp_option, nb))
379 if (ccp_option[1] < 2) /* preliminary check on the length byte */
381 for (cp = ppp_compressors; *cp != NULL; ++cp)
382 if ((*cp)->compress_proto == ccp_option[0]) {
384 * Found a handler for the protocol - try to allocate
385 * a compressor or decompressor.
390 if (sc->sc_xc_state != NULL)
391 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
393 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
394 if (sc->sc_xc_state == NULL) {
395 if (sc->sc_flags & SC_DEBUG)
396 printf("ppp%d: comp_alloc failed\n",
401 sc->sc_flags &= ~SC_COMP_RUN;
403 if (sc->sc_rc_state != NULL)
404 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
406 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
407 if (sc->sc_rc_state == NULL) {
408 if (sc->sc_flags & SC_DEBUG)
409 printf("ppp%d: decomp_alloc failed\n",
414 sc->sc_flags &= ~SC_DECOMP_RUN;
419 if (sc->sc_flags & SC_DEBUG)
420 printf("ppp%d: no compressor for [%x %x %x], %x\n",
421 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
423 return (EINVAL); /* no handler found */
424 #endif /* PPP_COMPRESS */
428 npi = (struct npioctl *) data;
429 switch (npi->protocol) {
436 if (cmd == PPPIOCGNPMODE) {
437 npi->mode = sc->sc_npmode[npx];
439 if (error = suser(p->p_ucred, &p->p_acflag))
441 if (npi->mode != sc->sc_npmode[npx]) {
443 sc->sc_npmode[npx] = npi->mode;
444 if (npi->mode != NPMODE_QUEUE) {
456 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
457 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
468 * Process an ioctl request to the ppp network interface.
471 pppsioctl(ifp, cmd, data)
472 register struct ifnet *ifp;
476 struct proc *p = curproc; /* XXX */
477 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
478 register struct ifaddr *ifa = (struct ifaddr *)data;
479 register struct ifreq *ifr = (struct ifreq *)data;
480 struct ppp_stats *psp;
482 struct ppp_comp_stats *pcp;
484 int s = splimp(), error = 0;
488 if ((ifp->if_flags & IFF_RUNNING) == 0)
489 ifp->if_flags &= ~IFF_UP;
493 if (ifa->ifa_addr->sa_family != AF_INET)
494 error = EAFNOSUPPORT;
498 if (ifa->ifa_addr->sa_family != AF_INET)
499 error = EAFNOSUPPORT;
503 if (error = suser(p->p_ucred, &p->p_acflag))
505 sc->sc_if.if_mtu = ifr->ifr_mtu;
509 ifr->ifr_mtu = sc->sc_if.if_mtu;
513 psp = &((struct ifpppstatsreq *) data)->stats;
514 bzero(psp, sizeof(*psp));
515 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
516 psp->p.ppp_ipackets = ifp->if_ipackets;
517 psp->p.ppp_ierrors = ifp->if_ierrors;
518 psp->p.ppp_obytes = sc->sc_bytessent;
519 psp->p.ppp_opackets = ifp->if_opackets;
520 psp->p.ppp_oerrors = ifp->if_oerrors;
522 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
523 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
524 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
525 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
526 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
527 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
528 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
529 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
535 pcp = &((struct ifpppcstatsreq *) data)->stats;
536 bzero(pcp, sizeof(*pcp));
537 if (sc->sc_xc_state != NULL)
538 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
539 if (sc->sc_rc_state != NULL)
540 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
542 #endif /* PPP_COMPRESS */
552 * Queue a packet. Start transmission if not active.
553 * Packet is placed in Information field of PPP frame.
556 pppoutput(ifp, m0, dst, rtp)
559 struct sockaddr *dst;
562 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
563 struct ppp_header *ph;
564 int protocol, address, control;
571 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
572 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
573 error = ENETDOWN; /* sort of */
578 * Compute PPP header.
580 m0->m_flags &= ~M_HIGHPRI;
581 switch (dst->sa_family) {
584 address = PPP_ALLSTATIONS;
587 mode = sc->sc_npmode[NP_IP];
590 * If this packet has the "low delay" bit set in the IP header,
591 * put it on the fastq instead.
593 ip = mtod(m0, struct ip *);
594 if (ip->ip_tos & IPTOS_LOWDELAY)
595 m0->m_flags |= M_HIGHPRI;
599 address = PPP_ADDRESS(dst->sa_data);
600 control = PPP_CONTROL(dst->sa_data);
601 protocol = PPP_PROTOCOL(dst->sa_data);
605 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
606 error = EAFNOSUPPORT;
611 * Drop this packet, or return an error, if necessary.
613 if (mode == NPMODE_ERROR) {
617 if (mode == NPMODE_DROP) {
623 * Add PPP header. If no space in first mbuf, allocate another.
624 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
626 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
627 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
634 m0->m_data -= PPP_HDRLEN;
636 cp = mtod(m0, u_char *);
639 *cp++ = protocol >> 8;
640 *cp++ = protocol & 0xff;
641 m0->m_len += PPP_HDRLEN;
643 if (sc->sc_flags & SC_LOG_OUTPKT) {
644 printf("ppp%d output: ", ifp->if_unit);
650 * See if bpf wants to look at the packet.
653 bpf_mtap(sc->sc_bpf, m0);
657 * Put the packet on the appropriate queue.
659 s = splimp(); /* splnet should be OK now */
660 if (mode == NPMODE_QUEUE) {
661 /* XXX we should limit the number of packets on this queue */
662 *sc->sc_npqtail = m0;
663 m0->m_nextpkt = NULL;
664 sc->sc_npqtail = &m0->m_nextpkt;
666 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
670 sc->sc_if.if_oerrors++;
687 * After a change in the NPmode for some NP, move packets from the
688 * npqueue to the send queue or the fast queue as appropriate.
689 * Should be called at splimp (actually splnet would probably suffice).
693 struct ppp_softc *sc;
695 struct mbuf *m, **mpp;
699 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
700 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
702 mode = sc->sc_npmode[NP_IP];
711 * This packet can now go on one of the queues to be sent.
715 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
718 sc->sc_if.if_oerrors++;
734 sc->sc_npqtail = mpp;
738 * Get a packet to send. This procedure is intended to be called
739 * at spltty()/splimp(), so it takes little time. If there isn't
740 * a packet waiting to go out, it schedules a software interrupt
741 * to prepare a new packet; the device start routine gets called
742 * again when a packet is ready.
746 struct ppp_softc *sc;
754 * Had a packet waiting - send it.
757 sc->sc_flags |= SC_TBUSY;
762 * Remember we wanted a packet and schedule a software interrupt.
764 sc->sc_flags &= ~SC_TBUSY;
765 schednetisr(NETISR_PPP);
771 * Software interrupt routine, called at splnet().
776 struct ppp_softc *sc;
782 for (i = 0; i < NPPP; ++i, ++sc) {
783 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
784 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
787 IF_DEQUEUE(&sc->sc_rawq, m);
797 * Grab another packet off a queue and apply VJ compression,
798 * packet compression, address/control and/or protocol compression
799 * if enabled. Should be called at splnet.
803 struct ppp_softc *sc;
808 int address, control, protocol;
812 * Grab a packet to send: first try the fast queue, then the
815 IF_DEQUEUE(&sc->sc_fastq, m);
817 IF_DEQUEUE(&sc->sc_if.if_snd, m);
822 * Extract the ppp header of the new packet.
823 * The ppp header will be in one mbuf.
825 cp = mtod(m, u_char *);
826 address = PPP_ADDRESS(cp);
827 control = PPP_CONTROL(cp);
828 protocol = PPP_PROTOCOL(cp);
833 * Update the time we sent the most recent packet.
835 sc->sc_last_sent = time.tv_sec;
839 * If the packet is a TCP/IP packet, see if we can compress it.
841 if (sc->sc_flags & SC_COMP_TCP) {
846 ip = (struct ip *) (cp + PPP_HDRLEN);
847 if (mp->m_len <= PPP_HDRLEN) {
851 ip = mtod(mp, struct ip *);
853 /* this code assumes the IP/TCP header is in one non-shared mbuf */
854 if (ip->ip_p == IPPROTO_TCP) {
855 type = vj_compress_tcp(mp, ip, &sc->sc_comp,
856 !(sc->sc_flags & SC_NO_TCP_CCID));
858 case TYPE_UNCOMPRESSED_TCP:
859 protocol = PPP_VJC_UNCOMP;
861 case TYPE_COMPRESSED_TCP:
862 protocol = PPP_VJC_COMP;
863 cp = mtod(m, u_char *);
864 cp[0] = address; /* header has moved */
869 cp[3] = protocol; /* update protocol in PPP header */
879 #endif /* PPP_COMPRESS */
883 if (protocol != PPP_LCP && protocol != PPP_CCP
884 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
889 for (mp = m; mp != NULL; mp = mp->m_next)
891 clen = (*sc->sc_xcomp->compress)
892 (sc->sc_xc_state, &mcomp, m, slen,
893 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
897 cp = mtod(m, u_char *);
901 #endif /* PPP_COMPRESS */
904 * Compress the address/control and protocol, if possible.
906 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
907 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
908 protocol != PPP_LCP) {
909 /* can compress address/control */
913 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
914 /* can compress protocol */
915 if (mtod(m, u_char *) == cp) {
916 cp[2] = cp[1]; /* move address/control up */
931 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
932 * 0 if it is about to be transmitted.
936 struct ppp_softc *sc;
945 * Get a pointer to the data after the PPP header.
947 if (m->m_len <= PPP_HDRLEN) {
951 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
954 dp = mtod(mp, u_char *) + PPP_HDRLEN;
957 ep = mtod(mp, u_char *) + mp->m_len;
958 if (dp + CCP_HDRLEN > ep)
960 slen = CCP_LENGTH(dp);
961 if (dp + slen > ep) {
962 if (sc->sc_flags & SC_DEBUG)
963 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
964 dp, slen, mtod(mp, u_char *), mp->m_len);
968 switch (CCP_CODE(dp)) {
972 /* CCP must be going down - disable compression */
973 if (sc->sc_flags & SC_CCP_UP) {
975 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
981 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
982 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
983 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
985 /* we're agreeing to send compressed packets. */
986 if (sc->sc_xc_state != NULL
987 && (*sc->sc_xcomp->comp_init)
988 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
989 sc->sc_if.if_unit, sc->sc_flags & SC_DEBUG)) {
991 sc->sc_flags |= SC_COMP_RUN;
995 /* peer is agreeing to send compressed packets. */
996 if (sc->sc_rc_state != NULL
997 && (*sc->sc_rcomp->decomp_init)
998 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
999 sc->sc_if.if_unit, 0, sc->sc_mru,
1000 sc->sc_flags & SC_DEBUG)) {
1002 sc->sc_flags |= SC_DECOMP_RUN;
1003 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1011 if (sc->sc_flags & SC_CCP_UP) {
1013 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1014 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1016 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1017 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1019 sc->sc_flags &= ~SC_DC_ERROR;
1029 * CCP is down; free (de)compressor state if necessary.
1033 struct ppp_softc *sc;
1035 if (sc->sc_xc_state) {
1036 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1037 sc->sc_xc_state = NULL;
1039 if (sc->sc_rc_state) {
1040 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1041 sc->sc_rc_state = NULL;
1044 #endif /* PPP_COMPRESS */
1047 * PPP packet input routine.
1048 * The caller has checked and removed the FCS and has inserted
1049 * the address/control bytes and the protocol high byte if they
1053 ppppktin(sc, m, lost)
1054 struct ppp_softc *sc;
1061 m->m_flags |= M_ERRMARK;
1062 IF_ENQUEUE(&sc->sc_rawq, m);
1063 schednetisr(NETISR_PPP);
1068 * Process a received PPP packet, doing decompression as necessary.
1070 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1071 TYPE_UNCOMPRESSED_TCP)
1075 struct ppp_softc *sc;
1078 struct ifqueue *inq;
1079 int s, ilen, xlen, proto, rv;
1080 u_char *cp, adrs, ctrl;
1081 struct mbuf *mp, *dmp;
1085 sc->sc_if.if_ipackets++;
1087 if (sc->sc_flags & SC_LOG_INPKT) {
1088 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1092 cp = mtod(m, u_char *);
1093 adrs = PPP_ADDRESS(cp);
1094 ctrl = PPP_CONTROL(cp);
1095 proto = PPP_PROTOCOL(cp);
1097 if (m->m_flags & M_ERRMARK) {
1098 m->m_flags &= ~M_ERRMARK;
1100 sc->sc_flags |= SC_VJ_RESET;
1106 * Decompress this packet if necessary, update the receiver's
1107 * dictionary, or take appropriate action on a CCP packet.
1109 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1110 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1111 /* decompress this packet */
1112 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1116 cp = mtod(m, u_char *);
1117 proto = PPP_PROTOCOL(cp);
1120 /* pass the compressed packet up to pppd, which may take
1121 CCP down or issue a Reset-Req. */
1122 if (sc->sc_flags & SC_DEBUG)
1123 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1125 sc->sc_flags |= SC_VJ_RESET;
1128 /* no error, but no decompressed packet produced */
1133 sc->sc_flags |= SC_DC_ERROR;
1135 case DECOMP_FATALERROR:
1136 sc->sc_flags |= SC_DC_FERROR;
1143 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1144 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1146 if (proto == PPP_CCP) {
1153 for (mp = m; mp != NULL; mp = mp->m_next)
1157 if (sc->sc_flags & SC_VJ_RESET) {
1159 * If we've missed a packet, we must toss subsequent compressed
1160 * packets which don't have an explicit connection ID.
1162 vj_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1164 sc->sc_flags &= ~SC_VJ_RESET;
1169 * See if we have a VJ-compressed packet to uncompress.
1171 if (proto == PPP_VJC_COMP) {
1172 if (sc->sc_flags & SC_REJ_COMP_TCP)
1175 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1176 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1177 &sc->sc_comp, &iphdr, &hlen);
1180 if (sc->sc_flags & SC_DEBUG)
1181 printf("ppp%d: VJ uncompress failed on type comp\n",
1186 /* Copy the PPP and IP headers into a new mbuf. */
1187 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1192 if (hlen + PPP_HDRLEN > MHLEN) {
1193 MCLGET(mp, M_DONTWAIT);
1194 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1196 goto bad; /* lose if big headers and no clusters */
1199 cp = mtod(mp, u_char *);
1205 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1206 mp->m_len = hlen + PPP_HDRLEN;
1209 * Trim the PPP and VJ headers off the old mbuf
1210 * and stick the new and old mbufs together.
1212 m->m_data += PPP_HDRLEN + xlen;
1213 m->m_len -= PPP_HDRLEN + xlen;
1214 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1215 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1216 mp->m_len += m->m_len;
1217 MFREE(m, mp->m_next);
1221 ilen += hlen - xlen;
1223 } else if (proto == PPP_VJC_UNCOMP) {
1224 if (sc->sc_flags & SC_REJ_COMP_TCP)
1227 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1228 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1229 &sc->sc_comp, &iphdr, &hlen);
1232 if (sc->sc_flags & SC_DEBUG)
1233 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1244 * If the packet will fit in a header mbuf, don't waste a
1245 * whole cluster on it.
1247 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1248 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1250 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1256 m->m_pkthdr.len = ilen;
1257 m->m_pkthdr.rcvif = &sc->sc_if;
1260 /* See if bpf wants to look at the packet. */
1262 bpf_mtap(sc->sc_bpf, m);
1270 * IP packet - take off the ppp header and pass it up to IP.
1272 if ((sc->sc_if.if_flags & IFF_UP) == 0
1273 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1274 /* interface is down - drop the packet. */
1278 m->m_pkthdr.len -= PPP_HDRLEN;
1279 m->m_data += PPP_HDRLEN;
1280 m->m_len -= PPP_HDRLEN;
1281 schednetisr(NETISR_IP);
1283 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1289 * Some other protocol - place on input queue for read().
1297 * Put the packet on the appropriate input queue.
1300 if (IF_QFULL(inq)) {
1303 if (sc->sc_flags & SC_DEBUG)
1304 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1305 sc->sc_if.if_iqdrops++;
1318 sc->sc_if.if_ierrors++;
1321 #define MAX_DUMP_BYTES 128
1327 char buf[3*MAX_DUMP_BYTES+4];
1330 static char digits[] = "0123456789abcdef";
1332 for (m = m0; m; m = m->m_next) {
1334 u_char *rptr = (u_char *)m->m_data;
1337 if (bp > buf + sizeof(buf) - 4)
1339 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1340 *bp++ = digits[*rptr++ & 0xf];
1344 if (bp > buf + sizeof(buf) - 3)
1354 printf("%s\n", buf);
1357 #endif /* NPPP > 0 */