tipc: eliminate delayed link deletion at link failover
[linux-2.6-block.git] / net / tipc / link.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/link.c: TIPC link code
c4307285 3 *
c1336ee4 4 * Copyright (c) 1996-2007, 2012-2015, Ericsson AB
198d73b8 5 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
b97bf3fd 19 *
9ea1fd3c
PL
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
e3eea1eb 38#include "subscr.h"
b97bf3fd 39#include "link.h"
7be57fc6 40#include "bcast.h"
9816f061 41#include "socket.h"
b97bf3fd 42#include "name_distr.h"
b97bf3fd 43#include "discover.h"
0655f6a8 44#include "netlink.h"
b97bf3fd 45
796c75d0
YX
46#include <linux/pkt_sched.h>
47
2cf8aa19
EH
48/*
49 * Error message prefixes
50 */
51static const char *link_co_err = "Link changeover error, ";
52static const char *link_rst_msg = "Resetting link ";
53static const char *link_unk_evt = "Unknown link event ";
b97bf3fd 54
7be57fc6
RA
55static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
56 [TIPC_NLA_LINK_UNSPEC] = { .type = NLA_UNSPEC },
57 [TIPC_NLA_LINK_NAME] = {
58 .type = NLA_STRING,
59 .len = TIPC_MAX_LINK_NAME
60 },
61 [TIPC_NLA_LINK_MTU] = { .type = NLA_U32 },
62 [TIPC_NLA_LINK_BROADCAST] = { .type = NLA_FLAG },
63 [TIPC_NLA_LINK_UP] = { .type = NLA_FLAG },
64 [TIPC_NLA_LINK_ACTIVE] = { .type = NLA_FLAG },
65 [TIPC_NLA_LINK_PROP] = { .type = NLA_NESTED },
66 [TIPC_NLA_LINK_STATS] = { .type = NLA_NESTED },
67 [TIPC_NLA_LINK_RX] = { .type = NLA_U32 },
68 [TIPC_NLA_LINK_TX] = { .type = NLA_U32 }
69};
70
0655f6a8
RA
71/* Properties valid for media, bearar and link */
72static const struct nla_policy tipc_nl_prop_policy[TIPC_NLA_PROP_MAX + 1] = {
73 [TIPC_NLA_PROP_UNSPEC] = { .type = NLA_UNSPEC },
74 [TIPC_NLA_PROP_PRIO] = { .type = NLA_U32 },
75 [TIPC_NLA_PROP_TOL] = { .type = NLA_U32 },
76 [TIPC_NLA_PROP_WIN] = { .type = NLA_U32 }
77};
78
a686e685
AS
79/*
80 * Out-of-range value for link session numbers
81 */
a686e685
AS
82#define INVALID_SESSION 0x10000
83
c4307285
YH
84/*
85 * Link state events:
b97bf3fd 86 */
b97bf3fd
PL
87#define STARTING_EVT 856384768 /* link processing trigger */
88#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
89#define TIMEOUT_EVT 560817u /* link timer expired */
90
c4307285 91/*
dff29b1a 92 * State value stored in 'failover_pkts'
b97bf3fd 93 */
dff29b1a 94#define FIRST_FAILOVER 0xffffu
b97bf3fd 95
c5898636
JPM
96static void link_handle_out_of_seq_msg(struct tipc_link *link,
97 struct sk_buff *skb);
98static void tipc_link_proto_rcv(struct tipc_link *link,
99 struct sk_buff *skb);
2f55c437 100static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol);
a18c4bc3
PG
101static void link_state_event(struct tipc_link *l_ptr, u32 event);
102static void link_reset_statistics(struct tipc_link *l_ptr);
103static void link_print(struct tipc_link *l_ptr, const char *str);
247f0f3c
YX
104static void tipc_link_sync_xmit(struct tipc_link *l);
105static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
c637c103
JPM
106static void tipc_link_input(struct tipc_link *l, struct sk_buff *skb);
107static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb);
dff29b1a 108static bool tipc_link_failover_rcv(struct tipc_link *l, struct sk_buff **skb);
b97bf3fd 109/*
05790c64 110 * Simple link routines
b97bf3fd 111 */
05790c64 112static unsigned int align(unsigned int i)
b97bf3fd
PL
113{
114 return (i + 3) & ~3u;
115}
116
2d72d495
JPM
117static void tipc_link_release(struct kref *kref)
118{
119 kfree(container_of(kref, struct tipc_link, ref));
120}
121
122static void tipc_link_get(struct tipc_link *l_ptr)
123{
124 kref_get(&l_ptr->ref);
125}
126
127static void tipc_link_put(struct tipc_link *l_ptr)
128{
129 kref_put(&l_ptr->ref, tipc_link_release);
130}
131
8b4ed863
JPM
132static struct tipc_link *tipc_parallel_link(struct tipc_link *l)
133{
134 if (l->owner->active_links[0] != l)
135 return l->owner->active_links[0];
136 return l->owner->active_links[1];
137}
138
a18c4bc3 139static void link_init_max_pkt(struct tipc_link *l_ptr)
b97bf3fd 140{
7f9f95d9
YX
141 struct tipc_node *node = l_ptr->owner;
142 struct tipc_net *tn = net_generic(node->net, tipc_net_id);
7a2f7d18 143 struct tipc_bearer *b_ptr;
b97bf3fd 144 u32 max_pkt;
c4307285 145
7a2f7d18 146 rcu_read_lock();
7f9f95d9 147 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
7a2f7d18
YX
148 if (!b_ptr) {
149 rcu_read_unlock();
150 return;
151 }
152 max_pkt = (b_ptr->mtu & ~3);
153 rcu_read_unlock();
154
b97bf3fd
PL
155 if (max_pkt > MAX_MSG_SIZE)
156 max_pkt = MAX_MSG_SIZE;
157
c4307285 158 l_ptr->max_pkt_target = max_pkt;
b97bf3fd
PL
159 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
160 l_ptr->max_pkt = l_ptr->max_pkt_target;
c4307285 161 else
b97bf3fd
PL
162 l_ptr->max_pkt = MAX_PKT_DEFAULT;
163
c4307285 164 l_ptr->max_pkt_probes = 0;
b97bf3fd
PL
165}
166
b97bf3fd 167/*
05790c64 168 * Simple non-static link routines (i.e. referenced outside this file)
b97bf3fd 169 */
a18c4bc3 170int tipc_link_is_up(struct tipc_link *l_ptr)
b97bf3fd
PL
171{
172 if (!l_ptr)
173 return 0;
a02cec21 174 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
b97bf3fd
PL
175}
176
a18c4bc3 177int tipc_link_is_active(struct tipc_link *l_ptr)
b97bf3fd 178{
a02cec21
ED
179 return (l_ptr->owner->active_links[0] == l_ptr) ||
180 (l_ptr->owner->active_links[1] == l_ptr);
b97bf3fd
PL
181}
182
b97bf3fd
PL
183/**
184 * link_timeout - handle expiration of link timer
185 * @l_ptr: pointer to link
b97bf3fd 186 */
2f55c437 187static void link_timeout(unsigned long data)
b97bf3fd 188{
2f55c437 189 struct tipc_link *l_ptr = (struct tipc_link *)data;
58dc55f2
YX
190 struct sk_buff *skb;
191
4323add6 192 tipc_node_lock(l_ptr->owner);
b97bf3fd
PL
193
194 /* update counters used in statistical profiling of send traffic */
05dcc5aa 195 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->transmq);
b97bf3fd
PL
196 l_ptr->stats.queue_sz_counts++;
197
05dcc5aa 198 skb = skb_peek(&l_ptr->transmq);
58dc55f2
YX
199 if (skb) {
200 struct tipc_msg *msg = buf_msg(skb);
b97bf3fd
PL
201 u32 length = msg_size(msg);
202
f64f9e71
JP
203 if ((msg_user(msg) == MSG_FRAGMENTER) &&
204 (msg_type(msg) == FIRST_FRAGMENT)) {
b97bf3fd
PL
205 length = msg_size(msg_get_wrapped(msg));
206 }
207 if (length) {
208 l_ptr->stats.msg_lengths_total += length;
209 l_ptr->stats.msg_length_counts++;
210 if (length <= 64)
211 l_ptr->stats.msg_length_profile[0]++;
212 else if (length <= 256)
213 l_ptr->stats.msg_length_profile[1]++;
214 else if (length <= 1024)
215 l_ptr->stats.msg_length_profile[2]++;
216 else if (length <= 4096)
217 l_ptr->stats.msg_length_profile[3]++;
218 else if (length <= 16384)
219 l_ptr->stats.msg_length_profile[4]++;
220 else if (length <= 32768)
221 l_ptr->stats.msg_length_profile[5]++;
222 else
223 l_ptr->stats.msg_length_profile[6]++;
224 }
225 }
226
227 /* do all other link processing performed on a periodic basis */
b97bf3fd
PL
228 link_state_event(l_ptr, TIMEOUT_EVT);
229
05dcc5aa 230 if (skb_queue_len(&l_ptr->backlogq))
47b4c9a8 231 tipc_link_push_packets(l_ptr);
b97bf3fd 232
4323add6 233 tipc_node_unlock(l_ptr->owner);
2d72d495 234 tipc_link_put(l_ptr);
b97bf3fd
PL
235}
236
2f55c437 237static void link_set_timer(struct tipc_link *link, unsigned long time)
b97bf3fd 238{
2d72d495
JPM
239 if (!mod_timer(&link->timer, jiffies + time))
240 tipc_link_get(link);
b97bf3fd
PL
241}
242
243/**
4323add6 244 * tipc_link_create - create a new link
37b9c08a 245 * @n_ptr: pointer to associated node
b97bf3fd 246 * @b_ptr: pointer to associated bearer
b97bf3fd 247 * @media_addr: media address to use when sending messages over link
c4307285 248 *
b97bf3fd
PL
249 * Returns pointer to link.
250 */
a18c4bc3 251struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
c61dd61d
YX
252 struct tipc_bearer *b_ptr,
253 const struct tipc_media_addr *media_addr)
b97bf3fd 254{
34747539 255 struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
a18c4bc3 256 struct tipc_link *l_ptr;
b97bf3fd
PL
257 struct tipc_msg *msg;
258 char *if_name;
37b9c08a
AS
259 char addr_string[16];
260 u32 peer = n_ptr->addr;
261
0372bf5c 262 if (n_ptr->link_cnt >= MAX_BEARERS) {
37b9c08a 263 tipc_addr_string_fill(addr_string, n_ptr->addr);
0372bf5c
HB
264 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
265 n_ptr->link_cnt, addr_string, MAX_BEARERS);
37b9c08a
AS
266 return NULL;
267 }
268
269 if (n_ptr->links[b_ptr->identity]) {
270 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19
EH
271 pr_err("Attempt to establish second link on <%s> to %s\n",
272 b_ptr->name, addr_string);
37b9c08a
AS
273 return NULL;
274 }
b97bf3fd 275
0da974f4 276 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
b97bf3fd 277 if (!l_ptr) {
2cf8aa19 278 pr_warn("Link creation failed, no memory\n");
b97bf3fd
PL
279 return NULL;
280 }
2d72d495 281 kref_init(&l_ptr->ref);
b97bf3fd 282 l_ptr->addr = peer;
2d627b92 283 if_name = strchr(b_ptr->name, ':') + 1;
062b4c99 284 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
34747539
YX
285 tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
286 tipc_node(tn->own_addr),
b97bf3fd
PL
287 if_name,
288 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
062b4c99 289 /* note: peer i/f name is updated by reset/activate message */
b97bf3fd 290 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
37b9c08a 291 l_ptr->owner = n_ptr;
b97bf3fd 292 l_ptr->checkpoint = 1;
f882cb76 293 l_ptr->peer_session = INVALID_SESSION;
7a2f7d18 294 l_ptr->bearer_id = b_ptr->identity;
5c216e1d 295 link_set_supervision_props(l_ptr, b_ptr->tolerance);
b97bf3fd
PL
296 l_ptr->state = RESET_UNKNOWN;
297
298 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
299 msg = l_ptr->pmsg;
c5898636 300 tipc_msg_init(tn->own_addr, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
34747539 301 l_ptr->addr);
b97bf3fd 302 msg_set_size(msg, sizeof(l_ptr->proto_msg));
bafa29e3 303 msg_set_session(msg, (tn->random & 0xffff));
b97bf3fd
PL
304 msg_set_bearer_id(msg, b_ptr->identity);
305 strcpy((char *)msg_data(msg), if_name);
7a2f7d18 306 l_ptr->net_plane = b_ptr->net_plane;
b97bf3fd 307 link_init_max_pkt(l_ptr);
e3eea1eb
JPM
308 l_ptr->priority = b_ptr->priority;
309 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
b97bf3fd 310 l_ptr->next_out_no = 1;
05dcc5aa
JPM
311 __skb_queue_head_init(&l_ptr->transmq);
312 __skb_queue_head_init(&l_ptr->backlogq);
313 __skb_queue_head_init(&l_ptr->deferdq);
c637c103
JPM
314 skb_queue_head_init(&l_ptr->wakeupq);
315 skb_queue_head_init(&l_ptr->inputq);
316 skb_queue_head_init(&l_ptr->namedq);
b97bf3fd 317 link_reset_statistics(l_ptr);
37b9c08a 318 tipc_node_attach_link(n_ptr, l_ptr);
2f55c437 319 setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
581465fa 320 link_state_event(l_ptr, STARTING_EVT);
b97bf3fd 321
b97bf3fd
PL
322 return l_ptr;
323}
324
2d72d495 325/**
dff29b1a
JPM
326 * tipc_link_delete - Delete a link
327 * @l: link to be deleted
2d72d495 328 */
dff29b1a 329void tipc_link_delete(struct tipc_link *l)
2d72d495 330{
dff29b1a
JPM
331 tipc_link_reset(l);
332 if (del_timer(&l->timer))
333 tipc_link_put(l);
334 l->flags |= LINK_STOPPED;
335 /* Delete link now, or when timer is finished: */
336 tipc_link_reset_fragments(l);
337 tipc_node_detach_link(l->owner, l);
338 tipc_link_put(l);
2d72d495
JPM
339}
340
f2f9800d
YX
341void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
342 bool shutting_down)
8d8439b6 343{
f2f9800d 344 struct tipc_net *tn = net_generic(net, tipc_net_id);
2d72d495
JPM
345 struct tipc_link *link;
346 struct tipc_node *node;
8d8439b6 347
6c7a762e 348 rcu_read_lock();
2d72d495
JPM
349 list_for_each_entry_rcu(node, &tn->node_list, list) {
350 tipc_node_lock(node);
351 link = node->links[bearer_id];
dff29b1a 352 if (link)
2d72d495
JPM
353 tipc_link_delete(link);
354 tipc_node_unlock(node);
8d8439b6 355 }
6c7a762e 356 rcu_read_unlock();
8d8439b6 357}
b97bf3fd
PL
358
359/**
3127a020 360 * link_schedule_user - schedule a message sender for wakeup after congestion
50100a5e 361 * @link: congested link
3127a020 362 * @list: message that was attempted sent
50100a5e 363 * Create pseudo msg to send back to user when congestion abates
3127a020 364 * Only consumes message if there is an error
b97bf3fd 365 */
3127a020 366static int link_schedule_user(struct tipc_link *link, struct sk_buff_head *list)
b97bf3fd 367{
3127a020
JPM
368 struct tipc_msg *msg = buf_msg(skb_peek(list));
369 int imp = msg_importance(msg);
370 u32 oport = msg_origport(msg);
371 u32 addr = link_own_addr(link);
372 struct sk_buff *skb;
373
374 /* This really cannot happen... */
375 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
376 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
377 tipc_link_reset(link);
378 goto err;
379 }
380 /* Non-blocking sender: */
381 if (TIPC_SKB_CB(skb_peek(list))->wakeup_pending)
382 return -ELINKCONG;
383
384 /* Create and schedule wakeup pseudo message */
385 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
386 addr, addr, oport, 0, 0);
387 if (!skb)
388 goto err;
389 TIPC_SKB_CB(skb)->chain_sz = skb_queue_len(list);
390 TIPC_SKB_CB(skb)->chain_imp = imp;
391 skb_queue_tail(&link->wakeupq, skb);
50100a5e 392 link->stats.link_congs++;
3127a020
JPM
393 return -ELINKCONG;
394err:
395 __skb_queue_purge(list);
396 return -ENOBUFS;
b97bf3fd
PL
397}
398
50100a5e
JPM
399/**
400 * link_prepare_wakeup - prepare users for wakeup after congestion
401 * @link: congested link
402 * Move a number of waiting users, as permitted by available space in
403 * the send queue, from link wait queue to node wait queue for wakeup
404 */
1f66d161 405void link_prepare_wakeup(struct tipc_link *l)
b97bf3fd 406{
1f66d161
JPM
407 int pnd[TIPC_SYSTEM_IMPORTANCE + 1] = {0,};
408 int imp, lim;
58d78b32 409 struct sk_buff *skb, *tmp;
50100a5e 410
1f66d161
JPM
411 skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
412 imp = TIPC_SKB_CB(skb)->chain_imp;
413 lim = l->window + l->backlog[imp].limit;
414 pnd[imp] += TIPC_SKB_CB(skb)->chain_sz;
415 if ((pnd[imp] + l->backlog[imp].len) >= lim)
b97bf3fd 416 break;
1f66d161
JPM
417 skb_unlink(skb, &l->wakeupq);
418 skb_queue_tail(&l->inputq, skb);
419 l->owner->inputq = &l->inputq;
420 l->owner->action_flags |= TIPC_MSG_EVT;
b97bf3fd 421 }
b97bf3fd
PL
422}
423
b97bf3fd 424/**
4323add6 425 * tipc_link_reset_fragments - purge link's inbound message fragments queue
b97bf3fd
PL
426 * @l_ptr: pointer to link
427 */
a18c4bc3 428void tipc_link_reset_fragments(struct tipc_link *l_ptr)
b97bf3fd 429{
37e22164
JPM
430 kfree_skb(l_ptr->reasm_buf);
431 l_ptr->reasm_buf = NULL;
b97bf3fd
PL
432}
433
1f66d161
JPM
434static void tipc_link_purge_backlog(struct tipc_link *l)
435{
436 __skb_queue_purge(&l->backlogq);
437 l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
438 l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
439 l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
440 l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
441 l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
442}
443
c4307285 444/**
581465fa 445 * tipc_link_purge_queues - purge all pkt queues associated with link
b97bf3fd
PL
446 * @l_ptr: pointer to link
447 */
581465fa 448void tipc_link_purge_queues(struct tipc_link *l_ptr)
b97bf3fd 449{
05dcc5aa
JPM
450 __skb_queue_purge(&l_ptr->deferdq);
451 __skb_queue_purge(&l_ptr->transmq);
1f66d161 452 tipc_link_purge_backlog(l_ptr);
4323add6 453 tipc_link_reset_fragments(l_ptr);
b97bf3fd
PL
454}
455
a18c4bc3 456void tipc_link_reset(struct tipc_link *l_ptr)
b97bf3fd 457{
b97bf3fd 458 u32 prev_state = l_ptr->state;
5392d646 459 int was_active_link = tipc_link_is_active(l_ptr);
50100a5e 460 struct tipc_node *owner = l_ptr->owner;
dff29b1a 461 struct tipc_link *pl = tipc_parallel_link(l_ptr);
c4307285 462
a686e685 463 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
b97bf3fd 464
a686e685
AS
465 /* Link is down, accept any session */
466 l_ptr->peer_session = INVALID_SESSION;
b97bf3fd 467
c4307285 468 /* Prepare for max packet size negotiation */
b97bf3fd 469 link_init_max_pkt(l_ptr);
c4307285 470
b97bf3fd 471 l_ptr->state = RESET_UNKNOWN;
b97bf3fd
PL
472
473 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
474 return;
475
4323add6 476 tipc_node_link_down(l_ptr->owner, l_ptr);
7f9f95d9 477 tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
7368ddf1 478
dff29b1a
JPM
479 if (was_active_link && tipc_node_is_up(l_ptr->owner) && (pl != l_ptr)) {
480 l_ptr->flags |= LINK_FAILINGOVER;
481 l_ptr->failover_checkpt = l_ptr->next_in_no;
482 pl->failover_pkts = FIRST_FAILOVER;
483 pl->failover_checkpt = l_ptr->next_in_no;
484 pl->failover_skb = l_ptr->reasm_buf;
485 } else {
486 kfree_skb(l_ptr->reasm_buf);
b97bf3fd 487 }
c637c103 488 /* Clean up all queues, except inputq: */
05dcc5aa 489 __skb_queue_purge(&l_ptr->transmq);
05dcc5aa 490 __skb_queue_purge(&l_ptr->deferdq);
e6441bae
JPM
491 if (!owner->inputq)
492 owner->inputq = &l_ptr->inputq;
493 skb_queue_splice_init(&l_ptr->wakeupq, owner->inputq);
494 if (!skb_queue_empty(owner->inputq))
c637c103 495 owner->action_flags |= TIPC_MSG_EVT;
1f66d161 496 tipc_link_purge_backlog(l_ptr);
dff29b1a 497 l_ptr->reasm_buf = NULL;
05dcc5aa 498 l_ptr->rcv_unacked = 0;
b97bf3fd
PL
499 l_ptr->checkpoint = 1;
500 l_ptr->next_out_no = 1;
b97bf3fd
PL
501 l_ptr->fsm_msg_cnt = 0;
502 l_ptr->stale_count = 0;
503 link_reset_statistics(l_ptr);
b97bf3fd
PL
504}
505
f2f9800d 506void tipc_link_reset_list(struct net *net, unsigned int bearer_id)
e0ca2c30 507{
f2f9800d 508 struct tipc_net *tn = net_generic(net, tipc_net_id);
e0ca2c30 509 struct tipc_link *l_ptr;
c61dd61d 510 struct tipc_node *n_ptr;
e0ca2c30 511
6c7a762e 512 rcu_read_lock();
f2f9800d 513 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
5356f3d7 514 tipc_node_lock(n_ptr);
c61dd61d
YX
515 l_ptr = n_ptr->links[bearer_id];
516 if (l_ptr)
517 tipc_link_reset(l_ptr);
5356f3d7 518 tipc_node_unlock(n_ptr);
e0ca2c30 519 }
6c7a762e 520 rcu_read_unlock();
e0ca2c30 521}
b97bf3fd 522
7f9f95d9 523static void link_activate(struct tipc_link *link)
b97bf3fd 524{
7f9f95d9
YX
525 struct tipc_node *node = link->owner;
526
527 link->next_in_no = 1;
528 link->stats.recv_info = 1;
529 tipc_node_link_up(node, link);
530 tipc_bearer_add_dest(node->net, link->bearer_id, link->addr);
b97bf3fd
PL
531}
532
533/**
534 * link_state_event - link finite state machine
535 * @l_ptr: pointer to link
536 * @event: state machine event to process
537 */
95c96174 538static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
b97bf3fd 539{
a18c4bc3 540 struct tipc_link *other;
2f55c437 541 unsigned long cont_intv = l_ptr->cont_intv;
b97bf3fd 542
7d33939f
JPM
543 if (l_ptr->flags & LINK_STOPPED)
544 return;
545
135daee6 546 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
b97bf3fd
PL
547 return; /* Not yet. */
548
dff29b1a 549 if (l_ptr->flags & LINK_FAILINGOVER) {
a016892c 550 if (event == TIMEOUT_EVT)
b97bf3fd 551 link_set_timer(l_ptr, cont_intv);
77a7e07a 552 return;
b97bf3fd 553 }
b97bf3fd
PL
554
555 switch (l_ptr->state) {
556 case WORKING_WORKING:
b97bf3fd
PL
557 switch (event) {
558 case TRAFFIC_MSG_EVT:
b97bf3fd 559 case ACTIVATE_MSG:
b97bf3fd
PL
560 break;
561 case TIMEOUT_EVT:
b97bf3fd
PL
562 if (l_ptr->next_in_no != l_ptr->checkpoint) {
563 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6 564 if (tipc_bclink_acks_missing(l_ptr->owner)) {
247f0f3c
YX
565 tipc_link_proto_xmit(l_ptr, STATE_MSG,
566 0, 0, 0, 0, 0);
b97bf3fd
PL
567 l_ptr->fsm_msg_cnt++;
568 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
247f0f3c
YX
569 tipc_link_proto_xmit(l_ptr, STATE_MSG,
570 1, 0, 0, 0, 0);
b97bf3fd
PL
571 l_ptr->fsm_msg_cnt++;
572 }
573 link_set_timer(l_ptr, cont_intv);
574 break;
575 }
b97bf3fd
PL
576 l_ptr->state = WORKING_UNKNOWN;
577 l_ptr->fsm_msg_cnt = 0;
247f0f3c 578 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
579 l_ptr->fsm_msg_cnt++;
580 link_set_timer(l_ptr, cont_intv / 4);
581 break;
582 case RESET_MSG:
3fa9cacd
EH
583 pr_debug("%s<%s>, requested by peer\n",
584 link_rst_msg, l_ptr->name);
4323add6 585 tipc_link_reset(l_ptr);
b97bf3fd
PL
586 l_ptr->state = RESET_RESET;
587 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
588 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
589 0, 0, 0, 0, 0);
b97bf3fd
PL
590 l_ptr->fsm_msg_cnt++;
591 link_set_timer(l_ptr, cont_intv);
592 break;
593 default:
3fa9cacd 594 pr_debug("%s%u in WW state\n", link_unk_evt, event);
b97bf3fd
PL
595 }
596 break;
597 case WORKING_UNKNOWN:
b97bf3fd
PL
598 switch (event) {
599 case TRAFFIC_MSG_EVT:
b97bf3fd 600 case ACTIVATE_MSG:
b97bf3fd
PL
601 l_ptr->state = WORKING_WORKING;
602 l_ptr->fsm_msg_cnt = 0;
603 link_set_timer(l_ptr, cont_intv);
604 break;
605 case RESET_MSG:
3fa9cacd
EH
606 pr_debug("%s<%s>, requested by peer while probing\n",
607 link_rst_msg, l_ptr->name);
4323add6 608 tipc_link_reset(l_ptr);
b97bf3fd
PL
609 l_ptr->state = RESET_RESET;
610 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
611 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
612 0, 0, 0, 0, 0);
b97bf3fd
PL
613 l_ptr->fsm_msg_cnt++;
614 link_set_timer(l_ptr, cont_intv);
615 break;
616 case TIMEOUT_EVT:
b97bf3fd 617 if (l_ptr->next_in_no != l_ptr->checkpoint) {
b97bf3fd
PL
618 l_ptr->state = WORKING_WORKING;
619 l_ptr->fsm_msg_cnt = 0;
620 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6 621 if (tipc_bclink_acks_missing(l_ptr->owner)) {
247f0f3c
YX
622 tipc_link_proto_xmit(l_ptr, STATE_MSG,
623 0, 0, 0, 0, 0);
b97bf3fd
PL
624 l_ptr->fsm_msg_cnt++;
625 }
626 link_set_timer(l_ptr, cont_intv);
627 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
247f0f3c
YX
628 tipc_link_proto_xmit(l_ptr, STATE_MSG,
629 1, 0, 0, 0, 0);
b97bf3fd
PL
630 l_ptr->fsm_msg_cnt++;
631 link_set_timer(l_ptr, cont_intv / 4);
632 } else { /* Link has failed */
3fa9cacd
EH
633 pr_debug("%s<%s>, peer not responding\n",
634 link_rst_msg, l_ptr->name);
4323add6 635 tipc_link_reset(l_ptr);
b97bf3fd
PL
636 l_ptr->state = RESET_UNKNOWN;
637 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
638 tipc_link_proto_xmit(l_ptr, RESET_MSG,
639 0, 0, 0, 0, 0);
b97bf3fd
PL
640 l_ptr->fsm_msg_cnt++;
641 link_set_timer(l_ptr, cont_intv);
642 }
643 break;
644 default:
2cf8aa19 645 pr_err("%s%u in WU state\n", link_unk_evt, event);
b97bf3fd
PL
646 }
647 break;
648 case RESET_UNKNOWN:
b97bf3fd
PL
649 switch (event) {
650 case TRAFFIC_MSG_EVT:
b97bf3fd
PL
651 break;
652 case ACTIVATE_MSG:
653 other = l_ptr->owner->active_links[0];
8d64a5ba 654 if (other && link_working_unknown(other))
b97bf3fd 655 break;
b97bf3fd
PL
656 l_ptr->state = WORKING_WORKING;
657 l_ptr->fsm_msg_cnt = 0;
658 link_activate(l_ptr);
247f0f3c 659 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd 660 l_ptr->fsm_msg_cnt++;
c64f7a6a 661 if (l_ptr->owner->working_links == 1)
247f0f3c 662 tipc_link_sync_xmit(l_ptr);
b97bf3fd
PL
663 link_set_timer(l_ptr, cont_intv);
664 break;
665 case RESET_MSG:
b97bf3fd
PL
666 l_ptr->state = RESET_RESET;
667 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
668 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
669 1, 0, 0, 0, 0);
b97bf3fd
PL
670 l_ptr->fsm_msg_cnt++;
671 link_set_timer(l_ptr, cont_intv);
672 break;
673 case STARTING_EVT:
135daee6 674 l_ptr->flags |= LINK_STARTED;
af9946fd
JPM
675 l_ptr->fsm_msg_cnt++;
676 link_set_timer(l_ptr, cont_intv);
677 break;
b97bf3fd 678 case TIMEOUT_EVT:
247f0f3c 679 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
680 l_ptr->fsm_msg_cnt++;
681 link_set_timer(l_ptr, cont_intv);
682 break;
683 default:
2cf8aa19 684 pr_err("%s%u in RU state\n", link_unk_evt, event);
b97bf3fd
PL
685 }
686 break;
687 case RESET_RESET:
b97bf3fd
PL
688 switch (event) {
689 case TRAFFIC_MSG_EVT:
b97bf3fd
PL
690 case ACTIVATE_MSG:
691 other = l_ptr->owner->active_links[0];
8d64a5ba 692 if (other && link_working_unknown(other))
b97bf3fd 693 break;
b97bf3fd
PL
694 l_ptr->state = WORKING_WORKING;
695 l_ptr->fsm_msg_cnt = 0;
696 link_activate(l_ptr);
247f0f3c 697 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd 698 l_ptr->fsm_msg_cnt++;
c64f7a6a 699 if (l_ptr->owner->working_links == 1)
247f0f3c 700 tipc_link_sync_xmit(l_ptr);
b97bf3fd
PL
701 link_set_timer(l_ptr, cont_intv);
702 break;
703 case RESET_MSG:
b97bf3fd
PL
704 break;
705 case TIMEOUT_EVT:
247f0f3c
YX
706 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
707 0, 0, 0, 0, 0);
b97bf3fd
PL
708 l_ptr->fsm_msg_cnt++;
709 link_set_timer(l_ptr, cont_intv);
b97bf3fd
PL
710 break;
711 default:
2cf8aa19 712 pr_err("%s%u in RR state\n", link_unk_evt, event);
b97bf3fd
PL
713 }
714 break;
715 default:
2cf8aa19 716 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
b97bf3fd
PL
717 }
718}
719
4f1688b2 720/**
9fbfb8b1 721 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
4f1688b2 722 * @link: link to use
a6ca1094
YX
723 * @list: chain of buffers containing message
724 *
3127a020
JPM
725 * Consumes the buffer chain, except when returning -ELINKCONG,
726 * since the caller then may want to make more send attempts.
727 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
728 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
4f1688b2 729 */
7f9f95d9
YX
730int __tipc_link_xmit(struct net *net, struct tipc_link *link,
731 struct sk_buff_head *list)
4f1688b2 732{
a6ca1094 733 struct tipc_msg *msg = buf_msg(skb_peek(list));
05dcc5aa 734 unsigned int maxwin = link->window;
e3eea1eb 735 unsigned int imp = msg_importance(msg);
4f1688b2
JPM
736 uint mtu = link->max_pkt;
737 uint ack = mod(link->next_in_no - 1);
738 uint seqno = link->next_out_no;
739 uint bc_last_in = link->owner->bclink.last_in;
740 struct tipc_media_addr *addr = &link->media_addr;
05dcc5aa
JPM
741 struct sk_buff_head *transmq = &link->transmq;
742 struct sk_buff_head *backlogq = &link->backlogq;
a6ca1094 743 struct sk_buff *skb, *tmp;
4f1688b2 744
1f66d161
JPM
745 /* Match backlog limit against msg importance: */
746 if (unlikely(link->backlog[imp].len >= link->backlog[imp].limit))
3127a020 747 return link_schedule_user(link, list);
4f1688b2 748
05dcc5aa 749 if (unlikely(msg_size(msg) > mtu)) {
a6ca1094 750 __skb_queue_purge(list);
4f1688b2
JPM
751 return -EMSGSIZE;
752 }
05dcc5aa 753 /* Prepare each packet for sending, and add to relevant queue: */
a6ca1094
YX
754 skb_queue_walk_safe(list, skb, tmp) {
755 __skb_unlink(skb, list);
58dc55f2 756 msg = buf_msg(skb);
05dcc5aa
JPM
757 msg_set_seqno(msg, seqno);
758 msg_set_ack(msg, ack);
4f1688b2
JPM
759 msg_set_bcast_ack(msg, bc_last_in);
760
05dcc5aa
JPM
761 if (likely(skb_queue_len(transmq) < maxwin)) {
762 __skb_queue_tail(transmq, skb);
763 tipc_bearer_send(net, link->bearer_id, skb, addr);
764 link->rcv_unacked = 0;
765 seqno++;
766 continue;
767 }
768 if (tipc_msg_bundle(skb_peek_tail(backlogq), skb, mtu)) {
4f1688b2 769 link->stats.sent_bundled++;
4f1688b2 770 continue;
05dcc5aa
JPM
771 }
772 if (tipc_msg_make_bundle(&skb, mtu, link->addr)) {
4f1688b2
JPM
773 link->stats.sent_bundled++;
774 link->stats.sent_bundles++;
1f66d161 775 imp = msg_importance(buf_msg(skb));
4f1688b2 776 }
05dcc5aa 777 __skb_queue_tail(backlogq, skb);
1f66d161 778 link->backlog[imp].len++;
4f1688b2 779 seqno++;
4f1688b2
JPM
780 }
781 link->next_out_no = seqno;
4f1688b2
JPM
782 return 0;
783}
784
a6ca1094
YX
785static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
786{
c637c103 787 skb_queue_head_init(list);
a6ca1094
YX
788 __skb_queue_tail(list, skb);
789}
790
791static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
792{
793 struct sk_buff_head head;
794
795 skb2list(skb, &head);
7f9f95d9 796 return __tipc_link_xmit(link->owner->net, link, &head);
a6ca1094
YX
797}
798
3127a020
JPM
799/* tipc_link_xmit_skb(): send single buffer to destination
800 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
801 * messages, which will not be rejected
802 * The only exception is datagram messages rerouted after secondary
803 * lookup, which are rare and safe to dispose of anyway.
804 * TODO: Return real return value, and let callers use
805 * tipc_wait_for_sendpkt() where applicable
806 */
f2f9800d
YX
807int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
808 u32 selector)
a6ca1094
YX
809{
810 struct sk_buff_head head;
3127a020 811 int rc;
a6ca1094
YX
812
813 skb2list(skb, &head);
3127a020
JPM
814 rc = tipc_link_xmit(net, &head, dnode, selector);
815 if (rc == -ELINKCONG)
816 kfree_skb(skb);
817 return 0;
a6ca1094
YX
818}
819
4f1688b2 820/**
9fbfb8b1 821 * tipc_link_xmit() is the general link level function for message sending
f2f9800d 822 * @net: the applicable net namespace
a6ca1094 823 * @list: chain of buffers containing message
4f1688b2
JPM
824 * @dsz: amount of user data to be sent
825 * @dnode: address of destination node
826 * @selector: a number used for deterministic link selection
827 * Consumes the buffer chain, except when returning -ELINKCONG
828 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
829 */
f2f9800d
YX
830int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
831 u32 selector)
4f1688b2
JPM
832{
833 struct tipc_link *link = NULL;
834 struct tipc_node *node;
835 int rc = -EHOSTUNREACH;
836
f2f9800d 837 node = tipc_node_find(net, dnode);
4f1688b2
JPM
838 if (node) {
839 tipc_node_lock(node);
840 link = node->active_links[selector & 1];
841 if (link)
7f9f95d9 842 rc = __tipc_link_xmit(net, link, list);
4f1688b2 843 tipc_node_unlock(node);
8a0f6ebe 844 tipc_node_put(node);
4f1688b2 845 }
4f1688b2
JPM
846 if (link)
847 return rc;
848
3bd88ee7
EH
849 if (likely(in_own_node(net, dnode))) {
850 tipc_sk_rcv(net, list);
851 return 0;
852 }
4f1688b2 853
c637c103 854 __skb_queue_purge(list);
4f1688b2
JPM
855 return rc;
856}
857
c64f7a6a 858/*
247f0f3c 859 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
c64f7a6a
JM
860 *
861 * Give a newly added peer node the sequence number where it should
862 * start receiving and acking broadcast packets.
863 *
864 * Called with node locked
865 */
25b660c7 866static void tipc_link_sync_xmit(struct tipc_link *link)
c64f7a6a 867{
a6ca1094 868 struct sk_buff *skb;
c64f7a6a
JM
869 struct tipc_msg *msg;
870
a6ca1094
YX
871 skb = tipc_buf_acquire(INT_H_SIZE);
872 if (!skb)
c64f7a6a
JM
873 return;
874
a6ca1094 875 msg = buf_msg(skb);
c5898636 876 tipc_msg_init(link_own_addr(link), msg, BCAST_PROTOCOL, STATE_MSG,
34747539 877 INT_H_SIZE, link->addr);
25b660c7 878 msg_set_last_bcast(msg, link->owner->bclink.acked);
a6ca1094 879 __tipc_link_xmit_skb(link, skb);
c64f7a6a
JM
880}
881
882/*
247f0f3c 883 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
c64f7a6a
JM
884 * Receive the sequence number where we should start receiving and
885 * acking broadcast packets from a newly added peer node, and open
886 * up for reception of such packets.
887 *
888 * Called with node locked
889 */
247f0f3c 890static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
c64f7a6a
JM
891{
892 struct tipc_msg *msg = buf_msg(buf);
893
894 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
895 n->bclink.recv_permitted = true;
896 kfree_skb(buf);
897}
898
c4307285 899/*
47b4c9a8
YX
900 * tipc_link_push_packets - push unsent packets to bearer
901 *
902 * Push out the unsent messages of a link where congestion
903 * has abated. Node is locked.
904 *
905 * Called with node locked
b97bf3fd 906 */
05dcc5aa 907void tipc_link_push_packets(struct tipc_link *link)
b97bf3fd 908{
05dcc5aa 909 struct sk_buff *skb;
47b4c9a8 910 struct tipc_msg *msg;
05dcc5aa 911 unsigned int ack = mod(link->next_in_no - 1);
b97bf3fd 912
05dcc5aa
JPM
913 while (skb_queue_len(&link->transmq) < link->window) {
914 skb = __skb_dequeue(&link->backlogq);
915 if (!skb)
47b4c9a8 916 break;
05dcc5aa 917 msg = buf_msg(skb);
1f66d161 918 link->backlog[msg_importance(msg)].len--;
05dcc5aa
JPM
919 msg_set_ack(msg, ack);
920 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
921 link->rcv_unacked = 0;
922 __skb_queue_tail(&link->transmq, skb);
923 tipc_bearer_send(link->owner->net, link->bearer_id,
924 skb, &link->media_addr);
b97bf3fd 925 }
b97bf3fd
PL
926}
927
3f5a12bd 928void tipc_link_reset_all(struct tipc_node *node)
d356eeba 929{
d356eeba
AS
930 char addr_string[16];
931 u32 i;
932
3f5a12bd 933 tipc_node_lock(node);
d356eeba 934
2cf8aa19 935 pr_warn("Resetting all links to %s\n",
3f5a12bd 936 tipc_addr_string_fill(addr_string, node->addr));
d356eeba
AS
937
938 for (i = 0; i < MAX_BEARERS; i++) {
3f5a12bd
YX
939 if (node->links[i]) {
940 link_print(node->links[i], "Resetting link\n");
941 tipc_link_reset(node->links[i]);
d356eeba
AS
942 }
943 }
944
3f5a12bd 945 tipc_node_unlock(node);
d356eeba
AS
946}
947
a18c4bc3 948static void link_retransmit_failure(struct tipc_link *l_ptr,
ae8509c4 949 struct sk_buff *buf)
d356eeba
AS
950{
951 struct tipc_msg *msg = buf_msg(buf);
1da46568 952 struct net *net = l_ptr->owner->net;
d356eeba 953
2cf8aa19 954 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
d356eeba
AS
955
956 if (l_ptr->addr) {
d356eeba 957 /* Handle failure on standard link */
8d64a5ba 958 link_print(l_ptr, "Resetting link\n");
d356eeba
AS
959 tipc_link_reset(l_ptr);
960
961 } else {
d356eeba 962 /* Handle failure on broadcast link */
6c00055a 963 struct tipc_node *n_ptr;
d356eeba
AS
964 char addr_string[16];
965
2cf8aa19
EH
966 pr_info("Msg seq number: %u, ", msg_seqno(msg));
967 pr_cont("Outstanding acks: %lu\n",
968 (unsigned long) TIPC_SKB_CB(buf)->handle);
617dbeaa 969
1da46568 970 n_ptr = tipc_bclink_retransmit_to(net);
d356eeba 971
c68ca7b7 972 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19 973 pr_info("Broadcast link info for %s\n", addr_string);
389dd9bc
YX
974 pr_info("Reception permitted: %d, Acked: %u\n",
975 n_ptr->bclink.recv_permitted,
2cf8aa19
EH
976 n_ptr->bclink.acked);
977 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
978 n_ptr->bclink.last_in,
979 n_ptr->bclink.oos_state,
980 n_ptr->bclink.last_sent);
d356eeba 981
b952b2be 982 n_ptr->action_flags |= TIPC_BCAST_RESET;
d356eeba
AS
983 l_ptr->stale_count = 0;
984 }
985}
986
58dc55f2 987void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
4323add6 988 u32 retransmits)
b97bf3fd
PL
989{
990 struct tipc_msg *msg;
991
58dc55f2 992 if (!skb)
d356eeba
AS
993 return;
994
58dc55f2 995 msg = buf_msg(skb);
c4307285 996
512137ee
EH
997 /* Detect repeated retransmit failures */
998 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
999 if (++l_ptr->stale_count > 100) {
58dc55f2 1000 link_retransmit_failure(l_ptr, skb);
512137ee 1001 return;
d356eeba
AS
1002 }
1003 } else {
512137ee
EH
1004 l_ptr->last_retransmitted = msg_seqno(msg);
1005 l_ptr->stale_count = 1;
b97bf3fd 1006 }
d356eeba 1007
05dcc5aa
JPM
1008 skb_queue_walk_from(&l_ptr->transmq, skb) {
1009 if (!retransmits)
58dc55f2
YX
1010 break;
1011 msg = buf_msg(skb);
b97bf3fd 1012 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 1013 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
7f9f95d9
YX
1014 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
1015 &l_ptr->media_addr);
3c294cb3
YX
1016 retransmits--;
1017 l_ptr->stats.retransmitted++;
b97bf3fd 1018 }
b97bf3fd
PL
1019}
1020
8b4ed863
JPM
1021/* link_synch(): check if all packets arrived before the synch
1022 * point have been consumed
1023 * Returns true if the parallel links are synched, otherwise false
1024 */
1025static bool link_synch(struct tipc_link *l)
1026{
1027 unsigned int post_synch;
1028 struct tipc_link *pl;
1029
1030 pl = tipc_parallel_link(l);
1031 if (pl == l)
1032 goto synched;
1033
1034 /* Was last pre-synch packet added to input queue ? */
1035 if (less_eq(pl->next_in_no, l->synch_point))
1036 return false;
1037
1038 /* Is it still in the input queue ? */
1039 post_synch = mod(pl->next_in_no - l->synch_point) - 1;
1040 if (skb_queue_len(&pl->inputq) > post_synch)
1041 return false;
1042synched:
1043 l->flags &= ~LINK_SYNCHING;
1044 return true;
1045}
1046
f03273f1
YX
1047static void link_retrieve_defq(struct tipc_link *link,
1048 struct sk_buff_head *list)
b97bf3fd
PL
1049{
1050 u32 seq_no;
1051
05dcc5aa 1052 if (skb_queue_empty(&link->deferdq))
f03273f1
YX
1053 return;
1054
05dcc5aa 1055 seq_no = buf_seqno(skb_peek(&link->deferdq));
f03273f1 1056 if (seq_no == mod(link->next_in_no))
05dcc5aa 1057 skb_queue_splice_tail_init(&link->deferdq, list);
b97bf3fd
PL
1058}
1059
b02b69c8 1060/**
170b3927 1061 * tipc_rcv - process TIPC packets/messages arriving from off-node
f2f9800d 1062 * @net: the applicable net namespace
f03273f1 1063 * @skb: TIPC packet
7a2f7d18 1064 * @b_ptr: pointer to bearer message arrived on
b02b69c8
AS
1065 *
1066 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1067 * structure (i.e. cannot be NULL), but bearer can be inactive.
1068 */
c93d3baa 1069void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
b97bf3fd 1070{
34747539 1071 struct tipc_net *tn = net_generic(net, tipc_net_id);
f03273f1
YX
1072 struct sk_buff_head head;
1073 struct tipc_node *n_ptr;
1074 struct tipc_link *l_ptr;
1075 struct sk_buff *skb1, *tmp;
1076 struct tipc_msg *msg;
1077 u32 seq_no;
1078 u32 ackd;
1079 u32 released;
b97bf3fd 1080
a6ca1094 1081 skb2list(skb, &head);
85035568 1082
f03273f1 1083 while ((skb = __skb_dequeue(&head))) {
85035568 1084 /* Ensure message is well-formed */
cf2157f8 1085 if (unlikely(!tipc_msg_validate(skb)))
3af390e2 1086 goto discard;
b97bf3fd 1087
85035568 1088 /* Handle arrival of a non-unicast link message */
f03273f1 1089 msg = buf_msg(skb);
b97bf3fd 1090 if (unlikely(msg_non_seq(msg))) {
1265a021 1091 if (msg_user(msg) == LINK_CONFIG)
c93d3baa 1092 tipc_disc_rcv(net, skb, b_ptr);
1265a021 1093 else
c93d3baa 1094 tipc_bclink_rcv(net, skb);
b97bf3fd
PL
1095 continue;
1096 }
c4307285 1097
ed33a9c4 1098 /* Discard unicast link messages destined for another node */
26008247 1099 if (unlikely(!msg_short(msg) &&
34747539 1100 (msg_destnode(msg) != tn->own_addr)))
3af390e2 1101 goto discard;
c4307285 1102
5a68d5ee 1103 /* Locate neighboring node that sent message */
f2f9800d 1104 n_ptr = tipc_node_find(net, msg_prevnode(msg));
b97bf3fd 1105 if (unlikely(!n_ptr))
3af390e2 1106 goto discard;
85035568 1107
8a0f6ebe 1108 tipc_node_lock(n_ptr);
b4b56102 1109 /* Locate unicast link endpoint that should handle message */
b4b56102 1110 l_ptr = n_ptr->links[b_ptr->identity];
3af390e2 1111 if (unlikely(!l_ptr))
c637c103 1112 goto unlock;
5a68d5ee 1113
b4b56102 1114 /* Verify that communication with node is currently allowed */
aecb9bb8 1115 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
10f465c4
YX
1116 msg_user(msg) == LINK_PROTOCOL &&
1117 (msg_type(msg) == RESET_MSG ||
1118 msg_type(msg) == ACTIVATE_MSG) &&
1119 !msg_redundant_link(msg))
aecb9bb8 1120 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
10f465c4
YX
1121
1122 if (tipc_node_blocked(n_ptr))
c637c103 1123 goto unlock;
85035568
AS
1124
1125 /* Validate message sequence number info */
85035568
AS
1126 seq_no = msg_seqno(msg);
1127 ackd = msg_ack(msg);
1128
1129 /* Release acked messages */
2cdf3918 1130 if (unlikely(n_ptr->bclink.acked != msg_bcast_ack(msg)))
36559591 1131 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
b97bf3fd 1132
58dc55f2 1133 released = 0;
05dcc5aa
JPM
1134 skb_queue_walk_safe(&l_ptr->transmq, skb1, tmp) {
1135 if (more(buf_seqno(skb1), ackd))
58dc55f2 1136 break;
05dcc5aa 1137 __skb_unlink(skb1, &l_ptr->transmq);
58dc55f2
YX
1138 kfree_skb(skb1);
1139 released = 1;
b97bf3fd 1140 }
85035568
AS
1141
1142 /* Try sending any messages link endpoint has pending */
05dcc5aa 1143 if (unlikely(skb_queue_len(&l_ptr->backlogq)))
47b4c9a8 1144 tipc_link_push_packets(l_ptr);
a5377831 1145
c637c103 1146 if (released && !skb_queue_empty(&l_ptr->wakeupq))
50100a5e 1147 link_prepare_wakeup(l_ptr);
a5377831 1148
a5377831 1149 /* Process the incoming packet */
3af390e2
YX
1150 if (unlikely(!link_working_working(l_ptr))) {
1151 if (msg_user(msg) == LINK_PROTOCOL) {
c5898636 1152 tipc_link_proto_rcv(l_ptr, skb);
f03273f1 1153 link_retrieve_defq(l_ptr, &head);
c637c103
JPM
1154 skb = NULL;
1155 goto unlock;
b97bf3fd 1156 }
3af390e2
YX
1157
1158 /* Traffic message. Conditionally activate link */
1159 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1160
1161 if (link_working_working(l_ptr)) {
1162 /* Re-insert buffer in front of queue */
f03273f1 1163 __skb_queue_head(&head, skb);
c637c103
JPM
1164 skb = NULL;
1165 goto unlock;
3af390e2 1166 }
c637c103 1167 goto unlock;
3af390e2
YX
1168 }
1169
1170 /* Link is now in state WORKING_WORKING */
1171 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
c5898636 1172 link_handle_out_of_seq_msg(l_ptr, skb);
f03273f1 1173 link_retrieve_defq(l_ptr, &head);
c637c103
JPM
1174 skb = NULL;
1175 goto unlock;
b97bf3fd 1176 }
8b4ed863
JPM
1177 /* Synchronize with parallel link if applicable */
1178 if (unlikely((l_ptr->flags & LINK_SYNCHING) && !msg_dup(msg))) {
1179 link_handle_out_of_seq_msg(l_ptr, skb);
1180 if (link_synch(l_ptr))
1181 link_retrieve_defq(l_ptr, &head);
1182 skb = NULL;
1183 goto unlock;
1184 }
3af390e2 1185 l_ptr->next_in_no++;
05dcc5aa 1186 if (unlikely(!skb_queue_empty(&l_ptr->deferdq)))
f03273f1 1187 link_retrieve_defq(l_ptr, &head);
05dcc5aa 1188 if (unlikely(++l_ptr->rcv_unacked >= TIPC_MIN_LINK_WIN)) {
3f53bd8f
EH
1189 l_ptr->stats.sent_acks++;
1190 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1191 }
c637c103
JPM
1192 tipc_link_input(l_ptr, skb);
1193 skb = NULL;
1194unlock:
3af390e2 1195 tipc_node_unlock(n_ptr);
8a0f6ebe 1196 tipc_node_put(n_ptr);
3af390e2 1197discard:
c637c103
JPM
1198 if (unlikely(skb))
1199 kfree_skb(skb);
b97bf3fd 1200 }
b97bf3fd
PL
1201}
1202
c637c103 1203/* tipc_data_input - deliver data and name distr msgs to upper layer
7ae934be 1204 *
c637c103 1205 * Consumes buffer if message is of right type
7ae934be
EH
1206 * Node lock must be held
1207 */
c637c103 1208static bool tipc_data_input(struct tipc_link *link, struct sk_buff *skb)
7ae934be 1209{
c637c103
JPM
1210 struct tipc_node *node = link->owner;
1211 struct tipc_msg *msg = buf_msg(skb);
1212 u32 dport = msg_destport(msg);
7ae934be 1213
7ae934be 1214 switch (msg_user(msg)) {
c637c103
JPM
1215 case TIPC_LOW_IMPORTANCE:
1216 case TIPC_MEDIUM_IMPORTANCE:
1217 case TIPC_HIGH_IMPORTANCE:
1218 case TIPC_CRITICAL_IMPORTANCE:
1219 case CONN_MANAGER:
1220 if (tipc_skb_queue_tail(&link->inputq, skb, dport)) {
1221 node->inputq = &link->inputq;
1222 node->action_flags |= TIPC_MSG_EVT;
7ae934be 1223 }
c637c103 1224 return true;
7ae934be 1225 case NAME_DISTRIBUTOR:
c637c103
JPM
1226 node->bclink.recv_permitted = true;
1227 node->namedq = &link->namedq;
1228 skb_queue_tail(&link->namedq, skb);
1229 if (skb_queue_len(&link->namedq) == 1)
1230 node->action_flags |= TIPC_NAMED_MSG_EVT;
1231 return true;
1232 case MSG_BUNDLER:
dff29b1a 1233 case TUNNEL_PROTOCOL:
c637c103 1234 case MSG_FRAGMENTER:
7ae934be 1235 case BCAST_PROTOCOL:
c637c103 1236 return false;
7ae934be 1237 default:
c637c103
JPM
1238 pr_warn("Dropping received illegal msg type\n");
1239 kfree_skb(skb);
1240 return false;
1241 };
7ae934be 1242}
c637c103
JPM
1243
1244/* tipc_link_input - process packet that has passed link protocol check
1245 *
1246 * Consumes buffer
1247 * Node lock must be held
7ae934be 1248 */
c637c103 1249static void tipc_link_input(struct tipc_link *link, struct sk_buff *skb)
7ae934be 1250{
c637c103
JPM
1251 struct tipc_node *node = link->owner;
1252 struct tipc_msg *msg = buf_msg(skb);
1253 struct sk_buff *iskb;
1254 int pos = 0;
1255
1256 if (likely(tipc_data_input(link, skb)))
1257 return;
7ae934be
EH
1258
1259 switch (msg_user(msg)) {
dff29b1a 1260 case TUNNEL_PROTOCOL:
8b4ed863
JPM
1261 if (msg_dup(msg)) {
1262 link->flags |= LINK_SYNCHING;
1263 link->synch_point = msg_seqno(msg_get_wrapped(msg));
2da71425
JPM
1264 kfree_skb(skb);
1265 break;
8b4ed863 1266 }
dff29b1a 1267 if (!tipc_link_failover_rcv(link, &skb))
c637c103
JPM
1268 break;
1269 if (msg_user(buf_msg(skb)) != MSG_BUNDLER) {
1270 tipc_data_input(link, skb);
1271 break;
1272 }
1273 case MSG_BUNDLER:
1274 link->stats.recv_bundles++;
1275 link->stats.recv_bundled += msg_msgcnt(msg);
1276
1277 while (tipc_msg_extract(skb, &iskb, &pos))
1278 tipc_data_input(link, iskb);
7ae934be 1279 break;
c637c103
JPM
1280 case MSG_FRAGMENTER:
1281 link->stats.recv_fragments++;
1282 if (tipc_buf_append(&link->reasm_buf, &skb)) {
1283 link->stats.recv_fragmented++;
1284 tipc_data_input(link, skb);
1285 } else if (!link->reasm_buf) {
1286 tipc_link_reset(link);
1287 }
7ae934be 1288 break;
c637c103
JPM
1289 case BCAST_PROTOCOL:
1290 tipc_link_sync_rcv(node, skb);
7ae934be
EH
1291 break;
1292 default:
c637c103
JPM
1293 break;
1294 };
7ae934be
EH
1295}
1296
2c53040f 1297/**
8809b255
AS
1298 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1299 *
1300 * Returns increase in queue length (i.e. 0 or 1)
b97bf3fd 1301 */
bc6fecd4 1302u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
b97bf3fd 1303{
bc6fecd4
YX
1304 struct sk_buff *skb1;
1305 u32 seq_no = buf_seqno(skb);
b97bf3fd
PL
1306
1307 /* Empty queue ? */
bc6fecd4
YX
1308 if (skb_queue_empty(list)) {
1309 __skb_queue_tail(list, skb);
b97bf3fd
PL
1310 return 1;
1311 }
1312
1313 /* Last ? */
bc6fecd4
YX
1314 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1315 __skb_queue_tail(list, skb);
b97bf3fd
PL
1316 return 1;
1317 }
1318
8809b255 1319 /* Locate insertion point in queue, then insert; discard if duplicate */
bc6fecd4
YX
1320 skb_queue_walk(list, skb1) {
1321 u32 curr_seqno = buf_seqno(skb1);
b97bf3fd 1322
8809b255 1323 if (seq_no == curr_seqno) {
bc6fecd4 1324 kfree_skb(skb);
8809b255 1325 return 0;
b97bf3fd 1326 }
8809b255
AS
1327
1328 if (less(seq_no, curr_seqno))
b97bf3fd 1329 break;
8809b255 1330 }
b97bf3fd 1331
bc6fecd4 1332 __skb_queue_before(list, skb1, skb);
8809b255 1333 return 1;
b97bf3fd
PL
1334}
1335
8809b255 1336/*
b97bf3fd
PL
1337 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1338 */
c5898636 1339static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
b97bf3fd
PL
1340 struct sk_buff *buf)
1341{
f905730c 1342 u32 seq_no = buf_seqno(buf);
b97bf3fd
PL
1343
1344 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
c5898636 1345 tipc_link_proto_rcv(l_ptr, buf);
b97bf3fd
PL
1346 return;
1347 }
1348
b97bf3fd 1349 /* Record OOS packet arrival (force mismatch on next timeout) */
b97bf3fd
PL
1350 l_ptr->checkpoint--;
1351
c4307285 1352 /*
b97bf3fd
PL
1353 * Discard packet if a duplicate; otherwise add it to deferred queue
1354 * and notify peer of gap as per protocol specification
1355 */
b97bf3fd
PL
1356 if (less(seq_no, mod(l_ptr->next_in_no))) {
1357 l_ptr->stats.duplicates++;
5f6d9123 1358 kfree_skb(buf);
b97bf3fd
PL
1359 return;
1360 }
1361
05dcc5aa 1362 if (tipc_link_defer_pkt(&l_ptr->deferdq, buf)) {
b97bf3fd 1363 l_ptr->stats.deferred_recv++;
05dcc5aa 1364 if ((skb_queue_len(&l_ptr->deferdq) % TIPC_MIN_LINK_WIN) == 1)
247f0f3c 1365 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
bc6fecd4 1366 } else {
b97bf3fd 1367 l_ptr->stats.duplicates++;
bc6fecd4 1368 }
b97bf3fd
PL
1369}
1370
1371/*
1372 * Send protocol message to the other endpoint.
1373 */
247f0f3c
YX
1374void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1375 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
b97bf3fd 1376{
1fc54d8f 1377 struct sk_buff *buf = NULL;
b97bf3fd 1378 struct tipc_msg *msg = l_ptr->pmsg;
c4307285 1379 u32 msg_size = sizeof(l_ptr->proto_msg);
75f0aa49 1380 int r_flag;
b97bf3fd 1381
dff29b1a
JPM
1382 /* Don't send protocol message during link failover */
1383 if (l_ptr->flags & LINK_FAILINGOVER)
b97bf3fd 1384 return;
b4b56102
AS
1385
1386 /* Abort non-RESET send if communication with node is prohibited */
10f465c4 1387 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
b4b56102
AS
1388 return;
1389
92d2c905 1390 /* Create protocol message with "out-of-sequence" sequence number */
b97bf3fd 1391 msg_set_type(msg, msg_typ);
7a2f7d18 1392 msg_set_net_plane(msg, l_ptr->net_plane);
7a54d4a9 1393 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1da46568 1394 msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
b97bf3fd
PL
1395
1396 if (msg_typ == STATE_MSG) {
1397 u32 next_sent = mod(l_ptr->next_out_no);
1398
4323add6 1399 if (!tipc_link_is_up(l_ptr))
b97bf3fd 1400 return;
05dcc5aa
JPM
1401 if (skb_queue_len(&l_ptr->backlogq))
1402 next_sent = buf_seqno(skb_peek(&l_ptr->backlogq));
b97bf3fd 1403 msg_set_next_sent(msg, next_sent);
05dcc5aa
JPM
1404 if (!skb_queue_empty(&l_ptr->deferdq)) {
1405 u32 rec = buf_seqno(skb_peek(&l_ptr->deferdq));
b97bf3fd
PL
1406 gap = mod(rec - mod(l_ptr->next_in_no));
1407 }
1408 msg_set_seq_gap(msg, gap);
1409 if (gap)
1410 l_ptr->stats.sent_nacks++;
1411 msg_set_link_tolerance(msg, tolerance);
1412 msg_set_linkprio(msg, priority);
1413 msg_set_max_pkt(msg, ack_mtu);
1414 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1415 msg_set_probe(msg, probe_msg != 0);
c4307285 1416 if (probe_msg) {
b97bf3fd
PL
1417 u32 mtu = l_ptr->max_pkt;
1418
c4307285 1419 if ((mtu < l_ptr->max_pkt_target) &&
b97bf3fd
PL
1420 link_working_working(l_ptr) &&
1421 l_ptr->fsm_msg_cnt) {
1422 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285
YH
1423 if (l_ptr->max_pkt_probes == 10) {
1424 l_ptr->max_pkt_target = (msg_size - 4);
1425 l_ptr->max_pkt_probes = 0;
b97bf3fd 1426 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285 1427 }
b97bf3fd 1428 l_ptr->max_pkt_probes++;
c4307285 1429 }
b97bf3fd
PL
1430
1431 l_ptr->stats.sent_probes++;
c4307285 1432 }
b97bf3fd
PL
1433 l_ptr->stats.sent_states++;
1434 } else { /* RESET_MSG or ACTIVATE_MSG */
dff29b1a 1435 msg_set_ack(msg, mod(l_ptr->failover_checkpt - 1));
b97bf3fd
PL
1436 msg_set_seq_gap(msg, 0);
1437 msg_set_next_sent(msg, 1);
f23d9bf2 1438 msg_set_probe(msg, 0);
b97bf3fd
PL
1439 msg_set_link_tolerance(msg, l_ptr->tolerance);
1440 msg_set_linkprio(msg, l_ptr->priority);
1441 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1442 }
1443
75f0aa49
AS
1444 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1445 msg_set_redundant_link(msg, r_flag);
b97bf3fd 1446 msg_set_linkprio(msg, l_ptr->priority);
92d2c905 1447 msg_set_size(msg, msg_size);
b97bf3fd
PL
1448
1449 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1450
31e3c3f6 1451 buf = tipc_buf_acquire(msg_size);
b97bf3fd
PL
1452 if (!buf)
1453 return;
1454
27d7ff46 1455 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
796c75d0 1456 buf->priority = TC_PRIO_CONTROL;
7f9f95d9
YX
1457 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
1458 &l_ptr->media_addr);
05dcc5aa 1459 l_ptr->rcv_unacked = 0;
5f6d9123 1460 kfree_skb(buf);
b97bf3fd
PL
1461}
1462
1463/*
1464 * Receive protocol message :
c4307285
YH
1465 * Note that network plane id propagates through the network, and may
1466 * change at any time. The node with lowest address rules
b97bf3fd 1467 */
c5898636 1468static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
c93d3baa 1469 struct sk_buff *buf)
b97bf3fd
PL
1470{
1471 u32 rec_gap = 0;
1472 u32 max_pkt_info;
c4307285 1473 u32 max_pkt_ack;
b97bf3fd
PL
1474 u32 msg_tol;
1475 struct tipc_msg *msg = buf_msg(buf);
1476
dff29b1a 1477 if (l_ptr->flags & LINK_FAILINGOVER)
b97bf3fd
PL
1478 goto exit;
1479
7a2f7d18 1480 if (l_ptr->net_plane != msg_net_plane(msg))
c5898636 1481 if (link_own_addr(l_ptr) > msg_prevnode(msg))
7a2f7d18 1482 l_ptr->net_plane = msg_net_plane(msg);
b97bf3fd 1483
b97bf3fd 1484 switch (msg_type(msg)) {
c4307285 1485
b97bf3fd 1486 case RESET_MSG:
a686e685
AS
1487 if (!link_working_unknown(l_ptr) &&
1488 (l_ptr->peer_session != INVALID_SESSION)) {
641c218d
AS
1489 if (less_eq(msg_session(msg), l_ptr->peer_session))
1490 break; /* duplicate or old reset: ignore */
b97bf3fd 1491 }
b4b56102
AS
1492
1493 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1494 link_working_unknown(l_ptr))) {
1495 /*
1496 * peer has lost contact -- don't allow peer's links
1497 * to reactivate before we recognize loss & clean up
1498 */
ca9cf06a 1499 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
b4b56102
AS
1500 }
1501
47361c87
AS
1502 link_state_event(l_ptr, RESET_MSG);
1503
b97bf3fd
PL
1504 /* fall thru' */
1505 case ACTIVATE_MSG:
1506 /* Update link settings according other endpoint's values */
b97bf3fd
PL
1507 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1508
2db9983a
AS
1509 msg_tol = msg_link_tolerance(msg);
1510 if (msg_tol > l_ptr->tolerance)
b97bf3fd
PL
1511 link_set_supervision_props(l_ptr, msg_tol);
1512
1513 if (msg_linkprio(msg) > l_ptr->priority)
1514 l_ptr->priority = msg_linkprio(msg);
1515
1516 max_pkt_info = msg_max_pkt(msg);
c4307285 1517 if (max_pkt_info) {
b97bf3fd
PL
1518 if (max_pkt_info < l_ptr->max_pkt_target)
1519 l_ptr->max_pkt_target = max_pkt_info;
1520 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1521 l_ptr->max_pkt = l_ptr->max_pkt_target;
1522 } else {
c4307285 1523 l_ptr->max_pkt = l_ptr->max_pkt_target;
b97bf3fd 1524 }
b97bf3fd 1525
4d75313c 1526 /* Synchronize broadcast link info, if not done previously */
7a54d4a9
AS
1527 if (!tipc_node_is_up(l_ptr->owner)) {
1528 l_ptr->owner->bclink.last_sent =
1529 l_ptr->owner->bclink.last_in =
1530 msg_last_bcast(msg);
1531 l_ptr->owner->bclink.oos_state = 0;
1532 }
4d75313c 1533
b97bf3fd
PL
1534 l_ptr->peer_session = msg_session(msg);
1535 l_ptr->peer_bearer_id = msg_bearer_id(msg);
47361c87
AS
1536
1537 if (msg_type(msg) == ACTIVATE_MSG)
1538 link_state_event(l_ptr, ACTIVATE_MSG);
b97bf3fd
PL
1539 break;
1540 case STATE_MSG:
1541
2db9983a
AS
1542 msg_tol = msg_link_tolerance(msg);
1543 if (msg_tol)
b97bf3fd 1544 link_set_supervision_props(l_ptr, msg_tol);
c4307285
YH
1545
1546 if (msg_linkprio(msg) &&
b97bf3fd 1547 (msg_linkprio(msg) != l_ptr->priority)) {
3fa9cacd
EH
1548 pr_debug("%s<%s>, priority change %u->%u\n",
1549 link_rst_msg, l_ptr->name,
1550 l_ptr->priority, msg_linkprio(msg));
b97bf3fd 1551 l_ptr->priority = msg_linkprio(msg);
4323add6 1552 tipc_link_reset(l_ptr); /* Enforce change to take effect */
b97bf3fd
PL
1553 break;
1554 }
ec37dcd3
JPM
1555
1556 /* Record reception; force mismatch at next timeout: */
1557 l_ptr->checkpoint--;
1558
b97bf3fd
PL
1559 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1560 l_ptr->stats.recv_states++;
1561 if (link_reset_unknown(l_ptr))
1562 break;
1563
1564 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
c4307285 1565 rec_gap = mod(msg_next_sent(msg) -
b97bf3fd
PL
1566 mod(l_ptr->next_in_no));
1567 }
1568
1569 max_pkt_ack = msg_max_pkt(msg);
c4307285 1570 if (max_pkt_ack > l_ptr->max_pkt) {
c4307285
YH
1571 l_ptr->max_pkt = max_pkt_ack;
1572 l_ptr->max_pkt_probes = 0;
1573 }
b97bf3fd
PL
1574
1575 max_pkt_ack = 0;
c4307285 1576 if (msg_probe(msg)) {
b97bf3fd 1577 l_ptr->stats.recv_probes++;
a016892c 1578 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
c4307285 1579 max_pkt_ack = msg_size(msg);
c4307285 1580 }
b97bf3fd
PL
1581
1582 /* Protocol message before retransmits, reduce loss risk */
389dd9bc 1583 if (l_ptr->owner->bclink.recv_permitted)
c5898636 1584 tipc_bclink_update_link_state(l_ptr->owner,
7a54d4a9 1585 msg_last_bcast(msg));
b97bf3fd
PL
1586
1587 if (rec_gap || (msg_probe(msg))) {
247f0f3c
YX
1588 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1589 0, max_pkt_ack);
b97bf3fd
PL
1590 }
1591 if (msg_seq_gap(msg)) {
b97bf3fd 1592 l_ptr->stats.recv_nacks++;
05dcc5aa 1593 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->transmq),
4323add6 1594 msg_seq_gap(msg));
b97bf3fd
PL
1595 }
1596 break;
b97bf3fd
PL
1597 }
1598exit:
5f6d9123 1599 kfree_skb(buf);
b97bf3fd
PL
1600}
1601
1602
170b3927
JPM
1603/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1604 * a different bearer. Owner node is locked.
b97bf3fd 1605 */
170b3927
JPM
1606static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1607 struct tipc_msg *tunnel_hdr,
1608 struct tipc_msg *msg,
1609 u32 selector)
b97bf3fd 1610{
a18c4bc3 1611 struct tipc_link *tunnel;
a6ca1094 1612 struct sk_buff *skb;
b97bf3fd
PL
1613 u32 length = msg_size(msg);
1614
1615 tunnel = l_ptr->owner->active_links[selector & 1];
5392d646 1616 if (!tipc_link_is_up(tunnel)) {
2cf8aa19 1617 pr_warn("%stunnel link no longer available\n", link_co_err);
b97bf3fd 1618 return;
5392d646 1619 }
b97bf3fd 1620 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
a6ca1094
YX
1621 skb = tipc_buf_acquire(length + INT_H_SIZE);
1622 if (!skb) {
2cf8aa19 1623 pr_warn("%sunable to send tunnel msg\n", link_co_err);
b97bf3fd 1624 return;
5392d646 1625 }
a6ca1094
YX
1626 skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
1627 skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
1628 __tipc_link_xmit_skb(tunnel, skb);
b97bf3fd
PL
1629}
1630
1631
170b3927
JPM
1632/* tipc_link_failover_send_queue(): A link has gone down, but a second
1633 * link is still active. We can do failover. Tunnel the failing link's
1634 * whole send queue via the remaining link. This way, we don't lose
1635 * any packets, and sequence order is preserved for subsequent traffic
1636 * sent over the remaining link. Owner node is locked.
b97bf3fd 1637 */
170b3927 1638void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
b97bf3fd 1639{
05dcc5aa 1640 int msgcount;
a18c4bc3 1641 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
b97bf3fd 1642 struct tipc_msg tunnel_hdr;
58dc55f2 1643 struct sk_buff *skb;
5392d646 1644 int split_bundles;
b97bf3fd
PL
1645
1646 if (!tunnel)
1647 return;
1648
dff29b1a
JPM
1649 tipc_msg_init(link_own_addr(l_ptr), &tunnel_hdr, TUNNEL_PROTOCOL,
1650 FAILOVER_MSG, INT_H_SIZE, l_ptr->addr);
05dcc5aa 1651 skb_queue_splice_tail_init(&l_ptr->backlogq, &l_ptr->transmq);
1f66d161 1652 tipc_link_purge_backlog(l_ptr);
05dcc5aa 1653 msgcount = skb_queue_len(&l_ptr->transmq);
b97bf3fd
PL
1654 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1655 msg_set_msgcnt(&tunnel_hdr, msgcount);
f131072c 1656
05dcc5aa 1657 if (skb_queue_empty(&l_ptr->transmq)) {
58dc55f2
YX
1658 skb = tipc_buf_acquire(INT_H_SIZE);
1659 if (skb) {
1660 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
b97bf3fd 1661 msg_set_size(&tunnel_hdr, INT_H_SIZE);
a6ca1094 1662 __tipc_link_xmit_skb(tunnel, skb);
b97bf3fd 1663 } else {
2cf8aa19
EH
1664 pr_warn("%sunable to send changeover msg\n",
1665 link_co_err);
b97bf3fd
PL
1666 }
1667 return;
1668 }
f131072c 1669
c4307285 1670 split_bundles = (l_ptr->owner->active_links[0] !=
5392d646
AS
1671 l_ptr->owner->active_links[1]);
1672
05dcc5aa 1673 skb_queue_walk(&l_ptr->transmq, skb) {
58dc55f2 1674 struct tipc_msg *msg = buf_msg(skb);
b97bf3fd
PL
1675
1676 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
b97bf3fd 1677 struct tipc_msg *m = msg_get_wrapped(msg);
0e65967e 1678 unchar *pos = (unchar *)m;
b97bf3fd 1679
d788d805 1680 msgcount = msg_msgcnt(msg);
b97bf3fd 1681 while (msgcount--) {
0e65967e 1682 msg_set_seqno(m, msg_seqno(msg));
170b3927
JPM
1683 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1684 msg_link_selector(m));
b97bf3fd
PL
1685 pos += align(msg_size(m));
1686 m = (struct tipc_msg *)pos;
1687 }
1688 } else {
170b3927
JPM
1689 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1690 msg_link_selector(msg));
b97bf3fd 1691 }
b97bf3fd
PL
1692 }
1693}
1694
247f0f3c 1695/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
170b3927
JPM
1696 * duplicate of the first link's send queue via the new link. This way, we
1697 * are guaranteed that currently queued packets from a socket are delivered
1698 * before future traffic from the same socket, even if this is using the
1699 * new link. The last arriving copy of each duplicate packet is dropped at
1700 * the receiving end by the regular protocol check, so packet cardinality
1701 * and sequence order is preserved per sender/receiver socket pair.
1702 * Owner node is locked.
1703 */
05dcc5aa
JPM
1704void tipc_link_dup_queue_xmit(struct tipc_link *link,
1705 struct tipc_link *tnl)
b97bf3fd 1706{
58dc55f2 1707 struct sk_buff *skb;
05dcc5aa
JPM
1708 struct tipc_msg tnl_hdr;
1709 struct sk_buff_head *queue = &link->transmq;
1710 int mcnt;
1711
dff29b1a
JPM
1712 tipc_msg_init(link_own_addr(link), &tnl_hdr, TUNNEL_PROTOCOL,
1713 SYNCH_MSG, INT_H_SIZE, link->addr);
05dcc5aa
JPM
1714 mcnt = skb_queue_len(&link->transmq) + skb_queue_len(&link->backlogq);
1715 msg_set_msgcnt(&tnl_hdr, mcnt);
1716 msg_set_bearer_id(&tnl_hdr, link->peer_bearer_id);
1717
1718tunnel_queue:
1719 skb_queue_walk(queue, skb) {
58dc55f2
YX
1720 struct sk_buff *outskb;
1721 struct tipc_msg *msg = buf_msg(skb);
05dcc5aa 1722 u32 len = msg_size(msg);
b97bf3fd 1723
05dcc5aa
JPM
1724 msg_set_ack(msg, mod(link->next_in_no - 1));
1725 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
1726 msg_set_size(&tnl_hdr, len + INT_H_SIZE);
1727 outskb = tipc_buf_acquire(len + INT_H_SIZE);
58dc55f2 1728 if (outskb == NULL) {
2cf8aa19
EH
1729 pr_warn("%sunable to send duplicate msg\n",
1730 link_co_err);
b97bf3fd
PL
1731 return;
1732 }
05dcc5aa
JPM
1733 skb_copy_to_linear_data(outskb, &tnl_hdr, INT_H_SIZE);
1734 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE,
1735 skb->data, len);
1736 __tipc_link_xmit_skb(tnl, outskb);
1737 if (!tipc_link_is_up(link))
b97bf3fd 1738 return;
b97bf3fd 1739 }
05dcc5aa
JPM
1740 if (queue == &link->backlogq)
1741 return;
1742 queue = &link->backlogq;
1743 goto tunnel_queue;
b97bf3fd
PL
1744}
1745
dff29b1a 1746/* tipc_link_failover_rcv(): Receive a tunnelled FAILOVER_MSG packet
f006c9c7
JPM
1747 * Owner node is locked.
1748 */
dff29b1a 1749static bool tipc_link_failover_rcv(struct tipc_link *link,
2da71425 1750 struct sk_buff **skb)
f006c9c7 1751{
2da71425
JPM
1752 struct tipc_msg *msg = buf_msg(*skb);
1753 struct sk_buff *iskb = NULL;
dff29b1a 1754 struct tipc_link *pl = NULL;
2da71425 1755 int bearer_id = msg_bearer_id(msg);
c1336ee4 1756 int pos = 0;
f006c9c7 1757
dff29b1a 1758 if (msg_type(msg) != FAILOVER_MSG) {
2da71425
JPM
1759 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
1760 goto exit;
f006c9c7 1761 }
2da71425
JPM
1762 if (bearer_id >= MAX_BEARERS)
1763 goto exit;
dff29b1a
JPM
1764
1765 if (bearer_id == link->bearer_id)
2da71425 1766 goto exit;
b97bf3fd 1767
dff29b1a
JPM
1768 pl = link->owner->links[bearer_id];
1769 if (pl && tipc_link_is_up(pl))
1770 tipc_link_reset(pl);
1771
1772 if (link->failover_pkts == FIRST_FAILOVER)
1773 link->failover_pkts = msg_msgcnt(msg);
1e9d47a9 1774
2da71425 1775 /* Should we expect an inner packet? */
dff29b1a 1776 if (!link->failover_pkts)
cb4b102f 1777 goto exit;
1dab3d5a 1778
2da71425
JPM
1779 if (!tipc_msg_extract(*skb, &iskb, &pos)) {
1780 pr_warn("%sno inner failover pkt\n", link_co_err);
1781 *skb = NULL;
b97bf3fd 1782 goto exit;
2da71425 1783 }
dff29b1a 1784 link->failover_pkts--;
2da71425 1785 *skb = NULL;
b97bf3fd 1786
dff29b1a
JPM
1787 /* Was this packet already delivered? */
1788 if (less(buf_seqno(iskb), link->failover_checkpt)) {
2da71425
JPM
1789 kfree_skb(iskb);
1790 iskb = NULL;
1791 goto exit;
1792 }
1793 if (msg_user(buf_msg(iskb)) == MSG_FRAGMENTER) {
1794 link->stats.recv_fragments++;
dff29b1a 1795 tipc_buf_append(&link->failover_skb, &iskb);
2da71425 1796 }
b97bf3fd 1797exit:
dff29b1a
JPM
1798 if (!link->failover_pkts && pl)
1799 pl->flags &= ~LINK_FAILINGOVER;
2da71425
JPM
1800 kfree_skb(*skb);
1801 *skb = iskb;
1802 return *skb;
b97bf3fd
PL
1803}
1804
2f55c437 1805static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
b97bf3fd 1806{
2f55c437
YX
1807 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
1808
1809 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
5413b4c6
AS
1810 return;
1811
2f55c437
YX
1812 l_ptr->tolerance = tol;
1813 l_ptr->cont_intv = msecs_to_jiffies(intv);
1814 l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
b97bf3fd
PL
1815}
1816
e3eea1eb 1817void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
b97bf3fd 1818{
e3eea1eb
JPM
1819 int max_bulk = TIPC_MAX_PUBLICATIONS / (l->max_pkt / ITEM_SIZE);
1820
1821 l->window = win;
1f66d161
JPM
1822 l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
1823 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
1824 l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
1825 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
1826 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
b97bf3fd
PL
1827}
1828
e099e86c 1829/* tipc_link_find_owner - locate owner node of link by link's name
f2f9800d 1830 * @net: the applicable net namespace
e099e86c
JPM
1831 * @name: pointer to link name string
1832 * @bearer_id: pointer to index in 'node->links' array where the link was found.
c4307285 1833 *
e099e86c 1834 * Returns pointer to node owning the link, or 0 if no matching link is found.
b97bf3fd 1835 */
f2f9800d
YX
1836static struct tipc_node *tipc_link_find_owner(struct net *net,
1837 const char *link_name,
e099e86c 1838 unsigned int *bearer_id)
b97bf3fd 1839{
f2f9800d 1840 struct tipc_net *tn = net_generic(net, tipc_net_id);
a18c4bc3 1841 struct tipc_link *l_ptr;
bbfbe47c 1842 struct tipc_node *n_ptr;
886eaa1f 1843 struct tipc_node *found_node = NULL;
bbfbe47c 1844 int i;
b97bf3fd 1845
e099e86c 1846 *bearer_id = 0;
6c7a762e 1847 rcu_read_lock();
f2f9800d 1848 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
a11607f5 1849 tipc_node_lock(n_ptr);
bbfbe47c
EH
1850 for (i = 0; i < MAX_BEARERS; i++) {
1851 l_ptr = n_ptr->links[i];
e099e86c
JPM
1852 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1853 *bearer_id = i;
1854 found_node = n_ptr;
1855 break;
1856 }
bbfbe47c 1857 }
a11607f5 1858 tipc_node_unlock(n_ptr);
e099e86c
JPM
1859 if (found_node)
1860 break;
bbfbe47c 1861 }
6c7a762e
YX
1862 rcu_read_unlock();
1863
e099e86c 1864 return found_node;
b97bf3fd
PL
1865}
1866
b97bf3fd
PL
1867/**
1868 * link_reset_statistics - reset link statistics
1869 * @l_ptr: pointer to link
1870 */
a18c4bc3 1871static void link_reset_statistics(struct tipc_link *l_ptr)
b97bf3fd
PL
1872{
1873 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
1874 l_ptr->stats.sent_info = l_ptr->next_out_no;
1875 l_ptr->stats.recv_info = l_ptr->next_in_no;
1876}
1877
a18c4bc3 1878static void link_print(struct tipc_link *l_ptr, const char *str)
b97bf3fd 1879{
7f9f95d9 1880 struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
7a2f7d18
YX
1881 struct tipc_bearer *b_ptr;
1882
1883 rcu_read_lock();
7f9f95d9 1884 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
7a2f7d18
YX
1885 if (b_ptr)
1886 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
1887 rcu_read_unlock();
8d64a5ba 1888
b97bf3fd 1889 if (link_working_unknown(l_ptr))
5deedde9 1890 pr_cont(":WU\n");
8d64a5ba 1891 else if (link_reset_reset(l_ptr))
5deedde9 1892 pr_cont(":RR\n");
8d64a5ba 1893 else if (link_reset_unknown(l_ptr))
5deedde9 1894 pr_cont(":RU\n");
8d64a5ba 1895 else if (link_working_working(l_ptr))
5deedde9
PG
1896 pr_cont(":WW\n");
1897 else
1898 pr_cont("\n");
b97bf3fd 1899}
0655f6a8
RA
1900
1901/* Parse and validate nested (link) properties valid for media, bearer and link
1902 */
1903int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
1904{
1905 int err;
1906
1907 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
1908 tipc_nl_prop_policy);
1909 if (err)
1910 return err;
1911
1912 if (props[TIPC_NLA_PROP_PRIO]) {
1913 u32 prio;
1914
1915 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1916 if (prio > TIPC_MAX_LINK_PRI)
1917 return -EINVAL;
1918 }
1919
1920 if (props[TIPC_NLA_PROP_TOL]) {
1921 u32 tol;
1922
1923 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1924 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
1925 return -EINVAL;
1926 }
1927
1928 if (props[TIPC_NLA_PROP_WIN]) {
1929 u32 win;
1930
1931 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1932 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
1933 return -EINVAL;
1934 }
1935
1936 return 0;
1937}
7be57fc6 1938
f96ce7a2
RA
1939int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
1940{
1941 int err;
1942 int res = 0;
1943 int bearer_id;
1944 char *name;
1945 struct tipc_link *link;
1946 struct tipc_node *node;
1947 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
37e2d484 1948 struct net *net = sock_net(skb->sk);
f96ce7a2
RA
1949
1950 if (!info->attrs[TIPC_NLA_LINK])
1951 return -EINVAL;
1952
1953 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1954 info->attrs[TIPC_NLA_LINK],
1955 tipc_nl_link_policy);
1956 if (err)
1957 return err;
1958
1959 if (!attrs[TIPC_NLA_LINK_NAME])
1960 return -EINVAL;
1961
1962 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1963
f2f9800d 1964 node = tipc_link_find_owner(net, name, &bearer_id);
f96ce7a2
RA
1965 if (!node)
1966 return -EINVAL;
1967
1968 tipc_node_lock(node);
1969
1970 link = node->links[bearer_id];
1971 if (!link) {
1972 res = -EINVAL;
1973 goto out;
1974 }
1975
1976 if (attrs[TIPC_NLA_LINK_PROP]) {
1977 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1978
1979 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1980 props);
1981 if (err) {
1982 res = err;
1983 goto out;
1984 }
1985
1986 if (props[TIPC_NLA_PROP_TOL]) {
1987 u32 tol;
1988
1989 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1990 link_set_supervision_props(link, tol);
1991 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
1992 }
1993 if (props[TIPC_NLA_PROP_PRIO]) {
1994 u32 prio;
1995
1996 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1997 link->priority = prio;
1998 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
1999 }
2000 if (props[TIPC_NLA_PROP_WIN]) {
2001 u32 win;
2002
2003 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2004 tipc_link_set_queue_limits(link, win);
2005 }
2006 }
2007
2008out:
2009 tipc_node_unlock(node);
2010
2011 return res;
2012}
d8182804
RA
2013
2014static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
7be57fc6
RA
2015{
2016 int i;
2017 struct nlattr *stats;
2018
2019 struct nla_map {
2020 u32 key;
2021 u32 val;
2022 };
2023
2024 struct nla_map map[] = {
2025 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2026 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2027 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2028 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2029 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2030 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2031 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2032 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2033 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2034 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2035 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2036 s->msg_length_counts : 1},
2037 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2038 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2039 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2040 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2041 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2042 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2043 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2044 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2045 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2046 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2047 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2048 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2049 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2050 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2051 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2052 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2053 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2054 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2055 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2056 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2057 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2058 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2059 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2060 };
2061
2062 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2063 if (!stats)
2064 return -EMSGSIZE;
2065
2066 for (i = 0; i < ARRAY_SIZE(map); i++)
2067 if (nla_put_u32(skb, map[i].key, map[i].val))
2068 goto msg_full;
2069
2070 nla_nest_end(skb, stats);
2071
2072 return 0;
2073msg_full:
2074 nla_nest_cancel(skb, stats);
2075
2076 return -EMSGSIZE;
2077}
2078
2079/* Caller should hold appropriate locks to protect the link */
34747539
YX
2080static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2081 struct tipc_link *link)
7be57fc6
RA
2082{
2083 int err;
2084 void *hdr;
2085 struct nlattr *attrs;
2086 struct nlattr *prop;
34747539 2087 struct tipc_net *tn = net_generic(net, tipc_net_id);
7be57fc6 2088
bfb3e5dd 2089 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
7be57fc6
RA
2090 NLM_F_MULTI, TIPC_NL_LINK_GET);
2091 if (!hdr)
2092 return -EMSGSIZE;
2093
2094 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2095 if (!attrs)
2096 goto msg_full;
2097
2098 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2099 goto attr_msg_full;
2100 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
34747539 2101 tipc_cluster_mask(tn->own_addr)))
7be57fc6
RA
2102 goto attr_msg_full;
2103 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2104 goto attr_msg_full;
2105 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2106 goto attr_msg_full;
2107 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2108 goto attr_msg_full;
2109
2110 if (tipc_link_is_up(link))
2111 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2112 goto attr_msg_full;
2113 if (tipc_link_is_active(link))
2114 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2115 goto attr_msg_full;
2116
2117 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2118 if (!prop)
2119 goto attr_msg_full;
2120 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2121 goto prop_msg_full;
2122 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2123 goto prop_msg_full;
2124 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
1f66d161 2125 link->window))
7be57fc6
RA
2126 goto prop_msg_full;
2127 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2128 goto prop_msg_full;
2129 nla_nest_end(msg->skb, prop);
2130
2131 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2132 if (err)
2133 goto attr_msg_full;
2134
2135 nla_nest_end(msg->skb, attrs);
2136 genlmsg_end(msg->skb, hdr);
2137
2138 return 0;
2139
2140prop_msg_full:
2141 nla_nest_cancel(msg->skb, prop);
2142attr_msg_full:
2143 nla_nest_cancel(msg->skb, attrs);
2144msg_full:
2145 genlmsg_cancel(msg->skb, hdr);
2146
2147 return -EMSGSIZE;
2148}
2149
2150/* Caller should hold node lock */
34747539
YX
2151static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2152 struct tipc_node *node, u32 *prev_link)
7be57fc6
RA
2153{
2154 u32 i;
2155 int err;
2156
2157 for (i = *prev_link; i < MAX_BEARERS; i++) {
2158 *prev_link = i;
2159
2160 if (!node->links[i])
2161 continue;
2162
34747539 2163 err = __tipc_nl_add_link(net, msg, node->links[i]);
7be57fc6
RA
2164 if (err)
2165 return err;
2166 }
2167 *prev_link = 0;
2168
2169 return 0;
2170}
2171
2172int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2173{
f2f9800d
YX
2174 struct net *net = sock_net(skb->sk);
2175 struct tipc_net *tn = net_generic(net, tipc_net_id);
7be57fc6
RA
2176 struct tipc_node *node;
2177 struct tipc_nl_msg msg;
2178 u32 prev_node = cb->args[0];
2179 u32 prev_link = cb->args[1];
2180 int done = cb->args[2];
2181 int err;
2182
2183 if (done)
2184 return 0;
2185
2186 msg.skb = skb;
2187 msg.portid = NETLINK_CB(cb->skb).portid;
2188 msg.seq = cb->nlh->nlmsg_seq;
2189
2190 rcu_read_lock();
7be57fc6 2191 if (prev_node) {
f2f9800d 2192 node = tipc_node_find(net, prev_node);
7be57fc6
RA
2193 if (!node) {
2194 /* We never set seq or call nl_dump_check_consistent()
2195 * this means that setting prev_seq here will cause the
2196 * consistence check to fail in the netlink callback
2197 * handler. Resulting in the last NLMSG_DONE message
2198 * having the NLM_F_DUMP_INTR flag set.
2199 */
2200 cb->prev_seq = 1;
2201 goto out;
2202 }
8a0f6ebe 2203 tipc_node_put(node);
7be57fc6 2204
f2f9800d
YX
2205 list_for_each_entry_continue_rcu(node, &tn->node_list,
2206 list) {
7be57fc6 2207 tipc_node_lock(node);
34747539
YX
2208 err = __tipc_nl_add_node_links(net, &msg, node,
2209 &prev_link);
7be57fc6 2210 tipc_node_unlock(node);
8a0f6ebe 2211 tipc_node_put(node);
7be57fc6
RA
2212 if (err)
2213 goto out;
2214
2215 prev_node = node->addr;
2216 }
2217 } else {
1da46568 2218 err = tipc_nl_add_bc_link(net, &msg);
7be57fc6
RA
2219 if (err)
2220 goto out;
2221
f2f9800d 2222 list_for_each_entry_rcu(node, &tn->node_list, list) {
7be57fc6 2223 tipc_node_lock(node);
34747539
YX
2224 err = __tipc_nl_add_node_links(net, &msg, node,
2225 &prev_link);
7be57fc6
RA
2226 tipc_node_unlock(node);
2227 if (err)
2228 goto out;
2229
2230 prev_node = node->addr;
2231 }
2232 }
2233 done = 1;
2234out:
2235 rcu_read_unlock();
2236
2237 cb->args[0] = prev_node;
2238 cb->args[1] = prev_link;
2239 cb->args[2] = done;
2240
2241 return skb->len;
2242}
2243
2244int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2245{
f2f9800d 2246 struct net *net = genl_info_net(info);
7be57fc6
RA
2247 struct sk_buff *ans_skb;
2248 struct tipc_nl_msg msg;
2249 struct tipc_link *link;
2250 struct tipc_node *node;
2251 char *name;
2252 int bearer_id;
2253 int err;
2254
2255 if (!info->attrs[TIPC_NLA_LINK_NAME])
2256 return -EINVAL;
2257
2258 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
f2f9800d 2259 node = tipc_link_find_owner(net, name, &bearer_id);
7be57fc6
RA
2260 if (!node)
2261 return -EINVAL;
2262
2263 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2264 if (!ans_skb)
2265 return -ENOMEM;
2266
2267 msg.skb = ans_skb;
2268 msg.portid = info->snd_portid;
2269 msg.seq = info->snd_seq;
2270
2271 tipc_node_lock(node);
2272 link = node->links[bearer_id];
2273 if (!link) {
2274 err = -EINVAL;
2275 goto err_out;
2276 }
2277
34747539 2278 err = __tipc_nl_add_link(net, &msg, link);
7be57fc6
RA
2279 if (err)
2280 goto err_out;
2281
2282 tipc_node_unlock(node);
2283
2284 return genlmsg_reply(ans_skb, info);
2285
2286err_out:
2287 tipc_node_unlock(node);
2288 nlmsg_free(ans_skb);
2289
2290 return err;
2291}
ae36342b
RA
2292
2293int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2294{
2295 int err;
2296 char *link_name;
2297 unsigned int bearer_id;
2298 struct tipc_link *link;
2299 struct tipc_node *node;
2300 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1817877b 2301 struct net *net = sock_net(skb->sk);
ae36342b
RA
2302
2303 if (!info->attrs[TIPC_NLA_LINK])
2304 return -EINVAL;
2305
2306 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2307 info->attrs[TIPC_NLA_LINK],
2308 tipc_nl_link_policy);
2309 if (err)
2310 return err;
2311
2312 if (!attrs[TIPC_NLA_LINK_NAME])
2313 return -EINVAL;
2314
2315 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2316
2317 if (strcmp(link_name, tipc_bclink_name) == 0) {
1da46568 2318 err = tipc_bclink_reset_stats(net);
ae36342b
RA
2319 if (err)
2320 return err;
2321 return 0;
2322 }
2323
f2f9800d 2324 node = tipc_link_find_owner(net, link_name, &bearer_id);
ae36342b
RA
2325 if (!node)
2326 return -EINVAL;
2327
2328 tipc_node_lock(node);
2329
2330 link = node->links[bearer_id];
2331 if (!link) {
2332 tipc_node_unlock(node);
2333 return -EINVAL;
2334 }
2335
2336 link_reset_statistics(link);
2337
2338 tipc_node_unlock(node);
2339
2340 return 0;
2341}