if_sl.c revision 1.47 1 /* $NetBSD: if_sl.c,v 1.47 1998/03/01 02:25:05 fvdl 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 "bpfilter.h"
73
74 #include <sys/param.h>
75 #include <sys/proc.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/buf.h>
79 #include <sys/dkstat.h>
80 #include <sys/socket.h>
81 #include <sys/ioctl.h>
82 #include <sys/file.h>
83 #include <sys/tty.h>
84 #include <sys/kernel.h>
85 #include <sys/conf.h>
86 #if __NetBSD__
87 #include <sys/systm.h>
88 #endif
89
90 #include <machine/cpu.h>
91
92 #include <net/if.h>
93 #include <net/if_types.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
96
97 #if INET
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/in_var.h>
101 #include <netinet/ip.h>
102 #else
103 Huh? Slip without inet?
104 #endif
105
106 #include <net/slcompress.h>
107 #include <net/if_slvar.h>
108 #include <net/slip.h>
109
110 #if NBPFILTER > 0
111 #include <sys/time.h>
112 #include <net/bpf.h>
113 #endif
114
115 /*
116 * SLMAX is a hard limit on input packet size. To simplify the code
117 * and improve performance, we require that packets fit in an mbuf
118 * cluster, and if we get a compressed packet, there's enough extra
119 * room to expand the header into a max length tcp/ip header (128
120 * bytes). So, SLMAX can be at most
121 * MCLBYTES - 128
122 *
123 * SLMTU is a hard limit on output packet size. To insure good
124 * interactive response, SLMTU wants to be the smallest size that
125 * amortizes the header cost. (Remember that even with
126 * type-of-service queuing, we have to wait for any in-progress
127 * packet to finish. I.e., we wait, on the average, 1/2 * mtu /
128 * cps, where cps is the line speed in characters per second.
129 * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line. The
130 * average compressed header size is 6-8 bytes so any MTU > 90
131 * bytes will give us 90% of the line bandwidth. A 100ms wait is
132 * tolerable (500ms is not), so want an MTU around 296. (Since TCP
133 * will send 256 byte segments (to allow for 40 byte headers), the
134 * typical packet size on the wire will be around 260 bytes). In
135 * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
136 * leave the interface MTU relatively high (so we don't IP fragment
137 * when acting as a gateway to someone using a stupid MTU).
138 *
139 * Similar considerations apply to SLIP_HIWAT: It's the amount of
140 * data that will be queued 'downstream' of us (i.e., in clists
141 * waiting to be picked up by the tty output interrupt). If we
142 * queue a lot of data downstream, it's immune to our t.o.s. queuing.
143 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
144 * telnet/ftp will see a 1 sec wait, independent of the mtu (the
145 * wait is dependent on the ftp window size but that's typically
146 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize
147 * the cost (in idle time on the wire) of the tty driver running
148 * off the end of its clists & having to call back slstart for a
149 * new packet. For a tty interface with any buffering at all, this
150 * cost will be zero. Even with a totally brain dead interface (like
151 * the one on a typical workstation), the cost will be <= 1 character
152 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
153 * at most 1% while maintaining good interactive response.
154 */
155 #if NBPFILTER > 0
156 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
157 #else
158 #define BUFOFFSET (128+sizeof(struct ifnet **))
159 #endif
160 #define SLMAX (MCLBYTES - BUFOFFSET)
161 #define SLBUFSIZE (SLMAX + BUFOFFSET)
162 #ifndef SLMTU
163 #define SLMTU 296
164 #endif
165 #if (SLMTU < 3)
166 Huh? SLMTU way too small.
167 #endif
168 #define SLIP_HIWAT roundup(50,CBSIZE)
169 #ifndef NetBSD /* XXX - cgd */
170 #define CLISTRESERVE 1024 /* Can't let clists get too low */
171 #endif /* !NetBSD */
172
173 /*
174 * SLIP ABORT ESCAPE MECHANISM:
175 * (inspired by HAYES modem escape arrangement)
176 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
177 * within window time signals a "soft" exit from slip mode by remote end
178 * if the IFF_DEBUG flag is on.
179 */
180 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
181 #define ABT_IDLE 1 /* in seconds - idle before an escape */
182 #define ABT_COUNT 3 /* count of escapes for abort */
183 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
184
185 struct sl_softc sl_softc[NSL];
186
187 #define FRAME_END 0xc0 /* Frame End */
188 #define FRAME_ESCAPE 0xdb /* Frame Esc */
189 #define TRANS_FRAME_END 0xdc /* transposed frame end */
190 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
191
192 static int slinit __P((struct sl_softc *));
193 static struct mbuf *sl_btom __P((struct sl_softc *, int));
194
195 /*
196 * Called from boot code to establish sl interfaces.
197 */
198 void
199 slattach()
200 {
201 register struct sl_softc *sc;
202 register int i = 0;
203
204 for (sc = sl_softc; i < NSL; sc++) {
205 sc->sc_unit = i; /* XXX */
206 sprintf(sc->sc_if.if_xname, "sl%d", i++);
207 sc->sc_if.if_softc = sc;
208 sc->sc_if.if_mtu = SLMTU;
209 sc->sc_if.if_flags =
210 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
211 sc->sc_if.if_type = IFT_SLIP;
212 sc->sc_if.if_ioctl = slioctl;
213 sc->sc_if.if_output = sloutput;
214 sc->sc_if.if_snd.ifq_maxlen = 50;
215 sc->sc_fastq.ifq_maxlen = 32;
216 if_attach(&sc->sc_if);
217 #if NBPFILTER > 0
218 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
219 #endif
220 }
221 }
222
223 static int
224 slinit(sc)
225 register struct sl_softc *sc;
226 {
227
228 if (sc->sc_ep == NULL) {
229 /*
230 * XXX the trick this is used for is evil...
231 */
232 sc->sc_xxx = (u_char *)malloc(MCLBYTES, M_MBUF, M_WAITOK);
233 if (sc->sc_xxx)
234 sc->sc_ep = sc->sc_xxx + SLBUFSIZE;
235 else {
236 printf("sl%d: can't allocate buffer\n", sc->sc_unit);
237 sc->sc_if.if_flags &= ~IFF_UP;
238 return (0);
239 }
240 }
241 sc->sc_buf = sc->sc_ep - SLMAX;
242 sc->sc_mp = sc->sc_buf;
243 sl_compress_init(&sc->sc_comp);
244 return (1);
245 }
246
247 /*
248 * Line specific open routine.
249 * Attach the given tty to the first available sl unit.
250 */
251 /* ARGSUSED */
252 int
253 slopen(dev, tp)
254 dev_t dev;
255 register struct tty *tp;
256 {
257 struct proc *p = curproc; /* XXX */
258 register struct sl_softc *sc;
259 register int nsl;
260 int error;
261 #ifdef NetBSD
262 int s;
263 #endif
264
265 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
266 return (error);
267
268 if (tp->t_line == 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*SLMTU 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*SLMTU+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, 3*SLMTU, 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 register struct sl_softc *sc;
320 int s;
321
322 ttywflush(tp);
323 s = splimp(); /* actually, max(spltty, splsoftnet) */
324 tp->t_line = 0;
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 register struct mbuf *m;
381 struct sockaddr *dst;
382 struct rtentry *rtp;
383 {
384 register struct sl_softc *sc = ifp->if_softc;
385 register struct ip *ip;
386 register struct ifqueue *ifq;
387 int s;
388
389 /*
390 * `Cannot happen' (see slioctl). Someday we will extend
391 * the line protocol to support other address families.
392 */
393 if (dst->sa_family != AF_INET) {
394 printf("%s: af%d not supported\n", sc->sc_if.if_xname,
395 dst->sa_family);
396 m_freem(m);
397 sc->sc_if.if_noproto++;
398 return (EAFNOSUPPORT);
399 }
400
401 if (sc->sc_ttyp == NULL) {
402 m_freem(m);
403 return (ENETDOWN); /* sort of */
404 }
405 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
406 (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
407 m_freem(m);
408 return (EHOSTUNREACH);
409 }
410 ifq = &sc->sc_if.if_snd;
411 ip = mtod(m, struct ip *);
412 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
413 m_freem(m);
414 return (ENETRESET); /* XXX ? */
415 }
416 if (ip->ip_tos & IPTOS_LOWDELAY)
417 ifq = &sc->sc_fastq;
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 (IF_QFULL(ifq)) {
430 IF_DROP(ifq);
431 m_freem(m);
432 splx(s);
433 sc->sc_if.if_oerrors++;
434 return (ENOBUFS);
435 }
436 IF_ENQUEUE(ifq, m);
437 sc->sc_if.if_lastchange = time;
438 if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
439 slstart(sc->sc_ttyp);
440 splx(s);
441 return (0);
442 }
443
444 /*
445 * Start output on interface. Get another datagram
446 * to send from the interface queue and map it to
447 * the interface before starting output.
448 */
449 void
450 slstart(tp)
451 register struct tty *tp;
452 {
453 register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
454 register struct mbuf *m;
455 register u_char *cp;
456 register struct ip *ip;
457 int s;
458 struct mbuf *m2;
459 #if NBPFILTER > 0
460 u_char bpfbuf[SLMTU + SLIP_HDRLEN];
461 register int len = 0;
462 #endif
463 #ifndef NetBSD /* XXX - cgd */
464 extern int cfreecount;
465 #endif
466
467 for (;;) {
468 /*
469 * If there is more in the output queue, just send it now.
470 * We are being called in lieu of ttstart and must do what
471 * it would.
472 */
473 if (tp->t_outq.c_cc != 0) {
474 (*tp->t_oproc)(tp);
475 if (tp->t_outq.c_cc > SLIP_HIWAT)
476 return;
477 }
478 /*
479 * This happens briefly when the line shuts down.
480 */
481 if (sc == NULL)
482 return;
483
484 #ifdef NetBSD /* XXX - cgd */
485 /*
486 * Do not remove the packet from the IP queue if it
487 * doesn't look like the packet will fit into the
488 * current serial output queue, with a packet full of
489 * escapes this could be as bad as SLMTU*2+2.
490 */
491 if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2*SLMTU+2)
492 return;
493 #endif /* NetBSD */
494
495 /*
496 * Get a packet and send it to the interface.
497 */
498 s = splimp();
499 IF_DEQUEUE(&sc->sc_fastq, m);
500 if (m)
501 sc->sc_if.if_omcasts++; /* XXX */
502 else
503 IF_DEQUEUE(&sc->sc_if.if_snd, m);
504 splx(s);
505 if (m == NULL)
506 return;
507
508 /*
509 * We do the header compression here rather than in sloutput
510 * because the packets will be out of order if we are using TOS
511 * queueing, and the connection id compression will get
512 * munged when this happens.
513 */
514 #if NBPFILTER > 0
515 if (sc->sc_bpf) {
516 /*
517 * We need to save the TCP/IP header before it's
518 * compressed. To avoid complicated code, we just
519 * copy the entire packet into a stack buffer (since
520 * this is a serial line, packets should be short
521 * and/or the copy should be negligible cost compared
522 * to the packet transmission time).
523 */
524 register struct mbuf *m1 = m;
525 register u_char *cp = bpfbuf + SLIP_HDRLEN;
526
527 len = 0;
528 do {
529 register int mlen = m1->m_len;
530
531 bcopy(mtod(m1, caddr_t), cp, mlen);
532 cp += mlen;
533 len += mlen;
534 } while ((m1 = m1->m_next) != NULL);
535 }
536 #endif
537 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
538 if (sc->sc_if.if_flags & SC_COMPRESS)
539 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
540 &sc->sc_comp, 1);
541 }
542 #if NBPFILTER > 0
543 if (sc->sc_bpf) {
544 /*
545 * Put the SLIP pseudo-"link header" in place. The
546 * compressed header is now at the beginning of the
547 * mbuf.
548 */
549 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
550 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
551 bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
552 }
553 #endif
554 sc->sc_if.if_lastchange = time;
555
556 #ifndef __NetBSD__ /* XXX - cgd */
557 /*
558 * If system is getting low on clists, just flush our
559 * output queue (if the stuff was important, it'll get
560 * retransmitted).
561 */
562 if (cfreecount < CLISTRESERVE + SLMTU) {
563 m_freem(m);
564 sc->sc_if.if_collisions++;
565 continue;
566 }
567 #endif /* !__NetBSD__ */
568 /*
569 * The extra FRAME_END will start up a new packet, and thus
570 * will flush any accumulated garbage. We do this whenever
571 * the line may have been idle for some time.
572 */
573 if (tp->t_outq.c_cc == 0) {
574 ++sc->sc_if.if_obytes;
575 (void) putc(FRAME_END, &tp->t_outq);
576 }
577
578 while (m) {
579 register u_char *ep;
580
581 cp = mtod(m, u_char *); ep = cp + m->m_len;
582 while (cp < ep) {
583 /*
584 * Find out how many bytes in the string we can
585 * handle without doing something special.
586 */
587 register u_char *bp = cp;
588
589 while (cp < ep) {
590 switch (*cp++) {
591 case FRAME_ESCAPE:
592 case FRAME_END:
593 --cp;
594 goto out;
595 }
596 }
597 out:
598 if (cp > bp) {
599 /*
600 * Put n characters at once
601 * into the tty output queue.
602 */
603 #ifdef __NetBSD__ /* XXX - cgd */
604 if (b_to_q((u_char *)bp, cp - bp,
605 #else
606 if (b_to_q((char *)bp, cp - bp,
607 #endif
608 &tp->t_outq))
609 break;
610 sc->sc_if.if_obytes += cp - bp;
611 }
612 /*
613 * If there are characters left in the mbuf,
614 * the first one must be special..
615 * Put it out in a different form.
616 */
617 if (cp < ep) {
618 if (putc(FRAME_ESCAPE, &tp->t_outq))
619 break;
620 if (putc(*cp++ == FRAME_ESCAPE ?
621 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
622 &tp->t_outq)) {
623 (void) unputc(&tp->t_outq);
624 break;
625 }
626 sc->sc_if.if_obytes += 2;
627 }
628 }
629 MFREE(m, m2);
630 m = m2;
631 }
632
633 if (putc(FRAME_END, &tp->t_outq)) {
634 /*
635 * Not enough room. Remove a char to make room
636 * and end the packet normally.
637 * If you get many collisions (more than one or two
638 * a day) you probably do not have enough clists
639 * and you should increase "nclist" in param.c.
640 */
641 (void) unputc(&tp->t_outq);
642 (void) putc(FRAME_END, &tp->t_outq);
643 sc->sc_if.if_collisions++;
644 } else {
645 ++sc->sc_if.if_obytes;
646 sc->sc_if.if_opackets++;
647 }
648 }
649 }
650
651 /*
652 * Copy data buffer to mbuf chain; add ifnet pointer.
653 */
654 static struct mbuf *
655 sl_btom(sc, len)
656 register struct sl_softc *sc;
657 register int len;
658 {
659 register struct mbuf *m;
660 register u_char *p;
661
662 MGETHDR(m, M_DONTWAIT, MT_DATA);
663 if (m == NULL)
664 return (NULL);
665
666 /*
667 * If we have more than MHLEN bytes, it's cheaper to
668 * queue the cluster we just filled & allocate a new one
669 * for the input buffer. Otherwise, fill the mbuf we
670 * allocated above. Note that code in the input routine
671 * guarantees that packet will fit in a cluster.
672 */
673 if (len >= MHLEN) {
674 /*
675 * XXX this is that evil trick I mentioned...
676 */
677 p = sc->sc_xxx;
678 sc->sc_xxx = (u_char *)malloc(MCLBYTES, M_MBUF, M_NOWAIT);
679 if (sc->sc_xxx == NULL) {
680 /*
681 * We couldn't allocate a new buffer - if
682 * memory's this low, it's time to start
683 * dropping packets.
684 */
685 (void) m_free(m);
686 return (NULL);
687 }
688 sc->sc_ep = sc->sc_xxx + SLBUFSIZE;
689 MEXTADD(m, p, MCLBYTES, M_MBUF, NULL, NULL);
690 m->m_data = (caddr_t)sc->sc_buf;
691 } else
692 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
693
694 m->m_len = len;
695 m->m_pkthdr.len = len;
696 m->m_pkthdr.rcvif = &sc->sc_if;
697 return (m);
698 }
699
700 /*
701 * tty interface receiver interrupt.
702 */
703 void
704 slinput(c, tp)
705 register int c;
706 register struct tty *tp;
707 {
708 register struct sl_softc *sc;
709 register struct mbuf *m;
710 register int len;
711 int s;
712 #if NBPFILTER > 0
713 u_char chdr[CHDR_LEN];
714 #endif
715
716 tk_nin++;
717 sc = (struct sl_softc *)tp->t_sc;
718 if (sc == NULL)
719 return;
720 if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
721 (tp->t_cflag & CLOCAL) == 0)) {
722 sc->sc_flags |= SC_ERROR;
723 return;
724 }
725 c &= TTY_CHARMASK;
726
727 ++sc->sc_if.if_ibytes;
728
729 if (sc->sc_if.if_flags & IFF_DEBUG) {
730 if (c == ABT_ESC) {
731 /*
732 * If we have a previous abort, see whether
733 * this one is within the time limit.
734 */
735 if (sc->sc_abortcount &&
736 time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
737 sc->sc_abortcount = 0;
738 /*
739 * If we see an abort after "idle" time, count it;
740 * record when the first abort escape arrived.
741 */
742 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
743 if (++sc->sc_abortcount == 1)
744 sc->sc_starttime = time.tv_sec;
745 if (sc->sc_abortcount >= ABT_COUNT) {
746 slclose(tp);
747 return;
748 }
749 }
750 } else
751 sc->sc_abortcount = 0;
752 sc->sc_lasttime = time.tv_sec;
753 }
754
755 switch (c) {
756
757 case TRANS_FRAME_ESCAPE:
758 if (sc->sc_escape)
759 c = FRAME_ESCAPE;
760 break;
761
762 case TRANS_FRAME_END:
763 if (sc->sc_escape)
764 c = FRAME_END;
765 break;
766
767 case FRAME_ESCAPE:
768 sc->sc_escape = 1;
769 return;
770
771 case FRAME_END:
772 if(sc->sc_flags & SC_ERROR) {
773 sc->sc_flags &= ~SC_ERROR;
774 goto newpack;
775 }
776 len = sc->sc_mp - sc->sc_buf;
777 if (len < 3)
778 /* less than min length packet - ignore */
779 goto newpack;
780
781 #if NBPFILTER > 0
782 if (sc->sc_bpf) {
783 /*
784 * Save the compressed header, so we
785 * can tack it on later. Note that we
786 * will end up copying garbage in some
787 * cases but this is okay. We remember
788 * where the buffer started so we can
789 * compute the new header length.
790 */
791 bcopy(sc->sc_buf, chdr, CHDR_LEN);
792 }
793 #endif
794
795 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
796 if (c & 0x80)
797 c = TYPE_COMPRESSED_TCP;
798 else if (c == TYPE_UNCOMPRESSED_TCP)
799 *sc->sc_buf &= 0x4f; /* XXX */
800 /*
801 * We've got something that's not an IP packet.
802 * If compression is enabled, try to decompress it.
803 * Otherwise, if `auto-enable' compression is on and
804 * it's a reasonable packet, decompress it and then
805 * enable compression. Otherwise, drop it.
806 */
807 if (sc->sc_if.if_flags & SC_COMPRESS) {
808 len = sl_uncompress_tcp(&sc->sc_buf, len,
809 (u_int)c, &sc->sc_comp);
810 if (len <= 0)
811 goto error;
812 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
813 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
814 len = sl_uncompress_tcp(&sc->sc_buf, len,
815 (u_int)c, &sc->sc_comp);
816 if (len <= 0)
817 goto error;
818 sc->sc_if.if_flags |= SC_COMPRESS;
819 } else
820 goto error;
821 }
822 #if NBPFILTER > 0
823 if (sc->sc_bpf) {
824 /*
825 * Put the SLIP pseudo-"link header" in place.
826 * We couldn't do this any earlier since
827 * decompression probably moved the buffer
828 * pointer. Then, invoke BPF.
829 */
830 register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
831
832 hp[SLX_DIR] = SLIPDIR_IN;
833 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
834 bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
835 }
836 #endif
837 m = sl_btom(sc, len);
838 if (m == NULL)
839 goto error;
840
841 sc->sc_if.if_ipackets++;
842 sc->sc_if.if_lastchange = time;
843 s = splimp();
844 if (IF_QFULL(&ipintrq)) {
845 IF_DROP(&ipintrq);
846 sc->sc_if.if_ierrors++;
847 sc->sc_if.if_iqdrops++;
848 m_freem(m);
849 } else {
850 IF_ENQUEUE(&ipintrq, m);
851 schednetisr(NETISR_IP);
852 }
853 splx(s);
854 goto newpack;
855 }
856 if (sc->sc_mp < sc->sc_ep) {
857 *sc->sc_mp++ = c;
858 sc->sc_escape = 0;
859 return;
860 }
861
862 /* can't put lower; would miss an extra frame */
863 sc->sc_flags |= SC_ERROR;
864
865 error:
866 sc->sc_if.if_ierrors++;
867 newpack:
868 sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
869 sc->sc_escape = 0;
870 }
871
872 /*
873 * Process an ioctl request.
874 */
875 int
876 slioctl(ifp, cmd, data)
877 register struct ifnet *ifp;
878 u_long cmd;
879 caddr_t data;
880 {
881 register struct ifaddr *ifa = (struct ifaddr *)data;
882 register struct ifreq *ifr;
883 register int s = splimp(), error = 0;
884
885 switch (cmd) {
886
887 case SIOCSIFADDR:
888 if (ifa->ifa_addr->sa_family == AF_INET)
889 ifp->if_flags |= IFF_UP;
890 else
891 error = EAFNOSUPPORT;
892 break;
893
894 case SIOCSIFDSTADDR:
895 if (ifa->ifa_addr->sa_family != AF_INET)
896 error = EAFNOSUPPORT;
897 break;
898
899 case SIOCADDMULTI:
900 case SIOCDELMULTI:
901 ifr = (struct ifreq *)data;
902 if (ifr == 0) {
903 error = EAFNOSUPPORT; /* XXX */
904 break;
905 }
906 switch (ifr->ifr_addr.sa_family) {
907
908 #ifdef INET
909 case AF_INET:
910 break;
911 #endif
912
913 default:
914 error = EAFNOSUPPORT;
915 break;
916 }
917 break;
918
919 default:
920 error = EINVAL;
921 }
922 splx(s);
923 return (error);
924 }
925 #endif
926