1 /* $NetBSD: if_ppp.c,v 1.46 1998/08/02 15:09:50 sommerfe Exp $ */
2 /* $Id: if_ppp.c,v 1.6 2002/12/06 12:03:44 paulus Exp $ */
5 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
7 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. The name "Carnegie Mellon University" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For permission or any legal
24 * details, please contact
25 * Office of Technology Transfer
26 * Carnegie Mellon University
28 * Pittsburgh, PA 15213-3890
29 * (412) 268-4387, fax: (412) 268-7395
30 * tech-transfer@andrew.cmu.edu
32 * 4. Redistributions of any form whatsoever must retain the following
34 * "This product includes software developed by Computing Services
35 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 * Carnegie Mellon University
48 * Pittsburgh, PA 15213
53 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
55 * Copyright (c) 1987 Regents of the University of California.
56 * All rights reserved.
58 * Redistribution and use in source and binary forms are permitted
59 * provided that the above copyright notice and this paragraph are
60 * duplicated in all such forms and that any documentation,
61 * advertising materials, and other materials related to such
62 * distribution and use acknowledge that the software was developed
63 * by the University of California, Berkeley. The name of the
64 * University may not be used to endorse or promote products derived
65 * from this software without specific prior written permission.
66 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
67 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
68 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
70 * Serial Line interface
73 * Center for Seismic Studies
74 * 1300 N 17th Street, Suite 1450
75 * Arlington, Virginia 22209
80 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
81 * Converted to 4.3BSD Beta by Chris Torek.
82 * Other changes made at Berkeley, based in part on code by Kirk Smith.
84 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
85 * Added VJ tcp header compression; more unified ioctls
87 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
88 * Cleaned up a lot of the mbuf-related code to fix bugs that
89 * caused system crashes and packet corruption. Changed pppstart
90 * so that it doesn't just give up with a collision if the whole
91 * packet doesn't fit in the output ring buffer.
93 * Added priority queueing for interactive IP packets, following
94 * the model of if_sl.c, plus hooks for bpf.
95 * Paul Mackerras (paulus@cs.anu.edu.au).
98 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
99 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
107 #include <sys/param.h>
109 #ifdef __NetBSD_Version__ /* Post 1.3 */
110 #include "opt_inet.h"
111 #include "opt_gateway.h"
114 #include <sys/proc.h>
115 #include <sys/mbuf.h>
116 #include <sys/socket.h>
117 #include <sys/ioctl.h>
118 #include <sys/kernel.h>
119 #include <sys/systm.h>
120 #include <sys/time.h>
121 #include <sys/malloc.h>
124 #include <net/if_types.h>
125 #include <net/netisr.h>
126 #include <net/route.h>
132 #include <netinet/in.h>
133 #include <netinet/in_systm.h>
134 #include <netinet/in_var.h>
135 #include <netinet/ip.h>
138 #include "bpfilter.h"
140 #include <sys/time.h>
145 #include <net/slcompress.h>
148 #include <net/ppp_defs.h>
149 #include <net/if_ppp.h>
150 #include <net/if_pppvar.h>
151 #include <machine/cpu.h>
154 #define PACKETPTR struct mbuf *
155 #include <net/ppp-comp.h>
158 static int pppsioctl __P((struct ifnet *, u_long, caddr_t));
159 static void ppp_requeue __P((struct ppp_softc *));
160 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
161 static void ppp_ccp_closed __P((struct ppp_softc *));
162 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
163 static void pppdumpm __P((struct mbuf *m0));
166 * Some useful mbuf macros not in mbuf.h.
168 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
170 #define M_DATASTART(m) \
171 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
172 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
174 #define M_DATASIZE(m) \
175 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
176 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
179 * We steal two bits in the mbuf m_flags, to mark high-priority packets
180 * for output, and received packets following lost/corrupted packets.
182 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
183 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
188 * List of compressors we know about.
189 * We leave some space so maybe we can modload compressors.
192 extern struct compressor ppp_bsd_compress;
193 extern struct compressor ppp_deflate, ppp_deflate_draft;
195 struct compressor *ppp_compressors[8] = {
196 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
199 #if DO_DEFLATE && defined(PPP_DEFLATE)
205 #endif /* PPP_COMPRESS */
209 * Called from boot code to establish ppp interfaces.
214 register struct ppp_softc *sc;
217 for (sc = ppp_softc; i < NPPP; sc++) {
218 sc->sc_unit = i; /* XXX */
219 sprintf(sc->sc_if.if_xname, "ppp%d", i++);
220 sc->sc_if.if_softc = sc;
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);
239 * Allocate a ppp interface unit and initialize it.
246 struct ppp_softc *sc;
248 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
249 if (sc->sc_xfer == pid) {
253 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
254 if (sc->sc_devp == NULL)
260 sc->sc_mru = PPP_MRU;
261 sc->sc_relinq = NULL;
262 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
264 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
267 sl_compress_init(sc->sc_comp);
270 sc->sc_xc_state = NULL;
271 sc->sc_rc_state = NULL;
272 #endif /* PPP_COMPRESS */
273 for (i = 0; i < NUM_NP; ++i)
274 sc->sc_npmode[i] = NPMODE_ERROR;
275 sc->sc_npqueue = NULL;
276 sc->sc_npqtail = &sc->sc_npqueue;
277 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
283 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
287 struct ppp_softc *sc;
292 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
296 IF_DEQUEUE(&sc->sc_rawq, m);
302 IF_DEQUEUE(&sc->sc_inq, m);
308 IF_DEQUEUE(&sc->sc_fastq, m);
313 while ((m = sc->sc_npqueue) != NULL) {
314 sc->sc_npqueue = m->m_nextpkt;
317 if (sc->sc_togo != NULL) {
318 m_freem(sc->sc_togo);
323 sc->sc_xc_state = NULL;
324 sc->sc_rc_state = NULL;
325 #endif /* PPP_COMPRESS */
327 if (sc->sc_pass_filt.bf_insns != 0) {
328 FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
329 sc->sc_pass_filt.bf_insns = 0;
330 sc->sc_pass_filt.bf_len = 0;
332 if (sc->sc_active_filt.bf_insns != 0) {
333 FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
334 sc->sc_active_filt.bf_insns = 0;
335 sc->sc_active_filt.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;
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))
394 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
399 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
402 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
407 *(int *)data = sc->sc_mru;
412 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
416 sl_compress_setup(sc->sc_comp, *(int *)data);
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.
449 if (sc->sc_xc_state != NULL)
450 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
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",
460 sc->sc_flags &= ~SC_COMP_RUN;
464 if (sc->sc_rc_state != NULL)
465 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
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",
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],
484 return (EINVAL); /* no handler found */
485 #endif /* PPP_COMPRESS */
489 npi = (struct npioctl *) data;
490 switch (npi->protocol) {
497 if (cmd == PPPIOCGNPMODE) {
498 npi->mode = sc->sc_npmode[npx];
500 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
502 if (npi->mode != sc->sc_npmode[npx]) {
504 sc->sc_npmode[npx] = npi->mode;
505 if (npi->mode != NPMODE_QUEUE) {
517 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
518 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
525 nbp = (struct bpf_program *) data;
526 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
528 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
529 if (newcodelen != 0) {
530 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
532 return EINVAL; /* or sumpin */
534 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
536 FREE(newcode, M_DEVBUF);
539 if (!bpf_validate(newcode, nbp->bf_len)) {
540 FREE(newcode, M_DEVBUF);
545 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
546 oldcode = bp->bf_insns;
548 bp->bf_len = nbp->bf_len;
549 bp->bf_insns = newcode;
552 FREE(oldcode, M_DEVBUF);
563 * Process an ioctl request to the ppp network interface.
566 pppsioctl(ifp, cmd, data)
567 register struct ifnet *ifp;
571 register struct proc *p = curproc; /* XXX */
572 register struct ppp_softc *sc = ifp->if_softc;
573 register struct ifaddr *ifa = (struct ifaddr *)data;
574 register struct ifreq *ifr = (struct ifreq *)data;
575 struct ppp_stats *psp;
577 struct ppp_comp_stats *pcp;
579 int s = splimp(), error = 0;
583 if ((ifp->if_flags & IFF_RUNNING) == 0)
584 ifp->if_flags &= ~IFF_UP;
588 if (ifa->ifa_addr->sa_family != AF_INET)
589 error = EAFNOSUPPORT;
593 if (ifa->ifa_addr->sa_family != AF_INET)
594 error = EAFNOSUPPORT;
598 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
600 sc->sc_if.if_mtu = ifr->ifr_mtu;
604 ifr->ifr_mtu = sc->sc_if.if_mtu;
610 error = EAFNOSUPPORT;
613 switch(ifr->ifr_addr.sa_family) {
619 error = EAFNOSUPPORT;
625 psp = &((struct ifpppstatsreq *) data)->stats;
626 bzero(psp, sizeof(*psp));
627 psp->p = sc->sc_stats;
628 #if defined(VJC) && !defined(SL_NO_STATS)
630 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
631 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
632 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
633 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
634 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
635 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
636 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
637 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
644 pcp = &((struct ifpppcstatsreq *) data)->stats;
645 bzero(pcp, sizeof(*pcp));
646 if (sc->sc_xc_state != NULL)
647 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
648 if (sc->sc_rc_state != NULL)
649 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
651 #endif /* PPP_COMPRESS */
661 * Queue a packet. Start transmission if not active.
662 * Packet is placed in Information field of PPP frame.
665 pppoutput(ifp, m0, dst, rtp)
668 struct sockaddr *dst;
671 register struct ppp_softc *sc = ifp->if_softc;
672 int protocol, address, control;
681 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
682 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
683 error = ENETDOWN; /* sort of */
688 * Compute PPP header.
690 m0->m_flags &= ~M_HIGHPRI;
691 switch (dst->sa_family) {
694 address = PPP_ALLSTATIONS;
697 mode = sc->sc_npmode[NP_IP];
700 * If this packet has the "low delay" bit set in the IP header,
701 * put it on the fastq instead.
703 ip = mtod(m0, struct ip *);
704 if (ip->ip_tos & IPTOS_LOWDELAY)
705 m0->m_flags |= M_HIGHPRI;
709 address = PPP_ADDRESS(dst->sa_data);
710 control = PPP_CONTROL(dst->sa_data);
711 protocol = PPP_PROTOCOL(dst->sa_data);
715 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
716 error = EAFNOSUPPORT;
721 * Drop this packet, or return an error, if necessary.
723 if (mode == NPMODE_ERROR) {
727 if (mode == NPMODE_DROP) {
733 * Add PPP header. If no space in first mbuf, allocate another.
734 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
736 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
737 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
744 m0->m_data -= PPP_HDRLEN;
746 cp = mtod(m0, u_char *);
749 *cp++ = protocol >> 8;
750 *cp++ = protocol & 0xff;
751 m0->m_len += PPP_HDRLEN;
754 for (m = m0; m != 0; m = m->m_next)
757 if (sc->sc_flags & SC_LOG_OUTPKT) {
758 printf("%s output: ", ifp->if_xname);
762 if ((protocol & 0x8000) == 0) {
765 * Apply the pass and active filters to the packet,
766 * but only if it is a data packet.
768 *mtod(m0, u_char *) = 1; /* indicates outbound */
769 if (sc->sc_pass_filt.bf_insns != 0
770 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
772 error = 0; /* drop this packet */
777 * Update the time we sent the most recent packet.
779 if (sc->sc_active_filt.bf_insns == 0
780 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
781 sc->sc_last_sent = time.tv_sec;
783 *mtod(m0, u_char *) = address;
786 * Update the time we sent the most recent packet.
788 sc->sc_last_sent = time.tv_sec;
789 #endif /* PPP_FILTER */
794 * See if bpf wants to look at the packet.
797 bpf_mtap(sc->sc_bpf, m0);
801 * Put the packet on the appropriate queue.
804 if (mode == NPMODE_QUEUE) {
805 /* XXX we should limit the number of packets on this queue */
806 *sc->sc_npqtail = m0;
807 m0->m_nextpkt = NULL;
808 sc->sc_npqtail = &m0->m_nextpkt;
810 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
811 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
814 sc->sc_if.if_oerrors++;
815 sc->sc_stats.ppp_oerrors++;
822 ifp->if_lastchange = time;
824 ifp->if_obytes += len;
835 * After a change in the NPmode for some NP, move packets from the
836 * npqueue to the send queue or the fast queue as appropriate.
837 * Should be called at splimp, since we muck with the queues.
841 struct ppp_softc *sc;
843 struct mbuf *m, **mpp;
847 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
848 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
850 mode = sc->sc_npmode[NP_IP];
859 * This packet can now go on one of the queues to be sent.
863 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
866 sc->sc_if.if_oerrors++;
867 sc->sc_stats.ppp_oerrors++;
883 sc->sc_npqtail = mpp;
887 * Transmitter has finished outputting some stuff;
888 * remember to call sc->sc_start later at splsoftnet.
892 struct ppp_softc *sc;
896 sc->sc_flags &= ~SC_TBUSY;
897 schednetisr(NETISR_PPP);
902 * Get a packet to send. This procedure is intended to be called at
903 * splsoftnet, since it may involve time-consuming operations such as
904 * applying VJ compression, packet compression, address/control and/or
905 * protocol field compression to the packet.
909 struct ppp_softc *sc;
913 int address, control, protocol;
917 * Grab a packet to send: first try the fast queue, then the
921 IF_DEQUEUE(&sc->sc_fastq, m);
923 IF_DEQUEUE(&sc->sc_if.if_snd, m);
929 ++sc->sc_stats.ppp_opackets;
932 * Extract the ppp header of the new packet.
933 * The ppp header will be in one mbuf.
935 cp = mtod(m, u_char *);
936 address = PPP_ADDRESS(cp);
937 control = PPP_CONTROL(cp);
938 protocol = PPP_PROTOCOL(cp);
944 * If the packet is a TCP/IP packet, see if we can compress it.
946 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
951 ip = (struct ip *) (cp + PPP_HDRLEN);
952 if (mp->m_len <= PPP_HDRLEN) {
956 ip = mtod(mp, struct ip *);
958 /* this code assumes the IP/TCP header is in one non-shared mbuf */
959 if (ip->ip_p == IPPROTO_TCP) {
960 type = sl_compress_tcp(mp, ip, sc->sc_comp,
961 !(sc->sc_flags & SC_NO_TCP_CCID));
963 case TYPE_UNCOMPRESSED_TCP:
964 protocol = PPP_VJC_UNCOMP;
966 case TYPE_COMPRESSED_TCP:
967 protocol = PPP_VJC_COMP;
968 cp = mtod(m, u_char *);
969 cp[0] = address; /* header has moved */
974 cp[3] = protocol; /* update protocol in PPP header */
984 #endif /* PPP_COMPRESS */
988 if (protocol != PPP_LCP && protocol != PPP_CCP
989 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
990 struct mbuf *mcomp = NULL;
994 for (mp = m; mp != NULL; mp = mp->m_next)
996 clen = (*sc->sc_xcomp->compress)
997 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
999 if (sc->sc_flags & SC_CCP_UP) {
1000 /* Send the compressed packet instead of the original. */
1003 cp = mtod(m, u_char *);
1006 /* Can't transmit compressed packets until CCP is up. */
1011 #endif /* PPP_COMPRESS */
1014 * Compress the address/control and protocol, if possible.
1016 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1017 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1018 protocol != PPP_LCP) {
1019 /* can compress address/control */
1023 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1024 /* can compress protocol */
1025 if (mtod(m, u_char *) == cp) {
1026 cp[2] = cp[1]; /* move address/control up */
1037 * Software interrupt routine, called at splsoftnet.
1042 struct ppp_softc *sc;
1048 for (i = 0; i < NPPP; ++i, ++sc) {
1049 if (!(sc->sc_flags & SC_TBUSY)
1050 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head
1053 sc->sc_flags |= SC_TBUSY;
1055 (*sc->sc_start)(sc);
1059 IF_DEQUEUE(&sc->sc_rawq, m);
1071 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1072 * 0 if it is about to be transmitted.
1075 ppp_ccp(sc, m, rcvd)
1076 struct ppp_softc *sc;
1085 * Get a pointer to the data after the PPP header.
1087 if (m->m_len <= PPP_HDRLEN) {
1091 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1094 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1097 ep = mtod(mp, u_char *) + mp->m_len;
1098 if (dp + CCP_HDRLEN > ep)
1100 slen = CCP_LENGTH(dp);
1101 if (dp + slen > ep) {
1102 if (sc->sc_flags & SC_DEBUG)
1103 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1104 dp, slen, mtod(mp, u_char *), mp->m_len);
1108 switch (CCP_CODE(dp)) {
1112 /* CCP must be going down - disable compression */
1113 if (sc->sc_flags & SC_CCP_UP) {
1115 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1121 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1122 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1123 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1125 /* we're agreeing to send compressed packets. */
1126 if (sc->sc_xc_state != NULL
1127 && (*sc->sc_xcomp->comp_init)
1128 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1129 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1131 sc->sc_flags |= SC_COMP_RUN;
1135 /* peer is agreeing to send compressed packets. */
1136 if (sc->sc_rc_state != NULL
1137 && (*sc->sc_rcomp->decomp_init)
1138 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1139 sc->sc_unit, 0, sc->sc_mru,
1140 sc->sc_flags & SC_DEBUG)) {
1142 sc->sc_flags |= SC_DECOMP_RUN;
1143 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1151 if (sc->sc_flags & SC_CCP_UP) {
1153 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1154 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1156 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1157 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1159 sc->sc_flags &= ~SC_DC_ERROR;
1169 * CCP is down; free (de)compressor state if necessary.
1173 struct ppp_softc *sc;
1175 if (sc->sc_xc_state) {
1176 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1177 sc->sc_xc_state = NULL;
1179 if (sc->sc_rc_state) {
1180 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1181 sc->sc_rc_state = NULL;
1184 #endif /* PPP_COMPRESS */
1187 * PPP packet input routine.
1188 * The caller has checked and removed the FCS and has inserted
1189 * the address/control bytes and the protocol high byte if they
1193 ppppktin(sc, m, lost)
1194 struct ppp_softc *sc;
1201 m->m_flags |= M_ERRMARK;
1202 IF_ENQUEUE(&sc->sc_rawq, m);
1203 schednetisr(NETISR_PPP);
1208 * Process a received PPP packet, doing decompression as necessary.
1209 * Should be called at splsoftnet.
1211 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1212 TYPE_UNCOMPRESSED_TCP)
1216 struct ppp_softc *sc;
1219 struct ifnet *ifp = &sc->sc_if;
1220 struct ifqueue *inq;
1221 int s, ilen, xlen, proto, rv;
1222 u_char *cp, adrs, ctrl;
1223 struct mbuf *mp, *dmp = NULL;
1227 sc->sc_stats.ppp_ipackets++;
1229 if (sc->sc_flags & SC_LOG_INPKT) {
1231 for (mp = m; mp != NULL; mp = mp->m_next)
1233 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1237 cp = mtod(m, u_char *);
1238 adrs = PPP_ADDRESS(cp);
1239 ctrl = PPP_CONTROL(cp);
1240 proto = PPP_PROTOCOL(cp);
1242 if (m->m_flags & M_ERRMARK) {
1243 m->m_flags &= ~M_ERRMARK;
1245 sc->sc_flags |= SC_VJ_RESET;
1251 * Decompress this packet if necessary, update the receiver's
1252 * dictionary, or take appropriate action on a CCP packet.
1254 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1255 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1256 /* decompress this packet */
1257 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1258 if (rv == DECOMP_OK) {
1261 /* no error, but no decompressed packet produced */
1265 cp = mtod(m, u_char *);
1266 proto = PPP_PROTOCOL(cp);
1270 * An error has occurred in decompression.
1271 * Pass the compressed packet up to pppd, which may take
1272 * CCP down or issue a Reset-Req.
1274 if (sc->sc_flags & SC_DEBUG)
1275 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1277 sc->sc_flags |= SC_VJ_RESET;
1278 if (rv == DECOMP_ERROR)
1279 sc->sc_flags |= SC_DC_ERROR;
1281 sc->sc_flags |= SC_DC_FERROR;
1286 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1287 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1289 if (proto == PPP_CCP) {
1296 for (mp = m; mp != NULL; mp = mp->m_next)
1300 if (sc->sc_flags & SC_VJ_RESET) {
1302 * If we've missed a packet, we must toss subsequent compressed
1303 * packets which don't have an explicit connection ID.
1306 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1308 sc->sc_flags &= ~SC_VJ_RESET;
1313 * See if we have a VJ-compressed packet to uncompress.
1315 if (proto == PPP_VJC_COMP) {
1316 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1319 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1320 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1321 sc->sc_comp, &iphdr, &hlen);
1324 if (sc->sc_flags & SC_DEBUG)
1325 printf("%s: VJ uncompress failed on type comp\n",
1330 /* Copy the PPP and IP headers into a new mbuf. */
1331 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1336 if (hlen + PPP_HDRLEN > MHLEN) {
1337 MCLGET(mp, M_DONTWAIT);
1338 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1340 goto bad; /* lose if big headers and no clusters */
1343 cp = mtod(mp, u_char *);
1349 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1350 mp->m_len = hlen + PPP_HDRLEN;
1353 * Trim the PPP and VJ headers off the old mbuf
1354 * and stick the new and old mbufs together.
1356 m->m_data += PPP_HDRLEN + xlen;
1357 m->m_len -= PPP_HDRLEN + xlen;
1358 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1359 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1360 mp->m_len += m->m_len;
1361 MFREE(m, mp->m_next);
1365 ilen += hlen - xlen;
1367 } else if (proto == PPP_VJC_UNCOMP) {
1368 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1371 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1372 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1373 sc->sc_comp, &iphdr, &hlen);
1376 if (sc->sc_flags & SC_DEBUG)
1377 printf("%s: VJ uncompress failed on type uncomp\n",
1388 * If the packet will fit in a header mbuf, don't waste a
1389 * whole cluster on it.
1391 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1392 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1394 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1400 m->m_pkthdr.len = ilen;
1401 m->m_pkthdr.rcvif = ifp;
1403 if ((proto & 0x8000) == 0) {
1406 * See whether we want to pass this packet, and
1407 * if it counts as link activity.
1409 adrs = *mtod(m, u_char *); /* save address field */
1410 *mtod(m, u_char *) = 0; /* indicate inbound */
1411 if (sc->sc_pass_filt.bf_insns != 0
1412 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1414 /* drop this packet */
1418 if (sc->sc_active_filt.bf_insns == 0
1419 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1420 sc->sc_last_recv = time.tv_sec;
1422 *mtod(m, u_char *) = adrs;
1425 * Record the time that we received this packet.
1427 sc->sc_last_recv = time.tv_sec;
1428 #endif /* PPP_FILTER */
1432 /* See if bpf wants to look at the packet. */
1434 bpf_mtap(sc->sc_bpf, m);
1442 * IP packet - take off the ppp header and pass it up to IP.
1444 if ((ifp->if_flags & IFF_UP) == 0
1445 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1446 /* interface is down - drop the packet. */
1450 m->m_pkthdr.len -= PPP_HDRLEN;
1451 m->m_data += PPP_HDRLEN;
1452 m->m_len -= PPP_HDRLEN;
1453 #ifdef __NetBSD_Version__
1455 if (ipflow_fastforward(m))
1459 schednetisr(NETISR_IP);
1466 * Some other protocol - place on input queue for read().
1474 * Put the packet on the appropriate input queue.
1477 if (IF_QFULL(inq)) {
1480 if (sc->sc_flags & SC_DEBUG)
1481 printf("%s: input queue full\n", ifp->if_xname);
1488 ifp->if_ibytes += ilen;
1489 ifp->if_lastchange = time;
1498 sc->sc_if.if_ierrors++;
1499 sc->sc_stats.ppp_ierrors++;
1502 #define MAX_DUMP_BYTES 128
1508 char buf[3*MAX_DUMP_BYTES+4];
1511 static char digits[] = "0123456789abcdef";
1513 for (m = m0; m; m = m->m_next) {
1515 u_char *rptr = (u_char *)m->m_data;
1518 if (bp > buf + sizeof(buf) - 4)
1520 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1521 *bp++ = digits[*rptr++ & 0xf];
1525 if (bp > buf + sizeof(buf) - 3)
1535 printf("%s\n", buf);
1538 #endif /* NPPP > 0 */