blob: d9c2310e417d9950af27e8c0608f886a833c6972 [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
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800153 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500155int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156{
157 if (!l_ptr)
158 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000159 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160}
161
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500162int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000164 return (l_ptr->owner->active_links[0] == l_ptr) ||
165 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166}
167
168/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 * link_timeout - handle expiration of link timer
170 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500172static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173{
Ying Xue58dc55f2014-11-26 11:41:52 +0800174 struct sk_buff *skb;
175
Per Liden4323add2006-01-18 00:38:21 +0100176 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177
178 /* update counters used in statistical profiling of send traffic */
Ying Xue58dc55f2014-11-26 11:41:52 +0800179 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->outqueue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 l_ptr->stats.queue_sz_counts++;
181
Ying Xue58dc55f2014-11-26 11:41:52 +0800182 skb = skb_peek(&l_ptr->outqueue);
183 if (skb) {
184 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 u32 length = msg_size(msg);
186
Joe Perchesf64f9e72009-11-29 16:55:45 -0800187 if ((msg_user(msg) == MSG_FRAGMENTER) &&
188 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 length = msg_size(msg_get_wrapped(msg));
190 }
191 if (length) {
192 l_ptr->stats.msg_lengths_total += length;
193 l_ptr->stats.msg_length_counts++;
194 if (length <= 64)
195 l_ptr->stats.msg_length_profile[0]++;
196 else if (length <= 256)
197 l_ptr->stats.msg_length_profile[1]++;
198 else if (length <= 1024)
199 l_ptr->stats.msg_length_profile[2]++;
200 else if (length <= 4096)
201 l_ptr->stats.msg_length_profile[3]++;
202 else if (length <= 16384)
203 l_ptr->stats.msg_length_profile[4]++;
204 else if (length <= 32768)
205 l_ptr->stats.msg_length_profile[5]++;
206 else
207 l_ptr->stats.msg_length_profile[6]++;
208 }
209 }
210
211 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 link_state_event(l_ptr, TIMEOUT_EVT);
213
214 if (l_ptr->next_out)
Ying Xue47b4c9a2014-11-26 11:41:48 +0800215 tipc_link_push_packets(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216
Per Liden4323add2006-01-18 00:38:21 +0100217 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218}
219
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500220static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221{
222 k_start_timer(&l_ptr->timer, time);
223}
224
225/**
Per Liden4323add2006-01-18 00:38:21 +0100226 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500227 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900230 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 * Returns pointer to link.
232 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500233struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500234 struct tipc_bearer *b_ptr,
235 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500237 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 struct tipc_msg *msg;
239 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500240 char addr_string[16];
241 u32 peer = n_ptr->addr;
242
Holger Brunck0372bf52014-11-14 18:33:19 +0100243 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500244 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100245 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
246 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500247 return NULL;
248 }
249
250 if (n_ptr->links[b_ptr->identity]) {
251 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400252 pr_err("Attempt to establish second link on <%s> to %s\n",
253 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500254 return NULL;
255 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700257 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400259 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 return NULL;
261 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262
263 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500264 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400265 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900267 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 if_name,
269 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400270 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500272 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400274 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800275 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400276 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 l_ptr->state = RESET_UNKNOWN;
278
279 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
280 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000281 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700283 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 msg_set_bearer_id(msg, b_ptr->identity);
285 strcpy((char *)msg_data(msg), if_name);
286
287 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400288 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289
Ying Xue7a2f7d12014-04-21 10:55:46 +0800290 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 link_init_max_pkt(l_ptr);
292
293 l_ptr->next_out_no = 1;
Ying Xue58dc55f2014-11-26 11:41:52 +0800294 __skb_queue_head_init(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800295 __skb_queue_head_init(&l_ptr->deferred_queue);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400296 __skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297
298 link_reset_statistics(l_ptr);
299
Allan Stephens37b9c082011-02-28 11:32:27 -0500300 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500302 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
303 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500304
305 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 return l_ptr;
308}
309
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500310void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500311{
312 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500313 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500314
Ying Xue6c7a7622014-03-27 12:54:37 +0800315 rcu_read_lock();
316 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800317 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500318 l_ptr = n_ptr->links[bearer_id];
319 if (l_ptr) {
320 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500321 if (shutting_down || !tipc_node_is_up(n_ptr)) {
322 tipc_node_detach_link(l_ptr->owner, l_ptr);
323 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800324 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500325
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500326 /* Nobody else can access this link now: */
327 del_timer_sync(&l_ptr->timer);
328 kfree(l_ptr);
329 } else {
330 /* Detach/delete when failover is finished: */
331 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800332 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500333 del_timer_sync(&l_ptr->timer);
334 }
Ying Xuec61dd612014-02-13 17:29:09 -0500335 continue;
336 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800337 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500338 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800339 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500340}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341
342/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400343 * link_schedule_user - schedule user for wakeup after congestion
344 * @link: congested link
345 * @oport: sending port
346 * @chain_sz: size of buffer chain that was attempted sent
347 * @imp: importance of message attempted sent
348 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400350static bool link_schedule_user(struct tipc_link *link, u32 oport,
351 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400353 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400355 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
356 tipc_own_addr, oport, 0, 0);
357 if (!buf)
358 return false;
359 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
360 TIPC_SKB_CB(buf)->chain_imp = imp;
361 __skb_queue_tail(&link->waiting_sks, buf);
362 link->stats.link_congs++;
363 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364}
365
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400366/**
367 * link_prepare_wakeup - prepare users for wakeup after congestion
368 * @link: congested link
369 * Move a number of waiting users, as permitted by available space in
370 * the send queue, from link wait queue to node wait queue for wakeup
371 */
372static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373{
Ying Xue58dc55f2014-11-26 11:41:52 +0800374 uint pend_qsz = skb_queue_len(&link->outqueue);
Ying Xue58d78b32014-11-26 11:41:51 +0800375 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376
Ying Xue58d78b32014-11-26 11:41:51 +0800377 skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
378 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 break;
Ying Xue58d78b32014-11-26 11:41:51 +0800380 pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
381 __skb_unlink(skb, &link->waiting_sks);
382 __skb_queue_tail(&link->owner->waiting_sks, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384}
385
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900386/**
Per Liden4323add2006-01-18 00:38:21 +0100387 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 * @l_ptr: pointer to link
389 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500390void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400392 kfree_skb(l_ptr->reasm_buf);
393 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394}
395
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900396/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500397 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 * @l_ptr: pointer to link
399 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500400void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401{
Ying Xuebc6fecd2014-11-26 11:41:53 +0800402 __skb_queue_purge(&l_ptr->deferred_queue);
Ying Xue58dc55f2014-11-26 11:41:52 +0800403 __skb_queue_purge(&l_ptr->outqueue);
Per Liden4323add2006-01-18 00:38:21 +0100404 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405}
406
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500407void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 u32 prev_state = l_ptr->state;
410 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700411 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400412 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900413
Allan Stephensa686e682008-06-04 17:29:39 -0700414 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415
Allan Stephensa686e682008-06-04 17:29:39 -0700416 /* Link is down, accept any session */
417 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900419 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423
424 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
425 return;
426
Per Liden4323add2006-01-18 00:38:21 +0100427 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800428 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000429
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500430 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 l_ptr->reset_checkpoint = checkpoint;
432 l_ptr->exp_msg_count = START_CHANGEOVER;
433 }
434
435 /* Clean up all queues: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800436 __skb_queue_purge(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800437 __skb_queue_purge(&l_ptr->deferred_queue);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400438 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
439 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
440 owner->action_flags |= TIPC_WAKEUP_USERS;
441 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 l_ptr->next_out = NULL;
443 l_ptr->unacked_window = 0;
444 l_ptr->checkpoint = 1;
445 l_ptr->next_out_no = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 l_ptr->fsm_msg_cnt = 0;
447 l_ptr->stale_count = 0;
448 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449}
450
Ying Xuec61dd612014-02-13 17:29:09 -0500451void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500452{
453 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500454 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500455
Ying Xue6c7a7622014-03-27 12:54:37 +0800456 rcu_read_lock();
457 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800458 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500459 l_ptr = n_ptr->links[bearer_id];
460 if (l_ptr)
461 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800462 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500463 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800464 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500465}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500467static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468{
Allan Stephens5392d642006-06-25 23:52:50 -0700469 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100470 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800471 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472}
473
474/**
475 * link_state_event - link finite state machine
476 * @l_ptr: pointer to link
477 * @event: state machine event to process
478 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000479static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500481 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 u32 cont_intv = l_ptr->continuity_interval;
483
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500484 if (l_ptr->flags & LINK_STOPPED)
485 return;
486
Ying Xue135daee2014-02-13 17:29:08 -0500487 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 return; /* Not yet. */
489
Ying Xue77a7e072013-12-10 20:45:44 -0800490 /* Check whether changeover is going on */
491 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000492 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800494 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496
497 switch (l_ptr->state) {
498 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 switch (event) {
500 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 break;
503 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 if (l_ptr->next_in_no != l_ptr->checkpoint) {
505 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100506 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800507 tipc_link_proto_xmit(l_ptr, STATE_MSG,
508 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 l_ptr->fsm_msg_cnt++;
510 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800511 tipc_link_proto_xmit(l_ptr, STATE_MSG,
512 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 l_ptr->fsm_msg_cnt++;
514 }
515 link_set_timer(l_ptr, cont_intv);
516 break;
517 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 l_ptr->state = WORKING_UNKNOWN;
519 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800520 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 l_ptr->fsm_msg_cnt++;
522 link_set_timer(l_ptr, cont_intv / 4);
523 break;
524 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400525 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
526 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100527 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 l_ptr->state = RESET_RESET;
529 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800530 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
531 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 l_ptr->fsm_msg_cnt++;
533 link_set_timer(l_ptr, cont_intv);
534 break;
535 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400536 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 }
538 break;
539 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 switch (event) {
541 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 l_ptr->state = WORKING_WORKING;
544 l_ptr->fsm_msg_cnt = 0;
545 link_set_timer(l_ptr, cont_intv);
546 break;
547 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400548 pr_info("%s<%s>, requested by peer while probing\n",
549 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100550 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 l_ptr->state = RESET_RESET;
552 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800553 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
554 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 l_ptr->fsm_msg_cnt++;
556 link_set_timer(l_ptr, cont_intv);
557 break;
558 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 l_ptr->state = WORKING_WORKING;
561 l_ptr->fsm_msg_cnt = 0;
562 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100563 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800564 tipc_link_proto_xmit(l_ptr, STATE_MSG,
565 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 l_ptr->fsm_msg_cnt++;
567 }
568 link_set_timer(l_ptr, cont_intv);
569 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800570 tipc_link_proto_xmit(l_ptr, STATE_MSG,
571 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 l_ptr->fsm_msg_cnt++;
573 link_set_timer(l_ptr, cont_intv / 4);
574 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400575 pr_warn("%s<%s>, peer not responding\n",
576 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100577 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 l_ptr->state = RESET_UNKNOWN;
579 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800580 tipc_link_proto_xmit(l_ptr, RESET_MSG,
581 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 l_ptr->fsm_msg_cnt++;
583 link_set_timer(l_ptr, cont_intv);
584 }
585 break;
586 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400587 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 }
589 break;
590 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 switch (event) {
592 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 break;
594 case ACTIVATE_MSG:
595 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000596 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 l_ptr->state = WORKING_WORKING;
599 l_ptr->fsm_msg_cnt = 0;
600 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800601 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800603 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800604 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 link_set_timer(l_ptr, cont_intv);
606 break;
607 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 l_ptr->state = RESET_RESET;
609 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800610 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
611 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 l_ptr->fsm_msg_cnt++;
613 link_set_timer(l_ptr, cont_intv);
614 break;
615 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500616 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 /* fall through */
618 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800619 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 l_ptr->fsm_msg_cnt++;
621 link_set_timer(l_ptr, cont_intv);
622 break;
623 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400624 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 }
626 break;
627 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 switch (event) {
629 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 case ACTIVATE_MSG:
631 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000632 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 l_ptr->state = WORKING_WORKING;
635 l_ptr->fsm_msg_cnt = 0;
636 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800637 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800639 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800640 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 link_set_timer(l_ptr, cont_intv);
642 break;
643 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 break;
645 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800646 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
647 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 l_ptr->fsm_msg_cnt++;
649 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 break;
651 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400652 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 }
654 break;
655 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400656 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 }
658}
659
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500660/* tipc_link_cong: determine return value and how to treat the
661 * sent buffer during link congestion.
662 * - For plain, errorless user data messages we keep the buffer and
663 * return -ELINKONG.
664 * - For all other messages we discard the buffer and return -EHOSTUNREACH
665 * - For TIPC internal messages we also reset the link
666 */
667static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
668{
669 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500670 uint imp = tipc_msg_tot_importance(msg);
671 u32 oport = msg_tot_origport(msg);
672
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400673 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500674 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
675 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400676 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500677 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400678 if (unlikely(msg_errcode(msg)))
679 goto drop;
680 if (unlikely(msg_reroute_cnt(msg)))
681 goto drop;
682 if (TIPC_SKB_CB(buf)->wakeup_pending)
683 return -ELINKCONG;
684 if (link_schedule_user(link, oport, TIPC_SKB_CB(buf)->chain_sz, imp))
685 return -ELINKCONG;
686drop:
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500687 kfree_skb_list(buf);
688 return -EHOSTUNREACH;
689}
690
691/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400692 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500693 * @link: link to use
Ying Xue58dc55f2014-11-26 11:41:52 +0800694 * @skb: chain of buffers containing message
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500695 * Consumes the buffer chain, except when returning -ELINKCONG
696 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
697 * user data messages) or -EHOSTUNREACH (all other messages/senders)
698 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
699 * to act on the return value, since they may need to do more send attempts.
700 */
Ying Xue58dc55f2014-11-26 11:41:52 +0800701int __tipc_link_xmit(struct tipc_link *link, struct sk_buff *skb)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500702{
Ying Xue58dc55f2014-11-26 11:41:52 +0800703 struct tipc_msg *msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500704 uint psz = msg_size(msg);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500705 uint sndlim = link->queue_limit[0];
706 uint imp = tipc_msg_tot_importance(msg);
707 uint mtu = link->max_pkt;
708 uint ack = mod(link->next_in_no - 1);
709 uint seqno = link->next_out_no;
710 uint bc_last_in = link->owner->bclink.last_in;
711 struct tipc_media_addr *addr = &link->media_addr;
Ying Xue58dc55f2014-11-26 11:41:52 +0800712 struct sk_buff_head *outqueue = &link->outqueue;
713 struct sk_buff *next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500714
715 /* Match queue limits against msg importance: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800716 if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp]))
717 return tipc_link_cong(link, skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500718
719 /* Has valid packet limit been used ? */
720 if (unlikely(psz > mtu)) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800721 kfree_skb_list(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500722 return -EMSGSIZE;
723 }
724
725 /* Prepare each packet for sending, and add to outqueue: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800726 while (skb) {
727 next = skb->next;
728 msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500729 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
730 msg_set_bcast_ack(msg, bc_last_in);
731
Ying Xue58dc55f2014-11-26 11:41:52 +0800732 if (skb_queue_len(outqueue) < sndlim) {
733 __skb_queue_tail(outqueue, skb);
734 tipc_bearer_send(link->bearer_id, skb, addr);
735 link->next_out = NULL;
736 link->unacked_window = 0;
737 } else if (tipc_msg_bundle(outqueue, skb, mtu)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500738 link->stats.sent_bundled++;
Ying Xue58dc55f2014-11-26 11:41:52 +0800739 skb = next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500740 continue;
Ying Xue58dc55f2014-11-26 11:41:52 +0800741 } else if (tipc_msg_make_bundle(outqueue, skb, mtu,
742 link->addr)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500743 link->stats.sent_bundled++;
744 link->stats.sent_bundles++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500745 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800746 link->next_out = skb_peek_tail(outqueue);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500747 } else {
Ying Xue58dc55f2014-11-26 11:41:52 +0800748 __skb_queue_tail(outqueue, skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500749 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800750 link->next_out = skb;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500751 }
752 seqno++;
Ying Xue58dc55f2014-11-26 11:41:52 +0800753 skb = next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500754 }
755 link->next_out_no = seqno;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500756 return 0;
757}
758
759/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400760 * tipc_link_xmit() is the general link level function for message sending
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500761 * @buf: chain of buffers containing message
762 * @dsz: amount of user data to be sent
763 * @dnode: address of destination node
764 * @selector: a number used for deterministic link selection
765 * Consumes the buffer chain, except when returning -ELINKCONG
766 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
767 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400768int tipc_link_xmit(struct sk_buff *buf, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500769{
770 struct tipc_link *link = NULL;
771 struct tipc_node *node;
772 int rc = -EHOSTUNREACH;
773
774 node = tipc_node_find(dnode);
775 if (node) {
776 tipc_node_lock(node);
777 link = node->active_links[selector & 1];
778 if (link)
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400779 rc = __tipc_link_xmit(link, buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500780 tipc_node_unlock(node);
781 }
782
783 if (link)
784 return rc;
785
786 if (likely(in_own_node(dnode)))
787 return tipc_sk_rcv(buf);
788
789 kfree_skb_list(buf);
790 return rc;
791}
792
Jon Maloyc64f7a62012-11-16 13:51:31 +0800793/*
Ying Xue247f0f32014-02-18 16:06:46 +0800794 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800795 *
796 * Give a newly added peer node the sequence number where it should
797 * start receiving and acking broadcast packets.
798 *
799 * Called with node locked
800 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400801static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800802{
803 struct sk_buff *buf;
804 struct tipc_msg *msg;
805
806 buf = tipc_buf_acquire(INT_H_SIZE);
807 if (!buf)
808 return;
809
810 msg = buf_msg(buf);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400811 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
812 msg_set_last_bcast(msg, link->owner->bclink.acked);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400813 __tipc_link_xmit(link, buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800814}
815
816/*
Ying Xue247f0f32014-02-18 16:06:46 +0800817 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800818 * Receive the sequence number where we should start receiving and
819 * acking broadcast packets from a newly added peer node, and open
820 * up for reception of such packets.
821 *
822 * Called with node locked
823 */
Ying Xue247f0f32014-02-18 16:06:46 +0800824static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800825{
826 struct tipc_msg *msg = buf_msg(buf);
827
828 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
829 n->bclink.recv_permitted = true;
830 kfree_skb(buf);
831}
832
Ying Xue58dc55f2014-11-26 11:41:52 +0800833struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
834 const struct sk_buff *skb)
835{
836 if (skb_queue_is_last(list, skb))
837 return NULL;
838 return skb->next;
839}
840
Jon Maloyc64f7a62012-11-16 13:51:31 +0800841/*
Ying Xue47b4c9a2014-11-26 11:41:48 +0800842 * tipc_link_push_packets - push unsent packets to bearer
843 *
844 * Push out the unsent messages of a link where congestion
845 * has abated. Node is locked.
846 *
847 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 */
Ying Xue47b4c9a2014-11-26 11:41:48 +0800849void tipc_link_push_packets(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850{
Ying Xue58dc55f2014-11-26 11:41:52 +0800851 struct sk_buff_head *outqueue = &l_ptr->outqueue;
852 struct sk_buff *skb = l_ptr->next_out;
Ying Xue47b4c9a2014-11-26 11:41:48 +0800853 struct tipc_msg *msg;
854 u32 next, first;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855
Ying Xue58dc55f2014-11-26 11:41:52 +0800856 skb_queue_walk_from(outqueue, skb) {
Ying Xue47b4c9a2014-11-26 11:41:48 +0800857 msg = buf_msg(skb);
858 next = msg_seqno(msg);
Ying Xue58dc55f2014-11-26 11:41:52 +0800859 first = buf_seqno(skb_peek(outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860
861 if (mod(next - first) < l_ptr->queue_limit[0]) {
862 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900863 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +0800864 if (msg_user(msg) == MSG_BUNDLER)
Ying Xue58311d12014-11-26 11:41:49 +0800865 TIPC_SKB_CB(skb)->bundling = false;
Ying Xue47b4c9a2014-11-26 11:41:48 +0800866 tipc_bearer_send(l_ptr->bearer_id, skb,
867 &l_ptr->media_addr);
Ying Xue58dc55f2014-11-26 11:41:52 +0800868 l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
Ying Xue47b4c9a2014-11-26 11:41:48 +0800869 } else {
870 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 }
872 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873}
874
Ying Xue3f5a12b2014-05-05 08:56:17 +0800875void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700876{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700877 char addr_string[16];
878 u32 i;
879
Ying Xue3f5a12b2014-05-05 08:56:17 +0800880 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700881
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400882 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800883 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700884
885 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800886 if (node->links[i]) {
887 link_print(node->links[i], "Resetting link\n");
888 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700889 }
890 }
891
Ying Xue3f5a12b2014-05-05 08:56:17 +0800892 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700893}
894
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500895static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400896 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700897{
898 struct tipc_msg *msg = buf_msg(buf);
899
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400900 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700901
902 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700903 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000904 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700905 tipc_link_reset(l_ptr);
906
907 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700908 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700909 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700910 char addr_string[16];
911
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400912 pr_info("Msg seq number: %u, ", msg_seqno(msg));
913 pr_cont("Outstanding acks: %lu\n",
914 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700915
Allan Stephens01d83ed2011-01-18 13:53:16 -0500916 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700917 tipc_node_lock(n_ptr);
918
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000919 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400920 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800921 pr_info("Reception permitted: %d, Acked: %u\n",
922 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400923 n_ptr->bclink.acked);
924 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
925 n_ptr->bclink.last_in,
926 n_ptr->bclink.oos_state,
927 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700928
Allan Stephensd356eeb2006-06-25 23:40:01 -0700929 tipc_node_unlock(n_ptr);
930
Ying Xue3f5a12b2014-05-05 08:56:17 +0800931 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700932 l_ptr->stale_count = 0;
933 }
934}
935
Ying Xue58dc55f2014-11-26 11:41:52 +0800936void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
Per Liden4323add2006-01-18 00:38:21 +0100937 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938{
939 struct tipc_msg *msg;
940
Ying Xue58dc55f2014-11-26 11:41:52 +0800941 if (!skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700942 return;
943
Ying Xue58dc55f2014-11-26 11:41:52 +0800944 msg = buf_msg(skb);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900945
Erik Hugne512137e2013-12-06 10:08:00 -0500946 /* Detect repeated retransmit failures */
947 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
948 if (++l_ptr->stale_count > 100) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800949 link_retransmit_failure(l_ptr, skb);
Erik Hugne512137e2013-12-06 10:08:00 -0500950 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700951 }
952 } else {
Erik Hugne512137e2013-12-06 10:08:00 -0500953 l_ptr->last_retransmitted = msg_seqno(msg);
954 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 }
Allan Stephensd356eeb2006-06-25 23:40:01 -0700956
Ying Xue58dc55f2014-11-26 11:41:52 +0800957 skb_queue_walk_from(&l_ptr->outqueue, skb) {
958 if (!retransmits || skb == l_ptr->next_out)
959 break;
960 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900962 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue58dc55f2014-11-26 11:41:52 +0800963 tipc_bearer_send(l_ptr->bearer_id, skb, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800964 retransmits--;
965 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967}
968
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900969/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 * link_insert_deferred_queue - insert deferred messages back into receive chain
971 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500972static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 struct sk_buff *buf)
974{
Ying Xuebc6fecd2014-11-26 11:41:53 +0800975 struct sk_buff_head head;
976 struct sk_buff *skb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 u32 seq_no;
978
Ying Xuebc6fecd2014-11-26 11:41:53 +0800979 if (skb_queue_empty(&l_ptr->deferred_queue))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 return buf;
981
Ying Xuebc6fecd2014-11-26 11:41:53 +0800982 seq_no = buf_seqno(skb_peek(&l_ptr->deferred_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 if (seq_no == mod(l_ptr->next_in_no)) {
Ying Xuebc6fecd2014-11-26 11:41:53 +0800984 __skb_queue_head_init(&head);
985 skb_queue_splice_tail_init(&l_ptr->deferred_queue, &head);
986 skb = head.next;
987 skb->prev = NULL;
988 head.prev->next = buf;
989 head.prev->prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 }
Ying Xuebc6fecd2014-11-26 11:41:53 +0800991 return skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992}
993
Allan Stephens85035562008-04-15 19:04:54 -0700994/**
995 * link_recv_buf_validate - validate basic format of received message
996 *
997 * This routine ensures a TIPC message has an acceptable header, and at least
998 * as much data as the header indicates it should. The routine also ensures
999 * that the entire message header is stored in the main fragment of the message
1000 * buffer, to simplify future access to message header fields.
1001 *
1002 * Note: Having extra info present in the message header or data areas is OK.
1003 * TIPC will ignore the excess, under the assumption that it is optional info
1004 * introduced by a later release of the protocol.
1005 */
Allan Stephens85035562008-04-15 19:04:54 -07001006static int link_recv_buf_validate(struct sk_buff *buf)
1007{
1008 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001009 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001010 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1011 };
1012
1013 struct tipc_msg *msg;
1014 u32 tipc_hdr[2];
1015 u32 size;
1016 u32 hdr_size;
1017 u32 min_hdr_size;
1018
Erik Hugne64380a02014-02-11 11:38:26 +01001019 /* If this packet comes from the defer queue, the skb has already
1020 * been validated
1021 */
1022 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1023 return 1;
1024
Allan Stephens85035562008-04-15 19:04:54 -07001025 if (unlikely(buf->len < MIN_H_SIZE))
1026 return 0;
1027
1028 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1029 if (msg == NULL)
1030 return 0;
1031
1032 if (unlikely(msg_version(msg) != TIPC_VERSION))
1033 return 0;
1034
1035 size = msg_size(msg);
1036 hdr_size = msg_hdr_sz(msg);
1037 min_hdr_size = msg_isdata(msg) ?
1038 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1039
1040 if (unlikely((hdr_size < min_hdr_size) ||
1041 (size < hdr_size) ||
1042 (buf->len < size) ||
1043 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1044 return 0;
1045
1046 return pskb_may_pull(buf, hdr_size);
1047}
1048
Allan Stephensb02b69c2010-08-17 11:00:07 +00001049/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001050 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001051 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001052 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001053 *
1054 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1055 * structure (i.e. cannot be NULL), but bearer can be inactive.
1056 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001057void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001060 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001061 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 struct sk_buff *buf = head;
Ying Xue58dc55f2014-11-26 11:41:52 +08001063 struct sk_buff *skb1, *tmp;
Allan Stephens85035562008-04-15 19:04:54 -07001064 struct tipc_msg *msg;
1065 u32 seq_no;
1066 u32 ackd;
Ying Xue58dc55f2014-11-26 11:41:52 +08001067 u32 released;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001070 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001071
1072 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001073 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001074 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075
Allan Stephensfe13dda2008-04-15 19:03:23 -07001076 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001077 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001078 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001079
Allan Stephens85035562008-04-15 19:04:54 -07001080 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001081 msg = buf_msg(buf);
1082
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001084 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001085 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001086 else
Ying Xue247f0f32014-02-18 16:06:46 +08001087 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 continue;
1089 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001090
Allan Stephensed33a9c2011-04-05 15:15:04 -04001091 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001092 if (unlikely(!msg_short(msg) &&
1093 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001094 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001095
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001096 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001097 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001099 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001100 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001101
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001102 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001104 if (unlikely(!l_ptr))
1105 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001106
Allan Stephensb4b56102011-05-27 11:00:51 -04001107 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001108 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001109 msg_user(msg) == LINK_PROTOCOL &&
1110 (msg_type(msg) == RESET_MSG ||
1111 msg_type(msg) == ACTIVATE_MSG) &&
1112 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001113 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001114
Ying Xue10f465c2014-05-05 08:56:11 +08001115 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001116 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001117
Allan Stephens85035562008-04-15 19:04:54 -07001118 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001119 seq_no = msg_seqno(msg);
1120 ackd = msg_ack(msg);
1121
1122 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001123 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001124 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125
Ying Xue58dc55f2014-11-26 11:41:52 +08001126 released = 0;
1127 skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) {
1128 if (skb1 == l_ptr->next_out ||
1129 more(buf_seqno(skb1), ackd))
1130 break;
1131 __skb_unlink(skb1, &l_ptr->outqueue);
1132 kfree_skb(skb1);
1133 released = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 }
Allan Stephens85035562008-04-15 19:04:54 -07001135
1136 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 if (unlikely(l_ptr->next_out))
Ying Xue47b4c9a2014-11-26 11:41:48 +08001138 tipc_link_push_packets(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001139
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001140 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1141 link_prepare_wakeup(l_ptr);
1142 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1143 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001144
Jon Paul Maloya5377832014-02-13 17:29:15 -05001145 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001146 if (unlikely(!link_working_working(l_ptr))) {
1147 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001148 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001149 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001150 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 continue;
1152 }
Ying Xue3af390e2013-10-30 11:26:57 +08001153
1154 /* Traffic message. Conditionally activate link */
1155 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1156
1157 if (link_working_working(l_ptr)) {
1158 /* Re-insert buffer in front of queue */
1159 buf->next = head;
1160 head = buf;
1161 tipc_node_unlock(n_ptr);
1162 continue;
1163 }
1164 goto unlock_discard;
1165 }
1166
1167 /* Link is now in state WORKING_WORKING */
1168 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 link_handle_out_of_seq_msg(l_ptr, buf);
1170 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001171 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 continue;
1173 }
Ying Xue3af390e2013-10-30 11:26:57 +08001174 l_ptr->next_in_no++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001175 if (unlikely(!skb_queue_empty(&l_ptr->deferred_queue)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001177
Erik Hugne3f53bd82014-07-01 10:22:41 +02001178 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1179 l_ptr->stats.sent_acks++;
1180 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1181 }
1182
Erik Hugne7ae934b2014-07-01 10:22:40 +02001183 if (tipc_link_prepare_input(l_ptr, &buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001184 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 }
Per Liden4323add2006-01-18 00:38:21 +01001187 tipc_node_unlock(n_ptr);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001188 msg = buf_msg(buf);
1189 if (tipc_link_input(l_ptr, buf) != 0)
1190 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001191 continue;
1192unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001193 tipc_node_unlock(n_ptr);
1194discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001195 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197}
1198
Ben Hutchings2c530402012-07-10 10:55:09 +00001199/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001200 * tipc_link_prepare_input - process TIPC link messages
1201 *
1202 * returns nonzero if the message was consumed
1203 *
1204 * Node lock must be held
1205 */
1206static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1207{
1208 struct tipc_node *n;
1209 struct tipc_msg *msg;
1210 int res = -EINVAL;
1211
1212 n = l->owner;
1213 msg = buf_msg(*buf);
1214 switch (msg_user(msg)) {
1215 case CHANGEOVER_PROTOCOL:
1216 if (tipc_link_tunnel_rcv(n, buf))
1217 res = 0;
1218 break;
1219 case MSG_FRAGMENTER:
1220 l->stats.recv_fragments++;
1221 if (tipc_buf_append(&l->reasm_buf, buf)) {
1222 l->stats.recv_fragmented++;
1223 res = 0;
1224 } else if (!l->reasm_buf) {
1225 tipc_link_reset(l);
1226 }
1227 break;
1228 case MSG_BUNDLER:
1229 l->stats.recv_bundles++;
1230 l->stats.recv_bundled += msg_msgcnt(msg);
1231 res = 0;
1232 break;
1233 case NAME_DISTRIBUTOR:
1234 n->bclink.recv_permitted = true;
1235 res = 0;
1236 break;
1237 case BCAST_PROTOCOL:
1238 tipc_link_sync_rcv(n, *buf);
1239 break;
1240 default:
1241 res = 0;
1242 }
1243 return res;
1244}
1245/**
1246 * tipc_link_input - Deliver message too higher layers
1247 */
1248static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1249{
1250 struct tipc_msg *msg = buf_msg(buf);
1251 int res = 0;
1252
1253 switch (msg_user(msg)) {
1254 case TIPC_LOW_IMPORTANCE:
1255 case TIPC_MEDIUM_IMPORTANCE:
1256 case TIPC_HIGH_IMPORTANCE:
1257 case TIPC_CRITICAL_IMPORTANCE:
1258 case CONN_MANAGER:
1259 tipc_sk_rcv(buf);
1260 break;
1261 case NAME_DISTRIBUTOR:
1262 tipc_named_rcv(buf);
1263 break;
1264 case MSG_BUNDLER:
1265 tipc_link_bundle_rcv(buf);
1266 break;
1267 default:
1268 res = -EINVAL;
1269 }
1270 return res;
1271}
1272
1273/**
Allan Stephens8809b252011-10-25 10:44:35 -04001274 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1275 *
1276 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001278u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279{
Ying Xuebc6fecd2014-11-26 11:41:53 +08001280 struct sk_buff *skb1;
1281 u32 seq_no = buf_seqno(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282
1283 /* Empty queue ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001284 if (skb_queue_empty(list)) {
1285 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 return 1;
1287 }
1288
1289 /* Last ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001290 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1291 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 return 1;
1293 }
1294
Allan Stephens8809b252011-10-25 10:44:35 -04001295 /* Locate insertion point in queue, then insert; discard if duplicate */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001296 skb_queue_walk(list, skb1) {
1297 u32 curr_seqno = buf_seqno(skb1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298
Allan Stephens8809b252011-10-25 10:44:35 -04001299 if (seq_no == curr_seqno) {
Ying Xuebc6fecd2014-11-26 11:41:53 +08001300 kfree_skb(skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001301 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 }
Allan Stephens8809b252011-10-25 10:44:35 -04001303
1304 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 break;
Allan Stephens8809b252011-10-25 10:44:35 -04001306 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307
Ying Xuebc6fecd2014-11-26 11:41:53 +08001308 __skb_queue_before(list, skb1, skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001309 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310}
1311
Allan Stephens8809b252011-10-25 10:44:35 -04001312/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1314 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001315static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 struct sk_buff *buf)
1317{
Allan Stephensf9057302011-10-24 16:03:12 -04001318 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319
1320 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001321 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 return;
1323 }
1324
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 l_ptr->checkpoint--;
1327
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001328 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 * Discard packet if a duplicate; otherwise add it to deferred queue
1330 * and notify peer of gap as per protocol specification
1331 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 if (less(seq_no, mod(l_ptr->next_in_no))) {
1333 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001334 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 return;
1336 }
1337
Ying Xuebc6fecd2014-11-26 11:41:53 +08001338 if (tipc_link_defer_pkt(&l_ptr->deferred_queue, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001340 TIPC_SKB_CB(buf)->deferred = true;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001341 if ((skb_queue_len(&l_ptr->deferred_queue) % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001342 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001343 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 l_ptr->stats.duplicates++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001345 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346}
1347
1348/*
1349 * Send protocol message to the other endpoint.
1350 */
Ying Xue247f0f32014-02-18 16:06:46 +08001351void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1352 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001354 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001356 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001357 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358
Ying Xue77a7e072013-12-10 20:45:44 -08001359 /* Don't send protocol message during link changeover */
1360 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001362
1363 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001364 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001365 return;
1366
Allan Stephens92d2c902011-10-25 11:20:26 -04001367 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001369 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001370 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001371 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372
1373 if (msg_typ == STATE_MSG) {
1374 u32 next_sent = mod(l_ptr->next_out_no);
1375
Per Liden4323add2006-01-18 00:38:21 +01001376 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 return;
1378 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001379 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 msg_set_next_sent(msg, next_sent);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001381 if (!skb_queue_empty(&l_ptr->deferred_queue)) {
1382 u32 rec = buf_seqno(skb_peek(&l_ptr->deferred_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 gap = mod(rec - mod(l_ptr->next_in_no));
1384 }
1385 msg_set_seq_gap(msg, gap);
1386 if (gap)
1387 l_ptr->stats.sent_nacks++;
1388 msg_set_link_tolerance(msg, tolerance);
1389 msg_set_linkprio(msg, priority);
1390 msg_set_max_pkt(msg, ack_mtu);
1391 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1392 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001393 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 u32 mtu = l_ptr->max_pkt;
1395
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001396 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 link_working_working(l_ptr) &&
1398 l_ptr->fsm_msg_cnt) {
1399 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001400 if (l_ptr->max_pkt_probes == 10) {
1401 l_ptr->max_pkt_target = (msg_size - 4);
1402 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001404 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001406 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407
1408 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001409 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 l_ptr->stats.sent_states++;
1411 } else { /* RESET_MSG or ACTIVATE_MSG */
1412 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1413 msg_set_seq_gap(msg, 0);
1414 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001415 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 msg_set_link_tolerance(msg, l_ptr->tolerance);
1417 msg_set_linkprio(msg, l_ptr->priority);
1418 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1419 }
1420
Allan Stephens75f0aa42011-02-28 15:30:20 -05001421 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1422 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001424 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425
1426 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1427
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001428 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 if (!buf)
1430 return;
1431
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001432 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001433 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434
Ying Xue7a2f7d12014-04-21 10:55:46 +08001435 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001436 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001437 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438}
1439
1440/*
1441 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001442 * Note that network plane id propagates through the network, and may
1443 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 */
Ying Xue247f0f32014-02-18 16:06:46 +08001445static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446{
1447 u32 rec_gap = 0;
1448 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001449 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450 u32 msg_tol;
1451 struct tipc_msg *msg = buf_msg(buf);
1452
Ying Xue77a7e072013-12-10 20:45:44 -08001453 /* Discard protocol message during link changeover */
1454 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 goto exit;
1456
Ying Xue7a2f7d12014-04-21 10:55:46 +08001457 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001459 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001462
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001464 if (!link_working_unknown(l_ptr) &&
1465 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001466 if (less_eq(msg_session(msg), l_ptr->peer_session))
1467 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001469
1470 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1471 link_working_unknown(l_ptr))) {
1472 /*
1473 * peer has lost contact -- don't allow peer's links
1474 * to reactivate before we recognize loss & clean up
1475 */
Ying Xueca9cf062014-05-08 08:54:40 +08001476 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001477 }
1478
Allan Stephens47361c82011-10-26 10:55:16 -04001479 link_state_event(l_ptr, RESET_MSG);
1480
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481 /* fall thru' */
1482 case ACTIVATE_MSG:
1483 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1485
Allan Stephens2db99832010-12-31 18:59:33 +00001486 msg_tol = msg_link_tolerance(msg);
1487 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 link_set_supervision_props(l_ptr, msg_tol);
1489
1490 if (msg_linkprio(msg) > l_ptr->priority)
1491 l_ptr->priority = msg_linkprio(msg);
1492
1493 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001494 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 if (max_pkt_info < l_ptr->max_pkt_target)
1496 l_ptr->max_pkt_target = max_pkt_info;
1497 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1498 l_ptr->max_pkt = l_ptr->max_pkt_target;
1499 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001500 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502
Allan Stephens4d753132011-10-25 12:19:05 -04001503 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001504 if (!tipc_node_is_up(l_ptr->owner)) {
1505 l_ptr->owner->bclink.last_sent =
1506 l_ptr->owner->bclink.last_in =
1507 msg_last_bcast(msg);
1508 l_ptr->owner->bclink.oos_state = 0;
1509 }
Allan Stephens4d753132011-10-25 12:19:05 -04001510
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 l_ptr->peer_session = msg_session(msg);
1512 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001513
1514 if (msg_type(msg) == ACTIVATE_MSG)
1515 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 break;
1517 case STATE_MSG:
1518
Allan Stephens2db99832010-12-31 18:59:33 +00001519 msg_tol = msg_link_tolerance(msg);
1520 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001522
1523 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001525 pr_warn("%s<%s>, priority change %u->%u\n",
1526 link_rst_msg, l_ptr->name, l_ptr->priority,
1527 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001529 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 break;
1531 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001532
1533 /* Record reception; force mismatch at next timeout: */
1534 l_ptr->checkpoint--;
1535
Per Lidenb97bf3f2006-01-02 19:04:38 +01001536 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1537 l_ptr->stats.recv_states++;
1538 if (link_reset_unknown(l_ptr))
1539 break;
1540
1541 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001542 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001543 mod(l_ptr->next_in_no));
1544 }
1545
1546 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001547 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001548 l_ptr->max_pkt = max_pkt_ack;
1549 l_ptr->max_pkt_probes = 0;
1550 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551
1552 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001553 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001555 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001556 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001557 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558
1559 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001560 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001561 tipc_bclink_update_link_state(l_ptr->owner,
1562 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563
1564 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001565 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1566 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567 }
1568 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569 l_ptr->stats.recv_nacks++;
Ying Xue58dc55f2014-11-26 11:41:52 +08001570 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue),
Per Liden4323add2006-01-18 00:38:21 +01001571 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572 }
1573 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 }
1575exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001576 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577}
1578
1579
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001580/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1581 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001583static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1584 struct tipc_msg *tunnel_hdr,
1585 struct tipc_msg *msg,
1586 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001588 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589 struct sk_buff *buf;
1590 u32 length = msg_size(msg);
1591
1592 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001593 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001594 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001596 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001598 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001599 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001600 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001601 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001602 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001603 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1604 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001605 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606}
1607
1608
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001609/* tipc_link_failover_send_queue(): A link has gone down, but a second
1610 * link is still active. We can do failover. Tunnel the failing link's
1611 * whole send queue via the remaining link. This way, we don't lose
1612 * any packets, and sequence order is preserved for subsequent traffic
1613 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001615void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616{
Ying Xue58dc55f2014-11-26 11:41:52 +08001617 u32 msgcount = skb_queue_len(&l_ptr->outqueue);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001618 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 struct tipc_msg tunnel_hdr;
Ying Xue58dc55f2014-11-26 11:41:52 +08001620 struct sk_buff *skb;
Allan Stephens5392d642006-06-25 23:52:50 -07001621 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622
1623 if (!tunnel)
1624 return;
1625
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001626 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001627 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1629 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001630
Ying Xue58dc55f2014-11-26 11:41:52 +08001631 if (skb_queue_empty(&l_ptr->outqueue)) {
1632 skb = tipc_buf_acquire(INT_H_SIZE);
1633 if (skb) {
1634 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001636 __tipc_link_xmit(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001638 pr_warn("%sunable to send changeover msg\n",
1639 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 }
1641 return;
1642 }
Allan Stephensf1310722006-06-25 23:51:37 -07001643
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001644 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001645 l_ptr->owner->active_links[1]);
1646
Ying Xue58dc55f2014-11-26 11:41:52 +08001647 skb_queue_walk(&l_ptr->outqueue, skb) {
1648 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001649
1650 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001652 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653
Florian Westphald788d802007-08-02 19:28:06 -07001654 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001656 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001657 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1658 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 pos += align(msg_size(m));
1660 m = (struct tipc_msg *)pos;
1661 }
1662 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001663 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1664 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 }
1667}
1668
Ying Xue247f0f32014-02-18 16:06:46 +08001669/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001670 * duplicate of the first link's send queue via the new link. This way, we
1671 * are guaranteed that currently queued packets from a socket are delivered
1672 * before future traffic from the same socket, even if this is using the
1673 * new link. The last arriving copy of each duplicate packet is dropped at
1674 * the receiving end by the regular protocol check, so packet cardinality
1675 * and sequence order is preserved per sender/receiver socket pair.
1676 * Owner node is locked.
1677 */
Ying Xue247f0f32014-02-18 16:06:46 +08001678void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001679 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680{
Ying Xue58dc55f2014-11-26 11:41:52 +08001681 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 struct tipc_msg tunnel_hdr;
1683
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001684 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001685 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Ying Xue58dc55f2014-11-26 11:41:52 +08001686 msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
Ying Xue58dc55f2014-11-26 11:41:52 +08001688 skb_queue_walk(&l_ptr->outqueue, skb) {
1689 struct sk_buff *outskb;
1690 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 u32 length = msg_size(msg);
1692
1693 if (msg_user(msg) == MSG_BUNDLER)
1694 msg_set_type(msg, CLOSED_MSG);
1695 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001696 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001698 outskb = tipc_buf_acquire(length + INT_H_SIZE);
1699 if (outskb == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001700 pr_warn("%sunable to send duplicate msg\n",
1701 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 return;
1703 }
Ying Xue58dc55f2014-11-26 11:41:52 +08001704 skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE);
1705 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data,
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001706 length);
Ying Xue58dc55f2014-11-26 11:41:52 +08001707 __tipc_link_xmit(tunnel, outskb);
Per Liden4323add2006-01-18 00:38:21 +01001708 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 }
1711}
1712
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713/**
1714 * buf_extract - extracts embedded TIPC message from another message
1715 * @skb: encapsulating message buffer
1716 * @from_pos: offset to extract from
1717 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001718 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 * encapsulating message itself is left unchanged.
1720 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1722{
1723 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1724 u32 size = msg_size(msg);
1725 struct sk_buff *eb;
1726
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001727 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001729 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 return eb;
1731}
1732
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001733
1734
1735/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1736 * Owner node is locked.
1737 */
1738static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1739 struct sk_buff *t_buf)
1740{
1741 struct sk_buff *buf;
1742
1743 if (!tipc_link_is_up(l_ptr))
1744 return;
1745
1746 buf = buf_extract(t_buf, INT_H_SIZE);
1747 if (buf == NULL) {
1748 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1749 return;
1750 }
1751
1752 /* Add buffer to deferred queue, if applicable: */
1753 link_handle_out_of_seq_msg(l_ptr, buf);
1754}
1755
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001756/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1757 * Owner node is locked.
1758 */
1759static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1760 struct sk_buff *t_buf)
1761{
1762 struct tipc_msg *t_msg = buf_msg(t_buf);
1763 struct sk_buff *buf = NULL;
1764 struct tipc_msg *msg;
1765
1766 if (tipc_link_is_up(l_ptr))
1767 tipc_link_reset(l_ptr);
1768
1769 /* First failover packet? */
1770 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1771 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1772
1773 /* Should there be an inner packet? */
1774 if (l_ptr->exp_msg_count) {
1775 l_ptr->exp_msg_count--;
1776 buf = buf_extract(t_buf, INT_H_SIZE);
1777 if (buf == NULL) {
1778 pr_warn("%sno inner failover pkt\n", link_co_err);
1779 goto exit;
1780 }
1781 msg = buf_msg(buf);
1782
1783 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1784 kfree_skb(buf);
1785 buf = NULL;
1786 goto exit;
1787 }
1788 if (msg_user(msg) == MSG_FRAGMENTER) {
1789 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001790 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001791 }
1792 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001793exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001794 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1795 tipc_node_detach_link(l_ptr->owner, l_ptr);
1796 kfree(l_ptr);
1797 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001798 return buf;
1799}
1800
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001801/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001802 * via other link as result of a failover (ORIGINAL_MSG) or
1803 * a new active link (DUPLICATE_MSG). Failover packets are
1804 * returned to the active link for delivery upwards.
1805 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05001807static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001808 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001810 struct sk_buff *t_buf = *buf;
1811 struct tipc_link *l_ptr;
1812 struct tipc_msg *t_msg = buf_msg(t_buf);
1813 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001815 *buf = NULL;
1816
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001817 if (bearer_id >= MAX_BEARERS)
1818 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001819
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001820 l_ptr = n_ptr->links[bearer_id];
1821 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001824 if (msg_type(t_msg) == DUPLICATE_MSG)
1825 tipc_link_dup_rcv(l_ptr, t_buf);
1826 else if (msg_type(t_msg) == ORIGINAL_MSG)
1827 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001828 else
1829 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001831 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001832 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833}
1834
1835/*
1836 * Bundler functionality:
1837 */
Ying Xue247f0f32014-02-18 16:06:46 +08001838void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839{
1840 u32 msgcount = msg_msgcnt(buf_msg(buf));
1841 u32 pos = INT_H_SIZE;
1842 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001843 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845 while (msgcount--) {
1846 obuf = buf_extract(buf, pos);
1847 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001848 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001849 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001850 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001851 omsg = buf_msg(obuf);
1852 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001853 if (msg_isdata(omsg)) {
1854 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
1855 tipc_sk_mcast_rcv(obuf);
1856 else
1857 tipc_sk_rcv(obuf);
1858 } else if (msg_user(omsg) == CONN_MANAGER) {
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001859 tipc_sk_rcv(obuf);
1860 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1861 tipc_named_rcv(obuf);
1862 } else {
1863 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1864 kfree_skb(obuf);
1865 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001867 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868}
1869
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001870static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871{
Allan Stephens5413b4c2011-01-18 13:24:55 -05001872 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
1873 return;
1874
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875 l_ptr->tolerance = tolerance;
1876 l_ptr->continuity_interval =
1877 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
1878 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
1879}
1880
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001881void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882{
1883 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001884 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1885 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1886 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1887 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001889 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1890 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1891 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1892 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1895 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1896 /* FRAGMENT and LAST_FRAGMENT packets */
1897 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1898}
1899
Jon Paul Maloye099e862014-02-13 17:29:18 -05001900/* tipc_link_find_owner - locate owner node of link by link's name
1901 * @name: pointer to link name string
1902 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001903 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001904 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001906static struct tipc_node *tipc_link_find_owner(const char *link_name,
1907 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001908{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001909 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001910 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001911 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001912 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913
Jon Paul Maloye099e862014-02-13 17:29:18 -05001914 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001915 rcu_read_lock();
1916 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001917 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001918 for (i = 0; i < MAX_BEARERS; i++) {
1919 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001920 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1921 *bearer_id = i;
1922 found_node = n_ptr;
1923 break;
1924 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001925 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001926 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001927 if (found_node)
1928 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001929 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001930 rcu_read_unlock();
1931
Jon Paul Maloye099e862014-02-13 17:29:18 -05001932 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933}
1934
Allan Stephens5c216e12011-10-18 11:34:29 -04001935/**
1936 * link_value_is_valid -- validate proposed link tolerance/priority/window
1937 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001938 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1939 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04001940 *
1941 * Returns 1 if value is within range, 0 if not.
1942 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001943static int link_value_is_valid(u16 cmd, u32 new_value)
1944{
1945 switch (cmd) {
1946 case TIPC_CMD_SET_LINK_TOL:
1947 return (new_value >= TIPC_MIN_LINK_TOL) &&
1948 (new_value <= TIPC_MAX_LINK_TOL);
1949 case TIPC_CMD_SET_LINK_PRI:
1950 return (new_value <= TIPC_MAX_LINK_PRI);
1951 case TIPC_CMD_SET_LINK_WINDOW:
1952 return (new_value >= TIPC_MIN_LINK_WIN) &&
1953 (new_value <= TIPC_MAX_LINK_WIN);
1954 }
1955 return 0;
1956}
1957
Allan Stephens5c216e12011-10-18 11:34:29 -04001958/**
1959 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00001960 * @name: ptr to link, bearer, or media name
1961 * @new_value: new value of link, bearer, or media setting
1962 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04001963 *
Ying Xue7216cd92014-04-21 10:55:48 +08001964 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04001965 *
1966 * Returns 0 if value updated and negative value on error.
1967 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001968static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
1969{
1970 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001971 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04001972 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05001973 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001974 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02001975 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04001976
Jon Paul Maloye099e862014-02-13 17:29:18 -05001977 node = tipc_link_find_owner(name, &bearer_id);
1978 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04001979 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001980 l_ptr = node->links[bearer_id];
1981
1982 if (l_ptr) {
1983 switch (cmd) {
1984 case TIPC_CMD_SET_LINK_TOL:
1985 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08001986 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
1987 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001988 break;
1989 case TIPC_CMD_SET_LINK_PRI:
1990 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08001991 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
1992 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001993 break;
1994 case TIPC_CMD_SET_LINK_WINDOW:
1995 tipc_link_set_queue_limits(l_ptr, new_value);
1996 break;
1997 default:
1998 res = -EINVAL;
1999 break;
2000 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002001 }
2002 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002003 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002004 }
2005
2006 b_ptr = tipc_bearer_find(name);
2007 if (b_ptr) {
2008 switch (cmd) {
2009 case TIPC_CMD_SET_LINK_TOL:
2010 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002011 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002012 case TIPC_CMD_SET_LINK_PRI:
2013 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002014 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002015 case TIPC_CMD_SET_LINK_WINDOW:
2016 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002017 break;
2018 default:
2019 res = -EINVAL;
2020 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002021 }
Ying Xue636c0372013-10-18 07:23:20 +02002022 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002023 }
2024
2025 m_ptr = tipc_media_find(name);
2026 if (!m_ptr)
2027 return -ENODEV;
2028 switch (cmd) {
2029 case TIPC_CMD_SET_LINK_TOL:
2030 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002031 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002032 case TIPC_CMD_SET_LINK_PRI:
2033 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002034 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002035 case TIPC_CMD_SET_LINK_WINDOW:
2036 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002037 break;
2038 default:
2039 res = -EINVAL;
2040 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002041 }
Ying Xue636c0372013-10-18 07:23:20 +02002042 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002043}
2044
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002045struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002046 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002047{
2048 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002049 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002050 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051
2052 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002053 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002054
2055 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2056 new_value = ntohl(args->value);
2057
Allan Stephens5c216e12011-10-18 11:34:29 -04002058 if (!link_value_is_valid(cmd, new_value))
2059 return tipc_cfg_reply_error_string(
2060 "cannot change, value invalid");
2061
Per Liden4323add2006-01-18 00:38:21 +01002062 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002064 (tipc_bclink_set_queue_limits(new_value) == 0))
2065 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002066 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002067 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068 }
2069
Allan Stephens5c216e12011-10-18 11:34:29 -04002070 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002072 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073
Per Liden4323add2006-01-18 00:38:21 +01002074 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075}
2076
2077/**
2078 * link_reset_statistics - reset link statistics
2079 * @l_ptr: pointer to link
2080 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002081static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002082{
2083 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2084 l_ptr->stats.sent_info = l_ptr->next_out_no;
2085 l_ptr->stats.recv_info = l_ptr->next_in_no;
2086}
2087
Per Liden4323add2006-01-18 00:38:21 +01002088struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089{
2090 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002091 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002092 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002093 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094
2095 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002096 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097
2098 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002099 if (!strcmp(link_name, tipc_bclink_name)) {
2100 if (tipc_bclink_reset_stats())
2101 return tipc_cfg_reply_error_string("link not found");
2102 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002104 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002105 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002106 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002107
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002108 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002109 l_ptr = node->links[bearer_id];
2110 if (!l_ptr) {
2111 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002112 return tipc_cfg_reply_error_string("link not found");
2113 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002115 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002116 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117}
2118
2119/**
2120 * percent - convert count to a percentage of total (rounding up or down)
2121 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122static u32 percent(u32 count, u32 total)
2123{
2124 return (count * 100 + (total / 2)) / total;
2125}
2126
2127/**
Per Liden4323add2006-01-18 00:38:21 +01002128 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002129 * @name: link name
2130 * @buf: print buffer area
2131 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002132 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002133 * Returns length of print buffer data string (or 0 if error)
2134 */
Per Liden4323add2006-01-18 00:38:21 +01002135static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002136{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002137 struct tipc_link *l;
2138 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002139 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140 char *status;
2141 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002142 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002143 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002144
Per Liden4323add2006-01-18 00:38:21 +01002145 if (!strcmp(name, tipc_bclink_name))
2146 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147
Jon Paul Maloye099e862014-02-13 17:29:18 -05002148 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002149 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002151
Per Liden4323add2006-01-18 00:38:21 +01002152 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002153
2154 l = node->links[bearer_id];
2155 if (!l) {
2156 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002157 return 0;
2158 }
2159
Erik Hugnedc1aed32012-06-29 00:50:23 -04002160 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161
Erik Hugnedc1aed32012-06-29 00:50:23 -04002162 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002163 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002164 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002165 status = "STANDBY";
2166 else
2167 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002168
2169 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2170 " %s MTU:%u Priority:%u Tolerance:%u ms"
2171 " Window:%u packets\n",
2172 l->name, status, l->max_pkt, l->priority,
2173 l->tolerance, l->queue_limit[0]);
2174
2175 ret += tipc_snprintf(buf + ret, buf_size - ret,
2176 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2177 l->next_in_no - s->recv_info, s->recv_fragments,
2178 s->recv_fragmented, s->recv_bundles,
2179 s->recv_bundled);
2180
2181 ret += tipc_snprintf(buf + ret, buf_size - ret,
2182 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2183 l->next_out_no - s->sent_info, s->sent_fragments,
2184 s->sent_fragmented, s->sent_bundles,
2185 s->sent_bundled);
2186
2187 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188 if (!profile_total)
2189 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002190
2191 ret += tipc_snprintf(buf + ret, buf_size - ret,
2192 " TX profile sample:%u packets average:%u octets\n"
2193 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2194 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2195 s->msg_length_counts,
2196 s->msg_lengths_total / profile_total,
2197 percent(s->msg_length_profile[0], profile_total),
2198 percent(s->msg_length_profile[1], profile_total),
2199 percent(s->msg_length_profile[2], profile_total),
2200 percent(s->msg_length_profile[3], profile_total),
2201 percent(s->msg_length_profile[4], profile_total),
2202 percent(s->msg_length_profile[5], profile_total),
2203 percent(s->msg_length_profile[6], profile_total));
2204
2205 ret += tipc_snprintf(buf + ret, buf_size - ret,
2206 " RX states:%u probes:%u naks:%u defs:%u"
2207 " dups:%u\n", s->recv_states, s->recv_probes,
2208 s->recv_nacks, s->deferred_recv, s->duplicates);
2209
2210 ret += tipc_snprintf(buf + ret, buf_size - ret,
2211 " TX states:%u probes:%u naks:%u acks:%u"
2212 " dups:%u\n", s->sent_states, s->sent_probes,
2213 s->sent_nacks, s->sent_acks, s->retransmitted);
2214
2215 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002216 " Congestion link:%u Send queue"
2217 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002218 s->max_queue_sz, s->queue_sz_counts ?
2219 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002220
Per Liden4323add2006-01-18 00:38:21 +01002221 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002222 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002223}
2224
Per Liden4323add2006-01-18 00:38:21 +01002225struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002226{
2227 struct sk_buff *buf;
2228 struct tlv_desc *rep_tlv;
2229 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002230 int pb_len;
2231 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002232
2233 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002234 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002235
Erik Hugnedc1aed32012-06-29 00:50:23 -04002236 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002237 if (!buf)
2238 return NULL;
2239
2240 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002241 pb = TLV_DATA(rep_tlv);
2242 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002243 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002244 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002245 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002246 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002247 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002248 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002249 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002250 skb_put(buf, TLV_SPACE(str_len));
2251 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2252
2253 return buf;
2254}
2255
Per Lidenb97bf3f2006-01-02 19:04:38 +01002256/**
Per Liden4323add2006-01-18 00:38:21 +01002257 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002258 * @dest: network address of destination node
2259 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002260 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002261 * If no active link can be found, uses default maximum packet size.
2262 */
Per Liden4323add2006-01-18 00:38:21 +01002263u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002264{
David S. Miller6c000552008-09-02 23:38:32 -07002265 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002266 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002268
Per Lidenb97bf3f2006-01-02 19:04:38 +01002269 if (dest == tipc_own_addr)
2270 return MAX_MSG_SIZE;
2271
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002272 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002273 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002274 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002275 l_ptr = n_ptr->active_links[selector & 1];
2276 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002277 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002278 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002279 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002280 return res;
2281}
2282
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002283static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002284{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002285 struct tipc_bearer *b_ptr;
2286
2287 rcu_read_lock();
2288 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2289 if (b_ptr)
2290 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2291 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002292
Per Lidenb97bf3f2006-01-02 19:04:38 +01002293 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002294 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002295 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002296 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002297 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002298 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002299 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002300 pr_cont(":WW\n");
2301 else
2302 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002303}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002304
2305/* Parse and validate nested (link) properties valid for media, bearer and link
2306 */
2307int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2308{
2309 int err;
2310
2311 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2312 tipc_nl_prop_policy);
2313 if (err)
2314 return err;
2315
2316 if (props[TIPC_NLA_PROP_PRIO]) {
2317 u32 prio;
2318
2319 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2320 if (prio > TIPC_MAX_LINK_PRI)
2321 return -EINVAL;
2322 }
2323
2324 if (props[TIPC_NLA_PROP_TOL]) {
2325 u32 tol;
2326
2327 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2328 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2329 return -EINVAL;
2330 }
2331
2332 if (props[TIPC_NLA_PROP_WIN]) {
2333 u32 win;
2334
2335 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2336 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2337 return -EINVAL;
2338 }
2339
2340 return 0;
2341}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002342
Richard Alpef96ce7a2014-11-20 10:29:13 +01002343int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2344{
2345 int err;
2346 int res = 0;
2347 int bearer_id;
2348 char *name;
2349 struct tipc_link *link;
2350 struct tipc_node *node;
2351 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2352
2353 if (!info->attrs[TIPC_NLA_LINK])
2354 return -EINVAL;
2355
2356 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2357 info->attrs[TIPC_NLA_LINK],
2358 tipc_nl_link_policy);
2359 if (err)
2360 return err;
2361
2362 if (!attrs[TIPC_NLA_LINK_NAME])
2363 return -EINVAL;
2364
2365 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2366
2367 node = tipc_link_find_owner(name, &bearer_id);
2368 if (!node)
2369 return -EINVAL;
2370
2371 tipc_node_lock(node);
2372
2373 link = node->links[bearer_id];
2374 if (!link) {
2375 res = -EINVAL;
2376 goto out;
2377 }
2378
2379 if (attrs[TIPC_NLA_LINK_PROP]) {
2380 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2381
2382 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2383 props);
2384 if (err) {
2385 res = err;
2386 goto out;
2387 }
2388
2389 if (props[TIPC_NLA_PROP_TOL]) {
2390 u32 tol;
2391
2392 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2393 link_set_supervision_props(link, tol);
2394 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2395 }
2396 if (props[TIPC_NLA_PROP_PRIO]) {
2397 u32 prio;
2398
2399 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2400 link->priority = prio;
2401 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2402 }
2403 if (props[TIPC_NLA_PROP_WIN]) {
2404 u32 win;
2405
2406 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2407 tipc_link_set_queue_limits(link, win);
2408 }
2409 }
2410
2411out:
2412 tipc_node_unlock(node);
2413
2414 return res;
2415}
Richard Alped8182802014-11-24 11:10:29 +01002416
2417static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002418{
2419 int i;
2420 struct nlattr *stats;
2421
2422 struct nla_map {
2423 u32 key;
2424 u32 val;
2425 };
2426
2427 struct nla_map map[] = {
2428 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2429 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2430 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2431 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2432 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2433 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2434 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2435 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2436 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2437 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2438 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2439 s->msg_length_counts : 1},
2440 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2441 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2442 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2443 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2444 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2445 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2446 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2447 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2448 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2449 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2450 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2451 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2452 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2453 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2454 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2455 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2456 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2457 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2458 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2459 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2460 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2461 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2462 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2463 };
2464
2465 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2466 if (!stats)
2467 return -EMSGSIZE;
2468
2469 for (i = 0; i < ARRAY_SIZE(map); i++)
2470 if (nla_put_u32(skb, map[i].key, map[i].val))
2471 goto msg_full;
2472
2473 nla_nest_end(skb, stats);
2474
2475 return 0;
2476msg_full:
2477 nla_nest_cancel(skb, stats);
2478
2479 return -EMSGSIZE;
2480}
2481
2482/* Caller should hold appropriate locks to protect the link */
Richard Alped8182802014-11-24 11:10:29 +01002483static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002484{
2485 int err;
2486 void *hdr;
2487 struct nlattr *attrs;
2488 struct nlattr *prop;
2489
2490 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2491 NLM_F_MULTI, TIPC_NL_LINK_GET);
2492 if (!hdr)
2493 return -EMSGSIZE;
2494
2495 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2496 if (!attrs)
2497 goto msg_full;
2498
2499 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2500 goto attr_msg_full;
2501 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2502 tipc_cluster_mask(tipc_own_addr)))
2503 goto attr_msg_full;
2504 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2505 goto attr_msg_full;
2506 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2507 goto attr_msg_full;
2508 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2509 goto attr_msg_full;
2510
2511 if (tipc_link_is_up(link))
2512 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2513 goto attr_msg_full;
2514 if (tipc_link_is_active(link))
2515 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2516 goto attr_msg_full;
2517
2518 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2519 if (!prop)
2520 goto attr_msg_full;
2521 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2522 goto prop_msg_full;
2523 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2524 goto prop_msg_full;
2525 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2526 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2527 goto prop_msg_full;
2528 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2529 goto prop_msg_full;
2530 nla_nest_end(msg->skb, prop);
2531
2532 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2533 if (err)
2534 goto attr_msg_full;
2535
2536 nla_nest_end(msg->skb, attrs);
2537 genlmsg_end(msg->skb, hdr);
2538
2539 return 0;
2540
2541prop_msg_full:
2542 nla_nest_cancel(msg->skb, prop);
2543attr_msg_full:
2544 nla_nest_cancel(msg->skb, attrs);
2545msg_full:
2546 genlmsg_cancel(msg->skb, hdr);
2547
2548 return -EMSGSIZE;
2549}
2550
2551/* Caller should hold node lock */
Richard Alped8182802014-11-24 11:10:29 +01002552static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
2553 struct tipc_node *node,
2554 u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002555{
2556 u32 i;
2557 int err;
2558
2559 for (i = *prev_link; i < MAX_BEARERS; i++) {
2560 *prev_link = i;
2561
2562 if (!node->links[i])
2563 continue;
2564
2565 err = __tipc_nl_add_link(msg, node->links[i]);
2566 if (err)
2567 return err;
2568 }
2569 *prev_link = 0;
2570
2571 return 0;
2572}
2573
2574int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2575{
2576 struct tipc_node *node;
2577 struct tipc_nl_msg msg;
2578 u32 prev_node = cb->args[0];
2579 u32 prev_link = cb->args[1];
2580 int done = cb->args[2];
2581 int err;
2582
2583 if (done)
2584 return 0;
2585
2586 msg.skb = skb;
2587 msg.portid = NETLINK_CB(cb->skb).portid;
2588 msg.seq = cb->nlh->nlmsg_seq;
2589
2590 rcu_read_lock();
2591
2592 if (prev_node) {
2593 node = tipc_node_find(prev_node);
2594 if (!node) {
2595 /* We never set seq or call nl_dump_check_consistent()
2596 * this means that setting prev_seq here will cause the
2597 * consistence check to fail in the netlink callback
2598 * handler. Resulting in the last NLMSG_DONE message
2599 * having the NLM_F_DUMP_INTR flag set.
2600 */
2601 cb->prev_seq = 1;
2602 goto out;
2603 }
2604
2605 list_for_each_entry_continue_rcu(node, &tipc_node_list, list) {
2606 tipc_node_lock(node);
2607 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2608 tipc_node_unlock(node);
2609 if (err)
2610 goto out;
2611
2612 prev_node = node->addr;
2613 }
2614 } else {
2615 err = tipc_nl_add_bc_link(&msg);
2616 if (err)
2617 goto out;
2618
2619 list_for_each_entry_rcu(node, &tipc_node_list, list) {
2620 tipc_node_lock(node);
2621 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2622 tipc_node_unlock(node);
2623 if (err)
2624 goto out;
2625
2626 prev_node = node->addr;
2627 }
2628 }
2629 done = 1;
2630out:
2631 rcu_read_unlock();
2632
2633 cb->args[0] = prev_node;
2634 cb->args[1] = prev_link;
2635 cb->args[2] = done;
2636
2637 return skb->len;
2638}
2639
2640int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2641{
2642 struct sk_buff *ans_skb;
2643 struct tipc_nl_msg msg;
2644 struct tipc_link *link;
2645 struct tipc_node *node;
2646 char *name;
2647 int bearer_id;
2648 int err;
2649
2650 if (!info->attrs[TIPC_NLA_LINK_NAME])
2651 return -EINVAL;
2652
2653 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
2654 node = tipc_link_find_owner(name, &bearer_id);
2655 if (!node)
2656 return -EINVAL;
2657
2658 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2659 if (!ans_skb)
2660 return -ENOMEM;
2661
2662 msg.skb = ans_skb;
2663 msg.portid = info->snd_portid;
2664 msg.seq = info->snd_seq;
2665
2666 tipc_node_lock(node);
2667 link = node->links[bearer_id];
2668 if (!link) {
2669 err = -EINVAL;
2670 goto err_out;
2671 }
2672
2673 err = __tipc_nl_add_link(&msg, link);
2674 if (err)
2675 goto err_out;
2676
2677 tipc_node_unlock(node);
2678
2679 return genlmsg_reply(ans_skb, info);
2680
2681err_out:
2682 tipc_node_unlock(node);
2683 nlmsg_free(ans_skb);
2684
2685 return err;
2686}
Richard Alpeae363422014-11-20 10:29:14 +01002687
2688int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2689{
2690 int err;
2691 char *link_name;
2692 unsigned int bearer_id;
2693 struct tipc_link *link;
2694 struct tipc_node *node;
2695 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2696
2697 if (!info->attrs[TIPC_NLA_LINK])
2698 return -EINVAL;
2699
2700 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2701 info->attrs[TIPC_NLA_LINK],
2702 tipc_nl_link_policy);
2703 if (err)
2704 return err;
2705
2706 if (!attrs[TIPC_NLA_LINK_NAME])
2707 return -EINVAL;
2708
2709 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2710
2711 if (strcmp(link_name, tipc_bclink_name) == 0) {
2712 err = tipc_bclink_reset_stats();
2713 if (err)
2714 return err;
2715 return 0;
2716 }
2717
2718 node = tipc_link_find_owner(link_name, &bearer_id);
2719 if (!node)
2720 return -EINVAL;
2721
2722 tipc_node_lock(node);
2723
2724 link = node->links[bearer_id];
2725 if (!link) {
2726 tipc_node_unlock(node);
2727 return -EINVAL;
2728 }
2729
2730 link_reset_statistics(link);
2731
2732 tipc_node_unlock(node);
2733
2734 return 0;
2735}