kernel - Refactor tty clist code
[dragonfly.git] / sys / net / sl / if_sl.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1987, 1989, 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      @(#)if_sl.c     8.6 (Berkeley) 2/1/94
32  * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $
33  */
34
35 /*
36  * Serial Line interface
37  *
38  * Rick Adams
39  * Center for Seismic Studies
40  * 1300 N 17th Street, Suite 1450
41  * Arlington, Virginia 22209
42  * (703)276-7900
43  * rick@seismo.ARPA
44  * seismo!rick
45  *
46  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
47  * N.B.: this belongs in netinet, not net, the way it stands now.
48  * Should have a link-layer type designation, but wouldn't be
49  * backwards-compatible.
50  *
51  * Converted to 4.3BSD Beta by Chris Torek.
52  * Other changes made at Berkeley, based in part on code by Kirk Smith.
53  * W. Jolitz added slip abort.
54  *
55  * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
56  * Added priority queuing for "interactive" traffic; hooks for TCP
57  * header compression; ICMP filtering (at 2400 baud, some cretin
58  * pinging you can use up all your bandwidth).  Made low clist behavior
59  * more robust and slightly less likely to hang serial line.
60  * Sped up a bunch of things.
61  */
62
63 #include "use_sl.h"
64 #include "opt_inet.h"
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/proc.h>
69 #include <sys/priv.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
72 #include <sys/dkstat.h>
73 #include <sys/socket.h>
74 #include <sys/sockio.h>
75 #include <sys/fcntl.h>
76 #include <sys/signalvar.h>
77 #include <sys/tty.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/conf.h>
81 #include <sys/thread2.h>
82
83 #include <net/if.h>
84 #include <net/if_types.h>
85 #include <net/ifq_var.h>
86 #include <net/netisr.h>
87
88 #if INET
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #else
94 #error "Huh? Slip without inet?"
95 #endif
96
97 #include <net/slcompress.h>
98 #include "if_slvar.h"
99 #include <net/slip.h>
100
101 #include <net/bpf.h>
102
103 static void slattach (void *);
104 PSEUDO_SET(slattach, if_sl);
105
106 /* Set to 0xC002 for broadcast instead of p-to-p */
107 static int sliffopts = IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
108 SYSCTL_INT(_net, OID_AUTO, sliffopts, CTLFLAG_RW,
109            &sliffopts, 0, "");
110 TUNABLE_INT("net.sliffopts", &sliffopts);
111
112 /*
113  * SLRMAX is a hard limit on input packet size.  To simplify the code
114  * and improve performance, we require that packets fit in an mbuf
115  * cluster, and if we get a compressed packet, there's enough extra
116  * room to expand the header into a max length tcp/ip header (128
117  * bytes).  So, SLRMAX can be at most
118  *      MCLBYTES - 128
119  *
120  * SLMTU is the default transmit MTU. The transmit MTU should be kept
121  * small enough so that interactive use doesn't suffer, but large
122  * enough to provide good performance. 552 is a good choice for SLMTU
123  * because it is high enough to not fragment TCP packets being routed
124  * through this host. Packet fragmentation is bad with SLIP because
125  * fragment headers aren't compressed. The previous assumptions about
126  * the best MTU value don't really hold when using modern modems with
127  * BTLZ data compression because the modem buffers play a much larger
128  * role in interactive performance than the MTU. The MTU can be changed
129  * at any time to suit the specific environment with ifconfig(8), and
130  * its maximum value is defined as SLTMAX. SLTMAX must not be so large
131  * that it would overflow the stack if BPF is configured (XXX; if_ppp.c
132  * handles this better).
133  *
134  * SLIP_HIWAT is the amount of data that will be queued 'downstream'
135  * of us (i.e., in clists waiting to be picked up by the tty output
136  * interrupt).  If we queue a lot of data downstream, it's immune to
137  * our t.o.s. queuing.
138  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
139  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
140  * wait is dependent on the ftp window size but that's typically
141  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
142  * the cost (in idle time on the wire) of the tty driver running
143  * off the end of its clists & having to call back slstart for a
144  * new packet.  For a tty interface with any buffering at all, this
145  * cost will be zero.  Even with a totally brain dead interface (like
146  * the one on a typical workstation), the cost will be <= 1 character
147  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
148  * at most 1% while maintaining good interactive response.
149  */
150 #define BUFOFFSET       (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
151 #define SLRMAX          (MCLBYTES - BUFOFFSET)
152 #define SLBUFSIZE       (SLRMAX + BUFOFFSET)
153 #ifndef SLMTU
154 #define SLMTU           552             /* default MTU */
155 #endif
156 #define SLTMAX          1500            /* maximum MTU */
157 #define SLIP_HIWAT      50
158 #define CLISTRESERVE    1024            /* Can't let clists get too low */
159
160 /*
161  * SLIP ABORT ESCAPE MECHANISM:
162  *      (inspired by HAYES modem escape arrangement)
163  *      1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
164  *      within window time signals a "soft" exit from slip mode by remote end
165  *      if the IFF_DEBUG flag is on.
166  */
167 #define ABT_ESC         '\033'  /* can't be t_intr - distant host must know it*/
168 #define ABT_IDLE        1       /* in seconds - idle before an escape */
169 #define ABT_COUNT       3       /* count of escapes for abort */
170 #define ABT_WINDOW      (ABT_COUNT*2+2) /* in seconds - time to count */
171
172 static struct sl_softc sl_softc[NSL];
173
174 #define FRAME_END               0xc0            /* Frame End */
175 #define FRAME_ESCAPE            0xdb            /* Frame Esc */
176 #define TRANS_FRAME_END         0xdc            /* transposed frame end */
177 #define TRANS_FRAME_ESCAPE      0xdd            /* transposed frame esc */
178
179 static int slinit (struct sl_softc *);
180 static struct mbuf *sl_btom (struct sl_softc *, int);
181 static timeout_t sl_keepalive;
182 static timeout_t sl_outfill;
183 static int      slclose (struct tty *,int);
184 static int      slinput (int, struct tty *);
185 static int      slioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
186 static int      sltioctl (struct tty *, u_long, caddr_t, int, struct ucred *);
187 static int      slopen (cdev_t, struct tty *);
188 static int      sloutput (struct ifnet *,
189             struct mbuf *, struct sockaddr *, struct rtentry *);
190 static int      slstart (struct tty *);
191
192 static struct linesw slipdisc = {
193         slopen,         slclose,        l_noread,       l_nowrite,
194         sltioctl,       slinput,        slstart,        ttymodem,
195         FRAME_END
196 };
197
198 /*
199  * Called from boot code to establish sl interfaces.
200  */
201 static void
202 slattach(void *dummy)
203 {
204         struct sl_softc *sc;
205         int i = 0;
206
207         linesw[SLIPDISC] = slipdisc;
208
209         for (sc = sl_softc; i < NSL; sc++) {
210                 if_initname(&(sc->sc_if), "sl", i++);
211                 sc->sc_if.if_mtu = SLMTU;
212                 sc->sc_if.if_flags = sliffopts;
213                 sc->sc_if.if_type = IFT_SLIP;
214                 sc->sc_if.if_ioctl = slioctl;
215                 sc->sc_if.if_output = sloutput;
216                 ifq_set_maxlen(&sc->sc_if.if_snd, 50);
217                 ifq_set_ready(&sc->sc_if.if_snd);
218                 sc->sc_fastq.ifq_maxlen = 32;
219                 sc->sc_if.if_linkmib = sc;
220                 sc->sc_if.if_linkmiblen = sizeof *sc;
221                 callout_init_mp(&sc->sc_oftimeout);
222                 callout_init_mp(&sc->sc_katimeout);
223                 if_attach(&sc->sc_if, NULL);
224                 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
225         }
226 }
227
228 static int
229 slinit(struct sl_softc *sc)
230 {
231         if (sc->sc_ep == NULL)
232                 sc->sc_ep = kmalloc(SLBUFSIZE, M_DEVBUF, M_WAITOK);
233         sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
234         sc->sc_mp = sc->sc_buf;
235         sl_compress_init(&sc->sc_comp, -1);
236
237         return (1);
238 }
239
240 /*
241  * Line specific open routine.
242  * Attach the given tty to the first available sl unit.
243  */
244 /* ARGSUSED */
245 static int
246 slopen(cdev_t dev, struct tty *tp)
247 {
248         struct sl_softc *sc;
249         int nsl;
250         int error;
251         struct thread *td = curthread;  /* XXX */
252
253         error = priv_check(td, PRIV_ROOT);
254         if (error)
255                 return (error);
256
257         lwkt_gettoken(&tty_token);
258         if (tp->t_line == SLIPDISC) {
259                 lwkt_reltoken(&tty_token);
260                 return (0);
261         }
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                                 lwkt_reltoken(&tty_token);
267                                 return (ENOBUFS);
268                         }
269                         tp->t_slsc = (caddr_t)sc;
270                         sc->sc_ttyp = tp;
271                         sc->sc_if.if_baudrate = tp->t_ospeed;
272                         ttyflush(tp, FREAD | FWRITE);
273
274                         tp->t_line = SLIPDISC;
275                         /*
276                          * We don't use t_canq or t_rawq, so reduce their
277                          * cblock resources to 0.  Reserve enough cblocks
278                          * for t_outq to guarantee that we can fit a full
279                          * packet if the SLIP_HIWAT check allows slstart()
280                          * to loop.  Use the same value for the cblock
281                          * limit since the reserved blocks should always
282                          * be enough.  Reserving cblocks probably makes
283                          * the CLISTRESERVE check unnecessary and wasteful.
284                          */
285                         clist_alloc_cblocks(&tp->t_canq, 0);
286                         clist_alloc_cblocks(&tp->t_outq,
287                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
288                         clist_alloc_cblocks(&tp->t_rawq, 0);
289
290                         if_up(&sc->sc_if);
291                         lwkt_reltoken(&tty_token);
292                         return (0);
293                 }
294         }
295         lwkt_reltoken(&tty_token);
296
297         return (ENXIO);
298 }
299
300 /*
301  * Line specific close routine.
302  * Detach the tty from the sl unit.
303  */
304 static int
305 slclose(struct tty *tp, int flag)
306 {
307         struct sl_softc *sc;
308
309         lwkt_gettoken(&tty_token);
310         lwkt_gettoken(&tp->t_token);
311         ttyflush(tp, FREAD | FWRITE);
312
313         clist_free_cblocks(&tp->t_outq);
314         tp->t_line = 0;
315         sc = (struct sl_softc *)tp->t_slsc;
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_slsc = 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         lwkt_reltoken(&tp->t_token);
337         lwkt_reltoken(&tty_token);
338
339         return 0;
340 }
341
342 /*
343  * Line specific (tty) ioctl routine.
344  * Provide a way to get the sl unit number.
345  */
346 /* ARGSUSED */
347 static int
348 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
349 {
350         struct sl_softc *sc = (struct sl_softc *)tp->t_slsc, *nc, *tmpnc;
351         int nsl;
352
353         lwkt_gettoken(&tty_token);
354         lwkt_gettoken(&tp->t_token);
355
356         switch (cmd) {
357         case SLIOCGUNIT:
358                 *(int *)data = sc->sc_if.if_dunit;
359                 break;
360
361         case SLIOCSUNIT:
362                 if (sc->sc_if.if_dunit != *(u_int *)data) {
363                         for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
364                                 if (   nc->sc_if.if_dunit == *(u_int *)data
365                                     && nc->sc_ttyp == NULL
366                                    ) {
367                                         tmpnc = kmalloc(sizeof *tmpnc, M_TEMP,
368                                                        M_WAITOK);
369                                         *tmpnc = *nc;
370                                         *nc = *sc;
371                                         nc->sc_if = tmpnc->sc_if;
372                                         tmpnc->sc_if = sc->sc_if;
373                                         *sc = *tmpnc;
374                                         kfree(tmpnc, M_TEMP);
375                                         if (sc->sc_if.if_flags & IFF_UP) {
376                                                 if_down(&sc->sc_if);
377                                                 if (!(nc->sc_if.if_flags & IFF_UP))
378                                                         if_up(&nc->sc_if);
379                                         } else if (nc->sc_if.if_flags & IFF_UP)
380                                                 if_down(&nc->sc_if);
381                                         sc->sc_flags &= ~SC_STATIC;
382                                         sc->sc_flags |= (nc->sc_flags & SC_STATIC);
383                                         tp->t_slsc = sc = nc;
384                                         clist_alloc_cblocks(&tp->t_outq,
385                                             SLIP_HIWAT +
386                                             2 * sc->sc_if.if_mtu + 1);
387                                         sl_compress_init(&sc->sc_comp, -1);
388                                         goto slfound;
389                                 }
390                         }
391                         lwkt_reltoken(&tp->t_token);
392                         lwkt_reltoken(&tty_token);
393                         return (ENXIO);
394                 }
395         slfound:
396                 sc->sc_flags |= SC_STATIC;
397                 break;
398
399         case SLIOCSKEEPAL:
400                 sc->sc_keepalive = *(u_int *)data * hz;
401                 if (sc->sc_keepalive) {
402                         sc->sc_flags |= SC_KEEPALIVE;
403                         callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
404                                         sl_keepalive, sc);
405                 } else {
406                         if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
407                                 callout_stop(&sc->sc_katimeout);
408                                 sc->sc_flags &= ~SC_KEEPALIVE;
409                         }
410                 }
411                 break;
412
413         case SLIOCGKEEPAL:
414                 *(int *)data = sc->sc_keepalive / hz;
415                 break;
416
417         case SLIOCSOUTFILL:
418                 sc->sc_outfill = *(u_int *)data * hz;
419                 if (sc->sc_outfill) {
420                         sc->sc_flags |= SC_OUTWAIT;
421                         callout_reset(&sc->sc_oftimeout, sc->sc_outfill, 
422                                         sl_outfill, sc);
423                 } else {
424                         if ((sc->sc_flags & SC_OUTWAIT) != 0) {
425                                 callout_stop(&sc->sc_oftimeout);
426                                 sc->sc_flags &= ~SC_OUTWAIT;
427                         }
428                 }
429                 break;
430
431         case SLIOCGOUTFILL:
432                 *(int *)data = sc->sc_outfill / hz;
433                 break;
434
435         default:
436                 lwkt_reltoken(&tp->t_token);
437                 lwkt_reltoken(&tty_token);
438                 return (ENOIOCTL);
439         }
440         lwkt_reltoken(&tp->t_token);
441         lwkt_reltoken(&tty_token);
442
443         return (0);
444 }
445
446 /*
447  * Queue a packet.  Start transmission if not active.
448  * Compression happens in slstart; if we do it here, IP TOS
449  * will cause us to not compress "background" packets, because
450  * ordering gets trashed.  It can be done for all packets in slstart.
451  */
452 static int
453 sloutput_serialized(struct ifnet *ifp, struct ifaltq_subque *ifsq,
454     struct mbuf *m, struct sockaddr *dst, struct rtentry *rtp)
455 {
456         struct sl_softc *sc = &sl_softc[ifp->if_dunit];
457         struct ip *ip;
458         int error;
459         struct altq_pktattr pktattr;
460
461         ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
462
463         /*
464          * `Cannot happen' (see slioctl).  Someday we will extend
465          * the line protocol to support other address families.
466          */
467         if (dst->sa_family != AF_INET) {
468                 kprintf("%s: af%d not supported\n", sc->sc_if.if_xname,
469                         dst->sa_family);
470                 m_freem(m);
471                 IFNET_STAT_INC(&sc->sc_if, noproto, 1);
472                 return (EAFNOSUPPORT);
473         }
474
475         if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
476                 m_freem(m);
477                 return (ENETDOWN);
478         }
479         if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
480                 m_freem(m);
481                 return (EHOSTUNREACH);
482         }
483         ip = mtod(m, struct ip *);
484         if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
485                 m_freem(m);
486                 return (ENETRESET);             /* XXX ? */
487         }
488
489         if ((ip->ip_tos & IPTOS_LOWDELAY) &&
490             !ifq_is_enabled(&sc->sc_if.if_snd)) {
491                 if (IF_QFULL(&sc->sc_fastq)) {
492                         IF_DROP(&sc->sc_fastq);
493                         m_freem(m);
494                         error = ENOBUFS;
495                 } else {
496                         IF_ENQUEUE(&sc->sc_fastq, m);
497                         error = 0;
498                 }
499         } else {
500                 error = ifsq_enqueue(ifsq, m, &pktattr);
501         }
502         if (error) {
503                 IFNET_STAT_INC(&sc->sc_if, oqdrops, 1);
504                 return (error);
505         }
506         if (sc->sc_ttyp->t_outq.c_cc == 0)
507                 slstart(sc->sc_ttyp);
508         return (0);
509 }
510
511 static int
512 sloutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
513          struct rtentry *rtp)
514 {
515         struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
516         int error;
517
518         ifsq_serialize_hw(ifsq);
519         error = sloutput_serialized(ifp, ifsq, m, dst, rtp);
520         ifsq_deserialize_hw(ifsq);
521
522         return error;
523 }
524
525 /*
526  * Start output on interface.  Get another datagram
527  * to send from the interface queue and map it to
528  * the interface before starting output.
529  */
530 static int
531 slstart(struct tty *tp)
532 {
533         struct sl_softc *sc = (struct sl_softc *)tp->t_slsc;
534         struct ifaltq_subque *ifsq = ifq_get_subq_default(&sc->sc_if.if_snd);
535         struct mbuf *m;
536         u_char *cp;
537         struct ip *ip;
538         u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
539         int len = 0;
540
541         lwkt_gettoken(&tp->t_token);
542         for (;;) {
543                 /*
544                  * Call output process whether or not there is more in the
545                  * output queue.  We are being called in lieu of ttstart
546                  * and must do what it would.
547                  */
548                 (*tp->t_oproc)(tp);
549
550                 if (tp->t_outq.c_cc != 0) {
551                         if (sc != NULL)
552                                 sc->sc_flags &= ~SC_OUTWAIT;
553                         if (tp->t_outq.c_cc > SLIP_HIWAT) {
554                                 lwkt_reltoken(&tp->t_token);
555                                 return 0;
556                         }
557                 }
558
559                 /*
560                  * This happens briefly when the line shuts down.
561                  */
562                 if (sc == NULL) {
563                         lwkt_reltoken(&tp->t_token);
564                         return 0;
565                 }
566
567                 /*
568                  * Get a packet and send it to the interface.
569                  */
570                 IF_DEQUEUE(&sc->sc_fastq, m);
571                 if (m)
572                         IFNET_STAT_INC(&sc->sc_if, omcasts, 1); /* XXX */
573                 else
574                         m = ifsq_dequeue(ifsq);
575                 if (m == NULL) {
576                         lwkt_reltoken(&tp->t_token);
577                         return 0;
578                 }
579
580                 /*
581                  * We do the header compression here rather than in sloutput
582                  * because the packets will be out of order if we are using TOS
583                  * queueing, and the connection id compression will get
584                  * munged when this happens.
585                  */
586                 if (sc->sc_if.if_bpf) {
587                         /*
588                          * We need to save the TCP/IP header before it's
589                          * compressed.  To avoid complicated code, we just
590                          * copy the entire packet into a stack buffer (since
591                          * this is a serial line, packets should be short
592                          * and/or the copy should be negligible cost compared
593                          * to the packet transmission time).
594                          */
595                         struct mbuf *m1 = m;
596                         u_char *cp = bpfbuf + SLIP_HDRLEN;
597
598                         len = 0;
599                         do {
600                                 int mlen = m1->m_len;
601
602                                 bcopy(mtod(m1, caddr_t), cp, mlen);
603                                 cp += mlen;
604                                 len += mlen;
605                         } while ((m1 = m1->m_next) != NULL);
606                 }
607                 ip = mtod(m, struct ip *);
608                 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
609                         if (sc->sc_if.if_flags & SC_COMPRESS)
610                                 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
611                                     &sc->sc_comp, 1);
612                 }
613                 if (sc->sc_if.if_bpf) {
614                         bpf_gettoken();
615
616                         if (sc->sc_if.if_bpf) {
617                                 /*
618                                  * Put the SLIP pseudo-"link header" in place.
619                                  * The compressed header is now at the
620                                  * beginning of the mbuf.
621                                  */
622                                 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
623                                 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR],
624                                     CHDR_LEN);
625                                 bpf_tap(sc->sc_if.if_bpf, bpfbuf,
626                                     len + SLIP_HDRLEN);
627                         }
628
629                         bpf_reltoken();
630                 }
631
632                 sc->sc_flags &= ~SC_OUTWAIT;
633
634                 /*
635                  * The extra FRAME_END will start up a new packet, and thus
636                  * will flush any accumulated garbage.  We do this whenever
637                  * the line may have been idle for some time.
638                  */
639                 if (tp->t_outq.c_cc == 0) {
640                         IFNET_STAT_INC(&sc->sc_if, obytes, 1);
641                         clist_putc(FRAME_END, &tp->t_outq);
642                 }
643
644                 while (m) {
645                         u_char *ep;
646
647                         cp = mtod(m, u_char *); ep = cp + m->m_len;
648                         while (cp < ep) {
649                                 /*
650                                  * Find out how many bytes in the string we can
651                                  * handle without doing something special.
652                                  */
653                                 u_char *bp = cp;
654
655                                 while (cp < ep) {
656                                         switch (*cp++) {
657                                         case FRAME_ESCAPE:
658                                         case FRAME_END:
659                                                 --cp;
660                                                 goto out;
661                                         }
662                                 }
663                                 out:
664                                 if (cp > bp) {
665                                         /*
666                                          * Put n characters at once
667                                          * into the tty output queue.
668                                          */
669                                         if (clist_btoq((char *)bp, cp - bp,
670                                                        &tp->t_outq)) {
671                                                 break;
672                                         }
673                                         IFNET_STAT_INC(&sc->sc_if, obytes,
674                                                        cp - bp);
675                                 }
676                                 /*
677                                  * If there are characters left in the mbuf,
678                                  * the first one must be special..
679                                  * Put it out in a different form.
680                                  */
681                                 if (cp < ep) {
682                                         if (clist_putc(FRAME_ESCAPE, &tp->t_outq))
683                                                 break;
684                                         if (clist_putc(*cp++ == FRAME_ESCAPE ?
685                                            TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
686                                            &tp->t_outq)) {
687                                                 clist_unputc(&tp->t_outq);
688                                                 break;
689                                         }
690                                         IFNET_STAT_INC(&sc->sc_if, obytes, 2);
691                                 }
692                         }
693                         m = m_free(m);
694                 }
695
696                 if (clist_putc(FRAME_END, &tp->t_outq)) {
697                         /*
698                          * Not enough room.  Remove a char to make room
699                          * and end the packet normally.
700                          * If you get many collisions (more than one or two
701                          * a day) you probably do not have enough clists
702                          * and you should increase "nclist" in param.c.
703                          */
704                         clist_unputc(&tp->t_outq);
705                         clist_putc(FRAME_END, &tp->t_outq);
706                         IFNET_STAT_INC(&sc->sc_if, collisions, 1);
707                 } else {
708                         IFNET_STAT_INC(&sc->sc_if, obytes, 1);
709                         IFNET_STAT_INC(&sc->sc_if, opackets, 1);
710                 }
711         }
712         lwkt_reltoken(&tp->t_token);
713
714         return 0;
715 }
716
717 /*
718  * Copy data buffer to mbuf chain; add ifnet pointer.
719  */
720 static struct mbuf *
721 sl_btom(struct sl_softc *sc, int len)
722 {
723         struct mbuf *m;
724
725         if (len >= MCLBYTES)
726                 return (NULL);
727
728         MGETHDR(m, M_NOWAIT, MT_DATA);
729         if (m == NULL)
730                 return (NULL);
731
732         /*
733          * If we have more than MHLEN bytes, it's cheaper to
734          * queue the cluster we just filled & allocate a new one
735          * for the input buffer.  Otherwise, fill the mbuf we
736          * allocated above.  Note that code in the input routine
737          * guarantees that packet will fit in a cluster.
738          */
739         if (len >= MHLEN) {
740                 MCLGET(m, M_NOWAIT);
741                 if ((m->m_flags & M_EXT) == 0) {
742                         /*
743                          * we couldn't get a cluster - if memory's this
744                          * low, it's time to start dropping packets.
745                          */
746                         m_free(m);
747                         return (NULL);
748                 }
749         }
750         bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
751         m->m_len = len;
752         m->m_pkthdr.len = len;
753         m->m_pkthdr.rcvif = &sc->sc_if;
754         return (m);
755 }
756
757 /*
758  * tty interface receiver interrupt.
759  */
760 static int
761 slinput(int c, struct tty *tp)
762 {
763         struct sl_softc *sc;
764         struct mbuf *m;
765         int len;
766         u_char chdr[CHDR_LEN];
767
768 #if 0
769         kprintf(" %02x", (unsigned char)c);
770         if ((unsigned char)c == 0xC0)
771                 kprintf("\n");
772 #endif
773
774         lwkt_gettoken(&tp->t_token);
775         tk_nin++;
776         sc = (struct sl_softc *)tp->t_slsc;
777         if (sc == NULL) {
778                 lwkt_reltoken(&tp->t_token);
779 #if 0
780                 kprintf("X");
781 #endif
782                 return 0;
783         }
784         if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
785                 sc->sc_flags |= SC_ERROR;
786                 lwkt_reltoken(&tp->t_token);
787 #if 0
788                 kprintf("Y");
789 #endif
790                 return 0;
791         }
792         c &= TTY_CHARMASK;
793
794         IFNET_STAT_INC(&sc->sc_if, ibytes, 1);
795
796         if (sc->sc_if.if_flags & IFF_DEBUG) {
797                 if (c == ABT_ESC) {
798                         /*
799                          * If we have a previous abort, see whether
800                          * this one is within the time limit.
801                          */
802                         if (sc->sc_abortcount &&
803                             time_uptime >= sc->sc_starttime + ABT_WINDOW)
804                                 sc->sc_abortcount = 0;
805                         /*
806                          * If we see an abort after "idle" time, count it;
807                          * record when the first abort escape arrived.
808                          */
809                         if (time_uptime >= sc->sc_lasttime + ABT_IDLE) {
810                                 if (++sc->sc_abortcount == 1)
811                                         sc->sc_starttime = time_uptime;
812                                 if (sc->sc_abortcount >= ABT_COUNT) {
813                                         slclose(tp,0);
814                                         lwkt_reltoken(&tp->t_token);
815                                         return 0;
816                                 }
817                         }
818                 } else
819                         sc->sc_abortcount = 0;
820                 sc->sc_lasttime = time_uptime;
821         }
822
823         switch (c) {
824
825         case TRANS_FRAME_ESCAPE:
826                 if (sc->sc_escape)
827                         c = FRAME_ESCAPE;
828                 break;
829
830         case TRANS_FRAME_END:
831                 if (sc->sc_escape)
832                         c = FRAME_END;
833                 break;
834
835         case FRAME_ESCAPE:
836                 sc->sc_escape = 1;
837                 lwkt_reltoken(&tp->t_token);
838                 return 0;
839
840         case FRAME_END:
841                 sc->sc_flags &= ~SC_KEEPALIVE;
842                 if(sc->sc_flags & SC_ERROR) {
843                         sc->sc_flags &= ~SC_ERROR;
844                         goto newpack;
845                 }
846                 len = sc->sc_mp - sc->sc_buf;
847                 if (len < 3)
848                         /* less than min length packet - ignore */
849                         goto newpack;
850
851                 if (sc->sc_if.if_bpf) {
852                         /*
853                          * Save the compressed header, so we
854                          * can tack it on later.  Note that we
855                          * will end up copying garbage in some
856                          * cases but this is okay.  We remember
857                          * where the buffer started so we can
858                          * compute the new header length.
859                          */
860                         bcopy(sc->sc_buf, chdr, CHDR_LEN);
861                 }
862
863                 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
864                         if (c & 0x80)
865                                 c = TYPE_COMPRESSED_TCP;
866                         else if (c == TYPE_UNCOMPRESSED_TCP)
867                                 *sc->sc_buf &= 0x4f; /* XXX */
868                         /*
869                          * We've got something that's not an IP packet.
870                          * If compression is enabled, try to decompress it.
871                          * Otherwise, if `auto-enable' compression is on and
872                          * it's a reasonable packet, decompress it and then
873                          * enable compression.  Otherwise, drop it.
874                          */
875                         if (sc->sc_if.if_flags & SC_COMPRESS) {
876                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
877                                                         (u_int)c, &sc->sc_comp);
878                                 if (len <= 0)
879                                         goto error;
880                         } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
881                             c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
882                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
883                                                         (u_int)c, &sc->sc_comp);
884                                 if (len <= 0)
885                                         goto error;
886                                 sc->sc_if.if_flags |= SC_COMPRESS;
887                         } else
888                                 goto error;
889                 }
890                 if (sc->sc_if.if_bpf) {
891                         bpf_gettoken();
892
893                         if (sc->sc_if.if_bpf) {
894                                 /*
895                                  * Put the SLIP pseudo-"link header" in place.
896                                  * We couldn't do this any earlier since
897                                  * decompression probably moved the buffer
898                                  * pointer.  Then, invoke BPF.
899                                  */
900                                 u_char *hp = sc->sc_buf - SLIP_HDRLEN;
901
902                                 hp[SLX_DIR] = SLIPDIR_IN;
903                                 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
904                                 bpf_tap(sc->sc_if.if_bpf, hp,
905                                     len + SLIP_HDRLEN);
906                         }
907
908                         bpf_reltoken();
909                 }
910                 m = sl_btom(sc, len);
911                 if (m == NULL)
912                         goto error;
913
914                 IFNET_STAT_INC(&sc->sc_if, ipackets, 1);
915
916                 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
917                         m_freem(m);
918                         goto newpack;
919                 }
920
921                 if (netisr_queue(NETISR_IP, m)) {
922                         IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
923                         IFNET_STAT_INC(&sc->sc_if, iqdrops, 1);
924                 }
925
926                 goto newpack;
927         }
928         if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) {
929                 *sc->sc_mp++ = c;
930                 sc->sc_escape = 0;
931                 lwkt_reltoken(&tp->t_token);
932                 return 0;
933         }
934
935         /* can't put lower; would miss an extra frame */
936         sc->sc_flags |= SC_ERROR;
937
938 error:
939         IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
940 newpack:
941         sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
942         sc->sc_escape = 0;
943         lwkt_reltoken(&tp->t_token);
944
945         return 0;
946 }
947
948 /*
949  * Process an ioctl request.
950  */
951 static int
952 slioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
953 {
954         struct ifaddr *ifa = (struct ifaddr *)data;
955         struct ifreq *ifr = (struct ifreq *)data;
956         int error = 0;
957
958         switch (cmd) {
959
960         case SIOCSIFFLAGS:
961                 /*
962                  * if.c will set the interface up even if we
963                  * don't want it to.
964                  */
965                 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
966                         ifp->if_flags &= ~IFF_UP;
967                 }
968                 break;
969         case SIOCSIFADDR:
970                 /*
971                  * This is "historical" - set the interface up when
972                  * setting the address.
973                  */
974                 if (ifa->ifa_addr->sa_family == AF_INET) {
975                         if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
976                                 ifp->if_flags |= IFF_UP;
977                 } else {
978                         error = EAFNOSUPPORT;
979                 }
980                 break;
981
982         case SIOCSIFDSTADDR:
983                 if (ifa->ifa_addr->sa_family != AF_INET)
984                         error = EAFNOSUPPORT;
985                 break;
986
987         case SIOCADDMULTI:
988         case SIOCDELMULTI:
989                 break;
990
991         case SIOCSIFMTU:
992                 /*
993                  * Set the interface MTU.
994                  */
995                 if (ifr->ifr_mtu > SLTMAX)
996                         error = EINVAL;
997                 else {
998                         struct tty *tp;
999
1000                         ifp->if_mtu = ifr->ifr_mtu;
1001                         tp = sl_softc[ifp->if_dunit].sc_ttyp;
1002                         if (tp != NULL)
1003                                 clist_alloc_cblocks(&tp->t_outq,
1004                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1);
1005                 }
1006                 break;
1007
1008         default:
1009                 error = EINVAL;
1010         }
1011         return (error);
1012 }
1013
1014 static void
1015 sl_keepalive(void *chan)
1016 {
1017         struct sl_softc *sc = chan;
1018         struct tty *tp = sc->sc_ttyp;
1019         struct pgrp *pg;
1020
1021         lwkt_gettoken(&tp->t_token);
1022         if (sc->sc_keepalive) {
1023                 if (sc->sc_flags & SC_KEEPALIVE) {
1024                         pg = sc->sc_ttyp->t_pgrp;
1025                         if (pg) {
1026                                 pgref(pg);
1027                                 pgsignal (pg, SIGURG, 1);
1028                                 pgrel(pg);
1029                         }
1030                 } else {
1031                         sc->sc_flags |= SC_KEEPALIVE;
1032                 }
1033                 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
1034                                 sl_keepalive, sc);
1035         } else {
1036                 sc->sc_flags &= ~SC_KEEPALIVE;
1037         }
1038         lwkt_reltoken(&tp->t_token);
1039 }
1040
1041 static void
1042 sl_outfill(void *chan)
1043 {
1044         struct sl_softc *sc = chan;
1045         struct tty *tp = sc->sc_ttyp;
1046
1047         lwkt_gettoken(&tp->t_token);
1048
1049         if (sc->sc_outfill && tp != NULL) {
1050                 if (sc->sc_flags & SC_OUTWAIT) {
1051                         IFNET_STAT_INC(&sc->sc_if, obytes, 1);
1052                         clist_putc(FRAME_END, &tp->t_outq);
1053                         (*tp->t_oproc)(tp);
1054                 } else
1055                         sc->sc_flags |= SC_OUTWAIT;
1056                 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
1057                                 sl_outfill, sc);
1058         } else {
1059                 sc->sc_flags &= ~SC_OUTWAIT;
1060         }
1061         lwkt_reltoken(&tp->t_token);
1062 }
1063
1064 MODULE_VERSION(if_sl, 1);