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