ppp_synctty.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 <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(unsigned long arg);
  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. 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_init(&ap->tsk, ppp_sync_process, (unsigned long) ap);
  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 int ppp_sync_hangup(struct tty_struct *tty)
  215. {
  216. ppp_sync_close(tty);
  217. return 0;
  218. }
  219. /*
  220. * Read does nothing - no data is ever available this way.
  221. * Pppd reads and writes packets via /dev/ppp instead.
  222. */
  223. static ssize_t
  224. ppp_sync_read(struct tty_struct *tty, struct file *file,
  225. unsigned char __user *buf, size_t count)
  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, struct file *file,
  241. unsigned int cmd, unsigned long arg)
  242. {
  243. struct syncppp *ap = sp_get(tty);
  244. int __user *p = (int __user *)arg;
  245. int err, val;
  246. if (!ap)
  247. return -ENXIO;
  248. err = -EFAULT;
  249. switch (cmd) {
  250. case PPPIOCGCHAN:
  251. err = -EFAULT;
  252. if (put_user(ppp_channel_index(&ap->chan), p))
  253. break;
  254. err = 0;
  255. break;
  256. case PPPIOCGUNIT:
  257. err = -EFAULT;
  258. if (put_user(ppp_unit_number(&ap->chan), p))
  259. break;
  260. err = 0;
  261. break;
  262. case TCFLSH:
  263. /* flush our buffers and the serial port's buffer */
  264. if (arg == TCIOFLUSH || arg == TCOFLUSH)
  265. ppp_sync_flush_output(ap);
  266. err = n_tty_ioctl_helper(tty, file, cmd, arg);
  267. break;
  268. case FIONREAD:
  269. val = 0;
  270. if (put_user(val, p))
  271. break;
  272. err = 0;
  273. break;
  274. default:
  275. err = tty_mode_ioctl(tty, file, cmd, arg);
  276. break;
  277. }
  278. sp_put(ap);
  279. return err;
  280. }
  281. /* No kernel lock - fine */
  282. static __poll_t
  283. ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
  284. {
  285. return 0;
  286. }
  287. /* May sleep, don't call from interrupt level or with interrupts disabled */
  288. static void
  289. ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
  290. char *cflags, int count)
  291. {
  292. struct syncppp *ap = sp_get(tty);
  293. unsigned long flags;
  294. if (!ap)
  295. return;
  296. spin_lock_irqsave(&ap->recv_lock, flags);
  297. ppp_sync_input(ap, buf, cflags, count);
  298. spin_unlock_irqrestore(&ap->recv_lock, flags);
  299. if (!skb_queue_empty(&ap->rqueue))
  300. tasklet_schedule(&ap->tsk);
  301. sp_put(ap);
  302. tty_unthrottle(tty);
  303. }
  304. static void
  305. ppp_sync_wakeup(struct tty_struct *tty)
  306. {
  307. struct syncppp *ap = sp_get(tty);
  308. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  309. if (!ap)
  310. return;
  311. set_bit(XMIT_WAKEUP, &ap->xmit_flags);
  312. tasklet_schedule(&ap->tsk);
  313. sp_put(ap);
  314. }
  315. static struct tty_ldisc_ops ppp_sync_ldisc = {
  316. .owner = THIS_MODULE,
  317. .magic = TTY_LDISC_MAGIC,
  318. .name = "pppsync",
  319. .open = ppp_sync_open,
  320. .close = ppp_sync_close,
  321. .hangup = ppp_sync_hangup,
  322. .read = ppp_sync_read,
  323. .write = ppp_sync_write,
  324. .ioctl = ppp_synctty_ioctl,
  325. .poll = ppp_sync_poll,
  326. .receive_buf = ppp_sync_receive,
  327. .write_wakeup = ppp_sync_wakeup,
  328. };
  329. static int __init
  330. ppp_sync_init(void)
  331. {
  332. int err;
  333. err = tty_register_ldisc(N_SYNC_PPP, &ppp_sync_ldisc);
  334. if (err != 0)
  335. printk(KERN_ERR "PPP_sync: error %d registering line disc.\n",
  336. err);
  337. return err;
  338. }
  339. /*
  340. * The following routines provide the PPP channel interface.
  341. */
  342. static int
  343. ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
  344. {
  345. struct syncppp *ap = chan->private;
  346. int err, val;
  347. u32 accm[8];
  348. void __user *argp = (void __user *)arg;
  349. u32 __user *p = argp;
  350. err = -EFAULT;
  351. switch (cmd) {
  352. case PPPIOCGFLAGS:
  353. val = ap->flags | ap->rbits;
  354. if (put_user(val, (int __user *) argp))
  355. break;
  356. err = 0;
  357. break;
  358. case PPPIOCSFLAGS:
  359. if (get_user(val, (int __user *) argp))
  360. break;
  361. ap->flags = val & ~SC_RCV_BITS;
  362. spin_lock_irq(&ap->recv_lock);
  363. ap->rbits = val & SC_RCV_BITS;
  364. spin_unlock_irq(&ap->recv_lock);
  365. err = 0;
  366. break;
  367. case PPPIOCGASYNCMAP:
  368. if (put_user(ap->xaccm[0], p))
  369. break;
  370. err = 0;
  371. break;
  372. case PPPIOCSASYNCMAP:
  373. if (get_user(ap->xaccm[0], p))
  374. break;
  375. err = 0;
  376. break;
  377. case PPPIOCGRASYNCMAP:
  378. if (put_user(ap->raccm, p))
  379. break;
  380. err = 0;
  381. break;
  382. case PPPIOCSRASYNCMAP:
  383. if (get_user(ap->raccm, p))
  384. break;
  385. err = 0;
  386. break;
  387. case PPPIOCGXASYNCMAP:
  388. if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  389. break;
  390. err = 0;
  391. break;
  392. case PPPIOCSXASYNCMAP:
  393. if (copy_from_user(accm, argp, sizeof(accm)))
  394. break;
  395. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  396. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  397. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  398. err = 0;
  399. break;
  400. case PPPIOCGMRU:
  401. if (put_user(ap->mru, (int __user *) argp))
  402. break;
  403. err = 0;
  404. break;
  405. case PPPIOCSMRU:
  406. if (get_user(val, (int __user *) argp))
  407. break;
  408. if (val < PPP_MRU)
  409. val = PPP_MRU;
  410. ap->mru = val;
  411. err = 0;
  412. break;
  413. default:
  414. err = -ENOTTY;
  415. }
  416. return err;
  417. }
  418. /*
  419. * This is called at softirq level to deliver received packets
  420. * to the ppp_generic code, and to tell the ppp_generic code
  421. * if we can accept more output now.
  422. */
  423. static void ppp_sync_process(unsigned long arg)
  424. {
  425. struct syncppp *ap = (struct syncppp *) arg;
  426. struct sk_buff *skb;
  427. /* process received packets */
  428. while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
  429. if (skb->len == 0) {
  430. /* zero length buffers indicate error */
  431. ppp_input_error(&ap->chan, 0);
  432. kfree_skb(skb);
  433. }
  434. else
  435. ppp_input(&ap->chan, skb);
  436. }
  437. /* try to push more stuff out */
  438. if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_sync_push(ap))
  439. ppp_output_wakeup(&ap->chan);
  440. }
  441. /*
  442. * Procedures for encapsulation and framing.
  443. */
  444. static struct sk_buff*
  445. ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *skb)
  446. {
  447. int proto;
  448. unsigned char *data;
  449. int islcp;
  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 unsigned char *buf,
  585. char *flags, int count)
  586. {
  587. struct sk_buff *skb;
  588. unsigned char *p;
  589. if (count == 0)
  590. return;
  591. if (ap->flags & SC_LOG_INPKT)
  592. ppp_print_buffer ("receive buffer", buf, count);
  593. /* stuff the chars in the skb */
  594. skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
  595. if (!skb) {
  596. printk(KERN_ERR "PPPsync: no memory (input pkt)\n");
  597. goto err;
  598. }
  599. /* Try to get the payload 4-byte aligned */
  600. if (buf[0] != PPP_ALLSTATIONS)
  601. skb_reserve(skb, 2 + (buf[0] & 1));
  602. if (flags && *flags) {
  603. /* error flag set, ignore frame */
  604. goto err;
  605. } else if (count > skb_tailroom(skb)) {
  606. /* packet overflowed MRU */
  607. goto err;
  608. }
  609. skb_put_data(skb, buf, count);
  610. /* strip address/control field if present */
  611. p = skb->data;
  612. if (p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
  613. /* chop off address/control */
  614. if (skb->len < 3)
  615. goto err;
  616. p = skb_pull(skb, 2);
  617. }
  618. /* PPP packet length should be >= 2 bytes when protocol field is not
  619. * compressed.
  620. */
  621. if (!(p[0] & 0x01) && skb->len < 2)
  622. goto err;
  623. /* queue the frame to be processed */
  624. skb_queue_tail(&ap->rqueue, skb);
  625. return;
  626. err:
  627. /* queue zero length packet as error indication */
  628. if (skb || (skb = dev_alloc_skb(0))) {
  629. skb_trim(skb, 0);
  630. skb_queue_tail(&ap->rqueue, skb);
  631. }
  632. }
  633. static void __exit
  634. ppp_sync_cleanup(void)
  635. {
  636. if (tty_unregister_ldisc(N_SYNC_PPP) != 0)
  637. printk(KERN_ERR "failed to unregister Sync PPP line discipline\n");
  638. }
  639. module_init(ppp_sync_init);
  640. module_exit(ppp_sync_cleanup);
  641. MODULE_LICENSE("GPL");
  642. MODULE_ALIAS_LDISC(N_SYNC_PPP);