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 */
79 #include "opt_inet6.h"
89 #include <sys/param.h>
90 #include <sys/systm.h>
94 #include <sys/socket.h>
95 #include <sys/filio.h>
96 #include <sys/sockio.h>
97 #include <sys/kernel.h>
99 #include <sys/malloc.h>
100 #include <sys/module.h>
103 #include <net/if_clone.h>
104 #include <net/if_types.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 <netipx/ipx.h>
117 #include <netipx/ipx_if.h>
121 #include <net/slcompress.h>
124 #include <net/if_ppp.h>
125 #include <net/if_pppvar.h>
127 #include <security/mac/mac_framework.h>
131 #define splsoftnet splnet
135 #define PACKETPTR struct mbuf *
136 #include <net/ppp_comp.h>
139 #define PPPNAME "ppp"
140 static MALLOC_DEFINE(M_PPP, PPPNAME, "PPP interface");
142 static struct mtx ppp_softc_list_mtx;
143 static LIST_HEAD(, ppp_softc) ppp_softc_list;
145 #define PPP_LIST_LOCK_INIT() mtx_init(&ppp_softc_list_mtx, \
146 "ppp_softc_list_mtx", NULL, MTX_DEF)
147 #define PPP_LIST_LOCK_DESTROY() mtx_destroy(&ppp_softc_list_mtx)
148 #define PPP_LIST_LOCK() mtx_lock(&ppp_softc_list_mtx)
149 #define PPP_LIST_UNLOCK() mtx_unlock(&ppp_softc_list_mtx)
151 /* XXX layering violation */
152 extern void pppasyncattach(void *);
153 extern void pppasyncdetach(void);
155 static int pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
156 static void pppintr(void);
158 static void ppp_requeue(struct ppp_softc *);
159 static void ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
160 static void ppp_ccp_closed(struct ppp_softc *);
161 static void ppp_inproc(struct ppp_softc *, struct mbuf *);
162 static void pppdumpm(struct mbuf *m0);
163 static int ppp_clone_create(struct if_clone *, int, caddr_t);
164 static void ppp_clone_destroy(struct ifnet *);
166 IFC_SIMPLE_DECLARE(ppp, 0);
169 * Some useful mbuf macros not in mbuf.h.
171 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
173 #define M_DATASTART(m) \
174 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
175 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
177 #define M_DATASIZE(m) \
178 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
179 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
182 * We steal two bits in the mbuf m_flags, to mark high-priority packets
183 * for output, and received packets following lost/corrupted packets.
185 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
186 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
191 * List of compressors we know about.
192 * We leave some space so maybe we can modload compressors.
195 extern struct compressor ppp_bsd_compress;
196 extern struct compressor ppp_deflate, ppp_deflate_draft;
198 static struct compressor *ppp_compressors[8] = {
199 #if defined(PPP_BSDCOMP)
202 #if defined(PPP_DEFLATE)
208 #endif /* PPP_COMPRESS */
211 ppp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
214 struct ppp_softc *sc;
216 sc = malloc(sizeof(struct ppp_softc), M_PPP, M_WAITOK | M_ZERO);
217 ifp = sc->sc_ifp = if_alloc(IFT_PPP);
223 callout_init(&sc->sc_timo_ch, 0);
225 if_initname(ifp, ifc->ifc_name, unit);
226 ifp->if_mtu = PPP_MTU;
227 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST | IFF_NEEDSGIANT;
228 ifp->if_hdrlen = PPP_HDRLEN;
229 ifp->if_ioctl = pppsioctl;
230 ifp->if_output = pppoutput;
231 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
232 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
233 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
234 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
235 mtx_init(&sc->sc_inq.ifq_mtx, "ppp_inq", NULL, MTX_DEF);
236 mtx_init(&sc->sc_fastq.ifq_mtx, "ppp_fastq", NULL, MTX_DEF);
237 mtx_init(&sc->sc_rawq.ifq_mtx, "ppp_rawq", NULL, MTX_DEF);
239 bpfattach(ifp, DLT_PPP, PPP_HDRLEN);
242 LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_list);
249 ppp_clone_destroy(struct ifnet *ifp)
251 struct ppp_softc *sc;
255 LIST_REMOVE(sc, sc_list);
261 mtx_destroy(&sc->sc_rawq.ifq_mtx);
262 mtx_destroy(&sc->sc_fastq.ifq_mtx);
263 mtx_destroy(&sc->sc_inq.ifq_mtx);
268 ppp_modevent(module_t mod, int type, void *data)
273 PPP_LIST_LOCK_INIT();
274 LIST_INIT(&ppp_softc_list);
275 if_clone_attach(&ppp_cloner);
277 netisr_register(NETISR_PPP, (netisr_t *)pppintr, NULL,
280 * XXX layering violation - if_ppp can work over any lower
281 * level transport that cares to attach to it.
283 pppasyncattach(NULL);
286 /* XXX: layering violation */
289 netisr_unregister(NETISR_PPP);
291 if_clone_detach(&ppp_cloner);
292 PPP_LIST_LOCK_DESTROY();
300 static moduledata_t ppp_mod = {
306 DECLARE_MODULE(if_ppp, ppp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
309 * Allocate a ppp interface unit and initialize it.
316 char tmpname[IFNAMSIZ];
318 struct ppp_softc *sc;
321 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
322 if (sc->sc_xfer == pid) {
328 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
329 if (sc->sc_devp == NULL)
333 /* Try to clone an interface if we don't have a free one */
335 strcpy(tmpname, PPPNAME);
336 if (if_clone_create(tmpname, sizeof(tmpname), (caddr_t) 0) != 0)
338 ifp = ifunit(tmpname);
343 if (sc == NULL || sc->sc_devp != NULL)
347 sc->sc_mru = PPP_MRU;
348 sc->sc_relinq = NULL;
349 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
351 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
354 sl_compress_init(sc->sc_comp, -1);
357 sc->sc_xc_state = NULL;
358 sc->sc_rc_state = NULL;
359 #endif /* PPP_COMPRESS */
360 for (i = 0; i < NUM_NP; ++i)
361 sc->sc_npmode[i] = NPMODE_ERROR;
362 sc->sc_npqueue = NULL;
363 sc->sc_npqtail = &sc->sc_npqueue;
364 sc->sc_last_sent = sc->sc_last_recv = time_uptime;
370 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
374 struct ppp_softc *sc;
378 if_down(PPP2IFP(sc));
379 PPP2IFP(sc)->if_flags &= ~IFF_UP;
380 PPP2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
381 getmicrotime(&PPP2IFP(sc)->if_lastchange);
384 IF_DRAIN(&sc->sc_rawq);
385 IF_DRAIN(&sc->sc_inq);
386 IF_DRAIN(&sc->sc_fastq);
387 while ((m = sc->sc_npqueue) != NULL) {
388 sc->sc_npqueue = m->m_nextpkt;
393 sc->sc_xc_state = NULL;
394 sc->sc_rc_state = NULL;
395 #endif /* PPP_COMPRESS */
397 if (sc->sc_pass_filt.bf_insns != 0) {
398 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
399 sc->sc_pass_filt.bf_insns = 0;
400 sc->sc_pass_filt.bf_len = 0;
402 if (sc->sc_active_filt.bf_insns != 0) {
403 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
404 sc->sc_active_filt.bf_insns = 0;
405 sc->sc_active_filt.bf_len = 0;
407 #endif /* PPP_FILTER */
409 if (sc->sc_comp != 0) {
410 free(sc->sc_comp, M_DEVBUF);
417 * Ioctl routine for generic ppp devices.
420 pppioctl(sc, cmd, data, flag, td)
421 struct ppp_softc *sc;
427 struct proc *p = td->td_proc;
428 int s, flags, mru, npx;
431 struct ppp_option_data *odp;
432 struct compressor **cp;
436 struct bpf_program *bp, *nbp;
437 struct bpf_insn *newcode, *oldcode;
439 #endif /* PPP_FILTER */
441 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
446 *(int *)data = sc->sc_inq.ifq_len;
450 *(int *)data = PPP2IFP(sc)->if_dunit;
454 *(u_int *)data = sc->sc_flags;
458 error = priv_check(td, PRIV_NET_PPP);
461 flags = *(int *)data & SC_MASK;
464 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
468 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
473 error = priv_check(td, PRIV_NET_PPP);
477 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
482 *(int *)data = sc->sc_mru;
487 error = priv_check(td, PRIV_NET_PPP);
492 sl_compress_init(sc->sc_comp, *(int *)data);
499 error = priv_check(td, PRIV_NET_PPP);
502 sc->sc_xfer = p->p_pid;
506 case PPPIOCSCOMPRESS:
507 error = priv_check(td, PRIV_NET_PPP);
510 odp = (struct ppp_option_data *) data;
512 if (nb > sizeof(ccp_option))
513 nb = sizeof(ccp_option);
514 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
516 if (ccp_option[1] < 2) { /* preliminary check on the length byte */
520 for (cp = ppp_compressors; *cp != NULL; ++cp)
521 if ((*cp)->compress_proto == ccp_option[0]) {
523 * Found a handler for the protocol - try to allocate
524 * a compressor or decompressor.
529 if (sc->sc_xc_state != NULL)
530 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
532 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
533 if (sc->sc_xc_state == NULL) {
534 if (sc->sc_flags & SC_DEBUG)
535 if_printf(PPP2IFP(sc), "comp_alloc failed\n");
539 sc->sc_flags &= ~SC_COMP_RUN;
543 if (sc->sc_rc_state != NULL)
544 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
546 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
547 if (sc->sc_rc_state == NULL) {
548 if (sc->sc_flags & SC_DEBUG)
549 if_printf(PPP2IFP(sc), "decomp_alloc failed\n");
553 sc->sc_flags &= ~SC_DECOMP_RUN;
558 if (sc->sc_flags & SC_DEBUG)
559 if_printf(PPP2IFP(sc), "no compressor for [%x %x %x], %x\n",
560 ccp_option[0], ccp_option[1], ccp_option[2], nb);
561 error = EINVAL; /* no handler found */
563 #endif /* PPP_COMPRESS */
567 npi = (struct npioctl *) data;
568 npx = 0; /* XXX: quiet gcc */
569 switch (npi->protocol) {
581 if (cmd == PPPIOCGNPMODE) {
582 npi->mode = sc->sc_npmode[npx];
584 error = priv_check(td, PRIV_NET_PPP);
587 if (npi->mode != sc->sc_npmode[npx]) {
589 sc->sc_npmode[npx] = npi->mode;
590 if (npi->mode != NPMODE_QUEUE) {
602 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
603 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
610 nbp = (struct bpf_program *) data;
611 if ((unsigned) nbp->bf_len > BPF_MAXINSNS) {
615 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
616 if (newcodelen != 0) {
617 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
619 error = EINVAL; /* or sumpin */
622 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
624 free(newcode, M_DEVBUF);
627 if (!bpf_validate(newcode, nbp->bf_len)) {
628 free(newcode, M_DEVBUF);
634 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
635 oldcode = bp->bf_insns;
637 bp->bf_len = nbp->bf_len;
638 bp->bf_insns = newcode;
641 free(oldcode, M_DEVBUF);
653 * Process an ioctl request to the ppp network interface.
656 pppsioctl(ifp, cmd, data)
657 register struct ifnet *ifp;
661 struct thread *td = curthread; /* XXX */
662 register struct ppp_softc *sc = ifp->if_softc;
663 register struct ifaddr *ifa = (struct ifaddr *)data;
664 register struct ifreq *ifr = (struct ifreq *)data;
665 struct ppp_stats *psp;
667 struct ppp_comp_stats *pcp;
669 int s = splimp(), error = 0;
673 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
674 ifp->if_flags &= ~IFF_UP;
679 switch(ifa->ifa_addr->sa_family) {
693 error = EAFNOSUPPORT;
699 switch(ifa->ifa_addr->sa_family) {
713 error = EAFNOSUPPORT;
720 * XXXRW: Isn't this priv_check() check redundant to the one at the
723 error = priv_check(td, PRIV_NET_SETIFMTU);
726 if (ifr->ifr_mtu > PPP_MAXMTU)
729 PPP2IFP(sc)->if_mtu = ifr->ifr_mtu;
731 (*sc->sc_setmtu)(sc);
736 ifr->ifr_mtu = PPP2IFP(sc)->if_mtu;
742 error = EAFNOSUPPORT;
745 switch(ifr->ifr_addr.sa_family) {
755 error = EAFNOSUPPORT;
761 psp = &((struct ifpppstatsreq *) data)->stats;
762 bzero(psp, sizeof(*psp));
763 psp->p = sc->sc_stats;
764 #if defined(VJC) && !defined(SL_NO_STATS)
766 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
767 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
768 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
769 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
770 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
771 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
772 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
773 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
780 pcp = &((struct ifpppcstatsreq *) data)->stats;
781 bzero(pcp, sizeof(*pcp));
782 if (sc->sc_xc_state != NULL)
783 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
784 if (sc->sc_rc_state != NULL)
785 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
787 #endif /* PPP_COMPRESS */
797 * Queue a packet. Start transmission if not active.
798 * Packet is placed in Information field of PPP frame.
799 * Called at splnet as the if->if_output handler.
800 * Called at splnet from pppwrite().
803 pppoutput(ifp, m0, dst, rtp)
806 struct sockaddr *dst;
809 register struct ppp_softc *sc = ifp->if_softc;
810 int protocol, address, control;
819 error = mac_ifnet_check_transmit(ifp, m0);
824 if (sc->sc_devp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0
825 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
826 error = ENETDOWN; /* sort of */
831 * Compute PPP header.
833 m0->m_flags &= ~M_HIGHPRI;
834 switch (dst->sa_family) {
837 address = PPP_ALLSTATIONS;
840 mode = sc->sc_npmode[NP_IP];
843 * If this packet has the "low delay" bit set in the IP header,
844 * put it on the fastq instead.
846 ip = mtod(m0, struct ip *);
847 if (ip->ip_tos & IPTOS_LOWDELAY)
848 m0->m_flags |= M_HIGHPRI;
853 address = PPP_ALLSTATIONS; /*XXX*/
854 control = PPP_UI; /*XXX*/
856 mode = sc->sc_npmode[NP_IPV6];
858 #if 0 /* XXX flowinfo/traffic class, maybe? */
860 * If this packet has the "low delay" bit set in the IP header,
861 * put it on the fastq instead.
863 ip = mtod(m0, struct ip *);
864 if (ip->ip_tos & IPTOS_LOWDELAY)
865 m0->m_flags |= M_HIGHPRI;
872 * This is pretty bogus.. We dont have an ipxcp module in pppd
873 * yet to configure the link parameters. Sigh. I guess a
874 * manual ifconfig would do.... -Peter
876 address = PPP_ALLSTATIONS;
883 address = PPP_ADDRESS(dst->sa_data);
884 control = PPP_CONTROL(dst->sa_data);
885 protocol = PPP_PROTOCOL(dst->sa_data);
889 if_printf(ifp, "af%d not supported\n", dst->sa_family);
890 error = EAFNOSUPPORT;
895 * Drop this packet, or return an error, if necessary.
897 if (mode == NPMODE_ERROR) {
901 if (mode == NPMODE_DROP) {
907 * Add PPP header. If no space in first mbuf, allocate another.
908 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
910 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
911 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
918 m0->m_data -= PPP_HDRLEN;
920 cp = mtod(m0, u_char *);
923 *cp++ = protocol >> 8;
924 *cp++ = protocol & 0xff;
925 m0->m_len += PPP_HDRLEN;
927 len = m_length(m0, NULL);
929 if (sc->sc_flags & SC_LOG_OUTPKT) {
930 printf("%s output: ", ifp->if_xname);
934 if ((protocol & 0x8000) == 0) {
937 * Apply the pass and active filters to the packet,
938 * but only if it is a data packet.
940 *mtod(m0, u_char *) = 1; /* indicates outbound */
941 if (sc->sc_pass_filt.bf_insns != 0
942 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
944 error = 0; /* drop this packet */
949 * Update the time we sent the most recent packet.
951 if (sc->sc_active_filt.bf_insns == 0
952 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
953 sc->sc_last_sent = time_uptime;
955 *mtod(m0, u_char *) = address;
958 * Update the time we sent the most recent data packet.
960 sc->sc_last_sent = time_uptime;
961 #endif /* PPP_FILTER */
965 * See if bpf wants to look at the packet.
970 * Put the packet on the appropriate queue.
972 s = splsoftnet(); /* redundant */
973 if (mode == NPMODE_QUEUE) {
974 /* XXX we should limit the number of packets on this queue */
975 *sc->sc_npqtail = m0;
976 m0->m_nextpkt = NULL;
977 sc->sc_npqtail = &m0->m_nextpkt;
979 /* fastq and if_snd are emptied at spl[soft]net now */
980 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq:
981 (struct ifqueue *)&ifp->if_snd;
983 if (_IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
986 PPP2IFP(sc)->if_oerrors++;
987 sc->sc_stats.ppp_oerrors++;
991 _IF_ENQUEUE(ifq, m0);
995 getmicrotime(&ifp->if_lastchange);
997 ifp->if_obytes += len;
1008 * After a change in the NPmode for some NP, move packets from the
1009 * npqueue to the send queue or the fast queue as appropriate.
1010 * Should be called at spl[soft]net.
1014 struct ppp_softc *sc;
1016 struct mbuf *m, **mpp;
1017 struct ifqueue *ifq;
1020 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1021 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1023 mode = sc->sc_npmode[NP_IP];
1026 mode = sc->sc_npmode[NP_IPV6];
1035 * This packet can now go on one of the queues to be sent.
1037 *mpp = m->m_nextpkt;
1038 m->m_nextpkt = NULL;
1039 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq:
1040 (struct ifqueue *)&PPP2IFP(sc)->if_snd;
1041 if (! IF_HANDOFF(ifq, m, NULL)) {
1042 PPP2IFP(sc)->if_oerrors++;
1043 sc->sc_stats.ppp_oerrors++;
1049 *mpp = m->m_nextpkt;
1054 mpp = &m->m_nextpkt;
1058 sc->sc_npqtail = mpp;
1062 * Transmitter has finished outputting some stuff;
1063 * remember to call sc->sc_start later at splsoftnet.
1067 struct ppp_softc *sc;
1071 sc->sc_flags &= ~SC_TBUSY;
1072 schednetisr(NETISR_PPP);
1078 * Get a packet to send. This procedure is intended to be called at
1079 * splsoftnet, since it may involve time-consuming operations such as
1080 * applying VJ compression, packet compression, address/control and/or
1081 * protocol field compression to the packet.
1085 struct ppp_softc *sc;
1087 struct mbuf *m, *mp;
1089 int address, control, protocol;
1092 * Grab a packet to send: first try the fast queue, then the
1095 IF_DEQUEUE(&sc->sc_fastq, m);
1097 IF_DEQUEUE(&PPP2IFP(sc)->if_snd, m);
1101 ++sc->sc_stats.ppp_opackets;
1104 * Extract the ppp header of the new packet.
1105 * The ppp header will be in one mbuf.
1107 cp = mtod(m, u_char *);
1108 address = PPP_ADDRESS(cp);
1109 control = PPP_CONTROL(cp);
1110 protocol = PPP_PROTOCOL(cp);
1116 * If the packet is a TCP/IP packet, see if we can compress it.
1118 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1123 ip = (struct ip *) (cp + PPP_HDRLEN);
1124 if (mp->m_len <= PPP_HDRLEN) {
1128 ip = mtod(mp, struct ip *);
1130 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1131 if (ip->ip_p == IPPROTO_TCP) {
1132 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1133 !(sc->sc_flags & SC_NO_TCP_CCID));
1135 case TYPE_UNCOMPRESSED_TCP:
1136 protocol = PPP_VJC_UNCOMP;
1138 case TYPE_COMPRESSED_TCP:
1139 protocol = PPP_VJC_COMP;
1140 cp = mtod(m, u_char *);
1141 cp[0] = address; /* header has moved */
1146 cp[3] = protocol; /* update protocol in PPP header */
1156 #endif /* PPP_COMPRESS */
1160 if (protocol != PPP_LCP && protocol != PPP_CCP
1161 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1162 struct mbuf *mcomp = NULL;
1165 slen = m_length(m, NULL);
1166 clen = (*sc->sc_xcomp->compress)
1167 (sc->sc_xc_state, &mcomp, m, slen, PPP2IFP(sc)->if_mtu + PPP_HDRLEN);
1168 if (mcomp != NULL) {
1169 if (sc->sc_flags & SC_CCP_UP) {
1170 /* Send the compressed packet instead of the original. */
1173 cp = mtod(m, u_char *);
1176 /* Can't transmit compressed packets until CCP is up. */
1181 #endif /* PPP_COMPRESS */
1184 * Compress the address/control and protocol, if possible.
1186 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1187 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1188 protocol != PPP_LCP) {
1189 /* can compress address/control */
1193 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1194 /* can compress protocol */
1195 if (mtod(m, u_char *) == cp) {
1196 cp[2] = cp[1]; /* move address/control up */
1207 * Software interrupt routine, called at spl[soft]net.
1212 struct ppp_softc *sc;
1218 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
1220 if (!(sc->sc_flags & SC_TBUSY)
1221 && (PPP2IFP(sc)->if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1222 sc->sc_flags |= SC_TBUSY;
1224 (*sc->sc_start)(sc);
1229 IF_DEQUEUE(&sc->sc_rawq, m);
1234 mac_ifnet_create_mbuf(PPP2IFP(sc), m);
1245 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1246 * 0 if it is about to be transmitted.
1249 ppp_ccp(sc, m, rcvd)
1250 struct ppp_softc *sc;
1259 * Get a pointer to the data after the PPP header.
1261 if (m->m_len <= PPP_HDRLEN) {
1265 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1268 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1271 ep = mtod(mp, u_char *) + mp->m_len;
1272 if (dp + CCP_HDRLEN > ep)
1274 slen = CCP_LENGTH(dp);
1275 if (dp + slen > ep) {
1276 if (sc->sc_flags & SC_DEBUG)
1277 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1278 dp, slen, mtod(mp, u_char *), mp->m_len);
1282 switch (CCP_CODE(dp)) {
1286 /* CCP must be going down - disable compression */
1287 if (sc->sc_flags & SC_CCP_UP) {
1289 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1295 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1296 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1297 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1299 /* we're agreeing to send compressed packets. */
1300 if (sc->sc_xc_state != NULL
1301 && (*sc->sc_xcomp->comp_init)
1302 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1303 PPP2IFP(sc)->if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1305 sc->sc_flags |= SC_COMP_RUN;
1309 /* peer is agreeing to send compressed packets. */
1310 if (sc->sc_rc_state != NULL
1311 && (*sc->sc_rcomp->decomp_init)
1312 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1313 PPP2IFP(sc)->if_dunit, 0, sc->sc_mru,
1314 sc->sc_flags & SC_DEBUG)) {
1316 sc->sc_flags |= SC_DECOMP_RUN;
1317 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1325 if (sc->sc_flags & SC_CCP_UP) {
1327 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1328 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1330 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1331 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1333 sc->sc_flags &= ~SC_DC_ERROR;
1343 * CCP is down; free (de)compressor state if necessary.
1347 struct ppp_softc *sc;
1349 if (sc->sc_xc_state) {
1350 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1351 sc->sc_xc_state = NULL;
1353 if (sc->sc_rc_state) {
1354 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1355 sc->sc_rc_state = NULL;
1358 #endif /* PPP_COMPRESS */
1361 * PPP packet input routine.
1362 * The caller has checked and removed the FCS and has inserted
1363 * the address/control bytes and the protocol high byte if they
1367 ppppktin(sc, m, lost)
1368 struct ppp_softc *sc;
1375 m->m_flags |= M_ERRMARK;
1376 IF_ENQUEUE(&sc->sc_rawq, m);
1377 schednetisr(NETISR_PPP);
1382 * Process a received PPP packet, doing decompression as necessary.
1383 * Should be called at splsoftnet.
1385 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1386 TYPE_UNCOMPRESSED_TCP)
1390 struct ppp_softc *sc;
1393 struct ifnet *ifp = PPP2IFP(sc);
1395 int s, ilen = 0, xlen, proto, rv;
1396 u_char *cp, adrs, ctrl;
1397 struct mbuf *mp, *dmp = NULL;
1401 sc->sc_stats.ppp_ipackets++;
1403 if (sc->sc_flags & SC_LOG_INPKT) {
1404 ilen = m_length(m, NULL);
1405 if_printf(ifp, "got %d bytes\n", ilen);
1409 cp = mtod(m, u_char *);
1410 adrs = PPP_ADDRESS(cp);
1411 ctrl = PPP_CONTROL(cp);
1412 proto = PPP_PROTOCOL(cp);
1414 if (m->m_flags & M_ERRMARK) {
1415 m->m_flags &= ~M_ERRMARK;
1417 sc->sc_flags |= SC_VJ_RESET;
1423 * Decompress this packet if necessary, update the receiver's
1424 * dictionary, or take appropriate action on a CCP packet.
1426 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1427 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1428 /* decompress this packet */
1429 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1430 if (rv == DECOMP_OK) {
1433 /* no error, but no decompressed packet produced */
1437 cp = mtod(m, u_char *);
1438 proto = PPP_PROTOCOL(cp);
1442 * An error has occurred in decompression.
1443 * Pass the compressed packet up to pppd, which may take
1444 * CCP down or issue a Reset-Req.
1446 if (sc->sc_flags & SC_DEBUG)
1447 if_printf(ifp, "decompress failed %d\n", rv);
1449 sc->sc_flags |= SC_VJ_RESET;
1450 if (rv == DECOMP_ERROR)
1451 sc->sc_flags |= SC_DC_ERROR;
1453 sc->sc_flags |= SC_DC_FERROR;
1458 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1459 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1461 if (proto == PPP_CCP) {
1467 ilen = m_length(m, NULL);
1470 if (sc->sc_flags & SC_VJ_RESET) {
1472 * If we've missed a packet, we must toss subsequent compressed
1473 * packets which don't have an explicit connection ID.
1476 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1478 sc->sc_flags &= ~SC_VJ_RESET;
1483 * See if we have a VJ-compressed packet to uncompress.
1485 if (proto == PPP_VJC_COMP) {
1486 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1489 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1490 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1491 sc->sc_comp, &iphdr, &hlen);
1494 if (sc->sc_flags & SC_DEBUG)
1495 if_printf(ifp, "VJ uncompress failed on type comp\n");
1499 /* Copy the PPP and IP headers into a new mbuf. */
1500 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1505 if (hlen + PPP_HDRLEN > MHLEN) {
1506 MCLGET(mp, M_DONTWAIT);
1507 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1509 goto bad; /* lose if big headers and no clusters */
1513 mac_mbuf_copy(m, mp);
1515 cp = mtod(mp, u_char *);
1521 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1522 mp->m_len = hlen + PPP_HDRLEN;
1525 * Trim the PPP and VJ headers off the old mbuf
1526 * and stick the new and old mbufs together.
1528 m->m_data += PPP_HDRLEN + xlen;
1529 m->m_len -= PPP_HDRLEN + xlen;
1530 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1531 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1532 mp->m_len += m->m_len;
1533 mp->m_next = m_free(m);
1538 ilen += hlen - xlen;
1540 } else if (proto == PPP_VJC_UNCOMP) {
1541 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1544 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1545 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1546 sc->sc_comp, &iphdr, &hlen);
1549 if (sc->sc_flags & SC_DEBUG)
1550 if_printf(ifp, "VJ uncompress failed on type uncomp\n");
1560 * If the packet will fit in a header mbuf, don't waste a
1561 * whole cluster on it.
1563 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1564 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1567 mac_mbuf_copy(m, mp);
1569 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1575 m->m_pkthdr.len = ilen;
1576 m->m_pkthdr.rcvif = ifp;
1578 if ((proto & 0x8000) == 0) {
1581 * See whether we want to pass this packet, and
1582 * if it counts as link activity.
1584 adrs = *mtod(m, u_char *); /* save address field */
1585 *mtod(m, u_char *) = 0; /* indicate inbound */
1586 if (sc->sc_pass_filt.bf_insns != 0
1587 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1589 /* drop this packet */
1593 if (sc->sc_active_filt.bf_insns == 0
1594 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1595 sc->sc_last_recv = time_uptime;
1597 *mtod(m, u_char *) = adrs;
1600 * Record the time that we received this packet.
1602 sc->sc_last_recv = time_uptime;
1603 #endif /* PPP_FILTER */
1606 /* See if bpf wants to look at the packet. */
1607 BPF_MTAP(PPP2IFP(sc), m);
1614 * IP packet - take off the ppp header and pass it up to IP.
1616 if ((ifp->if_flags & IFF_UP) == 0
1617 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1618 /* interface is down - drop the packet. */
1622 m->m_pkthdr.len -= PPP_HDRLEN;
1623 m->m_data += PPP_HDRLEN;
1624 m->m_len -= PPP_HDRLEN;
1625 if ((m = ip_fastforward(m)) == NULL)
1633 * IPv6 packet - take off the ppp header and pass it up to IPv6.
1635 if ((ifp->if_flags & IFF_UP) == 0
1636 || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1637 /* interface is down - drop the packet. */
1641 m->m_pkthdr.len -= PPP_HDRLEN;
1642 m->m_data += PPP_HDRLEN;
1643 m->m_len -= PPP_HDRLEN;
1650 * IPX packet - take off the ppp header and pass it up to IPX.
1652 if ((PPP2IFP(sc)->if_flags & IFF_UP) == 0
1653 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1654 /* interface is down - drop the packet. */
1658 m->m_pkthdr.len -= PPP_HDRLEN;
1659 m->m_data += PPP_HDRLEN;
1660 m->m_len -= PPP_HDRLEN;
1662 sc->sc_last_recv = time_uptime; /* update time of last pkt rcvd */
1668 * Some other protocol - place on input queue for read().
1674 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1676 rv = netisr_queue(isr, m); /* (0) on success. */
1677 if ((isr == -1 && !rv) || (isr != -1 && rv)) {
1678 if (sc->sc_flags & SC_DEBUG)
1679 if_printf(ifp, "input queue full\n");
1685 ifp->if_ibytes += ilen;
1686 getmicrotime(&ifp->if_lastchange);
1696 PPP2IFP(sc)->if_ierrors++;
1697 sc->sc_stats.ppp_ierrors++;
1700 #define MAX_DUMP_BYTES 128
1706 char buf[3*MAX_DUMP_BYTES+4];
1710 for (m = m0; m; m = m->m_next) {
1712 u_char *rptr = (u_char *)m->m_data;
1715 if (bp > buf + (sizeof(buf) - 4))
1717 *bp++ = hex2ascii(*rptr >> 4);
1718 *bp++ = hex2ascii(*rptr++ & 0xf);
1722 if (bp > buf + (sizeof(buf) - 3))
1732 printf("%s\n", buf);