Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:28:48 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 "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 <net/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 __P((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 __P((struct sl_softc *));
184 static struct mbuf *sl_btom __P((struct sl_softc *, int));
185 static timeout_t sl_keepalive;
186 static timeout_t sl_outfill;
187 static int      slclose __P((struct tty *,int));
188 static int      slinput __P((int, struct tty *));
189 static int      slioctl __P((struct ifnet *, u_long, caddr_t));
190 static int      sltioctl __P((struct tty *, u_long, caddr_t, int, struct proc *));
191 static int      slopen __P((dev_t, struct tty *));
192 static int      sloutput __P((struct ifnet *,
193             struct mbuf *, struct sockaddr *, struct rtentry *));
194 static int      slstart __P((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         register struct sl_softc *sc;
210         register int i = 0;
211
212         linesw[SLIPDISC] = slipdisc;
213
214         for (sc = sl_softc; i < NSL; sc++) {
215                 sc->sc_if.if_name = "sl";
216                 sc->sc_if.if_unit = i++;
217                 sc->sc_if.if_mtu = SLMTU;
218                 sc->sc_if.if_flags =
219 #ifdef SLIP_IFF_OPTS
220                     SLIP_IFF_OPTS;
221 #else
222                     IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
223 #endif
224                 sc->sc_if.if_type = IFT_SLIP;
225                 sc->sc_if.if_ioctl = slioctl;
226                 sc->sc_if.if_output = sloutput;
227                 sc->sc_if.if_snd.ifq_maxlen = 50;
228                 sc->sc_fastq.ifq_maxlen = 32;
229                 sc->sc_if.if_linkmib = sc;
230                 sc->sc_if.if_linkmiblen = sizeof *sc;
231                 if_attach(&sc->sc_if);
232                 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
233         }
234 }
235
236 static int
237 slinit(sc)
238         register struct sl_softc *sc;
239 {
240         register caddr_t p;
241
242 #ifdef __i386__
243         int s;
244
245         s = splhigh();
246         tty_imask |= net_imask;
247         net_imask = tty_imask;
248         update_intr_masks();
249         splx(s);
250         if (bootverbose)
251                 printf("new imasks: bio %x, tty %x, net %x\n",
252                     bio_imask, tty_imask, net_imask);
253 #endif
254         if (sc->sc_ep == (u_char *) 0) {
255                 MCLALLOC(p, M_WAIT);
256                 if (p)
257                         sc->sc_ep = (u_char *)p + SLBUFSIZE;
258                 else {
259                         printf("sl%ld: can't allocate buffer\n",
260                             (long)(sc - sl_softc));
261                         return (0);
262                 }
263         }
264         sc->sc_buf = sc->sc_ep - SLRMAX;
265         sc->sc_mp = sc->sc_buf;
266         sl_compress_init(&sc->sc_comp, -1);
267         return (1);
268 }
269
270 /*
271  * Line specific open routine.
272  * Attach the given tty to the first available sl unit.
273  */
274 /* ARGSUSED */
275 static int
276 slopen(dev, tp)
277         dev_t dev;
278         register struct tty *tp;
279 {
280         struct proc *p = curproc;               /* XXX */
281         register struct sl_softc *sc;
282         register int nsl;
283         int s, error;
284
285         error = suser(p);
286         if (error)
287                 return (error);
288
289         if (tp->t_line == SLIPDISC)
290                 return (0);
291
292         for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
293                 if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) {
294                         if (slinit(sc) == 0)
295                                 return (ENOBUFS);
296                         tp->t_sc = (caddr_t)sc;
297                         sc->sc_ttyp = tp;
298                         sc->sc_if.if_baudrate = tp->t_ospeed;
299                         ttyflush(tp, FREAD | FWRITE);
300
301                         tp->t_line = SLIPDISC;
302                         /*
303                          * We don't use t_canq or t_rawq, so reduce their
304                          * cblock resources to 0.  Reserve enough cblocks
305                          * for t_outq to guarantee that we can fit a full
306                          * packet if the SLIP_HIWAT check allows slstart()
307                          * to loop.  Use the same value for the cblock
308                          * limit since the reserved blocks should always
309                          * be enough.  Reserving cblocks probably makes
310                          * the CLISTRESERVE check unnecessary and wasteful.
311                          */
312                         clist_alloc_cblocks(&tp->t_canq, 0, 0);
313                         clist_alloc_cblocks(&tp->t_outq,
314                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
315                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
316                         clist_alloc_cblocks(&tp->t_rawq, 0, 0);
317
318                         s = splnet();
319                         if_up(&sc->sc_if);
320                         splx(s);
321                         return (0);
322                 }
323         return (ENXIO);
324 }
325
326 /*
327  * Line specific close routine.
328  * Detach the tty from the sl unit.
329  */
330 static int
331 slclose(tp,flag)
332         struct tty *tp;
333         int flag;
334 {
335         register struct sl_softc *sc;
336         int s;
337
338         ttyflush(tp, FREAD | FWRITE);
339         /*
340          * XXX the placement of the following spl is misleading.  tty
341          * interrupts must be blocked across line discipline switches
342          * and throughout closes to avoid races.
343          */
344         s = splimp();           /* actually, max(spltty, splnet) */
345         clist_free_cblocks(&tp->t_outq);
346         tp->t_line = 0;
347         sc = (struct sl_softc *)tp->t_sc;
348         if (sc != NULL) {
349                 if (sc->sc_outfill) {
350                         sc->sc_outfill = 0;
351                         untimeout(sl_outfill, sc, sc->sc_ofhandle);
352                 }
353                 if (sc->sc_keepalive) {
354                         sc->sc_keepalive = 0;
355                         untimeout(sl_keepalive, sc, sc->sc_kahandle);
356                 }
357                 if_down(&sc->sc_if);
358                 sc->sc_flags &= SC_STATIC;
359                 sc->sc_ttyp = NULL;
360                 tp->t_sc = NULL;
361                 MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
362                 sc->sc_ep = 0;
363                 sc->sc_mp = 0;
364                 sc->sc_buf = 0;
365         }
366         splx(s);
367         return 0;
368 }
369
370 /*
371  * Line specific (tty) ioctl routine.
372  * Provide a way to get the sl unit number.
373  */
374 /* ARGSUSED */
375 static int
376 sltioctl(tp, cmd, data, flag, p)
377         struct tty *tp;
378         u_long cmd;
379         caddr_t data;
380         int flag;
381         struct proc *p;
382 {
383         struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc, *tmpnc;
384         int s, nsl;
385
386         s = splimp();
387         switch (cmd) {
388         case SLIOCGUNIT:
389                 *(int *)data = sc->sc_if.if_unit;
390                 break;
391
392         case SLIOCSUNIT:
393                 if (sc->sc_if.if_unit != *(u_int *)data) {
394                         for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
395                                 if (   nc->sc_if.if_unit == *(u_int *)data
396                                     && nc->sc_ttyp == NULL
397                                    ) {
398                                         tmpnc = malloc(sizeof *tmpnc, M_TEMP,
399                                                        M_NOWAIT);
400                                         if (tmpnc == NULL) {
401                                                 splx(s);
402                                                 return (ENOMEM);
403                                         }
404                                         *tmpnc = *nc;
405                                         *nc = *sc;
406                                         nc->sc_if = tmpnc->sc_if;
407                                         tmpnc->sc_if = sc->sc_if;
408                                         *sc = *tmpnc;
409                                         free(tmpnc, M_TEMP);
410                                         if (sc->sc_if.if_flags & IFF_UP) {
411                                                 if_down(&sc->sc_if);
412                                                 if (!(nc->sc_if.if_flags & IFF_UP))
413                                                         if_up(&nc->sc_if);
414                                         } else if (nc->sc_if.if_flags & IFF_UP)
415                                                 if_down(&nc->sc_if);
416                                         sc->sc_flags &= ~SC_STATIC;
417                                         sc->sc_flags |= (nc->sc_flags & SC_STATIC);
418                                         tp->t_sc = sc = nc;
419                                         clist_alloc_cblocks(&tp->t_outq,
420                                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
421                                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
422                                         sl_compress_init(&sc->sc_comp, -1);
423                                         goto slfound;
424                                 }
425                         }
426                         splx(s);
427                         return (ENXIO);
428                 }
429         slfound:
430                 sc->sc_flags |= SC_STATIC;
431                 break;
432
433         case SLIOCSKEEPAL:
434                 sc->sc_keepalive = *(u_int *)data * hz;
435                 if (sc->sc_keepalive) {
436                         sc->sc_flags |= SC_KEEPALIVE;
437                         sc->sc_kahandle = timeout(sl_keepalive, sc,
438                                                   sc->sc_keepalive);
439                 } else {
440                         if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
441                                 untimeout(sl_keepalive, sc, sc->sc_kahandle);
442                                 sc->sc_flags &= ~SC_KEEPALIVE;
443                         }
444                 }
445                 break;
446
447         case SLIOCGKEEPAL:
448                 *(int *)data = sc->sc_keepalive / hz;
449                 break;
450
451         case SLIOCSOUTFILL:
452                 sc->sc_outfill = *(u_int *)data * hz;
453                 if (sc->sc_outfill) {
454                         sc->sc_flags |= SC_OUTWAIT;
455                         sc->sc_ofhandle = timeout(sl_outfill, sc,
456                                                   sc->sc_outfill);
457                 } else {
458                         if ((sc->sc_flags & SC_OUTWAIT) != 0) {
459                                 untimeout(sl_outfill, sc, sc->sc_ofhandle);
460                                 sc->sc_flags &= ~SC_OUTWAIT;
461                         }
462                 }
463                 break;
464
465         case SLIOCGOUTFILL:
466                 *(int *)data = sc->sc_outfill / hz;
467                 break;
468
469         default:
470                 splx(s);
471                 return (ENOIOCTL);
472         }
473         splx(s);
474         return (0);
475 }
476
477 /*
478  * Queue a packet.  Start transmission if not active.
479  * Compression happens in slstart; if we do it here, IP TOS
480  * will cause us to not compress "background" packets, because
481  * ordering gets trashed.  It can be done for all packets in slstart.
482  */
483 static int
484 sloutput(ifp, m, dst, rtp)
485         struct ifnet *ifp;
486         register struct mbuf *m;
487         struct sockaddr *dst;
488         struct rtentry *rtp;
489 {
490         register struct sl_softc *sc = &sl_softc[ifp->if_unit];
491         register struct ip *ip;
492         register struct ifqueue *ifq;
493         int s;
494
495         /*
496          * `Cannot happen' (see slioctl).  Someday we will extend
497          * the line protocol to support other address families.
498          */
499         if (dst->sa_family != AF_INET) {
500                 printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
501                         dst->sa_family);
502                 m_freem(m);
503                 sc->sc_if.if_noproto++;
504                 return (EAFNOSUPPORT);
505         }
506
507         if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
508                 m_freem(m);
509                 return (ENETDOWN);
510         }
511         if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
512                 m_freem(m);
513                 return (EHOSTUNREACH);
514         }
515         ifq = &sc->sc_if.if_snd;
516         ip = mtod(m, struct ip *);
517         if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
518                 m_freem(m);
519                 return (ENETRESET);             /* XXX ? */
520         }
521         if (ip->ip_tos & IPTOS_LOWDELAY)
522                 ifq = &sc->sc_fastq;
523         s = splimp();
524         if (IF_QFULL(ifq)) {
525                 IF_DROP(ifq);
526                 m_freem(m);
527                 splx(s);
528                 sc->sc_if.if_oerrors++;
529                 return (ENOBUFS);
530         }
531         IF_ENQUEUE(ifq, m);
532         if (sc->sc_ttyp->t_outq.c_cc == 0)
533                 slstart(sc->sc_ttyp);
534         splx(s);
535         return (0);
536 }
537
538 /*
539  * Start output on interface.  Get another datagram
540  * to send from the interface queue and map it to
541  * the interface before starting output.
542  */
543 static int
544 slstart(tp)
545         register struct tty *tp;
546 {
547         register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
548         register struct mbuf *m;
549         register u_char *cp;
550         register struct ip *ip;
551         int s;
552         u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
553         register int len = 0;
554
555         for (;;) {
556                 /*
557                  * Call output process whether or not there is more in the
558                  * output queue.  We are being called in lieu of ttstart
559                  * and must do what it would.
560                  */
561                 (*tp->t_oproc)(tp);
562
563                 if (tp->t_outq.c_cc != 0) {
564                         if (sc != NULL)
565                                 sc->sc_flags &= ~SC_OUTWAIT;
566                         if (tp->t_outq.c_cc > SLIP_HIWAT)
567                                 return 0;
568                 }
569
570                 /*
571                  * This happens briefly when the line shuts down.
572                  */
573                 if (sc == NULL)
574                         return 0;
575
576                 /*
577                  * Get a packet and send it to the interface.
578                  */
579                 s = splimp();
580                 IF_DEQUEUE(&sc->sc_fastq, m);
581                 if (m)
582                         sc->sc_if.if_omcasts++;         /* XXX */
583                 else
584                         IF_DEQUEUE(&sc->sc_if.if_snd, m);
585                 splx(s);
586                 if (m == NULL)
587                         return 0;
588
589                 /*
590                  * We do the header compression here rather than in sloutput
591                  * because the packets will be out of order if we are using TOS
592                  * queueing, and the connection id compression will get
593                  * munged when this happens.
594                  */
595                 if (sc->sc_if.if_bpf) {
596                         /*
597                          * We need to save the TCP/IP header before it's
598                          * compressed.  To avoid complicated code, we just
599                          * copy the entire packet into a stack buffer (since
600                          * this is a serial line, packets should be short
601                          * and/or the copy should be negligible cost compared
602                          * to the packet transmission time).
603                          */
604                         register struct mbuf *m1 = m;
605                         register u_char *cp = bpfbuf + SLIP_HDRLEN;
606
607                         len = 0;
608                         do {
609                                 register int mlen = m1->m_len;
610
611                                 bcopy(mtod(m1, caddr_t), cp, mlen);
612                                 cp += mlen;
613                                 len += mlen;
614                         } while ((m1 = m1->m_next) != NULL);
615                 }
616                 ip = mtod(m, struct ip *);
617                 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
618                         if (sc->sc_if.if_flags & SC_COMPRESS)
619                                 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
620                                     &sc->sc_comp, 1);
621                 }
622                 if (sc->sc_if.if_bpf) {
623                         /*
624                          * Put the SLIP pseudo-"link header" in place.  The
625                          * compressed header is now at the beginning of the
626                          * mbuf.
627                          */
628                         bpfbuf[SLX_DIR] = SLIPDIR_OUT;
629                         bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
630                         bpf_tap(&sc->sc_if, bpfbuf, len + SLIP_HDRLEN);
631                 }
632
633                 /*
634                  * If system is getting low on clists, just flush our
635                  * output queue (if the stuff was important, it'll get
636                  * retransmitted). Note that SLTMAX is used instead of
637                  * the current if_mtu setting because connections that
638                  * have already been established still use the original
639                  * (possibly larger) mss.
640                  */
641                 if (cfreecount < CLISTRESERVE + SLTMAX) {
642                         m_freem(m);
643                         sc->sc_if.if_collisions++;
644                         continue;
645                 }
646
647                 sc->sc_flags &= ~SC_OUTWAIT;
648                 /*
649                  * The extra FRAME_END will start up a new packet, and thus
650                  * will flush any accumulated garbage.  We do this whenever
651                  * the line may have been idle for some time.
652                  */
653                 if (tp->t_outq.c_cc == 0) {
654                         ++sc->sc_if.if_obytes;
655                         (void) putc(FRAME_END, &tp->t_outq);
656                 }
657
658                 while (m) {
659                         register u_char *ep;
660
661                         cp = mtod(m, u_char *); ep = cp + m->m_len;
662                         while (cp < ep) {
663                                 /*
664                                  * Find out how many bytes in the string we can
665                                  * handle without doing something special.
666                                  */
667                                 register u_char *bp = cp;
668
669                                 while (cp < ep) {
670                                         switch (*cp++) {
671                                         case FRAME_ESCAPE:
672                                         case FRAME_END:
673                                                 --cp;
674                                                 goto out;
675                                         }
676                                 }
677                                 out:
678                                 if (cp > bp) {
679                                         /*
680                                          * Put n characters at once
681                                          * into the tty output queue.
682                                          */
683                                         if (b_to_q((char *)bp, cp - bp,
684                                             &tp->t_outq))
685                                                 break;
686                                         sc->sc_if.if_obytes += cp - bp;
687                                 }
688                                 /*
689                                  * If there are characters left in the mbuf,
690                                  * the first one must be special..
691                                  * Put it out in a different form.
692                                  */
693                                 if (cp < ep) {
694                                         if (putc(FRAME_ESCAPE, &tp->t_outq))
695                                                 break;
696                                         if (putc(*cp++ == FRAME_ESCAPE ?
697                                            TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
698                                            &tp->t_outq)) {
699                                                 (void) unputc(&tp->t_outq);
700                                                 break;
701                                         }
702                                         sc->sc_if.if_obytes += 2;
703                                 }
704                         }
705                         m = m_free(m);
706                 }
707
708                 if (putc(FRAME_END, &tp->t_outq)) {
709                         /*
710                          * Not enough room.  Remove a char to make room
711                          * and end the packet normally.
712                          * If you get many collisions (more than one or two
713                          * a day) you probably do not have enough clists
714                          * and you should increase "nclist" in param.c.
715                          */
716                         (void) unputc(&tp->t_outq);
717                         (void) putc(FRAME_END, &tp->t_outq);
718                         sc->sc_if.if_collisions++;
719                 } else {
720                         ++sc->sc_if.if_obytes;
721                         sc->sc_if.if_opackets++;
722                 }
723         }
724         return 0;
725 }
726
727 /*
728  * Copy data buffer to mbuf chain; add ifnet pointer.
729  */
730 static struct mbuf *
731 sl_btom(sc, len)
732         register struct sl_softc *sc;
733         register int len;
734 {
735         register struct mbuf *m;
736
737         MGETHDR(m, M_DONTWAIT, MT_DATA);
738         if (m == NULL)
739                 return (NULL);
740
741         /*
742          * If we have more than MHLEN bytes, it's cheaper to
743          * queue the cluster we just filled & allocate a new one
744          * for the input buffer.  Otherwise, fill the mbuf we
745          * allocated above.  Note that code in the input routine
746          * guarantees that packet will fit in a cluster.
747          */
748         if (len >= MHLEN) {
749                 MCLGET(m, M_DONTWAIT);
750                 if ((m->m_flags & M_EXT) == 0) {
751                         /*
752                          * we couldn't get a cluster - if memory's this
753                          * low, it's time to start dropping packets.
754                          */
755                         (void) m_free(m);
756                         return (NULL);
757                 }
758                 sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
759                 m->m_data = (caddr_t)sc->sc_buf;
760                 m->m_ext.ext_buf = (caddr_t)((intptr_t)sc->sc_buf &~ MCLOFSET);
761         } else
762                 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
763
764         m->m_len = len;
765         m->m_pkthdr.len = len;
766         m->m_pkthdr.rcvif = &sc->sc_if;
767         return (m);
768 }
769
770 /*
771  * tty interface receiver interrupt.
772  */
773 static int
774 slinput(c, tp)
775         register int c;
776         register struct tty *tp;
777 {
778         register struct sl_softc *sc;
779         register struct mbuf *m;
780         register int len;
781         int s;
782         u_char chdr[CHDR_LEN];
783
784         tk_nin++;
785         sc = (struct sl_softc *)tp->t_sc;
786         if (sc == NULL)
787                 return 0;
788         if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
789                 sc->sc_flags |= SC_ERROR;
790                 return 0;
791         }
792         c &= TTY_CHARMASK;
793
794         ++sc->sc_if.if_ibytes;
795
796         if (sc->sc_if.if_flags & IFF_DEBUG) {
797                 if (c == ABT_ESC) {
798                         /*
799                          * If we have a previous abort, see whether
800                          * this one is within the time limit.
801                          */
802                         if (sc->sc_abortcount &&
803                             time_second >= sc->sc_starttime + ABT_WINDOW)
804                                 sc->sc_abortcount = 0;
805                         /*
806                          * If we see an abort after "idle" time, count it;
807                          * record when the first abort escape arrived.
808                          */
809                         if (time_second >= sc->sc_lasttime + ABT_IDLE) {
810                                 if (++sc->sc_abortcount == 1)
811                                         sc->sc_starttime = time_second;
812                                 if (sc->sc_abortcount >= ABT_COUNT) {
813                                         slclose(tp,0);
814                                         return 0;
815                                 }
816                         }
817                 } else
818                         sc->sc_abortcount = 0;
819                 sc->sc_lasttime = time_second;
820         }
821
822         switch (c) {
823
824         case TRANS_FRAME_ESCAPE:
825                 if (sc->sc_escape)
826                         c = FRAME_ESCAPE;
827                 break;
828
829         case TRANS_FRAME_END:
830                 if (sc->sc_escape)
831                         c = FRAME_END;
832                 break;
833
834         case FRAME_ESCAPE:
835                 sc->sc_escape = 1;
836                 return 0;
837
838         case FRAME_END:
839                 sc->sc_flags &= ~SC_KEEPALIVE;
840                 if(sc->sc_flags & SC_ERROR) {
841                         sc->sc_flags &= ~SC_ERROR;
842                         goto newpack;
843                 }
844                 len = sc->sc_mp - sc->sc_buf;
845                 if (len < 3)
846                         /* less than min length packet - ignore */
847                         goto newpack;
848
849                 if (sc->sc_if.if_bpf) {
850                         /*
851                          * Save the compressed header, so we
852                          * can tack it on later.  Note that we
853                          * will end up copying garbage in some
854                          * cases but this is okay.  We remember
855                          * where the buffer started so we can
856                          * compute the new header length.
857                          */
858                         bcopy(sc->sc_buf, chdr, CHDR_LEN);
859                 }
860
861                 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
862                         if (c & 0x80)
863                                 c = TYPE_COMPRESSED_TCP;
864                         else if (c == TYPE_UNCOMPRESSED_TCP)
865                                 *sc->sc_buf &= 0x4f; /* XXX */
866                         /*
867                          * We've got something that's not an IP packet.
868                          * If compression is enabled, try to decompress it.
869                          * Otherwise, if `auto-enable' compression is on and
870                          * it's a reasonable packet, decompress it and then
871                          * enable compression.  Otherwise, drop it.
872                          */
873                         if (sc->sc_if.if_flags & SC_COMPRESS) {
874                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
875                                                         (u_int)c, &sc->sc_comp);
876                                 if (len <= 0)
877                                         goto error;
878                         } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
879                             c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
880                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
881                                                         (u_int)c, &sc->sc_comp);
882                                 if (len <= 0)
883                                         goto error;
884                                 sc->sc_if.if_flags |= SC_COMPRESS;
885                         } else
886                                 goto error;
887                 }
888                 if (sc->sc_if.if_bpf) {
889                         /*
890                          * Put the SLIP pseudo-"link header" in place.
891                          * We couldn't do this any earlier since
892                          * decompression probably moved the buffer
893                          * pointer.  Then, invoke BPF.
894                          */
895                         register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
896
897                         hp[SLX_DIR] = SLIPDIR_IN;
898                         bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
899                         bpf_tap(&sc->sc_if, hp, len + SLIP_HDRLEN);
900                 }
901                 m = sl_btom(sc, len);
902                 if (m == NULL)
903                         goto error;
904
905                 sc->sc_if.if_ipackets++;
906
907                 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
908                         m_freem(m);
909                         goto newpack;
910                 }
911
912                 s = splimp();
913                 if (IF_QFULL(&ipintrq)) {
914                         IF_DROP(&ipintrq);
915                         sc->sc_if.if_ierrors++;
916                         sc->sc_if.if_iqdrops++;
917                         m_freem(m);
918                 } else {
919                         IF_ENQUEUE(&ipintrq, m);
920                         schednetisr(NETISR_IP);
921                 }
922                 splx(s);
923                 goto newpack;
924         }
925         if (sc->sc_mp < sc->sc_ep) {
926                 *sc->sc_mp++ = c;
927                 sc->sc_escape = 0;
928                 return 0;
929         }
930
931         /* can't put lower; would miss an extra frame */
932         sc->sc_flags |= SC_ERROR;
933
934 error:
935         sc->sc_if.if_ierrors++;
936 newpack:
937         sc->sc_mp = sc->sc_buf = sc->sc_ep - SLRMAX;
938         sc->sc_escape = 0;
939         return 0;
940 }
941
942 /*
943  * Process an ioctl request.
944  */
945 static int
946 slioctl(ifp, cmd, data)
947         register struct ifnet *ifp;
948         u_long cmd;
949         caddr_t data;
950 {
951         register struct ifaddr *ifa = (struct ifaddr *)data;
952         register struct ifreq *ifr = (struct ifreq *)data;
953         register int s, error = 0;
954
955         s = splimp();
956
957         switch (cmd) {
958
959         case SIOCSIFFLAGS:
960                 /*
961                  * if.c will set the interface up even if we
962                  * don't want it to.
963                  */
964                 if (sl_softc[ifp->if_unit].sc_ttyp == NULL) {
965                         ifp->if_flags &= ~IFF_UP;
966                 }
967                 break;
968         case SIOCSIFADDR:
969                 /*
970                  * This is "historical" - set the interface up when
971                  * setting the address.
972                  */
973                 if (ifa->ifa_addr->sa_family == AF_INET) {
974                         if (sl_softc[ifp->if_unit].sc_ttyp != NULL)
975                                 ifp->if_flags |= IFF_UP;
976                 } else {
977                         error = EAFNOSUPPORT;
978                 }
979                 break;
980
981         case SIOCSIFDSTADDR:
982                 if (ifa->ifa_addr->sa_family != AF_INET)
983                         error = EAFNOSUPPORT;
984                 break;
985
986         case SIOCADDMULTI:
987         case SIOCDELMULTI:
988                 break;
989
990         case SIOCSIFMTU:
991                 /*
992                  * Set the interface MTU.
993                  */
994                 if (ifr->ifr_mtu > SLTMAX)
995                         error = EINVAL;
996                 else {
997                         struct tty *tp;
998
999                         ifp->if_mtu = ifr->ifr_mtu;
1000                         tp = sl_softc[ifp->if_unit].sc_ttyp;
1001                         if (tp != NULL)
1002                                 clist_alloc_cblocks(&tp->t_outq,
1003                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1,
1004                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1);
1005                 }
1006                 break;
1007
1008         default:
1009                 error = EINVAL;
1010         }
1011         splx(s);
1012         return (error);
1013 }
1014
1015 static void
1016 sl_keepalive(chan)
1017         void *chan;
1018 {
1019         struct sl_softc *sc = chan;
1020
1021         if (sc->sc_keepalive) {
1022                 if (sc->sc_flags & SC_KEEPALIVE)
1023                         pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1);
1024                 else
1025                         sc->sc_flags |= SC_KEEPALIVE;
1026                 sc->sc_kahandle = timeout(sl_keepalive, sc, sc->sc_keepalive);
1027         } else {
1028                 sc->sc_flags &= ~SC_KEEPALIVE;
1029         }
1030 }
1031
1032 static void
1033 sl_outfill(chan)
1034         void *chan;
1035 {
1036         struct sl_softc *sc = chan;
1037         register struct tty *tp = sc->sc_ttyp;
1038         int s;
1039
1040         if (sc->sc_outfill && tp != NULL) {
1041                 if (sc->sc_flags & SC_OUTWAIT) {
1042                         s = splimp ();
1043                         ++sc->sc_if.if_obytes;
1044                         (void) putc(FRAME_END, &tp->t_outq);
1045                         (*tp->t_oproc)(tp);
1046                         splx (s);
1047                 } else
1048                         sc->sc_flags |= SC_OUTWAIT;
1049                 sc->sc_ofhandle = timeout(sl_outfill, sc, sc->sc_outfill);
1050         } else {
1051                 sc->sc_flags &= ~SC_OUTWAIT;
1052         }
1053 }