msg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * net/tipc/msg.c: TIPC message header routines
  3. *
  4. * Copyright (c) 2000-2006, 2014, Ericsson AB
  5. * Copyright (c) 2005, 2010-2011, 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 "msg.h"
  38. #include "addr.h"
  39. #include "name_table.h"
  40. #define MAX_FORWARD_SIZE 1024
  41. static unsigned int align(unsigned int i)
  42. {
  43. return (i + 3) & ~3u;
  44. }
  45. void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type, u32 hsize,
  46. u32 destnode)
  47. {
  48. memset(m, 0, hsize);
  49. msg_set_version(m);
  50. msg_set_user(m, user);
  51. msg_set_hdr_sz(m, hsize);
  52. msg_set_size(m, hsize);
  53. msg_set_prevnode(m, tipc_own_addr);
  54. msg_set_type(m, type);
  55. msg_set_orignode(m, tipc_own_addr);
  56. msg_set_destnode(m, destnode);
  57. }
  58. /**
  59. * tipc_msg_build - create message using specified header and data
  60. *
  61. * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
  62. *
  63. * Returns message data size or errno
  64. */
  65. int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect,
  66. unsigned int len, int max_size, struct sk_buff **buf)
  67. {
  68. int dsz, sz, hsz;
  69. unsigned char *to;
  70. dsz = len;
  71. hsz = msg_hdr_sz(hdr);
  72. sz = hsz + dsz;
  73. msg_set_size(hdr, sz);
  74. if (unlikely(sz > max_size)) {
  75. *buf = NULL;
  76. return dsz;
  77. }
  78. *buf = tipc_buf_acquire(sz);
  79. if (!(*buf))
  80. return -ENOMEM;
  81. skb_copy_to_linear_data(*buf, hdr, hsz);
  82. to = (*buf)->data + hsz;
  83. if (len && memcpy_fromiovecend(to, msg_sect, 0, dsz)) {
  84. kfree_skb(*buf);
  85. *buf = NULL;
  86. return -EFAULT;
  87. }
  88. return dsz;
  89. }
  90. /* tipc_buf_append(): Append a buffer to the fragment list of another buffer
  91. * Let first buffer become head buffer
  92. * Returns 1 and sets *buf to headbuf if chain is complete, otherwise 0
  93. * Leaves headbuf pointer at NULL if failure
  94. */
  95. int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
  96. {
  97. struct sk_buff *head = *headbuf;
  98. struct sk_buff *frag = *buf;
  99. struct sk_buff *tail;
  100. struct tipc_msg *msg = buf_msg(frag);
  101. u32 fragid = msg_type(msg);
  102. bool headstolen;
  103. int delta;
  104. skb_pull(frag, msg_hdr_sz(msg));
  105. if (fragid == FIRST_FRAGMENT) {
  106. if (head || skb_unclone(frag, GFP_ATOMIC))
  107. goto out_free;
  108. head = *headbuf = frag;
  109. skb_frag_list_init(head);
  110. return 0;
  111. }
  112. if (!head)
  113. goto out_free;
  114. tail = TIPC_SKB_CB(head)->tail;
  115. if (skb_try_coalesce(head, frag, &headstolen, &delta)) {
  116. kfree_skb_partial(frag, headstolen);
  117. } else {
  118. if (!skb_has_frag_list(head))
  119. skb_shinfo(head)->frag_list = frag;
  120. else
  121. tail->next = frag;
  122. head->truesize += frag->truesize;
  123. head->data_len += frag->len;
  124. head->len += frag->len;
  125. TIPC_SKB_CB(head)->tail = frag;
  126. }
  127. if (fragid == LAST_FRAGMENT) {
  128. *buf = head;
  129. TIPC_SKB_CB(head)->tail = NULL;
  130. *headbuf = NULL;
  131. return 1;
  132. }
  133. *buf = NULL;
  134. return 0;
  135. out_free:
  136. pr_warn_ratelimited("Unable to build fragment list\n");
  137. kfree_skb(*buf);
  138. return 0;
  139. }
  140. /**
  141. * tipc_msg_build2 - create buffer chain containing specified header and data
  142. * @mhdr: Message header, to be prepended to data
  143. * @iov: User data
  144. * @offset: Posision in iov to start copying from
  145. * @dsz: Total length of user data
  146. * @pktmax: Max packet size that can be used
  147. * @chain: Buffer or chain of buffers to be returned to caller
  148. * Returns message data size or errno: -ENOMEM, -EFAULT
  149. */
  150. int tipc_msg_build2(struct tipc_msg *mhdr, struct iovec const *iov,
  151. int offset, int dsz, int pktmax , struct sk_buff **chain)
  152. {
  153. int mhsz = msg_hdr_sz(mhdr);
  154. int msz = mhsz + dsz;
  155. int pktno = 1;
  156. int pktsz;
  157. int pktrem = pktmax;
  158. int drem = dsz;
  159. struct tipc_msg pkthdr;
  160. struct sk_buff *buf, *prev;
  161. char *pktpos;
  162. int rc;
  163. msg_set_size(mhdr, msz);
  164. /* No fragmentation needed? */
  165. if (likely(msz <= pktmax)) {
  166. buf = tipc_buf_acquire(msz);
  167. *chain = buf;
  168. if (unlikely(!buf))
  169. return -ENOMEM;
  170. skb_copy_to_linear_data(buf, mhdr, mhsz);
  171. pktpos = buf->data + mhsz;
  172. if (!dsz || !memcpy_fromiovecend(pktpos, iov, offset, dsz))
  173. return dsz;
  174. rc = -EFAULT;
  175. goto error;
  176. }
  177. /* Prepare reusable fragment header */
  178. tipc_msg_init(&pkthdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
  179. INT_H_SIZE, msg_destnode(mhdr));
  180. msg_set_size(&pkthdr, pktmax);
  181. msg_set_fragm_no(&pkthdr, pktno);
  182. /* Prepare first fragment */
  183. *chain = buf = tipc_buf_acquire(pktmax);
  184. if (!buf)
  185. return -ENOMEM;
  186. pktpos = buf->data;
  187. skb_copy_to_linear_data(buf, &pkthdr, INT_H_SIZE);
  188. pktpos += INT_H_SIZE;
  189. pktrem -= INT_H_SIZE;
  190. skb_copy_to_linear_data_offset(buf, INT_H_SIZE, mhdr, mhsz);
  191. pktpos += mhsz;
  192. pktrem -= mhsz;
  193. do {
  194. if (drem < pktrem)
  195. pktrem = drem;
  196. if (memcpy_fromiovecend(pktpos, iov, offset, pktrem)) {
  197. rc = -EFAULT;
  198. goto error;
  199. }
  200. drem -= pktrem;
  201. offset += pktrem;
  202. if (!drem)
  203. break;
  204. /* Prepare new fragment: */
  205. if (drem < (pktmax - INT_H_SIZE))
  206. pktsz = drem + INT_H_SIZE;
  207. else
  208. pktsz = pktmax;
  209. prev = buf;
  210. buf = tipc_buf_acquire(pktsz);
  211. if (!buf) {
  212. rc = -ENOMEM;
  213. goto error;
  214. }
  215. prev->next = buf;
  216. msg_set_type(&pkthdr, FRAGMENT);
  217. msg_set_size(&pkthdr, pktsz);
  218. msg_set_fragm_no(&pkthdr, ++pktno);
  219. skb_copy_to_linear_data(buf, &pkthdr, INT_H_SIZE);
  220. pktpos = buf->data + INT_H_SIZE;
  221. pktrem = pktsz - INT_H_SIZE;
  222. } while (1);
  223. msg_set_type(buf_msg(buf), LAST_FRAGMENT);
  224. return dsz;
  225. error:
  226. kfree_skb_list(*chain);
  227. *chain = NULL;
  228. return rc;
  229. }
  230. /**
  231. * tipc_msg_bundle(): Append contents of a buffer to tail of an existing one
  232. * @bbuf: the existing buffer ("bundle")
  233. * @buf: buffer to be appended
  234. * @mtu: max allowable size for the bundle buffer
  235. * Consumes buffer if successful
  236. * Returns true if bundling could be performed, otherwise false
  237. */
  238. bool tipc_msg_bundle(struct sk_buff *bbuf, struct sk_buff *buf, u32 mtu)
  239. {
  240. struct tipc_msg *bmsg = buf_msg(bbuf);
  241. struct tipc_msg *msg = buf_msg(buf);
  242. unsigned int bsz = msg_size(bmsg);
  243. unsigned int msz = msg_size(msg);
  244. u32 start = align(bsz);
  245. u32 max = mtu - INT_H_SIZE;
  246. u32 pad = start - bsz;
  247. if (likely(msg_user(msg) == MSG_FRAGMENTER))
  248. return false;
  249. if (unlikely(msg_user(msg) == CHANGEOVER_PROTOCOL))
  250. return false;
  251. if (unlikely(msg_user(msg) == BCAST_PROTOCOL))
  252. return false;
  253. if (likely(msg_user(bmsg) != MSG_BUNDLER))
  254. return false;
  255. if (likely(msg_type(bmsg) != BUNDLE_OPEN))
  256. return false;
  257. if (unlikely(skb_tailroom(bbuf) < (pad + msz)))
  258. return false;
  259. if (unlikely(max < (start + msz)))
  260. return false;
  261. skb_put(bbuf, pad + msz);
  262. skb_copy_to_linear_data_offset(bbuf, start, buf->data, msz);
  263. msg_set_size(bmsg, start + msz);
  264. msg_set_msgcnt(bmsg, msg_msgcnt(bmsg) + 1);
  265. bbuf->next = buf->next;
  266. kfree_skb(buf);
  267. return true;
  268. }
  269. /**
  270. * tipc_msg_make_bundle(): Create bundle buf and append message to its tail
  271. * @buf: buffer to be appended and replaced
  272. * @mtu: max allowable size for the bundle buffer, inclusive header
  273. * @dnode: destination node for message. (Not always present in header)
  274. * Replaces buffer if successful
  275. * Returns true if sucess, otherwise false
  276. */
  277. bool tipc_msg_make_bundle(struct sk_buff **buf, u32 mtu, u32 dnode)
  278. {
  279. struct sk_buff *bbuf;
  280. struct tipc_msg *bmsg;
  281. struct tipc_msg *msg = buf_msg(*buf);
  282. u32 msz = msg_size(msg);
  283. u32 max = mtu - INT_H_SIZE;
  284. if (msg_user(msg) == MSG_FRAGMENTER)
  285. return false;
  286. if (msg_user(msg) == CHANGEOVER_PROTOCOL)
  287. return false;
  288. if (msg_user(msg) == BCAST_PROTOCOL)
  289. return false;
  290. if (msz > (max / 2))
  291. return false;
  292. bbuf = tipc_buf_acquire(max);
  293. if (!bbuf)
  294. return false;
  295. skb_trim(bbuf, INT_H_SIZE);
  296. bmsg = buf_msg(bbuf);
  297. tipc_msg_init(bmsg, MSG_BUNDLER, BUNDLE_OPEN, INT_H_SIZE, dnode);
  298. msg_set_seqno(bmsg, msg_seqno(msg));
  299. msg_set_ack(bmsg, msg_ack(msg));
  300. msg_set_bcast_ack(bmsg, msg_bcast_ack(msg));
  301. bbuf->next = (*buf)->next;
  302. tipc_msg_bundle(bbuf, *buf, mtu);
  303. *buf = bbuf;
  304. return true;
  305. }
  306. /**
  307. * tipc_msg_reverse(): swap source and destination addresses and add error code
  308. * @buf: buffer containing message to be reversed
  309. * @dnode: return value: node where to send message after reversal
  310. * @err: error code to be set in message
  311. * Consumes buffer if failure
  312. * Returns true if success, otherwise false
  313. */
  314. bool tipc_msg_reverse(struct sk_buff *buf, u32 *dnode, int err)
  315. {
  316. struct tipc_msg *msg = buf_msg(buf);
  317. uint imp = msg_importance(msg);
  318. struct tipc_msg ohdr;
  319. uint rdsz = min_t(uint, msg_data_sz(msg), MAX_FORWARD_SIZE);
  320. if (skb_linearize(buf))
  321. goto exit;
  322. if (msg_dest_droppable(msg))
  323. goto exit;
  324. if (msg_errcode(msg))
  325. goto exit;
  326. memcpy(&ohdr, msg, msg_hdr_sz(msg));
  327. imp = min_t(uint, imp + 1, TIPC_CRITICAL_IMPORTANCE);
  328. if (msg_isdata(msg))
  329. msg_set_importance(msg, imp);
  330. msg_set_errcode(msg, err);
  331. msg_set_origport(msg, msg_destport(&ohdr));
  332. msg_set_destport(msg, msg_origport(&ohdr));
  333. msg_set_prevnode(msg, tipc_own_addr);
  334. if (!msg_short(msg)) {
  335. msg_set_orignode(msg, msg_destnode(&ohdr));
  336. msg_set_destnode(msg, msg_orignode(&ohdr));
  337. }
  338. msg_set_size(msg, msg_hdr_sz(msg) + rdsz);
  339. skb_trim(buf, msg_size(msg));
  340. skb_orphan(buf);
  341. *dnode = msg_orignode(&ohdr);
  342. return true;
  343. exit:
  344. kfree_skb(buf);
  345. return false;
  346. }
  347. /**
  348. * tipc_msg_eval: determine fate of message that found no destination
  349. * @buf: the buffer containing the message.
  350. * @dnode: return value: next-hop node, if message to be forwarded
  351. * @err: error code to use, if message to be rejected
  352. *
  353. * Does not consume buffer
  354. * Returns 0 (TIPC_OK) if message ok and we can try again, -TIPC error
  355. * code if message to be rejected
  356. */
  357. int tipc_msg_eval(struct sk_buff *buf, u32 *dnode)
  358. {
  359. struct tipc_msg *msg = buf_msg(buf);
  360. u32 dport;
  361. if (msg_type(msg) != TIPC_NAMED_MSG)
  362. return -TIPC_ERR_NO_PORT;
  363. if (skb_linearize(buf))
  364. return -TIPC_ERR_NO_NAME;
  365. if (msg_data_sz(msg) > MAX_FORWARD_SIZE)
  366. return -TIPC_ERR_NO_NAME;
  367. if (msg_reroute_cnt(msg) > 0)
  368. return -TIPC_ERR_NO_NAME;
  369. *dnode = addr_domain(msg_lookup_scope(msg));
  370. dport = tipc_nametbl_translate(msg_nametype(msg),
  371. msg_nameinst(msg),
  372. dnode);
  373. if (!dport)
  374. return -TIPC_ERR_NO_NAME;
  375. msg_incr_reroute_cnt(msg);
  376. msg_set_destnode(msg, *dnode);
  377. msg_set_destport(msg, dport);
  378. return TIPC_OK;
  379. }