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