1 /* $Id: if_ppp.c,v 1.7 1997/04/30 05:46:49 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>
106 #include <netinet/in.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip.h>
112 #include "bpfilter.h"
114 #include <sys/time.h>
119 #include <net/slcompress.h>
122 #include <net/ppp_defs.h>
123 #include <net/if_ppp.h>
124 #include <net/if_pppvar.h>
125 #include <machine/cpu.h>
128 #define splsoftnet splnet
132 #define PACKETPTR struct mbuf *
133 #include <net/ppp-comp.h>
136 static int pppsioctl __P((struct ifnet *, u_long, caddr_t));
137 static void ppp_requeue __P((struct ppp_softc *));
138 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
139 static void ppp_ccp_closed __P((struct ppp_softc *));
140 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
141 static void pppdumpm __P((struct mbuf *m0));
144 * Some useful mbuf macros not in mbuf.h.
146 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
148 #define M_DATASTART(m) \
149 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
150 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
152 #define M_DATASIZE(m) \
153 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
154 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
157 * We steal two bits in the mbuf m_flags, to mark high-priority packets
158 * for output, and received packets following lost/corrupted packets.
160 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
161 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
166 * List of compressors we know about.
167 * We leave some space so maybe we can modload compressors.
170 extern struct compressor ppp_bsd_compress;
171 extern struct compressor ppp_deflate;
173 struct compressor *ppp_compressors[8] = {
182 #endif /* PPP_COMPRESS */
186 * Called from boot code to establish ppp interfaces.
191 register struct ppp_softc *sc;
194 for (sc = ppp_softc; i < NPPP; sc++) {
195 sc->sc_if.if_name = "ppp";
196 sc->sc_if.if_unit = i++;
197 sc->sc_if.if_mtu = PPP_MTU;
198 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
199 sc->sc_if.if_type = IFT_PPP;
200 sc->sc_if.if_hdrlen = PPP_HDRLEN;
201 sc->sc_if.if_ioctl = pppsioctl;
202 sc->sc_if.if_output = pppoutput;
203 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
204 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
205 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
206 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
207 if_attach(&sc->sc_if);
209 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
213 #if NetBSD1_0 && defined(i386)
215 * XXX kludge to fix the bug in the i386 interrupt handling code,
216 * where software interrupts could be taken while hardware
217 * interrupts were blocked.
219 if ((imask[IPL_TTY] & (1 << SIR_NET)) == 0) {
220 imask[IPL_TTY] |= (1 << SIR_NET);
221 intr_calculatemasks();
227 * Allocate a ppp interface unit and initialize it.
234 struct ppp_softc *sc;
236 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
237 if (sc->sc_xfer == pid) {
241 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
242 if (sc->sc_devp == NULL)
248 sc->sc_mru = PPP_MRU;
249 sc->sc_relinq = NULL;
250 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
252 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
255 sl_compress_init(sc->sc_comp, -1);
258 sc->sc_xc_state = NULL;
259 sc->sc_rc_state = NULL;
260 #endif /* PPP_COMPRESS */
261 for (i = 0; i < NUM_NP; ++i)
262 sc->sc_npmode[i] = NPMODE_ERROR;
263 sc->sc_npqueue = NULL;
264 sc->sc_npqtail = &sc->sc_npqueue;
265 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
271 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
275 struct ppp_softc *sc;
280 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
284 IF_DEQUEUE(&sc->sc_rawq, m);
290 IF_DEQUEUE(&sc->sc_inq, m);
296 IF_DEQUEUE(&sc->sc_fastq, m);
301 while ((m = sc->sc_npqueue) != NULL) {
302 sc->sc_npqueue = m->m_nextpkt;
305 if (sc->sc_togo != NULL) {
306 m_freem(sc->sc_togo);
311 sc->sc_xc_state = NULL;
312 sc->sc_rc_state = NULL;
313 #endif /* PPP_COMPRESS */
315 if (sc->sc_pass_filt.bf_insns != 0) {
316 FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
317 sc->sc_pass_filt.bf_insns = 0;
318 sc->sc_pass_filt.bf_len = 0;
320 if (sc->sc_active_filt.bf_insns != 0) {
321 FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
322 sc->sc_active_filt.bf_insns = 0;
323 sc->sc_active_filt.bf_len = 0;
325 #endif /* PPP_FILTER */
327 if (sc->sc_comp != 0) {
328 FREE(sc->sc_comp, M_DEVBUF);
335 * Ioctl routine for generic ppp devices.
338 pppioctl(sc, cmd, data, flag, p)
339 struct ppp_softc *sc;
345 int s, error, flags, mru, nb, npx;
346 struct ppp_option_data *odp;
347 struct compressor **cp;
351 struct bpf_program *bp, *nbp;
352 struct bpf_insn *newcode, *oldcode;
354 #endif /* PPP_FILTER */
356 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
361 *(int *)data = sc->sc_inq.ifq_len;
365 *(int *)data = sc->sc_if.if_unit;
369 *(u_int *)data = sc->sc_flags;
373 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
375 flags = *(int *)data & SC_MASK;
378 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
382 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
387 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
390 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
395 *(int *)data = sc->sc_mru;
400 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
404 sl_compress_init(sc->sc_comp, *(int *)data);
411 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
413 sc->sc_xfer = p->p_pid;
417 case PPPIOCSCOMPRESS:
418 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
420 odp = (struct ppp_option_data *) data;
422 if (nb > sizeof(ccp_option))
423 nb = sizeof(ccp_option);
424 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
426 if (ccp_option[1] < 2) /* preliminary check on the length byte */
428 for (cp = ppp_compressors; *cp != NULL; ++cp)
429 if ((*cp)->compress_proto == ccp_option[0]) {
431 * Found a handler for the protocol - try to allocate
432 * a compressor or decompressor.
437 if (sc->sc_xc_state != NULL)
438 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
440 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
441 if (sc->sc_xc_state == NULL) {
442 if (sc->sc_flags & SC_DEBUG)
443 printf("ppp%d: comp_alloc failed\n",
448 sc->sc_flags &= ~SC_COMP_RUN;
452 if (sc->sc_rc_state != NULL)
453 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
455 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
456 if (sc->sc_rc_state == NULL) {
457 if (sc->sc_flags & SC_DEBUG)
458 printf("ppp%d: decomp_alloc failed\n",
463 sc->sc_flags &= ~SC_DECOMP_RUN;
468 if (sc->sc_flags & SC_DEBUG)
469 printf("ppp%d: no compressor for [%x %x %x], %x\n",
470 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
472 return (EINVAL); /* no handler found */
473 #endif /* PPP_COMPRESS */
477 npi = (struct npioctl *) data;
478 switch (npi->protocol) {
485 if (cmd == PPPIOCGNPMODE) {
486 npi->mode = sc->sc_npmode[npx];
488 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
490 if (npi->mode != sc->sc_npmode[npx]) {
492 sc->sc_npmode[npx] = npi->mode;
493 if (npi->mode != NPMODE_QUEUE) {
505 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
506 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
513 nbp = (struct bpf_program *) data;
514 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
516 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
517 if (newcodelen != 0) {
518 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
520 return EINVAL; /* or sumpin */
522 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
524 FREE(newcode, M_DEVBUF);
527 if (!bpf_validate(newcode, nbp->bf_len)) {
528 FREE(newcode, M_DEVBUF);
533 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
534 oldcode = bp->bf_insns;
536 bp->bf_len = nbp->bf_len;
537 bp->bf_insns = newcode;
540 FREE(oldcode, M_DEVBUF);
551 * Process an ioctl request to the ppp network interface.
554 pppsioctl(ifp, cmd, data)
555 register struct ifnet *ifp;
559 struct proc *p = curproc; /* XXX */
560 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
561 register struct ifaddr *ifa = (struct ifaddr *)data;
562 register struct ifreq *ifr = (struct ifreq *)data;
563 struct ppp_stats *psp;
565 struct ppp_comp_stats *pcp;
567 int s = splimp(), error = 0;
571 if ((ifp->if_flags & IFF_RUNNING) == 0)
572 ifp->if_flags &= ~IFF_UP;
576 if (ifa->ifa_addr->sa_family != AF_INET)
577 error = EAFNOSUPPORT;
581 if (ifa->ifa_addr->sa_family != AF_INET)
582 error = EAFNOSUPPORT;
586 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
588 sc->sc_if.if_mtu = ifr->ifr_mtu;
592 ifr->ifr_mtu = sc->sc_if.if_mtu;
598 error = EAFNOSUPPORT;
601 switch(ifr->ifr_addr.sa_family) {
607 error = EAFNOSUPPORT;
613 psp = &((struct ifpppstatsreq *) data)->stats;
614 bzero(psp, sizeof(*psp));
615 psp->p = sc->sc_stats;
616 #if defined(VJC) && !defined(SL_NO_STATS)
618 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
619 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
620 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
621 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
622 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
623 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
624 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
625 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
632 pcp = &((struct ifpppcstatsreq *) data)->stats;
633 bzero(pcp, sizeof(*pcp));
634 if (sc->sc_xc_state != NULL)
635 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
636 if (sc->sc_rc_state != NULL)
637 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
639 #endif /* PPP_COMPRESS */
649 * Queue a packet. Start transmission if not active.
650 * Packet is placed in Information field of PPP frame.
653 pppoutput(ifp, m0, dst, rtp)
656 struct sockaddr *dst;
659 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
660 int protocol, address, control;
669 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
670 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
671 error = ENETDOWN; /* sort of */
676 * Compute PPP header.
678 m0->m_flags &= ~M_HIGHPRI;
679 switch (dst->sa_family) {
682 address = PPP_ALLSTATIONS;
685 mode = sc->sc_npmode[NP_IP];
688 * If this packet has the "low delay" bit set in the IP header,
689 * put it on the fastq instead.
691 ip = mtod(m0, struct ip *);
692 if (ip->ip_tos & IPTOS_LOWDELAY)
693 m0->m_flags |= M_HIGHPRI;
697 address = PPP_ADDRESS(dst->sa_data);
698 control = PPP_CONTROL(dst->sa_data);
699 protocol = PPP_PROTOCOL(dst->sa_data);
703 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
704 error = EAFNOSUPPORT;
709 * Drop this packet, or return an error, if necessary.
711 if (mode == NPMODE_ERROR) {
715 if (mode == NPMODE_DROP) {
721 * Add PPP header. If no space in first mbuf, allocate another.
722 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
724 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
725 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
732 m0->m_data -= PPP_HDRLEN;
734 cp = mtod(m0, u_char *);
737 *cp++ = protocol >> 8;
738 *cp++ = protocol & 0xff;
739 m0->m_len += PPP_HDRLEN;
742 for (m = m0; m != 0; m = m->m_next)
745 if (sc->sc_flags & SC_LOG_OUTPKT) {
746 printf("ppp%d output: ", ifp->if_unit);
750 if ((protocol & 0x8000) == 0) {
753 * Apply the pass and active filters to the packet,
754 * but only if it is a data packet.
756 *mtod(m0, u_char *) = 1; /* indicates outbound */
757 if (sc->sc_pass_filt.bf_insns != 0
758 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
760 error = 0; /* drop this packet */
765 * Update the time we sent the most recent packet.
767 if (sc->sc_active_filt.bf_insns == 0
768 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
769 sc->sc_last_sent = time.tv_sec;
771 *mtod(m0, u_char *) = address;
774 * Update the time we sent the most recent data packet.
776 sc->sc_last_sent = time.tv_sec;
777 #endif /* PPP_FILTER */
782 * See if bpf wants to look at the packet.
785 bpf_mtap(sc->sc_bpf, m0);
789 * Put the packet on the appropriate queue.
792 if (mode == NPMODE_QUEUE) {
793 /* XXX we should limit the number of packets on this queue */
794 *sc->sc_npqtail = m0;
795 m0->m_nextpkt = NULL;
796 sc->sc_npqtail = &m0->m_nextpkt;
798 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
799 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
802 sc->sc_if.if_oerrors++;
803 sc->sc_stats.ppp_oerrors++;
810 ifp->if_lastchange = time;
812 ifp->if_obytes += len;
823 * After a change in the NPmode for some NP, move packets from the
824 * npqueue to the send queue or the fast queue as appropriate.
825 * Should be called at splsoftnet.
829 struct ppp_softc *sc;
831 struct mbuf *m, **mpp;
835 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
836 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
838 mode = sc->sc_npmode[NP_IP];
847 * This packet can now go on one of the queues to be sent.
851 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
854 sc->sc_if.if_oerrors++;
855 sc->sc_stats.ppp_oerrors++;
871 sc->sc_npqtail = mpp;
875 * Transmitter has finished outputting some stuff;
876 * remember to call sc->sc_start later at splsoftnet.
880 struct ppp_softc *sc;
884 sc->sc_flags &= ~SC_TBUSY;
885 schednetisr(NETISR_PPP);
890 * Get a packet to send. This procedure is intended to be called at
891 * splsoftnet, since it may involve time-consuming operations such as
892 * applying VJ compression, packet compression, address/control and/or
893 * protocol field compression to the packet.
897 struct ppp_softc *sc;
901 int address, control, protocol;
904 * Grab a packet to send: first try the fast queue, then the
907 IF_DEQUEUE(&sc->sc_fastq, m);
909 IF_DEQUEUE(&sc->sc_if.if_snd, m);
913 ++sc->sc_stats.ppp_opackets;
916 * Extract the ppp header of the new packet.
917 * The ppp header will be in one mbuf.
919 cp = mtod(m, u_char *);
920 address = PPP_ADDRESS(cp);
921 control = PPP_CONTROL(cp);
922 protocol = PPP_PROTOCOL(cp);
928 * If the packet is a TCP/IP packet, see if we can compress it.
930 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
935 ip = (struct ip *) (cp + PPP_HDRLEN);
936 if (mp->m_len <= PPP_HDRLEN) {
940 ip = mtod(mp, struct ip *);
942 /* this code assumes the IP/TCP header is in one non-shared mbuf */
943 if (ip->ip_p == IPPROTO_TCP) {
944 type = sl_compress_tcp(mp, ip, sc->sc_comp,
945 !(sc->sc_flags & SC_NO_TCP_CCID));
947 case TYPE_UNCOMPRESSED_TCP:
948 protocol = PPP_VJC_UNCOMP;
950 case TYPE_COMPRESSED_TCP:
951 protocol = PPP_VJC_COMP;
952 cp = mtod(m, u_char *);
953 cp[0] = address; /* header has moved */
958 cp[3] = protocol; /* update protocol in PPP header */
968 #endif /* PPP_COMPRESS */
972 if (protocol != PPP_LCP && protocol != PPP_CCP
973 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
974 struct mbuf *mcomp = NULL;
978 for (mp = m; mp != NULL; mp = mp->m_next)
980 clen = (*sc->sc_xcomp->compress)
981 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
983 if (sc->sc_flags & SC_CCP_UP) {
984 /* Send the compressed packet instead of the original. */
987 cp = mtod(m, u_char *);
990 /* Can't transmit compressed packets until CCP is up. */
995 #endif /* PPP_COMPRESS */
998 * Compress the address/control and protocol, if possible.
1000 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1001 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1002 protocol != PPP_LCP) {
1003 /* can compress address/control */
1007 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1008 /* can compress protocol */
1009 if (mtod(m, u_char *) == cp) {
1010 cp[2] = cp[1]; /* move address/control up */
1021 * Software interrupt routine, called at splsoftnet.
1026 struct ppp_softc *sc;
1032 for (i = 0; i < NPPP; ++i, ++sc) {
1033 if (!(sc->sc_flags & SC_TBUSY)
1034 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1036 sc->sc_flags |= SC_TBUSY;
1038 (*sc->sc_start)(sc);
1042 IF_DEQUEUE(&sc->sc_rawq, m);
1054 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1055 * 0 if it is about to be transmitted.
1058 ppp_ccp(sc, m, rcvd)
1059 struct ppp_softc *sc;
1068 * Get a pointer to the data after the PPP header.
1070 if (m->m_len <= PPP_HDRLEN) {
1074 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1077 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1080 ep = mtod(mp, u_char *) + mp->m_len;
1081 if (dp + CCP_HDRLEN > ep)
1083 slen = CCP_LENGTH(dp);
1084 if (dp + slen > ep) {
1085 if (sc->sc_flags & SC_DEBUG)
1086 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1087 dp, slen, mtod(mp, u_char *), mp->m_len);
1091 switch (CCP_CODE(dp)) {
1095 /* CCP must be going down - disable compression */
1096 if (sc->sc_flags & SC_CCP_UP) {
1098 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1104 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1105 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1106 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1108 /* we're agreeing to send compressed packets. */
1109 if (sc->sc_xc_state != NULL
1110 && (*sc->sc_xcomp->comp_init)
1111 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1112 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1114 sc->sc_flags |= SC_COMP_RUN;
1118 /* peer is agreeing to send compressed packets. */
1119 if (sc->sc_rc_state != NULL
1120 && (*sc->sc_rcomp->decomp_init)
1121 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1122 sc->sc_if.if_unit, 0, sc->sc_mru,
1123 sc->sc_flags & SC_DEBUG)) {
1125 sc->sc_flags |= SC_DECOMP_RUN;
1126 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1134 if (sc->sc_flags & SC_CCP_UP) {
1136 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1137 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1139 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1140 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1142 sc->sc_flags &= ~SC_DC_ERROR;
1152 * CCP is down; free (de)compressor state if necessary.
1156 struct ppp_softc *sc;
1158 if (sc->sc_xc_state) {
1159 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1160 sc->sc_xc_state = NULL;
1162 if (sc->sc_rc_state) {
1163 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1164 sc->sc_rc_state = NULL;
1167 #endif /* PPP_COMPRESS */
1170 * PPP packet input routine.
1171 * The caller has checked and removed the FCS and has inserted
1172 * the address/control bytes and the protocol high byte if they
1176 ppppktin(sc, m, lost)
1177 struct ppp_softc *sc;
1184 m->m_flags |= M_ERRMARK;
1185 IF_ENQUEUE(&sc->sc_rawq, m);
1186 schednetisr(NETISR_PPP);
1191 * Process a received PPP packet, doing decompression as necessary.
1192 * Should be called at splsoftnet.
1194 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1195 TYPE_UNCOMPRESSED_TCP)
1199 struct ppp_softc *sc;
1202 struct ifnet *ifp = &sc->sc_if;
1203 struct ifqueue *inq;
1204 int s, ilen, xlen, proto, rv;
1205 u_char *cp, adrs, ctrl;
1206 struct mbuf *mp, *dmp = NULL;
1210 sc->sc_stats.ppp_ipackets++;
1212 if (sc->sc_flags & SC_LOG_INPKT) {
1214 for (mp = m; mp != NULL; mp = mp->m_next)
1216 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1220 cp = mtod(m, u_char *);
1221 adrs = PPP_ADDRESS(cp);
1222 ctrl = PPP_CONTROL(cp);
1223 proto = PPP_PROTOCOL(cp);
1225 if (m->m_flags & M_ERRMARK) {
1226 m->m_flags &= ~M_ERRMARK;
1228 sc->sc_flags |= SC_VJ_RESET;
1234 * Decompress this packet if necessary, update the receiver's
1235 * dictionary, or take appropriate action on a CCP packet.
1237 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1238 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1239 /* decompress this packet */
1240 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1241 if (rv == DECOMP_OK) {
1244 /* no error, but no decompressed packet produced */
1248 cp = mtod(m, u_char *);
1249 proto = PPP_PROTOCOL(cp);
1253 * An error has occurred in decompression.
1254 * Pass the compressed packet up to pppd, which may take
1255 * CCP down or issue a Reset-Req.
1257 if (sc->sc_flags & SC_DEBUG)
1258 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1260 sc->sc_flags |= SC_VJ_RESET;
1261 if (rv == DECOMP_ERROR)
1262 sc->sc_flags |= SC_DC_ERROR;
1264 sc->sc_flags |= SC_DC_FERROR;
1269 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1270 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1272 if (proto == PPP_CCP) {
1279 for (mp = m; mp != NULL; mp = mp->m_next)
1283 if (sc->sc_flags & SC_VJ_RESET) {
1285 * If we've missed a packet, we must toss subsequent compressed
1286 * packets which don't have an explicit connection ID.
1289 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1291 sc->sc_flags &= ~SC_VJ_RESET;
1296 * See if we have a VJ-compressed packet to uncompress.
1298 if (proto == PPP_VJC_COMP) {
1299 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1302 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1303 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1304 sc->sc_comp, &iphdr, &hlen);
1307 if (sc->sc_flags & SC_DEBUG)
1308 printf("ppp%d: VJ uncompress failed on type comp\n",
1313 /* Copy the PPP and IP headers into a new mbuf. */
1314 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1319 if (hlen + PPP_HDRLEN > MHLEN) {
1320 MCLGET(mp, M_DONTWAIT);
1321 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1323 goto bad; /* lose if big headers and no clusters */
1326 cp = mtod(mp, u_char *);
1332 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1333 mp->m_len = hlen + PPP_HDRLEN;
1336 * Trim the PPP and VJ headers off the old mbuf
1337 * and stick the new and old mbufs together.
1339 m->m_data += PPP_HDRLEN + xlen;
1340 m->m_len -= PPP_HDRLEN + xlen;
1341 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1342 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1343 mp->m_len += m->m_len;
1344 MFREE(m, mp->m_next);
1348 ilen += hlen - xlen;
1350 } else if (proto == PPP_VJC_UNCOMP) {
1351 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1354 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1355 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1356 sc->sc_comp, &iphdr, &hlen);
1359 if (sc->sc_flags & SC_DEBUG)
1360 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1371 * If the packet will fit in a header mbuf, don't waste a
1372 * whole cluster on it.
1374 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1375 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1377 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1383 m->m_pkthdr.len = ilen;
1384 m->m_pkthdr.rcvif = ifp;
1386 if ((proto & 0x8000) == 0) {
1389 * See whether we want to pass this packet, and
1390 * if it counts as link activity.
1392 adrs = *mtod(m, u_char *); /* save address field */
1393 *mtod(m, u_char *) = 0; /* indicate inbound */
1394 if (sc->sc_pass_filt.bf_insns != 0
1395 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1397 /* drop this packet */
1401 if (sc->sc_active_filt.bf_insns == 0
1402 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1403 sc->sc_last_recv = time.tv_sec;
1405 *mtod(m, u_char *) = adrs;
1408 * Record the time that we received this packet.
1410 sc->sc_last_recv = time.tv_sec;
1411 #endif /* PPP_FILTER */
1415 /* See if bpf wants to look at the packet. */
1417 bpf_mtap(sc->sc_bpf, m);
1425 * IP packet - take off the ppp header and pass it up to IP.
1427 if ((ifp->if_flags & IFF_UP) == 0
1428 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1429 /* interface is down - drop the packet. */
1433 m->m_pkthdr.len -= PPP_HDRLEN;
1434 m->m_data += PPP_HDRLEN;
1435 m->m_len -= PPP_HDRLEN;
1436 schednetisr(NETISR_IP);
1443 * Some other protocol - place on input queue for read().
1451 * Put the packet on the appropriate input queue.
1454 if (IF_QFULL(inq)) {
1457 if (sc->sc_flags & SC_DEBUG)
1458 printf("ppp%d: input queue full\n", ifp->if_unit);
1465 ifp->if_ibytes += ilen;
1466 ifp->if_lastchange = time;
1475 sc->sc_if.if_ierrors++;
1476 sc->sc_stats.ppp_ierrors++;
1479 #define MAX_DUMP_BYTES 128
1485 char buf[3*MAX_DUMP_BYTES+4];
1488 static char digits[] = "0123456789abcdef";
1490 for (m = m0; m; m = m->m_next) {
1492 u_char *rptr = (u_char *)m->m_data;
1495 if (bp > buf + sizeof(buf) - 4)
1497 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1498 *bp++ = digits[*rptr++ & 0xf];
1502 if (bp > buf + sizeof(buf) - 3)
1512 printf("%s\n", buf);
1515 #endif /* NPPP > 0 */