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