w1_netlink.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * w1_netlink.c
  3. *
  4. * Copyright (c) 2003 Evgeniy Polyakov <zbr@ioremap.net>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/slab.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/netlink.h>
  24. #include <linux/connector.h>
  25. #include "w1.h"
  26. #include "w1_log.h"
  27. #include "w1_netlink.h"
  28. #if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
  29. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  30. /* Bundle together everything required to process a request in one memory
  31. * allocation.
  32. */
  33. struct w1_cb_block {
  34. atomic_t refcnt;
  35. u32 portid; /* Sending process port ID */
  36. /* maximum value for first_cn->len */
  37. u16 maxlen;
  38. /* pointers to building up the reply message */
  39. struct cn_msg *first_cn; /* fixed once the structure is populated */
  40. struct cn_msg *cn; /* advances as cn_msg is appeneded */
  41. struct w1_netlink_msg *msg; /* advances as w1_netlink_msg is appened */
  42. struct w1_netlink_cmd *cmd; /* advances as cmds are appened */
  43. struct w1_netlink_msg *cur_msg; /* currently message being processed */
  44. /* copy of the original request follows */
  45. struct cn_msg request_cn;
  46. /* followed by variable length:
  47. * cn_msg, data (w1_netlink_msg and w1_netlink_cmd)
  48. * one or more struct w1_cb_node
  49. * reply first_cn, data (w1_netlink_msg and w1_netlink_cmd)
  50. */
  51. };
  52. struct w1_cb_node {
  53. struct w1_async_cmd async;
  54. /* pointers within w1_cb_block and cn data */
  55. struct w1_cb_block *block;
  56. struct w1_netlink_msg *msg;
  57. struct w1_slave *sl;
  58. struct w1_master *dev;
  59. };
  60. /**
  61. * w1_reply_len() - calculate current reply length, compare to maxlen
  62. * @block: block to calculate
  63. *
  64. * Calculates the current message length including possible multiple
  65. * cn_msg and data, excludes the first sizeof(struct cn_msg). Direclty
  66. * compariable to maxlen and usable to send the message.
  67. */
  68. static u16 w1_reply_len(struct w1_cb_block *block)
  69. {
  70. if (!block->cn)
  71. return 0;
  72. return (u8 *)block->cn - (u8 *)block->first_cn + block->cn->len;
  73. }
  74. static void w1_unref_block(struct w1_cb_block *block)
  75. {
  76. if (atomic_sub_return(1, &block->refcnt) == 0) {
  77. u16 len = w1_reply_len(block);
  78. if (len) {
  79. cn_netlink_send_mult(block->first_cn, len,
  80. block->portid, 0, GFP_KERNEL);
  81. }
  82. kfree(block);
  83. }
  84. }
  85. /**
  86. * w1_reply_make_space() - send message if needed to make space
  87. * @block: block to make space on
  88. * @space: how many bytes requested
  89. *
  90. * Verify there is enough room left for the caller to add "space" bytes to the
  91. * message, if there isn't send the message and reset.
  92. */
  93. static void w1_reply_make_space(struct w1_cb_block *block, u16 space)
  94. {
  95. u16 len = w1_reply_len(block);
  96. if (len + space >= block->maxlen) {
  97. cn_netlink_send_mult(block->first_cn, len, block->portid, 0, GFP_KERNEL);
  98. block->first_cn->len = 0;
  99. block->cn = NULL;
  100. block->msg = NULL;
  101. block->cmd = NULL;
  102. }
  103. }
  104. /* Early send when replies aren't bundled. */
  105. static void w1_netlink_check_send(struct w1_cb_block *block)
  106. {
  107. if (!(block->request_cn.flags & W1_CN_BUNDLE) && block->cn)
  108. w1_reply_make_space(block, block->maxlen);
  109. }
  110. /**
  111. * w1_netlink_setup_msg() - prepare to write block->msg
  112. * @block: block to operate on
  113. * @ack: determines if cn can be reused
  114. *
  115. * block->cn will be setup with the correct ack, advancing if needed
  116. * block->cn->len does not include space for block->msg
  117. * block->msg advances but remains uninitialized
  118. */
  119. static void w1_netlink_setup_msg(struct w1_cb_block *block, u32 ack)
  120. {
  121. if (block->cn && block->cn->ack == ack) {
  122. block->msg = (struct w1_netlink_msg *)(block->cn->data + block->cn->len);
  123. } else {
  124. /* advance or set to data */
  125. if (block->cn)
  126. block->cn = (struct cn_msg *)(block->cn->data +
  127. block->cn->len);
  128. else
  129. block->cn = block->first_cn;
  130. memcpy(block->cn, &block->request_cn, sizeof(*block->cn));
  131. block->cn->len = 0;
  132. block->cn->ack = ack;
  133. block->msg = (struct w1_netlink_msg *)block->cn->data;
  134. }
  135. }
  136. /* Append cmd to msg, include cmd->data as well. This is because
  137. * any following data goes with the command and in the case of a read is
  138. * the results.
  139. */
  140. static void w1_netlink_queue_cmd(struct w1_cb_block *block,
  141. struct w1_netlink_cmd *cmd)
  142. {
  143. u32 space;
  144. w1_reply_make_space(block, sizeof(struct cn_msg) +
  145. sizeof(struct w1_netlink_msg) + sizeof(*cmd) + cmd->len);
  146. /* There's a status message sent after each command, so no point
  147. * in trying to bundle this cmd after an existing one, because
  148. * there won't be one. Allocate and copy over a new cn_msg.
  149. */
  150. w1_netlink_setup_msg(block, block->request_cn.seq + 1);
  151. memcpy(block->msg, block->cur_msg, sizeof(*block->msg));
  152. block->cn->len += sizeof(*block->msg);
  153. block->msg->len = 0;
  154. block->cmd = (struct w1_netlink_cmd *)(block->msg->data);
  155. space = sizeof(*cmd) + cmd->len;
  156. if (block->cmd != cmd)
  157. memcpy(block->cmd, cmd, space);
  158. block->cn->len += space;
  159. block->msg->len += space;
  160. }
  161. /* Append req_msg and req_cmd, no other commands and no data from req_cmd are
  162. * copied.
  163. */
  164. static void w1_netlink_queue_status(struct w1_cb_block *block,
  165. struct w1_netlink_msg *req_msg, struct w1_netlink_cmd *req_cmd,
  166. int error)
  167. {
  168. u16 space = sizeof(struct cn_msg) + sizeof(*req_msg) + sizeof(*req_cmd);
  169. w1_reply_make_space(block, space);
  170. w1_netlink_setup_msg(block, block->request_cn.ack);
  171. memcpy(block->msg, req_msg, sizeof(*req_msg));
  172. block->cn->len += sizeof(*req_msg);
  173. block->msg->len = 0;
  174. block->msg->status = (u8)-error;
  175. if (req_cmd) {
  176. struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)block->msg->data;
  177. memcpy(cmd, req_cmd, sizeof(*cmd));
  178. block->cn->len += sizeof(*cmd);
  179. block->msg->len += sizeof(*cmd);
  180. cmd->len = 0;
  181. }
  182. w1_netlink_check_send(block);
  183. }
  184. /**
  185. * w1_netlink_send_error() - sends the error message now
  186. * @cn: original cn_msg
  187. * @msg: original w1_netlink_msg
  188. * @portid: where to send it
  189. * @error: error status
  190. *
  191. * Use when a block isn't available to queue the message to and cn, msg
  192. * might not be contiguous.
  193. */
  194. static void w1_netlink_send_error(struct cn_msg *cn, struct w1_netlink_msg *msg,
  195. int portid, int error)
  196. {
  197. struct {
  198. struct cn_msg cn;
  199. struct w1_netlink_msg msg;
  200. } packet;
  201. memcpy(&packet.cn, cn, sizeof(packet.cn));
  202. memcpy(&packet.msg, msg, sizeof(packet.msg));
  203. packet.cn.len = sizeof(packet.msg);
  204. packet.msg.len = 0;
  205. packet.msg.status = (u8)-error;
  206. cn_netlink_send(&packet.cn, portid, 0, GFP_KERNEL);
  207. }
  208. /**
  209. * w1_netlink_send() - sends w1 netlink notifications
  210. * @dev: w1_master the even is associated with or for
  211. * @msg: w1_netlink_msg message to be sent
  212. *
  213. * This are notifications generated from the kernel.
  214. */
  215. void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
  216. {
  217. struct {
  218. struct cn_msg cn;
  219. struct w1_netlink_msg msg;
  220. } packet;
  221. memset(&packet, 0, sizeof(packet));
  222. packet.cn.id.idx = CN_W1_IDX;
  223. packet.cn.id.val = CN_W1_VAL;
  224. packet.cn.seq = dev->seq++;
  225. packet.cn.len = sizeof(*msg);
  226. memcpy(&packet.msg, msg, sizeof(*msg));
  227. packet.msg.len = 0;
  228. cn_netlink_send(&packet.cn, 0, 0, GFP_KERNEL);
  229. }
  230. static void w1_send_slave(struct w1_master *dev, u64 rn)
  231. {
  232. struct w1_cb_block *block = dev->priv;
  233. struct w1_netlink_cmd *cache_cmd = block->cmd;
  234. u64 *data;
  235. w1_reply_make_space(block, sizeof(*data));
  236. /* Add cmd back if the packet was sent */
  237. if (!block->cmd) {
  238. cache_cmd->len = 0;
  239. w1_netlink_queue_cmd(block, cache_cmd);
  240. }
  241. data = (u64 *)(block->cmd->data + block->cmd->len);
  242. *data = rn;
  243. block->cn->len += sizeof(*data);
  244. block->msg->len += sizeof(*data);
  245. block->cmd->len += sizeof(*data);
  246. }
  247. static void w1_found_send_slave(struct w1_master *dev, u64 rn)
  248. {
  249. /* update kernel slave list */
  250. w1_slave_found(dev, rn);
  251. w1_send_slave(dev, rn);
  252. }
  253. /* Get the current slave list, or search (with or without alarm) */
  254. static int w1_get_slaves(struct w1_master *dev, struct w1_netlink_cmd *req_cmd)
  255. {
  256. struct w1_slave *sl;
  257. req_cmd->len = 0;
  258. w1_netlink_queue_cmd(dev->priv, req_cmd);
  259. if (req_cmd->cmd == W1_CMD_LIST_SLAVES) {
  260. u64 rn;
  261. mutex_lock(&dev->list_mutex);
  262. list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
  263. memcpy(&rn, &sl->reg_num, sizeof(rn));
  264. w1_send_slave(dev, rn);
  265. }
  266. mutex_unlock(&dev->list_mutex);
  267. } else {
  268. w1_search_process_cb(dev, req_cmd->cmd == W1_CMD_ALARM_SEARCH ?
  269. W1_ALARM_SEARCH : W1_SEARCH, w1_found_send_slave);
  270. }
  271. return 0;
  272. }
  273. static int w1_process_command_io(struct w1_master *dev,
  274. struct w1_netlink_cmd *cmd)
  275. {
  276. int err = 0;
  277. switch (cmd->cmd) {
  278. case W1_CMD_TOUCH:
  279. w1_touch_block(dev, cmd->data, cmd->len);
  280. w1_netlink_queue_cmd(dev->priv, cmd);
  281. break;
  282. case W1_CMD_READ:
  283. w1_read_block(dev, cmd->data, cmd->len);
  284. w1_netlink_queue_cmd(dev->priv, cmd);
  285. break;
  286. case W1_CMD_WRITE:
  287. w1_write_block(dev, cmd->data, cmd->len);
  288. break;
  289. default:
  290. err = -EINVAL;
  291. break;
  292. }
  293. return err;
  294. }
  295. static int w1_process_command_addremove(struct w1_master *dev,
  296. struct w1_netlink_cmd *cmd)
  297. {
  298. struct w1_slave *sl;
  299. int err = 0;
  300. struct w1_reg_num *id;
  301. if (cmd->len != sizeof(*id))
  302. return -EINVAL;
  303. id = (struct w1_reg_num *)cmd->data;
  304. sl = w1_slave_search_device(dev, id);
  305. switch (cmd->cmd) {
  306. case W1_CMD_SLAVE_ADD:
  307. if (sl)
  308. err = -EINVAL;
  309. else
  310. err = w1_attach_slave_device(dev, id);
  311. break;
  312. case W1_CMD_SLAVE_REMOVE:
  313. if (sl)
  314. w1_slave_detach(sl);
  315. else
  316. err = -EINVAL;
  317. break;
  318. default:
  319. err = -EINVAL;
  320. break;
  321. }
  322. return err;
  323. }
  324. static int w1_process_command_master(struct w1_master *dev,
  325. struct w1_netlink_cmd *req_cmd)
  326. {
  327. int err = -EINVAL;
  328. /* drop bus_mutex for search (does it's own locking), and add/remove
  329. * which doesn't use the bus
  330. */
  331. switch (req_cmd->cmd) {
  332. case W1_CMD_SEARCH:
  333. case W1_CMD_ALARM_SEARCH:
  334. case W1_CMD_LIST_SLAVES:
  335. mutex_unlock(&dev->bus_mutex);
  336. err = w1_get_slaves(dev, req_cmd);
  337. mutex_lock(&dev->bus_mutex);
  338. break;
  339. case W1_CMD_READ:
  340. case W1_CMD_WRITE:
  341. case W1_CMD_TOUCH:
  342. err = w1_process_command_io(dev, req_cmd);
  343. break;
  344. case W1_CMD_RESET:
  345. err = w1_reset_bus(dev);
  346. break;
  347. case W1_CMD_SLAVE_ADD:
  348. case W1_CMD_SLAVE_REMOVE:
  349. mutex_unlock(&dev->bus_mutex);
  350. mutex_lock(&dev->mutex);
  351. err = w1_process_command_addremove(dev, req_cmd);
  352. mutex_unlock(&dev->mutex);
  353. mutex_lock(&dev->bus_mutex);
  354. break;
  355. default:
  356. err = -EINVAL;
  357. break;
  358. }
  359. return err;
  360. }
  361. static int w1_process_command_slave(struct w1_slave *sl,
  362. struct w1_netlink_cmd *cmd)
  363. {
  364. dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
  365. __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id,
  366. sl->reg_num.crc, cmd->cmd, cmd->len);
  367. return w1_process_command_io(sl->master, cmd);
  368. }
  369. static int w1_process_command_root(struct cn_msg *req_cn, u32 portid)
  370. {
  371. struct w1_master *dev;
  372. struct cn_msg *cn;
  373. struct w1_netlink_msg *msg;
  374. u32 *id;
  375. cn = kmalloc(PAGE_SIZE, GFP_KERNEL);
  376. if (!cn)
  377. return -ENOMEM;
  378. cn->id.idx = CN_W1_IDX;
  379. cn->id.val = CN_W1_VAL;
  380. cn->seq = req_cn->seq;
  381. cn->ack = req_cn->seq + 1;
  382. cn->len = sizeof(struct w1_netlink_msg);
  383. msg = (struct w1_netlink_msg *)cn->data;
  384. msg->type = W1_LIST_MASTERS;
  385. msg->status = 0;
  386. msg->len = 0;
  387. id = (u32 *)msg->data;
  388. mutex_lock(&w1_mlock);
  389. list_for_each_entry(dev, &w1_masters, w1_master_entry) {
  390. if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) {
  391. cn_netlink_send(cn, portid, 0, GFP_KERNEL);
  392. cn->len = sizeof(struct w1_netlink_msg);
  393. msg->len = 0;
  394. id = (u32 *)msg->data;
  395. }
  396. *id = dev->id;
  397. msg->len += sizeof(*id);
  398. cn->len += sizeof(*id);
  399. id++;
  400. }
  401. cn_netlink_send(cn, portid, 0, GFP_KERNEL);
  402. mutex_unlock(&w1_mlock);
  403. kfree(cn);
  404. return 0;
  405. }
  406. static void w1_process_cb(struct w1_master *dev, struct w1_async_cmd *async_cmd)
  407. {
  408. struct w1_cb_node *node = container_of(async_cmd, struct w1_cb_node,
  409. async);
  410. u16 mlen = node->msg->len;
  411. u16 len;
  412. int err = 0;
  413. struct w1_slave *sl = node->sl;
  414. struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)node->msg->data;
  415. mutex_lock(&dev->bus_mutex);
  416. dev->priv = node->block;
  417. if (sl && w1_reset_select_slave(sl))
  418. err = -ENODEV;
  419. node->block->cur_msg = node->msg;
  420. while (mlen && !err) {
  421. if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
  422. err = -E2BIG;
  423. break;
  424. }
  425. if (sl)
  426. err = w1_process_command_slave(sl, cmd);
  427. else
  428. err = w1_process_command_master(dev, cmd);
  429. w1_netlink_check_send(node->block);
  430. w1_netlink_queue_status(node->block, node->msg, cmd, err);
  431. err = 0;
  432. len = sizeof(*cmd) + cmd->len;
  433. cmd = (struct w1_netlink_cmd *)((u8 *)cmd + len);
  434. mlen -= len;
  435. }
  436. if (!cmd || err)
  437. w1_netlink_queue_status(node->block, node->msg, cmd, err);
  438. /* ref taken in w1_search_slave or w1_search_master_id when building
  439. * the block
  440. */
  441. if (sl)
  442. w1_unref_slave(sl);
  443. else
  444. atomic_dec(&dev->refcnt);
  445. dev->priv = NULL;
  446. mutex_unlock(&dev->bus_mutex);
  447. mutex_lock(&dev->list_mutex);
  448. list_del(&async_cmd->async_entry);
  449. mutex_unlock(&dev->list_mutex);
  450. w1_unref_block(node->block);
  451. }
  452. static void w1_list_count_cmds(struct w1_netlink_msg *msg, int *cmd_count,
  453. u16 *slave_len)
  454. {
  455. struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)msg->data;
  456. u16 mlen = msg->len;
  457. u16 len;
  458. int slave_list = 0;
  459. while (mlen) {
  460. if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen)
  461. break;
  462. switch (cmd->cmd) {
  463. case W1_CMD_SEARCH:
  464. case W1_CMD_ALARM_SEARCH:
  465. case W1_CMD_LIST_SLAVES:
  466. ++slave_list;
  467. }
  468. ++*cmd_count;
  469. len = sizeof(*cmd) + cmd->len;
  470. cmd = (struct w1_netlink_cmd *)((u8 *)cmd + len);
  471. mlen -= len;
  472. }
  473. if (slave_list) {
  474. struct w1_master *dev = w1_search_master_id(msg->id.mst.id);
  475. if (dev) {
  476. /* Bytes, and likely an overstimate, and if it isn't
  477. * the results can still be split between packets.
  478. */
  479. *slave_len += sizeof(struct w1_reg_num) * slave_list *
  480. (dev->slave_count + dev->max_slave_count);
  481. /* search incremented it */
  482. atomic_dec(&dev->refcnt);
  483. }
  484. }
  485. }
  486. static void w1_cn_callback(struct cn_msg *cn, struct netlink_skb_parms *nsp)
  487. {
  488. struct w1_netlink_msg *msg = (struct w1_netlink_msg *)(cn + 1);
  489. struct w1_slave *sl;
  490. struct w1_master *dev;
  491. u16 msg_len;
  492. u16 slave_len = 0;
  493. int err = 0;
  494. struct w1_cb_block *block = NULL;
  495. struct w1_cb_node *node = NULL;
  496. int node_count = 0;
  497. int cmd_count = 0;
  498. /* If any unknown flag is set let the application know, that way
  499. * applications can detect the absence of features in kernels that
  500. * don't know about them. http://lwn.net/Articles/587527/
  501. */
  502. if (cn->flags & ~(W1_CN_BUNDLE)) {
  503. w1_netlink_send_error(cn, msg, nsp->portid, -EINVAL);
  504. return;
  505. }
  506. /* Count the number of master or slave commands there are to allocate
  507. * space for one cb_node each.
  508. */
  509. msg_len = cn->len;
  510. while (msg_len && !err) {
  511. if (msg->len + sizeof(struct w1_netlink_msg) > msg_len) {
  512. err = -E2BIG;
  513. break;
  514. }
  515. /* count messages for nodes and allocate any additional space
  516. * required for slave lists
  517. */
  518. if (msg->type == W1_MASTER_CMD || msg->type == W1_SLAVE_CMD) {
  519. ++node_count;
  520. w1_list_count_cmds(msg, &cmd_count, &slave_len);
  521. }
  522. msg_len -= sizeof(struct w1_netlink_msg) + msg->len;
  523. msg = (struct w1_netlink_msg *)(((u8 *)msg) +
  524. sizeof(struct w1_netlink_msg) + msg->len);
  525. }
  526. msg = (struct w1_netlink_msg *)(cn + 1);
  527. if (node_count) {
  528. int size;
  529. int reply_size = sizeof(*cn) + cn->len + slave_len;
  530. if (cn->flags & W1_CN_BUNDLE) {
  531. /* bundling duplicats some of the messages */
  532. reply_size += 2 * cmd_count * (sizeof(struct cn_msg) +
  533. sizeof(struct w1_netlink_msg) +
  534. sizeof(struct w1_netlink_cmd));
  535. }
  536. reply_size = MIN(CONNECTOR_MAX_MSG_SIZE, reply_size);
  537. /* allocate space for the block, a copy of the original message,
  538. * one node per cmd to point into the original message,
  539. * space for replies which is the original message size plus
  540. * space for any list slave data and status messages
  541. * cn->len doesn't include itself which is part of the block
  542. * */
  543. size = /* block + original message */
  544. sizeof(struct w1_cb_block) + sizeof(*cn) + cn->len +
  545. /* space for nodes */
  546. node_count * sizeof(struct w1_cb_node) +
  547. /* replies */
  548. sizeof(struct cn_msg) + reply_size;
  549. block = kzalloc(size, GFP_KERNEL);
  550. if (!block) {
  551. /* if the system is already out of memory,
  552. * (A) will this work, and (B) would it be better
  553. * to not try?
  554. */
  555. w1_netlink_send_error(cn, msg, nsp->portid, -ENOMEM);
  556. return;
  557. }
  558. atomic_set(&block->refcnt, 1);
  559. block->portid = nsp->portid;
  560. memcpy(&block->request_cn, cn, sizeof(*cn) + cn->len);
  561. node = (struct w1_cb_node *)(block->request_cn.data + cn->len);
  562. /* Sneeky, when not bundling, reply_size is the allocated space
  563. * required for the reply, cn_msg isn't part of maxlen so
  564. * it should be reply_size - sizeof(struct cn_msg), however
  565. * when checking if there is enough space, w1_reply_make_space
  566. * is called with the full message size including cn_msg,
  567. * because it isn't known at that time if an additional cn_msg
  568. * will need to be allocated. So an extra cn_msg is added
  569. * above in "size".
  570. */
  571. block->maxlen = reply_size;
  572. block->first_cn = (struct cn_msg *)(node + node_count);
  573. memset(block->first_cn, 0, sizeof(*block->first_cn));
  574. }
  575. msg_len = cn->len;
  576. while (msg_len && !err) {
  577. dev = NULL;
  578. sl = NULL;
  579. if (msg->len + sizeof(struct w1_netlink_msg) > msg_len) {
  580. err = -E2BIG;
  581. break;
  582. }
  583. /* execute on this thread, no need to process later */
  584. if (msg->type == W1_LIST_MASTERS) {
  585. err = w1_process_command_root(cn, nsp->portid);
  586. goto out_cont;
  587. }
  588. /* All following message types require additional data,
  589. * check here before references are taken.
  590. */
  591. if (!msg->len) {
  592. err = -EPROTO;
  593. goto out_cont;
  594. }
  595. /* both search calls take references */
  596. if (msg->type == W1_MASTER_CMD) {
  597. dev = w1_search_master_id(msg->id.mst.id);
  598. } else if (msg->type == W1_SLAVE_CMD) {
  599. sl = w1_search_slave((struct w1_reg_num *)msg->id.id);
  600. if (sl)
  601. dev = sl->master;
  602. } else {
  603. pr_notice("%s: cn: %x.%x, wrong type: %u, len: %u.\n",
  604. __func__, cn->id.idx, cn->id.val,
  605. msg->type, msg->len);
  606. err = -EPROTO;
  607. goto out_cont;
  608. }
  609. if (!dev) {
  610. err = -ENODEV;
  611. goto out_cont;
  612. }
  613. err = 0;
  614. atomic_inc(&block->refcnt);
  615. node->async.cb = w1_process_cb;
  616. node->block = block;
  617. node->msg = (struct w1_netlink_msg *)((u8 *)&block->request_cn +
  618. (size_t)((u8 *)msg - (u8 *)cn));
  619. node->sl = sl;
  620. node->dev = dev;
  621. mutex_lock(&dev->list_mutex);
  622. list_add_tail(&node->async.async_entry, &dev->async_list);
  623. wake_up_process(dev->thread);
  624. mutex_unlock(&dev->list_mutex);
  625. ++node;
  626. out_cont:
  627. /* Can't queue because that modifies block and another
  628. * thread could be processing the messages by now and
  629. * there isn't a lock, send directly.
  630. */
  631. if (err)
  632. w1_netlink_send_error(cn, msg, nsp->portid, err);
  633. msg_len -= sizeof(struct w1_netlink_msg) + msg->len;
  634. msg = (struct w1_netlink_msg *)(((u8 *)msg) +
  635. sizeof(struct w1_netlink_msg) + msg->len);
  636. /*
  637. * Let's allow requests for nonexisting devices.
  638. */
  639. if (err == -ENODEV)
  640. err = 0;
  641. }
  642. if (block)
  643. w1_unref_block(block);
  644. }
  645. int w1_init_netlink(void)
  646. {
  647. struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
  648. return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
  649. }
  650. void w1_fini_netlink(void)
  651. {
  652. struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
  653. cn_del_callback(&w1_id);
  654. }
  655. #else
  656. void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *cn)
  657. {
  658. }
  659. int w1_init_netlink(void)
  660. {
  661. return 0;
  662. }
  663. void w1_fini_netlink(void)
  664. {
  665. }
  666. #endif