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).
71 * Ultrix port by Per Sundstrom <sundstrom@stkhlm.enet.dec.com>,
72 * Robert Olsson <robert@robur.slu.se> and Paul Mackerras.
75 /* $Id: if_ppp.c,v 1.8 1995/07/11 07:00:49 paulus Exp $ */
76 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
84 #include "../h/param.h"
85 #include "../h/user.h"
86 #include "../h/proc.h"
87 #include "../h/mbuf.h"
89 #include "../h/socket.h"
90 #include "../h/ioctl.h"
91 #include "../h/systm.h"
93 #include "../net/net/if.h"
94 #include "../net/net/netisr.h"
95 #include "../net/net/route.h"
98 #include "../net/netinet/in.h"
99 #include "../net/netinet/in_systm.h"
100 #include "../net/netinet/in_var.h"
101 #include "../net/netinet/ip.h"
105 #include "../machine/mtpr.h"
108 #include "ppp_defs.h"
112 #include "slcompress.h"
115 #include "if_pppvar.h"
118 #define PACKETPTR struct mbuf *
119 #include "ppp-comp.h"
122 void pppattach __P((void));
123 int pppioctl __P((struct ppp_softc *sc, int cmd, caddr_t data, int flag,
125 int pppoutput __P((struct ifnet *ifp, struct mbuf *m0,
126 struct sockaddr *dst));
127 int pppsioctl __P((struct ifnet *ifp, int cmd, caddr_t data));
128 void pppintr __P((void));
130 static void ppp_requeue __P((struct ppp_softc *));
131 static void ppp_outpkt __P((struct ppp_softc *));
132 static int ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
133 static void ppp_ccp_closed __P((struct ppp_softc *));
134 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
135 static void pppdumpm __P((struct mbuf *m0));
138 * Some useful mbuf macros not in mbuf.h.
140 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
142 #define M_TRAILINGSPACE(m) \
143 ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
144 - ((m)->m_off + (m)->m_len))
146 #define M_OFFSTART(m) \
147 (M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr : MMINOFF)
149 #define M_DATASIZE(m) \
150 (M_IS_CLUSTER(m) ? M_CLUSTERSZ : MLEN)
153 * The following disgusting hack gets around the problem that IP TOS
154 * can't be set yet. We want to put "interactive" traffic on a high
155 * priority queue. To decide if traffic is interactive, we check that
156 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
158 static u_short interactive_ports[8] = {
162 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
166 * List of compressors we know about.
169 extern struct compressor ppp_bsd_compress;
171 struct compressor *ppp_compressors[] = {
177 #endif /* PPP_COMPRESS */
180 * Called from boot code to establish ppp interfaces.
185 register struct ppp_softc *sc;
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;
193 sc->sc_if.if_type = IFT_PPP;
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);
208 * Allocate a ppp interface unit and initialize it.
215 struct ppp_softc *sc;
217 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
218 if (sc->sc_xfer == pid) {
222 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
223 if (sc->sc_devp == NULL)
229 sc->sc_mru = PPP_MRU;
230 sc->sc_relinq = NULL;
232 sl_compress_init(&sc->sc_comp);
235 sc->sc_xc_state = NULL;
236 sc->sc_rc_state = NULL;
237 #endif /* PPP_COMPRESS */
238 for (i = 0; i < NUM_NP; ++i)
239 sc->sc_npmode[i] = NPMODE_ERROR;
240 sc->sc_npqueue = NULL;
241 sc->sc_npqtail = &sc->sc_npqueue;
247 * Deallocate a ppp unit. Must be called at splnet or higher.
251 struct ppp_softc *sc;
256 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
260 IF_DEQUEUE(&sc->sc_rawq, m);
266 IF_DEQUEUE(&sc->sc_inq, m);
272 IF_DEQUEUE(&sc->sc_fastq, m);
277 while ((m = sc->sc_npqueue) != NULL) {
278 sc->sc_npqueue = m->m_act;
281 if (sc->sc_togo != NULL) {
282 m_freem(sc->sc_togo);
287 sc->sc_xc_state = NULL;
288 sc->sc_rc_state = NULL;
289 #endif /* PPP_COMPRESS */
293 * Ioctl routine for generic ppp devices.
296 pppioctl(sc, cmd, data, flag)
297 struct ppp_softc *sc;
301 struct proc *p = u.u_procp;
302 int s, error, flags, mru, mtu, nb, npx;
303 struct ppp_option_data *odp;
304 struct compressor **cp;
306 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
310 *(int *)data = sc->sc_inq.ifq_len;
314 *(int *)data = sc->sc_if.if_unit;
318 *(u_int *)data = sc->sc_flags;
324 flags = *(int *)data & SC_MASK;
326 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
329 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
337 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
342 *(int *)data = sc->sc_mru;
346 * PPPIOC[GS]MTU are implemented here, instead of supporting
347 * SIOC[GS]IFMTU in pppsioctl, because under Ultrix, we can't get an
348 * interface ioctl through to the interface until it has an IP
355 if (mtu <= 0 || mtu > PPP_MAXMRU)
358 sc->sc_if.if_mtu = mtu;
363 *(int *) data = sc->sc_if.if_mtu;
371 sl_compress_setup(&sc->sc_comp, *(int *)data);
379 sc->sc_xfer = p->p_pid;
383 case PPPIOCSCOMPRESS:
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))
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;
416 if (sc->sc_rc_state != NULL)
417 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
419 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
420 if (sc->sc_rc_state == NULL) {
421 if (sc->sc_flags & SC_DEBUG)
422 printf("ppp%d: decomp_alloc failed\n",
427 sc->sc_flags &= ~SC_DECOMP_RUN;
432 if (sc->sc_flags & SC_DEBUG)
433 printf("ppp%d: no compressor for [%x %x %x], %x\n",
434 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
436 return (EINVAL); /* no handler found */
437 #endif /* PPP_COMPRESS */
441 npi = (struct npioctl *) data;
442 switch (npi->protocol) {
449 if (cmd == PPPIOCGNPMODE) {
450 npi->mode = sc->sc_npmode[npx];
454 if (npi->mode != sc->sc_npmode[npx]) {
456 sc->sc_npmode[npx] = npi->mode;
457 if (npi->mode != NPMODE_QUEUE) {
473 * Process an ioctl request to the ppp network interface.
476 pppsioctl(ifp, cmd, data)
477 register struct ifnet *ifp;
481 struct proc *p = u.u_procp;
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;
486 struct ppp_comp_stats *pcp;
487 int s = splimp(), error = 0;
491 if ((ifp->if_flags & IFF_RUNNING) == 0)
492 ifp->if_flags &= ~IFF_UP;
496 if (ifa->ifa_addr.sa_family != AF_INET)
497 error = EAFNOSUPPORT;
501 if (ifa->ifa_addr.sa_family != AF_INET)
502 error = EAFNOSUPPORT;
506 * Ioctls other than the above don't get through until the
507 * interface has its IP addresses set :-(
514 sc->sc_if.if_mtu = ifr->ifr_mtu;
518 ifr->ifr_mtu = sc->sc_if.if_mtu;
523 psp = &((struct ifpppstatsreq *) data)->stats;
524 bzero(psp, sizeof(*psp));
525 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
526 psp->p.ppp_ipackets = ifp->if_ipackets;
527 psp->p.ppp_ierrors = ifp->if_ierrors;
528 psp->p.ppp_obytes = sc->sc_bytessent;
529 psp->p.ppp_opackets = ifp->if_opackets;
530 psp->p.ppp_oerrors = ifp->if_oerrors;
532 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
533 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
534 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
535 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
536 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
537 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
538 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
539 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
545 pcp = &((struct ifpppcstatsreq *) data)->stats;
546 bzero(pcp, sizeof(*pcp));
547 if (sc->sc_xc_state != NULL)
548 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
549 if (sc->sc_rc_state != NULL)
550 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
552 #endif /* PPP_COMPRESS */
562 * Queue a packet. Start transmission if not active.
563 * Packet is placed in Information field of PPP frame.
566 pppoutput(ifp, m0, dst)
569 struct sockaddr *dst;
571 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
572 struct ppp_header *ph;
573 int protocol, address, control;
580 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
581 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
582 error = ENETDOWN; /* sort of */
587 * Compute PPP header.
588 * We use the m_context field of the mbuf to indicate whether
589 * the packet should go on the fast queue.
592 switch (dst->sa_family) {
595 address = PPP_ALLSTATIONS;
598 mode = sc->sc_npmode[NP_IP];
601 * If this is a TCP packet to or from an "interactive" port,
602 * put the packet on the fastq instead.
604 if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
605 register int p = ntohl(((int *)ip)[ip->ip_hl]);
606 if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
612 address = PPP_ADDRESS(dst->sa_data);
613 control = PPP_CONTROL(dst->sa_data);
614 protocol = PPP_PROTOCOL(dst->sa_data);
618 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
619 error = EAFNOSUPPORT;
624 * Drop this packet, or return an error, if necessary.
626 if (mode == NPMODE_ERROR) {
630 if (mode == NPMODE_DROP) {
636 * Add PPP header. If no space in first mbuf, allocate another.
638 if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
641 MGET(m, M_DONTWAIT, MT_DATA);
650 m0->m_off -= PPP_HDRLEN;
652 cp = mtod(m0, u_char *);
655 *cp++ = protocol >> 8;
656 *cp++ = protocol & 0xff;
657 m0->m_len += PPP_HDRLEN;
659 if (sc->sc_flags & SC_LOG_OUTPKT) {
660 printf("ppp%d output: ", ifp->if_unit);
666 * See if bpf wants to look at the packet.
669 bpf_mtap(sc->sc_bpf, m0);
673 * Put the packet on the appropriate queue.
675 s = splimp(); /* splnet should be OK now */
676 if (mode == NPMODE_QUEUE) {
677 /* XXX we should limit the number of packets on this queue */
678 *sc->sc_npqtail = m0;
680 sc->sc_npqtail = &m0->m_act;
682 ifq = m0->m_context? &sc->sc_fastq: &ifp->if_snd;
686 sc->sc_if.if_oerrors++;
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 splimp (actually splnet would probably suffice).
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_context? &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
755 * at spltty()/splimp(), so it takes little time. If there isn't
756 * a packet waiting to go out, it schedules a software interrupt
757 * to prepare a new packet; the device start routine gets called
758 * again when a packet is ready.
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 splnet().
792 struct ppp_softc *sc;
798 for (i = 0; i < NPPP; ++i, ++sc) {
799 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
800 && (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 splnet.
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);
850 * If the packet is a TCP/IP packet, see if we can compress it.
852 if (sc->sc_flags & SC_COMP_TCP) {
857 ip = (struct ip *) (cp + PPP_HDRLEN);
858 if (mp->m_len <= PPP_HDRLEN) {
862 ip = mtod(mp, struct ip *);
864 /* this code assumes the IP/TCP header is in one non-shared mbuf */
865 if (ip->ip_p == IPPROTO_TCP) {
866 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
867 !(sc->sc_flags & SC_NO_TCP_CCID));
869 case TYPE_UNCOMPRESSED_TCP:
870 protocol = PPP_VJC_UNCOMP;
872 case TYPE_COMPRESSED_TCP:
873 protocol = PPP_VJC_COMP;
874 cp = mtod(m, u_char *);
875 cp[0] = address; /* header has moved */
880 cp[3] = protocol; /* update protocol in PPP header */
890 #endif /* PPP_COMPRESS */
894 if (protocol != PPP_LCP && protocol != PPP_CCP
895 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
900 for (mp = m; mp != NULL; mp = mp->m_next)
902 clen = (*sc->sc_xcomp->compress)
903 (sc->sc_xc_state, &mcomp, m, slen,
904 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
908 cp = mtod(m, u_char *);
912 #endif /* PPP_COMPRESS */
915 * Compress the address/control and protocol, if possible.
917 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
918 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
919 protocol != PPP_LCP) {
920 /* can compress address/control */
924 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
925 /* can compress protocol */
926 if (mtod(m, u_char *) == cp) {
927 cp[2] = cp[1]; /* move address/control up */
942 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
943 * 0 if it is about to be transmitted.
947 struct ppp_softc *sc;
957 * Get a pointer to the data after the PPP header.
959 if (m->m_len <= PPP_HDRLEN) {
963 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
966 dp = mtod(mp, u_char *) + PPP_HDRLEN;
969 ep = mtod(mp, u_char *) + mp->m_len;
970 if (dp + CCP_HDRLEN > ep)
972 slen = CCP_LENGTH(dp);
973 if (dp + slen > ep) {
974 if (sc->sc_flags & SC_DEBUG)
975 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
976 dp, slen, mtod(mp, u_char *), mp->m_len);
980 switch (CCP_CODE(dp)) {
984 /* CCP must be going down - disable compression */
985 if (sc->sc_flags & SC_CCP_UP) {
987 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
993 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
994 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
995 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
997 /* we're agreeing to send compressed packets. */
998 if (sc->sc_xc_state != NULL
999 && (*sc->sc_xcomp->comp_init)
1000 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1001 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1003 sc->sc_flags |= SC_COMP_RUN;
1007 /* peer is agreeing to send compressed packets. */
1008 if (sc->sc_rc_state != NULL
1009 && (*sc->sc_rcomp->decomp_init)
1010 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1011 sc->sc_if.if_unit, 0, sc->sc_mru,
1012 sc->sc_flags & SC_DEBUG)) {
1014 sc->sc_flags |= SC_DECOMP_RUN;
1015 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1023 if (sc->sc_flags & SC_CCP_UP) {
1025 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1026 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1028 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1029 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1031 sc->sc_flags &= ~SC_DC_ERROR;
1041 * CCP is down; free (de)compressor state if necessary.
1045 struct ppp_softc *sc;
1047 if (sc->sc_xc_state) {
1048 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1049 sc->sc_xc_state = NULL;
1051 if (sc->sc_rc_state) {
1052 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1053 sc->sc_rc_state = NULL;
1056 #endif /* PPP_COMPRESS */
1059 * PPP packet input routine.
1060 * The caller has checked and removed the FCS and has inserted
1061 * the address/control bytes and the protocol high byte if they
1065 ppppktin(sc, m, lost)
1066 struct ppp_softc *sc;
1072 m->m_context = lost;
1073 IF_ENQUEUE(&sc->sc_rawq, m);
1074 schednetisr(NETISR_PPP);
1079 * Process a received PPP packet, doing decompression as necessary.
1081 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1082 TYPE_UNCOMPRESSED_TCP)
1086 struct ppp_softc *sc;
1089 struct ifqueue *inq, *lock;
1090 int s, ilen, xlen, proto, rv;
1091 u_char *cp, adrs, ctrl;
1092 struct mbuf *mp, *dmp, *pc;
1096 sc->sc_if.if_ipackets++;
1098 if (sc->sc_flags & SC_LOG_INPKT) {
1099 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1103 cp = mtod(m, u_char *);
1104 adrs = PPP_ADDRESS(cp);
1105 ctrl = PPP_CONTROL(cp);
1106 proto = PPP_PROTOCOL(cp);
1110 sc->sc_flags |= SC_VJ_RESET;
1116 * Decompress this packet if necessary, update the receiver's
1117 * dictionary, or take appropriate action on a CCP packet.
1119 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1120 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1121 /* decompress this packet */
1122 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1123 if (rv == DECOMP_OK) {
1126 /* no error, but no decompressed packet produced */
1130 cp = mtod(m, u_char *);
1131 proto = PPP_PROTOCOL(cp);
1135 * An error has occurred in decompression.
1136 * Pass the compressed packet up to pppd, which may take
1137 * CCP down or issue a Reset-Req.
1139 if (sc->sc_flags & SC_DEBUG)
1140 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1142 sc->sc_flags |= SC_VJ_RESET;
1143 if (rv == DECOMP_ERROR)
1144 sc->sc_flags |= SC_DC_ERROR;
1146 sc->sc_flags |= SC_DC_FERROR;
1151 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1152 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1154 if (proto == PPP_CCP) {
1161 for (mp = m; mp != NULL; mp = mp->m_next)
1165 if (sc->sc_flags & SC_VJ_RESET) {
1167 * If we've missed a packet, we must toss subsequent compressed
1168 * packets which don't have an explicit connection ID.
1170 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1172 sc->sc_flags &= ~SC_VJ_RESET;
1177 * See if we have a VJ-compressed packet to uncompress.
1179 if (proto == PPP_VJC_COMP) {
1180 if (sc->sc_flags & SC_REJ_COMP_TCP)
1183 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1184 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1185 &sc->sc_comp, &iphdr, &hlen);
1188 if (sc->sc_flags & SC_DEBUG)
1189 printf("ppp%d: VJ uncompress failed on type comp\n",
1194 /* Copy the PPP and IP headers into a new mbuf. */
1195 MGET(mp, M_DONTWAIT, MT_DATA);
1200 if (hlen + PPP_HDRLEN > MLEN) {
1202 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1204 goto bad; /* lose if big headers and no clusters */
1207 cp = mtod(mp, u_char *);
1213 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1214 mp->m_len = hlen + PPP_HDRLEN;
1217 * Trim the PPP and VJ headers off the old mbuf
1218 * and stick the new and old mbufs together.
1220 m->m_off += PPP_HDRLEN + xlen;
1221 m->m_len -= PPP_HDRLEN + xlen;
1222 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1223 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1224 mp->m_len += m->m_len;
1225 MFREE(m, mp->m_next);
1229 ilen += hlen - xlen;
1231 } else if (proto == PPP_VJC_UNCOMP) {
1232 if (sc->sc_flags & SC_REJ_COMP_TCP)
1235 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1236 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1237 &sc->sc_comp, &iphdr, &hlen);
1240 if (sc->sc_flags & SC_DEBUG)
1241 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1252 * If the packet will fit in an ordinary mbuf, don't waste a
1253 * whole cluster on it.
1255 if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1256 MGET(mp, M_DONTWAIT, MT_DATA);
1258 m_copydata(m, mtod(mp, caddr_t), ilen);
1266 /* See if bpf wants to look at the packet. */
1268 bpf_mtap(sc->sc_bpf, m);
1276 * IP packet - take off the ppp header and pass it up to IP.
1278 if ((sc->sc_if.if_flags & IFF_UP) == 0
1279 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1280 /* interface is down - drop the packet. */
1284 m->m_off += PPP_HDRLEN;
1285 m->m_len -= PPP_HDRLEN;
1286 schednetisr(NETISR_IP);
1293 * Some other protocol - place on input queue for read().
1301 * Put the packet on the appropriate input queue.
1305 smp_lock(&lock->lk_ifqueue, LK_RETRY);
1306 if (IF_QFULL(inq)) {
1308 /* XXX should we unlock here? */
1310 if (sc->sc_flags & SC_DEBUG)
1311 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1314 IF_ENQUEUEIF(inq, m, &sc->sc_if);
1315 smp_unlock(&lock->lk_ifqueue);
1325 sc->sc_if.if_ierrors++;
1328 #define MAX_DUMP_BYTES 128
1334 char buf[3*MAX_DUMP_BYTES+4];
1337 static char digits[] = "0123456789abcdef";
1339 for (m = m0; m; m = m->m_next) {
1341 u_char *rptr = mtod(m, u_char *);
1344 if (bp > buf + sizeof(buf) - 4)
1346 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1347 *bp++ = digits[*rptr++ & 0xf];
1351 if (bp > buf + sizeof(buf) - 3)
1361 printf("%s\n", buf);
1364 #endif /* NPPP > 0 */