msg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * net/tipc/msg.c: TIPC message header routines
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2005, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "addr.h"
  38. #include "msg.h"
  39. u32 tipc_msg_tot_importance(struct tipc_msg *m)
  40. {
  41. if (likely(msg_isdata(m))) {
  42. if (likely(msg_orignode(m) == tipc_own_addr))
  43. return msg_importance(m);
  44. return msg_importance(m) + 4;
  45. }
  46. if ((msg_user(m) == MSG_FRAGMENTER) &&
  47. (msg_type(m) == FIRST_FRAGMENT))
  48. return msg_importance(msg_get_wrapped(m));
  49. return msg_importance(m);
  50. }
  51. void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type,
  52. u32 hsize, u32 destnode)
  53. {
  54. memset(m, 0, hsize);
  55. msg_set_version(m);
  56. msg_set_user(m, user);
  57. msg_set_hdr_sz(m, hsize);
  58. msg_set_size(m, hsize);
  59. msg_set_prevnode(m, tipc_own_addr);
  60. msg_set_type(m, type);
  61. if (!msg_short(m)) {
  62. msg_set_orignode(m, tipc_own_addr);
  63. msg_set_destnode(m, destnode);
  64. }
  65. }
  66. /**
  67. * tipc_msg_calc_data_size - determine total data size for message
  68. */
  69. int tipc_msg_calc_data_size(struct iovec const *msg_sect, u32 num_sect)
  70. {
  71. int dsz = 0;
  72. int i;
  73. for (i = 0; i < num_sect; i++)
  74. dsz += msg_sect[i].iov_len;
  75. return dsz;
  76. }
  77. /**
  78. * tipc_msg_build - create message using specified header and data
  79. *
  80. * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
  81. *
  82. * Returns message data size or errno
  83. */
  84. int tipc_msg_build(struct tipc_msg *hdr,
  85. struct iovec const *msg_sect, u32 num_sect,
  86. int max_size, int usrmem, struct sk_buff** buf)
  87. {
  88. int dsz, sz, hsz, pos, res, cnt;
  89. dsz = tipc_msg_calc_data_size(msg_sect, num_sect);
  90. if (unlikely(dsz > TIPC_MAX_USER_MSG_SIZE)) {
  91. *buf = NULL;
  92. return -EINVAL;
  93. }
  94. pos = hsz = msg_hdr_sz(hdr);
  95. sz = hsz + dsz;
  96. msg_set_size(hdr, sz);
  97. if (unlikely(sz > max_size)) {
  98. *buf = NULL;
  99. return dsz;
  100. }
  101. *buf = tipc_buf_acquire(sz);
  102. if (!(*buf))
  103. return -ENOMEM;
  104. skb_copy_to_linear_data(*buf, hdr, hsz);
  105. for (res = 1, cnt = 0; res && (cnt < num_sect); cnt++) {
  106. if (likely(usrmem))
  107. res = !copy_from_user((*buf)->data + pos,
  108. msg_sect[cnt].iov_base,
  109. msg_sect[cnt].iov_len);
  110. else
  111. skb_copy_to_linear_data_offset(*buf, pos,
  112. msg_sect[cnt].iov_base,
  113. msg_sect[cnt].iov_len);
  114. pos += msg_sect[cnt].iov_len;
  115. }
  116. if (likely(res))
  117. return dsz;
  118. buf_discard(*buf);
  119. *buf = NULL;
  120. return -EFAULT;
  121. }
  122. #ifdef CONFIG_TIPC_DEBUG
  123. void tipc_msg_dbg(struct print_buf *buf, struct tipc_msg *msg, const char *str)
  124. {
  125. u32 usr = msg_user(msg);
  126. tipc_printf(buf, KERN_DEBUG);
  127. tipc_printf(buf, str);
  128. switch (usr) {
  129. case MSG_BUNDLER:
  130. tipc_printf(buf, "BNDL::");
  131. tipc_printf(buf, "MSGS(%u):", msg_msgcnt(msg));
  132. break;
  133. case BCAST_PROTOCOL:
  134. tipc_printf(buf, "BCASTP::");
  135. break;
  136. case MSG_FRAGMENTER:
  137. tipc_printf(buf, "FRAGM::");
  138. switch (msg_type(msg)) {
  139. case FIRST_FRAGMENT:
  140. tipc_printf(buf, "FIRST:");
  141. break;
  142. case FRAGMENT:
  143. tipc_printf(buf, "BODY:");
  144. break;
  145. case LAST_FRAGMENT:
  146. tipc_printf(buf, "LAST:");
  147. break;
  148. default:
  149. tipc_printf(buf, "UNKNOWN:%x",msg_type(msg));
  150. }
  151. tipc_printf(buf, "NO(%u/%u):",msg_long_msgno(msg),
  152. msg_fragm_no(msg));
  153. break;
  154. case TIPC_LOW_IMPORTANCE:
  155. case TIPC_MEDIUM_IMPORTANCE:
  156. case TIPC_HIGH_IMPORTANCE:
  157. case TIPC_CRITICAL_IMPORTANCE:
  158. tipc_printf(buf, "DAT%u:", msg_user(msg));
  159. if (msg_short(msg)) {
  160. tipc_printf(buf, "CON:");
  161. break;
  162. }
  163. switch (msg_type(msg)) {
  164. case TIPC_CONN_MSG:
  165. tipc_printf(buf, "CON:");
  166. break;
  167. case TIPC_MCAST_MSG:
  168. tipc_printf(buf, "MCST:");
  169. break;
  170. case TIPC_NAMED_MSG:
  171. tipc_printf(buf, "NAM:");
  172. break;
  173. case TIPC_DIRECT_MSG:
  174. tipc_printf(buf, "DIR:");
  175. break;
  176. default:
  177. tipc_printf(buf, "UNKNOWN TYPE %u",msg_type(msg));
  178. }
  179. if (msg_routed(msg) && !msg_non_seq(msg))
  180. tipc_printf(buf, "ROUT:");
  181. if (msg_reroute_cnt(msg))
  182. tipc_printf(buf, "REROUTED(%u):",
  183. msg_reroute_cnt(msg));
  184. break;
  185. case NAME_DISTRIBUTOR:
  186. tipc_printf(buf, "NMD::");
  187. switch (msg_type(msg)) {
  188. case PUBLICATION:
  189. tipc_printf(buf, "PUBL(%u):", (msg_size(msg) - msg_hdr_sz(msg)) / 20); /* Items */
  190. break;
  191. case WITHDRAWAL:
  192. tipc_printf(buf, "WDRW:");
  193. break;
  194. default:
  195. tipc_printf(buf, "UNKNOWN:%x",msg_type(msg));
  196. }
  197. if (msg_routed(msg))
  198. tipc_printf(buf, "ROUT:");
  199. if (msg_reroute_cnt(msg))
  200. tipc_printf(buf, "REROUTED(%u):",
  201. msg_reroute_cnt(msg));
  202. break;
  203. case CONN_MANAGER:
  204. tipc_printf(buf, "CONN_MNG:");
  205. switch (msg_type(msg)) {
  206. case CONN_PROBE:
  207. tipc_printf(buf, "PROBE:");
  208. break;
  209. case CONN_PROBE_REPLY:
  210. tipc_printf(buf, "PROBE_REPLY:");
  211. break;
  212. case CONN_ACK:
  213. tipc_printf(buf, "CONN_ACK:");
  214. tipc_printf(buf, "ACK(%u):",msg_msgcnt(msg));
  215. break;
  216. default:
  217. tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
  218. }
  219. if (msg_routed(msg))
  220. tipc_printf(buf, "ROUT:");
  221. if (msg_reroute_cnt(msg))
  222. tipc_printf(buf, "REROUTED(%u):",msg_reroute_cnt(msg));
  223. break;
  224. case LINK_PROTOCOL:
  225. tipc_printf(buf, "PROT:TIM(%u):",msg_timestamp(msg));
  226. switch (msg_type(msg)) {
  227. case STATE_MSG:
  228. tipc_printf(buf, "STATE:");
  229. tipc_printf(buf, "%s:",msg_probe(msg) ? "PRB" :"");
  230. tipc_printf(buf, "NXS(%u):",msg_next_sent(msg));
  231. tipc_printf(buf, "GAP(%u):",msg_seq_gap(msg));
  232. tipc_printf(buf, "LSTBC(%u):",msg_last_bcast(msg));
  233. break;
  234. case RESET_MSG:
  235. tipc_printf(buf, "RESET:");
  236. if (msg_size(msg) != msg_hdr_sz(msg))
  237. tipc_printf(buf, "BEAR:%s:",msg_data(msg));
  238. break;
  239. case ACTIVATE_MSG:
  240. tipc_printf(buf, "ACTIVATE:");
  241. break;
  242. default:
  243. tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
  244. }
  245. tipc_printf(buf, "PLANE(%c):",msg_net_plane(msg));
  246. tipc_printf(buf, "SESS(%u):",msg_session(msg));
  247. break;
  248. case CHANGEOVER_PROTOCOL:
  249. tipc_printf(buf, "TUNL:");
  250. switch (msg_type(msg)) {
  251. case DUPLICATE_MSG:
  252. tipc_printf(buf, "DUPL:");
  253. break;
  254. case ORIGINAL_MSG:
  255. tipc_printf(buf, "ORIG:");
  256. tipc_printf(buf, "EXP(%u)",msg_msgcnt(msg));
  257. break;
  258. default:
  259. tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
  260. }
  261. break;
  262. case ROUTE_DISTRIBUTOR:
  263. tipc_printf(buf, "ROUTING_MNG:");
  264. switch (msg_type(msg)) {
  265. case EXT_ROUTING_TABLE:
  266. tipc_printf(buf, "EXT_TBL:");
  267. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  268. break;
  269. case LOCAL_ROUTING_TABLE:
  270. tipc_printf(buf, "LOCAL_TBL:");
  271. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  272. break;
  273. case SLAVE_ROUTING_TABLE:
  274. tipc_printf(buf, "DP_TBL:");
  275. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  276. break;
  277. case ROUTE_ADDITION:
  278. tipc_printf(buf, "ADD:");
  279. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  280. break;
  281. case ROUTE_REMOVAL:
  282. tipc_printf(buf, "REMOVE:");
  283. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  284. break;
  285. default:
  286. tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
  287. }
  288. break;
  289. case LINK_CONFIG:
  290. tipc_printf(buf, "CFG:");
  291. switch (msg_type(msg)) {
  292. case DSC_REQ_MSG:
  293. tipc_printf(buf, "DSC_REQ:");
  294. break;
  295. case DSC_RESP_MSG:
  296. tipc_printf(buf, "DSC_RESP:");
  297. break;
  298. default:
  299. tipc_printf(buf, "UNKNOWN TYPE:%x:",msg_type(msg));
  300. break;
  301. }
  302. break;
  303. default:
  304. tipc_printf(buf, "UNKNOWN USER:");
  305. }
  306. switch (usr) {
  307. case CONN_MANAGER:
  308. case TIPC_LOW_IMPORTANCE:
  309. case TIPC_MEDIUM_IMPORTANCE:
  310. case TIPC_HIGH_IMPORTANCE:
  311. case TIPC_CRITICAL_IMPORTANCE:
  312. switch (msg_errcode(msg)) {
  313. case TIPC_OK:
  314. break;
  315. case TIPC_ERR_NO_NAME:
  316. tipc_printf(buf, "NO_NAME:");
  317. break;
  318. case TIPC_ERR_NO_PORT:
  319. tipc_printf(buf, "NO_PORT:");
  320. break;
  321. case TIPC_ERR_NO_NODE:
  322. tipc_printf(buf, "NO_PROC:");
  323. break;
  324. case TIPC_ERR_OVERLOAD:
  325. tipc_printf(buf, "OVERLOAD:");
  326. break;
  327. case TIPC_CONN_SHUTDOWN:
  328. tipc_printf(buf, "SHUTDOWN:");
  329. break;
  330. default:
  331. tipc_printf(buf, "UNKNOWN ERROR(%x):",
  332. msg_errcode(msg));
  333. }
  334. default:{}
  335. }
  336. tipc_printf(buf, "HZ(%u):", msg_hdr_sz(msg));
  337. tipc_printf(buf, "SZ(%u):", msg_size(msg));
  338. tipc_printf(buf, "SQNO(%u):", msg_seqno(msg));
  339. if (msg_non_seq(msg))
  340. tipc_printf(buf, "NOSEQ:");
  341. else {
  342. tipc_printf(buf, "ACK(%u):", msg_ack(msg));
  343. }
  344. tipc_printf(buf, "BACK(%u):", msg_bcast_ack(msg));
  345. tipc_printf(buf, "PRND(%x)", msg_prevnode(msg));
  346. if (msg_isdata(msg)) {
  347. if (msg_named(msg)) {
  348. tipc_printf(buf, "NTYP(%u):", msg_nametype(msg));
  349. tipc_printf(buf, "NINST(%u)", msg_nameinst(msg));
  350. }
  351. }
  352. if ((usr != LINK_PROTOCOL) && (usr != LINK_CONFIG) &&
  353. (usr != MSG_BUNDLER)) {
  354. if (!msg_short(msg)) {
  355. tipc_printf(buf, ":ORIG(%x:%u):",
  356. msg_orignode(msg), msg_origport(msg));
  357. tipc_printf(buf, ":DEST(%x:%u):",
  358. msg_destnode(msg), msg_destport(msg));
  359. } else {
  360. tipc_printf(buf, ":OPRT(%u):", msg_origport(msg));
  361. tipc_printf(buf, ":DPRT(%u):", msg_destport(msg));
  362. }
  363. if (msg_routed(msg) && !msg_non_seq(msg))
  364. tipc_printf(buf, ":TSEQN(%u)", msg_transp_seqno(msg));
  365. }
  366. if (msg_user(msg) == NAME_DISTRIBUTOR) {
  367. tipc_printf(buf, ":ONOD(%x):", msg_orignode(msg));
  368. tipc_printf(buf, ":DNOD(%x):", msg_destnode(msg));
  369. if (msg_routed(msg)) {
  370. tipc_printf(buf, ":CSEQN(%u)", msg_transp_seqno(msg));
  371. }
  372. }
  373. if (msg_user(msg) == LINK_CONFIG) {
  374. u32* raw = (u32*)msg;
  375. struct tipc_media_addr* orig = (struct tipc_media_addr*)&raw[5];
  376. tipc_printf(buf, ":REQL(%u):", msg_req_links(msg));
  377. tipc_printf(buf, ":DDOM(%x):", msg_dest_domain(msg));
  378. tipc_printf(buf, ":NETID(%u):", msg_bc_netid(msg));
  379. tipc_media_addr_printf(buf, orig);
  380. }
  381. if (msg_user(msg) == BCAST_PROTOCOL) {
  382. tipc_printf(buf, "BCNACK:AFTER(%u):", msg_bcgap_after(msg));
  383. tipc_printf(buf, "TO(%u):", msg_bcgap_to(msg));
  384. }
  385. tipc_printf(buf, "\n");
  386. if ((usr == CHANGEOVER_PROTOCOL) && (msg_msgcnt(msg))) {
  387. tipc_msg_dbg(buf, msg_get_wrapped(msg), " /");
  388. }
  389. if ((usr == MSG_FRAGMENTER) && (msg_type(msg) == FIRST_FRAGMENT)) {
  390. tipc_msg_dbg(buf, msg_get_wrapped(msg), " /");
  391. }
  392. }
  393. #endif