1 /* $Id: if_ppp.c,v 1.1 1995/12/11 05:17:06 paulus Exp $ */
4 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6 * Copyright (c) 1989 Carnegie Mellon University.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by Carnegie Mellon University. The name of the
15 * University may not be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 * Carnegie Mellon University
24 * Pittsburgh, PA 15213
29 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
31 * Copyright (c) 1987 Regents of the University of California.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms are permitted
35 * provided that the above copyright notice and this paragraph are
36 * duplicated in all such forms and that any documentation,
37 * advertising materials, and other materials related to such
38 * distribution and use acknowledge that the software was developed
39 * by the University of California, Berkeley. The name of the
40 * University may not be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
44 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 * Serial Line interface
49 * Center for Seismic Studies
50 * 1300 N 17th Street, Suite 1450
51 * Arlington, Virginia 22209
56 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
57 * Converted to 4.3BSD Beta by Chris Torek.
58 * Other changes made at Berkeley, based in part on code by Kirk Smith.
60 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
61 * Added VJ tcp header compression; more unified ioctls
63 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
64 * Cleaned up a lot of the mbuf-related code to fix bugs that
65 * caused system crashes and packet corruption. Changed pppstart
66 * so that it doesn't just give up with a collision if the whole
67 * packet doesn't fit in the output ring buffer.
69 * Added priority queueing for interactive IP packets, following
70 * the model of if_sl.c, plus hooks for bpf.
71 * Paul Mackerras (paulus@cs.anu.edu.au).
74 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
75 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
83 #include <sys/param.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/kernel.h>
91 #include <machine/psl.h>
95 #include <net/if_types.h>
96 #include <net/netisr.h>
97 #include <net/route.h>
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/in_var.h>
103 #include <netinet/ip.h>
106 #include "bpfilter.h"
108 #include <sys/time.h>
113 #include <net/slcompress.h>
116 #include <net/ppp_defs.h>
117 #include <net/if_ppp.h>
118 #include <net/if_pppvar.h>
119 #include <machine/cpu.h>
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 */
179 * Called from boot code to establish ppp interfaces.
184 register struct ppp_softc *sc;
187 for (sc = ppp_softc; i < NPPP; sc++) {
188 sc->sc_if.if_name = "ppp";
189 sc->sc_if.if_unit = i++;
190 sc->sc_if.if_mtu = PPP_MTU;
191 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
192 sc->sc_if.if_type = IFT_PPP;
193 sc->sc_if.if_hdrlen = PPP_HDRLEN;
194 sc->sc_if.if_ioctl = pppsioctl;
195 sc->sc_if.if_output = pppoutput;
196 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
197 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
198 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
199 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
200 if_attach(&sc->sc_if);
202 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
209 * Allocate a ppp interface unit and initialize it.
216 struct ppp_softc *sc;
218 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
219 if (sc->sc_xfer == pid) {
223 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
224 if (sc->sc_devp == NULL)
230 sc->sc_mru = PPP_MRU;
231 sc->sc_relinq = NULL;
233 sl_compress_init(&sc->sc_comp, -1);
236 sc->sc_xc_state = NULL;
237 sc->sc_rc_state = NULL;
238 #endif /* PPP_COMPRESS */
239 for (i = 0; i < NUM_NP; ++i)
240 sc->sc_npmode[i] = NPMODE_ERROR;
241 sc->sc_npqueue = NULL;
242 sc->sc_npqtail = &sc->sc_npqueue;
243 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
249 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
253 struct ppp_softc *sc;
258 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
262 IF_DEQUEUE(&sc->sc_rawq, m);
268 IF_DEQUEUE(&sc->sc_inq, m);
274 IF_DEQUEUE(&sc->sc_fastq, m);
279 while ((m = sc->sc_npqueue) != NULL) {
280 sc->sc_npqueue = m->m_nextpkt;
283 if (sc->sc_togo != NULL) {
284 m_freem(sc->sc_togo);
289 sc->sc_xc_state = NULL;
290 sc->sc_rc_state = NULL;
291 #endif /* PPP_COMPRESS */
295 * Ioctl routine for generic ppp devices.
298 pppioctl(sc, cmd, data, flag, p)
299 struct ppp_softc *sc;
305 int s, error, flags, mru, nb, npx;
306 struct ppp_option_data *odp;
307 struct compressor **cp;
311 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
316 *(int *)data = sc->sc_inq.ifq_len;
320 *(int *)data = sc->sc_if.if_unit;
324 *(u_int *)data = sc->sc_flags;
328 if (error = suser(p->p_ucred, &p->p_acflag))
330 flags = *(int *)data & SC_MASK;
333 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
337 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
342 if (error = suser(p->p_ucred, &p->p_acflag))
345 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
350 *(int *)data = sc->sc_mru;
355 if (error = suser(p->p_ucred, &p->p_acflag))
358 sl_compress_init(&sc->sc_comp, *(int *)data);
364 if (error = suser(p->p_ucred, &p->p_acflag))
366 sc->sc_xfer = p->p_pid;
370 case PPPIOCSCOMPRESS:
371 if (error = suser(p->p_ucred, &p->p_acflag))
373 odp = (struct ppp_option_data *) data;
375 if (nb > sizeof(ccp_option))
376 nb = sizeof(ccp_option);
377 if (error = copyin(odp->ptr, ccp_option, nb))
379 if (ccp_option[1] < 2) /* preliminary check on the length byte */
381 for (cp = ppp_compressors; *cp != NULL; ++cp)
382 if ((*cp)->compress_proto == ccp_option[0]) {
384 * Found a handler for the protocol - try to allocate
385 * a compressor or decompressor.
390 if (sc->sc_xc_state != NULL)
391 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
393 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
394 if (sc->sc_xc_state == NULL) {
395 if (sc->sc_flags & SC_DEBUG)
396 printf("ppp%d: comp_alloc failed\n",
401 sc->sc_flags &= ~SC_COMP_RUN;
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;
516 switch(ifr->ifr_addr.sa_family) {
522 error = EAFNOSUPPORT;
528 psp = &((struct ifpppstatsreq *) data)->stats;
529 bzero(psp, sizeof(*psp));
530 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
531 psp->p.ppp_ipackets = ifp->if_ipackets;
532 psp->p.ppp_ierrors = ifp->if_ierrors;
533 psp->p.ppp_obytes = sc->sc_bytessent;
534 psp->p.ppp_opackets = ifp->if_opackets;
535 psp->p.ppp_oerrors = ifp->if_oerrors;
537 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
538 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
539 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
540 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
541 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
542 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
543 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
544 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
550 pcp = &((struct ifpppcstatsreq *) data)->stats;
551 bzero(pcp, sizeof(*pcp));
552 if (sc->sc_xc_state != NULL)
553 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
554 if (sc->sc_rc_state != NULL)
555 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
557 #endif /* PPP_COMPRESS */
567 * Queue a packet. Start transmission if not active.
568 * Packet is placed in Information field of PPP frame.
571 pppoutput(ifp, m0, dst, rtp)
574 struct sockaddr *dst;
577 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
578 struct ppp_header *ph;
579 int protocol, address, control;
586 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
587 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
588 error = ENETDOWN; /* sort of */
593 * Compute PPP header.
595 m0->m_flags &= ~M_HIGHPRI;
596 switch (dst->sa_family) {
599 address = PPP_ALLSTATIONS;
602 mode = sc->sc_npmode[NP_IP];
605 * If this packet has the "low delay" bit set in the IP header,
606 * put it on the fastq instead.
608 ip = mtod(m0, struct ip *);
609 if (ip->ip_tos & IPTOS_LOWDELAY)
610 m0->m_flags |= M_HIGHPRI;
614 address = PPP_ADDRESS(dst->sa_data);
615 control = PPP_CONTROL(dst->sa_data);
616 protocol = PPP_PROTOCOL(dst->sa_data);
620 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
621 error = EAFNOSUPPORT;
626 * Drop this packet, or return an error, if necessary.
628 if (mode == NPMODE_ERROR) {
632 if (mode == NPMODE_DROP) {
638 * Add PPP header. If no space in first mbuf, allocate another.
639 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
641 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
642 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
649 m0->m_data -= PPP_HDRLEN;
651 cp = mtod(m0, u_char *);
654 *cp++ = protocol >> 8;
655 *cp++ = protocol & 0xff;
656 m0->m_len += PPP_HDRLEN;
658 if (sc->sc_flags & SC_LOG_OUTPKT) {
659 printf("ppp%d output: ", ifp->if_unit);
665 * See if bpf wants to look at the packet.
668 bpf_mtap(sc->sc_bpf, m0);
672 * Put the packet on the appropriate queue.
675 if (mode == NPMODE_QUEUE) {
676 /* XXX we should limit the number of packets on this queue */
677 *sc->sc_npqtail = m0;
678 m0->m_nextpkt = NULL;
679 sc->sc_npqtail = &m0->m_nextpkt;
681 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
682 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
685 sc->sc_if.if_oerrors++;
692 ifp->if_lastchange = time;
703 * After a change in the NPmode for some NP, move packets from the
704 * npqueue to the send queue or the fast queue as appropriate.
705 * Should be called at splsoftnet.
709 struct ppp_softc *sc;
711 struct mbuf *m, **mpp;
715 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
716 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
718 mode = sc->sc_npmode[NP_IP];
727 * This packet can now go on one of the queues to be sent.
731 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
734 sc->sc_if.if_oerrors++;
750 sc->sc_npqtail = mpp;
754 * Get a packet to send. This procedure is intended to be called at
755 * spltty or splimp, so it takes little time. If there isn't a packet
756 * waiting to go out, it schedules a software interrupt to prepare a
757 * new packet; the device start routine gets called again when a
762 struct ppp_softc *sc;
770 * Had a packet waiting - send it.
773 sc->sc_flags |= SC_TBUSY;
778 * Remember we wanted a packet and schedule a software interrupt.
780 sc->sc_flags &= ~SC_TBUSY;
781 schednetisr(NETISR_PPP);
787 * Software interrupt routine, called at splsoftnet.
792 struct ppp_softc *sc;
797 for (i = 0; i < NPPP; ++i, ++sc) {
798 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
799 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
803 IF_DEQUEUE(&sc->sc_rawq, m);
813 * Grab another packet off a queue and apply VJ compression,
814 * packet compression, address/control and/or protocol compression
815 * if enabled. Should be called at splsoftnet.
819 struct ppp_softc *sc;
824 int address, control, protocol;
828 * Grab a packet to send: first try the fast queue, then the
831 IF_DEQUEUE(&sc->sc_fastq, m);
833 IF_DEQUEUE(&sc->sc_if.if_snd, m);
838 * Extract the ppp header of the new packet.
839 * The ppp header will be in one mbuf.
841 cp = mtod(m, u_char *);
842 address = PPP_ADDRESS(cp);
843 control = PPP_CONTROL(cp);
844 protocol = PPP_PROTOCOL(cp);
849 * Update the time we sent the most recent packet.
851 sc->sc_last_sent = time.tv_sec;
855 * If the packet is a TCP/IP packet, see if we can compress it.
857 if (sc->sc_flags & SC_COMP_TCP) {
862 ip = (struct ip *) (cp + PPP_HDRLEN);
863 if (mp->m_len <= PPP_HDRLEN) {
867 ip = mtod(mp, struct ip *);
869 /* this code assumes the IP/TCP header is in one non-shared mbuf */
870 if (ip->ip_p == IPPROTO_TCP) {
871 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
872 !(sc->sc_flags & SC_NO_TCP_CCID));
874 case TYPE_UNCOMPRESSED_TCP:
875 protocol = PPP_VJC_UNCOMP;
877 case TYPE_COMPRESSED_TCP:
878 protocol = PPP_VJC_COMP;
879 cp = mtod(m, u_char *);
880 cp[0] = address; /* header has moved */
885 cp[3] = protocol; /* update protocol in PPP header */
895 #endif /* PPP_COMPRESS */
899 if (protocol != PPP_LCP && protocol != PPP_CCP
900 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
901 struct mbuf *mcomp = NULL;
905 for (mp = m; mp != NULL; mp = mp->m_next)
907 clen = (*sc->sc_xcomp->compress)
908 (sc->sc_xc_state, &mcomp, m, slen,
909 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
913 cp = mtod(m, u_char *);
917 #endif /* PPP_COMPRESS */
920 * Compress the address/control and protocol, if possible.
922 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
923 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
924 protocol != PPP_LCP) {
925 /* can compress address/control */
929 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
930 /* can compress protocol */
931 if (mtod(m, u_char *) == cp) {
932 cp[2] = cp[1]; /* move address/control up */
945 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
946 * 0 if it is about to be transmitted.
950 struct ppp_softc *sc;
959 * Get a pointer to the data after the PPP header.
961 if (m->m_len <= PPP_HDRLEN) {
965 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
968 dp = mtod(mp, u_char *) + PPP_HDRLEN;
971 ep = mtod(mp, u_char *) + mp->m_len;
972 if (dp + CCP_HDRLEN > ep)
974 slen = CCP_LENGTH(dp);
975 if (dp + slen > ep) {
976 if (sc->sc_flags & SC_DEBUG)
977 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
978 dp, slen, mtod(mp, u_char *), mp->m_len);
982 switch (CCP_CODE(dp)) {
986 /* CCP must be going down - disable compression */
987 if (sc->sc_flags & SC_CCP_UP) {
989 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
995 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
996 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
997 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
999 /* we're agreeing to send compressed packets. */
1000 if (sc->sc_xc_state != NULL
1001 && (*sc->sc_xcomp->comp_init)
1002 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1003 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1005 sc->sc_flags |= SC_COMP_RUN;
1009 /* peer is agreeing to send compressed packets. */
1010 if (sc->sc_rc_state != NULL
1011 && (*sc->sc_rcomp->decomp_init)
1012 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1013 sc->sc_if.if_unit, 0, sc->sc_mru,
1014 sc->sc_flags & SC_DEBUG)) {
1016 sc->sc_flags |= SC_DECOMP_RUN;
1017 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1025 if (sc->sc_flags & SC_CCP_UP) {
1027 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1028 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1030 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1031 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1033 sc->sc_flags &= ~SC_DC_ERROR;
1043 * CCP is down; free (de)compressor state if necessary.
1047 struct ppp_softc *sc;
1049 if (sc->sc_xc_state) {
1050 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1051 sc->sc_xc_state = NULL;
1053 if (sc->sc_rc_state) {
1054 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1055 sc->sc_rc_state = NULL;
1058 #endif /* PPP_COMPRESS */
1061 * PPP packet input routine.
1062 * The caller has checked and removed the FCS and has inserted
1063 * the address/control bytes and the protocol high byte if they
1067 ppppktin(sc, m, lost)
1068 struct ppp_softc *sc;
1075 m->m_flags |= M_ERRMARK;
1076 IF_ENQUEUE(&sc->sc_rawq, m);
1077 schednetisr(NETISR_PPP);
1082 * Process a received PPP packet, doing decompression as necessary.
1083 * Should be called at splsoftnet.
1085 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1086 TYPE_UNCOMPRESSED_TCP)
1090 struct ppp_softc *sc;
1093 struct ifnet *ifp = &sc->sc_if;
1094 struct ifqueue *inq;
1095 int s, ilen, xlen, proto, rv;
1096 u_char *cp, adrs, ctrl;
1097 struct mbuf *mp, *dmp = NULL;
1102 ifp->if_lastchange = time;
1104 if (sc->sc_flags & SC_LOG_INPKT) {
1106 for (mp = m; mp != NULL; mp = mp->m_next)
1108 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1112 cp = mtod(m, u_char *);
1113 adrs = PPP_ADDRESS(cp);
1114 ctrl = PPP_CONTROL(cp);
1115 proto = PPP_PROTOCOL(cp);
1117 if (m->m_flags & M_ERRMARK) {
1118 m->m_flags &= ~M_ERRMARK;
1120 sc->sc_flags |= SC_VJ_RESET;
1126 * Decompress this packet if necessary, update the receiver's
1127 * dictionary, or take appropriate action on a CCP packet.
1129 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1130 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1131 /* decompress this packet */
1132 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1133 if (rv == DECOMP_OK) {
1136 /* no error, but no decompressed packet produced */
1140 cp = mtod(m, u_char *);
1141 proto = PPP_PROTOCOL(cp);
1145 * An error has occurred in decompression.
1146 * Pass the compressed packet up to pppd, which may take
1147 * CCP down or issue a Reset-Req.
1149 if (sc->sc_flags & SC_DEBUG)
1150 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1152 sc->sc_flags |= SC_VJ_RESET;
1153 if (rv == DECOMP_ERROR)
1154 sc->sc_flags |= SC_DC_ERROR;
1156 sc->sc_flags |= SC_DC_FERROR;
1161 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1162 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1164 if (proto == PPP_CCP) {
1171 for (mp = m; mp != NULL; mp = mp->m_next)
1175 if (sc->sc_flags & SC_VJ_RESET) {
1177 * If we've missed a packet, we must toss subsequent compressed
1178 * packets which don't have an explicit connection ID.
1180 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1182 sc->sc_flags &= ~SC_VJ_RESET;
1187 * See if we have a VJ-compressed packet to uncompress.
1189 if (proto == PPP_VJC_COMP) {
1190 if (sc->sc_flags & SC_REJ_COMP_TCP)
1193 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1194 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1195 &sc->sc_comp, &iphdr, &hlen);
1198 if (sc->sc_flags & SC_DEBUG)
1199 printf("ppp%d: VJ uncompress failed on type comp\n",
1204 /* Copy the PPP and IP headers into a new mbuf. */
1205 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1210 if (hlen + PPP_HDRLEN > MHLEN) {
1211 MCLGET(mp, M_DONTWAIT);
1212 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1214 goto bad; /* lose if big headers and no clusters */
1217 cp = mtod(mp, u_char *);
1223 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1224 mp->m_len = hlen + PPP_HDRLEN;
1227 * Trim the PPP and VJ headers off the old mbuf
1228 * and stick the new and old mbufs together.
1230 m->m_data += PPP_HDRLEN + xlen;
1231 m->m_len -= PPP_HDRLEN + xlen;
1232 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1233 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1234 mp->m_len += m->m_len;
1235 MFREE(m, mp->m_next);
1239 ilen += hlen - xlen;
1241 } else if (proto == PPP_VJC_UNCOMP) {
1242 if (sc->sc_flags & SC_REJ_COMP_TCP)
1245 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1246 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1247 &sc->sc_comp, &iphdr, &hlen);
1250 if (sc->sc_flags & SC_DEBUG)
1251 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1262 * If the packet will fit in a header mbuf, don't waste a
1263 * whole cluster on it.
1265 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1266 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1268 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1274 m->m_pkthdr.len = ilen;
1275 m->m_pkthdr.rcvif = ifp;
1278 /* See if bpf wants to look at the packet. */
1280 bpf_mtap(sc->sc_bpf, m);
1288 * IP packet - take off the ppp header and pass it up to IP.
1290 if ((ifp->if_flags & IFF_UP) == 0
1291 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1292 /* interface is down - drop the packet. */
1296 m->m_pkthdr.len -= PPP_HDRLEN;
1297 m->m_data += PPP_HDRLEN;
1298 m->m_len -= PPP_HDRLEN;
1299 schednetisr(NETISR_IP);
1301 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1307 * Some other protocol - place on input queue for read().
1315 * Put the packet on the appropriate input queue.
1318 if (IF_QFULL(inq)) {
1321 if (sc->sc_flags & SC_DEBUG)
1322 printf("ppp%d: input queue full\n", ifp->if_unit);
1336 sc->sc_if.if_ierrors++;
1339 #define MAX_DUMP_BYTES 128
1345 char buf[3*MAX_DUMP_BYTES+4];
1348 static char digits[] = "0123456789abcdef";
1350 for (m = m0; m; m = m->m_next) {
1352 u_char *rptr = (u_char *)m->m_data;
1355 if (bp > buf + sizeof(buf) - 4)
1357 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1358 *bp++ = digits[*rptr++ & 0xf];
1362 if (bp > buf + sizeof(buf) - 3)
1372 printf("%s\n", buf);
1375 #endif /* NPPP > 0 */