msg.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 MAX_FORWARD_SIZE 1024
  43. #ifdef CONFIG_TIPC_CRYPTO
  44. #define BUF_HEADROOM ALIGN(((LL_MAX_HEADER + 48) + EHDR_MAX_SIZE), 16)
  45. #define BUF_OVERHEAD (BUF_HEADROOM + TIPC_AES_GCM_TAG_SIZE)
  46. #else
  47. #define BUF_HEADROOM (LL_MAX_HEADER + 48)
  48. #define BUF_OVERHEAD BUF_HEADROOM
  49. #endif
  50. const int one_page_mtu = PAGE_SIZE - SKB_DATA_ALIGN(BUF_OVERHEAD) -
  51. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  52. static unsigned int align(unsigned int i)
  53. {
  54. return (i + 3) & ~3u;
  55. }
  56. /**
  57. * tipc_buf_acquire - creates a TIPC message buffer
  58. * @size: message size (including TIPC header)
  59. *
  60. * Returns a new buffer with data pointers set to the specified size.
  61. *
  62. * NOTE: Headroom is reserved to allow prepending of a data link header.
  63. * There may also be unrequested tailroom present at the buffer's end.
  64. */
  65. struct sk_buff *tipc_buf_acquire(u32 size, gfp_t gfp)
  66. {
  67. struct sk_buff *skb;
  68. skb = alloc_skb_fclone(BUF_OVERHEAD + size, gfp);
  69. if (skb) {
  70. skb_reserve(skb, BUF_HEADROOM);
  71. skb_put(skb, size);
  72. skb->next = NULL;
  73. }
  74. return skb;
  75. }
  76. void tipc_msg_init(u32 own_node, struct tipc_msg *m, u32 user, u32 type,
  77. u32 hsize, u32 dnode)
  78. {
  79. memset(m, 0, hsize);
  80. msg_set_version(m);
  81. msg_set_user(m, user);
  82. msg_set_hdr_sz(m, hsize);
  83. msg_set_size(m, hsize);
  84. msg_set_prevnode(m, own_node);
  85. msg_set_type(m, type);
  86. if (hsize > SHORT_H_SIZE) {
  87. msg_set_orignode(m, own_node);
  88. msg_set_destnode(m, dnode);
  89. }
  90. }
  91. struct sk_buff *tipc_msg_create(uint user, uint type,
  92. uint hdr_sz, uint data_sz, u32 dnode,
  93. u32 onode, u32 dport, u32 oport, int errcode)
  94. {
  95. struct tipc_msg *msg;
  96. struct sk_buff *buf;
  97. buf = tipc_buf_acquire(hdr_sz + data_sz, GFP_ATOMIC);
  98. if (unlikely(!buf))
  99. return NULL;
  100. msg = buf_msg(buf);
  101. tipc_msg_init(onode, msg, user, type, hdr_sz, dnode);
  102. msg_set_size(msg, hdr_sz + data_sz);
  103. msg_set_origport(msg, oport);
  104. msg_set_destport(msg, dport);
  105. msg_set_errcode(msg, errcode);
  106. if (hdr_sz > SHORT_H_SIZE) {
  107. msg_set_orignode(msg, onode);
  108. msg_set_destnode(msg, dnode);
  109. }
  110. return buf;
  111. }
  112. /* tipc_buf_append(): Append a buffer to the fragment list of another buffer
  113. * @*headbuf: in: NULL for first frag, otherwise value returned from prev call
  114. * out: set when successful non-complete reassembly, otherwise NULL
  115. * @*buf: in: the buffer to append. Always defined
  116. * out: head buf after successful complete reassembly, otherwise NULL
  117. * Returns 1 when reassembly complete, otherwise 0
  118. */
  119. int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
  120. {
  121. struct sk_buff *head = *headbuf;
  122. struct sk_buff *frag = *buf;
  123. struct sk_buff *tail = NULL;
  124. struct tipc_msg *msg;
  125. u32 fragid;
  126. int delta;
  127. bool headstolen;
  128. if (!frag)
  129. goto err;
  130. msg = buf_msg(frag);
  131. fragid = msg_type(msg);
  132. frag->next = NULL;
  133. skb_pull(frag, msg_hdr_sz(msg));
  134. if (fragid == FIRST_FRAGMENT) {
  135. if (unlikely(head))
  136. goto err;
  137. *buf = NULL;
  138. if (skb_has_frag_list(frag) && __skb_linearize(frag))
  139. goto err;
  140. frag = skb_unshare(frag, GFP_ATOMIC);
  141. if (unlikely(!frag))
  142. goto err;
  143. head = *headbuf = frag;
  144. TIPC_SKB_CB(head)->tail = NULL;
  145. return 0;
  146. }
  147. if (!head)
  148. goto err;
  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. *buf = NULL;
  172. return 0;
  173. err:
  174. kfree_skb(*buf);
  175. kfree_skb(*headbuf);
  176. *buf = *headbuf = NULL;
  177. return 0;
  178. }
  179. /**
  180. * tipc_msg_append(): Append data to tail of an existing buffer queue
  181. * @_hdr: header to be used
  182. * @m: the data to be appended
  183. * @mss: max allowable size of buffer
  184. * @dlen: size of data to be appended
  185. * @txq: queue to appand to
  186. * Returns the number og 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. * Returns 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. * @dsz: Total length of user data
  330. * @pktmax: Max packet size that can be used
  331. * @list: Buffer or chain of buffers to be returned to caller
  332. *
  333. * Note that the recursive call we are making here is safe, since it can
  334. * logically go only one further level down.
  335. *
  336. * Returns message data size or errno: -ENOMEM, -EFAULT
  337. */
  338. int tipc_msg_build(struct tipc_msg *mhdr, struct msghdr *m, int offset,
  339. int dsz, int pktmax, struct sk_buff_head *list)
  340. {
  341. int mhsz = msg_hdr_sz(mhdr);
  342. struct tipc_msg pkthdr;
  343. int msz = mhsz + dsz;
  344. int pktrem = pktmax;
  345. struct sk_buff *skb;
  346. int drem = dsz;
  347. int pktno = 1;
  348. char *pktpos;
  349. int pktsz;
  350. int rc;
  351. msg_set_size(mhdr, msz);
  352. /* No fragmentation needed? */
  353. if (likely(msz <= pktmax)) {
  354. skb = tipc_buf_acquire(msz, GFP_KERNEL);
  355. /* Fall back to smaller MTU if node local message */
  356. if (unlikely(!skb)) {
  357. if (pktmax != MAX_MSG_SIZE)
  358. return -ENOMEM;
  359. rc = tipc_msg_build(mhdr, m, offset, dsz,
  360. one_page_mtu, list);
  361. if (rc != dsz)
  362. return rc;
  363. if (tipc_msg_assemble(list))
  364. return dsz;
  365. return -ENOMEM;
  366. }
  367. skb_orphan(skb);
  368. __skb_queue_tail(list, skb);
  369. skb_copy_to_linear_data(skb, mhdr, mhsz);
  370. pktpos = skb->data + mhsz;
  371. if (copy_from_iter_full(pktpos, dsz, &m->msg_iter))
  372. return dsz;
  373. rc = -EFAULT;
  374. goto error;
  375. }
  376. /* Prepare reusable fragment header */
  377. tipc_msg_init(msg_prevnode(mhdr), &pkthdr, MSG_FRAGMENTER,
  378. FIRST_FRAGMENT, INT_H_SIZE, msg_destnode(mhdr));
  379. msg_set_size(&pkthdr, pktmax);
  380. msg_set_fragm_no(&pkthdr, pktno);
  381. msg_set_importance(&pkthdr, msg_importance(mhdr));
  382. /* Prepare first fragment */
  383. skb = tipc_buf_acquire(pktmax, GFP_KERNEL);
  384. if (!skb)
  385. return -ENOMEM;
  386. skb_orphan(skb);
  387. __skb_queue_tail(list, skb);
  388. pktpos = skb->data;
  389. skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
  390. pktpos += INT_H_SIZE;
  391. pktrem -= INT_H_SIZE;
  392. skb_copy_to_linear_data_offset(skb, INT_H_SIZE, mhdr, mhsz);
  393. pktpos += mhsz;
  394. pktrem -= mhsz;
  395. do {
  396. if (drem < pktrem)
  397. pktrem = drem;
  398. if (!copy_from_iter_full(pktpos, pktrem, &m->msg_iter)) {
  399. rc = -EFAULT;
  400. goto error;
  401. }
  402. drem -= pktrem;
  403. if (!drem)
  404. break;
  405. /* Prepare new fragment: */
  406. if (drem < (pktmax - INT_H_SIZE))
  407. pktsz = drem + INT_H_SIZE;
  408. else
  409. pktsz = pktmax;
  410. skb = tipc_buf_acquire(pktsz, GFP_KERNEL);
  411. if (!skb) {
  412. rc = -ENOMEM;
  413. goto error;
  414. }
  415. skb_orphan(skb);
  416. __skb_queue_tail(list, skb);
  417. msg_set_type(&pkthdr, FRAGMENT);
  418. msg_set_size(&pkthdr, pktsz);
  419. msg_set_fragm_no(&pkthdr, ++pktno);
  420. skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
  421. pktpos = skb->data + INT_H_SIZE;
  422. pktrem = pktsz - INT_H_SIZE;
  423. } while (1);
  424. msg_set_type(buf_msg(skb), LAST_FRAGMENT);
  425. return dsz;
  426. error:
  427. __skb_queue_purge(list);
  428. __skb_queue_head_init(list);
  429. return rc;
  430. }
  431. /**
  432. * tipc_msg_bundle - Append contents of a buffer to tail of an existing one
  433. * @bskb: the bundle buffer to append to
  434. * @msg: message to be appended
  435. * @max: max allowable size for the bundle buffer
  436. *
  437. * Returns "true" if bundling has been performed, otherwise "false"
  438. */
  439. static bool tipc_msg_bundle(struct sk_buff *bskb, struct tipc_msg *msg,
  440. u32 max)
  441. {
  442. struct tipc_msg *bmsg = buf_msg(bskb);
  443. u32 msz, bsz, offset, pad;
  444. msz = msg_size(msg);
  445. bsz = msg_size(bmsg);
  446. offset = align(bsz);
  447. pad = offset - bsz;
  448. if (unlikely(skb_tailroom(bskb) < (pad + msz)))
  449. return false;
  450. if (unlikely(max < (offset + msz)))
  451. return false;
  452. skb_put(bskb, pad + msz);
  453. skb_copy_to_linear_data_offset(bskb, offset, msg, msz);
  454. msg_set_size(bmsg, offset + msz);
  455. msg_set_msgcnt(bmsg, msg_msgcnt(bmsg) + 1);
  456. return true;
  457. }
  458. /**
  459. * tipc_msg_try_bundle - Try to bundle a new message to the last one
  460. * @tskb: the last/target message to which the new one will be appended
  461. * @skb: the new message skb pointer
  462. * @mss: max message size (header inclusive)
  463. * @dnode: destination node for the message
  464. * @new_bundle: if this call made a new bundle or not
  465. *
  466. * Return: "true" if the new message skb is potential for bundling this time or
  467. * later, in the case a bundling has been done this time, the skb is consumed
  468. * (the skb pointer = NULL).
  469. * Otherwise, "false" if the skb cannot be bundled at all.
  470. */
  471. bool tipc_msg_try_bundle(struct sk_buff *tskb, struct sk_buff **skb, u32 mss,
  472. u32 dnode, bool *new_bundle)
  473. {
  474. struct tipc_msg *msg, *inner, *outer;
  475. u32 tsz;
  476. /* First, check if the new buffer is suitable for bundling */
  477. msg = buf_msg(*skb);
  478. if (msg_user(msg) == MSG_FRAGMENTER)
  479. return false;
  480. if (msg_user(msg) == TUNNEL_PROTOCOL)
  481. return false;
  482. if (msg_user(msg) == BCAST_PROTOCOL)
  483. return false;
  484. if (mss <= INT_H_SIZE + msg_size(msg))
  485. return false;
  486. /* Ok, but the last/target buffer can be empty? */
  487. if (unlikely(!tskb))
  488. return true;
  489. /* Is it a bundle already? Try to bundle the new message to it */
  490. if (msg_user(buf_msg(tskb)) == MSG_BUNDLER) {
  491. *new_bundle = false;
  492. goto bundle;
  493. }
  494. /* Make a new bundle of the two messages if possible */
  495. tsz = msg_size(buf_msg(tskb));
  496. if (unlikely(mss < align(INT_H_SIZE + tsz) + msg_size(msg)))
  497. return true;
  498. if (unlikely(pskb_expand_head(tskb, INT_H_SIZE, mss - tsz - INT_H_SIZE,
  499. GFP_ATOMIC)))
  500. return true;
  501. inner = buf_msg(tskb);
  502. skb_push(tskb, INT_H_SIZE);
  503. outer = buf_msg(tskb);
  504. tipc_msg_init(msg_prevnode(inner), outer, MSG_BUNDLER, 0, INT_H_SIZE,
  505. dnode);
  506. msg_set_importance(outer, msg_importance(inner));
  507. msg_set_size(outer, INT_H_SIZE + tsz);
  508. msg_set_msgcnt(outer, 1);
  509. *new_bundle = true;
  510. bundle:
  511. if (likely(tipc_msg_bundle(tskb, msg, mss))) {
  512. consume_skb(*skb);
  513. *skb = NULL;
  514. }
  515. return true;
  516. }
  517. /**
  518. * tipc_msg_extract(): extract bundled inner packet from buffer
  519. * @skb: buffer to be extracted from.
  520. * @iskb: extracted inner buffer, to be returned
  521. * @pos: position in outer message of msg to be extracted.
  522. * Returns position of next msg
  523. * Consumes outer buffer when last packet extracted
  524. * Returns true when there is an extracted buffer, otherwise false
  525. */
  526. bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos)
  527. {
  528. struct tipc_msg *hdr, *ihdr;
  529. int imsz;
  530. *iskb = NULL;
  531. if (unlikely(skb_linearize(skb)))
  532. goto none;
  533. hdr = buf_msg(skb);
  534. if (unlikely(*pos > (msg_data_sz(hdr) - MIN_H_SIZE)))
  535. goto none;
  536. ihdr = (struct tipc_msg *)(msg_data(hdr) + *pos);
  537. imsz = msg_size(ihdr);
  538. if ((*pos + imsz) > msg_data_sz(hdr))
  539. goto none;
  540. *iskb = tipc_buf_acquire(imsz, GFP_ATOMIC);
  541. if (!*iskb)
  542. goto none;
  543. skb_copy_to_linear_data(*iskb, ihdr, imsz);
  544. if (unlikely(!tipc_msg_validate(iskb)))
  545. goto none;
  546. *pos += align(imsz);
  547. return true;
  548. none:
  549. kfree_skb(skb);
  550. kfree_skb(*iskb);
  551. *iskb = NULL;
  552. return false;
  553. }
  554. /**
  555. * tipc_msg_reverse(): swap source and destination addresses and add error code
  556. * @own_node: originating node id for reversed message
  557. * @skb: buffer containing message to be reversed; will be consumed
  558. * @err: error code to be set in message, if any
  559. * Replaces consumed buffer with new one when successful
  560. * Returns true if success, otherwise false
  561. */
  562. bool tipc_msg_reverse(u32 own_node, struct sk_buff **skb, int err)
  563. {
  564. struct sk_buff *_skb = *skb;
  565. struct tipc_msg *_hdr, *hdr;
  566. int hlen, dlen;
  567. if (skb_linearize(_skb))
  568. goto exit;
  569. _hdr = buf_msg(_skb);
  570. dlen = min_t(uint, msg_data_sz(_hdr), MAX_FORWARD_SIZE);
  571. hlen = msg_hdr_sz(_hdr);
  572. if (msg_dest_droppable(_hdr))
  573. goto exit;
  574. if (msg_errcode(_hdr))
  575. goto exit;
  576. /* Never return SHORT header */
  577. if (hlen == SHORT_H_SIZE)
  578. hlen = BASIC_H_SIZE;
  579. /* Don't return data along with SYN+, - sender has a clone */
  580. if (msg_is_syn(_hdr) && err == TIPC_ERR_OVERLOAD)
  581. dlen = 0;
  582. /* Allocate new buffer to return */
  583. *skb = tipc_buf_acquire(hlen + dlen, GFP_ATOMIC);
  584. if (!*skb)
  585. goto exit;
  586. memcpy((*skb)->data, _skb->data, msg_hdr_sz(_hdr));
  587. memcpy((*skb)->data + hlen, msg_data(_hdr), dlen);
  588. /* Build reverse header in new buffer */
  589. hdr = buf_msg(*skb);
  590. msg_set_hdr_sz(hdr, hlen);
  591. msg_set_errcode(hdr, err);
  592. msg_set_non_seq(hdr, 0);
  593. msg_set_origport(hdr, msg_destport(_hdr));
  594. msg_set_destport(hdr, msg_origport(_hdr));
  595. msg_set_destnode(hdr, msg_prevnode(_hdr));
  596. msg_set_prevnode(hdr, own_node);
  597. msg_set_orignode(hdr, own_node);
  598. msg_set_size(hdr, hlen + dlen);
  599. skb_orphan(_skb);
  600. kfree_skb(_skb);
  601. return true;
  602. exit:
  603. kfree_skb(_skb);
  604. *skb = NULL;
  605. return false;
  606. }
  607. bool tipc_msg_skb_clone(struct sk_buff_head *msg, struct sk_buff_head *cpy)
  608. {
  609. struct sk_buff *skb, *_skb;
  610. skb_queue_walk(msg, skb) {
  611. _skb = skb_clone(skb, GFP_ATOMIC);
  612. if (!_skb) {
  613. __skb_queue_purge(cpy);
  614. pr_err_ratelimited("Failed to clone buffer chain\n");
  615. return false;
  616. }
  617. __skb_queue_tail(cpy, _skb);
  618. }
  619. return true;
  620. }
  621. /**
  622. * tipc_msg_lookup_dest(): try to find new destination for named message
  623. * @skb: the buffer containing the message.
  624. * @err: error code to be used by caller if lookup fails
  625. * Does not consume buffer
  626. * Returns true if a destination is found, false otherwise
  627. */
  628. bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err)
  629. {
  630. struct tipc_msg *msg = buf_msg(skb);
  631. u32 dport, dnode;
  632. u32 onode = tipc_own_addr(net);
  633. if (!msg_isdata(msg))
  634. return false;
  635. if (!msg_named(msg))
  636. return false;
  637. if (msg_errcode(msg))
  638. return false;
  639. *err = TIPC_ERR_NO_NAME;
  640. if (skb_linearize(skb))
  641. return false;
  642. msg = buf_msg(skb);
  643. if (msg_reroute_cnt(msg))
  644. return false;
  645. dnode = tipc_scope2node(net, msg_lookup_scope(msg));
  646. dport = tipc_nametbl_translate(net, msg_nametype(msg),
  647. msg_nameinst(msg), &dnode);
  648. if (!dport)
  649. return false;
  650. msg_incr_reroute_cnt(msg);
  651. if (dnode != onode)
  652. msg_set_prevnode(msg, onode);
  653. msg_set_destnode(msg, dnode);
  654. msg_set_destport(msg, dport);
  655. *err = TIPC_OK;
  656. return true;
  657. }
  658. /* tipc_msg_assemble() - assemble chain of fragments into one message
  659. */
  660. bool tipc_msg_assemble(struct sk_buff_head *list)
  661. {
  662. struct sk_buff *skb, *tmp = NULL;
  663. if (skb_queue_len(list) == 1)
  664. return true;
  665. while ((skb = __skb_dequeue(list))) {
  666. skb->next = NULL;
  667. if (tipc_buf_append(&tmp, &skb)) {
  668. __skb_queue_tail(list, skb);
  669. return true;
  670. }
  671. if (!tmp)
  672. break;
  673. }
  674. __skb_queue_purge(list);
  675. __skb_queue_head_init(list);
  676. pr_warn("Failed do assemble buffer\n");
  677. return false;
  678. }
  679. /* tipc_msg_reassemble() - clone a buffer chain of fragments and
  680. * reassemble the clones into one message
  681. */
  682. bool tipc_msg_reassemble(struct sk_buff_head *list, struct sk_buff_head *rcvq)
  683. {
  684. struct sk_buff *skb, *_skb;
  685. struct sk_buff *frag = NULL;
  686. struct sk_buff *head = NULL;
  687. int hdr_len;
  688. /* Copy header if single buffer */
  689. if (skb_queue_len(list) == 1) {
  690. skb = skb_peek(list);
  691. hdr_len = skb_headroom(skb) + msg_hdr_sz(buf_msg(skb));
  692. _skb = __pskb_copy(skb, hdr_len, GFP_ATOMIC);
  693. if (!_skb)
  694. return false;
  695. __skb_queue_tail(rcvq, _skb);
  696. return true;
  697. }
  698. /* Clone all fragments and reassemble */
  699. skb_queue_walk(list, skb) {
  700. frag = skb_clone(skb, GFP_ATOMIC);
  701. if (!frag)
  702. goto error;
  703. frag->next = NULL;
  704. if (tipc_buf_append(&head, &frag))
  705. break;
  706. if (!head)
  707. goto error;
  708. }
  709. __skb_queue_tail(rcvq, frag);
  710. return true;
  711. error:
  712. pr_warn("Failed do clone local mcast rcv buffer\n");
  713. kfree_skb(head);
  714. return false;
  715. }
  716. bool tipc_msg_pskb_copy(u32 dst, struct sk_buff_head *msg,
  717. struct sk_buff_head *cpy)
  718. {
  719. struct sk_buff *skb, *_skb;
  720. skb_queue_walk(msg, skb) {
  721. _skb = pskb_copy(skb, GFP_ATOMIC);
  722. if (!_skb) {
  723. __skb_queue_purge(cpy);
  724. return false;
  725. }
  726. msg_set_destnode(buf_msg(_skb), dst);
  727. __skb_queue_tail(cpy, _skb);
  728. }
  729. return true;
  730. }
  731. /* tipc_skb_queue_sorted(); sort pkt into list according to sequence number
  732. * @list: list to be appended to
  733. * @seqno: sequence number of buffer to add
  734. * @skb: buffer to add
  735. */
  736. bool __tipc_skb_queue_sorted(struct sk_buff_head *list, u16 seqno,
  737. struct sk_buff *skb)
  738. {
  739. struct sk_buff *_skb, *tmp;
  740. if (skb_queue_empty(list) || less(seqno, buf_seqno(skb_peek(list)))) {
  741. __skb_queue_head(list, skb);
  742. return true;
  743. }
  744. if (more(seqno, buf_seqno(skb_peek_tail(list)))) {
  745. __skb_queue_tail(list, skb);
  746. return true;
  747. }
  748. skb_queue_walk_safe(list, _skb, tmp) {
  749. if (more(seqno, buf_seqno(_skb)))
  750. continue;
  751. if (seqno == buf_seqno(_skb))
  752. break;
  753. __skb_queue_before(list, _skb, skb);
  754. return true;
  755. }
  756. kfree_skb(skb);
  757. return false;
  758. }
  759. void tipc_skb_reject(struct net *net, int err, struct sk_buff *skb,
  760. struct sk_buff_head *xmitq)
  761. {
  762. if (tipc_msg_reverse(tipc_own_addr(net), &skb, err))
  763. __skb_queue_tail(xmitq, skb);
  764. }