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.2/sys/net/if_sl.c 121816 2003-10-31 18:32:15Z brooks $
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_mtu = SLMTU;
292 sc->sc_if.if_flags =
293 #ifdef SLIP_IFF_OPTS
294 SLIP_IFF_OPTS;
295 #else
296 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
297 #endif
298 sc->sc_if.if_type = IFT_SLIP;
299 sc->sc_if.if_ioctl = slioctl;
300 sc->sc_if.if_output = sloutput;
301 sc->sc_if.if_snd.ifq_maxlen = 50;
302 sc->sc_fastq.ifq_maxlen = 32;
303 sc->sc_if.if_linkmib = sc;
304 sc->sc_if.if_linkmiblen = sizeof *sc;
305 mtx_init(&sc->sc_fastq.ifq_mtx, "sl_fastq", NULL, MTX_DEF);
306
307 /*
308 * Find a suitable unit number.
309 */
310 for (unit=0; ; unit++) {
311 if (slisstatic(unit))
312 continue;
313 LIST_FOREACH(nc, &sl_list, sl_next) {
314 if (nc->sc_if.if_dunit == unit)
315 continue;
316 }
317 break;
318 }
319 if_initname(&sc->sc_if, "sl", unit);
320 LIST_INSERT_HEAD(&sl_list, sc, sl_next);
321
322 if_attach(&sc->sc_if);
323 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
324
325 return sc;
326 }
327
328
329 /*
330 * Line specific open routine.
331 * Attach the given tty to the first available sl unit.
332 */
333 /* ARGSUSED */
334 static int
335 slopen(dev, tp)
336 dev_t dev;
337 register struct tty *tp;
338 {
339 register struct sl_softc *sc;
340 int s, error;
341
342 error = suser(curthread);
343 if (error)
344 return (error);
345
346 if (tp->t_line == SLIPDISC)
347 return (0);
348
349 if ((sc = slcreate()) == NULL)
350 return (ENOBUFS);
351
352 tp->t_sc = (caddr_t)sc;
353 sc->sc_ttyp = tp;
354 sc->sc_if.if_baudrate = tp->t_ospeed;
355 ttyflush(tp, FREAD | FWRITE);
356 tp->t_line = SLIPDISC;
357
358 /*
359 * We don't use t_canq or t_rawq, so reduce their
360 * cblock resources to 0. Reserve enough cblocks
361 * for t_outq to guarantee that we can fit a full
362 * packet if the SLIP_HIWAT check allows slstart()
363 * to loop. Use the same value for the cblock
364 * limit since the reserved blocks should always
365 * be enough. Reserving cblocks probably makes
366 * the CLISTRESERVE check unnecessary and wasteful.
367 */
368 clist_alloc_cblocks(&tp->t_canq, 0, 0);
369 clist_alloc_cblocks(&tp->t_outq,
370 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
371 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
372 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
373
374 s = splnet();
375 if_up(&sc->sc_if);
376 splx(s);
377 return (0);
378 }
379
380 static void
381 sldestroy(struct sl_softc *sc)
382 {
383 bpfdetach(&sc->sc_if);
384 if_detach(&sc->sc_if);
385 LIST_REMOVE(sc, sl_next);
386 m_free(sc->sc_mbuf);
387 mtx_destroy(&sc->sc_fastq.ifq_mtx);
388 if (sc->bpfbuf)
389 free(sc->bpfbuf, M_SL);
390 free(sc, M_SL);
391 }
392
393 /*
394 * Line specific close routine.
395 * Detach the tty from the sl unit.
396 */
397 static int
398 slclose(tp,flag)
399 struct tty *tp;
400 int flag;
401 {
402 register struct sl_softc *sc;
403 int s;
404
405 ttyflush(tp, FREAD | FWRITE);
406 /*
407 * XXX the placement of the following spl is misleading. tty
408 * interrupts must be blocked across line discipline switches
409 * and throughout closes to avoid races.
410 */
411 s = splimp(); /* actually, max(spltty, splnet) */
412 clist_free_cblocks(&tp->t_outq);
413 tp->t_line = 0;
414 sc = (struct sl_softc *)tp->t_sc;
415 if (sc != NULL) {
416 if (sc->sc_outfill) {
417 sc->sc_outfill = 0;
418 untimeout(sl_outfill, sc, sc->sc_ofhandle);
419 }
420 if (sc->sc_keepalive) {
421 sc->sc_keepalive = 0;
422 untimeout(sl_keepalive, sc, sc->sc_kahandle);
423 }
424 if_down(&sc->sc_if);
425 sc->sc_ttyp = NULL;
426 tp->t_sc = NULL;
427 sldestroy(sc);
428 }
429 splx(s);
430 return 0;
431 }
432
433 /*
434 * Line specific (tty) ioctl routine.
435 * Provide a way to get the sl unit number.
436 */
437 /* ARGSUSED */
438 static int
439 sltioctl(tp, cmd, data, flag, td)
440 struct tty *tp;
441 u_long cmd;
442 caddr_t data;
443 int flag;
444 struct thread *td;
445 {
446 struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc;
447 int s, unit, wasup;
448
449 s = splimp();
450 switch (cmd) {
451 case SLIOCGUNIT:
452 *(int *)data = sc->sc_if.if_dunit;
453 break;
454
455 case SLIOCSUNIT:
456 unit = *(u_int *)data;
457 if (unit < 0) {
458 splx(s);
459 return (ENXIO);
460 }
461 if (sc->sc_if.if_dunit != unit) {
462 LIST_FOREACH(nc, &sl_list, sl_next) {
463 if (nc->sc_if.if_dunit == *(u_int *)data) {
464 splx(s);
465 return (ENXIO);
466 }
467 }
468
469 wasup = sc->sc_if.if_flags & IFF_UP;
470 bpfdetach(&sc->sc_if);
471 if_detach(&sc->sc_if);
472 LIST_REMOVE(sc, sl_next);
473 if_initname(&sc->sc_if, "sl", unit);
474 LIST_INSERT_HEAD(&sl_list, sc, sl_next);
475 if_attach(&sc->sc_if);
476 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
477 if (wasup)
478 if_up(&sc->sc_if);
479 else
480 if_down(&sc->sc_if);
481 clist_alloc_cblocks(&tp->t_outq,
482 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
483 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
484 }
485 slmarkstatic(unit);
486 break;
487
488 case SLIOCSKEEPAL:
489 sc->sc_keepalive = *(u_int *)data * hz;
490 if (sc->sc_keepalive) {
491 sc->sc_flags |= SC_KEEPALIVE;
492 sc->sc_kahandle = timeout(sl_keepalive, sc,
493 sc->sc_keepalive);
494 } else {
495 if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
496 untimeout(sl_keepalive, sc, sc->sc_kahandle);
497 sc->sc_flags &= ~SC_KEEPALIVE;
498 }
499 }
500 break;
501
502 case SLIOCGKEEPAL:
503 *(int *)data = sc->sc_keepalive / hz;
504 break;
505
506 case SLIOCSOUTFILL:
507 sc->sc_outfill = *(u_int *)data * hz;
508 if (sc->sc_outfill) {
509 sc->sc_flags |= SC_OUTWAIT;
510 sc->sc_ofhandle = timeout(sl_outfill, sc,
511 sc->sc_outfill);
512 } else {
513 if ((sc->sc_flags & SC_OUTWAIT) != 0) {
514 untimeout(sl_outfill, sc, sc->sc_ofhandle);
515 sc->sc_flags &= ~SC_OUTWAIT;
516 }
517 }
518 break;
519
520 case SLIOCGOUTFILL:
521 *(int *)data = sc->sc_outfill / hz;
522 break;
523
524 default:
525 splx(s);
526 return (ENOIOCTL);
527 }
528 splx(s);
529 return (0);
530 }
531
532 /*
533 * Queue a packet. Start transmission if not active.
534 * Compression happens in slstart; if we do it here, IP TOS
535 * will cause us to not compress "background" packets, because
536 * ordering gets trashed. It can be done for all packets in slstart.
537 */
538 static int
539 sloutput(ifp, m, dst, rtp)
540 struct ifnet *ifp;
541 register struct mbuf *m;
542 struct sockaddr *dst;
543 struct rtentry *rtp;
544 {
545 register struct sl_softc *sc = ifp->if_softc;
546 register struct ip *ip;
547 register struct ifqueue *ifq;
548 int s;
549
550 /*
551 * `Cannot happen' (see slioctl). Someday we will extend
552 * the line protocol to support other address families.
553 */
554 if (dst->sa_family != AF_INET) {
555 if_printf(ifp, "af%d not supported\n", dst->sa_family);
556 m_freem(m);
557 sc->sc_if.if_noproto++;
558 return (EAFNOSUPPORT);
559 }
560
561 if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
562 m_freem(m);
563 return (ENETDOWN);
564 }
565 if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
566 m_freem(m);
567 return (EHOSTUNREACH);
568 }
569 ifq = &sc->sc_if.if_snd;
570 ip = mtod(m, struct ip *);
571 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
572 m_freem(m);
573 return (ENETRESET); /* XXX ? */
574 }
575 if (ip->ip_tos & IPTOS_LOWDELAY)
576 ifq = &sc->sc_fastq;
577 if (! IF_HANDOFF(ifq, m, NULL)) {
578 sc->sc_if.if_oerrors++;
579 return (ENOBUFS);
580 }
581 s = splimp();
582 if (sc->sc_ttyp->t_outq.c_cc == 0)
583 slstart(sc->sc_ttyp);
584 splx(s);
585 return (0);
586 }
587
588 /*
589 * Start output on interface. Get another datagram
590 * to send from the interface queue and map it to
591 * the interface before starting output.
592 */
593 static int
594 slstart(tp)
595 register struct tty *tp;
596 {
597 register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
598 register struct mbuf *m;
599 register u_char *cp;
600 register struct ip *ip;
601 int s;
602 register int len = 0;
603
604 for (;;) {
605 /*
606 * Call output process whether or not there is more in the
607 * output queue. We are being called in lieu of ttstart
608 * and must do what it would.
609 */
610 (*tp->t_oproc)(tp);
611
612 if (tp->t_outq.c_cc != 0) {
613 if (sc != NULL)
614 sc->sc_flags &= ~SC_OUTWAIT;
615 if (tp->t_outq.c_cc > SLIP_HIWAT)
616 return 0;
617 }
618
619 /*
620 * This happens briefly when the line shuts down.
621 */
622 if (sc == NULL)
623 return 0;
624
625 /*
626 * Get a packet and send it to the interface.
627 */
628 s = splimp();
629 IF_DEQUEUE(&sc->sc_fastq, m);
630 if (m)
631 sc->sc_if.if_omcasts++; /* XXX */
632 else
633 IF_DEQUEUE(&sc->sc_if.if_snd, m);
634 splx(s);
635 if (m == NULL)
636 return 0;
637
638 /*
639 * We do the header compression here rather than in sloutput
640 * because the packets will be out of order if we are using TOS
641 * queueing, and the connection id compression will get
642 * munged when this happens.
643 */
644 if (sc->sc_if.if_bpf) {
645 /*
646 * We need to save the TCP/IP header before it's
647 * compressed. To avoid complicated code, we just
648 * copy the entire packet into a stack buffer (since
649 * this is a serial line, packets should be short
650 * and/or the copy should be negligible cost compared
651 * to the packet transmission time).
652 */
653 register struct mbuf *m1 = m;
654 register u_char *cp;
655
656 if (sc->bpfbuf == NULL)
657 MALLOC(sc->bpfbuf, u_char *,
658 SLTMAX + SLIP_HDRLEN, M_SL, M_NOWAIT);
659
660 if (sc->bpfbuf) {
661 cp = sc->bpfbuf + SLIP_HDRLEN;
662 len = 0;
663 do {
664 register int mlen = m1->m_len;
665
666 bcopy(mtod(m1, caddr_t), cp, mlen);
667 cp += mlen;
668 len += mlen;
669 } while ((m1 = m1->m_next) != NULL);
670 }
671 }
672 ip = mtod(m, struct ip *);
673 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
674 if (sc->sc_if.if_flags & SC_COMPRESS)
675 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
676 &sc->sc_comp, 1);
677 }
678 if (sc->sc_if.if_bpf && sc->bpfbuf) {
679 /*
680 * Put the SLIP pseudo-"link header" in place. The
681 * compressed header is now at the beginning of the
682 * mbuf.
683 */
684 sc->bpfbuf[SLX_DIR] = SLIPDIR_OUT;
685 bcopy(mtod(m, caddr_t), &sc->bpfbuf[SLX_CHDR], CHDR_LEN);
686 BPF_TAP(&sc->sc_if, sc->bpfbuf, len + SLIP_HDRLEN);
687 }
688
689 /*
690 * If system is getting low on clists, just flush our
691 * output queue (if the stuff was important, it'll get
692 * retransmitted). Note that SLTMAX is used instead of
693 * the current if_mtu setting because connections that
694 * have already been established still use the original
695 * (possibly larger) mss.
696 */
697 if (cfreecount < CLISTRESERVE + SLTMAX) {
698 m_freem(m);
699 sc->sc_if.if_collisions++;
700 continue;
701 }
702
703 sc->sc_flags &= ~SC_OUTWAIT;
704 /*
705 * The extra FRAME_END will start up a new packet, and thus
706 * will flush any accumulated garbage. We do this whenever
707 * the line may have been idle for some time.
708 */
709 if (tp->t_outq.c_cc == 0) {
710 ++sc->sc_if.if_obytes;
711 (void) putc(FRAME_END, &tp->t_outq);
712 }
713
714 while (m) {
715 register u_char *ep;
716
717 cp = mtod(m, u_char *); ep = cp + m->m_len;
718 while (cp < ep) {
719 /*
720 * Find out how many bytes in the string we can
721 * handle without doing something special.
722 */
723 register u_char *bp = cp;
724
725 while (cp < ep) {
726 switch (*cp++) {
727 case FRAME_ESCAPE:
728 case FRAME_END:
729 --cp;
730 goto out;
731 }
732 }
733 out:
734 if (cp > bp) {
735 /*
736 * Put n characters at once
737 * into the tty output queue.
738 */
739 if (b_to_q((char *)bp, cp - bp,
740 &tp->t_outq))
741 break;
742 sc->sc_if.if_obytes += cp - bp;
743 }
744 /*
745 * If there are characters left in the mbuf,
746 * the first one must be special..
747 * Put it out in a different form.
748 */
749 if (cp < ep) {
750 if (putc(FRAME_ESCAPE, &tp->t_outq))
751 break;
752 if (putc(*cp++ == FRAME_ESCAPE ?
753 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
754 &tp->t_outq)) {
755 (void) unputc(&tp->t_outq);
756 break;
757 }
758 sc->sc_if.if_obytes += 2;
759 }
760 }
761 m = m_free(m);
762 }
763
764 if (putc(FRAME_END, &tp->t_outq)) {
765 /*
766 * Not enough room. Remove a char to make room
767 * and end the packet normally.
768 * If you get many collisions (more than one or two
769 * a day) you probably do not have enough clists
770 * and you should increase "nclist" in param.c.
771 */
772 (void) unputc(&tp->t_outq);
773 (void) putc(FRAME_END, &tp->t_outq);
774 sc->sc_if.if_collisions++;
775 } else {
776 ++sc->sc_if.if_obytes;
777 sc->sc_if.if_opackets++;
778 }
779 }
780 return 0;
781 }
782
783 /*
784 * Copy data buffer to mbuf chain; add ifnet pointer.
785 */
786 static struct mbuf *
787 sl_btom(sc, len)
788 register struct sl_softc *sc;
789 register int len;
790 {
791 struct mbuf *m, *newm;
792
793 MGETHDR(m, M_DONTWAIT, MT_DATA);
794 if (m == NULL)
795 return (NULL);
796
797 /*
798 * If we have more than MHLEN bytes, it's cheaper to
799 * queue the cluster we just filled & allocate a new one
800 * for the input buffer. Otherwise, fill the mbuf we
801 * allocated above. Note that code in the input routine
802 * guarantees that packet will fit in a cluster.
803 */
804 if (len >= MHLEN) {
805 MCLGET(m, M_DONTWAIT);
806 if ((m->m_flags & M_EXT) == 0) {
807 /*
808 * we couldn't get a cluster - if memory's this
809 * low, it's time to start dropping packets.
810 */
811 (void) m_free(m);
812 return (NULL);
813 }
814 /* Swap the new and old clusters */
815 newm = m;
816 m = sc->sc_mbuf;
817 sc->sc_mbuf = newm;
818 sc->sc_ep = mtod(newm, u_char *) + SLBUFSIZE;
819
820 m->m_data = (caddr_t)sc->sc_buf;
821 } else
822 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
823
824 m->m_len = len;
825 m->m_pkthdr.len = len;
826 m->m_pkthdr.rcvif = &sc->sc_if;
827 return (m);
828 }
829
830 /*
831 * tty interface receiver interrupt.
832 */
833 static int
834 slinput(c, tp)
835 register int c;
836 register struct tty *tp;
837 {
838 register struct sl_softc *sc;
839 register struct mbuf *m;
840 register int len;
841 u_char chdr[CHDR_LEN];
842
843 tk_nin++;
844 sc = (struct sl_softc *)tp->t_sc;
845 if (sc == NULL)
846 return 0;
847 if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
848 sc->sc_flags |= SC_ERROR;
849 return 0;
850 }
851 c &= TTY_CHARMASK;
852
853 ++sc->sc_if.if_ibytes;
854
855 if (sc->sc_if.if_flags & IFF_DEBUG) {
856 if (c == ABT_ESC) {
857 /*
858 * If we have a previous abort, see whether
859 * this one is within the time limit.
860 */
861 if (sc->sc_abortcount &&
862 time_second >= sc->sc_starttime + ABT_WINDOW)
863 sc->sc_abortcount = 0;
864 /*
865 * If we see an abort after "idle" time, count it;
866 * record when the first abort escape arrived.
867 */
868 if (time_second >= sc->sc_lasttime + ABT_IDLE) {
869 if (++sc->sc_abortcount == 1)
870 sc->sc_starttime = time_second;
871 if (sc->sc_abortcount >= ABT_COUNT) {
872 slclose(tp,0);
873 return 0;
874 }
875 }
876 } else
877 sc->sc_abortcount = 0;
878 sc->sc_lasttime = time_second;
879 }
880
881 switch (c) {
882
883 case TRANS_FRAME_ESCAPE:
884 if (sc->sc_escape)
885 c = FRAME_ESCAPE;
886 break;
887
888 case TRANS_FRAME_END:
889 if (sc->sc_escape)
890 c = FRAME_END;
891 break;
892
893 case FRAME_ESCAPE:
894 sc->sc_escape = 1;
895 return 0;
896
897 case FRAME_END:
898 sc->sc_flags &= ~SC_KEEPALIVE;
899 if(sc->sc_flags & SC_ERROR) {
900 sc->sc_flags &= ~SC_ERROR;
901 goto newpack;
902 }
903 len = sc->sc_mp - sc->sc_buf;
904 if (len < 3)
905 /* less than min length packet - ignore */
906 goto newpack;
907
908 if (sc->sc_if.if_bpf) {
909 /*
910 * Save the compressed header, so we
911 * can tack it on later. Note that we
912 * will end up copying garbage in some
913 * cases but this is okay. We remember
914 * where the buffer started so we can
915 * compute the new header length.
916 */
917 bcopy(sc->sc_buf, chdr, CHDR_LEN);
918 }
919
920 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
921 if (c & 0x80)
922 c = TYPE_COMPRESSED_TCP;
923 else if (c == TYPE_UNCOMPRESSED_TCP)
924 *sc->sc_buf &= 0x4f; /* XXX */
925 /*
926 * We've got something that's not an IP packet.
927 * If compression is enabled, try to decompress it.
928 * Otherwise, if `auto-enable' compression is on and
929 * it's a reasonable packet, decompress it and then
930 * enable compression. Otherwise, drop it.
931 */
932 if (sc->sc_if.if_flags & SC_COMPRESS) {
933 len = sl_uncompress_tcp(&sc->sc_buf, len,
934 (u_int)c, &sc->sc_comp);
935 if (len <= 0)
936 goto error;
937 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
938 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
939 len = sl_uncompress_tcp(&sc->sc_buf, len,
940 (u_int)c, &sc->sc_comp);
941 if (len <= 0)
942 goto error;
943 sc->sc_if.if_flags |= SC_COMPRESS;
944 } else
945 goto error;
946 }
947 if (sc->sc_if.if_bpf) {
948 /*
949 * Put the SLIP pseudo-"link header" in place.
950 * We couldn't do this any earlier since
951 * decompression probably moved the buffer
952 * pointer. Then, invoke BPF.
953 */
954 register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
955
956 hp[SLX_DIR] = SLIPDIR_IN;
957 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
958 BPF_TAP(&sc->sc_if, hp, len + SLIP_HDRLEN);
959 }
960 m = sl_btom(sc, len);
961 if (m == NULL)
962 goto error;
963
964 sc->sc_if.if_ipackets++;
965
966 if ((sc->sc_if.if_flags & IFF_UP) == 0) {
967 m_freem(m);
968 goto newpack;
969 }
970 if (! netisr_queue(NETISR_IP, m)) {
971 sc->sc_if.if_ierrors++;
972 sc->sc_if.if_iqdrops++;
973 }
974 goto newpack;
975 }
976 if (sc->sc_mp < sc->sc_ep) {
977 *sc->sc_mp++ = c;
978 sc->sc_escape = 0;
979 return 0;
980 }
981
982 /* can't put lower; would miss an extra frame */
983 sc->sc_flags |= SC_ERROR;
984
985 error:
986 sc->sc_if.if_ierrors++;
987 newpack:
988 sc->sc_mp = sc->sc_buf = sc->sc_ep - SLRMAX;
989 sc->sc_escape = 0;
990 return 0;
991 }
992
993 /*
994 * Process an ioctl request.
995 */
996 static int
997 slioctl(ifp, cmd, data)
998 register struct ifnet *ifp;
999 u_long cmd;
1000 caddr_t data;
1001 {
1002 register struct ifaddr *ifa = (struct ifaddr *)data;
1003 register struct ifreq *ifr = (struct ifreq *)data;
1004 register int s, error = 0;
1005 struct sl_softc *sc = ifp->if_softc;
1006
1007 s = splimp();
1008
1009 switch (cmd) {
1010
1011 case SIOCSIFFLAGS:
1012 /*
1013 * if.c will set the interface up even if we
1014 * don't want it to.
1015 */
1016 if (sc->sc_ttyp == NULL) {
1017 ifp->if_flags &= ~IFF_UP;
1018 }
1019 break;
1020 case SIOCSIFADDR:
1021 /*
1022 * This is "historical" - set the interface up when
1023 * setting the address.
1024 */
1025 if (ifa->ifa_addr->sa_family == AF_INET) {
1026 if (sc->sc_ttyp != NULL)
1027 ifp->if_flags |= IFF_UP;
1028 } else {
1029 error = EAFNOSUPPORT;
1030 }
1031 break;
1032
1033 case SIOCSIFDSTADDR:
1034 if (ifa->ifa_addr->sa_family != AF_INET)
1035 error = EAFNOSUPPORT;
1036 break;
1037
1038 case SIOCADDMULTI:
1039 case SIOCDELMULTI:
1040 break;
1041
1042 case SIOCSIFMTU:
1043 /*
1044 * Set the interface MTU.
1045 */
1046 if (ifr->ifr_mtu > SLTMAX)
1047 error = EINVAL;
1048 else {
1049 struct tty *tp;
1050
1051 ifp->if_mtu = ifr->ifr_mtu;
1052 tp = sc->sc_ttyp;
1053 if (tp != NULL)
1054 clist_alloc_cblocks(&tp->t_outq,
1055 SLIP_HIWAT + 2 * ifp->if_mtu + 1,
1056 SLIP_HIWAT + 2 * ifp->if_mtu + 1);
1057 }
1058 break;
1059
1060 default:
1061 error = EINVAL;
1062 }
1063 splx(s);
1064 return (error);
1065 }
1066
1067 static void
1068 sl_keepalive(chan)
1069 void *chan;
1070 {
1071 struct sl_softc *sc = chan;
1072
1073 if (sc->sc_keepalive) {
1074 if (sc->sc_flags & SC_KEEPALIVE) {
1075 if (sc->sc_ttyp->t_pgrp != NULL) {
1076 PGRP_LOCK(sc->sc_ttyp->t_pgrp);
1077 pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1);
1078 PGRP_UNLOCK(sc->sc_ttyp->t_pgrp);
1079 }
1080 } else
1081 sc->sc_flags |= SC_KEEPALIVE;
1082 sc->sc_kahandle = timeout(sl_keepalive, sc, sc->sc_keepalive);
1083 } else {
1084 sc->sc_flags &= ~SC_KEEPALIVE;
1085 }
1086 }
1087
1088 static void
1089 sl_outfill(chan)
1090 void *chan;
1091 {
1092 struct sl_softc *sc = chan;
1093 register struct tty *tp = sc->sc_ttyp;
1094 int s;
1095
1096 if (sc->sc_outfill && tp != NULL) {
1097 if (sc->sc_flags & SC_OUTWAIT) {
1098 s = splimp ();
1099 ++sc->sc_if.if_obytes;
1100 (void) putc(FRAME_END, &tp->t_outq);
1101 (*tp->t_oproc)(tp);
1102 splx (s);
1103 } else
1104 sc->sc_flags |= SC_OUTWAIT;
1105 sc->sc_ofhandle = timeout(sl_outfill, sc, sc->sc_outfill);
1106 } else {
1107 sc->sc_flags &= ~SC_OUTWAIT;
1108 }
1109 }
Cache object: a0ecdccd9568a60003eecbec2f8735e1
|