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.9 1995/10/27 03:59:42 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));
124 int pppoutput __P((struct ifnet *ifp, struct mbuf *m0,
125 struct sockaddr *dst));
126 int pppsioctl __P((struct ifnet *ifp, int cmd, caddr_t data));
127 void pppintr __P((void));
129 static void ppp_requeue __P((struct ppp_softc *));
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[] = {
176 #endif /* PPP_COMPRESS */
179 * Called from boot code to establish ppp interfaces.
184 register struct ppp_softc *sc;
187 for (sc = ppp_softc; i < NPPP; sc++) {
188 sc->sc_if.if_name = "ppp";
189 sc->sc_if.if_unit = i++;
190 sc->sc_if.if_mtu = PPP_MTU;
191 sc->sc_if.if_flags = IFF_POINTOPOINT;
192 sc->sc_if.if_type = IFT_PPP;
193 sc->sc_if.if_ioctl = pppsioctl;
194 sc->sc_if.if_output = pppoutput;
195 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
196 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
197 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
198 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
199 if_attach(&sc->sc_if);
201 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
207 * Allocate a ppp interface unit and initialize it.
214 struct ppp_softc *sc;
216 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
217 if (sc->sc_xfer == pid) {
221 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
222 if (sc->sc_devp == NULL)
228 sc->sc_mru = PPP_MRU;
229 sc->sc_relinq = NULL;
231 sl_compress_init(&sc->sc_comp);
234 sc->sc_xc_state = NULL;
235 sc->sc_rc_state = NULL;
236 #endif /* PPP_COMPRESS */
237 for (i = 0; i < NUM_NP; ++i)
238 sc->sc_npmode[i] = NPMODE_ERROR;
239 sc->sc_npqueue = NULL;
240 sc->sc_npqtail = &sc->sc_npqueue;
241 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
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;
308 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
313 *(int *)data = sc->sc_inq.ifq_len;
317 *(int *)data = sc->sc_if.if_unit;
321 *(u_int *)data = sc->sc_flags;
327 flags = *(int *)data & SC_MASK;
330 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
334 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
342 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
347 *(int *)data = sc->sc_mru;
351 * PPPIOC[GS]MTU are implemented here, instead of supporting
352 * SIOC[GS]IFMTU in pppsioctl, because under Ultrix, we can't get an
353 * interface ioctl through to the interface until it has an IP
360 if (mtu <= 0 || mtu > PPP_MAXMRU)
363 sc->sc_if.if_mtu = mtu;
368 *(int *) data = sc->sc_if.if_mtu;
376 sl_compress_setup(&sc->sc_comp, *(int *)data);
384 sc->sc_xfer = p->p_pid;
388 case PPPIOCSCOMPRESS:
391 odp = (struct ppp_option_data *) data;
393 if (nb > sizeof(ccp_option))
394 nb = sizeof(ccp_option);
395 if (error = copyin(odp->ptr, ccp_option, nb))
397 if (ccp_option[1] < 2) /* preliminary check on the length byte */
399 for (cp = ppp_compressors; *cp != NULL; ++cp)
400 if ((*cp)->compress_proto == ccp_option[0]) {
402 * Found a handler for the protocol - try to allocate
403 * a compressor or decompressor.
408 if (sc->sc_xc_state != NULL)
409 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
411 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
412 if (sc->sc_xc_state == NULL) {
413 if (sc->sc_flags & SC_DEBUG)
414 printf("ppp%d: comp_alloc failed\n",
419 sc->sc_flags &= ~SC_COMP_RUN;
423 if (sc->sc_rc_state != NULL)
424 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
426 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
427 if (sc->sc_rc_state == NULL) {
428 if (sc->sc_flags & SC_DEBUG)
429 printf("ppp%d: decomp_alloc failed\n",
434 sc->sc_flags &= ~SC_DECOMP_RUN;
439 if (sc->sc_flags & SC_DEBUG)
440 printf("ppp%d: no compressor for [%x %x %x], %x\n",
441 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
443 return (EINVAL); /* no handler found */
444 #endif /* PPP_COMPRESS */
448 npi = (struct npioctl *) data;
449 switch (npi->protocol) {
456 if (cmd == PPPIOCGNPMODE) {
457 npi->mode = sc->sc_npmode[npx];
461 if (npi->mode != sc->sc_npmode[npx]) {
463 sc->sc_npmode[npx] = npi->mode;
464 if (npi->mode != NPMODE_QUEUE) {
476 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
477 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
488 * Process an ioctl request to the ppp network interface.
491 pppsioctl(ifp, cmd, data)
492 register struct ifnet *ifp;
496 struct proc *p = u.u_procp;
497 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
498 register struct ifaddr *ifa = (struct ifaddr *)data;
499 register struct ifreq *ifr = (struct ifreq *)data;
500 struct ppp_stats *psp;
502 struct ppp_comp_stats *pcp;
504 int s = splimp(), error = 0;
508 if ((ifp->if_flags & IFF_RUNNING) == 0)
509 ifp->if_flags &= ~IFF_UP;
513 if (ifa->ifa_addr.sa_family != AF_INET)
514 error = EAFNOSUPPORT;
518 if (ifa->ifa_addr.sa_family != AF_INET)
519 error = EAFNOSUPPORT;
523 * Ioctls other than the above don't get through until the
524 * interface has its IP addresses set :-(
531 sc->sc_if.if_mtu = ifr->ifr_mtu;
535 ifr->ifr_mtu = sc->sc_if.if_mtu;
540 psp = &((struct ifpppstatsreq *) data)->stats;
541 bzero(psp, sizeof(*psp));
542 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
543 psp->p.ppp_ipackets = ifp->if_ipackets;
544 psp->p.ppp_ierrors = ifp->if_ierrors;
545 psp->p.ppp_obytes = sc->sc_bytessent;
546 psp->p.ppp_opackets = ifp->if_opackets;
547 psp->p.ppp_oerrors = ifp->if_oerrors;
549 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
550 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
551 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
552 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
553 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
554 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
555 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
556 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
562 pcp = &((struct ifpppcstatsreq *) data)->stats;
563 bzero(pcp, sizeof(*pcp));
564 if (sc->sc_xc_state != NULL)
565 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
566 if (sc->sc_rc_state != NULL)
567 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
569 #endif /* PPP_COMPRESS */
579 * Queue a packet. Start transmission if not active.
580 * Packet is placed in Information field of PPP frame.
583 pppoutput(ifp, m0, dst)
586 struct sockaddr *dst;
588 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
589 struct ppp_header *ph;
590 int protocol, address, control;
597 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
598 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
599 error = ENETDOWN; /* sort of */
604 * Compute PPP header.
605 * We use the m_context field of the mbuf to indicate whether
606 * the packet should go on the fast queue.
609 switch (dst->sa_family) {
612 address = PPP_ALLSTATIONS;
615 mode = sc->sc_npmode[NP_IP];
618 * If this is a TCP packet to or from an "interactive" port,
619 * put the packet on the fastq instead.
621 if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
622 register int p = ntohl(((int *)ip)[ip->ip_hl]);
623 if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
629 address = PPP_ADDRESS(dst->sa_data);
630 control = PPP_CONTROL(dst->sa_data);
631 protocol = PPP_PROTOCOL(dst->sa_data);
635 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
636 error = EAFNOSUPPORT;
641 * Drop this packet, or return an error, if necessary.
643 if (mode == NPMODE_ERROR) {
647 if (mode == NPMODE_DROP) {
653 * Add PPP header. If no space in first mbuf, allocate another.
655 if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
658 MGET(m, M_DONTWAIT, MT_DATA);
667 m0->m_off -= PPP_HDRLEN;
669 cp = mtod(m0, u_char *);
672 *cp++ = protocol >> 8;
673 *cp++ = protocol & 0xff;
674 m0->m_len += PPP_HDRLEN;
676 if (sc->sc_flags & SC_LOG_OUTPKT) {
677 printf("ppp%d output: ", ifp->if_unit);
683 * See if bpf wants to look at the packet.
686 bpf_mtap(sc->sc_bpf, m0);
690 * Put the packet on the appropriate queue.
693 if (mode == NPMODE_QUEUE) {
694 /* XXX we should limit the number of packets on this queue */
695 *sc->sc_npqtail = m0;
697 sc->sc_npqtail = &m0->m_act;
699 ifq = m0->m_context? &sc->sc_fastq: &ifp->if_snd;
700 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
703 sc->sc_if.if_oerrors++;
720 * After a change in the NPmode for some NP, move packets from the
721 * npqueue to the send queue or the fast queue as appropriate.
722 * Should be called at splnet.
726 struct ppp_softc *sc;
728 struct mbuf *m, **mpp;
732 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
733 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
735 mode = sc->sc_npmode[NP_IP];
744 * This packet can now go on one of the queues to be sent.
748 ifq = m->m_context? &sc->sc_fastq: &sc->sc_if.if_snd;
751 sc->sc_if.if_oerrors++;
767 sc->sc_npqtail = mpp;
771 * Get a packet to send. This procedure is intended to be called at
772 * spltty or splimp, so it takes little time. If there isn't a packet
773 * waiting to go out, it schedules a software interrupt to prepare a
774 * new packet; the device start routine gets called again when a
779 struct ppp_softc *sc;
787 * Had a packet waiting - send it.
790 sc->sc_flags |= SC_TBUSY;
795 * Remember we wanted a packet and schedule a software interrupt.
797 sc->sc_flags &= ~SC_TBUSY;
798 schednetisr(NETISR_PPP);
804 * Software interrupt routine, called at splnet.
809 struct ppp_softc *sc;
815 for (i = 0; i < NPPP; ++i, ++sc) {
816 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
817 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
821 IF_DEQUEUE(&sc->sc_rawq, m);
832 * Grab another packet off a queue and apply VJ compression,
833 * packet compression, address/control and/or protocol compression
834 * if enabled. Should be called at splnet.
838 struct ppp_softc *sc;
843 int address, control, protocol;
847 * Grab a packet to send: first try the fast queue, then the
850 IF_DEQUEUE(&sc->sc_fastq, m);
852 IF_DEQUEUE(&sc->sc_if.if_snd, m);
857 * Extract the ppp header of the new packet.
858 * The ppp header will be in one mbuf.
860 cp = mtod(m, u_char *);
861 address = PPP_ADDRESS(cp);
862 control = PPP_CONTROL(cp);
863 protocol = PPP_PROTOCOL(cp);
868 * Update the time we sent the most recent packet.
870 sc->sc_last_sent = time.tv_sec;
874 * If the packet is a TCP/IP packet, see if we can compress it.
876 if (sc->sc_flags & SC_COMP_TCP) {
881 ip = (struct ip *) (cp + PPP_HDRLEN);
882 if (mp->m_len <= PPP_HDRLEN) {
886 ip = mtod(mp, struct ip *);
888 /* this code assumes the IP/TCP header is in one non-shared mbuf */
889 if (ip->ip_p == IPPROTO_TCP) {
890 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
891 !(sc->sc_flags & SC_NO_TCP_CCID));
893 case TYPE_UNCOMPRESSED_TCP:
894 protocol = PPP_VJC_UNCOMP;
896 case TYPE_COMPRESSED_TCP:
897 protocol = PPP_VJC_COMP;
898 cp = mtod(m, u_char *);
899 cp[0] = address; /* header has moved */
904 cp[3] = protocol; /* update protocol in PPP header */
914 #endif /* PPP_COMPRESS */
918 if (protocol != PPP_LCP && protocol != PPP_CCP
919 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
924 for (mp = m; mp != NULL; mp = mp->m_next)
926 clen = (*sc->sc_xcomp->compress)
927 (sc->sc_xc_state, &mcomp, m, slen,
928 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
932 cp = mtod(m, u_char *);
936 #endif /* PPP_COMPRESS */
939 * Compress the address/control and protocol, if possible.
941 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
942 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
943 protocol != PPP_LCP) {
944 /* can compress address/control */
948 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
949 /* can compress protocol */
950 if (mtod(m, u_char *) == cp) {
951 cp[2] = cp[1]; /* move address/control up */
964 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
965 * 0 if it is about to be transmitted.
969 struct ppp_softc *sc;
978 * Get a pointer to the data after the PPP header.
980 if (m->m_len <= PPP_HDRLEN) {
984 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
987 dp = mtod(mp, u_char *) + PPP_HDRLEN;
990 ep = mtod(mp, u_char *) + mp->m_len;
991 if (dp + CCP_HDRLEN > ep)
993 slen = CCP_LENGTH(dp);
994 if (dp + slen > ep) {
995 if (sc->sc_flags & SC_DEBUG)
996 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
997 dp, slen, mtod(mp, u_char *), mp->m_len);
1001 switch (CCP_CODE(dp)) {
1005 /* CCP must be going down - disable compression */
1006 if (sc->sc_flags & SC_CCP_UP) {
1008 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1014 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1015 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1016 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1018 /* we're agreeing to send compressed packets. */
1019 if (sc->sc_xc_state != NULL
1020 && (*sc->sc_xcomp->comp_init)
1021 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1022 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1024 sc->sc_flags |= SC_COMP_RUN;
1028 /* peer is agreeing to send compressed packets. */
1029 if (sc->sc_rc_state != NULL
1030 && (*sc->sc_rcomp->decomp_init)
1031 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1032 sc->sc_if.if_unit, 0, sc->sc_mru,
1033 sc->sc_flags & SC_DEBUG)) {
1035 sc->sc_flags |= SC_DECOMP_RUN;
1036 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1044 if (sc->sc_flags & SC_CCP_UP) {
1046 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1047 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1049 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1050 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1052 sc->sc_flags &= ~SC_DC_ERROR;
1062 * CCP is down; free (de)compressor state if necessary.
1066 struct ppp_softc *sc;
1068 if (sc->sc_xc_state) {
1069 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1070 sc->sc_xc_state = NULL;
1072 if (sc->sc_rc_state) {
1073 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1074 sc->sc_rc_state = NULL;
1077 #endif /* PPP_COMPRESS */
1080 * PPP packet input routine.
1081 * The caller has checked and removed the FCS and has inserted
1082 * the address/control bytes and the protocol high byte if they
1086 ppppktin(sc, m, lost)
1087 struct ppp_softc *sc;
1093 m->m_context = lost;
1094 IF_ENQUEUE(&sc->sc_rawq, m);
1095 schednetisr(NETISR_PPP);
1100 * Process a received PPP packet, doing decompression as necessary.
1101 * Should be called at splnet.
1103 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1104 TYPE_UNCOMPRESSED_TCP)
1108 struct ppp_softc *sc;
1111 struct ifqueue *inq, *lock;
1112 int s, ilen, xlen, proto, rv;
1113 u_char *cp, adrs, ctrl;
1114 struct mbuf *mp, *dmp, *pc;
1118 sc->sc_if.if_ipackets++;
1120 if (sc->sc_flags & SC_LOG_INPKT) {
1122 for (mp = m; mp != NULL; mp = mp->m_next)
1124 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1128 cp = mtod(m, u_char *);
1129 adrs = PPP_ADDRESS(cp);
1130 ctrl = PPP_CONTROL(cp);
1131 proto = PPP_PROTOCOL(cp);
1135 sc->sc_flags |= SC_VJ_RESET;
1141 * Decompress this packet if necessary, update the receiver's
1142 * dictionary, or take appropriate action on a CCP packet.
1144 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1145 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1146 /* decompress this packet */
1147 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1148 if (rv == DECOMP_OK) {
1151 /* no error, but no decompressed packet produced */
1155 cp = mtod(m, u_char *);
1156 proto = PPP_PROTOCOL(cp);
1160 * An error has occurred in decompression.
1161 * Pass the compressed packet up to pppd, which may take
1162 * CCP down or issue a Reset-Req.
1164 if (sc->sc_flags & SC_DEBUG)
1165 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1167 sc->sc_flags |= SC_VJ_RESET;
1168 if (rv == DECOMP_ERROR)
1169 sc->sc_flags |= SC_DC_ERROR;
1171 sc->sc_flags |= SC_DC_FERROR;
1176 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1177 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1179 if (proto == PPP_CCP) {
1186 for (mp = m; mp != NULL; mp = mp->m_next)
1190 if (sc->sc_flags & SC_VJ_RESET) {
1192 * If we've missed a packet, we must toss subsequent compressed
1193 * packets which don't have an explicit connection ID.
1195 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1197 sc->sc_flags &= ~SC_VJ_RESET;
1202 * See if we have a VJ-compressed packet to uncompress.
1204 if (proto == PPP_VJC_COMP) {
1205 if (sc->sc_flags & SC_REJ_COMP_TCP)
1208 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1209 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1210 &sc->sc_comp, &iphdr, &hlen);
1213 if (sc->sc_flags & SC_DEBUG)
1214 printf("ppp%d: VJ uncompress failed on type comp\n",
1219 /* Copy the PPP and IP headers into a new mbuf. */
1220 MGET(mp, M_DONTWAIT, MT_DATA);
1225 if (hlen + PPP_HDRLEN > MLEN) {
1227 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1229 goto bad; /* lose if big headers and no clusters */
1232 cp = mtod(mp, u_char *);
1238 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1239 mp->m_len = hlen + PPP_HDRLEN;
1242 * Trim the PPP and VJ headers off the old mbuf
1243 * and stick the new and old mbufs together.
1245 m->m_off += PPP_HDRLEN + xlen;
1246 m->m_len -= PPP_HDRLEN + xlen;
1247 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1248 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1249 mp->m_len += m->m_len;
1250 MFREE(m, mp->m_next);
1254 ilen += hlen - xlen;
1256 } else if (proto == PPP_VJC_UNCOMP) {
1257 if (sc->sc_flags & SC_REJ_COMP_TCP)
1260 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1261 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1262 &sc->sc_comp, &iphdr, &hlen);
1265 if (sc->sc_flags & SC_DEBUG)
1266 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1277 * If the packet will fit in an ordinary mbuf, don't waste a
1278 * whole cluster on it.
1280 if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1281 MGET(mp, M_DONTWAIT, MT_DATA);
1283 m_copydata(m, mtod(mp, caddr_t), ilen);
1291 /* See if bpf wants to look at the packet. */
1293 bpf_mtap(sc->sc_bpf, m);
1301 * IP packet - take off the ppp header and pass it up to IP.
1303 if ((sc->sc_if.if_flags & IFF_UP) == 0
1304 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1305 /* interface is down - drop the packet. */
1309 m->m_off += PPP_HDRLEN;
1310 m->m_len -= PPP_HDRLEN;
1311 schednetisr(NETISR_IP);
1313 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1319 * Some other protocol - place on input queue for read().
1327 * Put the packet on the appropriate input queue.
1331 smp_lock(&lock->lk_ifqueue, LK_RETRY);
1332 if (IF_QFULL(inq)) {
1334 smp_unlock(&lock->lk_ifqueue);
1336 if (sc->sc_flags & SC_DEBUG)
1337 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1340 IF_ENQUEUEIF(inq, m, &sc->sc_if);
1341 smp_unlock(&lock->lk_ifqueue);
1351 sc->sc_if.if_ierrors++;
1354 #define MAX_DUMP_BYTES 128
1360 char buf[3*MAX_DUMP_BYTES+4];
1363 static char digits[] = "0123456789abcdef";
1365 for (m = m0; m; m = m->m_next) {
1367 u_char *rptr = mtod(m, u_char *);
1370 if (bp > buf + sizeof(buf) - 4)
1372 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1373 *bp++ = digits[*rptr++ & 0xf];
1377 if (bp > buf + sizeof(buf) - 3)
1387 printf("%s\n", buf);
1390 #endif /* NPPP > 0 */