M_NOWAIT -> M_WAITOK or M_INTWAIT conversions. There is a whole lot of net
[dragonfly.git] / sys / net / sl / if_sl.c
1 /*
2  * Copyright (c) 1987, 1989, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)if_sl.c     8.6 (Berkeley) 2/1/94
34  * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $
35  * $DragonFly: src/sys/net/sl/if_sl.c,v 1.12 2004/04/22 04:22:06 dillon Exp $
36  */
37
38 /*
39  * Serial Line interface
40  *
41  * Rick Adams
42  * Center for Seismic Studies
43  * 1300 N 17th Street, Suite 1450
44  * Arlington, Virginia 22209
45  * (703)276-7900
46  * rick@seismo.ARPA
47  * seismo!rick
48  *
49  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
50  * N.B.: this belongs in netinet, not net, the way it stands now.
51  * Should have a link-layer type designation, but wouldn't be
52  * backwards-compatible.
53  *
54  * Converted to 4.3BSD Beta by Chris Torek.
55  * Other changes made at Berkeley, based in part on code by Kirk Smith.
56  * W. Jolitz added slip abort.
57  *
58  * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
59  * Added priority queuing for "interactive" traffic; hooks for TCP
60  * header compression; ICMP filtering (at 2400 baud, some cretin
61  * pinging you can use up all your bandwidth).  Made low clist behavior
62  * more robust and slightly less likely to hang serial line.
63  * Sped up a bunch of things.
64  *
65  * Note that splimp() is used throughout to block both (tty) input
66  * interrupts and network activity; thus, splimp must be >= spltty.
67  */
68
69 #include "use_sl.h"
70
71 #include "opt_inet.h"
72 #if !defined(KLD_MODULE)
73 #include "opt_slip.h"
74 #endif
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/proc.h>
78 #include <sys/malloc.h>
79 #include <sys/mbuf.h>
80 #include <sys/dkstat.h>
81 #include <sys/socket.h>
82 #include <sys/sockio.h>
83 #include <sys/fcntl.h>
84 #include <sys/signalvar.h>
85 #include <sys/tty.h>
86 #include <sys/clist.h>
87 #include <sys/kernel.h>
88 #include <sys/conf.h>
89
90 #include <net/if.h>
91 #include <net/if_types.h>
92 #include <net/netisr.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 #else
100 #error "Huh? Slip without inet?"
101 #endif
102
103 #include <net/slcompress.h>
104 #include "if_slvar.h"
105 #include <net/slip.h>
106
107 #include <net/bpf.h>
108
109 #ifdef __i386__
110 #include <i386/isa/intr_machdep.h>
111 #endif
112
113 static void slattach (void *);
114 PSEUDO_SET(slattach, if_sl);
115
116 /*
117  * SLRMAX is a hard limit on input packet size.  To simplify the code
118  * and improve performance, we require that packets fit in an mbuf
119  * cluster, and if we get a compressed packet, there's enough extra
120  * room to expand the header into a max length tcp/ip header (128
121  * bytes).  So, SLRMAX can be at most
122  *      MCLBYTES - 128
123  *
124  * SLMTU is the default transmit MTU. The transmit MTU should be kept
125  * small enough so that interactive use doesn't suffer, but large
126  * enough to provide good performance. 552 is a good choice for SLMTU
127  * because it is high enough to not fragment TCP packets being routed
128  * through this host. Packet fragmentation is bad with SLIP because
129  * fragment headers aren't compressed. The previous assumptions about
130  * the best MTU value don't really hold when using modern modems with
131  * BTLZ data compression because the modem buffers play a much larger
132  * role in interactive performance than the MTU. The MTU can be changed
133  * at any time to suit the specific environment with ifconfig(8), and
134  * its maximum value is defined as SLTMAX. SLTMAX must not be so large
135  * that it would overflow the stack if BPF is configured (XXX; if_ppp.c
136  * handles this better).
137  *
138  * SLIP_HIWAT is the amount of data that will be queued 'downstream'
139  * of us (i.e., in clists waiting to be picked up by the tty output
140  * interrupt).  If we queue a lot of data downstream, it's immune to
141  * our t.o.s. queuing.
142  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
143  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
144  * wait is dependent on the ftp window size but that's typically
145  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
146  * the cost (in idle time on the wire) of the tty driver running
147  * off the end of its clists & having to call back slstart for a
148  * new packet.  For a tty interface with any buffering at all, this
149  * cost will be zero.  Even with a totally brain dead interface (like
150  * the one on a typical workstation), the cost will be <= 1 character
151  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
152  * at most 1% while maintaining good interactive response.
153  */
154 #define BUFOFFSET       (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
155 #define SLRMAX          (MCLBYTES - BUFOFFSET)
156 #define SLBUFSIZE       (SLRMAX + BUFOFFSET)
157 #ifndef SLMTU
158 #define SLMTU           552             /* default MTU */
159 #endif
160 #define SLTMAX          1500            /* maximum MTU */
161 #define SLIP_HIWAT      roundup(50,CBSIZE)
162 #define CLISTRESERVE    1024            /* Can't let clists get too low */
163
164 /*
165  * SLIP ABORT ESCAPE MECHANISM:
166  *      (inspired by HAYES modem escape arrangement)
167  *      1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
168  *      within window time signals a "soft" exit from slip mode by remote end
169  *      if the IFF_DEBUG flag is on.
170  */
171 #define ABT_ESC         '\033'  /* can't be t_intr - distant host must know it*/
172 #define ABT_IDLE        1       /* in seconds - idle before an escape */
173 #define ABT_COUNT       3       /* count of escapes for abort */
174 #define ABT_WINDOW      (ABT_COUNT*2+2) /* in seconds - time to count */
175
176 static struct sl_softc sl_softc[NSL];
177
178 #define FRAME_END               0xc0            /* Frame End */
179 #define FRAME_ESCAPE            0xdb            /* Frame Esc */
180 #define TRANS_FRAME_END         0xdc            /* transposed frame end */
181 #define TRANS_FRAME_ESCAPE      0xdd            /* transposed frame esc */
182
183 static int slinit (struct sl_softc *);
184 static struct mbuf *sl_btom (struct sl_softc *, int);
185 static timeout_t sl_keepalive;
186 static timeout_t sl_outfill;
187 static int      slclose (struct tty *,int);
188 static int      slinput (int, struct tty *);
189 static int      slioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
190 static int      sltioctl (struct tty *, u_long, caddr_t, int, struct thread *);
191 static int      slopen (dev_t, struct tty *);
192 static int      sloutput (struct ifnet *,
193             struct mbuf *, struct sockaddr *, struct rtentry *);
194 static int      slstart (struct tty *);
195
196 static struct linesw slipdisc = {
197         slopen,         slclose,        l_noread,       l_nowrite,
198         sltioctl,       slinput,        slstart,        ttymodem,
199         FRAME_END
200 };
201
202 /*
203  * Called from boot code to establish sl interfaces.
204  */
205 static void
206 slattach(dummy)
207         void *dummy;
208 {
209         struct sl_softc *sc;
210         int i = 0;
211
212         linesw[SLIPDISC] = slipdisc;
213
214         for (sc = sl_softc; i < NSL; sc++) {
215                 if_initname(&(sc->sc_if), "sl", i++);
216                 sc->sc_if.if_mtu = SLMTU;
217                 sc->sc_if.if_flags =
218 #ifdef SLIP_IFF_OPTS
219                     SLIP_IFF_OPTS;
220 #else
221                     IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
222 #endif
223                 sc->sc_if.if_type = IFT_SLIP;
224                 sc->sc_if.if_ioctl = slioctl;
225                 sc->sc_if.if_output = sloutput;
226                 sc->sc_if.if_snd.ifq_maxlen = 50;
227                 sc->sc_fastq.ifq_maxlen = 32;
228                 sc->sc_if.if_linkmib = sc;
229                 sc->sc_if.if_linkmiblen = sizeof *sc;
230                 if_attach(&sc->sc_if);
231                 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
232         }
233 }
234
235 static int
236 slinit(sc)
237         struct sl_softc *sc;
238 {
239         caddr_t p;
240
241 #ifdef __i386__
242         int s;
243
244         s = splhigh();
245         tty_imask |= net_imask;
246         net_imask = tty_imask;
247         update_intr_masks();
248         splx(s);
249         if (bootverbose)
250                 printf("new imasks: bio %x, tty %x, net %x\n",
251                     bio_imask, tty_imask, net_imask);
252 #endif
253         if (sc->sc_ep == (u_char *) 0) {
254                 MCLALLOC(p, M_WAIT);
255                 if (p)
256                         sc->sc_ep = (u_char *)p + SLBUFSIZE;
257                 else {
258                         printf("sl%ld: can't allocate buffer\n",
259                             (long)(sc - sl_softc));
260                         return (0);
261                 }
262         }
263         sc->sc_buf = sc->sc_ep - SLRMAX;
264         sc->sc_mp = sc->sc_buf;
265         sl_compress_init(&sc->sc_comp, -1);
266         return (1);
267 }
268
269 /*
270  * Line specific open routine.
271  * Attach the given tty to the first available sl unit.
272  */
273 /* ARGSUSED */
274 static int
275 slopen(dev_t dev, struct tty *tp)
276 {
277         struct sl_softc *sc;
278         int nsl;
279         int s, error;
280         struct thread *td = curthread;  /* XXX */
281
282         error = suser(td);
283         if (error)
284                 return (error);
285
286         if (tp->t_line == SLIPDISC)
287                 return (0);
288
289         for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
290                 if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) {
291                         if (slinit(sc) == 0)
292                                 return (ENOBUFS);
293                         tp->t_sc = (caddr_t)sc;
294                         sc->sc_ttyp = tp;
295                         sc->sc_if.if_baudrate = tp->t_ospeed;
296                         ttyflush(tp, FREAD | FWRITE);
297
298                         tp->t_line = SLIPDISC;
299                         /*
300                          * We don't use t_canq or t_rawq, so reduce their
301                          * cblock resources to 0.  Reserve enough cblocks
302                          * for t_outq to guarantee that we can fit a full
303                          * packet if the SLIP_HIWAT check allows slstart()
304                          * to loop.  Use the same value for the cblock
305                          * limit since the reserved blocks should always
306                          * be enough.  Reserving cblocks probably makes
307                          * the CLISTRESERVE check unnecessary and wasteful.
308                          */
309                         clist_alloc_cblocks(&tp->t_canq, 0, 0);
310                         clist_alloc_cblocks(&tp->t_outq,
311                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
312                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
313                         clist_alloc_cblocks(&tp->t_rawq, 0, 0);
314
315                         s = splnet();
316                         if_up(&sc->sc_if);
317                         splx(s);
318                         return (0);
319                 }
320         return (ENXIO);
321 }
322
323 /*
324  * Line specific close routine.
325  * Detach the tty from the sl unit.
326  */
327 static int
328 slclose(tp,flag)
329         struct tty *tp;
330         int flag;
331 {
332         struct sl_softc *sc;
333         int s;
334
335         ttyflush(tp, FREAD | FWRITE);
336         /*
337          * XXX the placement of the following spl is misleading.  tty
338          * interrupts must be blocked across line discipline switches
339          * and throughout closes to avoid races.
340          */
341         s = splimp();           /* actually, max(spltty, splnet) */
342         clist_free_cblocks(&tp->t_outq);
343         tp->t_line = 0;
344         sc = (struct sl_softc *)tp->t_sc;
345         if (sc != NULL) {
346                 if (sc->sc_outfill) {
347                         sc->sc_outfill = 0;
348                         untimeout(sl_outfill, sc, sc->sc_ofhandle);
349                 }
350                 if (sc->sc_keepalive) {
351                         sc->sc_keepalive = 0;
352                         untimeout(sl_keepalive, sc, sc->sc_kahandle);
353                 }
354                 if_down(&sc->sc_if);
355                 sc->sc_flags &= SC_STATIC;
356                 sc->sc_ttyp = NULL;
357                 tp->t_sc = NULL;
358                 m_mclfree((caddr_t)(sc->sc_ep - SLBUFSIZE));
359                 sc->sc_ep = 0;
360                 sc->sc_mp = 0;
361                 sc->sc_buf = 0;
362         }
363         splx(s);
364         return 0;
365 }
366
367 /*
368  * Line specific (tty) ioctl routine.
369  * Provide a way to get the sl unit number.
370  */
371 /* ARGSUSED */
372 static int
373 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *p)
374 {
375         struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc, *tmpnc;
376         int s, nsl;
377
378         s = splimp();
379         switch (cmd) {
380         case SLIOCGUNIT:
381                 *(int *)data = sc->sc_if.if_dunit;
382                 break;
383
384         case SLIOCSUNIT:
385                 if (sc->sc_if.if_dunit != *(u_int *)data) {
386                         for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
387                                 if (   nc->sc_if.if_dunit == *(u_int *)data
388                                     && nc->sc_ttyp == NULL
389                                    ) {
390                                         tmpnc = malloc(sizeof *tmpnc, M_TEMP,
391                                                        M_WAITOK);
392                                         *tmpnc = *nc;
393                                         *nc = *sc;
394                                         nc->sc_if = tmpnc->sc_if;
395                                         tmpnc->sc_if = sc->sc_if;
396                                         *sc = *tmpnc;
397                                         free(tmpnc, M_TEMP);
398                                         if (sc->sc_if.if_flags & IFF_UP) {
399                                                 if_down(&sc->sc_if);
400                                                 if (!(nc->sc_if.if_flags & IFF_UP))
401                                                         if_up(&nc->sc_if);
402                                         } else if (nc->sc_if.if_flags & IFF_UP)
403                                                 if_down(&nc->sc_if);
404                                         sc->sc_flags &= ~SC_STATIC;
405                                         sc->sc_flags |= (nc->sc_flags & SC_STATIC);
406                                         tp->t_sc = sc = nc;
407                                         clist_alloc_cblocks(&tp->t_outq,
408                                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
409                                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
410                                         sl_compress_init(&sc->sc_comp, -1);
411                                         goto slfound;
412                                 }
413                         }
414                         splx(s);
415                         return (ENXIO);
416                 }
417         slfound:
418                 sc->sc_flags |= SC_STATIC;
419                 break;
420
421         case SLIOCSKEEPAL:
422                 sc->sc_keepalive = *(u_int *)data * hz;
423                 if (sc->sc_keepalive) {
424                         sc->sc_flags |= SC_KEEPALIVE;
425                         sc->sc_kahandle = timeout(sl_keepalive, sc,
426                                                   sc->sc_keepalive);
427                 } else {
428                         if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
429                                 untimeout(sl_keepalive, sc, sc->sc_kahandle);
430                                 sc->sc_flags &= ~SC_KEEPALIVE;
431                         }
432                 }
433                 break;
434
435         case SLIOCGKEEPAL:
436                 *(int *)data = sc->sc_keepalive / hz;
437                 break;
438
439         case SLIOCSOUTFILL:
440                 sc->sc_outfill = *(u_int *)data * hz;
441                 if (sc->sc_outfill) {
442                         sc->sc_flags |= SC_OUTWAIT;
443                         sc->sc_ofhandle = timeout(sl_outfill, sc,
444                                                   sc->sc_outfill);
445                 } else {
446                         if ((sc->sc_flags & SC_OUTWAIT) != 0) {
447                                 untimeout(sl_outfill, sc, sc->sc_ofhandle);
448                                 sc->sc_flags &= ~SC_OUTWAIT;
449                         }
450                 }
451                 break;
452
453         case SLIOCGOUTFILL:
454                 *(int *)data = sc->sc_outfill / hz;
455                 break;
456
457         default:
458                 splx(s);
459                 return (ENOIOCTL);
460         }
461         splx(s);
462         return (0);
463 }
464
465 /*
466  * Queue a packet.  Start transmission if not active.
467  * Compression happens in slstart; if we do it here, IP TOS
468  * will cause us to not compress "background" packets, because
469  * ordering gets trashed.  It can be done for all packets in slstart.
470  */
471 static int
472 sloutput(ifp, m, dst, rtp)
473         struct ifnet *ifp;
474         struct mbuf *m;
475         struct sockaddr *dst;
476         struct rtentry *rtp;
477 {
478         struct sl_softc *sc = &sl_softc[ifp->if_dunit];
479         struct ip *ip;
480         struct ifqueue *ifq;
481         int s;
482
483         /*
484          * `Cannot happen' (see slioctl).  Someday we will extend
485          * the line protocol to support other address families.
486          */
487         if (dst->sa_family != AF_INET) {
488                 printf("%s: af%d not supported\n", sc->sc_if.if_xname,
489                         dst->sa_family);
490                 m_freem(m);
491                 sc->sc_if.if_noproto++;
492                 return (EAFNOSUPPORT);
493         }
494
495         if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
496                 m_freem(m);
497                 return (ENETDOWN);
498         }
499         if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
500                 m_freem(m);
501                 return (EHOSTUNREACH);
502         }
503         ifq = &sc->sc_if.if_snd;
504         ip = mtod(m, struct ip *);
505         if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
506                 m_freem(m);
507                 return (ENETRESET);             /* XXX ? */
508         }
509         if (ip->ip_tos & IPTOS_LOWDELAY)
510                 ifq = &sc->sc_fastq;
511         s = splimp();
512         if (IF_QFULL(ifq)) {
513                 IF_DROP(ifq);
514                 m_freem(m);
515                 splx(s);
516                 sc->sc_if.if_oerrors++;
517                 return (ENOBUFS);
518         }
519         IF_ENQUEUE(ifq, m);
520         if (sc->sc_ttyp->t_outq.c_cc == 0)
521                 slstart(sc->sc_ttyp);
522         splx(s);
523         return (0);
524 }
525
526 /*
527  * Start output on interface.  Get another datagram
528  * to send from the interface queue and map it to
529  * the interface before starting output.
530  */
531 static int
532 slstart(tp)
533         struct tty *tp;
534 {
535         struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
536         struct mbuf *m;
537         u_char *cp;
538         struct ip *ip;
539         int s;
540         u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
541         int len = 0;
542
543         for (;;) {
544                 /*
545                  * Call output process whether or not there is more in the
546                  * output queue.  We are being called in lieu of ttstart
547                  * and must do what it would.
548                  */
549                 (*tp->t_oproc)(tp);
550
551                 if (tp->t_outq.c_cc != 0) {
552                         if (sc != NULL)
553                                 sc->sc_flags &= ~SC_OUTWAIT;
554                         if (tp->t_outq.c_cc > SLIP_HIWAT)
555                                 return 0;
556                 }
557
558                 /*
559                  * This happens briefly when the line shuts down.
560                  */
561                 if (sc == NULL)
562                         return 0;
563
564                 /*
565                  * Get a packet and send it to the interface.
566                  */
567                 s = splimp();
568                 IF_DEQUEUE(&sc->sc_fastq, m);
569                 if (m)
570                         sc->sc_if.if_omcasts++;         /* XXX */
571                 else
572                         IF_DEQUEUE(&sc->sc_if.if_snd, m);
573                 splx(s);
574                 if (m == NULL)
575                         return 0;
576
577                 /*
578                  * We do the header compression here rather than in sloutput
579                  * because the packets will be out of order if we are using TOS
580                  * queueing, and the connection id compression will get
581                  * munged when this happens.
582                  */
583                 if (sc->sc_if.if_bpf) {
584                         /*
585                          * We need to save the TCP/IP header before it's
586                          * compressed.  To avoid complicated code, we just
587                          * copy the entire packet into a stack buffer (since
588                          * this is a serial line, packets should be short
589                          * and/or the copy should be negligible cost compared
590                          * to the packet transmission time).
591                          */
592                         struct mbuf *m1 = m;
593                         u_char *cp = bpfbuf + SLIP_HDRLEN;
594
595                         len = 0;
596                         do {
597                                 int mlen = m1->m_len;
598
599                                 bcopy(mtod(m1, caddr_t), cp, mlen);
600                                 cp += mlen;
601                                 len += mlen;
602                         } while ((m1 = m1->m_next) != NULL);
603                 }
604                 ip = mtod(m, struct ip *);
605                 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
606                         if (sc->sc_if.if_flags & SC_COMPRESS)
607                                 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
608                                     &sc->sc_comp, 1);
609                 }
610                 if (sc->sc_if.if_bpf) {
611                         /*
612                          * Put the SLIP pseudo-"link header" in place.  The
613                          * compressed header is now at the beginning of the
614                          * mbuf.
615                          */
616                         bpfbuf[SLX_DIR] = SLIPDIR_OUT;
617                         bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
618                         bpf_tap(&sc->sc_if, bpfbuf, len + SLIP_HDRLEN);
619                 }
620
621                 /*
622                  * If system is getting low on clists, just flush our
623                  * output queue (if the stuff was important, it'll get
624                  * retransmitted). Note that SLTMAX is used instead of
625                  * the current if_mtu setting because connections that
626                  * have already been established still use the original
627                  * (possibly larger) mss.
628                  */
629                 if (cfreecount < CLISTRESERVE + SLTMAX) {
630                         m_freem(m);
631                         sc->sc_if.if_collisions++;
632                         continue;
633                 }
634
635                 sc->sc_flags &= ~SC_OUTWAIT;
636                 /*
637                  * The extra FRAME_END will start up a new packet, and thus
638                  * will flush any accumulated garbage.  We do this whenever
639                  * the line may have been idle for some time.
640                  */
641                 if (tp->t_outq.c_cc == 0) {
642                         ++sc->sc_if.if_obytes;
643                         (void) putc(FRAME_END, &tp->t_outq);
644                 }
645
646                 while (m) {
647                         u_char *ep;
648
649                         cp = mtod(m, u_char *); ep = cp + m->m_len;
650                         while (cp < ep) {
651                                 /*
652                                  * Find out how many bytes in the string we can
653                                  * handle without doing something special.
654                                  */
655                                 u_char *bp = cp;
656
657                                 while (cp < ep) {
658                                         switch (*cp++) {
659                                         case FRAME_ESCAPE:
660                                         case FRAME_END:
661                                                 --cp;
662                                                 goto out;
663                                         }
664                                 }
665                                 out:
666                                 if (cp > bp) {
667                                         /*
668                                          * Put n characters at once
669                                          * into the tty output queue.
670                                          */
671                                         if (b_to_q((char *)bp, cp - bp,
672                                             &tp->t_outq))
673                                                 break;
674                                         sc->sc_if.if_obytes += cp - bp;
675                                 }
676                                 /*
677                                  * If there are characters left in the mbuf,
678                                  * the first one must be special..
679                                  * Put it out in a different form.
680                                  */
681                                 if (cp < ep) {
682                                         if (putc(FRAME_ESCAPE, &tp->t_outq))
683                                                 break;
684                                         if (putc(*cp++ == FRAME_ESCAPE ?
685                                            TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
686                                            &tp->t_outq)) {
687                                                 (void) unputc(&tp->t_outq);
688                                                 break;
689                                         }
690                                         sc->sc_if.if_obytes += 2;
691                                 }
692                         }
693                         m = m_free(m);
694                 }
695
696                 if (putc(FRAME_END, &tp->t_outq)) {
697                         /*
698                          * Not enough room.  Remove a char to make room
699                          * and end the packet normally.
700                          * If you get many collisions (more than one or two
701                          * a day) you probably do not have enough clists
702                          * and you should increase "nclist" in param.c.
703                          */
704                         (void) unputc(&tp->t_outq);
705                         (void) putc(FRAME_END, &tp->t_outq);
706                         sc->sc_if.if_collisions++;
707                 } else {
708                         ++sc->sc_if.if_obytes;
709                         sc->sc_if.if_opackets++;
710                 }
711         }
712         return 0;
713 }
714
715 /*
716  * Copy data buffer to mbuf chain; add ifnet pointer.
717  */
718 static struct mbuf *
719 sl_btom(sc, len)
720         struct sl_softc *sc;
721         int len;
722 {
723         struct mbuf *m;
724
725         MGETHDR(m, M_DONTWAIT, MT_DATA);
726         if (m == NULL)
727                 return (NULL);
728
729         /*
730          * If we have more than MHLEN bytes, it's cheaper to
731          * queue the cluster we just filled & allocate a new one
732          * for the input buffer.  Otherwise, fill the mbuf we
733          * allocated above.  Note that code in the input routine
734          * guarantees that packet will fit in a cluster.
735          */
736         if (len >= MHLEN) {
737                 MCLGET(m, M_DONTWAIT);
738                 if ((m->m_flags & M_EXT) == 0) {
739                         /*
740                          * we couldn't get a cluster - if memory's this
741                          * low, it's time to start dropping packets.
742                          */
743                         (void) m_free(m);
744                         return (NULL);
745                 }
746                 sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
747                 m->m_data = (caddr_t)sc->sc_buf;
748                 m->m_ext.ext_buf = (caddr_t)((intptr_t)sc->sc_buf &~ MCLOFSET);
749         } else
750                 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
751
752         m->m_len = len;
753         m->m_pkthdr.len = len;
754         m->m_pkthdr.rcvif = &sc->sc_if;
755         return (m);
756 }
757
758 /*
759  * tty interface receiver interrupt.
760  */
761 static int
762 slinput(c, tp)
763         int c;
764         struct tty *tp;
765 {
766         struct sl_softc *sc;
767         struct mbuf *m;
768         int len;
769         u_char chdr[CHDR_LEN];
770
771         tk_nin++;
772         sc = (struct sl_softc *)tp->t_sc;
773         if (sc == NULL)
774                 return 0;
775         if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
776                 sc->sc_flags |= SC_ERROR;
777                 return 0;
778         }
779         c &= TTY_CHARMASK;
780
781         ++sc->sc_if.if_ibytes;
782
783         if (sc->sc_if.if_flags & IFF_DEBUG) {
784                 if (c == ABT_ESC) {
785                         /*
786                          * If we have a previous abort, see whether
787                          * this one is within the time limit.
788                          */
789                         if (sc->sc_abortcount &&
790                             time_second >= sc->sc_starttime + ABT_WINDOW)
791                                 sc->sc_abortcount = 0;
792                         /*
793                          * If we see an abort after "idle" time, count it;
794                          * record when the first abort escape arrived.
795                          */
796                         if (time_second >= sc->sc_lasttime + ABT_IDLE) {
797                                 if (++sc->sc_abortcount == 1)
798                                         sc->sc_starttime = time_second;
799                                 if (sc->sc_abortcount >= ABT_COUNT) {
800                                         slclose(tp,0);
801                                         return 0;
802                                 }
803                         }
804                 } else
805                         sc->sc_abortcount = 0;
806                 sc->sc_lasttime = time_second;
807         }
808
809         switch (c) {
810
811         case TRANS_FRAME_ESCAPE:
812                 if (sc->sc_escape)
813                         c = FRAME_ESCAPE;
814                 break;
815
816         case TRANS_FRAME_END:
817                 if (sc->sc_escape)
818                         c = FRAME_END;
819                 break;
820
821         case FRAME_ESCAPE:
822                 sc->sc_escape = 1;
823                 return 0;
824
825         case FRAME_END:
826                 sc->sc_flags &= ~SC_KEEPALIVE;
827                 if(sc->sc_flags & SC_ERROR) {
828                         sc->sc_flags &= ~SC_ERROR;
829                         goto newpack;
830                 }
831                 len = sc->sc_mp - sc->sc_buf;
832                 if (len < 3)
833                         /* less than min length packet - ignore */
834                         goto newpack;
835
836                 if (sc->sc_if.if_bpf) {
837                         /*
838                          * Save the compressed header, so we
839                          * can tack it on later.  Note that we
840                          * will end up copying garbage in some
841                          * cases but this is okay.  We remember
842                          * where the buffer started so we can
843                          * compute the new header length.
844                          */
845                         bcopy(sc->sc_buf, chdr, CHDR_LEN);
846                 }
847
848                 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
849                         if (c & 0x80)
850                                 c = TYPE_COMPRESSED_TCP;
851                         else if (c == TYPE_UNCOMPRESSED_TCP)
852                                 *sc->sc_buf &= 0x4f; /* XXX */
853                         /*
854                          * We've got something that's not an IP packet.
855                          * If compression is enabled, try to decompress it.
856                          * Otherwise, if `auto-enable' compression is on and
857                          * it's a reasonable packet, decompress it and then
858                          * enable compression.  Otherwise, drop it.
859                          */
860                         if (sc->sc_if.if_flags & SC_COMPRESS) {
861                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
862                                                         (u_int)c, &sc->sc_comp);
863                                 if (len <= 0)
864                                         goto error;
865                         } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
866                             c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
867                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
868                                                         (u_int)c, &sc->sc_comp);
869                                 if (len <= 0)
870                                         goto error;
871                                 sc->sc_if.if_flags |= SC_COMPRESS;
872                         } else
873                                 goto error;
874                 }
875                 if (sc->sc_if.if_bpf) {
876                         /*
877                          * Put the SLIP pseudo-"link header" in place.
878                          * We couldn't do this any earlier since
879                          * decompression probably moved the buffer
880                          * pointer.  Then, invoke BPF.
881                          */
882                         u_char *hp = sc->sc_buf - SLIP_HDRLEN;
883
884                         hp[SLX_DIR] = SLIPDIR_IN;
885                         bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
886                         bpf_tap(&sc->sc_if, hp, len + SLIP_HDRLEN);
887                 }
888                 m = sl_btom(sc, len);
889                 if (m == NULL)
890                         goto error;
891
892                 sc->sc_if.if_ipackets++;
893
894                 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
895                         m_freem(m);
896                         goto newpack;
897                 }
898
899                 if (netisr_queue(NETISR_IP, m)) {
900                         sc->sc_if.if_ierrors++;
901                         sc->sc_if.if_iqdrops++;
902                 }
903
904                 goto newpack;
905         }
906         if (sc->sc_mp < sc->sc_ep) {
907                 *sc->sc_mp++ = c;
908                 sc->sc_escape = 0;
909                 return 0;
910         }
911
912         /* can't put lower; would miss an extra frame */
913         sc->sc_flags |= SC_ERROR;
914
915 error:
916         sc->sc_if.if_ierrors++;
917 newpack:
918         sc->sc_mp = sc->sc_buf = sc->sc_ep - SLRMAX;
919         sc->sc_escape = 0;
920         return 0;
921 }
922
923 /*
924  * Process an ioctl request.
925  */
926 static int
927 slioctl(ifp, cmd, data, cr)
928         struct ifnet *ifp;
929         u_long cmd;
930         caddr_t data;
931         struct ucred *cr;
932 {
933         struct ifaddr *ifa = (struct ifaddr *)data;
934         struct ifreq *ifr = (struct ifreq *)data;
935         int s, error = 0;
936
937         s = splimp();
938
939         switch (cmd) {
940
941         case SIOCSIFFLAGS:
942                 /*
943                  * if.c will set the interface up even if we
944                  * don't want it to.
945                  */
946                 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
947                         ifp->if_flags &= ~IFF_UP;
948                 }
949                 break;
950         case SIOCSIFADDR:
951                 /*
952                  * This is "historical" - set the interface up when
953                  * setting the address.
954                  */
955                 if (ifa->ifa_addr->sa_family == AF_INET) {
956                         if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
957                                 ifp->if_flags |= IFF_UP;
958                 } else {
959                         error = EAFNOSUPPORT;
960                 }
961                 break;
962
963         case SIOCSIFDSTADDR:
964                 if (ifa->ifa_addr->sa_family != AF_INET)
965                         error = EAFNOSUPPORT;
966                 break;
967
968         case SIOCADDMULTI:
969         case SIOCDELMULTI:
970                 break;
971
972         case SIOCSIFMTU:
973                 /*
974                  * Set the interface MTU.
975                  */
976                 if (ifr->ifr_mtu > SLTMAX)
977                         error = EINVAL;
978                 else {
979                         struct tty *tp;
980
981                         ifp->if_mtu = ifr->ifr_mtu;
982                         tp = sl_softc[ifp->if_dunit].sc_ttyp;
983                         if (tp != NULL)
984                                 clist_alloc_cblocks(&tp->t_outq,
985                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1,
986                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1);
987                 }
988                 break;
989
990         default:
991                 error = EINVAL;
992         }
993         splx(s);
994         return (error);
995 }
996
997 static void
998 sl_keepalive(chan)
999         void *chan;
1000 {
1001         struct sl_softc *sc = chan;
1002
1003         if (sc->sc_keepalive) {
1004                 if (sc->sc_flags & SC_KEEPALIVE)
1005                         pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1);
1006                 else
1007                         sc->sc_flags |= SC_KEEPALIVE;
1008                 sc->sc_kahandle = timeout(sl_keepalive, sc, sc->sc_keepalive);
1009         } else {
1010                 sc->sc_flags &= ~SC_KEEPALIVE;
1011         }
1012 }
1013
1014 static void
1015 sl_outfill(chan)
1016         void *chan;
1017 {
1018         struct sl_softc *sc = chan;
1019         struct tty *tp = sc->sc_ttyp;
1020         int s;
1021
1022         if (sc->sc_outfill && tp != NULL) {
1023                 if (sc->sc_flags & SC_OUTWAIT) {
1024                         s = splimp ();
1025                         ++sc->sc_if.if_obytes;
1026                         (void) putc(FRAME_END, &tp->t_outq);
1027                         (*tp->t_oproc)(tp);
1028                         splx (s);
1029                 } else
1030                         sc->sc_flags |= SC_OUTWAIT;
1031                 sc->sc_ofhandle = timeout(sl_outfill, sc, sc->sc_outfill);
1032         } else {
1033                 sc->sc_flags &= ~SC_OUTWAIT;
1034         }
1035 }