1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PPP async serial channel driver for Linux. 4 * 5 * Copyright 1999 Paul Mackerras. 6 * 7 * This driver provides the encapsulation and framing for sending 8 * and receiving PPP frames over async serial lines. It relies on 9 * the generic PPP layer to give it frames to send and to process 10 * received frames. It implements the PPP line discipline. 11 * 12 * Part of the code in this driver was inspired by the old async-only 13 * PPP driver, written by Michael Callahan and Al Longyear, and 14 * subsequently hacked by Paul Mackerras. 15 */ 16 17 #include <linux/module.h> 18 #include <linux/kernel.h> 19 #include <linux/skbuff.h> 20 #include <linux/tty.h> 21 #include <linux/netdevice.h> 22 #include <linux/poll.h> 23 #include <linux/crc-ccitt.h> 24 #include <linux/ppp_defs.h> 25 #include <linux/ppp-ioctl.h> 26 #include <linux/ppp_channel.h> 27 #include <linux/spinlock.h> 28 #include <linux/init.h> 29 #include <linux/interrupt.h> 30 #include <linux/jiffies.h> 31 #include <linux/slab.h> 32 #include <linux/unaligned.h> 33 #include <linux/uaccess.h> 34 #include <asm/string.h> 35 36 #define PPP_VERSION "2.4.2" 37 38 #define OBUFSIZE 4096 39 40 /* Structure for storing local state. */ 41 struct asyncppp { 42 struct tty_struct *tty; 43 unsigned int flags; 44 unsigned int state; 45 unsigned int rbits; 46 int mru; 47 spinlock_t xmit_lock; 48 spinlock_t recv_lock; 49 unsigned long xmit_flags; 50 u32 xaccm[8]; 51 u32 raccm; 52 53 struct sk_buff *tpkt; 54 int tpkt_pos; 55 u16 tfcs; 56 unsigned char *optr; 57 unsigned char *olim; 58 unsigned long last_xmit; 59 60 struct sk_buff *rpkt; 61 int lcp_fcs; 62 struct sk_buff_head rqueue; 63 64 struct tasklet_struct tsk; 65 66 refcount_t refcnt; 67 struct completion dead; 68 struct ppp_channel chan; /* interface to generic ppp layer */ 69 unsigned char obuf[OBUFSIZE]; 70 }; 71 72 /* Bit numbers in xmit_flags */ 73 #define XMIT_WAKEUP 0 74 #define XMIT_FULL 1 75 #define XMIT_BUSY 2 76 77 /* State bits */ 78 #define SC_TOSS 1 79 #define SC_ESCAPE 2 80 #define SC_PREV_ERROR 4 81 82 /* Bits in rbits */ 83 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP) 84 85 static int flag_time = HZ; 86 module_param(flag_time, int, 0); 87 MODULE_PARM_DESC(flag_time, "ppp_async: interval between flagged packets (in clock ticks)"); 88 MODULE_DESCRIPTION("PPP async serial channel module"); 89 MODULE_LICENSE("GPL"); 90 MODULE_ALIAS_LDISC(N_PPP); 91 92 /* 93 * Prototypes. 94 */ 95 static int ppp_async_encode(struct asyncppp *ap); 96 static int ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb); 97 static int ppp_async_push(struct asyncppp *ap); 98 static void ppp_async_flush_output(struct asyncppp *ap); 99 static void ppp_async_input(struct asyncppp *ap, const unsigned char *buf, 100 const u8 *flags, int count); 101 static int ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd, 102 unsigned long arg); 103 static void ppp_async_process(struct tasklet_struct *t); 104 105 static void async_lcp_peek(struct asyncppp *ap, unsigned char *data, 106 int len, int inbound); 107 108 static const struct ppp_channel_ops async_ops = { 109 .start_xmit = ppp_async_send, 110 .ioctl = ppp_async_ioctl, 111 }; 112 113 /* 114 * Routines implementing the PPP line discipline. 115 */ 116 117 /* 118 * We have a potential race on dereferencing tty->disc_data, 119 * because the tty layer provides no locking at all - thus one 120 * cpu could be running ppp_asynctty_receive while another 121 * calls ppp_asynctty_close, which zeroes tty->disc_data and 122 * frees the memory that ppp_asynctty_receive is using. The best 123 * way to fix this is to use a rwlock in the tty struct, but for now 124 * we use a single global rwlock for all ttys in ppp line discipline. 125 * 126 * FIXME: this is no longer true. The _close path for the ldisc is 127 * now guaranteed to be sane. 128 */ 129 static DEFINE_RWLOCK(disc_data_lock); 130 131 static struct asyncppp *ap_get(struct tty_struct *tty) 132 { 133 struct asyncppp *ap; 134 135 read_lock(&disc_data_lock); 136 ap = tty->disc_data; 137 if (ap != NULL) 138 refcount_inc(&ap->refcnt); 139 read_unlock(&disc_data_lock); 140 return ap; 141 } 142 143 static void ap_put(struct asyncppp *ap) 144 { 145 if (refcount_dec_and_test(&ap->refcnt)) 146 complete(&ap->dead); 147 } 148 149 /* 150 * Called when a tty is put into PPP line discipline. Called in process 151 * context. 152 */ 153 static int 154 ppp_asynctty_open(struct tty_struct *tty) 155 { 156 struct asyncppp *ap; 157 int err; 158 int speed; 159 160 if (tty->ops->write == NULL) 161 return -EOPNOTSUPP; 162 163 err = -ENOMEM; 164 ap = kzalloc_obj(*ap); 165 if (!ap) 166 goto out; 167 168 /* initialize the asyncppp structure */ 169 ap->tty = tty; 170 ap->mru = PPP_MRU; 171 spin_lock_init(&ap->xmit_lock); 172 spin_lock_init(&ap->recv_lock); 173 ap->xaccm[0] = ~0U; 174 ap->xaccm[3] = 0x60000000U; 175 ap->raccm = ~0U; 176 ap->optr = ap->obuf; 177 ap->olim = ap->obuf; 178 ap->lcp_fcs = -1; 179 180 skb_queue_head_init(&ap->rqueue); 181 tasklet_setup(&ap->tsk, ppp_async_process); 182 183 refcount_set(&ap->refcnt, 1); 184 init_completion(&ap->dead); 185 186 ap->chan.private = ap; 187 ap->chan.ops = &async_ops; 188 ap->chan.mtu = PPP_MRU; 189 speed = tty_get_baud_rate(tty); 190 ap->chan.speed = speed; 191 err = ppp_register_channel(&ap->chan); 192 if (err) 193 goto out_free; 194 195 tty->disc_data = ap; 196 tty->receive_room = 65536; 197 return 0; 198 199 out_free: 200 kfree(ap); 201 out: 202 return err; 203 } 204 205 /* 206 * Called when the tty is put into another line discipline 207 * or it hangs up. We have to wait for any cpu currently 208 * executing in any of the other ppp_asynctty_* routines to 209 * finish before we can call ppp_unregister_channel and free 210 * the asyncppp struct. This routine must be called from 211 * process context, not interrupt or softirq context. 212 */ 213 static void 214 ppp_asynctty_close(struct tty_struct *tty) 215 { 216 struct asyncppp *ap; 217 218 write_lock_irq(&disc_data_lock); 219 ap = tty->disc_data; 220 tty->disc_data = NULL; 221 write_unlock_irq(&disc_data_lock); 222 if (!ap) 223 return; 224 225 /* 226 * We have now ensured that nobody can start using ap from now 227 * on, but we have to wait for all existing users to finish. 228 * Note that ppp_unregister_channel ensures that no calls to 229 * our channel ops (i.e. ppp_async_send/ioctl) are in progress 230 * by the time it returns. 231 */ 232 if (!refcount_dec_and_test(&ap->refcnt)) 233 wait_for_completion(&ap->dead); 234 tasklet_kill(&ap->tsk); 235 236 ppp_unregister_channel(&ap->chan); 237 kfree_skb(ap->rpkt); 238 skb_queue_purge(&ap->rqueue); 239 kfree_skb(ap->tpkt); 240 kfree(ap); 241 } 242 243 /* 244 * Called on tty hangup in process context. 245 * 246 * Wait for I/O to driver to complete and unregister PPP channel. 247 * This is already done by the close routine, so just call that. 248 */ 249 static void ppp_asynctty_hangup(struct tty_struct *tty) 250 { 251 ppp_asynctty_close(tty); 252 } 253 254 /* 255 * Read does nothing - no data is ever available this way. 256 * Pppd reads and writes packets via /dev/ppp instead. 257 */ 258 static ssize_t 259 ppp_asynctty_read(struct tty_struct *tty, struct file *file, u8 *buf, 260 size_t count, void **cookie, unsigned long offset) 261 { 262 return -EAGAIN; 263 } 264 265 /* 266 * Write on the tty does nothing, the packets all come in 267 * from the ppp generic stuff. 268 */ 269 static ssize_t 270 ppp_asynctty_write(struct tty_struct *tty, struct file *file, const u8 *buf, 271 size_t count) 272 { 273 return -EAGAIN; 274 } 275 276 /* 277 * Called in process context only. May be re-entered by multiple 278 * ioctl calling threads. 279 */ 280 281 static int 282 ppp_asynctty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) 283 { 284 struct asyncppp *ap = ap_get(tty); 285 int err, val; 286 int __user *p = (int __user *)arg; 287 288 if (!ap) 289 return -ENXIO; 290 err = -EFAULT; 291 switch (cmd) { 292 case PPPIOCGCHAN: 293 err = -EFAULT; 294 if (put_user(ppp_channel_index(&ap->chan), p)) 295 break; 296 err = 0; 297 break; 298 299 case PPPIOCGUNIT: 300 err = -EFAULT; 301 if (put_user(ppp_unit_number(&ap->chan), p)) 302 break; 303 err = 0; 304 break; 305 306 case TCFLSH: 307 /* flush our buffers and the serial port's buffer */ 308 if (arg == TCIOFLUSH || arg == TCOFLUSH) 309 ppp_async_flush_output(ap); 310 err = n_tty_ioctl_helper(tty, cmd, arg); 311 break; 312 313 case FIONREAD: 314 val = 0; 315 if (put_user(val, p)) 316 break; 317 err = 0; 318 break; 319 320 default: 321 /* Try the various mode ioctls */ 322 err = tty_mode_ioctl(tty, cmd, arg); 323 } 324 325 ap_put(ap); 326 return err; 327 } 328 329 /* May sleep, don't call from interrupt level or with interrupts disabled */ 330 static void 331 ppp_asynctty_receive(struct tty_struct *tty, const u8 *buf, const u8 *cflags, 332 size_t count) 333 { 334 struct asyncppp *ap = ap_get(tty); 335 unsigned long flags; 336 337 if (!ap) 338 return; 339 spin_lock_irqsave(&ap->recv_lock, flags); 340 ppp_async_input(ap, buf, cflags, count); 341 spin_unlock_irqrestore(&ap->recv_lock, flags); 342 if (!skb_queue_empty(&ap->rqueue)) 343 tasklet_schedule(&ap->tsk); 344 ap_put(ap); 345 tty_unthrottle(tty); 346 } 347 348 static void 349 ppp_asynctty_wakeup(struct tty_struct *tty) 350 { 351 struct asyncppp *ap = ap_get(tty); 352 353 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 354 if (!ap) 355 return; 356 set_bit(XMIT_WAKEUP, &ap->xmit_flags); 357 tasklet_schedule(&ap->tsk); 358 ap_put(ap); 359 } 360 361 362 static struct tty_ldisc_ops ppp_ldisc = { 363 .owner = THIS_MODULE, 364 .num = N_PPP, 365 .name = "ppp", 366 .open = ppp_asynctty_open, 367 .close = ppp_asynctty_close, 368 .hangup = ppp_asynctty_hangup, 369 .read = ppp_asynctty_read, 370 .write = ppp_asynctty_write, 371 .ioctl = ppp_asynctty_ioctl, 372 .receive_buf = ppp_asynctty_receive, 373 .write_wakeup = ppp_asynctty_wakeup, 374 }; 375 376 static int __init 377 ppp_async_init(void) 378 { 379 int err; 380 381 err = tty_register_ldisc(&ppp_ldisc); 382 if (err != 0) 383 printk(KERN_ERR "PPP_async: error %d registering line disc.\n", 384 err); 385 return err; 386 } 387 388 /* 389 * The following routines provide the PPP channel interface. 390 */ 391 static int 392 ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg) 393 { 394 struct asyncppp *ap = chan->private; 395 void __user *argp = (void __user *)arg; 396 int __user *p = argp; 397 int err, val; 398 u32 accm[8]; 399 400 err = -EFAULT; 401 switch (cmd) { 402 case PPPIOCGFLAGS: 403 val = ap->flags | ap->rbits; 404 if (put_user(val, p)) 405 break; 406 err = 0; 407 break; 408 case PPPIOCSFLAGS: 409 if (get_user(val, p)) 410 break; 411 ap->flags = val & ~SC_RCV_BITS; 412 spin_lock_irq(&ap->recv_lock); 413 ap->rbits = val & SC_RCV_BITS; 414 spin_unlock_irq(&ap->recv_lock); 415 err = 0; 416 break; 417 418 case PPPIOCGASYNCMAP: 419 if (put_user(ap->xaccm[0], (u32 __user *)argp)) 420 break; 421 err = 0; 422 break; 423 case PPPIOCSASYNCMAP: 424 if (get_user(ap->xaccm[0], (u32 __user *)argp)) 425 break; 426 err = 0; 427 break; 428 429 case PPPIOCGRASYNCMAP: 430 if (put_user(ap->raccm, (u32 __user *)argp)) 431 break; 432 err = 0; 433 break; 434 case PPPIOCSRASYNCMAP: 435 if (get_user(ap->raccm, (u32 __user *)argp)) 436 break; 437 err = 0; 438 break; 439 440 case PPPIOCGXASYNCMAP: 441 if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm))) 442 break; 443 err = 0; 444 break; 445 case PPPIOCSXASYNCMAP: 446 if (copy_from_user(accm, argp, sizeof(accm))) 447 break; 448 accm[2] &= ~0x40000000U; /* can't escape 0x5e */ 449 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */ 450 memcpy(ap->xaccm, accm, sizeof(ap->xaccm)); 451 err = 0; 452 break; 453 454 case PPPIOCGMRU: 455 if (put_user(ap->mru, p)) 456 break; 457 err = 0; 458 break; 459 case PPPIOCSMRU: 460 if (get_user(val, p)) 461 break; 462 if (val > U16_MAX) { 463 err = -EINVAL; 464 break; 465 } 466 if (val < PPP_MRU) 467 val = PPP_MRU; 468 ap->mru = val; 469 err = 0; 470 break; 471 472 default: 473 err = -ENOTTY; 474 } 475 476 return err; 477 } 478 479 /* 480 * This is called at softirq level to deliver received packets 481 * to the ppp_generic code, and to tell the ppp_generic code 482 * if we can accept more output now. 483 */ 484 static void ppp_async_process(struct tasklet_struct *t) 485 { 486 struct asyncppp *ap = from_tasklet(ap, t, tsk); 487 struct sk_buff *skb; 488 489 /* process received packets */ 490 while ((skb = skb_dequeue(&ap->rqueue)) != NULL) { 491 if (skb->cb[0]) 492 ppp_input_error(&ap->chan); 493 ppp_input(&ap->chan, skb); 494 } 495 496 /* try to push more stuff out */ 497 if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_async_push(ap)) 498 ppp_output_wakeup(&ap->chan); 499 } 500 501 /* 502 * Procedures for encapsulation and framing. 503 */ 504 505 /* 506 * Procedure to encode the data for async serial transmission. 507 * Does octet stuffing (escaping), puts the address/control bytes 508 * on if A/C compression is disabled, and does protocol compression. 509 * Assumes ap->tpkt != 0 on entry. 510 * Returns 1 if we finished the current frame, 0 otherwise. 511 */ 512 513 #define PUT_BYTE(ap, buf, c, islcp) do { \ 514 if ((islcp && c < 0x20) || (ap->xaccm[c >> 5] & (1 << (c & 0x1f)))) {\ 515 *buf++ = PPP_ESCAPE; \ 516 *buf++ = c ^ PPP_TRANS; \ 517 } else \ 518 *buf++ = c; \ 519 } while (0) 520 521 static int 522 ppp_async_encode(struct asyncppp *ap) 523 { 524 int fcs, i, count, c, proto; 525 unsigned char *buf, *buflim; 526 unsigned char *data; 527 int islcp; 528 529 buf = ap->obuf; 530 ap->olim = buf; 531 ap->optr = buf; 532 i = ap->tpkt_pos; 533 data = ap->tpkt->data; 534 count = ap->tpkt->len; 535 fcs = ap->tfcs; 536 proto = get_unaligned_be16(data); 537 538 /* 539 * LCP packets with code values between 1 (configure-request) 540 * and 7 (code-reject) must be sent as though no options 541 * had been negotiated. 542 */ 543 islcp = proto == PPP_LCP && count >= 3 && 1 <= data[2] && data[2] <= 7; 544 545 if (i == 0) { 546 if (islcp) 547 async_lcp_peek(ap, data, count, 0); 548 549 /* 550 * Start of a new packet - insert the leading FLAG 551 * character if necessary. 552 */ 553 if (islcp || flag_time == 0 || 554 time_after_eq(jiffies, ap->last_xmit + flag_time)) 555 *buf++ = PPP_FLAG; 556 ap->last_xmit = jiffies; 557 fcs = PPP_INITFCS; 558 559 /* 560 * Put in the address/control bytes if necessary 561 */ 562 if ((ap->flags & SC_COMP_AC) == 0 || islcp) { 563 PUT_BYTE(ap, buf, 0xff, islcp); 564 fcs = PPP_FCS(fcs, 0xff); 565 PUT_BYTE(ap, buf, 0x03, islcp); 566 fcs = PPP_FCS(fcs, 0x03); 567 } 568 } 569 570 /* 571 * Once we put in the last byte, we need to put in the FCS 572 * and closing flag, so make sure there is at least 7 bytes 573 * of free space in the output buffer. 574 */ 575 buflim = ap->obuf + OBUFSIZE - 6; 576 while (i < count && buf < buflim) { 577 c = data[i++]; 578 if (i == 1 && c == 0 && (ap->flags & SC_COMP_PROT)) 579 continue; /* compress protocol field */ 580 fcs = PPP_FCS(fcs, c); 581 PUT_BYTE(ap, buf, c, islcp); 582 } 583 584 if (i < count) { 585 /* 586 * Remember where we are up to in this packet. 587 */ 588 ap->olim = buf; 589 ap->tpkt_pos = i; 590 ap->tfcs = fcs; 591 return 0; 592 } 593 594 /* 595 * We have finished the packet. Add the FCS and flag. 596 */ 597 fcs = ~fcs; 598 c = fcs & 0xff; 599 PUT_BYTE(ap, buf, c, islcp); 600 c = (fcs >> 8) & 0xff; 601 PUT_BYTE(ap, buf, c, islcp); 602 *buf++ = PPP_FLAG; 603 ap->olim = buf; 604 605 consume_skb(ap->tpkt); 606 ap->tpkt = NULL; 607 return 1; 608 } 609 610 /* 611 * Transmit-side routines. 612 */ 613 614 /* 615 * Send a packet to the peer over an async tty line. 616 * Returns 1 iff the packet was accepted. 617 * If the packet was not accepted, we will call ppp_output_wakeup 618 * at some later time. 619 */ 620 static int 621 ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb) 622 { 623 struct asyncppp *ap = chan->private; 624 625 ppp_async_push(ap); 626 627 if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags)) 628 return 0; /* already full */ 629 ap->tpkt = skb; 630 ap->tpkt_pos = 0; 631 632 ppp_async_push(ap); 633 return 1; 634 } 635 636 /* 637 * Push as much data as possible out to the tty. 638 */ 639 static int 640 ppp_async_push(struct asyncppp *ap) 641 { 642 int avail, sent, done = 0; 643 struct tty_struct *tty = ap->tty; 644 int tty_stuffed = 0; 645 646 /* 647 * We can get called recursively here if the tty write 648 * function calls our wakeup function. This can happen 649 * for example on a pty with both the master and slave 650 * set to PPP line discipline. 651 * We use the XMIT_BUSY bit to detect this and get out, 652 * leaving the XMIT_WAKEUP bit set to tell the other 653 * instance that it may now be able to write more now. 654 */ 655 if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags)) 656 return 0; 657 spin_lock_bh(&ap->xmit_lock); 658 for (;;) { 659 if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags)) 660 tty_stuffed = 0; 661 if (!tty_stuffed && ap->optr < ap->olim) { 662 avail = ap->olim - ap->optr; 663 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 664 sent = tty->ops->write(tty, ap->optr, avail); 665 if (sent < 0) 666 goto flush; /* error, e.g. loss of CD */ 667 ap->optr += sent; 668 if (sent < avail) 669 tty_stuffed = 1; 670 continue; 671 } 672 if (ap->optr >= ap->olim && ap->tpkt) { 673 if (ppp_async_encode(ap)) { 674 /* finished processing ap->tpkt */ 675 clear_bit(XMIT_FULL, &ap->xmit_flags); 676 done = 1; 677 } 678 continue; 679 } 680 /* 681 * We haven't made any progress this time around. 682 * Clear XMIT_BUSY to let other callers in, but 683 * after doing so we have to check if anyone set 684 * XMIT_WAKEUP since we last checked it. If they 685 * did, we should try again to set XMIT_BUSY and go 686 * around again in case XMIT_BUSY was still set when 687 * the other caller tried. 688 */ 689 clear_bit(XMIT_BUSY, &ap->xmit_flags); 690 /* any more work to do? if not, exit the loop */ 691 if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags) || 692 (!tty_stuffed && ap->tpkt))) 693 break; 694 /* more work to do, see if we can do it now */ 695 if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags)) 696 break; 697 } 698 spin_unlock_bh(&ap->xmit_lock); 699 return done; 700 701 flush: 702 clear_bit(XMIT_BUSY, &ap->xmit_flags); 703 if (ap->tpkt) { 704 kfree_skb(ap->tpkt); 705 ap->tpkt = NULL; 706 clear_bit(XMIT_FULL, &ap->xmit_flags); 707 done = 1; 708 } 709 ap->optr = ap->olim; 710 spin_unlock_bh(&ap->xmit_lock); 711 return done; 712 } 713 714 /* 715 * Flush output from our internal buffers. 716 * Called for the TCFLSH ioctl. Can be entered in parallel 717 * but this is covered by the xmit_lock. 718 */ 719 static void 720 ppp_async_flush_output(struct asyncppp *ap) 721 { 722 int done = 0; 723 724 spin_lock_bh(&ap->xmit_lock); 725 ap->optr = ap->olim; 726 if (ap->tpkt != NULL) { 727 kfree_skb(ap->tpkt); 728 ap->tpkt = NULL; 729 clear_bit(XMIT_FULL, &ap->xmit_flags); 730 done = 1; 731 } 732 spin_unlock_bh(&ap->xmit_lock); 733 if (done) 734 ppp_output_wakeup(&ap->chan); 735 } 736 737 /* 738 * Receive-side routines. 739 */ 740 741 /* see how many ordinary chars there are at the start of buf */ 742 static inline int 743 scan_ordinary(struct asyncppp *ap, const unsigned char *buf, int count) 744 { 745 int i, c; 746 747 for (i = 0; i < count; ++i) { 748 c = buf[i]; 749 if (c == PPP_ESCAPE || c == PPP_FLAG || 750 (c < 0x20 && (ap->raccm & (1 << c)) != 0)) 751 break; 752 } 753 return i; 754 } 755 756 /* called when a flag is seen - do end-of-packet processing */ 757 static void 758 process_input_packet(struct asyncppp *ap) 759 { 760 struct sk_buff *skb; 761 unsigned char *p; 762 unsigned int len, fcs; 763 764 skb = ap->rpkt; 765 if (ap->state & (SC_TOSS | SC_ESCAPE)) 766 goto err; 767 768 if (skb == NULL) 769 return; /* 0-length packet */ 770 771 /* check the FCS */ 772 p = skb->data; 773 len = skb->len; 774 if (len < 3) 775 goto err; /* too short */ 776 fcs = PPP_INITFCS; 777 for (; len > 0; --len) 778 fcs = PPP_FCS(fcs, *p++); 779 if (fcs != PPP_GOODFCS) 780 goto err; /* bad FCS */ 781 skb_trim(skb, skb->len - 2); 782 783 /* check for address/control and protocol compression */ 784 p = skb->data; 785 if (p[0] == PPP_ALLSTATIONS) { 786 /* chop off address/control */ 787 if (p[1] != PPP_UI || skb->len < 3) 788 goto err; 789 p = skb_pull(skb, 2); 790 } 791 792 /* If protocol field is not compressed, it can be LCP packet */ 793 if (!(p[0] & 0x01)) { 794 unsigned int proto; 795 796 if (skb->len < 2) 797 goto err; 798 proto = (p[0] << 8) + p[1]; 799 if (proto == PPP_LCP) 800 async_lcp_peek(ap, p, skb->len, 1); 801 } 802 803 /* queue the frame to be processed */ 804 skb->cb[0] = ap->state; 805 skb_queue_tail(&ap->rqueue, skb); 806 ap->rpkt = NULL; 807 ap->state = 0; 808 return; 809 810 err: 811 /* frame had an error, remember that, reset SC_TOSS & SC_ESCAPE */ 812 ap->state = SC_PREV_ERROR; 813 if (skb) { 814 /* make skb appear as freshly allocated */ 815 skb_trim(skb, 0); 816 skb_reserve(skb, - skb_headroom(skb)); 817 } 818 } 819 820 /* Called when the tty driver has data for us. Runs parallel with the 821 other ldisc functions but will not be re-entered */ 822 823 static void 824 ppp_async_input(struct asyncppp *ap, const u8 *buf, const u8 *flags, int count) 825 { 826 struct sk_buff *skb; 827 int c, i, j, n, s, f; 828 unsigned char *sp; 829 830 /* update bits used for 8-bit cleanness detection */ 831 if (~ap->rbits & SC_RCV_BITS) { 832 s = 0; 833 for (i = 0; i < count; ++i) { 834 c = buf[i]; 835 if (flags && flags[i] != 0) 836 continue; 837 s |= (c & 0x80)? SC_RCV_B7_1: SC_RCV_B7_0; 838 c = ((c >> 4) ^ c) & 0xf; 839 s |= (0x6996 & (1 << c))? SC_RCV_ODDP: SC_RCV_EVNP; 840 } 841 ap->rbits |= s; 842 } 843 844 while (count > 0) { 845 /* scan through and see how many chars we can do in bulk */ 846 if ((ap->state & SC_ESCAPE) && buf[0] == PPP_ESCAPE) 847 n = 1; 848 else 849 n = scan_ordinary(ap, buf, count); 850 851 f = 0; 852 if (flags && (ap->state & SC_TOSS) == 0) { 853 /* check the flags to see if any char had an error */ 854 for (j = 0; j < n; ++j) 855 if ((f = flags[j]) != 0) 856 break; 857 } 858 if (f != 0) { 859 /* start tossing */ 860 ap->state |= SC_TOSS; 861 862 } else if (n > 0 && (ap->state & SC_TOSS) == 0) { 863 /* stuff the chars in the skb */ 864 skb = ap->rpkt; 865 if (!skb) { 866 skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2); 867 if (!skb) 868 goto nomem; 869 ap->rpkt = skb; 870 } 871 if (skb->len == 0) { 872 /* Try to get the payload 4-byte aligned. 873 * This should match the 874 * PPP_ALLSTATIONS/PPP_UI/compressed tests in 875 * process_input_packet, but we do not have 876 * enough chars here to test buf[1] and buf[2]. 877 */ 878 if (buf[0] != PPP_ALLSTATIONS) 879 skb_reserve(skb, 2 + (buf[0] & 1)); 880 } 881 if (n > skb_tailroom(skb)) { 882 /* packet overflowed MRU */ 883 ap->state |= SC_TOSS; 884 } else { 885 sp = skb_put_data(skb, buf, n); 886 if (ap->state & SC_ESCAPE) { 887 sp[0] ^= PPP_TRANS; 888 ap->state &= ~SC_ESCAPE; 889 } 890 } 891 } 892 893 if (n >= count) 894 break; 895 896 c = buf[n]; 897 if (flags != NULL && flags[n] != 0) { 898 ap->state |= SC_TOSS; 899 } else if (c == PPP_FLAG) { 900 process_input_packet(ap); 901 } else if (c == PPP_ESCAPE) { 902 ap->state |= SC_ESCAPE; 903 } else if (I_IXON(ap->tty)) { 904 if (c == START_CHAR(ap->tty)) 905 start_tty(ap->tty); 906 else if (c == STOP_CHAR(ap->tty)) 907 stop_tty(ap->tty); 908 } 909 /* otherwise it's a char in the recv ACCM */ 910 ++n; 911 912 buf += n; 913 if (flags) 914 flags += n; 915 count -= n; 916 } 917 return; 918 919 nomem: 920 printk(KERN_ERR "PPPasync: no memory (input pkt)\n"); 921 ap->state |= SC_TOSS; 922 } 923 924 /* 925 * We look at LCP frames going past so that we can notice 926 * and react to the LCP configure-ack from the peer. 927 * In the situation where the peer has been sent a configure-ack 928 * already, LCP is up once it has sent its configure-ack 929 * so the immediately following packet can be sent with the 930 * configured LCP options. This allows us to process the following 931 * packet correctly without pppd needing to respond quickly. 932 * 933 * We only respond to the received configure-ack if we have just 934 * sent a configure-request, and the configure-ack contains the 935 * same data (this is checked using a 16-bit crc of the data). 936 */ 937 #define CONFREQ 1 /* LCP code field values */ 938 #define CONFACK 2 939 #define LCP_MRU 1 /* LCP option numbers */ 940 #define LCP_ASYNCMAP 2 941 942 static void async_lcp_peek(struct asyncppp *ap, unsigned char *data, 943 int len, int inbound) 944 { 945 int dlen, fcs, i, code; 946 u32 val; 947 948 data += 2; /* skip protocol bytes */ 949 len -= 2; 950 if (len < 4) /* 4 = code, ID, length */ 951 return; 952 code = data[0]; 953 if (code != CONFACK && code != CONFREQ) 954 return; 955 dlen = get_unaligned_be16(data + 2); 956 if (len < dlen) 957 return; /* packet got truncated or length is bogus */ 958 959 if (code == (inbound? CONFACK: CONFREQ)) { 960 /* 961 * sent confreq or received confack: 962 * calculate the crc of the data from the ID field on. 963 */ 964 fcs = PPP_INITFCS; 965 for (i = 1; i < dlen; ++i) 966 fcs = PPP_FCS(fcs, data[i]); 967 968 if (!inbound) { 969 /* outbound confreq - remember the crc for later */ 970 ap->lcp_fcs = fcs; 971 return; 972 } 973 974 /* received confack, check the crc */ 975 fcs ^= ap->lcp_fcs; 976 ap->lcp_fcs = -1; 977 if (fcs != 0) 978 return; 979 } else if (inbound) 980 return; /* not interested in received confreq */ 981 982 /* process the options in the confack */ 983 data += 4; 984 dlen -= 4; 985 /* data[0] is code, data[1] is length */ 986 while (dlen >= 2 && dlen >= data[1] && data[1] >= 2) { 987 switch (data[0]) { 988 case LCP_MRU: 989 val = get_unaligned_be16(data + 2); 990 if (inbound) 991 ap->mru = val; 992 else 993 ap->chan.mtu = val; 994 break; 995 case LCP_ASYNCMAP: 996 val = get_unaligned_be32(data + 2); 997 if (inbound) 998 ap->raccm = val; 999 else 1000 ap->xaccm[0] = val; 1001 break; 1002 } 1003 dlen -= data[1]; 1004 data += data[1]; 1005 } 1006 } 1007 1008 static void __exit ppp_async_cleanup(void) 1009 { 1010 tty_unregister_ldisc(&ppp_ldisc); 1011 } 1012 1013 module_init(ppp_async_init); 1014 module_exit(ppp_async_cleanup); 1015