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.12 $ ($Date: 1999/12/23 01:48:44 $)";
88 #define KERNEL_FEATURES 1
93 #include <kernserv/clock_timer.h>
94 #include <kernserv/lock.h>
96 #include <kern/lock.h>
97 #endif /* NS_TARGET */
98 #endif /* NS_TARGET */
100 #include <sys/param.h>
102 typedef simple_lock_data_t lock_data_t; /* XXX */
103 #endif /* NS_TARGET */
104 #include <sys/proc.h>
106 #include <sys/socket.h>
107 #include <sys/conf.h>
108 #include <sys/time.h>
109 #include <sys/ioctl.h>
110 #include <sys/errno.h>
111 #if !(NS_TARGET >= 40)
112 #include <kernserv/prototypes.h>
117 #include <driverkit/generalFuncs.h>
118 #import <kernserv/machine/spl.h>
120 #if defined(sparc) || defined(m68k)
121 #include <machine/psl.h>
123 #include <kernserv/kern_server_types.h>
126 #include <net/route.h>
129 #include <netinet/in.h>
130 #include <netinet/in_systm.h>
131 #include <netinet/in_var.h>
132 #include <netinet/ip.h>
133 #include <netinet/tcp.h>
136 #include <net/ppp_defs.h>
138 #include <net/vjcompress.h>
140 #include <net/if_ppp.h>
141 #include "NeXT_Version.h"
142 #include "if_pppvar.h"
145 struct ppp_softc ppp_softc[NUM_PPP];
149 #define PACKETPTR NETBUF_T
150 #include <net/ppp-comp.h>
154 * The max number of NETBUF_Ts we wish to compress and cache for
157 #define COMPRESS_CACHE_LEN 1
162 * Necessary to avoid redefinition warnings or bogus type casts later.
164 int pppoutput __P((netif_t ifp, netbuf_t m, void *arg));
165 int pppsioctl __P((netif_t ifp, int cmd, caddr_t data));
166 int pppcontrol __P((netif_t ifp, const char *cmd, void *data));
167 void pppintr_comp __P((void *arg));
168 void pppintr_decomp __P((void *arg));
169 void pppfillfreeq __P((void *arg));
170 void pppgetm __P((register struct ppp_softc *sc));
172 static void ppp_requeue __P((struct ppp_softc *));
173 static void ppp_outpkt __P((struct ppp_softc *));
174 static void ppp_ccp __P((struct ppp_softc *, NETBUF_T, int rcvd));
175 static void ppp_ccp_closed __P((struct ppp_softc *));
176 static void ppp_inproc __P((struct ppp_softc *, NETBUF_T));
177 static void pppdumpm __P((NETBUF_T));
179 extern int install_ppp_ld __P((void));
180 extern int tty_ld_remove __P((int));
183 * We steal two bits in the mbuf m_flags, to mark high-priority packets
184 * for output, and received packets following lost/corrupted packets.
186 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
187 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
190 * The following disgusting hack gets around the problem that IP TOS
191 * can't be set yet. We want to put "interactive" traffic on a high
192 * priority queue. To decide if traffic is interactive, we check that
193 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
195 static u_short interactive_ports[8] = {
200 enum { QFREE, QRAW, QFAST, QSLOW, QIN, QNP, QCACHE };
202 static struct qparms qparms[] = {
203 {20, 40, 50, "free"}, /* freeq */
204 {5, 20, 25, "raw"}, /* rawq */
205 {5, 20, 25, "fast"}, /* fastq */
206 {5, 20, 25, "slow"}, /* slowq */
207 {5, 20, 25, "in"}, /* inq */
208 {5, 20, 25, "np"}, /* npq */
209 {0, COMPRESS_CACHE_LEN, COMPRESS_CACHE_LEN, "cache"} /* cache */
212 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
214 #ifndef IPTOS_LOWDELAY
215 #define IPTOS_LOWDELAY 0x10
220 * List of compressors we know about.
221 * We leave some space so maybe we can modload compressors.
224 extern struct compressor ppp_bsd_compress;
226 struct compressor *ppp_compressors[8] = {
232 #endif /* PPP_COMPRESS */
234 /* yeah, we sometimes have to change the MTU after having created the
235 * device. Let's hope this doesn't break anything!!!
237 #define if_mtu_set(ifn,mtu) (((struct ifnet *)ifn)->if_mtu = mtu)
239 extern int ipforwarding;
240 extern int ipsendredirects;
242 kern_server_t instance;
245 * Sigh. Should be defined in <net/if.h> but isn't...
250 u_long ifru_asyncmap;
257 * Returns a new "outgoing" netbuf.
259 * Must return an actual netbuf_t since other protocols
260 * use this to get our buffers. Before releasing, save
261 * any space we may need when the buffer returns.
265 pppgetbuf(netif_t ifp)
269 int len = MAX(if_mtu(ifp), PPP_MTU) + PPP_HDRLEN + PPP_FCSLEN;
273 NB_SHRINK_TOP(nb, PPP_HDRLEN);
274 NB_SHRINK_BOT(nb, PPP_FCSLEN); /* grown by pppstart() */
280 * Called from boot code to establish ppp interfaces.
285 register struct ppp_softc *sc;
288 IOLog("\nPPP version 2.3.11-%s for NeXTSTEP and OPENSTEP\n", PPPVERSION);
289 IOLog("by Stephen Perkins, Philip Prindeville, and Pete French\n");
290 if (install_ppp_ld() < 0) {
291 IOLog("ppp: Could not install line discipline\n");
294 for (sc = ppp_softc; i < NUM_PPP; sc++, i++) {
295 sc->sc_if = if_attach(NULL, NULL, pppoutput,
296 pppgetbuf, pppcontrol, "ppp", i, "Serial line PPP",
297 PPP_MTU, IFF_POINTOPOINT, NETIFCLASS_VIRTUAL, (void *) sc);
298 nbq_init(&sc->sc_freeq, &qparms[QFREE]);
299 nbq_init(&sc->sc_rawq, &qparms[QRAW]);
300 nbq_init(&sc->sc_fastq, &qparms[QFAST]);
301 nbq_init(&sc->sc_slowq, &qparms[QSLOW]);
302 nbq_init(&sc->sc_inq, &qparms[QIN]);
303 nbq_init(&sc->sc_npq, &qparms[QNP]);
304 nbq_init(&sc->sc_compq, &qparms[QCACHE]);
305 IOLog(" ppp%d successfully attached.\n", i);
311 IOLog("PPP Successfully Installed.\n\n");
317 struct ppp_softc *sc;
320 IOLog("Removing PPP on Line Discipline %d\n", PPPDISC);
321 if (!tty_ld_remove(PPPDISC))
322 IOLog("ppp: Could not remove line discipline\n");
324 IOLog("Removing interfaces:\n");
325 for (sc = ppp_softc, i = 0; i < NUM_PPP; sc++, i++) {
326 nbq_free(&sc->sc_freeq);
327 nbq_free(&sc->sc_rawq);
328 nbq_free(&sc->sc_fastq);
329 nbq_free(&sc->sc_slowq);
330 nbq_free(&sc->sc_inq);
331 nbq_free(&sc->sc_npq);
332 nbq_free(&sc->sc_compq);
333 if_detach(sc->sc_if);
334 /* no idea why we need this, but... */
335 bzero(sc->sc_if, sizeof(netif_t));
336 IOLog(" ppp%d successfully detached.\n", i);
338 IOLog("PPP-2.3 Successfully Removed.\n\n");
343 * Allocate a ppp interface unit and initialize it.
350 struct ppp_softc *sc;
352 struct timeval tv_time;
353 #endif /* NS_TARGET */
355 for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)
356 if (sc->sc_xfer == pid) {
357 IOLogDbg("ppp%d: alloc'ing unit %d to proc %d\n", nppp, nppp, pid);
361 for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)
362 if (sc->sc_devp == NULL)
368 sc->sc_mru = PPP_MRU;
369 sc->sc_relinq = NULL;
371 vj_compress_init(&sc->sc_comp, -1);
374 sc->sc_xc_state = NULL;
375 sc->sc_rc_state = NULL;
376 #endif /* PPP_COMPRESS */
377 for (i = 0; i < NUM_NP; ++i)
378 sc->sc_npmode[i] = NPMODE_ERROR;
379 /* XXX - I'm not sure why the npqueue was zapped here... */
382 ns_time_to_timeval(clock_value(System), &tv_time);
383 sc->sc_last_sent = sc->sc_last_recv = tv_time.tv_sec;
385 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
388 sc->sc_compsched = 0;
389 sc->sc_decompsched = 0;
392 * XXX -- We need to get packets here, and we don't care if we do block...
393 * We do this after we set the sc_mru.
395 pppfillfreeq((void *) sc);
401 * Deallocate a ppp unit. Must be called at splnet or higher.
405 struct ppp_softc *sc;
408 if_flags_set(sc->sc_if, if_flags(sc->sc_if) & ~(IFF_UP|IFF_RUNNING));
411 nbq_flush(&sc->sc_freeq);
412 nbq_flush(&sc->sc_rawq);
413 nbq_flush(&sc->sc_inq);
414 nbq_flush(&sc->sc_fastq);
415 nbq_flush(&sc->sc_slowq);
416 nbq_flush(&sc->sc_npq);
417 nbq_flush(&sc->sc_compq);
420 sc->sc_xc_state = NULL;
421 sc->sc_rc_state = NULL;
422 #endif /* PPP_COMPRESS */
428 * Ioctl routine for generic ppp devices.
431 pppioctl(sc, cmd, data, flag)
432 struct ppp_softc *sc;
437 struct proc *p = curproc;
438 int s, error, flags, mru, nb, npx, oldflags;
439 struct ppp_option_data *odp;
440 struct compressor **cp;
444 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
447 #ifdef HAS_BROKEN_TIOCSPGRP
448 struct tty *tp = sc->sc_devp;
451 struct timeval tv_time;
452 #endif /* NS_TARGET */
457 s = splimp(); /* paranoid; splnet probably ok */
458 if ((m = nbq_peek(&sc->sc_inq)) != NULL)
459 *(int *)data = NB_SIZE(m);
466 *(int *)data = if_unit(sc->sc_if);
470 *(u_int *)data = sc->sc_flags;
476 flags = *(int *)data & SC_MASK;
479 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
483 oldflags = sc->sc_flags;
484 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
493 IOLogDbg("ppp%d: setting mru %d\n", if_unit(sc->sc_if), mru);
495 if (mru >= PPP_MRU && mru <= PPP_MAXMRU) {
497 /* To make sure we handle the received packet
498 * correctly, we do two things. First, we
499 * empty out the free_q. We then remove
500 * the current input buffer, set the input length
501 * to zero, and set the flush flag.
504 nbq_flush(&sc->sc_freeq); /* get rid of old buffers */
509 if (sc->sc_ilen != 0)
510 sc->sc_flags |= SC_FLUSH;
514 pppfillfreeq((void *) sc); /* and make a queue of new ones */
520 *(int *)data = sc->sc_mru;
528 vj_compress_init(&sc->sc_comp, *(int *)data);
536 sc->sc_xfer = p->p_pid;
540 case PPPIOCSCOMPRESS:
543 odp = (struct ppp_option_data *) data;
545 if (nb > sizeof(ccp_option))
546 nb = sizeof(ccp_option);
547 if (error = copyin(odp->ptr, ccp_option, nb))
549 if (ccp_option[1] < 2) /* preliminary check on the length byte */
551 for (cp = ppp_compressors; *cp != NULL; ++cp)
552 if ((*cp)->compress_proto == ccp_option[0]) {
554 * Found a handler for the protocol - try to allocate
555 * a compressor or decompressor.
560 if (sc->sc_xc_state != NULL)
561 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
562 sc->sc_xcomp = *cp; /* entry points for compressor */
563 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
564 if (sc->sc_xc_state == NULL) {
565 IOLogDbg("ppp%d: comp_alloc failed", if_unit(sc->sc_if));
569 sc->sc_flags &= ~SC_COMP_RUN;
571 if (sc->sc_rc_state != NULL)
572 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
573 sc->sc_rcomp = *cp; /* entry points for compressor */
574 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
575 if (sc->sc_rc_state == NULL) {
576 IOLogDbg("ppp%d: decomp_alloc failed", if_unit(sc->sc_if));
580 sc->sc_flags &= ~SC_DECOMP_RUN;
585 IOLogDbg("ppp%d: no compressor for [%x %x %x], %x", if_unit(sc->sc_if),
586 ccp_option[0], ccp_option[1], ccp_option[2], nb);
587 return (EINVAL); /* no handler found */
588 #endif /* PPP_COMPRESS */
590 #ifdef HAS_BROKEN_TIOCSPGRP
592 tp->t_pgrp = *(int *)data;
598 npi = (struct npioctl *) data;
599 switch (npi->protocol) {
606 if (cmd == PPPIOCGNPMODE) {
607 npi->mode = sc->sc_npmode[npx];
611 if (npi->mode != sc->sc_npmode[npx]) {
613 sc->sc_npmode[npx] = npi->mode;
614 if (npi->mode != NPMODE_QUEUE) {
626 ns_time_to_timeval(clock_value(System), &tv_time);
630 #endif /* NS_TARGET */
631 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
632 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
643 pppcontrol(ifp, cmd, data)
649 if (!strcmp(cmd, IFCONTROL_UNIXIOCTL)) {
650 if_ioctl_t* ctl = (if_ioctl_t*)data;
651 return pppsioctl(ifp,
654 } else if (!strcmp(cmd, IFCONTROL_SETADDR)) {
655 struct sockaddr_in *sin = (struct sockaddr_in *)data;
656 if (sin->sin_family != AF_INET)
658 if_flags_set(ifp, if_flags(ifp) | IFF_UP);
662 * We implement this to allow iftab
663 * to contain -AUTOMATIC- entries
664 * without generating errors at boot time.
665 * We do not, however, mark it as UP.
667 else if (!strcmp(cmd, IFCONTROL_AUTOADDR)) {
668 struct sockaddr_in *sin = (struct sockaddr_in *) data;
669 if (sin->sin_family != AF_INET)
672 } else if (!strcmp(cmd, IFCONTROL_SETFLAGS)) {
673 register union ifr_ifru *ifr = (union ifr_ifru *)data;
676 if_flags_set(ifp, ifr->ifru_flags);
680 * Under 3.2 developer, I don't know the symbol for this
681 * new 3.3 command. So it is a constant for now. I don't
682 * believe I need to do anything to support this at the moment.
684 else if (strcmp(cmd, "add-multicast") == 0) {
685 struct sockaddr_in *sin = (struct sockaddr_in *) data;
686 if (sin->sin_family != AF_INET)
689 IOLog("ppp%d: Invalid ppp control %s\n", if_unit(ifp), cmd);
695 * Process an ioctl request to the ppp network interface.
698 pppsioctl(ifp, cmd, data)
699 register netif_t ifp;
703 register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
704 register struct ifaddr *ifa = (struct ifaddr *)data;
705 register struct ifreq *ifr = (struct ifreq *)data;
706 struct ppp_stats *psp;
708 struct ppp_comp_stats *pcp;
710 int s = splimp(), error = 0;
714 IOLog("ppp%d: pppioctl: SIOCSIFFLAGS called!\n", if_unit(ifp));
718 if (ifa->ifa_addr.sa_family != AF_INET)
719 error = EAFNOSUPPORT;
723 if (ifa->ifa_addr.sa_family != AF_INET)
724 error = EAFNOSUPPORT;
732 if_mtu_set(sc->sc_if, ifr->ifr_mtu);
733 nbq_flush(&sc->sc_freeq); /* get rid of old buffers */
734 pppsched(pppfillfreeq, sc); /* and make a queue of new ones */
739 ifr->ifr_mtu = if_mtu(sc->sc_if);
743 psp = &((struct ifpppstatsreq *) data)->stats;
744 bzero(psp, sizeof(*psp));
745 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
746 psp->p.ppp_ipackets = if_ipackets(sc->sc_if);
747 psp->p.ppp_ierrors = if_ierrors(sc->sc_if);
748 psp->p.ppp_obytes = sc->sc_bytessent;
749 psp->p.ppp_opackets = if_opackets(sc->sc_if);
750 psp->p.ppp_oerrors = if_oerrors(sc->sc_if);
752 psp->vj.vjs_packets = sc->sc_comp.stats.vjs_packets;
753 psp->vj.vjs_compressed = sc->sc_comp.stats.vjs_compressed;
754 psp->vj.vjs_searches = sc->sc_comp.stats.vjs_searches;
755 psp->vj.vjs_misses = sc->sc_comp.stats.vjs_misses;
756 psp->vj.vjs_uncompressedin = sc->sc_comp.stats.vjs_uncompressedin;
757 psp->vj.vjs_compressedin = sc->sc_comp.stats.vjs_compressedin;
758 psp->vj.vjs_errorin = sc->sc_comp.stats.vjs_errorin;
759 psp->vj.vjs_tossed = sc->sc_comp.stats.vjs_tossed;
765 pcp = &((struct ifpppcstatsreq *) data)->stats;
766 bzero(pcp, sizeof(*pcp));
767 if (sc->sc_xc_state != NULL)
768 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
769 if (sc->sc_rc_state != NULL)
770 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
772 #endif /* PPP_COMPRESS */
782 * Queue a packet. Start transmission if not active.
783 * Packet is placed in Information field of PPP frame.
785 * This procedure MUST take an actual netbuf_t as input
786 * since it may be called by procedures outside of us.
787 * The buffer received must be in the same format as that
788 * returned by pppgetbuf().
791 pppoutput(ifp, in_nb, arg)
796 register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
797 struct sockaddr *dst = (struct sockaddr *) arg;
798 int protocol, address, control;
803 struct nb_queue *ifq;
807 m0 = nb_TO_NB(in_nb);
809 if (sc->sc_devp == NULL || (if_flags(ifp) & IFF_RUNNING) == 0
810 || (if_flags(ifp) & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
811 error = ENETDOWN; /* sort of */
817 * Compute PPP header.
820 switch (dst->sa_family) {
823 address = PPP_ALLSTATIONS;
826 mode = sc->sc_npmode[NP_IP];
829 * If this packet has the "low delay" bit set in the IP header,
830 * or TCP and to an interactive port, put it on the fastq instead
832 ip = mtod(m0, struct ip *);
833 if (ip->ip_tos & IPTOS_LOWDELAY || ip->ip_p == IPPROTO_ICMP)
835 else if (ip->ip_p == IPPROTO_TCP) {
836 register u_short *p = (u_short *) &(((caddr_t) ip)[ip->ip_hl << 2]);
837 if (INTERACTIVE(ntohs(p[0])) || INTERACTIVE(ntohs(p[1])))
838 urgent: flags |= M_HIGHPRI;
844 address = PPP_ALLSTATIONS;
851 address = PPP_ADDRESS(dst->sa_data);
852 control = PPP_CONTROL(dst->sa_data);
853 protocol = PPP_PROTOCOL(dst->sa_data);
857 IOLog("ppp%d: af%d not supported\n", if_unit(ifp), dst->sa_family);
858 error = EAFNOSUPPORT;
863 * Drop this packet, or return an error, if necessary.
865 if (mode == NPMODE_ERROR) {
869 if (mode == NPMODE_DROP) {
877 NB_GROW_TOP(m0, PPP_HDRLEN);
879 cp = mtod(m0, u_char *);
882 *cp++ = protocol >> 8;
883 *cp++ = protocol & 0xff;
886 if (sc->sc_flags & SC_LOG_OUTPKT) {
887 IOLog("ppp%d: output:\n", if_unit(ifp)); /* XXX */
893 * Put the packet on the appropriate queue.
895 s = splimp(); /* splnet should be OK now */
896 if (mode == NPMODE_QUEUE) {
897 NB_SET_MARK(m0,flags); /* save priority */
898 /* XXX we should limit the number of packets on this queue */
899 nbq_enqueue(&sc->sc_npq, m0); /* XXX is this correct? */
901 ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq;
902 if (nbq_full(ifq) < 0) {
904 IOLog("ppp%d: output queue full\n", if_unit(sc->sc_if));
906 incr_cnt(sc->sc_if, if_oerrors);
910 nbq_enqueue(ifq, m0);
914 * If we don't have some compressed packets already
915 * and we are not at interrupt priority, then do some compression.
917 * We need to be especially careful here. pppouput() is typically
918 * called at 2 different priority levels. On a NeXT, neither of these
919 * is the interrupt priority level. However, on Intel, one of them is.
920 * I don't know about HPPA or Sparc. Simple fix is to just check.
923 if(!sc->sc_compsched && s != ipltospl(IPLIMP)) {
924 sc->sc_compsched = 1;
926 pppintr_comp(sc); /* Calls pppstart() */
941 * After a change in the NPmode for some NP, move packets from the
942 * npqueue to the send queue or the fast queue as appropriate.
943 * Should be called at splimp (actually splnet would probably suffice).
944 * Due to some of the uglies in the packet queueing system I have
945 * implemented this without the mpp stuff.
951 struct ppp_softc *sc;
954 struct nb_queue *ifq;
959 for (m = sc->sc_npq.head; m; ) {
962 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
964 mode = sc->sc_npmode[NP_IP];
973 * This packet can now go on one of the queues to be sent.
978 sc->sc_npq.head = nm;
980 NB_GET_MARK(m,&flags);
981 ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq;
984 incr_cnt(sc->sc_if, if_oerrors);
1003 sc->sc_npq.tail = lm; /* anything further on has been sent ! */
1007 * Get a packet to send. This procedure is intended to be called
1008 * at spltty()/splimp(), so it takes little time. If there isn't
1009 * a packet waiting to go out, it schedules a software interrupt
1010 * to prepare a new packet; the device start routine gets called
1011 * again when a packet is ready.
1015 struct ppp_softc *sc;
1020 m = nbq_dequeue(&sc->sc_compq);
1023 if (!sc->sc_compsched &&
1024 (! nbq_empty(&sc->sc_slowq) || ! nbq_empty(&sc->sc_fastq)))
1027 if ((error = pppsched(pppintr_comp, sc)) == KERN_SUCCESS)
1028 sc->sc_compsched = 1;
1031 IOLogDbg("ppp%d: compression callout failed returning %d\n",
1032 if_unit(sc->sc_if), error);
1040 * Takes all received input packets and uncompresses/hands_off.
1041 * Must not be reentrant and is called at normal priority.
1042 * Guaranteed Non-Reentrancy means we don't need to be at splnet().
1050 struct ppp_softc *sc = (struct ppp_softc *)arg;
1054 if (nbq_low(&sc->sc_freeq))
1055 pppfillfreeq((void *) sc);
1059 m = nbq_dequeue(&sc->sc_rawq);
1066 * Now we have aparently emptied the queue. So, we try to reset the
1067 * synchronization flag that schedules callbacks. We check for the
1068 * possibility that an interrupt occurred before we finish this check.
1071 if (!nbq_empty(&sc->sc_rawq))
1078 sc->sc_decompsched = 0;
1086 * Readies the next few output packet from
1087 * the sc_fastq/sc_slowq. Will try to
1088 * precompress all packets on the fast
1089 * queue and at most one from the slow queue.
1095 struct ppp_softc *sc = (struct ppp_softc *)arg;
1099 if (nbq_low(&sc->sc_freeq))
1100 pppfillfreeq((void *) sc);
1103 while (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_fastq))
1106 if (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_slowq))
1109 sc->sc_compsched = 0;
1113 * Grab another packet off a queue and apply VJ compression,
1114 * packet compression, address/control and/or protocol compression
1115 * if enabled. Should be called at splnet.
1119 struct ppp_softc *sc;
1124 int address, control, protocol;
1126 struct timeval tv_time;
1130 * Grab a packet to send: first try the fast queue, then the
1133 m = nbq_dequeue(&sc->sc_fastq);
1135 m = nbq_dequeue(&sc->sc_slowq);
1140 * Extract the ppp header of the new packet.
1141 * The ppp header will be in one netbuf.
1143 cp = mtod(m, u_char *);
1144 address = PPP_ADDRESS(cp);
1145 control = PPP_CONTROL(cp);
1146 protocol = PPP_PROTOCOL(cp);
1149 ns_time_to_timeval(clock_value(System), &tv_time);
1150 #endif /* NS_TARGET */
1155 * Update the time we sent the most recent packet.
1158 sc->sc_last_sent = tv_time.tv_sec;
1160 sc->sc_last_sent = time.tv_sec;
1161 #endif /* NS_TARGET */
1165 * If the packet is a TCP/IP packet, see if we can compress it.
1167 if (sc->sc_flags & SC_COMP_TCP) {
1172 ip = (struct ip *) (cp + PPP_HDRLEN);
1173 /* this code assumes the IP/TCP header is in one netbuf */
1174 if (ip->ip_p == IPPROTO_TCP) {
1175 type = vj_compress_tcp(ip, NB_SIZE(m) - PPP_HDRLEN,
1177 !(sc->sc_flags & SC_NO_TCP_CCID), &vjhdr);
1179 case TYPE_UNCOMPRESSED_TCP:
1180 protocol = PPP_VJC_UNCOMP;
1182 case TYPE_COMPRESSED_TCP:
1183 NB_SHRINK_TOP(m, vjhdr - (u_char *) ip);
1184 protocol = PPP_VJC_COMP;
1185 cp = mtod(m, u_char *);
1186 cp[0] = address; /* header has moved */
1191 cp[3] = protocol; /* update protocol in PPP header */
1203 #endif /* PPP_COMPRESS */
1208 if (protocol != PPP_LCP && protocol != PPP_CCP
1209 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1215 clen = (*sc->sc_xcomp->compress)
1216 (sc->sc_xc_state, &mcomp, m, slen,
1217 sc->sc_flags & SC_CCP_UP? if_mtu(sc->sc_if): 0);
1220 if (mcomp && (NB_SIZE(mcomp) >= slen))
1221 IOLog("BSD Warning... packet growth: Orig=%d New=%d.\n",
1222 slen, NB_SIZE(mcomp));
1224 if (mcomp != NULL) {
1228 cp = mtod(m, u_char *);
1232 #endif /* PPP_COMPRESS */
1235 * Compress the address/control and protocol, if possible.
1237 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1238 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1239 protocol != PPP_LCP) {
1240 /* can compress address/control */
1241 NB_SHRINK_TOP(m, 2);
1243 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1244 /* can compress protocol */
1245 if (mtod(m, u_char *) == cp) {
1246 cp[2] = cp[1]; /* move address/control up */
1249 NB_SHRINK_TOP(m, 1);
1254 nbq_enqueue(&sc->sc_compq, m);
1255 (*sc->sc_start)(sc);
1261 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1262 * 0 if it is about to be transmitted.
1265 ppp_ccp(sc, m, rcvd)
1266 struct ppp_softc *sc;
1274 * Get a pointer to the data after the PPP header.
1276 dp = mtod(m, u_char *) + PPP_HDRLEN;
1278 ep = mtod(m, u_char *) + NB_SIZE(m);
1279 if (dp + CCP_HDRLEN > ep)
1281 slen = CCP_LENGTH(dp);
1282 if (dp + slen > ep) {
1283 IOLogDbg("ppp%d: ccp: not enough data in netbuf (%x+%x > %x+%x)\n",
1284 if_unit(sc->sc_if), dp, slen, mtod(m, u_char *), NB_SIZE(m));
1288 switch (CCP_CODE(dp)) {
1292 /* CCP must be going down - disable compression */
1293 if (sc->sc_flags & SC_CCP_UP) {
1295 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1301 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1302 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1303 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1305 /* we're agreeing to send compressed packets. */
1306 if (sc->sc_xc_state != NULL
1307 && (*sc->sc_xcomp->comp_init)
1308 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1309 if_unit(sc->sc_if), 0, sc->sc_flags & SC_DEBUG)) {
1311 sc->sc_flags |= SC_COMP_RUN;
1315 /* peer is agreeing to send compressed packets. */
1316 if (sc->sc_rc_state != NULL
1317 && (*sc->sc_rcomp->decomp_init)
1318 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1323 0, sc->sc_mru, sc->sc_flags & SC_DEBUG)) {
1325 sc->sc_flags |= SC_DECOMP_RUN;
1326 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1334 if (sc->sc_flags & SC_CCP_UP) {
1336 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1337 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1338 nbq_flush(&sc->sc_compq); /* Flush pre-compressed packets */
1341 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1342 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1344 sc->sc_flags &= ~SC_DC_ERROR;
1354 * CCP is down; free (de)compressor state if necessary.
1358 struct ppp_softc *sc;
1360 if (sc->sc_xc_state) {
1361 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1362 sc->sc_xc_state = NULL;
1364 if (sc->sc_rc_state) {
1365 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1366 sc->sc_rc_state = NULL;
1369 #endif /* PPP_COMPRESS */
1372 * PPP packet input routine.
1373 * The caller has checked and removed the FCS and has inserted
1374 * the address/control bytes and the protocol high byte if they
1378 ppppktin(sc, m, lost)
1379 struct ppp_softc *sc;
1383 int error, s = splimp();
1385 NB_SET_MARK(m,(lost ? M_ERRMARK : 0));
1387 /* XXX - we should check for the raw queue overflowing... */
1388 nbq_enqueue(&sc->sc_rawq, m);
1389 if (!sc->sc_decompsched)
1391 if ((error = pppsched(pppintr_decomp, sc)) == KERN_SUCCESS)
1392 sc->sc_decompsched = 1;
1394 IOLogDbg("ppp%d: decompression callout failed returning %d\n",
1395 if_unit(sc->sc_if), error);
1402 * Process a received PPP packet, doing decompression as necessary.
1404 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1405 TYPE_UNCOMPRESSED_TCP)
1409 struct ppp_softc *sc;
1412 struct nb_queue *inq;
1413 int s, ilen, xlen, proto, rv;
1415 u_char *cp, adrs, ctrl;
1420 struct timeval tv_time;
1421 #endif /* NS_TARGET */
1424 incr_cnt(sc->sc_if, if_ipackets);
1426 NB_GET_MARK(m,&flags);
1428 if (sc->sc_flags & SC_LOG_INPKT) {
1429 IOLog("ppp%d: got %d bytes\n", if_unit(sc->sc_if), NB_SIZE(m));
1433 cp = mtod(m, u_char *);
1434 adrs = PPP_ADDRESS(cp);
1435 ctrl = PPP_CONTROL(cp);
1436 proto = PPP_PROTOCOL(cp);
1438 if (flags & M_ERRMARK) {
1440 sc->sc_flags |= SC_VJ_RESET;
1446 * Decompress this packet if necessary, update the receiver's
1447 * dictionary, or take appropriate action on a CCP packet.
1449 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1450 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1451 /* decompress this packet */
1452 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1453 if (rv == DECOMP_OK){
1457 /* No error, but no decompressed packet returned */
1461 cp = mtod(m, u_char *);
1462 proto = PPP_PROTOCOL(cp);
1465 * An error has occurred in decompression.
1466 * Pass the compressed packet up to pppd, which may take
1467 * CCP down or issue a Reset-Req.
1469 IOLogDbg("ppp%d: decompress failed %d\n", if_unit(sc->sc_if), rv);
1471 sc->sc_flags |= SC_VJ_RESET;
1473 if (rv == DECOMP_ERROR)
1474 sc->sc_flags |= SC_DC_ERROR;
1476 sc->sc_flags |= SC_DC_FERROR;
1481 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN))
1484 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1486 if (proto == PPP_CCP) {
1495 if (sc->sc_flags & SC_VJ_RESET) {
1497 * If we've missed a packet, we must toss subsequent compressed
1498 * packets which don't have an explicit connection ID.
1501 /* IOLog("SC_VJ_RESET was set!\n"); */
1503 vj_uncompress_err(&sc->sc_comp);
1505 sc->sc_flags &= ~SC_VJ_RESET;
1510 * See if we have a VJ-compressed packet to uncompress.
1512 if (proto == PPP_VJC_COMP) {
1513 if (sc->sc_flags & SC_REJ_COMP_TCP)
1517 xlen = vj_uncompress_tcp(cp + PPP_HDRLEN, ilen - PPP_HDRLEN,
1519 &sc->sc_comp, &iphdr, &hlen);
1523 IOLogDbg("ppp%d: VJ uncompress failed on type comp\n",
1524 if_unit(sc->sc_if));
1530 * Write the IP/TCP header back into the datagram.
1531 * The pointers point to the stored copy in the VJ
1532 * compression table.
1535 NB_GROW_TOP(m, hlen - xlen);
1536 NB_WRITE(m, PPP_HDRLEN, hlen, iphdr);
1538 cp = mtod(m, u_char *);
1542 #define getip_hl(base) ((base).ip_hl)
1544 u_short mytcpcksum (struct ip *pip);
1545 struct tcphdr *ptcp;
1550 iphdr = (struct ip*) (cp + PPP_HDRLEN);
1551 hlen = getip_hl(*iphdr) << 2; /* Length is in words */
1552 ptcp = (struct tcphdr *)&((u_char *)iphdr)[hlen];
1554 thecksum = (u_short)mytcpcksum(iphdr);
1556 if(ptcp->th_sum != thecksum)
1559 set_newvj_error_mode();
1561 IOLog("NEWVJ: Warning... TCP checksum failed Received=%u, Calculated=%u)\n",
1562 (ptcp->th_sum)&0xffff, thecksum&0xffff);
1574 ilen += hlen - xlen;
1576 } else if (proto == PPP_VJC_UNCOMP) {
1577 if (sc->sc_flags & SC_REJ_COMP_TCP)
1581 vj_uncompress_uncomp(cp + PPP_HDRLEN, ilen-PPP_HDRLEN, &sc->sc_comp);
1594 * IP packet - take off the ppp header and pass it up to IP.
1596 if ((if_flags(sc->sc_if) & IFF_UP) == 0
1597 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1598 /* interface is down - drop the packet. */
1600 IOLogDbg("ppp%d: IP packed dropped (NPmode)\n", if_unit(sc->sc_if));
1603 NB_SHRINK_TOP(m, PPP_HDRLEN);
1604 inet_queue(sc->sc_if, NB_TO_nb(m));
1606 /* I am assuming the time is different here than above. */
1607 ns_time_to_timeval(clock_value(System), &tv_time);
1608 sc->sc_last_recv = tv_time.tv_sec; /* update time of last pkt rcvd */
1610 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1617 * Some other protocol - place on input queue for read().
1625 * Put the packet on the appropriate input queue.
1628 if (nbq_full(inq)) {
1631 IOLog("ppp%d: input queue full\n", if_unit(sc->sc_if));
1634 nbq_enqueue(inq, m);
1644 incr_cnt(sc->sc_if, if_ierrors);
1647 #define MAX_DUMP_BYTES 128
1653 char buf[3*MAX_DUMP_BYTES+4];
1655 static char digits[] = "0123456789abcdef";
1656 int l = NB_SIZE(m0);
1657 u_char *rptr = mtod(m0, u_char *);
1660 if (bp > buf + sizeof(buf) - 4)
1662 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1663 *bp++ = digits[*rptr++ & 0xf];