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.13 2004/03/23 22:19:06 hsu 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,
143 static void pppintr (struct netmsg *msg);
145 static void ppp_requeue (struct ppp_softc *);
146 static void ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
147 static void ppp_ccp_closed (struct ppp_softc *);
148 static void ppp_inproc (struct ppp_softc *, struct mbuf *);
149 static void pppdumpm (struct mbuf *m0);
152 * Some useful mbuf macros not in mbuf.h.
154 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
156 #define M_DATASTART(m) \
157 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
158 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
160 #define M_DATASIZE(m) \
161 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
162 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
165 * We steal two bits in the mbuf m_flags, to mark high-priority packets
166 * for output, and received packets following lost/corrupted packets.
168 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
169 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
174 * List of compressors we know about.
175 * We leave some space so maybe we can modload compressors.
178 extern struct compressor ppp_bsd_compress;
179 extern struct compressor ppp_deflate, ppp_deflate_draft;
181 static struct compressor *ppp_compressors[8] = {
182 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
185 #if DO_DEFLATE && defined(PPP_DEFLATE)
191 #endif /* PPP_COMPRESS */
194 * Called from boot code to establish ppp interfaces.
200 struct ppp_softc *sc;
203 for (sc = ppp_softc; i < NPPP; sc++) {
204 if_initname(&(sc->sc_if), "ppp", i++);
205 sc->sc_if.if_mtu = PPP_MTU;
206 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
207 sc->sc_if.if_type = IFT_PPP;
208 sc->sc_if.if_hdrlen = PPP_HDRLEN;
209 sc->sc_if.if_ioctl = pppsioctl;
210 sc->sc_if.if_output = pppoutput;
211 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
212 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
213 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
214 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
215 if_attach(&sc->sc_if);
216 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
218 netisr_register(NETISR_PPP, cpu0_portfn, pppintr);
220 * XXX layering violation - if_ppp can work over any lower level
221 * transport that cares to attach to it.
223 pppasyncattach(dummy);
227 * Allocate a ppp interface unit and initialize it.
230 pppalloc(struct thread *td)
233 struct ppp_softc *sc;
235 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
236 if (sc->sc_xfer == td) {
240 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
241 if (sc->sc_devp == NULL)
247 sc->sc_mru = PPP_MRU;
248 sc->sc_relinq = NULL;
249 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
251 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
254 sl_compress_init(sc->sc_comp, -1);
257 sc->sc_xc_state = NULL;
258 sc->sc_rc_state = NULL;
259 #endif /* PPP_COMPRESS */
260 for (i = 0; i < NUM_NP; ++i)
261 sc->sc_npmode[i] = NPMODE_ERROR;
262 sc->sc_npqueue = NULL;
263 sc->sc_npqtail = &sc->sc_npqueue;
264 sc->sc_last_sent = sc->sc_last_recv = time_second;
270 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
274 struct ppp_softc *sc;
279 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
280 getmicrotime(&sc->sc_if.if_lastchange);
284 IF_DEQUEUE(&sc->sc_rawq, m);
290 IF_DEQUEUE(&sc->sc_inq, m);
296 IF_DEQUEUE(&sc->sc_fastq, m);
301 while ((m = sc->sc_npqueue) != NULL) {
302 sc->sc_npqueue = m->m_nextpkt;
307 sc->sc_xc_state = NULL;
308 sc->sc_rc_state = NULL;
309 #endif /* PPP_COMPRESS */
311 if (sc->sc_pass_filt.bf_insns != 0) {
312 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
313 sc->sc_pass_filt.bf_insns = 0;
314 sc->sc_pass_filt.bf_len = 0;
316 if (sc->sc_active_filt.bf_insns != 0) {
317 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
318 sc->sc_active_filt.bf_insns = 0;
319 sc->sc_active_filt.bf_len = 0;
321 #endif /* PPP_FILTER */
323 if (sc->sc_comp != 0) {
324 free(sc->sc_comp, M_DEVBUF);
331 * Ioctl routine for generic ppp devices.
334 pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
335 int flag, struct thread *td)
337 int s, error, flags, mru, npx;
339 struct ppp_option_data *odp;
340 struct compressor **cp;
344 struct bpf_program *bp, *nbp;
345 struct bpf_insn *newcode, *oldcode;
347 #endif /* PPP_FILTER */
349 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
354 *(int *)data = sc->sc_inq.ifq_len;
358 *(int *)data = sc->sc_if.if_dunit;
362 *(u_int *)data = sc->sc_flags;
366 if ((error = suser(td)) != 0)
368 flags = *(int *)data & SC_MASK;
371 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
375 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
380 if ((error = suser(td)) != 0)
383 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
388 *(int *)data = sc->sc_mru;
393 if ((error = suser(td)) != 0)
397 sl_compress_init(sc->sc_comp, *(int *)data);
404 if ((error = suser(td)) != 0)
410 case PPPIOCSCOMPRESS:
411 if ((error = suser(td)) != 0)
413 odp = (struct ppp_option_data *) data;
415 if (nb > sizeof(ccp_option))
416 nb = sizeof(ccp_option);
417 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
419 if (ccp_option[1] < 2) /* preliminary check on the length byte */
421 for (cp = ppp_compressors; *cp != NULL; ++cp)
422 if ((*cp)->compress_proto == ccp_option[0]) {
424 * Found a handler for the protocol - try to allocate
425 * a compressor or decompressor.
430 if (sc->sc_xc_state != NULL)
431 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
433 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
434 if (sc->sc_xc_state == NULL) {
435 if (sc->sc_flags & SC_DEBUG)
436 printf("%s: comp_alloc failed\n",
441 sc->sc_flags &= ~SC_COMP_RUN;
445 if (sc->sc_rc_state != NULL)
446 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
448 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
449 if (sc->sc_rc_state == NULL) {
450 if (sc->sc_flags & SC_DEBUG)
451 printf("%s: decomp_alloc failed\n",
456 sc->sc_flags &= ~SC_DECOMP_RUN;
461 if (sc->sc_flags & SC_DEBUG)
462 printf("%s: no compressor for [%x %x %x], %x\n",
463 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
465 return (EINVAL); /* no handler found */
466 #endif /* PPP_COMPRESS */
470 npi = (struct npioctl *) data;
471 switch (npi->protocol) {
478 if (cmd == PPPIOCGNPMODE) {
479 npi->mode = sc->sc_npmode[npx];
481 if ((error = suser(td)) != 0)
483 if (npi->mode != sc->sc_npmode[npx]) {
485 sc->sc_npmode[npx] = npi->mode;
486 if (npi->mode != NPMODE_QUEUE) {
498 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
499 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
506 nbp = (struct bpf_program *) data;
507 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
509 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
510 if (newcodelen != 0) {
511 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
513 return EINVAL; /* or sumpin */
515 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
517 free(newcode, M_DEVBUF);
520 if (!bpf_validate(newcode, nbp->bf_len)) {
521 free(newcode, M_DEVBUF);
526 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
527 oldcode = bp->bf_insns;
529 bp->bf_len = nbp->bf_len;
530 bp->bf_insns = newcode;
533 free(oldcode, M_DEVBUF);
544 * Process an ioctl request to the ppp network interface.
547 pppsioctl(ifp, cmd, data, cr)
553 struct thread *td = curthread; /* XXX */
554 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
555 struct ifaddr *ifa = (struct ifaddr *)data;
556 struct ifreq *ifr = (struct ifreq *)data;
557 struct ppp_stats *psp;
559 struct ppp_comp_stats *pcp;
561 int s = splimp(), error = 0;
565 if ((ifp->if_flags & IFF_RUNNING) == 0)
566 ifp->if_flags &= ~IFF_UP;
571 switch(ifa->ifa_addr->sa_family) {
581 error = EAFNOSUPPORT;
587 switch(ifa->ifa_addr->sa_family) {
597 error = EAFNOSUPPORT;
603 if ((error = suser(td)) != 0)
605 if (ifr->ifr_mtu > PPP_MAXMTU)
608 sc->sc_if.if_mtu = ifr->ifr_mtu;
610 (*sc->sc_setmtu)(sc);
615 ifr->ifr_mtu = sc->sc_if.if_mtu;
621 error = EAFNOSUPPORT;
624 switch(ifr->ifr_addr.sa_family) {
630 error = EAFNOSUPPORT;
636 psp = &((struct ifpppstatsreq *) data)->stats;
637 bzero(psp, sizeof(*psp));
638 psp->p = sc->sc_stats;
639 #if defined(VJC) && !defined(SL_NO_STATS)
641 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
642 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
643 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
644 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
645 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
646 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
647 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
648 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
655 pcp = &((struct ifpppcstatsreq *) data)->stats;
656 bzero(pcp, sizeof(*pcp));
657 if (sc->sc_xc_state != NULL)
658 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
659 if (sc->sc_rc_state != NULL)
660 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
662 #endif /* PPP_COMPRESS */
672 * Queue a packet. Start transmission if not active.
673 * Packet is placed in Information field of PPP frame.
674 * Called at splnet as the if->if_output handler.
675 * Called at splnet from pppwrite().
678 pppoutput(ifp, m0, dst, rtp)
681 struct sockaddr *dst;
684 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
685 int protocol, address, control;
694 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
695 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
696 error = ENETDOWN; /* sort of */
701 * Compute PPP header.
703 m0->m_flags &= ~M_HIGHPRI;
704 switch (dst->sa_family) {
707 address = PPP_ALLSTATIONS;
710 mode = sc->sc_npmode[NP_IP];
713 * If this packet has the "low delay" bit set in the IP header,
714 * put it on the fastq instead.
716 ip = mtod(m0, struct ip *);
717 if (ip->ip_tos & IPTOS_LOWDELAY)
718 m0->m_flags |= M_HIGHPRI;
724 * This is pretty bogus.. We dont have an ipxcp module in pppd
725 * yet to configure the link parameters. Sigh. I guess a
726 * manual ifconfig would do.... -Peter
728 address = PPP_ALLSTATIONS;
735 address = PPP_ADDRESS(dst->sa_data);
736 control = PPP_CONTROL(dst->sa_data);
737 protocol = PPP_PROTOCOL(dst->sa_data);
741 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
742 error = EAFNOSUPPORT;
747 * Drop this packet, or return an error, if necessary.
749 if (mode == NPMODE_ERROR) {
753 if (mode == NPMODE_DROP) {
759 * Add PPP header. If no space in first mbuf, allocate another.
760 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
762 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
763 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
770 m0->m_data -= PPP_HDRLEN;
772 cp = mtod(m0, u_char *);
775 *cp++ = protocol >> 8;
776 *cp++ = protocol & 0xff;
777 m0->m_len += PPP_HDRLEN;
780 for (m = m0; m != 0; m = m->m_next)
783 if (sc->sc_flags & SC_LOG_OUTPKT) {
784 printf("%s output: ", ifp->if_xname);
788 if ((protocol & 0x8000) == 0) {
791 * Apply the pass and active filters to the packet,
792 * but only if it is a data packet.
794 *mtod(m0, u_char *) = 1; /* indicates outbound */
795 if (sc->sc_pass_filt.bf_insns != 0
796 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
798 error = 0; /* drop this packet */
803 * Update the time we sent the most recent packet.
805 if (sc->sc_active_filt.bf_insns == 0
806 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
807 sc->sc_last_sent = time_second;
809 *mtod(m0, u_char *) = address;
812 * Update the time we sent the most recent data packet.
814 sc->sc_last_sent = time_second;
815 #endif /* PPP_FILTER */
819 * See if bpf wants to look at the packet.
825 * Put the packet on the appropriate queue.
827 s = splsoftnet(); /* redundant */
828 if (mode == NPMODE_QUEUE) {
829 /* XXX we should limit the number of packets on this queue */
830 *sc->sc_npqtail = m0;
831 m0->m_nextpkt = NULL;
832 sc->sc_npqtail = &m0->m_nextpkt;
834 /* fastq and if_snd are emptied at spl[soft]net now */
835 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
836 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
839 sc->sc_if.if_oerrors++;
840 sc->sc_stats.ppp_oerrors++;
847 getmicrotime(&ifp->if_lastchange);
849 ifp->if_obytes += len;
860 * After a change in the NPmode for some NP, move packets from the
861 * npqueue to the send queue or the fast queue as appropriate.
862 * Should be called at spl[soft]net.
866 struct ppp_softc *sc;
868 struct mbuf *m, **mpp;
872 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
873 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
875 mode = sc->sc_npmode[NP_IP];
884 * This packet can now go on one of the queues to be sent.
888 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
891 sc->sc_if.if_oerrors++;
892 sc->sc_stats.ppp_oerrors++;
908 sc->sc_npqtail = mpp;
912 * Transmitter has finished outputting some stuff;
913 * remember to call sc->sc_start later at splsoftnet.
917 struct ppp_softc *sc;
921 sc->sc_flags &= ~SC_TBUSY;
922 schednetisr(NETISR_PPP);
928 * Get a packet to send. This procedure is intended to be called at
929 * splsoftnet, since it may involve time-consuming operations such as
930 * applying VJ compression, packet compression, address/control and/or
931 * protocol field compression to the packet.
935 struct ppp_softc *sc;
939 int address, control, protocol;
942 * Grab a packet to send: first try the fast queue, then the
945 IF_DEQUEUE(&sc->sc_fastq, m);
947 IF_DEQUEUE(&sc->sc_if.if_snd, m);
951 ++sc->sc_stats.ppp_opackets;
954 * Extract the ppp header of the new packet.
955 * The ppp header will be in one mbuf.
957 cp = mtod(m, u_char *);
958 address = PPP_ADDRESS(cp);
959 control = PPP_CONTROL(cp);
960 protocol = PPP_PROTOCOL(cp);
966 * If the packet is a TCP/IP packet, see if we can compress it.
968 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
973 ip = (struct ip *) (cp + PPP_HDRLEN);
974 if (mp->m_len <= PPP_HDRLEN) {
978 ip = mtod(mp, struct ip *);
980 /* this code assumes the IP/TCP header is in one non-shared mbuf */
981 if (ip->ip_p == IPPROTO_TCP) {
982 type = sl_compress_tcp(mp, ip, sc->sc_comp,
983 !(sc->sc_flags & SC_NO_TCP_CCID));
985 case TYPE_UNCOMPRESSED_TCP:
986 protocol = PPP_VJC_UNCOMP;
988 case TYPE_COMPRESSED_TCP:
989 protocol = PPP_VJC_COMP;
990 cp = mtod(m, u_char *);
991 cp[0] = address; /* header has moved */
996 cp[3] = protocol; /* update protocol in PPP header */
1006 #endif /* PPP_COMPRESS */
1010 if (protocol != PPP_LCP && protocol != PPP_CCP
1011 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1012 struct mbuf *mcomp = NULL;
1016 for (mp = m; mp != NULL; mp = mp->m_next)
1018 clen = (*sc->sc_xcomp->compress)
1019 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1020 if (mcomp != NULL) {
1021 if (sc->sc_flags & SC_CCP_UP) {
1022 /* Send the compressed packet instead of the original. */
1025 cp = mtod(m, u_char *);
1028 /* Can't transmit compressed packets until CCP is up. */
1033 #endif /* PPP_COMPRESS */
1036 * Compress the address/control and protocol, if possible.
1038 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1039 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1040 protocol != PPP_LCP) {
1041 /* can compress address/control */
1045 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1046 /* can compress protocol */
1047 if (mtod(m, u_char *) == cp) {
1048 cp[2] = cp[1]; /* move address/control up */
1059 * Software interrupt routine, called at spl[soft]net.
1062 pppintr(struct netmsg *msg)
1064 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
1065 struct ppp_softc *sc;
1069 for (i = 0; i < NPPP; ++i, ++sc) {
1071 if (!(sc->sc_flags & SC_TBUSY)
1072 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1073 sc->sc_flags |= SC_TBUSY;
1075 (*sc->sc_start)(sc);
1080 IF_DEQUEUE(&sc->sc_rawq, m);
1091 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1092 * 0 if it is about to be transmitted.
1095 ppp_ccp(sc, m, rcvd)
1096 struct ppp_softc *sc;
1105 * Get a pointer to the data after the PPP header.
1107 if (m->m_len <= PPP_HDRLEN) {
1111 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1114 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1117 ep = mtod(mp, u_char *) + mp->m_len;
1118 if (dp + CCP_HDRLEN > ep)
1120 slen = CCP_LENGTH(dp);
1121 if (dp + slen > ep) {
1122 if (sc->sc_flags & SC_DEBUG)
1123 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1124 dp, slen, mtod(mp, u_char *), mp->m_len);
1128 switch (CCP_CODE(dp)) {
1132 /* CCP must be going down - disable compression */
1133 if (sc->sc_flags & SC_CCP_UP) {
1135 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1141 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1142 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1143 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1145 /* we're agreeing to send compressed packets. */
1146 if (sc->sc_xc_state != NULL
1147 && (*sc->sc_xcomp->comp_init)
1148 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1149 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1151 sc->sc_flags |= SC_COMP_RUN;
1155 /* peer is agreeing to send compressed packets. */
1156 if (sc->sc_rc_state != NULL
1157 && (*sc->sc_rcomp->decomp_init)
1158 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1159 sc->sc_if.if_dunit, 0, sc->sc_mru,
1160 sc->sc_flags & SC_DEBUG)) {
1162 sc->sc_flags |= SC_DECOMP_RUN;
1163 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1171 if (sc->sc_flags & SC_CCP_UP) {
1173 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1174 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1176 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1177 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1179 sc->sc_flags &= ~SC_DC_ERROR;
1189 * CCP is down; free (de)compressor state if necessary.
1193 struct ppp_softc *sc;
1195 if (sc->sc_xc_state) {
1196 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1197 sc->sc_xc_state = NULL;
1199 if (sc->sc_rc_state) {
1200 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1201 sc->sc_rc_state = NULL;
1204 #endif /* PPP_COMPRESS */
1207 * PPP packet input routine.
1208 * The caller has checked and removed the FCS and has inserted
1209 * the address/control bytes and the protocol high byte if they
1213 ppppktin(sc, m, lost)
1214 struct ppp_softc *sc;
1221 m->m_flags |= M_ERRMARK;
1222 IF_ENQUEUE(&sc->sc_rawq, m);
1223 schednetisr(NETISR_PPP);
1228 * Process a received PPP packet, doing decompression as necessary.
1229 * Should be called at splsoftnet.
1231 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1232 TYPE_UNCOMPRESSED_TCP)
1236 struct ppp_softc *sc;
1239 struct ifnet *ifp = &sc->sc_if;
1241 int s, ilen = 0, xlen, proto, rv;
1242 u_char *cp, adrs, ctrl;
1243 struct mbuf *mp, *dmp = NULL;
1247 sc->sc_stats.ppp_ipackets++;
1249 if (sc->sc_flags & SC_LOG_INPKT) {
1251 for (mp = m; mp != NULL; mp = mp->m_next)
1253 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1257 cp = mtod(m, u_char *);
1258 adrs = PPP_ADDRESS(cp);
1259 ctrl = PPP_CONTROL(cp);
1260 proto = PPP_PROTOCOL(cp);
1262 if (m->m_flags & M_ERRMARK) {
1263 m->m_flags &= ~M_ERRMARK;
1265 sc->sc_flags |= SC_VJ_RESET;
1271 * Decompress this packet if necessary, update the receiver's
1272 * dictionary, or take appropriate action on a CCP packet.
1274 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1275 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1276 /* decompress this packet */
1277 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1278 if (rv == DECOMP_OK) {
1281 /* no error, but no decompressed packet produced */
1285 cp = mtod(m, u_char *);
1286 proto = PPP_PROTOCOL(cp);
1290 * An error has occurred in decompression.
1291 * Pass the compressed packet up to pppd, which may take
1292 * CCP down or issue a Reset-Req.
1294 if (sc->sc_flags & SC_DEBUG)
1295 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1297 sc->sc_flags |= SC_VJ_RESET;
1298 if (rv == DECOMP_ERROR)
1299 sc->sc_flags |= SC_DC_ERROR;
1301 sc->sc_flags |= SC_DC_FERROR;
1306 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1307 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1309 if (proto == PPP_CCP) {
1316 for (mp = m; mp != NULL; mp = mp->m_next)
1320 if (sc->sc_flags & SC_VJ_RESET) {
1322 * If we've missed a packet, we must toss subsequent compressed
1323 * packets which don't have an explicit connection ID.
1326 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1328 sc->sc_flags &= ~SC_VJ_RESET;
1333 * See if we have a VJ-compressed packet to uncompress.
1335 if (proto == PPP_VJC_COMP) {
1336 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1339 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1340 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1341 sc->sc_comp, &iphdr, &hlen);
1344 if (sc->sc_flags & SC_DEBUG)
1345 printf("%s: VJ uncompress failed on type comp\n",
1350 /* Copy the PPP and IP headers into a new mbuf. */
1351 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1356 if (hlen + PPP_HDRLEN > MHLEN) {
1357 MCLGET(mp, M_DONTWAIT);
1358 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1360 goto bad; /* lose if big headers and no clusters */
1363 cp = mtod(mp, u_char *);
1369 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1370 mp->m_len = hlen + PPP_HDRLEN;
1373 * Trim the PPP and VJ headers off the old mbuf
1374 * and stick the new and old mbufs together.
1376 m->m_data += PPP_HDRLEN + xlen;
1377 m->m_len -= PPP_HDRLEN + xlen;
1378 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1379 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1380 mp->m_len += m->m_len;
1381 mp->m_next = m_free(m);
1386 ilen += hlen - xlen;
1388 } else if (proto == PPP_VJC_UNCOMP) {
1389 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1392 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1393 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1394 sc->sc_comp, &iphdr, &hlen);
1397 if (sc->sc_flags & SC_DEBUG)
1398 printf("%s: VJ uncompress failed on type uncomp\n",
1409 * If the packet will fit in a header mbuf, don't waste a
1410 * whole cluster on it.
1412 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1413 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1415 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1421 m->m_pkthdr.len = ilen;
1422 m->m_pkthdr.rcvif = ifp;
1424 if ((proto & 0x8000) == 0) {
1427 * See whether we want to pass this packet, and
1428 * if it counts as link activity.
1430 adrs = *mtod(m, u_char *); /* save address field */
1431 *mtod(m, u_char *) = 0; /* indicate inbound */
1432 if (sc->sc_pass_filt.bf_insns != 0
1433 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1435 /* drop this packet */
1439 if (sc->sc_active_filt.bf_insns == 0
1440 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1441 sc->sc_last_recv = time_second;
1443 *mtod(m, u_char *) = adrs;
1446 * Record the time that we received this packet.
1448 sc->sc_last_recv = time_second;
1449 #endif /* PPP_FILTER */
1452 /* See if bpf wants to look at the packet. */
1453 if (sc->sc_if.if_bpf)
1454 bpf_mtap(&sc->sc_if, m);
1461 * IP packet - take off the ppp header and pass it up to IP.
1463 if ((ifp->if_flags & IFF_UP) == 0
1464 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1465 /* interface is down - drop the packet. */
1469 m->m_pkthdr.len -= PPP_HDRLEN;
1470 m->m_data += PPP_HDRLEN;
1471 m->m_len -= PPP_HDRLEN;
1472 if (ipflow_fastforward(m))
1480 * IPX packet - take off the ppp header and pass it up to IPX.
1482 if ((sc->sc_if.if_flags & IFF_UP) == 0
1483 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1484 /* interface is down - drop the packet. */
1488 m->m_pkthdr.len -= PPP_HDRLEN;
1489 m->m_data += PPP_HDRLEN;
1490 m->m_len -= PPP_HDRLEN;
1492 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1498 * Some other protocol - place on input queue for read().
1504 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1506 rv = (netisr_queue(isr, m) == 0);
1508 if (sc->sc_flags & SC_DEBUG)
1509 printf("%s: input queue full\n", ifp->if_xname);
1515 ifp->if_ibytes += ilen;
1516 getmicrotime(&ifp->if_lastchange);
1525 sc->sc_if.if_ierrors++;
1526 sc->sc_stats.ppp_ierrors++;
1529 #define MAX_DUMP_BYTES 128
1535 char buf[3*MAX_DUMP_BYTES+4];
1539 for (m = m0; m; m = m->m_next) {
1541 u_char *rptr = (u_char *)m->m_data;
1544 if (bp > buf + sizeof(buf) - 4)
1546 *bp++ = hex2ascii(*rptr >> 4);
1547 *bp++ = hex2ascii(*rptr++ & 0xf);
1551 if (bp > buf + sizeof(buf) - 3)
1561 printf("%s\n", buf);