ppp_synctty.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PPP synchronous tty channel driver for Linux.
  4. *
  5. * This is a ppp channel driver that can be used with tty device drivers
  6. * that are frame oriented, such as synchronous HDLC devices.
  7. *
  8. * Complete PPP frames without encoding/decoding are exchanged between
  9. * the channel driver and the device driver.
  10. *
  11. * The async map IOCTL codes are implemented to keep the user mode
  12. * applications happy if they call them. Synchronous PPP does not use
  13. * the async maps.
  14. *
  15. * Copyright 1999 Paul Mackerras.
  16. *
  17. * Also touched by the grubby hands of Paul Fulghum [email protected]
  18. *
  19. * This driver provides the encapsulation and framing for sending
  20. * and receiving PPP frames over sync serial lines. It relies on
  21. * the generic PPP layer to give it frames to send and to process
  22. * received frames. It implements the PPP line discipline.
  23. *
  24. * Part of the code in this driver was inspired by the old async-only
  25. * PPP driver, written by Michael Callahan and Al Longyear, and
  26. * subsequently hacked by Paul Mackerras.
  27. *
  28. * ==FILEVERSION 20040616==
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/tty.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/poll.h>
  36. #include <linux/ppp_defs.h>
  37. #include <linux/ppp-ioctl.h>
  38. #include <linux/ppp_channel.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/completion.h>
  41. #include <linux/init.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/slab.h>
  44. #include <linux/refcount.h>
  45. #include <asm/unaligned.h>
  46. #include <linux/uaccess.h>
  47. #define PPP_VERSION "2.4.2"
  48. /* Structure for storing local state. */
  49. struct syncppp {
  50. struct tty_struct *tty;
  51. unsigned int flags;
  52. unsigned int rbits;
  53. int mru;
  54. spinlock_t xmit_lock;
  55. spinlock_t recv_lock;
  56. unsigned long xmit_flags;
  57. u32 xaccm[8];
  58. u32 raccm;
  59. unsigned int bytes_sent;
  60. unsigned int bytes_rcvd;
  61. struct sk_buff *tpkt;
  62. unsigned long last_xmit;
  63. struct sk_buff_head rqueue;
  64. struct tasklet_struct tsk;
  65. refcount_t refcnt;
  66. struct completion dead_cmp;
  67. struct ppp_channel chan; /* interface to generic ppp layer */
  68. };
  69. /* Bit numbers in xmit_flags */
  70. #define XMIT_WAKEUP 0
  71. #define XMIT_FULL 1
  72. /* Bits in rbits */
  73. #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
  74. #define PPPSYNC_MAX_RQLEN 32 /* arbitrary */
  75. /*
  76. * Prototypes.
  77. */
  78. static struct sk_buff* ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *);
  79. static int ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb);
  80. static int ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd,
  81. unsigned long arg);
  82. static void ppp_sync_process(struct tasklet_struct *t);
  83. static int ppp_sync_push(struct syncppp *ap);
  84. static void ppp_sync_flush_output(struct syncppp *ap);
  85. static void ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
  86. const char *flags, int count);
  87. static const struct ppp_channel_ops sync_ops = {
  88. .start_xmit = ppp_sync_send,
  89. .ioctl = ppp_sync_ioctl,
  90. };
  91. /*
  92. * Utility procedure to print a buffer in hex/ascii
  93. */
  94. static void
  95. ppp_print_buffer (const char *name, const __u8 *buf, int count)
  96. {
  97. if (name != NULL)
  98. printk(KERN_DEBUG "ppp_synctty: %s, count = %d\n", name, count);
  99. print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, count);
  100. }
  101. /*
  102. * Routines implementing the synchronous PPP line discipline.
  103. */
  104. /*
  105. * We have a potential race on dereferencing tty->disc_data,
  106. * because the tty layer provides no locking at all - thus one
  107. * cpu could be running ppp_synctty_receive while another
  108. * calls ppp_synctty_close, which zeroes tty->disc_data and
  109. * frees the memory that ppp_synctty_receive is using. The best
  110. * way to fix this is to use a rwlock in the tty struct, but for now
  111. * we use a single global rwlock for all ttys in ppp line discipline.
  112. *
  113. * FIXME: Fixed in tty_io nowadays.
  114. */
  115. static DEFINE_RWLOCK(disc_data_lock);
  116. static struct syncppp *sp_get(struct tty_struct *tty)
  117. {
  118. struct syncppp *ap;
  119. read_lock(&disc_data_lock);
  120. ap = tty->disc_data;
  121. if (ap != NULL)
  122. refcount_inc(&ap->refcnt);
  123. read_unlock(&disc_data_lock);
  124. return ap;
  125. }
  126. static void sp_put(struct syncppp *ap)
  127. {
  128. if (refcount_dec_and_test(&ap->refcnt))
  129. complete(&ap->dead_cmp);
  130. }
  131. /*
  132. * Called when a tty is put into sync-PPP line discipline.
  133. */
  134. static int
  135. ppp_sync_open(struct tty_struct *tty)
  136. {
  137. struct syncppp *ap;
  138. int err;
  139. int speed;
  140. if (tty->ops->write == NULL)
  141. return -EOPNOTSUPP;
  142. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  143. err = -ENOMEM;
  144. if (!ap)
  145. goto out;
  146. /* initialize the syncppp structure */
  147. ap->tty = tty;
  148. ap->mru = PPP_MRU;
  149. spin_lock_init(&ap->xmit_lock);
  150. spin_lock_init(&ap->recv_lock);
  151. ap->xaccm[0] = ~0U;
  152. ap->xaccm[3] = 0x60000000U;
  153. ap->raccm = ~0U;
  154. skb_queue_head_init(&ap->rqueue);
  155. tasklet_setup(&ap->tsk, ppp_sync_process);
  156. refcount_set(&ap->refcnt, 1);
  157. init_completion(&ap->dead_cmp);
  158. ap->chan.private = ap;
  159. ap->chan.ops = &sync_ops;
  160. ap->chan.mtu = PPP_MRU;
  161. ap->chan.hdrlen = 2; /* for A/C bytes */
  162. speed = tty_get_baud_rate(tty);
  163. ap->chan.speed = speed;
  164. err = ppp_register_channel(&ap->chan);
  165. if (err)
  166. goto out_free;
  167. tty->disc_data = ap;
  168. tty->receive_room = 65536;
  169. return 0;
  170. out_free:
  171. kfree(ap);
  172. out:
  173. return err;
  174. }
  175. /*
  176. * Called when the tty is put into another line discipline
  177. * or it hangs up. We have to wait for any cpu currently
  178. * executing in any of the other ppp_synctty_* routines to
  179. * finish before we can call ppp_unregister_channel and free
  180. * the syncppp struct. This routine must be called from
  181. * process context, not interrupt or softirq context.
  182. */
  183. static void
  184. ppp_sync_close(struct tty_struct *tty)
  185. {
  186. struct syncppp *ap;
  187. write_lock_irq(&disc_data_lock);
  188. ap = tty->disc_data;
  189. tty->disc_data = NULL;
  190. write_unlock_irq(&disc_data_lock);
  191. if (!ap)
  192. return;
  193. /*
  194. * We have now ensured that nobody can start using ap from now
  195. * on, but we have to wait for all existing users to finish.
  196. * Note that ppp_unregister_channel ensures that no calls to
  197. * our channel ops (i.e. ppp_sync_send/ioctl) are in progress
  198. * by the time it returns.
  199. */
  200. if (!refcount_dec_and_test(&ap->refcnt))
  201. wait_for_completion(&ap->dead_cmp);
  202. tasklet_kill(&ap->tsk);
  203. ppp_unregister_channel(&ap->chan);
  204. skb_queue_purge(&ap->rqueue);
  205. kfree_skb(ap->tpkt);
  206. kfree(ap);
  207. }
  208. /*
  209. * Called on tty hangup in process context.
  210. *
  211. * Wait for I/O to driver to complete and unregister PPP channel.
  212. * This is already done by the close routine, so just call that.
  213. */
  214. static void ppp_sync_hangup(struct tty_struct *tty)
  215. {
  216. ppp_sync_close(tty);
  217. }
  218. /*
  219. * Read does nothing - no data is ever available this way.
  220. * Pppd reads and writes packets via /dev/ppp instead.
  221. */
  222. static ssize_t
  223. ppp_sync_read(struct tty_struct *tty, struct file *file,
  224. unsigned char *buf, size_t count,
  225. void **cookie, unsigned long offset)
  226. {
  227. return -EAGAIN;
  228. }
  229. /*
  230. * Write on the tty does nothing, the packets all come in
  231. * from the ppp generic stuff.
  232. */
  233. static ssize_t
  234. ppp_sync_write(struct tty_struct *tty, struct file *file,
  235. const unsigned char *buf, size_t count)
  236. {
  237. return -EAGAIN;
  238. }
  239. static int
  240. ppp_synctty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  241. {
  242. struct syncppp *ap = sp_get(tty);
  243. int __user *p = (int __user *)arg;
  244. int err, val;
  245. if (!ap)
  246. return -ENXIO;
  247. err = -EFAULT;
  248. switch (cmd) {
  249. case PPPIOCGCHAN:
  250. err = -EFAULT;
  251. if (put_user(ppp_channel_index(&ap->chan), p))
  252. break;
  253. err = 0;
  254. break;
  255. case PPPIOCGUNIT:
  256. err = -EFAULT;
  257. if (put_user(ppp_unit_number(&ap->chan), p))
  258. break;
  259. err = 0;
  260. break;
  261. case TCFLSH:
  262. /* flush our buffers and the serial port's buffer */
  263. if (arg == TCIOFLUSH || arg == TCOFLUSH)
  264. ppp_sync_flush_output(ap);
  265. err = n_tty_ioctl_helper(tty, cmd, arg);
  266. break;
  267. case FIONREAD:
  268. val = 0;
  269. if (put_user(val, p))
  270. break;
  271. err = 0;
  272. break;
  273. default:
  274. err = tty_mode_ioctl(tty, cmd, arg);
  275. break;
  276. }
  277. sp_put(ap);
  278. return err;
  279. }
  280. /* No kernel lock - fine */
  281. static __poll_t
  282. ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
  283. {
  284. return 0;
  285. }
  286. /* May sleep, don't call from interrupt level or with interrupts disabled */
  287. static void
  288. ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
  289. const char *cflags, int count)
  290. {
  291. struct syncppp *ap = sp_get(tty);
  292. unsigned long flags;
  293. if (!ap)
  294. return;
  295. spin_lock_irqsave(&ap->recv_lock, flags);
  296. ppp_sync_input(ap, buf, cflags, count);
  297. spin_unlock_irqrestore(&ap->recv_lock, flags);
  298. if (!skb_queue_empty(&ap->rqueue))
  299. tasklet_schedule(&ap->tsk);
  300. sp_put(ap);
  301. tty_unthrottle(tty);
  302. }
  303. static void
  304. ppp_sync_wakeup(struct tty_struct *tty)
  305. {
  306. struct syncppp *ap = sp_get(tty);
  307. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  308. if (!ap)
  309. return;
  310. set_bit(XMIT_WAKEUP, &ap->xmit_flags);
  311. tasklet_schedule(&ap->tsk);
  312. sp_put(ap);
  313. }
  314. static struct tty_ldisc_ops ppp_sync_ldisc = {
  315. .owner = THIS_MODULE,
  316. .num = N_SYNC_PPP,
  317. .name = "pppsync",
  318. .open = ppp_sync_open,
  319. .close = ppp_sync_close,
  320. .hangup = ppp_sync_hangup,
  321. .read = ppp_sync_read,
  322. .write = ppp_sync_write,
  323. .ioctl = ppp_synctty_ioctl,
  324. .poll = ppp_sync_poll,
  325. .receive_buf = ppp_sync_receive,
  326. .write_wakeup = ppp_sync_wakeup,
  327. };
  328. static int __init
  329. ppp_sync_init(void)
  330. {
  331. int err;
  332. err = tty_register_ldisc(&ppp_sync_ldisc);
  333. if (err != 0)
  334. printk(KERN_ERR "PPP_sync: error %d registering line disc.\n",
  335. err);
  336. return err;
  337. }
  338. /*
  339. * The following routines provide the PPP channel interface.
  340. */
  341. static int
  342. ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
  343. {
  344. struct syncppp *ap = chan->private;
  345. int err, val;
  346. u32 accm[8];
  347. void __user *argp = (void __user *)arg;
  348. u32 __user *p = argp;
  349. err = -EFAULT;
  350. switch (cmd) {
  351. case PPPIOCGFLAGS:
  352. val = ap->flags | ap->rbits;
  353. if (put_user(val, (int __user *) argp))
  354. break;
  355. err = 0;
  356. break;
  357. case PPPIOCSFLAGS:
  358. if (get_user(val, (int __user *) argp))
  359. break;
  360. ap->flags = val & ~SC_RCV_BITS;
  361. spin_lock_irq(&ap->recv_lock);
  362. ap->rbits = val & SC_RCV_BITS;
  363. spin_unlock_irq(&ap->recv_lock);
  364. err = 0;
  365. break;
  366. case PPPIOCGASYNCMAP:
  367. if (put_user(ap->xaccm[0], p))
  368. break;
  369. err = 0;
  370. break;
  371. case PPPIOCSASYNCMAP:
  372. if (get_user(ap->xaccm[0], p))
  373. break;
  374. err = 0;
  375. break;
  376. case PPPIOCGRASYNCMAP:
  377. if (put_user(ap->raccm, p))
  378. break;
  379. err = 0;
  380. break;
  381. case PPPIOCSRASYNCMAP:
  382. if (get_user(ap->raccm, p))
  383. break;
  384. err = 0;
  385. break;
  386. case PPPIOCGXASYNCMAP:
  387. if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  388. break;
  389. err = 0;
  390. break;
  391. case PPPIOCSXASYNCMAP:
  392. if (copy_from_user(accm, argp, sizeof(accm)))
  393. break;
  394. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  395. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  396. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  397. err = 0;
  398. break;
  399. case PPPIOCGMRU:
  400. if (put_user(ap->mru, (int __user *) argp))
  401. break;
  402. err = 0;
  403. break;
  404. case PPPIOCSMRU:
  405. if (get_user(val, (int __user *) argp))
  406. break;
  407. if (val > U16_MAX) {
  408. err = -EINVAL;
  409. break;
  410. }
  411. if (val < PPP_MRU)
  412. val = PPP_MRU;
  413. ap->mru = val;
  414. err = 0;
  415. break;
  416. default:
  417. err = -ENOTTY;
  418. }
  419. return err;
  420. }
  421. /*
  422. * This is called at softirq level to deliver received packets
  423. * to the ppp_generic code, and to tell the ppp_generic code
  424. * if we can accept more output now.
  425. */
  426. static void ppp_sync_process(struct tasklet_struct *t)
  427. {
  428. struct syncppp *ap = from_tasklet(ap, t, tsk);
  429. struct sk_buff *skb;
  430. /* process received packets */
  431. while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
  432. if (skb->len == 0) {
  433. /* zero length buffers indicate error */
  434. ppp_input_error(&ap->chan, 0);
  435. kfree_skb(skb);
  436. }
  437. else
  438. ppp_input(&ap->chan, skb);
  439. }
  440. /* try to push more stuff out */
  441. if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_sync_push(ap))
  442. ppp_output_wakeup(&ap->chan);
  443. }
  444. /*
  445. * Procedures for encapsulation and framing.
  446. */
  447. static struct sk_buff*
  448. ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *skb)
  449. {
  450. int proto;
  451. unsigned char *data;
  452. int islcp;
  453. data = skb->data;
  454. proto = get_unaligned_be16(data);
  455. /* LCP packets with codes between 1 (configure-request)
  456. * and 7 (code-reject) must be sent as though no options
  457. * have been negotiated.
  458. */
  459. islcp = proto == PPP_LCP && 1 <= data[2] && data[2] <= 7;
  460. /* compress protocol field if option enabled */
  461. if (data[0] == 0 && (ap->flags & SC_COMP_PROT) && !islcp)
  462. skb_pull(skb,1);
  463. /* prepend address/control fields if necessary */
  464. if ((ap->flags & SC_COMP_AC) == 0 || islcp) {
  465. if (skb_headroom(skb) < 2) {
  466. struct sk_buff *npkt = dev_alloc_skb(skb->len + 2);
  467. if (npkt == NULL) {
  468. kfree_skb(skb);
  469. return NULL;
  470. }
  471. skb_reserve(npkt,2);
  472. skb_copy_from_linear_data(skb,
  473. skb_put(npkt, skb->len), skb->len);
  474. consume_skb(skb);
  475. skb = npkt;
  476. }
  477. skb_push(skb,2);
  478. skb->data[0] = PPP_ALLSTATIONS;
  479. skb->data[1] = PPP_UI;
  480. }
  481. ap->last_xmit = jiffies;
  482. if (skb && ap->flags & SC_LOG_OUTPKT)
  483. ppp_print_buffer ("send buffer", skb->data, skb->len);
  484. return skb;
  485. }
  486. /*
  487. * Transmit-side routines.
  488. */
  489. /*
  490. * Send a packet to the peer over an sync tty line.
  491. * Returns 1 iff the packet was accepted.
  492. * If the packet was not accepted, we will call ppp_output_wakeup
  493. * at some later time.
  494. */
  495. static int
  496. ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb)
  497. {
  498. struct syncppp *ap = chan->private;
  499. ppp_sync_push(ap);
  500. if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
  501. return 0; /* already full */
  502. skb = ppp_sync_txmunge(ap, skb);
  503. if (skb != NULL)
  504. ap->tpkt = skb;
  505. else
  506. clear_bit(XMIT_FULL, &ap->xmit_flags);
  507. ppp_sync_push(ap);
  508. return 1;
  509. }
  510. /*
  511. * Push as much data as possible out to the tty.
  512. */
  513. static int
  514. ppp_sync_push(struct syncppp *ap)
  515. {
  516. int sent, done = 0;
  517. struct tty_struct *tty = ap->tty;
  518. int tty_stuffed = 0;
  519. if (!spin_trylock_bh(&ap->xmit_lock))
  520. return 0;
  521. for (;;) {
  522. if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
  523. tty_stuffed = 0;
  524. if (!tty_stuffed && ap->tpkt) {
  525. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  526. sent = tty->ops->write(tty, ap->tpkt->data, ap->tpkt->len);
  527. if (sent < 0)
  528. goto flush; /* error, e.g. loss of CD */
  529. if (sent < ap->tpkt->len) {
  530. tty_stuffed = 1;
  531. } else {
  532. consume_skb(ap->tpkt);
  533. ap->tpkt = NULL;
  534. clear_bit(XMIT_FULL, &ap->xmit_flags);
  535. done = 1;
  536. }
  537. continue;
  538. }
  539. /* haven't made any progress */
  540. spin_unlock_bh(&ap->xmit_lock);
  541. if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags) ||
  542. (!tty_stuffed && ap->tpkt)))
  543. break;
  544. if (!spin_trylock_bh(&ap->xmit_lock))
  545. break;
  546. }
  547. return done;
  548. flush:
  549. if (ap->tpkt) {
  550. kfree_skb(ap->tpkt);
  551. ap->tpkt = NULL;
  552. clear_bit(XMIT_FULL, &ap->xmit_flags);
  553. done = 1;
  554. }
  555. spin_unlock_bh(&ap->xmit_lock);
  556. return done;
  557. }
  558. /*
  559. * Flush output from our internal buffers.
  560. * Called for the TCFLSH ioctl.
  561. */
  562. static void
  563. ppp_sync_flush_output(struct syncppp *ap)
  564. {
  565. int done = 0;
  566. spin_lock_bh(&ap->xmit_lock);
  567. if (ap->tpkt != NULL) {
  568. kfree_skb(ap->tpkt);
  569. ap->tpkt = NULL;
  570. clear_bit(XMIT_FULL, &ap->xmit_flags);
  571. done = 1;
  572. }
  573. spin_unlock_bh(&ap->xmit_lock);
  574. if (done)
  575. ppp_output_wakeup(&ap->chan);
  576. }
  577. /*
  578. * Receive-side routines.
  579. */
  580. /* called when the tty driver has data for us.
  581. *
  582. * Data is frame oriented: each call to ppp_sync_input is considered
  583. * a whole frame. If the 1st flag byte is non-zero then the whole
  584. * frame is considered to be in error and is tossed.
  585. */
  586. static void
  587. ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
  588. const char *flags, int count)
  589. {
  590. struct sk_buff *skb;
  591. unsigned char *p;
  592. if (count == 0)
  593. return;
  594. if (ap->flags & SC_LOG_INPKT)
  595. ppp_print_buffer ("receive buffer", buf, count);
  596. /* stuff the chars in the skb */
  597. skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
  598. if (!skb) {
  599. printk(KERN_ERR "PPPsync: no memory (input pkt)\n");
  600. goto err;
  601. }
  602. /* Try to get the payload 4-byte aligned */
  603. if (buf[0] != PPP_ALLSTATIONS)
  604. skb_reserve(skb, 2 + (buf[0] & 1));
  605. if (flags && *flags) {
  606. /* error flag set, ignore frame */
  607. goto err;
  608. } else if (count > skb_tailroom(skb)) {
  609. /* packet overflowed MRU */
  610. goto err;
  611. }
  612. skb_put_data(skb, buf, count);
  613. /* strip address/control field if present */
  614. p = skb->data;
  615. if (skb->len >= 2 && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
  616. /* chop off address/control */
  617. if (skb->len < 3)
  618. goto err;
  619. p = skb_pull(skb, 2);
  620. }
  621. /* PPP packet length should be >= 2 bytes when protocol field is not
  622. * compressed.
  623. */
  624. if (!(p[0] & 0x01) && skb->len < 2)
  625. goto err;
  626. /* queue the frame to be processed */
  627. skb_queue_tail(&ap->rqueue, skb);
  628. return;
  629. err:
  630. /* queue zero length packet as error indication */
  631. if (skb || (skb = dev_alloc_skb(0))) {
  632. skb_trim(skb, 0);
  633. skb_queue_tail(&ap->rqueue, skb);
  634. }
  635. }
  636. static void __exit
  637. ppp_sync_cleanup(void)
  638. {
  639. tty_unregister_ldisc(&ppp_sync_ldisc);
  640. }
  641. module_init(ppp_sync_init);
  642. module_exit(ppp_sync_cleanup);
  643. MODULE_LICENSE("GPL");
  644. MODULE_ALIAS_LDISC(N_SYNC_PPP);