blob: 7b027e99a5c945e2c96d7c95581905322a195897 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/port.c: TIPC port code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 1992-2007, 2014, Ericsson AB
Ying Xue198d73b2013-06-17 10:54:42 -04005 * Copyright (c) 2004-2008, 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"
38#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_table.h"
Jon Paul Maloy8826cde2014-03-12 11:31:09 -040041#include "socket.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042
43/* Connection management: */
44#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
45#define CONFIRMED 0
46#define PROBING 1
47
48#define MAX_REJECT_SIZE 1024
49
Ingo Molnar34af9462006-06-27 02:53:55 -070050DEFINE_SPINLOCK(tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010051
Per Liden4323add2006-01-18 00:38:21 +010052static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010053static void port_handle_node_down(unsigned long ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050054static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
55static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
Per Lidenb97bf3f2006-01-02 19:04:38 +010056static void port_timeout(unsigned long ref);
57
Ben Hutchings2c530402012-07-10 10:55:09 +000058/**
Allan Stephensf0712e862012-04-17 18:42:28 -040059 * tipc_port_peer_msg - verify message was sent by connected port's peer
60 *
61 * Handles cases where the node's network address has changed from
62 * the default of <0.0.0> to its configured setting.
63 */
Allan Stephensf0712e862012-04-17 18:42:28 -040064int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
65{
66 u32 peernode;
67 u32 orignode;
68
Jon Paul Maloyf9fef182014-03-12 11:31:08 -040069 if (msg_origport(msg) != tipc_port_peerport(p_ptr))
Allan Stephensf0712e862012-04-17 18:42:28 -040070 return 0;
71
72 orignode = msg_orignode(msg);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -040073 peernode = tipc_port_peernode(p_ptr);
Allan Stephensf0712e862012-04-17 18:42:28 -040074 return (orignode == peernode) ||
75 (!orignode && (peernode == tipc_own_addr)) ||
76 (!peernode && (orignode == tipc_own_addr));
77}
78
Per Lidenb97bf3f2006-01-02 19:04:38 +010079/**
Ying Xue247f0f32014-02-18 16:06:46 +080080 * tipc_port_mcast_xmit - send a multicast message to local and remote
81 * destinations
Per Lidenb97bf3f2006-01-02 19:04:38 +010082 */
Ying Xue247f0f32014-02-18 16:06:46 +080083int tipc_port_mcast_xmit(u32 ref, struct tipc_name_seq const *seq,
84 struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +010085{
86 struct tipc_msg *hdr;
87 struct sk_buff *buf;
88 struct sk_buff *ibuf = NULL;
Paul Gortmaker45843102011-12-29 20:33:30 -050089 struct tipc_port_list dports = {0, NULL, };
Allan Stephens23dd4cc2011-01-07 11:43:40 -050090 struct tipc_port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010091 int ext_targets;
92 int res;
93
94 if (unlikely(!oport))
95 return -EINVAL;
96
97 /* Create multicast message */
Allan Stephens23dd4cc2011-01-07 11:43:40 -050098 hdr = &oport->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +010099 msg_set_type(hdr, TIPC_MCAST_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400100 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
Allan Stephens7462b9e2011-04-18 10:08:22 -0400101 msg_set_destport(hdr, 0);
102 msg_set_destnode(hdr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 msg_set_nametype(hdr, seq->type);
104 msg_set_namelower(hdr, seq->lower);
105 msg_set_nameupper(hdr, seq->upper);
106 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Ying Xue9446b872013-10-18 07:23:15 +0200107 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108 if (unlikely(!buf))
109 return res;
110
111 /* Figure out where to send multicast message */
Per Liden4323add2006-01-18 00:38:21 +0100112 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
113 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900114
115 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116 if (ext_targets) {
117 if (dports.count != 0) {
118 ibuf = skb_copy(buf, GFP_ATOMIC);
119 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100120 tipc_port_list_free(&dports);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400121 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 return -ENOMEM;
123 }
124 }
Ying Xue247f0f32014-02-18 16:06:46 +0800125 res = tipc_bclink_xmit(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000126 if ((res < 0) && (dports.count != 0))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400127 kfree_skb(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 } else {
129 ibuf = buf;
130 }
131
132 if (res >= 0) {
133 if (ibuf)
Ying Xue247f0f32014-02-18 16:06:46 +0800134 tipc_port_mcast_rcv(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 } else {
Per Liden4323add2006-01-18 00:38:21 +0100136 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 }
138 return res;
139}
140
141/**
Ying Xue247f0f32014-02-18 16:06:46 +0800142 * tipc_port_mcast_rcv - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900143 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 * If there is no port list, perform a lookup to create one
145 */
Ying Xue247f0f32014-02-18 16:06:46 +0800146void tipc_port_mcast_rcv(struct sk_buff *buf, struct tipc_port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147{
Allan Stephens0e659672010-12-31 18:59:32 +0000148 struct tipc_msg *msg;
Paul Gortmaker45843102011-12-29 20:33:30 -0500149 struct tipc_port_list dports = {0, NULL, };
150 struct tipc_port_list *item = dp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 int cnt = 0;
152
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 msg = buf_msg(buf);
154
155 /* Create destination port list, if one wasn't supplied */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100157 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 msg_namelower(msg),
159 msg_nameupper(msg),
160 TIPC_CLUSTER_SCOPE,
161 &dports);
162 item = dp = &dports;
163 }
164
165 /* Deliver a copy of message to each destination port */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 if (dp->count != 0) {
Allan Stephens7f47f5c2011-04-18 10:14:26 -0400167 msg_set_destnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 if (dp->count == 1) {
169 msg_set_destport(msg, dp->ports[0]);
Ying Xue247f0f32014-02-18 16:06:46 +0800170 tipc_port_rcv(buf);
Per Liden4323add2006-01-18 00:38:21 +0100171 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 return;
173 }
174 for (; cnt < dp->count; cnt++) {
175 int index = cnt % PLSIZE;
176 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
177
178 if (b == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400179 pr_warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 goto exit;
181 }
Allan Stephensa0168922010-12-31 18:59:35 +0000182 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000184 msg_set_destport(buf_msg(b), item->ports[index]);
Ying Xue247f0f32014-02-18 16:06:46 +0800185 tipc_port_rcv(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 }
187 }
188exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400189 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100190 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191}
192
193/**
Ying Xue3c5db8e2013-06-17 10:54:44 -0400194 * tipc_createport - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900195 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700196 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 */
Ying Xuec0fee8a2013-06-17 10:54:46 -0400198struct tipc_port *tipc_createport(struct sock *sk,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400199 u32 (*dispatcher)(struct tipc_port *,
200 struct sk_buff *),
201 void (*wakeup)(struct tipc_port *),
202 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203{
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400204 struct tipc_port *p_ptr = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205 struct tipc_msg *msg;
206 u32 ref;
207
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500208 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 if (!ref) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400210 pr_warn("Port registration failed, ref. table exhausted\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700211 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 }
213
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500214 p_ptr->max_pkt = MAX_PKT_DEFAULT;
215 p_ptr->ref = ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 INIT_LIST_HEAD(&p_ptr->wait_list);
217 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 p_ptr->dispatcher = dispatcher;
219 p_ptr->wakeup = wakeup;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 INIT_LIST_HEAD(&p_ptr->publications);
222 INIT_LIST_HEAD(&p_ptr->port_list);
Allan Stephensf21536d2012-04-17 18:22:49 -0400223
224 /*
225 * Must hold port list lock while initializing message header template
226 * to ensure a change to node's own network address doesn't result
227 * in template containing out-dated network address information
228 */
Allan Stephensf21536d2012-04-17 18:22:49 -0400229 spin_lock_bh(&tipc_port_list_lock);
230 msg = &p_ptr->phdr;
231 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
232 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100234 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500235 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236}
237
Ying Xue84602762013-12-27 10:18:28 +0800238int tipc_deleteport(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800240 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241
Ying Xue84602762013-12-27 10:18:28 +0800242 tipc_withdraw(p_ptr, 0, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
Ying Xue84602762013-12-27 10:18:28 +0800244 spin_lock_bh(p_ptr->lock);
245 tipc_ref_discard(p_ptr->ref);
246 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247
248 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500249 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100251 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253
Per Liden4323add2006-01-18 00:38:21 +0100254 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 list_del(&p_ptr->port_list);
256 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100257 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 k_term_timer(&p_ptr->timer);
Per Liden4323add2006-01-18 00:38:21 +0100259 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700260 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261}
262
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500263static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500265 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266}
267
268int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
269{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500270 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900271
Per Liden4323add2006-01-18 00:38:21 +0100272 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 if (!p_ptr)
274 return -EINVAL;
275 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800276 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700277 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278}
279
280int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
281{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500282 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900283
Per Liden4323add2006-01-18 00:38:21 +0100284 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 if (!p_ptr)
286 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500287 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100288 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700289 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290}
291
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500292static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500294 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295}
296
297int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
298{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500299 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900300
Per Liden4323add2006-01-18 00:38:21 +0100301 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 if (!p_ptr)
303 return -EINVAL;
304 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800305 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700306 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307}
308
309int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
310{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500311 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900312
Per Liden4323add2006-01-18 00:38:21 +0100313 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 if (!p_ptr)
315 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500316 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100317 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700318 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319}
320
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900321/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400322 * port_build_proto_msg(): create connection protocol message for port
323 *
324 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400326static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
327 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328{
329 struct sk_buff *buf;
330 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900331
Allan Stephens741d9eb2011-05-31 15:03:18 -0400332 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 if (buf) {
334 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400335 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400336 tipc_port_peernode(p_ptr));
337 msg_set_destport(msg, tipc_port_peerport(p_ptr));
Allan Stephense4a0aee2011-06-01 16:21:12 -0400338 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 }
341 return buf;
342}
343
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344int tipc_reject_msg(struct sk_buff *buf, u32 err)
345{
346 struct tipc_msg *msg = buf_msg(buf);
347 struct sk_buff *rbuf;
348 struct tipc_msg *rmsg;
349 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400350 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400352 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400353 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
355 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400356 if (WARN(!msg_isdata(msg),
357 "attempt to reject message with user=%u", msg_user(msg))) {
358 dump_stack();
359 goto exit;
360 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400361 if (msg_errcode(msg) || msg_dest_droppable(msg))
362 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400364 /*
365 * construct returned message by copying rejected message header and
366 * data (or subset), then updating header fields that need adjusting
367 */
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400368 hdr_sz = msg_hdr_sz(msg);
369 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
370
371 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400372 if (rbuf == NULL)
373 goto exit;
374
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400376 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
377
378 if (msg_connected(rmsg)) {
379 imp = msg_importance(rmsg);
380 if (imp < TIPC_CRITICAL_IMPORTANCE)
381 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700382 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400383 msg_set_non_seq(rmsg, 0);
384 msg_set_size(rmsg, rmsg_sz);
385 msg_set_errcode(rmsg, err);
386 msg_set_prevnode(rmsg, tipc_own_addr);
387 msg_swap_words(rmsg, 4, 5);
388 if (!msg_short(rmsg))
389 msg_swap_words(rmsg, 6, 7);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390
391 /* send self-abort message when rejecting on a connected port */
392 if (msg_connected(msg)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500393 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394
395 if (p_ptr) {
Allan Stephensdff10e92011-11-02 10:32:14 -0400396 struct sk_buff *abuf = NULL;
397
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500398 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100400 tipc_port_unlock(p_ptr);
Allan Stephensdff10e92011-11-02 10:32:14 -0400401 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 }
404
Allan Stephensacc631b2011-05-23 13:47:44 -0400405 /* send returned message & dispose of rejected message */
Allan Stephens017dac32011-05-24 13:20:09 -0400406 src_node = msg_prevnode(msg);
Allan Stephens630d9202012-04-18 09:42:29 -0400407 if (in_own_node(src_node))
Ying Xue247f0f32014-02-18 16:06:46 +0800408 tipc_port_rcv(rbuf);
Allan Stephens017dac32011-05-24 13:20:09 -0400409 else
Ying Xue247f0f32014-02-18 16:06:46 +0800410 tipc_link_xmit(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400411exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400412 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 return data_sz;
414}
415
Ying Xue247f0f32014-02-18 16:06:46 +0800416int tipc_port_iovec_reject(struct tipc_port *p_ptr, struct tipc_msg *hdr,
417 struct iovec const *msg_sect, unsigned int len,
418 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419{
420 struct sk_buff *buf;
421 int res;
422
Ying Xue9446b872013-10-18 07:23:15 +0200423 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 if (!buf)
425 return res;
426
427 return tipc_reject_msg(buf, err);
428}
429
430static void port_timeout(unsigned long ref)
431{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500432 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800433 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434
Allan Stephens065fd172006-10-16 21:38:05 -0700435 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 return;
437
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500438 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700439 tipc_port_unlock(p_ptr);
440 return;
441 }
442
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 /* Last probe answered ? */
444 if (p_ptr->probing_state == PROBING) {
445 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
446 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400447 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 p_ptr->probing_state = PROBING;
449 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
450 }
Per Liden4323add2006-01-18 00:38:21 +0100451 tipc_port_unlock(p_ptr);
452 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453}
454
455
456static void port_handle_node_down(unsigned long ref)
457{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500458 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000459 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460
461 if (!p_ptr)
462 return;
463 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100464 tipc_port_unlock(p_ptr);
465 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466}
467
468
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500469static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470{
Allan Stephense244a912011-05-31 16:10:08 -0400471 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472
Allan Stephense244a912011-05-31 16:10:08 -0400473 if (buf) {
474 struct tipc_msg *msg = buf_msg(buf);
475 msg_swap_words(msg, 4, 5);
476 msg_swap_words(msg, 6, 7);
477 }
478 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479}
480
481
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500482static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483{
Allan Stephense244a912011-05-31 16:10:08 -0400484 struct sk_buff *buf;
485 struct tipc_msg *msg;
486 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500488 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800489 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400490
491 buf = tipc_buf_acquire(BASIC_H_SIZE);
492 if (buf) {
493 msg = buf_msg(buf);
494 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
495 msg_set_hdr_sz(msg, BASIC_H_SIZE);
496 msg_set_size(msg, BASIC_H_SIZE);
497 imp = msg_importance(msg);
498 if (imp < TIPC_CRITICAL_IMPORTANCE)
499 msg_set_importance(msg, ++imp);
500 msg_set_errcode(msg, err);
501 }
502 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503}
504
Ying Xue247f0f32014-02-18 16:06:46 +0800505void tipc_port_proto_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506{
507 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400508 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800509 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400510 u32 destport = msg_destport(msg);
511 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512
Allan Stephens1c1a5512011-06-01 15:08:10 -0400513 /* Validate connection */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400514 p_ptr = tipc_port_lock(destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400515 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400516 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
517 if (r_buf) {
518 msg = buf_msg(r_buf);
519 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
Allan Stephensf0712e862012-04-17 18:42:28 -0400520 BASIC_H_SIZE, msg_orignode(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400521 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
522 msg_set_origport(msg, destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400523 msg_set_destport(msg, msg_origport(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400524 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400525 if (p_ptr)
526 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 goto exit;
528 }
529
Allan Stephens1c1a5512011-06-01 15:08:10 -0400530 /* Process protocol message sent by peer */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400531 switch (msg_type(msg)) {
532 case CONN_ACK:
533 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
534 p_ptr->wakeup;
535 p_ptr->acked += msg_msgcnt(msg);
536 if (!tipc_port_congested(p_ptr)) {
537 p_ptr->congested = 0;
538 if (wakeable)
539 p_ptr->wakeup(p_ptr);
540 }
541 break;
542 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400543 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400544 break;
545 default:
546 /* CONN_PROBE_REPLY or unrecognized - no action required */
547 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 }
549 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400550 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551exit:
Per Liden4323add2006-01-18 00:38:21 +0100552 tipc_net_route_msg(r_buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400553 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554}
555
Erik Hugnedc1aed32012-06-29 00:50:23 -0400556static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900558 struct publication *publ;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400559 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560
561 if (full_id)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400562 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
563 tipc_zone(tipc_own_addr),
564 tipc_cluster(tipc_own_addr),
565 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400567 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500569 if (p_ptr->connected) {
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400570 u32 dport = tipc_port_peerport(p_ptr);
571 u32 destnode = tipc_port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572
Erik Hugnedc1aed32012-06-29 00:50:23 -0400573 ret += tipc_snprintf(buf + ret, len - ret,
574 " connected to <%u.%u.%u:%u>",
575 tipc_zone(destnode),
576 tipc_cluster(destnode),
577 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500578 if (p_ptr->conn_type != 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400579 ret += tipc_snprintf(buf + ret, len - ret,
580 " via {%u,%u}", p_ptr->conn_type,
581 p_ptr->conn_instance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500582 } else if (p_ptr->published) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400583 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 if (publ->lower == publ->upper)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400586 ret += tipc_snprintf(buf + ret, len - ret,
587 " {%u,%u}", publ->type,
588 publ->lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400590 ret += tipc_snprintf(buf + ret, len - ret,
591 " {%u,%u,%u}", publ->type,
592 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593 }
594 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400595 ret += tipc_snprintf(buf + ret, len - ret, "\n");
596 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597}
598
Per Liden4323add2006-01-18 00:38:21 +0100599struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600{
601 struct sk_buff *buf;
602 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400603 char *pb;
604 int pb_len;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500605 struct tipc_port *p_ptr;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400606 int str_len = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607
Erik Hugnedc1aed32012-06-29 00:50:23 -0400608 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 if (!buf)
610 return NULL;
611 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400612 pb = TLV_DATA(rep_tlv);
613 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614
Per Liden4323add2006-01-18 00:38:21 +0100615 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500617 spin_lock_bh(p_ptr->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400618 str_len += port_print(p_ptr, pb, pb_len, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500619 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 }
Per Liden4323add2006-01-18 00:38:21 +0100621 spin_unlock_bh(&tipc_port_list_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400622 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 skb_put(buf, TLV_SPACE(str_len));
624 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
625
626 return buf;
627}
628
Per Liden4323add2006-01-18 00:38:21 +0100629void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500631 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 struct tipc_msg *msg;
633
Per Liden4323add2006-01-18 00:38:21 +0100634 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500636 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700637 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 msg_set_orignode(msg, tipc_own_addr);
639 }
Per Liden4323add2006-01-18 00:38:21 +0100640 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641}
642
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643void tipc_acknowledge(u32 ref, u32 ack)
644{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500645 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800646 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647
Per Liden4323add2006-01-18 00:38:21 +0100648 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 if (!p_ptr)
650 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500651 if (p_ptr->connected) {
652 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400653 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 }
Per Liden4323add2006-01-18 00:38:21 +0100655 tipc_port_unlock(p_ptr);
656 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657}
658
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659int tipc_portimportance(u32 ref, unsigned int *importance)
660{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500661 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662
Per Liden4323add2006-01-18 00:38:21 +0100663 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 if (!p_ptr)
665 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500666 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800667 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700668 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669}
670
671int tipc_set_portimportance(u32 ref, unsigned int imp)
672{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500673 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674
675 if (imp > TIPC_CRITICAL_IMPORTANCE)
676 return -EINVAL;
677
Per Liden4323add2006-01-18 00:38:21 +0100678 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 if (!p_ptr)
680 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500681 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800682 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700683 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684}
685
686
Ying Xue84602762013-12-27 10:18:28 +0800687int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
688 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 struct publication *publ;
691 u32 key;
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800692
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500693 if (p_ptr->connected)
Ying Xue84602762013-12-27 10:18:28 +0800694 return -EINVAL;
695 key = p_ptr->ref + p_ptr->pub_count + 1;
696 if (key == p_ptr->ref)
697 return -EADDRINUSE;
698
Per Liden4323add2006-01-18 00:38:21 +0100699 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500700 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701 if (publ) {
702 list_add(&publ->pport_list, &p_ptr->publications);
703 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500704 p_ptr->published = 1;
Ying Xue84602762013-12-27 10:18:28 +0800705 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 }
Ying Xue84602762013-12-27 10:18:28 +0800707 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708}
709
Ying Xue84602762013-12-27 10:18:28 +0800710int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
711 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 struct publication *publ;
714 struct publication *tpubl;
715 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900716
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900718 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900720 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100721 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 }
Allan Stephens0e35fd52008-07-14 22:44:01 -0700723 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900725 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 &p_ptr->publications, pport_list) {
727 if (publ->scope != scope)
728 continue;
729 if (publ->type != seq->type)
730 continue;
731 if (publ->lower != seq->lower)
732 continue;
733 if (publ->upper != seq->upper)
734 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900735 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100736 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700737 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 break;
739 }
740 }
741 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500742 p_ptr->published = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 return res;
744}
745
Ying Xue247f0f32014-02-18 16:06:46 +0800746int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500748 struct tipc_port *p_ptr;
Paul Gortmakerbc879112012-11-29 13:48:40 -0500749 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750
Per Liden4323add2006-01-18 00:38:21 +0100751 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 if (!p_ptr)
753 return -EINVAL;
Ying Xue247f0f32014-02-18 16:06:46 +0800754 res = __tipc_port_connect(ref, p_ptr, peer);
Paul Gortmakerbc879112012-11-29 13:48:40 -0500755 tipc_port_unlock(p_ptr);
756 return res;
757}
758
759/*
Ying Xue247f0f32014-02-18 16:06:46 +0800760 * __tipc_port_connect - connect to a remote peer
Paul Gortmakerbc879112012-11-29 13:48:40 -0500761 *
762 * Port must be locked.
763 */
Ying Xue247f0f32014-02-18 16:06:46 +0800764int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
Paul Gortmakerbc879112012-11-29 13:48:40 -0500765 struct tipc_portid const *peer)
766{
767 struct tipc_msg *msg;
768 int res = -EINVAL;
769
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500770 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 goto exit;
772 if (!peer->ref)
773 goto exit;
774
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500775 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 msg_set_destnode(msg, peer->node);
777 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400779 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +0000780 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781
782 p_ptr->probing_interval = PROBING_INTERVAL;
783 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500784 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
786
Allan Stephens0e659672010-12-31 18:59:32 +0000787 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -0800788 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700790 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791exit:
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500792 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 return res;
794}
795
Paul Gortmakerbc879112012-11-29 13:48:40 -0500796/*
797 * __tipc_disconnect - disconnect port from peer
Allan Stephens0c3141e2008-04-15 00:22:02 -0700798 *
799 * Port must be locked.
800 */
Ying Xue247f0f32014-02-18 16:06:46 +0800801int __tipc_port_disconnect(struct tipc_port *tp_ptr)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700802{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700803 if (tp_ptr->connected) {
804 tp_ptr->connected = 0;
805 /* let timer expire on it's own to avoid deadlock! */
Joe Perchese3192692012-06-03 17:41:40 +0000806 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
wangweidong0cee6bb2013-12-12 09:36:39 +0800807 return 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700808 }
wangweidong0cee6bb2013-12-12 09:36:39 +0800809
810 return -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700811}
812
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813/*
Ying Xue247f0f32014-02-18 16:06:46 +0800814 * tipc_port_disconnect(): Disconnect port form peer.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 * This is a node local operation.
816 */
Ying Xue247f0f32014-02-18 16:06:46 +0800817int tipc_port_disconnect(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500819 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700820 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821
Per Liden4323add2006-01-18 00:38:21 +0100822 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 if (!p_ptr)
824 return -EINVAL;
Ying Xue247f0f32014-02-18 16:06:46 +0800825 res = __tipc_port_disconnect(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100826 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 return res;
828}
829
830/*
Ying Xue247f0f32014-02-18 16:06:46 +0800831 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 */
Ying Xue247f0f32014-02-18 16:06:46 +0800833int tipc_port_shutdown(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500835 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800836 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837
Per Liden4323add2006-01-18 00:38:21 +0100838 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 if (!p_ptr)
840 return -EINVAL;
841
Allan Stephense244a912011-05-31 16:10:08 -0400842 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +0100843 tipc_port_unlock(p_ptr);
844 tipc_net_route_msg(buf);
Ying Xue247f0f32014-02-18 16:06:46 +0800845 return tipc_port_disconnect(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846}
847
Allan Stephens9b641252011-11-09 13:29:18 -0500848/**
Ying Xue247f0f32014-02-18 16:06:46 +0800849 * tipc_port_rcv - receive message from lower layer and deliver to port user
Allan Stephens9b641252011-11-09 13:29:18 -0500850 */
Ying Xue247f0f32014-02-18 16:06:46 +0800851int tipc_port_rcv(struct sk_buff *buf)
Allan Stephens9b641252011-11-09 13:29:18 -0500852{
853 struct tipc_port *p_ptr;
854 struct tipc_msg *msg = buf_msg(buf);
855 u32 destport = msg_destport(msg);
856 u32 dsz = msg_data_sz(msg);
857 u32 err;
858
859 /* forward unresolved named message */
860 if (unlikely(!destport)) {
861 tipc_net_route_msg(buf);
862 return dsz;
863 }
864
865 /* validate destination & pass to port, otherwise reject message */
866 p_ptr = tipc_port_lock(destport);
867 if (likely(p_ptr)) {
Allan Stephens9b641252011-11-09 13:29:18 -0500868 err = p_ptr->dispatcher(p_ptr, buf);
869 tipc_port_unlock(p_ptr);
870 if (likely(!err))
871 return dsz;
872 } else {
873 err = TIPC_ERR_NO_PORT;
874 }
Allan Stephensf0712e862012-04-17 18:42:28 -0400875
Allan Stephens9b641252011-11-09 13:29:18 -0500876 return tipc_reject_msg(buf, err);
877}
878
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879/*
Ying Xue247f0f32014-02-18 16:06:46 +0800880 * tipc_port_iovec_rcv: Concatenate and deliver sectioned
881 * message for this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 */
Ying Xue247f0f32014-02-18 16:06:46 +0800883static int tipc_port_iovec_rcv(struct tipc_port *sender,
884 struct iovec const *msg_sect,
885 unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886{
887 struct sk_buff *buf;
888 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900889
Ying Xue9446b872013-10-18 07:23:15 +0200890 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 if (likely(buf))
Ying Xue247f0f32014-02-18 16:06:46 +0800892 tipc_port_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 return res;
894}
895
896/**
897 * tipc_send - send message sections on connection
898 */
Ying Xue9446b872013-10-18 07:23:15 +0200899int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500901 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 u32 destnode;
903 int res;
904
Per Liden4323add2006-01-18 00:38:21 +0100905 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500906 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 return -EINVAL;
908
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500909 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +0100910 if (!tipc_port_congested(p_ptr)) {
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400911 destnode = tipc_port_peernode(p_ptr);
Allan Stephens8a55fe72012-04-18 09:27:22 -0400912 if (likely(!in_own_node(destnode)))
Ying Xue247f0f32014-02-18 16:06:46 +0800913 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
914 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 else
Ying Xue247f0f32014-02-18 16:06:46 +0800916 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917
918 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500919 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500920 if (res > 0)
921 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 return res;
923 }
924 }
925 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500926 p_ptr->congested = 0;
Ying Xue9446b872013-10-18 07:23:15 +0200927 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 }
929 return -ELINKCONG;
930}
931
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900932/**
Allan Stephens12bae472010-11-30 12:01:02 +0000933 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 */
Allan Stephens12bae472010-11-30 12:01:02 +0000935int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Ying Xue9446b872013-10-18 07:23:15 +0200936 struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500938 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 struct tipc_msg *msg;
940 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +0000941 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 int res;
943
Per Liden4323add2006-01-18 00:38:21 +0100944 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500945 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 return -EINVAL;
947
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500948 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -0400950 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 msg_set_nametype(msg, name->type);
952 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000953 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +0100954 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 msg_set_destnode(msg, destnode);
956 msg_set_destport(msg, destport);
957
Allan Stephensbc9f8142011-11-07 17:00:54 -0500958 if (likely(destport || destnode)) {
Allan Stephens8a55fe72012-04-18 09:27:22 -0400959 if (likely(in_own_node(destnode)))
Ying Xue247f0f32014-02-18 16:06:46 +0800960 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400961 else if (tipc_own_addr)
Ying Xue247f0f32014-02-18 16:06:46 +0800962 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
963 destnode);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400964 else
Ying Xue247f0f32014-02-18 16:06:46 +0800965 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect,
966 len, TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500967 if (likely(res != -ELINKCONG)) {
968 if (res > 0)
969 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500971 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 if (port_unreliable(p_ptr)) {
Ying Xue9446b872013-10-18 07:23:15 +0200973 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 }
975 return -ELINKCONG;
976 }
Ying Xue247f0f32014-02-18 16:06:46 +0800977 return tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
978 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979}
980
981/**
Allan Stephens12bae472010-11-30 12:01:02 +0000982 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 */
Allan Stephens12bae472010-11-30 12:01:02 +0000984int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Ying Xue9446b872013-10-18 07:23:15 +0200985 struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500987 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 struct tipc_msg *msg;
989 int res;
990
Per Liden4323add2006-01-18 00:38:21 +0100991 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500992 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 return -EINVAL;
994
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500995 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400997 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998 msg_set_destnode(msg, dest->node);
999 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001000 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001001
Allan Stephens8a55fe72012-04-18 09:27:22 -04001002 if (in_own_node(dest->node))
Ying Xue247f0f32014-02-18 16:06:46 +08001003 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001004 else if (tipc_own_addr)
Ying Xue247f0f32014-02-18 16:06:46 +08001005 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
1006 dest->node);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001007 else
Ying Xue247f0f32014-02-18 16:06:46 +08001008 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
Ying Xue9446b872013-10-18 07:23:15 +02001009 TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001010 if (likely(res != -ELINKCONG)) {
1011 if (res > 0)
1012 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001014 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 if (port_unreliable(p_ptr)) {
Ying Xue9446b872013-10-18 07:23:15 +02001016 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 }
1018 return -ELINKCONG;
1019}