Merge tag 'mac80211-next-for-davem-2015-02-03' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / net / tipc / link.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/link.c: TIPC link code
c4307285 3 *
170b3927 4 * Copyright (c) 1996-2007, 2012-2014, 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"
b97bf3fd 38#include "link.h"
7be57fc6 39#include "bcast.h"
9816f061 40#include "socket.h"
b97bf3fd 41#include "name_distr.h"
b97bf3fd
PL
42#include "discover.h"
43#include "config.h"
0655f6a8 44#include "netlink.h"
b97bf3fd 45
796c75d0
YX
46#include <linux/pkt_sched.h>
47
2cf8aa19
EH
48/*
49 * Error message prefixes
50 */
51static const char *link_co_err = "Link changeover error, ";
52static const char *link_rst_msg = "Resetting link ";
53static const char *link_unk_evt = "Unknown link event ";
b97bf3fd 54
7be57fc6
RA
55static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
56 [TIPC_NLA_LINK_UNSPEC] = { .type = NLA_UNSPEC },
57 [TIPC_NLA_LINK_NAME] = {
58 .type = NLA_STRING,
59 .len = TIPC_MAX_LINK_NAME
60 },
61 [TIPC_NLA_LINK_MTU] = { .type = NLA_U32 },
62 [TIPC_NLA_LINK_BROADCAST] = { .type = NLA_FLAG },
63 [TIPC_NLA_LINK_UP] = { .type = NLA_FLAG },
64 [TIPC_NLA_LINK_ACTIVE] = { .type = NLA_FLAG },
65 [TIPC_NLA_LINK_PROP] = { .type = NLA_NESTED },
66 [TIPC_NLA_LINK_STATS] = { .type = NLA_NESTED },
67 [TIPC_NLA_LINK_RX] = { .type = NLA_U32 },
68 [TIPC_NLA_LINK_TX] = { .type = NLA_U32 }
69};
70
0655f6a8
RA
71/* Properties valid for media, bearar and link */
72static const struct nla_policy tipc_nl_prop_policy[TIPC_NLA_PROP_MAX + 1] = {
73 [TIPC_NLA_PROP_UNSPEC] = { .type = NLA_UNSPEC },
74 [TIPC_NLA_PROP_PRIO] = { .type = NLA_U32 },
75 [TIPC_NLA_PROP_TOL] = { .type = NLA_U32 },
76 [TIPC_NLA_PROP_WIN] = { .type = NLA_U32 }
77};
78
a686e685
AS
79/*
80 * Out-of-range value for link session numbers
81 */
a686e685
AS
82#define INVALID_SESSION 0x10000
83
c4307285
YH
84/*
85 * Link state events:
b97bf3fd 86 */
b97bf3fd
PL
87#define STARTING_EVT 856384768 /* link processing trigger */
88#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
89#define TIMEOUT_EVT 560817u /* link timer expired */
90
c4307285
YH
91/*
92 * The following two 'message types' is really just implementation
93 * data conveniently stored in the message header.
b97bf3fd
PL
94 * They must not be considered part of the protocol
95 */
96#define OPEN_MSG 0
97#define CLOSED_MSG 1
98
c4307285 99/*
b97bf3fd
PL
100 * State value stored in 'exp_msg_count'
101 */
b97bf3fd
PL
102#define START_CHANGEOVER 100000u
103
c93d3baa
YX
104static void link_handle_out_of_seq_msg(struct net *net,
105 struct tipc_link *l_ptr,
b97bf3fd 106 struct sk_buff *buf);
c93d3baa
YX
107static void tipc_link_proto_rcv(struct net *net, struct tipc_link *l_ptr,
108 struct sk_buff *buf);
109static int tipc_link_tunnel_rcv(struct net *net, struct tipc_node *n_ptr,
170b3927 110 struct sk_buff **buf);
2f55c437 111static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol);
a18c4bc3
PG
112static void link_state_event(struct tipc_link *l_ptr, u32 event);
113static void link_reset_statistics(struct tipc_link *l_ptr);
114static void link_print(struct tipc_link *l_ptr, const char *str);
247f0f3c
YX
115static void tipc_link_sync_xmit(struct tipc_link *l);
116static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
f2f9800d
YX
117static int tipc_link_input(struct net *net, struct tipc_link *l,
118 struct sk_buff *buf);
c93d3baa
YX
119static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
120 struct sk_buff **buf);
31e3c3f6 121
b97bf3fd 122/*
05790c64 123 * Simple link routines
b97bf3fd 124 */
05790c64 125static unsigned int align(unsigned int i)
b97bf3fd
PL
126{
127 return (i + 3) & ~3u;
128}
129
a18c4bc3 130static void link_init_max_pkt(struct tipc_link *l_ptr)
b97bf3fd 131{
7f9f95d9
YX
132 struct tipc_node *node = l_ptr->owner;
133 struct tipc_net *tn = net_generic(node->net, tipc_net_id);
7a2f7d18 134 struct tipc_bearer *b_ptr;
b97bf3fd 135 u32 max_pkt;
c4307285 136
7a2f7d18 137 rcu_read_lock();
7f9f95d9 138 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
7a2f7d18
YX
139 if (!b_ptr) {
140 rcu_read_unlock();
141 return;
142 }
143 max_pkt = (b_ptr->mtu & ~3);
144 rcu_read_unlock();
145
b97bf3fd
PL
146 if (max_pkt > MAX_MSG_SIZE)
147 max_pkt = MAX_MSG_SIZE;
148
c4307285 149 l_ptr->max_pkt_target = max_pkt;
b97bf3fd
PL
150 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
151 l_ptr->max_pkt = l_ptr->max_pkt_target;
c4307285 152 else
b97bf3fd
PL
153 l_ptr->max_pkt = MAX_PKT_DEFAULT;
154
c4307285 155 l_ptr->max_pkt_probes = 0;
b97bf3fd
PL
156}
157
b97bf3fd 158/*
05790c64 159 * Simple non-static link routines (i.e. referenced outside this file)
b97bf3fd 160 */
a18c4bc3 161int tipc_link_is_up(struct tipc_link *l_ptr)
b97bf3fd
PL
162{
163 if (!l_ptr)
164 return 0;
a02cec21 165 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
b97bf3fd
PL
166}
167
a18c4bc3 168int tipc_link_is_active(struct tipc_link *l_ptr)
b97bf3fd 169{
a02cec21
ED
170 return (l_ptr->owner->active_links[0] == l_ptr) ||
171 (l_ptr->owner->active_links[1] == l_ptr);
b97bf3fd
PL
172}
173
b97bf3fd
PL
174/**
175 * link_timeout - handle expiration of link timer
176 * @l_ptr: pointer to link
b97bf3fd 177 */
2f55c437 178static void link_timeout(unsigned long data)
b97bf3fd 179{
2f55c437 180 struct tipc_link *l_ptr = (struct tipc_link *)data;
58dc55f2
YX
181 struct sk_buff *skb;
182
4323add6 183 tipc_node_lock(l_ptr->owner);
b97bf3fd
PL
184
185 /* update counters used in statistical profiling of send traffic */
58dc55f2 186 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->outqueue);
b97bf3fd
PL
187 l_ptr->stats.queue_sz_counts++;
188
58dc55f2
YX
189 skb = skb_peek(&l_ptr->outqueue);
190 if (skb) {
191 struct tipc_msg *msg = buf_msg(skb);
b97bf3fd
PL
192 u32 length = msg_size(msg);
193
f64f9e71
JP
194 if ((msg_user(msg) == MSG_FRAGMENTER) &&
195 (msg_type(msg) == FIRST_FRAGMENT)) {
b97bf3fd
PL
196 length = msg_size(msg_get_wrapped(msg));
197 }
198 if (length) {
199 l_ptr->stats.msg_lengths_total += length;
200 l_ptr->stats.msg_length_counts++;
201 if (length <= 64)
202 l_ptr->stats.msg_length_profile[0]++;
203 else if (length <= 256)
204 l_ptr->stats.msg_length_profile[1]++;
205 else if (length <= 1024)
206 l_ptr->stats.msg_length_profile[2]++;
207 else if (length <= 4096)
208 l_ptr->stats.msg_length_profile[3]++;
209 else if (length <= 16384)
210 l_ptr->stats.msg_length_profile[4]++;
211 else if (length <= 32768)
212 l_ptr->stats.msg_length_profile[5]++;
213 else
214 l_ptr->stats.msg_length_profile[6]++;
215 }
216 }
217
218 /* do all other link processing performed on a periodic basis */
b97bf3fd
PL
219 link_state_event(l_ptr, TIMEOUT_EVT);
220
221 if (l_ptr->next_out)
47b4c9a8 222 tipc_link_push_packets(l_ptr);
b97bf3fd 223
4323add6 224 tipc_node_unlock(l_ptr->owner);
b97bf3fd
PL
225}
226
2f55c437 227static void link_set_timer(struct tipc_link *link, unsigned long time)
b97bf3fd 228{
2f55c437 229 mod_timer(&link->timer, jiffies + time);
b97bf3fd
PL
230}
231
232/**
4323add6 233 * tipc_link_create - create a new link
37b9c08a 234 * @n_ptr: pointer to associated node
b97bf3fd 235 * @b_ptr: pointer to associated bearer
b97bf3fd 236 * @media_addr: media address to use when sending messages over link
c4307285 237 *
b97bf3fd
PL
238 * Returns pointer to link.
239 */
a18c4bc3 240struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
c61dd61d
YX
241 struct tipc_bearer *b_ptr,
242 const struct tipc_media_addr *media_addr)
b97bf3fd 243{
34747539 244 struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
a18c4bc3 245 struct tipc_link *l_ptr;
b97bf3fd
PL
246 struct tipc_msg *msg;
247 char *if_name;
37b9c08a
AS
248 char addr_string[16];
249 u32 peer = n_ptr->addr;
250
0372bf5c 251 if (n_ptr->link_cnt >= MAX_BEARERS) {
37b9c08a 252 tipc_addr_string_fill(addr_string, n_ptr->addr);
0372bf5c
HB
253 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
254 n_ptr->link_cnt, addr_string, MAX_BEARERS);
37b9c08a
AS
255 return NULL;
256 }
257
258 if (n_ptr->links[b_ptr->identity]) {
259 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19
EH
260 pr_err("Attempt to establish second link on <%s> to %s\n",
261 b_ptr->name, addr_string);
37b9c08a
AS
262 return NULL;
263 }
b97bf3fd 264
0da974f4 265 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
b97bf3fd 266 if (!l_ptr) {
2cf8aa19 267 pr_warn("Link creation failed, no memory\n");
b97bf3fd
PL
268 return NULL;
269 }
b97bf3fd
PL
270
271 l_ptr->addr = peer;
2d627b92 272 if_name = strchr(b_ptr->name, ':') + 1;
062b4c99 273 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
34747539
YX
274 tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
275 tipc_node(tn->own_addr),
b97bf3fd
PL
276 if_name,
277 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
062b4c99 278 /* note: peer i/f name is updated by reset/activate message */
b97bf3fd 279 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
37b9c08a 280 l_ptr->owner = n_ptr;
b97bf3fd 281 l_ptr->checkpoint = 1;
f882cb76 282 l_ptr->peer_session = INVALID_SESSION;
7a2f7d18 283 l_ptr->bearer_id = b_ptr->identity;
5c216e1d 284 link_set_supervision_props(l_ptr, b_ptr->tolerance);
b97bf3fd
PL
285 l_ptr->state = RESET_UNKNOWN;
286
287 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
288 msg = l_ptr->pmsg;
34747539
YX
289 tipc_msg_init(n_ptr->net, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
290 l_ptr->addr);
b97bf3fd 291 msg_set_size(msg, sizeof(l_ptr->proto_msg));
bafa29e3 292 msg_set_session(msg, (tn->random & 0xffff));
b97bf3fd
PL
293 msg_set_bearer_id(msg, b_ptr->identity);
294 strcpy((char *)msg_data(msg), if_name);
295
296 l_ptr->priority = b_ptr->priority;
5c216e1d 297 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
b97bf3fd 298
7a2f7d18 299 l_ptr->net_plane = b_ptr->net_plane;
b97bf3fd
PL
300 link_init_max_pkt(l_ptr);
301
302 l_ptr->next_out_no = 1;
58dc55f2 303 __skb_queue_head_init(&l_ptr->outqueue);
bc6fecd4 304 __skb_queue_head_init(&l_ptr->deferred_queue);
340b6e59 305 skb_queue_head_init(&l_ptr->waiting_sks);
b97bf3fd
PL
306
307 link_reset_statistics(l_ptr);
308
37b9c08a 309 tipc_node_attach_link(n_ptr, l_ptr);
b97bf3fd 310
2f55c437 311 setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
581465fa
JPM
312
313 link_state_event(l_ptr, STARTING_EVT);
b97bf3fd 314
b97bf3fd
PL
315 return l_ptr;
316}
317
f2f9800d
YX
318void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
319 bool shutting_down)
8d8439b6 320{
f2f9800d 321 struct tipc_net *tn = net_generic(net, tipc_net_id);
8d8439b6 322 struct tipc_link *l_ptr;
c61dd61d 323 struct tipc_node *n_ptr;
8d8439b6 324
6c7a762e 325 rcu_read_lock();
f2f9800d 326 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
5356f3d7 327 tipc_node_lock(n_ptr);
c61dd61d
YX
328 l_ptr = n_ptr->links[bearer_id];
329 if (l_ptr) {
330 tipc_link_reset(l_ptr);
7d33939f
JPM
331 if (shutting_down || !tipc_node_is_up(n_ptr)) {
332 tipc_node_detach_link(l_ptr->owner, l_ptr);
333 tipc_link_reset_fragments(l_ptr);
5356f3d7 334 tipc_node_unlock(n_ptr);
7d33939f
JPM
335
336 /* Nobody else can access this link now: */
337 del_timer_sync(&l_ptr->timer);
338 kfree(l_ptr);
339 } else {
340 /* Detach/delete when failover is finished: */
341 l_ptr->flags |= LINK_STOPPED;
5356f3d7 342 tipc_node_unlock(n_ptr);
7d33939f
JPM
343 del_timer_sync(&l_ptr->timer);
344 }
c61dd61d
YX
345 continue;
346 }
5356f3d7 347 tipc_node_unlock(n_ptr);
8d8439b6 348 }
6c7a762e 349 rcu_read_unlock();
8d8439b6 350}
b97bf3fd
PL
351
352/**
50100a5e
JPM
353 * link_schedule_user - schedule user for wakeup after congestion
354 * @link: congested link
355 * @oport: sending port
356 * @chain_sz: size of buffer chain that was attempted sent
357 * @imp: importance of message attempted sent
358 * Create pseudo msg to send back to user when congestion abates
b97bf3fd 359 */
50100a5e
JPM
360static bool link_schedule_user(struct tipc_link *link, u32 oport,
361 uint chain_sz, uint imp)
b97bf3fd 362{
34747539
YX
363 struct net *net = link->owner->net;
364 struct tipc_net *tn = net_generic(net, tipc_net_id);
50100a5e
JPM
365 struct sk_buff *buf;
366
34747539
YX
367 buf = tipc_msg_create(net, SOCK_WAKEUP, 0, INT_H_SIZE, 0, tn->own_addr,
368 tn->own_addr, oport, 0, 0);
50100a5e
JPM
369 if (!buf)
370 return false;
371 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
372 TIPC_SKB_CB(buf)->chain_imp = imp;
340b6e59 373 skb_queue_tail(&link->waiting_sks, buf);
50100a5e
JPM
374 link->stats.link_congs++;
375 return true;
b97bf3fd
PL
376}
377
50100a5e
JPM
378/**
379 * link_prepare_wakeup - prepare users for wakeup after congestion
380 * @link: congested link
381 * Move a number of waiting users, as permitted by available space in
382 * the send queue, from link wait queue to node wait queue for wakeup
383 */
384static void link_prepare_wakeup(struct tipc_link *link)
b97bf3fd 385{
58dc55f2 386 uint pend_qsz = skb_queue_len(&link->outqueue);
58d78b32 387 struct sk_buff *skb, *tmp;
50100a5e 388
58d78b32
YX
389 skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
390 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
b97bf3fd 391 break;
58d78b32 392 pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
340b6e59
RA
393 skb_unlink(skb, &link->waiting_sks);
394 skb_queue_tail(&link->owner->waiting_sks, skb);
b97bf3fd 395 }
b97bf3fd
PL
396}
397
b97bf3fd 398/**
4323add6 399 * tipc_link_reset_fragments - purge link's inbound message fragments queue
b97bf3fd
PL
400 * @l_ptr: pointer to link
401 */
a18c4bc3 402void tipc_link_reset_fragments(struct tipc_link *l_ptr)
b97bf3fd 403{
37e22164
JPM
404 kfree_skb(l_ptr->reasm_buf);
405 l_ptr->reasm_buf = NULL;
b97bf3fd
PL
406}
407
c4307285 408/**
581465fa 409 * tipc_link_purge_queues - purge all pkt queues associated with link
b97bf3fd
PL
410 * @l_ptr: pointer to link
411 */
581465fa 412void tipc_link_purge_queues(struct tipc_link *l_ptr)
b97bf3fd 413{
bc6fecd4 414 __skb_queue_purge(&l_ptr->deferred_queue);
58dc55f2 415 __skb_queue_purge(&l_ptr->outqueue);
4323add6 416 tipc_link_reset_fragments(l_ptr);
b97bf3fd
PL
417}
418
a18c4bc3 419void tipc_link_reset(struct tipc_link *l_ptr)
b97bf3fd 420{
b97bf3fd
PL
421 u32 prev_state = l_ptr->state;
422 u32 checkpoint = l_ptr->next_in_no;
5392d646 423 int was_active_link = tipc_link_is_active(l_ptr);
50100a5e 424 struct tipc_node *owner = l_ptr->owner;
c4307285 425
a686e685 426 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
b97bf3fd 427
a686e685
AS
428 /* Link is down, accept any session */
429 l_ptr->peer_session = INVALID_SESSION;
b97bf3fd 430
c4307285 431 /* Prepare for max packet size negotiation */
b97bf3fd 432 link_init_max_pkt(l_ptr);
c4307285 433
b97bf3fd 434 l_ptr->state = RESET_UNKNOWN;
b97bf3fd
PL
435
436 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
437 return;
438
4323add6 439 tipc_node_link_down(l_ptr->owner, l_ptr);
7f9f95d9 440 tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
7368ddf1 441
b9d4c339 442 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
b97bf3fd
PL
443 l_ptr->reset_checkpoint = checkpoint;
444 l_ptr->exp_msg_count = START_CHANGEOVER;
445 }
446
447 /* Clean up all queues: */
58dc55f2 448 __skb_queue_purge(&l_ptr->outqueue);
bc6fecd4 449 __skb_queue_purge(&l_ptr->deferred_queue);
50100a5e
JPM
450 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
451 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
452 owner->action_flags |= TIPC_WAKEUP_USERS;
453 }
b97bf3fd
PL
454 l_ptr->next_out = NULL;
455 l_ptr->unacked_window = 0;
456 l_ptr->checkpoint = 1;
457 l_ptr->next_out_no = 1;
b97bf3fd
PL
458 l_ptr->fsm_msg_cnt = 0;
459 l_ptr->stale_count = 0;
460 link_reset_statistics(l_ptr);
b97bf3fd
PL
461}
462
f2f9800d 463void tipc_link_reset_list(struct net *net, unsigned int bearer_id)
e0ca2c30 464{
f2f9800d 465 struct tipc_net *tn = net_generic(net, tipc_net_id);
e0ca2c30 466 struct tipc_link *l_ptr;
c61dd61d 467 struct tipc_node *n_ptr;
e0ca2c30 468
6c7a762e 469 rcu_read_lock();
f2f9800d 470 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
5356f3d7 471 tipc_node_lock(n_ptr);
c61dd61d
YX
472 l_ptr = n_ptr->links[bearer_id];
473 if (l_ptr)
474 tipc_link_reset(l_ptr);
5356f3d7 475 tipc_node_unlock(n_ptr);
e0ca2c30 476 }
6c7a762e 477 rcu_read_unlock();
e0ca2c30 478}
b97bf3fd 479
7f9f95d9 480static void link_activate(struct tipc_link *link)
b97bf3fd 481{
7f9f95d9
YX
482 struct tipc_node *node = link->owner;
483
484 link->next_in_no = 1;
485 link->stats.recv_info = 1;
486 tipc_node_link_up(node, link);
487 tipc_bearer_add_dest(node->net, link->bearer_id, link->addr);
b97bf3fd
PL
488}
489
490/**
491 * link_state_event - link finite state machine
492 * @l_ptr: pointer to link
493 * @event: state machine event to process
494 */
95c96174 495static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
b97bf3fd 496{
a18c4bc3 497 struct tipc_link *other;
2f55c437 498 unsigned long cont_intv = l_ptr->cont_intv;
b97bf3fd 499
7d33939f
JPM
500 if (l_ptr->flags & LINK_STOPPED)
501 return;
502
135daee6 503 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
b97bf3fd
PL
504 return; /* Not yet. */
505
77a7e07a
YX
506 /* Check whether changeover is going on */
507 if (l_ptr->exp_msg_count) {
a016892c 508 if (event == TIMEOUT_EVT)
b97bf3fd 509 link_set_timer(l_ptr, cont_intv);
77a7e07a 510 return;
b97bf3fd 511 }
b97bf3fd
PL
512
513 switch (l_ptr->state) {
514 case WORKING_WORKING:
b97bf3fd
PL
515 switch (event) {
516 case TRAFFIC_MSG_EVT:
b97bf3fd 517 case ACTIVATE_MSG:
b97bf3fd
PL
518 break;
519 case TIMEOUT_EVT:
b97bf3fd
PL
520 if (l_ptr->next_in_no != l_ptr->checkpoint) {
521 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6 522 if (tipc_bclink_acks_missing(l_ptr->owner)) {
247f0f3c
YX
523 tipc_link_proto_xmit(l_ptr, STATE_MSG,
524 0, 0, 0, 0, 0);
b97bf3fd
PL
525 l_ptr->fsm_msg_cnt++;
526 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
247f0f3c
YX
527 tipc_link_proto_xmit(l_ptr, STATE_MSG,
528 1, 0, 0, 0, 0);
b97bf3fd
PL
529 l_ptr->fsm_msg_cnt++;
530 }
531 link_set_timer(l_ptr, cont_intv);
532 break;
533 }
b97bf3fd
PL
534 l_ptr->state = WORKING_UNKNOWN;
535 l_ptr->fsm_msg_cnt = 0;
247f0f3c 536 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
537 l_ptr->fsm_msg_cnt++;
538 link_set_timer(l_ptr, cont_intv / 4);
539 break;
540 case RESET_MSG:
3fa9cacd
EH
541 pr_debug("%s<%s>, requested by peer\n",
542 link_rst_msg, l_ptr->name);
4323add6 543 tipc_link_reset(l_ptr);
b97bf3fd
PL
544 l_ptr->state = RESET_RESET;
545 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
546 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
547 0, 0, 0, 0, 0);
b97bf3fd
PL
548 l_ptr->fsm_msg_cnt++;
549 link_set_timer(l_ptr, cont_intv);
550 break;
551 default:
3fa9cacd 552 pr_debug("%s%u in WW state\n", link_unk_evt, event);
b97bf3fd
PL
553 }
554 break;
555 case WORKING_UNKNOWN:
b97bf3fd
PL
556 switch (event) {
557 case TRAFFIC_MSG_EVT:
b97bf3fd 558 case ACTIVATE_MSG:
b97bf3fd
PL
559 l_ptr->state = WORKING_WORKING;
560 l_ptr->fsm_msg_cnt = 0;
561 link_set_timer(l_ptr, cont_intv);
562 break;
563 case RESET_MSG:
3fa9cacd
EH
564 pr_debug("%s<%s>, requested by peer while probing\n",
565 link_rst_msg, l_ptr->name);
4323add6 566 tipc_link_reset(l_ptr);
b97bf3fd
PL
567 l_ptr->state = RESET_RESET;
568 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
569 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
570 0, 0, 0, 0, 0);
b97bf3fd
PL
571 l_ptr->fsm_msg_cnt++;
572 link_set_timer(l_ptr, cont_intv);
573 break;
574 case TIMEOUT_EVT:
b97bf3fd 575 if (l_ptr->next_in_no != l_ptr->checkpoint) {
b97bf3fd
PL
576 l_ptr->state = WORKING_WORKING;
577 l_ptr->fsm_msg_cnt = 0;
578 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6 579 if (tipc_bclink_acks_missing(l_ptr->owner)) {
247f0f3c
YX
580 tipc_link_proto_xmit(l_ptr, STATE_MSG,
581 0, 0, 0, 0, 0);
b97bf3fd
PL
582 l_ptr->fsm_msg_cnt++;
583 }
584 link_set_timer(l_ptr, cont_intv);
585 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
247f0f3c
YX
586 tipc_link_proto_xmit(l_ptr, STATE_MSG,
587 1, 0, 0, 0, 0);
b97bf3fd
PL
588 l_ptr->fsm_msg_cnt++;
589 link_set_timer(l_ptr, cont_intv / 4);
590 } else { /* Link has failed */
3fa9cacd
EH
591 pr_debug("%s<%s>, peer not responding\n",
592 link_rst_msg, l_ptr->name);
4323add6 593 tipc_link_reset(l_ptr);
b97bf3fd
PL
594 l_ptr->state = RESET_UNKNOWN;
595 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
596 tipc_link_proto_xmit(l_ptr, RESET_MSG,
597 0, 0, 0, 0, 0);
b97bf3fd
PL
598 l_ptr->fsm_msg_cnt++;
599 link_set_timer(l_ptr, cont_intv);
600 }
601 break;
602 default:
2cf8aa19 603 pr_err("%s%u in WU state\n", link_unk_evt, event);
b97bf3fd
PL
604 }
605 break;
606 case RESET_UNKNOWN:
b97bf3fd
PL
607 switch (event) {
608 case TRAFFIC_MSG_EVT:
b97bf3fd
PL
609 break;
610 case ACTIVATE_MSG:
611 other = l_ptr->owner->active_links[0];
8d64a5ba 612 if (other && link_working_unknown(other))
b97bf3fd 613 break;
b97bf3fd
PL
614 l_ptr->state = WORKING_WORKING;
615 l_ptr->fsm_msg_cnt = 0;
616 link_activate(l_ptr);
247f0f3c 617 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd 618 l_ptr->fsm_msg_cnt++;
c64f7a6a 619 if (l_ptr->owner->working_links == 1)
247f0f3c 620 tipc_link_sync_xmit(l_ptr);
b97bf3fd
PL
621 link_set_timer(l_ptr, cont_intv);
622 break;
623 case RESET_MSG:
b97bf3fd
PL
624 l_ptr->state = RESET_RESET;
625 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
626 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
627 1, 0, 0, 0, 0);
b97bf3fd
PL
628 l_ptr->fsm_msg_cnt++;
629 link_set_timer(l_ptr, cont_intv);
630 break;
631 case STARTING_EVT:
135daee6 632 l_ptr->flags |= LINK_STARTED;
b97bf3fd
PL
633 /* fall through */
634 case TIMEOUT_EVT:
247f0f3c 635 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
636 l_ptr->fsm_msg_cnt++;
637 link_set_timer(l_ptr, cont_intv);
638 break;
639 default:
2cf8aa19 640 pr_err("%s%u in RU state\n", link_unk_evt, event);
b97bf3fd
PL
641 }
642 break;
643 case RESET_RESET:
b97bf3fd
PL
644 switch (event) {
645 case TRAFFIC_MSG_EVT:
b97bf3fd
PL
646 case ACTIVATE_MSG:
647 other = l_ptr->owner->active_links[0];
8d64a5ba 648 if (other && link_working_unknown(other))
b97bf3fd 649 break;
b97bf3fd
PL
650 l_ptr->state = WORKING_WORKING;
651 l_ptr->fsm_msg_cnt = 0;
652 link_activate(l_ptr);
247f0f3c 653 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd 654 l_ptr->fsm_msg_cnt++;
c64f7a6a 655 if (l_ptr->owner->working_links == 1)
247f0f3c 656 tipc_link_sync_xmit(l_ptr);
b97bf3fd
PL
657 link_set_timer(l_ptr, cont_intv);
658 break;
659 case RESET_MSG:
b97bf3fd
PL
660 break;
661 case TIMEOUT_EVT:
247f0f3c
YX
662 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
663 0, 0, 0, 0, 0);
b97bf3fd
PL
664 l_ptr->fsm_msg_cnt++;
665 link_set_timer(l_ptr, cont_intv);
b97bf3fd
PL
666 break;
667 default:
2cf8aa19 668 pr_err("%s%u in RR state\n", link_unk_evt, event);
b97bf3fd
PL
669 }
670 break;
671 default:
2cf8aa19 672 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
b97bf3fd
PL
673 }
674}
675
4f1688b2
JPM
676/* tipc_link_cong: determine return value and how to treat the
677 * sent buffer during link congestion.
678 * - For plain, errorless user data messages we keep the buffer and
679 * return -ELINKONG.
680 * - For all other messages we discard the buffer and return -EHOSTUNREACH
681 * - For TIPC internal messages we also reset the link
682 */
a6ca1094 683static int tipc_link_cong(struct tipc_link *link, struct sk_buff_head *list)
4f1688b2 684{
a6ca1094
YX
685 struct sk_buff *skb = skb_peek(list);
686 struct tipc_msg *msg = buf_msg(skb);
4f1688b2
JPM
687 uint imp = tipc_msg_tot_importance(msg);
688 u32 oport = msg_tot_origport(msg);
689
50100a5e 690 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
4f1688b2
JPM
691 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
692 tipc_link_reset(link);
50100a5e 693 goto drop;
4f1688b2 694 }
50100a5e
JPM
695 if (unlikely(msg_errcode(msg)))
696 goto drop;
697 if (unlikely(msg_reroute_cnt(msg)))
698 goto drop;
a6ca1094 699 if (TIPC_SKB_CB(skb)->wakeup_pending)
50100a5e 700 return -ELINKCONG;
a6ca1094 701 if (link_schedule_user(link, oport, skb_queue_len(list), imp))
50100a5e
JPM
702 return -ELINKCONG;
703drop:
a6ca1094 704 __skb_queue_purge(list);
4f1688b2
JPM
705 return -EHOSTUNREACH;
706}
707
708/**
9fbfb8b1 709 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
4f1688b2 710 * @link: link to use
a6ca1094
YX
711 * @list: chain of buffers containing message
712 *
4f1688b2
JPM
713 * Consumes the buffer chain, except when returning -ELINKCONG
714 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
715 * user data messages) or -EHOSTUNREACH (all other messages/senders)
716 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
717 * to act on the return value, since they may need to do more send attempts.
718 */
7f9f95d9
YX
719int __tipc_link_xmit(struct net *net, struct tipc_link *link,
720 struct sk_buff_head *list)
4f1688b2 721{
a6ca1094 722 struct tipc_msg *msg = buf_msg(skb_peek(list));
4f1688b2 723 uint psz = msg_size(msg);
4f1688b2
JPM
724 uint sndlim = link->queue_limit[0];
725 uint imp = tipc_msg_tot_importance(msg);
726 uint mtu = link->max_pkt;
727 uint ack = mod(link->next_in_no - 1);
728 uint seqno = link->next_out_no;
729 uint bc_last_in = link->owner->bclink.last_in;
730 struct tipc_media_addr *addr = &link->media_addr;
58dc55f2 731 struct sk_buff_head *outqueue = &link->outqueue;
a6ca1094 732 struct sk_buff *skb, *tmp;
4f1688b2
JPM
733
734 /* Match queue limits against msg importance: */
58dc55f2 735 if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp]))
a6ca1094 736 return tipc_link_cong(link, list);
4f1688b2
JPM
737
738 /* Has valid packet limit been used ? */
739 if (unlikely(psz > mtu)) {
a6ca1094 740 __skb_queue_purge(list);
4f1688b2
JPM
741 return -EMSGSIZE;
742 }
743
744 /* Prepare each packet for sending, and add to outqueue: */
a6ca1094
YX
745 skb_queue_walk_safe(list, skb, tmp) {
746 __skb_unlink(skb, list);
58dc55f2 747 msg = buf_msg(skb);
4f1688b2
JPM
748 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
749 msg_set_bcast_ack(msg, bc_last_in);
750
58dc55f2
YX
751 if (skb_queue_len(outqueue) < sndlim) {
752 __skb_queue_tail(outqueue, skb);
7f9f95d9
YX
753 tipc_bearer_send(net, link->bearer_id,
754 skb, addr);
58dc55f2
YX
755 link->next_out = NULL;
756 link->unacked_window = 0;
757 } else if (tipc_msg_bundle(outqueue, skb, mtu)) {
4f1688b2 758 link->stats.sent_bundled++;
4f1688b2 759 continue;
34747539 760 } else if (tipc_msg_make_bundle(net, outqueue, skb, mtu,
58dc55f2 761 link->addr)) {
4f1688b2
JPM
762 link->stats.sent_bundled++;
763 link->stats.sent_bundles++;
4f1688b2 764 if (!link->next_out)
58dc55f2 765 link->next_out = skb_peek_tail(outqueue);
4f1688b2 766 } else {
58dc55f2 767 __skb_queue_tail(outqueue, skb);
4f1688b2 768 if (!link->next_out)
58dc55f2 769 link->next_out = skb;
4f1688b2
JPM
770 }
771 seqno++;
4f1688b2
JPM
772 }
773 link->next_out_no = seqno;
4f1688b2
JPM
774 return 0;
775}
776
a6ca1094
YX
777static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
778{
779 __skb_queue_head_init(list);
780 __skb_queue_tail(list, skb);
781}
782
783static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
784{
785 struct sk_buff_head head;
786
787 skb2list(skb, &head);
7f9f95d9 788 return __tipc_link_xmit(link->owner->net, link, &head);
a6ca1094
YX
789}
790
f2f9800d
YX
791int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
792 u32 selector)
a6ca1094
YX
793{
794 struct sk_buff_head head;
795
796 skb2list(skb, &head);
f2f9800d 797 return tipc_link_xmit(net, &head, dnode, selector);
a6ca1094
YX
798}
799
4f1688b2 800/**
9fbfb8b1 801 * tipc_link_xmit() is the general link level function for message sending
f2f9800d 802 * @net: the applicable net namespace
a6ca1094 803 * @list: chain of buffers containing message
4f1688b2
JPM
804 * @dsz: amount of user data to be sent
805 * @dnode: address of destination node
806 * @selector: a number used for deterministic link selection
807 * Consumes the buffer chain, except when returning -ELINKCONG
808 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
809 */
f2f9800d
YX
810int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
811 u32 selector)
4f1688b2
JPM
812{
813 struct tipc_link *link = NULL;
814 struct tipc_node *node;
815 int rc = -EHOSTUNREACH;
816
f2f9800d 817 node = tipc_node_find(net, dnode);
4f1688b2
JPM
818 if (node) {
819 tipc_node_lock(node);
820 link = node->active_links[selector & 1];
821 if (link)
7f9f95d9 822 rc = __tipc_link_xmit(net, link, list);
4f1688b2
JPM
823 tipc_node_unlock(node);
824 }
825
826 if (link)
827 return rc;
828
34747539 829 if (likely(in_own_node(net, dnode))) {
a6ca1094
YX
830 /* As a node local message chain never contains more than one
831 * buffer, we just need to dequeue one SKB buffer from the
832 * head list.
833 */
f2f9800d 834 return tipc_sk_rcv(net, __skb_dequeue(list));
a6ca1094
YX
835 }
836 __skb_queue_purge(list);
4f1688b2 837
4f1688b2
JPM
838 return rc;
839}
840
c64f7a6a 841/*
247f0f3c 842 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
c64f7a6a
JM
843 *
844 * Give a newly added peer node the sequence number where it should
845 * start receiving and acking broadcast packets.
846 *
847 * Called with node locked
848 */
25b660c7 849static void tipc_link_sync_xmit(struct tipc_link *link)
c64f7a6a 850{
a6ca1094 851 struct sk_buff *skb;
c64f7a6a
JM
852 struct tipc_msg *msg;
853
a6ca1094
YX
854 skb = tipc_buf_acquire(INT_H_SIZE);
855 if (!skb)
c64f7a6a
JM
856 return;
857
a6ca1094 858 msg = buf_msg(skb);
34747539
YX
859 tipc_msg_init(link->owner->net, msg, BCAST_PROTOCOL, STATE_MSG,
860 INT_H_SIZE, link->addr);
25b660c7 861 msg_set_last_bcast(msg, link->owner->bclink.acked);
a6ca1094 862 __tipc_link_xmit_skb(link, skb);
c64f7a6a
JM
863}
864
865/*
247f0f3c 866 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
c64f7a6a
JM
867 * Receive the sequence number where we should start receiving and
868 * acking broadcast packets from a newly added peer node, and open
869 * up for reception of such packets.
870 *
871 * Called with node locked
872 */
247f0f3c 873static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
c64f7a6a
JM
874{
875 struct tipc_msg *msg = buf_msg(buf);
876
877 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
878 n->bclink.recv_permitted = true;
879 kfree_skb(buf);
880}
881
58dc55f2
YX
882struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
883 const struct sk_buff *skb)
884{
885 if (skb_queue_is_last(list, skb))
886 return NULL;
887 return skb->next;
888}
889
c4307285 890/*
47b4c9a8
YX
891 * tipc_link_push_packets - push unsent packets to bearer
892 *
893 * Push out the unsent messages of a link where congestion
894 * has abated. Node is locked.
895 *
896 * Called with node locked
b97bf3fd 897 */
47b4c9a8 898void tipc_link_push_packets(struct tipc_link *l_ptr)
b97bf3fd 899{
58dc55f2
YX
900 struct sk_buff_head *outqueue = &l_ptr->outqueue;
901 struct sk_buff *skb = l_ptr->next_out;
47b4c9a8
YX
902 struct tipc_msg *msg;
903 u32 next, first;
b97bf3fd 904
58dc55f2 905 skb_queue_walk_from(outqueue, skb) {
47b4c9a8
YX
906 msg = buf_msg(skb);
907 next = msg_seqno(msg);
58dc55f2 908 first = buf_seqno(skb_peek(outqueue));
b97bf3fd
PL
909
910 if (mod(next - first) < l_ptr->queue_limit[0]) {
911 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 912 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
3c294cb3 913 if (msg_user(msg) == MSG_BUNDLER)
58311d16 914 TIPC_SKB_CB(skb)->bundling = false;
7f9f95d9
YX
915 tipc_bearer_send(l_ptr->owner->net,
916 l_ptr->bearer_id, skb,
47b4c9a8 917 &l_ptr->media_addr);
58dc55f2 918 l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
47b4c9a8
YX
919 } else {
920 break;
b97bf3fd
PL
921 }
922 }
b97bf3fd
PL
923}
924
3f5a12bd 925void tipc_link_reset_all(struct tipc_node *node)
d356eeba 926{
d356eeba
AS
927 char addr_string[16];
928 u32 i;
929
3f5a12bd 930 tipc_node_lock(node);
d356eeba 931
2cf8aa19 932 pr_warn("Resetting all links to %s\n",
3f5a12bd 933 tipc_addr_string_fill(addr_string, node->addr));
d356eeba
AS
934
935 for (i = 0; i < MAX_BEARERS; i++) {
3f5a12bd
YX
936 if (node->links[i]) {
937 link_print(node->links[i], "Resetting link\n");
938 tipc_link_reset(node->links[i]);
d356eeba
AS
939 }
940 }
941
3f5a12bd 942 tipc_node_unlock(node);
d356eeba
AS
943}
944
a18c4bc3 945static void link_retransmit_failure(struct tipc_link *l_ptr,
ae8509c4 946 struct sk_buff *buf)
d356eeba
AS
947{
948 struct tipc_msg *msg = buf_msg(buf);
1da46568 949 struct net *net = l_ptr->owner->net;
d356eeba 950
2cf8aa19 951 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
d356eeba
AS
952
953 if (l_ptr->addr) {
d356eeba 954 /* Handle failure on standard link */
8d64a5ba 955 link_print(l_ptr, "Resetting link\n");
d356eeba
AS
956 tipc_link_reset(l_ptr);
957
958 } else {
d356eeba 959 /* Handle failure on broadcast link */
6c00055a 960 struct tipc_node *n_ptr;
d356eeba
AS
961 char addr_string[16];
962
2cf8aa19
EH
963 pr_info("Msg seq number: %u, ", msg_seqno(msg));
964 pr_cont("Outstanding acks: %lu\n",
965 (unsigned long) TIPC_SKB_CB(buf)->handle);
617dbeaa 966
1da46568 967 n_ptr = tipc_bclink_retransmit_to(net);
d356eeba
AS
968 tipc_node_lock(n_ptr);
969
c68ca7b7 970 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19 971 pr_info("Broadcast link info for %s\n", addr_string);
389dd9bc
YX
972 pr_info("Reception permitted: %d, Acked: %u\n",
973 n_ptr->bclink.recv_permitted,
2cf8aa19
EH
974 n_ptr->bclink.acked);
975 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
976 n_ptr->bclink.last_in,
977 n_ptr->bclink.oos_state,
978 n_ptr->bclink.last_sent);
d356eeba 979
d356eeba
AS
980 tipc_node_unlock(n_ptr);
981
1da46568 982 tipc_bclink_set_flags(net, TIPC_BCLINK_RESET);
d356eeba
AS
983 l_ptr->stale_count = 0;
984 }
985}
986
58dc55f2 987void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
4323add6 988 u32 retransmits)
b97bf3fd
PL
989{
990 struct tipc_msg *msg;
991
58dc55f2 992 if (!skb)
d356eeba
AS
993 return;
994
58dc55f2 995 msg = buf_msg(skb);
c4307285 996
512137ee
EH
997 /* Detect repeated retransmit failures */
998 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
999 if (++l_ptr->stale_count > 100) {
58dc55f2 1000 link_retransmit_failure(l_ptr, skb);
512137ee 1001 return;
d356eeba
AS
1002 }
1003 } else {
512137ee
EH
1004 l_ptr->last_retransmitted = msg_seqno(msg);
1005 l_ptr->stale_count = 1;
b97bf3fd 1006 }
d356eeba 1007
58dc55f2
YX
1008 skb_queue_walk_from(&l_ptr->outqueue, skb) {
1009 if (!retransmits || skb == l_ptr->next_out)
1010 break;
1011 msg = buf_msg(skb);
b97bf3fd 1012 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 1013 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
7f9f95d9
YX
1014 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
1015 &l_ptr->media_addr);
3c294cb3
YX
1016 retransmits--;
1017 l_ptr->stats.retransmitted++;
b97bf3fd 1018 }
b97bf3fd
PL
1019}
1020
f03273f1
YX
1021static void link_retrieve_defq(struct tipc_link *link,
1022 struct sk_buff_head *list)
b97bf3fd
PL
1023{
1024 u32 seq_no;
1025
f03273f1
YX
1026 if (skb_queue_empty(&link->deferred_queue))
1027 return;
1028
1029 seq_no = buf_seqno(skb_peek(&link->deferred_queue));
1030 if (seq_no == mod(link->next_in_no))
1031 skb_queue_splice_tail_init(&link->deferred_queue, list);
b97bf3fd
PL
1032}
1033
85035568
AS
1034/**
1035 * link_recv_buf_validate - validate basic format of received message
1036 *
1037 * This routine ensures a TIPC message has an acceptable header, and at least
1038 * as much data as the header indicates it should. The routine also ensures
1039 * that the entire message header is stored in the main fragment of the message
1040 * buffer, to simplify future access to message header fields.
1041 *
1042 * Note: Having extra info present in the message header or data areas is OK.
1043 * TIPC will ignore the excess, under the assumption that it is optional info
1044 * introduced by a later release of the protocol.
1045 */
85035568
AS
1046static int link_recv_buf_validate(struct sk_buff *buf)
1047{
1048 static u32 min_data_hdr_size[8] = {
741d9eb7 1049 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
85035568
AS
1050 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1051 };
1052
1053 struct tipc_msg *msg;
1054 u32 tipc_hdr[2];
1055 u32 size;
1056 u32 hdr_size;
1057 u32 min_hdr_size;
1058
64380a04
EH
1059 /* If this packet comes from the defer queue, the skb has already
1060 * been validated
1061 */
1062 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1063 return 1;
1064
85035568
AS
1065 if (unlikely(buf->len < MIN_H_SIZE))
1066 return 0;
1067
1068 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1069 if (msg == NULL)
1070 return 0;
1071
1072 if (unlikely(msg_version(msg) != TIPC_VERSION))
1073 return 0;
1074
1075 size = msg_size(msg);
1076 hdr_size = msg_hdr_sz(msg);
1077 min_hdr_size = msg_isdata(msg) ?
1078 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1079
1080 if (unlikely((hdr_size < min_hdr_size) ||
1081 (size < hdr_size) ||
1082 (buf->len < size) ||
1083 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1084 return 0;
1085
1086 return pskb_may_pull(buf, hdr_size);
1087}
1088
b02b69c8 1089/**
170b3927 1090 * tipc_rcv - process TIPC packets/messages arriving from off-node
f2f9800d 1091 * @net: the applicable net namespace
f03273f1 1092 * @skb: TIPC packet
7a2f7d18 1093 * @b_ptr: pointer to bearer message arrived on
b02b69c8
AS
1094 *
1095 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1096 * structure (i.e. cannot be NULL), but bearer can be inactive.
1097 */
c93d3baa 1098void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
b97bf3fd 1099{
34747539 1100 struct tipc_net *tn = net_generic(net, tipc_net_id);
f03273f1
YX
1101 struct sk_buff_head head;
1102 struct tipc_node *n_ptr;
1103 struct tipc_link *l_ptr;
1104 struct sk_buff *skb1, *tmp;
1105 struct tipc_msg *msg;
1106 u32 seq_no;
1107 u32 ackd;
1108 u32 released;
b97bf3fd 1109
a6ca1094 1110 skb2list(skb, &head);
85035568 1111
f03273f1 1112 while ((skb = __skb_dequeue(&head))) {
85035568 1113 /* Ensure message is well-formed */
f03273f1 1114 if (unlikely(!link_recv_buf_validate(skb)))
3af390e2 1115 goto discard;
b97bf3fd 1116
fe13dda2 1117 /* Ensure message data is a single contiguous unit */
f03273f1 1118 if (unlikely(skb_linearize(skb)))
3af390e2 1119 goto discard;
fe13dda2 1120
85035568 1121 /* Handle arrival of a non-unicast link message */
f03273f1 1122 msg = buf_msg(skb);
85035568 1123
b97bf3fd 1124 if (unlikely(msg_non_seq(msg))) {
1265a021 1125 if (msg_user(msg) == LINK_CONFIG)
c93d3baa 1126 tipc_disc_rcv(net, skb, b_ptr);
1265a021 1127 else
c93d3baa 1128 tipc_bclink_rcv(net, skb);
b97bf3fd
PL
1129 continue;
1130 }
c4307285 1131
ed33a9c4 1132 /* Discard unicast link messages destined for another node */
26008247 1133 if (unlikely(!msg_short(msg) &&
34747539 1134 (msg_destnode(msg) != tn->own_addr)))
3af390e2 1135 goto discard;
c4307285 1136
5a68d5ee 1137 /* Locate neighboring node that sent message */
f2f9800d 1138 n_ptr = tipc_node_find(net, msg_prevnode(msg));
b97bf3fd 1139 if (unlikely(!n_ptr))
3af390e2 1140 goto discard;
4323add6 1141 tipc_node_lock(n_ptr);
85035568 1142
b4b56102 1143 /* Locate unicast link endpoint that should handle message */
b4b56102 1144 l_ptr = n_ptr->links[b_ptr->identity];
3af390e2
YX
1145 if (unlikely(!l_ptr))
1146 goto unlock_discard;
5a68d5ee 1147
b4b56102 1148 /* Verify that communication with node is currently allowed */
aecb9bb8 1149 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
10f465c4
YX
1150 msg_user(msg) == LINK_PROTOCOL &&
1151 (msg_type(msg) == RESET_MSG ||
1152 msg_type(msg) == ACTIVATE_MSG) &&
1153 !msg_redundant_link(msg))
aecb9bb8 1154 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
10f465c4
YX
1155
1156 if (tipc_node_blocked(n_ptr))
3af390e2 1157 goto unlock_discard;
85035568
AS
1158
1159 /* Validate message sequence number info */
85035568
AS
1160 seq_no = msg_seqno(msg);
1161 ackd = msg_ack(msg);
1162
1163 /* Release acked messages */
389dd9bc 1164 if (n_ptr->bclink.recv_permitted)
36559591 1165 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
b97bf3fd 1166
58dc55f2
YX
1167 released = 0;
1168 skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) {
1169 if (skb1 == l_ptr->next_out ||
1170 more(buf_seqno(skb1), ackd))
1171 break;
1172 __skb_unlink(skb1, &l_ptr->outqueue);
1173 kfree_skb(skb1);
1174 released = 1;
b97bf3fd 1175 }
85035568
AS
1176
1177 /* Try sending any messages link endpoint has pending */
b97bf3fd 1178 if (unlikely(l_ptr->next_out))
47b4c9a8 1179 tipc_link_push_packets(l_ptr);
a5377831 1180
50100a5e
JPM
1181 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1182 link_prepare_wakeup(l_ptr);
1183 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1184 }
a5377831 1185
a5377831 1186 /* Process the incoming packet */
3af390e2
YX
1187 if (unlikely(!link_working_working(l_ptr))) {
1188 if (msg_user(msg) == LINK_PROTOCOL) {
c93d3baa 1189 tipc_link_proto_rcv(net, l_ptr, skb);
f03273f1 1190 link_retrieve_defq(l_ptr, &head);
4323add6 1191 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1192 continue;
1193 }
3af390e2
YX
1194
1195 /* Traffic message. Conditionally activate link */
1196 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1197
1198 if (link_working_working(l_ptr)) {
1199 /* Re-insert buffer in front of queue */
f03273f1 1200 __skb_queue_head(&head, skb);
3af390e2
YX
1201 tipc_node_unlock(n_ptr);
1202 continue;
1203 }
1204 goto unlock_discard;
1205 }
1206
1207 /* Link is now in state WORKING_WORKING */
1208 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
c93d3baa 1209 link_handle_out_of_seq_msg(net, l_ptr, skb);
f03273f1 1210 link_retrieve_defq(l_ptr, &head);
4323add6 1211 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1212 continue;
1213 }
3af390e2 1214 l_ptr->next_in_no++;
bc6fecd4 1215 if (unlikely(!skb_queue_empty(&l_ptr->deferred_queue)))
f03273f1 1216 link_retrieve_defq(l_ptr, &head);
a5377831 1217
3f53bd8f
EH
1218 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1219 l_ptr->stats.sent_acks++;
1220 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1221 }
1222
c93d3baa 1223 if (tipc_link_prepare_input(net, l_ptr, &skb)) {
3af390e2 1224 tipc_node_unlock(n_ptr);
3af390e2 1225 continue;
b97bf3fd 1226 }
4323add6 1227 tipc_node_unlock(n_ptr);
f03273f1 1228
f2f9800d 1229 if (tipc_link_input(net, l_ptr, skb) != 0)
7ae934be 1230 goto discard;
3af390e2
YX
1231 continue;
1232unlock_discard:
3af390e2
YX
1233 tipc_node_unlock(n_ptr);
1234discard:
f03273f1 1235 kfree_skb(skb);
b97bf3fd 1236 }
b97bf3fd
PL
1237}
1238
7ae934be
EH
1239/**
1240 * tipc_link_prepare_input - process TIPC link messages
1241 *
1242 * returns nonzero if the message was consumed
1243 *
1244 * Node lock must be held
1245 */
c93d3baa
YX
1246static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
1247 struct sk_buff **buf)
7ae934be
EH
1248{
1249 struct tipc_node *n;
1250 struct tipc_msg *msg;
1251 int res = -EINVAL;
1252
1253 n = l->owner;
1254 msg = buf_msg(*buf);
1255 switch (msg_user(msg)) {
1256 case CHANGEOVER_PROTOCOL:
c93d3baa 1257 if (tipc_link_tunnel_rcv(net, n, buf))
7ae934be
EH
1258 res = 0;
1259 break;
1260 case MSG_FRAGMENTER:
1261 l->stats.recv_fragments++;
1262 if (tipc_buf_append(&l->reasm_buf, buf)) {
1263 l->stats.recv_fragmented++;
1264 res = 0;
1265 } else if (!l->reasm_buf) {
1266 tipc_link_reset(l);
1267 }
1268 break;
1269 case MSG_BUNDLER:
1270 l->stats.recv_bundles++;
1271 l->stats.recv_bundled += msg_msgcnt(msg);
1272 res = 0;
1273 break;
1274 case NAME_DISTRIBUTOR:
1275 n->bclink.recv_permitted = true;
1276 res = 0;
1277 break;
1278 case BCAST_PROTOCOL:
1279 tipc_link_sync_rcv(n, *buf);
1280 break;
1281 default:
1282 res = 0;
1283 }
1284 return res;
1285}
1286/**
1287 * tipc_link_input - Deliver message too higher layers
1288 */
f2f9800d
YX
1289static int tipc_link_input(struct net *net, struct tipc_link *l,
1290 struct sk_buff *buf)
7ae934be
EH
1291{
1292 struct tipc_msg *msg = buf_msg(buf);
1293 int res = 0;
1294
1295 switch (msg_user(msg)) {
1296 case TIPC_LOW_IMPORTANCE:
1297 case TIPC_MEDIUM_IMPORTANCE:
1298 case TIPC_HIGH_IMPORTANCE:
1299 case TIPC_CRITICAL_IMPORTANCE:
1300 case CONN_MANAGER:
f2f9800d 1301 tipc_sk_rcv(net, buf);
7ae934be
EH
1302 break;
1303 case NAME_DISTRIBUTOR:
f2f9800d 1304 tipc_named_rcv(net, buf);
7ae934be
EH
1305 break;
1306 case MSG_BUNDLER:
f2f9800d 1307 tipc_link_bundle_rcv(net, buf);
7ae934be
EH
1308 break;
1309 default:
1310 res = -EINVAL;
1311 }
1312 return res;
1313}
1314
2c53040f 1315/**
8809b255
AS
1316 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1317 *
1318 * Returns increase in queue length (i.e. 0 or 1)
b97bf3fd 1319 */
bc6fecd4 1320u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
b97bf3fd 1321{
bc6fecd4
YX
1322 struct sk_buff *skb1;
1323 u32 seq_no = buf_seqno(skb);
b97bf3fd
PL
1324
1325 /* Empty queue ? */
bc6fecd4
YX
1326 if (skb_queue_empty(list)) {
1327 __skb_queue_tail(list, skb);
b97bf3fd
PL
1328 return 1;
1329 }
1330
1331 /* Last ? */
bc6fecd4
YX
1332 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1333 __skb_queue_tail(list, skb);
b97bf3fd
PL
1334 return 1;
1335 }
1336
8809b255 1337 /* Locate insertion point in queue, then insert; discard if duplicate */
bc6fecd4
YX
1338 skb_queue_walk(list, skb1) {
1339 u32 curr_seqno = buf_seqno(skb1);
b97bf3fd 1340
8809b255 1341 if (seq_no == curr_seqno) {
bc6fecd4 1342 kfree_skb(skb);
8809b255 1343 return 0;
b97bf3fd 1344 }
8809b255
AS
1345
1346 if (less(seq_no, curr_seqno))
b97bf3fd 1347 break;
8809b255 1348 }
b97bf3fd 1349
bc6fecd4 1350 __skb_queue_before(list, skb1, skb);
8809b255 1351 return 1;
b97bf3fd
PL
1352}
1353
8809b255 1354/*
b97bf3fd
PL
1355 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1356 */
c93d3baa
YX
1357static void link_handle_out_of_seq_msg(struct net *net,
1358 struct tipc_link *l_ptr,
b97bf3fd
PL
1359 struct sk_buff *buf)
1360{
f905730c 1361 u32 seq_no = buf_seqno(buf);
b97bf3fd
PL
1362
1363 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
c93d3baa 1364 tipc_link_proto_rcv(net, l_ptr, buf);
b97bf3fd
PL
1365 return;
1366 }
1367
b97bf3fd 1368 /* Record OOS packet arrival (force mismatch on next timeout) */
b97bf3fd
PL
1369 l_ptr->checkpoint--;
1370
c4307285 1371 /*
b97bf3fd
PL
1372 * Discard packet if a duplicate; otherwise add it to deferred queue
1373 * and notify peer of gap as per protocol specification
1374 */
b97bf3fd
PL
1375 if (less(seq_no, mod(l_ptr->next_in_no))) {
1376 l_ptr->stats.duplicates++;
5f6d9123 1377 kfree_skb(buf);
b97bf3fd
PL
1378 return;
1379 }
1380
bc6fecd4 1381 if (tipc_link_defer_pkt(&l_ptr->deferred_queue, buf)) {
b97bf3fd 1382 l_ptr->stats.deferred_recv++;
64380a04 1383 TIPC_SKB_CB(buf)->deferred = true;
bc6fecd4 1384 if ((skb_queue_len(&l_ptr->deferred_queue) % 16) == 1)
247f0f3c 1385 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
bc6fecd4 1386 } else {
b97bf3fd 1387 l_ptr->stats.duplicates++;
bc6fecd4 1388 }
b97bf3fd
PL
1389}
1390
1391/*
1392 * Send protocol message to the other endpoint.
1393 */
247f0f3c
YX
1394void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1395 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
b97bf3fd 1396{
1fc54d8f 1397 struct sk_buff *buf = NULL;
b97bf3fd 1398 struct tipc_msg *msg = l_ptr->pmsg;
c4307285 1399 u32 msg_size = sizeof(l_ptr->proto_msg);
75f0aa49 1400 int r_flag;
b97bf3fd 1401
77a7e07a
YX
1402 /* Don't send protocol message during link changeover */
1403 if (l_ptr->exp_msg_count)
b97bf3fd 1404 return;
b4b56102
AS
1405
1406 /* Abort non-RESET send if communication with node is prohibited */
10f465c4 1407 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
b4b56102
AS
1408 return;
1409
92d2c905 1410 /* Create protocol message with "out-of-sequence" sequence number */
b97bf3fd 1411 msg_set_type(msg, msg_typ);
7a2f7d18 1412 msg_set_net_plane(msg, l_ptr->net_plane);
7a54d4a9 1413 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1da46568 1414 msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
b97bf3fd
PL
1415
1416 if (msg_typ == STATE_MSG) {
1417 u32 next_sent = mod(l_ptr->next_out_no);
1418
4323add6 1419 if (!tipc_link_is_up(l_ptr))
b97bf3fd
PL
1420 return;
1421 if (l_ptr->next_out)
f905730c 1422 next_sent = buf_seqno(l_ptr->next_out);
b97bf3fd 1423 msg_set_next_sent(msg, next_sent);
bc6fecd4
YX
1424 if (!skb_queue_empty(&l_ptr->deferred_queue)) {
1425 u32 rec = buf_seqno(skb_peek(&l_ptr->deferred_queue));
b97bf3fd
PL
1426 gap = mod(rec - mod(l_ptr->next_in_no));
1427 }
1428 msg_set_seq_gap(msg, gap);
1429 if (gap)
1430 l_ptr->stats.sent_nacks++;
1431 msg_set_link_tolerance(msg, tolerance);
1432 msg_set_linkprio(msg, priority);
1433 msg_set_max_pkt(msg, ack_mtu);
1434 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1435 msg_set_probe(msg, probe_msg != 0);
c4307285 1436 if (probe_msg) {
b97bf3fd
PL
1437 u32 mtu = l_ptr->max_pkt;
1438
c4307285 1439 if ((mtu < l_ptr->max_pkt_target) &&
b97bf3fd
PL
1440 link_working_working(l_ptr) &&
1441 l_ptr->fsm_msg_cnt) {
1442 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285
YH
1443 if (l_ptr->max_pkt_probes == 10) {
1444 l_ptr->max_pkt_target = (msg_size - 4);
1445 l_ptr->max_pkt_probes = 0;
b97bf3fd 1446 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285 1447 }
b97bf3fd 1448 l_ptr->max_pkt_probes++;
c4307285 1449 }
b97bf3fd
PL
1450
1451 l_ptr->stats.sent_probes++;
c4307285 1452 }
b97bf3fd
PL
1453 l_ptr->stats.sent_states++;
1454 } else { /* RESET_MSG or ACTIVATE_MSG */
1455 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1456 msg_set_seq_gap(msg, 0);
1457 msg_set_next_sent(msg, 1);
f23d9bf2 1458 msg_set_probe(msg, 0);
b97bf3fd
PL
1459 msg_set_link_tolerance(msg, l_ptr->tolerance);
1460 msg_set_linkprio(msg, l_ptr->priority);
1461 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1462 }
1463
75f0aa49
AS
1464 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1465 msg_set_redundant_link(msg, r_flag);
b97bf3fd 1466 msg_set_linkprio(msg, l_ptr->priority);
92d2c905 1467 msg_set_size(msg, msg_size);
b97bf3fd
PL
1468
1469 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1470
31e3c3f6 1471 buf = tipc_buf_acquire(msg_size);
b97bf3fd
PL
1472 if (!buf)
1473 return;
1474
27d7ff46 1475 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
796c75d0 1476 buf->priority = TC_PRIO_CONTROL;
b97bf3fd 1477
7f9f95d9
YX
1478 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
1479 &l_ptr->media_addr);
92d2c905 1480 l_ptr->unacked_window = 0;
5f6d9123 1481 kfree_skb(buf);
b97bf3fd
PL
1482}
1483
1484/*
1485 * Receive protocol message :
c4307285
YH
1486 * Note that network plane id propagates through the network, and may
1487 * change at any time. The node with lowest address rules
b97bf3fd 1488 */
c93d3baa
YX
1489static void tipc_link_proto_rcv(struct net *net, struct tipc_link *l_ptr,
1490 struct sk_buff *buf)
b97bf3fd 1491{
34747539 1492 struct tipc_net *tn = net_generic(net, tipc_net_id);
b97bf3fd
PL
1493 u32 rec_gap = 0;
1494 u32 max_pkt_info;
c4307285 1495 u32 max_pkt_ack;
b97bf3fd
PL
1496 u32 msg_tol;
1497 struct tipc_msg *msg = buf_msg(buf);
1498
77a7e07a
YX
1499 /* Discard protocol message during link changeover */
1500 if (l_ptr->exp_msg_count)
b97bf3fd
PL
1501 goto exit;
1502
7a2f7d18 1503 if (l_ptr->net_plane != msg_net_plane(msg))
34747539 1504 if (tn->own_addr > msg_prevnode(msg))
7a2f7d18 1505 l_ptr->net_plane = msg_net_plane(msg);
b97bf3fd 1506
b97bf3fd 1507 switch (msg_type(msg)) {
c4307285 1508
b97bf3fd 1509 case RESET_MSG:
a686e685
AS
1510 if (!link_working_unknown(l_ptr) &&
1511 (l_ptr->peer_session != INVALID_SESSION)) {
641c218d
AS
1512 if (less_eq(msg_session(msg), l_ptr->peer_session))
1513 break; /* duplicate or old reset: ignore */
b97bf3fd 1514 }
b4b56102
AS
1515
1516 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1517 link_working_unknown(l_ptr))) {
1518 /*
1519 * peer has lost contact -- don't allow peer's links
1520 * to reactivate before we recognize loss & clean up
1521 */
ca9cf06a 1522 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
b4b56102
AS
1523 }
1524
47361c87
AS
1525 link_state_event(l_ptr, RESET_MSG);
1526
b97bf3fd
PL
1527 /* fall thru' */
1528 case ACTIVATE_MSG:
1529 /* Update link settings according other endpoint's values */
b97bf3fd
PL
1530 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1531
2db9983a
AS
1532 msg_tol = msg_link_tolerance(msg);
1533 if (msg_tol > l_ptr->tolerance)
b97bf3fd
PL
1534 link_set_supervision_props(l_ptr, msg_tol);
1535
1536 if (msg_linkprio(msg) > l_ptr->priority)
1537 l_ptr->priority = msg_linkprio(msg);
1538
1539 max_pkt_info = msg_max_pkt(msg);
c4307285 1540 if (max_pkt_info) {
b97bf3fd
PL
1541 if (max_pkt_info < l_ptr->max_pkt_target)
1542 l_ptr->max_pkt_target = max_pkt_info;
1543 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1544 l_ptr->max_pkt = l_ptr->max_pkt_target;
1545 } else {
c4307285 1546 l_ptr->max_pkt = l_ptr->max_pkt_target;
b97bf3fd 1547 }
b97bf3fd 1548
4d75313c 1549 /* Synchronize broadcast link info, if not done previously */
7a54d4a9
AS
1550 if (!tipc_node_is_up(l_ptr->owner)) {
1551 l_ptr->owner->bclink.last_sent =
1552 l_ptr->owner->bclink.last_in =
1553 msg_last_bcast(msg);
1554 l_ptr->owner->bclink.oos_state = 0;
1555 }
4d75313c 1556
b97bf3fd
PL
1557 l_ptr->peer_session = msg_session(msg);
1558 l_ptr->peer_bearer_id = msg_bearer_id(msg);
47361c87
AS
1559
1560 if (msg_type(msg) == ACTIVATE_MSG)
1561 link_state_event(l_ptr, ACTIVATE_MSG);
b97bf3fd
PL
1562 break;
1563 case STATE_MSG:
1564
2db9983a
AS
1565 msg_tol = msg_link_tolerance(msg);
1566 if (msg_tol)
b97bf3fd 1567 link_set_supervision_props(l_ptr, msg_tol);
c4307285
YH
1568
1569 if (msg_linkprio(msg) &&
b97bf3fd 1570 (msg_linkprio(msg) != l_ptr->priority)) {
3fa9cacd
EH
1571 pr_debug("%s<%s>, priority change %u->%u\n",
1572 link_rst_msg, l_ptr->name,
1573 l_ptr->priority, msg_linkprio(msg));
b97bf3fd 1574 l_ptr->priority = msg_linkprio(msg);
4323add6 1575 tipc_link_reset(l_ptr); /* Enforce change to take effect */
b97bf3fd
PL
1576 break;
1577 }
ec37dcd3
JPM
1578
1579 /* Record reception; force mismatch at next timeout: */
1580 l_ptr->checkpoint--;
1581
b97bf3fd
PL
1582 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1583 l_ptr->stats.recv_states++;
1584 if (link_reset_unknown(l_ptr))
1585 break;
1586
1587 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
c4307285 1588 rec_gap = mod(msg_next_sent(msg) -
b97bf3fd
PL
1589 mod(l_ptr->next_in_no));
1590 }
1591
1592 max_pkt_ack = msg_max_pkt(msg);
c4307285 1593 if (max_pkt_ack > l_ptr->max_pkt) {
c4307285
YH
1594 l_ptr->max_pkt = max_pkt_ack;
1595 l_ptr->max_pkt_probes = 0;
1596 }
b97bf3fd
PL
1597
1598 max_pkt_ack = 0;
c4307285 1599 if (msg_probe(msg)) {
b97bf3fd 1600 l_ptr->stats.recv_probes++;
a016892c 1601 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
c4307285 1602 max_pkt_ack = msg_size(msg);
c4307285 1603 }
b97bf3fd
PL
1604
1605 /* Protocol message before retransmits, reduce loss risk */
389dd9bc 1606 if (l_ptr->owner->bclink.recv_permitted)
c93d3baa 1607 tipc_bclink_update_link_state(net, l_ptr->owner,
7a54d4a9 1608 msg_last_bcast(msg));
b97bf3fd
PL
1609
1610 if (rec_gap || (msg_probe(msg))) {
247f0f3c
YX
1611 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1612 0, max_pkt_ack);
b97bf3fd
PL
1613 }
1614 if (msg_seq_gap(msg)) {
b97bf3fd 1615 l_ptr->stats.recv_nacks++;
58dc55f2 1616 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue),
4323add6 1617 msg_seq_gap(msg));
b97bf3fd
PL
1618 }
1619 break;
b97bf3fd
PL
1620 }
1621exit:
5f6d9123 1622 kfree_skb(buf);
b97bf3fd
PL
1623}
1624
1625
170b3927
JPM
1626/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1627 * a different bearer. Owner node is locked.
b97bf3fd 1628 */
170b3927
JPM
1629static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1630 struct tipc_msg *tunnel_hdr,
1631 struct tipc_msg *msg,
1632 u32 selector)
b97bf3fd 1633{
a18c4bc3 1634 struct tipc_link *tunnel;
a6ca1094 1635 struct sk_buff *skb;
b97bf3fd
PL
1636 u32 length = msg_size(msg);
1637
1638 tunnel = l_ptr->owner->active_links[selector & 1];
5392d646 1639 if (!tipc_link_is_up(tunnel)) {
2cf8aa19 1640 pr_warn("%stunnel link no longer available\n", link_co_err);
b97bf3fd 1641 return;
5392d646 1642 }
b97bf3fd 1643 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
a6ca1094
YX
1644 skb = tipc_buf_acquire(length + INT_H_SIZE);
1645 if (!skb) {
2cf8aa19 1646 pr_warn("%sunable to send tunnel msg\n", link_co_err);
b97bf3fd 1647 return;
5392d646 1648 }
a6ca1094
YX
1649 skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
1650 skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
1651 __tipc_link_xmit_skb(tunnel, skb);
b97bf3fd
PL
1652}
1653
1654
170b3927
JPM
1655/* tipc_link_failover_send_queue(): A link has gone down, but a second
1656 * link is still active. We can do failover. Tunnel the failing link's
1657 * whole send queue via the remaining link. This way, we don't lose
1658 * any packets, and sequence order is preserved for subsequent traffic
1659 * sent over the remaining link. Owner node is locked.
b97bf3fd 1660 */
170b3927 1661void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
b97bf3fd 1662{
58dc55f2 1663 u32 msgcount = skb_queue_len(&l_ptr->outqueue);
a18c4bc3 1664 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
b97bf3fd 1665 struct tipc_msg tunnel_hdr;
58dc55f2 1666 struct sk_buff *skb;
5392d646 1667 int split_bundles;
b97bf3fd
PL
1668
1669 if (!tunnel)
1670 return;
1671
34747539
YX
1672 tipc_msg_init(l_ptr->owner->net, &tunnel_hdr, CHANGEOVER_PROTOCOL,
1673 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd
PL
1674 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1675 msg_set_msgcnt(&tunnel_hdr, msgcount);
f131072c 1676
58dc55f2
YX
1677 if (skb_queue_empty(&l_ptr->outqueue)) {
1678 skb = tipc_buf_acquire(INT_H_SIZE);
1679 if (skb) {
1680 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
b97bf3fd 1681 msg_set_size(&tunnel_hdr, INT_H_SIZE);
a6ca1094 1682 __tipc_link_xmit_skb(tunnel, skb);
b97bf3fd 1683 } else {
2cf8aa19
EH
1684 pr_warn("%sunable to send changeover msg\n",
1685 link_co_err);
b97bf3fd
PL
1686 }
1687 return;
1688 }
f131072c 1689
c4307285 1690 split_bundles = (l_ptr->owner->active_links[0] !=
5392d646
AS
1691 l_ptr->owner->active_links[1]);
1692
58dc55f2
YX
1693 skb_queue_walk(&l_ptr->outqueue, skb) {
1694 struct tipc_msg *msg = buf_msg(skb);
b97bf3fd
PL
1695
1696 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
b97bf3fd 1697 struct tipc_msg *m = msg_get_wrapped(msg);
0e65967e 1698 unchar *pos = (unchar *)m;
b97bf3fd 1699
d788d805 1700 msgcount = msg_msgcnt(msg);
b97bf3fd 1701 while (msgcount--) {
0e65967e 1702 msg_set_seqno(m, msg_seqno(msg));
170b3927
JPM
1703 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1704 msg_link_selector(m));
b97bf3fd
PL
1705 pos += align(msg_size(m));
1706 m = (struct tipc_msg *)pos;
1707 }
1708 } else {
170b3927
JPM
1709 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1710 msg_link_selector(msg));
b97bf3fd 1711 }
b97bf3fd
PL
1712 }
1713}
1714
247f0f3c 1715/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
170b3927
JPM
1716 * duplicate of the first link's send queue via the new link. This way, we
1717 * are guaranteed that currently queued packets from a socket are delivered
1718 * before future traffic from the same socket, even if this is using the
1719 * new link. The last arriving copy of each duplicate packet is dropped at
1720 * the receiving end by the regular protocol check, so packet cardinality
1721 * and sequence order is preserved per sender/receiver socket pair.
1722 * Owner node is locked.
1723 */
247f0f3c 1724void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
170b3927 1725 struct tipc_link *tunnel)
b97bf3fd 1726{
58dc55f2 1727 struct sk_buff *skb;
b97bf3fd
PL
1728 struct tipc_msg tunnel_hdr;
1729
34747539
YX
1730 tipc_msg_init(l_ptr->owner->net, &tunnel_hdr, CHANGEOVER_PROTOCOL,
1731 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
58dc55f2 1732 msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue));
b97bf3fd 1733 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
58dc55f2
YX
1734 skb_queue_walk(&l_ptr->outqueue, skb) {
1735 struct sk_buff *outskb;
1736 struct tipc_msg *msg = buf_msg(skb);
b97bf3fd
PL
1737 u32 length = msg_size(msg);
1738
1739 if (msg_user(msg) == MSG_BUNDLER)
1740 msg_set_type(msg, CLOSED_MSG);
1741 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
c4307285 1742 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
b97bf3fd 1743 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
58dc55f2
YX
1744 outskb = tipc_buf_acquire(length + INT_H_SIZE);
1745 if (outskb == NULL) {
2cf8aa19
EH
1746 pr_warn("%sunable to send duplicate msg\n",
1747 link_co_err);
b97bf3fd
PL
1748 return;
1749 }
58dc55f2
YX
1750 skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE);
1751 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data,
27d7ff46 1752 length);
a6ca1094 1753 __tipc_link_xmit_skb(tunnel, outskb);
4323add6 1754 if (!tipc_link_is_up(l_ptr))
b97bf3fd 1755 return;
b97bf3fd
PL
1756 }
1757}
1758
b97bf3fd
PL
1759/**
1760 * buf_extract - extracts embedded TIPC message from another message
1761 * @skb: encapsulating message buffer
1762 * @from_pos: offset to extract from
1763 *
c4307285 1764 * Returns a new message buffer containing an embedded message. The
b97bf3fd
PL
1765 * encapsulating message itself is left unchanged.
1766 */
b97bf3fd
PL
1767static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1768{
1769 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1770 u32 size = msg_size(msg);
1771 struct sk_buff *eb;
1772
31e3c3f6 1773 eb = tipc_buf_acquire(size);
b97bf3fd 1774 if (eb)
27d7ff46 1775 skb_copy_to_linear_data(eb, msg, size);
b97bf3fd
PL
1776 return eb;
1777}
1778
1dab3d5a
JPM
1779
1780
1781/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1782 * Owner node is locked.
1783 */
c93d3baa 1784static void tipc_link_dup_rcv(struct net *net, struct tipc_link *l_ptr,
1dab3d5a
JPM
1785 struct sk_buff *t_buf)
1786{
1787 struct sk_buff *buf;
1788
1789 if (!tipc_link_is_up(l_ptr))
1790 return;
1791
1792 buf = buf_extract(t_buf, INT_H_SIZE);
1793 if (buf == NULL) {
1794 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1795 return;
1796 }
1797
1798 /* Add buffer to deferred queue, if applicable: */
c93d3baa 1799 link_handle_out_of_seq_msg(net, l_ptr, buf);
1dab3d5a
JPM
1800}
1801
f006c9c7
JPM
1802/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1803 * Owner node is locked.
1804 */
1805static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1806 struct sk_buff *t_buf)
1807{
1808 struct tipc_msg *t_msg = buf_msg(t_buf);
1809 struct sk_buff *buf = NULL;
1810 struct tipc_msg *msg;
1811
1812 if (tipc_link_is_up(l_ptr))
1813 tipc_link_reset(l_ptr);
1814
1815 /* First failover packet? */
1816 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1817 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1818
1819 /* Should there be an inner packet? */
1820 if (l_ptr->exp_msg_count) {
1821 l_ptr->exp_msg_count--;
1822 buf = buf_extract(t_buf, INT_H_SIZE);
1823 if (buf == NULL) {
1824 pr_warn("%sno inner failover pkt\n", link_co_err);
1825 goto exit;
1826 }
1827 msg = buf_msg(buf);
1828
1829 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1830 kfree_skb(buf);
1831 buf = NULL;
1832 goto exit;
1833 }
1834 if (msg_user(msg) == MSG_FRAGMENTER) {
1835 l_ptr->stats.recv_fragments++;
37e22164 1836 tipc_buf_append(&l_ptr->reasm_buf, &buf);
f006c9c7
JPM
1837 }
1838 }
f006c9c7 1839exit:
7d33939f
JPM
1840 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1841 tipc_node_detach_link(l_ptr->owner, l_ptr);
1842 kfree(l_ptr);
1843 }
f006c9c7
JPM
1844 return buf;
1845}
1846
1dab3d5a 1847/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
170b3927
JPM
1848 * via other link as result of a failover (ORIGINAL_MSG) or
1849 * a new active link (DUPLICATE_MSG). Failover packets are
1850 * returned to the active link for delivery upwards.
1851 * Owner node is locked.
b97bf3fd 1852 */
c93d3baa 1853static int tipc_link_tunnel_rcv(struct net *net, struct tipc_node *n_ptr,
170b3927 1854 struct sk_buff **buf)
b97bf3fd 1855{
02842f71
JPM
1856 struct sk_buff *t_buf = *buf;
1857 struct tipc_link *l_ptr;
1858 struct tipc_msg *t_msg = buf_msg(t_buf);
1859 u32 bearer_id = msg_bearer_id(t_msg);
b97bf3fd 1860
1e9d47a9
JPM
1861 *buf = NULL;
1862
cb4b102f
DC
1863 if (bearer_id >= MAX_BEARERS)
1864 goto exit;
1dab3d5a 1865
02842f71
JPM
1866 l_ptr = n_ptr->links[bearer_id];
1867 if (!l_ptr)
b97bf3fd 1868 goto exit;
b97bf3fd 1869
02842f71 1870 if (msg_type(t_msg) == DUPLICATE_MSG)
c93d3baa 1871 tipc_link_dup_rcv(net, l_ptr, t_buf);
02842f71
JPM
1872 else if (msg_type(t_msg) == ORIGINAL_MSG)
1873 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
1e9d47a9
JPM
1874 else
1875 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
b97bf3fd 1876exit:
02842f71 1877 kfree_skb(t_buf);
1e9d47a9 1878 return *buf != NULL;
b97bf3fd
PL
1879}
1880
1881/*
1882 * Bundler functionality:
1883 */
f2f9800d 1884void tipc_link_bundle_rcv(struct net *net, struct sk_buff *buf)
b97bf3fd
PL
1885{
1886 u32 msgcount = msg_msgcnt(buf_msg(buf));
1887 u32 pos = INT_H_SIZE;
1888 struct sk_buff *obuf;
ec8a2e56 1889 struct tipc_msg *omsg;
b97bf3fd 1890
b97bf3fd
PL
1891 while (msgcount--) {
1892 obuf = buf_extract(buf, pos);
1893 if (obuf == NULL) {
2cf8aa19 1894 pr_warn("Link unable to unbundle message(s)\n");
a10bd924 1895 break;
3ff50b79 1896 }
ec8a2e56
JPM
1897 omsg = buf_msg(obuf);
1898 pos += align(msg_size(omsg));
643566d4
JPM
1899 if (msg_isdata(omsg)) {
1900 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
f2f9800d 1901 tipc_sk_mcast_rcv(net, obuf);
643566d4 1902 else
f2f9800d 1903 tipc_sk_rcv(net, obuf);
643566d4 1904 } else if (msg_user(omsg) == CONN_MANAGER) {
f2f9800d 1905 tipc_sk_rcv(net, obuf);
ec8a2e56 1906 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
f2f9800d 1907 tipc_named_rcv(net, obuf);
ec8a2e56
JPM
1908 } else {
1909 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1910 kfree_skb(obuf);
1911 }
b97bf3fd 1912 }
5f6d9123 1913 kfree_skb(buf);
b97bf3fd
PL
1914}
1915
2f55c437 1916static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
b97bf3fd 1917{
2f55c437
YX
1918 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
1919
1920 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
5413b4c6
AS
1921 return;
1922
2f55c437
YX
1923 l_ptr->tolerance = tol;
1924 l_ptr->cont_intv = msecs_to_jiffies(intv);
1925 l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
b97bf3fd
PL
1926}
1927
a18c4bc3 1928void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
b97bf3fd
PL
1929{
1930 /* Data messages from this node, inclusive FIRST_FRAGM */
06d82c91
AS
1931 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1932 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1933 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1934 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
b97bf3fd 1935 /* Transiting data messages,inclusive FIRST_FRAGM */
06d82c91
AS
1936 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1937 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1938 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1939 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
b97bf3fd 1940 l_ptr->queue_limit[CONN_MANAGER] = 1200;
b97bf3fd
PL
1941 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1942 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1943 /* FRAGMENT and LAST_FRAGMENT packets */
1944 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1945}
1946
e099e86c 1947/* tipc_link_find_owner - locate owner node of link by link's name
f2f9800d 1948 * @net: the applicable net namespace
e099e86c
JPM
1949 * @name: pointer to link name string
1950 * @bearer_id: pointer to index in 'node->links' array where the link was found.
c4307285 1951 *
e099e86c 1952 * Returns pointer to node owning the link, or 0 if no matching link is found.
b97bf3fd 1953 */
f2f9800d
YX
1954static struct tipc_node *tipc_link_find_owner(struct net *net,
1955 const char *link_name,
e099e86c 1956 unsigned int *bearer_id)
b97bf3fd 1957{
f2f9800d 1958 struct tipc_net *tn = net_generic(net, tipc_net_id);
a18c4bc3 1959 struct tipc_link *l_ptr;
bbfbe47c 1960 struct tipc_node *n_ptr;
886eaa1f 1961 struct tipc_node *found_node = NULL;
bbfbe47c 1962 int i;
b97bf3fd 1963
e099e86c 1964 *bearer_id = 0;
6c7a762e 1965 rcu_read_lock();
f2f9800d 1966 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
a11607f5 1967 tipc_node_lock(n_ptr);
bbfbe47c
EH
1968 for (i = 0; i < MAX_BEARERS; i++) {
1969 l_ptr = n_ptr->links[i];
e099e86c
JPM
1970 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1971 *bearer_id = i;
1972 found_node = n_ptr;
1973 break;
1974 }
bbfbe47c 1975 }
a11607f5 1976 tipc_node_unlock(n_ptr);
e099e86c
JPM
1977 if (found_node)
1978 break;
bbfbe47c 1979 }
6c7a762e
YX
1980 rcu_read_unlock();
1981
e099e86c 1982 return found_node;
b97bf3fd
PL
1983}
1984
5c216e1d
AS
1985/**
1986 * link_value_is_valid -- validate proposed link tolerance/priority/window
1987 *
2c53040f
BH
1988 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1989 * @new_value: the new value
5c216e1d
AS
1990 *
1991 * Returns 1 if value is within range, 0 if not.
1992 */
5c216e1d
AS
1993static int link_value_is_valid(u16 cmd, u32 new_value)
1994{
1995 switch (cmd) {
1996 case TIPC_CMD_SET_LINK_TOL:
1997 return (new_value >= TIPC_MIN_LINK_TOL) &&
1998 (new_value <= TIPC_MAX_LINK_TOL);
1999 case TIPC_CMD_SET_LINK_PRI:
2000 return (new_value <= TIPC_MAX_LINK_PRI);
2001 case TIPC_CMD_SET_LINK_WINDOW:
2002 return (new_value >= TIPC_MIN_LINK_WIN) &&
2003 (new_value <= TIPC_MAX_LINK_WIN);
2004 }
2005 return 0;
2006}
2007
5c216e1d
AS
2008/**
2009 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
f2f9800d 2010 * @net: the applicable net namespace
2c53040f
BH
2011 * @name: ptr to link, bearer, or media name
2012 * @new_value: new value of link, bearer, or media setting
2013 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
5c216e1d 2014 *
7216cd94 2015 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
5c216e1d
AS
2016 *
2017 * Returns 0 if value updated and negative value on error.
2018 */
f2f9800d
YX
2019static int link_cmd_set_value(struct net *net, const char *name, u32 new_value,
2020 u16 cmd)
5c216e1d
AS
2021{
2022 struct tipc_node *node;
a18c4bc3 2023 struct tipc_link *l_ptr;
5c216e1d 2024 struct tipc_bearer *b_ptr;
358a0d1c 2025 struct tipc_media *m_ptr;
e099e86c 2026 int bearer_id;
636c0371 2027 int res = 0;
5c216e1d 2028
f2f9800d 2029 node = tipc_link_find_owner(net, name, &bearer_id);
e099e86c 2030 if (node) {
5c216e1d 2031 tipc_node_lock(node);
e099e86c
JPM
2032 l_ptr = node->links[bearer_id];
2033
2034 if (l_ptr) {
2035 switch (cmd) {
2036 case TIPC_CMD_SET_LINK_TOL:
2037 link_set_supervision_props(l_ptr, new_value);
247f0f3c
YX
2038 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2039 new_value, 0, 0);
e099e86c
JPM
2040 break;
2041 case TIPC_CMD_SET_LINK_PRI:
2042 l_ptr->priority = new_value;
247f0f3c
YX
2043 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2044 0, new_value, 0);
e099e86c
JPM
2045 break;
2046 case TIPC_CMD_SET_LINK_WINDOW:
2047 tipc_link_set_queue_limits(l_ptr, new_value);
2048 break;
2049 default:
2050 res = -EINVAL;
2051 break;
2052 }
5c216e1d
AS
2053 }
2054 tipc_node_unlock(node);
636c0371 2055 return res;
5c216e1d
AS
2056 }
2057
7f9f95d9 2058 b_ptr = tipc_bearer_find(net, name);
5c216e1d
AS
2059 if (b_ptr) {
2060 switch (cmd) {
2061 case TIPC_CMD_SET_LINK_TOL:
2062 b_ptr->tolerance = new_value;
636c0371 2063 break;
5c216e1d
AS
2064 case TIPC_CMD_SET_LINK_PRI:
2065 b_ptr->priority = new_value;
636c0371 2066 break;
5c216e1d
AS
2067 case TIPC_CMD_SET_LINK_WINDOW:
2068 b_ptr->window = new_value;
636c0371
YX
2069 break;
2070 default:
2071 res = -EINVAL;
2072 break;
5c216e1d 2073 }
636c0371 2074 return res;
5c216e1d
AS
2075 }
2076
2077 m_ptr = tipc_media_find(name);
2078 if (!m_ptr)
2079 return -ENODEV;
2080 switch (cmd) {
2081 case TIPC_CMD_SET_LINK_TOL:
2082 m_ptr->tolerance = new_value;
636c0371 2083 break;
5c216e1d
AS
2084 case TIPC_CMD_SET_LINK_PRI:
2085 m_ptr->priority = new_value;
636c0371 2086 break;
5c216e1d
AS
2087 case TIPC_CMD_SET_LINK_WINDOW:
2088 m_ptr->window = new_value;
636c0371
YX
2089 break;
2090 default:
2091 res = -EINVAL;
2092 break;
5c216e1d 2093 }
636c0371 2094 return res;
5c216e1d
AS
2095}
2096
f2f9800d
YX
2097struct sk_buff *tipc_link_cmd_config(struct net *net, const void *req_tlv_area,
2098 int req_tlv_space, u16 cmd)
b97bf3fd
PL
2099{
2100 struct tipc_link_config *args;
c4307285 2101 u32 new_value;
c4307285 2102 int res;
b97bf3fd
PL
2103
2104 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
4323add6 2105 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd
PL
2106
2107 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2108 new_value = ntohl(args->value);
2109
5c216e1d
AS
2110 if (!link_value_is_valid(cmd, new_value))
2111 return tipc_cfg_reply_error_string(
2112 "cannot change, value invalid");
2113
4323add6 2114 if (!strcmp(args->name, tipc_bclink_name)) {
b97bf3fd 2115 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
1da46568 2116 (tipc_bclink_set_queue_limits(net, new_value) == 0))
4323add6 2117 return tipc_cfg_reply_none();
c4307285 2118 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
4323add6 2119 " (cannot change setting on broadcast link)");
b97bf3fd
PL
2120 }
2121
f2f9800d 2122 res = link_cmd_set_value(net, args->name, new_value, cmd);
b97bf3fd 2123 if (res)
c4307285 2124 return tipc_cfg_reply_error_string("cannot change link setting");
b97bf3fd 2125
4323add6 2126 return tipc_cfg_reply_none();
b97bf3fd
PL
2127}
2128
2129/**
2130 * link_reset_statistics - reset link statistics
2131 * @l_ptr: pointer to link
2132 */
a18c4bc3 2133static void link_reset_statistics(struct tipc_link *l_ptr)
b97bf3fd
PL
2134{
2135 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2136 l_ptr->stats.sent_info = l_ptr->next_out_no;
2137 l_ptr->stats.recv_info = l_ptr->next_in_no;
2138}
2139
f2f9800d
YX
2140struct sk_buff *tipc_link_cmd_reset_stats(struct net *net,
2141 const void *req_tlv_area,
2142 int req_tlv_space)
b97bf3fd
PL
2143{
2144 char *link_name;
a18c4bc3 2145 struct tipc_link *l_ptr;
6c00055a 2146 struct tipc_node *node;
e099e86c 2147 unsigned int bearer_id;
b97bf3fd
PL
2148
2149 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
4323add6 2150 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd
PL
2151
2152 link_name = (char *)TLV_DATA(req_tlv_area);
4323add6 2153 if (!strcmp(link_name, tipc_bclink_name)) {
1da46568 2154 if (tipc_bclink_reset_stats(net))
4323add6
PL
2155 return tipc_cfg_reply_error_string("link not found");
2156 return tipc_cfg_reply_none();
b97bf3fd 2157 }
f2f9800d 2158 node = tipc_link_find_owner(net, link_name, &bearer_id);
7216cd94 2159 if (!node)
e099e86c 2160 return tipc_cfg_reply_error_string("link not found");
7216cd94 2161
a11607f5 2162 tipc_node_lock(node);
e099e86c 2163 l_ptr = node->links[bearer_id];
b97bf3fd 2164 if (!l_ptr) {
e099e86c 2165 tipc_node_unlock(node);
4323add6 2166 return tipc_cfg_reply_error_string("link not found");
b97bf3fd 2167 }
b97bf3fd 2168 link_reset_statistics(l_ptr);
4323add6 2169 tipc_node_unlock(node);
4323add6 2170 return tipc_cfg_reply_none();
b97bf3fd
PL
2171}
2172
2173/**
2174 * percent - convert count to a percentage of total (rounding up or down)
2175 */
b97bf3fd
PL
2176static u32 percent(u32 count, u32 total)
2177{
2178 return (count * 100 + (total / 2)) / total;
2179}
2180
2181/**
4323add6 2182 * tipc_link_stats - print link statistics
f2f9800d 2183 * @net: the applicable net namespace
b97bf3fd
PL
2184 * @name: link name
2185 * @buf: print buffer area
2186 * @buf_size: size of print buffer area
c4307285 2187 *
b97bf3fd
PL
2188 * Returns length of print buffer data string (or 0 if error)
2189 */
f2f9800d
YX
2190static int tipc_link_stats(struct net *net, const char *name, char *buf,
2191 const u32 buf_size)
b97bf3fd 2192{
dc1aed37
EH
2193 struct tipc_link *l;
2194 struct tipc_stats *s;
6c00055a 2195 struct tipc_node *node;
b97bf3fd
PL
2196 char *status;
2197 u32 profile_total = 0;
e099e86c 2198 unsigned int bearer_id;
dc1aed37 2199 int ret;
b97bf3fd 2200
4323add6 2201 if (!strcmp(name, tipc_bclink_name))
1da46568 2202 return tipc_bclink_stats(net, buf, buf_size);
b97bf3fd 2203
f2f9800d 2204 node = tipc_link_find_owner(net, name, &bearer_id);
7216cd94 2205 if (!node)
b97bf3fd 2206 return 0;
7216cd94 2207
4323add6 2208 tipc_node_lock(node);
e099e86c
JPM
2209
2210 l = node->links[bearer_id];
2211 if (!l) {
2212 tipc_node_unlock(node);
e099e86c
JPM
2213 return 0;
2214 }
2215
dc1aed37 2216 s = &l->stats;
b97bf3fd 2217
dc1aed37 2218 if (tipc_link_is_active(l))
b97bf3fd 2219 status = "ACTIVE";
dc1aed37 2220 else if (tipc_link_is_up(l))
b97bf3fd
PL
2221 status = "STANDBY";
2222 else
2223 status = "DEFUNCT";
dc1aed37
EH
2224
2225 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2226 " %s MTU:%u Priority:%u Tolerance:%u ms"
2227 " Window:%u packets\n",
2228 l->name, status, l->max_pkt, l->priority,
2229 l->tolerance, l->queue_limit[0]);
2230
2231 ret += tipc_snprintf(buf + ret, buf_size - ret,
2232 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2233 l->next_in_no - s->recv_info, s->recv_fragments,
2234 s->recv_fragmented, s->recv_bundles,
2235 s->recv_bundled);
2236
2237 ret += tipc_snprintf(buf + ret, buf_size - ret,
2238 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2239 l->next_out_no - s->sent_info, s->sent_fragments,
2240 s->sent_fragmented, s->sent_bundles,
2241 s->sent_bundled);
2242
2243 profile_total = s->msg_length_counts;
b97bf3fd
PL
2244 if (!profile_total)
2245 profile_total = 1;
dc1aed37
EH
2246
2247 ret += tipc_snprintf(buf + ret, buf_size - ret,
2248 " TX profile sample:%u packets average:%u octets\n"
2249 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2250 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2251 s->msg_length_counts,
2252 s->msg_lengths_total / profile_total,
2253 percent(s->msg_length_profile[0], profile_total),
2254 percent(s->msg_length_profile[1], profile_total),
2255 percent(s->msg_length_profile[2], profile_total),
2256 percent(s->msg_length_profile[3], profile_total),
2257 percent(s->msg_length_profile[4], profile_total),
2258 percent(s->msg_length_profile[5], profile_total),
2259 percent(s->msg_length_profile[6], profile_total));
2260
2261 ret += tipc_snprintf(buf + ret, buf_size - ret,
2262 " RX states:%u probes:%u naks:%u defs:%u"
2263 " dups:%u\n", s->recv_states, s->recv_probes,
2264 s->recv_nacks, s->deferred_recv, s->duplicates);
2265
2266 ret += tipc_snprintf(buf + ret, buf_size - ret,
2267 " TX states:%u probes:%u naks:%u acks:%u"
2268 " dups:%u\n", s->sent_states, s->sent_probes,
2269 s->sent_nacks, s->sent_acks, s->retransmitted);
2270
2271 ret += tipc_snprintf(buf + ret, buf_size - ret,
3c294cb3
YX
2272 " Congestion link:%u Send queue"
2273 " max:%u avg:%u\n", s->link_congs,
dc1aed37
EH
2274 s->max_queue_sz, s->queue_sz_counts ?
2275 (s->accu_queue_sz / s->queue_sz_counts) : 0);
b97bf3fd 2276
4323add6 2277 tipc_node_unlock(node);
dc1aed37 2278 return ret;
b97bf3fd
PL
2279}
2280
f2f9800d
YX
2281struct sk_buff *tipc_link_cmd_show_stats(struct net *net,
2282 const void *req_tlv_area,
2283 int req_tlv_space)
b97bf3fd
PL
2284{
2285 struct sk_buff *buf;
2286 struct tlv_desc *rep_tlv;
2287 int str_len;
dc1aed37
EH
2288 int pb_len;
2289 char *pb;
b97bf3fd
PL
2290
2291 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
4323add6 2292 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 2293
dc1aed37 2294 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
b97bf3fd
PL
2295 if (!buf)
2296 return NULL;
2297
2298 rep_tlv = (struct tlv_desc *)buf->data;
dc1aed37
EH
2299 pb = TLV_DATA(rep_tlv);
2300 pb_len = ULTRA_STRING_MAX_LEN;
f2f9800d 2301 str_len = tipc_link_stats(net, (char *)TLV_DATA(req_tlv_area),
dc1aed37 2302 pb, pb_len);
b97bf3fd 2303 if (!str_len) {
5f6d9123 2304 kfree_skb(buf);
c4307285 2305 return tipc_cfg_reply_error_string("link not found");
b97bf3fd 2306 }
dc1aed37 2307 str_len += 1; /* for "\0" */
b97bf3fd
PL
2308 skb_put(buf, TLV_SPACE(str_len));
2309 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2310
2311 return buf;
2312}
2313
a18c4bc3 2314static void link_print(struct tipc_link *l_ptr, const char *str)
b97bf3fd 2315{
7f9f95d9 2316 struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
7a2f7d18
YX
2317 struct tipc_bearer *b_ptr;
2318
2319 rcu_read_lock();
7f9f95d9 2320 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
7a2f7d18
YX
2321 if (b_ptr)
2322 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2323 rcu_read_unlock();
8d64a5ba 2324
b97bf3fd 2325 if (link_working_unknown(l_ptr))
5deedde9 2326 pr_cont(":WU\n");
8d64a5ba 2327 else if (link_reset_reset(l_ptr))
5deedde9 2328 pr_cont(":RR\n");
8d64a5ba 2329 else if (link_reset_unknown(l_ptr))
5deedde9 2330 pr_cont(":RU\n");
8d64a5ba 2331 else if (link_working_working(l_ptr))
5deedde9
PG
2332 pr_cont(":WW\n");
2333 else
2334 pr_cont("\n");
b97bf3fd 2335}
0655f6a8
RA
2336
2337/* Parse and validate nested (link) properties valid for media, bearer and link
2338 */
2339int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2340{
2341 int err;
2342
2343 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2344 tipc_nl_prop_policy);
2345 if (err)
2346 return err;
2347
2348 if (props[TIPC_NLA_PROP_PRIO]) {
2349 u32 prio;
2350
2351 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2352 if (prio > TIPC_MAX_LINK_PRI)
2353 return -EINVAL;
2354 }
2355
2356 if (props[TIPC_NLA_PROP_TOL]) {
2357 u32 tol;
2358
2359 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2360 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2361 return -EINVAL;
2362 }
2363
2364 if (props[TIPC_NLA_PROP_WIN]) {
2365 u32 win;
2366
2367 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2368 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2369 return -EINVAL;
2370 }
2371
2372 return 0;
2373}
7be57fc6 2374
f96ce7a2
RA
2375int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2376{
2377 int err;
2378 int res = 0;
2379 int bearer_id;
2380 char *name;
2381 struct tipc_link *link;
2382 struct tipc_node *node;
2383 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
f2f9800d 2384 struct net *net = genl_info_net(info);
f96ce7a2
RA
2385
2386 if (!info->attrs[TIPC_NLA_LINK])
2387 return -EINVAL;
2388
2389 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2390 info->attrs[TIPC_NLA_LINK],
2391 tipc_nl_link_policy);
2392 if (err)
2393 return err;
2394
2395 if (!attrs[TIPC_NLA_LINK_NAME])
2396 return -EINVAL;
2397
2398 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2399
f2f9800d 2400 node = tipc_link_find_owner(net, name, &bearer_id);
f96ce7a2
RA
2401 if (!node)
2402 return -EINVAL;
2403
2404 tipc_node_lock(node);
2405
2406 link = node->links[bearer_id];
2407 if (!link) {
2408 res = -EINVAL;
2409 goto out;
2410 }
2411
2412 if (attrs[TIPC_NLA_LINK_PROP]) {
2413 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2414
2415 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2416 props);
2417 if (err) {
2418 res = err;
2419 goto out;
2420 }
2421
2422 if (props[TIPC_NLA_PROP_TOL]) {
2423 u32 tol;
2424
2425 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2426 link_set_supervision_props(link, tol);
2427 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2428 }
2429 if (props[TIPC_NLA_PROP_PRIO]) {
2430 u32 prio;
2431
2432 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2433 link->priority = prio;
2434 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2435 }
2436 if (props[TIPC_NLA_PROP_WIN]) {
2437 u32 win;
2438
2439 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2440 tipc_link_set_queue_limits(link, win);
2441 }
2442 }
2443
2444out:
2445 tipc_node_unlock(node);
2446
2447 return res;
2448}
d8182804
RA
2449
2450static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
7be57fc6
RA
2451{
2452 int i;
2453 struct nlattr *stats;
2454
2455 struct nla_map {
2456 u32 key;
2457 u32 val;
2458 };
2459
2460 struct nla_map map[] = {
2461 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2462 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2463 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2464 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2465 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2466 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2467 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2468 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2469 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2470 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2471 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2472 s->msg_length_counts : 1},
2473 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2474 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2475 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2476 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2477 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2478 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2479 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2480 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2481 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2482 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2483 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2484 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2485 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2486 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2487 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2488 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2489 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2490 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2491 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2492 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2493 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2494 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2495 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2496 };
2497
2498 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2499 if (!stats)
2500 return -EMSGSIZE;
2501
2502 for (i = 0; i < ARRAY_SIZE(map); i++)
2503 if (nla_put_u32(skb, map[i].key, map[i].val))
2504 goto msg_full;
2505
2506 nla_nest_end(skb, stats);
2507
2508 return 0;
2509msg_full:
2510 nla_nest_cancel(skb, stats);
2511
2512 return -EMSGSIZE;
2513}
2514
2515/* Caller should hold appropriate locks to protect the link */
34747539
YX
2516static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2517 struct tipc_link *link)
7be57fc6
RA
2518{
2519 int err;
2520 void *hdr;
2521 struct nlattr *attrs;
2522 struct nlattr *prop;
34747539 2523 struct tipc_net *tn = net_generic(net, tipc_net_id);
7be57fc6
RA
2524
2525 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2526 NLM_F_MULTI, TIPC_NL_LINK_GET);
2527 if (!hdr)
2528 return -EMSGSIZE;
2529
2530 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2531 if (!attrs)
2532 goto msg_full;
2533
2534 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2535 goto attr_msg_full;
2536 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
34747539 2537 tipc_cluster_mask(tn->own_addr)))
7be57fc6
RA
2538 goto attr_msg_full;
2539 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2540 goto attr_msg_full;
2541 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2542 goto attr_msg_full;
2543 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2544 goto attr_msg_full;
2545
2546 if (tipc_link_is_up(link))
2547 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2548 goto attr_msg_full;
2549 if (tipc_link_is_active(link))
2550 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2551 goto attr_msg_full;
2552
2553 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2554 if (!prop)
2555 goto attr_msg_full;
2556 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2557 goto prop_msg_full;
2558 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2559 goto prop_msg_full;
2560 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2561 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2562 goto prop_msg_full;
2563 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2564 goto prop_msg_full;
2565 nla_nest_end(msg->skb, prop);
2566
2567 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2568 if (err)
2569 goto attr_msg_full;
2570
2571 nla_nest_end(msg->skb, attrs);
2572 genlmsg_end(msg->skb, hdr);
2573
2574 return 0;
2575
2576prop_msg_full:
2577 nla_nest_cancel(msg->skb, prop);
2578attr_msg_full:
2579 nla_nest_cancel(msg->skb, attrs);
2580msg_full:
2581 genlmsg_cancel(msg->skb, hdr);
2582
2583 return -EMSGSIZE;
2584}
2585
2586/* Caller should hold node lock */
34747539
YX
2587static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2588 struct tipc_node *node, u32 *prev_link)
7be57fc6
RA
2589{
2590 u32 i;
2591 int err;
2592
2593 for (i = *prev_link; i < MAX_BEARERS; i++) {
2594 *prev_link = i;
2595
2596 if (!node->links[i])
2597 continue;
2598
34747539 2599 err = __tipc_nl_add_link(net, msg, node->links[i]);
7be57fc6
RA
2600 if (err)
2601 return err;
2602 }
2603 *prev_link = 0;
2604
2605 return 0;
2606}
2607
2608int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2609{
f2f9800d
YX
2610 struct net *net = sock_net(skb->sk);
2611 struct tipc_net *tn = net_generic(net, tipc_net_id);
7be57fc6
RA
2612 struct tipc_node *node;
2613 struct tipc_nl_msg msg;
2614 u32 prev_node = cb->args[0];
2615 u32 prev_link = cb->args[1];
2616 int done = cb->args[2];
2617 int err;
2618
2619 if (done)
2620 return 0;
2621
2622 msg.skb = skb;
2623 msg.portid = NETLINK_CB(cb->skb).portid;
2624 msg.seq = cb->nlh->nlmsg_seq;
2625
2626 rcu_read_lock();
2627
2628 if (prev_node) {
f2f9800d 2629 node = tipc_node_find(net, prev_node);
7be57fc6
RA
2630 if (!node) {
2631 /* We never set seq or call nl_dump_check_consistent()
2632 * this means that setting prev_seq here will cause the
2633 * consistence check to fail in the netlink callback
2634 * handler. Resulting in the last NLMSG_DONE message
2635 * having the NLM_F_DUMP_INTR flag set.
2636 */
2637 cb->prev_seq = 1;
2638 goto out;
2639 }
2640
f2f9800d
YX
2641 list_for_each_entry_continue_rcu(node, &tn->node_list,
2642 list) {
7be57fc6 2643 tipc_node_lock(node);
34747539
YX
2644 err = __tipc_nl_add_node_links(net, &msg, node,
2645 &prev_link);
7be57fc6
RA
2646 tipc_node_unlock(node);
2647 if (err)
2648 goto out;
2649
2650 prev_node = node->addr;
2651 }
2652 } else {
1da46568 2653 err = tipc_nl_add_bc_link(net, &msg);
7be57fc6
RA
2654 if (err)
2655 goto out;
2656
f2f9800d 2657 list_for_each_entry_rcu(node, &tn->node_list, list) {
7be57fc6 2658 tipc_node_lock(node);
34747539
YX
2659 err = __tipc_nl_add_node_links(net, &msg, node,
2660 &prev_link);
7be57fc6
RA
2661 tipc_node_unlock(node);
2662 if (err)
2663 goto out;
2664
2665 prev_node = node->addr;
2666 }
2667 }
2668 done = 1;
2669out:
2670 rcu_read_unlock();
2671
2672 cb->args[0] = prev_node;
2673 cb->args[1] = prev_link;
2674 cb->args[2] = done;
2675
2676 return skb->len;
2677}
2678
2679int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2680{
f2f9800d 2681 struct net *net = genl_info_net(info);
7be57fc6
RA
2682 struct sk_buff *ans_skb;
2683 struct tipc_nl_msg msg;
2684 struct tipc_link *link;
2685 struct tipc_node *node;
2686 char *name;
2687 int bearer_id;
2688 int err;
2689
2690 if (!info->attrs[TIPC_NLA_LINK_NAME])
2691 return -EINVAL;
2692
2693 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
f2f9800d 2694 node = tipc_link_find_owner(net, name, &bearer_id);
7be57fc6
RA
2695 if (!node)
2696 return -EINVAL;
2697
2698 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2699 if (!ans_skb)
2700 return -ENOMEM;
2701
2702 msg.skb = ans_skb;
2703 msg.portid = info->snd_portid;
2704 msg.seq = info->snd_seq;
2705
2706 tipc_node_lock(node);
2707 link = node->links[bearer_id];
2708 if (!link) {
2709 err = -EINVAL;
2710 goto err_out;
2711 }
2712
34747539 2713 err = __tipc_nl_add_link(net, &msg, link);
7be57fc6
RA
2714 if (err)
2715 goto err_out;
2716
2717 tipc_node_unlock(node);
2718
2719 return genlmsg_reply(ans_skb, info);
2720
2721err_out:
2722 tipc_node_unlock(node);
2723 nlmsg_free(ans_skb);
2724
2725 return err;
2726}
ae36342b
RA
2727
2728int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2729{
2730 int err;
2731 char *link_name;
2732 unsigned int bearer_id;
2733 struct tipc_link *link;
2734 struct tipc_node *node;
2735 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
f2f9800d 2736 struct net *net = genl_info_net(info);
ae36342b
RA
2737
2738 if (!info->attrs[TIPC_NLA_LINK])
2739 return -EINVAL;
2740
2741 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2742 info->attrs[TIPC_NLA_LINK],
2743 tipc_nl_link_policy);
2744 if (err)
2745 return err;
2746
2747 if (!attrs[TIPC_NLA_LINK_NAME])
2748 return -EINVAL;
2749
2750 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2751
2752 if (strcmp(link_name, tipc_bclink_name) == 0) {
1da46568 2753 err = tipc_bclink_reset_stats(net);
ae36342b
RA
2754 if (err)
2755 return err;
2756 return 0;
2757 }
2758
f2f9800d 2759 node = tipc_link_find_owner(net, link_name, &bearer_id);
ae36342b
RA
2760 if (!node)
2761 return -EINVAL;
2762
2763 tipc_node_lock(node);
2764
2765 link = node->links[bearer_id];
2766 if (!link) {
2767 tipc_node_unlock(node);
2768 return -EINVAL;
2769 }
2770
2771 link_reset_statistics(link);
2772
2773 tipc_node_unlock(node);
2774
2775 return 0;
2776}