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