suser_* to priv_* conversion
[dragonfly.git] / sys / net / sl / if_sl.c
1 /*
2  * Copyright (c) 1987, 1989, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)if_sl.c     8.6 (Berkeley) 2/1/94
34  * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $
35  * $DragonFly: src/sys/net/sl/if_sl.c,v 1.32 2008/05/14 11:59:23 sephe Exp $
36  */
37
38 /*
39  * Serial Line interface
40  *
41  * Rick Adams
42  * Center for Seismic Studies
43  * 1300 N 17th Street, Suite 1450
44  * Arlington, Virginia 22209
45  * (703)276-7900
46  * rick@seismo.ARPA
47  * seismo!rick
48  *
49  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
50  * N.B.: this belongs in netinet, not net, the way it stands now.
51  * Should have a link-layer type designation, but wouldn't be
52  * backwards-compatible.
53  *
54  * Converted to 4.3BSD Beta by Chris Torek.
55  * Other changes made at Berkeley, based in part on code by Kirk Smith.
56  * W. Jolitz added slip abort.
57  *
58  * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
59  * Added priority queuing for "interactive" traffic; hooks for TCP
60  * header compression; ICMP filtering (at 2400 baud, some cretin
61  * pinging you can use up all your bandwidth).  Made low clist behavior
62  * more robust and slightly less likely to hang serial line.
63  * Sped up a bunch of things.
64  */
65
66 #include "use_sl.h"
67
68 #include "opt_inet.h"
69 #if !defined(KLD_MODULE)
70 #include "opt_slip.h"
71 #endif
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/priv.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/dkstat.h>
79 #include <sys/socket.h>
80 #include <sys/sockio.h>
81 #include <sys/fcntl.h>
82 #include <sys/signalvar.h>
83 #include <sys/tty.h>
84 #include <sys/clist.h>
85 #include <sys/kernel.h>
86 #include <sys/conf.h>
87 #include <sys/thread2.h>
88
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/ifq_var.h>
92 #include <net/netisr.h>
93
94 #if INET
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
99 #else
100 #error "Huh? Slip without inet?"
101 #endif
102
103 #include <net/slcompress.h>
104 #include "if_slvar.h"
105 #include <net/slip.h>
106
107 #include <net/bpf.h>
108
109 static void slattach (void *);
110 PSEUDO_SET(slattach, if_sl);
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      roundup(50,CBSIZE)
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 =
213 #ifdef SLIP_IFF_OPTS
214                     SLIP_IFF_OPTS;
215 #else
216                     IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
217 #endif
218                 sc->sc_if.if_type = IFT_SLIP;
219                 sc->sc_if.if_ioctl = slioctl;
220                 sc->sc_if.if_output = sloutput;
221                 ifq_set_maxlen(&sc->sc_if.if_snd, 50);
222                 ifq_set_ready(&sc->sc_if.if_snd);
223                 sc->sc_fastq.ifq_maxlen = 32;
224                 sc->sc_if.if_linkmib = sc;
225                 sc->sc_if.if_linkmiblen = sizeof *sc;
226                 callout_init(&sc->sc_oftimeout);
227                 callout_init(&sc->sc_katimeout);
228                 if_attach(&sc->sc_if, NULL);
229                 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
230         }
231 }
232
233 static int
234 slinit(struct sl_softc *sc)
235 {
236         if (sc->sc_ep == NULL)
237                 sc->sc_ep = kmalloc(SLBUFSIZE, M_DEVBUF, M_WAITOK);
238         sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
239         sc->sc_mp = sc->sc_buf;
240         sl_compress_init(&sc->sc_comp, -1);
241         return (1);
242 }
243
244 /*
245  * Line specific open routine.
246  * Attach the given tty to the first available sl unit.
247  */
248 /* ARGSUSED */
249 static int
250 slopen(cdev_t dev, struct tty *tp)
251 {
252         struct sl_softc *sc;
253         int nsl;
254         int error;
255         struct thread *td = curthread;  /* XXX */
256
257         error = priv_check(td, PRIV_ROOT);
258         if (error)
259                 return (error);
260
261         if (tp->t_line == SLIPDISC)
262                 return (0);
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                                 return (ENOBUFS);
268                         tp->t_sc = (caddr_t)sc;
269                         sc->sc_ttyp = tp;
270                         sc->sc_if.if_baudrate = tp->t_ospeed;
271                         ttyflush(tp, FREAD | FWRITE);
272
273                         tp->t_line = SLIPDISC;
274                         /*
275                          * We don't use t_canq or t_rawq, so reduce their
276                          * cblock resources to 0.  Reserve enough cblocks
277                          * for t_outq to guarantee that we can fit a full
278                          * packet if the SLIP_HIWAT check allows slstart()
279                          * to loop.  Use the same value for the cblock
280                          * limit since the reserved blocks should always
281                          * be enough.  Reserving cblocks probably makes
282                          * the CLISTRESERVE check unnecessary and wasteful.
283                          */
284                         clist_alloc_cblocks(&tp->t_canq, 0, 0);
285                         clist_alloc_cblocks(&tp->t_outq,
286                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
287                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
288                         clist_alloc_cblocks(&tp->t_rawq, 0, 0);
289
290                         crit_enter();
291                         if_up(&sc->sc_if);
292                         crit_exit();
293                         return (0);
294                 }
295         return (ENXIO);
296 }
297
298 /*
299  * Line specific close routine.
300  * Detach the tty from the sl unit.
301  */
302 static int
303 slclose(struct tty *tp, int flag)
304 {
305         struct sl_softc *sc;
306
307         ttyflush(tp, FREAD | FWRITE);
308         crit_enter();
309
310         clist_free_cblocks(&tp->t_outq);
311         tp->t_line = 0;
312         sc = (struct sl_softc *)tp->t_sc;
313         if (sc != NULL) {
314                 if (sc->sc_outfill) {
315                         sc->sc_outfill = 0;
316                         callout_stop(&sc->sc_oftimeout);
317                 }
318                 if (sc->sc_keepalive) {
319                         sc->sc_keepalive = 0;
320                         callout_stop(&sc->sc_katimeout);
321                 }
322                 if_down(&sc->sc_if);
323                 sc->sc_flags &= SC_STATIC;
324                 sc->sc_ttyp = NULL;
325                 tp->t_sc = NULL;
326                 if (sc->sc_ep) {
327                         kfree(sc->sc_ep, M_DEVBUF);
328                         sc->sc_ep = NULL;
329                 }
330                 sc->sc_mp = 0;
331                 sc->sc_buf = 0;
332         }
333         crit_exit();
334         return 0;
335 }
336
337 /*
338  * Line specific (tty) ioctl routine.
339  * Provide a way to get the sl unit number.
340  */
341 /* ARGSUSED */
342 static int
343 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
344 {
345         struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc, *tmpnc;
346         int nsl;
347
348         crit_enter();
349
350         switch (cmd) {
351         case SLIOCGUNIT:
352                 *(int *)data = sc->sc_if.if_dunit;
353                 break;
354
355         case SLIOCSUNIT:
356                 if (sc->sc_if.if_dunit != *(u_int *)data) {
357                         for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
358                                 if (   nc->sc_if.if_dunit == *(u_int *)data
359                                     && nc->sc_ttyp == NULL
360                                    ) {
361                                         tmpnc = kmalloc(sizeof *tmpnc, M_TEMP,
362                                                        M_WAITOK);
363                                         *tmpnc = *nc;
364                                         *nc = *sc;
365                                         nc->sc_if = tmpnc->sc_if;
366                                         tmpnc->sc_if = sc->sc_if;
367                                         *sc = *tmpnc;
368                                         kfree(tmpnc, M_TEMP);
369                                         if (sc->sc_if.if_flags & IFF_UP) {
370                                                 if_down(&sc->sc_if);
371                                                 if (!(nc->sc_if.if_flags & IFF_UP))
372                                                         if_up(&nc->sc_if);
373                                         } else if (nc->sc_if.if_flags & IFF_UP)
374                                                 if_down(&nc->sc_if);
375                                         sc->sc_flags &= ~SC_STATIC;
376                                         sc->sc_flags |= (nc->sc_flags & SC_STATIC);
377                                         tp->t_sc = sc = nc;
378                                         clist_alloc_cblocks(&tp->t_outq,
379                                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
380                                             SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
381                                         sl_compress_init(&sc->sc_comp, -1);
382                                         goto slfound;
383                                 }
384                         }
385                         crit_exit();
386                         return (ENXIO);
387                 }
388         slfound:
389                 sc->sc_flags |= SC_STATIC;
390                 break;
391
392         case SLIOCSKEEPAL:
393                 sc->sc_keepalive = *(u_int *)data * hz;
394                 if (sc->sc_keepalive) {
395                         sc->sc_flags |= SC_KEEPALIVE;
396                         callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
397                                         sl_keepalive, sc);
398                 } else {
399                         if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
400                                 callout_stop(&sc->sc_katimeout);
401                                 sc->sc_flags &= ~SC_KEEPALIVE;
402                         }
403                 }
404                 break;
405
406         case SLIOCGKEEPAL:
407                 *(int *)data = sc->sc_keepalive / hz;
408                 break;
409
410         case SLIOCSOUTFILL:
411                 sc->sc_outfill = *(u_int *)data * hz;
412                 if (sc->sc_outfill) {
413                         sc->sc_flags |= SC_OUTWAIT;
414                         callout_reset(&sc->sc_oftimeout, sc->sc_outfill, 
415                                         sl_outfill, sc);
416                 } else {
417                         if ((sc->sc_flags & SC_OUTWAIT) != 0) {
418                                 callout_stop(&sc->sc_oftimeout);
419                                 sc->sc_flags &= ~SC_OUTWAIT;
420                         }
421                 }
422                 break;
423
424         case SLIOCGOUTFILL:
425                 *(int *)data = sc->sc_outfill / hz;
426                 break;
427
428         default:
429                 crit_exit();
430                 return (ENOIOCTL);
431         }
432         crit_exit();
433         return (0);
434 }
435
436 /*
437  * Queue a packet.  Start transmission if not active.
438  * Compression happens in slstart; if we do it here, IP TOS
439  * will cause us to not compress "background" packets, because
440  * ordering gets trashed.  It can be done for all packets in slstart.
441  */
442 static int
443 sloutput_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
444                     struct rtentry *rtp)
445 {
446         struct sl_softc *sc = &sl_softc[ifp->if_dunit];
447         struct ip *ip;
448         int error;
449         struct altq_pktattr pktattr;
450
451         ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
452
453         /*
454          * `Cannot happen' (see slioctl).  Someday we will extend
455          * the line protocol to support other address families.
456          */
457         if (dst->sa_family != AF_INET) {
458                 kprintf("%s: af%d not supported\n", sc->sc_if.if_xname,
459                         dst->sa_family);
460                 m_freem(m);
461                 sc->sc_if.if_noproto++;
462                 return (EAFNOSUPPORT);
463         }
464
465         if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
466                 m_freem(m);
467                 return (ENETDOWN);
468         }
469         if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
470                 m_freem(m);
471                 return (EHOSTUNREACH);
472         }
473         ip = mtod(m, struct ip *);
474         if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
475                 m_freem(m);
476                 return (ENETRESET);             /* XXX ? */
477         }
478
479         crit_enter();
480
481         if ((ip->ip_tos & IPTOS_LOWDELAY) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
482                 if (IF_QFULL(&sc->sc_fastq)) {
483                         IF_DROP(&sc->sc_fastq);
484                         m_freem(m);
485                         error = ENOBUFS;
486                 } else {
487                         IF_ENQUEUE(&sc->sc_fastq, m);
488                         error = 0;
489                 }
490         } else {
491                 error = ifq_enqueue(&sc->sc_if.if_snd, m, &pktattr);
492         }
493         if (error) {
494                 sc->sc_if.if_oerrors++;
495                 crit_exit();
496                 return (error);
497         }
498         if (sc->sc_ttyp->t_outq.c_cc == 0)
499                 slstart(sc->sc_ttyp);
500         crit_exit();
501         return (0);
502 }
503
504 static int
505 sloutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
506          struct rtentry *rtp)
507 {
508         int error;
509
510         lwkt_serialize_enter(ifp->if_serializer);
511         error = sloutput_serialized(ifp, m, dst, rtp);
512         lwkt_serialize_exit(ifp->if_serializer);
513
514         return error;
515 }
516
517 /*
518  * Start output on interface.  Get another datagram
519  * to send from the interface queue and map it to
520  * the interface before starting output.
521  */
522 static int
523 slstart(struct tty *tp)
524 {
525         struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
526         struct mbuf *m;
527         u_char *cp;
528         struct ip *ip;
529         u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
530         int len = 0;
531
532         for (;;) {
533                 /*
534                  * Call output process whether or not there is more in the
535                  * output queue.  We are being called in lieu of ttstart
536                  * and must do what it would.
537                  */
538                 (*tp->t_oproc)(tp);
539
540                 if (tp->t_outq.c_cc != 0) {
541                         if (sc != NULL)
542                                 sc->sc_flags &= ~SC_OUTWAIT;
543                         if (tp->t_outq.c_cc > SLIP_HIWAT)
544                                 return 0;
545                 }
546
547                 /*
548                  * This happens briefly when the line shuts down.
549                  */
550                 if (sc == NULL)
551                         return 0;
552
553                 /*
554                  * Get a packet and send it to the interface.
555                  */
556                 crit_enter();
557                 IF_DEQUEUE(&sc->sc_fastq, m);
558                 if (m)
559                         sc->sc_if.if_omcasts++;         /* XXX */
560                 else
561                         m = ifq_dequeue(&sc->sc_if.if_snd, NULL);
562                 crit_exit();
563                 if (m == NULL)
564                         return 0;
565
566                 /*
567                  * We do the header compression here rather than in sloutput
568                  * because the packets will be out of order if we are using TOS
569                  * queueing, and the connection id compression will get
570                  * munged when this happens.
571                  */
572                 if (sc->sc_if.if_bpf) {
573                         /*
574                          * We need to save the TCP/IP header before it's
575                          * compressed.  To avoid complicated code, we just
576                          * copy the entire packet into a stack buffer (since
577                          * this is a serial line, packets should be short
578                          * and/or the copy should be negligible cost compared
579                          * to the packet transmission time).
580                          */
581                         struct mbuf *m1 = m;
582                         u_char *cp = bpfbuf + SLIP_HDRLEN;
583
584                         len = 0;
585                         do {
586                                 int mlen = m1->m_len;
587
588                                 bcopy(mtod(m1, caddr_t), cp, mlen);
589                                 cp += mlen;
590                                 len += mlen;
591                         } while ((m1 = m1->m_next) != NULL);
592                 }
593                 ip = mtod(m, struct ip *);
594                 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
595                         if (sc->sc_if.if_flags & SC_COMPRESS)
596                                 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
597                                     &sc->sc_comp, 1);
598                 }
599                 if (sc->sc_if.if_bpf) {
600                         /*
601                          * Put the SLIP pseudo-"link header" in place.  The
602                          * compressed header is now at the beginning of the
603                          * mbuf.
604                          */
605                         bpfbuf[SLX_DIR] = SLIPDIR_OUT;
606                         bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
607                         bpf_tap(sc->sc_if.if_bpf, bpfbuf, len + SLIP_HDRLEN);
608                 }
609
610                 /*
611                  * If system is getting low on clists, just flush our
612                  * output queue (if the stuff was important, it'll get
613                  * retransmitted). Note that SLTMAX is used instead of
614                  * the current if_mtu setting because connections that
615                  * have already been established still use the original
616                  * (possibly larger) mss.
617                  */
618                 if (cfreecount < CLISTRESERVE + SLTMAX) {
619                         m_freem(m);
620                         sc->sc_if.if_collisions++;
621                         continue;
622                 }
623
624                 sc->sc_flags &= ~SC_OUTWAIT;
625                 /*
626                  * The extra FRAME_END will start up a new packet, and thus
627                  * will flush any accumulated garbage.  We do this whenever
628                  * the line may have been idle for some time.
629                  */
630                 if (tp->t_outq.c_cc == 0) {
631                         ++sc->sc_if.if_obytes;
632                         clist_putc(FRAME_END, &tp->t_outq);
633                 }
634
635                 while (m) {
636                         u_char *ep;
637
638                         cp = mtod(m, u_char *); ep = cp + m->m_len;
639                         while (cp < ep) {
640                                 /*
641                                  * Find out how many bytes in the string we can
642                                  * handle without doing something special.
643                                  */
644                                 u_char *bp = cp;
645
646                                 while (cp < ep) {
647                                         switch (*cp++) {
648                                         case FRAME_ESCAPE:
649                                         case FRAME_END:
650                                                 --cp;
651                                                 goto out;
652                                         }
653                                 }
654                                 out:
655                                 if (cp > bp) {
656                                         /*
657                                          * Put n characters at once
658                                          * into the tty output queue.
659                                          */
660                                         if (b_to_q((char *)bp, cp - bp,
661                                             &tp->t_outq))
662                                                 break;
663                                         sc->sc_if.if_obytes += cp - bp;
664                                 }
665                                 /*
666                                  * If there are characters left in the mbuf,
667                                  * the first one must be special..
668                                  * Put it out in a different form.
669                                  */
670                                 if (cp < ep) {
671                                         if (clist_putc(FRAME_ESCAPE, &tp->t_outq))
672                                                 break;
673                                         if (clist_putc(*cp++ == FRAME_ESCAPE ?
674                                            TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
675                                            &tp->t_outq)) {
676                                                 clist_unputc(&tp->t_outq);
677                                                 break;
678                                         }
679                                         sc->sc_if.if_obytes += 2;
680                                 }
681                         }
682                         m = m_free(m);
683                 }
684
685                 if (clist_putc(FRAME_END, &tp->t_outq)) {
686                         /*
687                          * Not enough room.  Remove a char to make room
688                          * and end the packet normally.
689                          * If you get many collisions (more than one or two
690                          * a day) you probably do not have enough clists
691                          * and you should increase "nclist" in param.c.
692                          */
693                         clist_unputc(&tp->t_outq);
694                         clist_putc(FRAME_END, &tp->t_outq);
695                         sc->sc_if.if_collisions++;
696                 } else {
697                         ++sc->sc_if.if_obytes;
698                         sc->sc_if.if_opackets++;
699                 }
700         }
701         return 0;
702 }
703
704 /*
705  * Copy data buffer to mbuf chain; add ifnet pointer.
706  */
707 static struct mbuf *
708 sl_btom(struct sl_softc *sc, int len)
709 {
710         struct mbuf *m;
711
712         if (len >= MCLBYTES)
713                 return (NULL);
714
715         MGETHDR(m, MB_DONTWAIT, MT_DATA);
716         if (m == NULL)
717                 return (NULL);
718
719         /*
720          * If we have more than MHLEN bytes, it's cheaper to
721          * queue the cluster we just filled & allocate a new one
722          * for the input buffer.  Otherwise, fill the mbuf we
723          * allocated above.  Note that code in the input routine
724          * guarantees that packet will fit in a cluster.
725          */
726         if (len >= MHLEN) {
727                 MCLGET(m, MB_DONTWAIT);
728                 if ((m->m_flags & M_EXT) == 0) {
729                         /*
730                          * we couldn't get a cluster - if memory's this
731                          * low, it's time to start dropping packets.
732                          */
733                         m_free(m);
734                         return (NULL);
735                 }
736         }
737         bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
738         m->m_len = len;
739         m->m_pkthdr.len = len;
740         m->m_pkthdr.rcvif = &sc->sc_if;
741         return (m);
742 }
743
744 /*
745  * tty interface receiver interrupt.
746  */
747 static int
748 slinput(int c, struct tty *tp)
749 {
750         struct sl_softc *sc;
751         struct mbuf *m;
752         int len;
753         u_char chdr[CHDR_LEN];
754
755         tk_nin++;
756         sc = (struct sl_softc *)tp->t_sc;
757         if (sc == NULL)
758                 return 0;
759         if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
760                 sc->sc_flags |= SC_ERROR;
761                 return 0;
762         }
763         c &= TTY_CHARMASK;
764
765         ++sc->sc_if.if_ibytes;
766
767         if (sc->sc_if.if_flags & IFF_DEBUG) {
768                 if (c == ABT_ESC) {
769                         /*
770                          * If we have a previous abort, see whether
771                          * this one is within the time limit.
772                          */
773                         if (sc->sc_abortcount &&
774                             time_second >= sc->sc_starttime + ABT_WINDOW)
775                                 sc->sc_abortcount = 0;
776                         /*
777                          * If we see an abort after "idle" time, count it;
778                          * record when the first abort escape arrived.
779                          */
780                         if (time_second >= sc->sc_lasttime + ABT_IDLE) {
781                                 if (++sc->sc_abortcount == 1)
782                                         sc->sc_starttime = time_second;
783                                 if (sc->sc_abortcount >= ABT_COUNT) {
784                                         slclose(tp,0);
785                                         return 0;
786                                 }
787                         }
788                 } else
789                         sc->sc_abortcount = 0;
790                 sc->sc_lasttime = time_second;
791         }
792
793         switch (c) {
794
795         case TRANS_FRAME_ESCAPE:
796                 if (sc->sc_escape)
797                         c = FRAME_ESCAPE;
798                 break;
799
800         case TRANS_FRAME_END:
801                 if (sc->sc_escape)
802                         c = FRAME_END;
803                 break;
804
805         case FRAME_ESCAPE:
806                 sc->sc_escape = 1;
807                 return 0;
808
809         case FRAME_END:
810                 sc->sc_flags &= ~SC_KEEPALIVE;
811                 if(sc->sc_flags & SC_ERROR) {
812                         sc->sc_flags &= ~SC_ERROR;
813                         goto newpack;
814                 }
815                 len = sc->sc_mp - sc->sc_buf;
816                 if (len < 3)
817                         /* less than min length packet - ignore */
818                         goto newpack;
819
820                 if (sc->sc_if.if_bpf) {
821                         /*
822                          * Save the compressed header, so we
823                          * can tack it on later.  Note that we
824                          * will end up copying garbage in some
825                          * cases but this is okay.  We remember
826                          * where the buffer started so we can
827                          * compute the new header length.
828                          */
829                         bcopy(sc->sc_buf, chdr, CHDR_LEN);
830                 }
831
832                 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
833                         if (c & 0x80)
834                                 c = TYPE_COMPRESSED_TCP;
835                         else if (c == TYPE_UNCOMPRESSED_TCP)
836                                 *sc->sc_buf &= 0x4f; /* XXX */
837                         /*
838                          * We've got something that's not an IP packet.
839                          * If compression is enabled, try to decompress it.
840                          * Otherwise, if `auto-enable' compression is on and
841                          * it's a reasonable packet, decompress it and then
842                          * enable compression.  Otherwise, drop it.
843                          */
844                         if (sc->sc_if.if_flags & SC_COMPRESS) {
845                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
846                                                         (u_int)c, &sc->sc_comp);
847                                 if (len <= 0)
848                                         goto error;
849                         } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
850                             c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
851                                 len = sl_uncompress_tcp(&sc->sc_buf, len,
852                                                         (u_int)c, &sc->sc_comp);
853                                 if (len <= 0)
854                                         goto error;
855                                 sc->sc_if.if_flags |= SC_COMPRESS;
856                         } else
857                                 goto error;
858                 }
859                 if (sc->sc_if.if_bpf) {
860                         /*
861                          * Put the SLIP pseudo-"link header" in place.
862                          * We couldn't do this any earlier since
863                          * decompression probably moved the buffer
864                          * pointer.  Then, invoke BPF.
865                          */
866                         u_char *hp = sc->sc_buf - SLIP_HDRLEN;
867
868                         hp[SLX_DIR] = SLIPDIR_IN;
869                         bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
870                         bpf_tap(sc->sc_if.if_bpf, hp, len + SLIP_HDRLEN);
871                 }
872                 m = sl_btom(sc, len);
873                 if (m == NULL)
874                         goto error;
875
876                 sc->sc_if.if_ipackets++;
877
878                 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
879                         m_freem(m);
880                         goto newpack;
881                 }
882
883                 if (netisr_queue(NETISR_IP, m)) {
884                         sc->sc_if.if_ierrors++;
885                         sc->sc_if.if_iqdrops++;
886                 }
887
888                 goto newpack;
889         }
890         if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) {
891                 *sc->sc_mp++ = c;
892                 sc->sc_escape = 0;
893                 return 0;
894         }
895
896         /* can't put lower; would miss an extra frame */
897         sc->sc_flags |= SC_ERROR;
898
899 error:
900         sc->sc_if.if_ierrors++;
901 newpack:
902         sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
903         sc->sc_escape = 0;
904         return 0;
905 }
906
907 /*
908  * Process an ioctl request.
909  */
910 static int
911 slioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
912 {
913         struct ifaddr *ifa = (struct ifaddr *)data;
914         struct ifreq *ifr = (struct ifreq *)data;
915         int error = 0;
916
917         crit_enter();
918
919         switch (cmd) {
920
921         case SIOCSIFFLAGS:
922                 /*
923                  * if.c will set the interface up even if we
924                  * don't want it to.
925                  */
926                 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
927                         ifp->if_flags &= ~IFF_UP;
928                 }
929                 break;
930         case SIOCSIFADDR:
931                 /*
932                  * This is "historical" - set the interface up when
933                  * setting the address.
934                  */
935                 if (ifa->ifa_addr->sa_family == AF_INET) {
936                         if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
937                                 ifp->if_flags |= IFF_UP;
938                 } else {
939                         error = EAFNOSUPPORT;
940                 }
941                 break;
942
943         case SIOCSIFDSTADDR:
944                 if (ifa->ifa_addr->sa_family != AF_INET)
945                         error = EAFNOSUPPORT;
946                 break;
947
948         case SIOCADDMULTI:
949         case SIOCDELMULTI:
950                 break;
951
952         case SIOCSIFMTU:
953                 /*
954                  * Set the interface MTU.
955                  */
956                 if (ifr->ifr_mtu > SLTMAX)
957                         error = EINVAL;
958                 else {
959                         struct tty *tp;
960
961                         ifp->if_mtu = ifr->ifr_mtu;
962                         tp = sl_softc[ifp->if_dunit].sc_ttyp;
963                         if (tp != NULL)
964                                 clist_alloc_cblocks(&tp->t_outq,
965                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1,
966                                     SLIP_HIWAT + 2 * ifp->if_mtu + 1);
967                 }
968                 break;
969
970         default:
971                 error = EINVAL;
972         }
973
974         crit_exit();
975         return (error);
976 }
977
978 static void
979 sl_keepalive(void *chan)
980 {
981         struct sl_softc *sc = chan;
982
983         if (sc->sc_keepalive) {
984                 if (sc->sc_flags & SC_KEEPALIVE)
985                         pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1);
986                 else
987                         sc->sc_flags |= SC_KEEPALIVE;
988                 callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
989                                 sl_keepalive, sc);
990         } else {
991                 sc->sc_flags &= ~SC_KEEPALIVE;
992         }
993 }
994
995 static void
996 sl_outfill(void *chan)
997 {
998         struct sl_softc *sc = chan;
999         struct tty *tp = sc->sc_ttyp;
1000
1001         if (sc->sc_outfill && tp != NULL) {
1002                 if (sc->sc_flags & SC_OUTWAIT) {
1003                         crit_enter();
1004                         ++sc->sc_if.if_obytes;
1005                         clist_putc(FRAME_END, &tp->t_outq);
1006                         (*tp->t_oproc)(tp);
1007                         crit_exit();
1008                 } else
1009                         sc->sc_flags |= SC_OUTWAIT;
1010                 callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
1011                                 sl_outfill, sc);
1012         } else {
1013                 sc->sc_flags &= ~SC_OUTWAIT;
1014         }
1015 }