1 /* $NetBSD: if_ppp.c,v 1.46 1998/08/02 15:09:50 sommerfe Exp $ */
2 /* $Id: if_ppp.c,v 1.5 1998/09/02 21:19:44 christos 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>
86 #ifdef __NetBSD_Version__ /* Post 1.3 */
88 #include "opt_gateway.h"
93 #include <sys/socket.h>
94 #include <sys/ioctl.h>
95 #include <sys/kernel.h>
96 #include <sys/systm.h>
98 #include <sys/malloc.h>
101 #include <net/if_types.h>
102 #include <net/netisr.h>
103 #include <net/route.h>
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip.h>
115 #include "bpfilter.h"
117 #include <sys/time.h>
122 #include <net/slcompress.h>
125 #include <net/ppp_defs.h>
126 #include <net/if_ppp.h>
127 #include <net/if_pppvar.h>
128 #include <machine/cpu.h>
131 #define PACKETPTR struct mbuf *
132 #include <net/ppp-comp.h>
135 static int pppsioctl __P((struct ifnet *, u_long, caddr_t));
136 static void ppp_requeue __P((struct ppp_softc *));
137 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
138 static void ppp_ccp_closed __P((struct ppp_softc *));
139 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
140 static void pppdumpm __P((struct mbuf *m0));
143 * Some useful mbuf macros not in mbuf.h.
145 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
147 #define M_DATASTART(m) \
148 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
149 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
151 #define M_DATASIZE(m) \
152 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
153 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
156 * We steal two bits in the mbuf m_flags, to mark high-priority packets
157 * for output, and received packets following lost/corrupted packets.
159 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
160 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
165 * List of compressors we know about.
166 * We leave some space so maybe we can modload compressors.
169 extern struct compressor ppp_bsd_compress;
170 extern struct compressor ppp_deflate, ppp_deflate_draft;
172 struct compressor *ppp_compressors[8] = {
173 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
176 #if DO_DEFLATE && defined(PPP_DEFLATE)
182 #endif /* PPP_COMPRESS */
186 * Called from boot code to establish ppp interfaces.
191 register struct ppp_softc *sc;
194 for (sc = ppp_softc; i < NPPP; sc++) {
195 sc->sc_unit = i; /* XXX */
196 sprintf(sc->sc_if.if_xname, "ppp%d", i++);
197 sc->sc_if.if_softc = sc;
198 sc->sc_if.if_mtu = PPP_MTU;
199 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
200 sc->sc_if.if_type = IFT_PPP;
201 sc->sc_if.if_hdrlen = PPP_HDRLEN;
202 sc->sc_if.if_ioctl = pppsioctl;
203 sc->sc_if.if_output = pppoutput;
204 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
205 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
206 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
207 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
208 if_attach(&sc->sc_if);
210 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
216 * Allocate a ppp interface unit and initialize it.
223 struct ppp_softc *sc;
225 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
226 if (sc->sc_xfer == pid) {
230 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
231 if (sc->sc_devp == NULL)
237 sc->sc_mru = PPP_MRU;
238 sc->sc_relinq = NULL;
239 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
241 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
244 sl_compress_init(sc->sc_comp);
247 sc->sc_xc_state = NULL;
248 sc->sc_rc_state = NULL;
249 #endif /* PPP_COMPRESS */
250 for (i = 0; i < NUM_NP; ++i)
251 sc->sc_npmode[i] = NPMODE_ERROR;
252 sc->sc_npqueue = NULL;
253 sc->sc_npqtail = &sc->sc_npqueue;
254 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
260 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
264 struct ppp_softc *sc;
269 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
273 IF_DEQUEUE(&sc->sc_rawq, m);
279 IF_DEQUEUE(&sc->sc_inq, m);
285 IF_DEQUEUE(&sc->sc_fastq, m);
290 while ((m = sc->sc_npqueue) != NULL) {
291 sc->sc_npqueue = m->m_nextpkt;
294 if (sc->sc_togo != NULL) {
295 m_freem(sc->sc_togo);
300 sc->sc_xc_state = NULL;
301 sc->sc_rc_state = NULL;
302 #endif /* PPP_COMPRESS */
304 if (sc->sc_pass_filt.bf_insns != 0) {
305 FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
306 sc->sc_pass_filt.bf_insns = 0;
307 sc->sc_pass_filt.bf_len = 0;
309 if (sc->sc_active_filt.bf_insns != 0) {
310 FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
311 sc->sc_active_filt.bf_insns = 0;
312 sc->sc_active_filt.bf_len = 0;
314 #endif /* PPP_FILTER */
316 if (sc->sc_comp != 0) {
317 FREE(sc->sc_comp, M_DEVBUF);
324 * Ioctl routine for generic ppp devices.
327 pppioctl(sc, cmd, data, flag, p)
328 struct ppp_softc *sc;
334 int s, error, flags, mru, nb, npx;
335 struct ppp_option_data *odp;
336 struct compressor **cp;
340 struct bpf_program *bp, *nbp;
341 struct bpf_insn *newcode, *oldcode;
343 #endif /* PPP_FILTER */
345 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
350 *(int *)data = sc->sc_inq.ifq_len;
354 *(int *)data = sc->sc_unit; /* XXX */
358 *(u_int *)data = sc->sc_flags;
362 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
364 flags = *(int *)data & SC_MASK;
367 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
371 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
376 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
379 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
384 *(int *)data = sc->sc_mru;
389 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
393 sl_compress_setup(sc->sc_comp, *(int *)data);
400 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
402 sc->sc_xfer = p->p_pid;
406 case PPPIOCSCOMPRESS:
407 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
409 odp = (struct ppp_option_data *) data;
411 if (nb > sizeof(ccp_option))
412 nb = sizeof(ccp_option);
413 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
415 if (ccp_option[1] < 2) /* preliminary check on the length byte */
417 for (cp = ppp_compressors; *cp != NULL; ++cp)
418 if ((*cp)->compress_proto == ccp_option[0]) {
420 * Found a handler for the protocol - try to allocate
421 * a compressor or decompressor.
426 if (sc->sc_xc_state != NULL)
427 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
429 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
430 if (sc->sc_xc_state == NULL) {
431 if (sc->sc_flags & SC_DEBUG)
432 printf("%s: comp_alloc failed\n",
437 sc->sc_flags &= ~SC_COMP_RUN;
441 if (sc->sc_rc_state != NULL)
442 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
444 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
445 if (sc->sc_rc_state == NULL) {
446 if (sc->sc_flags & SC_DEBUG)
447 printf("%s: decomp_alloc failed\n",
452 sc->sc_flags &= ~SC_DECOMP_RUN;
457 if (sc->sc_flags & SC_DEBUG)
458 printf("%s: no compressor for [%x %x %x], %x\n",
459 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
461 return (EINVAL); /* no handler found */
462 #endif /* PPP_COMPRESS */
466 npi = (struct npioctl *) data;
467 switch (npi->protocol) {
474 if (cmd == PPPIOCGNPMODE) {
475 npi->mode = sc->sc_npmode[npx];
477 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
479 if (npi->mode != sc->sc_npmode[npx]) {
481 sc->sc_npmode[npx] = npi->mode;
482 if (npi->mode != NPMODE_QUEUE) {
494 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
495 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
502 nbp = (struct bpf_program *) data;
503 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
505 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
506 if (newcodelen != 0) {
507 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
509 return EINVAL; /* or sumpin */
511 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
513 FREE(newcode, M_DEVBUF);
516 if (!bpf_validate(newcode, nbp->bf_len)) {
517 FREE(newcode, M_DEVBUF);
522 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
523 oldcode = bp->bf_insns;
525 bp->bf_len = nbp->bf_len;
526 bp->bf_insns = newcode;
529 FREE(oldcode, M_DEVBUF);
540 * Process an ioctl request to the ppp network interface.
543 pppsioctl(ifp, cmd, data)
544 register struct ifnet *ifp;
548 register struct proc *p = curproc; /* XXX */
549 register struct ppp_softc *sc = ifp->if_softc;
550 register struct ifaddr *ifa = (struct ifaddr *)data;
551 register struct ifreq *ifr = (struct ifreq *)data;
552 struct ppp_stats *psp;
554 struct ppp_comp_stats *pcp;
556 int s = splimp(), error = 0;
560 if ((ifp->if_flags & IFF_RUNNING) == 0)
561 ifp->if_flags &= ~IFF_UP;
565 if (ifa->ifa_addr->sa_family != AF_INET)
566 error = EAFNOSUPPORT;
570 if (ifa->ifa_addr->sa_family != AF_INET)
571 error = EAFNOSUPPORT;
575 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
577 sc->sc_if.if_mtu = ifr->ifr_mtu;
581 ifr->ifr_mtu = sc->sc_if.if_mtu;
587 error = EAFNOSUPPORT;
590 switch(ifr->ifr_addr.sa_family) {
596 error = EAFNOSUPPORT;
602 psp = &((struct ifpppstatsreq *) data)->stats;
603 bzero(psp, sizeof(*psp));
604 psp->p = sc->sc_stats;
605 #if defined(VJC) && !defined(SL_NO_STATS)
607 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
608 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
609 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
610 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
611 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
612 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
613 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
614 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
621 pcp = &((struct ifpppcstatsreq *) data)->stats;
622 bzero(pcp, sizeof(*pcp));
623 if (sc->sc_xc_state != NULL)
624 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
625 if (sc->sc_rc_state != NULL)
626 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
628 #endif /* PPP_COMPRESS */
638 * Queue a packet. Start transmission if not active.
639 * Packet is placed in Information field of PPP frame.
642 pppoutput(ifp, m0, dst, rtp)
645 struct sockaddr *dst;
648 register struct ppp_softc *sc = ifp->if_softc;
649 int protocol, address, control;
658 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
659 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
660 error = ENETDOWN; /* sort of */
665 * Compute PPP header.
667 m0->m_flags &= ~M_HIGHPRI;
668 switch (dst->sa_family) {
671 address = PPP_ALLSTATIONS;
674 mode = sc->sc_npmode[NP_IP];
677 * If this packet has the "low delay" bit set in the IP header,
678 * put it on the fastq instead.
680 ip = mtod(m0, struct ip *);
681 if (ip->ip_tos & IPTOS_LOWDELAY)
682 m0->m_flags |= M_HIGHPRI;
686 address = PPP_ADDRESS(dst->sa_data);
687 control = PPP_CONTROL(dst->sa_data);
688 protocol = PPP_PROTOCOL(dst->sa_data);
692 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
693 error = EAFNOSUPPORT;
698 * Drop this packet, or return an error, if necessary.
700 if (mode == NPMODE_ERROR) {
704 if (mode == NPMODE_DROP) {
710 * Add PPP header. If no space in first mbuf, allocate another.
711 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
713 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
714 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
721 m0->m_data -= PPP_HDRLEN;
723 cp = mtod(m0, u_char *);
726 *cp++ = protocol >> 8;
727 *cp++ = protocol & 0xff;
728 m0->m_len += PPP_HDRLEN;
731 for (m = m0; m != 0; m = m->m_next)
734 if (sc->sc_flags & SC_LOG_OUTPKT) {
735 printf("%s output: ", ifp->if_xname);
739 if ((protocol & 0x8000) == 0) {
742 * Apply the pass and active filters to the packet,
743 * but only if it is a data packet.
745 *mtod(m0, u_char *) = 1; /* indicates outbound */
746 if (sc->sc_pass_filt.bf_insns != 0
747 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
749 error = 0; /* drop this packet */
754 * Update the time we sent the most recent packet.
756 if (sc->sc_active_filt.bf_insns == 0
757 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
758 sc->sc_last_sent = time.tv_sec;
760 *mtod(m0, u_char *) = address;
763 * Update the time we sent the most recent packet.
765 sc->sc_last_sent = time.tv_sec;
766 #endif /* PPP_FILTER */
771 * See if bpf wants to look at the packet.
774 bpf_mtap(sc->sc_bpf, m0);
778 * Put the packet on the appropriate queue.
781 if (mode == NPMODE_QUEUE) {
782 /* XXX we should limit the number of packets on this queue */
783 *sc->sc_npqtail = m0;
784 m0->m_nextpkt = NULL;
785 sc->sc_npqtail = &m0->m_nextpkt;
787 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
788 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
791 sc->sc_if.if_oerrors++;
792 sc->sc_stats.ppp_oerrors++;
799 ifp->if_lastchange = time;
801 ifp->if_obytes += len;
812 * After a change in the NPmode for some NP, move packets from the
813 * npqueue to the send queue or the fast queue as appropriate.
814 * Should be called at splimp, since we muck with the queues.
818 struct ppp_softc *sc;
820 struct mbuf *m, **mpp;
824 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
825 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
827 mode = sc->sc_npmode[NP_IP];
836 * This packet can now go on one of the queues to be sent.
840 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
843 sc->sc_if.if_oerrors++;
844 sc->sc_stats.ppp_oerrors++;
860 sc->sc_npqtail = mpp;
864 * Transmitter has finished outputting some stuff;
865 * remember to call sc->sc_start later at splsoftnet.
869 struct ppp_softc *sc;
873 sc->sc_flags &= ~SC_TBUSY;
874 schednetisr(NETISR_PPP);
879 * Get a packet to send. This procedure is intended to be called at
880 * splsoftnet, since it may involve time-consuming operations such as
881 * applying VJ compression, packet compression, address/control and/or
882 * protocol field compression to the packet.
886 struct ppp_softc *sc;
890 int address, control, protocol;
894 * Grab a packet to send: first try the fast queue, then the
898 IF_DEQUEUE(&sc->sc_fastq, m);
900 IF_DEQUEUE(&sc->sc_if.if_snd, m);
906 ++sc->sc_stats.ppp_opackets;
909 * Extract the ppp header of the new packet.
910 * The ppp header will be in one mbuf.
912 cp = mtod(m, u_char *);
913 address = PPP_ADDRESS(cp);
914 control = PPP_CONTROL(cp);
915 protocol = PPP_PROTOCOL(cp);
921 * If the packet is a TCP/IP packet, see if we can compress it.
923 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
928 ip = (struct ip *) (cp + PPP_HDRLEN);
929 if (mp->m_len <= PPP_HDRLEN) {
933 ip = mtod(mp, struct ip *);
935 /* this code assumes the IP/TCP header is in one non-shared mbuf */
936 if (ip->ip_p == IPPROTO_TCP) {
937 type = sl_compress_tcp(mp, ip, sc->sc_comp,
938 !(sc->sc_flags & SC_NO_TCP_CCID));
940 case TYPE_UNCOMPRESSED_TCP:
941 protocol = PPP_VJC_UNCOMP;
943 case TYPE_COMPRESSED_TCP:
944 protocol = PPP_VJC_COMP;
945 cp = mtod(m, u_char *);
946 cp[0] = address; /* header has moved */
951 cp[3] = protocol; /* update protocol in PPP header */
961 #endif /* PPP_COMPRESS */
965 if (protocol != PPP_LCP && protocol != PPP_CCP
966 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
967 struct mbuf *mcomp = NULL;
971 for (mp = m; mp != NULL; mp = mp->m_next)
973 clen = (*sc->sc_xcomp->compress)
974 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
976 if (sc->sc_flags & SC_CCP_UP) {
977 /* Send the compressed packet instead of the original. */
980 cp = mtod(m, u_char *);
983 /* Can't transmit compressed packets until CCP is up. */
988 #endif /* PPP_COMPRESS */
991 * Compress the address/control and protocol, if possible.
993 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
994 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
995 protocol != PPP_LCP) {
996 /* can compress address/control */
1000 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1001 /* can compress protocol */
1002 if (mtod(m, u_char *) == cp) {
1003 cp[2] = cp[1]; /* move address/control up */
1014 * Software interrupt routine, called at splsoftnet.
1019 struct ppp_softc *sc;
1025 for (i = 0; i < NPPP; ++i, ++sc) {
1026 if (!(sc->sc_flags & SC_TBUSY)
1027 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head
1030 sc->sc_flags |= SC_TBUSY;
1032 (*sc->sc_start)(sc);
1036 IF_DEQUEUE(&sc->sc_rawq, m);
1048 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1049 * 0 if it is about to be transmitted.
1052 ppp_ccp(sc, m, rcvd)
1053 struct ppp_softc *sc;
1062 * Get a pointer to the data after the PPP header.
1064 if (m->m_len <= PPP_HDRLEN) {
1068 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1071 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1074 ep = mtod(mp, u_char *) + mp->m_len;
1075 if (dp + CCP_HDRLEN > ep)
1077 slen = CCP_LENGTH(dp);
1078 if (dp + slen > ep) {
1079 if (sc->sc_flags & SC_DEBUG)
1080 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1081 dp, slen, mtod(mp, u_char *), mp->m_len);
1085 switch (CCP_CODE(dp)) {
1089 /* CCP must be going down - disable compression */
1090 if (sc->sc_flags & SC_CCP_UP) {
1092 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1098 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1099 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1100 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1102 /* we're agreeing to send compressed packets. */
1103 if (sc->sc_xc_state != NULL
1104 && (*sc->sc_xcomp->comp_init)
1105 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1106 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1108 sc->sc_flags |= SC_COMP_RUN;
1112 /* peer is agreeing to send compressed packets. */
1113 if (sc->sc_rc_state != NULL
1114 && (*sc->sc_rcomp->decomp_init)
1115 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1116 sc->sc_unit, 0, sc->sc_mru,
1117 sc->sc_flags & SC_DEBUG)) {
1119 sc->sc_flags |= SC_DECOMP_RUN;
1120 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1128 if (sc->sc_flags & SC_CCP_UP) {
1130 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1131 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1133 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1134 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1136 sc->sc_flags &= ~SC_DC_ERROR;
1146 * CCP is down; free (de)compressor state if necessary.
1150 struct ppp_softc *sc;
1152 if (sc->sc_xc_state) {
1153 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1154 sc->sc_xc_state = NULL;
1156 if (sc->sc_rc_state) {
1157 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1158 sc->sc_rc_state = NULL;
1161 #endif /* PPP_COMPRESS */
1164 * PPP packet input routine.
1165 * The caller has checked and removed the FCS and has inserted
1166 * the address/control bytes and the protocol high byte if they
1170 ppppktin(sc, m, lost)
1171 struct ppp_softc *sc;
1178 m->m_flags |= M_ERRMARK;
1179 IF_ENQUEUE(&sc->sc_rawq, m);
1180 schednetisr(NETISR_PPP);
1185 * Process a received PPP packet, doing decompression as necessary.
1186 * Should be called at splsoftnet.
1188 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1189 TYPE_UNCOMPRESSED_TCP)
1193 struct ppp_softc *sc;
1196 struct ifnet *ifp = &sc->sc_if;
1197 struct ifqueue *inq;
1198 int s, ilen, xlen, proto, rv;
1199 u_char *cp, adrs, ctrl;
1200 struct mbuf *mp, *dmp = NULL;
1204 sc->sc_stats.ppp_ipackets++;
1206 if (sc->sc_flags & SC_LOG_INPKT) {
1208 for (mp = m; mp != NULL; mp = mp->m_next)
1210 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1214 cp = mtod(m, u_char *);
1215 adrs = PPP_ADDRESS(cp);
1216 ctrl = PPP_CONTROL(cp);
1217 proto = PPP_PROTOCOL(cp);
1219 if (m->m_flags & M_ERRMARK) {
1220 m->m_flags &= ~M_ERRMARK;
1222 sc->sc_flags |= SC_VJ_RESET;
1228 * Decompress this packet if necessary, update the receiver's
1229 * dictionary, or take appropriate action on a CCP packet.
1231 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1232 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1233 /* decompress this packet */
1234 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1235 if (rv == DECOMP_OK) {
1238 /* no error, but no decompressed packet produced */
1242 cp = mtod(m, u_char *);
1243 proto = PPP_PROTOCOL(cp);
1247 * An error has occurred in decompression.
1248 * Pass the compressed packet up to pppd, which may take
1249 * CCP down or issue a Reset-Req.
1251 if (sc->sc_flags & SC_DEBUG)
1252 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1254 sc->sc_flags |= SC_VJ_RESET;
1255 if (rv == DECOMP_ERROR)
1256 sc->sc_flags |= SC_DC_ERROR;
1258 sc->sc_flags |= SC_DC_FERROR;
1263 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1264 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1266 if (proto == PPP_CCP) {
1273 for (mp = m; mp != NULL; mp = mp->m_next)
1277 if (sc->sc_flags & SC_VJ_RESET) {
1279 * If we've missed a packet, we must toss subsequent compressed
1280 * packets which don't have an explicit connection ID.
1283 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1285 sc->sc_flags &= ~SC_VJ_RESET;
1290 * See if we have a VJ-compressed packet to uncompress.
1292 if (proto == PPP_VJC_COMP) {
1293 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1296 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1297 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1298 sc->sc_comp, &iphdr, &hlen);
1301 if (sc->sc_flags & SC_DEBUG)
1302 printf("%s: VJ uncompress failed on type comp\n",
1307 /* Copy the PPP and IP headers into a new mbuf. */
1308 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1313 if (hlen + PPP_HDRLEN > MHLEN) {
1314 MCLGET(mp, M_DONTWAIT);
1315 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1317 goto bad; /* lose if big headers and no clusters */
1320 cp = mtod(mp, u_char *);
1326 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1327 mp->m_len = hlen + PPP_HDRLEN;
1330 * Trim the PPP and VJ headers off the old mbuf
1331 * and stick the new and old mbufs together.
1333 m->m_data += PPP_HDRLEN + xlen;
1334 m->m_len -= PPP_HDRLEN + xlen;
1335 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1336 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1337 mp->m_len += m->m_len;
1338 MFREE(m, mp->m_next);
1342 ilen += hlen - xlen;
1344 } else if (proto == PPP_VJC_UNCOMP) {
1345 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1348 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1349 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1350 sc->sc_comp, &iphdr, &hlen);
1353 if (sc->sc_flags & SC_DEBUG)
1354 printf("%s: VJ uncompress failed on type uncomp\n",
1365 * If the packet will fit in a header mbuf, don't waste a
1366 * whole cluster on it.
1368 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1369 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1371 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1377 m->m_pkthdr.len = ilen;
1378 m->m_pkthdr.rcvif = ifp;
1380 if ((proto & 0x8000) == 0) {
1383 * See whether we want to pass this packet, and
1384 * if it counts as link activity.
1386 adrs = *mtod(m, u_char *); /* save address field */
1387 *mtod(m, u_char *) = 0; /* indicate inbound */
1388 if (sc->sc_pass_filt.bf_insns != 0
1389 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1391 /* drop this packet */
1395 if (sc->sc_active_filt.bf_insns == 0
1396 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1397 sc->sc_last_recv = time.tv_sec;
1399 *mtod(m, u_char *) = adrs;
1402 * Record the time that we received this packet.
1404 sc->sc_last_recv = time.tv_sec;
1405 #endif /* PPP_FILTER */
1409 /* See if bpf wants to look at the packet. */
1411 bpf_mtap(sc->sc_bpf, m);
1419 * IP packet - take off the ppp header and pass it up to IP.
1421 if ((ifp->if_flags & IFF_UP) == 0
1422 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1423 /* interface is down - drop the packet. */
1427 m->m_pkthdr.len -= PPP_HDRLEN;
1428 m->m_data += PPP_HDRLEN;
1429 m->m_len -= PPP_HDRLEN;
1430 #ifdef __NetBSD_Version__
1432 if (ipflow_fastforward(m))
1436 schednetisr(NETISR_IP);
1443 * Some other protocol - place on input queue for read().
1451 * Put the packet on the appropriate input queue.
1454 if (IF_QFULL(inq)) {
1457 if (sc->sc_flags & SC_DEBUG)
1458 printf("%s: input queue full\n", ifp->if_xname);
1465 ifp->if_ibytes += ilen;
1466 ifp->if_lastchange = time;
1475 sc->sc_if.if_ierrors++;
1476 sc->sc_stats.ppp_ierrors++;
1479 #define MAX_DUMP_BYTES 128
1485 char buf[3*MAX_DUMP_BYTES+4];
1488 static char digits[] = "0123456789abcdef";
1490 for (m = m0; m; m = m->m_next) {
1492 u_char *rptr = (u_char *)m->m_data;
1495 if (bp > buf + sizeof(buf) - 4)
1497 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1498 *bp++ = digits[*rptr++ & 0xf];
1502 if (bp > buf + sizeof(buf) - 3)
1512 printf("%s\n", buf);
1515 #endif /* NPPP > 0 */