If compressed TCP packets were being received on a SLIP
[unix-history] / usr / src / sys / net / if_sl.c
CommitLineData
5b519e94 1/*
c8dfc989
KB
2 * Copyright (c) 1987, 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5b519e94 4 *
dbf0c423 5 * %sccs.include.redist.c%
5b519e94 6 *
5f3344a7 7 * @(#)if_sl.c 8.6 (Berkeley) %G%
5b519e94 8 */
7c2732d1 9
41b6e003
MK
10/*
11 * Serial Line interface
12 *
13 * Rick Adams
14 * Center for Seismic Studies
15 * 1300 N 17th Street, Suite 1450
16 * Arlington, Virginia 22209
17 * (703)276-7900
18 * rick@seismo.ARPA
19 * seismo!rick
20 *
41b6e003 21 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
8011f5df 22 * N.B.: this belongs in netinet, not net, the way it stands now.
7c2732d1
MK
23 * Should have a link-layer type designation, but wouldn't be
24 * backwards-compatible.
41b6e003
MK
25 *
26 * Converted to 4.3BSD Beta by Chris Torek.
8011f5df 27 * Other changes made at Berkeley, based in part on code by Kirk Smith.
270f7144
MK
28 * W. Jolitz added slip abort.
29 *
30 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
31 * Added priority queuing for "interactive" traffic; hooks for TCP
32 * header compression; ICMP filtering (at 2400 baud, some cretin
33 * pinging you can use up all your bandwidth). Made low clist behavior
34 * more robust and slightly less likely to hang serial line.
35 * Sped up a bunch of things.
36 *
37 * Note that splimp() is used throughout to block both (tty) input
38 * interrupts and network activity; thus, splimp must be >= spltty.
41b6e003
MK
39 */
40
41b6e003
MK
41#include "sl.h"
42#if NSL > 0
43
3509f52f
CD
44#include "bpfilter.h"
45
5548a02f
KB
46#include <sys/param.h>
47#include <sys/proc.h>
48#include <sys/mbuf.h>
49#include <sys/buf.h>
50#include <sys/dkstat.h>
51#include <sys/socket.h>
52#include <sys/ioctl.h>
53#include <sys/file.h>
54#include <sys/tty.h>
55#include <sys/kernel.h>
56#include <sys/conf.h>
41b6e003 57
5548a02f
KB
58#include <machine/cpu.h>
59
60#include <net/if.h>
61#include <net/if_types.h>
62#include <net/netisr.h>
63#include <net/route.h>
f81dc893 64
8011f5df 65#if INET
5548a02f
KB
66#include <netinet/in.h>
67#include <netinet/in_systm.h>
68#include <netinet/in_var.h>
69#include <netinet/ip.h>
06242857
MK
70#else
71Huh? Slip without inet?
8011f5df 72#endif
41b6e003 73
5548a02f
KB
74#include <net/slcompress.h>
75#include <net/if_slvar.h>
5f3344a7 76#include <net/slip.h>
270f7144 77
3509f52f
CD
78#if NBPFILTER > 0
79#include <sys/time.h>
80#include <net/bpf.h>
81#endif
82
41b6e003 83/*
af951bc0 84 * SLMAX is a hard limit on input packet size. To simplify the code
270f7144 85 * and improve performance, we require that packets fit in an mbuf
af951bc0
SL
86 * cluster, and if we get a compressed packet, there's enough extra
87 * room to expand the header into a max length tcp/ip header (128
88 * bytes). So, SLMAX can be at most
89 * MCLBYTES - 128
270f7144 90 *
af951bc0
SL
91 * SLMTU is a hard limit on output packet size. To insure good
92 * interactive response, SLMTU wants to be the smallest size that
93 * amortizes the header cost. (Remember that even with
94 * type-of-service queuing, we have to wait for any in-progress
95 * packet to finish. I.e., we wait, on the average, 1/2 * mtu /
96 * cps, where cps is the line speed in characters per second.
97 * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line. The
98 * average compressed header size is 6-8 bytes so any MTU > 90
99 * bytes will give us 90% of the line bandwidth. A 100ms wait is
100 * tolerable (500ms is not), so want an MTU around 296. (Since TCP
101 * will send 256 byte segments (to allow for 40 byte headers), the
102 * typical packet size on the wire will be around 260 bytes). In
103 * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
104 * leave the interface MTU relatively high (so we don't IP fragment
105 * when acting as a gateway to someone using a stupid MTU).
106 *
107 * Similar considerations apply to SLIP_HIWAT: It's the amount of
108 * data that will be queued 'downstream' of us (i.e., in clists
109 * waiting to be picked up by the tty output interrupt). If we
110 * queue a lot of data downstream, it's immune to our t.o.s. queuing.
111 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
112 * telnet/ftp will see a 1 sec wait, independent of the mtu (the
113 * wait is dependent on the ftp window size but that's typically
114 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize
115 * the cost (in idle time on the wire) of the tty driver running
116 * off the end of its clists & having to call back slstart for a
117 * new packet. For a tty interface with any buffering at all, this
118 * cost will be zero. Even with a totally brain dead interface (like
119 * the one on a typical workstation), the cost will be <= 1 character
120 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
121 * at most 1% while maintaining good interactive response.
41b6e003 122 */
3509f52f
CD
123#if NBPFILTER > 0
124#define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
125#else
126#define BUFOFFSET (128+sizeof(struct ifnet **))
127#endif
af951bc0
SL
128#define SLMAX (MCLBYTES - BUFOFFSET)
129#define SLBUFSIZE (SLMAX + BUFOFFSET)
130#define SLMTU 296
131#define SLIP_HIWAT roundup(50,CBSIZE)
270f7144 132#define CLISTRESERVE 1024 /* Can't let clists get too low */
b25c0eeb 133
4a23a8aa
WN
134/*
135 * SLIP ABORT ESCAPE MECHANISM:
136 * (inspired by HAYES modem escape arrangement)
137 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
db1ff792 138 * within window time signals a "soft" exit from slip mode by remote end
1db6ff40 139 * if the IFF_DEBUG flag is on.
4a23a8aa 140 */
4a23a8aa 141#define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
3509f52f
CD
142#define ABT_IDLE 1 /* in seconds - idle before an escape */
143#define ABT_COUNT 3 /* count of escapes for abort */
144#define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
4a23a8aa 145
270f7144 146struct sl_softc sl_softc[NSL];
4a23a8aa 147
270f7144
MK
148#define FRAME_END 0xc0 /* Frame End */
149#define FRAME_ESCAPE 0xdb /* Frame Esc */
150#define TRANS_FRAME_END 0xdc /* transposed frame end */
151#define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
41b6e003 152
4dcb4b06 153extern struct timeval time;
41b6e003 154
74d8a69c
KB
155static int slinit __P((struct sl_softc *));
156static struct mbuf *sl_btom __P((struct sl_softc *, int));
157
41b6e003
MK
158/*
159 * Called from boot code to establish sl interfaces.
160 */
74d8a69c 161void
41b6e003
MK
162slattach()
163{
164 register struct sl_softc *sc;
165 register int i = 0;
166
167 for (sc = sl_softc; i < NSL; sc++) {
168 sc->sc_if.if_name = "sl";
c1d0eac5 169 sc->sc_if.if_next = NULL;
41b6e003
MK
170 sc->sc_if.if_unit = i++;
171 sc->sc_if.if_mtu = SLMTU;
78cafbd4
KS
172 sc->sc_if.if_flags =
173 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
4dcb4b06 174 sc->sc_if.if_type = IFT_SLIP;
41b6e003
MK
175 sc->sc_if.if_ioctl = slioctl;
176 sc->sc_if.if_output = sloutput;
270f7144
MK
177 sc->sc_if.if_snd.ifq_maxlen = 50;
178 sc->sc_fastq.ifq_maxlen = 32;
41b6e003 179 if_attach(&sc->sc_if);
3509f52f
CD
180#if NBPFILTER > 0
181 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
182#endif
41b6e003
MK
183 }
184}
185
270f7144
MK
186static int
187slinit(sc)
188 register struct sl_softc *sc;
189{
190 register caddr_t p;
191
192 if (sc->sc_ep == (u_char *) 0) {
193 MCLALLOC(p, M_WAIT);
194 if (p)
b25c0eeb 195 sc->sc_ep = (u_char *)p + SLBUFSIZE;
270f7144
MK
196 else {
197 printf("sl%d: can't allocate buffer\n", sc - sl_softc);
198 sc->sc_if.if_flags &= ~IFF_UP;
199 return (0);
200 }
201 }
af951bc0 202 sc->sc_buf = sc->sc_ep - SLMAX;
270f7144
MK
203 sc->sc_mp = sc->sc_buf;
204 sl_compress_init(&sc->sc_comp);
205 return (1);
206}
207
41b6e003
MK
208/*
209 * Line specific open routine.
210 * Attach the given tty to the first available sl unit.
211 */
8011f5df 212/* ARGSUSED */
74d8a69c 213int
41b6e003
MK
214slopen(dev, tp)
215 dev_t dev;
216 register struct tty *tp;
217{
06242857 218 struct proc *p = curproc; /* XXX */
41b6e003
MK
219 register struct sl_softc *sc;
220 register int nsl;
06c16dfa 221 int error;
41b6e003 222
06242857 223 if (error = suser(p->p_ucred, &p->p_acflag))
06c16dfa 224 return (error);
270f7144 225
8011f5df 226 if (tp->t_line == SLIPDISC)
b25c0eeb 227 return (0);
41b6e003 228
270f7144 229 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
41b6e003 230 if (sc->sc_ttyp == NULL) {
8011f5df
MK
231 if (slinit(sc) == 0)
232 return (ENOBUFS);
41b6e003
MK
233 tp->t_sc = (caddr_t)sc;
234 sc->sc_ttyp = tp;
4dcb4b06 235 sc->sc_if.if_baudrate = tp->t_ospeed;
8011f5df 236 ttyflush(tp, FREAD | FWRITE);
41b6e003
MK
237 return (0);
238 }
8011f5df 239 return (ENXIO);
41b6e003
MK
240}
241
242/*
243 * Line specific close routine.
244 * Detach the tty from the sl unit.
41b6e003 245 */
74d8a69c 246void
41b6e003
MK
247slclose(tp)
248 struct tty *tp;
249{
250 register struct sl_softc *sc;
251 int s;
252
253 ttywflush(tp);
270f7144 254 s = splimp(); /* actually, max(spltty, splnet) */
41b6e003 255 tp->t_line = 0;
41b6e003
MK
256 sc = (struct sl_softc *)tp->t_sc;
257 if (sc != NULL) {
258 if_down(&sc->sc_if);
259 sc->sc_ttyp = NULL;
260 tp->t_sc = NULL;
b25c0eeb 261 MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
270f7144
MK
262 sc->sc_ep = 0;
263 sc->sc_mp = 0;
8011f5df 264 sc->sc_buf = 0;
41b6e003
MK
265 }
266 splx(s);
267}
268
269/*
270 * Line specific (tty) ioctl routine.
271 * Provide a way to get the sl unit number.
272 */
8011f5df 273/* ARGSUSED */
74d8a69c 274int
41b6e003
MK
275sltioctl(tp, cmd, data, flag)
276 struct tty *tp;
4d8438d7 277 int cmd;
41b6e003 278 caddr_t data;
4d8438d7 279 int flag;
41b6e003 280{
4a23a8aa 281 struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
41b6e003 282
4a23a8aa 283 switch (cmd) {
06242857 284 case SLIOCGUNIT:
4a23a8aa
WN
285 *(int *)data = sc->sc_if.if_unit;
286 break;
4a23a8aa 287
4a23a8aa
WN
288 default:
289 return (-1);
fc62c320 290 }
4a23a8aa 291 return (0);
41b6e003
MK
292}
293
294/*
295 * Queue a packet. Start transmission if not active.
3509f52f
CD
296 * Compression happens in slstart; if we do it here, IP TOS
297 * will cause us to not compress "background" packets, because
298 * ordering gets trashed. It can be done for all packets in slstart.
41b6e003 299 */
74d8a69c
KB
300int
301sloutput(ifp, m, dst, rtp)
b25c0eeb 302 struct ifnet *ifp;
41b6e003
MK
303 register struct mbuf *m;
304 struct sockaddr *dst;
74d8a69c 305 struct rtentry *rtp;
41b6e003 306{
b25c0eeb 307 register struct sl_softc *sc = &sl_softc[ifp->if_unit];
270f7144
MK
308 register struct ip *ip;
309 register struct ifqueue *ifq;
41b6e003
MK
310 int s;
311
312 /*
313 * `Cannot happen' (see slioctl). Someday we will extend
314 * the line protocol to support other address families.
315 */
316 if (dst->sa_family != AF_INET) {
b25c0eeb 317 printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
41b6e003
MK
318 dst->sa_family);
319 m_freem(m);
db1ff792 320 sc->sc_if.if_noproto++;
41b6e003
MK
321 return (EAFNOSUPPORT);
322 }
323
41b6e003
MK
324 if (sc->sc_ttyp == NULL) {
325 m_freem(m);
326 return (ENETDOWN); /* sort of */
327 }
3509f52f
CD
328 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
329 (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
eadd9817
MK
330 m_freem(m);
331 return (EHOSTUNREACH);
332 }
b25c0eeb 333 ifq = &sc->sc_if.if_snd;
4d8438d7 334 ip = mtod(m, struct ip *);
3509f52f 335 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
270f7144 336 m_freem(m);
db1ff792 337 return (ENETRESET); /* XXX ? */
4a23a8aa 338 }
3509f52f
CD
339 if (ip->ip_tos & IPTOS_LOWDELAY)
340 ifq = &sc->sc_fastq;
270f7144
MK
341 s = splimp();
342 if (IF_QFULL(ifq)) {
343 IF_DROP(ifq);
41b6e003 344 m_freem(m);
270f7144 345 splx(s);
8011f5df 346 sc->sc_if.if_oerrors++;
41b6e003
MK
347 return (ENOBUFS);
348 }
270f7144 349 IF_ENQUEUE(ifq, m);
4dcb4b06 350 sc->sc_if.if_lastchange = time;
b25c0eeb 351 if (sc->sc_ttyp->t_outq.c_cc == 0)
41b6e003 352 slstart(sc->sc_ttyp);
b25c0eeb 353 splx(s);
41b6e003
MK
354 return (0);
355}
356
357/*
358 * Start output on interface. Get another datagram
359 * to send from the interface queue and map it to
360 * the interface before starting output.
361 */
74d8a69c 362void
41b6e003
MK
363slstart(tp)
364 register struct tty *tp;
365{
366 register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
367 register struct mbuf *m;
8011f5df 368 register u_char *cp;
3509f52f 369 register struct ip *ip;
270f7144 370 int s;
8011f5df 371 struct mbuf *m2;
3509f52f
CD
372#if NBPFILTER > 0
373 u_char bpfbuf[SLMTU + SLIP_HDRLEN];
374 register int len;
375#endif
8011f5df 376 extern int cfreecount;
41b6e003 377
8011f5df
MK
378 for (;;) {
379 /*
380 * If there is more in the output queue, just send it now.
381 * We are being called in lieu of ttstart and must do what
382 * it would.
383 */
270f7144
MK
384 if (tp->t_outq.c_cc != 0) {
385 (*tp->t_oproc)(tp);
386 if (tp->t_outq.c_cc > SLIP_HIWAT)
387 return;
388 }
8011f5df
MK
389 /*
390 * This happens briefly when the line shuts down.
391 */
392 if (sc == NULL)
393 return;
41b6e003 394
8011f5df
MK
395 /*
396 * Get a packet and send it to the interface.
397 */
398 s = splimp();
270f7144 399 IF_DEQUEUE(&sc->sc_fastq, m);
db1ff792
MK
400 if (m)
401 sc->sc_if.if_omcasts++; /* XXX */
402 else
270f7144 403 IF_DEQUEUE(&sc->sc_if.if_snd, m);
8011f5df 404 splx(s);
e8e27e8e
MK
405 if (m == NULL)
406 return;
3509f52f
CD
407
408 /*
409 * We do the header compression here rather than in sloutput
410 * because the packets will be out of order if we are using TOS
411 * queueing, and the connection id compression will get
412 * munged when this happens.
413 */
414#if NBPFILTER > 0
415 if (sc->sc_bpf) {
416 /*
417 * We need to save the TCP/IP header before it's
418 * compressed. To avoid complicated code, we just
419 * copy the entire packet into a stack buffer (since
420 * this is a serial line, packets should be short
421 * and/or the copy should be negligible cost compared
422 * to the packet transmission time).
423 */
424 register struct mbuf *m1 = m;
425 register u_char *cp = bpfbuf + SLIP_HDRLEN;
426
427 len = 0;
428 do {
429 register int mlen = m1->m_len;
430
431 bcopy(mtod(m1, caddr_t), cp, mlen);
432 cp += mlen;
433 len += mlen;
434 } while (m1 = m1->m_next);
435 }
436#endif
437 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
438 if (sc->sc_if.if_flags & SC_COMPRESS)
439 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
440 &sc->sc_comp, 1);
441 }
442#if NBPFILTER > 0
443 if (sc->sc_bpf) {
444 /*
445 * Put the SLIP pseudo-"link header" in place. The
446 * compressed header is now at the beginning of the
447 * mbuf.
448 */
449 bpfbuf[SLX_DIR] = SLIPDIR_OUT;
450 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
451 bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
452 }
453#endif
4dcb4b06 454 sc->sc_if.if_lastchange = time;
0e97cde8 455
270f7144
MK
456 /*
457 * If system is getting low on clists, just flush our
458 * output queue (if the stuff was important, it'll get
459 * retransmitted).
460 */
461 if (cfreecount < CLISTRESERVE + SLMTU) {
462 m_freem(m);
463 sc->sc_if.if_collisions++;
464 continue;
465 }
41b6e003 466 /*
8011f5df
MK
467 * The extra FRAME_END will start up a new packet, and thus
468 * will flush any accumulated garbage. We do this whenever
469 * the line may have been idle for some time.
41b6e003 470 */
270f7144 471 if (tp->t_outq.c_cc == 0) {
db1ff792 472 ++sc->sc_if.if_obytes;
8011f5df 473 (void) putc(FRAME_END, &tp->t_outq);
270f7144 474 }
8011f5df
MK
475
476 while (m) {
270f7144
MK
477 register u_char *ep;
478
479 cp = mtod(m, u_char *); ep = cp + m->m_len;
480 while (cp < ep) {
8011f5df
MK
481 /*
482 * Find out how many bytes in the string we can
483 * handle without doing something special.
484 */
270f7144
MK
485 register u_char *bp = cp;
486
487 while (cp < ep) {
488 switch (*cp++) {
489 case FRAME_ESCAPE:
490 case FRAME_END:
491 --cp;
492 goto out;
493 }
494 }
495 out:
496 if (cp > bp) {
8011f5df
MK
497 /*
498 * Put n characters at once
499 * into the tty output queue.
500 */
3509f52f
CD
501 if (b_to_q((char *)bp, cp - bp,
502 &tp->t_outq))
8011f5df 503 break;
db1ff792 504 sc->sc_if.if_obytes += cp - bp;
8011f5df
MK
505 }
506 /*
507 * If there are characters left in the mbuf,
508 * the first one must be special..
509 * Put it out in a different form.
510 */
270f7144 511 if (cp < ep) {
8011f5df
MK
512 if (putc(FRAME_ESCAPE, &tp->t_outq))
513 break;
270f7144 514 if (putc(*cp++ == FRAME_ESCAPE ?
8011f5df
MK
515 TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
516 &tp->t_outq)) {
517 (void) unputc(&tp->t_outq);
518 break;
519 }
db1ff792 520 sc->sc_if.if_obytes += 2;
8011f5df
MK
521 }
522 }
523 MFREE(m, m2);
524 m = m2;
525 }
41b6e003 526
8011f5df
MK
527 if (putc(FRAME_END, &tp->t_outq)) {
528 /*
529 * Not enough room. Remove a char to make room
530 * and end the packet normally.
531 * If you get many collisions (more than one or two
532 * a day) you probably do not have enough clists
533 * and you should increase "nclist" in param.c.
534 */
535 (void) unputc(&tp->t_outq);
536 (void) putc(FRAME_END, &tp->t_outq);
537 sc->sc_if.if_collisions++;
8011f5df 538 } else {
db1ff792 539 ++sc->sc_if.if_obytes;
270f7144 540 sc->sc_if.if_opackets++;
8011f5df
MK
541 }
542 }
41b6e003
MK
543}
544
545/*
270f7144 546 * Copy data buffer to mbuf chain; add ifnet pointer.
41b6e003 547 */
270f7144
MK
548static struct mbuf *
549sl_btom(sc, len)
550 register struct sl_softc *sc;
41b6e003 551 register int len;
41b6e003 552{
270f7144 553 register struct mbuf *m;
41b6e003 554
270f7144
MK
555 MGETHDR(m, M_DONTWAIT, MT_DATA);
556 if (m == NULL)
557 return (NULL);
fc62c320 558
270f7144 559 /*
b25c0eeb 560 * If we have more than MHLEN bytes, it's cheaper to
270f7144
MK
561 * queue the cluster we just filled & allocate a new one
562 * for the input buffer. Otherwise, fill the mbuf we
563 * allocated above. Note that code in the input routine
564 * guarantees that packet will fit in a cluster.
565 */
270f7144
MK
566 if (len >= MHLEN) {
567 MCLGET(m, M_DONTWAIT);
568 if ((m->m_flags & M_EXT) == 0) {
b25c0eeb
MK
569 /*
570 * we couldn't get a cluster - if memory's this
571 * low, it's time to start dropping packets.
572 */
573 (void) m_free(m);
41b6e003
MK
574 return (NULL);
575 }
b25c0eeb
MK
576 sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
577 m->m_data = (caddr_t)sc->sc_buf;
578 m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET);
270f7144 579 } else
b25c0eeb 580 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
270f7144
MK
581
582 m->m_len = len;
583 m->m_pkthdr.len = len;
584 m->m_pkthdr.rcvif = &sc->sc_if;
585 return (m);
41b6e003
MK
586}
587
588/*
589 * tty interface receiver interrupt.
590 */
74d8a69c 591void
41b6e003
MK
592slinput(c, tp)
593 register int c;
594 register struct tty *tp;
595{
596 register struct sl_softc *sc;
597 register struct mbuf *m;
270f7144 598 register int len;
41b6e003 599 int s;
3509f52f
CD
600#if NBPFILTER > 0
601 u_char chdr[CHDR_LEN];
602#endif
41b6e003 603
8011f5df 604 tk_nin++;
41b6e003
MK
605 sc = (struct sl_softc *)tp->t_sc;
606 if (sc == NULL)
607 return;
3509f52f
CD
608 if (c & TTY_ERRORMASK || ((tp->t_state & TS_CARR_ON) == 0 &&
609 (tp->t_cflag & CLOCAL) == 0)) {
610 sc->sc_flags |= SC_ERROR;
4a23a8aa 611 return;
3509f52f
CD
612 }
613 c &= TTY_CHARMASK;
41b6e003 614
4dcb4b06 615 ++sc->sc_if.if_ibytes;
270f7144 616
db1ff792
MK
617 if (sc->sc_if.if_flags & IFF_DEBUG) {
618 if (c == ABT_ESC) {
619 /*
620 * If we have a previous abort, see whether
621 * this one is within the time limit.
622 */
623 if (sc->sc_abortcount &&
624 time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
270f7144 625 sc->sc_abortcount = 0;
db1ff792
MK
626 /*
627 * If we see an abort after "idle" time, count it;
628 * record when the first abort escape arrived.
629 */
630 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
631 if (++sc->sc_abortcount == 1)
632 sc->sc_starttime = time.tv_sec;
633 if (sc->sc_abortcount >= ABT_COUNT) {
634 slclose(tp);
635 return;
636 }
270f7144 637 }
db1ff792
MK
638 } else
639 sc->sc_abortcount = 0;
270f7144 640 sc->sc_lasttime = time.tv_sec;
4a23a8aa
WN
641 }
642
270f7144 643 switch (c) {
41b6e003 644
270f7144
MK
645 case TRANS_FRAME_ESCAPE:
646 if (sc->sc_escape)
41b6e003 647 c = FRAME_ESCAPE;
270f7144 648 break;
41b6e003 649
270f7144
MK
650 case TRANS_FRAME_END:
651 if (sc->sc_escape)
41b6e003 652 c = FRAME_END;
270f7144 653 break;
41b6e003 654
270f7144
MK
655 case FRAME_ESCAPE:
656 sc->sc_escape = 1;
657 return;
41b6e003 658
270f7144 659 case FRAME_END:
3509f52f
CD
660 if(sc->sc_flags & SC_ERROR) {
661 sc->sc_flags &= ~SC_ERROR;
662 goto newpack;
663 }
270f7144
MK
664 len = sc->sc_mp - sc->sc_buf;
665 if (len < 3)
666 /* less than min length packet - ignore */
667 goto newpack;
668
3509f52f
CD
669#if NBPFILTER > 0
670 if (sc->sc_bpf) {
671 /*
672 * Save the compressed header, so we
673 * can tack it on later. Note that we
674 * will end up copying garbage in some
675 * cases but this is okay. We remember
676 * where the buffer started so we can
677 * compute the new header length.
678 */
679 bcopy(sc->sc_buf, chdr, CHDR_LEN);
680 }
681#endif
682
270f7144
MK
683 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
684 if (c & 0x80)
685 c = TYPE_COMPRESSED_TCP;
686 else if (c == TYPE_UNCOMPRESSED_TCP)
687 *sc->sc_buf &= 0x4f; /* XXX */
af951bc0
SL
688 /*
689 * We've got something that's not an IP packet.
690 * If compression is enabled, try to decompress it.
691 * Otherwise, if `auto-enable' compression is on and
692 * it's a reasonable packet, decompress it and then
693 * enable compression. Otherwise, drop it.
694 */
0e97cde8 695 if (sc->sc_if.if_flags & SC_COMPRESS) {
af951bc0
SL
696 len = sl_uncompress_tcp(&sc->sc_buf, len,
697 (u_int)c, &sc->sc_comp);
698 if (len <= 0)
699 goto error;
0e97cde8 700 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
af951bc0
SL
701 c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
702 len = sl_uncompress_tcp(&sc->sc_buf, len,
703 (u_int)c, &sc->sc_comp);
704 if (len <= 0)
705 goto error;
0e97cde8 706 sc->sc_if.if_flags |= SC_COMPRESS;
af951bc0 707 } else
270f7144
MK
708 goto error;
709 }
3509f52f
CD
710#if NBPFILTER > 0
711 if (sc->sc_bpf) {
712 /*
713 * Put the SLIP pseudo-"link header" in place.
714 * We couldn't do this any earlier since
715 * decompression probably moved the buffer
716 * pointer. Then, invoke BPF.
717 */
718 register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
719
720 hp[SLX_DIR] = SLIPDIR_IN;
721 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
722 bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
723 }
724#endif
270f7144
MK
725 m = sl_btom(sc, len);
726 if (m == NULL)
727 goto error;
41b6e003 728
270f7144 729 sc->sc_if.if_ipackets++;
4dcb4b06 730 sc->sc_if.if_lastchange = time;
270f7144
MK
731 s = splimp();
732 if (IF_QFULL(&ipintrq)) {
733 IF_DROP(&ipintrq);
734 sc->sc_if.if_ierrors++;
4dcb4b06 735 sc->sc_if.if_iqdrops++;
270f7144
MK
736 m_freem(m);
737 } else {
738 IF_ENQUEUE(&ipintrq, m);
739 schednetisr(NETISR_IP);
41b6e003 740 }
270f7144
MK
741 splx(s);
742 goto newpack;
41b6e003 743 }
270f7144
MK
744 if (sc->sc_mp < sc->sc_ep) {
745 *sc->sc_mp++ = c;
746 sc->sc_escape = 0;
41b6e003
MK
747 return;
748 }
3509f52f
CD
749
750 /* can't put lower; would miss an extra frame */
751 sc->sc_flags |= SC_ERROR;
752
270f7144
MK
753error:
754 sc->sc_if.if_ierrors++;
755newpack:
af951bc0 756 sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
270f7144 757 sc->sc_escape = 0;
41b6e003
MK
758}
759
760/*
761 * Process an ioctl request.
762 */
74d8a69c 763int
41b6e003
MK
764slioctl(ifp, cmd, data)
765 register struct ifnet *ifp;
766 int cmd;
767 caddr_t data;
768{
769 register struct ifaddr *ifa = (struct ifaddr *)data;
78cafbd4 770 register struct ifreq *ifr;
78cafbd4 771 register int s = splimp(), error = 0;
41b6e003
MK
772
773 switch (cmd) {
774
775 case SIOCSIFADDR:
b72a6efb 776 if (ifa->ifa_addr->sa_family == AF_INET)
41b6e003
MK
777 ifp->if_flags |= IFF_UP;
778 else
779 error = EAFNOSUPPORT;
780 break;
781
782 case SIOCSIFDSTADDR:
b72a6efb 783 if (ifa->ifa_addr->sa_family != AF_INET)
41b6e003
MK
784 error = EAFNOSUPPORT;
785 break;
786
78cafbd4
KS
787 case SIOCADDMULTI:
788 case SIOCDELMULTI:
789 ifr = (struct ifreq *)data;
790 if (ifr == 0) {
791 error = EAFNOSUPPORT; /* XXX */
792 break;
793 }
794 switch (ifr->ifr_addr.sa_family) {
795
796#ifdef INET
797 case AF_INET:
798 break;
799#endif
800
801 default:
802 error = EAFNOSUPPORT;
803 break;
804 }
805 break;
78cafbd4 806
41b6e003
MK
807 default:
808 error = EINVAL;
809 }
810 splx(s);
811 return (error);
812}
3509f52f 813#endif