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.4 1994/12/08 00:32:59 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[] = {
175 #endif /* PPP_COMPRESS */
178 * Called from boot code to establish ppp interfaces.
183 register struct ppp_softc *sc;
186 for (sc = ppp_softc; i < NPPP; sc++) {
187 sc->sc_if.if_name = "ppp";
188 sc->sc_if.if_unit = i++;
189 sc->sc_if.if_mtu = PPP_MTU;
190 sc->sc_if.if_flags = IFF_POINTOPOINT;
191 sc->sc_if.if_type = IFT_PPP;
192 sc->sc_if.if_ioctl = pppsioctl;
193 sc->sc_if.if_output = pppoutput;
194 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
195 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
196 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
197 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
198 if_attach(&sc->sc_if);
200 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
206 * Allocate a ppp interface unit and initialize it.
213 struct ppp_softc *sc;
215 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
216 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;
228 sc->sc_relinq = NULL;
230 sl_compress_init(&sc->sc_comp);
233 sc->sc_xc_state = NULL;
234 sc->sc_rc_state = NULL;
235 #endif /* PPP_COMPRESS */
236 for (i = 0; i < NUM_NP; ++i)
237 sc->sc_npmode[i] = NPMODE_ERROR;
238 sc->sc_npqueue = NULL;
239 sc->sc_npqtail = &sc->sc_npqueue;
245 * Deallocate a ppp unit. Must be called at splnet or higher.
249 struct ppp_softc *sc;
254 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
258 IF_DEQUEUE(&sc->sc_rawq, m);
264 IF_DEQUEUE(&sc->sc_inq, m);
270 IF_DEQUEUE(&sc->sc_fastq, m);
275 while ((m = sc->sc_npqueue) != NULL) {
276 sc->sc_npqueue = m->m_nextpkt;
279 if (sc->sc_togo != NULL) {
280 m_freem(sc->sc_togo);
285 sc->sc_xc_state = NULL;
286 sc->sc_rc_state = NULL;
287 #endif /* PPP_COMPRESS */
291 * Ioctl routine for generic ppp devices.
294 pppioctl(sc, cmd, data, flag)
295 struct ppp_softc *sc;
299 struct proc *p = u.u_procp;
300 int s, error, flags, mru, nb, npx;
301 struct ppp_option_data *odp;
302 struct compressor **cp;
304 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
308 *(int *)data = sc->sc_inq.ifq_len;
312 *(int *)data = sc->sc_if.if_unit;
316 *(u_int *)data = sc->sc_flags;
322 flags = *(int *)data & SC_MASK;
324 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
327 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
335 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
340 *(int *)data = sc->sc_mru;
348 sl_compress_setup(&sc->sc_comp, *(int *)data);
356 sc->sc_xfer = p->p_pid;
360 case PPPIOCSCOMPRESS:
363 odp = (struct ppp_option_data *) data;
365 if (nb > sizeof(ccp_option))
366 nb = sizeof(ccp_option);
367 if (error = copyin(odp->ptr, ccp_option, nb))
369 if (ccp_option[1] < 2) /* preliminary check on the length byte */
371 for (cp = ppp_compressors; *cp != NULL; ++cp)
372 if ((*cp)->compress_proto == ccp_option[0]) {
374 * Found a handler for the protocol - try to allocate
375 * a compressor or decompressor.
380 if (sc->sc_xc_state != NULL)
381 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
383 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
384 if (sc->sc_xc_state == NULL) {
385 if (sc->sc_flags & SC_DEBUG)
386 printf("ppp%d: comp_alloc failed\n",
391 sc->sc_flags &= ~SC_COMP_RUN;
393 if (sc->sc_rc_state != NULL)
394 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
396 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
397 if (sc->sc_rc_state == NULL) {
398 if (sc->sc_flags & SC_DEBUG)
399 printf("ppp%d: decomp_alloc failed\n",
404 sc->sc_flags &= ~SC_DECOMP_RUN;
409 if (sc->sc_flags & SC_DEBUG)
410 printf("ppp%d: no compressor for [%x %x %x], %x\n",
411 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
413 return (EINVAL); /* no handler found */
414 #endif /* PPP_COMPRESS */
418 npi = (struct npioctl *) data;
419 switch (npi->protocol) {
426 if (cmd == PPPIOCGNPMODE) {
427 npi->mode = sc->sc_npmode[npx];
431 if (npi->mode != sc->sc_npmode[npx]) {
433 sc->sc_npmode[npx] = npi->mode;
434 if (npi->mode != NPMODE_QUEUE) {
450 * Process an ioctl request to the ppp network interface.
453 pppsioctl(ifp, cmd, data)
454 register struct ifnet *ifp;
458 struct proc *p = u.u_procp;
459 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
460 register struct ifaddr *ifa = (struct ifaddr *)data;
461 register struct ifreq *ifr = (struct ifreq *)data;
462 struct ppp_stats *psp;
463 struct ppp_comp_stats *pcp;
464 int s = splimp(), error = 0;
468 if ((ifp->if_flags & IFF_RUNNING) == 0)
469 ifp->if_flags &= ~IFF_UP;
473 if (ifa->ifa_addr.sa_family != AF_INET)
474 error = EAFNOSUPPORT;
478 if (ifa->ifa_addr.sa_family != AF_INET)
479 error = EAFNOSUPPORT;
485 sc->sc_if.if_mtu = ifr->ifr_mtu;
489 ifr->ifr_mtu = sc->sc_if.if_mtu;
493 psp = &((struct ifpppstatsreq *) data)->stats;
494 bzero(psp, sizeof(*psp));
495 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
496 psp->p.ppp_ipackets = ifp->if_ipackets;
497 psp->p.ppp_ierrors = ifp->if_ierrors;
498 psp->p.ppp_obytes = sc->sc_bytessent;
499 psp->p.ppp_opackets = ifp->if_opackets;
500 psp->p.ppp_oerrors = ifp->if_oerrors;
502 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
503 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
504 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
505 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
506 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
507 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
508 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
509 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
515 pcp = &((struct ifpppcstatsreq *) data)->stats;
516 bzero(pcp, sizeof(*pcp));
517 if (sc->sc_xc_state != NULL)
518 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
519 if (sc->sc_rc_state != NULL)
520 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
522 #endif /* PPP_COMPRESS */
532 * Queue a packet. Start transmission if not active.
533 * Packet is placed in Information field of PPP frame.
536 pppoutput(ifp, m0, dst)
539 struct sockaddr *dst;
541 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
542 struct ppp_header *ph;
543 int protocol, address, control;
550 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
551 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
552 error = ENETDOWN; /* sort of */
557 * Compute PPP header.
558 * We use the m_context field of the mbuf to indicate whether
559 * the packet should go on the fast queue.
562 switch (dst->sa_family) {
565 address = PPP_ALLSTATIONS;
568 mode = sc->sc_npmode[NP_IP];
571 * If this is a TCP packet to or from an "interactive" port,
572 * put the packet on the fastq instead.
574 if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
575 register int p = ntohl(((int *)ip)[ip->ip_hl]);
576 if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
582 address = PPP_ADDRESS(dst->sa_data);
583 control = PPP_CONTROL(dst->sa_data);
584 protocol = PPP_PROTOCOL(dst->sa_data);
588 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
589 error = EAFNOSUPPORT;
594 * Drop this packet, or return an error, if necessary.
596 if (mode == NPMODE_ERROR) {
600 if (mode == NPMODE_DROP) {
606 * Add PPP header. If no space in first mbuf, allocate another.
608 if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
611 MGET(m, M_DONTWAIT, MT_DATA);
620 m0->m_off -= PPP_HDRLEN;
622 cp = mtod(m0, u_char *);
625 *cp++ = protocol >> 8;
626 *cp++ = protocol & 0xff;
627 m0->m_len += PPP_HDRLEN;
629 if (sc->sc_flags & SC_LOG_OUTPKT) {
630 printf("ppp%d output: ", ifp->if_unit);
636 * See if bpf wants to look at the packet.
639 bpf_mtap(sc->sc_bpf, m0);
643 * Put the packet on the appropriate queue.
645 s = splimp(); /* splnet should be OK now */
646 if (mode == NPMODE_QUEUE) {
647 /* XXX we should limit the number of packets on this queue */
648 *sc->sc_npqtail = m0;
649 m0->m_nextpkt = NULL;
650 sc->sc_npqtail = &m0->m_nextpkt;
652 ifq = m0->m_context? &sc->sc_fastq: &ifp->if_snd;
656 sc->sc_if.if_oerrors++;
673 * After a change in the NPmode for some NP, move packets from the
674 * npqueue to the send queue or the fast queue as appropriate.
675 * Should be called at splimp (actually splnet would probably suffice).
679 struct ppp_softc *sc;
681 struct mbuf *m, **mpp;
685 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
686 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
688 mode = sc->sc_npmode[NP_IP];
697 * This packet can now go on one of the queues to be sent.
701 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
704 sc->sc_if.if_oerrors++;
720 sc->sc_npqtail = mpp;
724 * Get a packet to send. This procedure is intended to be called
725 * at spltty()/splimp(), so it takes little time. If there isn't
726 * a packet waiting to go out, it schedules a software interrupt
727 * to prepare a new packet; the device start routine gets called
728 * again when a packet is ready.
732 struct ppp_softc *sc;
740 * Had a packet waiting - send it.
743 sc->sc_flags |= SC_TBUSY;
748 * Remember we wanted a packet and schedule a software interrupt.
750 sc->sc_flags &= ~SC_TBUSY;
751 schednetisr(NETISR_PPP);
757 * Software interrupt routine, called at splnet().
762 struct ppp_softc *sc;
768 for (i = 0; i < NPPP; ++i, ++sc) {
769 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
770 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
773 IF_DEQUEUE(&sc->sc_rawq, m);
783 * Grab another packet off a queue and apply VJ compression,
784 * packet compression, address/control and/or protocol compression
785 * if enabled. Should be called at splnet.
789 struct ppp_softc *sc;
794 int address, control, protocol;
798 * Grab a packet to send: first try the fast queue, then the
801 IF_DEQUEUE(&sc->sc_fastq, m);
803 IF_DEQUEUE(&sc->sc_if.if_snd, m);
808 * Extract the ppp header of the new packet.
809 * The ppp header will be in one mbuf.
811 cp = mtod(m, u_char *);
812 address = PPP_ADDRESS(cp);
813 control = PPP_CONTROL(cp);
814 protocol = PPP_PROTOCOL(cp);
820 * If the packet is a TCP/IP packet, see if we can compress it.
822 if (sc->sc_flags & SC_COMP_TCP) {
827 ip = (struct ip *) (cp + PPP_HDRLEN);
828 if (mp->m_len <= PPP_HDRLEN) {
832 ip = mtod(mp, struct ip *);
834 /* this code assumes the IP/TCP header is in one non-shared mbuf */
835 if (ip->ip_p == IPPROTO_TCP) {
836 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
837 !(sc->sc_flags & SC_NO_TCP_CCID));
839 case TYPE_UNCOMPRESSED_TCP:
840 protocol = PPP_VJC_UNCOMP;
842 case TYPE_COMPRESSED_TCP:
843 protocol = PPP_VJC_COMP;
844 cp = mtod(m, u_char *);
845 cp[0] = address; /* header has moved */
850 cp[3] = protocol; /* update protocol in PPP header */
860 #endif /* PPP_COMPRESS */
864 if (protocol != PPP_LCP && protocol != PPP_CCP
865 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
870 for (mp = m; mp != NULL; mp = mp->m_next)
872 clen = (*sc->sc_xcomp->compress)
873 (sc->sc_xc_state, &mcomp, m, slen,
874 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
878 cp = mtod(m, u_char *);
882 #endif /* PPP_COMPRESS */
885 * Compress the address/control and protocol, if possible.
887 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
888 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
889 protocol != PPP_LCP) {
890 /* can compress address/control */
894 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
895 /* can compress protocol */
896 if (mtod(m, u_char *) == cp) {
897 cp[2] = cp[1]; /* move address/control up */
912 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
913 * 0 if it is about to be transmitted.
917 struct ppp_softc *sc;
927 * Get a pointer to the data after the PPP header.
929 if (m->m_len <= PPP_HDRLEN) {
933 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
936 dp = mtod(mp, u_char *) + PPP_HDRLEN;
939 ep = mtod(mp, u_char *) + mp->m_len;
940 if (dp + CCP_HDRLEN > ep)
942 slen = CCP_LENGTH(dp);
943 if (dp + slen > ep) {
944 if (sc->sc_flags & SC_DEBUG)
945 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
946 dp, slen, mtod(mp, u_char *), mp->m_len);
950 switch (CCP_CODE(dp)) {
954 /* CCP must be going down - disable compression */
955 if (sc->sc_flags & SC_CCP_UP) {
957 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
963 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
964 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
965 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
967 /* we're agreeing to send compressed packets. */
968 if (sc->sc_xc_state != NULL
969 && (*sc->sc_xcomp->comp_init)
970 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
971 sc->sc_if.if_unit, sc->sc_flags & SC_DEBUG)) {
973 sc->sc_flags |= SC_COMP_RUN;
977 /* peer is agreeing to send compressed packets. */
978 if (sc->sc_rc_state != NULL
979 && (*sc->sc_rcomp->decomp_init)
980 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
981 sc->sc_if.if_unit, 0, sc->sc_mru,
982 sc->sc_flags & SC_DEBUG)) {
984 sc->sc_flags |= SC_DECOMP_RUN;
985 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
993 if (sc->sc_flags & SC_CCP_UP) {
995 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
996 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
998 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
999 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1001 sc->sc_flags &= ~SC_DC_ERROR;
1011 * CCP is down; free (de)compressor state if necessary.
1015 struct ppp_softc *sc;
1017 if (sc->sc_xc_state) {
1018 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1019 sc->sc_xc_state = NULL;
1021 if (sc->sc_rc_state) {
1022 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1023 sc->sc_rc_state = NULL;
1026 #endif /* PPP_COMPRESS */
1029 * PPP packet input routine.
1030 * The caller has checked and removed the FCS and has inserted
1031 * the address/control bytes and the protocol high byte if they
1035 ppppktin(sc, m, lost)
1036 struct ppp_softc *sc;
1042 m->m_context = lost;
1043 IF_ENQUEUE(&sc->sc_rawq, m);
1044 schednetisr(NETISR_PPP);
1049 * Process a received PPP packet, doing decompression as necessary.
1051 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1052 TYPE_UNCOMPRESSED_TCP)
1056 struct ppp_softc *sc;
1059 struct ifqueue *inq;
1060 int s, ilen, xlen, proto, rv;
1061 u_char *cp, adrs, ctrl;
1062 struct mbuf *mp, *dmp, *pc;
1066 sc->sc_if.if_ipackets++;
1068 if (sc->sc_flags & SC_LOG_INPKT) {
1069 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1073 cp = mtod(m, u_char *);
1074 adrs = PPP_ADDRESS(cp);
1075 ctrl = PPP_CONTROL(cp);
1076 proto = PPP_PROTOCOL(cp);
1080 sc->sc_flags |= SC_VJ_RESET;
1086 * Decompress this packet if necessary, update the receiver's
1087 * dictionary, or take appropriate action on a CCP packet.
1089 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1090 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1091 /* decompress this packet */
1092 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1096 cp = mtod(m, u_char *);
1097 proto = PPP_PROTOCOL(cp);
1100 /* pass the compressed packet up to pppd, which may take
1101 CCP down or issue a Reset-Req. */
1102 if (sc->sc_flags & SC_DEBUG)
1103 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1105 sc->sc_flags |= SC_VJ_RESET;
1108 /* no error, but no decompressed packet produced */
1113 sc->sc_flags |= SC_DC_ERROR;
1115 case DECOMP_FATALERROR:
1116 sc->sc_flags |= SC_DC_FERROR;
1123 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1124 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1126 if (proto == PPP_CCP) {
1133 for (mp = m; mp != NULL; mp = mp->m_next)
1137 if (sc->sc_flags & SC_VJ_RESET) {
1139 * If we've missed a packet, we must toss subsequent compressed
1140 * packets which don't have an explicit connection ID.
1142 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1144 sc->sc_flags &= ~SC_VJ_RESET;
1149 * See if we have a VJ-compressed packet to uncompress.
1151 if (proto == PPP_VJC_COMP) {
1152 if (sc->sc_flags & SC_REJ_COMP_TCP)
1155 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1156 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1157 &sc->sc_comp, &iphdr, &hlen);
1160 if (sc->sc_flags & SC_DEBUG)
1161 printf("ppp%d: VJ uncompress failed on type comp\n",
1166 /* Copy the PPP and IP headers into a new mbuf. */
1167 MGET(mp, M_DONTWAIT, MT_DATA);
1172 if (hlen + PPP_HDRLEN > MLEN) {
1174 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1176 goto bad; /* lose if big headers and no clusters */
1179 cp = mtod(mp, u_char *);
1185 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1186 mp->m_len = hlen + PPP_HDRLEN;
1189 * Trim the PPP and VJ headers off the old mbuf
1190 * and stick the new and old mbufs together.
1192 m->m_off += PPP_HDRLEN + xlen;
1193 m->m_len -= PPP_HDRLEN + xlen;
1194 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1195 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1196 mp->m_len += m->m_len;
1197 MFREE(m, mp->m_next);
1201 ilen += hlen - xlen;
1203 } else if (proto == PPP_VJC_UNCOMP) {
1204 if (sc->sc_flags & SC_REJ_COMP_TCP)
1207 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1208 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1209 &sc->sc_comp, &iphdr, &hlen);
1212 if (sc->sc_flags & SC_DEBUG)
1213 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1224 * If the packet will fit in an ordinary mbuf, don't waste a
1225 * whole cluster on it.
1227 if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1228 MGET(mp, M_DONTWAIT, MT_DATA);
1230 m_copydata(m, mtod(mp, caddr_t), ilen);
1238 /* See if bpf wants to look at the packet. */
1240 bpf_mtap(sc->sc_bpf, m);
1248 * IP packet - take off the ppp header and pass it up to IP.
1250 if ((sc->sc_if.if_flags & IFF_UP) == 0
1251 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1252 /* interface is down - drop the packet. */
1256 m->m_off += PPP_HDRLEN;
1257 m->m_len -= PPP_HDRLEN;
1258 schednetisr(NETISR_IP);
1265 * Some other protocol - place on input queue for read().
1273 * Put the packet on the appropriate input queue.
1277 smp_lock(&lock->lk_ifqueue, LK_RETRY);
1278 if (IF_QFULL(inq)) {
1280 /* XXX should we unlock here? */
1282 if (sc->sc_flags & SC_DEBUG)
1283 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1286 IF_ENQUEUEIF(inq, m, &sc->sc_if);
1287 smp_unlock(&lock->lk_ifqueue);
1297 sc->sc_if.if_ierrors++;
1300 #define MAX_DUMP_BYTES 128
1306 char buf[3*MAX_DUMP_BYTES+4];
1309 static char digits[] = "0123456789abcdef";
1311 for (m = m0; m; m = m->m_next) {
1313 u_char *rptr = mtod(m, u_char *);
1316 if (bp > buf + sizeof(buf) - 4)
1318 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1319 *bp++ = digits[*rptr++ & 0xf];
1323 if (bp > buf + sizeof(buf) - 3)
1333 printf("%s\n", buf);
1336 #endif /* NPPP > 0 */