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