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, 0);
279 * XXX layering violation - if_ppp can work over any lower
280 * level transport that cares to attach to it.
282 pppasyncattach(NULL);
285 /* XXX: layering violation */
288 netisr_unregister(NETISR_PPP);
290 if_clone_detach(&ppp_cloner);
291 PPP_LIST_LOCK_DESTROY();
299 static moduledata_t ppp_mod = {
305 DECLARE_MODULE(if_ppp, ppp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
308 * Allocate a ppp interface unit and initialize it.
315 char tmpname[IFNAMSIZ];
317 struct ppp_softc *sc;
320 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
321 if (sc->sc_xfer == pid) {
327 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
328 if (sc->sc_devp == NULL)
332 /* Try to clone an interface if we don't have a free one */
334 strcpy(tmpname, PPPNAME);
335 if (if_clone_create(tmpname, sizeof(tmpname), (caddr_t) 0) != 0)
337 ifp = ifunit(tmpname);
342 if (sc == NULL || sc->sc_devp != NULL)
346 sc->sc_mru = PPP_MRU;
347 sc->sc_relinq = NULL;
348 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
350 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
353 sl_compress_init(sc->sc_comp, -1);
356 sc->sc_xc_state = NULL;
357 sc->sc_rc_state = NULL;
358 #endif /* PPP_COMPRESS */
359 for (i = 0; i < NUM_NP; ++i)
360 sc->sc_npmode[i] = NPMODE_ERROR;
361 sc->sc_npqueue = NULL;
362 sc->sc_npqtail = &sc->sc_npqueue;
363 sc->sc_last_sent = sc->sc_last_recv = time_uptime;
369 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
373 struct ppp_softc *sc;
377 if_down(PPP2IFP(sc));
378 PPP2IFP(sc)->if_flags &= ~IFF_UP;
379 PPP2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
380 getmicrotime(&PPP2IFP(sc)->if_lastchange);
383 IF_DRAIN(&sc->sc_rawq);
384 IF_DRAIN(&sc->sc_inq);
385 IF_DRAIN(&sc->sc_fastq);
386 while ((m = sc->sc_npqueue) != NULL) {
387 sc->sc_npqueue = m->m_nextpkt;
392 sc->sc_xc_state = NULL;
393 sc->sc_rc_state = NULL;
394 #endif /* PPP_COMPRESS */
396 if (sc->sc_pass_filt.bf_insns != 0) {
397 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
398 sc->sc_pass_filt.bf_insns = 0;
399 sc->sc_pass_filt.bf_len = 0;
401 if (sc->sc_active_filt.bf_insns != 0) {
402 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
403 sc->sc_active_filt.bf_insns = 0;
404 sc->sc_active_filt.bf_len = 0;
406 #endif /* PPP_FILTER */
408 if (sc->sc_comp != 0) {
409 free(sc->sc_comp, M_DEVBUF);
416 * Ioctl routine for generic ppp devices.
419 pppioctl(sc, cmd, data, flag, td)
420 struct ppp_softc *sc;
426 struct proc *p = td->td_proc;
427 int s, flags, mru, npx;
430 struct ppp_option_data *odp;
431 struct compressor **cp;
435 struct bpf_program *bp, *nbp;
436 struct bpf_insn *newcode, *oldcode;
438 #endif /* PPP_FILTER */
440 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
445 *(int *)data = sc->sc_inq.ifq_len;
449 *(int *)data = PPP2IFP(sc)->if_dunit;
453 *(u_int *)data = sc->sc_flags;
457 error = priv_check(td, PRIV_NET_PPP);
460 flags = *(int *)data & SC_MASK;
463 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
467 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
472 error = priv_check(td, PRIV_NET_PPP);
476 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
481 *(int *)data = sc->sc_mru;
486 error = priv_check(td, PRIV_NET_PPP);
491 sl_compress_init(sc->sc_comp, *(int *)data);
498 error = priv_check(td, PRIV_NET_PPP);
501 sc->sc_xfer = p->p_pid;
505 case PPPIOCSCOMPRESS:
506 error = priv_check(td, PRIV_NET_PPP);
509 odp = (struct ppp_option_data *) data;
511 if (nb > sizeof(ccp_option))
512 nb = sizeof(ccp_option);
513 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
515 if (ccp_option[1] < 2) { /* preliminary check on the length byte */
519 for (cp = ppp_compressors; *cp != NULL; ++cp)
520 if ((*cp)->compress_proto == ccp_option[0]) {
522 * Found a handler for the protocol - try to allocate
523 * a compressor or decompressor.
528 if (sc->sc_xc_state != NULL)
529 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
531 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
532 if (sc->sc_xc_state == NULL) {
533 if (sc->sc_flags & SC_DEBUG)
534 if_printf(PPP2IFP(sc), "comp_alloc failed\n");
538 sc->sc_flags &= ~SC_COMP_RUN;
542 if (sc->sc_rc_state != NULL)
543 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
545 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
546 if (sc->sc_rc_state == NULL) {
547 if (sc->sc_flags & SC_DEBUG)
548 if_printf(PPP2IFP(sc), "decomp_alloc failed\n");
552 sc->sc_flags &= ~SC_DECOMP_RUN;
557 if (sc->sc_flags & SC_DEBUG)
558 if_printf(PPP2IFP(sc), "no compressor for [%x %x %x], %x\n",
559 ccp_option[0], ccp_option[1], ccp_option[2], nb);
560 error = EINVAL; /* no handler found */
562 #endif /* PPP_COMPRESS */
566 npi = (struct npioctl *) data;
567 npx = 0; /* XXX: quiet gcc */
568 switch (npi->protocol) {
580 if (cmd == PPPIOCGNPMODE) {
581 npi->mode = sc->sc_npmode[npx];
583 error = priv_check(td, PRIV_NET_PPP);
586 if (npi->mode != sc->sc_npmode[npx]) {
588 sc->sc_npmode[npx] = npi->mode;
589 if (npi->mode != NPMODE_QUEUE) {
601 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
602 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
609 nbp = (struct bpf_program *) data;
610 if ((unsigned) nbp->bf_len > BPF_MAXINSNS) {
614 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
615 if (newcodelen != 0) {
616 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
618 error = EINVAL; /* or sumpin */
621 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
623 free(newcode, M_DEVBUF);
626 if (!bpf_validate(newcode, nbp->bf_len)) {
627 free(newcode, M_DEVBUF);
633 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
634 oldcode = bp->bf_insns;
636 bp->bf_len = nbp->bf_len;
637 bp->bf_insns = newcode;
640 free(oldcode, M_DEVBUF);
652 * Process an ioctl request to the ppp network interface.
655 pppsioctl(ifp, cmd, data)
656 register struct ifnet *ifp;
660 struct thread *td = curthread; /* XXX */
661 register struct ppp_softc *sc = ifp->if_softc;
662 register struct ifaddr *ifa = (struct ifaddr *)data;
663 register struct ifreq *ifr = (struct ifreq *)data;
664 struct ppp_stats *psp;
666 struct ppp_comp_stats *pcp;
668 int s = splimp(), error = 0;
672 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
673 ifp->if_flags &= ~IFF_UP;
678 switch(ifa->ifa_addr->sa_family) {
692 error = EAFNOSUPPORT;
698 switch(ifa->ifa_addr->sa_family) {
712 error = EAFNOSUPPORT;
719 * XXXRW: Isn't this priv_check() check redundant to the one at the
722 error = priv_check(td, PRIV_NET_SETIFMTU);
725 if (ifr->ifr_mtu > PPP_MAXMTU)
728 PPP2IFP(sc)->if_mtu = ifr->ifr_mtu;
730 (*sc->sc_setmtu)(sc);
735 ifr->ifr_mtu = PPP2IFP(sc)->if_mtu;
741 error = EAFNOSUPPORT;
744 switch(ifr->ifr_addr.sa_family) {
754 error = EAFNOSUPPORT;
760 psp = &((struct ifpppstatsreq *) data)->stats;
761 bzero(psp, sizeof(*psp));
762 psp->p = sc->sc_stats;
763 #if defined(VJC) && !defined(SL_NO_STATS)
765 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
766 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
767 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
768 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
769 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
770 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
771 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
772 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
779 pcp = &((struct ifpppcstatsreq *) data)->stats;
780 bzero(pcp, sizeof(*pcp));
781 if (sc->sc_xc_state != NULL)
782 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
783 if (sc->sc_rc_state != NULL)
784 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
786 #endif /* PPP_COMPRESS */
796 * Queue a packet. Start transmission if not active.
797 * Packet is placed in Information field of PPP frame.
798 * Called at splnet as the if->if_output handler.
799 * Called at splnet from pppwrite().
802 pppoutput(ifp, m0, dst, rtp)
805 struct sockaddr *dst;
808 register struct ppp_softc *sc = ifp->if_softc;
809 int protocol, address, control;
818 error = mac_check_ifnet_transmit(ifp, m0);
823 if (sc->sc_devp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0
824 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
825 error = ENETDOWN; /* sort of */
830 * Compute PPP header.
832 m0->m_flags &= ~M_HIGHPRI;
833 switch (dst->sa_family) {
836 address = PPP_ALLSTATIONS;
839 mode = sc->sc_npmode[NP_IP];
842 * If this packet has the "low delay" bit set in the IP header,
843 * put it on the fastq instead.
845 ip = mtod(m0, struct ip *);
846 if (ip->ip_tos & IPTOS_LOWDELAY)
847 m0->m_flags |= M_HIGHPRI;
852 address = PPP_ALLSTATIONS; /*XXX*/
853 control = PPP_UI; /*XXX*/
855 mode = sc->sc_npmode[NP_IPV6];
857 #if 0 /* XXX flowinfo/traffic class, maybe? */
859 * If this packet has the "low delay" bit set in the IP header,
860 * put it on the fastq instead.
862 ip = mtod(m0, struct ip *);
863 if (ip->ip_tos & IPTOS_LOWDELAY)
864 m0->m_flags |= M_HIGHPRI;
871 * This is pretty bogus.. We dont have an ipxcp module in pppd
872 * yet to configure the link parameters. Sigh. I guess a
873 * manual ifconfig would do.... -Peter
875 address = PPP_ALLSTATIONS;
882 address = PPP_ADDRESS(dst->sa_data);
883 control = PPP_CONTROL(dst->sa_data);
884 protocol = PPP_PROTOCOL(dst->sa_data);
888 if_printf(ifp, "af%d not supported\n", dst->sa_family);
889 error = EAFNOSUPPORT;
894 * Drop this packet, or return an error, if necessary.
896 if (mode == NPMODE_ERROR) {
900 if (mode == NPMODE_DROP) {
906 * Add PPP header. If no space in first mbuf, allocate another.
907 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
909 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
910 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
917 m0->m_data -= PPP_HDRLEN;
919 cp = mtod(m0, u_char *);
922 *cp++ = protocol >> 8;
923 *cp++ = protocol & 0xff;
924 m0->m_len += PPP_HDRLEN;
926 len = m_length(m0, NULL);
928 if (sc->sc_flags & SC_LOG_OUTPKT) {
929 printf("%s output: ", ifp->if_xname);
933 if ((protocol & 0x8000) == 0) {
936 * Apply the pass and active filters to the packet,
937 * but only if it is a data packet.
939 *mtod(m0, u_char *) = 1; /* indicates outbound */
940 if (sc->sc_pass_filt.bf_insns != 0
941 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
943 error = 0; /* drop this packet */
948 * Update the time we sent the most recent packet.
950 if (sc->sc_active_filt.bf_insns == 0
951 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
952 sc->sc_last_sent = time_uptime;
954 *mtod(m0, u_char *) = address;
957 * Update the time we sent the most recent data packet.
959 sc->sc_last_sent = time_uptime;
960 #endif /* PPP_FILTER */
964 * See if bpf wants to look at the packet.
969 * Put the packet on the appropriate queue.
971 s = splsoftnet(); /* redundant */
972 if (mode == NPMODE_QUEUE) {
973 /* XXX we should limit the number of packets on this queue */
974 *sc->sc_npqtail = m0;
975 m0->m_nextpkt = NULL;
976 sc->sc_npqtail = &m0->m_nextpkt;
978 /* fastq and if_snd are emptied at spl[soft]net now */
979 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq:
980 (struct ifqueue *)&ifp->if_snd;
982 if (_IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
985 PPP2IFP(sc)->if_oerrors++;
986 sc->sc_stats.ppp_oerrors++;
990 _IF_ENQUEUE(ifq, m0);
994 getmicrotime(&ifp->if_lastchange);
996 ifp->if_obytes += len;
1007 * After a change in the NPmode for some NP, move packets from the
1008 * npqueue to the send queue or the fast queue as appropriate.
1009 * Should be called at spl[soft]net.
1013 struct ppp_softc *sc;
1015 struct mbuf *m, **mpp;
1016 struct ifqueue *ifq;
1019 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1020 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1022 mode = sc->sc_npmode[NP_IP];
1025 mode = sc->sc_npmode[NP_IPV6];
1034 * This packet can now go on one of the queues to be sent.
1036 *mpp = m->m_nextpkt;
1037 m->m_nextpkt = NULL;
1038 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq:
1039 (struct ifqueue *)&PPP2IFP(sc)->if_snd;
1040 if (! IF_HANDOFF(ifq, m, NULL)) {
1041 PPP2IFP(sc)->if_oerrors++;
1042 sc->sc_stats.ppp_oerrors++;
1048 *mpp = m->m_nextpkt;
1053 mpp = &m->m_nextpkt;
1057 sc->sc_npqtail = mpp;
1061 * Transmitter has finished outputting some stuff;
1062 * remember to call sc->sc_start later at splsoftnet.
1066 struct ppp_softc *sc;
1070 sc->sc_flags &= ~SC_TBUSY;
1071 schednetisr(NETISR_PPP);
1077 * Get a packet to send. This procedure is intended to be called at
1078 * splsoftnet, since it may involve time-consuming operations such as
1079 * applying VJ compression, packet compression, address/control and/or
1080 * protocol field compression to the packet.
1084 struct ppp_softc *sc;
1086 struct mbuf *m, *mp;
1088 int address, control, protocol;
1091 * Grab a packet to send: first try the fast queue, then the
1094 IF_DEQUEUE(&sc->sc_fastq, m);
1096 IF_DEQUEUE(&PPP2IFP(sc)->if_snd, m);
1100 ++sc->sc_stats.ppp_opackets;
1103 * Extract the ppp header of the new packet.
1104 * The ppp header will be in one mbuf.
1106 cp = mtod(m, u_char *);
1107 address = PPP_ADDRESS(cp);
1108 control = PPP_CONTROL(cp);
1109 protocol = PPP_PROTOCOL(cp);
1115 * If the packet is a TCP/IP packet, see if we can compress it.
1117 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1122 ip = (struct ip *) (cp + PPP_HDRLEN);
1123 if (mp->m_len <= PPP_HDRLEN) {
1127 ip = mtod(mp, struct ip *);
1129 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1130 if (ip->ip_p == IPPROTO_TCP) {
1131 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1132 !(sc->sc_flags & SC_NO_TCP_CCID));
1134 case TYPE_UNCOMPRESSED_TCP:
1135 protocol = PPP_VJC_UNCOMP;
1137 case TYPE_COMPRESSED_TCP:
1138 protocol = PPP_VJC_COMP;
1139 cp = mtod(m, u_char *);
1140 cp[0] = address; /* header has moved */
1145 cp[3] = protocol; /* update protocol in PPP header */
1155 #endif /* PPP_COMPRESS */
1159 if (protocol != PPP_LCP && protocol != PPP_CCP
1160 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1161 struct mbuf *mcomp = NULL;
1164 slen = m_length(m, NULL);
1165 clen = (*sc->sc_xcomp->compress)
1166 (sc->sc_xc_state, &mcomp, m, slen, PPP2IFP(sc)->if_mtu + PPP_HDRLEN);
1167 if (mcomp != NULL) {
1168 if (sc->sc_flags & SC_CCP_UP) {
1169 /* Send the compressed packet instead of the original. */
1172 cp = mtod(m, u_char *);
1175 /* Can't transmit compressed packets until CCP is up. */
1180 #endif /* PPP_COMPRESS */
1183 * Compress the address/control and protocol, if possible.
1185 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1186 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1187 protocol != PPP_LCP) {
1188 /* can compress address/control */
1192 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1193 /* can compress protocol */
1194 if (mtod(m, u_char *) == cp) {
1195 cp[2] = cp[1]; /* move address/control up */
1206 * Software interrupt routine, called at spl[soft]net.
1211 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_create_mbuf_from_ifnet(PPP2IFP(sc), m);
1244 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1245 * 0 if it is about to be transmitted.
1248 ppp_ccp(sc, m, rcvd)
1249 struct ppp_softc *sc;
1258 * Get a pointer to the data after the PPP header.
1260 if (m->m_len <= PPP_HDRLEN) {
1264 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1267 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1270 ep = mtod(mp, u_char *) + mp->m_len;
1271 if (dp + CCP_HDRLEN > ep)
1273 slen = CCP_LENGTH(dp);
1274 if (dp + slen > ep) {
1275 if (sc->sc_flags & SC_DEBUG)
1276 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1277 dp, slen, mtod(mp, u_char *), mp->m_len);
1281 switch (CCP_CODE(dp)) {
1285 /* CCP must be going down - disable compression */
1286 if (sc->sc_flags & SC_CCP_UP) {
1288 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1294 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1295 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1296 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1298 /* we're agreeing to send compressed packets. */
1299 if (sc->sc_xc_state != NULL
1300 && (*sc->sc_xcomp->comp_init)
1301 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1302 PPP2IFP(sc)->if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1304 sc->sc_flags |= SC_COMP_RUN;
1308 /* peer is agreeing to send compressed packets. */
1309 if (sc->sc_rc_state != NULL
1310 && (*sc->sc_rcomp->decomp_init)
1311 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1312 PPP2IFP(sc)->if_dunit, 0, sc->sc_mru,
1313 sc->sc_flags & SC_DEBUG)) {
1315 sc->sc_flags |= SC_DECOMP_RUN;
1316 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1324 if (sc->sc_flags & SC_CCP_UP) {
1326 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1327 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1329 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1330 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1332 sc->sc_flags &= ~SC_DC_ERROR;
1342 * CCP is down; free (de)compressor state if necessary.
1346 struct ppp_softc *sc;
1348 if (sc->sc_xc_state) {
1349 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1350 sc->sc_xc_state = NULL;
1352 if (sc->sc_rc_state) {
1353 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1354 sc->sc_rc_state = NULL;
1357 #endif /* PPP_COMPRESS */
1360 * PPP packet input routine.
1361 * The caller has checked and removed the FCS and has inserted
1362 * the address/control bytes and the protocol high byte if they
1366 ppppktin(sc, m, lost)
1367 struct ppp_softc *sc;
1374 m->m_flags |= M_ERRMARK;
1375 IF_ENQUEUE(&sc->sc_rawq, m);
1376 schednetisr(NETISR_PPP);
1381 * Process a received PPP packet, doing decompression as necessary.
1382 * Should be called at splsoftnet.
1384 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1385 TYPE_UNCOMPRESSED_TCP)
1389 struct ppp_softc *sc;
1392 struct ifnet *ifp = PPP2IFP(sc);
1394 int s, ilen = 0, xlen, proto, rv;
1395 u_char *cp, adrs, ctrl;
1396 struct mbuf *mp, *dmp = NULL;
1400 sc->sc_stats.ppp_ipackets++;
1402 if (sc->sc_flags & SC_LOG_INPKT) {
1403 ilen = m_length(m, NULL);
1404 if_printf(ifp, "got %d bytes\n", ilen);
1408 cp = mtod(m, u_char *);
1409 adrs = PPP_ADDRESS(cp);
1410 ctrl = PPP_CONTROL(cp);
1411 proto = PPP_PROTOCOL(cp);
1413 if (m->m_flags & M_ERRMARK) {
1414 m->m_flags &= ~M_ERRMARK;
1416 sc->sc_flags |= SC_VJ_RESET;
1422 * Decompress this packet if necessary, update the receiver's
1423 * dictionary, or take appropriate action on a CCP packet.
1425 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1426 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1427 /* decompress this packet */
1428 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1429 if (rv == DECOMP_OK) {
1432 /* no error, but no decompressed packet produced */
1436 cp = mtod(m, u_char *);
1437 proto = PPP_PROTOCOL(cp);
1441 * An error has occurred in decompression.
1442 * Pass the compressed packet up to pppd, which may take
1443 * CCP down or issue a Reset-Req.
1445 if (sc->sc_flags & SC_DEBUG)
1446 if_printf(ifp, "decompress failed %d\n", rv);
1448 sc->sc_flags |= SC_VJ_RESET;
1449 if (rv == DECOMP_ERROR)
1450 sc->sc_flags |= SC_DC_ERROR;
1452 sc->sc_flags |= SC_DC_FERROR;
1457 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1458 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1460 if (proto == PPP_CCP) {
1466 ilen = m_length(m, NULL);
1469 if (sc->sc_flags & SC_VJ_RESET) {
1471 * If we've missed a packet, we must toss subsequent compressed
1472 * packets which don't have an explicit connection ID.
1475 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1477 sc->sc_flags &= ~SC_VJ_RESET;
1482 * See if we have a VJ-compressed packet to uncompress.
1484 if (proto == PPP_VJC_COMP) {
1485 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1488 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1489 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1490 sc->sc_comp, &iphdr, &hlen);
1493 if (sc->sc_flags & SC_DEBUG)
1494 if_printf(ifp, "VJ uncompress failed on type comp\n");
1498 /* Copy the PPP and IP headers into a new mbuf. */
1499 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1504 if (hlen + PPP_HDRLEN > MHLEN) {
1505 MCLGET(mp, M_DONTWAIT);
1506 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1508 goto bad; /* lose if big headers and no clusters */
1512 mac_copy_mbuf(m, mp);
1514 cp = mtod(mp, u_char *);
1520 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1521 mp->m_len = hlen + PPP_HDRLEN;
1524 * Trim the PPP and VJ headers off the old mbuf
1525 * and stick the new and old mbufs together.
1527 m->m_data += PPP_HDRLEN + xlen;
1528 m->m_len -= PPP_HDRLEN + xlen;
1529 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1530 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1531 mp->m_len += m->m_len;
1532 mp->m_next = m_free(m);
1537 ilen += hlen - xlen;
1539 } else if (proto == PPP_VJC_UNCOMP) {
1540 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1543 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1544 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1545 sc->sc_comp, &iphdr, &hlen);
1548 if (sc->sc_flags & SC_DEBUG)
1549 if_printf(ifp, "VJ uncompress failed on type uncomp\n");
1559 * If the packet will fit in a header mbuf, don't waste a
1560 * whole cluster on it.
1562 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1563 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1566 mac_copy_mbuf(m, mp);
1568 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1574 m->m_pkthdr.len = ilen;
1575 m->m_pkthdr.rcvif = ifp;
1577 if ((proto & 0x8000) == 0) {
1580 * See whether we want to pass this packet, and
1581 * if it counts as link activity.
1583 adrs = *mtod(m, u_char *); /* save address field */
1584 *mtod(m, u_char *) = 0; /* indicate inbound */
1585 if (sc->sc_pass_filt.bf_insns != 0
1586 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1588 /* drop this packet */
1592 if (sc->sc_active_filt.bf_insns == 0
1593 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1594 sc->sc_last_recv = time_uptime;
1596 *mtod(m, u_char *) = adrs;
1599 * Record the time that we received this packet.
1601 sc->sc_last_recv = time_uptime;
1602 #endif /* PPP_FILTER */
1605 /* See if bpf wants to look at the packet. */
1606 BPF_MTAP(PPP2IFP(sc), m);
1613 * IP packet - take off the ppp header and pass it up to IP.
1615 if ((ifp->if_flags & IFF_UP) == 0
1616 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1617 /* interface is down - drop the packet. */
1621 m->m_pkthdr.len -= PPP_HDRLEN;
1622 m->m_data += PPP_HDRLEN;
1623 m->m_len -= PPP_HDRLEN;
1624 if ((m = ip_fastforward(m)) == NULL)
1632 * IPv6 packet - take off the ppp header and pass it up to IPv6.
1634 if ((ifp->if_flags & IFF_UP) == 0
1635 || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1636 /* interface is down - drop the packet. */
1640 m->m_pkthdr.len -= PPP_HDRLEN;
1641 m->m_data += PPP_HDRLEN;
1642 m->m_len -= PPP_HDRLEN;
1649 * IPX packet - take off the ppp header and pass it up to IPX.
1651 if ((PPP2IFP(sc)->if_flags & IFF_UP) == 0
1652 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1653 /* interface is down - drop the packet. */
1657 m->m_pkthdr.len -= PPP_HDRLEN;
1658 m->m_data += PPP_HDRLEN;
1659 m->m_len -= PPP_HDRLEN;
1661 sc->sc_last_recv = time_uptime; /* update time of last pkt rcvd */
1667 * Some other protocol - place on input queue for read().
1673 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1675 rv = netisr_queue(isr, m); /* (0) on success. */
1676 if ((isr == -1 && !rv) || (isr != -1 && rv)) {
1677 if (sc->sc_flags & SC_DEBUG)
1678 if_printf(ifp, "input queue full\n");
1684 ifp->if_ibytes += ilen;
1685 getmicrotime(&ifp->if_lastchange);
1695 PPP2IFP(sc)->if_ierrors++;
1696 sc->sc_stats.ppp_ierrors++;
1699 #define MAX_DUMP_BYTES 128
1705 char buf[3*MAX_DUMP_BYTES+4];
1709 for (m = m0; m; m = m->m_next) {
1711 u_char *rptr = (u_char *)m->m_data;
1714 if (bp > buf + (sizeof(buf) - 4))
1716 *bp++ = hex2ascii(*rptr >> 4);
1717 *bp++ = hex2ascii(*rptr++ & 0xf);
1721 if (bp > buf + (sizeof(buf) - 3))
1731 printf("%s\n", buf);