pppoatm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. /* keep old push/pop for detaching */
  56. enum pppoatm_encaps encaps;
  57. int flags; /* SC_COMP_PROT - compress protocol */
  58. struct ppp_channel chan; /* interface to generic ppp layer */
  59. struct tasklet_struct wakeup_tasklet;
  60. };
  61. /*
  62. * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
  63. * ID (0xC021) used in autodetection
  64. */
  65. static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
  66. #define LLC_LEN (4)
  67. static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
  68. {
  69. return (struct pppoatm_vcc *) (atmvcc->user_back);
  70. }
  71. static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
  72. {
  73. return (struct pppoatm_vcc *) (chan->private);
  74. }
  75. /*
  76. * We can't do this directly from our _pop handler, since the ppp code
  77. * doesn't want to be called in interrupt context, so we do it from
  78. * a tasklet
  79. */
  80. static void pppoatm_wakeup_sender(unsigned long arg)
  81. {
  82. ppp_output_wakeup((struct ppp_channel *) arg);
  83. }
  84. /*
  85. * This gets called every time the ATM card has finished sending our
  86. * skb. The ->old_pop will take care up normal atm flow control,
  87. * but we also need to wake up the device if we blocked it
  88. */
  89. static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
  90. {
  91. struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
  92. pvcc->old_pop(atmvcc, skb);
  93. /*
  94. * We don't really always want to do this since it's
  95. * really inefficient - it would be much better if we could
  96. * test if we had actually throttled the generic layer.
  97. * Unfortunately then there would be a nasty SMP race where
  98. * we could clear that flag just as we refuse another packet.
  99. * For now we do the safe thing.
  100. */
  101. tasklet_schedule(&pvcc->wakeup_tasklet);
  102. }
  103. /*
  104. * Unbind from PPP - currently we only do this when closing the socket,
  105. * but we could put this into an ioctl if need be
  106. */
  107. static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
  108. {
  109. struct pppoatm_vcc *pvcc;
  110. pvcc = atmvcc_to_pvcc(atmvcc);
  111. atmvcc->push = pvcc->old_push;
  112. atmvcc->pop = pvcc->old_pop;
  113. tasklet_kill(&pvcc->wakeup_tasklet);
  114. ppp_unregister_channel(&pvcc->chan);
  115. atmvcc->user_back = NULL;
  116. kfree(pvcc);
  117. /* Gee, I hope we have the big kernel lock here... */
  118. module_put(THIS_MODULE);
  119. }
  120. /* Called when an AAL5 PDU comes in */
  121. static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
  122. {
  123. struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
  124. pr_debug("\n");
  125. if (skb == NULL) { /* VCC was closed */
  126. pr_debug("removing ATMPPP VCC %p\n", pvcc);
  127. pppoatm_unassign_vcc(atmvcc);
  128. atmvcc->push(atmvcc, NULL); /* Pass along bad news */
  129. return;
  130. }
  131. atm_return(atmvcc, skb->truesize);
  132. switch (pvcc->encaps) {
  133. case e_llc:
  134. if (skb->len < LLC_LEN ||
  135. memcmp(skb->data, pppllc, LLC_LEN))
  136. goto error;
  137. skb_pull(skb, LLC_LEN);
  138. break;
  139. case e_autodetect:
  140. if (pvcc->chan.ppp == NULL) { /* Not bound yet! */
  141. kfree_skb(skb);
  142. return;
  143. }
  144. if (skb->len >= sizeof(pppllc) &&
  145. !memcmp(skb->data, pppllc, sizeof(pppllc))) {
  146. pvcc->encaps = e_llc;
  147. skb_pull(skb, LLC_LEN);
  148. break;
  149. }
  150. if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
  151. !memcmp(skb->data, &pppllc[LLC_LEN],
  152. sizeof(pppllc) - LLC_LEN)) {
  153. pvcc->encaps = e_vc;
  154. pvcc->chan.mtu += LLC_LEN;
  155. break;
  156. }
  157. pr_debug("Couldn't autodetect yet (skb: %02X %02X %02X %02X %02X %02X)\n",
  158. skb->data[0], skb->data[1], skb->data[2],
  159. skb->data[3], skb->data[4], skb->data[5]);
  160. goto error;
  161. case e_vc:
  162. break;
  163. }
  164. ppp_input(&pvcc->chan, skb);
  165. return;
  166. error:
  167. kfree_skb(skb);
  168. ppp_input_error(&pvcc->chan, 0);
  169. }
  170. /*
  171. * Called by the ppp_generic.c to send a packet - returns true if packet
  172. * was accepted. If we return false, then it's our job to call
  173. * ppp_output_wakeup(chan) when we're feeling more up to it.
  174. * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
  175. * we should really drop the packet, but the generic layer doesn't
  176. * support this yet. We just return 'DROP_PACKET' which we actually define
  177. * as success, just to be clear what we're really doing.
  178. */
  179. #define DROP_PACKET 1
  180. static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
  181. {
  182. struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
  183. ATM_SKB(skb)->vcc = pvcc->atmvcc;
  184. pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
  185. if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
  186. (void) skb_pull(skb, 1);
  187. switch (pvcc->encaps) { /* LLC encapsulation needed */
  188. case e_llc:
  189. if (skb_headroom(skb) < LLC_LEN) {
  190. struct sk_buff *n;
  191. n = skb_realloc_headroom(skb, LLC_LEN);
  192. if (n != NULL &&
  193. !atm_may_send(pvcc->atmvcc, n->truesize)) {
  194. kfree_skb(n);
  195. goto nospace;
  196. }
  197. kfree_skb(skb);
  198. skb = n;
  199. if (skb == NULL)
  200. return DROP_PACKET;
  201. } else if (!atm_may_send(pvcc->atmvcc, skb->truesize))
  202. goto nospace;
  203. memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
  204. break;
  205. case e_vc:
  206. if (!atm_may_send(pvcc->atmvcc, skb->truesize))
  207. goto nospace;
  208. break;
  209. case e_autodetect:
  210. pr_debug("Trying to send without setting encaps!\n");
  211. kfree_skb(skb);
  212. return 1;
  213. }
  214. atomic_add(skb->truesize, &sk_atm(ATM_SKB(skb)->vcc)->sk_wmem_alloc);
  215. ATM_SKB(skb)->atm_options = ATM_SKB(skb)->vcc->atm_options;
  216. pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
  217. skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
  218. return ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
  219. ? DROP_PACKET : 1;
  220. nospace:
  221. /*
  222. * We don't have space to send this SKB now, but we might have
  223. * already applied SC_COMP_PROT compression, so may need to undo
  224. */
  225. if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
  226. skb->data[-1] == '\0')
  227. (void) skb_push(skb, 1);
  228. return 0;
  229. }
  230. /* This handles ioctls sent to the /dev/ppp interface */
  231. static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
  232. unsigned long arg)
  233. {
  234. switch (cmd) {
  235. case PPPIOCGFLAGS:
  236. return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
  237. ? -EFAULT : 0;
  238. case PPPIOCSFLAGS:
  239. return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
  240. ? -EFAULT : 0;
  241. }
  242. return -ENOTTY;
  243. }
  244. static const struct ppp_channel_ops pppoatm_ops = {
  245. .start_xmit = pppoatm_send,
  246. .ioctl = pppoatm_devppp_ioctl,
  247. };
  248. static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
  249. {
  250. struct atm_backend_ppp be;
  251. struct pppoatm_vcc *pvcc;
  252. int err;
  253. /*
  254. * Each PPPoATM instance has its own tasklet - this is just a
  255. * prototypical one used to initialize them
  256. */
  257. static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0);
  258. if (copy_from_user(&be, arg, sizeof be))
  259. return -EFAULT;
  260. if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
  261. be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
  262. return -EINVAL;
  263. pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
  264. if (pvcc == NULL)
  265. return -ENOMEM;
  266. pvcc->atmvcc = atmvcc;
  267. pvcc->old_push = atmvcc->push;
  268. pvcc->old_pop = atmvcc->pop;
  269. pvcc->encaps = (enum pppoatm_encaps) be.encaps;
  270. pvcc->chan.private = pvcc;
  271. pvcc->chan.ops = &pppoatm_ops;
  272. pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
  273. (be.encaps == e_vc ? 0 : LLC_LEN);
  274. pvcc->wakeup_tasklet = tasklet_proto;
  275. pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
  276. err = ppp_register_channel(&pvcc->chan);
  277. if (err != 0) {
  278. kfree(pvcc);
  279. return err;
  280. }
  281. atmvcc->user_back = pvcc;
  282. atmvcc->push = pppoatm_push;
  283. atmvcc->pop = pppoatm_pop;
  284. __module_get(THIS_MODULE);
  285. /* re-process everything received between connection setup and
  286. backend setup */
  287. vcc_process_recv_queue(atmvcc);
  288. return 0;
  289. }
  290. /*
  291. * This handles ioctls actually performed on our vcc - we must return
  292. * -ENOIOCTLCMD for any unrecognized ioctl
  293. */
  294. static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
  295. unsigned long arg)
  296. {
  297. struct atm_vcc *atmvcc = ATM_SD(sock);
  298. void __user *argp = (void __user *)arg;
  299. if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
  300. return -ENOIOCTLCMD;
  301. switch (cmd) {
  302. case ATM_SETBACKEND: {
  303. atm_backend_t b;
  304. if (get_user(b, (atm_backend_t __user *) argp))
  305. return -EFAULT;
  306. if (b != ATM_BACKEND_PPP)
  307. return -ENOIOCTLCMD;
  308. if (!capable(CAP_NET_ADMIN))
  309. return -EPERM;
  310. return pppoatm_assign_vcc(atmvcc, argp);
  311. }
  312. case PPPIOCGCHAN:
  313. return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
  314. chan), (int __user *) argp) ? -EFAULT : 0;
  315. case PPPIOCGUNIT:
  316. return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
  317. chan), (int __user *) argp) ? -EFAULT : 0;
  318. }
  319. return -ENOIOCTLCMD;
  320. }
  321. static struct atm_ioctl pppoatm_ioctl_ops = {
  322. .owner = THIS_MODULE,
  323. .ioctl = pppoatm_ioctl,
  324. };
  325. static int __init pppoatm_init(void)
  326. {
  327. register_atm_ioctl(&pppoatm_ioctl_ops);
  328. return 0;
  329. }
  330. static void __exit pppoatm_exit(void)
  331. {
  332. deregister_atm_ioctl(&pppoatm_ioctl_ops);
  333. }
  334. module_init(pppoatm_init);
  335. module_exit(pppoatm_exit);
  336. MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
  337. MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
  338. MODULE_LICENSE("GPL");