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).
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 */
87 #include <sys/param.h>
88 #include <sys/systm.h>
91 #include <sys/socket.h>
92 #include <sys/filio.h>
93 #include <sys/sockio.h>
94 #include <sys/kernel.h>
96 #include <sys/malloc.h>
97 #include <sys/module.h>
100 #include <net/if_types.h>
101 #include <net/netisr.h>
105 #include <netinet/in.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip.h>
112 #include <netipx/ipx.h>
113 #include <netipx/ipx_if.h>
117 #include <net/slcompress.h>
120 #include <net/if_ppp.h>
121 #include <net/if_pppvar.h>
125 #define splsoftnet splnet
129 #define PACKETPTR struct mbuf *
130 #include <net/ppp_comp.h>
133 static struct ppp_softc ppp_softc[NPPP];
135 /* XXX layering violation */
136 extern void pppasyncattach __P((void *));
138 static int pppsioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
139 static void pppintr __P((void));
141 static void ppp_requeue __P((struct ppp_softc *));
142 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
143 static void ppp_ccp_closed __P((struct ppp_softc *));
144 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
145 static void pppdumpm __P((struct mbuf *m0));
148 * Some useful mbuf macros not in mbuf.h.
150 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
152 #define M_DATASTART(m) \
153 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
154 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
156 #define M_DATASIZE(m) \
157 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
158 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
161 * We steal two bits in the mbuf m_flags, to mark high-priority packets
162 * for output, and received packets following lost/corrupted packets.
164 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
165 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
170 * List of compressors we know about.
171 * We leave some space so maybe we can modload compressors.
174 extern struct compressor ppp_bsd_compress;
175 extern struct compressor ppp_deflate, ppp_deflate_draft;
177 static struct compressor *ppp_compressors[8] = {
178 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
181 #if DO_DEFLATE && defined(PPP_DEFLATE)
187 #endif /* PPP_COMPRESS */
190 * Called from boot code to establish ppp interfaces.
195 register struct ppp_softc *sc;
198 for (sc = ppp_softc; i < NPPP; sc++) {
199 sc->sc_if.if_name = "ppp";
200 sc->sc_if.if_unit = i++;
201 sc->sc_if.if_mtu = PPP_MTU;
202 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
203 sc->sc_if.if_type = IFT_PPP;
204 sc->sc_if.if_hdrlen = PPP_HDRLEN;
205 sc->sc_if.if_ioctl = pppsioctl;
206 sc->sc_if.if_output = pppoutput;
207 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
208 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
209 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
210 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
211 mtx_init(&sc->sc_inq.ifq_mtx, "ppp_inq", MTX_DEF);
212 mtx_init(&sc->sc_fastq.ifq_mtx, "ppp_fastq", MTX_DEF);
213 mtx_init(&sc->sc_rawq.ifq_mtx, "ppp_rawq", MTX_DEF);
214 if_attach(&sc->sc_if);
215 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
217 register_netisr(NETISR_PPP, pppintr);
219 * XXX layering violation - if_ppp can work over any lower level
220 * transport that cares to attach to it.
222 pppasyncattach(NULL);
226 ppp_modevent(module_t mod, int type, void *data)
233 printf("if_ppp module unload - not possible for this module type\n");
239 static moduledata_t ppp_mod = {
245 DECLARE_MODULE(if_ppp, ppp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
248 * Allocate a ppp interface unit and initialize it.
255 struct ppp_softc *sc;
257 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
258 if (sc->sc_xfer == pid) {
262 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
263 if (sc->sc_devp == NULL)
269 sc->sc_mru = PPP_MRU;
270 sc->sc_relinq = NULL;
271 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
273 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
276 sl_compress_init(sc->sc_comp, -1);
279 sc->sc_xc_state = NULL;
280 sc->sc_rc_state = NULL;
281 #endif /* PPP_COMPRESS */
282 for (i = 0; i < NUM_NP; ++i)
283 sc->sc_npmode[i] = NPMODE_ERROR;
284 sc->sc_npqueue = NULL;
285 sc->sc_npqtail = &sc->sc_npqueue;
286 sc->sc_last_sent = sc->sc_last_recv = time_second;
292 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
296 struct ppp_softc *sc;
301 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
302 getmicrotime(&sc->sc_if.if_lastchange);
305 IF_DRAIN(&sc->sc_rawq);
306 IF_DRAIN(&sc->sc_inq);
307 IF_DRAIN(&sc->sc_fastq);
308 while ((m = sc->sc_npqueue) != NULL) {
309 sc->sc_npqueue = m->m_nextpkt;
314 sc->sc_xc_state = NULL;
315 sc->sc_rc_state = NULL;
316 #endif /* PPP_COMPRESS */
318 if (sc->sc_pass_filt.bf_insns != 0) {
319 FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
320 sc->sc_pass_filt.bf_insns = 0;
321 sc->sc_pass_filt.bf_len = 0;
323 if (sc->sc_active_filt.bf_insns != 0) {
324 FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
325 sc->sc_active_filt.bf_insns = 0;
326 sc->sc_active_filt.bf_len = 0;
328 #endif /* PPP_FILTER */
330 if (sc->sc_comp != 0) {
331 FREE(sc->sc_comp, M_DEVBUF);
338 * Ioctl routine for generic ppp devices.
341 pppioctl(sc, cmd, data, flag, td)
342 struct ppp_softc *sc;
348 struct proc *p = td->td_proc;
349 int s, flags, mru, npx;
352 struct ppp_option_data *odp;
353 struct compressor **cp;
357 struct bpf_program *bp, *nbp;
358 struct bpf_insn *newcode, *oldcode;
360 #endif /* PPP_FILTER */
362 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
367 *(int *)data = sc->sc_inq.ifq_len;
371 *(int *)data = sc->sc_if.if_unit;
375 *(u_int *)data = sc->sc_flags;
379 if ((error = suser(p)) != 0)
381 flags = *(int *)data & SC_MASK;
384 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
388 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
393 if ((error = suser(p)) != 0)
396 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
401 *(int *)data = sc->sc_mru;
406 if ((error = suser(p)) != 0)
410 sl_compress_init(sc->sc_comp, *(int *)data);
417 if ((error = suser(p)) != 0)
419 sc->sc_xfer = p->p_pid;
423 case PPPIOCSCOMPRESS:
424 if ((error = suser(p)) != 0)
426 odp = (struct ppp_option_data *) data;
428 if (nb > sizeof(ccp_option))
429 nb = sizeof(ccp_option);
430 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
432 if (ccp_option[1] < 2) { /* preliminary check on the length byte */
436 for (cp = ppp_compressors; *cp != NULL; ++cp)
437 if ((*cp)->compress_proto == ccp_option[0]) {
439 * Found a handler for the protocol - try to allocate
440 * a compressor or decompressor.
445 if (sc->sc_xc_state != NULL)
446 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
448 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
449 if (sc->sc_xc_state == NULL) {
450 if (sc->sc_flags & SC_DEBUG)
451 printf("ppp%d: comp_alloc failed\n",
456 sc->sc_flags &= ~SC_COMP_RUN;
460 if (sc->sc_rc_state != NULL)
461 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
463 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
464 if (sc->sc_rc_state == NULL) {
465 if (sc->sc_flags & SC_DEBUG)
466 printf("ppp%d: decomp_alloc failed\n",
471 sc->sc_flags &= ~SC_DECOMP_RUN;
476 if (sc->sc_flags & SC_DEBUG)
477 printf("ppp%d: no compressor for [%x %x %x], %x\n",
478 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
480 error = EINVAL; /* no handler found */
482 #endif /* PPP_COMPRESS */
486 npi = (struct npioctl *) data;
487 npx = 0; /* XXX: quiet gcc */
488 switch (npi->protocol) {
497 if (cmd == PPPIOCGNPMODE) {
498 npi->mode = sc->sc_npmode[npx];
500 if ((error = suser(p)) != 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) {
530 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
531 if (newcodelen != 0) {
532 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
534 error = EINVAL; /* or sumpin */
537 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
539 FREE(newcode, M_DEVBUF);
542 if (!bpf_validate(newcode, nbp->bf_len)) {
543 FREE(newcode, M_DEVBUF);
549 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
550 oldcode = bp->bf_insns;
552 bp->bf_len = nbp->bf_len;
553 bp->bf_insns = newcode;
556 FREE(oldcode, M_DEVBUF);
568 * Process an ioctl request to the ppp network interface.
571 pppsioctl(ifp, cmd, data)
572 register struct ifnet *ifp;
576 struct proc *p = curproc; /* XXX */
577 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
578 register struct ifaddr *ifa = (struct ifaddr *)data;
579 register struct ifreq *ifr = (struct ifreq *)data;
580 struct ppp_stats *psp;
582 struct ppp_comp_stats *pcp;
584 int s = splimp(), error = 0;
588 if ((ifp->if_flags & IFF_RUNNING) == 0)
589 ifp->if_flags &= ~IFF_UP;
594 switch(ifa->ifa_addr->sa_family) {
604 error = EAFNOSUPPORT;
610 switch(ifa->ifa_addr->sa_family) {
620 error = EAFNOSUPPORT;
626 if ((error = suser(p)) != 0)
628 if (ifr->ifr_mtu > PPP_MAXMTU)
631 sc->sc_if.if_mtu = ifr->ifr_mtu;
633 (*sc->sc_setmtu)(sc);
638 ifr->ifr_mtu = sc->sc_if.if_mtu;
644 error = EAFNOSUPPORT;
647 switch(ifr->ifr_addr.sa_family) {
653 error = EAFNOSUPPORT;
659 psp = &((struct ifpppstatsreq *) data)->stats;
660 bzero(psp, sizeof(*psp));
661 psp->p = sc->sc_stats;
662 #if defined(VJC) && !defined(SL_NO_STATS)
664 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
665 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
666 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
667 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
668 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
669 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
670 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
671 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
678 pcp = &((struct ifpppcstatsreq *) data)->stats;
679 bzero(pcp, sizeof(*pcp));
680 if (sc->sc_xc_state != NULL)
681 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
682 if (sc->sc_rc_state != NULL)
683 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
685 #endif /* PPP_COMPRESS */
695 * Queue a packet. Start transmission if not active.
696 * Packet is placed in Information field of PPP frame.
697 * Called at splnet as the if->if_output handler.
698 * Called at splnet from pppwrite().
701 pppoutput(ifp, m0, dst, rtp)
704 struct sockaddr *dst;
707 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
708 int protocol, address, control;
717 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
718 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
719 error = ENETDOWN; /* sort of */
724 * Compute PPP header.
726 m0->m_flags &= ~M_HIGHPRI;
727 switch (dst->sa_family) {
730 address = PPP_ALLSTATIONS;
733 mode = sc->sc_npmode[NP_IP];
736 * If this packet has the "low delay" bit set in the IP header,
737 * put it on the fastq instead.
739 ip = mtod(m0, struct ip *);
740 if (ip->ip_tos & IPTOS_LOWDELAY)
741 m0->m_flags |= M_HIGHPRI;
747 * This is pretty bogus.. We dont have an ipxcp module in pppd
748 * yet to configure the link parameters. Sigh. I guess a
749 * manual ifconfig would do.... -Peter
751 address = PPP_ALLSTATIONS;
758 address = PPP_ADDRESS(dst->sa_data);
759 control = PPP_CONTROL(dst->sa_data);
760 protocol = PPP_PROTOCOL(dst->sa_data);
764 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
765 error = EAFNOSUPPORT;
770 * Drop this packet, or return an error, if necessary.
772 if (mode == NPMODE_ERROR) {
776 if (mode == NPMODE_DROP) {
782 * Add PPP header. If no space in first mbuf, allocate another.
783 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
785 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
786 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
793 m0->m_data -= PPP_HDRLEN;
795 cp = mtod(m0, u_char *);
798 *cp++ = protocol >> 8;
799 *cp++ = protocol & 0xff;
800 m0->m_len += PPP_HDRLEN;
803 for (m = m0; m != 0; m = m->m_next)
806 if (sc->sc_flags & SC_LOG_OUTPKT) {
807 printf("ppp%d output: ", ifp->if_unit);
811 if ((protocol & 0x8000) == 0) {
814 * Apply the pass and active filters to the packet,
815 * but only if it is a data packet.
817 *mtod(m0, u_char *) = 1; /* indicates outbound */
818 if (sc->sc_pass_filt.bf_insns != 0
819 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
821 error = 0; /* drop this packet */
826 * Update the time we sent the most recent packet.
828 if (sc->sc_active_filt.bf_insns == 0
829 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
830 sc->sc_last_sent = time_second;
832 *mtod(m0, u_char *) = address;
835 * Update the time we sent the most recent data packet.
837 sc->sc_last_sent = time_second;
838 #endif /* PPP_FILTER */
842 * See if bpf wants to look at the packet.
848 * Put the packet on the appropriate queue.
850 s = splsoftnet(); /* redundant */
851 if (mode == NPMODE_QUEUE) {
852 /* XXX we should limit the number of packets on this queue */
853 *sc->sc_npqtail = m0;
854 m0->m_nextpkt = NULL;
855 sc->sc_npqtail = &m0->m_nextpkt;
857 /* fastq and if_snd are emptied at spl[soft]net now */
858 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
860 if (_IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
863 sc->sc_if.if_oerrors++;
864 sc->sc_stats.ppp_oerrors++;
868 _IF_ENQUEUE(ifq, m0);
872 getmicrotime(&ifp->if_lastchange);
874 ifp->if_obytes += len;
885 * After a change in the NPmode for some NP, move packets from the
886 * npqueue to the send queue or the fast queue as appropriate.
887 * Should be called at spl[soft]net.
891 struct ppp_softc *sc;
893 struct mbuf *m, **mpp;
897 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
898 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
900 mode = sc->sc_npmode[NP_IP];
909 * This packet can now go on one of the queues to be sent.
913 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
914 if (! IF_HANDOFF(ifq, m, NULL)) {
915 sc->sc_if.if_oerrors++;
916 sc->sc_stats.ppp_oerrors++;
931 sc->sc_npqtail = mpp;
935 * Transmitter has finished outputting some stuff;
936 * remember to call sc->sc_start later at splsoftnet.
940 struct ppp_softc *sc;
944 sc->sc_flags &= ~SC_TBUSY;
945 schednetisr(NETISR_PPP);
951 * Get a packet to send. This procedure is intended to be called at
952 * splsoftnet, since it may involve time-consuming operations such as
953 * applying VJ compression, packet compression, address/control and/or
954 * protocol field compression to the packet.
958 struct ppp_softc *sc;
962 int address, control, protocol;
965 * Grab a packet to send: first try the fast queue, then the
968 IF_DEQUEUE(&sc->sc_fastq, m);
970 IF_DEQUEUE(&sc->sc_if.if_snd, m);
974 ++sc->sc_stats.ppp_opackets;
977 * Extract the ppp header of the new packet.
978 * The ppp header will be in one mbuf.
980 cp = mtod(m, u_char *);
981 address = PPP_ADDRESS(cp);
982 control = PPP_CONTROL(cp);
983 protocol = PPP_PROTOCOL(cp);
989 * If the packet is a TCP/IP packet, see if we can compress it.
991 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
996 ip = (struct ip *) (cp + PPP_HDRLEN);
997 if (mp->m_len <= PPP_HDRLEN) {
1001 ip = mtod(mp, struct ip *);
1003 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1004 if (ip->ip_p == IPPROTO_TCP) {
1005 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1006 !(sc->sc_flags & SC_NO_TCP_CCID));
1008 case TYPE_UNCOMPRESSED_TCP:
1009 protocol = PPP_VJC_UNCOMP;
1011 case TYPE_COMPRESSED_TCP:
1012 protocol = PPP_VJC_COMP;
1013 cp = mtod(m, u_char *);
1014 cp[0] = address; /* header has moved */
1019 cp[3] = protocol; /* update protocol in PPP header */
1029 #endif /* PPP_COMPRESS */
1033 if (protocol != PPP_LCP && protocol != PPP_CCP
1034 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1035 struct mbuf *mcomp = NULL;
1039 for (mp = m; mp != NULL; mp = mp->m_next)
1041 clen = (*sc->sc_xcomp->compress)
1042 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1043 if (mcomp != NULL) {
1044 if (sc->sc_flags & SC_CCP_UP) {
1045 /* Send the compressed packet instead of the original. */
1048 cp = mtod(m, u_char *);
1051 /* Can't transmit compressed packets until CCP is up. */
1056 #endif /* PPP_COMPRESS */
1059 * Compress the address/control and protocol, if possible.
1061 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1062 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1063 protocol != PPP_LCP) {
1064 /* can compress address/control */
1068 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1069 /* can compress protocol */
1070 if (mtod(m, u_char *) == cp) {
1071 cp[2] = cp[1]; /* move address/control up */
1082 * Software interrupt routine, called at spl[soft]net.
1087 struct ppp_softc *sc;
1092 for (i = 0; i < NPPP; ++i, ++sc) {
1094 if (!(sc->sc_flags & SC_TBUSY)
1095 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1096 sc->sc_flags |= SC_TBUSY;
1098 (*sc->sc_start)(sc);
1103 IF_DEQUEUE(&sc->sc_rawq, m);
1114 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1115 * 0 if it is about to be transmitted.
1118 ppp_ccp(sc, m, rcvd)
1119 struct ppp_softc *sc;
1128 * Get a pointer to the data after the PPP header.
1130 if (m->m_len <= PPP_HDRLEN) {
1134 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1137 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1140 ep = mtod(mp, u_char *) + mp->m_len;
1141 if (dp + CCP_HDRLEN > ep)
1143 slen = CCP_LENGTH(dp);
1144 if (dp + slen > ep) {
1145 if (sc->sc_flags & SC_DEBUG)
1146 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1147 dp, slen, mtod(mp, u_char *), mp->m_len);
1151 switch (CCP_CODE(dp)) {
1155 /* CCP must be going down - disable compression */
1156 if (sc->sc_flags & SC_CCP_UP) {
1158 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1164 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1165 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1166 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1168 /* we're agreeing to send compressed packets. */
1169 if (sc->sc_xc_state != NULL
1170 && (*sc->sc_xcomp->comp_init)
1171 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1172 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1174 sc->sc_flags |= SC_COMP_RUN;
1178 /* peer is agreeing to send compressed packets. */
1179 if (sc->sc_rc_state != NULL
1180 && (*sc->sc_rcomp->decomp_init)
1181 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1182 sc->sc_if.if_unit, 0, sc->sc_mru,
1183 sc->sc_flags & SC_DEBUG)) {
1185 sc->sc_flags |= SC_DECOMP_RUN;
1186 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1194 if (sc->sc_flags & SC_CCP_UP) {
1196 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1197 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1199 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1200 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1202 sc->sc_flags &= ~SC_DC_ERROR;
1212 * CCP is down; free (de)compressor state if necessary.
1216 struct ppp_softc *sc;
1218 if (sc->sc_xc_state) {
1219 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1220 sc->sc_xc_state = NULL;
1222 if (sc->sc_rc_state) {
1223 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1224 sc->sc_rc_state = NULL;
1227 #endif /* PPP_COMPRESS */
1230 * PPP packet input routine.
1231 * The caller has checked and removed the FCS and has inserted
1232 * the address/control bytes and the protocol high byte if they
1236 ppppktin(sc, m, lost)
1237 struct ppp_softc *sc;
1244 m->m_flags |= M_ERRMARK;
1245 IF_ENQUEUE(&sc->sc_rawq, m);
1246 schednetisr(NETISR_PPP);
1251 * Process a received PPP packet, doing decompression as necessary.
1252 * Should be called at splsoftnet.
1254 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1255 TYPE_UNCOMPRESSED_TCP)
1259 struct ppp_softc *sc;
1262 struct ifnet *ifp = &sc->sc_if;
1263 struct ifqueue *inq;
1264 int s, ilen = 0, xlen, proto, rv;
1265 u_char *cp, adrs, ctrl;
1266 struct mbuf *mp, *dmp = NULL;
1270 sc->sc_stats.ppp_ipackets++;
1272 if (sc->sc_flags & SC_LOG_INPKT) {
1274 for (mp = m; mp != NULL; mp = mp->m_next)
1276 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1280 cp = mtod(m, u_char *);
1281 adrs = PPP_ADDRESS(cp);
1282 ctrl = PPP_CONTROL(cp);
1283 proto = PPP_PROTOCOL(cp);
1285 if (m->m_flags & M_ERRMARK) {
1286 m->m_flags &= ~M_ERRMARK;
1288 sc->sc_flags |= SC_VJ_RESET;
1294 * Decompress this packet if necessary, update the receiver's
1295 * dictionary, or take appropriate action on a CCP packet.
1297 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1298 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1299 /* decompress this packet */
1300 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1301 if (rv == DECOMP_OK) {
1304 /* no error, but no decompressed packet produced */
1308 cp = mtod(m, u_char *);
1309 proto = PPP_PROTOCOL(cp);
1313 * An error has occurred in decompression.
1314 * Pass the compressed packet up to pppd, which may take
1315 * CCP down or issue a Reset-Req.
1317 if (sc->sc_flags & SC_DEBUG)
1318 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1320 sc->sc_flags |= SC_VJ_RESET;
1321 if (rv == DECOMP_ERROR)
1322 sc->sc_flags |= SC_DC_ERROR;
1324 sc->sc_flags |= SC_DC_FERROR;
1329 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1330 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1332 if (proto == PPP_CCP) {
1339 for (mp = m; mp != NULL; mp = mp->m_next)
1343 if (sc->sc_flags & SC_VJ_RESET) {
1345 * If we've missed a packet, we must toss subsequent compressed
1346 * packets which don't have an explicit connection ID.
1349 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1351 sc->sc_flags &= ~SC_VJ_RESET;
1356 * See if we have a VJ-compressed packet to uncompress.
1358 if (proto == PPP_VJC_COMP) {
1359 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1362 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1363 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1364 sc->sc_comp, &iphdr, &hlen);
1367 if (sc->sc_flags & SC_DEBUG)
1368 printf("ppp%d: VJ uncompress failed on type comp\n",
1373 /* Copy the PPP and IP headers into a new mbuf. */
1374 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1379 if (hlen + PPP_HDRLEN > MHLEN) {
1380 MCLGET(mp, M_DONTWAIT);
1381 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1383 goto bad; /* lose if big headers and no clusters */
1386 cp = mtod(mp, u_char *);
1392 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1393 mp->m_len = hlen + PPP_HDRLEN;
1396 * Trim the PPP and VJ headers off the old mbuf
1397 * and stick the new and old mbufs together.
1399 m->m_data += PPP_HDRLEN + xlen;
1400 m->m_len -= PPP_HDRLEN + xlen;
1401 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1402 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1403 mp->m_len += m->m_len;
1404 mp->m_next = m_free(m);
1409 ilen += hlen - xlen;
1411 } else if (proto == PPP_VJC_UNCOMP) {
1412 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1415 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1416 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1417 sc->sc_comp, &iphdr, &hlen);
1420 if (sc->sc_flags & SC_DEBUG)
1421 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1432 * If the packet will fit in a header mbuf, don't waste a
1433 * whole cluster on it.
1435 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1436 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1438 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1444 m->m_pkthdr.len = ilen;
1445 m->m_pkthdr.rcvif = ifp;
1447 if ((proto & 0x8000) == 0) {
1450 * See whether we want to pass this packet, and
1451 * if it counts as link activity.
1453 adrs = *mtod(m, u_char *); /* save address field */
1454 *mtod(m, u_char *) = 0; /* indicate inbound */
1455 if (sc->sc_pass_filt.bf_insns != 0
1456 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1458 /* drop this packet */
1462 if (sc->sc_active_filt.bf_insns == 0
1463 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1464 sc->sc_last_recv = time_second;
1466 *mtod(m, u_char *) = adrs;
1469 * Record the time that we received this packet.
1471 sc->sc_last_recv = time_second;
1472 #endif /* PPP_FILTER */
1475 /* See if bpf wants to look at the packet. */
1476 if (sc->sc_if.if_bpf)
1477 bpf_mtap(&sc->sc_if, m);
1484 * IP packet - take off the ppp header and pass it up to IP.
1486 if ((ifp->if_flags & IFF_UP) == 0
1487 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1488 /* interface is down - drop the packet. */
1492 m->m_pkthdr.len -= PPP_HDRLEN;
1493 m->m_data += PPP_HDRLEN;
1494 m->m_len -= PPP_HDRLEN;
1495 if (ipflow_fastforward(m))
1497 schednetisr(NETISR_IP);
1504 * IPX packet - take off the ppp header and pass it up to IPX.
1506 if ((sc->sc_if.if_flags & IFF_UP) == 0
1507 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1508 /* interface is down - drop the packet. */
1512 m->m_pkthdr.len -= PPP_HDRLEN;
1513 m->m_data += PPP_HDRLEN;
1514 m->m_len -= PPP_HDRLEN;
1515 schednetisr(NETISR_IPX);
1517 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1523 * Some other protocol - place on input queue for read().
1531 * Put the packet on the appropriate input queue.
1533 if (! IF_HANDOFF(inq, m, NULL)) {
1534 if (sc->sc_flags & SC_DEBUG)
1535 printf("ppp%d: input queue full\n", ifp->if_unit);
1540 ifp->if_ibytes += ilen;
1541 getmicrotime(&ifp->if_lastchange);
1551 sc->sc_if.if_ierrors++;
1552 sc->sc_stats.ppp_ierrors++;
1555 #define MAX_DUMP_BYTES 128
1561 char buf[3*MAX_DUMP_BYTES+4];
1565 for (m = m0; m; m = m->m_next) {
1567 u_char *rptr = (u_char *)m->m_data;
1570 if (bp > buf + sizeof(buf) - 4)
1572 *bp++ = hex2ascii(*rptr >> 4);
1573 *bp++ = hex2ascii(*rptr++ & 0xf);
1577 if (bp > buf + sizeof(buf) - 3)
1587 printf("%s\n", buf);