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