blob: 5d24a195ea3d7c8563eeebbb054fb9bc35376315 [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
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400193
194void tipc_port_wakeup(struct tipc_port *port)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195{
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400196 tipc_sk_wakeup(tipc_port_to_sk(port));
197}
198
199/* tipc_port_init - intiate TIPC port and lock it
200 *
201 * Returns obtained reference if initialization is successful, zero otherwise
202 */
203u32 tipc_port_init(struct tipc_port *p_ptr,
204 const unsigned int importance)
205{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 struct tipc_msg *msg;
207 u32 ref;
208
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500209 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 if (!ref) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400211 pr_warn("Port registration failed, ref. table exhausted\n");
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400212 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 }
214
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500215 p_ptr->max_pkt = MAX_PKT_DEFAULT;
216 p_ptr->ref = ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 INIT_LIST_HEAD(&p_ptr->wait_list);
218 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 INIT_LIST_HEAD(&p_ptr->publications);
221 INIT_LIST_HEAD(&p_ptr->port_list);
Allan Stephensf21536d2012-04-17 18:22:49 -0400222
223 /*
224 * Must hold port list lock while initializing message header template
225 * to ensure a change to node's own network address doesn't result
226 * in template containing out-dated network address information
227 */
Allan Stephensf21536d2012-04-17 18:22:49 -0400228 spin_lock_bh(&tipc_port_list_lock);
229 msg = &p_ptr->phdr;
230 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
231 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100233 spin_unlock_bh(&tipc_port_list_lock);
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400234 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235}
236
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400237void tipc_port_destroy(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800239 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240
Ying Xue84602762013-12-27 10:18:28 +0800241 tipc_withdraw(p_ptr, 0, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242
Ying Xue84602762013-12-27 10:18:28 +0800243 spin_lock_bh(p_ptr->lock);
244 tipc_ref_discard(p_ptr->ref);
245 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246
247 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500248 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100250 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252
Per Liden4323add2006-01-18 00:38:21 +0100253 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 list_del(&p_ptr->port_list);
255 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100256 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 k_term_timer(&p_ptr->timer);
Per Liden4323add2006-01-18 00:38:21 +0100258 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259}
260
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500261static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500263 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264}
265
266int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
267{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500268 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900269
Per Liden4323add2006-01-18 00:38:21 +0100270 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 if (!p_ptr)
272 return -EINVAL;
273 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800274 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700275 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276}
277
278int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
279{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500280 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900281
Per Liden4323add2006-01-18 00:38:21 +0100282 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 if (!p_ptr)
284 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500285 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100286 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700287 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288}
289
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500290static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500292 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293}
294
295int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
296{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500297 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298
Per Liden4323add2006-01-18 00:38:21 +0100299 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 if (!p_ptr)
301 return -EINVAL;
302 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800303 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700304 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305}
306
307int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
308{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500309 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900310
Per Liden4323add2006-01-18 00:38:21 +0100311 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 if (!p_ptr)
313 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500314 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100315 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700316 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317}
318
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900319/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400320 * port_build_proto_msg(): create connection protocol message for port
321 *
322 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400324static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
325 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326{
327 struct sk_buff *buf;
328 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900329
Allan Stephens741d9eb2011-05-31 15:03:18 -0400330 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 if (buf) {
332 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400333 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400334 tipc_port_peernode(p_ptr));
335 msg_set_destport(msg, tipc_port_peerport(p_ptr));
Allan Stephense4a0aee2011-06-01 16:21:12 -0400336 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 }
339 return buf;
340}
341
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342int tipc_reject_msg(struct sk_buff *buf, u32 err)
343{
344 struct tipc_msg *msg = buf_msg(buf);
345 struct sk_buff *rbuf;
346 struct tipc_msg *rmsg;
347 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400348 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400350 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400351 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352
353 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400354 if (WARN(!msg_isdata(msg),
355 "attempt to reject message with user=%u", msg_user(msg))) {
356 dump_stack();
357 goto exit;
358 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400359 if (msg_errcode(msg) || msg_dest_droppable(msg))
360 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400362 /*
363 * construct returned message by copying rejected message header and
364 * data (or subset), then updating header fields that need adjusting
365 */
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400366 hdr_sz = msg_hdr_sz(msg);
367 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
368
369 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400370 if (rbuf == NULL)
371 goto exit;
372
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400374 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
375
376 if (msg_connected(rmsg)) {
377 imp = msg_importance(rmsg);
378 if (imp < TIPC_CRITICAL_IMPORTANCE)
379 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700380 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400381 msg_set_non_seq(rmsg, 0);
382 msg_set_size(rmsg, rmsg_sz);
383 msg_set_errcode(rmsg, err);
384 msg_set_prevnode(rmsg, tipc_own_addr);
385 msg_swap_words(rmsg, 4, 5);
386 if (!msg_short(rmsg))
387 msg_swap_words(rmsg, 6, 7);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388
389 /* send self-abort message when rejecting on a connected port */
390 if (msg_connected(msg)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500391 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392
393 if (p_ptr) {
Allan Stephensdff10e92011-11-02 10:32:14 -0400394 struct sk_buff *abuf = NULL;
395
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500396 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100398 tipc_port_unlock(p_ptr);
Allan Stephensdff10e92011-11-02 10:32:14 -0400399 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 }
402
Allan Stephensacc631b2011-05-23 13:47:44 -0400403 /* send returned message & dispose of rejected message */
Allan Stephens017dac32011-05-24 13:20:09 -0400404 src_node = msg_prevnode(msg);
Allan Stephens630d9202012-04-18 09:42:29 -0400405 if (in_own_node(src_node))
Ying Xue247f0f32014-02-18 16:06:46 +0800406 tipc_port_rcv(rbuf);
Allan Stephens017dac32011-05-24 13:20:09 -0400407 else
Ying Xue247f0f32014-02-18 16:06:46 +0800408 tipc_link_xmit(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400409exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400410 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 return data_sz;
412}
413
Ying Xue247f0f32014-02-18 16:06:46 +0800414int tipc_port_iovec_reject(struct tipc_port *p_ptr, struct tipc_msg *hdr,
415 struct iovec const *msg_sect, unsigned int len,
416 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417{
418 struct sk_buff *buf;
419 int res;
420
Ying Xue9446b872013-10-18 07:23:15 +0200421 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 if (!buf)
423 return res;
424
425 return tipc_reject_msg(buf, err);
426}
427
428static void port_timeout(unsigned long ref)
429{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500430 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800431 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432
Allan Stephens065fd172006-10-16 21:38:05 -0700433 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 return;
435
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500436 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700437 tipc_port_unlock(p_ptr);
438 return;
439 }
440
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 /* Last probe answered ? */
442 if (p_ptr->probing_state == PROBING) {
443 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
444 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400445 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 p_ptr->probing_state = PROBING;
447 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
448 }
Per Liden4323add2006-01-18 00:38:21 +0100449 tipc_port_unlock(p_ptr);
450 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451}
452
453
454static void port_handle_node_down(unsigned long ref)
455{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500456 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000457 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458
459 if (!p_ptr)
460 return;
461 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100462 tipc_port_unlock(p_ptr);
463 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464}
465
466
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500467static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468{
Allan Stephense244a912011-05-31 16:10:08 -0400469 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470
Allan Stephense244a912011-05-31 16:10:08 -0400471 if (buf) {
472 struct tipc_msg *msg = buf_msg(buf);
473 msg_swap_words(msg, 4, 5);
474 msg_swap_words(msg, 6, 7);
475 }
476 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477}
478
479
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500480static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481{
Allan Stephense244a912011-05-31 16:10:08 -0400482 struct sk_buff *buf;
483 struct tipc_msg *msg;
484 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500486 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800487 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400488
489 buf = tipc_buf_acquire(BASIC_H_SIZE);
490 if (buf) {
491 msg = buf_msg(buf);
492 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
493 msg_set_hdr_sz(msg, BASIC_H_SIZE);
494 msg_set_size(msg, BASIC_H_SIZE);
495 imp = msg_importance(msg);
496 if (imp < TIPC_CRITICAL_IMPORTANCE)
497 msg_set_importance(msg, ++imp);
498 msg_set_errcode(msg, err);
499 }
500 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501}
502
Ying Xue247f0f32014-02-18 16:06:46 +0800503void tipc_port_proto_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504{
505 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400506 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800507 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400508 u32 destport = msg_destport(msg);
509 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510
Allan Stephens1c1a5512011-06-01 15:08:10 -0400511 /* Validate connection */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400512 p_ptr = tipc_port_lock(destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400513 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400514 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
515 if (r_buf) {
516 msg = buf_msg(r_buf);
517 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
Allan Stephensf0712e862012-04-17 18:42:28 -0400518 BASIC_H_SIZE, msg_orignode(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400519 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
520 msg_set_origport(msg, destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400521 msg_set_destport(msg, msg_origport(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400522 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400523 if (p_ptr)
524 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 goto exit;
526 }
527
Allan Stephens1c1a5512011-06-01 15:08:10 -0400528 /* Process protocol message sent by peer */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400529 switch (msg_type(msg)) {
530 case CONN_ACK:
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400531 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400532 p_ptr->acked += msg_msgcnt(msg);
533 if (!tipc_port_congested(p_ptr)) {
534 p_ptr->congested = 0;
535 if (wakeable)
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400536 tipc_port_wakeup(p_ptr);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400537 }
538 break;
539 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400540 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400541 break;
542 default:
543 /* CONN_PROBE_REPLY or unrecognized - no action required */
544 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 }
546 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400547 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548exit:
Per Liden4323add2006-01-18 00:38:21 +0100549 tipc_net_route_msg(r_buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400550 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551}
552
Erik Hugnedc1aed32012-06-29 00:50:23 -0400553static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900555 struct publication *publ;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400556 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557
558 if (full_id)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400559 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
560 tipc_zone(tipc_own_addr),
561 tipc_cluster(tipc_own_addr),
562 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400564 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500566 if (p_ptr->connected) {
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400567 u32 dport = tipc_port_peerport(p_ptr);
568 u32 destnode = tipc_port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569
Erik Hugnedc1aed32012-06-29 00:50:23 -0400570 ret += tipc_snprintf(buf + ret, len - ret,
571 " connected to <%u.%u.%u:%u>",
572 tipc_zone(destnode),
573 tipc_cluster(destnode),
574 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500575 if (p_ptr->conn_type != 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400576 ret += tipc_snprintf(buf + ret, len - ret,
577 " via {%u,%u}", p_ptr->conn_type,
578 p_ptr->conn_instance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500579 } else if (p_ptr->published) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400580 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 if (publ->lower == publ->upper)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400583 ret += tipc_snprintf(buf + ret, len - ret,
584 " {%u,%u}", publ->type,
585 publ->lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400587 ret += tipc_snprintf(buf + ret, len - ret,
588 " {%u,%u,%u}", publ->type,
589 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 }
591 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400592 ret += tipc_snprintf(buf + ret, len - ret, "\n");
593 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594}
595
Per Liden4323add2006-01-18 00:38:21 +0100596struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597{
598 struct sk_buff *buf;
599 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400600 char *pb;
601 int pb_len;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500602 struct tipc_port *p_ptr;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400603 int str_len = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604
Erik Hugnedc1aed32012-06-29 00:50:23 -0400605 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 if (!buf)
607 return NULL;
608 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400609 pb = TLV_DATA(rep_tlv);
610 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611
Per Liden4323add2006-01-18 00:38:21 +0100612 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500614 spin_lock_bh(p_ptr->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400615 str_len += port_print(p_ptr, pb, pb_len, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500616 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 }
Per Liden4323add2006-01-18 00:38:21 +0100618 spin_unlock_bh(&tipc_port_list_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400619 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 skb_put(buf, TLV_SPACE(str_len));
621 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
622
623 return buf;
624}
625
Per Liden4323add2006-01-18 00:38:21 +0100626void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500628 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 struct tipc_msg *msg;
630
Per Liden4323add2006-01-18 00:38:21 +0100631 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500633 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700634 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 msg_set_orignode(msg, tipc_own_addr);
636 }
Per Liden4323add2006-01-18 00:38:21 +0100637 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638}
639
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640void tipc_acknowledge(u32 ref, u32 ack)
641{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500642 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800643 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644
Per Liden4323add2006-01-18 00:38:21 +0100645 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 if (!p_ptr)
647 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500648 if (p_ptr->connected) {
649 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400650 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 }
Per Liden4323add2006-01-18 00:38:21 +0100652 tipc_port_unlock(p_ptr);
653 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654}
655
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656int tipc_portimportance(u32 ref, unsigned int *importance)
657{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500658 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900659
Per Liden4323add2006-01-18 00:38:21 +0100660 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 if (!p_ptr)
662 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500663 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800664 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700665 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666}
667
668int tipc_set_portimportance(u32 ref, unsigned int imp)
669{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500670 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671
672 if (imp > TIPC_CRITICAL_IMPORTANCE)
673 return -EINVAL;
674
Per Liden4323add2006-01-18 00:38:21 +0100675 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 if (!p_ptr)
677 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500678 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800679 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700680 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681}
682
683
Ying Xue84602762013-12-27 10:18:28 +0800684int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
685 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 struct publication *publ;
688 u32 key;
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800689
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500690 if (p_ptr->connected)
Ying Xue84602762013-12-27 10:18:28 +0800691 return -EINVAL;
692 key = p_ptr->ref + p_ptr->pub_count + 1;
693 if (key == p_ptr->ref)
694 return -EADDRINUSE;
695
Per Liden4323add2006-01-18 00:38:21 +0100696 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500697 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698 if (publ) {
699 list_add(&publ->pport_list, &p_ptr->publications);
700 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500701 p_ptr->published = 1;
Ying Xue84602762013-12-27 10:18:28 +0800702 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 }
Ying Xue84602762013-12-27 10:18:28 +0800704 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705}
706
Ying Xue84602762013-12-27 10:18:28 +0800707int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
708 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 struct publication *publ;
711 struct publication *tpubl;
712 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900713
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900715 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100718 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 }
Allan Stephens0e35fd52008-07-14 22:44:01 -0700720 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900722 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723 &p_ptr->publications, pport_list) {
724 if (publ->scope != scope)
725 continue;
726 if (publ->type != seq->type)
727 continue;
728 if (publ->lower != seq->lower)
729 continue;
730 if (publ->upper != seq->upper)
731 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900732 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100733 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700734 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 break;
736 }
737 }
738 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500739 p_ptr->published = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 return res;
741}
742
Ying Xue247f0f32014-02-18 16:06:46 +0800743int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500745 struct tipc_port *p_ptr;
Paul Gortmakerbc879112012-11-29 13:48:40 -0500746 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747
Per Liden4323add2006-01-18 00:38:21 +0100748 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 if (!p_ptr)
750 return -EINVAL;
Ying Xue247f0f32014-02-18 16:06:46 +0800751 res = __tipc_port_connect(ref, p_ptr, peer);
Paul Gortmakerbc879112012-11-29 13:48:40 -0500752 tipc_port_unlock(p_ptr);
753 return res;
754}
755
756/*
Ying Xue247f0f32014-02-18 16:06:46 +0800757 * __tipc_port_connect - connect to a remote peer
Paul Gortmakerbc879112012-11-29 13:48:40 -0500758 *
759 * Port must be locked.
760 */
Ying Xue247f0f32014-02-18 16:06:46 +0800761int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
Paul Gortmakerbc879112012-11-29 13:48:40 -0500762 struct tipc_portid const *peer)
763{
764 struct tipc_msg *msg;
765 int res = -EINVAL;
766
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500767 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 goto exit;
769 if (!peer->ref)
770 goto exit;
771
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500772 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 msg_set_destnode(msg, peer->node);
774 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400776 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +0000777 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778
779 p_ptr->probing_interval = PROBING_INTERVAL;
780 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500781 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
783
Allan Stephens0e659672010-12-31 18:59:32 +0000784 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -0800785 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700787 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788exit:
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500789 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 return res;
791}
792
Paul Gortmakerbc879112012-11-29 13:48:40 -0500793/*
794 * __tipc_disconnect - disconnect port from peer
Allan Stephens0c3141e2008-04-15 00:22:02 -0700795 *
796 * Port must be locked.
797 */
Ying Xue247f0f32014-02-18 16:06:46 +0800798int __tipc_port_disconnect(struct tipc_port *tp_ptr)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700799{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700800 if (tp_ptr->connected) {
801 tp_ptr->connected = 0;
802 /* let timer expire on it's own to avoid deadlock! */
Joe Perchese3192692012-06-03 17:41:40 +0000803 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
wangweidong0cee6bb2013-12-12 09:36:39 +0800804 return 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700805 }
wangweidong0cee6bb2013-12-12 09:36:39 +0800806
807 return -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700808}
809
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810/*
Ying Xue247f0f32014-02-18 16:06:46 +0800811 * tipc_port_disconnect(): Disconnect port form peer.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 * This is a node local operation.
813 */
Ying Xue247f0f32014-02-18 16:06:46 +0800814int tipc_port_disconnect(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500816 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700817 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818
Per Liden4323add2006-01-18 00:38:21 +0100819 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 if (!p_ptr)
821 return -EINVAL;
Ying Xue247f0f32014-02-18 16:06:46 +0800822 res = __tipc_port_disconnect(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100823 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 return res;
825}
826
827/*
Ying Xue247f0f32014-02-18 16:06:46 +0800828 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 */
Ying Xue247f0f32014-02-18 16:06:46 +0800830int tipc_port_shutdown(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500832 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800833 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834
Per Liden4323add2006-01-18 00:38:21 +0100835 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 if (!p_ptr)
837 return -EINVAL;
838
Allan Stephense244a912011-05-31 16:10:08 -0400839 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +0100840 tipc_port_unlock(p_ptr);
841 tipc_net_route_msg(buf);
Ying Xue247f0f32014-02-18 16:06:46 +0800842 return tipc_port_disconnect(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843}
844
Allan Stephens9b641252011-11-09 13:29:18 -0500845/**
Ying Xue247f0f32014-02-18 16:06:46 +0800846 * tipc_port_rcv - receive message from lower layer and deliver to port user
Allan Stephens9b641252011-11-09 13:29:18 -0500847 */
Ying Xue247f0f32014-02-18 16:06:46 +0800848int tipc_port_rcv(struct sk_buff *buf)
Allan Stephens9b641252011-11-09 13:29:18 -0500849{
850 struct tipc_port *p_ptr;
851 struct tipc_msg *msg = buf_msg(buf);
852 u32 destport = msg_destport(msg);
853 u32 dsz = msg_data_sz(msg);
854 u32 err;
855
856 /* forward unresolved named message */
857 if (unlikely(!destport)) {
858 tipc_net_route_msg(buf);
859 return dsz;
860 }
861
862 /* validate destination & pass to port, otherwise reject message */
863 p_ptr = tipc_port_lock(destport);
864 if (likely(p_ptr)) {
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400865 err = tipc_sk_rcv(tipc_port_to_sk(p_ptr), buf);
Allan Stephens9b641252011-11-09 13:29:18 -0500866 tipc_port_unlock(p_ptr);
867 if (likely(!err))
868 return dsz;
869 } else {
870 err = TIPC_ERR_NO_PORT;
871 }
Allan Stephensf0712e862012-04-17 18:42:28 -0400872
Allan Stephens9b641252011-11-09 13:29:18 -0500873 return tipc_reject_msg(buf, err);
874}
875
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876/*
Ying Xue247f0f32014-02-18 16:06:46 +0800877 * tipc_port_iovec_rcv: Concatenate and deliver sectioned
878 * message for this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 */
Ying Xue247f0f32014-02-18 16:06:46 +0800880static int tipc_port_iovec_rcv(struct tipc_port *sender,
881 struct iovec const *msg_sect,
882 unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883{
884 struct sk_buff *buf;
885 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900886
Ying Xue9446b872013-10-18 07:23:15 +0200887 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 if (likely(buf))
Ying Xue247f0f32014-02-18 16:06:46 +0800889 tipc_port_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 return res;
891}
892
893/**
894 * tipc_send - send message sections on connection
895 */
Ying Xue9446b872013-10-18 07:23:15 +0200896int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500898 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 u32 destnode;
900 int res;
901
Per Liden4323add2006-01-18 00:38:21 +0100902 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500903 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 return -EINVAL;
905
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500906 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +0100907 if (!tipc_port_congested(p_ptr)) {
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400908 destnode = tipc_port_peernode(p_ptr);
Allan Stephens8a55fe72012-04-18 09:27:22 -0400909 if (likely(!in_own_node(destnode)))
Ying Xue247f0f32014-02-18 16:06:46 +0800910 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
911 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 else
Ying Xue247f0f32014-02-18 16:06:46 +0800913 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914
915 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500916 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500917 if (res > 0)
918 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 return res;
920 }
921 }
922 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500923 p_ptr->congested = 0;
Ying Xue9446b872013-10-18 07:23:15 +0200924 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 }
926 return -ELINKCONG;
927}
928
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900929/**
Allan Stephens12bae4792010-11-30 12:01:02 +0000930 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 */
Allan Stephens12bae4792010-11-30 12:01:02 +0000932int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Ying Xue9446b872013-10-18 07:23:15 +0200933 struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500935 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 struct tipc_msg *msg;
937 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +0000938 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 int res;
940
Per Liden4323add2006-01-18 00:38:21 +0100941 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500942 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943 return -EINVAL;
944
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500945 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -0400947 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 msg_set_nametype(msg, name->type);
949 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000950 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +0100951 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 msg_set_destnode(msg, destnode);
953 msg_set_destport(msg, destport);
954
Allan Stephensbc9f8142011-11-07 17:00:54 -0500955 if (likely(destport || destnode)) {
Allan Stephens8a55fe72012-04-18 09:27:22 -0400956 if (likely(in_own_node(destnode)))
Ying Xue247f0f32014-02-18 16:06:46 +0800957 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400958 else if (tipc_own_addr)
Ying Xue247f0f32014-02-18 16:06:46 +0800959 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
960 destnode);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400961 else
Ying Xue247f0f32014-02-18 16:06:46 +0800962 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect,
963 len, TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500964 if (likely(res != -ELINKCONG)) {
965 if (res > 0)
966 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500968 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 if (port_unreliable(p_ptr)) {
Ying Xue9446b872013-10-18 07:23:15 +0200970 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 }
972 return -ELINKCONG;
973 }
Ying Xue247f0f32014-02-18 16:06:46 +0800974 return tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
975 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976}
977
978/**
Allan Stephens12bae4792010-11-30 12:01:02 +0000979 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 */
Allan Stephens12bae4792010-11-30 12:01:02 +0000981int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Ying Xue9446b872013-10-18 07:23:15 +0200982 struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500984 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 struct tipc_msg *msg;
986 int res;
987
Per Liden4323add2006-01-18 00:38:21 +0100988 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500989 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 return -EINVAL;
991
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500992 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400994 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 msg_set_destnode(msg, dest->node);
996 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -0400997 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500998
Allan Stephens8a55fe72012-04-18 09:27:22 -0400999 if (in_own_node(dest->node))
Ying Xue247f0f32014-02-18 16:06:46 +08001000 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001001 else if (tipc_own_addr)
Ying Xue247f0f32014-02-18 16:06:46 +08001002 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
1003 dest->node);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001004 else
Ying Xue247f0f32014-02-18 16:06:46 +08001005 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
Ying Xue9446b872013-10-18 07:23:15 +02001006 TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001007 if (likely(res != -ELINKCONG)) {
1008 if (res > 0)
1009 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001011 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 if (port_unreliable(p_ptr)) {
Ying Xue9446b872013-10-18 07:23:15 +02001013 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 }
1015 return -ELINKCONG;
1016}