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