ppp_synctty.c 18 KB

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