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