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