msg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 "dbg.h"
  39. #include "msg.h"
  40. #include "bearer.h"
  41. u32 tipc_msg_tot_importance(struct tipc_msg *m)
  42. {
  43. if (likely(msg_isdata(m))) {
  44. if (likely(msg_orignode(m) == tipc_own_addr))
  45. return msg_importance(m);
  46. return msg_importance(m) + 4;
  47. }
  48. if ((msg_user(m) == MSG_FRAGMENTER) &&
  49. (msg_type(m) == FIRST_FRAGMENT))
  50. return msg_importance(msg_get_wrapped(m));
  51. return msg_importance(m);
  52. }
  53. void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type,
  54. u32 hsize, u32 destnode)
  55. {
  56. memset(m, 0, hsize);
  57. msg_set_version(m);
  58. msg_set_user(m, user);
  59. msg_set_hdr_sz(m, hsize);
  60. msg_set_size(m, hsize);
  61. msg_set_prevnode(m, tipc_own_addr);
  62. msg_set_type(m, type);
  63. if (!msg_short(m)) {
  64. msg_set_orignode(m, tipc_own_addr);
  65. msg_set_destnode(m, destnode);
  66. }
  67. }
  68. /**
  69. * tipc_msg_calc_data_size - determine total data size for message
  70. */
  71. int tipc_msg_calc_data_size(struct iovec const *msg_sect, u32 num_sect)
  72. {
  73. int dsz = 0;
  74. int i;
  75. for (i = 0; i < num_sect; i++)
  76. dsz += msg_sect[i].iov_len;
  77. return dsz;
  78. }
  79. /**
  80. * tipc_msg_build - create message using specified header and data
  81. *
  82. * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
  83. *
  84. * Returns message data size or errno
  85. */
  86. int tipc_msg_build(struct tipc_msg *hdr,
  87. struct iovec const *msg_sect, u32 num_sect,
  88. int max_size, int usrmem, struct sk_buff** buf)
  89. {
  90. int dsz, sz, hsz, pos, res, cnt;
  91. dsz = tipc_msg_calc_data_size(msg_sect, num_sect);
  92. if (unlikely(dsz > TIPC_MAX_USER_MSG_SIZE)) {
  93. *buf = NULL;
  94. return -EINVAL;
  95. }
  96. pos = hsz = msg_hdr_sz(hdr);
  97. sz = hsz + dsz;
  98. msg_set_size(hdr, sz);
  99. if (unlikely(sz > max_size)) {
  100. *buf = NULL;
  101. return dsz;
  102. }
  103. *buf = buf_acquire(sz);
  104. if (!(*buf))
  105. return -ENOMEM;
  106. skb_copy_to_linear_data(*buf, hdr, hsz);
  107. for (res = 1, cnt = 0; res && (cnt < num_sect); cnt++) {
  108. if (likely(usrmem))
  109. res = !copy_from_user((*buf)->data + pos,
  110. msg_sect[cnt].iov_base,
  111. msg_sect[cnt].iov_len);
  112. else
  113. skb_copy_to_linear_data_offset(*buf, pos,
  114. msg_sect[cnt].iov_base,
  115. msg_sect[cnt].iov_len);
  116. pos += msg_sect[cnt].iov_len;
  117. }
  118. if (likely(res))
  119. return dsz;
  120. buf_discard(*buf);
  121. *buf = NULL;
  122. return -EFAULT;
  123. }
  124. #ifdef CONFIG_TIPC_DEBUG
  125. void tipc_msg_dbg(struct print_buf *buf, struct tipc_msg *msg, const char *str)
  126. {
  127. u32 usr = msg_user(msg);
  128. tipc_printf(buf, str);
  129. switch (usr) {
  130. case MSG_BUNDLER:
  131. tipc_printf(buf, "BNDL::");
  132. tipc_printf(buf, "MSGS(%u):", msg_msgcnt(msg));
  133. break;
  134. case BCAST_PROTOCOL:
  135. tipc_printf(buf, "BCASTP::");
  136. break;
  137. case MSG_FRAGMENTER:
  138. tipc_printf(buf, "FRAGM::");
  139. switch (msg_type(msg)) {
  140. case FIRST_FRAGMENT:
  141. tipc_printf(buf, "FIRST:");
  142. break;
  143. case FRAGMENT:
  144. tipc_printf(buf, "BODY:");
  145. break;
  146. case LAST_FRAGMENT:
  147. tipc_printf(buf, "LAST:");
  148. break;
  149. default:
  150. tipc_printf(buf, "UNKNOWN:%x",msg_type(msg));
  151. }
  152. tipc_printf(buf, "NO(%u/%u):",msg_long_msgno(msg),
  153. msg_fragm_no(msg));
  154. break;
  155. case TIPC_LOW_IMPORTANCE:
  156. case TIPC_MEDIUM_IMPORTANCE:
  157. case TIPC_HIGH_IMPORTANCE:
  158. case TIPC_CRITICAL_IMPORTANCE:
  159. tipc_printf(buf, "DAT%u:", msg_user(msg));
  160. if (msg_short(msg)) {
  161. tipc_printf(buf, "CON:");
  162. break;
  163. }
  164. switch (msg_type(msg)) {
  165. case TIPC_CONN_MSG:
  166. tipc_printf(buf, "CON:");
  167. break;
  168. case TIPC_MCAST_MSG:
  169. tipc_printf(buf, "MCST:");
  170. break;
  171. case TIPC_NAMED_MSG:
  172. tipc_printf(buf, "NAM:");
  173. break;
  174. case TIPC_DIRECT_MSG:
  175. tipc_printf(buf, "DIR:");
  176. break;
  177. default:
  178. tipc_printf(buf, "UNKNOWN TYPE %u",msg_type(msg));
  179. }
  180. if (msg_routed(msg) && !msg_non_seq(msg))
  181. tipc_printf(buf, "ROUT:");
  182. if (msg_reroute_cnt(msg))
  183. tipc_printf(buf, "REROUTED(%u):",
  184. msg_reroute_cnt(msg));
  185. break;
  186. case NAME_DISTRIBUTOR:
  187. tipc_printf(buf, "NMD::");
  188. switch (msg_type(msg)) {
  189. case PUBLICATION:
  190. tipc_printf(buf, "PUBL(%u):", (msg_size(msg) - msg_hdr_sz(msg)) / 20); /* Items */
  191. break;
  192. case WITHDRAWAL:
  193. tipc_printf(buf, "WDRW:");
  194. break;
  195. default:
  196. tipc_printf(buf, "UNKNOWN:%x",msg_type(msg));
  197. }
  198. if (msg_routed(msg))
  199. tipc_printf(buf, "ROUT:");
  200. if (msg_reroute_cnt(msg))
  201. tipc_printf(buf, "REROUTED(%u):",
  202. msg_reroute_cnt(msg));
  203. break;
  204. case CONN_MANAGER:
  205. tipc_printf(buf, "CONN_MNG:");
  206. switch (msg_type(msg)) {
  207. case CONN_PROBE:
  208. tipc_printf(buf, "PROBE:");
  209. break;
  210. case CONN_PROBE_REPLY:
  211. tipc_printf(buf, "PROBE_REPLY:");
  212. break;
  213. case CONN_ACK:
  214. tipc_printf(buf, "CONN_ACK:");
  215. tipc_printf(buf, "ACK(%u):",msg_msgcnt(msg));
  216. break;
  217. default:
  218. tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
  219. }
  220. if (msg_routed(msg))
  221. tipc_printf(buf, "ROUT:");
  222. if (msg_reroute_cnt(msg))
  223. tipc_printf(buf, "REROUTED(%u):",msg_reroute_cnt(msg));
  224. break;
  225. case LINK_PROTOCOL:
  226. tipc_printf(buf, "PROT:TIM(%u):",msg_timestamp(msg));
  227. switch (msg_type(msg)) {
  228. case STATE_MSG:
  229. tipc_printf(buf, "STATE:");
  230. tipc_printf(buf, "%s:",msg_probe(msg) ? "PRB" :"");
  231. tipc_printf(buf, "NXS(%u):",msg_next_sent(msg));
  232. tipc_printf(buf, "GAP(%u):",msg_seq_gap(msg));
  233. tipc_printf(buf, "LSTBC(%u):",msg_last_bcast(msg));
  234. break;
  235. case RESET_MSG:
  236. tipc_printf(buf, "RESET:");
  237. if (msg_size(msg) != msg_hdr_sz(msg))
  238. tipc_printf(buf, "BEAR:%s:",msg_data(msg));
  239. break;
  240. case ACTIVATE_MSG:
  241. tipc_printf(buf, "ACTIVATE:");
  242. break;
  243. default:
  244. tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
  245. }
  246. tipc_printf(buf, "PLANE(%c):",msg_net_plane(msg));
  247. tipc_printf(buf, "SESS(%u):",msg_session(msg));
  248. break;
  249. case CHANGEOVER_PROTOCOL:
  250. tipc_printf(buf, "TUNL:");
  251. switch (msg_type(msg)) {
  252. case DUPLICATE_MSG:
  253. tipc_printf(buf, "DUPL:");
  254. break;
  255. case ORIGINAL_MSG:
  256. tipc_printf(buf, "ORIG:");
  257. tipc_printf(buf, "EXP(%u)",msg_msgcnt(msg));
  258. break;
  259. default:
  260. tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
  261. }
  262. break;
  263. case ROUTE_DISTRIBUTOR:
  264. tipc_printf(buf, "ROUTING_MNG:");
  265. switch (msg_type(msg)) {
  266. case EXT_ROUTING_TABLE:
  267. tipc_printf(buf, "EXT_TBL:");
  268. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  269. break;
  270. case LOCAL_ROUTING_TABLE:
  271. tipc_printf(buf, "LOCAL_TBL:");
  272. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  273. break;
  274. case SLAVE_ROUTING_TABLE:
  275. tipc_printf(buf, "DP_TBL:");
  276. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  277. break;
  278. case ROUTE_ADDITION:
  279. tipc_printf(buf, "ADD:");
  280. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  281. break;
  282. case ROUTE_REMOVAL:
  283. tipc_printf(buf, "REMOVE:");
  284. tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
  285. break;
  286. default:
  287. tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
  288. }
  289. break;
  290. case LINK_CONFIG:
  291. tipc_printf(buf, "CFG:");
  292. switch (msg_type(msg)) {
  293. case DSC_REQ_MSG:
  294. tipc_printf(buf, "DSC_REQ:");
  295. break;
  296. case DSC_RESP_MSG:
  297. tipc_printf(buf, "DSC_RESP:");
  298. break;
  299. default:
  300. tipc_printf(buf, "UNKNOWN TYPE:%x:",msg_type(msg));
  301. break;
  302. }
  303. break;
  304. default:
  305. tipc_printf(buf, "UNKNOWN USER:");
  306. }
  307. switch (usr) {
  308. case CONN_MANAGER:
  309. case TIPC_LOW_IMPORTANCE:
  310. case TIPC_MEDIUM_IMPORTANCE:
  311. case TIPC_HIGH_IMPORTANCE:
  312. case TIPC_CRITICAL_IMPORTANCE:
  313. switch (msg_errcode(msg)) {
  314. case TIPC_OK:
  315. break;
  316. case TIPC_ERR_NO_NAME:
  317. tipc_printf(buf, "NO_NAME:");
  318. break;
  319. case TIPC_ERR_NO_PORT:
  320. tipc_printf(buf, "NO_PORT:");
  321. break;
  322. case TIPC_ERR_NO_NODE:
  323. tipc_printf(buf, "NO_PROC:");
  324. break;
  325. case TIPC_ERR_OVERLOAD:
  326. tipc_printf(buf, "OVERLOAD:");
  327. break;
  328. case TIPC_CONN_SHUTDOWN:
  329. tipc_printf(buf, "SHUTDOWN:");
  330. break;
  331. default:
  332. tipc_printf(buf, "UNKNOWN ERROR(%x):",
  333. msg_errcode(msg));
  334. }
  335. default:{}
  336. }
  337. tipc_printf(buf, "HZ(%u):", msg_hdr_sz(msg));
  338. tipc_printf(buf, "SZ(%u):", msg_size(msg));
  339. tipc_printf(buf, "SQNO(%u):", msg_seqno(msg));
  340. if (msg_non_seq(msg))
  341. tipc_printf(buf, "NOSEQ:");
  342. else {
  343. tipc_printf(buf, "ACK(%u):", msg_ack(msg));
  344. }
  345. tipc_printf(buf, "BACK(%u):", msg_bcast_ack(msg));
  346. tipc_printf(buf, "PRND(%x)", msg_prevnode(msg));
  347. if (msg_isdata(msg)) {
  348. if (msg_named(msg)) {
  349. tipc_printf(buf, "NTYP(%u):", msg_nametype(msg));
  350. tipc_printf(buf, "NINST(%u)", msg_nameinst(msg));
  351. }
  352. }
  353. if ((usr != LINK_PROTOCOL) && (usr != LINK_CONFIG) &&
  354. (usr != MSG_BUNDLER)) {
  355. if (!msg_short(msg)) {
  356. tipc_printf(buf, ":ORIG(%x:%u):",
  357. msg_orignode(msg), msg_origport(msg));
  358. tipc_printf(buf, ":DEST(%x:%u):",
  359. msg_destnode(msg), msg_destport(msg));
  360. } else {
  361. tipc_printf(buf, ":OPRT(%u):", msg_origport(msg));
  362. tipc_printf(buf, ":DPRT(%u):", msg_destport(msg));
  363. }
  364. if (msg_routed(msg) && !msg_non_seq(msg))
  365. tipc_printf(buf, ":TSEQN(%u)", msg_transp_seqno(msg));
  366. }
  367. if (msg_user(msg) == NAME_DISTRIBUTOR) {
  368. tipc_printf(buf, ":ONOD(%x):", msg_orignode(msg));
  369. tipc_printf(buf, ":DNOD(%x):", msg_destnode(msg));
  370. if (msg_routed(msg)) {
  371. tipc_printf(buf, ":CSEQN(%u)", msg_transp_seqno(msg));
  372. }
  373. }
  374. if (msg_user(msg) == LINK_CONFIG) {
  375. u32* raw = (u32*)msg;
  376. struct tipc_media_addr* orig = (struct tipc_media_addr*)&raw[5];
  377. tipc_printf(buf, ":REQL(%u):", msg_req_links(msg));
  378. tipc_printf(buf, ":DDOM(%x):", msg_dest_domain(msg));
  379. tipc_printf(buf, ":NETID(%u):", msg_bc_netid(msg));
  380. tipc_media_addr_printf(buf, orig);
  381. }
  382. if (msg_user(msg) == BCAST_PROTOCOL) {
  383. tipc_printf(buf, "BCNACK:AFTER(%u):", msg_bcgap_after(msg));
  384. tipc_printf(buf, "TO(%u):", msg_bcgap_to(msg));
  385. }
  386. tipc_printf(buf, "\n");
  387. if ((usr == CHANGEOVER_PROTOCOL) && (msg_msgcnt(msg))) {
  388. tipc_msg_dbg(buf, msg_get_wrapped(msg), " /");
  389. }
  390. if ((usr == MSG_FRAGMENTER) && (msg_type(msg) == FIRST_FRAGMENT)) {
  391. tipc_msg_dbg(buf, msg_get_wrapped(msg), " /");
  392. }
  393. }
  394. #endif