op-msg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Linux WiMAX
  3. * Generic messaging interface between userspace and driver/device
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. *
  23. *
  24. * This implements a direct communication channel between user space and
  25. * the driver/device, by which free form messages can be sent back and
  26. * forth.
  27. *
  28. * This is intended for device-specific features, vendor quirks, etc.
  29. *
  30. * See include/net/wimax.h
  31. *
  32. * GENERIC NETLINK ENCODING AND CAPACITY
  33. *
  34. * A destination "pipe name" is added to each message; it is up to the
  35. * drivers to assign or use those names (if using them at all).
  36. *
  37. * Messages are encoded as a binary netlink attribute using nla_put()
  38. * using type NLA_UNSPEC (as some versions of libnl still in
  39. * deployment don't yet understand NLA_BINARY).
  40. *
  41. * The maximum capacity of this transport is PAGESIZE per message (so
  42. * the actual payload will be bit smaller depending on the
  43. * netlink/generic netlink attributes and headers).
  44. *
  45. * RECEPTION OF MESSAGES
  46. *
  47. * When a message is received from user space, it is passed verbatim
  48. * to the driver calling wimax_dev->op_msg_from_user(). The return
  49. * value from this function is passed back to user space as an ack
  50. * over the generic netlink protocol.
  51. *
  52. * The stack doesn't do any processing or interpretation of these
  53. * messages.
  54. *
  55. * SENDING MESSAGES
  56. *
  57. * Messages can be sent with wimax_msg().
  58. *
  59. * If the message delivery needs to happen on a different context to
  60. * that of its creation, wimax_msg_alloc() can be used to get a
  61. * pointer to the message that can be delivered later on with
  62. * wimax_msg_send().
  63. *
  64. * ROADMAP
  65. *
  66. * wimax_gnl_doit_msg_from_user() Process a message from user space
  67. * wimax_dev_get_by_genl_info()
  68. * wimax_dev->op_msg_from_user() Delivery of message to the driver
  69. *
  70. * wimax_msg() Send a message to user space
  71. * wimax_msg_alloc()
  72. * wimax_msg_send()
  73. */
  74. #include <linux/device.h>
  75. #include <net/genetlink.h>
  76. #include <linux/netdevice.h>
  77. #include <linux/wimax.h>
  78. #include <linux/security.h>
  79. #include "wimax-internal.h"
  80. #define D_SUBMODULE op_msg
  81. #include "debug-levels.h"
  82. /**
  83. * wimax_msg_alloc - Create a new skb for sending a message to userspace
  84. *
  85. * @wimax_dev: WiMAX device descriptor
  86. * @pipe_name: "named pipe" the message will be sent to
  87. * @msg: pointer to the message data to send
  88. * @size: size of the message to send (in bytes), including the header.
  89. * @gfp_flags: flags for memory allocation.
  90. *
  91. * Returns: %0 if ok, negative errno code on error
  92. *
  93. * Description:
  94. *
  95. * Allocates an skb that will contain the message to send to user
  96. * space over the messaging pipe and initializes it, copying the
  97. * payload.
  98. *
  99. * Once this call is done, you can deliver it with
  100. * wimax_msg_send().
  101. *
  102. * IMPORTANT:
  103. *
  104. * Don't use skb_push()/skb_pull()/skb_reserve() on the skb, as
  105. * wimax_msg_send() depends on skb->data being placed at the
  106. * beginning of the user message.
  107. */
  108. struct sk_buff *wimax_msg_alloc(struct wimax_dev *wimax_dev,
  109. const char *pipe_name,
  110. const void *msg, size_t size,
  111. gfp_t gfp_flags)
  112. {
  113. int result;
  114. struct device *dev = wimax_dev->net_dev->dev.parent;
  115. size_t msg_size;
  116. void *genl_msg;
  117. struct sk_buff *skb;
  118. msg_size = nla_total_size(size)
  119. + nla_total_size(sizeof(u32))
  120. + (pipe_name ? nla_total_size(strlen(pipe_name)) : 0);
  121. result = -ENOMEM;
  122. skb = genlmsg_new(msg_size, gfp_flags);
  123. if (skb == NULL)
  124. goto error_new;
  125. genl_msg = genlmsg_put(skb, 0, 0, &wimax_gnl_family,
  126. 0, WIMAX_GNL_OP_MSG_TO_USER);
  127. if (genl_msg == NULL) {
  128. dev_err(dev, "no memory to create generic netlink message\n");
  129. goto error_genlmsg_put;
  130. }
  131. result = nla_put_u32(skb, WIMAX_GNL_MSG_IFIDX,
  132. wimax_dev->net_dev->ifindex);
  133. if (result < 0) {
  134. dev_err(dev, "no memory to add ifindex attribute\n");
  135. goto error_nla_put;
  136. }
  137. if (pipe_name) {
  138. result = nla_put_string(skb, WIMAX_GNL_MSG_PIPE_NAME,
  139. pipe_name);
  140. if (result < 0) {
  141. dev_err(dev, "no memory to add pipe_name attribute\n");
  142. goto error_nla_put;
  143. }
  144. }
  145. result = nla_put(skb, WIMAX_GNL_MSG_DATA, size, msg);
  146. if (result < 0) {
  147. dev_err(dev, "no memory to add payload in attribute\n");
  148. goto error_nla_put;
  149. }
  150. genlmsg_end(skb, genl_msg);
  151. return skb;
  152. error_nla_put:
  153. error_genlmsg_put:
  154. error_new:
  155. nlmsg_free(skb);
  156. return ERR_PTR(result);
  157. }
  158. EXPORT_SYMBOL_GPL(wimax_msg_alloc);
  159. /**
  160. * wimax_msg_data_len - Return a pointer and size of a message's payload
  161. *
  162. * @msg: Pointer to a message created with wimax_msg_alloc()
  163. * @size: Pointer to where to store the message's size
  164. *
  165. * Returns the pointer to the message data.
  166. */
  167. const void *wimax_msg_data_len(struct sk_buff *msg, size_t *size)
  168. {
  169. struct nlmsghdr *nlh = (void *) msg->head;
  170. struct nlattr *nla;
  171. nla = nlmsg_find_attr(nlh, sizeof(struct genlmsghdr),
  172. WIMAX_GNL_MSG_DATA);
  173. if (nla == NULL) {
  174. printk(KERN_ERR "Cannot find attribute WIMAX_GNL_MSG_DATA\n");
  175. return NULL;
  176. }
  177. *size = nla_len(nla);
  178. return nla_data(nla);
  179. }
  180. EXPORT_SYMBOL_GPL(wimax_msg_data_len);
  181. /**
  182. * wimax_msg_data - Return a pointer to a message's payload
  183. *
  184. * @msg: Pointer to a message created with wimax_msg_alloc()
  185. */
  186. const void *wimax_msg_data(struct sk_buff *msg)
  187. {
  188. struct nlmsghdr *nlh = (void *) msg->head;
  189. struct nlattr *nla;
  190. nla = nlmsg_find_attr(nlh, sizeof(struct genlmsghdr),
  191. WIMAX_GNL_MSG_DATA);
  192. if (nla == NULL) {
  193. printk(KERN_ERR "Cannot find attribute WIMAX_GNL_MSG_DATA\n");
  194. return NULL;
  195. }
  196. return nla_data(nla);
  197. }
  198. EXPORT_SYMBOL_GPL(wimax_msg_data);
  199. /**
  200. * wimax_msg_len - Return a message's payload length
  201. *
  202. * @msg: Pointer to a message created with wimax_msg_alloc()
  203. */
  204. ssize_t wimax_msg_len(struct sk_buff *msg)
  205. {
  206. struct nlmsghdr *nlh = (void *) msg->head;
  207. struct nlattr *nla;
  208. nla = nlmsg_find_attr(nlh, sizeof(struct genlmsghdr),
  209. WIMAX_GNL_MSG_DATA);
  210. if (nla == NULL) {
  211. printk(KERN_ERR "Cannot find attribute WIMAX_GNL_MSG_DATA\n");
  212. return -EINVAL;
  213. }
  214. return nla_len(nla);
  215. }
  216. EXPORT_SYMBOL_GPL(wimax_msg_len);
  217. /**
  218. * wimax_msg_send - Send a pre-allocated message to user space
  219. *
  220. * @wimax_dev: WiMAX device descriptor
  221. *
  222. * @skb: &struct sk_buff returned by wimax_msg_alloc(). Note the
  223. * ownership of @skb is transferred to this function.
  224. *
  225. * Returns: 0 if ok, < 0 errno code on error
  226. *
  227. * Description:
  228. *
  229. * Sends a free-form message that was preallocated with
  230. * wimax_msg_alloc() and filled up.
  231. *
  232. * Assumes that once you pass an skb to this function for sending, it
  233. * owns it and will release it when done (on success).
  234. *
  235. * IMPORTANT:
  236. *
  237. * Don't use skb_push()/skb_pull()/skb_reserve() on the skb, as
  238. * wimax_msg_send() depends on skb->data being placed at the
  239. * beginning of the user message.
  240. */
  241. int wimax_msg_send(struct wimax_dev *wimax_dev, struct sk_buff *skb)
  242. {
  243. struct device *dev = wimax_dev->net_dev->dev.parent;
  244. void *msg = skb->data;
  245. size_t size = skb->len;
  246. might_sleep();
  247. d_printf(1, dev, "CTX: wimax msg, %zu bytes\n", size);
  248. d_dump(2, dev, msg, size);
  249. genlmsg_multicast(skb, 0, wimax_gnl_mcg.id, GFP_KERNEL);
  250. d_printf(1, dev, "CTX: genl multicast done\n");
  251. return 0;
  252. }
  253. EXPORT_SYMBOL_GPL(wimax_msg_send);
  254. /**
  255. * wimax_msg - Send a message to user space
  256. *
  257. * @wimax_dev: WiMAX device descriptor (properly referenced)
  258. * @pipe_name: "named pipe" the message will be sent to
  259. * @buf: pointer to the message to send.
  260. * @size: size of the buffer pointed to by @buf (in bytes).
  261. * @gfp_flags: flags for memory allocation.
  262. *
  263. * Returns: %0 if ok, negative errno code on error.
  264. *
  265. * Description:
  266. *
  267. * Sends a free-form message to user space on the device @wimax_dev.
  268. *
  269. * NOTES:
  270. *
  271. * Once the @skb is given to this function, who will own it and will
  272. * release it when done (unless it returns error).
  273. */
  274. int wimax_msg(struct wimax_dev *wimax_dev, const char *pipe_name,
  275. const void *buf, size_t size, gfp_t gfp_flags)
  276. {
  277. int result = -ENOMEM;
  278. struct sk_buff *skb;
  279. skb = wimax_msg_alloc(wimax_dev, pipe_name, buf, size, gfp_flags);
  280. if (skb == NULL)
  281. goto error_msg_new;
  282. result = wimax_msg_send(wimax_dev, skb);
  283. error_msg_new:
  284. return result;
  285. }
  286. EXPORT_SYMBOL_GPL(wimax_msg);
  287. static const
  288. struct nla_policy wimax_gnl_msg_policy[WIMAX_GNL_ATTR_MAX + 1] = {
  289. [WIMAX_GNL_MSG_IFIDX] = {
  290. .type = NLA_U32,
  291. },
  292. [WIMAX_GNL_MSG_DATA] = {
  293. .type = NLA_UNSPEC, /* libnl doesn't grok BINARY yet */
  294. },
  295. };
  296. /*
  297. * Relays a message from user space to the driver
  298. *
  299. * The skb is passed to the driver-specific function with the netlink
  300. * and generic netlink headers already stripped.
  301. *
  302. * This call will block while handling/relaying the message.
  303. */
  304. static
  305. int wimax_gnl_doit_msg_from_user(struct sk_buff *skb, struct genl_info *info)
  306. {
  307. int result, ifindex;
  308. struct wimax_dev *wimax_dev;
  309. struct device *dev;
  310. struct nlmsghdr *nlh = info->nlhdr;
  311. char *pipe_name;
  312. void *msg_buf;
  313. size_t msg_len;
  314. might_sleep();
  315. d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
  316. result = -ENODEV;
  317. if (info->attrs[WIMAX_GNL_MSG_IFIDX] == NULL) {
  318. printk(KERN_ERR "WIMAX_GNL_MSG_FROM_USER: can't find IFIDX "
  319. "attribute\n");
  320. goto error_no_wimax_dev;
  321. }
  322. ifindex = nla_get_u32(info->attrs[WIMAX_GNL_MSG_IFIDX]);
  323. wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
  324. if (wimax_dev == NULL)
  325. goto error_no_wimax_dev;
  326. dev = wimax_dev_to_dev(wimax_dev);
  327. /* Unpack arguments */
  328. result = -EINVAL;
  329. if (info->attrs[WIMAX_GNL_MSG_DATA] == NULL) {
  330. dev_err(dev, "WIMAX_GNL_MSG_FROM_USER: can't find MSG_DATA "
  331. "attribute\n");
  332. goto error_no_data;
  333. }
  334. msg_buf = nla_data(info->attrs[WIMAX_GNL_MSG_DATA]);
  335. msg_len = nla_len(info->attrs[WIMAX_GNL_MSG_DATA]);
  336. if (info->attrs[WIMAX_GNL_MSG_PIPE_NAME] == NULL)
  337. pipe_name = NULL;
  338. else {
  339. struct nlattr *attr = info->attrs[WIMAX_GNL_MSG_PIPE_NAME];
  340. size_t attr_len = nla_len(attr);
  341. /* libnl-1.1 does not yet support NLA_NUL_STRING */
  342. result = -ENOMEM;
  343. pipe_name = kstrndup(nla_data(attr), attr_len + 1, GFP_KERNEL);
  344. if (pipe_name == NULL)
  345. goto error_alloc;
  346. pipe_name[attr_len] = 0;
  347. }
  348. mutex_lock(&wimax_dev->mutex);
  349. result = wimax_dev_is_ready(wimax_dev);
  350. if (result < 0)
  351. goto error_not_ready;
  352. result = -ENOSYS;
  353. if (wimax_dev->op_msg_from_user == NULL)
  354. goto error_noop;
  355. d_printf(1, dev,
  356. "CRX: nlmsghdr len %u type %u flags 0x%04x seq 0x%x pid %u\n",
  357. nlh->nlmsg_len, nlh->nlmsg_type, nlh->nlmsg_flags,
  358. nlh->nlmsg_seq, nlh->nlmsg_pid);
  359. d_printf(1, dev, "CRX: wimax message %zu bytes\n", msg_len);
  360. d_dump(2, dev, msg_buf, msg_len);
  361. result = wimax_dev->op_msg_from_user(wimax_dev, pipe_name,
  362. msg_buf, msg_len, info);
  363. error_noop:
  364. error_not_ready:
  365. mutex_unlock(&wimax_dev->mutex);
  366. error_alloc:
  367. kfree(pipe_name);
  368. error_no_data:
  369. dev_put(wimax_dev->net_dev);
  370. error_no_wimax_dev:
  371. d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
  372. return result;
  373. }
  374. /*
  375. * Generic Netlink glue
  376. */
  377. struct genl_ops wimax_gnl_msg_from_user = {
  378. .cmd = WIMAX_GNL_OP_MSG_FROM_USER,
  379. .flags = GENL_ADMIN_PERM,
  380. .policy = wimax_gnl_msg_policy,
  381. .doit = wimax_gnl_doit_msg_from_user,
  382. .dumpit = NULL,
  383. };