]> git.ozlabs.org Git - ppp.git/blob - ultrix/if_ppp.c
fix for linux compilation
[ppp.git] / ultrix / if_ppp.c
1 /*
2  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
3  *
4  * Copyright (c) 1989 Carnegie Mellon University.
5  * All rights reserved.
6  *
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.
18  *
19  * Drew D. Perkins
20  * Carnegie Mellon University
21  * 4910 Forbes Ave.
22  * Pittsburgh, PA 15213
23  * (412) 268-8576
24  * ddp@andrew.cmu.edu
25  *
26  * Based on:
27  *      @(#)if_sl.c     7.6.1.2 (Berkeley) 2/15/89
28  *
29  * Copyright (c) 1987 Regents of the University of California.
30  * All rights reserved.
31  *
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.
43  *
44  * Serial Line interface
45  *
46  * Rick Adams
47  * Center for Seismic Studies
48  * 1300 N 17th Street, Suite 1450
49  * Arlington, Virginia 22209
50  * (703)276-7900
51  * rick@seismo.ARPA
52  * seismo!rick
53  *
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.
57  *
58  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59  * Added VJ tcp header compression; more unified ioctls
60  *
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.
66  *
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).
70  *
71  * Ultrix port by Per Sundstrom <sundstrom@stkhlm.enet.dec.com>,
72  * Robert Olsson <robert@robur.slu.se> and Paul Mackerras.
73  */
74
75 /* $Id: if_ppp.c,v 1.5 1994/12/13 03:24:47 paulus Exp $ */
76 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
77
78 #include "ppp.h"
79 #if NPPP > 0
80
81 #define VJC
82 #define PPP_COMPRESS
83
84 #include "../h/param.h"
85 #include "../h/user.h"
86 #include "../h/proc.h"
87 #include "../h/mbuf.h"
88 #include "../h/buf.h"
89 #include "../h/socket.h"
90 #include "../h/ioctl.h"
91 #include "../h/systm.h"
92
93 #include "../net/net/if.h"
94 #include "../net/net/netisr.h"
95 #include "../net/net/route.h"
96
97 #if INET
98 #include "../net/netinet/in.h"
99 #include "../net/netinet/in_systm.h"
100 #include "../net/netinet/in_var.h"
101 #include "../net/netinet/ip.h"
102 #endif
103
104 #ifdef vax
105 #include "../machine/mtpr.h"
106 #endif
107
108 #include "ppp_defs.h"
109 #include "if_ppp.h"
110
111 #ifdef VJC
112 #include "slcompress.h"
113 #endif
114
115 #include "if_pppvar.h"
116
117 #ifdef PPP_COMPRESS
118 #define PACKETPTR       struct mbuf *
119 #include "ppp-comp.h"
120 #endif
121
122 void    pppattach __P((void));
123 int     pppioctl __P((struct ppp_softc *sc, int cmd, caddr_t data, int flag,
124                       struct proc *));
125 int     pppoutput __P((struct ifnet *ifp, struct mbuf *m0,
126                        struct sockaddr *dst));
127 int     pppsioctl __P((struct ifnet *ifp, int cmd, caddr_t data));
128 void    pppintr __P((void));
129
130 static void     ppp_requeue __P((struct ppp_softc *));
131 static void     ppp_outpkt __P((struct ppp_softc *));
132 static int      ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
133 static void     ppp_ccp_closed __P((struct ppp_softc *));
134 static void     ppp_inproc __P((struct ppp_softc *, struct mbuf *));
135 static void     pppdumpm __P((struct mbuf *m0));
136
137 /*
138  * Some useful mbuf macros not in mbuf.h.
139  */
140 #define M_IS_CLUSTER(m) ((m)->m_off > MMAXOFF)
141
142 #define M_TRAILINGSPACE(m) \
143         ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \
144          - ((m)->m_off + (m)->m_len))
145
146 #define M_OFFSTART(m)   \
147         (M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr : MMINOFF)
148
149 #define M_DATASIZE(m)   \
150         (M_IS_CLUSTER(m) ? M_CLUSTERSZ : MLEN)
151
152 /*
153  * The following disgusting hack gets around the problem that IP TOS
154  * can't be set yet.  We want to put "interactive" traffic on a high
155  * priority queue.  To decide if traffic is interactive, we check that
156  * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
157  */
158 static u_short interactive_ports[8] = {
159         0,      513,    0,      0,
160         0,      21,     0,      23,
161 };
162 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
163
164 #ifdef PPP_COMPRESS
165 /*
166  * List of compressors we know about.
167  */
168
169 extern struct compressor ppp_bsd_compress;
170
171 struct compressor *ppp_compressors[] = {
172     &ppp_bsd_compress,
173     NULL
174 };
175 #endif /* PPP_COMPRESS */
176
177 /*
178  * Called from boot code to establish ppp interfaces.
179  */
180 void
181 pppattach()
182 {
183     register struct ppp_softc *sc;
184     register int i = 0;
185
186     for (sc = ppp_softc; i < NPPP; sc++) {
187         sc->sc_if.if_name = "ppp";
188         sc->sc_if.if_unit = i++;
189         sc->sc_if.if_mtu = PPP_MTU;
190         sc->sc_if.if_flags = IFF_POINTOPOINT;
191         sc->sc_if.if_type = IFT_PPP;
192         sc->sc_if.if_ioctl = pppsioctl;
193         sc->sc_if.if_output = pppoutput;
194         sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
195         sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
196         sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
197         sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
198         if_attach(&sc->sc_if);
199 #if NBPFILTER > 0
200         bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
201 #endif
202     }
203 }
204
205 /*
206  * Allocate a ppp interface unit and initialize it.
207  */
208 struct ppp_softc *
209 pppalloc(pid)
210     pid_t pid;
211 {
212     int nppp, i;
213     struct ppp_softc *sc;
214
215     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
216         if (sc->sc_xfer == pid) {
217             sc->sc_xfer = 0;
218             return sc;
219         }
220     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
221         if (sc->sc_devp == NULL)
222             break;
223     if (nppp >= NPPP)
224         return NULL;
225
226     sc->sc_flags = 0;
227     sc->sc_mru = PPP_MRU;
228     sc->sc_relinq = NULL;
229 #ifdef VJC
230     sl_compress_init(&sc->sc_comp);
231 #endif
232 #ifdef PPP_COMPRESS
233     sc->sc_xc_state = NULL;
234     sc->sc_rc_state = NULL;
235 #endif /* PPP_COMPRESS */
236     for (i = 0; i < NUM_NP; ++i)
237         sc->sc_npmode[i] = NPMODE_ERROR;
238     sc->sc_npqueue = NULL;
239     sc->sc_npqtail = &sc->sc_npqueue;
240
241     return sc;
242 }
243
244 /*
245  * Deallocate a ppp unit.  Must be called at splnet or higher.
246  */
247 void
248 pppdealloc(sc)
249     struct ppp_softc *sc;
250 {
251     struct mbuf *m;
252
253     if_down(&sc->sc_if);
254     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
255     sc->sc_devp = NULL;
256     sc->sc_xfer = 0;
257     for (;;) {
258         IF_DEQUEUE(&sc->sc_rawq, m);
259         if (m == NULL)
260             break;
261         m_freem(m);
262     }
263     for (;;) {
264         IF_DEQUEUE(&sc->sc_inq, m);
265         if (m == NULL)
266             break;
267         m_freem(m);
268     }
269     for (;;) {
270         IF_DEQUEUE(&sc->sc_fastq, m);
271         if (m == NULL)
272             break;
273         m_freem(m);
274     }
275     while ((m = sc->sc_npqueue) != NULL) {
276         sc->sc_npqueue = m->m_act;
277         m_freem(m);
278     }
279     if (sc->sc_togo != NULL) {
280         m_freem(sc->sc_togo);
281         sc->sc_togo = NULL;
282     }
283 #ifdef PPP_COMPRESS
284     ppp_ccp_closed(sc);
285     sc->sc_xc_state = NULL;
286     sc->sc_rc_state = NULL;
287 #endif /* PPP_COMPRESS */
288 }
289
290 /*
291  * Ioctl routine for generic ppp devices.
292  */
293 int
294 pppioctl(sc, cmd, data, flag)
295     struct ppp_softc *sc;
296     caddr_t data;
297     int cmd, flag;
298 {
299     struct proc *p = u.u_procp;
300     int s, error, flags, mru, mtu, nb, npx;
301     struct ppp_option_data *odp;
302     struct compressor **cp;
303     struct npioctl *npi;
304     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
305
306     switch (cmd) {
307     case FIONREAD:
308         *(int *)data = sc->sc_inq.ifq_len;
309         break;
310
311     case PPPIOCGUNIT:
312         *(int *)data = sc->sc_if.if_unit;
313         break;
314
315     case PPPIOCGFLAGS:
316         *(u_int *)data = sc->sc_flags;
317         break;
318
319     case PPPIOCSFLAGS:
320         if (!suser())
321             return EPERM;
322         flags = *(int *)data & SC_MASK;
323         s = splnet();
324         if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
325             ppp_ccp_closed(sc);
326         splimp();
327         sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
328         splx(s);
329         break;
330
331     case PPPIOCSMRU:
332         if (!suser())
333             return EPERM;
334         mru = *(int *)data;
335         if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
336             sc->sc_mru = mru;
337         break;
338
339     case PPPIOCGMRU:
340         *(int *)data = sc->sc_mru;
341         break;
342
343     /*
344      * PPPIOC[GS]MTU are implemented here, instead of supporting
345      * SIOC[GS]IFMTU in pppsioctl, because under Ultrix, we can't get an
346      * interface ioctl through to the interface until it has an IP
347      * address set.
348      */
349     case PPPIOCSMTU:
350         if (!suser())
351             return EPERM;
352         mtu = *(int *) data;
353         if (mtu < PPP_MRU || mtu > PPP_MAXMRU)
354             return EINVAL;
355         s = splimp();
356         sc->sc_if.if_mtu = mtu;
357         splx(s);
358         break;
359
360     case PPPIOCGMTU:
361         *(int *) data = sc->sc_if.if_mtu;
362         break;
363
364 #ifdef VJC
365     case PPPIOCSMAXCID:
366         if (!suser())
367             return EPERM;
368         s = splnet();
369         sl_compress_setup(&sc->sc_comp, *(int *)data);
370         splx(s);
371         break;
372 #endif
373
374     case PPPIOCXFERUNIT:
375         if (!suser())
376             return EPERM;
377         sc->sc_xfer = p->p_pid;
378         break;
379
380 #ifdef PPP_COMPRESS
381     case PPPIOCSCOMPRESS:
382         if (!suser())
383             return EPERM;
384         odp = (struct ppp_option_data *) data;
385         nb = odp->length;
386         if (nb > sizeof(ccp_option))
387             nb = sizeof(ccp_option);
388         if (error = copyin(odp->ptr, ccp_option, nb))
389             return (error);
390         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
391             return (EINVAL);
392         for (cp = ppp_compressors; *cp != NULL; ++cp)
393             if ((*cp)->compress_proto == ccp_option[0]) {
394                 /*
395                  * Found a handler for the protocol - try to allocate
396                  * a compressor or decompressor.
397                  */
398                 error = 0;
399                 s = splnet();
400                 if (odp->transmit) {
401                     if (sc->sc_xc_state != NULL)
402                         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
403                     sc->sc_xcomp = *cp;
404                     sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
405                     if (sc->sc_xc_state == NULL) {
406                         if (sc->sc_flags & SC_DEBUG)
407                             printf("ppp%d: comp_alloc failed\n",
408                                sc->sc_if.if_unit);
409                         error = ENOBUFS;
410                     }
411                     splimp();
412                     sc->sc_flags &= ~SC_COMP_RUN;
413                 } else {
414                     if (sc->sc_rc_state != NULL)
415                         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
416                     sc->sc_rcomp = *cp;
417                     sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
418                     if (sc->sc_rc_state == NULL) {
419                         if (sc->sc_flags & SC_DEBUG)
420                             printf("ppp%d: decomp_alloc failed\n",
421                                sc->sc_if.if_unit);
422                         error = ENOBUFS;
423                     }
424                     splimp();
425                     sc->sc_flags &= ~SC_DECOMP_RUN;
426                 }
427                 splx(s);
428                 return (error);
429             }
430         if (sc->sc_flags & SC_DEBUG)
431             printf("ppp%d: no compressor for [%x %x %x], %x\n",
432                    sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
433                    ccp_option[2], nb);
434         return (EINVAL);        /* no handler found */
435 #endif /* PPP_COMPRESS */
436
437     case PPPIOCGNPMODE:
438     case PPPIOCSNPMODE:
439         npi = (struct npioctl *) data;
440         switch (npi->protocol) {
441         case PPP_IP:
442             npx = NP_IP;
443             break;
444         default:
445             return EINVAL;
446         }
447         if (cmd == PPPIOCGNPMODE) {
448             npi->mode = sc->sc_npmode[npx];
449         } else {
450             if (!suser())
451                 return EPERM;
452             if (npi->mode != sc->sc_npmode[npx]) {
453                 s = splimp();
454                 sc->sc_npmode[npx] = npi->mode;
455                 if (npi->mode != NPMODE_QUEUE) {
456                     ppp_requeue(sc);
457                     (*sc->sc_start)(sc);
458                 }
459                 splx(s);
460             }
461         }
462         break;
463
464     default:
465         return (-1);
466     }
467     return (0);
468 }
469
470 /*
471  * Process an ioctl request to the ppp network interface.
472  */
473 int
474 pppsioctl(ifp, cmd, data)
475     register struct ifnet *ifp;
476     int cmd;
477     caddr_t data;
478 {
479     struct proc *p = u.u_procp;
480     register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
481     register struct ifaddr *ifa = (struct ifaddr *)data;
482     register struct ifreq *ifr = (struct ifreq *)data;
483     struct ppp_stats *psp;
484     struct ppp_comp_stats *pcp;
485     int s = splimp(), error = 0;
486
487     switch (cmd) {
488     case SIOCSIFFLAGS:
489         if ((ifp->if_flags & IFF_RUNNING) == 0)
490             ifp->if_flags &= ~IFF_UP;
491         break;
492
493     case SIOCSIFADDR:
494         if (ifa->ifa_addr.sa_family != AF_INET)
495             error = EAFNOSUPPORT;
496         break;
497
498     case SIOCSIFDSTADDR:
499         if (ifa->ifa_addr.sa_family != AF_INET)
500             error = EAFNOSUPPORT;
501         break;
502
503 /*
504  * Ioctls other than the above don't get through until the
505  * interface has its IP addresses set :-(
506  */
507
508 #if 0
509     case SIOCSIFMTU:
510         if (!suser())
511             return EPERM;
512         sc->sc_if.if_mtu = ifr->ifr_mtu;
513         break;
514
515     case SIOCGIFMTU:
516         ifr->ifr_mtu = sc->sc_if.if_mtu;
517         break;
518 #endif
519
520     case SIOCGPPPSTATS:
521         psp = &((struct ifpppstatsreq *) data)->stats;
522         bzero(psp, sizeof(*psp));
523         psp->p.ppp_ibytes = sc->sc_bytesrcvd;
524         psp->p.ppp_ipackets = ifp->if_ipackets;
525         psp->p.ppp_ierrors = ifp->if_ierrors;
526         psp->p.ppp_obytes = sc->sc_bytessent;
527         psp->p.ppp_opackets = ifp->if_opackets;
528         psp->p.ppp_oerrors = ifp->if_oerrors;
529 #ifdef VJC
530         psp->vj.vjs_packets = sc->sc_comp.sls_packets;
531         psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
532         psp->vj.vjs_searches = sc->sc_comp.sls_searches;
533         psp->vj.vjs_misses = sc->sc_comp.sls_misses;
534         psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
535         psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
536         psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
537         psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
538 #endif /* VJC */
539         break;
540
541 #ifdef PPP_COMPRESS
542     case SIOCGPPPCSTATS:
543         pcp = &((struct ifpppcstatsreq *) data)->stats;
544         bzero(pcp, sizeof(*pcp));
545         if (sc->sc_xc_state != NULL)
546             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
547         if (sc->sc_rc_state != NULL)
548             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
549         break;
550 #endif /* PPP_COMPRESS */
551
552     default:
553         error = EINVAL;
554     }
555     splx(s);
556     return (error);
557 }
558
559 /*
560  * Queue a packet.  Start transmission if not active.
561  * Packet is placed in Information field of PPP frame.
562  */
563 int
564 pppoutput(ifp, m0, dst)
565     struct ifnet *ifp;
566     struct mbuf *m0;
567     struct sockaddr *dst;
568 {
569     register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
570     struct ppp_header *ph;
571     int protocol, address, control;
572     u_char *cp;
573     int s, error;
574     struct ip *ip;
575     struct ifqueue *ifq;
576     enum NPmode mode;
577
578     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
579         || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
580         error = ENETDOWN;       /* sort of */
581         goto bad;
582     }
583
584     /*
585      * Compute PPP header.
586      * We use the m_context field of the mbuf to indicate whether
587      * the packet should go on the fast queue.
588      */
589     m0->m_context = 0;
590     switch (dst->sa_family) {
591 #ifdef INET
592     case AF_INET:
593         address = PPP_ALLSTATIONS;
594         control = PPP_UI;
595         protocol = PPP_IP;
596         mode = sc->sc_npmode[NP_IP];
597         
598         /*
599          * If this is a TCP packet to or from an "interactive" port,
600          * put the packet on the fastq instead.
601          */
602         if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
603             register int p = ntohl(((int *)ip)[ip->ip_hl]);
604             if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
605                 m0->m_context = 1;
606         }
607         break;
608 #endif
609     case AF_UNSPEC:
610         address = PPP_ADDRESS(dst->sa_data);
611         control = PPP_CONTROL(dst->sa_data);
612         protocol = PPP_PROTOCOL(dst->sa_data);
613         mode = NPMODE_PASS;
614         break;
615     default:
616         printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
617         error = EAFNOSUPPORT;
618         goto bad;
619     }
620
621     /*
622      * Drop this packet, or return an error, if necessary.
623      */
624     if (mode == NPMODE_ERROR) {
625         error = ENETDOWN;
626         goto bad;
627     }
628     if (mode == NPMODE_DROP) {
629         error = 0;
630         goto bad;
631     }
632
633     /*
634      * Add PPP header.  If no space in first mbuf, allocate another.
635      */
636     if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
637         struct mbuf *m;
638
639         MGET(m, M_DONTWAIT, MT_DATA);
640         if (m == NULL) {
641             m_freem(m0);
642             return (ENOBUFS);
643         }
644         m->m_len = 0;
645         m->m_next = m0;
646         m0 = m;
647     } else
648         m0->m_off -= PPP_HDRLEN;
649
650     cp = mtod(m0, u_char *);
651     *cp++ = address;
652     *cp++ = control;
653     *cp++ = protocol >> 8;
654     *cp++ = protocol & 0xff;
655     m0->m_len += PPP_HDRLEN;
656
657     if (sc->sc_flags & SC_LOG_OUTPKT) {
658         printf("ppp%d output: ", ifp->if_unit);
659         pppdumpm(m0);
660     }
661
662 #if NBPFILTER > 0
663     /*
664      * See if bpf wants to look at the packet.
665      */
666     if (sc->sc_bpf)
667         bpf_mtap(sc->sc_bpf, m0);
668 #endif
669
670     /*
671      * Put the packet on the appropriate queue.
672      */
673     s = splimp();               /* splnet should be OK now */
674     if (mode == NPMODE_QUEUE) {
675         /* XXX we should limit the number of packets on this queue */
676         *sc->sc_npqtail = m0;
677         m0->m_act = NULL;
678         sc->sc_npqtail = &m0->m_act;
679     } else {
680         ifq = m0->m_context? &sc->sc_fastq: &ifp->if_snd;
681         if (IF_QFULL(ifq)) {
682             IF_DROP(ifq);
683             splx(s);
684             sc->sc_if.if_oerrors++;
685             error = ENOBUFS;
686             goto bad;
687         }
688         IF_ENQUEUE(ifq, m0);
689         (*sc->sc_start)(sc);
690     }
691
692     splx(s);
693     return (0);
694
695 bad:
696     m_freem(m0);
697     return (error);
698 }
699
700 /*
701  * After a change in the NPmode for some NP, move packets from the
702  * npqueue to the send queue or the fast queue as appropriate.
703  * Should be called at splimp (actually splnet would probably suffice).
704  */
705 static void
706 ppp_requeue(sc)
707     struct ppp_softc *sc;
708 {
709     struct mbuf *m, **mpp;
710     struct ifqueue *ifq;
711     enum NPmode mode;
712
713     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
714         switch (PPP_PROTOCOL(mtod(m, u_char *))) {
715         case PPP_IP:
716             mode = sc->sc_npmode[NP_IP];
717             break;
718         default:
719             mode = NPMODE_PASS;
720         }
721
722         switch (mode) {
723         case NPMODE_PASS:
724             /*
725              * This packet can now go on one of the queues to be sent.
726              */
727             *mpp = m->m_act;
728             m->m_act = NULL;
729             ifq = m->m_context? &sc->sc_fastq: &sc->sc_if.if_snd;
730             if (IF_QFULL(ifq)) {
731                 IF_DROP(ifq);
732                 sc->sc_if.if_oerrors++;
733             } else
734                 IF_ENQUEUE(ifq, m);
735             break;
736
737         case NPMODE_DROP:
738         case NPMODE_ERROR:
739             *mpp = m->m_act;
740             m_freem(m);
741             break;
742
743         case NPMODE_QUEUE:
744             mpp = &m->m_act;
745             break;
746         }
747     }
748     sc->sc_npqtail = mpp;
749 }
750
751 /*
752  * Get a packet to send.  This procedure is intended to be called
753  * at spltty()/splimp(), so it takes little time.  If there isn't
754  * a packet waiting to go out, it schedules a software interrupt
755  * to prepare a new packet; the device start routine gets called
756  * again when a packet is ready.
757  */
758 struct mbuf *
759 ppp_dequeue(sc)
760     struct ppp_softc *sc;
761 {
762     struct mbuf *m;
763     int s = splimp();
764
765     m = sc->sc_togo;
766     if (m) {
767         /*
768          * Had a packet waiting - send it.
769          */
770         sc->sc_togo = NULL;
771         sc->sc_flags |= SC_TBUSY;
772         splx(s);
773         return m;
774     }
775     /*
776      * Remember we wanted a packet and schedule a software interrupt.
777      */
778     sc->sc_flags &= ~SC_TBUSY;
779     schednetisr(NETISR_PPP);
780     splx(s);
781     return NULL;
782 }
783
784 /*
785  * Software interrupt routine, called at splnet().
786  */
787 void
788 pppintr()
789 {
790     struct ppp_softc *sc;
791     int i, s;
792     struct mbuf *m;
793
794     s = splnet();
795     sc = ppp_softc;
796     for (i = 0; i < NPPP; ++i, ++sc) {
797         if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
798             && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
799             ppp_outpkt(sc);
800         for (;;) {
801             IF_DEQUEUE(&sc->sc_rawq, m);
802             if (m == NULL)
803                 break;
804             ppp_inproc(sc, m);
805         }
806     }
807     splx(s);
808 }
809
810 /*
811  * Grab another packet off a queue and apply VJ compression,
812  * packet compression, address/control and/or protocol compression
813  * if enabled.  Should be called at splnet.
814  */
815 static void
816 ppp_outpkt(sc)
817     struct ppp_softc *sc;
818 {
819     int s;
820     struct mbuf *m, *mp;
821     u_char *cp;
822     int address, control, protocol;
823     enum NPmode mode;
824
825     /*
826      * Grab a packet to send: first try the fast queue, then the
827      * normal queue.
828      */
829     IF_DEQUEUE(&sc->sc_fastq, m);
830     if (m == NULL)
831         IF_DEQUEUE(&sc->sc_if.if_snd, m);
832     if (m == NULL)
833         return;
834
835     /*
836      * Extract the ppp header of the new packet.
837      * The ppp header will be in one mbuf.
838      */
839     cp = mtod(m, u_char *);
840     address = PPP_ADDRESS(cp);
841     control = PPP_CONTROL(cp);
842     protocol = PPP_PROTOCOL(cp);
843
844     switch (protocol) {
845 #ifdef VJC
846     case PPP_IP:
847         /*
848          * If the packet is a TCP/IP packet, see if we can compress it.
849          */
850         if (sc->sc_flags & SC_COMP_TCP) {
851             struct ip *ip;
852             int type;
853
854             mp = m;
855             ip = (struct ip *) (cp + PPP_HDRLEN);
856             if (mp->m_len <= PPP_HDRLEN) {
857                 mp = mp->m_next;
858                 if (mp == NULL)
859                     break;
860                 ip = mtod(mp, struct ip *);
861             }
862             /* this code assumes the IP/TCP header is in one non-shared mbuf */
863             if (ip->ip_p == IPPROTO_TCP) {
864                 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
865                                        !(sc->sc_flags & SC_NO_TCP_CCID));
866                 switch (type) {
867                 case TYPE_UNCOMPRESSED_TCP:
868                     protocol = PPP_VJC_UNCOMP;
869                     break;
870                 case TYPE_COMPRESSED_TCP:
871                     protocol = PPP_VJC_COMP;
872                     cp = mtod(m, u_char *);
873                     cp[0] = address;    /* header has moved */
874                     cp[1] = control;
875                     cp[2] = 0;
876                     break;
877                 }
878                 cp[3] = protocol;       /* update protocol in PPP header */
879             }
880         }
881         break;
882 #endif  /* VJC */
883
884 #ifdef PPP_COMPRESS
885     case PPP_CCP:
886         ppp_ccp(sc, m, 0);
887         break;
888 #endif  /* PPP_COMPRESS */
889     }
890
891 #ifdef PPP_COMPRESS
892     if (protocol != PPP_LCP && protocol != PPP_CCP
893         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
894         struct mbuf *mcomp;
895         int slen, clen;
896
897         slen = 0;
898         for (mp = m; mp != NULL; mp = mp->m_next)
899             slen += mp->m_len;
900         clen = (*sc->sc_xcomp->compress)
901             (sc->sc_xc_state, &mcomp, m, slen,
902              (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
903         if (mcomp != NULL) {
904             m_freem(m);
905             m = mcomp;
906             cp = mtod(m, u_char *);
907             protocol = cp[3];
908         }
909     }
910 #endif  /* PPP_COMPRESS */
911
912     /*
913      * Compress the address/control and protocol, if possible.
914      */
915     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
916         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
917         protocol != PPP_LCP) {
918         /* can compress address/control */
919         m->m_off += 2;
920         m->m_len -= 2;
921     }
922     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
923         /* can compress protocol */
924         if (mtod(m, u_char *) == cp) {
925             cp[2] = cp[1];      /* move address/control up */
926             cp[1] = cp[0];
927         }
928         ++m->m_off;
929         --m->m_len;
930     }
931
932     s = splimp();
933     sc->sc_togo = m;
934     (*sc->sc_start)(sc);
935     splx(s);
936 }
937
938 #ifdef PPP_COMPRESS
939 /*
940  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
941  * 0 if it is about to be transmitted.
942  */
943 static int
944 ppp_ccp(sc, m, rcvd)
945     struct ppp_softc *sc;
946     struct mbuf *m;
947     int rcvd;
948 {
949     u_char *dp, *ep;
950     struct mbuf *mp;
951     int slen, s;
952     struct bsd_db *db;
953
954     /*
955      * Get a pointer to the data after the PPP header.
956      */
957     if (m->m_len <= PPP_HDRLEN) {
958         mp = m->m_next;
959         if (mp == NULL)
960             return;
961         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
962     } else {
963         mp = m;
964         dp = mtod(mp, u_char *) + PPP_HDRLEN;
965     }
966
967     ep = mtod(mp, u_char *) + mp->m_len;
968     if (dp + CCP_HDRLEN > ep)
969         return;
970     slen = CCP_LENGTH(dp);
971     if (dp + slen > ep) {
972         if (sc->sc_flags & SC_DEBUG)
973             printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
974                    dp, slen, mtod(mp, u_char *), mp->m_len);
975         return;
976     }
977
978     switch (CCP_CODE(dp)) {
979     case CCP_CONFREQ:
980     case CCP_TERMREQ:
981     case CCP_TERMACK:
982         /* CCP must be going down - disable compression */
983         if (sc->sc_flags & SC_CCP_UP) {
984             s = splimp();
985             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
986             splx(s);
987         }
988         break;
989
990     case CCP_CONFACK:
991         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
992             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
993             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
994             if (!rcvd) {
995                 /* we're agreeing to send compressed packets. */
996                 if (sc->sc_xc_state != NULL
997                     && (*sc->sc_xcomp->comp_init)
998                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
999                          sc->sc_if.if_unit, sc->sc_flags & SC_DEBUG)) {
1000                     s = splimp();
1001                     sc->sc_flags |= SC_COMP_RUN;
1002                     splx(s);
1003                 }
1004             } else {
1005                 /* peer is agreeing to send compressed packets. */
1006                 if (sc->sc_rc_state != NULL
1007                     && (*sc->sc_rcomp->decomp_init)
1008                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1009                          sc->sc_if.if_unit, 0, sc->sc_mru,
1010                          sc->sc_flags & SC_DEBUG)) {
1011                     s = splimp();
1012                     sc->sc_flags |= SC_DECOMP_RUN;
1013                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1014                     splx(s);
1015                 }
1016             }
1017         }
1018         break;
1019
1020     case CCP_RESETACK:
1021         if (sc->sc_flags & SC_CCP_UP) {
1022             if (!rcvd) {
1023                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1024                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1025             } else {
1026                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1027                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1028                     s = splimp();
1029                     sc->sc_flags &= ~SC_DC_ERROR;
1030                     splx(s);
1031                 }
1032             }
1033         }
1034         break;
1035     }
1036 }
1037
1038 /*
1039  * CCP is down; free (de)compressor state if necessary.
1040  */
1041 static void
1042 ppp_ccp_closed(sc)
1043     struct ppp_softc *sc;
1044 {
1045     if (sc->sc_xc_state) {
1046         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1047         sc->sc_xc_state = NULL;
1048     }
1049     if (sc->sc_rc_state) {
1050         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1051         sc->sc_rc_state = NULL;
1052     }
1053 }
1054 #endif /* PPP_COMPRESS */
1055
1056 /*
1057  * PPP packet input routine.
1058  * The caller has checked and removed the FCS and has inserted
1059  * the address/control bytes and the protocol high byte if they
1060  * were omitted.
1061  */
1062 void
1063 ppppktin(sc, m, lost)
1064     struct ppp_softc *sc;
1065     struct mbuf *m;
1066     int lost;
1067 {
1068     int s = splimp();
1069
1070     m->m_context = lost;
1071     IF_ENQUEUE(&sc->sc_rawq, m);
1072     schednetisr(NETISR_PPP);
1073     splx(s);
1074 }
1075
1076 /*
1077  * Process a received PPP packet, doing decompression as necessary.
1078  */
1079 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1080                          TYPE_UNCOMPRESSED_TCP)
1081
1082 static void
1083 ppp_inproc(sc, m)
1084     struct ppp_softc *sc;
1085     struct mbuf *m;
1086 {
1087     struct ifqueue *inq, *lock;
1088     int s, ilen, xlen, proto, rv;
1089     u_char *cp, adrs, ctrl;
1090     struct mbuf *mp, *dmp, *pc;
1091     u_char *iphdr;
1092     u_int hlen;
1093
1094     sc->sc_if.if_ipackets++;
1095
1096     if (sc->sc_flags & SC_LOG_INPKT) {
1097         printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1098         pppdumpm(m);
1099     }
1100
1101     cp = mtod(m, u_char *);
1102     adrs = PPP_ADDRESS(cp);
1103     ctrl = PPP_CONTROL(cp);
1104     proto = PPP_PROTOCOL(cp);
1105
1106     if (m->m_context) {
1107         s = splimp();
1108         sc->sc_flags |= SC_VJ_RESET;
1109         splx(s);
1110     }
1111
1112 #ifdef PPP_COMPRESS
1113     /*
1114      * Decompress this packet if necessary, update the receiver's
1115      * dictionary, or take appropriate action on a CCP packet.
1116      */
1117     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1118         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1119         /* decompress this packet */
1120         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1121         if (dmp != NULL) {
1122             m_freem(m);
1123             m = dmp;
1124             cp = mtod(m, u_char *);
1125             proto = PPP_PROTOCOL(cp);
1126
1127         } else {
1128             /* pass the compressed packet up to pppd, which may take
1129                CCP down or issue a Reset-Req. */
1130             if (sc->sc_flags & SC_DEBUG)
1131                 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1132             s = splimp();
1133             sc->sc_flags |= SC_VJ_RESET;
1134             switch (rv) {
1135             case DECOMP_OK:
1136                 /* no error, but no decompressed packet produced */
1137                 splx(s);
1138                 m_freem(m);
1139                 return;
1140             case DECOMP_ERROR:
1141                 sc->sc_flags |= SC_DC_ERROR;
1142                 break;
1143             case DECOMP_FATALERROR:
1144                 sc->sc_flags |= SC_DC_FERROR;
1145                 break;
1146             }
1147             splx(s);
1148         }
1149
1150     } else {
1151         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1152             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1153         }
1154         if (proto == PPP_CCP) {
1155             ppp_ccp(sc, m, 1);
1156         }
1157     }
1158 #endif
1159
1160     ilen = 0;
1161     for (mp = m; mp != NULL; mp = mp->m_next)
1162         ilen += mp->m_len;
1163
1164 #ifdef VJC
1165     if (sc->sc_flags & SC_VJ_RESET) {
1166         /*
1167          * If we've missed a packet, we must toss subsequent compressed
1168          * packets which don't have an explicit connection ID.
1169          */
1170         sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1171         s = splimp();
1172         sc->sc_flags &= ~SC_VJ_RESET;
1173         splx(s);
1174     }
1175
1176     /*
1177      * See if we have a VJ-compressed packet to uncompress.
1178      */
1179     if (proto == PPP_VJC_COMP) {
1180         if (sc->sc_flags & SC_REJ_COMP_TCP)
1181             goto bad;
1182
1183         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1184                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1185                                       &sc->sc_comp, &iphdr, &hlen);
1186
1187         if (xlen <= 0) {
1188             if (sc->sc_flags & SC_DEBUG)
1189                 printf("ppp%d: VJ uncompress failed on type comp\n",
1190                         sc->sc_if.if_unit);
1191             goto bad;
1192         }
1193
1194         /* Copy the PPP and IP headers into a new mbuf. */
1195         MGET(mp, M_DONTWAIT, MT_DATA);
1196         if (mp == NULL)
1197             goto bad;
1198         mp->m_len = 0;
1199         mp->m_next = NULL;
1200         if (hlen + PPP_HDRLEN > MLEN) {
1201             MCLGET(mp, pc);
1202             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1203                 m_freem(mp);
1204                 goto bad;       /* lose if big headers and no clusters */
1205             }
1206         }
1207         cp = mtod(mp, u_char *);
1208         cp[0] = adrs;
1209         cp[1] = ctrl;
1210         cp[2] = 0;
1211         cp[3] = PPP_IP;
1212         proto = PPP_IP;
1213         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1214         mp->m_len = hlen + PPP_HDRLEN;
1215
1216         /*
1217          * Trim the PPP and VJ headers off the old mbuf
1218          * and stick the new and old mbufs together.
1219          */
1220         m->m_off += PPP_HDRLEN + xlen;
1221         m->m_len -= PPP_HDRLEN + xlen;
1222         if (m->m_len <= M_TRAILINGSPACE(mp)) {
1223             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1224             mp->m_len += m->m_len;
1225             MFREE(m, mp->m_next);
1226         } else
1227             mp->m_next = m;
1228         m = mp;
1229         ilen += hlen - xlen;
1230
1231     } else if (proto == PPP_VJC_UNCOMP) {
1232         if (sc->sc_flags & SC_REJ_COMP_TCP)
1233             goto bad;
1234
1235         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1236                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1237                                       &sc->sc_comp, &iphdr, &hlen);
1238
1239         if (xlen < 0) {
1240             if (sc->sc_flags & SC_DEBUG)
1241                 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1242                         sc->sc_if.if_unit);
1243             goto bad;
1244         }
1245
1246         proto = PPP_IP;
1247         cp[3] = PPP_IP;
1248     }
1249 #endif /* VJC */
1250
1251     /*
1252      * If the packet will fit in an ordinary mbuf, don't waste a
1253      * whole cluster on it.
1254      */
1255     if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1256         MGET(mp, M_DONTWAIT, MT_DATA);
1257         if (mp != NULL) {
1258             m_copydata(m, mtod(mp, caddr_t), ilen);
1259             m_freem(m);
1260             m = mp;
1261             m->m_len = ilen;
1262         }
1263     }
1264
1265 #if NBPFILTER > 0
1266     /* See if bpf wants to look at the packet. */
1267     if (sc->sc_bpf)
1268         bpf_mtap(sc->sc_bpf, m);
1269 #endif
1270
1271     rv = 0;
1272     switch (proto) {
1273 #ifdef INET
1274     case PPP_IP:
1275         /*
1276          * IP packet - take off the ppp header and pass it up to IP.
1277          */
1278         if ((sc->sc_if.if_flags & IFF_UP) == 0
1279             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1280             /* interface is down - drop the packet. */
1281             m_freem(m);
1282             return;
1283         }
1284         m->m_off += PPP_HDRLEN;
1285         m->m_len -= PPP_HDRLEN;
1286         schednetisr(NETISR_IP);
1287         inq = &ipintrq;
1288         break;
1289 #endif
1290
1291     default:
1292         /*
1293          * Some other protocol - place on input queue for read().
1294          */
1295         inq = &sc->sc_inq;
1296         rv = 1;
1297         break;
1298     }
1299
1300     /*
1301      * Put the packet on the appropriate input queue.
1302      */
1303     s = splimp();
1304     lock = inq;
1305     smp_lock(&lock->lk_ifqueue, LK_RETRY);
1306     if (IF_QFULL(inq)) {
1307         IF_DROP(inq);
1308         /* XXX should we unlock here? */
1309         splx(s);
1310         if (sc->sc_flags & SC_DEBUG)
1311             printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1312         goto bad;
1313     }
1314     IF_ENQUEUEIF(inq, m, &sc->sc_if);
1315     smp_unlock(&lock->lk_ifqueue);
1316     splx(s);
1317
1318     if (rv)
1319         (*sc->sc_ctlp)(sc);
1320
1321     return;
1322
1323  bad:
1324     m_freem(m);
1325     sc->sc_if.if_ierrors++;
1326 }
1327
1328 #define MAX_DUMP_BYTES  128
1329
1330 static void
1331 pppdumpm(m0)
1332     struct mbuf *m0;
1333 {
1334     char buf[3*MAX_DUMP_BYTES+4];
1335     char *bp = buf;
1336     struct mbuf *m;
1337     static char digits[] = "0123456789abcdef";
1338
1339     for (m = m0; m; m = m->m_next) {
1340         int l = m->m_len;
1341         u_char *rptr = mtod(m, u_char *);
1342
1343         while (l--) {
1344             if (bp > buf + sizeof(buf) - 4)
1345                 goto done;
1346             *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1347             *bp++ = digits[*rptr++ & 0xf];
1348         }
1349
1350         if (m->m_next) {
1351             if (bp > buf + sizeof(buf) - 3)
1352                 goto done;
1353             *bp++ = '|';
1354         } else
1355             *bp++ = ' ';
1356     }
1357 done:
1358     if (m)
1359         *bp++ = '>';
1360     *bp = 0;
1361     printf("%s\n", buf);
1362 }
1363
1364 #endif  /* NPPP > 0 */