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.22 2005/01/23 20:23:22 joerg 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 callout_init(&sc->sc_timeout);
250 if_attach(&sc->sc_if);
251 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
253 netisr_register(NETISR_PPP, cpu0_portfn, pppintr);
255 * XXX layering violation - if_ppp can work over any lower level
256 * transport that cares to attach to it.
258 pppasyncattach(dummy);
262 * Allocate a ppp interface unit and initialize it.
265 pppalloc(struct thread *td)
268 struct ppp_softc *sc;
270 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
271 if (sc->sc_xfer == td) {
275 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
276 if (sc->sc_devp == NULL)
282 sc->sc_mru = PPP_MRU;
283 sc->sc_relinq = NULL;
284 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
286 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
288 sl_compress_init(sc->sc_comp, -1);
291 sc->sc_xc_state = NULL;
292 sc->sc_rc_state = NULL;
293 #endif /* PPP_COMPRESS */
294 for (i = 0; i < NUM_NP; ++i)
295 sc->sc_npmode[i] = NPMODE_ERROR;
296 sc->sc_npqueue = NULL;
297 sc->sc_npqtail = &sc->sc_npqueue;
298 sc->sc_last_sent = sc->sc_last_recv = time_second;
304 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
308 struct ppp_softc *sc;
313 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
314 getmicrotime(&sc->sc_if.if_lastchange);
318 IF_DEQUEUE(&sc->sc_rawq, m);
324 IF_DEQUEUE(&sc->sc_inq, m);
330 IF_DEQUEUE(&sc->sc_fastq, m);
335 while ((m = sc->sc_npqueue) != NULL) {
336 sc->sc_npqueue = m->m_nextpkt;
341 sc->sc_xc_state = NULL;
342 sc->sc_rc_state = NULL;
343 #endif /* PPP_COMPRESS */
345 if (sc->sc_pass_filt.bf_insns != 0) {
346 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
347 sc->sc_pass_filt.bf_insns = 0;
348 sc->sc_pass_filt.bf_len = 0;
350 if (sc->sc_active_filt.bf_insns != 0) {
351 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
352 sc->sc_active_filt.bf_insns = 0;
353 sc->sc_active_filt.bf_len = 0;
355 #endif /* PPP_FILTER */
357 if (sc->sc_comp != 0) {
358 free(sc->sc_comp, M_DEVBUF);
365 * Ioctl routine for generic ppp devices.
368 pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
369 int flag, struct thread *td)
371 int s, error, flags, mru, npx;
373 struct ppp_option_data *odp;
374 struct compressor **cp;
378 struct bpf_program *bp, *nbp;
379 struct bpf_insn *newcode, *oldcode;
381 #endif /* PPP_FILTER */
383 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
388 *(int *)data = sc->sc_inq.ifq_len;
392 *(int *)data = sc->sc_if.if_dunit;
396 *(u_int *)data = sc->sc_flags;
400 if ((error = suser(td)) != 0)
402 flags = *(int *)data & SC_MASK;
405 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
409 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
414 if ((error = suser(td)) != 0)
417 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
422 *(int *)data = sc->sc_mru;
427 if ((error = suser(td)) != 0)
431 sl_compress_init(sc->sc_comp, *(int *)data);
438 if ((error = suser(td)) != 0)
444 case PPPIOCSCOMPRESS:
445 if ((error = suser(td)) != 0)
447 odp = (struct ppp_option_data *) data;
449 if (nb > sizeof(ccp_option))
450 nb = sizeof(ccp_option);
451 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
453 if (ccp_option[1] < 2) /* preliminary check on the length byte */
455 for (cp = ppp_compressors; *cp != NULL; ++cp)
456 if ((*cp)->compress_proto == ccp_option[0]) {
458 * Found a handler for the protocol - try to allocate
459 * a compressor or decompressor.
464 if (sc->sc_xc_state != NULL)
465 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
467 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
468 if (sc->sc_xc_state == NULL) {
469 if (sc->sc_flags & SC_DEBUG)
470 printf("%s: comp_alloc failed\n",
475 sc->sc_flags &= ~SC_COMP_RUN;
479 if (sc->sc_rc_state != NULL)
480 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
482 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
483 if (sc->sc_rc_state == NULL) {
484 if (sc->sc_flags & SC_DEBUG)
485 printf("%s: decomp_alloc failed\n",
490 sc->sc_flags &= ~SC_DECOMP_RUN;
495 if (sc->sc_flags & SC_DEBUG)
496 printf("%s: no compressor for [%x %x %x], %x\n",
497 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
499 return (EINVAL); /* no handler found */
500 #endif /* PPP_COMPRESS */
504 npi = (struct npioctl *) data;
505 switch (npi->protocol) {
512 if (cmd == PPPIOCGNPMODE) {
513 npi->mode = sc->sc_npmode[npx];
515 if ((error = suser(td)) != 0)
517 if (npi->mode != sc->sc_npmode[npx]) {
519 sc->sc_npmode[npx] = npi->mode;
520 if (npi->mode != NPMODE_QUEUE) {
532 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
533 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
540 nbp = (struct bpf_program *) data;
541 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
543 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
544 if (newcodelen != 0) {
545 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
547 return EINVAL; /* or sumpin */
549 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
551 free(newcode, M_DEVBUF);
554 if (!bpf_validate(newcode, nbp->bf_len)) {
555 free(newcode, M_DEVBUF);
560 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
561 oldcode = bp->bf_insns;
563 bp->bf_len = nbp->bf_len;
564 bp->bf_insns = newcode;
567 free(oldcode, M_DEVBUF);
578 * Process an ioctl request to the ppp network interface.
581 pppsioctl(ifp, cmd, data, cr)
587 struct thread *td = curthread; /* XXX */
588 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
589 struct ifaddr *ifa = (struct ifaddr *)data;
590 struct ifreq *ifr = (struct ifreq *)data;
591 struct ppp_stats *psp;
593 struct ppp_comp_stats *pcp;
595 int s = splimp(), error = 0;
599 if ((ifp->if_flags & IFF_RUNNING) == 0)
600 ifp->if_flags &= ~IFF_UP;
605 switch(ifa->ifa_addr->sa_family) {
615 error = EAFNOSUPPORT;
621 switch(ifa->ifa_addr->sa_family) {
631 error = EAFNOSUPPORT;
637 if ((error = suser(td)) != 0)
639 if (ifr->ifr_mtu > PPP_MAXMTU)
642 sc->sc_if.if_mtu = ifr->ifr_mtu;
644 (*sc->sc_setmtu)(sc);
649 ifr->ifr_mtu = sc->sc_if.if_mtu;
655 error = EAFNOSUPPORT;
658 switch(ifr->ifr_addr.sa_family) {
664 error = EAFNOSUPPORT;
670 psp = &((struct ifpppstatsreq *) data)->stats;
671 bzero(psp, sizeof(*psp));
672 psp->p = sc->sc_stats;
673 #if defined(VJC) && !defined(SL_NO_STATS)
675 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
676 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
677 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
678 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
679 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
680 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
681 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
682 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
689 pcp = &((struct ifpppcstatsreq *) data)->stats;
690 bzero(pcp, sizeof(*pcp));
691 if (sc->sc_xc_state != NULL)
692 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
693 if (sc->sc_rc_state != NULL)
694 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
696 #endif /* PPP_COMPRESS */
706 * Queue a packet. Start transmission if not active.
707 * Packet is placed in Information field of PPP frame.
708 * Called at splnet as the if->if_output handler.
709 * Called at splnet from pppwrite().
712 pppoutput(ifp, m0, dst, rtp)
715 struct sockaddr *dst;
718 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
719 int protocol, address, control;
728 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
729 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
730 error = ENETDOWN; /* sort of */
735 * Compute PPP header.
737 m0->m_flags &= ~M_HIGHPRI;
738 switch (dst->sa_family) {
741 address = PPP_ALLSTATIONS;
744 mode = sc->sc_npmode[NP_IP];
747 * If this packet has the "low delay" bit set in the IP header,
748 * put it on the fastq instead.
750 ip = mtod(m0, struct ip *);
751 if (ip->ip_tos & IPTOS_LOWDELAY)
752 m0->m_flags |= M_HIGHPRI;
758 * This is pretty bogus.. We dont have an ipxcp module in pppd
759 * yet to configure the link parameters. Sigh. I guess a
760 * manual ifconfig would do.... -Peter
762 address = PPP_ALLSTATIONS;
769 address = PPP_ADDRESS(dst->sa_data);
770 control = PPP_CONTROL(dst->sa_data);
771 protocol = PPP_PROTOCOL(dst->sa_data);
775 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
776 error = EAFNOSUPPORT;
781 * Drop this packet, or return an error, if necessary.
783 if (mode == NPMODE_ERROR) {
787 if (mode == NPMODE_DROP) {
793 * Add PPP header. If no space in first mbuf, allocate another.
794 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
796 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
797 m0 = m_prepend(m0, PPP_HDRLEN, MB_DONTWAIT);
804 m0->m_data -= PPP_HDRLEN;
806 cp = mtod(m0, u_char *);
809 *cp++ = protocol >> 8;
810 *cp++ = protocol & 0xff;
811 m0->m_len += PPP_HDRLEN;
814 for (m = m0; m != 0; m = m->m_next)
817 if (sc->sc_flags & SC_LOG_OUTPKT) {
818 printf("%s output: ", ifp->if_xname);
822 if ((protocol & 0x8000) == 0) {
825 * Apply the pass and active filters to the packet,
826 * but only if it is a data packet.
828 *mtod(m0, u_char *) = 1; /* indicates outbound */
829 if (sc->sc_pass_filt.bf_insns != 0
830 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
832 error = 0; /* drop this packet */
837 * Update the time we sent the most recent packet.
839 if (sc->sc_active_filt.bf_insns == 0
840 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
841 sc->sc_last_sent = time_second;
843 *mtod(m0, u_char *) = address;
846 * Update the time we sent the most recent data packet.
848 sc->sc_last_sent = time_second;
849 #endif /* PPP_FILTER */
853 * See if bpf wants to look at the packet.
859 * Put the packet on the appropriate queue.
861 s = splsoftnet(); /* redundant */
862 if (mode == NPMODE_QUEUE) {
863 /* XXX we should limit the number of packets on this queue */
864 *sc->sc_npqtail = m0;
865 m0->m_nextpkt = NULL;
866 sc->sc_npqtail = &m0->m_nextpkt;
868 /* fastq and if_snd are emptied at spl[soft]net now */
869 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
870 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
873 sc->sc_if.if_oerrors++;
874 sc->sc_stats.ppp_oerrors++;
881 getmicrotime(&ifp->if_lastchange);
883 ifp->if_obytes += len;
894 * After a change in the NPmode for some NP, move packets from the
895 * npqueue to the send queue or the fast queue as appropriate.
896 * Should be called at spl[soft]net.
900 struct ppp_softc *sc;
902 struct mbuf *m, **mpp;
906 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
907 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
909 mode = sc->sc_npmode[NP_IP];
918 * This packet can now go on one of the queues to be sent.
922 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
925 sc->sc_if.if_oerrors++;
926 sc->sc_stats.ppp_oerrors++;
942 sc->sc_npqtail = mpp;
946 * Transmitter has finished outputting some stuff;
947 * remember to call sc->sc_start later at splsoftnet.
951 struct ppp_softc *sc;
955 sc->sc_flags &= ~SC_TBUSY;
956 schednetisr(NETISR_PPP);
962 * Get a packet to send. This procedure is intended to be called at
963 * splsoftnet, since it may involve time-consuming operations such as
964 * applying VJ compression, packet compression, address/control and/or
965 * protocol field compression to the packet.
969 struct ppp_softc *sc;
973 int address, control, protocol;
976 * Grab a packet to send: first try the fast queue, then the
979 IF_DEQUEUE(&sc->sc_fastq, m);
981 IF_DEQUEUE(&sc->sc_if.if_snd, m);
985 ++sc->sc_stats.ppp_opackets;
988 * Extract the ppp header of the new packet.
989 * The ppp header will be in one mbuf.
991 cp = mtod(m, u_char *);
992 address = PPP_ADDRESS(cp);
993 control = PPP_CONTROL(cp);
994 protocol = PPP_PROTOCOL(cp);
1000 * If the packet is a TCP/IP packet, see if we can compress it.
1002 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1007 ip = (struct ip *) (cp + PPP_HDRLEN);
1008 if (mp->m_len <= PPP_HDRLEN) {
1012 ip = mtod(mp, struct ip *);
1014 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1015 if (ip->ip_p == IPPROTO_TCP) {
1016 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1017 !(sc->sc_flags & SC_NO_TCP_CCID));
1019 case TYPE_UNCOMPRESSED_TCP:
1020 protocol = PPP_VJC_UNCOMP;
1022 case TYPE_COMPRESSED_TCP:
1023 protocol = PPP_VJC_COMP;
1024 cp = mtod(m, u_char *);
1025 cp[0] = address; /* header has moved */
1030 cp[3] = protocol; /* update protocol in PPP header */
1040 #endif /* PPP_COMPRESS */
1044 if (protocol != PPP_LCP && protocol != PPP_CCP
1045 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1046 struct mbuf *mcomp = NULL;
1050 for (mp = m; mp != NULL; mp = mp->m_next)
1052 clen = (*sc->sc_xcomp->compress)
1053 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1054 if (mcomp != NULL) {
1055 if (sc->sc_flags & SC_CCP_UP) {
1056 /* Send the compressed packet instead of the original. */
1059 cp = mtod(m, u_char *);
1062 /* Can't transmit compressed packets until CCP is up. */
1067 #endif /* PPP_COMPRESS */
1070 * Compress the address/control and protocol, if possible.
1072 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1073 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1074 protocol != PPP_LCP) {
1075 /* can compress address/control */
1079 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1080 /* can compress protocol */
1081 if (mtod(m, u_char *) == cp) {
1082 cp[2] = cp[1]; /* move address/control up */
1094 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1095 * 0 if it is about to be transmitted.
1098 ppp_ccp(sc, m, rcvd)
1099 struct ppp_softc *sc;
1108 * Get a pointer to the data after the PPP header.
1110 if (m->m_len <= PPP_HDRLEN) {
1114 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1117 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1120 ep = mtod(mp, u_char *) + mp->m_len;
1121 if (dp + CCP_HDRLEN > ep)
1123 slen = CCP_LENGTH(dp);
1124 if (dp + slen > ep) {
1125 if (sc->sc_flags & SC_DEBUG)
1126 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1127 dp, slen, mtod(mp, u_char *), mp->m_len);
1131 switch (CCP_CODE(dp)) {
1135 /* CCP must be going down - disable compression */
1136 if (sc->sc_flags & SC_CCP_UP) {
1138 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1144 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1145 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1146 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1148 /* we're agreeing to send compressed packets. */
1149 if (sc->sc_xc_state != NULL
1150 && (*sc->sc_xcomp->comp_init)
1151 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1152 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1154 sc->sc_flags |= SC_COMP_RUN;
1158 /* peer is agreeing to send compressed packets. */
1159 if (sc->sc_rc_state != NULL
1160 && (*sc->sc_rcomp->decomp_init)
1161 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1162 sc->sc_if.if_dunit, 0, sc->sc_mru,
1163 sc->sc_flags & SC_DEBUG)) {
1165 sc->sc_flags |= SC_DECOMP_RUN;
1166 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1174 if (sc->sc_flags & SC_CCP_UP) {
1176 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1177 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1179 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1180 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1182 sc->sc_flags &= ~SC_DC_ERROR;
1192 * CCP is down; free (de)compressor state if necessary.
1196 struct ppp_softc *sc;
1198 if (sc->sc_xc_state) {
1199 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1200 sc->sc_xc_state = NULL;
1202 if (sc->sc_rc_state) {
1203 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1204 sc->sc_rc_state = NULL;
1207 #endif /* PPP_COMPRESS */
1210 * PPP packet input routine.
1211 * The caller has checked and removed the FCS and has inserted
1212 * the address/control bytes and the protocol high byte if they
1216 ppppktin(sc, m, lost)
1217 struct ppp_softc *sc;
1224 m->m_flags |= M_ERRMARK;
1225 IF_ENQUEUE(&sc->sc_rawq, m);
1226 schednetisr(NETISR_PPP);
1231 * Process a received PPP packet, doing decompression as necessary.
1232 * Should be called at splsoftnet.
1234 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1235 TYPE_UNCOMPRESSED_TCP)
1239 struct ppp_softc *sc;
1242 struct ifnet *ifp = &sc->sc_if;
1244 int s, ilen = 0, xlen, proto, rv;
1245 u_char *cp, adrs, ctrl;
1246 struct mbuf *mp, *dmp = NULL;
1250 sc->sc_stats.ppp_ipackets++;
1252 if (sc->sc_flags & SC_LOG_INPKT) {
1254 for (mp = m; mp != NULL; mp = mp->m_next)
1256 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1260 cp = mtod(m, u_char *);
1261 adrs = PPP_ADDRESS(cp);
1262 ctrl = PPP_CONTROL(cp);
1263 proto = PPP_PROTOCOL(cp);
1265 if (m->m_flags & M_ERRMARK) {
1266 m->m_flags &= ~M_ERRMARK;
1268 sc->sc_flags |= SC_VJ_RESET;
1274 * Decompress this packet if necessary, update the receiver's
1275 * dictionary, or take appropriate action on a CCP packet.
1277 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1278 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1279 /* decompress this packet */
1280 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1281 if (rv == DECOMP_OK) {
1284 /* no error, but no decompressed packet produced */
1288 cp = mtod(m, u_char *);
1289 proto = PPP_PROTOCOL(cp);
1293 * An error has occurred in decompression.
1294 * Pass the compressed packet up to pppd, which may take
1295 * CCP down or issue a Reset-Req.
1297 if (sc->sc_flags & SC_DEBUG)
1298 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1300 sc->sc_flags |= SC_VJ_RESET;
1301 if (rv == DECOMP_ERROR)
1302 sc->sc_flags |= SC_DC_ERROR;
1304 sc->sc_flags |= SC_DC_FERROR;
1309 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1310 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1312 if (proto == PPP_CCP) {
1319 for (mp = m; mp != NULL; mp = mp->m_next)
1323 if (sc->sc_flags & SC_VJ_RESET) {
1325 * If we've missed a packet, we must toss subsequent compressed
1326 * packets which don't have an explicit connection ID.
1329 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1331 sc->sc_flags &= ~SC_VJ_RESET;
1336 * See if we have a VJ-compressed packet to uncompress.
1338 if (proto == PPP_VJC_COMP) {
1339 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1342 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1343 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1344 sc->sc_comp, &iphdr, &hlen);
1347 if (sc->sc_flags & SC_DEBUG)
1348 printf("%s: VJ uncompress failed on type comp\n",
1353 /* Copy the PPP and IP headers into a new mbuf. */
1354 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1359 if (hlen + PPP_HDRLEN > MHLEN) {
1360 MCLGET(mp, MB_DONTWAIT);
1361 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1363 goto bad; /* lose if big headers and no clusters */
1366 cp = mtod(mp, u_char *);
1372 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1373 mp->m_len = hlen + PPP_HDRLEN;
1376 * Trim the PPP and VJ headers off the old mbuf
1377 * and stick the new and old mbufs together.
1379 m->m_data += PPP_HDRLEN + xlen;
1380 m->m_len -= PPP_HDRLEN + xlen;
1381 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1382 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1383 mp->m_len += m->m_len;
1384 mp->m_next = m_free(m);
1389 ilen += hlen - xlen;
1391 } else if (proto == PPP_VJC_UNCOMP) {
1392 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1395 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1396 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1397 sc->sc_comp, &iphdr, &hlen);
1400 if (sc->sc_flags & SC_DEBUG)
1401 printf("%s: VJ uncompress failed on type uncomp\n",
1412 * If the packet will fit in a header mbuf, don't waste a
1413 * whole cluster on it.
1415 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1416 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1418 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1424 m->m_pkthdr.len = ilen;
1425 m->m_pkthdr.rcvif = ifp;
1427 if ((proto & 0x8000) == 0) {
1430 * See whether we want to pass this packet, and
1431 * if it counts as link activity.
1433 adrs = *mtod(m, u_char *); /* save address field */
1434 *mtod(m, u_char *) = 0; /* indicate inbound */
1435 if (sc->sc_pass_filt.bf_insns != 0
1436 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1438 /* drop this packet */
1442 if (sc->sc_active_filt.bf_insns == 0
1443 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1444 sc->sc_last_recv = time_second;
1446 *mtod(m, u_char *) = adrs;
1449 * Record the time that we received this packet.
1451 sc->sc_last_recv = time_second;
1452 #endif /* PPP_FILTER */
1455 BPF_MTAP(&sc->sc_if, m);
1462 * IP packet - take off the ppp header and pass it up to IP.
1464 if ((ifp->if_flags & IFF_UP) == 0
1465 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1466 /* interface is down - drop the packet. */
1470 m->m_pkthdr.len -= PPP_HDRLEN;
1471 m->m_data += PPP_HDRLEN;
1472 m->m_len -= PPP_HDRLEN;
1473 if (ipflow_fastforward(m))
1481 * IPX packet - take off the ppp header and pass it up to IPX.
1483 if ((sc->sc_if.if_flags & IFF_UP) == 0
1484 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1485 /* interface is down - drop the packet. */
1489 m->m_pkthdr.len -= PPP_HDRLEN;
1490 m->m_data += PPP_HDRLEN;
1491 m->m_len -= PPP_HDRLEN;
1493 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1499 * Some other protocol - place on input queue for read().
1505 * If we found a netproto just pass it to the proto. Otherwise queue
1506 * the packet to the tty (e.g. pppd). Note that sc_ctlp() must be
1507 * called EXACTLY once per packet queued.
1510 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1514 rv = (netisr_queue(isr, m) == 0);
1517 if (sc->sc_flags & SC_DEBUG)
1518 printf("%s: input queue full\n", ifp->if_xname);
1524 ifp->if_ibytes += ilen;
1525 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);