/net/atm/pppoatm.c

http://github.com/mirrors/linux · C · 494 lines · 309 code · 42 blank · 143 comment · 53 complexity · 7a7a62083dadccdec8c57757d00fb05c MD5 · raw file

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