msg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * net/tipc/msg.c: TIPC message header routines
  3. *
  4. * Copyright (c) 2000-2006, 2014-2015, 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 <net/sock.h>
  37. #include "core.h"
  38. #include "msg.h"
  39. #include "addr.h"
  40. #include "name_table.h"
  41. #include "crypto.h"
  42. #define BUF_ALIGN(x) ALIGN(x, 4)
  43. #define MAX_FORWARD_SIZE 1024
  44. #ifdef CONFIG_TIPC_CRYPTO
  45. #define BUF_HEADROOM ALIGN(((LL_MAX_HEADER + 48) + EHDR_MAX_SIZE), 16)
  46. #define BUF_OVERHEAD (BUF_HEADROOM + TIPC_AES_GCM_TAG_SIZE)
  47. #else
  48. #define BUF_HEADROOM (LL_MAX_HEADER + 48)
  49. #define BUF_OVERHEAD BUF_HEADROOM
  50. #endif
  51. const int one_page_mtu = PAGE_SIZE - SKB_DATA_ALIGN(BUF_OVERHEAD) -
  52. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  53. /**
  54. * tipc_buf_acquire - creates a TIPC message buffer
  55. * @size: message size (including TIPC header)
  56. * @gfp: memory allocation flags
  57. *
  58. * Return: a new buffer with data pointers set to the specified size.
  59. *
  60. * NOTE:
  61. * Headroom is reserved to allow prepending of a data link header.
  62. * There may also be unrequested tailroom present at the buffer's end.
  63. */
  64. struct sk_buff *tipc_buf_acquire(u32 size, gfp_t gfp)
  65. {
  66. struct sk_buff *skb;
  67. skb = alloc_skb_fclone(BUF_OVERHEAD + size, gfp);
  68. if (skb) {
  69. skb_reserve(skb, BUF_HEADROOM);
  70. skb_put(skb, size);
  71. skb->next = NULL;
  72. }
  73. return skb;
  74. }
  75. void tipc_msg_init(u32 own_node, struct tipc_msg *m, u32 user, u32 type,
  76. u32 hsize, u32 dnode)
  77. {
  78. memset(m, 0, hsize);
  79. msg_set_version(m);
  80. msg_set_user(m, user);
  81. msg_set_hdr_sz(m, hsize);
  82. msg_set_size(m, hsize);
  83. msg_set_prevnode(m, own_node);
  84. msg_set_type(m, type);
  85. if (hsize > SHORT_H_SIZE) {
  86. msg_set_orignode(m, own_node);
  87. msg_set_destnode(m, dnode);
  88. }
  89. }
  90. struct sk_buff *tipc_msg_create(uint user, uint type,
  91. uint hdr_sz, uint data_sz, u32 dnode,
  92. u32 onode, u32 dport, u32 oport, int errcode)
  93. {
  94. struct tipc_msg *msg;
  95. struct sk_buff *buf;
  96. buf = tipc_buf_acquire(hdr_sz + data_sz, GFP_ATOMIC);
  97. if (unlikely(!buf))
  98. return NULL;
  99. msg = buf_msg(buf);
  100. tipc_msg_init(onode, msg, user, type, hdr_sz, dnode);
  101. msg_set_size(msg, hdr_sz + data_sz);
  102. msg_set_origport(msg, oport);
  103. msg_set_destport(msg, dport);
  104. msg_set_errcode(msg, errcode);
  105. return buf;
  106. }
  107. /* tipc_buf_append(): Append a buffer to the fragment list of another buffer
  108. * @*headbuf: in: NULL for first frag, otherwise value returned from prev call
  109. * out: set when successful non-complete reassembly, otherwise NULL
  110. * @*buf: in: the buffer to append. Always defined
  111. * out: head buf after successful complete reassembly, otherwise NULL
  112. * Returns 1 when reassembly complete, otherwise 0
  113. */
  114. int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
  115. {
  116. struct sk_buff *head = *headbuf;
  117. struct sk_buff *frag = *buf;
  118. struct sk_buff *tail = NULL;
  119. struct tipc_msg *msg;
  120. u32 fragid;
  121. int delta;
  122. bool headstolen;
  123. if (!frag)
  124. goto err;
  125. msg = buf_msg(frag);
  126. fragid = msg_type(msg);
  127. frag->next = NULL;
  128. skb_pull(frag, msg_hdr_sz(msg));
  129. if (fragid == FIRST_FRAGMENT) {
  130. if (unlikely(head))
  131. goto err;
  132. if (skb_has_frag_list(frag) && __skb_linearize(frag))
  133. goto err;
  134. *buf = NULL;
  135. frag = skb_unshare(frag, GFP_ATOMIC);
  136. if (unlikely(!frag))
  137. goto err;
  138. head = *headbuf = frag;
  139. TIPC_SKB_CB(head)->tail = NULL;
  140. return 0;
  141. }
  142. if (!head)
  143. goto err;
  144. /* Either the input skb ownership is transferred to headskb
  145. * or the input skb is freed, clear the reference to avoid
  146. * bad access on error path.
  147. */
  148. *buf = NULL;
  149. if (skb_try_coalesce(head, frag, &headstolen, &delta)) {
  150. kfree_skb_partial(frag, headstolen);
  151. } else {
  152. tail = TIPC_SKB_CB(head)->tail;
  153. if (!skb_has_frag_list(head))
  154. skb_shinfo(head)->frag_list = frag;
  155. else
  156. tail->next = frag;
  157. head->truesize += frag->truesize;
  158. head->data_len += frag->len;
  159. head->len += frag->len;
  160. TIPC_SKB_CB(head)->tail = frag;
  161. }
  162. if (fragid == LAST_FRAGMENT) {
  163. TIPC_SKB_CB(head)->validated = 0;
  164. if (unlikely(!tipc_msg_validate(&head)))
  165. goto err;
  166. *buf = head;
  167. TIPC_SKB_CB(head)->tail = NULL;
  168. *headbuf = NULL;
  169. return 1;
  170. }
  171. return 0;
  172. err:
  173. kfree_skb(*buf);
  174. kfree_skb(*headbuf);
  175. *buf = *headbuf = NULL;
  176. return 0;
  177. }
  178. /**
  179. * tipc_msg_append(): Append data to tail of an existing buffer queue
  180. * @_hdr: header to be used
  181. * @m: the data to be appended
  182. * @mss: max allowable size of buffer
  183. * @dlen: size of data to be appended
  184. * @txq: queue to append to
  185. *
  186. * Return: the number of 1k blocks appended or errno value
  187. */
  188. int tipc_msg_append(struct tipc_msg *_hdr, struct msghdr *m, int dlen,
  189. int mss, struct sk_buff_head *txq)
  190. {
  191. struct sk_buff *skb;
  192. int accounted, total, curr;
  193. int mlen, cpy, rem = dlen;
  194. struct tipc_msg *hdr;
  195. skb = skb_peek_tail(txq);
  196. accounted = skb ? msg_blocks(buf_msg(skb)) : 0;
  197. total = accounted;
  198. do {
  199. if (!skb || skb->len >= mss) {
  200. skb = tipc_buf_acquire(mss, GFP_KERNEL);
  201. if (unlikely(!skb))
  202. return -ENOMEM;
  203. skb_orphan(skb);
  204. skb_trim(skb, MIN_H_SIZE);
  205. hdr = buf_msg(skb);
  206. skb_copy_to_linear_data(skb, _hdr, MIN_H_SIZE);
  207. msg_set_hdr_sz(hdr, MIN_H_SIZE);
  208. msg_set_size(hdr, MIN_H_SIZE);
  209. __skb_queue_tail(txq, skb);
  210. total += 1;
  211. }
  212. hdr = buf_msg(skb);
  213. curr = msg_blocks(hdr);
  214. mlen = msg_size(hdr);
  215. cpy = min_t(size_t, rem, mss - mlen);
  216. if (cpy != copy_from_iter(skb->data + mlen, cpy, &m->msg_iter))
  217. return -EFAULT;
  218. msg_set_size(hdr, mlen + cpy);
  219. skb_put(skb, cpy);
  220. rem -= cpy;
  221. total += msg_blocks(hdr) - curr;
  222. } while (rem > 0);
  223. return total - accounted;
  224. }
  225. /* tipc_msg_validate - validate basic format of received message
  226. *
  227. * This routine ensures a TIPC message has an acceptable header, and at least
  228. * as much data as the header indicates it should. The routine also ensures
  229. * that the entire message header is stored in the main fragment of the message
  230. * buffer, to simplify future access to message header fields.
  231. *
  232. * Note: Having extra info present in the message header or data areas is OK.
  233. * TIPC will ignore the excess, under the assumption that it is optional info
  234. * introduced by a later release of the protocol.
  235. */
  236. bool tipc_msg_validate(struct sk_buff **_skb)
  237. {
  238. struct sk_buff *skb = *_skb;
  239. struct tipc_msg *hdr;
  240. int msz, hsz;
  241. /* Ensure that flow control ratio condition is satisfied */
  242. if (unlikely(skb->truesize / buf_roundup_len(skb) >= 4)) {
  243. skb = skb_copy_expand(skb, BUF_HEADROOM, 0, GFP_ATOMIC);
  244. if (!skb)
  245. return false;
  246. kfree_skb(*_skb);
  247. *_skb = skb;
  248. }
  249. if (unlikely(TIPC_SKB_CB(skb)->validated))
  250. return true;
  251. if (unlikely(!pskb_may_pull(skb, MIN_H_SIZE)))
  252. return false;
  253. hsz = msg_hdr_sz(buf_msg(skb));
  254. if (unlikely(hsz < MIN_H_SIZE) || (hsz > MAX_H_SIZE))
  255. return false;
  256. if (unlikely(!pskb_may_pull(skb, hsz)))
  257. return false;
  258. hdr = buf_msg(skb);
  259. if (unlikely(msg_version(hdr) != TIPC_VERSION))
  260. return false;
  261. msz = msg_size(hdr);
  262. if (unlikely(msz < hsz))
  263. return false;
  264. if (unlikely((msz - hsz) > TIPC_MAX_USER_MSG_SIZE))
  265. return false;
  266. if (unlikely(skb->len < msz))
  267. return false;
  268. TIPC_SKB_CB(skb)->validated = 1;
  269. return true;
  270. }
  271. /**
  272. * tipc_msg_fragment - build a fragment skb list for TIPC message
  273. *
  274. * @skb: TIPC message skb
  275. * @hdr: internal msg header to be put on the top of the fragments
  276. * @pktmax: max size of a fragment incl. the header
  277. * @frags: returned fragment skb list
  278. *
  279. * Return: 0 if the fragmentation is successful, otherwise: -EINVAL
  280. * or -ENOMEM
  281. */
  282. int tipc_msg_fragment(struct sk_buff *skb, const struct tipc_msg *hdr,
  283. int pktmax, struct sk_buff_head *frags)
  284. {
  285. int pktno, nof_fragms, dsz, dmax, eat;
  286. struct tipc_msg *_hdr;
  287. struct sk_buff *_skb;
  288. u8 *data;
  289. /* Non-linear buffer? */
  290. if (skb_linearize(skb))
  291. return -ENOMEM;
  292. data = (u8 *)skb->data;
  293. dsz = msg_size(buf_msg(skb));
  294. dmax = pktmax - INT_H_SIZE;
  295. if (dsz <= dmax || !dmax)
  296. return -EINVAL;
  297. nof_fragms = dsz / dmax + 1;
  298. for (pktno = 1; pktno <= nof_fragms; pktno++) {
  299. if (pktno < nof_fragms)
  300. eat = dmax;
  301. else
  302. eat = dsz % dmax;
  303. /* Allocate a new fragment */
  304. _skb = tipc_buf_acquire(INT_H_SIZE + eat, GFP_ATOMIC);
  305. if (!_skb)
  306. goto error;
  307. skb_orphan(_skb);
  308. __skb_queue_tail(frags, _skb);
  309. /* Copy header & data to the fragment */
  310. skb_copy_to_linear_data(_skb, hdr, INT_H_SIZE);
  311. skb_copy_to_linear_data_offset(_skb, INT_H_SIZE, data, eat);
  312. data += eat;
  313. /* Update the fragment's header */
  314. _hdr = buf_msg(_skb);
  315. msg_set_fragm_no(_hdr, pktno);
  316. msg_set_nof_fragms(_hdr, nof_fragms);
  317. msg_set_size(_hdr, INT_H_SIZE + eat);
  318. }
  319. return 0;
  320. error:
  321. __skb_queue_purge(frags);
  322. __skb_queue_head_init(frags);
  323. return -ENOMEM;
  324. }
  325. /**
  326. * tipc_msg_build - create buffer chain containing specified header and data
  327. * @mhdr: Message header, to be prepended to data
  328. * @m: User message
  329. * @offset: buffer offset for fragmented messages (FIXME)
  330. * @dsz: Total length of user data
  331. * @pktmax: Max packet size that can be used
  332. * @list: Buffer or chain of buffers to be returned to caller
  333. *
  334. * Note that the recursive call we are making here is safe, since it can
  335. * logically go only one further level down.
  336. *
  337. * Return: message data size or errno: -ENOMEM, -EFAULT
  338. */
  339. int tipc_msg_build(struct tipc_msg *mhdr, struct msghdr *m, int offset,
  340. int dsz, int pktmax, struct sk_buff_head *list)
  341. {
  342. int mhsz = msg_hdr_sz(mhdr);
  343. struct tipc_msg pkthdr;
  344. int msz = mhsz + dsz;
  345. int pktrem = pktmax;
  346. struct sk_buff *skb;
  347. int drem = dsz;
  348. int pktno = 1;
  349. char *pktpos;
  350. int pktsz;
  351. int rc;
  352. msg_set_size(mhdr, msz);
  353. /* No fragmentation needed? */
  354. if (likely(msz <= pktmax)) {
  355. skb = tipc_buf_acquire(msz, GFP_KERNEL);
  356. /* Fall back to smaller MTU if node local message */
  357. if (unlikely(!skb)) {
  358. if (pktmax != MAX_MSG_SIZE)
  359. return -ENOMEM;
  360. rc = tipc_msg_build(mhdr, m, offset, dsz,
  361. one_page_mtu, list);
  362. if (rc != dsz)
  363. return rc;
  364. if (tipc_msg_assemble(list))
  365. return dsz;
  366. return -ENOMEM;
  367. }
  368. skb_orphan(skb);
  369. __skb_queue_tail(list, skb);
  370. skb_copy_to_linear_data(skb, mhdr, mhsz);
  371. pktpos = skb->data + mhsz;
  372. if (copy_from_iter_full(pktpos, dsz, &m->msg_iter))
  373. return dsz;
  374. rc = -EFAULT;
  375. goto error;
  376. }
  377. /* Prepare reusable fragment header */
  378. tipc_msg_init(msg_prevnode(mhdr), &pkthdr, MSG_FRAGMENTER,
  379. FIRST_FRAGMENT, INT_H_SIZE, msg_destnode(mhdr));
  380. msg_set_size(&pkthdr, pktmax);
  381. msg_set_fragm_no(&pkthdr, pktno);
  382. msg_set_importance(&pkthdr, msg_importance(mhdr));
  383. /* Prepare first fragment */
  384. skb = tipc_buf_acquire(pktmax, GFP_KERNEL);
  385. if (!skb)
  386. return -ENOMEM;
  387. skb_orphan(skb);
  388. __skb_queue_tail(list, skb);
  389. pktpos = skb->data;
  390. skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
  391. pktpos += INT_H_SIZE;
  392. pktrem -= INT_H_SIZE;
  393. skb_copy_to_linear_data_offset(skb, INT_H_SIZE, mhdr, mhsz);
  394. pktpos += mhsz;
  395. pktrem -= mhsz;
  396. do {
  397. if (drem < pktrem)
  398. pktrem = drem;
  399. if (!copy_from_iter_full(pktpos, pktrem, &m->msg_iter)) {
  400. rc = -EFAULT;
  401. goto error;
  402. }
  403. drem -= pktrem;
  404. if (!drem)
  405. break;
  406. /* Prepare new fragment: */
  407. if (drem < (pktmax - INT_H_SIZE))
  408. pktsz = drem + INT_H_SIZE;
  409. else
  410. pktsz = pktmax;
  411. skb = tipc_buf_acquire(pktsz, GFP_KERNEL);
  412. if (!skb) {
  413. rc = -ENOMEM;
  414. goto error;
  415. }
  416. skb_orphan(skb);
  417. __skb_queue_tail(list, skb);
  418. msg_set_type(&pkthdr, FRAGMENT);
  419. msg_set_size(&pkthdr, pktsz);
  420. msg_set_fragm_no(&pkthdr, ++pktno);
  421. skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
  422. pktpos = skb->data + INT_H_SIZE;
  423. pktrem = pktsz - INT_H_SIZE;
  424. } while (1);
  425. msg_set_type(buf_msg(skb), LAST_FRAGMENT);
  426. return dsz;
  427. error:
  428. __skb_queue_purge(list);
  429. __skb_queue_head_init(list);
  430. return rc;
  431. }
  432. /**
  433. * tipc_msg_bundle - Append contents of a buffer to tail of an existing one
  434. * @bskb: the bundle buffer to append to
  435. * @msg: message to be appended
  436. * @max: max allowable size for the bundle buffer
  437. *
  438. * Return: "true" if bundling has been performed, otherwise "false"
  439. */
  440. static bool tipc_msg_bundle(struct sk_buff *bskb, struct tipc_msg *msg,
  441. u32 max)
  442. {
  443. struct tipc_msg *bmsg = buf_msg(bskb);
  444. u32 msz, bsz, offset, pad;
  445. msz = msg_size(msg);
  446. bsz = msg_size(bmsg);
  447. offset = BUF_ALIGN(bsz);
  448. pad = offset - bsz;
  449. if (unlikely(skb_tailroom(bskb) < (pad + msz)))
  450. return false;
  451. if (unlikely(max < (offset + msz)))
  452. return false;
  453. skb_put(bskb, pad + msz);
  454. skb_copy_to_linear_data_offset(bskb, offset, msg, msz);
  455. msg_set_size(bmsg, offset + msz);
  456. msg_set_msgcnt(bmsg, msg_msgcnt(bmsg) + 1);
  457. return true;
  458. }
  459. /**
  460. * tipc_msg_try_bundle - Try to bundle a new message to the last one
  461. * @tskb: the last/target message to which the new one will be appended
  462. * @skb: the new message skb pointer
  463. * @mss: max message size (header inclusive)
  464. * @dnode: destination node for the message
  465. * @new_bundle: if this call made a new bundle or not
  466. *
  467. * Return: "true" if the new message skb is potential for bundling this time or
  468. * later, in the case a bundling has been done this time, the skb is consumed
  469. * (the skb pointer = NULL).
  470. * Otherwise, "false" if the skb cannot be bundled at all.
  471. */
  472. bool tipc_msg_try_bundle(struct sk_buff *tskb, struct sk_buff **skb, u32 mss,
  473. u32 dnode, bool *new_bundle)
  474. {
  475. struct tipc_msg *msg, *inner, *outer;
  476. u32 tsz;
  477. /* First, check if the new buffer is suitable for bundling */
  478. msg = buf_msg(*skb);
  479. if (msg_user(msg) == MSG_FRAGMENTER)
  480. return false;
  481. if (msg_user(msg) == TUNNEL_PROTOCOL)
  482. return false;
  483. if (msg_user(msg) == BCAST_PROTOCOL)
  484. return false;
  485. if (mss <= INT_H_SIZE + msg_size(msg))
  486. return false;
  487. /* Ok, but the last/target buffer can be empty? */
  488. if (unlikely(!tskb))
  489. return true;
  490. /* Is it a bundle already? Try to bundle the new message to it */
  491. if (msg_user(buf_msg(tskb)) == MSG_BUNDLER) {
  492. *new_bundle = false;
  493. goto bundle;
  494. }
  495. /* Make a new bundle of the two messages if possible */
  496. tsz = msg_size(buf_msg(tskb));
  497. if (unlikely(mss < BUF_ALIGN(INT_H_SIZE + tsz) + msg_size(msg)))
  498. return true;
  499. if (unlikely(pskb_expand_head(tskb, INT_H_SIZE, mss - tsz - INT_H_SIZE,
  500. GFP_ATOMIC)))
  501. return true;
  502. inner = buf_msg(tskb);
  503. skb_push(tskb, INT_H_SIZE);
  504. outer = buf_msg(tskb);
  505. tipc_msg_init(msg_prevnode(inner), outer, MSG_BUNDLER, 0, INT_H_SIZE,
  506. dnode);
  507. msg_set_importance(outer, msg_importance(inner));
  508. msg_set_size(outer, INT_H_SIZE + tsz);
  509. msg_set_msgcnt(outer, 1);
  510. *new_bundle = true;
  511. bundle:
  512. if (likely(tipc_msg_bundle(tskb, msg, mss))) {
  513. consume_skb(*skb);
  514. *skb = NULL;
  515. }
  516. return true;
  517. }
  518. /**
  519. * tipc_msg_extract(): extract bundled inner packet from buffer
  520. * @skb: buffer to be extracted from.
  521. * @iskb: extracted inner buffer, to be returned
  522. * @pos: position in outer message of msg to be extracted.
  523. * Returns position of next msg.
  524. * Consumes outer buffer when last packet extracted
  525. * Return: true when there is an extracted buffer, otherwise false
  526. */
  527. bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos)
  528. {
  529. struct tipc_msg *hdr, *ihdr;
  530. int imsz;
  531. *iskb = NULL;
  532. if (unlikely(skb_linearize(skb)))
  533. goto none;
  534. hdr = buf_msg(skb);
  535. if (unlikely(*pos > (msg_data_sz(hdr) - MIN_H_SIZE)))
  536. goto none;
  537. ihdr = (struct tipc_msg *)(msg_data(hdr) + *pos);
  538. imsz = msg_size(ihdr);
  539. if ((*pos + imsz) > msg_data_sz(hdr))
  540. goto none;
  541. *iskb = tipc_buf_acquire(imsz, GFP_ATOMIC);
  542. if (!*iskb)
  543. goto none;
  544. skb_copy_to_linear_data(*iskb, ihdr, imsz);
  545. if (unlikely(!tipc_msg_validate(iskb)))
  546. goto none;
  547. *pos += BUF_ALIGN(imsz);
  548. return true;
  549. none:
  550. kfree_skb(skb);
  551. kfree_skb(*iskb);
  552. *iskb = NULL;
  553. return false;
  554. }
  555. /**
  556. * tipc_msg_reverse(): swap source and destination addresses and add error code
  557. * @own_node: originating node id for reversed message
  558. * @skb: buffer containing message to be reversed; will be consumed
  559. * @err: error code to be set in message, if any
  560. * Replaces consumed buffer with new one when successful
  561. * Return: true if success, otherwise false
  562. */
  563. bool tipc_msg_reverse(u32 own_node, struct sk_buff **skb, int err)
  564. {
  565. struct sk_buff *_skb = *skb;
  566. struct tipc_msg *_hdr, *hdr;
  567. int hlen, dlen;
  568. if (skb_linearize(_skb))
  569. goto exit;
  570. _hdr = buf_msg(_skb);
  571. dlen = min_t(uint, msg_data_sz(_hdr), MAX_FORWARD_SIZE);
  572. hlen = msg_hdr_sz(_hdr);
  573. if (msg_dest_droppable(_hdr))
  574. goto exit;
  575. if (msg_errcode(_hdr))
  576. goto exit;
  577. /* Never return SHORT header */
  578. if (hlen == SHORT_H_SIZE)
  579. hlen = BASIC_H_SIZE;
  580. /* Don't return data along with SYN+, - sender has a clone */
  581. if (msg_is_syn(_hdr) && err == TIPC_ERR_OVERLOAD)
  582. dlen = 0;
  583. /* Allocate new buffer to return */
  584. *skb = tipc_buf_acquire(hlen + dlen, GFP_ATOMIC);
  585. if (!*skb)
  586. goto exit;
  587. memcpy((*skb)->data, _skb->data, msg_hdr_sz(_hdr));
  588. memcpy((*skb)->data + hlen, msg_data(_hdr), dlen);
  589. /* Build reverse header in new buffer */
  590. hdr = buf_msg(*skb);
  591. msg_set_hdr_sz(hdr, hlen);
  592. msg_set_errcode(hdr, err);
  593. msg_set_non_seq(hdr, 0);
  594. msg_set_origport(hdr, msg_destport(_hdr));
  595. msg_set_destport(hdr, msg_origport(_hdr));
  596. msg_set_destnode(hdr, msg_prevnode(_hdr));
  597. msg_set_prevnode(hdr, own_node);
  598. msg_set_orignode(hdr, own_node);
  599. msg_set_size(hdr, hlen + dlen);
  600. skb_orphan(_skb);
  601. kfree_skb(_skb);
  602. return true;
  603. exit:
  604. kfree_skb(_skb);
  605. *skb = NULL;
  606. return false;
  607. }
  608. bool tipc_msg_skb_clone(struct sk_buff_head *msg, struct sk_buff_head *cpy)
  609. {
  610. struct sk_buff *skb, *_skb;
  611. skb_queue_walk(msg, skb) {
  612. _skb = skb_clone(skb, GFP_ATOMIC);
  613. if (!_skb) {
  614. __skb_queue_purge(cpy);
  615. pr_err_ratelimited("Failed to clone buffer chain\n");
  616. return false;
  617. }
  618. __skb_queue_tail(cpy, _skb);
  619. }
  620. return true;
  621. }
  622. /**
  623. * tipc_msg_lookup_dest(): try to find new destination for named message
  624. * @net: pointer to associated network namespace
  625. * @skb: the buffer containing the message.
  626. * @err: error code to be used by caller if lookup fails
  627. * Does not consume buffer
  628. * Return: true if a destination is found, false otherwise
  629. */
  630. bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err)
  631. {
  632. struct tipc_msg *msg = buf_msg(skb);
  633. u32 scope = msg_lookup_scope(msg);
  634. u32 self = tipc_own_addr(net);
  635. u32 inst = msg_nameinst(msg);
  636. struct tipc_socket_addr sk;
  637. struct tipc_uaddr ua;
  638. if (!msg_isdata(msg))
  639. return false;
  640. if (!msg_named(msg))
  641. return false;
  642. if (msg_errcode(msg))
  643. return false;
  644. *err = TIPC_ERR_NO_NAME;
  645. if (skb_linearize(skb))
  646. return false;
  647. msg = buf_msg(skb);
  648. if (msg_reroute_cnt(msg))
  649. return false;
  650. tipc_uaddr(&ua, TIPC_SERVICE_RANGE, scope,
  651. msg_nametype(msg), inst, inst);
  652. sk.node = tipc_scope2node(net, scope);
  653. if (!tipc_nametbl_lookup_anycast(net, &ua, &sk))
  654. return false;
  655. msg_incr_reroute_cnt(msg);
  656. if (sk.node != self)
  657. msg_set_prevnode(msg, self);
  658. msg_set_destnode(msg, sk.node);
  659. msg_set_destport(msg, sk.ref);
  660. *err = TIPC_OK;
  661. return true;
  662. }
  663. /* tipc_msg_assemble() - assemble chain of fragments into one message
  664. */
  665. bool tipc_msg_assemble(struct sk_buff_head *list)
  666. {
  667. struct sk_buff *skb, *tmp = NULL;
  668. if (skb_queue_len(list) == 1)
  669. return true;
  670. while ((skb = __skb_dequeue(list))) {
  671. skb->next = NULL;
  672. if (tipc_buf_append(&tmp, &skb)) {
  673. __skb_queue_tail(list, skb);
  674. return true;
  675. }
  676. if (!tmp)
  677. break;
  678. }
  679. __skb_queue_purge(list);
  680. __skb_queue_head_init(list);
  681. pr_warn("Failed do assemble buffer\n");
  682. return false;
  683. }
  684. /* tipc_msg_reassemble() - clone a buffer chain of fragments and
  685. * reassemble the clones into one message
  686. */
  687. bool tipc_msg_reassemble(struct sk_buff_head *list, struct sk_buff_head *rcvq)
  688. {
  689. struct sk_buff *skb, *_skb;
  690. struct sk_buff *frag = NULL;
  691. struct sk_buff *head = NULL;
  692. int hdr_len;
  693. /* Copy header if single buffer */
  694. if (skb_queue_len(list) == 1) {
  695. skb = skb_peek(list);
  696. hdr_len = skb_headroom(skb) + msg_hdr_sz(buf_msg(skb));
  697. _skb = __pskb_copy(skb, hdr_len, GFP_ATOMIC);
  698. if (!_skb)
  699. return false;
  700. __skb_queue_tail(rcvq, _skb);
  701. return true;
  702. }
  703. /* Clone all fragments and reassemble */
  704. skb_queue_walk(list, skb) {
  705. frag = skb_clone(skb, GFP_ATOMIC);
  706. if (!frag)
  707. goto error;
  708. frag->next = NULL;
  709. if (tipc_buf_append(&head, &frag))
  710. break;
  711. if (!head)
  712. goto error;
  713. }
  714. __skb_queue_tail(rcvq, frag);
  715. return true;
  716. error:
  717. pr_warn("Failed do clone local mcast rcv buffer\n");
  718. kfree_skb(head);
  719. return false;
  720. }
  721. bool tipc_msg_pskb_copy(u32 dst, struct sk_buff_head *msg,
  722. struct sk_buff_head *cpy)
  723. {
  724. struct sk_buff *skb, *_skb;
  725. skb_queue_walk(msg, skb) {
  726. _skb = pskb_copy(skb, GFP_ATOMIC);
  727. if (!_skb) {
  728. __skb_queue_purge(cpy);
  729. return false;
  730. }
  731. msg_set_destnode(buf_msg(_skb), dst);
  732. __skb_queue_tail(cpy, _skb);
  733. }
  734. return true;
  735. }
  736. /* tipc_skb_queue_sorted(); sort pkt into list according to sequence number
  737. * @list: list to be appended to
  738. * @seqno: sequence number of buffer to add
  739. * @skb: buffer to add
  740. */
  741. bool __tipc_skb_queue_sorted(struct sk_buff_head *list, u16 seqno,
  742. struct sk_buff *skb)
  743. {
  744. struct sk_buff *_skb, *tmp;
  745. if (skb_queue_empty(list) || less(seqno, buf_seqno(skb_peek(list)))) {
  746. __skb_queue_head(list, skb);
  747. return true;
  748. }
  749. if (more(seqno, buf_seqno(skb_peek_tail(list)))) {
  750. __skb_queue_tail(list, skb);
  751. return true;
  752. }
  753. skb_queue_walk_safe(list, _skb, tmp) {
  754. if (more(seqno, buf_seqno(_skb)))
  755. continue;
  756. if (seqno == buf_seqno(_skb))
  757. break;
  758. __skb_queue_before(list, _skb, skb);
  759. return true;
  760. }
  761. kfree_skb(skb);
  762. return false;
  763. }
  764. void tipc_skb_reject(struct net *net, int err, struct sk_buff *skb,
  765. struct sk_buff_head *xmitq)
  766. {
  767. if (tipc_msg_reverse(tipc_own_addr(net), &skb, err))
  768. __skb_queue_tail(xmitq, skb);
  769. }