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