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.3 1994/11/28 01:38:25 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_outpkt __P((struct ppp_softc *));
131 static int ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
132 static void ppp_ccp_closed __P((struct ppp_softc *));
133 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
134 static void pppdumpm __P((struct mbuf *m0));
137 * Some useful mbuf macros not in mbuf.h.
139 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
141 #define M_TRAILINGSPACE(m) \
142 ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
143 - ((m)->m_off + (m)->m_len))
145 #define M_OFFSTART(m) \
146 (M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr : MMINOFF)
148 #define M_DATASIZE(m) \
149 (M_IS_CLUSTER(m) ? M_CLUSTERSZ : MLEN)
152 * The following disgusting hack gets around the problem that IP TOS
153 * can't be set yet. We want to put "interactive" traffic on a high
154 * priority queue. To decide if traffic is interactive, we check that
155 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
157 static u_short interactive_ports[8] = {
161 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
165 * List of compressors we know about.
168 extern struct compressor ppp_bsd_compress;
170 struct compressor *ppp_compressors[] = {
174 #endif /* PPP_COMPRESS */
177 * Called from boot code to establish ppp interfaces.
182 register struct ppp_softc *sc;
185 for (sc = ppp_softc; i < NPPP; sc++) {
186 sc->sc_if.if_name = "ppp";
187 sc->sc_if.if_unit = i++;
188 sc->sc_if.if_mtu = PPP_MTU;
189 sc->sc_if.if_flags = IFF_POINTOPOINT;
190 sc->sc_if.if_type = IFT_PPP;
191 sc->sc_if.if_ioctl = pppsioctl;
192 sc->sc_if.if_output = pppoutput;
193 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
194 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
195 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
196 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
197 if_attach(&sc->sc_if);
199 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
205 * Allocate a ppp interface unit and initialize it.
212 struct ppp_softc *sc;
214 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
215 if (sc->sc_xfer == pid) {
220 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
221 if (sc->sc_devp == NULL)
227 sc->sc_mru = PPP_MRU;
229 sl_compress_init(&sc->sc_comp);
232 sc->sc_xc_state = NULL;
233 sc->sc_rc_state = NULL;
234 #endif /* PPP_COMPRESS */
235 for (i = 0; i < NUM_NP; ++i)
236 sc->sc_npmode[i] = NPMODE_ERROR;
237 sc->sc_if.if_flags |= IFF_RUNNING;
243 * Deallocate a ppp unit.
247 struct ppp_softc *sc;
252 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
256 IF_DEQUEUE(&sc->sc_rawq, m);
262 IF_DEQUEUE(&sc->sc_inq, m);
268 IF_DEQUEUE(&sc->sc_fastq, m);
273 if (sc->sc_togo != NULL) {
274 m_freem(sc->sc_togo);
279 sc->sc_xc_state = NULL;
280 sc->sc_rc_state = NULL;
281 #endif /* PPP_COMPRESS */
285 * Ioctl routine for generic ppp devices.
288 pppioctl(sc, cmd, data, flag)
289 struct ppp_softc *sc;
293 struct proc *p = u.u_procp;
294 int s, error, flags, mru, nb, npx;
295 struct ppp_option_data *odp;
296 struct compressor **cp;
298 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
302 *(int *)data = sc->sc_inq.ifq_len;
306 *(int *)data = sc->sc_if.if_unit;
310 *(u_int *)data = sc->sc_flags;
316 flags = *(int *)data & SC_MASK;
318 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
321 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
329 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
334 *(int *)data = sc->sc_mru;
342 sl_compress_setup(&sc->sc_comp, *(int *)data);
350 sc->sc_xfer = p->p_pid;
354 case PPPIOCSCOMPRESS:
357 odp = (struct ppp_option_data *) data;
359 if (nb > sizeof(ccp_option))
360 nb = sizeof(ccp_option);
361 if (error = copyin(odp->ptr, ccp_option, nb))
363 if (ccp_option[1] < 2) /* preliminary check on the length byte */
365 for (cp = ppp_compressors; *cp != NULL; ++cp)
366 if ((*cp)->compress_proto == ccp_option[0]) {
368 * Found a handler for the protocol - try to allocate
369 * a compressor or decompressor.
374 if (sc->sc_xc_state != NULL)
375 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
377 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
378 if (sc->sc_xc_state == NULL) {
379 if (sc->sc_flags & SC_DEBUG)
380 printf("ppp%d: comp_alloc failed\n",
385 sc->sc_flags &= ~SC_COMP_RUN;
387 if (sc->sc_rc_state != NULL)
388 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
390 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
391 if (sc->sc_rc_state == NULL) {
392 if (sc->sc_flags & SC_DEBUG)
393 printf("ppp%d: decomp_alloc failed\n",
398 sc->sc_flags &= ~SC_DECOMP_RUN;
403 if (sc->sc_flags & SC_DEBUG)
404 printf("ppp%d: no compressor for [%x %x %x], %x\n",
405 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
407 return (EINVAL); /* no handler found */
408 #endif /* PPP_COMPRESS */
412 npi = (struct npioctl *) data;
413 switch (npi->protocol) {
420 if (cmd == PPPIOCGNPMODE) {
421 npi->mode = sc->sc_npmode[npx];
425 if (npi->mode != sc->sc_npmode[npx]) {
427 sc->sc_npmode[npx] = npi->mode;
428 if (npi->mode != NPMODE_QUEUE)
442 * Process an ioctl request to the ppp network interface.
445 pppsioctl(ifp, cmd, data)
446 register struct ifnet *ifp;
450 struct proc *p = u.u_procp;
451 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
452 register struct ifaddr *ifa = (struct ifaddr *)data;
453 register struct ifreq *ifr = (struct ifreq *)data;
454 struct ppp_stats *psp;
455 struct ppp_comp_stats *pcp;
456 int s = splimp(), error = 0;
460 if ((ifp->if_flags & IFF_RUNNING) == 0)
461 ifp->if_flags &= ~IFF_UP;
465 if (ifa->ifa_addr.sa_family != AF_INET)
466 error = EAFNOSUPPORT;
470 if (ifa->ifa_addr.sa_family != AF_INET)
471 error = EAFNOSUPPORT;
477 sc->sc_if.if_mtu = ifr->ifr_mtu;
481 ifr->ifr_mtu = sc->sc_if.if_mtu;
485 psp = &((struct ifpppstatsreq *) data)->stats;
486 bzero(psp, sizeof(*psp));
487 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
488 psp->p.ppp_ipackets = ifp->if_ipackets;
489 psp->p.ppp_ierrors = ifp->if_ierrors;
490 psp->p.ppp_obytes = sc->sc_bytessent;
491 psp->p.ppp_opackets = ifp->if_opackets;
492 psp->p.ppp_oerrors = ifp->if_oerrors;
494 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
495 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
496 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
497 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
498 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
499 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
500 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
501 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
507 pcp = &((struct ifpppcstatsreq *) data)->stats;
508 bzero(pcp, sizeof(*pcp));
509 if (sc->sc_xc_state != NULL)
510 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
511 if (sc->sc_rc_state != NULL)
512 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
514 #endif /* PPP_COMPRESS */
524 * Queue a packet. Start transmission if not active.
525 * Packet is placed in Information field of PPP frame.
528 pppoutput(ifp, m0, dst)
531 struct sockaddr *dst;
533 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
534 struct ppp_header *ph;
535 int protocol, address, control;
542 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
543 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
544 error = ENETDOWN; /* sort of */
549 * Compute PPP header.
552 switch (dst->sa_family) {
555 address = PPP_ALLSTATIONS;
558 mode = sc->sc_npmode[NP_IP];
561 * If this is a TCP packet to or from an "interactive" port,
562 * put the packet on the fastq instead.
564 if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
565 register int p = ntohl(((int *)ip)[ip->ip_hl]);
566 if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
572 address = PPP_ADDRESS(dst->sa_data);
573 control = PPP_CONTROL(dst->sa_data);
574 protocol = PPP_PROTOCOL(dst->sa_data);
578 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
579 error = EAFNOSUPPORT;
584 * Drop this packet, or return an error, if necessary.
586 if (mode == NPMODE_ERROR) {
590 if (mode == NPMODE_DROP) {
596 * Add PPP header. If no space in first mbuf, allocate another.
598 if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
601 MGET(m, M_DONTWAIT, MT_DATA);
610 m0->m_off -= PPP_HDRLEN;
612 cp = mtod(m0, u_char *);
615 *cp++ = protocol >> 8;
616 *cp++ = protocol & 0xff;
617 m0->m_len += PPP_HDRLEN;
619 if (sc->sc_flags & SC_LOG_OUTPKT) {
620 printf("ppp%d output: ", ifp->if_unit);
626 * See if bpf wants to look at the packet.
629 bpf_mtap(sc->sc_bpf, m0);
633 * Put the packet on the appropriate queue.
635 s = splimp(); /* splnet should be OK now */
639 sc->sc_if.if_oerrors++;
646 * Tell the device to send it out.
648 if (mode == NPMODE_PASS)
660 * Get a packet to send. This procedure is intended to be called
661 * at spltty()/splimp(), so it takes little time. If there isn't
662 * a packet waiting to go out, it schedules a software interrupt
663 * to prepare a new packet; the device start routine gets called
664 * again when a packet is ready.
668 struct ppp_softc *sc;
675 * Had a packet waiting - send it.
678 sc->sc_flags |= SC_TBUSY;
682 * Remember we wanted a packet and schedule a software interrupt.
684 sc->sc_flags &= ~SC_TBUSY;
685 schednetisr(NETISR_PPP);
690 * Software interrupt routine, called at splnet().
695 struct ppp_softc *sc;
700 for (i = 0; i < NPPP; ++i, ++sc) {
701 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
702 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
705 IF_DEQUEUE(&sc->sc_rawq, m);
714 * Grab another packet off a queue and apply VJ compression,
715 * packet compression, address/control and/or protocol compression
716 * if enabled. Should be called at splnet.
720 struct ppp_softc *sc;
723 struct mbuf *m, *mp, **mpp;
725 int address, control, protocol;
730 * Scan through the send queues looking for a packet
731 * which can be sent: first the fast queue, then the normal queue.
735 mpp = &ifq->ifq_head;
737 while ((m = *mpp) != NULL) {
738 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
740 mode = sc->sc_npmode[NP_IP];
745 if (mode == NPMODE_PASS)
763 if (ifq == &sc->sc_if.if_snd)
765 /* Finished the fast queue; do the normal queue. */
766 ifq = &sc->sc_if.if_snd;
772 if ((*mpp = m->m_act) == NULL)
778 * Extract the ppp header of the new packet.
779 * The ppp header will be in one mbuf.
781 cp = mtod(m, u_char *);
782 address = PPP_ADDRESS(cp);
783 control = PPP_CONTROL(cp);
784 protocol = PPP_PROTOCOL(cp);
790 * If the packet is a TCP/IP packet, see if we can compress it.
792 if (sc->sc_flags & SC_COMP_TCP) {
797 ip = (struct ip *) (cp + PPP_HDRLEN);
798 if (mp->m_len <= PPP_HDRLEN) {
802 ip = mtod(mp, struct ip *);
804 /* this code assumes the IP/TCP header is in one non-shared mbuf */
805 if (ip->ip_p == IPPROTO_TCP) {
806 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
807 !(sc->sc_flags & SC_NO_TCP_CCID));
809 case TYPE_UNCOMPRESSED_TCP:
810 protocol = PPP_VJC_UNCOMP;
812 case TYPE_COMPRESSED_TCP:
813 protocol = PPP_VJC_COMP;
814 cp = mtod(m, u_char *);
815 cp[0] = address; /* header has moved */
820 cp[3] = protocol; /* update protocol in PPP header */
830 #endif /* PPP_COMPRESS */
834 if (protocol != PPP_LCP && protocol != PPP_CCP
835 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
840 for (mp = m; mp != NULL; mp = mp->m_next)
842 clen = (*sc->sc_xcomp->compress)
843 (sc->sc_xc_state, &mcomp, m, slen,
844 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
848 cp = mtod(m, u_char *);
852 #endif /* PPP_COMPRESS */
855 * Compress the address/control and protocol, if possible.
857 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
858 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
859 protocol != PPP_LCP) {
860 /* can compress address/control */
864 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
865 /* can compress protocol */
866 if (mtod(m, u_char *) == cp) {
867 cp[2] = cp[1]; /* move address/control up */
882 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
883 * 0 if it is about to be transmitted.
887 struct ppp_softc *sc;
897 * Get a pointer to the data after the PPP header.
899 if (m->m_len <= PPP_HDRLEN) {
903 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
906 dp = mtod(mp, u_char *) + PPP_HDRLEN;
909 ep = mtod(mp, u_char *) + mp->m_len;
910 if (dp + CCP_HDRLEN > ep)
912 slen = CCP_LENGTH(dp);
913 if (dp + slen > ep) {
914 if (sc->sc_flags & SC_DEBUG)
915 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
916 dp, slen, mtod(mp, u_char *), mp->m_len);
920 switch (CCP_CODE(dp)) {
924 /* CCP must be going down - disable compression */
925 if (sc->sc_flags & SC_CCP_UP) {
927 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
933 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
934 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
935 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
937 /* we're agreeing to send compressed packets. */
938 if (sc->sc_xc_state != NULL
939 && (*sc->sc_xcomp->comp_init)
940 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
941 sc->sc_if.if_unit, sc->sc_flags & SC_DEBUG)) {
943 sc->sc_flags |= SC_COMP_RUN;
947 /* peer is agreeing to send compressed packets. */
948 if (sc->sc_rc_state != NULL
949 && (*sc->sc_rcomp->decomp_init)
950 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
951 sc->sc_if.if_unit, sc->sc_mru,
952 sc->sc_flags & SC_DEBUG)) {
954 sc->sc_flags |= SC_DECOMP_RUN;
955 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
963 if (sc->sc_flags & SC_CCP_UP) {
965 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
966 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
968 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
969 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
971 sc->sc_flags &= ~SC_DC_ERROR;
981 * CCP is down; free (de)compressor state if necessary.
985 struct ppp_softc *sc;
987 if (sc->sc_xc_state) {
988 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
989 sc->sc_xc_state = NULL;
991 if (sc->sc_rc_state) {
992 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
993 sc->sc_rc_state = NULL;
996 #endif /* PPP_COMPRESS */
999 * PPP packet input routine.
1000 * The caller has checked and removed the FCS and has inserted
1001 * the address/control bytes and the protocol high byte if they
1002 * were omitted. Should be called at splimp/spltty.
1005 ppppktin(sc, m, lost)
1006 struct ppp_softc *sc;
1010 m->m_context = lost;
1011 IF_ENQUEUE(&sc->sc_rawq, m);
1012 schednetisr(NETISR_PPP);
1016 * Process a received PPP packet, doing decompression as necessary.
1018 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1019 TYPE_UNCOMPRESSED_TCP)
1023 struct ppp_softc *sc;
1026 struct ifqueue *inq, *lock;
1027 int s, ilen, xlen, proto, rv;
1028 u_char *cp, adrs, ctrl;
1029 struct mbuf *mp, *dmp, *pc;
1033 sc->sc_if.if_ipackets++;
1035 if (sc->sc_flags & SC_LOG_INPKT) {
1036 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1040 cp = mtod(m, u_char *);
1041 adrs = PPP_ADDRESS(cp);
1042 ctrl = PPP_CONTROL(cp);
1043 proto = PPP_PROTOCOL(cp);
1047 sc->sc_flags |= SC_VJ_RESET;
1053 * Decompress this packet if necessary, update the receiver's
1054 * dictionary, or take appropriate action on a CCP packet.
1056 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1057 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1058 /* decompress this packet */
1059 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1063 cp = mtod(m, u_char *);
1064 proto = PPP_PROTOCOL(cp);
1067 /* pass the compressed packet up to pppd, which may take
1068 CCP down or issue a Reset-Req. */
1069 if (sc->sc_flags & SC_DEBUG)
1070 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1072 sc->sc_flags |= SC_VJ_RESET;
1075 /* no error, but no decompressed packet produced */
1080 sc->sc_flags |= SC_DC_ERROR;
1082 case DECOMP_FATALERROR:
1083 sc->sc_flags |= SC_DC_FERROR;
1090 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1091 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1093 if (proto == PPP_CCP) {
1100 for (mp = m; mp != NULL; mp = mp->m_next)
1104 if (sc->sc_flags & SC_VJ_RESET) {
1106 * If we've missed a packet, we must toss subsequent compressed
1107 * packets which don't have an explicit connection ID.
1109 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1111 sc->sc_flags &= ~SC_VJ_RESET;
1116 * See if we have a VJ-compressed packet to uncompress.
1118 if (proto == PPP_VJC_COMP) {
1119 if (sc->sc_flags & SC_REJ_COMP_TCP)
1122 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1123 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1124 &sc->sc_comp, &iphdr, &hlen);
1127 if (sc->sc_flags & SC_DEBUG)
1128 printf("ppp%d: VJ uncompress failed on type comp\n",
1133 /* Copy the PPP and IP headers into a new mbuf. */
1134 MGET(mp, M_DONTWAIT, MT_DATA);
1139 if (hlen + PPP_HDRLEN > MLEN) {
1141 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1143 goto bad; /* lose if big headers and no clusters */
1146 cp = mtod(mp, u_char *);
1152 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1153 mp->m_len = hlen + PPP_HDRLEN;
1156 * Trim the PPP and VJ headers off the old mbuf
1157 * and stick the new and old mbufs together.
1159 m->m_off += PPP_HDRLEN + xlen;
1160 m->m_len -= PPP_HDRLEN + xlen;
1161 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1162 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1163 mp->m_len += m->m_len;
1164 MFREE(m, mp->m_next);
1168 ilen += hlen - xlen;
1170 } else if (proto == PPP_VJC_UNCOMP) {
1171 if (sc->sc_flags & SC_REJ_COMP_TCP)
1174 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1175 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1176 &sc->sc_comp, &iphdr, &hlen);
1179 if (sc->sc_flags & SC_DEBUG)
1180 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1191 * If the packet will fit in an ordinary mbuf, don't waste a
1192 * whole cluster on it.
1194 if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1195 MGET(mp, M_DONTWAIT, MT_DATA);
1197 m_copydata(m, mtod(mp, caddr_t), ilen);
1205 /* See if bpf wants to look at the packet. */
1207 bpf_mtap(sc->sc_bpf, m);
1214 * IP packet - take off the ppp header and pass it up to IP.
1216 if ((sc->sc_if.if_flags & IFF_UP) == 0
1217 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1218 /* interface is down - drop the packet. */
1222 m->m_off += PPP_HDRLEN;
1223 m->m_len -= PPP_HDRLEN;
1224 schednetisr(NETISR_IP);
1231 * Some other protocol - place on input queue for read().
1239 * Put the packet on the appropriate input queue.
1243 smp_lock(&lock->lk_ifqueue, LK_RETRY);
1244 if (IF_QFULL(inq)) {
1246 /* XXX should we unlock here? */
1248 if (sc->sc_flags & SC_DEBUG)
1249 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1252 IF_ENQUEUEIF(inq, m, &sc->sc_if);
1253 smp_unlock(&lock->lk_ifqueue);
1263 sc->sc_if.if_ierrors++;
1266 #define MAX_DUMP_BYTES 128
1272 char buf[3*MAX_DUMP_BYTES+4];
1275 static char digits[] = "0123456789abcdef";
1277 for (m = m0; m; m = m->m_next) {
1279 u_char *rptr = mtod(m, u_char *);
1282 if (bp > buf + sizeof(buf) - 4)
1284 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1285 *bp++ = digits[*rptr++ & 0xf];
1289 if (bp > buf + sizeof(buf) - 3)
1299 printf("%s\n", buf);
1302 #endif /* NPPP > 0 */