blob: 450ed0ce9071bbb0eb19885e6555251f06466fd8 [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
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500104static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 struct sk_buff *buf);
Ying Xue247f0f32014-02-18 16:06:46 +0800106static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Paul Maloy3bb53382014-02-13 17:29:12 -0500107static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500108 struct sk_buff **buf);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500109static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500110static void link_state_event(struct tipc_link *l_ptr, u32 event);
111static void link_reset_statistics(struct tipc_link *l_ptr);
112static void link_print(struct tipc_link *l_ptr, const char *str);
Ying Xue247f0f32014-02-18 16:06:46 +0800113static void tipc_link_sync_xmit(struct tipc_link *l);
114static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +0200115static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf);
116static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000117
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800119 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800121static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122{
123 return (i + 3) & ~3u;
124}
125
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500126static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800128 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900130
Ying Xue7a2f7d12014-04-21 10:55:46 +0800131 rcu_read_lock();
132 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
133 if (!b_ptr) {
134 rcu_read_unlock();
135 return;
136 }
137 max_pkt = (b_ptr->mtu & ~3);
138 rcu_read_unlock();
139
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 if (max_pkt > MAX_MSG_SIZE)
141 max_pkt = MAX_MSG_SIZE;
142
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900143 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
145 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900146 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 l_ptr->max_pkt = MAX_PKT_DEFAULT;
148
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900149 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150}
151
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500152static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153{
154 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400155 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 return mod(l_ptr->next_out_no);
157}
158
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500159static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160{
161 return mod(link_next_sent(l_ptr) - 1);
162}
163
164/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800165 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500167int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168{
169 if (!l_ptr)
170 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000171 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172}
173
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500174int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000176 return (l_ptr->owner->active_links[0] == l_ptr) ||
177 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178}
179
180/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 * link_timeout - handle expiration of link timer
182 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500184static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185{
Per Liden4323add2006-01-18 00:38:21 +0100186 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187
188 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
190 l_ptr->stats.queue_sz_counts++;
191
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 if (l_ptr->first_out) {
193 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
194 u32 length = msg_size(msg);
195
Joe Perchesf64f9e72009-11-29 16:55:45 -0800196 if ((msg_user(msg) == MSG_FRAGMENTER) &&
197 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 length = msg_size(msg_get_wrapped(msg));
199 }
200 if (length) {
201 l_ptr->stats.msg_lengths_total += length;
202 l_ptr->stats.msg_length_counts++;
203 if (length <= 64)
204 l_ptr->stats.msg_length_profile[0]++;
205 else if (length <= 256)
206 l_ptr->stats.msg_length_profile[1]++;
207 else if (length <= 1024)
208 l_ptr->stats.msg_length_profile[2]++;
209 else if (length <= 4096)
210 l_ptr->stats.msg_length_profile[3]++;
211 else if (length <= 16384)
212 l_ptr->stats.msg_length_profile[4]++;
213 else if (length <= 32768)
214 l_ptr->stats.msg_length_profile[5]++;
215 else
216 l_ptr->stats.msg_length_profile[6]++;
217 }
218 }
219
220 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221
222 link_state_event(l_ptr, TIMEOUT_EVT);
223
224 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100225 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226
Per Liden4323add2006-01-18 00:38:21 +0100227 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228}
229
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500230static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231{
232 k_start_timer(&l_ptr->timer, time);
233}
234
235/**
Per Liden4323add2006-01-18 00:38:21 +0100236 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500237 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900240 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 * Returns pointer to link.
242 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500243struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500244 struct tipc_bearer *b_ptr,
245 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500247 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 struct tipc_msg *msg;
249 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500250 char addr_string[16];
251 u32 peer = n_ptr->addr;
252
Holger Brunck0372bf52014-11-14 18:33:19 +0100253 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500254 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100255 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
256 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500257 return NULL;
258 }
259
260 if (n_ptr->links[b_ptr->identity]) {
261 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400262 pr_err("Attempt to establish second link on <%s> to %s\n",
263 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500264 return NULL;
265 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700267 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400269 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 return NULL;
271 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272
273 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500274 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400275 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900277 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 if_name,
279 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400280 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500282 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400284 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800285 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400286 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 l_ptr->state = RESET_UNKNOWN;
288
289 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
290 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000291 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700293 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 msg_set_bearer_id(msg, b_ptr->identity);
295 strcpy((char *)msg_data(msg), if_name);
296
297 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400298 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299
Ying Xue7a2f7d12014-04-21 10:55:46 +0800300 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 link_init_max_pkt(l_ptr);
302
303 l_ptr->next_out_no = 1;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400304 __skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305
306 link_reset_statistics(l_ptr);
307
Allan Stephens37b9c082011-02-28 11:32:27 -0500308 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500310 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
311 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500312
313 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 return l_ptr;
316}
317
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500318void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500319{
320 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();
324 list_for_each_entry_rcu(n_ptr, &tipc_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;
369 __skb_queue_tail(&link->waiting_sks, buf);
370 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{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400382 struct sk_buff_head *wq = &link->waiting_sks;
383 struct sk_buff *buf;
384 uint pend_qsz = link->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400386 for (buf = skb_peek(wq); buf; buf = skb_peek(wq)) {
387 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(buf)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400389 pend_qsz += TIPC_SKB_CB(buf)->chain_sz;
390 __skb_queue_tail(&link->owner->waiting_sks, __skb_dequeue(wq));
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 Lidenb97bf3f2006-01-02 19:04:38 +0100395 * link_release_outqueue - purge link's outbound message queue
396 * @l_ptr: pointer to link
397 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500398static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399{
Ying Xued77b3832013-12-10 20:45:38 -0800400 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 l_ptr->first_out = NULL;
402 l_ptr->out_queue_size = 0;
403}
404
405/**
Per Liden4323add2006-01-18 00:38:21 +0100406 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 * @l_ptr: pointer to link
408 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500409void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400411 kfree_skb(l_ptr->reasm_buf);
412 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413}
414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500416 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 * @l_ptr: pointer to link
418 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500419void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420{
Ying Xued77b3832013-12-10 20:45:38 -0800421 kfree_skb_list(l_ptr->oldest_deferred_in);
422 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100423 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424}
425
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500426void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 u32 prev_state = l_ptr->state;
429 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700430 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400431 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900432
Allan Stephensa686e682008-06-04 17:29:39 -0700433 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434
Allan Stephensa686e682008-06-04 17:29:39 -0700435 /* Link is down, accept any session */
436 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900438 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900440
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442
443 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
444 return;
445
Per Liden4323add2006-01-18 00:38:21 +0100446 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800447 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000448
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500449 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 l_ptr->reset_checkpoint = checkpoint;
451 l_ptr->exp_msg_count = START_CHANGEOVER;
452 }
453
454 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 link_release_outqueue(l_ptr);
Ying Xued77b3832013-12-10 20:45:38 -0800456 kfree_skb_list(l_ptr->oldest_deferred_in);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400457 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
458 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
459 owner->action_flags |= TIPC_WAKEUP_USERS;
460 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 l_ptr->retransm_queue_head = 0;
462 l_ptr->retransm_queue_size = 0;
463 l_ptr->last_out = NULL;
464 l_ptr->first_out = NULL;
465 l_ptr->next_out = NULL;
466 l_ptr->unacked_window = 0;
467 l_ptr->checkpoint = 1;
468 l_ptr->next_out_no = 1;
469 l_ptr->deferred_inqueue_sz = 0;
470 l_ptr->oldest_deferred_in = NULL;
471 l_ptr->newest_deferred_in = NULL;
472 l_ptr->fsm_msg_cnt = 0;
473 l_ptr->stale_count = 0;
474 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475}
476
Ying Xuec61dd612014-02-13 17:29:09 -0500477void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500478{
479 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500480 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500481
Ying Xue6c7a7622014-03-27 12:54:37 +0800482 rcu_read_lock();
483 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800484 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500485 l_ptr = n_ptr->links[bearer_id];
486 if (l_ptr)
487 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800488 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500489 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800490 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500491}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500493static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494{
Allan Stephens5392d642006-06-25 23:52:50 -0700495 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100496 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800497 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498}
499
500/**
501 * link_state_event - link finite state machine
502 * @l_ptr: pointer to link
503 * @event: state machine event to process
504 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000505static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500507 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 u32 cont_intv = l_ptr->continuity_interval;
509
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500510 if (l_ptr->flags & LINK_STOPPED)
511 return;
512
Ying Xue135daee2014-02-13 17:29:08 -0500513 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 return; /* Not yet. */
515
Ying Xue77a7e072013-12-10 20:45:44 -0800516 /* Check whether changeover is going on */
517 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000518 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800520 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522
523 switch (l_ptr->state) {
524 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 switch (event) {
526 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 break;
529 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 if (l_ptr->next_in_no != l_ptr->checkpoint) {
531 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100532 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800533 tipc_link_proto_xmit(l_ptr, STATE_MSG,
534 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 l_ptr->fsm_msg_cnt++;
536 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800537 tipc_link_proto_xmit(l_ptr, STATE_MSG,
538 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 l_ptr->fsm_msg_cnt++;
540 }
541 link_set_timer(l_ptr, cont_intv);
542 break;
543 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 l_ptr->state = WORKING_UNKNOWN;
545 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800546 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 l_ptr->fsm_msg_cnt++;
548 link_set_timer(l_ptr, cont_intv / 4);
549 break;
550 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400551 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
552 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100553 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 l_ptr->state = RESET_RESET;
555 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800556 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
557 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 l_ptr->fsm_msg_cnt++;
559 link_set_timer(l_ptr, cont_intv);
560 break;
561 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400562 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 }
564 break;
565 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 switch (event) {
567 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 l_ptr->state = WORKING_WORKING;
570 l_ptr->fsm_msg_cnt = 0;
571 link_set_timer(l_ptr, cont_intv);
572 break;
573 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400574 pr_info("%s<%s>, requested by peer while probing\n",
575 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100576 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 l_ptr->state = RESET_RESET;
578 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800579 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
580 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 l_ptr->fsm_msg_cnt++;
582 link_set_timer(l_ptr, cont_intv);
583 break;
584 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 l_ptr->state = WORKING_WORKING;
587 l_ptr->fsm_msg_cnt = 0;
588 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100589 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800590 tipc_link_proto_xmit(l_ptr, STATE_MSG,
591 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 l_ptr->fsm_msg_cnt++;
593 }
594 link_set_timer(l_ptr, cont_intv);
595 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800596 tipc_link_proto_xmit(l_ptr, STATE_MSG,
597 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 l_ptr->fsm_msg_cnt++;
599 link_set_timer(l_ptr, cont_intv / 4);
600 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400601 pr_warn("%s<%s>, peer not responding\n",
602 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100603 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 l_ptr->state = RESET_UNKNOWN;
605 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800606 tipc_link_proto_xmit(l_ptr, RESET_MSG,
607 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 l_ptr->fsm_msg_cnt++;
609 link_set_timer(l_ptr, cont_intv);
610 }
611 break;
612 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400613 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 }
615 break;
616 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 switch (event) {
618 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 break;
620 case ACTIVATE_MSG:
621 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000622 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 l_ptr->state = WORKING_WORKING;
625 l_ptr->fsm_msg_cnt = 0;
626 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800627 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800629 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800630 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 link_set_timer(l_ptr, cont_intv);
632 break;
633 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 l_ptr->state = RESET_RESET;
635 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800636 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
637 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 l_ptr->fsm_msg_cnt++;
639 link_set_timer(l_ptr, cont_intv);
640 break;
641 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500642 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 /* fall through */
644 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800645 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 l_ptr->fsm_msg_cnt++;
647 link_set_timer(l_ptr, cont_intv);
648 break;
649 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400650 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 }
652 break;
653 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 switch (event) {
655 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 case ACTIVATE_MSG:
657 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000658 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 l_ptr->state = WORKING_WORKING;
661 l_ptr->fsm_msg_cnt = 0;
662 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800663 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800665 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800666 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 link_set_timer(l_ptr, cont_intv);
668 break;
669 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 break;
671 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800672 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
673 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 l_ptr->fsm_msg_cnt++;
675 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 break;
677 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400678 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 }
680 break;
681 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400682 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 }
684}
685
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500686/* tipc_link_cong: determine return value and how to treat the
687 * sent buffer during link congestion.
688 * - For plain, errorless user data messages we keep the buffer and
689 * return -ELINKONG.
690 * - For all other messages we discard the buffer and return -EHOSTUNREACH
691 * - For TIPC internal messages we also reset the link
692 */
693static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
694{
695 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500696 uint imp = tipc_msg_tot_importance(msg);
697 u32 oport = msg_tot_origport(msg);
698
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400699 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500700 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
701 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400702 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500703 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400704 if (unlikely(msg_errcode(msg)))
705 goto drop;
706 if (unlikely(msg_reroute_cnt(msg)))
707 goto drop;
708 if (TIPC_SKB_CB(buf)->wakeup_pending)
709 return -ELINKCONG;
710 if (link_schedule_user(link, oport, TIPC_SKB_CB(buf)->chain_sz, imp))
711 return -ELINKCONG;
712drop:
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500713 kfree_skb_list(buf);
714 return -EHOSTUNREACH;
715}
716
717/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400718 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500719 * @link: link to use
720 * @buf: chain of buffers containing message
721 * Consumes the buffer chain, except when returning -ELINKCONG
722 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
723 * user data messages) or -EHOSTUNREACH (all other messages/senders)
724 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
725 * to act on the return value, since they may need to do more send attempts.
726 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400727int __tipc_link_xmit(struct tipc_link *link, struct sk_buff *buf)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500728{
729 struct tipc_msg *msg = buf_msg(buf);
730 uint psz = msg_size(msg);
731 uint qsz = link->out_queue_size;
732 uint sndlim = link->queue_limit[0];
733 uint imp = tipc_msg_tot_importance(msg);
734 uint mtu = link->max_pkt;
735 uint ack = mod(link->next_in_no - 1);
736 uint seqno = link->next_out_no;
737 uint bc_last_in = link->owner->bclink.last_in;
738 struct tipc_media_addr *addr = &link->media_addr;
739 struct sk_buff *next = buf->next;
740
741 /* Match queue limits against msg importance: */
742 if (unlikely(qsz >= link->queue_limit[imp]))
743 return tipc_link_cong(link, buf);
744
745 /* Has valid packet limit been used ? */
746 if (unlikely(psz > mtu)) {
747 kfree_skb_list(buf);
748 return -EMSGSIZE;
749 }
750
751 /* Prepare each packet for sending, and add to outqueue: */
752 while (buf) {
753 next = buf->next;
754 msg = buf_msg(buf);
755 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
756 msg_set_bcast_ack(msg, bc_last_in);
757
758 if (!link->first_out) {
759 link->first_out = buf;
760 } else if (qsz < sndlim) {
761 link->last_out->next = buf;
762 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
763 link->stats.sent_bundled++;
764 buf = next;
765 next = buf->next;
766 continue;
767 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
768 link->stats.sent_bundled++;
769 link->stats.sent_bundles++;
770 link->last_out->next = buf;
771 if (!link->next_out)
772 link->next_out = buf;
773 } else {
774 link->last_out->next = buf;
775 if (!link->next_out)
776 link->next_out = buf;
777 }
778
779 /* Send packet if possible: */
780 if (likely(++qsz <= sndlim)) {
781 tipc_bearer_send(link->bearer_id, buf, addr);
782 link->next_out = next;
783 link->unacked_window = 0;
784 }
785 seqno++;
786 link->last_out = buf;
787 buf = next;
788 }
789 link->next_out_no = seqno;
790 link->out_queue_size = qsz;
791 return 0;
792}
793
794/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400795 * tipc_link_xmit() is the general link level function for message sending
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500796 * @buf: chain of buffers containing message
797 * @dsz: amount of user data to be sent
798 * @dnode: address of destination node
799 * @selector: a number used for deterministic link selection
800 * Consumes the buffer chain, except when returning -ELINKCONG
801 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
802 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400803int tipc_link_xmit(struct sk_buff *buf, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500804{
805 struct tipc_link *link = NULL;
806 struct tipc_node *node;
807 int rc = -EHOSTUNREACH;
808
809 node = tipc_node_find(dnode);
810 if (node) {
811 tipc_node_lock(node);
812 link = node->active_links[selector & 1];
813 if (link)
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400814 rc = __tipc_link_xmit(link, buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500815 tipc_node_unlock(node);
816 }
817
818 if (link)
819 return rc;
820
821 if (likely(in_own_node(dnode)))
822 return tipc_sk_rcv(buf);
823
824 kfree_skb_list(buf);
825 return rc;
826}
827
Jon Maloyc64f7a62012-11-16 13:51:31 +0800828/*
Ying Xue247f0f32014-02-18 16:06:46 +0800829 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800830 *
831 * Give a newly added peer node the sequence number where it should
832 * start receiving and acking broadcast packets.
833 *
834 * Called with node locked
835 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400836static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800837{
838 struct sk_buff *buf;
839 struct tipc_msg *msg;
840
841 buf = tipc_buf_acquire(INT_H_SIZE);
842 if (!buf)
843 return;
844
845 msg = buf_msg(buf);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400846 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
847 msg_set_last_bcast(msg, link->owner->bclink.acked);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400848 __tipc_link_xmit(link, buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800849}
850
851/*
Ying Xue247f0f32014-02-18 16:06:46 +0800852 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800853 * Receive the sequence number where we should start receiving and
854 * acking broadcast packets from a newly added peer node, and open
855 * up for reception of such packets.
856 *
857 * Called with node locked
858 */
Ying Xue247f0f32014-02-18 16:06:46 +0800859static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800860{
861 struct tipc_msg *msg = buf_msg(buf);
862
863 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
864 n->bclink.recv_permitted = true;
865 kfree_skb(buf);
866}
867
868/*
Per Liden4323add2006-01-18 00:38:21 +0100869 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 */
stephen hemminger98056962014-01-04 13:47:48 -0800871static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872{
873 struct sk_buff *buf = l_ptr->first_out;
874 u32 r_q_size = l_ptr->retransm_queue_size;
875 u32 r_q_head = l_ptr->retransm_queue_head;
876
877 /* Step to position where retransmission failed, if any, */
878 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900880 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -0400882 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883
884 while (buf && less(first, r_q_head)) {
885 first = mod(first + 1);
886 buf = buf->next;
887 }
888 l_ptr->retransm_queue_head = r_q_head = first;
889 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
890 }
891
892 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +0000893 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900895 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800896 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800897 l_ptr->retransm_queue_head = mod(++r_q_head);
898 l_ptr->retransm_queue_size = --r_q_size;
899 l_ptr->stats.retransmitted++;
900 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 }
902
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 buf = l_ptr->next_out;
905 if (buf) {
906 struct tipc_msg *msg = buf_msg(buf);
907 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -0400908 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909
910 if (mod(next - first) < l_ptr->queue_limit[0]) {
911 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900912 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800913 tipc_bearer_send(l_ptr->bearer_id, buf,
914 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800915 if (msg_user(msg) == MSG_BUNDLER)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500916 msg_set_type(msg, BUNDLE_CLOSED);
Ying Xue3c294cb2012-11-15 11:34:45 +0800917 l_ptr->next_out = buf->next;
918 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 }
920 }
Ying Xue3c294cb2012-11-15 11:34:45 +0800921 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922}
923
924/*
925 * push_queue(): push out the unsent messages of a link where
926 * congestion has abated. Node is locked
927 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500928void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929{
930 u32 res;
931
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932 do {
Per Liden4323add2006-01-18 00:38:21 +0100933 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700934 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935}
936
Ying Xue3f5a12b2014-05-05 08:56:17 +0800937void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700938{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700939 char addr_string[16];
940 u32 i;
941
Ying Xue3f5a12b2014-05-05 08:56:17 +0800942 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700943
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400944 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800945 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700946
947 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800948 if (node->links[i]) {
949 link_print(node->links[i], "Resetting link\n");
950 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700951 }
952 }
953
Ying Xue3f5a12b2014-05-05 08:56:17 +0800954 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700955}
956
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500957static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400958 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700959{
960 struct tipc_msg *msg = buf_msg(buf);
961
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400962 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700963
964 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700965 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000966 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700967 tipc_link_reset(l_ptr);
968
969 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700970 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700971 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700972 char addr_string[16];
973
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400974 pr_info("Msg seq number: %u, ", msg_seqno(msg));
975 pr_cont("Outstanding acks: %lu\n",
976 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700977
Allan Stephens01d83ed2011-01-18 13:53:16 -0500978 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700979 tipc_node_lock(n_ptr);
980
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000981 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400982 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800983 pr_info("Reception permitted: %d, Acked: %u\n",
984 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400985 n_ptr->bclink.acked);
986 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
987 n_ptr->bclink.last_in,
988 n_ptr->bclink.oos_state,
989 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700990
Allan Stephensd356eeb2006-06-25 23:40:01 -0700991 tipc_node_unlock(n_ptr);
992
Ying Xue3f5a12b2014-05-05 08:56:17 +0800993 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700994 l_ptr->stale_count = 0;
995 }
996}
997
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500998void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +0100999 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000{
1001 struct tipc_msg *msg;
1002
Allan Stephensd356eeb2006-06-25 23:40:01 -07001003 if (!buf)
1004 return;
1005
1006 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001007
Erik Hugne512137e2013-12-06 10:08:00 -05001008 /* Detect repeated retransmit failures */
1009 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1010 if (++l_ptr->stale_count > 100) {
1011 link_retransmit_failure(l_ptr, buf);
1012 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001013 }
1014 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001015 l_ptr->last_retransmitted = msg_seqno(msg);
1016 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001018
Neil Hormanca509102010-03-15 07:58:45 +00001019 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 msg = buf_msg(buf);
1021 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001022 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001023 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001024 buf = buf->next;
1025 retransmits--;
1026 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001028
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1030}
1031
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001032/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 * link_insert_deferred_queue - insert deferred messages back into receive chain
1034 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001035static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 struct sk_buff *buf)
1037{
1038 u32 seq_no;
1039
1040 if (l_ptr->oldest_deferred_in == NULL)
1041 return buf;
1042
Allan Stephensf9057302011-10-24 16:03:12 -04001043 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 if (seq_no == mod(l_ptr->next_in_no)) {
1045 l_ptr->newest_deferred_in->next = buf;
1046 buf = l_ptr->oldest_deferred_in;
1047 l_ptr->oldest_deferred_in = NULL;
1048 l_ptr->deferred_inqueue_sz = 0;
1049 }
1050 return buf;
1051}
1052
Allan Stephens85035562008-04-15 19:04:54 -07001053/**
1054 * link_recv_buf_validate - validate basic format of received message
1055 *
1056 * This routine ensures a TIPC message has an acceptable header, and at least
1057 * as much data as the header indicates it should. The routine also ensures
1058 * that the entire message header is stored in the main fragment of the message
1059 * buffer, to simplify future access to message header fields.
1060 *
1061 * Note: Having extra info present in the message header or data areas is OK.
1062 * TIPC will ignore the excess, under the assumption that it is optional info
1063 * introduced by a later release of the protocol.
1064 */
Allan Stephens85035562008-04-15 19:04:54 -07001065static int link_recv_buf_validate(struct sk_buff *buf)
1066{
1067 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001068 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001069 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1070 };
1071
1072 struct tipc_msg *msg;
1073 u32 tipc_hdr[2];
1074 u32 size;
1075 u32 hdr_size;
1076 u32 min_hdr_size;
1077
Erik Hugne64380a02014-02-11 11:38:26 +01001078 /* If this packet comes from the defer queue, the skb has already
1079 * been validated
1080 */
1081 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1082 return 1;
1083
Allan Stephens85035562008-04-15 19:04:54 -07001084 if (unlikely(buf->len < MIN_H_SIZE))
1085 return 0;
1086
1087 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1088 if (msg == NULL)
1089 return 0;
1090
1091 if (unlikely(msg_version(msg) != TIPC_VERSION))
1092 return 0;
1093
1094 size = msg_size(msg);
1095 hdr_size = msg_hdr_sz(msg);
1096 min_hdr_size = msg_isdata(msg) ?
1097 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1098
1099 if (unlikely((hdr_size < min_hdr_size) ||
1100 (size < hdr_size) ||
1101 (buf->len < size) ||
1102 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1103 return 0;
1104
1105 return pskb_may_pull(buf, hdr_size);
1106}
1107
Allan Stephensb02b69c2010-08-17 11:00:07 +00001108/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001109 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001110 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001111 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001112 *
1113 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1114 * structure (i.e. cannot be NULL), but bearer can be inactive.
1115 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001116void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001119 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001120 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 struct sk_buff *crs;
1122 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001123 struct tipc_msg *msg;
1124 u32 seq_no;
1125 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 u32 released = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001129 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001130
1131 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001132 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001133 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134
Allan Stephensfe13dda2008-04-15 19:03:23 -07001135 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001136 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001137 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001138
Allan Stephens85035562008-04-15 19:04:54 -07001139 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001140 msg = buf_msg(buf);
1141
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001143 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001144 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001145 else
Ying Xue247f0f32014-02-18 16:06:46 +08001146 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 continue;
1148 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001149
Allan Stephensed33a9c2011-04-05 15:15:04 -04001150 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001151 if (unlikely(!msg_short(msg) &&
1152 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001153 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001154
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001155 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001156 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001158 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001159 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001160
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001161 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001163 if (unlikely(!l_ptr))
1164 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001165
Allan Stephensb4b56102011-05-27 11:00:51 -04001166 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001167 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001168 msg_user(msg) == LINK_PROTOCOL &&
1169 (msg_type(msg) == RESET_MSG ||
1170 msg_type(msg) == ACTIVATE_MSG) &&
1171 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001172 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001173
Ying Xue10f465c2014-05-05 08:56:11 +08001174 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001175 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001176
Allan Stephens85035562008-04-15 19:04:54 -07001177 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001178 seq_no = msg_seqno(msg);
1179 ackd = msg_ack(msg);
1180
1181 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001182 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001183 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184
1185 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001186 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001187 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 struct sk_buff *next = crs->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001189 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 crs = next;
1191 released++;
1192 }
1193 if (released) {
1194 l_ptr->first_out = crs;
1195 l_ptr->out_queue_size -= released;
1196 }
Allan Stephens85035562008-04-15 19:04:54 -07001197
1198 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001200 tipc_link_push_queue(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001201
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001202 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1203 link_prepare_wakeup(l_ptr);
1204 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1205 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001206
Jon Paul Maloya5377832014-02-13 17:29:15 -05001207 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001208 if (unlikely(!link_working_working(l_ptr))) {
1209 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001210 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001211 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001212 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 continue;
1214 }
Ying Xue3af390e2013-10-30 11:26:57 +08001215
1216 /* Traffic message. Conditionally activate link */
1217 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1218
1219 if (link_working_working(l_ptr)) {
1220 /* Re-insert buffer in front of queue */
1221 buf->next = head;
1222 head = buf;
1223 tipc_node_unlock(n_ptr);
1224 continue;
1225 }
1226 goto unlock_discard;
1227 }
1228
1229 /* Link is now in state WORKING_WORKING */
1230 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 link_handle_out_of_seq_msg(l_ptr, buf);
1232 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001233 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 continue;
1235 }
Ying Xue3af390e2013-10-30 11:26:57 +08001236 l_ptr->next_in_no++;
1237 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001239
Erik Hugne3f53bd82014-07-01 10:22:41 +02001240 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1241 l_ptr->stats.sent_acks++;
1242 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1243 }
1244
Erik Hugne7ae934b2014-07-01 10:22:40 +02001245 if (tipc_link_prepare_input(l_ptr, &buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001246 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 }
Per Liden4323add2006-01-18 00:38:21 +01001249 tipc_node_unlock(n_ptr);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001250 msg = buf_msg(buf);
1251 if (tipc_link_input(l_ptr, buf) != 0)
1252 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001253 continue;
1254unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001255 tipc_node_unlock(n_ptr);
1256discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001257 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259}
1260
Ben Hutchings2c530402012-07-10 10:55:09 +00001261/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001262 * tipc_link_prepare_input - process TIPC link messages
1263 *
1264 * returns nonzero if the message was consumed
1265 *
1266 * Node lock must be held
1267 */
1268static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1269{
1270 struct tipc_node *n;
1271 struct tipc_msg *msg;
1272 int res = -EINVAL;
1273
1274 n = l->owner;
1275 msg = buf_msg(*buf);
1276 switch (msg_user(msg)) {
1277 case CHANGEOVER_PROTOCOL:
1278 if (tipc_link_tunnel_rcv(n, buf))
1279 res = 0;
1280 break;
1281 case MSG_FRAGMENTER:
1282 l->stats.recv_fragments++;
1283 if (tipc_buf_append(&l->reasm_buf, buf)) {
1284 l->stats.recv_fragmented++;
1285 res = 0;
1286 } else if (!l->reasm_buf) {
1287 tipc_link_reset(l);
1288 }
1289 break;
1290 case MSG_BUNDLER:
1291 l->stats.recv_bundles++;
1292 l->stats.recv_bundled += msg_msgcnt(msg);
1293 res = 0;
1294 break;
1295 case NAME_DISTRIBUTOR:
1296 n->bclink.recv_permitted = true;
1297 res = 0;
1298 break;
1299 case BCAST_PROTOCOL:
1300 tipc_link_sync_rcv(n, *buf);
1301 break;
1302 default:
1303 res = 0;
1304 }
1305 return res;
1306}
1307/**
1308 * tipc_link_input - Deliver message too higher layers
1309 */
1310static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1311{
1312 struct tipc_msg *msg = buf_msg(buf);
1313 int res = 0;
1314
1315 switch (msg_user(msg)) {
1316 case TIPC_LOW_IMPORTANCE:
1317 case TIPC_MEDIUM_IMPORTANCE:
1318 case TIPC_HIGH_IMPORTANCE:
1319 case TIPC_CRITICAL_IMPORTANCE:
1320 case CONN_MANAGER:
1321 tipc_sk_rcv(buf);
1322 break;
1323 case NAME_DISTRIBUTOR:
1324 tipc_named_rcv(buf);
1325 break;
1326 case MSG_BUNDLER:
1327 tipc_link_bundle_rcv(buf);
1328 break;
1329 default:
1330 res = -EINVAL;
1331 }
1332 return res;
1333}
1334
1335/**
Allan Stephens8809b252011-10-25 10:44:35 -04001336 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1337 *
1338 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 */
Allan Stephens8809b252011-10-25 10:44:35 -04001340u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001341 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342{
Allan Stephens8809b252011-10-25 10:44:35 -04001343 struct sk_buff *queue_buf;
1344 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001345 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346
1347 buf->next = NULL;
1348
1349 /* Empty queue ? */
1350 if (*head == NULL) {
1351 *head = *tail = buf;
1352 return 1;
1353 }
1354
1355 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001356 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 (*tail)->next = buf;
1358 *tail = buf;
1359 return 1;
1360 }
1361
Allan Stephens8809b252011-10-25 10:44:35 -04001362 /* Locate insertion point in queue, then insert; discard if duplicate */
1363 prev = head;
1364 queue_buf = *head;
1365 for (;;) {
1366 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367
Allan Stephens8809b252011-10-25 10:44:35 -04001368 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001369 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001370 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 }
Allan Stephens8809b252011-10-25 10:44:35 -04001372
1373 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375
Allan Stephens8809b252011-10-25 10:44:35 -04001376 prev = &queue_buf->next;
1377 queue_buf = queue_buf->next;
1378 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379
Allan Stephens8809b252011-10-25 10:44:35 -04001380 buf->next = queue_buf;
1381 *prev = buf;
1382 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383}
1384
Allan Stephens8809b252011-10-25 10:44:35 -04001385/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1387 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001388static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389 struct sk_buff *buf)
1390{
Allan Stephensf9057302011-10-24 16:03:12 -04001391 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392
1393 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001394 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395 return;
1396 }
1397
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399 l_ptr->checkpoint--;
1400
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001401 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 * Discard packet if a duplicate; otherwise add it to deferred queue
1403 * and notify peer of gap as per protocol specification
1404 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 if (less(seq_no, mod(l_ptr->next_in_no))) {
1406 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001407 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 return;
1409 }
1410
Per Liden4323add2006-01-18 00:38:21 +01001411 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1412 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 l_ptr->deferred_inqueue_sz++;
1414 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001415 TIPC_SKB_CB(buf)->deferred = true;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001417 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 } else
1419 l_ptr->stats.duplicates++;
1420}
1421
1422/*
1423 * Send protocol message to the other endpoint.
1424 */
Ying Xue247f0f32014-02-18 16:06:46 +08001425void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1426 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001428 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001430 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001431 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432
Ying Xue77a7e072013-12-10 20:45:44 -08001433 /* Don't send protocol message during link changeover */
1434 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001436
1437 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001438 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001439 return;
1440
Allan Stephens92d2c902011-10-25 11:20:26 -04001441 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001443 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001444 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001445 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446
1447 if (msg_typ == STATE_MSG) {
1448 u32 next_sent = mod(l_ptr->next_out_no);
1449
Per Liden4323add2006-01-18 00:38:21 +01001450 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 return;
1452 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001453 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 msg_set_next_sent(msg, next_sent);
1455 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001456 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 gap = mod(rec - mod(l_ptr->next_in_no));
1458 }
1459 msg_set_seq_gap(msg, gap);
1460 if (gap)
1461 l_ptr->stats.sent_nacks++;
1462 msg_set_link_tolerance(msg, tolerance);
1463 msg_set_linkprio(msg, priority);
1464 msg_set_max_pkt(msg, ack_mtu);
1465 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1466 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001467 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 u32 mtu = l_ptr->max_pkt;
1469
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001470 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 link_working_working(l_ptr) &&
1472 l_ptr->fsm_msg_cnt) {
1473 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001474 if (l_ptr->max_pkt_probes == 10) {
1475 l_ptr->max_pkt_target = (msg_size - 4);
1476 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001478 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001480 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481
1482 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001483 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 l_ptr->stats.sent_states++;
1485 } else { /* RESET_MSG or ACTIVATE_MSG */
1486 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1487 msg_set_seq_gap(msg, 0);
1488 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001489 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490 msg_set_link_tolerance(msg, l_ptr->tolerance);
1491 msg_set_linkprio(msg, l_ptr->priority);
1492 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1493 }
1494
Allan Stephens75f0aa42011-02-28 15:30:20 -05001495 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1496 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001498 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499
1500 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1501
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001502 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503 if (!buf)
1504 return;
1505
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001506 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001507 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508
Ying Xue7a2f7d12014-04-21 10:55:46 +08001509 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001510 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001511 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512}
1513
1514/*
1515 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001516 * Note that network plane id propagates through the network, and may
1517 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518 */
Ying Xue247f0f32014-02-18 16:06:46 +08001519static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520{
1521 u32 rec_gap = 0;
1522 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001523 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524 u32 msg_tol;
1525 struct tipc_msg *msg = buf_msg(buf);
1526
Ying Xue77a7e072013-12-10 20:45:44 -08001527 /* Discard protocol message during link changeover */
1528 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529 goto exit;
1530
Ying Xue7a2f7d12014-04-21 10:55:46 +08001531 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001533 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001536
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001538 if (!link_working_unknown(l_ptr) &&
1539 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001540 if (less_eq(msg_session(msg), l_ptr->peer_session))
1541 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001543
1544 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1545 link_working_unknown(l_ptr))) {
1546 /*
1547 * peer has lost contact -- don't allow peer's links
1548 * to reactivate before we recognize loss & clean up
1549 */
Ying Xueca9cf062014-05-08 08:54:40 +08001550 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001551 }
1552
Allan Stephens47361c82011-10-26 10:55:16 -04001553 link_state_event(l_ptr, RESET_MSG);
1554
Per Lidenb97bf3f2006-01-02 19:04:38 +01001555 /* fall thru' */
1556 case ACTIVATE_MSG:
1557 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1559
Allan Stephens2db99832010-12-31 18:59:33 +00001560 msg_tol = msg_link_tolerance(msg);
1561 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 link_set_supervision_props(l_ptr, msg_tol);
1563
1564 if (msg_linkprio(msg) > l_ptr->priority)
1565 l_ptr->priority = msg_linkprio(msg);
1566
1567 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001568 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569 if (max_pkt_info < l_ptr->max_pkt_target)
1570 l_ptr->max_pkt_target = max_pkt_info;
1571 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1572 l_ptr->max_pkt = l_ptr->max_pkt_target;
1573 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001574 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576
Allan Stephens4d753132011-10-25 12:19:05 -04001577 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001578 if (!tipc_node_is_up(l_ptr->owner)) {
1579 l_ptr->owner->bclink.last_sent =
1580 l_ptr->owner->bclink.last_in =
1581 msg_last_bcast(msg);
1582 l_ptr->owner->bclink.oos_state = 0;
1583 }
Allan Stephens4d753132011-10-25 12:19:05 -04001584
Per Lidenb97bf3f2006-01-02 19:04:38 +01001585 l_ptr->peer_session = msg_session(msg);
1586 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001587
1588 if (msg_type(msg) == ACTIVATE_MSG)
1589 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 break;
1591 case STATE_MSG:
1592
Allan Stephens2db99832010-12-31 18:59:33 +00001593 msg_tol = msg_link_tolerance(msg);
1594 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001596
1597 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001599 pr_warn("%s<%s>, priority change %u->%u\n",
1600 link_rst_msg, l_ptr->name, l_ptr->priority,
1601 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001603 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604 break;
1605 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001606
1607 /* Record reception; force mismatch at next timeout: */
1608 l_ptr->checkpoint--;
1609
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1611 l_ptr->stats.recv_states++;
1612 if (link_reset_unknown(l_ptr))
1613 break;
1614
1615 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001616 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001617 mod(l_ptr->next_in_no));
1618 }
1619
1620 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001621 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001622 l_ptr->max_pkt = max_pkt_ack;
1623 l_ptr->max_pkt_probes = 0;
1624 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001625
1626 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001627 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001629 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001630 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001631 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632
1633 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001634 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001635 tipc_bclink_update_link_state(l_ptr->owner,
1636 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637
1638 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001639 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1640 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001641 }
1642 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001644 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1645 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646 }
1647 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648 }
1649exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001650 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651}
1652
1653
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001654/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1655 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001657static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1658 struct tipc_msg *tunnel_hdr,
1659 struct tipc_msg *msg,
1660 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001662 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 struct sk_buff *buf;
1664 u32 length = msg_size(msg);
1665
1666 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001667 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001668 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001670 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001672 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001673 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001674 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001676 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001677 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1678 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001679 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680}
1681
1682
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001683/* tipc_link_failover_send_queue(): A link has gone down, but a second
1684 * link is still active. We can do failover. Tunnel the failing link's
1685 * whole send queue via the remaining link. This way, we don't lose
1686 * any packets, and sequence order is preserved for subsequent traffic
1687 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001689void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690{
1691 u32 msgcount = l_ptr->out_queue_size;
1692 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001693 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001695 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696
1697 if (!tunnel)
1698 return;
1699
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001700 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001701 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1703 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001704
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 if (!l_ptr->first_out) {
1706 struct sk_buff *buf;
1707
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001708 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001710 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001712 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001714 pr_warn("%sunable to send changeover msg\n",
1715 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 }
1717 return;
1718 }
Allan Stephensf1310722006-06-25 23:51:37 -07001719
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001720 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001721 l_ptr->owner->active_links[1]);
1722
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 while (crs) {
1724 struct tipc_msg *msg = buf_msg(crs);
1725
1726 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001728 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729
Florian Westphald788d802007-08-02 19:28:06 -07001730 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001732 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001733 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1734 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 pos += align(msg_size(m));
1736 m = (struct tipc_msg *)pos;
1737 }
1738 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001739 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1740 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 }
1742 crs = crs->next;
1743 }
1744}
1745
Ying Xue247f0f32014-02-18 16:06:46 +08001746/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001747 * duplicate of the first link's send queue via the new link. This way, we
1748 * are guaranteed that currently queued packets from a socket are delivered
1749 * before future traffic from the same socket, even if this is using the
1750 * new link. The last arriving copy of each duplicate packet is dropped at
1751 * the receiving end by the regular protocol check, so packet cardinality
1752 * and sequence order is preserved per sender/receiver socket pair.
1753 * Owner node is locked.
1754 */
Ying Xue247f0f32014-02-18 16:06:46 +08001755void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001756 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757{
1758 struct sk_buff *iter;
1759 struct tipc_msg tunnel_hdr;
1760
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001761 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001762 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001763 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1764 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1765 iter = l_ptr->first_out;
1766 while (iter) {
1767 struct sk_buff *outbuf;
1768 struct tipc_msg *msg = buf_msg(iter);
1769 u32 length = msg_size(msg);
1770
1771 if (msg_user(msg) == MSG_BUNDLER)
1772 msg_set_type(msg, CLOSED_MSG);
1773 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001774 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001776 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001778 pr_warn("%sunable to send duplicate msg\n",
1779 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 return;
1781 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001782 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1783 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1784 length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001785 __tipc_link_xmit(tunnel, outbuf);
Per Liden4323add2006-01-18 00:38:21 +01001786 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 return;
1788 iter = iter->next;
1789 }
1790}
1791
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792/**
1793 * buf_extract - extracts embedded TIPC message from another message
1794 * @skb: encapsulating message buffer
1795 * @from_pos: offset to extract from
1796 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001797 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 * encapsulating message itself is left unchanged.
1799 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1801{
1802 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1803 u32 size = msg_size(msg);
1804 struct sk_buff *eb;
1805
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001806 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001807 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001808 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 return eb;
1810}
1811
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001812
1813
1814/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1815 * Owner node is locked.
1816 */
1817static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1818 struct sk_buff *t_buf)
1819{
1820 struct sk_buff *buf;
1821
1822 if (!tipc_link_is_up(l_ptr))
1823 return;
1824
1825 buf = buf_extract(t_buf, INT_H_SIZE);
1826 if (buf == NULL) {
1827 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1828 return;
1829 }
1830
1831 /* Add buffer to deferred queue, if applicable: */
1832 link_handle_out_of_seq_msg(l_ptr, buf);
1833}
1834
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001835/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1836 * Owner node is locked.
1837 */
1838static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1839 struct sk_buff *t_buf)
1840{
1841 struct tipc_msg *t_msg = buf_msg(t_buf);
1842 struct sk_buff *buf = NULL;
1843 struct tipc_msg *msg;
1844
1845 if (tipc_link_is_up(l_ptr))
1846 tipc_link_reset(l_ptr);
1847
1848 /* First failover packet? */
1849 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1850 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1851
1852 /* Should there be an inner packet? */
1853 if (l_ptr->exp_msg_count) {
1854 l_ptr->exp_msg_count--;
1855 buf = buf_extract(t_buf, INT_H_SIZE);
1856 if (buf == NULL) {
1857 pr_warn("%sno inner failover pkt\n", link_co_err);
1858 goto exit;
1859 }
1860 msg = buf_msg(buf);
1861
1862 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1863 kfree_skb(buf);
1864 buf = NULL;
1865 goto exit;
1866 }
1867 if (msg_user(msg) == MSG_FRAGMENTER) {
1868 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001869 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001870 }
1871 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001872exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001873 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1874 tipc_node_detach_link(l_ptr->owner, l_ptr);
1875 kfree(l_ptr);
1876 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001877 return buf;
1878}
1879
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001880/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001881 * via other link as result of a failover (ORIGINAL_MSG) or
1882 * a new active link (DUPLICATE_MSG). Failover packets are
1883 * returned to the active link for delivery upwards.
1884 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05001886static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001887 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001889 struct sk_buff *t_buf = *buf;
1890 struct tipc_link *l_ptr;
1891 struct tipc_msg *t_msg = buf_msg(t_buf);
1892 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001894 *buf = NULL;
1895
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001896 if (bearer_id >= MAX_BEARERS)
1897 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001898
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001899 l_ptr = n_ptr->links[bearer_id];
1900 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001903 if (msg_type(t_msg) == DUPLICATE_MSG)
1904 tipc_link_dup_rcv(l_ptr, t_buf);
1905 else if (msg_type(t_msg) == ORIGINAL_MSG)
1906 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001907 else
1908 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001910 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001911 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912}
1913
1914/*
1915 * Bundler functionality:
1916 */
Ying Xue247f0f32014-02-18 16:06:46 +08001917void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918{
1919 u32 msgcount = msg_msgcnt(buf_msg(buf));
1920 u32 pos = INT_H_SIZE;
1921 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001922 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924 while (msgcount--) {
1925 obuf = buf_extract(buf, pos);
1926 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001927 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001928 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001929 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001930 omsg = buf_msg(obuf);
1931 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001932 if (msg_isdata(omsg)) {
1933 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
1934 tipc_sk_mcast_rcv(obuf);
1935 else
1936 tipc_sk_rcv(obuf);
1937 } else if (msg_user(omsg) == CONN_MANAGER) {
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001938 tipc_sk_rcv(obuf);
1939 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1940 tipc_named_rcv(obuf);
1941 } else {
1942 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1943 kfree_skb(obuf);
1944 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001946 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001947}
1948
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001949static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001950{
Allan Stephens5413b4c2011-01-18 13:24:55 -05001951 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
1952 return;
1953
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954 l_ptr->tolerance = tolerance;
1955 l_ptr->continuity_interval =
1956 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
1957 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
1958}
1959
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001960void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001961{
1962 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001963 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1964 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1965 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1966 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001967 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001968 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1969 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1970 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1971 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001972 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1974 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1975 /* FRAGMENT and LAST_FRAGMENT packets */
1976 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1977}
1978
Jon Paul Maloye099e862014-02-13 17:29:18 -05001979/* tipc_link_find_owner - locate owner node of link by link's name
1980 * @name: pointer to link name string
1981 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001982 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001983 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001985static struct tipc_node *tipc_link_find_owner(const char *link_name,
1986 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001987{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001988 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001989 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001990 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001991 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992
Jon Paul Maloye099e862014-02-13 17:29:18 -05001993 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001994 rcu_read_lock();
1995 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001996 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001997 for (i = 0; i < MAX_BEARERS; i++) {
1998 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001999 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
2000 *bearer_id = i;
2001 found_node = n_ptr;
2002 break;
2003 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02002004 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002005 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002006 if (found_node)
2007 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002008 }
Ying Xue6c7a7622014-03-27 12:54:37 +08002009 rcu_read_unlock();
2010
Jon Paul Maloye099e862014-02-13 17:29:18 -05002011 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012}
2013
Allan Stephens5c216e12011-10-18 11:34:29 -04002014/**
2015 * link_value_is_valid -- validate proposed link tolerance/priority/window
2016 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002017 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2018 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002019 *
2020 * Returns 1 if value is within range, 0 if not.
2021 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002022static int link_value_is_valid(u16 cmd, u32 new_value)
2023{
2024 switch (cmd) {
2025 case TIPC_CMD_SET_LINK_TOL:
2026 return (new_value >= TIPC_MIN_LINK_TOL) &&
2027 (new_value <= TIPC_MAX_LINK_TOL);
2028 case TIPC_CMD_SET_LINK_PRI:
2029 return (new_value <= TIPC_MAX_LINK_PRI);
2030 case TIPC_CMD_SET_LINK_WINDOW:
2031 return (new_value >= TIPC_MIN_LINK_WIN) &&
2032 (new_value <= TIPC_MAX_LINK_WIN);
2033 }
2034 return 0;
2035}
2036
Allan Stephens5c216e12011-10-18 11:34:29 -04002037/**
2038 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002039 * @name: ptr to link, bearer, or media name
2040 * @new_value: new value of link, bearer, or media setting
2041 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002042 *
Ying Xue7216cd92014-04-21 10:55:48 +08002043 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04002044 *
2045 * Returns 0 if value updated and negative value on error.
2046 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002047static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2048{
2049 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002050 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002051 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002052 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002053 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002054 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002055
Jon Paul Maloye099e862014-02-13 17:29:18 -05002056 node = tipc_link_find_owner(name, &bearer_id);
2057 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002058 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002059 l_ptr = node->links[bearer_id];
2060
2061 if (l_ptr) {
2062 switch (cmd) {
2063 case TIPC_CMD_SET_LINK_TOL:
2064 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002065 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2066 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002067 break;
2068 case TIPC_CMD_SET_LINK_PRI:
2069 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002070 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2071 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002072 break;
2073 case TIPC_CMD_SET_LINK_WINDOW:
2074 tipc_link_set_queue_limits(l_ptr, new_value);
2075 break;
2076 default:
2077 res = -EINVAL;
2078 break;
2079 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002080 }
2081 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002082 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002083 }
2084
2085 b_ptr = tipc_bearer_find(name);
2086 if (b_ptr) {
2087 switch (cmd) {
2088 case TIPC_CMD_SET_LINK_TOL:
2089 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002090 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002091 case TIPC_CMD_SET_LINK_PRI:
2092 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002093 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002094 case TIPC_CMD_SET_LINK_WINDOW:
2095 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002096 break;
2097 default:
2098 res = -EINVAL;
2099 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002100 }
Ying Xue636c0372013-10-18 07:23:20 +02002101 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002102 }
2103
2104 m_ptr = tipc_media_find(name);
2105 if (!m_ptr)
2106 return -ENODEV;
2107 switch (cmd) {
2108 case TIPC_CMD_SET_LINK_TOL:
2109 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002110 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002111 case TIPC_CMD_SET_LINK_PRI:
2112 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002113 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002114 case TIPC_CMD_SET_LINK_WINDOW:
2115 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002116 break;
2117 default:
2118 res = -EINVAL;
2119 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002120 }
Ying Xue636c0372013-10-18 07:23:20 +02002121 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002122}
2123
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002124struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002125 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126{
2127 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002128 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002129 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002130
2131 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002132 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002133
2134 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2135 new_value = ntohl(args->value);
2136
Allan Stephens5c216e12011-10-18 11:34:29 -04002137 if (!link_value_is_valid(cmd, new_value))
2138 return tipc_cfg_reply_error_string(
2139 "cannot change, value invalid");
2140
Per Liden4323add2006-01-18 00:38:21 +01002141 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002143 (tipc_bclink_set_queue_limits(new_value) == 0))
2144 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002145 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002146 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 }
2148
Allan Stephens5c216e12011-10-18 11:34:29 -04002149 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002151 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152
Per Liden4323add2006-01-18 00:38:21 +01002153 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002154}
2155
2156/**
2157 * link_reset_statistics - reset link statistics
2158 * @l_ptr: pointer to link
2159 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002160static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161{
2162 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2163 l_ptr->stats.sent_info = l_ptr->next_out_no;
2164 l_ptr->stats.recv_info = l_ptr->next_in_no;
2165}
2166
Per Liden4323add2006-01-18 00:38:21 +01002167struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168{
2169 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002170 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002171 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002172 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002173
2174 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002175 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002176
2177 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002178 if (!strcmp(link_name, tipc_bclink_name)) {
2179 if (tipc_bclink_reset_stats())
2180 return tipc_cfg_reply_error_string("link not found");
2181 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002182 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002183 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002184 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002185 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002186
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002187 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002188 l_ptr = node->links[bearer_id];
2189 if (!l_ptr) {
2190 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002191 return tipc_cfg_reply_error_string("link not found");
2192 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002193 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002194 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002195 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196}
2197
2198/**
2199 * percent - convert count to a percentage of total (rounding up or down)
2200 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002201static u32 percent(u32 count, u32 total)
2202{
2203 return (count * 100 + (total / 2)) / total;
2204}
2205
2206/**
Per Liden4323add2006-01-18 00:38:21 +01002207 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002208 * @name: link name
2209 * @buf: print buffer area
2210 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002211 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002212 * Returns length of print buffer data string (or 0 if error)
2213 */
Per Liden4323add2006-01-18 00:38:21 +01002214static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002215{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002216 struct tipc_link *l;
2217 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002218 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002219 char *status;
2220 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002221 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002222 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002223
Per Liden4323add2006-01-18 00:38:21 +01002224 if (!strcmp(name, tipc_bclink_name))
2225 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002226
Jon Paul Maloye099e862014-02-13 17:29:18 -05002227 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002228 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002229 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002230
Per Liden4323add2006-01-18 00:38:21 +01002231 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002232
2233 l = node->links[bearer_id];
2234 if (!l) {
2235 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002236 return 0;
2237 }
2238
Erik Hugnedc1aed32012-06-29 00:50:23 -04002239 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002240
Erik Hugnedc1aed32012-06-29 00:50:23 -04002241 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002242 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002243 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002244 status = "STANDBY";
2245 else
2246 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002247
2248 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2249 " %s MTU:%u Priority:%u Tolerance:%u ms"
2250 " Window:%u packets\n",
2251 l->name, status, l->max_pkt, l->priority,
2252 l->tolerance, l->queue_limit[0]);
2253
2254 ret += tipc_snprintf(buf + ret, buf_size - ret,
2255 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2256 l->next_in_no - s->recv_info, s->recv_fragments,
2257 s->recv_fragmented, s->recv_bundles,
2258 s->recv_bundled);
2259
2260 ret += tipc_snprintf(buf + ret, buf_size - ret,
2261 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2262 l->next_out_no - s->sent_info, s->sent_fragments,
2263 s->sent_fragmented, s->sent_bundles,
2264 s->sent_bundled);
2265
2266 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267 if (!profile_total)
2268 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002269
2270 ret += tipc_snprintf(buf + ret, buf_size - ret,
2271 " TX profile sample:%u packets average:%u octets\n"
2272 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2273 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2274 s->msg_length_counts,
2275 s->msg_lengths_total / profile_total,
2276 percent(s->msg_length_profile[0], profile_total),
2277 percent(s->msg_length_profile[1], profile_total),
2278 percent(s->msg_length_profile[2], profile_total),
2279 percent(s->msg_length_profile[3], profile_total),
2280 percent(s->msg_length_profile[4], profile_total),
2281 percent(s->msg_length_profile[5], profile_total),
2282 percent(s->msg_length_profile[6], profile_total));
2283
2284 ret += tipc_snprintf(buf + ret, buf_size - ret,
2285 " RX states:%u probes:%u naks:%u defs:%u"
2286 " dups:%u\n", s->recv_states, s->recv_probes,
2287 s->recv_nacks, s->deferred_recv, s->duplicates);
2288
2289 ret += tipc_snprintf(buf + ret, buf_size - ret,
2290 " TX states:%u probes:%u naks:%u acks:%u"
2291 " dups:%u\n", s->sent_states, s->sent_probes,
2292 s->sent_nacks, s->sent_acks, s->retransmitted);
2293
2294 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002295 " Congestion link:%u Send queue"
2296 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002297 s->max_queue_sz, s->queue_sz_counts ?
2298 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002299
Per Liden4323add2006-01-18 00:38:21 +01002300 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002301 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302}
2303
Per Liden4323add2006-01-18 00:38:21 +01002304struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002305{
2306 struct sk_buff *buf;
2307 struct tlv_desc *rep_tlv;
2308 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002309 int pb_len;
2310 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002311
2312 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002313 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002314
Erik Hugnedc1aed32012-06-29 00:50:23 -04002315 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002316 if (!buf)
2317 return NULL;
2318
2319 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002320 pb = TLV_DATA(rep_tlv);
2321 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002322 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002323 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002324 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002325 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002326 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002327 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002328 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002329 skb_put(buf, TLV_SPACE(str_len));
2330 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2331
2332 return buf;
2333}
2334
Per Lidenb97bf3f2006-01-02 19:04:38 +01002335/**
Per Liden4323add2006-01-18 00:38:21 +01002336 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337 * @dest: network address of destination node
2338 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002339 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002340 * If no active link can be found, uses default maximum packet size.
2341 */
Per Liden4323add2006-01-18 00:38:21 +01002342u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002343{
David S. Miller6c000552008-09-02 23:38:32 -07002344 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002345 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002346 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002347
Per Lidenb97bf3f2006-01-02 19:04:38 +01002348 if (dest == tipc_own_addr)
2349 return MAX_MSG_SIZE;
2350
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002351 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002352 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002353 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002354 l_ptr = n_ptr->active_links[selector & 1];
2355 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002356 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002357 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002358 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359 return res;
2360}
2361
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002362static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002364 struct tipc_bearer *b_ptr;
2365
2366 rcu_read_lock();
2367 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2368 if (b_ptr)
2369 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2370 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002371
Per Lidenb97bf3f2006-01-02 19:04:38 +01002372 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002373 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002374 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002375 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002376 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002377 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002378 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002379 pr_cont(":WW\n");
2380 else
2381 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002382}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002383
2384/* Parse and validate nested (link) properties valid for media, bearer and link
2385 */
2386int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2387{
2388 int err;
2389
2390 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2391 tipc_nl_prop_policy);
2392 if (err)
2393 return err;
2394
2395 if (props[TIPC_NLA_PROP_PRIO]) {
2396 u32 prio;
2397
2398 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2399 if (prio > TIPC_MAX_LINK_PRI)
2400 return -EINVAL;
2401 }
2402
2403 if (props[TIPC_NLA_PROP_TOL]) {
2404 u32 tol;
2405
2406 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2407 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2408 return -EINVAL;
2409 }
2410
2411 if (props[TIPC_NLA_PROP_WIN]) {
2412 u32 win;
2413
2414 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2415 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2416 return -EINVAL;
2417 }
2418
2419 return 0;
2420}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002421
Richard Alpef96ce7a2014-11-20 10:29:13 +01002422int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2423{
2424 int err;
2425 int res = 0;
2426 int bearer_id;
2427 char *name;
2428 struct tipc_link *link;
2429 struct tipc_node *node;
2430 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2431
2432 if (!info->attrs[TIPC_NLA_LINK])
2433 return -EINVAL;
2434
2435 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2436 info->attrs[TIPC_NLA_LINK],
2437 tipc_nl_link_policy);
2438 if (err)
2439 return err;
2440
2441 if (!attrs[TIPC_NLA_LINK_NAME])
2442 return -EINVAL;
2443
2444 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2445
2446 node = tipc_link_find_owner(name, &bearer_id);
2447 if (!node)
2448 return -EINVAL;
2449
2450 tipc_node_lock(node);
2451
2452 link = node->links[bearer_id];
2453 if (!link) {
2454 res = -EINVAL;
2455 goto out;
2456 }
2457
2458 if (attrs[TIPC_NLA_LINK_PROP]) {
2459 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2460
2461 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2462 props);
2463 if (err) {
2464 res = err;
2465 goto out;
2466 }
2467
2468 if (props[TIPC_NLA_PROP_TOL]) {
2469 u32 tol;
2470
2471 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2472 link_set_supervision_props(link, tol);
2473 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2474 }
2475 if (props[TIPC_NLA_PROP_PRIO]) {
2476 u32 prio;
2477
2478 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2479 link->priority = prio;
2480 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2481 }
2482 if (props[TIPC_NLA_PROP_WIN]) {
2483 u32 win;
2484
2485 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2486 tipc_link_set_queue_limits(link, win);
2487 }
2488 }
2489
2490out:
2491 tipc_node_unlock(node);
2492
2493 return res;
2494}
Richard Alped8182802014-11-24 11:10:29 +01002495
2496static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002497{
2498 int i;
2499 struct nlattr *stats;
2500
2501 struct nla_map {
2502 u32 key;
2503 u32 val;
2504 };
2505
2506 struct nla_map map[] = {
2507 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2508 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2509 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2510 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2511 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2512 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2513 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2514 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2515 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2516 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2517 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2518 s->msg_length_counts : 1},
2519 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2520 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2521 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2522 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2523 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2524 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2525 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2526 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2527 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2528 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2529 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2530 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2531 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2532 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2533 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2534 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2535 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2536 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2537 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2538 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2539 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2540 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2541 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2542 };
2543
2544 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2545 if (!stats)
2546 return -EMSGSIZE;
2547
2548 for (i = 0; i < ARRAY_SIZE(map); i++)
2549 if (nla_put_u32(skb, map[i].key, map[i].val))
2550 goto msg_full;
2551
2552 nla_nest_end(skb, stats);
2553
2554 return 0;
2555msg_full:
2556 nla_nest_cancel(skb, stats);
2557
2558 return -EMSGSIZE;
2559}
2560
2561/* Caller should hold appropriate locks to protect the link */
Richard Alped8182802014-11-24 11:10:29 +01002562static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002563{
2564 int err;
2565 void *hdr;
2566 struct nlattr *attrs;
2567 struct nlattr *prop;
2568
2569 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2570 NLM_F_MULTI, TIPC_NL_LINK_GET);
2571 if (!hdr)
2572 return -EMSGSIZE;
2573
2574 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2575 if (!attrs)
2576 goto msg_full;
2577
2578 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2579 goto attr_msg_full;
2580 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2581 tipc_cluster_mask(tipc_own_addr)))
2582 goto attr_msg_full;
2583 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2584 goto attr_msg_full;
2585 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2586 goto attr_msg_full;
2587 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2588 goto attr_msg_full;
2589
2590 if (tipc_link_is_up(link))
2591 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2592 goto attr_msg_full;
2593 if (tipc_link_is_active(link))
2594 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2595 goto attr_msg_full;
2596
2597 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2598 if (!prop)
2599 goto attr_msg_full;
2600 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2601 goto prop_msg_full;
2602 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2603 goto prop_msg_full;
2604 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2605 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2606 goto prop_msg_full;
2607 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2608 goto prop_msg_full;
2609 nla_nest_end(msg->skb, prop);
2610
2611 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2612 if (err)
2613 goto attr_msg_full;
2614
2615 nla_nest_end(msg->skb, attrs);
2616 genlmsg_end(msg->skb, hdr);
2617
2618 return 0;
2619
2620prop_msg_full:
2621 nla_nest_cancel(msg->skb, prop);
2622attr_msg_full:
2623 nla_nest_cancel(msg->skb, attrs);
2624msg_full:
2625 genlmsg_cancel(msg->skb, hdr);
2626
2627 return -EMSGSIZE;
2628}
2629
2630/* Caller should hold node lock */
Richard Alped8182802014-11-24 11:10:29 +01002631static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
2632 struct tipc_node *node,
2633 u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002634{
2635 u32 i;
2636 int err;
2637
2638 for (i = *prev_link; i < MAX_BEARERS; i++) {
2639 *prev_link = i;
2640
2641 if (!node->links[i])
2642 continue;
2643
2644 err = __tipc_nl_add_link(msg, node->links[i]);
2645 if (err)
2646 return err;
2647 }
2648 *prev_link = 0;
2649
2650 return 0;
2651}
2652
2653int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2654{
2655 struct tipc_node *node;
2656 struct tipc_nl_msg msg;
2657 u32 prev_node = cb->args[0];
2658 u32 prev_link = cb->args[1];
2659 int done = cb->args[2];
2660 int err;
2661
2662 if (done)
2663 return 0;
2664
2665 msg.skb = skb;
2666 msg.portid = NETLINK_CB(cb->skb).portid;
2667 msg.seq = cb->nlh->nlmsg_seq;
2668
2669 rcu_read_lock();
2670
2671 if (prev_node) {
2672 node = tipc_node_find(prev_node);
2673 if (!node) {
2674 /* We never set seq or call nl_dump_check_consistent()
2675 * this means that setting prev_seq here will cause the
2676 * consistence check to fail in the netlink callback
2677 * handler. Resulting in the last NLMSG_DONE message
2678 * having the NLM_F_DUMP_INTR flag set.
2679 */
2680 cb->prev_seq = 1;
2681 goto out;
2682 }
2683
2684 list_for_each_entry_continue_rcu(node, &tipc_node_list, list) {
2685 tipc_node_lock(node);
2686 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2687 tipc_node_unlock(node);
2688 if (err)
2689 goto out;
2690
2691 prev_node = node->addr;
2692 }
2693 } else {
2694 err = tipc_nl_add_bc_link(&msg);
2695 if (err)
2696 goto out;
2697
2698 list_for_each_entry_rcu(node, &tipc_node_list, list) {
2699 tipc_node_lock(node);
2700 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2701 tipc_node_unlock(node);
2702 if (err)
2703 goto out;
2704
2705 prev_node = node->addr;
2706 }
2707 }
2708 done = 1;
2709out:
2710 rcu_read_unlock();
2711
2712 cb->args[0] = prev_node;
2713 cb->args[1] = prev_link;
2714 cb->args[2] = done;
2715
2716 return skb->len;
2717}
2718
2719int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2720{
2721 struct sk_buff *ans_skb;
2722 struct tipc_nl_msg msg;
2723 struct tipc_link *link;
2724 struct tipc_node *node;
2725 char *name;
2726 int bearer_id;
2727 int err;
2728
2729 if (!info->attrs[TIPC_NLA_LINK_NAME])
2730 return -EINVAL;
2731
2732 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
2733 node = tipc_link_find_owner(name, &bearer_id);
2734 if (!node)
2735 return -EINVAL;
2736
2737 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2738 if (!ans_skb)
2739 return -ENOMEM;
2740
2741 msg.skb = ans_skb;
2742 msg.portid = info->snd_portid;
2743 msg.seq = info->snd_seq;
2744
2745 tipc_node_lock(node);
2746 link = node->links[bearer_id];
2747 if (!link) {
2748 err = -EINVAL;
2749 goto err_out;
2750 }
2751
2752 err = __tipc_nl_add_link(&msg, link);
2753 if (err)
2754 goto err_out;
2755
2756 tipc_node_unlock(node);
2757
2758 return genlmsg_reply(ans_skb, info);
2759
2760err_out:
2761 tipc_node_unlock(node);
2762 nlmsg_free(ans_skb);
2763
2764 return err;
2765}
Richard Alpeae363422014-11-20 10:29:14 +01002766
2767int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2768{
2769 int err;
2770 char *link_name;
2771 unsigned int bearer_id;
2772 struct tipc_link *link;
2773 struct tipc_node *node;
2774 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2775
2776 if (!info->attrs[TIPC_NLA_LINK])
2777 return -EINVAL;
2778
2779 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2780 info->attrs[TIPC_NLA_LINK],
2781 tipc_nl_link_policy);
2782 if (err)
2783 return err;
2784
2785 if (!attrs[TIPC_NLA_LINK_NAME])
2786 return -EINVAL;
2787
2788 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2789
2790 if (strcmp(link_name, tipc_bclink_name) == 0) {
2791 err = tipc_bclink_reset_stats();
2792 if (err)
2793 return err;
2794 return 0;
2795 }
2796
2797 node = tipc_link_find_owner(link_name, &bearer_id);
2798 if (!node)
2799 return -EINVAL;
2800
2801 tipc_node_lock(node);
2802
2803 link = node->links[bearer_id];
2804 if (!link) {
2805 tipc_node_unlock(node);
2806 return -EINVAL;
2807 }
2808
2809 link_reset_statistics(link);
2810
2811 tipc_node_unlock(node);
2812
2813 return 0;
2814}