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