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;
723 struct altq_pktattr pktattr;
725 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
726 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
727 error = ENETDOWN; /* sort of */
731 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
734 * Compute PPP header.
736 m0->m_flags &= ~M_HIGHPRI;
737 switch (dst->sa_family) {
740 address = PPP_ALLSTATIONS;
743 mode = sc->sc_npmode[NP_IP];
746 * If this packet has the "low delay" bit set in the IP header,
747 * put it on the fastq instead.
749 ip = mtod(m0, struct ip *);
750 if (ip->ip_tos & IPTOS_LOWDELAY)
751 m0->m_flags |= M_HIGHPRI;
757 * This is pretty bogus.. We dont have an ipxcp module in pppd
758 * yet to configure the link parameters. Sigh. I guess a
759 * manual ifconfig would do.... -Peter
761 address = PPP_ALLSTATIONS;
768 address = PPP_ADDRESS(dst->sa_data);
769 control = PPP_CONTROL(dst->sa_data);
770 protocol = PPP_PROTOCOL(dst->sa_data);
774 kprintf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
775 error = EAFNOSUPPORT;
780 * Drop this packet, or return an error, if necessary.
782 if (mode == NPMODE_ERROR) {
786 if (mode == NPMODE_DROP) {
792 * Add PPP header. If no space in first mbuf, allocate another.
793 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
795 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
796 m0 = m_prepend(m0, PPP_HDRLEN, MB_DONTWAIT);
803 m0->m_data -= PPP_HDRLEN;
805 cp = mtod(m0, u_char *);
808 *cp++ = protocol >> 8;
809 *cp++ = protocol & 0xff;
810 m0->m_len += PPP_HDRLEN;
813 for (m = m0; m != 0; m = m->m_next)
816 if (sc->sc_flags & SC_LOG_OUTPKT) {
817 kprintf("%s output: ", ifp->if_xname);
821 if ((protocol & 0x8000) == 0) {
824 * Apply the pass and active filters to the packet,
825 * but only if it is a data packet.
827 *mtod(m0, u_char *) = 1; /* indicates outbound */
828 if (sc->sc_pass_filt.bf_insns != 0
829 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
831 error = 0; /* drop this packet */
836 * Update the time we sent the most recent packet.
838 if (sc->sc_active_filt.bf_insns == 0
839 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
840 sc->sc_last_sent = time_second;
842 *mtod(m0, u_char *) = address;
845 * Update the time we sent the most recent data packet.
847 sc->sc_last_sent = time_second;
848 #endif /* PPP_FILTER */
854 * Put the packet on the appropriate queue.
857 if (mode == NPMODE_QUEUE) {
858 /* XXX we should limit the number of packets on this queue */
859 *sc->sc_npqtail = m0;
860 m0->m_nextpkt = NULL;
861 sc->sc_npqtail = &m0->m_nextpkt;
863 /* fastq and if_snd are emptied at spl[soft]net now */
864 if ((m0->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
866 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
875 ASSERT_IFNET_SERIALIZED_TX(&sc->sc_if);
876 error = ifq_enqueue(&sc->sc_if.if_snd, m0, &pktattr);
880 sc->sc_if.if_oerrors++;
881 sc->sc_stats.ppp_oerrors++;
886 getmicrotime(&ifp->if_lastchange);
888 ifp->if_obytes += len;
899 pppoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
904 ifnet_serialize_tx(ifp);
905 error = pppoutput_serialized(ifp, m0, dst, rtp);
906 ifnet_deserialize_tx(ifp);
912 * After a change in the NPmode for some NP, move packets from the
913 * npqueue to the send queue or the fast queue as appropriate.
914 * Should be called at spl[soft]net.
917 ppp_requeue(struct ppp_softc *sc)
919 struct mbuf *m, **mpp;
924 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
925 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
927 mode = sc->sc_npmode[NP_IP];
936 * This packet can now go on one of the queues to be sent.
940 if ((m->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
950 error = ifq_enqueue(&sc->sc_if.if_snd, m, NULL);
953 sc->sc_if.if_oerrors++;
954 sc->sc_stats.ppp_oerrors++;
969 sc->sc_npqtail = mpp;
973 * Transmitter has finished outputting some stuff;
974 * remember to call sc->sc_start later at splsoftnet.
977 ppp_restart(struct ppp_softc *sc)
980 sc->sc_flags &= ~SC_TBUSY;
981 schednetisr(NETISR_PPP);
987 * Get a packet to send. This procedure is intended to be called at
988 * splsoftnet, since it may involve time-consuming operations such as
989 * applying VJ compression, packet compression, address/control and/or
990 * protocol field compression to the packet.
993 ppp_dequeue(struct ppp_softc *sc)
997 int address, control, protocol;
1000 * Grab a packet to send: first try the fast queue, then the
1003 IF_DEQUEUE(&sc->sc_fastq, m);
1005 m = ifq_dequeue(&sc->sc_if.if_snd, NULL);
1009 ++sc->sc_stats.ppp_opackets;
1012 * Extract the ppp header of the new packet.
1013 * The ppp header will be in one mbuf.
1015 cp = mtod(m, u_char *);
1016 address = PPP_ADDRESS(cp);
1017 control = PPP_CONTROL(cp);
1018 protocol = PPP_PROTOCOL(cp);
1024 * If the packet is a TCP/IP packet, see if we can compress it.
1026 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1031 ip = (struct ip *) (cp + PPP_HDRLEN);
1032 if (mp->m_len <= PPP_HDRLEN) {
1036 ip = mtod(mp, struct ip *);
1038 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1039 if (ip->ip_p == IPPROTO_TCP) {
1040 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1041 !(sc->sc_flags & SC_NO_TCP_CCID));
1043 case TYPE_UNCOMPRESSED_TCP:
1044 protocol = PPP_VJC_UNCOMP;
1046 case TYPE_COMPRESSED_TCP:
1047 protocol = PPP_VJC_COMP;
1048 cp = mtod(m, u_char *);
1049 cp[0] = address; /* header has moved */
1054 cp[3] = protocol; /* update protocol in PPP header */
1064 #endif /* PPP_COMPRESS */
1068 if (protocol != PPP_LCP && protocol != PPP_CCP
1069 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1070 struct mbuf *mcomp = NULL;
1074 for (mp = m; mp != NULL; mp = mp->m_next)
1076 clen = (*sc->sc_xcomp->compress)
1077 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1078 if (mcomp != NULL) {
1079 if (sc->sc_flags & SC_CCP_UP) {
1080 /* Send the compressed packet instead of the original. */
1083 cp = mtod(m, u_char *);
1086 /* Can't transmit compressed packets until CCP is up. */
1091 #endif /* PPP_COMPRESS */
1094 * Compress the address/control and protocol, if possible.
1096 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1097 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1098 protocol != PPP_LCP) {
1099 /* can compress address/control */
1103 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1104 /* can compress protocol */
1105 if (mtod(m, u_char *) == cp) {
1106 cp[2] = cp[1]; /* move address/control up */
1118 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1119 * 0 if it is about to be transmitted.
1122 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
1129 * Get a pointer to the data after the PPP header.
1131 if (m->m_len <= PPP_HDRLEN) {
1135 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1138 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1141 ep = mtod(mp, u_char *) + mp->m_len;
1142 if (dp + CCP_HDRLEN > ep)
1144 slen = CCP_LENGTH(dp);
1145 if (dp + slen > ep) {
1146 if (sc->sc_flags & SC_DEBUG)
1147 kprintf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1148 dp, slen, mtod(mp, u_char *), mp->m_len);
1152 switch (CCP_CODE(dp)) {
1156 /* CCP must be going down - disable compression */
1157 if (sc->sc_flags & SC_CCP_UP) {
1159 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1165 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1166 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1167 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1169 /* we're agreeing to send compressed packets. */
1170 if (sc->sc_xc_state != NULL
1171 && (*sc->sc_xcomp->comp_init)
1172 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1173 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1175 sc->sc_flags |= SC_COMP_RUN;
1179 /* peer is agreeing to send compressed packets. */
1180 if (sc->sc_rc_state != NULL
1181 && (*sc->sc_rcomp->decomp_init)
1182 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1183 sc->sc_if.if_dunit, 0, sc->sc_mru,
1184 sc->sc_flags & SC_DEBUG)) {
1186 sc->sc_flags |= SC_DECOMP_RUN;
1187 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1195 if (sc->sc_flags & SC_CCP_UP) {
1197 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1198 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1200 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1201 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1203 sc->sc_flags &= ~SC_DC_ERROR;
1213 * CCP is down; free (de)compressor state if necessary.
1216 ppp_ccp_closed(struct ppp_softc *sc)
1218 if (sc->sc_xc_state) {
1219 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1220 sc->sc_xc_state = NULL;
1222 if (sc->sc_rc_state) {
1223 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1224 sc->sc_rc_state = NULL;
1227 #endif /* PPP_COMPRESS */
1230 * PPP packet input routine.
1231 * The caller has checked and removed the FCS and has inserted
1232 * the address/control bytes and the protocol high byte if they
1236 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
1241 m->m_flags |= M_ERRMARK;
1242 IF_ENQUEUE(&sc->sc_rawq, m);
1243 schednetisr(NETISR_PPP);
1249 * Process a received PPP packet, doing decompression as necessary.
1250 * Should be called at splsoftnet.
1252 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1253 TYPE_UNCOMPRESSED_TCP)
1256 ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
1258 struct ifnet *ifp = &sc->sc_if;
1260 int ilen = 0, xlen, proto, rv;
1261 u_char *cp, adrs, ctrl;
1262 struct mbuf *mp, *dmp = NULL;
1266 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1268 sc->sc_stats.ppp_ipackets++;
1270 if (sc->sc_flags & SC_LOG_INPKT) {
1272 for (mp = m; mp != NULL; mp = mp->m_next)
1274 kprintf("%s: got %d bytes\n", ifp->if_xname, ilen);
1278 cp = mtod(m, u_char *);
1279 adrs = PPP_ADDRESS(cp);
1280 ctrl = PPP_CONTROL(cp);
1281 proto = PPP_PROTOCOL(cp);
1283 if (m->m_flags & M_ERRMARK) {
1284 m->m_flags &= ~M_ERRMARK;
1286 sc->sc_flags |= SC_VJ_RESET;
1292 * Decompress this packet if necessary, update the receiver's
1293 * dictionary, or take appropriate action on a CCP packet.
1295 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1296 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1297 /* decompress this packet */
1298 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1299 if (rv == DECOMP_OK) {
1302 /* no error, but no decompressed packet produced */
1306 cp = mtod(m, u_char *);
1307 proto = PPP_PROTOCOL(cp);
1311 * An error has occurred in decompression.
1312 * Pass the compressed packet up to pppd, which may take
1313 * CCP down or issue a Reset-Req.
1315 if (sc->sc_flags & SC_DEBUG)
1316 kprintf("%s: decompress failed %d\n", ifp->if_xname, rv);
1318 sc->sc_flags |= SC_VJ_RESET;
1319 if (rv == DECOMP_ERROR)
1320 sc->sc_flags |= SC_DC_ERROR;
1322 sc->sc_flags |= SC_DC_FERROR;
1327 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1328 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1330 if (proto == PPP_CCP) {
1337 for (mp = m; mp != NULL; mp = mp->m_next)
1341 if (sc->sc_flags & SC_VJ_RESET) {
1343 * If we've missed a packet, we must toss subsequent compressed
1344 * packets which don't have an explicit connection ID.
1347 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1349 sc->sc_flags &= ~SC_VJ_RESET;
1354 * See if we have a VJ-compressed packet to uncompress.
1356 if (proto == PPP_VJC_COMP) {
1357 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1360 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1361 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1362 sc->sc_comp, &iphdr, &hlen);
1365 if (sc->sc_flags & SC_DEBUG)
1366 kprintf("%s: VJ uncompress failed on type comp\n",
1371 /* Copy the PPP and IP headers into a new mbuf. */
1372 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1377 if (hlen + PPP_HDRLEN > MHLEN) {
1378 MCLGET(mp, MB_DONTWAIT);
1379 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1381 goto bad; /* lose if big headers and no clusters */
1384 cp = mtod(mp, u_char *);
1390 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1391 mp->m_len = hlen + PPP_HDRLEN;
1394 * Trim the PPP and VJ headers off the old mbuf
1395 * and stick the new and old mbufs together.
1397 m->m_data += PPP_HDRLEN + xlen;
1398 m->m_len -= PPP_HDRLEN + xlen;
1399 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1400 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1401 mp->m_len += m->m_len;
1402 mp->m_next = m_free(m);
1407 ilen += hlen - xlen;
1409 } else if (proto == PPP_VJC_UNCOMP) {
1410 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1413 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1414 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1415 sc->sc_comp, &iphdr, &hlen);
1418 if (sc->sc_flags & SC_DEBUG)
1419 kprintf("%s: VJ uncompress failed on type uncomp\n",
1430 * If the packet will fit in a header mbuf, don't waste a
1431 * whole cluster on it.
1433 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1434 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1436 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1442 m->m_pkthdr.len = ilen;
1443 m->m_pkthdr.rcvif = ifp;
1445 if ((proto & 0x8000) == 0) {
1448 * See whether we want to pass this packet, and
1449 * if it counts as link activity.
1451 adrs = *mtod(m, u_char *); /* save address field */
1452 *mtod(m, u_char *) = 0; /* indicate inbound */
1453 if (sc->sc_pass_filt.bf_insns != 0
1454 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1456 /* drop this packet */
1460 if (sc->sc_active_filt.bf_insns == 0
1461 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1462 sc->sc_last_recv = time_second;
1464 *mtod(m, u_char *) = adrs;
1467 * Record the time that we received this packet.
1469 sc->sc_last_recv = time_second;
1470 #endif /* PPP_FILTER */
1473 BPF_MTAP(&sc->sc_if, m);
1480 * IP packet - take off the ppp header and pass it up to IP.
1482 if ((ifp->if_flags & IFF_UP) == 0
1483 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1484 /* interface is down - drop the packet. */
1488 m->m_pkthdr.len -= PPP_HDRLEN;
1489 m->m_data += PPP_HDRLEN;
1490 m->m_len -= PPP_HDRLEN;
1492 if (ipflow_fastforward(m))
1501 * IPX packet - take off the ppp header and pass it up to IPX.
1503 if ((sc->sc_if.if_flags & IFF_UP) == 0
1504 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1505 /* interface is down - drop the packet. */
1509 m->m_pkthdr.len -= PPP_HDRLEN;
1510 m->m_data += PPP_HDRLEN;
1511 m->m_len -= PPP_HDRLEN;
1513 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1519 * Some other protocol - place on input queue for read().
1525 * If we found a netproto just pass it to the proto. Otherwise queue
1526 * the packet to the tty (e.g. pppd). Note that sc_ctlp() must be
1527 * called EXACTLY once per packet queued.
1530 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1534 rv = (netisr_queue(isr, m) == 0);
1537 if (sc->sc_flags & SC_DEBUG)
1538 kprintf("%s: input queue full\n", ifp->if_xname);
1544 ifp->if_ibytes += ilen;
1545 getmicrotime(&ifp->if_lastchange);
1551 sc->sc_if.if_ierrors++;
1552 sc->sc_stats.ppp_ierrors++;
1555 #define MAX_DUMP_BYTES 128
1558 pppdumpm(struct mbuf *m0)
1560 char buf[3*MAX_DUMP_BYTES+4];
1564 for (m = m0; m; m = m->m_next) {
1566 u_char *rptr = (u_char *)m->m_data;
1569 if (bp > buf + sizeof(buf) - 4)
1571 *bp++ = hex2ascii(*rptr >> 4);
1572 *bp++ = hex2ascii(*rptr++ & 0xf);
1576 if (bp > buf + sizeof(buf) - 3)
1586 kprintf("%s\n", buf);
1590 * a wrapper to transmit a packet from if_start since ALTQ uses
1591 * if_start to send a packet.
1594 ppp_ifstart(struct ifnet *ifp)
1596 struct ppp_softc *sc;
1599 (*sc->sc_start)(sc);