pppoatm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */
  2. /* Copyright 1999-2000 by Mitchell Blank Jr */
  3. /* Based on clip.c; 1995-1999 by Werner Almesberger, EPFL LRC/ICA */
  4. /* And on ppp_async.c; Copyright 1999 Paul Mackerras */
  5. /* And help from Jens Axboe */
  6. /*
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * This driver provides the encapsulation and framing for sending
  13. * and receiving PPP frames in ATM AAL5 PDUs.
  14. */
  15. /*
  16. * One shortcoming of this driver is that it does not comply with
  17. * section 8 of RFC2364 - we are supposed to detect a change
  18. * in encapsulation and immediately abort the connection (in order
  19. * to avoid a black-hole being created if our peer loses state
  20. * and changes encapsulation unilaterally. However, since the
  21. * ppp_generic layer actually does the decapsulation, we need
  22. * a way of notifying it when we _think_ there might be a problem)
  23. * There's two cases:
  24. * 1. LLC-encapsulation was missing when it was enabled. In
  25. * this case, we should tell the upper layer "tear down
  26. * this session if this skb looks ok to you"
  27. * 2. LLC-encapsulation was present when it was disabled. Then
  28. * we need to tell the upper layer "this packet may be
  29. * ok, but if its in error tear down the session"
  30. * These hooks are not yet available in ppp_generic
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/slab.h>
  38. #include <linux/atm.h>
  39. #include <linux/atmdev.h>
  40. #include <linux/capability.h>
  41. #include <linux/ppp_defs.h>
  42. #include <linux/ppp-ioctl.h>
  43. #include <linux/ppp_channel.h>
  44. #include <linux/atmppp.h>
  45. #include "common.h"
  46. enum pppoatm_encaps {
  47. e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
  48. e_vc = PPPOATM_ENCAPS_VC,
  49. e_llc = PPPOATM_ENCAPS_LLC,
  50. };
  51. struct pppoatm_vcc {
  52. struct atm_vcc *atmvcc; /* VCC descriptor */
  53. void (*old_push)(struct atm_vcc *, struct sk_buff *);
  54. void (*old_pop)(struct atm_vcc *, struct sk_buff *);
  55. void (*old_release_cb)(struct atm_vcc *);
  56. struct module *old_owner;
  57. /* keep old push/pop for detaching */
  58. enum pppoatm_encaps encaps;
  59. atomic_t inflight;
  60. unsigned long blocked;
  61. int flags; /* SC_COMP_PROT - compress protocol */
  62. struct ppp_channel chan; /* interface to generic ppp layer */
  63. struct tasklet_struct wakeup_tasklet;
  64. };
  65. /*
  66. * We want to allow two packets in the queue. The one that's currently in
  67. * flight, and *one* queued up ready for the ATM device to send immediately
  68. * from its TX done IRQ. We want to be able to use atomic_inc_not_zero(), so
  69. * inflight == -2 represents an empty queue, -1 one packet, and zero means
  70. * there are two packets in the queue.
  71. */
  72. #define NONE_INFLIGHT -2
  73. #define BLOCKED 0
  74. /*
  75. * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
  76. * ID (0xC021) used in autodetection
  77. */
  78. static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
  79. #define LLC_LEN (4)
  80. static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
  81. {
  82. return (struct pppoatm_vcc *) (atmvcc->user_back);
  83. }
  84. static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
  85. {
  86. return (struct pppoatm_vcc *) (chan->private);
  87. }
  88. /*
  89. * We can't do this directly from our _pop handler, since the ppp code
  90. * doesn't want to be called in interrupt context, so we do it from
  91. * a tasklet
  92. */
  93. static void pppoatm_wakeup_sender(unsigned long arg)
  94. {
  95. ppp_output_wakeup((struct ppp_channel *) arg);
  96. }
  97. static void pppoatm_release_cb(struct atm_vcc *atmvcc)
  98. {
  99. struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
  100. /*
  101. * As in pppoatm_pop(), it's safe to clear the BLOCKED bit here because
  102. * the wakeup *can't* race with pppoatm_send(). They both hold the PPP
  103. * channel's ->downl lock. And the potential race with *setting* it,
  104. * which leads to the double-check dance in pppoatm_may_send(), doesn't
  105. * exist here. In the sock_owned_by_user() case in pppoatm_send(), we
  106. * set the BLOCKED bit while the socket is still locked. We know that
  107. * ->release_cb() can't be called until that's done.
  108. */
  109. if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
  110. tasklet_schedule(&pvcc->wakeup_tasklet);
  111. if (pvcc->old_release_cb)
  112. pvcc->old_release_cb(atmvcc);
  113. }
  114. /*
  115. * This gets called every time the ATM card has finished sending our
  116. * skb. The ->old_pop will take care up normal atm flow control,
  117. * but we also need to wake up the device if we blocked it
  118. */
  119. static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
  120. {
  121. struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
  122. pvcc->old_pop(atmvcc, skb);
  123. atomic_dec(&pvcc->inflight);
  124. /*
  125. * We always used to run the wakeup tasklet unconditionally here, for
  126. * fear of race conditions where we clear the BLOCKED flag just as we
  127. * refuse another packet in pppoatm_send(). This was quite inefficient.
  128. *
  129. * In fact it's OK. The PPP core will only ever call pppoatm_send()
  130. * while holding the channel->downl lock. And ppp_output_wakeup() as
  131. * called by the tasklet will *also* grab that lock. So even if another
  132. * CPU is in pppoatm_send() right now, the tasklet isn't going to race
  133. * with it. The wakeup *will* happen after the other CPU is safely out
  134. * of pppoatm_send() again.
  135. *
  136. * So if the CPU in pppoatm_send() has already set the BLOCKED bit and
  137. * it about to return, that's fine. We trigger a wakeup which will
  138. * happen later. And if the CPU in pppoatm_send() *hasn't* set the
  139. * BLOCKED bit yet, that's fine too because of the double check in
  140. * pppoatm_may_send() which is commented there.
  141. */
  142. if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
  143. tasklet_schedule(&pvcc->wakeup_tasklet);
  144. }
  145. /*
  146. * Unbind from PPP - currently we only do this when closing the socket,
  147. * but we could put this into an ioctl if need be
  148. */
  149. static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
  150. {
  151. struct pppoatm_vcc *pvcc;
  152. pvcc = atmvcc_to_pvcc(atmvcc);
  153. atmvcc->push = pvcc->old_push;
  154. atmvcc->pop = pvcc->old_pop;
  155. atmvcc->release_cb = pvcc->old_release_cb;
  156. tasklet_kill(&pvcc->wakeup_tasklet);
  157. ppp_unregister_channel(&pvcc->chan);
  158. atmvcc->user_back = NULL;
  159. kfree(pvcc);
  160. }
  161. /* Called when an AAL5 PDU comes in */
  162. static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
  163. {
  164. struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
  165. pr_debug("\n");
  166. if (skb == NULL) { /* VCC was closed */
  167. struct module *module;
  168. pr_debug("removing ATMPPP VCC %p\n", pvcc);
  169. module = pvcc->old_owner;
  170. pppoatm_unassign_vcc(atmvcc);
  171. atmvcc->push(atmvcc, NULL); /* Pass along bad news */
  172. module_put(module);
  173. return;
  174. }
  175. atm_return(atmvcc, skb->truesize);
  176. switch (pvcc->encaps) {
  177. case e_llc:
  178. if (skb->len < LLC_LEN ||
  179. memcmp(skb->data, pppllc, LLC_LEN))
  180. goto error;
  181. skb_pull(skb, LLC_LEN);
  182. break;
  183. case e_autodetect:
  184. if (pvcc->chan.ppp == NULL) { /* Not bound yet! */
  185. kfree_skb(skb);
  186. return;
  187. }
  188. if (skb->len >= sizeof(pppllc) &&
  189. !memcmp(skb->data, pppllc, sizeof(pppllc))) {
  190. pvcc->encaps = e_llc;
  191. skb_pull(skb, LLC_LEN);
  192. break;
  193. }
  194. if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
  195. !memcmp(skb->data, &pppllc[LLC_LEN],
  196. sizeof(pppllc) - LLC_LEN)) {
  197. pvcc->encaps = e_vc;
  198. pvcc->chan.mtu += LLC_LEN;
  199. break;
  200. }
  201. pr_debug("Couldn't autodetect yet (skb: %02X %02X %02X %02X %02X %02X)\n",
  202. skb->data[0], skb->data[1], skb->data[2],
  203. skb->data[3], skb->data[4], skb->data[5]);
  204. goto error;
  205. case e_vc:
  206. break;
  207. }
  208. ppp_input(&pvcc->chan, skb);
  209. return;
  210. error:
  211. kfree_skb(skb);
  212. ppp_input_error(&pvcc->chan, 0);
  213. }
  214. static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
  215. {
  216. /*
  217. * It's not clear that we need to bother with using atm_may_send()
  218. * to check we don't exceed sk->sk_sndbuf. If userspace sets a
  219. * value of sk_sndbuf which is lower than the MTU, we're going to
  220. * block for ever. But the code always did that before we introduced
  221. * the packet count limit, so...
  222. */
  223. if (atm_may_send(pvcc->atmvcc, size) &&
  224. atomic_inc_not_zero_hint(&pvcc->inflight, NONE_INFLIGHT))
  225. return 1;
  226. /*
  227. * We use test_and_set_bit() rather than set_bit() here because
  228. * we need to ensure there's a memory barrier after it. The bit
  229. * *must* be set before we do the atomic_inc() on pvcc->inflight.
  230. * There's no smp_mb__after_set_bit(), so it's this or abuse
  231. * smp_mb__after_atomic().
  232. */
  233. test_and_set_bit(BLOCKED, &pvcc->blocked);
  234. /*
  235. * We may have raced with pppoatm_pop(). If it ran for the
  236. * last packet in the queue, *just* before we set the BLOCKED
  237. * bit, then it might never run again and the channel could
  238. * remain permanently blocked. Cope with that race by checking
  239. * *again*. If it did run in that window, we'll have space on
  240. * the queue now and can return success. It's harmless to leave
  241. * the BLOCKED flag set, since it's only used as a trigger to
  242. * run the wakeup tasklet. Another wakeup will never hurt.
  243. * If pppoatm_pop() is running but hasn't got as far as making
  244. * space on the queue yet, then it hasn't checked the BLOCKED
  245. * flag yet either, so we're safe in that case too. It'll issue
  246. * an "immediate" wakeup... where "immediate" actually involves
  247. * taking the PPP channel's ->downl lock, which is held by the
  248. * code path that calls pppoatm_send(), and is thus going to
  249. * wait for us to finish.
  250. */
  251. if (atm_may_send(pvcc->atmvcc, size) &&
  252. atomic_inc_not_zero(&pvcc->inflight))
  253. return 1;
  254. return 0;
  255. }
  256. /*
  257. * Called by the ppp_generic.c to send a packet - returns true if packet
  258. * was accepted. If we return false, then it's our job to call
  259. * ppp_output_wakeup(chan) when we're feeling more up to it.
  260. * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
  261. * we should really drop the packet, but the generic layer doesn't
  262. * support this yet. We just return 'DROP_PACKET' which we actually define
  263. * as success, just to be clear what we're really doing.
  264. */
  265. #define DROP_PACKET 1
  266. static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
  267. {
  268. struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
  269. struct atm_vcc *vcc;
  270. int ret;
  271. ATM_SKB(skb)->vcc = pvcc->atmvcc;
  272. pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
  273. if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
  274. (void) skb_pull(skb, 1);
  275. vcc = ATM_SKB(skb)->vcc;
  276. bh_lock_sock(sk_atm(vcc));
  277. if (sock_owned_by_user(sk_atm(vcc))) {
  278. /*
  279. * Needs to happen (and be flushed, hence test_and_) before we unlock
  280. * the socket. It needs to be seen by the time our ->release_cb gets
  281. * called.
  282. */
  283. test_and_set_bit(BLOCKED, &pvcc->blocked);
  284. goto nospace;
  285. }
  286. if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
  287. test_bit(ATM_VF_CLOSE, &vcc->flags) ||
  288. !test_bit(ATM_VF_READY, &vcc->flags)) {
  289. bh_unlock_sock(sk_atm(vcc));
  290. kfree_skb(skb);
  291. return DROP_PACKET;
  292. }
  293. switch (pvcc->encaps) { /* LLC encapsulation needed */
  294. case e_llc:
  295. if (skb_headroom(skb) < LLC_LEN) {
  296. struct sk_buff *n;
  297. n = skb_realloc_headroom(skb, LLC_LEN);
  298. if (n != NULL &&
  299. !pppoatm_may_send(pvcc, n->truesize)) {
  300. kfree_skb(n);
  301. goto nospace;
  302. }
  303. consume_skb(skb);
  304. skb = n;
  305. if (skb == NULL) {
  306. bh_unlock_sock(sk_atm(vcc));
  307. return DROP_PACKET;
  308. }
  309. } else if (!pppoatm_may_send(pvcc, skb->truesize))
  310. goto nospace;
  311. memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
  312. break;
  313. case e_vc:
  314. if (!pppoatm_may_send(pvcc, skb->truesize))
  315. goto nospace;
  316. break;
  317. case e_autodetect:
  318. bh_unlock_sock(sk_atm(vcc));
  319. pr_debug("Trying to send without setting encaps!\n");
  320. kfree_skb(skb);
  321. return 1;
  322. }
  323. atomic_add(skb->truesize, &sk_atm(ATM_SKB(skb)->vcc)->sk_wmem_alloc);
  324. ATM_SKB(skb)->atm_options = ATM_SKB(skb)->vcc->atm_options;
  325. pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
  326. skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
  327. ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
  328. ? DROP_PACKET : 1;
  329. bh_unlock_sock(sk_atm(vcc));
  330. return ret;
  331. nospace:
  332. bh_unlock_sock(sk_atm(vcc));
  333. /*
  334. * We don't have space to send this SKB now, but we might have
  335. * already applied SC_COMP_PROT compression, so may need to undo
  336. */
  337. if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
  338. skb->data[-1] == '\0')
  339. (void) skb_push(skb, 1);
  340. return 0;
  341. }
  342. /* This handles ioctls sent to the /dev/ppp interface */
  343. static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
  344. unsigned long arg)
  345. {
  346. switch (cmd) {
  347. case PPPIOCGFLAGS:
  348. return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
  349. ? -EFAULT : 0;
  350. case PPPIOCSFLAGS:
  351. return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
  352. ? -EFAULT : 0;
  353. }
  354. return -ENOTTY;
  355. }
  356. static const struct ppp_channel_ops pppoatm_ops = {
  357. .start_xmit = pppoatm_send,
  358. .ioctl = pppoatm_devppp_ioctl,
  359. };
  360. static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
  361. {
  362. struct atm_backend_ppp be;
  363. struct pppoatm_vcc *pvcc;
  364. int err;
  365. /*
  366. * Each PPPoATM instance has its own tasklet - this is just a
  367. * prototypical one used to initialize them
  368. */
  369. static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0);
  370. if (copy_from_user(&be, arg, sizeof be))
  371. return -EFAULT;
  372. if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
  373. be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
  374. return -EINVAL;
  375. pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
  376. if (pvcc == NULL)
  377. return -ENOMEM;
  378. pvcc->atmvcc = atmvcc;
  379. /* Maximum is zero, so that we can use atomic_inc_not_zero() */
  380. atomic_set(&pvcc->inflight, NONE_INFLIGHT);
  381. pvcc->old_push = atmvcc->push;
  382. pvcc->old_pop = atmvcc->pop;
  383. pvcc->old_owner = atmvcc->owner;
  384. pvcc->old_release_cb = atmvcc->release_cb;
  385. pvcc->encaps = (enum pppoatm_encaps) be.encaps;
  386. pvcc->chan.private = pvcc;
  387. pvcc->chan.ops = &pppoatm_ops;
  388. pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
  389. (be.encaps == e_vc ? 0 : LLC_LEN);
  390. pvcc->wakeup_tasklet = tasklet_proto;
  391. pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
  392. err = ppp_register_channel(&pvcc->chan);
  393. if (err != 0) {
  394. kfree(pvcc);
  395. return err;
  396. }
  397. atmvcc->user_back = pvcc;
  398. atmvcc->push = pppoatm_push;
  399. atmvcc->pop = pppoatm_pop;
  400. atmvcc->release_cb = pppoatm_release_cb;
  401. __module_get(THIS_MODULE);
  402. atmvcc->owner = THIS_MODULE;
  403. /* re-process everything received between connection setup and
  404. backend setup */
  405. vcc_process_recv_queue(atmvcc);
  406. return 0;
  407. }
  408. /*
  409. * This handles ioctls actually performed on our vcc - we must return
  410. * -ENOIOCTLCMD for any unrecognized ioctl
  411. */
  412. static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
  413. unsigned long arg)
  414. {
  415. struct atm_vcc *atmvcc = ATM_SD(sock);
  416. void __user *argp = (void __user *)arg;
  417. if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
  418. return -ENOIOCTLCMD;
  419. switch (cmd) {
  420. case ATM_SETBACKEND: {
  421. atm_backend_t b;
  422. if (get_user(b, (atm_backend_t __user *) argp))
  423. return -EFAULT;
  424. if (b != ATM_BACKEND_PPP)
  425. return -ENOIOCTLCMD;
  426. if (!capable(CAP_NET_ADMIN))
  427. return -EPERM;
  428. if (sock->state != SS_CONNECTED)
  429. return -EINVAL;
  430. return pppoatm_assign_vcc(atmvcc, argp);
  431. }
  432. case PPPIOCGCHAN:
  433. return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
  434. chan), (int __user *) argp) ? -EFAULT : 0;
  435. case PPPIOCGUNIT:
  436. return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
  437. chan), (int __user *) argp) ? -EFAULT : 0;
  438. }
  439. return -ENOIOCTLCMD;
  440. }
  441. static struct atm_ioctl pppoatm_ioctl_ops = {
  442. .owner = THIS_MODULE,
  443. .ioctl = pppoatm_ioctl,
  444. };
  445. static int __init pppoatm_init(void)
  446. {
  447. register_atm_ioctl(&pppoatm_ioctl_ops);
  448. return 0;
  449. }
  450. static void __exit pppoatm_exit(void)
  451. {
  452. deregister_atm_ioctl(&pppoatm_ioctl_ops);
  453. }
  454. module_init(pppoatm_init);
  455. module_exit(pppoatm_exit);
  456. MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
  457. MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
  458. MODULE_LICENSE("GPL");