]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/tipc/link.c
phy: Add MOXA MDIO driver
[mirror_ubuntu-jammy-kernel.git] / net / tipc / link.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/link.c: TIPC link code
c4307285 3 *
c64f7a6a 4 * Copyright (c) 1996-2007, 2012, 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"
b97bf3fd 39#include "port.h"
b97bf3fd 40#include "name_distr.h"
b97bf3fd
PL
41#include "discover.h"
42#include "config.h"
b97bf3fd 43
796c75d0
YX
44#include <linux/pkt_sched.h>
45
2cf8aa19
EH
46/*
47 * Error message prefixes
48 */
49static const char *link_co_err = "Link changeover error, ";
50static const char *link_rst_msg = "Resetting link ";
51static const char *link_unk_evt = "Unknown link event ";
b97bf3fd 52
a686e685
AS
53/*
54 * Out-of-range value for link session numbers
55 */
a686e685
AS
56#define INVALID_SESSION 0x10000
57
c4307285
YH
58/*
59 * Link state events:
b97bf3fd 60 */
b97bf3fd
PL
61#define STARTING_EVT 856384768 /* link processing trigger */
62#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
63#define TIMEOUT_EVT 560817u /* link timer expired */
64
c4307285
YH
65/*
66 * The following two 'message types' is really just implementation
67 * data conveniently stored in the message header.
b97bf3fd
PL
68 * They must not be considered part of the protocol
69 */
70#define OPEN_MSG 0
71#define CLOSED_MSG 1
72
c4307285 73/*
b97bf3fd
PL
74 * State value stored in 'exp_msg_count'
75 */
b97bf3fd
PL
76#define START_CHANGEOVER 100000u
77
a18c4bc3 78static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
b97bf3fd 79 struct sk_buff *buf);
a18c4bc3
PG
80static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf);
81static int link_recv_changeover_msg(struct tipc_link **l_ptr,
82 struct sk_buff **buf);
83static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
23dd4cce 84static int link_send_sections_long(struct tipc_port *sender,
b97bf3fd 85 struct iovec const *msg_sect,
9446b87a 86 unsigned int len, u32 destnode);
a18c4bc3
PG
87static void link_state_event(struct tipc_link *l_ptr, u32 event);
88static void link_reset_statistics(struct tipc_link *l_ptr);
89static void link_print(struct tipc_link *l_ptr, const char *str);
90static void link_start(struct tipc_link *l_ptr);
91static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
c64f7a6a
JM
92static void tipc_link_send_sync(struct tipc_link *l);
93static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf);
31e3c3f6 94
b97bf3fd 95/*
05790c64 96 * Simple link routines
b97bf3fd 97 */
05790c64 98static unsigned int align(unsigned int i)
b97bf3fd
PL
99{
100 return (i + 3) & ~3u;
101}
102
a18c4bc3 103static void link_init_max_pkt(struct tipc_link *l_ptr)
b97bf3fd
PL
104{
105 u32 max_pkt;
c4307285 106
2d627b92 107 max_pkt = (l_ptr->b_ptr->mtu & ~3);
b97bf3fd
PL
108 if (max_pkt > MAX_MSG_SIZE)
109 max_pkt = MAX_MSG_SIZE;
110
c4307285 111 l_ptr->max_pkt_target = max_pkt;
b97bf3fd
PL
112 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
113 l_ptr->max_pkt = l_ptr->max_pkt_target;
c4307285 114 else
b97bf3fd
PL
115 l_ptr->max_pkt = MAX_PKT_DEFAULT;
116
c4307285 117 l_ptr->max_pkt_probes = 0;
b97bf3fd
PL
118}
119
a18c4bc3 120static u32 link_next_sent(struct tipc_link *l_ptr)
b97bf3fd
PL
121{
122 if (l_ptr->next_out)
f905730c 123 return buf_seqno(l_ptr->next_out);
b97bf3fd
PL
124 return mod(l_ptr->next_out_no);
125}
126
a18c4bc3 127static u32 link_last_sent(struct tipc_link *l_ptr)
b97bf3fd
PL
128{
129 return mod(link_next_sent(l_ptr) - 1);
130}
131
132/*
05790c64 133 * Simple non-static link routines (i.e. referenced outside this file)
b97bf3fd 134 */
a18c4bc3 135int tipc_link_is_up(struct tipc_link *l_ptr)
b97bf3fd
PL
136{
137 if (!l_ptr)
138 return 0;
a02cec21 139 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
b97bf3fd
PL
140}
141
a18c4bc3 142int tipc_link_is_active(struct tipc_link *l_ptr)
b97bf3fd 143{
a02cec21
ED
144 return (l_ptr->owner->active_links[0] == l_ptr) ||
145 (l_ptr->owner->active_links[1] == l_ptr);
b97bf3fd
PL
146}
147
b97bf3fd
PL
148/**
149 * link_timeout - handle expiration of link timer
150 * @l_ptr: pointer to link
c4307285 151 *
4323add6
PL
152 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
153 * with tipc_link_delete(). (There is no risk that the node will be deleted by
154 * another thread because tipc_link_delete() always cancels the link timer before
155 * tipc_node_delete() is called.)
b97bf3fd 156 */
a18c4bc3 157static void link_timeout(struct tipc_link *l_ptr)
b97bf3fd 158{
4323add6 159 tipc_node_lock(l_ptr->owner);
b97bf3fd
PL
160
161 /* update counters used in statistical profiling of send traffic */
b97bf3fd
PL
162 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
163 l_ptr->stats.queue_sz_counts++;
164
b97bf3fd
PL
165 if (l_ptr->first_out) {
166 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
167 u32 length = msg_size(msg);
168
f64f9e71
JP
169 if ((msg_user(msg) == MSG_FRAGMENTER) &&
170 (msg_type(msg) == FIRST_FRAGMENT)) {
b97bf3fd
PL
171 length = msg_size(msg_get_wrapped(msg));
172 }
173 if (length) {
174 l_ptr->stats.msg_lengths_total += length;
175 l_ptr->stats.msg_length_counts++;
176 if (length <= 64)
177 l_ptr->stats.msg_length_profile[0]++;
178 else if (length <= 256)
179 l_ptr->stats.msg_length_profile[1]++;
180 else if (length <= 1024)
181 l_ptr->stats.msg_length_profile[2]++;
182 else if (length <= 4096)
183 l_ptr->stats.msg_length_profile[3]++;
184 else if (length <= 16384)
185 l_ptr->stats.msg_length_profile[4]++;
186 else if (length <= 32768)
187 l_ptr->stats.msg_length_profile[5]++;
188 else
189 l_ptr->stats.msg_length_profile[6]++;
190 }
191 }
192
193 /* do all other link processing performed on a periodic basis */
b97bf3fd
PL
194
195 link_state_event(l_ptr, TIMEOUT_EVT);
196
197 if (l_ptr->next_out)
4323add6 198 tipc_link_push_queue(l_ptr);
b97bf3fd 199
4323add6 200 tipc_node_unlock(l_ptr->owner);
b97bf3fd
PL
201}
202
a18c4bc3 203static void link_set_timer(struct tipc_link *l_ptr, u32 time)
b97bf3fd
PL
204{
205 k_start_timer(&l_ptr->timer, time);
206}
207
208/**
4323add6 209 * tipc_link_create - create a new link
37b9c08a 210 * @n_ptr: pointer to associated node
b97bf3fd 211 * @b_ptr: pointer to associated bearer
b97bf3fd 212 * @media_addr: media address to use when sending messages over link
c4307285 213 *
b97bf3fd
PL
214 * Returns pointer to link.
215 */
a18c4bc3 216struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
37b9c08a 217 struct tipc_bearer *b_ptr,
4323add6 218 const struct tipc_media_addr *media_addr)
b97bf3fd 219{
a18c4bc3 220 struct tipc_link *l_ptr;
b97bf3fd
PL
221 struct tipc_msg *msg;
222 char *if_name;
37b9c08a
AS
223 char addr_string[16];
224 u32 peer = n_ptr->addr;
225
226 if (n_ptr->link_cnt >= 2) {
227 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19 228 pr_err("Attempt to establish third link to %s\n", addr_string);
37b9c08a
AS
229 return NULL;
230 }
231
232 if (n_ptr->links[b_ptr->identity]) {
233 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19
EH
234 pr_err("Attempt to establish second link on <%s> to %s\n",
235 b_ptr->name, addr_string);
37b9c08a
AS
236 return NULL;
237 }
b97bf3fd 238
0da974f4 239 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
b97bf3fd 240 if (!l_ptr) {
2cf8aa19 241 pr_warn("Link creation failed, no memory\n");
b97bf3fd
PL
242 return NULL;
243 }
b97bf3fd
PL
244
245 l_ptr->addr = peer;
2d627b92 246 if_name = strchr(b_ptr->name, ':') + 1;
062b4c99 247 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
b97bf3fd 248 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
c4307285 249 tipc_node(tipc_own_addr),
b97bf3fd
PL
250 if_name,
251 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
062b4c99 252 /* note: peer i/f name is updated by reset/activate message */
b97bf3fd 253 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
37b9c08a 254 l_ptr->owner = n_ptr;
b97bf3fd 255 l_ptr->checkpoint = 1;
f882cb76 256 l_ptr->peer_session = INVALID_SESSION;
b97bf3fd 257 l_ptr->b_ptr = b_ptr;
5c216e1d 258 link_set_supervision_props(l_ptr, b_ptr->tolerance);
b97bf3fd
PL
259 l_ptr->state = RESET_UNKNOWN;
260
261 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
262 msg = l_ptr->pmsg;
c68ca7b7 263 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd 264 msg_set_size(msg, sizeof(l_ptr->proto_msg));
a686e685 265 msg_set_session(msg, (tipc_random & 0xffff));
b97bf3fd
PL
266 msg_set_bearer_id(msg, b_ptr->identity);
267 strcpy((char *)msg_data(msg), if_name);
268
269 l_ptr->priority = b_ptr->priority;
5c216e1d 270 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
b97bf3fd
PL
271
272 link_init_max_pkt(l_ptr);
273
274 l_ptr->next_out_no = 1;
275 INIT_LIST_HEAD(&l_ptr->waiting_ports);
276
277 link_reset_statistics(l_ptr);
278
37b9c08a 279 tipc_node_attach_link(n_ptr, l_ptr);
b97bf3fd 280
94571065
FW
281 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
282 list_add_tail(&l_ptr->link_list, &b_ptr->links);
31e3c3f6 283 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
b97bf3fd 284
b97bf3fd
PL
285 return l_ptr;
286}
287
c4307285 288/**
4323add6 289 * tipc_link_delete - delete a link
b97bf3fd 290 * @l_ptr: pointer to link
c4307285 291 *
4323add6 292 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
b97bf3fd 293 * This routine must not grab the node lock until after link timer cancellation
c4307285 294 * to avoid a potential deadlock situation.
b97bf3fd 295 */
a18c4bc3 296void tipc_link_delete(struct tipc_link *l_ptr)
b97bf3fd
PL
297{
298 if (!l_ptr) {
2cf8aa19 299 pr_err("Attempt to delete non-existent link\n");
b97bf3fd
PL
300 return;
301 }
302
b97bf3fd 303 k_cancel_timer(&l_ptr->timer);
c4307285 304
4323add6
PL
305 tipc_node_lock(l_ptr->owner);
306 tipc_link_reset(l_ptr);
307 tipc_node_detach_link(l_ptr->owner, l_ptr);
308 tipc_link_stop(l_ptr);
b97bf3fd 309 list_del_init(&l_ptr->link_list);
4323add6 310 tipc_node_unlock(l_ptr->owner);
b97bf3fd
PL
311 k_term_timer(&l_ptr->timer);
312 kfree(l_ptr);
313}
314
a18c4bc3 315static void link_start(struct tipc_link *l_ptr)
b97bf3fd 316{
214dda4a 317 tipc_node_lock(l_ptr->owner);
b97bf3fd 318 link_state_event(l_ptr, STARTING_EVT);
214dda4a 319 tipc_node_unlock(l_ptr->owner);
b97bf3fd
PL
320}
321
322/**
c4307285 323 * link_schedule_port - schedule port for deferred sending
b97bf3fd
PL
324 * @l_ptr: pointer to link
325 * @origport: reference to sending port
326 * @sz: amount of data to be sent
c4307285
YH
327 *
328 * Schedules port for renewed sending of messages after link congestion
b97bf3fd
PL
329 * has abated.
330 */
a18c4bc3 331static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
b97bf3fd 332{
23dd4cce 333 struct tipc_port *p_ptr;
b97bf3fd 334
4323add6
PL
335 spin_lock_bh(&tipc_port_list_lock);
336 p_ptr = tipc_port_lock(origport);
b97bf3fd
PL
337 if (p_ptr) {
338 if (!p_ptr->wakeup)
339 goto exit;
340 if (!list_empty(&p_ptr->wait_list))
341 goto exit;
23dd4cce 342 p_ptr->congested = 1;
15e979da 343 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
b97bf3fd
PL
344 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
345 l_ptr->stats.link_congs++;
346exit:
4323add6 347 tipc_port_unlock(p_ptr);
b97bf3fd 348 }
4323add6 349 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
350 return -ELINKCONG;
351}
352
a18c4bc3 353void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
b97bf3fd 354{
23dd4cce
AS
355 struct tipc_port *p_ptr;
356 struct tipc_port *temp_p_ptr;
b97bf3fd
PL
357 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
358
359 if (all)
360 win = 100000;
361 if (win <= 0)
362 return;
4323add6 363 if (!spin_trylock_bh(&tipc_port_list_lock))
b97bf3fd
PL
364 return;
365 if (link_congested(l_ptr))
366 goto exit;
c4307285 367 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
b97bf3fd
PL
368 wait_list) {
369 if (win <= 0)
370 break;
371 list_del_init(&p_ptr->wait_list);
23dd4cce
AS
372 spin_lock_bh(p_ptr->lock);
373 p_ptr->congested = 0;
374 p_ptr->wakeup(p_ptr);
b97bf3fd 375 win -= p_ptr->waiting_pkts;
23dd4cce 376 spin_unlock_bh(p_ptr->lock);
b97bf3fd
PL
377 }
378
379exit:
4323add6 380 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
381}
382
c4307285 383/**
b97bf3fd
PL
384 * link_release_outqueue - purge link's outbound message queue
385 * @l_ptr: pointer to link
386 */
a18c4bc3 387static void link_release_outqueue(struct tipc_link *l_ptr)
b97bf3fd
PL
388{
389 struct sk_buff *buf = l_ptr->first_out;
390 struct sk_buff *next;
391
392 while (buf) {
393 next = buf->next;
5f6d9123 394 kfree_skb(buf);
b97bf3fd
PL
395 buf = next;
396 }
397 l_ptr->first_out = NULL;
398 l_ptr->out_queue_size = 0;
399}
400
401/**
4323add6 402 * tipc_link_reset_fragments - purge link's inbound message fragments queue
b97bf3fd
PL
403 * @l_ptr: pointer to link
404 */
a18c4bc3 405void tipc_link_reset_fragments(struct tipc_link *l_ptr)
b97bf3fd
PL
406{
407 struct sk_buff *buf = l_ptr->defragm_buf;
408 struct sk_buff *next;
409
410 while (buf) {
411 next = buf->next;
5f6d9123 412 kfree_skb(buf);
b97bf3fd
PL
413 buf = next;
414 }
415 l_ptr->defragm_buf = NULL;
416}
417
c4307285 418/**
4323add6 419 * tipc_link_stop - purge all inbound and outbound messages associated with link
b97bf3fd
PL
420 * @l_ptr: pointer to link
421 */
a18c4bc3 422void tipc_link_stop(struct tipc_link *l_ptr)
b97bf3fd
PL
423{
424 struct sk_buff *buf;
425 struct sk_buff *next;
426
427 buf = l_ptr->oldest_deferred_in;
428 while (buf) {
429 next = buf->next;
5f6d9123 430 kfree_skb(buf);
b97bf3fd
PL
431 buf = next;
432 }
433
434 buf = l_ptr->first_out;
435 while (buf) {
436 next = buf->next;
5f6d9123 437 kfree_skb(buf);
b97bf3fd
PL
438 buf = next;
439 }
440
4323add6 441 tipc_link_reset_fragments(l_ptr);
b97bf3fd 442
5f6d9123 443 kfree_skb(l_ptr->proto_msg_queue);
b97bf3fd
PL
444 l_ptr->proto_msg_queue = NULL;
445}
446
a18c4bc3 447void tipc_link_reset(struct tipc_link *l_ptr)
b97bf3fd
PL
448{
449 struct sk_buff *buf;
450 u32 prev_state = l_ptr->state;
451 u32 checkpoint = l_ptr->next_in_no;
5392d646 452 int was_active_link = tipc_link_is_active(l_ptr);
c4307285 453
a686e685 454 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
b97bf3fd 455
a686e685
AS
456 /* Link is down, accept any session */
457 l_ptr->peer_session = INVALID_SESSION;
b97bf3fd 458
c4307285 459 /* Prepare for max packet size negotiation */
b97bf3fd 460 link_init_max_pkt(l_ptr);
c4307285 461
b97bf3fd 462 l_ptr->state = RESET_UNKNOWN;
b97bf3fd
PL
463
464 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
465 return;
466
4323add6
PL
467 tipc_node_link_down(l_ptr->owner, l_ptr);
468 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
7368ddf1 469
8f19afb2 470 if (was_active_link && tipc_node_active_links(l_ptr->owner) &&
b97bf3fd
PL
471 l_ptr->owner->permit_changeover) {
472 l_ptr->reset_checkpoint = checkpoint;
473 l_ptr->exp_msg_count = START_CHANGEOVER;
474 }
475
476 /* Clean up all queues: */
b97bf3fd 477 link_release_outqueue(l_ptr);
5f6d9123 478 kfree_skb(l_ptr->proto_msg_queue);
b97bf3fd
PL
479 l_ptr->proto_msg_queue = NULL;
480 buf = l_ptr->oldest_deferred_in;
481 while (buf) {
482 struct sk_buff *next = buf->next;
5f6d9123 483 kfree_skb(buf);
b97bf3fd
PL
484 buf = next;
485 }
486 if (!list_empty(&l_ptr->waiting_ports))
4323add6 487 tipc_link_wakeup_ports(l_ptr, 1);
b97bf3fd
PL
488
489 l_ptr->retransm_queue_head = 0;
490 l_ptr->retransm_queue_size = 0;
491 l_ptr->last_out = NULL;
492 l_ptr->first_out = NULL;
493 l_ptr->next_out = NULL;
494 l_ptr->unacked_window = 0;
495 l_ptr->checkpoint = 1;
496 l_ptr->next_out_no = 1;
497 l_ptr->deferred_inqueue_sz = 0;
498 l_ptr->oldest_deferred_in = NULL;
499 l_ptr->newest_deferred_in = NULL;
500 l_ptr->fsm_msg_cnt = 0;
501 l_ptr->stale_count = 0;
502 link_reset_statistics(l_ptr);
b97bf3fd
PL
503}
504
505
a18c4bc3 506static void link_activate(struct tipc_link *l_ptr)
b97bf3fd 507{
5392d646 508 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
4323add6
PL
509 tipc_node_link_up(l_ptr->owner, l_ptr);
510 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
b97bf3fd
PL
511}
512
513/**
514 * link_state_event - link finite state machine
515 * @l_ptr: pointer to link
516 * @event: state machine event to process
517 */
95c96174 518static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
b97bf3fd 519{
a18c4bc3 520 struct tipc_link *other;
b97bf3fd
PL
521 u32 cont_intv = l_ptr->continuity_interval;
522
523 if (!l_ptr->started && (event != STARTING_EVT))
524 return; /* Not yet. */
525
526 if (link_blocked(l_ptr)) {
a016892c 527 if (event == TIMEOUT_EVT)
b97bf3fd 528 link_set_timer(l_ptr, cont_intv);
b97bf3fd
PL
529 return; /* Changeover going on */
530 }
b97bf3fd
PL
531
532 switch (l_ptr->state) {
533 case WORKING_WORKING:
b97bf3fd
PL
534 switch (event) {
535 case TRAFFIC_MSG_EVT:
b97bf3fd 536 case ACTIVATE_MSG:
b97bf3fd
PL
537 break;
538 case TIMEOUT_EVT:
b97bf3fd
PL
539 if (l_ptr->next_in_no != l_ptr->checkpoint) {
540 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6 541 if (tipc_bclink_acks_missing(l_ptr->owner)) {
c4307285 542 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
4323add6 543 0, 0, 0, 0, 0);
b97bf3fd
PL
544 l_ptr->fsm_msg_cnt++;
545 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
c4307285 546 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
4323add6 547 1, 0, 0, 0, 0);
b97bf3fd
PL
548 l_ptr->fsm_msg_cnt++;
549 }
550 link_set_timer(l_ptr, cont_intv);
551 break;
552 }
b97bf3fd
PL
553 l_ptr->state = WORKING_UNKNOWN;
554 l_ptr->fsm_msg_cnt = 0;
4323add6 555 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
556 l_ptr->fsm_msg_cnt++;
557 link_set_timer(l_ptr, cont_intv / 4);
558 break;
559 case RESET_MSG:
2cf8aa19
EH
560 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
561 l_ptr->name);
4323add6 562 tipc_link_reset(l_ptr);
b97bf3fd
PL
563 l_ptr->state = RESET_RESET;
564 l_ptr->fsm_msg_cnt = 0;
4323add6 565 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
566 l_ptr->fsm_msg_cnt++;
567 link_set_timer(l_ptr, cont_intv);
568 break;
569 default:
2cf8aa19 570 pr_err("%s%u in WW state\n", link_unk_evt, event);
b97bf3fd
PL
571 }
572 break;
573 case WORKING_UNKNOWN:
b97bf3fd
PL
574 switch (event) {
575 case TRAFFIC_MSG_EVT:
b97bf3fd 576 case ACTIVATE_MSG:
b97bf3fd
PL
577 l_ptr->state = WORKING_WORKING;
578 l_ptr->fsm_msg_cnt = 0;
579 link_set_timer(l_ptr, cont_intv);
580 break;
581 case RESET_MSG:
2cf8aa19
EH
582 pr_info("%s<%s>, requested by peer while probing\n",
583 link_rst_msg, l_ptr->name);
4323add6 584 tipc_link_reset(l_ptr);
b97bf3fd
PL
585 l_ptr->state = RESET_RESET;
586 l_ptr->fsm_msg_cnt = 0;
4323add6 587 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
588 l_ptr->fsm_msg_cnt++;
589 link_set_timer(l_ptr, cont_intv);
590 break;
591 case TIMEOUT_EVT:
b97bf3fd 592 if (l_ptr->next_in_no != l_ptr->checkpoint) {
b97bf3fd
PL
593 l_ptr->state = WORKING_WORKING;
594 l_ptr->fsm_msg_cnt = 0;
595 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6
PL
596 if (tipc_bclink_acks_missing(l_ptr->owner)) {
597 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
598 0, 0, 0, 0, 0);
b97bf3fd
PL
599 l_ptr->fsm_msg_cnt++;
600 }
601 link_set_timer(l_ptr, cont_intv);
602 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
c4307285 603 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
4323add6 604 1, 0, 0, 0, 0);
b97bf3fd
PL
605 l_ptr->fsm_msg_cnt++;
606 link_set_timer(l_ptr, cont_intv / 4);
607 } else { /* Link has failed */
2cf8aa19
EH
608 pr_warn("%s<%s>, peer not responding\n",
609 link_rst_msg, l_ptr->name);
4323add6 610 tipc_link_reset(l_ptr);
b97bf3fd
PL
611 l_ptr->state = RESET_UNKNOWN;
612 l_ptr->fsm_msg_cnt = 0;
4323add6
PL
613 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
614 0, 0, 0, 0, 0);
b97bf3fd
PL
615 l_ptr->fsm_msg_cnt++;
616 link_set_timer(l_ptr, cont_intv);
617 }
618 break;
619 default:
2cf8aa19 620 pr_err("%s%u in WU state\n", link_unk_evt, event);
b97bf3fd
PL
621 }
622 break;
623 case RESET_UNKNOWN:
b97bf3fd
PL
624 switch (event) {
625 case TRAFFIC_MSG_EVT:
b97bf3fd
PL
626 break;
627 case ACTIVATE_MSG:
628 other = l_ptr->owner->active_links[0];
8d64a5ba 629 if (other && link_working_unknown(other))
b97bf3fd 630 break;
b97bf3fd
PL
631 l_ptr->state = WORKING_WORKING;
632 l_ptr->fsm_msg_cnt = 0;
633 link_activate(l_ptr);
4323add6 634 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd 635 l_ptr->fsm_msg_cnt++;
c64f7a6a
JM
636 if (l_ptr->owner->working_links == 1)
637 tipc_link_send_sync(l_ptr);
b97bf3fd
PL
638 link_set_timer(l_ptr, cont_intv);
639 break;
640 case RESET_MSG:
b97bf3fd
PL
641 l_ptr->state = RESET_RESET;
642 l_ptr->fsm_msg_cnt = 0;
4323add6 643 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
644 l_ptr->fsm_msg_cnt++;
645 link_set_timer(l_ptr, cont_intv);
646 break;
647 case STARTING_EVT:
b97bf3fd
PL
648 l_ptr->started = 1;
649 /* fall through */
650 case TIMEOUT_EVT:
4323add6 651 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
652 l_ptr->fsm_msg_cnt++;
653 link_set_timer(l_ptr, cont_intv);
654 break;
655 default:
2cf8aa19 656 pr_err("%s%u in RU state\n", link_unk_evt, event);
b97bf3fd
PL
657 }
658 break;
659 case RESET_RESET:
b97bf3fd
PL
660 switch (event) {
661 case TRAFFIC_MSG_EVT:
b97bf3fd
PL
662 case ACTIVATE_MSG:
663 other = l_ptr->owner->active_links[0];
8d64a5ba 664 if (other && link_working_unknown(other))
b97bf3fd 665 break;
b97bf3fd
PL
666 l_ptr->state = WORKING_WORKING;
667 l_ptr->fsm_msg_cnt = 0;
668 link_activate(l_ptr);
4323add6 669 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd 670 l_ptr->fsm_msg_cnt++;
c64f7a6a
JM
671 if (l_ptr->owner->working_links == 1)
672 tipc_link_send_sync(l_ptr);
b97bf3fd
PL
673 link_set_timer(l_ptr, cont_intv);
674 break;
675 case RESET_MSG:
b97bf3fd
PL
676 break;
677 case TIMEOUT_EVT:
4323add6 678 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
679 l_ptr->fsm_msg_cnt++;
680 link_set_timer(l_ptr, cont_intv);
b97bf3fd
PL
681 break;
682 default:
2cf8aa19 683 pr_err("%s%u in RR state\n", link_unk_evt, event);
b97bf3fd
PL
684 }
685 break;
686 default:
2cf8aa19 687 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
b97bf3fd
PL
688 }
689}
690
691/*
692 * link_bundle_buf(): Append contents of a buffer to
c4307285 693 * the tail of an existing one.
b97bf3fd 694 */
ae8509c4 695static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
b97bf3fd
PL
696 struct sk_buff *buf)
697{
698 struct tipc_msg *bundler_msg = buf_msg(bundler);
699 struct tipc_msg *msg = buf_msg(buf);
700 u32 size = msg_size(msg);
e49060c7
AS
701 u32 bundle_size = msg_size(bundler_msg);
702 u32 to_pos = align(bundle_size);
703 u32 pad = to_pos - bundle_size;
b97bf3fd
PL
704
705 if (msg_user(bundler_msg) != MSG_BUNDLER)
706 return 0;
707 if (msg_type(bundler_msg) != OPEN_MSG)
708 return 0;
e49060c7 709 if (skb_tailroom(bundler) < (pad + size))
b97bf3fd 710 return 0;
15e979da 711 if (l_ptr->max_pkt < (to_pos + size))
863fae66 712 return 0;
b97bf3fd 713
e49060c7 714 skb_put(bundler, pad + size);
27d7ff46 715 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
b97bf3fd
PL
716 msg_set_size(bundler_msg, to_pos + size);
717 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
5f6d9123 718 kfree_skb(buf);
b97bf3fd
PL
719 l_ptr->stats.sent_bundled++;
720 return 1;
721}
722
a18c4bc3 723static void link_add_to_outqueue(struct tipc_link *l_ptr,
05790c64
SR
724 struct sk_buff *buf,
725 struct tipc_msg *msg)
b97bf3fd
PL
726{
727 u32 ack = mod(l_ptr->next_in_no - 1);
728 u32 seqno = mod(l_ptr->next_out_no++);
729
730 msg_set_word(msg, 2, ((ack << 16) | seqno));
731 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
732 buf->next = NULL;
733 if (l_ptr->first_out) {
734 l_ptr->last_out->next = buf;
735 l_ptr->last_out = buf;
736 } else
737 l_ptr->first_out = l_ptr->last_out = buf;
9bd80b60 738
b97bf3fd 739 l_ptr->out_queue_size++;
9bd80b60
AS
740 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
741 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
b97bf3fd
PL
742}
743
a18c4bc3 744static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
dc63d91e
AS
745 struct sk_buff *buf_chain,
746 u32 long_msgno)
747{
748 struct sk_buff *buf;
749 struct tipc_msg *msg;
750
751 if (!l_ptr->next_out)
752 l_ptr->next_out = buf_chain;
753 while (buf_chain) {
754 buf = buf_chain;
755 buf_chain = buf_chain->next;
756
757 msg = buf_msg(buf);
758 msg_set_long_msgno(msg, long_msgno);
759 link_add_to_outqueue(l_ptr, buf, msg);
760 }
761}
762
c4307285
YH
763/*
764 * tipc_link_send_buf() is the 'full path' for messages, called from
b97bf3fd
PL
765 * inside TIPC when the 'fast path' in tipc_send_buf
766 * has failed, and from link_send()
767 */
a18c4bc3 768int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
b97bf3fd
PL
769{
770 struct tipc_msg *msg = buf_msg(buf);
771 u32 size = msg_size(msg);
772 u32 dsz = msg_data_sz(msg);
773 u32 queue_size = l_ptr->out_queue_size;
c68ca7b7 774 u32 imp = tipc_msg_tot_importance(msg);
b97bf3fd 775 u32 queue_limit = l_ptr->queue_limit[imp];
15e979da 776 u32 max_packet = l_ptr->max_pkt;
b97bf3fd 777
b97bf3fd 778 /* Match msg importance against queue limits: */
b97bf3fd
PL
779 if (unlikely(queue_size >= queue_limit)) {
780 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
bebc55ae 781 link_schedule_port(l_ptr, msg_origport(msg), size);
5f6d9123 782 kfree_skb(buf);
bebc55ae 783 return -ELINKCONG;
b97bf3fd 784 }
5f6d9123 785 kfree_skb(buf);
b97bf3fd 786 if (imp > CONN_MANAGER) {
2cf8aa19
EH
787 pr_warn("%s<%s>, send queue full", link_rst_msg,
788 l_ptr->name);
4323add6 789 tipc_link_reset(l_ptr);
b97bf3fd
PL
790 }
791 return dsz;
792 }
793
794 /* Fragmentation needed ? */
b97bf3fd 795 if (size > max_packet)
31e3c3f6 796 return link_send_long_buf(l_ptr, buf);
b97bf3fd 797
617d3c7a 798 /* Packet can be queued or sent. */
3c294cb3 799 if (likely(!tipc_bearer_blocked(l_ptr->b_ptr) &&
b97bf3fd
PL
800 !link_congested(l_ptr))) {
801 link_add_to_outqueue(l_ptr, buf, msg);
802
3c294cb3
YX
803 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
804 l_ptr->unacked_window = 0;
b97bf3fd
PL
805 return dsz;
806 }
617d3c7a 807 /* Congestion: can message be bundled ? */
b97bf3fd
PL
808 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
809 (msg_user(msg) != MSG_FRAGMENTER)) {
810
811 /* Try adding message to an existing bundle */
c4307285 812 if (l_ptr->next_out &&
3c294cb3 813 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
b97bf3fd 814 return dsz;
b97bf3fd
PL
815
816 /* Try creating a new bundle */
b97bf3fd 817 if (size <= max_packet * 2 / 3) {
31e3c3f6 818 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
b97bf3fd
PL
819 struct tipc_msg bundler_hdr;
820
821 if (bundler) {
c68ca7b7 822 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
75715217 823 INT_H_SIZE, l_ptr->addr);
27d7ff46
ACM
824 skb_copy_to_linear_data(bundler, &bundler_hdr,
825 INT_H_SIZE);
b97bf3fd
PL
826 skb_trim(bundler, INT_H_SIZE);
827 link_bundle_buf(l_ptr, bundler, buf);
828 buf = bundler;
829 msg = buf_msg(buf);
830 l_ptr->stats.sent_bundles++;
831 }
832 }
833 }
834 if (!l_ptr->next_out)
835 l_ptr->next_out = buf;
836 link_add_to_outqueue(l_ptr, buf, msg);
b97bf3fd
PL
837 return dsz;
838}
839
c4307285
YH
840/*
841 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
b97bf3fd
PL
842 * not been selected yet, and the the owner node is not locked
843 * Called by TIPC internal users, e.g. the name distributor
844 */
4323add6 845int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
b97bf3fd 846{
a18c4bc3 847 struct tipc_link *l_ptr;
6c00055a 848 struct tipc_node *n_ptr;
b97bf3fd
PL
849 int res = -ELINKCONG;
850
4323add6 851 read_lock_bh(&tipc_net_lock);
51a8e4de 852 n_ptr = tipc_node_find(dest);
b97bf3fd 853 if (n_ptr) {
4323add6 854 tipc_node_lock(n_ptr);
b97bf3fd 855 l_ptr = n_ptr->active_links[selector & 1];
a016892c 856 if (l_ptr)
4323add6 857 res = tipc_link_send_buf(l_ptr, buf);
a016892c 858 else
5f6d9123 859 kfree_skb(buf);
4323add6 860 tipc_node_unlock(n_ptr);
b97bf3fd 861 } else {
5f6d9123 862 kfree_skb(buf);
b97bf3fd 863 }
4323add6 864 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
865 return res;
866}
867
c64f7a6a
JM
868/*
869 * tipc_link_send_sync - synchronize broadcast link endpoints.
870 *
871 * Give a newly added peer node the sequence number where it should
872 * start receiving and acking broadcast packets.
873 *
874 * Called with node locked
875 */
876static void tipc_link_send_sync(struct tipc_link *l)
877{
878 struct sk_buff *buf;
879 struct tipc_msg *msg;
880
881 buf = tipc_buf_acquire(INT_H_SIZE);
882 if (!buf)
883 return;
884
885 msg = buf_msg(buf);
886 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
887 msg_set_last_bcast(msg, l->owner->bclink.acked);
888 link_add_chain_to_outqueue(l, buf, 0);
889 tipc_link_push_queue(l);
890}
891
892/*
893 * tipc_link_recv_sync - synchronize broadcast link endpoints.
894 * Receive the sequence number where we should start receiving and
895 * acking broadcast packets from a newly added peer node, and open
896 * up for reception of such packets.
897 *
898 * Called with node locked
899 */
900static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf)
901{
902 struct tipc_msg *msg = buf_msg(buf);
903
904 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
905 n->bclink.recv_permitted = true;
906 kfree_skb(buf);
907}
908
909/*
9aa88c2a
AS
910 * tipc_link_send_names - send name table entries to new neighbor
911 *
912 * Send routine for bulk delivery of name table messages when contact
913 * with a new neighbor occurs. No link congestion checking is performed
914 * because name table messages *must* be delivered. The messages must be
915 * small enough not to require fragmentation.
916 * Called without any locks held.
917 */
9aa88c2a
AS
918void tipc_link_send_names(struct list_head *message_list, u32 dest)
919{
920 struct tipc_node *n_ptr;
a18c4bc3 921 struct tipc_link *l_ptr;
9aa88c2a
AS
922 struct sk_buff *buf;
923 struct sk_buff *temp_buf;
924
925 if (list_empty(message_list))
926 return;
927
928 read_lock_bh(&tipc_net_lock);
929 n_ptr = tipc_node_find(dest);
930 if (n_ptr) {
931 tipc_node_lock(n_ptr);
932 l_ptr = n_ptr->active_links[0];
933 if (l_ptr) {
934 /* convert circular list to linear list */
935 ((struct sk_buff *)message_list->prev)->next = NULL;
936 link_add_chain_to_outqueue(l_ptr,
937 (struct sk_buff *)message_list->next, 0);
938 tipc_link_push_queue(l_ptr);
939 INIT_LIST_HEAD(message_list);
940 }
941 tipc_node_unlock(n_ptr);
942 }
943 read_unlock_bh(&tipc_net_lock);
944
945 /* discard the messages if they couldn't be sent */
9aa88c2a
AS
946 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
947 list_del((struct list_head *)buf);
5f6d9123 948 kfree_skb(buf);
9aa88c2a
AS
949 }
950}
951
c4307285
YH
952/*
953 * link_send_buf_fast: Entry for data messages where the
b97bf3fd
PL
954 * destination link is known and the header is complete,
955 * inclusive total message length. Very time critical.
956 * Link is locked. Returns user data length.
957 */
a18c4bc3 958static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
05790c64 959 u32 *used_max_pkt)
b97bf3fd
PL
960{
961 struct tipc_msg *msg = buf_msg(buf);
962 int res = msg_data_sz(msg);
963
964 if (likely(!link_congested(l_ptr))) {
15e979da 965 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
3c294cb3 966 if (likely(!tipc_bearer_blocked(l_ptr->b_ptr))) {
b97bf3fd 967 link_add_to_outqueue(l_ptr, buf, msg);
3c294cb3
YX
968 tipc_bearer_send(l_ptr->b_ptr, buf,
969 &l_ptr->media_addr);
970 l_ptr->unacked_window = 0;
b97bf3fd
PL
971 return res;
972 }
0e65967e 973 } else
15e979da 974 *used_max_pkt = l_ptr->max_pkt;
b97bf3fd 975 }
4323add6 976 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
b97bf3fd
PL
977}
978
c4307285
YH
979/*
980 * tipc_link_send_sections_fast: Entry for messages where the
b97bf3fd 981 * destination processor is known and the header is complete,
c4307285 982 * except for total message length.
b97bf3fd
PL
983 * Returns user data length or errno.
984 */
23dd4cce 985int tipc_link_send_sections_fast(struct tipc_port *sender,
4323add6 986 struct iovec const *msg_sect,
9446b87a 987 unsigned int len, u32 destaddr)
b97bf3fd 988{
23dd4cce 989 struct tipc_msg *hdr = &sender->phdr;
a18c4bc3 990 struct tipc_link *l_ptr;
b97bf3fd 991 struct sk_buff *buf;
6c00055a 992 struct tipc_node *node;
b97bf3fd
PL
993 int res;
994 u32 selector = msg_origport(hdr) & 1;
995
b97bf3fd
PL
996again:
997 /*
998 * Try building message using port's max_pkt hint.
999 * (Must not hold any locks while building message.)
1000 */
9446b87a 1001 res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
7410f967
YX
1002 /* Exit if build request was invalid */
1003 if (unlikely(res < 0))
1004 return res;
b97bf3fd 1005
4323add6 1006 read_lock_bh(&tipc_net_lock);
51a8e4de 1007 node = tipc_node_find(destaddr);
b97bf3fd 1008 if (likely(node)) {
4323add6 1009 tipc_node_lock(node);
b97bf3fd
PL
1010 l_ptr = node->active_links[selector];
1011 if (likely(l_ptr)) {
1012 if (likely(buf)) {
1013 res = link_send_buf_fast(l_ptr, buf,
23dd4cce 1014 &sender->max_pkt);
b97bf3fd 1015exit:
4323add6
PL
1016 tipc_node_unlock(node);
1017 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1018 return res;
1019 }
1020
b97bf3fd 1021 /* Exit if link (or bearer) is congested */
c4307285 1022 if (link_congested(l_ptr) ||
3c294cb3 1023 tipc_bearer_blocked(l_ptr->b_ptr)) {
b97bf3fd 1024 res = link_schedule_port(l_ptr,
23dd4cce 1025 sender->ref, res);
b97bf3fd
PL
1026 goto exit;
1027 }
1028
c4307285 1029 /*
b97bf3fd
PL
1030 * Message size exceeds max_pkt hint; update hint,
1031 * then re-try fast path or fragment the message
1032 */
23dd4cce 1033 sender->max_pkt = l_ptr->max_pkt;
4323add6
PL
1034 tipc_node_unlock(node);
1035 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1036
1037
23dd4cce 1038 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
b97bf3fd
PL
1039 goto again;
1040
9446b87a 1041 return link_send_sections_long(sender, msg_sect, len,
26896904 1042 destaddr);
b97bf3fd 1043 }
4323add6 1044 tipc_node_unlock(node);
b97bf3fd 1045 }
4323add6 1046 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1047
1048 /* Couldn't find a link to the destination node */
b97bf3fd
PL
1049 if (buf)
1050 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1051 if (res >= 0)
9446b87a
YX
1052 return tipc_port_reject_sections(sender, hdr, msg_sect,
1053 len, TIPC_ERR_NO_NODE);
b97bf3fd
PL
1054 return res;
1055}
1056
c4307285
YH
1057/*
1058 * link_send_sections_long(): Entry for long messages where the
b97bf3fd 1059 * destination node is known and the header is complete,
c4307285 1060 * inclusive total message length.
b97bf3fd
PL
1061 * Link and bearer congestion status have been checked to be ok,
1062 * and are ignored if they change.
1063 *
1064 * Note that fragments do not use the full link MTU so that they won't have
1065 * to undergo refragmentation if link changeover causes them to be sent
1066 * over another link with an additional tunnel header added as prefix.
1067 * (Refragmentation will still occur if the other link has a smaller MTU.)
1068 *
1069 * Returns user data length or errno.
1070 */
23dd4cce 1071static int link_send_sections_long(struct tipc_port *sender,
b97bf3fd 1072 struct iovec const *msg_sect,
9446b87a 1073 unsigned int len, u32 destaddr)
b97bf3fd 1074{
a18c4bc3 1075 struct tipc_link *l_ptr;
6c00055a 1076 struct tipc_node *node;
23dd4cce 1077 struct tipc_msg *hdr = &sender->phdr;
9446b87a 1078 u32 dsz = len;
0e65967e 1079 u32 max_pkt, fragm_sz, rest;
b97bf3fd 1080 struct tipc_msg fragm_hdr;
0e65967e
AS
1081 struct sk_buff *buf, *buf_chain, *prev;
1082 u32 fragm_crs, fragm_rest, hsz, sect_rest;
40682432 1083 const unchar __user *sect_crs;
b97bf3fd
PL
1084 int curr_sect;
1085 u32 fragm_no;
126c0524 1086 int res = 0;
b97bf3fd
PL
1087
1088again:
1089 fragm_no = 1;
23dd4cce 1090 max_pkt = sender->max_pkt - INT_H_SIZE;
b97bf3fd 1091 /* leave room for tunnel header in case of link changeover */
c4307285 1092 fragm_sz = max_pkt - INT_H_SIZE;
b97bf3fd
PL
1093 /* leave room for fragmentation header in each fragment */
1094 rest = dsz;
1095 fragm_crs = 0;
1096 fragm_rest = 0;
1097 sect_rest = 0;
1fc54d8f 1098 sect_crs = NULL;
b97bf3fd
PL
1099 curr_sect = -1;
1100
617d3c7a 1101 /* Prepare reusable fragment header */
c68ca7b7 1102 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
75715217 1103 INT_H_SIZE, msg_destnode(hdr));
b97bf3fd
PL
1104 msg_set_size(&fragm_hdr, max_pkt);
1105 msg_set_fragm_no(&fragm_hdr, 1);
1106
617d3c7a 1107 /* Prepare header of first fragment */
31e3c3f6 1108 buf_chain = buf = tipc_buf_acquire(max_pkt);
b97bf3fd
PL
1109 if (!buf)
1110 return -ENOMEM;
1111 buf->next = NULL;
27d7ff46 1112 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
b97bf3fd 1113 hsz = msg_hdr_sz(hdr);
27d7ff46 1114 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
b97bf3fd 1115
617d3c7a 1116 /* Chop up message */
b97bf3fd
PL
1117 fragm_crs = INT_H_SIZE + hsz;
1118 fragm_rest = fragm_sz - hsz;
1119
1120 do { /* For all sections */
1121 u32 sz;
1122
1123 if (!sect_rest) {
1124 sect_rest = msg_sect[++curr_sect].iov_len;
40682432 1125 sect_crs = msg_sect[curr_sect].iov_base;
b97bf3fd
PL
1126 }
1127
1128 if (sect_rest < fragm_rest)
1129 sz = sect_rest;
1130 else
1131 sz = fragm_rest;
1132
f1733d75 1133 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
126c0524 1134 res = -EFAULT;
b97bf3fd 1135error:
f1733d75
YX
1136 for (; buf_chain; buf_chain = buf) {
1137 buf = buf_chain->next;
1138 kfree_skb(buf_chain);
b97bf3fd 1139 }
126c0524 1140 return res;
f1733d75 1141 }
b97bf3fd
PL
1142 sect_crs += sz;
1143 sect_rest -= sz;
1144 fragm_crs += sz;
1145 fragm_rest -= sz;
1146 rest -= sz;
1147
1148 if (!fragm_rest && rest) {
1149
1150 /* Initiate new fragment: */
1151 if (rest <= fragm_sz) {
1152 fragm_sz = rest;
0e65967e 1153 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
b97bf3fd
PL
1154 } else {
1155 msg_set_type(&fragm_hdr, FRAGMENT);
1156 }
1157 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1158 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1159 prev = buf;
31e3c3f6 1160 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
126c0524
YX
1161 if (!buf) {
1162 res = -ENOMEM;
b97bf3fd 1163 goto error;
126c0524 1164 }
b97bf3fd 1165
c4307285 1166 buf->next = NULL;
b97bf3fd 1167 prev->next = buf;
27d7ff46 1168 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
b97bf3fd
PL
1169 fragm_crs = INT_H_SIZE;
1170 fragm_rest = fragm_sz;
b97bf3fd 1171 }
0e65967e 1172 } while (rest > 0);
b97bf3fd 1173
c4307285 1174 /*
b97bf3fd
PL
1175 * Now we have a buffer chain. Select a link and check
1176 * that packet size is still OK
1177 */
51a8e4de 1178 node = tipc_node_find(destaddr);
b97bf3fd 1179 if (likely(node)) {
4323add6 1180 tipc_node_lock(node);
23dd4cce 1181 l_ptr = node->active_links[sender->ref & 1];
b97bf3fd 1182 if (!l_ptr) {
4323add6 1183 tipc_node_unlock(node);
b97bf3fd
PL
1184 goto reject;
1185 }
15e979da 1186 if (l_ptr->max_pkt < max_pkt) {
23dd4cce 1187 sender->max_pkt = l_ptr->max_pkt;
4323add6 1188 tipc_node_unlock(node);
b97bf3fd
PL
1189 for (; buf_chain; buf_chain = buf) {
1190 buf = buf_chain->next;
5f6d9123 1191 kfree_skb(buf_chain);
b97bf3fd
PL
1192 }
1193 goto again;
1194 }
1195 } else {
1196reject:
1197 for (; buf_chain; buf_chain = buf) {
1198 buf = buf_chain->next;
5f6d9123 1199 kfree_skb(buf_chain);
b97bf3fd 1200 }
9446b87a
YX
1201 return tipc_port_reject_sections(sender, hdr, msg_sect,
1202 len, TIPC_ERR_NO_NODE);
b97bf3fd
PL
1203 }
1204
dc63d91e 1205 /* Append chain of fragments to send queue & send them */
e0f08596 1206 l_ptr->long_msg_seq_no++;
dc63d91e
AS
1207 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
1208 l_ptr->stats.sent_fragments += fragm_no;
b97bf3fd 1209 l_ptr->stats.sent_fragmented++;
4323add6
PL
1210 tipc_link_push_queue(l_ptr);
1211 tipc_node_unlock(node);
b97bf3fd
PL
1212 return dsz;
1213}
1214
c4307285 1215/*
4323add6 1216 * tipc_link_push_packet: Push one unsent packet to the media
b97bf3fd 1217 */
a18c4bc3 1218u32 tipc_link_push_packet(struct tipc_link *l_ptr)
b97bf3fd
PL
1219{
1220 struct sk_buff *buf = l_ptr->first_out;
1221 u32 r_q_size = l_ptr->retransm_queue_size;
1222 u32 r_q_head = l_ptr->retransm_queue_head;
1223
1224 /* Step to position where retransmission failed, if any, */
1225 /* consider that buffers may have been released in meantime */
b97bf3fd 1226 if (r_q_size && buf) {
c4307285 1227 u32 last = lesser(mod(r_q_head + r_q_size),
b97bf3fd 1228 link_last_sent(l_ptr));
f905730c 1229 u32 first = buf_seqno(buf);
b97bf3fd
PL
1230
1231 while (buf && less(first, r_q_head)) {
1232 first = mod(first + 1);
1233 buf = buf->next;
1234 }
1235 l_ptr->retransm_queue_head = r_q_head = first;
1236 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1237 }
1238
1239 /* Continue retransmission now, if there is anything: */
ca509101 1240 if (r_q_size && buf) {
b97bf3fd 1241 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
c4307285 1242 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
3c294cb3
YX
1243 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1244 l_ptr->retransm_queue_head = mod(++r_q_head);
1245 l_ptr->retransm_queue_size = --r_q_size;
1246 l_ptr->stats.retransmitted++;
1247 return 0;
b97bf3fd
PL
1248 }
1249
1250 /* Send deferred protocol message, if any: */
b97bf3fd
PL
1251 buf = l_ptr->proto_msg_queue;
1252 if (buf) {
1253 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
0e65967e 1254 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
3c294cb3
YX
1255 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1256 l_ptr->unacked_window = 0;
1257 kfree_skb(buf);
1258 l_ptr->proto_msg_queue = NULL;
1259 return 0;
b97bf3fd
PL
1260 }
1261
1262 /* Send one deferred data message, if send window not full: */
b97bf3fd
PL
1263 buf = l_ptr->next_out;
1264 if (buf) {
1265 struct tipc_msg *msg = buf_msg(buf);
1266 u32 next = msg_seqno(msg);
f905730c 1267 u32 first = buf_seqno(l_ptr->first_out);
b97bf3fd
PL
1268
1269 if (mod(next - first) < l_ptr->queue_limit[0]) {
1270 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 1271 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
3c294cb3
YX
1272 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1273 if (msg_user(msg) == MSG_BUNDLER)
1274 msg_set_type(msg, CLOSED_MSG);
1275 l_ptr->next_out = buf->next;
1276 return 0;
b97bf3fd
PL
1277 }
1278 }
3c294cb3 1279 return 1;
b97bf3fd
PL
1280}
1281
1282/*
1283 * push_queue(): push out the unsent messages of a link where
1284 * congestion has abated. Node is locked
1285 */
a18c4bc3 1286void tipc_link_push_queue(struct tipc_link *l_ptr)
b97bf3fd
PL
1287{
1288 u32 res;
1289
3c294cb3 1290 if (tipc_bearer_blocked(l_ptr->b_ptr))
b97bf3fd
PL
1291 return;
1292
1293 do {
4323add6 1294 res = tipc_link_push_packet(l_ptr);
0e35fd5e 1295 } while (!res);
b97bf3fd
PL
1296}
1297
d356eeba
AS
1298static void link_reset_all(unsigned long addr)
1299{
6c00055a 1300 struct tipc_node *n_ptr;
d356eeba
AS
1301 char addr_string[16];
1302 u32 i;
1303
1304 read_lock_bh(&tipc_net_lock);
1305 n_ptr = tipc_node_find((u32)addr);
1306 if (!n_ptr) {
1307 read_unlock_bh(&tipc_net_lock);
1308 return; /* node no longer exists */
1309 }
1310
1311 tipc_node_lock(n_ptr);
1312
2cf8aa19
EH
1313 pr_warn("Resetting all links to %s\n",
1314 tipc_addr_string_fill(addr_string, n_ptr->addr));
d356eeba
AS
1315
1316 for (i = 0; i < MAX_BEARERS; i++) {
1317 if (n_ptr->links[i]) {
8d64a5ba 1318 link_print(n_ptr->links[i], "Resetting link\n");
d356eeba
AS
1319 tipc_link_reset(n_ptr->links[i]);
1320 }
1321 }
1322
1323 tipc_node_unlock(n_ptr);
1324 read_unlock_bh(&tipc_net_lock);
1325}
1326
a18c4bc3 1327static void link_retransmit_failure(struct tipc_link *l_ptr,
ae8509c4 1328 struct sk_buff *buf)
d356eeba
AS
1329{
1330 struct tipc_msg *msg = buf_msg(buf);
1331
2cf8aa19 1332 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
d356eeba
AS
1333
1334 if (l_ptr->addr) {
d356eeba 1335 /* Handle failure on standard link */
8d64a5ba 1336 link_print(l_ptr, "Resetting link\n");
d356eeba
AS
1337 tipc_link_reset(l_ptr);
1338
1339 } else {
d356eeba 1340 /* Handle failure on broadcast link */
6c00055a 1341 struct tipc_node *n_ptr;
d356eeba
AS
1342 char addr_string[16];
1343
2cf8aa19
EH
1344 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1345 pr_cont("Outstanding acks: %lu\n",
1346 (unsigned long) TIPC_SKB_CB(buf)->handle);
617dbeaa 1347
01d83edd 1348 n_ptr = tipc_bclink_retransmit_to();
d356eeba
AS
1349 tipc_node_lock(n_ptr);
1350
c68ca7b7 1351 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19 1352 pr_info("Broadcast link info for %s\n", addr_string);
389dd9bc
YX
1353 pr_info("Reception permitted: %d, Acked: %u\n",
1354 n_ptr->bclink.recv_permitted,
2cf8aa19
EH
1355 n_ptr->bclink.acked);
1356 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1357 n_ptr->bclink.last_in,
1358 n_ptr->bclink.oos_state,
1359 n_ptr->bclink.last_sent);
d356eeba
AS
1360
1361 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1362
1363 tipc_node_unlock(n_ptr);
1364
1365 l_ptr->stale_count = 0;
1366 }
1367}
1368
a18c4bc3 1369void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
4323add6 1370 u32 retransmits)
b97bf3fd
PL
1371{
1372 struct tipc_msg *msg;
1373
d356eeba
AS
1374 if (!buf)
1375 return;
1376
1377 msg = buf_msg(buf);
c4307285 1378
3c294cb3 1379 if (tipc_bearer_blocked(l_ptr->b_ptr)) {
ca509101 1380 if (l_ptr->retransm_queue_size == 0) {
d356eeba
AS
1381 l_ptr->retransm_queue_head = msg_seqno(msg);
1382 l_ptr->retransm_queue_size = retransmits;
d356eeba 1383 } else {
2cf8aa19
EH
1384 pr_err("Unexpected retransmit on link %s (qsize=%d)\n",
1385 l_ptr->name, l_ptr->retransm_queue_size);
d356eeba 1386 }
ca509101 1387 return;
d356eeba 1388 } else {
3c294cb3 1389 /* Detect repeated retransmit failures on unblocked bearer */
d356eeba
AS
1390 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1391 if (++l_ptr->stale_count > 100) {
1392 link_retransmit_failure(l_ptr, buf);
1393 return;
1394 }
1395 } else {
1396 l_ptr->last_retransmitted = msg_seqno(msg);
1397 l_ptr->stale_count = 1;
1398 }
b97bf3fd 1399 }
d356eeba 1400
ca509101 1401 while (retransmits && (buf != l_ptr->next_out) && buf) {
b97bf3fd
PL
1402 msg = buf_msg(buf);
1403 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 1404 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
3c294cb3
YX
1405 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1406 buf = buf->next;
1407 retransmits--;
1408 l_ptr->stats.retransmitted++;
b97bf3fd 1409 }
d356eeba 1410
b97bf3fd
PL
1411 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1412}
1413
c4307285 1414/**
b97bf3fd
PL
1415 * link_insert_deferred_queue - insert deferred messages back into receive chain
1416 */
a18c4bc3 1417static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
b97bf3fd
PL
1418 struct sk_buff *buf)
1419{
1420 u32 seq_no;
1421
1422 if (l_ptr->oldest_deferred_in == NULL)
1423 return buf;
1424
f905730c 1425 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
b97bf3fd
PL
1426 if (seq_no == mod(l_ptr->next_in_no)) {
1427 l_ptr->newest_deferred_in->next = buf;
1428 buf = l_ptr->oldest_deferred_in;
1429 l_ptr->oldest_deferred_in = NULL;
1430 l_ptr->deferred_inqueue_sz = 0;
1431 }
1432 return buf;
1433}
1434
85035568
AS
1435/**
1436 * link_recv_buf_validate - validate basic format of received message
1437 *
1438 * This routine ensures a TIPC message has an acceptable header, and at least
1439 * as much data as the header indicates it should. The routine also ensures
1440 * that the entire message header is stored in the main fragment of the message
1441 * buffer, to simplify future access to message header fields.
1442 *
1443 * Note: Having extra info present in the message header or data areas is OK.
1444 * TIPC will ignore the excess, under the assumption that it is optional info
1445 * introduced by a later release of the protocol.
1446 */
85035568
AS
1447static int link_recv_buf_validate(struct sk_buff *buf)
1448{
1449 static u32 min_data_hdr_size[8] = {
741d9eb7 1450 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
85035568
AS
1451 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1452 };
1453
1454 struct tipc_msg *msg;
1455 u32 tipc_hdr[2];
1456 u32 size;
1457 u32 hdr_size;
1458 u32 min_hdr_size;
1459
1460 if (unlikely(buf->len < MIN_H_SIZE))
1461 return 0;
1462
1463 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1464 if (msg == NULL)
1465 return 0;
1466
1467 if (unlikely(msg_version(msg) != TIPC_VERSION))
1468 return 0;
1469
1470 size = msg_size(msg);
1471 hdr_size = msg_hdr_sz(msg);
1472 min_hdr_size = msg_isdata(msg) ?
1473 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1474
1475 if (unlikely((hdr_size < min_hdr_size) ||
1476 (size < hdr_size) ||
1477 (buf->len < size) ||
1478 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1479 return 0;
1480
1481 return pskb_may_pull(buf, hdr_size);
1482}
1483
b02b69c8
AS
1484/**
1485 * tipc_recv_msg - process TIPC messages arriving from off-node
1486 * @head: pointer to message buffer chain
1487 * @tb_ptr: pointer to bearer message arrived on
1488 *
1489 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1490 * structure (i.e. cannot be NULL), but bearer can be inactive.
1491 */
2d627b92 1492void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
b97bf3fd 1493{
4323add6 1494 read_lock_bh(&tipc_net_lock);
b97bf3fd 1495 while (head) {
6c00055a 1496 struct tipc_node *n_ptr;
a18c4bc3 1497 struct tipc_link *l_ptr;
b97bf3fd
PL
1498 struct sk_buff *crs;
1499 struct sk_buff *buf = head;
85035568
AS
1500 struct tipc_msg *msg;
1501 u32 seq_no;
1502 u32 ackd;
b97bf3fd
PL
1503 u32 released = 0;
1504 int type;
1505
b97bf3fd 1506 head = head->next;
85035568 1507
b02b69c8 1508 /* Ensure bearer is still enabled */
b02b69c8 1509 if (unlikely(!b_ptr->active))
3af390e2 1510 goto discard;
b02b69c8 1511
85035568 1512 /* Ensure message is well-formed */
85035568 1513 if (unlikely(!link_recv_buf_validate(buf)))
3af390e2 1514 goto discard;
b97bf3fd 1515
fe13dda2 1516 /* Ensure message data is a single contiguous unit */
5f6d9123 1517 if (unlikely(skb_linearize(buf)))
3af390e2 1518 goto discard;
fe13dda2 1519
85035568 1520 /* Handle arrival of a non-unicast link message */
85035568
AS
1521 msg = buf_msg(buf);
1522
b97bf3fd 1523 if (unlikely(msg_non_seq(msg))) {
1265a021
AS
1524 if (msg_user(msg) == LINK_CONFIG)
1525 tipc_disc_recv_msg(buf, b_ptr);
1526 else
1527 tipc_bclink_recv_pkt(buf);
b97bf3fd
PL
1528 continue;
1529 }
c4307285 1530
ed33a9c4 1531 /* Discard unicast link messages destined for another node */
26008247
AS
1532 if (unlikely(!msg_short(msg) &&
1533 (msg_destnode(msg) != tipc_own_addr)))
3af390e2 1534 goto discard;
c4307285 1535
5a68d5ee 1536 /* Locate neighboring node that sent message */
4323add6 1537 n_ptr = tipc_node_find(msg_prevnode(msg));
b97bf3fd 1538 if (unlikely(!n_ptr))
3af390e2 1539 goto discard;
4323add6 1540 tipc_node_lock(n_ptr);
85035568 1541
b4b56102 1542 /* Locate unicast link endpoint that should handle message */
b4b56102 1543 l_ptr = n_ptr->links[b_ptr->identity];
3af390e2
YX
1544 if (unlikely(!l_ptr))
1545 goto unlock_discard;
5a68d5ee 1546
b4b56102 1547 /* Verify that communication with node is currently allowed */
b4b56102
AS
1548 if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
1549 msg_user(msg) == LINK_PROTOCOL &&
1550 (msg_type(msg) == RESET_MSG ||
1551 msg_type(msg) == ACTIVATE_MSG) &&
1552 !msg_redundant_link(msg))
1553 n_ptr->block_setup &= ~WAIT_PEER_DOWN;
1554
3af390e2
YX
1555 if (n_ptr->block_setup)
1556 goto unlock_discard;
85035568
AS
1557
1558 /* Validate message sequence number info */
85035568
AS
1559 seq_no = msg_seqno(msg);
1560 ackd = msg_ack(msg);
1561
1562 /* Release acked messages */
389dd9bc 1563 if (n_ptr->bclink.recv_permitted)
36559591 1564 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
b97bf3fd
PL
1565
1566 crs = l_ptr->first_out;
c4307285 1567 while ((crs != l_ptr->next_out) &&
f905730c 1568 less_eq(buf_seqno(crs), ackd)) {
b97bf3fd
PL
1569 struct sk_buff *next = crs->next;
1570
5f6d9123 1571 kfree_skb(crs);
b97bf3fd
PL
1572 crs = next;
1573 released++;
1574 }
1575 if (released) {
1576 l_ptr->first_out = crs;
1577 l_ptr->out_queue_size -= released;
1578 }
85035568
AS
1579
1580 /* Try sending any messages link endpoint has pending */
b97bf3fd 1581 if (unlikely(l_ptr->next_out))
4323add6 1582 tipc_link_push_queue(l_ptr);
b97bf3fd 1583 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
4323add6 1584 tipc_link_wakeup_ports(l_ptr, 0);
b97bf3fd
PL
1585 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1586 l_ptr->stats.sent_acks++;
4323add6 1587 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
1588 }
1589
85035568 1590 /* Now (finally!) process the incoming message */
b97bf3fd 1591protocol_check:
3af390e2
YX
1592 if (unlikely(!link_working_working(l_ptr))) {
1593 if (msg_user(msg) == LINK_PROTOCOL) {
1594 link_recv_proto_msg(l_ptr, buf);
1595 head = link_insert_deferred_queue(l_ptr, head);
4323add6 1596 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1597 continue;
1598 }
3af390e2
YX
1599
1600 /* Traffic message. Conditionally activate link */
1601 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1602
1603 if (link_working_working(l_ptr)) {
1604 /* Re-insert buffer in front of queue */
1605 buf->next = head;
1606 head = buf;
1607 tipc_node_unlock(n_ptr);
1608 continue;
1609 }
1610 goto unlock_discard;
1611 }
1612
1613 /* Link is now in state WORKING_WORKING */
1614 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
b97bf3fd
PL
1615 link_handle_out_of_seq_msg(l_ptr, buf);
1616 head = link_insert_deferred_queue(l_ptr, head);
4323add6 1617 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1618 continue;
1619 }
3af390e2
YX
1620 l_ptr->next_in_no++;
1621 if (unlikely(l_ptr->oldest_deferred_in))
b97bf3fd 1622 head = link_insert_deferred_queue(l_ptr, head);
3af390e2
YX
1623deliver:
1624 if (likely(msg_isdata(msg))) {
4323add6 1625 tipc_node_unlock(n_ptr);
3af390e2 1626 tipc_port_recv_msg(buf);
b97bf3fd
PL
1627 continue;
1628 }
3af390e2
YX
1629 switch (msg_user(msg)) {
1630 int ret;
1631 case MSG_BUNDLER:
1632 l_ptr->stats.recv_bundles++;
1633 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1634 tipc_node_unlock(n_ptr);
1635 tipc_link_recv_bundle(buf);
1636 continue;
1637 case NAME_DISTRIBUTOR:
1638 n_ptr->bclink.recv_permitted = true;
1639 tipc_node_unlock(n_ptr);
1640 tipc_named_recv(buf);
1641 continue;
1642 case BCAST_PROTOCOL:
1643 tipc_link_recv_sync(n_ptr, buf);
4323add6 1644 tipc_node_unlock(n_ptr);
b97bf3fd 1645 continue;
3af390e2
YX
1646 case CONN_MANAGER:
1647 tipc_node_unlock(n_ptr);
1648 tipc_port_recv_proto_msg(buf);
1649 continue;
1650 case MSG_FRAGMENTER:
1651 l_ptr->stats.recv_fragments++;
1652 ret = tipc_link_recv_fragment(&l_ptr->defragm_buf,
1653 &buf, &msg);
1654 if (ret == 1) {
1655 l_ptr->stats.recv_fragmented++;
1656 goto deliver;
1657 }
1658 if (ret == -1)
1659 l_ptr->next_in_no--;
1660 break;
1661 case CHANGEOVER_PROTOCOL:
1662 type = msg_type(msg);
1663 if (link_recv_changeover_msg(&l_ptr, &buf)) {
1664 msg = buf_msg(buf);
1665 seq_no = msg_seqno(msg);
1666 if (type == ORIGINAL_MSG)
1667 goto deliver;
1668 goto protocol_check;
1669 }
1670 break;
1671 default:
1672 kfree_skb(buf);
1673 buf = NULL;
1674 break;
b97bf3fd 1675 }
4323add6 1676 tipc_node_unlock(n_ptr);
3af390e2
YX
1677 tipc_net_route_msg(buf);
1678 continue;
1679unlock_discard:
1680
1681 tipc_node_unlock(n_ptr);
1682discard:
5f6d9123 1683 kfree_skb(buf);
b97bf3fd 1684 }
4323add6 1685 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
1686}
1687
2c53040f 1688/**
8809b255
AS
1689 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1690 *
1691 * Returns increase in queue length (i.e. 0 or 1)
b97bf3fd 1692 */
8809b255 1693u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
4323add6 1694 struct sk_buff *buf)
b97bf3fd 1695{
8809b255
AS
1696 struct sk_buff *queue_buf;
1697 struct sk_buff **prev;
f905730c 1698 u32 seq_no = buf_seqno(buf);
b97bf3fd
PL
1699
1700 buf->next = NULL;
1701
1702 /* Empty queue ? */
1703 if (*head == NULL) {
1704 *head = *tail = buf;
1705 return 1;
1706 }
1707
1708 /* Last ? */
f905730c 1709 if (less(buf_seqno(*tail), seq_no)) {
b97bf3fd
PL
1710 (*tail)->next = buf;
1711 *tail = buf;
1712 return 1;
1713 }
1714
8809b255
AS
1715 /* Locate insertion point in queue, then insert; discard if duplicate */
1716 prev = head;
1717 queue_buf = *head;
1718 for (;;) {
1719 u32 curr_seqno = buf_seqno(queue_buf);
b97bf3fd 1720
8809b255 1721 if (seq_no == curr_seqno) {
5f6d9123 1722 kfree_skb(buf);
8809b255 1723 return 0;
b97bf3fd 1724 }
8809b255
AS
1725
1726 if (less(seq_no, curr_seqno))
b97bf3fd 1727 break;
b97bf3fd 1728
8809b255
AS
1729 prev = &queue_buf->next;
1730 queue_buf = queue_buf->next;
1731 }
b97bf3fd 1732
8809b255
AS
1733 buf->next = queue_buf;
1734 *prev = buf;
1735 return 1;
b97bf3fd
PL
1736}
1737
8809b255 1738/*
b97bf3fd
PL
1739 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1740 */
a18c4bc3 1741static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
b97bf3fd
PL
1742 struct sk_buff *buf)
1743{
f905730c 1744 u32 seq_no = buf_seqno(buf);
b97bf3fd
PL
1745
1746 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1747 link_recv_proto_msg(l_ptr, buf);
1748 return;
1749 }
1750
b97bf3fd 1751 /* Record OOS packet arrival (force mismatch on next timeout) */
b97bf3fd
PL
1752 l_ptr->checkpoint--;
1753
c4307285 1754 /*
b97bf3fd
PL
1755 * Discard packet if a duplicate; otherwise add it to deferred queue
1756 * and notify peer of gap as per protocol specification
1757 */
b97bf3fd
PL
1758 if (less(seq_no, mod(l_ptr->next_in_no))) {
1759 l_ptr->stats.duplicates++;
5f6d9123 1760 kfree_skb(buf);
b97bf3fd
PL
1761 return;
1762 }
1763
4323add6
PL
1764 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1765 &l_ptr->newest_deferred_in, buf)) {
b97bf3fd
PL
1766 l_ptr->deferred_inqueue_sz++;
1767 l_ptr->stats.deferred_recv++;
1768 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
4323add6 1769 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
1770 } else
1771 l_ptr->stats.duplicates++;
1772}
1773
1774/*
1775 * Send protocol message to the other endpoint.
1776 */
a18c4bc3 1777void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ,
ae8509c4
PG
1778 int probe_msg, u32 gap, u32 tolerance,
1779 u32 priority, u32 ack_mtu)
b97bf3fd 1780{
1fc54d8f 1781 struct sk_buff *buf = NULL;
b97bf3fd 1782 struct tipc_msg *msg = l_ptr->pmsg;
c4307285 1783 u32 msg_size = sizeof(l_ptr->proto_msg);
75f0aa49 1784 int r_flag;
b97bf3fd 1785
92d2c905 1786 /* Discard any previous message that was deferred due to congestion */
92d2c905 1787 if (l_ptr->proto_msg_queue) {
5f6d9123 1788 kfree_skb(l_ptr->proto_msg_queue);
92d2c905
AS
1789 l_ptr->proto_msg_queue = NULL;
1790 }
1791
b97bf3fd
PL
1792 if (link_blocked(l_ptr))
1793 return;
b4b56102
AS
1794
1795 /* Abort non-RESET send if communication with node is prohibited */
b4b56102
AS
1796 if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
1797 return;
1798
92d2c905 1799 /* Create protocol message with "out-of-sequence" sequence number */
b97bf3fd
PL
1800 msg_set_type(msg, msg_typ);
1801 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
7a54d4a9 1802 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
4323add6 1803 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
b97bf3fd
PL
1804
1805 if (msg_typ == STATE_MSG) {
1806 u32 next_sent = mod(l_ptr->next_out_no);
1807
4323add6 1808 if (!tipc_link_is_up(l_ptr))
b97bf3fd
PL
1809 return;
1810 if (l_ptr->next_out)
f905730c 1811 next_sent = buf_seqno(l_ptr->next_out);
b97bf3fd
PL
1812 msg_set_next_sent(msg, next_sent);
1813 if (l_ptr->oldest_deferred_in) {
f905730c 1814 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
b97bf3fd
PL
1815 gap = mod(rec - mod(l_ptr->next_in_no));
1816 }
1817 msg_set_seq_gap(msg, gap);
1818 if (gap)
1819 l_ptr->stats.sent_nacks++;
1820 msg_set_link_tolerance(msg, tolerance);
1821 msg_set_linkprio(msg, priority);
1822 msg_set_max_pkt(msg, ack_mtu);
1823 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1824 msg_set_probe(msg, probe_msg != 0);
c4307285 1825 if (probe_msg) {
b97bf3fd
PL
1826 u32 mtu = l_ptr->max_pkt;
1827
c4307285 1828 if ((mtu < l_ptr->max_pkt_target) &&
b97bf3fd
PL
1829 link_working_working(l_ptr) &&
1830 l_ptr->fsm_msg_cnt) {
1831 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285
YH
1832 if (l_ptr->max_pkt_probes == 10) {
1833 l_ptr->max_pkt_target = (msg_size - 4);
1834 l_ptr->max_pkt_probes = 0;
b97bf3fd 1835 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285 1836 }
b97bf3fd 1837 l_ptr->max_pkt_probes++;
c4307285 1838 }
b97bf3fd
PL
1839
1840 l_ptr->stats.sent_probes++;
c4307285 1841 }
b97bf3fd
PL
1842 l_ptr->stats.sent_states++;
1843 } else { /* RESET_MSG or ACTIVATE_MSG */
1844 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1845 msg_set_seq_gap(msg, 0);
1846 msg_set_next_sent(msg, 1);
f23d9bf2 1847 msg_set_probe(msg, 0);
b97bf3fd
PL
1848 msg_set_link_tolerance(msg, l_ptr->tolerance);
1849 msg_set_linkprio(msg, l_ptr->priority);
1850 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1851 }
1852
75f0aa49
AS
1853 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1854 msg_set_redundant_link(msg, r_flag);
b97bf3fd 1855 msg_set_linkprio(msg, l_ptr->priority);
92d2c905 1856 msg_set_size(msg, msg_size);
b97bf3fd
PL
1857
1858 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1859
31e3c3f6 1860 buf = tipc_buf_acquire(msg_size);
b97bf3fd
PL
1861 if (!buf)
1862 return;
1863
27d7ff46 1864 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
796c75d0 1865 buf->priority = TC_PRIO_CONTROL;
b97bf3fd 1866
3c294cb3
YX
1867 /* Defer message if bearer is already blocked */
1868 if (tipc_bearer_blocked(l_ptr->b_ptr)) {
92d2c905 1869 l_ptr->proto_msg_queue = buf;
b97bf3fd
PL
1870 return;
1871 }
1872
3c294cb3 1873 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
92d2c905 1874 l_ptr->unacked_window = 0;
5f6d9123 1875 kfree_skb(buf);
b97bf3fd
PL
1876}
1877
1878/*
1879 * Receive protocol message :
c4307285
YH
1880 * Note that network plane id propagates through the network, and may
1881 * change at any time. The node with lowest address rules
b97bf3fd 1882 */
a18c4bc3 1883static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf)
b97bf3fd
PL
1884{
1885 u32 rec_gap = 0;
1886 u32 max_pkt_info;
c4307285 1887 u32 max_pkt_ack;
b97bf3fd
PL
1888 u32 msg_tol;
1889 struct tipc_msg *msg = buf_msg(buf);
1890
b97bf3fd
PL
1891 if (link_blocked(l_ptr))
1892 goto exit;
1893
1894 /* record unnumbered packet arrival (force mismatch on next timeout) */
b97bf3fd
PL
1895 l_ptr->checkpoint--;
1896
1897 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
1898 if (tipc_own_addr > msg_prevnode(msg))
1899 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
1900
1901 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
1902
1903 switch (msg_type(msg)) {
c4307285 1904
b97bf3fd 1905 case RESET_MSG:
a686e685
AS
1906 if (!link_working_unknown(l_ptr) &&
1907 (l_ptr->peer_session != INVALID_SESSION)) {
641c218d
AS
1908 if (less_eq(msg_session(msg), l_ptr->peer_session))
1909 break; /* duplicate or old reset: ignore */
b97bf3fd 1910 }
b4b56102
AS
1911
1912 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1913 link_working_unknown(l_ptr))) {
1914 /*
1915 * peer has lost contact -- don't allow peer's links
1916 * to reactivate before we recognize loss & clean up
1917 */
1918 l_ptr->owner->block_setup = WAIT_NODE_DOWN;
1919 }
1920
47361c87
AS
1921 link_state_event(l_ptr, RESET_MSG);
1922
b97bf3fd
PL
1923 /* fall thru' */
1924 case ACTIVATE_MSG:
1925 /* Update link settings according other endpoint's values */
b97bf3fd
PL
1926 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1927
2db9983a
AS
1928 msg_tol = msg_link_tolerance(msg);
1929 if (msg_tol > l_ptr->tolerance)
b97bf3fd
PL
1930 link_set_supervision_props(l_ptr, msg_tol);
1931
1932 if (msg_linkprio(msg) > l_ptr->priority)
1933 l_ptr->priority = msg_linkprio(msg);
1934
1935 max_pkt_info = msg_max_pkt(msg);
c4307285 1936 if (max_pkt_info) {
b97bf3fd
PL
1937 if (max_pkt_info < l_ptr->max_pkt_target)
1938 l_ptr->max_pkt_target = max_pkt_info;
1939 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1940 l_ptr->max_pkt = l_ptr->max_pkt_target;
1941 } else {
c4307285 1942 l_ptr->max_pkt = l_ptr->max_pkt_target;
b97bf3fd 1943 }
b97bf3fd 1944
4d75313c 1945 /* Synchronize broadcast link info, if not done previously */
7a54d4a9
AS
1946 if (!tipc_node_is_up(l_ptr->owner)) {
1947 l_ptr->owner->bclink.last_sent =
1948 l_ptr->owner->bclink.last_in =
1949 msg_last_bcast(msg);
1950 l_ptr->owner->bclink.oos_state = 0;
1951 }
4d75313c 1952
b97bf3fd
PL
1953 l_ptr->peer_session = msg_session(msg);
1954 l_ptr->peer_bearer_id = msg_bearer_id(msg);
47361c87
AS
1955
1956 if (msg_type(msg) == ACTIVATE_MSG)
1957 link_state_event(l_ptr, ACTIVATE_MSG);
b97bf3fd
PL
1958 break;
1959 case STATE_MSG:
1960
2db9983a
AS
1961 msg_tol = msg_link_tolerance(msg);
1962 if (msg_tol)
b97bf3fd 1963 link_set_supervision_props(l_ptr, msg_tol);
c4307285
YH
1964
1965 if (msg_linkprio(msg) &&
b97bf3fd 1966 (msg_linkprio(msg) != l_ptr->priority)) {
2cf8aa19
EH
1967 pr_warn("%s<%s>, priority change %u->%u\n",
1968 link_rst_msg, l_ptr->name, l_ptr->priority,
1969 msg_linkprio(msg));
b97bf3fd 1970 l_ptr->priority = msg_linkprio(msg);
4323add6 1971 tipc_link_reset(l_ptr); /* Enforce change to take effect */
b97bf3fd
PL
1972 break;
1973 }
1974 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1975 l_ptr->stats.recv_states++;
1976 if (link_reset_unknown(l_ptr))
1977 break;
1978
1979 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
c4307285 1980 rec_gap = mod(msg_next_sent(msg) -
b97bf3fd
PL
1981 mod(l_ptr->next_in_no));
1982 }
1983
1984 max_pkt_ack = msg_max_pkt(msg);
c4307285 1985 if (max_pkt_ack > l_ptr->max_pkt) {
c4307285
YH
1986 l_ptr->max_pkt = max_pkt_ack;
1987 l_ptr->max_pkt_probes = 0;
1988 }
b97bf3fd
PL
1989
1990 max_pkt_ack = 0;
c4307285 1991 if (msg_probe(msg)) {
b97bf3fd 1992 l_ptr->stats.recv_probes++;
a016892c 1993 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
c4307285 1994 max_pkt_ack = msg_size(msg);
c4307285 1995 }
b97bf3fd
PL
1996
1997 /* Protocol message before retransmits, reduce loss risk */
389dd9bc 1998 if (l_ptr->owner->bclink.recv_permitted)
7a54d4a9
AS
1999 tipc_bclink_update_link_state(l_ptr->owner,
2000 msg_last_bcast(msg));
b97bf3fd
PL
2001
2002 if (rec_gap || (msg_probe(msg))) {
4323add6
PL
2003 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2004 0, rec_gap, 0, 0, max_pkt_ack);
b97bf3fd
PL
2005 }
2006 if (msg_seq_gap(msg)) {
b97bf3fd 2007 l_ptr->stats.recv_nacks++;
4323add6
PL
2008 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2009 msg_seq_gap(msg));
b97bf3fd
PL
2010 }
2011 break;
b97bf3fd
PL
2012 }
2013exit:
5f6d9123 2014 kfree_skb(buf);
b97bf3fd
PL
2015}
2016
2017
2018/*
c4307285 2019 * tipc_link_tunnel(): Send one message via a link belonging to
b97bf3fd
PL
2020 * another bearer. Owner node is locked.
2021 */
a18c4bc3 2022static void tipc_link_tunnel(struct tipc_link *l_ptr,
ae8509c4 2023 struct tipc_msg *tunnel_hdr, struct tipc_msg *msg,
31e3c3f6 2024 u32 selector)
b97bf3fd 2025{
a18c4bc3 2026 struct tipc_link *tunnel;
b97bf3fd
PL
2027 struct sk_buff *buf;
2028 u32 length = msg_size(msg);
2029
2030 tunnel = l_ptr->owner->active_links[selector & 1];
5392d646 2031 if (!tipc_link_is_up(tunnel)) {
2cf8aa19 2032 pr_warn("%stunnel link no longer available\n", link_co_err);
b97bf3fd 2033 return;
5392d646 2034 }
b97bf3fd 2035 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
31e3c3f6 2036 buf = tipc_buf_acquire(length + INT_H_SIZE);
5392d646 2037 if (!buf) {
2cf8aa19 2038 pr_warn("%sunable to send tunnel msg\n", link_co_err);
b97bf3fd 2039 return;
5392d646 2040 }
27d7ff46
ACM
2041 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2042 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
4323add6 2043 tipc_link_send_buf(tunnel, buf);
b97bf3fd
PL
2044}
2045
2046
2047
2048/*
2049 * changeover(): Send whole message queue via the remaining link
2050 * Owner node is locked.
2051 */
a18c4bc3 2052void tipc_link_changeover(struct tipc_link *l_ptr)
b97bf3fd
PL
2053{
2054 u32 msgcount = l_ptr->out_queue_size;
2055 struct sk_buff *crs = l_ptr->first_out;
a18c4bc3 2056 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
b97bf3fd 2057 struct tipc_msg tunnel_hdr;
5392d646 2058 int split_bundles;
b97bf3fd
PL
2059
2060 if (!tunnel)
2061 return;
2062
5392d646 2063 if (!l_ptr->owner->permit_changeover) {
2cf8aa19 2064 pr_warn("%speer did not permit changeover\n", link_co_err);
b97bf3fd 2065 return;
5392d646 2066 }
b97bf3fd 2067
c68ca7b7 2068 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
75715217 2069 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd
PL
2070 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2071 msg_set_msgcnt(&tunnel_hdr, msgcount);
f131072c 2072
b97bf3fd
PL
2073 if (!l_ptr->first_out) {
2074 struct sk_buff *buf;
2075
31e3c3f6 2076 buf = tipc_buf_acquire(INT_H_SIZE);
b97bf3fd 2077 if (buf) {
27d7ff46 2078 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
b97bf3fd 2079 msg_set_size(&tunnel_hdr, INT_H_SIZE);
4323add6 2080 tipc_link_send_buf(tunnel, buf);
b97bf3fd 2081 } else {
2cf8aa19
EH
2082 pr_warn("%sunable to send changeover msg\n",
2083 link_co_err);
b97bf3fd
PL
2084 }
2085 return;
2086 }
f131072c 2087
c4307285 2088 split_bundles = (l_ptr->owner->active_links[0] !=
5392d646
AS
2089 l_ptr->owner->active_links[1]);
2090
b97bf3fd
PL
2091 while (crs) {
2092 struct tipc_msg *msg = buf_msg(crs);
2093
2094 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
b97bf3fd 2095 struct tipc_msg *m = msg_get_wrapped(msg);
0e65967e 2096 unchar *pos = (unchar *)m;
b97bf3fd 2097
d788d805 2098 msgcount = msg_msgcnt(msg);
b97bf3fd 2099 while (msgcount--) {
0e65967e 2100 msg_set_seqno(m, msg_seqno(msg));
4323add6
PL
2101 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2102 msg_link_selector(m));
b97bf3fd
PL
2103 pos += align(msg_size(m));
2104 m = (struct tipc_msg *)pos;
2105 }
2106 } else {
4323add6
PL
2107 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2108 msg_link_selector(msg));
b97bf3fd
PL
2109 }
2110 crs = crs->next;
2111 }
2112}
2113
a18c4bc3 2114void tipc_link_send_duplicate(struct tipc_link *l_ptr, struct tipc_link *tunnel)
b97bf3fd
PL
2115{
2116 struct sk_buff *iter;
2117 struct tipc_msg tunnel_hdr;
2118
c68ca7b7 2119 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
75715217 2120 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd
PL
2121 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2122 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2123 iter = l_ptr->first_out;
2124 while (iter) {
2125 struct sk_buff *outbuf;
2126 struct tipc_msg *msg = buf_msg(iter);
2127 u32 length = msg_size(msg);
2128
2129 if (msg_user(msg) == MSG_BUNDLER)
2130 msg_set_type(msg, CLOSED_MSG);
2131 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
c4307285 2132 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
b97bf3fd 2133 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
31e3c3f6 2134 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
b97bf3fd 2135 if (outbuf == NULL) {
2cf8aa19
EH
2136 pr_warn("%sunable to send duplicate msg\n",
2137 link_co_err);
b97bf3fd
PL
2138 return;
2139 }
27d7ff46
ACM
2140 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2141 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2142 length);
4323add6
PL
2143 tipc_link_send_buf(tunnel, outbuf);
2144 if (!tipc_link_is_up(l_ptr))
b97bf3fd
PL
2145 return;
2146 iter = iter->next;
2147 }
2148}
2149
b97bf3fd
PL
2150/**
2151 * buf_extract - extracts embedded TIPC message from another message
2152 * @skb: encapsulating message buffer
2153 * @from_pos: offset to extract from
2154 *
c4307285 2155 * Returns a new message buffer containing an embedded message. The
b97bf3fd
PL
2156 * encapsulating message itself is left unchanged.
2157 */
b97bf3fd
PL
2158static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2159{
2160 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2161 u32 size = msg_size(msg);
2162 struct sk_buff *eb;
2163
31e3c3f6 2164 eb = tipc_buf_acquire(size);
b97bf3fd 2165 if (eb)
27d7ff46 2166 skb_copy_to_linear_data(eb, msg, size);
b97bf3fd
PL
2167 return eb;
2168}
2169
c4307285 2170/*
b97bf3fd
PL
2171 * link_recv_changeover_msg(): Receive tunneled packet sent
2172 * via other link. Node is locked. Return extracted buffer.
2173 */
a18c4bc3 2174static int link_recv_changeover_msg(struct tipc_link **l_ptr,
b97bf3fd
PL
2175 struct sk_buff **buf)
2176{
2177 struct sk_buff *tunnel_buf = *buf;
a18c4bc3 2178 struct tipc_link *dest_link;
b97bf3fd
PL
2179 struct tipc_msg *msg;
2180 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2181 u32 msg_typ = msg_type(tunnel_msg);
2182 u32 msg_count = msg_msgcnt(tunnel_msg);
cb4b102f 2183 u32 bearer_id = msg_bearer_id(tunnel_msg);
b97bf3fd 2184
cb4b102f
DC
2185 if (bearer_id >= MAX_BEARERS)
2186 goto exit;
2187 dest_link = (*l_ptr)->owner->links[bearer_id];
b29f1428 2188 if (!dest_link)
b97bf3fd 2189 goto exit;
f131072c 2190 if (dest_link == *l_ptr) {
2cf8aa19
EH
2191 pr_err("Unexpected changeover message on link <%s>\n",
2192 (*l_ptr)->name);
f131072c
AS
2193 goto exit;
2194 }
b97bf3fd
PL
2195 *l_ptr = dest_link;
2196 msg = msg_get_wrapped(tunnel_msg);
2197
2198 if (msg_typ == DUPLICATE_MSG) {
b29f1428 2199 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
b97bf3fd 2200 goto exit;
0e65967e 2201 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
b97bf3fd 2202 if (*buf == NULL) {
2cf8aa19 2203 pr_warn("%sduplicate msg dropped\n", link_co_err);
b97bf3fd
PL
2204 goto exit;
2205 }
5f6d9123 2206 kfree_skb(tunnel_buf);
b97bf3fd
PL
2207 return 1;
2208 }
2209
2210 /* First original message ?: */
4323add6 2211 if (tipc_link_is_up(dest_link)) {
2cf8aa19
EH
2212 pr_info("%s<%s>, changeover initiated by peer\n", link_rst_msg,
2213 dest_link->name);
4323add6 2214 tipc_link_reset(dest_link);
b97bf3fd
PL
2215 dest_link->exp_msg_count = msg_count;
2216 if (!msg_count)
2217 goto exit;
2218 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
b97bf3fd
PL
2219 dest_link->exp_msg_count = msg_count;
2220 if (!msg_count)
2221 goto exit;
2222 }
2223
2224 /* Receive original message */
b97bf3fd 2225 if (dest_link->exp_msg_count == 0) {
2cf8aa19 2226 pr_warn("%sgot too many tunnelled messages\n", link_co_err);
b97bf3fd
PL
2227 goto exit;
2228 }
2229 dest_link->exp_msg_count--;
2230 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
b97bf3fd
PL
2231 goto exit;
2232 } else {
2233 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2234 if (*buf != NULL) {
5f6d9123 2235 kfree_skb(tunnel_buf);
b97bf3fd
PL
2236 return 1;
2237 } else {
2cf8aa19 2238 pr_warn("%soriginal msg dropped\n", link_co_err);
b97bf3fd
PL
2239 }
2240 }
2241exit:
1fc54d8f 2242 *buf = NULL;
5f6d9123 2243 kfree_skb(tunnel_buf);
b97bf3fd
PL
2244 return 0;
2245}
2246
2247/*
2248 * Bundler functionality:
2249 */
4323add6 2250void tipc_link_recv_bundle(struct sk_buff *buf)
b97bf3fd
PL
2251{
2252 u32 msgcount = msg_msgcnt(buf_msg(buf));
2253 u32 pos = INT_H_SIZE;
2254 struct sk_buff *obuf;
2255
b97bf3fd
PL
2256 while (msgcount--) {
2257 obuf = buf_extract(buf, pos);
2258 if (obuf == NULL) {
2cf8aa19 2259 pr_warn("Link unable to unbundle message(s)\n");
a10bd924 2260 break;
3ff50b79 2261 }
b97bf3fd 2262 pos += align(msg_size(buf_msg(obuf)));
4323add6 2263 tipc_net_route_msg(obuf);
b97bf3fd 2264 }
5f6d9123 2265 kfree_skb(buf);
b97bf3fd
PL
2266}
2267
2268/*
2269 * Fragmentation/defragmentation:
2270 */
2271
c4307285 2272/*
31e3c3f6 2273 * link_send_long_buf: Entry for buffers needing fragmentation.
c4307285 2274 * The buffer is complete, inclusive total message length.
b97bf3fd
PL
2275 * Returns user data length.
2276 */
a18c4bc3 2277static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
b97bf3fd 2278{
77561557
AS
2279 struct sk_buff *buf_chain = NULL;
2280 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
b97bf3fd
PL
2281 struct tipc_msg *inmsg = buf_msg(buf);
2282 struct tipc_msg fragm_hdr;
2283 u32 insize = msg_size(inmsg);
2284 u32 dsz = msg_data_sz(inmsg);
2285 unchar *crs = buf->data;
2286 u32 rest = insize;
15e979da 2287 u32 pack_sz = l_ptr->max_pkt;
b97bf3fd 2288 u32 fragm_sz = pack_sz - INT_H_SIZE;
77561557 2289 u32 fragm_no = 0;
9c396a7b 2290 u32 destaddr;
b97bf3fd
PL
2291
2292 if (msg_short(inmsg))
2293 destaddr = l_ptr->addr;
9c396a7b
AS
2294 else
2295 destaddr = msg_destnode(inmsg);
b97bf3fd 2296
b97bf3fd 2297 /* Prepare reusable fragment header: */
c68ca7b7 2298 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
75715217 2299 INT_H_SIZE, destaddr);
b97bf3fd
PL
2300
2301 /* Chop up message: */
b97bf3fd
PL
2302 while (rest > 0) {
2303 struct sk_buff *fragm;
2304
2305 if (rest <= fragm_sz) {
2306 fragm_sz = rest;
2307 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2308 }
31e3c3f6 2309 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
b97bf3fd 2310 if (fragm == NULL) {
5f6d9123 2311 kfree_skb(buf);
77561557
AS
2312 while (buf_chain) {
2313 buf = buf_chain;
2314 buf_chain = buf_chain->next;
5f6d9123 2315 kfree_skb(buf);
77561557
AS
2316 }
2317 return -ENOMEM;
b97bf3fd
PL
2318 }
2319 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
77561557
AS
2320 fragm_no++;
2321 msg_set_fragm_no(&fragm_hdr, fragm_no);
27d7ff46
ACM
2322 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2323 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2324 fragm_sz);
77561557
AS
2325 buf_chain_tail->next = fragm;
2326 buf_chain_tail = fragm;
b97bf3fd 2327
b97bf3fd
PL
2328 rest -= fragm_sz;
2329 crs += fragm_sz;
2330 msg_set_type(&fragm_hdr, FRAGMENT);
2331 }
5f6d9123 2332 kfree_skb(buf);
77561557
AS
2333
2334 /* Append chain of fragments to send queue & send them */
77561557
AS
2335 l_ptr->long_msg_seq_no++;
2336 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2337 l_ptr->stats.sent_fragments += fragm_no;
2338 l_ptr->stats.sent_fragmented++;
2339 tipc_link_push_queue(l_ptr);
2340
b97bf3fd
PL
2341 return dsz;
2342}
2343
c4307285
YH
2344/*
2345 * A pending message being re-assembled must store certain values
2346 * to handle subsequent fragments correctly. The following functions
b97bf3fd 2347 * help storing these values in unused, available fields in the
25985edc 2348 * pending message. This makes dynamic memory allocation unnecessary.
b97bf3fd 2349 */
05790c64 2350static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
b97bf3fd
PL
2351{
2352 msg_set_seqno(buf_msg(buf), seqno);
2353}
2354
05790c64 2355static u32 get_fragm_size(struct sk_buff *buf)
b97bf3fd
PL
2356{
2357 return msg_ack(buf_msg(buf));
2358}
2359
05790c64 2360static void set_fragm_size(struct sk_buff *buf, u32 sz)
b97bf3fd
PL
2361{
2362 msg_set_ack(buf_msg(buf), sz);
2363}
2364
05790c64 2365static u32 get_expected_frags(struct sk_buff *buf)
b97bf3fd
PL
2366{
2367 return msg_bcast_ack(buf_msg(buf));
2368}
2369
05790c64 2370static void set_expected_frags(struct sk_buff *buf, u32 exp)
b97bf3fd
PL
2371{
2372 msg_set_bcast_ack(buf_msg(buf), exp);
2373}
2374
c4307285
YH
2375/*
2376 * tipc_link_recv_fragment(): Called with node lock on. Returns
b97bf3fd
PL
2377 * the reassembled buffer if message is complete.
2378 */
c4307285 2379int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
4323add6 2380 struct tipc_msg **m)
b97bf3fd 2381{
1fc54d8f 2382 struct sk_buff *prev = NULL;
b97bf3fd
PL
2383 struct sk_buff *fbuf = *fb;
2384 struct tipc_msg *fragm = buf_msg(fbuf);
2385 struct sk_buff *pbuf = *pending;
2386 u32 long_msg_seq_no = msg_long_msgno(fragm);
2387
1fc54d8f 2388 *fb = NULL;
b97bf3fd
PL
2389
2390 /* Is there an incomplete message waiting for this fragment? */
f905730c 2391 while (pbuf && ((buf_seqno(pbuf) != long_msg_seq_no) ||
f64f9e71 2392 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
b97bf3fd
PL
2393 prev = pbuf;
2394 pbuf = pbuf->next;
2395 }
2396
2397 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2398 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2399 u32 msg_sz = msg_size(imsg);
2400 u32 fragm_sz = msg_data_sz(fragm);
6bf15191 2401 u32 exp_fragm_cnt;
741d9eb7 2402 u32 max = TIPC_MAX_USER_MSG_SIZE + NAMED_H_SIZE;
6bf15191 2403
b97bf3fd
PL
2404 if (msg_type(imsg) == TIPC_MCAST_MSG)
2405 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
6bf15191 2406 if (fragm_sz == 0 || msg_size(imsg) > max) {
5f6d9123 2407 kfree_skb(fbuf);
b97bf3fd
PL
2408 return 0;
2409 }
6bf15191 2410 exp_fragm_cnt = msg_sz / fragm_sz + !!(msg_sz % fragm_sz);
31e3c3f6 2411 pbuf = tipc_buf_acquire(msg_size(imsg));
b97bf3fd
PL
2412 if (pbuf != NULL) {
2413 pbuf->next = *pending;
2414 *pending = pbuf;
27d7ff46
ACM
2415 skb_copy_to_linear_data(pbuf, imsg,
2416 msg_data_sz(fragm));
b97bf3fd 2417 /* Prepare buffer for subsequent fragments. */
c4307285 2418 set_long_msg_seqno(pbuf, long_msg_seq_no);
0e65967e
AS
2419 set_fragm_size(pbuf, fragm_sz);
2420 set_expected_frags(pbuf, exp_fragm_cnt - 1);
b97bf3fd 2421 } else {
2cf8aa19 2422 pr_debug("Link unable to reassemble fragmented message\n");
5f6d9123 2423 kfree_skb(fbuf);
b76b27ca 2424 return -1;
b97bf3fd 2425 }
5f6d9123 2426 kfree_skb(fbuf);
b97bf3fd
PL
2427 return 0;
2428 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2429 u32 dsz = msg_data_sz(fragm);
2430 u32 fsz = get_fragm_size(pbuf);
2431 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2432 u32 exp_frags = get_expected_frags(pbuf) - 1;
27d7ff46
ACM
2433 skb_copy_to_linear_data_offset(pbuf, crs,
2434 msg_data(fragm), dsz);
5f6d9123 2435 kfree_skb(fbuf);
b97bf3fd
PL
2436
2437 /* Is message complete? */
b97bf3fd
PL
2438 if (exp_frags == 0) {
2439 if (prev)
2440 prev->next = pbuf->next;
2441 else
2442 *pending = pbuf->next;
2443 msg_reset_reroute_cnt(buf_msg(pbuf));
2444 *fb = pbuf;
2445 *m = buf_msg(pbuf);
2446 return 1;
2447 }
0e65967e 2448 set_expected_frags(pbuf, exp_frags);
b97bf3fd
PL
2449 return 0;
2450 }
5f6d9123 2451 kfree_skb(fbuf);
b97bf3fd
PL
2452 return 0;
2453}
2454
a18c4bc3 2455static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
b97bf3fd 2456{
5413b4c6
AS
2457 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2458 return;
2459
b97bf3fd
PL
2460 l_ptr->tolerance = tolerance;
2461 l_ptr->continuity_interval =
2462 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2463 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2464}
2465
a18c4bc3 2466void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
b97bf3fd
PL
2467{
2468 /* Data messages from this node, inclusive FIRST_FRAGM */
06d82c91
AS
2469 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2470 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2471 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2472 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
b97bf3fd 2473 /* Transiting data messages,inclusive FIRST_FRAGM */
06d82c91
AS
2474 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2475 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2476 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2477 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
b97bf3fd 2478 l_ptr->queue_limit[CONN_MANAGER] = 1200;
b97bf3fd
PL
2479 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2480 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2481 /* FRAGMENT and LAST_FRAGMENT packets */
2482 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2483}
2484
2485/**
2486 * link_find_link - locate link by name
2c53040f
BH
2487 * @name: ptr to link name string
2488 * @node: ptr to area to be filled with ptr to associated node
c4307285 2489 *
4323add6 2490 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
b97bf3fd 2491 * this also prevents link deletion.
c4307285 2492 *
b97bf3fd
PL
2493 * Returns pointer to link (or 0 if invalid link name).
2494 */
a18c4bc3
PG
2495static struct tipc_link *link_find_link(const char *name,
2496 struct tipc_node **node)
b97bf3fd 2497{
a18c4bc3 2498 struct tipc_link *l_ptr;
bbfbe47c
EH
2499 struct tipc_node *n_ptr;
2500 int i;
b97bf3fd 2501
bbfbe47c
EH
2502 list_for_each_entry(n_ptr, &tipc_node_list, list) {
2503 for (i = 0; i < MAX_BEARERS; i++) {
2504 l_ptr = n_ptr->links[i];
2505 if (l_ptr && !strcmp(l_ptr->name, name))
2506 goto found;
2507 }
2508 }
2509 l_ptr = NULL;
2510 n_ptr = NULL;
2511found:
2512 *node = n_ptr;
b97bf3fd
PL
2513 return l_ptr;
2514}
2515
5c216e1d
AS
2516/**
2517 * link_value_is_valid -- validate proposed link tolerance/priority/window
2518 *
2c53040f
BH
2519 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2520 * @new_value: the new value
5c216e1d
AS
2521 *
2522 * Returns 1 if value is within range, 0 if not.
2523 */
5c216e1d
AS
2524static int link_value_is_valid(u16 cmd, u32 new_value)
2525{
2526 switch (cmd) {
2527 case TIPC_CMD_SET_LINK_TOL:
2528 return (new_value >= TIPC_MIN_LINK_TOL) &&
2529 (new_value <= TIPC_MAX_LINK_TOL);
2530 case TIPC_CMD_SET_LINK_PRI:
2531 return (new_value <= TIPC_MAX_LINK_PRI);
2532 case TIPC_CMD_SET_LINK_WINDOW:
2533 return (new_value >= TIPC_MIN_LINK_WIN) &&
2534 (new_value <= TIPC_MAX_LINK_WIN);
2535 }
2536 return 0;
2537}
2538
5c216e1d
AS
2539/**
2540 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
2c53040f
BH
2541 * @name: ptr to link, bearer, or media name
2542 * @new_value: new value of link, bearer, or media setting
2543 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
5c216e1d
AS
2544 *
2545 * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
2546 *
2547 * Returns 0 if value updated and negative value on error.
2548 */
5c216e1d
AS
2549static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2550{
2551 struct tipc_node *node;
a18c4bc3 2552 struct tipc_link *l_ptr;
5c216e1d 2553 struct tipc_bearer *b_ptr;
358a0d1c 2554 struct tipc_media *m_ptr;
636c0371 2555 int res = 0;
5c216e1d
AS
2556
2557 l_ptr = link_find_link(name, &node);
2558 if (l_ptr) {
2559 /*
2560 * acquire node lock for tipc_link_send_proto_msg().
2561 * see "TIPC locking policy" in net.c.
2562 */
2563 tipc_node_lock(node);
2564 switch (cmd) {
2565 case TIPC_CMD_SET_LINK_TOL:
2566 link_set_supervision_props(l_ptr, new_value);
2567 tipc_link_send_proto_msg(l_ptr,
2568 STATE_MSG, 0, 0, new_value, 0, 0);
2569 break;
2570 case TIPC_CMD_SET_LINK_PRI:
2571 l_ptr->priority = new_value;
2572 tipc_link_send_proto_msg(l_ptr,
2573 STATE_MSG, 0, 0, 0, new_value, 0);
2574 break;
2575 case TIPC_CMD_SET_LINK_WINDOW:
2576 tipc_link_set_queue_limits(l_ptr, new_value);
2577 break;
636c0371
YX
2578 default:
2579 res = -EINVAL;
2580 break;
5c216e1d
AS
2581 }
2582 tipc_node_unlock(node);
636c0371 2583 return res;
5c216e1d
AS
2584 }
2585
2586 b_ptr = tipc_bearer_find(name);
2587 if (b_ptr) {
2588 switch (cmd) {
2589 case TIPC_CMD_SET_LINK_TOL:
2590 b_ptr->tolerance = new_value;
636c0371 2591 break;
5c216e1d
AS
2592 case TIPC_CMD_SET_LINK_PRI:
2593 b_ptr->priority = new_value;
636c0371 2594 break;
5c216e1d
AS
2595 case TIPC_CMD_SET_LINK_WINDOW:
2596 b_ptr->window = new_value;
636c0371
YX
2597 break;
2598 default:
2599 res = -EINVAL;
2600 break;
5c216e1d 2601 }
636c0371 2602 return res;
5c216e1d
AS
2603 }
2604
2605 m_ptr = tipc_media_find(name);
2606 if (!m_ptr)
2607 return -ENODEV;
2608 switch (cmd) {
2609 case TIPC_CMD_SET_LINK_TOL:
2610 m_ptr->tolerance = new_value;
636c0371 2611 break;
5c216e1d
AS
2612 case TIPC_CMD_SET_LINK_PRI:
2613 m_ptr->priority = new_value;
636c0371 2614 break;
5c216e1d
AS
2615 case TIPC_CMD_SET_LINK_WINDOW:
2616 m_ptr->window = new_value;
636c0371
YX
2617 break;
2618 default:
2619 res = -EINVAL;
2620 break;
5c216e1d 2621 }
636c0371 2622 return res;
5c216e1d
AS
2623}
2624
c4307285 2625struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
4323add6 2626 u16 cmd)
b97bf3fd
PL
2627{
2628 struct tipc_link_config *args;
c4307285 2629 u32 new_value;
c4307285 2630 int res;
b97bf3fd
PL
2631
2632 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
4323add6 2633 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd
PL
2634
2635 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2636 new_value = ntohl(args->value);
2637
5c216e1d
AS
2638 if (!link_value_is_valid(cmd, new_value))
2639 return tipc_cfg_reply_error_string(
2640 "cannot change, value invalid");
2641
4323add6 2642 if (!strcmp(args->name, tipc_bclink_name)) {
b97bf3fd 2643 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
4323add6
PL
2644 (tipc_bclink_set_queue_limits(new_value) == 0))
2645 return tipc_cfg_reply_none();
c4307285 2646 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
4323add6 2647 " (cannot change setting on broadcast link)");
b97bf3fd
PL
2648 }
2649
4323add6 2650 read_lock_bh(&tipc_net_lock);
5c216e1d 2651 res = link_cmd_set_value(args->name, new_value, cmd);
4323add6 2652 read_unlock_bh(&tipc_net_lock);
b97bf3fd 2653 if (res)
c4307285 2654 return tipc_cfg_reply_error_string("cannot change link setting");
b97bf3fd 2655
4323add6 2656 return tipc_cfg_reply_none();
b97bf3fd
PL
2657}
2658
2659/**
2660 * link_reset_statistics - reset link statistics
2661 * @l_ptr: pointer to link
2662 */
a18c4bc3 2663static void link_reset_statistics(struct tipc_link *l_ptr)
b97bf3fd
PL
2664{
2665 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2666 l_ptr->stats.sent_info = l_ptr->next_out_no;
2667 l_ptr->stats.recv_info = l_ptr->next_in_no;
2668}
2669
4323add6 2670struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
2671{
2672 char *link_name;
a18c4bc3 2673 struct tipc_link *l_ptr;
6c00055a 2674 struct tipc_node *node;
b97bf3fd
PL
2675
2676 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
4323add6 2677 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd
PL
2678
2679 link_name = (char *)TLV_DATA(req_tlv_area);
4323add6
PL
2680 if (!strcmp(link_name, tipc_bclink_name)) {
2681 if (tipc_bclink_reset_stats())
2682 return tipc_cfg_reply_error_string("link not found");
2683 return tipc_cfg_reply_none();
b97bf3fd
PL
2684 }
2685
4323add6 2686 read_lock_bh(&tipc_net_lock);
c4307285 2687 l_ptr = link_find_link(link_name, &node);
b97bf3fd 2688 if (!l_ptr) {
4323add6
PL
2689 read_unlock_bh(&tipc_net_lock);
2690 return tipc_cfg_reply_error_string("link not found");
b97bf3fd
PL
2691 }
2692
4323add6 2693 tipc_node_lock(node);
b97bf3fd 2694 link_reset_statistics(l_ptr);
4323add6
PL
2695 tipc_node_unlock(node);
2696 read_unlock_bh(&tipc_net_lock);
2697 return tipc_cfg_reply_none();
b97bf3fd
PL
2698}
2699
2700/**
2701 * percent - convert count to a percentage of total (rounding up or down)
2702 */
b97bf3fd
PL
2703static u32 percent(u32 count, u32 total)
2704{
2705 return (count * 100 + (total / 2)) / total;
2706}
2707
2708/**
4323add6 2709 * tipc_link_stats - print link statistics
b97bf3fd
PL
2710 * @name: link name
2711 * @buf: print buffer area
2712 * @buf_size: size of print buffer area
c4307285 2713 *
b97bf3fd
PL
2714 * Returns length of print buffer data string (or 0 if error)
2715 */
4323add6 2716static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
b97bf3fd 2717{
dc1aed37
EH
2718 struct tipc_link *l;
2719 struct tipc_stats *s;
6c00055a 2720 struct tipc_node *node;
b97bf3fd
PL
2721 char *status;
2722 u32 profile_total = 0;
dc1aed37 2723 int ret;
b97bf3fd 2724
4323add6
PL
2725 if (!strcmp(name, tipc_bclink_name))
2726 return tipc_bclink_stats(buf, buf_size);
b97bf3fd 2727
4323add6 2728 read_lock_bh(&tipc_net_lock);
dc1aed37
EH
2729 l = link_find_link(name, &node);
2730 if (!l) {
4323add6 2731 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
2732 return 0;
2733 }
4323add6 2734 tipc_node_lock(node);
dc1aed37 2735 s = &l->stats;
b97bf3fd 2736
dc1aed37 2737 if (tipc_link_is_active(l))
b97bf3fd 2738 status = "ACTIVE";
dc1aed37 2739 else if (tipc_link_is_up(l))
b97bf3fd
PL
2740 status = "STANDBY";
2741 else
2742 status = "DEFUNCT";
dc1aed37
EH
2743
2744 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2745 " %s MTU:%u Priority:%u Tolerance:%u ms"
2746 " Window:%u packets\n",
2747 l->name, status, l->max_pkt, l->priority,
2748 l->tolerance, l->queue_limit[0]);
2749
2750 ret += tipc_snprintf(buf + ret, buf_size - ret,
2751 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2752 l->next_in_no - s->recv_info, s->recv_fragments,
2753 s->recv_fragmented, s->recv_bundles,
2754 s->recv_bundled);
2755
2756 ret += tipc_snprintf(buf + ret, buf_size - ret,
2757 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2758 l->next_out_no - s->sent_info, s->sent_fragments,
2759 s->sent_fragmented, s->sent_bundles,
2760 s->sent_bundled);
2761
2762 profile_total = s->msg_length_counts;
b97bf3fd
PL
2763 if (!profile_total)
2764 profile_total = 1;
dc1aed37
EH
2765
2766 ret += tipc_snprintf(buf + ret, buf_size - ret,
2767 " TX profile sample:%u packets average:%u octets\n"
2768 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2769 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2770 s->msg_length_counts,
2771 s->msg_lengths_total / profile_total,
2772 percent(s->msg_length_profile[0], profile_total),
2773 percent(s->msg_length_profile[1], profile_total),
2774 percent(s->msg_length_profile[2], profile_total),
2775 percent(s->msg_length_profile[3], profile_total),
2776 percent(s->msg_length_profile[4], profile_total),
2777 percent(s->msg_length_profile[5], profile_total),
2778 percent(s->msg_length_profile[6], profile_total));
2779
2780 ret += tipc_snprintf(buf + ret, buf_size - ret,
2781 " RX states:%u probes:%u naks:%u defs:%u"
2782 " dups:%u\n", s->recv_states, s->recv_probes,
2783 s->recv_nacks, s->deferred_recv, s->duplicates);
2784
2785 ret += tipc_snprintf(buf + ret, buf_size - ret,
2786 " TX states:%u probes:%u naks:%u acks:%u"
2787 " dups:%u\n", s->sent_states, s->sent_probes,
2788 s->sent_nacks, s->sent_acks, s->retransmitted);
2789
2790 ret += tipc_snprintf(buf + ret, buf_size - ret,
3c294cb3
YX
2791 " Congestion link:%u Send queue"
2792 " max:%u avg:%u\n", s->link_congs,
dc1aed37
EH
2793 s->max_queue_sz, s->queue_sz_counts ?
2794 (s->accu_queue_sz / s->queue_sz_counts) : 0);
b97bf3fd 2795
4323add6
PL
2796 tipc_node_unlock(node);
2797 read_unlock_bh(&tipc_net_lock);
dc1aed37 2798 return ret;
b97bf3fd
PL
2799}
2800
4323add6 2801struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
2802{
2803 struct sk_buff *buf;
2804 struct tlv_desc *rep_tlv;
2805 int str_len;
dc1aed37
EH
2806 int pb_len;
2807 char *pb;
b97bf3fd
PL
2808
2809 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
4323add6 2810 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 2811
dc1aed37 2812 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
b97bf3fd
PL
2813 if (!buf)
2814 return NULL;
2815
2816 rep_tlv = (struct tlv_desc *)buf->data;
dc1aed37
EH
2817 pb = TLV_DATA(rep_tlv);
2818 pb_len = ULTRA_STRING_MAX_LEN;
4323add6 2819 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
dc1aed37 2820 pb, pb_len);
b97bf3fd 2821 if (!str_len) {
5f6d9123 2822 kfree_skb(buf);
c4307285 2823 return tipc_cfg_reply_error_string("link not found");
b97bf3fd 2824 }
dc1aed37 2825 str_len += 1; /* for "\0" */
b97bf3fd
PL
2826 skb_put(buf, TLV_SPACE(str_len));
2827 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2828
2829 return buf;
2830}
2831
b97bf3fd 2832/**
4323add6 2833 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
b97bf3fd
PL
2834 * @dest: network address of destination node
2835 * @selector: used to select from set of active links
c4307285 2836 *
b97bf3fd
PL
2837 * If no active link can be found, uses default maximum packet size.
2838 */
4323add6 2839u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
b97bf3fd 2840{
6c00055a 2841 struct tipc_node *n_ptr;
a18c4bc3 2842 struct tipc_link *l_ptr;
b97bf3fd 2843 u32 res = MAX_PKT_DEFAULT;
c4307285 2844
b97bf3fd
PL
2845 if (dest == tipc_own_addr)
2846 return MAX_MSG_SIZE;
2847
c4307285 2848 read_lock_bh(&tipc_net_lock);
51a8e4de 2849 n_ptr = tipc_node_find(dest);
b97bf3fd 2850 if (n_ptr) {
4323add6 2851 tipc_node_lock(n_ptr);
b97bf3fd
PL
2852 l_ptr = n_ptr->active_links[selector & 1];
2853 if (l_ptr)
15e979da 2854 res = l_ptr->max_pkt;
4323add6 2855 tipc_node_unlock(n_ptr);
b97bf3fd 2856 }
c4307285 2857 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
2858 return res;
2859}
2860
a18c4bc3 2861static void link_print(struct tipc_link *l_ptr, const char *str)
b97bf3fd 2862{
5deedde9 2863 pr_info("%s Link %x<%s>:", str, l_ptr->addr, l_ptr->b_ptr->name);
8d64a5ba 2864
b97bf3fd 2865 if (link_working_unknown(l_ptr))
5deedde9 2866 pr_cont(":WU\n");
8d64a5ba 2867 else if (link_reset_reset(l_ptr))
5deedde9 2868 pr_cont(":RR\n");
8d64a5ba 2869 else if (link_reset_unknown(l_ptr))
5deedde9 2870 pr_cont(":RU\n");
8d64a5ba 2871 else if (link_working_working(l_ptr))
5deedde9
PG
2872 pr_cont(":WW\n");
2873 else
2874 pr_cont("\n");
b97bf3fd 2875}