xref: /openbmc/linux/net/tipc/bcast.c (revision 75cee397)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/bcast.c: TIPC broadcast code
3b97bf3fdSPer Liden  *
44c94cc2dSJon Maloy  * Copyright (c) 2004-2006, 2014-2017, Ericsson AB
5b97bf3fdSPer Liden  * Copyright (c) 2004, Intel Corporation.
62d627b92SAllan Stephens  * Copyright (c) 2005, 2010-2011, Wind River Systems
7b97bf3fdSPer Liden  * All rights reserved.
8b97bf3fdSPer Liden  *
9b97bf3fdSPer Liden  * Redistribution and use in source and binary forms, with or without
10b97bf3fdSPer Liden  * modification, are permitted provided that the following conditions are met:
11b97bf3fdSPer Liden  *
129ea1fd3cSPer Liden  * 1. Redistributions of source code must retain the above copyright
139ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer.
149ea1fd3cSPer Liden  * 2. Redistributions in binary form must reproduce the above copyright
159ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer in the
169ea1fd3cSPer Liden  *    documentation and/or other materials provided with the distribution.
179ea1fd3cSPer Liden  * 3. Neither the names of the copyright holders nor the names of its
189ea1fd3cSPer Liden  *    contributors may be used to endorse or promote products derived from
199ea1fd3cSPer Liden  *    this software without specific prior written permission.
209ea1fd3cSPer Liden  *
219ea1fd3cSPer Liden  * Alternatively, this software may be distributed under the terms of the
229ea1fd3cSPer Liden  * GNU General Public License ("GPL") version 2 as published by the Free
239ea1fd3cSPer Liden  * Software Foundation.
24b97bf3fdSPer Liden  *
25b97bf3fdSPer Liden  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26b97bf3fdSPer Liden  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27b97bf3fdSPer Liden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28b97bf3fdSPer Liden  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29b97bf3fdSPer Liden  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30b97bf3fdSPer Liden  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31b97bf3fdSPer Liden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32b97bf3fdSPer Liden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33b97bf3fdSPer Liden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34b97bf3fdSPer Liden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35b97bf3fdSPer Liden  * POSSIBILITY OF SUCH DAMAGE.
36b97bf3fdSPer Liden  */
37b97bf3fdSPer Liden 
386beb19a6SJon Paul Maloy #include <linux/tipc_config.h>
39078bec82SJon Paul Maloy #include "socket.h"
40078bec82SJon Paul Maloy #include "msg.h"
41b97bf3fdSPer Liden #include "bcast.h"
426beb19a6SJon Paul Maloy #include "link.h"
432ae0b8afSJon Paul Maloy #include "name_table.h"
44b97bf3fdSPer Liden 
4553387c4eSJon Paul Maloy #define BCLINK_WIN_DEFAULT  50	/* bcast link window size (default) */
4653387c4eSJon Paul Maloy #define BCLINK_WIN_MIN      32	/* bcast minimum link window size */
47c47e9b91SAllan Stephens 
483aec9cc9SAllan Stephens const char tipc_bclink_name[] = "broadcast-link";
49a91d55d1STuong Lien unsigned long sysctl_tipc_bc_retruni __read_mostly;
50b97bf3fdSPer Liden 
516beb19a6SJon Paul Maloy /**
522af5ae37SJon Paul Maloy  * struct tipc_bc_base - base structure for keeping broadcast send state
53b06b281eSJon Paul Maloy  * @link: broadcast send link structure
542af5ae37SJon Paul Maloy  * @inputq: data input queue; will only carry SOCK_WAKEUP messages
559cc1bf39SZhenbo Gao  * @dests: array keeping number of reachable destinations per bearer
562af5ae37SJon Paul Maloy  * @primary_bearer: a bearer having links to all broadcast destinations, if any
579999974aSJon Paul Maloy  * @bcast_support: indicates if primary bearer, if any, supports broadcast
5802ec6cafSHoang Le  * @force_bcast: forces broadcast for multicast traffic
5901fd12bbSJon Paul Maloy  * @rcast_support: indicates if all peer nodes support replicast
6002ec6cafSHoang Le  * @force_rcast: forces replicast for multicast traffic
6101fd12bbSJon Paul Maloy  * @rc_ratio: dest count as percentage of cluster size where send method changes
629cc1bf39SZhenbo Gao  * @bc_threshold: calculated from rc_ratio; if dests > threshold use broadcast
636beb19a6SJon Paul Maloy  */
646beb19a6SJon Paul Maloy struct tipc_bc_base {
6532301906SJon Paul Maloy 	struct tipc_link *link;
666beb19a6SJon Paul Maloy 	struct sk_buff_head inputq;
67b06b281eSJon Paul Maloy 	int dests[MAX_BEARERS];
68b06b281eSJon Paul Maloy 	int primary_bearer;
699999974aSJon Paul Maloy 	bool bcast_support;
7002ec6cafSHoang Le 	bool force_bcast;
7101fd12bbSJon Paul Maloy 	bool rcast_support;
7202ec6cafSHoang Le 	bool force_rcast;
7301fd12bbSJon Paul Maloy 	int rc_ratio;
7401fd12bbSJon Paul Maloy 	int bc_threshold;
756beb19a6SJon Paul Maloy };
766beb19a6SJon Paul Maloy 
775fd9fd63SJon Paul Maloy static struct tipc_bc_base *tipc_bc_base(struct net *net)
785fd9fd63SJon Paul Maloy {
795fd9fd63SJon Paul Maloy 	return tipc_net(net)->bcbase;
805fd9fd63SJon Paul Maloy }
815fd9fd63SJon Paul Maloy 
824c94cc2dSJon Maloy /* tipc_bcast_get_mtu(): -get the MTU currently used by broadcast link
834c94cc2dSJon Maloy  * Note: the MTU is decremented to give room for a tunnel header, in
844c94cc2dSJon Maloy  * case the message needs to be sent as replicast
854c94cc2dSJon Maloy  */
86959e1781SJon Paul Maloy int tipc_bcast_get_mtu(struct net *net)
87078bec82SJon Paul Maloy {
88fc1b6d6dSTuong Lien 	return tipc_link_mss(tipc_bc_sndlink(net));
89078bec82SJon Paul Maloy }
90078bec82SJon Paul Maloy 
91ba5f6a86SHoang Le void tipc_bcast_toggle_rcast(struct net *net, bool supp)
9201fd12bbSJon Paul Maloy {
93ba5f6a86SHoang Le 	tipc_bc_base(net)->rcast_support = supp;
9401fd12bbSJon Paul Maloy }
9501fd12bbSJon Paul Maloy 
9601fd12bbSJon Paul Maloy static void tipc_bcbase_calc_bc_threshold(struct net *net)
9701fd12bbSJon Paul Maloy {
9801fd12bbSJon Paul Maloy 	struct tipc_bc_base *bb = tipc_bc_base(net);
9901fd12bbSJon Paul Maloy 	int cluster_size = tipc_link_bc_peers(tipc_bc_sndlink(net));
10001fd12bbSJon Paul Maloy 
10101fd12bbSJon Paul Maloy 	bb->bc_threshold = 1 + (cluster_size * bb->rc_ratio / 100);
10201fd12bbSJon Paul Maloy }
10301fd12bbSJon Paul Maloy 
104b06b281eSJon Paul Maloy /* tipc_bcbase_select_primary(): find a bearer with links to all destinations,
105b06b281eSJon Paul Maloy  *                               if any, and make it primary bearer
106b06b281eSJon Paul Maloy  */
107b06b281eSJon Paul Maloy static void tipc_bcbase_select_primary(struct net *net)
108b06b281eSJon Paul Maloy {
109b06b281eSJon Paul Maloy 	struct tipc_bc_base *bb = tipc_bc_base(net);
110b06b281eSJon Paul Maloy 	int all_dests =  tipc_link_bc_peers(bb->link);
11175cee397SHoang Huu Le 	int max_win = tipc_link_max_win(bb->link);
1129999974aSJon Paul Maloy 	int i, mtu, prim;
113b06b281eSJon Paul Maloy 
114b06b281eSJon Paul Maloy 	bb->primary_bearer = INVALID_BEARER_ID;
1159999974aSJon Paul Maloy 	bb->bcast_support = true;
116b06b281eSJon Paul Maloy 
117b06b281eSJon Paul Maloy 	if (!all_dests)
118b06b281eSJon Paul Maloy 		return;
119b06b281eSJon Paul Maloy 
120b06b281eSJon Paul Maloy 	for (i = 0; i < MAX_BEARERS; i++) {
121959e1781SJon Paul Maloy 		if (!bb->dests[i])
122959e1781SJon Paul Maloy 			continue;
123959e1781SJon Paul Maloy 
124959e1781SJon Paul Maloy 		mtu = tipc_bearer_mtu(net, i);
12575cee397SHoang Huu Le 		if (mtu < tipc_link_mtu(bb->link)) {
126959e1781SJon Paul Maloy 			tipc_link_set_mtu(bb->link, mtu);
12775cee397SHoang Huu Le 			tipc_link_set_queue_limits(bb->link, max_win,
12875cee397SHoang Huu Le 						   max_win);
12975cee397SHoang Huu Le 		}
1309999974aSJon Paul Maloy 		bb->bcast_support &= tipc_bearer_bcast_support(net, i);
131b06b281eSJon Paul Maloy 		if (bb->dests[i] < all_dests)
132b06b281eSJon Paul Maloy 			continue;
133b06b281eSJon Paul Maloy 
134b06b281eSJon Paul Maloy 		bb->primary_bearer = i;
135b06b281eSJon Paul Maloy 
136b06b281eSJon Paul Maloy 		/* Reduce risk that all nodes select same primary */
137b06b281eSJon Paul Maloy 		if ((i ^ tipc_own_addr(net)) & 1)
138b06b281eSJon Paul Maloy 			break;
139b06b281eSJon Paul Maloy 	}
1409999974aSJon Paul Maloy 	prim = bb->primary_bearer;
1419999974aSJon Paul Maloy 	if (prim != INVALID_BEARER_ID)
1429999974aSJon Paul Maloy 		bb->bcast_support = tipc_bearer_bcast_support(net, prim);
143b06b281eSJon Paul Maloy }
144b06b281eSJon Paul Maloy 
145b06b281eSJon Paul Maloy void tipc_bcast_inc_bearer_dst_cnt(struct net *net, int bearer_id)
146b06b281eSJon Paul Maloy {
147b06b281eSJon Paul Maloy 	struct tipc_bc_base *bb = tipc_bc_base(net);
148b06b281eSJon Paul Maloy 
149b06b281eSJon Paul Maloy 	tipc_bcast_lock(net);
150b06b281eSJon Paul Maloy 	bb->dests[bearer_id]++;
151b06b281eSJon Paul Maloy 	tipc_bcbase_select_primary(net);
152b06b281eSJon Paul Maloy 	tipc_bcast_unlock(net);
153b06b281eSJon Paul Maloy }
154b06b281eSJon Paul Maloy 
155b06b281eSJon Paul Maloy void tipc_bcast_dec_bearer_dst_cnt(struct net *net, int bearer_id)
156b06b281eSJon Paul Maloy {
157b06b281eSJon Paul Maloy 	struct tipc_bc_base *bb = tipc_bc_base(net);
158b06b281eSJon Paul Maloy 
159b06b281eSJon Paul Maloy 	tipc_bcast_lock(net);
160b06b281eSJon Paul Maloy 	bb->dests[bearer_id]--;
161b06b281eSJon Paul Maloy 	tipc_bcbase_select_primary(net);
162b06b281eSJon Paul Maloy 	tipc_bcast_unlock(net);
163b06b281eSJon Paul Maloy }
164b06b281eSJon Paul Maloy 
165b06b281eSJon Paul Maloy /* tipc_bcbase_xmit - broadcast a packet queue across one or more bearers
166b06b281eSJon Paul Maloy  *
167b06b281eSJon Paul Maloy  * Note that number of reachable destinations, as indicated in the dests[]
168b06b281eSJon Paul Maloy  * array, may transitionally differ from the number of destinations indicated
169b06b281eSJon Paul Maloy  * in each sent buffer. We can sustain this. Excess destination nodes will
170b06b281eSJon Paul Maloy  * drop and never acknowledge the unexpected packets, and missing destinations
171b06b281eSJon Paul Maloy  * will either require retransmission (if they are just about to be added to
172b06b281eSJon Paul Maloy  * the bearer), or be removed from the buffer's 'ackers' counter (if they
173b06b281eSJon Paul Maloy  * just went down)
174b06b281eSJon Paul Maloy  */
175b06b281eSJon Paul Maloy static void tipc_bcbase_xmit(struct net *net, struct sk_buff_head *xmitq)
176b06b281eSJon Paul Maloy {
177b06b281eSJon Paul Maloy 	int bearer_id;
178b06b281eSJon Paul Maloy 	struct tipc_bc_base *bb = tipc_bc_base(net);
179b06b281eSJon Paul Maloy 	struct sk_buff *skb, *_skb;
180b06b281eSJon Paul Maloy 	struct sk_buff_head _xmitq;
181b06b281eSJon Paul Maloy 
182b06b281eSJon Paul Maloy 	if (skb_queue_empty(xmitq))
183b06b281eSJon Paul Maloy 		return;
184b06b281eSJon Paul Maloy 
185b06b281eSJon Paul Maloy 	/* The typical case: at least one bearer has links to all nodes */
186b06b281eSJon Paul Maloy 	bearer_id = bb->primary_bearer;
187b06b281eSJon Paul Maloy 	if (bearer_id >= 0) {
188b06b281eSJon Paul Maloy 		tipc_bearer_bc_xmit(net, bearer_id, xmitq);
189b06b281eSJon Paul Maloy 		return;
190b06b281eSJon Paul Maloy 	}
191b06b281eSJon Paul Maloy 
192b06b281eSJon Paul Maloy 	/* We have to transmit across all bearers */
193e654f9f5SJon Maloy 	__skb_queue_head_init(&_xmitq);
194b06b281eSJon Paul Maloy 	for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
195b06b281eSJon Paul Maloy 		if (!bb->dests[bearer_id])
196b06b281eSJon Paul Maloy 			continue;
197b06b281eSJon Paul Maloy 
198b06b281eSJon Paul Maloy 		skb_queue_walk(xmitq, skb) {
199b06b281eSJon Paul Maloy 			_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
200b06b281eSJon Paul Maloy 			if (!_skb)
201b06b281eSJon Paul Maloy 				break;
202b06b281eSJon Paul Maloy 			__skb_queue_tail(&_xmitq, _skb);
203b06b281eSJon Paul Maloy 		}
204b06b281eSJon Paul Maloy 		tipc_bearer_bc_xmit(net, bearer_id, &_xmitq);
205b06b281eSJon Paul Maloy 	}
206b06b281eSJon Paul Maloy 	__skb_queue_purge(xmitq);
207b06b281eSJon Paul Maloy 	__skb_queue_purge(&_xmitq);
208b06b281eSJon Paul Maloy }
209b06b281eSJon Paul Maloy 
21001fd12bbSJon Paul Maloy static void tipc_bcast_select_xmit_method(struct net *net, int dests,
21101fd12bbSJon Paul Maloy 					  struct tipc_mc_method *method)
21201fd12bbSJon Paul Maloy {
21301fd12bbSJon Paul Maloy 	struct tipc_bc_base *bb = tipc_bc_base(net);
21401fd12bbSJon Paul Maloy 	unsigned long exp = method->expires;
21501fd12bbSJon Paul Maloy 
21601fd12bbSJon Paul Maloy 	/* Broadcast supported by used bearer/bearers? */
21701fd12bbSJon Paul Maloy 	if (!bb->bcast_support) {
21801fd12bbSJon Paul Maloy 		method->rcast = true;
21901fd12bbSJon Paul Maloy 		return;
22001fd12bbSJon Paul Maloy 	}
22101fd12bbSJon Paul Maloy 	/* Any destinations which don't support replicast ? */
22201fd12bbSJon Paul Maloy 	if (!bb->rcast_support) {
22301fd12bbSJon Paul Maloy 		method->rcast = false;
22401fd12bbSJon Paul Maloy 		return;
22501fd12bbSJon Paul Maloy 	}
22601fd12bbSJon Paul Maloy 	/* Can current method be changed ? */
22701fd12bbSJon Paul Maloy 	method->expires = jiffies + TIPC_METHOD_EXPIRE;
228c55c8edaSHoang Le 	if (method->mandatory)
22901fd12bbSJon Paul Maloy 		return;
23001fd12bbSJon Paul Maloy 
231c55c8edaSHoang Le 	if (!(tipc_net(net)->capabilities & TIPC_MCAST_RBCTL) &&
232c55c8edaSHoang Le 	    time_before(jiffies, exp))
233c55c8edaSHoang Le 		return;
234c55c8edaSHoang Le 
235c55c8edaSHoang Le 	/* Configuration as force 'broadcast' method */
236c55c8edaSHoang Le 	if (bb->force_bcast) {
237c55c8edaSHoang Le 		method->rcast = false;
238c55c8edaSHoang Le 		return;
239c55c8edaSHoang Le 	}
240c55c8edaSHoang Le 	/* Configuration as force 'replicast' method */
241c55c8edaSHoang Le 	if (bb->force_rcast) {
242c55c8edaSHoang Le 		method->rcast = true;
243c55c8edaSHoang Le 		return;
244c55c8edaSHoang Le 	}
245c55c8edaSHoang Le 	/* Configuration as 'autoselect' or default method */
24601fd12bbSJon Paul Maloy 	/* Determine method to use now */
24701fd12bbSJon Paul Maloy 	method->rcast = dests <= bb->bc_threshold;
24801fd12bbSJon Paul Maloy }
24901fd12bbSJon Paul Maloy 
250a853e4c6SJon Paul Maloy /* tipc_bcast_xmit - broadcast the buffer chain to all external nodes
251f2f9800dSYing Xue  * @net: the applicable net namespace
252a853e4c6SJon Paul Maloy  * @pkts: chain of buffers containing message
253a853e4c6SJon Paul Maloy  * @cong_link_cnt: set to 1 if broadcast link is congested, otherwise 0
254365ad353SJon Paul Maloy  * Consumes the buffer chain.
255a853e4c6SJon Paul Maloy  * Returns 0 if success, otherwise errno: -EHOSTUNREACH,-EMSGSIZE
256078bec82SJon Paul Maloy  */
257cad2929dSHoang Huu Le int tipc_bcast_xmit(struct net *net, struct sk_buff_head *pkts,
258a853e4c6SJon Paul Maloy 		    u16 *cong_link_cnt)
259078bec82SJon Paul Maloy {
2602f566124SJon Paul Maloy 	struct tipc_link *l = tipc_bc_sndlink(net);
261a853e4c6SJon Paul Maloy 	struct sk_buff_head xmitq;
262078bec82SJon Paul Maloy 	int rc = 0;
263078bec82SJon Paul Maloy 
264e654f9f5SJon Maloy 	__skb_queue_head_init(&xmitq);
2652f566124SJon Paul Maloy 	tipc_bcast_lock(net);
2662f566124SJon Paul Maloy 	if (tipc_link_bc_peers(l))
267a853e4c6SJon Paul Maloy 		rc = tipc_link_xmit(l, pkts, &xmitq);
2682f566124SJon Paul Maloy 	tipc_bcast_unlock(net);
269a853e4c6SJon Paul Maloy 	tipc_bcbase_xmit(net, &xmitq);
270a853e4c6SJon Paul Maloy 	__skb_queue_purge(pkts);
271a853e4c6SJon Paul Maloy 	if (rc == -ELINKCONG) {
272a853e4c6SJon Paul Maloy 		*cong_link_cnt = 1;
273a853e4c6SJon Paul Maloy 		rc = 0;
274a853e4c6SJon Paul Maloy 	}
275cb1b7280SJon Paul Maloy 	return rc;
276cb1b7280SJon Paul Maloy }
27752666986SJon Paul Maloy 
278a853e4c6SJon Paul Maloy /* tipc_rcast_xmit - replicate and send a message to given destination nodes
279a853e4c6SJon Paul Maloy  * @net: the applicable net namespace
280a853e4c6SJon Paul Maloy  * @pkts: chain of buffers containing message
281a853e4c6SJon Paul Maloy  * @dests: list of destination nodes
282a853e4c6SJon Paul Maloy  * @cong_link_cnt: returns number of congested links
283a853e4c6SJon Paul Maloy  * @cong_links: returns identities of congested links
284a853e4c6SJon Paul Maloy  * Returns 0 if success, otherwise errno
285a853e4c6SJon Paul Maloy  */
286a853e4c6SJon Paul Maloy static int tipc_rcast_xmit(struct net *net, struct sk_buff_head *pkts,
287a853e4c6SJon Paul Maloy 			   struct tipc_nlist *dests, u16 *cong_link_cnt)
288a853e4c6SJon Paul Maloy {
289a80ae530SJon Maloy 	struct tipc_dest *dst, *tmp;
290a853e4c6SJon Paul Maloy 	struct sk_buff_head _pkts;
291a80ae530SJon Maloy 	u32 dnode, selector;
292a853e4c6SJon Paul Maloy 
293a853e4c6SJon Paul Maloy 	selector = msg_link_selector(buf_msg(skb_peek(pkts)));
294e654f9f5SJon Maloy 	__skb_queue_head_init(&_pkts);
295a853e4c6SJon Paul Maloy 
296a80ae530SJon Maloy 	list_for_each_entry_safe(dst, tmp, &dests->list, list) {
297a80ae530SJon Maloy 		dnode = dst->node;
298a80ae530SJon Maloy 		if (!tipc_msg_pskb_copy(dnode, pkts, &_pkts))
299a853e4c6SJon Paul Maloy 			return -ENOMEM;
300a853e4c6SJon Paul Maloy 
301a853e4c6SJon Paul Maloy 		/* Any other return value than -ELINKCONG is ignored */
302a80ae530SJon Maloy 		if (tipc_node_xmit(net, &_pkts, dnode, selector) == -ELINKCONG)
303a853e4c6SJon Paul Maloy 			(*cong_link_cnt)++;
304a853e4c6SJon Paul Maloy 	}
305a853e4c6SJon Paul Maloy 	return 0;
306a853e4c6SJon Paul Maloy }
307a853e4c6SJon Paul Maloy 
308c55c8edaSHoang Le /* tipc_mcast_send_sync - deliver a dummy message with SYN bit
309c55c8edaSHoang Le  * @net: the applicable net namespace
310c55c8edaSHoang Le  * @skb: socket buffer to copy
311c55c8edaSHoang Le  * @method: send method to be used
312c55c8edaSHoang Le  * @dests: destination nodes for message.
313c55c8edaSHoang Le  * Returns 0 if success, otherwise errno
314c55c8edaSHoang Le  */
315c55c8edaSHoang Le static int tipc_mcast_send_sync(struct net *net, struct sk_buff *skb,
316c55c8edaSHoang Le 				struct tipc_mc_method *method,
317dca4a17dSTuong Lien 				struct tipc_nlist *dests)
318c55c8edaSHoang Le {
319c55c8edaSHoang Le 	struct tipc_msg *hdr, *_hdr;
320c55c8edaSHoang Le 	struct sk_buff_head tmpq;
321c55c8edaSHoang Le 	struct sk_buff *_skb;
322dca4a17dSTuong Lien 	u16 cong_link_cnt;
323dca4a17dSTuong Lien 	int rc = 0;
324c55c8edaSHoang Le 
325c55c8edaSHoang Le 	/* Is a cluster supporting with new capabilities ? */
326c55c8edaSHoang Le 	if (!(tipc_net(net)->capabilities & TIPC_MCAST_RBCTL))
327c55c8edaSHoang Le 		return 0;
328c55c8edaSHoang Le 
329c55c8edaSHoang Le 	hdr = buf_msg(skb);
330c55c8edaSHoang Le 	if (msg_user(hdr) == MSG_FRAGMENTER)
331a7dc51adSJon Maloy 		hdr = msg_inner_hdr(hdr);
332c55c8edaSHoang Le 	if (msg_type(hdr) != TIPC_MCAST_MSG)
333c55c8edaSHoang Le 		return 0;
334c55c8edaSHoang Le 
335c55c8edaSHoang Le 	/* Allocate dummy message */
336c55c8edaSHoang Le 	_skb = tipc_buf_acquire(MCAST_H_SIZE, GFP_KERNEL);
3376da88a82SWei Yongjun 	if (!_skb)
338c55c8edaSHoang Le 		return -ENOMEM;
339c55c8edaSHoang Le 
340c55c8edaSHoang Le 	/* Preparing for 'synching' header */
341c55c8edaSHoang Le 	msg_set_syn(hdr, 1);
342c55c8edaSHoang Le 
343c55c8edaSHoang Le 	/* Copy skb's header into a dummy header */
344c55c8edaSHoang Le 	skb_copy_to_linear_data(_skb, hdr, MCAST_H_SIZE);
345c55c8edaSHoang Le 	skb_orphan(_skb);
346c55c8edaSHoang Le 
347c55c8edaSHoang Le 	/* Reverse method for dummy message */
348c55c8edaSHoang Le 	_hdr = buf_msg(_skb);
349c55c8edaSHoang Le 	msg_set_size(_hdr, MCAST_H_SIZE);
350c55c8edaSHoang Le 	msg_set_is_rcast(_hdr, !msg_is_rcast(hdr));
351dca4a17dSTuong Lien 	msg_set_errcode(_hdr, TIPC_ERR_NO_PORT);
352c55c8edaSHoang Le 
353e654f9f5SJon Maloy 	__skb_queue_head_init(&tmpq);
354c55c8edaSHoang Le 	__skb_queue_tail(&tmpq, _skb);
355c55c8edaSHoang Le 	if (method->rcast)
356dca4a17dSTuong Lien 		rc = tipc_bcast_xmit(net, &tmpq, &cong_link_cnt);
357c55c8edaSHoang Le 	else
358dca4a17dSTuong Lien 		rc = tipc_rcast_xmit(net, &tmpq, dests, &cong_link_cnt);
359c55c8edaSHoang Le 
360c55c8edaSHoang Le 	/* This queue should normally be empty by now */
361c55c8edaSHoang Le 	__skb_queue_purge(&tmpq);
362c55c8edaSHoang Le 
363dca4a17dSTuong Lien 	return rc;
364c55c8edaSHoang Le }
365c55c8edaSHoang Le 
366a853e4c6SJon Paul Maloy /* tipc_mcast_xmit - deliver message to indicated destination nodes
367a853e4c6SJon Paul Maloy  *                   and to identified node local sockets
368a853e4c6SJon Paul Maloy  * @net: the applicable net namespace
369a853e4c6SJon Paul Maloy  * @pkts: chain of buffers containing message
37001fd12bbSJon Paul Maloy  * @method: send method to be used
37101fd12bbSJon Paul Maloy  * @dests: destination nodes for message.
372a853e4c6SJon Paul Maloy  * @cong_link_cnt: returns number of encountered congested destination links
373a853e4c6SJon Paul Maloy  * Consumes buffer chain.
374a853e4c6SJon Paul Maloy  * Returns 0 if success, otherwise errno
375a853e4c6SJon Paul Maloy  */
376a853e4c6SJon Paul Maloy int tipc_mcast_xmit(struct net *net, struct sk_buff_head *pkts,
37701fd12bbSJon Paul Maloy 		    struct tipc_mc_method *method, struct tipc_nlist *dests,
37801fd12bbSJon Paul Maloy 		    u16 *cong_link_cnt)
379a853e4c6SJon Paul Maloy {
380a853e4c6SJon Paul Maloy 	struct sk_buff_head inputq, localq;
381c55c8edaSHoang Le 	bool rcast = method->rcast;
382c55c8edaSHoang Le 	struct tipc_msg *hdr;
383c55c8edaSHoang Le 	struct sk_buff *skb;
384a853e4c6SJon Paul Maloy 	int rc = 0;
385a853e4c6SJon Paul Maloy 
386a853e4c6SJon Paul Maloy 	skb_queue_head_init(&inputq);
387e654f9f5SJon Maloy 	__skb_queue_head_init(&localq);
388a853e4c6SJon Paul Maloy 
389a853e4c6SJon Paul Maloy 	/* Clone packets before they are consumed by next call */
390a853e4c6SJon Paul Maloy 	if (dests->local && !tipc_msg_reassemble(pkts, &localq)) {
391a853e4c6SJon Paul Maloy 		rc = -ENOMEM;
392a853e4c6SJon Paul Maloy 		goto exit;
393a853e4c6SJon Paul Maloy 	}
39401fd12bbSJon Paul Maloy 	/* Send according to determined transmit method */
395a853e4c6SJon Paul Maloy 	if (dests->remote) {
39601fd12bbSJon Paul Maloy 		tipc_bcast_select_xmit_method(net, dests->remote, method);
397c55c8edaSHoang Le 
398c55c8edaSHoang Le 		skb = skb_peek(pkts);
399c55c8edaSHoang Le 		hdr = buf_msg(skb);
400c55c8edaSHoang Le 		if (msg_user(hdr) == MSG_FRAGMENTER)
401a7dc51adSJon Maloy 			hdr = msg_inner_hdr(hdr);
402c55c8edaSHoang Le 		msg_set_is_rcast(hdr, method->rcast);
403c55c8edaSHoang Le 
404c55c8edaSHoang Le 		/* Switch method ? */
405dca4a17dSTuong Lien 		if (rcast != method->rcast) {
406dca4a17dSTuong Lien 			rc = tipc_mcast_send_sync(net, skb, method, dests);
407dca4a17dSTuong Lien 			if (unlikely(rc)) {
408dca4a17dSTuong Lien 				pr_err("Unable to send SYN: method %d, rc %d\n",
409dca4a17dSTuong Lien 				       rcast, rc);
410dca4a17dSTuong Lien 				goto exit;
411dca4a17dSTuong Lien 			}
412dca4a17dSTuong Lien 		}
413c55c8edaSHoang Le 
41401fd12bbSJon Paul Maloy 		if (method->rcast)
415a853e4c6SJon Paul Maloy 			rc = tipc_rcast_xmit(net, pkts, dests, cong_link_cnt);
416a853e4c6SJon Paul Maloy 		else
417a853e4c6SJon Paul Maloy 			rc = tipc_bcast_xmit(net, pkts, cong_link_cnt);
418a853e4c6SJon Paul Maloy 	}
419a853e4c6SJon Paul Maloy 
4206c9081a3SJohn Rutherford 	if (dests->local) {
4216c9081a3SJohn Rutherford 		tipc_loopback_trace(net, &localq);
422a853e4c6SJon Paul Maloy 		tipc_sk_mcast_rcv(net, &localq, &inputq);
4236c9081a3SJohn Rutherford 	}
424a853e4c6SJon Paul Maloy exit:
42501fd12bbSJon Paul Maloy 	/* This queue should normally be empty by now */
426a853e4c6SJon Paul Maloy 	__skb_queue_purge(pkts);
427365ad353SJon Paul Maloy 	return rc;
428078bec82SJon Paul Maloy }
42952666986SJon Paul Maloy 
43052666986SJon Paul Maloy /* tipc_bcast_rcv - receive a broadcast packet, and deliver to rcv link
43152666986SJon Paul Maloy  *
43252666986SJon Paul Maloy  * RCU is locked, no other locks set
43352666986SJon Paul Maloy  */
43452666986SJon Paul Maloy int tipc_bcast_rcv(struct net *net, struct tipc_link *l, struct sk_buff *skb)
43552666986SJon Paul Maloy {
43652666986SJon Paul Maloy 	struct tipc_msg *hdr = buf_msg(skb);
43752666986SJon Paul Maloy 	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
43852666986SJon Paul Maloy 	struct sk_buff_head xmitq;
43952666986SJon Paul Maloy 	int rc;
44052666986SJon Paul Maloy 
44152666986SJon Paul Maloy 	__skb_queue_head_init(&xmitq);
44252666986SJon Paul Maloy 
44352666986SJon Paul Maloy 	if (msg_mc_netid(hdr) != tipc_netid(net) || !tipc_link_is_up(l)) {
44452666986SJon Paul Maloy 		kfree_skb(skb);
44552666986SJon Paul Maloy 		return 0;
44652666986SJon Paul Maloy 	}
44752666986SJon Paul Maloy 
44852666986SJon Paul Maloy 	tipc_bcast_lock(net);
44952666986SJon Paul Maloy 	if (msg_user(hdr) == BCAST_PROTOCOL)
45052666986SJon Paul Maloy 		rc = tipc_link_bc_nack_rcv(l, skb, &xmitq);
45152666986SJon Paul Maloy 	else
45252666986SJon Paul Maloy 		rc = tipc_link_rcv(l, skb, NULL);
45352666986SJon Paul Maloy 	tipc_bcast_unlock(net);
45452666986SJon Paul Maloy 
455b06b281eSJon Paul Maloy 	tipc_bcbase_xmit(net, &xmitq);
45652666986SJon Paul Maloy 
45752666986SJon Paul Maloy 	/* Any socket wakeup messages ? */
45852666986SJon Paul Maloy 	if (!skb_queue_empty(inputq))
45952666986SJon Paul Maloy 		tipc_sk_rcv(net, inputq);
46052666986SJon Paul Maloy 
46152666986SJon Paul Maloy 	return rc;
46252666986SJon Paul Maloy }
46352666986SJon Paul Maloy 
46452666986SJon Paul Maloy /* tipc_bcast_ack_rcv - receive and handle a broadcast acknowledge
46552666986SJon Paul Maloy  *
46652666986SJon Paul Maloy  * RCU is locked, no other locks set
46752666986SJon Paul Maloy  */
46806bd2b1eSJon Paul Maloy void tipc_bcast_ack_rcv(struct net *net, struct tipc_link *l,
46906bd2b1eSJon Paul Maloy 			struct tipc_msg *hdr)
47052666986SJon Paul Maloy {
47152666986SJon Paul Maloy 	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
47206bd2b1eSJon Paul Maloy 	u16 acked = msg_bcast_ack(hdr);
47352666986SJon Paul Maloy 	struct sk_buff_head xmitq;
47452666986SJon Paul Maloy 
47506bd2b1eSJon Paul Maloy 	/* Ignore bc acks sent by peer before bcast synch point was received */
47606bd2b1eSJon Paul Maloy 	if (msg_bc_ack_invalid(hdr))
47706bd2b1eSJon Paul Maloy 		return;
47806bd2b1eSJon Paul Maloy 
47952666986SJon Paul Maloy 	__skb_queue_head_init(&xmitq);
48052666986SJon Paul Maloy 
48152666986SJon Paul Maloy 	tipc_bcast_lock(net);
482a91d55d1STuong Lien 	tipc_link_bc_ack_rcv(l, acked, 0, NULL, &xmitq, NULL);
48352666986SJon Paul Maloy 	tipc_bcast_unlock(net);
48452666986SJon Paul Maloy 
485b06b281eSJon Paul Maloy 	tipc_bcbase_xmit(net, &xmitq);
48652666986SJon Paul Maloy 
48752666986SJon Paul Maloy 	/* Any socket wakeup messages ? */
48852666986SJon Paul Maloy 	if (!skb_queue_empty(inputq))
48952666986SJon Paul Maloy 		tipc_sk_rcv(net, inputq);
49052666986SJon Paul Maloy }
49152666986SJon Paul Maloy 
49252666986SJon Paul Maloy /* tipc_bcast_synch_rcv -  check and update rcv link with peer's send state
49352666986SJon Paul Maloy  *
49452666986SJon Paul Maloy  * RCU is locked, no other locks set
49552666986SJon Paul Maloy  */
49602d11ca2SJon Paul Maloy int tipc_bcast_sync_rcv(struct net *net, struct tipc_link *l,
497a91d55d1STuong Lien 			struct tipc_msg *hdr,
498a91d55d1STuong Lien 			struct sk_buff_head *retrq)
49952666986SJon Paul Maloy {
50052666986SJon Paul Maloy 	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
501d7626b5aSTuong Lien 	struct tipc_gap_ack_blks *ga;
50252666986SJon Paul Maloy 	struct sk_buff_head xmitq;
50302d11ca2SJon Paul Maloy 	int rc = 0;
50452666986SJon Paul Maloy 
50552666986SJon Paul Maloy 	__skb_queue_head_init(&xmitq);
50652666986SJon Paul Maloy 
50752666986SJon Paul Maloy 	tipc_bcast_lock(net);
50806bd2b1eSJon Paul Maloy 	if (msg_type(hdr) != STATE_MSG) {
50906bd2b1eSJon Paul Maloy 		tipc_link_bc_init_rcv(l, hdr);
51006bd2b1eSJon Paul Maloy 	} else if (!msg_bc_ack_invalid(hdr)) {
511d7626b5aSTuong Lien 		tipc_get_gap_ack_blks(&ga, l, hdr, false);
512a91d55d1STuong Lien 		if (!sysctl_tipc_bc_retruni)
513a91d55d1STuong Lien 			retrq = &xmitq;
514d7626b5aSTuong Lien 		rc = tipc_link_bc_ack_rcv(l, msg_bcast_ack(hdr),
515a91d55d1STuong Lien 					  msg_bc_gap(hdr), ga, &xmitq,
516a91d55d1STuong Lien 					  retrq);
517d7626b5aSTuong Lien 		rc |= tipc_link_bc_sync_rcv(l, hdr, &xmitq);
51852666986SJon Paul Maloy 	}
51952666986SJon Paul Maloy 	tipc_bcast_unlock(net);
52052666986SJon Paul Maloy 
521b06b281eSJon Paul Maloy 	tipc_bcbase_xmit(net, &xmitq);
52252666986SJon Paul Maloy 
52352666986SJon Paul Maloy 	/* Any socket wakeup messages ? */
52452666986SJon Paul Maloy 	if (!skb_queue_empty(inputq))
52552666986SJon Paul Maloy 		tipc_sk_rcv(net, inputq);
52602d11ca2SJon Paul Maloy 	return rc;
52752666986SJon Paul Maloy }
52852666986SJon Paul Maloy 
52952666986SJon Paul Maloy /* tipc_bcast_add_peer - add a peer node to broadcast link and bearer
53052666986SJon Paul Maloy  *
53152666986SJon Paul Maloy  * RCU is locked, node lock is set
53252666986SJon Paul Maloy  */
533b06b281eSJon Paul Maloy void tipc_bcast_add_peer(struct net *net, struct tipc_link *uc_l,
53452666986SJon Paul Maloy 			 struct sk_buff_head *xmitq)
53552666986SJon Paul Maloy {
53652666986SJon Paul Maloy 	struct tipc_link *snd_l = tipc_bc_sndlink(net);
53752666986SJon Paul Maloy 
538b06b281eSJon Paul Maloy 	tipc_bcast_lock(net);
53952666986SJon Paul Maloy 	tipc_link_add_bc_peer(snd_l, uc_l, xmitq);
540b06b281eSJon Paul Maloy 	tipc_bcbase_select_primary(net);
54101fd12bbSJon Paul Maloy 	tipc_bcbase_calc_bc_threshold(net);
542b06b281eSJon Paul Maloy 	tipc_bcast_unlock(net);
54352666986SJon Paul Maloy }
54452666986SJon Paul Maloy 
54552666986SJon Paul Maloy /* tipc_bcast_remove_peer - remove a peer node from broadcast link and bearer
54652666986SJon Paul Maloy  *
54752666986SJon Paul Maloy  * RCU is locked, node lock is set
54852666986SJon Paul Maloy  */
549b06b281eSJon Paul Maloy void tipc_bcast_remove_peer(struct net *net, struct tipc_link *rcv_l)
55052666986SJon Paul Maloy {
55152666986SJon Paul Maloy 	struct tipc_link *snd_l = tipc_bc_sndlink(net);
552b06b281eSJon Paul Maloy 	struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
55352666986SJon Paul Maloy 	struct sk_buff_head xmitq;
55452666986SJon Paul Maloy 
55552666986SJon Paul Maloy 	__skb_queue_head_init(&xmitq);
55652666986SJon Paul Maloy 
557b06b281eSJon Paul Maloy 	tipc_bcast_lock(net);
55852666986SJon Paul Maloy 	tipc_link_remove_bc_peer(snd_l, rcv_l, &xmitq);
559b06b281eSJon Paul Maloy 	tipc_bcbase_select_primary(net);
56001fd12bbSJon Paul Maloy 	tipc_bcbase_calc_bc_threshold(net);
561b06b281eSJon Paul Maloy 	tipc_bcast_unlock(net);
56252666986SJon Paul Maloy 
563b06b281eSJon Paul Maloy 	tipc_bcbase_xmit(net, &xmitq);
56452666986SJon Paul Maloy 
56552666986SJon Paul Maloy 	/* Any socket wakeup messages ? */
56652666986SJon Paul Maloy 	if (!skb_queue_empty(inputq))
56752666986SJon Paul Maloy 		tipc_sk_rcv(net, inputq);
56852666986SJon Paul Maloy }
56952666986SJon Paul Maloy 
57003b6fefdSTuong Lien int tipc_bclink_reset_stats(struct net *net, struct tipc_link *l)
571b97bf3fdSPer Liden {
57238206d59SJon Paul Maloy 	if (!l)
573b97bf3fdSPer Liden 		return -ENOPROTOOPT;
574b97bf3fdSPer Liden 
5752af5ae37SJon Paul Maloy 	tipc_bcast_lock(net);
57638206d59SJon Paul Maloy 	tipc_link_reset_stats(l);
5772af5ae37SJon Paul Maloy 	tipc_bcast_unlock(net);
5780e35fd5eSAllan Stephens 	return 0;
579b97bf3fdSPer Liden }
580b97bf3fdSPer Liden 
58116ad3f40SJon Maloy static int tipc_bc_link_set_queue_limits(struct net *net, u32 max_win)
582b97bf3fdSPer Liden {
5832af5ae37SJon Paul Maloy 	struct tipc_link *l = tipc_bc_sndlink(net);
5841da46568SYing Xue 
5852af5ae37SJon Paul Maloy 	if (!l)
586b97bf3fdSPer Liden 		return -ENOPROTOOPT;
58716ad3f40SJon Maloy 	if (max_win < BCLINK_WIN_MIN)
58816ad3f40SJon Maloy 		max_win = BCLINK_WIN_MIN;
58916ad3f40SJon Maloy 	if (max_win > TIPC_MAX_LINK_WIN)
590b97bf3fdSPer Liden 		return -EINVAL;
5912af5ae37SJon Paul Maloy 	tipc_bcast_lock(net);
59216ad3f40SJon Maloy 	tipc_link_set_queue_limits(l, BCLINK_WIN_MIN, max_win);
5932af5ae37SJon Paul Maloy 	tipc_bcast_unlock(net);
5940e35fd5eSAllan Stephens 	return 0;
595b97bf3fdSPer Liden }
596b97bf3fdSPer Liden 
59702ec6cafSHoang Le static int tipc_bc_link_set_broadcast_mode(struct net *net, u32 bc_mode)
59802ec6cafSHoang Le {
59902ec6cafSHoang Le 	struct tipc_bc_base *bb = tipc_bc_base(net);
60002ec6cafSHoang Le 
60102ec6cafSHoang Le 	switch (bc_mode) {
60202ec6cafSHoang Le 	case BCLINK_MODE_BCAST:
60302ec6cafSHoang Le 		if (!bb->bcast_support)
60402ec6cafSHoang Le 			return -ENOPROTOOPT;
60502ec6cafSHoang Le 
60602ec6cafSHoang Le 		bb->force_bcast = true;
60702ec6cafSHoang Le 		bb->force_rcast = false;
60802ec6cafSHoang Le 		break;
60902ec6cafSHoang Le 	case BCLINK_MODE_RCAST:
61002ec6cafSHoang Le 		if (!bb->rcast_support)
61102ec6cafSHoang Le 			return -ENOPROTOOPT;
61202ec6cafSHoang Le 
61302ec6cafSHoang Le 		bb->force_bcast = false;
61402ec6cafSHoang Le 		bb->force_rcast = true;
61502ec6cafSHoang Le 		break;
61602ec6cafSHoang Le 	case BCLINK_MODE_SEL:
61702ec6cafSHoang Le 		if (!bb->bcast_support || !bb->rcast_support)
61802ec6cafSHoang Le 			return -ENOPROTOOPT;
61902ec6cafSHoang Le 
62002ec6cafSHoang Le 		bb->force_bcast = false;
62102ec6cafSHoang Le 		bb->force_rcast = false;
62202ec6cafSHoang Le 		break;
62302ec6cafSHoang Le 	default:
62402ec6cafSHoang Le 		return -EINVAL;
62502ec6cafSHoang Le 	}
62602ec6cafSHoang Le 
62702ec6cafSHoang Le 	return 0;
62802ec6cafSHoang Le }
62902ec6cafSHoang Le 
63002ec6cafSHoang Le static int tipc_bc_link_set_broadcast_ratio(struct net *net, u32 bc_ratio)
63102ec6cafSHoang Le {
63202ec6cafSHoang Le 	struct tipc_bc_base *bb = tipc_bc_base(net);
63302ec6cafSHoang Le 
63402ec6cafSHoang Le 	if (!bb->bcast_support || !bb->rcast_support)
63502ec6cafSHoang Le 		return -ENOPROTOOPT;
63602ec6cafSHoang Le 
63702ec6cafSHoang Le 	if (bc_ratio > 100 || bc_ratio <= 0)
63802ec6cafSHoang Le 		return -EINVAL;
63902ec6cafSHoang Le 
64002ec6cafSHoang Le 	bb->rc_ratio = bc_ratio;
64102ec6cafSHoang Le 	tipc_bcast_lock(net);
64202ec6cafSHoang Le 	tipc_bcbase_calc_bc_threshold(net);
64302ec6cafSHoang Le 	tipc_bcast_unlock(net);
64402ec6cafSHoang Le 
64502ec6cafSHoang Le 	return 0;
64602ec6cafSHoang Le }
64702ec6cafSHoang Le 
648670f4f88SRichard Alpe int tipc_nl_bc_link_set(struct net *net, struct nlattr *attrs[])
649670f4f88SRichard Alpe {
650670f4f88SRichard Alpe 	int err;
651670f4f88SRichard Alpe 	u32 win;
65202ec6cafSHoang Le 	u32 bc_mode;
65302ec6cafSHoang Le 	u32 bc_ratio;
654670f4f88SRichard Alpe 	struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
655670f4f88SRichard Alpe 
656670f4f88SRichard Alpe 	if (!attrs[TIPC_NLA_LINK_PROP])
657670f4f88SRichard Alpe 		return -EINVAL;
658670f4f88SRichard Alpe 
659670f4f88SRichard Alpe 	err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
660670f4f88SRichard Alpe 	if (err)
661670f4f88SRichard Alpe 		return err;
662670f4f88SRichard Alpe 
66302ec6cafSHoang Le 	if (!props[TIPC_NLA_PROP_WIN] &&
66402ec6cafSHoang Le 	    !props[TIPC_NLA_PROP_BROADCAST] &&
66502ec6cafSHoang Le 	    !props[TIPC_NLA_PROP_BROADCAST_RATIO]) {
666670f4f88SRichard Alpe 		return -EOPNOTSUPP;
66702ec6cafSHoang Le 	}
668670f4f88SRichard Alpe 
66902ec6cafSHoang Le 	if (props[TIPC_NLA_PROP_BROADCAST]) {
67002ec6cafSHoang Le 		bc_mode = nla_get_u32(props[TIPC_NLA_PROP_BROADCAST]);
67102ec6cafSHoang Le 		err = tipc_bc_link_set_broadcast_mode(net, bc_mode);
67202ec6cafSHoang Le 	}
67302ec6cafSHoang Le 
67402ec6cafSHoang Le 	if (!err && props[TIPC_NLA_PROP_BROADCAST_RATIO]) {
67502ec6cafSHoang Le 		bc_ratio = nla_get_u32(props[TIPC_NLA_PROP_BROADCAST_RATIO]);
67602ec6cafSHoang Le 		err = tipc_bc_link_set_broadcast_ratio(net, bc_ratio);
67702ec6cafSHoang Le 	}
67802ec6cafSHoang Le 
67902ec6cafSHoang Le 	if (!err && props[TIPC_NLA_PROP_WIN]) {
680670f4f88SRichard Alpe 		win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
68102ec6cafSHoang Le 		err = tipc_bc_link_set_queue_limits(net, win);
68202ec6cafSHoang Le 	}
683670f4f88SRichard Alpe 
68402ec6cafSHoang Le 	return err;
685670f4f88SRichard Alpe }
686670f4f88SRichard Alpe 
6876beb19a6SJon Paul Maloy int tipc_bcast_init(struct net *net)
688b97bf3fdSPer Liden {
68932301906SJon Paul Maloy 	struct tipc_net *tn = tipc_net(net);
69032301906SJon Paul Maloy 	struct tipc_bc_base *bb = NULL;
69132301906SJon Paul Maloy 	struct tipc_link *l = NULL;
6927f9f95d9SYing Xue 
693a0732548SJia-Ju Bai 	bb = kzalloc(sizeof(*bb), GFP_KERNEL);
69432301906SJon Paul Maloy 	if (!bb)
69532301906SJon Paul Maloy 		goto enomem;
69632301906SJon Paul Maloy 	tn->bcbase = bb;
6970043550bSJon Paul Maloy 	spin_lock_init(&tipc_net(net)->bclock);
6985fd9fd63SJon Paul Maloy 
69903b6fefdSTuong Lien 	if (!tipc_link_bc_create(net, 0, 0, NULL,
7004c94cc2dSJon Maloy 				 FB_MTU,
70132301906SJon Paul Maloy 				 BCLINK_WIN_DEFAULT,
70216ad3f40SJon Maloy 				 BCLINK_WIN_DEFAULT,
703fd556f20SJon Paul Maloy 				 0,
70432301906SJon Paul Maloy 				 &bb->inputq,
7052af5ae37SJon Paul Maloy 				 NULL,
70652666986SJon Paul Maloy 				 NULL,
70732301906SJon Paul Maloy 				 &l))
70832301906SJon Paul Maloy 		goto enomem;
70932301906SJon Paul Maloy 	bb->link = l;
71032301906SJon Paul Maloy 	tn->bcl = l;
71102ec6cafSHoang Le 	bb->rc_ratio = 10;
71201fd12bbSJon Paul Maloy 	bb->rcast_support = true;
713eb8b00f5SYing Xue 	return 0;
71432301906SJon Paul Maloy enomem:
71532301906SJon Paul Maloy 	kfree(bb);
71632301906SJon Paul Maloy 	kfree(l);
71732301906SJon Paul Maloy 	return -ENOMEM;
718b97bf3fdSPer Liden }
719b97bf3fdSPer Liden 
7206beb19a6SJon Paul Maloy void tipc_bcast_stop(struct net *net)
721b97bf3fdSPer Liden {
7227f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
7237f9f95d9SYing Xue 
724eb8b00f5SYing Xue 	synchronize_net();
7256beb19a6SJon Paul Maloy 	kfree(tn->bcbase);
72632301906SJon Paul Maloy 	kfree(tn->bcl);
727b97bf3fdSPer Liden }
7282ae0b8afSJon Paul Maloy 
7292ae0b8afSJon Paul Maloy void tipc_nlist_init(struct tipc_nlist *nl, u32 self)
7302ae0b8afSJon Paul Maloy {
7312ae0b8afSJon Paul Maloy 	memset(nl, 0, sizeof(*nl));
7322ae0b8afSJon Paul Maloy 	INIT_LIST_HEAD(&nl->list);
7332ae0b8afSJon Paul Maloy 	nl->self = self;
7342ae0b8afSJon Paul Maloy }
7352ae0b8afSJon Paul Maloy 
7362ae0b8afSJon Paul Maloy void tipc_nlist_add(struct tipc_nlist *nl, u32 node)
7372ae0b8afSJon Paul Maloy {
7382ae0b8afSJon Paul Maloy 	if (node == nl->self)
7392ae0b8afSJon Paul Maloy 		nl->local = true;
740a80ae530SJon Maloy 	else if (tipc_dest_push(&nl->list, node, 0))
7412ae0b8afSJon Paul Maloy 		nl->remote++;
7422ae0b8afSJon Paul Maloy }
7432ae0b8afSJon Paul Maloy 
7442ae0b8afSJon Paul Maloy void tipc_nlist_del(struct tipc_nlist *nl, u32 node)
7452ae0b8afSJon Paul Maloy {
7462ae0b8afSJon Paul Maloy 	if (node == nl->self)
7472ae0b8afSJon Paul Maloy 		nl->local = false;
748a80ae530SJon Maloy 	else if (tipc_dest_del(&nl->list, node, 0))
7492ae0b8afSJon Paul Maloy 		nl->remote--;
7502ae0b8afSJon Paul Maloy }
7512ae0b8afSJon Paul Maloy 
7522ae0b8afSJon Paul Maloy void tipc_nlist_purge(struct tipc_nlist *nl)
7532ae0b8afSJon Paul Maloy {
754a80ae530SJon Maloy 	tipc_dest_list_purge(&nl->list);
7552ae0b8afSJon Paul Maloy 	nl->remote = 0;
756c9efb15aSGustavo A. R. Silva 	nl->local = false;
7572ae0b8afSJon Paul Maloy }
75802ec6cafSHoang Le 
759cad2929dSHoang Huu Le u32 tipc_bcast_get_mode(struct net *net)
76002ec6cafSHoang Le {
76102ec6cafSHoang Le 	struct tipc_bc_base *bb = tipc_bc_base(net);
76202ec6cafSHoang Le 
76302ec6cafSHoang Le 	if (bb->force_bcast)
76402ec6cafSHoang Le 		return BCLINK_MODE_BCAST;
76502ec6cafSHoang Le 
76602ec6cafSHoang Le 	if (bb->force_rcast)
76702ec6cafSHoang Le 		return BCLINK_MODE_RCAST;
76802ec6cafSHoang Le 
76902ec6cafSHoang Le 	if (bb->bcast_support && bb->rcast_support)
77002ec6cafSHoang Le 		return BCLINK_MODE_SEL;
77102ec6cafSHoang Le 
77202ec6cafSHoang Le 	return 0;
77302ec6cafSHoang Le }
77402ec6cafSHoang Le 
77502ec6cafSHoang Le u32 tipc_bcast_get_broadcast_ratio(struct net *net)
77602ec6cafSHoang Le {
77702ec6cafSHoang Le 	struct tipc_bc_base *bb = tipc_bc_base(net);
77802ec6cafSHoang Le 
77902ec6cafSHoang Le 	return bb->rc_ratio;
78002ec6cafSHoang Le }
781c55c8edaSHoang Le 
78208e046c8SHoang Le void tipc_mcast_filter_msg(struct net *net, struct sk_buff_head *defq,
783c55c8edaSHoang Le 			   struct sk_buff_head *inputq)
784c55c8edaSHoang Le {
785c55c8edaSHoang Le 	struct sk_buff *skb, *_skb, *tmp;
786c55c8edaSHoang Le 	struct tipc_msg *hdr, *_hdr;
787c55c8edaSHoang Le 	bool match = false;
788c55c8edaSHoang Le 	u32 node, port;
789c55c8edaSHoang Le 
790c55c8edaSHoang Le 	skb = skb_peek(inputq);
791e1279ff7SHoang Le 	if (!skb)
792e1279ff7SHoang Le 		return;
793e1279ff7SHoang Le 
794c55c8edaSHoang Le 	hdr = buf_msg(skb);
795c55c8edaSHoang Le 
796c55c8edaSHoang Le 	if (likely(!msg_is_syn(hdr) && skb_queue_empty(defq)))
797c55c8edaSHoang Le 		return;
798c55c8edaSHoang Le 
799c55c8edaSHoang Le 	node = msg_orignode(hdr);
80008e046c8SHoang Le 	if (node == tipc_own_addr(net))
80108e046c8SHoang Le 		return;
80208e046c8SHoang Le 
803c55c8edaSHoang Le 	port = msg_origport(hdr);
804c55c8edaSHoang Le 
805c55c8edaSHoang Le 	/* Has the twin SYN message already arrived ? */
806c55c8edaSHoang Le 	skb_queue_walk(defq, _skb) {
807c55c8edaSHoang Le 		_hdr = buf_msg(_skb);
808c55c8edaSHoang Le 		if (msg_orignode(_hdr) != node)
809c55c8edaSHoang Le 			continue;
810c55c8edaSHoang Le 		if (msg_origport(_hdr) != port)
811c55c8edaSHoang Le 			continue;
812c55c8edaSHoang Le 		match = true;
813c55c8edaSHoang Le 		break;
814c55c8edaSHoang Le 	}
815c55c8edaSHoang Le 
816c55c8edaSHoang Le 	if (!match) {
817c55c8edaSHoang Le 		if (!msg_is_syn(hdr))
818c55c8edaSHoang Le 			return;
819c55c8edaSHoang Le 		__skb_dequeue(inputq);
820c55c8edaSHoang Le 		__skb_queue_tail(defq, skb);
821c55c8edaSHoang Le 		return;
822c55c8edaSHoang Le 	}
823c55c8edaSHoang Le 
824c55c8edaSHoang Le 	/* Deliver non-SYN message from other link, otherwise queue it */
825c55c8edaSHoang Le 	if (!msg_is_syn(hdr)) {
826c55c8edaSHoang Le 		if (msg_is_rcast(hdr) != msg_is_rcast(_hdr))
827c55c8edaSHoang Le 			return;
828c55c8edaSHoang Le 		__skb_dequeue(inputq);
829c55c8edaSHoang Le 		__skb_queue_tail(defq, skb);
830c55c8edaSHoang Le 		return;
831c55c8edaSHoang Le 	}
832c55c8edaSHoang Le 
833c55c8edaSHoang Le 	/* Queue non-SYN/SYN message from same link */
834c55c8edaSHoang Le 	if (msg_is_rcast(hdr) == msg_is_rcast(_hdr)) {
835c55c8edaSHoang Le 		__skb_dequeue(inputq);
836c55c8edaSHoang Le 		__skb_queue_tail(defq, skb);
837c55c8edaSHoang Le 		return;
838c55c8edaSHoang Le 	}
839c55c8edaSHoang Le 
840c55c8edaSHoang Le 	/* Matching SYN messages => return the one with data, if any */
841c55c8edaSHoang Le 	__skb_unlink(_skb, defq);
842c55c8edaSHoang Le 	if (msg_data_sz(hdr)) {
843c55c8edaSHoang Le 		kfree_skb(_skb);
844c55c8edaSHoang Le 	} else {
845c55c8edaSHoang Le 		__skb_dequeue(inputq);
846c55c8edaSHoang Le 		kfree_skb(skb);
847c55c8edaSHoang Le 		__skb_queue_tail(inputq, _skb);
848c55c8edaSHoang Le 	}
849c55c8edaSHoang Le 
850c55c8edaSHoang Le 	/* Deliver subsequent non-SYN messages from same peer */
851c55c8edaSHoang Le 	skb_queue_walk_safe(defq, _skb, tmp) {
852c55c8edaSHoang Le 		_hdr = buf_msg(_skb);
853c55c8edaSHoang Le 		if (msg_orignode(_hdr) != node)
854c55c8edaSHoang Le 			continue;
855c55c8edaSHoang Le 		if (msg_origport(_hdr) != port)
856c55c8edaSHoang Le 			continue;
857c55c8edaSHoang Le 		if (msg_is_syn(_hdr))
858c55c8edaSHoang Le 			break;
859c55c8edaSHoang Le 		__skb_unlink(_skb, defq);
860c55c8edaSHoang Le 		__skb_queue_tail(inputq, _skb);
861c55c8edaSHoang Le 	}
862c55c8edaSHoang Le }
863