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