2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name "Carnegie Mellon University" must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission. For permission or any legal
21 * details, please contact
22 * Office of Technology Transfer
23 * Carnegie Mellon University
25 * Pittsburgh, PA 15213-3890
26 * (412) 268-4387, fax: (412) 268-7395
27 * tech-transfer@andrew.cmu.edu
29 * 4. Redistributions of any form whatsoever must retain the following
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 * Carnegie Mellon University
45 * Pittsburgh, PA 15213
50 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
52 * Copyright (c) 1987 Regents of the University of California.
53 * All rights reserved.
55 * Redistribution and use in source and binary forms are permitted
56 * provided that the above copyright notice and this paragraph are
57 * duplicated in all such forms and that any documentation,
58 * advertising materials, and other materials related to such
59 * distribution and use acknowledge that the software was developed
60 * by the University of California, Berkeley. The name of the
61 * University may not be used to endorse or promote products derived
62 * from this software without specific prior written permission.
63 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
64 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
65 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
67 * Serial Line interface
70 * Center for Seismic Studies
71 * 1300 N 17th Street, Suite 1450
72 * Arlington, Virginia 22209
77 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
78 * Converted to 4.3BSD Beta by Chris Torek.
79 * Other changes made at Berkeley, based in part on code by Kirk Smith.
81 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
82 * Added VJ tcp header compression; more unified ioctls
84 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
85 * Cleaned up a lot of the mbuf-related code to fix bugs that
86 * caused system crashes and packet corruption. Changed pppstart
87 * so that it doesn't just give up with a collision if the whole
88 * packet doesn't fit in the output ring buffer.
90 * Added priority queueing for interactive IP packets, following
91 * the model of if_sl.c, plus hooks for bpf.
92 * Paul Mackerras (paulus@cs.anu.edu.au).
94 * Ultrix port by Per Sundstrom <sundstrom@stkhlm.enet.dec.com>,
95 * Robert Olsson <robert@robur.slu.se> and Paul Mackerras.
98 /* $Id: if_ppp.c,v 1.16 2002/12/06 12:03:44 paulus Exp $ */
99 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
100 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
108 #include "../h/param.h"
109 #include "../h/user.h"
110 #include "../h/proc.h"
111 #include "../h/mbuf.h"
112 #include "../h/buf.h"
113 #include "../h/socket.h"
114 #include "../h/ioctl.h"
115 #include "../h/systm.h"
116 #include "../h/time.h"
118 #include "../net/net/if.h"
119 #include "../net/net/netisr.h"
120 #include "../net/net/route.h"
123 #include "../net/netinet/in.h"
124 #include "../net/netinet/in_systm.h"
125 #include "../net/netinet/in_var.h"
126 #include "../net/netinet/ip.h"
130 #include "../machine/mtpr.h"
134 #include "slcompress.h"
137 #include "ppp_defs.h"
139 #include "if_pppvar.h"
142 #define PACKETPTR struct mbuf *
143 #include "ppp-comp.h"
146 static int pppsioctl(struct ifnet *, int, caddr_t);
147 static void ppp_requeue __P((struct ppp_softc *));
148 static void ppp_outpkt __P((struct ppp_softc *));
149 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
150 static void ppp_ccp_closed __P((struct ppp_softc *));
151 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
152 static void pppdumpm __P((struct mbuf *m0));
155 * Some useful mbuf macros not in mbuf.h.
157 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
159 #define M_TRAILINGSPACE(m) \
160 ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
161 - ((m)->m_off + (m)->m_len))
163 #define M_OFFSTART(m) \
164 (M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr : MMINOFF)
166 #define M_DATASIZE(m) \
167 (M_IS_CLUSTER(m) ? M_CLUSTERSZ : MLEN)
170 * The following disgusting hack gets around the problem that IP TOS
171 * can't be set yet. We want to put "interactive" traffic on a high
172 * priority queue. To decide if traffic is interactive, we check that
173 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
175 static u_short interactive_ports[8] = {
179 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
183 * List of compressors we know about.
186 extern struct compressor ppp_bsd_compress;
187 extern struct compressor ppp_deflate;
188 extern struct compressor ppp_deflate_draft;
190 struct compressor *ppp_compressors[] = {
200 #endif /* PPP_COMPRESS */
204 * Called from boot code to establish ppp interfaces.
209 register struct ppp_softc *sc;
212 for (sc = ppp_softc; i < NPPP; sc++) {
213 sc->sc_if.if_name = "ppp";
214 sc->sc_if.if_unit = i++;
215 sc->sc_if.if_mtu = PPP_MTU;
216 sc->sc_if.if_flags = IFF_POINTOPOINT;
217 sc->sc_if.if_type = IFT_PPP;
218 sc->sc_if.if_ioctl = pppsioctl;
219 sc->sc_if.if_output = pppoutput;
220 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
221 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
222 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
223 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
224 if_attach(&sc->sc_if);
226 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
232 * Allocate a ppp interface unit and initialize it.
239 struct ppp_softc *sc;
241 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
242 if (sc->sc_xfer == pid) {
246 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
247 if (sc->sc_devp == NULL)
253 sc->sc_mru = PPP_MRU;
254 sc->sc_relinq = NULL;
255 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
257 KM_ALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
258 KM_DEVBUF, KM_NOARG);
260 sl_compress_init(sc->sc_comp);
263 sc->sc_xc_state = NULL;
264 sc->sc_rc_state = NULL;
265 #endif /* PPP_COMPRESS */
266 for (i = 0; i < NUM_NP; ++i)
267 sc->sc_npmode[i] = NPMODE_ERROR;
268 sc->sc_npqueue = NULL;
269 sc->sc_npqtail = &sc->sc_npqueue;
270 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
276 * Deallocate a ppp unit. Must be called at splnet or higher.
280 struct ppp_softc *sc;
285 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
289 IF_DEQUEUE(&sc->sc_rawq, m);
295 IF_DEQUEUE(&sc->sc_inq, m);
301 IF_DEQUEUE(&sc->sc_fastq, m);
306 while ((m = sc->sc_npqueue) != NULL) {
307 sc->sc_npqueue = m->m_act;
310 if (sc->sc_togo != NULL) {
311 m_freem(sc->sc_togo);
316 sc->sc_xc_state = NULL;
317 sc->sc_rc_state = NULL;
318 #endif /* PPP_COMPRESS */
320 if (sc->sc_comp != 0) {
321 KM_FREE(sc->sc_comp, KM_DEVBUF);
328 * Ioctl routine for generic ppp devices.
331 pppioctl(sc, cmd, data, flag)
332 struct ppp_softc *sc;
336 struct proc *p = u.u_procp;
337 int s, error, flags, mru, mtu, nb, npx;
338 struct ppp_option_data *odp;
339 struct compressor **cp;
343 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
348 *(int *)data = sc->sc_inq.ifq_len;
352 *(int *)data = sc->sc_if.if_unit;
356 *(u_int *)data = sc->sc_flags;
362 flags = *(int *)data & SC_MASK;
365 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
369 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
377 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
382 *(int *)data = sc->sc_mru;
386 * PPPIOC[GS]MTU are implemented here, instead of supporting
387 * SIOC[GS]IFMTU in pppsioctl, because under Ultrix, we can't get an
388 * interface ioctl through to the interface until it has an IP
395 if (mtu <= 0 || mtu > PPP_MAXMRU)
398 sc->sc_if.if_mtu = mtu;
403 *(int *) data = sc->sc_if.if_mtu;
412 sl_compress_setup(sc->sc_comp, *(int *)data);
421 sc->sc_xfer = p->p_pid;
425 case PPPIOCSCOMPRESS:
428 odp = (struct ppp_option_data *) data;
430 if (nb > sizeof(ccp_option))
431 nb = sizeof(ccp_option);
432 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
434 if (ccp_option[1] < 2) /* preliminary check on the length byte */
436 for (cp = ppp_compressors; *cp != NULL; ++cp)
437 if ((*cp)->compress_proto == ccp_option[0]) {
439 * Found a handler for the protocol - try to allocate
440 * a compressor or decompressor.
445 if (sc->sc_xc_state != NULL)
446 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
448 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
449 if (sc->sc_xc_state == NULL) {
450 if (sc->sc_flags & SC_DEBUG)
451 printf("ppp%d: comp_alloc failed\n",
456 sc->sc_flags &= ~SC_COMP_RUN;
460 if (sc->sc_rc_state != NULL)
461 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
463 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
464 if (sc->sc_rc_state == NULL) {
465 if (sc->sc_flags & SC_DEBUG)
466 printf("ppp%d: decomp_alloc failed\n",
471 sc->sc_flags &= ~SC_DECOMP_RUN;
476 if (sc->sc_flags & SC_DEBUG)
477 printf("ppp%d: no compressor for [%x %x %x], %x\n",
478 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
480 return (EINVAL); /* no handler found */
481 #endif /* PPP_COMPRESS */
485 npi = (struct npioctl *) data;
486 switch (npi->protocol) {
493 if (cmd == PPPIOCGNPMODE) {
494 npi->mode = sc->sc_npmode[npx];
498 if (npi->mode != sc->sc_npmode[npx]) {
500 sc->sc_npmode[npx] = npi->mode;
501 if (npi->mode != NPMODE_QUEUE) {
513 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
514 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
525 * Process an ioctl request to the ppp network interface.
528 pppsioctl(ifp, cmd, data)
529 register struct ifnet *ifp;
533 struct proc *p = u.u_procp;
534 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
535 register struct ifaddr *ifa = (struct ifaddr *)data;
536 register struct ifreq *ifr = (struct ifreq *)data;
537 struct ppp_stats *psp;
539 struct ppp_comp_stats *pcp;
541 int s = splimp(), error = 0;
545 if ((ifp->if_flags & IFF_RUNNING) == 0)
546 ifp->if_flags &= ~IFF_UP;
550 if (ifa->ifa_addr.sa_family != AF_INET)
551 error = EAFNOSUPPORT;
555 if (ifa->ifa_addr.sa_family != AF_INET)
556 error = EAFNOSUPPORT;
560 * Ioctls other than the above don't get through until the
561 * interface has its IP addresses set :-(
568 sc->sc_if.if_mtu = ifr->ifr_mtu;
572 ifr->ifr_mtu = sc->sc_if.if_mtu;
577 psp = &((struct ifpppstatsreq *) data)->stats;
578 bzero(psp, sizeof(*psp));
579 psp->p = sc->sc_stats;
580 #if defined(VJC) && !defined(SL_NO_STATS)
582 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
583 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
584 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
585 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
586 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
587 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
588 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
589 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
596 pcp = &((struct ifpppcstatsreq *) data)->stats;
597 bzero(pcp, sizeof(*pcp));
598 if (sc->sc_xc_state != NULL)
599 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
600 if (sc->sc_rc_state != NULL)
601 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
603 #endif /* PPP_COMPRESS */
613 * Queue a packet. Start transmission if not active.
614 * Packet is placed in Information field of PPP frame.
617 pppoutput(ifp, m0, dst)
620 struct sockaddr *dst;
622 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
623 int protocol, address, control;
632 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
633 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
634 error = ENETDOWN; /* sort of */
639 * Compute PPP header.
640 * We use the m_context field of the mbuf to indicate whether
641 * the packet should go on the fast queue.
644 switch (dst->sa_family) {
647 address = PPP_ALLSTATIONS;
650 mode = sc->sc_npmode[NP_IP];
653 * If this is a TCP packet to or from an "interactive" port,
654 * put the packet on the fastq instead.
656 if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
657 register int p = ntohl(((int *)ip)[ip->ip_hl]);
658 if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
664 address = PPP_ADDRESS(dst->sa_data);
665 control = PPP_CONTROL(dst->sa_data);
666 protocol = PPP_PROTOCOL(dst->sa_data);
670 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
671 error = EAFNOSUPPORT;
676 * Drop this packet, or return an error, if necessary.
678 if (mode == NPMODE_ERROR) {
682 if (mode == NPMODE_DROP) {
688 * Add PPP header. If no space in first mbuf, allocate another.
690 if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
693 MGET(m, M_DONTWAIT, MT_DATA);
702 m0->m_off -= PPP_HDRLEN;
704 cp = mtod(m0, u_char *);
707 *cp++ = protocol >> 8;
708 *cp++ = protocol & 0xff;
709 m0->m_len += PPP_HDRLEN;
712 for (m = m0; m != 0; m = m->m_next)
715 if (sc->sc_flags & SC_LOG_OUTPKT) {
716 printf("ppp%d output: ", ifp->if_unit);
720 if ((protocol & 0x8000) == 0) {
722 * Update the time we sent the most recent data packet.
724 sc->sc_last_sent = time.tv_sec;
729 * See if bpf wants to look at the packet.
732 bpf_mtap(sc->sc_bpf, m0);
736 * Put the packet on the appropriate queue.
739 if (mode == NPMODE_QUEUE) {
740 /* XXX we should limit the number of packets on this queue */
741 *sc->sc_npqtail = m0;
743 sc->sc_npqtail = &m0->m_act;
745 ifq = m0->m_context? &sc->sc_fastq: &ifp->if_snd;
746 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
749 sc->sc_if.if_oerrors++;
750 sc->sc_stats.ppp_oerrors++;
768 * After a change in the NPmode for some NP, move packets from the
769 * npqueue to the send queue or the fast queue as appropriate.
770 * Should be called at splnet.
774 struct ppp_softc *sc;
776 struct mbuf *m, **mpp;
780 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
781 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
783 mode = sc->sc_npmode[NP_IP];
792 * This packet can now go on one of the queues to be sent.
796 ifq = m->m_context? &sc->sc_fastq: &sc->sc_if.if_snd;
799 sc->sc_if.if_oerrors++;
800 sc->sc_stats.ppp_oerrors++;
816 sc->sc_npqtail = mpp;
820 * Transmitter has finished outputting some stuff;
821 * remember to call sc->sc_start later at splnet.
825 struct ppp_softc *sc;
829 sc->sc_flags &= ~SC_TBUSY;
830 schednetisr(NETISR_PPP);
835 * Get a packet to send. This procedure is intended to be called at
836 * splnet, since it may involve time-consuming operations such as
837 * applying VJ compression, packet compression, address/control and/or
838 * protocol field compression to the packet.
842 struct ppp_softc *sc;
846 int address, control, protocol;
850 * Grab a packet to send: first try the fast queue, then the
853 IF_DEQUEUE(&sc->sc_fastq, m);
855 IF_DEQUEUE(&sc->sc_if.if_snd, m);
859 ++sc->sc_stats.ppp_opackets;
862 * Extract the ppp header of the new packet.
863 * The ppp header will be in one mbuf.
865 cp = mtod(m, u_char *);
866 address = PPP_ADDRESS(cp);
867 control = PPP_CONTROL(cp);
868 protocol = PPP_PROTOCOL(cp);
874 * If the packet is a TCP/IP packet, see if we can compress it.
876 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
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)) {
920 struct mbuf *mcomp = NULL;
924 for (mp = m; mp != NULL; mp = mp->m_next)
926 clen = (*sc->sc_xcomp->compress)
927 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
929 if (sc->sc_flags & SC_CCP_UP) {
930 /* Send the compressed packet instead of the original. */
933 cp = mtod(m, u_char *);
936 /* Can't transmit compressed packets until CCP is up. */
941 #endif /* PPP_COMPRESS */
944 * Compress the address/control and protocol, if possible.
946 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
947 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
948 protocol != PPP_LCP) {
949 /* can compress address/control */
953 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
954 /* can compress protocol */
955 if (mtod(m, u_char *) == cp) {
956 cp[2] = cp[1]; /* move address/control up */
967 * Software interrupt routine, called at splnet.
972 struct ppp_softc *sc;
978 for (i = 0; i < NPPP; ++i, ++sc) {
979 if (!(sc->sc_flags & SC_TBUSY)
980 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
982 sc->sc_flags |= SC_TBUSY;
988 IF_DEQUEUE(&sc->sc_rawq, m);
1000 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1001 * 0 if it is about to be transmitted.
1004 ppp_ccp(sc, m, rcvd)
1005 struct ppp_softc *sc;
1014 * Get a pointer to the data after the PPP header.
1016 if (m->m_len <= PPP_HDRLEN) {
1020 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1023 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1026 ep = mtod(mp, u_char *) + mp->m_len;
1027 if (dp + CCP_HDRLEN > ep)
1029 slen = CCP_LENGTH(dp);
1030 if (dp + slen > ep) {
1031 if (sc->sc_flags & SC_DEBUG)
1032 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
1033 dp, slen, mtod(mp, u_char *), mp->m_len);
1037 switch (CCP_CODE(dp)) {
1041 /* CCP must be going down - disable compression */
1042 if (sc->sc_flags & SC_CCP_UP) {
1044 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1050 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1051 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1052 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1054 /* we're agreeing to send compressed packets. */
1055 if (sc->sc_xc_state != NULL
1056 && (*sc->sc_xcomp->comp_init)
1057 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1058 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1060 sc->sc_flags |= SC_COMP_RUN;
1064 /* peer is agreeing to send compressed packets. */
1065 if (sc->sc_rc_state != NULL
1066 && (*sc->sc_rcomp->decomp_init)
1067 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1068 sc->sc_if.if_unit, 0, sc->sc_mru,
1069 sc->sc_flags & SC_DEBUG)) {
1071 sc->sc_flags |= SC_DECOMP_RUN;
1072 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1080 if (sc->sc_flags & SC_CCP_UP) {
1082 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1083 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1085 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1086 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1088 sc->sc_flags &= ~SC_DC_ERROR;
1098 * CCP is down; free (de)compressor state if necessary.
1102 struct ppp_softc *sc;
1104 if (sc->sc_xc_state) {
1105 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1106 sc->sc_xc_state = NULL;
1108 if (sc->sc_rc_state) {
1109 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1110 sc->sc_rc_state = NULL;
1113 #endif /* PPP_COMPRESS */
1116 * PPP packet input routine.
1117 * The caller has checked and removed the FCS and has inserted
1118 * the address/control bytes and the protocol high byte if they
1122 ppppktin(sc, m, lost)
1123 struct ppp_softc *sc;
1129 m->m_context = lost;
1130 IF_ENQUEUE(&sc->sc_rawq, m);
1131 schednetisr(NETISR_PPP);
1136 * Process a received PPP packet, doing decompression as necessary.
1137 * Should be called at splnet.
1139 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1140 TYPE_UNCOMPRESSED_TCP)
1144 struct ppp_softc *sc;
1147 struct ifnet *ifp = &sc->sc_if;
1148 struct ifqueue *inq, *lock;
1149 int s, ilen, xlen, proto, rv;
1150 u_char *cp, adrs, ctrl;
1151 struct mbuf *mp, *dmp = NULL, *pc;
1155 sc->sc_stats.ppp_ipackets++;
1157 if (sc->sc_flags & SC_LOG_INPKT) {
1159 for (mp = m; mp != NULL; mp = mp->m_next)
1161 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1165 cp = mtod(m, u_char *);
1166 adrs = PPP_ADDRESS(cp);
1167 ctrl = PPP_CONTROL(cp);
1168 proto = PPP_PROTOCOL(cp);
1172 sc->sc_flags |= SC_VJ_RESET;
1178 * Decompress this packet if necessary, update the receiver's
1179 * dictionary, or take appropriate action on a CCP packet.
1181 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1182 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1183 /* decompress this packet */
1184 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1185 if (rv == DECOMP_OK) {
1188 /* no error, but no decompressed packet produced */
1192 cp = mtod(m, u_char *);
1193 proto = PPP_PROTOCOL(cp);
1197 * An error has occurred in decompression.
1198 * Pass the compressed packet up to pppd, which may take
1199 * CCP down or issue a Reset-Req.
1201 if (sc->sc_flags & SC_DEBUG)
1202 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1204 sc->sc_flags |= SC_VJ_RESET;
1205 if (rv == DECOMP_ERROR)
1206 sc->sc_flags |= SC_DC_ERROR;
1208 sc->sc_flags |= SC_DC_FERROR;
1213 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1214 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1216 if (proto == PPP_CCP) {
1223 for (mp = m; mp != NULL; mp = mp->m_next)
1227 if (sc->sc_flags & SC_VJ_RESET) {
1229 * If we've missed a packet, we must toss subsequent compressed
1230 * packets which don't have an explicit connection ID.
1233 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1235 sc->sc_flags &= ~SC_VJ_RESET;
1240 * See if we have a VJ-compressed packet to uncompress.
1242 if (proto == PPP_VJC_COMP) {
1243 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1246 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1247 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1248 sc->sc_comp, &iphdr, &hlen);
1251 if (sc->sc_flags & SC_DEBUG)
1252 printf("ppp%d: VJ uncompress failed on type comp\n",
1257 /* Copy the PPP and IP headers into a new mbuf. */
1258 MGET(mp, M_DONTWAIT, MT_DATA);
1263 if (hlen + PPP_HDRLEN > MLEN) {
1265 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1267 goto bad; /* lose if big headers and no clusters */
1270 cp = mtod(mp, u_char *);
1276 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1277 mp->m_len = hlen + PPP_HDRLEN;
1280 * Trim the PPP and VJ headers off the old mbuf
1281 * and stick the new and old mbufs together.
1283 m->m_off += PPP_HDRLEN + xlen;
1284 m->m_len -= PPP_HDRLEN + xlen;
1285 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1286 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1287 mp->m_len += m->m_len;
1288 MFREE(m, mp->m_next);
1292 ilen += hlen - xlen;
1294 } else if (proto == PPP_VJC_UNCOMP) {
1295 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1298 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1299 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1300 sc->sc_comp, &iphdr, &hlen);
1303 if (sc->sc_flags & SC_DEBUG)
1304 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1315 * If the packet will fit in an ordinary mbuf, don't waste a
1316 * whole cluster on it.
1318 if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1319 MGET(mp, M_DONTWAIT, MT_DATA);
1321 m_copydata(m, mtod(mp, caddr_t), ilen);
1329 * Record the time that we received this packet.
1331 if ((proto & 0x8000) == 0) {
1332 sc->sc_last_recv = time.tv_sec;
1336 /* See if bpf wants to look at the packet. */
1338 bpf_mtap(sc->sc_bpf, m);
1346 * IP packet - take off the ppp header and pass it up to IP.
1348 if ((ifp->if_flags & IFF_UP) == 0
1349 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1350 /* interface is down - drop the packet. */
1354 m->m_off += PPP_HDRLEN;
1355 m->m_len -= PPP_HDRLEN;
1356 schednetisr(NETISR_IP);
1363 * Some other protocol - place on input queue for read().
1371 * Put the packet on the appropriate input queue.
1375 smp_lock(&lock->lk_ifqueue, LK_RETRY);
1376 if (IF_QFULL(inq)) {
1378 smp_unlock(&lock->lk_ifqueue);
1380 if (sc->sc_flags & SC_DEBUG)
1381 printf("ppp%d: input queue full\n", ifp->if_unit);
1384 IF_ENQUEUEIF(inq, m, &sc->sc_if);
1385 smp_unlock(&lock->lk_ifqueue);
1396 sc->sc_if.if_ierrors++;
1397 sc->sc_stats.ppp_ierrors++;
1400 #define MAX_DUMP_BYTES 128
1406 char buf[3*MAX_DUMP_BYTES+4];
1409 static char digits[] = "0123456789abcdef";
1411 for (m = m0; m; m = m->m_next) {
1413 u_char *rptr = mtod(m, u_char *);
1416 if (bp > buf + sizeof(buf) - 4)
1418 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1419 *bp++ = digits[*rptr++ & 0xf];
1423 if (bp > buf + sizeof(buf) - 3)
1433 printf("%s\n", buf);
1436 #endif /* NPPP > 0 */