]> git.ozlabs.org Git - ppp.git/blob - ultrix/if_ppp.c
added hdrlen; define little-endian flag.
[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.4 1994/12/08 00:32:59 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_nextpkt;
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, 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 #ifdef VJC
344     case PPPIOCSMAXCID:
345         if (!suser())
346             return EPERM;
347         s = splnet();
348         sl_compress_setup(&sc->sc_comp, *(int *)data);
349         splx(s);
350         break;
351 #endif
352
353     case PPPIOCXFERUNIT:
354         if (!suser())
355             return EPERM;
356         sc->sc_xfer = p->p_pid;
357         break;
358
359 #ifdef PPP_COMPRESS
360     case PPPIOCSCOMPRESS:
361         if (!suser())
362             return EPERM;
363         odp = (struct ppp_option_data *) data;
364         nb = odp->length;
365         if (nb > sizeof(ccp_option))
366             nb = sizeof(ccp_option);
367         if (error = copyin(odp->ptr, ccp_option, nb))
368             return (error);
369         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
370             return (EINVAL);
371         for (cp = ppp_compressors; *cp != NULL; ++cp)
372             if ((*cp)->compress_proto == ccp_option[0]) {
373                 /*
374                  * Found a handler for the protocol - try to allocate
375                  * a compressor or decompressor.
376                  */
377                 error = 0;
378                 s = splnet();
379                 if (odp->transmit) {
380                     if (sc->sc_xc_state != NULL)
381                         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
382                     sc->sc_xcomp = *cp;
383                     sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
384                     if (sc->sc_xc_state == NULL) {
385                         if (sc->sc_flags & SC_DEBUG)
386                             printf("ppp%d: comp_alloc failed\n",
387                                sc->sc_if.if_unit);
388                         error = ENOBUFS;
389                     }
390                     splimp();
391                     sc->sc_flags &= ~SC_COMP_RUN;
392                 } else {
393                     if (sc->sc_rc_state != NULL)
394                         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
395                     sc->sc_rcomp = *cp;
396                     sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
397                     if (sc->sc_rc_state == NULL) {
398                         if (sc->sc_flags & SC_DEBUG)
399                             printf("ppp%d: decomp_alloc failed\n",
400                                sc->sc_if.if_unit);
401                         error = ENOBUFS;
402                     }
403                     splimp();
404                     sc->sc_flags &= ~SC_DECOMP_RUN;
405                 }
406                 splx(s);
407                 return (error);
408             }
409         if (sc->sc_flags & SC_DEBUG)
410             printf("ppp%d: no compressor for [%x %x %x], %x\n",
411                    sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
412                    ccp_option[2], nb);
413         return (EINVAL);        /* no handler found */
414 #endif /* PPP_COMPRESS */
415
416     case PPPIOCGNPMODE:
417     case PPPIOCSNPMODE:
418         npi = (struct npioctl *) data;
419         switch (npi->protocol) {
420         case PPP_IP:
421             npx = NP_IP;
422             break;
423         default:
424             return EINVAL;
425         }
426         if (cmd == PPPIOCGNPMODE) {
427             npi->mode = sc->sc_npmode[npx];
428         } else {
429             if (!suser())
430                 return EPERM;
431             if (npi->mode != sc->sc_npmode[npx]) {
432                 s = splimp();
433                 sc->sc_npmode[npx] = npi->mode;
434                 if (npi->mode != NPMODE_QUEUE) {
435                     ppp_requeue(sc);
436                     (*sc->sc_start)(sc);
437                 }
438                 splx(s);
439             }
440         }
441         break;
442
443     default:
444         return (-1);
445     }
446     return (0);
447 }
448
449 /*
450  * Process an ioctl request to the ppp network interface.
451  */
452 int
453 pppsioctl(ifp, cmd, data)
454     register struct ifnet *ifp;
455     int cmd;
456     caddr_t data;
457 {
458     struct proc *p = u.u_procp;
459     register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
460     register struct ifaddr *ifa = (struct ifaddr *)data;
461     register struct ifreq *ifr = (struct ifreq *)data;
462     struct ppp_stats *psp;
463     struct ppp_comp_stats *pcp;
464     int s = splimp(), error = 0;
465
466     switch (cmd) {
467     case SIOCSIFFLAGS:
468         if ((ifp->if_flags & IFF_RUNNING) == 0)
469             ifp->if_flags &= ~IFF_UP;
470         break;
471
472     case SIOCSIFADDR:
473         if (ifa->ifa_addr.sa_family != AF_INET)
474             error = EAFNOSUPPORT;
475         break;
476
477     case SIOCSIFDSTADDR:
478         if (ifa->ifa_addr.sa_family != AF_INET)
479             error = EAFNOSUPPORT;
480         break;
481
482     case SIOCSIFMTU:
483         if (!suser())
484             return EPERM;
485         sc->sc_if.if_mtu = ifr->ifr_mtu;
486         break;
487
488     case SIOCGIFMTU:
489         ifr->ifr_mtu = sc->sc_if.if_mtu;
490         break;
491
492     case SIOCGPPPSTATS:
493         psp = &((struct ifpppstatsreq *) data)->stats;
494         bzero(psp, sizeof(*psp));
495         psp->p.ppp_ibytes = sc->sc_bytesrcvd;
496         psp->p.ppp_ipackets = ifp->if_ipackets;
497         psp->p.ppp_ierrors = ifp->if_ierrors;
498         psp->p.ppp_obytes = sc->sc_bytessent;
499         psp->p.ppp_opackets = ifp->if_opackets;
500         psp->p.ppp_oerrors = ifp->if_oerrors;
501 #ifdef VJC
502         psp->vj.vjs_packets = sc->sc_comp.sls_packets;
503         psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
504         psp->vj.vjs_searches = sc->sc_comp.sls_searches;
505         psp->vj.vjs_misses = sc->sc_comp.sls_misses;
506         psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
507         psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
508         psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
509         psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
510 #endif /* VJC */
511         break;
512
513 #ifdef PPP_COMPRESS
514     case SIOCGPPPCSTATS:
515         pcp = &((struct ifpppcstatsreq *) data)->stats;
516         bzero(pcp, sizeof(*pcp));
517         if (sc->sc_xc_state != NULL)
518             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
519         if (sc->sc_rc_state != NULL)
520             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
521         break;
522 #endif /* PPP_COMPRESS */
523
524     default:
525         error = EINVAL;
526     }
527     splx(s);
528     return (error);
529 }
530
531 /*
532  * Queue a packet.  Start transmission if not active.
533  * Packet is placed in Information field of PPP frame.
534  */
535 int
536 pppoutput(ifp, m0, dst)
537     struct ifnet *ifp;
538     struct mbuf *m0;
539     struct sockaddr *dst;
540 {
541     register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
542     struct ppp_header *ph;
543     int protocol, address, control;
544     u_char *cp;
545     int s, error;
546     struct ip *ip;
547     struct ifqueue *ifq;
548     enum NPmode mode;
549
550     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
551         || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
552         error = ENETDOWN;       /* sort of */
553         goto bad;
554     }
555
556     /*
557      * Compute PPP header.
558      * We use the m_context field of the mbuf to indicate whether
559      * the packet should go on the fast queue.
560      */
561     m0->m_context = 0;
562     switch (dst->sa_family) {
563 #ifdef INET
564     case AF_INET:
565         address = PPP_ALLSTATIONS;
566         control = PPP_UI;
567         protocol = PPP_IP;
568         mode = sc->sc_npmode[NP_IP];
569         
570         /*
571          * If this is a TCP packet to or from an "interactive" port,
572          * put the packet on the fastq instead.
573          */
574         if ((ip = mtod(m0, struct ip *))->ip_p == IPPROTO_TCP) {
575             register int p = ntohl(((int *)ip)[ip->ip_hl]);
576             if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
577                 m0->m_context = 1;
578         }
579         break;
580 #endif
581     case AF_UNSPEC:
582         address = PPP_ADDRESS(dst->sa_data);
583         control = PPP_CONTROL(dst->sa_data);
584         protocol = PPP_PROTOCOL(dst->sa_data);
585         mode = NPMODE_PASS;
586         break;
587     default:
588         printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
589         error = EAFNOSUPPORT;
590         goto bad;
591     }
592
593     /*
594      * Drop this packet, or return an error, if necessary.
595      */
596     if (mode == NPMODE_ERROR) {
597         error = ENETDOWN;
598         goto bad;
599     }
600     if (mode == NPMODE_DROP) {
601         error = 0;
602         goto bad;
603     }
604
605     /*
606      * Add PPP header.  If no space in first mbuf, allocate another.
607      */
608     if (M_IS_CLUSTER(m0) || m0->m_off < MMINOFF + PPP_HDRLEN) {
609         struct mbuf *m;
610
611         MGET(m, M_DONTWAIT, MT_DATA);
612         if (m == NULL) {
613             m_freem(m0);
614             return (ENOBUFS);
615         }
616         m->m_len = 0;
617         m->m_next = m0;
618         m0 = m;
619     } else
620         m0->m_off -= PPP_HDRLEN;
621
622     cp = mtod(m0, u_char *);
623     *cp++ = address;
624     *cp++ = control;
625     *cp++ = protocol >> 8;
626     *cp++ = protocol & 0xff;
627     m0->m_len += PPP_HDRLEN;
628
629     if (sc->sc_flags & SC_LOG_OUTPKT) {
630         printf("ppp%d output: ", ifp->if_unit);
631         pppdumpm(m0);
632     }
633
634 #if NBPFILTER > 0
635     /*
636      * See if bpf wants to look at the packet.
637      */
638     if (sc->sc_bpf)
639         bpf_mtap(sc->sc_bpf, m0);
640 #endif
641
642     /*
643      * Put the packet on the appropriate queue.
644      */
645     s = splimp();               /* splnet should be OK now */
646     if (mode == NPMODE_QUEUE) {
647         /* XXX we should limit the number of packets on this queue */
648         *sc->sc_npqtail = m0;
649         m0->m_nextpkt = NULL;
650         sc->sc_npqtail = &m0->m_nextpkt;
651     } else {
652         ifq = m0->m_context? &sc->sc_fastq: &ifp->if_snd;
653         if (IF_QFULL(ifq)) {
654             IF_DROP(ifq);
655             splx(s);
656             sc->sc_if.if_oerrors++;
657             error = ENOBUFS;
658             goto bad;
659         }
660         IF_ENQUEUE(ifq, m0);
661         (*sc->sc_start)(sc);
662     }
663
664     splx(s);
665     return (0);
666
667 bad:
668     m_freem(m0);
669     return (error);
670 }
671
672 /*
673  * After a change in the NPmode for some NP, move packets from the
674  * npqueue to the send queue or the fast queue as appropriate.
675  * Should be called at splimp (actually splnet would probably suffice).
676  */
677 static void
678 ppp_requeue(sc)
679     struct ppp_softc *sc;
680 {
681     struct mbuf *m, **mpp;
682     struct ifqueue *ifq;
683     enum NPmode mode;
684
685     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
686         switch (PPP_PROTOCOL(mtod(m, u_char *))) {
687         case PPP_IP:
688             mode = sc->sc_npmode[NP_IP];
689             break;
690         default:
691             mode = NPMODE_PASS;
692         }
693
694         switch (mode) {
695         case NPMODE_PASS:
696             /*
697              * This packet can now go on one of the queues to be sent.
698              */
699             *mpp = m->m_nextpkt;
700             m->m_nextpkt = NULL;
701             ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
702             if (IF_QFULL(ifq)) {
703                 IF_DROP(ifq);
704                 sc->sc_if.if_oerrors++;
705             } else
706                 IF_ENQUEUE(ifq, m);
707             break;
708
709         case NPMODE_DROP:
710         case NPMODE_ERROR:
711             *mpp = m->m_nextpkt;
712             m_freem(m);
713             break;
714
715         case NPMODE_QUEUE:
716             mpp = &m->m_nextpkt;
717             break;
718         }
719     }
720     sc->sc_npqtail = mpp;
721 }
722
723 /*
724  * Get a packet to send.  This procedure is intended to be called
725  * at spltty()/splimp(), so it takes little time.  If there isn't
726  * a packet waiting to go out, it schedules a software interrupt
727  * to prepare a new packet; the device start routine gets called
728  * again when a packet is ready.
729  */
730 struct mbuf *
731 ppp_dequeue(sc)
732     struct ppp_softc *sc;
733 {
734     struct mbuf *m;
735     int s = splimp();
736
737     m = sc->sc_togo;
738     if (m) {
739         /*
740          * Had a packet waiting - send it.
741          */
742         sc->sc_togo = NULL;
743         sc->sc_flags |= SC_TBUSY;
744         splx(s);
745         return m;
746     }
747     /*
748      * Remember we wanted a packet and schedule a software interrupt.
749      */
750     sc->sc_flags &= ~SC_TBUSY;
751     schednetisr(NETISR_PPP);
752     splx(s);
753     return NULL;
754 }
755
756 /*
757  * Software interrupt routine, called at splnet().
758  */
759 void
760 pppintr()
761 {
762     struct ppp_softc *sc;
763     int i, s;
764     struct mbuf *m;
765
766     s = splnet();
767     sc = ppp_softc;
768     for (i = 0; i < NPPP; ++i, ++sc) {
769         if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
770             && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
771             ppp_outpkt(sc);
772         for (;;) {
773             IF_DEQUEUE(&sc->sc_rawq, m);
774             if (m == NULL)
775                 break;
776             ppp_inproc(sc, m);
777         }
778     }
779     splx(s);
780 }
781
782 /*
783  * Grab another packet off a queue and apply VJ compression,
784  * packet compression, address/control and/or protocol compression
785  * if enabled.  Should be called at splnet.
786  */
787 static void
788 ppp_outpkt(sc)
789     struct ppp_softc *sc;
790 {
791     int s;
792     struct mbuf *m, *mp;
793     u_char *cp;
794     int address, control, protocol;
795     enum NPmode mode;
796
797     /*
798      * Grab a packet to send: first try the fast queue, then the
799      * normal queue.
800      */
801     IF_DEQUEUE(&sc->sc_fastq, m);
802     if (m == NULL)
803         IF_DEQUEUE(&sc->sc_if.if_snd, m);
804     if (m == NULL)
805         return;
806
807     /*
808      * Extract the ppp header of the new packet.
809      * The ppp header will be in one mbuf.
810      */
811     cp = mtod(m, u_char *);
812     address = PPP_ADDRESS(cp);
813     control = PPP_CONTROL(cp);
814     protocol = PPP_PROTOCOL(cp);
815
816     switch (protocol) {
817 #ifdef VJC
818     case PPP_IP:
819         /*
820          * If the packet is a TCP/IP packet, see if we can compress it.
821          */
822         if (sc->sc_flags & SC_COMP_TCP) {
823             struct ip *ip;
824             int type;
825
826             mp = m;
827             ip = (struct ip *) (cp + PPP_HDRLEN);
828             if (mp->m_len <= PPP_HDRLEN) {
829                 mp = mp->m_next;
830                 if (mp == NULL)
831                     break;
832                 ip = mtod(mp, struct ip *);
833             }
834             /* this code assumes the IP/TCP header is in one non-shared mbuf */
835             if (ip->ip_p == IPPROTO_TCP) {
836                 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
837                                        !(sc->sc_flags & SC_NO_TCP_CCID));
838                 switch (type) {
839                 case TYPE_UNCOMPRESSED_TCP:
840                     protocol = PPP_VJC_UNCOMP;
841                     break;
842                 case TYPE_COMPRESSED_TCP:
843                     protocol = PPP_VJC_COMP;
844                     cp = mtod(m, u_char *);
845                     cp[0] = address;    /* header has moved */
846                     cp[1] = control;
847                     cp[2] = 0;
848                     break;
849                 }
850                 cp[3] = protocol;       /* update protocol in PPP header */
851             }
852         }
853         break;
854 #endif  /* VJC */
855
856 #ifdef PPP_COMPRESS
857     case PPP_CCP:
858         ppp_ccp(sc, m, 0);
859         break;
860 #endif  /* PPP_COMPRESS */
861     }
862
863 #ifdef PPP_COMPRESS
864     if (protocol != PPP_LCP && protocol != PPP_CCP
865         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
866         struct mbuf *mcomp;
867         int slen, clen;
868
869         slen = 0;
870         for (mp = m; mp != NULL; mp = mp->m_next)
871             slen += mp->m_len;
872         clen = (*sc->sc_xcomp->compress)
873             (sc->sc_xc_state, &mcomp, m, slen,
874              (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
875         if (mcomp != NULL) {
876             m_freem(m);
877             m = mcomp;
878             cp = mtod(m, u_char *);
879             protocol = cp[3];
880         }
881     }
882 #endif  /* PPP_COMPRESS */
883
884     /*
885      * Compress the address/control and protocol, if possible.
886      */
887     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
888         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
889         protocol != PPP_LCP) {
890         /* can compress address/control */
891         m->m_off += 2;
892         m->m_len -= 2;
893     }
894     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
895         /* can compress protocol */
896         if (mtod(m, u_char *) == cp) {
897             cp[2] = cp[1];      /* move address/control up */
898             cp[1] = cp[0];
899         }
900         ++m->m_off;
901         --m->m_len;
902     }
903
904     s = splimp();
905     sc->sc_togo = m;
906     (*sc->sc_start)(sc);
907     splx(s);
908 }
909
910 #ifdef PPP_COMPRESS
911 /*
912  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
913  * 0 if it is about to be transmitted.
914  */
915 static int
916 ppp_ccp(sc, m, rcvd)
917     struct ppp_softc *sc;
918     struct mbuf *m;
919     int rcvd;
920 {
921     u_char *dp, *ep;
922     struct mbuf *mp;
923     int slen, s;
924     struct bsd_db *db;
925
926     /*
927      * Get a pointer to the data after the PPP header.
928      */
929     if (m->m_len <= PPP_HDRLEN) {
930         mp = m->m_next;
931         if (mp == NULL)
932             return;
933         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
934     } else {
935         mp = m;
936         dp = mtod(mp, u_char *) + PPP_HDRLEN;
937     }
938
939     ep = mtod(mp, u_char *) + mp->m_len;
940     if (dp + CCP_HDRLEN > ep)
941         return;
942     slen = CCP_LENGTH(dp);
943     if (dp + slen > ep) {
944         if (sc->sc_flags & SC_DEBUG)
945             printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
946                    dp, slen, mtod(mp, u_char *), mp->m_len);
947         return;
948     }
949
950     switch (CCP_CODE(dp)) {
951     case CCP_CONFREQ:
952     case CCP_TERMREQ:
953     case CCP_TERMACK:
954         /* CCP must be going down - disable compression */
955         if (sc->sc_flags & SC_CCP_UP) {
956             s = splimp();
957             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
958             splx(s);
959         }
960         break;
961
962     case CCP_CONFACK:
963         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
964             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
965             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
966             if (!rcvd) {
967                 /* we're agreeing to send compressed packets. */
968                 if (sc->sc_xc_state != NULL
969                     && (*sc->sc_xcomp->comp_init)
970                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
971                          sc->sc_if.if_unit, sc->sc_flags & SC_DEBUG)) {
972                     s = splimp();
973                     sc->sc_flags |= SC_COMP_RUN;
974                     splx(s);
975                 }
976             } else {
977                 /* peer is agreeing to send compressed packets. */
978                 if (sc->sc_rc_state != NULL
979                     && (*sc->sc_rcomp->decomp_init)
980                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
981                          sc->sc_if.if_unit, 0, sc->sc_mru,
982                          sc->sc_flags & SC_DEBUG)) {
983                     s = splimp();
984                     sc->sc_flags |= SC_DECOMP_RUN;
985                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
986                     splx(s);
987                 }
988             }
989         }
990         break;
991
992     case CCP_RESETACK:
993         if (sc->sc_flags & SC_CCP_UP) {
994             if (!rcvd) {
995                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
996                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
997             } else {
998                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
999                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1000                     s = splimp();
1001                     sc->sc_flags &= ~SC_DC_ERROR;
1002                     splx(s);
1003                 }
1004             }
1005         }
1006         break;
1007     }
1008 }
1009
1010 /*
1011  * CCP is down; free (de)compressor state if necessary.
1012  */
1013 static void
1014 ppp_ccp_closed(sc)
1015     struct ppp_softc *sc;
1016 {
1017     if (sc->sc_xc_state) {
1018         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1019         sc->sc_xc_state = NULL;
1020     }
1021     if (sc->sc_rc_state) {
1022         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1023         sc->sc_rc_state = NULL;
1024     }
1025 }
1026 #endif /* PPP_COMPRESS */
1027
1028 /*
1029  * PPP packet input routine.
1030  * The caller has checked and removed the FCS and has inserted
1031  * the address/control bytes and the protocol high byte if they
1032  * were omitted.
1033  */
1034 void
1035 ppppktin(sc, m, lost)
1036     struct ppp_softc *sc;
1037     struct mbuf *m;
1038     int lost;
1039 {
1040     int s = splimp();
1041
1042     m->m_context = lost;
1043     IF_ENQUEUE(&sc->sc_rawq, m);
1044     schednetisr(NETISR_PPP);
1045     splx(s);
1046 }
1047
1048 /*
1049  * Process a received PPP packet, doing decompression as necessary.
1050  */
1051 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1052                          TYPE_UNCOMPRESSED_TCP)
1053
1054 static void
1055 ppp_inproc(sc, m)
1056     struct ppp_softc *sc;
1057     struct mbuf *m;
1058 {
1059     struct ifqueue *inq;
1060     int s, ilen, xlen, proto, rv;
1061     u_char *cp, adrs, ctrl;
1062     struct mbuf *mp, *dmp, *pc;
1063     u_char *iphdr;
1064     u_int hlen;
1065
1066     sc->sc_if.if_ipackets++;
1067
1068     if (sc->sc_flags & SC_LOG_INPKT) {
1069         printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1070         pppdumpm(m);
1071     }
1072
1073     cp = mtod(m, u_char *);
1074     adrs = PPP_ADDRESS(cp);
1075     ctrl = PPP_CONTROL(cp);
1076     proto = PPP_PROTOCOL(cp);
1077
1078     if (m->m_context) {
1079         s = splimp();
1080         sc->sc_flags |= SC_VJ_RESET;
1081         splx(s);
1082     }
1083
1084 #ifdef PPP_COMPRESS
1085     /*
1086      * Decompress this packet if necessary, update the receiver's
1087      * dictionary, or take appropriate action on a CCP packet.
1088      */
1089     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1090         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1091         /* decompress this packet */
1092         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1093         if (dmp != NULL) {
1094             m_freem(m);
1095             m = dmp;
1096             cp = mtod(m, u_char *);
1097             proto = PPP_PROTOCOL(cp);
1098
1099         } else {
1100             /* pass the compressed packet up to pppd, which may take
1101                CCP down or issue a Reset-Req. */
1102             if (sc->sc_flags & SC_DEBUG)
1103                 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1104             s = splimp();
1105             sc->sc_flags |= SC_VJ_RESET;
1106             switch (rv) {
1107             case DECOMP_OK:
1108                 /* no error, but no decompressed packet produced */
1109                 splx(s);
1110                 m_freem(m);
1111                 return;
1112             case DECOMP_ERROR:
1113                 sc->sc_flags |= SC_DC_ERROR;
1114                 break;
1115             case DECOMP_FATALERROR:
1116                 sc->sc_flags |= SC_DC_FERROR;
1117                 break;
1118             }
1119             splx(s);
1120         }
1121
1122     } else {
1123         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1124             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1125         }
1126         if (proto == PPP_CCP) {
1127             ppp_ccp(sc, m, 1);
1128         }
1129     }
1130 #endif
1131
1132     ilen = 0;
1133     for (mp = m; mp != NULL; mp = mp->m_next)
1134         ilen += mp->m_len;
1135
1136 #ifdef VJC
1137     if (sc->sc_flags & SC_VJ_RESET) {
1138         /*
1139          * If we've missed a packet, we must toss subsequent compressed
1140          * packets which don't have an explicit connection ID.
1141          */
1142         sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1143         s = splimp();
1144         sc->sc_flags &= ~SC_VJ_RESET;
1145         splx(s);
1146     }
1147
1148     /*
1149      * See if we have a VJ-compressed packet to uncompress.
1150      */
1151     if (proto == PPP_VJC_COMP) {
1152         if (sc->sc_flags & SC_REJ_COMP_TCP)
1153             goto bad;
1154
1155         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1156                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1157                                       &sc->sc_comp, &iphdr, &hlen);
1158
1159         if (xlen <= 0) {
1160             if (sc->sc_flags & SC_DEBUG)
1161                 printf("ppp%d: VJ uncompress failed on type comp\n",
1162                         sc->sc_if.if_unit);
1163             goto bad;
1164         }
1165
1166         /* Copy the PPP and IP headers into a new mbuf. */
1167         MGET(mp, M_DONTWAIT, MT_DATA);
1168         if (mp == NULL)
1169             goto bad;
1170         mp->m_len = 0;
1171         mp->m_next = NULL;
1172         if (hlen + PPP_HDRLEN > MLEN) {
1173             MCLGET(mp, pc);
1174             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1175                 m_freem(mp);
1176                 goto bad;       /* lose if big headers and no clusters */
1177             }
1178         }
1179         cp = mtod(mp, u_char *);
1180         cp[0] = adrs;
1181         cp[1] = ctrl;
1182         cp[2] = 0;
1183         cp[3] = PPP_IP;
1184         proto = PPP_IP;
1185         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1186         mp->m_len = hlen + PPP_HDRLEN;
1187
1188         /*
1189          * Trim the PPP and VJ headers off the old mbuf
1190          * and stick the new and old mbufs together.
1191          */
1192         m->m_off += PPP_HDRLEN + xlen;
1193         m->m_len -= PPP_HDRLEN + xlen;
1194         if (m->m_len <= M_TRAILINGSPACE(mp)) {
1195             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1196             mp->m_len += m->m_len;
1197             MFREE(m, mp->m_next);
1198         } else
1199             mp->m_next = m;
1200         m = mp;
1201         ilen += hlen - xlen;
1202
1203     } else if (proto == PPP_VJC_UNCOMP) {
1204         if (sc->sc_flags & SC_REJ_COMP_TCP)
1205             goto bad;
1206
1207         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1208                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1209                                       &sc->sc_comp, &iphdr, &hlen);
1210
1211         if (xlen < 0) {
1212             if (sc->sc_flags & SC_DEBUG)
1213                 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1214                         sc->sc_if.if_unit);
1215             goto bad;
1216         }
1217
1218         proto = PPP_IP;
1219         cp[3] = PPP_IP;
1220     }
1221 #endif /* VJC */
1222
1223     /*
1224      * If the packet will fit in an ordinary mbuf, don't waste a
1225      * whole cluster on it.
1226      */
1227     if (ilen <= MLEN && M_IS_CLUSTER(m)) {
1228         MGET(mp, M_DONTWAIT, MT_DATA);
1229         if (mp != NULL) {
1230             m_copydata(m, mtod(mp, caddr_t), ilen);
1231             m_freem(m);
1232             m = mp;
1233             m->m_len = ilen;
1234         }
1235     }
1236
1237 #if NBPFILTER > 0
1238     /* See if bpf wants to look at the packet. */
1239     if (sc->sc_bpf)
1240         bpf_mtap(sc->sc_bpf, m);
1241 #endif
1242
1243     rv = 0;
1244     switch (proto) {
1245 #ifdef INET
1246     case PPP_IP:
1247         /*
1248          * IP packet - take off the ppp header and pass it up to IP.
1249          */
1250         if ((sc->sc_if.if_flags & IFF_UP) == 0
1251             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1252             /* interface is down - drop the packet. */
1253             m_freem(m);
1254             return;
1255         }
1256         m->m_off += PPP_HDRLEN;
1257         m->m_len -= PPP_HDRLEN;
1258         schednetisr(NETISR_IP);
1259         inq = &ipintrq;
1260         break;
1261 #endif
1262
1263     default:
1264         /*
1265          * Some other protocol - place on input queue for read().
1266          */
1267         inq = &sc->sc_inq;
1268         rv = 1;
1269         break;
1270     }
1271
1272     /*
1273      * Put the packet on the appropriate input queue.
1274      */
1275     s = splimp();
1276     lock = inq;
1277     smp_lock(&lock->lk_ifqueue, LK_RETRY);
1278     if (IF_QFULL(inq)) {
1279         IF_DROP(inq);
1280         /* XXX should we unlock here? */
1281         splx(s);
1282         if (sc->sc_flags & SC_DEBUG)
1283             printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1284         goto bad;
1285     }
1286     IF_ENQUEUEIF(inq, m, &sc->sc_if);
1287     smp_unlock(&lock->lk_ifqueue);
1288     splx(s);
1289
1290     if (rv)
1291         (*sc->sc_ctlp)(sc);
1292
1293     return;
1294
1295  bad:
1296     m_freem(m);
1297     sc->sc_if.if_ierrors++;
1298 }
1299
1300 #define MAX_DUMP_BYTES  128
1301
1302 static void
1303 pppdumpm(m0)
1304     struct mbuf *m0;
1305 {
1306     char buf[3*MAX_DUMP_BYTES+4];
1307     char *bp = buf;
1308     struct mbuf *m;
1309     static char digits[] = "0123456789abcdef";
1310
1311     for (m = m0; m; m = m->m_next) {
1312         int l = m->m_len;
1313         u_char *rptr = mtod(m, u_char *);
1314
1315         while (l--) {
1316             if (bp > buf + sizeof(buf) - 4)
1317                 goto done;
1318             *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1319             *bp++ = digits[*rptr++ & 0xf];
1320         }
1321
1322         if (m->m_next) {
1323             if (bp > buf + sizeof(buf) - 3)
1324                 goto done;
1325             *bp++ = '|';
1326         } else
1327             *bp++ = ' ';
1328     }
1329 done:
1330     if (m)
1331         *bp++ = '>';
1332     *bp = 0;
1333     printf("%s\n", buf);
1334 }
1335
1336 #endif  /* NPPP > 0 */