2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Carnegie Mellon University
22 * Pittsburgh, PA 15213
27 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
29 * Copyright (c) 1987 Regents of the University of California.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms are permitted
33 * provided that the above copyright notice and this paragraph are
34 * duplicated in all such forms and that any documentation,
35 * advertising materials, and other materials related to such
36 * distribution and use acknowledge that the software was developed
37 * by the University of California, Berkeley. The name of the
38 * University may not be used to endorse or promote products derived
39 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
44 * Serial Line interface
47 * Center for Seismic Studies
48 * 1300 N 17th Street, Suite 1450
49 * Arlington, Virginia 22209
54 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
55 * Converted to 4.3BSD Beta by Chris Torek.
56 * Other changes made at Berkeley, based in part on code by Kirk Smith.
58 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59 * Added VJ tcp header compression; more unified ioctls
61 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
62 * Cleaned up a lot of the mbuf-related code to fix bugs that
63 * caused system crashes and packet corruption. Changed pppstart
64 * so that it doesn't just give up with a collision if the whole
65 * packet doesn't fit in the output ring buffer.
67 * Added priority queueing for interactive IP packets, following
68 * the model of if_sl.c, plus hooks for bpf.
69 * Paul Mackerras (paulus@cs.anu.edu.au).
72 /* $Id: if_ppp.c,v 1.13 1998/02/04 01:36:39 paulus Exp $ */
73 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
74 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
82 #include <sys/param.h>
83 #include <sys/systm.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/kernel.h>
90 #include <sys/malloc.h>
93 #include <net/if_types.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
101 #include <netinet/in.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip.h>
107 #include "bpfilter.h"
113 #include <net/pppcompress.h>
116 #include <net/ppp_defs.h>
117 #include <net/if_ppp.h>
118 #include <net/if_pppvar.h>
119 #include <machine/cpu.h>
121 #define splsoftnet splnet
124 /* This definition should be moved to net/netisr.h */
125 #define NETISR_PPP 26 /* PPP software interrupt */
129 #define PACKETPTR struct mbuf *
130 #include <net/ppp-comp.h>
133 static int pppsioctl __P((struct ifnet *, int, caddr_t));
134 static void ppp_requeue __P((struct ppp_softc *));
135 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
136 static void ppp_ccp_closed __P((struct ppp_softc *));
137 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
138 static void pppdumpm __P((struct mbuf *m0));
141 * Some useful mbuf macros not in mbuf.h.
143 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
145 #define M_DATASTART(m) \
146 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
147 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
149 #define M_DATASIZE(m) \
150 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
151 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
154 * We steal two bits in the mbuf m_flags, to mark high-priority packets
155 * for output, and received packets following lost/corrupted packets.
157 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
158 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
163 * List of compressors we know about.
164 * We leave some space so maybe we can modload compressors.
167 extern struct compressor ppp_bsd_compress;
168 extern struct compressor ppp_deflate;
170 struct compressor *ppp_compressors[8] = {
179 #endif /* PPP_COMPRESS */
181 TEXT_SET(pseudo_set, pppattach);
184 * Called from boot code to establish ppp interfaces.
189 register struct ppp_softc *sc;
191 extern void (*netisrs[])__P((void));
193 for (sc = ppp_softc; i < NPPP; sc++) {
194 sc->sc_if.if_name = "ppp";
195 sc->sc_if.if_unit = i++;
196 sc->sc_if.if_mtu = PPP_MTU;
197 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
198 sc->sc_if.if_type = IFT_PPP;
199 sc->sc_if.if_hdrlen = PPP_HDRLEN;
200 sc->sc_if.if_ioctl = pppsioctl;
201 sc->sc_if.if_output = pppoutput;
202 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
203 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
204 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
205 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
206 if_attach(&sc->sc_if);
208 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
211 netisrs[NETISR_PPP] = pppintr;
215 * Allocate a ppp interface unit and initialize it.
222 struct ppp_softc *sc;
224 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
225 if (sc->sc_xfer == pid) {
229 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
230 if (sc->sc_devp == NULL)
236 sc->sc_mru = PPP_MRU;
237 sc->sc_relinq = NULL;
238 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
240 MALLOC(sc->sc_comp, struct vjcompress *, sizeof(struct vjcompress),
243 vj_compress_init(sc->sc_comp, -1);
246 sc->sc_xc_state = NULL;
247 sc->sc_rc_state = NULL;
248 #endif /* PPP_COMPRESS */
249 for (i = 0; i < NUM_NP; ++i)
250 sc->sc_npmode[i] = NPMODE_ERROR;
251 sc->sc_npqueue = NULL;
252 sc->sc_npqtail = &sc->sc_npqueue;
253 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
259 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
263 struct ppp_softc *sc;
268 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
272 IF_DEQUEUE(&sc->sc_rawq, m);
278 IF_DEQUEUE(&sc->sc_inq, m);
284 IF_DEQUEUE(&sc->sc_fastq, m);
289 while ((m = sc->sc_npqueue) != NULL) {
290 sc->sc_npqueue = m->m_nextpkt;
293 if (sc->sc_togo != NULL) {
294 m_freem(sc->sc_togo);
299 sc->sc_xc_state = NULL;
300 sc->sc_rc_state = NULL;
301 #endif /* PPP_COMPRESS */
303 if (sc->sc_pass_filt.bf_insns != 0) {
304 FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
305 sc->sc_pass_filt.bf_insns = 0;
306 sc->sc_pass_filt.bf_len = 0;
308 if (sc->sc_active_filt.bf_insns != 0) {
309 FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
310 sc->sc_active_filt.bf_insns = 0;
311 sc->sc_active_filt.bf_len = 0;
313 #endif /* PPP_FILTER */
315 if (sc->sc_comp != 0) {
316 FREE(sc->sc_comp, M_DEVBUF);
323 * Ioctl routine for generic ppp devices.
326 pppioctl(sc, cmd, data, flag, p)
327 struct ppp_softc *sc;
333 int s, error, flags, mru, nb, npx;
334 struct ppp_option_data *odp;
335 struct compressor **cp;
339 struct bpf_program *bp, *nbp;
340 struct bpf_insn *newcode, *oldcode;
342 #endif /* PPP_FILTER */
344 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
349 *(int *)data = sc->sc_inq.ifq_len;
353 *(int *)data = sc->sc_if.if_unit;
357 *(u_int *)data = sc->sc_flags;
361 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
363 flags = *(int *)data & SC_MASK;
366 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
370 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
375 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
378 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
383 *(int *)data = sc->sc_mru;
388 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
392 vj_compress_init(sc->sc_comp, *(int *)data);
399 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
401 sc->sc_xfer = p->p_pid;
405 case PPPIOCSCOMPRESS:
406 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
408 odp = (struct ppp_option_data *) data;
410 if (nb > sizeof(ccp_option))
411 nb = sizeof(ccp_option);
412 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
414 if (ccp_option[1] < 2) /* preliminary check on the length byte */
416 for (cp = ppp_compressors; *cp != NULL; ++cp)
417 if ((*cp)->compress_proto == ccp_option[0]) {
419 * Found a handler for the protocol - try to allocate
420 * a compressor or decompressor.
425 if (sc->sc_xc_state != NULL)
426 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
428 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
429 if (sc->sc_xc_state == NULL) {
430 if (sc->sc_flags & SC_DEBUG)
431 printf("ppp%d: comp_alloc failed\n",
436 sc->sc_flags &= ~SC_COMP_RUN;
440 if (sc->sc_rc_state != NULL)
441 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
443 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
444 if (sc->sc_rc_state == NULL) {
445 if (sc->sc_flags & SC_DEBUG)
446 printf("ppp%d: decomp_alloc failed\n",
451 sc->sc_flags &= ~SC_DECOMP_RUN;
456 if (sc->sc_flags & SC_DEBUG)
457 printf("ppp%d: no compressor for [%x %x %x], %x\n",
458 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
460 return (EINVAL); /* no handler found */
461 #endif /* PPP_COMPRESS */
465 npi = (struct npioctl *) data;
466 switch (npi->protocol) {
473 if (cmd == PPPIOCGNPMODE) {
474 npi->mode = sc->sc_npmode[npx];
476 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
478 if (npi->mode != sc->sc_npmode[npx]) {
480 sc->sc_npmode[npx] = npi->mode;
481 if (npi->mode != NPMODE_QUEUE) {
493 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
494 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
501 nbp = (struct bpf_program *) data;
502 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
504 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
505 if (newcodelen != 0) {
506 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
508 return EINVAL; /* or sumpin */
510 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
512 FREE(newcode, M_DEVBUF);
515 if (!bpf_validate(newcode, nbp->bf_len)) {
516 FREE(newcode, M_DEVBUF);
521 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
522 oldcode = bp->bf_insns;
524 bp->bf_len = nbp->bf_len;
525 bp->bf_insns = newcode;
528 FREE(oldcode, M_DEVBUF);
539 * Process an ioctl request to the ppp network interface.
542 pppsioctl(ifp, cmd, data)
543 register struct ifnet *ifp;
547 struct proc *p = curproc; /* XXX */
548 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
549 register struct ifaddr *ifa = (struct ifaddr *)data;
550 register struct ifreq *ifr = (struct ifreq *)data;
551 struct ppp_stats *psp;
553 struct ppp_comp_stats *pcp;
555 int s = splimp(), error = 0;
559 if ((ifp->if_flags & IFF_RUNNING) == 0)
560 ifp->if_flags &= ~IFF_UP;
564 if (ifa->ifa_addr->sa_family != AF_INET)
565 error = EAFNOSUPPORT;
569 if (ifa->ifa_addr->sa_family != AF_INET)
570 error = EAFNOSUPPORT;
574 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
576 sc->sc_if.if_mtu = ifr->ifr_mtu;
580 ifr->ifr_mtu = sc->sc_if.if_mtu;
586 error = EAFNOSUPPORT;
589 switch(ifr->ifr_addr.sa_family) {
595 error = EAFNOSUPPORT;
601 psp = &((struct ifpppstatsreq *) data)->stats;
602 bzero(psp, sizeof(*psp));
603 psp->p = sc->sc_stats;
604 #if defined(VJC) && !defined(SL_NO_STATS)
606 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
607 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
608 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
609 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
610 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
611 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
612 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
613 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
620 pcp = &((struct ifpppcstatsreq *) data)->stats;
621 bzero(pcp, sizeof(*pcp));
622 if (sc->sc_xc_state != NULL)
623 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
624 if (sc->sc_rc_state != NULL)
625 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
627 #endif /* PPP_COMPRESS */
637 * Queue a packet. Start transmission if not active.
638 * Packet is placed in Information field of PPP frame.
641 pppoutput(ifp, m0, dst, rtp)
644 struct sockaddr *dst;
647 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
648 int protocol, address, control;
657 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
658 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
659 error = ENETDOWN; /* sort of */
664 * Compute PPP header.
666 m0->m_flags &= ~M_HIGHPRI;
667 switch (dst->sa_family) {
670 address = PPP_ALLSTATIONS;
673 mode = sc->sc_npmode[NP_IP];
676 * If this packet has the "low delay" bit set in the IP header,
677 * put it on the fastq instead.
679 ip = mtod(m0, struct ip *);
680 if (ip->ip_tos & IPTOS_LOWDELAY)
681 m0->m_flags |= M_HIGHPRI;
685 address = PPP_ADDRESS(dst->sa_data);
686 control = PPP_CONTROL(dst->sa_data);
687 protocol = PPP_PROTOCOL(dst->sa_data);
691 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
692 error = EAFNOSUPPORT;
697 * Drop this packet, or return an error, if necessary.
699 if (mode == NPMODE_ERROR) {
703 if (mode == NPMODE_DROP) {
709 * Add PPP header. If no space in first mbuf, allocate another.
710 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
712 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
713 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
720 m0->m_data -= PPP_HDRLEN;
722 cp = mtod(m0, u_char *);
725 *cp++ = protocol >> 8;
726 *cp++ = protocol & 0xff;
727 m0->m_len += PPP_HDRLEN;
730 for (m = m0; m != 0; m = m->m_next)
733 if (sc->sc_flags & SC_LOG_OUTPKT) {
734 printf("ppp%d output: ", ifp->if_unit);
738 if ((protocol & 0x8000) == 0) {
741 * Apply the pass and active filters to the packet,
742 * but only if it is a data packet.
744 *mtod(m0, u_char *) = 1; /* indicates outbound */
745 if (sc->sc_pass_filt.bf_insns != 0
746 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
748 error = 0; /* drop this packet */
753 * Update the time we sent the most recent packet.
755 if (sc->sc_active_filt.bf_insns == 0
756 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
757 sc->sc_last_sent = time.tv_sec;
759 *mtod(m0, u_char *) = address;
762 * Update the time we sent the most recent data packet.
764 sc->sc_last_sent = time.tv_sec;
765 #endif /* PPP_FILTER */
770 * See if bpf wants to look at the packet.
773 bpf_mtap(sc->sc_bpf, m0);
777 * Put the packet on the appropriate queue.
780 if (mode == NPMODE_QUEUE) {
781 /* XXX we should limit the number of packets on this queue */
782 *sc->sc_npqtail = m0;
783 m0->m_nextpkt = NULL;
784 sc->sc_npqtail = &m0->m_nextpkt;
786 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
787 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
790 sc->sc_if.if_oerrors++;
791 sc->sc_stats.ppp_oerrors++;
798 ifp->if_lastchange = time;
800 ifp->if_obytes += len;
811 * After a change in the NPmode for some NP, move packets from the
812 * npqueue to the send queue or the fast queue as appropriate.
813 * Should be called at splsoftnet.
817 struct ppp_softc *sc;
819 struct mbuf *m, **mpp;
823 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
824 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
826 mode = sc->sc_npmode[NP_IP];
835 * This packet can now go on one of the queues to be sent.
839 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
842 sc->sc_if.if_oerrors++;
843 sc->sc_stats.ppp_oerrors++;
859 sc->sc_npqtail = mpp;
863 * Transmitter has finished outputting some stuff;
864 * remember to call sc->sc_start later at splsoftnet.
868 struct ppp_softc *sc;
872 sc->sc_flags &= ~SC_TBUSY;
873 schednetisr(NETISR_PPP);
878 * Get a packet to send. This procedure is intended to be called at
879 * splsoftnet, since it may involve time-consuming operations such as
880 * applying VJ compression, packet compression, address/control and/or
881 * protocol field compression to the packet.
885 struct ppp_softc *sc;
889 int address, control, protocol;
892 * Grab a packet to send: first try the fast queue, then the
895 IF_DEQUEUE(&sc->sc_fastq, m);
897 IF_DEQUEUE(&sc->sc_if.if_snd, m);
901 ++sc->sc_stats.ppp_opackets;
904 * Extract the ppp header of the new packet.
905 * The ppp header will be in one mbuf.
907 cp = mtod(m, u_char *);
908 address = PPP_ADDRESS(cp);
909 control = PPP_CONTROL(cp);
910 protocol = PPP_PROTOCOL(cp);
916 * If the packet is a TCP/IP packet, see if we can compress it.
918 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
923 ip = (struct ip *) (cp + PPP_HDRLEN);
924 if (mp->m_len <= PPP_HDRLEN) {
928 ip = mtod(mp, struct ip *);
930 /* this code assumes the IP/TCP header is in one non-shared mbuf */
931 if (ip->ip_p == IPPROTO_TCP) {
932 type = vj_compress_tcp(mp, ip, sc->sc_comp,
933 !(sc->sc_flags & SC_NO_TCP_CCID));
935 case TYPE_UNCOMPRESSED_TCP:
936 protocol = PPP_VJC_UNCOMP;
938 case TYPE_COMPRESSED_TCP:
939 protocol = PPP_VJC_COMP;
940 cp = mtod(m, u_char *);
941 cp[0] = address; /* header has moved */
946 cp[3] = protocol; /* update protocol in PPP header */
956 #endif /* PPP_COMPRESS */
960 if (protocol != PPP_LCP && protocol != PPP_CCP
961 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
962 struct mbuf *mcomp = NULL;
966 for (mp = m; mp != NULL; mp = mp->m_next)
968 clen = (*sc->sc_xcomp->compress)
969 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
971 if (sc->sc_flags & SC_CCP_UP) {
972 /* Send the compressed packet instead of the original. */
975 cp = mtod(m, u_char *);
978 /* Can't transmit compressed packets until CCP is up. */
983 #endif /* PPP_COMPRESS */
986 * Compress the address/control and protocol, if possible.
988 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
989 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
990 protocol != PPP_LCP) {
991 /* can compress address/control */
995 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
996 /* can compress protocol */
997 if (mtod(m, u_char *) == cp) {
998 cp[2] = cp[1]; /* move address/control up */
1009 * Software interrupt routine, called at splsoftnet.
1014 struct ppp_softc *sc;
1020 for (i = 0; i < NPPP; ++i, ++sc) {
1021 if (!(sc->sc_flags & SC_TBUSY)
1022 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1024 sc->sc_flags |= SC_TBUSY;
1026 (*sc->sc_start)(sc);
1030 IF_DEQUEUE(&sc->sc_rawq, m);
1042 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1043 * 0 if it is about to be transmitted.
1046 ppp_ccp(sc, m, rcvd)
1047 struct ppp_softc *sc;
1056 * Get a pointer to the data after the PPP header.
1058 if (m->m_len <= PPP_HDRLEN) {
1062 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1065 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1068 ep = mtod(mp, u_char *) + mp->m_len;
1069 if (dp + CCP_HDRLEN > ep)
1071 slen = CCP_LENGTH(dp);
1072 if (dp + slen > ep) {
1073 if (sc->sc_flags & SC_DEBUG)
1074 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1075 dp, slen, mtod(mp, u_char *), mp->m_len);
1079 switch (CCP_CODE(dp)) {
1083 /* CCP must be going down - disable compression */
1084 if (sc->sc_flags & SC_CCP_UP) {
1086 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1092 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1093 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1094 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1096 /* we're agreeing to send compressed packets. */
1097 if (sc->sc_xc_state != NULL
1098 && (*sc->sc_xcomp->comp_init)
1099 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1100 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1102 sc->sc_flags |= SC_COMP_RUN;
1106 /* peer is agreeing to send compressed packets. */
1107 if (sc->sc_rc_state != NULL
1108 && (*sc->sc_rcomp->decomp_init)
1109 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1110 sc->sc_if.if_unit, 0, sc->sc_mru,
1111 sc->sc_flags & SC_DEBUG)) {
1113 sc->sc_flags |= SC_DECOMP_RUN;
1114 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1122 if (sc->sc_flags & SC_CCP_UP) {
1124 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1125 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1127 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1128 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1130 sc->sc_flags &= ~SC_DC_ERROR;
1140 * CCP is down; free (de)compressor state if necessary.
1144 struct ppp_softc *sc;
1146 if (sc->sc_xc_state) {
1147 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1148 sc->sc_xc_state = NULL;
1150 if (sc->sc_rc_state) {
1151 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1152 sc->sc_rc_state = NULL;
1155 #endif /* PPP_COMPRESS */
1158 * PPP packet input routine.
1159 * The caller has checked and removed the FCS and has inserted
1160 * the address/control bytes and the protocol high byte if they
1164 ppppktin(sc, m, lost)
1165 struct ppp_softc *sc;
1172 m->m_flags |= M_ERRMARK;
1173 IF_ENQUEUE(&sc->sc_rawq, m);
1174 schednetisr(NETISR_PPP);
1179 * Process a received PPP packet, doing decompression as necessary.
1180 * Should be called at splsoftnet.
1182 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1183 TYPE_UNCOMPRESSED_TCP)
1187 struct ppp_softc *sc;
1190 struct ifnet *ifp = &sc->sc_if;
1191 struct ifqueue *inq;
1192 int s, ilen, xlen, proto, rv;
1193 u_char *cp, adrs, ctrl;
1194 struct mbuf *mp, *dmp = NULL;
1198 sc->sc_stats.ppp_ipackets++;
1200 if (sc->sc_flags & SC_LOG_INPKT) {
1202 for (mp = m; mp != NULL; mp = mp->m_next)
1204 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1208 cp = mtod(m, u_char *);
1209 adrs = PPP_ADDRESS(cp);
1210 ctrl = PPP_CONTROL(cp);
1211 proto = PPP_PROTOCOL(cp);
1213 if (m->m_flags & M_ERRMARK) {
1214 m->m_flags &= ~M_ERRMARK;
1216 sc->sc_flags |= SC_VJ_RESET;
1222 * Decompress this packet if necessary, update the receiver's
1223 * dictionary, or take appropriate action on a CCP packet.
1225 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1226 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1227 /* decompress this packet */
1228 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1229 if (rv == DECOMP_OK) {
1232 /* no error, but no decompressed packet produced */
1236 cp = mtod(m, u_char *);
1237 proto = PPP_PROTOCOL(cp);
1241 * An error has occurred in decompression.
1242 * Pass the compressed packet up to pppd, which may take
1243 * CCP down or issue a Reset-Req.
1245 if (sc->sc_flags & SC_DEBUG)
1246 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1248 sc->sc_flags |= SC_VJ_RESET;
1249 if (rv == DECOMP_ERROR)
1250 sc->sc_flags |= SC_DC_ERROR;
1252 sc->sc_flags |= SC_DC_FERROR;
1257 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1258 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1260 if (proto == PPP_CCP) {
1267 for (mp = m; mp != NULL; mp = mp->m_next)
1271 if (sc->sc_flags & SC_VJ_RESET) {
1273 * If we've missed a packet, we must toss subsequent compressed
1274 * packets which don't have an explicit connection ID.
1277 vj_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1279 sc->sc_flags &= ~SC_VJ_RESET;
1284 * See if we have a VJ-compressed packet to uncompress.
1286 if (proto == PPP_VJC_COMP) {
1287 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1290 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1291 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1292 sc->sc_comp, &iphdr, &hlen);
1295 if (sc->sc_flags & SC_DEBUG)
1296 printf("ppp%d: VJ uncompress failed on type comp\n",
1301 /* Copy the PPP and IP headers into a new mbuf. */
1302 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1307 if (hlen + PPP_HDRLEN > MHLEN) {
1308 MCLGET(mp, M_DONTWAIT);
1309 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1311 goto bad; /* lose if big headers and no clusters */
1314 cp = mtod(mp, u_char *);
1320 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1321 mp->m_len = hlen + PPP_HDRLEN;
1324 * Trim the PPP and VJ headers off the old mbuf
1325 * and stick the new and old mbufs together.
1327 m->m_data += PPP_HDRLEN + xlen;
1328 m->m_len -= PPP_HDRLEN + xlen;
1329 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1330 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1331 mp->m_len += m->m_len;
1332 MFREE(m, mp->m_next);
1336 ilen += hlen - xlen;
1338 } else if (proto == PPP_VJC_UNCOMP) {
1339 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1342 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1343 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1344 sc->sc_comp, &iphdr, &hlen);
1347 if (sc->sc_flags & SC_DEBUG)
1348 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1359 * If the packet will fit in a header mbuf, don't waste a
1360 * whole cluster on it.
1362 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1363 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1365 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1371 m->m_pkthdr.len = ilen;
1372 m->m_pkthdr.rcvif = ifp;
1374 if ((proto & 0x8000) == 0) {
1377 * See whether we want to pass this packet, and
1378 * if it counts as link activity.
1380 adrs = *mtod(m, u_char *); /* save address field */
1381 *mtod(m, u_char *) = 0; /* indicate inbound */
1382 if (sc->sc_pass_filt.bf_insns != 0
1383 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1385 /* drop this packet */
1389 if (sc->sc_active_filt.bf_insns == 0
1390 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1391 sc->sc_last_recv = time.tv_sec;
1393 *mtod(m, u_char *) = adrs;
1396 * Record the time that we received this packet.
1398 sc->sc_last_recv = time.tv_sec;
1399 #endif /* PPP_FILTER */
1403 /* See if bpf wants to look at the packet. */
1405 bpf_mtap(sc->sc_bpf, m);
1413 * IP packet - take off the ppp header and pass it up to IP.
1415 if ((ifp->if_flags & IFF_UP) == 0
1416 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1417 /* interface is down - drop the packet. */
1421 m->m_pkthdr.len -= PPP_HDRLEN;
1422 m->m_data += PPP_HDRLEN;
1423 m->m_len -= PPP_HDRLEN;
1424 schednetisr(NETISR_IP);
1431 * Some other protocol - place on input queue for read().
1439 * Put the packet on the appropriate input queue.
1442 if (IF_QFULL(inq)) {
1445 if (sc->sc_flags & SC_DEBUG)
1446 printf("ppp%d: input queue full\n", ifp->if_unit);
1453 ifp->if_ibytes += ilen;
1454 ifp->if_lastchange = time;
1463 sc->sc_if.if_ierrors++;
1464 sc->sc_stats.ppp_ierrors++;
1467 #define MAX_DUMP_BYTES 128
1473 char buf[3*MAX_DUMP_BYTES+4];
1476 static char digits[] = "0123456789abcdef";
1478 for (m = m0; m; m = m->m_next) {
1480 u_char *rptr = (u_char *)m->m_data;
1483 if (bp > buf + sizeof(buf) - 4)
1485 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1486 *bp++ = digits[*rptr++ & 0xf];
1490 if (bp > buf + sizeof(buf) - 3)
1500 printf("%s\n", buf);
1503 #endif /* NPPP > 0 */