]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/tipc/link.c
Merge tag 'for-linus-5.4-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-hirsute-kernel.git] / net / tipc / link.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/link.c: TIPC link code
c4307285 3 *
e74a386d 4 * Copyright (c) 1996-2007, 2012-2016, Ericsson AB
198d73b8 5 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
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.
b97bf3fd 19 *
9ea1fd3c
PL
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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
e3eea1eb 38#include "subscr.h"
b97bf3fd 39#include "link.h"
7be57fc6 40#include "bcast.h"
9816f061 41#include "socket.h"
b97bf3fd 42#include "name_distr.h"
b97bf3fd 43#include "discover.h"
0655f6a8 44#include "netlink.h"
35c55c98 45#include "monitor.h"
b4b9771b 46#include "trace.h"
b97bf3fd 47
796c75d0
YX
48#include <linux/pkt_sched.h>
49
38206d59 50struct tipc_stats {
95901122
JPM
51 u32 sent_pkts;
52 u32 recv_pkts;
38206d59
JPM
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]
38206d59
JPM
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')
35c55c98 100 * @mon_state: cookie with information needed by link monitor
38206d59
JPM
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
38206d59
JPM
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 */
123struct tipc_link {
124 u32 addr;
125 char name[TIPC_MAX_LINK_NAME];
38206d59
JPM
126 struct net *net;
127
128 /* Management and link supervision data */
7ea817f4
JM
129 u16 peer_session;
130 u16 session;
9012de50
JM
131 u16 snd_nxt_state;
132 u16 rcv_nxt_state;
38206d59
JPM
133 u32 peer_bearer_id;
134 u32 bearer_id;
135 u32 tolerance;
38206d59
JPM
136 u32 abort_limit;
137 u32 state;
138 u16 peer_caps;
7ea817f4 139 bool in_session;
38206d59
JPM
140 bool active;
141 u32 silent_intv_cnt;
e74a386d 142 char if_name[TIPC_MAX_IF_NAME];
38206d59
JPM
143 u32 priority;
144 char net_plane;
35c55c98 145 struct tipc_mon_state mon_state;
88e8ac70 146 u16 rst_cnt;
38206d59
JPM
147
148 /* Failover/synch */
149 u16 drop_point;
150 struct sk_buff *failover_reasm_skb;
58ee86b8 151 struct sk_buff_head failover_deferdq;
38206d59
JPM
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;
38206d59 165 u16 window;
38206d59
JPM
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;
2320bcda 179 struct sk_buff *reasm_tnlmsg;
38206d59
JPM
180
181 /* Broadcast */
182 u16 ackers;
183 u16 acked;
184 struct tipc_link *bc_rcvlink;
185 struct tipc_link *bc_sndlink;
7c4a54b9 186 u8 nack_state;
38206d59
JPM
187 bool bc_peer_is_up;
188
189 /* Statistics */
190 struct tipc_stats stats;
191};
192
2cf8aa19
EH
193/*
194 * Error message prefixes
195 */
6e498158 196static const char *link_co_err = "Link tunneling error, ";
2cf8aa19 197static const char *link_rst_msg = "Resetting link ";
7be57fc6 198
52666986
JPM
199/* Send states for broadcast NACKs
200 */
201enum {
202 BC_NACK_SND_CONDITIONAL,
203 BC_NACK_SND_UNCONDITIONAL,
204 BC_NACK_SND_SUPPRESS,
205};
206
53962bce 207#define TIPC_BC_RETR_LIM (jiffies + msecs_to_jiffies(10))
382f598f 208#define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
7c4a54b9 209
d999297c
JPM
210/*
211 * Interval between NACKs when packets arrive out of order
212 */
213#define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
e74a386d 214
662921cd 215/* Link FSM states:
b97bf3fd 216 */
d3504c34 217enum {
662921cd
JPM
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
d3504c34
JPM
225};
226
227/* Link FSM state checking routines
228 */
662921cd 229static int link_is_up(struct tipc_link *l)
d3504c34 230{
662921cd 231 return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
d3504c34 232}
b97bf3fd 233
d999297c
JPM
234static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
235 struct sk_buff_head *xmitq);
426cc2b8 236static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
8d6e79d3
JM
237 bool probe_reply, u16 rcvgap,
238 int tolerance, int priority,
426cc2b8 239 struct sk_buff_head *xmitq);
1a90632d 240static void link_print(struct tipc_link *l, const char *str);
e0a05ebe
JPM
241static int tipc_link_build_nack_msg(struct tipc_link *l,
242 struct sk_buff_head *xmitq);
52666986
JPM
243static void tipc_link_build_bc_init_msg(struct tipc_link *l,
244 struct sk_buff_head *xmitq);
245static bool tipc_link_release_pkts(struct tipc_link *l, u16 to);
9195948f 246static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data);
6a6b5c8b
TL
247static 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);
8b4ed863 250
b97bf3fd 251/*
05790c64 252 * Simple non-static link routines (i.e. referenced outside this file)
b97bf3fd 253 */
662921cd 254bool tipc_link_is_up(struct tipc_link *l)
b97bf3fd 255{
662921cd
JPM
256 return link_is_up(l);
257}
258
c8199300
JPM
259bool tipc_link_peer_is_down(struct tipc_link *l)
260{
261 return l->state == LINK_PEER_RESET;
262}
263
662921cd
JPM
264bool tipc_link_is_reset(struct tipc_link *l)
265{
266 return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
267}
268
73f646ce
JPM
269bool tipc_link_is_establishing(struct tipc_link *l)
270{
271 return l->state == LINK_ESTABLISHING;
272}
273
662921cd
JPM
274bool tipc_link_is_synching(struct tipc_link *l)
275{
276 return l->state == LINK_SYNCHING;
277}
278
279bool tipc_link_is_failingover(struct tipc_link *l)
280{
281 return l->state == LINK_FAILINGOVER;
282}
283
284bool tipc_link_is_blocked(struct tipc_link *l)
285{
286 return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
b97bf3fd
PL
287}
288
742e0383 289static bool link_is_bc_sndlink(struct tipc_link *l)
52666986
JPM
290{
291 return !l->bc_sndlink;
292}
293
742e0383 294static bool link_is_bc_rcvlink(struct tipc_link *l)
52666986
JPM
295{
296 return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
297}
298
c72fa872
JPM
299void tipc_link_set_active(struct tipc_link *l, bool active)
300{
301 l->active = active;
b97bf3fd
PL
302}
303
38206d59
JPM
304u32 tipc_link_id(struct tipc_link *l)
305{
306 return l->peer_bearer_id << 16 | l->bearer_id;
307}
308
309int tipc_link_window(struct tipc_link *l)
310{
311 return l->window;
312}
313
314int tipc_link_prio(struct tipc_link *l)
315{
316 return l->priority;
317}
318
319unsigned long tipc_link_tolerance(struct tipc_link *l)
320{
321 return l->tolerance;
322}
323
324struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
325{
326 return l->inputq;
327}
328
329char tipc_link_plane(struct tipc_link *l)
330{
331 return l->net_plane;
332}
333
9012de50
JM
334void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
335{
336 l->peer_caps = capabilities;
337}
338
52666986
JPM
339void tipc_link_add_bc_peer(struct tipc_link *snd_l,
340 struct tipc_link *uc_l,
341 struct sk_buff_head *xmitq)
2f566124 342{
52666986
JPM
343 struct tipc_link *rcv_l = uc_l->bc_rcvlink;
344
345 snd_l->ackers++;
346 rcv_l->acked = snd_l->snd_nxt - 1;
9a650838 347 snd_l->state = LINK_ESTABLISHED;
52666986 348 tipc_link_build_bc_init_msg(uc_l, xmitq);
2f566124
JPM
349}
350
52666986
JPM
351void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
352 struct tipc_link *rcv_l,
353 struct sk_buff_head *xmitq)
2f566124 354{
52666986
JPM
355 u16 ack = snd_l->snd_nxt - 1;
356
357 snd_l->ackers--;
a71eb720
JPM
358 rcv_l->bc_peer_is_up = true;
359 rcv_l->state = LINK_ESTABLISHED;
52666986 360 tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
26574db0 361 trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
52666986
JPM
362 tipc_link_reset(rcv_l);
363 rcv_l->state = LINK_RESET;
364 if (!snd_l->ackers) {
26574db0 365 trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
52666986 366 tipc_link_reset(snd_l);
9a650838 367 snd_l->state = LINK_RESET;
52666986
JPM
368 __skb_queue_purge(xmitq);
369 }
2f566124
JPM
370}
371
372int tipc_link_bc_peers(struct tipc_link *l)
373{
374 return l->ackers;
375}
376
e064cce1 377static u16 link_bc_rcv_gap(struct tipc_link *l)
02d11ca2
JPM
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
959e1781
JPM
389void tipc_link_set_mtu(struct tipc_link *l, int mtu)
390{
391 l->mtu = mtu;
392}
393
394int tipc_link_mtu(struct tipc_link *l)
395{
396 return l->mtu;
397}
398
38206d59
JPM
399u16 tipc_link_rcv_nxt(struct tipc_link *l)
400{
401 return l->rcv_nxt;
402}
403
404u16 tipc_link_acked(struct tipc_link *l)
405{
406 return l->acked;
407}
408
409char *tipc_link_name(struct tipc_link *l)
410{
411 return l->name;
412}
413
c140eb16
LDC
414u32 tipc_link_state(struct tipc_link *l)
415{
416 return l->state;
417}
418
b97bf3fd 419/**
4323add6 420 * tipc_link_create - create a new link
440d8963 421 * @n: pointer to associated node
0e05498e
JPM
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
440d8963 430 * @ownnode: identity of own node
0e05498e 431 * @peer: node id of peer node
fd556f20 432 * @peer_caps: bitmap describing peer node capabilities
52666986
JPM
433 * @bc_sndlink: the namespace global link used for broadcast sending
434 * @bc_rcvlink: the peer specific link used for broadcast reception
440d8963
JPM
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
c4307285 438 *
440d8963 439 * Returns true if link was created, otherwise false
b97bf3fd 440 */
c72fa872 441bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
0e05498e 442 int tolerance, char net_plane, u32 mtu, int priority,
25b0b9c4
JM
443 int window, u32 session, u32 self,
444 u32 peer, u8 *peer_id, u16 peer_caps,
52666986
JPM
445 struct tipc_link *bc_sndlink,
446 struct tipc_link *bc_rcvlink,
447 struct sk_buff_head *inputq,
448 struct sk_buff_head *namedq,
440d8963 449 struct tipc_link **link)
b97bf3fd 450{
25b0b9c4
JM
451 char peer_str[NODE_ID_STR_LEN] = {0,};
452 char self_str[NODE_ID_STR_LEN] = {0,};
440d8963 453 struct tipc_link *l;
37b9c08a 454
440d8963
JPM
455 l = kzalloc(sizeof(*l), GFP_ATOMIC);
456 if (!l)
457 return false;
458 *link = l;
e74a386d 459 l->session = session;
37b9c08a 460
25b0b9c4
JM
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 */
7494cfa6
JM
471 snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
472 self_str, if_name, peer_str);
25b0b9c4 473
e74a386d 474 strcpy(l->if_name, if_name);
440d8963 475 l->addr = peer;
fd556f20 476 l->peer_caps = peer_caps;
c72fa872 477 l->net = net;
7ea817f4 478 l->in_session = false;
0e05498e
JPM
479 l->bearer_id = bearer_id;
480 l->tolerance = tolerance;
047491ea
JM
481 if (bc_rcvlink)
482 bc_rcvlink->tolerance = tolerance;
0e05498e
JPM
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);
c1ab3f1d 488 l->ackers = 1;
52666986
JPM
489 l->bc_sndlink = bc_sndlink;
490 l->bc_rcvlink = bc_rcvlink;
440d8963
JPM
491 l->inputq = inputq;
492 l->namedq = namedq;
493 l->state = LINK_RESETTING;
440d8963
JPM
494 __skb_queue_head_init(&l->transmq);
495 __skb_queue_head_init(&l->backlogq);
496 __skb_queue_head_init(&l->deferdq);
58ee86b8 497 __skb_queue_head_init(&l->failover_deferdq);
440d8963
JPM
498 skb_queue_head_init(&l->wakeupq);
499 skb_queue_head_init(l->inputq);
500 return true;
b97bf3fd
PL
501}
502
32301906
JPM
503/**
504 * tipc_link_bc_create - create new link to be used for broadcast
505 * @n: pointer to associated node
4c94cc2d 506 * @mtu: mtu to be used initially if no peers
32301906
JPM
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 */
c72fa872 514bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
52666986 515 int mtu, int window, u16 peer_caps,
32301906
JPM
516 struct sk_buff_head *inputq,
517 struct sk_buff_head *namedq,
52666986 518 struct tipc_link *bc_sndlink,
32301906
JPM
519 struct tipc_link **link)
520{
521 struct tipc_link *l;
522
c72fa872 523 if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
25b0b9c4 524 0, ownnode, peer, NULL, peer_caps, bc_sndlink,
52666986 525 NULL, inputq, namedq, link))
32301906
JPM
526 return false;
527
528 l = *link;
529 strcpy(l->name, tipc_bclink_name);
26574db0 530 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
32301906 531 tipc_link_reset(l);
52666986 532 l->state = LINK_RESET;
2f566124 533 l->ackers = 0;
52666986 534 l->bc_rcvlink = l;
32301906 535
52666986
JPM
536 /* Broadcast send link is always up */
537 if (link_is_bc_sndlink(l))
538 l->state = LINK_ESTABLISHED;
d999297c 539
01fd12bb
JPM
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
52666986 544 return true;
d999297c
JPM
545}
546
6ab30f9c
JPM
547/**
548 * tipc_link_fsm_evt - link finite state machine
549 * @l: pointer to link
550 * @evt: state machine event to be processed
6ab30f9c 551 */
662921cd 552int tipc_link_fsm_evt(struct tipc_link *l, int evt)
6ab30f9c 553{
5045f7b9 554 int rc = 0;
26574db0 555 int old_state = l->state;
6ab30f9c
JPM
556
557 switch (l->state) {
662921cd 558 case LINK_RESETTING:
6ab30f9c 559 switch (evt) {
662921cd
JPM
560 case LINK_PEER_RESET_EVT:
561 l->state = LINK_PEER_RESET;
6ab30f9c 562 break;
662921cd
JPM
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;
6ab30f9c 580 break;
662921cd
JPM
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:
6ab30f9c 587 break;
662921cd
JPM
588 case LINK_SYNCH_BEGIN_EVT:
589 case LINK_SYNCH_END_EVT:
6ab30f9c 590 default:
662921cd 591 goto illegal_evt;
6ab30f9c
JPM
592 }
593 break;
662921cd 594 case LINK_PEER_RESET:
6ab30f9c 595 switch (evt) {
662921cd
JPM
596 case LINK_RESET_EVT:
597 l->state = LINK_ESTABLISHING;
6ab30f9c 598 break;
662921cd
JPM
599 case LINK_PEER_RESET_EVT:
600 case LINK_ESTABLISH_EVT:
601 case LINK_FAILURE_EVT:
6ab30f9c 602 break;
662921cd
JPM
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;
6ab30f9c 615 break;
662921cd
JPM
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:
6ab30f9c 624 default:
662921cd 625 goto illegal_evt;
6ab30f9c
JPM
626 }
627 break;
662921cd 628 case LINK_ESTABLISHING:
6ab30f9c 629 switch (evt) {
662921cd
JPM
630 case LINK_ESTABLISH_EVT:
631 l->state = LINK_ESTABLISHED;
6ab30f9c 632 break;
662921cd
JPM
633 case LINK_FAILOVER_BEGIN_EVT:
634 l->state = LINK_FAILINGOVER;
635 break;
662921cd 636 case LINK_RESET_EVT:
73f646ce
JPM
637 l->state = LINK_RESET;
638 break;
662921cd 639 case LINK_FAILURE_EVT:
73f646ce 640 case LINK_PEER_RESET_EVT:
662921cd
JPM
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;
6ab30f9c 658 break;
662921cd
JPM
659 case LINK_RESET_EVT:
660 l->state = LINK_RESET;
6ab30f9c 661 break;
662921cd 662 case LINK_ESTABLISH_EVT:
5ae2f8e6 663 case LINK_SYNCH_END_EVT:
6ab30f9c 664 break;
662921cd
JPM
665 case LINK_SYNCH_BEGIN_EVT:
666 l->state = LINK_SYNCHING;
667 break;
662921cd
JPM
668 case LINK_FAILOVER_BEGIN_EVT:
669 case LINK_FAILOVER_END_EVT:
6ab30f9c 670 default:
662921cd 671 goto illegal_evt;
6ab30f9c
JPM
672 }
673 break;
662921cd 674 case LINK_SYNCHING:
6ab30f9c 675 switch (evt) {
662921cd
JPM
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;
6ab30f9c 683 break;
662921cd
JPM
684 case LINK_RESET_EVT:
685 l->state = LINK_RESET;
6ab30f9c 686 break;
662921cd
JPM
687 case LINK_ESTABLISH_EVT:
688 case LINK_SYNCH_BEGIN_EVT:
6ab30f9c 689 break;
662921cd
JPM
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:
6ab30f9c 695 default:
662921cd 696 goto illegal_evt;
6ab30f9c
JPM
697 }
698 break;
699 default:
662921cd 700 pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
6ab30f9c 701 }
26574db0 702 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
662921cd
JPM
703 return rc;
704illegal_evt:
705 pr_err("Illegal FSM event %x in state %x on link %s\n",
706 evt, l->state, l->name);
26574db0 707 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
6ab30f9c
JPM
708 return rc;
709}
710
333ef69e
JPM
711/* link_profile_stats - update statistical profiling of traffic
712 */
713static 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;
a7dc51ad 732 length = msg_size(msg_inner_hdr(msg));
333ef69e
JPM
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
26574db0
TL
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 */
759bool tipc_link_too_silent(struct tipc_link *l)
760{
761 return (l->silent_intv_cnt + 2 > l->abort_limit);
762}
763
333ef69e
JPM
764/* tipc_link_timeout - perform periodic task as instructed from node timeout
765 */
766int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
767{
c91522f8
YX
768 int mtyp = 0;
769 int rc = 0;
42b18f60
JPM
770 bool state = false;
771 bool probe = false;
772 bool setup = false;
52666986
JPM
773 u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
774 u16 bc_acked = l->bc_rcvlink->acked;
35c55c98 775 struct tipc_mon_state *mstate = &l->mon_state;
5045f7b9 776
26574db0
TL
777 trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
778 trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
662921cd
JPM
779 switch (l->state) {
780 case LINK_ESTABLISHED:
781 case LINK_SYNCHING:
42b18f60 782 mtyp = STATE_MSG;
35c55c98
JPM
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);
42b18f60 787 state = bc_acked != bc_snt;
35c55c98
JPM
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++;
662921cd
JPM
796 break;
797 case LINK_RESET:
42b18f60
JPM
798 setup = l->rst_cnt++ <= 4;
799 setup |= !(l->rst_cnt % 16);
5045f7b9 800 mtyp = RESET_MSG;
662921cd
JPM
801 break;
802 case LINK_ESTABLISHING:
42b18f60 803 setup = true;
5045f7b9 804 mtyp = ACTIVATE_MSG;
662921cd 805 break;
662921cd 806 case LINK_PEER_RESET:
598411d7 807 case LINK_RESETTING:
662921cd
JPM
808 case LINK_FAILINGOVER:
809 break;
810 default:
811 break;
5045f7b9 812 }
662921cd 813
42b18f60 814 if (state || probe || setup)
8d6e79d3 815 tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
5045f7b9 816
333ef69e
JPM
817 return rc;
818}
819
b97bf3fd 820/**
3127a020 821 * link_schedule_user - schedule a message sender for wakeup after congestion
365ad353
JPM
822 * @l: congested link
823 * @hdr: header of message that is being sent
50100a5e 824 * Create pseudo msg to send back to user when congestion abates
b97bf3fd 825 */
365ad353 826static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
b97bf3fd 827{
365ad353
JPM
828 u32 dnode = tipc_own_addr(l->net);
829 u32 dport = msg_origport(hdr);
3127a020
JPM
830 struct sk_buff *skb;
831
3127a020
JPM
832 /* Create and schedule wakeup pseudo message */
833 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
365ad353 834 dnode, l->addr, dport, 0, 0);
3127a020 835 if (!skb)
22d85c79 836 return -ENOBUFS;
365ad353
JPM
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++;
26574db0 841 trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
3127a020 842 return -ELINKCONG;
b97bf3fd
PL
843}
844
50100a5e
JPM
845/**
846 * link_prepare_wakeup - prepare users for wakeup after congestion
365ad353
JPM
847 * @l: congested link
848 * Wake up a number of waiting users, as permitted by available space
849 * in the send queue
50100a5e 850 */
e064cce1 851static void link_prepare_wakeup(struct tipc_link *l)
b97bf3fd 852{
7c5b4205
JM
853 struct sk_buff_head *wakeupq = &l->wakeupq;
854 struct sk_buff_head *inputq = l->inputq;
58d78b32 855 struct sk_buff *skb, *tmp;
7c5b4205
JM
856 struct sk_buff_head tmpq;
857 int avail[5] = {0,};
858 int imp = 0;
50100a5e 859
7c5b4205
JM
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) {
1f66d161 866 imp = TIPC_SKB_CB(skb)->chain_imp;
7c5b4205
JM
867 if (avail[imp] <= 0)
868 continue;
869 avail[imp]--;
870 __skb_unlink(skb, wakeupq);
871 __skb_queue_tail(&tmpq, skb);
b97bf3fd 872 }
7c5b4205
JM
873
874 spin_lock_bh(&inputq->lock);
875 skb_queue_splice_tail(&tmpq, inputq);
876 spin_unlock_bh(&inputq->lock);
877
b97bf3fd
PL
878}
879
6e498158 880void tipc_link_reset(struct tipc_link *l)
b97bf3fd 881{
a1f8dd34
YX
882 struct sk_buff_head list;
883
884 __skb_queue_head_init(&list);
885
7ea817f4 886 l->in_session = false;
f7a93780
TL
887 /* Force re-synch of peer session number before establishing */
888 l->peer_session--;
e74a386d 889 l->session++;
6e498158 890 l->mtu = l->advertised_mtu;
a1f8dd34 891
3f32d0be 892 spin_lock_bh(&l->wakeupq.lock);
a1f8dd34
YX
893 skb_queue_splice_init(&l->wakeupq, &list);
894 spin_unlock_bh(&l->wakeupq.lock);
895
3f32d0be 896 spin_lock_bh(&l->inputq->lock);
a1f8dd34 897 skb_queue_splice_init(&list, l->inputq);
3f32d0be 898 spin_unlock_bh(&l->inputq->lock);
3f32d0be 899
6e498158
JPM
900 __skb_queue_purge(&l->transmq);
901 __skb_queue_purge(&l->deferdq);
2af5ae37 902 __skb_queue_purge(&l->backlogq);
58ee86b8 903 __skb_queue_purge(&l->failover_deferdq);
2af5ae37
JPM
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;
6e498158 909 kfree_skb(l->reasm_buf);
2320bcda 910 kfree_skb(l->reasm_tnlmsg);
6e498158
JPM
911 kfree_skb(l->failover_reasm_skb);
912 l->reasm_buf = NULL;
2320bcda 913 l->reasm_tnlmsg = NULL;
6e498158
JPM
914 l->failover_reasm_skb = NULL;
915 l->rcv_unacked = 0;
916 l->snd_nxt = 1;
917 l->rcv_nxt = 1;
9012de50
JM
918 l->snd_nxt_state = 1;
919 l->rcv_nxt_state = 1;
c1ab3f1d 920 l->acked = 0;
6e498158 921 l->silent_intv_cnt = 0;
88e8ac70 922 l->rst_cnt = 0;
52666986 923 l->bc_peer_is_up = false;
35c55c98 924 memset(&l->mon_state, 0, sizeof(l->mon_state));
38206d59 925 tipc_link_reset_stats(l);
b97bf3fd
PL
926}
927
af9b028e
JPM
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 *
365ad353 934 * Consumes the buffer chain.
af9b028e
JPM
935 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
936 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
937 */
938int 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;
365ad353 943 int imp = msg_importance(hdr);
af9b028e
JPM
944 unsigned int mtu = l->mtu;
945 u16 ack = l->rcv_nxt - 1;
946 u16 seqno = l->snd_nxt;
52666986 947 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
af9b028e
JPM
948 struct sk_buff_head *transmq = &l->transmq;
949 struct sk_buff_head *backlogq = &l->backlogq;
950 struct sk_buff *skb, *_skb, *bskb;
95901122 951 int pkt_cnt = skb_queue_len(list);
365ad353 952 int rc = 0;
af9b028e 953
4952cd3e 954 if (unlikely(msg_size(hdr) > mtu)) {
2320bcda
TL
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);
e654f9f5 958 __skb_queue_purge(list);
af9b028e 959 return -EMSGSIZE;
4952cd3e 960 }
af9b028e 961
365ad353
JPM
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
95901122
JPM
971 if (pkt_cnt > 1) {
972 l->stats.sent_fragmented++;
973 l->stats.sent_fragments += pkt_cnt;
974 }
975
af9b028e
JPM
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);
52666986 982 msg_set_bcast_ack(hdr, bc_ack);
af9b028e
JPM
983
984 if (likely(skb_queue_len(transmq) < maxwin)) {
985 _skb = skb_clone(skb, GFP_ATOMIC);
4952cd3e 986 if (!_skb) {
e654f9f5 987 __skb_queue_purge(list);
af9b028e 988 return -ENOBUFS;
4952cd3e 989 }
af9b028e
JPM
990 __skb_dequeue(list);
991 __skb_queue_tail(transmq, skb);
05572271
HL
992 /* next retransmit attempt */
993 if (link_is_bc_sndlink(l))
53962bce 994 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
af9b028e 995 __skb_queue_tail(xmitq, _skb);
c1ab3f1d 996 TIPC_SKB_CB(skb)->ackers = l->ackers;
af9b028e 997 l->rcv_unacked = 0;
95901122 998 l->stats.sent_pkts++;
af9b028e
JPM
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;
365ad353 1019 return rc;
af9b028e
JPM
1020}
1021
e064cce1
Y
1022static void tipc_link_advance_backlog(struct tipc_link *l,
1023 struct sk_buff_head *xmitq)
d999297c
JPM
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;
52666986 1029 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
d999297c
JPM
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);
05572271
HL
1042 /* next retransmit attempt */
1043 if (link_is_bc_sndlink(l))
53962bce 1044 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
05572271 1045
d999297c 1046 __skb_queue_tail(xmitq, _skb);
c1ab3f1d 1047 TIPC_SKB_CB(skb)->ackers = l->ackers;
d999297c 1048 msg_set_seqno(hdr, seqno);
52666986
JPM
1049 msg_set_ack(hdr, ack);
1050 msg_set_bcast_ack(hdr, bc_ack);
d999297c 1051 l->rcv_unacked = 0;
95901122 1052 l->stats.sent_pkts++;
d999297c
JPM
1053 seqno++;
1054 }
1055 l->snd_nxt = seqno;
1056}
1057
6a6b5c8b
TL
1058/**
1059 * link_retransmit_failure() - Detect repeated retransmit failures
1060 * @l: tipc link sender
1061 * @r: tipc link receiver (= l in case of unicast)
6a6b5c8b
TL
1062 * @rc: returned code
1063 *
1064 * Return: true if the repeated retransmit failures happens, otherwise
1065 * false
1066 */
1067static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
71204231 1068 int *rc)
d356eeba 1069{
6a6b5c8b
TL
1070 struct sk_buff *skb = skb_peek(&l->transmq);
1071 struct tipc_msg *hdr;
1072
1073 if (!skb)
1074 return false;
6a6b5c8b 1075
71204231
TL
1076 if (!TIPC_SKB_CB(skb)->retr_cnt)
1077 return false;
6a6b5c8b 1078
71204231
TL
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;
6a6b5c8b 1086
71204231
TL
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 {
6a6b5c8b 1105 *rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
6a6b5c8b 1106 }
52666986 1107
71204231 1108 return true;
d356eeba
AS
1109}
1110
6a6b5c8b 1111/* tipc_link_bc_retrans() - retransmit zero or more packets
a4dc70d4
JM
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 */
6a6b5c8b
TL
1118static int tipc_link_bc_retrans(struct tipc_link *l, struct tipc_link *r,
1119 u16 from, u16 to, struct sk_buff_head *xmitq)
d999297c
JPM
1120{
1121 struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
52666986 1122 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
a4dc70d4
JM
1123 u16 ack = l->rcv_nxt - 1;
1124 struct tipc_msg *hdr;
6a6b5c8b 1125 int rc = 0;
d999297c
JPM
1126
1127 if (!skb)
1128 return 0;
31c4f4cc
LDC
1129 if (less(to, from))
1130 return 0;
d999297c 1131
26574db0 1132 trace_tipc_link_retrans(r, from, to, &l->transmq);
6a6b5c8b 1133
71204231 1134 if (link_retransmit_failure(l, r, &rc))
6a6b5c8b 1135 return rc;
c1ab3f1d 1136
d999297c 1137 skb_queue_walk(&l->transmq, skb) {
d999297c 1138 hdr = buf_msg(skb);
a4dc70d4
JM
1139 if (less(msg_seqno(hdr), from))
1140 continue;
1141 if (more(msg_seqno(hdr), to))
1142 break;
71204231
TL
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;
20c67312 1147 _skb = __pskb_copy(skb, LL_MAX_HEADER + MIN_H_SIZE, GFP_ATOMIC);
d999297c
JPM
1148 if (!_skb)
1149 return 0;
1150 hdr = buf_msg(_skb);
c1ab3f1d
JPM
1151 msg_set_ack(hdr, ack);
1152 msg_set_bcast_ack(hdr, bc_ack);
d999297c
JPM
1153 _skb->priority = TC_PRIO_CONTROL;
1154 __skb_queue_tail(xmitq, _skb);
d999297c 1155 l->stats.retransmitted++;
71204231
TL
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;
d999297c
JPM
1160 }
1161 return 0;
1162}
1163
c637c103 1164/* tipc_data_input - deliver data and name distr msgs to upper layer
7ae934be 1165 *
c637c103 1166 * Consumes buffer if message is of right type
7ae934be
EH
1167 * Node lock must be held
1168 */
52666986 1169static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
9073fb8b 1170 struct sk_buff_head *inputq)
7ae934be 1171{
399574d4 1172 struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
a853e4c6
JPM
1173 struct tipc_msg *hdr = buf_msg(skb);
1174
1175 switch (msg_user(hdr)) {
c637c103
JPM
1176 case TIPC_LOW_IMPORTANCE:
1177 case TIPC_MEDIUM_IMPORTANCE:
1178 case TIPC_HIGH_IMPORTANCE:
1179 case TIPC_CRITICAL_IMPORTANCE:
2f487712 1180 if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
399574d4 1181 skb_queue_tail(mc_inputq, skb);
a853e4c6
JPM
1182 return true;
1183 }
f79e3365 1184 /* fall through */
2f487712 1185 case CONN_MANAGER:
36c0a9df 1186 skb_queue_tail(inputq, skb);
c637c103 1187 return true;
399574d4
JM
1188 case GROUP_PROTOCOL:
1189 skb_queue_tail(mc_inputq, skb);
1190 return true;
7ae934be 1191 case NAME_DISTRIBUTOR:
52666986
JPM
1192 l->bc_rcvlink->state = LINK_ESTABLISHED;
1193 skb_queue_tail(l->namedq, skb);
c637c103
JPM
1194 return true;
1195 case MSG_BUNDLER:
dff29b1a 1196 case TUNNEL_PROTOCOL:
c637c103 1197 case MSG_FRAGMENTER:
7ae934be 1198 case BCAST_PROTOCOL:
c637c103 1199 return false;
7ae934be 1200 default:
c637c103
JPM
1201 pr_warn("Dropping received illegal msg type\n");
1202 kfree_skb(skb);
7384b538 1203 return true;
c637c103 1204 };
7ae934be 1205}
c637c103
JPM
1206
1207/* tipc_link_input - process packet that has passed link protocol check
1208 *
1209 * Consumes buffer
7ae934be 1210 */
9073fb8b 1211static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
58ee86b8
TL
1212 struct sk_buff_head *inputq,
1213 struct sk_buff **reasm_skb)
7ae934be 1214{
6e498158 1215 struct tipc_msg *hdr = buf_msg(skb);
c637c103 1216 struct sk_buff *iskb;
9945e804 1217 struct sk_buff_head tmpq;
6e498158 1218 int usr = msg_user(hdr);
6e498158 1219 int pos = 0;
c637c103 1220
6e498158 1221 if (usr == MSG_BUNDLER) {
9945e804 1222 skb_queue_head_init(&tmpq);
6e498158
JPM
1223 l->stats.recv_bundles++;
1224 l->stats.recv_bundled += msg_msgcnt(hdr);
c637c103 1225 while (tipc_msg_extract(skb, &iskb, &pos))
9945e804
JPM
1226 tipc_data_input(l, iskb, &tmpq);
1227 tipc_skb_queue_splice_tail(&tmpq, inputq);
662921cd 1228 return 0;
6e498158
JPM
1229 } else if (usr == MSG_FRAGMENTER) {
1230 l->stats.recv_fragments++;
1231 if (tipc_buf_append(reasm_skb, &skb)) {
1232 l->stats.recv_fragmented++;
9073fb8b 1233 tipc_data_input(l, skb, inputq);
52666986
JPM
1234 } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
1235 pr_warn_ratelimited("Unable to build fragment list\n");
662921cd 1236 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
c637c103 1237 }
662921cd 1238 return 0;
6e498158 1239 } else if (usr == BCAST_PROTOCOL) {
c72fa872 1240 tipc_bcast_lock(l->net);
52666986 1241 tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
c72fa872 1242 tipc_bcast_unlock(l->net);
6e498158 1243 }
58ee86b8 1244
6e498158 1245 kfree_skb(skb);
662921cd 1246 return 0;
7ae934be
EH
1247}
1248
58ee86b8
TL
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 */
1256static 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;
2320bcda 1260 struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
58ee86b8
TL
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
2320bcda
TL
1268 if (msg_type(hdr) == SYNCH_MSG) {
1269 kfree_skb(skb);
1270 return 0;
1271 }
58ee86b8 1272
2320bcda
TL
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;
58ee86b8
TL
1298 }
1299
1300 do {
1301 seqno = buf_seqno(iskb);
58ee86b8
TL
1302 if (unlikely(less(seqno, l->drop_point))) {
1303 kfree_skb(iskb);
1304 continue;
1305 }
58ee86b8
TL
1306 if (unlikely(seqno != l->drop_point)) {
1307 __tipc_skb_queue_sorted(fdefq, seqno, iskb);
1308 continue;
1309 }
1310
1311 l->drop_point++;
58ee86b8
TL
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
58ee86b8
TL
1318 return rc;
1319}
1320
d999297c
JPM
1321static 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
9195948f
TL
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 */
1342static 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
1374exit:
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
6a6b5c8b
TL
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)
9195948f 1392 */
6a6b5c8b
TL
1393static 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)
9195948f
TL
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;
71204231 1401 bool passed = false;
6a6b5c8b
TL
1402 u16 seqno, n = 0;
1403 int rc = 0;
1404
9195948f
TL
1405 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1406 seqno = buf_seqno(skb);
1407
1408next_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)) {
71204231
TL
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*/
382f598f
TL
1420 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1421 continue;
1422 TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
71204231
TL
1423 _skb = __pskb_copy(skb, LL_MAX_HEADER + MIN_H_SIZE,
1424 GFP_ATOMIC);
9195948f
TL
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++;
71204231
TL
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;
9195948f
TL
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 }
6a6b5c8b
TL
1447
1448 return 0;
9195948f
TL
1449}
1450
34b9cd64 1451/* tipc_link_build_state_msg: prepare link state message for transmission
52666986
JPM
1452 *
1453 * Note that sending of broadcast ack is coordinated among nodes, to reduce
1454 * risk of ack storms towards the sender
f9aa358a 1455 */
34b9cd64 1456int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
f9aa358a 1457{
52666986
JPM
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)) {
e74a386d 1463 if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
52666986
JPM
1464 return 0;
1465 l->rcv_unacked = 0;
02d11ca2
JPM
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;
52666986
JPM
1470 }
1471
1472 /* Unicast ACK */
f9aa358a
JPM
1473 l->rcv_unacked = 0;
1474 l->stats.sent_acks++;
8d6e79d3 1475 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
52666986 1476 return 0;
f9aa358a
JPM
1477}
1478
282b3a05
JPM
1479/* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
1480 */
1481void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1482{
1483 int mtyp = RESET_MSG;
634696b1 1484 struct sk_buff *skb;
282b3a05
JPM
1485
1486 if (l->state == LINK_ESTABLISHING)
1487 mtyp = ACTIVATE_MSG;
1488
8d6e79d3 1489 tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
634696b1
JPM
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);
282b3a05
JPM
1495}
1496
f9aa358a 1497/* tipc_link_build_nack_msg: prepare link nack message for transmission
e0a05ebe
JPM
1498 * Note that sending of broadcast NACK is coordinated among nodes, to
1499 * reduce the risk of NACK storms towards the sender
f9aa358a 1500 */
e0a05ebe
JPM
1501static int tipc_link_build_nack_msg(struct tipc_link *l,
1502 struct sk_buff_head *xmitq)
f9aa358a
JPM
1503{
1504 u32 def_cnt = ++l->stats.deferred_recv;
382f598f 1505 u32 defq_len = skb_queue_len(&l->deferdq);
e0a05ebe 1506 int match1, match2;
f9aa358a 1507
e0a05ebe
JPM
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 }
52666986 1515
382f598f 1516 if (defq_len >= 3 && !((defq_len - 3) % 16))
8d6e79d3 1517 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
e0a05ebe 1518 return 0;
f9aa358a
JPM
1519}
1520
d999297c 1521/* tipc_link_rcv - process TIPC packets/messages arriving from off-node
f9aa358a 1522 * @l: the link that should handle the message
d999297c
JPM
1523 * @skb: TIPC packet
1524 * @xmitq: queue to place packets to be sent after this call
1525 */
1526int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1527 struct sk_buff_head *xmitq)
1528{
f9aa358a 1529 struct sk_buff_head *defq = &l->deferdq;
382f598f 1530 struct tipc_msg *hdr = buf_msg(skb);
81204c49 1531 u16 seqno, rcv_nxt, win_lim;
d999297c
JPM
1532 int rc = 0;
1533
382f598f
TL
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
f9aa358a 1541 do {
d999297c 1542 hdr = buf_msg(skb);
f9aa358a
JPM
1543 seqno = msg_seqno(hdr);
1544 rcv_nxt = l->rcv_nxt;
81204c49 1545 win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
d999297c 1546
662921cd 1547 if (unlikely(!link_is_up(l))) {
73f646ce
JPM
1548 if (l->state == LINK_ESTABLISHING)
1549 rc = TIPC_LINK_UP_EVT;
1550 goto drop;
d999297c
JPM
1551 }
1552
81204c49
JPM
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
d999297c
JPM
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
81204c49
JPM
1566 /* Defer delivery if sequence gap */
1567 if (unlikely(seqno != rcv_nxt)) {
8306f99a 1568 __tipc_skb_queue_sorted(defq, seqno, skb);
e0a05ebe 1569 rc |= tipc_link_build_nack_msg(l, xmitq);
f9aa358a 1570 break;
d999297c
JPM
1571 }
1572
81204c49 1573 /* Deliver packet */
d999297c 1574 l->rcv_nxt++;
95901122 1575 l->stats.recv_pkts++;
58ee86b8
TL
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);
f9aa358a 1581 if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
34b9cd64 1582 rc |= tipc_link_build_state_msg(l, xmitq);
02d11ca2 1583 if (unlikely(rc & ~TIPC_LINK_SND_STATE))
52666986 1584 break;
382f598f 1585 } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
f9aa358a
JPM
1586
1587 return rc;
1588drop:
1589 kfree_skb(skb);
d999297c
JPM
1590 return rc;
1591}
1592
426cc2b8 1593static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
8d6e79d3
JM
1594 bool probe_reply, u16 rcvgap,
1595 int tolerance, int priority,
426cc2b8
JPM
1596 struct sk_buff_head *xmitq)
1597{
02d11ca2 1598 struct tipc_link *bcl = l->bc_rcvlink;
e74a386d
JPM
1599 struct sk_buff *skb;
1600 struct tipc_msg *hdr;
1601 struct sk_buff_head *dfq = &l->deferdq;
02d11ca2 1602 bool node_up = link_is_up(bcl);
35c55c98
JPM
1603 struct tipc_mon_state *mstate = &l->mon_state;
1604 int dlen = 0;
1605 void *data;
9195948f 1606 u16 glen = 0;
426cc2b8
JPM
1607
1608 /* Don't send protocol message during reset or link failover */
662921cd 1609 if (tipc_link_is_blocked(l))
426cc2b8
JPM
1610 return;
1611
e74a386d
JPM
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,
9195948f
TL
1619 tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
1620 l->addr, tipc_own_addr(l->net), 0, 0, 0);
e74a386d
JPM
1621 if (!skb)
1622 return;
1623
1624 hdr = buf_msg(skb);
35c55c98 1625 data = msg_data(hdr);
e74a386d
JPM
1626 msg_set_session(hdr, l->session);
1627 msg_set_bearer_id(hdr, l->bearer_id);
426cc2b8 1628 msg_set_net_plane(hdr, l->net_plane);
52666986
JPM
1629 msg_set_next_sent(hdr, l->snd_nxt);
1630 msg_set_ack(hdr, l->rcv_nxt - 1);
02d11ca2 1631 msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
06bd2b1e 1632 msg_set_bc_ack_invalid(hdr, !node_up);
52666986 1633 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
426cc2b8
JPM
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);
52666986 1638 msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
426cc2b8
JPM
1639
1640 if (mtyp == STATE_MSG) {
9012de50
JM
1641 if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
1642 msg_set_seqno(hdr, l->snd_nxt_state++);
e74a386d 1643 msg_set_seq_gap(hdr, rcvgap);
02d11ca2 1644 msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
426cc2b8 1645 msg_set_probe(hdr, probe);
8d6e79d3 1646 msg_set_is_keepalive(hdr, probe || probe_reply);
9195948f
TL
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);
426cc2b8 1652 l->stats.sent_states++;
52666986 1653 l->rcv_unacked = 0;
426cc2b8
JPM
1654 } else {
1655 /* RESET_MSG or ACTIVATE_MSG */
91986ee1
TL
1656 if (mtyp == ACTIVATE_MSG) {
1657 msg_set_dest_session_valid(hdr, 1);
1658 msg_set_dest_session(hdr, l->peer_session);
1659 }
426cc2b8 1660 msg_set_max_pkt(hdr, l->advertised_mtu);
35c55c98
JPM
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);
426cc2b8 1664 }
e74a386d
JPM
1665 if (probe)
1666 l->stats.sent_probes++;
1667 if (rcvgap)
1668 l->stats.sent_nacks++;
426cc2b8 1669 skb->priority = TC_PRIO_CONTROL;
6e498158 1670 __skb_queue_tail(xmitq, skb);
26574db0 1671 trace_tipc_proto_build(skb, false, l->name);
b97bf3fd
PL
1672}
1673
c140eb16
LDC
1674void 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
e654f9f5 1683 __skb_queue_head_init(&tnlq);
c140eb16
LDC
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
6e498158 1704/* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
f9aa358a 1705 * with contents of the link's transmit and backlog queues.
b97bf3fd 1706 */
6e498158
JPM
1707void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1708 int mtyp, struct sk_buff_head *xmitq)
b97bf3fd 1709{
58ee86b8 1710 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
6e498158
JPM
1711 struct sk_buff *skb, *tnlskb;
1712 struct tipc_msg *hdr, tnlhdr;
1713 struct sk_buff_head *queue = &l->transmq;
2320bcda 1714 struct sk_buff_head tmpxq, tnlq, frags;
6e498158 1715 u16 pktlen, pktcnt, seqno = l->snd_nxt;
2320bcda 1716 bool pktcnt_need_update = false;
4929a932 1717 u16 syncpt;
2320bcda 1718 int rc;
b97bf3fd 1719
6e498158 1720 if (!tnl)
b97bf3fd
PL
1721 return;
1722
e654f9f5
JM
1723 __skb_queue_head_init(&tnlq);
1724 __skb_queue_head_init(&tmpxq);
1725 __skb_queue_head_init(&frags);
dd3f9e70 1726
6e498158
JPM
1727 /* At least one packet required for safe algorithm => add dummy */
1728 skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
e74a386d 1729 BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
6e498158
JPM
1730 0, 0, TIPC_ERR_NO_PORT);
1731 if (!skb) {
1732 pr_warn("%sunable to create tunnel packet\n", link_co_err);
b97bf3fd
PL
1733 return;
1734 }
e654f9f5 1735 __skb_queue_tail(&tnlq, skb);
6e498158
JPM
1736 tipc_link_xmit(l, &tnlq, &tmpxq);
1737 __skb_queue_purge(&tmpxq);
1738
4929a932
TL
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
6e498158 1764 /* Initialize reusable tunnel packet header */
e74a386d 1765 tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
6e498158 1766 mtyp, INT_H_SIZE, l->addr);
58ee86b8
TL
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);
6e498158
JPM
1772 msg_set_msgcnt(&tnlhdr, pktcnt);
1773 msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1774tnl:
1775 /* Wrap each packet into a tunnel packet */
05dcc5aa 1776 skb_queue_walk(queue, skb) {
6e498158
JPM
1777 hdr = buf_msg(skb);
1778 if (queue == &l->backlogq)
1779 msg_set_seqno(hdr, seqno++);
1780 pktlen = msg_size(hdr);
2320bcda
TL
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
6e498158 1814 msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
57d5f64d 1815 tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
6e498158
JPM
1816 if (!tnlskb) {
1817 pr_warn("%sunable to send packet\n", link_co_err);
b97bf3fd
PL
1818 return;
1819 }
6e498158
JPM
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);
b97bf3fd 1823 }
6e498158
JPM
1824 if (queue != &l->backlogq) {
1825 queue = &l->backlogq;
1826 goto tnl;
f006c9c7 1827 }
1dab3d5a 1828
2320bcda
TL
1829 if (pktcnt_need_update)
1830 skb_queue_walk(&tnlq, skb) {
1831 hdr = buf_msg(skb);
1832 msg_set_msgcnt(hdr, pktcnt);
1833 }
1834
6e498158 1835 tipc_link_xmit(tnl, &tnlq, xmitq);
b97bf3fd 1836
6e498158
JPM
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;
58ee86b8
TL
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);
2da71425 1849 }
b97bf3fd
PL
1850}
1851
c0b14a08
TL
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 */
1862void 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 */
c0b14a08
TL
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
7ea817f4
JM
1886/* tipc_link_validate_msg(): validate message against current link state
1887 * Returns true if message should be accepted, otherwise false
1888 */
1889bool 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;
d949cfed
LDC
1915 /* Extra sanity check */
1916 if (!link_is_up(l) && msg_ack(hdr))
1917 return false;
7ea817f4
JM
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
d999297c
JPM
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 */
1932static 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);
9195948f 1936 struct tipc_gap_ack_blks *ga = NULL;
d999297c 1937 u16 rcvgap = 0;
c1ab3f1d
JPM
1938 u16 ack = msg_ack(hdr);
1939 u16 gap = msg_seq_gap(hdr);
d999297c
JPM
1940 u16 peers_snd_nxt = msg_next_sent(hdr);
1941 u16 peers_tol = msg_link_tolerance(hdr);
1942 u16 peers_prio = msg_linkprio(hdr);
2be80c2d 1943 u16 rcv_nxt = l->rcv_nxt;
35c55c98 1944 u16 dlen = msg_data_sz(hdr);
73f646ce 1945 int mtyp = msg_type(hdr);
8d6e79d3 1946 bool reply = msg_probe(hdr);
9195948f 1947 u16 glen = 0;
35c55c98 1948 void *data;
d999297c
JPM
1949 char *if_name;
1950 int rc = 0;
1951
26574db0 1952 trace_tipc_proto_rcv(skb, false, l->name);
52666986 1953 if (tipc_link_is_blocked(l) || !xmitq)
d999297c
JPM
1954 goto exit;
1955
e74a386d 1956 if (tipc_own_addr(l->net) > msg_prevnode(hdr))
d999297c
JPM
1957 l->net_plane = msg_net_plane(hdr);
1958
35c55c98
JPM
1959 skb_linearize(skb);
1960 hdr = buf_msg(skb);
1961 data = msg_data(hdr);
1962
26574db0
TL
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)!");
7ea817f4 1966 goto exit;
26574db0 1967 }
7ea817f4 1968
73f646ce 1969 switch (mtyp) {
d999297c 1970 case RESET_MSG:
d999297c 1971 case ACTIVATE_MSG:
d999297c
JPM
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;
35c55c98 1978 strncpy(if_name, data, TIPC_MAX_IF_NAME);
d999297c
JPM
1979
1980 /* Update own tolerance if peer indicates a non-zero value */
047491ea 1981 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
d999297c 1982 l->tolerance = peers_tol;
047491ea
JM
1983 l->bc_rcvlink->tolerance = peers_tol;
1984 }
d999297c
JPM
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
7ab412d3
JM
1989 /* If peer is going down we want full re-establish cycle */
1990 if (msg_peer_stopping(hdr)) {
634696b1 1991 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
7ab412d3
JM
1992 break;
1993 }
91986ee1
TL
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
7ab412d3
JM
2005 /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
2006 if (mtyp == RESET_MSG || !link_is_up(l))
73f646ce
JPM
2007 rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
2008
2009 /* ACTIVATE_MSG takes up link if it was already locally reset */
7ab412d3 2010 if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
73f646ce
JPM
2011 rc = TIPC_LINK_UP_EVT;
2012
d999297c 2013 l->peer_session = msg_session(hdr);
7ea817f4 2014 l->in_session = true;
d999297c 2015 l->peer_bearer_id = msg_bearer_id(hdr);
d999297c
JPM
2016 if (l->mtu > msg_max_pkt(hdr))
2017 l->mtu = msg_max_pkt(hdr);
2018 break;
662921cd 2019
d999297c 2020 case STATE_MSG:
9012de50
JM
2021 l->rcv_nxt_state = msg_seqno(hdr) + 1;
2022
d999297c 2023 /* Update own tolerance if peer indicates a non-zero value */
047491ea 2024 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
d999297c 2025 l->tolerance = peers_tol;
047491ea
JM
2026 l->bc_rcvlink->tolerance = peers_tol;
2027 }
f7967556
JPM
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)) {
81729810
RA
2031 l->priority = peers_prio;
2032 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2033 }
2034
d999297c
JPM
2035 l->silent_intv_cnt = 0;
2036 l->stats.recv_states++;
2037 if (msg_probe(hdr))
2038 l->stats.recv_probes++;
73f646ce
JPM
2039
2040 if (!link_is_up(l)) {
2041 if (l->state == LINK_ESTABLISHING)
2042 rc = TIPC_LINK_UP_EVT;
d999297c 2043 break;
73f646ce 2044 }
9195948f
TL
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,
35c55c98 2056 &l->mon_state, l->bearer_id);
d999297c 2057
662921cd 2058 /* Send NACK if peer has sent pkts we haven't received yet */
2be80c2d 2059 if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
d999297c 2060 rcvgap = peers_snd_nxt - l->rcv_nxt;
8d6e79d3
JM
2061 if (rcvgap || reply)
2062 tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
2063 rcvgap, 0, 0, xmitq);
9195948f 2064
6a6b5c8b 2065 rc |= tipc_link_advance_transmq(l, ack, gap, ga, xmitq);
d999297c
JPM
2066
2067 /* If NACK, retransmit will now start at right position */
9195948f 2068 if (gap)
d999297c 2069 l->stats.recv_nacks++;
662921cd 2070
d999297c
JPM
2071 tipc_link_advance_backlog(l, xmitq);
2072 if (unlikely(!skb_queue_empty(&l->wakeupq)))
2073 link_prepare_wakeup(l);
2074 }
2075exit:
2076 kfree_skb(skb);
2077 return rc;
2078}
2079
52666986
JPM
2080/* tipc_link_build_bc_proto_msg() - create broadcast protocol message
2081 */
2082static 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,
e74a386d 2093 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
52666986
JPM
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 */
742e0383
WF
2113static void tipc_link_build_bc_init_msg(struct tipc_link *l,
2114 struct sk_buff_head *xmitq)
52666986
JPM
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;
06bd2b1e 2121 msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
52666986
JPM
2122 tipc_link_xmit(l, &list, xmitq);
2123}
2124
2125/* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
2126 */
2127void 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 */
02d11ca2
JPM
2154int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
2155 struct sk_buff_head *xmitq)
52666986 2156{
7c4a54b9 2157 struct tipc_link *snd_l = l->bc_sndlink;
52666986 2158 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
02d11ca2
JPM
2159 u16 from = msg_bcast_ack(hdr) + 1;
2160 u16 to = from + msg_bc_gap(hdr) - 1;
2161 int rc = 0;
52666986
JPM
2162
2163 if (!link_is_up(l))
02d11ca2 2164 return rc;
52666986
JPM
2165
2166 if (!msg_peer_node_is_up(hdr))
02d11ca2 2167 return rc;
52666986 2168
2d18ac4b
JPM
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)
02d11ca2 2174 return rc;
52666986 2175
7c4a54b9
JPM
2176 l->stats.recv_nacks++;
2177
52666986
JPM
2178 /* Ignore if peers_snd_nxt goes beyond receive window */
2179 if (more(peers_snd_nxt, l->rcv_nxt + l->window))
02d11ca2
JPM
2180 return rc;
2181
6a6b5c8b 2182 rc = tipc_link_bc_retrans(snd_l, l, from, to, xmitq);
02d11ca2
JPM
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 */
52666986
JPM
2193
2194 if (!more(peers_snd_nxt, l->rcv_nxt)) {
2195 l->nack_state = BC_NACK_SND_CONDITIONAL;
02d11ca2 2196 return 0;
52666986
JPM
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;
02d11ca2 2202 return 0;
52666986
JPM
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)
02d11ca2 2209 return 0;
52666986
JPM
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;
02d11ca2 2215 return 0;
52666986
JPM
2216}
2217
2218void 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
26574db0 2230 trace_tipc_link_bc_ack(l, l->acked, acked, &snd_l->transmq);
52666986
JPM
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
02d11ca2
JPM
2253 * This function is here for backwards compatibility, since
2254 * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
52666986
JPM
2255 */
2256int 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
e74a386d 2276 if (dnode == tipc_own_addr(l->net)) {
52666986 2277 tipc_link_bc_ack_rcv(l, acked, xmitq);
6a6b5c8b 2278 rc = tipc_link_bc_retrans(l->bc_sndlink, l, from, to, xmitq);
52666986
JPM
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
e3eea1eb 2290void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
b97bf3fd 2291{
218527fe 2292 int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
e3eea1eb
JPM
2293
2294 l->window = win;
5a0950c2
JPM
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);
1f66d161 2299 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
b97bf3fd
PL
2300}
2301
b97bf3fd 2302/**
38206d59 2303 * link_reset_stats - reset link statistics
1a90632d 2304 * @l: pointer to link
b97bf3fd 2305 */
38206d59 2306void tipc_link_reset_stats(struct tipc_link *l)
b97bf3fd 2307{
38206d59 2308 memset(&l->stats, 0, sizeof(l->stats));
b97bf3fd
PL
2309}
2310
1a20cc25 2311static void link_print(struct tipc_link *l, const char *str)
b97bf3fd 2312{
1a20cc25 2313 struct sk_buff *hskb = skb_peek(&l->transmq);
c1ab3f1d 2314 u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
1a20cc25 2315 u16 tail = l->snd_nxt - 1;
7a2f7d18 2316
662921cd 2317 pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
1a20cc25
JPM
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);
b97bf3fd 2321}
0655f6a8
RA
2322
2323/* Parse and validate nested (link) properties valid for media, bearer and link
2324 */
2325int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2326{
2327 int err;
2328
8cb08174
JB
2329 err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
2330 tipc_nl_prop_policy, NULL);
0655f6a8
RA
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}
7be57fc6 2360
d8182804 2361static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
7be57fc6
RA
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[] = {
95901122 2372 {TIPC_NLA_STATS_RX_INFO, 0},
7be57fc6
RA
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},
95901122 2377 {TIPC_NLA_STATS_TX_INFO, 0},
7be57fc6
RA
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
ae0be8de 2409 stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
7be57fc6
RA
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;
2420msg_full:
2421 nla_nest_cancel(skb, stats);
2422
2423 return -EMSGSIZE;
2424}
2425
2426/* Caller should hold appropriate locks to protect the link */
5be9c086
JPM
2427int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2428 struct tipc_link *link, int nlflags)
7be57fc6 2429{
23fd3eac 2430 u32 self = tipc_own_addr(net);
7be57fc6
RA
2431 struct nlattr *attrs;
2432 struct nlattr *prop;
23fd3eac
JM
2433 void *hdr;
2434 int err;
7be57fc6 2435
bfb3e5dd 2436 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
f2f67390 2437 nlflags, TIPC_NL_LINK_GET);
7be57fc6
RA
2438 if (!hdr)
2439 return -EMSGSIZE;
2440
ae0be8de 2441 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
7be57fc6
RA
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;
23fd3eac 2447 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
7be57fc6 2448 goto attr_msg_full;
ed193ece 2449 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
7be57fc6 2450 goto attr_msg_full;
95901122 2451 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
7be57fc6 2452 goto attr_msg_full;
95901122 2453 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
7be57fc6
RA
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;
c72fa872 2459 if (link->active)
7be57fc6
RA
2460 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2461 goto attr_msg_full;
2462
ae0be8de 2463 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
7be57fc6
RA
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,
1f66d161 2471 link->window))
7be57fc6
RA
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
2486prop_msg_full:
2487 nla_nest_cancel(msg->skb, prop);
2488attr_msg_full:
2489 nla_nest_cancel(msg->skb, attrs);
2490msg_full:
2491 genlmsg_cancel(msg->skb, hdr);
2492
2493 return -EMSGSIZE;
2494}
38206d59
JPM
2495
2496static 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[] = {
95901122 2508 {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
38206d59
JPM
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},
95901122 2513 {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
38206d59
JPM
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
ae0be8de 2530 nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
38206d59
JPM
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;
2541msg_full:
2542 nla_nest_cancel(skb, nest);
2543
2544 return -EMSGSIZE;
2545}
2546
2547int 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);
02ec6caf
HL
2554 u32 bc_mode = tipc_bcast_get_broadcast_mode(net);
2555 u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
38206d59
JPM
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);
b53ce3e7
IY
2565 if (!hdr) {
2566 tipc_bcast_unlock(net);
38206d59 2567 return -EMSGSIZE;
b53ce3e7 2568 }
38206d59 2569
ae0be8de 2570 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
38206d59
JPM
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;
95901122 2582 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
38206d59 2583 goto attr_msg_full;
95901122 2584 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
38206d59
JPM
2585 goto attr_msg_full;
2586
ae0be8de 2587 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
38206d59
JPM
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;
02ec6caf
HL
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;
38206d59
JPM
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
2610prop_msg_full:
2611 nla_nest_cancel(msg->skb, prop);
2612attr_msg_full:
2613 nla_nest_cancel(msg->skb, attrs);
2614msg_full:
2615 tipc_bcast_unlock(net);
2616 genlmsg_cancel(msg->skb, hdr);
2617
2618 return -EMSGSIZE;
2619}
2620
d01332f1
RA
2621void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
2622 struct sk_buff_head *xmitq)
38206d59
JPM
2623{
2624 l->tolerance = tol;
047491ea
JM
2625 if (l->bc_rcvlink)
2626 l->bc_rcvlink->tolerance = tol;
37c64cf6
JM
2627 if (link_is_up(l))
2628 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
38206d59
JPM
2629}
2630
d01332f1
RA
2631void tipc_link_set_prio(struct tipc_link *l, u32 prio,
2632 struct sk_buff_head *xmitq)
38206d59
JPM
2633{
2634 l->priority = prio;
8d6e79d3 2635 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
38206d59
JPM
2636}
2637
2638void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
2639{
2640 l->abort_limit = limit;
2641}
b4b9771b
TL
2642
2643char *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 */
2671int 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);
71204231 2696 i += scnprintf(buf + i, sz - i, " %u", 0);
77cf8edb 2697 i += scnprintf(buf + i, sz - i, " %u", 0);
b4b9771b
TL
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}