ppp_synctty.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 <asm/unaligned.h>
  49. #include <asm/uaccess.h>
  50. #define PPP_VERSION "2.4.2"
  51. /* Structure for storing local state. */
  52. struct syncppp {
  53. struct tty_struct *tty;
  54. unsigned int flags;
  55. unsigned int rbits;
  56. int mru;
  57. spinlock_t xmit_lock;
  58. spinlock_t recv_lock;
  59. unsigned long xmit_flags;
  60. u32 xaccm[8];
  61. u32 raccm;
  62. unsigned int bytes_sent;
  63. unsigned int bytes_rcvd;
  64. struct sk_buff *tpkt;
  65. unsigned long last_xmit;
  66. struct sk_buff_head rqueue;
  67. struct tasklet_struct tsk;
  68. atomic_t refcnt;
  69. struct completion dead_cmp;
  70. struct ppp_channel chan; /* interface to generic ppp layer */
  71. };
  72. /* Bit numbers in xmit_flags */
  73. #define XMIT_WAKEUP 0
  74. #define XMIT_FULL 1
  75. /* Bits in rbits */
  76. #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
  77. #define PPPSYNC_MAX_RQLEN 32 /* arbitrary */
  78. /*
  79. * Prototypes.
  80. */
  81. static struct sk_buff* ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *);
  82. static int ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb);
  83. static int ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd,
  84. unsigned long arg);
  85. static void ppp_sync_process(unsigned long arg);
  86. static int ppp_sync_push(struct syncppp *ap);
  87. static void ppp_sync_flush_output(struct syncppp *ap);
  88. static void ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
  89. char *flags, int count);
  90. static const struct ppp_channel_ops sync_ops = {
  91. .start_xmit = ppp_sync_send,
  92. .ioctl = ppp_sync_ioctl,
  93. };
  94. /*
  95. * Utility procedure to print a buffer in hex/ascii
  96. */
  97. static void
  98. ppp_print_buffer (const char *name, const __u8 *buf, int count)
  99. {
  100. if (name != NULL)
  101. printk(KERN_DEBUG "ppp_synctty: %s, count = %d\n", name, count);
  102. print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, count);
  103. }
  104. /*
  105. * Routines implementing the synchronous PPP line discipline.
  106. */
  107. /*
  108. * We have a potential race on dereferencing tty->disc_data,
  109. * because the tty layer provides no locking at all - thus one
  110. * cpu could be running ppp_synctty_receive while another
  111. * calls ppp_synctty_close, which zeroes tty->disc_data and
  112. * frees the memory that ppp_synctty_receive is using. The best
  113. * way to fix this is to use a rwlock in the tty struct, but for now
  114. * we use a single global rwlock for all ttys in ppp line discipline.
  115. *
  116. * FIXME: Fixed in tty_io nowadays.
  117. */
  118. static DEFINE_RWLOCK(disc_data_lock);
  119. static struct syncppp *sp_get(struct tty_struct *tty)
  120. {
  121. struct syncppp *ap;
  122. read_lock(&disc_data_lock);
  123. ap = tty->disc_data;
  124. if (ap != NULL)
  125. atomic_inc(&ap->refcnt);
  126. read_unlock(&disc_data_lock);
  127. return ap;
  128. }
  129. static void sp_put(struct syncppp *ap)
  130. {
  131. if (atomic_dec_and_test(&ap->refcnt))
  132. complete(&ap->dead_cmp);
  133. }
  134. /*
  135. * Called when a tty is put into sync-PPP line discipline.
  136. */
  137. static int
  138. ppp_sync_open(struct tty_struct *tty)
  139. {
  140. struct syncppp *ap;
  141. int err;
  142. int speed;
  143. if (tty->ops->write == NULL)
  144. return -EOPNOTSUPP;
  145. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  146. err = -ENOMEM;
  147. if (!ap)
  148. goto out;
  149. /* initialize the syncppp structure */
  150. ap->tty = tty;
  151. ap->mru = PPP_MRU;
  152. spin_lock_init(&ap->xmit_lock);
  153. spin_lock_init(&ap->recv_lock);
  154. ap->xaccm[0] = ~0U;
  155. ap->xaccm[3] = 0x60000000U;
  156. ap->raccm = ~0U;
  157. skb_queue_head_init(&ap->rqueue);
  158. tasklet_init(&ap->tsk, ppp_sync_process, (unsigned long) ap);
  159. atomic_set(&ap->refcnt, 1);
  160. init_completion(&ap->dead_cmp);
  161. ap->chan.private = ap;
  162. ap->chan.ops = &sync_ops;
  163. ap->chan.mtu = PPP_MRU;
  164. ap->chan.hdrlen = 2; /* for A/C bytes */
  165. speed = tty_get_baud_rate(tty);
  166. ap->chan.speed = speed;
  167. err = ppp_register_channel(&ap->chan);
  168. if (err)
  169. goto out_free;
  170. tty->disc_data = ap;
  171. tty->receive_room = 65536;
  172. return 0;
  173. out_free:
  174. kfree(ap);
  175. out:
  176. return err;
  177. }
  178. /*
  179. * Called when the tty is put into another line discipline
  180. * or it hangs up. We have to wait for any cpu currently
  181. * executing in any of the other ppp_synctty_* routines to
  182. * finish before we can call ppp_unregister_channel and free
  183. * the syncppp struct. This routine must be called from
  184. * process context, not interrupt or softirq context.
  185. */
  186. static void
  187. ppp_sync_close(struct tty_struct *tty)
  188. {
  189. struct syncppp *ap;
  190. write_lock_irq(&disc_data_lock);
  191. ap = tty->disc_data;
  192. tty->disc_data = NULL;
  193. write_unlock_irq(&disc_data_lock);
  194. if (!ap)
  195. return;
  196. /*
  197. * We have now ensured that nobody can start using ap from now
  198. * on, but we have to wait for all existing users to finish.
  199. * Note that ppp_unregister_channel ensures that no calls to
  200. * our channel ops (i.e. ppp_sync_send/ioctl) are in progress
  201. * by the time it returns.
  202. */
  203. if (!atomic_dec_and_test(&ap->refcnt))
  204. wait_for_completion(&ap->dead_cmp);
  205. tasklet_kill(&ap->tsk);
  206. ppp_unregister_channel(&ap->chan);
  207. skb_queue_purge(&ap->rqueue);
  208. kfree_skb(ap->tpkt);
  209. kfree(ap);
  210. }
  211. /*
  212. * Called on tty hangup in process context.
  213. *
  214. * Wait for I/O to driver to complete and unregister PPP channel.
  215. * This is already done by the close routine, so just call that.
  216. */
  217. static int ppp_sync_hangup(struct tty_struct *tty)
  218. {
  219. ppp_sync_close(tty);
  220. return 0;
  221. }
  222. /*
  223. * Read does nothing - no data is ever available this way.
  224. * Pppd reads and writes packets via /dev/ppp instead.
  225. */
  226. static ssize_t
  227. ppp_sync_read(struct tty_struct *tty, struct file *file,
  228. unsigned char __user *buf, size_t count)
  229. {
  230. return -EAGAIN;
  231. }
  232. /*
  233. * Write on the tty does nothing, the packets all come in
  234. * from the ppp generic stuff.
  235. */
  236. static ssize_t
  237. ppp_sync_write(struct tty_struct *tty, struct file *file,
  238. const unsigned char *buf, size_t count)
  239. {
  240. return -EAGAIN;
  241. }
  242. static int
  243. ppp_synctty_ioctl(struct tty_struct *tty, struct file *file,
  244. unsigned int cmd, unsigned long arg)
  245. {
  246. struct syncppp *ap = sp_get(tty);
  247. int __user *p = (int __user *)arg;
  248. int err, val;
  249. if (!ap)
  250. return -ENXIO;
  251. err = -EFAULT;
  252. switch (cmd) {
  253. case PPPIOCGCHAN:
  254. err = -EFAULT;
  255. if (put_user(ppp_channel_index(&ap->chan), p))
  256. break;
  257. err = 0;
  258. break;
  259. case PPPIOCGUNIT:
  260. err = -EFAULT;
  261. if (put_user(ppp_unit_number(&ap->chan), p))
  262. break;
  263. err = 0;
  264. break;
  265. case TCFLSH:
  266. /* flush our buffers and the serial port's buffer */
  267. if (arg == TCIOFLUSH || arg == TCOFLUSH)
  268. ppp_sync_flush_output(ap);
  269. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  270. break;
  271. case FIONREAD:
  272. val = 0;
  273. if (put_user(val, p))
  274. break;
  275. err = 0;
  276. break;
  277. default:
  278. err = tty_mode_ioctl(tty, file, cmd, arg);
  279. break;
  280. }
  281. sp_put(ap);
  282. return err;
  283. }
  284. /* No kernel lock - fine */
  285. static unsigned int
  286. ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
  287. {
  288. return 0;
  289. }
  290. /* May sleep, don't call from interrupt level or with interrupts disabled */
  291. static void
  292. ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
  293. char *cflags, int count)
  294. {
  295. struct syncppp *ap = sp_get(tty);
  296. unsigned long flags;
  297. if (!ap)
  298. return;
  299. spin_lock_irqsave(&ap->recv_lock, flags);
  300. ppp_sync_input(ap, buf, cflags, count);
  301. spin_unlock_irqrestore(&ap->recv_lock, flags);
  302. if (!skb_queue_empty(&ap->rqueue))
  303. tasklet_schedule(&ap->tsk);
  304. sp_put(ap);
  305. tty_unthrottle(tty);
  306. }
  307. static void
  308. ppp_sync_wakeup(struct tty_struct *tty)
  309. {
  310. struct syncppp *ap = sp_get(tty);
  311. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  312. if (!ap)
  313. return;
  314. set_bit(XMIT_WAKEUP, &ap->xmit_flags);
  315. tasklet_schedule(&ap->tsk);
  316. sp_put(ap);
  317. }
  318. static struct tty_ldisc_ops ppp_sync_ldisc = {
  319. .owner = THIS_MODULE,
  320. .magic = TTY_LDISC_MAGIC,
  321. .name = "pppsync",
  322. .open = ppp_sync_open,
  323. .close = ppp_sync_close,
  324. .hangup = ppp_sync_hangup,
  325. .read = ppp_sync_read,
  326. .write = ppp_sync_write,
  327. .ioctl = ppp_synctty_ioctl,
  328. .poll = ppp_sync_poll,
  329. .receive_buf = ppp_sync_receive,
  330. .write_wakeup = ppp_sync_wakeup,
  331. };
  332. static int __init
  333. ppp_sync_init(void)
  334. {
  335. int err;
  336. err = tty_register_ldisc(N_SYNC_PPP, &ppp_sync_ldisc);
  337. if (err != 0)
  338. printk(KERN_ERR "PPP_sync: error %d registering line disc.\n",
  339. err);
  340. return err;
  341. }
  342. /*
  343. * The following routines provide the PPP channel interface.
  344. */
  345. static int
  346. ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
  347. {
  348. struct syncppp *ap = chan->private;
  349. int err, val;
  350. u32 accm[8];
  351. void __user *argp = (void __user *)arg;
  352. u32 __user *p = argp;
  353. err = -EFAULT;
  354. switch (cmd) {
  355. case PPPIOCGFLAGS:
  356. val = ap->flags | ap->rbits;
  357. if (put_user(val, (int __user *) argp))
  358. break;
  359. err = 0;
  360. break;
  361. case PPPIOCSFLAGS:
  362. if (get_user(val, (int __user *) argp))
  363. break;
  364. ap->flags = val & ~SC_RCV_BITS;
  365. spin_lock_irq(&ap->recv_lock);
  366. ap->rbits = val & SC_RCV_BITS;
  367. spin_unlock_irq(&ap->recv_lock);
  368. err = 0;
  369. break;
  370. case PPPIOCGASYNCMAP:
  371. if (put_user(ap->xaccm[0], p))
  372. break;
  373. err = 0;
  374. break;
  375. case PPPIOCSASYNCMAP:
  376. if (get_user(ap->xaccm[0], p))
  377. break;
  378. err = 0;
  379. break;
  380. case PPPIOCGRASYNCMAP:
  381. if (put_user(ap->raccm, p))
  382. break;
  383. err = 0;
  384. break;
  385. case PPPIOCSRASYNCMAP:
  386. if (get_user(ap->raccm, p))
  387. break;
  388. err = 0;
  389. break;
  390. case PPPIOCGXASYNCMAP:
  391. if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  392. break;
  393. err = 0;
  394. break;
  395. case PPPIOCSXASYNCMAP:
  396. if (copy_from_user(accm, argp, sizeof(accm)))
  397. break;
  398. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  399. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  400. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  401. err = 0;
  402. break;
  403. case PPPIOCGMRU:
  404. if (put_user(ap->mru, (int __user *) argp))
  405. break;
  406. err = 0;
  407. break;
  408. case PPPIOCSMRU:
  409. if (get_user(val, (int __user *) argp))
  410. break;
  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(unsigned long arg)
  427. {
  428. struct syncppp *ap = (struct syncppp *) arg;
  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. 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. p = skb_put(skb, count);
  613. memcpy(p, 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. skb_push(skb, 1)[0] = 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);