blob: d5f4005f388f06f96f195c02d773c3e0f962736f [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 Maloyc1336ee2015-03-13 16:08:08 -04004 * Copyright (c) 1996-2007, 2012-2015, 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"
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -040038#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "link.h"
Richard Alpe7be57fc2014-11-20 10:29:12 +010040#include "bcast.h"
Jon Paul Maloy9816f062014-05-14 05:39:15 -040041#include "socket.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#include "discover.h"
Richard Alpe0655f6a2014-11-20 10:29:07 +010044#include "netlink.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010045
Ying Xue796c75d2013-06-17 10:54:48 -040046#include <linux/pkt_sched.h>
47
Erik Hugne2cf8aa12012-06-29 00:16:37 -040048/*
49 * Error message prefixes
50 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -040051static const char *link_co_err = "Link tunneling error, ";
Erik Hugne2cf8aa12012-06-29 00:16:37 -040052static const char *link_rst_msg = "Resetting link ";
53static const char *link_unk_evt = "Unknown link event ";
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Richard Alpe7be57fc2014-11-20 10:29:12 +010055static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
56 [TIPC_NLA_LINK_UNSPEC] = { .type = NLA_UNSPEC },
57 [TIPC_NLA_LINK_NAME] = {
58 .type = NLA_STRING,
59 .len = TIPC_MAX_LINK_NAME
60 },
61 [TIPC_NLA_LINK_MTU] = { .type = NLA_U32 },
62 [TIPC_NLA_LINK_BROADCAST] = { .type = NLA_FLAG },
63 [TIPC_NLA_LINK_UP] = { .type = NLA_FLAG },
64 [TIPC_NLA_LINK_ACTIVE] = { .type = NLA_FLAG },
65 [TIPC_NLA_LINK_PROP] = { .type = NLA_NESTED },
66 [TIPC_NLA_LINK_STATS] = { .type = NLA_NESTED },
67 [TIPC_NLA_LINK_RX] = { .type = NLA_U32 },
68 [TIPC_NLA_LINK_TX] = { .type = NLA_U32 }
69};
70
Richard Alpe0655f6a2014-11-20 10:29:07 +010071/* Properties valid for media, bearar and link */
72static const struct nla_policy tipc_nl_prop_policy[TIPC_NLA_PROP_MAX + 1] = {
73 [TIPC_NLA_PROP_UNSPEC] = { .type = NLA_UNSPEC },
74 [TIPC_NLA_PROP_PRIO] = { .type = NLA_U32 },
75 [TIPC_NLA_PROP_TOL] = { .type = NLA_U32 },
76 [TIPC_NLA_PROP_WIN] = { .type = NLA_U32 }
77};
78
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090079/*
Jon Paul Maloyd9992972015-07-16 16:54:31 -040080 * Interval between NACKs when packets arrive out of order
81 */
82#define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
83/*
Allan Stephensa686e682008-06-04 17:29:39 -070084 * Out-of-range value for link session numbers
85 */
Jon Paul Maloyd3504c32015-07-16 16:54:25 -040086#define WILDCARD_SESSION 0x10000
Allan Stephensa686e682008-06-04 17:29:39 -070087
Jon Paul Maloyd3504c32015-07-16 16:54:25 -040088/* State value stored in 'failover_pkts'
Per Lidenb97bf3f2006-01-02 19:04:38 +010089 */
Jon Paul Maloydff29b12015-04-02 09:33:01 -040090#define FIRST_FAILOVER 0xffffu
Per Lidenb97bf3f2006-01-02 19:04:38 +010091
Jon Paul Maloyd3504c32015-07-16 16:54:25 -040092/* Link FSM states and events:
93 */
94enum {
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -040095 TIPC_LINK_WORKING,
96 TIPC_LINK_PROBING,
97 TIPC_LINK_RESETTING,
98 TIPC_LINK_ESTABLISHING
Jon Paul Maloyd3504c32015-07-16 16:54:25 -040099};
100
101enum {
102 PEER_RESET_EVT = RESET_MSG,
103 ACTIVATE_EVT = ACTIVATE_MSG,
104 TRAFFIC_EVT, /* Any other valid msg from peer */
105 SILENCE_EVT /* Peer was silent during last timer interval*/
106};
107
108/* Link FSM state checking routines
109 */
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400110static int link_working(struct tipc_link *l)
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400111{
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400112 return l->state == TIPC_LINK_WORKING;
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400113}
114
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400115static int link_probing(struct tipc_link *l)
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400116{
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400117 return l->state == TIPC_LINK_PROBING;
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400118}
119
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400120static int link_resetting(struct tipc_link *l)
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400121{
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400122 return l->state == TIPC_LINK_RESETTING;
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400123}
124
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400125static int link_establishing(struct tipc_link *l)
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400126{
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400127 return l->state == TIPC_LINK_ESTABLISHING;
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400128}
129
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400130static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
131 struct sk_buff_head *xmitq);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -0400132static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
133 u16 rcvgap, int tolerance, int priority,
134 struct sk_buff_head *xmitq);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500135static void link_reset_statistics(struct tipc_link *l_ptr);
136static void link_print(struct tipc_link *l_ptr, const char *str);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400137static void tipc_link_build_bcast_sync_msg(struct tipc_link *l,
138 struct sk_buff_head *xmitq);
Ying Xue247f0f32014-02-18 16:06:46 +0800139static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400140static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb);
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500141static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb);
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -0400142
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800144 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500146int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147{
148 if (!l_ptr)
149 return 0;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400150 return link_working(l_ptr) || link_probing(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151}
152
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400153int tipc_link_is_active(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154{
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400155 struct tipc_node *n = l->owner;
156
157 return (node_active_link(n, 0) == l) || (node_active_link(n, 1) == l);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158}
159
160/**
Per Liden4323add2006-01-18 00:38:21 +0100161 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500162 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900165 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 * Returns pointer to link.
167 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500168struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500169 struct tipc_bearer *b_ptr,
Jon Paul Maloyd39bbd42015-07-16 16:54:21 -0400170 const struct tipc_media_addr *media_addr,
171 struct sk_buff_head *inputq,
172 struct sk_buff_head *namedq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173{
Ying Xue34747532015-01-09 15:27:10 +0800174 struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500175 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 struct tipc_msg *msg;
177 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500178 char addr_string[16];
179 u32 peer = n_ptr->addr;
180
Holger Brunck0372bf52014-11-14 18:33:19 +0100181 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500182 tipc_addr_string_fill(addr_string, n_ptr->addr);
Jon Paul Maloya97b9d32015-05-14 10:46:15 -0400183 pr_err("Cannot establish %uth link to %s. Max %u allowed.\n",
184 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500185 return NULL;
186 }
187
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -0400188 if (n_ptr->links[b_ptr->identity].link) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500189 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400190 pr_err("Attempt to establish second link on <%s> to %s\n",
191 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500192 return NULL;
193 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700195 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400197 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 return NULL;
199 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500201 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400202 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Ying Xue34747532015-01-09 15:27:10 +0800203 tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
204 tipc_node(tn->own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205 if_name,
206 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400207 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500209 l_ptr->owner = n_ptr;
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400210 l_ptr->peer_session = WILDCARD_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800211 l_ptr->bearer_id = b_ptr->identity;
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -0400212 l_ptr->tolerance = b_ptr->tolerance;
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400213 l_ptr->snd_nxt = 1;
214 l_ptr->rcv_nxt = 1;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400215 l_ptr->state = TIPC_LINK_RESETTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216
217 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
218 msg = l_ptr->pmsg;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500219 tipc_msg_init(tn->own_addr, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
Ying Xue34747532015-01-09 15:27:10 +0800220 l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Ying Xuebafa29e2015-01-09 15:27:12 +0800222 msg_set_session(msg, (tn->random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 msg_set_bearer_id(msg, b_ptr->identity);
224 strcpy((char *)msg_data(msg), if_name);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800225 l_ptr->net_plane = b_ptr->net_plane;
Jon Paul Maloyed193ec2015-04-02 09:33:02 -0400226 l_ptr->advertised_mtu = b_ptr->mtu;
227 l_ptr->mtu = l_ptr->advertised_mtu;
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -0400228 l_ptr->priority = b_ptr->priority;
229 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Jon Paul Maloya97b9d32015-05-14 10:46:15 -0400230 l_ptr->snd_nxt = 1;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400231 __skb_queue_head_init(&l_ptr->transmq);
232 __skb_queue_head_init(&l_ptr->backlogq);
233 __skb_queue_head_init(&l_ptr->deferdq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500234 skb_queue_head_init(&l_ptr->wakeupq);
Jon Paul Maloyd39bbd42015-07-16 16:54:21 -0400235 l_ptr->inputq = inputq;
236 l_ptr->namedq = namedq;
237 skb_queue_head_init(l_ptr->inputq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 link_reset_statistics(l_ptr);
Allan Stephens37b9c082011-02-28 11:32:27 -0500239 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 return l_ptr;
241}
242
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400243/* tipc_link_build_bcast_sync_msg() - synchronize broadcast link endpoints.
244 *
245 * Give a newly added peer node the sequence number where it should
246 * start receiving and acking broadcast packets.
247 */
248static void tipc_link_build_bcast_sync_msg(struct tipc_link *l,
249 struct sk_buff_head *xmitq)
250{
251 struct sk_buff *skb;
252 struct sk_buff_head list;
Jon Maloy5a4c3552015-07-29 18:28:01 -0400253 u16 last_sent;
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400254
255 skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
256 0, l->addr, link_own_addr(l), 0, 0, 0);
257 if (!skb)
258 return;
Jon Maloy5a4c3552015-07-29 18:28:01 -0400259 last_sent = tipc_bclink_get_last_sent(l->owner->net);
260 msg_set_last_bcast(buf_msg(skb), last_sent);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400261 __skb_queue_head_init(&list);
262 __skb_queue_tail(&list, skb);
263 tipc_link_xmit(l, &list, xmitq);
264}
265
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266/**
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400267 * tipc_link_fsm_evt - link finite state machine
268 * @l: pointer to link
269 * @evt: state machine event to be processed
270 * @xmitq: queue to prepend created protocol message, if any
271 */
272static int tipc_link_fsm_evt(struct tipc_link *l, int evt,
273 struct sk_buff_head *xmitq)
274{
275 int mtyp = 0, rc = 0;
276 struct tipc_link *pl;
277 enum {
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400278 LINK_RESET = 1,
279 LINK_ACTIVATE = (1 << 1),
280 SND_PROBE = (1 << 2),
281 SND_STATE = (1 << 3),
282 SND_RESET = (1 << 4),
283 SND_ACTIVATE = (1 << 5),
284 SND_BCAST_SYNC = (1 << 6)
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400285 } actions = 0;
286
287 if (l->exec_mode == TIPC_LINK_BLOCKED)
288 return rc;
289
290 switch (l->state) {
291 case TIPC_LINK_WORKING:
292 switch (evt) {
293 case TRAFFIC_EVT:
294 case ACTIVATE_EVT:
295 break;
296 case SILENCE_EVT:
297 l->state = TIPC_LINK_PROBING;
298 actions |= SND_PROBE;
299 break;
300 case PEER_RESET_EVT:
301 actions |= LINK_RESET | SND_ACTIVATE;
302 break;
303 default:
304 pr_debug("%s%u WORKING\n", link_unk_evt, evt);
305 }
306 break;
307 case TIPC_LINK_PROBING:
308 switch (evt) {
309 case TRAFFIC_EVT:
310 case ACTIVATE_EVT:
311 l->state = TIPC_LINK_WORKING;
312 break;
313 case PEER_RESET_EVT:
314 actions |= LINK_RESET | SND_ACTIVATE;
315 break;
316 case SILENCE_EVT:
317 if (l->silent_intv_cnt <= l->abort_limit) {
318 actions |= SND_PROBE;
319 break;
320 }
321 actions |= LINK_RESET | SND_RESET;
322 break;
323 default:
324 pr_err("%s%u PROBING\n", link_unk_evt, evt);
325 }
326 break;
327 case TIPC_LINK_RESETTING:
328 switch (evt) {
329 case TRAFFIC_EVT:
330 break;
331 case ACTIVATE_EVT:
332 pl = node_active_link(l->owner, 0);
333 if (pl && link_probing(pl))
334 break;
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400335 l->state = TIPC_LINK_WORKING;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400336 actions |= LINK_ACTIVATE;
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400337 if (!l->owner->working_links)
338 actions |= SND_BCAST_SYNC;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400339 break;
340 case PEER_RESET_EVT:
341 l->state = TIPC_LINK_ESTABLISHING;
342 actions |= SND_ACTIVATE;
343 break;
344 case SILENCE_EVT:
345 actions |= SND_RESET;
346 break;
347 default:
348 pr_err("%s%u in RESETTING\n", link_unk_evt, evt);
349 }
350 break;
351 case TIPC_LINK_ESTABLISHING:
352 switch (evt) {
353 case TRAFFIC_EVT:
354 case ACTIVATE_EVT:
355 pl = node_active_link(l->owner, 0);
356 if (pl && link_probing(pl))
357 break;
Jon Paul Maloycbeb83c2015-07-30 18:24:15 -0400358 l->state = TIPC_LINK_WORKING;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400359 actions |= LINK_ACTIVATE;
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400360 if (!l->owner->working_links)
361 actions |= SND_BCAST_SYNC;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400362 break;
363 case PEER_RESET_EVT:
364 break;
365 case SILENCE_EVT:
366 actions |= SND_ACTIVATE;
367 break;
368 default:
369 pr_err("%s%u ESTABLISHING\n", link_unk_evt, evt);
370 }
371 break;
372 default:
373 pr_err("Unknown link state %u/%u\n", l->state, evt);
374 }
375
376 /* Perform actions as decided by FSM */
377 if (actions & LINK_RESET) {
378 l->exec_mode = TIPC_LINK_BLOCKED;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400379 rc = TIPC_LINK_DOWN_EVT;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400380 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400381 if (actions & LINK_ACTIVATE)
382 rc = TIPC_LINK_UP_EVT;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400383 if (actions & (SND_STATE | SND_PROBE))
384 mtyp = STATE_MSG;
385 if (actions & SND_RESET)
386 mtyp = RESET_MSG;
387 if (actions & SND_ACTIVATE)
388 mtyp = ACTIVATE_MSG;
389 if (actions & (SND_PROBE | SND_STATE | SND_RESET | SND_ACTIVATE))
390 tipc_link_build_proto_msg(l, mtyp, actions & SND_PROBE,
391 0, 0, 0, xmitq);
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400392 if (actions & SND_BCAST_SYNC)
393 tipc_link_build_bcast_sync_msg(l, xmitq);
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400394 return rc;
395}
396
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400397/* link_profile_stats - update statistical profiling of traffic
398 */
399static void link_profile_stats(struct tipc_link *l)
400{
401 struct sk_buff *skb;
402 struct tipc_msg *msg;
403 int length;
404
405 /* Update counters used in statistical profiling of send traffic */
406 l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
407 l->stats.queue_sz_counts++;
408
409 skb = skb_peek(&l->transmq);
410 if (!skb)
411 return;
412 msg = buf_msg(skb);
413 length = msg_size(msg);
414
415 if (msg_user(msg) == MSG_FRAGMENTER) {
416 if (msg_type(msg) != FIRST_FRAGMENT)
417 return;
418 length = msg_size(msg_get_wrapped(msg));
419 }
420 l->stats.msg_lengths_total += length;
421 l->stats.msg_length_counts++;
422 if (length <= 64)
423 l->stats.msg_length_profile[0]++;
424 else if (length <= 256)
425 l->stats.msg_length_profile[1]++;
426 else if (length <= 1024)
427 l->stats.msg_length_profile[2]++;
428 else if (length <= 4096)
429 l->stats.msg_length_profile[3]++;
430 else if (length <= 16384)
431 l->stats.msg_length_profile[4]++;
432 else if (length <= 32768)
433 l->stats.msg_length_profile[5]++;
434 else
435 l->stats.msg_length_profile[6]++;
436}
437
438/* tipc_link_timeout - perform periodic task as instructed from node timeout
439 */
440int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
441{
442 int rc = 0;
443
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400444 if (l->exec_mode == TIPC_LINK_BLOCKED)
445 return rc;
446
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400447 link_profile_stats(l);
448 if (l->silent_intv_cnt)
449 rc = tipc_link_fsm_evt(l, SILENCE_EVT, xmitq);
450 else if (link_working(l) && tipc_bclink_acks_missing(l->owner))
451 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, xmitq);
452 l->silent_intv_cnt++;
453 return rc;
454}
455
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400456/**
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400457 * link_schedule_user - schedule a message sender for wakeup after congestion
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400458 * @link: congested link
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400459 * @list: message that was attempted sent
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400460 * Create pseudo msg to send back to user when congestion abates
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400461 * Does not consume buffer list
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 */
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400463static int link_schedule_user(struct tipc_link *link, struct sk_buff_head *list)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464{
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400465 struct tipc_msg *msg = buf_msg(skb_peek(list));
466 int imp = msg_importance(msg);
467 u32 oport = msg_origport(msg);
468 u32 addr = link_own_addr(link);
469 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400471 /* This really cannot happen... */
472 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
473 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400474 return -ENOBUFS;
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400475 }
476 /* Non-blocking sender: */
477 if (TIPC_SKB_CB(skb_peek(list))->wakeup_pending)
478 return -ELINKCONG;
479
480 /* Create and schedule wakeup pseudo message */
481 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
482 addr, addr, oport, 0, 0);
483 if (!skb)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400484 return -ENOBUFS;
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400485 TIPC_SKB_CB(skb)->chain_sz = skb_queue_len(list);
486 TIPC_SKB_CB(skb)->chain_imp = imp;
487 skb_queue_tail(&link->wakeupq, skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400488 link->stats.link_congs++;
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400489 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490}
491
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400492/**
493 * link_prepare_wakeup - prepare users for wakeup after congestion
494 * @link: congested link
495 * Move a number of waiting users, as permitted by available space in
496 * the send queue, from link wait queue to node wait queue for wakeup
497 */
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400498void link_prepare_wakeup(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499{
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400500 int pnd[TIPC_SYSTEM_IMPORTANCE + 1] = {0,};
501 int imp, lim;
Ying Xue58d78b32014-11-26 11:41:51 +0800502 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400504 skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
505 imp = TIPC_SKB_CB(skb)->chain_imp;
506 lim = l->window + l->backlog[imp].limit;
507 pnd[imp] += TIPC_SKB_CB(skb)->chain_sz;
508 if ((pnd[imp] + l->backlog[imp].len) >= lim)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 break;
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400510 skb_unlink(skb, &l->wakeupq);
Jon Paul Maloyd39bbd42015-07-16 16:54:21 -0400511 skb_queue_tail(l->inputq, skb);
512 l->owner->inputq = l->inputq;
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400513 l->owner->action_flags |= TIPC_MSG_EVT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515}
516
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900517/**
Per Liden4323add2006-01-18 00:38:21 +0100518 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 * @l_ptr: pointer to link
520 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500521void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400523 kfree_skb(l_ptr->reasm_buf);
524 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525}
526
Jon Paul Maloy7d967b62015-06-28 09:44:44 -0400527void tipc_link_purge_backlog(struct tipc_link *l)
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400528{
529 __skb_queue_purge(&l->backlogq);
530 l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
531 l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
532 l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
533 l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
534 l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
535}
536
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500538 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 * @l_ptr: pointer to link
540 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500541void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542{
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400543 __skb_queue_purge(&l_ptr->deferdq);
544 __skb_queue_purge(&l_ptr->transmq);
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400545 tipc_link_purge_backlog(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100546 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547}
548
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400549void tipc_link_reset(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550{
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400551 struct tipc_node *owner = l->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400553 l->state = TIPC_LINK_RESETTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554
Allan Stephensa686e682008-06-04 17:29:39 -0700555 /* Link is down, accept any session */
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400556 l->peer_session = WILDCARD_SESSION;
557
558 /* If peer is up, it only accepts an incremented session number */
559 msg_set_session(l->pmsg, msg_session(l->pmsg) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560
Jon Paul Maloyed193ec2015-04-02 09:33:02 -0400561 /* Prepare for renewed mtu size negotiation */
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400562 l->mtu = l->advertised_mtu;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500564 /* Clean up all queues, except inputq: */
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400565 __skb_queue_purge(&l->transmq);
566 __skb_queue_purge(&l->deferdq);
Jon Paul Maloye6441ba2015-03-09 16:16:22 -0400567 if (!owner->inputq)
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400568 owner->inputq = l->inputq;
569 skb_queue_splice_init(&l->wakeupq, owner->inputq);
Jon Paul Maloye6441ba2015-03-09 16:16:22 -0400570 if (!skb_queue_empty(owner->inputq))
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500571 owner->action_flags |= TIPC_MSG_EVT;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400572
573 tipc_link_purge_backlog(l);
574 kfree_skb(l->reasm_buf);
575 kfree_skb(l->failover_reasm_skb);
576 l->reasm_buf = NULL;
577 l->failover_reasm_skb = NULL;
578 l->rcv_unacked = 0;
579 l->snd_nxt = 1;
580 l->rcv_nxt = 1;
581 l->silent_intv_cnt = 0;
582 l->stats.recv_info = 0;
583 l->stale_count = 0;
584 link_reset_statistics(l);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585}
586
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400588 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500589 * @link: link to use
Ying Xuea6ca1092014-11-26 11:41:55 +0800590 * @list: chain of buffers containing message
591 *
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400592 * Consumes the buffer chain, except when returning an error code,
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400593 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
594 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500595 */
Ying Xue7f9f95d2015-01-09 15:27:06 +0800596int __tipc_link_xmit(struct net *net, struct tipc_link *link,
597 struct sk_buff_head *list)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500598{
Ying Xuea6ca1092014-11-26 11:41:55 +0800599 struct tipc_msg *msg = buf_msg(skb_peek(list));
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400600 unsigned int maxwin = link->window;
Jon Paul Maloyf21e8972015-05-14 10:46:17 -0400601 unsigned int i, imp = msg_importance(msg);
Jon Paul Maloyed193ec2015-04-02 09:33:02 -0400602 uint mtu = link->mtu;
Jon Paul Maloya97b9d32015-05-14 10:46:15 -0400603 u16 ack = mod(link->rcv_nxt - 1);
604 u16 seqno = link->snd_nxt;
Jon Paul Maloye4bf4f72015-05-14 10:46:14 -0400605 u16 bc_last_in = link->owner->bclink.last_in;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500606 struct tipc_media_addr *addr = &link->media_addr;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400607 struct sk_buff_head *transmq = &link->transmq;
608 struct sk_buff_head *backlogq = &link->backlogq;
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400609 struct sk_buff *skb, *bskb;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500610
Jon Paul Maloyf21e8972015-05-14 10:46:17 -0400611 /* Match msg importance against this and all higher backlog limits: */
612 for (i = imp; i <= TIPC_SYSTEM_IMPORTANCE; i++) {
613 if (unlikely(link->backlog[i].len >= link->backlog[i].limit))
614 return link_schedule_user(link, list);
615 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400616 if (unlikely(msg_size(msg) > mtu))
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500617 return -EMSGSIZE;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400618
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400619 /* Prepare each packet for sending, and add to relevant queue: */
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400620 while (skb_queue_len(list)) {
621 skb = skb_peek(list);
Ying Xue58dc55f2014-11-26 11:41:52 +0800622 msg = buf_msg(skb);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400623 msg_set_seqno(msg, seqno);
624 msg_set_ack(msg, ack);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500625 msg_set_bcast_ack(msg, bc_last_in);
626
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400627 if (likely(skb_queue_len(transmq) < maxwin)) {
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400628 __skb_dequeue(list);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400629 __skb_queue_tail(transmq, skb);
630 tipc_bearer_send(net, link->bearer_id, skb, addr);
631 link->rcv_unacked = 0;
632 seqno++;
633 continue;
634 }
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400635 if (tipc_msg_bundle(skb_peek_tail(backlogq), msg, mtu)) {
636 kfree_skb(__skb_dequeue(list));
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500637 link->stats.sent_bundled++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500638 continue;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400639 }
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400640 if (tipc_msg_make_bundle(&bskb, msg, mtu, link->addr)) {
641 kfree_skb(__skb_dequeue(list));
642 __skb_queue_tail(backlogq, bskb);
643 link->backlog[msg_importance(buf_msg(bskb))].len++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500644 link->stats.sent_bundled++;
645 link->stats.sent_bundles++;
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400646 continue;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500647 }
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400648 link->backlog[imp].len += skb_queue_len(list);
649 skb_queue_splice_tail_init(list, backlogq);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500650 }
Jon Paul Maloya97b9d32015-05-14 10:46:15 -0400651 link->snd_nxt = seqno;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500652 return 0;
653}
654
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400655/**
656 * tipc_link_xmit(): enqueue buffer list according to queue situation
657 * @link: link to use
658 * @list: chain of buffers containing message
659 * @xmitq: returned list of packets to be sent by caller
660 *
661 * Consumes the buffer chain, except when returning -ELINKCONG,
662 * since the caller then may want to make more send attempts.
663 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
664 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
665 */
666int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
667 struct sk_buff_head *xmitq)
668{
669 struct tipc_msg *hdr = buf_msg(skb_peek(list));
670 unsigned int maxwin = l->window;
671 unsigned int i, imp = msg_importance(hdr);
672 unsigned int mtu = l->mtu;
673 u16 ack = l->rcv_nxt - 1;
674 u16 seqno = l->snd_nxt;
675 u16 bc_last_in = l->owner->bclink.last_in;
676 struct sk_buff_head *transmq = &l->transmq;
677 struct sk_buff_head *backlogq = &l->backlogq;
678 struct sk_buff *skb, *_skb, *bskb;
679
680 /* Match msg importance against this and all higher backlog limits: */
681 for (i = imp; i <= TIPC_SYSTEM_IMPORTANCE; i++) {
682 if (unlikely(l->backlog[i].len >= l->backlog[i].limit))
683 return link_schedule_user(l, list);
684 }
685 if (unlikely(msg_size(hdr) > mtu))
686 return -EMSGSIZE;
687
688 /* Prepare each packet for sending, and add to relevant queue: */
689 while (skb_queue_len(list)) {
690 skb = skb_peek(list);
691 hdr = buf_msg(skb);
692 msg_set_seqno(hdr, seqno);
693 msg_set_ack(hdr, ack);
694 msg_set_bcast_ack(hdr, bc_last_in);
695
696 if (likely(skb_queue_len(transmq) < maxwin)) {
697 _skb = skb_clone(skb, GFP_ATOMIC);
698 if (!_skb)
699 return -ENOBUFS;
700 __skb_dequeue(list);
701 __skb_queue_tail(transmq, skb);
702 __skb_queue_tail(xmitq, _skb);
703 l->rcv_unacked = 0;
704 seqno++;
705 continue;
706 }
707 if (tipc_msg_bundle(skb_peek_tail(backlogq), hdr, mtu)) {
708 kfree_skb(__skb_dequeue(list));
709 l->stats.sent_bundled++;
710 continue;
711 }
712 if (tipc_msg_make_bundle(&bskb, hdr, mtu, l->addr)) {
713 kfree_skb(__skb_dequeue(list));
714 __skb_queue_tail(backlogq, bskb);
715 l->backlog[msg_importance(buf_msg(bskb))].len++;
716 l->stats.sent_bundled++;
717 l->stats.sent_bundles++;
718 continue;
719 }
720 l->backlog[imp].len += skb_queue_len(list);
721 skb_queue_splice_tail_init(list, backlogq);
722 }
723 l->snd_nxt = seqno;
724 return 0;
725}
726
Jon Maloyc64f7a62012-11-16 13:51:31 +0800727/*
Ying Xue247f0f32014-02-18 16:06:46 +0800728 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800729 * Receive the sequence number where we should start receiving and
730 * acking broadcast packets from a newly added peer node, and open
731 * up for reception of such packets.
732 *
733 * Called with node locked
734 */
Ying Xue247f0f32014-02-18 16:06:46 +0800735static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800736{
737 struct tipc_msg *msg = buf_msg(buf);
738
739 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
740 n->bclink.recv_permitted = true;
741 kfree_skb(buf);
742}
743
744/*
Ying Xue47b4c9a82014-11-26 11:41:48 +0800745 * tipc_link_push_packets - push unsent packets to bearer
746 *
747 * Push out the unsent messages of a link where congestion
748 * has abated. Node is locked.
749 *
750 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400752void tipc_link_push_packets(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753{
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400754 struct sk_buff *skb;
Ying Xue47b4c9a82014-11-26 11:41:48 +0800755 struct tipc_msg *msg;
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400756 u16 seqno = link->snd_nxt;
Jon Paul Maloya97b9d32015-05-14 10:46:15 -0400757 u16 ack = mod(link->rcv_nxt - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400759 while (skb_queue_len(&link->transmq) < link->window) {
760 skb = __skb_dequeue(&link->backlogq);
761 if (!skb)
Ying Xue47b4c9a82014-11-26 11:41:48 +0800762 break;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400763 msg = buf_msg(skb);
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400764 link->backlog[msg_importance(msg)].len--;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400765 msg_set_ack(msg, ack);
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400766 msg_set_seqno(msg, seqno);
767 seqno = mod(seqno + 1);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400768 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
769 link->rcv_unacked = 0;
770 __skb_queue_tail(&link->transmq, skb);
771 tipc_bearer_send(link->owner->net, link->bearer_id,
772 skb, &link->media_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 }
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -0400774 link->snd_nxt = seqno;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775}
776
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400777void tipc_link_advance_backlog(struct tipc_link *l, struct sk_buff_head *xmitq)
778{
779 struct sk_buff *skb, *_skb;
780 struct tipc_msg *hdr;
781 u16 seqno = l->snd_nxt;
782 u16 ack = l->rcv_nxt - 1;
783
784 while (skb_queue_len(&l->transmq) < l->window) {
785 skb = skb_peek(&l->backlogq);
786 if (!skb)
787 break;
788 _skb = skb_clone(skb, GFP_ATOMIC);
789 if (!_skb)
790 break;
791 __skb_dequeue(&l->backlogq);
792 hdr = buf_msg(skb);
793 l->backlog[msg_importance(hdr)].len--;
794 __skb_queue_tail(&l->transmq, skb);
795 __skb_queue_tail(xmitq, _skb);
796 msg_set_ack(hdr, ack);
797 msg_set_seqno(hdr, seqno);
798 msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
799 l->rcv_unacked = 0;
800 seqno++;
801 }
802 l->snd_nxt = seqno;
803}
804
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500805static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400806 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700807{
808 struct tipc_msg *msg = buf_msg(buf);
Ying Xue1da46562015-01-09 15:27:07 +0800809 struct net *net = l_ptr->owner->net;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700810
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400811 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700812
813 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700814 /* Handle failure on standard link */
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -0400815 link_print(l_ptr, "Resetting link ");
816 pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
817 msg_user(msg), msg_type(msg), msg_size(msg),
818 msg_errcode(msg));
819 pr_info("sqno %u, prev: %x, src: %x\n",
820 msg_seqno(msg), msg_prevnode(msg), msg_orignode(msg));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700821 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700822 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700823 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700824 char addr_string[16];
825
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400826 pr_info("Msg seq number: %u, ", msg_seqno(msg));
827 pr_cont("Outstanding acks: %lu\n",
828 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700829
Ying Xue1da46562015-01-09 15:27:07 +0800830 n_ptr = tipc_bclink_retransmit_to(net);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700831
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000832 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400833 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800834 pr_info("Reception permitted: %d, Acked: %u\n",
835 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400836 n_ptr->bclink.acked);
837 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
838 n_ptr->bclink.last_in,
839 n_ptr->bclink.oos_state,
840 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700841
Ying Xueb952b2b2015-03-26 18:10:23 +0800842 n_ptr->action_flags |= TIPC_BCAST_RESET;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700843 l_ptr->stale_count = 0;
844 }
845}
846
Ying Xue58dc55f2014-11-26 11:41:52 +0800847void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
Per Liden4323add2006-01-18 00:38:21 +0100848 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849{
850 struct tipc_msg *msg;
851
Ying Xue58dc55f2014-11-26 11:41:52 +0800852 if (!skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700853 return;
854
Ying Xue58dc55f2014-11-26 11:41:52 +0800855 msg = buf_msg(skb);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900856
Erik Hugne512137e2013-12-06 10:08:00 -0500857 /* Detect repeated retransmit failures */
Jon Paul Maloya97b9d32015-05-14 10:46:15 -0400858 if (l_ptr->last_retransm == msg_seqno(msg)) {
Erik Hugne512137e2013-12-06 10:08:00 -0500859 if (++l_ptr->stale_count > 100) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800860 link_retransmit_failure(l_ptr, skb);
Erik Hugne512137e2013-12-06 10:08:00 -0500861 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700862 }
863 } else {
Jon Paul Maloya97b9d32015-05-14 10:46:15 -0400864 l_ptr->last_retransm = msg_seqno(msg);
Erik Hugne512137e2013-12-06 10:08:00 -0500865 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 }
Allan Stephensd356eeb2006-06-25 23:40:01 -0700867
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400868 skb_queue_walk_from(&l_ptr->transmq, skb) {
869 if (!retransmits)
Ying Xue58dc55f2014-11-26 11:41:52 +0800870 break;
871 msg = buf_msg(skb);
Jon Paul Maloya97b9d32015-05-14 10:46:15 -0400872 msg_set_ack(msg, mod(l_ptr->rcv_nxt - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800874 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
875 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800876 retransmits--;
877 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879}
880
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400881static int tipc_link_retransm(struct tipc_link *l, int retransm,
882 struct sk_buff_head *xmitq)
883{
884 struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
885 struct tipc_msg *hdr;
886
887 if (!skb)
888 return 0;
889
890 /* Detect repeated retransmit failures on same packet */
891 if (likely(l->last_retransm != buf_seqno(skb))) {
892 l->last_retransm = buf_seqno(skb);
893 l->stale_count = 1;
894 } else if (++l->stale_count > 100) {
895 link_retransmit_failure(l, skb);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400896 l->exec_mode = TIPC_LINK_BLOCKED;
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400897 return TIPC_LINK_DOWN_EVT;
898 }
899 skb_queue_walk(&l->transmq, skb) {
900 if (!retransm)
901 return 0;
902 hdr = buf_msg(skb);
903 _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
904 if (!_skb)
905 return 0;
906 hdr = buf_msg(_skb);
907 msg_set_ack(hdr, l->rcv_nxt - 1);
908 msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
909 _skb->priority = TC_PRIO_CONTROL;
910 __skb_queue_tail(xmitq, _skb);
911 retransm--;
912 l->stats.retransmitted++;
913 }
914 return 0;
915}
916
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500917/* tipc_data_input - deliver data and name distr msgs to upper layer
Erik Hugne7ae934b2014-07-01 10:22:40 +0200918 *
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500919 * Consumes buffer if message is of right type
Erik Hugne7ae934b2014-07-01 10:22:40 +0200920 * Node lock must be held
921 */
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500922static bool tipc_data_input(struct tipc_link *link, struct sk_buff *skb)
Erik Hugne7ae934b2014-07-01 10:22:40 +0200923{
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500924 struct tipc_node *node = link->owner;
925 struct tipc_msg *msg = buf_msg(skb);
926 u32 dport = msg_destport(msg);
Erik Hugne7ae934b2014-07-01 10:22:40 +0200927
928 switch (msg_user(msg)) {
929 case TIPC_LOW_IMPORTANCE:
930 case TIPC_MEDIUM_IMPORTANCE:
931 case TIPC_HIGH_IMPORTANCE:
932 case TIPC_CRITICAL_IMPORTANCE:
933 case CONN_MANAGER:
Jon Paul Maloyd39bbd42015-07-16 16:54:21 -0400934 if (tipc_skb_queue_tail(link->inputq, skb, dport)) {
935 node->inputq = link->inputq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500936 node->action_flags |= TIPC_MSG_EVT;
937 }
938 return true;
Erik Hugne7ae934b2014-07-01 10:22:40 +0200939 case NAME_DISTRIBUTOR:
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500940 node->bclink.recv_permitted = true;
Jon Paul Maloyd39bbd42015-07-16 16:54:21 -0400941 node->namedq = link->namedq;
942 skb_queue_tail(link->namedq, skb);
943 if (skb_queue_len(link->namedq) == 1)
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500944 node->action_flags |= TIPC_NAMED_MSG_EVT;
945 return true;
Erik Hugne7ae934b2014-07-01 10:22:40 +0200946 case MSG_BUNDLER:
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400947 case TUNNEL_PROTOCOL:
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500948 case MSG_FRAGMENTER:
949 case BCAST_PROTOCOL:
950 return false;
951 default:
952 pr_warn("Dropping received illegal msg type\n");
953 kfree_skb(skb);
954 return false;
955 };
956}
957
958/* tipc_link_input - process packet that has passed link protocol check
959 *
960 * Consumes buffer
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500961 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400962static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb)
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500963{
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400964 struct tipc_node *node = l->owner;
965 struct tipc_msg *hdr = buf_msg(skb);
966 struct sk_buff **reasm_skb = &l->reasm_buf;
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500967 struct sk_buff *iskb;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400968 int usr = msg_user(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -0400969 int rc = 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400970 int pos = 0;
971 int ipos = 0;
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500972
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400973 if (unlikely(usr == TUNNEL_PROTOCOL)) {
974 if (msg_type(hdr) == SYNCH_MSG) {
975 __skb_queue_purge(&l->deferdq);
976 goto drop;
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -0400977 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400978 if (!tipc_msg_extract(skb, &iskb, &ipos))
979 return rc;
980 kfree_skb(skb);
981 skb = iskb;
982 hdr = buf_msg(skb);
983 if (less(msg_seqno(hdr), l->drop_point))
984 goto drop;
985 if (tipc_data_input(l, skb))
986 return rc;
987 usr = msg_user(hdr);
988 reasm_skb = &l->failover_reasm_skb;
989 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500990
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400991 if (usr == MSG_BUNDLER) {
992 l->stats.recv_bundles++;
993 l->stats.recv_bundled += msg_msgcnt(hdr);
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500994 while (tipc_msg_extract(skb, &iskb, &pos))
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400995 tipc_data_input(l, iskb);
996 return rc;
997 } else if (usr == MSG_FRAGMENTER) {
998 l->stats.recv_fragments++;
999 if (tipc_buf_append(reasm_skb, &skb)) {
1000 l->stats.recv_fragmented++;
1001 tipc_data_input(l, skb);
1002 } else if (!*reasm_skb) {
1003 l->exec_mode = TIPC_LINK_BLOCKED;
1004 l->state = TIPC_LINK_RESETTING;
1005 rc = TIPC_LINK_DOWN_EVT;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001006 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001007 return rc;
1008 } else if (usr == BCAST_PROTOCOL) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001009 tipc_link_sync_rcv(node, skb);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001010 return rc;
1011 }
1012drop:
1013 kfree_skb(skb);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001014 return rc;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001015}
1016
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001017static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
1018{
1019 bool released = false;
1020 struct sk_buff *skb, *tmp;
1021
1022 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1023 if (more(buf_seqno(skb), acked))
1024 break;
1025 __skb_unlink(skb, &l->transmq);
1026 kfree_skb(skb);
1027 released = true;
1028 }
1029 return released;
1030}
1031
1032/* tipc_link_rcv - process TIPC packets/messages arriving from off-node
1033 * @link: the link that should handle the message
1034 * @skb: TIPC packet
1035 * @xmitq: queue to place packets to be sent after this call
1036 */
1037int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1038 struct sk_buff_head *xmitq)
1039{
1040 struct sk_buff_head *arrvq = &l->deferdq;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001041 struct tipc_msg *hdr;
1042 u16 seqno, rcv_nxt;
1043 int rc = 0;
1044
1045 if (unlikely(!__tipc_skb_queue_sorted(arrvq, skb))) {
1046 if (!(skb_queue_len(arrvq) % TIPC_NACK_INTV))
1047 tipc_link_build_proto_msg(l, STATE_MSG, 0,
1048 0, 0, 0, xmitq);
1049 return rc;
1050 }
1051
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001052 while ((skb = skb_peek(arrvq))) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001053 hdr = buf_msg(skb);
1054
1055 /* Verify and update link state */
1056 if (unlikely(msg_user(hdr) == LINK_PROTOCOL)) {
1057 __skb_dequeue(arrvq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001058 rc = tipc_link_proto_rcv(l, skb, xmitq);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001059 continue;
1060 }
1061
1062 if (unlikely(!link_working(l))) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001063 rc = tipc_link_fsm_evt(l, TRAFFIC_EVT, xmitq);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001064 if (!link_working(l)) {
1065 kfree_skb(__skb_dequeue(arrvq));
1066 return rc;
1067 }
1068 }
1069
1070 l->silent_intv_cnt = 0;
1071
1072 /* Forward queues and wake up waiting users */
1073 if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
1074 tipc_link_advance_backlog(l, xmitq);
1075 if (unlikely(!skb_queue_empty(&l->wakeupq)))
1076 link_prepare_wakeup(l);
1077 }
1078
1079 /* Defer reception if there is a gap in the sequence */
1080 seqno = msg_seqno(hdr);
1081 rcv_nxt = l->rcv_nxt;
1082 if (unlikely(less(rcv_nxt, seqno))) {
1083 l->stats.deferred_recv++;
1084 return rc;
1085 }
1086
1087 __skb_dequeue(arrvq);
1088
1089 /* Drop if packet already received */
1090 if (unlikely(more(rcv_nxt, seqno))) {
1091 l->stats.duplicates++;
1092 kfree_skb(skb);
1093 return rc;
1094 }
1095
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001096 /* Packet can be delivered */
1097 l->rcv_nxt++;
1098 l->stats.recv_info++;
1099 if (unlikely(!tipc_data_input(l, skb)))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001100 rc = tipc_link_input(l, skb);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001101
1102 /* Ack at regular intervals */
1103 if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN)) {
1104 l->rcv_unacked = 0;
1105 l->stats.sent_acks++;
1106 tipc_link_build_proto_msg(l, STATE_MSG,
1107 0, 0, 0, 0, xmitq);
1108 }
1109 }
1110 return rc;
1111}
1112
Erik Hugne7ae934b2014-07-01 10:22:40 +02001113/**
Allan Stephens8809b252011-10-25 10:44:35 -04001114 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1115 *
1116 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001118u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119{
Ying Xuebc6fecd2014-11-26 11:41:53 +08001120 struct sk_buff *skb1;
Jon Paul Maloye4bf4f72015-05-14 10:46:14 -04001121 u16 seq_no = buf_seqno(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122
1123 /* Empty queue ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001124 if (skb_queue_empty(list)) {
1125 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 return 1;
1127 }
1128
1129 /* Last ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001130 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1131 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 return 1;
1133 }
1134
Allan Stephens8809b252011-10-25 10:44:35 -04001135 /* Locate insertion point in queue, then insert; discard if duplicate */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001136 skb_queue_walk(list, skb1) {
Jon Paul Maloye4bf4f72015-05-14 10:46:14 -04001137 u16 curr_seqno = buf_seqno(skb1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138
Allan Stephens8809b252011-10-25 10:44:35 -04001139 if (seq_no == curr_seqno) {
Ying Xuebc6fecd2014-11-26 11:41:53 +08001140 kfree_skb(skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001141 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 }
Allan Stephens8809b252011-10-25 10:44:35 -04001143
1144 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 break;
Allan Stephens8809b252011-10-25 10:44:35 -04001146 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147
Ying Xuebc6fecd2014-11-26 11:41:53 +08001148 __skb_queue_before(list, skb1, skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001149 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150}
1151
Allan Stephens8809b252011-10-25 10:44:35 -04001152/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 * Send protocol message to the other endpoint.
1154 */
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001155void tipc_link_proto_xmit(struct tipc_link *l, u32 msg_typ, int probe_msg,
Jon Paul Maloyed193ec2015-04-02 09:33:02 -04001156 u32 gap, u32 tolerance, u32 priority)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157{
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001158 struct sk_buff *skb = NULL;
1159 struct sk_buff_head xmitq;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001161 __skb_queue_head_init(&xmitq);
1162 tipc_link_build_proto_msg(l, msg_typ, probe_msg, gap,
1163 tolerance, priority, &xmitq);
1164 skb = __skb_dequeue(&xmitq);
1165 if (!skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 return;
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001167 tipc_bearer_send(l->owner->net, l->bearer_id, skb, &l->media_addr);
1168 l->rcv_unacked = 0;
1169 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170}
1171
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001172/* tipc_link_build_proto_msg: prepare link protocol message for transmission
1173 */
1174static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
1175 u16 rcvgap, int tolerance, int priority,
1176 struct sk_buff_head *xmitq)
1177{
1178 struct sk_buff *skb = NULL;
1179 struct tipc_msg *hdr = l->pmsg;
1180 u16 snd_nxt = l->snd_nxt;
1181 u16 rcv_nxt = l->rcv_nxt;
1182 u16 rcv_last = rcv_nxt - 1;
1183 int node_up = l->owner->bclink.recv_permitted;
1184
1185 /* Don't send protocol message during reset or link failover */
1186 if (l->exec_mode == TIPC_LINK_BLOCKED)
1187 return;
1188
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001189 msg_set_type(hdr, mtyp);
1190 msg_set_net_plane(hdr, l->net_plane);
1191 msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
1192 msg_set_last_bcast(hdr, tipc_bclink_get_last_sent(l->owner->net));
1193 msg_set_link_tolerance(hdr, tolerance);
1194 msg_set_linkprio(hdr, priority);
1195 msg_set_redundant_link(hdr, node_up);
1196 msg_set_seq_gap(hdr, 0);
1197
1198 /* Compatibility: created msg must not be in sequence with pkt flow */
1199 msg_set_seqno(hdr, snd_nxt + U16_MAX / 2);
1200
1201 if (mtyp == STATE_MSG) {
1202 if (!tipc_link_is_up(l))
1203 return;
1204 msg_set_next_sent(hdr, snd_nxt);
1205
1206 /* Override rcvgap if there are packets in deferred queue */
1207 if (!skb_queue_empty(&l->deferdq))
1208 rcvgap = buf_seqno(skb_peek(&l->deferdq)) - rcv_nxt;
1209 if (rcvgap) {
1210 msg_set_seq_gap(hdr, rcvgap);
1211 l->stats.sent_nacks++;
1212 }
1213 msg_set_ack(hdr, rcv_last);
1214 msg_set_probe(hdr, probe);
1215 if (probe)
1216 l->stats.sent_probes++;
1217 l->stats.sent_states++;
1218 } else {
1219 /* RESET_MSG or ACTIVATE_MSG */
1220 msg_set_max_pkt(hdr, l->advertised_mtu);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001221 msg_set_ack(hdr, l->rcv_nxt - 1);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001222 msg_set_next_sent(hdr, 1);
1223 }
1224 skb = tipc_buf_acquire(msg_size(hdr));
1225 if (!skb)
1226 return;
1227 skb_copy_to_linear_data(skb, hdr, msg_size(hdr));
1228 skb->priority = TC_PRIO_CONTROL;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001229 __skb_queue_tail(xmitq, skb);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001230}
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001232/* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
1233 * with contents of the link's tranmsit and backlog queues.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001235void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1236 int mtyp, struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237{
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001238 struct sk_buff *skb, *tnlskb;
1239 struct tipc_msg *hdr, tnlhdr;
1240 struct sk_buff_head *queue = &l->transmq;
1241 struct sk_buff_head tmpxq, tnlq;
1242 u16 pktlen, pktcnt, seqno = l->snd_nxt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001244 if (!tnl)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 return;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001246
1247 skb_queue_head_init(&tnlq);
1248 skb_queue_head_init(&tmpxq);
1249
1250 /* At least one packet required for safe algorithm => add dummy */
1251 skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1252 BASIC_H_SIZE, 0, l->addr, link_own_addr(l),
1253 0, 0, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +08001254 if (!skb) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001255 pr_warn("%sunable to create tunnel packet\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001257 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001258 skb_queue_tail(&tnlq, skb);
1259 tipc_link_xmit(l, &tnlq, &tmpxq);
1260 __skb_queue_purge(&tmpxq);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001262 /* Initialize reusable tunnel packet header */
1263 tipc_msg_init(link_own_addr(l), &tnlhdr, TUNNEL_PROTOCOL,
1264 mtyp, INT_H_SIZE, l->addr);
1265 pktcnt = skb_queue_len(&l->transmq) + skb_queue_len(&l->backlogq);
1266 msg_set_msgcnt(&tnlhdr, pktcnt);
1267 msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1268tnl:
1269 /* Wrap each packet into a tunnel packet */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001270 skb_queue_walk(queue, skb) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001271 hdr = buf_msg(skb);
1272 if (queue == &l->backlogq)
1273 msg_set_seqno(hdr, seqno++);
1274 pktlen = msg_size(hdr);
1275 msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
1276 tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE);
1277 if (!tnlskb) {
1278 pr_warn("%sunable to send packet\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 return;
1280 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001281 skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
1282 skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
1283 __skb_queue_tail(&tnlq, tnlskb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001285 if (queue != &l->backlogq) {
1286 queue = &l->backlogq;
1287 goto tnl;
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -04001288 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001290 tipc_link_xmit(tnl, &tnlq, xmitq);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001291
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001292 if (mtyp == FAILOVER_MSG) {
1293 tnl->drop_point = l->rcv_nxt;
1294 tnl->failover_reasm_skb = l->reasm_buf;
1295 l->reasm_buf = NULL;
1296 l->exec_mode = TIPC_LINK_BLOCKED;
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001297 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298}
1299
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001300/* tipc_link_proto_rcv(): receive link level protocol message :
1301 * Note that network plane id propagates through the network, and may
1302 * change at any time. The node with lowest numerical id determines
1303 * network plane
1304 */
1305static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
1306 struct sk_buff_head *xmitq)
1307{
1308 struct tipc_msg *hdr = buf_msg(skb);
1309 u16 rcvgap = 0;
1310 u16 nacked_gap = msg_seq_gap(hdr);
1311 u16 peers_snd_nxt = msg_next_sent(hdr);
1312 u16 peers_tol = msg_link_tolerance(hdr);
1313 u16 peers_prio = msg_linkprio(hdr);
1314 char *if_name;
1315 int rc = 0;
1316
1317 if (l->exec_mode == TIPC_LINK_BLOCKED)
1318 goto exit;
1319
1320 if (link_own_addr(l) > msg_prevnode(hdr))
1321 l->net_plane = msg_net_plane(hdr);
1322
1323 switch (msg_type(hdr)) {
1324 case RESET_MSG:
1325
1326 /* Ignore duplicate RESET with old session number */
1327 if ((less_eq(msg_session(hdr), l->peer_session)) &&
1328 (l->peer_session != WILDCARD_SESSION))
1329 break;
1330 /* fall thru' */
1331 case ACTIVATE_MSG:
1332
1333 /* Complete own link name with peer's interface name */
1334 if_name = strrchr(l->name, ':') + 1;
1335 if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
1336 break;
1337 if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
1338 break;
1339 strncpy(if_name, msg_data(hdr), TIPC_MAX_IF_NAME);
1340
1341 /* Update own tolerance if peer indicates a non-zero value */
1342 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
1343 l->tolerance = peers_tol;
1344
1345 /* Update own priority if peer's priority is higher */
1346 if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
1347 l->priority = peers_prio;
1348
1349 l->peer_session = msg_session(hdr);
1350 l->peer_bearer_id = msg_bearer_id(hdr);
1351 rc = tipc_link_fsm_evt(l, msg_type(hdr), xmitq);
1352 if (l->mtu > msg_max_pkt(hdr))
1353 l->mtu = msg_max_pkt(hdr);
1354 break;
1355 case STATE_MSG:
1356 /* Update own tolerance if peer indicates a non-zero value */
1357 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL))
1358 l->tolerance = peers_tol;
1359
1360 l->silent_intv_cnt = 0;
1361 l->stats.recv_states++;
1362 if (msg_probe(hdr))
1363 l->stats.recv_probes++;
1364 rc = tipc_link_fsm_evt(l, TRAFFIC_EVT, xmitq);
1365 if (!tipc_link_is_up(l))
1366 break;
1367
1368 /* Has peer sent packets we haven't received yet ? */
1369 if (more(peers_snd_nxt, l->rcv_nxt))
1370 rcvgap = peers_snd_nxt - l->rcv_nxt;
1371 if (rcvgap || (msg_probe(hdr)))
1372 tipc_link_build_proto_msg(l, STATE_MSG, 0, rcvgap,
Jon Paul Maloy16040892015-07-21 06:42:28 -04001373 0, 0, xmitq);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001374 tipc_link_release_pkts(l, msg_ack(hdr));
1375
1376 /* If NACK, retransmit will now start at right position */
1377 if (nacked_gap) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001378 rc = tipc_link_retransm(l, nacked_gap, xmitq);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001379 l->stats.recv_nacks++;
1380 }
1381 tipc_link_advance_backlog(l, xmitq);
1382 if (unlikely(!skb_queue_empty(&l->wakeupq)))
1383 link_prepare_wakeup(l);
1384 }
1385exit:
1386 kfree_skb(skb);
1387 return rc;
1388}
1389
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -04001390void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391{
Jon Paul Maloyed193ec2015-04-02 09:33:02 -04001392 int max_bulk = TIPC_MAX_PUBLICATIONS / (l->mtu / ITEM_SIZE);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001393
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -04001394 l->window = win;
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04001395 l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
1396 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
1397 l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
1398 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
1399 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400}
1401
Jon Paul Maloye099e862014-02-13 17:29:18 -05001402/* tipc_link_find_owner - locate owner node of link by link's name
Ying Xuef2f98002015-01-09 15:27:05 +08001403 * @net: the applicable net namespace
Jon Paul Maloye099e862014-02-13 17:29:18 -05001404 * @name: pointer to link name string
1405 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001406 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001407 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 */
Ying Xuef2f98002015-01-09 15:27:05 +08001409static struct tipc_node *tipc_link_find_owner(struct net *net,
1410 const char *link_name,
Jon Paul Maloye099e862014-02-13 17:29:18 -05001411 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412{
Ying Xuef2f98002015-01-09 15:27:05 +08001413 struct tipc_net *tn = net_generic(net, tipc_net_id);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001414 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001415 struct tipc_node *n_ptr;
Fabian Frederick886eaa12014-12-25 12:05:50 +01001416 struct tipc_node *found_node = NULL;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001417 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418
Jon Paul Maloye099e862014-02-13 17:29:18 -05001419 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001420 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +08001421 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001422 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001423 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001424 l_ptr = n_ptr->links[i].link;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001425 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1426 *bearer_id = i;
1427 found_node = n_ptr;
1428 break;
1429 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001430 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001431 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001432 if (found_node)
1433 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001434 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001435 rcu_read_unlock();
1436
Jon Paul Maloye099e862014-02-13 17:29:18 -05001437 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438}
1439
Allan Stephens5c216e12011-10-18 11:34:29 -04001440/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441 * link_reset_statistics - reset link statistics
1442 * @l_ptr: pointer to link
1443 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001444static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445{
1446 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
Jon Paul Maloya97b9d32015-05-14 10:46:15 -04001447 l_ptr->stats.sent_info = l_ptr->snd_nxt;
1448 l_ptr->stats.recv_info = l_ptr->rcv_nxt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449}
1450
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001451static void link_print(struct tipc_link *l, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452{
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001453 struct sk_buff *hskb = skb_peek(&l->transmq);
1454 u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt;
1455 u16 tail = l->snd_nxt - 1;
Ying Xue7a2f7d12014-04-21 10:55:46 +08001456
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001457 pr_info("%s Link <%s>:", str, l->name);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001458
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001459 if (link_probing(l))
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -04001460 pr_cont(":P\n");
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001461 else if (link_establishing(l))
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -04001462 pr_cont(":E\n");
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001463 else if (link_resetting(l))
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -04001464 pr_cont(":R\n");
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001465 else if (link_working(l))
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -04001466 pr_cont(":W\n");
Paul Gortmaker5deedde2012-07-11 19:27:56 -04001467 else
1468 pr_cont("\n");
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04001469
1470 pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
1471 skb_queue_len(&l->transmq), head, tail,
1472 skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473}
Richard Alpe0655f6a2014-11-20 10:29:07 +01001474
1475/* Parse and validate nested (link) properties valid for media, bearer and link
1476 */
1477int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
1478{
1479 int err;
1480
1481 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
1482 tipc_nl_prop_policy);
1483 if (err)
1484 return err;
1485
1486 if (props[TIPC_NLA_PROP_PRIO]) {
1487 u32 prio;
1488
1489 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1490 if (prio > TIPC_MAX_LINK_PRI)
1491 return -EINVAL;
1492 }
1493
1494 if (props[TIPC_NLA_PROP_TOL]) {
1495 u32 tol;
1496
1497 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1498 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
1499 return -EINVAL;
1500 }
1501
1502 if (props[TIPC_NLA_PROP_WIN]) {
1503 u32 win;
1504
1505 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1506 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
1507 return -EINVAL;
1508 }
1509
1510 return 0;
1511}
Richard Alpe7be57fc2014-11-20 10:29:12 +01001512
Richard Alpef96ce7a2014-11-20 10:29:13 +01001513int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
1514{
1515 int err;
1516 int res = 0;
1517 int bearer_id;
1518 char *name;
1519 struct tipc_link *link;
1520 struct tipc_node *node;
1521 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Richard Alpe37e2d482015-02-09 09:50:08 +01001522 struct net *net = sock_net(skb->sk);
Richard Alpef96ce7a2014-11-20 10:29:13 +01001523
1524 if (!info->attrs[TIPC_NLA_LINK])
1525 return -EINVAL;
1526
1527 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1528 info->attrs[TIPC_NLA_LINK],
1529 tipc_nl_link_policy);
1530 if (err)
1531 return err;
1532
1533 if (!attrs[TIPC_NLA_LINK_NAME])
1534 return -EINVAL;
1535
1536 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1537
Richard Alpe670f4f82015-05-06 13:58:55 +02001538 if (strcmp(name, tipc_bclink_name) == 0)
1539 return tipc_nl_bc_link_set(net, attrs);
1540
Ying Xuef2f98002015-01-09 15:27:05 +08001541 node = tipc_link_find_owner(net, name, &bearer_id);
Richard Alpef96ce7a2014-11-20 10:29:13 +01001542 if (!node)
1543 return -EINVAL;
1544
1545 tipc_node_lock(node);
1546
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001547 link = node->links[bearer_id].link;
Richard Alpef96ce7a2014-11-20 10:29:13 +01001548 if (!link) {
1549 res = -EINVAL;
1550 goto out;
1551 }
1552
1553 if (attrs[TIPC_NLA_LINK_PROP]) {
1554 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1555
1556 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1557 props);
1558 if (err) {
1559 res = err;
1560 goto out;
1561 }
1562
1563 if (props[TIPC_NLA_PROP_TOL]) {
1564 u32 tol;
1565
1566 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
Jon Paul Maloy8a1577c2015-07-16 16:54:29 -04001567 link->tolerance = tol;
Jon Paul Maloyed193ec2015-04-02 09:33:02 -04001568 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0);
Richard Alpef96ce7a2014-11-20 10:29:13 +01001569 }
1570 if (props[TIPC_NLA_PROP_PRIO]) {
1571 u32 prio;
1572
1573 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1574 link->priority = prio;
Jon Paul Maloyed193ec2015-04-02 09:33:02 -04001575 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio);
Richard Alpef96ce7a2014-11-20 10:29:13 +01001576 }
1577 if (props[TIPC_NLA_PROP_WIN]) {
1578 u32 win;
1579
1580 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1581 tipc_link_set_queue_limits(link, win);
1582 }
1583 }
1584
1585out:
1586 tipc_node_unlock(node);
1587
1588 return res;
1589}
Richard Alped8182802014-11-24 11:10:29 +01001590
1591static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01001592{
1593 int i;
1594 struct nlattr *stats;
1595
1596 struct nla_map {
1597 u32 key;
1598 u32 val;
1599 };
1600
1601 struct nla_map map[] = {
1602 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
1603 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
1604 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
1605 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
1606 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
1607 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
1608 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
1609 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
1610 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
1611 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
1612 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
1613 s->msg_length_counts : 1},
1614 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
1615 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
1616 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
1617 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
1618 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
1619 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
1620 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
1621 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
1622 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
1623 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
1624 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
1625 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
1626 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
1627 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
1628 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
1629 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
1630 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
1631 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
1632 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
1633 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
1634 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
1635 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
1636 (s->accu_queue_sz / s->queue_sz_counts) : 0}
1637 };
1638
1639 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
1640 if (!stats)
1641 return -EMSGSIZE;
1642
1643 for (i = 0; i < ARRAY_SIZE(map); i++)
1644 if (nla_put_u32(skb, map[i].key, map[i].val))
1645 goto msg_full;
1646
1647 nla_nest_end(skb, stats);
1648
1649 return 0;
1650msg_full:
1651 nla_nest_cancel(skb, stats);
1652
1653 return -EMSGSIZE;
1654}
1655
1656/* Caller should hold appropriate locks to protect the link */
Ying Xue34747532015-01-09 15:27:10 +08001657static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
Nicolas Dichtelf2f67392015-04-28 18:33:50 +02001658 struct tipc_link *link, int nlflags)
Richard Alpe7be57fc2014-11-20 10:29:12 +01001659{
1660 int err;
1661 void *hdr;
1662 struct nlattr *attrs;
1663 struct nlattr *prop;
Ying Xue34747532015-01-09 15:27:10 +08001664 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001665
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001666 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Nicolas Dichtelf2f67392015-04-28 18:33:50 +02001667 nlflags, TIPC_NL_LINK_GET);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001668 if (!hdr)
1669 return -EMSGSIZE;
1670
1671 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
1672 if (!attrs)
1673 goto msg_full;
1674
1675 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
1676 goto attr_msg_full;
1677 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
Ying Xue34747532015-01-09 15:27:10 +08001678 tipc_cluster_mask(tn->own_addr)))
Richard Alpe7be57fc2014-11-20 10:29:12 +01001679 goto attr_msg_full;
Jon Paul Maloyed193ec2015-04-02 09:33:02 -04001680 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
Richard Alpe7be57fc2014-11-20 10:29:12 +01001681 goto attr_msg_full;
Jon Paul Maloya97b9d32015-05-14 10:46:15 -04001682 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->rcv_nxt))
Richard Alpe7be57fc2014-11-20 10:29:12 +01001683 goto attr_msg_full;
Jon Paul Maloya97b9d32015-05-14 10:46:15 -04001684 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->snd_nxt))
Richard Alpe7be57fc2014-11-20 10:29:12 +01001685 goto attr_msg_full;
1686
1687 if (tipc_link_is_up(link))
1688 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
1689 goto attr_msg_full;
1690 if (tipc_link_is_active(link))
1691 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
1692 goto attr_msg_full;
1693
1694 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
1695 if (!prop)
1696 goto attr_msg_full;
1697 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
1698 goto prop_msg_full;
1699 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
1700 goto prop_msg_full;
1701 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04001702 link->window))
Richard Alpe7be57fc2014-11-20 10:29:12 +01001703 goto prop_msg_full;
1704 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
1705 goto prop_msg_full;
1706 nla_nest_end(msg->skb, prop);
1707
1708 err = __tipc_nl_add_stats(msg->skb, &link->stats);
1709 if (err)
1710 goto attr_msg_full;
1711
1712 nla_nest_end(msg->skb, attrs);
1713 genlmsg_end(msg->skb, hdr);
1714
1715 return 0;
1716
1717prop_msg_full:
1718 nla_nest_cancel(msg->skb, prop);
1719attr_msg_full:
1720 nla_nest_cancel(msg->skb, attrs);
1721msg_full:
1722 genlmsg_cancel(msg->skb, hdr);
1723
1724 return -EMSGSIZE;
1725}
1726
1727/* Caller should hold node lock */
Ying Xue34747532015-01-09 15:27:10 +08001728static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
1729 struct tipc_node *node, u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01001730{
1731 u32 i;
1732 int err;
1733
1734 for (i = *prev_link; i < MAX_BEARERS; i++) {
1735 *prev_link = i;
1736
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001737 if (!node->links[i].link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01001738 continue;
1739
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001740 err = __tipc_nl_add_link(net, msg,
1741 node->links[i].link, NLM_F_MULTI);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001742 if (err)
1743 return err;
1744 }
1745 *prev_link = 0;
1746
1747 return 0;
1748}
1749
1750int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
1751{
Ying Xuef2f98002015-01-09 15:27:05 +08001752 struct net *net = sock_net(skb->sk);
1753 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001754 struct tipc_node *node;
1755 struct tipc_nl_msg msg;
1756 u32 prev_node = cb->args[0];
1757 u32 prev_link = cb->args[1];
1758 int done = cb->args[2];
1759 int err;
1760
1761 if (done)
1762 return 0;
1763
1764 msg.skb = skb;
1765 msg.portid = NETLINK_CB(cb->skb).portid;
1766 msg.seq = cb->nlh->nlmsg_seq;
1767
1768 rcu_read_lock();
Richard Alpe7be57fc2014-11-20 10:29:12 +01001769 if (prev_node) {
Ying Xuef2f98002015-01-09 15:27:05 +08001770 node = tipc_node_find(net, prev_node);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001771 if (!node) {
1772 /* We never set seq or call nl_dump_check_consistent()
1773 * this means that setting prev_seq here will cause the
1774 * consistence check to fail in the netlink callback
1775 * handler. Resulting in the last NLMSG_DONE message
1776 * having the NLM_F_DUMP_INTR flag set.
1777 */
1778 cb->prev_seq = 1;
1779 goto out;
1780 }
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001781 tipc_node_put(node);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001782
Ying Xuef2f98002015-01-09 15:27:05 +08001783 list_for_each_entry_continue_rcu(node, &tn->node_list,
1784 list) {
Richard Alpe7be57fc2014-11-20 10:29:12 +01001785 tipc_node_lock(node);
Ying Xue34747532015-01-09 15:27:10 +08001786 err = __tipc_nl_add_node_links(net, &msg, node,
1787 &prev_link);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001788 tipc_node_unlock(node);
1789 if (err)
1790 goto out;
1791
1792 prev_node = node->addr;
1793 }
1794 } else {
Ying Xue1da46562015-01-09 15:27:07 +08001795 err = tipc_nl_add_bc_link(net, &msg);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001796 if (err)
1797 goto out;
1798
Ying Xuef2f98002015-01-09 15:27:05 +08001799 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe7be57fc2014-11-20 10:29:12 +01001800 tipc_node_lock(node);
Ying Xue34747532015-01-09 15:27:10 +08001801 err = __tipc_nl_add_node_links(net, &msg, node,
1802 &prev_link);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001803 tipc_node_unlock(node);
1804 if (err)
1805 goto out;
1806
1807 prev_node = node->addr;
1808 }
1809 }
1810 done = 1;
1811out:
1812 rcu_read_unlock();
1813
1814 cb->args[0] = prev_node;
1815 cb->args[1] = prev_link;
1816 cb->args[2] = done;
1817
1818 return skb->len;
1819}
1820
1821int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
1822{
Ying Xuef2f98002015-01-09 15:27:05 +08001823 struct net *net = genl_info_net(info);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001824 struct tipc_nl_msg msg;
Richard Alpe7be57fc2014-11-20 10:29:12 +01001825 char *name;
Richard Alpe7be57fc2014-11-20 10:29:12 +01001826 int err;
1827
Richard Alpe7be57fc2014-11-20 10:29:12 +01001828 msg.portid = info->snd_portid;
1829 msg.seq = info->snd_seq;
1830
Richard Alpe670f4f82015-05-06 13:58:55 +02001831 if (!info->attrs[TIPC_NLA_LINK_NAME])
1832 return -EINVAL;
1833 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
1834
1835 msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1836 if (!msg.skb)
1837 return -ENOMEM;
1838
1839 if (strcmp(name, tipc_bclink_name) == 0) {
1840 err = tipc_nl_add_bc_link(net, &msg);
1841 if (err) {
1842 nlmsg_free(msg.skb);
1843 return err;
1844 }
1845 } else {
1846 int bearer_id;
1847 struct tipc_node *node;
1848 struct tipc_link *link;
1849
1850 node = tipc_link_find_owner(net, name, &bearer_id);
1851 if (!node)
1852 return -EINVAL;
1853
1854 tipc_node_lock(node);
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001855 link = node->links[bearer_id].link;
Richard Alpe670f4f82015-05-06 13:58:55 +02001856 if (!link) {
1857 tipc_node_unlock(node);
1858 nlmsg_free(msg.skb);
1859 return -EINVAL;
1860 }
1861
1862 err = __tipc_nl_add_link(net, &msg, link, 0);
1863 tipc_node_unlock(node);
1864 if (err) {
1865 nlmsg_free(msg.skb);
1866 return err;
1867 }
Richard Alpe7be57fc2014-11-20 10:29:12 +01001868 }
1869
Richard Alpe670f4f82015-05-06 13:58:55 +02001870 return genlmsg_reply(msg.skb, info);
Richard Alpe7be57fc2014-11-20 10:29:12 +01001871}
Richard Alpeae363422014-11-20 10:29:14 +01001872
1873int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
1874{
1875 int err;
1876 char *link_name;
1877 unsigned int bearer_id;
1878 struct tipc_link *link;
1879 struct tipc_node *node;
1880 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Richard Alpe18178772015-02-09 09:50:09 +01001881 struct net *net = sock_net(skb->sk);
Richard Alpeae363422014-11-20 10:29:14 +01001882
1883 if (!info->attrs[TIPC_NLA_LINK])
1884 return -EINVAL;
1885
1886 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1887 info->attrs[TIPC_NLA_LINK],
1888 tipc_nl_link_policy);
1889 if (err)
1890 return err;
1891
1892 if (!attrs[TIPC_NLA_LINK_NAME])
1893 return -EINVAL;
1894
1895 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1896
1897 if (strcmp(link_name, tipc_bclink_name) == 0) {
Ying Xue1da46562015-01-09 15:27:07 +08001898 err = tipc_bclink_reset_stats(net);
Richard Alpeae363422014-11-20 10:29:14 +01001899 if (err)
1900 return err;
1901 return 0;
1902 }
1903
Ying Xuef2f98002015-01-09 15:27:05 +08001904 node = tipc_link_find_owner(net, link_name, &bearer_id);
Richard Alpeae363422014-11-20 10:29:14 +01001905 if (!node)
1906 return -EINVAL;
1907
1908 tipc_node_lock(node);
1909
Jon Paul Maloy9d13ec62015-07-16 16:54:19 -04001910 link = node->links[bearer_id].link;
Richard Alpeae363422014-11-20 10:29:14 +01001911 if (!link) {
1912 tipc_node_unlock(node);
1913 return -EINVAL;
1914 }
1915
1916 link_reset_statistics(link);
1917
1918 tipc_node_unlock(node);
1919
1920 return 0;
1921}