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).
71 * Rewritten for NextStep's funky kernel functions, I/O threads,
72 * and netbufs (instead of real mbufs). Also, ifnets don't install
73 * into the kernel under NS as they do under BSD. We have tried to
74 * make the code remain as similar to the NetBSD version without
75 * incurring too much hassle. This code is the merge of
76 * Philip Prindeville's <philipp@res.enst.fr>/Pete French's <pete@ohm.york.ac.uk>
77 * and Stephen Perkins' <perkins@cps.msu.edu> independent ports.
81 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
84 static char sccsid[] = "$Revision: 1.2 $ ($Date: 1997/04/30 05:39:30 $)";
88 #define KERNEL_FEATURES 1
91 /* #include "ppp.h" */
94 #include <sys/param.h>
97 #include <sys/socket.h>
100 #include <sys/ioctl.h>
101 #include <sys/errno.h>
102 #include <kernserv/prototypes.h>
106 #include <driverkit/generalFuncs.h>
107 #import <kernserv/machine/spl.h>
109 #if defined(sparc) || defined(m68k)
110 #include <machine/psl.h>
112 #include <kernserv/kern_server_types.h>
115 #include <net/route.h>
118 #include <netinet/in.h>
119 #include <netinet/in_systm.h>
120 #include <netinet/in_var.h>
121 #include <netinet/ip.h>
122 #include <netinet/tcp.h>
126 #include <net/ppp_defs.h>
128 #include <net/vjcompress.h>
130 #include <net/if_ppp.h>
131 #include "if_pppvar.h"
133 struct ppp_softc ppp_softc[NUM_PPP];
137 #define PACKETPTR NETBUF_T
138 #include <net/ppp-comp.h>
144 * We store the address of necessary BPF functions
147 struct bpf_fns fnarg;
151 * The max number of NETBUF_Ts we wish to compress and cache for
154 #define COMPRESS_CACHE_LEN 1
159 * Necessary to avoid redefinition warnings or bogus type casts later.
161 int pppoutput __P((netif_t ifp, netbuf_t m, void *arg));
162 int pppsioctl __P((netif_t ifp, int cmd, caddr_t data));
163 int pppcontrol __P((netif_t ifp, const char *cmd, void *data));
164 void pppintr_comp __P((void *arg));
165 void pppintr_decomp __P((void *arg));
166 void pppfillfreeq __P((void *arg));
167 void pppgetm __P((register struct ppp_softc *sc));
169 static void ppp_requeue __P((struct ppp_softc *));
170 static void ppp_outpkt __P((struct ppp_softc *));
171 static void ppp_ccp __P((struct ppp_softc *, NETBUF_T, int rcvd));
172 static void ppp_ccp_closed __P((struct ppp_softc *));
173 static void ppp_inproc __P((struct ppp_softc *, NETBUF_T));
174 static void pppdumpm __P((NETBUF_T));
176 extern int install_ppp_ld __P((void));
177 extern int tty_ld_remove __P((int));
180 * We steal two bits in the mbuf m_flags, to mark high-priority packets
181 * for output, and received packets following lost/corrupted packets.
183 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
184 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
187 * The following disgusting hack gets around the problem that IP TOS
188 * can't be set yet. We want to put "interactive" traffic on a high
189 * priority queue. To decide if traffic is interactive, we check that
190 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
192 static u_short interactive_ports[8] = {
197 enum { QFREE, QRAW, QFAST, QSLOW, QIN, QNP, QCACHE };
199 static struct qparms qparms[] = {
200 {20, 40, 50, "free"}, /* freeq */
201 {5, 20, 25, "raw"}, /* rawq */
202 {5, 20, 25, "fast"}, /* fastq */
203 {5, 20, 25, "slow"}, /* slowq */
204 {5, 20, 25, "in"}, /* inq */
205 {5, 20, 25, "np"}, /* npq */
206 {0, COMPRESS_CACHE_LEN, COMPRESS_CACHE_LEN, "cache"} /* cache */
209 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
211 #ifndef IPTOS_LOWDELAY
212 #define IPTOS_LOWDELAY 0x10
217 * List of compressors we know about.
218 * We leave some space so maybe we can modload compressors.
221 extern struct compressor ppp_bsd_compress;
223 struct compressor *ppp_compressors[8] = {
229 #endif /* PPP_COMPRESS */
231 /* yeah, we sometimes have to change the MTU after having created the
232 * device. Let's hope this doesn't break anything!!!
234 #define if_mtu_set(ifn,mtu) (((struct ifnet *)ifn)->if_mtu = mtu)
236 extern int ipforwarding;
237 extern int ipsendredirects;
239 kern_server_t instance;
242 * Sigh. Should be defined in <net/if.h> but isn't...
247 u_long ifru_asyncmap;
254 * Returns a new "outgoing" netbuf.
256 * Must return an actual netbuf_t since other protocols
257 * use this to get our buffers. Before releasing, save
258 * any space we may need when the buffer returns.
262 pppgetbuf(netif_t ifp)
266 int len = MAX(if_mtu(ifp), PPP_MTU) + PPP_HDRLEN + PPP_FCSLEN;
270 NB_SHRINK_TOP(nb, PPP_HDRLEN);
271 NB_SHRINK_BOT(nb, PPP_FCSLEN); /* grown by pppstart() */
277 * Called from boot code to establish ppp interfaces.
282 register struct ppp_softc *sc;
285 IOLog("\nPPP version 2.3b3 for NS 3.2 and 3.3\n");
286 IOLog("LKS: %s\n", sccsid);
287 IOLog("by Stephen Perkins, Philip Prindeville, and Pete French\n");
289 IOLog("Installing PPP on Line Discipline %d\n", PPPDISC);
290 if (install_ppp_ld() < 0) {
291 IOLog("ppp: Could not install line discipline\n");
295 IOLog("Installing interfaces:\n");
296 for (sc = ppp_softc; i < NUM_PPP; sc++, i++) {
297 sc->sc_if = if_attach(NULL, NULL, pppoutput,
298 pppgetbuf, pppcontrol, "ppp", i, "Serial line PPP",
299 PPP_MTU, IFF_POINTOPOINT, NETIFCLASS_VIRTUAL, (void *) sc);
300 IOLog(" Initializing ppp%d\n", i);
301 nbq_init(&sc->sc_freeq, &qparms[QFREE]);
302 nbq_init(&sc->sc_rawq, &qparms[QRAW]);
303 nbq_init(&sc->sc_fastq, &qparms[QFAST]);
304 nbq_init(&sc->sc_slowq, &qparms[QSLOW]);
305 nbq_init(&sc->sc_inq, &qparms[QIN]);
306 nbq_init(&sc->sc_npq, &qparms[QNP]);
307 nbq_init(&sc->sc_compq, &qparms[QCACHE]);
308 IOLog(" ppp%d successfully attached.\n", i);
310 bpfattach((caddr_t *) &sc->sc_bpf, sc->sc_if, DLT_PPP, PPP_HDRLEN);
317 IOLog("PPP-2.3 Successfully Installed.\n\n");
323 struct ppp_softc *sc;
326 IOLog("Removing PPP on Line Discipline %d\n", PPPDISC);
327 if (!tty_ld_remove(PPPDISC))
328 IOLog("ppp: Could not remove line discipline\n");
330 IOLog("Removing interfaces:\n");
331 for (sc = ppp_softc, i = 0; i < NUM_PPP; sc++, i++) {
332 nbq_free(&sc->sc_freeq);
333 nbq_free(&sc->sc_rawq);
334 nbq_free(&sc->sc_fastq);
335 nbq_free(&sc->sc_slowq);
336 nbq_free(&sc->sc_inq);
337 nbq_free(&sc->sc_npq);
338 nbq_free(&sc->sc_compq);
339 if_detach(sc->sc_if);
340 /* no idea why we need this, but... */
341 bzero(sc->sc_if, sizeof(netif_t));
342 IOLog(" ppp%d successfully detached.\n", i);
344 IOLog("PPP-2.3 Successfully Removed.\n\n");
349 * Allocate a ppp interface unit and initialize it.
356 struct ppp_softc *sc;
358 for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)
359 if (sc->sc_xfer == pid) {
360 IOLogDbg("ppp%d: alloc'ing unit %d to proc %d\n", nppp, nppp, pid);
364 for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)
365 if (sc->sc_devp == NULL)
371 sc->sc_mru = PPP_MRU;
372 sc->sc_relinq = NULL;
374 vj_compress_init(&sc->sc_comp, -1);
377 sc->sc_xc_state = NULL;
378 sc->sc_rc_state = NULL;
379 #endif /* PPP_COMPRESS */
380 for (i = 0; i < NUM_NP; ++i)
381 sc->sc_npmode[i] = NPMODE_ERROR;
382 /* XXX - I'm not sure why the npqueue was zapped here... */
383 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
384 sc->sc_compsched = 0;
385 sc->sc_decompsched = 0;
388 * XXX -- We need to get packets here, and we don't care if we do block...
389 * We do this after we set the sc_mru.
391 pppfillfreeq((void *) sc);
397 * Deallocate a ppp unit. Must be called at splnet or higher.
401 struct ppp_softc *sc;
404 if_flags_set(sc->sc_if, if_flags(sc->sc_if) & ~(IFF_UP|IFF_RUNNING));
407 nbq_flush(&sc->sc_freeq);
408 nbq_flush(&sc->sc_rawq);
409 nbq_flush(&sc->sc_inq);
410 nbq_flush(&sc->sc_fastq);
411 nbq_flush(&sc->sc_slowq);
412 nbq_flush(&sc->sc_npq);
413 nbq_flush(&sc->sc_compq);
416 sc->sc_xc_state = NULL;
417 sc->sc_rc_state = NULL;
418 #endif /* PPP_COMPRESS */
424 * Ioctl routine for generic ppp devices.
427 pppioctl(sc, cmd, data, flag)
428 struct ppp_softc *sc;
433 struct proc *p = curproc;
434 int s, error, flags, mru, nb, npx, oldflags;
435 struct ppp_option_data *odp;
436 struct compressor **cp;
440 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
443 #ifdef HAS_BROKEN_TIOCSPGRP
444 struct tty *tp = sc->sc_devp;
449 s = splimp(); /* paranoid; splnet probably ok */
450 if ((m = nbq_peek(&sc->sc_inq)) != NULL)
451 *(int *)data = NB_SIZE(m);
458 *(int *)data = if_unit(sc->sc_if);
462 *(u_int *)data = sc->sc_flags;
468 flags = *(int *)data & SC_MASK;
471 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
475 oldflags = sc->sc_flags;
476 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
485 IOLogDbg("ppp%d: setting mru %d\n", if_unit(sc->sc_if), mru);
487 if (mru >= PPP_MRU && mru <= PPP_MAXMRU) {
489 /* To make sure we handle the received packet
490 * correctly, we do two things. First, we
491 * empty out the free_q. We then remove
492 * the current input buffer, set the input length
493 * to zero, and set the flush flag.
496 nbq_flush(&sc->sc_freeq); /* get rid of old buffers */
501 if (sc->sc_ilen != 0)
502 sc->sc_flags |= SC_FLUSH;
506 pppfillfreeq((void *) sc); /* and make a queue of new ones */
512 *(int *)data = sc->sc_mru;
520 vj_compress_init(&sc->sc_comp, *(int *)data);
528 sc->sc_xfer = p->p_pid;
532 case PPPIOCSCOMPRESS:
535 odp = (struct ppp_option_data *) data;
537 if (nb > sizeof(ccp_option))
538 nb = sizeof(ccp_option);
539 if (error = copyin(odp->ptr, ccp_option, nb))
541 if (ccp_option[1] < 2) /* preliminary check on the length byte */
543 for (cp = ppp_compressors; *cp != NULL; ++cp)
544 if ((*cp)->compress_proto == ccp_option[0]) {
546 * Found a handler for the protocol - try to allocate
547 * a compressor or decompressor.
552 if (sc->sc_xc_state != NULL)
553 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
554 sc->sc_xcomp = *cp; /* entry points for compressor */
555 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
556 if (sc->sc_xc_state == NULL) {
557 IOLogDbg("ppp%d: comp_alloc failed", if_unit(sc->sc_if));
561 sc->sc_flags &= ~SC_COMP_RUN;
563 if (sc->sc_rc_state != NULL)
564 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
565 sc->sc_rcomp = *cp; /* entry points for compressor */
566 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
567 if (sc->sc_rc_state == NULL) {
568 IOLogDbg("ppp%d: decomp_alloc failed", if_unit(sc->sc_if));
572 sc->sc_flags &= ~SC_DECOMP_RUN;
577 IOLogDbg("ppp%d: no compressor for [%x %x %x], %x", if_unit(sc->sc_if),
578 ccp_option[0], ccp_option[1], ccp_option[2], nb);
579 return (EINVAL); /* no handler found */
580 #endif /* PPP_COMPRESS */
582 #ifdef HAS_BROKEN_TIOCSPGRP
584 tp->t_pgrp = *(int *)data;
590 npi = (struct npioctl *) data;
591 switch (npi->protocol) {
598 if (cmd == PPPIOCGNPMODE) {
599 npi->mode = sc->sc_npmode[npx];
603 if (npi->mode != sc->sc_npmode[npx]) {
605 sc->sc_npmode[npx] = npi->mode;
606 if (npi->mode != NPMODE_QUEUE) {
618 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
619 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
630 pppcontrol(ifp, cmd, data)
636 if (!strcmp(cmd, IFCONTROL_UNIXIOCTL)) {
637 if_ioctl_t* ctl = (if_ioctl_t*)data;
638 return pppsioctl(ifp,
641 } else if (!strcmp(cmd, IFCONTROL_SETADDR)) {
642 struct sockaddr_in *sin = (struct sockaddr_in *)data;
643 if (sin->sin_family != AF_INET)
645 if_flags_set(ifp, if_flags(ifp) | IFF_UP);
649 * We implement this to allow iftab
650 * to contain -AUTOMATIC- entries
651 * without generating errors at boot time.
652 * We do not, however, mark it as UP.
654 else if (!strcmp(cmd, IFCONTROL_AUTOADDR)) {
655 struct sockaddr_in *sin = (struct sockaddr_in *) data;
656 if (sin->sin_family != AF_INET)
659 } else if (!strcmp(cmd, IFCONTROL_SETFLAGS)) {
660 register union ifr_ifru *ifr = (union ifr_ifru *)data;
663 if_flags_set(ifp, ifr->ifru_flags);
667 * Under 3.2 developer, I don't know the symbol for this
668 * new 3.3 command. So it is a constant for now. I don't
669 * believe I need to do anything to support this at the moment.
671 else if (strcmp(cmd, "add-multicast") == 0) {
672 struct sockaddr_in *sin = (struct sockaddr_in *) data;
673 if (sin->sin_family != AF_INET)
676 IOLog("ppp%d: Invalid ppp control %s\n", if_unit(ifp), cmd);
682 * Process an ioctl request to the ppp network interface.
685 pppsioctl(ifp, cmd, data)
686 register netif_t ifp;
690 register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
691 register struct ifaddr *ifa = (struct ifaddr *)data;
692 register struct ifreq *ifr = (struct ifreq *)data;
693 struct ppp_stats *psp;
695 struct ppp_comp_stats *pcp;
697 int s = splimp(), error = 0;
701 IOLog("ppp%d: pppioctl: SIOCSIFFLAGS called!\n", if_unit(ifp));
705 if (ifa->ifa_addr.sa_family != AF_INET)
706 error = EAFNOSUPPORT;
710 if (ifa->ifa_addr.sa_family != AF_INET)
711 error = EAFNOSUPPORT;
719 if_mtu_set(sc->sc_if, ifr->ifr_mtu);
720 nbq_flush(&sc->sc_freeq); /* get rid of old buffers */
721 pppsched(pppfillfreeq, sc); /* and make a queue of new ones */
726 ifr->ifr_mtu = if_mtu(sc->sc_if);
730 psp = &((struct ifpppstatsreq *) data)->stats;
731 bzero(psp, sizeof(*psp));
732 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
733 psp->p.ppp_ipackets = if_ipackets(sc->sc_if);
734 psp->p.ppp_ierrors = if_ierrors(sc->sc_if);
735 psp->p.ppp_obytes = sc->sc_bytessent;
736 psp->p.ppp_opackets = if_opackets(sc->sc_if);
737 psp->p.ppp_oerrors = if_oerrors(sc->sc_if);
739 psp->vj.vjs_packets = sc->sc_comp.stats.vjs_packets;
740 psp->vj.vjs_compressed = sc->sc_comp.stats.vjs_compressed;
741 psp->vj.vjs_searches = sc->sc_comp.stats.vjs_searches;
742 psp->vj.vjs_misses = sc->sc_comp.stats.vjs_misses;
743 psp->vj.vjs_uncompressedin = sc->sc_comp.stats.vjs_uncompressedin;
744 psp->vj.vjs_compressedin = sc->sc_comp.stats.vjs_compressedin;
745 psp->vj.vjs_errorin = sc->sc_comp.stats.vjs_errorin;
746 psp->vj.vjs_tossed = sc->sc_comp.stats.vjs_tossed;
752 pcp = &((struct ifpppcstatsreq *) data)->stats;
753 bzero(pcp, sizeof(*pcp));
754 if (sc->sc_xc_state != NULL)
755 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
756 if (sc->sc_rc_state != NULL)
757 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
759 #endif /* PPP_COMPRESS */
769 * Queue a packet. Start transmission if not active.
770 * Packet is placed in Information field of PPP frame.
772 * This procedure MUST take an actual netbuf_t as input
773 * since it may be called by procedures outside of us.
774 * The buffer received must be in the same format as that
775 * returned by pppgetbuf().
778 pppoutput(ifp, in_nb, arg)
783 register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
784 struct sockaddr *dst = (struct sockaddr *) arg;
785 int protocol, address, control;
790 struct nb_queue *ifq;
794 m0 = nb_TO_NB(in_nb);
796 if (sc->sc_devp == NULL || (if_flags(ifp) & IFF_RUNNING) == 0
797 || (if_flags(ifp) & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
798 error = ENETDOWN; /* sort of */
803 m0->pktinfo.first.tv_sec = time.tv_sec;
804 m0->pktinfo.first.tv_usec = time.tv_usec;
805 m0->pktinfo.size1 = NB_SIZE(m0);
809 * Compute PPP header.
812 switch (dst->sa_family) {
815 address = PPP_ALLSTATIONS;
818 mode = sc->sc_npmode[NP_IP];
821 * If this packet has the "low delay" bit set in the IP header,
822 * or TCP and to an interactive port, put it on the fastq instead
824 ip = mtod(m0, struct ip *);
825 if (ip->ip_tos & IPTOS_LOWDELAY || ip->ip_p == IPPROTO_ICMP)
827 else if (ip->ip_p == IPPROTO_TCP) {
828 register u_short *p = (u_short *) &(((caddr_t) ip)[ip->ip_hl << 2]);
829 if (INTERACTIVE(ntohs(p[0])) || INTERACTIVE(ntohs(p[1])))
830 urgent: flags |= M_HIGHPRI;
836 address = PPP_ALLSTATIONS;
843 address = PPP_ADDRESS(dst->sa_data);
844 control = PPP_CONTROL(dst->sa_data);
845 protocol = PPP_PROTOCOL(dst->sa_data);
849 IOLog("ppp%d: af%d not supported\n", if_unit(ifp), dst->sa_family);
850 error = EAFNOSUPPORT;
855 * Drop this packet, or return an error, if necessary.
857 if (mode == NPMODE_ERROR) {
861 if (mode == NPMODE_DROP) {
869 NB_GROW_TOP(m0, PPP_HDRLEN);
871 cp = mtod(m0, u_char *);
874 *cp++ = protocol >> 8;
875 *cp++ = protocol & 0xff;
877 #if defined(NBPFILTER) && defined(NETBUF_PROXY)
879 * We need to copy the header to the original copy.
881 bcopy(mtod(m0, u_char *), NB_MAP_ORIG(m0), PPP_HDRLEN);
885 if (sc->sc_flags & SC_LOG_OUTPKT) {
886 IOLog("ppp%d: output:\n", if_unit(ifp)); /* XXX */
891 #if defined(NBPFILTER) && !defined(NETBUF_PROXY)
893 * See if bpf wants to look at the packet.
894 * We must hand it off _before_ any compression
895 * takes place. If NETBUF_PROXY is defined,
896 * we do the handoff later (in ppp_tty.c)
897 * because we keep a copy of the original
902 bpf_tap(sc->sc_bpf, NB_MAP(m0), NB_SIZE(m0));
908 * Put the packet on the appropriate queue.
910 s = splimp(); /* splnet should be OK now */
911 if (mode == NPMODE_QUEUE) {
912 NB_SET_MARK(m0,flags); /* save priority */
913 /* XXX we should limit the number of packets on this queue */
914 nbq_enqueue(&sc->sc_npq, m0); /* XXX is this correct? */
916 ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq;
917 if (nbq_full(ifq) < 0) {
919 IOLog("ppp%d: output queue full\n", if_unit(sc->sc_if));
921 incr_cnt(sc->sc_if, if_oerrors);
925 nbq_enqueue(ifq, m0);
929 * If we don't have some compressed packets already
930 * and we are not at interrupt priority, then do some compression.
932 * We need to be especially careful here. pppouput() is typically
933 * called at 2 different priority levels. On a NeXT, neither of these
934 * is the interrupt priority level. However, on Intel, one of them is.
935 * I don't know about HPPA or Sparc. Simple fix is to just check.
938 if(!sc->sc_compsched && s != ipltospl(IPLIMP)) {
939 sc->sc_compsched = 1;
941 pppintr_comp(sc); /* Calls pppstart() */
956 * After a change in the NPmode for some NP, move packets from the
957 * npqueue to the send queue or the fast queue as appropriate.
958 * Should be called at splimp (actually splnet would probably suffice).
959 * Due to some of the uglies in the packet queueing system I have
960 * implemented this without the mpp stuff.
966 struct ppp_softc *sc;
969 struct nb_queue *ifq;
974 for (m = sc->sc_npq.head; m; ) {
977 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
979 mode = sc->sc_npmode[NP_IP];
988 * This packet can now go on one of the queues to be sent.
993 sc->sc_npq.head = nm;
995 NB_GET_MARK(m,&flags);
996 ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq;
999 incr_cnt(sc->sc_if, if_oerrors);
1002 nbq_enqueue(ifq, m);
1018 sc->sc_npq.tail = lm; /* anything further on has been sent ! */
1022 * Get a packet to send. This procedure is intended to be called
1023 * at spltty()/splimp(), so it takes little time. If there isn't
1024 * a packet waiting to go out, it schedules a software interrupt
1025 * to prepare a new packet; the device start routine gets called
1026 * again when a packet is ready.
1030 struct ppp_softc *sc;
1035 m = nbq_dequeue(&sc->sc_compq);
1038 if (!sc->sc_compsched &&
1039 (! nbq_empty(&sc->sc_slowq) || ! nbq_empty(&sc->sc_fastq)))
1042 if ((error = pppsched(pppintr_comp, sc)) == KERN_SUCCESS)
1043 sc->sc_compsched = 1;
1046 IOLogDbg("ppp%d: compression callout failed returning %d\n",
1047 if_unit(sc->sc_if), error);
1055 * Takes all received input packets and uncompresses/hands_off.
1056 * Must not be reentrant and is called at normal priority.
1057 * Guaranteed Non-Reentrancy means we don't need to be at splnet().
1065 struct ppp_softc *sc = (struct ppp_softc *)arg;
1069 if (nbq_low(&sc->sc_freeq))
1070 pppfillfreeq((void *) sc);
1074 m = nbq_dequeue(&sc->sc_rawq);
1081 * Now we have aparently emptied the queue. So, we try to reset the
1082 * synchronization flag that schedules callbacks. We check for the
1083 * possibility that an interrupt occurred before we finish this check.
1086 if (!nbq_empty(&sc->sc_rawq))
1093 sc->sc_decompsched = 0;
1101 * Readies the next few output packet from
1102 * the sc_fastq/sc_slowq. Will try to
1103 * precompress all packets on the fast
1104 * queue and at most one from the slow queue.
1110 struct ppp_softc *sc = (struct ppp_softc *)arg;
1114 if (nbq_low(&sc->sc_freeq))
1115 pppfillfreeq((void *) sc);
1118 while (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_fastq))
1121 if (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_slowq))
1124 sc->sc_compsched = 0;
1128 * Grab another packet off a queue and apply VJ compression,
1129 * packet compression, address/control and/or protocol compression
1130 * if enabled. Should be called at splnet.
1134 struct ppp_softc *sc;
1139 int address, control, protocol;
1142 * Grab a packet to send: first try the fast queue, then the
1145 m = nbq_dequeue(&sc->sc_fastq);
1147 m = nbq_dequeue(&sc->sc_slowq);
1152 * Extract the ppp header of the new packet.
1153 * The ppp header will be in one netbuf.
1155 cp = mtod(m, u_char *);
1156 address = PPP_ADDRESS(cp);
1157 control = PPP_CONTROL(cp);
1158 protocol = PPP_PROTOCOL(cp);
1161 m->pktinfo.second.tv_sec = time.tv_sec;
1162 m->pktinfo.second.tv_usec = time.tv_usec;
1168 * Update the time we sent the most recent packet.
1170 sc->sc_last_sent = time.tv_sec;
1174 * If the packet is a TCP/IP packet, see if we can compress it.
1176 if (sc->sc_flags & SC_COMP_TCP) {
1181 ip = (struct ip *) (cp + PPP_HDRLEN);
1182 /* this code assumes the IP/TCP header is in one netbuf */
1183 if (ip->ip_p == IPPROTO_TCP) {
1184 type = vj_compress_tcp(ip, NB_SIZE(m) - PPP_HDRLEN,
1186 !(sc->sc_flags & SC_NO_TCP_CCID), &vjhdr);
1188 case TYPE_UNCOMPRESSED_TCP:
1189 protocol = PPP_VJC_UNCOMP;
1191 m->pktinfo.flags |= NBFLAG_VJCINC;
1194 case TYPE_COMPRESSED_TCP:
1195 NB_SHRINK_TOP(m, vjhdr - (u_char *) ip);
1197 m->pktinfo.flags |= NBFLAG_VJC;
1199 protocol = PPP_VJC_COMP;
1200 cp = mtod(m, u_char *);
1201 cp[0] = address; /* header has moved */
1206 cp[3] = protocol; /* update protocol in PPP header */
1218 #endif /* PPP_COMPRESS */
1223 m->pktinfo.size2 = NB_SIZE(m);
1228 if (protocol != PPP_LCP && protocol != PPP_CCP
1229 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1235 clen = (*sc->sc_xcomp->compress)
1236 (sc->sc_xc_state, &mcomp, m, slen,
1237 sc->sc_flags & SC_CCP_UP? if_mtu(sc->sc_if): 0);
1240 if (mcomp && (NB_SIZE(mcomp) >= slen))
1241 IOLog("BSD Warning... packet growth: Orig=%d New=%d.\n",
1242 slen, NB_SIZE(mcomp));
1244 if (mcomp != NULL) {
1248 * duplicate all the stat info into the new
1251 m->pktinfo.flags |= NBFLAG_CCP;
1253 NB_DUPLICATE(m, mcomp);
1258 cp = mtod(m, u_char *);
1263 m->pktinfo.flags |= NBFLAG_CCPINC;
1267 #endif /* PPP_COMPRESS */
1270 m->pktinfo.size3 = NB_SIZE(m);
1275 * Compress the address/control and protocol, if possible.
1277 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1278 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1279 protocol != PPP_LCP) {
1280 /* can compress address/control */
1281 NB_SHRINK_TOP(m, 2);
1283 m->pktinfo.flags |= NBFLAG_AC;
1286 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1287 /* can compress protocol */
1288 if (mtod(m, u_char *) == cp) {
1289 cp[2] = cp[1]; /* move address/control up */
1292 NB_SHRINK_TOP(m, 1);
1294 m->pktinfo.flags |= NBFLAG_PC;
1299 m->pktinfo.third.tv_sec = time.tv_sec;
1300 m->pktinfo.third.tv_usec = time.tv_usec;
1304 nbq_enqueue(&sc->sc_compq, m);
1305 (*sc->sc_start)(sc);
1311 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1312 * 0 if it is about to be transmitted.
1315 ppp_ccp(sc, m, rcvd)
1316 struct ppp_softc *sc;
1324 * Get a pointer to the data after the PPP header.
1326 dp = mtod(m, u_char *) + PPP_HDRLEN;
1328 ep = mtod(m, u_char *) + NB_SIZE(m);
1329 if (dp + CCP_HDRLEN > ep)
1331 slen = CCP_LENGTH(dp);
1332 if (dp + slen > ep) {
1333 IOLogDbg("ppp%d: ccp: not enough data in netbuf (%x+%x > %x+%x)\n",
1334 if_unit(sc->sc_if), dp, slen, mtod(m, u_char *), NB_SIZE(m));
1338 switch (CCP_CODE(dp)) {
1342 /* CCP must be going down - disable compression */
1343 if (sc->sc_flags & SC_CCP_UP) {
1345 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1351 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1352 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1353 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1355 /* we're agreeing to send compressed packets. */
1356 if (sc->sc_xc_state != NULL
1357 && (*sc->sc_xcomp->comp_init)
1358 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1359 if_unit(sc->sc_if), 0, sc->sc_flags & SC_DEBUG)) {
1361 sc->sc_flags |= SC_COMP_RUN;
1365 /* peer is agreeing to send compressed packets. */
1366 if (sc->sc_rc_state != NULL
1367 && (*sc->sc_rcomp->decomp_init)
1368 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1373 0, sc->sc_mru, sc->sc_flags & SC_DEBUG)) {
1375 sc->sc_flags |= SC_DECOMP_RUN;
1376 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1384 if (sc->sc_flags & SC_CCP_UP) {
1386 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1387 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1388 nbq_flush(&sc->sc_compq); /* Flush pre-compressed packets */
1391 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1392 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1394 sc->sc_flags &= ~SC_DC_ERROR;
1404 * CCP is down; free (de)compressor state if necessary.
1408 struct ppp_softc *sc;
1410 if (sc->sc_xc_state) {
1411 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1412 sc->sc_xc_state = NULL;
1414 if (sc->sc_rc_state) {
1415 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1416 sc->sc_rc_state = NULL;
1419 #endif /* PPP_COMPRESS */
1422 * PPP packet input routine.
1423 * The caller has checked and removed the FCS and has inserted
1424 * the address/control bytes and the protocol high byte if they
1428 ppppktin(sc, m, lost)
1429 struct ppp_softc *sc;
1433 int error, s = splimp();
1435 NB_SET_MARK(m,(lost ? M_ERRMARK : 0));
1437 /* XXX - we should check for the raw queue overflowing... */
1438 nbq_enqueue(&sc->sc_rawq, m);
1439 if (!sc->sc_decompsched)
1441 if ((error = pppsched(pppintr_decomp, sc)) == KERN_SUCCESS)
1442 sc->sc_decompsched = 1;
1444 IOLogDbg("ppp%d: decompression callout failed returning %d\n",
1445 if_unit(sc->sc_if), error);
1452 * Process a received PPP packet, doing decompression as necessary.
1454 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1455 TYPE_UNCOMPRESSED_TCP)
1459 struct ppp_softc *sc;
1462 struct nb_queue *inq;
1463 int s, ilen, xlen, proto, rv;
1465 u_char *cp, adrs, ctrl;
1471 m->pktinfo.third.tv_sec = time.tv_sec;
1472 m->pktinfo.third.tv_usec = time.tv_usec;
1475 incr_cnt(sc->sc_if, if_ipackets);
1477 NB_GET_MARK(m,&flags);
1479 if (sc->sc_flags & SC_LOG_INPKT) {
1480 IOLog("ppp%d: got %d bytes\n", if_unit(sc->sc_if), NB_SIZE(m));
1484 cp = mtod(m, u_char *);
1485 adrs = PPP_ADDRESS(cp);
1486 ctrl = PPP_CONTROL(cp);
1487 proto = PPP_PROTOCOL(cp);
1489 if (flags & M_ERRMARK) {
1491 sc->sc_flags |= SC_VJ_RESET;
1497 * Decompress this packet if necessary, update the receiver's
1498 * dictionary, or take appropriate action on a CCP packet.
1500 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1501 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1503 m->pktinfo.flags |= NBFLAG_CCP;
1505 /* decompress this packet */
1506 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1507 if (rv == DECOMP_OK){
1513 NB_DUPLICATE(m, dmp);
1514 /* bcopy(&(m->pktinfo), &(dmp->pktinfo), sizeof(bpf_encapsulater)); */
1520 /* No error, but no decompressed packet returned */
1524 cp = mtod(m, u_char *);
1525 proto = PPP_PROTOCOL(cp);
1528 * An error has occurred in decompression.
1529 * Pass the compressed packet up to pppd, which may take
1530 * CCP down or issue a Reset-Req.
1532 IOLogDbg("ppp%d: decompress failed %d\n", if_unit(sc->sc_if), rv);
1534 sc->sc_flags |= SC_VJ_RESET;
1536 if (rv == DECOMP_ERROR)
1537 sc->sc_flags |= SC_DC_ERROR;
1539 sc->sc_flags |= SC_DC_FERROR;
1544 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN))
1548 m->pktinfo.flags |= NBFLAG_CCPINC;
1550 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1552 if (proto == PPP_CCP) {
1561 m->pktinfo.size2 = ilen;
1565 if (sc->sc_flags & SC_VJ_RESET) {
1567 * If we've missed a packet, we must toss subsequent compressed
1568 * packets which don't have an explicit connection ID.
1571 /* IOLog("SC_VJ_RESET was set!\n"); */
1573 vj_uncompress_err(&sc->sc_comp);
1575 sc->sc_flags &= ~SC_VJ_RESET;
1580 * See if we have a VJ-compressed packet to uncompress.
1582 if (proto == PPP_VJC_COMP) {
1583 if (sc->sc_flags & SC_REJ_COMP_TCP)
1587 m->pktinfo.flags |= NBFLAG_VJC;
1590 xlen = vj_uncompress_tcp(cp + PPP_HDRLEN, ilen - PPP_HDRLEN,
1592 &sc->sc_comp, &iphdr, &hlen);
1596 IOLogDbg("ppp%d: VJ uncompress failed on type comp\n",
1597 if_unit(sc->sc_if));
1603 * Write the IP/TCP header back into the datagram.
1604 * The pointers point to the stored copy in the VJ
1605 * compression table.
1608 NB_GROW_TOP(m, hlen - xlen);
1609 NB_WRITE(m, PPP_HDRLEN, hlen, iphdr);
1611 cp = mtod(m, u_char *);
1615 #define getip_hl(base) ((base).ip_hl)
1617 u_short mytcpcksum (struct ip *pip);
1618 struct tcphdr *ptcp;
1623 iphdr = (struct ip*) (cp + PPP_HDRLEN);
1624 hlen = getip_hl(*iphdr) << 2; /* Length is in words */
1625 ptcp = (struct tcphdr *)&((u_char *)iphdr)[hlen];
1627 thecksum = (u_short)mytcpcksum(iphdr);
1629 if(ptcp->th_sum != thecksum)
1632 set_newvj_error_mode();
1634 IOLog("NEWVJ: Warning... TCP checksum failed Received=%u, Calculated=%u)\n",
1635 (ptcp->th_sum)&0xffff, thecksum&0xffff);
1647 ilen += hlen - xlen;
1649 } else if (proto == PPP_VJC_UNCOMP) {
1650 if (sc->sc_flags & SC_REJ_COMP_TCP)
1654 m->pktinfo.flags |= NBFLAG_VJCINC;
1657 vj_uncompress_uncomp(cp + PPP_HDRLEN, ilen-PPP_HDRLEN, &sc->sc_comp);
1665 m->pktinfo.size3 = NB_SIZE(m);
1666 m->pktinfo.fourth.tv_sec = time.tv_sec;
1667 m->pktinfo.fourth.tv_usec = time.tv_usec;
1672 * See if bpf wants to look at the packet.
1673 * Size includes the PPP header by not the trailer.
1676 #ifndef NETBUF_PROXY
1677 bpf_tap(sc->sc_bpf, NB_MAP(m), NB_SIZE(m));
1679 bpf_tap(sc->sc_bpf, m, 0);
1688 * IP packet - take off the ppp header and pass it up to IP.
1690 if ((if_flags(sc->sc_if) & IFF_UP) == 0
1691 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1692 /* interface is down - drop the packet. */
1694 IOLogDbg("ppp%d: IP packed dropped (NPmode)\n", if_unit(sc->sc_if));
1697 NB_SHRINK_TOP(m, PPP_HDRLEN);
1698 inet_queue(sc->sc_if, NB_TO_nb(m));
1699 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1705 * Some other protocol - place on input queue for read().
1713 * Put the packet on the appropriate input queue.
1716 if (nbq_full(inq)) {
1719 IOLog("ppp%d: input queue full\n", if_unit(sc->sc_if));
1722 nbq_enqueue(inq, m);
1732 incr_cnt(sc->sc_if, if_ierrors);
1735 #define MAX_DUMP_BYTES 128
1741 char buf[3*MAX_DUMP_BYTES+4];
1743 static char digits[] = "0123456789abcdef";
1744 int l = NB_SIZE(m0);
1745 u_char *rptr = mtod(m0, u_char *);
1748 if (bp > buf + sizeof(buf) - 4)
1750 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1751 *bp++ = digits[*rptr++ & 0xf];