pppoatm.c 11 KB

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