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