blob: a84d5c67997ed3c614fc7d713a0ab1b4647cf60a [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{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500244 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 struct tipc_msg *msg;
246 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500247 char addr_string[16];
248 u32 peer = n_ptr->addr;
249
Holger Brunck0372bf52014-11-14 18:33:19 +0100250 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500251 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100252 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
253 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500254 return NULL;
255 }
256
257 if (n_ptr->links[b_ptr->identity]) {
258 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400259 pr_err("Attempt to establish second link on <%s> to %s\n",
260 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500261 return NULL;
262 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700264 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400266 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 return NULL;
268 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269
270 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500271 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400272 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900274 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 if_name,
276 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400277 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500279 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400281 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800282 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400283 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 l_ptr->state = RESET_UNKNOWN;
285
286 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
287 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000288 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700290 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 msg_set_bearer_id(msg, b_ptr->identity);
292 strcpy((char *)msg_data(msg), if_name);
293
294 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400295 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296
Ying Xue7a2f7d12014-04-21 10:55:46 +0800297 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 link_init_max_pkt(l_ptr);
299
300 l_ptr->next_out_no = 1;
Ying Xue58dc55f2014-11-26 11:41:52 +0800301 __skb_queue_head_init(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800302 __skb_queue_head_init(&l_ptr->deferred_queue);
Richard Alpe340b6e52014-12-10 09:46:54 +0100303 skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304
305 link_reset_statistics(l_ptr);
306
Allan Stephens37b9c082011-02-28 11:32:27 -0500307 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308
Ying Xue2f55c432015-01-09 15:27:00 +0800309 setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500310
311 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 return l_ptr;
314}
315
Ying Xuef2f98002015-01-09 15:27:05 +0800316void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
317 bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500318{
Ying Xuef2f98002015-01-09 15:27:05 +0800319 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue8d8439b2014-02-13 17:29:07 -0500320 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500321 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500322
Ying Xue6c7a7622014-03-27 12:54:37 +0800323 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +0800324 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800325 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500326 l_ptr = n_ptr->links[bearer_id];
327 if (l_ptr) {
328 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500329 if (shutting_down || !tipc_node_is_up(n_ptr)) {
330 tipc_node_detach_link(l_ptr->owner, l_ptr);
331 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800332 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500333
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500334 /* Nobody else can access this link now: */
335 del_timer_sync(&l_ptr->timer);
336 kfree(l_ptr);
337 } else {
338 /* Detach/delete when failover is finished: */
339 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800340 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500341 del_timer_sync(&l_ptr->timer);
342 }
Ying Xuec61dd612014-02-13 17:29:09 -0500343 continue;
344 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800345 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500346 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800347 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500348}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349
350/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400351 * link_schedule_user - schedule user for wakeup after congestion
352 * @link: congested link
353 * @oport: sending port
354 * @chain_sz: size of buffer chain that was attempted sent
355 * @imp: importance of message attempted sent
356 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400358static bool link_schedule_user(struct tipc_link *link, u32 oport,
359 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400361 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400363 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
364 tipc_own_addr, oport, 0, 0);
365 if (!buf)
366 return false;
367 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
368 TIPC_SKB_CB(buf)->chain_imp = imp;
Richard Alpe340b6e52014-12-10 09:46:54 +0100369 skb_queue_tail(&link->waiting_sks, buf);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400370 link->stats.link_congs++;
371 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372}
373
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400374/**
375 * link_prepare_wakeup - prepare users for wakeup after congestion
376 * @link: congested link
377 * Move a number of waiting users, as permitted by available space in
378 * the send queue, from link wait queue to node wait queue for wakeup
379 */
380static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381{
Ying Xue58dc55f2014-11-26 11:41:52 +0800382 uint pend_qsz = skb_queue_len(&link->outqueue);
Ying Xue58d78b32014-11-26 11:41:51 +0800383 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384
Ying Xue58d78b32014-11-26 11:41:51 +0800385 skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
386 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 break;
Ying Xue58d78b32014-11-26 11:41:51 +0800388 pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
Richard Alpe340b6e52014-12-10 09:46:54 +0100389 skb_unlink(skb, &link->waiting_sks);
390 skb_queue_tail(&link->owner->waiting_sks, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392}
393
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900394/**
Per Liden4323add2006-01-18 00:38:21 +0100395 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396 * @l_ptr: pointer to link
397 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500398void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400400 kfree_skb(l_ptr->reasm_buf);
401 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402}
403
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900404/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500405 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 * @l_ptr: pointer to link
407 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500408void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409{
Ying Xuebc6fecd2014-11-26 11:41:53 +0800410 __skb_queue_purge(&l_ptr->deferred_queue);
Ying Xue58dc55f2014-11-26 11:41:52 +0800411 __skb_queue_purge(&l_ptr->outqueue);
Per Liden4323add2006-01-18 00:38:21 +0100412 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413}
414
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500415void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 u32 prev_state = l_ptr->state;
418 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700419 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400420 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421
Allan Stephensa686e682008-06-04 17:29:39 -0700422 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423
Allan Stephensa686e682008-06-04 17:29:39 -0700424 /* Link is down, accept any session */
425 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900427 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900429
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431
432 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
433 return;
434
Per Liden4323add2006-01-18 00:38:21 +0100435 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800436 tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000437
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500438 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 l_ptr->reset_checkpoint = checkpoint;
440 l_ptr->exp_msg_count = START_CHANGEOVER;
441 }
442
443 /* Clean up all queues: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800444 __skb_queue_purge(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800445 __skb_queue_purge(&l_ptr->deferred_queue);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400446 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
447 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
448 owner->action_flags |= TIPC_WAKEUP_USERS;
449 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 l_ptr->next_out = NULL;
451 l_ptr->unacked_window = 0;
452 l_ptr->checkpoint = 1;
453 l_ptr->next_out_no = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 l_ptr->fsm_msg_cnt = 0;
455 l_ptr->stale_count = 0;
456 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457}
458
Ying Xuef2f98002015-01-09 15:27:05 +0800459void tipc_link_reset_list(struct net *net, unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500460{
Ying Xuef2f98002015-01-09 15:27:05 +0800461 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500462 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500463 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500464
Ying Xue6c7a7622014-03-27 12:54:37 +0800465 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +0800466 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800467 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500468 l_ptr = n_ptr->links[bearer_id];
469 if (l_ptr)
470 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800471 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500472 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800473 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500474}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475
Ying Xue7f9f95d2015-01-09 15:27:06 +0800476static void link_activate(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800478 struct tipc_node *node = link->owner;
479
480 link->next_in_no = 1;
481 link->stats.recv_info = 1;
482 tipc_node_link_up(node, link);
483 tipc_bearer_add_dest(node->net, link->bearer_id, link->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484}
485
486/**
487 * link_state_event - link finite state machine
488 * @l_ptr: pointer to link
489 * @event: state machine event to process
490 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000491static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500493 struct tipc_link *other;
Ying Xue2f55c432015-01-09 15:27:00 +0800494 unsigned long cont_intv = l_ptr->cont_intv;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500496 if (l_ptr->flags & LINK_STOPPED)
497 return;
498
Ying Xue135daee2014-02-13 17:29:08 -0500499 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 return; /* Not yet. */
501
Ying Xue77a7e072013-12-10 20:45:44 -0800502 /* Check whether changeover is going on */
503 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000504 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800506 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508
509 switch (l_ptr->state) {
510 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 switch (event) {
512 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 break;
515 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 if (l_ptr->next_in_no != l_ptr->checkpoint) {
517 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100518 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800519 tipc_link_proto_xmit(l_ptr, STATE_MSG,
520 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 l_ptr->fsm_msg_cnt++;
522 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800523 tipc_link_proto_xmit(l_ptr, STATE_MSG,
524 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 l_ptr->fsm_msg_cnt++;
526 }
527 link_set_timer(l_ptr, cont_intv);
528 break;
529 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 l_ptr->state = WORKING_UNKNOWN;
531 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800532 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 l_ptr->fsm_msg_cnt++;
534 link_set_timer(l_ptr, cont_intv / 4);
535 break;
536 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400537 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
538 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100539 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 l_ptr->state = RESET_RESET;
541 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800542 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
543 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 l_ptr->fsm_msg_cnt++;
545 link_set_timer(l_ptr, cont_intv);
546 break;
547 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400548 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 }
550 break;
551 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 switch (event) {
553 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 l_ptr->state = WORKING_WORKING;
556 l_ptr->fsm_msg_cnt = 0;
557 link_set_timer(l_ptr, cont_intv);
558 break;
559 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400560 pr_info("%s<%s>, requested by peer while probing\n",
561 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100562 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 l_ptr->state = RESET_RESET;
564 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800565 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
566 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 l_ptr->fsm_msg_cnt++;
568 link_set_timer(l_ptr, cont_intv);
569 break;
570 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 l_ptr->state = WORKING_WORKING;
573 l_ptr->fsm_msg_cnt = 0;
574 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100575 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800576 tipc_link_proto_xmit(l_ptr, STATE_MSG,
577 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 l_ptr->fsm_msg_cnt++;
579 }
580 link_set_timer(l_ptr, cont_intv);
581 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800582 tipc_link_proto_xmit(l_ptr, STATE_MSG,
583 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 l_ptr->fsm_msg_cnt++;
585 link_set_timer(l_ptr, cont_intv / 4);
586 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400587 pr_warn("%s<%s>, peer not responding\n",
588 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100589 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 l_ptr->state = RESET_UNKNOWN;
591 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800592 tipc_link_proto_xmit(l_ptr, RESET_MSG,
593 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 l_ptr->fsm_msg_cnt++;
595 link_set_timer(l_ptr, cont_intv);
596 }
597 break;
598 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400599 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 }
601 break;
602 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 switch (event) {
604 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 break;
606 case ACTIVATE_MSG:
607 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000608 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 l_ptr->state = WORKING_WORKING;
611 l_ptr->fsm_msg_cnt = 0;
612 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800613 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800615 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800616 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 link_set_timer(l_ptr, cont_intv);
618 break;
619 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 l_ptr->state = RESET_RESET;
621 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800622 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
623 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 l_ptr->fsm_msg_cnt++;
625 link_set_timer(l_ptr, cont_intv);
626 break;
627 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500628 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 /* fall through */
630 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800631 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 l_ptr->fsm_msg_cnt++;
633 link_set_timer(l_ptr, cont_intv);
634 break;
635 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400636 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 }
638 break;
639 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 switch (event) {
641 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 case ACTIVATE_MSG:
643 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000644 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 l_ptr->state = WORKING_WORKING;
647 l_ptr->fsm_msg_cnt = 0;
648 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800649 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800651 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800652 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 link_set_timer(l_ptr, cont_intv);
654 break;
655 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 break;
657 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800658 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
659 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 l_ptr->fsm_msg_cnt++;
661 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 break;
663 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400664 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 }
666 break;
667 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400668 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 }
670}
671
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500672/* tipc_link_cong: determine return value and how to treat the
673 * sent buffer during link congestion.
674 * - For plain, errorless user data messages we keep the buffer and
675 * return -ELINKONG.
676 * - For all other messages we discard the buffer and return -EHOSTUNREACH
677 * - For TIPC internal messages we also reset the link
678 */
Ying Xuea6ca1092014-11-26 11:41:55 +0800679static int tipc_link_cong(struct tipc_link *link, struct sk_buff_head *list)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500680{
Ying Xuea6ca1092014-11-26 11:41:55 +0800681 struct sk_buff *skb = skb_peek(list);
682 struct tipc_msg *msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500683 uint imp = tipc_msg_tot_importance(msg);
684 u32 oport = msg_tot_origport(msg);
685
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400686 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500687 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
688 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400689 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500690 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400691 if (unlikely(msg_errcode(msg)))
692 goto drop;
693 if (unlikely(msg_reroute_cnt(msg)))
694 goto drop;
Ying Xuea6ca1092014-11-26 11:41:55 +0800695 if (TIPC_SKB_CB(skb)->wakeup_pending)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400696 return -ELINKCONG;
Ying Xuea6ca1092014-11-26 11:41:55 +0800697 if (link_schedule_user(link, oport, skb_queue_len(list), imp))
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400698 return -ELINKCONG;
699drop:
Ying Xuea6ca1092014-11-26 11:41:55 +0800700 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500701 return -EHOSTUNREACH;
702}
703
704/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400705 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500706 * @link: link to use
Ying Xuea6ca1092014-11-26 11:41:55 +0800707 * @list: chain of buffers containing message
708 *
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500709 * Consumes the buffer chain, except when returning -ELINKCONG
710 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
711 * user data messages) or -EHOSTUNREACH (all other messages/senders)
712 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
713 * to act on the return value, since they may need to do more send attempts.
714 */
Ying Xue7f9f95d2015-01-09 15:27:06 +0800715int __tipc_link_xmit(struct net *net, struct tipc_link *link,
716 struct sk_buff_head *list)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500717{
Ying Xuea6ca1092014-11-26 11:41:55 +0800718 struct tipc_msg *msg = buf_msg(skb_peek(list));
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500719 uint psz = msg_size(msg);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500720 uint sndlim = link->queue_limit[0];
721 uint imp = tipc_msg_tot_importance(msg);
722 uint mtu = link->max_pkt;
723 uint ack = mod(link->next_in_no - 1);
724 uint seqno = link->next_out_no;
725 uint bc_last_in = link->owner->bclink.last_in;
726 struct tipc_media_addr *addr = &link->media_addr;
Ying Xue58dc55f2014-11-26 11:41:52 +0800727 struct sk_buff_head *outqueue = &link->outqueue;
Ying Xuea6ca1092014-11-26 11:41:55 +0800728 struct sk_buff *skb, *tmp;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500729
730 /* Match queue limits against msg importance: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800731 if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp]))
Ying Xuea6ca1092014-11-26 11:41:55 +0800732 return tipc_link_cong(link, list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500733
734 /* Has valid packet limit been used ? */
735 if (unlikely(psz > mtu)) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800736 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500737 return -EMSGSIZE;
738 }
739
740 /* Prepare each packet for sending, and add to outqueue: */
Ying Xuea6ca1092014-11-26 11:41:55 +0800741 skb_queue_walk_safe(list, skb, tmp) {
742 __skb_unlink(skb, list);
Ying Xue58dc55f2014-11-26 11:41:52 +0800743 msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500744 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
745 msg_set_bcast_ack(msg, bc_last_in);
746
Ying Xue58dc55f2014-11-26 11:41:52 +0800747 if (skb_queue_len(outqueue) < sndlim) {
748 __skb_queue_tail(outqueue, skb);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800749 tipc_bearer_send(net, link->bearer_id,
750 skb, addr);
Ying Xue58dc55f2014-11-26 11:41:52 +0800751 link->next_out = NULL;
752 link->unacked_window = 0;
753 } else if (tipc_msg_bundle(outqueue, skb, mtu)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500754 link->stats.sent_bundled++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500755 continue;
Ying Xue58dc55f2014-11-26 11:41:52 +0800756 } else if (tipc_msg_make_bundle(outqueue, skb, mtu,
757 link->addr)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500758 link->stats.sent_bundled++;
759 link->stats.sent_bundles++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500760 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800761 link->next_out = skb_peek_tail(outqueue);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500762 } else {
Ying Xue58dc55f2014-11-26 11:41:52 +0800763 __skb_queue_tail(outqueue, skb);
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;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500766 }
767 seqno++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500768 }
769 link->next_out_no = seqno;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500770 return 0;
771}
772
Ying Xuea6ca1092014-11-26 11:41:55 +0800773static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
774{
775 __skb_queue_head_init(list);
776 __skb_queue_tail(list, skb);
777}
778
779static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
780{
781 struct sk_buff_head head;
782
783 skb2list(skb, &head);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800784 return __tipc_link_xmit(link->owner->net, link, &head);
Ying Xuea6ca1092014-11-26 11:41:55 +0800785}
786
Ying Xuef2f98002015-01-09 15:27:05 +0800787int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
788 u32 selector)
Ying Xuea6ca1092014-11-26 11:41:55 +0800789{
790 struct sk_buff_head head;
791
792 skb2list(skb, &head);
Ying Xuef2f98002015-01-09 15:27:05 +0800793 return tipc_link_xmit(net, &head, dnode, selector);
Ying Xuea6ca1092014-11-26 11:41:55 +0800794}
795
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500796/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400797 * tipc_link_xmit() is the general link level function for message sending
Ying Xuef2f98002015-01-09 15:27:05 +0800798 * @net: the applicable net namespace
Ying Xuea6ca1092014-11-26 11:41:55 +0800799 * @list: chain of buffers containing message
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500800 * @dsz: amount of user data to be sent
801 * @dnode: address of destination node
802 * @selector: a number used for deterministic link selection
803 * Consumes the buffer chain, except when returning -ELINKCONG
804 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
805 */
Ying Xuef2f98002015-01-09 15:27:05 +0800806int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
807 u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500808{
809 struct tipc_link *link = NULL;
810 struct tipc_node *node;
811 int rc = -EHOSTUNREACH;
812
Ying Xuef2f98002015-01-09 15:27:05 +0800813 node = tipc_node_find(net, dnode);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500814 if (node) {
815 tipc_node_lock(node);
816 link = node->active_links[selector & 1];
817 if (link)
Ying Xue7f9f95d2015-01-09 15:27:06 +0800818 rc = __tipc_link_xmit(net, link, list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500819 tipc_node_unlock(node);
820 }
821
822 if (link)
823 return rc;
824
Ying Xuea6ca1092014-11-26 11:41:55 +0800825 if (likely(in_own_node(dnode))) {
826 /* As a node local message chain never contains more than one
827 * buffer, we just need to dequeue one SKB buffer from the
828 * head list.
829 */
Ying Xuef2f98002015-01-09 15:27:05 +0800830 return tipc_sk_rcv(net, __skb_dequeue(list));
Ying Xuea6ca1092014-11-26 11:41:55 +0800831 }
832 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500833
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500834 return rc;
835}
836
Jon Maloyc64f7a62012-11-16 13:51:31 +0800837/*
Ying Xue247f0f32014-02-18 16:06:46 +0800838 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800839 *
840 * Give a newly added peer node the sequence number where it should
841 * start receiving and acking broadcast packets.
842 *
843 * Called with node locked
844 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400845static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800846{
Ying Xuea6ca1092014-11-26 11:41:55 +0800847 struct sk_buff *skb;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800848 struct tipc_msg *msg;
849
Ying Xuea6ca1092014-11-26 11:41:55 +0800850 skb = tipc_buf_acquire(INT_H_SIZE);
851 if (!skb)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800852 return;
853
Ying Xuea6ca1092014-11-26 11:41:55 +0800854 msg = buf_msg(skb);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400855 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
856 msg_set_last_bcast(msg, link->owner->bclink.acked);
Ying Xuea6ca1092014-11-26 11:41:55 +0800857 __tipc_link_xmit_skb(link, skb);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800858}
859
860/*
Ying Xue247f0f32014-02-18 16:06:46 +0800861 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800862 * Receive the sequence number where we should start receiving and
863 * acking broadcast packets from a newly added peer node, and open
864 * up for reception of such packets.
865 *
866 * Called with node locked
867 */
Ying Xue247f0f32014-02-18 16:06:46 +0800868static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800869{
870 struct tipc_msg *msg = buf_msg(buf);
871
872 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
873 n->bclink.recv_permitted = true;
874 kfree_skb(buf);
875}
876
Ying Xue58dc55f2014-11-26 11:41:52 +0800877struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
878 const struct sk_buff *skb)
879{
880 if (skb_queue_is_last(list, skb))
881 return NULL;
882 return skb->next;
883}
884
Jon Maloyc64f7a62012-11-16 13:51:31 +0800885/*
Ying Xue47b4c9a2014-11-26 11:41:48 +0800886 * tipc_link_push_packets - push unsent packets to bearer
887 *
888 * Push out the unsent messages of a link where congestion
889 * has abated. Node is locked.
890 *
891 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 */
Ying Xue47b4c9a2014-11-26 11:41:48 +0800893void tipc_link_push_packets(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894{
Ying Xue58dc55f2014-11-26 11:41:52 +0800895 struct sk_buff_head *outqueue = &l_ptr->outqueue;
896 struct sk_buff *skb = l_ptr->next_out;
Ying Xue47b4c9a2014-11-26 11:41:48 +0800897 struct tipc_msg *msg;
898 u32 next, first;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899
Ying Xue58dc55f2014-11-26 11:41:52 +0800900 skb_queue_walk_from(outqueue, skb) {
Ying Xue47b4c9a2014-11-26 11:41:48 +0800901 msg = buf_msg(skb);
902 next = msg_seqno(msg);
Ying Xue58dc55f2014-11-26 11:41:52 +0800903 first = buf_seqno(skb_peek(outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904
905 if (mod(next - first) < l_ptr->queue_limit[0]) {
906 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900907 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +0800908 if (msg_user(msg) == MSG_BUNDLER)
Ying Xue58311d12014-11-26 11:41:49 +0800909 TIPC_SKB_CB(skb)->bundling = false;
Ying Xue7f9f95d2015-01-09 15:27:06 +0800910 tipc_bearer_send(l_ptr->owner->net,
911 l_ptr->bearer_id, skb,
Ying Xue47b4c9a2014-11-26 11:41:48 +0800912 &l_ptr->media_addr);
Ying Xue58dc55f2014-11-26 11:41:52 +0800913 l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
Ying Xue47b4c9a2014-11-26 11:41:48 +0800914 } else {
915 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 }
917 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918}
919
Ying Xue3f5a12b2014-05-05 08:56:17 +0800920void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700921{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700922 char addr_string[16];
923 u32 i;
924
Ying Xue3f5a12b2014-05-05 08:56:17 +0800925 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700926
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400927 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800928 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700929
930 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800931 if (node->links[i]) {
932 link_print(node->links[i], "Resetting link\n");
933 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700934 }
935 }
936
Ying Xue3f5a12b2014-05-05 08:56:17 +0800937 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700938}
939
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500940static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400941 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700942{
943 struct tipc_msg *msg = buf_msg(buf);
Ying Xue1da46562015-01-09 15:27:07 +0800944 struct net *net = l_ptr->owner->net;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700945
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400946 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700947
948 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700949 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000950 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700951 tipc_link_reset(l_ptr);
952
953 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700954 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700955 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700956 char addr_string[16];
957
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400958 pr_info("Msg seq number: %u, ", msg_seqno(msg));
959 pr_cont("Outstanding acks: %lu\n",
960 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700961
Ying Xue1da46562015-01-09 15:27:07 +0800962 n_ptr = tipc_bclink_retransmit_to(net);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700963 tipc_node_lock(n_ptr);
964
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000965 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400966 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800967 pr_info("Reception permitted: %d, Acked: %u\n",
968 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400969 n_ptr->bclink.acked);
970 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
971 n_ptr->bclink.last_in,
972 n_ptr->bclink.oos_state,
973 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700974
Allan Stephensd356eeb2006-06-25 23:40:01 -0700975 tipc_node_unlock(n_ptr);
976
Ying Xue1da46562015-01-09 15:27:07 +0800977 tipc_bclink_set_flags(net, TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700978 l_ptr->stale_count = 0;
979 }
980}
981
Ying Xue58dc55f2014-11-26 11:41:52 +0800982void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
Per Liden4323add2006-01-18 00:38:21 +0100983 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984{
985 struct tipc_msg *msg;
986
Ying Xue58dc55f2014-11-26 11:41:52 +0800987 if (!skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700988 return;
989
Ying Xue58dc55f2014-11-26 11:41:52 +0800990 msg = buf_msg(skb);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900991
Erik Hugne512137e2013-12-06 10:08:00 -0500992 /* Detect repeated retransmit failures */
993 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
994 if (++l_ptr->stale_count > 100) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800995 link_retransmit_failure(l_ptr, skb);
Erik Hugne512137e2013-12-06 10:08:00 -0500996 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700997 }
998 } else {
Erik Hugne512137e2013-12-06 10:08:00 -0500999 l_ptr->last_retransmitted = msg_seqno(msg);
1000 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001002
Ying Xue58dc55f2014-11-26 11:41:52 +08001003 skb_queue_walk_from(&l_ptr->outqueue, skb) {
1004 if (!retransmits || skb == l_ptr->next_out)
1005 break;
1006 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001008 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7f9f95d2015-01-09 15:27:06 +08001009 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
1010 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001011 retransmits--;
1012 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014}
1015
Ying Xuef03273f2014-11-26 11:41:54 +08001016static void link_retrieve_defq(struct tipc_link *link,
1017 struct sk_buff_head *list)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018{
1019 u32 seq_no;
1020
Ying Xuef03273f2014-11-26 11:41:54 +08001021 if (skb_queue_empty(&link->deferred_queue))
1022 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023
Ying Xuef03273f2014-11-26 11:41:54 +08001024 seq_no = buf_seqno(skb_peek(&link->deferred_queue));
1025 if (seq_no == mod(link->next_in_no))
1026 skb_queue_splice_tail_init(&link->deferred_queue, list);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027}
1028
Allan Stephens85035562008-04-15 19:04:54 -07001029/**
1030 * link_recv_buf_validate - validate basic format of received message
1031 *
1032 * This routine ensures a TIPC message has an acceptable header, and at least
1033 * as much data as the header indicates it should. The routine also ensures
1034 * that the entire message header is stored in the main fragment of the message
1035 * buffer, to simplify future access to message header fields.
1036 *
1037 * Note: Having extra info present in the message header or data areas is OK.
1038 * TIPC will ignore the excess, under the assumption that it is optional info
1039 * introduced by a later release of the protocol.
1040 */
Allan Stephens85035562008-04-15 19:04:54 -07001041static int link_recv_buf_validate(struct sk_buff *buf)
1042{
1043 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001044 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001045 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1046 };
1047
1048 struct tipc_msg *msg;
1049 u32 tipc_hdr[2];
1050 u32 size;
1051 u32 hdr_size;
1052 u32 min_hdr_size;
1053
Erik Hugne64380a02014-02-11 11:38:26 +01001054 /* If this packet comes from the defer queue, the skb has already
1055 * been validated
1056 */
1057 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1058 return 1;
1059
Allan Stephens85035562008-04-15 19:04:54 -07001060 if (unlikely(buf->len < MIN_H_SIZE))
1061 return 0;
1062
1063 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1064 if (msg == NULL)
1065 return 0;
1066
1067 if (unlikely(msg_version(msg) != TIPC_VERSION))
1068 return 0;
1069
1070 size = msg_size(msg);
1071 hdr_size = msg_hdr_sz(msg);
1072 min_hdr_size = msg_isdata(msg) ?
1073 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1074
1075 if (unlikely((hdr_size < min_hdr_size) ||
1076 (size < hdr_size) ||
1077 (buf->len < size) ||
1078 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1079 return 0;
1080
1081 return pskb_may_pull(buf, hdr_size);
1082}
1083
Allan Stephensb02b69c2010-08-17 11:00:07 +00001084/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001085 * tipc_rcv - process TIPC packets/messages arriving from off-node
Ying Xuef2f98002015-01-09 15:27:05 +08001086 * @net: the applicable net namespace
Ying Xuef03273f2014-11-26 11:41:54 +08001087 * @skb: TIPC packet
Ying Xue7a2f7d12014-04-21 10:55:46 +08001088 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001089 *
1090 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1091 * structure (i.e. cannot be NULL), but bearer can be inactive.
1092 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001093void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094{
Ying Xuef03273f2014-11-26 11:41:54 +08001095 struct sk_buff_head head;
1096 struct tipc_node *n_ptr;
1097 struct tipc_link *l_ptr;
1098 struct sk_buff *skb1, *tmp;
1099 struct tipc_msg *msg;
1100 u32 seq_no;
1101 u32 ackd;
1102 u32 released;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103
Ying Xuea6ca1092014-11-26 11:41:55 +08001104 skb2list(skb, &head);
Allan Stephens85035562008-04-15 19:04:54 -07001105
Ying Xuef03273f2014-11-26 11:41:54 +08001106 while ((skb = __skb_dequeue(&head))) {
Allan Stephens85035562008-04-15 19:04:54 -07001107 /* Ensure message is well-formed */
Ying Xuef03273f2014-11-26 11:41:54 +08001108 if (unlikely(!link_recv_buf_validate(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001109 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110
Allan Stephensfe13dda2008-04-15 19:03:23 -07001111 /* Ensure message data is a single contiguous unit */
Ying Xuef03273f2014-11-26 11:41:54 +08001112 if (unlikely(skb_linearize(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001113 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001114
Allan Stephens85035562008-04-15 19:04:54 -07001115 /* Handle arrival of a non-unicast link message */
Ying Xuef03273f2014-11-26 11:41:54 +08001116 msg = buf_msg(skb);
Allan Stephens85035562008-04-15 19:04:54 -07001117
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001119 if (msg_user(msg) == LINK_CONFIG)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001120 tipc_disc_rcv(net, skb, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001121 else
Ying Xuec93d3ba2015-01-09 15:27:04 +08001122 tipc_bclink_rcv(net, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 continue;
1124 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001125
Allan Stephensed33a9c2011-04-05 15:15:04 -04001126 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001127 if (unlikely(!msg_short(msg) &&
1128 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001129 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001130
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001131 /* Locate neighboring node that sent message */
Ying Xuef2f98002015-01-09 15:27:05 +08001132 n_ptr = tipc_node_find(net, msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001134 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001135 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001136
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001137 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001139 if (unlikely(!l_ptr))
1140 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001141
Allan Stephensb4b56102011-05-27 11:00:51 -04001142 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001143 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001144 msg_user(msg) == LINK_PROTOCOL &&
1145 (msg_type(msg) == RESET_MSG ||
1146 msg_type(msg) == ACTIVATE_MSG) &&
1147 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001148 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001149
Ying Xue10f465c2014-05-05 08:56:11 +08001150 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001151 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001152
Allan Stephens85035562008-04-15 19:04:54 -07001153 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001154 seq_no = msg_seqno(msg);
1155 ackd = msg_ack(msg);
1156
1157 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001158 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001159 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160
Ying Xue58dc55f2014-11-26 11:41:52 +08001161 released = 0;
1162 skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) {
1163 if (skb1 == l_ptr->next_out ||
1164 more(buf_seqno(skb1), ackd))
1165 break;
1166 __skb_unlink(skb1, &l_ptr->outqueue);
1167 kfree_skb(skb1);
1168 released = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 }
Allan Stephens85035562008-04-15 19:04:54 -07001170
1171 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 if (unlikely(l_ptr->next_out))
Ying Xue47b4c9a2014-11-26 11:41:48 +08001173 tipc_link_push_packets(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001174
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001175 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1176 link_prepare_wakeup(l_ptr);
1177 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1178 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001179
Jon Paul Maloya5377832014-02-13 17:29:15 -05001180 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001181 if (unlikely(!link_working_working(l_ptr))) {
1182 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001183 tipc_link_proto_rcv(net, l_ptr, skb);
Ying Xuef03273f2014-11-26 11:41:54 +08001184 link_retrieve_defq(l_ptr, &head);
Per Liden4323add2006-01-18 00:38:21 +01001185 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 continue;
1187 }
Ying Xue3af390e2013-10-30 11:26:57 +08001188
1189 /* Traffic message. Conditionally activate link */
1190 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1191
1192 if (link_working_working(l_ptr)) {
1193 /* Re-insert buffer in front of queue */
Ying Xuef03273f2014-11-26 11:41:54 +08001194 __skb_queue_head(&head, skb);
Ying Xue3af390e2013-10-30 11:26:57 +08001195 tipc_node_unlock(n_ptr);
1196 continue;
1197 }
1198 goto unlock_discard;
1199 }
1200
1201 /* Link is now in state WORKING_WORKING */
1202 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001203 link_handle_out_of_seq_msg(net, l_ptr, skb);
Ying Xuef03273f2014-11-26 11:41:54 +08001204 link_retrieve_defq(l_ptr, &head);
Per Liden4323add2006-01-18 00:38:21 +01001205 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 continue;
1207 }
Ying Xue3af390e2013-10-30 11:26:57 +08001208 l_ptr->next_in_no++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001209 if (unlikely(!skb_queue_empty(&l_ptr->deferred_queue)))
Ying Xuef03273f2014-11-26 11:41:54 +08001210 link_retrieve_defq(l_ptr, &head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001211
Erik Hugne3f53bd82014-07-01 10:22:41 +02001212 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1213 l_ptr->stats.sent_acks++;
1214 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1215 }
1216
Ying Xuec93d3ba2015-01-09 15:27:04 +08001217 if (tipc_link_prepare_input(net, l_ptr, &skb)) {
Per Liden4323add2006-01-18 00:38:21 +01001218 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 }
Per Liden4323add2006-01-18 00:38:21 +01001221 tipc_node_unlock(n_ptr);
Ying Xuef03273f2014-11-26 11:41:54 +08001222
Ying Xuef2f98002015-01-09 15:27:05 +08001223 if (tipc_link_input(net, l_ptr, skb) != 0)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001224 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001225 continue;
1226unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001227 tipc_node_unlock(n_ptr);
1228discard:
Ying Xuef03273f2014-11-26 11:41:54 +08001229 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231}
1232
Ben Hutchings2c530402012-07-10 10:55:09 +00001233/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001234 * tipc_link_prepare_input - process TIPC link messages
1235 *
1236 * returns nonzero if the message was consumed
1237 *
1238 * Node lock must be held
1239 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001240static int tipc_link_prepare_input(struct net *net, struct tipc_link *l,
1241 struct sk_buff **buf)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001242{
1243 struct tipc_node *n;
1244 struct tipc_msg *msg;
1245 int res = -EINVAL;
1246
1247 n = l->owner;
1248 msg = buf_msg(*buf);
1249 switch (msg_user(msg)) {
1250 case CHANGEOVER_PROTOCOL:
Ying Xuec93d3ba2015-01-09 15:27:04 +08001251 if (tipc_link_tunnel_rcv(net, n, buf))
Erik Hugne7ae934b2014-07-01 10:22:40 +02001252 res = 0;
1253 break;
1254 case MSG_FRAGMENTER:
1255 l->stats.recv_fragments++;
1256 if (tipc_buf_append(&l->reasm_buf, buf)) {
1257 l->stats.recv_fragmented++;
1258 res = 0;
1259 } else if (!l->reasm_buf) {
1260 tipc_link_reset(l);
1261 }
1262 break;
1263 case MSG_BUNDLER:
1264 l->stats.recv_bundles++;
1265 l->stats.recv_bundled += msg_msgcnt(msg);
1266 res = 0;
1267 break;
1268 case NAME_DISTRIBUTOR:
1269 n->bclink.recv_permitted = true;
1270 res = 0;
1271 break;
1272 case BCAST_PROTOCOL:
1273 tipc_link_sync_rcv(n, *buf);
1274 break;
1275 default:
1276 res = 0;
1277 }
1278 return res;
1279}
1280/**
1281 * tipc_link_input - Deliver message too higher layers
1282 */
Ying Xuef2f98002015-01-09 15:27:05 +08001283static int tipc_link_input(struct net *net, struct tipc_link *l,
1284 struct sk_buff *buf)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001285{
1286 struct tipc_msg *msg = buf_msg(buf);
1287 int res = 0;
1288
1289 switch (msg_user(msg)) {
1290 case TIPC_LOW_IMPORTANCE:
1291 case TIPC_MEDIUM_IMPORTANCE:
1292 case TIPC_HIGH_IMPORTANCE:
1293 case TIPC_CRITICAL_IMPORTANCE:
1294 case CONN_MANAGER:
Ying Xuef2f98002015-01-09 15:27:05 +08001295 tipc_sk_rcv(net, buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001296 break;
1297 case NAME_DISTRIBUTOR:
Ying Xuef2f98002015-01-09 15:27:05 +08001298 tipc_named_rcv(net, buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001299 break;
1300 case MSG_BUNDLER:
Ying Xuef2f98002015-01-09 15:27:05 +08001301 tipc_link_bundle_rcv(net, buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001302 break;
1303 default:
1304 res = -EINVAL;
1305 }
1306 return res;
1307}
1308
1309/**
Allan Stephens8809b252011-10-25 10:44:35 -04001310 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1311 *
1312 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001314u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315{
Ying Xuebc6fecd2014-11-26 11:41:53 +08001316 struct sk_buff *skb1;
1317 u32 seq_no = buf_seqno(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318
1319 /* Empty queue ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001320 if (skb_queue_empty(list)) {
1321 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 return 1;
1323 }
1324
1325 /* Last ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001326 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1327 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 return 1;
1329 }
1330
Allan Stephens8809b252011-10-25 10:44:35 -04001331 /* Locate insertion point in queue, then insert; discard if duplicate */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001332 skb_queue_walk(list, skb1) {
1333 u32 curr_seqno = buf_seqno(skb1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334
Allan Stephens8809b252011-10-25 10:44:35 -04001335 if (seq_no == curr_seqno) {
Ying Xuebc6fecd2014-11-26 11:41:53 +08001336 kfree_skb(skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001337 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 }
Allan Stephens8809b252011-10-25 10:44:35 -04001339
1340 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 break;
Allan Stephens8809b252011-10-25 10:44:35 -04001342 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343
Ying Xuebc6fecd2014-11-26 11:41:53 +08001344 __skb_queue_before(list, skb1, skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001345 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346}
1347
Allan Stephens8809b252011-10-25 10:44:35 -04001348/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1350 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001351static void link_handle_out_of_seq_msg(struct net *net,
1352 struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 struct sk_buff *buf)
1354{
Allan Stephensf9057302011-10-24 16:03:12 -04001355 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356
1357 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xuec93d3ba2015-01-09 15:27:04 +08001358 tipc_link_proto_rcv(net, l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 return;
1360 }
1361
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 l_ptr->checkpoint--;
1364
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001365 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 * Discard packet if a duplicate; otherwise add it to deferred queue
1367 * and notify peer of gap as per protocol specification
1368 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369 if (less(seq_no, mod(l_ptr->next_in_no))) {
1370 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001371 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 return;
1373 }
1374
Ying Xuebc6fecd2014-11-26 11:41:53 +08001375 if (tipc_link_defer_pkt(&l_ptr->deferred_queue, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001377 TIPC_SKB_CB(buf)->deferred = true;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001378 if ((skb_queue_len(&l_ptr->deferred_queue) % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001379 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001380 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 l_ptr->stats.duplicates++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001382 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383}
1384
1385/*
1386 * Send protocol message to the other endpoint.
1387 */
Ying Xue247f0f32014-02-18 16:06:46 +08001388void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1389 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001391 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001393 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001394 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395
Ying Xue77a7e072013-12-10 20:45:44 -08001396 /* Don't send protocol message during link changeover */
1397 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001399
1400 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001401 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001402 return;
1403
Allan Stephens92d2c902011-10-25 11:20:26 -04001404 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001406 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001407 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue1da46562015-01-09 15:27:07 +08001408 msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409
1410 if (msg_typ == STATE_MSG) {
1411 u32 next_sent = mod(l_ptr->next_out_no);
1412
Per Liden4323add2006-01-18 00:38:21 +01001413 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414 return;
1415 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001416 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 msg_set_next_sent(msg, next_sent);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001418 if (!skb_queue_empty(&l_ptr->deferred_queue)) {
1419 u32 rec = buf_seqno(skb_peek(&l_ptr->deferred_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 gap = mod(rec - mod(l_ptr->next_in_no));
1421 }
1422 msg_set_seq_gap(msg, gap);
1423 if (gap)
1424 l_ptr->stats.sent_nacks++;
1425 msg_set_link_tolerance(msg, tolerance);
1426 msg_set_linkprio(msg, priority);
1427 msg_set_max_pkt(msg, ack_mtu);
1428 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1429 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001430 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 u32 mtu = l_ptr->max_pkt;
1432
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001433 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 link_working_working(l_ptr) &&
1435 l_ptr->fsm_msg_cnt) {
1436 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001437 if (l_ptr->max_pkt_probes == 10) {
1438 l_ptr->max_pkt_target = (msg_size - 4);
1439 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001441 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001443 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444
1445 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001446 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447 l_ptr->stats.sent_states++;
1448 } else { /* RESET_MSG or ACTIVATE_MSG */
1449 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1450 msg_set_seq_gap(msg, 0);
1451 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001452 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 msg_set_link_tolerance(msg, l_ptr->tolerance);
1454 msg_set_linkprio(msg, l_ptr->priority);
1455 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1456 }
1457
Allan Stephens75f0aa42011-02-28 15:30:20 -05001458 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1459 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001461 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462
1463 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1464
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001465 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 if (!buf)
1467 return;
1468
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001469 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001470 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471
Ying Xue7f9f95d2015-01-09 15:27:06 +08001472 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
1473 &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001474 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001475 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476}
1477
1478/*
1479 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001480 * Note that network plane id propagates through the network, and may
1481 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001483static void tipc_link_proto_rcv(struct net *net, struct tipc_link *l_ptr,
1484 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485{
1486 u32 rec_gap = 0;
1487 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001488 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489 u32 msg_tol;
1490 struct tipc_msg *msg = buf_msg(buf);
1491
Ying Xue77a7e072013-12-10 20:45:44 -08001492 /* Discard protocol message during link changeover */
1493 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 goto exit;
1495
Ying Xue7a2f7d12014-04-21 10:55:46 +08001496 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001498 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001501
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001503 if (!link_working_unknown(l_ptr) &&
1504 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001505 if (less_eq(msg_session(msg), l_ptr->peer_session))
1506 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001508
1509 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1510 link_working_unknown(l_ptr))) {
1511 /*
1512 * peer has lost contact -- don't allow peer's links
1513 * to reactivate before we recognize loss & clean up
1514 */
Ying Xueca9cf062014-05-08 08:54:40 +08001515 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001516 }
1517
Allan Stephens47361c82011-10-26 10:55:16 -04001518 link_state_event(l_ptr, RESET_MSG);
1519
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 /* fall thru' */
1521 case ACTIVATE_MSG:
1522 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001523 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1524
Allan Stephens2db99832010-12-31 18:59:33 +00001525 msg_tol = msg_link_tolerance(msg);
1526 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001527 link_set_supervision_props(l_ptr, msg_tol);
1528
1529 if (msg_linkprio(msg) > l_ptr->priority)
1530 l_ptr->priority = msg_linkprio(msg);
1531
1532 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001533 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 if (max_pkt_info < l_ptr->max_pkt_target)
1535 l_ptr->max_pkt_target = max_pkt_info;
1536 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1537 l_ptr->max_pkt = l_ptr->max_pkt_target;
1538 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001539 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541
Allan Stephens4d753132011-10-25 12:19:05 -04001542 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001543 if (!tipc_node_is_up(l_ptr->owner)) {
1544 l_ptr->owner->bclink.last_sent =
1545 l_ptr->owner->bclink.last_in =
1546 msg_last_bcast(msg);
1547 l_ptr->owner->bclink.oos_state = 0;
1548 }
Allan Stephens4d753132011-10-25 12:19:05 -04001549
Per Lidenb97bf3f2006-01-02 19:04:38 +01001550 l_ptr->peer_session = msg_session(msg);
1551 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001552
1553 if (msg_type(msg) == ACTIVATE_MSG)
1554 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001555 break;
1556 case STATE_MSG:
1557
Allan Stephens2db99832010-12-31 18:59:33 +00001558 msg_tol = msg_link_tolerance(msg);
1559 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001560 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001561
1562 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001564 pr_warn("%s<%s>, priority change %u->%u\n",
1565 link_rst_msg, l_ptr->name, l_ptr->priority,
1566 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001568 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569 break;
1570 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001571
1572 /* Record reception; force mismatch at next timeout: */
1573 l_ptr->checkpoint--;
1574
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1576 l_ptr->stats.recv_states++;
1577 if (link_reset_unknown(l_ptr))
1578 break;
1579
1580 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001581 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582 mod(l_ptr->next_in_no));
1583 }
1584
1585 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001586 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001587 l_ptr->max_pkt = max_pkt_ack;
1588 l_ptr->max_pkt_probes = 0;
1589 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590
1591 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001592 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001594 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001595 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001596 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597
1598 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001599 if (l_ptr->owner->bclink.recv_permitted)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001600 tipc_bclink_update_link_state(net, l_ptr->owner,
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001601 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602
1603 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001604 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1605 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 }
1607 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001608 l_ptr->stats.recv_nacks++;
Ying Xue58dc55f2014-11-26 11:41:52 +08001609 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue),
Per Liden4323add2006-01-18 00:38:21 +01001610 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001611 }
1612 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 }
1614exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001615 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616}
1617
1618
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001619/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1620 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001622static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1623 struct tipc_msg *tunnel_hdr,
1624 struct tipc_msg *msg,
1625 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001626{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001627 struct tipc_link *tunnel;
Ying Xuea6ca1092014-11-26 11:41:55 +08001628 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001629 u32 length = msg_size(msg);
1630
1631 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001632 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001633 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001635 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001637 skb = tipc_buf_acquire(length + INT_H_SIZE);
1638 if (!skb) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001639 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001641 }
Ying Xuea6ca1092014-11-26 11:41:55 +08001642 skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
1643 skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
1644 __tipc_link_xmit_skb(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645}
1646
1647
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001648/* tipc_link_failover_send_queue(): A link has gone down, but a second
1649 * link is still active. We can do failover. Tunnel the failing link's
1650 * whole send queue via the remaining link. This way, we don't lose
1651 * any packets, and sequence order is preserved for subsequent traffic
1652 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001654void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655{
Ying Xue58dc55f2014-11-26 11:41:52 +08001656 u32 msgcount = skb_queue_len(&l_ptr->outqueue);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001657 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 struct tipc_msg tunnel_hdr;
Ying Xue58dc55f2014-11-26 11:41:52 +08001659 struct sk_buff *skb;
Allan Stephens5392d642006-06-25 23:52:50 -07001660 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661
1662 if (!tunnel)
1663 return;
1664
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001665 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001666 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1668 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001669
Ying Xue58dc55f2014-11-26 11:41:52 +08001670 if (skb_queue_empty(&l_ptr->outqueue)) {
1671 skb = tipc_buf_acquire(INT_H_SIZE);
1672 if (skb) {
1673 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001675 __tipc_link_xmit_skb(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001677 pr_warn("%sunable to send changeover msg\n",
1678 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 }
1680 return;
1681 }
Allan Stephensf1310722006-06-25 23:51:37 -07001682
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001683 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001684 l_ptr->owner->active_links[1]);
1685
Ying Xue58dc55f2014-11-26 11:41:52 +08001686 skb_queue_walk(&l_ptr->outqueue, skb) {
1687 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688
1689 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001691 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692
Florian Westphald788d802007-08-02 19:28:06 -07001693 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001695 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001696 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1697 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698 pos += align(msg_size(m));
1699 m = (struct tipc_msg *)pos;
1700 }
1701 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001702 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1703 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 }
1706}
1707
Ying Xue247f0f32014-02-18 16:06:46 +08001708/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001709 * duplicate of the first link's send queue via the new link. This way, we
1710 * are guaranteed that currently queued packets from a socket are delivered
1711 * before future traffic from the same socket, even if this is using the
1712 * new link. The last arriving copy of each duplicate packet is dropped at
1713 * the receiving end by the regular protocol check, so packet cardinality
1714 * and sequence order is preserved per sender/receiver socket pair.
1715 * Owner node is locked.
1716 */
Ying Xue247f0f32014-02-18 16:06:46 +08001717void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001718 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719{
Ying Xue58dc55f2014-11-26 11:41:52 +08001720 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 struct tipc_msg tunnel_hdr;
1722
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001723 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001724 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Ying Xue58dc55f2014-11-26 11:41:52 +08001725 msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
Ying Xue58dc55f2014-11-26 11:41:52 +08001727 skb_queue_walk(&l_ptr->outqueue, skb) {
1728 struct sk_buff *outskb;
1729 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 u32 length = msg_size(msg);
1731
1732 if (msg_user(msg) == MSG_BUNDLER)
1733 msg_set_type(msg, CLOSED_MSG);
1734 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001735 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001737 outskb = tipc_buf_acquire(length + INT_H_SIZE);
1738 if (outskb == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001739 pr_warn("%sunable to send duplicate msg\n",
1740 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 return;
1742 }
Ying Xue58dc55f2014-11-26 11:41:52 +08001743 skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE);
1744 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data,
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001745 length);
Ying Xuea6ca1092014-11-26 11:41:55 +08001746 __tipc_link_xmit_skb(tunnel, outskb);
Per Liden4323add2006-01-18 00:38:21 +01001747 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 }
1750}
1751
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752/**
1753 * buf_extract - extracts embedded TIPC message from another message
1754 * @skb: encapsulating message buffer
1755 * @from_pos: offset to extract from
1756 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001757 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 * encapsulating message itself is left unchanged.
1759 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1761{
1762 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1763 u32 size = msg_size(msg);
1764 struct sk_buff *eb;
1765
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001766 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001768 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001769 return eb;
1770}
1771
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001772
1773
1774/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1775 * Owner node is locked.
1776 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001777static void tipc_link_dup_rcv(struct net *net, struct tipc_link *l_ptr,
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001778 struct sk_buff *t_buf)
1779{
1780 struct sk_buff *buf;
1781
1782 if (!tipc_link_is_up(l_ptr))
1783 return;
1784
1785 buf = buf_extract(t_buf, INT_H_SIZE);
1786 if (buf == NULL) {
1787 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1788 return;
1789 }
1790
1791 /* Add buffer to deferred queue, if applicable: */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001792 link_handle_out_of_seq_msg(net, l_ptr, buf);
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001793}
1794
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001795/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1796 * Owner node is locked.
1797 */
1798static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1799 struct sk_buff *t_buf)
1800{
1801 struct tipc_msg *t_msg = buf_msg(t_buf);
1802 struct sk_buff *buf = NULL;
1803 struct tipc_msg *msg;
1804
1805 if (tipc_link_is_up(l_ptr))
1806 tipc_link_reset(l_ptr);
1807
1808 /* First failover packet? */
1809 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1810 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1811
1812 /* Should there be an inner packet? */
1813 if (l_ptr->exp_msg_count) {
1814 l_ptr->exp_msg_count--;
1815 buf = buf_extract(t_buf, INT_H_SIZE);
1816 if (buf == NULL) {
1817 pr_warn("%sno inner failover pkt\n", link_co_err);
1818 goto exit;
1819 }
1820 msg = buf_msg(buf);
1821
1822 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1823 kfree_skb(buf);
1824 buf = NULL;
1825 goto exit;
1826 }
1827 if (msg_user(msg) == MSG_FRAGMENTER) {
1828 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001829 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001830 }
1831 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001832exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001833 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1834 tipc_node_detach_link(l_ptr->owner, l_ptr);
1835 kfree(l_ptr);
1836 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001837 return buf;
1838}
1839
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001840/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001841 * via other link as result of a failover (ORIGINAL_MSG) or
1842 * a new active link (DUPLICATE_MSG). Failover packets are
1843 * returned to the active link for delivery upwards.
1844 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001846static int tipc_link_tunnel_rcv(struct net *net, struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001847 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001849 struct sk_buff *t_buf = *buf;
1850 struct tipc_link *l_ptr;
1851 struct tipc_msg *t_msg = buf_msg(t_buf);
1852 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001854 *buf = NULL;
1855
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001856 if (bearer_id >= MAX_BEARERS)
1857 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001858
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001859 l_ptr = n_ptr->links[bearer_id];
1860 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001863 if (msg_type(t_msg) == DUPLICATE_MSG)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001864 tipc_link_dup_rcv(net, l_ptr, t_buf);
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001865 else if (msg_type(t_msg) == ORIGINAL_MSG)
1866 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001867 else
1868 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001870 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001871 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872}
1873
1874/*
1875 * Bundler functionality:
1876 */
Ying Xuef2f98002015-01-09 15:27:05 +08001877void tipc_link_bundle_rcv(struct net *net, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878{
1879 u32 msgcount = msg_msgcnt(buf_msg(buf));
1880 u32 pos = INT_H_SIZE;
1881 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001882 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 while (msgcount--) {
1885 obuf = buf_extract(buf, pos);
1886 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001887 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001888 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001889 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001890 omsg = buf_msg(obuf);
1891 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001892 if (msg_isdata(omsg)) {
1893 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
Ying Xuef2f98002015-01-09 15:27:05 +08001894 tipc_sk_mcast_rcv(net, obuf);
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001895 else
Ying Xuef2f98002015-01-09 15:27:05 +08001896 tipc_sk_rcv(net, obuf);
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001897 } else if (msg_user(omsg) == CONN_MANAGER) {
Ying Xuef2f98002015-01-09 15:27:05 +08001898 tipc_sk_rcv(net, obuf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001899 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
Ying Xuef2f98002015-01-09 15:27:05 +08001900 tipc_named_rcv(net, obuf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001901 } else {
1902 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1903 kfree_skb(obuf);
1904 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001906 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907}
1908
Ying Xue2f55c432015-01-09 15:27:00 +08001909static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910{
Ying Xue2f55c432015-01-09 15:27:00 +08001911 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
1912
1913 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
Allan Stephens5413b4c2011-01-18 13:24:55 -05001914 return;
1915
Ying Xue2f55c432015-01-09 15:27:00 +08001916 l_ptr->tolerance = tol;
1917 l_ptr->cont_intv = msecs_to_jiffies(intv);
1918 l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919}
1920
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001921void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922{
1923 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001924 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1925 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1926 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1927 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001929 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1930 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1931 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1932 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1935 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1936 /* FRAGMENT and LAST_FRAGMENT packets */
1937 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1938}
1939
Jon Paul Maloye099e862014-02-13 17:29:18 -05001940/* tipc_link_find_owner - locate owner node of link by link's name
Ying Xuef2f98002015-01-09 15:27:05 +08001941 * @net: the applicable net namespace
Jon Paul Maloye099e862014-02-13 17:29:18 -05001942 * @name: pointer to link name string
1943 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001944 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001945 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 */
Ying Xuef2f98002015-01-09 15:27:05 +08001947static struct tipc_node *tipc_link_find_owner(struct net *net,
1948 const char *link_name,
Jon Paul Maloye099e862014-02-13 17:29:18 -05001949 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001950{
Ying Xuef2f98002015-01-09 15:27:05 +08001951 struct tipc_net *tn = net_generic(net, tipc_net_id);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001952 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001953 struct tipc_node *n_ptr;
Fabian Frederick886eaa12014-12-25 12:05:50 +01001954 struct tipc_node *found_node = NULL;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001955 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001956
Jon Paul Maloye099e862014-02-13 17:29:18 -05001957 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001958 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +08001959 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001960 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001961 for (i = 0; i < MAX_BEARERS; i++) {
1962 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001963 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1964 *bearer_id = i;
1965 found_node = n_ptr;
1966 break;
1967 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001968 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001969 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001970 if (found_node)
1971 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001972 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001973 rcu_read_unlock();
1974
Jon Paul Maloye099e862014-02-13 17:29:18 -05001975 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976}
1977
Allan Stephens5c216e12011-10-18 11:34:29 -04001978/**
1979 * link_value_is_valid -- validate proposed link tolerance/priority/window
1980 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001981 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1982 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04001983 *
1984 * Returns 1 if value is within range, 0 if not.
1985 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001986static int link_value_is_valid(u16 cmd, u32 new_value)
1987{
1988 switch (cmd) {
1989 case TIPC_CMD_SET_LINK_TOL:
1990 return (new_value >= TIPC_MIN_LINK_TOL) &&
1991 (new_value <= TIPC_MAX_LINK_TOL);
1992 case TIPC_CMD_SET_LINK_PRI:
1993 return (new_value <= TIPC_MAX_LINK_PRI);
1994 case TIPC_CMD_SET_LINK_WINDOW:
1995 return (new_value >= TIPC_MIN_LINK_WIN) &&
1996 (new_value <= TIPC_MAX_LINK_WIN);
1997 }
1998 return 0;
1999}
2000
Allan Stephens5c216e12011-10-18 11:34:29 -04002001/**
2002 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ying Xuef2f98002015-01-09 15:27:05 +08002003 * @net: the applicable net namespace
Ben Hutchings2c530402012-07-10 10:55:09 +00002004 * @name: ptr to link, bearer, or media name
2005 * @new_value: new value of link, bearer, or media setting
2006 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002007 *
Ying Xue7216cd92014-04-21 10:55:48 +08002008 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04002009 *
2010 * Returns 0 if value updated and negative value on error.
2011 */
Ying Xuef2f98002015-01-09 15:27:05 +08002012static int link_cmd_set_value(struct net *net, const char *name, u32 new_value,
2013 u16 cmd)
Allan Stephens5c216e12011-10-18 11:34:29 -04002014{
2015 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002016 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002017 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002018 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002019 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002020 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002021
Ying Xuef2f98002015-01-09 15:27:05 +08002022 node = tipc_link_find_owner(net, name, &bearer_id);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002023 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002024 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002025 l_ptr = node->links[bearer_id];
2026
2027 if (l_ptr) {
2028 switch (cmd) {
2029 case TIPC_CMD_SET_LINK_TOL:
2030 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002031 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2032 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002033 break;
2034 case TIPC_CMD_SET_LINK_PRI:
2035 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002036 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2037 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002038 break;
2039 case TIPC_CMD_SET_LINK_WINDOW:
2040 tipc_link_set_queue_limits(l_ptr, new_value);
2041 break;
2042 default:
2043 res = -EINVAL;
2044 break;
2045 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002046 }
2047 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002048 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002049 }
2050
Ying Xue7f9f95d2015-01-09 15:27:06 +08002051 b_ptr = tipc_bearer_find(net, name);
Allan Stephens5c216e12011-10-18 11:34:29 -04002052 if (b_ptr) {
2053 switch (cmd) {
2054 case TIPC_CMD_SET_LINK_TOL:
2055 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002056 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002057 case TIPC_CMD_SET_LINK_PRI:
2058 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002059 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002060 case TIPC_CMD_SET_LINK_WINDOW:
2061 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002062 break;
2063 default:
2064 res = -EINVAL;
2065 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002066 }
Ying Xue636c0372013-10-18 07:23:20 +02002067 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002068 }
2069
2070 m_ptr = tipc_media_find(name);
2071 if (!m_ptr)
2072 return -ENODEV;
2073 switch (cmd) {
2074 case TIPC_CMD_SET_LINK_TOL:
2075 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002076 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002077 case TIPC_CMD_SET_LINK_PRI:
2078 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002079 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002080 case TIPC_CMD_SET_LINK_WINDOW:
2081 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002082 break;
2083 default:
2084 res = -EINVAL;
2085 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002086 }
Ying Xue636c0372013-10-18 07:23:20 +02002087 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002088}
2089
Ying Xuef2f98002015-01-09 15:27:05 +08002090struct sk_buff *tipc_link_cmd_config(struct net *net, const void *req_tlv_area,
2091 int req_tlv_space, u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092{
2093 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002094 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002095 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096
2097 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002098 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099
2100 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2101 new_value = ntohl(args->value);
2102
Allan Stephens5c216e12011-10-18 11:34:29 -04002103 if (!link_value_is_valid(cmd, new_value))
2104 return tipc_cfg_reply_error_string(
2105 "cannot change, value invalid");
2106
Per Liden4323add2006-01-18 00:38:21 +01002107 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Ying Xue1da46562015-01-09 15:27:07 +08002109 (tipc_bclink_set_queue_limits(net, new_value) == 0))
Per Liden4323add2006-01-18 00:38:21 +01002110 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002111 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002112 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113 }
2114
Ying Xuef2f98002015-01-09 15:27:05 +08002115 res = link_cmd_set_value(net, args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002116 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002117 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002118
Per Liden4323add2006-01-18 00:38:21 +01002119 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002120}
2121
2122/**
2123 * link_reset_statistics - reset link statistics
2124 * @l_ptr: pointer to link
2125 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002126static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127{
2128 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2129 l_ptr->stats.sent_info = l_ptr->next_out_no;
2130 l_ptr->stats.recv_info = l_ptr->next_in_no;
2131}
2132
Ying Xuef2f98002015-01-09 15:27:05 +08002133struct sk_buff *tipc_link_cmd_reset_stats(struct net *net,
2134 const void *req_tlv_area,
2135 int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002136{
2137 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002138 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002139 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002140 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002141
2142 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002143 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002144
2145 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002146 if (!strcmp(link_name, tipc_bclink_name)) {
Ying Xue1da46562015-01-09 15:27:07 +08002147 if (tipc_bclink_reset_stats(net))
Per Liden4323add2006-01-18 00:38:21 +01002148 return tipc_cfg_reply_error_string("link not found");
2149 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 }
Ying Xuef2f98002015-01-09 15:27:05 +08002151 node = tipc_link_find_owner(net, link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002152 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002153 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002154
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002155 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002156 l_ptr = node->links[bearer_id];
2157 if (!l_ptr) {
2158 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002159 return tipc_cfg_reply_error_string("link not found");
2160 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002162 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002163 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002164}
2165
2166/**
2167 * percent - convert count to a percentage of total (rounding up or down)
2168 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002169static u32 percent(u32 count, u32 total)
2170{
2171 return (count * 100 + (total / 2)) / total;
2172}
2173
2174/**
Per Liden4323add2006-01-18 00:38:21 +01002175 * tipc_link_stats - print link statistics
Ying Xuef2f98002015-01-09 15:27:05 +08002176 * @net: the applicable net namespace
Per Lidenb97bf3f2006-01-02 19:04:38 +01002177 * @name: link name
2178 * @buf: print buffer area
2179 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002180 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002181 * Returns length of print buffer data string (or 0 if error)
2182 */
Ying Xuef2f98002015-01-09 15:27:05 +08002183static int tipc_link_stats(struct net *net, const char *name, char *buf,
2184 const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002185{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002186 struct tipc_link *l;
2187 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002188 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002189 char *status;
2190 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002191 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002192 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002193
Per Liden4323add2006-01-18 00:38:21 +01002194 if (!strcmp(name, tipc_bclink_name))
Ying Xue1da46562015-01-09 15:27:07 +08002195 return tipc_bclink_stats(net, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196
Ying Xuef2f98002015-01-09 15:27:05 +08002197 node = tipc_link_find_owner(net, name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002198 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002199 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002200
Per Liden4323add2006-01-18 00:38:21 +01002201 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002202
2203 l = node->links[bearer_id];
2204 if (!l) {
2205 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002206 return 0;
2207 }
2208
Erik Hugnedc1aed32012-06-29 00:50:23 -04002209 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210
Erik Hugnedc1aed32012-06-29 00:50:23 -04002211 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002212 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002213 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214 status = "STANDBY";
2215 else
2216 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002217
2218 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2219 " %s MTU:%u Priority:%u Tolerance:%u ms"
2220 " Window:%u packets\n",
2221 l->name, status, l->max_pkt, l->priority,
2222 l->tolerance, l->queue_limit[0]);
2223
2224 ret += tipc_snprintf(buf + ret, buf_size - ret,
2225 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2226 l->next_in_no - s->recv_info, s->recv_fragments,
2227 s->recv_fragmented, s->recv_bundles,
2228 s->recv_bundled);
2229
2230 ret += tipc_snprintf(buf + ret, buf_size - ret,
2231 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2232 l->next_out_no - s->sent_info, s->sent_fragments,
2233 s->sent_fragmented, s->sent_bundles,
2234 s->sent_bundled);
2235
2236 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002237 if (!profile_total)
2238 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002239
2240 ret += tipc_snprintf(buf + ret, buf_size - ret,
2241 " TX profile sample:%u packets average:%u octets\n"
2242 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2243 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2244 s->msg_length_counts,
2245 s->msg_lengths_total / profile_total,
2246 percent(s->msg_length_profile[0], profile_total),
2247 percent(s->msg_length_profile[1], profile_total),
2248 percent(s->msg_length_profile[2], profile_total),
2249 percent(s->msg_length_profile[3], profile_total),
2250 percent(s->msg_length_profile[4], profile_total),
2251 percent(s->msg_length_profile[5], profile_total),
2252 percent(s->msg_length_profile[6], profile_total));
2253
2254 ret += tipc_snprintf(buf + ret, buf_size - ret,
2255 " RX states:%u probes:%u naks:%u defs:%u"
2256 " dups:%u\n", s->recv_states, s->recv_probes,
2257 s->recv_nacks, s->deferred_recv, s->duplicates);
2258
2259 ret += tipc_snprintf(buf + ret, buf_size - ret,
2260 " TX states:%u probes:%u naks:%u acks:%u"
2261 " dups:%u\n", s->sent_states, s->sent_probes,
2262 s->sent_nacks, s->sent_acks, s->retransmitted);
2263
2264 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002265 " Congestion link:%u Send queue"
2266 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002267 s->max_queue_sz, s->queue_sz_counts ?
2268 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002269
Per Liden4323add2006-01-18 00:38:21 +01002270 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002271 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002272}
2273
Ying Xuef2f98002015-01-09 15:27:05 +08002274struct sk_buff *tipc_link_cmd_show_stats(struct net *net,
2275 const void *req_tlv_area,
2276 int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002277{
2278 struct sk_buff *buf;
2279 struct tlv_desc *rep_tlv;
2280 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002281 int pb_len;
2282 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002283
2284 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002285 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286
Erik Hugnedc1aed32012-06-29 00:50:23 -04002287 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002288 if (!buf)
2289 return NULL;
2290
2291 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002292 pb = TLV_DATA(rep_tlv);
2293 pb_len = ULTRA_STRING_MAX_LEN;
Ying Xuef2f98002015-01-09 15:27:05 +08002294 str_len = tipc_link_stats(net, (char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002295 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002296 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002297 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002298 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002299 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002300 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002301 skb_put(buf, TLV_SPACE(str_len));
2302 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2303
2304 return buf;
2305}
2306
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002307static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002308{
Ying Xue7f9f95d2015-01-09 15:27:06 +08002309 struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
Ying Xue7a2f7d12014-04-21 10:55:46 +08002310 struct tipc_bearer *b_ptr;
2311
2312 rcu_read_lock();
Ying Xue7f9f95d2015-01-09 15:27:06 +08002313 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
Ying Xue7a2f7d12014-04-21 10:55:46 +08002314 if (b_ptr)
2315 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2316 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002317
Per Lidenb97bf3f2006-01-02 19:04:38 +01002318 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002319 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002320 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002321 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002322 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002323 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002324 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002325 pr_cont(":WW\n");
2326 else
2327 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002328}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002329
2330/* Parse and validate nested (link) properties valid for media, bearer and link
2331 */
2332int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2333{
2334 int err;
2335
2336 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2337 tipc_nl_prop_policy);
2338 if (err)
2339 return err;
2340
2341 if (props[TIPC_NLA_PROP_PRIO]) {
2342 u32 prio;
2343
2344 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2345 if (prio > TIPC_MAX_LINK_PRI)
2346 return -EINVAL;
2347 }
2348
2349 if (props[TIPC_NLA_PROP_TOL]) {
2350 u32 tol;
2351
2352 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2353 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2354 return -EINVAL;
2355 }
2356
2357 if (props[TIPC_NLA_PROP_WIN]) {
2358 u32 win;
2359
2360 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2361 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2362 return -EINVAL;
2363 }
2364
2365 return 0;
2366}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002367
Richard Alpef96ce7a2014-11-20 10:29:13 +01002368int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2369{
2370 int err;
2371 int res = 0;
2372 int bearer_id;
2373 char *name;
2374 struct tipc_link *link;
2375 struct tipc_node *node;
2376 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Ying Xuef2f98002015-01-09 15:27:05 +08002377 struct net *net = genl_info_net(info);
Richard Alpef96ce7a2014-11-20 10:29:13 +01002378
2379 if (!info->attrs[TIPC_NLA_LINK])
2380 return -EINVAL;
2381
2382 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2383 info->attrs[TIPC_NLA_LINK],
2384 tipc_nl_link_policy);
2385 if (err)
2386 return err;
2387
2388 if (!attrs[TIPC_NLA_LINK_NAME])
2389 return -EINVAL;
2390
2391 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2392
Ying Xuef2f98002015-01-09 15:27:05 +08002393 node = tipc_link_find_owner(net, name, &bearer_id);
Richard Alpef96ce7a2014-11-20 10:29:13 +01002394 if (!node)
2395 return -EINVAL;
2396
2397 tipc_node_lock(node);
2398
2399 link = node->links[bearer_id];
2400 if (!link) {
2401 res = -EINVAL;
2402 goto out;
2403 }
2404
2405 if (attrs[TIPC_NLA_LINK_PROP]) {
2406 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2407
2408 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2409 props);
2410 if (err) {
2411 res = err;
2412 goto out;
2413 }
2414
2415 if (props[TIPC_NLA_PROP_TOL]) {
2416 u32 tol;
2417
2418 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2419 link_set_supervision_props(link, tol);
2420 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2421 }
2422 if (props[TIPC_NLA_PROP_PRIO]) {
2423 u32 prio;
2424
2425 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2426 link->priority = prio;
2427 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2428 }
2429 if (props[TIPC_NLA_PROP_WIN]) {
2430 u32 win;
2431
2432 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2433 tipc_link_set_queue_limits(link, win);
2434 }
2435 }
2436
2437out:
2438 tipc_node_unlock(node);
2439
2440 return res;
2441}
Richard Alped8182802014-11-24 11:10:29 +01002442
2443static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002444{
2445 int i;
2446 struct nlattr *stats;
2447
2448 struct nla_map {
2449 u32 key;
2450 u32 val;
2451 };
2452
2453 struct nla_map map[] = {
2454 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2455 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2456 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2457 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2458 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2459 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2460 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2461 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2462 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2463 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2464 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2465 s->msg_length_counts : 1},
2466 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2467 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2468 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2469 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2470 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2471 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2472 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2473 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2474 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2475 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2476 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2477 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2478 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2479 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2480 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2481 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2482 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2483 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2484 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2485 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2486 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2487 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2488 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2489 };
2490
2491 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2492 if (!stats)
2493 return -EMSGSIZE;
2494
2495 for (i = 0; i < ARRAY_SIZE(map); i++)
2496 if (nla_put_u32(skb, map[i].key, map[i].val))
2497 goto msg_full;
2498
2499 nla_nest_end(skb, stats);
2500
2501 return 0;
2502msg_full:
2503 nla_nest_cancel(skb, stats);
2504
2505 return -EMSGSIZE;
2506}
2507
2508/* Caller should hold appropriate locks to protect the link */
Richard Alped8182802014-11-24 11:10:29 +01002509static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002510{
2511 int err;
2512 void *hdr;
2513 struct nlattr *attrs;
2514 struct nlattr *prop;
2515
2516 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2517 NLM_F_MULTI, TIPC_NL_LINK_GET);
2518 if (!hdr)
2519 return -EMSGSIZE;
2520
2521 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2522 if (!attrs)
2523 goto msg_full;
2524
2525 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2526 goto attr_msg_full;
2527 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2528 tipc_cluster_mask(tipc_own_addr)))
2529 goto attr_msg_full;
2530 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2531 goto attr_msg_full;
2532 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2533 goto attr_msg_full;
2534 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2535 goto attr_msg_full;
2536
2537 if (tipc_link_is_up(link))
2538 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2539 goto attr_msg_full;
2540 if (tipc_link_is_active(link))
2541 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2542 goto attr_msg_full;
2543
2544 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2545 if (!prop)
2546 goto attr_msg_full;
2547 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2548 goto prop_msg_full;
2549 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2550 goto prop_msg_full;
2551 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2552 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2553 goto prop_msg_full;
2554 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2555 goto prop_msg_full;
2556 nla_nest_end(msg->skb, prop);
2557
2558 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2559 if (err)
2560 goto attr_msg_full;
2561
2562 nla_nest_end(msg->skb, attrs);
2563 genlmsg_end(msg->skb, hdr);
2564
2565 return 0;
2566
2567prop_msg_full:
2568 nla_nest_cancel(msg->skb, prop);
2569attr_msg_full:
2570 nla_nest_cancel(msg->skb, attrs);
2571msg_full:
2572 genlmsg_cancel(msg->skb, hdr);
2573
2574 return -EMSGSIZE;
2575}
2576
2577/* Caller should hold node lock */
Richard Alped8182802014-11-24 11:10:29 +01002578static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
2579 struct tipc_node *node,
2580 u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002581{
2582 u32 i;
2583 int err;
2584
2585 for (i = *prev_link; i < MAX_BEARERS; i++) {
2586 *prev_link = i;
2587
2588 if (!node->links[i])
2589 continue;
2590
2591 err = __tipc_nl_add_link(msg, node->links[i]);
2592 if (err)
2593 return err;
2594 }
2595 *prev_link = 0;
2596
2597 return 0;
2598}
2599
2600int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2601{
Ying Xuef2f98002015-01-09 15:27:05 +08002602 struct net *net = sock_net(skb->sk);
2603 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002604 struct tipc_node *node;
2605 struct tipc_nl_msg msg;
2606 u32 prev_node = cb->args[0];
2607 u32 prev_link = cb->args[1];
2608 int done = cb->args[2];
2609 int err;
2610
2611 if (done)
2612 return 0;
2613
2614 msg.skb = skb;
2615 msg.portid = NETLINK_CB(cb->skb).portid;
2616 msg.seq = cb->nlh->nlmsg_seq;
2617
2618 rcu_read_lock();
2619
2620 if (prev_node) {
Ying Xuef2f98002015-01-09 15:27:05 +08002621 node = tipc_node_find(net, prev_node);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002622 if (!node) {
2623 /* We never set seq or call nl_dump_check_consistent()
2624 * this means that setting prev_seq here will cause the
2625 * consistence check to fail in the netlink callback
2626 * handler. Resulting in the last NLMSG_DONE message
2627 * having the NLM_F_DUMP_INTR flag set.
2628 */
2629 cb->prev_seq = 1;
2630 goto out;
2631 }
2632
Ying Xuef2f98002015-01-09 15:27:05 +08002633 list_for_each_entry_continue_rcu(node, &tn->node_list,
2634 list) {
Richard Alpe7be57fc2014-11-20 10:29:12 +01002635 tipc_node_lock(node);
2636 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2637 tipc_node_unlock(node);
2638 if (err)
2639 goto out;
2640
2641 prev_node = node->addr;
2642 }
2643 } else {
Ying Xue1da46562015-01-09 15:27:07 +08002644 err = tipc_nl_add_bc_link(net, &msg);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002645 if (err)
2646 goto out;
2647
Ying Xuef2f98002015-01-09 15:27:05 +08002648 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe7be57fc2014-11-20 10:29:12 +01002649 tipc_node_lock(node);
2650 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2651 tipc_node_unlock(node);
2652 if (err)
2653 goto out;
2654
2655 prev_node = node->addr;
2656 }
2657 }
2658 done = 1;
2659out:
2660 rcu_read_unlock();
2661
2662 cb->args[0] = prev_node;
2663 cb->args[1] = prev_link;
2664 cb->args[2] = done;
2665
2666 return skb->len;
2667}
2668
2669int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2670{
Ying Xuef2f98002015-01-09 15:27:05 +08002671 struct net *net = genl_info_net(info);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002672 struct sk_buff *ans_skb;
2673 struct tipc_nl_msg msg;
2674 struct tipc_link *link;
2675 struct tipc_node *node;
2676 char *name;
2677 int bearer_id;
2678 int err;
2679
2680 if (!info->attrs[TIPC_NLA_LINK_NAME])
2681 return -EINVAL;
2682
2683 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
Ying Xuef2f98002015-01-09 15:27:05 +08002684 node = tipc_link_find_owner(net, name, &bearer_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002685 if (!node)
2686 return -EINVAL;
2687
2688 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2689 if (!ans_skb)
2690 return -ENOMEM;
2691
2692 msg.skb = ans_skb;
2693 msg.portid = info->snd_portid;
2694 msg.seq = info->snd_seq;
2695
2696 tipc_node_lock(node);
2697 link = node->links[bearer_id];
2698 if (!link) {
2699 err = -EINVAL;
2700 goto err_out;
2701 }
2702
2703 err = __tipc_nl_add_link(&msg, link);
2704 if (err)
2705 goto err_out;
2706
2707 tipc_node_unlock(node);
2708
2709 return genlmsg_reply(ans_skb, info);
2710
2711err_out:
2712 tipc_node_unlock(node);
2713 nlmsg_free(ans_skb);
2714
2715 return err;
2716}
Richard Alpeae363422014-11-20 10:29:14 +01002717
2718int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2719{
2720 int err;
2721 char *link_name;
2722 unsigned int bearer_id;
2723 struct tipc_link *link;
2724 struct tipc_node *node;
2725 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Ying Xuef2f98002015-01-09 15:27:05 +08002726 struct net *net = genl_info_net(info);
Richard Alpeae363422014-11-20 10:29:14 +01002727
2728 if (!info->attrs[TIPC_NLA_LINK])
2729 return -EINVAL;
2730
2731 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2732 info->attrs[TIPC_NLA_LINK],
2733 tipc_nl_link_policy);
2734 if (err)
2735 return err;
2736
2737 if (!attrs[TIPC_NLA_LINK_NAME])
2738 return -EINVAL;
2739
2740 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2741
2742 if (strcmp(link_name, tipc_bclink_name) == 0) {
Ying Xue1da46562015-01-09 15:27:07 +08002743 err = tipc_bclink_reset_stats(net);
Richard Alpeae363422014-11-20 10:29:14 +01002744 if (err)
2745 return err;
2746 return 0;
2747 }
2748
Ying Xuef2f98002015-01-09 15:27:05 +08002749 node = tipc_link_find_owner(net, link_name, &bearer_id);
Richard Alpeae363422014-11-20 10:29:14 +01002750 if (!node)
2751 return -EINVAL;
2752
2753 tipc_node_lock(node);
2754
2755 link = node->links[bearer_id];
2756 if (!link) {
2757 tipc_node_unlock(node);
2758 return -EINVAL;
2759 }
2760
2761 link_reset_statistics(link);
2762
2763 tipc_node_unlock(node);
2764
2765 return 0;
2766}