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