3 --- /dev/null Sat Sep 18 04:00:15 1999
4 +++ netbsd-1.4/if_ppp.c Sat Sep 18 04:06:21 1999
6 +/* NetBSD: if_ppp.c,v 1.55 1999/07/30 10:35:38 itojun Exp */
7 +/* Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp */
10 + * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
12 + * Copyright (c) 1989 Carnegie Mellon University.
13 + * All rights reserved.
15 + * Redistribution and use in source and binary forms are permitted
16 + * provided that the above copyright notice and this paragraph are
17 + * duplicated in all such forms and that any documentation,
18 + * advertising materials, and other materials related to such
19 + * distribution and use acknowledge that the software was developed
20 + * by Carnegie Mellon University. The name of the
21 + * University may not be used to endorse or promote products derived
22 + * from this software without specific prior written permission.
23 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25 + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 + * Carnegie Mellon University
30 + * Pittsburgh, PA 15213
32 + * ddp@andrew.cmu.edu
35 + * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
37 + * Copyright (c) 1987 Regents of the University of California.
38 + * All rights reserved.
40 + * Redistribution and use in source and binary forms are permitted
41 + * provided that the above copyright notice and this paragraph are
42 + * duplicated in all such forms and that any documentation,
43 + * advertising materials, and other materials related to such
44 + * distribution and use acknowledge that the software was developed
45 + * by the University of California, Berkeley. The name of the
46 + * University may not be used to endorse or promote products derived
47 + * from this software without specific prior written permission.
48 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
49 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
50 + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
52 + * Serial Line interface
55 + * Center for Seismic Studies
56 + * 1300 N 17th Street, Suite 1450
57 + * Arlington, Virginia 22209
62 + * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
63 + * Converted to 4.3BSD Beta by Chris Torek.
64 + * Other changes made at Berkeley, based in part on code by Kirk Smith.
66 + * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
67 + * Added VJ tcp header compression; more unified ioctls
69 + * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
70 + * Cleaned up a lot of the mbuf-related code to fix bugs that
71 + * caused system crashes and packet corruption. Changed pppstart
72 + * so that it doesn't just give up with a collision if the whole
73 + * packet doesn't fit in the output ring buffer.
75 + * Added priority queueing for interactive IP packets, following
76 + * the model of if_sl.c, plus hooks for bpf.
77 + * Paul Mackerras (paulus@cs.anu.edu.au).
80 +/* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
81 +/* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
89 +#include "opt_inet.h"
90 +#include "opt_gateway.h"
93 +#include <sys/param.h>
94 +#include <sys/proc.h>
95 +#include <sys/mbuf.h>
96 +#include <sys/socket.h>
97 +#include <sys/ioctl.h>
98 +#include <sys/kernel.h>
99 +#include <sys/systm.h>
100 +#include <sys/time.h>
101 +#include <sys/malloc.h>
104 +#include <net/if_types.h>
105 +#include <net/netisr.h>
106 +#include <net/route.h>
108 +#include <net/bpf.h>
112 +#include <netinet/in.h>
113 +#include <netinet/in_systm.h>
114 +#include <netinet/in_var.h>
115 +#include <netinet/ip.h>
119 +#error ppp device with VJC assumes INET
124 +#include "bpfilter.h"
126 +#include <sys/time.h>
127 +#include <net/bpf.h>
130 +#if defined(PPP_FILTER) || NBPFILTER > 0
131 +#include <net/slip.h>
135 +#include <net/slcompress.h>
138 +#include <net/ppp_defs.h>
139 +#include <net/if_ppp.h>
140 +#include <net/if_pppvar.h>
141 +#include <machine/cpu.h>
144 +#define PACKETPTR struct mbuf *
145 +#include <net/ppp-comp.h>
148 +static int pppsioctl __P((struct ifnet *, u_long, caddr_t));
149 +static void ppp_requeue __P((struct ppp_softc *));
150 +static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
151 +static void ppp_ccp_closed __P((struct ppp_softc *));
152 +static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
153 +static void pppdumpm __P((struct mbuf *m0));
156 + * Some useful mbuf macros not in mbuf.h.
158 +#define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
160 +#define M_DATASTART(m) \
161 + (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
162 + (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
164 +#define M_DATASIZE(m) \
165 + (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
166 + (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
169 + * We define two link layer specific mbuf flags, to mark high-priority
170 + * packets for output, and received packets following lost/corrupted
173 +#define M_HIGHPRI M_LINK0 /* output packet for sc_fastq */
174 +#define M_ERRMARK M_LINK1 /* rx packet following lost/corrupted pkt */
178 + * List of compressors we know about.
179 + * We leave some space so maybe we can modload compressors.
182 +extern struct compressor ppp_bsd_compress;
183 +extern struct compressor ppp_deflate, ppp_deflate_draft;
185 +struct compressor *ppp_compressors[8] = {
186 +#if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
189 +#if DO_DEFLATE && defined(PPP_DEFLATE)
191 + &ppp_deflate_draft,
195 +#endif /* PPP_COMPRESS */
199 + * Called from boot code to establish ppp interfaces.
204 + register struct ppp_softc *sc;
205 + register int i = 0;
207 + for (sc = ppp_softc; i < NPPP; sc++) {
208 + sc->sc_unit = i; /* XXX */
209 + sprintf(sc->sc_if.if_xname, "ppp%d", i++);
210 + sc->sc_if.if_softc = sc;
211 + sc->sc_if.if_mtu = PPP_MTU;
212 + sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
213 + sc->sc_if.if_type = IFT_PPP;
214 + sc->sc_if.if_hdrlen = PPP_HDRLEN;
215 + sc->sc_if.if_ioctl = pppsioctl;
216 + sc->sc_if.if_output = pppoutput;
217 + sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
218 + sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
219 + sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
220 + sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
221 + if_attach(&sc->sc_if);
223 + bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_NULL, 0);
229 + * Allocate a ppp interface unit and initialize it.
236 + struct ppp_softc *sc;
238 + for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
239 + if (sc->sc_xfer == pid) {
243 + for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
244 + if (sc->sc_devp == NULL)
250 + sc->sc_mru = PPP_MRU;
251 + sc->sc_relinq = NULL;
252 + bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
254 + MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
255 + M_DEVBUF, M_NOWAIT);
257 + sl_compress_init(sc->sc_comp);
260 + sc->sc_xc_state = NULL;
261 + sc->sc_rc_state = NULL;
262 +#endif /* PPP_COMPRESS */
263 + for (i = 0; i < NUM_NP; ++i)
264 + sc->sc_npmode[i] = NPMODE_ERROR;
265 + sc->sc_npqueue = NULL;
266 + sc->sc_npqtail = &sc->sc_npqueue;
267 + sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
273 + * Deallocate a ppp unit. Must be called at splsoftnet or higher.
277 + struct ppp_softc *sc;
281 + if_down(&sc->sc_if);
282 + sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
283 + sc->sc_devp = NULL;
286 + IF_DEQUEUE(&sc->sc_rawq, m);
292 + IF_DEQUEUE(&sc->sc_inq, m);
298 + IF_DEQUEUE(&sc->sc_fastq, m);
303 + while ((m = sc->sc_npqueue) != NULL) {
304 + sc->sc_npqueue = m->m_nextpkt;
307 + if (sc->sc_togo != NULL) {
308 + m_freem(sc->sc_togo);
309 + sc->sc_togo = NULL;
312 + ppp_ccp_closed(sc);
313 + sc->sc_xc_state = NULL;
314 + sc->sc_rc_state = NULL;
315 +#endif /* PPP_COMPRESS */
317 + if (sc->sc_pass_filt_in.bf_insns != 0) {
318 + FREE(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
319 + sc->sc_pass_filt_in.bf_insns = 0;
320 + sc->sc_pass_filt_in.bf_len = 0;
322 + if (sc->sc_pass_filt_out.bf_insns != 0) {
323 + FREE(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
324 + sc->sc_pass_filt_out.bf_insns = 0;
325 + sc->sc_pass_filt_out.bf_len = 0;
327 + if (sc->sc_active_filt_in.bf_insns != 0) {
328 + FREE(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
329 + sc->sc_active_filt_in.bf_insns = 0;
330 + sc->sc_active_filt_in.bf_len = 0;
332 + if (sc->sc_active_filt_out.bf_insns != 0) {
333 + FREE(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
334 + sc->sc_active_filt_out.bf_insns = 0;
335 + sc->sc_active_filt_out.bf_len = 0;
337 +#endif /* PPP_FILTER */
339 + if (sc->sc_comp != 0) {
340 + FREE(sc->sc_comp, M_DEVBUF);
347 + * Ioctl routine for generic ppp devices.
350 +pppioctl(sc, cmd, data, flag, p)
351 + struct ppp_softc *sc;
357 + int s, error, flags, mru, nb, npx;
358 + struct ppp_option_data *odp;
359 + struct compressor **cp;
360 + struct npioctl *npi;
363 + struct bpf_program *bp, *nbp;
364 + struct bpf_insn *newcode, *oldcode;
366 +#endif /* PPP_FILTER */
368 + u_char ccp_option[CCP_MAX_OPTION_LENGTH];
373 + *(int *)data = sc->sc_inq.ifq_len;
377 + *(int *)data = sc->sc_unit; /* XXX */
381 + *(u_int *)data = sc->sc_flags;
385 + if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
387 + flags = *(int *)data & SC_MASK;
390 + if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
391 + ppp_ccp_closed(sc);
394 + sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
399 + if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
401 + mru = *(int *)data;
402 + if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
407 + *(int *)data = sc->sc_mru;
411 + case PPPIOCSMAXCID:
412 + if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
416 + sl_compress_setup(sc->sc_comp, *(int *)data);
422 + case PPPIOCXFERUNIT:
423 + if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
425 + sc->sc_xfer = p->p_pid;
429 + case PPPIOCSCOMPRESS:
430 + if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
432 + odp = (struct ppp_option_data *) data;
434 + if (nb > sizeof(ccp_option))
435 + nb = sizeof(ccp_option);
436 + if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
438 + if (ccp_option[1] < 2) /* preliminary check on the length byte */
440 + for (cp = ppp_compressors; *cp != NULL; ++cp)
441 + if ((*cp)->compress_proto == ccp_option[0]) {
443 + * Found a handler for the protocol - try to allocate
444 + * a compressor or decompressor.
447 + if (odp->transmit) {
449 + if (sc->sc_xc_state != NULL)
450 + (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
451 + sc->sc_xcomp = *cp;
452 + sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
453 + if (sc->sc_xc_state == NULL) {
454 + if (sc->sc_flags & SC_DEBUG)
455 + printf("%s: comp_alloc failed\n",
456 + sc->sc_if.if_xname);
460 + sc->sc_flags &= ~SC_COMP_RUN;
464 + if (sc->sc_rc_state != NULL)
465 + (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
466 + sc->sc_rcomp = *cp;
467 + sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
468 + if (sc->sc_rc_state == NULL) {
469 + if (sc->sc_flags & SC_DEBUG)
470 + printf("%s: decomp_alloc failed\n",
471 + sc->sc_if.if_xname);
475 + sc->sc_flags &= ~SC_DECOMP_RUN;
480 + if (sc->sc_flags & SC_DEBUG)
481 + printf("%s: no compressor for [%x %x %x], %x\n",
482 + sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
483 + ccp_option[2], nb);
484 + return (EINVAL); /* no handler found */
485 +#endif /* PPP_COMPRESS */
487 + case PPPIOCGNPMODE:
488 + case PPPIOCSNPMODE:
489 + npi = (struct npioctl *) data;
490 + switch (npi->protocol) {
500 + if (cmd == PPPIOCGNPMODE) {
501 + npi->mode = sc->sc_npmode[npx];
503 + if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
505 + if (npi->mode != sc->sc_npmode[npx]) {
507 + sc->sc_npmode[npx] = npi->mode;
508 + if (npi->mode != NPMODE_QUEUE) {
520 + ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
521 + ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
527 + case PPPIOCSACTIVE:
528 + /* These are no longer supported. */
533 + case PPPIOCSIACTIVE:
534 + case PPPIOCSOACTIVE:
535 + nbp = (struct bpf_program *) data;
536 + if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
538 + newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
539 + if (newcodelen != 0) {
540 + newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
541 + /* WAITOK -- malloc() never fails. */
542 + if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
543 + newcodelen)) != 0) {
544 + free(newcode, M_DEVBUF);
547 + if (!bpf_validate(newcode, nbp->bf_len)) {
548 + free(newcode, M_DEVBUF);
555 + bp = &sc->sc_pass_filt_in;
559 + bp = &sc->sc_pass_filt_out;
562 + case PPPIOCSIACTIVE:
563 + bp = &sc->sc_active_filt_in;
566 + case PPPIOCSOACTIVE:
567 + bp = &sc->sc_active_filt_out;
570 + oldcode = bp->bf_insns;
572 + bp->bf_len = nbp->bf_len;
573 + bp->bf_insns = newcode;
576 + free(oldcode, M_DEVBUF);
578 +#endif /* PPP_FILTER */
587 + * Process an ioctl request to the ppp network interface.
590 +pppsioctl(ifp, cmd, data)
591 + register struct ifnet *ifp;
595 + register struct proc *p = curproc; /* XXX */
596 + register struct ppp_softc *sc = ifp->if_softc;
597 + register struct ifaddr *ifa = (struct ifaddr *)data;
598 + register struct ifreq *ifr = (struct ifreq *)data;
599 + struct ppp_stats *psp;
601 + struct ppp_comp_stats *pcp;
603 + int s = splimp(), error = 0;
607 + if ((ifp->if_flags & IFF_RUNNING) == 0)
608 + ifp->if_flags &= ~IFF_UP;
612 + switch (ifa->ifa_addr->sa_family) {
622 + error = EAFNOSUPPORT;
627 + case SIOCSIFDSTADDR:
628 + switch (ifa->ifa_addr->sa_family) {
638 + error = EAFNOSUPPORT;
644 + if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
646 + sc->sc_if.if_mtu = ifr->ifr_mtu;
650 + ifr->ifr_mtu = sc->sc_if.if_mtu;
656 + error = EAFNOSUPPORT;
659 + switch(ifr->ifr_addr.sa_family) {
669 + error = EAFNOSUPPORT;
674 + case SIOCGPPPSTATS:
675 + psp = &((struct ifpppstatsreq *) data)->stats;
676 + bzero(psp, sizeof(*psp));
677 + psp->p = sc->sc_stats;
678 +#if defined(VJC) && !defined(SL_NO_STATS)
680 + psp->vj.vjs_packets = sc->sc_comp->sls_packets;
681 + psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
682 + psp->vj.vjs_searches = sc->sc_comp->sls_searches;
683 + psp->vj.vjs_misses = sc->sc_comp->sls_misses;
684 + psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
685 + psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
686 + psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
687 + psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
693 + case SIOCGPPPCSTATS:
694 + pcp = &((struct ifpppcstatsreq *) data)->stats;
695 + bzero(pcp, sizeof(*pcp));
696 + if (sc->sc_xc_state != NULL)
697 + (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
698 + if (sc->sc_rc_state != NULL)
699 + (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
701 +#endif /* PPP_COMPRESS */
711 + * Queue a packet. Start transmission if not active.
712 + * Packet is placed in Information field of PPP frame.
715 +pppoutput(ifp, m0, dst, rtp)
718 + struct sockaddr *dst;
719 + struct rtentry *rtp;
721 + register struct ppp_softc *sc = ifp->if_softc;
722 + int protocol, address, control;
726 + struct ifqueue *ifq;
731 + if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
732 + || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
733 + error = ENETDOWN; /* sort of */
738 + * Compute PPP header.
740 + m0->m_flags &= ~M_HIGHPRI;
741 + switch (dst->sa_family) {
744 + address = PPP_ALLSTATIONS;
747 + mode = sc->sc_npmode[NP_IP];
750 + * If this packet has the "low delay" bit set in the IP header,
751 + * put it on the fastq instead.
753 + ip = mtod(m0, struct ip *);
754 + if (ip->ip_tos & IPTOS_LOWDELAY)
755 + m0->m_flags |= M_HIGHPRI;
760 + address = PPP_ALLSTATIONS; /*XXX*/
761 + control = PPP_UI; /*XXX*/
762 + protocol = PPP_IPV6;
763 + mode = sc->sc_npmode[NP_IPV6];
765 +#if 0 /* XXX flowinfo/traffic class, maybe? */
767 + * If this packet has the "low delay" bit set in the IP header,
768 + * put it on the fastq instead.
770 + ip = mtod(m0, struct ip *);
771 + if (ip->ip_tos & IPTOS_LOWDELAY)
772 + m0->m_flags |= M_HIGHPRI;
777 + address = PPP_ADDRESS(dst->sa_data);
778 + control = PPP_CONTROL(dst->sa_data);
779 + protocol = PPP_PROTOCOL(dst->sa_data);
780 + mode = NPMODE_PASS;
783 + printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
784 + error = EAFNOSUPPORT;
789 + * Drop this packet, or return an error, if necessary.
791 + if (mode == NPMODE_ERROR) {
795 + if (mode == NPMODE_DROP) {
801 + * Add PPP header. If no space in first mbuf, allocate another.
802 + * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
804 + if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
805 + m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
812 + m0->m_data -= PPP_HDRLEN;
814 + cp = mtod(m0, u_char *);
817 + *cp++ = protocol >> 8;
818 + *cp++ = protocol & 0xff;
819 + m0->m_len += PPP_HDRLEN;
822 + for (m = m0; m != 0; m = m->m_next)
825 + if (sc->sc_flags & SC_LOG_OUTPKT) {
826 + printf("%s output: ", ifp->if_xname);
830 + if ((protocol & 0x8000) == 0) {
833 + * Apply the pass and active filters to the packet,
834 + * but only if it is a data packet.
836 + if (sc->sc_pass_filt_out.bf_insns != 0
837 + && bpf_filter(sc->sc_pass_filt_out.bf_insns, (u_char *) m0,
839 + error = 0; /* drop this packet */
844 + * Update the time we sent the most recent packet.
846 + if (sc->sc_active_filt_out.bf_insns == 0
847 + || bpf_filter(sc->sc_active_filt_out.bf_insns, (u_char *) m0,
849 + sc->sc_last_sent = time.tv_sec;
852 + * Update the time we sent the most recent packet.
854 + sc->sc_last_sent = time.tv_sec;
855 +#endif /* PPP_FILTER */
860 + * See if bpf wants to look at the packet.
863 + bpf_mtap(sc->sc_bpf, m0);
867 + * Put the packet on the appropriate queue.
870 + if (mode == NPMODE_QUEUE) {
871 + /* XXX we should limit the number of packets on this queue */
872 + *sc->sc_npqtail = m0;
873 + m0->m_nextpkt = NULL;
874 + sc->sc_npqtail = &m0->m_nextpkt;
876 + ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
877 + if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
880 + sc->sc_if.if_oerrors++;
881 + sc->sc_stats.ppp_oerrors++;
885 + IF_ENQUEUE(ifq, m0);
888 + ifp->if_lastchange = time;
889 + ifp->if_opackets++;
890 + ifp->if_obytes += len;
901 + * After a change in the NPmode for some NP, move packets from the
902 + * npqueue to the send queue or the fast queue as appropriate.
903 + * Should be called at splimp, since we muck with the queues.
907 + struct ppp_softc *sc;
909 + struct mbuf *m, **mpp;
910 + struct ifqueue *ifq;
913 + for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
914 + switch (PPP_PROTOCOL(mtod(m, u_char *))) {
916 + mode = sc->sc_npmode[NP_IP];
919 + mode = sc->sc_npmode[NP_IPV6];
922 + mode = NPMODE_PASS;
928 + * This packet can now go on one of the queues to be sent.
930 + *mpp = m->m_nextpkt;
931 + m->m_nextpkt = NULL;
932 + ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
933 + if (IF_QFULL(ifq)) {
935 + sc->sc_if.if_oerrors++;
936 + sc->sc_stats.ppp_oerrors++;
938 + IF_ENQUEUE(ifq, m);
943 + *mpp = m->m_nextpkt;
948 + mpp = &m->m_nextpkt;
952 + sc->sc_npqtail = mpp;
956 + * Transmitter has finished outputting some stuff;
957 + * remember to call sc->sc_start later at splsoftnet.
961 + struct ppp_softc *sc;
965 + sc->sc_flags &= ~SC_TBUSY;
966 + schednetisr(NETISR_PPP);
971 + * Get a packet to send. This procedure is intended to be called at
972 + * splsoftnet, since it may involve time-consuming operations such as
973 + * applying VJ compression, packet compression, address/control and/or
974 + * protocol field compression to the packet.
978 + struct ppp_softc *sc;
980 + struct mbuf *m, *mp;
982 + int address, control, protocol;
986 + * Grab a packet to send: first try the fast queue, then the
990 + IF_DEQUEUE(&sc->sc_fastq, m);
992 + IF_DEQUEUE(&sc->sc_if.if_snd, m);
998 + ++sc->sc_stats.ppp_opackets;
1001 + * Extract the ppp header of the new packet.
1002 + * The ppp header will be in one mbuf.
1004 + cp = mtod(m, u_char *);
1005 + address = PPP_ADDRESS(cp);
1006 + control = PPP_CONTROL(cp);
1007 + protocol = PPP_PROTOCOL(cp);
1009 + switch (protocol) {
1013 + * If the packet is a TCP/IP packet, see if we can compress it.
1015 + if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1020 + ip = (struct ip *) (cp + PPP_HDRLEN);
1021 + if (mp->m_len <= PPP_HDRLEN) {
1025 + ip = mtod(mp, struct ip *);
1027 + /* this code assumes the IP/TCP header is in one non-shared mbuf */
1028 + if (ip->ip_p == IPPROTO_TCP) {
1029 + type = sl_compress_tcp(mp, ip, sc->sc_comp,
1030 + !(sc->sc_flags & SC_NO_TCP_CCID));
1032 + case TYPE_UNCOMPRESSED_TCP:
1033 + protocol = PPP_VJC_UNCOMP;
1035 + case TYPE_COMPRESSED_TCP:
1036 + protocol = PPP_VJC_COMP;
1037 + cp = mtod(m, u_char *);
1038 + cp[0] = address; /* header has moved */
1043 + cp[3] = protocol; /* update protocol in PPP header */
1049 +#ifdef PPP_COMPRESS
1051 + ppp_ccp(sc, m, 0);
1053 +#endif /* PPP_COMPRESS */
1056 +#ifdef PPP_COMPRESS
1057 + if (protocol != PPP_LCP && protocol != PPP_CCP
1058 + && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1059 + struct mbuf *mcomp = NULL;
1063 + for (mp = m; mp != NULL; mp = mp->m_next)
1064 + slen += mp->m_len;
1065 + clen = (*sc->sc_xcomp->compress)
1066 + (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1067 + if (mcomp != NULL) {
1068 + if (sc->sc_flags & SC_CCP_UP) {
1069 + /* Send the compressed packet instead of the original. */
1072 + cp = mtod(m, u_char *);
1075 + /* Can't transmit compressed packets until CCP is up. */
1080 +#endif /* PPP_COMPRESS */
1083 + * Compress the address/control and protocol, if possible.
1085 + if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1086 + control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1087 + protocol != PPP_LCP) {
1088 + /* can compress address/control */
1092 + if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1093 + /* can compress protocol */
1094 + if (mtod(m, u_char *) == cp) {
1095 + cp[2] = cp[1]; /* move address/control up */
1106 + * Software interrupt routine, called at splsoftnet.
1111 + struct ppp_softc *sc;
1117 + for (i = 0; i < NPPP; ++i, ++sc) {
1118 + if (!(sc->sc_flags & SC_TBUSY)
1119 + && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head
1120 + || sc->sc_outm)) {
1122 + sc->sc_flags |= SC_TBUSY;
1124 + (*sc->sc_start)(sc);
1128 + IF_DEQUEUE(&sc->sc_rawq, m);
1132 + ppp_inproc(sc, m);
1138 +#ifdef PPP_COMPRESS
1140 + * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1141 + * 0 if it is about to be transmitted.
1144 +ppp_ccp(sc, m, rcvd)
1145 + struct ppp_softc *sc;
1154 + * Get a pointer to the data after the PPP header.
1156 + if (m->m_len <= PPP_HDRLEN) {
1160 + dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1163 + dp = mtod(mp, u_char *) + PPP_HDRLEN;
1166 + ep = mtod(mp, u_char *) + mp->m_len;
1167 + if (dp + CCP_HDRLEN > ep)
1169 + slen = CCP_LENGTH(dp);
1170 + if (dp + slen > ep) {
1171 + if (sc->sc_flags & SC_DEBUG)
1172 + printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1173 + dp, slen, mtod(mp, u_char *), mp->m_len);
1177 + switch (CCP_CODE(dp)) {
1181 + /* CCP must be going down - disable compression */
1182 + if (sc->sc_flags & SC_CCP_UP) {
1184 + sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1190 + if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1191 + && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1192 + && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1194 + /* we're agreeing to send compressed packets. */
1195 + if (sc->sc_xc_state != NULL
1196 + && (*sc->sc_xcomp->comp_init)
1197 + (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1198 + sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1200 + sc->sc_flags |= SC_COMP_RUN;
1204 + /* peer is agreeing to send compressed packets. */
1205 + if (sc->sc_rc_state != NULL
1206 + && (*sc->sc_rcomp->decomp_init)
1207 + (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1208 + sc->sc_unit, 0, sc->sc_mru,
1209 + sc->sc_flags & SC_DEBUG)) {
1211 + sc->sc_flags |= SC_DECOMP_RUN;
1212 + sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1219 + case CCP_RESETACK:
1220 + if (sc->sc_flags & SC_CCP_UP) {
1222 + if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1223 + (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1225 + if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1226 + (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1228 + sc->sc_flags &= ~SC_DC_ERROR;
1238 + * CCP is down; free (de)compressor state if necessary.
1242 + struct ppp_softc *sc;
1244 + if (sc->sc_xc_state) {
1245 + (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1246 + sc->sc_xc_state = NULL;
1248 + if (sc->sc_rc_state) {
1249 + (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1250 + sc->sc_rc_state = NULL;
1253 +#endif /* PPP_COMPRESS */
1256 + * PPP packet input routine.
1257 + * The caller has checked and removed the FCS and has inserted
1258 + * the address/control bytes and the protocol high byte if they
1262 +ppppktin(sc, m, lost)
1263 + struct ppp_softc *sc;
1270 + m->m_flags |= M_ERRMARK;
1271 + IF_ENQUEUE(&sc->sc_rawq, m);
1272 + schednetisr(NETISR_PPP);
1277 + * Process a received PPP packet, doing decompression as necessary.
1278 + * Should be called at splsoftnet.
1280 +#define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1281 + TYPE_UNCOMPRESSED_TCP)
1285 + struct ppp_softc *sc;
1288 + struct ifnet *ifp = &sc->sc_if;
1289 + struct ifqueue *inq;
1290 + int s, ilen, xlen, proto, rv;
1291 + u_char *cp, adrs, ctrl;
1292 + struct mbuf *mp, *dmp = NULL;
1296 + sc->sc_stats.ppp_ipackets++;
1298 + if (sc->sc_flags & SC_LOG_INPKT) {
1300 + for (mp = m; mp != NULL; mp = mp->m_next)
1301 + ilen += mp->m_len;
1302 + printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1306 + cp = mtod(m, u_char *);
1307 + adrs = PPP_ADDRESS(cp);
1308 + ctrl = PPP_CONTROL(cp);
1309 + proto = PPP_PROTOCOL(cp);
1311 + if (m->m_flags & M_ERRMARK) {
1312 + m->m_flags &= ~M_ERRMARK;
1314 + sc->sc_flags |= SC_VJ_RESET;
1318 +#ifdef PPP_COMPRESS
1320 + * Decompress this packet if necessary, update the receiver's
1321 + * dictionary, or take appropriate action on a CCP packet.
1323 + if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1324 + && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1325 + /* decompress this packet */
1326 + rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1327 + if (rv == DECOMP_OK) {
1329 + if (dmp == NULL) {
1330 + /* no error, but no decompressed packet produced */
1334 + cp = mtod(m, u_char *);
1335 + proto = PPP_PROTOCOL(cp);
1339 + * An error has occurred in decompression.
1340 + * Pass the compressed packet up to pppd, which may take
1341 + * CCP down or issue a Reset-Req.
1343 + if (sc->sc_flags & SC_DEBUG)
1344 + printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1346 + sc->sc_flags |= SC_VJ_RESET;
1347 + if (rv == DECOMP_ERROR)
1348 + sc->sc_flags |= SC_DC_ERROR;
1350 + sc->sc_flags |= SC_DC_FERROR;
1355 + if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1356 + (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1358 + if (proto == PPP_CCP) {
1359 + ppp_ccp(sc, m, 1);
1365 + for (mp = m; mp != NULL; mp = mp->m_next)
1366 + ilen += mp->m_len;
1369 + if (sc->sc_flags & SC_VJ_RESET) {
1371 + * If we've missed a packet, we must toss subsequent compressed
1372 + * packets which don't have an explicit connection ID.
1375 + sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1377 + sc->sc_flags &= ~SC_VJ_RESET;
1382 + * See if we have a VJ-compressed packet to uncompress.
1384 + if (proto == PPP_VJC_COMP) {
1385 + if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1388 + xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1389 + ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1390 + sc->sc_comp, &iphdr, &hlen);
1393 + if (sc->sc_flags & SC_DEBUG)
1394 + printf("%s: VJ uncompress failed on type comp\n",
1399 + /* Copy the PPP and IP headers into a new mbuf. */
1400 + MGETHDR(mp, M_DONTWAIT, MT_DATA);
1404 + mp->m_next = NULL;
1405 + if (hlen + PPP_HDRLEN > MHLEN) {
1406 + MCLGET(mp, M_DONTWAIT);
1407 + if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1409 + goto bad; /* lose if big headers and no clusters */
1412 + cp = mtod(mp, u_char *);
1418 + bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1419 + mp->m_len = hlen + PPP_HDRLEN;
1422 + * Trim the PPP and VJ headers off the old mbuf
1423 + * and stick the new and old mbufs together.
1425 + m->m_data += PPP_HDRLEN + xlen;
1426 + m->m_len -= PPP_HDRLEN + xlen;
1427 + if (m->m_len <= M_TRAILINGSPACE(mp)) {
1428 + bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1429 + mp->m_len += m->m_len;
1430 + MFREE(m, mp->m_next);
1434 + ilen += hlen - xlen;
1436 + } else if (proto == PPP_VJC_UNCOMP) {
1437 + if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1440 + xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1441 + ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1442 + sc->sc_comp, &iphdr, &hlen);
1445 + if (sc->sc_flags & SC_DEBUG)
1446 + printf("%s: VJ uncompress failed on type uncomp\n",
1457 + * If the packet will fit in a header mbuf, don't waste a
1458 + * whole cluster on it.
1460 + if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1461 + MGETHDR(mp, M_DONTWAIT, MT_DATA);
1463 + m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1469 + m->m_pkthdr.len = ilen;
1470 + m->m_pkthdr.rcvif = ifp;
1472 + if ((proto & 0x8000) == 0) {
1475 + * See whether we want to pass this packet, and
1476 + * if it counts as link activity.
1478 + if (sc->sc_pass_filt_in.bf_insns != 0
1479 + && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
1481 + /* drop this packet */
1485 + if (sc->sc_active_filt_in.bf_insns == 0
1486 + || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
1488 + sc->sc_last_recv = time.tv_sec;
1491 + * Record the time that we received this packet.
1493 + sc->sc_last_recv = time.tv_sec;
1494 +#endif /* PPP_FILTER */
1498 + /* See if bpf wants to look at the packet. */
1500 + bpf_mtap(sc->sc_bpf, m);
1508 + * IP packet - take off the ppp header and pass it up to IP.
1510 + if ((ifp->if_flags & IFF_UP) == 0
1511 + || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1512 + /* interface is down - drop the packet. */
1516 + m->m_pkthdr.len -= PPP_HDRLEN;
1517 + m->m_data += PPP_HDRLEN;
1518 + m->m_len -= PPP_HDRLEN;
1520 + if (ipflow_fastforward(m))
1523 + schednetisr(NETISR_IP);
1531 + * IPv6 packet - take off the ppp header and pass it up to IPv6.
1533 + if ((ifp->if_flags & IFF_UP) == 0
1534 + || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1535 + /* interface is down - drop the packet. */
1539 + m->m_pkthdr.len -= PPP_HDRLEN;
1540 + m->m_data += PPP_HDRLEN;
1541 + m->m_len -= PPP_HDRLEN;
1542 + schednetisr(NETISR_IPV6);
1549 + * Some other protocol - place on input queue for read().
1551 + inq = &sc->sc_inq;
1557 + * Put the packet on the appropriate input queue.
1560 + if (IF_QFULL(inq)) {
1563 + if (sc->sc_flags & SC_DEBUG)
1564 + printf("%s: input queue full\n", ifp->if_xname);
1565 + ifp->if_iqdrops++;
1568 + IF_ENQUEUE(inq, m);
1570 + ifp->if_ipackets++;
1571 + ifp->if_ibytes += ilen;
1572 + ifp->if_lastchange = time;
1575 + (*sc->sc_ctlp)(sc);
1581 + sc->sc_if.if_ierrors++;
1582 + sc->sc_stats.ppp_ierrors++;
1585 +#define MAX_DUMP_BYTES 128
1591 + char buf[3*MAX_DUMP_BYTES+4];
1594 + static char digits[] = "0123456789abcdef";
1596 + for (m = m0; m; m = m->m_next) {
1598 + u_char *rptr = (u_char *)m->m_data;
1601 + if (bp > buf + sizeof(buf) - 4)
1603 + *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1604 + *bp++ = digits[*rptr++ & 0xf];
1608 + if (bp > buf + sizeof(buf) - 3)
1618 + printf("%s\n", buf);
1621 +#endif /* NPPP > 0 */