pppoatm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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(struct tasklet_struct *t)
  91. {
  92. struct pppoatm_vcc *pvcc = from_tasklet(pvcc, t, wakeup_tasklet);
  93. ppp_output_wakeup(&pvcc->chan);
  94. }
  95. static void pppoatm_release_cb(struct atm_vcc *atmvcc)
  96. {
  97. struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
  98. /*
  99. * As in pppoatm_pop(), it's safe to clear the BLOCKED bit here because
  100. * the wakeup *can't* race with pppoatm_send(). They both hold the PPP
  101. * channel's ->downl lock. And the potential race with *setting* it,
  102. * which leads to the double-check dance in pppoatm_may_send(), doesn't
  103. * exist here. In the sock_owned_by_user() case in pppoatm_send(), we
  104. * set the BLOCKED bit while the socket is still locked. We know that
  105. * ->release_cb() can't be called until that's done.
  106. */
  107. if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
  108. tasklet_schedule(&pvcc->wakeup_tasklet);
  109. if (pvcc->old_release_cb)
  110. pvcc->old_release_cb(atmvcc);
  111. }
  112. /*
  113. * This gets called every time the ATM card has finished sending our
  114. * skb. The ->old_pop will take care up normal atm flow control,
  115. * but we also need to wake up the device if we blocked it
  116. */
  117. static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
  118. {
  119. struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
  120. pvcc->old_pop(atmvcc, skb);
  121. atomic_dec(&pvcc->inflight);
  122. /*
  123. * We always used to run the wakeup tasklet unconditionally here, for
  124. * fear of race conditions where we clear the BLOCKED flag just as we
  125. * refuse another packet in pppoatm_send(). This was quite inefficient.
  126. *
  127. * In fact it's OK. The PPP core will only ever call pppoatm_send()
  128. * while holding the channel->downl lock. And ppp_output_wakeup() as
  129. * called by the tasklet will *also* grab that lock. So even if another
  130. * CPU is in pppoatm_send() right now, the tasklet isn't going to race
  131. * with it. The wakeup *will* happen after the other CPU is safely out
  132. * of pppoatm_send() again.
  133. *
  134. * So if the CPU in pppoatm_send() has already set the BLOCKED bit and
  135. * it about to return, that's fine. We trigger a wakeup which will
  136. * happen later. And if the CPU in pppoatm_send() *hasn't* set the
  137. * BLOCKED bit yet, that's fine too because of the double check in
  138. * pppoatm_may_send() which is commented there.
  139. */
  140. if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
  141. tasklet_schedule(&pvcc->wakeup_tasklet);
  142. }
  143. /*
  144. * Unbind from PPP - currently we only do this when closing the socket,
  145. * but we could put this into an ioctl if need be
  146. */
  147. static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
  148. {
  149. struct pppoatm_vcc *pvcc;
  150. pvcc = atmvcc_to_pvcc(atmvcc);
  151. atmvcc->push = pvcc->old_push;
  152. atmvcc->pop = pvcc->old_pop;
  153. atmvcc->release_cb = pvcc->old_release_cb;
  154. tasklet_kill(&pvcc->wakeup_tasklet);
  155. ppp_unregister_channel(&pvcc->chan);
  156. atmvcc->user_back = NULL;
  157. kfree(pvcc);
  158. }
  159. /* Called when an AAL5 PDU comes in */
  160. static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
  161. {
  162. struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
  163. pr_debug("\n");
  164. if (skb == NULL) { /* VCC was closed */
  165. struct module *module;
  166. pr_debug("removing ATMPPP VCC %p\n", pvcc);
  167. module = pvcc->old_owner;
  168. pppoatm_unassign_vcc(atmvcc);
  169. atmvcc->push(atmvcc, NULL); /* Pass along bad news */
  170. module_put(module);
  171. return;
  172. }
  173. atm_return(atmvcc, skb->truesize);
  174. switch (pvcc->encaps) {
  175. case e_llc:
  176. if (skb->len < LLC_LEN ||
  177. memcmp(skb->data, pppllc, LLC_LEN))
  178. goto error;
  179. skb_pull(skb, LLC_LEN);
  180. break;
  181. case e_autodetect:
  182. if (pvcc->chan.ppp == NULL) { /* Not bound yet! */
  183. kfree_skb(skb);
  184. return;
  185. }
  186. if (skb->len >= sizeof(pppllc) &&
  187. !memcmp(skb->data, pppllc, sizeof(pppllc))) {
  188. pvcc->encaps = e_llc;
  189. skb_pull(skb, LLC_LEN);
  190. break;
  191. }
  192. if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
  193. !memcmp(skb->data, &pppllc[LLC_LEN],
  194. sizeof(pppllc) - LLC_LEN)) {
  195. pvcc->encaps = e_vc;
  196. pvcc->chan.mtu += LLC_LEN;
  197. break;
  198. }
  199. pr_debug("Couldn't autodetect yet (skb: %6ph)\n", skb->data);
  200. goto error;
  201. case e_vc:
  202. break;
  203. }
  204. ppp_input(&pvcc->chan, skb);
  205. return;
  206. error:
  207. kfree_skb(skb);
  208. ppp_input_error(&pvcc->chan, 0);
  209. }
  210. static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
  211. {
  212. /*
  213. * It's not clear that we need to bother with using atm_may_send()
  214. * to check we don't exceed sk->sk_sndbuf. If userspace sets a
  215. * value of sk_sndbuf which is lower than the MTU, we're going to
  216. * block for ever. But the code always did that before we introduced
  217. * the packet count limit, so...
  218. */
  219. if (atm_may_send(pvcc->atmvcc, size) &&
  220. atomic_inc_not_zero(&pvcc->inflight))
  221. return 1;
  222. /*
  223. * We use test_and_set_bit() rather than set_bit() here because
  224. * we need to ensure there's a memory barrier after it. The bit
  225. * *must* be set before we do the atomic_inc() on pvcc->inflight.
  226. * There's no smp_mb__after_set_bit(), so it's this or abuse
  227. * smp_mb__after_atomic().
  228. */
  229. test_and_set_bit(BLOCKED, &pvcc->blocked);
  230. /*
  231. * We may have raced with pppoatm_pop(). If it ran for the
  232. * last packet in the queue, *just* before we set the BLOCKED
  233. * bit, then it might never run again and the channel could
  234. * remain permanently blocked. Cope with that race by checking
  235. * *again*. If it did run in that window, we'll have space on
  236. * the queue now and can return success. It's harmless to leave
  237. * the BLOCKED flag set, since it's only used as a trigger to
  238. * run the wakeup tasklet. Another wakeup will never hurt.
  239. * If pppoatm_pop() is running but hasn't got as far as making
  240. * space on the queue yet, then it hasn't checked the BLOCKED
  241. * flag yet either, so we're safe in that case too. It'll issue
  242. * an "immediate" wakeup... where "immediate" actually involves
  243. * taking the PPP channel's ->downl lock, which is held by the
  244. * code path that calls pppoatm_send(), and is thus going to
  245. * wait for us to finish.
  246. */
  247. if (atm_may_send(pvcc->atmvcc, size) &&
  248. atomic_inc_not_zero(&pvcc->inflight))
  249. return 1;
  250. return 0;
  251. }
  252. /*
  253. * Called by the ppp_generic.c to send a packet - returns true if packet
  254. * was accepted. If we return false, then it's our job to call
  255. * ppp_output_wakeup(chan) when we're feeling more up to it.
  256. * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
  257. * we should really drop the packet, but the generic layer doesn't
  258. * support this yet. We just return 'DROP_PACKET' which we actually define
  259. * as success, just to be clear what we're really doing.
  260. */
  261. #define DROP_PACKET 1
  262. static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
  263. {
  264. struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
  265. struct atm_vcc *vcc;
  266. int ret;
  267. ATM_SKB(skb)->vcc = pvcc->atmvcc;
  268. pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
  269. if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
  270. (void) skb_pull(skb, 1);
  271. vcc = ATM_SKB(skb)->vcc;
  272. bh_lock_sock(sk_atm(vcc));
  273. if (sock_owned_by_user(sk_atm(vcc))) {
  274. /*
  275. * Needs to happen (and be flushed, hence test_and_) before we unlock
  276. * the socket. It needs to be seen by the time our ->release_cb gets
  277. * called.
  278. */
  279. test_and_set_bit(BLOCKED, &pvcc->blocked);
  280. goto nospace;
  281. }
  282. if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
  283. test_bit(ATM_VF_CLOSE, &vcc->flags) ||
  284. !test_bit(ATM_VF_READY, &vcc->flags)) {
  285. bh_unlock_sock(sk_atm(vcc));
  286. kfree_skb(skb);
  287. return DROP_PACKET;
  288. }
  289. switch (pvcc->encaps) { /* LLC encapsulation needed */
  290. case e_llc:
  291. if (skb_headroom(skb) < LLC_LEN) {
  292. struct sk_buff *n;
  293. n = skb_realloc_headroom(skb, LLC_LEN);
  294. if (n != NULL &&
  295. !pppoatm_may_send(pvcc, n->truesize)) {
  296. kfree_skb(n);
  297. goto nospace;
  298. }
  299. consume_skb(skb);
  300. skb = n;
  301. if (skb == NULL) {
  302. bh_unlock_sock(sk_atm(vcc));
  303. return DROP_PACKET;
  304. }
  305. } else if (!pppoatm_may_send(pvcc, skb->truesize))
  306. goto nospace;
  307. memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
  308. break;
  309. case e_vc:
  310. if (!pppoatm_may_send(pvcc, skb->truesize))
  311. goto nospace;
  312. break;
  313. case e_autodetect:
  314. bh_unlock_sock(sk_atm(vcc));
  315. pr_debug("Trying to send without setting encaps!\n");
  316. kfree_skb(skb);
  317. return 1;
  318. }
  319. atm_account_tx(vcc, skb);
  320. pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
  321. skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
  322. ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
  323. ? DROP_PACKET : 1;
  324. bh_unlock_sock(sk_atm(vcc));
  325. return ret;
  326. nospace:
  327. bh_unlock_sock(sk_atm(vcc));
  328. /*
  329. * We don't have space to send this SKB now, but we might have
  330. * already applied SC_COMP_PROT compression, so may need to undo
  331. */
  332. if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
  333. skb->data[-1] == '\0')
  334. (void) skb_push(skb, 1);
  335. return 0;
  336. }
  337. /* This handles ioctls sent to the /dev/ppp interface */
  338. static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
  339. unsigned long arg)
  340. {
  341. switch (cmd) {
  342. case PPPIOCGFLAGS:
  343. return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
  344. ? -EFAULT : 0;
  345. case PPPIOCSFLAGS:
  346. return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
  347. ? -EFAULT : 0;
  348. }
  349. return -ENOTTY;
  350. }
  351. static const struct ppp_channel_ops pppoatm_ops = {
  352. .start_xmit = pppoatm_send,
  353. .ioctl = pppoatm_devppp_ioctl,
  354. };
  355. static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
  356. {
  357. struct atm_backend_ppp be;
  358. struct pppoatm_vcc *pvcc;
  359. int err;
  360. if (copy_from_user(&be, arg, sizeof be))
  361. return -EFAULT;
  362. if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
  363. be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
  364. return -EINVAL;
  365. pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
  366. if (pvcc == NULL)
  367. return -ENOMEM;
  368. pvcc->atmvcc = atmvcc;
  369. /* Maximum is zero, so that we can use atomic_inc_not_zero() */
  370. atomic_set(&pvcc->inflight, NONE_INFLIGHT);
  371. pvcc->old_push = atmvcc->push;
  372. pvcc->old_pop = atmvcc->pop;
  373. pvcc->old_owner = atmvcc->owner;
  374. pvcc->old_release_cb = atmvcc->release_cb;
  375. pvcc->encaps = (enum pppoatm_encaps) be.encaps;
  376. pvcc->chan.private = pvcc;
  377. pvcc->chan.ops = &pppoatm_ops;
  378. pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
  379. (be.encaps == e_vc ? 0 : LLC_LEN);
  380. tasklet_setup(&pvcc->wakeup_tasklet, pppoatm_wakeup_sender);
  381. err = ppp_register_channel(&pvcc->chan);
  382. if (err != 0) {
  383. kfree(pvcc);
  384. return err;
  385. }
  386. atmvcc->user_back = pvcc;
  387. atmvcc->push = pppoatm_push;
  388. atmvcc->pop = pppoatm_pop;
  389. atmvcc->release_cb = pppoatm_release_cb;
  390. __module_get(THIS_MODULE);
  391. atmvcc->owner = THIS_MODULE;
  392. /* re-process everything received between connection setup and
  393. backend setup */
  394. vcc_process_recv_queue(atmvcc);
  395. return 0;
  396. }
  397. /*
  398. * This handles ioctls actually performed on our vcc - we must return
  399. * -ENOIOCTLCMD for any unrecognized ioctl
  400. */
  401. static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
  402. unsigned long arg)
  403. {
  404. struct atm_vcc *atmvcc = ATM_SD(sock);
  405. void __user *argp = (void __user *)arg;
  406. if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
  407. return -ENOIOCTLCMD;
  408. switch (cmd) {
  409. case ATM_SETBACKEND: {
  410. atm_backend_t b;
  411. if (get_user(b, (atm_backend_t __user *) argp))
  412. return -EFAULT;
  413. if (b != ATM_BACKEND_PPP)
  414. return -ENOIOCTLCMD;
  415. if (!capable(CAP_NET_ADMIN))
  416. return -EPERM;
  417. if (sock->state != SS_CONNECTED)
  418. return -EINVAL;
  419. return pppoatm_assign_vcc(atmvcc, argp);
  420. }
  421. case PPPIOCGCHAN:
  422. return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
  423. chan), (int __user *) argp) ? -EFAULT : 0;
  424. case PPPIOCGUNIT:
  425. return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
  426. chan), (int __user *) argp) ? -EFAULT : 0;
  427. }
  428. return -ENOIOCTLCMD;
  429. }
  430. static struct atm_ioctl pppoatm_ioctl_ops = {
  431. .owner = THIS_MODULE,
  432. .ioctl = pppoatm_ioctl,
  433. };
  434. static int __init pppoatm_init(void)
  435. {
  436. register_atm_ioctl(&pppoatm_ioctl_ops);
  437. return 0;
  438. }
  439. static void __exit pppoatm_exit(void)
  440. {
  441. deregister_atm_ioctl(&pppoatm_ioctl_ops);
  442. }
  443. module_init(pppoatm_init);
  444. module_exit(pppoatm_exit);
  445. MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
  446. MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
  447. MODULE_LICENSE("GPL");