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