if_sl.c revision 1.44 1 /* $NetBSD: if_sl.c,v 1.44 1996/10/13 02:11:04 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_unit = i; /* XXX */
205 sprintf(sc->sc_if.if_xname, "sl%d", i++);
206 sc->sc_if.if_softc = sc;
207 sc->sc_if.if_mtu = SLMTU;
208 sc->sc_if.if_flags =
209 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
210 sc->sc_if.if_type = IFT_SLIP;
211 sc->sc_if.if_ioctl = slioctl;
212 sc->sc_if.if_output = sloutput;
213 sc->sc_if.if_snd.ifq_maxlen = 50;
214 sc->sc_fastq.ifq_maxlen = 32;
215 if_attach(&sc->sc_if);
216 #if NBPFILTER > 0
217 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
218 #endif
219 }
220 }
221
222 static int
223 slinit(sc)
224 register struct sl_softc *sc;
225 {
226 register caddr_t p;
227
228 if (sc->sc_ep == (u_char *) 0) {
229 MCLALLOC(p, M_WAIT);
230 if (p)
231 sc->sc_ep = (u_char *)p + SLBUFSIZE;
232 else {
233 printf("sl%d: can't allocate buffer\n", sc->sc_unit);
234 sc->sc_if.if_flags &= ~IFF_UP;
235 return (0);
236 }
237 }
238 sc->sc_buf = sc->sc_ep - SLMAX;
239 sc->sc_mp = sc->sc_buf;
240 sl_compress_init(&sc->sc_comp, -1);
241 return (1);
242 }
243
244 /*
245 * Line specific open routine.
246 * Attach the given tty to the first available sl unit.
247 */
248 /* ARGSUSED */
249 int
250 slopen(dev, tp)
251 dev_t dev;
252 register struct tty *tp;
253 {
254 struct proc *p = curproc; /* XXX */
255 register struct sl_softc *sc;
256 register int nsl;
257 int error;
258 #ifdef NetBSD
259 int s;
260 #endif
261
262 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
263 return (error);
264
265 if (tp->t_line == SLIPDISC)
266 return (0);
267
268 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
269 if (sc->sc_ttyp == NULL) {
270 if (slinit(sc) == 0)
271 return (ENOBUFS);
272 tp->t_sc = (caddr_t)sc;
273 sc->sc_ttyp = tp;
274 sc->sc_if.if_baudrate = tp->t_ospeed;
275 ttyflush(tp, FREAD | FWRITE);
276 #ifdef NetBSD
277 /*
278 * make sure tty output queue is large enough
279 * to hold a full-sized packet (including frame
280 * end, and a possible extra frame end). full-sized
281 * packet occupies a max of 2*SLMTU bytes (because
282 * of possible escapes), and add two on for frame
283 * ends.
284 */
285 s = spltty();
286 if (tp->t_outq.c_cn < 2*SLMTU+2) {
287 sc->sc_oldbufsize = tp->t_outq.c_cn;
288 sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
289
290 clfree(&tp->t_outq);
291 error = clalloc(&tp->t_outq, 3*SLMTU, 0);
292 if (error) {
293 splx(s);
294 return(error);
295 }
296 } else
297 sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
298 splx(s);
299 #endif /* NetBSD */
300 return (0);
301 }
302 return (ENXIO);
303 }
304
305 /*
306 * Line specific close routine.
307 * Detach the tty from the sl unit.
308 */
309 void
310 slclose(tp)
311 struct tty *tp;
312 {
313 register struct sl_softc *sc;
314 int s;
315
316 ttywflush(tp);
317 s = splimp(); /* actually, max(spltty, splsoftnet) */
318 tp->t_line = 0;
319 sc = (struct sl_softc *)tp->t_sc;
320 if (sc != NULL) {
321 if_down(&sc->sc_if);
322 sc->sc_ttyp = NULL;
323 tp->t_sc = NULL;
324 MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
325 sc->sc_ep = 0;
326 sc->sc_mp = 0;
327 sc->sc_buf = 0;
328 }
329 #ifdef NetBSD
330 /* if necessary, install a new outq buffer of the appropriate size */
331 if (sc->sc_oldbufsize != 0) {
332 clfree(&tp->t_outq);
333 clalloc(&tp->t_outq, sc->sc_oldbufsize, sc->sc_oldbufquot);
334 }
335 #endif
336 splx(s);
337 }
338
339 /*
340 * Line specific (tty) ioctl routine.
341 * Provide a way to get the sl unit number.
342 */
343 /* ARGSUSED */
344 int
345 sltioctl(tp, cmd, data, flag)
346 struct tty *tp;
347 u_long cmd;
348 caddr_t data;
349 int flag;
350 {
351 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
352
353 switch (cmd) {
354 case SLIOCGUNIT:
355 *(int *)data = sc->sc_unit; /* XXX */
356 break;
357
358 default:
359 return (-1);
360 }
361 return (0);
362 }
363
364 /*
365 * Queue a packet. Start transmission if not active.
366 * Compression happens in slstart; if we do it here, IP TOS
367 * will cause us to not compress "background" packets, because
368 * ordering gets trashed. It can be done for all packets in slstart.
369 */
370 int
371 sloutput(ifp, m, dst, rtp)
372 struct ifnet *ifp;
373 register struct mbuf *m;
374 struct sockaddr *dst;
375 struct rtentry *rtp;
376 {
377 register struct sl_softc *sc = ifp->if_softc;
378 register struct ip *ip;
379 register struct ifqueue *ifq;
380 int s;
381
382 /*
383 * `Cannot happen' (see slioctl). Someday we will extend
384 * the line protocol to support other address families.
385 */
386 if (dst->sa_family != AF_INET) {
387 printf("%s: af%d not supported\n", sc->sc_if.if_xname,
388 dst->sa_family);
389 m_freem(m);
390 sc->sc_if.if_noproto++;
391 return (EAFNOSUPPORT);
392 }
393
394 if (sc->sc_ttyp == NULL) {
395 m_freem(m);
396 return (ENETDOWN); /* sort of */
397 }
398 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
399 (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
400 m_freem(m);
401 return (EHOSTUNREACH);
402 }
403 ifq = &sc->sc_if.if_snd;
404 ip = mtod(m, struct ip *);
405 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
406 m_freem(m);
407 return (ENETRESET); /* XXX ? */
408 }
409 if (ip->ip_tos & IPTOS_LOWDELAY)
410 ifq = &sc->sc_fastq;
411 s = splimp();
412 if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
413 struct timeval tv;
414
415 /* if output's been stalled for too long, and restart */
416 timersub(&time, &sc->sc_if.if_lastchange, &tv);
417 if (tv.tv_sec > 0) {
418 sc->sc_otimeout++;
419 slstart(sc->sc_ttyp);
420 }
421 }
422 if (IF_QFULL(ifq)) {
423 IF_DROP(ifq);
424 m_freem(m);
425 splx(s);
426 sc->sc_if.if_oerrors++;
427 return (ENOBUFS);
428 }
429 IF_ENQUEUE(ifq, m);
430 sc->sc_if.if_lastchange = time;
431 if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
432 slstart(sc->sc_ttyp);
433 splx(s);
434 return (0);
435 }
436
437 /*
438 * Start output on interface. Get another datagram
439 * to send from the interface queue and map it to
440 * the interface before starting output.
441 */
442 void
443 slstart(tp)
444 register struct tty *tp;
445 {
446 register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
447 register struct mbuf *m;
448 register u_char *cp;
449 register struct ip *ip;
450 int s;
451 struct mbuf *m2;
452 #if NBPFILTER > 0
453 u_char bpfbuf[SLMTU + SLIP_HDRLEN];
454 register int len = 0;
455 #endif
456 #ifndef NetBSD /* XXX - cgd */
457 extern int cfreecount;
458 #endif
459
460 for (;;) {
461 /*
462 * If there is more in the output queue, just send it now.
463 * We are being called in lieu of ttstart and must do what
464 * it would.
465 */
466 if (tp->t_outq.c_cc != 0) {
467 (*tp->t_oproc)(tp);
468 if (tp->t_outq.c_cc > SLIP_HIWAT)
469 return;
470 }
471 /*
472 * This happens briefly when the line shuts down.
473 */
474 if (sc == NULL)
475 return;
476
477 #ifdef NetBSD /* XXX - cgd */
478 /*
479 * Do not remove the packet from the IP queue if it
480 * doesn't look like the packet will fit into the
481 * current serial output queue, with a packet full of
482 * escapes this could be as bad as SLMTU*2+2.
483 */
484 if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2*SLMTU+2)
485 return;
486 #endif /* NetBSD */
487
488 /*
489 * Get a packet and send it to the interface.
490 */
491 s = splimp();
492 IF_DEQUEUE(&sc->sc_fastq, m);
493 if (m)
494 sc->sc_if.if_omcasts++; /* XXX */
495 else
496 IF_DEQUEUE(&sc->sc_if.if_snd, m);
497 splx(s);
498 if (m == NULL)
499 return;
500
501 /*
502 * We do the header compression here rather than in sloutput
503 * because the packets will be out of order if we are using TOS
504 * queueing, and the connection id compression will get
505 * munged when this happens.
506 */
507 #if NBPFILTER > 0
508 if (sc->sc_bpf) {
509 /*
510 * We need to save the TCP/IP header before it's
511 * compressed. To avoid complicated code, we just
512 * copy the entire packet into a stack buffer (since
513 * this is a serial line, packets should be short
514 * and/or the copy should be negligible cost compared
515 * to the packet transmission time).
516 */
517 register struct mbuf *m1 = m;
518 register u_char *cp = bpfbuf + SLIP_HDRLEN;
519
520 len = 0;
521 do {
522 register int mlen = m1->m_len;
523
524 bcopy(mtod(m1, caddr_t), cp, mlen);
525 cp += mlen;
526 len += mlen;
527 } while ((m1 = m1->m_next) != NULL);
528 }
529 #endif
530 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
531 if (sc->sc_if.if_flags & SC_COMPRESS)
532 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
533 &sc->sc_comp, 1);
534 }
535 #if NBPFILTER > 0
536 if (sc->sc_bpf) {
537 /*
538 * Put the SLIP pseudo-"link header" in place. The
539 * compressed header is now at the beginning of the
540 * mbuf.
541 */
542 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
543 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
544 bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
545 }
546 #endif
547 sc->sc_if.if_lastchange = time;
548
549 #ifndef __NetBSD__ /* XXX - cgd */
550 /*
551 * If system is getting low on clists, just flush our
552 * output queue (if the stuff was important, it'll get
553 * retransmitted).
554 */
555 if (cfreecount < CLISTRESERVE + SLMTU) {
556 m_freem(m);
557 sc->sc_if.if_collisions++;
558 continue;
559 }
560 #endif /* !__NetBSD__ */
561 /*
562 * The extra FRAME_END will start up a new packet, and thus
563 * will flush any accumulated garbage. We do this whenever
564 * the line may have been idle for some time.
565 */
566 if (tp->t_outq.c_cc == 0) {
567 ++sc->sc_if.if_obytes;
568 (void) putc(FRAME_END, &tp->t_outq);
569 }
570
571 while (m) {
572 register u_char *ep;
573
574 cp = mtod(m, u_char *); ep = cp + m->m_len;
575 while (cp < ep) {
576 /*
577 * Find out how many bytes in the string we can
578 * handle without doing something special.
579 */
580 register u_char *bp = cp;
581
582 while (cp < ep) {
583 switch (*cp++) {
584 case FRAME_ESCAPE:
585 case FRAME_END:
586 --cp;
587 goto out;
588 }
589 }
590 out:
591 if (cp > bp) {
592 /*
593 * Put n characters at once
594 * into the tty output queue.
595 */
596 #ifdef __NetBSD__ /* XXX - cgd */
597 if (b_to_q((u_char *)bp, cp - bp,
598 #else
599 if (b_to_q((char *)bp, cp - bp,
600 #endif
601 &tp->t_outq))
602 break;
603 sc->sc_if.if_obytes += cp - bp;
604 }
605 /*
606 * If there are characters left in the mbuf,
607 * the first one must be special..
608 * Put it out in a different form.
609 */
610 if (cp < ep) {
611 if (putc(FRAME_ESCAPE, &tp->t_outq))
612 break;
613 if (putc(*cp++ == FRAME_ESCAPE ?
614 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
615 &tp->t_outq)) {
616 (void) unputc(&tp->t_outq);
617 break;
618 }
619 sc->sc_if.if_obytes += 2;
620 }
621 }
622 MFREE(m, m2);
623 m = m2;
624 }
625
626 if (putc(FRAME_END, &tp->t_outq)) {
627 /*
628 * Not enough room. Remove a char to make room
629 * and end the packet normally.
630 * If you get many collisions (more than one or two
631 * a day) you probably do not have enough clists
632 * and you should increase "nclist" in param.c.
633 */
634 (void) unputc(&tp->t_outq);
635 (void) putc(FRAME_END, &tp->t_outq);
636 sc->sc_if.if_collisions++;
637 } else {
638 ++sc->sc_if.if_obytes;
639 sc->sc_if.if_opackets++;
640 }
641 }
642 }
643
644 /*
645 * Copy data buffer to mbuf chain; add ifnet pointer.
646 */
647 static struct mbuf *
648 sl_btom(sc, len)
649 register struct sl_softc *sc;
650 register int len;
651 {
652 register struct mbuf *m;
653
654 MGETHDR(m, M_DONTWAIT, MT_DATA);
655 if (m == NULL)
656 return (NULL);
657
658 /*
659 * If we have more than MHLEN bytes, it's cheaper to
660 * queue the cluster we just filled & allocate a new one
661 * for the input buffer. Otherwise, fill the mbuf we
662 * allocated above. Note that code in the input routine
663 * guarantees that packet will fit in a cluster.
664 */
665 if (len >= MHLEN) {
666 MCLGET(m, M_DONTWAIT);
667 if ((m->m_flags & M_EXT) == 0) {
668 /*
669 * we couldn't get a cluster - if memory's this
670 * low, it's time to start dropping packets.
671 */
672 (void) m_free(m);
673 return (NULL);
674 }
675 sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
676 m->m_data = (caddr_t)sc->sc_buf;
677 m->m_ext.ext_buf = (caddr_t)((long)sc->sc_buf &~ MCLOFSET);
678 } else
679 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
680
681 m->m_len = len;
682 m->m_pkthdr.len = len;
683 m->m_pkthdr.rcvif = &sc->sc_if;
684 return (m);
685 }
686
687 /*
688 * tty interface receiver interrupt.
689 */
690 void
691 slinput(c, tp)
692 register int c;
693 register struct tty *tp;
694 {
695 register struct sl_softc *sc;
696 register struct mbuf *m;
697 register int len;
698 int s;
699 #if NBPFILTER > 0
700 u_char chdr[CHDR_LEN];
701 #endif
702
703 tk_nin++;
704 sc = (struct sl_softc *)tp->t_sc;
705 if (sc == NULL)
706 return;
707 if (c & TTY_ERRORMASK || ((tp->t_state & TS_CARR_ON) == 0 &&
708 (tp->t_cflag & CLOCAL) == 0)) {
709 sc->sc_flags |= SC_ERROR;
710 return;
711 }
712 c &= TTY_CHARMASK;
713
714 ++sc->sc_if.if_ibytes;
715
716 if (sc->sc_if.if_flags & IFF_DEBUG) {
717 if (c == ABT_ESC) {
718 /*
719 * If we have a previous abort, see whether
720 * this one is within the time limit.
721 */
722 if (sc->sc_abortcount &&
723 time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
724 sc->sc_abortcount = 0;
725 /*
726 * If we see an abort after "idle" time, count it;
727 * record when the first abort escape arrived.
728 */
729 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
730 if (++sc->sc_abortcount == 1)
731 sc->sc_starttime = time.tv_sec;
732 if (sc->sc_abortcount >= ABT_COUNT) {
733 slclose(tp);
734 return;
735 }
736 }
737 } else
738 sc->sc_abortcount = 0;
739 sc->sc_lasttime = time.tv_sec;
740 }
741
742 switch (c) {
743
744 case TRANS_FRAME_ESCAPE:
745 if (sc->sc_escape)
746 c = FRAME_ESCAPE;
747 break;
748
749 case TRANS_FRAME_END:
750 if (sc->sc_escape)
751 c = FRAME_END;
752 break;
753
754 case FRAME_ESCAPE:
755 sc->sc_escape = 1;
756 return;
757
758 case FRAME_END:
759 if(sc->sc_flags & SC_ERROR) {
760 sc->sc_flags &= ~SC_ERROR;
761 goto newpack;
762 }
763 len = sc->sc_mp - sc->sc_buf;
764 if (len < 3)
765 /* less than min length packet - ignore */
766 goto newpack;
767
768 #if NBPFILTER > 0
769 if (sc->sc_bpf) {
770 /*
771 * Save the compressed header, so we
772 * can tack it on later. Note that we
773 * will end up copying garbage in some
774 * cases but this is okay. We remember
775 * where the buffer started so we can
776 * compute the new header length.
777 */
778 bcopy(sc->sc_buf, chdr, CHDR_LEN);
779 }
780 #endif
781
782 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
783 if (c & 0x80)
784 c = TYPE_COMPRESSED_TCP;
785 else if (c == TYPE_UNCOMPRESSED_TCP)
786 *sc->sc_buf &= 0x4f; /* XXX */
787 /*
788 * We've got something that's not an IP packet.
789 * If compression is enabled, try to decompress it.
790 * Otherwise, if `auto-enable' compression is on and
791 * it's a reasonable packet, decompress it and then
792 * enable compression. Otherwise, drop it.
793 */
794 if (sc->sc_if.if_flags & SC_COMPRESS) {
795 len = sl_uncompress_tcp(&sc->sc_buf, len,
796 (u_int)c, &sc->sc_comp);
797 if (len <= 0)
798 goto error;
799 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
800 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
801 len = sl_uncompress_tcp(&sc->sc_buf, len,
802 (u_int)c, &sc->sc_comp);
803 if (len <= 0)
804 goto error;
805 sc->sc_if.if_flags |= SC_COMPRESS;
806 } else
807 goto error;
808 }
809 #if NBPFILTER > 0
810 if (sc->sc_bpf) {
811 /*
812 * Put the SLIP pseudo-"link header" in place.
813 * We couldn't do this any earlier since
814 * decompression probably moved the buffer
815 * pointer. Then, invoke BPF.
816 */
817 register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
818
819 hp[SLX_DIR] = SLIPDIR_IN;
820 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
821 bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
822 }
823 #endif
824 m = sl_btom(sc, len);
825 if (m == NULL)
826 goto error;
827
828 sc->sc_if.if_ipackets++;
829 sc->sc_if.if_lastchange = time;
830 s = splimp();
831 if (IF_QFULL(&ipintrq)) {
832 IF_DROP(&ipintrq);
833 sc->sc_if.if_ierrors++;
834 sc->sc_if.if_iqdrops++;
835 m_freem(m);
836 } else {
837 IF_ENQUEUE(&ipintrq, m);
838 schednetisr(NETISR_IP);
839 }
840 splx(s);
841 goto newpack;
842 }
843 if (sc->sc_mp < sc->sc_ep) {
844 *sc->sc_mp++ = c;
845 sc->sc_escape = 0;
846 return;
847 }
848
849 /* can't put lower; would miss an extra frame */
850 sc->sc_flags |= SC_ERROR;
851
852 error:
853 sc->sc_if.if_ierrors++;
854 newpack:
855 sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
856 sc->sc_escape = 0;
857 }
858
859 /*
860 * Process an ioctl request.
861 */
862 int
863 slioctl(ifp, cmd, data)
864 register struct ifnet *ifp;
865 u_long cmd;
866 caddr_t data;
867 {
868 register struct ifaddr *ifa = (struct ifaddr *)data;
869 register struct ifreq *ifr;
870 register int s = splimp(), error = 0;
871
872 switch (cmd) {
873
874 case SIOCSIFADDR:
875 if (ifa->ifa_addr->sa_family == AF_INET)
876 ifp->if_flags |= IFF_UP;
877 else
878 error = EAFNOSUPPORT;
879 break;
880
881 case SIOCSIFDSTADDR:
882 if (ifa->ifa_addr->sa_family != AF_INET)
883 error = EAFNOSUPPORT;
884 break;
885
886 case SIOCADDMULTI:
887 case SIOCDELMULTI:
888 ifr = (struct ifreq *)data;
889 if (ifr == 0) {
890 error = EAFNOSUPPORT; /* XXX */
891 break;
892 }
893 switch (ifr->ifr_addr.sa_family) {
894
895 #ifdef INET
896 case AF_INET:
897 break;
898 #endif
899
900 default:
901 error = EAFNOSUPPORT;
902 break;
903 }
904 break;
905
906 default:
907 error = EINVAL;
908 }
909 splx(s);
910 return (error);
911 }
912 #endif
913