blob: d4b5de41b682188f1cf3bcffb08a44f38a9f84ae [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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "discover.h"
42#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
Ying Xue796c75d2013-06-17 10:54:48 -040044#include <linux/pkt_sched.h>
45
Erik Hugne2cf8aa12012-06-29 00:16:37 -040046/*
47 * Error message prefixes
48 */
49static const char *link_co_err = "Link changeover error, ";
50static const char *link_rst_msg = "Resetting link ";
51static const char *link_unk_evt = "Unknown link event ";
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090053/*
Allan Stephensa686e682008-06-04 17:29:39 -070054 * Out-of-range value for link session numbers
55 */
Allan Stephensa686e682008-06-04 17:29:39 -070056#define INVALID_SESSION 0x10000
57
58/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090059 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010060 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010061#define STARTING_EVT 856384768 /* link processing trigger */
62#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
63#define TIMEOUT_EVT 560817u /* link timer expired */
64
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090065/*
66 * The following two 'message types' is really just implementation
67 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010068 * They must not be considered part of the protocol
69 */
70#define OPEN_MSG 0
71#define CLOSED_MSG 1
72
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090073/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010074 * State value stored in 'exp_msg_count'
75 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010076#define START_CHANGEOVER 100000u
77
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050078static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +010079 struct sk_buff *buf);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050080static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Paul Maloy170b3922014-01-07 17:02:41 -050081static int tipc_link_tunnel_rcv(struct tipc_link **l_ptr,
82 struct sk_buff **buf);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050083static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050084static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +010085 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +020086 unsigned int len, u32 destnode);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050087static void link_state_event(struct tipc_link *l_ptr, u32 event);
88static void link_reset_statistics(struct tipc_link *l_ptr);
89static void link_print(struct tipc_link *l_ptr, const char *str);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050090static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +080091static void tipc_link_send_sync(struct tipc_link *l);
92static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +000093
Per Lidenb97bf3f2006-01-02 19:04:38 +010094/*
Sam Ravnborg05790c62006-03-20 22:37:04 -080095 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +010096 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080097static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +010098{
99 return (i + 3) & ~3u;
100}
101
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500102static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103{
104 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900105
Allan Stephens2d627b92011-01-07 13:00:11 -0500106 max_pkt = (l_ptr->b_ptr->mtu & ~3);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107 if (max_pkt > MAX_MSG_SIZE)
108 max_pkt = MAX_MSG_SIZE;
109
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900110 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
112 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900113 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 l_ptr->max_pkt = MAX_PKT_DEFAULT;
115
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900116 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117}
118
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500119static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120{
121 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400122 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123 return mod(l_ptr->next_out_no);
124}
125
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500126static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
128 return mod(link_next_sent(l_ptr) - 1);
129}
130
131/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800132 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500134int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135{
136 if (!l_ptr)
137 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000138 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139}
140
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500141int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000143 return (l_ptr->owner->active_links[0] == l_ptr) ||
144 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145}
146
147/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 * link_timeout - handle expiration of link timer
149 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900150 *
Per Liden4323add2006-01-18 00:38:21 +0100151 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
152 * with tipc_link_delete(). (There is no risk that the node will be deleted by
153 * another thread because tipc_link_delete() always cancels the link timer before
154 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500156static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157{
Per Liden4323add2006-01-18 00:38:21 +0100158 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159
160 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
162 l_ptr->stats.queue_sz_counts++;
163
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 if (l_ptr->first_out) {
165 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
166 u32 length = msg_size(msg);
167
Joe Perchesf64f9e72009-11-29 16:55:45 -0800168 if ((msg_user(msg) == MSG_FRAGMENTER) &&
169 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 length = msg_size(msg_get_wrapped(msg));
171 }
172 if (length) {
173 l_ptr->stats.msg_lengths_total += length;
174 l_ptr->stats.msg_length_counts++;
175 if (length <= 64)
176 l_ptr->stats.msg_length_profile[0]++;
177 else if (length <= 256)
178 l_ptr->stats.msg_length_profile[1]++;
179 else if (length <= 1024)
180 l_ptr->stats.msg_length_profile[2]++;
181 else if (length <= 4096)
182 l_ptr->stats.msg_length_profile[3]++;
183 else if (length <= 16384)
184 l_ptr->stats.msg_length_profile[4]++;
185 else if (length <= 32768)
186 l_ptr->stats.msg_length_profile[5]++;
187 else
188 l_ptr->stats.msg_length_profile[6]++;
189 }
190 }
191
192 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193
194 link_state_event(l_ptr, TIMEOUT_EVT);
195
196 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100197 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198
Per Liden4323add2006-01-18 00:38:21 +0100199 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200}
201
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500202static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203{
204 k_start_timer(&l_ptr->timer, time);
205}
206
207/**
Per Liden4323add2006-01-18 00:38:21 +0100208 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500209 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900212 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 * Returns pointer to link.
214 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500215struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Allan Stephens37b9c082011-02-28 11:32:27 -0500216 struct tipc_bearer *b_ptr,
Per Liden4323add2006-01-18 00:38:21 +0100217 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500219 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 struct tipc_msg *msg;
221 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500222 char addr_string[16];
223 u32 peer = n_ptr->addr;
224
225 if (n_ptr->link_cnt >= 2) {
226 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400227 pr_err("Attempt to establish third link to %s\n", addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500228 return NULL;
229 }
230
231 if (n_ptr->links[b_ptr->identity]) {
232 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400233 pr_err("Attempt to establish second link on <%s> to %s\n",
234 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500235 return NULL;
236 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700238 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400240 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 return NULL;
242 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
244 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500245 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400246 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900248 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 if_name,
250 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400251 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500253 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 l_ptr->checkpoint = 1;
Allan Stephensf882cb72011-04-07 09:43:27 -0400255 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 l_ptr->b_ptr = b_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -0400257 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 l_ptr->state = RESET_UNKNOWN;
259
260 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
261 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000262 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700264 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 msg_set_bearer_id(msg, b_ptr->identity);
266 strcpy((char *)msg_data(msg), if_name);
267
268 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400269 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270
271 link_init_max_pkt(l_ptr);
272
273 l_ptr->next_out_no = 1;
274 INIT_LIST_HEAD(&l_ptr->waiting_ports);
275
276 link_reset_statistics(l_ptr);
277
Allan Stephens37b9c082011-02-28 11:32:27 -0500278 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500280 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
281 (unsigned long)l_ptr);
Florian Westphal945710652007-07-26 00:05:07 -0700282 list_add_tail(&l_ptr->link_list, &b_ptr->links);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500283
284 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 return l_ptr;
287}
288
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900289/**
Per Liden4323add2006-01-18 00:38:21 +0100290 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900292 *
Per Liden4323add2006-01-18 00:38:21 +0100293 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900295 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500297void tipc_link_delete(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298{
299 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400300 pr_err("Attempt to delete non-existent link\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 return;
302 }
303
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900305
Per Liden4323add2006-01-18 00:38:21 +0100306 tipc_node_lock(l_ptr->owner);
307 tipc_link_reset(l_ptr);
308 tipc_node_detach_link(l_ptr->owner, l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500309 tipc_link_purge_queues(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 list_del_init(&l_ptr->link_list);
Per Liden4323add2006-01-18 00:38:21 +0100311 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 k_term_timer(&l_ptr->timer);
313 kfree(l_ptr);
314}
315
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316
317/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900318 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 * @l_ptr: pointer to link
320 * @origport: reference to sending port
321 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900322 *
323 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 * has abated.
325 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500326static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500328 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329
Per Liden4323add2006-01-18 00:38:21 +0100330 spin_lock_bh(&tipc_port_list_lock);
331 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 if (p_ptr) {
333 if (!p_ptr->wakeup)
334 goto exit;
335 if (!list_empty(&p_ptr->wait_list))
336 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500337 p_ptr->congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000338 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
340 l_ptr->stats.link_congs++;
341exit:
Per Liden4323add2006-01-18 00:38:21 +0100342 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 }
Per Liden4323add2006-01-18 00:38:21 +0100344 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 return -ELINKCONG;
346}
347
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500348void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500350 struct tipc_port *p_ptr;
351 struct tipc_port *temp_p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
353
354 if (all)
355 win = 100000;
356 if (win <= 0)
357 return;
Per Liden4323add2006-01-18 00:38:21 +0100358 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 return;
360 if (link_congested(l_ptr))
361 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900362 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 wait_list) {
364 if (win <= 0)
365 break;
366 list_del_init(&p_ptr->wait_list);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500367 spin_lock_bh(p_ptr->lock);
368 p_ptr->congested = 0;
369 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 win -= p_ptr->waiting_pkts;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500371 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 }
373
374exit:
Per Liden4323add2006-01-18 00:38:21 +0100375 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376}
377
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900378/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 * link_release_outqueue - purge link's outbound message queue
380 * @l_ptr: pointer to link
381 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500382static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383{
Ying Xued77b3832013-12-10 20:45:38 -0800384 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 l_ptr->first_out = NULL;
386 l_ptr->out_queue_size = 0;
387}
388
389/**
Per Liden4323add2006-01-18 00:38:21 +0100390 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 * @l_ptr: pointer to link
392 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500393void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394{
Erik Hugne40ba3cd2013-11-06 09:28:06 +0100395 kfree_skb(l_ptr->reasm_head);
396 l_ptr->reasm_head = NULL;
397 l_ptr->reasm_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398}
399
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900400/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500401 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 * @l_ptr: pointer to link
403 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500404void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405{
Ying Xued77b3832013-12-10 20:45:38 -0800406 kfree_skb_list(l_ptr->oldest_deferred_in);
407 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100408 tipc_link_reset_fragments(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400409 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 l_ptr->proto_msg_queue = NULL;
411}
412
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500413void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 u32 prev_state = l_ptr->state;
416 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700417 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900418
Allan Stephensa686e682008-06-04 17:29:39 -0700419 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420
Allan Stephensa686e682008-06-04 17:29:39 -0700421 /* Link is down, accept any session */
422 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900424 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900426
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428
429 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
430 return;
431
Per Liden4323add2006-01-18 00:38:21 +0100432 tipc_node_link_down(l_ptr->owner, l_ptr);
433 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000434
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500435 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 l_ptr->reset_checkpoint = checkpoint;
437 l_ptr->exp_msg_count = START_CHANGEOVER;
438 }
439
440 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 link_release_outqueue(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400442 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 l_ptr->proto_msg_queue = NULL;
Ying Xued77b3832013-12-10 20:45:38 -0800444 kfree_skb_list(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100446 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447
448 l_ptr->retransm_queue_head = 0;
449 l_ptr->retransm_queue_size = 0;
450 l_ptr->last_out = NULL;
451 l_ptr->first_out = NULL;
452 l_ptr->next_out = NULL;
453 l_ptr->unacked_window = 0;
454 l_ptr->checkpoint = 1;
455 l_ptr->next_out_no = 1;
456 l_ptr->deferred_inqueue_sz = 0;
457 l_ptr->oldest_deferred_in = NULL;
458 l_ptr->newest_deferred_in = NULL;
459 l_ptr->fsm_msg_cnt = 0;
460 l_ptr->stale_count = 0;
461 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462}
463
464
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500465static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466{
Allan Stephens5392d642006-06-25 23:52:50 -0700467 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100468 tipc_node_link_up(l_ptr->owner, l_ptr);
469 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470}
471
472/**
473 * link_state_event - link finite state machine
474 * @l_ptr: pointer to link
475 * @event: state machine event to process
476 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000477static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500479 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 u32 cont_intv = l_ptr->continuity_interval;
481
482 if (!l_ptr->started && (event != STARTING_EVT))
483 return; /* Not yet. */
484
Ying Xue77a7e072013-12-10 20:45:44 -0800485 /* Check whether changeover is going on */
486 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000487 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800489 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491
492 switch (l_ptr->state) {
493 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 switch (event) {
495 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 break;
498 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 if (l_ptr->next_in_no != l_ptr->checkpoint) {
500 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100501 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900502 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100503 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 l_ptr->fsm_msg_cnt++;
505 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900506 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100507 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 l_ptr->fsm_msg_cnt++;
509 }
510 link_set_timer(l_ptr, cont_intv);
511 break;
512 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 l_ptr->state = WORKING_UNKNOWN;
514 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100515 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 l_ptr->fsm_msg_cnt++;
517 link_set_timer(l_ptr, cont_intv / 4);
518 break;
519 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400520 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
521 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100522 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 l_ptr->state = RESET_RESET;
524 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100525 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 l_ptr->fsm_msg_cnt++;
527 link_set_timer(l_ptr, cont_intv);
528 break;
529 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400530 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 }
532 break;
533 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 switch (event) {
535 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 l_ptr->state = WORKING_WORKING;
538 l_ptr->fsm_msg_cnt = 0;
539 link_set_timer(l_ptr, cont_intv);
540 break;
541 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400542 pr_info("%s<%s>, requested by peer while probing\n",
543 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100544 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 l_ptr->state = RESET_RESET;
546 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100547 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 l_ptr->fsm_msg_cnt++;
549 link_set_timer(l_ptr, cont_intv);
550 break;
551 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 l_ptr->state = WORKING_WORKING;
554 l_ptr->fsm_msg_cnt = 0;
555 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100556 if (tipc_bclink_acks_missing(l_ptr->owner)) {
557 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
558 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 l_ptr->fsm_msg_cnt++;
560 }
561 link_set_timer(l_ptr, cont_intv);
562 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100564 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 l_ptr->fsm_msg_cnt++;
566 link_set_timer(l_ptr, cont_intv / 4);
567 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400568 pr_warn("%s<%s>, peer not responding\n",
569 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100570 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 l_ptr->state = RESET_UNKNOWN;
572 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100573 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
574 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 l_ptr->fsm_msg_cnt++;
576 link_set_timer(l_ptr, cont_intv);
577 }
578 break;
579 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400580 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 }
582 break;
583 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 switch (event) {
585 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 break;
587 case ACTIVATE_MSG:
588 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000589 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 l_ptr->state = WORKING_WORKING;
592 l_ptr->fsm_msg_cnt = 0;
593 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100594 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800596 if (l_ptr->owner->working_links == 1)
597 tipc_link_send_sync(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 link_set_timer(l_ptr, cont_intv);
599 break;
600 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 l_ptr->state = RESET_RESET;
602 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100603 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 l_ptr->fsm_msg_cnt++;
605 link_set_timer(l_ptr, cont_intv);
606 break;
607 case STARTING_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 l_ptr->started = 1;
609 /* fall through */
610 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100611 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 l_ptr->fsm_msg_cnt++;
613 link_set_timer(l_ptr, cont_intv);
614 break;
615 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400616 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 }
618 break;
619 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 switch (event) {
621 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 case ACTIVATE_MSG:
623 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000624 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 l_ptr->state = WORKING_WORKING;
627 l_ptr->fsm_msg_cnt = 0;
628 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100629 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800631 if (l_ptr->owner->working_links == 1)
632 tipc_link_send_sync(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 link_set_timer(l_ptr, cont_intv);
634 break;
635 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 break;
637 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100638 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639 l_ptr->fsm_msg_cnt++;
640 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 break;
642 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400643 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 }
645 break;
646 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400647 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 }
649}
650
651/*
652 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900653 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400655static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 struct sk_buff *buf)
657{
658 struct tipc_msg *bundler_msg = buf_msg(bundler);
659 struct tipc_msg *msg = buf_msg(buf);
660 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700661 u32 bundle_size = msg_size(bundler_msg);
662 u32 to_pos = align(bundle_size);
663 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664
665 if (msg_user(bundler_msg) != MSG_BUNDLER)
666 return 0;
667 if (msg_type(bundler_msg) != OPEN_MSG)
668 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700669 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000671 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700672 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673
Allan Stephense49060c2006-06-29 12:32:46 -0700674 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300675 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 msg_set_size(bundler_msg, to_pos + size);
677 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400678 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 l_ptr->stats.sent_bundled++;
680 return 1;
681}
682
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500683static void link_add_to_outqueue(struct tipc_link *l_ptr,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800684 struct sk_buff *buf,
685 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686{
687 u32 ack = mod(l_ptr->next_in_no - 1);
688 u32 seqno = mod(l_ptr->next_out_no++);
689
690 msg_set_word(msg, 2, ((ack << 16) | seqno));
691 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
692 buf->next = NULL;
693 if (l_ptr->first_out) {
694 l_ptr->last_out->next = buf;
695 l_ptr->last_out = buf;
696 } else
697 l_ptr->first_out = l_ptr->last_out = buf;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500698
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 l_ptr->out_queue_size++;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500700 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
701 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702}
703
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500704static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
Allan Stephensdc63d912011-04-21 11:50:42 -0400705 struct sk_buff *buf_chain,
706 u32 long_msgno)
707{
708 struct sk_buff *buf;
709 struct tipc_msg *msg;
710
711 if (!l_ptr->next_out)
712 l_ptr->next_out = buf_chain;
713 while (buf_chain) {
714 buf = buf_chain;
715 buf_chain = buf_chain->next;
716
717 msg = buf_msg(buf);
718 msg_set_long_msgno(msg, long_msgno);
719 link_add_to_outqueue(l_ptr, buf, msg);
720 }
721}
722
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723/*
724 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 * inside TIPC when the 'fast path' in tipc_send_buf
726 * has failed, and from link_send()
727 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500728int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729{
730 struct tipc_msg *msg = buf_msg(buf);
731 u32 size = msg_size(msg);
732 u32 dsz = msg_data_sz(msg);
733 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000734 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000736 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 /* Match msg importance against queue limits: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 if (unlikely(queue_size >= queue_limit)) {
740 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
Allan Stephensbebc55a2011-04-19 10:17:58 -0400741 link_schedule_port(l_ptr, msg_origport(msg), size);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400742 kfree_skb(buf);
Allan Stephensbebc55a2011-04-19 10:17:58 -0400743 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 }
Allan Stephens5f6d9122011-11-04 13:24:29 -0400745 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 if (imp > CONN_MANAGER) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400747 pr_warn("%s<%s>, send queue full", link_rst_msg,
748 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100749 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 }
751 return dsz;
752 }
753
754 /* Fragmentation needed ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000756 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400758 /* Packet can be queued or sent. */
Erik Hugne512137e2013-12-06 10:08:00 -0500759 if (likely(!link_congested(l_ptr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 link_add_to_outqueue(l_ptr, buf, msg);
761
Ying Xue3c294cb2012-11-15 11:34:45 +0800762 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
763 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 return dsz;
765 }
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400766 /* Congestion: can message be bundled ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
768 (msg_user(msg) != MSG_FRAGMENTER)) {
769
770 /* Try adding message to an existing bundle */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900771 if (l_ptr->next_out &&
Ying Xue3c294cb2012-11-15 11:34:45 +0800772 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 return dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774
775 /* Try creating a new bundle */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000777 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 struct tipc_msg bundler_hdr;
779
780 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000781 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700782 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300783 skb_copy_to_linear_data(bundler, &bundler_hdr,
784 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 skb_trim(bundler, INT_H_SIZE);
786 link_bundle_buf(l_ptr, bundler, buf);
787 buf = bundler;
788 msg = buf_msg(buf);
789 l_ptr->stats.sent_bundles++;
790 }
791 }
792 }
793 if (!l_ptr->next_out)
794 l_ptr->next_out = buf;
795 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 return dsz;
797}
798
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900799/*
800 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 * not been selected yet, and the the owner node is not locked
802 * Called by TIPC internal users, e.g. the name distributor
803 */
Per Liden4323add2006-01-18 00:38:21 +0100804int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500806 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700807 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 int res = -ELINKCONG;
809
Per Liden4323add2006-01-18 00:38:21 +0100810 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000811 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100813 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 l_ptr = n_ptr->active_links[selector & 1];
Allan Stephensa0168922010-12-31 18:59:35 +0000815 if (l_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100816 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000817 else
Allan Stephens5f6d9122011-11-04 13:24:29 -0400818 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100819 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 } else {
Allan Stephens5f6d9122011-11-04 13:24:29 -0400821 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 }
Per Liden4323add2006-01-18 00:38:21 +0100823 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 return res;
825}
826
Jon Maloyc64f7a62012-11-16 13:51:31 +0800827/*
828 * tipc_link_send_sync - synchronize broadcast link endpoints.
829 *
830 * Give a newly added peer node the sequence number where it should
831 * start receiving and acking broadcast packets.
832 *
833 * Called with node locked
834 */
835static void tipc_link_send_sync(struct tipc_link *l)
836{
837 struct sk_buff *buf;
838 struct tipc_msg *msg;
839
840 buf = tipc_buf_acquire(INT_H_SIZE);
841 if (!buf)
842 return;
843
844 msg = buf_msg(buf);
845 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
846 msg_set_last_bcast(msg, l->owner->bclink.acked);
847 link_add_chain_to_outqueue(l, buf, 0);
848 tipc_link_push_queue(l);
849}
850
851/*
852 * tipc_link_recv_sync - synchronize broadcast link endpoints.
853 * Receive the sequence number where we should start receiving and
854 * acking broadcast packets from a newly added peer node, and open
855 * up for reception of such packets.
856 *
857 * Called with node locked
858 */
859static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf)
860{
861 struct tipc_msg *msg = buf_msg(buf);
862
863 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
864 n->bclink.recv_permitted = true;
865 kfree_skb(buf);
866}
867
868/*
Allan Stephens9aa88c22011-05-31 13:38:02 -0400869 * tipc_link_send_names - send name table entries to new neighbor
870 *
871 * Send routine for bulk delivery of name table messages when contact
872 * with a new neighbor occurs. No link congestion checking is performed
873 * because name table messages *must* be delivered. The messages must be
874 * small enough not to require fragmentation.
875 * Called without any locks held.
876 */
Allan Stephens9aa88c22011-05-31 13:38:02 -0400877void tipc_link_send_names(struct list_head *message_list, u32 dest)
878{
879 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500880 struct tipc_link *l_ptr;
Allan Stephens9aa88c22011-05-31 13:38:02 -0400881 struct sk_buff *buf;
882 struct sk_buff *temp_buf;
883
884 if (list_empty(message_list))
885 return;
886
887 read_lock_bh(&tipc_net_lock);
888 n_ptr = tipc_node_find(dest);
889 if (n_ptr) {
890 tipc_node_lock(n_ptr);
891 l_ptr = n_ptr->active_links[0];
892 if (l_ptr) {
893 /* convert circular list to linear list */
894 ((struct sk_buff *)message_list->prev)->next = NULL;
895 link_add_chain_to_outqueue(l_ptr,
896 (struct sk_buff *)message_list->next, 0);
897 tipc_link_push_queue(l_ptr);
898 INIT_LIST_HEAD(message_list);
899 }
900 tipc_node_unlock(n_ptr);
901 }
902 read_unlock_bh(&tipc_net_lock);
903
904 /* discard the messages if they couldn't be sent */
Allan Stephens9aa88c22011-05-31 13:38:02 -0400905 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
906 list_del((struct list_head *)buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400907 kfree_skb(buf);
Allan Stephens9aa88c22011-05-31 13:38:02 -0400908 }
909}
910
911/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900912 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 * destination link is known and the header is complete,
914 * inclusive total message length. Very time critical.
915 * Link is locked. Returns user data length.
916 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500917static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800918 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919{
920 struct tipc_msg *msg = buf_msg(buf);
921 int res = msg_data_sz(msg);
922
923 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +0000924 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Erik Hugne512137e2013-12-06 10:08:00 -0500925 link_add_to_outqueue(l_ptr, buf, msg);
926 tipc_bearer_send(l_ptr->b_ptr, buf,
927 &l_ptr->media_addr);
928 l_ptr->unacked_window = 0;
929 return res;
930 }
931 else
Allan Stephens15e979d2010-05-11 14:30:10 +0000932 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 }
Per Liden4323add2006-01-18 00:38:21 +0100934 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935}
936
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900937/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900938 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900940 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 * Returns user data length or errno.
942 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500943int tipc_link_send_sections_fast(struct tipc_port *sender,
Per Liden4323add2006-01-18 00:38:21 +0100944 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +0200945 unsigned int len, u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500947 struct tipc_msg *hdr = &sender->phdr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500948 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700950 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 int res;
952 u32 selector = msg_origport(hdr) & 1;
953
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954again:
955 /*
956 * Try building message using port's max_pkt hint.
957 * (Must not hold any locks while building message.)
958 */
Ying Xue9446b872013-10-18 07:23:15 +0200959 res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
Ying Xue7410f962013-06-17 10:54:49 -0400960 /* Exit if build request was invalid */
961 if (unlikely(res < 0))
962 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963
Per Liden4323add2006-01-18 00:38:21 +0100964 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000965 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +0100967 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 l_ptr = node->active_links[selector];
969 if (likely(l_ptr)) {
970 if (likely(buf)) {
971 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500972 &sender->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973exit:
Per Liden4323add2006-01-18 00:38:21 +0100974 tipc_node_unlock(node);
975 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 return res;
977 }
978
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979 /* Exit if link (or bearer) is congested */
Erik Hugne512137e2013-12-06 10:08:00 -0500980 if (link_congested(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 res = link_schedule_port(l_ptr,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500982 sender->ref, res);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 goto exit;
984 }
985
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 * Message size exceeds max_pkt hint; update hint,
988 * then re-try fast path or fragment the message
989 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500990 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +0100991 tipc_node_unlock(node);
992 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993
994
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500995 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 goto again;
997
Ying Xue9446b872013-10-18 07:23:15 +0200998 return link_send_sections_long(sender, msg_sect, len,
Allan Stephens26896902011-04-21 10:42:07 -0500999 destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 }
Per Liden4323add2006-01-18 00:38:21 +01001001 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 }
Per Liden4323add2006-01-18 00:38:21 +01001003 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004
1005 /* Couldn't find a link to the destination node */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 if (buf)
1007 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1008 if (res >= 0)
Ying Xue9446b872013-10-18 07:23:15 +02001009 return tipc_port_reject_sections(sender, hdr, msg_sect,
1010 len, TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 return res;
1012}
1013
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001014/*
1015 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001017 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 * Link and bearer congestion status have been checked to be ok,
1019 * and are ignored if they change.
1020 *
1021 * Note that fragments do not use the full link MTU so that they won't have
1022 * to undergo refragmentation if link changeover causes them to be sent
1023 * over another link with an additional tunnel header added as prefix.
1024 * (Refragmentation will still occur if the other link has a smaller MTU.)
1025 *
1026 * Returns user data length or errno.
1027 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001028static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +02001030 unsigned int len, u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001032 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001033 struct tipc_node *node;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001034 struct tipc_msg *hdr = &sender->phdr;
Ying Xue9446b872013-10-18 07:23:15 +02001035 u32 dsz = len;
Allan Stephens0e659672010-12-31 18:59:32 +00001036 u32 max_pkt, fragm_sz, rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 struct tipc_msg fragm_hdr;
Allan Stephens0e659672010-12-31 18:59:32 +00001038 struct sk_buff *buf, *buf_chain, *prev;
1039 u32 fragm_crs, fragm_rest, hsz, sect_rest;
Ying Xue40682432013-10-18 07:23:16 +02001040 const unchar __user *sect_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 int curr_sect;
1042 u32 fragm_no;
Ying Xue126c0522013-06-17 10:54:50 -04001043 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044
1045again:
1046 fragm_no = 1;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001047 max_pkt = sender->max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001049 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 /* leave room for fragmentation header in each fragment */
1051 rest = dsz;
1052 fragm_crs = 0;
1053 fragm_rest = 0;
1054 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001055 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056 curr_sect = -1;
1057
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001058 /* Prepare reusable fragment header */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001059 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001060 INT_H_SIZE, msg_destnode(hdr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 msg_set_size(&fragm_hdr, max_pkt);
1062 msg_set_fragm_no(&fragm_hdr, 1);
1063
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001064 /* Prepare header of first fragment */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001065 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 if (!buf)
1067 return -ENOMEM;
1068 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001069 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001071 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001073 /* Chop up message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 fragm_crs = INT_H_SIZE + hsz;
1075 fragm_rest = fragm_sz - hsz;
1076
1077 do { /* For all sections */
1078 u32 sz;
1079
1080 if (!sect_rest) {
1081 sect_rest = msg_sect[++curr_sect].iov_len;
Ying Xue40682432013-10-18 07:23:16 +02001082 sect_crs = msg_sect[curr_sect].iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 }
1084
1085 if (sect_rest < fragm_rest)
1086 sz = sect_rest;
1087 else
1088 sz = fragm_rest;
1089
Ying Xuef1733d72013-06-17 10:54:43 -04001090 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
Ying Xue126c0522013-06-17 10:54:50 -04001091 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092error:
Ying Xued77b3832013-12-10 20:45:38 -08001093 kfree_skb_list(buf_chain);
Ying Xue126c0522013-06-17 10:54:50 -04001094 return res;
Ying Xuef1733d72013-06-17 10:54:43 -04001095 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 sect_crs += sz;
1097 sect_rest -= sz;
1098 fragm_crs += sz;
1099 fragm_rest -= sz;
1100 rest -= sz;
1101
1102 if (!fragm_rest && rest) {
1103
1104 /* Initiate new fragment: */
1105 if (rest <= fragm_sz) {
1106 fragm_sz = rest;
Allan Stephens0e659672010-12-31 18:59:32 +00001107 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 } else {
1109 msg_set_type(&fragm_hdr, FRAGMENT);
1110 }
1111 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1112 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1113 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001114 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Ying Xue126c0522013-06-17 10:54:50 -04001115 if (!buf) {
1116 res = -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 goto error;
Ying Xue126c0522013-06-17 10:54:50 -04001118 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001120 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001122 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 fragm_crs = INT_H_SIZE;
1124 fragm_rest = fragm_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125 }
Allan Stephens0e659672010-12-31 18:59:32 +00001126 } while (rest > 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001128 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 * Now we have a buffer chain. Select a link and check
1130 * that packet size is still OK
1131 */
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001132 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001134 tipc_node_lock(node);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001135 l_ptr = node->active_links[sender->ref & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001137 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 goto reject;
1139 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001140 if (l_ptr->max_pkt < max_pkt) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001141 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001142 tipc_node_unlock(node);
Ying Xued77b3832013-12-10 20:45:38 -08001143 kfree_skb_list(buf_chain);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 goto again;
1145 }
1146 } else {
1147reject:
Ying Xued77b3832013-12-10 20:45:38 -08001148 kfree_skb_list(buf_chain);
Ying Xue9446b872013-10-18 07:23:15 +02001149 return tipc_port_reject_sections(sender, hdr, msg_sect,
1150 len, TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 }
1152
Allan Stephensdc63d912011-04-21 11:50:42 -04001153 /* Append chain of fragments to send queue & send them */
Allan Stephense0f08592011-04-17 11:44:24 -04001154 l_ptr->long_msg_seq_no++;
Allan Stephensdc63d912011-04-21 11:50:42 -04001155 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
1156 l_ptr->stats.sent_fragments += fragm_no;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 l_ptr->stats.sent_fragmented++;
Per Liden4323add2006-01-18 00:38:21 +01001158 tipc_link_push_queue(l_ptr);
1159 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 return dsz;
1161}
1162
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001163/*
Per Liden4323add2006-01-18 00:38:21 +01001164 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 */
stephen hemminger98056962014-01-04 13:47:48 -08001166static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167{
1168 struct sk_buff *buf = l_ptr->first_out;
1169 u32 r_q_size = l_ptr->retransm_queue_size;
1170 u32 r_q_head = l_ptr->retransm_queue_head;
1171
1172 /* Step to position where retransmission failed, if any, */
1173 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001175 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -04001177 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178
1179 while (buf && less(first, r_q_head)) {
1180 first = mod(first + 1);
1181 buf = buf->next;
1182 }
1183 l_ptr->retransm_queue_head = r_q_head = first;
1184 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1185 }
1186
1187 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +00001188 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001190 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001191 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1192 l_ptr->retransm_queue_head = mod(++r_q_head);
1193 l_ptr->retransm_queue_size = --r_q_size;
1194 l_ptr->stats.retransmitted++;
1195 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 }
1197
1198 /* Send deferred protocol message, if any: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 buf = l_ptr->proto_msg_queue;
1200 if (buf) {
1201 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001202 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001203 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1204 l_ptr->unacked_window = 0;
1205 kfree_skb(buf);
1206 l_ptr->proto_msg_queue = NULL;
1207 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 }
1209
1210 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 buf = l_ptr->next_out;
1212 if (buf) {
1213 struct tipc_msg *msg = buf_msg(buf);
1214 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -04001215 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216
1217 if (mod(next - first) < l_ptr->queue_limit[0]) {
1218 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001219 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001220 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1221 if (msg_user(msg) == MSG_BUNDLER)
1222 msg_set_type(msg, CLOSED_MSG);
1223 l_ptr->next_out = buf->next;
1224 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 }
1226 }
Ying Xue3c294cb2012-11-15 11:34:45 +08001227 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228}
1229
1230/*
1231 * push_queue(): push out the unsent messages of a link where
1232 * congestion has abated. Node is locked
1233 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001234void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235{
1236 u32 res;
1237
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 do {
Per Liden4323add2006-01-18 00:38:21 +01001239 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001240 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241}
1242
Allan Stephensd356eeb2006-06-25 23:40:01 -07001243static void link_reset_all(unsigned long addr)
1244{
David S. Miller6c000552008-09-02 23:38:32 -07001245 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001246 char addr_string[16];
1247 u32 i;
1248
1249 read_lock_bh(&tipc_net_lock);
1250 n_ptr = tipc_node_find((u32)addr);
1251 if (!n_ptr) {
1252 read_unlock_bh(&tipc_net_lock);
1253 return; /* node no longer exists */
1254 }
1255
1256 tipc_node_lock(n_ptr);
1257
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001258 pr_warn("Resetting all links to %s\n",
1259 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001260
1261 for (i = 0; i < MAX_BEARERS; i++) {
1262 if (n_ptr->links[i]) {
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001263 link_print(n_ptr->links[i], "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001264 tipc_link_reset(n_ptr->links[i]);
1265 }
1266 }
1267
1268 tipc_node_unlock(n_ptr);
1269 read_unlock_bh(&tipc_net_lock);
1270}
1271
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001272static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001273 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001274{
1275 struct tipc_msg *msg = buf_msg(buf);
1276
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001277 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001278
1279 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001280 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001281 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001282 tipc_link_reset(l_ptr);
1283
1284 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001285 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -07001286 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001287 char addr_string[16];
1288
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001289 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1290 pr_cont("Outstanding acks: %lu\n",
1291 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001292
Allan Stephens01d83ed2011-01-18 13:53:16 -05001293 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -07001294 tipc_node_lock(n_ptr);
1295
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001296 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001297 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +08001298 pr_info("Reception permitted: %d, Acked: %u\n",
1299 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001300 n_ptr->bclink.acked);
1301 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1302 n_ptr->bclink.last_in,
1303 n_ptr->bclink.oos_state,
1304 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001305
1306 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1307
1308 tipc_node_unlock(n_ptr);
1309
1310 l_ptr->stale_count = 0;
1311 }
1312}
1313
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001314void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001315 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316{
1317 struct tipc_msg *msg;
1318
Allan Stephensd356eeb2006-06-25 23:40:01 -07001319 if (!buf)
1320 return;
1321
1322 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001323
Erik Hugne512137e2013-12-06 10:08:00 -05001324 /* Detect repeated retransmit failures */
1325 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1326 if (++l_ptr->stale_count > 100) {
1327 link_retransmit_failure(l_ptr, buf);
1328 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001329 }
1330 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001331 l_ptr->last_retransmitted = msg_seqno(msg);
1332 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001334
Neil Hormanca509102010-03-15 07:58:45 +00001335 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 msg = buf_msg(buf);
1337 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001338 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001339 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1340 buf = buf->next;
1341 retransmits--;
1342 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001344
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1346}
1347
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001348/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 * link_insert_deferred_queue - insert deferred messages back into receive chain
1350 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001351static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 struct sk_buff *buf)
1353{
1354 u32 seq_no;
1355
1356 if (l_ptr->oldest_deferred_in == NULL)
1357 return buf;
1358
Allan Stephensf9057302011-10-24 16:03:12 -04001359 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 if (seq_no == mod(l_ptr->next_in_no)) {
1361 l_ptr->newest_deferred_in->next = buf;
1362 buf = l_ptr->oldest_deferred_in;
1363 l_ptr->oldest_deferred_in = NULL;
1364 l_ptr->deferred_inqueue_sz = 0;
1365 }
1366 return buf;
1367}
1368
Allan Stephens85035562008-04-15 19:04:54 -07001369/**
1370 * link_recv_buf_validate - validate basic format of received message
1371 *
1372 * This routine ensures a TIPC message has an acceptable header, and at least
1373 * as much data as the header indicates it should. The routine also ensures
1374 * that the entire message header is stored in the main fragment of the message
1375 * buffer, to simplify future access to message header fields.
1376 *
1377 * Note: Having extra info present in the message header or data areas is OK.
1378 * TIPC will ignore the excess, under the assumption that it is optional info
1379 * introduced by a later release of the protocol.
1380 */
Allan Stephens85035562008-04-15 19:04:54 -07001381static int link_recv_buf_validate(struct sk_buff *buf)
1382{
1383 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001384 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001385 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1386 };
1387
1388 struct tipc_msg *msg;
1389 u32 tipc_hdr[2];
1390 u32 size;
1391 u32 hdr_size;
1392 u32 min_hdr_size;
1393
1394 if (unlikely(buf->len < MIN_H_SIZE))
1395 return 0;
1396
1397 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1398 if (msg == NULL)
1399 return 0;
1400
1401 if (unlikely(msg_version(msg) != TIPC_VERSION))
1402 return 0;
1403
1404 size = msg_size(msg);
1405 hdr_size = msg_hdr_sz(msg);
1406 min_hdr_size = msg_isdata(msg) ?
1407 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1408
1409 if (unlikely((hdr_size < min_hdr_size) ||
1410 (size < hdr_size) ||
1411 (buf->len < size) ||
1412 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1413 return 0;
1414
1415 return pskb_may_pull(buf, hdr_size);
1416}
1417
Allan Stephensb02b69c2010-08-17 11:00:07 +00001418/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001419 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001420 * @head: pointer to message buffer chain
1421 * @tb_ptr: pointer to bearer message arrived on
1422 *
1423 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1424 * structure (i.e. cannot be NULL), but bearer can be inactive.
1425 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001426void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427{
Per Liden4323add2006-01-18 00:38:21 +01001428 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001430 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001431 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432 struct sk_buff *crs;
1433 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001434 struct tipc_msg *msg;
1435 u32 seq_no;
1436 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437 u32 released = 0;
1438 int type;
1439
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001441 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001442
Allan Stephensb02b69c2010-08-17 11:00:07 +00001443 /* Ensure bearer is still enabled */
Allan Stephensb02b69c2010-08-17 11:00:07 +00001444 if (unlikely(!b_ptr->active))
Ying Xue3af390e2013-10-30 11:26:57 +08001445 goto discard;
Allan Stephensb02b69c2010-08-17 11:00:07 +00001446
Allan Stephens85035562008-04-15 19:04:54 -07001447 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001448 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001449 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450
Allan Stephensfe13dda2008-04-15 19:03:23 -07001451 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001452 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001453 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001454
Allan Stephens85035562008-04-15 19:04:54 -07001455 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001456 msg = buf_msg(buf);
1457
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001459 if (msg_user(msg) == LINK_CONFIG)
1460 tipc_disc_recv_msg(buf, b_ptr);
1461 else
1462 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 continue;
1464 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001465
Allan Stephensed33a9c2011-04-05 15:15:04 -04001466 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001467 if (unlikely(!msg_short(msg) &&
1468 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001469 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001470
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001471 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001472 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001474 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001475 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001476
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001477 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001479 if (unlikely(!l_ptr))
1480 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001481
Allan Stephensb4b56102011-05-27 11:00:51 -04001482 /* Verify that communication with node is currently allowed */
Allan Stephensb4b56102011-05-27 11:00:51 -04001483 if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
1484 msg_user(msg) == LINK_PROTOCOL &&
1485 (msg_type(msg) == RESET_MSG ||
1486 msg_type(msg) == ACTIVATE_MSG) &&
1487 !msg_redundant_link(msg))
1488 n_ptr->block_setup &= ~WAIT_PEER_DOWN;
1489
Ying Xue3af390e2013-10-30 11:26:57 +08001490 if (n_ptr->block_setup)
1491 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001492
Allan Stephens85035562008-04-15 19:04:54 -07001493 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001494 seq_no = msg_seqno(msg);
1495 ackd = msg_ack(msg);
1496
1497 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001498 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001499 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500
1501 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001502 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001503 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504 struct sk_buff *next = crs->next;
1505
Allan Stephens5f6d9122011-11-04 13:24:29 -04001506 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507 crs = next;
1508 released++;
1509 }
1510 if (released) {
1511 l_ptr->first_out = crs;
1512 l_ptr->out_queue_size -= released;
1513 }
Allan Stephens85035562008-04-15 19:04:54 -07001514
1515 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001517 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001519 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1521 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001522 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001523 }
1524
Allan Stephens85035562008-04-15 19:04:54 -07001525 /* Now (finally!) process the incoming message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526protocol_check:
Ying Xue3af390e2013-10-30 11:26:57 +08001527 if (unlikely(!link_working_working(l_ptr))) {
1528 if (msg_user(msg) == LINK_PROTOCOL) {
1529 link_recv_proto_msg(l_ptr, buf);
1530 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001531 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 continue;
1533 }
Ying Xue3af390e2013-10-30 11:26:57 +08001534
1535 /* Traffic message. Conditionally activate link */
1536 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1537
1538 if (link_working_working(l_ptr)) {
1539 /* Re-insert buffer in front of queue */
1540 buf->next = head;
1541 head = buf;
1542 tipc_node_unlock(n_ptr);
1543 continue;
1544 }
1545 goto unlock_discard;
1546 }
1547
1548 /* Link is now in state WORKING_WORKING */
1549 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001550 link_handle_out_of_seq_msg(l_ptr, buf);
1551 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001552 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 continue;
1554 }
Ying Xue3af390e2013-10-30 11:26:57 +08001555 l_ptr->next_in_no++;
1556 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557 head = link_insert_deferred_queue(l_ptr, head);
Ying Xue3af390e2013-10-30 11:26:57 +08001558deliver:
1559 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001560 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001561 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 continue;
1563 }
Ying Xue3af390e2013-10-30 11:26:57 +08001564 switch (msg_user(msg)) {
1565 int ret;
1566 case MSG_BUNDLER:
1567 l_ptr->stats.recv_bundles++;
1568 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1569 tipc_node_unlock(n_ptr);
1570 tipc_link_recv_bundle(buf);
1571 continue;
1572 case NAME_DISTRIBUTOR:
1573 n_ptr->bclink.recv_permitted = true;
1574 tipc_node_unlock(n_ptr);
1575 tipc_named_recv(buf);
1576 continue;
1577 case BCAST_PROTOCOL:
1578 tipc_link_recv_sync(n_ptr, buf);
Per Liden4323add2006-01-18 00:38:21 +01001579 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001581 case CONN_MANAGER:
1582 tipc_node_unlock(n_ptr);
1583 tipc_port_recv_proto_msg(buf);
1584 continue;
1585 case MSG_FRAGMENTER:
1586 l_ptr->stats.recv_fragments++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001587 ret = tipc_link_recv_fragment(&l_ptr->reasm_head,
1588 &l_ptr->reasm_tail,
1589 &buf);
1590 if (ret == LINK_REASM_COMPLETE) {
Ying Xue3af390e2013-10-30 11:26:57 +08001591 l_ptr->stats.recv_fragmented++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001592 msg = buf_msg(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001593 goto deliver;
1594 }
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001595 if (ret == LINK_REASM_ERROR)
Erik Hugnea715b492013-11-06 09:28:07 +01001596 tipc_link_reset(l_ptr);
Erik Hugne528f6f42013-11-06 09:28:05 +01001597 tipc_node_unlock(n_ptr);
1598 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001599 case CHANGEOVER_PROTOCOL:
1600 type = msg_type(msg);
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001601 if (tipc_link_tunnel_rcv(&l_ptr, &buf)) {
Ying Xue3af390e2013-10-30 11:26:57 +08001602 msg = buf_msg(buf);
1603 seq_no = msg_seqno(msg);
1604 if (type == ORIGINAL_MSG)
1605 goto deliver;
1606 goto protocol_check;
1607 }
1608 break;
1609 default:
1610 kfree_skb(buf);
1611 buf = NULL;
1612 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 }
Per Liden4323add2006-01-18 00:38:21 +01001614 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001615 tipc_net_route_msg(buf);
1616 continue;
1617unlock_discard:
1618
1619 tipc_node_unlock(n_ptr);
1620discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001621 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 }
Per Liden4323add2006-01-18 00:38:21 +01001623 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624}
1625
Ben Hutchings2c530402012-07-10 10:55:09 +00001626/**
Allan Stephens8809b252011-10-25 10:44:35 -04001627 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1628 *
1629 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 */
Allan Stephens8809b252011-10-25 10:44:35 -04001631u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001632 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633{
Allan Stephens8809b252011-10-25 10:44:35 -04001634 struct sk_buff *queue_buf;
1635 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001636 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637
1638 buf->next = NULL;
1639
1640 /* Empty queue ? */
1641 if (*head == NULL) {
1642 *head = *tail = buf;
1643 return 1;
1644 }
1645
1646 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001647 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648 (*tail)->next = buf;
1649 *tail = buf;
1650 return 1;
1651 }
1652
Allan Stephens8809b252011-10-25 10:44:35 -04001653 /* Locate insertion point in queue, then insert; discard if duplicate */
1654 prev = head;
1655 queue_buf = *head;
1656 for (;;) {
1657 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658
Allan Stephens8809b252011-10-25 10:44:35 -04001659 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001660 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001661 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662 }
Allan Stephens8809b252011-10-25 10:44:35 -04001663
1664 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666
Allan Stephens8809b252011-10-25 10:44:35 -04001667 prev = &queue_buf->next;
1668 queue_buf = queue_buf->next;
1669 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670
Allan Stephens8809b252011-10-25 10:44:35 -04001671 buf->next = queue_buf;
1672 *prev = buf;
1673 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674}
1675
Allan Stephens8809b252011-10-25 10:44:35 -04001676/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1678 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001679static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680 struct sk_buff *buf)
1681{
Allan Stephensf9057302011-10-24 16:03:12 -04001682 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683
1684 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1685 link_recv_proto_msg(l_ptr, buf);
1686 return;
1687 }
1688
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 l_ptr->checkpoint--;
1691
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001692 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 * Discard packet if a duplicate; otherwise add it to deferred queue
1694 * and notify peer of gap as per protocol specification
1695 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 if (less(seq_no, mod(l_ptr->next_in_no))) {
1697 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001698 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 return;
1700 }
1701
Per Liden4323add2006-01-18 00:38:21 +01001702 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1703 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704 l_ptr->deferred_inqueue_sz++;
1705 l_ptr->stats.deferred_recv++;
1706 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01001707 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 } else
1709 l_ptr->stats.duplicates++;
1710}
1711
1712/*
1713 * Send protocol message to the other endpoint.
1714 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001715void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001716 int probe_msg, u32 gap, u32 tolerance,
1717 u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001719 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001721 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001722 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723
Allan Stephens92d2c902011-10-25 11:20:26 -04001724 /* Discard any previous message that was deferred due to congestion */
Allan Stephens92d2c902011-10-25 11:20:26 -04001725 if (l_ptr->proto_msg_queue) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001726 kfree_skb(l_ptr->proto_msg_queue);
Allan Stephens92d2c902011-10-25 11:20:26 -04001727 l_ptr->proto_msg_queue = NULL;
1728 }
1729
Ying Xue77a7e072013-12-10 20:45:44 -08001730 /* Don't send protocol message during link changeover */
1731 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001733
1734 /* Abort non-RESET send if communication with node is prohibited */
Allan Stephensb4b56102011-05-27 11:00:51 -04001735 if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
1736 return;
1737
Allan Stephens92d2c902011-10-25 11:20:26 -04001738 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 msg_set_type(msg, msg_typ);
1740 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001741 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001742 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743
1744 if (msg_typ == STATE_MSG) {
1745 u32 next_sent = mod(l_ptr->next_out_no);
1746
Per Liden4323add2006-01-18 00:38:21 +01001747 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 return;
1749 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001750 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001751 msg_set_next_sent(msg, next_sent);
1752 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001753 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 gap = mod(rec - mod(l_ptr->next_in_no));
1755 }
1756 msg_set_seq_gap(msg, gap);
1757 if (gap)
1758 l_ptr->stats.sent_nacks++;
1759 msg_set_link_tolerance(msg, tolerance);
1760 msg_set_linkprio(msg, priority);
1761 msg_set_max_pkt(msg, ack_mtu);
1762 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1763 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001764 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 u32 mtu = l_ptr->max_pkt;
1766
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001767 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 link_working_working(l_ptr) &&
1769 l_ptr->fsm_msg_cnt) {
1770 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001771 if (l_ptr->max_pkt_probes == 10) {
1772 l_ptr->max_pkt_target = (msg_size - 4);
1773 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001775 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001777 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778
1779 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001780 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001781 l_ptr->stats.sent_states++;
1782 } else { /* RESET_MSG or ACTIVATE_MSG */
1783 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1784 msg_set_seq_gap(msg, 0);
1785 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001786 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 msg_set_link_tolerance(msg, l_ptr->tolerance);
1788 msg_set_linkprio(msg, l_ptr->priority);
1789 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1790 }
1791
Allan Stephens75f0aa42011-02-28 15:30:20 -05001792 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1793 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001795 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796
1797 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1798
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001799 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 if (!buf)
1801 return;
1802
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001803 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001804 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805
Ying Xue3c294cb2012-11-15 11:34:45 +08001806 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001807 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001808 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809}
1810
1811/*
1812 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001813 * Note that network plane id propagates through the network, and may
1814 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001816static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817{
1818 u32 rec_gap = 0;
1819 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001820 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 u32 msg_tol;
1822 struct tipc_msg *msg = buf_msg(buf);
1823
Ying Xue77a7e072013-12-10 20:45:44 -08001824 /* Discard protocol message during link changeover */
1825 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 goto exit;
1827
1828 /* record unnumbered packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 l_ptr->checkpoint--;
1830
1831 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
1832 if (tipc_own_addr > msg_prevnode(msg))
1833 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
1834
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001836
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001838 if (!link_working_unknown(l_ptr) &&
1839 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001840 if (less_eq(msg_session(msg), l_ptr->peer_session))
1841 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001843
1844 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1845 link_working_unknown(l_ptr))) {
1846 /*
1847 * peer has lost contact -- don't allow peer's links
1848 * to reactivate before we recognize loss & clean up
1849 */
1850 l_ptr->owner->block_setup = WAIT_NODE_DOWN;
1851 }
1852
Allan Stephens47361c82011-10-26 10:55:16 -04001853 link_state_event(l_ptr, RESET_MSG);
1854
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 /* fall thru' */
1856 case ACTIVATE_MSG:
1857 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1859
Allan Stephens2db99832010-12-31 18:59:33 +00001860 msg_tol = msg_link_tolerance(msg);
1861 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 link_set_supervision_props(l_ptr, msg_tol);
1863
1864 if (msg_linkprio(msg) > l_ptr->priority)
1865 l_ptr->priority = msg_linkprio(msg);
1866
1867 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001868 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 if (max_pkt_info < l_ptr->max_pkt_target)
1870 l_ptr->max_pkt_target = max_pkt_info;
1871 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1872 l_ptr->max_pkt = l_ptr->max_pkt_target;
1873 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001874 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876
Allan Stephens4d753132011-10-25 12:19:05 -04001877 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001878 if (!tipc_node_is_up(l_ptr->owner)) {
1879 l_ptr->owner->bclink.last_sent =
1880 l_ptr->owner->bclink.last_in =
1881 msg_last_bcast(msg);
1882 l_ptr->owner->bclink.oos_state = 0;
1883 }
Allan Stephens4d753132011-10-25 12:19:05 -04001884
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 l_ptr->peer_session = msg_session(msg);
1886 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001887
1888 if (msg_type(msg) == ACTIVATE_MSG)
1889 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 break;
1891 case STATE_MSG:
1892
Allan Stephens2db99832010-12-31 18:59:33 +00001893 msg_tol = msg_link_tolerance(msg);
1894 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001896
1897 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001899 pr_warn("%s<%s>, priority change %u->%u\n",
1900 link_rst_msg, l_ptr->name, l_ptr->priority,
1901 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001903 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904 break;
1905 }
1906 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1907 l_ptr->stats.recv_states++;
1908 if (link_reset_unknown(l_ptr))
1909 break;
1910
1911 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001912 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913 mod(l_ptr->next_in_no));
1914 }
1915
1916 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001917 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001918 l_ptr->max_pkt = max_pkt_ack;
1919 l_ptr->max_pkt_probes = 0;
1920 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921
1922 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001923 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001925 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001926 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001927 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928
1929 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001930 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001931 tipc_bclink_update_link_state(l_ptr->owner,
1932 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933
1934 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001935 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
1936 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937 }
1938 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001940 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1941 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942 }
1943 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001944 }
1945exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001946 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001947}
1948
1949
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001950/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1951 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001953static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1954 struct tipc_msg *tunnel_hdr,
1955 struct tipc_msg *msg,
1956 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001957{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001958 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959 struct sk_buff *buf;
1960 u32 length = msg_size(msg);
1961
1962 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001963 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001964 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001966 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001967 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001968 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001969 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001970 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001972 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001973 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1974 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Liden4323add2006-01-18 00:38:21 +01001975 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976}
1977
1978
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001979/* tipc_link_failover_send_queue(): A link has gone down, but a second
1980 * link is still active. We can do failover. Tunnel the failing link's
1981 * whole send queue via the remaining link. This way, we don't lose
1982 * any packets, and sequence order is preserved for subsequent traffic
1983 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001985void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986{
1987 u32 msgcount = l_ptr->out_queue_size;
1988 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001989 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001991 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992
1993 if (!tunnel)
1994 return;
1995
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001996 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001997 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001998 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1999 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002000
Per Lidenb97bf3f2006-01-02 19:04:38 +01002001 if (!l_ptr->first_out) {
2002 struct sk_buff *buf;
2003
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002004 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002005 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002006 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Per Liden4323add2006-01-18 00:38:21 +01002008 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002010 pr_warn("%sunable to send changeover msg\n",
2011 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012 }
2013 return;
2014 }
Allan Stephensf1310722006-06-25 23:51:37 -07002015
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002016 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002017 l_ptr->owner->active_links[1]);
2018
Per Lidenb97bf3f2006-01-02 19:04:38 +01002019 while (crs) {
2020 struct tipc_msg *msg = buf_msg(crs);
2021
2022 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002023 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00002024 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025
Florian Westphald788d802007-08-02 19:28:06 -07002026 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00002028 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002029 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
2030 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031 pos += align(msg_size(m));
2032 m = (struct tipc_msg *)pos;
2033 }
2034 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002035 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
2036 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037 }
2038 crs = crs->next;
2039 }
2040}
2041
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002042/* tipc_link_dup_send_queue(): A second link has become active. Tunnel a
2043 * duplicate of the first link's send queue via the new link. This way, we
2044 * are guaranteed that currently queued packets from a socket are delivered
2045 * before future traffic from the same socket, even if this is using the
2046 * new link. The last arriving copy of each duplicate packet is dropped at
2047 * the receiving end by the regular protocol check, so packet cardinality
2048 * and sequence order is preserved per sender/receiver socket pair.
2049 * Owner node is locked.
2050 */
2051void tipc_link_dup_send_queue(struct tipc_link *l_ptr,
2052 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053{
2054 struct sk_buff *iter;
2055 struct tipc_msg tunnel_hdr;
2056
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002057 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002058 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002059 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2060 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2061 iter = l_ptr->first_out;
2062 while (iter) {
2063 struct sk_buff *outbuf;
2064 struct tipc_msg *msg = buf_msg(iter);
2065 u32 length = msg_size(msg);
2066
2067 if (msg_user(msg) == MSG_BUNDLER)
2068 msg_set_type(msg, CLOSED_MSG);
2069 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002070 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002072 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002074 pr_warn("%sunable to send duplicate msg\n",
2075 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076 return;
2077 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002078 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2079 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2080 length);
Per Liden4323add2006-01-18 00:38:21 +01002081 tipc_link_send_buf(tunnel, outbuf);
2082 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083 return;
2084 iter = iter->next;
2085 }
2086}
2087
Per Lidenb97bf3f2006-01-02 19:04:38 +01002088/**
2089 * buf_extract - extracts embedded TIPC message from another message
2090 * @skb: encapsulating message buffer
2091 * @from_pos: offset to extract from
2092 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002093 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094 * encapsulating message itself is left unchanged.
2095 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2097{
2098 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2099 u32 size = msg_size(msg);
2100 struct sk_buff *eb;
2101
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002102 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002104 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002105 return eb;
2106}
2107
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002108/* tipc_link_tunnel_rcv(): Receive a tunneled packet, sent
2109 * via other link as result of a failover (ORIGINAL_MSG) or
2110 * a new active link (DUPLICATE_MSG). Failover packets are
2111 * returned to the active link for delivery upwards.
2112 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002114static int tipc_link_tunnel_rcv(struct tipc_link **l_ptr,
2115 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002116{
2117 struct sk_buff *tunnel_buf = *buf;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002118 struct tipc_link *dest_link;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119 struct tipc_msg *msg;
2120 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2121 u32 msg_typ = msg_type(tunnel_msg);
2122 u32 msg_count = msg_msgcnt(tunnel_msg);
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002123 u32 bearer_id = msg_bearer_id(tunnel_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002125 if (bearer_id >= MAX_BEARERS)
2126 goto exit;
2127 dest_link = (*l_ptr)->owner->links[bearer_id];
Allan Stephensb29f1422010-12-31 18:59:25 +00002128 if (!dest_link)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002129 goto exit;
Allan Stephensf1310722006-06-25 23:51:37 -07002130 if (dest_link == *l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002131 pr_err("Unexpected changeover message on link <%s>\n",
2132 (*l_ptr)->name);
Allan Stephensf1310722006-06-25 23:51:37 -07002133 goto exit;
2134 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135 *l_ptr = dest_link;
2136 msg = msg_get_wrapped(tunnel_msg);
2137
2138 if (msg_typ == DUPLICATE_MSG) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002139 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140 goto exit;
Allan Stephens0e659672010-12-31 18:59:32 +00002141 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 if (*buf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002143 pr_warn("%sduplicate msg dropped\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002144 goto exit;
2145 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002146 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 return 1;
2148 }
2149
2150 /* First original message ?: */
Per Liden4323add2006-01-18 00:38:21 +01002151 if (tipc_link_is_up(dest_link)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002152 pr_info("%s<%s>, changeover initiated by peer\n", link_rst_msg,
2153 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002154 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155 dest_link->exp_msg_count = msg_count;
2156 if (!msg_count)
2157 goto exit;
2158 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159 dest_link->exp_msg_count = msg_count;
2160 if (!msg_count)
2161 goto exit;
2162 }
2163
2164 /* Receive original message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002165 if (dest_link->exp_msg_count == 0) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002166 pr_warn("%sgot too many tunnelled messages\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002167 goto exit;
2168 }
2169 dest_link->exp_msg_count--;
2170 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171 goto exit;
2172 } else {
2173 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2174 if (*buf != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002175 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002176 return 1;
2177 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002178 pr_warn("%soriginal msg dropped\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002179 }
2180 }
2181exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002182 *buf = NULL;
Allan Stephens5f6d9122011-11-04 13:24:29 -04002183 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 return 0;
2185}
2186
2187/*
2188 * Bundler functionality:
2189 */
Per Liden4323add2006-01-18 00:38:21 +01002190void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002191{
2192 u32 msgcount = msg_msgcnt(buf_msg(buf));
2193 u32 pos = INT_H_SIZE;
2194 struct sk_buff *obuf;
2195
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 while (msgcount--) {
2197 obuf = buf_extract(buf, pos);
2198 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002199 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07002200 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002201 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002202 pos += align(msg_size(buf_msg(obuf)));
Per Liden4323add2006-01-18 00:38:21 +01002203 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002204 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002205 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206}
2207
2208/*
2209 * Fragmentation/defragmentation:
2210 */
2211
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002212/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002213 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002214 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002215 * Returns user data length.
2216 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002217static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002218{
Allan Stephens77561552011-04-17 13:06:23 -04002219 struct sk_buff *buf_chain = NULL;
2220 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002221 struct tipc_msg *inmsg = buf_msg(buf);
2222 struct tipc_msg fragm_hdr;
2223 u32 insize = msg_size(inmsg);
2224 u32 dsz = msg_data_sz(inmsg);
2225 unchar *crs = buf->data;
2226 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002227 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002228 u32 fragm_sz = pack_sz - INT_H_SIZE;
Allan Stephens77561552011-04-17 13:06:23 -04002229 u32 fragm_no = 0;
Allan Stephens9c396a72008-06-04 17:36:58 -07002230 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002231
2232 if (msg_short(inmsg))
2233 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002234 else
2235 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002236
Per Lidenb97bf3f2006-01-02 19:04:38 +01002237 /* Prepare reusable fragment header: */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002238 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002239 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002240
2241 /* Chop up message: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002242 while (rest > 0) {
2243 struct sk_buff *fragm;
2244
2245 if (rest <= fragm_sz) {
2246 fragm_sz = rest;
2247 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2248 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002249 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002250 if (fragm == NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002251 kfree_skb(buf);
Ying Xued77b3832013-12-10 20:45:38 -08002252 kfree_skb_list(buf_chain);
Allan Stephens77561552011-04-17 13:06:23 -04002253 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002254 }
2255 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Allan Stephens77561552011-04-17 13:06:23 -04002256 fragm_no++;
2257 msg_set_fragm_no(&fragm_hdr, fragm_no);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002258 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2259 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2260 fragm_sz);
Allan Stephens77561552011-04-17 13:06:23 -04002261 buf_chain_tail->next = fragm;
2262 buf_chain_tail = fragm;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002263
Per Lidenb97bf3f2006-01-02 19:04:38 +01002264 rest -= fragm_sz;
2265 crs += fragm_sz;
2266 msg_set_type(&fragm_hdr, FRAGMENT);
2267 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002268 kfree_skb(buf);
Allan Stephens77561552011-04-17 13:06:23 -04002269
2270 /* Append chain of fragments to send queue & send them */
Allan Stephens77561552011-04-17 13:06:23 -04002271 l_ptr->long_msg_seq_no++;
2272 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2273 l_ptr->stats.sent_fragments += fragm_no;
2274 l_ptr->stats.sent_fragmented++;
2275 tipc_link_push_queue(l_ptr);
2276
Per Lidenb97bf3f2006-01-02 19:04:38 +01002277 return dsz;
2278}
2279
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002280/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002281 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002282 * the reassembled buffer if message is complete.
2283 */
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002284int tipc_link_recv_fragment(struct sk_buff **head, struct sk_buff **tail,
2285 struct sk_buff **fbuf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286{
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002287 struct sk_buff *frag = *fbuf;
2288 struct tipc_msg *msg = buf_msg(frag);
2289 u32 fragid = msg_type(msg);
2290 bool headstolen;
2291 int delta;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002292
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002293 skb_pull(frag, msg_hdr_sz(msg));
2294 if (fragid == FIRST_FRAGMENT) {
2295 if (*head || skb_unclone(frag, GFP_ATOMIC))
2296 goto out_free;
2297 *head = frag;
2298 skb_frag_list_init(*head);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002299 return 0;
Erik Hugne3db0a192013-11-13 09:35:11 +01002300 } else if (*head &&
2301 skb_try_coalesce(*head, frag, &headstolen, &delta)) {
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002302 kfree_skb_partial(frag, headstolen);
2303 } else {
2304 if (!*head)
2305 goto out_free;
2306 if (!skb_has_frag_list(*head))
2307 skb_shinfo(*head)->frag_list = frag;
2308 else
2309 (*tail)->next = frag;
2310 *tail = frag;
2311 (*head)->truesize += frag->truesize;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002312 }
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002313 if (fragid == LAST_FRAGMENT) {
2314 *fbuf = *head;
2315 *tail = *head = NULL;
2316 return LINK_REASM_COMPLETE;
2317 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002318 return 0;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002319out_free:
2320 pr_warn_ratelimited("Link unable to reassemble fragmented message\n");
2321 kfree_skb(*fbuf);
2322 return LINK_REASM_ERROR;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002323}
2324
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002325static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002326{
Allan Stephens5413b4c2011-01-18 13:24:55 -05002327 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2328 return;
2329
Per Lidenb97bf3f2006-01-02 19:04:38 +01002330 l_ptr->tolerance = tolerance;
2331 l_ptr->continuity_interval =
2332 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2333 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2334}
2335
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002336void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337{
2338 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002339 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2340 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2341 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2342 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002343 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002344 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2345 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2346 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2347 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002348 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002349 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2350 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2351 /* FRAGMENT and LAST_FRAGMENT packets */
2352 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2353}
2354
2355/**
2356 * link_find_link - locate link by name
Ben Hutchings2c530402012-07-10 10:55:09 +00002357 * @name: ptr to link name string
2358 * @node: ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002359 *
Per Liden4323add2006-01-18 00:38:21 +01002360 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002361 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002362 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363 * Returns pointer to link (or 0 if invalid link name).
2364 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002365static struct tipc_link *link_find_link(const char *name,
2366 struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002367{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002368 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002369 struct tipc_node *n_ptr;
2370 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002371
Erik Hugnebbfbe472013-10-18 07:23:21 +02002372 list_for_each_entry(n_ptr, &tipc_node_list, list) {
2373 for (i = 0; i < MAX_BEARERS; i++) {
2374 l_ptr = n_ptr->links[i];
2375 if (l_ptr && !strcmp(l_ptr->name, name))
2376 goto found;
2377 }
2378 }
2379 l_ptr = NULL;
2380 n_ptr = NULL;
2381found:
2382 *node = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002383 return l_ptr;
2384}
2385
Allan Stephens5c216e12011-10-18 11:34:29 -04002386/**
2387 * link_value_is_valid -- validate proposed link tolerance/priority/window
2388 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002389 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2390 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002391 *
2392 * Returns 1 if value is within range, 0 if not.
2393 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002394static int link_value_is_valid(u16 cmd, u32 new_value)
2395{
2396 switch (cmd) {
2397 case TIPC_CMD_SET_LINK_TOL:
2398 return (new_value >= TIPC_MIN_LINK_TOL) &&
2399 (new_value <= TIPC_MAX_LINK_TOL);
2400 case TIPC_CMD_SET_LINK_PRI:
2401 return (new_value <= TIPC_MAX_LINK_PRI);
2402 case TIPC_CMD_SET_LINK_WINDOW:
2403 return (new_value >= TIPC_MIN_LINK_WIN) &&
2404 (new_value <= TIPC_MAX_LINK_WIN);
2405 }
2406 return 0;
2407}
2408
Allan Stephens5c216e12011-10-18 11:34:29 -04002409/**
2410 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002411 * @name: ptr to link, bearer, or media name
2412 * @new_value: new value of link, bearer, or media setting
2413 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002414 *
2415 * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
2416 *
2417 * Returns 0 if value updated and negative value on error.
2418 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002419static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2420{
2421 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002422 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002423 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002424 struct tipc_media *m_ptr;
Ying Xue636c0372013-10-18 07:23:20 +02002425 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002426
2427 l_ptr = link_find_link(name, &node);
2428 if (l_ptr) {
2429 /*
2430 * acquire node lock for tipc_link_send_proto_msg().
2431 * see "TIPC locking policy" in net.c.
2432 */
2433 tipc_node_lock(node);
2434 switch (cmd) {
2435 case TIPC_CMD_SET_LINK_TOL:
2436 link_set_supervision_props(l_ptr, new_value);
2437 tipc_link_send_proto_msg(l_ptr,
2438 STATE_MSG, 0, 0, new_value, 0, 0);
2439 break;
2440 case TIPC_CMD_SET_LINK_PRI:
2441 l_ptr->priority = new_value;
2442 tipc_link_send_proto_msg(l_ptr,
2443 STATE_MSG, 0, 0, 0, new_value, 0);
2444 break;
2445 case TIPC_CMD_SET_LINK_WINDOW:
2446 tipc_link_set_queue_limits(l_ptr, new_value);
2447 break;
Ying Xue636c0372013-10-18 07:23:20 +02002448 default:
2449 res = -EINVAL;
2450 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002451 }
2452 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002453 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002454 }
2455
2456 b_ptr = tipc_bearer_find(name);
2457 if (b_ptr) {
2458 switch (cmd) {
2459 case TIPC_CMD_SET_LINK_TOL:
2460 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002461 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002462 case TIPC_CMD_SET_LINK_PRI:
2463 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002464 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002465 case TIPC_CMD_SET_LINK_WINDOW:
2466 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002467 break;
2468 default:
2469 res = -EINVAL;
2470 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002471 }
Ying Xue636c0372013-10-18 07:23:20 +02002472 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002473 }
2474
2475 m_ptr = tipc_media_find(name);
2476 if (!m_ptr)
2477 return -ENODEV;
2478 switch (cmd) {
2479 case TIPC_CMD_SET_LINK_TOL:
2480 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002481 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002482 case TIPC_CMD_SET_LINK_PRI:
2483 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002484 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002485 case TIPC_CMD_SET_LINK_WINDOW:
2486 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002487 break;
2488 default:
2489 res = -EINVAL;
2490 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002491 }
Ying Xue636c0372013-10-18 07:23:20 +02002492 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002493}
2494
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002495struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002496 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002497{
2498 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002499 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002500 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002501
2502 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002503 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002504
2505 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2506 new_value = ntohl(args->value);
2507
Allan Stephens5c216e12011-10-18 11:34:29 -04002508 if (!link_value_is_valid(cmd, new_value))
2509 return tipc_cfg_reply_error_string(
2510 "cannot change, value invalid");
2511
Per Liden4323add2006-01-18 00:38:21 +01002512 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002513 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002514 (tipc_bclink_set_queue_limits(new_value) == 0))
2515 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002516 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002517 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002518 }
2519
Per Liden4323add2006-01-18 00:38:21 +01002520 read_lock_bh(&tipc_net_lock);
Allan Stephens5c216e12011-10-18 11:34:29 -04002521 res = link_cmd_set_value(args->name, new_value, cmd);
Per Liden4323add2006-01-18 00:38:21 +01002522 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002523 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002524 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002525
Per Liden4323add2006-01-18 00:38:21 +01002526 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002527}
2528
2529/**
2530 * link_reset_statistics - reset link statistics
2531 * @l_ptr: pointer to link
2532 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002533static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002534{
2535 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2536 l_ptr->stats.sent_info = l_ptr->next_out_no;
2537 l_ptr->stats.recv_info = l_ptr->next_in_no;
2538}
2539
Per Liden4323add2006-01-18 00:38:21 +01002540struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002541{
2542 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002543 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002544 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002545
2546 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002547 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002548
2549 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002550 if (!strcmp(link_name, tipc_bclink_name)) {
2551 if (tipc_bclink_reset_stats())
2552 return tipc_cfg_reply_error_string("link not found");
2553 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554 }
2555
Per Liden4323add2006-01-18 00:38:21 +01002556 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002557 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002558 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002559 read_unlock_bh(&tipc_net_lock);
2560 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002561 }
2562
Per Liden4323add2006-01-18 00:38:21 +01002563 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002564 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002565 tipc_node_unlock(node);
2566 read_unlock_bh(&tipc_net_lock);
2567 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002568}
2569
2570/**
2571 * percent - convert count to a percentage of total (rounding up or down)
2572 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002573static u32 percent(u32 count, u32 total)
2574{
2575 return (count * 100 + (total / 2)) / total;
2576}
2577
2578/**
Per Liden4323add2006-01-18 00:38:21 +01002579 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580 * @name: link name
2581 * @buf: print buffer area
2582 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002583 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002584 * Returns length of print buffer data string (or 0 if error)
2585 */
Per Liden4323add2006-01-18 00:38:21 +01002586static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002587{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002588 struct tipc_link *l;
2589 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002590 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002591 char *status;
2592 u32 profile_total = 0;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002593 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594
Per Liden4323add2006-01-18 00:38:21 +01002595 if (!strcmp(name, tipc_bclink_name))
2596 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002597
Per Liden4323add2006-01-18 00:38:21 +01002598 read_lock_bh(&tipc_net_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002599 l = link_find_link(name, &node);
2600 if (!l) {
Per Liden4323add2006-01-18 00:38:21 +01002601 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002602 return 0;
2603 }
Per Liden4323add2006-01-18 00:38:21 +01002604 tipc_node_lock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002605 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002606
Erik Hugnedc1aed32012-06-29 00:50:23 -04002607 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002608 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002609 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002610 status = "STANDBY";
2611 else
2612 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002613
2614 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2615 " %s MTU:%u Priority:%u Tolerance:%u ms"
2616 " Window:%u packets\n",
2617 l->name, status, l->max_pkt, l->priority,
2618 l->tolerance, l->queue_limit[0]);
2619
2620 ret += tipc_snprintf(buf + ret, buf_size - ret,
2621 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2622 l->next_in_no - s->recv_info, s->recv_fragments,
2623 s->recv_fragmented, s->recv_bundles,
2624 s->recv_bundled);
2625
2626 ret += tipc_snprintf(buf + ret, buf_size - ret,
2627 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2628 l->next_out_no - s->sent_info, s->sent_fragments,
2629 s->sent_fragmented, s->sent_bundles,
2630 s->sent_bundled);
2631
2632 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002633 if (!profile_total)
2634 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002635
2636 ret += tipc_snprintf(buf + ret, buf_size - ret,
2637 " TX profile sample:%u packets average:%u octets\n"
2638 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2639 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2640 s->msg_length_counts,
2641 s->msg_lengths_total / profile_total,
2642 percent(s->msg_length_profile[0], profile_total),
2643 percent(s->msg_length_profile[1], profile_total),
2644 percent(s->msg_length_profile[2], profile_total),
2645 percent(s->msg_length_profile[3], profile_total),
2646 percent(s->msg_length_profile[4], profile_total),
2647 percent(s->msg_length_profile[5], profile_total),
2648 percent(s->msg_length_profile[6], profile_total));
2649
2650 ret += tipc_snprintf(buf + ret, buf_size - ret,
2651 " RX states:%u probes:%u naks:%u defs:%u"
2652 " dups:%u\n", s->recv_states, s->recv_probes,
2653 s->recv_nacks, s->deferred_recv, s->duplicates);
2654
2655 ret += tipc_snprintf(buf + ret, buf_size - ret,
2656 " TX states:%u probes:%u naks:%u acks:%u"
2657 " dups:%u\n", s->sent_states, s->sent_probes,
2658 s->sent_nacks, s->sent_acks, s->retransmitted);
2659
2660 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002661 " Congestion link:%u Send queue"
2662 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002663 s->max_queue_sz, s->queue_sz_counts ?
2664 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002665
Per Liden4323add2006-01-18 00:38:21 +01002666 tipc_node_unlock(node);
2667 read_unlock_bh(&tipc_net_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002668 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002669}
2670
Per Liden4323add2006-01-18 00:38:21 +01002671struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002672{
2673 struct sk_buff *buf;
2674 struct tlv_desc *rep_tlv;
2675 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002676 int pb_len;
2677 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002678
2679 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002680 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002681
Erik Hugnedc1aed32012-06-29 00:50:23 -04002682 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002683 if (!buf)
2684 return NULL;
2685
2686 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002687 pb = TLV_DATA(rep_tlv);
2688 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002689 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002690 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002691 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002692 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002693 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002694 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002695 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002696 skb_put(buf, TLV_SPACE(str_len));
2697 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2698
2699 return buf;
2700}
2701
Per Lidenb97bf3f2006-01-02 19:04:38 +01002702/**
Per Liden4323add2006-01-18 00:38:21 +01002703 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002704 * @dest: network address of destination node
2705 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002706 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002707 * If no active link can be found, uses default maximum packet size.
2708 */
Per Liden4323add2006-01-18 00:38:21 +01002709u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710{
David S. Miller6c000552008-09-02 23:38:32 -07002711 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002712 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002713 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002714
Per Lidenb97bf3f2006-01-02 19:04:38 +01002715 if (dest == tipc_own_addr)
2716 return MAX_MSG_SIZE;
2717
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002718 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002719 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002720 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002721 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002722 l_ptr = n_ptr->active_links[selector & 1];
2723 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002724 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002725 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002726 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002727 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002728 return res;
2729}
2730
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002731static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002732{
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002733 pr_info("%s Link %x<%s>:", str, l_ptr->addr, l_ptr->b_ptr->name);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002734
Per Lidenb97bf3f2006-01-02 19:04:38 +01002735 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002736 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002737 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002738 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002739 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002740 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002741 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002742 pr_cont(":WW\n");
2743 else
2744 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002745}