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