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