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