1 /* $Id: if_ppp.c,v 1.3 1996/07/01 01:03:38 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>
94 #include <net/if_types.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/in_var.h>
102 #include <netinet/ip.h>
105 #include "bpfilter.h"
111 #include <net/slcompress.h>
114 #include <net/ppp_defs.h>
115 #include <net/if_ppp.h>
116 #include <net/if_pppvar.h>
117 #include <machine/cpu.h>
120 #define PACKETPTR struct mbuf *
121 #include <net/ppp-comp.h>
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_flags & M_EXT)
136 #define M_DATASTART(m) \
137 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
138 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
140 #define M_DATASIZE(m) \
141 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
142 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
145 * We steal two bits in the mbuf m_flags, to mark high-priority packets
146 * for output, and received packets following lost/corrupted packets.
148 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
149 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
154 * List of compressors we know about.
155 * We leave some space so maybe we can modload compressors.
158 extern struct compressor ppp_bsd_compress;
159 extern struct compressor ppp_deflate;
161 struct compressor *ppp_compressors[8] = {
170 #endif /* PPP_COMPRESS */
174 * Called from boot code to establish ppp interfaces.
179 register struct ppp_softc *sc;
182 for (sc = ppp_softc; i < NPPP; sc++) {
183 sc->sc_if.if_name = "ppp";
184 sc->sc_if.if_unit = i++;
185 sc->sc_if.if_mtu = PPP_MTU;
186 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
187 sc->sc_if.if_type = IFT_PPP;
188 sc->sc_if.if_hdrlen = PPP_HDRLEN;
189 sc->sc_if.if_ioctl = pppsioctl;
190 sc->sc_if.if_output = pppoutput;
191 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
192 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
193 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
194 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
195 if_attach(&sc->sc_if);
197 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
203 * Allocate a ppp interface unit and initialize it.
210 struct ppp_softc *sc;
212 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
213 if (sc->sc_xfer == pid) {
217 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
218 if (sc->sc_devp == NULL)
224 sc->sc_mru = PPP_MRU;
225 sc->sc_relinq = NULL;
226 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
228 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
231 sl_compress_init(sc->sc_comp, -1);
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 splsoftnet 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_nextpkt;
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 */
291 if (sc->sc_comp != 0) {
292 FREE(sc->sc_comp, M_DEVBUF);
299 * Ioctl routine for generic ppp devices.
302 pppioctl(sc, cmd, data, flag, p)
303 struct ppp_softc *sc;
309 int s, error, flags, mru, nb, npx;
310 struct ppp_option_data *odp;
311 struct compressor **cp;
315 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
320 *(int *)data = sc->sc_inq.ifq_len;
324 *(int *)data = sc->sc_if.if_unit;
328 *(u_int *)data = sc->sc_flags;
332 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
334 flags = *(int *)data & SC_MASK;
337 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
341 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
346 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
349 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
354 *(int *)data = sc->sc_mru;
359 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
363 sl_compress_init(sc->sc_comp, *(int *)data);
370 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
372 sc->sc_xfer = p->p_pid;
376 case PPPIOCSCOMPRESS:
377 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
379 odp = (struct ppp_option_data *) data;
381 if (nb > sizeof(ccp_option))
382 nb = sizeof(ccp_option);
383 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
385 if (ccp_option[1] < 2) /* preliminary check on the length byte */
387 for (cp = ppp_compressors; *cp != NULL; ++cp)
388 if ((*cp)->compress_proto == ccp_option[0]) {
390 * Found a handler for the protocol - try to allocate
391 * a compressor or decompressor.
396 if (sc->sc_xc_state != NULL)
397 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
399 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
400 if (sc->sc_xc_state == NULL) {
401 if (sc->sc_flags & SC_DEBUG)
402 printf("ppp%d: comp_alloc failed\n",
407 sc->sc_flags &= ~SC_COMP_RUN;
411 if (sc->sc_rc_state != NULL)
412 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
414 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
415 if (sc->sc_rc_state == NULL) {
416 if (sc->sc_flags & SC_DEBUG)
417 printf("ppp%d: decomp_alloc failed\n",
422 sc->sc_flags &= ~SC_DECOMP_RUN;
427 if (sc->sc_flags & SC_DEBUG)
428 printf("ppp%d: no compressor for [%x %x %x], %x\n",
429 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
431 return (EINVAL); /* no handler found */
432 #endif /* PPP_COMPRESS */
436 npi = (struct npioctl *) data;
437 switch (npi->protocol) {
444 if (cmd == PPPIOCGNPMODE) {
445 npi->mode = sc->sc_npmode[npx];
447 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
449 if (npi->mode != sc->sc_npmode[npx]) {
451 sc->sc_npmode[npx] = npi->mode;
452 if (npi->mode != NPMODE_QUEUE) {
464 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
465 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
472 nbp = (struct bpf_program *) data;
473 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
475 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
476 if (newcodelen != 0) {
477 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
479 return EINVAL; /* or sumpin */
481 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
483 FREE(newcode, M_DEVBUF);
486 if (!bpf_validate(newcode, nbp->bf_len)) {
487 FREE(newcode, M_DEVBUF);
492 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
493 oldcode = bp->bf_insns;
495 bp->bf_len = nbp->bf_len;
496 bp->bf_insns = newcode;
499 FREE(oldcode, M_DEVBUF);
510 * Process an ioctl request to the ppp network interface.
513 pppsioctl(ifp, cmd, data)
514 register struct ifnet *ifp;
518 struct proc *p = curproc; /* XXX */
519 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
520 register struct ifaddr *ifa = (struct ifaddr *)data;
521 register struct ifreq *ifr = (struct ifreq *)data;
522 struct ppp_stats *psp;
524 struct ppp_comp_stats *pcp;
526 int s = splimp(), error = 0;
530 if ((ifp->if_flags & IFF_RUNNING) == 0)
531 ifp->if_flags &= ~IFF_UP;
535 if (ifa->ifa_addr->sa_family != AF_INET)
536 error = EAFNOSUPPORT;
540 if (ifa->ifa_addr->sa_family != AF_INET)
541 error = EAFNOSUPPORT;
545 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
547 sc->sc_if.if_mtu = ifr->ifr_mtu;
551 ifr->ifr_mtu = sc->sc_if.if_mtu;
557 error = EAFNOSUPPORT;
560 switch(ifr->ifr_addr.sa_family) {
566 error = EAFNOSUPPORT;
572 psp = &((struct ifpppstatsreq *) data)->stats;
573 bzero(psp, sizeof(*psp));
574 psp->p = sc->sc_stats;
575 #if defined(VJC) && !defined(SL_NO_STATS)
577 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
578 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
579 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
580 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
581 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
582 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
583 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
584 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
591 pcp = &((struct ifpppcstatsreq *) data)->stats;
592 bzero(pcp, sizeof(*pcp));
593 if (sc->sc_xc_state != NULL)
594 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
595 if (sc->sc_rc_state != NULL)
596 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
598 #endif /* PPP_COMPRESS */
608 * Queue a packet. Start transmission if not active.
609 * Packet is placed in Information field of PPP frame.
612 pppoutput(ifp, m0, dst, rtp)
615 struct sockaddr *dst;
618 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
619 int protocol, address, control;
626 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
627 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
628 error = ENETDOWN; /* sort of */
633 * Compute PPP header.
635 m0->m_flags &= ~M_HIGHPRI;
636 switch (dst->sa_family) {
639 address = PPP_ALLSTATIONS;
642 mode = sc->sc_npmode[NP_IP];
645 * If this packet has the "low delay" bit set in the IP header,
646 * put it on the fastq instead.
648 ip = mtod(m0, struct ip *);
649 if (ip->ip_tos & IPTOS_LOWDELAY)
650 m0->m_flags |= M_HIGHPRI;
654 address = PPP_ADDRESS(dst->sa_data);
655 control = PPP_CONTROL(dst->sa_data);
656 protocol = PPP_PROTOCOL(dst->sa_data);
660 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
661 error = EAFNOSUPPORT;
666 * Drop this packet, or return an error, if necessary.
668 if (mode == NPMODE_ERROR) {
672 if (mode == NPMODE_DROP) {
678 * Add PPP header. If no space in first mbuf, allocate another.
679 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
681 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
682 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
689 m0->m_data -= PPP_HDRLEN;
691 cp = mtod(m0, u_char *);
694 *cp++ = protocol >> 8;
695 *cp++ = protocol & 0xff;
696 m0->m_len += PPP_HDRLEN;
698 if (sc->sc_flags & SC_LOG_OUTPKT) {
699 printf("ppp%d output: ", ifp->if_unit);
703 if ((protocol & 0x8000) == 0) {
705 * Update the time we sent the most recent data packet.
707 sc->sc_last_sent = time.tv_sec;
712 * See if bpf wants to look at the packet.
715 bpf_mtap(sc->sc_bpf, m0);
719 * Put the packet on the appropriate queue.
722 if (mode == NPMODE_QUEUE) {
723 /* XXX we should limit the number of packets on this queue */
724 *sc->sc_npqtail = m0;
725 m0->m_nextpkt = NULL;
726 sc->sc_npqtail = &m0->m_nextpkt;
728 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
729 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
732 sc->sc_if.if_oerrors++;
733 sc->sc_stats.ppp_oerrors++;
740 ifp->if_lastchange = time;
742 ifp->if_obytes += len;
753 * After a change in the NPmode for some NP, move packets from the
754 * npqueue to the send queue or the fast queue as appropriate.
755 * Should be called at splsoftnet.
759 struct ppp_softc *sc;
761 struct mbuf *m, **mpp;
765 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
766 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
768 mode = sc->sc_npmode[NP_IP];
777 * This packet can now go on one of the queues to be sent.
781 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
784 sc->sc_if.if_oerrors++;
785 sc->sc_stats.ppp_oerrors++;
801 sc->sc_npqtail = mpp;
805 * Transmitter has finished outputting some stuff;
806 * remember to call sc->sc_start later at splsoftnet.
810 struct ppp_softc *sc;
814 sc->sc_flags &= ~SC_TBUSY;
815 schednetisr(NETISR_PPP);
820 * Get a packet to send. This procedure is intended to be called at
821 * splsoftnet, since it may involve time-consuming operations such as
822 * applying VJ compression, packet compression, address/control and/or
823 * protocol field compression to the packet.
827 struct ppp_softc *sc;
831 int address, control, protocol;
835 * Grab a packet to send: first try the fast queue, then the
838 IF_DEQUEUE(&sc->sc_fastq, m);
840 IF_DEQUEUE(&sc->sc_if.if_snd, m);
844 ++sc->sc_stats.ppp_opackets;
847 * Extract the ppp header of the new packet.
848 * The ppp header will be in one mbuf.
850 cp = mtod(m, u_char *);
851 address = PPP_ADDRESS(cp);
852 control = PPP_CONTROL(cp);
853 protocol = PPP_PROTOCOL(cp);
859 * If the packet is a TCP/IP packet, see if we can compress it.
861 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
866 ip = (struct ip *) (cp + PPP_HDRLEN);
867 if (mp->m_len <= PPP_HDRLEN) {
871 ip = mtod(mp, struct ip *);
873 /* this code assumes the IP/TCP header is in one non-shared mbuf */
874 if (ip->ip_p == IPPROTO_TCP) {
875 type = sl_compress_tcp(mp, ip, sc->sc_comp,
876 !(sc->sc_flags & SC_NO_TCP_CCID));
878 case TYPE_UNCOMPRESSED_TCP:
879 protocol = PPP_VJC_UNCOMP;
881 case TYPE_COMPRESSED_TCP:
882 protocol = PPP_VJC_COMP;
883 cp = mtod(m, u_char *);
884 cp[0] = address; /* header has moved */
889 cp[3] = protocol; /* update protocol in PPP header */
899 #endif /* PPP_COMPRESS */
903 if (protocol != PPP_LCP && protocol != PPP_CCP
904 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
905 struct mbuf *mcomp = NULL;
909 for (mp = m; mp != NULL; mp = mp->m_next)
911 clen = (*sc->sc_xcomp->compress)
912 (sc->sc_xc_state, &mcomp, m, slen,
913 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
917 cp = mtod(m, u_char *);
921 #endif /* PPP_COMPRESS */
924 * Compress the address/control and protocol, if possible.
926 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
927 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
928 protocol != PPP_LCP) {
929 /* can compress address/control */
933 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
934 /* can compress protocol */
935 if (mtod(m, u_char *) == cp) {
936 cp[2] = cp[1]; /* move address/control up */
947 * Software interrupt routine, called at splsoftnet.
952 struct ppp_softc *sc;
958 for (i = 0; i < NPPP; ++i, ++sc) {
959 if (!(sc->sc_flags & SC_TBUSY)
960 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
962 sc->sc_flags |= SC_TBUSY;
968 IF_DEQUEUE(&sc->sc_rawq, m);
980 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
981 * 0 if it is about to be transmitted.
985 struct ppp_softc *sc;
994 * Get a pointer to the data after the PPP header.
996 if (m->m_len <= PPP_HDRLEN) {
1000 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1003 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1006 ep = mtod(mp, u_char *) + mp->m_len;
1007 if (dp + CCP_HDRLEN > ep)
1009 slen = CCP_LENGTH(dp);
1010 if (dp + slen > ep) {
1011 if (sc->sc_flags & SC_DEBUG)
1012 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1013 dp, slen, mtod(mp, u_char *), mp->m_len);
1017 switch (CCP_CODE(dp)) {
1021 /* CCP must be going down - disable compression */
1022 if (sc->sc_flags & SC_CCP_UP) {
1024 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1030 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1031 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1032 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1034 /* we're agreeing to send compressed packets. */
1035 if (sc->sc_xc_state != NULL
1036 && (*sc->sc_xcomp->comp_init)
1037 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1038 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1040 sc->sc_flags |= SC_COMP_RUN;
1044 /* peer is agreeing to send compressed packets. */
1045 if (sc->sc_rc_state != NULL
1046 && (*sc->sc_rcomp->decomp_init)
1047 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1048 sc->sc_if.if_unit, 0, sc->sc_mru,
1049 sc->sc_flags & SC_DEBUG)) {
1051 sc->sc_flags |= SC_DECOMP_RUN;
1052 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1060 if (sc->sc_flags & SC_CCP_UP) {
1062 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1063 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1065 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1066 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1068 sc->sc_flags &= ~SC_DC_ERROR;
1078 * CCP is down; free (de)compressor state if necessary.
1082 struct ppp_softc *sc;
1084 if (sc->sc_xc_state) {
1085 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1086 sc->sc_xc_state = NULL;
1088 if (sc->sc_rc_state) {
1089 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1090 sc->sc_rc_state = NULL;
1093 #endif /* PPP_COMPRESS */
1096 * PPP packet input routine.
1097 * The caller has checked and removed the FCS and has inserted
1098 * the address/control bytes and the protocol high byte if they
1102 ppppktin(sc, m, lost)
1103 struct ppp_softc *sc;
1110 m->m_flags |= M_ERRMARK;
1111 IF_ENQUEUE(&sc->sc_rawq, m);
1112 schednetisr(NETISR_PPP);
1117 * Process a received PPP packet, doing decompression as necessary.
1118 * Should be called at splsoftnet.
1120 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1121 TYPE_UNCOMPRESSED_TCP)
1125 struct ppp_softc *sc;
1128 struct ifnet *ifp = &sc->sc_if;
1129 struct ifqueue *inq;
1130 int s, ilen, xlen, proto, rv;
1131 u_char *cp, adrs, ctrl;
1132 struct mbuf *mp, *dmp = NULL;
1136 sc->sc_stats.ppp_ipackets++;
1138 if (sc->sc_flags & SC_LOG_INPKT) {
1140 for (mp = m; mp != NULL; mp = mp->m_next)
1142 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1146 cp = mtod(m, u_char *);
1147 adrs = PPP_ADDRESS(cp);
1148 ctrl = PPP_CONTROL(cp);
1149 proto = PPP_PROTOCOL(cp);
1151 if (m->m_flags & M_ERRMARK) {
1152 m->m_flags &= ~M_ERRMARK;
1154 sc->sc_flags |= SC_VJ_RESET;
1160 * Decompress this packet if necessary, update the receiver's
1161 * dictionary, or take appropriate action on a CCP packet.
1163 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1164 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1165 /* decompress this packet */
1166 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1167 if (rv == DECOMP_OK) {
1170 /* no error, but no decompressed packet produced */
1174 cp = mtod(m, u_char *);
1175 proto = PPP_PROTOCOL(cp);
1179 * An error has occurred in decompression.
1180 * Pass the compressed packet up to pppd, which may take
1181 * CCP down or issue a Reset-Req.
1183 if (sc->sc_flags & SC_DEBUG)
1184 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1186 sc->sc_flags |= SC_VJ_RESET;
1187 if (rv == DECOMP_ERROR)
1188 sc->sc_flags |= SC_DC_ERROR;
1190 sc->sc_flags |= SC_DC_FERROR;
1195 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1196 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1198 if (proto == PPP_CCP) {
1205 for (mp = m; mp != NULL; mp = mp->m_next)
1209 if (sc->sc_flags & SC_VJ_RESET) {
1211 * If we've missed a packet, we must toss subsequent compressed
1212 * packets which don't have an explicit connection ID.
1215 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1217 sc->sc_flags &= ~SC_VJ_RESET;
1222 * See if we have a VJ-compressed packet to uncompress.
1224 if (proto == PPP_VJC_COMP) {
1225 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1228 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1229 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1230 sc->sc_comp, &iphdr, &hlen);
1233 if (sc->sc_flags & SC_DEBUG)
1234 printf("ppp%d: VJ uncompress failed on type comp\n",
1239 /* Copy the PPP and IP headers into a new mbuf. */
1240 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1245 if (hlen + PPP_HDRLEN > MHLEN) {
1246 MCLGET(mp, M_DONTWAIT);
1247 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1249 goto bad; /* lose if big headers and no clusters */
1252 cp = mtod(mp, u_char *);
1258 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1259 mp->m_len = hlen + PPP_HDRLEN;
1262 * Trim the PPP and VJ headers off the old mbuf
1263 * and stick the new and old mbufs together.
1265 m->m_data += PPP_HDRLEN + xlen;
1266 m->m_len -= PPP_HDRLEN + xlen;
1267 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1268 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1269 mp->m_len += m->m_len;
1270 MFREE(m, mp->m_next);
1274 ilen += hlen - xlen;
1276 } else if (proto == PPP_VJC_UNCOMP) {
1277 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1280 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1281 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1282 sc->sc_comp, &iphdr, &hlen);
1285 if (sc->sc_flags & SC_DEBUG)
1286 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1297 * If the packet will fit in a header mbuf, don't waste a
1298 * whole cluster on it.
1300 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1301 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1303 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1309 m->m_pkthdr.len = ilen;
1310 m->m_pkthdr.rcvif = ifp;
1313 * Record the time that we received this packet.
1315 if ((proto & 0x8000) == 0) {
1316 sc->sc_last_recv = time.tv_sec;
1320 /* See if bpf wants to look at the packet. */
1322 bpf_mtap(sc->sc_bpf, m);
1330 * IP packet - take off the ppp header and pass it up to IP.
1332 if ((ifp->if_flags & IFF_UP) == 0
1333 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1334 /* interface is down - drop the packet. */
1338 m->m_pkthdr.len -= PPP_HDRLEN;
1339 m->m_data += PPP_HDRLEN;
1340 m->m_len -= PPP_HDRLEN;
1341 schednetisr(NETISR_IP);
1348 * Some other protocol - place on input queue for read().
1356 * Put the packet on the appropriate input queue.
1359 if (IF_QFULL(inq)) {
1362 if (sc->sc_flags & SC_DEBUG)
1363 printf("ppp%d: input queue full\n", ifp->if_unit);
1370 ifp->if_ibytes += ilen;
1371 ifp->if_lastchange = time;
1380 sc->sc_if.if_ierrors++;
1381 sc->sc_stats.ppp_ierrors++;
1384 #define MAX_DUMP_BYTES 128
1390 char buf[3*MAX_DUMP_BYTES+4];
1393 static char digits[] = "0123456789abcdef";
1395 for (m = m0; m; m = m->m_next) {
1397 u_char *rptr = (u_char *)m->m_data;
1400 if (bp > buf + sizeof(buf) - 4)
1402 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1403 *bp++ = digits[*rptr++ & 0xf];
1407 if (bp > buf + sizeof(buf) - 3)
1417 printf("%s\n", buf);
1420 #endif /* NPPP > 0 */