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