1 /* $Id: if_ppp.c,v 1.4 1996/07/01 05:30:45 paulus Exp $ */
4 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6 * Copyright (c) 1989 Carnegie Mellon University.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by Carnegie Mellon University. The name of the
15 * University may not be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 * Carnegie Mellon University
24 * Pittsburgh, PA 15213
29 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
31 * Copyright (c) 1987 Regents of the University of California.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms are permitted
35 * provided that the above copyright notice and this paragraph are
36 * duplicated in all such forms and that any documentation,
37 * advertising materials, and other materials related to such
38 * distribution and use acknowledge that the software was developed
39 * by the University of California, Berkeley. The name of the
40 * University may not be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
44 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 * Serial Line interface
49 * Center for Seismic Studies
50 * 1300 N 17th Street, Suite 1450
51 * Arlington, Virginia 22209
56 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
57 * Converted to 4.3BSD Beta by Chris Torek.
58 * Other changes made at Berkeley, based in part on code by Kirk Smith.
60 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
61 * Added VJ tcp header compression; more unified ioctls
63 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
64 * Cleaned up a lot of the mbuf-related code to fix bugs that
65 * caused system crashes and packet corruption. Changed pppstart
66 * so that it doesn't just give up with a collision if the whole
67 * packet doesn't fit in the output ring buffer.
69 * Added priority queueing for interactive IP packets, following
70 * the model of if_sl.c, plus hooks for bpf.
71 * Paul Mackerras (paulus@cs.anu.edu.au).
74 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
75 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
83 #include <sys/param.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/kernel.h>
89 #include <sys/systm.h>
91 #include <sys/malloc.h>
93 #if NetBSD1_0 && defined(i386)
94 #include <machine/psl.h>
98 #include <net/if_types.h>
99 #include <net/netisr.h>
100 #include <net/route.h>
103 #include <netinet/in.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip.h>
109 #include "bpfilter.h"
115 #include <net/slcompress.h>
118 #include <net/ppp_defs.h>
119 #include <net/if_ppp.h>
120 #include <net/if_pppvar.h>
121 #include <machine/cpu.h>
124 #define splsoftnet splnet
128 #define PACKETPTR struct mbuf *
129 #include <net/ppp-comp.h>
132 static void ppp_requeue __P((struct ppp_softc *));
133 static void ppp_outpkt __P((struct ppp_softc *));
134 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
135 static void ppp_ccp_closed __P((struct ppp_softc *));
136 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
137 static void pppdumpm __P((struct mbuf *m0));
140 * Some useful mbuf macros not in mbuf.h.
142 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
144 #define M_DATASTART(m) \
145 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
146 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
148 #define M_DATASIZE(m) \
149 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
150 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
153 * We steal two bits in the mbuf m_flags, to mark high-priority packets
154 * for output, and received packets following lost/corrupted packets.
156 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
157 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
162 * List of compressors we know about.
163 * We leave some space so maybe we can modload compressors.
166 extern struct compressor ppp_bsd_compress;
167 extern struct compressor ppp_deflate;
169 struct compressor *ppp_compressors[8] = {
178 #endif /* PPP_COMPRESS */
182 * Called from boot code to establish ppp interfaces.
187 register struct ppp_softc *sc;
190 for (sc = ppp_softc; i < NPPP; sc++) {
191 sc->sc_if.if_name = "ppp";
192 sc->sc_if.if_unit = i++;
193 sc->sc_if.if_mtu = PPP_MTU;
194 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
195 sc->sc_if.if_type = IFT_PPP;
196 sc->sc_if.if_hdrlen = PPP_HDRLEN;
197 sc->sc_if.if_ioctl = pppsioctl;
198 sc->sc_if.if_output = pppoutput;
199 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
200 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
201 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
202 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
203 if_attach(&sc->sc_if);
205 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
209 #if NetBSD1_0 && defined(i386)
211 * XXX kludge to fix the bug in the i386 interrupt handling code,
212 * where software interrupts could be taken while hardware
213 * interrupts were blocked.
215 if ((imask[IPL_TTY] & (1 << SIR_NET)) == 0) {
216 imask[IPL_TTY] |= (1 << SIR_NET);
217 intr_calculatemasks();
223 * Allocate a ppp interface unit and initialize it.
230 struct ppp_softc *sc;
232 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
233 if (sc->sc_xfer == pid) {
237 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
238 if (sc->sc_devp == NULL)
244 sc->sc_mru = PPP_MRU;
245 sc->sc_relinq = NULL;
246 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
248 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
251 sl_compress_init(sc->sc_comp, -1);
254 sc->sc_xc_state = NULL;
255 sc->sc_rc_state = NULL;
256 #endif /* PPP_COMPRESS */
257 for (i = 0; i < NUM_NP; ++i)
258 sc->sc_npmode[i] = NPMODE_ERROR;
259 sc->sc_npqueue = NULL;
260 sc->sc_npqtail = &sc->sc_npqueue;
261 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
267 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
271 struct ppp_softc *sc;
276 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
280 IF_DEQUEUE(&sc->sc_rawq, m);
286 IF_DEQUEUE(&sc->sc_inq, m);
292 IF_DEQUEUE(&sc->sc_fastq, m);
297 while ((m = sc->sc_npqueue) != NULL) {
298 sc->sc_npqueue = m->m_nextpkt;
301 if (sc->sc_togo != NULL) {
302 m_freem(sc->sc_togo);
307 sc->sc_xc_state = NULL;
308 sc->sc_rc_state = NULL;
309 #endif /* PPP_COMPRESS */
311 if (sc->sc_comp != 0) {
312 FREE(sc->sc_comp, M_DEVBUF);
319 * Ioctl routine for generic ppp devices.
322 pppioctl(sc, cmd, data, flag, p)
323 struct ppp_softc *sc;
329 int s, error, flags, mru, nb, npx;
330 struct ppp_option_data *odp;
331 struct compressor **cp;
335 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
340 *(int *)data = sc->sc_inq.ifq_len;
344 *(int *)data = sc->sc_if.if_unit;
348 *(u_int *)data = sc->sc_flags;
352 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
354 flags = *(int *)data & SC_MASK;
357 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
361 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
366 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
369 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
374 *(int *)data = sc->sc_mru;
379 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
383 sl_compress_init(sc->sc_comp, *(int *)data);
390 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
392 sc->sc_xfer = p->p_pid;
396 case PPPIOCSCOMPRESS:
397 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
399 odp = (struct ppp_option_data *) data;
401 if (nb > sizeof(ccp_option))
402 nb = sizeof(ccp_option);
403 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
405 if (ccp_option[1] < 2) /* preliminary check on the length byte */
407 for (cp = ppp_compressors; *cp != NULL; ++cp)
408 if ((*cp)->compress_proto == ccp_option[0]) {
410 * Found a handler for the protocol - try to allocate
411 * a compressor or decompressor.
416 if (sc->sc_xc_state != NULL)
417 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
419 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
420 if (sc->sc_xc_state == NULL) {
421 if (sc->sc_flags & SC_DEBUG)
422 printf("ppp%d: comp_alloc failed\n",
427 sc->sc_flags &= ~SC_COMP_RUN;
431 if (sc->sc_rc_state != NULL)
432 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
434 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
435 if (sc->sc_rc_state == NULL) {
436 if (sc->sc_flags & SC_DEBUG)
437 printf("ppp%d: decomp_alloc failed\n",
442 sc->sc_flags &= ~SC_DECOMP_RUN;
447 if (sc->sc_flags & SC_DEBUG)
448 printf("ppp%d: no compressor for [%x %x %x], %x\n",
449 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
451 return (EINVAL); /* no handler found */
452 #endif /* PPP_COMPRESS */
456 npi = (struct npioctl *) data;
457 switch (npi->protocol) {
464 if (cmd == PPPIOCGNPMODE) {
465 npi->mode = sc->sc_npmode[npx];
467 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
469 if (npi->mode != sc->sc_npmode[npx]) {
471 sc->sc_npmode[npx] = npi->mode;
472 if (npi->mode != NPMODE_QUEUE) {
484 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
485 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
492 nbp = (struct bpf_program *) data;
493 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
495 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
496 if (newcodelen != 0) {
497 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
499 return EINVAL; /* or sumpin */
501 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
503 FREE(newcode, M_DEVBUF);
506 if (!bpf_validate(newcode, nbp->bf_len)) {
507 FREE(newcode, M_DEVBUF);
512 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
513 oldcode = bp->bf_insns;
515 bp->bf_len = nbp->bf_len;
516 bp->bf_insns = newcode;
519 FREE(oldcode, M_DEVBUF);
530 * Process an ioctl request to the ppp network interface.
533 pppsioctl(ifp, cmd, data)
534 register struct ifnet *ifp;
538 struct proc *p = curproc; /* XXX */
539 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
540 register struct ifaddr *ifa = (struct ifaddr *)data;
541 register struct ifreq *ifr = (struct ifreq *)data;
542 struct ppp_stats *psp;
544 struct ppp_comp_stats *pcp;
546 int s = splimp(), error = 0;
550 if ((ifp->if_flags & IFF_RUNNING) == 0)
551 ifp->if_flags &= ~IFF_UP;
555 if (ifa->ifa_addr->sa_family != AF_INET)
556 error = EAFNOSUPPORT;
560 if (ifa->ifa_addr->sa_family != AF_INET)
561 error = EAFNOSUPPORT;
565 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
567 sc->sc_if.if_mtu = ifr->ifr_mtu;
571 ifr->ifr_mtu = sc->sc_if.if_mtu;
577 error = EAFNOSUPPORT;
580 switch(ifr->ifr_addr.sa_family) {
586 error = EAFNOSUPPORT;
592 psp = &((struct ifpppstatsreq *) data)->stats;
593 bzero(psp, sizeof(*psp));
594 psp->p = sc->sc_stats;
595 #if defined(VJC) && !defined(SL_NO_STATS)
597 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
598 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
599 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
600 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
601 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
602 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
603 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
604 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
611 pcp = &((struct ifpppcstatsreq *) data)->stats;
612 bzero(pcp, sizeof(*pcp));
613 if (sc->sc_xc_state != NULL)
614 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
615 if (sc->sc_rc_state != NULL)
616 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
618 #endif /* PPP_COMPRESS */
628 * Queue a packet. Start transmission if not active.
629 * Packet is placed in Information field of PPP frame.
632 pppoutput(ifp, m0, dst, rtp)
635 struct sockaddr *dst;
638 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
639 int protocol, address, control;
648 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
649 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
650 error = ENETDOWN; /* sort of */
655 * Compute PPP header.
657 m0->m_flags &= ~M_HIGHPRI;
658 switch (dst->sa_family) {
661 address = PPP_ALLSTATIONS;
664 mode = sc->sc_npmode[NP_IP];
667 * If this packet has the "low delay" bit set in the IP header,
668 * put it on the fastq instead.
670 ip = mtod(m0, struct ip *);
671 if (ip->ip_tos & IPTOS_LOWDELAY)
672 m0->m_flags |= M_HIGHPRI;
676 address = PPP_ADDRESS(dst->sa_data);
677 control = PPP_CONTROL(dst->sa_data);
678 protocol = PPP_PROTOCOL(dst->sa_data);
682 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
683 error = EAFNOSUPPORT;
688 * Drop this packet, or return an error, if necessary.
690 if (mode == NPMODE_ERROR) {
694 if (mode == NPMODE_DROP) {
700 * Add PPP header. If no space in first mbuf, allocate another.
701 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
703 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
704 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
711 m0->m_data -= PPP_HDRLEN;
713 cp = mtod(m0, u_char *);
716 *cp++ = protocol >> 8;
717 *cp++ = protocol & 0xff;
718 m0->m_len += PPP_HDRLEN;
721 for (m = m0; m != 0; m = m->m_next)
724 if (sc->sc_flags & SC_LOG_OUTPKT) {
725 printf("ppp%d output: ", ifp->if_unit);
729 if ((protocol & 0x8000) == 0) {
731 * Update the time we sent the most recent data packet.
733 sc->sc_last_sent = time.tv_sec;
738 * See if bpf wants to look at the packet.
741 bpf_mtap(sc->sc_bpf, m0);
745 * Put the packet on the appropriate queue.
748 if (mode == NPMODE_QUEUE) {
749 /* XXX we should limit the number of packets on this queue */
750 *sc->sc_npqtail = m0;
751 m0->m_nextpkt = NULL;
752 sc->sc_npqtail = &m0->m_nextpkt;
754 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
755 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
758 sc->sc_if.if_oerrors++;
759 sc->sc_stats.ppp_oerrors++;
766 ifp->if_lastchange = time;
768 ifp->if_obytes += len;
779 * After a change in the NPmode for some NP, move packets from the
780 * npqueue to the send queue or the fast queue as appropriate.
781 * Should be called at splsoftnet.
785 struct ppp_softc *sc;
787 struct mbuf *m, **mpp;
791 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
792 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
794 mode = sc->sc_npmode[NP_IP];
803 * This packet can now go on one of the queues to be sent.
807 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
810 sc->sc_if.if_oerrors++;
811 sc->sc_stats.ppp_oerrors++;
827 sc->sc_npqtail = mpp;
831 * Transmitter has finished outputting some stuff;
832 * remember to call sc->sc_start later at splsoftnet.
836 struct ppp_softc *sc;
840 sc->sc_flags &= ~SC_TBUSY;
841 schednetisr(NETISR_PPP);
846 * Get a packet to send. This procedure is intended to be called at
847 * splsoftnet, since it may involve time-consuming operations such as
848 * applying VJ compression, packet compression, address/control and/or
849 * protocol field compression to the packet.
853 struct ppp_softc *sc;
857 int address, control, protocol;
861 * Grab a packet to send: first try the fast queue, then the
864 IF_DEQUEUE(&sc->sc_fastq, m);
866 IF_DEQUEUE(&sc->sc_if.if_snd, m);
870 ++sc->sc_stats.ppp_opackets;
873 * Extract the ppp header of the new packet.
874 * The ppp header will be in one mbuf.
876 cp = mtod(m, u_char *);
877 address = PPP_ADDRESS(cp);
878 control = PPP_CONTROL(cp);
879 protocol = PPP_PROTOCOL(cp);
885 * If the packet is a TCP/IP packet, see if we can compress it.
887 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
892 ip = (struct ip *) (cp + PPP_HDRLEN);
893 if (mp->m_len <= PPP_HDRLEN) {
897 ip = mtod(mp, struct ip *);
899 /* this code assumes the IP/TCP header is in one non-shared mbuf */
900 if (ip->ip_p == IPPROTO_TCP) {
901 type = sl_compress_tcp(mp, ip, sc->sc_comp,
902 !(sc->sc_flags & SC_NO_TCP_CCID));
904 case TYPE_UNCOMPRESSED_TCP:
905 protocol = PPP_VJC_UNCOMP;
907 case TYPE_COMPRESSED_TCP:
908 protocol = PPP_VJC_COMP;
909 cp = mtod(m, u_char *);
910 cp[0] = address; /* header has moved */
915 cp[3] = protocol; /* update protocol in PPP header */
925 #endif /* PPP_COMPRESS */
929 if (protocol != PPP_LCP && protocol != PPP_CCP
930 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
931 struct mbuf *mcomp = NULL;
935 for (mp = m; mp != NULL; mp = mp->m_next)
937 clen = (*sc->sc_xcomp->compress)
938 (sc->sc_xc_state, &mcomp, m, slen,
939 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
943 cp = mtod(m, u_char *);
947 #endif /* PPP_COMPRESS */
950 * Compress the address/control and protocol, if possible.
952 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
953 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
954 protocol != PPP_LCP) {
955 /* can compress address/control */
959 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
960 /* can compress protocol */
961 if (mtod(m, u_char *) == cp) {
962 cp[2] = cp[1]; /* move address/control up */
973 * Software interrupt routine, called at splsoftnet.
978 struct ppp_softc *sc;
984 for (i = 0; i < NPPP; ++i, ++sc) {
985 if (!(sc->sc_flags & SC_TBUSY)
986 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
988 sc->sc_flags |= SC_TBUSY;
994 IF_DEQUEUE(&sc->sc_rawq, m);
1006 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1007 * 0 if it is about to be transmitted.
1010 ppp_ccp(sc, m, rcvd)
1011 struct ppp_softc *sc;
1020 * Get a pointer to the data after the PPP header.
1022 if (m->m_len <= PPP_HDRLEN) {
1026 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1029 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1032 ep = mtod(mp, u_char *) + mp->m_len;
1033 if (dp + CCP_HDRLEN > ep)
1035 slen = CCP_LENGTH(dp);
1036 if (dp + slen > ep) {
1037 if (sc->sc_flags & SC_DEBUG)
1038 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1039 dp, slen, mtod(mp, u_char *), mp->m_len);
1043 switch (CCP_CODE(dp)) {
1047 /* CCP must be going down - disable compression */
1048 if (sc->sc_flags & SC_CCP_UP) {
1050 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1056 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1057 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1058 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1060 /* we're agreeing to send compressed packets. */
1061 if (sc->sc_xc_state != NULL
1062 && (*sc->sc_xcomp->comp_init)
1063 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1064 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1066 sc->sc_flags |= SC_COMP_RUN;
1070 /* peer is agreeing to send compressed packets. */
1071 if (sc->sc_rc_state != NULL
1072 && (*sc->sc_rcomp->decomp_init)
1073 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1074 sc->sc_if.if_unit, 0, sc->sc_mru,
1075 sc->sc_flags & SC_DEBUG)) {
1077 sc->sc_flags |= SC_DECOMP_RUN;
1078 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1086 if (sc->sc_flags & SC_CCP_UP) {
1088 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1089 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1091 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1092 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1094 sc->sc_flags &= ~SC_DC_ERROR;
1104 * CCP is down; free (de)compressor state if necessary.
1108 struct ppp_softc *sc;
1110 if (sc->sc_xc_state) {
1111 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1112 sc->sc_xc_state = NULL;
1114 if (sc->sc_rc_state) {
1115 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1116 sc->sc_rc_state = NULL;
1119 #endif /* PPP_COMPRESS */
1122 * PPP packet input routine.
1123 * The caller has checked and removed the FCS and has inserted
1124 * the address/control bytes and the protocol high byte if they
1128 ppppktin(sc, m, lost)
1129 struct ppp_softc *sc;
1136 m->m_flags |= M_ERRMARK;
1137 IF_ENQUEUE(&sc->sc_rawq, m);
1138 schednetisr(NETISR_PPP);
1143 * Process a received PPP packet, doing decompression as necessary.
1144 * Should be called at splsoftnet.
1146 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1147 TYPE_UNCOMPRESSED_TCP)
1151 struct ppp_softc *sc;
1154 struct ifnet *ifp = &sc->sc_if;
1155 struct ifqueue *inq;
1156 int s, ilen, xlen, proto, rv;
1157 u_char *cp, adrs, ctrl;
1158 struct mbuf *mp, *dmp = NULL;
1162 sc->sc_stats.ppp_ipackets++;
1164 if (sc->sc_flags & SC_LOG_INPKT) {
1166 for (mp = m; mp != NULL; mp = mp->m_next)
1168 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1172 cp = mtod(m, u_char *);
1173 adrs = PPP_ADDRESS(cp);
1174 ctrl = PPP_CONTROL(cp);
1175 proto = PPP_PROTOCOL(cp);
1177 if (m->m_flags & M_ERRMARK) {
1178 m->m_flags &= ~M_ERRMARK;
1180 sc->sc_flags |= SC_VJ_RESET;
1186 * Decompress this packet if necessary, update the receiver's
1187 * dictionary, or take appropriate action on a CCP packet.
1189 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1190 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1191 /* decompress this packet */
1192 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1193 if (rv == DECOMP_OK) {
1196 /* no error, but no decompressed packet produced */
1200 cp = mtod(m, u_char *);
1201 proto = PPP_PROTOCOL(cp);
1205 * An error has occurred in decompression.
1206 * Pass the compressed packet up to pppd, which may take
1207 * CCP down or issue a Reset-Req.
1209 if (sc->sc_flags & SC_DEBUG)
1210 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1212 sc->sc_flags |= SC_VJ_RESET;
1213 if (rv == DECOMP_ERROR)
1214 sc->sc_flags |= SC_DC_ERROR;
1216 sc->sc_flags |= SC_DC_FERROR;
1221 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1222 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1224 if (proto == PPP_CCP) {
1231 for (mp = m; mp != NULL; mp = mp->m_next)
1235 if (sc->sc_flags & SC_VJ_RESET) {
1237 * If we've missed a packet, we must toss subsequent compressed
1238 * packets which don't have an explicit connection ID.
1241 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1243 sc->sc_flags &= ~SC_VJ_RESET;
1248 * See if we have a VJ-compressed packet to uncompress.
1250 if (proto == PPP_VJC_COMP) {
1251 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1254 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1255 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1256 sc->sc_comp, &iphdr, &hlen);
1259 if (sc->sc_flags & SC_DEBUG)
1260 printf("ppp%d: VJ uncompress failed on type comp\n",
1265 /* Copy the PPP and IP headers into a new mbuf. */
1266 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1271 if (hlen + PPP_HDRLEN > MHLEN) {
1272 MCLGET(mp, M_DONTWAIT);
1273 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1275 goto bad; /* lose if big headers and no clusters */
1278 cp = mtod(mp, u_char *);
1284 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1285 mp->m_len = hlen + PPP_HDRLEN;
1288 * Trim the PPP and VJ headers off the old mbuf
1289 * and stick the new and old mbufs together.
1291 m->m_data += PPP_HDRLEN + xlen;
1292 m->m_len -= PPP_HDRLEN + xlen;
1293 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1294 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1295 mp->m_len += m->m_len;
1296 MFREE(m, mp->m_next);
1300 ilen += hlen - xlen;
1302 } else if (proto == PPP_VJC_UNCOMP) {
1303 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1306 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1307 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1308 sc->sc_comp, &iphdr, &hlen);
1311 if (sc->sc_flags & SC_DEBUG)
1312 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1323 * If the packet will fit in a header mbuf, don't waste a
1324 * whole cluster on it.
1326 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1327 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1329 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1335 m->m_pkthdr.len = ilen;
1336 m->m_pkthdr.rcvif = ifp;
1339 * Record the time that we received this packet.
1341 if ((proto & 0x8000) == 0) {
1342 sc->sc_last_recv = time.tv_sec;
1346 /* See if bpf wants to look at the packet. */
1348 bpf_mtap(sc->sc_bpf, m);
1356 * IP packet - take off the ppp header and pass it up to IP.
1358 if ((ifp->if_flags & IFF_UP) == 0
1359 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1360 /* interface is down - drop the packet. */
1364 m->m_pkthdr.len -= PPP_HDRLEN;
1365 m->m_data += PPP_HDRLEN;
1366 m->m_len -= PPP_HDRLEN;
1367 schednetisr(NETISR_IP);
1374 * Some other protocol - place on input queue for read().
1382 * Put the packet on the appropriate input queue.
1385 if (IF_QFULL(inq)) {
1388 if (sc->sc_flags & SC_DEBUG)
1389 printf("ppp%d: input queue full\n", ifp->if_unit);
1396 ifp->if_ibytes += ilen;
1397 ifp->if_lastchange = time;
1406 sc->sc_if.if_ierrors++;
1407 sc->sc_stats.ppp_ierrors++;
1410 #define MAX_DUMP_BYTES 128
1416 char buf[3*MAX_DUMP_BYTES+4];
1419 static char digits[] = "0123456789abcdef";
1421 for (m = m0; m; m = m->m_next) {
1423 u_char *rptr = (u_char *)m->m_data;
1426 if (bp > buf + sizeof(buf) - 4)
1428 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1429 *bp++ = digits[*rptr++ & 0xf];
1433 if (bp > buf + sizeof(buf) - 3)
1443 printf("%s\n", buf);
1446 #endif /* NPPP > 0 */