Change the kernel dev_t, representing a pointer to a specinfo structure,
[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.28 2006/09/10 01:26:40 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 ucred *);
190 static int      slopen (cdev_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 = kmalloc(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(cdev_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                         kfree(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 ucred *cred)
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 = kmalloc(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                                         kfree(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                 error = ifq_enqueue(&sc->sc_if.if_snd, m, &pktattr);
495         }
496         if (error) {
497                 sc->sc_if.if_oerrors++;
498                 crit_exit();
499                 return (error);
500         }
501         if (sc->sc_ttyp->t_outq.c_cc == 0)
502                 slstart(sc->sc_ttyp);
503         crit_exit();
504         return (0);
505 }
506
507 /*
508  * Start output on interface.  Get another datagram
509  * to send from the interface queue and map it to
510  * the interface before starting output.
511  */
512 static int
513 slstart(struct tty *tp)
514 {
515         struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
516         struct mbuf *m;
517         u_char *cp;
518         struct ip *ip;
519         u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
520         int len = 0;
521
522         for (;;) {
523                 /*
524                  * Call output process whether or not there is more in the
525                  * output queue.  We are being called in lieu of ttstart
526                  * and must do what it would.
527                  */
528                 (*tp->t_oproc)(tp);
529
530                 if (tp->t_outq.c_cc != 0) {
531                         if (sc != NULL)
532                                 sc->sc_flags &= ~SC_OUTWAIT;
533                         if (tp->t_outq.c_cc > SLIP_HIWAT)
534                                 return 0;
535                 }
536
537                 /*
538                  * This happens briefly when the line shuts down.
539                  */
540                 if (sc == NULL)
541                         return 0;
542
543                 /*
544                  * Get a packet and send it to the interface.
545                  */
546                 crit_enter();
547                 IF_DEQUEUE(&sc->sc_fastq, m);
548                 if (m)
549                         sc->sc_if.if_omcasts++;         /* XXX */
550                 else
551                         m = ifq_dequeue(&sc->sc_if.if_snd, NULL);
552                 crit_exit();
553                 if (m == NULL)
554                         return 0;
555
556                 /*
557                  * We do the header compression here rather than in sloutput
558                  * because the packets will be out of order if we are using TOS
559                  * queueing, and the connection id compression will get
560                  * munged when this happens.
561                  */
562                 if (sc->sc_if.if_bpf) {
563                         /*
564                          * We need to save the TCP/IP header before it's
565                          * compressed.  To avoid complicated code, we just
566                          * copy the entire packet into a stack buffer (since
567                          * this is a serial line, packets should be short
568                          * and/or the copy should be negligible cost compared
569                          * to the packet transmission time).
570                          */
571                         struct mbuf *m1 = m;
572                         u_char *cp = bpfbuf + SLIP_HDRLEN;
573
574                         len = 0;
575                         do {
576                                 int mlen = m1->m_len;
577
578                                 bcopy(mtod(m1, caddr_t), cp, mlen);
579                                 cp += mlen;
580                                 len += mlen;
581                         } while ((m1 = m1->m_next) != NULL);
582                 }
583                 ip = mtod(m, struct ip *);
584                 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
585                         if (sc->sc_if.if_flags & SC_COMPRESS)
586                                 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
587                                     &sc->sc_comp, 1);
588                 }
589                 if (sc->sc_if.if_bpf) {
590                         /*
591                          * Put the SLIP pseudo-"link header" in place.  The
592                          * compressed header is now at the beginning of the
593                          * mbuf.
594                          */
595                         bpfbuf[SLX_DIR] = SLIPDIR_OUT;
596                         bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
597                         bpf_tap(sc->sc_if.if_bpf, bpfbuf, len + SLIP_HDRLEN);
598                 }
599
600                 /*
601                  * If system is getting low on clists, just flush our
602                  * output queue (if the stuff was important, it'll get
603                  * retransmitted). Note that SLTMAX is used instead of
604                  * the current if_mtu setting because connections that
605                  * have already been established still use the original
606                  * (possibly larger) mss.
607                  */
608                 if (cfreecount < CLISTRESERVE + SLTMAX) {
609                         m_freem(m);
610                         sc->sc_if.if_collisions++;
611                         continue;
612                 }
613
614                 sc->sc_flags &= ~SC_OUTWAIT;
615                 /*
616                  * The extra FRAME_END will start up a new packet, and thus
617                  * will flush any accumulated garbage.  We do this whenever
618                  * the line may have been idle for some time.
619                  */
620                 if (tp->t_outq.c_cc == 0) {
621                         ++sc->sc_if.if_obytes;
622                         clist_putc(FRAME_END, &tp->t_outq);
623                 }
624
625                 while (m) {
626                         u_char *ep;
627
628                         cp = mtod(m, u_char *); ep = cp + m->m_len;
629                         while (cp < ep) {
630                                 /*
631                                  * Find out how many bytes in the string we can
632                                  * handle without doing something special.
633                                  */
634                                 u_char *bp = cp;
635
636                                 while (cp < ep) {
637                                         switch (*cp++) {
638                                         case FRAME_ESCAPE:
639                                         case FRAME_END:
640                                                 --cp;
641                                                 goto out;
642                                         }
643                                 }
644                                 out:
645                                 if (cp > bp) {
646                                         /*
647                                          * Put n characters at once
648                                          * into the tty output queue.
649                                          */
650                                         if (b_to_q((char *)bp, cp - bp,
651                                             &tp->t_outq))
652                                                 break;
653                                         sc->sc_if.if_obytes += cp - bp;
654                                 }
655                                 /*
656                                  * If there are characters left in the mbuf,
657                                  * the first one must be special..
658                                  * Put it out in a different form.
659                                  */
660                                 if (cp < ep) {
661                                         if (clist_putc(FRAME_ESCAPE, &tp->t_outq))
662                                                 break;
663                                         if (clist_putc(*cp++ == FRAME_ESCAPE ?
664                                            TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
665                                            &tp->t_outq)) {
666                                                 clist_unputc(&tp->t_outq);
667                                                 break;
668                                         }
669                                         sc->sc_if.if_obytes += 2;
670                                 }
671                         }
672                         m = m_free(m);
673                 }
674
675                 if (clist_putc(FRAME_END, &tp->t_outq)) {
676                         /*
677                          * Not enough room.  Remove a char to make room
678                          * and end the packet normally.
679                          * If you get many collisions (more than one or two
680                          * a day) you probably do not have enough clists
681                          * and you should increase "nclist" in param.c.
682                          */
683                         clist_unputc(&tp->t_outq);
684                         clist_putc(FRAME_END, &tp->t_outq);
685                         sc->sc_if.if_collisions++;
686                 } else {
687                         ++sc->sc_if.if_obytes;
688                         sc->sc_if.if_opackets++;
689                 }
690         }
691         return 0;
692 }
693
694 /*
695  * Copy data buffer to mbuf chain; add ifnet pointer.
696  */
697 static struct mbuf *
698 sl_btom(struct sl_softc *sc, int len)
699 {
700         struct mbuf *m;
701
702         if (len >= MCLBYTES)
703                 return (NULL);
704
705         MGETHDR(m, MB_DONTWAIT, MT_DATA);
706         if (m == NULL)
707                 return (NULL);
708
709         /*
710          * If we have more than MHLEN bytes, it's cheaper to
711          * queue the cluster we just filled & allocate a new one
712          * for the input buffer.  Otherwise, fill the mbuf we
713          * allocated above.  Note that code in the input routine
714          * guarantees that packet will fit in a cluster.
715          */
716         if (len >= MHLEN) {
717                 MCLGET(m, MB_DONTWAIT);
718                 if ((m->m_flags & M_EXT) == 0) {
719                         /*
720                          * we couldn't get a cluster - if memory's this
721                          * low, it's time to start dropping packets.
722                          */
723                         m_free(m);
724                         return (NULL);
725                 }
726         }
727         bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
728         m->m_len = len;
729         m->m_pkthdr.len = len;
730         m->m_pkthdr.rcvif = &sc->sc_if;
731         return (m);
732 }
733
734 /*
735  * tty interface receiver interrupt.
736  */
737 static int
738 slinput(int c, struct tty *tp)
739 {
740         struct sl_softc *sc;
741         struct mbuf *m;
742         int len;
743         u_char chdr[CHDR_LEN];
744
745         tk_nin++;
746         sc = (struct sl_softc *)tp->t_sc;
747         if (sc == NULL)
748                 return 0;
749         if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
750                 sc->sc_flags |= SC_ERROR;
751                 return 0;
752         }
753         c &= TTY_CHARMASK;
754
755         ++sc->sc_if.if_ibytes;
756
757         if (sc->sc_if.if_flags & IFF_DEBUG) {
758                 if (c == ABT_ESC) {
759                         /*
760                          * If we have a previous abort, see whether
761                          * this one is within the time limit.
762                          */
763                         if (sc->sc_abortcount &&
764                             time_second >= sc->sc_starttime + ABT_WINDOW)
765                                 sc->sc_abortcount = 0;
766                         /*
767                          * If we see an abort after "idle" time, count it;
768                          * record when the first abort escape arrived.
769                          */
770                         if (time_second >= sc->sc_lasttime + ABT_IDLE) {
771                                 if (++sc->sc_abortcount == 1)
772                                         sc->sc_starttime = time_second;
773                                 if (sc->sc_abortcount >= ABT_COUNT) {
774                                         slclose(tp,0);
775                                         return 0;
776                                 }
777                         }
778                 } else
779                         sc->sc_abortcount = 0;
780                 sc->sc_lasttime = time_second;
781         }
782
783         switch (c) {
784
785         case TRANS_FRAME_ESCAPE:
786                 if (sc->sc_escape)
787                         c = FRAME_ESCAPE;
788                 break;
789
790         case TRANS_FRAME_END:
791                 if (sc->sc_escape)
792                         c = FRAME_END;
793                 break;
794
795         case FRAME_ESCAPE:
796                 sc->sc_escape = 1;
797                 return 0;
798
799         case FRAME_END:
800                 sc->sc_flags &= ~SC_KEEPALIVE;
801                 if(sc->sc_flags & SC_ERROR) {
802                         sc->sc_flags &= ~SC_ERROR;
803                         goto newpack;
804                 }
805                 len = sc->sc_mp - sc->sc_buf;
806                 if (len < 3)
807                         /* less than min length packet - ignore */
808                         goto newpack;
809
810                 if (sc->sc_if.if_bpf) {
811                         /*
812                          * Save the compressed header, so we
813                          * can tack it on later.  Note that we
814                          * will end up copying garbage in some
815                          * cases but this is okay.  We remember
816                          * where the buffer started so we can
817                          * compute the new header length.
818                          */
819                         bcopy(sc->sc_buf, chdr, CHDR_LEN);
820                 }
821
822                 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
823                         if (c & 0x80)
824                                 c = TYPE_COMPRESSED_TCP;
825                         else if (c == TYPE_UNCOMPRESSED_TCP)
826                                 *sc->sc_buf &= 0x4f; /* XXX */
827                         /*
828                          * We've got something that's not an IP packet.
829                          * If compression is enabled, try to decompress it.
830                          * Otherwise, if `auto-enable' compression is on and
831                          * it's a reasonable packet, decompress it and then
832                          * enable compression.  Otherwise, drop it.
833                          */
834                         if (sc->sc_if.if_flags & SC_COMPRESS) {
835                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
836                                                         (u_int)c, &sc->sc_comp);
837                                 if (len <= 0)
838                                         goto error;
839                         } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
840                             c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
841                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
842                                                         (u_int)c, &sc->sc_comp);
843                                 if (len <= 0)
844                                         goto error;
845                                 sc->sc_if.if_flags |= SC_COMPRESS;
846                         } else
847                                 goto error;
848                 }
849                 if (sc->sc_if.if_bpf) {
850                         /*
851                          * Put the SLIP pseudo-"link header" in place.
852                          * We couldn't do this any earlier since
853                          * decompression probably moved the buffer
854                          * pointer.  Then, invoke BPF.
855                          */
856                         u_char *hp = sc->sc_buf - SLIP_HDRLEN;
857
858                         hp[SLX_DIR] = SLIPDIR_IN;
859                         bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
860                         bpf_tap(sc->sc_if.if_bpf, hp, len + SLIP_HDRLEN);
861                 }
862                 m = sl_btom(sc, len);
863                 if (m == NULL)
864                         goto error;
865
866                 sc->sc_if.if_ipackets++;
867
868                 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
869                         m_freem(m);
870                         goto newpack;
871                 }
872
873                 if (netisr_queue(NETISR_IP, m)) {
874                         sc->sc_if.if_ierrors++;
875                         sc->sc_if.if_iqdrops++;
876                 }
877
878                 goto newpack;
879         }
880         if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) {
881                 *sc->sc_mp++ = c;
882                 sc->sc_escape = 0;
883                 return 0;
884         }
885
886         /* can't put lower; would miss an extra frame */
887         sc->sc_flags |= SC_ERROR;
888
889 error:
890         sc->sc_if.if_ierrors++;
891 newpack:
892         sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
893         sc->sc_escape = 0;
894         return 0;
895 }
896
897 /*
898  * Process an ioctl request.
899  */
900 static int
901 slioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
902 {
903         struct ifaddr *ifa = (struct ifaddr *)data;
904         struct ifreq *ifr = (struct ifreq *)data;
905         int error = 0;
906
907         crit_enter();
908
909         switch (cmd) {
910
911         case SIOCSIFFLAGS:
912                 /*
913                  * if.c will set the interface up even if we
914                  * don't want it to.
915                  */
916                 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
917                         ifp->if_flags &= ~IFF_UP;
918                 }
919                 break;
920         case SIOCSIFADDR:
921                 /*
922                  * This is "historical" - set the interface up when
923                  * setting the address.
924                  */
925                 if (ifa->ifa_addr->sa_family == AF_INET) {
926                         if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
927                                 ifp->if_flags |= IFF_UP;
928                 } else {
929                         error = EAFNOSUPPORT;
930                 }
931                 break;
932
933         case SIOCSIFDSTADDR:
934                 if (ifa->ifa_addr->sa_family != AF_INET)
935                         error = EAFNOSUPPORT;
936                 break;
937
938         case SIOCADDMULTI:
939         case SIOCDELMULTI:
940                 break;
941
942         case SIOCSIFMTU:
943                 /*
944                  * Set the interface MTU.
945                  */
946                 if (ifr->ifr_mtu > SLTMAX)
947                         error = EINVAL;
948                 else {
949                         struct tty *tp;
950
951                         ifp->if_mtu = ifr->ifr_mtu;
952                         tp = sl_softc[ifp->if_dunit].sc_ttyp;
953                         if (tp != NULL)
954                                 clist_alloc_cblocks(&tp->t_outq,
955                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1,
956                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1);
957                 }
958                 break;
959
960         default:
961                 error = EINVAL;
962         }
963
964         crit_exit();
965         return (error);
966 }
967
968 static void
969 sl_keepalive(void *chan)
970 {
971         struct sl_softc *sc = chan;
972
973         if (sc->sc_keepalive) {
974                 if (sc->sc_flags & SC_KEEPALIVE)
975                         pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1);
976                 else
977                         sc->sc_flags |= SC_KEEPALIVE;
978                 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
979                                 sl_keepalive, sc);
980         } else {
981                 sc->sc_flags &= ~SC_KEEPALIVE;
982         }
983 }
984
985 static void
986 sl_outfill(void *chan)
987 {
988         struct sl_softc *sc = chan;
989         struct tty *tp = sc->sc_ttyp;
990
991         if (sc->sc_outfill && tp != NULL) {
992                 if (sc->sc_flags & SC_OUTWAIT) {
993                         crit_enter();
994                         ++sc->sc_if.if_obytes;
995                         clist_putc(FRAME_END, &tp->t_outq);
996                         (*tp->t_oproc)(tp);
997                         crit_exit();
998                 } else
999                         sc->sc_flags |= SC_OUTWAIT;
1000                 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
1001                                 sl_outfill, sc);
1002         } else {
1003                 sc->sc_flags &= ~SC_OUTWAIT;
1004         }
1005 }