if_sl.c revision 1.64 1 /* $NetBSD: if_sl.c,v 1.64 2001/01/09 04:42:48 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 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
157 #define SLMAX (MCLBYTES - BUFOFFSET)
158 #define SLBUFSIZE (SLMAX + BUFOFFSET)
159 #ifndef SLMTU
160 #define SLMTU 296
161 #endif
162 #if (SLMTU < 3)
163 #error SLMTU way too small.
164 #endif
165 #define SLIP_HIWAT roundup(50,CBSIZE)
166 #ifndef __NetBSD__ /* XXX - cgd */
167 #define CLISTRESERVE 1024 /* Can't let clists get too low */
168 #endif /* !__NetBSD__ */
169
170 /*
171 * SLIP ABORT ESCAPE MECHANISM:
172 * (inspired by HAYES modem escape arrangement)
173 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
174 * within window time signals a "soft" exit from slip mode by remote end
175 * if the IFF_DEBUG flag is on.
176 */
177 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
178 #define ABT_IDLE 1 /* in seconds - idle before an escape */
179 #define ABT_COUNT 3 /* count of escapes for abort */
180 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
181
182 struct sl_softc sl_softc[NSL];
183
184 #define FRAME_END 0xc0 /* Frame End */
185 #define FRAME_ESCAPE 0xdb /* Frame Esc */
186 #define TRANS_FRAME_END 0xdc /* transposed frame end */
187 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
188
189 static int slinit __P((struct sl_softc *));
190 static struct mbuf *sl_btom __P((struct sl_softc *, int));
191
192 /*
193 * Called from boot code to establish sl interfaces.
194 */
195 void
196 slattach()
197 {
198 struct sl_softc *sc;
199 int i = 0;
200
201 for (sc = sl_softc; i < NSL; sc++) {
202 sc->sc_unit = i; /* XXX */
203 sprintf(sc->sc_if.if_xname, "sl%d", i++);
204 sc->sc_if.if_softc = sc;
205 sc->sc_if.if_mtu = SLMTU;
206 sc->sc_if.if_flags =
207 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
208 sc->sc_if.if_type = IFT_SLIP;
209 sc->sc_if.if_ioctl = slioctl;
210 sc->sc_if.if_output = sloutput;
211 sc->sc_if.if_dlt = DLT_SLIP;
212 sc->sc_fastq.ifq_maxlen = 32;
213 IFQ_SET_READY(&sc->sc_if.if_snd);
214 if_attach(&sc->sc_if);
215 #if NBPFILTER > 0
216 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
217 #endif
218 }
219 }
220
221 static int
222 slinit(sc)
223 struct sl_softc *sc;
224 {
225
226 if (sc->sc_mbuf == NULL) {
227 MGETHDR(sc->sc_mbuf, M_WAIT, MT_DATA);
228 MCLGET(sc->sc_mbuf, M_WAIT);
229 }
230 sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
231 sc->sc_mbuf->m_ext.ext_size;
232 sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
233 BUFOFFSET;
234
235 sl_compress_init(&sc->sc_comp);
236
237 return (1);
238 }
239
240 /*
241 * Line specific open routine.
242 * Attach the given tty to the first available sl unit.
243 */
244 /* ARGSUSED */
245 int
246 slopen(dev, tp)
247 dev_t dev;
248 struct tty *tp;
249 {
250 struct proc *p = curproc; /* XXX */
251 struct sl_softc *sc;
252 int nsl;
253 int error;
254 int s;
255
256 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
257 return (error);
258
259 if (tp->t_linesw && (tp->t_linesw->l_no == SLIPDISC))
260 return (0);
261
262 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
263 if (sc->sc_ttyp == NULL) {
264 if (slinit(sc) == 0)
265 return (ENOBUFS);
266 tp->t_sc = (caddr_t)sc;
267 sc->sc_ttyp = tp;
268 sc->sc_if.if_baudrate = tp->t_ospeed;
269 s = spltty();
270 tp->t_state |= TS_ISOPEN | TS_XCLUDE;
271 splx(s);
272 ttyflush(tp, FREAD | FWRITE);
273 #ifdef __NetBSD__
274 /*
275 * make sure tty output queue is large enough
276 * to hold a full-sized packet (including frame
277 * end, and a possible extra frame end). full-sized
278 * packet occupies a max of 2*SLMAX bytes (because
279 * of possible escapes), and add two on for frame
280 * ends.
281 */
282 s = spltty();
283 if (tp->t_outq.c_cn < 2*SLMAX+2) {
284 sc->sc_oldbufsize = tp->t_outq.c_cn;
285 sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
286
287 clfree(&tp->t_outq);
288 error = clalloc(&tp->t_outq, 2*SLMAX+2, 0);
289 if (error) {
290 splx(s);
291 return(error);
292 }
293 } else
294 sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
295 splx(s);
296 #endif /* __NetBSD__ */
297 return (0);
298 }
299 return (ENXIO);
300 }
301
302 /*
303 * Line specific close routine.
304 * Detach the tty from the sl unit.
305 */
306 void
307 slclose(tp)
308 struct tty *tp;
309 {
310 struct sl_softc *sc;
311 int s;
312
313 ttywflush(tp);
314 s = splimp(); /* actually, max(spltty, splsoftnet) */
315 tp->t_linesw = linesw[0]; /* default line discipline */
316 tp->t_state = 0;
317 sc = (struct sl_softc *)tp->t_sc;
318 if (sc != NULL) {
319 if_down(&sc->sc_if);
320 sc->sc_ttyp = NULL;
321 tp->t_sc = NULL;
322 m_freem(sc->sc_mbuf);
323 sc->sc_mbuf = NULL;
324 sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL;
325 }
326 #ifdef __NetBSD__
327 /* if necessary, install a new outq buffer of the appropriate size */
328 if (sc->sc_oldbufsize != 0) {
329 clfree(&tp->t_outq);
330 clalloc(&tp->t_outq, sc->sc_oldbufsize, sc->sc_oldbufquot);
331 }
332 #endif
333 splx(s);
334 }
335
336 /*
337 * Line specific (tty) ioctl routine.
338 * Provide a way to get the sl unit number.
339 */
340 /* ARGSUSED */
341 int
342 sltioctl(tp, cmd, data, flag)
343 struct tty *tp;
344 u_long cmd;
345 caddr_t data;
346 int flag;
347 {
348 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
349
350 switch (cmd) {
351 case SLIOCGUNIT:
352 *(int *)data = sc->sc_unit; /* XXX */
353 break;
354
355 default:
356 return (-1);
357 }
358 return (0);
359 }
360
361 /*
362 * Queue a packet. Start transmission if not active.
363 * Compression happens in slstart; if we do it here, IP TOS
364 * will cause us to not compress "background" packets, because
365 * ordering gets trashed. It can be done for all packets in slstart.
366 */
367 int
368 sloutput(ifp, m, dst, rtp)
369 struct ifnet *ifp;
370 struct mbuf *m;
371 struct sockaddr *dst;
372 struct rtentry *rtp;
373 {
374 struct sl_softc *sc = ifp->if_softc;
375 struct ip *ip;
376 int s, error;
377 ALTQ_DECL(struct altq_pktattr pktattr;)
378
379 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
380
381 /*
382 * `Cannot happen' (see slioctl). Someday we will extend
383 * the line protocol to support other address families.
384 */
385 if (dst->sa_family != AF_INET) {
386 printf("%s: af%d not supported\n", sc->sc_if.if_xname,
387 dst->sa_family);
388 m_freem(m);
389 sc->sc_if.if_noproto++;
390 return (EAFNOSUPPORT);
391 }
392
393 if (sc->sc_ttyp == NULL) {
394 m_freem(m);
395 return (ENETDOWN); /* sort of */
396 }
397 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
398 (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
399 m_freem(m);
400 printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
401 return (EHOSTUNREACH);
402 }
403 ip = mtod(m, struct ip *);
404 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
405 m_freem(m);
406 return (ENETRESET); /* XXX ? */
407 }
408 s = splimp();
409 if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
410 struct timeval tv;
411
412 /* if output's been stalled for too long, and restart */
413 timersub(&time, &sc->sc_if.if_lastchange, &tv);
414 if (tv.tv_sec > 0) {
415 sc->sc_otimeout++;
416 slstart(sc->sc_ttyp);
417 }
418 }
419 if ((ip->ip_tos & IPTOS_LOWDELAY) != 0
420 #ifdef ALTQ
421 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
422 #endif
423 ) {
424 if (IF_QFULL(&sc->sc_fastq)) {
425 IF_DROP(&sc->sc_fastq);
426 m_freem(m);
427 error = ENOBUFS;
428 } else {
429 IF_ENQUEUE(&sc->sc_fastq, m);
430 error = 0;
431 }
432 } else
433 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
434 if (error) {
435 splx(s);
436 ifp->if_oerrors++;
437 return (error);
438 }
439 sc->sc_if.if_lastchange = time;
440 if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
441 slstart(sc->sc_ttyp);
442 splx(s);
443 return (0);
444 }
445
446 /*
447 * Start output on interface. Get another datagram
448 * to send from the interface queue and map it to
449 * the interface before starting output.
450 */
451 void
452 slstart(tp)
453 struct tty *tp;
454 {
455 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
456 struct mbuf *m;
457 u_char *cp;
458 struct ip *ip;
459 int s;
460 struct mbuf *m2;
461 #if NBPFILTER > 0
462 u_char *bpfbuf;
463 int len = 0;
464 #endif
465 #ifndef __NetBSD__ /* XXX - cgd */
466 extern int cfreecount;
467 #endif
468 #if NBPFILTER > 0
469 MALLOC(bpfbuf, u_char *, SLMAX + SLIP_HDRLEN, M_DEVBUF, M_NOWAIT);
470 #endif
471
472 for (;;) {
473 /*
474 * If there is more in the output queue, just send it now.
475 * We are being called in lieu of ttstart and must do what
476 * it would.
477 */
478 if (tp->t_outq.c_cc != 0) {
479 (*tp->t_oproc)(tp);
480 if (tp->t_outq.c_cc > SLIP_HIWAT) {
481 #if NBPFILTER > 0
482 if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
483 #endif
484 return;
485 }
486 }
487 /*
488 * This happens briefly when the line shuts down.
489 */
490 if (sc == NULL) {
491 #if NBPFILTER > 0
492 if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
493 #endif
494 return;
495 }
496
497 #ifdef __NetBSD__ /* XXX - cgd */
498 /*
499 * Do not remove the packet from the IP queue if it
500 * doesn't look like the packet will fit into the
501 * current serial output queue, with a packet full of
502 * escapes this could be as bad as MTU*2+2.
503 */
504 if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2*sc->sc_if.if_mtu+2) {
505 #if NBPFILTER > 0
506 if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
507 #endif
508 return;
509 }
510 #endif /* __NetBSD__ */
511
512 /*
513 * Get a packet and send it to the interface.
514 */
515 s = splimp();
516 IF_DEQUEUE(&sc->sc_fastq, m);
517 if (m)
518 sc->sc_if.if_omcasts++; /* XXX */
519 else
520 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
521 splx(s);
522 if (m == NULL) {
523 #if NBPFILTER > 0
524 if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
525 #endif
526 return;
527 }
528
529 /*
530 * We do the header compression here rather than in sloutput
531 * because the packets will be out of order if we are using TOS
532 * queueing, and the connection id compression will get
533 * munged when this happens.
534 */
535 #if NBPFILTER > 0
536 if (sc->sc_if.if_bpf && bpfbuf != NULL) {
537 /*
538 * We need to save the TCP/IP header before it's
539 * compressed. To avoid complicated code, we just
540 * copy the entire packet into a stack buffer (since
541 * this is a serial line, packets should be short
542 * and/or the copy should be negligible cost compared
543 * to the packet transmission time).
544 */
545 struct mbuf *m1 = m;
546 u_char *cp = bpfbuf + SLIP_HDRLEN;
547
548 len = 0;
549 do {
550 int mlen = m1->m_len;
551
552 bcopy(mtod(m1, caddr_t), cp, mlen);
553 cp += mlen;
554 len += mlen;
555 } while ((m1 = m1->m_next) != NULL);
556 }
557 #endif
558 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
559 if (sc->sc_if.if_flags & SC_COMPRESS)
560 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
561 &sc->sc_comp, 1);
562 }
563 #if NBPFILTER > 0
564 if (sc->sc_if.if_bpf && bpfbuf != NULL) {
565 /*
566 * Put the SLIP pseudo-"link header" in place. The
567 * compressed header is now at the beginning of the
568 * mbuf.
569 */
570 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
571 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
572 bpf_tap(sc->sc_if.if_bpf, bpfbuf, len + SLIP_HDRLEN);
573 }
574 #endif
575 sc->sc_if.if_lastchange = time;
576
577 #ifndef __NetBSD__ /* XXX - cgd */
578 /*
579 * If system is getting low on clists, just flush our
580 * output queue (if the stuff was important, it'll get
581 * retransmitted).
582 */
583 if (cfreecount < CLISTRESERVE + SLMTU) {
584 m_freem(m);
585 sc->sc_if.if_collisions++;
586 continue;
587 }
588 #endif /* !__NetBSD__ */
589 /*
590 * The extra FRAME_END will start up a new packet, and thus
591 * will flush any accumulated garbage. We do this whenever
592 * the line may have been idle for some time.
593 */
594 if (tp->t_outq.c_cc == 0) {
595 ++sc->sc_if.if_obytes;
596 (void) putc(FRAME_END, &tp->t_outq);
597 }
598
599 while (m) {
600 u_char *ep;
601
602 cp = mtod(m, u_char *); ep = cp + m->m_len;
603 while (cp < ep) {
604 /*
605 * Find out how many bytes in the string we can
606 * handle without doing something special.
607 */
608 u_char *bp = cp;
609
610 while (cp < ep) {
611 switch (*cp++) {
612 case FRAME_ESCAPE:
613 case FRAME_END:
614 --cp;
615 goto out;
616 }
617 }
618 out:
619 if (cp > bp) {
620 /*
621 * Put n characters at once
622 * into the tty output queue.
623 */
624 #ifdef __NetBSD__ /* XXX - cgd */
625 if (b_to_q((u_char *)bp, cp - bp,
626 #else
627 if (b_to_q((char *)bp, cp - bp,
628 #endif
629 &tp->t_outq))
630 break;
631 sc->sc_if.if_obytes += cp - bp;
632 }
633 /*
634 * If there are characters left in the mbuf,
635 * the first one must be special..
636 * Put it out in a different form.
637 */
638 if (cp < ep) {
639 if (putc(FRAME_ESCAPE, &tp->t_outq))
640 break;
641 if (putc(*cp++ == FRAME_ESCAPE ?
642 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
643 &tp->t_outq)) {
644 (void) unputc(&tp->t_outq);
645 break;
646 }
647 sc->sc_if.if_obytes += 2;
648 }
649 }
650 MFREE(m, m2);
651 m = m2;
652 }
653
654 if (putc(FRAME_END, &tp->t_outq)) {
655 /*
656 * Not enough room. Remove a char to make room
657 * and end the packet normally.
658 * If you get many collisions (more than one or two
659 * a day) you probably do not have enough clists
660 * and you should increase "nclist" in param.c.
661 */
662 (void) unputc(&tp->t_outq);
663 (void) putc(FRAME_END, &tp->t_outq);
664 sc->sc_if.if_collisions++;
665 } else {
666 ++sc->sc_if.if_obytes;
667 sc->sc_if.if_opackets++;
668 }
669 }
670 }
671
672 /*
673 * Copy data buffer to mbuf chain; add ifnet pointer.
674 */
675 static struct mbuf *
676 sl_btom(sc, len)
677 struct sl_softc *sc;
678 int len;
679 {
680 struct mbuf *m;
681
682 MGETHDR(m, M_DONTWAIT, MT_DATA);
683 if (m == NULL)
684 return (NULL);
685
686 /*
687 * We always prepend enough space for the SLIP "header".
688 */
689 sc->sc_pktstart -= SLIP_HDRLEN;
690 len += SLIP_HDRLEN;
691
692 /*
693 * If the packet will fit into the header mbuf we allocated
694 * above, copy it there and re-use the input buffer we already
695 * have.
696 */
697 if (len < MHLEN)
698 memcpy(mtod(m, caddr_t), sc->sc_pktstart, len);
699 else {
700 /*
701 * Allocate a new input buffer and swap.
702 */
703 m = sc->sc_mbuf;
704 MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA);
705 if (sc->sc_mbuf == NULL) {
706 sc->sc_mbuf = m;
707 return (NULL);
708 }
709 MCLGET(sc->sc_mbuf, M_DONTWAIT);
710 if ((sc->sc_mbuf->m_flags & M_EXT) == 0) {
711 sc->sc_mbuf = m;
712 return (NULL);
713 }
714 sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
715 sc->sc_mbuf->m_ext.ext_size;
716
717 m->m_data = sc->sc_pktstart;
718 }
719
720 m->m_pkthdr.len = m->m_len = len;
721 m->m_pkthdr.rcvif = &sc->sc_if;
722 return (m);
723 }
724
725 /*
726 * tty interface receiver interrupt.
727 */
728 void
729 slinput(c, tp)
730 int c;
731 struct tty *tp;
732 {
733 struct sl_softc *sc;
734 struct mbuf *m;
735 int len;
736 int s;
737 #if NBPFILTER > 0
738 u_char chdr[CHDR_LEN];
739 #endif
740
741 tk_nin++;
742 sc = (struct sl_softc *)tp->t_sc;
743 if (sc == NULL)
744 return;
745 if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
746 (tp->t_cflag & CLOCAL) == 0)) {
747 sc->sc_flags |= SC_ERROR;
748 return;
749 }
750 c &= TTY_CHARMASK;
751
752 ++sc->sc_if.if_ibytes;
753
754 if (sc->sc_if.if_flags & IFF_DEBUG) {
755 if (c == ABT_ESC) {
756 /*
757 * If we have a previous abort, see whether
758 * this one is within the time limit.
759 */
760 if (sc->sc_abortcount &&
761 time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
762 sc->sc_abortcount = 0;
763 /*
764 * If we see an abort after "idle" time, count it;
765 * record when the first abort escape arrived.
766 */
767 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
768 if (++sc->sc_abortcount == 1)
769 sc->sc_starttime = time.tv_sec;
770 if (sc->sc_abortcount >= ABT_COUNT) {
771 slclose(tp);
772 return;
773 }
774 }
775 } else
776 sc->sc_abortcount = 0;
777 sc->sc_lasttime = time.tv_sec;
778 }
779
780 switch (c) {
781
782 case TRANS_FRAME_ESCAPE:
783 if (sc->sc_escape)
784 c = FRAME_ESCAPE;
785 break;
786
787 case TRANS_FRAME_END:
788 if (sc->sc_escape)
789 c = FRAME_END;
790 break;
791
792 case FRAME_ESCAPE:
793 sc->sc_escape = 1;
794 return;
795
796 case FRAME_END:
797 if(sc->sc_flags & SC_ERROR) {
798 sc->sc_flags &= ~SC_ERROR;
799 goto newpack;
800 }
801 len = sc->sc_mp - sc->sc_pktstart;
802 if (len < 3)
803 /* less than min length packet - ignore */
804 goto newpack;
805
806 #if NBPFILTER > 0
807 if (sc->sc_if.if_bpf) {
808 /*
809 * Save the compressed header, so we
810 * can tack it on later. Note that we
811 * will end up copying garbage in some
812 * cases but this is okay. We remember
813 * where the buffer started so we can
814 * compute the new header length.
815 */
816 bcopy(sc->sc_pktstart, chdr, CHDR_LEN);
817 }
818 #endif
819
820 if ((c = (*sc->sc_pktstart & 0xf0)) != (IPVERSION << 4)) {
821 if (c & 0x80)
822 c = TYPE_COMPRESSED_TCP;
823 else if (c == TYPE_UNCOMPRESSED_TCP)
824 *sc->sc_pktstart &= 0x4f; /* XXX */
825 /*
826 * We've got something that's not an IP packet.
827 * If compression is enabled, try to decompress it.
828 * Otherwise, if `auto-enable' compression is on and
829 * it's a reasonable packet, decompress it and then
830 * enable compression. Otherwise, drop it.
831 */
832 if (sc->sc_if.if_flags & SC_COMPRESS) {
833 len = sl_uncompress_tcp(&sc->sc_pktstart, len,
834 (u_int)c, &sc->sc_comp);
835 if (len <= 0)
836 goto error;
837 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
838 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
839 len = sl_uncompress_tcp(&sc->sc_pktstart, len,
840 (u_int)c, &sc->sc_comp);
841 if (len <= 0)
842 goto error;
843 sc->sc_if.if_flags |= SC_COMPRESS;
844 } else
845 goto error;
846 }
847
848 m = sl_btom(sc, len);
849 if (m == NULL)
850 goto error;
851
852 #if NBPFILTER > 0
853 if (sc->sc_if.if_bpf) {
854 /*
855 * Put the SLIP pseudo-"link header" in place.
856 * Note the space for it has already been
857 * allocated in sl_btom().
858 */
859 u_char *hp = mtod(m, u_char *);
860
861 hp[SLX_DIR] = SLIPDIR_IN;
862 memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
863
864 bpf_mtap(sc->sc_if.if_bpf, m);
865 }
866 #endif
867
868 m_adj(m, SLIP_HDRLEN);
869 sc->sc_if.if_ipackets++;
870 sc->sc_if.if_lastchange = time;
871 s = splimp();
872 if (IF_QFULL(&ipintrq)) {
873 IF_DROP(&ipintrq);
874 sc->sc_if.if_ierrors++;
875 sc->sc_if.if_iqdrops++;
876 m_freem(m);
877 } else {
878 IF_ENQUEUE(&ipintrq, m);
879 schednetisr(NETISR_IP);
880 }
881 splx(s);
882 goto newpack;
883 }
884 if (sc->sc_mp < sc->sc_ep) {
885 *sc->sc_mp++ = c;
886 sc->sc_escape = 0;
887 return;
888 }
889
890 /* can't put lower; would miss an extra frame */
891 sc->sc_flags |= SC_ERROR;
892
893 error:
894 sc->sc_if.if_ierrors++;
895 newpack:
896 sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
897 BUFOFFSET;
898 sc->sc_escape = 0;
899 }
900
901 /*
902 * Process an ioctl request.
903 */
904 int
905 slioctl(ifp, cmd, data)
906 struct ifnet *ifp;
907 u_long cmd;
908 caddr_t data;
909 {
910 struct ifaddr *ifa = (struct ifaddr *)data;
911 struct ifreq *ifr = (struct ifreq *)data;
912 int s = splimp(), error = 0;
913 struct sl_softc *sc = ifp->if_softc;
914
915 switch (cmd) {
916
917 case SIOCSIFADDR:
918 if (ifa->ifa_addr->sa_family == AF_INET)
919 ifp->if_flags |= IFF_UP;
920 else
921 error = EAFNOSUPPORT;
922 break;
923
924 case SIOCSIFDSTADDR:
925 if (ifa->ifa_addr->sa_family != AF_INET)
926 error = EAFNOSUPPORT;
927 break;
928
929 case SIOCSIFMTU:
930 if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
931 error = EINVAL;
932 break;
933 }
934 sc->sc_if.if_mtu = ifr->ifr_mtu;
935 break;
936
937 case SIOCGIFMTU:
938 ifr->ifr_mtu = sc->sc_if.if_mtu;
939 break;
940
941 case SIOCADDMULTI:
942 case SIOCDELMULTI:
943 if (ifr == 0) {
944 error = EAFNOSUPPORT; /* XXX */
945 break;
946 }
947 switch (ifr->ifr_addr.sa_family) {
948
949 #ifdef INET
950 case AF_INET:
951 break;
952 #endif
953
954 default:
955 error = EAFNOSUPPORT;
956 break;
957 }
958 break;
959
960 default:
961 error = EINVAL;
962 }
963 splx(s);
964 return (error);
965 }
966 #endif
967