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