1 /* $NetBSD: if_ppp.c,v 1.39 1997/05/17 21:11:59 christos Exp $ */
2 /* Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp */
5 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
7 * Copyright (c) 1989 Carnegie Mellon University.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that the above copyright notice and this paragraph are
12 * duplicated in all such forms and that any documentation,
13 * advertising materials, and other materials related to such
14 * distribution and use acknowledge that the software was developed
15 * by Carnegie Mellon University. The name of the
16 * University may not be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * Carnegie Mellon University
25 * Pittsburgh, PA 15213
30 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
32 * Copyright (c) 1987 Regents of the University of California.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms are permitted
36 * provided that the above copyright notice and this paragraph are
37 * duplicated in all such forms and that any documentation,
38 * advertising materials, and other materials related to such
39 * distribution and use acknowledge that the software was developed
40 * by the University of California, Berkeley. The name of the
41 * University may not be used to endorse or promote products derived
42 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
45 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
47 * Serial Line interface
50 * Center for Seismic Studies
51 * 1300 N 17th Street, Suite 1450
52 * Arlington, Virginia 22209
57 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
58 * Converted to 4.3BSD Beta by Chris Torek.
59 * Other changes made at Berkeley, based in part on code by Kirk Smith.
61 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
62 * Added VJ tcp header compression; more unified ioctls
64 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
65 * Cleaned up a lot of the mbuf-related code to fix bugs that
66 * caused system crashes and packet corruption. Changed pppstart
67 * so that it doesn't just give up with a collision if the whole
68 * packet doesn't fit in the output ring buffer.
70 * Added priority queueing for interactive IP packets, following
71 * the model of if_sl.c, plus hooks for bpf.
72 * Paul Mackerras (paulus@cs.anu.edu.au).
75 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
76 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
84 #include <sys/param.h>
87 #include <sys/socket.h>
88 #include <sys/ioctl.h>
89 #include <sys/kernel.h>
90 #include <sys/systm.h>
92 #include <sys/malloc.h>
95 #include <net/if_types.h>
96 #include <net/netisr.h>
97 #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"
111 #include <sys/time.h>
116 #include <net/slcompress.h>
119 #include <net/ppp_defs.h>
120 #include <net/if_ppp.h>
121 #include <net/if_pppvar.h>
122 #include <machine/cpu.h>
125 #define PACKETPTR struct mbuf *
126 #include <net/ppp-comp.h>
129 static int pppsioctl __P((struct ifnet *, u_long, caddr_t));
130 static void ppp_requeue __P((struct ppp_softc *));
131 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
132 static void ppp_ccp_closed __P((struct ppp_softc *));
133 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
134 static void pppdumpm __P((struct mbuf *m0));
137 * Some useful mbuf macros not in mbuf.h.
139 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
141 #define M_DATASTART(m) \
142 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
143 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
145 #define M_DATASIZE(m) \
146 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
147 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
150 * We steal two bits in the mbuf m_flags, to mark high-priority packets
151 * for output, and received packets following lost/corrupted packets.
153 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
154 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
159 * List of compressors we know about.
160 * We leave some space so maybe we can modload compressors.
163 extern struct compressor ppp_bsd_compress;
164 extern struct compressor ppp_deflate, ppp_deflate_draft;
166 struct compressor *ppp_compressors[8] = {
167 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
170 #if DO_DEFLATE && defined(PPP_DEFLATE)
176 #endif /* PPP_COMPRESS */
180 * Called from boot code to establish ppp interfaces.
185 register struct ppp_softc *sc;
188 for (sc = ppp_softc; i < NPPP; sc++) {
189 sc->sc_unit = i; /* XXX */
190 sprintf(sc->sc_if.if_xname, "ppp%d", i++);
191 sc->sc_if.if_softc = sc;
192 sc->sc_if.if_mtu = PPP_MTU;
193 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
194 sc->sc_if.if_type = IFT_PPP;
195 sc->sc_if.if_hdrlen = PPP_HDRLEN;
196 sc->sc_if.if_ioctl = pppsioctl;
197 sc->sc_if.if_output = pppoutput;
198 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
199 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
200 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
201 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
202 if_attach(&sc->sc_if);
204 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
210 * Allocate a ppp interface unit and initialize it.
217 struct ppp_softc *sc;
219 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
220 if (sc->sc_xfer == pid) {
224 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
225 if (sc->sc_devp == NULL)
231 sc->sc_mru = PPP_MRU;
232 sc->sc_relinq = NULL;
233 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
235 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
238 sl_compress_init(sc->sc_comp);
241 sc->sc_xc_state = NULL;
242 sc->sc_rc_state = NULL;
243 #endif /* PPP_COMPRESS */
244 for (i = 0; i < NUM_NP; ++i)
245 sc->sc_npmode[i] = NPMODE_ERROR;
246 sc->sc_npqueue = NULL;
247 sc->sc_npqtail = &sc->sc_npqueue;
248 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
254 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
258 struct ppp_softc *sc;
263 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
267 IF_DEQUEUE(&sc->sc_rawq, m);
273 IF_DEQUEUE(&sc->sc_inq, m);
279 IF_DEQUEUE(&sc->sc_fastq, m);
284 while ((m = sc->sc_npqueue) != NULL) {
285 sc->sc_npqueue = m->m_nextpkt;
288 if (sc->sc_togo != NULL) {
289 m_freem(sc->sc_togo);
294 sc->sc_xc_state = NULL;
295 sc->sc_rc_state = NULL;
296 #endif /* PPP_COMPRESS */
298 if (sc->sc_pass_filt.bf_insns != 0) {
299 FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
300 sc->sc_pass_filt.bf_insns = 0;
301 sc->sc_pass_filt.bf_len = 0;
303 if (sc->sc_active_filt.bf_insns != 0) {
304 FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
305 sc->sc_active_filt.bf_insns = 0;
306 sc->sc_active_filt.bf_len = 0;
308 #endif /* PPP_FILTER */
310 if (sc->sc_comp != 0) {
311 FREE(sc->sc_comp, M_DEVBUF);
318 * Ioctl routine for generic ppp devices.
321 pppioctl(sc, cmd, data, flag, p)
322 struct ppp_softc *sc;
328 int s, error, flags, mru, nb, npx;
329 struct ppp_option_data *odp;
330 struct compressor **cp;
334 struct bpf_program *bp, *nbp;
335 struct bpf_insn *newcode, *oldcode;
337 #endif /* PPP_FILTER */
339 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
344 *(int *)data = sc->sc_inq.ifq_len;
348 *(int *)data = sc->sc_unit; /* XXX */
352 *(u_int *)data = sc->sc_flags;
356 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
358 flags = *(int *)data & SC_MASK;
361 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
365 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
370 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
373 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
378 *(int *)data = sc->sc_mru;
383 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
387 sl_compress_setup(sc->sc_comp, *(int *)data);
394 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
396 sc->sc_xfer = p->p_pid;
400 case PPPIOCSCOMPRESS:
401 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
403 odp = (struct ppp_option_data *) data;
405 if (nb > sizeof(ccp_option))
406 nb = sizeof(ccp_option);
407 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
409 if (ccp_option[1] < 2) /* preliminary check on the length byte */
411 for (cp = ppp_compressors; *cp != NULL; ++cp)
412 if ((*cp)->compress_proto == ccp_option[0]) {
414 * Found a handler for the protocol - try to allocate
415 * a compressor or decompressor.
420 if (sc->sc_xc_state != NULL)
421 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
423 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
424 if (sc->sc_xc_state == NULL) {
425 if (sc->sc_flags & SC_DEBUG)
426 printf("%s: comp_alloc failed\n",
431 sc->sc_flags &= ~SC_COMP_RUN;
435 if (sc->sc_rc_state != NULL)
436 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
438 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
439 if (sc->sc_rc_state == NULL) {
440 if (sc->sc_flags & SC_DEBUG)
441 printf("%s: decomp_alloc failed\n",
446 sc->sc_flags &= ~SC_DECOMP_RUN;
451 if (sc->sc_flags & SC_DEBUG)
452 printf("%s: no compressor for [%x %x %x], %x\n",
453 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
455 return (EINVAL); /* no handler found */
456 #endif /* PPP_COMPRESS */
460 npi = (struct npioctl *) data;
461 switch (npi->protocol) {
468 if (cmd == PPPIOCGNPMODE) {
469 npi->mode = sc->sc_npmode[npx];
471 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
473 if (npi->mode != sc->sc_npmode[npx]) {
475 sc->sc_npmode[npx] = npi->mode;
476 if (npi->mode != NPMODE_QUEUE) {
488 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
489 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
496 nbp = (struct bpf_program *) data;
497 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
499 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
500 if (newcodelen != 0) {
501 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
503 return EINVAL; /* or sumpin */
505 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
507 FREE(newcode, M_DEVBUF);
510 if (!bpf_validate(newcode, nbp->bf_len)) {
511 FREE(newcode, M_DEVBUF);
516 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
517 oldcode = bp->bf_insns;
519 bp->bf_len = nbp->bf_len;
520 bp->bf_insns = newcode;
523 FREE(oldcode, M_DEVBUF);
534 * Process an ioctl request to the ppp network interface.
537 pppsioctl(ifp, cmd, data)
538 register struct ifnet *ifp;
542 register struct proc *p = curproc; /* XXX */
543 register struct ppp_softc *sc = ifp->if_softc;
544 register struct ifaddr *ifa = (struct ifaddr *)data;
545 register struct ifreq *ifr = (struct ifreq *)data;
546 struct ppp_stats *psp;
548 struct ppp_comp_stats *pcp;
550 int s = splimp(), error = 0;
554 if ((ifp->if_flags & IFF_RUNNING) == 0)
555 ifp->if_flags &= ~IFF_UP;
559 if (ifa->ifa_addr->sa_family != AF_INET)
560 error = EAFNOSUPPORT;
564 if (ifa->ifa_addr->sa_family != AF_INET)
565 error = EAFNOSUPPORT;
569 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
571 sc->sc_if.if_mtu = ifr->ifr_mtu;
575 ifr->ifr_mtu = sc->sc_if.if_mtu;
581 error = EAFNOSUPPORT;
584 switch(ifr->ifr_addr.sa_family) {
590 error = EAFNOSUPPORT;
596 psp = &((struct ifpppstatsreq *) data)->stats;
597 bzero(psp, sizeof(*psp));
598 psp->p = sc->sc_stats;
599 #if defined(VJC) && !defined(SL_NO_STATS)
601 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
602 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
603 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
604 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
605 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
606 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
607 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
608 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
615 pcp = &((struct ifpppcstatsreq *) data)->stats;
616 bzero(pcp, sizeof(*pcp));
617 if (sc->sc_xc_state != NULL)
618 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
619 if (sc->sc_rc_state != NULL)
620 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
622 #endif /* PPP_COMPRESS */
632 * Queue a packet. Start transmission if not active.
633 * Packet is placed in Information field of PPP frame.
636 pppoutput(ifp, m0, dst, rtp)
639 struct sockaddr *dst;
642 register struct ppp_softc *sc = ifp->if_softc;
643 int protocol, address, control;
652 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
653 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
654 error = ENETDOWN; /* sort of */
659 * Compute PPP header.
661 m0->m_flags &= ~M_HIGHPRI;
662 switch (dst->sa_family) {
665 address = PPP_ALLSTATIONS;
668 mode = sc->sc_npmode[NP_IP];
671 * If this packet has the "low delay" bit set in the IP header,
672 * put it on the fastq instead.
674 ip = mtod(m0, struct ip *);
675 if (ip->ip_tos & IPTOS_LOWDELAY)
676 m0->m_flags |= M_HIGHPRI;
680 address = PPP_ADDRESS(dst->sa_data);
681 control = PPP_CONTROL(dst->sa_data);
682 protocol = PPP_PROTOCOL(dst->sa_data);
686 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
687 error = EAFNOSUPPORT;
692 * Drop this packet, or return an error, if necessary.
694 if (mode == NPMODE_ERROR) {
698 if (mode == NPMODE_DROP) {
704 * Add PPP header. If no space in first mbuf, allocate another.
705 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
707 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
708 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
715 m0->m_data -= PPP_HDRLEN;
717 cp = mtod(m0, u_char *);
720 *cp++ = protocol >> 8;
721 *cp++ = protocol & 0xff;
722 m0->m_len += PPP_HDRLEN;
725 for (m = m0; m != 0; m = m->m_next)
728 if (sc->sc_flags & SC_LOG_OUTPKT) {
729 printf("%s output: ", ifp->if_xname);
733 if ((protocol & 0x8000) == 0) {
736 * Apply the pass and active filters to the packet,
737 * but only if it is a data packet.
739 *mtod(m0, u_char *) = 1; /* indicates outbound */
740 if (sc->sc_pass_filt.bf_insns != 0
741 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
743 error = 0; /* drop this packet */
748 * Update the time we sent the most recent packet.
750 if (sc->sc_active_filt.bf_insns == 0
751 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
752 sc->sc_last_sent = time.tv_sec;
754 *mtod(m0, u_char *) = address;
757 * Update the time we sent the most recent packet.
759 sc->sc_last_sent = time.tv_sec;
760 #endif /* PPP_FILTER */
765 * See if bpf wants to look at the packet.
768 bpf_mtap(sc->sc_bpf, m0);
772 * Put the packet on the appropriate queue.
775 if (mode == NPMODE_QUEUE) {
776 /* XXX we should limit the number of packets on this queue */
777 *sc->sc_npqtail = m0;
778 m0->m_nextpkt = NULL;
779 sc->sc_npqtail = &m0->m_nextpkt;
781 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
782 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
785 sc->sc_if.if_oerrors++;
786 sc->sc_stats.ppp_oerrors++;
793 ifp->if_lastchange = time;
795 ifp->if_obytes += len;
806 * After a change in the NPmode for some NP, move packets from the
807 * npqueue to the send queue or the fast queue as appropriate.
808 * Should be called at splsoftnet.
812 struct ppp_softc *sc;
814 struct mbuf *m, **mpp;
818 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
819 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
821 mode = sc->sc_npmode[NP_IP];
830 * This packet can now go on one of the queues to be sent.
834 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
837 sc->sc_if.if_oerrors++;
838 sc->sc_stats.ppp_oerrors++;
854 sc->sc_npqtail = mpp;
858 * Transmitter has finished outputting some stuff;
859 * remember to call sc->sc_start later at splsoftnet.
863 struct ppp_softc *sc;
867 sc->sc_flags &= ~SC_TBUSY;
868 schednetisr(NETISR_PPP);
873 * Get a packet to send. This procedure is intended to be called at
874 * splsoftnet, since it may involve time-consuming operations such as
875 * applying VJ compression, packet compression, address/control and/or
876 * protocol field compression to the packet.
880 struct ppp_softc *sc;
884 int address, control, protocol;
887 * Grab a packet to send: first try the fast queue, then the
890 IF_DEQUEUE(&sc->sc_fastq, m);
892 IF_DEQUEUE(&sc->sc_if.if_snd, m);
896 ++sc->sc_stats.ppp_opackets;
899 * Extract the ppp header of the new packet.
900 * The ppp header will be in one mbuf.
902 cp = mtod(m, u_char *);
903 address = PPP_ADDRESS(cp);
904 control = PPP_CONTROL(cp);
905 protocol = PPP_PROTOCOL(cp);
911 * If the packet is a TCP/IP packet, see if we can compress it.
913 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
918 ip = (struct ip *) (cp + PPP_HDRLEN);
919 if (mp->m_len <= PPP_HDRLEN) {
923 ip = mtod(mp, struct ip *);
925 /* this code assumes the IP/TCP header is in one non-shared mbuf */
926 if (ip->ip_p == IPPROTO_TCP) {
927 type = sl_compress_tcp(mp, ip, sc->sc_comp,
928 !(sc->sc_flags & SC_NO_TCP_CCID));
930 case TYPE_UNCOMPRESSED_TCP:
931 protocol = PPP_VJC_UNCOMP;
933 case TYPE_COMPRESSED_TCP:
934 protocol = PPP_VJC_COMP;
935 cp = mtod(m, u_char *);
936 cp[0] = address; /* header has moved */
941 cp[3] = protocol; /* update protocol in PPP header */
951 #endif /* PPP_COMPRESS */
955 if (protocol != PPP_LCP && protocol != PPP_CCP
956 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
957 struct mbuf *mcomp = NULL;
961 for (mp = m; mp != NULL; mp = mp->m_next)
963 clen = (*sc->sc_xcomp->compress)
964 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
966 if (sc->sc_flags & SC_CCP_UP) {
967 /* Send the compressed packet instead of the original. */
970 cp = mtod(m, u_char *);
973 /* Can't transmit compressed packets until CCP is up. */
978 #endif /* PPP_COMPRESS */
981 * Compress the address/control and protocol, if possible.
983 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
984 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
985 protocol != PPP_LCP) {
986 /* can compress address/control */
990 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
991 /* can compress protocol */
992 if (mtod(m, u_char *) == cp) {
993 cp[2] = cp[1]; /* move address/control up */
1004 * Software interrupt routine, called at splsoftnet.
1009 struct ppp_softc *sc;
1015 for (i = 0; i < NPPP; ++i, ++sc) {
1016 if (!(sc->sc_flags & SC_TBUSY)
1017 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1019 sc->sc_flags |= SC_TBUSY;
1021 (*sc->sc_start)(sc);
1025 IF_DEQUEUE(&sc->sc_rawq, m);
1037 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1038 * 0 if it is about to be transmitted.
1041 ppp_ccp(sc, m, rcvd)
1042 struct ppp_softc *sc;
1051 * Get a pointer to the data after the PPP header.
1053 if (m->m_len <= PPP_HDRLEN) {
1057 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1060 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1063 ep = mtod(mp, u_char *) + mp->m_len;
1064 if (dp + CCP_HDRLEN > ep)
1066 slen = CCP_LENGTH(dp);
1067 if (dp + slen > ep) {
1068 if (sc->sc_flags & SC_DEBUG)
1069 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1070 dp, slen, mtod(mp, u_char *), mp->m_len);
1074 switch (CCP_CODE(dp)) {
1078 /* CCP must be going down - disable compression */
1079 if (sc->sc_flags & SC_CCP_UP) {
1081 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1087 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1088 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1089 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1091 /* we're agreeing to send compressed packets. */
1092 if (sc->sc_xc_state != NULL
1093 && (*sc->sc_xcomp->comp_init)
1094 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1095 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1097 sc->sc_flags |= SC_COMP_RUN;
1101 /* peer is agreeing to send compressed packets. */
1102 if (sc->sc_rc_state != NULL
1103 && (*sc->sc_rcomp->decomp_init)
1104 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1105 sc->sc_unit, 0, sc->sc_mru,
1106 sc->sc_flags & SC_DEBUG)) {
1108 sc->sc_flags |= SC_DECOMP_RUN;
1109 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1117 if (sc->sc_flags & SC_CCP_UP) {
1119 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1120 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1122 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1123 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1125 sc->sc_flags &= ~SC_DC_ERROR;
1135 * CCP is down; free (de)compressor state if necessary.
1139 struct ppp_softc *sc;
1141 if (sc->sc_xc_state) {
1142 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1143 sc->sc_xc_state = NULL;
1145 if (sc->sc_rc_state) {
1146 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1147 sc->sc_rc_state = NULL;
1150 #endif /* PPP_COMPRESS */
1153 * PPP packet input routine.
1154 * The caller has checked and removed the FCS and has inserted
1155 * the address/control bytes and the protocol high byte if they
1159 ppppktin(sc, m, lost)
1160 struct ppp_softc *sc;
1167 m->m_flags |= M_ERRMARK;
1168 IF_ENQUEUE(&sc->sc_rawq, m);
1169 schednetisr(NETISR_PPP);
1174 * Process a received PPP packet, doing decompression as necessary.
1175 * Should be called at splsoftnet.
1177 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1178 TYPE_UNCOMPRESSED_TCP)
1182 struct ppp_softc *sc;
1185 struct ifnet *ifp = &sc->sc_if;
1186 struct ifqueue *inq;
1187 int s, ilen, xlen, proto, rv;
1188 u_char *cp, adrs, ctrl;
1189 struct mbuf *mp, *dmp = NULL;
1193 sc->sc_stats.ppp_ipackets++;
1195 if (sc->sc_flags & SC_LOG_INPKT) {
1197 for (mp = m; mp != NULL; mp = mp->m_next)
1199 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1203 cp = mtod(m, u_char *);
1204 adrs = PPP_ADDRESS(cp);
1205 ctrl = PPP_CONTROL(cp);
1206 proto = PPP_PROTOCOL(cp);
1208 if (m->m_flags & M_ERRMARK) {
1209 m->m_flags &= ~M_ERRMARK;
1211 sc->sc_flags |= SC_VJ_RESET;
1217 * Decompress this packet if necessary, update the receiver's
1218 * dictionary, or take appropriate action on a CCP packet.
1220 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1221 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1222 /* decompress this packet */
1223 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1224 if (rv == DECOMP_OK) {
1227 /* no error, but no decompressed packet produced */
1231 cp = mtod(m, u_char *);
1232 proto = PPP_PROTOCOL(cp);
1236 * An error has occurred in decompression.
1237 * Pass the compressed packet up to pppd, which may take
1238 * CCP down or issue a Reset-Req.
1240 if (sc->sc_flags & SC_DEBUG)
1241 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1243 sc->sc_flags |= SC_VJ_RESET;
1244 if (rv == DECOMP_ERROR)
1245 sc->sc_flags |= SC_DC_ERROR;
1247 sc->sc_flags |= SC_DC_FERROR;
1252 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1253 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1255 if (proto == PPP_CCP) {
1262 for (mp = m; mp != NULL; mp = mp->m_next)
1266 if (sc->sc_flags & SC_VJ_RESET) {
1268 * If we've missed a packet, we must toss subsequent compressed
1269 * packets which don't have an explicit connection ID.
1272 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1274 sc->sc_flags &= ~SC_VJ_RESET;
1279 * See if we have a VJ-compressed packet to uncompress.
1281 if (proto == PPP_VJC_COMP) {
1282 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1285 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1286 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1287 sc->sc_comp, &iphdr, &hlen);
1290 if (sc->sc_flags & SC_DEBUG)
1291 printf("%s: VJ uncompress failed on type comp\n",
1296 /* Copy the PPP and IP headers into a new mbuf. */
1297 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1302 if (hlen + PPP_HDRLEN > MHLEN) {
1303 MCLGET(mp, M_DONTWAIT);
1304 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1306 goto bad; /* lose if big headers and no clusters */
1309 cp = mtod(mp, u_char *);
1315 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1316 mp->m_len = hlen + PPP_HDRLEN;
1319 * Trim the PPP and VJ headers off the old mbuf
1320 * and stick the new and old mbufs together.
1322 m->m_data += PPP_HDRLEN + xlen;
1323 m->m_len -= PPP_HDRLEN + xlen;
1324 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1325 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1326 mp->m_len += m->m_len;
1327 MFREE(m, mp->m_next);
1331 ilen += hlen - xlen;
1333 } else if (proto == PPP_VJC_UNCOMP) {
1334 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1337 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1338 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1339 sc->sc_comp, &iphdr, &hlen);
1342 if (sc->sc_flags & SC_DEBUG)
1343 printf("%s: VJ uncompress failed on type uncomp\n",
1354 * If the packet will fit in a header mbuf, don't waste a
1355 * whole cluster on it.
1357 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1358 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1360 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1366 m->m_pkthdr.len = ilen;
1367 m->m_pkthdr.rcvif = ifp;
1369 if ((proto & 0x8000) == 0) {
1372 * See whether we want to pass this packet, and
1373 * if it counts as link activity.
1375 adrs = *mtod(m, u_char *); /* save address field */
1376 *mtod(m, u_char *) = 0; /* indicate inbound */
1377 if (sc->sc_pass_filt.bf_insns != 0
1378 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1380 /* drop this packet */
1384 if (sc->sc_active_filt.bf_insns == 0
1385 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1386 sc->sc_last_recv = time.tv_sec;
1388 *mtod(m, u_char *) = adrs;
1391 * Record the time that we received this packet.
1393 sc->sc_last_recv = time.tv_sec;
1394 #endif /* PPP_FILTER */
1398 /* See if bpf wants to look at the packet. */
1400 bpf_mtap(sc->sc_bpf, m);
1408 * IP packet - take off the ppp header and pass it up to IP.
1410 if ((ifp->if_flags & IFF_UP) == 0
1411 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1412 /* interface is down - drop the packet. */
1416 m->m_pkthdr.len -= PPP_HDRLEN;
1417 m->m_data += PPP_HDRLEN;
1418 m->m_len -= PPP_HDRLEN;
1419 schednetisr(NETISR_IP);
1426 * Some other protocol - place on input queue for read().
1434 * Put the packet on the appropriate input queue.
1437 if (IF_QFULL(inq)) {
1440 if (sc->sc_flags & SC_DEBUG)
1441 printf("%s: input queue full\n", ifp->if_xname);
1448 ifp->if_ibytes += ilen;
1449 ifp->if_lastchange = time;
1458 sc->sc_if.if_ierrors++;
1459 sc->sc_stats.ppp_ierrors++;
1462 #define MAX_DUMP_BYTES 128
1468 char buf[3*MAX_DUMP_BYTES+4];
1471 static char digits[] = "0123456789abcdef";
1473 for (m = m0; m; m = m->m_next) {
1475 u_char *rptr = (u_char *)m->m_data;
1478 if (bp > buf + sizeof(buf) - 4)
1480 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1481 *bp++ = digits[*rptr++ & 0xf];
1485 if (bp > buf + sizeof(buf) - 3)
1495 printf("%s\n", buf);
1498 #endif /* NPPP > 0 */