2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6 * Copyright (c) 1989 Carnegie Mellon University.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by Carnegie Mellon University. The name of the
15 * University may not be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 * Carnegie Mellon University
24 * Pittsburgh, PA 15213
29 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
31 * Copyright (c) 1987 Regents of the University of California.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms are permitted
35 * provided that the above copyright notice and this paragraph are
36 * duplicated in all such forms and that any documentation,
37 * advertising materials, and other materials related to such
38 * distribution and use acknowledge that the software was developed
39 * by the University of California, Berkeley. The name of the
40 * University may not be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
44 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 * Serial Line interface
49 * Center for Seismic Studies
50 * 1300 N 17th Street, Suite 1450
51 * Arlington, Virginia 22209
56 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
57 * Converted to 4.3BSD Beta by Chris Torek.
58 * Other changes made at Berkeley, based in part on code by Kirk Smith.
60 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
61 * Added VJ tcp header compression; more unified ioctls
63 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
64 * Cleaned up a lot of the mbuf-related code to fix bugs that
65 * caused system crashes and packet corruption. Changed pppstart
66 * so that it doesn't just give up with a collision if the whole
67 * packet doesn't fit in the output ring buffer.
69 * Added priority queueing for interactive IP packets, following
70 * the model of if_sl.c, plus hooks for bpf.
71 * Paul Mackerras (paulus@cs.anu.edu.au).
75 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
76 /* 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>
99 #include <sys/module.h>
102 #include <net/if_clone.h>
103 #include <net/if_types.h>
104 #include <net/netisr.h>
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/in_var.h>
111 #include <netinet/ip.h>
115 #include <netipx/ipx.h>
116 #include <netipx/ipx_if.h>
120 #include <net/slcompress.h>
123 #include <net/if_ppp.h>
124 #include <net/if_pppvar.h>
128 #define splsoftnet splnet
132 #define PACKETPTR struct mbuf *
133 #include <net/ppp_comp.h>
136 #define PPPNAME "ppp"
137 static MALLOC_DEFINE(M_PPP, PPPNAME, "PPP interface");
139 static struct mtx ppp_softc_list_mtx;
140 static LIST_HEAD(, ppp_softc) ppp_softc_list;
142 #define PPP_LIST_LOCK_INIT() mtx_init(&ppp_softc_list_mtx, \
143 "ppp_softc_list_mtx", NULL, MTX_DEF)
144 #define PPP_LIST_LOCK_DESTROY() mtx_destroy(&ppp_softc_list_mtx)
145 #define PPP_LIST_LOCK() mtx_lock(&ppp_softc_list_mtx)
146 #define PPP_LIST_UNLOCK() mtx_unlock(&ppp_softc_list_mtx)
148 /* XXX layering violation */
149 extern void pppasyncattach(void *);
150 extern void pppasyncdetach(void);
152 static int pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
153 static void pppintr(void);
155 static void ppp_requeue(struct ppp_softc *);
156 static void ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
157 static void ppp_ccp_closed(struct ppp_softc *);
158 static void ppp_inproc(struct ppp_softc *, struct mbuf *);
159 static void pppdumpm(struct mbuf *m0);
160 static int ppp_clone_create(struct if_clone *, int, caddr_t);
161 static void ppp_clone_destroy(struct ifnet *);
163 IFC_SIMPLE_DECLARE(ppp, 0);
166 * Some useful mbuf macros not in mbuf.h.
168 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
170 #define M_DATASTART(m) \
171 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
172 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
174 #define M_DATASIZE(m) \
175 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
176 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
179 * We steal two bits in the mbuf m_flags, to mark high-priority packets
180 * for output, and received packets following lost/corrupted packets.
182 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
183 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
188 * List of compressors we know about.
189 * We leave some space so maybe we can modload compressors.
192 extern struct compressor ppp_bsd_compress;
193 extern struct compressor ppp_deflate, ppp_deflate_draft;
195 static struct compressor *ppp_compressors[8] = {
196 #if defined(PPP_BSDCOMP)
199 #if defined(PPP_DEFLATE)
205 #endif /* PPP_COMPRESS */
208 ppp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
211 struct ppp_softc *sc;
213 sc = malloc(sizeof(struct ppp_softc), M_PPP, M_WAITOK | M_ZERO);
214 ifp = sc->sc_ifp = if_alloc(IFT_PPP);
221 if_initname(ifp, ifc->ifc_name, unit);
222 ifp->if_mtu = PPP_MTU;
223 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST | IFF_NEEDSGIANT;
224 ifp->if_hdrlen = PPP_HDRLEN;
225 ifp->if_ioctl = pppsioctl;
226 ifp->if_output = pppoutput;
227 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
228 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
229 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
230 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
231 mtx_init(&sc->sc_inq.ifq_mtx, "ppp_inq", NULL, MTX_DEF);
232 mtx_init(&sc->sc_fastq.ifq_mtx, "ppp_fastq", NULL, MTX_DEF);
233 mtx_init(&sc->sc_rawq.ifq_mtx, "ppp_rawq", NULL, MTX_DEF);
235 bpfattach(ifp, DLT_PPP, PPP_HDRLEN);
238 LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_list);
245 ppp_clone_destroy(struct ifnet *ifp)
247 struct ppp_softc *sc;
251 LIST_REMOVE(sc, sc_list);
257 mtx_destroy(&sc->sc_rawq.ifq_mtx);
258 mtx_destroy(&sc->sc_fastq.ifq_mtx);
259 mtx_destroy(&sc->sc_inq.ifq_mtx);
264 ppp_modevent(module_t mod, int type, void *data)
269 PPP_LIST_LOCK_INIT();
270 LIST_INIT(&ppp_softc_list);
271 if_clone_attach(&ppp_cloner);
273 netisr_register(NETISR_PPP, (netisr_t *)pppintr, NULL, 0);
275 * XXX layering violation - if_ppp can work over any lower
276 * level transport that cares to attach to it.
278 pppasyncattach(NULL);
281 /* XXX: layering violation */
284 netisr_unregister(NETISR_PPP);
286 if_clone_detach(&ppp_cloner);
287 PPP_LIST_LOCK_DESTROY();
295 static moduledata_t ppp_mod = {
301 DECLARE_MODULE(if_ppp, ppp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
304 * Allocate a ppp interface unit and initialize it.
311 char tmpname[IFNAMSIZ];
313 struct ppp_softc *sc;
316 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
317 if (sc->sc_xfer == pid) {
323 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
324 if (sc->sc_devp == NULL)
328 /* Try to clone an interface if we don't have a free one */
330 strcpy(tmpname, PPPNAME);
331 if (if_clone_create(tmpname, sizeof(tmpname), (caddr_t) 0) != 0)
333 ifp = ifunit(tmpname);
338 if (sc == NULL || sc->sc_devp != NULL)
342 sc->sc_mru = PPP_MRU;
343 sc->sc_relinq = NULL;
344 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
346 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
349 sl_compress_init(sc->sc_comp, -1);
352 sc->sc_xc_state = NULL;
353 sc->sc_rc_state = NULL;
354 #endif /* PPP_COMPRESS */
355 for (i = 0; i < NUM_NP; ++i)
356 sc->sc_npmode[i] = NPMODE_ERROR;
357 sc->sc_npqueue = NULL;
358 sc->sc_npqtail = &sc->sc_npqueue;
359 sc->sc_last_sent = sc->sc_last_recv = time_uptime;
365 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
369 struct ppp_softc *sc;
373 if_down(PPP2IFP(sc));
374 PPP2IFP(sc)->if_flags &= ~IFF_UP;
375 PPP2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
376 getmicrotime(&PPP2IFP(sc)->if_lastchange);
379 IF_DRAIN(&sc->sc_rawq);
380 IF_DRAIN(&sc->sc_inq);
381 IF_DRAIN(&sc->sc_fastq);
382 while ((m = sc->sc_npqueue) != NULL) {
383 sc->sc_npqueue = m->m_nextpkt;
388 sc->sc_xc_state = NULL;
389 sc->sc_rc_state = NULL;
390 #endif /* PPP_COMPRESS */
392 if (sc->sc_pass_filt.bf_insns != 0) {
393 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
394 sc->sc_pass_filt.bf_insns = 0;
395 sc->sc_pass_filt.bf_len = 0;
397 if (sc->sc_active_filt.bf_insns != 0) {
398 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
399 sc->sc_active_filt.bf_insns = 0;
400 sc->sc_active_filt.bf_len = 0;
402 #endif /* PPP_FILTER */
404 if (sc->sc_comp != 0) {
405 free(sc->sc_comp, M_DEVBUF);
412 * Ioctl routine for generic ppp devices.
415 pppioctl(sc, cmd, data, flag, td)
416 struct ppp_softc *sc;
422 struct proc *p = td->td_proc;
423 int s, flags, mru, npx;
426 struct ppp_option_data *odp;
427 struct compressor **cp;
431 struct bpf_program *bp, *nbp;
432 struct bpf_insn *newcode, *oldcode;
434 #endif /* PPP_FILTER */
436 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
441 *(int *)data = sc->sc_inq.ifq_len;
445 *(int *)data = PPP2IFP(sc)->if_dunit;
449 *(u_int *)data = sc->sc_flags;
453 if ((error = suser(td)) != 0)
455 flags = *(int *)data & SC_MASK;
458 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
462 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
467 if ((error = suser(td)) != 0)
470 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
475 *(int *)data = sc->sc_mru;
480 if ((error = suser(td)) != 0)
484 sl_compress_init(sc->sc_comp, *(int *)data);
491 if ((error = suser(td)) != 0)
493 sc->sc_xfer = p->p_pid;
497 case PPPIOCSCOMPRESS:
498 if ((error = suser(td)) != 0)
500 odp = (struct ppp_option_data *) data;
502 if (nb > sizeof(ccp_option))
503 nb = sizeof(ccp_option);
504 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
506 if (ccp_option[1] < 2) { /* preliminary check on the length byte */
510 for (cp = ppp_compressors; *cp != NULL; ++cp)
511 if ((*cp)->compress_proto == ccp_option[0]) {
513 * Found a handler for the protocol - try to allocate
514 * a compressor or decompressor.
519 if (sc->sc_xc_state != NULL)
520 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
522 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
523 if (sc->sc_xc_state == NULL) {
524 if (sc->sc_flags & SC_DEBUG)
525 if_printf(PPP2IFP(sc), "comp_alloc failed\n");
529 sc->sc_flags &= ~SC_COMP_RUN;
533 if (sc->sc_rc_state != NULL)
534 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
536 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
537 if (sc->sc_rc_state == NULL) {
538 if (sc->sc_flags & SC_DEBUG)
539 if_printf(PPP2IFP(sc), "decomp_alloc failed\n");
543 sc->sc_flags &= ~SC_DECOMP_RUN;
548 if (sc->sc_flags & SC_DEBUG)
549 if_printf(PPP2IFP(sc), "no compressor for [%x %x %x], %x\n",
550 ccp_option[0], ccp_option[1], ccp_option[2], nb);
551 error = EINVAL; /* no handler found */
553 #endif /* PPP_COMPRESS */
557 npi = (struct npioctl *) data;
558 npx = 0; /* XXX: quiet gcc */
559 switch (npi->protocol) {
568 if (cmd == PPPIOCGNPMODE) {
569 npi->mode = sc->sc_npmode[npx];
571 if ((error = suser(td)) != 0)
573 if (npi->mode != sc->sc_npmode[npx]) {
575 sc->sc_npmode[npx] = npi->mode;
576 if (npi->mode != NPMODE_QUEUE) {
588 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
589 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
596 nbp = (struct bpf_program *) data;
597 if ((unsigned) nbp->bf_len > BPF_MAXINSNS) {
601 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
602 if (newcodelen != 0) {
603 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
605 error = EINVAL; /* or sumpin */
608 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
610 free(newcode, M_DEVBUF);
613 if (!bpf_validate(newcode, nbp->bf_len)) {
614 free(newcode, M_DEVBUF);
620 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
621 oldcode = bp->bf_insns;
623 bp->bf_len = nbp->bf_len;
624 bp->bf_insns = newcode;
627 free(oldcode, M_DEVBUF);
639 * Process an ioctl request to the ppp network interface.
642 pppsioctl(ifp, cmd, data)
643 register struct ifnet *ifp;
647 struct thread *td = curthread; /* XXX */
648 register struct ppp_softc *sc = ifp->if_softc;
649 register struct ifaddr *ifa = (struct ifaddr *)data;
650 register struct ifreq *ifr = (struct ifreq *)data;
651 struct ppp_stats *psp;
653 struct ppp_comp_stats *pcp;
655 int s = splimp(), error = 0;
659 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
660 ifp->if_flags &= ~IFF_UP;
665 switch(ifa->ifa_addr->sa_family) {
675 error = EAFNOSUPPORT;
681 switch(ifa->ifa_addr->sa_family) {
691 error = EAFNOSUPPORT;
697 if ((error = suser(td)) != 0)
699 if (ifr->ifr_mtu > PPP_MAXMTU)
702 PPP2IFP(sc)->if_mtu = ifr->ifr_mtu;
704 (*sc->sc_setmtu)(sc);
709 ifr->ifr_mtu = PPP2IFP(sc)->if_mtu;
715 error = EAFNOSUPPORT;
718 switch(ifr->ifr_addr.sa_family) {
724 error = EAFNOSUPPORT;
730 psp = &((struct ifpppstatsreq *) data)->stats;
731 bzero(psp, sizeof(*psp));
732 psp->p = sc->sc_stats;
733 #if defined(VJC) && !defined(SL_NO_STATS)
735 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
736 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
737 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
738 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
739 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
740 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
741 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
742 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
749 pcp = &((struct ifpppcstatsreq *) data)->stats;
750 bzero(pcp, sizeof(*pcp));
751 if (sc->sc_xc_state != NULL)
752 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
753 if (sc->sc_rc_state != NULL)
754 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
756 #endif /* PPP_COMPRESS */
766 * Queue a packet. Start transmission if not active.
767 * Packet is placed in Information field of PPP frame.
768 * Called at splnet as the if->if_output handler.
769 * Called at splnet from pppwrite().
772 pppoutput(ifp, m0, dst, rtp)
775 struct sockaddr *dst;
778 register struct ppp_softc *sc = ifp->if_softc;
779 int protocol, address, control;
788 error = mac_check_ifnet_transmit(ifp, m0);
793 if (sc->sc_devp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0
794 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
795 error = ENETDOWN; /* sort of */
800 * Compute PPP header.
802 m0->m_flags &= ~M_HIGHPRI;
803 switch (dst->sa_family) {
806 address = PPP_ALLSTATIONS;
809 mode = sc->sc_npmode[NP_IP];
812 * If this packet has the "low delay" bit set in the IP header,
813 * put it on the fastq instead.
815 ip = mtod(m0, struct ip *);
816 if (ip->ip_tos & IPTOS_LOWDELAY)
817 m0->m_flags |= M_HIGHPRI;
823 * This is pretty bogus.. We dont have an ipxcp module in pppd
824 * yet to configure the link parameters. Sigh. I guess a
825 * manual ifconfig would do.... -Peter
827 address = PPP_ALLSTATIONS;
834 address = PPP_ADDRESS(dst->sa_data);
835 control = PPP_CONTROL(dst->sa_data);
836 protocol = PPP_PROTOCOL(dst->sa_data);
840 if_printf(ifp, "af%d not supported\n", dst->sa_family);
841 error = EAFNOSUPPORT;
846 * Drop this packet, or return an error, if necessary.
848 if (mode == NPMODE_ERROR) {
852 if (mode == NPMODE_DROP) {
858 * Add PPP header. If no space in first mbuf, allocate another.
859 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
861 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
862 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
869 m0->m_data -= PPP_HDRLEN;
871 cp = mtod(m0, u_char *);
874 *cp++ = protocol >> 8;
875 *cp++ = protocol & 0xff;
876 m0->m_len += PPP_HDRLEN;
878 len = m_length(m0, NULL);
880 if (sc->sc_flags & SC_LOG_OUTPKT) {
881 printf("%s output: ", ifp->if_xname);
885 if ((protocol & 0x8000) == 0) {
888 * Apply the pass and active filters to the packet,
889 * but only if it is a data packet.
891 *mtod(m0, u_char *) = 1; /* indicates outbound */
892 if (sc->sc_pass_filt.bf_insns != 0
893 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
895 error = 0; /* drop this packet */
900 * Update the time we sent the most recent packet.
902 if (sc->sc_active_filt.bf_insns == 0
903 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
904 sc->sc_last_sent = time_uptime;
906 *mtod(m0, u_char *) = address;
909 * Update the time we sent the most recent data packet.
911 sc->sc_last_sent = time_uptime;
912 #endif /* PPP_FILTER */
916 * See if bpf wants to look at the packet.
921 * Put the packet on the appropriate queue.
923 s = splsoftnet(); /* redundant */
924 if (mode == NPMODE_QUEUE) {
925 /* XXX we should limit the number of packets on this queue */
926 *sc->sc_npqtail = m0;
927 m0->m_nextpkt = NULL;
928 sc->sc_npqtail = &m0->m_nextpkt;
930 /* fastq and if_snd are emptied at spl[soft]net now */
931 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq:
932 (struct ifqueue *)&ifp->if_snd;
934 if (_IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
937 PPP2IFP(sc)->if_oerrors++;
938 sc->sc_stats.ppp_oerrors++;
942 _IF_ENQUEUE(ifq, m0);
946 getmicrotime(&ifp->if_lastchange);
948 ifp->if_obytes += len;
959 * After a change in the NPmode for some NP, move packets from the
960 * npqueue to the send queue or the fast queue as appropriate.
961 * Should be called at spl[soft]net.
965 struct ppp_softc *sc;
967 struct mbuf *m, **mpp;
971 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
972 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
974 mode = sc->sc_npmode[NP_IP];
983 * This packet can now go on one of the queues to be sent.
987 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq:
988 (struct ifqueue *)&PPP2IFP(sc)->if_snd;
989 if (! IF_HANDOFF(ifq, m, NULL)) {
990 PPP2IFP(sc)->if_oerrors++;
991 sc->sc_stats.ppp_oerrors++;
1002 mpp = &m->m_nextpkt;
1006 sc->sc_npqtail = mpp;
1010 * Transmitter has finished outputting some stuff;
1011 * remember to call sc->sc_start later at splsoftnet.
1015 struct ppp_softc *sc;
1019 sc->sc_flags &= ~SC_TBUSY;
1020 schednetisr(NETISR_PPP);
1026 * Get a packet to send. This procedure is intended to be called at
1027 * splsoftnet, since it may involve time-consuming operations such as
1028 * applying VJ compression, packet compression, address/control and/or
1029 * protocol field compression to the packet.
1033 struct ppp_softc *sc;
1035 struct mbuf *m, *mp;
1037 int address, control, protocol;
1040 * Grab a packet to send: first try the fast queue, then the
1043 IF_DEQUEUE(&sc->sc_fastq, m);
1045 IF_DEQUEUE(&PPP2IFP(sc)->if_snd, m);
1049 ++sc->sc_stats.ppp_opackets;
1052 * Extract the ppp header of the new packet.
1053 * The ppp header will be in one mbuf.
1055 cp = mtod(m, u_char *);
1056 address = PPP_ADDRESS(cp);
1057 control = PPP_CONTROL(cp);
1058 protocol = PPP_PROTOCOL(cp);
1064 * If the packet is a TCP/IP packet, see if we can compress it.
1066 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1071 ip = (struct ip *) (cp + PPP_HDRLEN);
1072 if (mp->m_len <= PPP_HDRLEN) {
1076 ip = mtod(mp, struct ip *);
1078 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1079 if (ip->ip_p == IPPROTO_TCP) {
1080 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1081 !(sc->sc_flags & SC_NO_TCP_CCID));
1083 case TYPE_UNCOMPRESSED_TCP:
1084 protocol = PPP_VJC_UNCOMP;
1086 case TYPE_COMPRESSED_TCP:
1087 protocol = PPP_VJC_COMP;
1088 cp = mtod(m, u_char *);
1089 cp[0] = address; /* header has moved */
1094 cp[3] = protocol; /* update protocol in PPP header */
1104 #endif /* PPP_COMPRESS */
1108 if (protocol != PPP_LCP && protocol != PPP_CCP
1109 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1110 struct mbuf *mcomp = NULL;
1113 slen = m_length(m, NULL);
1114 clen = (*sc->sc_xcomp->compress)
1115 (sc->sc_xc_state, &mcomp, m, slen, PPP2IFP(sc)->if_mtu + PPP_HDRLEN);
1116 if (mcomp != NULL) {
1117 if (sc->sc_flags & SC_CCP_UP) {
1118 /* Send the compressed packet instead of the original. */
1121 cp = mtod(m, u_char *);
1124 /* Can't transmit compressed packets until CCP is up. */
1129 #endif /* PPP_COMPRESS */
1132 * Compress the address/control and protocol, if possible.
1134 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1135 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1136 protocol != PPP_LCP) {
1137 /* can compress address/control */
1141 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1142 /* can compress protocol */
1143 if (mtod(m, u_char *) == cp) {
1144 cp[2] = cp[1]; /* move address/control up */
1155 * Software interrupt routine, called at spl[soft]net.
1160 struct ppp_softc *sc;
1167 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
1169 if (!(sc->sc_flags & SC_TBUSY)
1170 && (PPP2IFP(sc)->if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1171 sc->sc_flags |= SC_TBUSY;
1173 (*sc->sc_start)(sc);
1178 IF_DEQUEUE(&sc->sc_rawq, m);
1183 mac_create_mbuf_from_ifnet(PPP2IFP(sc), m);
1193 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1194 * 0 if it is about to be transmitted.
1197 ppp_ccp(sc, m, rcvd)
1198 struct ppp_softc *sc;
1207 * Get a pointer to the data after the PPP header.
1209 if (m->m_len <= PPP_HDRLEN) {
1213 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1216 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1219 ep = mtod(mp, u_char *) + mp->m_len;
1220 if (dp + CCP_HDRLEN > ep)
1222 slen = CCP_LENGTH(dp);
1223 if (dp + slen > ep) {
1224 if (sc->sc_flags & SC_DEBUG)
1225 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1226 dp, slen, mtod(mp, u_char *), mp->m_len);
1230 switch (CCP_CODE(dp)) {
1234 /* CCP must be going down - disable compression */
1235 if (sc->sc_flags & SC_CCP_UP) {
1237 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1243 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1244 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1245 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1247 /* we're agreeing to send compressed packets. */
1248 if (sc->sc_xc_state != NULL
1249 && (*sc->sc_xcomp->comp_init)
1250 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1251 PPP2IFP(sc)->if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1253 sc->sc_flags |= SC_COMP_RUN;
1257 /* peer is agreeing to send compressed packets. */
1258 if (sc->sc_rc_state != NULL
1259 && (*sc->sc_rcomp->decomp_init)
1260 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1261 PPP2IFP(sc)->if_dunit, 0, sc->sc_mru,
1262 sc->sc_flags & SC_DEBUG)) {
1264 sc->sc_flags |= SC_DECOMP_RUN;
1265 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1273 if (sc->sc_flags & SC_CCP_UP) {
1275 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1276 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1278 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1279 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1281 sc->sc_flags &= ~SC_DC_ERROR;
1291 * CCP is down; free (de)compressor state if necessary.
1295 struct ppp_softc *sc;
1297 if (sc->sc_xc_state) {
1298 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1299 sc->sc_xc_state = NULL;
1301 if (sc->sc_rc_state) {
1302 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1303 sc->sc_rc_state = NULL;
1306 #endif /* PPP_COMPRESS */
1309 * PPP packet input routine.
1310 * The caller has checked and removed the FCS and has inserted
1311 * the address/control bytes and the protocol high byte if they
1315 ppppktin(sc, m, lost)
1316 struct ppp_softc *sc;
1323 m->m_flags |= M_ERRMARK;
1324 IF_ENQUEUE(&sc->sc_rawq, m);
1325 schednetisr(NETISR_PPP);
1330 * Process a received PPP packet, doing decompression as necessary.
1331 * Should be called at splsoftnet.
1333 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1334 TYPE_UNCOMPRESSED_TCP)
1338 struct ppp_softc *sc;
1341 struct ifnet *ifp = PPP2IFP(sc);
1343 int s, ilen = 0, xlen, proto, rv;
1344 u_char *cp, adrs, ctrl;
1345 struct mbuf *mp, *dmp = NULL;
1349 sc->sc_stats.ppp_ipackets++;
1351 if (sc->sc_flags & SC_LOG_INPKT) {
1352 ilen = m_length(m, NULL);
1353 if_printf(ifp, "got %d bytes\n", ilen);
1357 cp = mtod(m, u_char *);
1358 adrs = PPP_ADDRESS(cp);
1359 ctrl = PPP_CONTROL(cp);
1360 proto = PPP_PROTOCOL(cp);
1362 if (m->m_flags & M_ERRMARK) {
1363 m->m_flags &= ~M_ERRMARK;
1365 sc->sc_flags |= SC_VJ_RESET;
1371 * Decompress this packet if necessary, update the receiver's
1372 * dictionary, or take appropriate action on a CCP packet.
1374 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1375 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1376 /* decompress this packet */
1377 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1378 if (rv == DECOMP_OK) {
1381 /* no error, but no decompressed packet produced */
1385 cp = mtod(m, u_char *);
1386 proto = PPP_PROTOCOL(cp);
1390 * An error has occurred in decompression.
1391 * Pass the compressed packet up to pppd, which may take
1392 * CCP down or issue a Reset-Req.
1394 if (sc->sc_flags & SC_DEBUG)
1395 if_printf(ifp, "decompress failed %d\n", rv);
1397 sc->sc_flags |= SC_VJ_RESET;
1398 if (rv == DECOMP_ERROR)
1399 sc->sc_flags |= SC_DC_ERROR;
1401 sc->sc_flags |= SC_DC_FERROR;
1406 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1407 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1409 if (proto == PPP_CCP) {
1415 ilen = m_length(m, NULL);
1418 if (sc->sc_flags & SC_VJ_RESET) {
1420 * If we've missed a packet, we must toss subsequent compressed
1421 * packets which don't have an explicit connection ID.
1424 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1426 sc->sc_flags &= ~SC_VJ_RESET;
1431 * See if we have a VJ-compressed packet to uncompress.
1433 if (proto == PPP_VJC_COMP) {
1434 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1437 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1438 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1439 sc->sc_comp, &iphdr, &hlen);
1442 if (sc->sc_flags & SC_DEBUG)
1443 if_printf(ifp, "VJ uncompress failed on type comp\n");
1447 /* Copy the PPP and IP headers into a new mbuf. */
1448 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1453 if (hlen + PPP_HDRLEN > MHLEN) {
1454 MCLGET(mp, M_DONTWAIT);
1455 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1457 goto bad; /* lose if big headers and no clusters */
1461 mac_copy_mbuf(m, mp);
1463 cp = mtod(mp, u_char *);
1469 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1470 mp->m_len = hlen + PPP_HDRLEN;
1473 * Trim the PPP and VJ headers off the old mbuf
1474 * and stick the new and old mbufs together.
1476 m->m_data += PPP_HDRLEN + xlen;
1477 m->m_len -= PPP_HDRLEN + xlen;
1478 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1479 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1480 mp->m_len += m->m_len;
1481 mp->m_next = m_free(m);
1486 ilen += hlen - xlen;
1488 } else if (proto == PPP_VJC_UNCOMP) {
1489 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1492 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1493 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1494 sc->sc_comp, &iphdr, &hlen);
1497 if (sc->sc_flags & SC_DEBUG)
1498 if_printf(ifp, "VJ uncompress failed on type uncomp\n");
1508 * If the packet will fit in a header mbuf, don't waste a
1509 * whole cluster on it.
1511 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1512 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1515 mac_copy_mbuf(m, mp);
1517 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1523 m->m_pkthdr.len = ilen;
1524 m->m_pkthdr.rcvif = ifp;
1526 if ((proto & 0x8000) == 0) {
1529 * See whether we want to pass this packet, and
1530 * if it counts as link activity.
1532 adrs = *mtod(m, u_char *); /* save address field */
1533 *mtod(m, u_char *) = 0; /* indicate inbound */
1534 if (sc->sc_pass_filt.bf_insns != 0
1535 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1537 /* drop this packet */
1541 if (sc->sc_active_filt.bf_insns == 0
1542 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1543 sc->sc_last_recv = time_uptime;
1545 *mtod(m, u_char *) = adrs;
1548 * Record the time that we received this packet.
1550 sc->sc_last_recv = time_uptime;
1551 #endif /* PPP_FILTER */
1554 /* See if bpf wants to look at the packet. */
1555 BPF_MTAP(PPP2IFP(sc), m);
1562 * IP packet - take off the ppp header and pass it up to IP.
1564 if ((ifp->if_flags & IFF_UP) == 0
1565 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1566 /* interface is down - drop the packet. */
1570 m->m_pkthdr.len -= PPP_HDRLEN;
1571 m->m_data += PPP_HDRLEN;
1572 m->m_len -= PPP_HDRLEN;
1573 if ((m = ip_fastforward(m)) == NULL)
1581 * IPX packet - take off the ppp header and pass it up to IPX.
1583 if ((PPP2IFP(sc)->if_flags & IFF_UP) == 0
1584 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1585 /* interface is down - drop the packet. */
1589 m->m_pkthdr.len -= PPP_HDRLEN;
1590 m->m_data += PPP_HDRLEN;
1591 m->m_len -= PPP_HDRLEN;
1593 sc->sc_last_recv = time_uptime; /* update time of last pkt rcvd */
1599 * Some other protocol - place on input queue for read().
1605 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1607 rv = netisr_queue(isr, m); /* (0) on success. */
1608 if ((isr == -1 && !rv) || (isr != -1 && rv)) {
1609 if (sc->sc_flags & SC_DEBUG)
1610 if_printf(ifp, "input queue full\n");
1616 ifp->if_ibytes += ilen;
1617 getmicrotime(&ifp->if_lastchange);
1627 PPP2IFP(sc)->if_ierrors++;
1628 sc->sc_stats.ppp_ierrors++;
1631 #define MAX_DUMP_BYTES 128
1637 char buf[3*MAX_DUMP_BYTES+4];
1641 for (m = m0; m; m = m->m_next) {
1643 u_char *rptr = (u_char *)m->m_data;
1646 if (bp > buf + (sizeof(buf) - 4))
1648 *bp++ = hex2ascii(*rptr >> 4);
1649 *bp++ = hex2ascii(*rptr++ & 0xf);
1653 if (bp > buf + (sizeof(buf) - 3))
1663 printf("%s\n", buf);