pppoatm.c 11 KB

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