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.4 1995/07/11 06:37:41 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] = {
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;
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] = ppp_intr;
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;
236 vj_compress_init(&sc->sc_comp, -1);
239 sc->sc_xc_state = NULL;
240 sc->sc_rc_state = NULL;
241 #endif /* PPP_COMPRESS */
242 for (i = 0; i < NUM_NP; ++i)
243 sc->sc_npmode[i] = NPMODE_ERROR;
244 sc->sc_npqueue = NULL;
245 sc->sc_npqtail = &sc->sc_npqueue;
246 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
252 * Deallocate a ppp unit. Must be called at splnet or higher.
256 struct ppp_softc *sc;
261 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
265 IF_DEQUEUE(&sc->sc_rawq, m);
271 IF_DEQUEUE(&sc->sc_inq, m);
277 IF_DEQUEUE(&sc->sc_fastq, m);
282 while ((m = sc->sc_npqueue) != NULL) {
283 sc->sc_npqueue = m->m_nextpkt;
286 if (sc->sc_togo != NULL) {
287 m_freem(sc->sc_togo);
292 sc->sc_xc_state = NULL;
293 sc->sc_rc_state = NULL;
294 #endif /* PPP_COMPRESS */
298 * Ioctl routine for generic ppp devices.
301 pppioctl(sc, cmd, data, flag, p)
302 struct ppp_softc *sc;
307 int s, error, flags, mru, nb, npx;
308 struct ppp_option_data *odp;
309 struct compressor **cp;
313 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
318 *(int *)data = sc->sc_inq.ifq_len;
322 *(int *)data = sc->sc_if.if_unit;
326 *(u_int *)data = sc->sc_flags;
330 if (error = suser(p->p_ucred, &p->p_acflag))
332 flags = *(int *)data & SC_MASK;
335 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
339 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
344 if (error = suser(p->p_ucred, &p->p_acflag))
347 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
352 *(int *)data = sc->sc_mru;
357 if (error = suser(p->p_ucred, &p->p_acflag))
360 vj_compress_init(&sc->sc_comp, *(int *)data);
366 if (error = suser(p->p_ucred, &p->p_acflag))
368 sc->sc_xfer = p->p_pid;
372 case PPPIOCSCOMPRESS:
373 if (error = suser(p->p_ucred, &p->p_acflag))
375 odp = (struct ppp_option_data *) data;
377 if (nb > sizeof(ccp_option))
378 nb = sizeof(ccp_option);
379 if (error = copyin(odp->ptr, ccp_option, nb))
381 if (ccp_option[1] < 2) /* preliminary check on the length byte */
383 for (cp = ppp_compressors; *cp != NULL; ++cp)
384 if ((*cp)->compress_proto == ccp_option[0]) {
386 * Found a handler for the protocol - try to allocate
387 * a compressor or decompressor.
392 if (sc->sc_xc_state != NULL)
393 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
395 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
396 if (sc->sc_xc_state == NULL) {
397 if (sc->sc_flags & SC_DEBUG)
398 printf("ppp%d: comp_alloc failed\n",
403 sc->sc_flags &= ~SC_COMP_RUN;
405 if (sc->sc_rc_state != NULL)
406 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
408 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
409 if (sc->sc_rc_state == NULL) {
410 if (sc->sc_flags & SC_DEBUG)
411 printf("ppp%d: decomp_alloc failed\n",
416 sc->sc_flags &= ~SC_DECOMP_RUN;
421 if (sc->sc_flags & SC_DEBUG)
422 printf("ppp%d: no compressor for [%x %x %x], %x\n",
423 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
425 return (EINVAL); /* no handler found */
426 #endif /* PPP_COMPRESS */
430 npi = (struct npioctl *) data;
431 switch (npi->protocol) {
438 if (cmd == PPPIOCGNPMODE) {
439 npi->mode = sc->sc_npmode[npx];
441 if (error = suser(p->p_ucred, &p->p_acflag))
443 if (npi->mode != sc->sc_npmode[npx]) {
445 sc->sc_npmode[npx] = npi->mode;
446 if (npi->mode != NPMODE_QUEUE) {
458 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
459 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
470 * Process an ioctl request to the ppp network interface.
473 pppsioctl(ifp, cmd, data)
474 register struct ifnet *ifp;
478 struct proc *p = curproc; /* XXX */
479 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
480 register struct ifaddr *ifa = (struct ifaddr *)data;
481 register struct ifreq *ifr = (struct ifreq *)data;
482 struct ppp_stats *psp;
484 struct ppp_comp_stats *pcp;
486 int s = splimp(), error = 0;
490 if ((ifp->if_flags & IFF_RUNNING) == 0)
491 ifp->if_flags &= ~IFF_UP;
495 if (ifa->ifa_addr->sa_family != AF_INET)
496 error = EAFNOSUPPORT;
500 if (ifa->ifa_addr->sa_family != AF_INET)
501 error = EAFNOSUPPORT;
505 if (error = suser(p->p_ucred, &p->p_acflag))
507 sc->sc_if.if_mtu = ifr->ifr_mtu;
511 ifr->ifr_mtu = sc->sc_if.if_mtu;
515 psp = &((struct ifpppstatsreq *) data)->stats;
516 bzero(psp, sizeof(*psp));
517 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
518 psp->p.ppp_ipackets = ifp->if_ipackets;
519 psp->p.ppp_ierrors = ifp->if_ierrors;
520 psp->p.ppp_obytes = sc->sc_bytessent;
521 psp->p.ppp_opackets = ifp->if_opackets;
522 psp->p.ppp_oerrors = ifp->if_oerrors;
524 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
525 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
526 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
527 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
528 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
529 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
530 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
531 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
537 pcp = &((struct ifpppcstatsreq *) data)->stats;
538 bzero(pcp, sizeof(*pcp));
539 if (sc->sc_xc_state != NULL)
540 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
541 if (sc->sc_rc_state != NULL)
542 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
544 #endif /* PPP_COMPRESS */
554 * Queue a packet. Start transmission if not active.
555 * Packet is placed in Information field of PPP frame.
558 pppoutput(ifp, m0, dst, rtp)
561 struct sockaddr *dst;
564 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
565 struct ppp_header *ph;
566 int protocol, address, control;
573 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
574 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
575 error = ENETDOWN; /* sort of */
580 * Compute PPP header.
582 m0->m_flags &= ~M_HIGHPRI;
583 switch (dst->sa_family) {
586 address = PPP_ALLSTATIONS;
589 mode = sc->sc_npmode[NP_IP];
592 * If this packet has the "low delay" bit set in the IP header,
593 * put it on the fastq instead.
595 ip = mtod(m0, struct ip *);
596 if (ip->ip_tos & IPTOS_LOWDELAY)
597 m0->m_flags |= M_HIGHPRI;
601 address = PPP_ADDRESS(dst->sa_data);
602 control = PPP_CONTROL(dst->sa_data);
603 protocol = PPP_PROTOCOL(dst->sa_data);
607 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
608 error = EAFNOSUPPORT;
613 * Drop this packet, or return an error, if necessary.
615 if (mode == NPMODE_ERROR) {
619 if (mode == NPMODE_DROP) {
625 * Add PPP header. If no space in first mbuf, allocate another.
626 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
628 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
629 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
636 m0->m_data -= PPP_HDRLEN;
638 cp = mtod(m0, u_char *);
641 *cp++ = protocol >> 8;
642 *cp++ = protocol & 0xff;
643 m0->m_len += PPP_HDRLEN;
645 if (sc->sc_flags & SC_LOG_OUTPKT) {
646 printf("ppp%d output: ", ifp->if_unit);
652 * See if bpf wants to look at the packet.
655 bpf_mtap(sc->sc_bpf, m0);
659 * Put the packet on the appropriate queue.
661 s = splimp(); /* splnet should be OK now */
662 if (mode == NPMODE_QUEUE) {
663 /* XXX we should limit the number of packets on this queue */
664 *sc->sc_npqtail = m0;
665 m0->m_nextpkt = NULL;
666 sc->sc_npqtail = &m0->m_nextpkt;
668 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
672 sc->sc_if.if_oerrors++;
689 * After a change in the NPmode for some NP, move packets from the
690 * npqueue to the send queue or the fast queue as appropriate.
691 * Should be called at splimp (actually splnet would probably suffice).
695 struct ppp_softc *sc;
697 struct mbuf *m, **mpp;
701 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
702 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
704 mode = sc->sc_npmode[NP_IP];
713 * This packet can now go on one of the queues to be sent.
717 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
720 sc->sc_if.if_oerrors++;
736 sc->sc_npqtail = mpp;
740 * Get a packet to send. This procedure is intended to be called
741 * at spltty()/splimp(), so it takes little time. If there isn't
742 * a packet waiting to go out, it schedules a software interrupt
743 * to prepare a new packet; the device start routine gets called
744 * again when a packet is ready.
748 struct ppp_softc *sc;
756 * Had a packet waiting - send it.
759 sc->sc_flags |= SC_TBUSY;
764 * Remember we wanted a packet and schedule a software interrupt.
766 sc->sc_flags &= ~SC_TBUSY;
767 schednetisr(NETISR_PPP);
773 * Software interrupt routine, called at splnet().
778 struct ppp_softc *sc;
784 for (i = 0; i < NPPP; ++i, ++sc) {
785 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
786 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
789 IF_DEQUEUE(&sc->sc_rawq, m);
799 * Grab another packet off a queue and apply VJ compression,
800 * packet compression, address/control and/or protocol compression
801 * if enabled. Should be called at splnet.
805 struct ppp_softc *sc;
810 int address, control, protocol;
814 * Grab a packet to send: first try the fast queue, then the
817 IF_DEQUEUE(&sc->sc_fastq, m);
819 IF_DEQUEUE(&sc->sc_if.if_snd, m);
824 * Extract the ppp header of the new packet.
825 * The ppp header will be in one mbuf.
827 cp = mtod(m, u_char *);
828 address = PPP_ADDRESS(cp);
829 control = PPP_CONTROL(cp);
830 protocol = PPP_PROTOCOL(cp);
835 * Update the time we sent the most recent packet.
837 sc->sc_last_sent = time.tv_sec;
841 * If the packet is a TCP/IP packet, see if we can compress it.
843 if (sc->sc_flags & SC_COMP_TCP) {
848 ip = (struct ip *) (cp + PPP_HDRLEN);
849 if (mp->m_len <= PPP_HDRLEN) {
853 ip = mtod(mp, struct ip *);
855 /* this code assumes the IP/TCP header is in one non-shared mbuf */
856 if (ip->ip_p == IPPROTO_TCP) {
857 type = vj_compress_tcp(mp, ip, &sc->sc_comp,
858 !(sc->sc_flags & SC_NO_TCP_CCID));
860 case TYPE_UNCOMPRESSED_TCP:
861 protocol = PPP_VJC_UNCOMP;
863 case TYPE_COMPRESSED_TCP:
864 protocol = PPP_VJC_COMP;
865 cp = mtod(m, u_char *);
866 cp[0] = address; /* header has moved */
871 cp[3] = protocol; /* update protocol in PPP header */
881 #endif /* PPP_COMPRESS */
885 if (protocol != PPP_LCP && protocol != PPP_CCP
886 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
887 struct mbuf *mcomp = NULL;
891 for (mp = m; mp != NULL; mp = mp->m_next)
893 clen = (*sc->sc_xcomp->compress)
894 (sc->sc_xc_state, &mcomp, m, slen,
895 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
899 cp = mtod(m, u_char *);
903 #endif /* PPP_COMPRESS */
906 * Compress the address/control and protocol, if possible.
908 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
909 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
910 protocol != PPP_LCP) {
911 /* can compress address/control */
915 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
916 /* can compress protocol */
917 if (mtod(m, u_char *) == cp) {
918 cp[2] = cp[1]; /* move address/control up */
933 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
934 * 0 if it is about to be transmitted.
938 struct ppp_softc *sc;
947 * Get a pointer to the data after the PPP header.
949 if (m->m_len <= PPP_HDRLEN) {
953 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
956 dp = mtod(mp, u_char *) + PPP_HDRLEN;
959 ep = mtod(mp, u_char *) + mp->m_len;
960 if (dp + CCP_HDRLEN > ep)
962 slen = CCP_LENGTH(dp);
963 if (dp + slen > ep) {
964 if (sc->sc_flags & SC_DEBUG)
965 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
966 dp, slen, mtod(mp, u_char *), mp->m_len);
970 switch (CCP_CODE(dp)) {
974 /* CCP must be going down - disable compression */
975 if (sc->sc_flags & SC_CCP_UP) {
977 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
983 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
984 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
985 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
987 /* we're agreeing to send compressed packets. */
988 if (sc->sc_xc_state != NULL
989 && (*sc->sc_xcomp->comp_init)
990 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
991 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
993 sc->sc_flags |= SC_COMP_RUN;
997 /* peer is agreeing to send compressed packets. */
998 if (sc->sc_rc_state != NULL
999 && (*sc->sc_rcomp->decomp_init)
1000 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1001 sc->sc_if.if_unit, 0, sc->sc_mru,
1002 sc->sc_flags & SC_DEBUG)) {
1004 sc->sc_flags |= SC_DECOMP_RUN;
1005 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1013 if (sc->sc_flags & SC_CCP_UP) {
1015 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1016 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1018 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1019 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1021 sc->sc_flags &= ~SC_DC_ERROR;
1031 * CCP is down; free (de)compressor state if necessary.
1035 struct ppp_softc *sc;
1037 if (sc->sc_xc_state) {
1038 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1039 sc->sc_xc_state = NULL;
1041 if (sc->sc_rc_state) {
1042 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1043 sc->sc_rc_state = NULL;
1046 #endif /* PPP_COMPRESS */
1049 * PPP packet input routine.
1050 * The caller has checked and removed the FCS and has inserted
1051 * the address/control bytes and the protocol high byte if they
1055 ppppktin(sc, m, lost)
1056 struct ppp_softc *sc;
1063 m->m_flags |= M_ERRMARK;
1064 IF_ENQUEUE(&sc->sc_rawq, m);
1065 schednetisr(NETISR_PPP);
1070 * Process a received PPP packet, doing decompression as necessary.
1072 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1073 TYPE_UNCOMPRESSED_TCP)
1077 struct ppp_softc *sc;
1080 struct ifqueue *inq;
1081 int s, ilen, xlen, proto, rv;
1082 u_char *cp, adrs, ctrl;
1083 struct mbuf *mp, *dmp = NULL;
1087 sc->sc_if.if_ipackets++;
1089 if (sc->sc_flags & SC_LOG_INPKT) {
1090 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1094 cp = mtod(m, u_char *);
1095 adrs = PPP_ADDRESS(cp);
1096 ctrl = PPP_CONTROL(cp);
1097 proto = PPP_PROTOCOL(cp);
1099 if (m->m_flags & M_ERRMARK) {
1100 m->m_flags &= ~M_ERRMARK;
1102 sc->sc_flags |= SC_VJ_RESET;
1108 * Decompress this packet if necessary, update the receiver's
1109 * dictionary, or take appropriate action on a CCP packet.
1111 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1112 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1113 /* decompress this packet */
1114 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1115 if (rv == DECOMP_OK) {
1118 /* no error, but no decompressed packet produced */
1122 cp = mtod(m, u_char *);
1123 proto = PPP_PROTOCOL(cp);
1127 * An error has occurred in decompression.
1128 * Pass the compressed packet up to pppd, which may take
1129 * CCP down or issue a Reset-Req.
1131 if (sc->sc_flags & SC_DEBUG)
1132 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1134 sc->sc_flags |= SC_VJ_RESET;
1135 if (rv == DECOMP_ERROR)
1136 sc->sc_flags |= SC_DC_ERROR;
1138 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 */