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.1 1994/12/15 22:28:09 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 #define PACKETPTR struct mbuf *
118 #include <net/ppp-comp.h>
121 void pppattach __P((void));
122 int pppioctl __P((struct ppp_softc *sc, int cmd, caddr_t data, int flag,
124 int pppoutput __P((struct ifnet *ifp, struct mbuf *m0,
125 struct sockaddr *dst, struct rtentry *rtp));
126 int pppsioctl __P((struct ifnet *ifp, int cmd, caddr_t data));
127 void pppintr __P((void));
129 static void ppp_requeue __P((struct ppp_softc *));
130 static void ppp_outpkt __P((struct ppp_softc *));
131 static int ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
132 static void ppp_ccp_closed __P((struct ppp_softc *));
133 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
134 static void pppdumpm __P((struct mbuf *m0));
137 * Some useful mbuf macros not in mbuf.h.
139 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
141 #define M_DATASTART(m) \
142 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
143 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
145 #define M_DATASIZE(m) \
146 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
147 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
150 * We steal two bits in the mbuf m_flags, to mark high-priority packets
151 * for output, and received packets following lost/corrupted packets.
153 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
154 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
159 * List of compressors we know about.
160 * We leave some space so maybe we can modload compressors.
163 extern struct compressor ppp_bsd_compress;
165 struct compressor *ppp_compressors[8] = {
169 #endif /* PPP_COMPRESS */
171 TEXT_SET(pseudo_set, pppattach);
174 * Called from boot code to establish ppp interfaces.
179 register struct ppp_softc *sc;
181 extern void (*netisrs[])__P((void));
183 for (sc = ppp_softc; i < NPPP; sc++) {
184 sc->sc_if.if_name = "ppp";
185 sc->sc_if.if_unit = i++;
186 sc->sc_if.if_mtu = PPP_MTU;
187 sc->sc_if.if_flags = IFF_POINTOPOINT;
188 sc->sc_if.if_type = IFT_PPP;
189 sc->sc_if.if_hdrlen = PPP_HDRLEN;
190 sc->sc_if.if_ioctl = pppsioctl;
191 sc->sc_if.if_output = pppoutput;
192 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
193 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
194 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
195 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
196 if_attach(&sc->sc_if);
198 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
201 netisrs[NETISR_PPP] = ppp_intr;
205 * Allocate a ppp interface unit and initialize it.
212 struct ppp_softc *sc;
214 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
215 if (sc->sc_xfer == pid) {
219 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
220 if (sc->sc_devp == NULL)
226 sc->sc_mru = PPP_MRU;
227 sc->sc_relinq = NULL;
229 vj_compress_init(&sc->sc_comp, -1);
232 sc->sc_xc_state = NULL;
233 sc->sc_rc_state = NULL;
234 #endif /* PPP_COMPRESS */
235 for (i = 0; i < NUM_NP; ++i)
236 sc->sc_npmode[i] = NPMODE_ERROR;
237 sc->sc_npqueue = NULL;
238 sc->sc_npqtail = &sc->sc_npqueue;
239 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
245 * Deallocate a ppp unit. Must be called at splnet or higher.
249 struct ppp_softc *sc;
254 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
258 IF_DEQUEUE(&sc->sc_rawq, m);
264 IF_DEQUEUE(&sc->sc_inq, m);
270 IF_DEQUEUE(&sc->sc_fastq, m);
275 while ((m = sc->sc_npqueue) != NULL) {
276 sc->sc_npqueue = m->m_nextpkt;
279 if (sc->sc_togo != NULL) {
280 m_freem(sc->sc_togo);
285 sc->sc_xc_state = NULL;
286 sc->sc_rc_state = NULL;
287 #endif /* PPP_COMPRESS */
291 * Ioctl routine for generic ppp devices.
294 pppioctl(sc, cmd, data, flag, p)
295 struct ppp_softc *sc;
300 int s, error, flags, mru, nb, npx;
301 struct ppp_option_data *odp;
302 struct compressor **cp;
306 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
311 *(int *)data = sc->sc_inq.ifq_len;
315 *(int *)data = sc->sc_if.if_unit;
319 *(u_int *)data = sc->sc_flags;
323 if (error = suser(p->p_ucred, &p->p_acflag))
325 flags = *(int *)data & SC_MASK;
328 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
332 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
337 if (error = suser(p->p_ucred, &p->p_acflag))
340 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
345 *(int *)data = sc->sc_mru;
350 if (error = suser(p->p_ucred, &p->p_acflag))
353 vj_compress_init(&sc->sc_comp, *(int *)data);
359 if (error = suser(p->p_ucred, &p->p_acflag))
361 sc->sc_xfer = p->p_pid;
365 case PPPIOCSCOMPRESS:
366 if (error = suser(p->p_ucred, &p->p_acflag))
368 odp = (struct ppp_option_data *) data;
370 if (nb > sizeof(ccp_option))
371 nb = sizeof(ccp_option);
372 if (error = copyin(odp->ptr, ccp_option, nb))
374 if (ccp_option[1] < 2) /* preliminary check on the length byte */
376 for (cp = ppp_compressors; *cp != NULL; ++cp)
377 if ((*cp)->compress_proto == ccp_option[0]) {
379 * Found a handler for the protocol - try to allocate
380 * a compressor or decompressor.
385 if (sc->sc_xc_state != NULL)
386 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
388 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
389 if (sc->sc_xc_state == NULL) {
390 if (sc->sc_flags & SC_DEBUG)
391 printf("ppp%d: comp_alloc failed\n",
396 sc->sc_flags &= ~SC_COMP_RUN;
398 if (sc->sc_rc_state != NULL)
399 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
401 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
402 if (sc->sc_rc_state == NULL) {
403 if (sc->sc_flags & SC_DEBUG)
404 printf("ppp%d: decomp_alloc failed\n",
409 sc->sc_flags &= ~SC_DECOMP_RUN;
414 if (sc->sc_flags & SC_DEBUG)
415 printf("ppp%d: no compressor for [%x %x %x], %x\n",
416 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
418 return (EINVAL); /* no handler found */
419 #endif /* PPP_COMPRESS */
423 npi = (struct npioctl *) data;
424 switch (npi->protocol) {
431 if (cmd == PPPIOCGNPMODE) {
432 npi->mode = sc->sc_npmode[npx];
434 if (error = suser(p->p_ucred, &p->p_acflag))
436 if (npi->mode != sc->sc_npmode[npx]) {
438 sc->sc_npmode[npx] = npi->mode;
439 if (npi->mode != NPMODE_QUEUE) {
451 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
452 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
463 * Process an ioctl request to the ppp network interface.
466 pppsioctl(ifp, cmd, data)
467 register struct ifnet *ifp;
471 struct proc *p = curproc; /* XXX */
472 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
473 register struct ifaddr *ifa = (struct ifaddr *)data;
474 register struct ifreq *ifr = (struct ifreq *)data;
475 struct ppp_stats *psp;
477 struct ppp_comp_stats *pcp;
479 int s = splimp(), error = 0;
483 if ((ifp->if_flags & IFF_RUNNING) == 0)
484 ifp->if_flags &= ~IFF_UP;
488 if (ifa->ifa_addr->sa_family != AF_INET)
489 error = EAFNOSUPPORT;
493 if (ifa->ifa_addr->sa_family != AF_INET)
494 error = EAFNOSUPPORT;
498 if (error = suser(p->p_ucred, &p->p_acflag))
500 sc->sc_if.if_mtu = ifr->ifr_mtu;
504 ifr->ifr_mtu = sc->sc_if.if_mtu;
508 psp = &((struct ifpppstatsreq *) data)->stats;
509 bzero(psp, sizeof(*psp));
510 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
511 psp->p.ppp_ipackets = ifp->if_ipackets;
512 psp->p.ppp_ierrors = ifp->if_ierrors;
513 psp->p.ppp_obytes = sc->sc_bytessent;
514 psp->p.ppp_opackets = ifp->if_opackets;
515 psp->p.ppp_oerrors = ifp->if_oerrors;
517 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
518 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
519 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
520 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
521 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
522 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
523 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
524 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
530 pcp = &((struct ifpppcstatsreq *) data)->stats;
531 bzero(pcp, sizeof(*pcp));
532 if (sc->sc_xc_state != NULL)
533 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
534 if (sc->sc_rc_state != NULL)
535 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
537 #endif /* PPP_COMPRESS */
547 * Queue a packet. Start transmission if not active.
548 * Packet is placed in Information field of PPP frame.
551 pppoutput(ifp, m0, dst, rtp)
554 struct sockaddr *dst;
557 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
558 struct ppp_header *ph;
559 int protocol, address, control;
566 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
567 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
568 error = ENETDOWN; /* sort of */
573 * Compute PPP header.
575 m0->m_flags &= ~M_HIGHPRI;
576 switch (dst->sa_family) {
579 address = PPP_ALLSTATIONS;
582 mode = sc->sc_npmode[NP_IP];
585 * If this packet has the "low delay" bit set in the IP header,
586 * put it on the fastq instead.
588 ip = mtod(m0, struct ip *);
589 if (ip->ip_tos & IPTOS_LOWDELAY)
590 m0->m_flags |= M_HIGHPRI;
594 address = PPP_ADDRESS(dst->sa_data);
595 control = PPP_CONTROL(dst->sa_data);
596 protocol = PPP_PROTOCOL(dst->sa_data);
600 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
601 error = EAFNOSUPPORT;
606 * Drop this packet, or return an error, if necessary.
608 if (mode == NPMODE_ERROR) {
612 if (mode == NPMODE_DROP) {
618 * Add PPP header. If no space in first mbuf, allocate another.
619 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
621 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
622 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
629 m0->m_data -= PPP_HDRLEN;
631 cp = mtod(m0, u_char *);
634 *cp++ = protocol >> 8;
635 *cp++ = protocol & 0xff;
636 m0->m_len += PPP_HDRLEN;
638 if (sc->sc_flags & SC_LOG_OUTPKT) {
639 printf("ppp%d output: ", ifp->if_unit);
645 * See if bpf wants to look at the packet.
648 bpf_mtap(sc->sc_bpf, m0);
652 * Put the packet on the appropriate queue.
654 s = splimp(); /* splnet should be OK now */
655 if (mode == NPMODE_QUEUE) {
656 /* XXX we should limit the number of packets on this queue */
657 *sc->sc_npqtail = m0;
658 m0->m_nextpkt = NULL;
659 sc->sc_npqtail = &m0->m_nextpkt;
661 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
665 sc->sc_if.if_oerrors++;
682 * After a change in the NPmode for some NP, move packets from the
683 * npqueue to the send queue or the fast queue as appropriate.
684 * Should be called at splimp (actually splnet would probably suffice).
688 struct ppp_softc *sc;
690 struct mbuf *m, **mpp;
694 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
695 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
697 mode = sc->sc_npmode[NP_IP];
706 * This packet can now go on one of the queues to be sent.
710 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
713 sc->sc_if.if_oerrors++;
729 sc->sc_npqtail = mpp;
733 * Get a packet to send. This procedure is intended to be called
734 * at spltty()/splimp(), so it takes little time. If there isn't
735 * a packet waiting to go out, it schedules a software interrupt
736 * to prepare a new packet; the device start routine gets called
737 * again when a packet is ready.
741 struct ppp_softc *sc;
749 * Had a packet waiting - send it.
752 sc->sc_flags |= SC_TBUSY;
757 * Remember we wanted a packet and schedule a software interrupt.
759 sc->sc_flags &= ~SC_TBUSY;
760 schednetisr(NETISR_PPP);
766 * Software interrupt routine, called at splnet().
771 struct ppp_softc *sc;
777 for (i = 0; i < NPPP; ++i, ++sc) {
778 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
779 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
782 IF_DEQUEUE(&sc->sc_rawq, m);
792 * Grab another packet off a queue and apply VJ compression,
793 * packet compression, address/control and/or protocol compression
794 * if enabled. Should be called at splnet.
798 struct ppp_softc *sc;
803 int address, control, protocol;
807 * Grab a packet to send: first try the fast queue, then the
810 IF_DEQUEUE(&sc->sc_fastq, m);
812 IF_DEQUEUE(&sc->sc_if.if_snd, m);
817 * Extract the ppp header of the new packet.
818 * The ppp header will be in one mbuf.
820 cp = mtod(m, u_char *);
821 address = PPP_ADDRESS(cp);
822 control = PPP_CONTROL(cp);
823 protocol = PPP_PROTOCOL(cp);
828 * Update the time we sent the most recent packet.
830 sc->sc_last_sent = time.tv_sec;
834 * If the packet is a TCP/IP packet, see if we can compress it.
836 if (sc->sc_flags & SC_COMP_TCP) {
841 ip = (struct ip *) (cp + PPP_HDRLEN);
842 if (mp->m_len <= PPP_HDRLEN) {
846 ip = mtod(mp, struct ip *);
848 /* this code assumes the IP/TCP header is in one non-shared mbuf */
849 if (ip->ip_p == IPPROTO_TCP) {
850 type = vj_compress_tcp(mp, ip, &sc->sc_comp,
851 !(sc->sc_flags & SC_NO_TCP_CCID));
853 case TYPE_UNCOMPRESSED_TCP:
854 protocol = PPP_VJC_UNCOMP;
856 case TYPE_COMPRESSED_TCP:
857 protocol = PPP_VJC_COMP;
858 cp = mtod(m, u_char *);
859 cp[0] = address; /* header has moved */
864 cp[3] = protocol; /* update protocol in PPP header */
874 #endif /* PPP_COMPRESS */
878 if (protocol != PPP_LCP && protocol != PPP_CCP
879 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
884 for (mp = m; mp != NULL; mp = mp->m_next)
886 clen = (*sc->sc_xcomp->compress)
887 (sc->sc_xc_state, &mcomp, m, slen,
888 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
892 cp = mtod(m, u_char *);
896 #endif /* PPP_COMPRESS */
899 * Compress the address/control and protocol, if possible.
901 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
902 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
903 protocol != PPP_LCP) {
904 /* can compress address/control */
908 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
909 /* can compress protocol */
910 if (mtod(m, u_char *) == cp) {
911 cp[2] = cp[1]; /* move address/control up */
926 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
927 * 0 if it is about to be transmitted.
931 struct ppp_softc *sc;
940 * Get a pointer to the data after the PPP header.
942 if (m->m_len <= PPP_HDRLEN) {
946 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
949 dp = mtod(mp, u_char *) + PPP_HDRLEN;
952 ep = mtod(mp, u_char *) + mp->m_len;
953 if (dp + CCP_HDRLEN > ep)
955 slen = CCP_LENGTH(dp);
956 if (dp + slen > ep) {
957 if (sc->sc_flags & SC_DEBUG)
958 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
959 dp, slen, mtod(mp, u_char *), mp->m_len);
963 switch (CCP_CODE(dp)) {
967 /* CCP must be going down - disable compression */
968 if (sc->sc_flags & SC_CCP_UP) {
970 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
976 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
977 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
978 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
980 /* we're agreeing to send compressed packets. */
981 if (sc->sc_xc_state != NULL
982 && (*sc->sc_xcomp->comp_init)
983 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
984 sc->sc_if.if_unit, sc->sc_flags & SC_DEBUG)) {
986 sc->sc_flags |= SC_COMP_RUN;
990 /* peer is agreeing to send compressed packets. */
991 if (sc->sc_rc_state != NULL
992 && (*sc->sc_rcomp->decomp_init)
993 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
994 sc->sc_if.if_unit, 0, sc->sc_mru,
995 sc->sc_flags & SC_DEBUG)) {
997 sc->sc_flags |= SC_DECOMP_RUN;
998 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1006 if (sc->sc_flags & SC_CCP_UP) {
1008 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1009 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1011 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1012 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1014 sc->sc_flags &= ~SC_DC_ERROR;
1024 * CCP is down; free (de)compressor state if necessary.
1028 struct ppp_softc *sc;
1030 if (sc->sc_xc_state) {
1031 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1032 sc->sc_xc_state = NULL;
1034 if (sc->sc_rc_state) {
1035 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1036 sc->sc_rc_state = NULL;
1039 #endif /* PPP_COMPRESS */
1042 * PPP packet input routine.
1043 * The caller has checked and removed the FCS and has inserted
1044 * the address/control bytes and the protocol high byte if they
1048 ppppktin(sc, m, lost)
1049 struct ppp_softc *sc;
1056 m->m_flags |= M_ERRMARK;
1057 IF_ENQUEUE(&sc->sc_rawq, m);
1058 schednetisr(NETISR_PPP);
1063 * Process a received PPP packet, doing decompression as necessary.
1065 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1066 TYPE_UNCOMPRESSED_TCP)
1070 struct ppp_softc *sc;
1073 struct ifqueue *inq;
1074 int s, ilen, xlen, proto, rv;
1075 u_char *cp, adrs, ctrl;
1076 struct mbuf *mp, *dmp;
1080 sc->sc_if.if_ipackets++;
1082 if (sc->sc_flags & SC_LOG_INPKT) {
1083 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1087 cp = mtod(m, u_char *);
1088 adrs = PPP_ADDRESS(cp);
1089 ctrl = PPP_CONTROL(cp);
1090 proto = PPP_PROTOCOL(cp);
1092 if (m->m_flags & M_ERRMARK) {
1093 m->m_flags &= ~M_ERRMARK;
1095 sc->sc_flags |= SC_VJ_RESET;
1101 * Decompress this packet if necessary, update the receiver's
1102 * dictionary, or take appropriate action on a CCP packet.
1104 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1105 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1106 /* decompress this packet */
1107 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1111 cp = mtod(m, u_char *);
1112 proto = PPP_PROTOCOL(cp);
1115 /* pass the compressed packet up to pppd, which may take
1116 CCP down or issue a Reset-Req. */
1117 if (sc->sc_flags & SC_DEBUG)
1118 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1120 sc->sc_flags |= SC_VJ_RESET;
1123 /* no error, but no decompressed packet produced */
1128 sc->sc_flags |= SC_DC_ERROR;
1130 case DECOMP_FATALERROR:
1131 sc->sc_flags |= SC_DC_FERROR;
1138 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1139 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1141 if (proto == PPP_CCP) {
1148 for (mp = m; mp != NULL; mp = mp->m_next)
1152 if (sc->sc_flags & SC_VJ_RESET) {
1154 * If we've missed a packet, we must toss subsequent compressed
1155 * packets which don't have an explicit connection ID.
1157 vj_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1159 sc->sc_flags &= ~SC_VJ_RESET;
1164 * See if we have a VJ-compressed packet to uncompress.
1166 if (proto == PPP_VJC_COMP) {
1167 if (sc->sc_flags & SC_REJ_COMP_TCP)
1170 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1171 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1172 &sc->sc_comp, &iphdr, &hlen);
1175 if (sc->sc_flags & SC_DEBUG)
1176 printf("ppp%d: VJ uncompress failed on type comp\n",
1181 /* Copy the PPP and IP headers into a new mbuf. */
1182 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1187 if (hlen + PPP_HDRLEN > MHLEN) {
1188 MCLGET(mp, M_DONTWAIT);
1189 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1191 goto bad; /* lose if big headers and no clusters */
1194 cp = mtod(mp, u_char *);
1200 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1201 mp->m_len = hlen + PPP_HDRLEN;
1204 * Trim the PPP and VJ headers off the old mbuf
1205 * and stick the new and old mbufs together.
1207 m->m_data += PPP_HDRLEN + xlen;
1208 m->m_len -= PPP_HDRLEN + xlen;
1209 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1210 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1211 mp->m_len += m->m_len;
1212 MFREE(m, mp->m_next);
1216 ilen += hlen - xlen;
1218 } else if (proto == PPP_VJC_UNCOMP) {
1219 if (sc->sc_flags & SC_REJ_COMP_TCP)
1222 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1223 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1224 &sc->sc_comp, &iphdr, &hlen);
1227 if (sc->sc_flags & SC_DEBUG)
1228 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1239 * If the packet will fit in a header mbuf, don't waste a
1240 * whole cluster on it.
1242 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1243 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1245 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1251 m->m_pkthdr.len = ilen;
1252 m->m_pkthdr.rcvif = &sc->sc_if;
1255 /* See if bpf wants to look at the packet. */
1257 bpf_mtap(sc->sc_bpf, m);
1265 * IP packet - take off the ppp header and pass it up to IP.
1267 if ((sc->sc_if.if_flags & IFF_UP) == 0
1268 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1269 /* interface is down - drop the packet. */
1273 m->m_pkthdr.len -= PPP_HDRLEN;
1274 m->m_data += PPP_HDRLEN;
1275 m->m_len -= PPP_HDRLEN;
1276 schednetisr(NETISR_IP);
1278 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1284 * Some other protocol - place on input queue for read().
1292 * Put the packet on the appropriate input queue.
1295 if (IF_QFULL(inq)) {
1298 if (sc->sc_flags & SC_DEBUG)
1299 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1300 sc->sc_if.if_iqdrops++;
1313 sc->sc_if.if_ierrors++;
1316 #define MAX_DUMP_BYTES 128
1322 char buf[3*MAX_DUMP_BYTES+4];
1325 static char digits[] = "0123456789abcdef";
1327 for (m = m0; m; m = m->m_next) {
1329 u_char *rptr = (u_char *)m->m_data;
1332 if (bp > buf + sizeof(buf) - 4)
1334 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1335 *bp++ = digits[*rptr++ & 0xf];
1339 if (bp > buf + sizeof(buf) - 3)
1349 printf("%s\n", buf);
1352 #endif /* NPPP > 0 */