if_sl.c revision 1.63 1 /* $NetBSD: if_sl.c,v 1.63 2000/12/18 20:41:44 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)if_sl.c 8.9 (Berkeley) 1/9/95
36 */
37
38 /*
39 * Serial Line interface
40 *
41 * Rick Adams
42 * Center for Seismic Studies
43 * 1300 N 17th Street, Suite 1450
44 * Arlington, Virginia 22209
45 * (703)276-7900
46 * rick (at) seismo.ARPA
47 * seismo!rick
48 *
49 * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
50 * N.B.: this belongs in netinet, not net, the way it stands now.
51 * Should have a link-layer type designation, but wouldn't be
52 * backwards-compatible.
53 *
54 * Converted to 4.3BSD Beta by Chris Torek.
55 * Other changes made at Berkeley, based in part on code by Kirk Smith.
56 * W. Jolitz added slip abort.
57 *
58 * Hacked almost beyond recognition by Van Jacobson (van (at) helios.ee.lbl.gov).
59 * Added priority queuing for "interactive" traffic; hooks for TCP
60 * header compression; ICMP filtering (at 2400 baud, some cretin
61 * pinging you can use up all your bandwidth). Made low clist behavior
62 * more robust and slightly less likely to hang serial line.
63 * Sped up a bunch of things.
64 *
65 * Note that splimp() is used throughout to block both (tty) input
66 * interrupts and network activity; thus, splimp must be >= spltty.
67 */
68
69 #include "sl.h"
70 #if NSL > 0
71
72 #include "opt_inet.h"
73 #include "bpfilter.h"
74
75 #include <sys/param.h>
76 #include <sys/proc.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/buf.h>
80 #include <sys/dkstat.h>
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
83 #include <sys/file.h>
84 #include <sys/conf.h>
85 #include <sys/tty.h>
86 #include <sys/kernel.h>
87 #if __NetBSD__
88 #include <sys/systm.h>
89 #endif
90
91 #include <machine/cpu.h>
92
93 #include <net/if.h>
94 #include <net/if_types.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
97
98 #ifdef INET
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/in_var.h>
102 #include <netinet/ip.h>
103 #else
104 #error Slip without inet?
105 #endif
106
107 #include <net/slcompress.h>
108 #include <net/if_slvar.h>
109 #include <net/slip.h>
110
111 #if NBPFILTER > 0
112 #include <sys/time.h>
113 #include <net/bpf.h>
114 #endif
115
116 /*
117 * SLMAX is a hard limit on input packet size. To simplify the code
118 * and improve performance, we require that packets fit in an mbuf
119 * cluster, and if we get a compressed packet, there's enough extra
120 * room to expand the header into a max length tcp/ip header (128
121 * bytes). So, SLMAX can be at most
122 * MCLBYTES - 128
123 *
124 * SLMTU is a hard limit on output packet size. To insure good
125 * interactive response, SLMTU wants to be the smallest size that
126 * amortizes the header cost. (Remember that even with
127 * type-of-service queuing, we have to wait for any in-progress
128 * packet to finish. I.e., we wait, on the average, 1/2 * mtu /
129 * cps, where cps is the line speed in characters per second.
130 * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line. The
131 * average compressed header size is 6-8 bytes so any MTU > 90
132 * bytes will give us 90% of the line bandwidth. A 100ms wait is
133 * tolerable (500ms is not), so want an MTU around 296. (Since TCP
134 * will send 256 byte segments (to allow for 40 byte headers), the
135 * typical packet size on the wire will be around 260 bytes). In
136 * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
137 * leave the interface MTU relatively high (so we don't IP fragment
138 * when acting as a gateway to someone using a stupid MTU).
139 *
140 * Similar considerations apply to SLIP_HIWAT: It's the amount of
141 * data that will be queued 'downstream' of us (i.e., in clists
142 * waiting to be picked up by the tty output interrupt). If we
143 * queue a lot of data downstream, it's immune to our t.o.s. queuing.
144 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
145 * telnet/ftp will see a 1 sec wait, independent of the mtu (the
146 * wait is dependent on the ftp window size but that's typically
147 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize
148 * the cost (in idle time on the wire) of the tty driver running
149 * off the end of its clists & having to call back slstart for a
150 * new packet. For a tty interface with any buffering at all, this
151 * cost will be zero. Even with a totally brain dead interface (like
152 * the one on a typical workstation), the cost will be <= 1 character
153 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
154 * at most 1% while maintaining good interactive response.
155 */
156 #if NBPFILTER > 0
157 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
158 #else
159 #define BUFOFFSET (128+sizeof(struct ifnet **))
160 #endif
161 #define SLMAX (MCLBYTES - BUFOFFSET)
162 #define SLBUFSIZE (SLMAX + BUFOFFSET)
163 #ifndef SLMTU
164 #define SLMTU 296
165 #endif
166 #if (SLMTU < 3)
167 #error SLMTU way too small.
168 #endif
169 #define SLIP_HIWAT roundup(50,CBSIZE)
170 #ifndef __NetBSD__ /* XXX - cgd */
171 #define CLISTRESERVE 1024 /* Can't let clists get too low */
172 #endif /* !__NetBSD__ */
173
174 /*
175 * SLIP ABORT ESCAPE MECHANISM:
176 * (inspired by HAYES modem escape arrangement)
177 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
178 * within window time signals a "soft" exit from slip mode by remote end
179 * if the IFF_DEBUG flag is on.
180 */
181 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
182 #define ABT_IDLE 1 /* in seconds - idle before an escape */
183 #define ABT_COUNT 3 /* count of escapes for abort */
184 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
185
186 struct sl_softc sl_softc[NSL];
187
188 #define FRAME_END 0xc0 /* Frame End */
189 #define FRAME_ESCAPE 0xdb /* Frame Esc */
190 #define TRANS_FRAME_END 0xdc /* transposed frame end */
191 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
192
193 static int slinit __P((struct sl_softc *));
194 static struct mbuf *sl_btom __P((struct sl_softc *, int));
195
196 /*
197 * Called from boot code to establish sl interfaces.
198 */
199 void
200 slattach()
201 {
202 struct sl_softc *sc;
203 int i = 0;
204
205 for (sc = sl_softc; i < NSL; sc++) {
206 sc->sc_unit = i; /* XXX */
207 sprintf(sc->sc_if.if_xname, "sl%d", i++);
208 sc->sc_if.if_softc = sc;
209 sc->sc_if.if_mtu = SLMTU;
210 sc->sc_if.if_flags =
211 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
212 sc->sc_if.if_type = IFT_SLIP;
213 sc->sc_if.if_ioctl = slioctl;
214 sc->sc_if.if_output = sloutput;
215 sc->sc_if.if_dlt = DLT_SLIP;
216 sc->sc_fastq.ifq_maxlen = 32;
217 IFQ_SET_READY(&sc->sc_if.if_snd);
218 if_attach(&sc->sc_if);
219 #if NBPFILTER > 0
220 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
221 #endif
222 }
223 }
224
225 static int
226 slinit(sc)
227 struct sl_softc *sc;
228 {
229
230 if (sc->sc_ep == NULL) {
231 /*
232 * XXX the trick this is used for is evil...
233 */
234 sc->sc_xxx = (u_char *)malloc(MCLBYTES, M_MBUF, M_WAITOK);
235 if (sc->sc_xxx)
236 sc->sc_ep = sc->sc_xxx + SLBUFSIZE;
237 else {
238 printf("sl%d: can't allocate buffer\n", sc->sc_unit);
239 sc->sc_if.if_flags &= ~IFF_UP;
240 return (0);
241 }
242 }
243 sc->sc_buf = sc->sc_ep - SLMAX;
244 sc->sc_mp = sc->sc_buf;
245 sl_compress_init(&sc->sc_comp);
246 return (1);
247 }
248
249 /*
250 * Line specific open routine.
251 * Attach the given tty to the first available sl unit.
252 */
253 /* ARGSUSED */
254 int
255 slopen(dev, tp)
256 dev_t dev;
257 struct tty *tp;
258 {
259 struct proc *p = curproc; /* XXX */
260 struct sl_softc *sc;
261 int nsl;
262 int error;
263 int s;
264
265 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
266 return (error);
267
268 if (tp->t_linesw && (tp->t_linesw->l_no == SLIPDISC))
269 return (0);
270
271 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
272 if (sc->sc_ttyp == NULL) {
273 if (slinit(sc) == 0)
274 return (ENOBUFS);
275 tp->t_sc = (caddr_t)sc;
276 sc->sc_ttyp = tp;
277 sc->sc_if.if_baudrate = tp->t_ospeed;
278 s = spltty();
279 tp->t_state |= TS_ISOPEN | TS_XCLUDE;
280 splx(s);
281 ttyflush(tp, FREAD | FWRITE);
282 #ifdef __NetBSD__
283 /*
284 * make sure tty output queue is large enough
285 * to hold a full-sized packet (including frame
286 * end, and a possible extra frame end). full-sized
287 * packet occupies a max of 2*SLMAX bytes (because
288 * of possible escapes), and add two on for frame
289 * ends.
290 */
291 s = spltty();
292 if (tp->t_outq.c_cn < 2*SLMAX+2) {
293 sc->sc_oldbufsize = tp->t_outq.c_cn;
294 sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
295
296 clfree(&tp->t_outq);
297 error = clalloc(&tp->t_outq, 2*SLMAX+2, 0);
298 if (error) {
299 splx(s);
300 return(error);
301 }
302 } else
303 sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
304 splx(s);
305 #endif /* __NetBSD__ */
306 return (0);
307 }
308 return (ENXIO);
309 }
310
311 /*
312 * Line specific close routine.
313 * Detach the tty from the sl unit.
314 */
315 void
316 slclose(tp)
317 struct tty *tp;
318 {
319 struct sl_softc *sc;
320 int s;
321
322 ttywflush(tp);
323 s = splimp(); /* actually, max(spltty, splsoftnet) */
324 tp->t_linesw = linesw[0]; /* default line discipline */
325 tp->t_state = 0;
326 sc = (struct sl_softc *)tp->t_sc;
327 if (sc != NULL) {
328 if_down(&sc->sc_if);
329 sc->sc_ttyp = NULL;
330 tp->t_sc = NULL;
331 free((caddr_t)(sc->sc_ep - SLBUFSIZE), M_MBUF);
332 sc->sc_ep = 0;
333 sc->sc_mp = 0;
334 sc->sc_buf = 0;
335 }
336 #ifdef __NetBSD__
337 /* if necessary, install a new outq buffer of the appropriate size */
338 if (sc->sc_oldbufsize != 0) {
339 clfree(&tp->t_outq);
340 clalloc(&tp->t_outq, sc->sc_oldbufsize, sc->sc_oldbufquot);
341 }
342 #endif
343 splx(s);
344 }
345
346 /*
347 * Line specific (tty) ioctl routine.
348 * Provide a way to get the sl unit number.
349 */
350 /* ARGSUSED */
351 int
352 sltioctl(tp, cmd, data, flag)
353 struct tty *tp;
354 u_long cmd;
355 caddr_t data;
356 int flag;
357 {
358 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
359
360 switch (cmd) {
361 case SLIOCGUNIT:
362 *(int *)data = sc->sc_unit; /* XXX */
363 break;
364
365 default:
366 return (-1);
367 }
368 return (0);
369 }
370
371 /*
372 * Queue a packet. Start transmission if not active.
373 * Compression happens in slstart; if we do it here, IP TOS
374 * will cause us to not compress "background" packets, because
375 * ordering gets trashed. It can be done for all packets in slstart.
376 */
377 int
378 sloutput(ifp, m, dst, rtp)
379 struct ifnet *ifp;
380 struct mbuf *m;
381 struct sockaddr *dst;
382 struct rtentry *rtp;
383 {
384 struct sl_softc *sc = ifp->if_softc;
385 struct ip *ip;
386 int s, error;
387 ALTQ_DECL(struct altq_pktattr pktattr;)
388
389 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
390
391 /*
392 * `Cannot happen' (see slioctl). Someday we will extend
393 * the line protocol to support other address families.
394 */
395 if (dst->sa_family != AF_INET) {
396 printf("%s: af%d not supported\n", sc->sc_if.if_xname,
397 dst->sa_family);
398 m_freem(m);
399 sc->sc_if.if_noproto++;
400 return (EAFNOSUPPORT);
401 }
402
403 if (sc->sc_ttyp == NULL) {
404 m_freem(m);
405 return (ENETDOWN); /* sort of */
406 }
407 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
408 (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
409 m_freem(m);
410 printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
411 return (EHOSTUNREACH);
412 }
413 ip = mtod(m, struct ip *);
414 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
415 m_freem(m);
416 return (ENETRESET); /* XXX ? */
417 }
418 s = splimp();
419 if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
420 struct timeval tv;
421
422 /* if output's been stalled for too long, and restart */
423 timersub(&time, &sc->sc_if.if_lastchange, &tv);
424 if (tv.tv_sec > 0) {
425 sc->sc_otimeout++;
426 slstart(sc->sc_ttyp);
427 }
428 }
429 if ((ip->ip_tos & IPTOS_LOWDELAY) != 0
430 #ifdef ALTQ
431 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
432 #endif
433 ) {
434 if (IF_QFULL(&sc->sc_fastq)) {
435 IF_DROP(&sc->sc_fastq);
436 m_freem(m);
437 error = ENOBUFS;
438 } else {
439 IF_ENQUEUE(&sc->sc_fastq, m);
440 error = 0;
441 }
442 } else
443 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
444 if (error) {
445 splx(s);
446 ifp->if_oerrors++;
447 return (error);
448 }
449 sc->sc_if.if_lastchange = time;
450 if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
451 slstart(sc->sc_ttyp);
452 splx(s);
453 return (0);
454 }
455
456 /*
457 * Start output on interface. Get another datagram
458 * to send from the interface queue and map it to
459 * the interface before starting output.
460 */
461 void
462 slstart(tp)
463 struct tty *tp;
464 {
465 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
466 struct mbuf *m;
467 u_char *cp;
468 struct ip *ip;
469 int s;
470 struct mbuf *m2;
471 #if NBPFILTER > 0
472 u_char *bpfbuf;
473 int len = 0;
474 #endif
475 #ifndef __NetBSD__ /* XXX - cgd */
476 extern int cfreecount;
477 #endif
478 #if NBPFILTER > 0
479 MALLOC(bpfbuf, u_char *, SLMAX + SLIP_HDRLEN, M_DEVBUF, M_NOWAIT);
480 #endif
481
482 for (;;) {
483 /*
484 * If there is more in the output queue, just send it now.
485 * We are being called in lieu of ttstart and must do what
486 * it would.
487 */
488 if (tp->t_outq.c_cc != 0) {
489 (*tp->t_oproc)(tp);
490 if (tp->t_outq.c_cc > SLIP_HIWAT) {
491 #if NBPFILTER > 0
492 if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
493 #endif
494 return;
495 }
496 }
497 /*
498 * This happens briefly when the line shuts down.
499 */
500 if (sc == NULL) {
501 #if NBPFILTER > 0
502 if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
503 #endif
504 return;
505 }
506
507 #ifdef __NetBSD__ /* XXX - cgd */
508 /*
509 * Do not remove the packet from the IP queue if it
510 * doesn't look like the packet will fit into the
511 * current serial output queue, with a packet full of
512 * escapes this could be as bad as MTU*2+2.
513 */
514 if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2*sc->sc_if.if_mtu+2) {
515 #if NBPFILTER > 0
516 if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
517 #endif
518 return;
519 }
520 #endif /* __NetBSD__ */
521
522 /*
523 * Get a packet and send it to the interface.
524 */
525 s = splimp();
526 IF_DEQUEUE(&sc->sc_fastq, m);
527 if (m)
528 sc->sc_if.if_omcasts++; /* XXX */
529 else
530 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
531 splx(s);
532 if (m == NULL) {
533 #if NBPFILTER > 0
534 if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
535 #endif
536 return;
537 }
538
539 /*
540 * We do the header compression here rather than in sloutput
541 * because the packets will be out of order if we are using TOS
542 * queueing, and the connection id compression will get
543 * munged when this happens.
544 */
545 #if NBPFILTER > 0
546 if (sc->sc_if.if_bpf && bpfbuf != NULL) {
547 /*
548 * We need to save the TCP/IP header before it's
549 * compressed. To avoid complicated code, we just
550 * copy the entire packet into a stack buffer (since
551 * this is a serial line, packets should be short
552 * and/or the copy should be negligible cost compared
553 * to the packet transmission time).
554 */
555 struct mbuf *m1 = m;
556 u_char *cp = bpfbuf + SLIP_HDRLEN;
557
558 len = 0;
559 do {
560 int mlen = m1->m_len;
561
562 bcopy(mtod(m1, caddr_t), cp, mlen);
563 cp += mlen;
564 len += mlen;
565 } while ((m1 = m1->m_next) != NULL);
566 }
567 #endif
568 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
569 if (sc->sc_if.if_flags & SC_COMPRESS)
570 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
571 &sc->sc_comp, 1);
572 }
573 #if NBPFILTER > 0
574 if (sc->sc_if.if_bpf && bpfbuf != NULL) {
575 /*
576 * Put the SLIP pseudo-"link header" in place. The
577 * compressed header is now at the beginning of the
578 * mbuf.
579 */
580 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
581 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
582 bpf_tap(sc->sc_if.if_bpf, bpfbuf, len + SLIP_HDRLEN);
583 }
584 #endif
585 sc->sc_if.if_lastchange = time;
586
587 #ifndef __NetBSD__ /* XXX - cgd */
588 /*
589 * If system is getting low on clists, just flush our
590 * output queue (if the stuff was important, it'll get
591 * retransmitted).
592 */
593 if (cfreecount < CLISTRESERVE + SLMTU) {
594 m_freem(m);
595 sc->sc_if.if_collisions++;
596 continue;
597 }
598 #endif /* !__NetBSD__ */
599 /*
600 * The extra FRAME_END will start up a new packet, and thus
601 * will flush any accumulated garbage. We do this whenever
602 * the line may have been idle for some time.
603 */
604 if (tp->t_outq.c_cc == 0) {
605 ++sc->sc_if.if_obytes;
606 (void) putc(FRAME_END, &tp->t_outq);
607 }
608
609 while (m) {
610 u_char *ep;
611
612 cp = mtod(m, u_char *); ep = cp + m->m_len;
613 while (cp < ep) {
614 /*
615 * Find out how many bytes in the string we can
616 * handle without doing something special.
617 */
618 u_char *bp = cp;
619
620 while (cp < ep) {
621 switch (*cp++) {
622 case FRAME_ESCAPE:
623 case FRAME_END:
624 --cp;
625 goto out;
626 }
627 }
628 out:
629 if (cp > bp) {
630 /*
631 * Put n characters at once
632 * into the tty output queue.
633 */
634 #ifdef __NetBSD__ /* XXX - cgd */
635 if (b_to_q((u_char *)bp, cp - bp,
636 #else
637 if (b_to_q((char *)bp, cp - bp,
638 #endif
639 &tp->t_outq))
640 break;
641 sc->sc_if.if_obytes += cp - bp;
642 }
643 /*
644 * If there are characters left in the mbuf,
645 * the first one must be special..
646 * Put it out in a different form.
647 */
648 if (cp < ep) {
649 if (putc(FRAME_ESCAPE, &tp->t_outq))
650 break;
651 if (putc(*cp++ == FRAME_ESCAPE ?
652 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
653 &tp->t_outq)) {
654 (void) unputc(&tp->t_outq);
655 break;
656 }
657 sc->sc_if.if_obytes += 2;
658 }
659 }
660 MFREE(m, m2);
661 m = m2;
662 }
663
664 if (putc(FRAME_END, &tp->t_outq)) {
665 /*
666 * Not enough room. Remove a char to make room
667 * and end the packet normally.
668 * If you get many collisions (more than one or two
669 * a day) you probably do not have enough clists
670 * and you should increase "nclist" in param.c.
671 */
672 (void) unputc(&tp->t_outq);
673 (void) putc(FRAME_END, &tp->t_outq);
674 sc->sc_if.if_collisions++;
675 } else {
676 ++sc->sc_if.if_obytes;
677 sc->sc_if.if_opackets++;
678 }
679 }
680 }
681
682 /*
683 * Copy data buffer to mbuf chain; add ifnet pointer.
684 */
685 static struct mbuf *
686 sl_btom(sc, len)
687 struct sl_softc *sc;
688 int len;
689 {
690 struct mbuf *m;
691 u_char *p;
692
693 MGETHDR(m, M_DONTWAIT, MT_DATA);
694 if (m == NULL)
695 return (NULL);
696
697 /*
698 * If we have more than MHLEN bytes, it's cheaper to
699 * queue the cluster we just filled & allocate a new one
700 * for the input buffer. Otherwise, fill the mbuf we
701 * allocated above. Note that code in the input routine
702 * guarantees that packet will fit in a cluster.
703 */
704 if (len >= MHLEN) {
705 /*
706 * XXX this is that evil trick I mentioned...
707 */
708 p = sc->sc_xxx;
709 sc->sc_xxx = (u_char *)malloc(MCLBYTES, M_MBUF, M_NOWAIT);
710 if (sc->sc_xxx == NULL) {
711 /*
712 * We couldn't allocate a new buffer - if
713 * memory's this low, it's time to start
714 * dropping packets.
715 */
716 (void) m_free(m);
717 return (NULL);
718 }
719 sc->sc_ep = sc->sc_xxx + SLBUFSIZE;
720 MEXTADD(m, p, MCLBYTES, M_MBUF, NULL, NULL);
721 m->m_data = (caddr_t)sc->sc_buf;
722 } else
723 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
724
725 m->m_len = len;
726 m->m_pkthdr.len = len;
727 m->m_pkthdr.rcvif = &sc->sc_if;
728 return (m);
729 }
730
731 /*
732 * tty interface receiver interrupt.
733 */
734 void
735 slinput(c, tp)
736 int c;
737 struct tty *tp;
738 {
739 struct sl_softc *sc;
740 struct mbuf *m;
741 int len;
742 int s;
743 #if NBPFILTER > 0
744 u_char chdr[CHDR_LEN];
745 #endif
746
747 tk_nin++;
748 sc = (struct sl_softc *)tp->t_sc;
749 if (sc == NULL)
750 return;
751 if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
752 (tp->t_cflag & CLOCAL) == 0)) {
753 sc->sc_flags |= SC_ERROR;
754 return;
755 }
756 c &= TTY_CHARMASK;
757
758 ++sc->sc_if.if_ibytes;
759
760 if (sc->sc_if.if_flags & IFF_DEBUG) {
761 if (c == ABT_ESC) {
762 /*
763 * If we have a previous abort, see whether
764 * this one is within the time limit.
765 */
766 if (sc->sc_abortcount &&
767 time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
768 sc->sc_abortcount = 0;
769 /*
770 * If we see an abort after "idle" time, count it;
771 * record when the first abort escape arrived.
772 */
773 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
774 if (++sc->sc_abortcount == 1)
775 sc->sc_starttime = time.tv_sec;
776 if (sc->sc_abortcount >= ABT_COUNT) {
777 slclose(tp);
778 return;
779 }
780 }
781 } else
782 sc->sc_abortcount = 0;
783 sc->sc_lasttime = time.tv_sec;
784 }
785
786 switch (c) {
787
788 case TRANS_FRAME_ESCAPE:
789 if (sc->sc_escape)
790 c = FRAME_ESCAPE;
791 break;
792
793 case TRANS_FRAME_END:
794 if (sc->sc_escape)
795 c = FRAME_END;
796 break;
797
798 case FRAME_ESCAPE:
799 sc->sc_escape = 1;
800 return;
801
802 case FRAME_END:
803 if(sc->sc_flags & SC_ERROR) {
804 sc->sc_flags &= ~SC_ERROR;
805 goto newpack;
806 }
807 len = sc->sc_mp - sc->sc_buf;
808 if (len < 3)
809 /* less than min length packet - ignore */
810 goto newpack;
811
812 #if NBPFILTER > 0
813 if (sc->sc_if.if_bpf) {
814 /*
815 * Save the compressed header, so we
816 * can tack it on later. Note that we
817 * will end up copying garbage in some
818 * cases but this is okay. We remember
819 * where the buffer started so we can
820 * compute the new header length.
821 */
822 bcopy(sc->sc_buf, chdr, CHDR_LEN);
823 }
824 #endif
825
826 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
827 if (c & 0x80)
828 c = TYPE_COMPRESSED_TCP;
829 else if (c == TYPE_UNCOMPRESSED_TCP)
830 *sc->sc_buf &= 0x4f; /* XXX */
831 /*
832 * We've got something that's not an IP packet.
833 * If compression is enabled, try to decompress it.
834 * Otherwise, if `auto-enable' compression is on and
835 * it's a reasonable packet, decompress it and then
836 * enable compression. Otherwise, drop it.
837 */
838 if (sc->sc_if.if_flags & SC_COMPRESS) {
839 len = sl_uncompress_tcp(&sc->sc_buf, len,
840 (u_int)c, &sc->sc_comp);
841 if (len <= 0)
842 goto error;
843 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
844 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
845 len = sl_uncompress_tcp(&sc->sc_buf, len,
846 (u_int)c, &sc->sc_comp);
847 if (len <= 0)
848 goto error;
849 sc->sc_if.if_flags |= SC_COMPRESS;
850 } else
851 goto error;
852 }
853 #if NBPFILTER > 0
854 if (sc->sc_if.if_bpf) {
855 /*
856 * Put the SLIP pseudo-"link header" in place.
857 * We couldn't do this any earlier since
858 * decompression probably moved the buffer
859 * pointer. Then, invoke BPF.
860 */
861 u_char *hp = sc->sc_buf - SLIP_HDRLEN;
862
863 hp[SLX_DIR] = SLIPDIR_IN;
864 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
865 bpf_tap(sc->sc_if.if_bpf, hp, len + SLIP_HDRLEN);
866 }
867 #endif
868 m = sl_btom(sc, len);
869 if (m == NULL)
870 goto error;
871
872 sc->sc_if.if_ipackets++;
873 sc->sc_if.if_lastchange = time;
874 s = splimp();
875 if (IF_QFULL(&ipintrq)) {
876 IF_DROP(&ipintrq);
877 sc->sc_if.if_ierrors++;
878 sc->sc_if.if_iqdrops++;
879 m_freem(m);
880 } else {
881 IF_ENQUEUE(&ipintrq, m);
882 schednetisr(NETISR_IP);
883 }
884 splx(s);
885 goto newpack;
886 }
887 if (sc->sc_mp < sc->sc_ep) {
888 *sc->sc_mp++ = c;
889 sc->sc_escape = 0;
890 return;
891 }
892
893 /* can't put lower; would miss an extra frame */
894 sc->sc_flags |= SC_ERROR;
895
896 error:
897 sc->sc_if.if_ierrors++;
898 newpack:
899 sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
900 sc->sc_escape = 0;
901 }
902
903 /*
904 * Process an ioctl request.
905 */
906 int
907 slioctl(ifp, cmd, data)
908 struct ifnet *ifp;
909 u_long cmd;
910 caddr_t data;
911 {
912 struct ifaddr *ifa = (struct ifaddr *)data;
913 struct ifreq *ifr = (struct ifreq *)data;
914 int s = splimp(), error = 0;
915 struct sl_softc *sc = ifp->if_softc;
916
917 switch (cmd) {
918
919 case SIOCSIFADDR:
920 if (ifa->ifa_addr->sa_family == AF_INET)
921 ifp->if_flags |= IFF_UP;
922 else
923 error = EAFNOSUPPORT;
924 break;
925
926 case SIOCSIFDSTADDR:
927 if (ifa->ifa_addr->sa_family != AF_INET)
928 error = EAFNOSUPPORT;
929 break;
930
931 case SIOCSIFMTU:
932 if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
933 error = EINVAL;
934 break;
935 }
936 sc->sc_if.if_mtu = ifr->ifr_mtu;
937 break;
938
939 case SIOCGIFMTU:
940 ifr->ifr_mtu = sc->sc_if.if_mtu;
941 break;
942
943 case SIOCADDMULTI:
944 case SIOCDELMULTI:
945 if (ifr == 0) {
946 error = EAFNOSUPPORT; /* XXX */
947 break;
948 }
949 switch (ifr->ifr_addr.sa_family) {
950
951 #ifdef INET
952 case AF_INET:
953 break;
954 #endif
955
956 default:
957 error = EAFNOSUPPORT;
958 break;
959 }
960 break;
961
962 default:
963 error = EINVAL;
964 }
965 splx(s);
966 return (error);
967 }
968 #endif
969