if_sl.c revision 1.65 1 /* $NetBSD: if_sl.c,v 1.65 2001/01/09 05:04:23 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 * We always prepend enough space for the SLIP "header".
690 */
691 sc->sc_pktstart -= SLIP_HDRLEN;
692 len += SLIP_HDRLEN;
693
694 /*
695 * If the packet will fit into the header mbuf we allocated
696 * above, copy it there and re-use the input buffer we already
697 * have.
698 */
699 if (len < MHLEN)
700 memcpy(mtod(m, caddr_t), sc->sc_pktstart, len);
701 else {
702 /*
703 * Allocate a new input buffer and swap.
704 */
705 m = sc->sc_mbuf;
706 MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA);
707 if (sc->sc_mbuf == NULL) {
708 sc->sc_mbuf = m;
709 return (NULL);
710 }
711 MCLGET(sc->sc_mbuf, M_DONTWAIT);
712 if ((sc->sc_mbuf->m_flags & M_EXT) == 0) {
713 sc->sc_mbuf = m;
714 return (NULL);
715 }
716 sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
717 sc->sc_mbuf->m_ext.ext_size;
718
719 m->m_data = sc->sc_pktstart;
720 }
721
722 m->m_pkthdr.len = m->m_len = len;
723 m->m_pkthdr.rcvif = &sc->sc_if;
724 return (m);
725 }
726
727 /*
728 * tty interface receiver interrupt.
729 */
730 void
731 slinput(c, tp)
732 int c;
733 struct tty *tp;
734 {
735 struct sl_softc *sc;
736 struct mbuf *m;
737 int len;
738 int s;
739 #if NBPFILTER > 0
740 u_char chdr[CHDR_LEN];
741 #endif
742
743 tk_nin++;
744 sc = (struct sl_softc *)tp->t_sc;
745 if (sc == NULL)
746 return;
747 if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
748 (tp->t_cflag & CLOCAL) == 0)) {
749 sc->sc_flags |= SC_ERROR;
750 return;
751 }
752 c &= TTY_CHARMASK;
753
754 ++sc->sc_if.if_ibytes;
755
756 if (sc->sc_if.if_flags & IFF_DEBUG) {
757 if (c == ABT_ESC) {
758 /*
759 * If we have a previous abort, see whether
760 * this one is within the time limit.
761 */
762 if (sc->sc_abortcount &&
763 time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
764 sc->sc_abortcount = 0;
765 /*
766 * If we see an abort after "idle" time, count it;
767 * record when the first abort escape arrived.
768 */
769 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
770 if (++sc->sc_abortcount == 1)
771 sc->sc_starttime = time.tv_sec;
772 if (sc->sc_abortcount >= ABT_COUNT) {
773 slclose(tp);
774 return;
775 }
776 }
777 } else
778 sc->sc_abortcount = 0;
779 sc->sc_lasttime = time.tv_sec;
780 }
781
782 switch (c) {
783
784 case TRANS_FRAME_ESCAPE:
785 if (sc->sc_escape)
786 c = FRAME_ESCAPE;
787 break;
788
789 case TRANS_FRAME_END:
790 if (sc->sc_escape)
791 c = FRAME_END;
792 break;
793
794 case FRAME_ESCAPE:
795 sc->sc_escape = 1;
796 return;
797
798 case FRAME_END:
799 if(sc->sc_flags & SC_ERROR) {
800 sc->sc_flags &= ~SC_ERROR;
801 goto newpack;
802 }
803 len = sc->sc_mp - sc->sc_pktstart;
804 if (len < 3)
805 /* less than min length packet - ignore */
806 goto newpack;
807
808 #if NBPFILTER > 0
809 if (sc->sc_if.if_bpf) {
810 /*
811 * Save the compressed header, so we
812 * can tack it on later. Note that we
813 * will end up copying garbage in some
814 * cases but this is okay. We remember
815 * where the buffer started so we can
816 * compute the new header length.
817 */
818 bcopy(sc->sc_pktstart, chdr, CHDR_LEN);
819 }
820 #endif
821
822 if ((c = (*sc->sc_pktstart & 0xf0)) != (IPVERSION << 4)) {
823 if (c & 0x80)
824 c = TYPE_COMPRESSED_TCP;
825 else if (c == TYPE_UNCOMPRESSED_TCP)
826 *sc->sc_pktstart &= 0x4f; /* XXX */
827 /*
828 * We've got something that's not an IP packet.
829 * If compression is enabled, try to decompress it.
830 * Otherwise, if `auto-enable' compression is on and
831 * it's a reasonable packet, decompress it and then
832 * enable compression. Otherwise, drop it.
833 */
834 if (sc->sc_if.if_flags & SC_COMPRESS) {
835 len = sl_uncompress_tcp(&sc->sc_pktstart, len,
836 (u_int)c, &sc->sc_comp);
837 if (len <= 0)
838 goto error;
839 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
840 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
841 len = sl_uncompress_tcp(&sc->sc_pktstart, len,
842 (u_int)c, &sc->sc_comp);
843 if (len <= 0)
844 goto error;
845 sc->sc_if.if_flags |= SC_COMPRESS;
846 } else
847 goto error;
848 }
849
850 m = sl_btom(sc, len);
851 if (m == NULL)
852 goto error;
853
854 #if NBPFILTER > 0
855 if (sc->sc_if.if_bpf) {
856 /*
857 * Put the SLIP pseudo-"link header" in place.
858 * Note the space for it has already been
859 * allocated in sl_btom().
860 */
861 u_char *hp = mtod(m, u_char *);
862
863 hp[SLX_DIR] = SLIPDIR_IN;
864 memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
865
866 bpf_mtap(sc->sc_if.if_bpf, m);
867 }
868 #endif
869
870 IF_ENQUEUE(&sc->sc_inq, m);
871 s = splimp();
872 schednetisr(NETISR_SLIP);
873 splx(s);
874
875 goto newpack;
876 }
877 if (sc->sc_mp < sc->sc_ep) {
878 *sc->sc_mp++ = c;
879 sc->sc_escape = 0;
880 return;
881 }
882
883 /* can't put lower; would miss an extra frame */
884 sc->sc_flags |= SC_ERROR;
885
886 error:
887 sc->sc_if.if_ierrors++;
888 newpack:
889 sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
890 BUFOFFSET;
891 sc->sc_escape = 0;
892 }
893
894 void
895 slintr(void)
896 {
897 struct sl_softc *sc;
898 struct mbuf *m;
899 int i, s;
900
901 for (i = 0; i < NSL; i++) {
902 sc = &sl_softc[i];
903 for (;;) {
904 s = spltty();
905 IF_DEQUEUE(&sc->sc_inq, m);
906 splx(s);
907 if (m == NULL)
908 break;
909 #if NBPFILTER > 0
910 if (sc->sc_if.if_bpf) {
911 s = splnet();
912 bpf_mtap(sc->sc_if.if_bpf, m);
913 splx(s);
914 }
915 #endif
916 m_adj(m, SLIP_HDRLEN);
917 sc->sc_if.if_ipackets++;
918 sc->sc_if.if_lastchange = time;
919 s = splimp();
920 if (IF_QFULL(&ipintrq)) {
921 IF_DROP(&ipintrq);
922 sc->sc_if.if_ierrors++;
923 sc->sc_if.if_iqdrops++;
924 m_freem(m);
925 } else {
926 IF_ENQUEUE(&ipintrq, m);
927 schednetisr(NETISR_IP);
928 }
929 splx(s);
930 }
931 }
932 }
933
934 /*
935 * Process an ioctl request.
936 */
937 int
938 slioctl(ifp, cmd, data)
939 struct ifnet *ifp;
940 u_long cmd;
941 caddr_t data;
942 {
943 struct ifaddr *ifa = (struct ifaddr *)data;
944 struct ifreq *ifr = (struct ifreq *)data;
945 int s = splimp(), error = 0;
946 struct sl_softc *sc = ifp->if_softc;
947
948 switch (cmd) {
949
950 case SIOCSIFADDR:
951 if (ifa->ifa_addr->sa_family == AF_INET)
952 ifp->if_flags |= IFF_UP;
953 else
954 error = EAFNOSUPPORT;
955 break;
956
957 case SIOCSIFDSTADDR:
958 if (ifa->ifa_addr->sa_family != AF_INET)
959 error = EAFNOSUPPORT;
960 break;
961
962 case SIOCSIFMTU:
963 if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
964 error = EINVAL;
965 break;
966 }
967 sc->sc_if.if_mtu = ifr->ifr_mtu;
968 break;
969
970 case SIOCGIFMTU:
971 ifr->ifr_mtu = sc->sc_if.if_mtu;
972 break;
973
974 case SIOCADDMULTI:
975 case SIOCDELMULTI:
976 if (ifr == 0) {
977 error = EAFNOSUPPORT; /* XXX */
978 break;
979 }
980 switch (ifr->ifr_addr.sa_family) {
981
982 #ifdef INET
983 case AF_INET:
984 break;
985 #endif
986
987 default:
988 error = EAFNOSUPPORT;
989 break;
990 }
991 break;
992
993 default:
994 error = EINVAL;
995 }
996 splx(s);
997 return (error);
998 }
999 #endif
1000