wext-priv.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * This file implement the Wireless Extensions priv API.
  3. *
  4. * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
  5. * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
  6. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * (As all part of the Linux kernel, this file is GPL)
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/wireless.h>
  12. #include <linux/netdevice.h>
  13. #include <net/iw_handler.h>
  14. #include <net/wext.h>
  15. int iw_handler_get_private(struct net_device * dev,
  16. struct iw_request_info * info,
  17. union iwreq_data * wrqu,
  18. char * extra)
  19. {
  20. /* Check if the driver has something to export */
  21. if ((dev->wireless_handlers->num_private_args == 0) ||
  22. (dev->wireless_handlers->private_args == NULL))
  23. return -EOPNOTSUPP;
  24. /* Check if there is enough buffer up there */
  25. if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
  26. /* User space can't know in advance how large the buffer
  27. * needs to be. Give it a hint, so that we can support
  28. * any size buffer we want somewhat efficiently... */
  29. wrqu->data.length = dev->wireless_handlers->num_private_args;
  30. return -E2BIG;
  31. }
  32. /* Set the number of available ioctls. */
  33. wrqu->data.length = dev->wireless_handlers->num_private_args;
  34. /* Copy structure to the user buffer. */
  35. memcpy(extra, dev->wireless_handlers->private_args,
  36. sizeof(struct iw_priv_args) * wrqu->data.length);
  37. return 0;
  38. }
  39. /* Size (in bytes) of the various private data types */
  40. static const char iw_priv_type_size[] = {
  41. 0, /* IW_PRIV_TYPE_NONE */
  42. 1, /* IW_PRIV_TYPE_BYTE */
  43. 1, /* IW_PRIV_TYPE_CHAR */
  44. 0, /* Not defined */
  45. sizeof(__u32), /* IW_PRIV_TYPE_INT */
  46. sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
  47. sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
  48. 0, /* Not defined */
  49. };
  50. static int get_priv_size(__u16 args)
  51. {
  52. int num = args & IW_PRIV_SIZE_MASK;
  53. int type = (args & IW_PRIV_TYPE_MASK) >> 12;
  54. return num * iw_priv_type_size[type];
  55. }
  56. static int adjust_priv_size(__u16 args, struct iw_point *iwp)
  57. {
  58. int num = iwp->length;
  59. int max = args & IW_PRIV_SIZE_MASK;
  60. int type = (args & IW_PRIV_TYPE_MASK) >> 12;
  61. /* Make sure the driver doesn't goof up */
  62. if (max < num)
  63. num = max;
  64. return num * iw_priv_type_size[type];
  65. }
  66. /*
  67. * Wrapper to call a private Wireless Extension handler.
  68. * We do various checks and also take care of moving data between
  69. * user space and kernel space.
  70. * It's not as nice and slimline as the standard wrapper. The cause
  71. * is struct iw_priv_args, which was not really designed for the
  72. * job we are going here.
  73. *
  74. * IMPORTANT : This function prevent to set and get data on the same
  75. * IOCTL and enforce the SET/GET convention. Not doing it would be
  76. * far too hairy...
  77. * If you need to set and get data at the same time, please don't use
  78. * a iw_handler but process it in your ioctl handler (i.e. use the
  79. * old driver API).
  80. */
  81. static int get_priv_descr_and_size(struct net_device *dev, unsigned int cmd,
  82. const struct iw_priv_args **descrp)
  83. {
  84. const struct iw_priv_args *descr;
  85. int i, extra_size;
  86. descr = NULL;
  87. for (i = 0; i < dev->wireless_handlers->num_private_args; i++) {
  88. if (cmd == dev->wireless_handlers->private_args[i].cmd) {
  89. descr = &dev->wireless_handlers->private_args[i];
  90. break;
  91. }
  92. }
  93. extra_size = 0;
  94. if (descr) {
  95. if (IW_IS_SET(cmd)) {
  96. int offset = 0; /* For sub-ioctls */
  97. /* Check for sub-ioctl handler */
  98. if (descr->name[0] == '\0')
  99. /* Reserve one int for sub-ioctl index */
  100. offset = sizeof(__u32);
  101. /* Size of set arguments */
  102. extra_size = get_priv_size(descr->set_args);
  103. /* Does it fits in iwr ? */
  104. if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
  105. ((extra_size + offset) <= IFNAMSIZ))
  106. extra_size = 0;
  107. } else {
  108. /* Size of get arguments */
  109. extra_size = get_priv_size(descr->get_args);
  110. /* Does it fits in iwr ? */
  111. if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
  112. (extra_size <= IFNAMSIZ))
  113. extra_size = 0;
  114. }
  115. }
  116. *descrp = descr;
  117. return extra_size;
  118. }
  119. static int ioctl_private_iw_point(struct iw_point *iwp, unsigned int cmd,
  120. const struct iw_priv_args *descr,
  121. iw_handler handler, struct net_device *dev,
  122. struct iw_request_info *info, int extra_size)
  123. {
  124. char *extra;
  125. int err;
  126. /* Check what user space is giving us */
  127. if (IW_IS_SET(cmd)) {
  128. if (!iwp->pointer && iwp->length != 0)
  129. return -EFAULT;
  130. if (iwp->length > (descr->set_args & IW_PRIV_SIZE_MASK))
  131. return -E2BIG;
  132. } else if (!iwp->pointer)
  133. return -EFAULT;
  134. extra = kzalloc(extra_size, GFP_KERNEL);
  135. if (!extra)
  136. return -ENOMEM;
  137. /* If it is a SET, get all the extra data in here */
  138. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  139. if (copy_from_user(extra, iwp->pointer, extra_size)) {
  140. err = -EFAULT;
  141. goto out;
  142. }
  143. }
  144. /* Call the handler */
  145. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  146. /* If we have something to return to the user */
  147. if (!err && IW_IS_GET(cmd)) {
  148. /* Adjust for the actual length if it's variable,
  149. * avoid leaking kernel bits outside.
  150. */
  151. if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
  152. extra_size = adjust_priv_size(descr->get_args, iwp);
  153. if (copy_to_user(iwp->pointer, extra, extra_size))
  154. err = -EFAULT;
  155. }
  156. out:
  157. kfree(extra);
  158. return err;
  159. }
  160. int ioctl_private_call(struct net_device *dev, struct iwreq *iwr,
  161. unsigned int cmd, struct iw_request_info *info,
  162. iw_handler handler)
  163. {
  164. int extra_size = 0, ret = -EINVAL;
  165. const struct iw_priv_args *descr;
  166. extra_size = get_priv_descr_and_size(dev, cmd, &descr);
  167. /* Check if we have a pointer to user space data or not. */
  168. if (extra_size == 0) {
  169. /* No extra arguments. Trivial to handle */
  170. ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
  171. } else {
  172. ret = ioctl_private_iw_point(&iwr->u.data, cmd, descr,
  173. handler, dev, info, extra_size);
  174. }
  175. /* Call commit handler if needed and defined */
  176. if (ret == -EIWCOMMIT)
  177. ret = call_commit_handler(dev);
  178. return ret;
  179. }
  180. #ifdef CONFIG_COMPAT
  181. int compat_private_call(struct net_device *dev, struct iwreq *iwr,
  182. unsigned int cmd, struct iw_request_info *info,
  183. iw_handler handler)
  184. {
  185. const struct iw_priv_args *descr;
  186. int ret, extra_size;
  187. extra_size = get_priv_descr_and_size(dev, cmd, &descr);
  188. /* Check if we have a pointer to user space data or not. */
  189. if (extra_size == 0) {
  190. /* No extra arguments. Trivial to handle */
  191. ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
  192. } else {
  193. struct compat_iw_point *iwp_compat;
  194. struct iw_point iwp;
  195. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  196. iwp.pointer = compat_ptr(iwp_compat->pointer);
  197. iwp.length = iwp_compat->length;
  198. iwp.flags = iwp_compat->flags;
  199. ret = ioctl_private_iw_point(&iwp, cmd, descr,
  200. handler, dev, info, extra_size);
  201. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  202. iwp_compat->length = iwp.length;
  203. iwp_compat->flags = iwp.flags;
  204. }
  205. /* Call commit handler if needed and defined */
  206. if (ret == -EIWCOMMIT)
  207. ret = call_commit_handler(dev);
  208. return ret;
  209. }
  210. #endif