if_sl.c revision 1.81 1 /* $NetBSD: if_sl.c,v 1.81 2002/09/11 05:36:27 itojun 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.81 2002/09/11 05:36:27 itojun 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/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 #include <machine/intr.h>
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; /* 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->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 /*
312 * clalloc() might return -1 which
313 * is no good, so we need to return
314 * something else.
315 */
316 return (ENOMEM); /* XXX ?! */
317 }
318 } else
319 sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
320 splx(s);
321 #endif /* __NetBSD__ */
322 return (0);
323 }
324 return (ENXIO);
325 }
326
327 /*
328 * Line specific close routine.
329 * Detach the tty from the sl unit.
330 */
331 void
332 slclose(tp)
333 struct tty *tp;
334 {
335 struct sl_softc *sc;
336 int s;
337
338 ttywflush(tp);
339 sc = tp->t_sc;
340
341 if (sc != NULL) {
342 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
343 softintr_disestablish(sc->sc_si);
344 #endif
345 s = splnet();
346 if_down(&sc->sc_if);
347 IF_PURGE(&sc->sc_fastq);
348 splx(s);
349
350 s = spltty();
351 tp->t_linesw = linesw[0]; /* default line disc. */
352 tp->t_state = 0;
353
354 sc->sc_ttyp = NULL;
355 tp->t_sc = NULL;
356
357 m_freem(sc->sc_mbuf);
358 sc->sc_mbuf = NULL;
359 sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL;
360 IF_PURGE(&sc->sc_inq);
361
362 /*
363 * If necessary, install a new outq buffer of the
364 * appropriate size.
365 */
366 if (sc->sc_oldbufsize != 0) {
367 clfree(&tp->t_outq);
368 clalloc(&tp->t_outq, sc->sc_oldbufsize,
369 sc->sc_oldbufquot);
370 }
371 splx(s);
372 }
373 }
374
375 /*
376 * Line specific (tty) ioctl routine.
377 * Provide a way to get the sl unit number.
378 */
379 /* ARGSUSED */
380 int
381 sltioctl(tp, cmd, data, flag)
382 struct tty *tp;
383 u_long cmd;
384 caddr_t data;
385 int flag;
386 {
387 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
388
389 switch (cmd) {
390 case SLIOCGUNIT:
391 *(int *)data = sc->sc_unit; /* XXX */
392 break;
393
394 default:
395 return (EPASSTHROUGH);
396 }
397 return (0);
398 }
399
400 /*
401 * Queue a packet. Start transmission if not active.
402 * Compression happens in slintr(); if we do it here, IP TOS
403 * will cause us to not compress "background" packets, because
404 * ordering gets trashed. It can be done for all packets in slintr().
405 */
406 int
407 sloutput(ifp, m, dst, rtp)
408 struct ifnet *ifp;
409 struct mbuf *m;
410 struct sockaddr *dst;
411 struct rtentry *rtp;
412 {
413 struct sl_softc *sc = ifp->if_softc;
414 struct ip *ip;
415 int s, error;
416 ALTQ_DECL(struct altq_pktattr pktattr;)
417
418 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
419
420 /*
421 * `Cannot happen' (see slioctl). Someday we will extend
422 * the line protocol to support other address families.
423 */
424 if (dst->sa_family != AF_INET) {
425 printf("%s: af%d not supported\n", sc->sc_if.if_xname,
426 dst->sa_family);
427 m_freem(m);
428 sc->sc_if.if_noproto++;
429 return (EAFNOSUPPORT);
430 }
431
432 if (sc->sc_ttyp == NULL) {
433 m_freem(m);
434 return (ENETDOWN); /* sort of */
435 }
436 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
437 (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
438 m_freem(m);
439 printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
440 return (EHOSTUNREACH);
441 }
442 ip = mtod(m, struct ip *);
443 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
444 m_freem(m);
445 return (ENETRESET); /* XXX ? */
446 }
447
448 s = spltty();
449 if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
450 struct timeval tv;
451
452 /* if output's been stalled for too long, and restart */
453 timersub(&time, &sc->sc_lastpacket, &tv);
454 if (tv.tv_sec > 0) {
455 sc->sc_otimeout++;
456 slstart(sc->sc_ttyp);
457 }
458 }
459 splx(s);
460
461 s = splnet();
462 if ((ip->ip_tos & IPTOS_LOWDELAY) != 0
463 #ifdef ALTQ
464 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
465 #endif
466 ) {
467 if (IF_QFULL(&sc->sc_fastq)) {
468 IF_DROP(&sc->sc_fastq);
469 m_freem(m);
470 error = ENOBUFS;
471 } else {
472 IF_ENQUEUE(&sc->sc_fastq, m);
473 error = 0;
474 }
475 } else
476 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
477 if (error) {
478 splx(s);
479 ifp->if_oerrors++;
480 return (error);
481 }
482 sc->sc_lastpacket = time;
483 splx(s);
484
485 s = spltty();
486 if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
487 slstart(sc->sc_ttyp);
488 splx(s);
489
490 return (0);
491 }
492
493 /*
494 * Start output on interface. Get another datagram
495 * to send from the interface queue and map it to
496 * the interface before starting output.
497 */
498 void
499 slstart(tp)
500 struct tty *tp;
501 {
502 struct sl_softc *sc = tp->t_sc;
503
504 /*
505 * If there is more in the output queue, just send it now.
506 * We are being called in lieu of ttstart and must do what
507 * it would.
508 */
509 if (tp->t_outq.c_cc != 0) {
510 (*tp->t_oproc)(tp);
511 if (tp->t_outq.c_cc > SLIP_HIWAT)
512 return;
513 }
514
515 /*
516 * This happens briefly when the line shuts down.
517 */
518 if (sc == NULL)
519 return;
520 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
521 softintr_schedule(sc->sc_si);
522 #else
523 {
524 int s = splhigh();
525 schednetisr(NETISR_SLIP);
526 splx(s);
527 }
528 #endif
529 }
530
531 /*
532 * Copy data buffer to mbuf chain; add ifnet pointer.
533 */
534 static struct mbuf *
535 sl_btom(sc, len)
536 struct sl_softc *sc;
537 int len;
538 {
539 struct mbuf *m;
540
541 /*
542 * Allocate a new input buffer and swap.
543 */
544 m = sc->sc_mbuf;
545 MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA);
546 if (sc->sc_mbuf == NULL) {
547 sc->sc_mbuf = m;
548 return (NULL);
549 }
550 MCLGET(sc->sc_mbuf, M_DONTWAIT);
551 if ((sc->sc_mbuf->m_flags & M_EXT) == 0) {
552 m_freem(sc->sc_mbuf);
553 sc->sc_mbuf = m;
554 return (NULL);
555 }
556 sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
557 sc->sc_mbuf->m_ext.ext_size;
558
559 m->m_data = sc->sc_pktstart;
560
561 m->m_pkthdr.len = m->m_len = len;
562 m->m_pkthdr.rcvif = &sc->sc_if;
563 return (m);
564 }
565
566 /*
567 * tty interface receiver interrupt.
568 */
569 void
570 slinput(c, tp)
571 int c;
572 struct tty *tp;
573 {
574 struct sl_softc *sc;
575 struct mbuf *m;
576 int len;
577
578 tk_nin++;
579 sc = (struct sl_softc *)tp->t_sc;
580 if (sc == NULL)
581 return;
582 if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
583 (tp->t_cflag & CLOCAL) == 0)) {
584 sc->sc_flags |= SC_ERROR;
585 return;
586 }
587 c &= TTY_CHARMASK;
588
589 ++sc->sc_if.if_ibytes;
590
591 if (sc->sc_if.if_flags & IFF_DEBUG) {
592 if (c == ABT_ESC) {
593 /*
594 * If we have a previous abort, see whether
595 * this one is within the time limit.
596 */
597 if (sc->sc_abortcount &&
598 time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
599 sc->sc_abortcount = 0;
600 /*
601 * If we see an abort after "idle" time, count it;
602 * record when the first abort escape arrived.
603 */
604 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
605 if (++sc->sc_abortcount == 1)
606 sc->sc_starttime = time.tv_sec;
607 if (sc->sc_abortcount >= ABT_COUNT) {
608 slclose(tp);
609 return;
610 }
611 }
612 } else
613 sc->sc_abortcount = 0;
614 sc->sc_lasttime = time.tv_sec;
615 }
616
617 switch (c) {
618
619 case TRANS_FRAME_ESCAPE:
620 if (sc->sc_escape)
621 c = FRAME_ESCAPE;
622 break;
623
624 case TRANS_FRAME_END:
625 if (sc->sc_escape)
626 c = FRAME_END;
627 break;
628
629 case FRAME_ESCAPE:
630 sc->sc_escape = 1;
631 return;
632
633 case FRAME_END:
634 if(sc->sc_flags & SC_ERROR) {
635 sc->sc_flags &= ~SC_ERROR;
636 goto newpack;
637 }
638 len = sc->sc_mp - sc->sc_pktstart;
639 if (len < 3)
640 /* less than min length packet - ignore */
641 goto newpack;
642
643 m = sl_btom(sc, len);
644 if (m == NULL)
645 goto error;
646
647 IF_ENQUEUE(&sc->sc_inq, m);
648 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
649 softintr_schedule(sc->sc_si);
650 #else
651 {
652 int s = splhigh();
653 schednetisr(NETISR_SLIP);
654 splx(s);
655 }
656 #endif
657 goto newpack;
658 }
659 if (sc->sc_mp < sc->sc_ep) {
660 *sc->sc_mp++ = c;
661 sc->sc_escape = 0;
662 return;
663 }
664
665 /* can't put lower; would miss an extra frame */
666 sc->sc_flags |= SC_ERROR;
667
668 error:
669 sc->sc_if.if_ierrors++;
670 newpack:
671 sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
672 BUFOFFSET;
673 sc->sc_escape = 0;
674 }
675
676 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
677 void
678 slnetisr(void)
679 {
680 struct sl_softc *sc;
681 int i;
682
683 for (i = 0; i < NSL; i++) {
684 sc = &sl_softc[i];
685 if (sc->sc_ttyp == NULL)
686 continue;
687 slintr(sc);
688 }
689 }
690 #endif
691
692 void
693 slintr(void *arg)
694 {
695 struct sl_softc *sc = arg;
696 struct tty *tp = sc->sc_ttyp;
697 struct mbuf *m;
698 int s, len;
699 u_char *pktstart, c;
700 #if NBPFILTER > 0
701 u_char chdr[CHDR_LEN];
702 #endif
703
704 KASSERT(tp != NULL);
705
706 /*
707 * Output processing loop.
708 */
709 for (;;) {
710 struct ip *ip;
711 struct mbuf *m2;
712 #if NBPFILTER > 0
713 struct mbuf *bpf_m;
714 #endif
715
716 /*
717 * Do not remove the packet from the queue if it
718 * doesn't look like it will fit into the current
719 * serial output queue. With a packet full of
720 * escapes, this could be as bad as MTU*2+2.
721 */
722 s = spltty();
723 if (tp->t_outq.c_cn - tp->t_outq.c_cc <
724 2*sc->sc_if.if_mtu+2) {
725 splx(s);
726 break;
727 }
728 splx(s);
729
730 /*
731 * Get a packet and send it to the interface.
732 */
733 s = splnet();
734 IF_DEQUEUE(&sc->sc_fastq, m);
735 if (m)
736 sc->sc_if.if_omcasts++; /* XXX */
737 else
738 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
739 splx(s);
740
741 if (m == NULL)
742 break;
743
744 /*
745 * We do the header compression here rather than in
746 * sloutput() because the packets will be out of order
747 * if we are using TOS queueing, and the connection
748 * ID compression will get munged when this happens.
749 */
750 #if NBPFILTER > 0
751 if (sc->sc_if.if_bpf) {
752 /*
753 * We need to save the TCP/IP header before
754 * it's compressed. To avoid complicated
755 * code, we just make a deep copy of the
756 * entire packet (since this is a serial
757 * line, packets should be short and/or the
758 * copy should be negligible cost compared
759 * to the packet transmission time).
760 */
761 bpf_m = m_dup(m, 0, M_COPYALL, M_DONTWAIT);
762 } else
763 bpf_m = NULL;
764 #endif
765 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
766 if (sc->sc_if.if_flags & SC_COMPRESS)
767 *mtod(m, u_char *) |=
768 sl_compress_tcp(m, ip,
769 &sc->sc_comp, 1);
770 }
771 #if NBPFILTER > 0
772 if (sc->sc_if.if_bpf && bpf_m != NULL) {
773 /*
774 * Put the SLIP pseudo-"link header" in
775 * place. The compressed header is now
776 * at the beginning of the mbuf.
777 */
778 struct mbuf n;
779 u_char *hp;
780
781 n.m_next = bpf_m;
782 n.m_data = n.m_dat;
783 n.m_len = SLIP_HDRLEN;
784
785 hp = mtod(&n, u_char *);
786
787 hp[SLX_DIR] = SLIPDIR_OUT;
788 memcpy(&hp[SLX_CHDR], mtod(m, caddr_t),
789 CHDR_LEN);
790
791 s = splnet();
792 bpf_mtap(sc->sc_if.if_bpf, &n);
793 splx(s);
794 m_freem(bpf_m);
795 }
796 #endif
797 sc->sc_lastpacket = time;
798
799 s = spltty();
800
801 /*
802 * The extra FRAME_END will start up a new packet,
803 * and thus will flush any accumulated garbage. We
804 * do this whenever the line may have been idle for
805 * some time.
806 */
807 if (tp->t_outq.c_cc == 0) {
808 sc->sc_if.if_obytes++;
809 (void) putc(FRAME_END, &tp->t_outq);
810 }
811
812 while (m) {
813 u_char *bp, *cp, *ep;
814
815 bp = cp = mtod(m, u_char *);
816 ep = cp + m->m_len;
817 while (cp < ep) {
818 /*
819 * Find out how many bytes in the
820 * string we can handle without
821 * doing something special.
822 */
823 while (cp < ep) {
824 switch (*cp++) {
825 case FRAME_ESCAPE:
826 case FRAME_END:
827 cp--;
828 goto out;
829 }
830 }
831 out:
832 if (cp > bp) {
833 /*
834 * Put N characters at once
835 * into the tty output queue.
836 */
837 if (b_to_q(bp, cp - bp,
838 &tp->t_outq))
839 break;
840 sc->sc_if.if_obytes += cp - bp;
841 }
842 /*
843 * If there are characters left in
844 * the mbuf, the first one must be
845 * special.. Put it out in a different
846 * form.
847 */
848 if (cp < ep) {
849 if (putc(FRAME_ESCAPE,
850 &tp->t_outq))
851 break;
852 if (putc(*cp++ == FRAME_ESCAPE ?
853 TRANS_FRAME_ESCAPE :
854 TRANS_FRAME_END,
855 &tp->t_outq)) {
856 (void)
857 unputc(&tp->t_outq);
858 break;
859 }
860 sc->sc_if.if_obytes += 2;
861 }
862 bp = cp;
863 }
864 MFREE(m, m2);
865 m = m2;
866 }
867
868 if (putc(FRAME_END, &tp->t_outq)) {
869 /*
870 * Not enough room. Remove a char to make
871 * room and end the packet normally. If
872 * you get many collisions (more than one
873 * or two a day), you probably do not have
874 * enough clists and you should increase
875 * "nclist" in param.c
876 */
877 (void) unputc(&tp->t_outq);
878 (void) putc(FRAME_END, &tp->t_outq);
879 sc->sc_if.if_collisions++;
880 } else {
881 sc->sc_if.if_obytes++;
882 sc->sc_if.if_opackets++;
883 }
884
885 /*
886 * We now have characters in the output queue,
887 * kick the serial port.
888 */
889 (*tp->t_oproc)(tp);
890 splx(s);
891 }
892
893 /*
894 * Input processing loop.
895 */
896 for (;;) {
897 s = spltty();
898 IF_DEQUEUE(&sc->sc_inq, m);
899 splx(s);
900 if (m == NULL)
901 break;
902 pktstart = mtod(m, u_char *);
903 len = m->m_pkthdr.len;
904 #if NBPFILTER > 0
905 if (sc->sc_if.if_bpf) {
906 /*
907 * Save the compressed header, so we
908 * can tack it on later. Note that we
909 * will end up copying garbage in some
910 * cases but this is okay. We remember
911 * where the buffer started so we can
912 * compute the new header length.
913 */
914 memcpy(chdr, pktstart, CHDR_LEN);
915 }
916 #endif /* NBPFILTER > 0 */
917 if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) {
918 if (c & 0x80)
919 c = TYPE_COMPRESSED_TCP;
920 else if (c == TYPE_UNCOMPRESSED_TCP)
921 *pktstart &= 0x4f; /* XXX */
922 /*
923 * We've got something that's not an IP
924 * packet. If compression is enabled,
925 * try to decompress it. Otherwise, if
926 * `auto-enable' compression is on and
927 * it's a reasonable packet, decompress
928 * it and then enable compression.
929 * Otherwise, drop it.
930 */
931 if (sc->sc_if.if_flags & SC_COMPRESS) {
932 len = sl_uncompress_tcp(&pktstart, len,
933 (u_int)c, &sc->sc_comp);
934 if (len <= 0) {
935 m_freem(m);
936 continue;
937 }
938 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
939 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
940 len = sl_uncompress_tcp(&pktstart, len,
941 (u_int)c, &sc->sc_comp);
942 if (len <= 0) {
943 m_freem(m);
944 continue;
945 }
946 sc->sc_if.if_flags |= SC_COMPRESS;
947 } else {
948 m_freem(m);
949 continue;
950 }
951 }
952 m->m_data = (caddr_t) pktstart;
953 m->m_pkthdr.len = m->m_len = len;
954 #if NBPFILTER > 0
955 if (sc->sc_if.if_bpf) {
956 /*
957 * Put the SLIP pseudo-"link header" in place.
958 * Note this M_PREPEND() should bever fail,
959 * since we know we always have enough space
960 * in the input buffer.
961 */
962 u_char *hp;
963
964 M_PREPEND(m, SLIP_HDRLEN, M_DONTWAIT);
965 if (m == NULL)
966 continue;
967
968 hp = mtod(m, u_char *);
969 hp[SLX_DIR] = SLIPDIR_IN;
970 memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
971
972 s = splnet();
973 bpf_mtap(sc->sc_if.if_bpf, m);
974 splx(s);
975
976 m_adj(m, SLIP_HDRLEN);
977 }
978 #endif /* NBPFILTER > 0 */
979 /*
980 * If the packet will fit into a single
981 * header mbuf, copy it into one, to save
982 * memory.
983 */
984 if (m->m_pkthdr.len < MHLEN) {
985 struct mbuf *n;
986
987 MGETHDR(n, M_DONTWAIT, MT_DATA);
988 M_COPY_PKTHDR(n, m);
989 memcpy(mtod(n, caddr_t), mtod(m, caddr_t),
990 m->m_pkthdr.len);
991 n->m_len = m->m_len;
992 m_freem(m);
993 m = n;
994 }
995
996 sc->sc_if.if_ipackets++;
997 sc->sc_lastpacket = time;
998
999 s = splnet();
1000 if (IF_QFULL(&ipintrq)) {
1001 IF_DROP(&ipintrq);
1002 sc->sc_if.if_ierrors++;
1003 sc->sc_if.if_iqdrops++;
1004 m_freem(m);
1005 } else {
1006 IF_ENQUEUE(&ipintrq, m);
1007 schednetisr(NETISR_IP);
1008 }
1009 splx(s);
1010 }
1011 }
1012
1013 /*
1014 * Process an ioctl request.
1015 */
1016 int
1017 slioctl(ifp, cmd, data)
1018 struct ifnet *ifp;
1019 u_long cmd;
1020 caddr_t data;
1021 {
1022 struct ifaddr *ifa = (struct ifaddr *)data;
1023 struct ifreq *ifr = (struct ifreq *)data;
1024 int s = splnet(), error = 0;
1025 struct sl_softc *sc = ifp->if_softc;
1026
1027 switch (cmd) {
1028
1029 case SIOCSIFADDR:
1030 if (ifa->ifa_addr->sa_family == AF_INET)
1031 ifp->if_flags |= IFF_UP;
1032 else
1033 error = EAFNOSUPPORT;
1034 break;
1035
1036 case SIOCSIFDSTADDR:
1037 if (ifa->ifa_addr->sa_family != AF_INET)
1038 error = EAFNOSUPPORT;
1039 break;
1040
1041 case SIOCSIFMTU:
1042 if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
1043 error = EINVAL;
1044 break;
1045 }
1046 sc->sc_if.if_mtu = ifr->ifr_mtu;
1047 break;
1048
1049 case SIOCGIFMTU:
1050 ifr->ifr_mtu = sc->sc_if.if_mtu;
1051 break;
1052
1053 case SIOCADDMULTI:
1054 case SIOCDELMULTI:
1055 if (ifr == 0) {
1056 error = EAFNOSUPPORT; /* XXX */
1057 break;
1058 }
1059 switch (ifr->ifr_addr.sa_family) {
1060
1061 #ifdef INET
1062 case AF_INET:
1063 break;
1064 #endif
1065
1066 default:
1067 error = EAFNOSUPPORT;
1068 break;
1069 }
1070 break;
1071
1072 default:
1073 error = EINVAL;
1074 }
1075 splx(s);
1076 return (error);
1077 }
1078 #endif
1079