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