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