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.19 2004/08/10 22:05: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>
99 #include <sys/msgport2.h>
102 #include <net/if_types.h>
103 #include <net/netisr.h>
107 #include <netinet/in.h>
108 #include <netinet/in_systm.h>
109 #include <netinet/in_var.h>
110 #include <netinet/ip.h>
114 #include <netproto/ipx/ipx.h>
115 #include <netproto/ipx/ipx_if.h>
119 #include <net/slcompress.h>
123 #include "if_pppvar.h"
127 #define splsoftnet splnet
131 #define PACKETPTR struct mbuf *
132 #include <net/ppp_layer/ppp_comp.h>
135 struct ppp_softc ppp_softc[NPPP];
137 /* XXX layering violation */
138 extern void pppasyncattach (void *);
140 static void pppattach (void *);
141 PSEUDO_SET(pppattach, if_ppp);
143 static int pppsioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
146 static void ppp_requeue (struct ppp_softc *);
147 static void ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
148 static void ppp_ccp_closed (struct ppp_softc *);
149 static void ppp_inproc (struct ppp_softc *, struct mbuf *);
150 static void pppdumpm (struct mbuf *m0);
153 * Some useful mbuf macros not in mbuf.h.
155 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
157 #define M_DATASTART(m) \
158 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
159 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
161 #define M_DATASIZE(m) \
162 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
163 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
166 * We steal two bits in the mbuf m_flags, to mark high-priority packets
167 * for output, and received packets following lost/corrupted packets.
169 #define M_HIGHPRI M_PROTO2 /* output packet for sc_fastq */
170 #define M_ERRMARK M_PROTO3 /* steal a bit in mbuf m_flags */
175 * List of compressors we know about.
176 * We leave some space so maybe we can modload compressors.
179 extern struct compressor ppp_bsd_compress;
180 extern struct compressor ppp_deflate, ppp_deflate_draft;
182 static struct compressor *ppp_compressors[8] = {
183 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
186 #if DO_DEFLATE && defined(PPP_DEFLATE)
192 #endif /* PPP_COMPRESS */
195 * Software interrupt routine, called at spl[soft]net.
198 pppintr(struct netmsg *msg)
200 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
201 struct ppp_softc *sc;
205 for (i = 0; i < NPPP; ++i, ++sc) {
207 if (!(sc->sc_flags & SC_TBUSY)
208 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
209 sc->sc_flags |= SC_TBUSY;
216 IF_DEQUEUE(&sc->sc_rawq, m);
223 lwkt_replymsg(&msg->nm_lmsg, 0);
228 * Called from boot code to establish ppp interfaces.
234 struct ppp_softc *sc;
237 for (sc = ppp_softc; i < NPPP; sc++) {
238 if_initname(&(sc->sc_if), "ppp", i++);
239 sc->sc_if.if_mtu = PPP_MTU;
240 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
241 sc->sc_if.if_type = IFT_PPP;
242 sc->sc_if.if_hdrlen = PPP_HDRLEN;
243 sc->sc_if.if_ioctl = pppsioctl;
244 sc->sc_if.if_output = pppoutput;
245 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
246 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
247 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
248 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
249 if_attach(&sc->sc_if);
250 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
252 netisr_register(NETISR_PPP, cpu0_portfn, pppintr);
254 * XXX layering violation - if_ppp can work over any lower level
255 * transport that cares to attach to it.
257 pppasyncattach(dummy);
261 * Allocate a ppp interface unit and initialize it.
264 pppalloc(struct thread *td)
267 struct ppp_softc *sc;
269 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
270 if (sc->sc_xfer == td) {
274 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
275 if (sc->sc_devp == NULL)
281 sc->sc_mru = PPP_MRU;
282 sc->sc_relinq = NULL;
283 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
285 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
287 sl_compress_init(sc->sc_comp, -1);
290 sc->sc_xc_state = NULL;
291 sc->sc_rc_state = NULL;
292 #endif /* PPP_COMPRESS */
293 for (i = 0; i < NUM_NP; ++i)
294 sc->sc_npmode[i] = NPMODE_ERROR;
295 sc->sc_npqueue = NULL;
296 sc->sc_npqtail = &sc->sc_npqueue;
297 sc->sc_last_sent = sc->sc_last_recv = time_second;
303 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
307 struct ppp_softc *sc;
312 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
313 getmicrotime(&sc->sc_if.if_lastchange);
317 IF_DEQUEUE(&sc->sc_rawq, m);
323 IF_DEQUEUE(&sc->sc_inq, m);
329 IF_DEQUEUE(&sc->sc_fastq, m);
334 while ((m = sc->sc_npqueue) != NULL) {
335 sc->sc_npqueue = m->m_nextpkt;
340 sc->sc_xc_state = NULL;
341 sc->sc_rc_state = NULL;
342 #endif /* PPP_COMPRESS */
344 if (sc->sc_pass_filt.bf_insns != 0) {
345 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
346 sc->sc_pass_filt.bf_insns = 0;
347 sc->sc_pass_filt.bf_len = 0;
349 if (sc->sc_active_filt.bf_insns != 0) {
350 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
351 sc->sc_active_filt.bf_insns = 0;
352 sc->sc_active_filt.bf_len = 0;
354 #endif /* PPP_FILTER */
356 if (sc->sc_comp != 0) {
357 free(sc->sc_comp, M_DEVBUF);
364 * Ioctl routine for generic ppp devices.
367 pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
368 int flag, struct thread *td)
370 int s, error, flags, mru, npx;
372 struct ppp_option_data *odp;
373 struct compressor **cp;
377 struct bpf_program *bp, *nbp;
378 struct bpf_insn *newcode, *oldcode;
380 #endif /* PPP_FILTER */
382 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
387 *(int *)data = sc->sc_inq.ifq_len;
391 *(int *)data = sc->sc_if.if_dunit;
395 *(u_int *)data = sc->sc_flags;
399 if ((error = suser(td)) != 0)
401 flags = *(int *)data & SC_MASK;
404 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
408 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
413 if ((error = suser(td)) != 0)
416 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
421 *(int *)data = sc->sc_mru;
426 if ((error = suser(td)) != 0)
430 sl_compress_init(sc->sc_comp, *(int *)data);
437 if ((error = suser(td)) != 0)
443 case PPPIOCSCOMPRESS:
444 if ((error = suser(td)) != 0)
446 odp = (struct ppp_option_data *) data;
448 if (nb > sizeof(ccp_option))
449 nb = sizeof(ccp_option);
450 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
452 if (ccp_option[1] < 2) /* preliminary check on the length byte */
454 for (cp = ppp_compressors; *cp != NULL; ++cp)
455 if ((*cp)->compress_proto == ccp_option[0]) {
457 * Found a handler for the protocol - try to allocate
458 * a compressor or decompressor.
463 if (sc->sc_xc_state != NULL)
464 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
466 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
467 if (sc->sc_xc_state == NULL) {
468 if (sc->sc_flags & SC_DEBUG)
469 printf("%s: comp_alloc failed\n",
474 sc->sc_flags &= ~SC_COMP_RUN;
478 if (sc->sc_rc_state != NULL)
479 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
481 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
482 if (sc->sc_rc_state == NULL) {
483 if (sc->sc_flags & SC_DEBUG)
484 printf("%s: decomp_alloc failed\n",
489 sc->sc_flags &= ~SC_DECOMP_RUN;
494 if (sc->sc_flags & SC_DEBUG)
495 printf("%s: no compressor for [%x %x %x], %x\n",
496 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
498 return (EINVAL); /* no handler found */
499 #endif /* PPP_COMPRESS */
503 npi = (struct npioctl *) data;
504 switch (npi->protocol) {
511 if (cmd == PPPIOCGNPMODE) {
512 npi->mode = sc->sc_npmode[npx];
514 if ((error = suser(td)) != 0)
516 if (npi->mode != sc->sc_npmode[npx]) {
518 sc->sc_npmode[npx] = npi->mode;
519 if (npi->mode != NPMODE_QUEUE) {
531 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
532 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
539 nbp = (struct bpf_program *) data;
540 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
542 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
543 if (newcodelen != 0) {
544 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
546 return EINVAL; /* or sumpin */
548 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
550 free(newcode, M_DEVBUF);
553 if (!bpf_validate(newcode, nbp->bf_len)) {
554 free(newcode, M_DEVBUF);
559 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
560 oldcode = bp->bf_insns;
562 bp->bf_len = nbp->bf_len;
563 bp->bf_insns = newcode;
566 free(oldcode, M_DEVBUF);
577 * Process an ioctl request to the ppp network interface.
580 pppsioctl(ifp, cmd, data, cr)
586 struct thread *td = curthread; /* XXX */
587 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
588 struct ifaddr *ifa = (struct ifaddr *)data;
589 struct ifreq *ifr = (struct ifreq *)data;
590 struct ppp_stats *psp;
592 struct ppp_comp_stats *pcp;
594 int s = splimp(), error = 0;
598 if ((ifp->if_flags & IFF_RUNNING) == 0)
599 ifp->if_flags &= ~IFF_UP;
604 switch(ifa->ifa_addr->sa_family) {
614 error = EAFNOSUPPORT;
620 switch(ifa->ifa_addr->sa_family) {
630 error = EAFNOSUPPORT;
636 if ((error = suser(td)) != 0)
638 if (ifr->ifr_mtu > PPP_MAXMTU)
641 sc->sc_if.if_mtu = ifr->ifr_mtu;
643 (*sc->sc_setmtu)(sc);
648 ifr->ifr_mtu = sc->sc_if.if_mtu;
654 error = EAFNOSUPPORT;
657 switch(ifr->ifr_addr.sa_family) {
663 error = EAFNOSUPPORT;
669 psp = &((struct ifpppstatsreq *) data)->stats;
670 bzero(psp, sizeof(*psp));
671 psp->p = sc->sc_stats;
672 #if defined(VJC) && !defined(SL_NO_STATS)
674 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
675 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
676 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
677 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
678 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
679 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
680 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
681 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
688 pcp = &((struct ifpppcstatsreq *) data)->stats;
689 bzero(pcp, sizeof(*pcp));
690 if (sc->sc_xc_state != NULL)
691 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
692 if (sc->sc_rc_state != NULL)
693 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
695 #endif /* PPP_COMPRESS */
705 * Queue a packet. Start transmission if not active.
706 * Packet is placed in Information field of PPP frame.
707 * Called at splnet as the if->if_output handler.
708 * Called at splnet from pppwrite().
711 pppoutput(ifp, m0, dst, rtp)
714 struct sockaddr *dst;
717 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
718 int protocol, address, control;
727 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
728 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
729 error = ENETDOWN; /* sort of */
734 * Compute PPP header.
736 m0->m_flags &= ~M_HIGHPRI;
737 switch (dst->sa_family) {
740 address = PPP_ALLSTATIONS;
743 mode = sc->sc_npmode[NP_IP];
746 * If this packet has the "low delay" bit set in the IP header,
747 * put it on the fastq instead.
749 ip = mtod(m0, struct ip *);
750 if (ip->ip_tos & IPTOS_LOWDELAY)
751 m0->m_flags |= M_HIGHPRI;
757 * This is pretty bogus.. We dont have an ipxcp module in pppd
758 * yet to configure the link parameters. Sigh. I guess a
759 * manual ifconfig would do.... -Peter
761 address = PPP_ALLSTATIONS;
768 address = PPP_ADDRESS(dst->sa_data);
769 control = PPP_CONTROL(dst->sa_data);
770 protocol = PPP_PROTOCOL(dst->sa_data);
774 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
775 error = EAFNOSUPPORT;
780 * Drop this packet, or return an error, if necessary.
782 if (mode == NPMODE_ERROR) {
786 if (mode == NPMODE_DROP) {
792 * Add PPP header. If no space in first mbuf, allocate another.
793 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
795 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
796 m0 = m_prepend(m0, PPP_HDRLEN, MB_DONTWAIT);
803 m0->m_data -= PPP_HDRLEN;
805 cp = mtod(m0, u_char *);
808 *cp++ = protocol >> 8;
809 *cp++ = protocol & 0xff;
810 m0->m_len += PPP_HDRLEN;
813 for (m = m0; m != 0; m = m->m_next)
816 if (sc->sc_flags & SC_LOG_OUTPKT) {
817 printf("%s output: ", ifp->if_xname);
821 if ((protocol & 0x8000) == 0) {
824 * Apply the pass and active filters to the packet,
825 * but only if it is a data packet.
827 *mtod(m0, u_char *) = 1; /* indicates outbound */
828 if (sc->sc_pass_filt.bf_insns != 0
829 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
831 error = 0; /* drop this packet */
836 * Update the time we sent the most recent packet.
838 if (sc->sc_active_filt.bf_insns == 0
839 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
840 sc->sc_last_sent = time_second;
842 *mtod(m0, u_char *) = address;
845 * Update the time we sent the most recent data packet.
847 sc->sc_last_sent = time_second;
848 #endif /* PPP_FILTER */
852 * See if bpf wants to look at the packet.
858 * Put the packet on the appropriate queue.
860 s = splsoftnet(); /* redundant */
861 if (mode == NPMODE_QUEUE) {
862 /* XXX we should limit the number of packets on this queue */
863 *sc->sc_npqtail = m0;
864 m0->m_nextpkt = NULL;
865 sc->sc_npqtail = &m0->m_nextpkt;
867 /* fastq and if_snd are emptied at spl[soft]net now */
868 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
869 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
872 sc->sc_if.if_oerrors++;
873 sc->sc_stats.ppp_oerrors++;
880 getmicrotime(&ifp->if_lastchange);
882 ifp->if_obytes += len;
893 * After a change in the NPmode for some NP, move packets from the
894 * npqueue to the send queue or the fast queue as appropriate.
895 * Should be called at spl[soft]net.
899 struct ppp_softc *sc;
901 struct mbuf *m, **mpp;
905 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
906 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
908 mode = sc->sc_npmode[NP_IP];
917 * This packet can now go on one of the queues to be sent.
921 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
924 sc->sc_if.if_oerrors++;
925 sc->sc_stats.ppp_oerrors++;
941 sc->sc_npqtail = mpp;
945 * Transmitter has finished outputting some stuff;
946 * remember to call sc->sc_start later at splsoftnet.
950 struct ppp_softc *sc;
954 sc->sc_flags &= ~SC_TBUSY;
955 schednetisr(NETISR_PPP);
961 * Get a packet to send. This procedure is intended to be called at
962 * splsoftnet, since it may involve time-consuming operations such as
963 * applying VJ compression, packet compression, address/control and/or
964 * protocol field compression to the packet.
968 struct ppp_softc *sc;
972 int address, control, protocol;
975 * Grab a packet to send: first try the fast queue, then the
978 IF_DEQUEUE(&sc->sc_fastq, m);
980 IF_DEQUEUE(&sc->sc_if.if_snd, m);
984 ++sc->sc_stats.ppp_opackets;
987 * Extract the ppp header of the new packet.
988 * The ppp header will be in one mbuf.
990 cp = mtod(m, u_char *);
991 address = PPP_ADDRESS(cp);
992 control = PPP_CONTROL(cp);
993 protocol = PPP_PROTOCOL(cp);
999 * If the packet is a TCP/IP packet, see if we can compress it.
1001 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1006 ip = (struct ip *) (cp + PPP_HDRLEN);
1007 if (mp->m_len <= PPP_HDRLEN) {
1011 ip = mtod(mp, struct ip *);
1013 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1014 if (ip->ip_p == IPPROTO_TCP) {
1015 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1016 !(sc->sc_flags & SC_NO_TCP_CCID));
1018 case TYPE_UNCOMPRESSED_TCP:
1019 protocol = PPP_VJC_UNCOMP;
1021 case TYPE_COMPRESSED_TCP:
1022 protocol = PPP_VJC_COMP;
1023 cp = mtod(m, u_char *);
1024 cp[0] = address; /* header has moved */
1029 cp[3] = protocol; /* update protocol in PPP header */
1039 #endif /* PPP_COMPRESS */
1043 if (protocol != PPP_LCP && protocol != PPP_CCP
1044 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1045 struct mbuf *mcomp = NULL;
1049 for (mp = m; mp != NULL; mp = mp->m_next)
1051 clen = (*sc->sc_xcomp->compress)
1052 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1053 if (mcomp != NULL) {
1054 if (sc->sc_flags & SC_CCP_UP) {
1055 /* Send the compressed packet instead of the original. */
1058 cp = mtod(m, u_char *);
1061 /* Can't transmit compressed packets until CCP is up. */
1066 #endif /* PPP_COMPRESS */
1069 * Compress the address/control and protocol, if possible.
1071 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1072 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1073 protocol != PPP_LCP) {
1074 /* can compress address/control */
1078 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1079 /* can compress protocol */
1080 if (mtod(m, u_char *) == cp) {
1081 cp[2] = cp[1]; /* move address/control up */
1093 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1094 * 0 if it is about to be transmitted.
1097 ppp_ccp(sc, m, rcvd)
1098 struct ppp_softc *sc;
1107 * Get a pointer to the data after the PPP header.
1109 if (m->m_len <= PPP_HDRLEN) {
1113 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1116 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1119 ep = mtod(mp, u_char *) + mp->m_len;
1120 if (dp + CCP_HDRLEN > ep)
1122 slen = CCP_LENGTH(dp);
1123 if (dp + slen > ep) {
1124 if (sc->sc_flags & SC_DEBUG)
1125 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1126 dp, slen, mtod(mp, u_char *), mp->m_len);
1130 switch (CCP_CODE(dp)) {
1134 /* CCP must be going down - disable compression */
1135 if (sc->sc_flags & SC_CCP_UP) {
1137 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1143 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1144 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1145 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1147 /* we're agreeing to send compressed packets. */
1148 if (sc->sc_xc_state != NULL
1149 && (*sc->sc_xcomp->comp_init)
1150 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1151 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1153 sc->sc_flags |= SC_COMP_RUN;
1157 /* peer is agreeing to send compressed packets. */
1158 if (sc->sc_rc_state != NULL
1159 && (*sc->sc_rcomp->decomp_init)
1160 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1161 sc->sc_if.if_dunit, 0, sc->sc_mru,
1162 sc->sc_flags & SC_DEBUG)) {
1164 sc->sc_flags |= SC_DECOMP_RUN;
1165 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1173 if (sc->sc_flags & SC_CCP_UP) {
1175 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1176 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1178 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1179 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1181 sc->sc_flags &= ~SC_DC_ERROR;
1191 * CCP is down; free (de)compressor state if necessary.
1195 struct ppp_softc *sc;
1197 if (sc->sc_xc_state) {
1198 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1199 sc->sc_xc_state = NULL;
1201 if (sc->sc_rc_state) {
1202 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1203 sc->sc_rc_state = NULL;
1206 #endif /* PPP_COMPRESS */
1209 * PPP packet input routine.
1210 * The caller has checked and removed the FCS and has inserted
1211 * the address/control bytes and the protocol high byte if they
1215 ppppktin(sc, m, lost)
1216 struct ppp_softc *sc;
1223 m->m_flags |= M_ERRMARK;
1224 IF_ENQUEUE(&sc->sc_rawq, m);
1225 schednetisr(NETISR_PPP);
1230 * Process a received PPP packet, doing decompression as necessary.
1231 * Should be called at splsoftnet.
1233 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1234 TYPE_UNCOMPRESSED_TCP)
1238 struct ppp_softc *sc;
1241 struct ifnet *ifp = &sc->sc_if;
1243 int s, ilen = 0, xlen, proto, rv;
1244 u_char *cp, adrs, ctrl;
1245 struct mbuf *mp, *dmp = NULL;
1249 sc->sc_stats.ppp_ipackets++;
1251 if (sc->sc_flags & SC_LOG_INPKT) {
1253 for (mp = m; mp != NULL; mp = mp->m_next)
1255 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1259 cp = mtod(m, u_char *);
1260 adrs = PPP_ADDRESS(cp);
1261 ctrl = PPP_CONTROL(cp);
1262 proto = PPP_PROTOCOL(cp);
1264 if (m->m_flags & M_ERRMARK) {
1265 m->m_flags &= ~M_ERRMARK;
1267 sc->sc_flags |= SC_VJ_RESET;
1273 * Decompress this packet if necessary, update the receiver's
1274 * dictionary, or take appropriate action on a CCP packet.
1276 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1277 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1278 /* decompress this packet */
1279 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1280 if (rv == DECOMP_OK) {
1283 /* no error, but no decompressed packet produced */
1287 cp = mtod(m, u_char *);
1288 proto = PPP_PROTOCOL(cp);
1292 * An error has occurred in decompression.
1293 * Pass the compressed packet up to pppd, which may take
1294 * CCP down or issue a Reset-Req.
1296 if (sc->sc_flags & SC_DEBUG)
1297 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1299 sc->sc_flags |= SC_VJ_RESET;
1300 if (rv == DECOMP_ERROR)
1301 sc->sc_flags |= SC_DC_ERROR;
1303 sc->sc_flags |= SC_DC_FERROR;
1308 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1309 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1311 if (proto == PPP_CCP) {
1318 for (mp = m; mp != NULL; mp = mp->m_next)
1322 if (sc->sc_flags & SC_VJ_RESET) {
1324 * If we've missed a packet, we must toss subsequent compressed
1325 * packets which don't have an explicit connection ID.
1328 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1330 sc->sc_flags &= ~SC_VJ_RESET;
1335 * See if we have a VJ-compressed packet to uncompress.
1337 if (proto == PPP_VJC_COMP) {
1338 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1341 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1342 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1343 sc->sc_comp, &iphdr, &hlen);
1346 if (sc->sc_flags & SC_DEBUG)
1347 printf("%s: VJ uncompress failed on type comp\n",
1352 /* Copy the PPP and IP headers into a new mbuf. */
1353 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1358 if (hlen + PPP_HDRLEN > MHLEN) {
1359 MCLGET(mp, MB_DONTWAIT);
1360 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1362 goto bad; /* lose if big headers and no clusters */
1365 cp = mtod(mp, u_char *);
1371 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1372 mp->m_len = hlen + PPP_HDRLEN;
1375 * Trim the PPP and VJ headers off the old mbuf
1376 * and stick the new and old mbufs together.
1378 m->m_data += PPP_HDRLEN + xlen;
1379 m->m_len -= PPP_HDRLEN + xlen;
1380 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1381 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1382 mp->m_len += m->m_len;
1383 mp->m_next = m_free(m);
1388 ilen += hlen - xlen;
1390 } else if (proto == PPP_VJC_UNCOMP) {
1391 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1394 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1395 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1396 sc->sc_comp, &iphdr, &hlen);
1399 if (sc->sc_flags & SC_DEBUG)
1400 printf("%s: VJ uncompress failed on type uncomp\n",
1411 * If the packet will fit in a header mbuf, don't waste a
1412 * whole cluster on it.
1414 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1415 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1417 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1423 m->m_pkthdr.len = ilen;
1424 m->m_pkthdr.rcvif = ifp;
1426 if ((proto & 0x8000) == 0) {
1429 * See whether we want to pass this packet, and
1430 * if it counts as link activity.
1432 adrs = *mtod(m, u_char *); /* save address field */
1433 *mtod(m, u_char *) = 0; /* indicate inbound */
1434 if (sc->sc_pass_filt.bf_insns != 0
1435 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1437 /* drop this packet */
1441 if (sc->sc_active_filt.bf_insns == 0
1442 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1443 sc->sc_last_recv = time_second;
1445 *mtod(m, u_char *) = adrs;
1448 * Record the time that we received this packet.
1450 sc->sc_last_recv = time_second;
1451 #endif /* PPP_FILTER */
1454 /* See if bpf wants to look at the packet. */
1455 if (sc->sc_if.if_bpf)
1456 bpf_mtap(&sc->sc_if, m);
1463 * IP packet - take off the ppp header and pass it up to IP.
1465 if ((ifp->if_flags & IFF_UP) == 0
1466 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1467 /* interface is down - drop the packet. */
1471 m->m_pkthdr.len -= PPP_HDRLEN;
1472 m->m_data += PPP_HDRLEN;
1473 m->m_len -= PPP_HDRLEN;
1474 if (ipflow_fastforward(m))
1482 * IPX packet - take off the ppp header and pass it up to IPX.
1484 if ((sc->sc_if.if_flags & IFF_UP) == 0
1485 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1486 /* interface is down - drop the packet. */
1490 m->m_pkthdr.len -= PPP_HDRLEN;
1491 m->m_data += PPP_HDRLEN;
1492 m->m_len -= PPP_HDRLEN;
1494 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1500 * Some other protocol - place on input queue for read().
1506 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1508 rv = (netisr_queue(isr, m) == 0);
1510 if (sc->sc_flags & SC_DEBUG)
1511 printf("%s: input queue full\n", ifp->if_xname);
1517 ifp->if_ibytes += ilen;
1518 getmicrotime(&ifp->if_lastchange);
1527 sc->sc_if.if_ierrors++;
1528 sc->sc_stats.ppp_ierrors++;
1531 #define MAX_DUMP_BYTES 128
1537 char buf[3*MAX_DUMP_BYTES+4];
1541 for (m = m0; m; m = m->m_next) {
1543 u_char *rptr = (u_char *)m->m_data;
1546 if (bp > buf + sizeof(buf) - 4)
1548 *bp++ = hex2ascii(*rptr >> 4);
1549 *bp++ = hex2ascii(*rptr++ & 0xf);
1553 if (bp > buf + sizeof(buf) - 3)
1563 printf("%s\n", buf);