msg.c 13 KB

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