blob: 193bc1560677a4237fdc9304f3e4ac38f3f7b1b3 [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);
Ying Xuef2f98002015-01-09 15:27:05 +0800117static int tipc_link_input(struct net *net, struct tipc_link *l,
118 struct sk_buff *buf);
Ying Xuec93d3ba2015-01-09 15:27:04 +0800119static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
120 struct sk_buff **buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000121
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800123 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800125static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126{
127 return (i + 3) & ~3u;
128}
129
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500130static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800132 struct tipc_node *node = l_ptr->owner;
133 struct tipc_net *tn = net_generic(node->net, tipc_net_id);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800134 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900136
Ying Xue7a2f7d12014-04-21 10:55:46 +0800137 rcu_read_lock();
Ying Xue7f9f95d2015-01-09 15:27:06 +0800138 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800139 if (!b_ptr) {
140 rcu_read_unlock();
141 return;
142 }
143 max_pkt = (b_ptr->mtu & ~3);
144 rcu_read_unlock();
145
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 if (max_pkt > MAX_MSG_SIZE)
147 max_pkt = MAX_MSG_SIZE;
148
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900149 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
151 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 l_ptr->max_pkt = MAX_PKT_DEFAULT;
154
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900155 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156}
157
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800159 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500161int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162{
163 if (!l_ptr)
164 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000165 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166}
167
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500168int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000170 return (l_ptr->owner->active_links[0] == l_ptr) ||
171 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172}
173
174/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 * link_timeout - handle expiration of link timer
176 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 */
Ying Xue2f55c432015-01-09 15:27:00 +0800178static void link_timeout(unsigned long data)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179{
Ying Xue2f55c432015-01-09 15:27:00 +0800180 struct tipc_link *l_ptr = (struct tipc_link *)data;
Ying Xue58dc55f2014-11-26 11:41:52 +0800181 struct sk_buff *skb;
182
Per Liden4323add2006-01-18 00:38:21 +0100183 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184
185 /* update counters used in statistical profiling of send traffic */
Ying Xue58dc55f2014-11-26 11:41:52 +0800186 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->outqueue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 l_ptr->stats.queue_sz_counts++;
188
Ying Xue58dc55f2014-11-26 11:41:52 +0800189 skb = skb_peek(&l_ptr->outqueue);
190 if (skb) {
191 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 u32 length = msg_size(msg);
193
Joe Perchesf64f9e72009-11-29 16:55:45 -0800194 if ((msg_user(msg) == MSG_FRAGMENTER) &&
195 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 length = msg_size(msg_get_wrapped(msg));
197 }
198 if (length) {
199 l_ptr->stats.msg_lengths_total += length;
200 l_ptr->stats.msg_length_counts++;
201 if (length <= 64)
202 l_ptr->stats.msg_length_profile[0]++;
203 else if (length <= 256)
204 l_ptr->stats.msg_length_profile[1]++;
205 else if (length <= 1024)
206 l_ptr->stats.msg_length_profile[2]++;
207 else if (length <= 4096)
208 l_ptr->stats.msg_length_profile[3]++;
209 else if (length <= 16384)
210 l_ptr->stats.msg_length_profile[4]++;
211 else if (length <= 32768)
212 l_ptr->stats.msg_length_profile[5]++;
213 else
214 l_ptr->stats.msg_length_profile[6]++;
215 }
216 }
217
218 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 link_state_event(l_ptr, TIMEOUT_EVT);
220
221 if (l_ptr->next_out)
Ying Xue47b4c9a2014-11-26 11:41:48 +0800222 tipc_link_push_packets(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223
Per Liden4323add2006-01-18 00:38:21 +0100224 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225}
226
Ying Xue2f55c432015-01-09 15:27:00 +0800227static void link_set_timer(struct tipc_link *link, unsigned long time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228{
Ying Xue2f55c432015-01-09 15:27:00 +0800229 mod_timer(&link->timer, jiffies + time);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230}
231
232/**
Per Liden4323add2006-01-18 00:38:21 +0100233 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500234 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900237 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 * Returns pointer to link.
239 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500240struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500241 struct tipc_bearer *b_ptr,
242 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243{
Ying Xue34747532015-01-09 15:27:10 +0800244 struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500245 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 struct tipc_msg *msg;
247 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500248 char addr_string[16];
249 u32 peer = n_ptr->addr;
250
Holger Brunck0372bf52014-11-14 18:33:19 +0100251 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500252 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100253 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
254 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500255 return NULL;
256 }
257
258 if (n_ptr->links[b_ptr->identity]) {
259 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400260 pr_err("Attempt to establish second link on <%s> to %s\n",
261 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500262 return NULL;
263 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700265 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400267 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 return NULL;
269 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270
271 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500272 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400273 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Ying Xue34747532015-01-09 15:27:10 +0800274 tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
275 tipc_node(tn->own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 if_name,
277 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400278 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500280 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 l_ptr->checkpoint = 1;
Allan Stephensf882cb72011-04-07 09:43:27 -0400282 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800283 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400284 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 l_ptr->state = RESET_UNKNOWN;
286
287 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
288 msg = l_ptr->pmsg;
Ying Xue34747532015-01-09 15:27:10 +0800289 tipc_msg_init(n_ptr->net, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
290 l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Ying Xuebafa29e2015-01-09 15:27:12 +0800292 msg_set_session(msg, (tn->random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 msg_set_bearer_id(msg, b_ptr->identity);
294 strcpy((char *)msg_data(msg), if_name);
295
296 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400297 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298
Ying Xue7a2f7d12014-04-21 10:55:46 +0800299 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 link_init_max_pkt(l_ptr);
301
302 l_ptr->next_out_no = 1;
Ying Xue58dc55f2014-11-26 11:41:52 +0800303 __skb_queue_head_init(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800304 __skb_queue_head_init(&l_ptr->deferred_queue);
Richard Alpe340b6e52014-12-10 09:46:54 +0100305 skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306
307 link_reset_statistics(l_ptr);
308
Allan Stephens37b9c082011-02-28 11:32:27 -0500309 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310
Ying Xue2f55c432015-01-09 15:27:00 +0800311 setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500312
313 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 return l_ptr;
316}
317
Ying Xuef2f98002015-01-09 15:27:05 +0800318void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
319 bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500320{
Ying Xuef2f98002015-01-09 15:27:05 +0800321 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue8d8439b2014-02-13 17:29:07 -0500322 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500323 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500324
Ying Xue6c7a7622014-03-27 12:54:37 +0800325 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +0800326 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800327 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500328 l_ptr = n_ptr->links[bearer_id];
329 if (l_ptr) {
330 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500331 if (shutting_down || !tipc_node_is_up(n_ptr)) {
332 tipc_node_detach_link(l_ptr->owner, l_ptr);
333 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800334 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500335
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500336 /* Nobody else can access this link now: */
337 del_timer_sync(&l_ptr->timer);
338 kfree(l_ptr);
339 } else {
340 /* Detach/delete when failover is finished: */
341 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800342 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500343 del_timer_sync(&l_ptr->timer);
344 }
Ying Xuec61dd612014-02-13 17:29:09 -0500345 continue;
346 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800347 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500348 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800349 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500350}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351
352/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400353 * link_schedule_user - schedule user for wakeup after congestion
354 * @link: congested link
355 * @oport: sending port
356 * @chain_sz: size of buffer chain that was attempted sent
357 * @imp: importance of message attempted sent
358 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400360static bool link_schedule_user(struct tipc_link *link, u32 oport,
361 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362{
Ying Xue34747532015-01-09 15:27:10 +0800363 struct net *net = link->owner->net;
364 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400365 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366
Ying Xue34747532015-01-09 15:27:10 +0800367 buf = tipc_msg_create(net, SOCK_WAKEUP, 0, INT_H_SIZE, 0, tn->own_addr,
368 tn->own_addr, oport, 0, 0);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400369 if (!buf)
370 return false;
371 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
372 TIPC_SKB_CB(buf)->chain_imp = imp;
Richard Alpe340b6e52014-12-10 09:46:54 +0100373 skb_queue_tail(&link->waiting_sks, buf);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400374 link->stats.link_congs++;
375 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376}
377
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400378/**
379 * link_prepare_wakeup - prepare users for wakeup after congestion
380 * @link: congested link
381 * Move a number of waiting users, as permitted by available space in
382 * the send queue, from link wait queue to node wait queue for wakeup
383 */
384static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385{
Ying Xue58dc55f2014-11-26 11:41:52 +0800386 uint pend_qsz = skb_queue_len(&link->outqueue);
Ying Xue58d78b32014-11-26 11:41:51 +0800387 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388
Ying Xue58d78b32014-11-26 11:41:51 +0800389 skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
390 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 break;
Ying Xue58d78b32014-11-26 11:41:51 +0800392 pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
Richard Alpe340b6e52014-12-10 09:46:54 +0100393 skb_unlink(skb, &link->waiting_sks);
394 skb_queue_tail(&link->owner->waiting_sks, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396}
397
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900398/**
Per Liden4323add2006-01-18 00:38:21 +0100399 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 * @l_ptr: pointer to link
401 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500402void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400404 kfree_skb(l_ptr->reasm_buf);
405 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406}
407
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900408/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500409 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 * @l_ptr: pointer to link
411 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500412void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413{
Ying Xuebc6fecd2014-11-26 11:41:53 +0800414 __skb_queue_purge(&l_ptr->deferred_queue);
Ying Xue58dc55f2014-11-26 11:41:52 +0800415 __skb_queue_purge(&l_ptr->outqueue);
Per Liden4323add2006-01-18 00:38:21 +0100416 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417}
418
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500419void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 u32 prev_state = l_ptr->state;
422 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700423 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400424 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425
Allan Stephensa686e682008-06-04 17:29:39 -0700426 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427
Allan Stephensa686e682008-06-04 17:29:39 -0700428 /* Link is down, accept any session */
429 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900431 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900433
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435
436 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
437 return;
438
Per Liden4323add2006-01-18 00:38:21 +0100439 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800440 tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000441
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500442 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 l_ptr->reset_checkpoint = checkpoint;
444 l_ptr->exp_msg_count = START_CHANGEOVER;
445 }
446
447 /* Clean up all queues: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800448 __skb_queue_purge(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800449 __skb_queue_purge(&l_ptr->deferred_queue);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400450 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
451 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
452 owner->action_flags |= TIPC_WAKEUP_USERS;
453 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 l_ptr->next_out = NULL;
455 l_ptr->unacked_window = 0;
456 l_ptr->checkpoint = 1;
457 l_ptr->next_out_no = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 l_ptr->fsm_msg_cnt = 0;
459 l_ptr->stale_count = 0;
460 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461}
462
Ying Xuef2f98002015-01-09 15:27:05 +0800463void tipc_link_reset_list(struct net *net, unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500464{
Ying Xuef2f98002015-01-09 15:27:05 +0800465 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500466 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500467 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500468
Ying Xue6c7a7622014-03-27 12:54:37 +0800469 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +0800470 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800471 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500472 l_ptr = n_ptr->links[bearer_id];
473 if (l_ptr)
474 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800475 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500476 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800477 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500478}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
Ying Xue7f9f95d2015-01-09 15:27:06 +0800480static void link_activate(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800482 struct tipc_node *node = link->owner;
483
484 link->next_in_no = 1;
485 link->stats.recv_info = 1;
486 tipc_node_link_up(node, link);
487 tipc_bearer_add_dest(node->net, link->bearer_id, link->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488}
489
490/**
491 * link_state_event - link finite state machine
492 * @l_ptr: pointer to link
493 * @event: state machine event to process
494 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000495static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500497 struct tipc_link *other;
Ying Xue2f55c432015-01-09 15:27:00 +0800498 unsigned long cont_intv = l_ptr->cont_intv;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500500 if (l_ptr->flags & LINK_STOPPED)
501 return;
502
Ying Xue135daee2014-02-13 17:29:08 -0500503 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 return; /* Not yet. */
505
Ying Xue77a7e072013-12-10 20:45:44 -0800506 /* Check whether changeover is going on */
507 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000508 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800510 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512
513 switch (l_ptr->state) {
514 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 switch (event) {
516 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 break;
519 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 if (l_ptr->next_in_no != l_ptr->checkpoint) {
521 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100522 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800523 tipc_link_proto_xmit(l_ptr, STATE_MSG,
524 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 l_ptr->fsm_msg_cnt++;
526 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800527 tipc_link_proto_xmit(l_ptr, STATE_MSG,
528 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 l_ptr->fsm_msg_cnt++;
530 }
531 link_set_timer(l_ptr, cont_intv);
532 break;
533 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 l_ptr->state = WORKING_UNKNOWN;
535 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800536 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 l_ptr->fsm_msg_cnt++;
538 link_set_timer(l_ptr, cont_intv / 4);
539 break;
540 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400541 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
542 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100543 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 l_ptr->state = RESET_RESET;
545 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800546 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
547 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 l_ptr->fsm_msg_cnt++;
549 link_set_timer(l_ptr, cont_intv);
550 break;
551 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400552 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 }
554 break;
555 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 switch (event) {
557 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 l_ptr->state = WORKING_WORKING;
560 l_ptr->fsm_msg_cnt = 0;
561 link_set_timer(l_ptr, cont_intv);
562 break;
563 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400564 pr_info("%s<%s>, requested by peer while probing\n",
565 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100566 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 l_ptr->state = RESET_RESET;
568 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800569 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
570 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 l_ptr->fsm_msg_cnt++;
572 link_set_timer(l_ptr, cont_intv);
573 break;
574 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 l_ptr->state = WORKING_WORKING;
577 l_ptr->fsm_msg_cnt = 0;
578 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100579 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800580 tipc_link_proto_xmit(l_ptr, STATE_MSG,
581 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 l_ptr->fsm_msg_cnt++;
583 }
584 link_set_timer(l_ptr, cont_intv);
585 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800586 tipc_link_proto_xmit(l_ptr, STATE_MSG,
587 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 l_ptr->fsm_msg_cnt++;
589 link_set_timer(l_ptr, cont_intv / 4);
590 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400591 pr_warn("%s<%s>, peer not responding\n",
592 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100593 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 l_ptr->state = RESET_UNKNOWN;
595 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800596 tipc_link_proto_xmit(l_ptr, RESET_MSG,
597 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 l_ptr->fsm_msg_cnt++;
599 link_set_timer(l_ptr, cont_intv);
600 }
601 break;
602 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400603 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 }
605 break;
606 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 switch (event) {
608 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 break;
610 case ACTIVATE_MSG:
611 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000612 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 l_ptr->state = WORKING_WORKING;
615 l_ptr->fsm_msg_cnt = 0;
616 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800617 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800619 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800620 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 link_set_timer(l_ptr, cont_intv);
622 break;
623 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 l_ptr->state = RESET_RESET;
625 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800626 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
627 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 l_ptr->fsm_msg_cnt++;
629 link_set_timer(l_ptr, cont_intv);
630 break;
631 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500632 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 /* fall through */
634 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800635 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 l_ptr->fsm_msg_cnt++;
637 link_set_timer(l_ptr, cont_intv);
638 break;
639 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400640 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 }
642 break;
643 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 switch (event) {
645 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 case ACTIVATE_MSG:
647 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000648 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 l_ptr->state = WORKING_WORKING;
651 l_ptr->fsm_msg_cnt = 0;
652 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800653 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800655 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800656 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 link_set_timer(l_ptr, cont_intv);
658 break;
659 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 break;
661 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800662 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
663 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 l_ptr->fsm_msg_cnt++;
665 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 break;
667 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400668 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 }
670 break;
671 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400672 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 }
674}
675
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500676/* tipc_link_cong: determine return value and how to treat the
677 * sent buffer during link congestion.
678 * - For plain, errorless user data messages we keep the buffer and
679 * return -ELINKONG.
680 * - For all other messages we discard the buffer and return -EHOSTUNREACH
681 * - For TIPC internal messages we also reset the link
682 */
Ying Xuea6ca1092014-11-26 11:41:55 +0800683static int tipc_link_cong(struct tipc_link *link, struct sk_buff_head *list)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500684{
Ying Xuea6ca1092014-11-26 11:41:55 +0800685 struct sk_buff *skb = skb_peek(list);
686 struct tipc_msg *msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500687 uint imp = tipc_msg_tot_importance(msg);
688 u32 oport = msg_tot_origport(msg);
689
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400690 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500691 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
692 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400693 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500694 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400695 if (unlikely(msg_errcode(msg)))
696 goto drop;
697 if (unlikely(msg_reroute_cnt(msg)))
698 goto drop;
Ying Xuea6ca1092014-11-26 11:41:55 +0800699 if (TIPC_SKB_CB(skb)->wakeup_pending)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400700 return -ELINKCONG;
Ying Xuea6ca1092014-11-26 11:41:55 +0800701 if (link_schedule_user(link, oport, skb_queue_len(list), imp))
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400702 return -ELINKCONG;
703drop:
Ying Xuea6ca1092014-11-26 11:41:55 +0800704 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500705 return -EHOSTUNREACH;
706}
707
708/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400709 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500710 * @link: link to use
Ying Xuea6ca1092014-11-26 11:41:55 +0800711 * @list: chain of buffers containing message
712 *
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500713 * Consumes the buffer chain, except when returning -ELINKCONG
714 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
715 * user data messages) or -EHOSTUNREACH (all other messages/senders)
716 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
717 * to act on the return value, since they may need to do more send attempts.
718 */
Ying Xue7f9f95d2015-01-09 15:27:06 +0800719int __tipc_link_xmit(struct net *net, struct tipc_link *link,
720 struct sk_buff_head *list)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500721{
Ying Xuea6ca1092014-11-26 11:41:55 +0800722 struct tipc_msg *msg = buf_msg(skb_peek(list));
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500723 uint psz = msg_size(msg);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500724 uint sndlim = link->queue_limit[0];
725 uint imp = tipc_msg_tot_importance(msg);
726 uint mtu = link->max_pkt;
727 uint ack = mod(link->next_in_no - 1);
728 uint seqno = link->next_out_no;
729 uint bc_last_in = link->owner->bclink.last_in;
730 struct tipc_media_addr *addr = &link->media_addr;
Ying Xue58dc55f2014-11-26 11:41:52 +0800731 struct sk_buff_head *outqueue = &link->outqueue;
Ying Xuea6ca1092014-11-26 11:41:55 +0800732 struct sk_buff *skb, *tmp;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500733
734 /* Match queue limits against msg importance: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800735 if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp]))
Ying Xuea6ca1092014-11-26 11:41:55 +0800736 return tipc_link_cong(link, list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500737
738 /* Has valid packet limit been used ? */
739 if (unlikely(psz > mtu)) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800740 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500741 return -EMSGSIZE;
742 }
743
744 /* Prepare each packet for sending, and add to outqueue: */
Ying Xuea6ca1092014-11-26 11:41:55 +0800745 skb_queue_walk_safe(list, skb, tmp) {
746 __skb_unlink(skb, list);
Ying Xue58dc55f2014-11-26 11:41:52 +0800747 msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500748 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
749 msg_set_bcast_ack(msg, bc_last_in);
750
Ying Xue58dc55f2014-11-26 11:41:52 +0800751 if (skb_queue_len(outqueue) < sndlim) {
752 __skb_queue_tail(outqueue, skb);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800753 tipc_bearer_send(net, link->bearer_id,
754 skb, addr);
Ying Xue58dc55f2014-11-26 11:41:52 +0800755 link->next_out = NULL;
756 link->unacked_window = 0;
757 } else if (tipc_msg_bundle(outqueue, skb, mtu)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500758 link->stats.sent_bundled++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500759 continue;
Ying Xue34747532015-01-09 15:27:10 +0800760 } else if (tipc_msg_make_bundle(net, outqueue, skb, mtu,
Ying Xue58dc55f2014-11-26 11:41:52 +0800761 link->addr)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500762 link->stats.sent_bundled++;
763 link->stats.sent_bundles++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500764 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800765 link->next_out = skb_peek_tail(outqueue);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500766 } else {
Ying Xue58dc55f2014-11-26 11:41:52 +0800767 __skb_queue_tail(outqueue, skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500768 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800769 link->next_out = skb;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500770 }
771 seqno++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500772 }
773 link->next_out_no = seqno;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500774 return 0;
775}
776
Ying Xuea6ca1092014-11-26 11:41:55 +0800777static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
778{
779 __skb_queue_head_init(list);
780 __skb_queue_tail(list, skb);
781}
782
783static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
784{
785 struct sk_buff_head head;
786
787 skb2list(skb, &head);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800788 return __tipc_link_xmit(link->owner->net, link, &head);
Ying Xuea6ca1092014-11-26 11:41:55 +0800789}
790
Ying Xuef2f98002015-01-09 15:27:05 +0800791int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
792 u32 selector)
Ying Xuea6ca1092014-11-26 11:41:55 +0800793{
794 struct sk_buff_head head;
795
796 skb2list(skb, &head);
Ying Xuef2f98002015-01-09 15:27:05 +0800797 return tipc_link_xmit(net, &head, dnode, selector);
Ying Xuea6ca1092014-11-26 11:41:55 +0800798}
799
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500800/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400801 * tipc_link_xmit() is the general link level function for message sending
Ying Xuef2f98002015-01-09 15:27:05 +0800802 * @net: the applicable net namespace
Ying Xuea6ca1092014-11-26 11:41:55 +0800803 * @list: chain of buffers containing message
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500804 * @dsz: amount of user data to be sent
805 * @dnode: address of destination node
806 * @selector: a number used for deterministic link selection
807 * Consumes the buffer chain, except when returning -ELINKCONG
808 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
809 */
Ying Xuef2f98002015-01-09 15:27:05 +0800810int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
811 u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500812{
813 struct tipc_link *link = NULL;
814 struct tipc_node *node;
815 int rc = -EHOSTUNREACH;
816
Ying Xuef2f98002015-01-09 15:27:05 +0800817 node = tipc_node_find(net, dnode);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500818 if (node) {
819 tipc_node_lock(node);
820 link = node->active_links[selector & 1];
821 if (link)
Ying Xue7f9f95d2015-01-09 15:27:06 +0800822 rc = __tipc_link_xmit(net, link, list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500823 tipc_node_unlock(node);
824 }
825
826 if (link)
827 return rc;
828
Ying Xue34747532015-01-09 15:27:10 +0800829 if (likely(in_own_node(net, dnode))) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800830 /* As a node local message chain never contains more than one
831 * buffer, we just need to dequeue one SKB buffer from the
832 * head list.
833 */
Ying Xuef2f98002015-01-09 15:27:05 +0800834 return tipc_sk_rcv(net, __skb_dequeue(list));
Ying Xuea6ca1092014-11-26 11:41:55 +0800835 }
836 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500837
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500838 return rc;
839}
840
Jon Maloyc64f7a62012-11-16 13:51:31 +0800841/*
Ying Xue247f0f32014-02-18 16:06:46 +0800842 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800843 *
844 * Give a newly added peer node the sequence number where it should
845 * start receiving and acking broadcast packets.
846 *
847 * Called with node locked
848 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400849static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800850{
Ying Xuea6ca1092014-11-26 11:41:55 +0800851 struct sk_buff *skb;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800852 struct tipc_msg *msg;
853
Ying Xuea6ca1092014-11-26 11:41:55 +0800854 skb = tipc_buf_acquire(INT_H_SIZE);
855 if (!skb)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800856 return;
857
Ying Xuea6ca1092014-11-26 11:41:55 +0800858 msg = buf_msg(skb);
Ying Xue34747532015-01-09 15:27:10 +0800859 tipc_msg_init(link->owner->net, msg, BCAST_PROTOCOL, STATE_MSG,
860 INT_H_SIZE, link->addr);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400861 msg_set_last_bcast(msg, link->owner->bclink.acked);
Ying Xuea6ca1092014-11-26 11:41:55 +0800862 __tipc_link_xmit_skb(link, skb);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800863}
864
865/*
Ying Xue247f0f32014-02-18 16:06:46 +0800866 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800867 * Receive the sequence number where we should start receiving and
868 * acking broadcast packets from a newly added peer node, and open
869 * up for reception of such packets.
870 *
871 * Called with node locked
872 */
Ying Xue247f0f32014-02-18 16:06:46 +0800873static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800874{
875 struct tipc_msg *msg = buf_msg(buf);
876
877 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
878 n->bclink.recv_permitted = true;
879 kfree_skb(buf);
880}
881
Ying Xue58dc55f2014-11-26 11:41:52 +0800882struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
883 const struct sk_buff *skb)
884{
885 if (skb_queue_is_last(list, skb))
886 return NULL;
887 return skb->next;
888}
889
Jon Maloyc64f7a62012-11-16 13:51:31 +0800890/*
Ying Xue47b4c9a2014-11-26 11:41:48 +0800891 * tipc_link_push_packets - push unsent packets to bearer
892 *
893 * Push out the unsent messages of a link where congestion
894 * has abated. Node is locked.
895 *
896 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 */
Ying Xue47b4c9a2014-11-26 11:41:48 +0800898void tipc_link_push_packets(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899{
Ying Xue58dc55f2014-11-26 11:41:52 +0800900 struct sk_buff_head *outqueue = &l_ptr->outqueue;
901 struct sk_buff *skb = l_ptr->next_out;
Ying Xue47b4c9a2014-11-26 11:41:48 +0800902 struct tipc_msg *msg;
903 u32 next, first;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904
Ying Xue58dc55f2014-11-26 11:41:52 +0800905 skb_queue_walk_from(outqueue, skb) {
Ying Xue47b4c9a2014-11-26 11:41:48 +0800906 msg = buf_msg(skb);
907 next = msg_seqno(msg);
Ying Xue58dc55f2014-11-26 11:41:52 +0800908 first = buf_seqno(skb_peek(outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909
910 if (mod(next - first) < l_ptr->queue_limit[0]) {
911 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900912 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +0800913 if (msg_user(msg) == MSG_BUNDLER)
Ying Xue58311d12014-11-26 11:41:49 +0800914 TIPC_SKB_CB(skb)->bundling = false;
Ying Xue7f9f95d2015-01-09 15:27:06 +0800915 tipc_bearer_send(l_ptr->owner->net,
916 l_ptr->bearer_id, skb,
Ying Xue47b4c9a2014-11-26 11:41:48 +0800917 &l_ptr->media_addr);
Ying Xue58dc55f2014-11-26 11:41:52 +0800918 l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
Ying Xue47b4c9a2014-11-26 11:41:48 +0800919 } else {
920 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 }
922 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923}
924
Ying Xue3f5a12b2014-05-05 08:56:17 +0800925void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700926{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700927 char addr_string[16];
928 u32 i;
929
Ying Xue3f5a12b2014-05-05 08:56:17 +0800930 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700931
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400932 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800933 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700934
935 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800936 if (node->links[i]) {
937 link_print(node->links[i], "Resetting link\n");
938 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700939 }
940 }
941
Ying Xue3f5a12b2014-05-05 08:56:17 +0800942 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700943}
944
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500945static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400946 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700947{
948 struct tipc_msg *msg = buf_msg(buf);
Ying Xue1da46562015-01-09 15:27:07 +0800949 struct net *net = l_ptr->owner->net;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700950
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400951 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700952
953 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700954 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000955 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700956 tipc_link_reset(l_ptr);
957
958 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700959 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700960 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700961 char addr_string[16];
962
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400963 pr_info("Msg seq number: %u, ", msg_seqno(msg));
964 pr_cont("Outstanding acks: %lu\n",
965 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700966
Ying Xue1da46562015-01-09 15:27:07 +0800967 n_ptr = tipc_bclink_retransmit_to(net);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700968 tipc_node_lock(n_ptr);
969
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000970 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400971 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800972 pr_info("Reception permitted: %d, Acked: %u\n",
973 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400974 n_ptr->bclink.acked);
975 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
976 n_ptr->bclink.last_in,
977 n_ptr->bclink.oos_state,
978 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700979
Allan Stephensd356eeb2006-06-25 23:40:01 -0700980 tipc_node_unlock(n_ptr);
981
Ying Xue1da46562015-01-09 15:27:07 +0800982 tipc_bclink_set_flags(net, TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700983 l_ptr->stale_count = 0;
984 }
985}
986
Ying Xue58dc55f2014-11-26 11:41:52 +0800987void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
Per Liden4323add2006-01-18 00:38:21 +0100988 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989{
990 struct tipc_msg *msg;
991
Ying Xue58dc55f2014-11-26 11:41:52 +0800992 if (!skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700993 return;
994
Ying Xue58dc55f2014-11-26 11:41:52 +0800995 msg = buf_msg(skb);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900996
Erik Hugne512137e2013-12-06 10:08:00 -0500997 /* Detect repeated retransmit failures */
998 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
999 if (++l_ptr->stale_count > 100) {
Ying Xue58dc55f2014-11-26 11:41:52 +08001000 link_retransmit_failure(l_ptr, skb);
Erik Hugne512137e2013-12-06 10:08:00 -05001001 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001002 }
1003 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001004 l_ptr->last_retransmitted = msg_seqno(msg);
1005 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001007
Ying Xue58dc55f2014-11-26 11:41:52 +08001008 skb_queue_walk_from(&l_ptr->outqueue, skb) {
1009 if (!retransmits || skb == l_ptr->next_out)
1010 break;
1011 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001013 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7f9f95d2015-01-09 15:27:06 +08001014 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
1015 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001016 retransmits--;
1017 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019}
1020
Ying Xuef03273f2014-11-26 11:41:54 +08001021static void link_retrieve_defq(struct tipc_link *link,
1022 struct sk_buff_head *list)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023{
1024 u32 seq_no;
1025
Ying Xuef03273f2014-11-26 11:41:54 +08001026 if (skb_queue_empty(&link->deferred_queue))
1027 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028
Ying Xuef03273f2014-11-26 11:41:54 +08001029 seq_no = buf_seqno(skb_peek(&link->deferred_queue));
1030 if (seq_no == mod(link->next_in_no))
1031 skb_queue_splice_tail_init(&link->deferred_queue, list);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032}
1033
Allan Stephens85035562008-04-15 19:04:54 -07001034/**
1035 * link_recv_buf_validate - validate basic format of received message
1036 *
1037 * This routine ensures a TIPC message has an acceptable header, and at least
1038 * as much data as the header indicates it should. The routine also ensures
1039 * that the entire message header is stored in the main fragment of the message
1040 * buffer, to simplify future access to message header fields.
1041 *
1042 * Note: Having extra info present in the message header or data areas is OK.
1043 * TIPC will ignore the excess, under the assumption that it is optional info
1044 * introduced by a later release of the protocol.
1045 */
Allan Stephens85035562008-04-15 19:04:54 -07001046static int link_recv_buf_validate(struct sk_buff *buf)
1047{
1048 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001049 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001050 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1051 };
1052
1053 struct tipc_msg *msg;
1054 u32 tipc_hdr[2];
1055 u32 size;
1056 u32 hdr_size;
1057 u32 min_hdr_size;
1058
Erik Hugne64380a02014-02-11 11:38:26 +01001059 /* If this packet comes from the defer queue, the skb has already
1060 * been validated
1061 */
1062 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1063 return 1;
1064
Allan Stephens85035562008-04-15 19:04:54 -07001065 if (unlikely(buf->len < MIN_H_SIZE))
1066 return 0;
1067
1068 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1069 if (msg == NULL)
1070 return 0;
1071
1072 if (unlikely(msg_version(msg) != TIPC_VERSION))
1073 return 0;
1074
1075 size = msg_size(msg);
1076 hdr_size = msg_hdr_sz(msg);
1077 min_hdr_size = msg_isdata(msg) ?
1078 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1079
1080 if (unlikely((hdr_size < min_hdr_size) ||
1081 (size < hdr_size) ||
1082 (buf->len < size) ||
1083 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1084 return 0;
1085
1086 return pskb_may_pull(buf, hdr_size);
1087}
1088
Allan Stephensb02b69c2010-08-17 11:00:07 +00001089/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001090 * tipc_rcv - process TIPC packets/messages arriving from off-node
Ying Xuef2f98002015-01-09 15:27:05 +08001091 * @net: the applicable net namespace
Ying Xuef03273f2014-11-26 11:41:54 +08001092 * @skb: TIPC packet
Ying Xue7a2f7d12014-04-21 10:55:46 +08001093 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001094 *
1095 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1096 * structure (i.e. cannot be NULL), but bearer can be inactive.
1097 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001098void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099{
Ying Xue34747532015-01-09 15:27:10 +08001100 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuef03273f2014-11-26 11:41:54 +08001101 struct sk_buff_head head;
1102 struct tipc_node *n_ptr;
1103 struct tipc_link *l_ptr;
1104 struct sk_buff *skb1, *tmp;
1105 struct tipc_msg *msg;
1106 u32 seq_no;
1107 u32 ackd;
1108 u32 released;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109
Ying Xuea6ca1092014-11-26 11:41:55 +08001110 skb2list(skb, &head);
Allan Stephens85035562008-04-15 19:04:54 -07001111
Ying Xuef03273f2014-11-26 11:41:54 +08001112 while ((skb = __skb_dequeue(&head))) {
Allan Stephens85035562008-04-15 19:04:54 -07001113 /* Ensure message is well-formed */
Ying Xuef03273f2014-11-26 11:41:54 +08001114 if (unlikely(!link_recv_buf_validate(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001115 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116
Allan Stephensfe13dda2008-04-15 19:03:23 -07001117 /* Ensure message data is a single contiguous unit */
Ying Xuef03273f2014-11-26 11:41:54 +08001118 if (unlikely(skb_linearize(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001119 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001120
Allan Stephens85035562008-04-15 19:04:54 -07001121 /* Handle arrival of a non-unicast link message */
Ying Xuef03273f2014-11-26 11:41:54 +08001122 msg = buf_msg(skb);
Allan Stephens85035562008-04-15 19:04:54 -07001123
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001125 if (msg_user(msg) == LINK_CONFIG)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001126 tipc_disc_rcv(net, skb, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001127 else
Ying Xuec93d3ba2015-01-09 15:27:04 +08001128 tipc_bclink_rcv(net, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 continue;
1130 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001131
Allan Stephensed33a9c2011-04-05 15:15:04 -04001132 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001133 if (unlikely(!msg_short(msg) &&
Ying Xue34747532015-01-09 15:27:10 +08001134 (msg_destnode(msg) != tn->own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001135 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001136
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001137 /* Locate neighboring node that sent message */
Ying Xuef2f98002015-01-09 15:27:05 +08001138 n_ptr = tipc_node_find(net, msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001140 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001141 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001142
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001143 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001145 if (unlikely(!l_ptr))
1146 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001147
Allan Stephensb4b56102011-05-27 11:00:51 -04001148 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001149 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001150 msg_user(msg) == LINK_PROTOCOL &&
1151 (msg_type(msg) == RESET_MSG ||
1152 msg_type(msg) == ACTIVATE_MSG) &&
1153 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001154 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001155
Ying Xue10f465c2014-05-05 08:56:11 +08001156 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001157 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001158
Allan Stephens85035562008-04-15 19:04:54 -07001159 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001160 seq_no = msg_seqno(msg);
1161 ackd = msg_ack(msg);
1162
1163 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001164 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001165 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166
Ying Xue58dc55f2014-11-26 11:41:52 +08001167 released = 0;
1168 skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) {
1169 if (skb1 == l_ptr->next_out ||
1170 more(buf_seqno(skb1), ackd))
1171 break;
1172 __skb_unlink(skb1, &l_ptr->outqueue);
1173 kfree_skb(skb1);
1174 released = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 }
Allan Stephens85035562008-04-15 19:04:54 -07001176
1177 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 if (unlikely(l_ptr->next_out))
Ying Xue47b4c9a2014-11-26 11:41:48 +08001179 tipc_link_push_packets(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001180
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001181 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1182 link_prepare_wakeup(l_ptr);
1183 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1184 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001185
Jon Paul Maloya5377832014-02-13 17:29:15 -05001186 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001187 if (unlikely(!link_working_working(l_ptr))) {
1188 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001189 tipc_link_proto_rcv(net, l_ptr, skb);
Ying Xuef03273f2014-11-26 11:41:54 +08001190 link_retrieve_defq(l_ptr, &head);
Per Liden4323add2006-01-18 00:38:21 +01001191 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 continue;
1193 }
Ying Xue3af390e2013-10-30 11:26:57 +08001194
1195 /* Traffic message. Conditionally activate link */
1196 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1197
1198 if (link_working_working(l_ptr)) {
1199 /* Re-insert buffer in front of queue */
Ying Xuef03273f2014-11-26 11:41:54 +08001200 __skb_queue_head(&head, skb);
Ying Xue3af390e2013-10-30 11:26:57 +08001201 tipc_node_unlock(n_ptr);
1202 continue;
1203 }
1204 goto unlock_discard;
1205 }
1206
1207 /* Link is now in state WORKING_WORKING */
1208 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001209 link_handle_out_of_seq_msg(net, l_ptr, skb);
Ying Xuef03273f2014-11-26 11:41:54 +08001210 link_retrieve_defq(l_ptr, &head);
Per Liden4323add2006-01-18 00:38:21 +01001211 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 continue;
1213 }
Ying Xue3af390e2013-10-30 11:26:57 +08001214 l_ptr->next_in_no++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001215 if (unlikely(!skb_queue_empty(&l_ptr->deferred_queue)))
Ying Xuef03273f2014-11-26 11:41:54 +08001216 link_retrieve_defq(l_ptr, &head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001217
Erik Hugne3f53bd82014-07-01 10:22:41 +02001218 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1219 l_ptr->stats.sent_acks++;
1220 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1221 }
1222
Ying Xuec93d3ba2015-01-09 15:27:04 +08001223 if (tipc_link_prepare_input(net, l_ptr, &skb)) {
Per Liden4323add2006-01-18 00:38:21 +01001224 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 }
Per Liden4323add2006-01-18 00:38:21 +01001227 tipc_node_unlock(n_ptr);
Ying Xuef03273f2014-11-26 11:41:54 +08001228
Ying Xuef2f98002015-01-09 15:27:05 +08001229 if (tipc_link_input(net, l_ptr, skb) != 0)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001230 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001231 continue;
1232unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001233 tipc_node_unlock(n_ptr);
1234discard:
Ying Xuef03273f2014-11-26 11:41:54 +08001235 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237}
1238
Ben Hutchings2c530402012-07-10 10:55:09 +00001239/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001240 * tipc_link_prepare_input - process TIPC link messages
1241 *
1242 * returns nonzero if the message was consumed
1243 *
1244 * Node lock must be held
1245 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001246static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
1247 struct sk_buff **buf)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001248{
1249 struct tipc_node *n;
1250 struct tipc_msg *msg;
1251 int res = -EINVAL;
1252
1253 n = l->owner;
1254 msg = buf_msg(*buf);
1255 switch (msg_user(msg)) {
1256 case CHANGEOVER_PROTOCOL:
Ying Xuec93d3ba2015-01-09 15:27:04 +08001257 if (tipc_link_tunnel_rcv(net, n, buf))
Erik Hugne7ae934b2014-07-01 10:22:40 +02001258 res = 0;
1259 break;
1260 case MSG_FRAGMENTER:
1261 l->stats.recv_fragments++;
1262 if (tipc_buf_append(&l->reasm_buf, buf)) {
1263 l->stats.recv_fragmented++;
1264 res = 0;
1265 } else if (!l->reasm_buf) {
1266 tipc_link_reset(l);
1267 }
1268 break;
1269 case MSG_BUNDLER:
1270 l->stats.recv_bundles++;
1271 l->stats.recv_bundled += msg_msgcnt(msg);
1272 res = 0;
1273 break;
1274 case NAME_DISTRIBUTOR:
1275 n->bclink.recv_permitted = true;
1276 res = 0;
1277 break;
1278 case BCAST_PROTOCOL:
1279 tipc_link_sync_rcv(n, *buf);
1280 break;
1281 default:
1282 res = 0;
1283 }
1284 return res;
1285}
1286/**
1287 * tipc_link_input - Deliver message too higher layers
1288 */
Ying Xuef2f98002015-01-09 15:27:05 +08001289static int tipc_link_input(struct net *net, struct tipc_link *l,
1290 struct sk_buff *buf)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001291{
1292 struct tipc_msg *msg = buf_msg(buf);
1293 int res = 0;
1294
1295 switch (msg_user(msg)) {
1296 case TIPC_LOW_IMPORTANCE:
1297 case TIPC_MEDIUM_IMPORTANCE:
1298 case TIPC_HIGH_IMPORTANCE:
1299 case TIPC_CRITICAL_IMPORTANCE:
1300 case CONN_MANAGER:
Ying Xuef2f98002015-01-09 15:27:05 +08001301 tipc_sk_rcv(net, buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001302 break;
1303 case NAME_DISTRIBUTOR:
Ying Xuef2f98002015-01-09 15:27:05 +08001304 tipc_named_rcv(net, buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001305 break;
1306 case MSG_BUNDLER:
Ying Xuef2f98002015-01-09 15:27:05 +08001307 tipc_link_bundle_rcv(net, buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001308 break;
1309 default:
1310 res = -EINVAL;
1311 }
1312 return res;
1313}
1314
1315/**
Allan Stephens8809b252011-10-25 10:44:35 -04001316 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1317 *
1318 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001320u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321{
Ying Xuebc6fecd2014-11-26 11:41:53 +08001322 struct sk_buff *skb1;
1323 u32 seq_no = buf_seqno(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324
1325 /* Empty queue ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001326 if (skb_queue_empty(list)) {
1327 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 return 1;
1329 }
1330
1331 /* Last ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001332 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1333 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 return 1;
1335 }
1336
Allan Stephens8809b252011-10-25 10:44:35 -04001337 /* Locate insertion point in queue, then insert; discard if duplicate */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001338 skb_queue_walk(list, skb1) {
1339 u32 curr_seqno = buf_seqno(skb1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340
Allan Stephens8809b252011-10-25 10:44:35 -04001341 if (seq_no == curr_seqno) {
Ying Xuebc6fecd2014-11-26 11:41:53 +08001342 kfree_skb(skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001343 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 }
Allan Stephens8809b252011-10-25 10:44:35 -04001345
1346 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 break;
Allan Stephens8809b252011-10-25 10:44:35 -04001348 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349
Ying Xuebc6fecd2014-11-26 11:41:53 +08001350 __skb_queue_before(list, skb1, skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001351 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352}
1353
Allan Stephens8809b252011-10-25 10:44:35 -04001354/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1356 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001357static void link_handle_out_of_seq_msg(struct net *net,
1358 struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 struct sk_buff *buf)
1360{
Allan Stephensf9057302011-10-24 16:03:12 -04001361 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362
1363 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001364 tipc_link_proto_rcv(net, l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 return;
1366 }
1367
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369 l_ptr->checkpoint--;
1370
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001371 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 * Discard packet if a duplicate; otherwise add it to deferred queue
1373 * and notify peer of gap as per protocol specification
1374 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 if (less(seq_no, mod(l_ptr->next_in_no))) {
1376 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001377 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 return;
1379 }
1380
Ying Xuebc6fecd2014-11-26 11:41:53 +08001381 if (tipc_link_defer_pkt(&l_ptr->deferred_queue, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001383 TIPC_SKB_CB(buf)->deferred = true;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001384 if ((skb_queue_len(&l_ptr->deferred_queue) % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001385 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001386 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 l_ptr->stats.duplicates++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001388 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389}
1390
1391/*
1392 * Send protocol message to the other endpoint.
1393 */
Ying Xue247f0f32014-02-18 16:06:46 +08001394void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1395 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001397 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001399 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001400 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401
Ying Xue77a7e072013-12-10 20:45:44 -08001402 /* Don't send protocol message during link changeover */
1403 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001405
1406 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001407 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001408 return;
1409
Allan Stephens92d2c902011-10-25 11:20:26 -04001410 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001411 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001412 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001413 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue1da46562015-01-09 15:27:07 +08001414 msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415
1416 if (msg_typ == STATE_MSG) {
1417 u32 next_sent = mod(l_ptr->next_out_no);
1418
Per Liden4323add2006-01-18 00:38:21 +01001419 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 return;
1421 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001422 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 msg_set_next_sent(msg, next_sent);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001424 if (!skb_queue_empty(&l_ptr->deferred_queue)) {
1425 u32 rec = buf_seqno(skb_peek(&l_ptr->deferred_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 gap = mod(rec - mod(l_ptr->next_in_no));
1427 }
1428 msg_set_seq_gap(msg, gap);
1429 if (gap)
1430 l_ptr->stats.sent_nacks++;
1431 msg_set_link_tolerance(msg, tolerance);
1432 msg_set_linkprio(msg, priority);
1433 msg_set_max_pkt(msg, ack_mtu);
1434 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1435 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001436 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437 u32 mtu = l_ptr->max_pkt;
1438
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001439 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 link_working_working(l_ptr) &&
1441 l_ptr->fsm_msg_cnt) {
1442 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001443 if (l_ptr->max_pkt_probes == 10) {
1444 l_ptr->max_pkt_target = (msg_size - 4);
1445 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001447 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001449 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450
1451 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001452 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 l_ptr->stats.sent_states++;
1454 } else { /* RESET_MSG or ACTIVATE_MSG */
1455 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1456 msg_set_seq_gap(msg, 0);
1457 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001458 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 msg_set_link_tolerance(msg, l_ptr->tolerance);
1460 msg_set_linkprio(msg, l_ptr->priority);
1461 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1462 }
1463
Allan Stephens75f0aa42011-02-28 15:30:20 -05001464 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1465 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001467 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468
1469 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1470
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001471 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472 if (!buf)
1473 return;
1474
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001475 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001476 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477
Ying Xue7f9f95d2015-01-09 15:27:06 +08001478 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
1479 &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001480 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001481 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482}
1483
1484/*
1485 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001486 * Note that network plane id propagates through the network, and may
1487 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001489static void tipc_link_proto_rcv(struct net *net, struct tipc_link *l_ptr,
1490 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491{
Ying Xue34747532015-01-09 15:27:10 +08001492 struct tipc_net *tn = net_generic(net, tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493 u32 rec_gap = 0;
1494 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001495 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496 u32 msg_tol;
1497 struct tipc_msg *msg = buf_msg(buf);
1498
Ying Xue77a7e072013-12-10 20:45:44 -08001499 /* Discard protocol message during link changeover */
1500 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 goto exit;
1502
Ying Xue7a2f7d12014-04-21 10:55:46 +08001503 if (l_ptr->net_plane != msg_net_plane(msg))
Ying Xue34747532015-01-09 15:27:10 +08001504 if (tn->own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001505 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001508
Per Lidenb97bf3f2006-01-02 19:04:38 +01001509 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001510 if (!link_working_unknown(l_ptr) &&
1511 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001512 if (less_eq(msg_session(msg), l_ptr->peer_session))
1513 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001515
1516 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1517 link_working_unknown(l_ptr))) {
1518 /*
1519 * peer has lost contact -- don't allow peer's links
1520 * to reactivate before we recognize loss & clean up
1521 */
Ying Xueca9cf062014-05-08 08:54:40 +08001522 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001523 }
1524
Allan Stephens47361c82011-10-26 10:55:16 -04001525 link_state_event(l_ptr, RESET_MSG);
1526
Per Lidenb97bf3f2006-01-02 19:04:38 +01001527 /* fall thru' */
1528 case ACTIVATE_MSG:
1529 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1531
Allan Stephens2db99832010-12-31 18:59:33 +00001532 msg_tol = msg_link_tolerance(msg);
1533 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 link_set_supervision_props(l_ptr, msg_tol);
1535
1536 if (msg_linkprio(msg) > l_ptr->priority)
1537 l_ptr->priority = msg_linkprio(msg);
1538
1539 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001540 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 if (max_pkt_info < l_ptr->max_pkt_target)
1542 l_ptr->max_pkt_target = max_pkt_info;
1543 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1544 l_ptr->max_pkt = l_ptr->max_pkt_target;
1545 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001546 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001547 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001548
Allan Stephens4d753132011-10-25 12:19:05 -04001549 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001550 if (!tipc_node_is_up(l_ptr->owner)) {
1551 l_ptr->owner->bclink.last_sent =
1552 l_ptr->owner->bclink.last_in =
1553 msg_last_bcast(msg);
1554 l_ptr->owner->bclink.oos_state = 0;
1555 }
Allan Stephens4d753132011-10-25 12:19:05 -04001556
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557 l_ptr->peer_session = msg_session(msg);
1558 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001559
1560 if (msg_type(msg) == ACTIVATE_MSG)
1561 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 break;
1563 case STATE_MSG:
1564
Allan Stephens2db99832010-12-31 18:59:33 +00001565 msg_tol = msg_link_tolerance(msg);
1566 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001568
1569 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001571 pr_warn("%s<%s>, priority change %u->%u\n",
1572 link_rst_msg, l_ptr->name, l_ptr->priority,
1573 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001575 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576 break;
1577 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001578
1579 /* Record reception; force mismatch at next timeout: */
1580 l_ptr->checkpoint--;
1581
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1583 l_ptr->stats.recv_states++;
1584 if (link_reset_unknown(l_ptr))
1585 break;
1586
1587 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001588 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589 mod(l_ptr->next_in_no));
1590 }
1591
1592 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001593 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001594 l_ptr->max_pkt = max_pkt_ack;
1595 l_ptr->max_pkt_probes = 0;
1596 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597
1598 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001599 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001601 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001602 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001603 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604
1605 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001606 if (l_ptr->owner->bclink.recv_permitted)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001607 tipc_bclink_update_link_state(net, l_ptr->owner,
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001608 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609
1610 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001611 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1612 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 }
1614 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 l_ptr->stats.recv_nacks++;
Ying Xue58dc55f2014-11-26 11:41:52 +08001616 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue),
Per Liden4323add2006-01-18 00:38:21 +01001617 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618 }
1619 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620 }
1621exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001622 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001623}
1624
1625
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001626/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1627 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001629static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1630 struct tipc_msg *tunnel_hdr,
1631 struct tipc_msg *msg,
1632 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001634 struct tipc_link *tunnel;
Ying Xuea6ca1092014-11-26 11:41:55 +08001635 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636 u32 length = msg_size(msg);
1637
1638 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001639 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001640 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001641 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001642 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001644 skb = tipc_buf_acquire(length + INT_H_SIZE);
1645 if (!skb) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001646 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001648 }
Ying Xuea6ca1092014-11-26 11:41:55 +08001649 skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
1650 skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
1651 __tipc_link_xmit_skb(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652}
1653
1654
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001655/* tipc_link_failover_send_queue(): A link has gone down, but a second
1656 * link is still active. We can do failover. Tunnel the failing link's
1657 * whole send queue via the remaining link. This way, we don't lose
1658 * any packets, and sequence order is preserved for subsequent traffic
1659 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001661void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662{
Ying Xue58dc55f2014-11-26 11:41:52 +08001663 u32 msgcount = skb_queue_len(&l_ptr->outqueue);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001664 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 struct tipc_msg tunnel_hdr;
Ying Xue58dc55f2014-11-26 11:41:52 +08001666 struct sk_buff *skb;
Allan Stephens5392d642006-06-25 23:52:50 -07001667 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668
1669 if (!tunnel)
1670 return;
1671
Ying Xue34747532015-01-09 15:27:10 +08001672 tipc_msg_init(l_ptr->owner->net, &tunnel_hdr, CHANGEOVER_PROTOCOL,
1673 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1675 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001676
Ying Xue58dc55f2014-11-26 11:41:52 +08001677 if (skb_queue_empty(&l_ptr->outqueue)) {
1678 skb = tipc_buf_acquire(INT_H_SIZE);
1679 if (skb) {
1680 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001682 __tipc_link_xmit_skb(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001684 pr_warn("%sunable to send changeover msg\n",
1685 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686 }
1687 return;
1688 }
Allan Stephensf1310722006-06-25 23:51:37 -07001689
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001690 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001691 l_ptr->owner->active_links[1]);
1692
Ying Xue58dc55f2014-11-26 11:41:52 +08001693 skb_queue_walk(&l_ptr->outqueue, skb) {
1694 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695
1696 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001698 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699
Florian Westphald788d802007-08-02 19:28:06 -07001700 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001702 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001703 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1704 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 pos += align(msg_size(m));
1706 m = (struct tipc_msg *)pos;
1707 }
1708 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001709 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1710 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 }
1713}
1714
Ying Xue247f0f32014-02-18 16:06:46 +08001715/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001716 * duplicate of the first link's send queue via the new link. This way, we
1717 * are guaranteed that currently queued packets from a socket are delivered
1718 * before future traffic from the same socket, even if this is using the
1719 * new link. The last arriving copy of each duplicate packet is dropped at
1720 * the receiving end by the regular protocol check, so packet cardinality
1721 * and sequence order is preserved per sender/receiver socket pair.
1722 * Owner node is locked.
1723 */
Ying Xue247f0f32014-02-18 16:06:46 +08001724void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001725 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726{
Ying Xue58dc55f2014-11-26 11:41:52 +08001727 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 struct tipc_msg tunnel_hdr;
1729
Ying Xue34747532015-01-09 15:27:10 +08001730 tipc_msg_init(l_ptr->owner->net, &tunnel_hdr, CHANGEOVER_PROTOCOL,
1731 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Ying Xue58dc55f2014-11-26 11:41:52 +08001732 msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
Ying Xue58dc55f2014-11-26 11:41:52 +08001734 skb_queue_walk(&l_ptr->outqueue, skb) {
1735 struct sk_buff *outskb;
1736 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 u32 length = msg_size(msg);
1738
1739 if (msg_user(msg) == MSG_BUNDLER)
1740 msg_set_type(msg, CLOSED_MSG);
1741 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001742 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001744 outskb = tipc_buf_acquire(length + INT_H_SIZE);
1745 if (outskb == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001746 pr_warn("%sunable to send duplicate msg\n",
1747 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 return;
1749 }
Ying Xue58dc55f2014-11-26 11:41:52 +08001750 skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE);
1751 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data,
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001752 length);
Ying Xuea6ca1092014-11-26 11:41:55 +08001753 __tipc_link_xmit_skb(tunnel, outskb);
Per Liden4323add2006-01-18 00:38:21 +01001754 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 }
1757}
1758
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759/**
1760 * buf_extract - extracts embedded TIPC message from another message
1761 * @skb: encapsulating message buffer
1762 * @from_pos: offset to extract from
1763 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001764 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 * encapsulating message itself is left unchanged.
1766 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1768{
1769 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1770 u32 size = msg_size(msg);
1771 struct sk_buff *eb;
1772
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001773 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001775 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776 return eb;
1777}
1778
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001779
1780
1781/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1782 * Owner node is locked.
1783 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001784static void tipc_link_dup_rcv(struct net *net, struct tipc_link *l_ptr,
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001785 struct sk_buff *t_buf)
1786{
1787 struct sk_buff *buf;
1788
1789 if (!tipc_link_is_up(l_ptr))
1790 return;
1791
1792 buf = buf_extract(t_buf, INT_H_SIZE);
1793 if (buf == NULL) {
1794 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1795 return;
1796 }
1797
1798 /* Add buffer to deferred queue, if applicable: */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001799 link_handle_out_of_seq_msg(net, l_ptr, buf);
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001800}
1801
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001802/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1803 * Owner node is locked.
1804 */
1805static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1806 struct sk_buff *t_buf)
1807{
1808 struct tipc_msg *t_msg = buf_msg(t_buf);
1809 struct sk_buff *buf = NULL;
1810 struct tipc_msg *msg;
1811
1812 if (tipc_link_is_up(l_ptr))
1813 tipc_link_reset(l_ptr);
1814
1815 /* First failover packet? */
1816 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1817 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1818
1819 /* Should there be an inner packet? */
1820 if (l_ptr->exp_msg_count) {
1821 l_ptr->exp_msg_count--;
1822 buf = buf_extract(t_buf, INT_H_SIZE);
1823 if (buf == NULL) {
1824 pr_warn("%sno inner failover pkt\n", link_co_err);
1825 goto exit;
1826 }
1827 msg = buf_msg(buf);
1828
1829 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1830 kfree_skb(buf);
1831 buf = NULL;
1832 goto exit;
1833 }
1834 if (msg_user(msg) == MSG_FRAGMENTER) {
1835 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001836 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001837 }
1838 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001839exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001840 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1841 tipc_node_detach_link(l_ptr->owner, l_ptr);
1842 kfree(l_ptr);
1843 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001844 return buf;
1845}
1846
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001847/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001848 * via other link as result of a failover (ORIGINAL_MSG) or
1849 * a new active link (DUPLICATE_MSG). Failover packets are
1850 * returned to the active link for delivery upwards.
1851 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001853static int tipc_link_tunnel_rcv(struct net *net, struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001854 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001856 struct sk_buff *t_buf = *buf;
1857 struct tipc_link *l_ptr;
1858 struct tipc_msg *t_msg = buf_msg(t_buf);
1859 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001861 *buf = NULL;
1862
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001863 if (bearer_id >= MAX_BEARERS)
1864 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001865
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001866 l_ptr = n_ptr->links[bearer_id];
1867 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001870 if (msg_type(t_msg) == DUPLICATE_MSG)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001871 tipc_link_dup_rcv(net, l_ptr, t_buf);
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001872 else if (msg_type(t_msg) == ORIGINAL_MSG)
1873 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001874 else
1875 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001877 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001878 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879}
1880
1881/*
1882 * Bundler functionality:
1883 */
Ying Xuef2f98002015-01-09 15:27:05 +08001884void tipc_link_bundle_rcv(struct net *net, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885{
1886 u32 msgcount = msg_msgcnt(buf_msg(buf));
1887 u32 pos = INT_H_SIZE;
1888 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001889 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 while (msgcount--) {
1892 obuf = buf_extract(buf, pos);
1893 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001894 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001895 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001896 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001897 omsg = buf_msg(obuf);
1898 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001899 if (msg_isdata(omsg)) {
1900 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
Ying Xuef2f98002015-01-09 15:27:05 +08001901 tipc_sk_mcast_rcv(net, obuf);
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001902 else
Ying Xuef2f98002015-01-09 15:27:05 +08001903 tipc_sk_rcv(net, obuf);
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001904 } else if (msg_user(omsg) == CONN_MANAGER) {
Ying Xuef2f98002015-01-09 15:27:05 +08001905 tipc_sk_rcv(net, obuf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001906 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
Ying Xuef2f98002015-01-09 15:27:05 +08001907 tipc_named_rcv(net, obuf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001908 } else {
1909 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1910 kfree_skb(obuf);
1911 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001913 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914}
1915
Ying Xue2f55c432015-01-09 15:27:00 +08001916static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917{
Ying Xue2f55c432015-01-09 15:27:00 +08001918 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
1919
1920 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
Allan Stephens5413b4c2011-01-18 13:24:55 -05001921 return;
1922
Ying Xue2f55c432015-01-09 15:27:00 +08001923 l_ptr->tolerance = tol;
1924 l_ptr->cont_intv = msecs_to_jiffies(intv);
1925 l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001926}
1927
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001928void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929{
1930 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001931 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1932 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1933 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1934 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001936 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1937 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1938 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1939 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001941 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1942 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1943 /* FRAGMENT and LAST_FRAGMENT packets */
1944 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1945}
1946
Jon Paul Maloye099e862014-02-13 17:29:18 -05001947/* tipc_link_find_owner - locate owner node of link by link's name
Ying Xuef2f98002015-01-09 15:27:05 +08001948 * @net: the applicable net namespace
Jon Paul Maloye099e862014-02-13 17:29:18 -05001949 * @name: pointer to link name string
1950 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001951 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001952 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001953 */
Ying Xuef2f98002015-01-09 15:27:05 +08001954static struct tipc_node *tipc_link_find_owner(struct net *net,
1955 const char *link_name,
Jon Paul Maloye099e862014-02-13 17:29:18 -05001956 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001957{
Ying Xuef2f98002015-01-09 15:27:05 +08001958 struct tipc_net *tn = net_generic(net, tipc_net_id);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001959 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001960 struct tipc_node *n_ptr;
Fabian Frederick886eaa12014-12-25 12:05:50 +01001961 struct tipc_node *found_node = NULL;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001962 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963
Jon Paul Maloye099e862014-02-13 17:29:18 -05001964 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001965 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +08001966 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001967 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001968 for (i = 0; i < MAX_BEARERS; i++) {
1969 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001970 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1971 *bearer_id = i;
1972 found_node = n_ptr;
1973 break;
1974 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001975 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001976 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001977 if (found_node)
1978 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001979 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001980 rcu_read_unlock();
1981
Jon Paul Maloye099e862014-02-13 17:29:18 -05001982 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983}
1984
Allan Stephens5c216e12011-10-18 11:34:29 -04001985/**
1986 * link_value_is_valid -- validate proposed link tolerance/priority/window
1987 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001988 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1989 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04001990 *
1991 * Returns 1 if value is within range, 0 if not.
1992 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001993static int link_value_is_valid(u16 cmd, u32 new_value)
1994{
1995 switch (cmd) {
1996 case TIPC_CMD_SET_LINK_TOL:
1997 return (new_value >= TIPC_MIN_LINK_TOL) &&
1998 (new_value <= TIPC_MAX_LINK_TOL);
1999 case TIPC_CMD_SET_LINK_PRI:
2000 return (new_value <= TIPC_MAX_LINK_PRI);
2001 case TIPC_CMD_SET_LINK_WINDOW:
2002 return (new_value >= TIPC_MIN_LINK_WIN) &&
2003 (new_value <= TIPC_MAX_LINK_WIN);
2004 }
2005 return 0;
2006}
2007
Allan Stephens5c216e12011-10-18 11:34:29 -04002008/**
2009 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ying Xuef2f98002015-01-09 15:27:05 +08002010 * @net: the applicable net namespace
Ben Hutchings2c530402012-07-10 10:55:09 +00002011 * @name: ptr to link, bearer, or media name
2012 * @new_value: new value of link, bearer, or media setting
2013 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002014 *
Ying Xue7216cd92014-04-21 10:55:48 +08002015 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04002016 *
2017 * Returns 0 if value updated and negative value on error.
2018 */
Ying Xuef2f98002015-01-09 15:27:05 +08002019static int link_cmd_set_value(struct net *net, const char *name, u32 new_value,
2020 u16 cmd)
Allan Stephens5c216e12011-10-18 11:34:29 -04002021{
2022 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002023 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002024 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002025 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002026 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002027 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002028
Ying Xuef2f98002015-01-09 15:27:05 +08002029 node = tipc_link_find_owner(net, name, &bearer_id);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002030 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002031 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002032 l_ptr = node->links[bearer_id];
2033
2034 if (l_ptr) {
2035 switch (cmd) {
2036 case TIPC_CMD_SET_LINK_TOL:
2037 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002038 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2039 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002040 break;
2041 case TIPC_CMD_SET_LINK_PRI:
2042 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002043 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2044 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002045 break;
2046 case TIPC_CMD_SET_LINK_WINDOW:
2047 tipc_link_set_queue_limits(l_ptr, new_value);
2048 break;
2049 default:
2050 res = -EINVAL;
2051 break;
2052 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002053 }
2054 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002055 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002056 }
2057
Ying Xue7f9f95d2015-01-09 15:27:06 +08002058 b_ptr = tipc_bearer_find(net, name);
Allan Stephens5c216e12011-10-18 11:34:29 -04002059 if (b_ptr) {
2060 switch (cmd) {
2061 case TIPC_CMD_SET_LINK_TOL:
2062 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002063 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002064 case TIPC_CMD_SET_LINK_PRI:
2065 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002066 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002067 case TIPC_CMD_SET_LINK_WINDOW:
2068 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002069 break;
2070 default:
2071 res = -EINVAL;
2072 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002073 }
Ying Xue636c0372013-10-18 07:23:20 +02002074 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002075 }
2076
2077 m_ptr = tipc_media_find(name);
2078 if (!m_ptr)
2079 return -ENODEV;
2080 switch (cmd) {
2081 case TIPC_CMD_SET_LINK_TOL:
2082 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002083 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002084 case TIPC_CMD_SET_LINK_PRI:
2085 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002086 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002087 case TIPC_CMD_SET_LINK_WINDOW:
2088 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002089 break;
2090 default:
2091 res = -EINVAL;
2092 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002093 }
Ying Xue636c0372013-10-18 07:23:20 +02002094 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002095}
2096
Ying Xuef2f98002015-01-09 15:27:05 +08002097struct sk_buff *tipc_link_cmd_config(struct net *net, const void *req_tlv_area,
2098 int req_tlv_space, u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099{
2100 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002101 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002102 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103
2104 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002105 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106
2107 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2108 new_value = ntohl(args->value);
2109
Allan Stephens5c216e12011-10-18 11:34:29 -04002110 if (!link_value_is_valid(cmd, new_value))
2111 return tipc_cfg_reply_error_string(
2112 "cannot change, value invalid");
2113
Per Liden4323add2006-01-18 00:38:21 +01002114 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Ying Xue1da46562015-01-09 15:27:07 +08002116 (tipc_bclink_set_queue_limits(net, new_value) == 0))
Per Liden4323add2006-01-18 00:38:21 +01002117 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002118 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002119 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002120 }
2121
Ying Xuef2f98002015-01-09 15:27:05 +08002122 res = link_cmd_set_value(net, args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002124 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002125
Per Liden4323add2006-01-18 00:38:21 +01002126 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127}
2128
2129/**
2130 * link_reset_statistics - reset link statistics
2131 * @l_ptr: pointer to link
2132 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002133static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002134{
2135 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2136 l_ptr->stats.sent_info = l_ptr->next_out_no;
2137 l_ptr->stats.recv_info = l_ptr->next_in_no;
2138}
2139
Ying Xuef2f98002015-01-09 15:27:05 +08002140struct sk_buff *tipc_link_cmd_reset_stats(struct net *net,
2141 const void *req_tlv_area,
2142 int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143{
2144 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002145 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002146 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002147 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002148
2149 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002150 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002151
2152 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002153 if (!strcmp(link_name, tipc_bclink_name)) {
Ying Xue1da46562015-01-09 15:27:07 +08002154 if (tipc_bclink_reset_stats(net))
Per Liden4323add2006-01-18 00:38:21 +01002155 return tipc_cfg_reply_error_string("link not found");
2156 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157 }
Ying Xuef2f98002015-01-09 15:27:05 +08002158 node = tipc_link_find_owner(net, link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002159 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002160 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002161
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002162 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002163 l_ptr = node->links[bearer_id];
2164 if (!l_ptr) {
2165 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002166 return tipc_cfg_reply_error_string("link not found");
2167 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002169 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002170 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171}
2172
2173/**
2174 * percent - convert count to a percentage of total (rounding up or down)
2175 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002176static u32 percent(u32 count, u32 total)
2177{
2178 return (count * 100 + (total / 2)) / total;
2179}
2180
2181/**
Per Liden4323add2006-01-18 00:38:21 +01002182 * tipc_link_stats - print link statistics
Ying Xuef2f98002015-01-09 15:27:05 +08002183 * @net: the applicable net namespace
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 * @name: link name
2185 * @buf: print buffer area
2186 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002187 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188 * Returns length of print buffer data string (or 0 if error)
2189 */
Ying Xuef2f98002015-01-09 15:27:05 +08002190static int tipc_link_stats(struct net *net, const char *name, char *buf,
2191 const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002193 struct tipc_link *l;
2194 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002195 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 char *status;
2197 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002198 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002199 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002200
Per Liden4323add2006-01-18 00:38:21 +01002201 if (!strcmp(name, tipc_bclink_name))
Ying Xue1da46562015-01-09 15:27:07 +08002202 return tipc_bclink_stats(net, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002203
Ying Xuef2f98002015-01-09 15:27:05 +08002204 node = tipc_link_find_owner(net, name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002205 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002207
Per Liden4323add2006-01-18 00:38:21 +01002208 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002209
2210 l = node->links[bearer_id];
2211 if (!l) {
2212 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002213 return 0;
2214 }
2215
Erik Hugnedc1aed32012-06-29 00:50:23 -04002216 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217
Erik Hugnedc1aed32012-06-29 00:50:23 -04002218 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002219 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002220 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002221 status = "STANDBY";
2222 else
2223 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002224
2225 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2226 " %s MTU:%u Priority:%u Tolerance:%u ms"
2227 " Window:%u packets\n",
2228 l->name, status, l->max_pkt, l->priority,
2229 l->tolerance, l->queue_limit[0]);
2230
2231 ret += tipc_snprintf(buf + ret, buf_size - ret,
2232 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2233 l->next_in_no - s->recv_info, s->recv_fragments,
2234 s->recv_fragmented, s->recv_bundles,
2235 s->recv_bundled);
2236
2237 ret += tipc_snprintf(buf + ret, buf_size - ret,
2238 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2239 l->next_out_no - s->sent_info, s->sent_fragments,
2240 s->sent_fragmented, s->sent_bundles,
2241 s->sent_bundled);
2242
2243 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002244 if (!profile_total)
2245 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002246
2247 ret += tipc_snprintf(buf + ret, buf_size - ret,
2248 " TX profile sample:%u packets average:%u octets\n"
2249 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2250 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2251 s->msg_length_counts,
2252 s->msg_lengths_total / profile_total,
2253 percent(s->msg_length_profile[0], profile_total),
2254 percent(s->msg_length_profile[1], profile_total),
2255 percent(s->msg_length_profile[2], profile_total),
2256 percent(s->msg_length_profile[3], profile_total),
2257 percent(s->msg_length_profile[4], profile_total),
2258 percent(s->msg_length_profile[5], profile_total),
2259 percent(s->msg_length_profile[6], profile_total));
2260
2261 ret += tipc_snprintf(buf + ret, buf_size - ret,
2262 " RX states:%u probes:%u naks:%u defs:%u"
2263 " dups:%u\n", s->recv_states, s->recv_probes,
2264 s->recv_nacks, s->deferred_recv, s->duplicates);
2265
2266 ret += tipc_snprintf(buf + ret, buf_size - ret,
2267 " TX states:%u probes:%u naks:%u acks:%u"
2268 " dups:%u\n", s->sent_states, s->sent_probes,
2269 s->sent_nacks, s->sent_acks, s->retransmitted);
2270
2271 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002272 " Congestion link:%u Send queue"
2273 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002274 s->max_queue_sz, s->queue_sz_counts ?
2275 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002276
Per Liden4323add2006-01-18 00:38:21 +01002277 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002278 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002279}
2280
Ying Xuef2f98002015-01-09 15:27:05 +08002281struct sk_buff *tipc_link_cmd_show_stats(struct net *net,
2282 const void *req_tlv_area,
2283 int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002284{
2285 struct sk_buff *buf;
2286 struct tlv_desc *rep_tlv;
2287 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002288 int pb_len;
2289 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002290
2291 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002292 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002293
Erik Hugnedc1aed32012-06-29 00:50:23 -04002294 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002295 if (!buf)
2296 return NULL;
2297
2298 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002299 pb = TLV_DATA(rep_tlv);
2300 pb_len = ULTRA_STRING_MAX_LEN;
Ying Xuef2f98002015-01-09 15:27:05 +08002301 str_len = tipc_link_stats(net, (char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002302 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002303 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002304 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002305 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002306 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002307 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002308 skb_put(buf, TLV_SPACE(str_len));
2309 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2310
2311 return buf;
2312}
2313
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002314static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002315{
Ying Xue7f9f95d2015-01-09 15:27:06 +08002316 struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
Ying Xue7a2f7d12014-04-21 10:55:46 +08002317 struct tipc_bearer *b_ptr;
2318
2319 rcu_read_lock();
Ying Xue7f9f95d2015-01-09 15:27:06 +08002320 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
Ying Xue7a2f7d12014-04-21 10:55:46 +08002321 if (b_ptr)
2322 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2323 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002324
Per Lidenb97bf3f2006-01-02 19:04:38 +01002325 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002326 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002327 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002328 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002329 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002330 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002331 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002332 pr_cont(":WW\n");
2333 else
2334 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002335}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002336
2337/* Parse and validate nested (link) properties valid for media, bearer and link
2338 */
2339int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2340{
2341 int err;
2342
2343 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2344 tipc_nl_prop_policy);
2345 if (err)
2346 return err;
2347
2348 if (props[TIPC_NLA_PROP_PRIO]) {
2349 u32 prio;
2350
2351 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2352 if (prio > TIPC_MAX_LINK_PRI)
2353 return -EINVAL;
2354 }
2355
2356 if (props[TIPC_NLA_PROP_TOL]) {
2357 u32 tol;
2358
2359 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2360 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2361 return -EINVAL;
2362 }
2363
2364 if (props[TIPC_NLA_PROP_WIN]) {
2365 u32 win;
2366
2367 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2368 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2369 return -EINVAL;
2370 }
2371
2372 return 0;
2373}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002374
Richard Alpef96ce7a2014-11-20 10:29:13 +01002375int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2376{
2377 int err;
2378 int res = 0;
2379 int bearer_id;
2380 char *name;
2381 struct tipc_link *link;
2382 struct tipc_node *node;
2383 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Ying Xuef2f98002015-01-09 15:27:05 +08002384 struct net *net = genl_info_net(info);
Richard Alpef96ce7a2014-11-20 10:29:13 +01002385
2386 if (!info->attrs[TIPC_NLA_LINK])
2387 return -EINVAL;
2388
2389 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2390 info->attrs[TIPC_NLA_LINK],
2391 tipc_nl_link_policy);
2392 if (err)
2393 return err;
2394
2395 if (!attrs[TIPC_NLA_LINK_NAME])
2396 return -EINVAL;
2397
2398 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2399
Ying Xuef2f98002015-01-09 15:27:05 +08002400 node = tipc_link_find_owner(net, name, &bearer_id);
Richard Alpef96ce7a2014-11-20 10:29:13 +01002401 if (!node)
2402 return -EINVAL;
2403
2404 tipc_node_lock(node);
2405
2406 link = node->links[bearer_id];
2407 if (!link) {
2408 res = -EINVAL;
2409 goto out;
2410 }
2411
2412 if (attrs[TIPC_NLA_LINK_PROP]) {
2413 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2414
2415 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2416 props);
2417 if (err) {
2418 res = err;
2419 goto out;
2420 }
2421
2422 if (props[TIPC_NLA_PROP_TOL]) {
2423 u32 tol;
2424
2425 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2426 link_set_supervision_props(link, tol);
2427 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2428 }
2429 if (props[TIPC_NLA_PROP_PRIO]) {
2430 u32 prio;
2431
2432 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2433 link->priority = prio;
2434 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2435 }
2436 if (props[TIPC_NLA_PROP_WIN]) {
2437 u32 win;
2438
2439 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2440 tipc_link_set_queue_limits(link, win);
2441 }
2442 }
2443
2444out:
2445 tipc_node_unlock(node);
2446
2447 return res;
2448}
Richard Alped8182802014-11-24 11:10:29 +01002449
2450static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002451{
2452 int i;
2453 struct nlattr *stats;
2454
2455 struct nla_map {
2456 u32 key;
2457 u32 val;
2458 };
2459
2460 struct nla_map map[] = {
2461 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2462 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2463 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2464 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2465 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2466 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2467 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2468 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2469 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2470 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2471 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2472 s->msg_length_counts : 1},
2473 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2474 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2475 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2476 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2477 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2478 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2479 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2480 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2481 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2482 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2483 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2484 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2485 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2486 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2487 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2488 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2489 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2490 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2491 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2492 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2493 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2494 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2495 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2496 };
2497
2498 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2499 if (!stats)
2500 return -EMSGSIZE;
2501
2502 for (i = 0; i < ARRAY_SIZE(map); i++)
2503 if (nla_put_u32(skb, map[i].key, map[i].val))
2504 goto msg_full;
2505
2506 nla_nest_end(skb, stats);
2507
2508 return 0;
2509msg_full:
2510 nla_nest_cancel(skb, stats);
2511
2512 return -EMSGSIZE;
2513}
2514
2515/* Caller should hold appropriate locks to protect the link */
Ying Xue34747532015-01-09 15:27:10 +08002516static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2517 struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002518{
2519 int err;
2520 void *hdr;
2521 struct nlattr *attrs;
2522 struct nlattr *prop;
Ying Xue34747532015-01-09 15:27:10 +08002523 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002524
2525 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2526 NLM_F_MULTI, TIPC_NL_LINK_GET);
2527 if (!hdr)
2528 return -EMSGSIZE;
2529
2530 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2531 if (!attrs)
2532 goto msg_full;
2533
2534 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2535 goto attr_msg_full;
2536 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
Ying Xue34747532015-01-09 15:27:10 +08002537 tipc_cluster_mask(tn->own_addr)))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002538 goto attr_msg_full;
2539 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2540 goto attr_msg_full;
2541 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2542 goto attr_msg_full;
2543 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2544 goto attr_msg_full;
2545
2546 if (tipc_link_is_up(link))
2547 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2548 goto attr_msg_full;
2549 if (tipc_link_is_active(link))
2550 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2551 goto attr_msg_full;
2552
2553 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2554 if (!prop)
2555 goto attr_msg_full;
2556 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2557 goto prop_msg_full;
2558 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2559 goto prop_msg_full;
2560 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2561 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2562 goto prop_msg_full;
2563 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2564 goto prop_msg_full;
2565 nla_nest_end(msg->skb, prop);
2566
2567 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2568 if (err)
2569 goto attr_msg_full;
2570
2571 nla_nest_end(msg->skb, attrs);
2572 genlmsg_end(msg->skb, hdr);
2573
2574 return 0;
2575
2576prop_msg_full:
2577 nla_nest_cancel(msg->skb, prop);
2578attr_msg_full:
2579 nla_nest_cancel(msg->skb, attrs);
2580msg_full:
2581 genlmsg_cancel(msg->skb, hdr);
2582
2583 return -EMSGSIZE;
2584}
2585
2586/* Caller should hold node lock */
Ying Xue34747532015-01-09 15:27:10 +08002587static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2588 struct tipc_node *node, u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002589{
2590 u32 i;
2591 int err;
2592
2593 for (i = *prev_link; i < MAX_BEARERS; i++) {
2594 *prev_link = i;
2595
2596 if (!node->links[i])
2597 continue;
2598
Ying Xue34747532015-01-09 15:27:10 +08002599 err = __tipc_nl_add_link(net, msg, node->links[i]);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002600 if (err)
2601 return err;
2602 }
2603 *prev_link = 0;
2604
2605 return 0;
2606}
2607
2608int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2609{
Ying Xuef2f98002015-01-09 15:27:05 +08002610 struct net *net = sock_net(skb->sk);
2611 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002612 struct tipc_node *node;
2613 struct tipc_nl_msg msg;
2614 u32 prev_node = cb->args[0];
2615 u32 prev_link = cb->args[1];
2616 int done = cb->args[2];
2617 int err;
2618
2619 if (done)
2620 return 0;
2621
2622 msg.skb = skb;
2623 msg.portid = NETLINK_CB(cb->skb).portid;
2624 msg.seq = cb->nlh->nlmsg_seq;
2625
2626 rcu_read_lock();
2627
2628 if (prev_node) {
Ying Xuef2f98002015-01-09 15:27:05 +08002629 node = tipc_node_find(net, prev_node);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002630 if (!node) {
2631 /* We never set seq or call nl_dump_check_consistent()
2632 * this means that setting prev_seq here will cause the
2633 * consistence check to fail in the netlink callback
2634 * handler. Resulting in the last NLMSG_DONE message
2635 * having the NLM_F_DUMP_INTR flag set.
2636 */
2637 cb->prev_seq = 1;
2638 goto out;
2639 }
2640
Ying Xuef2f98002015-01-09 15:27:05 +08002641 list_for_each_entry_continue_rcu(node, &tn->node_list,
2642 list) {
Richard Alpe7be57fc2014-11-20 10:29:12 +01002643 tipc_node_lock(node);
Ying Xue34747532015-01-09 15:27:10 +08002644 err = __tipc_nl_add_node_links(net, &msg, node,
2645 &prev_link);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002646 tipc_node_unlock(node);
2647 if (err)
2648 goto out;
2649
2650 prev_node = node->addr;
2651 }
2652 } else {
Ying Xue1da46562015-01-09 15:27:07 +08002653 err = tipc_nl_add_bc_link(net, &msg);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002654 if (err)
2655 goto out;
2656
Ying Xuef2f98002015-01-09 15:27:05 +08002657 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe7be57fc2014-11-20 10:29:12 +01002658 tipc_node_lock(node);
Ying Xue34747532015-01-09 15:27:10 +08002659 err = __tipc_nl_add_node_links(net, &msg, node,
2660 &prev_link);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002661 tipc_node_unlock(node);
2662 if (err)
2663 goto out;
2664
2665 prev_node = node->addr;
2666 }
2667 }
2668 done = 1;
2669out:
2670 rcu_read_unlock();
2671
2672 cb->args[0] = prev_node;
2673 cb->args[1] = prev_link;
2674 cb->args[2] = done;
2675
2676 return skb->len;
2677}
2678
2679int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2680{
Ying Xuef2f98002015-01-09 15:27:05 +08002681 struct net *net = genl_info_net(info);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002682 struct sk_buff *ans_skb;
2683 struct tipc_nl_msg msg;
2684 struct tipc_link *link;
2685 struct tipc_node *node;
2686 char *name;
2687 int bearer_id;
2688 int err;
2689
2690 if (!info->attrs[TIPC_NLA_LINK_NAME])
2691 return -EINVAL;
2692
2693 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
Ying Xuef2f98002015-01-09 15:27:05 +08002694 node = tipc_link_find_owner(net, name, &bearer_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002695 if (!node)
2696 return -EINVAL;
2697
2698 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2699 if (!ans_skb)
2700 return -ENOMEM;
2701
2702 msg.skb = ans_skb;
2703 msg.portid = info->snd_portid;
2704 msg.seq = info->snd_seq;
2705
2706 tipc_node_lock(node);
2707 link = node->links[bearer_id];
2708 if (!link) {
2709 err = -EINVAL;
2710 goto err_out;
2711 }
2712
Ying Xue34747532015-01-09 15:27:10 +08002713 err = __tipc_nl_add_link(net, &msg, link);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002714 if (err)
2715 goto err_out;
2716
2717 tipc_node_unlock(node);
2718
2719 return genlmsg_reply(ans_skb, info);
2720
2721err_out:
2722 tipc_node_unlock(node);
2723 nlmsg_free(ans_skb);
2724
2725 return err;
2726}
Richard Alpeae363422014-11-20 10:29:14 +01002727
2728int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2729{
2730 int err;
2731 char *link_name;
2732 unsigned int bearer_id;
2733 struct tipc_link *link;
2734 struct tipc_node *node;
2735 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Ying Xuef2f98002015-01-09 15:27:05 +08002736 struct net *net = genl_info_net(info);
Richard Alpeae363422014-11-20 10:29:14 +01002737
2738 if (!info->attrs[TIPC_NLA_LINK])
2739 return -EINVAL;
2740
2741 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2742 info->attrs[TIPC_NLA_LINK],
2743 tipc_nl_link_policy);
2744 if (err)
2745 return err;
2746
2747 if (!attrs[TIPC_NLA_LINK_NAME])
2748 return -EINVAL;
2749
2750 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2751
2752 if (strcmp(link_name, tipc_bclink_name) == 0) {
Ying Xue1da46562015-01-09 15:27:07 +08002753 err = tipc_bclink_reset_stats(net);
Richard Alpeae363422014-11-20 10:29:14 +01002754 if (err)
2755 return err;
2756 return 0;
2757 }
2758
Ying Xuef2f98002015-01-09 15:27:05 +08002759 node = tipc_link_find_owner(net, link_name, &bearer_id);
Richard Alpeae363422014-11-20 10:29:14 +01002760 if (!node)
2761 return -EINVAL;
2762
2763 tipc_node_lock(node);
2764
2765 link = node->links[bearer_id];
2766 if (!link) {
2767 tipc_node_unlock(node);
2768 return -EINVAL;
2769 }
2770
2771 link_reset_statistics(link);
2772
2773 tipc_node_unlock(node);
2774
2775 return 0;
2776}