xref: /openbmc/linux/net/tipc/link.c (revision 0760aad038b5a032c31ea124feed63d88627d2f1)
1 /*
2  * net/tipc/link.c: TIPC link code
3  *
4  * Copyright (c) 1996-2007, 2012-2016, Ericsson AB
5  * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the names of the copyright holders nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include "core.h"
38 #include "subscr.h"
39 #include "link.h"
40 #include "bcast.h"
41 #include "socket.h"
42 #include "name_distr.h"
43 #include "discover.h"
44 #include "netlink.h"
45 #include "monitor.h"
46 #include "trace.h"
47 #include "crypto.h"
48 
49 #include <linux/pkt_sched.h>
50 
51 struct tipc_stats {
52 	u32 sent_pkts;
53 	u32 recv_pkts;
54 	u32 sent_states;
55 	u32 recv_states;
56 	u32 sent_probes;
57 	u32 recv_probes;
58 	u32 sent_nacks;
59 	u32 recv_nacks;
60 	u32 sent_acks;
61 	u32 sent_bundled;
62 	u32 sent_bundles;
63 	u32 recv_bundled;
64 	u32 recv_bundles;
65 	u32 retransmitted;
66 	u32 sent_fragmented;
67 	u32 sent_fragments;
68 	u32 recv_fragmented;
69 	u32 recv_fragments;
70 	u32 link_congs;		/* # port sends blocked by congestion */
71 	u32 deferred_recv;
72 	u32 duplicates;
73 	u32 max_queue_sz;	/* send queue size high water mark */
74 	u32 accu_queue_sz;	/* used for send queue size profiling */
75 	u32 queue_sz_counts;	/* used for send queue size profiling */
76 	u32 msg_length_counts;	/* used for message length profiling */
77 	u32 msg_lengths_total;	/* used for message length profiling */
78 	u32 msg_length_profile[7]; /* used for msg. length profiling */
79 };
80 
81 /**
82  * struct tipc_link - TIPC link data structure
83  * @addr: network address of link's peer node
84  * @name: link name character string
85  * @media_addr: media address to use when sending messages over link
86  * @timer: link timer
87  * @net: pointer to namespace struct
88  * @refcnt: reference counter for permanent references (owner node & timer)
89  * @peer_session: link session # being used by peer end of link
90  * @peer_bearer_id: bearer id used by link's peer endpoint
91  * @bearer_id: local bearer id used by link
92  * @tolerance: minimum link continuity loss needed to reset link [in ms]
93  * @abort_limit: # of unacknowledged continuity probes needed to reset link
94  * @state: current state of link FSM
95  * @peer_caps: bitmap describing capabilities of peer node
96  * @silent_intv_cnt: # of timer intervals without any reception from peer
97  * @proto_msg: template for control messages generated by link
98  * @pmsg: convenience pointer to "proto_msg" field
99  * @priority: current link priority
100  * @net_plane: current link network plane ('A' through 'H')
101  * @mon_state: cookie with information needed by link monitor
102  * @backlog_limit: backlog queue congestion thresholds (indexed by importance)
103  * @exp_msg_count: # of tunnelled messages expected during link changeover
104  * @reset_rcv_checkpt: seq # of last acknowledged message at time of link reset
105  * @mtu: current maximum packet size for this link
106  * @advertised_mtu: advertised own mtu when link is being established
107  * @transmitq: queue for sent, non-acked messages
108  * @backlogq: queue for messages waiting to be sent
109  * @snt_nxt: next sequence number to use for outbound messages
110  * @ackers: # of peers that needs to ack each packet before it can be released
111  * @acked: # last packet acked by a certain peer. Used for broadcast.
112  * @rcv_nxt: next sequence number to expect for inbound messages
113  * @deferred_queue: deferred queue saved OOS b'cast message received from node
114  * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
115  * @inputq: buffer queue for messages to be delivered upwards
116  * @namedq: buffer queue for name table messages to be delivered upwards
117  * @next_out: ptr to first unsent outbound message in queue
118  * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
119  * @long_msg_seq_no: next identifier to use for outbound fragmented messages
120  * @reasm_buf: head of partially reassembled inbound message fragments
121  * @bc_rcvr: marks that this is a broadcast receiver link
122  * @stats: collects statistics regarding link activity
123  */
124 struct tipc_link {
125 	u32 addr;
126 	char name[TIPC_MAX_LINK_NAME];
127 	struct net *net;
128 
129 	/* Management and link supervision data */
130 	u16 peer_session;
131 	u16 session;
132 	u16 snd_nxt_state;
133 	u16 rcv_nxt_state;
134 	u32 peer_bearer_id;
135 	u32 bearer_id;
136 	u32 tolerance;
137 	u32 abort_limit;
138 	u32 state;
139 	u16 peer_caps;
140 	bool in_session;
141 	bool active;
142 	u32 silent_intv_cnt;
143 	char if_name[TIPC_MAX_IF_NAME];
144 	u32 priority;
145 	char net_plane;
146 	struct tipc_mon_state mon_state;
147 	u16 rst_cnt;
148 
149 	/* Failover/synch */
150 	u16 drop_point;
151 	struct sk_buff *failover_reasm_skb;
152 	struct sk_buff_head failover_deferdq;
153 
154 	/* Max packet negotiation */
155 	u16 mtu;
156 	u16 advertised_mtu;
157 
158 	/* Sending */
159 	struct sk_buff_head transmq;
160 	struct sk_buff_head backlogq;
161 	struct {
162 		u16 len;
163 		u16 limit;
164 		struct sk_buff *target_bskb;
165 	} backlog[5];
166 	u16 snd_nxt;
167 
168 	/* Reception */
169 	u16 rcv_nxt;
170 	u32 rcv_unacked;
171 	struct sk_buff_head deferdq;
172 	struct sk_buff_head *inputq;
173 	struct sk_buff_head *namedq;
174 
175 	/* Congestion handling */
176 	struct sk_buff_head wakeupq;
177 	u16 window;
178 	u16 min_win;
179 	u16 ssthresh;
180 	u16 max_win;
181 	u16 cong_acks;
182 	u16 checkpoint;
183 
184 	/* Fragmentation/reassembly */
185 	struct sk_buff *reasm_buf;
186 	struct sk_buff *reasm_tnlmsg;
187 
188 	/* Broadcast */
189 	u16 ackers;
190 	u16 acked;
191 	u16 last_gap;
192 	struct tipc_gap_ack_blks *last_ga;
193 	struct tipc_link *bc_rcvlink;
194 	struct tipc_link *bc_sndlink;
195 	u8 nack_state;
196 	bool bc_peer_is_up;
197 
198 	/* Statistics */
199 	struct tipc_stats stats;
200 };
201 
202 /*
203  * Error message prefixes
204  */
205 static const char *link_co_err = "Link tunneling error, ";
206 static const char *link_rst_msg = "Resetting link ";
207 
208 /* Send states for broadcast NACKs
209  */
210 enum {
211 	BC_NACK_SND_CONDITIONAL,
212 	BC_NACK_SND_UNCONDITIONAL,
213 	BC_NACK_SND_SUPPRESS,
214 };
215 
216 #define TIPC_BC_RETR_LIM  (jiffies + msecs_to_jiffies(10))
217 #define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
218 
219 /* Link FSM states:
220  */
221 enum {
222 	LINK_ESTABLISHED     = 0xe,
223 	LINK_ESTABLISHING    = 0xe  << 4,
224 	LINK_RESET           = 0x1  << 8,
225 	LINK_RESETTING       = 0x2  << 12,
226 	LINK_PEER_RESET      = 0xd  << 16,
227 	LINK_FAILINGOVER     = 0xf  << 20,
228 	LINK_SYNCHING        = 0xc  << 24
229 };
230 
231 /* Link FSM state checking routines
232  */
233 static int link_is_up(struct tipc_link *l)
234 {
235 	return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
236 }
237 
238 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
239 			       struct sk_buff_head *xmitq);
240 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
241 				      bool probe_reply, u16 rcvgap,
242 				      int tolerance, int priority,
243 				      struct sk_buff_head *xmitq);
244 static void link_print(struct tipc_link *l, const char *str);
245 static int tipc_link_build_nack_msg(struct tipc_link *l,
246 				    struct sk_buff_head *xmitq);
247 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
248 					struct sk_buff_head *xmitq);
249 static u8 __tipc_build_gap_ack_blks(struct tipc_gap_ack_blks *ga,
250 				    struct tipc_link *l, u8 start_index);
251 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, struct tipc_msg *hdr);
252 static int tipc_link_advance_transmq(struct tipc_link *l, struct tipc_link *r,
253 				     u16 acked, u16 gap,
254 				     struct tipc_gap_ack_blks *ga,
255 				     struct sk_buff_head *xmitq,
256 				     bool *retransmitted, int *rc);
257 static void tipc_link_update_cwin(struct tipc_link *l, int released,
258 				  bool retransmitted);
259 /*
260  *  Simple non-static link routines (i.e. referenced outside this file)
261  */
262 bool tipc_link_is_up(struct tipc_link *l)
263 {
264 	return link_is_up(l);
265 }
266 
267 bool tipc_link_peer_is_down(struct tipc_link *l)
268 {
269 	return l->state == LINK_PEER_RESET;
270 }
271 
272 bool tipc_link_is_reset(struct tipc_link *l)
273 {
274 	return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
275 }
276 
277 bool tipc_link_is_establishing(struct tipc_link *l)
278 {
279 	return l->state == LINK_ESTABLISHING;
280 }
281 
282 bool tipc_link_is_synching(struct tipc_link *l)
283 {
284 	return l->state == LINK_SYNCHING;
285 }
286 
287 bool tipc_link_is_failingover(struct tipc_link *l)
288 {
289 	return l->state == LINK_FAILINGOVER;
290 }
291 
292 bool tipc_link_is_blocked(struct tipc_link *l)
293 {
294 	return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
295 }
296 
297 static bool link_is_bc_sndlink(struct tipc_link *l)
298 {
299 	return !l->bc_sndlink;
300 }
301 
302 static bool link_is_bc_rcvlink(struct tipc_link *l)
303 {
304 	return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
305 }
306 
307 void tipc_link_set_active(struct tipc_link *l, bool active)
308 {
309 	l->active = active;
310 }
311 
312 u32 tipc_link_id(struct tipc_link *l)
313 {
314 	return l->peer_bearer_id << 16 | l->bearer_id;
315 }
316 
317 int tipc_link_min_win(struct tipc_link *l)
318 {
319 	return l->min_win;
320 }
321 
322 int tipc_link_max_win(struct tipc_link *l)
323 {
324 	return l->max_win;
325 }
326 
327 int tipc_link_prio(struct tipc_link *l)
328 {
329 	return l->priority;
330 }
331 
332 unsigned long tipc_link_tolerance(struct tipc_link *l)
333 {
334 	return l->tolerance;
335 }
336 
337 struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
338 {
339 	return l->inputq;
340 }
341 
342 char tipc_link_plane(struct tipc_link *l)
343 {
344 	return l->net_plane;
345 }
346 
347 void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
348 {
349 	l->peer_caps = capabilities;
350 }
351 
352 void tipc_link_add_bc_peer(struct tipc_link *snd_l,
353 			   struct tipc_link *uc_l,
354 			   struct sk_buff_head *xmitq)
355 {
356 	struct tipc_link *rcv_l = uc_l->bc_rcvlink;
357 
358 	snd_l->ackers++;
359 	rcv_l->acked = snd_l->snd_nxt - 1;
360 	snd_l->state = LINK_ESTABLISHED;
361 	tipc_link_build_bc_init_msg(uc_l, xmitq);
362 }
363 
364 void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
365 			      struct tipc_link *rcv_l,
366 			      struct sk_buff_head *xmitq)
367 {
368 	u16 ack = snd_l->snd_nxt - 1;
369 
370 	snd_l->ackers--;
371 	rcv_l->bc_peer_is_up = true;
372 	rcv_l->state = LINK_ESTABLISHED;
373 	tipc_link_bc_ack_rcv(rcv_l, ack, 0, NULL, xmitq, NULL);
374 	trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
375 	tipc_link_reset(rcv_l);
376 	rcv_l->state = LINK_RESET;
377 	if (!snd_l->ackers) {
378 		trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
379 		tipc_link_reset(snd_l);
380 		snd_l->state = LINK_RESET;
381 		__skb_queue_purge(xmitq);
382 	}
383 }
384 
385 int tipc_link_bc_peers(struct tipc_link *l)
386 {
387 	return l->ackers;
388 }
389 
390 static u16 link_bc_rcv_gap(struct tipc_link *l)
391 {
392 	struct sk_buff *skb = skb_peek(&l->deferdq);
393 	u16 gap = 0;
394 
395 	if (more(l->snd_nxt, l->rcv_nxt))
396 		gap = l->snd_nxt - l->rcv_nxt;
397 	if (skb)
398 		gap = buf_seqno(skb) - l->rcv_nxt;
399 	return gap;
400 }
401 
402 void tipc_link_set_mtu(struct tipc_link *l, int mtu)
403 {
404 	l->mtu = mtu;
405 }
406 
407 int tipc_link_mtu(struct tipc_link *l)
408 {
409 	return l->mtu;
410 }
411 
412 int tipc_link_mss(struct tipc_link *l)
413 {
414 #ifdef CONFIG_TIPC_CRYPTO
415 	return l->mtu - INT_H_SIZE - EMSG_OVERHEAD;
416 #else
417 	return l->mtu - INT_H_SIZE;
418 #endif
419 }
420 
421 u16 tipc_link_rcv_nxt(struct tipc_link *l)
422 {
423 	return l->rcv_nxt;
424 }
425 
426 u16 tipc_link_acked(struct tipc_link *l)
427 {
428 	return l->acked;
429 }
430 
431 char *tipc_link_name(struct tipc_link *l)
432 {
433 	return l->name;
434 }
435 
436 u32 tipc_link_state(struct tipc_link *l)
437 {
438 	return l->state;
439 }
440 
441 /**
442  * tipc_link_create - create a new link
443  * @net: pointer to associated network namespace
444  * @if_name: associated interface name
445  * @bearer_id: id (index) of associated bearer
446  * @tolerance: link tolerance to be used by link
447  * @net_plane: network plane (A,B,c..) this link belongs to
448  * @mtu: mtu to be advertised by link
449  * @priority: priority to be used by link
450  * @min_win: minimal send window to be used by link
451  * @max_win: maximal send window to be used by link
452  * @session: session to be used by link
453  * @ownnode: identity of own node
454  * @peer: node id of peer node
455  * @peer_caps: bitmap describing peer node capabilities
456  * @bc_sndlink: the namespace global link used for broadcast sending
457  * @bc_rcvlink: the peer specific link used for broadcast reception
458  * @inputq: queue to put messages ready for delivery
459  * @namedq: queue to put binding table update messages ready for delivery
460  * @link: return value, pointer to put the created link
461  *
462  * Returns true if link was created, otherwise false
463  */
464 bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
465 		      int tolerance, char net_plane, u32 mtu, int priority,
466 		      u32 min_win, u32 max_win, u32 session, u32 self,
467 		      u32 peer, u8 *peer_id, u16 peer_caps,
468 		      struct tipc_link *bc_sndlink,
469 		      struct tipc_link *bc_rcvlink,
470 		      struct sk_buff_head *inputq,
471 		      struct sk_buff_head *namedq,
472 		      struct tipc_link **link)
473 {
474 	char peer_str[NODE_ID_STR_LEN] = {0,};
475 	char self_str[NODE_ID_STR_LEN] = {0,};
476 	struct tipc_link *l;
477 
478 	l = kzalloc(sizeof(*l), GFP_ATOMIC);
479 	if (!l)
480 		return false;
481 	*link = l;
482 	l->session = session;
483 
484 	/* Set link name for unicast links only */
485 	if (peer_id) {
486 		tipc_nodeid2string(self_str, tipc_own_id(net));
487 		if (strlen(self_str) > 16)
488 			sprintf(self_str, "%x", self);
489 		tipc_nodeid2string(peer_str, peer_id);
490 		if (strlen(peer_str) > 16)
491 			sprintf(peer_str, "%x", peer);
492 	}
493 	/* Peer i/f name will be completed by reset/activate message */
494 	snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
495 		 self_str, if_name, peer_str);
496 
497 	strcpy(l->if_name, if_name);
498 	l->addr = peer;
499 	l->peer_caps = peer_caps;
500 	l->net = net;
501 	l->in_session = false;
502 	l->bearer_id = bearer_id;
503 	l->tolerance = tolerance;
504 	if (bc_rcvlink)
505 		bc_rcvlink->tolerance = tolerance;
506 	l->net_plane = net_plane;
507 	l->advertised_mtu = mtu;
508 	l->mtu = mtu;
509 	l->priority = priority;
510 	tipc_link_set_queue_limits(l, min_win, max_win);
511 	l->ackers = 1;
512 	l->bc_sndlink = bc_sndlink;
513 	l->bc_rcvlink = bc_rcvlink;
514 	l->inputq = inputq;
515 	l->namedq = namedq;
516 	l->state = LINK_RESETTING;
517 	__skb_queue_head_init(&l->transmq);
518 	__skb_queue_head_init(&l->backlogq);
519 	__skb_queue_head_init(&l->deferdq);
520 	__skb_queue_head_init(&l->failover_deferdq);
521 	skb_queue_head_init(&l->wakeupq);
522 	skb_queue_head_init(l->inputq);
523 	return true;
524 }
525 
526 /**
527  * tipc_link_bc_create - create new link to be used for broadcast
528  * @net: pointer to associated network namespace
529  * @mtu: mtu to be used initially if no peers
530  * @window: send window to be used
531  * @inputq: queue to put messages ready for delivery
532  * @namedq: queue to put binding table update messages ready for delivery
533  * @link: return value, pointer to put the created link
534  *
535  * Returns true if link was created, otherwise false
536  */
537 bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer, u8 *peer_id,
538 			 int mtu, u32 min_win, u32 max_win, u16 peer_caps,
539 			 struct sk_buff_head *inputq,
540 			 struct sk_buff_head *namedq,
541 			 struct tipc_link *bc_sndlink,
542 			 struct tipc_link **link)
543 {
544 	struct tipc_link *l;
545 
546 	if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, min_win,
547 			      max_win, 0, ownnode, peer, NULL, peer_caps,
548 			      bc_sndlink, NULL, inputq, namedq, link))
549 		return false;
550 
551 	l = *link;
552 	if (peer_id) {
553 		char peer_str[NODE_ID_STR_LEN] = {0,};
554 
555 		tipc_nodeid2string(peer_str, peer_id);
556 		if (strlen(peer_str) > 16)
557 			sprintf(peer_str, "%x", peer);
558 		/* Broadcast receiver link name: "broadcast-link:<peer>" */
559 		snprintf(l->name, sizeof(l->name), "%s:%s", tipc_bclink_name,
560 			 peer_str);
561 	} else {
562 		strcpy(l->name, tipc_bclink_name);
563 	}
564 	trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
565 	tipc_link_reset(l);
566 	l->state = LINK_RESET;
567 	l->ackers = 0;
568 	l->bc_rcvlink = l;
569 
570 	/* Broadcast send link is always up */
571 	if (link_is_bc_sndlink(l))
572 		l->state = LINK_ESTABLISHED;
573 
574 	/* Disable replicast if even a single peer doesn't support it */
575 	if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
576 		tipc_bcast_toggle_rcast(net, false);
577 
578 	return true;
579 }
580 
581 /**
582  * tipc_link_fsm_evt - link finite state machine
583  * @l: pointer to link
584  * @evt: state machine event to be processed
585  */
586 int tipc_link_fsm_evt(struct tipc_link *l, int evt)
587 {
588 	int rc = 0;
589 	int old_state = l->state;
590 
591 	switch (l->state) {
592 	case LINK_RESETTING:
593 		switch (evt) {
594 		case LINK_PEER_RESET_EVT:
595 			l->state = LINK_PEER_RESET;
596 			break;
597 		case LINK_RESET_EVT:
598 			l->state = LINK_RESET;
599 			break;
600 		case LINK_FAILURE_EVT:
601 		case LINK_FAILOVER_BEGIN_EVT:
602 		case LINK_ESTABLISH_EVT:
603 		case LINK_FAILOVER_END_EVT:
604 		case LINK_SYNCH_BEGIN_EVT:
605 		case LINK_SYNCH_END_EVT:
606 		default:
607 			goto illegal_evt;
608 		}
609 		break;
610 	case LINK_RESET:
611 		switch (evt) {
612 		case LINK_PEER_RESET_EVT:
613 			l->state = LINK_ESTABLISHING;
614 			break;
615 		case LINK_FAILOVER_BEGIN_EVT:
616 			l->state = LINK_FAILINGOVER;
617 		case LINK_FAILURE_EVT:
618 		case LINK_RESET_EVT:
619 		case LINK_ESTABLISH_EVT:
620 		case LINK_FAILOVER_END_EVT:
621 			break;
622 		case LINK_SYNCH_BEGIN_EVT:
623 		case LINK_SYNCH_END_EVT:
624 		default:
625 			goto illegal_evt;
626 		}
627 		break;
628 	case LINK_PEER_RESET:
629 		switch (evt) {
630 		case LINK_RESET_EVT:
631 			l->state = LINK_ESTABLISHING;
632 			break;
633 		case LINK_PEER_RESET_EVT:
634 		case LINK_ESTABLISH_EVT:
635 		case LINK_FAILURE_EVT:
636 			break;
637 		case LINK_SYNCH_BEGIN_EVT:
638 		case LINK_SYNCH_END_EVT:
639 		case LINK_FAILOVER_BEGIN_EVT:
640 		case LINK_FAILOVER_END_EVT:
641 		default:
642 			goto illegal_evt;
643 		}
644 		break;
645 	case LINK_FAILINGOVER:
646 		switch (evt) {
647 		case LINK_FAILOVER_END_EVT:
648 			l->state = LINK_RESET;
649 			break;
650 		case LINK_PEER_RESET_EVT:
651 		case LINK_RESET_EVT:
652 		case LINK_ESTABLISH_EVT:
653 		case LINK_FAILURE_EVT:
654 			break;
655 		case LINK_FAILOVER_BEGIN_EVT:
656 		case LINK_SYNCH_BEGIN_EVT:
657 		case LINK_SYNCH_END_EVT:
658 		default:
659 			goto illegal_evt;
660 		}
661 		break;
662 	case LINK_ESTABLISHING:
663 		switch (evt) {
664 		case LINK_ESTABLISH_EVT:
665 			l->state = LINK_ESTABLISHED;
666 			break;
667 		case LINK_FAILOVER_BEGIN_EVT:
668 			l->state = LINK_FAILINGOVER;
669 			break;
670 		case LINK_RESET_EVT:
671 			l->state = LINK_RESET;
672 			break;
673 		case LINK_FAILURE_EVT:
674 		case LINK_PEER_RESET_EVT:
675 		case LINK_SYNCH_BEGIN_EVT:
676 		case LINK_FAILOVER_END_EVT:
677 			break;
678 		case LINK_SYNCH_END_EVT:
679 		default:
680 			goto illegal_evt;
681 		}
682 		break;
683 	case LINK_ESTABLISHED:
684 		switch (evt) {
685 		case LINK_PEER_RESET_EVT:
686 			l->state = LINK_PEER_RESET;
687 			rc |= TIPC_LINK_DOWN_EVT;
688 			break;
689 		case LINK_FAILURE_EVT:
690 			l->state = LINK_RESETTING;
691 			rc |= TIPC_LINK_DOWN_EVT;
692 			break;
693 		case LINK_RESET_EVT:
694 			l->state = LINK_RESET;
695 			break;
696 		case LINK_ESTABLISH_EVT:
697 		case LINK_SYNCH_END_EVT:
698 			break;
699 		case LINK_SYNCH_BEGIN_EVT:
700 			l->state = LINK_SYNCHING;
701 			break;
702 		case LINK_FAILOVER_BEGIN_EVT:
703 		case LINK_FAILOVER_END_EVT:
704 		default:
705 			goto illegal_evt;
706 		}
707 		break;
708 	case LINK_SYNCHING:
709 		switch (evt) {
710 		case LINK_PEER_RESET_EVT:
711 			l->state = LINK_PEER_RESET;
712 			rc |= TIPC_LINK_DOWN_EVT;
713 			break;
714 		case LINK_FAILURE_EVT:
715 			l->state = LINK_RESETTING;
716 			rc |= TIPC_LINK_DOWN_EVT;
717 			break;
718 		case LINK_RESET_EVT:
719 			l->state = LINK_RESET;
720 			break;
721 		case LINK_ESTABLISH_EVT:
722 		case LINK_SYNCH_BEGIN_EVT:
723 			break;
724 		case LINK_SYNCH_END_EVT:
725 			l->state = LINK_ESTABLISHED;
726 			break;
727 		case LINK_FAILOVER_BEGIN_EVT:
728 		case LINK_FAILOVER_END_EVT:
729 		default:
730 			goto illegal_evt;
731 		}
732 		break;
733 	default:
734 		pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
735 	}
736 	trace_tipc_link_fsm(l->name, old_state, l->state, evt);
737 	return rc;
738 illegal_evt:
739 	pr_err("Illegal FSM event %x in state %x on link %s\n",
740 	       evt, l->state, l->name);
741 	trace_tipc_link_fsm(l->name, old_state, l->state, evt);
742 	return rc;
743 }
744 
745 /* link_profile_stats - update statistical profiling of traffic
746  */
747 static void link_profile_stats(struct tipc_link *l)
748 {
749 	struct sk_buff *skb;
750 	struct tipc_msg *msg;
751 	int length;
752 
753 	/* Update counters used in statistical profiling of send traffic */
754 	l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
755 	l->stats.queue_sz_counts++;
756 
757 	skb = skb_peek(&l->transmq);
758 	if (!skb)
759 		return;
760 	msg = buf_msg(skb);
761 	length = msg_size(msg);
762 
763 	if (msg_user(msg) == MSG_FRAGMENTER) {
764 		if (msg_type(msg) != FIRST_FRAGMENT)
765 			return;
766 		length = msg_size(msg_inner_hdr(msg));
767 	}
768 	l->stats.msg_lengths_total += length;
769 	l->stats.msg_length_counts++;
770 	if (length <= 64)
771 		l->stats.msg_length_profile[0]++;
772 	else if (length <= 256)
773 		l->stats.msg_length_profile[1]++;
774 	else if (length <= 1024)
775 		l->stats.msg_length_profile[2]++;
776 	else if (length <= 4096)
777 		l->stats.msg_length_profile[3]++;
778 	else if (length <= 16384)
779 		l->stats.msg_length_profile[4]++;
780 	else if (length <= 32768)
781 		l->stats.msg_length_profile[5]++;
782 	else
783 		l->stats.msg_length_profile[6]++;
784 }
785 
786 /**
787  * tipc_link_too_silent - check if link is "too silent"
788  * @l: tipc link to be checked
789  *
790  * Returns true if the link 'silent_intv_cnt' is about to reach the
791  * 'abort_limit' value, otherwise false
792  */
793 bool tipc_link_too_silent(struct tipc_link *l)
794 {
795 	return (l->silent_intv_cnt + 2 > l->abort_limit);
796 }
797 
798 /* tipc_link_timeout - perform periodic task as instructed from node timeout
799  */
800 int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
801 {
802 	int mtyp = 0;
803 	int rc = 0;
804 	bool state = false;
805 	bool probe = false;
806 	bool setup = false;
807 	u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
808 	u16 bc_acked = l->bc_rcvlink->acked;
809 	struct tipc_mon_state *mstate = &l->mon_state;
810 
811 	trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
812 	trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
813 	switch (l->state) {
814 	case LINK_ESTABLISHED:
815 	case LINK_SYNCHING:
816 		mtyp = STATE_MSG;
817 		link_profile_stats(l);
818 		tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
819 		if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
820 			return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
821 		state = bc_acked != bc_snt;
822 		state |= l->bc_rcvlink->rcv_unacked;
823 		state |= l->rcv_unacked;
824 		state |= !skb_queue_empty(&l->transmq);
825 		probe = mstate->probing;
826 		probe |= l->silent_intv_cnt;
827 		if (probe || mstate->monitoring)
828 			l->silent_intv_cnt++;
829 		probe |= !skb_queue_empty(&l->deferdq);
830 		if (l->snd_nxt == l->checkpoint) {
831 			tipc_link_update_cwin(l, 0, 0);
832 			probe = true;
833 		}
834 		l->checkpoint = l->snd_nxt;
835 		break;
836 	case LINK_RESET:
837 		setup = l->rst_cnt++ <= 4;
838 		setup |= !(l->rst_cnt % 16);
839 		mtyp = RESET_MSG;
840 		break;
841 	case LINK_ESTABLISHING:
842 		setup = true;
843 		mtyp = ACTIVATE_MSG;
844 		break;
845 	case LINK_PEER_RESET:
846 	case LINK_RESETTING:
847 	case LINK_FAILINGOVER:
848 		break;
849 	default:
850 		break;
851 	}
852 
853 	if (state || probe || setup)
854 		tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
855 
856 	return rc;
857 }
858 
859 /**
860  * link_schedule_user - schedule a message sender for wakeup after congestion
861  * @l: congested link
862  * @hdr: header of message that is being sent
863  * Create pseudo msg to send back to user when congestion abates
864  */
865 static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
866 {
867 	u32 dnode = tipc_own_addr(l->net);
868 	u32 dport = msg_origport(hdr);
869 	struct sk_buff *skb;
870 
871 	/* Create and schedule wakeup pseudo message */
872 	skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
873 			      dnode, l->addr, dport, 0, 0);
874 	if (!skb)
875 		return -ENOBUFS;
876 	msg_set_dest_droppable(buf_msg(skb), true);
877 	TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
878 	skb_queue_tail(&l->wakeupq, skb);
879 	l->stats.link_congs++;
880 	trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
881 	return -ELINKCONG;
882 }
883 
884 /**
885  * link_prepare_wakeup - prepare users for wakeup after congestion
886  * @l: congested link
887  * Wake up a number of waiting users, as permitted by available space
888  * in the send queue
889  */
890 static void link_prepare_wakeup(struct tipc_link *l)
891 {
892 	struct sk_buff_head *wakeupq = &l->wakeupq;
893 	struct sk_buff_head *inputq = l->inputq;
894 	struct sk_buff *skb, *tmp;
895 	struct sk_buff_head tmpq;
896 	int avail[5] = {0,};
897 	int imp = 0;
898 
899 	__skb_queue_head_init(&tmpq);
900 
901 	for (; imp <= TIPC_SYSTEM_IMPORTANCE; imp++)
902 		avail[imp] = l->backlog[imp].limit - l->backlog[imp].len;
903 
904 	skb_queue_walk_safe(wakeupq, skb, tmp) {
905 		imp = TIPC_SKB_CB(skb)->chain_imp;
906 		if (avail[imp] <= 0)
907 			continue;
908 		avail[imp]--;
909 		__skb_unlink(skb, wakeupq);
910 		__skb_queue_tail(&tmpq, skb);
911 	}
912 
913 	spin_lock_bh(&inputq->lock);
914 	skb_queue_splice_tail(&tmpq, inputq);
915 	spin_unlock_bh(&inputq->lock);
916 
917 }
918 
919 /**
920  * tipc_link_set_skb_retransmit_time - set the time at which retransmission of
921  *                                     the given skb should be next attempted
922  * @skb: skb to set a future retransmission time for
923  * @l: link the skb will be transmitted on
924  */
925 static void tipc_link_set_skb_retransmit_time(struct sk_buff *skb,
926 					      struct tipc_link *l)
927 {
928 	if (link_is_bc_sndlink(l))
929 		TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
930 	else
931 		TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
932 }
933 
934 void tipc_link_reset(struct tipc_link *l)
935 {
936 	struct sk_buff_head list;
937 	u32 imp;
938 
939 	__skb_queue_head_init(&list);
940 
941 	l->in_session = false;
942 	/* Force re-synch of peer session number before establishing */
943 	l->peer_session--;
944 	l->session++;
945 	l->mtu = l->advertised_mtu;
946 
947 	spin_lock_bh(&l->wakeupq.lock);
948 	skb_queue_splice_init(&l->wakeupq, &list);
949 	spin_unlock_bh(&l->wakeupq.lock);
950 
951 	spin_lock_bh(&l->inputq->lock);
952 	skb_queue_splice_init(&list, l->inputq);
953 	spin_unlock_bh(&l->inputq->lock);
954 
955 	__skb_queue_purge(&l->transmq);
956 	__skb_queue_purge(&l->deferdq);
957 	__skb_queue_purge(&l->backlogq);
958 	__skb_queue_purge(&l->failover_deferdq);
959 	for (imp = 0; imp <= TIPC_SYSTEM_IMPORTANCE; imp++) {
960 		l->backlog[imp].len = 0;
961 		l->backlog[imp].target_bskb = NULL;
962 	}
963 	kfree_skb(l->reasm_buf);
964 	kfree_skb(l->reasm_tnlmsg);
965 	kfree_skb(l->failover_reasm_skb);
966 	l->reasm_buf = NULL;
967 	l->reasm_tnlmsg = NULL;
968 	l->failover_reasm_skb = NULL;
969 	l->rcv_unacked = 0;
970 	l->snd_nxt = 1;
971 	l->rcv_nxt = 1;
972 	l->snd_nxt_state = 1;
973 	l->rcv_nxt_state = 1;
974 	l->acked = 0;
975 	l->last_gap = 0;
976 	kfree(l->last_ga);
977 	l->last_ga = NULL;
978 	l->silent_intv_cnt = 0;
979 	l->rst_cnt = 0;
980 	l->bc_peer_is_up = false;
981 	memset(&l->mon_state, 0, sizeof(l->mon_state));
982 	tipc_link_reset_stats(l);
983 }
984 
985 /**
986  * tipc_link_xmit(): enqueue buffer list according to queue situation
987  * @l: link to use
988  * @list: chain of buffers containing message
989  * @xmitq: returned list of packets to be sent by caller
990  *
991  * Consumes the buffer chain.
992  * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
993  * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
994  */
995 int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
996 		   struct sk_buff_head *xmitq)
997 {
998 	struct tipc_msg *hdr = buf_msg(skb_peek(list));
999 	struct sk_buff_head *backlogq = &l->backlogq;
1000 	struct sk_buff_head *transmq = &l->transmq;
1001 	struct sk_buff *skb, *_skb;
1002 	u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1003 	u16 ack = l->rcv_nxt - 1;
1004 	u16 seqno = l->snd_nxt;
1005 	int pkt_cnt = skb_queue_len(list);
1006 	int imp = msg_importance(hdr);
1007 	unsigned int mss = tipc_link_mss(l);
1008 	unsigned int cwin = l->window;
1009 	unsigned int mtu = l->mtu;
1010 	bool new_bundle;
1011 	int rc = 0;
1012 
1013 	if (unlikely(msg_size(hdr) > mtu)) {
1014 		pr_warn("Too large msg, purging xmit list %d %d %d %d %d!\n",
1015 			skb_queue_len(list), msg_user(hdr),
1016 			msg_type(hdr), msg_size(hdr), mtu);
1017 		__skb_queue_purge(list);
1018 		return -EMSGSIZE;
1019 	}
1020 
1021 	/* Allow oversubscription of one data msg per source at congestion */
1022 	if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
1023 		if (imp == TIPC_SYSTEM_IMPORTANCE) {
1024 			pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
1025 			return -ENOBUFS;
1026 		}
1027 		rc = link_schedule_user(l, hdr);
1028 	}
1029 
1030 	if (pkt_cnt > 1) {
1031 		l->stats.sent_fragmented++;
1032 		l->stats.sent_fragments += pkt_cnt;
1033 	}
1034 
1035 	/* Prepare each packet for sending, and add to relevant queue: */
1036 	while ((skb = __skb_dequeue(list))) {
1037 		if (likely(skb_queue_len(transmq) < cwin)) {
1038 			hdr = buf_msg(skb);
1039 			msg_set_seqno(hdr, seqno);
1040 			msg_set_ack(hdr, ack);
1041 			msg_set_bcast_ack(hdr, bc_ack);
1042 			_skb = skb_clone(skb, GFP_ATOMIC);
1043 			if (!_skb) {
1044 				kfree_skb(skb);
1045 				__skb_queue_purge(list);
1046 				return -ENOBUFS;
1047 			}
1048 			__skb_queue_tail(transmq, skb);
1049 			tipc_link_set_skb_retransmit_time(skb, l);
1050 			__skb_queue_tail(xmitq, _skb);
1051 			TIPC_SKB_CB(skb)->ackers = l->ackers;
1052 			l->rcv_unacked = 0;
1053 			l->stats.sent_pkts++;
1054 			seqno++;
1055 			continue;
1056 		}
1057 		if (tipc_msg_try_bundle(l->backlog[imp].target_bskb, &skb,
1058 					mss, l->addr, &new_bundle)) {
1059 			if (skb) {
1060 				/* Keep a ref. to the skb for next try */
1061 				l->backlog[imp].target_bskb = skb;
1062 				l->backlog[imp].len++;
1063 				__skb_queue_tail(backlogq, skb);
1064 			} else {
1065 				if (new_bundle) {
1066 					l->stats.sent_bundles++;
1067 					l->stats.sent_bundled++;
1068 				}
1069 				l->stats.sent_bundled++;
1070 			}
1071 			continue;
1072 		}
1073 		l->backlog[imp].target_bskb = NULL;
1074 		l->backlog[imp].len += (1 + skb_queue_len(list));
1075 		__skb_queue_tail(backlogq, skb);
1076 		skb_queue_splice_tail_init(list, backlogq);
1077 	}
1078 	l->snd_nxt = seqno;
1079 	return rc;
1080 }
1081 
1082 static void tipc_link_update_cwin(struct tipc_link *l, int released,
1083 				  bool retransmitted)
1084 {
1085 	int bklog_len = skb_queue_len(&l->backlogq);
1086 	struct sk_buff_head *txq = &l->transmq;
1087 	int txq_len = skb_queue_len(txq);
1088 	u16 cwin = l->window;
1089 
1090 	/* Enter fast recovery */
1091 	if (unlikely(retransmitted)) {
1092 		l->ssthresh = max_t(u16, l->window / 2, 300);
1093 		l->window = min_t(u16, l->ssthresh, l->window);
1094 		return;
1095 	}
1096 	/* Enter slow start */
1097 	if (unlikely(!released)) {
1098 		l->ssthresh = max_t(u16, l->window / 2, 300);
1099 		l->window = l->min_win;
1100 		return;
1101 	}
1102 	/* Don't increase window if no pressure on the transmit queue */
1103 	if (txq_len + bklog_len < cwin)
1104 		return;
1105 
1106 	/* Don't increase window if there are holes the transmit queue */
1107 	if (txq_len && l->snd_nxt - buf_seqno(skb_peek(txq)) != txq_len)
1108 		return;
1109 
1110 	l->cong_acks += released;
1111 
1112 	/* Slow start  */
1113 	if (cwin <= l->ssthresh) {
1114 		l->window = min_t(u16, cwin + released, l->max_win);
1115 		return;
1116 	}
1117 	/* Congestion avoidance */
1118 	if (l->cong_acks < cwin)
1119 		return;
1120 	l->window = min_t(u16, ++cwin, l->max_win);
1121 	l->cong_acks = 0;
1122 }
1123 
1124 static void tipc_link_advance_backlog(struct tipc_link *l,
1125 				      struct sk_buff_head *xmitq)
1126 {
1127 	u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1128 	struct sk_buff_head *txq = &l->transmq;
1129 	struct sk_buff *skb, *_skb;
1130 	u16 ack = l->rcv_nxt - 1;
1131 	u16 seqno = l->snd_nxt;
1132 	struct tipc_msg *hdr;
1133 	u16 cwin = l->window;
1134 	u32 imp;
1135 
1136 	while (skb_queue_len(txq) < cwin) {
1137 		skb = skb_peek(&l->backlogq);
1138 		if (!skb)
1139 			break;
1140 		_skb = skb_clone(skb, GFP_ATOMIC);
1141 		if (!_skb)
1142 			break;
1143 		__skb_dequeue(&l->backlogq);
1144 		hdr = buf_msg(skb);
1145 		imp = msg_importance(hdr);
1146 		l->backlog[imp].len--;
1147 		if (unlikely(skb == l->backlog[imp].target_bskb))
1148 			l->backlog[imp].target_bskb = NULL;
1149 		__skb_queue_tail(&l->transmq, skb);
1150 		tipc_link_set_skb_retransmit_time(skb, l);
1151 
1152 		__skb_queue_tail(xmitq, _skb);
1153 		TIPC_SKB_CB(skb)->ackers = l->ackers;
1154 		msg_set_seqno(hdr, seqno);
1155 		msg_set_ack(hdr, ack);
1156 		msg_set_bcast_ack(hdr, bc_ack);
1157 		l->rcv_unacked = 0;
1158 		l->stats.sent_pkts++;
1159 		seqno++;
1160 	}
1161 	l->snd_nxt = seqno;
1162 }
1163 
1164 /**
1165  * link_retransmit_failure() - Detect repeated retransmit failures
1166  * @l: tipc link sender
1167  * @r: tipc link receiver (= l in case of unicast)
1168  * @rc: returned code
1169  *
1170  * Return: true if the repeated retransmit failures happens, otherwise
1171  * false
1172  */
1173 static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
1174 				    int *rc)
1175 {
1176 	struct sk_buff *skb = skb_peek(&l->transmq);
1177 	struct tipc_msg *hdr;
1178 
1179 	if (!skb)
1180 		return false;
1181 
1182 	if (!TIPC_SKB_CB(skb)->retr_cnt)
1183 		return false;
1184 
1185 	if (!time_after(jiffies, TIPC_SKB_CB(skb)->retr_stamp +
1186 			msecs_to_jiffies(r->tolerance * 10)))
1187 		return false;
1188 
1189 	hdr = buf_msg(skb);
1190 	if (link_is_bc_sndlink(l) && !less(r->acked, msg_seqno(hdr)))
1191 		return false;
1192 
1193 	pr_warn("Retransmission failure on link <%s>\n", l->name);
1194 	link_print(l, "State of link ");
1195 	pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
1196 		msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
1197 	pr_info("sqno %u, prev: %x, dest: %x\n",
1198 		msg_seqno(hdr), msg_prevnode(hdr), msg_destnode(hdr));
1199 	pr_info("retr_stamp %d, retr_cnt %d\n",
1200 		jiffies_to_msecs(TIPC_SKB_CB(skb)->retr_stamp),
1201 		TIPC_SKB_CB(skb)->retr_cnt);
1202 
1203 	trace_tipc_list_dump(&l->transmq, true, "retrans failure!");
1204 	trace_tipc_link_dump(l, TIPC_DUMP_NONE, "retrans failure!");
1205 	trace_tipc_link_dump(r, TIPC_DUMP_NONE, "retrans failure!");
1206 
1207 	if (link_is_bc_sndlink(l)) {
1208 		r->state = LINK_RESET;
1209 		*rc |= TIPC_LINK_DOWN_EVT;
1210 	} else {
1211 		*rc |= tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1212 	}
1213 
1214 	return true;
1215 }
1216 
1217 /* tipc_data_input - deliver data and name distr msgs to upper layer
1218  *
1219  * Consumes buffer if message is of right type
1220  * Node lock must be held
1221  */
1222 static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
1223 			    struct sk_buff_head *inputq)
1224 {
1225 	struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
1226 	struct tipc_msg *hdr = buf_msg(skb);
1227 
1228 	switch (msg_user(hdr)) {
1229 	case TIPC_LOW_IMPORTANCE:
1230 	case TIPC_MEDIUM_IMPORTANCE:
1231 	case TIPC_HIGH_IMPORTANCE:
1232 	case TIPC_CRITICAL_IMPORTANCE:
1233 		if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
1234 			skb_queue_tail(mc_inputq, skb);
1235 			return true;
1236 		}
1237 		fallthrough;
1238 	case CONN_MANAGER:
1239 		skb_queue_tail(inputq, skb);
1240 		return true;
1241 	case GROUP_PROTOCOL:
1242 		skb_queue_tail(mc_inputq, skb);
1243 		return true;
1244 	case NAME_DISTRIBUTOR:
1245 		l->bc_rcvlink->state = LINK_ESTABLISHED;
1246 		skb_queue_tail(l->namedq, skb);
1247 		return true;
1248 	case MSG_BUNDLER:
1249 	case TUNNEL_PROTOCOL:
1250 	case MSG_FRAGMENTER:
1251 	case BCAST_PROTOCOL:
1252 		return false;
1253 	default:
1254 		pr_warn("Dropping received illegal msg type\n");
1255 		kfree_skb(skb);
1256 		return true;
1257 	};
1258 }
1259 
1260 /* tipc_link_input - process packet that has passed link protocol check
1261  *
1262  * Consumes buffer
1263  */
1264 static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
1265 			   struct sk_buff_head *inputq,
1266 			   struct sk_buff **reasm_skb)
1267 {
1268 	struct tipc_msg *hdr = buf_msg(skb);
1269 	struct sk_buff *iskb;
1270 	struct sk_buff_head tmpq;
1271 	int usr = msg_user(hdr);
1272 	int pos = 0;
1273 
1274 	if (usr == MSG_BUNDLER) {
1275 		skb_queue_head_init(&tmpq);
1276 		l->stats.recv_bundles++;
1277 		l->stats.recv_bundled += msg_msgcnt(hdr);
1278 		while (tipc_msg_extract(skb, &iskb, &pos))
1279 			tipc_data_input(l, iskb, &tmpq);
1280 		tipc_skb_queue_splice_tail(&tmpq, inputq);
1281 		return 0;
1282 	} else if (usr == MSG_FRAGMENTER) {
1283 		l->stats.recv_fragments++;
1284 		if (tipc_buf_append(reasm_skb, &skb)) {
1285 			l->stats.recv_fragmented++;
1286 			tipc_data_input(l, skb, inputq);
1287 		} else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
1288 			pr_warn_ratelimited("Unable to build fragment list\n");
1289 			return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1290 		}
1291 		return 0;
1292 	} else if (usr == BCAST_PROTOCOL) {
1293 		tipc_bcast_lock(l->net);
1294 		tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
1295 		tipc_bcast_unlock(l->net);
1296 	}
1297 
1298 	kfree_skb(skb);
1299 	return 0;
1300 }
1301 
1302 /* tipc_link_tnl_rcv() - receive TUNNEL_PROTOCOL message, drop or process the
1303  *			 inner message along with the ones in the old link's
1304  *			 deferdq
1305  * @l: tunnel link
1306  * @skb: TUNNEL_PROTOCOL message
1307  * @inputq: queue to put messages ready for delivery
1308  */
1309 static int tipc_link_tnl_rcv(struct tipc_link *l, struct sk_buff *skb,
1310 			     struct sk_buff_head *inputq)
1311 {
1312 	struct sk_buff **reasm_skb = &l->failover_reasm_skb;
1313 	struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
1314 	struct sk_buff_head *fdefq = &l->failover_deferdq;
1315 	struct tipc_msg *hdr = buf_msg(skb);
1316 	struct sk_buff *iskb;
1317 	int ipos = 0;
1318 	int rc = 0;
1319 	u16 seqno;
1320 
1321 	if (msg_type(hdr) == SYNCH_MSG) {
1322 		kfree_skb(skb);
1323 		return 0;
1324 	}
1325 
1326 	/* Not a fragment? */
1327 	if (likely(!msg_nof_fragms(hdr))) {
1328 		if (unlikely(!tipc_msg_extract(skb, &iskb, &ipos))) {
1329 			pr_warn_ratelimited("Unable to extract msg, defq: %d\n",
1330 					    skb_queue_len(fdefq));
1331 			return 0;
1332 		}
1333 		kfree_skb(skb);
1334 	} else {
1335 		/* Set fragment type for buf_append */
1336 		if (msg_fragm_no(hdr) == 1)
1337 			msg_set_type(hdr, FIRST_FRAGMENT);
1338 		else if (msg_fragm_no(hdr) < msg_nof_fragms(hdr))
1339 			msg_set_type(hdr, FRAGMENT);
1340 		else
1341 			msg_set_type(hdr, LAST_FRAGMENT);
1342 
1343 		if (!tipc_buf_append(reasm_tnlmsg, &skb)) {
1344 			/* Successful but non-complete reassembly? */
1345 			if (*reasm_tnlmsg || link_is_bc_rcvlink(l))
1346 				return 0;
1347 			pr_warn_ratelimited("Unable to reassemble tunnel msg\n");
1348 			return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1349 		}
1350 		iskb = skb;
1351 	}
1352 
1353 	do {
1354 		seqno = buf_seqno(iskb);
1355 		if (unlikely(less(seqno, l->drop_point))) {
1356 			kfree_skb(iskb);
1357 			continue;
1358 		}
1359 		if (unlikely(seqno != l->drop_point)) {
1360 			__tipc_skb_queue_sorted(fdefq, seqno, iskb);
1361 			continue;
1362 		}
1363 
1364 		l->drop_point++;
1365 		if (!tipc_data_input(l, iskb, inputq))
1366 			rc |= tipc_link_input(l, iskb, inputq, reasm_skb);
1367 		if (unlikely(rc))
1368 			break;
1369 	} while ((iskb = __tipc_skb_dequeue(fdefq, l->drop_point)));
1370 
1371 	return rc;
1372 }
1373 
1374 /**
1375  * tipc_get_gap_ack_blks - get Gap ACK blocks from PROTOCOL/STATE_MSG
1376  * @ga: returned pointer to the Gap ACK blocks if any
1377  * @l: the tipc link
1378  * @hdr: the PROTOCOL/STATE_MSG header
1379  * @uc: desired Gap ACK blocks type, i.e. unicast (= 1) or broadcast (= 0)
1380  *
1381  * Return: the total Gap ACK blocks size
1382  */
1383 u16 tipc_get_gap_ack_blks(struct tipc_gap_ack_blks **ga, struct tipc_link *l,
1384 			  struct tipc_msg *hdr, bool uc)
1385 {
1386 	struct tipc_gap_ack_blks *p;
1387 	u16 sz = 0;
1388 
1389 	/* Does peer support the Gap ACK blocks feature? */
1390 	if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
1391 		p = (struct tipc_gap_ack_blks *)msg_data(hdr);
1392 		sz = ntohs(p->len);
1393 		/* Sanity check */
1394 		if (sz == struct_size(p, gacks, p->ugack_cnt + p->bgack_cnt)) {
1395 			/* Good, check if the desired type exists */
1396 			if ((uc && p->ugack_cnt) || (!uc && p->bgack_cnt))
1397 				goto ok;
1398 		/* Backward compatible: peer might not support bc, but uc? */
1399 		} else if (uc && sz == struct_size(p, gacks, p->ugack_cnt)) {
1400 			if (p->ugack_cnt) {
1401 				p->bgack_cnt = 0;
1402 				goto ok;
1403 			}
1404 		}
1405 	}
1406 	/* Other cases: ignore! */
1407 	p = NULL;
1408 
1409 ok:
1410 	*ga = p;
1411 	return sz;
1412 }
1413 
1414 static u8 __tipc_build_gap_ack_blks(struct tipc_gap_ack_blks *ga,
1415 				    struct tipc_link *l, u8 start_index)
1416 {
1417 	struct tipc_gap_ack *gacks = &ga->gacks[start_index];
1418 	struct sk_buff *skb = skb_peek(&l->deferdq);
1419 	u16 expect, seqno = 0;
1420 	u8 n = 0;
1421 
1422 	if (!skb)
1423 		return 0;
1424 
1425 	expect = buf_seqno(skb);
1426 	skb_queue_walk(&l->deferdq, skb) {
1427 		seqno = buf_seqno(skb);
1428 		if (unlikely(more(seqno, expect))) {
1429 			gacks[n].ack = htons(expect - 1);
1430 			gacks[n].gap = htons(seqno - expect);
1431 			if (++n >= MAX_GAP_ACK_BLKS / 2) {
1432 				pr_info_ratelimited("Gacks on %s: %d, ql: %d!\n",
1433 						    l->name, n,
1434 						    skb_queue_len(&l->deferdq));
1435 				return n;
1436 			}
1437 		} else if (unlikely(less(seqno, expect))) {
1438 			pr_warn("Unexpected skb in deferdq!\n");
1439 			continue;
1440 		}
1441 		expect = seqno + 1;
1442 	}
1443 
1444 	/* last block */
1445 	gacks[n].ack = htons(seqno);
1446 	gacks[n].gap = 0;
1447 	n++;
1448 	return n;
1449 }
1450 
1451 /* tipc_build_gap_ack_blks - build Gap ACK blocks
1452  * @l: tipc unicast link
1453  * @hdr: the tipc message buffer to store the Gap ACK blocks after built
1454  *
1455  * The function builds Gap ACK blocks for both the unicast & broadcast receiver
1456  * links of a certain peer, the buffer after built has the network data format
1457  * as found at the struct tipc_gap_ack_blks definition.
1458  *
1459  * returns the actual allocated memory size
1460  */
1461 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, struct tipc_msg *hdr)
1462 {
1463 	struct tipc_link *bcl = l->bc_rcvlink;
1464 	struct tipc_gap_ack_blks *ga;
1465 	u16 len;
1466 
1467 	ga = (struct tipc_gap_ack_blks *)msg_data(hdr);
1468 
1469 	/* Start with broadcast link first */
1470 	tipc_bcast_lock(bcl->net);
1471 	msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
1472 	msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
1473 	ga->bgack_cnt = __tipc_build_gap_ack_blks(ga, bcl, 0);
1474 	tipc_bcast_unlock(bcl->net);
1475 
1476 	/* Now for unicast link, but an explicit NACK only (???) */
1477 	ga->ugack_cnt = (msg_seq_gap(hdr)) ?
1478 			__tipc_build_gap_ack_blks(ga, l, ga->bgack_cnt) : 0;
1479 
1480 	/* Total len */
1481 	len = struct_size(ga, gacks, ga->bgack_cnt + ga->ugack_cnt);
1482 	ga->len = htons(len);
1483 	return len;
1484 }
1485 
1486 /* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
1487  *			       acked packets, also doing retransmissions if
1488  *			       gaps found
1489  * @l: tipc link with transmq queue to be advanced
1490  * @r: tipc link "receiver" i.e. in case of broadcast (= "l" if unicast)
1491  * @acked: seqno of last packet acked by peer without any gaps before
1492  * @gap: # of gap packets
1493  * @ga: buffer pointer to Gap ACK blocks from peer
1494  * @xmitq: queue for accumulating the retransmitted packets if any
1495  * @retransmitted: returned boolean value if a retransmission is really issued
1496  * @rc: returned code e.g. TIPC_LINK_DOWN_EVT if a repeated retransmit failures
1497  *      happens (- unlikely case)
1498  *
1499  * Return: the number of packets released from the link transmq
1500  */
1501 static int tipc_link_advance_transmq(struct tipc_link *l, struct tipc_link *r,
1502 				     u16 acked, u16 gap,
1503 				     struct tipc_gap_ack_blks *ga,
1504 				     struct sk_buff_head *xmitq,
1505 				     bool *retransmitted, int *rc)
1506 {
1507 	struct tipc_gap_ack_blks *last_ga = r->last_ga, *this_ga = NULL;
1508 	struct tipc_gap_ack *gacks = NULL;
1509 	struct sk_buff *skb, *_skb, *tmp;
1510 	struct tipc_msg *hdr;
1511 	u32 qlen = skb_queue_len(&l->transmq);
1512 	u16 nacked = acked, ngap = gap, gack_cnt = 0;
1513 	u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1514 	u16 ack = l->rcv_nxt - 1;
1515 	u16 seqno, n = 0;
1516 	u16 end = r->acked, start = end, offset = r->last_gap;
1517 	u16 si = (last_ga) ? last_ga->start_index : 0;
1518 	bool is_uc = !link_is_bc_sndlink(l);
1519 	bool bc_has_acked = false;
1520 
1521 	trace_tipc_link_retrans(r, acked + 1, acked + gap, &l->transmq);
1522 
1523 	/* Determine Gap ACK blocks if any for the particular link */
1524 	if (ga && is_uc) {
1525 		/* Get the Gap ACKs, uc part */
1526 		gack_cnt = ga->ugack_cnt;
1527 		gacks = &ga->gacks[ga->bgack_cnt];
1528 	} else if (ga) {
1529 		/* Copy the Gap ACKs, bc part, for later renewal if needed */
1530 		this_ga = kmemdup(ga, struct_size(ga, gacks, ga->bgack_cnt),
1531 				  GFP_ATOMIC);
1532 		if (likely(this_ga)) {
1533 			this_ga->start_index = 0;
1534 			/* Start with the bc Gap ACKs */
1535 			gack_cnt = this_ga->bgack_cnt;
1536 			gacks = &this_ga->gacks[0];
1537 		} else {
1538 			/* Hmm, we can get in trouble..., simply ignore it */
1539 			pr_warn_ratelimited("Ignoring bc Gap ACKs, no memory\n");
1540 		}
1541 	}
1542 
1543 	/* Advance the link transmq */
1544 	skb_queue_walk_safe(&l->transmq, skb, tmp) {
1545 		seqno = buf_seqno(skb);
1546 
1547 next_gap_ack:
1548 		if (less_eq(seqno, nacked)) {
1549 			if (is_uc)
1550 				goto release;
1551 			/* Skip packets peer has already acked */
1552 			if (!more(seqno, r->acked))
1553 				continue;
1554 			/* Get the next of last Gap ACK blocks */
1555 			while (more(seqno, end)) {
1556 				if (!last_ga || si >= last_ga->bgack_cnt)
1557 					break;
1558 				start = end + offset + 1;
1559 				end = ntohs(last_ga->gacks[si].ack);
1560 				offset = ntohs(last_ga->gacks[si].gap);
1561 				si++;
1562 				WARN_ONCE(more(start, end) ||
1563 					  (!offset &&
1564 					   si < last_ga->bgack_cnt) ||
1565 					  si > MAX_GAP_ACK_BLKS,
1566 					  "Corrupted Gap ACK: %d %d %d %d %d\n",
1567 					  start, end, offset, si,
1568 					  last_ga->bgack_cnt);
1569 			}
1570 			/* Check against the last Gap ACK block */
1571 			if (in_range(seqno, start, end))
1572 				continue;
1573 			/* Update/release the packet peer is acking */
1574 			bc_has_acked = true;
1575 			if (--TIPC_SKB_CB(skb)->ackers)
1576 				continue;
1577 release:
1578 			/* release skb */
1579 			__skb_unlink(skb, &l->transmq);
1580 			kfree_skb(skb);
1581 		} else if (less_eq(seqno, nacked + ngap)) {
1582 			/* First gap: check if repeated retrans failures? */
1583 			if (unlikely(seqno == acked + 1 &&
1584 				     link_retransmit_failure(l, r, rc))) {
1585 				/* Ignore this bc Gap ACKs if any */
1586 				kfree(this_ga);
1587 				this_ga = NULL;
1588 				break;
1589 			}
1590 			/* retransmit skb if unrestricted*/
1591 			if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1592 				continue;
1593 			tipc_link_set_skb_retransmit_time(skb, l);
1594 			_skb = pskb_copy(skb, GFP_ATOMIC);
1595 			if (!_skb)
1596 				continue;
1597 			hdr = buf_msg(_skb);
1598 			msg_set_ack(hdr, ack);
1599 			msg_set_bcast_ack(hdr, bc_ack);
1600 			_skb->priority = TC_PRIO_CONTROL;
1601 			__skb_queue_tail(xmitq, _skb);
1602 			l->stats.retransmitted++;
1603 			if (!is_uc)
1604 				r->stats.retransmitted++;
1605 			*retransmitted = true;
1606 			/* Increase actual retrans counter & mark first time */
1607 			if (!TIPC_SKB_CB(skb)->retr_cnt++)
1608 				TIPC_SKB_CB(skb)->retr_stamp = jiffies;
1609 		} else {
1610 			/* retry with Gap ACK blocks if any */
1611 			if (n >= gack_cnt)
1612 				break;
1613 			nacked = ntohs(gacks[n].ack);
1614 			ngap = ntohs(gacks[n].gap);
1615 			n++;
1616 			goto next_gap_ack;
1617 		}
1618 	}
1619 
1620 	/* Renew last Gap ACK blocks for bc if needed */
1621 	if (bc_has_acked) {
1622 		if (this_ga) {
1623 			kfree(last_ga);
1624 			r->last_ga = this_ga;
1625 			r->last_gap = gap;
1626 		} else if (last_ga) {
1627 			if (less(acked, start)) {
1628 				si--;
1629 				offset = start - acked - 1;
1630 			} else if (less(acked, end)) {
1631 				acked = end;
1632 			}
1633 			if (si < last_ga->bgack_cnt) {
1634 				last_ga->start_index = si;
1635 				r->last_gap = offset;
1636 			} else {
1637 				kfree(last_ga);
1638 				r->last_ga = NULL;
1639 				r->last_gap = 0;
1640 			}
1641 		} else {
1642 			r->last_gap = 0;
1643 		}
1644 		r->acked = acked;
1645 	} else {
1646 		kfree(this_ga);
1647 	}
1648 
1649 	return qlen - skb_queue_len(&l->transmq);
1650 }
1651 
1652 /* tipc_link_build_state_msg: prepare link state message for transmission
1653  *
1654  * Note that sending of broadcast ack is coordinated among nodes, to reduce
1655  * risk of ack storms towards the sender
1656  */
1657 int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1658 {
1659 	if (!l)
1660 		return 0;
1661 
1662 	/* Broadcast ACK must be sent via a unicast link => defer to caller */
1663 	if (link_is_bc_rcvlink(l)) {
1664 		if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
1665 			return 0;
1666 		l->rcv_unacked = 0;
1667 
1668 		/* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
1669 		l->snd_nxt = l->rcv_nxt;
1670 		return TIPC_LINK_SND_STATE;
1671 	}
1672 	/* Unicast ACK */
1673 	l->rcv_unacked = 0;
1674 	l->stats.sent_acks++;
1675 	tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
1676 	return 0;
1677 }
1678 
1679 /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
1680  */
1681 void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1682 {
1683 	int mtyp = RESET_MSG;
1684 	struct sk_buff *skb;
1685 
1686 	if (l->state == LINK_ESTABLISHING)
1687 		mtyp = ACTIVATE_MSG;
1688 
1689 	tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
1690 
1691 	/* Inform peer that this endpoint is going down if applicable */
1692 	skb = skb_peek_tail(xmitq);
1693 	if (skb && (l->state == LINK_RESET))
1694 		msg_set_peer_stopping(buf_msg(skb), 1);
1695 }
1696 
1697 /* tipc_link_build_nack_msg: prepare link nack message for transmission
1698  * Note that sending of broadcast NACK is coordinated among nodes, to
1699  * reduce the risk of NACK storms towards the sender
1700  */
1701 static int tipc_link_build_nack_msg(struct tipc_link *l,
1702 				    struct sk_buff_head *xmitq)
1703 {
1704 	u32 def_cnt = ++l->stats.deferred_recv;
1705 	struct sk_buff_head *dfq = &l->deferdq;
1706 	u32 defq_len = skb_queue_len(dfq);
1707 	int match1, match2;
1708 
1709 	if (link_is_bc_rcvlink(l)) {
1710 		match1 = def_cnt & 0xf;
1711 		match2 = tipc_own_addr(l->net) & 0xf;
1712 		if (match1 == match2)
1713 			return TIPC_LINK_SND_STATE;
1714 		return 0;
1715 	}
1716 
1717 	if (defq_len >= 3 && !((defq_len - 3) % 16)) {
1718 		u16 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1719 
1720 		tipc_link_build_proto_msg(l, STATE_MSG, 0, 0,
1721 					  rcvgap, 0, 0, xmitq);
1722 	}
1723 	return 0;
1724 }
1725 
1726 /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
1727  * @l: the link that should handle the message
1728  * @skb: TIPC packet
1729  * @xmitq: queue to place packets to be sent after this call
1730  */
1731 int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1732 		  struct sk_buff_head *xmitq)
1733 {
1734 	struct sk_buff_head *defq = &l->deferdq;
1735 	struct tipc_msg *hdr = buf_msg(skb);
1736 	u16 seqno, rcv_nxt, win_lim;
1737 	int released = 0;
1738 	int rc = 0;
1739 
1740 	/* Verify and update link state */
1741 	if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
1742 		return tipc_link_proto_rcv(l, skb, xmitq);
1743 
1744 	/* Don't send probe at next timeout expiration */
1745 	l->silent_intv_cnt = 0;
1746 
1747 	do {
1748 		hdr = buf_msg(skb);
1749 		seqno = msg_seqno(hdr);
1750 		rcv_nxt = l->rcv_nxt;
1751 		win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
1752 
1753 		if (unlikely(!link_is_up(l))) {
1754 			if (l->state == LINK_ESTABLISHING)
1755 				rc = TIPC_LINK_UP_EVT;
1756 			kfree_skb(skb);
1757 			break;
1758 		}
1759 
1760 		/* Drop if outside receive window */
1761 		if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
1762 			l->stats.duplicates++;
1763 			kfree_skb(skb);
1764 			break;
1765 		}
1766 		released += tipc_link_advance_transmq(l, l, msg_ack(hdr), 0,
1767 						      NULL, NULL, NULL, NULL);
1768 
1769 		/* Defer delivery if sequence gap */
1770 		if (unlikely(seqno != rcv_nxt)) {
1771 			if (!__tipc_skb_queue_sorted(defq, seqno, skb))
1772 				l->stats.duplicates++;
1773 			rc |= tipc_link_build_nack_msg(l, xmitq);
1774 			break;
1775 		}
1776 
1777 		/* Deliver packet */
1778 		l->rcv_nxt++;
1779 		l->stats.recv_pkts++;
1780 
1781 		if (unlikely(msg_user(hdr) == TUNNEL_PROTOCOL))
1782 			rc |= tipc_link_tnl_rcv(l, skb, l->inputq);
1783 		else if (!tipc_data_input(l, skb, l->inputq))
1784 			rc |= tipc_link_input(l, skb, l->inputq, &l->reasm_buf);
1785 		if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
1786 			rc |= tipc_link_build_state_msg(l, xmitq);
1787 		if (unlikely(rc & ~TIPC_LINK_SND_STATE))
1788 			break;
1789 	} while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
1790 
1791 	/* Forward queues and wake up waiting users */
1792 	if (released) {
1793 		tipc_link_update_cwin(l, released, 0);
1794 		tipc_link_advance_backlog(l, xmitq);
1795 		if (unlikely(!skb_queue_empty(&l->wakeupq)))
1796 			link_prepare_wakeup(l);
1797 	}
1798 	return rc;
1799 }
1800 
1801 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
1802 				      bool probe_reply, u16 rcvgap,
1803 				      int tolerance, int priority,
1804 				      struct sk_buff_head *xmitq)
1805 {
1806 	struct tipc_mon_state *mstate = &l->mon_state;
1807 	struct sk_buff_head *dfq = &l->deferdq;
1808 	struct tipc_link *bcl = l->bc_rcvlink;
1809 	struct tipc_msg *hdr;
1810 	struct sk_buff *skb;
1811 	bool node_up = link_is_up(bcl);
1812 	u16 glen = 0, bc_rcvgap = 0;
1813 	int dlen = 0;
1814 	void *data;
1815 
1816 	/* Don't send protocol message during reset or link failover */
1817 	if (tipc_link_is_blocked(l))
1818 		return;
1819 
1820 	if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
1821 		return;
1822 
1823 	if ((probe || probe_reply) && !skb_queue_empty(dfq))
1824 		rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1825 
1826 	skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
1827 			      tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
1828 			      l->addr, tipc_own_addr(l->net), 0, 0, 0);
1829 	if (!skb)
1830 		return;
1831 
1832 	hdr = buf_msg(skb);
1833 	data = msg_data(hdr);
1834 	msg_set_session(hdr, l->session);
1835 	msg_set_bearer_id(hdr, l->bearer_id);
1836 	msg_set_net_plane(hdr, l->net_plane);
1837 	msg_set_next_sent(hdr, l->snd_nxt);
1838 	msg_set_ack(hdr, l->rcv_nxt - 1);
1839 	msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
1840 	msg_set_bc_ack_invalid(hdr, !node_up);
1841 	msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
1842 	msg_set_link_tolerance(hdr, tolerance);
1843 	msg_set_linkprio(hdr, priority);
1844 	msg_set_redundant_link(hdr, node_up);
1845 	msg_set_seq_gap(hdr, 0);
1846 	msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
1847 
1848 	if (mtyp == STATE_MSG) {
1849 		if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
1850 			msg_set_seqno(hdr, l->snd_nxt_state++);
1851 		msg_set_seq_gap(hdr, rcvgap);
1852 		bc_rcvgap = link_bc_rcv_gap(bcl);
1853 		msg_set_bc_gap(hdr, bc_rcvgap);
1854 		msg_set_probe(hdr, probe);
1855 		msg_set_is_keepalive(hdr, probe || probe_reply);
1856 		if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
1857 			glen = tipc_build_gap_ack_blks(l, hdr);
1858 		tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
1859 		msg_set_size(hdr, INT_H_SIZE + glen + dlen);
1860 		skb_trim(skb, INT_H_SIZE + glen + dlen);
1861 		l->stats.sent_states++;
1862 		l->rcv_unacked = 0;
1863 	} else {
1864 		/* RESET_MSG or ACTIVATE_MSG */
1865 		if (mtyp == ACTIVATE_MSG) {
1866 			msg_set_dest_session_valid(hdr, 1);
1867 			msg_set_dest_session(hdr, l->peer_session);
1868 		}
1869 		msg_set_max_pkt(hdr, l->advertised_mtu);
1870 		strcpy(data, l->if_name);
1871 		msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
1872 		skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
1873 	}
1874 	if (probe)
1875 		l->stats.sent_probes++;
1876 	if (rcvgap)
1877 		l->stats.sent_nacks++;
1878 	if (bc_rcvgap)
1879 		bcl->stats.sent_nacks++;
1880 	skb->priority = TC_PRIO_CONTROL;
1881 	__skb_queue_tail(xmitq, skb);
1882 	trace_tipc_proto_build(skb, false, l->name);
1883 }
1884 
1885 void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
1886 				    struct sk_buff_head *xmitq)
1887 {
1888 	u32 onode = tipc_own_addr(l->net);
1889 	struct tipc_msg *hdr, *ihdr;
1890 	struct sk_buff_head tnlq;
1891 	struct sk_buff *skb;
1892 	u32 dnode = l->addr;
1893 
1894 	__skb_queue_head_init(&tnlq);
1895 	skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
1896 			      INT_H_SIZE, BASIC_H_SIZE,
1897 			      dnode, onode, 0, 0, 0);
1898 	if (!skb) {
1899 		pr_warn("%sunable to create tunnel packet\n", link_co_err);
1900 		return;
1901 	}
1902 
1903 	hdr = buf_msg(skb);
1904 	msg_set_msgcnt(hdr, 1);
1905 	msg_set_bearer_id(hdr, l->peer_bearer_id);
1906 
1907 	ihdr = (struct tipc_msg *)msg_data(hdr);
1908 	tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1909 		      BASIC_H_SIZE, dnode);
1910 	msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
1911 	__skb_queue_tail(&tnlq, skb);
1912 	tipc_link_xmit(l, &tnlq, xmitq);
1913 }
1914 
1915 /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
1916  * with contents of the link's transmit and backlog queues.
1917  */
1918 void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1919 			   int mtyp, struct sk_buff_head *xmitq)
1920 {
1921 	struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1922 	struct sk_buff *skb, *tnlskb;
1923 	struct tipc_msg *hdr, tnlhdr;
1924 	struct sk_buff_head *queue = &l->transmq;
1925 	struct sk_buff_head tmpxq, tnlq, frags;
1926 	u16 pktlen, pktcnt, seqno = l->snd_nxt;
1927 	bool pktcnt_need_update = false;
1928 	u16 syncpt;
1929 	int rc;
1930 
1931 	if (!tnl)
1932 		return;
1933 
1934 	__skb_queue_head_init(&tnlq);
1935 	/* Link Synching:
1936 	 * From now on, send only one single ("dummy") SYNCH message
1937 	 * to peer. The SYNCH message does not contain any data, just
1938 	 * a header conveying the synch point to the peer.
1939 	 */
1940 	if (mtyp == SYNCH_MSG && (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1941 		tnlskb = tipc_msg_create(TUNNEL_PROTOCOL, SYNCH_MSG,
1942 					 INT_H_SIZE, 0, l->addr,
1943 					 tipc_own_addr(l->net),
1944 					 0, 0, 0);
1945 		if (!tnlskb) {
1946 			pr_warn("%sunable to create dummy SYNCH_MSG\n",
1947 				link_co_err);
1948 			return;
1949 		}
1950 
1951 		hdr = buf_msg(tnlskb);
1952 		syncpt = l->snd_nxt + skb_queue_len(&l->backlogq) - 1;
1953 		msg_set_syncpt(hdr, syncpt);
1954 		msg_set_bearer_id(hdr, l->peer_bearer_id);
1955 		__skb_queue_tail(&tnlq, tnlskb);
1956 		tipc_link_xmit(tnl, &tnlq, xmitq);
1957 		return;
1958 	}
1959 
1960 	__skb_queue_head_init(&tmpxq);
1961 	__skb_queue_head_init(&frags);
1962 	/* At least one packet required for safe algorithm => add dummy */
1963 	skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1964 			      BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
1965 			      0, 0, TIPC_ERR_NO_PORT);
1966 	if (!skb) {
1967 		pr_warn("%sunable to create tunnel packet\n", link_co_err);
1968 		return;
1969 	}
1970 	__skb_queue_tail(&tnlq, skb);
1971 	tipc_link_xmit(l, &tnlq, &tmpxq);
1972 	__skb_queue_purge(&tmpxq);
1973 
1974 	/* Initialize reusable tunnel packet header */
1975 	tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
1976 		      mtyp, INT_H_SIZE, l->addr);
1977 	if (mtyp == SYNCH_MSG)
1978 		pktcnt = l->snd_nxt - buf_seqno(skb_peek(&l->transmq));
1979 	else
1980 		pktcnt = skb_queue_len(&l->transmq);
1981 	pktcnt += skb_queue_len(&l->backlogq);
1982 	msg_set_msgcnt(&tnlhdr, pktcnt);
1983 	msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1984 tnl:
1985 	/* Wrap each packet into a tunnel packet */
1986 	skb_queue_walk(queue, skb) {
1987 		hdr = buf_msg(skb);
1988 		if (queue == &l->backlogq)
1989 			msg_set_seqno(hdr, seqno++);
1990 		pktlen = msg_size(hdr);
1991 
1992 		/* Tunnel link MTU is not large enough? This could be
1993 		 * due to:
1994 		 * 1) Link MTU has just changed or set differently;
1995 		 * 2) Or FAILOVER on the top of a SYNCH message
1996 		 *
1997 		 * The 2nd case should not happen if peer supports
1998 		 * TIPC_TUNNEL_ENHANCED
1999 		 */
2000 		if (pktlen > tnl->mtu - INT_H_SIZE) {
2001 			if (mtyp == FAILOVER_MSG &&
2002 			    (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
2003 				rc = tipc_msg_fragment(skb, &tnlhdr, tnl->mtu,
2004 						       &frags);
2005 				if (rc) {
2006 					pr_warn("%sunable to frag msg: rc %d\n",
2007 						link_co_err, rc);
2008 					return;
2009 				}
2010 				pktcnt += skb_queue_len(&frags) - 1;
2011 				pktcnt_need_update = true;
2012 				skb_queue_splice_tail_init(&frags, &tnlq);
2013 				continue;
2014 			}
2015 			/* Unluckily, peer doesn't have TIPC_TUNNEL_ENHANCED
2016 			 * => Just warn it and return!
2017 			 */
2018 			pr_warn_ratelimited("%stoo large msg <%d, %d>: %d!\n",
2019 					    link_co_err, msg_user(hdr),
2020 					    msg_type(hdr), msg_size(hdr));
2021 			return;
2022 		}
2023 
2024 		msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
2025 		tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
2026 		if (!tnlskb) {
2027 			pr_warn("%sunable to send packet\n", link_co_err);
2028 			return;
2029 		}
2030 		skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
2031 		skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
2032 		__skb_queue_tail(&tnlq, tnlskb);
2033 	}
2034 	if (queue != &l->backlogq) {
2035 		queue = &l->backlogq;
2036 		goto tnl;
2037 	}
2038 
2039 	if (pktcnt_need_update)
2040 		skb_queue_walk(&tnlq, skb) {
2041 			hdr = buf_msg(skb);
2042 			msg_set_msgcnt(hdr, pktcnt);
2043 		}
2044 
2045 	tipc_link_xmit(tnl, &tnlq, xmitq);
2046 
2047 	if (mtyp == FAILOVER_MSG) {
2048 		tnl->drop_point = l->rcv_nxt;
2049 		tnl->failover_reasm_skb = l->reasm_buf;
2050 		l->reasm_buf = NULL;
2051 
2052 		/* Failover the link's deferdq */
2053 		if (unlikely(!skb_queue_empty(fdefq))) {
2054 			pr_warn("Link failover deferdq not empty: %d!\n",
2055 				skb_queue_len(fdefq));
2056 			__skb_queue_purge(fdefq);
2057 		}
2058 		skb_queue_splice_init(&l->deferdq, fdefq);
2059 	}
2060 }
2061 
2062 /**
2063  * tipc_link_failover_prepare() - prepare tnl for link failover
2064  *
2065  * This is a special version of the precursor - tipc_link_tnl_prepare(),
2066  * see the tipc_node_link_failover() for details
2067  *
2068  * @l: failover link
2069  * @tnl: tunnel link
2070  * @xmitq: queue for messages to be xmited
2071  */
2072 void tipc_link_failover_prepare(struct tipc_link *l, struct tipc_link *tnl,
2073 				struct sk_buff_head *xmitq)
2074 {
2075 	struct sk_buff_head *fdefq = &tnl->failover_deferdq;
2076 
2077 	tipc_link_create_dummy_tnl_msg(tnl, xmitq);
2078 
2079 	/* This failover link endpoint was never established before,
2080 	 * so it has not received anything from peer.
2081 	 * Otherwise, it must be a normal failover situation or the
2082 	 * node has entered SELF_DOWN_PEER_LEAVING and both peer nodes
2083 	 * would have to start over from scratch instead.
2084 	 */
2085 	tnl->drop_point = 1;
2086 	tnl->failover_reasm_skb = NULL;
2087 
2088 	/* Initiate the link's failover deferdq */
2089 	if (unlikely(!skb_queue_empty(fdefq))) {
2090 		pr_warn("Link failover deferdq not empty: %d!\n",
2091 			skb_queue_len(fdefq));
2092 		__skb_queue_purge(fdefq);
2093 	}
2094 }
2095 
2096 /* tipc_link_validate_msg(): validate message against current link state
2097  * Returns true if message should be accepted, otherwise false
2098  */
2099 bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
2100 {
2101 	u16 curr_session = l->peer_session;
2102 	u16 session = msg_session(hdr);
2103 	int mtyp = msg_type(hdr);
2104 
2105 	if (msg_user(hdr) != LINK_PROTOCOL)
2106 		return true;
2107 
2108 	switch (mtyp) {
2109 	case RESET_MSG:
2110 		if (!l->in_session)
2111 			return true;
2112 		/* Accept only RESET with new session number */
2113 		return more(session, curr_session);
2114 	case ACTIVATE_MSG:
2115 		if (!l->in_session)
2116 			return true;
2117 		/* Accept only ACTIVATE with new or current session number */
2118 		return !less(session, curr_session);
2119 	case STATE_MSG:
2120 		/* Accept only STATE with current session number */
2121 		if (!l->in_session)
2122 			return false;
2123 		if (session != curr_session)
2124 			return false;
2125 		/* Extra sanity check */
2126 		if (!link_is_up(l) && msg_ack(hdr))
2127 			return false;
2128 		if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
2129 			return true;
2130 		/* Accept only STATE with new sequence number */
2131 		return !less(msg_seqno(hdr), l->rcv_nxt_state);
2132 	default:
2133 		return false;
2134 	}
2135 }
2136 
2137 /* tipc_link_proto_rcv(): receive link level protocol message :
2138  * Note that network plane id propagates through the network, and may
2139  * change at any time. The node with lowest numerical id determines
2140  * network plane
2141  */
2142 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
2143 			       struct sk_buff_head *xmitq)
2144 {
2145 	struct tipc_msg *hdr = buf_msg(skb);
2146 	struct tipc_gap_ack_blks *ga = NULL;
2147 	bool reply = msg_probe(hdr), retransmitted = false;
2148 	u16 dlen = msg_data_sz(hdr), glen = 0;
2149 	u16 peers_snd_nxt =  msg_next_sent(hdr);
2150 	u16 peers_tol = msg_link_tolerance(hdr);
2151 	u16 peers_prio = msg_linkprio(hdr);
2152 	u16 gap = msg_seq_gap(hdr);
2153 	u16 ack = msg_ack(hdr);
2154 	u16 rcv_nxt = l->rcv_nxt;
2155 	u16 rcvgap = 0;
2156 	int mtyp = msg_type(hdr);
2157 	int rc = 0, released;
2158 	char *if_name;
2159 	void *data;
2160 
2161 	trace_tipc_proto_rcv(skb, false, l->name);
2162 	if (tipc_link_is_blocked(l) || !xmitq)
2163 		goto exit;
2164 
2165 	if (tipc_own_addr(l->net) > msg_prevnode(hdr))
2166 		l->net_plane = msg_net_plane(hdr);
2167 
2168 	skb_linearize(skb);
2169 	hdr = buf_msg(skb);
2170 	data = msg_data(hdr);
2171 
2172 	if (!tipc_link_validate_msg(l, hdr)) {
2173 		trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
2174 		trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
2175 		goto exit;
2176 	}
2177 
2178 	switch (mtyp) {
2179 	case RESET_MSG:
2180 	case ACTIVATE_MSG:
2181 		/* Complete own link name with peer's interface name */
2182 		if_name =  strrchr(l->name, ':') + 1;
2183 		if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
2184 			break;
2185 		if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
2186 			break;
2187 		strncpy(if_name, data, TIPC_MAX_IF_NAME);
2188 
2189 		/* Update own tolerance if peer indicates a non-zero value */
2190 		if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
2191 			l->tolerance = peers_tol;
2192 			l->bc_rcvlink->tolerance = peers_tol;
2193 		}
2194 		/* Update own priority if peer's priority is higher */
2195 		if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
2196 			l->priority = peers_prio;
2197 
2198 		/* If peer is going down we want full re-establish cycle */
2199 		if (msg_peer_stopping(hdr)) {
2200 			rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2201 			break;
2202 		}
2203 
2204 		/* If this endpoint was re-created while peer was ESTABLISHING
2205 		 * it doesn't know current session number. Force re-synch.
2206 		 */
2207 		if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
2208 		    l->session != msg_dest_session(hdr)) {
2209 			if (less(l->session, msg_dest_session(hdr)))
2210 				l->session = msg_dest_session(hdr) + 1;
2211 			break;
2212 		}
2213 
2214 		/* ACTIVATE_MSG serves as PEER_RESET if link is already down */
2215 		if (mtyp == RESET_MSG || !link_is_up(l))
2216 			rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
2217 
2218 		/* ACTIVATE_MSG takes up link if it was already locally reset */
2219 		if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
2220 			rc = TIPC_LINK_UP_EVT;
2221 
2222 		l->peer_session = msg_session(hdr);
2223 		l->in_session = true;
2224 		l->peer_bearer_id = msg_bearer_id(hdr);
2225 		if (l->mtu > msg_max_pkt(hdr))
2226 			l->mtu = msg_max_pkt(hdr);
2227 		break;
2228 
2229 	case STATE_MSG:
2230 		l->rcv_nxt_state = msg_seqno(hdr) + 1;
2231 
2232 		/* Update own tolerance if peer indicates a non-zero value */
2233 		if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
2234 			l->tolerance = peers_tol;
2235 			l->bc_rcvlink->tolerance = peers_tol;
2236 		}
2237 		/* Update own prio if peer indicates a different value */
2238 		if ((peers_prio != l->priority) &&
2239 		    in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
2240 			l->priority = peers_prio;
2241 			rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2242 		}
2243 
2244 		l->silent_intv_cnt = 0;
2245 		l->stats.recv_states++;
2246 		if (msg_probe(hdr))
2247 			l->stats.recv_probes++;
2248 
2249 		if (!link_is_up(l)) {
2250 			if (l->state == LINK_ESTABLISHING)
2251 				rc = TIPC_LINK_UP_EVT;
2252 			break;
2253 		}
2254 
2255 		/* Receive Gap ACK blocks from peer if any */
2256 		glen = tipc_get_gap_ack_blks(&ga, l, hdr, true);
2257 
2258 		tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
2259 			     &l->mon_state, l->bearer_id);
2260 
2261 		/* Send NACK if peer has sent pkts we haven't received yet */
2262 		if ((reply || msg_is_keepalive(hdr)) &&
2263 		    more(peers_snd_nxt, rcv_nxt) &&
2264 		    !tipc_link_is_synching(l) &&
2265 		    skb_queue_empty(&l->deferdq))
2266 			rcvgap = peers_snd_nxt - l->rcv_nxt;
2267 		if (rcvgap || reply)
2268 			tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
2269 						  rcvgap, 0, 0, xmitq);
2270 
2271 		released = tipc_link_advance_transmq(l, l, ack, gap, ga, xmitq,
2272 						     &retransmitted, &rc);
2273 		if (gap)
2274 			l->stats.recv_nacks++;
2275 		if (released || retransmitted)
2276 			tipc_link_update_cwin(l, released, retransmitted);
2277 		if (released)
2278 			tipc_link_advance_backlog(l, xmitq);
2279 		if (unlikely(!skb_queue_empty(&l->wakeupq)))
2280 			link_prepare_wakeup(l);
2281 	}
2282 exit:
2283 	kfree_skb(skb);
2284 	return rc;
2285 }
2286 
2287 /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
2288  */
2289 static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
2290 					 u16 peers_snd_nxt,
2291 					 struct sk_buff_head *xmitq)
2292 {
2293 	struct sk_buff *skb;
2294 	struct tipc_msg *hdr;
2295 	struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
2296 	u16 ack = l->rcv_nxt - 1;
2297 	u16 gap_to = peers_snd_nxt - 1;
2298 
2299 	skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
2300 			      0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
2301 	if (!skb)
2302 		return false;
2303 	hdr = buf_msg(skb);
2304 	msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
2305 	msg_set_bcast_ack(hdr, ack);
2306 	msg_set_bcgap_after(hdr, ack);
2307 	if (dfrd_skb)
2308 		gap_to = buf_seqno(dfrd_skb) - 1;
2309 	msg_set_bcgap_to(hdr, gap_to);
2310 	msg_set_non_seq(hdr, bcast);
2311 	__skb_queue_tail(xmitq, skb);
2312 	return true;
2313 }
2314 
2315 /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
2316  *
2317  * Give a newly added peer node the sequence number where it should
2318  * start receiving and acking broadcast packets.
2319  */
2320 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
2321 					struct sk_buff_head *xmitq)
2322 {
2323 	struct sk_buff_head list;
2324 
2325 	__skb_queue_head_init(&list);
2326 	if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
2327 		return;
2328 	msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
2329 	tipc_link_xmit(l, &list, xmitq);
2330 }
2331 
2332 /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
2333  */
2334 void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
2335 {
2336 	int mtyp = msg_type(hdr);
2337 	u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2338 
2339 	if (link_is_up(l))
2340 		return;
2341 
2342 	if (msg_user(hdr) == BCAST_PROTOCOL) {
2343 		l->rcv_nxt = peers_snd_nxt;
2344 		l->state = LINK_ESTABLISHED;
2345 		return;
2346 	}
2347 
2348 	if (l->peer_caps & TIPC_BCAST_SYNCH)
2349 		return;
2350 
2351 	if (msg_peer_node_is_up(hdr))
2352 		return;
2353 
2354 	/* Compatibility: accept older, less safe initial synch data */
2355 	if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
2356 		l->rcv_nxt = peers_snd_nxt;
2357 }
2358 
2359 /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
2360  */
2361 int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
2362 			  struct sk_buff_head *xmitq)
2363 {
2364 	u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2365 	int rc = 0;
2366 
2367 	if (!link_is_up(l))
2368 		return rc;
2369 
2370 	if (!msg_peer_node_is_up(hdr))
2371 		return rc;
2372 
2373 	/* Open when peer ackowledges our bcast init msg (pkt #1) */
2374 	if (msg_ack(hdr))
2375 		l->bc_peer_is_up = true;
2376 
2377 	if (!l->bc_peer_is_up)
2378 		return rc;
2379 
2380 	/* Ignore if peers_snd_nxt goes beyond receive window */
2381 	if (more(peers_snd_nxt, l->rcv_nxt + l->window))
2382 		return rc;
2383 
2384 	l->snd_nxt = peers_snd_nxt;
2385 	if (link_bc_rcv_gap(l))
2386 		rc |= TIPC_LINK_SND_STATE;
2387 
2388 	/* Return now if sender supports nack via STATE messages */
2389 	if (l->peer_caps & TIPC_BCAST_STATE_NACK)
2390 		return rc;
2391 
2392 	/* Otherwise, be backwards compatible */
2393 
2394 	if (!more(peers_snd_nxt, l->rcv_nxt)) {
2395 		l->nack_state = BC_NACK_SND_CONDITIONAL;
2396 		return 0;
2397 	}
2398 
2399 	/* Don't NACK if one was recently sent or peeked */
2400 	if (l->nack_state == BC_NACK_SND_SUPPRESS) {
2401 		l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2402 		return 0;
2403 	}
2404 
2405 	/* Conditionally delay NACK sending until next synch rcv */
2406 	if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
2407 		l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2408 		if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
2409 			return 0;
2410 	}
2411 
2412 	/* Send NACK now but suppress next one */
2413 	tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
2414 	l->nack_state = BC_NACK_SND_SUPPRESS;
2415 	return 0;
2416 }
2417 
2418 int tipc_link_bc_ack_rcv(struct tipc_link *r, u16 acked, u16 gap,
2419 			 struct tipc_gap_ack_blks *ga,
2420 			 struct sk_buff_head *xmitq,
2421 			 struct sk_buff_head *retrq)
2422 {
2423 	struct tipc_link *l = r->bc_sndlink;
2424 	bool unused = false;
2425 	int rc = 0;
2426 
2427 	if (!link_is_up(r) || !r->bc_peer_is_up)
2428 		return 0;
2429 
2430 	if (gap) {
2431 		l->stats.recv_nacks++;
2432 		r->stats.recv_nacks++;
2433 	}
2434 
2435 	if (less(acked, r->acked) || (acked == r->acked && !gap && !ga))
2436 		return 0;
2437 
2438 	trace_tipc_link_bc_ack(r, acked, gap, &l->transmq);
2439 	tipc_link_advance_transmq(l, r, acked, gap, ga, retrq, &unused, &rc);
2440 
2441 	tipc_link_advance_backlog(l, xmitq);
2442 	if (unlikely(!skb_queue_empty(&l->wakeupq)))
2443 		link_prepare_wakeup(l);
2444 
2445 	return rc;
2446 }
2447 
2448 /* tipc_link_bc_nack_rcv(): receive broadcast nack message
2449  * This function is here for backwards compatibility, since
2450  * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
2451  */
2452 int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
2453 			  struct sk_buff_head *xmitq)
2454 {
2455 	struct tipc_msg *hdr = buf_msg(skb);
2456 	u32 dnode = msg_destnode(hdr);
2457 	int mtyp = msg_type(hdr);
2458 	u16 acked = msg_bcast_ack(hdr);
2459 	u16 from = acked + 1;
2460 	u16 to = msg_bcgap_to(hdr);
2461 	u16 peers_snd_nxt = to + 1;
2462 	int rc = 0;
2463 
2464 	kfree_skb(skb);
2465 
2466 	if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
2467 		return 0;
2468 
2469 	if (mtyp != STATE_MSG)
2470 		return 0;
2471 
2472 	if (dnode == tipc_own_addr(l->net)) {
2473 		rc = tipc_link_bc_ack_rcv(l, acked, to - acked, NULL, xmitq,
2474 					  xmitq);
2475 		l->stats.recv_nacks++;
2476 		return rc;
2477 	}
2478 
2479 	/* Msg for other node => suppress own NACK at next sync if applicable */
2480 	if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
2481 		l->nack_state = BC_NACK_SND_SUPPRESS;
2482 
2483 	return 0;
2484 }
2485 
2486 void tipc_link_set_queue_limits(struct tipc_link *l, u32 min_win, u32 max_win)
2487 {
2488 	int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
2489 
2490 	l->min_win = min_win;
2491 	l->ssthresh = max_win;
2492 	l->max_win = max_win;
2493 	l->window = min_win;
2494 	l->backlog[TIPC_LOW_IMPORTANCE].limit      = min_win * 2;
2495 	l->backlog[TIPC_MEDIUM_IMPORTANCE].limit   = min_win * 4;
2496 	l->backlog[TIPC_HIGH_IMPORTANCE].limit     = min_win * 6;
2497 	l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = min_win * 8;
2498 	l->backlog[TIPC_SYSTEM_IMPORTANCE].limit   = max_bulk;
2499 }
2500 
2501 /**
2502  * link_reset_stats - reset link statistics
2503  * @l: pointer to link
2504  */
2505 void tipc_link_reset_stats(struct tipc_link *l)
2506 {
2507 	memset(&l->stats, 0, sizeof(l->stats));
2508 }
2509 
2510 static void link_print(struct tipc_link *l, const char *str)
2511 {
2512 	struct sk_buff *hskb = skb_peek(&l->transmq);
2513 	u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
2514 	u16 tail = l->snd_nxt - 1;
2515 
2516 	pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
2517 	pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
2518 		skb_queue_len(&l->transmq), head, tail,
2519 		skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
2520 }
2521 
2522 /* Parse and validate nested (link) properties valid for media, bearer and link
2523  */
2524 int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2525 {
2526 	int err;
2527 
2528 	err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
2529 					  tipc_nl_prop_policy, NULL);
2530 	if (err)
2531 		return err;
2532 
2533 	if (props[TIPC_NLA_PROP_PRIO]) {
2534 		u32 prio;
2535 
2536 		prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2537 		if (prio > TIPC_MAX_LINK_PRI)
2538 			return -EINVAL;
2539 	}
2540 
2541 	if (props[TIPC_NLA_PROP_TOL]) {
2542 		u32 tol;
2543 
2544 		tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2545 		if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2546 			return -EINVAL;
2547 	}
2548 
2549 	if (props[TIPC_NLA_PROP_WIN]) {
2550 		u32 max_win;
2551 
2552 		max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2553 		if (max_win < TIPC_DEF_LINK_WIN || max_win > TIPC_MAX_LINK_WIN)
2554 			return -EINVAL;
2555 	}
2556 
2557 	return 0;
2558 }
2559 
2560 static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
2561 {
2562 	int i;
2563 	struct nlattr *stats;
2564 
2565 	struct nla_map {
2566 		u32 key;
2567 		u32 val;
2568 	};
2569 
2570 	struct nla_map map[] = {
2571 		{TIPC_NLA_STATS_RX_INFO, 0},
2572 		{TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2573 		{TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2574 		{TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2575 		{TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2576 		{TIPC_NLA_STATS_TX_INFO, 0},
2577 		{TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2578 		{TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2579 		{TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2580 		{TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2581 		{TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2582 			s->msg_length_counts : 1},
2583 		{TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2584 		{TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2585 		{TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2586 		{TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2587 		{TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2588 		{TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2589 		{TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2590 		{TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2591 		{TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2592 		{TIPC_NLA_STATS_RX_STATES, s->recv_states},
2593 		{TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2594 		{TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2595 		{TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2596 		{TIPC_NLA_STATS_TX_STATES, s->sent_states},
2597 		{TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2598 		{TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2599 		{TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2600 		{TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2601 		{TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2602 		{TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2603 		{TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2604 		{TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2605 			(s->accu_queue_sz / s->queue_sz_counts) : 0}
2606 	};
2607 
2608 	stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2609 	if (!stats)
2610 		return -EMSGSIZE;
2611 
2612 	for (i = 0; i <  ARRAY_SIZE(map); i++)
2613 		if (nla_put_u32(skb, map[i].key, map[i].val))
2614 			goto msg_full;
2615 
2616 	nla_nest_end(skb, stats);
2617 
2618 	return 0;
2619 msg_full:
2620 	nla_nest_cancel(skb, stats);
2621 
2622 	return -EMSGSIZE;
2623 }
2624 
2625 /* Caller should hold appropriate locks to protect the link */
2626 int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2627 		       struct tipc_link *link, int nlflags)
2628 {
2629 	u32 self = tipc_own_addr(net);
2630 	struct nlattr *attrs;
2631 	struct nlattr *prop;
2632 	void *hdr;
2633 	int err;
2634 
2635 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2636 			  nlflags, TIPC_NL_LINK_GET);
2637 	if (!hdr)
2638 		return -EMSGSIZE;
2639 
2640 	attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2641 	if (!attrs)
2642 		goto msg_full;
2643 
2644 	if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2645 		goto attr_msg_full;
2646 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
2647 		goto attr_msg_full;
2648 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
2649 		goto attr_msg_full;
2650 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
2651 		goto attr_msg_full;
2652 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
2653 		goto attr_msg_full;
2654 
2655 	if (tipc_link_is_up(link))
2656 		if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2657 			goto attr_msg_full;
2658 	if (link->active)
2659 		if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2660 			goto attr_msg_full;
2661 
2662 	prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2663 	if (!prop)
2664 		goto attr_msg_full;
2665 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2666 		goto prop_msg_full;
2667 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2668 		goto prop_msg_full;
2669 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2670 			link->window))
2671 		goto prop_msg_full;
2672 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2673 		goto prop_msg_full;
2674 	nla_nest_end(msg->skb, prop);
2675 
2676 	err = __tipc_nl_add_stats(msg->skb, &link->stats);
2677 	if (err)
2678 		goto attr_msg_full;
2679 
2680 	nla_nest_end(msg->skb, attrs);
2681 	genlmsg_end(msg->skb, hdr);
2682 
2683 	return 0;
2684 
2685 prop_msg_full:
2686 	nla_nest_cancel(msg->skb, prop);
2687 attr_msg_full:
2688 	nla_nest_cancel(msg->skb, attrs);
2689 msg_full:
2690 	genlmsg_cancel(msg->skb, hdr);
2691 
2692 	return -EMSGSIZE;
2693 }
2694 
2695 static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
2696 				      struct tipc_stats *stats)
2697 {
2698 	int i;
2699 	struct nlattr *nest;
2700 
2701 	struct nla_map {
2702 		__u32 key;
2703 		__u32 val;
2704 	};
2705 
2706 	struct nla_map map[] = {
2707 		{TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
2708 		{TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
2709 		{TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
2710 		{TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
2711 		{TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
2712 		{TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
2713 		{TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
2714 		{TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
2715 		{TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
2716 		{TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
2717 		{TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
2718 		{TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
2719 		{TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
2720 		{TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
2721 		{TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
2722 		{TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
2723 		{TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
2724 		{TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
2725 		{TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
2726 			(stats->accu_queue_sz / stats->queue_sz_counts) : 0}
2727 	};
2728 
2729 	nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2730 	if (!nest)
2731 		return -EMSGSIZE;
2732 
2733 	for (i = 0; i <  ARRAY_SIZE(map); i++)
2734 		if (nla_put_u32(skb, map[i].key, map[i].val))
2735 			goto msg_full;
2736 
2737 	nla_nest_end(skb, nest);
2738 
2739 	return 0;
2740 msg_full:
2741 	nla_nest_cancel(skb, nest);
2742 
2743 	return -EMSGSIZE;
2744 }
2745 
2746 int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg,
2747 			struct tipc_link *bcl)
2748 {
2749 	int err;
2750 	void *hdr;
2751 	struct nlattr *attrs;
2752 	struct nlattr *prop;
2753 	u32 bc_mode = tipc_bcast_get_mode(net);
2754 	u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
2755 
2756 	if (!bcl)
2757 		return 0;
2758 
2759 	tipc_bcast_lock(net);
2760 
2761 	hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2762 			  NLM_F_MULTI, TIPC_NL_LINK_GET);
2763 	if (!hdr) {
2764 		tipc_bcast_unlock(net);
2765 		return -EMSGSIZE;
2766 	}
2767 
2768 	attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2769 	if (!attrs)
2770 		goto msg_full;
2771 
2772 	/* The broadcast link is always up */
2773 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2774 		goto attr_msg_full;
2775 
2776 	if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
2777 		goto attr_msg_full;
2778 	if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
2779 		goto attr_msg_full;
2780 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
2781 		goto attr_msg_full;
2782 	if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
2783 		goto attr_msg_full;
2784 
2785 	prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2786 	if (!prop)
2787 		goto attr_msg_full;
2788 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->max_win))
2789 		goto prop_msg_full;
2790 	if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
2791 		goto prop_msg_full;
2792 	if (bc_mode & BCLINK_MODE_SEL)
2793 		if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
2794 				bc_ratio))
2795 			goto prop_msg_full;
2796 	nla_nest_end(msg->skb, prop);
2797 
2798 	err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
2799 	if (err)
2800 		goto attr_msg_full;
2801 
2802 	tipc_bcast_unlock(net);
2803 	nla_nest_end(msg->skb, attrs);
2804 	genlmsg_end(msg->skb, hdr);
2805 
2806 	return 0;
2807 
2808 prop_msg_full:
2809 	nla_nest_cancel(msg->skb, prop);
2810 attr_msg_full:
2811 	nla_nest_cancel(msg->skb, attrs);
2812 msg_full:
2813 	tipc_bcast_unlock(net);
2814 	genlmsg_cancel(msg->skb, hdr);
2815 
2816 	return -EMSGSIZE;
2817 }
2818 
2819 void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
2820 			     struct sk_buff_head *xmitq)
2821 {
2822 	l->tolerance = tol;
2823 	if (l->bc_rcvlink)
2824 		l->bc_rcvlink->tolerance = tol;
2825 	if (link_is_up(l))
2826 		tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
2827 }
2828 
2829 void tipc_link_set_prio(struct tipc_link *l, u32 prio,
2830 			struct sk_buff_head *xmitq)
2831 {
2832 	l->priority = prio;
2833 	tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
2834 }
2835 
2836 void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
2837 {
2838 	l->abort_limit = limit;
2839 }
2840 
2841 /**
2842  * tipc_link_dump - dump TIPC link data
2843  * @l: tipc link to be dumped
2844  * @dqueues: bitmask to decide if any link queue to be dumped?
2845  *           - TIPC_DUMP_NONE: don't dump link queues
2846  *           - TIPC_DUMP_TRANSMQ: dump link transmq queue
2847  *           - TIPC_DUMP_BACKLOGQ: dump link backlog queue
2848  *           - TIPC_DUMP_DEFERDQ: dump link deferd queue
2849  *           - TIPC_DUMP_INPUTQ: dump link input queue
2850  *           - TIPC_DUMP_WAKEUP: dump link wakeup queue
2851  *           - TIPC_DUMP_ALL: dump all the link queues above
2852  * @buf: returned buffer of dump data in format
2853  */
2854 int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
2855 {
2856 	int i = 0;
2857 	size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
2858 	struct sk_buff_head *list;
2859 	struct sk_buff *hskb, *tskb;
2860 	u32 len;
2861 
2862 	if (!l) {
2863 		i += scnprintf(buf, sz, "link data: (null)\n");
2864 		return i;
2865 	}
2866 
2867 	i += scnprintf(buf, sz, "link data: %x", l->addr);
2868 	i += scnprintf(buf + i, sz - i, " %x", l->state);
2869 	i += scnprintf(buf + i, sz - i, " %u", l->in_session);
2870 	i += scnprintf(buf + i, sz - i, " %u", l->session);
2871 	i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
2872 	i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
2873 	i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
2874 	i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
2875 	i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
2876 	i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
2877 	i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
2878 	i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
2879 	i += scnprintf(buf + i, sz - i, " %u", 0);
2880 	i += scnprintf(buf + i, sz - i, " %u", 0);
2881 	i += scnprintf(buf + i, sz - i, " %u", l->acked);
2882 
2883 	list = &l->transmq;
2884 	len = skb_queue_len(list);
2885 	hskb = skb_peek(list);
2886 	tskb = skb_peek_tail(list);
2887 	i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2888 		       (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2889 		       (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2890 
2891 	list = &l->deferdq;
2892 	len = skb_queue_len(list);
2893 	hskb = skb_peek(list);
2894 	tskb = skb_peek_tail(list);
2895 	i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2896 		       (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2897 		       (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2898 
2899 	list = &l->backlogq;
2900 	len = skb_queue_len(list);
2901 	hskb = skb_peek(list);
2902 	tskb = skb_peek_tail(list);
2903 	i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2904 		       (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2905 		       (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2906 
2907 	list = l->inputq;
2908 	len = skb_queue_len(list);
2909 	hskb = skb_peek(list);
2910 	tskb = skb_peek_tail(list);
2911 	i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
2912 		       (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2913 		       (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2914 
2915 	if (dqueues & TIPC_DUMP_TRANSMQ) {
2916 		i += scnprintf(buf + i, sz - i, "transmq: ");
2917 		i += tipc_list_dump(&l->transmq, false, buf + i);
2918 	}
2919 	if (dqueues & TIPC_DUMP_BACKLOGQ) {
2920 		i += scnprintf(buf + i, sz - i,
2921 			       "backlogq: <%u %u %u %u %u>, ",
2922 			       l->backlog[TIPC_LOW_IMPORTANCE].len,
2923 			       l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
2924 			       l->backlog[TIPC_HIGH_IMPORTANCE].len,
2925 			       l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
2926 			       l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
2927 		i += tipc_list_dump(&l->backlogq, false, buf + i);
2928 	}
2929 	if (dqueues & TIPC_DUMP_DEFERDQ) {
2930 		i += scnprintf(buf + i, sz - i, "deferdq: ");
2931 		i += tipc_list_dump(&l->deferdq, false, buf + i);
2932 	}
2933 	if (dqueues & TIPC_DUMP_INPUTQ) {
2934 		i += scnprintf(buf + i, sz - i, "inputq: ");
2935 		i += tipc_list_dump(l->inputq, false, buf + i);
2936 	}
2937 	if (dqueues & TIPC_DUMP_WAKEUP) {
2938 		i += scnprintf(buf + i, sz - i, "wakeup: ");
2939 		i += tipc_list_dump(&l->wakeupq, false, buf + i);
2940 	}
2941 
2942 	return i;
2943 }
2944