msg.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. #define MAX_FORWARD_SIZE 1024
  42. #define BUF_HEADROOM (LL_MAX_HEADER + 48)
  43. #define BUF_TAILROOM 16
  44. static unsigned int align(unsigned int i)
  45. {
  46. return (i + 3) & ~3u;
  47. }
  48. /**
  49. * tipc_buf_acquire - creates a TIPC message buffer
  50. * @size: message size (including TIPC header)
  51. *
  52. * Returns a new buffer with data pointers set to the specified size.
  53. *
  54. * NOTE: Headroom is reserved to allow prepending of a data link header.
  55. * There may also be unrequested tailroom present at the buffer's end.
  56. */
  57. struct sk_buff *tipc_buf_acquire(u32 size, gfp_t gfp)
  58. {
  59. struct sk_buff *skb;
  60. unsigned int buf_size = (BUF_HEADROOM + size + 3) & ~3u;
  61. skb = alloc_skb_fclone(buf_size, gfp);
  62. if (skb) {
  63. skb_reserve(skb, BUF_HEADROOM);
  64. skb_put(skb, size);
  65. skb->next = NULL;
  66. }
  67. return skb;
  68. }
  69. void tipc_msg_init(u32 own_node, struct tipc_msg *m, u32 user, u32 type,
  70. u32 hsize, u32 dnode)
  71. {
  72. memset(m, 0, hsize);
  73. msg_set_version(m);
  74. msg_set_user(m, user);
  75. msg_set_hdr_sz(m, hsize);
  76. msg_set_size(m, hsize);
  77. msg_set_prevnode(m, own_node);
  78. msg_set_type(m, type);
  79. if (hsize > SHORT_H_SIZE) {
  80. msg_set_orignode(m, own_node);
  81. msg_set_destnode(m, dnode);
  82. }
  83. }
  84. struct sk_buff *tipc_msg_create(uint user, uint type,
  85. uint hdr_sz, uint data_sz, u32 dnode,
  86. u32 onode, u32 dport, u32 oport, int errcode)
  87. {
  88. struct tipc_msg *msg;
  89. struct sk_buff *buf;
  90. buf = tipc_buf_acquire(hdr_sz + data_sz, GFP_ATOMIC);
  91. if (unlikely(!buf))
  92. return NULL;
  93. msg = buf_msg(buf);
  94. tipc_msg_init(onode, msg, user, type, hdr_sz, dnode);
  95. msg_set_size(msg, hdr_sz + data_sz);
  96. msg_set_origport(msg, oport);
  97. msg_set_destport(msg, dport);
  98. msg_set_errcode(msg, errcode);
  99. if (hdr_sz > SHORT_H_SIZE) {
  100. msg_set_orignode(msg, onode);
  101. msg_set_destnode(msg, dnode);
  102. }
  103. return buf;
  104. }
  105. /* tipc_buf_append(): Append a buffer to the fragment list of another buffer
  106. * @*headbuf: in: NULL for first frag, otherwise value returned from prev call
  107. * out: set when successful non-complete reassembly, otherwise NULL
  108. * @*buf: in: the buffer to append. Always defined
  109. * out: head buf after successful complete reassembly, otherwise NULL
  110. * Returns 1 when reassembly complete, otherwise 0
  111. */
  112. int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
  113. {
  114. struct sk_buff *head = *headbuf;
  115. struct sk_buff *frag = *buf;
  116. struct sk_buff *tail = NULL;
  117. struct tipc_msg *msg;
  118. u32 fragid;
  119. int delta;
  120. bool headstolen;
  121. if (!frag)
  122. goto err;
  123. msg = buf_msg(frag);
  124. fragid = msg_type(msg);
  125. frag->next = NULL;
  126. skb_pull(frag, msg_hdr_sz(msg));
  127. if (fragid == FIRST_FRAGMENT) {
  128. if (unlikely(head))
  129. goto err;
  130. *buf = NULL;
  131. if (skb_has_frag_list(frag) && __skb_linearize(frag))
  132. goto err;
  133. frag = skb_unshare(frag, GFP_ATOMIC);
  134. if (unlikely(!frag))
  135. goto err;
  136. head = *headbuf = frag;
  137. TIPC_SKB_CB(head)->tail = NULL;
  138. return 0;
  139. }
  140. if (!head)
  141. goto err;
  142. if (skb_try_coalesce(head, frag, &headstolen, &delta)) {
  143. kfree_skb_partial(frag, headstolen);
  144. } else {
  145. tail = TIPC_SKB_CB(head)->tail;
  146. if (!skb_has_frag_list(head))
  147. skb_shinfo(head)->frag_list = frag;
  148. else
  149. tail->next = frag;
  150. head->truesize += frag->truesize;
  151. head->data_len += frag->len;
  152. head->len += frag->len;
  153. TIPC_SKB_CB(head)->tail = frag;
  154. }
  155. if (fragid == LAST_FRAGMENT) {
  156. TIPC_SKB_CB(head)->validated = false;
  157. if (unlikely(!tipc_msg_validate(head)))
  158. goto err;
  159. *buf = head;
  160. TIPC_SKB_CB(head)->tail = NULL;
  161. *headbuf = NULL;
  162. return 1;
  163. }
  164. *buf = NULL;
  165. return 0;
  166. err:
  167. kfree_skb(*buf);
  168. kfree_skb(*headbuf);
  169. *buf = *headbuf = NULL;
  170. return 0;
  171. }
  172. /* tipc_msg_validate - validate basic format of received message
  173. *
  174. * This routine ensures a TIPC message has an acceptable header, and at least
  175. * as much data as the header indicates it should. The routine also ensures
  176. * that the entire message header is stored in the main fragment of the message
  177. * buffer, to simplify future access to message header fields.
  178. *
  179. * Note: Having extra info present in the message header or data areas is OK.
  180. * TIPC will ignore the excess, under the assumption that it is optional info
  181. * introduced by a later release of the protocol.
  182. */
  183. bool tipc_msg_validate(struct sk_buff *skb)
  184. {
  185. struct tipc_msg *msg;
  186. int msz, hsz;
  187. if (unlikely(TIPC_SKB_CB(skb)->validated))
  188. return true;
  189. if (unlikely(!pskb_may_pull(skb, MIN_H_SIZE)))
  190. return false;
  191. hsz = msg_hdr_sz(buf_msg(skb));
  192. if (unlikely(hsz < MIN_H_SIZE) || (hsz > MAX_H_SIZE))
  193. return false;
  194. if (unlikely(!pskb_may_pull(skb, hsz)))
  195. return false;
  196. msg = buf_msg(skb);
  197. if (unlikely(msg_version(msg) != TIPC_VERSION))
  198. return false;
  199. msz = msg_size(msg);
  200. if (unlikely(msz < hsz))
  201. return false;
  202. if (unlikely((msz - hsz) > TIPC_MAX_USER_MSG_SIZE))
  203. return false;
  204. if (unlikely(skb->len < msz))
  205. return false;
  206. TIPC_SKB_CB(skb)->validated = true;
  207. return true;
  208. }
  209. /**
  210. * tipc_msg_build - create buffer chain containing specified header and data
  211. * @mhdr: Message header, to be prepended to data
  212. * @m: User message
  213. * @dsz: Total length of user data
  214. * @pktmax: Max packet size that can be used
  215. * @list: Buffer or chain of buffers to be returned to caller
  216. *
  217. * Returns message data size or errno: -ENOMEM, -EFAULT
  218. */
  219. int tipc_msg_build(struct tipc_msg *mhdr, struct msghdr *m,
  220. int offset, int dsz, int pktmax, struct sk_buff_head *list)
  221. {
  222. int mhsz = msg_hdr_sz(mhdr);
  223. int msz = mhsz + dsz;
  224. int pktno = 1;
  225. int pktsz;
  226. int pktrem = pktmax;
  227. int drem = dsz;
  228. struct tipc_msg pkthdr;
  229. struct sk_buff *skb;
  230. char *pktpos;
  231. int rc;
  232. msg_set_size(mhdr, msz);
  233. /* No fragmentation needed? */
  234. if (likely(msz <= pktmax)) {
  235. skb = tipc_buf_acquire(msz, GFP_KERNEL);
  236. if (unlikely(!skb))
  237. return -ENOMEM;
  238. skb_orphan(skb);
  239. __skb_queue_tail(list, skb);
  240. skb_copy_to_linear_data(skb, mhdr, mhsz);
  241. pktpos = skb->data + mhsz;
  242. if (copy_from_iter_full(pktpos, dsz, &m->msg_iter))
  243. return dsz;
  244. rc = -EFAULT;
  245. goto error;
  246. }
  247. /* Prepare reusable fragment header */
  248. tipc_msg_init(msg_prevnode(mhdr), &pkthdr, MSG_FRAGMENTER,
  249. FIRST_FRAGMENT, INT_H_SIZE, msg_destnode(mhdr));
  250. msg_set_size(&pkthdr, pktmax);
  251. msg_set_fragm_no(&pkthdr, pktno);
  252. msg_set_importance(&pkthdr, msg_importance(mhdr));
  253. /* Prepare first fragment */
  254. skb = tipc_buf_acquire(pktmax, GFP_KERNEL);
  255. if (!skb)
  256. return -ENOMEM;
  257. skb_orphan(skb);
  258. __skb_queue_tail(list, skb);
  259. pktpos = skb->data;
  260. skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
  261. pktpos += INT_H_SIZE;
  262. pktrem -= INT_H_SIZE;
  263. skb_copy_to_linear_data_offset(skb, INT_H_SIZE, mhdr, mhsz);
  264. pktpos += mhsz;
  265. pktrem -= mhsz;
  266. do {
  267. if (drem < pktrem)
  268. pktrem = drem;
  269. if (!copy_from_iter_full(pktpos, pktrem, &m->msg_iter)) {
  270. rc = -EFAULT;
  271. goto error;
  272. }
  273. drem -= pktrem;
  274. if (!drem)
  275. break;
  276. /* Prepare new fragment: */
  277. if (drem < (pktmax - INT_H_SIZE))
  278. pktsz = drem + INT_H_SIZE;
  279. else
  280. pktsz = pktmax;
  281. skb = tipc_buf_acquire(pktsz, GFP_KERNEL);
  282. if (!skb) {
  283. rc = -ENOMEM;
  284. goto error;
  285. }
  286. skb_orphan(skb);
  287. __skb_queue_tail(list, skb);
  288. msg_set_type(&pkthdr, FRAGMENT);
  289. msg_set_size(&pkthdr, pktsz);
  290. msg_set_fragm_no(&pkthdr, ++pktno);
  291. skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
  292. pktpos = skb->data + INT_H_SIZE;
  293. pktrem = pktsz - INT_H_SIZE;
  294. } while (1);
  295. msg_set_type(buf_msg(skb), LAST_FRAGMENT);
  296. return dsz;
  297. error:
  298. __skb_queue_purge(list);
  299. __skb_queue_head_init(list);
  300. return rc;
  301. }
  302. /**
  303. * tipc_msg_bundle(): Append contents of a buffer to tail of an existing one
  304. * @skb: the buffer to append to ("bundle")
  305. * @msg: message to be appended
  306. * @mtu: max allowable size for the bundle buffer
  307. * Consumes buffer if successful
  308. * Returns true if bundling could be performed, otherwise false
  309. */
  310. bool tipc_msg_bundle(struct sk_buff *skb, struct tipc_msg *msg, u32 mtu)
  311. {
  312. struct tipc_msg *bmsg;
  313. unsigned int bsz;
  314. unsigned int msz = msg_size(msg);
  315. u32 start, pad;
  316. u32 max = mtu - INT_H_SIZE;
  317. if (likely(msg_user(msg) == MSG_FRAGMENTER))
  318. return false;
  319. if (!skb)
  320. return false;
  321. bmsg = buf_msg(skb);
  322. bsz = msg_size(bmsg);
  323. start = align(bsz);
  324. pad = start - bsz;
  325. if (unlikely(msg_user(msg) == TUNNEL_PROTOCOL))
  326. return false;
  327. if (unlikely(msg_user(msg) == BCAST_PROTOCOL))
  328. return false;
  329. if (unlikely(msg_user(bmsg) != MSG_BUNDLER))
  330. return false;
  331. if (unlikely(skb_tailroom(skb) < (pad + msz)))
  332. return false;
  333. if (unlikely(max < (start + msz)))
  334. return false;
  335. if ((msg_importance(msg) < TIPC_SYSTEM_IMPORTANCE) &&
  336. (msg_importance(bmsg) == TIPC_SYSTEM_IMPORTANCE))
  337. return false;
  338. skb_put(skb, pad + msz);
  339. skb_copy_to_linear_data_offset(skb, start, msg, msz);
  340. msg_set_size(bmsg, start + msz);
  341. msg_set_msgcnt(bmsg, msg_msgcnt(bmsg) + 1);
  342. return true;
  343. }
  344. /**
  345. * tipc_msg_extract(): extract bundled inner packet from buffer
  346. * @skb: buffer to be extracted from.
  347. * @iskb: extracted inner buffer, to be returned
  348. * @pos: position in outer message of msg to be extracted.
  349. * Returns position of next msg
  350. * Consumes outer buffer when last packet extracted
  351. * Returns true when when there is an extracted buffer, otherwise false
  352. */
  353. bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos)
  354. {
  355. struct tipc_msg *msg;
  356. int imsz, offset;
  357. *iskb = NULL;
  358. if (unlikely(skb_linearize(skb)))
  359. goto none;
  360. msg = buf_msg(skb);
  361. offset = msg_hdr_sz(msg) + *pos;
  362. if (unlikely(offset > (msg_size(msg) - MIN_H_SIZE)))
  363. goto none;
  364. *iskb = skb_clone(skb, GFP_ATOMIC);
  365. if (unlikely(!*iskb))
  366. goto none;
  367. skb_pull(*iskb, offset);
  368. imsz = msg_size(buf_msg(*iskb));
  369. skb_trim(*iskb, imsz);
  370. if (unlikely(!tipc_msg_validate(*iskb)))
  371. goto none;
  372. *pos += align(imsz);
  373. return true;
  374. none:
  375. kfree_skb(skb);
  376. kfree_skb(*iskb);
  377. *iskb = NULL;
  378. return false;
  379. }
  380. /**
  381. * tipc_msg_make_bundle(): Create bundle buf and append message to its tail
  382. * @list: the buffer chain, where head is the buffer to replace/append
  383. * @skb: buffer to be created, appended to and returned in case of success
  384. * @msg: message to be appended
  385. * @mtu: max allowable size for the bundle buffer, inclusive header
  386. * @dnode: destination node for message. (Not always present in header)
  387. * Returns true if success, otherwise false
  388. */
  389. bool tipc_msg_make_bundle(struct sk_buff **skb, struct tipc_msg *msg,
  390. u32 mtu, u32 dnode)
  391. {
  392. struct sk_buff *_skb;
  393. struct tipc_msg *bmsg;
  394. u32 msz = msg_size(msg);
  395. u32 max = mtu - INT_H_SIZE;
  396. if (msg_user(msg) == MSG_FRAGMENTER)
  397. return false;
  398. if (msg_user(msg) == TUNNEL_PROTOCOL)
  399. return false;
  400. if (msg_user(msg) == BCAST_PROTOCOL)
  401. return false;
  402. if (msz > (max / 2))
  403. return false;
  404. _skb = tipc_buf_acquire(max, GFP_ATOMIC);
  405. if (!_skb)
  406. return false;
  407. skb_trim(_skb, INT_H_SIZE);
  408. bmsg = buf_msg(_skb);
  409. tipc_msg_init(msg_prevnode(msg), bmsg, MSG_BUNDLER, 0,
  410. INT_H_SIZE, dnode);
  411. msg_set_importance(bmsg, msg_importance(msg));
  412. msg_set_seqno(bmsg, msg_seqno(msg));
  413. msg_set_ack(bmsg, msg_ack(msg));
  414. msg_set_bcast_ack(bmsg, msg_bcast_ack(msg));
  415. tipc_msg_bundle(_skb, msg, mtu);
  416. *skb = _skb;
  417. return true;
  418. }
  419. /**
  420. * tipc_msg_reverse(): swap source and destination addresses and add error code
  421. * @own_node: originating node id for reversed message
  422. * @skb: buffer containing message to be reversed; may be replaced.
  423. * @err: error code to be set in message, if any
  424. * Consumes buffer at failure
  425. * Returns true if success, otherwise false
  426. */
  427. bool tipc_msg_reverse(u32 own_node, struct sk_buff **skb, int err)
  428. {
  429. struct sk_buff *_skb = *skb;
  430. struct tipc_msg *hdr;
  431. struct tipc_msg ohdr;
  432. int dlen;
  433. if (skb_linearize(_skb))
  434. goto exit;
  435. hdr = buf_msg(_skb);
  436. dlen = min_t(uint, msg_data_sz(hdr), MAX_FORWARD_SIZE);
  437. if (msg_dest_droppable(hdr))
  438. goto exit;
  439. if (msg_errcode(hdr))
  440. goto exit;
  441. /* Take a copy of original header before altering message */
  442. memcpy(&ohdr, hdr, msg_hdr_sz(hdr));
  443. /* Never return SHORT header; expand by replacing buffer if necessary */
  444. if (msg_short(hdr)) {
  445. *skb = tipc_buf_acquire(BASIC_H_SIZE + dlen, GFP_ATOMIC);
  446. if (!*skb)
  447. goto exit;
  448. memcpy((*skb)->data + BASIC_H_SIZE, msg_data(hdr), dlen);
  449. kfree_skb(_skb);
  450. _skb = *skb;
  451. hdr = buf_msg(_skb);
  452. memcpy(hdr, &ohdr, BASIC_H_SIZE);
  453. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  454. }
  455. if (skb_cloned(_skb) &&
  456. pskb_expand_head(_skb, BUF_HEADROOM, BUF_TAILROOM, GFP_ATOMIC))
  457. goto exit;
  458. /* reassign after skb header modifications */
  459. hdr = buf_msg(_skb);
  460. /* Now reverse the concerned fields */
  461. msg_set_errcode(hdr, err);
  462. msg_set_non_seq(hdr, 0);
  463. msg_set_origport(hdr, msg_destport(&ohdr));
  464. msg_set_destport(hdr, msg_origport(&ohdr));
  465. msg_set_destnode(hdr, msg_prevnode(&ohdr));
  466. msg_set_prevnode(hdr, own_node);
  467. msg_set_orignode(hdr, own_node);
  468. msg_set_size(hdr, msg_hdr_sz(hdr) + dlen);
  469. skb_trim(_skb, msg_size(hdr));
  470. skb_orphan(_skb);
  471. return true;
  472. exit:
  473. kfree_skb(_skb);
  474. *skb = NULL;
  475. return false;
  476. }
  477. /**
  478. * tipc_msg_lookup_dest(): try to find new destination for named message
  479. * @skb: the buffer containing the message.
  480. * @err: error code to be used by caller if lookup fails
  481. * Does not consume buffer
  482. * Returns true if a destination is found, false otherwise
  483. */
  484. bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err)
  485. {
  486. struct tipc_msg *msg = buf_msg(skb);
  487. u32 dport, dnode;
  488. u32 onode = tipc_own_addr(net);
  489. if (!msg_isdata(msg))
  490. return false;
  491. if (!msg_named(msg))
  492. return false;
  493. if (msg_errcode(msg))
  494. return false;
  495. *err = TIPC_ERR_NO_NAME;
  496. if (skb_linearize(skb))
  497. return false;
  498. msg = buf_msg(skb);
  499. if (msg_reroute_cnt(msg))
  500. return false;
  501. dnode = addr_domain(net, msg_lookup_scope(msg));
  502. dport = tipc_nametbl_translate(net, msg_nametype(msg),
  503. msg_nameinst(msg), &dnode);
  504. if (!dport)
  505. return false;
  506. msg_incr_reroute_cnt(msg);
  507. if (dnode != onode)
  508. msg_set_prevnode(msg, onode);
  509. msg_set_destnode(msg, dnode);
  510. msg_set_destport(msg, dport);
  511. *err = TIPC_OK;
  512. if (!skb_cloned(skb))
  513. return true;
  514. /* Unclone buffer in case it was bundled */
  515. if (pskb_expand_head(skb, BUF_HEADROOM, BUF_TAILROOM, GFP_ATOMIC))
  516. return false;
  517. return true;
  518. }
  519. /* tipc_msg_reassemble() - clone a buffer chain of fragments and
  520. * reassemble the clones into one message
  521. */
  522. bool tipc_msg_reassemble(struct sk_buff_head *list, struct sk_buff_head *rcvq)
  523. {
  524. struct sk_buff *skb, *_skb;
  525. struct sk_buff *frag = NULL;
  526. struct sk_buff *head = NULL;
  527. int hdr_len;
  528. /* Copy header if single buffer */
  529. if (skb_queue_len(list) == 1) {
  530. skb = skb_peek(list);
  531. hdr_len = skb_headroom(skb) + msg_hdr_sz(buf_msg(skb));
  532. _skb = __pskb_copy(skb, hdr_len, GFP_ATOMIC);
  533. if (!_skb)
  534. return false;
  535. __skb_queue_tail(rcvq, _skb);
  536. return true;
  537. }
  538. /* Clone all fragments and reassemble */
  539. skb_queue_walk(list, skb) {
  540. frag = skb_clone(skb, GFP_ATOMIC);
  541. if (!frag)
  542. goto error;
  543. frag->next = NULL;
  544. if (tipc_buf_append(&head, &frag))
  545. break;
  546. if (!head)
  547. goto error;
  548. }
  549. __skb_queue_tail(rcvq, frag);
  550. return true;
  551. error:
  552. pr_warn("Failed do clone local mcast rcv buffer\n");
  553. kfree_skb(head);
  554. return false;
  555. }
  556. bool tipc_msg_pskb_copy(u32 dst, struct sk_buff_head *msg,
  557. struct sk_buff_head *cpy)
  558. {
  559. struct sk_buff *skb, *_skb;
  560. skb_queue_walk(msg, skb) {
  561. _skb = pskb_copy(skb, GFP_ATOMIC);
  562. if (!_skb) {
  563. __skb_queue_purge(cpy);
  564. return false;
  565. }
  566. msg_set_destnode(buf_msg(_skb), dst);
  567. __skb_queue_tail(cpy, _skb);
  568. }
  569. return true;
  570. }
  571. /* tipc_skb_queue_sorted(); sort pkt into list according to sequence number
  572. * @list: list to be appended to
  573. * @seqno: sequence number of buffer to add
  574. * @skb: buffer to add
  575. */
  576. void __tipc_skb_queue_sorted(struct sk_buff_head *list, u16 seqno,
  577. struct sk_buff *skb)
  578. {
  579. struct sk_buff *_skb, *tmp;
  580. if (skb_queue_empty(list) || less(seqno, buf_seqno(skb_peek(list)))) {
  581. __skb_queue_head(list, skb);
  582. return;
  583. }
  584. if (more(seqno, buf_seqno(skb_peek_tail(list)))) {
  585. __skb_queue_tail(list, skb);
  586. return;
  587. }
  588. skb_queue_walk_safe(list, _skb, tmp) {
  589. if (more(seqno, buf_seqno(_skb)))
  590. continue;
  591. if (seqno == buf_seqno(_skb))
  592. break;
  593. __skb_queue_before(list, _skb, skb);
  594. return;
  595. }
  596. kfree_skb(skb);
  597. }