blob: 248813cb6d68a0f53e5f77e4384164d11ee0f447 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/link.c: TIPC link code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy170b3922014-01-07 17:02:41 -05004 * Copyright (c) 1996-2007, 2012-2014, Ericsson AB
Ying Xue198d73b2013-06-17 10:54:42 -04005 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "link.h"
Richard Alpe7be57fc2014-11-20 10:29:12 +010039#include "bcast.h"
Jon Paul Maloy9816f062014-05-14 05:39:15 -040040#include "socket.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "discover.h"
43#include "config.h"
Richard Alpe0655f6a2014-11-20 10:29:07 +010044#include "netlink.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010045
Ying Xue796c75d2013-06-17 10:54:48 -040046#include <linux/pkt_sched.h>
47
Erik Hugne2cf8aa12012-06-29 00:16:37 -040048/*
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 ";
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Richard Alpe7be57fc2014-11-20 10:29:12 +010055static 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
Richard Alpe0655f6a2014-11-20 10:29:07 +010071/* 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
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090079/*
Allan Stephensa686e682008-06-04 17:29:39 -070080 * Out-of-range value for link session numbers
81 */
Allan Stephensa686e682008-06-04 17:29:39 -070082#define INVALID_SESSION 0x10000
83
84/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090085 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010086 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010087#define STARTING_EVT 856384768 /* link processing trigger */
88#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
89#define TIMEOUT_EVT 560817u /* link timer expired */
90
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090091/*
92 * The following two 'message types' is really just implementation
93 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 * They must not be considered part of the protocol
95 */
96#define OPEN_MSG 0
97#define CLOSED_MSG 1
98
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090099/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100 * State value stored in 'exp_msg_count'
101 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102#define START_CHANGEOVER 100000u
103
Ying Xuec93d3ba2015-01-09 15:27:04 +0800104static void link_handle_out_of_seq_msg(struct net *net,
105 struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106 struct sk_buff *buf);
Ying Xuec93d3ba2015-01-09 15:27:04 +0800107static void tipc_link_proto_rcv(struct net *net, struct tipc_link *l_ptr,
108 struct sk_buff *buf);
109static int tipc_link_tunnel_rcv(struct net *net, struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500110 struct sk_buff **buf);
Ying Xue2f55c432015-01-09 15:27:00 +0800111static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500112static void link_state_event(struct tipc_link *l_ptr, u32 event);
113static void link_reset_statistics(struct tipc_link *l_ptr);
114static void link_print(struct tipc_link *l_ptr, const char *str);
Ying Xue247f0f32014-02-18 16:06:46 +0800115static void tipc_link_sync_xmit(struct tipc_link *l);
116static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +0200117static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf);
Ying Xuec93d3ba2015-01-09 15:27:04 +0800118static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
119 struct sk_buff **buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000120
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800122 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800124static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125{
126 return (i + 3) & ~3u;
127}
128
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500129static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800131 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900133
Ying Xue7a2f7d12014-04-21 10:55:46 +0800134 rcu_read_lock();
135 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
136 if (!b_ptr) {
137 rcu_read_unlock();
138 return;
139 }
140 max_pkt = (b_ptr->mtu & ~3);
141 rcu_read_unlock();
142
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 if (max_pkt > MAX_MSG_SIZE)
144 max_pkt = MAX_MSG_SIZE;
145
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900146 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
148 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900149 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 l_ptr->max_pkt = MAX_PKT_DEFAULT;
151
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153}
154
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800156 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500158int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159{
160 if (!l_ptr)
161 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000162 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163}
164
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500165int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000167 return (l_ptr->owner->active_links[0] == l_ptr) ||
168 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169}
170
171/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 * link_timeout - handle expiration of link timer
173 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 */
Ying Xue2f55c432015-01-09 15:27:00 +0800175static void link_timeout(unsigned long data)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176{
Ying Xue2f55c432015-01-09 15:27:00 +0800177 struct tipc_link *l_ptr = (struct tipc_link *)data;
Ying Xue58dc55f2014-11-26 11:41:52 +0800178 struct sk_buff *skb;
179
Per Liden4323add2006-01-18 00:38:21 +0100180 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181
182 /* update counters used in statistical profiling of send traffic */
Ying Xue58dc55f2014-11-26 11:41:52 +0800183 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->outqueue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 l_ptr->stats.queue_sz_counts++;
185
Ying Xue58dc55f2014-11-26 11:41:52 +0800186 skb = skb_peek(&l_ptr->outqueue);
187 if (skb) {
188 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 u32 length = msg_size(msg);
190
Joe Perchesf64f9e72009-11-29 16:55:45 -0800191 if ((msg_user(msg) == MSG_FRAGMENTER) &&
192 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 length = msg_size(msg_get_wrapped(msg));
194 }
195 if (length) {
196 l_ptr->stats.msg_lengths_total += length;
197 l_ptr->stats.msg_length_counts++;
198 if (length <= 64)
199 l_ptr->stats.msg_length_profile[0]++;
200 else if (length <= 256)
201 l_ptr->stats.msg_length_profile[1]++;
202 else if (length <= 1024)
203 l_ptr->stats.msg_length_profile[2]++;
204 else if (length <= 4096)
205 l_ptr->stats.msg_length_profile[3]++;
206 else if (length <= 16384)
207 l_ptr->stats.msg_length_profile[4]++;
208 else if (length <= 32768)
209 l_ptr->stats.msg_length_profile[5]++;
210 else
211 l_ptr->stats.msg_length_profile[6]++;
212 }
213 }
214
215 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 link_state_event(l_ptr, TIMEOUT_EVT);
217
218 if (l_ptr->next_out)
Ying Xue47b4c9a2014-11-26 11:41:48 +0800219 tipc_link_push_packets(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220
Per Liden4323add2006-01-18 00:38:21 +0100221 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222}
223
Ying Xue2f55c432015-01-09 15:27:00 +0800224static void link_set_timer(struct tipc_link *link, unsigned long time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225{
Ying Xue2f55c432015-01-09 15:27:00 +0800226 mod_timer(&link->timer, jiffies + time);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227}
228
229/**
Per Liden4323add2006-01-18 00:38:21 +0100230 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500231 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900234 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 * Returns pointer to link.
236 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500237struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500238 struct tipc_bearer *b_ptr,
239 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500241 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 struct tipc_msg *msg;
243 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500244 char addr_string[16];
245 u32 peer = n_ptr->addr;
246
Holger Brunck0372bf52014-11-14 18:33:19 +0100247 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500248 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100249 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
250 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500251 return NULL;
252 }
253
254 if (n_ptr->links[b_ptr->identity]) {
255 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400256 pr_err("Attempt to establish second link on <%s> to %s\n",
257 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500258 return NULL;
259 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700261 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400263 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 return NULL;
265 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266
267 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500268 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400269 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900271 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272 if_name,
273 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400274 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500276 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400278 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800279 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400280 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 l_ptr->state = RESET_UNKNOWN;
282
283 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
284 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000285 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700287 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288 msg_set_bearer_id(msg, b_ptr->identity);
289 strcpy((char *)msg_data(msg), if_name);
290
291 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400292 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293
Ying Xue7a2f7d12014-04-21 10:55:46 +0800294 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295 link_init_max_pkt(l_ptr);
296
297 l_ptr->next_out_no = 1;
Ying Xue58dc55f2014-11-26 11:41:52 +0800298 __skb_queue_head_init(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800299 __skb_queue_head_init(&l_ptr->deferred_queue);
Richard Alpe340b6e52014-12-10 09:46:54 +0100300 skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301
302 link_reset_statistics(l_ptr);
303
Allan Stephens37b9c082011-02-28 11:32:27 -0500304 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305
Ying Xue2f55c432015-01-09 15:27:00 +0800306 setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500307
308 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 return l_ptr;
311}
312
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500313void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500314{
315 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500316 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500317
Ying Xue6c7a7622014-03-27 12:54:37 +0800318 rcu_read_lock();
319 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800320 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500321 l_ptr = n_ptr->links[bearer_id];
322 if (l_ptr) {
323 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500324 if (shutting_down || !tipc_node_is_up(n_ptr)) {
325 tipc_node_detach_link(l_ptr->owner, l_ptr);
326 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800327 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500328
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500329 /* Nobody else can access this link now: */
330 del_timer_sync(&l_ptr->timer);
331 kfree(l_ptr);
332 } else {
333 /* Detach/delete when failover is finished: */
334 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800335 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500336 del_timer_sync(&l_ptr->timer);
337 }
Ying Xuec61dd612014-02-13 17:29:09 -0500338 continue;
339 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800340 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500341 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800342 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500343}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344
345/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400346 * link_schedule_user - schedule user for wakeup after congestion
347 * @link: congested link
348 * @oport: sending port
349 * @chain_sz: size of buffer chain that was attempted sent
350 * @imp: importance of message attempted sent
351 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400353static bool link_schedule_user(struct tipc_link *link, u32 oport,
354 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400356 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400358 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
359 tipc_own_addr, oport, 0, 0);
360 if (!buf)
361 return false;
362 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
363 TIPC_SKB_CB(buf)->chain_imp = imp;
Richard Alpe340b6e52014-12-10 09:46:54 +0100364 skb_queue_tail(&link->waiting_sks, buf);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400365 link->stats.link_congs++;
366 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367}
368
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400369/**
370 * link_prepare_wakeup - prepare users for wakeup after congestion
371 * @link: congested link
372 * Move a number of waiting users, as permitted by available space in
373 * the send queue, from link wait queue to node wait queue for wakeup
374 */
375static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376{
Ying Xue58dc55f2014-11-26 11:41:52 +0800377 uint pend_qsz = skb_queue_len(&link->outqueue);
Ying Xue58d78b32014-11-26 11:41:51 +0800378 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379
Ying Xue58d78b32014-11-26 11:41:51 +0800380 skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
381 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 break;
Ying Xue58d78b32014-11-26 11:41:51 +0800383 pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
Richard Alpe340b6e52014-12-10 09:46:54 +0100384 skb_unlink(skb, &link->waiting_sks);
385 skb_queue_tail(&link->owner->waiting_sks, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387}
388
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900389/**
Per Liden4323add2006-01-18 00:38:21 +0100390 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 * @l_ptr: pointer to link
392 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500393void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400395 kfree_skb(l_ptr->reasm_buf);
396 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397}
398
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900399/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500400 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 * @l_ptr: pointer to link
402 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500403void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404{
Ying Xuebc6fecd2014-11-26 11:41:53 +0800405 __skb_queue_purge(&l_ptr->deferred_queue);
Ying Xue58dc55f2014-11-26 11:41:52 +0800406 __skb_queue_purge(&l_ptr->outqueue);
Per Liden4323add2006-01-18 00:38:21 +0100407 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408}
409
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500410void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 u32 prev_state = l_ptr->state;
413 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700414 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400415 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900416
Allan Stephensa686e682008-06-04 17:29:39 -0700417 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418
Allan Stephensa686e682008-06-04 17:29:39 -0700419 /* Link is down, accept any session */
420 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900422 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900424
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426
427 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
428 return;
429
Per Liden4323add2006-01-18 00:38:21 +0100430 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800431 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000432
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500433 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 l_ptr->reset_checkpoint = checkpoint;
435 l_ptr->exp_msg_count = START_CHANGEOVER;
436 }
437
438 /* Clean up all queues: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800439 __skb_queue_purge(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800440 __skb_queue_purge(&l_ptr->deferred_queue);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400441 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
442 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
443 owner->action_flags |= TIPC_WAKEUP_USERS;
444 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 l_ptr->next_out = NULL;
446 l_ptr->unacked_window = 0;
447 l_ptr->checkpoint = 1;
448 l_ptr->next_out_no = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 l_ptr->fsm_msg_cnt = 0;
450 l_ptr->stale_count = 0;
451 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452}
453
Ying Xuec61dd612014-02-13 17:29:09 -0500454void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500455{
456 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500457 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500458
Ying Xue6c7a7622014-03-27 12:54:37 +0800459 rcu_read_lock();
460 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800461 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500462 l_ptr = n_ptr->links[bearer_id];
463 if (l_ptr)
464 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800465 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500466 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800467 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500468}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500470static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471{
Allan Stephens5392d642006-06-25 23:52:50 -0700472 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100473 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800474 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475}
476
477/**
478 * link_state_event - link finite state machine
479 * @l_ptr: pointer to link
480 * @event: state machine event to process
481 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000482static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500484 struct tipc_link *other;
Ying Xue2f55c432015-01-09 15:27:00 +0800485 unsigned long cont_intv = l_ptr->cont_intv;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500487 if (l_ptr->flags & LINK_STOPPED)
488 return;
489
Ying Xue135daee2014-02-13 17:29:08 -0500490 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 return; /* Not yet. */
492
Ying Xue77a7e072013-12-10 20:45:44 -0800493 /* Check whether changeover is going on */
494 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000495 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800497 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
500 switch (l_ptr->state) {
501 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 switch (event) {
503 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 break;
506 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 if (l_ptr->next_in_no != l_ptr->checkpoint) {
508 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100509 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800510 tipc_link_proto_xmit(l_ptr, STATE_MSG,
511 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 l_ptr->fsm_msg_cnt++;
513 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800514 tipc_link_proto_xmit(l_ptr, STATE_MSG,
515 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 l_ptr->fsm_msg_cnt++;
517 }
518 link_set_timer(l_ptr, cont_intv);
519 break;
520 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 l_ptr->state = WORKING_UNKNOWN;
522 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800523 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 l_ptr->fsm_msg_cnt++;
525 link_set_timer(l_ptr, cont_intv / 4);
526 break;
527 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400528 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
529 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100530 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 l_ptr->state = RESET_RESET;
532 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800533 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
534 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 l_ptr->fsm_msg_cnt++;
536 link_set_timer(l_ptr, cont_intv);
537 break;
538 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400539 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 }
541 break;
542 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 switch (event) {
544 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 l_ptr->state = WORKING_WORKING;
547 l_ptr->fsm_msg_cnt = 0;
548 link_set_timer(l_ptr, cont_intv);
549 break;
550 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400551 pr_info("%s<%s>, requested by peer while probing\n",
552 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100553 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 l_ptr->state = RESET_RESET;
555 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800556 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
557 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 l_ptr->fsm_msg_cnt++;
559 link_set_timer(l_ptr, cont_intv);
560 break;
561 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 l_ptr->state = WORKING_WORKING;
564 l_ptr->fsm_msg_cnt = 0;
565 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100566 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800567 tipc_link_proto_xmit(l_ptr, STATE_MSG,
568 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 l_ptr->fsm_msg_cnt++;
570 }
571 link_set_timer(l_ptr, cont_intv);
572 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800573 tipc_link_proto_xmit(l_ptr, STATE_MSG,
574 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 l_ptr->fsm_msg_cnt++;
576 link_set_timer(l_ptr, cont_intv / 4);
577 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400578 pr_warn("%s<%s>, peer not responding\n",
579 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100580 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 l_ptr->state = RESET_UNKNOWN;
582 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800583 tipc_link_proto_xmit(l_ptr, RESET_MSG,
584 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 l_ptr->fsm_msg_cnt++;
586 link_set_timer(l_ptr, cont_intv);
587 }
588 break;
589 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400590 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 }
592 break;
593 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 switch (event) {
595 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 break;
597 case ACTIVATE_MSG:
598 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000599 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 l_ptr->state = WORKING_WORKING;
602 l_ptr->fsm_msg_cnt = 0;
603 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800604 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800606 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800607 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 link_set_timer(l_ptr, cont_intv);
609 break;
610 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 l_ptr->state = RESET_RESET;
612 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800613 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
614 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 l_ptr->fsm_msg_cnt++;
616 link_set_timer(l_ptr, cont_intv);
617 break;
618 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500619 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 /* fall through */
621 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800622 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 l_ptr->fsm_msg_cnt++;
624 link_set_timer(l_ptr, cont_intv);
625 break;
626 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400627 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 }
629 break;
630 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 switch (event) {
632 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 case ACTIVATE_MSG:
634 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000635 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 l_ptr->state = WORKING_WORKING;
638 l_ptr->fsm_msg_cnt = 0;
639 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800640 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800642 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800643 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 link_set_timer(l_ptr, cont_intv);
645 break;
646 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 break;
648 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800649 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
650 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 l_ptr->fsm_msg_cnt++;
652 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 break;
654 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400655 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 }
657 break;
658 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400659 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 }
661}
662
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500663/* tipc_link_cong: determine return value and how to treat the
664 * sent buffer during link congestion.
665 * - For plain, errorless user data messages we keep the buffer and
666 * return -ELINKONG.
667 * - For all other messages we discard the buffer and return -EHOSTUNREACH
668 * - For TIPC internal messages we also reset the link
669 */
Ying Xuea6ca1092014-11-26 11:41:55 +0800670static int tipc_link_cong(struct tipc_link *link, struct sk_buff_head *list)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500671{
Ying Xuea6ca1092014-11-26 11:41:55 +0800672 struct sk_buff *skb = skb_peek(list);
673 struct tipc_msg *msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500674 uint imp = tipc_msg_tot_importance(msg);
675 u32 oport = msg_tot_origport(msg);
676
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400677 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500678 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
679 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400680 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500681 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400682 if (unlikely(msg_errcode(msg)))
683 goto drop;
684 if (unlikely(msg_reroute_cnt(msg)))
685 goto drop;
Ying Xuea6ca1092014-11-26 11:41:55 +0800686 if (TIPC_SKB_CB(skb)->wakeup_pending)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400687 return -ELINKCONG;
Ying Xuea6ca1092014-11-26 11:41:55 +0800688 if (link_schedule_user(link, oport, skb_queue_len(list), imp))
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400689 return -ELINKCONG;
690drop:
Ying Xuea6ca1092014-11-26 11:41:55 +0800691 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500692 return -EHOSTUNREACH;
693}
694
695/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400696 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500697 * @link: link to use
Ying Xuea6ca1092014-11-26 11:41:55 +0800698 * @list: chain of buffers containing message
699 *
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500700 * Consumes the buffer chain, except when returning -ELINKCONG
701 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
702 * user data messages) or -EHOSTUNREACH (all other messages/senders)
703 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
704 * to act on the return value, since they may need to do more send attempts.
705 */
Ying Xuea6ca1092014-11-26 11:41:55 +0800706int __tipc_link_xmit(struct tipc_link *link, struct sk_buff_head *list)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500707{
Ying Xuea6ca1092014-11-26 11:41:55 +0800708 struct tipc_msg *msg = buf_msg(skb_peek(list));
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500709 uint psz = msg_size(msg);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500710 uint sndlim = link->queue_limit[0];
711 uint imp = tipc_msg_tot_importance(msg);
712 uint mtu = link->max_pkt;
713 uint ack = mod(link->next_in_no - 1);
714 uint seqno = link->next_out_no;
715 uint bc_last_in = link->owner->bclink.last_in;
716 struct tipc_media_addr *addr = &link->media_addr;
Ying Xue58dc55f2014-11-26 11:41:52 +0800717 struct sk_buff_head *outqueue = &link->outqueue;
Ying Xuea6ca1092014-11-26 11:41:55 +0800718 struct sk_buff *skb, *tmp;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500719
720 /* Match queue limits against msg importance: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800721 if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp]))
Ying Xuea6ca1092014-11-26 11:41:55 +0800722 return tipc_link_cong(link, list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500723
724 /* Has valid packet limit been used ? */
725 if (unlikely(psz > mtu)) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800726 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500727 return -EMSGSIZE;
728 }
729
730 /* Prepare each packet for sending, and add to outqueue: */
Ying Xuea6ca1092014-11-26 11:41:55 +0800731 skb_queue_walk_safe(list, skb, tmp) {
732 __skb_unlink(skb, list);
Ying Xue58dc55f2014-11-26 11:41:52 +0800733 msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500734 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
735 msg_set_bcast_ack(msg, bc_last_in);
736
Ying Xue58dc55f2014-11-26 11:41:52 +0800737 if (skb_queue_len(outqueue) < sndlim) {
738 __skb_queue_tail(outqueue, skb);
739 tipc_bearer_send(link->bearer_id, skb, addr);
740 link->next_out = NULL;
741 link->unacked_window = 0;
742 } else if (tipc_msg_bundle(outqueue, skb, mtu)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500743 link->stats.sent_bundled++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500744 continue;
Ying Xue58dc55f2014-11-26 11:41:52 +0800745 } else if (tipc_msg_make_bundle(outqueue, skb, mtu,
746 link->addr)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500747 link->stats.sent_bundled++;
748 link->stats.sent_bundles++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500749 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800750 link->next_out = skb_peek_tail(outqueue);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500751 } else {
Ying Xue58dc55f2014-11-26 11:41:52 +0800752 __skb_queue_tail(outqueue, skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500753 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800754 link->next_out = skb;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500755 }
756 seqno++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500757 }
758 link->next_out_no = seqno;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500759 return 0;
760}
761
Ying Xuea6ca1092014-11-26 11:41:55 +0800762static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
763{
764 __skb_queue_head_init(list);
765 __skb_queue_tail(list, skb);
766}
767
768static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
769{
770 struct sk_buff_head head;
771
772 skb2list(skb, &head);
773 return __tipc_link_xmit(link, &head);
774}
775
776int tipc_link_xmit_skb(struct sk_buff *skb, u32 dnode, u32 selector)
777{
778 struct sk_buff_head head;
779
780 skb2list(skb, &head);
781 return tipc_link_xmit(&head, dnode, selector);
782}
783
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500784/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400785 * tipc_link_xmit() is the general link level function for message sending
Ying Xuea6ca1092014-11-26 11:41:55 +0800786 * @list: chain of buffers containing message
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500787 * @dsz: amount of user data to be sent
788 * @dnode: address of destination node
789 * @selector: a number used for deterministic link selection
790 * Consumes the buffer chain, except when returning -ELINKCONG
791 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
792 */
Ying Xuea6ca1092014-11-26 11:41:55 +0800793int tipc_link_xmit(struct sk_buff_head *list, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500794{
795 struct tipc_link *link = NULL;
796 struct tipc_node *node;
797 int rc = -EHOSTUNREACH;
798
799 node = tipc_node_find(dnode);
800 if (node) {
801 tipc_node_lock(node);
802 link = node->active_links[selector & 1];
803 if (link)
Ying Xuea6ca1092014-11-26 11:41:55 +0800804 rc = __tipc_link_xmit(link, list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500805 tipc_node_unlock(node);
806 }
807
808 if (link)
809 return rc;
810
Ying Xuea6ca1092014-11-26 11:41:55 +0800811 if (likely(in_own_node(dnode))) {
812 /* As a node local message chain never contains more than one
813 * buffer, we just need to dequeue one SKB buffer from the
814 * head list.
815 */
816 return tipc_sk_rcv(__skb_dequeue(list));
817 }
818 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500819
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500820 return rc;
821}
822
Jon Maloyc64f7a62012-11-16 13:51:31 +0800823/*
Ying Xue247f0f32014-02-18 16:06:46 +0800824 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800825 *
826 * Give a newly added peer node the sequence number where it should
827 * start receiving and acking broadcast packets.
828 *
829 * Called with node locked
830 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400831static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800832{
Ying Xuea6ca1092014-11-26 11:41:55 +0800833 struct sk_buff *skb;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800834 struct tipc_msg *msg;
835
Ying Xuea6ca1092014-11-26 11:41:55 +0800836 skb = tipc_buf_acquire(INT_H_SIZE);
837 if (!skb)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800838 return;
839
Ying Xuea6ca1092014-11-26 11:41:55 +0800840 msg = buf_msg(skb);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400841 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
842 msg_set_last_bcast(msg, link->owner->bclink.acked);
Ying Xuea6ca1092014-11-26 11:41:55 +0800843 __tipc_link_xmit_skb(link, skb);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800844}
845
846/*
Ying Xue247f0f32014-02-18 16:06:46 +0800847 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800848 * Receive the sequence number where we should start receiving and
849 * acking broadcast packets from a newly added peer node, and open
850 * up for reception of such packets.
851 *
852 * Called with node locked
853 */
Ying Xue247f0f32014-02-18 16:06:46 +0800854static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800855{
856 struct tipc_msg *msg = buf_msg(buf);
857
858 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
859 n->bclink.recv_permitted = true;
860 kfree_skb(buf);
861}
862
Ying Xue58dc55f2014-11-26 11:41:52 +0800863struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
864 const struct sk_buff *skb)
865{
866 if (skb_queue_is_last(list, skb))
867 return NULL;
868 return skb->next;
869}
870
Jon Maloyc64f7a62012-11-16 13:51:31 +0800871/*
Ying Xue47b4c9a2014-11-26 11:41:48 +0800872 * tipc_link_push_packets - push unsent packets to bearer
873 *
874 * Push out the unsent messages of a link where congestion
875 * has abated. Node is locked.
876 *
877 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 */
Ying Xue47b4c9a2014-11-26 11:41:48 +0800879void tipc_link_push_packets(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880{
Ying Xue58dc55f2014-11-26 11:41:52 +0800881 struct sk_buff_head *outqueue = &l_ptr->outqueue;
882 struct sk_buff *skb = l_ptr->next_out;
Ying Xue47b4c9a2014-11-26 11:41:48 +0800883 struct tipc_msg *msg;
884 u32 next, first;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885
Ying Xue58dc55f2014-11-26 11:41:52 +0800886 skb_queue_walk_from(outqueue, skb) {
Ying Xue47b4c9a2014-11-26 11:41:48 +0800887 msg = buf_msg(skb);
888 next = msg_seqno(msg);
Ying Xue58dc55f2014-11-26 11:41:52 +0800889 first = buf_seqno(skb_peek(outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890
891 if (mod(next - first) < l_ptr->queue_limit[0]) {
892 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900893 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +0800894 if (msg_user(msg) == MSG_BUNDLER)
Ying Xue58311d12014-11-26 11:41:49 +0800895 TIPC_SKB_CB(skb)->bundling = false;
Ying Xue47b4c9a2014-11-26 11:41:48 +0800896 tipc_bearer_send(l_ptr->bearer_id, skb,
897 &l_ptr->media_addr);
Ying Xue58dc55f2014-11-26 11:41:52 +0800898 l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
Ying Xue47b4c9a2014-11-26 11:41:48 +0800899 } else {
900 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 }
902 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903}
904
Ying Xue3f5a12b2014-05-05 08:56:17 +0800905void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700906{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700907 char addr_string[16];
908 u32 i;
909
Ying Xue3f5a12b2014-05-05 08:56:17 +0800910 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700911
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400912 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800913 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700914
915 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800916 if (node->links[i]) {
917 link_print(node->links[i], "Resetting link\n");
918 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700919 }
920 }
921
Ying Xue3f5a12b2014-05-05 08:56:17 +0800922 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700923}
924
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500925static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400926 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700927{
928 struct tipc_msg *msg = buf_msg(buf);
929
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400930 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700931
932 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700933 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000934 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700935 tipc_link_reset(l_ptr);
936
937 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700938 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700939 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700940 char addr_string[16];
941
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400942 pr_info("Msg seq number: %u, ", msg_seqno(msg));
943 pr_cont("Outstanding acks: %lu\n",
944 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700945
Allan Stephens01d83ed2011-01-18 13:53:16 -0500946 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700947 tipc_node_lock(n_ptr);
948
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000949 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400950 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800951 pr_info("Reception permitted: %d, Acked: %u\n",
952 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400953 n_ptr->bclink.acked);
954 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
955 n_ptr->bclink.last_in,
956 n_ptr->bclink.oos_state,
957 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700958
Allan Stephensd356eeb2006-06-25 23:40:01 -0700959 tipc_node_unlock(n_ptr);
960
Ying Xue3f5a12b2014-05-05 08:56:17 +0800961 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700962 l_ptr->stale_count = 0;
963 }
964}
965
Ying Xue58dc55f2014-11-26 11:41:52 +0800966void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
Per Liden4323add2006-01-18 00:38:21 +0100967 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968{
969 struct tipc_msg *msg;
970
Ying Xue58dc55f2014-11-26 11:41:52 +0800971 if (!skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700972 return;
973
Ying Xue58dc55f2014-11-26 11:41:52 +0800974 msg = buf_msg(skb);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900975
Erik Hugne512137e2013-12-06 10:08:00 -0500976 /* Detect repeated retransmit failures */
977 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
978 if (++l_ptr->stale_count > 100) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800979 link_retransmit_failure(l_ptr, skb);
Erik Hugne512137e2013-12-06 10:08:00 -0500980 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700981 }
982 } else {
Erik Hugne512137e2013-12-06 10:08:00 -0500983 l_ptr->last_retransmitted = msg_seqno(msg);
984 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 }
Allan Stephensd356eeb2006-06-25 23:40:01 -0700986
Ying Xue58dc55f2014-11-26 11:41:52 +0800987 skb_queue_walk_from(&l_ptr->outqueue, skb) {
988 if (!retransmits || skb == l_ptr->next_out)
989 break;
990 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900992 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue58dc55f2014-11-26 11:41:52 +0800993 tipc_bearer_send(l_ptr->bearer_id, skb, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800994 retransmits--;
995 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997}
998
Ying Xuef03273f2014-11-26 11:41:54 +0800999static void link_retrieve_defq(struct tipc_link *link,
1000 struct sk_buff_head *list)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001{
1002 u32 seq_no;
1003
Ying Xuef03273f2014-11-26 11:41:54 +08001004 if (skb_queue_empty(&link->deferred_queue))
1005 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006
Ying Xuef03273f2014-11-26 11:41:54 +08001007 seq_no = buf_seqno(skb_peek(&link->deferred_queue));
1008 if (seq_no == mod(link->next_in_no))
1009 skb_queue_splice_tail_init(&link->deferred_queue, list);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010}
1011
Allan Stephens85035562008-04-15 19:04:54 -07001012/**
1013 * link_recv_buf_validate - validate basic format of received message
1014 *
1015 * This routine ensures a TIPC message has an acceptable header, and at least
1016 * as much data as the header indicates it should. The routine also ensures
1017 * that the entire message header is stored in the main fragment of the message
1018 * buffer, to simplify future access to message header fields.
1019 *
1020 * Note: Having extra info present in the message header or data areas is OK.
1021 * TIPC will ignore the excess, under the assumption that it is optional info
1022 * introduced by a later release of the protocol.
1023 */
Allan Stephens85035562008-04-15 19:04:54 -07001024static int link_recv_buf_validate(struct sk_buff *buf)
1025{
1026 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001027 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001028 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1029 };
1030
1031 struct tipc_msg *msg;
1032 u32 tipc_hdr[2];
1033 u32 size;
1034 u32 hdr_size;
1035 u32 min_hdr_size;
1036
Erik Hugne64380a02014-02-11 11:38:26 +01001037 /* If this packet comes from the defer queue, the skb has already
1038 * been validated
1039 */
1040 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1041 return 1;
1042
Allan Stephens85035562008-04-15 19:04:54 -07001043 if (unlikely(buf->len < MIN_H_SIZE))
1044 return 0;
1045
1046 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1047 if (msg == NULL)
1048 return 0;
1049
1050 if (unlikely(msg_version(msg) != TIPC_VERSION))
1051 return 0;
1052
1053 size = msg_size(msg);
1054 hdr_size = msg_hdr_sz(msg);
1055 min_hdr_size = msg_isdata(msg) ?
1056 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1057
1058 if (unlikely((hdr_size < min_hdr_size) ||
1059 (size < hdr_size) ||
1060 (buf->len < size) ||
1061 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1062 return 0;
1063
1064 return pskb_may_pull(buf, hdr_size);
1065}
1066
Allan Stephensb02b69c2010-08-17 11:00:07 +00001067/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001068 * tipc_rcv - process TIPC packets/messages arriving from off-node
Ying Xuec93d3ba2015-01-09 15:27:04 +08001069 * @net: net namespace handler
Ying Xuef03273f2014-11-26 11:41:54 +08001070 * @skb: TIPC packet
Ying Xue7a2f7d12014-04-21 10:55:46 +08001071 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001072 *
1073 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1074 * structure (i.e. cannot be NULL), but bearer can be inactive.
1075 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001076void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077{
Ying Xuef03273f2014-11-26 11:41:54 +08001078 struct sk_buff_head head;
1079 struct tipc_node *n_ptr;
1080 struct tipc_link *l_ptr;
1081 struct sk_buff *skb1, *tmp;
1082 struct tipc_msg *msg;
1083 u32 seq_no;
1084 u32 ackd;
1085 u32 released;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086
Ying Xuea6ca1092014-11-26 11:41:55 +08001087 skb2list(skb, &head);
Allan Stephens85035562008-04-15 19:04:54 -07001088
Ying Xuef03273f2014-11-26 11:41:54 +08001089 while ((skb = __skb_dequeue(&head))) {
Allan Stephens85035562008-04-15 19:04:54 -07001090 /* Ensure message is well-formed */
Ying Xuef03273f2014-11-26 11:41:54 +08001091 if (unlikely(!link_recv_buf_validate(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001092 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093
Allan Stephensfe13dda2008-04-15 19:03:23 -07001094 /* Ensure message data is a single contiguous unit */
Ying Xuef03273f2014-11-26 11:41:54 +08001095 if (unlikely(skb_linearize(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001096 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001097
Allan Stephens85035562008-04-15 19:04:54 -07001098 /* Handle arrival of a non-unicast link message */
Ying Xuef03273f2014-11-26 11:41:54 +08001099 msg = buf_msg(skb);
Allan Stephens85035562008-04-15 19:04:54 -07001100
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001102 if (msg_user(msg) == LINK_CONFIG)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001103 tipc_disc_rcv(net, skb, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001104 else
Ying Xuec93d3ba2015-01-09 15:27:04 +08001105 tipc_bclink_rcv(net, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 continue;
1107 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001108
Allan Stephensed33a9c2011-04-05 15:15:04 -04001109 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001110 if (unlikely(!msg_short(msg) &&
1111 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001112 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001113
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001114 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001115 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001117 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001118 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001119
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001120 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001122 if (unlikely(!l_ptr))
1123 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001124
Allan Stephensb4b56102011-05-27 11:00:51 -04001125 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001126 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001127 msg_user(msg) == LINK_PROTOCOL &&
1128 (msg_type(msg) == RESET_MSG ||
1129 msg_type(msg) == ACTIVATE_MSG) &&
1130 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001131 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001132
Ying Xue10f465c2014-05-05 08:56:11 +08001133 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001134 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001135
Allan Stephens85035562008-04-15 19:04:54 -07001136 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001137 seq_no = msg_seqno(msg);
1138 ackd = msg_ack(msg);
1139
1140 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001141 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001142 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143
Ying Xue58dc55f2014-11-26 11:41:52 +08001144 released = 0;
1145 skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) {
1146 if (skb1 == l_ptr->next_out ||
1147 more(buf_seqno(skb1), ackd))
1148 break;
1149 __skb_unlink(skb1, &l_ptr->outqueue);
1150 kfree_skb(skb1);
1151 released = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 }
Allan Stephens85035562008-04-15 19:04:54 -07001153
1154 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 if (unlikely(l_ptr->next_out))
Ying Xue47b4c9a2014-11-26 11:41:48 +08001156 tipc_link_push_packets(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001157
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001158 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1159 link_prepare_wakeup(l_ptr);
1160 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1161 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001162
Jon Paul Maloya5377832014-02-13 17:29:15 -05001163 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001164 if (unlikely(!link_working_working(l_ptr))) {
1165 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001166 tipc_link_proto_rcv(net, l_ptr, skb);
Ying Xuef03273f2014-11-26 11:41:54 +08001167 link_retrieve_defq(l_ptr, &head);
Per Liden4323add2006-01-18 00:38:21 +01001168 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 continue;
1170 }
Ying Xue3af390e2013-10-30 11:26:57 +08001171
1172 /* Traffic message. Conditionally activate link */
1173 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1174
1175 if (link_working_working(l_ptr)) {
1176 /* Re-insert buffer in front of queue */
Ying Xuef03273f2014-11-26 11:41:54 +08001177 __skb_queue_head(&head, skb);
Ying Xue3af390e2013-10-30 11:26:57 +08001178 tipc_node_unlock(n_ptr);
1179 continue;
1180 }
1181 goto unlock_discard;
1182 }
1183
1184 /* Link is now in state WORKING_WORKING */
1185 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001186 link_handle_out_of_seq_msg(net, l_ptr, skb);
Ying Xuef03273f2014-11-26 11:41:54 +08001187 link_retrieve_defq(l_ptr, &head);
Per Liden4323add2006-01-18 00:38:21 +01001188 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 continue;
1190 }
Ying Xue3af390e2013-10-30 11:26:57 +08001191 l_ptr->next_in_no++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001192 if (unlikely(!skb_queue_empty(&l_ptr->deferred_queue)))
Ying Xuef03273f2014-11-26 11:41:54 +08001193 link_retrieve_defq(l_ptr, &head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001194
Erik Hugne3f53bd82014-07-01 10:22:41 +02001195 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1196 l_ptr->stats.sent_acks++;
1197 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1198 }
1199
Ying Xuec93d3ba2015-01-09 15:27:04 +08001200 if (tipc_link_prepare_input(net, l_ptr, &skb)) {
Per Liden4323add2006-01-18 00:38:21 +01001201 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 }
Per Liden4323add2006-01-18 00:38:21 +01001204 tipc_node_unlock(n_ptr);
Ying Xuef03273f2014-11-26 11:41:54 +08001205
1206 if (tipc_link_input(l_ptr, skb) != 0)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001207 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001208 continue;
1209unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001210 tipc_node_unlock(n_ptr);
1211discard:
Ying Xuef03273f2014-11-26 11:41:54 +08001212 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214}
1215
Ben Hutchings2c530402012-07-10 10:55:09 +00001216/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001217 * tipc_link_prepare_input - process TIPC link messages
1218 *
1219 * returns nonzero if the message was consumed
1220 *
1221 * Node lock must be held
1222 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001223static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
1224 struct sk_buff **buf)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001225{
1226 struct tipc_node *n;
1227 struct tipc_msg *msg;
1228 int res = -EINVAL;
1229
1230 n = l->owner;
1231 msg = buf_msg(*buf);
1232 switch (msg_user(msg)) {
1233 case CHANGEOVER_PROTOCOL:
Ying Xuec93d3ba2015-01-09 15:27:04 +08001234 if (tipc_link_tunnel_rcv(net, n, buf))
Erik Hugne7ae934b2014-07-01 10:22:40 +02001235 res = 0;
1236 break;
1237 case MSG_FRAGMENTER:
1238 l->stats.recv_fragments++;
1239 if (tipc_buf_append(&l->reasm_buf, buf)) {
1240 l->stats.recv_fragmented++;
1241 res = 0;
1242 } else if (!l->reasm_buf) {
1243 tipc_link_reset(l);
1244 }
1245 break;
1246 case MSG_BUNDLER:
1247 l->stats.recv_bundles++;
1248 l->stats.recv_bundled += msg_msgcnt(msg);
1249 res = 0;
1250 break;
1251 case NAME_DISTRIBUTOR:
1252 n->bclink.recv_permitted = true;
1253 res = 0;
1254 break;
1255 case BCAST_PROTOCOL:
1256 tipc_link_sync_rcv(n, *buf);
1257 break;
1258 default:
1259 res = 0;
1260 }
1261 return res;
1262}
1263/**
1264 * tipc_link_input - Deliver message too higher layers
1265 */
1266static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1267{
1268 struct tipc_msg *msg = buf_msg(buf);
1269 int res = 0;
1270
1271 switch (msg_user(msg)) {
1272 case TIPC_LOW_IMPORTANCE:
1273 case TIPC_MEDIUM_IMPORTANCE:
1274 case TIPC_HIGH_IMPORTANCE:
1275 case TIPC_CRITICAL_IMPORTANCE:
1276 case CONN_MANAGER:
1277 tipc_sk_rcv(buf);
1278 break;
1279 case NAME_DISTRIBUTOR:
1280 tipc_named_rcv(buf);
1281 break;
1282 case MSG_BUNDLER:
1283 tipc_link_bundle_rcv(buf);
1284 break;
1285 default:
1286 res = -EINVAL;
1287 }
1288 return res;
1289}
1290
1291/**
Allan Stephens8809b252011-10-25 10:44:35 -04001292 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1293 *
1294 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001296u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297{
Ying Xuebc6fecd2014-11-26 11:41:53 +08001298 struct sk_buff *skb1;
1299 u32 seq_no = buf_seqno(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300
1301 /* Empty queue ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001302 if (skb_queue_empty(list)) {
1303 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 return 1;
1305 }
1306
1307 /* Last ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001308 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1309 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 return 1;
1311 }
1312
Allan Stephens8809b252011-10-25 10:44:35 -04001313 /* Locate insertion point in queue, then insert; discard if duplicate */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001314 skb_queue_walk(list, skb1) {
1315 u32 curr_seqno = buf_seqno(skb1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316
Allan Stephens8809b252011-10-25 10:44:35 -04001317 if (seq_no == curr_seqno) {
Ying Xuebc6fecd2014-11-26 11:41:53 +08001318 kfree_skb(skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001319 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 }
Allan Stephens8809b252011-10-25 10:44:35 -04001321
1322 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 break;
Allan Stephens8809b252011-10-25 10:44:35 -04001324 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325
Ying Xuebc6fecd2014-11-26 11:41:53 +08001326 __skb_queue_before(list, skb1, skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001327 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328}
1329
Allan Stephens8809b252011-10-25 10:44:35 -04001330/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1332 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001333static void link_handle_out_of_seq_msg(struct net *net,
1334 struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 struct sk_buff *buf)
1336{
Allan Stephensf9057302011-10-24 16:03:12 -04001337 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338
1339 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001340 tipc_link_proto_rcv(net, l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 return;
1342 }
1343
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 l_ptr->checkpoint--;
1346
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001347 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 * Discard packet if a duplicate; otherwise add it to deferred queue
1349 * and notify peer of gap as per protocol specification
1350 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 if (less(seq_no, mod(l_ptr->next_in_no))) {
1352 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001353 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 return;
1355 }
1356
Ying Xuebc6fecd2014-11-26 11:41:53 +08001357 if (tipc_link_defer_pkt(&l_ptr->deferred_queue, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001359 TIPC_SKB_CB(buf)->deferred = true;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001360 if ((skb_queue_len(&l_ptr->deferred_queue) % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001361 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001362 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 l_ptr->stats.duplicates++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001364 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365}
1366
1367/*
1368 * Send protocol message to the other endpoint.
1369 */
Ying Xue247f0f32014-02-18 16:06:46 +08001370void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1371 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001373 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001375 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001376 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377
Ying Xue77a7e072013-12-10 20:45:44 -08001378 /* Don't send protocol message during link changeover */
1379 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001381
1382 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001383 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001384 return;
1385
Allan Stephens92d2c902011-10-25 11:20:26 -04001386 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001388 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001389 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001390 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391
1392 if (msg_typ == STATE_MSG) {
1393 u32 next_sent = mod(l_ptr->next_out_no);
1394
Per Liden4323add2006-01-18 00:38:21 +01001395 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 return;
1397 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001398 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399 msg_set_next_sent(msg, next_sent);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001400 if (!skb_queue_empty(&l_ptr->deferred_queue)) {
1401 u32 rec = buf_seqno(skb_peek(&l_ptr->deferred_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 gap = mod(rec - mod(l_ptr->next_in_no));
1403 }
1404 msg_set_seq_gap(msg, gap);
1405 if (gap)
1406 l_ptr->stats.sent_nacks++;
1407 msg_set_link_tolerance(msg, tolerance);
1408 msg_set_linkprio(msg, priority);
1409 msg_set_max_pkt(msg, ack_mtu);
1410 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1411 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001412 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 u32 mtu = l_ptr->max_pkt;
1414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001415 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 link_working_working(l_ptr) &&
1417 l_ptr->fsm_msg_cnt) {
1418 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001419 if (l_ptr->max_pkt_probes == 10) {
1420 l_ptr->max_pkt_target = (msg_size - 4);
1421 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001423 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001425 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426
1427 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001428 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 l_ptr->stats.sent_states++;
1430 } else { /* RESET_MSG or ACTIVATE_MSG */
1431 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1432 msg_set_seq_gap(msg, 0);
1433 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001434 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 msg_set_link_tolerance(msg, l_ptr->tolerance);
1436 msg_set_linkprio(msg, l_ptr->priority);
1437 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1438 }
1439
Allan Stephens75f0aa42011-02-28 15:30:20 -05001440 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1441 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001443 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444
1445 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1446
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001447 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 if (!buf)
1449 return;
1450
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001451 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001452 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453
Ying Xue7a2f7d12014-04-21 10:55:46 +08001454 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001455 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001456 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457}
1458
1459/*
1460 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001461 * Note that network plane id propagates through the network, and may
1462 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001464static void tipc_link_proto_rcv(struct net *net, struct tipc_link *l_ptr,
1465 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466{
1467 u32 rec_gap = 0;
1468 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001469 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 u32 msg_tol;
1471 struct tipc_msg *msg = buf_msg(buf);
1472
Ying Xue77a7e072013-12-10 20:45:44 -08001473 /* Discard protocol message during link changeover */
1474 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 goto exit;
1476
Ying Xue7a2f7d12014-04-21 10:55:46 +08001477 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001479 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001482
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001484 if (!link_working_unknown(l_ptr) &&
1485 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001486 if (less_eq(msg_session(msg), l_ptr->peer_session))
1487 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001489
1490 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1491 link_working_unknown(l_ptr))) {
1492 /*
1493 * peer has lost contact -- don't allow peer's links
1494 * to reactivate before we recognize loss & clean up
1495 */
Ying Xueca9cf062014-05-08 08:54:40 +08001496 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001497 }
1498
Allan Stephens47361c82011-10-26 10:55:16 -04001499 link_state_event(l_ptr, RESET_MSG);
1500
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 /* fall thru' */
1502 case ACTIVATE_MSG:
1503 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1505
Allan Stephens2db99832010-12-31 18:59:33 +00001506 msg_tol = msg_link_tolerance(msg);
1507 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 link_set_supervision_props(l_ptr, msg_tol);
1509
1510 if (msg_linkprio(msg) > l_ptr->priority)
1511 l_ptr->priority = msg_linkprio(msg);
1512
1513 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001514 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515 if (max_pkt_info < l_ptr->max_pkt_target)
1516 l_ptr->max_pkt_target = max_pkt_info;
1517 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1518 l_ptr->max_pkt = l_ptr->max_pkt_target;
1519 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001520 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522
Allan Stephens4d753132011-10-25 12:19:05 -04001523 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001524 if (!tipc_node_is_up(l_ptr->owner)) {
1525 l_ptr->owner->bclink.last_sent =
1526 l_ptr->owner->bclink.last_in =
1527 msg_last_bcast(msg);
1528 l_ptr->owner->bclink.oos_state = 0;
1529 }
Allan Stephens4d753132011-10-25 12:19:05 -04001530
Per Lidenb97bf3f2006-01-02 19:04:38 +01001531 l_ptr->peer_session = msg_session(msg);
1532 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001533
1534 if (msg_type(msg) == ACTIVATE_MSG)
1535 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001536 break;
1537 case STATE_MSG:
1538
Allan Stephens2db99832010-12-31 18:59:33 +00001539 msg_tol = msg_link_tolerance(msg);
1540 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001542
1543 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001545 pr_warn("%s<%s>, priority change %u->%u\n",
1546 link_rst_msg, l_ptr->name, l_ptr->priority,
1547 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001548 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001549 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001550 break;
1551 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001552
1553 /* Record reception; force mismatch at next timeout: */
1554 l_ptr->checkpoint--;
1555
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1557 l_ptr->stats.recv_states++;
1558 if (link_reset_unknown(l_ptr))
1559 break;
1560
1561 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001562 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563 mod(l_ptr->next_in_no));
1564 }
1565
1566 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001567 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001568 l_ptr->max_pkt = max_pkt_ack;
1569 l_ptr->max_pkt_probes = 0;
1570 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571
1572 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001573 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001575 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001576 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001577 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001578
1579 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001580 if (l_ptr->owner->bclink.recv_permitted)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001581 tipc_bclink_update_link_state(net, l_ptr->owner,
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001582 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583
1584 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001585 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1586 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 }
1588 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589 l_ptr->stats.recv_nacks++;
Ying Xue58dc55f2014-11-26 11:41:52 +08001590 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue),
Per Liden4323add2006-01-18 00:38:21 +01001591 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001592 }
1593 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594 }
1595exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001596 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597}
1598
1599
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001600/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1601 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001603static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1604 struct tipc_msg *tunnel_hdr,
1605 struct tipc_msg *msg,
1606 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001608 struct tipc_link *tunnel;
Ying Xuea6ca1092014-11-26 11:41:55 +08001609 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610 u32 length = msg_size(msg);
1611
1612 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001613 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001614 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001616 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001617 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001618 skb = tipc_buf_acquire(length + INT_H_SIZE);
1619 if (!skb) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001620 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001622 }
Ying Xuea6ca1092014-11-26 11:41:55 +08001623 skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
1624 skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
1625 __tipc_link_xmit_skb(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001626}
1627
1628
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001629/* tipc_link_failover_send_queue(): A link has gone down, but a second
1630 * link is still active. We can do failover. Tunnel the failing link's
1631 * whole send queue via the remaining link. This way, we don't lose
1632 * any packets, and sequence order is preserved for subsequent traffic
1633 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001635void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636{
Ying Xue58dc55f2014-11-26 11:41:52 +08001637 u32 msgcount = skb_queue_len(&l_ptr->outqueue);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001638 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639 struct tipc_msg tunnel_hdr;
Ying Xue58dc55f2014-11-26 11:41:52 +08001640 struct sk_buff *skb;
Allan Stephens5392d642006-06-25 23:52:50 -07001641 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642
1643 if (!tunnel)
1644 return;
1645
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001646 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001647 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1649 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001650
Ying Xue58dc55f2014-11-26 11:41:52 +08001651 if (skb_queue_empty(&l_ptr->outqueue)) {
1652 skb = tipc_buf_acquire(INT_H_SIZE);
1653 if (skb) {
1654 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001656 __tipc_link_xmit_skb(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001658 pr_warn("%sunable to send changeover msg\n",
1659 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 }
1661 return;
1662 }
Allan Stephensf1310722006-06-25 23:51:37 -07001663
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001664 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001665 l_ptr->owner->active_links[1]);
1666
Ying Xue58dc55f2014-11-26 11:41:52 +08001667 skb_queue_walk(&l_ptr->outqueue, skb) {
1668 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669
1670 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001672 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673
Florian Westphald788d802007-08-02 19:28:06 -07001674 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001676 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001677 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1678 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 pos += align(msg_size(m));
1680 m = (struct tipc_msg *)pos;
1681 }
1682 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001683 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1684 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686 }
1687}
1688
Ying Xue247f0f32014-02-18 16:06:46 +08001689/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001690 * duplicate of the first link's send queue via the new link. This way, we
1691 * are guaranteed that currently queued packets from a socket are delivered
1692 * before future traffic from the same socket, even if this is using the
1693 * new link. The last arriving copy of each duplicate packet is dropped at
1694 * the receiving end by the regular protocol check, so packet cardinality
1695 * and sequence order is preserved per sender/receiver socket pair.
1696 * Owner node is locked.
1697 */
Ying Xue247f0f32014-02-18 16:06:46 +08001698void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001699 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700{
Ying Xue58dc55f2014-11-26 11:41:52 +08001701 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 struct tipc_msg tunnel_hdr;
1703
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001704 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001705 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Ying Xue58dc55f2014-11-26 11:41:52 +08001706 msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
Ying Xue58dc55f2014-11-26 11:41:52 +08001708 skb_queue_walk(&l_ptr->outqueue, skb) {
1709 struct sk_buff *outskb;
1710 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 u32 length = msg_size(msg);
1712
1713 if (msg_user(msg) == MSG_BUNDLER)
1714 msg_set_type(msg, CLOSED_MSG);
1715 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001716 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001718 outskb = tipc_buf_acquire(length + INT_H_SIZE);
1719 if (outskb == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001720 pr_warn("%sunable to send duplicate msg\n",
1721 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722 return;
1723 }
Ying Xue58dc55f2014-11-26 11:41:52 +08001724 skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE);
1725 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data,
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001726 length);
Ying Xuea6ca1092014-11-26 11:41:55 +08001727 __tipc_link_xmit_skb(tunnel, outskb);
Per Liden4323add2006-01-18 00:38:21 +01001728 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 }
1731}
1732
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733/**
1734 * buf_extract - extracts embedded TIPC message from another message
1735 * @skb: encapsulating message buffer
1736 * @from_pos: offset to extract from
1737 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001738 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 * encapsulating message itself is left unchanged.
1740 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1742{
1743 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1744 u32 size = msg_size(msg);
1745 struct sk_buff *eb;
1746
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001747 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001749 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 return eb;
1751}
1752
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001753
1754
1755/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1756 * Owner node is locked.
1757 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001758static void tipc_link_dup_rcv(struct net *net, struct tipc_link *l_ptr,
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001759 struct sk_buff *t_buf)
1760{
1761 struct sk_buff *buf;
1762
1763 if (!tipc_link_is_up(l_ptr))
1764 return;
1765
1766 buf = buf_extract(t_buf, INT_H_SIZE);
1767 if (buf == NULL) {
1768 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1769 return;
1770 }
1771
1772 /* Add buffer to deferred queue, if applicable: */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001773 link_handle_out_of_seq_msg(net, l_ptr, buf);
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001774}
1775
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001776/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1777 * Owner node is locked.
1778 */
1779static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1780 struct sk_buff *t_buf)
1781{
1782 struct tipc_msg *t_msg = buf_msg(t_buf);
1783 struct sk_buff *buf = NULL;
1784 struct tipc_msg *msg;
1785
1786 if (tipc_link_is_up(l_ptr))
1787 tipc_link_reset(l_ptr);
1788
1789 /* First failover packet? */
1790 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1791 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1792
1793 /* Should there be an inner packet? */
1794 if (l_ptr->exp_msg_count) {
1795 l_ptr->exp_msg_count--;
1796 buf = buf_extract(t_buf, INT_H_SIZE);
1797 if (buf == NULL) {
1798 pr_warn("%sno inner failover pkt\n", link_co_err);
1799 goto exit;
1800 }
1801 msg = buf_msg(buf);
1802
1803 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1804 kfree_skb(buf);
1805 buf = NULL;
1806 goto exit;
1807 }
1808 if (msg_user(msg) == MSG_FRAGMENTER) {
1809 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001810 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001811 }
1812 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001813exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001814 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1815 tipc_node_detach_link(l_ptr->owner, l_ptr);
1816 kfree(l_ptr);
1817 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001818 return buf;
1819}
1820
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001821/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001822 * via other link as result of a failover (ORIGINAL_MSG) or
1823 * a new active link (DUPLICATE_MSG). Failover packets are
1824 * returned to the active link for delivery upwards.
1825 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001827static int tipc_link_tunnel_rcv(struct net *net, struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001828 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001830 struct sk_buff *t_buf = *buf;
1831 struct tipc_link *l_ptr;
1832 struct tipc_msg *t_msg = buf_msg(t_buf);
1833 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001835 *buf = NULL;
1836
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001837 if (bearer_id >= MAX_BEARERS)
1838 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001839
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001840 l_ptr = n_ptr->links[bearer_id];
1841 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001844 if (msg_type(t_msg) == DUPLICATE_MSG)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001845 tipc_link_dup_rcv(net, l_ptr, t_buf);
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001846 else if (msg_type(t_msg) == ORIGINAL_MSG)
1847 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001848 else
1849 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001851 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001852 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853}
1854
1855/*
1856 * Bundler functionality:
1857 */
Ying Xue247f0f32014-02-18 16:06:46 +08001858void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859{
1860 u32 msgcount = msg_msgcnt(buf_msg(buf));
1861 u32 pos = INT_H_SIZE;
1862 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001863 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 while (msgcount--) {
1866 obuf = buf_extract(buf, pos);
1867 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001868 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001869 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001870 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001871 omsg = buf_msg(obuf);
1872 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001873 if (msg_isdata(omsg)) {
1874 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
1875 tipc_sk_mcast_rcv(obuf);
1876 else
1877 tipc_sk_rcv(obuf);
1878 } else if (msg_user(omsg) == CONN_MANAGER) {
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001879 tipc_sk_rcv(obuf);
1880 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1881 tipc_named_rcv(obuf);
1882 } else {
1883 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1884 kfree_skb(obuf);
1885 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001887 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888}
1889
Ying Xue2f55c432015-01-09 15:27:00 +08001890static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891{
Ying Xue2f55c432015-01-09 15:27:00 +08001892 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
1893
1894 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
Allan Stephens5413b4c2011-01-18 13:24:55 -05001895 return;
1896
Ying Xue2f55c432015-01-09 15:27:00 +08001897 l_ptr->tolerance = tol;
1898 l_ptr->cont_intv = msecs_to_jiffies(intv);
1899 l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900}
1901
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001902void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903{
1904 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001905 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1906 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1907 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1908 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001910 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1911 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1912 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1913 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1916 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1917 /* FRAGMENT and LAST_FRAGMENT packets */
1918 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1919}
1920
Jon Paul Maloye099e862014-02-13 17:29:18 -05001921/* tipc_link_find_owner - locate owner node of link by link's name
1922 * @name: pointer to link name string
1923 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001924 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001925 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001926 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001927static struct tipc_node *tipc_link_find_owner(const char *link_name,
1928 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001930 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001931 struct tipc_node *n_ptr;
Fabian Frederick886eaa12014-12-25 12:05:50 +01001932 struct tipc_node *found_node = NULL;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001933 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934
Jon Paul Maloye099e862014-02-13 17:29:18 -05001935 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001936 rcu_read_lock();
1937 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001938 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001939 for (i = 0; i < MAX_BEARERS; i++) {
1940 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001941 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1942 *bearer_id = i;
1943 found_node = n_ptr;
1944 break;
1945 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001946 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001947 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001948 if (found_node)
1949 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001950 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001951 rcu_read_unlock();
1952
Jon Paul Maloye099e862014-02-13 17:29:18 -05001953 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954}
1955
Allan Stephens5c216e12011-10-18 11:34:29 -04001956/**
1957 * link_value_is_valid -- validate proposed link tolerance/priority/window
1958 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001959 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1960 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04001961 *
1962 * Returns 1 if value is within range, 0 if not.
1963 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001964static int link_value_is_valid(u16 cmd, u32 new_value)
1965{
1966 switch (cmd) {
1967 case TIPC_CMD_SET_LINK_TOL:
1968 return (new_value >= TIPC_MIN_LINK_TOL) &&
1969 (new_value <= TIPC_MAX_LINK_TOL);
1970 case TIPC_CMD_SET_LINK_PRI:
1971 return (new_value <= TIPC_MAX_LINK_PRI);
1972 case TIPC_CMD_SET_LINK_WINDOW:
1973 return (new_value >= TIPC_MIN_LINK_WIN) &&
1974 (new_value <= TIPC_MAX_LINK_WIN);
1975 }
1976 return 0;
1977}
1978
Allan Stephens5c216e12011-10-18 11:34:29 -04001979/**
1980 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00001981 * @name: ptr to link, bearer, or media name
1982 * @new_value: new value of link, bearer, or media setting
1983 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04001984 *
Ying Xue7216cd92014-04-21 10:55:48 +08001985 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04001986 *
1987 * Returns 0 if value updated and negative value on error.
1988 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001989static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
1990{
1991 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001992 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04001993 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05001994 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001995 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02001996 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04001997
Jon Paul Maloye099e862014-02-13 17:29:18 -05001998 node = tipc_link_find_owner(name, &bearer_id);
1999 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002000 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002001 l_ptr = node->links[bearer_id];
2002
2003 if (l_ptr) {
2004 switch (cmd) {
2005 case TIPC_CMD_SET_LINK_TOL:
2006 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002007 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2008 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002009 break;
2010 case TIPC_CMD_SET_LINK_PRI:
2011 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002012 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2013 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002014 break;
2015 case TIPC_CMD_SET_LINK_WINDOW:
2016 tipc_link_set_queue_limits(l_ptr, new_value);
2017 break;
2018 default:
2019 res = -EINVAL;
2020 break;
2021 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002022 }
2023 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002024 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002025 }
2026
2027 b_ptr = tipc_bearer_find(name);
2028 if (b_ptr) {
2029 switch (cmd) {
2030 case TIPC_CMD_SET_LINK_TOL:
2031 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002032 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002033 case TIPC_CMD_SET_LINK_PRI:
2034 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002035 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002036 case TIPC_CMD_SET_LINK_WINDOW:
2037 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002038 break;
2039 default:
2040 res = -EINVAL;
2041 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002042 }
Ying Xue636c0372013-10-18 07:23:20 +02002043 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002044 }
2045
2046 m_ptr = tipc_media_find(name);
2047 if (!m_ptr)
2048 return -ENODEV;
2049 switch (cmd) {
2050 case TIPC_CMD_SET_LINK_TOL:
2051 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002052 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002053 case TIPC_CMD_SET_LINK_PRI:
2054 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002055 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002056 case TIPC_CMD_SET_LINK_WINDOW:
2057 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002058 break;
2059 default:
2060 res = -EINVAL;
2061 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002062 }
Ying Xue636c0372013-10-18 07:23:20 +02002063 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002064}
2065
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002066struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002067 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068{
2069 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002070 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002071 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072
2073 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002074 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075
2076 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2077 new_value = ntohl(args->value);
2078
Allan Stephens5c216e12011-10-18 11:34:29 -04002079 if (!link_value_is_valid(cmd, new_value))
2080 return tipc_cfg_reply_error_string(
2081 "cannot change, value invalid");
2082
Per Liden4323add2006-01-18 00:38:21 +01002083 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002085 (tipc_bclink_set_queue_limits(new_value) == 0))
2086 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002087 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002088 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 }
2090
Allan Stephens5c216e12011-10-18 11:34:29 -04002091 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002093 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094
Per Liden4323add2006-01-18 00:38:21 +01002095 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096}
2097
2098/**
2099 * link_reset_statistics - reset link statistics
2100 * @l_ptr: pointer to link
2101 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002102static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103{
2104 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2105 l_ptr->stats.sent_info = l_ptr->next_out_no;
2106 l_ptr->stats.recv_info = l_ptr->next_in_no;
2107}
2108
Per Liden4323add2006-01-18 00:38:21 +01002109struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002110{
2111 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002112 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002113 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002114 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115
2116 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002117 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002118
2119 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002120 if (!strcmp(link_name, tipc_bclink_name)) {
2121 if (tipc_bclink_reset_stats())
2122 return tipc_cfg_reply_error_string("link not found");
2123 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002125 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002126 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002127 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002128
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002129 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002130 l_ptr = node->links[bearer_id];
2131 if (!l_ptr) {
2132 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002133 return tipc_cfg_reply_error_string("link not found");
2134 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002136 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002137 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002138}
2139
2140/**
2141 * percent - convert count to a percentage of total (rounding up or down)
2142 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143static u32 percent(u32 count, u32 total)
2144{
2145 return (count * 100 + (total / 2)) / total;
2146}
2147
2148/**
Per Liden4323add2006-01-18 00:38:21 +01002149 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 * @name: link name
2151 * @buf: print buffer area
2152 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002153 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002154 * Returns length of print buffer data string (or 0 if error)
2155 */
Per Liden4323add2006-01-18 00:38:21 +01002156static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002158 struct tipc_link *l;
2159 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002160 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161 char *status;
2162 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002163 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002164 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002165
Per Liden4323add2006-01-18 00:38:21 +01002166 if (!strcmp(name, tipc_bclink_name))
2167 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168
Jon Paul Maloye099e862014-02-13 17:29:18 -05002169 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002170 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002172
Per Liden4323add2006-01-18 00:38:21 +01002173 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002174
2175 l = node->links[bearer_id];
2176 if (!l) {
2177 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002178 return 0;
2179 }
2180
Erik Hugnedc1aed32012-06-29 00:50:23 -04002181 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002182
Erik Hugnedc1aed32012-06-29 00:50:23 -04002183 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002185 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002186 status = "STANDBY";
2187 else
2188 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002189
2190 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2191 " %s MTU:%u Priority:%u Tolerance:%u ms"
2192 " Window:%u packets\n",
2193 l->name, status, l->max_pkt, l->priority,
2194 l->tolerance, l->queue_limit[0]);
2195
2196 ret += tipc_snprintf(buf + ret, buf_size - ret,
2197 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2198 l->next_in_no - s->recv_info, s->recv_fragments,
2199 s->recv_fragmented, s->recv_bundles,
2200 s->recv_bundled);
2201
2202 ret += tipc_snprintf(buf + ret, buf_size - ret,
2203 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2204 l->next_out_no - s->sent_info, s->sent_fragments,
2205 s->sent_fragmented, s->sent_bundles,
2206 s->sent_bundled);
2207
2208 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002209 if (!profile_total)
2210 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002211
2212 ret += tipc_snprintf(buf + ret, buf_size - ret,
2213 " TX profile sample:%u packets average:%u octets\n"
2214 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2215 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2216 s->msg_length_counts,
2217 s->msg_lengths_total / profile_total,
2218 percent(s->msg_length_profile[0], profile_total),
2219 percent(s->msg_length_profile[1], profile_total),
2220 percent(s->msg_length_profile[2], profile_total),
2221 percent(s->msg_length_profile[3], profile_total),
2222 percent(s->msg_length_profile[4], profile_total),
2223 percent(s->msg_length_profile[5], profile_total),
2224 percent(s->msg_length_profile[6], profile_total));
2225
2226 ret += tipc_snprintf(buf + ret, buf_size - ret,
2227 " RX states:%u probes:%u naks:%u defs:%u"
2228 " dups:%u\n", s->recv_states, s->recv_probes,
2229 s->recv_nacks, s->deferred_recv, s->duplicates);
2230
2231 ret += tipc_snprintf(buf + ret, buf_size - ret,
2232 " TX states:%u probes:%u naks:%u acks:%u"
2233 " dups:%u\n", s->sent_states, s->sent_probes,
2234 s->sent_nacks, s->sent_acks, s->retransmitted);
2235
2236 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002237 " Congestion link:%u Send queue"
2238 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002239 s->max_queue_sz, s->queue_sz_counts ?
2240 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002241
Per Liden4323add2006-01-18 00:38:21 +01002242 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002243 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002244}
2245
Per Liden4323add2006-01-18 00:38:21 +01002246struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002247{
2248 struct sk_buff *buf;
2249 struct tlv_desc *rep_tlv;
2250 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002251 int pb_len;
2252 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002253
2254 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002255 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002256
Erik Hugnedc1aed32012-06-29 00:50:23 -04002257 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002258 if (!buf)
2259 return NULL;
2260
2261 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002262 pb = TLV_DATA(rep_tlv);
2263 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002264 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002265 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002266 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002267 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002268 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002269 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002270 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002271 skb_put(buf, TLV_SPACE(str_len));
2272 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2273
2274 return buf;
2275}
2276
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002277static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002278{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002279 struct tipc_bearer *b_ptr;
2280
2281 rcu_read_lock();
2282 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2283 if (b_ptr)
2284 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2285 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002286
Per Lidenb97bf3f2006-01-02 19:04:38 +01002287 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002288 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002289 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002290 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002291 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002292 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002293 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002294 pr_cont(":WW\n");
2295 else
2296 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002297}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002298
2299/* Parse and validate nested (link) properties valid for media, bearer and link
2300 */
2301int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2302{
2303 int err;
2304
2305 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2306 tipc_nl_prop_policy);
2307 if (err)
2308 return err;
2309
2310 if (props[TIPC_NLA_PROP_PRIO]) {
2311 u32 prio;
2312
2313 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2314 if (prio > TIPC_MAX_LINK_PRI)
2315 return -EINVAL;
2316 }
2317
2318 if (props[TIPC_NLA_PROP_TOL]) {
2319 u32 tol;
2320
2321 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2322 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2323 return -EINVAL;
2324 }
2325
2326 if (props[TIPC_NLA_PROP_WIN]) {
2327 u32 win;
2328
2329 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2330 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2331 return -EINVAL;
2332 }
2333
2334 return 0;
2335}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002336
Richard Alpef96ce7a2014-11-20 10:29:13 +01002337int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2338{
2339 int err;
2340 int res = 0;
2341 int bearer_id;
2342 char *name;
2343 struct tipc_link *link;
2344 struct tipc_node *node;
2345 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2346
2347 if (!info->attrs[TIPC_NLA_LINK])
2348 return -EINVAL;
2349
2350 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2351 info->attrs[TIPC_NLA_LINK],
2352 tipc_nl_link_policy);
2353 if (err)
2354 return err;
2355
2356 if (!attrs[TIPC_NLA_LINK_NAME])
2357 return -EINVAL;
2358
2359 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2360
2361 node = tipc_link_find_owner(name, &bearer_id);
2362 if (!node)
2363 return -EINVAL;
2364
2365 tipc_node_lock(node);
2366
2367 link = node->links[bearer_id];
2368 if (!link) {
2369 res = -EINVAL;
2370 goto out;
2371 }
2372
2373 if (attrs[TIPC_NLA_LINK_PROP]) {
2374 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2375
2376 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2377 props);
2378 if (err) {
2379 res = err;
2380 goto out;
2381 }
2382
2383 if (props[TIPC_NLA_PROP_TOL]) {
2384 u32 tol;
2385
2386 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2387 link_set_supervision_props(link, tol);
2388 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2389 }
2390 if (props[TIPC_NLA_PROP_PRIO]) {
2391 u32 prio;
2392
2393 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2394 link->priority = prio;
2395 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2396 }
2397 if (props[TIPC_NLA_PROP_WIN]) {
2398 u32 win;
2399
2400 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2401 tipc_link_set_queue_limits(link, win);
2402 }
2403 }
2404
2405out:
2406 tipc_node_unlock(node);
2407
2408 return res;
2409}
Richard Alped8182802014-11-24 11:10:29 +01002410
2411static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002412{
2413 int i;
2414 struct nlattr *stats;
2415
2416 struct nla_map {
2417 u32 key;
2418 u32 val;
2419 };
2420
2421 struct nla_map map[] = {
2422 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2423 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2424 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2425 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2426 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2427 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2428 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2429 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2430 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2431 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2432 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2433 s->msg_length_counts : 1},
2434 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2435 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2436 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2437 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2438 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2439 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2440 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2441 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2442 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2443 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2444 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2445 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2446 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2447 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2448 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2449 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2450 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2451 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2452 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2453 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2454 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2455 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2456 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2457 };
2458
2459 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2460 if (!stats)
2461 return -EMSGSIZE;
2462
2463 for (i = 0; i < ARRAY_SIZE(map); i++)
2464 if (nla_put_u32(skb, map[i].key, map[i].val))
2465 goto msg_full;
2466
2467 nla_nest_end(skb, stats);
2468
2469 return 0;
2470msg_full:
2471 nla_nest_cancel(skb, stats);
2472
2473 return -EMSGSIZE;
2474}
2475
2476/* Caller should hold appropriate locks to protect the link */
Richard Alped8182802014-11-24 11:10:29 +01002477static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002478{
2479 int err;
2480 void *hdr;
2481 struct nlattr *attrs;
2482 struct nlattr *prop;
2483
2484 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2485 NLM_F_MULTI, TIPC_NL_LINK_GET);
2486 if (!hdr)
2487 return -EMSGSIZE;
2488
2489 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2490 if (!attrs)
2491 goto msg_full;
2492
2493 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2494 goto attr_msg_full;
2495 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2496 tipc_cluster_mask(tipc_own_addr)))
2497 goto attr_msg_full;
2498 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2499 goto attr_msg_full;
2500 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2501 goto attr_msg_full;
2502 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2503 goto attr_msg_full;
2504
2505 if (tipc_link_is_up(link))
2506 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2507 goto attr_msg_full;
2508 if (tipc_link_is_active(link))
2509 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2510 goto attr_msg_full;
2511
2512 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2513 if (!prop)
2514 goto attr_msg_full;
2515 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2516 goto prop_msg_full;
2517 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2518 goto prop_msg_full;
2519 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2520 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2521 goto prop_msg_full;
2522 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2523 goto prop_msg_full;
2524 nla_nest_end(msg->skb, prop);
2525
2526 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2527 if (err)
2528 goto attr_msg_full;
2529
2530 nla_nest_end(msg->skb, attrs);
2531 genlmsg_end(msg->skb, hdr);
2532
2533 return 0;
2534
2535prop_msg_full:
2536 nla_nest_cancel(msg->skb, prop);
2537attr_msg_full:
2538 nla_nest_cancel(msg->skb, attrs);
2539msg_full:
2540 genlmsg_cancel(msg->skb, hdr);
2541
2542 return -EMSGSIZE;
2543}
2544
2545/* Caller should hold node lock */
Richard Alped8182802014-11-24 11:10:29 +01002546static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
2547 struct tipc_node *node,
2548 u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002549{
2550 u32 i;
2551 int err;
2552
2553 for (i = *prev_link; i < MAX_BEARERS; i++) {
2554 *prev_link = i;
2555
2556 if (!node->links[i])
2557 continue;
2558
2559 err = __tipc_nl_add_link(msg, node->links[i]);
2560 if (err)
2561 return err;
2562 }
2563 *prev_link = 0;
2564
2565 return 0;
2566}
2567
2568int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2569{
2570 struct tipc_node *node;
2571 struct tipc_nl_msg msg;
2572 u32 prev_node = cb->args[0];
2573 u32 prev_link = cb->args[1];
2574 int done = cb->args[2];
2575 int err;
2576
2577 if (done)
2578 return 0;
2579
2580 msg.skb = skb;
2581 msg.portid = NETLINK_CB(cb->skb).portid;
2582 msg.seq = cb->nlh->nlmsg_seq;
2583
2584 rcu_read_lock();
2585
2586 if (prev_node) {
2587 node = tipc_node_find(prev_node);
2588 if (!node) {
2589 /* We never set seq or call nl_dump_check_consistent()
2590 * this means that setting prev_seq here will cause the
2591 * consistence check to fail in the netlink callback
2592 * handler. Resulting in the last NLMSG_DONE message
2593 * having the NLM_F_DUMP_INTR flag set.
2594 */
2595 cb->prev_seq = 1;
2596 goto out;
2597 }
2598
2599 list_for_each_entry_continue_rcu(node, &tipc_node_list, list) {
2600 tipc_node_lock(node);
2601 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2602 tipc_node_unlock(node);
2603 if (err)
2604 goto out;
2605
2606 prev_node = node->addr;
2607 }
2608 } else {
2609 err = tipc_nl_add_bc_link(&msg);
2610 if (err)
2611 goto out;
2612
2613 list_for_each_entry_rcu(node, &tipc_node_list, list) {
2614 tipc_node_lock(node);
2615 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2616 tipc_node_unlock(node);
2617 if (err)
2618 goto out;
2619
2620 prev_node = node->addr;
2621 }
2622 }
2623 done = 1;
2624out:
2625 rcu_read_unlock();
2626
2627 cb->args[0] = prev_node;
2628 cb->args[1] = prev_link;
2629 cb->args[2] = done;
2630
2631 return skb->len;
2632}
2633
2634int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2635{
2636 struct sk_buff *ans_skb;
2637 struct tipc_nl_msg msg;
2638 struct tipc_link *link;
2639 struct tipc_node *node;
2640 char *name;
2641 int bearer_id;
2642 int err;
2643
2644 if (!info->attrs[TIPC_NLA_LINK_NAME])
2645 return -EINVAL;
2646
2647 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
2648 node = tipc_link_find_owner(name, &bearer_id);
2649 if (!node)
2650 return -EINVAL;
2651
2652 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2653 if (!ans_skb)
2654 return -ENOMEM;
2655
2656 msg.skb = ans_skb;
2657 msg.portid = info->snd_portid;
2658 msg.seq = info->snd_seq;
2659
2660 tipc_node_lock(node);
2661 link = node->links[bearer_id];
2662 if (!link) {
2663 err = -EINVAL;
2664 goto err_out;
2665 }
2666
2667 err = __tipc_nl_add_link(&msg, link);
2668 if (err)
2669 goto err_out;
2670
2671 tipc_node_unlock(node);
2672
2673 return genlmsg_reply(ans_skb, info);
2674
2675err_out:
2676 tipc_node_unlock(node);
2677 nlmsg_free(ans_skb);
2678
2679 return err;
2680}
Richard Alpeae363422014-11-20 10:29:14 +01002681
2682int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2683{
2684 int err;
2685 char *link_name;
2686 unsigned int bearer_id;
2687 struct tipc_link *link;
2688 struct tipc_node *node;
2689 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2690
2691 if (!info->attrs[TIPC_NLA_LINK])
2692 return -EINVAL;
2693
2694 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2695 info->attrs[TIPC_NLA_LINK],
2696 tipc_nl_link_policy);
2697 if (err)
2698 return err;
2699
2700 if (!attrs[TIPC_NLA_LINK_NAME])
2701 return -EINVAL;
2702
2703 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2704
2705 if (strcmp(link_name, tipc_bclink_name) == 0) {
2706 err = tipc_bclink_reset_stats();
2707 if (err)
2708 return err;
2709 return 0;
2710 }
2711
2712 node = tipc_link_find_owner(link_name, &bearer_id);
2713 if (!node)
2714 return -EINVAL;
2715
2716 tipc_node_lock(node);
2717
2718 link = node->links[bearer_id];
2719 if (!link) {
2720 tipc_node_unlock(node);
2721 return -EINVAL;
2722 }
2723
2724 link_reset_statistics(link);
2725
2726 tipc_node_unlock(node);
2727
2728 return 0;
2729}