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