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