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