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