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