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.14 1998/02/04 01:43:39 paulus Exp $ */
76 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
77 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
85 #include "../h/param.h"
86 #include "../h/user.h"
87 #include "../h/proc.h"
88 #include "../h/mbuf.h"
90 #include "../h/socket.h"
91 #include "../h/ioctl.h"
92 #include "../h/systm.h"
93 #include "../h/time.h"
95 #include "../net/net/if.h"
96 #include "../net/net/netisr.h"
97 #include "../net/net/route.h"
100 #include "../net/netinet/in.h"
101 #include "../net/netinet/in_systm.h"
102 #include "../net/netinet/in_var.h"
103 #include "../net/netinet/ip.h"
107 #include "../machine/mtpr.h"
111 #include "slcompress.h"
114 #include "ppp_defs.h"
116 #include "if_pppvar.h"
119 #define PACKETPTR struct mbuf *
120 #include "ppp-comp.h"
123 static int pppsioctl(struct ifnet *, int, caddr_t);
124 static void ppp_requeue __P((struct ppp_softc *));
125 static void ppp_outpkt __P((struct ppp_softc *));
126 static void 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;
164 extern struct compressor ppp_deflate;
166 struct compressor *ppp_compressors[] = {
175 #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;
230 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
232 KM_ALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
233 KM_DEVBUF, KM_NOARG);
235 sl_compress_init(sc->sc_comp);
238 sc->sc_xc_state = NULL;
239 sc->sc_rc_state = NULL;
240 #endif /* PPP_COMPRESS */
241 for (i = 0; i < NUM_NP; ++i)
242 sc->sc_npmode[i] = NPMODE_ERROR;
243 sc->sc_npqueue = NULL;
244 sc->sc_npqtail = &sc->sc_npqueue;
245 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
251 * Deallocate a ppp unit. Must be called at splnet or higher.
255 struct ppp_softc *sc;
260 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
264 IF_DEQUEUE(&sc->sc_rawq, m);
270 IF_DEQUEUE(&sc->sc_inq, m);
276 IF_DEQUEUE(&sc->sc_fastq, m);
281 while ((m = sc->sc_npqueue) != NULL) {
282 sc->sc_npqueue = m->m_act;
285 if (sc->sc_togo != NULL) {
286 m_freem(sc->sc_togo);
291 sc->sc_xc_state = NULL;
292 sc->sc_rc_state = NULL;
293 #endif /* PPP_COMPRESS */
295 if (sc->sc_comp != 0) {
296 KM_FREE(sc->sc_comp, KM_DEVBUF);
303 * Ioctl routine for generic ppp devices.
306 pppioctl(sc, cmd, data, flag)
307 struct ppp_softc *sc;
311 struct proc *p = u.u_procp;
312 int s, error, flags, mru, mtu, nb, npx;
313 struct ppp_option_data *odp;
314 struct compressor **cp;
318 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
323 *(int *)data = sc->sc_inq.ifq_len;
327 *(int *)data = sc->sc_if.if_unit;
331 *(u_int *)data = sc->sc_flags;
337 flags = *(int *)data & SC_MASK;
340 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
344 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
352 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
357 *(int *)data = sc->sc_mru;
361 * PPPIOC[GS]MTU are implemented here, instead of supporting
362 * SIOC[GS]IFMTU in pppsioctl, because under Ultrix, we can't get an
363 * interface ioctl through to the interface until it has an IP
370 if (mtu <= 0 || mtu > PPP_MAXMRU)
373 sc->sc_if.if_mtu = mtu;
378 *(int *) data = sc->sc_if.if_mtu;
387 sl_compress_setup(sc->sc_comp, *(int *)data);
396 sc->sc_xfer = p->p_pid;
400 case PPPIOCSCOMPRESS:
403 odp = (struct ppp_option_data *) data;
405 if (nb > sizeof(ccp_option))
406 nb = sizeof(ccp_option);
407 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
409 if (ccp_option[1] < 2) /* preliminary check on the length byte */
411 for (cp = ppp_compressors; *cp != NULL; ++cp)
412 if ((*cp)->compress_proto == ccp_option[0]) {
414 * Found a handler for the protocol - try to allocate
415 * a compressor or decompressor.
420 if (sc->sc_xc_state != NULL)
421 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
423 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
424 if (sc->sc_xc_state == NULL) {
425 if (sc->sc_flags & SC_DEBUG)
426 printf("ppp%d: comp_alloc failed\n",
431 sc->sc_flags &= ~SC_COMP_RUN;
435 if (sc->sc_rc_state != NULL)
436 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
438 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
439 if (sc->sc_rc_state == NULL) {
440 if (sc->sc_flags & SC_DEBUG)
441 printf("ppp%d: decomp_alloc failed\n",
446 sc->sc_flags &= ~SC_DECOMP_RUN;
451 if (sc->sc_flags & SC_DEBUG)
452 printf("ppp%d: no compressor for [%x %x %x], %x\n",
453 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
455 return (EINVAL); /* no handler found */
456 #endif /* PPP_COMPRESS */
460 npi = (struct npioctl *) data;
461 switch (npi->protocol) {
468 if (cmd == PPPIOCGNPMODE) {
469 npi->mode = sc->sc_npmode[npx];
473 if (npi->mode != sc->sc_npmode[npx]) {
475 sc->sc_npmode[npx] = npi->mode;
476 if (npi->mode != NPMODE_QUEUE) {
488 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
489 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
500 * Process an ioctl request to the ppp network interface.
503 pppsioctl(ifp, cmd, data)
504 register struct ifnet *ifp;
508 struct proc *p = u.u_procp;
509 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
510 register struct ifaddr *ifa = (struct ifaddr *)data;
511 register struct ifreq *ifr = (struct ifreq *)data;
512 struct ppp_stats *psp;
514 struct ppp_comp_stats *pcp;
516 int s = splimp(), error = 0;
520 if ((ifp->if_flags & IFF_RUNNING) == 0)
521 ifp->if_flags &= ~IFF_UP;
525 if (ifa->ifa_addr.sa_family != AF_INET)
526 error = EAFNOSUPPORT;
530 if (ifa->ifa_addr.sa_family != AF_INET)
531 error = EAFNOSUPPORT;
535 * Ioctls other than the above don't get through until the
536 * interface has its IP addresses set :-(
543 sc->sc_if.if_mtu = ifr->ifr_mtu;
547 ifr->ifr_mtu = sc->sc_if.if_mtu;
552 psp = &((struct ifpppstatsreq *) data)->stats;
553 bzero(psp, sizeof(*psp));
554 psp->p = sc->sc_stats;
555 #if defined(VJC) && !defined(SL_NO_STATS)
557 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
558 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
559 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
560 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
561 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
562 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
563 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
564 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
571 pcp = &((struct ifpppcstatsreq *) data)->stats;
572 bzero(pcp, sizeof(*pcp));
573 if (sc->sc_xc_state != NULL)
574 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
575 if (sc->sc_rc_state != NULL)
576 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
578 #endif /* PPP_COMPRESS */
588 * Queue a packet. Start transmission if not active.
589 * Packet is placed in Information field of PPP frame.
592 pppoutput(ifp, m0, dst)
595 struct sockaddr *dst;
597 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
598 int protocol, address, control;
607 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
608 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
609 error = ENETDOWN; /* sort of */
614 * Compute PPP header.
615 * We use the m_context field of the mbuf to indicate whether
616 * the packet should go on the fast queue.
619 switch (dst->sa_family) {
622 address = PPP_ALLSTATIONS;
625 mode = sc->sc_npmode[NP_IP];
628 * If this is a TCP packet to or from an "interactive" port,
629 * put the packet on the fastq instead.
631 if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
632 register int p = ntohl(((int *)ip)[ip->ip_hl]);
633 if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
639 address = PPP_ADDRESS(dst->sa_data);
640 control = PPP_CONTROL(dst->sa_data);
641 protocol = PPP_PROTOCOL(dst->sa_data);
645 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
646 error = EAFNOSUPPORT;
651 * Drop this packet, or return an error, if necessary.
653 if (mode == NPMODE_ERROR) {
657 if (mode == NPMODE_DROP) {
663 * Add PPP header. If no space in first mbuf, allocate another.
665 if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
668 MGET(m, M_DONTWAIT, MT_DATA);
677 m0->m_off -= PPP_HDRLEN;
679 cp = mtod(m0, u_char *);
682 *cp++ = protocol >> 8;
683 *cp++ = protocol & 0xff;
684 m0->m_len += PPP_HDRLEN;
687 for (m = m0; m != 0; m = m->m_next)
690 if (sc->sc_flags & SC_LOG_OUTPKT) {
691 printf("ppp%d output: ", ifp->if_unit);
695 if ((protocol & 0x8000) == 0) {
697 * Update the time we sent the most recent data packet.
699 sc->sc_last_sent = time.tv_sec;
704 * See if bpf wants to look at the packet.
707 bpf_mtap(sc->sc_bpf, m0);
711 * Put the packet on the appropriate queue.
714 if (mode == NPMODE_QUEUE) {
715 /* XXX we should limit the number of packets on this queue */
716 *sc->sc_npqtail = m0;
718 sc->sc_npqtail = &m0->m_act;
720 ifq = m0->m_context? &sc->sc_fastq: &ifp->if_snd;
721 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
724 sc->sc_if.if_oerrors++;
725 sc->sc_stats.ppp_oerrors++;
743 * After a change in the NPmode for some NP, move packets from the
744 * npqueue to the send queue or the fast queue as appropriate.
745 * Should be called at splnet.
749 struct ppp_softc *sc;
751 struct mbuf *m, **mpp;
755 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
756 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
758 mode = sc->sc_npmode[NP_IP];
767 * This packet can now go on one of the queues to be sent.
771 ifq = m->m_context? &sc->sc_fastq: &sc->sc_if.if_snd;
774 sc->sc_if.if_oerrors++;
775 sc->sc_stats.ppp_oerrors++;
791 sc->sc_npqtail = mpp;
795 * Transmitter has finished outputting some stuff;
796 * remember to call sc->sc_start later at splnet.
800 struct ppp_softc *sc;
804 sc->sc_flags &= ~SC_TBUSY;
805 schednetisr(NETISR_PPP);
810 * Get a packet to send. This procedure is intended to be called at
811 * splnet, since it may involve time-consuming operations such as
812 * applying VJ compression, packet compression, address/control and/or
813 * protocol field compression to the packet.
817 struct ppp_softc *sc;
821 int address, control, protocol;
825 * Grab a packet to send: first try the fast queue, then the
828 IF_DEQUEUE(&sc->sc_fastq, m);
830 IF_DEQUEUE(&sc->sc_if.if_snd, m);
834 ++sc->sc_stats.ppp_opackets;
837 * Extract the ppp header of the new packet.
838 * The ppp header will be in one mbuf.
840 cp = mtod(m, u_char *);
841 address = PPP_ADDRESS(cp);
842 control = PPP_CONTROL(cp);
843 protocol = PPP_PROTOCOL(cp);
849 * If the packet is a TCP/IP packet, see if we can compress it.
851 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
856 ip = (struct ip *) (cp + PPP_HDRLEN);
857 if (mp->m_len <= PPP_HDRLEN) {
861 ip = mtod(mp, struct ip *);
863 /* this code assumes the IP/TCP header is in one non-shared mbuf */
864 if (ip->ip_p == IPPROTO_TCP) {
865 type = sl_compress_tcp(mp, ip, sc->sc_comp,
866 !(sc->sc_flags & SC_NO_TCP_CCID));
868 case TYPE_UNCOMPRESSED_TCP:
869 protocol = PPP_VJC_UNCOMP;
871 case TYPE_COMPRESSED_TCP:
872 protocol = PPP_VJC_COMP;
873 cp = mtod(m, u_char *);
874 cp[0] = address; /* header has moved */
879 cp[3] = protocol; /* update protocol in PPP header */
889 #endif /* PPP_COMPRESS */
893 if (protocol != PPP_LCP && protocol != PPP_CCP
894 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
895 struct mbuf *mcomp = NULL;
899 for (mp = m; mp != NULL; mp = mp->m_next)
901 clen = (*sc->sc_xcomp->compress)
902 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
904 if (sc->sc_flags & SC_CCP_UP) {
905 /* Send the compressed packet instead of the original. */
908 cp = mtod(m, u_char *);
911 /* Can't transmit compressed packets until CCP is up. */
916 #endif /* PPP_COMPRESS */
919 * Compress the address/control and protocol, if possible.
921 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
922 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
923 protocol != PPP_LCP) {
924 /* can compress address/control */
928 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
929 /* can compress protocol */
930 if (mtod(m, u_char *) == cp) {
931 cp[2] = cp[1]; /* move address/control up */
942 * Software interrupt routine, called at splnet.
947 struct ppp_softc *sc;
953 for (i = 0; i < NPPP; ++i, ++sc) {
954 if (!(sc->sc_flags & SC_TBUSY)
955 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
957 sc->sc_flags |= SC_TBUSY;
963 IF_DEQUEUE(&sc->sc_rawq, m);
975 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
976 * 0 if it is about to be transmitted.
980 struct ppp_softc *sc;
989 * Get a pointer to the data after the PPP header.
991 if (m->m_len <= PPP_HDRLEN) {
995 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
998 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1001 ep = mtod(mp, u_char *) + mp->m_len;
1002 if (dp + CCP_HDRLEN > ep)
1004 slen = CCP_LENGTH(dp);
1005 if (dp + slen > ep) {
1006 if (sc->sc_flags & SC_DEBUG)
1007 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
1008 dp, slen, mtod(mp, u_char *), mp->m_len);
1012 switch (CCP_CODE(dp)) {
1016 /* CCP must be going down - disable compression */
1017 if (sc->sc_flags & SC_CCP_UP) {
1019 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1025 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1026 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1027 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1029 /* we're agreeing to send compressed packets. */
1030 if (sc->sc_xc_state != NULL
1031 && (*sc->sc_xcomp->comp_init)
1032 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1033 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1035 sc->sc_flags |= SC_COMP_RUN;
1039 /* peer is agreeing to send compressed packets. */
1040 if (sc->sc_rc_state != NULL
1041 && (*sc->sc_rcomp->decomp_init)
1042 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1043 sc->sc_if.if_unit, 0, sc->sc_mru,
1044 sc->sc_flags & SC_DEBUG)) {
1046 sc->sc_flags |= SC_DECOMP_RUN;
1047 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1055 if (sc->sc_flags & SC_CCP_UP) {
1057 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1058 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1060 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1061 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1063 sc->sc_flags &= ~SC_DC_ERROR;
1073 * CCP is down; free (de)compressor state if necessary.
1077 struct ppp_softc *sc;
1079 if (sc->sc_xc_state) {
1080 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1081 sc->sc_xc_state = NULL;
1083 if (sc->sc_rc_state) {
1084 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1085 sc->sc_rc_state = NULL;
1088 #endif /* PPP_COMPRESS */
1091 * PPP packet input routine.
1092 * The caller has checked and removed the FCS and has inserted
1093 * the address/control bytes and the protocol high byte if they
1097 ppppktin(sc, m, lost)
1098 struct ppp_softc *sc;
1104 m->m_context = lost;
1105 IF_ENQUEUE(&sc->sc_rawq, m);
1106 schednetisr(NETISR_PPP);
1111 * Process a received PPP packet, doing decompression as necessary.
1112 * Should be called at splnet.
1114 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1115 TYPE_UNCOMPRESSED_TCP)
1119 struct ppp_softc *sc;
1122 struct ifnet *ifp = &sc->sc_if;
1123 struct ifqueue *inq, *lock;
1124 int s, ilen, xlen, proto, rv;
1125 u_char *cp, adrs, ctrl;
1126 struct mbuf *mp, *dmp = NULL, *pc;
1130 sc->sc_stats.ppp_ipackets++;
1132 if (sc->sc_flags & SC_LOG_INPKT) {
1134 for (mp = m; mp != NULL; mp = mp->m_next)
1136 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1140 cp = mtod(m, u_char *);
1141 adrs = PPP_ADDRESS(cp);
1142 ctrl = PPP_CONTROL(cp);
1143 proto = PPP_PROTOCOL(cp);
1147 sc->sc_flags |= SC_VJ_RESET;
1153 * Decompress this packet if necessary, update the receiver's
1154 * dictionary, or take appropriate action on a CCP packet.
1156 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1157 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1158 /* decompress this packet */
1159 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1160 if (rv == DECOMP_OK) {
1163 /* no error, but no decompressed packet produced */
1167 cp = mtod(m, u_char *);
1168 proto = PPP_PROTOCOL(cp);
1172 * An error has occurred in decompression.
1173 * Pass the compressed packet up to pppd, which may take
1174 * CCP down or issue a Reset-Req.
1176 if (sc->sc_flags & SC_DEBUG)
1177 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1179 sc->sc_flags |= SC_VJ_RESET;
1180 if (rv == DECOMP_ERROR)
1181 sc->sc_flags |= SC_DC_ERROR;
1183 sc->sc_flags |= SC_DC_FERROR;
1188 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1189 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1191 if (proto == PPP_CCP) {
1198 for (mp = m; mp != NULL; mp = mp->m_next)
1202 if (sc->sc_flags & SC_VJ_RESET) {
1204 * If we've missed a packet, we must toss subsequent compressed
1205 * packets which don't have an explicit connection ID.
1208 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1210 sc->sc_flags &= ~SC_VJ_RESET;
1215 * See if we have a VJ-compressed packet to uncompress.
1217 if (proto == PPP_VJC_COMP) {
1218 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1221 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1222 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1223 sc->sc_comp, &iphdr, &hlen);
1226 if (sc->sc_flags & SC_DEBUG)
1227 printf("ppp%d: VJ uncompress failed on type comp\n",
1232 /* Copy the PPP and IP headers into a new mbuf. */
1233 MGET(mp, M_DONTWAIT, MT_DATA);
1238 if (hlen + PPP_HDRLEN > MLEN) {
1240 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1242 goto bad; /* lose if big headers and no clusters */
1245 cp = mtod(mp, u_char *);
1251 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1252 mp->m_len = hlen + PPP_HDRLEN;
1255 * Trim the PPP and VJ headers off the old mbuf
1256 * and stick the new and old mbufs together.
1258 m->m_off += PPP_HDRLEN + xlen;
1259 m->m_len -= PPP_HDRLEN + xlen;
1260 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1261 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1262 mp->m_len += m->m_len;
1263 MFREE(m, mp->m_next);
1267 ilen += hlen - xlen;
1269 } else if (proto == PPP_VJC_UNCOMP) {
1270 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1273 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1274 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1275 sc->sc_comp, &iphdr, &hlen);
1278 if (sc->sc_flags & SC_DEBUG)
1279 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1290 * If the packet will fit in an ordinary mbuf, don't waste a
1291 * whole cluster on it.
1293 if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1294 MGET(mp, M_DONTWAIT, MT_DATA);
1296 m_copydata(m, mtod(mp, caddr_t), ilen);
1304 * Record the time that we received this packet.
1306 if ((proto & 0x8000) == 0) {
1307 sc->sc_last_recv = time.tv_sec;
1311 /* See if bpf wants to look at the packet. */
1313 bpf_mtap(sc->sc_bpf, m);
1321 * IP packet - take off the ppp header and pass it up to IP.
1323 if ((ifp->if_flags & IFF_UP) == 0
1324 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1325 /* interface is down - drop the packet. */
1329 m->m_off += PPP_HDRLEN;
1330 m->m_len -= PPP_HDRLEN;
1331 schednetisr(NETISR_IP);
1338 * Some other protocol - place on input queue for read().
1346 * Put the packet on the appropriate input queue.
1350 smp_lock(&lock->lk_ifqueue, LK_RETRY);
1351 if (IF_QFULL(inq)) {
1353 smp_unlock(&lock->lk_ifqueue);
1355 if (sc->sc_flags & SC_DEBUG)
1356 printf("ppp%d: input queue full\n", ifp->if_unit);
1359 IF_ENQUEUEIF(inq, m, &sc->sc_if);
1360 smp_unlock(&lock->lk_ifqueue);
1371 sc->sc_if.if_ierrors++;
1372 sc->sc_stats.ppp_ierrors++;
1375 #define MAX_DUMP_BYTES 128
1381 char buf[3*MAX_DUMP_BYTES+4];
1384 static char digits[] = "0123456789abcdef";
1386 for (m = m0; m; m = m->m_next) {
1388 u_char *rptr = mtod(m, u_char *);
1391 if (bp > buf + sizeof(buf) - 4)
1393 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1394 *bp++ = digits[*rptr++ & 0xf];
1398 if (bp > buf + sizeof(buf) - 3)
1408 printf("%s\n", buf);
1411 #endif /* NPPP > 0 */