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