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;
166 struct compressor *ppp_compressors[8] = {
167 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
170 #if DO_DEFLATE && defined(PPP_DEFLATE)
175 #endif /* PPP_COMPRESS */
179 * Called from boot code to establish ppp interfaces.
184 register struct ppp_softc *sc;
187 for (sc = ppp_softc; i < NPPP; sc++) {
188 sc->sc_unit = i; /* XXX */
189 sprintf(sc->sc_if.if_xname, "ppp%d", i++);
190 sc->sc_if.if_softc = sc;
191 sc->sc_if.if_mtu = PPP_MTU;
192 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
193 sc->sc_if.if_type = IFT_PPP;
194 sc->sc_if.if_hdrlen = PPP_HDRLEN;
195 sc->sc_if.if_ioctl = pppsioctl;
196 sc->sc_if.if_output = pppoutput;
197 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
198 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
199 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
200 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
201 if_attach(&sc->sc_if);
203 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
209 * Allocate a ppp interface unit and initialize it.
216 struct ppp_softc *sc;
218 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
219 if (sc->sc_xfer == pid) {
223 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
224 if (sc->sc_devp == NULL)
230 sc->sc_mru = PPP_MRU;
231 sc->sc_relinq = NULL;
232 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
234 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
237 sl_compress_init(sc->sc_comp);
240 sc->sc_xc_state = NULL;
241 sc->sc_rc_state = NULL;
242 #endif /* PPP_COMPRESS */
243 for (i = 0; i < NUM_NP; ++i)
244 sc->sc_npmode[i] = NPMODE_ERROR;
245 sc->sc_npqueue = NULL;
246 sc->sc_npqtail = &sc->sc_npqueue;
247 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
253 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
257 struct ppp_softc *sc;
262 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
266 IF_DEQUEUE(&sc->sc_rawq, m);
272 IF_DEQUEUE(&sc->sc_inq, m);
278 IF_DEQUEUE(&sc->sc_fastq, m);
283 while ((m = sc->sc_npqueue) != NULL) {
284 sc->sc_npqueue = m->m_nextpkt;
287 if (sc->sc_togo != NULL) {
288 m_freem(sc->sc_togo);
293 sc->sc_xc_state = NULL;
294 sc->sc_rc_state = NULL;
295 #endif /* PPP_COMPRESS */
297 if (sc->sc_pass_filt.bf_insns != 0) {
298 FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
299 sc->sc_pass_filt.bf_insns = 0;
300 sc->sc_pass_filt.bf_len = 0;
302 if (sc->sc_active_filt.bf_insns != 0) {
303 FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
304 sc->sc_active_filt.bf_insns = 0;
305 sc->sc_active_filt.bf_len = 0;
307 #endif /* PPP_FILTER */
309 if (sc->sc_comp != 0) {
310 FREE(sc->sc_comp, M_DEVBUF);
317 * Ioctl routine for generic ppp devices.
320 pppioctl(sc, cmd, data, flag, p)
321 struct ppp_softc *sc;
327 int s, error, flags, mru, nb, npx;
328 struct ppp_option_data *odp;
329 struct compressor **cp;
333 struct bpf_program *bp, *nbp;
334 struct bpf_insn *newcode, *oldcode;
336 #endif /* PPP_FILTER */
338 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
343 *(int *)data = sc->sc_inq.ifq_len;
347 *(int *)data = sc->sc_unit; /* XXX */
351 *(u_int *)data = sc->sc_flags;
355 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
357 flags = *(int *)data & SC_MASK;
360 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
364 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
369 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
372 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
377 *(int *)data = sc->sc_mru;
382 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
386 sl_compress_setup(sc->sc_comp, *(int *)data);
393 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
395 sc->sc_xfer = p->p_pid;
399 case PPPIOCSCOMPRESS:
400 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
402 odp = (struct ppp_option_data *) data;
404 if (nb > sizeof(ccp_option))
405 nb = sizeof(ccp_option);
406 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
408 if (ccp_option[1] < 2) /* preliminary check on the length byte */
410 for (cp = ppp_compressors; *cp != NULL; ++cp)
411 if ((*cp)->compress_proto == ccp_option[0]) {
413 * Found a handler for the protocol - try to allocate
414 * a compressor or decompressor.
419 if (sc->sc_xc_state != NULL)
420 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
422 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
423 if (sc->sc_xc_state == NULL) {
424 if (sc->sc_flags & SC_DEBUG)
425 printf("%s: comp_alloc failed\n",
430 sc->sc_flags &= ~SC_COMP_RUN;
434 if (sc->sc_rc_state != NULL)
435 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
437 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
438 if (sc->sc_rc_state == NULL) {
439 if (sc->sc_flags & SC_DEBUG)
440 printf("%s: decomp_alloc failed\n",
445 sc->sc_flags &= ~SC_DECOMP_RUN;
450 if (sc->sc_flags & SC_DEBUG)
451 printf("%s: no compressor for [%x %x %x], %x\n",
452 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
454 return (EINVAL); /* no handler found */
455 #endif /* PPP_COMPRESS */
459 npi = (struct npioctl *) data;
460 switch (npi->protocol) {
467 if (cmd == PPPIOCGNPMODE) {
468 npi->mode = sc->sc_npmode[npx];
470 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
472 if (npi->mode != sc->sc_npmode[npx]) {
474 sc->sc_npmode[npx] = npi->mode;
475 if (npi->mode != NPMODE_QUEUE) {
487 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
488 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
495 nbp = (struct bpf_program *) data;
496 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
498 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
499 if (newcodelen != 0) {
500 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
502 return EINVAL; /* or sumpin */
504 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
506 FREE(newcode, M_DEVBUF);
509 if (!bpf_validate(newcode, nbp->bf_len)) {
510 FREE(newcode, M_DEVBUF);
515 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
516 oldcode = bp->bf_insns;
518 bp->bf_len = nbp->bf_len;
519 bp->bf_insns = newcode;
522 FREE(oldcode, M_DEVBUF);
533 * Process an ioctl request to the ppp network interface.
536 pppsioctl(ifp, cmd, data)
537 register struct ifnet *ifp;
541 register struct proc *p = curproc; /* XXX */
542 register struct ppp_softc *sc = ifp->if_softc;
543 register struct ifaddr *ifa = (struct ifaddr *)data;
544 register struct ifreq *ifr = (struct ifreq *)data;
545 struct ppp_stats *psp;
547 struct ppp_comp_stats *pcp;
549 int s = splimp(), error = 0;
553 if ((ifp->if_flags & IFF_RUNNING) == 0)
554 ifp->if_flags &= ~IFF_UP;
558 if (ifa->ifa_addr->sa_family != AF_INET)
559 error = EAFNOSUPPORT;
563 if (ifa->ifa_addr->sa_family != AF_INET)
564 error = EAFNOSUPPORT;
568 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
570 sc->sc_if.if_mtu = ifr->ifr_mtu;
574 ifr->ifr_mtu = sc->sc_if.if_mtu;
580 error = EAFNOSUPPORT;
583 switch(ifr->ifr_addr.sa_family) {
589 error = EAFNOSUPPORT;
595 psp = &((struct ifpppstatsreq *) data)->stats;
596 bzero(psp, sizeof(*psp));
597 psp->p = sc->sc_stats;
598 #if defined(VJC) && !defined(SL_NO_STATS)
600 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
601 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
602 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
603 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
604 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
605 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
606 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
607 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
614 pcp = &((struct ifpppcstatsreq *) data)->stats;
615 bzero(pcp, sizeof(*pcp));
616 if (sc->sc_xc_state != NULL)
617 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
618 if (sc->sc_rc_state != NULL)
619 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
621 #endif /* PPP_COMPRESS */
631 * Queue a packet. Start transmission if not active.
632 * Packet is placed in Information field of PPP frame.
635 pppoutput(ifp, m0, dst, rtp)
638 struct sockaddr *dst;
641 register struct ppp_softc *sc = ifp->if_softc;
642 int protocol, address, control;
651 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
652 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
653 error = ENETDOWN; /* sort of */
658 * Compute PPP header.
660 m0->m_flags &= ~M_HIGHPRI;
661 switch (dst->sa_family) {
664 address = PPP_ALLSTATIONS;
667 mode = sc->sc_npmode[NP_IP];
670 * If this packet has the "low delay" bit set in the IP header,
671 * put it on the fastq instead.
673 ip = mtod(m0, struct ip *);
674 if (ip->ip_tos & IPTOS_LOWDELAY)
675 m0->m_flags |= M_HIGHPRI;
679 address = PPP_ADDRESS(dst->sa_data);
680 control = PPP_CONTROL(dst->sa_data);
681 protocol = PPP_PROTOCOL(dst->sa_data);
685 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
686 error = EAFNOSUPPORT;
691 * Drop this packet, or return an error, if necessary.
693 if (mode == NPMODE_ERROR) {
697 if (mode == NPMODE_DROP) {
703 * Add PPP header. If no space in first mbuf, allocate another.
704 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
706 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
707 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
714 m0->m_data -= PPP_HDRLEN;
716 cp = mtod(m0, u_char *);
719 *cp++ = protocol >> 8;
720 *cp++ = protocol & 0xff;
721 m0->m_len += PPP_HDRLEN;
724 for (m = m0; m != 0; m = m->m_next)
727 if (sc->sc_flags & SC_LOG_OUTPKT) {
728 printf("%s output: ", ifp->if_xname);
732 if ((protocol & 0x8000) == 0) {
735 * Apply the pass and active filters to the packet,
736 * but only if it is a data packet.
738 *mtod(m0, u_char *) = 1; /* indicates outbound */
739 if (sc->sc_pass_filt.bf_insns != 0
740 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
742 error = 0; /* drop this packet */
747 * Update the time we sent the most recent packet.
749 if (sc->sc_active_filt.bf_insns == 0
750 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
751 sc->sc_last_sent = time.tv_sec;
753 *mtod(m0, u_char *) = address;
756 * Update the time we sent the most recent packet.
758 sc->sc_last_sent = time.tv_sec;
759 #endif /* PPP_FILTER */
764 * See if bpf wants to look at the packet.
767 bpf_mtap(sc->sc_bpf, m0);
771 * Put the packet on the appropriate queue.
774 if (mode == NPMODE_QUEUE) {
775 /* XXX we should limit the number of packets on this queue */
776 *sc->sc_npqtail = m0;
777 m0->m_nextpkt = NULL;
778 sc->sc_npqtail = &m0->m_nextpkt;
780 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
781 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
784 sc->sc_if.if_oerrors++;
785 sc->sc_stats.ppp_oerrors++;
792 ifp->if_lastchange = time;
794 ifp->if_obytes += len;
805 * After a change in the NPmode for some NP, move packets from the
806 * npqueue to the send queue or the fast queue as appropriate.
807 * Should be called at splsoftnet.
811 struct ppp_softc *sc;
813 struct mbuf *m, **mpp;
817 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
818 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
820 mode = sc->sc_npmode[NP_IP];
829 * This packet can now go on one of the queues to be sent.
833 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
836 sc->sc_if.if_oerrors++;
837 sc->sc_stats.ppp_oerrors++;
853 sc->sc_npqtail = mpp;
857 * Transmitter has finished outputting some stuff;
858 * remember to call sc->sc_start later at splsoftnet.
862 struct ppp_softc *sc;
866 sc->sc_flags &= ~SC_TBUSY;
867 schednetisr(NETISR_PPP);
872 * Get a packet to send. This procedure is intended to be called at
873 * splsoftnet, since it may involve time-consuming operations such as
874 * applying VJ compression, packet compression, address/control and/or
875 * protocol field compression to the packet.
879 struct ppp_softc *sc;
883 int address, control, protocol;
886 * Grab a packet to send: first try the fast queue, then the
889 IF_DEQUEUE(&sc->sc_fastq, m);
891 IF_DEQUEUE(&sc->sc_if.if_snd, m);
895 ++sc->sc_stats.ppp_opackets;
898 * Extract the ppp header of the new packet.
899 * The ppp header will be in one mbuf.
901 cp = mtod(m, u_char *);
902 address = PPP_ADDRESS(cp);
903 control = PPP_CONTROL(cp);
904 protocol = PPP_PROTOCOL(cp);
910 * If the packet is a TCP/IP packet, see if we can compress it.
912 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
917 ip = (struct ip *) (cp + PPP_HDRLEN);
918 if (mp->m_len <= PPP_HDRLEN) {
922 ip = mtod(mp, struct ip *);
924 /* this code assumes the IP/TCP header is in one non-shared mbuf */
925 if (ip->ip_p == IPPROTO_TCP) {
926 type = sl_compress_tcp(mp, ip, sc->sc_comp,
927 !(sc->sc_flags & SC_NO_TCP_CCID));
929 case TYPE_UNCOMPRESSED_TCP:
930 protocol = PPP_VJC_UNCOMP;
932 case TYPE_COMPRESSED_TCP:
933 protocol = PPP_VJC_COMP;
934 cp = mtod(m, u_char *);
935 cp[0] = address; /* header has moved */
940 cp[3] = protocol; /* update protocol in PPP header */
950 #endif /* PPP_COMPRESS */
954 if (protocol != PPP_LCP && protocol != PPP_CCP
955 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
956 struct mbuf *mcomp = NULL;
960 for (mp = m; mp != NULL; mp = mp->m_next)
962 clen = (*sc->sc_xcomp->compress)
963 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
965 if (sc->sc_flags & SC_CCP_UP) {
966 /* Send the compressed packet instead of the original. */
969 cp = mtod(m, u_char *);
972 /* Can't transmit compressed packets until CCP is up. */
977 #endif /* PPP_COMPRESS */
980 * Compress the address/control and protocol, if possible.
982 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
983 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
984 protocol != PPP_LCP) {
985 /* can compress address/control */
989 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
990 /* can compress protocol */
991 if (mtod(m, u_char *) == cp) {
992 cp[2] = cp[1]; /* move address/control up */
1003 * Software interrupt routine, called at splsoftnet.
1008 struct ppp_softc *sc;
1014 for (i = 0; i < NPPP; ++i, ++sc) {
1015 if (!(sc->sc_flags & SC_TBUSY)
1016 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1018 sc->sc_flags |= SC_TBUSY;
1020 (*sc->sc_start)(sc);
1024 IF_DEQUEUE(&sc->sc_rawq, m);
1036 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1037 * 0 if it is about to be transmitted.
1040 ppp_ccp(sc, m, rcvd)
1041 struct ppp_softc *sc;
1050 * Get a pointer to the data after the PPP header.
1052 if (m->m_len <= PPP_HDRLEN) {
1056 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1059 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1062 ep = mtod(mp, u_char *) + mp->m_len;
1063 if (dp + CCP_HDRLEN > ep)
1065 slen = CCP_LENGTH(dp);
1066 if (dp + slen > ep) {
1067 if (sc->sc_flags & SC_DEBUG)
1068 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1069 dp, slen, mtod(mp, u_char *), mp->m_len);
1073 switch (CCP_CODE(dp)) {
1077 /* CCP must be going down - disable compression */
1078 if (sc->sc_flags & SC_CCP_UP) {
1080 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1086 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1087 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1088 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1090 /* we're agreeing to send compressed packets. */
1091 if (sc->sc_xc_state != NULL
1092 && (*sc->sc_xcomp->comp_init)
1093 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1094 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1096 sc->sc_flags |= SC_COMP_RUN;
1100 /* peer is agreeing to send compressed packets. */
1101 if (sc->sc_rc_state != NULL
1102 && (*sc->sc_rcomp->decomp_init)
1103 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1104 sc->sc_unit, 0, sc->sc_mru,
1105 sc->sc_flags & SC_DEBUG)) {
1107 sc->sc_flags |= SC_DECOMP_RUN;
1108 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1116 if (sc->sc_flags & SC_CCP_UP) {
1118 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1119 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1121 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1122 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1124 sc->sc_flags &= ~SC_DC_ERROR;
1134 * CCP is down; free (de)compressor state if necessary.
1138 struct ppp_softc *sc;
1140 if (sc->sc_xc_state) {
1141 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1142 sc->sc_xc_state = NULL;
1144 if (sc->sc_rc_state) {
1145 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1146 sc->sc_rc_state = NULL;
1149 #endif /* PPP_COMPRESS */
1152 * PPP packet input routine.
1153 * The caller has checked and removed the FCS and has inserted
1154 * the address/control bytes and the protocol high byte if they
1158 ppppktin(sc, m, lost)
1159 struct ppp_softc *sc;
1166 m->m_flags |= M_ERRMARK;
1167 IF_ENQUEUE(&sc->sc_rawq, m);
1168 schednetisr(NETISR_PPP);
1173 * Process a received PPP packet, doing decompression as necessary.
1174 * Should be called at splsoftnet.
1176 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1177 TYPE_UNCOMPRESSED_TCP)
1181 struct ppp_softc *sc;
1184 struct ifnet *ifp = &sc->sc_if;
1185 struct ifqueue *inq;
1186 int s, ilen, xlen, proto, rv;
1187 u_char *cp, adrs, ctrl;
1188 struct mbuf *mp, *dmp = NULL;
1192 sc->sc_stats.ppp_ipackets++;
1194 if (sc->sc_flags & SC_LOG_INPKT) {
1196 for (mp = m; mp != NULL; mp = mp->m_next)
1198 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1202 cp = mtod(m, u_char *);
1203 adrs = PPP_ADDRESS(cp);
1204 ctrl = PPP_CONTROL(cp);
1205 proto = PPP_PROTOCOL(cp);
1207 if (m->m_flags & M_ERRMARK) {
1208 m->m_flags &= ~M_ERRMARK;
1210 sc->sc_flags |= SC_VJ_RESET;
1216 * Decompress this packet if necessary, update the receiver's
1217 * dictionary, or take appropriate action on a CCP packet.
1219 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1220 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1221 /* decompress this packet */
1222 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1223 if (rv == DECOMP_OK) {
1226 /* no error, but no decompressed packet produced */
1230 cp = mtod(m, u_char *);
1231 proto = PPP_PROTOCOL(cp);
1235 * An error has occurred in decompression.
1236 * Pass the compressed packet up to pppd, which may take
1237 * CCP down or issue a Reset-Req.
1239 if (sc->sc_flags & SC_DEBUG)
1240 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1242 sc->sc_flags |= SC_VJ_RESET;
1243 if (rv == DECOMP_ERROR)
1244 sc->sc_flags |= SC_DC_ERROR;
1246 sc->sc_flags |= SC_DC_FERROR;
1251 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1252 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1254 if (proto == PPP_CCP) {
1261 for (mp = m; mp != NULL; mp = mp->m_next)
1265 if (sc->sc_flags & SC_VJ_RESET) {
1267 * If we've missed a packet, we must toss subsequent compressed
1268 * packets which don't have an explicit connection ID.
1271 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1273 sc->sc_flags &= ~SC_VJ_RESET;
1278 * See if we have a VJ-compressed packet to uncompress.
1280 if (proto == PPP_VJC_COMP) {
1281 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1284 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1285 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1286 sc->sc_comp, &iphdr, &hlen);
1289 if (sc->sc_flags & SC_DEBUG)
1290 printf("%s: VJ uncompress failed on type comp\n",
1295 /* Copy the PPP and IP headers into a new mbuf. */
1296 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1301 if (hlen + PPP_HDRLEN > MHLEN) {
1302 MCLGET(mp, M_DONTWAIT);
1303 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1305 goto bad; /* lose if big headers and no clusters */
1308 cp = mtod(mp, u_char *);
1314 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1315 mp->m_len = hlen + PPP_HDRLEN;
1318 * Trim the PPP and VJ headers off the old mbuf
1319 * and stick the new and old mbufs together.
1321 m->m_data += PPP_HDRLEN + xlen;
1322 m->m_len -= PPP_HDRLEN + xlen;
1323 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1324 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1325 mp->m_len += m->m_len;
1326 MFREE(m, mp->m_next);
1330 ilen += hlen - xlen;
1332 } else if (proto == PPP_VJC_UNCOMP) {
1333 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1336 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1337 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1338 sc->sc_comp, &iphdr, &hlen);
1341 if (sc->sc_flags & SC_DEBUG)
1342 printf("%s: VJ uncompress failed on type uncomp\n",
1353 * If the packet will fit in a header mbuf, don't waste a
1354 * whole cluster on it.
1356 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1357 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1359 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1365 m->m_pkthdr.len = ilen;
1366 m->m_pkthdr.rcvif = ifp;
1368 if ((proto & 0x8000) == 0) {
1371 * See whether we want to pass this packet, and
1372 * if it counts as link activity.
1374 adrs = *mtod(m, u_char *); /* save address field */
1375 *mtod(m, u_char *) = 0; /* indicate inbound */
1376 if (sc->sc_pass_filt.bf_insns != 0
1377 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1379 /* drop this packet */
1383 if (sc->sc_active_filt.bf_insns == 0
1384 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1385 sc->sc_last_recv = time.tv_sec;
1387 *mtod(m, u_char *) = adrs;
1390 * Record the time that we received this packet.
1392 sc->sc_last_recv = time.tv_sec;
1393 #endif /* PPP_FILTER */
1397 /* See if bpf wants to look at the packet. */
1399 bpf_mtap(sc->sc_bpf, m);
1407 * IP packet - take off the ppp header and pass it up to IP.
1409 if ((ifp->if_flags & IFF_UP) == 0
1410 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1411 /* interface is down - drop the packet. */
1415 m->m_pkthdr.len -= PPP_HDRLEN;
1416 m->m_data += PPP_HDRLEN;
1417 m->m_len -= PPP_HDRLEN;
1418 schednetisr(NETISR_IP);
1425 * Some other protocol - place on input queue for read().
1433 * Put the packet on the appropriate input queue.
1436 if (IF_QFULL(inq)) {
1439 if (sc->sc_flags & SC_DEBUG)
1440 printf("%s: input queue full\n", ifp->if_xname);
1447 ifp->if_ibytes += ilen;
1448 ifp->if_lastchange = time;
1457 sc->sc_if.if_ierrors++;
1458 sc->sc_stats.ppp_ierrors++;
1461 #define MAX_DUMP_BYTES 128
1467 char buf[3*MAX_DUMP_BYTES+4];
1470 static char digits[] = "0123456789abcdef";
1472 for (m = m0; m; m = m->m_next) {
1474 u_char *rptr = (u_char *)m->m_data;
1477 if (bp > buf + sizeof(buf) - 4)
1479 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1480 *bp++ = digits[*rptr++ & 0xf];
1484 if (bp > buf + sizeof(buf) - 3)
1494 printf("%s\n", buf);
1497 #endif /* NPPP > 0 */