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.41 2008/09/24 14:26:38 sephe 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>
93 #include <sys/socket.h>
94 #include <sys/filio.h>
95 #include <sys/sockio.h>
96 #include <sys/kernel.h>
98 #include <sys/malloc.h>
100 #include <sys/msgport2.h>
103 #include <net/if_types.h>
104 #include <net/ifq_var.h>
105 #include <net/netisr.h>
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip.h>
116 #include <netproto/ipx/ipx.h>
117 #include <netproto/ipx/ipx_if.h>
121 #include <net/slcompress.h>
125 #include "if_pppvar.h"
129 #define splsoftnet splnet
133 #define PACKETPTR struct mbuf *
134 #include <net/ppp_layer/ppp_comp.h>
137 struct ppp_softc ppp_softc[NPPP];
139 /* XXX layering violation */
140 extern void pppasyncattach (void *);
142 static void pppattach (void *);
143 PSEUDO_SET(pppattach, if_ppp);
145 static int pppsioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
148 static void ppp_requeue (struct ppp_softc *);
149 static void ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
150 static void ppp_ccp_closed (struct ppp_softc *);
151 static void ppp_inproc (struct ppp_softc *, struct mbuf *);
152 static void pppdumpm (struct mbuf *m0);
153 static void ppp_ifstart(struct ifnet *ifp);
156 * Some useful mbuf macros not in mbuf.h.
158 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
160 #define M_DATASTART(m) \
161 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
162 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
164 #define M_DATASIZE(m) \
165 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
166 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
169 * We steal two bits in the mbuf m_flags, to mark high-priority packets
170 * for output, and received packets following lost/corrupted packets.
172 #define M_HIGHPRI M_PROTO2 /* output packet for sc_fastq */
173 #define M_ERRMARK M_PROTO3 /* steal a bit in mbuf m_flags */
178 * List of compressors we know about.
179 * We leave some space so maybe we can modload compressors.
182 extern struct compressor ppp_bsd_compress;
183 extern struct compressor ppp_deflate, ppp_deflate_draft;
185 static struct compressor *ppp_compressors[8] = {
186 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
189 #if DO_DEFLATE && defined(PPP_DEFLATE)
195 #endif /* PPP_COMPRESS */
198 * Software interrupt routine, called at spl[soft]net.
201 pppintr(struct netmsg *msg)
204 struct ppp_softc *sc;
208 * Packets are never sent to this netisr so the message must always
209 * be replied. Interlock processing and notification by replying
212 lwkt_replymsg(&msg->nm_lmsg, 0);
215 for (i = 0; i < NPPP; ++i, ++sc) {
216 lwkt_serialize_enter(sc->sc_if.if_serializer);
217 if (!(sc->sc_flags & SC_TBUSY)
218 && (!ifq_is_empty(&sc->sc_if.if_snd) || !IF_QEMPTY(&sc->sc_fastq))) {
219 sc->sc_flags |= SC_TBUSY;
223 IF_DEQUEUE(&sc->sc_rawq, m);
228 lwkt_serialize_exit(sc->sc_if.if_serializer);
233 * Called from boot code to establish ppp interfaces.
236 pppattach(void *dummy)
238 struct ppp_softc *sc;
241 for (sc = ppp_softc; i < NPPP; sc++) {
242 if_initname(&(sc->sc_if), "ppp", i++);
243 sc->sc_if.if_softc = sc;
244 sc->sc_if.if_mtu = PPP_MTU;
245 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
246 sc->sc_if.if_type = IFT_PPP;
247 sc->sc_if.if_hdrlen = PPP_HDRLEN;
248 sc->sc_if.if_ioctl = pppsioctl;
249 sc->sc_if.if_output = pppoutput;
250 sc->sc_if.if_start = ppp_ifstart;
251 ifq_set_maxlen(&sc->sc_if.if_snd, IFQ_MAXLEN);
252 ifq_set_ready(&sc->sc_if.if_snd);
253 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
254 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
255 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
256 callout_init(&sc->sc_timeout);
257 if_attach(&sc->sc_if, NULL);
258 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
260 netisr_register(NETISR_PPP, cpu0_portfn, pktinfo_portfn_cpu0,
261 pppintr, NETISR_FLAG_NOTMPSAFE);
263 * XXX layering violation - if_ppp can work over any lower level
264 * transport that cares to attach to it.
266 pppasyncattach(dummy);
270 * Allocate a ppp interface unit and initialize it.
273 pppalloc(struct thread *td)
276 struct ppp_softc *sc;
278 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
279 if (sc->sc_xfer == td) {
283 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
284 if (sc->sc_devp == NULL)
290 sc->sc_mru = PPP_MRU;
291 sc->sc_relinq = NULL;
292 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
294 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
296 sl_compress_init(sc->sc_comp, -1);
299 sc->sc_xc_state = NULL;
300 sc->sc_rc_state = NULL;
301 #endif /* PPP_COMPRESS */
302 for (i = 0; i < NUM_NP; ++i)
303 sc->sc_npmode[i] = NPMODE_ERROR;
304 sc->sc_npqueue = NULL;
305 sc->sc_npqtail = &sc->sc_npqueue;
306 sc->sc_last_sent = sc->sc_last_recv = time_second;
312 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
315 pppdealloc(struct ppp_softc *sc)
320 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
321 getmicrotime(&sc->sc_if.if_lastchange);
325 IF_DEQUEUE(&sc->sc_rawq, m);
331 IF_DEQUEUE(&sc->sc_inq, m);
337 IF_DEQUEUE(&sc->sc_fastq, m);
342 while ((m = sc->sc_npqueue) != NULL) {
343 sc->sc_npqueue = m->m_nextpkt;
348 sc->sc_xc_state = NULL;
349 sc->sc_rc_state = NULL;
350 #endif /* PPP_COMPRESS */
352 if (sc->sc_pass_filt.bf_insns != 0) {
353 kfree(sc->sc_pass_filt.bf_insns, M_DEVBUF);
354 sc->sc_pass_filt.bf_insns = 0;
355 sc->sc_pass_filt.bf_len = 0;
357 if (sc->sc_active_filt.bf_insns != 0) {
358 kfree(sc->sc_active_filt.bf_insns, M_DEVBUF);
359 sc->sc_active_filt.bf_insns = 0;
360 sc->sc_active_filt.bf_len = 0;
362 #endif /* PPP_FILTER */
364 if (sc->sc_comp != 0) {
365 kfree(sc->sc_comp, M_DEVBUF);
372 * Ioctl routine for generic ppp devices.
375 pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
376 int flag, struct ucred *cred)
378 int error, flags, mru, npx;
380 struct ppp_option_data *odp;
381 struct compressor **cp;
385 struct bpf_program *bp, *nbp;
386 struct bpf_insn *newcode, *oldcode;
388 #endif /* PPP_FILTER */
390 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
395 *(int *)data = sc->sc_inq.ifq_len;
399 *(int *)data = sc->sc_if.if_dunit;
403 *(u_int *)data = sc->sc_flags;
407 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
409 flags = *(int *)data & SC_MASK;
412 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
415 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
420 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
423 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
428 *(int *)data = sc->sc_mru;
433 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
437 sl_compress_init(sc->sc_comp, *(int *)data);
444 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
446 sc->sc_xfer = curthread;
450 case PPPIOCSCOMPRESS:
451 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
453 odp = (struct ppp_option_data *) data;
455 if (nb > sizeof(ccp_option))
456 nb = sizeof(ccp_option);
457 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
459 if (ccp_option[1] < 2) /* preliminary check on the length byte */
461 for (cp = ppp_compressors; *cp != NULL; ++cp)
462 if ((*cp)->compress_proto == ccp_option[0]) {
464 * Found a handler for the protocol - try to allocate
465 * a compressor or decompressor.
470 if (sc->sc_xc_state != NULL)
471 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
473 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
474 if (sc->sc_xc_state == NULL) {
475 if (sc->sc_flags & SC_DEBUG)
476 kprintf("%s: comp_alloc failed\n",
480 sc->sc_flags &= ~SC_COMP_RUN;
484 if (sc->sc_rc_state != NULL)
485 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
487 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
488 if (sc->sc_rc_state == NULL) {
489 if (sc->sc_flags & SC_DEBUG)
490 kprintf("%s: decomp_alloc failed\n",
494 sc->sc_flags &= ~SC_DECOMP_RUN;
499 if (sc->sc_flags & SC_DEBUG)
500 kprintf("%s: no compressor for [%x %x %x], %x\n",
501 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
503 return (EINVAL); /* no handler found */
504 #endif /* PPP_COMPRESS */
508 npi = (struct npioctl *) data;
509 switch (npi->protocol) {
516 if (cmd == PPPIOCGNPMODE) {
517 npi->mode = sc->sc_npmode[npx];
519 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
521 if (npi->mode != sc->sc_npmode[npx]) {
523 sc->sc_npmode[npx] = npi->mode;
524 if (npi->mode != NPMODE_QUEUE) {
536 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
537 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
544 nbp = (struct bpf_program *) data;
545 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
547 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
548 if (newcodelen != 0) {
549 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
550 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
552 kfree(newcode, M_DEVBUF);
555 if (!bpf_validate(newcode, nbp->bf_len)) {
556 kfree(newcode, M_DEVBUF);
561 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
562 oldcode = bp->bf_insns;
564 bp->bf_len = nbp->bf_len;
565 bp->bf_insns = newcode;
568 kfree(oldcode, M_DEVBUF);
579 * Process an ioctl request to the ppp network interface.
582 pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
584 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
585 struct ifaddr *ifa = (struct ifaddr *)data;
586 struct ifreq *ifr = (struct ifreq *)data;
587 struct ppp_stats *psp;
589 struct ppp_comp_stats *pcp;
597 if ((ifp->if_flags & IFF_RUNNING) == 0)
598 ifp->if_flags &= ~IFF_UP;
603 switch(ifa->ifa_addr->sa_family) {
613 error = EAFNOSUPPORT;
619 switch(ifa->ifa_addr->sa_family) {
629 error = EAFNOSUPPORT;
635 if ((error = priv_check_cred(cr, PRIV_ROOT, 0)) != 0)
637 if (ifr->ifr_mtu > PPP_MAXMTU)
640 sc->sc_if.if_mtu = ifr->ifr_mtu;
642 (*sc->sc_setmtu)(sc);
647 ifr->ifr_mtu = sc->sc_if.if_mtu;
653 error = EAFNOSUPPORT;
656 switch(ifr->ifr_addr.sa_family) {
662 error = EAFNOSUPPORT;
668 psp = &((struct ifpppstatsreq *) data)->stats;
669 bzero(psp, sizeof(*psp));
670 psp->p = sc->sc_stats;
671 #if defined(VJC) && !defined(SL_NO_STATS)
673 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
674 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
675 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
676 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
677 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
678 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
679 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
680 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
687 pcp = &((struct ifpppcstatsreq *) data)->stats;
688 bzero(pcp, sizeof(*pcp));
689 if (sc->sc_xc_state != NULL)
690 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
691 if (sc->sc_rc_state != NULL)
692 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
694 #endif /* PPP_COMPRESS */
704 * Queue a packet. Start transmission if not active.
705 * Packet is placed in Information field of PPP frame.
706 * Called at splnet as the if->if_output handler.
707 * Called at splnet from pppwrite().
710 pppoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
713 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
714 int protocol, address, control;
722 struct altq_pktattr pktattr;
724 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
725 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
726 error = ENETDOWN; /* sort of */
730 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
733 * Compute PPP header.
735 m0->m_flags &= ~M_HIGHPRI;
736 switch (dst->sa_family) {
739 address = PPP_ALLSTATIONS;
742 mode = sc->sc_npmode[NP_IP];
745 * If this packet has the "low delay" bit set in the IP header,
746 * put it on the fastq instead.
748 ip = mtod(m0, struct ip *);
749 if (ip->ip_tos & IPTOS_LOWDELAY)
750 m0->m_flags |= M_HIGHPRI;
756 * This is pretty bogus.. We dont have an ipxcp module in pppd
757 * yet to configure the link parameters. Sigh. I guess a
758 * manual ifconfig would do.... -Peter
760 address = PPP_ALLSTATIONS;
767 address = PPP_ADDRESS(dst->sa_data);
768 control = PPP_CONTROL(dst->sa_data);
769 protocol = PPP_PROTOCOL(dst->sa_data);
773 kprintf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
774 error = EAFNOSUPPORT;
779 * Drop this packet, or return an error, if necessary.
781 if (mode == NPMODE_ERROR) {
785 if (mode == NPMODE_DROP) {
791 * Add PPP header. If no space in first mbuf, allocate another.
792 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
794 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
795 m0 = m_prepend(m0, PPP_HDRLEN, MB_DONTWAIT);
802 m0->m_data -= PPP_HDRLEN;
804 cp = mtod(m0, u_char *);
807 *cp++ = protocol >> 8;
808 *cp++ = protocol & 0xff;
809 m0->m_len += PPP_HDRLEN;
812 for (m = m0; m != 0; m = m->m_next)
815 if (sc->sc_flags & SC_LOG_OUTPKT) {
816 kprintf("%s output: ", ifp->if_xname);
820 if ((protocol & 0x8000) == 0) {
823 * Apply the pass and active filters to the packet,
824 * but only if it is a data packet.
826 *mtod(m0, u_char *) = 1; /* indicates outbound */
827 if (sc->sc_pass_filt.bf_insns != 0
828 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
830 error = 0; /* drop this packet */
835 * Update the time we sent the most recent packet.
837 if (sc->sc_active_filt.bf_insns == 0
838 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
839 sc->sc_last_sent = time_second;
841 *mtod(m0, u_char *) = address;
844 * Update the time we sent the most recent data packet.
846 sc->sc_last_sent = time_second;
847 #endif /* PPP_FILTER */
853 * Put the packet on the appropriate queue.
856 if (mode == NPMODE_QUEUE) {
857 /* XXX we should limit the number of packets on this queue */
858 *sc->sc_npqtail = m0;
859 m0->m_nextpkt = NULL;
860 sc->sc_npqtail = &m0->m_nextpkt;
862 /* fastq and if_snd are emptied at spl[soft]net now */
863 if ((m0->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
865 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
874 ASSERT_SERIALIZED(sc->sc_if.if_serializer);
875 error = ifq_enqueue(&sc->sc_if.if_snd, m0, &pktattr);
879 sc->sc_if.if_oerrors++;
880 sc->sc_stats.ppp_oerrors++;
885 getmicrotime(&ifp->if_lastchange);
887 ifp->if_obytes += len;
898 pppoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
903 lwkt_serialize_enter(ifp->if_serializer);
904 error = pppoutput_serialized(ifp, m0, dst, rtp);
905 lwkt_serialize_exit(ifp->if_serializer);
911 * After a change in the NPmode for some NP, move packets from the
912 * npqueue to the send queue or the fast queue as appropriate.
913 * Should be called at spl[soft]net.
916 ppp_requeue(struct ppp_softc *sc)
918 struct mbuf *m, **mpp;
923 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
924 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
926 mode = sc->sc_npmode[NP_IP];
935 * This packet can now go on one of the queues to be sent.
939 if ((m->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
949 lwkt_serialize_enter(sc->sc_if.if_serializer);
950 error = ifq_enqueue(&sc->sc_if.if_snd, m, NULL);
951 lwkt_serialize_exit(sc->sc_if.if_serializer);
954 sc->sc_if.if_oerrors++;
955 sc->sc_stats.ppp_oerrors++;
970 sc->sc_npqtail = mpp;
974 * Transmitter has finished outputting some stuff;
975 * remember to call sc->sc_start later at splsoftnet.
978 ppp_restart(struct ppp_softc *sc)
981 sc->sc_flags &= ~SC_TBUSY;
982 schednetisr(NETISR_PPP);
988 * Get a packet to send. This procedure is intended to be called at
989 * splsoftnet, since it may involve time-consuming operations such as
990 * applying VJ compression, packet compression, address/control and/or
991 * protocol field compression to the packet.
994 ppp_dequeue(struct ppp_softc *sc)
998 int address, control, protocol;
1001 * Grab a packet to send: first try the fast queue, then the
1004 IF_DEQUEUE(&sc->sc_fastq, m);
1006 m = ifq_dequeue(&sc->sc_if.if_snd, NULL);
1010 ++sc->sc_stats.ppp_opackets;
1013 * Extract the ppp header of the new packet.
1014 * The ppp header will be in one mbuf.
1016 cp = mtod(m, u_char *);
1017 address = PPP_ADDRESS(cp);
1018 control = PPP_CONTROL(cp);
1019 protocol = PPP_PROTOCOL(cp);
1025 * If the packet is a TCP/IP packet, see if we can compress it.
1027 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1032 ip = (struct ip *) (cp + PPP_HDRLEN);
1033 if (mp->m_len <= PPP_HDRLEN) {
1037 ip = mtod(mp, struct ip *);
1039 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1040 if (ip->ip_p == IPPROTO_TCP) {
1041 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1042 !(sc->sc_flags & SC_NO_TCP_CCID));
1044 case TYPE_UNCOMPRESSED_TCP:
1045 protocol = PPP_VJC_UNCOMP;
1047 case TYPE_COMPRESSED_TCP:
1048 protocol = PPP_VJC_COMP;
1049 cp = mtod(m, u_char *);
1050 cp[0] = address; /* header has moved */
1055 cp[3] = protocol; /* update protocol in PPP header */
1065 #endif /* PPP_COMPRESS */
1069 if (protocol != PPP_LCP && protocol != PPP_CCP
1070 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1071 struct mbuf *mcomp = NULL;
1075 for (mp = m; mp != NULL; mp = mp->m_next)
1077 clen = (*sc->sc_xcomp->compress)
1078 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1079 if (mcomp != NULL) {
1080 if (sc->sc_flags & SC_CCP_UP) {
1081 /* Send the compressed packet instead of the original. */
1084 cp = mtod(m, u_char *);
1087 /* Can't transmit compressed packets until CCP is up. */
1092 #endif /* PPP_COMPRESS */
1095 * Compress the address/control and protocol, if possible.
1097 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1098 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1099 protocol != PPP_LCP) {
1100 /* can compress address/control */
1104 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1105 /* can compress protocol */
1106 if (mtod(m, u_char *) == cp) {
1107 cp[2] = cp[1]; /* move address/control up */
1119 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1120 * 0 if it is about to be transmitted.
1123 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
1130 * Get a pointer to the data after the PPP header.
1132 if (m->m_len <= PPP_HDRLEN) {
1136 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1139 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1142 ep = mtod(mp, u_char *) + mp->m_len;
1143 if (dp + CCP_HDRLEN > ep)
1145 slen = CCP_LENGTH(dp);
1146 if (dp + slen > ep) {
1147 if (sc->sc_flags & SC_DEBUG)
1148 kprintf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1149 dp, slen, mtod(mp, u_char *), mp->m_len);
1153 switch (CCP_CODE(dp)) {
1157 /* CCP must be going down - disable compression */
1158 if (sc->sc_flags & SC_CCP_UP) {
1160 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1166 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1167 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1168 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1170 /* we're agreeing to send compressed packets. */
1171 if (sc->sc_xc_state != NULL
1172 && (*sc->sc_xcomp->comp_init)
1173 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1174 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1176 sc->sc_flags |= SC_COMP_RUN;
1180 /* peer is agreeing to send compressed packets. */
1181 if (sc->sc_rc_state != NULL
1182 && (*sc->sc_rcomp->decomp_init)
1183 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1184 sc->sc_if.if_dunit, 0, sc->sc_mru,
1185 sc->sc_flags & SC_DEBUG)) {
1187 sc->sc_flags |= SC_DECOMP_RUN;
1188 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1196 if (sc->sc_flags & SC_CCP_UP) {
1198 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1199 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1201 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1202 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1204 sc->sc_flags &= ~SC_DC_ERROR;
1214 * CCP is down; free (de)compressor state if necessary.
1217 ppp_ccp_closed(struct ppp_softc *sc)
1219 if (sc->sc_xc_state) {
1220 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1221 sc->sc_xc_state = NULL;
1223 if (sc->sc_rc_state) {
1224 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1225 sc->sc_rc_state = NULL;
1228 #endif /* PPP_COMPRESS */
1231 * PPP packet input routine.
1232 * The caller has checked and removed the FCS and has inserted
1233 * the address/control bytes and the protocol high byte if they
1237 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
1242 m->m_flags |= M_ERRMARK;
1243 IF_ENQUEUE(&sc->sc_rawq, m);
1244 schednetisr(NETISR_PPP);
1250 * Process a received PPP packet, doing decompression as necessary.
1251 * Should be called at splsoftnet.
1253 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1254 TYPE_UNCOMPRESSED_TCP)
1257 ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
1259 struct ifnet *ifp = &sc->sc_if;
1261 int ilen = 0, xlen, proto, rv;
1262 u_char *cp, adrs, ctrl;
1263 struct mbuf *mp, *dmp = NULL;
1267 ASSERT_SERIALIZED(ifp->if_serializer);
1269 sc->sc_stats.ppp_ipackets++;
1271 if (sc->sc_flags & SC_LOG_INPKT) {
1273 for (mp = m; mp != NULL; mp = mp->m_next)
1275 kprintf("%s: got %d bytes\n", ifp->if_xname, ilen);
1279 cp = mtod(m, u_char *);
1280 adrs = PPP_ADDRESS(cp);
1281 ctrl = PPP_CONTROL(cp);
1282 proto = PPP_PROTOCOL(cp);
1284 if (m->m_flags & M_ERRMARK) {
1285 m->m_flags &= ~M_ERRMARK;
1287 sc->sc_flags |= SC_VJ_RESET;
1293 * Decompress this packet if necessary, update the receiver's
1294 * dictionary, or take appropriate action on a CCP packet.
1296 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1297 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1298 /* decompress this packet */
1299 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1300 if (rv == DECOMP_OK) {
1303 /* no error, but no decompressed packet produced */
1307 cp = mtod(m, u_char *);
1308 proto = PPP_PROTOCOL(cp);
1312 * An error has occurred in decompression.
1313 * Pass the compressed packet up to pppd, which may take
1314 * CCP down or issue a Reset-Req.
1316 if (sc->sc_flags & SC_DEBUG)
1317 kprintf("%s: decompress failed %d\n", ifp->if_xname, rv);
1319 sc->sc_flags |= SC_VJ_RESET;
1320 if (rv == DECOMP_ERROR)
1321 sc->sc_flags |= SC_DC_ERROR;
1323 sc->sc_flags |= SC_DC_FERROR;
1328 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1329 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1331 if (proto == PPP_CCP) {
1338 for (mp = m; mp != NULL; mp = mp->m_next)
1342 if (sc->sc_flags & SC_VJ_RESET) {
1344 * If we've missed a packet, we must toss subsequent compressed
1345 * packets which don't have an explicit connection ID.
1348 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1350 sc->sc_flags &= ~SC_VJ_RESET;
1355 * See if we have a VJ-compressed packet to uncompress.
1357 if (proto == PPP_VJC_COMP) {
1358 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1361 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1362 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1363 sc->sc_comp, &iphdr, &hlen);
1366 if (sc->sc_flags & SC_DEBUG)
1367 kprintf("%s: VJ uncompress failed on type comp\n",
1372 /* Copy the PPP and IP headers into a new mbuf. */
1373 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1378 if (hlen + PPP_HDRLEN > MHLEN) {
1379 MCLGET(mp, MB_DONTWAIT);
1380 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1382 goto bad; /* lose if big headers and no clusters */
1385 cp = mtod(mp, u_char *);
1391 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1392 mp->m_len = hlen + PPP_HDRLEN;
1395 * Trim the PPP and VJ headers off the old mbuf
1396 * and stick the new and old mbufs together.
1398 m->m_data += PPP_HDRLEN + xlen;
1399 m->m_len -= PPP_HDRLEN + xlen;
1400 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1401 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1402 mp->m_len += m->m_len;
1403 mp->m_next = m_free(m);
1408 ilen += hlen - xlen;
1410 } else if (proto == PPP_VJC_UNCOMP) {
1411 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1414 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1415 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1416 sc->sc_comp, &iphdr, &hlen);
1419 if (sc->sc_flags & SC_DEBUG)
1420 kprintf("%s: VJ uncompress failed on type uncomp\n",
1431 * If the packet will fit in a header mbuf, don't waste a
1432 * whole cluster on it.
1434 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1435 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1437 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1443 m->m_pkthdr.len = ilen;
1444 m->m_pkthdr.rcvif = ifp;
1446 if ((proto & 0x8000) == 0) {
1449 * See whether we want to pass this packet, and
1450 * if it counts as link activity.
1452 adrs = *mtod(m, u_char *); /* save address field */
1453 *mtod(m, u_char *) = 0; /* indicate inbound */
1454 if (sc->sc_pass_filt.bf_insns != 0
1455 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1457 /* drop this packet */
1461 if (sc->sc_active_filt.bf_insns == 0
1462 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1463 sc->sc_last_recv = time_second;
1465 *mtod(m, u_char *) = adrs;
1468 * Record the time that we received this packet.
1470 sc->sc_last_recv = time_second;
1471 #endif /* PPP_FILTER */
1474 BPF_MTAP(&sc->sc_if, m);
1481 * IP packet - take off the ppp header and pass it up to IP.
1483 if ((ifp->if_flags & IFF_UP) == 0
1484 || sc->sc_npmode[NP_IP] != 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 if (ipflow_fastforward(m))
1502 * IPX packet - take off the ppp header and pass it up to IPX.
1504 if ((sc->sc_if.if_flags & IFF_UP) == 0
1505 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1506 /* interface is down - drop the packet. */
1510 m->m_pkthdr.len -= PPP_HDRLEN;
1511 m->m_data += PPP_HDRLEN;
1512 m->m_len -= PPP_HDRLEN;
1514 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1520 * Some other protocol - place on input queue for read().
1526 * If we found a netproto just pass it to the proto. Otherwise queue
1527 * the packet to the tty (e.g. pppd). Note that sc_ctlp() must be
1528 * called EXACTLY once per packet queued.
1531 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1535 rv = (netisr_queue(isr, m) == 0);
1538 if (sc->sc_flags & SC_DEBUG)
1539 kprintf("%s: input queue full\n", ifp->if_xname);
1545 ifp->if_ibytes += ilen;
1546 getmicrotime(&ifp->if_lastchange);
1552 sc->sc_if.if_ierrors++;
1553 sc->sc_stats.ppp_ierrors++;
1556 #define MAX_DUMP_BYTES 128
1559 pppdumpm(struct mbuf *m0)
1561 char buf[3*MAX_DUMP_BYTES+4];
1565 for (m = m0; m; m = m->m_next) {
1567 u_char *rptr = (u_char *)m->m_data;
1570 if (bp > buf + sizeof(buf) - 4)
1572 *bp++ = hex2ascii(*rptr >> 4);
1573 *bp++ = hex2ascii(*rptr++ & 0xf);
1577 if (bp > buf + sizeof(buf) - 3)
1587 kprintf("%s\n", buf);
1591 * a wrapper to transmit a packet from if_start since ALTQ uses
1592 * if_start to send a packet.
1595 ppp_ifstart(struct ifnet *ifp)
1597 struct ppp_softc *sc;
1600 (*sc->sc_start)(sc);