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