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