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.6 2003/08/07 21:17:30 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 <netipx/ipx.h>
113 #include <netipx/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 __P((void *));
138 static void pppattach __P((void *));
139 PSEUDO_SET(pppattach, if_ppp);
141 static int pppsioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
142 static void pppintr __P((void));
144 static void ppp_requeue __P((struct ppp_softc *));
145 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
146 static void ppp_ccp_closed __P((struct ppp_softc *));
147 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
148 static void pppdumpm __P((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 sc->sc_if.if_name = "ppp";
204 sc->sc_if.if_unit = 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 register_netisr(NETISR_PPP, 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_unit;
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("ppp%d: 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("ppp%d: decomp_alloc failed\n",
456 sc->sc_flags &= ~SC_DECOMP_RUN;
461 if (sc->sc_flags & SC_DEBUG)
462 printf("ppp%d: no compressor for [%x %x %x], %x\n",
463 sc->sc_if.if_unit, 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)
552 struct thread *td = curthread; /* XXX */
553 struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
554 struct ifaddr *ifa = (struct ifaddr *)data;
555 struct ifreq *ifr = (struct ifreq *)data;
556 struct ppp_stats *psp;
558 struct ppp_comp_stats *pcp;
560 int s = splimp(), error = 0;
564 if ((ifp->if_flags & IFF_RUNNING) == 0)
565 ifp->if_flags &= ~IFF_UP;
570 switch(ifa->ifa_addr->sa_family) {
580 error = EAFNOSUPPORT;
586 switch(ifa->ifa_addr->sa_family) {
596 error = EAFNOSUPPORT;
602 if ((error = suser(td)) != 0)
604 if (ifr->ifr_mtu > PPP_MAXMTU)
607 sc->sc_if.if_mtu = ifr->ifr_mtu;
609 (*sc->sc_setmtu)(sc);
614 ifr->ifr_mtu = sc->sc_if.if_mtu;
620 error = EAFNOSUPPORT;
623 switch(ifr->ifr_addr.sa_family) {
629 error = EAFNOSUPPORT;
635 psp = &((struct ifpppstatsreq *) data)->stats;
636 bzero(psp, sizeof(*psp));
637 psp->p = sc->sc_stats;
638 #if defined(VJC) && !defined(SL_NO_STATS)
640 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
641 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
642 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
643 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
644 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
645 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
646 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
647 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
654 pcp = &((struct ifpppcstatsreq *) data)->stats;
655 bzero(pcp, sizeof(*pcp));
656 if (sc->sc_xc_state != NULL)
657 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
658 if (sc->sc_rc_state != NULL)
659 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
661 #endif /* PPP_COMPRESS */
671 * Queue a packet. Start transmission if not active.
672 * Packet is placed in Information field of PPP frame.
673 * Called at splnet as the if->if_output handler.
674 * Called at splnet from pppwrite().
677 pppoutput(ifp, m0, dst, rtp)
680 struct sockaddr *dst;
683 struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
684 int protocol, address, control;
693 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
694 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
695 error = ENETDOWN; /* sort of */
700 * Compute PPP header.
702 m0->m_flags &= ~M_HIGHPRI;
703 switch (dst->sa_family) {
706 address = PPP_ALLSTATIONS;
709 mode = sc->sc_npmode[NP_IP];
712 * If this packet has the "low delay" bit set in the IP header,
713 * put it on the fastq instead.
715 ip = mtod(m0, struct ip *);
716 if (ip->ip_tos & IPTOS_LOWDELAY)
717 m0->m_flags |= M_HIGHPRI;
723 * This is pretty bogus.. We dont have an ipxcp module in pppd
724 * yet to configure the link parameters. Sigh. I guess a
725 * manual ifconfig would do.... -Peter
727 address = PPP_ALLSTATIONS;
734 address = PPP_ADDRESS(dst->sa_data);
735 control = PPP_CONTROL(dst->sa_data);
736 protocol = PPP_PROTOCOL(dst->sa_data);
740 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
741 error = EAFNOSUPPORT;
746 * Drop this packet, or return an error, if necessary.
748 if (mode == NPMODE_ERROR) {
752 if (mode == NPMODE_DROP) {
758 * Add PPP header. If no space in first mbuf, allocate another.
759 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
761 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
762 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
769 m0->m_data -= PPP_HDRLEN;
771 cp = mtod(m0, u_char *);
774 *cp++ = protocol >> 8;
775 *cp++ = protocol & 0xff;
776 m0->m_len += PPP_HDRLEN;
779 for (m = m0; m != 0; m = m->m_next)
782 if (sc->sc_flags & SC_LOG_OUTPKT) {
783 printf("ppp%d output: ", ifp->if_unit);
787 if ((protocol & 0x8000) == 0) {
790 * Apply the pass and active filters to the packet,
791 * but only if it is a data packet.
793 *mtod(m0, u_char *) = 1; /* indicates outbound */
794 if (sc->sc_pass_filt.bf_insns != 0
795 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
797 error = 0; /* drop this packet */
802 * Update the time we sent the most recent packet.
804 if (sc->sc_active_filt.bf_insns == 0
805 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
806 sc->sc_last_sent = time_second;
808 *mtod(m0, u_char *) = address;
811 * Update the time we sent the most recent data packet.
813 sc->sc_last_sent = time_second;
814 #endif /* PPP_FILTER */
818 * See if bpf wants to look at the packet.
824 * Put the packet on the appropriate queue.
826 s = splsoftnet(); /* redundant */
827 if (mode == NPMODE_QUEUE) {
828 /* XXX we should limit the number of packets on this queue */
829 *sc->sc_npqtail = m0;
830 m0->m_nextpkt = NULL;
831 sc->sc_npqtail = &m0->m_nextpkt;
833 /* fastq and if_snd are emptied at spl[soft]net now */
834 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
835 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
838 sc->sc_if.if_oerrors++;
839 sc->sc_stats.ppp_oerrors++;
846 getmicrotime(&ifp->if_lastchange);
848 ifp->if_obytes += len;
859 * After a change in the NPmode for some NP, move packets from the
860 * npqueue to the send queue or the fast queue as appropriate.
861 * Should be called at spl[soft]net.
865 struct ppp_softc *sc;
867 struct mbuf *m, **mpp;
871 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
872 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
874 mode = sc->sc_npmode[NP_IP];
883 * This packet can now go on one of the queues to be sent.
887 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
890 sc->sc_if.if_oerrors++;
891 sc->sc_stats.ppp_oerrors++;
907 sc->sc_npqtail = mpp;
911 * Transmitter has finished outputting some stuff;
912 * remember to call sc->sc_start later at splsoftnet.
916 struct ppp_softc *sc;
920 sc->sc_flags &= ~SC_TBUSY;
921 schednetisr(NETISR_PPP);
927 * Get a packet to send. This procedure is intended to be called at
928 * splsoftnet, since it may involve time-consuming operations such as
929 * applying VJ compression, packet compression, address/control and/or
930 * protocol field compression to the packet.
934 struct ppp_softc *sc;
938 int address, control, protocol;
941 * Grab a packet to send: first try the fast queue, then the
944 IF_DEQUEUE(&sc->sc_fastq, m);
946 IF_DEQUEUE(&sc->sc_if.if_snd, m);
950 ++sc->sc_stats.ppp_opackets;
953 * Extract the ppp header of the new packet.
954 * The ppp header will be in one mbuf.
956 cp = mtod(m, u_char *);
957 address = PPP_ADDRESS(cp);
958 control = PPP_CONTROL(cp);
959 protocol = PPP_PROTOCOL(cp);
965 * If the packet is a TCP/IP packet, see if we can compress it.
967 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
972 ip = (struct ip *) (cp + PPP_HDRLEN);
973 if (mp->m_len <= PPP_HDRLEN) {
977 ip = mtod(mp, struct ip *);
979 /* this code assumes the IP/TCP header is in one non-shared mbuf */
980 if (ip->ip_p == IPPROTO_TCP) {
981 type = sl_compress_tcp(mp, ip, sc->sc_comp,
982 !(sc->sc_flags & SC_NO_TCP_CCID));
984 case TYPE_UNCOMPRESSED_TCP:
985 protocol = PPP_VJC_UNCOMP;
987 case TYPE_COMPRESSED_TCP:
988 protocol = PPP_VJC_COMP;
989 cp = mtod(m, u_char *);
990 cp[0] = address; /* header has moved */
995 cp[3] = protocol; /* update protocol in PPP header */
1005 #endif /* PPP_COMPRESS */
1009 if (protocol != PPP_LCP && protocol != PPP_CCP
1010 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1011 struct mbuf *mcomp = NULL;
1015 for (mp = m; mp != NULL; mp = mp->m_next)
1017 clen = (*sc->sc_xcomp->compress)
1018 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1019 if (mcomp != NULL) {
1020 if (sc->sc_flags & SC_CCP_UP) {
1021 /* Send the compressed packet instead of the original. */
1024 cp = mtod(m, u_char *);
1027 /* Can't transmit compressed packets until CCP is up. */
1032 #endif /* PPP_COMPRESS */
1035 * Compress the address/control and protocol, if possible.
1037 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1038 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1039 protocol != PPP_LCP) {
1040 /* can compress address/control */
1044 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1045 /* can compress protocol */
1046 if (mtod(m, u_char *) == cp) {
1047 cp[2] = cp[1]; /* move address/control up */
1058 * Software interrupt routine, called at spl[soft]net.
1063 struct ppp_softc *sc;
1068 for (i = 0; i < NPPP; ++i, ++sc) {
1070 if (!(sc->sc_flags & SC_TBUSY)
1071 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1072 sc->sc_flags |= SC_TBUSY;
1074 (*sc->sc_start)(sc);
1079 IF_DEQUEUE(&sc->sc_rawq, m);
1090 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1091 * 0 if it is about to be transmitted.
1094 ppp_ccp(sc, m, rcvd)
1095 struct ppp_softc *sc;
1104 * Get a pointer to the data after the PPP header.
1106 if (m->m_len <= PPP_HDRLEN) {
1110 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1113 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1116 ep = mtod(mp, u_char *) + mp->m_len;
1117 if (dp + CCP_HDRLEN > ep)
1119 slen = CCP_LENGTH(dp);
1120 if (dp + slen > ep) {
1121 if (sc->sc_flags & SC_DEBUG)
1122 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1123 dp, slen, mtod(mp, u_char *), mp->m_len);
1127 switch (CCP_CODE(dp)) {
1131 /* CCP must be going down - disable compression */
1132 if (sc->sc_flags & SC_CCP_UP) {
1134 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1140 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1141 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1142 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1144 /* we're agreeing to send compressed packets. */
1145 if (sc->sc_xc_state != NULL
1146 && (*sc->sc_xcomp->comp_init)
1147 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1148 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1150 sc->sc_flags |= SC_COMP_RUN;
1154 /* peer is agreeing to send compressed packets. */
1155 if (sc->sc_rc_state != NULL
1156 && (*sc->sc_rcomp->decomp_init)
1157 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1158 sc->sc_if.if_unit, 0, sc->sc_mru,
1159 sc->sc_flags & SC_DEBUG)) {
1161 sc->sc_flags |= SC_DECOMP_RUN;
1162 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1170 if (sc->sc_flags & SC_CCP_UP) {
1172 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1173 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1175 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1176 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1178 sc->sc_flags &= ~SC_DC_ERROR;
1188 * CCP is down; free (de)compressor state if necessary.
1192 struct ppp_softc *sc;
1194 if (sc->sc_xc_state) {
1195 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1196 sc->sc_xc_state = NULL;
1198 if (sc->sc_rc_state) {
1199 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1200 sc->sc_rc_state = NULL;
1203 #endif /* PPP_COMPRESS */
1206 * PPP packet input routine.
1207 * The caller has checked and removed the FCS and has inserted
1208 * the address/control bytes and the protocol high byte if they
1212 ppppktin(sc, m, lost)
1213 struct ppp_softc *sc;
1220 m->m_flags |= M_ERRMARK;
1221 IF_ENQUEUE(&sc->sc_rawq, m);
1222 schednetisr(NETISR_PPP);
1227 * Process a received PPP packet, doing decompression as necessary.
1228 * Should be called at splsoftnet.
1230 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1231 TYPE_UNCOMPRESSED_TCP)
1235 struct ppp_softc *sc;
1238 struct ifnet *ifp = &sc->sc_if;
1239 struct ifqueue *inq;
1240 int s, ilen = 0, xlen, proto, rv;
1241 u_char *cp, adrs, ctrl;
1242 struct mbuf *mp, *dmp = NULL;
1246 sc->sc_stats.ppp_ipackets++;
1248 if (sc->sc_flags & SC_LOG_INPKT) {
1250 for (mp = m; mp != NULL; mp = mp->m_next)
1252 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1256 cp = mtod(m, u_char *);
1257 adrs = PPP_ADDRESS(cp);
1258 ctrl = PPP_CONTROL(cp);
1259 proto = PPP_PROTOCOL(cp);
1261 if (m->m_flags & M_ERRMARK) {
1262 m->m_flags &= ~M_ERRMARK;
1264 sc->sc_flags |= SC_VJ_RESET;
1270 * Decompress this packet if necessary, update the receiver's
1271 * dictionary, or take appropriate action on a CCP packet.
1273 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1274 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1275 /* decompress this packet */
1276 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1277 if (rv == DECOMP_OK) {
1280 /* no error, but no decompressed packet produced */
1284 cp = mtod(m, u_char *);
1285 proto = PPP_PROTOCOL(cp);
1289 * An error has occurred in decompression.
1290 * Pass the compressed packet up to pppd, which may take
1291 * CCP down or issue a Reset-Req.
1293 if (sc->sc_flags & SC_DEBUG)
1294 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1296 sc->sc_flags |= SC_VJ_RESET;
1297 if (rv == DECOMP_ERROR)
1298 sc->sc_flags |= SC_DC_ERROR;
1300 sc->sc_flags |= SC_DC_FERROR;
1305 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1306 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1308 if (proto == PPP_CCP) {
1315 for (mp = m; mp != NULL; mp = mp->m_next)
1319 if (sc->sc_flags & SC_VJ_RESET) {
1321 * If we've missed a packet, we must toss subsequent compressed
1322 * packets which don't have an explicit connection ID.
1325 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1327 sc->sc_flags &= ~SC_VJ_RESET;
1332 * See if we have a VJ-compressed packet to uncompress.
1334 if (proto == PPP_VJC_COMP) {
1335 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1338 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1339 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1340 sc->sc_comp, &iphdr, &hlen);
1343 if (sc->sc_flags & SC_DEBUG)
1344 printf("ppp%d: VJ uncompress failed on type comp\n",
1349 /* Copy the PPP and IP headers into a new mbuf. */
1350 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1355 if (hlen + PPP_HDRLEN > MHLEN) {
1356 MCLGET(mp, M_DONTWAIT);
1357 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1359 goto bad; /* lose if big headers and no clusters */
1362 cp = mtod(mp, u_char *);
1368 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1369 mp->m_len = hlen + PPP_HDRLEN;
1372 * Trim the PPP and VJ headers off the old mbuf
1373 * and stick the new and old mbufs together.
1375 m->m_data += PPP_HDRLEN + xlen;
1376 m->m_len -= PPP_HDRLEN + xlen;
1377 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1378 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1379 mp->m_len += m->m_len;
1380 mp->m_next = m_free(m);
1385 ilen += hlen - xlen;
1387 } else if (proto == PPP_VJC_UNCOMP) {
1388 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1391 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1392 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1393 sc->sc_comp, &iphdr, &hlen);
1396 if (sc->sc_flags & SC_DEBUG)
1397 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1408 * If the packet will fit in a header mbuf, don't waste a
1409 * whole cluster on it.
1411 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1412 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1414 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1420 m->m_pkthdr.len = ilen;
1421 m->m_pkthdr.rcvif = ifp;
1423 if ((proto & 0x8000) == 0) {
1426 * See whether we want to pass this packet, and
1427 * if it counts as link activity.
1429 adrs = *mtod(m, u_char *); /* save address field */
1430 *mtod(m, u_char *) = 0; /* indicate inbound */
1431 if (sc->sc_pass_filt.bf_insns != 0
1432 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1434 /* drop this packet */
1438 if (sc->sc_active_filt.bf_insns == 0
1439 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1440 sc->sc_last_recv = time_second;
1442 *mtod(m, u_char *) = adrs;
1445 * Record the time that we received this packet.
1447 sc->sc_last_recv = time_second;
1448 #endif /* PPP_FILTER */
1451 /* See if bpf wants to look at the packet. */
1452 if (sc->sc_if.if_bpf)
1453 bpf_mtap(&sc->sc_if, m);
1460 * IP packet - take off the ppp header and pass it up to IP.
1462 if ((ifp->if_flags & IFF_UP) == 0
1463 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1464 /* interface is down - drop the packet. */
1468 m->m_pkthdr.len -= PPP_HDRLEN;
1469 m->m_data += PPP_HDRLEN;
1470 m->m_len -= PPP_HDRLEN;
1471 if (ipflow_fastforward(m))
1473 schednetisr(NETISR_IP);
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;
1491 schednetisr(NETISR_IPX);
1493 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1499 * Some other protocol - place on input queue for read().
1507 * Put the packet on the appropriate input queue.
1510 if (IF_QFULL(inq)) {
1513 if (sc->sc_flags & SC_DEBUG)
1514 printf("ppp%d: input queue full\n", ifp->if_unit);
1521 ifp->if_ibytes += ilen;
1522 getmicrotime(&ifp->if_lastchange);
1531 sc->sc_if.if_ierrors++;
1532 sc->sc_stats.ppp_ierrors++;
1535 #define MAX_DUMP_BYTES 128
1541 char buf[3*MAX_DUMP_BYTES+4];
1545 for (m = m0; m; m = m->m_next) {
1547 u_char *rptr = (u_char *)m->m_data;
1550 if (bp > buf + sizeof(buf) - 4)
1552 *bp++ = hex2ascii(*rptr >> 4);
1553 *bp++ = hex2ascii(*rptr++ & 0xf);
1557 if (bp > buf + sizeof(buf) - 3)
1567 printf("%s\n", buf);