wext-priv.c 6.8 KB

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