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