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.7 1996/07/01 01:00:27 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>
119 /* This definition should be moved to net/netisr.h */
120 #define NETISR_PPP 26 /* PPP software interrupt */
124 #define PACKETPTR struct mbuf *
125 #include <net/ppp-comp.h>
128 static void ppp_requeue __P((struct ppp_softc *));
129 static void ppp_outpkt __P((struct ppp_softc *));
130 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
131 static void ppp_ccp_closed __P((struct ppp_softc *));
132 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
133 static void pppdumpm __P((struct mbuf *m0));
136 * Some useful mbuf macros not in mbuf.h.
138 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
140 #define M_DATASTART(m) \
141 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
142 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
144 #define M_DATASIZE(m) \
145 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
146 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
149 * We steal two bits in the mbuf m_flags, to mark high-priority packets
150 * for output, and received packets following lost/corrupted packets.
152 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
153 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
158 * List of compressors we know about.
159 * We leave some space so maybe we can modload compressors.
162 extern struct compressor ppp_bsd_compress;
163 extern struct compressor ppp_deflate;
165 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 | IFF_MULTICAST;
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] = pppintr;
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;
233 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
235 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
238 vj_compress_init(sc->sc_comp, -1);
241 sc->sc_xc_state = NULL;
242 sc->sc_rc_state = NULL;
243 #endif /* PPP_COMPRESS */
244 for (i = 0; i < NUM_NP; ++i)
245 sc->sc_npmode[i] = NPMODE_ERROR;
246 sc->sc_npqueue = NULL;
247 sc->sc_npqtail = &sc->sc_npqueue;
248 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
254 * Deallocate a ppp unit. Must be called at splnet or higher.
258 struct ppp_softc *sc;
263 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
267 IF_DEQUEUE(&sc->sc_rawq, m);
273 IF_DEQUEUE(&sc->sc_inq, m);
279 IF_DEQUEUE(&sc->sc_fastq, m);
284 while ((m = sc->sc_npqueue) != NULL) {
285 sc->sc_npqueue = m->m_nextpkt;
288 if (sc->sc_togo != NULL) {
289 m_freem(sc->sc_togo);
294 sc->sc_xc_state = NULL;
295 sc->sc_rc_state = NULL;
296 #endif /* PPP_COMPRESS */
298 if (sc->sc_comp != 0) {
299 FREE(sc->sc_comp, M_DEVBUF);
306 * Ioctl routine for generic ppp devices.
309 pppioctl(sc, cmd, data, flag, p)
310 struct ppp_softc *sc;
316 int s, error, flags, mru, nb, npx;
317 struct ppp_option_data *odp;
318 struct compressor **cp;
322 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
327 *(int *)data = sc->sc_inq.ifq_len;
331 *(int *)data = sc->sc_if.if_unit;
335 *(u_int *)data = sc->sc_flags;
339 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
341 flags = *(int *)data & SC_MASK;
344 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
348 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
353 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
356 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
361 *(int *)data = sc->sc_mru;
366 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
370 vj_compress_init(sc->sc_comp, *(int *)data);
377 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
379 sc->sc_xfer = p->p_pid;
383 case PPPIOCSCOMPRESS:
384 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
386 odp = (struct ppp_option_data *) data;
388 if (nb > sizeof(ccp_option))
389 nb = sizeof(ccp_option);
390 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
392 if (ccp_option[1] < 2) /* preliminary check on the length byte */
394 for (cp = ppp_compressors; *cp != NULL; ++cp)
395 if ((*cp)->compress_proto == ccp_option[0]) {
397 * Found a handler for the protocol - try to allocate
398 * a compressor or decompressor.
403 if (sc->sc_xc_state != NULL)
404 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
406 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
407 if (sc->sc_xc_state == NULL) {
408 if (sc->sc_flags & SC_DEBUG)
409 printf("ppp%d: comp_alloc failed\n",
414 sc->sc_flags &= ~SC_COMP_RUN;
418 if (sc->sc_rc_state != NULL)
419 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
421 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
422 if (sc->sc_rc_state == NULL) {
423 if (sc->sc_flags & SC_DEBUG)
424 printf("ppp%d: decomp_alloc failed\n",
429 sc->sc_flags &= ~SC_DECOMP_RUN;
434 if (sc->sc_flags & SC_DEBUG)
435 printf("ppp%d: no compressor for [%x %x %x], %x\n",
436 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
438 return (EINVAL); /* no handler found */
439 #endif /* PPP_COMPRESS */
443 npi = (struct npioctl *) data;
444 switch (npi->protocol) {
451 if (cmd == PPPIOCGNPMODE) {
452 npi->mode = sc->sc_npmode[npx];
454 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
456 if (npi->mode != sc->sc_npmode[npx]) {
458 sc->sc_npmode[npx] = npi->mode;
459 if (npi->mode != NPMODE_QUEUE) {
471 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
472 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
483 * Process an ioctl request to the ppp network interface.
486 pppsioctl(ifp, cmd, data)
487 register struct ifnet *ifp;
491 struct proc *p = curproc; /* XXX */
492 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
493 register struct ifaddr *ifa = (struct ifaddr *)data;
494 register struct ifreq *ifr = (struct ifreq *)data;
495 struct ppp_stats *psp;
497 struct ppp_comp_stats *pcp;
499 int s = splimp(), error = 0;
503 if ((ifp->if_flags & IFF_RUNNING) == 0)
504 ifp->if_flags &= ~IFF_UP;
508 if (ifa->ifa_addr->sa_family != AF_INET)
509 error = EAFNOSUPPORT;
513 if (ifa->ifa_addr->sa_family != AF_INET)
514 error = EAFNOSUPPORT;
518 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
520 sc->sc_if.if_mtu = ifr->ifr_mtu;
524 ifr->ifr_mtu = sc->sc_if.if_mtu;
530 error = EAFNOSUPPORT;
533 switch(ifr->ifr_addr.sa_family) {
539 error = EAFNOSUPPORT;
545 psp = &((struct ifpppstatsreq *) data)->stats;
546 bzero(psp, sizeof(*psp));
547 psp->p = sc->sc_stats;
548 #if defined(VJC) && !defined(SL_NO_STATS)
550 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
551 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
552 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
553 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
554 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
555 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
556 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
557 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
564 pcp = &((struct ifpppcstatsreq *) data)->stats;
565 bzero(pcp, sizeof(*pcp));
566 if (sc->sc_xc_state != NULL)
567 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
568 if (sc->sc_rc_state != NULL)
569 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
571 #endif /* PPP_COMPRESS */
581 * Queue a packet. Start transmission if not active.
582 * Packet is placed in Information field of PPP frame.
585 pppoutput(ifp, m0, dst, rtp)
588 struct sockaddr *dst;
591 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
592 int protocol, address, control;
599 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
600 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
601 error = ENETDOWN; /* sort of */
606 * Compute PPP header.
608 m0->m_flags &= ~M_HIGHPRI;
609 switch (dst->sa_family) {
612 address = PPP_ALLSTATIONS;
615 mode = sc->sc_npmode[NP_IP];
618 * If this packet has the "low delay" bit set in the IP header,
619 * put it on the fastq instead.
621 ip = mtod(m0, struct ip *);
622 if (ip->ip_tos & IPTOS_LOWDELAY)
623 m0->m_flags |= M_HIGHPRI;
627 address = PPP_ADDRESS(dst->sa_data);
628 control = PPP_CONTROL(dst->sa_data);
629 protocol = PPP_PROTOCOL(dst->sa_data);
633 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
634 error = EAFNOSUPPORT;
639 * Drop this packet, or return an error, if necessary.
641 if (mode == NPMODE_ERROR) {
645 if (mode == NPMODE_DROP) {
651 * Add PPP header. If no space in first mbuf, allocate another.
652 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
654 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
655 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
662 m0->m_data -= PPP_HDRLEN;
664 cp = mtod(m0, u_char *);
667 *cp++ = protocol >> 8;
668 *cp++ = protocol & 0xff;
669 m0->m_len += PPP_HDRLEN;
671 if (sc->sc_flags & SC_LOG_OUTPKT) {
672 printf("ppp%d output: ", ifp->if_unit);
676 if ((protocol & 0x8000) == 0) {
678 * Update the time we sent the most recent data packet.
680 sc->sc_last_sent = time.tv_sec;
685 * See if bpf wants to look at the packet.
688 bpf_mtap(sc->sc_bpf, m0);
692 * Put the packet on the appropriate queue.
695 if (mode == NPMODE_QUEUE) {
696 /* XXX we should limit the number of packets on this queue */
697 *sc->sc_npqtail = m0;
698 m0->m_nextpkt = NULL;
699 sc->sc_npqtail = &m0->m_nextpkt;
701 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
702 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
705 sc->sc_if.if_oerrors++;
706 sc->sc_stats.ppp_oerrors++;
713 ifp->if_lastchange = time;
715 ifp->if_obytes += len;
726 * After a change in the NPmode for some NP, move packets from the
727 * npqueue to the send queue or the fast queue as appropriate.
728 * Should be called at splnet.
732 struct ppp_softc *sc;
734 struct mbuf *m, **mpp;
738 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
739 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
741 mode = sc->sc_npmode[NP_IP];
750 * This packet can now go on one of the queues to be sent.
754 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
757 sc->sc_if.if_oerrors++;
758 sc->sc_stats.ppp_oerrors++;
774 sc->sc_npqtail = mpp;
778 * Transmitter has finished outputting some stuff;
779 * remember to call sc->sc_start later at splnet.
783 struct ppp_softc *sc;
787 sc->sc_flags &= ~SC_TBUSY;
788 schednetisr(NETISR_PPP);
793 * Get a packet to send. This procedure is intended to be called at
794 * splnet, since it may involve time-consuming operations such as
795 * applying VJ compression, packet compression, address/control and/or
796 * protocol field compression to the packet.
800 struct ppp_softc *sc;
804 int address, control, protocol;
808 * Grab a packet to send: first try the fast queue, then the
811 IF_DEQUEUE(&sc->sc_fastq, m);
813 IF_DEQUEUE(&sc->sc_if.if_snd, m);
817 ++sc->sc_stats.ppp_opackets;
820 * Extract the ppp header of the new packet.
821 * The ppp header will be in one mbuf.
823 cp = mtod(m, u_char *);
824 address = PPP_ADDRESS(cp);
825 control = PPP_CONTROL(cp);
826 protocol = PPP_PROTOCOL(cp);
832 * If the packet is a TCP/IP packet, see if we can compress it.
834 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
839 ip = (struct ip *) (cp + PPP_HDRLEN);
840 if (mp->m_len <= PPP_HDRLEN) {
844 ip = mtod(mp, struct ip *);
846 /* this code assumes the IP/TCP header is in one non-shared mbuf */
847 if (ip->ip_p == IPPROTO_TCP) {
848 type = vj_compress_tcp(mp, ip, sc->sc_comp,
849 !(sc->sc_flags & SC_NO_TCP_CCID));
851 case TYPE_UNCOMPRESSED_TCP:
852 protocol = PPP_VJC_UNCOMP;
854 case TYPE_COMPRESSED_TCP:
855 protocol = PPP_VJC_COMP;
856 cp = mtod(m, u_char *);
857 cp[0] = address; /* header has moved */
862 cp[3] = protocol; /* update protocol in PPP header */
872 #endif /* PPP_COMPRESS */
876 if (protocol != PPP_LCP && protocol != PPP_CCP
877 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
878 struct mbuf *mcomp = NULL;
882 for (mp = m; mp != NULL; mp = mp->m_next)
884 clen = (*sc->sc_xcomp->compress)
885 (sc->sc_xc_state, &mcomp, m, slen,
886 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
890 cp = mtod(m, u_char *);
894 #endif /* PPP_COMPRESS */
897 * Compress the address/control and protocol, if possible.
899 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
900 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
901 protocol != PPP_LCP) {
902 /* can compress address/control */
906 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
907 /* can compress protocol */
908 if (mtod(m, u_char *) == cp) {
909 cp[2] = cp[1]; /* move address/control up */
920 * Software interrupt routine, called at splnet.
925 struct ppp_softc *sc;
931 for (i = 0; i < NPPP; ++i, ++sc) {
932 if (!(sc->sc_flags & SC_TBUSY)
933 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
935 sc->sc_flags |= SC_TBUSY;
941 IF_DEQUEUE(&sc->sc_rawq, m);
953 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
954 * 0 if it is about to be transmitted.
958 struct ppp_softc *sc;
967 * Get a pointer to the data after the PPP header.
969 if (m->m_len <= PPP_HDRLEN) {
973 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
976 dp = mtod(mp, u_char *) + PPP_HDRLEN;
979 ep = mtod(mp, u_char *) + mp->m_len;
980 if (dp + CCP_HDRLEN > ep)
982 slen = CCP_LENGTH(dp);
983 if (dp + slen > ep) {
984 if (sc->sc_flags & SC_DEBUG)
985 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
986 dp, slen, mtod(mp, u_char *), mp->m_len);
990 switch (CCP_CODE(dp)) {
994 /* CCP must be going down - disable compression */
995 if (sc->sc_flags & SC_CCP_UP) {
997 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1003 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1004 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1005 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1007 /* we're agreeing to send compressed packets. */
1008 if (sc->sc_xc_state != NULL
1009 && (*sc->sc_xcomp->comp_init)
1010 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1011 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1013 sc->sc_flags |= SC_COMP_RUN;
1017 /* peer is agreeing to send compressed packets. */
1018 if (sc->sc_rc_state != NULL
1019 && (*sc->sc_rcomp->decomp_init)
1020 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1021 sc->sc_if.if_unit, 0, sc->sc_mru,
1022 sc->sc_flags & SC_DEBUG)) {
1024 sc->sc_flags |= SC_DECOMP_RUN;
1025 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1033 if (sc->sc_flags & SC_CCP_UP) {
1035 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1036 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1038 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1039 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1041 sc->sc_flags &= ~SC_DC_ERROR;
1051 * CCP is down; free (de)compressor state if necessary.
1055 struct ppp_softc *sc;
1057 if (sc->sc_xc_state) {
1058 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1059 sc->sc_xc_state = NULL;
1061 if (sc->sc_rc_state) {
1062 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1063 sc->sc_rc_state = NULL;
1066 #endif /* PPP_COMPRESS */
1069 * PPP packet input routine.
1070 * The caller has checked and removed the FCS and has inserted
1071 * the address/control bytes and the protocol high byte if they
1075 ppppktin(sc, m, lost)
1076 struct ppp_softc *sc;
1083 m->m_flags |= M_ERRMARK;
1084 IF_ENQUEUE(&sc->sc_rawq, m);
1085 schednetisr(NETISR_PPP);
1090 * Process a received PPP packet, doing decompression as necessary.
1091 * Should be called at splnet.
1093 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1094 TYPE_UNCOMPRESSED_TCP)
1098 struct ppp_softc *sc;
1101 struct ifnet *ifp = &sc->sc_if;
1102 struct ifqueue *inq;
1103 int s, ilen, xlen, proto, rv;
1104 u_char *cp, adrs, ctrl;
1105 struct mbuf *mp, *dmp = NULL;
1109 sc->sc_stats.ppp_ipackets++;
1111 if (sc->sc_flags & SC_LOG_INPKT) {
1113 for (mp = m; mp != NULL; mp = mp->m_next)
1115 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1119 cp = mtod(m, u_char *);
1120 adrs = PPP_ADDRESS(cp);
1121 ctrl = PPP_CONTROL(cp);
1122 proto = PPP_PROTOCOL(cp);
1124 if (m->m_flags & M_ERRMARK) {
1125 m->m_flags &= ~M_ERRMARK;
1127 sc->sc_flags |= SC_VJ_RESET;
1133 * Decompress this packet if necessary, update the receiver's
1134 * dictionary, or take appropriate action on a CCP packet.
1136 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1137 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1138 /* decompress this packet */
1139 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1140 if (rv == DECOMP_OK) {
1143 /* no error, but no decompressed packet produced */
1147 cp = mtod(m, u_char *);
1148 proto = PPP_PROTOCOL(cp);
1152 * An error has occurred in decompression.
1153 * Pass the compressed packet up to pppd, which may take
1154 * CCP down or issue a Reset-Req.
1156 if (sc->sc_flags & SC_DEBUG)
1157 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1159 sc->sc_flags |= SC_VJ_RESET;
1160 if (rv == DECOMP_ERROR)
1161 sc->sc_flags |= SC_DC_ERROR;
1163 sc->sc_flags |= SC_DC_FERROR;
1168 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1169 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1171 if (proto == PPP_CCP) {
1178 for (mp = m; mp != NULL; mp = mp->m_next)
1182 if (sc->sc_flags & SC_VJ_RESET) {
1184 * If we've missed a packet, we must toss subsequent compressed
1185 * packets which don't have an explicit connection ID.
1188 vj_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1190 sc->sc_flags &= ~SC_VJ_RESET;
1195 * See if we have a VJ-compressed packet to uncompress.
1197 if (proto == PPP_VJC_COMP) {
1198 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1201 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1202 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1203 sc->sc_comp, &iphdr, &hlen);
1206 if (sc->sc_flags & SC_DEBUG)
1207 printf("ppp%d: VJ uncompress failed on type comp\n",
1212 /* Copy the PPP and IP headers into a new mbuf. */
1213 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1218 if (hlen + PPP_HDRLEN > MHLEN) {
1219 MCLGET(mp, M_DONTWAIT);
1220 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1222 goto bad; /* lose if big headers and no clusters */
1225 cp = mtod(mp, u_char *);
1231 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1232 mp->m_len = hlen + PPP_HDRLEN;
1235 * Trim the PPP and VJ headers off the old mbuf
1236 * and stick the new and old mbufs together.
1238 m->m_data += PPP_HDRLEN + xlen;
1239 m->m_len -= PPP_HDRLEN + xlen;
1240 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1241 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1242 mp->m_len += m->m_len;
1243 MFREE(m, mp->m_next);
1247 ilen += hlen - xlen;
1249 } else if (proto == PPP_VJC_UNCOMP) {
1250 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1253 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1254 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1255 sc->sc_comp, &iphdr, &hlen);
1258 if (sc->sc_flags & SC_DEBUG)
1259 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1270 * If the packet will fit in a header mbuf, don't waste a
1271 * whole cluster on it.
1273 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1274 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1276 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1282 m->m_pkthdr.len = ilen;
1283 m->m_pkthdr.rcvif = ifp;
1286 * Record the time that we received this packet.
1288 if ((proto & 0x8000) == 0) {
1289 sc->sc_last_recv = time.tv_sec;
1293 /* See if bpf wants to look at the packet. */
1295 bpf_mtap(sc->sc_bpf, m);
1303 * IP packet - take off the ppp header and pass it up to IP.
1305 if ((ifp->if_flags & IFF_UP) == 0
1306 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1307 /* interface is down - drop the packet. */
1311 m->m_pkthdr.len -= PPP_HDRLEN;
1312 m->m_data += PPP_HDRLEN;
1313 m->m_len -= PPP_HDRLEN;
1314 schednetisr(NETISR_IP);
1321 * Some other protocol - place on input queue for read().
1329 * Put the packet on the appropriate input queue.
1332 if (IF_QFULL(inq)) {
1335 if (sc->sc_flags & SC_DEBUG)
1336 printf("ppp%d: input queue full\n", ifp->if_unit);
1343 ifp->if_ibytes += ilen;
1344 ifp->if_lastchange = time;
1353 sc->sc_if.if_ierrors++;
1354 sc->sc_stats.ppp_ierrors++;
1357 #define MAX_DUMP_BYTES 128
1363 char buf[3*MAX_DUMP_BYTES+4];
1366 static char digits[] = "0123456789abcdef";
1368 for (m = m0; m; m = m->m_next) {
1370 u_char *rptr = (u_char *)m->m_data;
1373 if (bp > buf + sizeof(buf) - 4)
1375 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1376 *bp++ = digits[*rptr++ & 0xf];
1380 if (bp > buf + sizeof(buf) - 3)
1390 printf("%s\n", buf);
1393 #endif /* NPPP > 0 */