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