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.6 1995/10/27 03:34:42 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, u_long 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, u_long 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 | IFF_MULTICAST;
195 sc->sc_if.if_type = IFT_PPP;
196 sc->sc_if.if_hdrlen = PPP_HDRLEN;
197 sc->sc_if.if_ioctl = pppsioctl;
198 sc->sc_if.if_output = pppoutput;
199 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
200 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
201 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
202 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
203 if_attach(&sc->sc_if);
205 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
208 netisrs[NETISR_PPP] = pppintr;
212 * Allocate a ppp interface unit and initialize it.
219 struct ppp_softc *sc;
221 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
222 if (sc->sc_xfer == pid) {
226 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
227 if (sc->sc_devp == NULL)
233 sc->sc_mru = PPP_MRU;
234 sc->sc_relinq = NULL;
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;
308 int s, error, flags, mru, nb, npx;
309 struct ppp_option_data *odp;
310 struct compressor **cp;
314 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
319 *(int *)data = sc->sc_inq.ifq_len;
323 *(int *)data = sc->sc_if.if_unit;
327 *(u_int *)data = sc->sc_flags;
331 if (error = suser(p->p_ucred, &p->p_acflag))
333 flags = *(int *)data & SC_MASK;
336 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
340 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
345 if (error = suser(p->p_ucred, &p->p_acflag))
348 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
353 *(int *)data = sc->sc_mru;
358 if (error = suser(p->p_ucred, &p->p_acflag))
361 vj_compress_init(&sc->sc_comp, *(int *)data);
367 if (error = suser(p->p_ucred, &p->p_acflag))
369 sc->sc_xfer = p->p_pid;
373 case PPPIOCSCOMPRESS:
374 if (error = suser(p->p_ucred, &p->p_acflag))
376 odp = (struct ppp_option_data *) data;
378 if (nb > sizeof(ccp_option))
379 nb = sizeof(ccp_option);
380 if (error = copyin(odp->ptr, ccp_option, nb))
382 if (ccp_option[1] < 2) /* preliminary check on the length byte */
384 for (cp = ppp_compressors; *cp != NULL; ++cp)
385 if ((*cp)->compress_proto == ccp_option[0]) {
387 * Found a handler for the protocol - try to allocate
388 * a compressor or decompressor.
393 if (sc->sc_xc_state != NULL)
394 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
396 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
397 if (sc->sc_xc_state == NULL) {
398 if (sc->sc_flags & SC_DEBUG)
399 printf("ppp%d: comp_alloc failed\n",
404 sc->sc_flags &= ~SC_COMP_RUN;
408 if (sc->sc_rc_state != NULL)
409 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
411 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
412 if (sc->sc_rc_state == NULL) {
413 if (sc->sc_flags & SC_DEBUG)
414 printf("ppp%d: decomp_alloc failed\n",
419 sc->sc_flags &= ~SC_DECOMP_RUN;
424 if (sc->sc_flags & SC_DEBUG)
425 printf("ppp%d: no compressor for [%x %x %x], %x\n",
426 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
428 return (EINVAL); /* no handler found */
429 #endif /* PPP_COMPRESS */
433 npi = (struct npioctl *) data;
434 switch (npi->protocol) {
441 if (cmd == PPPIOCGNPMODE) {
442 npi->mode = sc->sc_npmode[npx];
444 if (error = suser(p->p_ucred, &p->p_acflag))
446 if (npi->mode != sc->sc_npmode[npx]) {
448 sc->sc_npmode[npx] = npi->mode;
449 if (npi->mode != NPMODE_QUEUE) {
461 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
462 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
473 * Process an ioctl request to the ppp network interface.
476 pppsioctl(ifp, cmd, data)
477 register struct ifnet *ifp;
481 struct proc *p = curproc; /* XXX */
482 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
483 register struct ifaddr *ifa = (struct ifaddr *)data;
484 register struct ifreq *ifr = (struct ifreq *)data;
485 struct ppp_stats *psp;
487 struct ppp_comp_stats *pcp;
489 int s = splimp(), error = 0;
493 if ((ifp->if_flags & IFF_RUNNING) == 0)
494 ifp->if_flags &= ~IFF_UP;
498 if (ifa->ifa_addr->sa_family != AF_INET)
499 error = EAFNOSUPPORT;
503 if (ifa->ifa_addr->sa_family != AF_INET)
504 error = EAFNOSUPPORT;
508 if (error = suser(p->p_ucred, &p->p_acflag))
510 sc->sc_if.if_mtu = ifr->ifr_mtu;
514 ifr->ifr_mtu = sc->sc_if.if_mtu;
519 switch(ifr->ifr_addr.sa_family) {
525 error = EAFNOSUPPORT;
531 psp = &((struct ifpppstatsreq *) data)->stats;
532 bzero(psp, sizeof(*psp));
533 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
534 psp->p.ppp_ipackets = ifp->if_ipackets;
535 psp->p.ppp_ierrors = ifp->if_ierrors;
536 psp->p.ppp_obytes = sc->sc_bytessent;
537 psp->p.ppp_opackets = ifp->if_opackets;
538 psp->p.ppp_oerrors = ifp->if_oerrors;
540 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
541 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
542 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
543 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
544 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
545 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
546 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
547 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
553 pcp = &((struct ifpppcstatsreq *) data)->stats;
554 bzero(pcp, sizeof(*pcp));
555 if (sc->sc_xc_state != NULL)
556 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
557 if (sc->sc_rc_state != NULL)
558 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
560 #endif /* PPP_COMPRESS */
570 * Queue a packet. Start transmission if not active.
571 * Packet is placed in Information field of PPP frame.
574 pppoutput(ifp, m0, dst, rtp)
577 struct sockaddr *dst;
580 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
581 struct ppp_header *ph;
582 int protocol, address, control;
589 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
590 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
591 error = ENETDOWN; /* sort of */
596 * Compute PPP header.
598 m0->m_flags &= ~M_HIGHPRI;
599 switch (dst->sa_family) {
602 address = PPP_ALLSTATIONS;
605 mode = sc->sc_npmode[NP_IP];
608 * If this packet has the "low delay" bit set in the IP header,
609 * put it on the fastq instead.
611 ip = mtod(m0, struct ip *);
612 if (ip->ip_tos & IPTOS_LOWDELAY)
613 m0->m_flags |= M_HIGHPRI;
617 address = PPP_ADDRESS(dst->sa_data);
618 control = PPP_CONTROL(dst->sa_data);
619 protocol = PPP_PROTOCOL(dst->sa_data);
623 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
624 error = EAFNOSUPPORT;
629 * Drop this packet, or return an error, if necessary.
631 if (mode == NPMODE_ERROR) {
635 if (mode == NPMODE_DROP) {
641 * Add PPP header. If no space in first mbuf, allocate another.
642 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
644 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
645 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
652 m0->m_data -= PPP_HDRLEN;
654 cp = mtod(m0, u_char *);
657 *cp++ = protocol >> 8;
658 *cp++ = protocol & 0xff;
659 m0->m_len += PPP_HDRLEN;
661 if (sc->sc_flags & SC_LOG_OUTPKT) {
662 printf("ppp%d output: ", ifp->if_unit);
668 * See if bpf wants to look at the packet.
671 bpf_mtap(sc->sc_bpf, m0);
675 * Put the packet on the appropriate queue.
678 if (mode == NPMODE_QUEUE) {
679 /* XXX we should limit the number of packets on this queue */
680 *sc->sc_npqtail = m0;
681 m0->m_nextpkt = NULL;
682 sc->sc_npqtail = &m0->m_nextpkt;
684 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
685 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
688 sc->sc_if.if_oerrors++;
705 * After a change in the NPmode for some NP, move packets from the
706 * npqueue to the send queue or the fast queue as appropriate.
707 * Should be called at splnet.
711 struct ppp_softc *sc;
713 struct mbuf *m, **mpp;
717 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
718 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
720 mode = sc->sc_npmode[NP_IP];
729 * This packet can now go on one of the queues to be sent.
733 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
736 sc->sc_if.if_oerrors++;
752 sc->sc_npqtail = mpp;
756 * Get a packet to send. This procedure is intended to be called at
757 * spltty or splimp, so it takes little time. If there isn't a packet
758 * waiting to go out, it schedules a software interrupt to prepare a
759 * new packet; the device start routine gets called again when a
764 struct ppp_softc *sc;
772 * Had a packet waiting - send it.
775 sc->sc_flags |= SC_TBUSY;
780 * Remember we wanted a packet and schedule a software interrupt.
782 sc->sc_flags &= ~SC_TBUSY;
783 schednetisr(NETISR_PPP);
789 * Software interrupt routine, called at splnet().
794 struct ppp_softc *sc;
800 for (i = 0; i < NPPP; ++i, ++sc) {
801 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
802 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
806 IF_DEQUEUE(&sc->sc_rawq, m);
817 * Grab another packet off a queue and apply VJ compression,
818 * packet compression, address/control and/or protocol compression
819 * if enabled. Should be called at splnet.
823 struct ppp_softc *sc;
828 int address, control, protocol;
832 * Grab a packet to send: first try the fast queue, then the
835 IF_DEQUEUE(&sc->sc_fastq, m);
837 IF_DEQUEUE(&sc->sc_if.if_snd, m);
842 * Extract the ppp header of the new packet.
843 * The ppp header will be in one mbuf.
845 cp = mtod(m, u_char *);
846 address = PPP_ADDRESS(cp);
847 control = PPP_CONTROL(cp);
848 protocol = PPP_PROTOCOL(cp);
853 * Update the time we sent the most recent packet.
855 sc->sc_last_sent = time.tv_sec;
859 * If the packet is a TCP/IP packet, see if we can compress it.
861 if (sc->sc_flags & SC_COMP_TCP) {
866 ip = (struct ip *) (cp + PPP_HDRLEN);
867 if (mp->m_len <= PPP_HDRLEN) {
871 ip = mtod(mp, struct ip *);
873 /* this code assumes the IP/TCP header is in one non-shared mbuf */
874 if (ip->ip_p == IPPROTO_TCP) {
875 type = vj_compress_tcp(mp, ip, &sc->sc_comp,
876 !(sc->sc_flags & SC_NO_TCP_CCID));
878 case TYPE_UNCOMPRESSED_TCP:
879 protocol = PPP_VJC_UNCOMP;
881 case TYPE_COMPRESSED_TCP:
882 protocol = PPP_VJC_COMP;
883 cp = mtod(m, u_char *);
884 cp[0] = address; /* header has moved */
889 cp[3] = protocol; /* update protocol in PPP header */
899 #endif /* PPP_COMPRESS */
903 if (protocol != PPP_LCP && protocol != PPP_CCP
904 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
905 struct mbuf *mcomp = NULL;
909 for (mp = m; mp != NULL; mp = mp->m_next)
911 clen = (*sc->sc_xcomp->compress)
912 (sc->sc_xc_state, &mcomp, m, slen,
913 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
917 cp = mtod(m, u_char *);
921 #endif /* PPP_COMPRESS */
924 * Compress the address/control and protocol, if possible.
926 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
927 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
928 protocol != PPP_LCP) {
929 /* can compress address/control */
933 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
934 /* can compress protocol */
935 if (mtod(m, u_char *) == cp) {
936 cp[2] = cp[1]; /* move address/control up */
949 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
950 * 0 if it is about to be transmitted.
954 struct ppp_softc *sc;
963 * Get a pointer to the data after the PPP header.
965 if (m->m_len <= PPP_HDRLEN) {
969 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
972 dp = mtod(mp, u_char *) + PPP_HDRLEN;
975 ep = mtod(mp, u_char *) + mp->m_len;
976 if (dp + CCP_HDRLEN > ep)
978 slen = CCP_LENGTH(dp);
979 if (dp + slen > ep) {
980 if (sc->sc_flags & SC_DEBUG)
981 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
982 dp, slen, mtod(mp, u_char *), mp->m_len);
986 switch (CCP_CODE(dp)) {
990 /* CCP must be going down - disable compression */
991 if (sc->sc_flags & SC_CCP_UP) {
993 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
999 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1000 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1001 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1003 /* we're agreeing to send compressed packets. */
1004 if (sc->sc_xc_state != NULL
1005 && (*sc->sc_xcomp->comp_init)
1006 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1007 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1009 sc->sc_flags |= SC_COMP_RUN;
1013 /* peer is agreeing to send compressed packets. */
1014 if (sc->sc_rc_state != NULL
1015 && (*sc->sc_rcomp->decomp_init)
1016 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1017 sc->sc_if.if_unit, 0, sc->sc_mru,
1018 sc->sc_flags & SC_DEBUG)) {
1020 sc->sc_flags |= SC_DECOMP_RUN;
1021 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1029 if (sc->sc_flags & SC_CCP_UP) {
1031 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1032 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1034 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1035 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1037 sc->sc_flags &= ~SC_DC_ERROR;
1047 * CCP is down; free (de)compressor state if necessary.
1051 struct ppp_softc *sc;
1053 if (sc->sc_xc_state) {
1054 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1055 sc->sc_xc_state = NULL;
1057 if (sc->sc_rc_state) {
1058 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1059 sc->sc_rc_state = NULL;
1062 #endif /* PPP_COMPRESS */
1065 * PPP packet input routine.
1066 * The caller has checked and removed the FCS and has inserted
1067 * the address/control bytes and the protocol high byte if they
1071 ppppktin(sc, m, lost)
1072 struct ppp_softc *sc;
1079 m->m_flags |= M_ERRMARK;
1080 IF_ENQUEUE(&sc->sc_rawq, m);
1081 schednetisr(NETISR_PPP);
1086 * Process a received PPP packet, doing decompression as necessary.
1087 * Should be called at splnet.
1089 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1090 TYPE_UNCOMPRESSED_TCP)
1094 struct ppp_softc *sc;
1097 struct ifqueue *inq;
1098 int s, ilen, xlen, proto, rv;
1099 u_char *cp, adrs, ctrl;
1100 struct mbuf *mp, *dmp = NULL;
1104 sc->sc_if.if_ipackets++;
1106 if (sc->sc_flags & SC_LOG_INPKT) {
1108 for (mp = m; mp != NULL; mp = mp->m_next)
1110 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1114 cp = mtod(m, u_char *);
1115 adrs = PPP_ADDRESS(cp);
1116 ctrl = PPP_CONTROL(cp);
1117 proto = PPP_PROTOCOL(cp);
1119 if (m->m_flags & M_ERRMARK) {
1120 m->m_flags &= ~M_ERRMARK;
1122 sc->sc_flags |= SC_VJ_RESET;
1128 * Decompress this packet if necessary, update the receiver's
1129 * dictionary, or take appropriate action on a CCP packet.
1131 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1132 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1133 /* decompress this packet */
1134 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1135 if (rv == DECOMP_OK) {
1138 /* no error, but no decompressed packet produced */
1142 cp = mtod(m, u_char *);
1143 proto = PPP_PROTOCOL(cp);
1147 * An error has occurred in decompression.
1148 * Pass the compressed packet up to pppd, which may take
1149 * CCP down or issue a Reset-Req.
1151 if (sc->sc_flags & SC_DEBUG)
1152 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1154 sc->sc_flags |= SC_VJ_RESET;
1155 if (rv == DECOMP_ERROR)
1156 sc->sc_flags |= SC_DC_ERROR;
1158 sc->sc_flags |= SC_DC_FERROR;
1163 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1164 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1166 if (proto == PPP_CCP) {
1173 for (mp = m; mp != NULL; mp = mp->m_next)
1177 if (sc->sc_flags & SC_VJ_RESET) {
1179 * If we've missed a packet, we must toss subsequent compressed
1180 * packets which don't have an explicit connection ID.
1182 vj_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1184 sc->sc_flags &= ~SC_VJ_RESET;
1189 * See if we have a VJ-compressed packet to uncompress.
1191 if (proto == PPP_VJC_COMP) {
1192 if (sc->sc_flags & SC_REJ_COMP_TCP)
1195 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1196 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1197 &sc->sc_comp, &iphdr, &hlen);
1200 if (sc->sc_flags & SC_DEBUG)
1201 printf("ppp%d: VJ uncompress failed on type comp\n",
1206 /* Copy the PPP and IP headers into a new mbuf. */
1207 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1212 if (hlen + PPP_HDRLEN > MHLEN) {
1213 MCLGET(mp, M_DONTWAIT);
1214 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1216 goto bad; /* lose if big headers and no clusters */
1219 cp = mtod(mp, u_char *);
1225 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1226 mp->m_len = hlen + PPP_HDRLEN;
1229 * Trim the PPP and VJ headers off the old mbuf
1230 * and stick the new and old mbufs together.
1232 m->m_data += PPP_HDRLEN + xlen;
1233 m->m_len -= PPP_HDRLEN + xlen;
1234 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1235 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1236 mp->m_len += m->m_len;
1237 MFREE(m, mp->m_next);
1241 ilen += hlen - xlen;
1243 } else if (proto == PPP_VJC_UNCOMP) {
1244 if (sc->sc_flags & SC_REJ_COMP_TCP)
1247 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1248 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1249 &sc->sc_comp, &iphdr, &hlen);
1252 if (sc->sc_flags & SC_DEBUG)
1253 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1264 * If the packet will fit in a header mbuf, don't waste a
1265 * whole cluster on it.
1267 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1268 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1270 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1276 m->m_pkthdr.len = ilen;
1277 m->m_pkthdr.rcvif = &sc->sc_if;
1280 /* See if bpf wants to look at the packet. */
1282 bpf_mtap(sc->sc_bpf, m);
1290 * IP packet - take off the ppp header and pass it up to IP.
1292 if ((sc->sc_if.if_flags & IFF_UP) == 0
1293 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1294 /* interface is down - drop the packet. */
1298 m->m_pkthdr.len -= PPP_HDRLEN;
1299 m->m_data += PPP_HDRLEN;
1300 m->m_len -= PPP_HDRLEN;
1301 schednetisr(NETISR_IP);
1303 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1309 * Some other protocol - place on input queue for read().
1317 * Put the packet on the appropriate input queue.
1320 if (IF_QFULL(inq)) {
1323 if (sc->sc_flags & SC_DEBUG)
1324 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1325 sc->sc_if.if_iqdrops++;
1338 sc->sc_if.if_ierrors++;
1341 #define MAX_DUMP_BYTES 128
1347 char buf[3*MAX_DUMP_BYTES+4];
1350 static char digits[] = "0123456789abcdef";
1352 for (m = m0; m; m = m->m_next) {
1354 u_char *rptr = (u_char *)m->m_data;
1357 if (bp > buf + sizeof(buf) - 4)
1359 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1360 *bp++ = digits[*rptr++ & 0xf];
1364 if (bp > buf + sizeof(buf) - 3)
1374 printf("%s\n", buf);
1377 #endif /* NPPP > 0 */