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