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