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.2 1994/11/21 04:50:36 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 "slcompress.h"
108 #include "ppp_defs.h"
110 #include "if_pppvar.h"
113 #define PACKETPTR struct mbuf *
114 #include "ppp-comp.h"
117 void pppattach __P((void));
118 int pppioctl __P((struct ppp_softc *sc, int cmd, caddr_t data, int flag,
120 int pppoutput __P((struct ifnet *ifp, struct mbuf *m0,
121 struct sockaddr *dst));
122 int pppsioctl __P((struct ifnet *ifp, int cmd, caddr_t data));
123 void pppintr __P((void));
125 static void ppp_outpkt __P((struct ppp_softc *));
126 static int ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
127 static void ppp_ccp_closed __P((struct ppp_softc *));
128 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
129 static void pppdumpm __P((struct mbuf *m0));
132 * Some useful mbuf macros not in mbuf.h.
134 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
136 #define M_TRAILINGSPACE(m) \
137 ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
138 - ((m)->m_off + (m)->m_len))
140 #define M_OFFSTART(m) \
141 (M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr : MMINOFF)
143 #define M_DATASIZE(m) \
144 (M_IS_CLUSTER(m) ? M_CLUSTERSZ : MLEN)
147 * The following disgusting hack gets around the problem that IP TOS
148 * can't be set yet. We want to put "interactive" traffic on a high
149 * priority queue. To decide if traffic is interactive, we check that
150 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
152 static u_short interactive_ports[8] = {
156 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
160 * List of compressors we know about.
163 extern struct compressor ppp_bsd_compress;
165 struct compressor *ppp_compressors[] = {
169 #endif /* PPP_COMPRESS */
172 * Called from boot code to establish ppp interfaces.
177 register struct ppp_softc *sc;
180 for (sc = ppp_softc; i < NPPP; sc++) {
181 sc->sc_if.if_name = "ppp";
182 sc->sc_if.if_unit = i++;
183 sc->sc_if.if_mtu = PPP_MTU;
184 sc->sc_if.if_flags = IFF_POINTOPOINT;
185 sc->sc_if.if_type = IFT_PPP;
186 sc->sc_if.if_ioctl = pppsioctl;
187 sc->sc_if.if_output = pppoutput;
188 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
189 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
190 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
191 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
192 if_attach(&sc->sc_if);
194 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
200 * Allocate a ppp interface unit and initialize it.
207 struct ppp_softc *sc;
209 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
210 if (sc->sc_xfer == pid) {
215 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
216 if (sc->sc_devp == NULL)
222 sc->sc_mru = PPP_MRU;
224 sl_compress_init(&sc->sc_comp);
227 sc->sc_xc_state = NULL;
228 sc->sc_rc_state = NULL;
229 #endif /* PPP_COMPRESS */
230 for (i = 0; i < NUM_NP; ++i)
231 sc->sc_npmode[i] = NPMODE_ERROR;
232 sc->sc_if.if_flags |= IFF_RUNNING;
238 * Deallocate a ppp unit.
242 struct ppp_softc *sc;
247 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
251 IF_DEQUEUE(&sc->sc_rawq, m);
257 IF_DEQUEUE(&sc->sc_inq, m);
263 IF_DEQUEUE(&sc->sc_fastq, m);
268 if (sc->sc_togo != NULL) {
269 m_freem(sc->sc_togo);
274 sc->sc_xc_state = NULL;
275 sc->sc_rc_state = NULL;
276 #endif /* PPP_COMPRESS */
280 * Ioctl routine for generic ppp devices.
283 pppioctl(sc, cmd, data, flag)
284 struct ppp_softc *sc;
288 struct proc *p = u.u_procp;
289 int s, error, flags, mru, nb, npx;
290 struct ppp_option_data *odp;
291 struct compressor **cp;
293 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
297 *(int *)data = sc->sc_inq.ifq_len;
301 *(int *)data = sc->sc_if.if_unit;
305 *(u_int *)data = sc->sc_flags;
311 flags = *(int *)data & SC_MASK;
313 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
316 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
324 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
329 *(int *)data = sc->sc_mru;
337 sl_compress_setup(&sc->sc_comp, *(int *)data);
345 sc->sc_xfer = p->p_pid;
349 case PPPIOCSCOMPRESS:
352 odp = (struct ppp_option_data *) data;
354 if (nb > sizeof(ccp_option))
355 nb = sizeof(ccp_option);
356 if (error = copyin(odp->ptr, ccp_option, nb))
358 if (ccp_option[1] < 2) /* preliminary check on the length byte */
360 for (cp = ppp_compressors; *cp != NULL; ++cp)
361 if ((*cp)->compress_proto == ccp_option[0]) {
363 * Found a handler for the protocol - try to allocate
364 * a compressor or decompressor.
369 if (sc->sc_xc_state != NULL)
370 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
372 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
373 if (sc->sc_xc_state == NULL) {
374 if (sc->sc_flags & SC_DEBUG)
375 printf("ppp%d: comp_alloc failed\n",
380 sc->sc_flags &= ~SC_COMP_RUN;
382 if (sc->sc_rc_state != NULL)
383 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
385 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
386 if (sc->sc_rc_state == NULL) {
387 if (sc->sc_flags & SC_DEBUG)
388 printf("ppp%d: decomp_alloc failed\n",
393 sc->sc_flags &= ~SC_DECOMP_RUN;
398 if (sc->sc_flags & SC_DEBUG)
399 printf("ppp%d: no compressor for [%x %x %x], %x\n",
400 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
402 return (EINVAL); /* no handler found */
403 #endif /* PPP_COMPRESS */
407 npi = (struct npioctl *) data;
408 switch (npi->protocol) {
415 if (cmd == PPPIOCGNPMODE) {
416 npi->mode = sc->sc_npmode[npx];
420 if (npi->mode != sc->sc_npmode[npx]) {
422 sc->sc_npmode[npx] = npi->mode;
423 if (npi->mode != NPMODE_QUEUE)
437 * Process an ioctl request to the ppp network interface.
440 pppsioctl(ifp, cmd, data)
441 register struct ifnet *ifp;
445 struct proc *p = u.u_procp;
446 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
447 register struct ifaddr *ifa = (struct ifaddr *)data;
448 register struct ifreq *ifr = (struct ifreq *)data;
449 struct ppp_stats *psp;
450 struct ppp_comp_stats *pcp;
451 int s = splimp(), error = 0;
455 if ((ifp->if_flags & IFF_RUNNING) == 0)
456 ifp->if_flags &= ~IFF_UP;
460 if (ifa->ifa_addr->sa_family != AF_INET)
461 error = EAFNOSUPPORT;
465 if (ifa->ifa_addr->sa_family != AF_INET)
466 error = EAFNOSUPPORT;
472 sc->sc_if.if_mtu = ifr->ifr_mtu;
476 ifr->ifr_mtu = sc->sc_if.if_mtu;
480 psp = &((struct ifpppstatsreq *) data)->stats;
481 bzero(psp, sizeof(*psp));
482 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
483 psp->p.ppp_ipackets = ifp->if_ipackets;
484 psp->p.ppp_ierrors = ifp->if_ierrors;
485 psp->p.ppp_obytes = sc->sc_bytessent;
486 psp->p.ppp_opackets = ifp->if_opackets;
487 psp->p.ppp_oerrors = ifp->if_oerrors;
489 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
490 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
491 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
492 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
493 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
494 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
495 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
496 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
502 pcp = &((struct ifpppcstatsreq *) data)->stats;
503 bzero(pcp, sizeof(*pcp));
504 if (sc->sc_xc_state != NULL)
505 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
506 if (sc->sc_rc_state != NULL)
507 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
509 #endif /* PPP_COMPRESS */
519 * Queue a packet. Start transmission if not active.
520 * Packet is placed in Information field of PPP frame.
523 pppoutput(ifp, m0, dst)
526 struct sockaddr *dst;
528 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
529 struct ppp_header *ph;
530 int protocol, address, control;
537 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
538 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
539 error = ENETDOWN; /* sort of */
544 * Compute PPP header.
547 switch (dst->sa_family) {
550 address = PPP_ALLSTATIONS;
553 mode = sc->sc_npmode[NP_IP];
556 * If this is a TCP packet to or from an "interactive" port,
557 * put the packet on the fastq instead.
559 if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
560 register int p = ntohl(((int *)ip)[ip->ip_hl]);
561 if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
567 address = PPP_ADDRESS(dst->sa_data);
568 control = PPP_CONTROL(dst->sa_data);
569 protocol = PPP_PROTOCOL(dst->sa_data);
573 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
574 error = EAFNOSUPPORT;
579 * Drop this packet, or return an error, if necessary.
581 if (mode == NPMODE_ERROR) {
585 if (mode == NPMODE_DROP) {
591 * Add PPP header. If no space in first mbuf, allocate another.
593 if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
596 MGET(m, M_DONTWAIT, MT_DATA);
605 m0->m_off -= PPP_HDRLEN;
607 cp = mtod(m0, u_char *);
610 *cp++ = protocol >> 8;
611 *cp++ = protocol & 0xff;
612 m0->m_len += PPP_HDRLEN;
614 if (sc->sc_flags & SC_LOG_OUTPKT) {
615 printf("ppp%d output: ", ifp->if_unit);
621 * See if bpf wants to look at the packet.
624 bpf_mtap(sc->sc_bpf, m0);
628 * Put the packet on the appropriate queue.
630 s = splimp(); /* splnet should be OK now */
634 sc->sc_if.if_oerrors++;
641 * Tell the device to send it out.
643 if (mode == NPMODE_PASS)
655 * Get a packet to send. This procedure is intended to be called
656 * at spltty()/splimp(), so it takes little time. If there isn't
657 * a packet waiting to go out, it schedules a software interrupt
658 * to prepare a new packet; the device start routine gets called
659 * again when a packet is ready.
663 struct ppp_softc *sc;
670 * Had a packet waiting - send it.
673 sc->sc_flags |= SC_TBUSY;
677 * Remember we wanted a packet and schedule a software interrupt.
679 sc->sc_flags &= ~SC_TBUSY;
680 schednetisr(NETISR_PPP);
685 * Software interrupt routine, called at splnet().
690 struct ppp_softc *sc;
695 for (i = 0; i < NPPP; ++i, ++sc) {
696 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
697 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
700 IF_DEQUEUE(&sc->sc_rawq, m);
709 * Grab another packet off a queue and apply VJ compression,
710 * packet compression, address/control and/or protocol compression
711 * if enabled. Should be called at splnet.
715 struct ppp_softc *sc;
718 struct mbuf *m, *mp, **mpp;
720 int address, control, protocol;
725 * Scan through the send queues looking for a packet
726 * which can be sent: first the fast queue, then the normal queue.
730 mpp = &ifq->ifq_head;
732 while ((m = *mpp) != NULL) {
733 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
735 mode = sc->sc_npmode[NP_IP];
740 if (mode == NPMODE_PASS)
758 if (ifq == &sc->sc_if.if_snd)
760 /* Finished the fast queue; do the normal queue. */
761 ifq = &sc->sc_if.if_snd;
767 if ((*mpp = m->m_nextpkt) == NULL)
773 * Extract the ppp header of the new packet.
774 * The ppp header will be in one mbuf.
776 cp = mtod(m, u_char *);
777 address = PPP_ADDRESS(cp);
778 control = PPP_CONTROL(cp);
779 protocol = PPP_PROTOCOL(cp);
785 * If the packet is a TCP/IP packet, see if we can compress it.
787 if (sc->sc_flags & SC_COMP_TCP) {
792 ip = (struct ip *) (cp + PPP_HDRLEN);
793 if (mp->m_len <= PPP_HDRLEN) {
797 ip = mtod(mp, struct ip *);
799 /* this code assumes the IP/TCP header is in one non-shared mbuf */
800 if (ip->ip_p == IPPROTO_TCP) {
801 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
802 !(sc->sc_flags & SC_NO_TCP_CCID));
804 case TYPE_UNCOMPRESSED_TCP:
805 protocol = PPP_VJC_UNCOMP;
807 case TYPE_COMPRESSED_TCP:
808 protocol = PPP_VJC_COMP;
809 cp = mtod(m, u_char *);
810 cp[0] = address; /* header has moved */
815 cp[3] = protocol; /* update protocol in PPP header */
825 #endif /* PPP_COMPRESS */
829 if (protocol != PPP_LCP && protocol != PPP_CCP
830 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
835 for (mp = m; mp != NULL; mp = mp->m_next)
837 clen = (*sc->sc_xcomp->compress)
838 (sc->sc_xc_state, &mcomp, m, slen,
839 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
843 cp = mtod(m, u_char *);
847 #endif /* PPP_COMPRESS */
850 * Compress the address/control and protocol, if possible.
852 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
853 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
854 protocol != PPP_LCP) {
855 /* can compress address/control */
859 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
860 /* can compress protocol */
861 if (mtod(m, u_char *) == cp) {
862 cp[2] = cp[1]; /* move address/control up */
877 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
878 * 0 if it is about to be transmitted.
882 struct ppp_softc *sc;
892 * Get a pointer to the data after the PPP header.
894 if (m->m_len <= PPP_HDRLEN) {
898 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
901 dp = mtod(mp, u_char *) + PPP_HDRLEN;
904 ep = mtod(mp, u_char *) + mp->m_len;
905 if (dp + CCP_HDRLEN > ep)
907 slen = CCP_LENGTH(dp);
908 if (dp + slen > ep) {
909 if (sc->sc_flags & SC_DEBUG)
910 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
911 dp, slen, mtod(mp, u_char *), mp->m_len);
915 switch (CCP_CODE(dp)) {
919 /* CCP must be going down - disable compression */
920 if (sc->sc_flags & SC_CCP_UP) {
922 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
928 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
929 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
930 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
932 /* we're agreeing to send compressed packets. */
933 if (sc->sc_xc_state != NULL
934 && (*sc->sc_xcomp->comp_init)
935 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
936 sc->sc_if.if_unit, sc->sc_flags & SC_DEBUG)) {
938 sc->sc_flags |= SC_COMP_RUN;
942 /* peer is agreeing to send compressed packets. */
943 if (sc->sc_rc_state != NULL
944 && (*sc->sc_rcomp->decomp_init)
945 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
946 sc->sc_if.if_unit, sc->sc_mru,
947 sc->sc_flags & SC_DEBUG)) {
949 sc->sc_flags |= SC_DECOMP_RUN;
950 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
958 if (sc->sc_flags & SC_CCP_UP) {
960 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
961 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
963 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
964 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
966 sc->sc_flags &= ~SC_DC_ERROR;
976 * CCP is down; free (de)compressor state if necessary.
980 struct ppp_softc *sc;
982 if (sc->sc_xc_state) {
983 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
984 sc->sc_xc_state = NULL;
986 if (sc->sc_rc_state) {
987 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
988 sc->sc_rc_state = NULL;
991 #endif /* PPP_COMPRESS */
994 * PPP packet input routine.
995 * The caller has checked and removed the FCS and has inserted
996 * the address/control bytes and the protocol high byte if they
997 * were omitted. Should be called at splimp/spltty.
999 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
1002 ppppktin(sc, m, lost)
1003 struct ppp_softc *sc;
1008 m->m_flags |= M_ERRMARK;
1009 IF_ENQUEUE(&sc->sc_rawq, m);
1010 schednetisr(NETISR_PPP);
1014 * Process a received PPP packet, doing decompression as necessary.
1016 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1017 TYPE_UNCOMPRESSED_TCP)
1021 struct ppp_softc *sc;
1024 struct ifqueue *inq, *lock;
1025 int s, ilen, xlen, proto, rv;
1026 u_char *cp, adrs, ctrl;
1027 struct mbuf *mp, *dmp, *pc;
1031 sc->sc_if.if_ipackets++;
1033 if (sc->sc_flags & SC_LOG_INPKT) {
1034 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1038 cp = mtod(m, u_char *);
1039 adrs = PPP_ADDRESS(cp);
1040 ctrl = PPP_CONTROL(cp);
1041 proto = PPP_PROTOCOL(cp);
1043 if (m->m_flags & M_ERRMARK) {
1044 m->m_flags &= ~M_ERRMARK;
1046 sc->sc_flags |= SC_VJ_RESET;
1052 * Decompress this packet if necessary, update the receiver's
1053 * dictionary, or take appropriate action on a CCP packet.
1055 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1056 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1057 /* decompress this packet */
1058 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1062 cp = mtod(m, u_char *);
1063 proto = PPP_PROTOCOL(cp);
1066 /* pass the compressed packet up to pppd, which may take
1067 CCP down or issue a Reset-Req. */
1068 if (sc->sc_flags & SC_DEBUG)
1069 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1071 sc->sc_flags |= SC_VJ_RESET;
1074 /* no error, but no decompressed packet produced */
1079 sc->sc_flags |= SC_DC_ERROR;
1081 case DECOMP_FATALERROR:
1082 sc->sc_flags |= SC_DC_FERROR;
1089 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1090 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1092 if (proto == PPP_CCP) {
1099 for (mp = m; mp != NULL; mp = mp->m_next)
1103 if (sc->sc_flags & SC_VJ_RESET) {
1105 * If we've missed a packet, we must toss subsequent compressed
1106 * packets which don't have an explicit connection ID.
1108 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1110 sc->sc_flags &= ~SC_VJ_RESET;
1115 * See if we have a VJ-compressed packet to uncompress.
1117 if (proto == PPP_VJC_COMP) {
1118 if (sc->sc_flags & SC_REJ_COMP_TCP)
1121 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1122 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1123 &sc->sc_comp, &iphdr, &hlen);
1126 if (sc->sc_flags & SC_DEBUG)
1127 printf("ppp%d: VJ uncompress failed on type comp\n",
1132 /* Copy the PPP and IP headers into a new mbuf. */
1133 MGET(mp, M_DONTWAIT, MT_DATA);
1138 if (hlen + PPP_HDRLEN > MHLEN) {
1140 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1142 goto bad; /* lose if big headers and no clusters */
1145 cp = mtod(mp, u_char *);
1151 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1152 mp->m_len = hlen + PPP_HDRLEN;
1155 * Trim the PPP and VJ headers off the old mbuf
1156 * and stick the new and old mbufs together.
1158 m->m_off += PPP_HDRLEN + xlen;
1159 m->m_len -= PPP_HDRLEN + xlen;
1160 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1161 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1162 mp->m_len += m->m_len;
1163 MFREE(m, mp->m_next);
1167 ilen += hlen - xlen;
1169 } else if (proto == PPP_VJC_UNCOMP) {
1170 if (sc->sc_flags & SC_REJ_COMP_TCP)
1173 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1174 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1175 &sc->sc_comp, &iphdr, &hlen);
1178 if (sc->sc_flags & SC_DEBUG)
1179 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1190 * If the packet will fit in an ordinary mbuf, don't waste a
1191 * whole cluster on it.
1193 if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1194 MGET(mp, M_DONTWAIT, MT_DATA);
1196 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1204 /* See if bpf wants to look at the packet. */
1206 bpf_mtap(sc->sc_bpf, m);
1213 * IP packet - take off the ppp header and pass it up to IP.
1215 if ((sc->sc_if.if_flags & IFF_UP) == 0
1216 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1217 /* interface is down - drop the packet. */
1221 m->m_off += PPP_HDRLEN;
1222 m->m_len -= PPP_HDRLEN;
1223 schednetisr(NETISR_IP);
1230 * Some other protocol - place on input queue for read().
1238 * Put the packet on the appropriate input queue.
1242 smp_lock(&lock->lk_ifqueue, LK_RETRY);
1243 if (IF_QFULL(inq)) {
1245 /* XXX should we unlock here? */
1247 if (sc->sc_flags & SC_DEBUG)
1248 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1251 IF_ENQUEUEIF(inq, m, &sc->sc_if);
1252 smp_unlock(&lock->lk_ifqueue);
1262 sc->sc_if.if_ierrors++;
1265 #define MAX_DUMP_BYTES 128
1271 char buf[3*MAX_DUMP_BYTES+4];
1274 static char digits[] = "0123456789abcdef";
1276 for (m = m0; m; m = m->m_next) {
1278 u_char *rptr = mtod(m, u_char *);
1281 if (bp > buf + sizeof(buf) - 4)
1283 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1284 *bp++ = digits[*rptr++ & 0xf];
1288 if (bp > buf + sizeof(buf) - 3)
1298 printf("%s\n", buf);
1301 #endif /* NPPP > 0 */