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