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>
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>
98 #include <sys/module.h>
101 #include <net/if_clone.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 <netipx/ipx.h>
115 #include <netipx/ipx_if.h>
119 #include <net/slcompress.h>
122 #include <net/if_ppp.h>
123 #include <net/if_pppvar.h>
125 #include <security/mac/mac_framework.h>
129 #define splsoftnet splnet
133 #define PACKETPTR struct mbuf *
134 #include <net/ppp_comp.h>
137 #define PPPNAME "ppp"
138 static MALLOC_DEFINE(M_PPP, PPPNAME, "PPP interface");
140 static struct mtx ppp_softc_list_mtx;
141 static LIST_HEAD(, ppp_softc) ppp_softc_list;
143 #define PPP_LIST_LOCK_INIT() mtx_init(&ppp_softc_list_mtx, \
144 "ppp_softc_list_mtx", NULL, MTX_DEF)
145 #define PPP_LIST_LOCK_DESTROY() mtx_destroy(&ppp_softc_list_mtx)
146 #define PPP_LIST_LOCK() mtx_lock(&ppp_softc_list_mtx)
147 #define PPP_LIST_UNLOCK() mtx_unlock(&ppp_softc_list_mtx)
149 /* XXX layering violation */
150 extern void pppasyncattach(void *);
151 extern void pppasyncdetach(void);
153 static int pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
154 static void pppintr(void);
156 static void ppp_requeue(struct ppp_softc *);
157 static void ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
158 static void ppp_ccp_closed(struct ppp_softc *);
159 static void ppp_inproc(struct ppp_softc *, struct mbuf *);
160 static void pppdumpm(struct mbuf *m0);
161 static int ppp_clone_create(struct if_clone *, int, caddr_t);
162 static void ppp_clone_destroy(struct ifnet *);
164 IFC_SIMPLE_DECLARE(ppp, 0);
167 * Some useful mbuf macros not in mbuf.h.
169 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
171 #define M_DATASTART(m) \
172 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
173 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
175 #define M_DATASIZE(m) \
176 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
177 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
180 * We steal two bits in the mbuf m_flags, to mark high-priority packets
181 * for output, and received packets following lost/corrupted packets.
183 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
184 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
189 * List of compressors we know about.
190 * We leave some space so maybe we can modload compressors.
193 extern struct compressor ppp_bsd_compress;
194 extern struct compressor ppp_deflate, ppp_deflate_draft;
196 static struct compressor *ppp_compressors[8] = {
197 #if defined(PPP_BSDCOMP)
200 #if defined(PPP_DEFLATE)
206 #endif /* PPP_COMPRESS */
209 ppp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
212 struct ppp_softc *sc;
214 sc = malloc(sizeof(struct ppp_softc), M_PPP, M_WAITOK | M_ZERO);
215 ifp = sc->sc_ifp = if_alloc(IFT_PPP);
222 if_initname(ifp, ifc->ifc_name, unit);
223 ifp->if_mtu = PPP_MTU;
224 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST | IFF_NEEDSGIANT;
225 ifp->if_hdrlen = PPP_HDRLEN;
226 ifp->if_ioctl = pppsioctl;
227 ifp->if_output = pppoutput;
228 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
229 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
230 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
231 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
232 mtx_init(&sc->sc_inq.ifq_mtx, "ppp_inq", NULL, MTX_DEF);
233 mtx_init(&sc->sc_fastq.ifq_mtx, "ppp_fastq", NULL, MTX_DEF);
234 mtx_init(&sc->sc_rawq.ifq_mtx, "ppp_rawq", NULL, MTX_DEF);
236 bpfattach(ifp, DLT_PPP, PPP_HDRLEN);
239 LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_list);
246 ppp_clone_destroy(struct ifnet *ifp)
248 struct ppp_softc *sc;
252 LIST_REMOVE(sc, sc_list);
258 mtx_destroy(&sc->sc_rawq.ifq_mtx);
259 mtx_destroy(&sc->sc_fastq.ifq_mtx);
260 mtx_destroy(&sc->sc_inq.ifq_mtx);
265 ppp_modevent(module_t mod, int type, void *data)
270 PPP_LIST_LOCK_INIT();
271 LIST_INIT(&ppp_softc_list);
272 if_clone_attach(&ppp_cloner);
274 netisr_register(NETISR_PPP, (netisr_t *)pppintr, NULL, 0);
276 * XXX layering violation - if_ppp can work over any lower
277 * level transport that cares to attach to it.
279 pppasyncattach(NULL);
282 /* XXX: layering violation */
285 netisr_unregister(NETISR_PPP);
287 if_clone_detach(&ppp_cloner);
288 PPP_LIST_LOCK_DESTROY();
296 static moduledata_t ppp_mod = {
302 DECLARE_MODULE(if_ppp, ppp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
305 * Allocate a ppp interface unit and initialize it.
312 char tmpname[IFNAMSIZ];
314 struct ppp_softc *sc;
317 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
318 if (sc->sc_xfer == pid) {
324 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
325 if (sc->sc_devp == NULL)
329 /* Try to clone an interface if we don't have a free one */
331 strcpy(tmpname, PPPNAME);
332 if (if_clone_create(tmpname, sizeof(tmpname), (caddr_t) 0) != 0)
334 ifp = ifunit(tmpname);
339 if (sc == NULL || sc->sc_devp != NULL)
343 sc->sc_mru = PPP_MRU;
344 sc->sc_relinq = NULL;
345 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
347 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
350 sl_compress_init(sc->sc_comp, -1);
353 sc->sc_xc_state = NULL;
354 sc->sc_rc_state = NULL;
355 #endif /* PPP_COMPRESS */
356 for (i = 0; i < NUM_NP; ++i)
357 sc->sc_npmode[i] = NPMODE_ERROR;
358 sc->sc_npqueue = NULL;
359 sc->sc_npqtail = &sc->sc_npqueue;
360 sc->sc_last_sent = sc->sc_last_recv = time_uptime;
366 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
370 struct ppp_softc *sc;
374 if_down(PPP2IFP(sc));
375 PPP2IFP(sc)->if_flags &= ~IFF_UP;
376 PPP2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
377 getmicrotime(&PPP2IFP(sc)->if_lastchange);
380 IF_DRAIN(&sc->sc_rawq);
381 IF_DRAIN(&sc->sc_inq);
382 IF_DRAIN(&sc->sc_fastq);
383 while ((m = sc->sc_npqueue) != NULL) {
384 sc->sc_npqueue = m->m_nextpkt;
389 sc->sc_xc_state = NULL;
390 sc->sc_rc_state = NULL;
391 #endif /* PPP_COMPRESS */
393 if (sc->sc_pass_filt.bf_insns != 0) {
394 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
395 sc->sc_pass_filt.bf_insns = 0;
396 sc->sc_pass_filt.bf_len = 0;
398 if (sc->sc_active_filt.bf_insns != 0) {
399 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
400 sc->sc_active_filt.bf_insns = 0;
401 sc->sc_active_filt.bf_len = 0;
403 #endif /* PPP_FILTER */
405 if (sc->sc_comp != 0) {
406 free(sc->sc_comp, M_DEVBUF);
413 * Ioctl routine for generic ppp devices.
416 pppioctl(sc, cmd, data, flag, td)
417 struct ppp_softc *sc;
423 struct proc *p = td->td_proc;
424 int s, flags, mru, npx;
427 struct ppp_option_data *odp;
428 struct compressor **cp;
432 struct bpf_program *bp, *nbp;
433 struct bpf_insn *newcode, *oldcode;
435 #endif /* PPP_FILTER */
437 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
442 *(int *)data = sc->sc_inq.ifq_len;
446 *(int *)data = PPP2IFP(sc)->if_dunit;
450 *(u_int *)data = sc->sc_flags;
454 if ((error = suser(td)) != 0)
456 flags = *(int *)data & SC_MASK;
459 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
463 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
468 if ((error = suser(td)) != 0)
471 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
476 *(int *)data = sc->sc_mru;
481 if ((error = suser(td)) != 0)
485 sl_compress_init(sc->sc_comp, *(int *)data);
492 if ((error = suser(td)) != 0)
494 sc->sc_xfer = p->p_pid;
498 case PPPIOCSCOMPRESS:
499 if ((error = suser(td)) != 0)
501 odp = (struct ppp_option_data *) data;
503 if (nb > sizeof(ccp_option))
504 nb = sizeof(ccp_option);
505 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
507 if (ccp_option[1] < 2) { /* preliminary check on the length byte */
511 for (cp = ppp_compressors; *cp != NULL; ++cp)
512 if ((*cp)->compress_proto == ccp_option[0]) {
514 * Found a handler for the protocol - try to allocate
515 * a compressor or decompressor.
520 if (sc->sc_xc_state != NULL)
521 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
523 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
524 if (sc->sc_xc_state == NULL) {
525 if (sc->sc_flags & SC_DEBUG)
526 if_printf(PPP2IFP(sc), "comp_alloc failed\n");
530 sc->sc_flags &= ~SC_COMP_RUN;
534 if (sc->sc_rc_state != NULL)
535 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
537 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
538 if (sc->sc_rc_state == NULL) {
539 if (sc->sc_flags & SC_DEBUG)
540 if_printf(PPP2IFP(sc), "decomp_alloc failed\n");
544 sc->sc_flags &= ~SC_DECOMP_RUN;
549 if (sc->sc_flags & SC_DEBUG)
550 if_printf(PPP2IFP(sc), "no compressor for [%x %x %x], %x\n",
551 ccp_option[0], ccp_option[1], ccp_option[2], nb);
552 error = EINVAL; /* no handler found */
554 #endif /* PPP_COMPRESS */
558 npi = (struct npioctl *) data;
559 npx = 0; /* XXX: quiet gcc */
560 switch (npi->protocol) {
569 if (cmd == PPPIOCGNPMODE) {
570 npi->mode = sc->sc_npmode[npx];
572 if ((error = suser(td)) != 0)
574 if (npi->mode != sc->sc_npmode[npx]) {
576 sc->sc_npmode[npx] = npi->mode;
577 if (npi->mode != NPMODE_QUEUE) {
589 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
590 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
597 nbp = (struct bpf_program *) data;
598 if ((unsigned) nbp->bf_len > BPF_MAXINSNS) {
602 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
603 if (newcodelen != 0) {
604 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
606 error = EINVAL; /* or sumpin */
609 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
611 free(newcode, M_DEVBUF);
614 if (!bpf_validate(newcode, nbp->bf_len)) {
615 free(newcode, M_DEVBUF);
621 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
622 oldcode = bp->bf_insns;
624 bp->bf_len = nbp->bf_len;
625 bp->bf_insns = newcode;
628 free(oldcode, M_DEVBUF);
640 * Process an ioctl request to the ppp network interface.
643 pppsioctl(ifp, cmd, data)
644 register struct ifnet *ifp;
648 struct thread *td = curthread; /* XXX */
649 register struct ppp_softc *sc = ifp->if_softc;
650 register struct ifaddr *ifa = (struct ifaddr *)data;
651 register struct ifreq *ifr = (struct ifreq *)data;
652 struct ppp_stats *psp;
654 struct ppp_comp_stats *pcp;
656 int s = splimp(), error = 0;
660 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
661 ifp->if_flags &= ~IFF_UP;
666 switch(ifa->ifa_addr->sa_family) {
676 error = EAFNOSUPPORT;
682 switch(ifa->ifa_addr->sa_family) {
692 error = EAFNOSUPPORT;
698 if ((error = suser(td)) != 0)
700 if (ifr->ifr_mtu > PPP_MAXMTU)
703 PPP2IFP(sc)->if_mtu = ifr->ifr_mtu;
705 (*sc->sc_setmtu)(sc);
710 ifr->ifr_mtu = PPP2IFP(sc)->if_mtu;
716 error = EAFNOSUPPORT;
719 switch(ifr->ifr_addr.sa_family) {
725 error = EAFNOSUPPORT;
731 psp = &((struct ifpppstatsreq *) data)->stats;
732 bzero(psp, sizeof(*psp));
733 psp->p = sc->sc_stats;
734 #if defined(VJC) && !defined(SL_NO_STATS)
736 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
737 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
738 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
739 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
740 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
741 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
742 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
743 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
750 pcp = &((struct ifpppcstatsreq *) data)->stats;
751 bzero(pcp, sizeof(*pcp));
752 if (sc->sc_xc_state != NULL)
753 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
754 if (sc->sc_rc_state != NULL)
755 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
757 #endif /* PPP_COMPRESS */
767 * Queue a packet. Start transmission if not active.
768 * Packet is placed in Information field of PPP frame.
769 * Called at splnet as the if->if_output handler.
770 * Called at splnet from pppwrite().
773 pppoutput(ifp, m0, dst, rtp)
776 struct sockaddr *dst;
779 register struct ppp_softc *sc = ifp->if_softc;
780 int protocol, address, control;
789 error = mac_check_ifnet_transmit(ifp, m0);
794 if (sc->sc_devp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0
795 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
796 error = ENETDOWN; /* sort of */
801 * Compute PPP header.
803 m0->m_flags &= ~M_HIGHPRI;
804 switch (dst->sa_family) {
807 address = PPP_ALLSTATIONS;
810 mode = sc->sc_npmode[NP_IP];
813 * If this packet has the "low delay" bit set in the IP header,
814 * put it on the fastq instead.
816 ip = mtod(m0, struct ip *);
817 if (ip->ip_tos & IPTOS_LOWDELAY)
818 m0->m_flags |= M_HIGHPRI;
824 * This is pretty bogus.. We dont have an ipxcp module in pppd
825 * yet to configure the link parameters. Sigh. I guess a
826 * manual ifconfig would do.... -Peter
828 address = PPP_ALLSTATIONS;
835 address = PPP_ADDRESS(dst->sa_data);
836 control = PPP_CONTROL(dst->sa_data);
837 protocol = PPP_PROTOCOL(dst->sa_data);
841 if_printf(ifp, "af%d not supported\n", dst->sa_family);
842 error = EAFNOSUPPORT;
847 * Drop this packet, or return an error, if necessary.
849 if (mode == NPMODE_ERROR) {
853 if (mode == NPMODE_DROP) {
859 * Add PPP header. If no space in first mbuf, allocate another.
860 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
862 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
863 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
870 m0->m_data -= PPP_HDRLEN;
872 cp = mtod(m0, u_char *);
875 *cp++ = protocol >> 8;
876 *cp++ = protocol & 0xff;
877 m0->m_len += PPP_HDRLEN;
879 len = m_length(m0, NULL);
881 if (sc->sc_flags & SC_LOG_OUTPKT) {
882 printf("%s output: ", ifp->if_xname);
886 if ((protocol & 0x8000) == 0) {
889 * Apply the pass and active filters to the packet,
890 * but only if it is a data packet.
892 *mtod(m0, u_char *) = 1; /* indicates outbound */
893 if (sc->sc_pass_filt.bf_insns != 0
894 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
896 error = 0; /* drop this packet */
901 * Update the time we sent the most recent packet.
903 if (sc->sc_active_filt.bf_insns == 0
904 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
905 sc->sc_last_sent = time_uptime;
907 *mtod(m0, u_char *) = address;
910 * Update the time we sent the most recent data packet.
912 sc->sc_last_sent = time_uptime;
913 #endif /* PPP_FILTER */
917 * See if bpf wants to look at the packet.
922 * Put the packet on the appropriate queue.
924 s = splsoftnet(); /* redundant */
925 if (mode == NPMODE_QUEUE) {
926 /* XXX we should limit the number of packets on this queue */
927 *sc->sc_npqtail = m0;
928 m0->m_nextpkt = NULL;
929 sc->sc_npqtail = &m0->m_nextpkt;
931 /* fastq and if_snd are emptied at spl[soft]net now */
932 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq:
933 (struct ifqueue *)&ifp->if_snd;
935 if (_IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
938 PPP2IFP(sc)->if_oerrors++;
939 sc->sc_stats.ppp_oerrors++;
943 _IF_ENQUEUE(ifq, m0);
947 getmicrotime(&ifp->if_lastchange);
949 ifp->if_obytes += len;
960 * After a change in the NPmode for some NP, move packets from the
961 * npqueue to the send queue or the fast queue as appropriate.
962 * Should be called at spl[soft]net.
966 struct ppp_softc *sc;
968 struct mbuf *m, **mpp;
972 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
973 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
975 mode = sc->sc_npmode[NP_IP];
984 * This packet can now go on one of the queues to be sent.
988 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq:
989 (struct ifqueue *)&PPP2IFP(sc)->if_snd;
990 if (! IF_HANDOFF(ifq, m, NULL)) {
991 PPP2IFP(sc)->if_oerrors++;
992 sc->sc_stats.ppp_oerrors++;
1003 mpp = &m->m_nextpkt;
1007 sc->sc_npqtail = mpp;
1011 * Transmitter has finished outputting some stuff;
1012 * remember to call sc->sc_start later at splsoftnet.
1016 struct ppp_softc *sc;
1020 sc->sc_flags &= ~SC_TBUSY;
1021 schednetisr(NETISR_PPP);
1027 * Get a packet to send. This procedure is intended to be called at
1028 * splsoftnet, since it may involve time-consuming operations such as
1029 * applying VJ compression, packet compression, address/control and/or
1030 * protocol field compression to the packet.
1034 struct ppp_softc *sc;
1036 struct mbuf *m, *mp;
1038 int address, control, protocol;
1041 * Grab a packet to send: first try the fast queue, then the
1044 IF_DEQUEUE(&sc->sc_fastq, m);
1046 IF_DEQUEUE(&PPP2IFP(sc)->if_snd, m);
1050 ++sc->sc_stats.ppp_opackets;
1053 * Extract the ppp header of the new packet.
1054 * The ppp header will be in one mbuf.
1056 cp = mtod(m, u_char *);
1057 address = PPP_ADDRESS(cp);
1058 control = PPP_CONTROL(cp);
1059 protocol = PPP_PROTOCOL(cp);
1065 * If the packet is a TCP/IP packet, see if we can compress it.
1067 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1072 ip = (struct ip *) (cp + PPP_HDRLEN);
1073 if (mp->m_len <= PPP_HDRLEN) {
1077 ip = mtod(mp, struct ip *);
1079 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1080 if (ip->ip_p == IPPROTO_TCP) {
1081 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1082 !(sc->sc_flags & SC_NO_TCP_CCID));
1084 case TYPE_UNCOMPRESSED_TCP:
1085 protocol = PPP_VJC_UNCOMP;
1087 case TYPE_COMPRESSED_TCP:
1088 protocol = PPP_VJC_COMP;
1089 cp = mtod(m, u_char *);
1090 cp[0] = address; /* header has moved */
1095 cp[3] = protocol; /* update protocol in PPP header */
1105 #endif /* PPP_COMPRESS */
1109 if (protocol != PPP_LCP && protocol != PPP_CCP
1110 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1111 struct mbuf *mcomp = NULL;
1114 slen = m_length(m, NULL);
1115 clen = (*sc->sc_xcomp->compress)
1116 (sc->sc_xc_state, &mcomp, m, slen, PPP2IFP(sc)->if_mtu + PPP_HDRLEN);
1117 if (mcomp != NULL) {
1118 if (sc->sc_flags & SC_CCP_UP) {
1119 /* Send the compressed packet instead of the original. */
1122 cp = mtod(m, u_char *);
1125 /* Can't transmit compressed packets until CCP is up. */
1130 #endif /* PPP_COMPRESS */
1133 * Compress the address/control and protocol, if possible.
1135 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1136 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1137 protocol != PPP_LCP) {
1138 /* can compress address/control */
1142 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1143 /* can compress protocol */
1144 if (mtod(m, u_char *) == cp) {
1145 cp[2] = cp[1]; /* move address/control up */
1156 * Software interrupt routine, called at spl[soft]net.
1161 struct ppp_softc *sc;
1168 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
1170 if (!(sc->sc_flags & SC_TBUSY)
1171 && (PPP2IFP(sc)->if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1172 sc->sc_flags |= SC_TBUSY;
1174 (*sc->sc_start)(sc);
1179 IF_DEQUEUE(&sc->sc_rawq, m);
1184 mac_create_mbuf_from_ifnet(PPP2IFP(sc), m);
1194 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1195 * 0 if it is about to be transmitted.
1198 ppp_ccp(sc, m, rcvd)
1199 struct ppp_softc *sc;
1208 * Get a pointer to the data after the PPP header.
1210 if (m->m_len <= PPP_HDRLEN) {
1214 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1217 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1220 ep = mtod(mp, u_char *) + mp->m_len;
1221 if (dp + CCP_HDRLEN > ep)
1223 slen = CCP_LENGTH(dp);
1224 if (dp + slen > ep) {
1225 if (sc->sc_flags & SC_DEBUG)
1226 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1227 dp, slen, mtod(mp, u_char *), mp->m_len);
1231 switch (CCP_CODE(dp)) {
1235 /* CCP must be going down - disable compression */
1236 if (sc->sc_flags & SC_CCP_UP) {
1238 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1244 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1245 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1246 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1248 /* we're agreeing to send compressed packets. */
1249 if (sc->sc_xc_state != NULL
1250 && (*sc->sc_xcomp->comp_init)
1251 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1252 PPP2IFP(sc)->if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1254 sc->sc_flags |= SC_COMP_RUN;
1258 /* peer is agreeing to send compressed packets. */
1259 if (sc->sc_rc_state != NULL
1260 && (*sc->sc_rcomp->decomp_init)
1261 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1262 PPP2IFP(sc)->if_dunit, 0, sc->sc_mru,
1263 sc->sc_flags & SC_DEBUG)) {
1265 sc->sc_flags |= SC_DECOMP_RUN;
1266 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1274 if (sc->sc_flags & SC_CCP_UP) {
1276 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1277 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1279 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1280 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1282 sc->sc_flags &= ~SC_DC_ERROR;
1292 * CCP is down; free (de)compressor state if necessary.
1296 struct ppp_softc *sc;
1298 if (sc->sc_xc_state) {
1299 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1300 sc->sc_xc_state = NULL;
1302 if (sc->sc_rc_state) {
1303 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1304 sc->sc_rc_state = NULL;
1307 #endif /* PPP_COMPRESS */
1310 * PPP packet input routine.
1311 * The caller has checked and removed the FCS and has inserted
1312 * the address/control bytes and the protocol high byte if they
1316 ppppktin(sc, m, lost)
1317 struct ppp_softc *sc;
1324 m->m_flags |= M_ERRMARK;
1325 IF_ENQUEUE(&sc->sc_rawq, m);
1326 schednetisr(NETISR_PPP);
1331 * Process a received PPP packet, doing decompression as necessary.
1332 * Should be called at splsoftnet.
1334 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1335 TYPE_UNCOMPRESSED_TCP)
1339 struct ppp_softc *sc;
1342 struct ifnet *ifp = PPP2IFP(sc);
1344 int s, ilen = 0, xlen, proto, rv;
1345 u_char *cp, adrs, ctrl;
1346 struct mbuf *mp, *dmp = NULL;
1350 sc->sc_stats.ppp_ipackets++;
1352 if (sc->sc_flags & SC_LOG_INPKT) {
1353 ilen = m_length(m, NULL);
1354 if_printf(ifp, "got %d bytes\n", ilen);
1358 cp = mtod(m, u_char *);
1359 adrs = PPP_ADDRESS(cp);
1360 ctrl = PPP_CONTROL(cp);
1361 proto = PPP_PROTOCOL(cp);
1363 if (m->m_flags & M_ERRMARK) {
1364 m->m_flags &= ~M_ERRMARK;
1366 sc->sc_flags |= SC_VJ_RESET;
1372 * Decompress this packet if necessary, update the receiver's
1373 * dictionary, or take appropriate action on a CCP packet.
1375 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1376 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1377 /* decompress this packet */
1378 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1379 if (rv == DECOMP_OK) {
1382 /* no error, but no decompressed packet produced */
1386 cp = mtod(m, u_char *);
1387 proto = PPP_PROTOCOL(cp);
1391 * An error has occurred in decompression.
1392 * Pass the compressed packet up to pppd, which may take
1393 * CCP down or issue a Reset-Req.
1395 if (sc->sc_flags & SC_DEBUG)
1396 if_printf(ifp, "decompress failed %d\n", rv);
1398 sc->sc_flags |= SC_VJ_RESET;
1399 if (rv == DECOMP_ERROR)
1400 sc->sc_flags |= SC_DC_ERROR;
1402 sc->sc_flags |= SC_DC_FERROR;
1407 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1408 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1410 if (proto == PPP_CCP) {
1416 ilen = m_length(m, NULL);
1419 if (sc->sc_flags & SC_VJ_RESET) {
1421 * If we've missed a packet, we must toss subsequent compressed
1422 * packets which don't have an explicit connection ID.
1425 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1427 sc->sc_flags &= ~SC_VJ_RESET;
1432 * See if we have a VJ-compressed packet to uncompress.
1434 if (proto == PPP_VJC_COMP) {
1435 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1438 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1439 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1440 sc->sc_comp, &iphdr, &hlen);
1443 if (sc->sc_flags & SC_DEBUG)
1444 if_printf(ifp, "VJ uncompress failed on type comp\n");
1448 /* Copy the PPP and IP headers into a new mbuf. */
1449 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1454 if (hlen + PPP_HDRLEN > MHLEN) {
1455 MCLGET(mp, M_DONTWAIT);
1456 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1458 goto bad; /* lose if big headers and no clusters */
1462 mac_copy_mbuf(m, mp);
1464 cp = mtod(mp, u_char *);
1470 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1471 mp->m_len = hlen + PPP_HDRLEN;
1474 * Trim the PPP and VJ headers off the old mbuf
1475 * and stick the new and old mbufs together.
1477 m->m_data += PPP_HDRLEN + xlen;
1478 m->m_len -= PPP_HDRLEN + xlen;
1479 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1480 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1481 mp->m_len += m->m_len;
1482 mp->m_next = m_free(m);
1487 ilen += hlen - xlen;
1489 } else if (proto == PPP_VJC_UNCOMP) {
1490 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1493 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1494 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1495 sc->sc_comp, &iphdr, &hlen);
1498 if (sc->sc_flags & SC_DEBUG)
1499 if_printf(ifp, "VJ uncompress failed on type uncomp\n");
1509 * If the packet will fit in a header mbuf, don't waste a
1510 * whole cluster on it.
1512 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1513 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1516 mac_copy_mbuf(m, mp);
1518 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1524 m->m_pkthdr.len = ilen;
1525 m->m_pkthdr.rcvif = ifp;
1527 if ((proto & 0x8000) == 0) {
1530 * See whether we want to pass this packet, and
1531 * if it counts as link activity.
1533 adrs = *mtod(m, u_char *); /* save address field */
1534 *mtod(m, u_char *) = 0; /* indicate inbound */
1535 if (sc->sc_pass_filt.bf_insns != 0
1536 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1538 /* drop this packet */
1542 if (sc->sc_active_filt.bf_insns == 0
1543 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1544 sc->sc_last_recv = time_uptime;
1546 *mtod(m, u_char *) = adrs;
1549 * Record the time that we received this packet.
1551 sc->sc_last_recv = time_uptime;
1552 #endif /* PPP_FILTER */
1555 /* See if bpf wants to look at the packet. */
1556 BPF_MTAP(PPP2IFP(sc), m);
1563 * IP packet - take off the ppp header and pass it up to IP.
1565 if ((ifp->if_flags & IFF_UP) == 0
1566 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1567 /* interface is down - drop the packet. */
1571 m->m_pkthdr.len -= PPP_HDRLEN;
1572 m->m_data += PPP_HDRLEN;
1573 m->m_len -= PPP_HDRLEN;
1574 if ((m = ip_fastforward(m)) == NULL)
1582 * IPX packet - take off the ppp header and pass it up to IPX.
1584 if ((PPP2IFP(sc)->if_flags & IFF_UP) == 0
1585 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1586 /* interface is down - drop the packet. */
1590 m->m_pkthdr.len -= PPP_HDRLEN;
1591 m->m_data += PPP_HDRLEN;
1592 m->m_len -= PPP_HDRLEN;
1594 sc->sc_last_recv = time_uptime; /* update time of last pkt rcvd */
1600 * Some other protocol - place on input queue for read().
1606 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1608 rv = netisr_queue(isr, m); /* (0) on success. */
1609 if ((isr == -1 && !rv) || (isr != -1 && rv)) {
1610 if (sc->sc_flags & SC_DEBUG)
1611 if_printf(ifp, "input queue full\n");
1617 ifp->if_ibytes += ilen;
1618 getmicrotime(&ifp->if_lastchange);
1628 PPP2IFP(sc)->if_ierrors++;
1629 sc->sc_stats.ppp_ierrors++;
1632 #define MAX_DUMP_BYTES 128
1638 char buf[3*MAX_DUMP_BYTES+4];
1642 for (m = m0; m; m = m->m_next) {
1644 u_char *rptr = (u_char *)m->m_data;
1647 if (bp > buf + (sizeof(buf) - 4))
1649 *bp++ = hex2ascii(*rptr >> 4);
1650 *bp++ = hex2ascii(*rptr++ & 0xf);
1654 if (bp > buf + (sizeof(buf) - 3))
1664 printf("%s\n", buf);