xref: /openbmc/linux/net/tipc/bcast.c (revision 1f66d161)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/bcast.c: TIPC broadcast code
3b97bf3fdSPer Liden  *
43c724acdSJon Paul Maloy  * Copyright (c) 2004-2006, 2014-2015, 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 
38078bec82SJon Paul Maloy #include "socket.h"
39078bec82SJon Paul Maloy #include "msg.h"
40b97bf3fdSPer Liden #include "bcast.h"
419f6bdcd4SAllan Stephens #include "name_distr.h"
421da46568SYing Xue #include "core.h"
43b97bf3fdSPer Liden 
44b97bf3fdSPer Liden #define	MAX_PKT_DEFAULT_MCAST	1500	/* bcast link max packet size (fixed) */
45b97bf3fdSPer Liden #define	BCLINK_WIN_DEFAULT	20	/* bcast link window size (default) */
46c47e9b91SAllan Stephens 
473aec9cc9SAllan Stephens const char tipc_bclink_name[] = "broadcast-link";
48b97bf3fdSPer Liden 
4931e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
5031e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
5131e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_diff);
5228dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node);
5328dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node);
54b97bf3fdSPer Liden 
551da46568SYing Xue static void tipc_bclink_lock(struct net *net)
56d69afc90SYing Xue {
571da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
581da46568SYing Xue 
591da46568SYing Xue 	spin_lock_bh(&tn->bclink->lock);
60d69afc90SYing Xue }
61d69afc90SYing Xue 
621da46568SYing Xue static void tipc_bclink_unlock(struct net *net)
63d69afc90SYing Xue {
641da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
653f5a12bdSYing Xue 	struct tipc_node *node = NULL;
663f5a12bdSYing Xue 
671da46568SYing Xue 	if (likely(!tn->bclink->flags)) {
681da46568SYing Xue 		spin_unlock_bh(&tn->bclink->lock);
693f5a12bdSYing Xue 		return;
703f5a12bdSYing Xue 	}
713f5a12bdSYing Xue 
721da46568SYing Xue 	if (tn->bclink->flags & TIPC_BCLINK_RESET) {
731da46568SYing Xue 		tn->bclink->flags &= ~TIPC_BCLINK_RESET;
741da46568SYing Xue 		node = tipc_bclink_retransmit_to(net);
753f5a12bdSYing Xue 	}
761da46568SYing Xue 	spin_unlock_bh(&tn->bclink->lock);
773f5a12bdSYing Xue 
783f5a12bdSYing Xue 	if (node)
793f5a12bdSYing Xue 		tipc_link_reset_all(node);
803f5a12bdSYing Xue }
813f5a12bdSYing Xue 
82cb1b7280SJon Paul Maloy void tipc_bclink_input(struct net *net)
83cb1b7280SJon Paul Maloy {
84cb1b7280SJon Paul Maloy 	struct tipc_net *tn = net_generic(net, tipc_net_id);
85cb1b7280SJon Paul Maloy 
86cb1b7280SJon Paul Maloy 	tipc_sk_mcast_rcv(net, &tn->bclink->arrvq, &tn->bclink->inputq);
87cb1b7280SJon Paul Maloy }
88cb1b7280SJon Paul Maloy 
89078bec82SJon Paul Maloy uint  tipc_bclink_get_mtu(void)
90078bec82SJon Paul Maloy {
91078bec82SJon Paul Maloy 	return MAX_PKT_DEFAULT_MCAST;
92078bec82SJon Paul Maloy }
93078bec82SJon Paul Maloy 
941da46568SYing Xue void tipc_bclink_set_flags(struct net *net, unsigned int flags)
953f5a12bdSYing Xue {
961da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
971da46568SYing Xue 
981da46568SYing Xue 	tn->bclink->flags |= flags;
99d69afc90SYing Xue }
100d69afc90SYing Xue 
10105790c64SSam Ravnborg static u32 bcbuf_acks(struct sk_buff *buf)
102b97bf3fdSPer Liden {
103880b005fSDavid S. Miller 	return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
104b97bf3fdSPer Liden }
105b97bf3fdSPer Liden 
10605790c64SSam Ravnborg static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
107b97bf3fdSPer Liden {
108880b005fSDavid S. Miller 	TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
109b97bf3fdSPer Liden }
110b97bf3fdSPer Liden 
11105790c64SSam Ravnborg static void bcbuf_decr_acks(struct sk_buff *buf)
112b97bf3fdSPer Liden {
113b97bf3fdSPer Liden 	bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
114b97bf3fdSPer Liden }
115b97bf3fdSPer Liden 
1161da46568SYing Xue void tipc_bclink_add_node(struct net *net, u32 addr)
117cd3decdfSAllan Stephens {
1181da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1191da46568SYing Xue 
1201da46568SYing Xue 	tipc_bclink_lock(net);
1211da46568SYing Xue 	tipc_nmap_add(&tn->bclink->bcast_nodes, addr);
1221da46568SYing Xue 	tipc_bclink_unlock(net);
123cd3decdfSAllan Stephens }
124cd3decdfSAllan Stephens 
1251da46568SYing Xue void tipc_bclink_remove_node(struct net *net, u32 addr)
126cd3decdfSAllan Stephens {
1271da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1281da46568SYing Xue 
1291da46568SYing Xue 	tipc_bclink_lock(net);
1301da46568SYing Xue 	tipc_nmap_remove(&tn->bclink->bcast_nodes, addr);
1311da46568SYing Xue 	tipc_bclink_unlock(net);
132cd3decdfSAllan Stephens }
133b97bf3fdSPer Liden 
1341da46568SYing Xue static void bclink_set_last_sent(struct net *net)
1355b1f7bdeSAllan Stephens {
1361da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1371da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
13805dcc5aaSJon Paul Maloy 	struct sk_buff *skb = skb_peek(&bcl->backlogq);
1391da46568SYing Xue 
14005dcc5aaSJon Paul Maloy 	if (skb)
14105dcc5aaSJon Paul Maloy 		bcl->fsm_msg_cnt = mod(buf_seqno(skb) - 1);
1425b1f7bdeSAllan Stephens 	else
1435b1f7bdeSAllan Stephens 		bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
1445b1f7bdeSAllan Stephens }
1455b1f7bdeSAllan Stephens 
1461da46568SYing Xue u32 tipc_bclink_get_last_sent(struct net *net)
1475b1f7bdeSAllan Stephens {
1481da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1491da46568SYing Xue 
1501da46568SYing Xue 	return tn->bcl->fsm_msg_cnt;
1515b1f7bdeSAllan Stephens }
1525b1f7bdeSAllan Stephens 
1537a54d4a9SAllan Stephens static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
154b97bf3fdSPer Liden {
1557a54d4a9SAllan Stephens 	node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
1567a54d4a9SAllan Stephens 						seqno : node->bclink.last_sent;
157b97bf3fdSPer Liden }
158b97bf3fdSPer Liden 
159b97bf3fdSPer Liden 
1602c53040fSBen Hutchings /**
16101d83eddSAllan Stephens  * tipc_bclink_retransmit_to - get most recent node to request retransmission
16201d83eddSAllan Stephens  *
163d69afc90SYing Xue  * Called with bclink_lock locked
16401d83eddSAllan Stephens  */
1651da46568SYing Xue struct tipc_node *tipc_bclink_retransmit_to(struct net *net)
16601d83eddSAllan Stephens {
1671da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1681da46568SYing Xue 
1691da46568SYing Xue 	return tn->bclink->retransmit_to;
17001d83eddSAllan Stephens }
17101d83eddSAllan Stephens 
17201d83eddSAllan Stephens /**
173b97bf3fdSPer Liden  * bclink_retransmit_pkt - retransmit broadcast packets
174b97bf3fdSPer Liden  * @after: sequence number of last packet to *not* retransmit
175b97bf3fdSPer Liden  * @to: sequence number of last packet to retransmit
176b97bf3fdSPer Liden  *
177d69afc90SYing Xue  * Called with bclink_lock locked
178b97bf3fdSPer Liden  */
1791da46568SYing Xue static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
180b97bf3fdSPer Liden {
18158dc55f2SYing Xue 	struct sk_buff *skb;
1821da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
183b97bf3fdSPer Liden 
18405dcc5aaSJon Paul Maloy 	skb_queue_walk(&bcl->transmq, skb) {
185703068eeSJon Maloy 		if (more(buf_seqno(skb), after)) {
186703068eeSJon Maloy 			tipc_link_retransmit(bcl, skb, mod(to - after));
18758dc55f2SYing Xue 			break;
18858dc55f2SYing Xue 		}
189703068eeSJon Maloy 	}
190b97bf3fdSPer Liden }
191b97bf3fdSPer Liden 
192b97bf3fdSPer Liden /**
193908344cdSJon Maloy  * tipc_bclink_wakeup_users - wake up pending users
194908344cdSJon Maloy  *
195908344cdSJon Maloy  * Called with no locks taken
196908344cdSJon Maloy  */
197f2f9800dSYing Xue void tipc_bclink_wakeup_users(struct net *net)
198908344cdSJon Maloy {
1991da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
2004988bb4aSErik Hugne 
201c637c103SJon Paul Maloy 	tipc_sk_rcv(net, &tn->bclink->link.wakeupq);
202908344cdSJon Maloy }
203908344cdSJon Maloy 
204908344cdSJon Maloy /**
2054323add6SPer Liden  * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
206b97bf3fdSPer Liden  * @n_ptr: node that sent acknowledgement info
207b97bf3fdSPer Liden  * @acked: broadcast sequence # that has been acknowledged
208b97bf3fdSPer Liden  *
209d69afc90SYing Xue  * Node is locked, bclink_lock unlocked.
210b97bf3fdSPer Liden  */
2116c00055aSDavid S. Miller void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
212b97bf3fdSPer Liden {
21358dc55f2SYing Xue 	struct sk_buff *skb, *tmp;
214b97bf3fdSPer Liden 	unsigned int released = 0;
2151da46568SYing Xue 	struct net *net = n_ptr->net;
2161da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
217b97bf3fdSPer Liden 
2182cdf3918SJon Paul Maloy 	if (unlikely(!n_ptr->bclink.recv_permitted))
2192cdf3918SJon Paul Maloy 		return;
2202cdf3918SJon Paul Maloy 
2211da46568SYing Xue 	tipc_bclink_lock(net);
2222cdf3918SJon Paul Maloy 
22336559591SAllan Stephens 	/* Bail out if tx queue is empty (no clean up is required) */
22405dcc5aaSJon Paul Maloy 	skb = skb_peek(&tn->bcl->transmq);
22558dc55f2SYing Xue 	if (!skb)
22636559591SAllan Stephens 		goto exit;
22736559591SAllan Stephens 
22836559591SAllan Stephens 	/* Determine which messages need to be acknowledged */
22936559591SAllan Stephens 	if (acked == INVALID_LINK_SEQ) {
23036559591SAllan Stephens 		/*
23136559591SAllan Stephens 		 * Contact with specified node has been lost, so need to
23236559591SAllan Stephens 		 * acknowledge sent messages only (if other nodes still exist)
23336559591SAllan Stephens 		 * or both sent and unsent messages (otherwise)
23436559591SAllan Stephens 		 */
2351da46568SYing Xue 		if (tn->bclink->bcast_nodes.count)
2361da46568SYing Xue 			acked = tn->bcl->fsm_msg_cnt;
23736559591SAllan Stephens 		else
2381da46568SYing Xue 			acked = tn->bcl->next_out_no;
23936559591SAllan Stephens 	} else {
24036559591SAllan Stephens 		/*
24136559591SAllan Stephens 		 * Bail out if specified sequence number does not correspond
24236559591SAllan Stephens 		 * to a message that has been sent and not yet acknowledged
24336559591SAllan Stephens 		 */
24458dc55f2SYing Xue 		if (less(acked, buf_seqno(skb)) ||
2451da46568SYing Xue 		    less(tn->bcl->fsm_msg_cnt, acked) ||
24636559591SAllan Stephens 		    less_eq(acked, n_ptr->bclink.acked))
24736559591SAllan Stephens 			goto exit;
24836559591SAllan Stephens 	}
24936559591SAllan Stephens 
25036559591SAllan Stephens 	/* Skip over packets that node has previously acknowledged */
25105dcc5aaSJon Paul Maloy 	skb_queue_walk(&tn->bcl->transmq, skb) {
25258dc55f2SYing Xue 		if (more(buf_seqno(skb), n_ptr->bclink.acked))
25358dc55f2SYing Xue 			break;
25458dc55f2SYing Xue 	}
255b97bf3fdSPer Liden 
256b97bf3fdSPer Liden 	/* Update packets that node is now acknowledging */
25705dcc5aaSJon Paul Maloy 	skb_queue_walk_from_safe(&tn->bcl->transmq, skb, tmp) {
25858dc55f2SYing Xue 		if (more(buf_seqno(skb), acked))
25958dc55f2SYing Xue 			break;
26058dc55f2SYing Xue 		bcbuf_decr_acks(skb);
2611da46568SYing Xue 		bclink_set_last_sent(net);
26258dc55f2SYing Xue 		if (bcbuf_acks(skb) == 0) {
26305dcc5aaSJon Paul Maloy 			__skb_unlink(skb, &tn->bcl->transmq);
26458dc55f2SYing Xue 			kfree_skb(skb);
265b97bf3fdSPer Liden 			released = 1;
266b97bf3fdSPer Liden 		}
267b97bf3fdSPer Liden 	}
268b97bf3fdSPer Liden 	n_ptr->bclink.acked = acked;
269b97bf3fdSPer Liden 
270b97bf3fdSPer Liden 	/* Try resolving broadcast link congestion, if necessary */
27105dcc5aaSJon Paul Maloy 	if (unlikely(skb_peek(&tn->bcl->backlogq))) {
2721da46568SYing Xue 		tipc_link_push_packets(tn->bcl);
2731da46568SYing Xue 		bclink_set_last_sent(net);
2745b1f7bdeSAllan Stephens 	}
275c637c103SJon Paul Maloy 	if (unlikely(released && !skb_queue_empty(&tn->bcl->wakeupq)))
276908344cdSJon Maloy 		n_ptr->action_flags |= TIPC_WAKEUP_BCAST_USERS;
27736559591SAllan Stephens exit:
2781da46568SYing Xue 	tipc_bclink_unlock(net);
279b97bf3fdSPer Liden }
280b97bf3fdSPer Liden 
2812c53040fSBen Hutchings /**
2827a54d4a9SAllan Stephens  * tipc_bclink_update_link_state - update broadcast link state
283b97bf3fdSPer Liden  *
2847216cd94SYing Xue  * RCU and node lock set
285b97bf3fdSPer Liden  */
286c5898636SJon Paul Maloy void tipc_bclink_update_link_state(struct tipc_node *n_ptr,
287c93d3baaSYing Xue 				   u32 last_sent)
288b97bf3fdSPer Liden {
289b97bf3fdSPer Liden 	struct sk_buff *buf;
290c5898636SJon Paul Maloy 	struct net *net = n_ptr->net;
291c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
292b97bf3fdSPer Liden 
2937a54d4a9SAllan Stephens 	/* Ignore "stale" link state info */
2947a54d4a9SAllan Stephens 	if (less_eq(last_sent, n_ptr->bclink.last_in))
295b97bf3fdSPer Liden 		return;
296b97bf3fdSPer Liden 
2977a54d4a9SAllan Stephens 	/* Update link synchronization state; quit if in sync */
2987a54d4a9SAllan Stephens 	bclink_update_last_sent(n_ptr, last_sent);
2997a54d4a9SAllan Stephens 
3007a54d4a9SAllan Stephens 	if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
3017a54d4a9SAllan Stephens 		return;
3027a54d4a9SAllan Stephens 
3037a54d4a9SAllan Stephens 	/* Update out-of-sync state; quit if loss is still unconfirmed */
3047a54d4a9SAllan Stephens 	if ((++n_ptr->bclink.oos_state) == 1) {
3057a54d4a9SAllan Stephens 		if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
3067a54d4a9SAllan Stephens 			return;
3077a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
3087a54d4a9SAllan Stephens 	}
3097a54d4a9SAllan Stephens 
3107a54d4a9SAllan Stephens 	/* Don't NACK if one has been recently sent (or seen) */
3117a54d4a9SAllan Stephens 	if (n_ptr->bclink.oos_state & 0x1)
3127a54d4a9SAllan Stephens 		return;
3137a54d4a9SAllan Stephens 
3147a54d4a9SAllan Stephens 	/* Send NACK */
31531e3c3f6Sstephen hemminger 	buf = tipc_buf_acquire(INT_H_SIZE);
316b97bf3fdSPer Liden 	if (buf) {
3177a54d4a9SAllan Stephens 		struct tipc_msg *msg = buf_msg(buf);
31805dcc5aaSJon Paul Maloy 		struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferdq);
319bc6fecd4SYing Xue 		u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
3207a54d4a9SAllan Stephens 
321c5898636SJon Paul Maloy 		tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG,
32275715217SAllan Stephens 			      INT_H_SIZE, n_ptr->addr);
323bf781ecfSAllan Stephens 		msg_set_non_seq(msg, 1);
324c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
3257a54d4a9SAllan Stephens 		msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
3267a54d4a9SAllan Stephens 		msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
327bc6fecd4SYing Xue 		msg_set_bcgap_to(msg, to);
328b97bf3fdSPer Liden 
3291da46568SYing Xue 		tipc_bclink_lock(net);
3307f9f95d9SYing Xue 		tipc_bearer_send(net, MAX_BEARERS, buf, NULL);
3311da46568SYing Xue 		tn->bcl->stats.sent_nacks++;
3321da46568SYing Xue 		tipc_bclink_unlock(net);
3335f6d9123SAllan Stephens 		kfree_skb(buf);
334b97bf3fdSPer Liden 
3357a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state++;
3367a54d4a9SAllan Stephens 	}
3377a54d4a9SAllan Stephens }
3387a54d4a9SAllan Stephens 
3392c53040fSBen Hutchings /**
3407a54d4a9SAllan Stephens  * bclink_peek_nack - monitor retransmission requests sent by other nodes
341b97bf3fdSPer Liden  *
3427a54d4a9SAllan Stephens  * Delay any upcoming NACK by this node if another node has already
3437a54d4a9SAllan Stephens  * requested the first message this node is going to ask for.
344b97bf3fdSPer Liden  */
345f2f9800dSYing Xue static void bclink_peek_nack(struct net *net, struct tipc_msg *msg)
346b97bf3fdSPer Liden {
347f2f9800dSYing Xue 	struct tipc_node *n_ptr = tipc_node_find(net, msg_destnode(msg));
348b97bf3fdSPer Liden 
3497a54d4a9SAllan Stephens 	if (unlikely(!n_ptr))
350b97bf3fdSPer Liden 		return;
3517a54d4a9SAllan Stephens 
3524323add6SPer Liden 	tipc_node_lock(n_ptr);
353b97bf3fdSPer Liden 
354389dd9bcSYing Xue 	if (n_ptr->bclink.recv_permitted &&
3557a54d4a9SAllan Stephens 	    (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
3567a54d4a9SAllan Stephens 	    (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
3577a54d4a9SAllan Stephens 		n_ptr->bclink.oos_state = 2;
358b97bf3fdSPer Liden 
3594323add6SPer Liden 	tipc_node_unlock(n_ptr);
360b97bf3fdSPer Liden }
361b97bf3fdSPer Liden 
362cb1b7280SJon Paul Maloy /* tipc_bclink_xmit - deliver buffer chain to all nodes in cluster
363078bec82SJon Paul Maloy  *                    and to identified node local sockets
364f2f9800dSYing Xue  * @net: the applicable net namespace
365a6ca1094SYing Xue  * @list: chain of buffers containing message
366078bec82SJon Paul Maloy  * Consumes the buffer chain, except when returning -ELINKCONG
367078bec82SJon Paul Maloy  * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
368078bec82SJon Paul Maloy  */
369f2f9800dSYing Xue int tipc_bclink_xmit(struct net *net, struct sk_buff_head *list)
370078bec82SJon Paul Maloy {
3711da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
3721da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
3731da46568SYing Xue 	struct tipc_bclink *bclink = tn->bclink;
374078bec82SJon Paul Maloy 	int rc = 0;
375078bec82SJon Paul Maloy 	int bc = 0;
376a6ca1094SYing Xue 	struct sk_buff *skb;
377cb1b7280SJon Paul Maloy 	struct sk_buff_head arrvq;
378cb1b7280SJon Paul Maloy 	struct sk_buff_head inputq;
379078bec82SJon Paul Maloy 
380078bec82SJon Paul Maloy 	/* Prepare clone of message for local node */
381a6ca1094SYing Xue 	skb = tipc_msg_reassemble(list);
382a6ca1094SYing Xue 	if (unlikely(!skb)) {
383a6ca1094SYing Xue 		__skb_queue_purge(list);
384078bec82SJon Paul Maloy 		return -EHOSTUNREACH;
385078bec82SJon Paul Maloy 	}
386cb1b7280SJon Paul Maloy 	/* Broadcast to all nodes */
387078bec82SJon Paul Maloy 	if (likely(bclink)) {
3881da46568SYing Xue 		tipc_bclink_lock(net);
389078bec82SJon Paul Maloy 		if (likely(bclink->bcast_nodes.count)) {
3907f9f95d9SYing Xue 			rc = __tipc_link_xmit(net, bcl, list);
391078bec82SJon Paul Maloy 			if (likely(!rc)) {
39205dcc5aaSJon Paul Maloy 				u32 len = skb_queue_len(&bcl->transmq);
39358dc55f2SYing Xue 
3941da46568SYing Xue 				bclink_set_last_sent(net);
395078bec82SJon Paul Maloy 				bcl->stats.queue_sz_counts++;
39658dc55f2SYing Xue 				bcl->stats.accu_queue_sz += len;
397078bec82SJon Paul Maloy 			}
398078bec82SJon Paul Maloy 			bc = 1;
399078bec82SJon Paul Maloy 		}
4001da46568SYing Xue 		tipc_bclink_unlock(net);
401078bec82SJon Paul Maloy 	}
402078bec82SJon Paul Maloy 
403078bec82SJon Paul Maloy 	if (unlikely(!bc))
404a6ca1094SYing Xue 		__skb_queue_purge(list);
405078bec82SJon Paul Maloy 
406cb1b7280SJon Paul Maloy 	if (unlikely(rc)) {
407a6ca1094SYing Xue 		kfree_skb(skb);
408cb1b7280SJon Paul Maloy 		return rc;
409cb1b7280SJon Paul Maloy 	}
410cb1b7280SJon Paul Maloy 	/* Deliver message clone */
411cb1b7280SJon Paul Maloy 	__skb_queue_head_init(&arrvq);
412cb1b7280SJon Paul Maloy 	skb_queue_head_init(&inputq);
413cb1b7280SJon Paul Maloy 	__skb_queue_tail(&arrvq, skb);
414cb1b7280SJon Paul Maloy 	tipc_sk_mcast_rcv(net, &arrvq, &inputq);
415078bec82SJon Paul Maloy 	return rc;
416078bec82SJon Paul Maloy }
417078bec82SJon Paul Maloy 
4182c53040fSBen Hutchings /**
41963e7f1acSAllan Stephens  * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
42063e7f1acSAllan Stephens  *
421d69afc90SYing Xue  * Called with both sending node's lock and bclink_lock taken.
42263e7f1acSAllan Stephens  */
42363e7f1acSAllan Stephens static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
42463e7f1acSAllan Stephens {
4251da46568SYing Xue 	struct tipc_net *tn = net_generic(node->net, tipc_net_id);
4261da46568SYing Xue 
42763e7f1acSAllan Stephens 	bclink_update_last_sent(node, seqno);
42863e7f1acSAllan Stephens 	node->bclink.last_in = seqno;
42963e7f1acSAllan Stephens 	node->bclink.oos_state = 0;
4301da46568SYing Xue 	tn->bcl->stats.recv_info++;
43163e7f1acSAllan Stephens 
43263e7f1acSAllan Stephens 	/*
43363e7f1acSAllan Stephens 	 * Unicast an ACK periodically, ensuring that
43463e7f1acSAllan Stephens 	 * all nodes in the cluster don't ACK at the same time
43563e7f1acSAllan Stephens 	 */
43634747539SYing Xue 	if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
437247f0f3cSYing Xue 		tipc_link_proto_xmit(node->active_links[node->addr & 1],
43863e7f1acSAllan Stephens 				     STATE_MSG, 0, 0, 0, 0, 0);
4391da46568SYing Xue 		tn->bcl->stats.sent_acks++;
44063e7f1acSAllan Stephens 	}
44163e7f1acSAllan Stephens }
44263e7f1acSAllan Stephens 
4432c53040fSBen Hutchings /**
444247f0f3cSYing Xue  * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
445b97bf3fdSPer Liden  *
4467216cd94SYing Xue  * RCU is locked, no other locks set
447b97bf3fdSPer Liden  */
448c93d3baaSYing Xue void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
449b97bf3fdSPer Liden {
450c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
4511da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
452b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
4535d3c488dSAllan Stephens 	struct tipc_node *node;
454b97bf3fdSPer Liden 	u32 next_in;
455b97bf3fdSPer Liden 	u32 seqno;
4560abd8ff2SJon Paul Maloy 	int deferred = 0;
457c637c103SJon Paul Maloy 	int pos = 0;
458c637c103SJon Paul Maloy 	struct sk_buff *iskb;
459cb1b7280SJon Paul Maloy 	struct sk_buff_head *arrvq, *inputq;
460b97bf3fdSPer Liden 
4615d3c488dSAllan Stephens 	/* Screen out unwanted broadcast messages */
462c93d3baaSYing Xue 	if (msg_mc_netid(msg) != tn->net_id)
4635d3c488dSAllan Stephens 		goto exit;
4645d3c488dSAllan Stephens 
465f2f9800dSYing Xue 	node = tipc_node_find(net, msg_prevnode(msg));
4665d3c488dSAllan Stephens 	if (unlikely(!node))
4675d3c488dSAllan Stephens 		goto exit;
4685d3c488dSAllan Stephens 
4695d3c488dSAllan Stephens 	tipc_node_lock(node);
470389dd9bcSYing Xue 	if (unlikely(!node->bclink.recv_permitted))
4715d3c488dSAllan Stephens 		goto unlock;
472b97bf3fdSPer Liden 
4738a275a6aSAllan Stephens 	/* Handle broadcast protocol message */
474b97bf3fdSPer Liden 	if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
4759f6bdcd4SAllan Stephens 		if (msg_type(msg) != STATE_MSG)
4769f6bdcd4SAllan Stephens 			goto unlock;
47734747539SYing Xue 		if (msg_destnode(msg) == tn->own_addr) {
4784323add6SPer Liden 			tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
4794323add6SPer Liden 			tipc_node_unlock(node);
4801da46568SYing Xue 			tipc_bclink_lock(net);
481b97bf3fdSPer Liden 			bcl->stats.recv_nacks++;
4821da46568SYing Xue 			tn->bclink->retransmit_to = node;
4831da46568SYing Xue 			bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
484b97bf3fdSPer Liden 					      msg_bcgap_to(msg));
4851da46568SYing Xue 			tipc_bclink_unlock(net);
486b97bf3fdSPer Liden 		} else {
4875d3c488dSAllan Stephens 			tipc_node_unlock(node);
488f2f9800dSYing Xue 			bclink_peek_nack(net, msg);
489b97bf3fdSPer Liden 		}
4905d3c488dSAllan Stephens 		goto exit;
491b97bf3fdSPer Liden 	}
492b97bf3fdSPer Liden 
4935d3c488dSAllan Stephens 	/* Handle in-sequence broadcast message */
494b97bf3fdSPer Liden 	seqno = msg_seqno(msg);
4958a275a6aSAllan Stephens 	next_in = mod(node->bclink.last_in + 1);
496cb1b7280SJon Paul Maloy 	arrvq = &tn->bclink->arrvq;
497cb1b7280SJon Paul Maloy 	inputq = &tn->bclink->inputq;
498b97bf3fdSPer Liden 
499b97bf3fdSPer Liden 	if (likely(seqno == next_in)) {
5008a275a6aSAllan Stephens receive:
5017a54d4a9SAllan Stephens 		/* Deliver message to destination */
502b97bf3fdSPer Liden 		if (likely(msg_isdata(msg))) {
5031da46568SYing Xue 			tipc_bclink_lock(net);
50463e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
505cb1b7280SJon Paul Maloy 			spin_lock_bh(&inputq->lock);
506cb1b7280SJon Paul Maloy 			__skb_queue_tail(arrvq, buf);
507cb1b7280SJon Paul Maloy 			spin_unlock_bh(&inputq->lock);
508cb1b7280SJon Paul Maloy 			node->action_flags |= TIPC_BCAST_MSG_EVT;
5091da46568SYing Xue 			tipc_bclink_unlock(net);
5104323add6SPer Liden 			tipc_node_unlock(node);
511b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_BUNDLER) {
5121da46568SYing Xue 			tipc_bclink_lock(net);
51363e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
514b97bf3fdSPer Liden 			bcl->stats.recv_bundles++;
515b97bf3fdSPer Liden 			bcl->stats.recv_bundled += msg_msgcnt(msg);
516cb1b7280SJon Paul Maloy 			pos = 0;
517cb1b7280SJon Paul Maloy 			while (tipc_msg_extract(buf, &iskb, &pos)) {
518cb1b7280SJon Paul Maloy 				spin_lock_bh(&inputq->lock);
519cb1b7280SJon Paul Maloy 				__skb_queue_tail(arrvq, iskb);
520cb1b7280SJon Paul Maloy 				spin_unlock_bh(&inputq->lock);
521cb1b7280SJon Paul Maloy 			}
522cb1b7280SJon Paul Maloy 			node->action_flags |= TIPC_BCAST_MSG_EVT;
5231da46568SYing Xue 			tipc_bclink_unlock(net);
5244323add6SPer Liden 			tipc_node_unlock(node);
525b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_FRAGMENTER) {
5261da46568SYing Xue 			tipc_bclink_lock(net);
52763e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
528bc14b8d6SYing Xue 			tipc_buf_append(&node->bclink.reasm_buf, &buf);
529bc14b8d6SYing Xue 			if (unlikely(!buf && !node->bclink.reasm_buf)) {
530bc14b8d6SYing Xue 				tipc_bclink_unlock(net);
531bc14b8d6SYing Xue 				goto unlock;
532bc14b8d6SYing Xue 			}
533b97bf3fdSPer Liden 			bcl->stats.recv_fragments++;
53437e22164SJon Paul Maloy 			if (buf) {
535b97bf3fdSPer Liden 				bcl->stats.recv_fragmented++;
53640ba3cdfSErik Hugne 				msg = buf_msg(buf);
5371da46568SYing Xue 				tipc_bclink_unlock(net);
538528f6f4bSErik Hugne 				goto receive;
539528f6f4bSErik Hugne 			}
5401da46568SYing Xue 			tipc_bclink_unlock(net);
5414323add6SPer Liden 			tipc_node_unlock(node);
542b97bf3fdSPer Liden 		} else {
5431da46568SYing Xue 			tipc_bclink_lock(net);
54463e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
5451da46568SYing Xue 			tipc_bclink_unlock(net);
5464323add6SPer Liden 			tipc_node_unlock(node);
5475f6d9123SAllan Stephens 			kfree_skb(buf);
548b97bf3fdSPer Liden 		}
5495d3c488dSAllan Stephens 		buf = NULL;
5508a275a6aSAllan Stephens 
5518a275a6aSAllan Stephens 		/* Determine new synchronization state */
5524323add6SPer Liden 		tipc_node_lock(node);
5538a275a6aSAllan Stephens 		if (unlikely(!tipc_node_is_up(node)))
5548a275a6aSAllan Stephens 			goto unlock;
5558a275a6aSAllan Stephens 
5567a54d4a9SAllan Stephens 		if (node->bclink.last_in == node->bclink.last_sent)
5578a275a6aSAllan Stephens 			goto unlock;
5588a275a6aSAllan Stephens 
55905dcc5aaSJon Paul Maloy 		if (skb_queue_empty(&node->bclink.deferdq)) {
5607a54d4a9SAllan Stephens 			node->bclink.oos_state = 1;
5617a54d4a9SAllan Stephens 			goto unlock;
5627a54d4a9SAllan Stephens 		}
5637a54d4a9SAllan Stephens 
56405dcc5aaSJon Paul Maloy 		msg = buf_msg(skb_peek(&node->bclink.deferdq));
5658a275a6aSAllan Stephens 		seqno = msg_seqno(msg);
5668a275a6aSAllan Stephens 		next_in = mod(next_in + 1);
5678a275a6aSAllan Stephens 		if (seqno != next_in)
5688a275a6aSAllan Stephens 			goto unlock;
5698a275a6aSAllan Stephens 
5708a275a6aSAllan Stephens 		/* Take in-sequence message from deferred queue & deliver it */
57105dcc5aaSJon Paul Maloy 		buf = __skb_dequeue(&node->bclink.deferdq);
572b97bf3fdSPer Liden 		goto receive;
573b97bf3fdSPer Liden 	}
5748a275a6aSAllan Stephens 
5758a275a6aSAllan Stephens 	/* Handle out-of-sequence broadcast message */
5768a275a6aSAllan Stephens 	if (less(next_in, seqno)) {
57705dcc5aaSJon Paul Maloy 		deferred = tipc_link_defer_pkt(&node->bclink.deferdq,
5788a275a6aSAllan Stephens 					       buf);
5797a54d4a9SAllan Stephens 		bclink_update_last_sent(node, seqno);
5805d3c488dSAllan Stephens 		buf = NULL;
5810abd8ff2SJon Paul Maloy 	}
5828a275a6aSAllan Stephens 
5831da46568SYing Xue 	tipc_bclink_lock(net);
584b98158e3SAllan Stephens 
5858a275a6aSAllan Stephens 	if (deferred)
5868a275a6aSAllan Stephens 		bcl->stats.deferred_recv++;
5870232c5a5SAllan Stephens 	else
5880232c5a5SAllan Stephens 		bcl->stats.duplicates++;
5898a275a6aSAllan Stephens 
5901da46568SYing Xue 	tipc_bclink_unlock(net);
591b98158e3SAllan Stephens 
5925d3c488dSAllan Stephens unlock:
5934323add6SPer Liden 	tipc_node_unlock(node);
5945d3c488dSAllan Stephens exit:
5955f6d9123SAllan Stephens 	kfree_skb(buf);
596b97bf3fdSPer Liden }
597b97bf3fdSPer Liden 
5986c00055aSDavid S. Miller u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
599b97bf3fdSPer Liden {
600389dd9bcSYing Xue 	return (n_ptr->bclink.recv_permitted &&
6011da46568SYing Xue 		(tipc_bclink_get_last_sent(n_ptr->net) != n_ptr->bclink.acked));
602b97bf3fdSPer Liden }
603b97bf3fdSPer Liden 
604b97bf3fdSPer Liden 
605b97bf3fdSPer Liden /**
6064323add6SPer Liden  * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
607b97bf3fdSPer Liden  *
6082ff9f924SAllan Stephens  * Send packet over as many bearers as necessary to reach all nodes
6092ff9f924SAllan Stephens  * that have joined the broadcast link.
610b97bf3fdSPer Liden  *
6112ff9f924SAllan Stephens  * Returns 0 (packet sent successfully) under all circumstances,
6122ff9f924SAllan Stephens  * since the broadcast link's pseudo-bearer never blocks
613b97bf3fdSPer Liden  */
6141da46568SYing Xue static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
6151da46568SYing Xue 			      struct tipc_bearer *unused1,
616b97bf3fdSPer Liden 			      struct tipc_media_addr *unused2)
617b97bf3fdSPer Liden {
618b97bf3fdSPer Liden 	int bp_index;
6196f92ee54SJon Paul Maloy 	struct tipc_msg *msg = buf_msg(buf);
620c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6211da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6221da46568SYing Xue 	struct tipc_bclink *bclink = tn->bclink;
623b97bf3fdSPer Liden 
624e6160710SGerlando Falauto 	/* Prepare broadcast link message for reliable transmission,
6252ff9f924SAllan Stephens 	 * if first time trying to send it;
6262ff9f924SAllan Stephens 	 * preparation is skipped for broadcast link protocol messages
6272ff9f924SAllan Stephens 	 * since they are sent in an unreliable manner and don't need it
6282ff9f924SAllan Stephens 	 */
629b97bf3fdSPer Liden 	if (likely(!msg_non_seq(buf_msg(buf)))) {
630cd3decdfSAllan Stephens 		bcbuf_set_acks(buf, bclink->bcast_nodes.count);
63140aecb1bSAllan Stephens 		msg_set_non_seq(msg, 1);
632c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
6331da46568SYing Xue 		tn->bcl->stats.sent_info++;
634cd3decdfSAllan Stephens 		if (WARN_ON(!bclink->bcast_nodes.count)) {
6355e726900SAllan Stephens 			dump_stack();
6365e726900SAllan Stephens 			return 0;
6375e726900SAllan Stephens 		}
638b97bf3fdSPer Liden 	}
639b97bf3fdSPer Liden 
640b97bf3fdSPer Liden 	/* Send buffer over bearers until all targets reached */
641cd3decdfSAllan Stephens 	bcbearer->remains = bclink->bcast_nodes;
642b97bf3fdSPer Liden 
643b97bf3fdSPer Liden 	for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
6442d627b92SAllan Stephens 		struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
6452d627b92SAllan Stephens 		struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
6466f92ee54SJon Paul Maloy 		struct tipc_bearer *bp[2] = {p, s};
6476f92ee54SJon Paul Maloy 		struct tipc_bearer *b = bp[msg_link_selector(msg)];
648488fc9afSGerlando Falauto 		struct sk_buff *tbuf;
649b97bf3fdSPer Liden 
650b97bf3fdSPer Liden 		if (!p)
651e6160710SGerlando Falauto 			break; /* No more bearers to try */
6526f92ee54SJon Paul Maloy 		if (!b)
6536f92ee54SJon Paul Maloy 			b = p;
65477861d9cSGerlando Falauto 		tipc_nmap_diff(&bcbearer->remains, &b->nodes,
655e6160710SGerlando Falauto 			       &bcbearer->remains_new);
65665f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == bcbearer->remains.count)
657e6160710SGerlando Falauto 			continue; /* Nothing added by bearer pair */
658b97bf3fdSPer Liden 
659488fc9afSGerlando Falauto 		if (bp_index == 0) {
660488fc9afSGerlando Falauto 			/* Use original buffer for first bearer */
6617f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, buf, &b->bcast_addr);
662488fc9afSGerlando Falauto 		} else {
663488fc9afSGerlando Falauto 			/* Avoid concurrent buffer access */
664bad93e9dSOctavian Purdila 			tbuf = pskb_copy_for_clone(buf, GFP_ATOMIC);
665488fc9afSGerlando Falauto 			if (!tbuf)
666488fc9afSGerlando Falauto 				break;
6677f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, tbuf,
6687f9f95d9SYing Xue 					 &b->bcast_addr);
669488fc9afSGerlando Falauto 			kfree_skb(tbuf); /* Bearer keeps a clone */
670488fc9afSGerlando Falauto 		}
67165f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == 0)
672e6160710SGerlando Falauto 			break; /* All targets reached */
673b97bf3fdSPer Liden 
67465f51ef0SAllan Stephens 		bcbearer->remains = bcbearer->remains_new;
675b97bf3fdSPer Liden 	}
676b97bf3fdSPer Liden 
6772ff9f924SAllan Stephens 	return 0;
678b97bf3fdSPer Liden }
679b97bf3fdSPer Liden 
680b97bf3fdSPer Liden /**
6814323add6SPer Liden  * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
682b97bf3fdSPer Liden  */
6837f9f95d9SYing Xue void tipc_bcbearer_sort(struct net *net, struct tipc_node_map *nm_ptr,
6847f9f95d9SYing Xue 			u32 node, bool action)
685b97bf3fdSPer Liden {
6867f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6871da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6887f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
6897f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_curr;
690f8322dfcSYing Xue 	struct tipc_bearer *b;
691b97bf3fdSPer Liden 	int b_index;
692b97bf3fdSPer Liden 	int pri;
693b97bf3fdSPer Liden 
6941da46568SYing Xue 	tipc_bclink_lock(net);
695b97bf3fdSPer Liden 
69628dd9418SYing Xue 	if (action)
69728dd9418SYing Xue 		tipc_nmap_add(nm_ptr, node);
69828dd9418SYing Xue 	else
69928dd9418SYing Xue 		tipc_nmap_remove(nm_ptr, node);
70028dd9418SYing Xue 
701b97bf3fdSPer Liden 	/* Group bearers by priority (can assume max of two per priority) */
702b97bf3fdSPer Liden 	memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
703b97bf3fdSPer Liden 
704f8322dfcSYing Xue 	rcu_read_lock();
705b97bf3fdSPer Liden 	for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
7067f9f95d9SYing Xue 		b = rcu_dereference_rtnl(tn->bearer_list[b_index]);
707f47de12bSYing Xue 		if (!b || !b->nodes.count)
708b97bf3fdSPer Liden 			continue;
709b97bf3fdSPer Liden 
710b97bf3fdSPer Liden 		if (!bp_temp[b->priority].primary)
711b97bf3fdSPer Liden 			bp_temp[b->priority].primary = b;
712b97bf3fdSPer Liden 		else
713b97bf3fdSPer Liden 			bp_temp[b->priority].secondary = b;
714b97bf3fdSPer Liden 	}
715f8322dfcSYing Xue 	rcu_read_unlock();
716b97bf3fdSPer Liden 
717b97bf3fdSPer Liden 	/* Create array of bearer pairs for broadcasting */
718b97bf3fdSPer Liden 	bp_curr = bcbearer->bpairs;
719b97bf3fdSPer Liden 	memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
720b97bf3fdSPer Liden 
72116cb4b33SPer Liden 	for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
722b97bf3fdSPer Liden 
723b97bf3fdSPer Liden 		if (!bp_temp[pri].primary)
724b97bf3fdSPer Liden 			continue;
725b97bf3fdSPer Liden 
726b97bf3fdSPer Liden 		bp_curr->primary = bp_temp[pri].primary;
727b97bf3fdSPer Liden 
728b97bf3fdSPer Liden 		if (bp_temp[pri].secondary) {
7294323add6SPer Liden 			if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
730b97bf3fdSPer Liden 					    &bp_temp[pri].secondary->nodes)) {
731b97bf3fdSPer Liden 				bp_curr->secondary = bp_temp[pri].secondary;
732b97bf3fdSPer Liden 			} else {
733b97bf3fdSPer Liden 				bp_curr++;
734b97bf3fdSPer Liden 				bp_curr->primary = bp_temp[pri].secondary;
735b97bf3fdSPer Liden 			}
736b97bf3fdSPer Liden 		}
737b97bf3fdSPer Liden 
738b97bf3fdSPer Liden 		bp_curr++;
739b97bf3fdSPer Liden 	}
740b97bf3fdSPer Liden 
7411da46568SYing Xue 	tipc_bclink_unlock(net);
742b97bf3fdSPer Liden }
743b97bf3fdSPer Liden 
744d8182804SRichard Alpe static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
745d8182804SRichard Alpe 				      struct tipc_stats *stats)
7467be57fc6SRichard Alpe {
7477be57fc6SRichard Alpe 	int i;
7487be57fc6SRichard Alpe 	struct nlattr *nest;
7497be57fc6SRichard Alpe 
7507be57fc6SRichard Alpe 	struct nla_map {
7517be57fc6SRichard Alpe 		__u32 key;
7527be57fc6SRichard Alpe 		__u32 val;
7537be57fc6SRichard Alpe 	};
7547be57fc6SRichard Alpe 
7557be57fc6SRichard Alpe 	struct nla_map map[] = {
7567be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_INFO, stats->recv_info},
7577be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
7587be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
7597be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
7607be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
7617be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_INFO, stats->sent_info},
7627be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
7637be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
7647be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
7657be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
7667be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
7677be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
7687be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
7697be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
7707be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
7717be57fc6SRichard Alpe 		{TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
7727be57fc6SRichard Alpe 		{TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
7737be57fc6SRichard Alpe 		{TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
7747be57fc6SRichard Alpe 		{TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
7757be57fc6SRichard Alpe 			(stats->accu_queue_sz / stats->queue_sz_counts) : 0}
7767be57fc6SRichard Alpe 	};
7777be57fc6SRichard Alpe 
7787be57fc6SRichard Alpe 	nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
7797be57fc6SRichard Alpe 	if (!nest)
7807be57fc6SRichard Alpe 		return -EMSGSIZE;
7817be57fc6SRichard Alpe 
7827be57fc6SRichard Alpe 	for (i = 0; i <  ARRAY_SIZE(map); i++)
7837be57fc6SRichard Alpe 		if (nla_put_u32(skb, map[i].key, map[i].val))
7847be57fc6SRichard Alpe 			goto msg_full;
7857be57fc6SRichard Alpe 
7867be57fc6SRichard Alpe 	nla_nest_end(skb, nest);
7877be57fc6SRichard Alpe 
7887be57fc6SRichard Alpe 	return 0;
7897be57fc6SRichard Alpe msg_full:
7907be57fc6SRichard Alpe 	nla_nest_cancel(skb, nest);
7917be57fc6SRichard Alpe 
7927be57fc6SRichard Alpe 	return -EMSGSIZE;
7937be57fc6SRichard Alpe }
7947be57fc6SRichard Alpe 
7951da46568SYing Xue int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
7967be57fc6SRichard Alpe {
7977be57fc6SRichard Alpe 	int err;
7987be57fc6SRichard Alpe 	void *hdr;
7997be57fc6SRichard Alpe 	struct nlattr *attrs;
8007be57fc6SRichard Alpe 	struct nlattr *prop;
8011da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8021da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8037be57fc6SRichard Alpe 
8047be57fc6SRichard Alpe 	if (!bcl)
8057be57fc6SRichard Alpe 		return 0;
8067be57fc6SRichard Alpe 
8071da46568SYing Xue 	tipc_bclink_lock(net);
8087be57fc6SRichard Alpe 
809bfb3e5ddSRichard Alpe 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
8107be57fc6SRichard Alpe 			  NLM_F_MULTI, TIPC_NL_LINK_GET);
8117be57fc6SRichard Alpe 	if (!hdr)
8127be57fc6SRichard Alpe 		return -EMSGSIZE;
8137be57fc6SRichard Alpe 
8147be57fc6SRichard Alpe 	attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
8157be57fc6SRichard Alpe 	if (!attrs)
8167be57fc6SRichard Alpe 		goto msg_full;
8177be57fc6SRichard Alpe 
8187be57fc6SRichard Alpe 	/* The broadcast link is always up */
8197be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
8207be57fc6SRichard Alpe 		goto attr_msg_full;
8217be57fc6SRichard Alpe 
8227be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
8237be57fc6SRichard Alpe 		goto attr_msg_full;
8247be57fc6SRichard Alpe 	if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
8257be57fc6SRichard Alpe 		goto attr_msg_full;
8267be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->next_in_no))
8277be57fc6SRichard Alpe 		goto attr_msg_full;
8287be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->next_out_no))
8297be57fc6SRichard Alpe 		goto attr_msg_full;
8307be57fc6SRichard Alpe 
8317be57fc6SRichard Alpe 	prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
8327be57fc6SRichard Alpe 	if (!prop)
8337be57fc6SRichard Alpe 		goto attr_msg_full;
834*1f66d161SJon Paul Maloy 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
8357be57fc6SRichard Alpe 		goto prop_msg_full;
8367be57fc6SRichard Alpe 	nla_nest_end(msg->skb, prop);
8377be57fc6SRichard Alpe 
8387be57fc6SRichard Alpe 	err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
8397be57fc6SRichard Alpe 	if (err)
8407be57fc6SRichard Alpe 		goto attr_msg_full;
8417be57fc6SRichard Alpe 
8421da46568SYing Xue 	tipc_bclink_unlock(net);
8437be57fc6SRichard Alpe 	nla_nest_end(msg->skb, attrs);
8447be57fc6SRichard Alpe 	genlmsg_end(msg->skb, hdr);
8457be57fc6SRichard Alpe 
8467be57fc6SRichard Alpe 	return 0;
8477be57fc6SRichard Alpe 
8487be57fc6SRichard Alpe prop_msg_full:
8497be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, prop);
8507be57fc6SRichard Alpe attr_msg_full:
8517be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, attrs);
8527be57fc6SRichard Alpe msg_full:
8531da46568SYing Xue 	tipc_bclink_unlock(net);
8547be57fc6SRichard Alpe 	genlmsg_cancel(msg->skb, hdr);
8557be57fc6SRichard Alpe 
8567be57fc6SRichard Alpe 	return -EMSGSIZE;
8577be57fc6SRichard Alpe }
858b97bf3fdSPer Liden 
8591da46568SYing Xue int tipc_bclink_reset_stats(struct net *net)
860b97bf3fdSPer Liden {
8611da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8621da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8631da46568SYing Xue 
864b97bf3fdSPer Liden 	if (!bcl)
865b97bf3fdSPer Liden 		return -ENOPROTOOPT;
866b97bf3fdSPer Liden 
8671da46568SYing Xue 	tipc_bclink_lock(net);
868b97bf3fdSPer Liden 	memset(&bcl->stats, 0, sizeof(bcl->stats));
8691da46568SYing Xue 	tipc_bclink_unlock(net);
8700e35fd5eSAllan Stephens 	return 0;
871b97bf3fdSPer Liden }
872b97bf3fdSPer Liden 
8731da46568SYing Xue int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
874b97bf3fdSPer Liden {
8751da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8761da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8771da46568SYing Xue 
878b97bf3fdSPer Liden 	if (!bcl)
879b97bf3fdSPer Liden 		return -ENOPROTOOPT;
880b97bf3fdSPer Liden 	if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
881b97bf3fdSPer Liden 		return -EINVAL;
882b97bf3fdSPer Liden 
8831da46568SYing Xue 	tipc_bclink_lock(net);
8844323add6SPer Liden 	tipc_link_set_queue_limits(bcl, limit);
8851da46568SYing Xue 	tipc_bclink_unlock(net);
8860e35fd5eSAllan Stephens 	return 0;
887b97bf3fdSPer Liden }
888b97bf3fdSPer Liden 
8897f9f95d9SYing Xue int tipc_bclink_init(struct net *net)
890b97bf3fdSPer Liden {
8917f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8921da46568SYing Xue 	struct tipc_bcbearer *bcbearer;
8931da46568SYing Xue 	struct tipc_bclink *bclink;
8941da46568SYing Xue 	struct tipc_link *bcl;
8957f9f95d9SYing Xue 
896eb8b00f5SYing Xue 	bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
897eb8b00f5SYing Xue 	if (!bcbearer)
898eb8b00f5SYing Xue 		return -ENOMEM;
899eb8b00f5SYing Xue 
900eb8b00f5SYing Xue 	bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
901eb8b00f5SYing Xue 	if (!bclink) {
902eb8b00f5SYing Xue 		kfree(bcbearer);
903eb8b00f5SYing Xue 		return -ENOMEM;
904eb8b00f5SYing Xue 	}
905eb8b00f5SYing Xue 
906eb8b00f5SYing Xue 	bcl = &bclink->link;
907b97bf3fdSPer Liden 	bcbearer->bearer.media = &bcbearer->media;
9084323add6SPer Liden 	bcbearer->media.send_msg = tipc_bcbearer_send;
9092e2d9be8SAllan Stephens 	sprintf(bcbearer->media.name, "tipc-broadcast");
910b97bf3fdSPer Liden 
911d69afc90SYing Xue 	spin_lock_init(&bclink->lock);
91205dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->transmq);
91305dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->backlogq);
91405dcc5aaSJon Paul Maloy 	__skb_queue_head_init(&bcl->deferdq);
915c637c103SJon Paul Maloy 	skb_queue_head_init(&bcl->wakeupq);
916b97bf3fdSPer Liden 	bcl->next_out_no = 1;
91734af946aSIngo Molnar 	spin_lock_init(&bclink->node.lock);
918cb1b7280SJon Paul Maloy 	__skb_queue_head_init(&bclink->arrvq);
919cb1b7280SJon Paul Maloy 	skb_queue_head_init(&bclink->inputq);
920b97bf3fdSPer Liden 	bcl->owner = &bclink->node;
9211da46568SYing Xue 	bcl->owner->net = net;
922b97bf3fdSPer Liden 	bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
9234323add6SPer Liden 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
9247a2f7d18SYing Xue 	bcl->bearer_id = MAX_BEARERS;
9257f9f95d9SYing Xue 	rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
926b97bf3fdSPer Liden 	bcl->state = WORKING_WORKING;
927c5898636SJon Paul Maloy 	bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
928c5898636SJon Paul Maloy 	msg_set_prevnode(bcl->pmsg, tn->own_addr);
9294b704d59SStephen Hemminger 	strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
9301da46568SYing Xue 	tn->bcbearer = bcbearer;
9311da46568SYing Xue 	tn->bclink = bclink;
9321da46568SYing Xue 	tn->bcl = bcl;
933eb8b00f5SYing Xue 	return 0;
934b97bf3fdSPer Liden }
935b97bf3fdSPer Liden 
9367f9f95d9SYing Xue void tipc_bclink_stop(struct net *net)
937b97bf3fdSPer Liden {
9387f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9397f9f95d9SYing Xue 
9401da46568SYing Xue 	tipc_bclink_lock(net);
9411da46568SYing Xue 	tipc_link_purge_queues(tn->bcl);
9421da46568SYing Xue 	tipc_bclink_unlock(net);
943c47e9b91SAllan Stephens 
9447f9f95d9SYing Xue 	RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
945eb8b00f5SYing Xue 	synchronize_net();
9461da46568SYing Xue 	kfree(tn->bcbearer);
9471da46568SYing Xue 	kfree(tn->bclink);
948b97bf3fdSPer Liden }
949b97bf3fdSPer Liden 
9503e22e62bSAllan Stephens /**
9513e22e62bSAllan Stephens  * tipc_nmap_add - add a node to a node map
9523e22e62bSAllan Stephens  */
95328dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
9543e22e62bSAllan Stephens {
9553e22e62bSAllan Stephens 	int n = tipc_node(node);
9563e22e62bSAllan Stephens 	int w = n / WSIZE;
9573e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
9583e22e62bSAllan Stephens 
9593e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) == 0) {
9603e22e62bSAllan Stephens 		nm_ptr->count++;
9613e22e62bSAllan Stephens 		nm_ptr->map[w] |= mask;
9623e22e62bSAllan Stephens 	}
9633e22e62bSAllan Stephens }
9643e22e62bSAllan Stephens 
9653e22e62bSAllan Stephens /**
9663e22e62bSAllan Stephens  * tipc_nmap_remove - remove a node from a node map
9673e22e62bSAllan Stephens  */
96828dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
9693e22e62bSAllan Stephens {
9703e22e62bSAllan Stephens 	int n = tipc_node(node);
9713e22e62bSAllan Stephens 	int w = n / WSIZE;
9723e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
9733e22e62bSAllan Stephens 
9743e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) != 0) {
9753e22e62bSAllan Stephens 		nm_ptr->map[w] &= ~mask;
9763e22e62bSAllan Stephens 		nm_ptr->count--;
9773e22e62bSAllan Stephens 	}
9783e22e62bSAllan Stephens }
9793e22e62bSAllan Stephens 
9803e22e62bSAllan Stephens /**
9813e22e62bSAllan Stephens  * tipc_nmap_diff - find differences between node maps
9823e22e62bSAllan Stephens  * @nm_a: input node map A
9833e22e62bSAllan Stephens  * @nm_b: input node map B
9843e22e62bSAllan Stephens  * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
9853e22e62bSAllan Stephens  */
98631e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
98731e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
9883e22e62bSAllan Stephens 			   struct tipc_node_map *nm_diff)
9893e22e62bSAllan Stephens {
9903e22e62bSAllan Stephens 	int stop = ARRAY_SIZE(nm_a->map);
9913e22e62bSAllan Stephens 	int w;
9923e22e62bSAllan Stephens 	int b;
9933e22e62bSAllan Stephens 	u32 map;
9943e22e62bSAllan Stephens 
9953e22e62bSAllan Stephens 	memset(nm_diff, 0, sizeof(*nm_diff));
9963e22e62bSAllan Stephens 	for (w = 0; w < stop; w++) {
9973e22e62bSAllan Stephens 		map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
9983e22e62bSAllan Stephens 		nm_diff->map[w] = map;
9993e22e62bSAllan Stephens 		if (map != 0) {
10003e22e62bSAllan Stephens 			for (b = 0 ; b < WSIZE; b++) {
10013e22e62bSAllan Stephens 				if (map & (1 << b))
10023e22e62bSAllan Stephens 					nm_diff->count++;
10033e22e62bSAllan Stephens 			}
10043e22e62bSAllan Stephens 		}
10053e22e62bSAllan Stephens 	}
10063e22e62bSAllan Stephens }
1007