blob: 9e94bf935e48f4713e9345f288038f66295e83dd [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 Stephensf882cb72011-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);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400295 __skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296
297 link_reset_statistics(l_ptr);
298
Allan Stephens37b9c082011-02-28 11:32:27 -0500299 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500301 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
302 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500303
304 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 return l_ptr;
307}
308
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500309void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500310{
311 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500312 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500313
Ying Xue6c7a7622014-03-27 12:54:37 +0800314 rcu_read_lock();
315 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800316 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500317 l_ptr = n_ptr->links[bearer_id];
318 if (l_ptr) {
319 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500320 if (shutting_down || !tipc_node_is_up(n_ptr)) {
321 tipc_node_detach_link(l_ptr->owner, l_ptr);
322 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800323 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500324
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500325 /* Nobody else can access this link now: */
326 del_timer_sync(&l_ptr->timer);
327 kfree(l_ptr);
328 } else {
329 /* Detach/delete when failover is finished: */
330 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800331 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500332 del_timer_sync(&l_ptr->timer);
333 }
Ying Xuec61dd612014-02-13 17:29:09 -0500334 continue;
335 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800336 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500337 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800338 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500339}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340
341/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400342 * link_schedule_user - schedule user for wakeup after congestion
343 * @link: congested link
344 * @oport: sending port
345 * @chain_sz: size of buffer chain that was attempted sent
346 * @imp: importance of message attempted sent
347 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400349static bool link_schedule_user(struct tipc_link *link, u32 oport,
350 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400352 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400354 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
355 tipc_own_addr, oport, 0, 0);
356 if (!buf)
357 return false;
358 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
359 TIPC_SKB_CB(buf)->chain_imp = imp;
360 __skb_queue_tail(&link->waiting_sks, buf);
361 link->stats.link_congs++;
362 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363}
364
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400365/**
366 * link_prepare_wakeup - prepare users for wakeup after congestion
367 * @link: congested link
368 * Move a number of waiting users, as permitted by available space in
369 * the send queue, from link wait queue to node wait queue for wakeup
370 */
371static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372{
Ying Xue58dc55f2014-11-26 11:41:52 +0800373 uint pend_qsz = skb_queue_len(&link->outqueue);
Ying Xue58d78b32014-11-26 11:41:51 +0800374 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375
Ying Xue58d78b32014-11-26 11:41:51 +0800376 skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
377 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 break;
Ying Xue58d78b32014-11-26 11:41:51 +0800379 pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
380 __skb_unlink(skb, &link->waiting_sks);
381 __skb_queue_tail(&link->owner->waiting_sks, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383}
384
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900385/**
Per Liden4323add2006-01-18 00:38:21 +0100386 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 * @l_ptr: pointer to link
388 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500389void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400391 kfree_skb(l_ptr->reasm_buf);
392 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393}
394
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900395/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500396 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 * @l_ptr: pointer to link
398 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500399void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400{
Ying Xued77b3832013-12-10 20:45:38 -0800401 kfree_skb_list(l_ptr->oldest_deferred_in);
Ying Xue58dc55f2014-11-26 11:41:52 +0800402 __skb_queue_purge(&l_ptr->outqueue);
Per Liden4323add2006-01-18 00:38:21 +0100403 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404}
405
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500406void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 u32 prev_state = l_ptr->state;
409 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700410 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400411 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900412
Allan Stephensa686e682008-06-04 17:29:39 -0700413 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414
Allan Stephensa686e682008-06-04 17:29:39 -0700415 /* Link is down, accept any session */
416 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900418 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900420
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422
423 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
424 return;
425
Per Liden4323add2006-01-18 00:38:21 +0100426 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800427 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000428
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500429 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 l_ptr->reset_checkpoint = checkpoint;
431 l_ptr->exp_msg_count = START_CHANGEOVER;
432 }
433
434 /* Clean up all queues: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800435 __skb_queue_purge(&l_ptr->outqueue);
Ying Xued77b3832013-12-10 20:45:38 -0800436 kfree_skb_list(l_ptr->oldest_deferred_in);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400437 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
438 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
439 owner->action_flags |= TIPC_WAKEUP_USERS;
440 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 l_ptr->next_out = NULL;
442 l_ptr->unacked_window = 0;
443 l_ptr->checkpoint = 1;
444 l_ptr->next_out_no = 1;
445 l_ptr->deferred_inqueue_sz = 0;
446 l_ptr->oldest_deferred_in = NULL;
447 l_ptr->newest_deferred_in = NULL;
448 l_ptr->fsm_msg_cnt = 0;
449 l_ptr->stale_count = 0;
450 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451}
452
Ying Xuec61dd612014-02-13 17:29:09 -0500453void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500454{
455 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500456 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500457
Ying Xue6c7a7622014-03-27 12:54:37 +0800458 rcu_read_lock();
459 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800460 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500461 l_ptr = n_ptr->links[bearer_id];
462 if (l_ptr)
463 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800464 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500465 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800466 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500467}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500469static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470{
Allan Stephens5392d642006-06-25 23:52:50 -0700471 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100472 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800473 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474}
475
476/**
477 * link_state_event - link finite state machine
478 * @l_ptr: pointer to link
479 * @event: state machine event to process
480 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000481static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500483 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 u32 cont_intv = l_ptr->continuity_interval;
485
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500486 if (l_ptr->flags & LINK_STOPPED)
487 return;
488
Ying Xue135daee2014-02-13 17:29:08 -0500489 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490 return; /* Not yet. */
491
Ying Xue77a7e072013-12-10 20:45:44 -0800492 /* Check whether changeover is going on */
493 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000494 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800496 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
499 switch (l_ptr->state) {
500 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 switch (event) {
502 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 break;
505 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 if (l_ptr->next_in_no != l_ptr->checkpoint) {
507 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100508 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800509 tipc_link_proto_xmit(l_ptr, STATE_MSG,
510 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 l_ptr->fsm_msg_cnt++;
512 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800513 tipc_link_proto_xmit(l_ptr, STATE_MSG,
514 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 l_ptr->fsm_msg_cnt++;
516 }
517 link_set_timer(l_ptr, cont_intv);
518 break;
519 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 l_ptr->state = WORKING_UNKNOWN;
521 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800522 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 l_ptr->fsm_msg_cnt++;
524 link_set_timer(l_ptr, cont_intv / 4);
525 break;
526 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400527 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
528 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100529 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 l_ptr->state = RESET_RESET;
531 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800532 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
533 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 l_ptr->fsm_msg_cnt++;
535 link_set_timer(l_ptr, cont_intv);
536 break;
537 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400538 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 }
540 break;
541 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 switch (event) {
543 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 l_ptr->state = WORKING_WORKING;
546 l_ptr->fsm_msg_cnt = 0;
547 link_set_timer(l_ptr, cont_intv);
548 break;
549 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400550 pr_info("%s<%s>, requested by peer while probing\n",
551 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100552 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 l_ptr->state = RESET_RESET;
554 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800555 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
556 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 l_ptr->fsm_msg_cnt++;
558 link_set_timer(l_ptr, cont_intv);
559 break;
560 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 l_ptr->state = WORKING_WORKING;
563 l_ptr->fsm_msg_cnt = 0;
564 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100565 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800566 tipc_link_proto_xmit(l_ptr, STATE_MSG,
567 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 l_ptr->fsm_msg_cnt++;
569 }
570 link_set_timer(l_ptr, cont_intv);
571 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800572 tipc_link_proto_xmit(l_ptr, STATE_MSG,
573 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 l_ptr->fsm_msg_cnt++;
575 link_set_timer(l_ptr, cont_intv / 4);
576 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400577 pr_warn("%s<%s>, peer not responding\n",
578 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100579 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 l_ptr->state = RESET_UNKNOWN;
581 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800582 tipc_link_proto_xmit(l_ptr, RESET_MSG,
583 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 l_ptr->fsm_msg_cnt++;
585 link_set_timer(l_ptr, cont_intv);
586 }
587 break;
588 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400589 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 }
591 break;
592 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 switch (event) {
594 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 break;
596 case ACTIVATE_MSG:
597 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000598 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 l_ptr->state = WORKING_WORKING;
601 l_ptr->fsm_msg_cnt = 0;
602 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800603 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800605 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800606 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 link_set_timer(l_ptr, cont_intv);
608 break;
609 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 l_ptr->state = RESET_RESET;
611 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800612 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
613 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 l_ptr->fsm_msg_cnt++;
615 link_set_timer(l_ptr, cont_intv);
616 break;
617 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500618 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 /* fall through */
620 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800621 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 l_ptr->fsm_msg_cnt++;
623 link_set_timer(l_ptr, cont_intv);
624 break;
625 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400626 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 }
628 break;
629 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 switch (event) {
631 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 case ACTIVATE_MSG:
633 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000634 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 l_ptr->state = WORKING_WORKING;
637 l_ptr->fsm_msg_cnt = 0;
638 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800639 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800641 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800642 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 link_set_timer(l_ptr, cont_intv);
644 break;
645 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 break;
647 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800648 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
649 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 l_ptr->fsm_msg_cnt++;
651 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 break;
653 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400654 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 }
656 break;
657 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400658 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 }
660}
661
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500662/* tipc_link_cong: determine return value and how to treat the
663 * sent buffer during link congestion.
664 * - For plain, errorless user data messages we keep the buffer and
665 * return -ELINKONG.
666 * - For all other messages we discard the buffer and return -EHOSTUNREACH
667 * - For TIPC internal messages we also reset the link
668 */
669static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
670{
671 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500672 uint imp = tipc_msg_tot_importance(msg);
673 u32 oport = msg_tot_origport(msg);
674
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400675 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500676 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
677 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400678 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500679 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400680 if (unlikely(msg_errcode(msg)))
681 goto drop;
682 if (unlikely(msg_reroute_cnt(msg)))
683 goto drop;
684 if (TIPC_SKB_CB(buf)->wakeup_pending)
685 return -ELINKCONG;
686 if (link_schedule_user(link, oport, TIPC_SKB_CB(buf)->chain_sz, imp))
687 return -ELINKCONG;
688drop:
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500689 kfree_skb_list(buf);
690 return -EHOSTUNREACH;
691}
692
693/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400694 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500695 * @link: link to use
Ying Xue58dc55f2014-11-26 11:41:52 +0800696 * @skb: chain of buffers containing message
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500697 * Consumes the buffer chain, except when returning -ELINKCONG
698 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
699 * user data messages) or -EHOSTUNREACH (all other messages/senders)
700 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
701 * to act on the return value, since they may need to do more send attempts.
702 */
Ying Xue58dc55f2014-11-26 11:41:52 +0800703int __tipc_link_xmit(struct tipc_link *link, struct sk_buff *skb)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500704{
Ying Xue58dc55f2014-11-26 11:41:52 +0800705 struct tipc_msg *msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500706 uint psz = msg_size(msg);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500707 uint sndlim = link->queue_limit[0];
708 uint imp = tipc_msg_tot_importance(msg);
709 uint mtu = link->max_pkt;
710 uint ack = mod(link->next_in_no - 1);
711 uint seqno = link->next_out_no;
712 uint bc_last_in = link->owner->bclink.last_in;
713 struct tipc_media_addr *addr = &link->media_addr;
Ying Xue58dc55f2014-11-26 11:41:52 +0800714 struct sk_buff_head *outqueue = &link->outqueue;
715 struct sk_buff *next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500716
717 /* Match queue limits against msg importance: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800718 if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp]))
719 return tipc_link_cong(link, skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500720
721 /* Has valid packet limit been used ? */
722 if (unlikely(psz > mtu)) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800723 kfree_skb_list(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500724 return -EMSGSIZE;
725 }
726
727 /* Prepare each packet for sending, and add to outqueue: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800728 while (skb) {
729 next = skb->next;
730 msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500731 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
732 msg_set_bcast_ack(msg, bc_last_in);
733
Ying Xue58dc55f2014-11-26 11:41:52 +0800734 if (skb_queue_len(outqueue) < sndlim) {
735 __skb_queue_tail(outqueue, skb);
736 tipc_bearer_send(link->bearer_id, skb, addr);
737 link->next_out = NULL;
738 link->unacked_window = 0;
739 } else if (tipc_msg_bundle(outqueue, skb, mtu)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500740 link->stats.sent_bundled++;
Ying Xue58dc55f2014-11-26 11:41:52 +0800741 skb = next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500742 continue;
Ying Xue58dc55f2014-11-26 11:41:52 +0800743 } else if (tipc_msg_make_bundle(outqueue, skb, mtu,
744 link->addr)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500745 link->stats.sent_bundled++;
746 link->stats.sent_bundles++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500747 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800748 link->next_out = skb_peek_tail(outqueue);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500749 } else {
Ying Xue58dc55f2014-11-26 11:41:52 +0800750 __skb_queue_tail(outqueue, skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500751 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800752 link->next_out = skb;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500753 }
754 seqno++;
Ying Xue58dc55f2014-11-26 11:41:52 +0800755 skb = next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500756 }
757 link->next_out_no = seqno;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500758 return 0;
759}
760
761/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400762 * tipc_link_xmit() is the general link level function for message sending
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500763 * @buf: chain of buffers containing message
764 * @dsz: amount of user data to be sent
765 * @dnode: address of destination node
766 * @selector: a number used for deterministic link selection
767 * Consumes the buffer chain, except when returning -ELINKCONG
768 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
769 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400770int tipc_link_xmit(struct sk_buff *buf, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500771{
772 struct tipc_link *link = NULL;
773 struct tipc_node *node;
774 int rc = -EHOSTUNREACH;
775
776 node = tipc_node_find(dnode);
777 if (node) {
778 tipc_node_lock(node);
779 link = node->active_links[selector & 1];
780 if (link)
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400781 rc = __tipc_link_xmit(link, buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500782 tipc_node_unlock(node);
783 }
784
785 if (link)
786 return rc;
787
788 if (likely(in_own_node(dnode)))
789 return tipc_sk_rcv(buf);
790
791 kfree_skb_list(buf);
792 return rc;
793}
794
Jon Maloyc64f7a62012-11-16 13:51:31 +0800795/*
Ying Xue247f0f32014-02-18 16:06:46 +0800796 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800797 *
798 * Give a newly added peer node the sequence number where it should
799 * start receiving and acking broadcast packets.
800 *
801 * Called with node locked
802 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400803static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800804{
805 struct sk_buff *buf;
806 struct tipc_msg *msg;
807
808 buf = tipc_buf_acquire(INT_H_SIZE);
809 if (!buf)
810 return;
811
812 msg = buf_msg(buf);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400813 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
814 msg_set_last_bcast(msg, link->owner->bclink.acked);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400815 __tipc_link_xmit(link, buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800816}
817
818/*
Ying Xue247f0f32014-02-18 16:06:46 +0800819 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800820 * Receive the sequence number where we should start receiving and
821 * acking broadcast packets from a newly added peer node, and open
822 * up for reception of such packets.
823 *
824 * Called with node locked
825 */
Ying Xue247f0f32014-02-18 16:06:46 +0800826static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800827{
828 struct tipc_msg *msg = buf_msg(buf);
829
830 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
831 n->bclink.recv_permitted = true;
832 kfree_skb(buf);
833}
834
Ying Xue58dc55f2014-11-26 11:41:52 +0800835struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
836 const struct sk_buff *skb)
837{
838 if (skb_queue_is_last(list, skb))
839 return NULL;
840 return skb->next;
841}
842
Jon Maloyc64f7a62012-11-16 13:51:31 +0800843/*
Ying Xue47b4c9a2014-11-26 11:41:48 +0800844 * tipc_link_push_packets - push unsent packets to bearer
845 *
846 * Push out the unsent messages of a link where congestion
847 * has abated. Node is locked.
848 *
849 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 */
Ying Xue47b4c9a2014-11-26 11:41:48 +0800851void tipc_link_push_packets(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852{
Ying Xue58dc55f2014-11-26 11:41:52 +0800853 struct sk_buff_head *outqueue = &l_ptr->outqueue;
854 struct sk_buff *skb = l_ptr->next_out;
Ying Xue47b4c9a2014-11-26 11:41:48 +0800855 struct tipc_msg *msg;
856 u32 next, first;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857
Ying Xue58dc55f2014-11-26 11:41:52 +0800858 skb_queue_walk_from(outqueue, skb) {
Ying Xue47b4c9a2014-11-26 11:41:48 +0800859 msg = buf_msg(skb);
860 next = msg_seqno(msg);
Ying Xue58dc55f2014-11-26 11:41:52 +0800861 first = buf_seqno(skb_peek(outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862
863 if (mod(next - first) < l_ptr->queue_limit[0]) {
864 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900865 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +0800866 if (msg_user(msg) == MSG_BUNDLER)
Ying Xue58311d12014-11-26 11:41:49 +0800867 TIPC_SKB_CB(skb)->bundling = false;
Ying Xue47b4c9a2014-11-26 11:41:48 +0800868 tipc_bearer_send(l_ptr->bearer_id, skb,
869 &l_ptr->media_addr);
Ying Xue58dc55f2014-11-26 11:41:52 +0800870 l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
Ying Xue47b4c9a2014-11-26 11:41:48 +0800871 } else {
872 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 }
874 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875}
876
Ying Xue3f5a12b2014-05-05 08:56:17 +0800877void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700878{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700879 char addr_string[16];
880 u32 i;
881
Ying Xue3f5a12b2014-05-05 08:56:17 +0800882 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700883
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400884 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800885 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700886
887 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800888 if (node->links[i]) {
889 link_print(node->links[i], "Resetting link\n");
890 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700891 }
892 }
893
Ying Xue3f5a12b2014-05-05 08:56:17 +0800894 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700895}
896
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500897static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400898 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700899{
900 struct tipc_msg *msg = buf_msg(buf);
901
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400902 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700903
904 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700905 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000906 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700907 tipc_link_reset(l_ptr);
908
909 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700910 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700911 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700912 char addr_string[16];
913
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400914 pr_info("Msg seq number: %u, ", msg_seqno(msg));
915 pr_cont("Outstanding acks: %lu\n",
916 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700917
Allan Stephens01d83ed2011-01-18 13:53:16 -0500918 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700919 tipc_node_lock(n_ptr);
920
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000921 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400922 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800923 pr_info("Reception permitted: %d, Acked: %u\n",
924 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400925 n_ptr->bclink.acked);
926 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
927 n_ptr->bclink.last_in,
928 n_ptr->bclink.oos_state,
929 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700930
Allan Stephensd356eeb2006-06-25 23:40:01 -0700931 tipc_node_unlock(n_ptr);
932
Ying Xue3f5a12b2014-05-05 08:56:17 +0800933 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700934 l_ptr->stale_count = 0;
935 }
936}
937
Ying Xue58dc55f2014-11-26 11:41:52 +0800938void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
Per Liden4323add2006-01-18 00:38:21 +0100939 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940{
941 struct tipc_msg *msg;
942
Ying Xue58dc55f2014-11-26 11:41:52 +0800943 if (!skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700944 return;
945
Ying Xue58dc55f2014-11-26 11:41:52 +0800946 msg = buf_msg(skb);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900947
Erik Hugne512137e2013-12-06 10:08:00 -0500948 /* Detect repeated retransmit failures */
949 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
950 if (++l_ptr->stale_count > 100) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800951 link_retransmit_failure(l_ptr, skb);
Erik Hugne512137e2013-12-06 10:08:00 -0500952 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700953 }
954 } else {
Erik Hugne512137e2013-12-06 10:08:00 -0500955 l_ptr->last_retransmitted = msg_seqno(msg);
956 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957 }
Allan Stephensd356eeb2006-06-25 23:40:01 -0700958
Ying Xue58dc55f2014-11-26 11:41:52 +0800959 skb_queue_walk_from(&l_ptr->outqueue, skb) {
960 if (!retransmits || skb == l_ptr->next_out)
961 break;
962 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900964 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue58dc55f2014-11-26 11:41:52 +0800965 tipc_bearer_send(l_ptr->bearer_id, skb, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800966 retransmits--;
967 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969}
970
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900971/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 * link_insert_deferred_queue - insert deferred messages back into receive chain
973 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500974static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 struct sk_buff *buf)
976{
977 u32 seq_no;
978
979 if (l_ptr->oldest_deferred_in == NULL)
980 return buf;
981
Allan Stephensf9057302011-10-24 16:03:12 -0400982 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 if (seq_no == mod(l_ptr->next_in_no)) {
984 l_ptr->newest_deferred_in->next = buf;
985 buf = l_ptr->oldest_deferred_in;
986 l_ptr->oldest_deferred_in = NULL;
987 l_ptr->deferred_inqueue_sz = 0;
988 }
989 return buf;
990}
991
Allan Stephens85035562008-04-15 19:04:54 -0700992/**
993 * link_recv_buf_validate - validate basic format of received message
994 *
995 * This routine ensures a TIPC message has an acceptable header, and at least
996 * as much data as the header indicates it should. The routine also ensures
997 * that the entire message header is stored in the main fragment of the message
998 * buffer, to simplify future access to message header fields.
999 *
1000 * Note: Having extra info present in the message header or data areas is OK.
1001 * TIPC will ignore the excess, under the assumption that it is optional info
1002 * introduced by a later release of the protocol.
1003 */
Allan Stephens85035562008-04-15 19:04:54 -07001004static int link_recv_buf_validate(struct sk_buff *buf)
1005{
1006 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001007 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001008 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1009 };
1010
1011 struct tipc_msg *msg;
1012 u32 tipc_hdr[2];
1013 u32 size;
1014 u32 hdr_size;
1015 u32 min_hdr_size;
1016
Erik Hugne64380a02014-02-11 11:38:26 +01001017 /* If this packet comes from the defer queue, the skb has already
1018 * been validated
1019 */
1020 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1021 return 1;
1022
Allan Stephens85035562008-04-15 19:04:54 -07001023 if (unlikely(buf->len < MIN_H_SIZE))
1024 return 0;
1025
1026 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1027 if (msg == NULL)
1028 return 0;
1029
1030 if (unlikely(msg_version(msg) != TIPC_VERSION))
1031 return 0;
1032
1033 size = msg_size(msg);
1034 hdr_size = msg_hdr_sz(msg);
1035 min_hdr_size = msg_isdata(msg) ?
1036 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1037
1038 if (unlikely((hdr_size < min_hdr_size) ||
1039 (size < hdr_size) ||
1040 (buf->len < size) ||
1041 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1042 return 0;
1043
1044 return pskb_may_pull(buf, hdr_size);
1045}
1046
Allan Stephensb02b69c2010-08-17 11:00:07 +00001047/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001048 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001049 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001050 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001051 *
1052 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1053 * structure (i.e. cannot be NULL), but bearer can be inactive.
1054 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001055void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001058 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001059 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 struct sk_buff *buf = head;
Ying Xue58dc55f2014-11-26 11:41:52 +08001061 struct sk_buff *skb1, *tmp;
Allan Stephens85035562008-04-15 19:04:54 -07001062 struct tipc_msg *msg;
1063 u32 seq_no;
1064 u32 ackd;
Ying Xue58dc55f2014-11-26 11:41:52 +08001065 u32 released;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001068 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001069
1070 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001071 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001072 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073
Allan Stephensfe13dda2008-04-15 19:03:23 -07001074 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001075 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001076 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001077
Allan Stephens85035562008-04-15 19:04:54 -07001078 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001079 msg = buf_msg(buf);
1080
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001082 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001083 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001084 else
Ying Xue247f0f32014-02-18 16:06:46 +08001085 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 continue;
1087 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001088
Allan Stephensed33a9c2011-04-05 15:15:04 -04001089 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001090 if (unlikely(!msg_short(msg) &&
1091 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001092 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001093
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001094 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001095 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001097 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001098 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001099
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001100 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001102 if (unlikely(!l_ptr))
1103 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001104
Allan Stephensb4b56102011-05-27 11:00:51 -04001105 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001106 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001107 msg_user(msg) == LINK_PROTOCOL &&
1108 (msg_type(msg) == RESET_MSG ||
1109 msg_type(msg) == ACTIVATE_MSG) &&
1110 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001111 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001112
Ying Xue10f465c2014-05-05 08:56:11 +08001113 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001114 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001115
Allan Stephens85035562008-04-15 19:04:54 -07001116 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001117 seq_no = msg_seqno(msg);
1118 ackd = msg_ack(msg);
1119
1120 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001121 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001122 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123
Ying Xue58dc55f2014-11-26 11:41:52 +08001124 released = 0;
1125 skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) {
1126 if (skb1 == l_ptr->next_out ||
1127 more(buf_seqno(skb1), ackd))
1128 break;
1129 __skb_unlink(skb1, &l_ptr->outqueue);
1130 kfree_skb(skb1);
1131 released = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 }
Allan Stephens85035562008-04-15 19:04:54 -07001133
1134 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 if (unlikely(l_ptr->next_out))
Ying Xue47b4c9a2014-11-26 11:41:48 +08001136 tipc_link_push_packets(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001137
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001138 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1139 link_prepare_wakeup(l_ptr);
1140 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1141 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001142
Jon Paul Maloya5377832014-02-13 17:29:15 -05001143 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001144 if (unlikely(!link_working_working(l_ptr))) {
1145 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001146 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001147 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001148 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 continue;
1150 }
Ying Xue3af390e2013-10-30 11:26:57 +08001151
1152 /* Traffic message. Conditionally activate link */
1153 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1154
1155 if (link_working_working(l_ptr)) {
1156 /* Re-insert buffer in front of queue */
1157 buf->next = head;
1158 head = buf;
1159 tipc_node_unlock(n_ptr);
1160 continue;
1161 }
1162 goto unlock_discard;
1163 }
1164
1165 /* Link is now in state WORKING_WORKING */
1166 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 link_handle_out_of_seq_msg(l_ptr, buf);
1168 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001169 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 continue;
1171 }
Ying Xue3af390e2013-10-30 11:26:57 +08001172 l_ptr->next_in_no++;
1173 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001175
Erik Hugne3f53bd82014-07-01 10:22:41 +02001176 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1177 l_ptr->stats.sent_acks++;
1178 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1179 }
1180
Erik Hugne7ae934b2014-07-01 10:22:40 +02001181 if (tipc_link_prepare_input(l_ptr, &buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001182 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 }
Per Liden4323add2006-01-18 00:38:21 +01001185 tipc_node_unlock(n_ptr);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001186 msg = buf_msg(buf);
1187 if (tipc_link_input(l_ptr, buf) != 0)
1188 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001189 continue;
1190unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001191 tipc_node_unlock(n_ptr);
1192discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001193 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195}
1196
Ben Hutchings2c530402012-07-10 10:55:09 +00001197/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001198 * tipc_link_prepare_input - process TIPC link messages
1199 *
1200 * returns nonzero if the message was consumed
1201 *
1202 * Node lock must be held
1203 */
1204static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1205{
1206 struct tipc_node *n;
1207 struct tipc_msg *msg;
1208 int res = -EINVAL;
1209
1210 n = l->owner;
1211 msg = buf_msg(*buf);
1212 switch (msg_user(msg)) {
1213 case CHANGEOVER_PROTOCOL:
1214 if (tipc_link_tunnel_rcv(n, buf))
1215 res = 0;
1216 break;
1217 case MSG_FRAGMENTER:
1218 l->stats.recv_fragments++;
1219 if (tipc_buf_append(&l->reasm_buf, buf)) {
1220 l->stats.recv_fragmented++;
1221 res = 0;
1222 } else if (!l->reasm_buf) {
1223 tipc_link_reset(l);
1224 }
1225 break;
1226 case MSG_BUNDLER:
1227 l->stats.recv_bundles++;
1228 l->stats.recv_bundled += msg_msgcnt(msg);
1229 res = 0;
1230 break;
1231 case NAME_DISTRIBUTOR:
1232 n->bclink.recv_permitted = true;
1233 res = 0;
1234 break;
1235 case BCAST_PROTOCOL:
1236 tipc_link_sync_rcv(n, *buf);
1237 break;
1238 default:
1239 res = 0;
1240 }
1241 return res;
1242}
1243/**
1244 * tipc_link_input - Deliver message too higher layers
1245 */
1246static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1247{
1248 struct tipc_msg *msg = buf_msg(buf);
1249 int res = 0;
1250
1251 switch (msg_user(msg)) {
1252 case TIPC_LOW_IMPORTANCE:
1253 case TIPC_MEDIUM_IMPORTANCE:
1254 case TIPC_HIGH_IMPORTANCE:
1255 case TIPC_CRITICAL_IMPORTANCE:
1256 case CONN_MANAGER:
1257 tipc_sk_rcv(buf);
1258 break;
1259 case NAME_DISTRIBUTOR:
1260 tipc_named_rcv(buf);
1261 break;
1262 case MSG_BUNDLER:
1263 tipc_link_bundle_rcv(buf);
1264 break;
1265 default:
1266 res = -EINVAL;
1267 }
1268 return res;
1269}
1270
1271/**
Allan Stephens8809b252011-10-25 10:44:35 -04001272 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1273 *
1274 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275 */
Allan Stephens8809b252011-10-25 10:44:35 -04001276u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001277 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278{
Allan Stephens8809b252011-10-25 10:44:35 -04001279 struct sk_buff *queue_buf;
1280 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001281 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282
1283 buf->next = NULL;
1284
1285 /* Empty queue ? */
1286 if (*head == NULL) {
1287 *head = *tail = buf;
1288 return 1;
1289 }
1290
1291 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001292 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 (*tail)->next = buf;
1294 *tail = buf;
1295 return 1;
1296 }
1297
Allan Stephens8809b252011-10-25 10:44:35 -04001298 /* Locate insertion point in queue, then insert; discard if duplicate */
1299 prev = head;
1300 queue_buf = *head;
1301 for (;;) {
1302 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303
Allan Stephens8809b252011-10-25 10:44:35 -04001304 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001305 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001306 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 }
Allan Stephens8809b252011-10-25 10:44:35 -04001308
1309 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311
Allan Stephens8809b252011-10-25 10:44:35 -04001312 prev = &queue_buf->next;
1313 queue_buf = queue_buf->next;
1314 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315
Allan Stephens8809b252011-10-25 10:44:35 -04001316 buf->next = queue_buf;
1317 *prev = buf;
1318 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319}
1320
Allan Stephens8809b252011-10-25 10:44:35 -04001321/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1323 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001324static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 struct sk_buff *buf)
1326{
Allan Stephensf9057302011-10-24 16:03:12 -04001327 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328
1329 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001330 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 return;
1332 }
1333
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 l_ptr->checkpoint--;
1336
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001337 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 * Discard packet if a duplicate; otherwise add it to deferred queue
1339 * and notify peer of gap as per protocol specification
1340 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 if (less(seq_no, mod(l_ptr->next_in_no))) {
1342 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001343 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 return;
1345 }
1346
Per Liden4323add2006-01-18 00:38:21 +01001347 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1348 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 l_ptr->deferred_inqueue_sz++;
1350 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001351 TIPC_SKB_CB(buf)->deferred = true;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001353 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 } else
1355 l_ptr->stats.duplicates++;
1356}
1357
1358/*
1359 * Send protocol message to the other endpoint.
1360 */
Ying Xue247f0f32014-02-18 16:06:46 +08001361void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1362 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001364 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001366 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001367 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368
Ying Xue77a7e072013-12-10 20:45:44 -08001369 /* Don't send protocol message during link changeover */
1370 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001372
1373 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001374 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001375 return;
1376
Allan Stephens92d2c902011-10-25 11:20:26 -04001377 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001379 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001380 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001381 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382
1383 if (msg_typ == STATE_MSG) {
1384 u32 next_sent = mod(l_ptr->next_out_no);
1385
Per Liden4323add2006-01-18 00:38:21 +01001386 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 return;
1388 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001389 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 msg_set_next_sent(msg, next_sent);
1391 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001392 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 gap = mod(rec - mod(l_ptr->next_in_no));
1394 }
1395 msg_set_seq_gap(msg, gap);
1396 if (gap)
1397 l_ptr->stats.sent_nacks++;
1398 msg_set_link_tolerance(msg, tolerance);
1399 msg_set_linkprio(msg, priority);
1400 msg_set_max_pkt(msg, ack_mtu);
1401 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1402 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001403 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 u32 mtu = l_ptr->max_pkt;
1405
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001406 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 link_working_working(l_ptr) &&
1408 l_ptr->fsm_msg_cnt) {
1409 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001410 if (l_ptr->max_pkt_probes == 10) {
1411 l_ptr->max_pkt_target = (msg_size - 4);
1412 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001414 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001416 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417
1418 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001419 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 l_ptr->stats.sent_states++;
1421 } else { /* RESET_MSG or ACTIVATE_MSG */
1422 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1423 msg_set_seq_gap(msg, 0);
1424 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001425 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 msg_set_link_tolerance(msg, l_ptr->tolerance);
1427 msg_set_linkprio(msg, l_ptr->priority);
1428 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1429 }
1430
Allan Stephens75f0aa42011-02-28 15:30:20 -05001431 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1432 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001434 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435
1436 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1437
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001438 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 if (!buf)
1440 return;
1441
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001442 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001443 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444
Ying Xue7a2f7d12014-04-21 10:55:46 +08001445 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001446 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001447 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448}
1449
1450/*
1451 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001452 * Note that network plane id propagates through the network, and may
1453 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 */
Ying Xue247f0f32014-02-18 16:06:46 +08001455static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456{
1457 u32 rec_gap = 0;
1458 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001459 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 u32 msg_tol;
1461 struct tipc_msg *msg = buf_msg(buf);
1462
Ying Xue77a7e072013-12-10 20:45:44 -08001463 /* Discard protocol message during link changeover */
1464 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465 goto exit;
1466
Ying Xue7a2f7d12014-04-21 10:55:46 +08001467 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001469 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001472
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001474 if (!link_working_unknown(l_ptr) &&
1475 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001476 if (less_eq(msg_session(msg), l_ptr->peer_session))
1477 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001479
1480 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1481 link_working_unknown(l_ptr))) {
1482 /*
1483 * peer has lost contact -- don't allow peer's links
1484 * to reactivate before we recognize loss & clean up
1485 */
Ying Xueca9cf062014-05-08 08:54:40 +08001486 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001487 }
1488
Allan Stephens47361c82011-10-26 10:55:16 -04001489 link_state_event(l_ptr, RESET_MSG);
1490
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491 /* fall thru' */
1492 case ACTIVATE_MSG:
1493 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1495
Allan Stephens2db99832010-12-31 18:59:33 +00001496 msg_tol = msg_link_tolerance(msg);
1497 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 link_set_supervision_props(l_ptr, msg_tol);
1499
1500 if (msg_linkprio(msg) > l_ptr->priority)
1501 l_ptr->priority = msg_linkprio(msg);
1502
1503 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001504 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 if (max_pkt_info < l_ptr->max_pkt_target)
1506 l_ptr->max_pkt_target = max_pkt_info;
1507 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1508 l_ptr->max_pkt = l_ptr->max_pkt_target;
1509 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001510 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512
Allan Stephens4d753132011-10-25 12:19:05 -04001513 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001514 if (!tipc_node_is_up(l_ptr->owner)) {
1515 l_ptr->owner->bclink.last_sent =
1516 l_ptr->owner->bclink.last_in =
1517 msg_last_bcast(msg);
1518 l_ptr->owner->bclink.oos_state = 0;
1519 }
Allan Stephens4d753132011-10-25 12:19:05 -04001520
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 l_ptr->peer_session = msg_session(msg);
1522 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001523
1524 if (msg_type(msg) == ACTIVATE_MSG)
1525 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526 break;
1527 case STATE_MSG:
1528
Allan Stephens2db99832010-12-31 18:59:33 +00001529 msg_tol = msg_link_tolerance(msg);
1530 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001531 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001532
1533 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001535 pr_warn("%s<%s>, priority change %u->%u\n",
1536 link_rst_msg, l_ptr->name, l_ptr->priority,
1537 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001539 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540 break;
1541 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001542
1543 /* Record reception; force mismatch at next timeout: */
1544 l_ptr->checkpoint--;
1545
Per Lidenb97bf3f2006-01-02 19:04:38 +01001546 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1547 l_ptr->stats.recv_states++;
1548 if (link_reset_unknown(l_ptr))
1549 break;
1550
1551 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001552 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 mod(l_ptr->next_in_no));
1554 }
1555
1556 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001557 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001558 l_ptr->max_pkt = max_pkt_ack;
1559 l_ptr->max_pkt_probes = 0;
1560 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561
1562 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001563 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001564 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001565 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001566 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001567 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568
1569 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001570 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001571 tipc_bclink_update_link_state(l_ptr->owner,
1572 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573
1574 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001575 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1576 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 }
1578 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579 l_ptr->stats.recv_nacks++;
Ying Xue58dc55f2014-11-26 11:41:52 +08001580 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue),
Per Liden4323add2006-01-18 00:38:21 +01001581 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582 }
1583 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584 }
1585exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001586 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587}
1588
1589
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001590/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1591 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001592 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001593static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1594 struct tipc_msg *tunnel_hdr,
1595 struct tipc_msg *msg,
1596 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001598 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599 struct sk_buff *buf;
1600 u32 length = msg_size(msg);
1601
1602 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001603 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001604 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001606 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001608 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001609 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001610 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001611 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001612 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001613 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1614 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001615 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616}
1617
1618
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001619/* tipc_link_failover_send_queue(): A link has gone down, but a second
1620 * link is still active. We can do failover. Tunnel the failing link's
1621 * whole send queue via the remaining link. This way, we don't lose
1622 * any packets, and sequence order is preserved for subsequent traffic
1623 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001625void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001626{
Ying Xue58dc55f2014-11-26 11:41:52 +08001627 u32 msgcount = skb_queue_len(&l_ptr->outqueue);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001628 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001629 struct tipc_msg tunnel_hdr;
Ying Xue58dc55f2014-11-26 11:41:52 +08001630 struct sk_buff *skb;
Allan Stephens5392d642006-06-25 23:52:50 -07001631 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632
1633 if (!tunnel)
1634 return;
1635
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001636 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001637 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1639 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001640
Ying Xue58dc55f2014-11-26 11:41:52 +08001641 if (skb_queue_empty(&l_ptr->outqueue)) {
1642 skb = tipc_buf_acquire(INT_H_SIZE);
1643 if (skb) {
1644 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001646 __tipc_link_xmit(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001648 pr_warn("%sunable to send changeover msg\n",
1649 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001650 }
1651 return;
1652 }
Allan Stephensf1310722006-06-25 23:51:37 -07001653
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001654 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001655 l_ptr->owner->active_links[1]);
1656
Ying Xue58dc55f2014-11-26 11:41:52 +08001657 skb_queue_walk(&l_ptr->outqueue, skb) {
1658 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659
1660 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001662 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663
Florian Westphald788d802007-08-02 19:28:06 -07001664 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001666 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001667 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1668 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 pos += align(msg_size(m));
1670 m = (struct tipc_msg *)pos;
1671 }
1672 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001673 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1674 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 }
1677}
1678
Ying Xue247f0f32014-02-18 16:06:46 +08001679/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001680 * duplicate of the first link's send queue via the new link. This way, we
1681 * are guaranteed that currently queued packets from a socket are delivered
1682 * before future traffic from the same socket, even if this is using the
1683 * new link. The last arriving copy of each duplicate packet is dropped at
1684 * the receiving end by the regular protocol check, so packet cardinality
1685 * and sequence order is preserved per sender/receiver socket pair.
1686 * Owner node is locked.
1687 */
Ying Xue247f0f32014-02-18 16:06:46 +08001688void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001689 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690{
Ying Xue58dc55f2014-11-26 11:41:52 +08001691 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 struct tipc_msg tunnel_hdr;
1693
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001694 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001695 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Ying Xue58dc55f2014-11-26 11:41:52 +08001696 msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
Ying Xue58dc55f2014-11-26 11:41:52 +08001698 skb_queue_walk(&l_ptr->outqueue, skb) {
1699 struct sk_buff *outskb;
1700 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 u32 length = msg_size(msg);
1702
1703 if (msg_user(msg) == MSG_BUNDLER)
1704 msg_set_type(msg, CLOSED_MSG);
1705 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001706 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001708 outskb = tipc_buf_acquire(length + INT_H_SIZE);
1709 if (outskb == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001710 pr_warn("%sunable to send duplicate msg\n",
1711 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 return;
1713 }
Ying Xue58dc55f2014-11-26 11:41:52 +08001714 skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE);
1715 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data,
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001716 length);
Ying Xue58dc55f2014-11-26 11:41:52 +08001717 __tipc_link_xmit(tunnel, outskb);
Per Liden4323add2006-01-18 00:38:21 +01001718 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 }
1721}
1722
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723/**
1724 * buf_extract - extracts embedded TIPC message from another message
1725 * @skb: encapsulating message buffer
1726 * @from_pos: offset to extract from
1727 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001728 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729 * encapsulating message itself is left unchanged.
1730 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1732{
1733 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1734 u32 size = msg_size(msg);
1735 struct sk_buff *eb;
1736
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001737 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001739 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 return eb;
1741}
1742
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001743
1744
1745/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1746 * Owner node is locked.
1747 */
1748static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1749 struct sk_buff *t_buf)
1750{
1751 struct sk_buff *buf;
1752
1753 if (!tipc_link_is_up(l_ptr))
1754 return;
1755
1756 buf = buf_extract(t_buf, INT_H_SIZE);
1757 if (buf == NULL) {
1758 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1759 return;
1760 }
1761
1762 /* Add buffer to deferred queue, if applicable: */
1763 link_handle_out_of_seq_msg(l_ptr, buf);
1764}
1765
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001766/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1767 * Owner node is locked.
1768 */
1769static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1770 struct sk_buff *t_buf)
1771{
1772 struct tipc_msg *t_msg = buf_msg(t_buf);
1773 struct sk_buff *buf = NULL;
1774 struct tipc_msg *msg;
1775
1776 if (tipc_link_is_up(l_ptr))
1777 tipc_link_reset(l_ptr);
1778
1779 /* First failover packet? */
1780 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1781 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1782
1783 /* Should there be an inner packet? */
1784 if (l_ptr->exp_msg_count) {
1785 l_ptr->exp_msg_count--;
1786 buf = buf_extract(t_buf, INT_H_SIZE);
1787 if (buf == NULL) {
1788 pr_warn("%sno inner failover pkt\n", link_co_err);
1789 goto exit;
1790 }
1791 msg = buf_msg(buf);
1792
1793 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1794 kfree_skb(buf);
1795 buf = NULL;
1796 goto exit;
1797 }
1798 if (msg_user(msg) == MSG_FRAGMENTER) {
1799 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001800 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001801 }
1802 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001803exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001804 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1805 tipc_node_detach_link(l_ptr->owner, l_ptr);
1806 kfree(l_ptr);
1807 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001808 return buf;
1809}
1810
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001811/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001812 * via other link as result of a failover (ORIGINAL_MSG) or
1813 * a new active link (DUPLICATE_MSG). Failover packets are
1814 * returned to the active link for delivery upwards.
1815 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05001817static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001818 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001820 struct sk_buff *t_buf = *buf;
1821 struct tipc_link *l_ptr;
1822 struct tipc_msg *t_msg = buf_msg(t_buf);
1823 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001824
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001825 *buf = NULL;
1826
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001827 if (bearer_id >= MAX_BEARERS)
1828 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001829
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001830 l_ptr = n_ptr->links[bearer_id];
1831 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001834 if (msg_type(t_msg) == DUPLICATE_MSG)
1835 tipc_link_dup_rcv(l_ptr, t_buf);
1836 else if (msg_type(t_msg) == ORIGINAL_MSG)
1837 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001838 else
1839 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001841 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001842 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843}
1844
1845/*
1846 * Bundler functionality:
1847 */
Ying Xue247f0f32014-02-18 16:06:46 +08001848void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849{
1850 u32 msgcount = msg_msgcnt(buf_msg(buf));
1851 u32 pos = INT_H_SIZE;
1852 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001853 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 while (msgcount--) {
1856 obuf = buf_extract(buf, pos);
1857 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001858 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001859 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001860 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001861 omsg = buf_msg(obuf);
1862 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001863 if (msg_isdata(omsg)) {
1864 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
1865 tipc_sk_mcast_rcv(obuf);
1866 else
1867 tipc_sk_rcv(obuf);
1868 } else if (msg_user(omsg) == CONN_MANAGER) {
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001869 tipc_sk_rcv(obuf);
1870 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1871 tipc_named_rcv(obuf);
1872 } else {
1873 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1874 kfree_skb(obuf);
1875 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001877 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878}
1879
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001880static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881{
Allan Stephens5413b4c2011-01-18 13:24:55 -05001882 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
1883 return;
1884
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 l_ptr->tolerance = tolerance;
1886 l_ptr->continuity_interval =
1887 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
1888 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
1889}
1890
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001891void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892{
1893 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001894 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1895 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1896 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1897 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001899 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1900 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1901 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1902 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1905 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1906 /* FRAGMENT and LAST_FRAGMENT packets */
1907 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1908}
1909
Jon Paul Maloye099e862014-02-13 17:29:18 -05001910/* tipc_link_find_owner - locate owner node of link by link's name
1911 * @name: pointer to link name string
1912 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001913 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001914 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001916static struct tipc_node *tipc_link_find_owner(const char *link_name,
1917 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001919 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001920 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001921 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001922 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923
Jon Paul Maloye099e862014-02-13 17:29:18 -05001924 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001925 rcu_read_lock();
1926 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001927 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001928 for (i = 0; i < MAX_BEARERS; i++) {
1929 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001930 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1931 *bearer_id = i;
1932 found_node = n_ptr;
1933 break;
1934 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001935 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001936 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001937 if (found_node)
1938 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001939 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001940 rcu_read_unlock();
1941
Jon Paul Maloye099e862014-02-13 17:29:18 -05001942 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001943}
1944
Allan Stephens5c216e12011-10-18 11:34:29 -04001945/**
1946 * link_value_is_valid -- validate proposed link tolerance/priority/window
1947 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001948 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1949 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04001950 *
1951 * Returns 1 if value is within range, 0 if not.
1952 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001953static int link_value_is_valid(u16 cmd, u32 new_value)
1954{
1955 switch (cmd) {
1956 case TIPC_CMD_SET_LINK_TOL:
1957 return (new_value >= TIPC_MIN_LINK_TOL) &&
1958 (new_value <= TIPC_MAX_LINK_TOL);
1959 case TIPC_CMD_SET_LINK_PRI:
1960 return (new_value <= TIPC_MAX_LINK_PRI);
1961 case TIPC_CMD_SET_LINK_WINDOW:
1962 return (new_value >= TIPC_MIN_LINK_WIN) &&
1963 (new_value <= TIPC_MAX_LINK_WIN);
1964 }
1965 return 0;
1966}
1967
Allan Stephens5c216e12011-10-18 11:34:29 -04001968/**
1969 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00001970 * @name: ptr to link, bearer, or media name
1971 * @new_value: new value of link, bearer, or media setting
1972 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04001973 *
Ying Xue7216cd92014-04-21 10:55:48 +08001974 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04001975 *
1976 * Returns 0 if value updated and negative value on error.
1977 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001978static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
1979{
1980 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001981 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04001982 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05001983 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001984 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02001985 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04001986
Jon Paul Maloye099e862014-02-13 17:29:18 -05001987 node = tipc_link_find_owner(name, &bearer_id);
1988 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04001989 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001990 l_ptr = node->links[bearer_id];
1991
1992 if (l_ptr) {
1993 switch (cmd) {
1994 case TIPC_CMD_SET_LINK_TOL:
1995 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08001996 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
1997 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001998 break;
1999 case TIPC_CMD_SET_LINK_PRI:
2000 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002001 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2002 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002003 break;
2004 case TIPC_CMD_SET_LINK_WINDOW:
2005 tipc_link_set_queue_limits(l_ptr, new_value);
2006 break;
2007 default:
2008 res = -EINVAL;
2009 break;
2010 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002011 }
2012 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002013 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002014 }
2015
2016 b_ptr = tipc_bearer_find(name);
2017 if (b_ptr) {
2018 switch (cmd) {
2019 case TIPC_CMD_SET_LINK_TOL:
2020 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002021 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002022 case TIPC_CMD_SET_LINK_PRI:
2023 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002024 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002025 case TIPC_CMD_SET_LINK_WINDOW:
2026 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002027 break;
2028 default:
2029 res = -EINVAL;
2030 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002031 }
Ying Xue636c0372013-10-18 07:23:20 +02002032 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002033 }
2034
2035 m_ptr = tipc_media_find(name);
2036 if (!m_ptr)
2037 return -ENODEV;
2038 switch (cmd) {
2039 case TIPC_CMD_SET_LINK_TOL:
2040 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002041 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002042 case TIPC_CMD_SET_LINK_PRI:
2043 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002044 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002045 case TIPC_CMD_SET_LINK_WINDOW:
2046 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002047 break;
2048 default:
2049 res = -EINVAL;
2050 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002051 }
Ying Xue636c0372013-10-18 07:23:20 +02002052 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002053}
2054
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002055struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002056 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057{
2058 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002059 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002060 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002061
2062 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002063 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064
2065 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2066 new_value = ntohl(args->value);
2067
Allan Stephens5c216e12011-10-18 11:34:29 -04002068 if (!link_value_is_valid(cmd, new_value))
2069 return tipc_cfg_reply_error_string(
2070 "cannot change, value invalid");
2071
Per Liden4323add2006-01-18 00:38:21 +01002072 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002074 (tipc_bclink_set_queue_limits(new_value) == 0))
2075 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002076 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002077 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078 }
2079
Allan Stephens5c216e12011-10-18 11:34:29 -04002080 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002082 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083
Per Liden4323add2006-01-18 00:38:21 +01002084 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002085}
2086
2087/**
2088 * link_reset_statistics - reset link statistics
2089 * @l_ptr: pointer to link
2090 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002091static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092{
2093 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2094 l_ptr->stats.sent_info = l_ptr->next_out_no;
2095 l_ptr->stats.recv_info = l_ptr->next_in_no;
2096}
2097
Per Liden4323add2006-01-18 00:38:21 +01002098struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099{
2100 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002101 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002102 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002103 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104
2105 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002106 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107
2108 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002109 if (!strcmp(link_name, tipc_bclink_name)) {
2110 if (tipc_bclink_reset_stats())
2111 return tipc_cfg_reply_error_string("link not found");
2112 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002114 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002115 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002116 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002117
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002118 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002119 l_ptr = node->links[bearer_id];
2120 if (!l_ptr) {
2121 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002122 return tipc_cfg_reply_error_string("link not found");
2123 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002125 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002126 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127}
2128
2129/**
2130 * percent - convert count to a percentage of total (rounding up or down)
2131 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002132static u32 percent(u32 count, u32 total)
2133{
2134 return (count * 100 + (total / 2)) / total;
2135}
2136
2137/**
Per Liden4323add2006-01-18 00:38:21 +01002138 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002139 * @name: link name
2140 * @buf: print buffer area
2141 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002142 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143 * Returns length of print buffer data string (or 0 if error)
2144 */
Per Liden4323add2006-01-18 00:38:21 +01002145static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002146{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002147 struct tipc_link *l;
2148 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002149 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 char *status;
2151 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002152 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002153 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002154
Per Liden4323add2006-01-18 00:38:21 +01002155 if (!strcmp(name, tipc_bclink_name))
2156 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157
Jon Paul Maloye099e862014-02-13 17:29:18 -05002158 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002159 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002160 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002161
Per Liden4323add2006-01-18 00:38:21 +01002162 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002163
2164 l = node->links[bearer_id];
2165 if (!l) {
2166 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002167 return 0;
2168 }
2169
Erik Hugnedc1aed32012-06-29 00:50:23 -04002170 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171
Erik Hugnedc1aed32012-06-29 00:50:23 -04002172 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002173 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002174 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175 status = "STANDBY";
2176 else
2177 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002178
2179 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2180 " %s MTU:%u Priority:%u Tolerance:%u ms"
2181 " Window:%u packets\n",
2182 l->name, status, l->max_pkt, l->priority,
2183 l->tolerance, l->queue_limit[0]);
2184
2185 ret += tipc_snprintf(buf + ret, buf_size - ret,
2186 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2187 l->next_in_no - s->recv_info, s->recv_fragments,
2188 s->recv_fragmented, s->recv_bundles,
2189 s->recv_bundled);
2190
2191 ret += tipc_snprintf(buf + ret, buf_size - ret,
2192 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2193 l->next_out_no - s->sent_info, s->sent_fragments,
2194 s->sent_fragmented, s->sent_bundles,
2195 s->sent_bundled);
2196
2197 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002198 if (!profile_total)
2199 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002200
2201 ret += tipc_snprintf(buf + ret, buf_size - ret,
2202 " TX profile sample:%u packets average:%u octets\n"
2203 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2204 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2205 s->msg_length_counts,
2206 s->msg_lengths_total / profile_total,
2207 percent(s->msg_length_profile[0], profile_total),
2208 percent(s->msg_length_profile[1], profile_total),
2209 percent(s->msg_length_profile[2], profile_total),
2210 percent(s->msg_length_profile[3], profile_total),
2211 percent(s->msg_length_profile[4], profile_total),
2212 percent(s->msg_length_profile[5], profile_total),
2213 percent(s->msg_length_profile[6], profile_total));
2214
2215 ret += tipc_snprintf(buf + ret, buf_size - ret,
2216 " RX states:%u probes:%u naks:%u defs:%u"
2217 " dups:%u\n", s->recv_states, s->recv_probes,
2218 s->recv_nacks, s->deferred_recv, s->duplicates);
2219
2220 ret += tipc_snprintf(buf + ret, buf_size - ret,
2221 " TX states:%u probes:%u naks:%u acks:%u"
2222 " dups:%u\n", s->sent_states, s->sent_probes,
2223 s->sent_nacks, s->sent_acks, s->retransmitted);
2224
2225 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002226 " Congestion link:%u Send queue"
2227 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002228 s->max_queue_sz, s->queue_sz_counts ?
2229 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002230
Per Liden4323add2006-01-18 00:38:21 +01002231 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002232 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002233}
2234
Per Liden4323add2006-01-18 00:38:21 +01002235struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002236{
2237 struct sk_buff *buf;
2238 struct tlv_desc *rep_tlv;
2239 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002240 int pb_len;
2241 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002242
2243 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002244 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002245
Erik Hugnedc1aed32012-06-29 00:50:23 -04002246 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002247 if (!buf)
2248 return NULL;
2249
2250 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002251 pb = TLV_DATA(rep_tlv);
2252 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002253 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002254 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002255 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002256 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002257 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002258 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002259 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002260 skb_put(buf, TLV_SPACE(str_len));
2261 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2262
2263 return buf;
2264}
2265
Per Lidenb97bf3f2006-01-02 19:04:38 +01002266/**
Per Liden4323add2006-01-18 00:38:21 +01002267 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002268 * @dest: network address of destination node
2269 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002270 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002271 * If no active link can be found, uses default maximum packet size.
2272 */
Per Liden4323add2006-01-18 00:38:21 +01002273u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002274{
David S. Miller6c000552008-09-02 23:38:32 -07002275 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002276 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002277 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002278
Per Lidenb97bf3f2006-01-02 19:04:38 +01002279 if (dest == tipc_own_addr)
2280 return MAX_MSG_SIZE;
2281
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002282 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002283 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002284 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002285 l_ptr = n_ptr->active_links[selector & 1];
2286 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002287 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002288 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002289 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002290 return res;
2291}
2292
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002293static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002294{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002295 struct tipc_bearer *b_ptr;
2296
2297 rcu_read_lock();
2298 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2299 if (b_ptr)
2300 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2301 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002302
Per Lidenb97bf3f2006-01-02 19:04:38 +01002303 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002304 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002305 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002306 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002307 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002308 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002309 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002310 pr_cont(":WW\n");
2311 else
2312 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002313}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002314
2315/* Parse and validate nested (link) properties valid for media, bearer and link
2316 */
2317int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2318{
2319 int err;
2320
2321 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2322 tipc_nl_prop_policy);
2323 if (err)
2324 return err;
2325
2326 if (props[TIPC_NLA_PROP_PRIO]) {
2327 u32 prio;
2328
2329 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2330 if (prio > TIPC_MAX_LINK_PRI)
2331 return -EINVAL;
2332 }
2333
2334 if (props[TIPC_NLA_PROP_TOL]) {
2335 u32 tol;
2336
2337 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2338 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2339 return -EINVAL;
2340 }
2341
2342 if (props[TIPC_NLA_PROP_WIN]) {
2343 u32 win;
2344
2345 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2346 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2347 return -EINVAL;
2348 }
2349
2350 return 0;
2351}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002352
Richard Alpef96ce7a2014-11-20 10:29:13 +01002353int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2354{
2355 int err;
2356 int res = 0;
2357 int bearer_id;
2358 char *name;
2359 struct tipc_link *link;
2360 struct tipc_node *node;
2361 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2362
2363 if (!info->attrs[TIPC_NLA_LINK])
2364 return -EINVAL;
2365
2366 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2367 info->attrs[TIPC_NLA_LINK],
2368 tipc_nl_link_policy);
2369 if (err)
2370 return err;
2371
2372 if (!attrs[TIPC_NLA_LINK_NAME])
2373 return -EINVAL;
2374
2375 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2376
2377 node = tipc_link_find_owner(name, &bearer_id);
2378 if (!node)
2379 return -EINVAL;
2380
2381 tipc_node_lock(node);
2382
2383 link = node->links[bearer_id];
2384 if (!link) {
2385 res = -EINVAL;
2386 goto out;
2387 }
2388
2389 if (attrs[TIPC_NLA_LINK_PROP]) {
2390 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2391
2392 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2393 props);
2394 if (err) {
2395 res = err;
2396 goto out;
2397 }
2398
2399 if (props[TIPC_NLA_PROP_TOL]) {
2400 u32 tol;
2401
2402 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2403 link_set_supervision_props(link, tol);
2404 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2405 }
2406 if (props[TIPC_NLA_PROP_PRIO]) {
2407 u32 prio;
2408
2409 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2410 link->priority = prio;
2411 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2412 }
2413 if (props[TIPC_NLA_PROP_WIN]) {
2414 u32 win;
2415
2416 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2417 tipc_link_set_queue_limits(link, win);
2418 }
2419 }
2420
2421out:
2422 tipc_node_unlock(node);
2423
2424 return res;
2425}
Richard Alped8182802014-11-24 11:10:29 +01002426
2427static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002428{
2429 int i;
2430 struct nlattr *stats;
2431
2432 struct nla_map {
2433 u32 key;
2434 u32 val;
2435 };
2436
2437 struct nla_map map[] = {
2438 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2439 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2440 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2441 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2442 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2443 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2444 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2445 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2446 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2447 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2448 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2449 s->msg_length_counts : 1},
2450 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2451 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2452 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2453 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2454 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2455 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2456 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2457 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2458 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2459 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2460 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2461 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2462 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2463 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2464 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2465 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2466 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2467 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2468 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2469 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2470 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2471 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2472 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2473 };
2474
2475 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2476 if (!stats)
2477 return -EMSGSIZE;
2478
2479 for (i = 0; i < ARRAY_SIZE(map); i++)
2480 if (nla_put_u32(skb, map[i].key, map[i].val))
2481 goto msg_full;
2482
2483 nla_nest_end(skb, stats);
2484
2485 return 0;
2486msg_full:
2487 nla_nest_cancel(skb, stats);
2488
2489 return -EMSGSIZE;
2490}
2491
2492/* Caller should hold appropriate locks to protect the link */
Richard Alped8182802014-11-24 11:10:29 +01002493static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002494{
2495 int err;
2496 void *hdr;
2497 struct nlattr *attrs;
2498 struct nlattr *prop;
2499
2500 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2501 NLM_F_MULTI, TIPC_NL_LINK_GET);
2502 if (!hdr)
2503 return -EMSGSIZE;
2504
2505 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2506 if (!attrs)
2507 goto msg_full;
2508
2509 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2510 goto attr_msg_full;
2511 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2512 tipc_cluster_mask(tipc_own_addr)))
2513 goto attr_msg_full;
2514 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2515 goto attr_msg_full;
2516 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2517 goto attr_msg_full;
2518 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2519 goto attr_msg_full;
2520
2521 if (tipc_link_is_up(link))
2522 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2523 goto attr_msg_full;
2524 if (tipc_link_is_active(link))
2525 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2526 goto attr_msg_full;
2527
2528 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2529 if (!prop)
2530 goto attr_msg_full;
2531 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2532 goto prop_msg_full;
2533 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2534 goto prop_msg_full;
2535 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2536 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2537 goto prop_msg_full;
2538 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2539 goto prop_msg_full;
2540 nla_nest_end(msg->skb, prop);
2541
2542 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2543 if (err)
2544 goto attr_msg_full;
2545
2546 nla_nest_end(msg->skb, attrs);
2547 genlmsg_end(msg->skb, hdr);
2548
2549 return 0;
2550
2551prop_msg_full:
2552 nla_nest_cancel(msg->skb, prop);
2553attr_msg_full:
2554 nla_nest_cancel(msg->skb, attrs);
2555msg_full:
2556 genlmsg_cancel(msg->skb, hdr);
2557
2558 return -EMSGSIZE;
2559}
2560
2561/* Caller should hold node lock */
Richard Alped8182802014-11-24 11:10:29 +01002562static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
2563 struct tipc_node *node,
2564 u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002565{
2566 u32 i;
2567 int err;
2568
2569 for (i = *prev_link; i < MAX_BEARERS; i++) {
2570 *prev_link = i;
2571
2572 if (!node->links[i])
2573 continue;
2574
2575 err = __tipc_nl_add_link(msg, node->links[i]);
2576 if (err)
2577 return err;
2578 }
2579 *prev_link = 0;
2580
2581 return 0;
2582}
2583
2584int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2585{
2586 struct tipc_node *node;
2587 struct tipc_nl_msg msg;
2588 u32 prev_node = cb->args[0];
2589 u32 prev_link = cb->args[1];
2590 int done = cb->args[2];
2591 int err;
2592
2593 if (done)
2594 return 0;
2595
2596 msg.skb = skb;
2597 msg.portid = NETLINK_CB(cb->skb).portid;
2598 msg.seq = cb->nlh->nlmsg_seq;
2599
2600 rcu_read_lock();
2601
2602 if (prev_node) {
2603 node = tipc_node_find(prev_node);
2604 if (!node) {
2605 /* We never set seq or call nl_dump_check_consistent()
2606 * this means that setting prev_seq here will cause the
2607 * consistence check to fail in the netlink callback
2608 * handler. Resulting in the last NLMSG_DONE message
2609 * having the NLM_F_DUMP_INTR flag set.
2610 */
2611 cb->prev_seq = 1;
2612 goto out;
2613 }
2614
2615 list_for_each_entry_continue_rcu(node, &tipc_node_list, list) {
2616 tipc_node_lock(node);
2617 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2618 tipc_node_unlock(node);
2619 if (err)
2620 goto out;
2621
2622 prev_node = node->addr;
2623 }
2624 } else {
2625 err = tipc_nl_add_bc_link(&msg);
2626 if (err)
2627 goto out;
2628
2629 list_for_each_entry_rcu(node, &tipc_node_list, list) {
2630 tipc_node_lock(node);
2631 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2632 tipc_node_unlock(node);
2633 if (err)
2634 goto out;
2635
2636 prev_node = node->addr;
2637 }
2638 }
2639 done = 1;
2640out:
2641 rcu_read_unlock();
2642
2643 cb->args[0] = prev_node;
2644 cb->args[1] = prev_link;
2645 cb->args[2] = done;
2646
2647 return skb->len;
2648}
2649
2650int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2651{
2652 struct sk_buff *ans_skb;
2653 struct tipc_nl_msg msg;
2654 struct tipc_link *link;
2655 struct tipc_node *node;
2656 char *name;
2657 int bearer_id;
2658 int err;
2659
2660 if (!info->attrs[TIPC_NLA_LINK_NAME])
2661 return -EINVAL;
2662
2663 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
2664 node = tipc_link_find_owner(name, &bearer_id);
2665 if (!node)
2666 return -EINVAL;
2667
2668 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2669 if (!ans_skb)
2670 return -ENOMEM;
2671
2672 msg.skb = ans_skb;
2673 msg.portid = info->snd_portid;
2674 msg.seq = info->snd_seq;
2675
2676 tipc_node_lock(node);
2677 link = node->links[bearer_id];
2678 if (!link) {
2679 err = -EINVAL;
2680 goto err_out;
2681 }
2682
2683 err = __tipc_nl_add_link(&msg, link);
2684 if (err)
2685 goto err_out;
2686
2687 tipc_node_unlock(node);
2688
2689 return genlmsg_reply(ans_skb, info);
2690
2691err_out:
2692 tipc_node_unlock(node);
2693 nlmsg_free(ans_skb);
2694
2695 return err;
2696}
Richard Alpeae363422014-11-20 10:29:14 +01002697
2698int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2699{
2700 int err;
2701 char *link_name;
2702 unsigned int bearer_id;
2703 struct tipc_link *link;
2704 struct tipc_node *node;
2705 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2706
2707 if (!info->attrs[TIPC_NLA_LINK])
2708 return -EINVAL;
2709
2710 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2711 info->attrs[TIPC_NLA_LINK],
2712 tipc_nl_link_policy);
2713 if (err)
2714 return err;
2715
2716 if (!attrs[TIPC_NLA_LINK_NAME])
2717 return -EINVAL;
2718
2719 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2720
2721 if (strcmp(link_name, tipc_bclink_name) == 0) {
2722 err = tipc_bclink_reset_stats();
2723 if (err)
2724 return err;
2725 return 0;
2726 }
2727
2728 node = tipc_link_find_owner(link_name, &bearer_id);
2729 if (!node)
2730 return -EINVAL;
2731
2732 tipc_node_lock(node);
2733
2734 link = node->links[bearer_id];
2735 if (!link) {
2736 tipc_node_unlock(node);
2737 return -EINVAL;
2738 }
2739
2740 link_reset_statistics(link);
2741
2742 tipc_node_unlock(node);
2743
2744 return 0;
2745}