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