2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Carnegie Mellon University
22 * Pittsburgh, PA 15213
27 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
29 * Copyright (c) 1987 Regents of the University of California.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms are permitted
33 * provided that the above copyright notice and this paragraph are
34 * duplicated in all such forms and that any documentation,
35 * advertising materials, and other materials related to such
36 * distribution and use acknowledge that the software was developed
37 * by the University of California, Berkeley. The name of the
38 * University may not be used to endorse or promote products derived
39 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
44 * Serial Line interface
47 * Center for Seismic Studies
48 * 1300 N 17th Street, Suite 1450
49 * Arlington, Virginia 22209
54 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
55 * Converted to 4.3BSD Beta by Chris Torek.
56 * Other changes made at Berkeley, based in part on code by Kirk Smith.
58 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59 * Added VJ tcp header compression; more unified ioctls
61 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
62 * Cleaned up a lot of the mbuf-related code to fix bugs that
63 * caused system crashes and packet corruption. Changed pppstart
64 * so that it doesn't just give up with a collision if the whole
65 * packet doesn't fit in the output ring buffer.
67 * Added priority queueing for interactive IP packets, following
68 * the model of if_sl.c, plus hooks for bpf.
69 * Paul Mackerras (paulus@cs.anu.edu.au).
72 /* $FreeBSD: src/sys/net/if_ppp.c,v 1.67.2.4 2002/04/14 21:41:48 luigi Exp $ */
73 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
74 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
87 #include <sys/param.h>
88 #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>
99 #include <sys/msgport2.h>
100 #include <sys/mplock2.h>
103 #include <net/if_types.h>
104 #include <net/ifq_var.h>
105 #include <net/netisr.h>
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/in_var.h>
112 #include <netinet/ip.h>
116 #include <netproto/ipx/ipx.h>
117 #include <netproto/ipx/ipx_if.h>
121 #include <net/slcompress.h>
125 #include "if_pppvar.h"
129 #define splsoftnet splnet
133 #define PACKETPTR struct mbuf *
134 #include <net/ppp_layer/ppp_comp.h>
137 struct ppp_softc ppp_softc[NPPP];
139 /* XXX layering violation */
140 extern void pppasyncattach (void *);
142 static void pppattach (void *);
143 PSEUDO_SET(pppattach, if_ppp);
145 static int pppsioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
148 static void ppp_requeue (struct ppp_softc *);
149 static void ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
150 static void ppp_ccp_closed (struct ppp_softc *);
151 static void ppp_inproc (struct ppp_softc *, struct mbuf *);
152 static void pppdumpm (struct mbuf *m0);
153 static void ppp_ifstart(struct ifnet *ifp);
156 * Some useful mbuf macros not in mbuf.h.
158 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
160 #define M_DATASTART(m) \
161 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
162 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
164 #define M_DATASIZE(m) \
165 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
166 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
169 * We steal two bits in the mbuf m_flags, to mark high-priority packets
170 * for output, and received packets following lost/corrupted packets.
172 #define M_HIGHPRI M_PROTO2 /* output packet for sc_fastq */
173 #define M_ERRMARK M_PROTO3 /* steal a bit in mbuf m_flags */
178 * List of compressors we know about.
179 * We leave some space so maybe we can modload compressors.
182 extern struct compressor ppp_bsd_compress;
183 extern struct compressor ppp_deflate, ppp_deflate_draft;
185 static struct compressor *ppp_compressors[8] = {
186 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
189 #if DO_DEFLATE && defined(PPP_DEFLATE)
195 #endif /* PPP_COMPRESS */
198 * Software interrupt routine, called at spl[soft]net.
201 pppintr(netmsg_t msg)
204 struct ppp_softc *sc;
208 * Packets are never sent to this netisr so the message must always
209 * be replied. Interlock processing and notification by replying
212 lwkt_replymsg(&msg->lmsg, 0);
217 for (i = 0; i < NPPP; ++i, ++sc) {
218 ifnet_serialize_all(&sc->sc_if);
219 if (!(sc->sc_flags & SC_TBUSY)
220 && (!ifq_is_empty(&sc->sc_if.if_snd) || !IF_QEMPTY(&sc->sc_fastq))) {
221 sc->sc_flags |= SC_TBUSY;
225 IF_DEQUEUE(&sc->sc_rawq, m);
230 ifnet_deserialize_all(&sc->sc_if);
236 * Called from boot code to establish ppp interfaces.
239 pppattach(void *dummy)
241 struct ppp_softc *sc;
244 for (sc = ppp_softc; i < NPPP; sc++) {
245 if_initname(&(sc->sc_if), "ppp", i++);
246 sc->sc_if.if_softc = sc;
247 sc->sc_if.if_mtu = PPP_MTU;
248 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
249 sc->sc_if.if_type = IFT_PPP;
250 sc->sc_if.if_hdrlen = PPP_HDRLEN;
251 sc->sc_if.if_ioctl = pppsioctl;
252 sc->sc_if.if_output = pppoutput;
253 sc->sc_if.if_start = ppp_ifstart;
254 ifq_set_maxlen(&sc->sc_if.if_snd, IFQ_MAXLEN);
255 ifq_set_ready(&sc->sc_if.if_snd);
256 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
257 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
258 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
259 callout_init(&sc->sc_timeout);
260 if_attach(&sc->sc_if, NULL);
261 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
263 netisr_register(NETISR_PPP, pppintr, NULL);
265 * XXX layering violation - if_ppp can work over any lower level
266 * transport that cares to attach to it.
268 pppasyncattach(dummy);
272 * Allocate a ppp interface unit and initialize it.
275 pppalloc(struct thread *td)
278 struct ppp_softc *sc;
280 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
281 if (sc->sc_xfer == td) {
285 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
286 if (sc->sc_devp == NULL)
292 sc->sc_mru = PPP_MRU;
293 sc->sc_relinq = NULL;
294 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
296 sc->sc_comp = kmalloc(sizeof(struct slcompress), M_DEVBUF, M_WAITOK);
297 sl_compress_init(sc->sc_comp, -1);
300 sc->sc_xc_state = NULL;
301 sc->sc_rc_state = NULL;
302 #endif /* PPP_COMPRESS */
303 for (i = 0; i < NUM_NP; ++i)
304 sc->sc_npmode[i] = NPMODE_ERROR;
305 sc->sc_npqueue = NULL;
306 sc->sc_npqtail = &sc->sc_npqueue;
307 sc->sc_last_sent = sc->sc_last_recv = time_second;
313 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
316 pppdealloc(struct ppp_softc *sc)
321 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
322 getmicrotime(&sc->sc_if.if_lastchange);
326 IF_DEQUEUE(&sc->sc_rawq, m);
332 IF_DEQUEUE(&sc->sc_inq, m);
338 IF_DEQUEUE(&sc->sc_fastq, m);
343 while ((m = sc->sc_npqueue) != NULL) {
344 sc->sc_npqueue = m->m_nextpkt;
349 sc->sc_xc_state = NULL;
350 sc->sc_rc_state = NULL;
351 #endif /* PPP_COMPRESS */
353 if (sc->sc_pass_filt.bf_insns != 0) {
354 kfree(sc->sc_pass_filt.bf_insns, M_DEVBUF);
355 sc->sc_pass_filt.bf_insns = 0;
356 sc->sc_pass_filt.bf_len = 0;
358 if (sc->sc_active_filt.bf_insns != 0) {
359 kfree(sc->sc_active_filt.bf_insns, M_DEVBUF);
360 sc->sc_active_filt.bf_insns = 0;
361 sc->sc_active_filt.bf_len = 0;
363 #endif /* PPP_FILTER */
365 if (sc->sc_comp != 0) {
366 kfree(sc->sc_comp, M_DEVBUF);
373 * Ioctl routine for generic ppp devices.
376 pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
377 int flag, struct ucred *cred)
379 int error, flags, mru, npx;
381 struct ppp_option_data *odp;
382 struct compressor **cp;
386 struct bpf_program *bp, *nbp;
387 struct bpf_insn *newcode, *oldcode;
389 #endif /* PPP_FILTER */
391 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
396 *(int *)data = sc->sc_inq.ifq_len;
400 *(int *)data = sc->sc_if.if_dunit;
404 *(u_int *)data = sc->sc_flags;
408 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
410 flags = *(int *)data & SC_MASK;
413 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
416 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
421 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
424 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
429 *(int *)data = sc->sc_mru;
434 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
438 sl_compress_init(sc->sc_comp, *(int *)data);
445 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
447 sc->sc_xfer = curthread;
451 case PPPIOCSCOMPRESS:
452 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
454 odp = (struct ppp_option_data *) data;
456 if (nb > sizeof(ccp_option))
457 nb = sizeof(ccp_option);
458 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
460 if (ccp_option[1] < 2) /* preliminary check on the length byte */
462 for (cp = ppp_compressors; *cp != NULL; ++cp)
463 if ((*cp)->compress_proto == ccp_option[0]) {
465 * Found a handler for the protocol - try to allocate
466 * a compressor or decompressor.
471 if (sc->sc_xc_state != NULL)
472 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
474 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
475 if (sc->sc_xc_state == NULL) {
476 if (sc->sc_flags & SC_DEBUG)
477 kprintf("%s: comp_alloc failed\n",
481 sc->sc_flags &= ~SC_COMP_RUN;
485 if (sc->sc_rc_state != NULL)
486 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
488 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
489 if (sc->sc_rc_state == NULL) {
490 if (sc->sc_flags & SC_DEBUG)
491 kprintf("%s: decomp_alloc failed\n",
495 sc->sc_flags &= ~SC_DECOMP_RUN;
500 if (sc->sc_flags & SC_DEBUG)
501 kprintf("%s: no compressor for [%x %x %x], %x\n",
502 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
504 return (EINVAL); /* no handler found */
505 #endif /* PPP_COMPRESS */
509 npi = (struct npioctl *) data;
510 switch (npi->protocol) {
517 if (cmd == PPPIOCGNPMODE) {
518 npi->mode = sc->sc_npmode[npx];
520 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
522 if (npi->mode != sc->sc_npmode[npx]) {
524 sc->sc_npmode[npx] = npi->mode;
525 if (npi->mode != NPMODE_QUEUE) {
537 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
538 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
545 nbp = (struct bpf_program *) data;
546 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
548 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
549 if (newcodelen != 0) {
550 newcode = kmalloc(newcodelen, M_DEVBUF, M_WAITOK);
551 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
553 kfree(newcode, M_DEVBUF);
556 if (!bpf_validate(newcode, nbp->bf_len)) {
557 kfree(newcode, M_DEVBUF);
562 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
563 oldcode = bp->bf_insns;
565 bp->bf_len = nbp->bf_len;
566 bp->bf_insns = newcode;
569 kfree(oldcode, M_DEVBUF);
580 * Process an ioctl request to the ppp network interface.
583 pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
585 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
586 struct ifaddr *ifa = (struct ifaddr *)data;
587 struct ifreq *ifr = (struct ifreq *)data;
588 struct ppp_stats *psp;
590 struct ppp_comp_stats *pcp;
598 if ((ifp->if_flags & IFF_RUNNING) == 0)
599 ifp->if_flags &= ~IFF_UP;
604 switch(ifa->ifa_addr->sa_family) {
614 error = EAFNOSUPPORT;
620 switch(ifa->ifa_addr->sa_family) {
630 error = EAFNOSUPPORT;
636 if ((error = priv_check_cred(cr, PRIV_ROOT, 0)) != 0)
638 if (ifr->ifr_mtu > PPP_MAXMTU)
641 sc->sc_if.if_mtu = ifr->ifr_mtu;
643 (*sc->sc_setmtu)(sc);
648 ifr->ifr_mtu = sc->sc_if.if_mtu;
654 error = EAFNOSUPPORT;
657 switch(ifr->ifr_addr.sa_family) {
663 error = EAFNOSUPPORT;
669 psp = &((struct ifpppstatsreq *) data)->stats;
670 bzero(psp, sizeof(*psp));
671 psp->p = sc->sc_stats;
672 #if defined(VJC) && !defined(SL_NO_STATS)
674 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
675 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
676 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
677 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
678 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
679 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
680 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
681 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
688 pcp = &((struct ifpppcstatsreq *) data)->stats;
689 bzero(pcp, sizeof(*pcp));
690 if (sc->sc_xc_state != NULL)
691 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
692 if (sc->sc_rc_state != NULL)
693 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
695 #endif /* PPP_COMPRESS */
705 * Queue a packet. Start transmission if not active.
706 * Packet is placed in Information field of PPP frame.
707 * Called at splnet as the if->if_output handler.
708 * Called at splnet from pppwrite().
711 pppoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
714 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
715 int protocol, address, control;
725 struct altq_pktattr pktattr;
727 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
728 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
729 error = ENETDOWN; /* sort of */
733 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
736 * Compute PPP header.
738 m0->m_flags &= ~M_HIGHPRI;
739 switch (dst->sa_family) {
742 address = PPP_ALLSTATIONS;
745 mode = sc->sc_npmode[NP_IP];
748 * If this packet has the "low delay" bit set in the IP header,
749 * put it on the fastq instead.
751 ip = mtod(m0, struct ip *);
752 if (ip->ip_tos & IPTOS_LOWDELAY)
753 m0->m_flags |= M_HIGHPRI;
759 * This is pretty bogus.. We dont have an ipxcp module in pppd
760 * yet to configure the link parameters. Sigh. I guess a
761 * manual ifconfig would do.... -Peter
763 address = PPP_ALLSTATIONS;
770 address = PPP_ADDRESS(dst->sa_data);
771 control = PPP_CONTROL(dst->sa_data);
772 protocol = PPP_PROTOCOL(dst->sa_data);
776 kprintf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
777 error = EAFNOSUPPORT;
782 * Drop this packet, or return an error, if necessary.
784 if (mode == NPMODE_ERROR) {
788 if (mode == NPMODE_DROP) {
794 * Add PPP header. If no space in first mbuf, allocate another.
795 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
797 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
798 m0 = m_prepend(m0, PPP_HDRLEN, MB_DONTWAIT);
805 m0->m_data -= PPP_HDRLEN;
807 cp = mtod(m0, u_char *);
810 *cp++ = protocol >> 8;
811 *cp++ = protocol & 0xff;
812 m0->m_len += PPP_HDRLEN;
815 for (m = m0; m != NULL; m = m->m_next)
818 if (sc->sc_flags & SC_LOG_OUTPKT) {
819 kprintf("%s output: ", ifp->if_xname);
823 if ((protocol & 0x8000) == 0) {
826 * Apply the pass and active filters to the packet,
827 * but only if it is a data packet.
829 *mtod(m0, u_char *) = 1; /* indicates outbound */
830 if (sc->sc_pass_filt.bf_insns != 0
831 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
833 error = 0; /* drop this packet */
838 * Update the time we sent the most recent packet.
840 if (sc->sc_active_filt.bf_insns == 0
841 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
842 sc->sc_last_sent = time_second;
844 *mtod(m0, u_char *) = address;
847 * Update the time we sent the most recent data packet.
849 sc->sc_last_sent = time_second;
850 #endif /* PPP_FILTER */
856 * Put the packet on the appropriate queue.
859 if (mode == NPMODE_QUEUE) {
860 /* XXX we should limit the number of packets on this queue */
861 *sc->sc_npqtail = m0;
862 m0->m_nextpkt = NULL;
863 sc->sc_npqtail = &m0->m_nextpkt;
865 /* fastq and if_snd are emptied at spl[soft]net now */
866 if ((m0->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
868 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
877 ASSERT_IFNET_SERIALIZED_TX(&sc->sc_if);
878 error = ifq_enqueue(&sc->sc_if.if_snd, m0, &pktattr);
882 sc->sc_if.if_oerrors++;
883 sc->sc_stats.ppp_oerrors++;
888 getmicrotime(&ifp->if_lastchange);
890 ifp->if_obytes += len;
901 pppoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
906 ifnet_serialize_tx(ifp);
907 error = pppoutput_serialized(ifp, m0, dst, rtp);
908 ifnet_deserialize_tx(ifp);
914 * After a change in the NPmode for some NP, move packets from the
915 * npqueue to the send queue or the fast queue as appropriate.
916 * Should be called at spl[soft]net.
919 ppp_requeue(struct ppp_softc *sc)
921 struct mbuf *m, **mpp;
926 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
927 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
929 mode = sc->sc_npmode[NP_IP];
938 * This packet can now go on one of the queues to be sent.
942 if ((m->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
952 error = ifq_enqueue(&sc->sc_if.if_snd, m, NULL);
955 sc->sc_if.if_oerrors++;
956 sc->sc_stats.ppp_oerrors++;
971 sc->sc_npqtail = mpp;
975 * Transmitter has finished outputting some stuff;
976 * remember to call sc->sc_start later at splsoftnet.
979 ppp_restart(struct ppp_softc *sc)
982 sc->sc_flags &= ~SC_TBUSY;
983 schednetisr(NETISR_PPP);
989 * Get a packet to send. This procedure is intended to be called at
990 * splsoftnet, since it may involve time-consuming operations such as
991 * applying VJ compression, packet compression, address/control and/or
992 * protocol field compression to the packet.
995 ppp_dequeue(struct ppp_softc *sc)
999 int address, control, protocol;
1002 * Grab a packet to send: first try the fast queue, then the
1005 IF_DEQUEUE(&sc->sc_fastq, m);
1007 m = ifq_dequeue(&sc->sc_if.if_snd, NULL);
1011 ++sc->sc_stats.ppp_opackets;
1014 * Extract the ppp header of the new packet.
1015 * The ppp header will be in one mbuf.
1017 cp = mtod(m, u_char *);
1018 address = PPP_ADDRESS(cp);
1019 control = PPP_CONTROL(cp);
1020 protocol = PPP_PROTOCOL(cp);
1026 * If the packet is a TCP/IP packet, see if we can compress it.
1028 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1033 ip = (struct ip *) (cp + PPP_HDRLEN);
1034 if (mp->m_len <= PPP_HDRLEN) {
1038 ip = mtod(mp, struct ip *);
1040 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1041 if (ip->ip_p == IPPROTO_TCP) {
1042 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1043 !(sc->sc_flags & SC_NO_TCP_CCID));
1045 case TYPE_UNCOMPRESSED_TCP:
1046 protocol = PPP_VJC_UNCOMP;
1048 case TYPE_COMPRESSED_TCP:
1049 protocol = PPP_VJC_COMP;
1050 cp = mtod(m, u_char *);
1051 cp[0] = address; /* header has moved */
1056 cp[3] = protocol; /* update protocol in PPP header */
1066 #endif /* PPP_COMPRESS */
1070 if (protocol != PPP_LCP && protocol != PPP_CCP
1071 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1072 struct mbuf *mcomp = NULL;
1076 for (mp = m; mp != NULL; mp = mp->m_next)
1078 clen = (*sc->sc_xcomp->compress)
1079 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1080 if (mcomp != NULL) {
1081 if (sc->sc_flags & SC_CCP_UP) {
1082 /* Send the compressed packet instead of the original. */
1085 cp = mtod(m, u_char *);
1088 /* Can't transmit compressed packets until CCP is up. */
1093 #endif /* PPP_COMPRESS */
1096 * Compress the address/control and protocol, if possible.
1098 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1099 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1100 protocol != PPP_LCP) {
1101 /* can compress address/control */
1105 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1106 /* can compress protocol */
1107 if (mtod(m, u_char *) == cp) {
1108 cp[2] = cp[1]; /* move address/control up */
1120 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1121 * 0 if it is about to be transmitted.
1124 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
1131 * Get a pointer to the data after the PPP header.
1133 if (m->m_len <= PPP_HDRLEN) {
1137 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1140 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1143 ep = mtod(mp, u_char *) + mp->m_len;
1144 if (dp + CCP_HDRLEN > ep)
1146 slen = CCP_LENGTH(dp);
1147 if (dp + slen > ep) {
1148 if (sc->sc_flags & SC_DEBUG)
1149 kprintf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1150 dp, slen, mtod(mp, u_char *), mp->m_len);
1154 switch (CCP_CODE(dp)) {
1158 /* CCP must be going down - disable compression */
1159 if (sc->sc_flags & SC_CCP_UP) {
1161 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1167 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1168 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1169 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1171 /* we're agreeing to send compressed packets. */
1172 if (sc->sc_xc_state != NULL
1173 && (*sc->sc_xcomp->comp_init)
1174 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1175 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1177 sc->sc_flags |= SC_COMP_RUN;
1181 /* peer is agreeing to send compressed packets. */
1182 if (sc->sc_rc_state != NULL
1183 && (*sc->sc_rcomp->decomp_init)
1184 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1185 sc->sc_if.if_dunit, 0, sc->sc_mru,
1186 sc->sc_flags & SC_DEBUG)) {
1188 sc->sc_flags |= SC_DECOMP_RUN;
1189 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1197 if (sc->sc_flags & SC_CCP_UP) {
1199 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1200 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1202 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1203 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1205 sc->sc_flags &= ~SC_DC_ERROR;
1215 * CCP is down; free (de)compressor state if necessary.
1218 ppp_ccp_closed(struct ppp_softc *sc)
1220 if (sc->sc_xc_state) {
1221 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1222 sc->sc_xc_state = NULL;
1224 if (sc->sc_rc_state) {
1225 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1226 sc->sc_rc_state = NULL;
1229 #endif /* PPP_COMPRESS */
1232 * PPP packet input routine.
1233 * The caller has checked and removed the FCS and has inserted
1234 * the address/control bytes and the protocol high byte if they
1238 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
1243 m->m_flags |= M_ERRMARK;
1244 IF_ENQUEUE(&sc->sc_rawq, m);
1245 schednetisr(NETISR_PPP);
1251 * Process a received PPP packet, doing decompression as necessary.
1252 * Should be called at splsoftnet.
1254 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1255 TYPE_UNCOMPRESSED_TCP)
1258 ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
1260 struct ifnet *ifp = &sc->sc_if;
1262 int ilen = 0, proto, rv;
1263 u_char *cp, adrs, ctrl;
1264 struct mbuf *mp, *dmp = NULL;
1271 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1273 sc->sc_stats.ppp_ipackets++;
1275 if (sc->sc_flags & SC_LOG_INPKT) {
1277 for (mp = m; mp != NULL; mp = mp->m_next)
1279 kprintf("%s: got %d bytes\n", ifp->if_xname, ilen);
1283 cp = mtod(m, u_char *);
1284 adrs = PPP_ADDRESS(cp);
1285 ctrl = PPP_CONTROL(cp);
1286 proto = PPP_PROTOCOL(cp);
1288 if (m->m_flags & M_ERRMARK) {
1289 m->m_flags &= ~M_ERRMARK;
1291 sc->sc_flags |= SC_VJ_RESET;
1297 * Decompress this packet if necessary, update the receiver's
1298 * dictionary, or take appropriate action on a CCP packet.
1300 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1301 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1302 /* decompress this packet */
1303 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1304 if (rv == DECOMP_OK) {
1307 /* no error, but no decompressed packet produced */
1311 cp = mtod(m, u_char *);
1312 proto = PPP_PROTOCOL(cp);
1316 * An error has occurred in decompression.
1317 * Pass the compressed packet up to pppd, which may take
1318 * CCP down or issue a Reset-Req.
1320 if (sc->sc_flags & SC_DEBUG)
1321 kprintf("%s: decompress failed %d\n", ifp->if_xname, rv);
1323 sc->sc_flags |= SC_VJ_RESET;
1324 if (rv == DECOMP_ERROR)
1325 sc->sc_flags |= SC_DC_ERROR;
1327 sc->sc_flags |= SC_DC_FERROR;
1332 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1333 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1335 if (proto == PPP_CCP) {
1342 for (mp = m; mp != NULL; mp = mp->m_next)
1346 if (sc->sc_flags & SC_VJ_RESET) {
1348 * If we've missed a packet, we must toss subsequent compressed
1349 * packets which don't have an explicit connection ID.
1352 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1354 sc->sc_flags &= ~SC_VJ_RESET;
1359 * See if we have a VJ-compressed packet to uncompress.
1361 if (proto == PPP_VJC_COMP) {
1362 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1365 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1366 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1367 sc->sc_comp, &iphdr, &hlen);
1370 if (sc->sc_flags & SC_DEBUG)
1371 kprintf("%s: VJ uncompress failed on type comp\n",
1376 /* Copy the PPP and IP headers into a new mbuf. */
1377 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1382 if (hlen + PPP_HDRLEN > MHLEN) {
1383 MCLGET(mp, MB_DONTWAIT);
1384 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1386 goto bad; /* lose if big headers and no clusters */
1389 cp = mtod(mp, u_char *);
1395 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1396 mp->m_len = hlen + PPP_HDRLEN;
1399 * Trim the PPP and VJ headers off the old mbuf
1400 * and stick the new and old mbufs together.
1402 m->m_data += PPP_HDRLEN + xlen;
1403 m->m_len -= PPP_HDRLEN + xlen;
1404 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1405 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1406 mp->m_len += m->m_len;
1407 mp->m_next = m_free(m);
1412 ilen += hlen - xlen;
1414 } else if (proto == PPP_VJC_UNCOMP) {
1415 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1418 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1419 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1420 sc->sc_comp, &iphdr, &hlen);
1423 if (sc->sc_flags & SC_DEBUG)
1424 kprintf("%s: VJ uncompress failed on type uncomp\n",
1435 * If the packet will fit in a header mbuf, don't waste a
1436 * whole cluster on it.
1438 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1439 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1441 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1447 m->m_pkthdr.len = ilen;
1448 m->m_pkthdr.rcvif = ifp;
1450 if ((proto & 0x8000) == 0) {
1453 * See whether we want to pass this packet, and
1454 * if it counts as link activity.
1456 adrs = *mtod(m, u_char *); /* save address field */
1457 *mtod(m, u_char *) = 0; /* indicate inbound */
1458 if (sc->sc_pass_filt.bf_insns != 0
1459 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1461 /* drop this packet */
1465 if (sc->sc_active_filt.bf_insns == 0
1466 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1467 sc->sc_last_recv = time_second;
1469 *mtod(m, u_char *) = adrs;
1472 * Record the time that we received this packet.
1474 sc->sc_last_recv = time_second;
1475 #endif /* PPP_FILTER */
1478 BPF_MTAP(&sc->sc_if, m);
1485 * IP packet - take off the ppp header and pass it up to IP.
1487 if ((ifp->if_flags & IFF_UP) == 0
1488 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1489 /* interface is down - drop the packet. */
1493 m->m_pkthdr.len -= PPP_HDRLEN;
1494 m->m_data += PPP_HDRLEN;
1495 m->m_len -= PPP_HDRLEN;
1497 if (ipflow_fastforward(m))
1506 * IPX packet - take off the ppp header and pass it up to IPX.
1508 if ((sc->sc_if.if_flags & IFF_UP) == 0
1509 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1510 /* interface is down - drop the packet. */
1514 m->m_pkthdr.len -= PPP_HDRLEN;
1515 m->m_data += PPP_HDRLEN;
1516 m->m_len -= PPP_HDRLEN;
1518 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1524 * Some other protocol - place on input queue for read().
1530 * If we found a netproto just pass it to the proto. Otherwise queue
1531 * the packet to the tty (e.g. pppd). Note that sc_ctlp() must be
1532 * called EXACTLY once per packet queued.
1535 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1539 rv = (netisr_queue(isr, m) == 0);
1542 if (sc->sc_flags & SC_DEBUG)
1543 kprintf("%s: input queue full\n", ifp->if_xname);
1549 ifp->if_ibytes += ilen;
1550 getmicrotime(&ifp->if_lastchange);
1556 sc->sc_if.if_ierrors++;
1557 sc->sc_stats.ppp_ierrors++;
1560 #define MAX_DUMP_BYTES 128
1563 pppdumpm(struct mbuf *m0)
1565 char buf[3*MAX_DUMP_BYTES+4];
1569 for (m = m0; m; m = m->m_next) {
1571 u_char *rptr = (u_char *)m->m_data;
1574 if (bp > buf + sizeof(buf) - 4)
1576 *bp++ = hex2ascii(*rptr >> 4);
1577 *bp++ = hex2ascii(*rptr++ & 0xf);
1581 if (bp > buf + sizeof(buf) - 3)
1591 kprintf("%s\n", buf);
1595 * a wrapper to transmit a packet from if_start since ALTQ uses
1596 * if_start to send a packet.
1599 ppp_ifstart(struct ifnet *ifp)
1601 struct ppp_softc *sc;
1604 (*sc->sc_start)(sc);