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.1 $ ($Date: 1995/12/18 03:30:02 $)";
88 #define KERNEL_FEATURES 1
91 /* #include "ppp.h" */
93 #include <sys/param.h>
96 #include <sys/socket.h>
99 #include <sys/ioctl.h>
100 #include <sys/errno.h>
101 #include <kernserv/prototypes.h>
105 #include <driverkit/generalFuncs.h>
106 #import <kernserv/machine/spl.h>
108 #if defined(sparc) || defined(m68k)
109 #include <machine/psl.h>
111 #include <kernserv/kern_server_types.h>
114 #include <net/route.h>
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/in_var.h>
120 #include <netinet/ip.h>
124 #include <net/ppp_defs.h>
126 #include <net/vjcompress.h>
128 #include <net/if_ppp.h>
129 #include "if_pppvar.h"
131 struct ppp_softc ppp_softc[NUM_PPP];
134 #define PACKETPTR netbuf_t
135 #include <net/ppp-comp.h>
141 * We store the address of necessary BPF functions
144 struct bpf_fns fnarg;
148 * The max number of netbuf_ts we wish to compress and cache for
151 #define COMPRESS_CACHE_LEN 5
156 * Necessary to avoid redefinition warnings or bogus type casts later.
158 int pppoutput __P((netif_t ifp, netbuf_t m, void *arg));
159 int pppsioctl __P((netif_t ifp, int cmd, caddr_t data));
160 int pppcontrol __P((netif_t ifp, const char *cmd, void *data));
161 void pppintr_comp __P((void *arg));
162 void pppintr_decomp __P((void *arg));
163 void pppfillfreeq __P((void *arg));
164 void pppgetm __P((register struct ppp_softc *sc));
166 static void ppp_requeue __P((struct ppp_softc *));
167 static void ppp_outpkt __P((struct ppp_softc *));
168 static void ppp_ccp __P((struct ppp_softc *, netbuf_t, int rcvd));
169 static void ppp_ccp_closed __P((struct ppp_softc *));
170 static void ppp_inproc __P((struct ppp_softc *, netbuf_t));
171 static void pppdumpm __P((netbuf_t));
173 extern int install_ppp_ld __P((void));
174 extern int tty_ld_remove __P((int));
178 * We steal two bits in the mbuf m_flags, to mark high-priority packets
179 * for output, and received packets following lost/corrupted packets.
181 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
182 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
185 * The following disgusting hack gets around the problem that IP TOS
186 * can't be set yet. We want to put "interactive" traffic on a high
187 * priority queue. To decide if traffic is interactive, we check that
188 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
190 static u_short interactive_ports[8] = {
195 enum { QFREE, QRAW, QFAST, QSLOW, QIN, QNP, QCACHE };
197 static struct qparms qparms[] = {
198 {20, 40, 50, "free"}, /* freeq */
199 {5, 20, 25, "fast"}, /* rawq */
200 {5, 20, 25, "slow"}, /* fastq */
201 {5, 20, 25, "in"}, /* slowq */
202 {5, 20, 25, "out"}, /* inq */
203 {5, 20, 25, "np"}, /* npq */
204 {0, COMPRESS_CACHE_LEN, COMPRESS_CACHE_LEN, "cache"} /* cache */
207 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
209 #ifndef IPTOS_LOWDELAY
210 #define IPTOS_LOWDELAY 0x10
215 * List of compressors we know about.
216 * We leave some space so maybe we can modload compressors.
219 extern struct compressor ppp_bsd_compress;
221 struct compressor *ppp_compressors[8] = {
227 #endif /* PPP_COMPRESS */
229 /* yeah, we sometimes have to change the MTU after having created the
230 * device. Let's hope this doesn't break anything!!!
232 #define if_mtu_set(ifn,mtu) (((struct ifnet *)ifn)->if_mtu = mtu)
234 extern int ipforwarding;
235 extern int ipsendredirects;
237 kern_server_t instance;
240 * Sigh. Should be defined in <net/if.h> but isn't...
245 u_long ifru_asyncmap;
252 pppgetbuf(netif_t ifp)
256 int len = MAX(if_mtu(ifp), PPP_MTU) + PPP_HDRLEN + PPP_FCSLEN;
257 nb = ppp_nb_alloc(len);
260 ppp_nb_shrink_top(nb, PPP_HDRLEN);
261 nb_shrink_bot(nb, PPP_FCSLEN); /* grown by pppstart() */
267 * Called from boot code to establish ppp interfaces.
272 register struct ppp_softc *sc;
275 IOLog("\nPPP version 2.2 released for NS 3.2 and 3.3\n");
276 IOLog("LKS: %s\n", sccsid);
277 IOLog("by Stephen Perkins, Philip Prindeville, and Pete French\n");
279 IOLog("Installing PPP on Line Discipline %d\n", PPPDISC);
280 if (install_ppp_ld() < 0) {
281 IOLog("ppp: Could not install line discipline\n");
286 IOLog("Installing interfaces:\n");
287 for (sc = ppp_softc; i < NUM_PPP; sc++, i++) {
288 sc->sc_if = if_attach(NULL, NULL, pppoutput,
289 pppgetbuf, pppcontrol, "ppp", i, "Serial line PPP",
290 PPP_MTU, IFF_POINTOPOINT, NETIFCLASS_VIRTUAL, (void *) sc);
291 IOLog(" Initializing ppp%d\n", i);
292 nbq_init(&sc->sc_freeq, &qparms[QFREE]);
293 nbq_init(&sc->sc_rawq, &qparms[QRAW]);
294 nbq_init(&sc->sc_fastq, &qparms[QFAST]);
295 nbq_init(&sc->sc_slowq, &qparms[QSLOW]);
296 nbq_init(&sc->sc_inq, &qparms[QIN]);
297 nbq_init(&sc->sc_npq, &qparms[QNP]);
298 nbq_init(&sc->sc_compq, &qparms[QCACHE]);
299 IOLog(" ppp%d successfully attached.\n", i);
301 bpfattach((caddr_t *) &sc->sc_bpf, sc->sc_if, DLT_PPP, PPP_HDRLEN);
307 IOLog("PPP-2.2 Successfully Installed.\n\n");
314 struct ppp_softc *sc;
317 IOLog("Removing PPP on Line Discipline %d\n", PPPDISC);
318 if (!tty_ld_remove(PPPDISC))
319 IOLog("ppp: Could not remove line discipline\n");
321 IOLog("Removing interfaces:\n");
322 for (sc = ppp_softc, i = 0; i < NUM_PPP; sc++, i++) {
323 nbq_free(&sc->sc_freeq);
324 nbq_free(&sc->sc_rawq);
325 nbq_free(&sc->sc_fastq);
326 nbq_free(&sc->sc_slowq);
327 nbq_free(&sc->sc_inq);
328 nbq_free(&sc->sc_npq);
329 nbq_free(&sc->sc_compq);
330 if_detach(sc->sc_if);
331 /* no idea why we need this, but... */
332 bzero(sc->sc_if, sizeof(netif_t));
333 IOLog(" ppp%d successfully detached.\n", i);
335 IOLog("PPP-2.2 Successfully Removed.\n\n");
340 * Allocate a ppp interface unit and initialize it.
347 struct ppp_softc *sc;
349 for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)
350 if (sc->sc_xfer == pid) {
351 IOLogDbg("ppp%d: alloc'ing unit %d to proc %d\n", nppp, nppp, pid);
355 for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)
356 if (sc->sc_devp == NULL)
362 sc->sc_mru = PPP_MRU;
363 sc->sc_relinq = NULL;
365 vj_compress_init(&sc->sc_comp, -1);
368 sc->sc_xc_state = NULL;
369 sc->sc_rc_state = NULL;
370 #endif /* PPP_COMPRESS */
371 for (i = 0; i < NUM_NP; ++i)
372 sc->sc_npmode[i] = NPMODE_ERROR;
373 /* XXX - I'm not sure why the npqueue was zapped here... */
374 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
375 sc->sc_compsched = 0;
376 sc->sc_decompsched = 0;
380 * Clear PPP stats information for the new session
382 sc->sc_bytessent = 0;
383 sc->sc_bytesrcvd = 0;
384 if_opackets_set(sc->sc_if, 0);
385 if_ipackets_set(sc->sc_if, 0);
386 if_oerrors_set(sc->sc_if, 0);
387 if_ierrors_set(sc->sc_if, 0);
391 * XXX -- We need to get packets here, and we don't care if we do block...
392 * We do this after we set the sc_mru.
394 pppfillfreeq((void *) sc);
400 * Deallocate a ppp unit. Must be called at splnet or higher.
404 struct ppp_softc *sc;
407 if_flags_set(sc->sc_if, if_flags(sc->sc_if) & ~(IFF_UP|IFF_RUNNING));
410 nbq_flush(&sc->sc_freeq);
411 nbq_flush(&sc->sc_rawq);
412 nbq_flush(&sc->sc_inq);
413 nbq_flush(&sc->sc_fastq);
414 nbq_flush(&sc->sc_slowq);
415 nbq_flush(&sc->sc_npq);
416 nbq_flush(&sc->sc_compq);
419 sc->sc_xc_state = NULL;
420 sc->sc_rc_state = NULL;
421 #endif /* PPP_COMPRESS */
427 * Ioctl routine for generic ppp devices.
430 pppioctl(sc, cmd, data, flag)
431 struct ppp_softc *sc;
436 struct proc *p = curproc;
437 int s, error, flags, mru, nb, npx, oldflags;
438 struct ppp_option_data *odp;
439 struct compressor **cp;
443 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
446 #ifdef HAS_BROKEN_TIOCSPGRP
447 struct tty *tp = sc->sc_devp;
452 s = splimp(); /* paranoid; splnet probably ok */
453 if ((m = nbq_peek(&sc->sc_inq)) != NULL)
454 *(int *)data = nb_size(m);
461 *(int *)data = if_unit(sc->sc_if);
463 IOLogDbg("ppp%d: unit gets %d\n", if_unit(sc->sc_if), *(int *)data);
468 *(u_int *)data = sc->sc_flags;
470 IOLogDbg("ppp%d: flags gets 0x%x\n", if_unit(sc->sc_if), *(u_int *)data);
477 flags = *(int *)data & SC_MASK;
480 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
484 oldflags = sc->sc_flags;
485 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
488 IOLogDbg("ppp%d: data 0x%x, flags: old 0x%x new 0x%x\n",
489 if_unit(sc->sc_if), flags, oldflags, sc->sc_flags);
498 IOLogDbg("ppp%d: setting mru %d\n", if_unit(sc->sc_if), mru);
500 if (mru >= PPP_MRU && mru <= PPP_MAXMRU) {
502 /* To make sure we handle the received packet
503 * correctly, we do two things. First, we
504 * empty out the free_q. We then remove
505 * the current input buffer, set the input length
506 * to zero, and set the flush flag.
509 nbq_flush(&sc->sc_freeq); /* get rid of old buffers */
514 if (sc->sc_ilen != 0)
515 sc->sc_flags |= SC_FLUSH;
519 pppfillfreeq((void *) sc); /* and make a queue of new ones */
525 *(int *)data = sc->sc_mru;
527 IOLogDbg("ppp%d: mru gets 0x%x\n", if_unit(sc->sc_if), *(int *)data);
536 IOLogDbg("ppp%d: setting max cid %d\n", if_unit(sc->sc_if), *(int *)data);
539 vj_compress_init(&sc->sc_comp, *(int *)data);
547 sc->sc_xfer = p->p_pid;
549 IOLogDbg("ppp%d: setting pid %d\n", if_unit(sc->sc_if), sc->sc_xfer);
554 case PPPIOCSCOMPRESS:
557 odp = (struct ppp_option_data *) data;
559 if (nb > sizeof(ccp_option))
560 nb = sizeof(ccp_option);
561 if (error = copyin(odp->ptr, ccp_option, nb))
563 if (ccp_option[1] < 2) /* preliminary check on the length byte */
565 for (cp = ppp_compressors; *cp != NULL; ++cp)
566 if ((*cp)->compress_proto == ccp_option[0]) {
568 * Found a handler for the protocol - try to allocate
569 * a compressor or decompressor.
574 if (sc->sc_xc_state != NULL)
575 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
577 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
578 if (sc->sc_xc_state == NULL) {
579 IOLogDbg("ppp%d: comp_alloc failed", if_unit(sc->sc_if));
583 sc->sc_flags &= ~SC_COMP_RUN;
585 if (sc->sc_rc_state != NULL)
586 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
588 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
589 if (sc->sc_rc_state == NULL) {
590 IOLogDbg("ppp%d: decomp_alloc failed", if_unit(sc->sc_if));
594 sc->sc_flags &= ~SC_DECOMP_RUN;
599 IOLogDbg("ppp%d: no compressor for [%x %x %x], %x", if_unit(sc->sc_if),
600 ccp_option[0], ccp_option[1], ccp_option[2], nb);
601 return (EINVAL); /* no handler found */
602 #endif /* PPP_COMPRESS */
604 #ifdef HAS_BROKEN_TIOCSPGRP
607 IOLogDbg("ppp%d: pgrp was %d, setting pgrp %d, flags 0x%x, state 0x%x\n",
608 if_unit(sc->sc_if), tp->t_pgrp, *(int *)data,
609 tp->t_flags, tp->t_state);
611 tp->t_pgrp = *(int *)data;
617 npi = (struct npioctl *) data;
618 switch (npi->protocol) {
625 if (cmd == PPPIOCGNPMODE) {
626 npi->mode = sc->sc_npmode[npx];
631 IOLogDbg("ppp%d: setting protocol %d to mode %d\n", if_unit(sc->sc_if), npx, npi->mode);
633 if (npi->mode != sc->sc_npmode[npx]) {
635 sc->sc_npmode[npx] = npi->mode;
636 if (npi->mode != NPMODE_QUEUE) {
648 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
649 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
660 pppcontrol(ifp, cmd, data)
666 register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
669 if (!strcmp(cmd, IFCONTROL_UNIXIOCTL)) {
670 if_ioctl_t* ctl = (if_ioctl_t*)data;
671 return pppsioctl(ifp,
674 } else if (!strcmp(cmd, IFCONTROL_SETADDR)) {
675 struct sockaddr_in *sin = (struct sockaddr_in *)data;
676 if (sin->sin_family != AF_INET)
679 IOLogDbg("ppp%d: setting address, and bringing up interface\n",
682 if_flags_set(ifp, if_flags(ifp) | IFF_UP);
686 * We implement this to allow iftab
687 * to contain -AUTOMATIC- entries
688 * without generating errors at boot time.
689 * We do not, however, mark it as UP.
691 else if (!strcmp(cmd, IFCONTROL_AUTOADDR)) {
692 struct sockaddr_in *sin = (struct sockaddr_in *) data;
693 if (sin->sin_family != AF_INET)
696 IOLogDbg("ppp%d: probing for interface address\n", if_unit(ifp));
699 } else if (!strcmp(cmd, IFCONTROL_SETFLAGS)) {
700 register union ifr_ifru *ifr = (union ifr_ifru *)data;
704 IOLogDbg("ppp%d: old flags 0x%x, new flags 0x%x\n", if_unit(ifp),
705 if_flags(ifp), ifr->ifru_flags);
707 if_flags_set(ifp, ifr->ifru_flags);
711 * Under 3.2 developer, I don't know the symbol for this
712 * new 3.3 command. So it is a constant for now. I don't
713 * believe I need to do anything to support this at the moment.
715 else if (strcmp(cmd, "add-multicast") == 0) {
716 struct sockaddr_in *sin = (struct sockaddr_in *) data;
717 if (sin->sin_family != AF_INET)
720 IOLog("ppp%d: Invalid ppp control %s\n", if_unit(ifp), cmd);
726 * Process an ioctl request to the ppp network interface.
729 pppsioctl(ifp, cmd, data)
730 register netif_t ifp;
734 register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
735 register struct ifaddr *ifa = (struct ifaddr *)data;
736 register struct ifreq *ifr = (struct ifreq *)data;
737 struct ppp_stats *psp;
739 struct ppp_comp_stats *pcp;
741 int s = splimp(), error = 0;
745 IOLog("ppp%d: pppioctl: SIOCSIFFLAGS called!\n", if_unit(ifp));
747 if_flags_set(ifp, (if_flags(ifp) & IFF_CANTCHANGE)
748 | (ifr->ifr_flags & ~IFF_CANTCHANGE));
753 if (ifa->ifa_addr.sa_family != AF_INET)
754 error = EAFNOSUPPORT;
758 if (ifa->ifa_addr.sa_family != AF_INET)
759 error = EAFNOSUPPORT;
767 if_mtu_set(sc->sc_if, ifr->ifr_mtu);
768 nbq_flush(&sc->sc_freeq); /* get rid of old buffers */
769 pppsched(pppfillfreeq, sc); /* and make a queue of new ones */
774 ifr->ifr_mtu = if_mtu(sc->sc_if);
778 psp = &((struct ifpppstatsreq *) data)->stats;
779 bzero(psp, sizeof(*psp));
780 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
781 psp->p.ppp_ipackets = if_ipackets(sc->sc_if);
782 psp->p.ppp_ierrors = if_ierrors(sc->sc_if);
783 psp->p.ppp_obytes = sc->sc_bytessent;
784 psp->p.ppp_opackets = if_opackets(sc->sc_if);
785 psp->p.ppp_oerrors = if_oerrors(sc->sc_if);
787 psp->vj.vjs_packets = sc->sc_comp.stats.vjs_packets;
788 psp->vj.vjs_compressed = sc->sc_comp.stats.vjs_compressed;
789 psp->vj.vjs_searches = sc->sc_comp.stats.vjs_searches;
790 psp->vj.vjs_misses = sc->sc_comp.stats.vjs_misses;
791 psp->vj.vjs_uncompressedin = sc->sc_comp.stats.vjs_uncompressedin;
792 psp->vj.vjs_compressedin = sc->sc_comp.stats.vjs_compressedin;
793 psp->vj.vjs_errorin = sc->sc_comp.stats.vjs_errorin;
794 psp->vj.vjs_tossed = sc->sc_comp.stats.vjs_tossed;
800 pcp = &((struct ifpppcstatsreq *) data)->stats;
801 bzero(pcp, sizeof(*pcp));
802 if (sc->sc_xc_state != NULL)
803 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
804 if (sc->sc_rc_state != NULL)
805 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
807 #endif /* PPP_COMPRESS */
817 * Queue a packet. Start transmission if not active.
818 * Packet is placed in Information field of PPP frame.
821 pppoutput(ifp, m0, arg)
826 register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)];
827 struct sockaddr *dst = (struct sockaddr *) arg;
828 int protocol, address, control;
833 struct nb_queue *ifq;
836 if (sc->sc_devp == NULL || (if_flags(ifp) & IFF_RUNNING) == 0
837 || (if_flags(ifp) & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
838 error = ENETDOWN; /* sort of */
843 * Compute PPP header.
846 switch (dst->sa_family) {
849 address = PPP_ALLSTATIONS;
852 mode = sc->sc_npmode[NP_IP];
855 * If this packet has the "low delay" bit set in the IP header,
856 * or TCP and to an interactive port, put it on the fastq instead
858 ip = mtod(m0, struct ip *);
859 if (ip->ip_tos & IPTOS_LOWDELAY || ip->ip_p == IPPROTO_ICMP)
861 else if (ip->ip_p == IPPROTO_TCP) {
862 register u_short *p = (u_short *) &(((caddr_t) ip)[ip->ip_hl << 2]);
863 if (INTERACTIVE(ntohs(p[0])) || INTERACTIVE(ntohs(p[1])))
864 urgent: flags |= M_HIGHPRI;
870 address = PPP_ALLSTATIONS;
877 address = PPP_ADDRESS(dst->sa_data);
878 control = PPP_CONTROL(dst->sa_data);
879 protocol = PPP_PROTOCOL(dst->sa_data);
883 IOLog("ppp%d: af%d not supported\n", if_unit(ifp), dst->sa_family);
884 error = EAFNOSUPPORT;
889 * Drop this packet, or return an error, if necessary.
891 if (mode == NPMODE_ERROR) {
895 if (mode == NPMODE_DROP) {
903 ppp_nb_grow_top(m0, PPP_HDRLEN);
905 cp = mtod(m0, u_char *);
908 *cp++ = protocol >> 8;
909 *cp++ = protocol & 0xff;
911 if (sc->sc_flags & SC_LOG_OUTPKT) {
912 IOLog("ppp%d: output:\n", if_unit(ifp)); /* XXX */
918 * See if bpf wants to look at the packet.
921 bpf_tap(sc->sc_bpf, nb_map(m0), nb_size(m0));
925 * Put the packet on the appropriate queue.
927 s = splimp(); /* splnet should be OK now */
928 if (mode == NPMODE_QUEUE) {
929 nb_set_mark(m0,flags); /* save priority */
930 /* XXX we should limit the number of packets on this queue */
931 nbq_enqueue(&sc->sc_npq, m0); /* XXX is this correct? */
933 ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq;
934 if (nbq_full(ifq) < 0) {
936 IOLog("ppp%d: output queue full\n", if_unit(sc->sc_if));
938 incr_cnt(sc->sc_if, if_oerrors);
942 nbq_enqueue(ifq, m0);
946 * If we don't have some compressed packets already
947 * and we are not at interrupt priority, then do some compression.
949 * We need to be especially careful here. pppouput() is typically
950 * called at 2 different priority levels. On a NeXT, neither of these
951 * is the interrupt priority level. However, on Intel, one of them is.
952 * I don't know about HPPA or Sparc. Simple fix is to just check.
955 if(!sc->sc_compsched && s != ipltospl(IPLIMP)) {
956 sc->sc_compsched = 1;
958 pppintr_comp(sc); /* Calls pppstart() */
973 * After a change in the NPmode for some NP, move packets from the
974 * npqueue to the send queue or the fast queue as appropriate.
975 * Should be called at splimp (actually splnet would probably suffice).
976 * Due to some of the uglies in the packet queueing system I have
977 * implemented this without the mpp stuff.
983 struct ppp_softc *sc;
986 struct nb_queue *ifq;
991 for (m = sc->sc_npq.head; m; ) {
994 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
996 mode = sc->sc_npmode[NP_IP];
1005 * This packet can now go on one of the queues to be sent.
1010 sc->sc_npq.head = nm;
1011 nb_set_next(m,NULL);
1012 nb_get_mark(m,&flags);
1013 ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq;
1014 if (nbq_full(ifq)) {
1016 incr_cnt(sc->sc_if, if_oerrors);
1019 nbq_enqueue(ifq, m);
1035 sc->sc_npq.tail = lm; /* anything further on has been sent ! */
1039 * Get a packet to send. This procedure is intended to be called
1040 * at spltty()/splimp(), so it takes little time. If there isn't
1041 * a packet waiting to go out, it schedules a software interrupt
1042 * to prepare a new packet; the device start routine gets called
1043 * again when a packet is ready.
1047 struct ppp_softc *sc;
1052 m = nbq_dequeue(&sc->sc_compq);
1055 if (!sc->sc_compsched &&
1056 (! nbq_empty(&sc->sc_slowq) || ! nbq_empty(&sc->sc_fastq)))
1059 if ((error = pppsched(pppintr_comp, sc)) == KERN_SUCCESS)
1060 sc->sc_compsched = 1;
1063 IOLogDbg("ppp%d: compression callout failed returning %d\n",
1064 if_unit(sc->sc_if), error);
1072 * Takes all received input packets and uncompresses/hands_off.
1073 * Must not be reentrant and is called at normal priority.
1074 * Guaranteed Non-Reentrancy means we don't need to be at splnet().
1082 struct ppp_softc *sc = (struct ppp_softc *)arg;
1086 if (nbq_low(&sc->sc_freeq))
1087 pppfillfreeq((void *) sc);
1091 m = nbq_dequeue(&sc->sc_rawq);
1098 * Now we have aparently emptied the queue. So, we try to reset the
1099 * synchronization flag that schedules callbacks. We check for the
1100 * possibility that an interrupt occurred before we finish this check.
1103 if (!nbq_empty(&sc->sc_rawq))
1110 sc->sc_decompsched = 0;
1118 * Readies the next few output packet from
1119 * the sc_fastq/sc_slowq. Will try to
1120 * precompress all packets on the fast
1121 * queue and at most one from the slow queue.
1127 struct ppp_softc *sc = (struct ppp_softc *)arg;
1131 if (nbq_low(&sc->sc_freeq))
1132 pppfillfreeq((void *) sc);
1135 while (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_fastq))
1138 if (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_slowq))
1141 sc->sc_compsched = 0;
1145 * Grab another packet off a queue and apply VJ compression,
1146 * packet compression, address/control and/or protocol compression
1147 * if enabled. Should be called at splnet.
1151 struct ppp_softc *sc;
1156 int address, control, protocol;
1159 * Grab a packet to send: first try the fast queue, then the
1162 m = nbq_dequeue(&sc->sc_fastq);
1164 m = nbq_dequeue(&sc->sc_slowq);
1169 * Extract the ppp header of the new packet.
1170 * The ppp header will be in one netbuf.
1172 cp = mtod(m, u_char *);
1173 address = PPP_ADDRESS(cp);
1174 control = PPP_CONTROL(cp);
1175 protocol = PPP_PROTOCOL(cp);
1180 * Update the time we sent the most recent packet.
1182 sc->sc_last_sent = time.tv_sec;
1186 * If the packet is a TCP/IP packet, see if we can compress it.
1188 if (sc->sc_flags & SC_COMP_TCP) {
1193 ip = (struct ip *) (cp + PPP_HDRLEN);
1194 /* this code assumes the IP/TCP header is in one netbuf */
1195 if (ip->ip_p == IPPROTO_TCP) {
1196 type = vj_compress_tcp(ip, nb_size(m) - PPP_HDRLEN,
1198 !(sc->sc_flags & SC_NO_TCP_CCID), &vjhdr);
1200 case TYPE_UNCOMPRESSED_TCP:
1201 protocol = PPP_VJC_UNCOMP;
1203 case TYPE_COMPRESSED_TCP:
1204 ppp_nb_shrink_top(m, vjhdr - (u_char *) ip);
1205 protocol = PPP_VJC_COMP;
1206 cp = mtod(m, u_char *);
1207 cp[0] = address; /* header has moved */
1212 cp[3] = protocol; /* update protocol in PPP header */
1222 #endif /* PPP_COMPRESS */
1226 if (protocol != PPP_LCP && protocol != PPP_CCP
1227 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1232 clen = (*sc->sc_xcomp->compress)
1233 (sc->sc_xc_state, &mcomp, m, slen,
1234 sc->sc_flags & SC_CCP_UP? if_mtu(sc->sc_if): 0);
1235 if (mcomp != NULL) {
1238 cp = mtod(m, u_char *);
1242 #endif /* PPP_COMPRESS */
1245 * Compress the address/control and protocol, if possible.
1247 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1248 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1249 protocol != PPP_LCP) {
1250 /* can compress address/control */
1251 ppp_nb_shrink_top(m, 2);
1253 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1254 /* can compress protocol */
1255 if (mtod(m, u_char *) == cp) {
1256 cp[2] = cp[1]; /* move address/control up */
1259 ppp_nb_shrink_top(m, 1);
1263 nbq_enqueue(&sc->sc_compq, m);
1264 (*sc->sc_start)(sc);
1270 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1271 * 0 if it is about to be transmitted.
1274 ppp_ccp(sc, m, rcvd)
1275 struct ppp_softc *sc;
1283 * Get a pointer to the data after the PPP header.
1285 dp = mtod(m, u_char *) + PPP_HDRLEN;
1287 ep = mtod(m, u_char *) + nb_size(m);
1288 if (dp + CCP_HDRLEN > ep)
1290 slen = CCP_LENGTH(dp);
1291 if (dp + slen > ep) {
1292 IOLogDbg("ppp%d: ccp: not enough data in netbuf (%x+%x > %x+%x)\n",
1293 if_unit(sc->sc_if), dp, slen, mtod(m, u_char *), nb_size(m));
1297 switch (CCP_CODE(dp)) {
1301 /* CCP must be going down - disable compression */
1302 if (sc->sc_flags & SC_CCP_UP) {
1304 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1310 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1311 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1312 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1314 /* we're agreeing to send compressed packets. */
1315 if (sc->sc_xc_state != NULL
1316 && (*sc->sc_xcomp->comp_init)
1317 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1318 if_unit(sc->sc_if), 0, sc->sc_flags & SC_DEBUG)) {
1320 sc->sc_flags |= SC_COMP_RUN;
1324 /* peer is agreeing to send compressed packets. */
1325 if (sc->sc_rc_state != NULL
1326 && (*sc->sc_rcomp->decomp_init)
1327 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1332 0, sc->sc_mru, sc->sc_flags & SC_DEBUG)) {
1334 sc->sc_flags |= SC_DECOMP_RUN;
1335 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1343 if (sc->sc_flags & SC_CCP_UP) {
1345 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1346 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1347 nbq_flush(&sc->sc_compq); /* Flush pre-compressed packets */
1350 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1351 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1353 sc->sc_flags &= ~SC_DC_ERROR;
1363 * CCP is down; free (de)compressor state if necessary.
1367 struct ppp_softc *sc;
1369 if (sc->sc_xc_state) {
1370 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1371 sc->sc_xc_state = NULL;
1373 if (sc->sc_rc_state) {
1374 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1375 sc->sc_rc_state = NULL;
1378 #endif /* PPP_COMPRESS */
1381 * PPP packet input routine.
1382 * The caller has checked and removed the FCS and has inserted
1383 * the address/control bytes and the protocol high byte if they
1387 ppppktin(sc, m, lost)
1388 struct ppp_softc *sc;
1392 int error, s = splimp();
1394 nb_set_mark(m,(lost ? M_ERRMARK : 0));
1396 /* XXX - we should check for the raw queue overflowing... */
1397 nbq_enqueue(&sc->sc_rawq, m);
1398 if (!sc->sc_decompsched)
1400 if ((error = pppsched(pppintr_decomp, sc)) == KERN_SUCCESS)
1401 sc->sc_decompsched = 1;
1403 IOLogDbg("ppp%d: decompression callout failed returning %d\n",
1404 if_unit(sc->sc_if), error);
1411 * Process a received PPP packet, doing decompression as necessary.
1413 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1414 TYPE_UNCOMPRESSED_TCP)
1418 struct ppp_softc *sc;
1421 struct nb_queue *inq;
1422 int s, ilen, xlen, proto, rv;
1424 u_char *cp, adrs, ctrl;
1429 incr_cnt(sc->sc_if, if_ipackets);
1431 nb_get_mark(m,&flags);
1433 if (sc->sc_flags & SC_LOG_INPKT) {
1434 IOLog("ppp%d: got %d bytes\n", if_unit(sc->sc_if), nb_size(m));
1438 cp = mtod(m, u_char *);
1439 adrs = PPP_ADDRESS(cp);
1440 ctrl = PPP_CONTROL(cp);
1441 proto = PPP_PROTOCOL(cp);
1443 if (flags & M_ERRMARK) {
1445 sc->sc_flags |= SC_VJ_RESET;
1451 * Decompress this packet if necessary, update the receiver's
1452 * dictionary, or take appropriate action on a CCP packet.
1454 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1455 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1456 /* decompress this packet */
1457 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1458 if (rv == DECOMP_OK){
1461 /* No error, but no decompressed packet returned */
1465 cp = mtod(m, u_char *);
1466 proto = PPP_PROTOCOL(cp);
1470 * An error has occurred in decompression.
1471 * Pass the compressed packet up to pppd, which may take
1472 * CCP down or issue a Reset-Req.
1474 IOLogDbg("ppp%d: decompress failed %d\n", if_unit(sc->sc_if), rv);
1476 sc->sc_flags |= SC_VJ_RESET;
1477 if (rv == DECOMP_ERROR)
1478 sc->sc_flags |= SC_DC_ERROR;
1480 sc->sc_flags |= SC_DC_FERROR;
1485 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1486 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1488 if (proto == PPP_CCP) {
1497 if (sc->sc_flags & SC_VJ_RESET) {
1499 * If we've missed a packet, we must toss subsequent compressed
1500 * packets which don't have an explicit connection ID.
1502 vj_uncompress_err(&sc->sc_comp);
1504 sc->sc_flags &= ~SC_VJ_RESET;
1509 * See if we have a VJ-compressed packet to uncompress.
1511 if (proto == PPP_VJC_COMP) {
1512 if (sc->sc_flags & SC_REJ_COMP_TCP)
1515 xlen = vj_uncompress_tcp(cp + PPP_HDRLEN, ilen - PPP_HDRLEN,
1517 &sc->sc_comp, &iphdr, &hlen);
1520 IOLogDbg("ppp%d: VJ uncompress failed on type comp\n",
1521 if_unit(sc->sc_if));
1525 ppp_nb_grow_top(m, hlen - xlen);
1526 nb_write(m, PPP_HDRLEN, hlen, iphdr);
1528 cp = mtod(m, u_char *);
1535 ilen += hlen - xlen;
1537 } else if (proto == PPP_VJC_UNCOMP) {
1538 if (sc->sc_flags & SC_REJ_COMP_TCP)
1541 vj_uncompress_uncomp(cp + PPP_HDRLEN, &sc->sc_comp);
1549 /* See if bpf wants to look at the packet. */
1551 bpf_tap(sc->sc_bpf, nb_map(m), nb_size(m));
1559 * IP packet - take off the ppp header and pass it up to IP.
1561 if ((if_flags(sc->sc_if) & IFF_UP) == 0
1562 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1563 /* interface is down - drop the packet. */
1565 IOLogDbg("ppp%d: IP packed dropped (NPmode)\n", if_unit(sc->sc_if));
1568 ppp_nb_shrink_top(m, PPP_HDRLEN);
1569 inet_queue(sc->sc_if, m);
1570 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1576 * Some other protocol - place on input queue for read().
1584 * Put the packet on the appropriate input queue.
1587 if (nbq_full(inq)) {
1590 IOLog("ppp%d: input queue full\n", if_unit(sc->sc_if));
1593 nbq_enqueue(inq, m);
1603 incr_cnt(sc->sc_if, if_ierrors);
1606 #define MAX_DUMP_BYTES 128
1612 char buf[3*MAX_DUMP_BYTES+4];
1614 static char digits[] = "0123456789abcdef";
1615 int l = nb_size(m0);
1616 u_char *rptr = mtod(m0, u_char *);
1619 if (bp > buf + sizeof(buf) - 4)
1621 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1622 *bp++ = digits[*rptr++ & 0xf];