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 /* $FreeBSD: src/sys/net/if_ppp.c,v 1.67.2.4 2002/04/14 21:41:48 luigi Exp $ */
73 /* $DragonFly: src/sys/net/ppp/if_ppp.c,v 1.11 2004/01/06 03:17:26 dillon Exp $ */
74 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
75 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
88 #include <sys/param.h>
89 #include <sys/systm.h>
92 #include <sys/socket.h>
93 #include <sys/filio.h>
94 #include <sys/sockio.h>
95 #include <sys/kernel.h>
97 #include <sys/malloc.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 <netproto/ipx/ipx.h>
113 #include <netproto/ipx/ipx_if.h>
117 #include <net/slcompress.h>
121 #include "if_pppvar.h"
125 #define splsoftnet splnet
129 #define PACKETPTR struct mbuf *
130 #include <net/ppp_layer/ppp_comp.h>
133 struct ppp_softc ppp_softc[NPPP];
135 /* XXX layering violation */
136 extern void pppasyncattach (void *);
138 static void pppattach (void *);
139 PSEUDO_SET(pppattach, if_ppp);
141 static int pppsioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
142 static void pppintr (struct mbuf *m);
144 static void ppp_requeue (struct ppp_softc *);
145 static void ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
146 static void ppp_ccp_closed (struct ppp_softc *);
147 static void ppp_inproc (struct ppp_softc *, struct mbuf *);
148 static void pppdumpm (struct mbuf *m0);
151 * Some useful mbuf macros not in mbuf.h.
153 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
155 #define M_DATASTART(m) \
156 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
157 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
159 #define M_DATASIZE(m) \
160 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
161 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
164 * We steal two bits in the mbuf m_flags, to mark high-priority packets
165 * for output, and received packets following lost/corrupted packets.
167 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
168 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
173 * List of compressors we know about.
174 * We leave some space so maybe we can modload compressors.
177 extern struct compressor ppp_bsd_compress;
178 extern struct compressor ppp_deflate, ppp_deflate_draft;
180 static struct compressor *ppp_compressors[8] = {
181 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
184 #if DO_DEFLATE && defined(PPP_DEFLATE)
190 #endif /* PPP_COMPRESS */
193 * Called from boot code to establish ppp interfaces.
199 struct ppp_softc *sc;
202 for (sc = ppp_softc; i < NPPP; sc++) {
203 if_initname(&(sc->sc_if), "ppp", i++);
204 sc->sc_if.if_mtu = PPP_MTU;
205 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
206 sc->sc_if.if_type = IFT_PPP;
207 sc->sc_if.if_hdrlen = PPP_HDRLEN;
208 sc->sc_if.if_ioctl = pppsioctl;
209 sc->sc_if.if_output = pppoutput;
210 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
211 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
212 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
213 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
214 if_attach(&sc->sc_if);
215 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
217 netisr_register(NETISR_PPP, cpu0_portfn, pppintr);
219 * XXX layering violation - if_ppp can work over any lower level
220 * transport that cares to attach to it.
222 pppasyncattach(dummy);
226 * Allocate a ppp interface unit and initialize it.
229 pppalloc(struct thread *td)
232 struct ppp_softc *sc;
234 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
235 if (sc->sc_xfer == td) {
239 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
240 if (sc->sc_devp == NULL)
246 sc->sc_mru = PPP_MRU;
247 sc->sc_relinq = NULL;
248 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
250 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
253 sl_compress_init(sc->sc_comp, -1);
256 sc->sc_xc_state = NULL;
257 sc->sc_rc_state = NULL;
258 #endif /* PPP_COMPRESS */
259 for (i = 0; i < NUM_NP; ++i)
260 sc->sc_npmode[i] = NPMODE_ERROR;
261 sc->sc_npqueue = NULL;
262 sc->sc_npqtail = &sc->sc_npqueue;
263 sc->sc_last_sent = sc->sc_last_recv = time_second;
269 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
273 struct ppp_softc *sc;
278 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
279 getmicrotime(&sc->sc_if.if_lastchange);
283 IF_DEQUEUE(&sc->sc_rawq, m);
289 IF_DEQUEUE(&sc->sc_inq, m);
295 IF_DEQUEUE(&sc->sc_fastq, m);
300 while ((m = sc->sc_npqueue) != NULL) {
301 sc->sc_npqueue = m->m_nextpkt;
306 sc->sc_xc_state = NULL;
307 sc->sc_rc_state = NULL;
308 #endif /* PPP_COMPRESS */
310 if (sc->sc_pass_filt.bf_insns != 0) {
311 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
312 sc->sc_pass_filt.bf_insns = 0;
313 sc->sc_pass_filt.bf_len = 0;
315 if (sc->sc_active_filt.bf_insns != 0) {
316 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
317 sc->sc_active_filt.bf_insns = 0;
318 sc->sc_active_filt.bf_len = 0;
320 #endif /* PPP_FILTER */
322 if (sc->sc_comp != 0) {
323 free(sc->sc_comp, M_DEVBUF);
330 * Ioctl routine for generic ppp devices.
333 pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
334 int flag, struct thread *td)
336 int s, error, flags, mru, npx;
338 struct ppp_option_data *odp;
339 struct compressor **cp;
343 struct bpf_program *bp, *nbp;
344 struct bpf_insn *newcode, *oldcode;
346 #endif /* PPP_FILTER */
348 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
353 *(int *)data = sc->sc_inq.ifq_len;
357 *(int *)data = sc->sc_if.if_dunit;
361 *(u_int *)data = sc->sc_flags;
365 if ((error = suser(td)) != 0)
367 flags = *(int *)data & SC_MASK;
370 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
374 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
379 if ((error = suser(td)) != 0)
382 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
387 *(int *)data = sc->sc_mru;
392 if ((error = suser(td)) != 0)
396 sl_compress_init(sc->sc_comp, *(int *)data);
403 if ((error = suser(td)) != 0)
409 case PPPIOCSCOMPRESS:
410 if ((error = suser(td)) != 0)
412 odp = (struct ppp_option_data *) data;
414 if (nb > sizeof(ccp_option))
415 nb = sizeof(ccp_option);
416 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
418 if (ccp_option[1] < 2) /* preliminary check on the length byte */
420 for (cp = ppp_compressors; *cp != NULL; ++cp)
421 if ((*cp)->compress_proto == ccp_option[0]) {
423 * Found a handler for the protocol - try to allocate
424 * a compressor or decompressor.
429 if (sc->sc_xc_state != NULL)
430 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
432 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
433 if (sc->sc_xc_state == NULL) {
434 if (sc->sc_flags & SC_DEBUG)
435 printf("%s: comp_alloc failed\n",
440 sc->sc_flags &= ~SC_COMP_RUN;
444 if (sc->sc_rc_state != NULL)
445 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
447 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
448 if (sc->sc_rc_state == NULL) {
449 if (sc->sc_flags & SC_DEBUG)
450 printf("%s: decomp_alloc failed\n",
455 sc->sc_flags &= ~SC_DECOMP_RUN;
460 if (sc->sc_flags & SC_DEBUG)
461 printf("%s: no compressor for [%x %x %x], %x\n",
462 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
464 return (EINVAL); /* no handler found */
465 #endif /* PPP_COMPRESS */
469 npi = (struct npioctl *) data;
470 switch (npi->protocol) {
477 if (cmd == PPPIOCGNPMODE) {
478 npi->mode = sc->sc_npmode[npx];
480 if ((error = suser(td)) != 0)
482 if (npi->mode != sc->sc_npmode[npx]) {
484 sc->sc_npmode[npx] = npi->mode;
485 if (npi->mode != NPMODE_QUEUE) {
497 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
498 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
505 nbp = (struct bpf_program *) data;
506 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
508 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
509 if (newcodelen != 0) {
510 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
512 return EINVAL; /* or sumpin */
514 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
516 free(newcode, M_DEVBUF);
519 if (!bpf_validate(newcode, nbp->bf_len)) {
520 free(newcode, M_DEVBUF);
525 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
526 oldcode = bp->bf_insns;
528 bp->bf_len = nbp->bf_len;
529 bp->bf_insns = newcode;
532 free(oldcode, M_DEVBUF);
543 * Process an ioctl request to the ppp network interface.
546 pppsioctl(ifp, cmd, data)
551 struct thread *td = curthread; /* XXX */
552 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
553 struct ifaddr *ifa = (struct ifaddr *)data;
554 struct ifreq *ifr = (struct ifreq *)data;
555 struct ppp_stats *psp;
557 struct ppp_comp_stats *pcp;
559 int s = splimp(), error = 0;
563 if ((ifp->if_flags & IFF_RUNNING) == 0)
564 ifp->if_flags &= ~IFF_UP;
569 switch(ifa->ifa_addr->sa_family) {
579 error = EAFNOSUPPORT;
585 switch(ifa->ifa_addr->sa_family) {
595 error = EAFNOSUPPORT;
601 if ((error = suser(td)) != 0)
603 if (ifr->ifr_mtu > PPP_MAXMTU)
606 sc->sc_if.if_mtu = ifr->ifr_mtu;
608 (*sc->sc_setmtu)(sc);
613 ifr->ifr_mtu = sc->sc_if.if_mtu;
619 error = EAFNOSUPPORT;
622 switch(ifr->ifr_addr.sa_family) {
628 error = EAFNOSUPPORT;
634 psp = &((struct ifpppstatsreq *) data)->stats;
635 bzero(psp, sizeof(*psp));
636 psp->p = sc->sc_stats;
637 #if defined(VJC) && !defined(SL_NO_STATS)
639 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
640 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
641 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
642 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
643 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
644 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
645 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
646 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
653 pcp = &((struct ifpppcstatsreq *) data)->stats;
654 bzero(pcp, sizeof(*pcp));
655 if (sc->sc_xc_state != NULL)
656 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
657 if (sc->sc_rc_state != NULL)
658 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
660 #endif /* PPP_COMPRESS */
670 * Queue a packet. Start transmission if not active.
671 * Packet is placed in Information field of PPP frame.
672 * Called at splnet as the if->if_output handler.
673 * Called at splnet from pppwrite().
676 pppoutput(ifp, m0, dst, rtp)
679 struct sockaddr *dst;
682 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
683 int protocol, address, control;
692 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
693 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
694 error = ENETDOWN; /* sort of */
699 * Compute PPP header.
701 m0->m_flags &= ~M_HIGHPRI;
702 switch (dst->sa_family) {
705 address = PPP_ALLSTATIONS;
708 mode = sc->sc_npmode[NP_IP];
711 * If this packet has the "low delay" bit set in the IP header,
712 * put it on the fastq instead.
714 ip = mtod(m0, struct ip *);
715 if (ip->ip_tos & IPTOS_LOWDELAY)
716 m0->m_flags |= M_HIGHPRI;
722 * This is pretty bogus.. We dont have an ipxcp module in pppd
723 * yet to configure the link parameters. Sigh. I guess a
724 * manual ifconfig would do.... -Peter
726 address = PPP_ALLSTATIONS;
733 address = PPP_ADDRESS(dst->sa_data);
734 control = PPP_CONTROL(dst->sa_data);
735 protocol = PPP_PROTOCOL(dst->sa_data);
739 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
740 error = EAFNOSUPPORT;
745 * Drop this packet, or return an error, if necessary.
747 if (mode == NPMODE_ERROR) {
751 if (mode == NPMODE_DROP) {
757 * Add PPP header. If no space in first mbuf, allocate another.
758 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
760 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
761 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
768 m0->m_data -= PPP_HDRLEN;
770 cp = mtod(m0, u_char *);
773 *cp++ = protocol >> 8;
774 *cp++ = protocol & 0xff;
775 m0->m_len += PPP_HDRLEN;
778 for (m = m0; m != 0; m = m->m_next)
781 if (sc->sc_flags & SC_LOG_OUTPKT) {
782 printf("%s output: ", ifp->if_xname);
786 if ((protocol & 0x8000) == 0) {
789 * Apply the pass and active filters to the packet,
790 * but only if it is a data packet.
792 *mtod(m0, u_char *) = 1; /* indicates outbound */
793 if (sc->sc_pass_filt.bf_insns != 0
794 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
796 error = 0; /* drop this packet */
801 * Update the time we sent the most recent packet.
803 if (sc->sc_active_filt.bf_insns == 0
804 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
805 sc->sc_last_sent = time_second;
807 *mtod(m0, u_char *) = address;
810 * Update the time we sent the most recent data packet.
812 sc->sc_last_sent = time_second;
813 #endif /* PPP_FILTER */
817 * See if bpf wants to look at the packet.
823 * Put the packet on the appropriate queue.
825 s = splsoftnet(); /* redundant */
826 if (mode == NPMODE_QUEUE) {
827 /* XXX we should limit the number of packets on this queue */
828 *sc->sc_npqtail = m0;
829 m0->m_nextpkt = NULL;
830 sc->sc_npqtail = &m0->m_nextpkt;
832 /* fastq and if_snd are emptied at spl[soft]net now */
833 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
834 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
837 sc->sc_if.if_oerrors++;
838 sc->sc_stats.ppp_oerrors++;
845 getmicrotime(&ifp->if_lastchange);
847 ifp->if_obytes += len;
858 * After a change in the NPmode for some NP, move packets from the
859 * npqueue to the send queue or the fast queue as appropriate.
860 * Should be called at spl[soft]net.
864 struct ppp_softc *sc;
866 struct mbuf *m, **mpp;
870 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
871 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
873 mode = sc->sc_npmode[NP_IP];
882 * This packet can now go on one of the queues to be sent.
886 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
889 sc->sc_if.if_oerrors++;
890 sc->sc_stats.ppp_oerrors++;
906 sc->sc_npqtail = mpp;
910 * Transmitter has finished outputting some stuff;
911 * remember to call sc->sc_start later at splsoftnet.
915 struct ppp_softc *sc;
919 sc->sc_flags &= ~SC_TBUSY;
920 schednetisr(NETISR_PPP);
926 * Get a packet to send. This procedure is intended to be called at
927 * splsoftnet, since it may involve time-consuming operations such as
928 * applying VJ compression, packet compression, address/control and/or
929 * protocol field compression to the packet.
933 struct ppp_softc *sc;
937 int address, control, protocol;
940 * Grab a packet to send: first try the fast queue, then the
943 IF_DEQUEUE(&sc->sc_fastq, m);
945 IF_DEQUEUE(&sc->sc_if.if_snd, m);
949 ++sc->sc_stats.ppp_opackets;
952 * Extract the ppp header of the new packet.
953 * The ppp header will be in one mbuf.
955 cp = mtod(m, u_char *);
956 address = PPP_ADDRESS(cp);
957 control = PPP_CONTROL(cp);
958 protocol = PPP_PROTOCOL(cp);
964 * If the packet is a TCP/IP packet, see if we can compress it.
966 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
971 ip = (struct ip *) (cp + PPP_HDRLEN);
972 if (mp->m_len <= PPP_HDRLEN) {
976 ip = mtod(mp, struct ip *);
978 /* this code assumes the IP/TCP header is in one non-shared mbuf */
979 if (ip->ip_p == IPPROTO_TCP) {
980 type = sl_compress_tcp(mp, ip, sc->sc_comp,
981 !(sc->sc_flags & SC_NO_TCP_CCID));
983 case TYPE_UNCOMPRESSED_TCP:
984 protocol = PPP_VJC_UNCOMP;
986 case TYPE_COMPRESSED_TCP:
987 protocol = PPP_VJC_COMP;
988 cp = mtod(m, u_char *);
989 cp[0] = address; /* header has moved */
994 cp[3] = protocol; /* update protocol in PPP header */
1004 #endif /* PPP_COMPRESS */
1008 if (protocol != PPP_LCP && protocol != PPP_CCP
1009 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1010 struct mbuf *mcomp = NULL;
1014 for (mp = m; mp != NULL; mp = mp->m_next)
1016 clen = (*sc->sc_xcomp->compress)
1017 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1018 if (mcomp != NULL) {
1019 if (sc->sc_flags & SC_CCP_UP) {
1020 /* Send the compressed packet instead of the original. */
1023 cp = mtod(m, u_char *);
1026 /* Can't transmit compressed packets until CCP is up. */
1031 #endif /* PPP_COMPRESS */
1034 * Compress the address/control and protocol, if possible.
1036 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1037 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1038 protocol != PPP_LCP) {
1039 /* can compress address/control */
1043 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1044 /* can compress protocol */
1045 if (mtod(m, u_char *) == cp) {
1046 cp[2] = cp[1]; /* move address/control up */
1057 * Software interrupt routine, called at spl[soft]net.
1060 pppintr(struct mbuf *m)
1062 struct ppp_softc *sc;
1066 for (i = 0; i < NPPP; ++i, ++sc) {
1068 if (!(sc->sc_flags & SC_TBUSY)
1069 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1070 sc->sc_flags |= SC_TBUSY;
1072 (*sc->sc_start)(sc);
1077 IF_DEQUEUE(&sc->sc_rawq, m);
1088 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1089 * 0 if it is about to be transmitted.
1092 ppp_ccp(sc, m, rcvd)
1093 struct ppp_softc *sc;
1102 * Get a pointer to the data after the PPP header.
1104 if (m->m_len <= PPP_HDRLEN) {
1108 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1111 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1114 ep = mtod(mp, u_char *) + mp->m_len;
1115 if (dp + CCP_HDRLEN > ep)
1117 slen = CCP_LENGTH(dp);
1118 if (dp + slen > ep) {
1119 if (sc->sc_flags & SC_DEBUG)
1120 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1121 dp, slen, mtod(mp, u_char *), mp->m_len);
1125 switch (CCP_CODE(dp)) {
1129 /* CCP must be going down - disable compression */
1130 if (sc->sc_flags & SC_CCP_UP) {
1132 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1138 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1139 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1140 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1142 /* we're agreeing to send compressed packets. */
1143 if (sc->sc_xc_state != NULL
1144 && (*sc->sc_xcomp->comp_init)
1145 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1146 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1148 sc->sc_flags |= SC_COMP_RUN;
1152 /* peer is agreeing to send compressed packets. */
1153 if (sc->sc_rc_state != NULL
1154 && (*sc->sc_rcomp->decomp_init)
1155 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1156 sc->sc_if.if_dunit, 0, sc->sc_mru,
1157 sc->sc_flags & SC_DEBUG)) {
1159 sc->sc_flags |= SC_DECOMP_RUN;
1160 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1168 if (sc->sc_flags & SC_CCP_UP) {
1170 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1171 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1173 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1174 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1176 sc->sc_flags &= ~SC_DC_ERROR;
1186 * CCP is down; free (de)compressor state if necessary.
1190 struct ppp_softc *sc;
1192 if (sc->sc_xc_state) {
1193 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1194 sc->sc_xc_state = NULL;
1196 if (sc->sc_rc_state) {
1197 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1198 sc->sc_rc_state = NULL;
1201 #endif /* PPP_COMPRESS */
1204 * PPP packet input routine.
1205 * The caller has checked and removed the FCS and has inserted
1206 * the address/control bytes and the protocol high byte if they
1210 ppppktin(sc, m, lost)
1211 struct ppp_softc *sc;
1218 m->m_flags |= M_ERRMARK;
1219 IF_ENQUEUE(&sc->sc_rawq, m);
1220 schednetisr(NETISR_PPP);
1225 * Process a received PPP packet, doing decompression as necessary.
1226 * Should be called at splsoftnet.
1228 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1229 TYPE_UNCOMPRESSED_TCP)
1233 struct ppp_softc *sc;
1236 struct ifnet *ifp = &sc->sc_if;
1238 int s, ilen = 0, xlen, proto, rv;
1239 u_char *cp, adrs, ctrl;
1240 struct mbuf *mp, *dmp = NULL;
1244 sc->sc_stats.ppp_ipackets++;
1246 if (sc->sc_flags & SC_LOG_INPKT) {
1248 for (mp = m; mp != NULL; mp = mp->m_next)
1250 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1254 cp = mtod(m, u_char *);
1255 adrs = PPP_ADDRESS(cp);
1256 ctrl = PPP_CONTROL(cp);
1257 proto = PPP_PROTOCOL(cp);
1259 if (m->m_flags & M_ERRMARK) {
1260 m->m_flags &= ~M_ERRMARK;
1262 sc->sc_flags |= SC_VJ_RESET;
1268 * Decompress this packet if necessary, update the receiver's
1269 * dictionary, or take appropriate action on a CCP packet.
1271 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1272 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1273 /* decompress this packet */
1274 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1275 if (rv == DECOMP_OK) {
1278 /* no error, but no decompressed packet produced */
1282 cp = mtod(m, u_char *);
1283 proto = PPP_PROTOCOL(cp);
1287 * An error has occurred in decompression.
1288 * Pass the compressed packet up to pppd, which may take
1289 * CCP down or issue a Reset-Req.
1291 if (sc->sc_flags & SC_DEBUG)
1292 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1294 sc->sc_flags |= SC_VJ_RESET;
1295 if (rv == DECOMP_ERROR)
1296 sc->sc_flags |= SC_DC_ERROR;
1298 sc->sc_flags |= SC_DC_FERROR;
1303 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1304 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1306 if (proto == PPP_CCP) {
1313 for (mp = m; mp != NULL; mp = mp->m_next)
1317 if (sc->sc_flags & SC_VJ_RESET) {
1319 * If we've missed a packet, we must toss subsequent compressed
1320 * packets which don't have an explicit connection ID.
1323 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1325 sc->sc_flags &= ~SC_VJ_RESET;
1330 * See if we have a VJ-compressed packet to uncompress.
1332 if (proto == PPP_VJC_COMP) {
1333 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1336 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1337 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1338 sc->sc_comp, &iphdr, &hlen);
1341 if (sc->sc_flags & SC_DEBUG)
1342 printf("%s: VJ uncompress failed on type comp\n",
1347 /* Copy the PPP and IP headers into a new mbuf. */
1348 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1353 if (hlen + PPP_HDRLEN > MHLEN) {
1354 MCLGET(mp, M_DONTWAIT);
1355 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1357 goto bad; /* lose if big headers and no clusters */
1360 cp = mtod(mp, u_char *);
1366 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1367 mp->m_len = hlen + PPP_HDRLEN;
1370 * Trim the PPP and VJ headers off the old mbuf
1371 * and stick the new and old mbufs together.
1373 m->m_data += PPP_HDRLEN + xlen;
1374 m->m_len -= PPP_HDRLEN + xlen;
1375 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1376 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1377 mp->m_len += m->m_len;
1378 mp->m_next = m_free(m);
1383 ilen += hlen - xlen;
1385 } else if (proto == PPP_VJC_UNCOMP) {
1386 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1389 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1390 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1391 sc->sc_comp, &iphdr, &hlen);
1394 if (sc->sc_flags & SC_DEBUG)
1395 printf("%s: VJ uncompress failed on type uncomp\n",
1406 * If the packet will fit in a header mbuf, don't waste a
1407 * whole cluster on it.
1409 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1410 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1412 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1418 m->m_pkthdr.len = ilen;
1419 m->m_pkthdr.rcvif = ifp;
1421 if ((proto & 0x8000) == 0) {
1424 * See whether we want to pass this packet, and
1425 * if it counts as link activity.
1427 adrs = *mtod(m, u_char *); /* save address field */
1428 *mtod(m, u_char *) = 0; /* indicate inbound */
1429 if (sc->sc_pass_filt.bf_insns != 0
1430 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1432 /* drop this packet */
1436 if (sc->sc_active_filt.bf_insns == 0
1437 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1438 sc->sc_last_recv = time_second;
1440 *mtod(m, u_char *) = adrs;
1443 * Record the time that we received this packet.
1445 sc->sc_last_recv = time_second;
1446 #endif /* PPP_FILTER */
1449 /* See if bpf wants to look at the packet. */
1450 if (sc->sc_if.if_bpf)
1451 bpf_mtap(&sc->sc_if, m);
1458 * IP packet - take off the ppp header and pass it up to IP.
1460 if ((ifp->if_flags & IFF_UP) == 0
1461 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1462 /* interface is down - drop the packet. */
1466 m->m_pkthdr.len -= PPP_HDRLEN;
1467 m->m_data += PPP_HDRLEN;
1468 m->m_len -= PPP_HDRLEN;
1469 if (ipflow_fastforward(m))
1477 * IPX packet - take off the ppp header and pass it up to IPX.
1479 if ((sc->sc_if.if_flags & IFF_UP) == 0
1480 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1481 /* interface is down - drop the packet. */
1485 m->m_pkthdr.len -= PPP_HDRLEN;
1486 m->m_data += PPP_HDRLEN;
1487 m->m_len -= PPP_HDRLEN;
1489 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1495 * Some other protocol - place on input queue for read().
1501 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1503 rv = (netisr_queue(isr, m) == 0);
1505 if (sc->sc_flags & SC_DEBUG)
1506 printf("%s: input queue full\n", ifp->if_xname);
1512 ifp->if_ibytes += ilen;
1513 getmicrotime(&ifp->if_lastchange);
1522 sc->sc_if.if_ierrors++;
1523 sc->sc_stats.ppp_ierrors++;
1526 #define MAX_DUMP_BYTES 128
1532 char buf[3*MAX_DUMP_BYTES+4];
1536 for (m = m0; m; m = m->m_next) {
1538 u_char *rptr = (u_char *)m->m_data;
1541 if (bp > buf + sizeof(buf) - 4)
1543 *bp++ = hex2ascii(*rptr >> 4);
1544 *bp++ = hex2ascii(*rptr++ & 0xf);
1548 if (bp > buf + sizeof(buf) - 3)
1558 printf("%s\n", buf);