Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * PPP async serial channel driver for Linux.
0004  *
0005  * Copyright 1999 Paul Mackerras.
0006  *
0007  * This driver provides the encapsulation and framing for sending
0008  * and receiving PPP frames over async serial lines.  It relies on
0009  * the generic PPP layer to give it frames to send and to process
0010  * received frames.  It implements the PPP line discipline.
0011  *
0012  * Part of the code in this driver was inspired by the old async-only
0013  * PPP driver, written by Michael Callahan and Al Longyear, and
0014  * subsequently hacked by Paul Mackerras.
0015  */
0016 
0017 #include <linux/module.h>
0018 #include <linux/kernel.h>
0019 #include <linux/skbuff.h>
0020 #include <linux/tty.h>
0021 #include <linux/netdevice.h>
0022 #include <linux/poll.h>
0023 #include <linux/crc-ccitt.h>
0024 #include <linux/ppp_defs.h>
0025 #include <linux/ppp-ioctl.h>
0026 #include <linux/ppp_channel.h>
0027 #include <linux/spinlock.h>
0028 #include <linux/init.h>
0029 #include <linux/interrupt.h>
0030 #include <linux/jiffies.h>
0031 #include <linux/slab.h>
0032 #include <asm/unaligned.h>
0033 #include <linux/uaccess.h>
0034 #include <asm/string.h>
0035 
0036 #define PPP_VERSION "2.4.2"
0037 
0038 #define OBUFSIZE    4096
0039 
0040 /* Structure for storing local state. */
0041 struct asyncppp {
0042     struct tty_struct *tty;
0043     unsigned int    flags;
0044     unsigned int    state;
0045     unsigned int    rbits;
0046     int     mru;
0047     spinlock_t  xmit_lock;
0048     spinlock_t  recv_lock;
0049     unsigned long   xmit_flags;
0050     u32     xaccm[8];
0051     u32     raccm;
0052     unsigned int    bytes_sent;
0053     unsigned int    bytes_rcvd;
0054 
0055     struct sk_buff  *tpkt;
0056     int     tpkt_pos;
0057     u16     tfcs;
0058     unsigned char   *optr;
0059     unsigned char   *olim;
0060     unsigned long   last_xmit;
0061 
0062     struct sk_buff  *rpkt;
0063     int     lcp_fcs;
0064     struct sk_buff_head rqueue;
0065 
0066     struct tasklet_struct tsk;
0067 
0068     refcount_t  refcnt;
0069     struct completion dead;
0070     struct ppp_channel chan;    /* interface to generic ppp layer */
0071     unsigned char   obuf[OBUFSIZE];
0072 };
0073 
0074 /* Bit numbers in xmit_flags */
0075 #define XMIT_WAKEUP 0
0076 #define XMIT_FULL   1
0077 #define XMIT_BUSY   2
0078 
0079 /* State bits */
0080 #define SC_TOSS     1
0081 #define SC_ESCAPE   2
0082 #define SC_PREV_ERROR   4
0083 
0084 /* Bits in rbits */
0085 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
0086 
0087 static int flag_time = HZ;
0088 module_param(flag_time, int, 0);
0089 MODULE_PARM_DESC(flag_time, "ppp_async: interval between flagged packets (in clock ticks)");
0090 MODULE_LICENSE("GPL");
0091 MODULE_ALIAS_LDISC(N_PPP);
0092 
0093 /*
0094  * Prototypes.
0095  */
0096 static int ppp_async_encode(struct asyncppp *ap);
0097 static int ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb);
0098 static int ppp_async_push(struct asyncppp *ap);
0099 static void ppp_async_flush_output(struct asyncppp *ap);
0100 static void ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
0101                 const char *flags, int count);
0102 static int ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd,
0103                unsigned long arg);
0104 static void ppp_async_process(struct tasklet_struct *t);
0105 
0106 static void async_lcp_peek(struct asyncppp *ap, unsigned char *data,
0107                int len, int inbound);
0108 
0109 static const struct ppp_channel_ops async_ops = {
0110     .start_xmit = ppp_async_send,
0111     .ioctl      = ppp_async_ioctl,
0112 };
0113 
0114 /*
0115  * Routines implementing the PPP line discipline.
0116  */
0117 
0118 /*
0119  * We have a potential race on dereferencing tty->disc_data,
0120  * because the tty layer provides no locking at all - thus one
0121  * cpu could be running ppp_asynctty_receive while another
0122  * calls ppp_asynctty_close, which zeroes tty->disc_data and
0123  * frees the memory that ppp_asynctty_receive is using.  The best
0124  * way to fix this is to use a rwlock in the tty struct, but for now
0125  * we use a single global rwlock for all ttys in ppp line discipline.
0126  *
0127  * FIXME: this is no longer true. The _close path for the ldisc is
0128  * now guaranteed to be sane.
0129  */
0130 static DEFINE_RWLOCK(disc_data_lock);
0131 
0132 static struct asyncppp *ap_get(struct tty_struct *tty)
0133 {
0134     struct asyncppp *ap;
0135 
0136     read_lock(&disc_data_lock);
0137     ap = tty->disc_data;
0138     if (ap != NULL)
0139         refcount_inc(&ap->refcnt);
0140     read_unlock(&disc_data_lock);
0141     return ap;
0142 }
0143 
0144 static void ap_put(struct asyncppp *ap)
0145 {
0146     if (refcount_dec_and_test(&ap->refcnt))
0147         complete(&ap->dead);
0148 }
0149 
0150 /*
0151  * Called when a tty is put into PPP line discipline. Called in process
0152  * context.
0153  */
0154 static int
0155 ppp_asynctty_open(struct tty_struct *tty)
0156 {
0157     struct asyncppp *ap;
0158     int err;
0159     int speed;
0160 
0161     if (tty->ops->write == NULL)
0162         return -EOPNOTSUPP;
0163 
0164     err = -ENOMEM;
0165     ap = kzalloc(sizeof(*ap), GFP_KERNEL);
0166     if (!ap)
0167         goto out;
0168 
0169     /* initialize the asyncppp structure */
0170     ap->tty = tty;
0171     ap->mru = PPP_MRU;
0172     spin_lock_init(&ap->xmit_lock);
0173     spin_lock_init(&ap->recv_lock);
0174     ap->xaccm[0] = ~0U;
0175     ap->xaccm[3] = 0x60000000U;
0176     ap->raccm = ~0U;
0177     ap->optr = ap->obuf;
0178     ap->olim = ap->obuf;
0179     ap->lcp_fcs = -1;
0180 
0181     skb_queue_head_init(&ap->rqueue);
0182     tasklet_setup(&ap->tsk, ppp_async_process);
0183 
0184     refcount_set(&ap->refcnt, 1);
0185     init_completion(&ap->dead);
0186 
0187     ap->chan.private = ap;
0188     ap->chan.ops = &async_ops;
0189     ap->chan.mtu = PPP_MRU;
0190     speed = tty_get_baud_rate(tty);
0191     ap->chan.speed = speed;
0192     err = ppp_register_channel(&ap->chan);
0193     if (err)
0194         goto out_free;
0195 
0196     tty->disc_data = ap;
0197     tty->receive_room = 65536;
0198     return 0;
0199 
0200  out_free:
0201     kfree(ap);
0202  out:
0203     return err;
0204 }
0205 
0206 /*
0207  * Called when the tty is put into another line discipline
0208  * or it hangs up.  We have to wait for any cpu currently
0209  * executing in any of the other ppp_asynctty_* routines to
0210  * finish before we can call ppp_unregister_channel and free
0211  * the asyncppp struct.  This routine must be called from
0212  * process context, not interrupt or softirq context.
0213  */
0214 static void
0215 ppp_asynctty_close(struct tty_struct *tty)
0216 {
0217     struct asyncppp *ap;
0218 
0219     write_lock_irq(&disc_data_lock);
0220     ap = tty->disc_data;
0221     tty->disc_data = NULL;
0222     write_unlock_irq(&disc_data_lock);
0223     if (!ap)
0224         return;
0225 
0226     /*
0227      * We have now ensured that nobody can start using ap from now
0228      * on, but we have to wait for all existing users to finish.
0229      * Note that ppp_unregister_channel ensures that no calls to
0230      * our channel ops (i.e. ppp_async_send/ioctl) are in progress
0231      * by the time it returns.
0232      */
0233     if (!refcount_dec_and_test(&ap->refcnt))
0234         wait_for_completion(&ap->dead);
0235     tasklet_kill(&ap->tsk);
0236 
0237     ppp_unregister_channel(&ap->chan);
0238     kfree_skb(ap->rpkt);
0239     skb_queue_purge(&ap->rqueue);
0240     kfree_skb(ap->tpkt);
0241     kfree(ap);
0242 }
0243 
0244 /*
0245  * Called on tty hangup in process context.
0246  *
0247  * Wait for I/O to driver to complete and unregister PPP channel.
0248  * This is already done by the close routine, so just call that.
0249  */
0250 static void ppp_asynctty_hangup(struct tty_struct *tty)
0251 {
0252     ppp_asynctty_close(tty);
0253 }
0254 
0255 /*
0256  * Read does nothing - no data is ever available this way.
0257  * Pppd reads and writes packets via /dev/ppp instead.
0258  */
0259 static ssize_t
0260 ppp_asynctty_read(struct tty_struct *tty, struct file *file,
0261           unsigned char *buf, size_t count,
0262           void **cookie, unsigned long offset)
0263 {
0264     return -EAGAIN;
0265 }
0266 
0267 /*
0268  * Write on the tty does nothing, the packets all come in
0269  * from the ppp generic stuff.
0270  */
0271 static ssize_t
0272 ppp_asynctty_write(struct tty_struct *tty, struct file *file,
0273            const unsigned char *buf, size_t count)
0274 {
0275     return -EAGAIN;
0276 }
0277 
0278 /*
0279  * Called in process context only. May be re-entered by multiple
0280  * ioctl calling threads.
0281  */
0282 
0283 static int
0284 ppp_asynctty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
0285 {
0286     struct asyncppp *ap = ap_get(tty);
0287     int err, val;
0288     int __user *p = (int __user *)arg;
0289 
0290     if (!ap)
0291         return -ENXIO;
0292     err = -EFAULT;
0293     switch (cmd) {
0294     case PPPIOCGCHAN:
0295         err = -EFAULT;
0296         if (put_user(ppp_channel_index(&ap->chan), p))
0297             break;
0298         err = 0;
0299         break;
0300 
0301     case PPPIOCGUNIT:
0302         err = -EFAULT;
0303         if (put_user(ppp_unit_number(&ap->chan), p))
0304             break;
0305         err = 0;
0306         break;
0307 
0308     case TCFLSH:
0309         /* flush our buffers and the serial port's buffer */
0310         if (arg == TCIOFLUSH || arg == TCOFLUSH)
0311             ppp_async_flush_output(ap);
0312         err = n_tty_ioctl_helper(tty, cmd, arg);
0313         break;
0314 
0315     case FIONREAD:
0316         val = 0;
0317         if (put_user(val, p))
0318             break;
0319         err = 0;
0320         break;
0321 
0322     default:
0323         /* Try the various mode ioctls */
0324         err = tty_mode_ioctl(tty, cmd, arg);
0325     }
0326 
0327     ap_put(ap);
0328     return err;
0329 }
0330 
0331 /* No kernel lock - fine */
0332 static __poll_t
0333 ppp_asynctty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
0334 {
0335     return 0;
0336 }
0337 
0338 /* May sleep, don't call from interrupt level or with interrupts disabled */
0339 static void
0340 ppp_asynctty_receive(struct tty_struct *tty, const unsigned char *buf,
0341           const char *cflags, int count)
0342 {
0343     struct asyncppp *ap = ap_get(tty);
0344     unsigned long flags;
0345 
0346     if (!ap)
0347         return;
0348     spin_lock_irqsave(&ap->recv_lock, flags);
0349     ppp_async_input(ap, buf, cflags, count);
0350     spin_unlock_irqrestore(&ap->recv_lock, flags);
0351     if (!skb_queue_empty(&ap->rqueue))
0352         tasklet_schedule(&ap->tsk);
0353     ap_put(ap);
0354     tty_unthrottle(tty);
0355 }
0356 
0357 static void
0358 ppp_asynctty_wakeup(struct tty_struct *tty)
0359 {
0360     struct asyncppp *ap = ap_get(tty);
0361 
0362     clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0363     if (!ap)
0364         return;
0365     set_bit(XMIT_WAKEUP, &ap->xmit_flags);
0366     tasklet_schedule(&ap->tsk);
0367     ap_put(ap);
0368 }
0369 
0370 
0371 static struct tty_ldisc_ops ppp_ldisc = {
0372     .owner  = THIS_MODULE,
0373     .num    = N_PPP,
0374     .name   = "ppp",
0375     .open   = ppp_asynctty_open,
0376     .close  = ppp_asynctty_close,
0377     .hangup = ppp_asynctty_hangup,
0378     .read   = ppp_asynctty_read,
0379     .write  = ppp_asynctty_write,
0380     .ioctl  = ppp_asynctty_ioctl,
0381     .poll   = ppp_asynctty_poll,
0382     .receive_buf = ppp_asynctty_receive,
0383     .write_wakeup = ppp_asynctty_wakeup,
0384 };
0385 
0386 static int __init
0387 ppp_async_init(void)
0388 {
0389     int err;
0390 
0391     err = tty_register_ldisc(&ppp_ldisc);
0392     if (err != 0)
0393         printk(KERN_ERR "PPP_async: error %d registering line disc.\n",
0394                err);
0395     return err;
0396 }
0397 
0398 /*
0399  * The following routines provide the PPP channel interface.
0400  */
0401 static int
0402 ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
0403 {
0404     struct asyncppp *ap = chan->private;
0405     void __user *argp = (void __user *)arg;
0406     int __user *p = argp;
0407     int err, val;
0408     u32 accm[8];
0409 
0410     err = -EFAULT;
0411     switch (cmd) {
0412     case PPPIOCGFLAGS:
0413         val = ap->flags | ap->rbits;
0414         if (put_user(val, p))
0415             break;
0416         err = 0;
0417         break;
0418     case PPPIOCSFLAGS:
0419         if (get_user(val, p))
0420             break;
0421         ap->flags = val & ~SC_RCV_BITS;
0422         spin_lock_irq(&ap->recv_lock);
0423         ap->rbits = val & SC_RCV_BITS;
0424         spin_unlock_irq(&ap->recv_lock);
0425         err = 0;
0426         break;
0427 
0428     case PPPIOCGASYNCMAP:
0429         if (put_user(ap->xaccm[0], (u32 __user *)argp))
0430             break;
0431         err = 0;
0432         break;
0433     case PPPIOCSASYNCMAP:
0434         if (get_user(ap->xaccm[0], (u32 __user *)argp))
0435             break;
0436         err = 0;
0437         break;
0438 
0439     case PPPIOCGRASYNCMAP:
0440         if (put_user(ap->raccm, (u32 __user *)argp))
0441             break;
0442         err = 0;
0443         break;
0444     case PPPIOCSRASYNCMAP:
0445         if (get_user(ap->raccm, (u32 __user *)argp))
0446             break;
0447         err = 0;
0448         break;
0449 
0450     case PPPIOCGXASYNCMAP:
0451         if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
0452             break;
0453         err = 0;
0454         break;
0455     case PPPIOCSXASYNCMAP:
0456         if (copy_from_user(accm, argp, sizeof(accm)))
0457             break;
0458         accm[2] &= ~0x40000000U;    /* can't escape 0x5e */
0459         accm[3] |= 0x60000000U;     /* must escape 0x7d, 0x7e */
0460         memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
0461         err = 0;
0462         break;
0463 
0464     case PPPIOCGMRU:
0465         if (put_user(ap->mru, p))
0466             break;
0467         err = 0;
0468         break;
0469     case PPPIOCSMRU:
0470         if (get_user(val, p))
0471             break;
0472         if (val < PPP_MRU)
0473             val = PPP_MRU;
0474         ap->mru = val;
0475         err = 0;
0476         break;
0477 
0478     default:
0479         err = -ENOTTY;
0480     }
0481 
0482     return err;
0483 }
0484 
0485 /*
0486  * This is called at softirq level to deliver received packets
0487  * to the ppp_generic code, and to tell the ppp_generic code
0488  * if we can accept more output now.
0489  */
0490 static void ppp_async_process(struct tasklet_struct *t)
0491 {
0492     struct asyncppp *ap = from_tasklet(ap, t, tsk);
0493     struct sk_buff *skb;
0494 
0495     /* process received packets */
0496     while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
0497         if (skb->cb[0])
0498             ppp_input_error(&ap->chan, 0);
0499         ppp_input(&ap->chan, skb);
0500     }
0501 
0502     /* try to push more stuff out */
0503     if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_async_push(ap))
0504         ppp_output_wakeup(&ap->chan);
0505 }
0506 
0507 /*
0508  * Procedures for encapsulation and framing.
0509  */
0510 
0511 /*
0512  * Procedure to encode the data for async serial transmission.
0513  * Does octet stuffing (escaping), puts the address/control bytes
0514  * on if A/C compression is disabled, and does protocol compression.
0515  * Assumes ap->tpkt != 0 on entry.
0516  * Returns 1 if we finished the current frame, 0 otherwise.
0517  */
0518 
0519 #define PUT_BYTE(ap, buf, c, islcp) do {        \
0520     if ((islcp && c < 0x20) || (ap->xaccm[c >> 5] & (1 << (c & 0x1f)))) {\
0521         *buf++ = PPP_ESCAPE;            \
0522         *buf++ = c ^ PPP_TRANS;         \
0523     } else                      \
0524         *buf++ = c;             \
0525 } while (0)
0526 
0527 static int
0528 ppp_async_encode(struct asyncppp *ap)
0529 {
0530     int fcs, i, count, c, proto;
0531     unsigned char *buf, *buflim;
0532     unsigned char *data;
0533     int islcp;
0534 
0535     buf = ap->obuf;
0536     ap->olim = buf;
0537     ap->optr = buf;
0538     i = ap->tpkt_pos;
0539     data = ap->tpkt->data;
0540     count = ap->tpkt->len;
0541     fcs = ap->tfcs;
0542     proto = get_unaligned_be16(data);
0543 
0544     /*
0545      * LCP packets with code values between 1 (configure-reqest)
0546      * and 7 (code-reject) must be sent as though no options
0547      * had been negotiated.
0548      */
0549     islcp = proto == PPP_LCP && 1 <= data[2] && data[2] <= 7;
0550 
0551     if (i == 0) {
0552         if (islcp)
0553             async_lcp_peek(ap, data, count, 0);
0554 
0555         /*
0556          * Start of a new packet - insert the leading FLAG
0557          * character if necessary.
0558          */
0559         if (islcp || flag_time == 0 ||
0560             time_after_eq(jiffies, ap->last_xmit + flag_time))
0561             *buf++ = PPP_FLAG;
0562         ap->last_xmit = jiffies;
0563         fcs = PPP_INITFCS;
0564 
0565         /*
0566          * Put in the address/control bytes if necessary
0567          */
0568         if ((ap->flags & SC_COMP_AC) == 0 || islcp) {
0569             PUT_BYTE(ap, buf, 0xff, islcp);
0570             fcs = PPP_FCS(fcs, 0xff);
0571             PUT_BYTE(ap, buf, 0x03, islcp);
0572             fcs = PPP_FCS(fcs, 0x03);
0573         }
0574     }
0575 
0576     /*
0577      * Once we put in the last byte, we need to put in the FCS
0578      * and closing flag, so make sure there is at least 7 bytes
0579      * of free space in the output buffer.
0580      */
0581     buflim = ap->obuf + OBUFSIZE - 6;
0582     while (i < count && buf < buflim) {
0583         c = data[i++];
0584         if (i == 1 && c == 0 && (ap->flags & SC_COMP_PROT))
0585             continue;   /* compress protocol field */
0586         fcs = PPP_FCS(fcs, c);
0587         PUT_BYTE(ap, buf, c, islcp);
0588     }
0589 
0590     if (i < count) {
0591         /*
0592          * Remember where we are up to in this packet.
0593          */
0594         ap->olim = buf;
0595         ap->tpkt_pos = i;
0596         ap->tfcs = fcs;
0597         return 0;
0598     }
0599 
0600     /*
0601      * We have finished the packet.  Add the FCS and flag.
0602      */
0603     fcs = ~fcs;
0604     c = fcs & 0xff;
0605     PUT_BYTE(ap, buf, c, islcp);
0606     c = (fcs >> 8) & 0xff;
0607     PUT_BYTE(ap, buf, c, islcp);
0608     *buf++ = PPP_FLAG;
0609     ap->olim = buf;
0610 
0611     consume_skb(ap->tpkt);
0612     ap->tpkt = NULL;
0613     return 1;
0614 }
0615 
0616 /*
0617  * Transmit-side routines.
0618  */
0619 
0620 /*
0621  * Send a packet to the peer over an async tty line.
0622  * Returns 1 iff the packet was accepted.
0623  * If the packet was not accepted, we will call ppp_output_wakeup
0624  * at some later time.
0625  */
0626 static int
0627 ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb)
0628 {
0629     struct asyncppp *ap = chan->private;
0630 
0631     ppp_async_push(ap);
0632 
0633     if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
0634         return 0;   /* already full */
0635     ap->tpkt = skb;
0636     ap->tpkt_pos = 0;
0637 
0638     ppp_async_push(ap);
0639     return 1;
0640 }
0641 
0642 /*
0643  * Push as much data as possible out to the tty.
0644  */
0645 static int
0646 ppp_async_push(struct asyncppp *ap)
0647 {
0648     int avail, sent, done = 0;
0649     struct tty_struct *tty = ap->tty;
0650     int tty_stuffed = 0;
0651 
0652     /*
0653      * We can get called recursively here if the tty write
0654      * function calls our wakeup function.  This can happen
0655      * for example on a pty with both the master and slave
0656      * set to PPP line discipline.
0657      * We use the XMIT_BUSY bit to detect this and get out,
0658      * leaving the XMIT_WAKEUP bit set to tell the other
0659      * instance that it may now be able to write more now.
0660      */
0661     if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags))
0662         return 0;
0663     spin_lock_bh(&ap->xmit_lock);
0664     for (;;) {
0665         if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
0666             tty_stuffed = 0;
0667         if (!tty_stuffed && ap->optr < ap->olim) {
0668             avail = ap->olim - ap->optr;
0669             set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0670             sent = tty->ops->write(tty, ap->optr, avail);
0671             if (sent < 0)
0672                 goto flush; /* error, e.g. loss of CD */
0673             ap->optr += sent;
0674             if (sent < avail)
0675                 tty_stuffed = 1;
0676             continue;
0677         }
0678         if (ap->optr >= ap->olim && ap->tpkt) {
0679             if (ppp_async_encode(ap)) {
0680                 /* finished processing ap->tpkt */
0681                 clear_bit(XMIT_FULL, &ap->xmit_flags);
0682                 done = 1;
0683             }
0684             continue;
0685         }
0686         /*
0687          * We haven't made any progress this time around.
0688          * Clear XMIT_BUSY to let other callers in, but
0689          * after doing so we have to check if anyone set
0690          * XMIT_WAKEUP since we last checked it.  If they
0691          * did, we should try again to set XMIT_BUSY and go
0692          * around again in case XMIT_BUSY was still set when
0693          * the other caller tried.
0694          */
0695         clear_bit(XMIT_BUSY, &ap->xmit_flags);
0696         /* any more work to do? if not, exit the loop */
0697         if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags) ||
0698               (!tty_stuffed && ap->tpkt)))
0699             break;
0700         /* more work to do, see if we can do it now */
0701         if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags))
0702             break;
0703     }
0704     spin_unlock_bh(&ap->xmit_lock);
0705     return done;
0706 
0707 flush:
0708     clear_bit(XMIT_BUSY, &ap->xmit_flags);
0709     if (ap->tpkt) {
0710         kfree_skb(ap->tpkt);
0711         ap->tpkt = NULL;
0712         clear_bit(XMIT_FULL, &ap->xmit_flags);
0713         done = 1;
0714     }
0715     ap->optr = ap->olim;
0716     spin_unlock_bh(&ap->xmit_lock);
0717     return done;
0718 }
0719 
0720 /*
0721  * Flush output from our internal buffers.
0722  * Called for the TCFLSH ioctl. Can be entered in parallel
0723  * but this is covered by the xmit_lock.
0724  */
0725 static void
0726 ppp_async_flush_output(struct asyncppp *ap)
0727 {
0728     int done = 0;
0729 
0730     spin_lock_bh(&ap->xmit_lock);
0731     ap->optr = ap->olim;
0732     if (ap->tpkt != NULL) {
0733         kfree_skb(ap->tpkt);
0734         ap->tpkt = NULL;
0735         clear_bit(XMIT_FULL, &ap->xmit_flags);
0736         done = 1;
0737     }
0738     spin_unlock_bh(&ap->xmit_lock);
0739     if (done)
0740         ppp_output_wakeup(&ap->chan);
0741 }
0742 
0743 /*
0744  * Receive-side routines.
0745  */
0746 
0747 /* see how many ordinary chars there are at the start of buf */
0748 static inline int
0749 scan_ordinary(struct asyncppp *ap, const unsigned char *buf, int count)
0750 {
0751     int i, c;
0752 
0753     for (i = 0; i < count; ++i) {
0754         c = buf[i];
0755         if (c == PPP_ESCAPE || c == PPP_FLAG ||
0756             (c < 0x20 && (ap->raccm & (1 << c)) != 0))
0757             break;
0758     }
0759     return i;
0760 }
0761 
0762 /* called when a flag is seen - do end-of-packet processing */
0763 static void
0764 process_input_packet(struct asyncppp *ap)
0765 {
0766     struct sk_buff *skb;
0767     unsigned char *p;
0768     unsigned int len, fcs;
0769 
0770     skb = ap->rpkt;
0771     if (ap->state & (SC_TOSS | SC_ESCAPE))
0772         goto err;
0773 
0774     if (skb == NULL)
0775         return;     /* 0-length packet */
0776 
0777     /* check the FCS */
0778     p = skb->data;
0779     len = skb->len;
0780     if (len < 3)
0781         goto err;   /* too short */
0782     fcs = PPP_INITFCS;
0783     for (; len > 0; --len)
0784         fcs = PPP_FCS(fcs, *p++);
0785     if (fcs != PPP_GOODFCS)
0786         goto err;   /* bad FCS */
0787     skb_trim(skb, skb->len - 2);
0788 
0789     /* check for address/control and protocol compression */
0790     p = skb->data;
0791     if (p[0] == PPP_ALLSTATIONS) {
0792         /* chop off address/control */
0793         if (p[1] != PPP_UI || skb->len < 3)
0794             goto err;
0795         p = skb_pull(skb, 2);
0796     }
0797 
0798     /* If protocol field is not compressed, it can be LCP packet */
0799     if (!(p[0] & 0x01)) {
0800         unsigned int proto;
0801 
0802         if (skb->len < 2)
0803             goto err;
0804         proto = (p[0] << 8) + p[1];
0805         if (proto == PPP_LCP)
0806             async_lcp_peek(ap, p, skb->len, 1);
0807     }
0808 
0809     /* queue the frame to be processed */
0810     skb->cb[0] = ap->state;
0811     skb_queue_tail(&ap->rqueue, skb);
0812     ap->rpkt = NULL;
0813     ap->state = 0;
0814     return;
0815 
0816  err:
0817     /* frame had an error, remember that, reset SC_TOSS & SC_ESCAPE */
0818     ap->state = SC_PREV_ERROR;
0819     if (skb) {
0820         /* make skb appear as freshly allocated */
0821         skb_trim(skb, 0);
0822         skb_reserve(skb, - skb_headroom(skb));
0823     }
0824 }
0825 
0826 /* Called when the tty driver has data for us. Runs parallel with the
0827    other ldisc functions but will not be re-entered */
0828 
0829 static void
0830 ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
0831         const char *flags, int count)
0832 {
0833     struct sk_buff *skb;
0834     int c, i, j, n, s, f;
0835     unsigned char *sp;
0836 
0837     /* update bits used for 8-bit cleanness detection */
0838     if (~ap->rbits & SC_RCV_BITS) {
0839         s = 0;
0840         for (i = 0; i < count; ++i) {
0841             c = buf[i];
0842             if (flags && flags[i] != 0)
0843                 continue;
0844             s |= (c & 0x80)? SC_RCV_B7_1: SC_RCV_B7_0;
0845             c = ((c >> 4) ^ c) & 0xf;
0846             s |= (0x6996 & (1 << c))? SC_RCV_ODDP: SC_RCV_EVNP;
0847         }
0848         ap->rbits |= s;
0849     }
0850 
0851     while (count > 0) {
0852         /* scan through and see how many chars we can do in bulk */
0853         if ((ap->state & SC_ESCAPE) && buf[0] == PPP_ESCAPE)
0854             n = 1;
0855         else
0856             n = scan_ordinary(ap, buf, count);
0857 
0858         f = 0;
0859         if (flags && (ap->state & SC_TOSS) == 0) {
0860             /* check the flags to see if any char had an error */
0861             for (j = 0; j < n; ++j)
0862                 if ((f = flags[j]) != 0)
0863                     break;
0864         }
0865         if (f != 0) {
0866             /* start tossing */
0867             ap->state |= SC_TOSS;
0868 
0869         } else if (n > 0 && (ap->state & SC_TOSS) == 0) {
0870             /* stuff the chars in the skb */
0871             skb = ap->rpkt;
0872             if (!skb) {
0873                 skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
0874                 if (!skb)
0875                     goto nomem;
0876                 ap->rpkt = skb;
0877             }
0878             if (skb->len == 0) {
0879                 /* Try to get the payload 4-byte aligned.
0880                  * This should match the
0881                  * PPP_ALLSTATIONS/PPP_UI/compressed tests in
0882                  * process_input_packet, but we do not have
0883                  * enough chars here to test buf[1] and buf[2].
0884                  */
0885                 if (buf[0] != PPP_ALLSTATIONS)
0886                     skb_reserve(skb, 2 + (buf[0] & 1));
0887             }
0888             if (n > skb_tailroom(skb)) {
0889                 /* packet overflowed MRU */
0890                 ap->state |= SC_TOSS;
0891             } else {
0892                 sp = skb_put_data(skb, buf, n);
0893                 if (ap->state & SC_ESCAPE) {
0894                     sp[0] ^= PPP_TRANS;
0895                     ap->state &= ~SC_ESCAPE;
0896                 }
0897             }
0898         }
0899 
0900         if (n >= count)
0901             break;
0902 
0903         c = buf[n];
0904         if (flags != NULL && flags[n] != 0) {
0905             ap->state |= SC_TOSS;
0906         } else if (c == PPP_FLAG) {
0907             process_input_packet(ap);
0908         } else if (c == PPP_ESCAPE) {
0909             ap->state |= SC_ESCAPE;
0910         } else if (I_IXON(ap->tty)) {
0911             if (c == START_CHAR(ap->tty))
0912                 start_tty(ap->tty);
0913             else if (c == STOP_CHAR(ap->tty))
0914                 stop_tty(ap->tty);
0915         }
0916         /* otherwise it's a char in the recv ACCM */
0917         ++n;
0918 
0919         buf += n;
0920         if (flags)
0921             flags += n;
0922         count -= n;
0923     }
0924     return;
0925 
0926  nomem:
0927     printk(KERN_ERR "PPPasync: no memory (input pkt)\n");
0928     ap->state |= SC_TOSS;
0929 }
0930 
0931 /*
0932  * We look at LCP frames going past so that we can notice
0933  * and react to the LCP configure-ack from the peer.
0934  * In the situation where the peer has been sent a configure-ack
0935  * already, LCP is up once it has sent its configure-ack
0936  * so the immediately following packet can be sent with the
0937  * configured LCP options.  This allows us to process the following
0938  * packet correctly without pppd needing to respond quickly.
0939  *
0940  * We only respond to the received configure-ack if we have just
0941  * sent a configure-request, and the configure-ack contains the
0942  * same data (this is checked using a 16-bit crc of the data).
0943  */
0944 #define CONFREQ     1   /* LCP code field values */
0945 #define CONFACK     2
0946 #define LCP_MRU     1   /* LCP option numbers */
0947 #define LCP_ASYNCMAP    2
0948 
0949 static void async_lcp_peek(struct asyncppp *ap, unsigned char *data,
0950                int len, int inbound)
0951 {
0952     int dlen, fcs, i, code;
0953     u32 val;
0954 
0955     data += 2;      /* skip protocol bytes */
0956     len -= 2;
0957     if (len < 4)        /* 4 = code, ID, length */
0958         return;
0959     code = data[0];
0960     if (code != CONFACK && code != CONFREQ)
0961         return;
0962     dlen = get_unaligned_be16(data + 2);
0963     if (len < dlen)
0964         return;     /* packet got truncated or length is bogus */
0965 
0966     if (code == (inbound? CONFACK: CONFREQ)) {
0967         /*
0968          * sent confreq or received confack:
0969          * calculate the crc of the data from the ID field on.
0970          */
0971         fcs = PPP_INITFCS;
0972         for (i = 1; i < dlen; ++i)
0973             fcs = PPP_FCS(fcs, data[i]);
0974 
0975         if (!inbound) {
0976             /* outbound confreq - remember the crc for later */
0977             ap->lcp_fcs = fcs;
0978             return;
0979         }
0980 
0981         /* received confack, check the crc */
0982         fcs ^= ap->lcp_fcs;
0983         ap->lcp_fcs = -1;
0984         if (fcs != 0)
0985             return;
0986     } else if (inbound)
0987         return; /* not interested in received confreq */
0988 
0989     /* process the options in the confack */
0990     data += 4;
0991     dlen -= 4;
0992     /* data[0] is code, data[1] is length */
0993     while (dlen >= 2 && dlen >= data[1] && data[1] >= 2) {
0994         switch (data[0]) {
0995         case LCP_MRU:
0996             val = get_unaligned_be16(data + 2);
0997             if (inbound)
0998                 ap->mru = val;
0999             else
1000                 ap->chan.mtu = val;
1001             break;
1002         case LCP_ASYNCMAP:
1003             val = get_unaligned_be32(data + 2);
1004             if (inbound)
1005                 ap->raccm = val;
1006             else
1007                 ap->xaccm[0] = val;
1008             break;
1009         }
1010         dlen -= data[1];
1011         data += data[1];
1012     }
1013 }
1014 
1015 static void __exit ppp_async_cleanup(void)
1016 {
1017     tty_unregister_ldisc(&ppp_ldisc);
1018 }
1019 
1020 module_init(ppp_async_init);
1021 module_exit(ppp_async_cleanup);