1 /* $Id: if_ppp.c,v 1.9 2002/12/06 12:03:44 paulus Exp $ */
4 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. The name "Carnegie Mellon University" must not be used to
21 * endorse or promote products derived from this software without
22 * prior written permission. For permission or any legal
23 * details, please contact
24 * Office of Technology Transfer
25 * Carnegie Mellon University
27 * Pittsburgh, PA 15213-3890
28 * (412) 268-4387, fax: (412) 268-7395
29 * tech-transfer@andrew.cmu.edu
31 * 4. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by Computing Services
34 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
37 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
38 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
39 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
41 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
42 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45 * Carnegie Mellon University
47 * Pittsburgh, PA 15213
52 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
54 * Copyright (c) 1987 Regents of the University of California.
55 * All rights reserved.
57 * Redistribution and use in source and binary forms are permitted
58 * provided that the above copyright notice and this paragraph are
59 * duplicated in all such forms and that any documentation,
60 * advertising materials, and other materials related to such
61 * distribution and use acknowledge that the software was developed
62 * by the University of California, Berkeley. The name of the
63 * University may not be used to endorse or promote products derived
64 * from this software without specific prior written permission.
65 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
66 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
67 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
69 * Serial Line interface
72 * Center for Seismic Studies
73 * 1300 N 17th Street, Suite 1450
74 * Arlington, Virginia 22209
79 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
80 * Converted to 4.3BSD Beta by Chris Torek.
81 * Other changes made at Berkeley, based in part on code by Kirk Smith.
83 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
84 * Added VJ tcp header compression; more unified ioctls
86 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
87 * Cleaned up a lot of the mbuf-related code to fix bugs that
88 * caused system crashes and packet corruption. Changed pppstart
89 * so that it doesn't just give up with a collision if the whole
90 * packet doesn't fit in the output ring buffer.
92 * Added priority queueing for interactive IP packets, following
93 * the model of if_sl.c, plus hooks for bpf.
94 * Paul Mackerras (paulus@cs.anu.edu.au).
97 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
98 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
106 #include <sys/param.h>
107 #include <sys/proc.h>
108 #include <sys/mbuf.h>
109 #include <sys/socket.h>
110 #include <sys/ioctl.h>
111 #include <sys/kernel.h>
112 #include <sys/systm.h>
113 #include <sys/time.h>
114 #include <sys/malloc.h>
116 #if NetBSD1_0 && defined(i386)
117 #include <machine/psl.h>
121 #include <net/if_types.h>
122 #include <net/netisr.h>
123 #include <net/route.h>
129 #include <netinet/in.h>
130 #include <netinet/in_systm.h>
131 #include <netinet/in_var.h>
132 #include <netinet/ip.h>
135 #include "bpfilter.h"
137 #include <sys/time.h>
142 #include <net/slcompress.h>
145 #include <net/ppp_defs.h>
146 #include <net/if_ppp.h>
147 #include <net/if_pppvar.h>
148 #include <machine/cpu.h>
151 #define splsoftnet splnet
155 #define PACKETPTR struct mbuf *
156 #include <net/ppp-comp.h>
159 static int pppsioctl __P((struct ifnet *, u_long, caddr_t));
160 static void ppp_requeue __P((struct ppp_softc *));
161 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
162 static void ppp_ccp_closed __P((struct ppp_softc *));
163 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
164 static void pppdumpm __P((struct mbuf *m0));
167 * Some useful mbuf macros not in mbuf.h.
169 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
171 #define M_DATASTART(m) \
172 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
173 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
175 #define M_DATASIZE(m) \
176 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
177 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
180 * We steal two bits in the mbuf m_flags, to mark high-priority packets
181 * for output, and received packets following lost/corrupted packets.
183 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
184 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
189 * List of compressors we know about.
190 * We leave some space so maybe we can modload compressors.
193 extern struct compressor ppp_bsd_compress;
194 extern struct compressor ppp_deflate, ppp_deflate_draft;
196 struct compressor *ppp_compressors[8] = {
206 #endif /* PPP_COMPRESS */
210 * Called from boot code to establish ppp interfaces.
215 register struct ppp_softc *sc;
218 for (sc = ppp_softc; i < NPPP; sc++) {
219 sc->sc_if.if_name = "ppp";
220 sc->sc_if.if_unit = i++;
221 sc->sc_if.if_mtu = PPP_MTU;
222 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
223 sc->sc_if.if_type = IFT_PPP;
224 sc->sc_if.if_hdrlen = PPP_HDRLEN;
225 sc->sc_if.if_ioctl = pppsioctl;
226 sc->sc_if.if_output = pppoutput;
227 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
228 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
229 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
230 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
231 if_attach(&sc->sc_if);
233 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
237 #if NetBSD1_0 && defined(i386)
239 * XXX kludge to fix the bug in the i386 interrupt handling code,
240 * where software interrupts could be taken while hardware
241 * interrupts were blocked.
243 if ((imask[IPL_TTY] & (1 << SIR_NET)) == 0) {
244 imask[IPL_TTY] |= (1 << SIR_NET);
245 intr_calculatemasks();
251 * Allocate a ppp interface unit and initialize it.
258 struct ppp_softc *sc;
260 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
261 if (sc->sc_xfer == pid) {
265 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
266 if (sc->sc_devp == NULL)
272 sc->sc_mru = PPP_MRU;
273 sc->sc_relinq = NULL;
274 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
276 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
279 sl_compress_init(sc->sc_comp, -1);
282 sc->sc_xc_state = NULL;
283 sc->sc_rc_state = NULL;
284 #endif /* PPP_COMPRESS */
285 for (i = 0; i < NUM_NP; ++i)
286 sc->sc_npmode[i] = NPMODE_ERROR;
287 sc->sc_npqueue = NULL;
288 sc->sc_npqtail = &sc->sc_npqueue;
289 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
295 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
299 struct ppp_softc *sc;
304 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
308 IF_DEQUEUE(&sc->sc_rawq, m);
314 IF_DEQUEUE(&sc->sc_inq, m);
320 IF_DEQUEUE(&sc->sc_fastq, m);
325 while ((m = sc->sc_npqueue) != NULL) {
326 sc->sc_npqueue = m->m_nextpkt;
329 if (sc->sc_togo != NULL) {
330 m_freem(sc->sc_togo);
335 sc->sc_xc_state = NULL;
336 sc->sc_rc_state = NULL;
337 #endif /* PPP_COMPRESS */
339 if (sc->sc_pass_filt.bf_insns != 0) {
340 FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
341 sc->sc_pass_filt.bf_insns = 0;
342 sc->sc_pass_filt.bf_len = 0;
344 if (sc->sc_active_filt.bf_insns != 0) {
345 FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
346 sc->sc_active_filt.bf_insns = 0;
347 sc->sc_active_filt.bf_len = 0;
349 #endif /* PPP_FILTER */
351 if (sc->sc_comp != 0) {
352 FREE(sc->sc_comp, M_DEVBUF);
359 * Ioctl routine for generic ppp devices.
362 pppioctl(sc, cmd, data, flag, p)
363 struct ppp_softc *sc;
369 int s, error, flags, mru, nb, npx;
370 struct ppp_option_data *odp;
371 struct compressor **cp;
375 struct bpf_program *bp, *nbp;
376 struct bpf_insn *newcode, *oldcode;
378 #endif /* PPP_FILTER */
380 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
385 *(int *)data = sc->sc_inq.ifq_len;
389 *(int *)data = sc->sc_if.if_unit;
393 *(u_int *)data = sc->sc_flags;
397 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
399 flags = *(int *)data & SC_MASK;
402 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
406 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
411 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
414 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
419 *(int *)data = sc->sc_mru;
424 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
428 sl_compress_init(sc->sc_comp, *(int *)data);
435 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
437 sc->sc_xfer = p->p_pid;
441 case PPPIOCSCOMPRESS:
442 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
444 odp = (struct ppp_option_data *) data;
446 if (nb > sizeof(ccp_option))
447 nb = sizeof(ccp_option);
448 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
450 if (ccp_option[1] < 2) /* preliminary check on the length byte */
452 for (cp = ppp_compressors; *cp != NULL; ++cp)
453 if ((*cp)->compress_proto == ccp_option[0]) {
455 * Found a handler for the protocol - try to allocate
456 * a compressor or decompressor.
461 if (sc->sc_xc_state != NULL)
462 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
464 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
465 if (sc->sc_xc_state == NULL) {
466 if (sc->sc_flags & SC_DEBUG)
467 printf("ppp%d: comp_alloc failed\n",
472 sc->sc_flags &= ~SC_COMP_RUN;
476 if (sc->sc_rc_state != NULL)
477 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
479 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
480 if (sc->sc_rc_state == NULL) {
481 if (sc->sc_flags & SC_DEBUG)
482 printf("ppp%d: decomp_alloc failed\n",
487 sc->sc_flags &= ~SC_DECOMP_RUN;
492 if (sc->sc_flags & SC_DEBUG)
493 printf("ppp%d: no compressor for [%x %x %x], %x\n",
494 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
496 return (EINVAL); /* no handler found */
497 #endif /* PPP_COMPRESS */
501 npi = (struct npioctl *) data;
502 switch (npi->protocol) {
509 if (cmd == PPPIOCGNPMODE) {
510 npi->mode = sc->sc_npmode[npx];
512 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
514 if (npi->mode != sc->sc_npmode[npx]) {
516 sc->sc_npmode[npx] = npi->mode;
517 if (npi->mode != NPMODE_QUEUE) {
529 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
530 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
537 nbp = (struct bpf_program *) data;
538 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
540 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
541 if (newcodelen != 0) {
542 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
544 return EINVAL; /* or sumpin */
546 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
548 FREE(newcode, M_DEVBUF);
551 if (!bpf_validate(newcode, nbp->bf_len)) {
552 FREE(newcode, M_DEVBUF);
557 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
558 oldcode = bp->bf_insns;
560 bp->bf_len = nbp->bf_len;
561 bp->bf_insns = newcode;
564 FREE(oldcode, M_DEVBUF);
575 * Process an ioctl request to the ppp network interface.
578 pppsioctl(ifp, cmd, data)
579 register struct ifnet *ifp;
583 struct proc *p = curproc; /* XXX */
584 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
585 register struct ifaddr *ifa = (struct ifaddr *)data;
586 register struct ifreq *ifr = (struct ifreq *)data;
587 struct ppp_stats *psp;
589 struct ppp_comp_stats *pcp;
591 int s = splimp(), error = 0;
595 if ((ifp->if_flags & IFF_RUNNING) == 0)
596 ifp->if_flags &= ~IFF_UP;
600 if (ifa->ifa_addr->sa_family != AF_INET)
601 error = EAFNOSUPPORT;
605 if (ifa->ifa_addr->sa_family != AF_INET)
606 error = EAFNOSUPPORT;
610 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
612 sc->sc_if.if_mtu = ifr->ifr_mtu;
616 ifr->ifr_mtu = sc->sc_if.if_mtu;
622 error = EAFNOSUPPORT;
625 switch(ifr->ifr_addr.sa_family) {
631 error = EAFNOSUPPORT;
637 psp = &((struct ifpppstatsreq *) data)->stats;
638 bzero(psp, sizeof(*psp));
639 psp->p = sc->sc_stats;
640 #if defined(VJC) && !defined(SL_NO_STATS)
642 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
643 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
644 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
645 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
646 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
647 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
648 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
649 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
656 pcp = &((struct ifpppcstatsreq *) data)->stats;
657 bzero(pcp, sizeof(*pcp));
658 if (sc->sc_xc_state != NULL)
659 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
660 if (sc->sc_rc_state != NULL)
661 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
663 #endif /* PPP_COMPRESS */
673 * Queue a packet. Start transmission if not active.
674 * Packet is placed in Information field of PPP frame.
677 pppoutput(ifp, m0, dst, rtp)
680 struct sockaddr *dst;
683 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
684 int protocol, address, control;
693 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
694 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
695 error = ENETDOWN; /* sort of */
700 * Compute PPP header.
702 m0->m_flags &= ~M_HIGHPRI;
703 switch (dst->sa_family) {
706 address = PPP_ALLSTATIONS;
709 mode = sc->sc_npmode[NP_IP];
712 * If this packet has the "low delay" bit set in the IP header,
713 * put it on the fastq instead.
715 ip = mtod(m0, struct ip *);
716 if (ip->ip_tos & IPTOS_LOWDELAY)
717 m0->m_flags |= M_HIGHPRI;
721 address = PPP_ADDRESS(dst->sa_data);
722 control = PPP_CONTROL(dst->sa_data);
723 protocol = PPP_PROTOCOL(dst->sa_data);
727 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
728 error = EAFNOSUPPORT;
733 * Drop this packet, or return an error, if necessary.
735 if (mode == NPMODE_ERROR) {
739 if (mode == NPMODE_DROP) {
745 * Add PPP header. If no space in first mbuf, allocate another.
746 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
748 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
749 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
756 m0->m_data -= PPP_HDRLEN;
758 cp = mtod(m0, u_char *);
761 *cp++ = protocol >> 8;
762 *cp++ = protocol & 0xff;
763 m0->m_len += PPP_HDRLEN;
766 for (m = m0; m != 0; m = m->m_next)
769 if (sc->sc_flags & SC_LOG_OUTPKT) {
770 printf("ppp%d output: ", ifp->if_unit);
774 if ((protocol & 0x8000) == 0) {
777 * Apply the pass and active filters to the packet,
778 * but only if it is a data packet.
780 *mtod(m0, u_char *) = 1; /* indicates outbound */
781 if (sc->sc_pass_filt.bf_insns != 0
782 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
784 error = 0; /* drop this packet */
789 * Update the time we sent the most recent packet.
791 if (sc->sc_active_filt.bf_insns == 0
792 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
793 sc->sc_last_sent = time.tv_sec;
795 *mtod(m0, u_char *) = address;
798 * Update the time we sent the most recent data packet.
800 sc->sc_last_sent = time.tv_sec;
801 #endif /* PPP_FILTER */
806 * See if bpf wants to look at the packet.
809 bpf_mtap(sc->sc_bpf, m0);
813 * Put the packet on the appropriate queue.
816 if (mode == NPMODE_QUEUE) {
817 /* XXX we should limit the number of packets on this queue */
818 *sc->sc_npqtail = m0;
819 m0->m_nextpkt = NULL;
820 sc->sc_npqtail = &m0->m_nextpkt;
822 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
823 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
826 sc->sc_if.if_oerrors++;
827 sc->sc_stats.ppp_oerrors++;
834 ifp->if_lastchange = time;
836 ifp->if_obytes += len;
847 * After a change in the NPmode for some NP, move packets from the
848 * npqueue to the send queue or the fast queue as appropriate.
849 * Should be called at splsoftnet.
853 struct ppp_softc *sc;
855 struct mbuf *m, **mpp;
859 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
860 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
862 mode = sc->sc_npmode[NP_IP];
871 * This packet can now go on one of the queues to be sent.
875 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
878 sc->sc_if.if_oerrors++;
879 sc->sc_stats.ppp_oerrors++;
895 sc->sc_npqtail = mpp;
899 * Transmitter has finished outputting some stuff;
900 * remember to call sc->sc_start later at splsoftnet.
904 struct ppp_softc *sc;
908 sc->sc_flags &= ~SC_TBUSY;
909 schednetisr(NETISR_PPP);
914 * Get a packet to send. This procedure is intended to be called at
915 * splsoftnet, since it may involve time-consuming operations such as
916 * applying VJ compression, packet compression, address/control and/or
917 * protocol field compression to the packet.
921 struct ppp_softc *sc;
925 int address, control, protocol;
928 * Grab a packet to send: first try the fast queue, then the
931 IF_DEQUEUE(&sc->sc_fastq, m);
933 IF_DEQUEUE(&sc->sc_if.if_snd, m);
937 ++sc->sc_stats.ppp_opackets;
940 * Extract the ppp header of the new packet.
941 * The ppp header will be in one mbuf.
943 cp = mtod(m, u_char *);
944 address = PPP_ADDRESS(cp);
945 control = PPP_CONTROL(cp);
946 protocol = PPP_PROTOCOL(cp);
952 * If the packet is a TCP/IP packet, see if we can compress it.
954 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
959 ip = (struct ip *) (cp + PPP_HDRLEN);
960 if (mp->m_len <= PPP_HDRLEN) {
964 ip = mtod(mp, struct ip *);
966 /* this code assumes the IP/TCP header is in one non-shared mbuf */
967 if (ip->ip_p == IPPROTO_TCP) {
968 type = sl_compress_tcp(mp, ip, sc->sc_comp,
969 !(sc->sc_flags & SC_NO_TCP_CCID));
971 case TYPE_UNCOMPRESSED_TCP:
972 protocol = PPP_VJC_UNCOMP;
974 case TYPE_COMPRESSED_TCP:
975 protocol = PPP_VJC_COMP;
976 cp = mtod(m, u_char *);
977 cp[0] = address; /* header has moved */
982 cp[3] = protocol; /* update protocol in PPP header */
992 #endif /* PPP_COMPRESS */
996 if (protocol != PPP_LCP && protocol != PPP_CCP
997 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
998 struct mbuf *mcomp = NULL;
1002 for (mp = m; mp != NULL; mp = mp->m_next)
1004 clen = (*sc->sc_xcomp->compress)
1005 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1006 if (mcomp != NULL) {
1007 if (sc->sc_flags & SC_CCP_UP) {
1008 /* Send the compressed packet instead of the original. */
1011 cp = mtod(m, u_char *);
1014 /* Can't transmit compressed packets until CCP is up. */
1019 #endif /* PPP_COMPRESS */
1022 * Compress the address/control and protocol, if possible.
1024 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1025 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1026 protocol != PPP_LCP) {
1027 /* can compress address/control */
1031 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1032 /* can compress protocol */
1033 if (mtod(m, u_char *) == cp) {
1034 cp[2] = cp[1]; /* move address/control up */
1045 * Software interrupt routine, called at splsoftnet.
1050 struct ppp_softc *sc;
1056 for (i = 0; i < NPPP; ++i, ++sc) {
1057 if (!(sc->sc_flags & SC_TBUSY)
1058 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1060 sc->sc_flags |= SC_TBUSY;
1062 (*sc->sc_start)(sc);
1066 IF_DEQUEUE(&sc->sc_rawq, m);
1078 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1079 * 0 if it is about to be transmitted.
1082 ppp_ccp(sc, m, rcvd)
1083 struct ppp_softc *sc;
1092 * Get a pointer to the data after the PPP header.
1094 if (m->m_len <= PPP_HDRLEN) {
1098 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1101 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1104 ep = mtod(mp, u_char *) + mp->m_len;
1105 if (dp + CCP_HDRLEN > ep)
1107 slen = CCP_LENGTH(dp);
1108 if (dp + slen > ep) {
1109 if (sc->sc_flags & SC_DEBUG)
1110 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1111 dp, slen, mtod(mp, u_char *), mp->m_len);
1115 switch (CCP_CODE(dp)) {
1119 /* CCP must be going down - disable compression */
1120 if (sc->sc_flags & SC_CCP_UP) {
1122 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1128 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1129 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1130 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1132 /* we're agreeing to send compressed packets. */
1133 if (sc->sc_xc_state != NULL
1134 && (*sc->sc_xcomp->comp_init)
1135 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1136 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1138 sc->sc_flags |= SC_COMP_RUN;
1142 /* peer is agreeing to send compressed packets. */
1143 if (sc->sc_rc_state != NULL
1144 && (*sc->sc_rcomp->decomp_init)
1145 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1146 sc->sc_if.if_unit, 0, sc->sc_mru,
1147 sc->sc_flags & SC_DEBUG)) {
1149 sc->sc_flags |= SC_DECOMP_RUN;
1150 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1158 if (sc->sc_flags & SC_CCP_UP) {
1160 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1161 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1163 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1164 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1166 sc->sc_flags &= ~SC_DC_ERROR;
1176 * CCP is down; free (de)compressor state if necessary.
1180 struct ppp_softc *sc;
1182 if (sc->sc_xc_state) {
1183 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1184 sc->sc_xc_state = NULL;
1186 if (sc->sc_rc_state) {
1187 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1188 sc->sc_rc_state = NULL;
1191 #endif /* PPP_COMPRESS */
1194 * PPP packet input routine.
1195 * The caller has checked and removed the FCS and has inserted
1196 * the address/control bytes and the protocol high byte if they
1200 ppppktin(sc, m, lost)
1201 struct ppp_softc *sc;
1208 m->m_flags |= M_ERRMARK;
1209 IF_ENQUEUE(&sc->sc_rawq, m);
1210 schednetisr(NETISR_PPP);
1215 * Process a received PPP packet, doing decompression as necessary.
1216 * Should be called at splsoftnet.
1218 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1219 TYPE_UNCOMPRESSED_TCP)
1223 struct ppp_softc *sc;
1226 struct ifnet *ifp = &sc->sc_if;
1227 struct ifqueue *inq;
1228 int s, ilen, xlen, proto, rv;
1229 u_char *cp, adrs, ctrl;
1230 struct mbuf *mp, *dmp = NULL;
1234 sc->sc_stats.ppp_ipackets++;
1236 if (sc->sc_flags & SC_LOG_INPKT) {
1238 for (mp = m; mp != NULL; mp = mp->m_next)
1240 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1244 cp = mtod(m, u_char *);
1245 adrs = PPP_ADDRESS(cp);
1246 ctrl = PPP_CONTROL(cp);
1247 proto = PPP_PROTOCOL(cp);
1249 if (m->m_flags & M_ERRMARK) {
1250 m->m_flags &= ~M_ERRMARK;
1252 sc->sc_flags |= SC_VJ_RESET;
1258 * Decompress this packet if necessary, update the receiver's
1259 * dictionary, or take appropriate action on a CCP packet.
1261 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1262 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1263 /* decompress this packet */
1264 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1265 if (rv == DECOMP_OK) {
1268 /* no error, but no decompressed packet produced */
1272 cp = mtod(m, u_char *);
1273 proto = PPP_PROTOCOL(cp);
1277 * An error has occurred in decompression.
1278 * Pass the compressed packet up to pppd, which may take
1279 * CCP down or issue a Reset-Req.
1281 if (sc->sc_flags & SC_DEBUG)
1282 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1284 sc->sc_flags |= SC_VJ_RESET;
1285 if (rv == DECOMP_ERROR)
1286 sc->sc_flags |= SC_DC_ERROR;
1288 sc->sc_flags |= SC_DC_FERROR;
1293 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1294 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1296 if (proto == PPP_CCP) {
1303 for (mp = m; mp != NULL; mp = mp->m_next)
1307 if (sc->sc_flags & SC_VJ_RESET) {
1309 * If we've missed a packet, we must toss subsequent compressed
1310 * packets which don't have an explicit connection ID.
1313 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1315 sc->sc_flags &= ~SC_VJ_RESET;
1320 * See if we have a VJ-compressed packet to uncompress.
1322 if (proto == PPP_VJC_COMP) {
1323 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1326 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1327 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1328 sc->sc_comp, &iphdr, &hlen);
1331 if (sc->sc_flags & SC_DEBUG)
1332 printf("ppp%d: VJ uncompress failed on type comp\n",
1337 /* Copy the PPP and IP headers into a new mbuf. */
1338 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1343 if (hlen + PPP_HDRLEN > MHLEN) {
1344 MCLGET(mp, M_DONTWAIT);
1345 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1347 goto bad; /* lose if big headers and no clusters */
1350 cp = mtod(mp, u_char *);
1356 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1357 mp->m_len = hlen + PPP_HDRLEN;
1360 * Trim the PPP and VJ headers off the old mbuf
1361 * and stick the new and old mbufs together.
1363 m->m_data += PPP_HDRLEN + xlen;
1364 m->m_len -= PPP_HDRLEN + xlen;
1365 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1366 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1367 mp->m_len += m->m_len;
1368 MFREE(m, mp->m_next);
1372 ilen += hlen - xlen;
1374 } else if (proto == PPP_VJC_UNCOMP) {
1375 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1378 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1379 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1380 sc->sc_comp, &iphdr, &hlen);
1383 if (sc->sc_flags & SC_DEBUG)
1384 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1395 * If the packet will fit in a header mbuf, don't waste a
1396 * whole cluster on it.
1398 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1399 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1401 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1407 m->m_pkthdr.len = ilen;
1408 m->m_pkthdr.rcvif = ifp;
1410 if ((proto & 0x8000) == 0) {
1413 * See whether we want to pass this packet, and
1414 * if it counts as link activity.
1416 adrs = *mtod(m, u_char *); /* save address field */
1417 *mtod(m, u_char *) = 0; /* indicate inbound */
1418 if (sc->sc_pass_filt.bf_insns != 0
1419 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1421 /* drop this packet */
1425 if (sc->sc_active_filt.bf_insns == 0
1426 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1427 sc->sc_last_recv = time.tv_sec;
1429 *mtod(m, u_char *) = adrs;
1432 * Record the time that we received this packet.
1434 sc->sc_last_recv = time.tv_sec;
1435 #endif /* PPP_FILTER */
1439 /* See if bpf wants to look at the packet. */
1441 bpf_mtap(sc->sc_bpf, m);
1449 * IP packet - take off the ppp header and pass it up to IP.
1451 if ((ifp->if_flags & IFF_UP) == 0
1452 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1453 /* interface is down - drop the packet. */
1457 m->m_pkthdr.len -= PPP_HDRLEN;
1458 m->m_data += PPP_HDRLEN;
1459 m->m_len -= PPP_HDRLEN;
1460 schednetisr(NETISR_IP);
1467 * Some other protocol - place on input queue for read().
1475 * Put the packet on the appropriate input queue.
1478 if (IF_QFULL(inq)) {
1481 if (sc->sc_flags & SC_DEBUG)
1482 printf("ppp%d: input queue full\n", ifp->if_unit);
1489 ifp->if_ibytes += ilen;
1490 ifp->if_lastchange = time;
1499 sc->sc_if.if_ierrors++;
1500 sc->sc_stats.ppp_ierrors++;
1503 #define MAX_DUMP_BYTES 128
1509 char buf[3*MAX_DUMP_BYTES+4];
1512 static char digits[] = "0123456789abcdef";
1514 for (m = m0; m; m = m->m_next) {
1516 u_char *rptr = (u_char *)m->m_data;
1519 if (bp > buf + sizeof(buf) - 4)
1521 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1522 *bp++ = digits[*rptr++ & 0xf];
1526 if (bp > buf + sizeof(buf) - 3)
1536 printf("%s\n", buf);
1539 #endif /* NPPP > 0 */