2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Carnegie Mellon University
22 * Pittsburgh, PA 15213
27 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
29 * Copyright (c) 1987 Regents of the University of California.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms are permitted
33 * provided that the above copyright notice and this paragraph are
34 * duplicated in all such forms and that any documentation,
35 * advertising materials, and other materials related to such
36 * distribution and use acknowledge that the software was developed
37 * by the University of California, Berkeley. The name of the
38 * University may not be used to endorse or promote products derived
39 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
44 * Serial Line interface
47 * Center for Seismic Studies
48 * 1300 N 17th Street, Suite 1450
49 * Arlington, Virginia 22209
54 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
55 * Converted to 4.3BSD Beta by Chris Torek.
56 * Other changes made at Berkeley, based in part on code by Kirk Smith.
58 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59 * Added VJ tcp header compression; more unified ioctls
61 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
62 * Cleaned up a lot of the mbuf-related code to fix bugs that
63 * caused system crashes and packet corruption. Changed pppstart
64 * so that it doesn't just give up with a collision if the whole
65 * packet doesn't fit in the output ring buffer.
67 * Added priority queueing for interactive IP packets, following
68 * the model of if_sl.c, plus hooks for bpf.
69 * Paul Mackerras (paulus@cs.anu.edu.au).
72 /* $Id: if_ppp.c,v 1.14 1998/03/25 04:05:01 paulus Exp $ */
73 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
74 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
82 #include <sys/param.h>
83 #include <sys/systm.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/kernel.h>
90 #include <sys/malloc.h>
93 #include <net/if_types.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
101 #include <netinet/in.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip.h>
107 #include "bpfilter.h"
113 #include <net/pppcompress.h>
116 #include <net/ppp_defs.h>
117 #include <net/if_ppp.h>
118 #include <net/if_pppvar.h>
119 #include <machine/cpu.h>
121 #define splsoftnet splnet
124 /* This definition should be moved to net/netisr.h */
125 #define NETISR_PPP 26 /* PPP software interrupt */
129 #define PACKETPTR struct mbuf *
130 #include <net/ppp-comp.h>
133 static int pppsioctl __P((struct ifnet *, int, caddr_t));
134 static void ppp_requeue __P((struct ppp_softc *));
135 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
136 static void ppp_ccp_closed __P((struct ppp_softc *));
137 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
138 static void pppdumpm __P((struct mbuf *m0));
141 * Some useful mbuf macros not in mbuf.h.
143 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
145 #define M_DATASTART(m) \
146 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
147 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
149 #define M_DATASIZE(m) \
150 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
151 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
154 * We steal two bits in the mbuf m_flags, to mark high-priority packets
155 * for output, and received packets following lost/corrupted packets.
157 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
158 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
163 * List of compressors we know about.
164 * We leave some space so maybe we can modload compressors.
167 extern struct compressor ppp_bsd_compress;
168 extern struct compressor ppp_deflate, ppp_deflate_draft;
170 struct compressor *ppp_compressors[8] = {
180 #endif /* PPP_COMPRESS */
182 TEXT_SET(pseudo_set, pppattach);
185 * Called from boot code to establish ppp interfaces.
190 register struct ppp_softc *sc;
192 extern void (*netisrs[])__P((void));
194 for (sc = ppp_softc; i < NPPP; sc++) {
195 sc->sc_if.if_name = "ppp";
196 sc->sc_if.if_unit = i++;
197 sc->sc_if.if_mtu = PPP_MTU;
198 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
199 sc->sc_if.if_type = IFT_PPP;
200 sc->sc_if.if_hdrlen = PPP_HDRLEN;
201 sc->sc_if.if_ioctl = pppsioctl;
202 sc->sc_if.if_output = pppoutput;
203 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
204 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
205 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
206 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
207 if_attach(&sc->sc_if);
209 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
212 netisrs[NETISR_PPP] = pppintr;
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 vjcompress *, sizeof(struct vjcompress),
244 vj_compress_init(sc->sc_comp, -1);
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_if.if_unit;
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 vj_compress_init(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("ppp%d: 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("ppp%d: decomp_alloc failed\n",
452 sc->sc_flags &= ~SC_DECOMP_RUN;
457 if (sc->sc_flags & SC_DEBUG)
458 printf("ppp%d: no compressor for [%x %x %x], %x\n",
459 sc->sc_if.if_unit, 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 struct proc *p = curproc; /* XXX */
549 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
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 = &ppp_softc[ifp->if_unit];
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("ppp%d: af%d not supported\n", ifp->if_unit, 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("ppp%d output: ", ifp->if_unit);
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 data 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 splsoftnet.
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;
893 * Grab a packet to send: first try the fast queue, then the
896 IF_DEQUEUE(&sc->sc_fastq, m);
898 IF_DEQUEUE(&sc->sc_if.if_snd, m);
902 ++sc->sc_stats.ppp_opackets;
905 * Extract the ppp header of the new packet.
906 * The ppp header will be in one mbuf.
908 cp = mtod(m, u_char *);
909 address = PPP_ADDRESS(cp);
910 control = PPP_CONTROL(cp);
911 protocol = PPP_PROTOCOL(cp);
917 * If the packet is a TCP/IP packet, see if we can compress it.
919 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
924 ip = (struct ip *) (cp + PPP_HDRLEN);
925 if (mp->m_len <= PPP_HDRLEN) {
929 ip = mtod(mp, struct ip *);
931 /* this code assumes the IP/TCP header is in one non-shared mbuf */
932 if (ip->ip_p == IPPROTO_TCP) {
933 type = vj_compress_tcp(mp, ip, sc->sc_comp,
934 !(sc->sc_flags & SC_NO_TCP_CCID));
936 case TYPE_UNCOMPRESSED_TCP:
937 protocol = PPP_VJC_UNCOMP;
939 case TYPE_COMPRESSED_TCP:
940 protocol = PPP_VJC_COMP;
941 cp = mtod(m, u_char *);
942 cp[0] = address; /* header has moved */
947 cp[3] = protocol; /* update protocol in PPP header */
957 #endif /* PPP_COMPRESS */
961 if (protocol != PPP_LCP && protocol != PPP_CCP
962 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
963 struct mbuf *mcomp = NULL;
967 for (mp = m; mp != NULL; mp = mp->m_next)
969 clen = (*sc->sc_xcomp->compress)
970 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
972 if (sc->sc_flags & SC_CCP_UP) {
973 /* Send the compressed packet instead of the original. */
976 cp = mtod(m, u_char *);
979 /* Can't transmit compressed packets until CCP is up. */
984 #endif /* PPP_COMPRESS */
987 * Compress the address/control and protocol, if possible.
989 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
990 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
991 protocol != PPP_LCP) {
992 /* can compress address/control */
996 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
997 /* can compress protocol */
998 if (mtod(m, u_char *) == cp) {
999 cp[2] = cp[1]; /* move address/control up */
1010 * Software interrupt routine, called at splsoftnet.
1015 struct ppp_softc *sc;
1021 for (i = 0; i < NPPP; ++i, ++sc) {
1022 if (!(sc->sc_flags & SC_TBUSY)
1023 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1025 sc->sc_flags |= SC_TBUSY;
1027 (*sc->sc_start)(sc);
1031 IF_DEQUEUE(&sc->sc_rawq, m);
1043 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1044 * 0 if it is about to be transmitted.
1047 ppp_ccp(sc, m, rcvd)
1048 struct ppp_softc *sc;
1057 * Get a pointer to the data after the PPP header.
1059 if (m->m_len <= PPP_HDRLEN) {
1063 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1066 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1069 ep = mtod(mp, u_char *) + mp->m_len;
1070 if (dp + CCP_HDRLEN > ep)
1072 slen = CCP_LENGTH(dp);
1073 if (dp + slen > ep) {
1074 if (sc->sc_flags & SC_DEBUG)
1075 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1076 dp, slen, mtod(mp, u_char *), mp->m_len);
1080 switch (CCP_CODE(dp)) {
1084 /* CCP must be going down - disable compression */
1085 if (sc->sc_flags & SC_CCP_UP) {
1087 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1093 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1094 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1095 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1097 /* we're agreeing to send compressed packets. */
1098 if (sc->sc_xc_state != NULL
1099 && (*sc->sc_xcomp->comp_init)
1100 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1101 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1103 sc->sc_flags |= SC_COMP_RUN;
1107 /* peer is agreeing to send compressed packets. */
1108 if (sc->sc_rc_state != NULL
1109 && (*sc->sc_rcomp->decomp_init)
1110 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1111 sc->sc_if.if_unit, 0, sc->sc_mru,
1112 sc->sc_flags & SC_DEBUG)) {
1114 sc->sc_flags |= SC_DECOMP_RUN;
1115 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1123 if (sc->sc_flags & SC_CCP_UP) {
1125 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1126 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1128 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1129 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1131 sc->sc_flags &= ~SC_DC_ERROR;
1141 * CCP is down; free (de)compressor state if necessary.
1145 struct ppp_softc *sc;
1147 if (sc->sc_xc_state) {
1148 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1149 sc->sc_xc_state = NULL;
1151 if (sc->sc_rc_state) {
1152 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1153 sc->sc_rc_state = NULL;
1156 #endif /* PPP_COMPRESS */
1159 * PPP packet input routine.
1160 * The caller has checked and removed the FCS and has inserted
1161 * the address/control bytes and the protocol high byte if they
1165 ppppktin(sc, m, lost)
1166 struct ppp_softc *sc;
1173 m->m_flags |= M_ERRMARK;
1174 IF_ENQUEUE(&sc->sc_rawq, m);
1175 schednetisr(NETISR_PPP);
1180 * Process a received PPP packet, doing decompression as necessary.
1181 * Should be called at splsoftnet.
1183 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1184 TYPE_UNCOMPRESSED_TCP)
1188 struct ppp_softc *sc;
1191 struct ifnet *ifp = &sc->sc_if;
1192 struct ifqueue *inq;
1193 int s, ilen, xlen, proto, rv;
1194 u_char *cp, adrs, ctrl;
1195 struct mbuf *mp, *dmp = NULL;
1199 sc->sc_stats.ppp_ipackets++;
1201 if (sc->sc_flags & SC_LOG_INPKT) {
1203 for (mp = m; mp != NULL; mp = mp->m_next)
1205 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1209 cp = mtod(m, u_char *);
1210 adrs = PPP_ADDRESS(cp);
1211 ctrl = PPP_CONTROL(cp);
1212 proto = PPP_PROTOCOL(cp);
1214 if (m->m_flags & M_ERRMARK) {
1215 m->m_flags &= ~M_ERRMARK;
1217 sc->sc_flags |= SC_VJ_RESET;
1223 * Decompress this packet if necessary, update the receiver's
1224 * dictionary, or take appropriate action on a CCP packet.
1226 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1227 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1228 /* decompress this packet */
1229 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1230 if (rv == DECOMP_OK) {
1233 /* no error, but no decompressed packet produced */
1237 cp = mtod(m, u_char *);
1238 proto = PPP_PROTOCOL(cp);
1242 * An error has occurred in decompression.
1243 * Pass the compressed packet up to pppd, which may take
1244 * CCP down or issue a Reset-Req.
1246 if (sc->sc_flags & SC_DEBUG)
1247 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1249 sc->sc_flags |= SC_VJ_RESET;
1250 if (rv == DECOMP_ERROR)
1251 sc->sc_flags |= SC_DC_ERROR;
1253 sc->sc_flags |= SC_DC_FERROR;
1258 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1259 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1261 if (proto == PPP_CCP) {
1268 for (mp = m; mp != NULL; mp = mp->m_next)
1272 if (sc->sc_flags & SC_VJ_RESET) {
1274 * If we've missed a packet, we must toss subsequent compressed
1275 * packets which don't have an explicit connection ID.
1278 vj_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1280 sc->sc_flags &= ~SC_VJ_RESET;
1285 * See if we have a VJ-compressed packet to uncompress.
1287 if (proto == PPP_VJC_COMP) {
1288 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1291 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1292 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1293 sc->sc_comp, &iphdr, &hlen);
1296 if (sc->sc_flags & SC_DEBUG)
1297 printf("ppp%d: VJ uncompress failed on type comp\n",
1302 /* Copy the PPP and IP headers into a new mbuf. */
1303 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1308 if (hlen + PPP_HDRLEN > MHLEN) {
1309 MCLGET(mp, M_DONTWAIT);
1310 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1312 goto bad; /* lose if big headers and no clusters */
1315 cp = mtod(mp, u_char *);
1321 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1322 mp->m_len = hlen + PPP_HDRLEN;
1325 * Trim the PPP and VJ headers off the old mbuf
1326 * and stick the new and old mbufs together.
1328 m->m_data += PPP_HDRLEN + xlen;
1329 m->m_len -= PPP_HDRLEN + xlen;
1330 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1331 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1332 mp->m_len += m->m_len;
1333 MFREE(m, mp->m_next);
1337 ilen += hlen - xlen;
1339 } else if (proto == PPP_VJC_UNCOMP) {
1340 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1343 xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1344 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1345 sc->sc_comp, &iphdr, &hlen);
1348 if (sc->sc_flags & SC_DEBUG)
1349 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1360 * If the packet will fit in a header mbuf, don't waste a
1361 * whole cluster on it.
1363 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1364 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1366 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1372 m->m_pkthdr.len = ilen;
1373 m->m_pkthdr.rcvif = ifp;
1375 if ((proto & 0x8000) == 0) {
1378 * See whether we want to pass this packet, and
1379 * if it counts as link activity.
1381 adrs = *mtod(m, u_char *); /* save address field */
1382 *mtod(m, u_char *) = 0; /* indicate inbound */
1383 if (sc->sc_pass_filt.bf_insns != 0
1384 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1386 /* drop this packet */
1390 if (sc->sc_active_filt.bf_insns == 0
1391 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1392 sc->sc_last_recv = time.tv_sec;
1394 *mtod(m, u_char *) = adrs;
1397 * Record the time that we received this packet.
1399 sc->sc_last_recv = time.tv_sec;
1400 #endif /* PPP_FILTER */
1404 /* See if bpf wants to look at the packet. */
1406 bpf_mtap(sc->sc_bpf, m);
1414 * IP packet - take off the ppp header and pass it up to IP.
1416 if ((ifp->if_flags & IFF_UP) == 0
1417 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1418 /* interface is down - drop the packet. */
1422 m->m_pkthdr.len -= PPP_HDRLEN;
1423 m->m_data += PPP_HDRLEN;
1424 m->m_len -= PPP_HDRLEN;
1425 schednetisr(NETISR_IP);
1432 * Some other protocol - place on input queue for read().
1440 * Put the packet on the appropriate input queue.
1443 if (IF_QFULL(inq)) {
1446 if (sc->sc_flags & SC_DEBUG)
1447 printf("ppp%d: input queue full\n", ifp->if_unit);
1454 ifp->if_ibytes += ilen;
1455 ifp->if_lastchange = time;
1464 sc->sc_if.if_ierrors++;
1465 sc->sc_stats.ppp_ierrors++;
1468 #define MAX_DUMP_BYTES 128
1474 char buf[3*MAX_DUMP_BYTES+4];
1477 static char digits[] = "0123456789abcdef";
1479 for (m = m0; m; m = m->m_next) {
1481 u_char *rptr = (u_char *)m->m_data;
1484 if (bp > buf + sizeof(buf) - 4)
1486 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1487 *bp++ = digits[*rptr++ & 0xf];
1491 if (bp > buf + sizeof(buf) - 3)
1501 printf("%s\n", buf);
1504 #endif /* NPPP > 0 */