xref: /openbmc/linux/net/tipc/bcast.c (revision c5898636)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/bcast.c: TIPC broadcast code
3b97bf3fdSPer Liden  *
4078bec82SJon Paul Maloy  * Copyright (c) 2004-2006, 2014, 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 
82078bec82SJon Paul Maloy uint  tipc_bclink_get_mtu(void)
83078bec82SJon Paul Maloy {
84078bec82SJon Paul Maloy 	return MAX_PKT_DEFAULT_MCAST;
85078bec82SJon Paul Maloy }
86078bec82SJon Paul Maloy 
871da46568SYing Xue void tipc_bclink_set_flags(struct net *net, unsigned int flags)
883f5a12bdSYing Xue {
891da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
901da46568SYing Xue 
911da46568SYing Xue 	tn->bclink->flags |= flags;
92d69afc90SYing Xue }
93d69afc90SYing Xue 
9405790c64SSam Ravnborg static u32 bcbuf_acks(struct sk_buff *buf)
95b97bf3fdSPer Liden {
96880b005fSDavid S. Miller 	return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
97b97bf3fdSPer Liden }
98b97bf3fdSPer Liden 
9905790c64SSam Ravnborg static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
100b97bf3fdSPer Liden {
101880b005fSDavid S. Miller 	TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
102b97bf3fdSPer Liden }
103b97bf3fdSPer Liden 
10405790c64SSam Ravnborg static void bcbuf_decr_acks(struct sk_buff *buf)
105b97bf3fdSPer Liden {
106b97bf3fdSPer Liden 	bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
107b97bf3fdSPer Liden }
108b97bf3fdSPer Liden 
1091da46568SYing Xue void tipc_bclink_add_node(struct net *net, u32 addr)
110cd3decdfSAllan Stephens {
1111da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1121da46568SYing Xue 
1131da46568SYing Xue 	tipc_bclink_lock(net);
1141da46568SYing Xue 	tipc_nmap_add(&tn->bclink->bcast_nodes, addr);
1151da46568SYing Xue 	tipc_bclink_unlock(net);
116cd3decdfSAllan Stephens }
117cd3decdfSAllan Stephens 
1181da46568SYing Xue void tipc_bclink_remove_node(struct net *net, u32 addr)
119cd3decdfSAllan Stephens {
1201da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1211da46568SYing Xue 
1221da46568SYing Xue 	tipc_bclink_lock(net);
1231da46568SYing Xue 	tipc_nmap_remove(&tn->bclink->bcast_nodes, addr);
1241da46568SYing Xue 	tipc_bclink_unlock(net);
125cd3decdfSAllan Stephens }
126b97bf3fdSPer Liden 
1271da46568SYing Xue static void bclink_set_last_sent(struct net *net)
1285b1f7bdeSAllan Stephens {
1291da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1301da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
1311da46568SYing Xue 
1325b1f7bdeSAllan Stephens 	if (bcl->next_out)
1335b1f7bdeSAllan Stephens 		bcl->fsm_msg_cnt = mod(buf_seqno(bcl->next_out) - 1);
1345b1f7bdeSAllan Stephens 	else
1355b1f7bdeSAllan Stephens 		bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
1365b1f7bdeSAllan Stephens }
1375b1f7bdeSAllan Stephens 
1381da46568SYing Xue u32 tipc_bclink_get_last_sent(struct net *net)
1395b1f7bdeSAllan Stephens {
1401da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1411da46568SYing Xue 
1421da46568SYing Xue 	return tn->bcl->fsm_msg_cnt;
1435b1f7bdeSAllan Stephens }
1445b1f7bdeSAllan Stephens 
1457a54d4a9SAllan Stephens static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
146b97bf3fdSPer Liden {
1477a54d4a9SAllan Stephens 	node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
1487a54d4a9SAllan Stephens 						seqno : node->bclink.last_sent;
149b97bf3fdSPer Liden }
150b97bf3fdSPer Liden 
151b97bf3fdSPer Liden 
1522c53040fSBen Hutchings /**
15301d83eddSAllan Stephens  * tipc_bclink_retransmit_to - get most recent node to request retransmission
15401d83eddSAllan Stephens  *
155d69afc90SYing Xue  * Called with bclink_lock locked
15601d83eddSAllan Stephens  */
1571da46568SYing Xue struct tipc_node *tipc_bclink_retransmit_to(struct net *net)
15801d83eddSAllan Stephens {
1591da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1601da46568SYing Xue 
1611da46568SYing Xue 	return tn->bclink->retransmit_to;
16201d83eddSAllan Stephens }
16301d83eddSAllan Stephens 
16401d83eddSAllan Stephens /**
165b97bf3fdSPer Liden  * bclink_retransmit_pkt - retransmit broadcast packets
166b97bf3fdSPer Liden  * @after: sequence number of last packet to *not* retransmit
167b97bf3fdSPer Liden  * @to: sequence number of last packet to retransmit
168b97bf3fdSPer Liden  *
169d69afc90SYing Xue  * Called with bclink_lock locked
170b97bf3fdSPer Liden  */
1711da46568SYing Xue static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
172b97bf3fdSPer Liden {
17358dc55f2SYing Xue 	struct sk_buff *skb;
1741da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
175b97bf3fdSPer Liden 
17658dc55f2SYing Xue 	skb_queue_walk(&bcl->outqueue, skb) {
177703068eeSJon Maloy 		if (more(buf_seqno(skb), after)) {
178703068eeSJon Maloy 			tipc_link_retransmit(bcl, skb, mod(to - after));
17958dc55f2SYing Xue 			break;
18058dc55f2SYing Xue 		}
181703068eeSJon Maloy 	}
182b97bf3fdSPer Liden }
183b97bf3fdSPer Liden 
184b97bf3fdSPer Liden /**
185908344cdSJon Maloy  * tipc_bclink_wakeup_users - wake up pending users
186908344cdSJon Maloy  *
187908344cdSJon Maloy  * Called with no locks taken
188908344cdSJon Maloy  */
189f2f9800dSYing Xue void tipc_bclink_wakeup_users(struct net *net)
190908344cdSJon Maloy {
1911da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
1924988bb4aSErik Hugne 	struct sk_buff *skb;
1934988bb4aSErik Hugne 
1941da46568SYing Xue 	while ((skb = skb_dequeue(&tn->bclink->link.waiting_sks)))
195f2f9800dSYing Xue 		tipc_sk_rcv(net, skb);
196908344cdSJon Maloy }
197908344cdSJon Maloy 
198908344cdSJon Maloy /**
1994323add6SPer Liden  * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
200b97bf3fdSPer Liden  * @n_ptr: node that sent acknowledgement info
201b97bf3fdSPer Liden  * @acked: broadcast sequence # that has been acknowledged
202b97bf3fdSPer Liden  *
203d69afc90SYing Xue  * Node is locked, bclink_lock unlocked.
204b97bf3fdSPer Liden  */
2056c00055aSDavid S. Miller void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
206b97bf3fdSPer Liden {
20758dc55f2SYing Xue 	struct sk_buff *skb, *tmp;
208b97bf3fdSPer Liden 	struct sk_buff *next;
209b97bf3fdSPer Liden 	unsigned int released = 0;
2101da46568SYing Xue 	struct net *net = n_ptr->net;
2111da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
212b97bf3fdSPer Liden 
2131da46568SYing Xue 	tipc_bclink_lock(net);
21436559591SAllan Stephens 	/* Bail out if tx queue is empty (no clean up is required) */
2151da46568SYing Xue 	skb = skb_peek(&tn->bcl->outqueue);
21658dc55f2SYing Xue 	if (!skb)
21736559591SAllan Stephens 		goto exit;
21836559591SAllan Stephens 
21936559591SAllan Stephens 	/* Determine which messages need to be acknowledged */
22036559591SAllan Stephens 	if (acked == INVALID_LINK_SEQ) {
22136559591SAllan Stephens 		/*
22236559591SAllan Stephens 		 * Contact with specified node has been lost, so need to
22336559591SAllan Stephens 		 * acknowledge sent messages only (if other nodes still exist)
22436559591SAllan Stephens 		 * or both sent and unsent messages (otherwise)
22536559591SAllan Stephens 		 */
2261da46568SYing Xue 		if (tn->bclink->bcast_nodes.count)
2271da46568SYing Xue 			acked = tn->bcl->fsm_msg_cnt;
22836559591SAllan Stephens 		else
2291da46568SYing Xue 			acked = tn->bcl->next_out_no;
23036559591SAllan Stephens 	} else {
23136559591SAllan Stephens 		/*
23236559591SAllan Stephens 		 * Bail out if specified sequence number does not correspond
23336559591SAllan Stephens 		 * to a message that has been sent and not yet acknowledged
23436559591SAllan Stephens 		 */
23558dc55f2SYing Xue 		if (less(acked, buf_seqno(skb)) ||
2361da46568SYing Xue 		    less(tn->bcl->fsm_msg_cnt, acked) ||
23736559591SAllan Stephens 		    less_eq(acked, n_ptr->bclink.acked))
23836559591SAllan Stephens 			goto exit;
23936559591SAllan Stephens 	}
24036559591SAllan Stephens 
24136559591SAllan Stephens 	/* Skip over packets that node has previously acknowledged */
2421da46568SYing Xue 	skb_queue_walk(&tn->bcl->outqueue, skb) {
24358dc55f2SYing Xue 		if (more(buf_seqno(skb), n_ptr->bclink.acked))
24458dc55f2SYing Xue 			break;
24558dc55f2SYing Xue 	}
246b97bf3fdSPer Liden 
247b97bf3fdSPer Liden 	/* Update packets that node is now acknowledging */
2481da46568SYing Xue 	skb_queue_walk_from_safe(&tn->bcl->outqueue, skb, tmp) {
24958dc55f2SYing Xue 		if (more(buf_seqno(skb), acked))
25058dc55f2SYing Xue 			break;
251b97bf3fdSPer Liden 
2521da46568SYing Xue 		next = tipc_skb_queue_next(&tn->bcl->outqueue, skb);
2531da46568SYing Xue 		if (skb != tn->bcl->next_out) {
25458dc55f2SYing Xue 			bcbuf_decr_acks(skb);
25558dc55f2SYing Xue 		} else {
25658dc55f2SYing Xue 			bcbuf_set_acks(skb, 0);
2571da46568SYing Xue 			tn->bcl->next_out = next;
2581da46568SYing Xue 			bclink_set_last_sent(net);
25910745cd5SAllan Stephens 		}
26010745cd5SAllan Stephens 
26158dc55f2SYing Xue 		if (bcbuf_acks(skb) == 0) {
2621da46568SYing Xue 			__skb_unlink(skb, &tn->bcl->outqueue);
26358dc55f2SYing Xue 			kfree_skb(skb);
264b97bf3fdSPer Liden 			released = 1;
265b97bf3fdSPer Liden 		}
266b97bf3fdSPer Liden 	}
267b97bf3fdSPer Liden 	n_ptr->bclink.acked = acked;
268b97bf3fdSPer Liden 
269b97bf3fdSPer Liden 	/* Try resolving broadcast link congestion, if necessary */
2701da46568SYing Xue 	if (unlikely(tn->bcl->next_out)) {
2711da46568SYing Xue 		tipc_link_push_packets(tn->bcl);
2721da46568SYing Xue 		bclink_set_last_sent(net);
2735b1f7bdeSAllan Stephens 	}
2741da46568SYing Xue 	if (unlikely(released && !skb_queue_empty(&tn->bcl->waiting_sks)))
275908344cdSJon Maloy 		n_ptr->action_flags |= TIPC_WAKEUP_BCAST_USERS;
276908344cdSJon Maloy 
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  */
286*c5898636SJon 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;
290*c5898636SJon 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);
318bc6fecd4SYing Xue 		struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferred_queue);
319bc6fecd4SYing Xue 		u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
3207a54d4a9SAllan Stephens 
321*c5898636SJon 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 
3629fbfb8b1SJon Paul Maloy /* tipc_bclink_xmit - broadcast 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;
377078bec82SJon Paul Maloy 
378078bec82SJon Paul Maloy 	/* Prepare clone of message for local node */
379a6ca1094SYing Xue 	skb = tipc_msg_reassemble(list);
380a6ca1094SYing Xue 	if (unlikely(!skb)) {
381a6ca1094SYing Xue 		__skb_queue_purge(list);
382078bec82SJon Paul Maloy 		return -EHOSTUNREACH;
383078bec82SJon Paul Maloy 	}
384078bec82SJon Paul Maloy 
385078bec82SJon Paul Maloy 	/* Broadcast to all other nodes */
386078bec82SJon Paul Maloy 	if (likely(bclink)) {
3871da46568SYing Xue 		tipc_bclink_lock(net);
388078bec82SJon Paul Maloy 		if (likely(bclink->bcast_nodes.count)) {
3897f9f95d9SYing Xue 			rc = __tipc_link_xmit(net, bcl, list);
390078bec82SJon Paul Maloy 			if (likely(!rc)) {
39158dc55f2SYing Xue 				u32 len = skb_queue_len(&bcl->outqueue);
39258dc55f2SYing Xue 
3931da46568SYing Xue 				bclink_set_last_sent(net);
394078bec82SJon Paul Maloy 				bcl->stats.queue_sz_counts++;
39558dc55f2SYing Xue 				bcl->stats.accu_queue_sz += len;
396078bec82SJon Paul Maloy 			}
397078bec82SJon Paul Maloy 			bc = 1;
398078bec82SJon Paul Maloy 		}
3991da46568SYing Xue 		tipc_bclink_unlock(net);
400078bec82SJon Paul Maloy 	}
401078bec82SJon Paul Maloy 
402078bec82SJon Paul Maloy 	if (unlikely(!bc))
403a6ca1094SYing Xue 		__skb_queue_purge(list);
404078bec82SJon Paul Maloy 
405078bec82SJon Paul Maloy 	/* Deliver message clone */
406078bec82SJon Paul Maloy 	if (likely(!rc))
407f2f9800dSYing Xue 		tipc_sk_mcast_rcv(net, skb);
408078bec82SJon Paul Maloy 	else
409a6ca1094SYing Xue 		kfree_skb(skb);
410078bec82SJon Paul Maloy 
411078bec82SJon Paul Maloy 	return rc;
412078bec82SJon Paul Maloy }
413078bec82SJon Paul Maloy 
4142c53040fSBen Hutchings /**
41563e7f1acSAllan Stephens  * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
41663e7f1acSAllan Stephens  *
417d69afc90SYing Xue  * Called with both sending node's lock and bclink_lock taken.
41863e7f1acSAllan Stephens  */
41963e7f1acSAllan Stephens static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
42063e7f1acSAllan Stephens {
4211da46568SYing Xue 	struct tipc_net *tn = net_generic(node->net, tipc_net_id);
4221da46568SYing Xue 
42363e7f1acSAllan Stephens 	bclink_update_last_sent(node, seqno);
42463e7f1acSAllan Stephens 	node->bclink.last_in = seqno;
42563e7f1acSAllan Stephens 	node->bclink.oos_state = 0;
4261da46568SYing Xue 	tn->bcl->stats.recv_info++;
42763e7f1acSAllan Stephens 
42863e7f1acSAllan Stephens 	/*
42963e7f1acSAllan Stephens 	 * Unicast an ACK periodically, ensuring that
43063e7f1acSAllan Stephens 	 * all nodes in the cluster don't ACK at the same time
43163e7f1acSAllan Stephens 	 */
43234747539SYing Xue 	if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
433247f0f3cSYing Xue 		tipc_link_proto_xmit(node->active_links[node->addr & 1],
43463e7f1acSAllan Stephens 				     STATE_MSG, 0, 0, 0, 0, 0);
4351da46568SYing Xue 		tn->bcl->stats.sent_acks++;
43663e7f1acSAllan Stephens 	}
43763e7f1acSAllan Stephens }
43863e7f1acSAllan Stephens 
4392c53040fSBen Hutchings /**
440247f0f3cSYing Xue  * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
441b97bf3fdSPer Liden  *
4427216cd94SYing Xue  * RCU is locked, no other locks set
443b97bf3fdSPer Liden  */
444c93d3baaSYing Xue void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
445b97bf3fdSPer Liden {
446c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
4471da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
448b97bf3fdSPer Liden 	struct tipc_msg *msg = buf_msg(buf);
4495d3c488dSAllan Stephens 	struct tipc_node *node;
450b97bf3fdSPer Liden 	u32 next_in;
451b97bf3fdSPer Liden 	u32 seqno;
4520abd8ff2SJon Paul Maloy 	int deferred = 0;
453b97bf3fdSPer Liden 
4545d3c488dSAllan Stephens 	/* Screen out unwanted broadcast messages */
455c93d3baaSYing Xue 	if (msg_mc_netid(msg) != tn->net_id)
4565d3c488dSAllan Stephens 		goto exit;
4575d3c488dSAllan Stephens 
458f2f9800dSYing Xue 	node = tipc_node_find(net, msg_prevnode(msg));
4595d3c488dSAllan Stephens 	if (unlikely(!node))
4605d3c488dSAllan Stephens 		goto exit;
4615d3c488dSAllan Stephens 
4625d3c488dSAllan Stephens 	tipc_node_lock(node);
463389dd9bcSYing Xue 	if (unlikely(!node->bclink.recv_permitted))
4645d3c488dSAllan Stephens 		goto unlock;
465b97bf3fdSPer Liden 
4668a275a6aSAllan Stephens 	/* Handle broadcast protocol message */
467b97bf3fdSPer Liden 	if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
4689f6bdcd4SAllan Stephens 		if (msg_type(msg) != STATE_MSG)
4699f6bdcd4SAllan Stephens 			goto unlock;
47034747539SYing Xue 		if (msg_destnode(msg) == tn->own_addr) {
4714323add6SPer Liden 			tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
4724323add6SPer Liden 			tipc_node_unlock(node);
4731da46568SYing Xue 			tipc_bclink_lock(net);
474b97bf3fdSPer Liden 			bcl->stats.recv_nacks++;
4751da46568SYing Xue 			tn->bclink->retransmit_to = node;
4761da46568SYing Xue 			bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
477b97bf3fdSPer Liden 					      msg_bcgap_to(msg));
4781da46568SYing Xue 			tipc_bclink_unlock(net);
479b97bf3fdSPer Liden 		} else {
4805d3c488dSAllan Stephens 			tipc_node_unlock(node);
481f2f9800dSYing Xue 			bclink_peek_nack(net, msg);
482b97bf3fdSPer Liden 		}
4835d3c488dSAllan Stephens 		goto exit;
484b97bf3fdSPer Liden 	}
485b97bf3fdSPer Liden 
4865d3c488dSAllan Stephens 	/* Handle in-sequence broadcast message */
487b97bf3fdSPer Liden 	seqno = msg_seqno(msg);
4888a275a6aSAllan Stephens 	next_in = mod(node->bclink.last_in + 1);
489b97bf3fdSPer Liden 
490b97bf3fdSPer Liden 	if (likely(seqno == next_in)) {
4918a275a6aSAllan Stephens receive:
4927a54d4a9SAllan Stephens 		/* Deliver message to destination */
493b97bf3fdSPer Liden 		if (likely(msg_isdata(msg))) {
4941da46568SYing Xue 			tipc_bclink_lock(net);
49563e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
4961da46568SYing Xue 			tipc_bclink_unlock(net);
4974323add6SPer Liden 			tipc_node_unlock(node);
4989f6bdcd4SAllan Stephens 			if (likely(msg_mcast(msg)))
499f2f9800dSYing Xue 				tipc_sk_mcast_rcv(net, buf);
5009f6bdcd4SAllan Stephens 			else
5015f6d9123SAllan Stephens 				kfree_skb(buf);
502b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_BUNDLER) {
5031da46568SYing Xue 			tipc_bclink_lock(net);
50463e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
505b97bf3fdSPer Liden 			bcl->stats.recv_bundles++;
506b97bf3fdSPer Liden 			bcl->stats.recv_bundled += msg_msgcnt(msg);
5071da46568SYing Xue 			tipc_bclink_unlock(net);
5084323add6SPer Liden 			tipc_node_unlock(node);
509f2f9800dSYing Xue 			tipc_link_bundle_rcv(net, buf);
510b97bf3fdSPer Liden 		} else if (msg_user(msg) == MSG_FRAGMENTER) {
51137e22164SJon Paul Maloy 			tipc_buf_append(&node->bclink.reasm_buf, &buf);
51237e22164SJon Paul Maloy 			if (unlikely(!buf && !node->bclink.reasm_buf))
51363e7f1acSAllan Stephens 				goto unlock;
5141da46568SYing Xue 			tipc_bclink_lock(net);
51563e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
516b97bf3fdSPer Liden 			bcl->stats.recv_fragments++;
51737e22164SJon Paul Maloy 			if (buf) {
518b97bf3fdSPer Liden 				bcl->stats.recv_fragmented++;
51940ba3cdfSErik Hugne 				msg = buf_msg(buf);
5201da46568SYing Xue 				tipc_bclink_unlock(net);
521528f6f4bSErik Hugne 				goto receive;
522528f6f4bSErik Hugne 			}
5231da46568SYing Xue 			tipc_bclink_unlock(net);
5244323add6SPer Liden 			tipc_node_unlock(node);
5259f6bdcd4SAllan Stephens 		} else if (msg_user(msg) == NAME_DISTRIBUTOR) {
5261da46568SYing Xue 			tipc_bclink_lock(net);
52763e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
5281da46568SYing Xue 			tipc_bclink_unlock(net);
5299f6bdcd4SAllan Stephens 			tipc_node_unlock(node);
530f2f9800dSYing Xue 			tipc_named_rcv(net, buf);
531b97bf3fdSPer Liden 		} else {
5321da46568SYing Xue 			tipc_bclink_lock(net);
53363e7f1acSAllan Stephens 			bclink_accept_pkt(node, seqno);
5341da46568SYing Xue 			tipc_bclink_unlock(net);
5354323add6SPer Liden 			tipc_node_unlock(node);
5365f6d9123SAllan Stephens 			kfree_skb(buf);
537b97bf3fdSPer Liden 		}
5385d3c488dSAllan Stephens 		buf = NULL;
5398a275a6aSAllan Stephens 
5408a275a6aSAllan Stephens 		/* Determine new synchronization state */
5414323add6SPer Liden 		tipc_node_lock(node);
5428a275a6aSAllan Stephens 		if (unlikely(!tipc_node_is_up(node)))
5438a275a6aSAllan Stephens 			goto unlock;
5448a275a6aSAllan Stephens 
5457a54d4a9SAllan Stephens 		if (node->bclink.last_in == node->bclink.last_sent)
5468a275a6aSAllan Stephens 			goto unlock;
5478a275a6aSAllan Stephens 
548bc6fecd4SYing Xue 		if (skb_queue_empty(&node->bclink.deferred_queue)) {
5497a54d4a9SAllan Stephens 			node->bclink.oos_state = 1;
5507a54d4a9SAllan Stephens 			goto unlock;
5517a54d4a9SAllan Stephens 		}
5527a54d4a9SAllan Stephens 
553bc6fecd4SYing Xue 		msg = buf_msg(skb_peek(&node->bclink.deferred_queue));
5548a275a6aSAllan Stephens 		seqno = msg_seqno(msg);
5558a275a6aSAllan Stephens 		next_in = mod(next_in + 1);
5568a275a6aSAllan Stephens 		if (seqno != next_in)
5578a275a6aSAllan Stephens 			goto unlock;
5588a275a6aSAllan Stephens 
5598a275a6aSAllan Stephens 		/* Take in-sequence message from deferred queue & deliver it */
560bc6fecd4SYing Xue 		buf = __skb_dequeue(&node->bclink.deferred_queue);
561b97bf3fdSPer Liden 		goto receive;
562b97bf3fdSPer Liden 	}
5638a275a6aSAllan Stephens 
5648a275a6aSAllan Stephens 	/* Handle out-of-sequence broadcast message */
5658a275a6aSAllan Stephens 	if (less(next_in, seqno)) {
566bc6fecd4SYing Xue 		deferred = tipc_link_defer_pkt(&node->bclink.deferred_queue,
5678a275a6aSAllan Stephens 					       buf);
5687a54d4a9SAllan Stephens 		bclink_update_last_sent(node, seqno);
5695d3c488dSAllan Stephens 		buf = NULL;
5700abd8ff2SJon Paul Maloy 	}
5718a275a6aSAllan Stephens 
5721da46568SYing Xue 	tipc_bclink_lock(net);
573b98158e3SAllan Stephens 
5748a275a6aSAllan Stephens 	if (deferred)
5758a275a6aSAllan Stephens 		bcl->stats.deferred_recv++;
5760232c5a5SAllan Stephens 	else
5770232c5a5SAllan Stephens 		bcl->stats.duplicates++;
5788a275a6aSAllan Stephens 
5791da46568SYing Xue 	tipc_bclink_unlock(net);
580b98158e3SAllan Stephens 
5815d3c488dSAllan Stephens unlock:
5824323add6SPer Liden 	tipc_node_unlock(node);
5835d3c488dSAllan Stephens exit:
5845f6d9123SAllan Stephens 	kfree_skb(buf);
585b97bf3fdSPer Liden }
586b97bf3fdSPer Liden 
5876c00055aSDavid S. Miller u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
588b97bf3fdSPer Liden {
589389dd9bcSYing Xue 	return (n_ptr->bclink.recv_permitted &&
5901da46568SYing Xue 		(tipc_bclink_get_last_sent(n_ptr->net) != n_ptr->bclink.acked));
591b97bf3fdSPer Liden }
592b97bf3fdSPer Liden 
593b97bf3fdSPer Liden 
594b97bf3fdSPer Liden /**
5954323add6SPer Liden  * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
596b97bf3fdSPer Liden  *
5972ff9f924SAllan Stephens  * Send packet over as many bearers as necessary to reach all nodes
5982ff9f924SAllan Stephens  * that have joined the broadcast link.
599b97bf3fdSPer Liden  *
6002ff9f924SAllan Stephens  * Returns 0 (packet sent successfully) under all circumstances,
6012ff9f924SAllan Stephens  * since the broadcast link's pseudo-bearer never blocks
602b97bf3fdSPer Liden  */
6031da46568SYing Xue static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
6041da46568SYing Xue 			      struct tipc_bearer *unused1,
605b97bf3fdSPer Liden 			      struct tipc_media_addr *unused2)
606b97bf3fdSPer Liden {
607b97bf3fdSPer Liden 	int bp_index;
6086f92ee54SJon Paul Maloy 	struct tipc_msg *msg = buf_msg(buf);
609c93d3baaSYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6101da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6111da46568SYing Xue 	struct tipc_bclink *bclink = tn->bclink;
612b97bf3fdSPer Liden 
613e6160710SGerlando Falauto 	/* Prepare broadcast link message for reliable transmission,
6142ff9f924SAllan Stephens 	 * if first time trying to send it;
6152ff9f924SAllan Stephens 	 * preparation is skipped for broadcast link protocol messages
6162ff9f924SAllan Stephens 	 * since they are sent in an unreliable manner and don't need it
6172ff9f924SAllan Stephens 	 */
618b97bf3fdSPer Liden 	if (likely(!msg_non_seq(buf_msg(buf)))) {
619cd3decdfSAllan Stephens 		bcbuf_set_acks(buf, bclink->bcast_nodes.count);
62040aecb1bSAllan Stephens 		msg_set_non_seq(msg, 1);
621c93d3baaSYing Xue 		msg_set_mc_netid(msg, tn->net_id);
6221da46568SYing Xue 		tn->bcl->stats.sent_info++;
6235e726900SAllan Stephens 
624cd3decdfSAllan Stephens 		if (WARN_ON(!bclink->bcast_nodes.count)) {
6255e726900SAllan Stephens 			dump_stack();
6265e726900SAllan Stephens 			return 0;
6275e726900SAllan Stephens 		}
628b97bf3fdSPer Liden 	}
629b97bf3fdSPer Liden 
630b97bf3fdSPer Liden 	/* Send buffer over bearers until all targets reached */
631cd3decdfSAllan Stephens 	bcbearer->remains = bclink->bcast_nodes;
632b97bf3fdSPer Liden 
633b97bf3fdSPer Liden 	for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
6342d627b92SAllan Stephens 		struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
6352d627b92SAllan Stephens 		struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
6366f92ee54SJon Paul Maloy 		struct tipc_bearer *bp[2] = {p, s};
6376f92ee54SJon Paul Maloy 		struct tipc_bearer *b = bp[msg_link_selector(msg)];
638488fc9afSGerlando Falauto 		struct sk_buff *tbuf;
639b97bf3fdSPer Liden 
640b97bf3fdSPer Liden 		if (!p)
641e6160710SGerlando Falauto 			break; /* No more bearers to try */
6426f92ee54SJon Paul Maloy 		if (!b)
6436f92ee54SJon Paul Maloy 			b = p;
64477861d9cSGerlando Falauto 		tipc_nmap_diff(&bcbearer->remains, &b->nodes,
645e6160710SGerlando Falauto 			       &bcbearer->remains_new);
64665f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == bcbearer->remains.count)
647e6160710SGerlando Falauto 			continue; /* Nothing added by bearer pair */
648b97bf3fdSPer Liden 
649488fc9afSGerlando Falauto 		if (bp_index == 0) {
650488fc9afSGerlando Falauto 			/* Use original buffer for first bearer */
6517f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, buf, &b->bcast_addr);
652488fc9afSGerlando Falauto 		} else {
653488fc9afSGerlando Falauto 			/* Avoid concurrent buffer access */
654bad93e9dSOctavian Purdila 			tbuf = pskb_copy_for_clone(buf, GFP_ATOMIC);
655488fc9afSGerlando Falauto 			if (!tbuf)
656488fc9afSGerlando Falauto 				break;
6577f9f95d9SYing Xue 			tipc_bearer_send(net, b->identity, tbuf,
6587f9f95d9SYing Xue 					 &b->bcast_addr);
659488fc9afSGerlando Falauto 			kfree_skb(tbuf); /* Bearer keeps a clone */
660488fc9afSGerlando Falauto 		}
66165f51ef0SAllan Stephens 		if (bcbearer->remains_new.count == 0)
662e6160710SGerlando Falauto 			break; /* All targets reached */
663b97bf3fdSPer Liden 
66465f51ef0SAllan Stephens 		bcbearer->remains = bcbearer->remains_new;
665b97bf3fdSPer Liden 	}
666b97bf3fdSPer Liden 
6672ff9f924SAllan Stephens 	return 0;
668b97bf3fdSPer Liden }
669b97bf3fdSPer Liden 
670b97bf3fdSPer Liden /**
6714323add6SPer Liden  * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
672b97bf3fdSPer Liden  */
6737f9f95d9SYing Xue void tipc_bcbearer_sort(struct net *net, struct tipc_node_map *nm_ptr,
6747f9f95d9SYing Xue 			u32 node, bool action)
675b97bf3fdSPer Liden {
6767f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
6771da46568SYing Xue 	struct tipc_bcbearer *bcbearer = tn->bcbearer;
6787f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
6797f9ab6acSPaul Gortmaker 	struct tipc_bcbearer_pair *bp_curr;
680f8322dfcSYing Xue 	struct tipc_bearer *b;
681b97bf3fdSPer Liden 	int b_index;
682b97bf3fdSPer Liden 	int pri;
683b97bf3fdSPer Liden 
6841da46568SYing Xue 	tipc_bclink_lock(net);
685b97bf3fdSPer Liden 
68628dd9418SYing Xue 	if (action)
68728dd9418SYing Xue 		tipc_nmap_add(nm_ptr, node);
68828dd9418SYing Xue 	else
68928dd9418SYing Xue 		tipc_nmap_remove(nm_ptr, node);
69028dd9418SYing Xue 
691b97bf3fdSPer Liden 	/* Group bearers by priority (can assume max of two per priority) */
692b97bf3fdSPer Liden 	memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
693b97bf3fdSPer Liden 
694f8322dfcSYing Xue 	rcu_read_lock();
695b97bf3fdSPer Liden 	for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
6967f9f95d9SYing Xue 		b = rcu_dereference_rtnl(tn->bearer_list[b_index]);
697f47de12bSYing Xue 		if (!b || !b->nodes.count)
698b97bf3fdSPer Liden 			continue;
699b97bf3fdSPer Liden 
700b97bf3fdSPer Liden 		if (!bp_temp[b->priority].primary)
701b97bf3fdSPer Liden 			bp_temp[b->priority].primary = b;
702b97bf3fdSPer Liden 		else
703b97bf3fdSPer Liden 			bp_temp[b->priority].secondary = b;
704b97bf3fdSPer Liden 	}
705f8322dfcSYing Xue 	rcu_read_unlock();
706b97bf3fdSPer Liden 
707b97bf3fdSPer Liden 	/* Create array of bearer pairs for broadcasting */
708b97bf3fdSPer Liden 	bp_curr = bcbearer->bpairs;
709b97bf3fdSPer Liden 	memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
710b97bf3fdSPer Liden 
71116cb4b33SPer Liden 	for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
712b97bf3fdSPer Liden 
713b97bf3fdSPer Liden 		if (!bp_temp[pri].primary)
714b97bf3fdSPer Liden 			continue;
715b97bf3fdSPer Liden 
716b97bf3fdSPer Liden 		bp_curr->primary = bp_temp[pri].primary;
717b97bf3fdSPer Liden 
718b97bf3fdSPer Liden 		if (bp_temp[pri].secondary) {
7194323add6SPer Liden 			if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
720b97bf3fdSPer Liden 					    &bp_temp[pri].secondary->nodes)) {
721b97bf3fdSPer Liden 				bp_curr->secondary = bp_temp[pri].secondary;
722b97bf3fdSPer Liden 			} else {
723b97bf3fdSPer Liden 				bp_curr++;
724b97bf3fdSPer Liden 				bp_curr->primary = bp_temp[pri].secondary;
725b97bf3fdSPer Liden 			}
726b97bf3fdSPer Liden 		}
727b97bf3fdSPer Liden 
728b97bf3fdSPer Liden 		bp_curr++;
729b97bf3fdSPer Liden 	}
730b97bf3fdSPer Liden 
7311da46568SYing Xue 	tipc_bclink_unlock(net);
732b97bf3fdSPer Liden }
733b97bf3fdSPer Liden 
734d8182804SRichard Alpe static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
735d8182804SRichard Alpe 				      struct tipc_stats *stats)
7367be57fc6SRichard Alpe {
7377be57fc6SRichard Alpe 	int i;
7387be57fc6SRichard Alpe 	struct nlattr *nest;
7397be57fc6SRichard Alpe 
7407be57fc6SRichard Alpe 	struct nla_map {
7417be57fc6SRichard Alpe 		__u32 key;
7427be57fc6SRichard Alpe 		__u32 val;
7437be57fc6SRichard Alpe 	};
7447be57fc6SRichard Alpe 
7457be57fc6SRichard Alpe 	struct nla_map map[] = {
7467be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_INFO, stats->recv_info},
7477be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
7487be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
7497be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
7507be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
7517be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_INFO, stats->sent_info},
7527be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
7537be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
7547be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
7557be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
7567be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
7577be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
7587be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
7597be57fc6SRichard Alpe 		{TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
7607be57fc6SRichard Alpe 		{TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
7617be57fc6SRichard Alpe 		{TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
7627be57fc6SRichard Alpe 		{TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
7637be57fc6SRichard Alpe 		{TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
7647be57fc6SRichard Alpe 		{TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
7657be57fc6SRichard Alpe 			(stats->accu_queue_sz / stats->queue_sz_counts) : 0}
7667be57fc6SRichard Alpe 	};
7677be57fc6SRichard Alpe 
7687be57fc6SRichard Alpe 	nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
7697be57fc6SRichard Alpe 	if (!nest)
7707be57fc6SRichard Alpe 		return -EMSGSIZE;
7717be57fc6SRichard Alpe 
7727be57fc6SRichard Alpe 	for (i = 0; i <  ARRAY_SIZE(map); i++)
7737be57fc6SRichard Alpe 		if (nla_put_u32(skb, map[i].key, map[i].val))
7747be57fc6SRichard Alpe 			goto msg_full;
7757be57fc6SRichard Alpe 
7767be57fc6SRichard Alpe 	nla_nest_end(skb, nest);
7777be57fc6SRichard Alpe 
7787be57fc6SRichard Alpe 	return 0;
7797be57fc6SRichard Alpe msg_full:
7807be57fc6SRichard Alpe 	nla_nest_cancel(skb, nest);
7817be57fc6SRichard Alpe 
7827be57fc6SRichard Alpe 	return -EMSGSIZE;
7837be57fc6SRichard Alpe }
7847be57fc6SRichard Alpe 
7851da46568SYing Xue int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
7867be57fc6SRichard Alpe {
7877be57fc6SRichard Alpe 	int err;
7887be57fc6SRichard Alpe 	void *hdr;
7897be57fc6SRichard Alpe 	struct nlattr *attrs;
7907be57fc6SRichard Alpe 	struct nlattr *prop;
7911da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
7921da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
7937be57fc6SRichard Alpe 
7947be57fc6SRichard Alpe 	if (!bcl)
7957be57fc6SRichard Alpe 		return 0;
7967be57fc6SRichard Alpe 
7971da46568SYing Xue 	tipc_bclink_lock(net);
7987be57fc6SRichard Alpe 
7997be57fc6SRichard Alpe 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
8007be57fc6SRichard Alpe 			  NLM_F_MULTI, TIPC_NL_LINK_GET);
8017be57fc6SRichard Alpe 	if (!hdr)
8027be57fc6SRichard Alpe 		return -EMSGSIZE;
8037be57fc6SRichard Alpe 
8047be57fc6SRichard Alpe 	attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
8057be57fc6SRichard Alpe 	if (!attrs)
8067be57fc6SRichard Alpe 		goto msg_full;
8077be57fc6SRichard Alpe 
8087be57fc6SRichard Alpe 	/* The broadcast link is always up */
8097be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
8107be57fc6SRichard Alpe 		goto attr_msg_full;
8117be57fc6SRichard Alpe 
8127be57fc6SRichard Alpe 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
8137be57fc6SRichard Alpe 		goto attr_msg_full;
8147be57fc6SRichard Alpe 	if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
8157be57fc6SRichard Alpe 		goto attr_msg_full;
8167be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->next_in_no))
8177be57fc6SRichard Alpe 		goto attr_msg_full;
8187be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->next_out_no))
8197be57fc6SRichard Alpe 		goto attr_msg_full;
8207be57fc6SRichard Alpe 
8217be57fc6SRichard Alpe 	prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
8227be57fc6SRichard Alpe 	if (!prop)
8237be57fc6SRichard Alpe 		goto attr_msg_full;
8247be57fc6SRichard Alpe 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->queue_limit[0]))
8257be57fc6SRichard Alpe 		goto prop_msg_full;
8267be57fc6SRichard Alpe 	nla_nest_end(msg->skb, prop);
8277be57fc6SRichard Alpe 
8287be57fc6SRichard Alpe 	err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
8297be57fc6SRichard Alpe 	if (err)
8307be57fc6SRichard Alpe 		goto attr_msg_full;
8317be57fc6SRichard Alpe 
8321da46568SYing Xue 	tipc_bclink_unlock(net);
8337be57fc6SRichard Alpe 	nla_nest_end(msg->skb, attrs);
8347be57fc6SRichard Alpe 	genlmsg_end(msg->skb, hdr);
8357be57fc6SRichard Alpe 
8367be57fc6SRichard Alpe 	return 0;
8377be57fc6SRichard Alpe 
8387be57fc6SRichard Alpe prop_msg_full:
8397be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, prop);
8407be57fc6SRichard Alpe attr_msg_full:
8417be57fc6SRichard Alpe 	nla_nest_cancel(msg->skb, attrs);
8427be57fc6SRichard Alpe msg_full:
8431da46568SYing Xue 	tipc_bclink_unlock(net);
8447be57fc6SRichard Alpe 	genlmsg_cancel(msg->skb, hdr);
8457be57fc6SRichard Alpe 
8467be57fc6SRichard Alpe 	return -EMSGSIZE;
8477be57fc6SRichard Alpe }
848b97bf3fdSPer Liden 
8491da46568SYing Xue int tipc_bclink_stats(struct net *net, char *buf, const u32 buf_size)
850b97bf3fdSPer Liden {
851dc1aed37SErik Hugne 	int ret;
852dc1aed37SErik Hugne 	struct tipc_stats *s;
8531da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8541da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
855b97bf3fdSPer Liden 
856b97bf3fdSPer Liden 	if (!bcl)
857b97bf3fdSPer Liden 		return 0;
858b97bf3fdSPer Liden 
8591da46568SYing Xue 	tipc_bclink_lock(net);
860b97bf3fdSPer Liden 
861dc1aed37SErik Hugne 	s = &bcl->stats;
862dc1aed37SErik Hugne 
863dc1aed37SErik Hugne 	ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
864b97bf3fdSPer Liden 			    "  Window:%u packets\n",
865b97bf3fdSPer Liden 			    bcl->name, bcl->queue_limit[0]);
866dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
867dc1aed37SErik Hugne 			     "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
868dc1aed37SErik Hugne 			     s->recv_info, s->recv_fragments,
869dc1aed37SErik Hugne 			     s->recv_fragmented, s->recv_bundles,
870dc1aed37SErik Hugne 			     s->recv_bundled);
871dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
872dc1aed37SErik Hugne 			     "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
873dc1aed37SErik Hugne 			     s->sent_info, s->sent_fragments,
874dc1aed37SErik Hugne 			     s->sent_fragmented, s->sent_bundles,
875dc1aed37SErik Hugne 			     s->sent_bundled);
876dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
877dc1aed37SErik Hugne 			     "  RX naks:%u defs:%u dups:%u\n",
878dc1aed37SErik Hugne 			     s->recv_nacks, s->deferred_recv, s->duplicates);
879dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
880dc1aed37SErik Hugne 			     "  TX naks:%u acks:%u dups:%u\n",
881dc1aed37SErik Hugne 			     s->sent_nacks, s->sent_acks, s->retransmitted);
882dc1aed37SErik Hugne 	ret += tipc_snprintf(buf + ret, buf_size - ret,
8833c294cb3SYing Xue 			     "  Congestion link:%u  Send queue max:%u avg:%u\n",
8843c294cb3SYing Xue 			     s->link_congs, s->max_queue_sz,
885dc1aed37SErik Hugne 			     s->queue_sz_counts ?
886dc1aed37SErik Hugne 			     (s->accu_queue_sz / s->queue_sz_counts) : 0);
887b97bf3fdSPer Liden 
8881da46568SYing Xue 	tipc_bclink_unlock(net);
889dc1aed37SErik Hugne 	return ret;
890b97bf3fdSPer Liden }
891b97bf3fdSPer Liden 
8921da46568SYing Xue int tipc_bclink_reset_stats(struct net *net)
893b97bf3fdSPer Liden {
8941da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
8951da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
8961da46568SYing Xue 
897b97bf3fdSPer Liden 	if (!bcl)
898b97bf3fdSPer Liden 		return -ENOPROTOOPT;
899b97bf3fdSPer Liden 
9001da46568SYing Xue 	tipc_bclink_lock(net);
901b97bf3fdSPer Liden 	memset(&bcl->stats, 0, sizeof(bcl->stats));
9021da46568SYing Xue 	tipc_bclink_unlock(net);
9030e35fd5eSAllan Stephens 	return 0;
904b97bf3fdSPer Liden }
905b97bf3fdSPer Liden 
9061da46568SYing Xue int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
907b97bf3fdSPer Liden {
9081da46568SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9091da46568SYing Xue 	struct tipc_link *bcl = tn->bcl;
9101da46568SYing Xue 
911b97bf3fdSPer Liden 	if (!bcl)
912b97bf3fdSPer Liden 		return -ENOPROTOOPT;
913b97bf3fdSPer Liden 	if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
914b97bf3fdSPer Liden 		return -EINVAL;
915b97bf3fdSPer Liden 
9161da46568SYing Xue 	tipc_bclink_lock(net);
9174323add6SPer Liden 	tipc_link_set_queue_limits(bcl, limit);
9181da46568SYing Xue 	tipc_bclink_unlock(net);
9190e35fd5eSAllan Stephens 	return 0;
920b97bf3fdSPer Liden }
921b97bf3fdSPer Liden 
9227f9f95d9SYing Xue int tipc_bclink_init(struct net *net)
923b97bf3fdSPer Liden {
9247f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9251da46568SYing Xue 	struct tipc_bcbearer *bcbearer;
9261da46568SYing Xue 	struct tipc_bclink *bclink;
9271da46568SYing Xue 	struct tipc_link *bcl;
9287f9f95d9SYing Xue 
929eb8b00f5SYing Xue 	bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
930eb8b00f5SYing Xue 	if (!bcbearer)
931eb8b00f5SYing Xue 		return -ENOMEM;
932eb8b00f5SYing Xue 
933eb8b00f5SYing Xue 	bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
934eb8b00f5SYing Xue 	if (!bclink) {
935eb8b00f5SYing Xue 		kfree(bcbearer);
936eb8b00f5SYing Xue 		return -ENOMEM;
937eb8b00f5SYing Xue 	}
938eb8b00f5SYing Xue 
939eb8b00f5SYing Xue 	bcl = &bclink->link;
940b97bf3fdSPer Liden 	bcbearer->bearer.media = &bcbearer->media;
9414323add6SPer Liden 	bcbearer->media.send_msg = tipc_bcbearer_send;
9422e2d9be8SAllan Stephens 	sprintf(bcbearer->media.name, "tipc-broadcast");
943b97bf3fdSPer Liden 
944d69afc90SYing Xue 	spin_lock_init(&bclink->lock);
94558dc55f2SYing Xue 	__skb_queue_head_init(&bcl->outqueue);
946bc6fecd4SYing Xue 	__skb_queue_head_init(&bcl->deferred_queue);
9474988bb4aSErik Hugne 	skb_queue_head_init(&bcl->waiting_sks);
948b97bf3fdSPer Liden 	bcl->next_out_no = 1;
94934af946aSIngo Molnar 	spin_lock_init(&bclink->node.lock);
95050100a5eSJon Paul Maloy 	__skb_queue_head_init(&bclink->node.waiting_sks);
951b97bf3fdSPer Liden 	bcl->owner = &bclink->node;
9521da46568SYing Xue 	bcl->owner->net = net;
953b97bf3fdSPer Liden 	bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
9544323add6SPer Liden 	tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
9557a2f7d18SYing Xue 	bcl->bearer_id = MAX_BEARERS;
9567f9f95d9SYing Xue 	rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
957b97bf3fdSPer Liden 	bcl->state = WORKING_WORKING;
958*c5898636SJon Paul Maloy 	bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
959*c5898636SJon Paul Maloy 	msg_set_prevnode(bcl->pmsg, tn->own_addr);
9604b704d59SStephen Hemminger 	strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
9611da46568SYing Xue 	tn->bcbearer = bcbearer;
9621da46568SYing Xue 	tn->bclink = bclink;
9631da46568SYing Xue 	tn->bcl = bcl;
964eb8b00f5SYing Xue 	return 0;
965b97bf3fdSPer Liden }
966b97bf3fdSPer Liden 
9677f9f95d9SYing Xue void tipc_bclink_stop(struct net *net)
968b97bf3fdSPer Liden {
9697f9f95d9SYing Xue 	struct tipc_net *tn = net_generic(net, tipc_net_id);
9707f9f95d9SYing Xue 
9711da46568SYing Xue 	tipc_bclink_lock(net);
9721da46568SYing Xue 	tipc_link_purge_queues(tn->bcl);
9731da46568SYing Xue 	tipc_bclink_unlock(net);
974c47e9b91SAllan Stephens 
9757f9f95d9SYing Xue 	RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
976eb8b00f5SYing Xue 	synchronize_net();
9771da46568SYing Xue 	kfree(tn->bcbearer);
9781da46568SYing Xue 	kfree(tn->bclink);
979b97bf3fdSPer Liden }
980b97bf3fdSPer Liden 
9813e22e62bSAllan Stephens /**
9823e22e62bSAllan Stephens  * tipc_nmap_add - add a node to a node map
9833e22e62bSAllan Stephens  */
98428dd9418SYing Xue static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
9853e22e62bSAllan Stephens {
9863e22e62bSAllan Stephens 	int n = tipc_node(node);
9873e22e62bSAllan Stephens 	int w = n / WSIZE;
9883e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
9893e22e62bSAllan Stephens 
9903e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) == 0) {
9913e22e62bSAllan Stephens 		nm_ptr->count++;
9923e22e62bSAllan Stephens 		nm_ptr->map[w] |= mask;
9933e22e62bSAllan Stephens 	}
9943e22e62bSAllan Stephens }
9953e22e62bSAllan Stephens 
9963e22e62bSAllan Stephens /**
9973e22e62bSAllan Stephens  * tipc_nmap_remove - remove a node from a node map
9983e22e62bSAllan Stephens  */
99928dd9418SYing Xue static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
10003e22e62bSAllan Stephens {
10013e22e62bSAllan Stephens 	int n = tipc_node(node);
10023e22e62bSAllan Stephens 	int w = n / WSIZE;
10033e22e62bSAllan Stephens 	u32 mask = (1 << (n % WSIZE));
10043e22e62bSAllan Stephens 
10053e22e62bSAllan Stephens 	if ((nm_ptr->map[w] & mask) != 0) {
10063e22e62bSAllan Stephens 		nm_ptr->map[w] &= ~mask;
10073e22e62bSAllan Stephens 		nm_ptr->count--;
10083e22e62bSAllan Stephens 	}
10093e22e62bSAllan Stephens }
10103e22e62bSAllan Stephens 
10113e22e62bSAllan Stephens /**
10123e22e62bSAllan Stephens  * tipc_nmap_diff - find differences between node maps
10133e22e62bSAllan Stephens  * @nm_a: input node map A
10143e22e62bSAllan Stephens  * @nm_b: input node map B
10153e22e62bSAllan Stephens  * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
10163e22e62bSAllan Stephens  */
101731e3c3f6Sstephen hemminger static void tipc_nmap_diff(struct tipc_node_map *nm_a,
101831e3c3f6Sstephen hemminger 			   struct tipc_node_map *nm_b,
10193e22e62bSAllan Stephens 			   struct tipc_node_map *nm_diff)
10203e22e62bSAllan Stephens {
10213e22e62bSAllan Stephens 	int stop = ARRAY_SIZE(nm_a->map);
10223e22e62bSAllan Stephens 	int w;
10233e22e62bSAllan Stephens 	int b;
10243e22e62bSAllan Stephens 	u32 map;
10253e22e62bSAllan Stephens 
10263e22e62bSAllan Stephens 	memset(nm_diff, 0, sizeof(*nm_diff));
10273e22e62bSAllan Stephens 	for (w = 0; w < stop; w++) {
10283e22e62bSAllan Stephens 		map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
10293e22e62bSAllan Stephens 		nm_diff->map[w] = map;
10303e22e62bSAllan Stephens 		if (map != 0) {
10313e22e62bSAllan Stephens 			for (b = 0 ; b < WSIZE; b++) {
10323e22e62bSAllan Stephens 				if (map & (1 << b))
10333e22e62bSAllan Stephens 					nm_diff->count++;
10343e22e62bSAllan Stephens 			}
10353e22e62bSAllan Stephens 		}
10363e22e62bSAllan Stephens 	}
10373e22e62bSAllan Stephens }
103843608edcSAllan Stephens 
103943608edcSAllan Stephens /**
104043608edcSAllan Stephens  * tipc_port_list_add - add a port to a port list, ensuring no duplicates
104143608edcSAllan Stephens  */
10424584310bSPaul Gortmaker void tipc_port_list_add(struct tipc_port_list *pl_ptr, u32 port)
104343608edcSAllan Stephens {
10444584310bSPaul Gortmaker 	struct tipc_port_list *item = pl_ptr;
104543608edcSAllan Stephens 	int i;
104643608edcSAllan Stephens 	int item_sz = PLSIZE;
104743608edcSAllan Stephens 	int cnt = pl_ptr->count;
104843608edcSAllan Stephens 
104943608edcSAllan Stephens 	for (; ; cnt -= item_sz, item = item->next) {
105043608edcSAllan Stephens 		if (cnt < PLSIZE)
105143608edcSAllan Stephens 			item_sz = cnt;
105243608edcSAllan Stephens 		for (i = 0; i < item_sz; i++)
105343608edcSAllan Stephens 			if (item->ports[i] == port)
105443608edcSAllan Stephens 				return;
105543608edcSAllan Stephens 		if (i < PLSIZE) {
105643608edcSAllan Stephens 			item->ports[i] = port;
105743608edcSAllan Stephens 			pl_ptr->count++;
105843608edcSAllan Stephens 			return;
105943608edcSAllan Stephens 		}
106043608edcSAllan Stephens 		if (!item->next) {
106143608edcSAllan Stephens 			item->next = kmalloc(sizeof(*item), GFP_ATOMIC);
106243608edcSAllan Stephens 			if (!item->next) {
10632cf8aa19SErik Hugne 				pr_warn("Incomplete multicast delivery, no memory\n");
106443608edcSAllan Stephens 				return;
106543608edcSAllan Stephens 			}
106643608edcSAllan Stephens 			item->next->next = NULL;
106743608edcSAllan Stephens 		}
106843608edcSAllan Stephens 	}
106943608edcSAllan Stephens }
107043608edcSAllan Stephens 
107143608edcSAllan Stephens /**
107243608edcSAllan Stephens  * tipc_port_list_free - free dynamically created entries in port_list chain
107343608edcSAllan Stephens  *
107443608edcSAllan Stephens  */
10754584310bSPaul Gortmaker void tipc_port_list_free(struct tipc_port_list *pl_ptr)
107643608edcSAllan Stephens {
10774584310bSPaul Gortmaker 	struct tipc_port_list *item;
10784584310bSPaul Gortmaker 	struct tipc_port_list *next;
107943608edcSAllan Stephens 
108043608edcSAllan Stephens 	for (item = pl_ptr->next; item; item = next) {
108143608edcSAllan Stephens 		next = item->next;
108243608edcSAllan Stephens 		kfree(item);
108343608edcSAllan Stephens 	}
108443608edcSAllan Stephens }
1085