ppp_synctty.c 17 KB

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