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