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