18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * net/tipc/msg.c: TIPC message header routines
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (c) 2000-2006, 2014-2015, Ericsson AB
58c2ecf20Sopenharmony_ci * Copyright (c) 2005, 2010-2011, Wind River Systems
68c2ecf20Sopenharmony_ci * All rights reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
98c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
128c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
138c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
148c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
158c2ecf20Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
168c2ecf20Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its
178c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived from
188c2ecf20Sopenharmony_ci *    this software without specific prior written permission.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
218c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
228c2ecf20Sopenharmony_ci * Software Foundation.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
258c2ecf20Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
268c2ecf20Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
278c2ecf20Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
288c2ecf20Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
298c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
308c2ecf20Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
318c2ecf20Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
328c2ecf20Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
338c2ecf20Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
348c2ecf20Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include <net/sock.h>
388c2ecf20Sopenharmony_ci#include "core.h"
398c2ecf20Sopenharmony_ci#include "msg.h"
408c2ecf20Sopenharmony_ci#include "addr.h"
418c2ecf20Sopenharmony_ci#include "name_table.h"
428c2ecf20Sopenharmony_ci#include "crypto.h"
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define MAX_FORWARD_SIZE 1024
458c2ecf20Sopenharmony_ci#ifdef CONFIG_TIPC_CRYPTO
468c2ecf20Sopenharmony_ci#define BUF_HEADROOM ALIGN(((LL_MAX_HEADER + 48) + EHDR_MAX_SIZE), 16)
478c2ecf20Sopenharmony_ci#define BUF_OVERHEAD (BUF_HEADROOM + TIPC_AES_GCM_TAG_SIZE)
488c2ecf20Sopenharmony_ci#else
498c2ecf20Sopenharmony_ci#define BUF_HEADROOM (LL_MAX_HEADER + 48)
508c2ecf20Sopenharmony_ci#define BUF_OVERHEAD BUF_HEADROOM
518c2ecf20Sopenharmony_ci#endif
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciconst int one_page_mtu = PAGE_SIZE - SKB_DATA_ALIGN(BUF_OVERHEAD) -
548c2ecf20Sopenharmony_ci			 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic unsigned int align(unsigned int i)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	return (i + 3) & ~3u;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/**
628c2ecf20Sopenharmony_ci * tipc_buf_acquire - creates a TIPC message buffer
638c2ecf20Sopenharmony_ci * @size: message size (including TIPC header)
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Returns a new buffer with data pointers set to the specified size.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * NOTE: Headroom is reserved to allow prepending of a data link header.
688c2ecf20Sopenharmony_ci *       There may also be unrequested tailroom present at the buffer's end.
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_cistruct sk_buff *tipc_buf_acquire(u32 size, gfp_t gfp)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct sk_buff *skb;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	skb = alloc_skb_fclone(BUF_OVERHEAD + size, gfp);
758c2ecf20Sopenharmony_ci	if (skb) {
768c2ecf20Sopenharmony_ci		skb_reserve(skb, BUF_HEADROOM);
778c2ecf20Sopenharmony_ci		skb_put(skb, size);
788c2ecf20Sopenharmony_ci		skb->next = NULL;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci	return skb;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_civoid tipc_msg_init(u32 own_node, struct tipc_msg *m, u32 user, u32 type,
848c2ecf20Sopenharmony_ci		   u32 hsize, u32 dnode)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	memset(m, 0, hsize);
878c2ecf20Sopenharmony_ci	msg_set_version(m);
888c2ecf20Sopenharmony_ci	msg_set_user(m, user);
898c2ecf20Sopenharmony_ci	msg_set_hdr_sz(m, hsize);
908c2ecf20Sopenharmony_ci	msg_set_size(m, hsize);
918c2ecf20Sopenharmony_ci	msg_set_prevnode(m, own_node);
928c2ecf20Sopenharmony_ci	msg_set_type(m, type);
938c2ecf20Sopenharmony_ci	if (hsize > SHORT_H_SIZE) {
948c2ecf20Sopenharmony_ci		msg_set_orignode(m, own_node);
958c2ecf20Sopenharmony_ci		msg_set_destnode(m, dnode);
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistruct sk_buff *tipc_msg_create(uint user, uint type,
1008c2ecf20Sopenharmony_ci				uint hdr_sz, uint data_sz, u32 dnode,
1018c2ecf20Sopenharmony_ci				u32 onode, u32 dport, u32 oport, int errcode)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct tipc_msg *msg;
1048c2ecf20Sopenharmony_ci	struct sk_buff *buf;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	buf = tipc_buf_acquire(hdr_sz + data_sz, GFP_ATOMIC);
1078c2ecf20Sopenharmony_ci	if (unlikely(!buf))
1088c2ecf20Sopenharmony_ci		return NULL;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	msg = buf_msg(buf);
1118c2ecf20Sopenharmony_ci	tipc_msg_init(onode, msg, user, type, hdr_sz, dnode);
1128c2ecf20Sopenharmony_ci	msg_set_size(msg, hdr_sz + data_sz);
1138c2ecf20Sopenharmony_ci	msg_set_origport(msg, oport);
1148c2ecf20Sopenharmony_ci	msg_set_destport(msg, dport);
1158c2ecf20Sopenharmony_ci	msg_set_errcode(msg, errcode);
1168c2ecf20Sopenharmony_ci	if (hdr_sz > SHORT_H_SIZE) {
1178c2ecf20Sopenharmony_ci		msg_set_orignode(msg, onode);
1188c2ecf20Sopenharmony_ci		msg_set_destnode(msg, dnode);
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci	return buf;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* tipc_buf_append(): Append a buffer to the fragment list of another buffer
1248c2ecf20Sopenharmony_ci * @*headbuf: in:  NULL for first frag, otherwise value returned from prev call
1258c2ecf20Sopenharmony_ci *            out: set when successful non-complete reassembly, otherwise NULL
1268c2ecf20Sopenharmony_ci * @*buf:     in:  the buffer to append. Always defined
1278c2ecf20Sopenharmony_ci *            out: head buf after successful complete reassembly, otherwise NULL
1288c2ecf20Sopenharmony_ci * Returns 1 when reassembly complete, otherwise 0
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_ciint tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct sk_buff *head = *headbuf;
1338c2ecf20Sopenharmony_ci	struct sk_buff *frag = *buf;
1348c2ecf20Sopenharmony_ci	struct sk_buff *tail = NULL;
1358c2ecf20Sopenharmony_ci	struct tipc_msg *msg;
1368c2ecf20Sopenharmony_ci	u32 fragid;
1378c2ecf20Sopenharmony_ci	int delta;
1388c2ecf20Sopenharmony_ci	bool headstolen;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (!frag)
1418c2ecf20Sopenharmony_ci		goto err;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	msg = buf_msg(frag);
1448c2ecf20Sopenharmony_ci	fragid = msg_type(msg);
1458c2ecf20Sopenharmony_ci	frag->next = NULL;
1468c2ecf20Sopenharmony_ci	skb_pull(frag, msg_hdr_sz(msg));
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (fragid == FIRST_FRAGMENT) {
1498c2ecf20Sopenharmony_ci		if (unlikely(head))
1508c2ecf20Sopenharmony_ci			goto err;
1518c2ecf20Sopenharmony_ci		*buf = NULL;
1528c2ecf20Sopenharmony_ci		if (skb_has_frag_list(frag) && __skb_linearize(frag))
1538c2ecf20Sopenharmony_ci			goto err;
1548c2ecf20Sopenharmony_ci		frag = skb_unshare(frag, GFP_ATOMIC);
1558c2ecf20Sopenharmony_ci		if (unlikely(!frag))
1568c2ecf20Sopenharmony_ci			goto err;
1578c2ecf20Sopenharmony_ci		head = *headbuf = frag;
1588c2ecf20Sopenharmony_ci		TIPC_SKB_CB(head)->tail = NULL;
1598c2ecf20Sopenharmony_ci		return 0;
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (!head)
1638c2ecf20Sopenharmony_ci		goto err;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (skb_try_coalesce(head, frag, &headstolen, &delta)) {
1668c2ecf20Sopenharmony_ci		kfree_skb_partial(frag, headstolen);
1678c2ecf20Sopenharmony_ci	} else {
1688c2ecf20Sopenharmony_ci		tail = TIPC_SKB_CB(head)->tail;
1698c2ecf20Sopenharmony_ci		if (!skb_has_frag_list(head))
1708c2ecf20Sopenharmony_ci			skb_shinfo(head)->frag_list = frag;
1718c2ecf20Sopenharmony_ci		else
1728c2ecf20Sopenharmony_ci			tail->next = frag;
1738c2ecf20Sopenharmony_ci		head->truesize += frag->truesize;
1748c2ecf20Sopenharmony_ci		head->data_len += frag->len;
1758c2ecf20Sopenharmony_ci		head->len += frag->len;
1768c2ecf20Sopenharmony_ci		TIPC_SKB_CB(head)->tail = frag;
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (fragid == LAST_FRAGMENT) {
1808c2ecf20Sopenharmony_ci		TIPC_SKB_CB(head)->validated = 0;
1818c2ecf20Sopenharmony_ci		if (unlikely(!tipc_msg_validate(&head)))
1828c2ecf20Sopenharmony_ci			goto err;
1838c2ecf20Sopenharmony_ci		*buf = head;
1848c2ecf20Sopenharmony_ci		TIPC_SKB_CB(head)->tail = NULL;
1858c2ecf20Sopenharmony_ci		*headbuf = NULL;
1868c2ecf20Sopenharmony_ci		return 1;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	*buf = NULL;
1898c2ecf20Sopenharmony_ci	return 0;
1908c2ecf20Sopenharmony_cierr:
1918c2ecf20Sopenharmony_ci	kfree_skb(*buf);
1928c2ecf20Sopenharmony_ci	kfree_skb(*headbuf);
1938c2ecf20Sopenharmony_ci	*buf = *headbuf = NULL;
1948c2ecf20Sopenharmony_ci	return 0;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/**
1988c2ecf20Sopenharmony_ci * tipc_msg_append(): Append data to tail of an existing buffer queue
1998c2ecf20Sopenharmony_ci * @_hdr: header to be used
2008c2ecf20Sopenharmony_ci * @m: the data to be appended
2018c2ecf20Sopenharmony_ci * @mss: max allowable size of buffer
2028c2ecf20Sopenharmony_ci * @dlen: size of data to be appended
2038c2ecf20Sopenharmony_ci * @txq: queue to appand to
2048c2ecf20Sopenharmony_ci * Returns the number og 1k blocks appended or errno value
2058c2ecf20Sopenharmony_ci */
2068c2ecf20Sopenharmony_ciint tipc_msg_append(struct tipc_msg *_hdr, struct msghdr *m, int dlen,
2078c2ecf20Sopenharmony_ci		    int mss, struct sk_buff_head *txq)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct sk_buff *skb;
2108c2ecf20Sopenharmony_ci	int accounted, total, curr;
2118c2ecf20Sopenharmony_ci	int mlen, cpy, rem = dlen;
2128c2ecf20Sopenharmony_ci	struct tipc_msg *hdr;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	skb = skb_peek_tail(txq);
2158c2ecf20Sopenharmony_ci	accounted = skb ? msg_blocks(buf_msg(skb)) : 0;
2168c2ecf20Sopenharmony_ci	total = accounted;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	do {
2198c2ecf20Sopenharmony_ci		if (!skb || skb->len >= mss) {
2208c2ecf20Sopenharmony_ci			skb = tipc_buf_acquire(mss, GFP_KERNEL);
2218c2ecf20Sopenharmony_ci			if (unlikely(!skb))
2228c2ecf20Sopenharmony_ci				return -ENOMEM;
2238c2ecf20Sopenharmony_ci			skb_orphan(skb);
2248c2ecf20Sopenharmony_ci			skb_trim(skb, MIN_H_SIZE);
2258c2ecf20Sopenharmony_ci			hdr = buf_msg(skb);
2268c2ecf20Sopenharmony_ci			skb_copy_to_linear_data(skb, _hdr, MIN_H_SIZE);
2278c2ecf20Sopenharmony_ci			msg_set_hdr_sz(hdr, MIN_H_SIZE);
2288c2ecf20Sopenharmony_ci			msg_set_size(hdr, MIN_H_SIZE);
2298c2ecf20Sopenharmony_ci			__skb_queue_tail(txq, skb);
2308c2ecf20Sopenharmony_ci			total += 1;
2318c2ecf20Sopenharmony_ci		}
2328c2ecf20Sopenharmony_ci		hdr = buf_msg(skb);
2338c2ecf20Sopenharmony_ci		curr = msg_blocks(hdr);
2348c2ecf20Sopenharmony_ci		mlen = msg_size(hdr);
2358c2ecf20Sopenharmony_ci		cpy = min_t(size_t, rem, mss - mlen);
2368c2ecf20Sopenharmony_ci		if (cpy != copy_from_iter(skb->data + mlen, cpy, &m->msg_iter))
2378c2ecf20Sopenharmony_ci			return -EFAULT;
2388c2ecf20Sopenharmony_ci		msg_set_size(hdr, mlen + cpy);
2398c2ecf20Sopenharmony_ci		skb_put(skb, cpy);
2408c2ecf20Sopenharmony_ci		rem -= cpy;
2418c2ecf20Sopenharmony_ci		total += msg_blocks(hdr) - curr;
2428c2ecf20Sopenharmony_ci	} while (rem > 0);
2438c2ecf20Sopenharmony_ci	return total - accounted;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/* tipc_msg_validate - validate basic format of received message
2478c2ecf20Sopenharmony_ci *
2488c2ecf20Sopenharmony_ci * This routine ensures a TIPC message has an acceptable header, and at least
2498c2ecf20Sopenharmony_ci * as much data as the header indicates it should.  The routine also ensures
2508c2ecf20Sopenharmony_ci * that the entire message header is stored in the main fragment of the message
2518c2ecf20Sopenharmony_ci * buffer, to simplify future access to message header fields.
2528c2ecf20Sopenharmony_ci *
2538c2ecf20Sopenharmony_ci * Note: Having extra info present in the message header or data areas is OK.
2548c2ecf20Sopenharmony_ci * TIPC will ignore the excess, under the assumption that it is optional info
2558c2ecf20Sopenharmony_ci * introduced by a later release of the protocol.
2568c2ecf20Sopenharmony_ci */
2578c2ecf20Sopenharmony_cibool tipc_msg_validate(struct sk_buff **_skb)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct sk_buff *skb = *_skb;
2608c2ecf20Sopenharmony_ci	struct tipc_msg *hdr;
2618c2ecf20Sopenharmony_ci	int msz, hsz;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* Ensure that flow control ratio condition is satisfied */
2648c2ecf20Sopenharmony_ci	if (unlikely(skb->truesize / buf_roundup_len(skb) >= 4)) {
2658c2ecf20Sopenharmony_ci		skb = skb_copy_expand(skb, BUF_HEADROOM, 0, GFP_ATOMIC);
2668c2ecf20Sopenharmony_ci		if (!skb)
2678c2ecf20Sopenharmony_ci			return false;
2688c2ecf20Sopenharmony_ci		kfree_skb(*_skb);
2698c2ecf20Sopenharmony_ci		*_skb = skb;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if (unlikely(TIPC_SKB_CB(skb)->validated))
2738c2ecf20Sopenharmony_ci		return true;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (unlikely(!pskb_may_pull(skb, MIN_H_SIZE)))
2768c2ecf20Sopenharmony_ci		return false;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	hsz = msg_hdr_sz(buf_msg(skb));
2798c2ecf20Sopenharmony_ci	if (unlikely(hsz < MIN_H_SIZE) || (hsz > MAX_H_SIZE))
2808c2ecf20Sopenharmony_ci		return false;
2818c2ecf20Sopenharmony_ci	if (unlikely(!pskb_may_pull(skb, hsz)))
2828c2ecf20Sopenharmony_ci		return false;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	hdr = buf_msg(skb);
2858c2ecf20Sopenharmony_ci	if (unlikely(msg_version(hdr) != TIPC_VERSION))
2868c2ecf20Sopenharmony_ci		return false;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	msz = msg_size(hdr);
2898c2ecf20Sopenharmony_ci	if (unlikely(msz < hsz))
2908c2ecf20Sopenharmony_ci		return false;
2918c2ecf20Sopenharmony_ci	if (unlikely((msz - hsz) > TIPC_MAX_USER_MSG_SIZE))
2928c2ecf20Sopenharmony_ci		return false;
2938c2ecf20Sopenharmony_ci	if (unlikely(skb->len < msz))
2948c2ecf20Sopenharmony_ci		return false;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	TIPC_SKB_CB(skb)->validated = 1;
2978c2ecf20Sopenharmony_ci	return true;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci/**
3018c2ecf20Sopenharmony_ci * tipc_msg_fragment - build a fragment skb list for TIPC message
3028c2ecf20Sopenharmony_ci *
3038c2ecf20Sopenharmony_ci * @skb: TIPC message skb
3048c2ecf20Sopenharmony_ci * @hdr: internal msg header to be put on the top of the fragments
3058c2ecf20Sopenharmony_ci * @pktmax: max size of a fragment incl. the header
3068c2ecf20Sopenharmony_ci * @frags: returned fragment skb list
3078c2ecf20Sopenharmony_ci *
3088c2ecf20Sopenharmony_ci * Returns 0 if the fragmentation is successful, otherwise: -EINVAL
3098c2ecf20Sopenharmony_ci * or -ENOMEM
3108c2ecf20Sopenharmony_ci */
3118c2ecf20Sopenharmony_ciint tipc_msg_fragment(struct sk_buff *skb, const struct tipc_msg *hdr,
3128c2ecf20Sopenharmony_ci		      int pktmax, struct sk_buff_head *frags)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	int pktno, nof_fragms, dsz, dmax, eat;
3158c2ecf20Sopenharmony_ci	struct tipc_msg *_hdr;
3168c2ecf20Sopenharmony_ci	struct sk_buff *_skb;
3178c2ecf20Sopenharmony_ci	u8 *data;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/* Non-linear buffer? */
3208c2ecf20Sopenharmony_ci	if (skb_linearize(skb))
3218c2ecf20Sopenharmony_ci		return -ENOMEM;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	data = (u8 *)skb->data;
3248c2ecf20Sopenharmony_ci	dsz = msg_size(buf_msg(skb));
3258c2ecf20Sopenharmony_ci	dmax = pktmax - INT_H_SIZE;
3268c2ecf20Sopenharmony_ci	if (dsz <= dmax || !dmax)
3278c2ecf20Sopenharmony_ci		return -EINVAL;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	nof_fragms = dsz / dmax + 1;
3308c2ecf20Sopenharmony_ci	for (pktno = 1; pktno <= nof_fragms; pktno++) {
3318c2ecf20Sopenharmony_ci		if (pktno < nof_fragms)
3328c2ecf20Sopenharmony_ci			eat = dmax;
3338c2ecf20Sopenharmony_ci		else
3348c2ecf20Sopenharmony_ci			eat = dsz % dmax;
3358c2ecf20Sopenharmony_ci		/* Allocate a new fragment */
3368c2ecf20Sopenharmony_ci		_skb = tipc_buf_acquire(INT_H_SIZE + eat, GFP_ATOMIC);
3378c2ecf20Sopenharmony_ci		if (!_skb)
3388c2ecf20Sopenharmony_ci			goto error;
3398c2ecf20Sopenharmony_ci		skb_orphan(_skb);
3408c2ecf20Sopenharmony_ci		__skb_queue_tail(frags, _skb);
3418c2ecf20Sopenharmony_ci		/* Copy header & data to the fragment */
3428c2ecf20Sopenharmony_ci		skb_copy_to_linear_data(_skb, hdr, INT_H_SIZE);
3438c2ecf20Sopenharmony_ci		skb_copy_to_linear_data_offset(_skb, INT_H_SIZE, data, eat);
3448c2ecf20Sopenharmony_ci		data += eat;
3458c2ecf20Sopenharmony_ci		/* Update the fragment's header */
3468c2ecf20Sopenharmony_ci		_hdr = buf_msg(_skb);
3478c2ecf20Sopenharmony_ci		msg_set_fragm_no(_hdr, pktno);
3488c2ecf20Sopenharmony_ci		msg_set_nof_fragms(_hdr, nof_fragms);
3498c2ecf20Sopenharmony_ci		msg_set_size(_hdr, INT_H_SIZE + eat);
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci	return 0;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cierror:
3548c2ecf20Sopenharmony_ci	__skb_queue_purge(frags);
3558c2ecf20Sopenharmony_ci	__skb_queue_head_init(frags);
3568c2ecf20Sopenharmony_ci	return -ENOMEM;
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci/**
3608c2ecf20Sopenharmony_ci * tipc_msg_build - create buffer chain containing specified header and data
3618c2ecf20Sopenharmony_ci * @mhdr: Message header, to be prepended to data
3628c2ecf20Sopenharmony_ci * @m: User message
3638c2ecf20Sopenharmony_ci * @dsz: Total length of user data
3648c2ecf20Sopenharmony_ci * @pktmax: Max packet size that can be used
3658c2ecf20Sopenharmony_ci * @list: Buffer or chain of buffers to be returned to caller
3668c2ecf20Sopenharmony_ci *
3678c2ecf20Sopenharmony_ci * Note that the recursive call we are making here is safe, since it can
3688c2ecf20Sopenharmony_ci * logically go only one further level down.
3698c2ecf20Sopenharmony_ci *
3708c2ecf20Sopenharmony_ci * Returns message data size or errno: -ENOMEM, -EFAULT
3718c2ecf20Sopenharmony_ci */
3728c2ecf20Sopenharmony_ciint tipc_msg_build(struct tipc_msg *mhdr, struct msghdr *m, int offset,
3738c2ecf20Sopenharmony_ci		   int dsz, int pktmax, struct sk_buff_head *list)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	int mhsz = msg_hdr_sz(mhdr);
3768c2ecf20Sopenharmony_ci	struct tipc_msg pkthdr;
3778c2ecf20Sopenharmony_ci	int msz = mhsz + dsz;
3788c2ecf20Sopenharmony_ci	int pktrem = pktmax;
3798c2ecf20Sopenharmony_ci	struct sk_buff *skb;
3808c2ecf20Sopenharmony_ci	int drem = dsz;
3818c2ecf20Sopenharmony_ci	int pktno = 1;
3828c2ecf20Sopenharmony_ci	char *pktpos;
3838c2ecf20Sopenharmony_ci	int pktsz;
3848c2ecf20Sopenharmony_ci	int rc;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	msg_set_size(mhdr, msz);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* No fragmentation needed? */
3898c2ecf20Sopenharmony_ci	if (likely(msz <= pktmax)) {
3908c2ecf20Sopenharmony_ci		skb = tipc_buf_acquire(msz, GFP_KERNEL);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		/* Fall back to smaller MTU if node local message */
3938c2ecf20Sopenharmony_ci		if (unlikely(!skb)) {
3948c2ecf20Sopenharmony_ci			if (pktmax != MAX_MSG_SIZE)
3958c2ecf20Sopenharmony_ci				return -ENOMEM;
3968c2ecf20Sopenharmony_ci			rc = tipc_msg_build(mhdr, m, offset, dsz,
3978c2ecf20Sopenharmony_ci					    one_page_mtu, list);
3988c2ecf20Sopenharmony_ci			if (rc != dsz)
3998c2ecf20Sopenharmony_ci				return rc;
4008c2ecf20Sopenharmony_ci			if (tipc_msg_assemble(list))
4018c2ecf20Sopenharmony_ci				return dsz;
4028c2ecf20Sopenharmony_ci			return -ENOMEM;
4038c2ecf20Sopenharmony_ci		}
4048c2ecf20Sopenharmony_ci		skb_orphan(skb);
4058c2ecf20Sopenharmony_ci		__skb_queue_tail(list, skb);
4068c2ecf20Sopenharmony_ci		skb_copy_to_linear_data(skb, mhdr, mhsz);
4078c2ecf20Sopenharmony_ci		pktpos = skb->data + mhsz;
4088c2ecf20Sopenharmony_ci		if (copy_from_iter_full(pktpos, dsz, &m->msg_iter))
4098c2ecf20Sopenharmony_ci			return dsz;
4108c2ecf20Sopenharmony_ci		rc = -EFAULT;
4118c2ecf20Sopenharmony_ci		goto error;
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* Prepare reusable fragment header */
4158c2ecf20Sopenharmony_ci	tipc_msg_init(msg_prevnode(mhdr), &pkthdr, MSG_FRAGMENTER,
4168c2ecf20Sopenharmony_ci		      FIRST_FRAGMENT, INT_H_SIZE, msg_destnode(mhdr));
4178c2ecf20Sopenharmony_ci	msg_set_size(&pkthdr, pktmax);
4188c2ecf20Sopenharmony_ci	msg_set_fragm_no(&pkthdr, pktno);
4198c2ecf20Sopenharmony_ci	msg_set_importance(&pkthdr, msg_importance(mhdr));
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	/* Prepare first fragment */
4228c2ecf20Sopenharmony_ci	skb = tipc_buf_acquire(pktmax, GFP_KERNEL);
4238c2ecf20Sopenharmony_ci	if (!skb)
4248c2ecf20Sopenharmony_ci		return -ENOMEM;
4258c2ecf20Sopenharmony_ci	skb_orphan(skb);
4268c2ecf20Sopenharmony_ci	__skb_queue_tail(list, skb);
4278c2ecf20Sopenharmony_ci	pktpos = skb->data;
4288c2ecf20Sopenharmony_ci	skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
4298c2ecf20Sopenharmony_ci	pktpos += INT_H_SIZE;
4308c2ecf20Sopenharmony_ci	pktrem -= INT_H_SIZE;
4318c2ecf20Sopenharmony_ci	skb_copy_to_linear_data_offset(skb, INT_H_SIZE, mhdr, mhsz);
4328c2ecf20Sopenharmony_ci	pktpos += mhsz;
4338c2ecf20Sopenharmony_ci	pktrem -= mhsz;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	do {
4368c2ecf20Sopenharmony_ci		if (drem < pktrem)
4378c2ecf20Sopenharmony_ci			pktrem = drem;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		if (!copy_from_iter_full(pktpos, pktrem, &m->msg_iter)) {
4408c2ecf20Sopenharmony_ci			rc = -EFAULT;
4418c2ecf20Sopenharmony_ci			goto error;
4428c2ecf20Sopenharmony_ci		}
4438c2ecf20Sopenharmony_ci		drem -= pktrem;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci		if (!drem)
4468c2ecf20Sopenharmony_ci			break;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci		/* Prepare new fragment: */
4498c2ecf20Sopenharmony_ci		if (drem < (pktmax - INT_H_SIZE))
4508c2ecf20Sopenharmony_ci			pktsz = drem + INT_H_SIZE;
4518c2ecf20Sopenharmony_ci		else
4528c2ecf20Sopenharmony_ci			pktsz = pktmax;
4538c2ecf20Sopenharmony_ci		skb = tipc_buf_acquire(pktsz, GFP_KERNEL);
4548c2ecf20Sopenharmony_ci		if (!skb) {
4558c2ecf20Sopenharmony_ci			rc = -ENOMEM;
4568c2ecf20Sopenharmony_ci			goto error;
4578c2ecf20Sopenharmony_ci		}
4588c2ecf20Sopenharmony_ci		skb_orphan(skb);
4598c2ecf20Sopenharmony_ci		__skb_queue_tail(list, skb);
4608c2ecf20Sopenharmony_ci		msg_set_type(&pkthdr, FRAGMENT);
4618c2ecf20Sopenharmony_ci		msg_set_size(&pkthdr, pktsz);
4628c2ecf20Sopenharmony_ci		msg_set_fragm_no(&pkthdr, ++pktno);
4638c2ecf20Sopenharmony_ci		skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
4648c2ecf20Sopenharmony_ci		pktpos = skb->data + INT_H_SIZE;
4658c2ecf20Sopenharmony_ci		pktrem = pktsz - INT_H_SIZE;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	} while (1);
4688c2ecf20Sopenharmony_ci	msg_set_type(buf_msg(skb), LAST_FRAGMENT);
4698c2ecf20Sopenharmony_ci	return dsz;
4708c2ecf20Sopenharmony_cierror:
4718c2ecf20Sopenharmony_ci	__skb_queue_purge(list);
4728c2ecf20Sopenharmony_ci	__skb_queue_head_init(list);
4738c2ecf20Sopenharmony_ci	return rc;
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci/**
4778c2ecf20Sopenharmony_ci * tipc_msg_bundle - Append contents of a buffer to tail of an existing one
4788c2ecf20Sopenharmony_ci * @bskb: the bundle buffer to append to
4798c2ecf20Sopenharmony_ci * @msg: message to be appended
4808c2ecf20Sopenharmony_ci * @max: max allowable size for the bundle buffer
4818c2ecf20Sopenharmony_ci *
4828c2ecf20Sopenharmony_ci * Returns "true" if bundling has been performed, otherwise "false"
4838c2ecf20Sopenharmony_ci */
4848c2ecf20Sopenharmony_cistatic bool tipc_msg_bundle(struct sk_buff *bskb, struct tipc_msg *msg,
4858c2ecf20Sopenharmony_ci			    u32 max)
4868c2ecf20Sopenharmony_ci{
4878c2ecf20Sopenharmony_ci	struct tipc_msg *bmsg = buf_msg(bskb);
4888c2ecf20Sopenharmony_ci	u32 msz, bsz, offset, pad;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	msz = msg_size(msg);
4918c2ecf20Sopenharmony_ci	bsz = msg_size(bmsg);
4928c2ecf20Sopenharmony_ci	offset = align(bsz);
4938c2ecf20Sopenharmony_ci	pad = offset - bsz;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	if (unlikely(skb_tailroom(bskb) < (pad + msz)))
4968c2ecf20Sopenharmony_ci		return false;
4978c2ecf20Sopenharmony_ci	if (unlikely(max < (offset + msz)))
4988c2ecf20Sopenharmony_ci		return false;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	skb_put(bskb, pad + msz);
5018c2ecf20Sopenharmony_ci	skb_copy_to_linear_data_offset(bskb, offset, msg, msz);
5028c2ecf20Sopenharmony_ci	msg_set_size(bmsg, offset + msz);
5038c2ecf20Sopenharmony_ci	msg_set_msgcnt(bmsg, msg_msgcnt(bmsg) + 1);
5048c2ecf20Sopenharmony_ci	return true;
5058c2ecf20Sopenharmony_ci}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci/**
5088c2ecf20Sopenharmony_ci * tipc_msg_try_bundle - Try to bundle a new message to the last one
5098c2ecf20Sopenharmony_ci * @tskb: the last/target message to which the new one will be appended
5108c2ecf20Sopenharmony_ci * @skb: the new message skb pointer
5118c2ecf20Sopenharmony_ci * @mss: max message size (header inclusive)
5128c2ecf20Sopenharmony_ci * @dnode: destination node for the message
5138c2ecf20Sopenharmony_ci * @new_bundle: if this call made a new bundle or not
5148c2ecf20Sopenharmony_ci *
5158c2ecf20Sopenharmony_ci * Return: "true" if the new message skb is potential for bundling this time or
5168c2ecf20Sopenharmony_ci * later, in the case a bundling has been done this time, the skb is consumed
5178c2ecf20Sopenharmony_ci * (the skb pointer = NULL).
5188c2ecf20Sopenharmony_ci * Otherwise, "false" if the skb cannot be bundled at all.
5198c2ecf20Sopenharmony_ci */
5208c2ecf20Sopenharmony_cibool tipc_msg_try_bundle(struct sk_buff *tskb, struct sk_buff **skb, u32 mss,
5218c2ecf20Sopenharmony_ci			 u32 dnode, bool *new_bundle)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	struct tipc_msg *msg, *inner, *outer;
5248c2ecf20Sopenharmony_ci	u32 tsz;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	/* First, check if the new buffer is suitable for bundling */
5278c2ecf20Sopenharmony_ci	msg = buf_msg(*skb);
5288c2ecf20Sopenharmony_ci	if (msg_user(msg) == MSG_FRAGMENTER)
5298c2ecf20Sopenharmony_ci		return false;
5308c2ecf20Sopenharmony_ci	if (msg_user(msg) == TUNNEL_PROTOCOL)
5318c2ecf20Sopenharmony_ci		return false;
5328c2ecf20Sopenharmony_ci	if (msg_user(msg) == BCAST_PROTOCOL)
5338c2ecf20Sopenharmony_ci		return false;
5348c2ecf20Sopenharmony_ci	if (mss <= INT_H_SIZE + msg_size(msg))
5358c2ecf20Sopenharmony_ci		return false;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	/* Ok, but the last/target buffer can be empty? */
5388c2ecf20Sopenharmony_ci	if (unlikely(!tskb))
5398c2ecf20Sopenharmony_ci		return true;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	/* Is it a bundle already? Try to bundle the new message to it */
5428c2ecf20Sopenharmony_ci	if (msg_user(buf_msg(tskb)) == MSG_BUNDLER) {
5438c2ecf20Sopenharmony_ci		*new_bundle = false;
5448c2ecf20Sopenharmony_ci		goto bundle;
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	/* Make a new bundle of the two messages if possible */
5488c2ecf20Sopenharmony_ci	tsz = msg_size(buf_msg(tskb));
5498c2ecf20Sopenharmony_ci	if (unlikely(mss < align(INT_H_SIZE + tsz) + msg_size(msg)))
5508c2ecf20Sopenharmony_ci		return true;
5518c2ecf20Sopenharmony_ci	if (unlikely(pskb_expand_head(tskb, INT_H_SIZE, mss - tsz - INT_H_SIZE,
5528c2ecf20Sopenharmony_ci				      GFP_ATOMIC)))
5538c2ecf20Sopenharmony_ci		return true;
5548c2ecf20Sopenharmony_ci	inner = buf_msg(tskb);
5558c2ecf20Sopenharmony_ci	skb_push(tskb, INT_H_SIZE);
5568c2ecf20Sopenharmony_ci	outer = buf_msg(tskb);
5578c2ecf20Sopenharmony_ci	tipc_msg_init(msg_prevnode(inner), outer, MSG_BUNDLER, 0, INT_H_SIZE,
5588c2ecf20Sopenharmony_ci		      dnode);
5598c2ecf20Sopenharmony_ci	msg_set_importance(outer, msg_importance(inner));
5608c2ecf20Sopenharmony_ci	msg_set_size(outer, INT_H_SIZE + tsz);
5618c2ecf20Sopenharmony_ci	msg_set_msgcnt(outer, 1);
5628c2ecf20Sopenharmony_ci	*new_bundle = true;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_cibundle:
5658c2ecf20Sopenharmony_ci	if (likely(tipc_msg_bundle(tskb, msg, mss))) {
5668c2ecf20Sopenharmony_ci		consume_skb(*skb);
5678c2ecf20Sopenharmony_ci		*skb = NULL;
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci	return true;
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci/**
5738c2ecf20Sopenharmony_ci *  tipc_msg_extract(): extract bundled inner packet from buffer
5748c2ecf20Sopenharmony_ci *  @skb: buffer to be extracted from.
5758c2ecf20Sopenharmony_ci *  @iskb: extracted inner buffer, to be returned
5768c2ecf20Sopenharmony_ci *  @pos: position in outer message of msg to be extracted.
5778c2ecf20Sopenharmony_ci *        Returns position of next msg
5788c2ecf20Sopenharmony_ci *  Consumes outer buffer when last packet extracted
5798c2ecf20Sopenharmony_ci *  Returns true when there is an extracted buffer, otherwise false
5808c2ecf20Sopenharmony_ci */
5818c2ecf20Sopenharmony_cibool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos)
5828c2ecf20Sopenharmony_ci{
5838c2ecf20Sopenharmony_ci	struct tipc_msg *hdr, *ihdr;
5848c2ecf20Sopenharmony_ci	int imsz;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	*iskb = NULL;
5878c2ecf20Sopenharmony_ci	if (unlikely(skb_linearize(skb)))
5888c2ecf20Sopenharmony_ci		goto none;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	hdr = buf_msg(skb);
5918c2ecf20Sopenharmony_ci	if (unlikely(*pos > (msg_data_sz(hdr) - MIN_H_SIZE)))
5928c2ecf20Sopenharmony_ci		goto none;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	ihdr = (struct tipc_msg *)(msg_data(hdr) + *pos);
5958c2ecf20Sopenharmony_ci	imsz = msg_size(ihdr);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	if ((*pos + imsz) > msg_data_sz(hdr))
5988c2ecf20Sopenharmony_ci		goto none;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	*iskb = tipc_buf_acquire(imsz, GFP_ATOMIC);
6018c2ecf20Sopenharmony_ci	if (!*iskb)
6028c2ecf20Sopenharmony_ci		goto none;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	skb_copy_to_linear_data(*iskb, ihdr, imsz);
6058c2ecf20Sopenharmony_ci	if (unlikely(!tipc_msg_validate(iskb)))
6068c2ecf20Sopenharmony_ci		goto none;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	*pos += align(imsz);
6098c2ecf20Sopenharmony_ci	return true;
6108c2ecf20Sopenharmony_cinone:
6118c2ecf20Sopenharmony_ci	kfree_skb(skb);
6128c2ecf20Sopenharmony_ci	kfree_skb(*iskb);
6138c2ecf20Sopenharmony_ci	*iskb = NULL;
6148c2ecf20Sopenharmony_ci	return false;
6158c2ecf20Sopenharmony_ci}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci/**
6188c2ecf20Sopenharmony_ci * tipc_msg_reverse(): swap source and destination addresses and add error code
6198c2ecf20Sopenharmony_ci * @own_node: originating node id for reversed message
6208c2ecf20Sopenharmony_ci * @skb:  buffer containing message to be reversed; will be consumed
6218c2ecf20Sopenharmony_ci * @err:  error code to be set in message, if any
6228c2ecf20Sopenharmony_ci * Replaces consumed buffer with new one when successful
6238c2ecf20Sopenharmony_ci * Returns true if success, otherwise false
6248c2ecf20Sopenharmony_ci */
6258c2ecf20Sopenharmony_cibool tipc_msg_reverse(u32 own_node,  struct sk_buff **skb, int err)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	struct sk_buff *_skb = *skb;
6288c2ecf20Sopenharmony_ci	struct tipc_msg *_hdr, *hdr;
6298c2ecf20Sopenharmony_ci	int hlen, dlen;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	if (skb_linearize(_skb))
6328c2ecf20Sopenharmony_ci		goto exit;
6338c2ecf20Sopenharmony_ci	_hdr = buf_msg(_skb);
6348c2ecf20Sopenharmony_ci	dlen = min_t(uint, msg_data_sz(_hdr), MAX_FORWARD_SIZE);
6358c2ecf20Sopenharmony_ci	hlen = msg_hdr_sz(_hdr);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	if (msg_dest_droppable(_hdr))
6388c2ecf20Sopenharmony_ci		goto exit;
6398c2ecf20Sopenharmony_ci	if (msg_errcode(_hdr))
6408c2ecf20Sopenharmony_ci		goto exit;
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	/* Never return SHORT header */
6438c2ecf20Sopenharmony_ci	if (hlen == SHORT_H_SIZE)
6448c2ecf20Sopenharmony_ci		hlen = BASIC_H_SIZE;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	/* Don't return data along with SYN+, - sender has a clone */
6478c2ecf20Sopenharmony_ci	if (msg_is_syn(_hdr) && err == TIPC_ERR_OVERLOAD)
6488c2ecf20Sopenharmony_ci		dlen = 0;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	/* Allocate new buffer to return */
6518c2ecf20Sopenharmony_ci	*skb = tipc_buf_acquire(hlen + dlen, GFP_ATOMIC);
6528c2ecf20Sopenharmony_ci	if (!*skb)
6538c2ecf20Sopenharmony_ci		goto exit;
6548c2ecf20Sopenharmony_ci	memcpy((*skb)->data, _skb->data, msg_hdr_sz(_hdr));
6558c2ecf20Sopenharmony_ci	memcpy((*skb)->data + hlen, msg_data(_hdr), dlen);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	/* Build reverse header in new buffer */
6588c2ecf20Sopenharmony_ci	hdr = buf_msg(*skb);
6598c2ecf20Sopenharmony_ci	msg_set_hdr_sz(hdr, hlen);
6608c2ecf20Sopenharmony_ci	msg_set_errcode(hdr, err);
6618c2ecf20Sopenharmony_ci	msg_set_non_seq(hdr, 0);
6628c2ecf20Sopenharmony_ci	msg_set_origport(hdr, msg_destport(_hdr));
6638c2ecf20Sopenharmony_ci	msg_set_destport(hdr, msg_origport(_hdr));
6648c2ecf20Sopenharmony_ci	msg_set_destnode(hdr, msg_prevnode(_hdr));
6658c2ecf20Sopenharmony_ci	msg_set_prevnode(hdr, own_node);
6668c2ecf20Sopenharmony_ci	msg_set_orignode(hdr, own_node);
6678c2ecf20Sopenharmony_ci	msg_set_size(hdr, hlen + dlen);
6688c2ecf20Sopenharmony_ci	skb_orphan(_skb);
6698c2ecf20Sopenharmony_ci	kfree_skb(_skb);
6708c2ecf20Sopenharmony_ci	return true;
6718c2ecf20Sopenharmony_ciexit:
6728c2ecf20Sopenharmony_ci	kfree_skb(_skb);
6738c2ecf20Sopenharmony_ci	*skb = NULL;
6748c2ecf20Sopenharmony_ci	return false;
6758c2ecf20Sopenharmony_ci}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_cibool tipc_msg_skb_clone(struct sk_buff_head *msg, struct sk_buff_head *cpy)
6788c2ecf20Sopenharmony_ci{
6798c2ecf20Sopenharmony_ci	struct sk_buff *skb, *_skb;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	skb_queue_walk(msg, skb) {
6828c2ecf20Sopenharmony_ci		_skb = skb_clone(skb, GFP_ATOMIC);
6838c2ecf20Sopenharmony_ci		if (!_skb) {
6848c2ecf20Sopenharmony_ci			__skb_queue_purge(cpy);
6858c2ecf20Sopenharmony_ci			pr_err_ratelimited("Failed to clone buffer chain\n");
6868c2ecf20Sopenharmony_ci			return false;
6878c2ecf20Sopenharmony_ci		}
6888c2ecf20Sopenharmony_ci		__skb_queue_tail(cpy, _skb);
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci	return true;
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci/**
6948c2ecf20Sopenharmony_ci * tipc_msg_lookup_dest(): try to find new destination for named message
6958c2ecf20Sopenharmony_ci * @skb: the buffer containing the message.
6968c2ecf20Sopenharmony_ci * @err: error code to be used by caller if lookup fails
6978c2ecf20Sopenharmony_ci * Does not consume buffer
6988c2ecf20Sopenharmony_ci * Returns true if a destination is found, false otherwise
6998c2ecf20Sopenharmony_ci */
7008c2ecf20Sopenharmony_cibool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err)
7018c2ecf20Sopenharmony_ci{
7028c2ecf20Sopenharmony_ci	struct tipc_msg *msg = buf_msg(skb);
7038c2ecf20Sopenharmony_ci	u32 dport, dnode;
7048c2ecf20Sopenharmony_ci	u32 onode = tipc_own_addr(net);
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	if (!msg_isdata(msg))
7078c2ecf20Sopenharmony_ci		return false;
7088c2ecf20Sopenharmony_ci	if (!msg_named(msg))
7098c2ecf20Sopenharmony_ci		return false;
7108c2ecf20Sopenharmony_ci	if (msg_errcode(msg))
7118c2ecf20Sopenharmony_ci		return false;
7128c2ecf20Sopenharmony_ci	*err = TIPC_ERR_NO_NAME;
7138c2ecf20Sopenharmony_ci	if (skb_linearize(skb))
7148c2ecf20Sopenharmony_ci		return false;
7158c2ecf20Sopenharmony_ci	msg = buf_msg(skb);
7168c2ecf20Sopenharmony_ci	if (msg_reroute_cnt(msg))
7178c2ecf20Sopenharmony_ci		return false;
7188c2ecf20Sopenharmony_ci	dnode = tipc_scope2node(net, msg_lookup_scope(msg));
7198c2ecf20Sopenharmony_ci	dport = tipc_nametbl_translate(net, msg_nametype(msg),
7208c2ecf20Sopenharmony_ci				       msg_nameinst(msg), &dnode);
7218c2ecf20Sopenharmony_ci	if (!dport)
7228c2ecf20Sopenharmony_ci		return false;
7238c2ecf20Sopenharmony_ci	msg_incr_reroute_cnt(msg);
7248c2ecf20Sopenharmony_ci	if (dnode != onode)
7258c2ecf20Sopenharmony_ci		msg_set_prevnode(msg, onode);
7268c2ecf20Sopenharmony_ci	msg_set_destnode(msg, dnode);
7278c2ecf20Sopenharmony_ci	msg_set_destport(msg, dport);
7288c2ecf20Sopenharmony_ci	*err = TIPC_OK;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	return true;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci/* tipc_msg_assemble() - assemble chain of fragments into one message
7348c2ecf20Sopenharmony_ci */
7358c2ecf20Sopenharmony_cibool tipc_msg_assemble(struct sk_buff_head *list)
7368c2ecf20Sopenharmony_ci{
7378c2ecf20Sopenharmony_ci	struct sk_buff *skb, *tmp = NULL;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	if (skb_queue_len(list) == 1)
7408c2ecf20Sopenharmony_ci		return true;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	while ((skb = __skb_dequeue(list))) {
7438c2ecf20Sopenharmony_ci		skb->next = NULL;
7448c2ecf20Sopenharmony_ci		if (tipc_buf_append(&tmp, &skb)) {
7458c2ecf20Sopenharmony_ci			__skb_queue_tail(list, skb);
7468c2ecf20Sopenharmony_ci			return true;
7478c2ecf20Sopenharmony_ci		}
7488c2ecf20Sopenharmony_ci		if (!tmp)
7498c2ecf20Sopenharmony_ci			break;
7508c2ecf20Sopenharmony_ci	}
7518c2ecf20Sopenharmony_ci	__skb_queue_purge(list);
7528c2ecf20Sopenharmony_ci	__skb_queue_head_init(list);
7538c2ecf20Sopenharmony_ci	pr_warn("Failed do assemble buffer\n");
7548c2ecf20Sopenharmony_ci	return false;
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci/* tipc_msg_reassemble() - clone a buffer chain of fragments and
7588c2ecf20Sopenharmony_ci *                         reassemble the clones into one message
7598c2ecf20Sopenharmony_ci */
7608c2ecf20Sopenharmony_cibool tipc_msg_reassemble(struct sk_buff_head *list, struct sk_buff_head *rcvq)
7618c2ecf20Sopenharmony_ci{
7628c2ecf20Sopenharmony_ci	struct sk_buff *skb, *_skb;
7638c2ecf20Sopenharmony_ci	struct sk_buff *frag = NULL;
7648c2ecf20Sopenharmony_ci	struct sk_buff *head = NULL;
7658c2ecf20Sopenharmony_ci	int hdr_len;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	/* Copy header if single buffer */
7688c2ecf20Sopenharmony_ci	if (skb_queue_len(list) == 1) {
7698c2ecf20Sopenharmony_ci		skb = skb_peek(list);
7708c2ecf20Sopenharmony_ci		hdr_len = skb_headroom(skb) + msg_hdr_sz(buf_msg(skb));
7718c2ecf20Sopenharmony_ci		_skb = __pskb_copy(skb, hdr_len, GFP_ATOMIC);
7728c2ecf20Sopenharmony_ci		if (!_skb)
7738c2ecf20Sopenharmony_ci			return false;
7748c2ecf20Sopenharmony_ci		__skb_queue_tail(rcvq, _skb);
7758c2ecf20Sopenharmony_ci		return true;
7768c2ecf20Sopenharmony_ci	}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	/* Clone all fragments and reassemble */
7798c2ecf20Sopenharmony_ci	skb_queue_walk(list, skb) {
7808c2ecf20Sopenharmony_ci		frag = skb_clone(skb, GFP_ATOMIC);
7818c2ecf20Sopenharmony_ci		if (!frag)
7828c2ecf20Sopenharmony_ci			goto error;
7838c2ecf20Sopenharmony_ci		frag->next = NULL;
7848c2ecf20Sopenharmony_ci		if (tipc_buf_append(&head, &frag))
7858c2ecf20Sopenharmony_ci			break;
7868c2ecf20Sopenharmony_ci		if (!head)
7878c2ecf20Sopenharmony_ci			goto error;
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci	__skb_queue_tail(rcvq, frag);
7908c2ecf20Sopenharmony_ci	return true;
7918c2ecf20Sopenharmony_cierror:
7928c2ecf20Sopenharmony_ci	pr_warn("Failed do clone local mcast rcv buffer\n");
7938c2ecf20Sopenharmony_ci	kfree_skb(head);
7948c2ecf20Sopenharmony_ci	return false;
7958c2ecf20Sopenharmony_ci}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_cibool tipc_msg_pskb_copy(u32 dst, struct sk_buff_head *msg,
7988c2ecf20Sopenharmony_ci			struct sk_buff_head *cpy)
7998c2ecf20Sopenharmony_ci{
8008c2ecf20Sopenharmony_ci	struct sk_buff *skb, *_skb;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	skb_queue_walk(msg, skb) {
8038c2ecf20Sopenharmony_ci		_skb = pskb_copy(skb, GFP_ATOMIC);
8048c2ecf20Sopenharmony_ci		if (!_skb) {
8058c2ecf20Sopenharmony_ci			__skb_queue_purge(cpy);
8068c2ecf20Sopenharmony_ci			return false;
8078c2ecf20Sopenharmony_ci		}
8088c2ecf20Sopenharmony_ci		msg_set_destnode(buf_msg(_skb), dst);
8098c2ecf20Sopenharmony_ci		__skb_queue_tail(cpy, _skb);
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci	return true;
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci/* tipc_skb_queue_sorted(); sort pkt into list according to sequence number
8158c2ecf20Sopenharmony_ci * @list: list to be appended to
8168c2ecf20Sopenharmony_ci * @seqno: sequence number of buffer to add
8178c2ecf20Sopenharmony_ci * @skb: buffer to add
8188c2ecf20Sopenharmony_ci */
8198c2ecf20Sopenharmony_cibool __tipc_skb_queue_sorted(struct sk_buff_head *list, u16 seqno,
8208c2ecf20Sopenharmony_ci			     struct sk_buff *skb)
8218c2ecf20Sopenharmony_ci{
8228c2ecf20Sopenharmony_ci	struct sk_buff *_skb, *tmp;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	if (skb_queue_empty(list) || less(seqno, buf_seqno(skb_peek(list)))) {
8258c2ecf20Sopenharmony_ci		__skb_queue_head(list, skb);
8268c2ecf20Sopenharmony_ci		return true;
8278c2ecf20Sopenharmony_ci	}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	if (more(seqno, buf_seqno(skb_peek_tail(list)))) {
8308c2ecf20Sopenharmony_ci		__skb_queue_tail(list, skb);
8318c2ecf20Sopenharmony_ci		return true;
8328c2ecf20Sopenharmony_ci	}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	skb_queue_walk_safe(list, _skb, tmp) {
8358c2ecf20Sopenharmony_ci		if (more(seqno, buf_seqno(_skb)))
8368c2ecf20Sopenharmony_ci			continue;
8378c2ecf20Sopenharmony_ci		if (seqno == buf_seqno(_skb))
8388c2ecf20Sopenharmony_ci			break;
8398c2ecf20Sopenharmony_ci		__skb_queue_before(list, _skb, skb);
8408c2ecf20Sopenharmony_ci		return true;
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci	kfree_skb(skb);
8438c2ecf20Sopenharmony_ci	return false;
8448c2ecf20Sopenharmony_ci}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_civoid tipc_skb_reject(struct net *net, int err, struct sk_buff *skb,
8478c2ecf20Sopenharmony_ci		     struct sk_buff_head *xmitq)
8488c2ecf20Sopenharmony_ci{
8498c2ecf20Sopenharmony_ci	if (tipc_msg_reverse(tipc_own_addr(net), &skb, err))
8508c2ecf20Sopenharmony_ci		__skb_queue_tail(xmitq, skb);
8518c2ecf20Sopenharmony_ci}
852