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