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