ppp_synctty.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 paulkf@microgate.com
  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 <linux/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 u8 *buf, const u8 *flags,
  86. 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, u8 *buf, size_t count,
  224. void **cookie, unsigned long offset)
  225. {
  226. return -EAGAIN;
  227. }
  228. /*
  229. * Write on the tty does nothing, the packets all come in
  230. * from the ppp generic stuff.
  231. */
  232. static ssize_t
  233. ppp_sync_write(struct tty_struct *tty, struct file *file, const u8 *buf,
  234. size_t count)
  235. {
  236. return -EAGAIN;
  237. }
  238. static int
  239. ppp_synctty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  240. {
  241. struct syncppp *ap = sp_get(tty);
  242. int __user *p = (int __user *)arg;
  243. int err, val;
  244. if (!ap)
  245. return -ENXIO;
  246. err = -EFAULT;
  247. switch (cmd) {
  248. case PPPIOCGCHAN:
  249. err = -EFAULT;
  250. if (put_user(ppp_channel_index(&ap->chan), p))
  251. break;
  252. err = 0;
  253. break;
  254. case PPPIOCGUNIT:
  255. err = -EFAULT;
  256. if (put_user(ppp_unit_number(&ap->chan), p))
  257. break;
  258. err = 0;
  259. break;
  260. case TCFLSH:
  261. /* flush our buffers and the serial port's buffer */
  262. if (arg == TCIOFLUSH || arg == TCOFLUSH)
  263. ppp_sync_flush_output(ap);
  264. err = n_tty_ioctl_helper(tty, cmd, arg);
  265. break;
  266. case FIONREAD:
  267. val = 0;
  268. if (put_user(val, p))
  269. break;
  270. err = 0;
  271. break;
  272. default:
  273. err = tty_mode_ioctl(tty, cmd, arg);
  274. break;
  275. }
  276. sp_put(ap);
  277. return err;
  278. }
  279. /* May sleep, don't call from interrupt level or with interrupts disabled */
  280. static void
  281. ppp_sync_receive(struct tty_struct *tty, const u8 *buf, const u8 *cflags,
  282. size_t count)
  283. {
  284. struct syncppp *ap = sp_get(tty);
  285. unsigned long flags;
  286. if (!ap)
  287. return;
  288. spin_lock_irqsave(&ap->recv_lock, flags);
  289. ppp_sync_input(ap, buf, cflags, count);
  290. spin_unlock_irqrestore(&ap->recv_lock, flags);
  291. if (!skb_queue_empty(&ap->rqueue))
  292. tasklet_schedule(&ap->tsk);
  293. sp_put(ap);
  294. tty_unthrottle(tty);
  295. }
  296. static void
  297. ppp_sync_wakeup(struct tty_struct *tty)
  298. {
  299. struct syncppp *ap = sp_get(tty);
  300. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  301. if (!ap)
  302. return;
  303. set_bit(XMIT_WAKEUP, &ap->xmit_flags);
  304. tasklet_schedule(&ap->tsk);
  305. sp_put(ap);
  306. }
  307. static struct tty_ldisc_ops ppp_sync_ldisc = {
  308. .owner = THIS_MODULE,
  309. .num = N_SYNC_PPP,
  310. .name = "pppsync",
  311. .open = ppp_sync_open,
  312. .close = ppp_sync_close,
  313. .hangup = ppp_sync_hangup,
  314. .read = ppp_sync_read,
  315. .write = ppp_sync_write,
  316. .ioctl = ppp_synctty_ioctl,
  317. .receive_buf = ppp_sync_receive,
  318. .write_wakeup = ppp_sync_wakeup,
  319. };
  320. static int __init
  321. ppp_sync_init(void)
  322. {
  323. int err;
  324. err = tty_register_ldisc(&ppp_sync_ldisc);
  325. if (err != 0)
  326. printk(KERN_ERR "PPP_sync: error %d registering line disc.\n",
  327. err);
  328. return err;
  329. }
  330. /*
  331. * The following routines provide the PPP channel interface.
  332. */
  333. static int
  334. ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
  335. {
  336. struct syncppp *ap = chan->private;
  337. int err, val;
  338. u32 accm[8];
  339. void __user *argp = (void __user *)arg;
  340. u32 __user *p = argp;
  341. err = -EFAULT;
  342. switch (cmd) {
  343. case PPPIOCGFLAGS:
  344. val = ap->flags | ap->rbits;
  345. if (put_user(val, (int __user *) argp))
  346. break;
  347. err = 0;
  348. break;
  349. case PPPIOCSFLAGS:
  350. if (get_user(val, (int __user *) argp))
  351. break;
  352. ap->flags = val & ~SC_RCV_BITS;
  353. spin_lock_irq(&ap->recv_lock);
  354. ap->rbits = val & SC_RCV_BITS;
  355. spin_unlock_irq(&ap->recv_lock);
  356. err = 0;
  357. break;
  358. case PPPIOCGASYNCMAP:
  359. if (put_user(ap->xaccm[0], p))
  360. break;
  361. err = 0;
  362. break;
  363. case PPPIOCSASYNCMAP:
  364. if (get_user(ap->xaccm[0], p))
  365. break;
  366. err = 0;
  367. break;
  368. case PPPIOCGRASYNCMAP:
  369. if (put_user(ap->raccm, p))
  370. break;
  371. err = 0;
  372. break;
  373. case PPPIOCSRASYNCMAP:
  374. if (get_user(ap->raccm, p))
  375. break;
  376. err = 0;
  377. break;
  378. case PPPIOCGXASYNCMAP:
  379. if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  380. break;
  381. err = 0;
  382. break;
  383. case PPPIOCSXASYNCMAP:
  384. if (copy_from_user(accm, argp, sizeof(accm)))
  385. break;
  386. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  387. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  388. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  389. err = 0;
  390. break;
  391. case PPPIOCGMRU:
  392. if (put_user(ap->mru, (int __user *) argp))
  393. break;
  394. err = 0;
  395. break;
  396. case PPPIOCSMRU:
  397. if (get_user(val, (int __user *) argp))
  398. break;
  399. if (val > U16_MAX) {
  400. err = -EINVAL;
  401. break;
  402. }
  403. if (val < PPP_MRU)
  404. val = PPP_MRU;
  405. ap->mru = val;
  406. err = 0;
  407. break;
  408. default:
  409. err = -ENOTTY;
  410. }
  411. return err;
  412. }
  413. /*
  414. * This is called at softirq level to deliver received packets
  415. * to the ppp_generic code, and to tell the ppp_generic code
  416. * if we can accept more output now.
  417. */
  418. static void ppp_sync_process(struct tasklet_struct *t)
  419. {
  420. struct syncppp *ap = from_tasklet(ap, t, tsk);
  421. struct sk_buff *skb;
  422. /* process received packets */
  423. while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
  424. if (skb->len == 0) {
  425. /* zero length buffers indicate error */
  426. ppp_input_error(&ap->chan, 0);
  427. kfree_skb(skb);
  428. }
  429. else
  430. ppp_input(&ap->chan, skb);
  431. }
  432. /* try to push more stuff out */
  433. if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_sync_push(ap))
  434. ppp_output_wakeup(&ap->chan);
  435. }
  436. /*
  437. * Procedures for encapsulation and framing.
  438. */
  439. static struct sk_buff*
  440. ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *skb)
  441. {
  442. int proto;
  443. unsigned char *data;
  444. int islcp;
  445. /* Ensure we can safely access protocol field and LCP code */
  446. if (!pskb_may_pull(skb, 3)) {
  447. kfree_skb(skb);
  448. return NULL;
  449. }
  450. data = skb->data;
  451. proto = get_unaligned_be16(data);
  452. /* LCP packets with codes between 1 (configure-request)
  453. * and 7 (code-reject) must be sent as though no options
  454. * have been negotiated.
  455. */
  456. islcp = proto == PPP_LCP && 1 <= data[2] && data[2] <= 7;
  457. /* compress protocol field if option enabled */
  458. if (data[0] == 0 && (ap->flags & SC_COMP_PROT) && !islcp)
  459. skb_pull(skb,1);
  460. /* prepend address/control fields if necessary */
  461. if ((ap->flags & SC_COMP_AC) == 0 || islcp) {
  462. if (skb_headroom(skb) < 2) {
  463. struct sk_buff *npkt = dev_alloc_skb(skb->len + 2);
  464. if (npkt == NULL) {
  465. kfree_skb(skb);
  466. return NULL;
  467. }
  468. skb_reserve(npkt,2);
  469. skb_copy_from_linear_data(skb,
  470. skb_put(npkt, skb->len), skb->len);
  471. consume_skb(skb);
  472. skb = npkt;
  473. }
  474. skb_push(skb,2);
  475. skb->data[0] = PPP_ALLSTATIONS;
  476. skb->data[1] = PPP_UI;
  477. }
  478. ap->last_xmit = jiffies;
  479. if (skb && ap->flags & SC_LOG_OUTPKT)
  480. ppp_print_buffer ("send buffer", skb->data, skb->len);
  481. return skb;
  482. }
  483. /*
  484. * Transmit-side routines.
  485. */
  486. /*
  487. * Send a packet to the peer over an sync tty line.
  488. * Returns 1 iff the packet was accepted.
  489. * If the packet was not accepted, we will call ppp_output_wakeup
  490. * at some later time.
  491. */
  492. static int
  493. ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb)
  494. {
  495. struct syncppp *ap = chan->private;
  496. ppp_sync_push(ap);
  497. if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
  498. return 0; /* already full */
  499. skb = ppp_sync_txmunge(ap, skb);
  500. if (skb != NULL)
  501. ap->tpkt = skb;
  502. else
  503. clear_bit(XMIT_FULL, &ap->xmit_flags);
  504. ppp_sync_push(ap);
  505. return 1;
  506. }
  507. /*
  508. * Push as much data as possible out to the tty.
  509. */
  510. static int
  511. ppp_sync_push(struct syncppp *ap)
  512. {
  513. int sent, done = 0;
  514. struct tty_struct *tty = ap->tty;
  515. int tty_stuffed = 0;
  516. if (!spin_trylock_bh(&ap->xmit_lock))
  517. return 0;
  518. for (;;) {
  519. if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
  520. tty_stuffed = 0;
  521. if (!tty_stuffed && ap->tpkt) {
  522. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  523. sent = tty->ops->write(tty, ap->tpkt->data, ap->tpkt->len);
  524. if (sent < 0)
  525. goto flush; /* error, e.g. loss of CD */
  526. if (sent < ap->tpkt->len) {
  527. tty_stuffed = 1;
  528. } else {
  529. consume_skb(ap->tpkt);
  530. ap->tpkt = NULL;
  531. clear_bit(XMIT_FULL, &ap->xmit_flags);
  532. done = 1;
  533. }
  534. continue;
  535. }
  536. /* haven't made any progress */
  537. spin_unlock_bh(&ap->xmit_lock);
  538. if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags) ||
  539. (!tty_stuffed && ap->tpkt)))
  540. break;
  541. if (!spin_trylock_bh(&ap->xmit_lock))
  542. break;
  543. }
  544. return done;
  545. flush:
  546. if (ap->tpkt) {
  547. kfree_skb(ap->tpkt);
  548. ap->tpkt = NULL;
  549. clear_bit(XMIT_FULL, &ap->xmit_flags);
  550. done = 1;
  551. }
  552. spin_unlock_bh(&ap->xmit_lock);
  553. return done;
  554. }
  555. /*
  556. * Flush output from our internal buffers.
  557. * Called for the TCFLSH ioctl.
  558. */
  559. static void
  560. ppp_sync_flush_output(struct syncppp *ap)
  561. {
  562. int done = 0;
  563. spin_lock_bh(&ap->xmit_lock);
  564. if (ap->tpkt != NULL) {
  565. kfree_skb(ap->tpkt);
  566. ap->tpkt = NULL;
  567. clear_bit(XMIT_FULL, &ap->xmit_flags);
  568. done = 1;
  569. }
  570. spin_unlock_bh(&ap->xmit_lock);
  571. if (done)
  572. ppp_output_wakeup(&ap->chan);
  573. }
  574. /*
  575. * Receive-side routines.
  576. */
  577. /* called when the tty driver has data for us.
  578. *
  579. * Data is frame oriented: each call to ppp_sync_input is considered
  580. * a whole frame. If the 1st flag byte is non-zero then the whole
  581. * frame is considered to be in error and is tossed.
  582. */
  583. static void
  584. ppp_sync_input(struct syncppp *ap, const u8 *buf, const u8 *flags, int count)
  585. {
  586. struct sk_buff *skb;
  587. unsigned char *p;
  588. if (count == 0)
  589. return;
  590. if (ap->flags & SC_LOG_INPKT)
  591. ppp_print_buffer ("receive buffer", buf, count);
  592. /* stuff the chars in the skb */
  593. skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
  594. if (!skb) {
  595. printk(KERN_ERR "PPPsync: no memory (input pkt)\n");
  596. goto err;
  597. }
  598. /* Try to get the payload 4-byte aligned */
  599. if (buf[0] != PPP_ALLSTATIONS)
  600. skb_reserve(skb, 2 + (buf[0] & 1));
  601. if (flags && *flags) {
  602. /* error flag set, ignore frame */
  603. goto err;
  604. } else if (count > skb_tailroom(skb)) {
  605. /* packet overflowed MRU */
  606. goto err;
  607. }
  608. skb_put_data(skb, buf, count);
  609. /* strip address/control field if present */
  610. p = skb->data;
  611. if (skb->len >= 2 && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
  612. /* chop off address/control */
  613. if (skb->len < 3)
  614. goto err;
  615. p = skb_pull(skb, 2);
  616. }
  617. /* PPP packet length should be >= 2 bytes when protocol field is not
  618. * compressed.
  619. */
  620. if (!(p[0] & 0x01) && skb->len < 2)
  621. goto err;
  622. /* queue the frame to be processed */
  623. skb_queue_tail(&ap->rqueue, skb);
  624. return;
  625. err:
  626. /* queue zero length packet as error indication */
  627. if (skb || (skb = dev_alloc_skb(0))) {
  628. skb_trim(skb, 0);
  629. skb_queue_tail(&ap->rqueue, skb);
  630. }
  631. }
  632. static void __exit
  633. ppp_sync_cleanup(void)
  634. {
  635. tty_unregister_ldisc(&ppp_sync_ldisc);
  636. }
  637. module_init(ppp_sync_init);
  638. module_exit(ppp_sync_cleanup);
  639. MODULE_DESCRIPTION("PPP synchronous TTY channel module");
  640. MODULE_LICENSE("GPL");
  641. MODULE_ALIAS_LDISC(N_SYNC_PPP);