blob: 5c14c7801ee65095d809d502cab9f78d33d51e5e [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 */
Jon Paul Maloy5c311422014-03-12 11:31:13 -040083int tipc_port_mcast_xmit(struct tipc_port *oport,
84 struct tipc_name_seq const *seq,
85 struct iovec const *msg_sect,
86 unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +010087{
88 struct tipc_msg *hdr;
89 struct sk_buff *buf;
90 struct sk_buff *ibuf = NULL;
Paul Gortmaker45843102011-12-29 20:33:30 -050091 struct tipc_port_list dports = {0, NULL, };
Per Lidenb97bf3f2006-01-02 19:04:38 +010092 int ext_targets;
93 int res;
94
Per Lidenb97bf3f2006-01-02 19:04:38 +010095 /* Create multicast message */
Allan Stephens23dd4cc2011-01-07 11:43:40 -050096 hdr = &oport->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 msg_set_type(hdr, TIPC_MCAST_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -040098 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
Allan Stephens7462b9e2011-04-18 10:08:22 -040099 msg_set_destport(hdr, 0);
100 msg_set_destnode(hdr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101 msg_set_nametype(hdr, seq->type);
102 msg_set_namelower(hdr, seq->lower);
103 msg_set_nameupper(hdr, seq->upper);
104 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Ying Xue9446b872013-10-18 07:23:15 +0200105 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106 if (unlikely(!buf))
107 return res;
108
109 /* Figure out where to send multicast message */
Per Liden4323add2006-01-18 00:38:21 +0100110 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
111 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900112
113 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 if (ext_targets) {
115 if (dports.count != 0) {
116 ibuf = skb_copy(buf, GFP_ATOMIC);
117 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100118 tipc_port_list_free(&dports);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400119 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 return -ENOMEM;
121 }
122 }
Ying Xue247f0f32014-02-18 16:06:46 +0800123 res = tipc_bclink_xmit(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000124 if ((res < 0) && (dports.count != 0))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400125 kfree_skb(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126 } else {
127 ibuf = buf;
128 }
129
130 if (res >= 0) {
131 if (ibuf)
Ying Xue247f0f32014-02-18 16:06:46 +0800132 tipc_port_mcast_rcv(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133 } else {
Per Liden4323add2006-01-18 00:38:21 +0100134 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 }
136 return res;
137}
138
139/**
Ying Xue247f0f32014-02-18 16:06:46 +0800140 * tipc_port_mcast_rcv - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900141 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 * If there is no port list, perform a lookup to create one
143 */
Ying Xue247f0f32014-02-18 16:06:46 +0800144void tipc_port_mcast_rcv(struct sk_buff *buf, struct tipc_port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145{
Allan Stephens0e659672010-12-31 18:59:32 +0000146 struct tipc_msg *msg;
Paul Gortmaker45843102011-12-29 20:33:30 -0500147 struct tipc_port_list dports = {0, NULL, };
148 struct tipc_port_list *item = dp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 int cnt = 0;
150
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 msg = buf_msg(buf);
152
153 /* Create destination port list, if one wasn't supplied */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100155 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 msg_namelower(msg),
157 msg_nameupper(msg),
158 TIPC_CLUSTER_SCOPE,
159 &dports);
160 item = dp = &dports;
161 }
162
163 /* Deliver a copy of message to each destination port */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 if (dp->count != 0) {
Allan Stephens7f47f5c2011-04-18 10:14:26 -0400165 msg_set_destnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 if (dp->count == 1) {
167 msg_set_destport(msg, dp->ports[0]);
Ying Xue247f0f32014-02-18 16:06:46 +0800168 tipc_port_rcv(buf);
Per Liden4323add2006-01-18 00:38:21 +0100169 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 return;
171 }
172 for (; cnt < dp->count; cnt++) {
173 int index = cnt % PLSIZE;
174 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
175
176 if (b == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400177 pr_warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 goto exit;
179 }
Allan Stephensa0168922010-12-31 18:59:35 +0000180 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000182 msg_set_destport(buf_msg(b), item->ports[index]);
Ying Xue247f0f32014-02-18 16:06:46 +0800183 tipc_port_rcv(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 }
185 }
186exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400187 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100188 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189}
190
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400191
192void tipc_port_wakeup(struct tipc_port *port)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400194 tipc_sock_wakeup(tipc_port_to_sock(port));
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400195}
196
197/* tipc_port_init - intiate TIPC port and lock it
198 *
199 * Returns obtained reference if initialization is successful, zero otherwise
200 */
201u32 tipc_port_init(struct tipc_port *p_ptr,
202 const unsigned int importance)
203{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 struct tipc_msg *msg;
205 u32 ref;
206
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500207 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 if (!ref) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400209 pr_warn("Port registration failed, ref. table exhausted\n");
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400210 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 }
212
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500213 p_ptr->max_pkt = MAX_PKT_DEFAULT;
214 p_ptr->ref = ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 INIT_LIST_HEAD(&p_ptr->wait_list);
216 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 INIT_LIST_HEAD(&p_ptr->publications);
219 INIT_LIST_HEAD(&p_ptr->port_list);
Allan Stephensf21536d2012-04-17 18:22:49 -0400220
221 /*
222 * Must hold port list lock while initializing message header template
223 * to ensure a change to node's own network address doesn't result
224 * in template containing out-dated network address information
225 */
Allan Stephensf21536d2012-04-17 18:22:49 -0400226 spin_lock_bh(&tipc_port_list_lock);
227 msg = &p_ptr->phdr;
228 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
229 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100231 spin_unlock_bh(&tipc_port_list_lock);
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400232 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233}
234
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400235void tipc_port_destroy(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800237 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238
Ying Xue84602762013-12-27 10:18:28 +0800239 tipc_withdraw(p_ptr, 0, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240
Ying Xue84602762013-12-27 10:18:28 +0800241 spin_lock_bh(p_ptr->lock);
242 tipc_ref_discard(p_ptr->ref);
243 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244
245 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500246 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100248 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250
Per Liden4323add2006-01-18 00:38:21 +0100251 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 list_del(&p_ptr->port_list);
253 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100254 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 k_term_timer(&p_ptr->timer);
Per Liden4323add2006-01-18 00:38:21 +0100256 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257}
258
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900259/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400260 * port_build_proto_msg(): create connection protocol message for port
261 *
262 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400264static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
265 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266{
267 struct sk_buff *buf;
268 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900269
Allan Stephens741d9eb2011-05-31 15:03:18 -0400270 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 if (buf) {
272 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400273 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400274 tipc_port_peernode(p_ptr));
275 msg_set_destport(msg, tipc_port_peerport(p_ptr));
Allan Stephense4a0aee2011-06-01 16:21:12 -0400276 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 }
279 return buf;
280}
281
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282int tipc_reject_msg(struct sk_buff *buf, u32 err)
283{
284 struct tipc_msg *msg = buf_msg(buf);
285 struct sk_buff *rbuf;
286 struct tipc_msg *rmsg;
287 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400288 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400290 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400291 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292
293 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400294 if (WARN(!msg_isdata(msg),
295 "attempt to reject message with user=%u", msg_user(msg))) {
296 dump_stack();
297 goto exit;
298 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400299 if (msg_errcode(msg) || msg_dest_droppable(msg))
300 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400302 /*
303 * construct returned message by copying rejected message header and
304 * data (or subset), then updating header fields that need adjusting
305 */
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400306 hdr_sz = msg_hdr_sz(msg);
307 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
308
309 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400310 if (rbuf == NULL)
311 goto exit;
312
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400314 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
315
316 if (msg_connected(rmsg)) {
317 imp = msg_importance(rmsg);
318 if (imp < TIPC_CRITICAL_IMPORTANCE)
319 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700320 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400321 msg_set_non_seq(rmsg, 0);
322 msg_set_size(rmsg, rmsg_sz);
323 msg_set_errcode(rmsg, err);
324 msg_set_prevnode(rmsg, tipc_own_addr);
325 msg_swap_words(rmsg, 4, 5);
326 if (!msg_short(rmsg))
327 msg_swap_words(rmsg, 6, 7);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328
329 /* send self-abort message when rejecting on a connected port */
330 if (msg_connected(msg)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500331 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332
333 if (p_ptr) {
Allan Stephensdff10e92011-11-02 10:32:14 -0400334 struct sk_buff *abuf = NULL;
335
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500336 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100338 tipc_port_unlock(p_ptr);
Allan Stephensdff10e92011-11-02 10:32:14 -0400339 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 }
342
Allan Stephensacc631b2011-05-23 13:47:44 -0400343 /* send returned message & dispose of rejected message */
Allan Stephens017dac32011-05-24 13:20:09 -0400344 src_node = msg_prevnode(msg);
Allan Stephens630d9202012-04-18 09:42:29 -0400345 if (in_own_node(src_node))
Ying Xue247f0f32014-02-18 16:06:46 +0800346 tipc_port_rcv(rbuf);
Allan Stephens017dac32011-05-24 13:20:09 -0400347 else
Ying Xue247f0f32014-02-18 16:06:46 +0800348 tipc_link_xmit(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400349exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400350 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 return data_sz;
352}
353
Ying Xue247f0f32014-02-18 16:06:46 +0800354int tipc_port_iovec_reject(struct tipc_port *p_ptr, struct tipc_msg *hdr,
355 struct iovec const *msg_sect, unsigned int len,
356 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357{
358 struct sk_buff *buf;
359 int res;
360
Ying Xue9446b872013-10-18 07:23:15 +0200361 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 if (!buf)
363 return res;
364
365 return tipc_reject_msg(buf, err);
366}
367
368static void port_timeout(unsigned long ref)
369{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500370 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800371 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372
Allan Stephens065fd172006-10-16 21:38:05 -0700373 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 return;
375
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500376 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700377 tipc_port_unlock(p_ptr);
378 return;
379 }
380
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381 /* Last probe answered ? */
382 if (p_ptr->probing_state == PROBING) {
383 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
384 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400385 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 p_ptr->probing_state = PROBING;
387 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
388 }
Per Liden4323add2006-01-18 00:38:21 +0100389 tipc_port_unlock(p_ptr);
390 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391}
392
393
394static void port_handle_node_down(unsigned long ref)
395{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500396 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000397 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398
399 if (!p_ptr)
400 return;
401 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100402 tipc_port_unlock(p_ptr);
403 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404}
405
406
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500407static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408{
Allan Stephense244a912011-05-31 16:10:08 -0400409 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410
Allan Stephense244a912011-05-31 16:10:08 -0400411 if (buf) {
412 struct tipc_msg *msg = buf_msg(buf);
413 msg_swap_words(msg, 4, 5);
414 msg_swap_words(msg, 6, 7);
415 }
416 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417}
418
419
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500420static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421{
Allan Stephense244a912011-05-31 16:10:08 -0400422 struct sk_buff *buf;
423 struct tipc_msg *msg;
424 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500426 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800427 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400428
429 buf = tipc_buf_acquire(BASIC_H_SIZE);
430 if (buf) {
431 msg = buf_msg(buf);
432 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
433 msg_set_hdr_sz(msg, BASIC_H_SIZE);
434 msg_set_size(msg, BASIC_H_SIZE);
435 imp = msg_importance(msg);
436 if (imp < TIPC_CRITICAL_IMPORTANCE)
437 msg_set_importance(msg, ++imp);
438 msg_set_errcode(msg, err);
439 }
440 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441}
442
Ying Xue247f0f32014-02-18 16:06:46 +0800443void tipc_port_proto_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444{
445 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400446 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800447 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400448 u32 destport = msg_destport(msg);
449 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450
Allan Stephens1c1a5512011-06-01 15:08:10 -0400451 /* Validate connection */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400452 p_ptr = tipc_port_lock(destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400453 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400454 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
455 if (r_buf) {
456 msg = buf_msg(r_buf);
457 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
Allan Stephensf0712e862012-04-17 18:42:28 -0400458 BASIC_H_SIZE, msg_orignode(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400459 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
460 msg_set_origport(msg, destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400461 msg_set_destport(msg, msg_origport(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400462 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400463 if (p_ptr)
464 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 goto exit;
466 }
467
Allan Stephens1c1a5512011-06-01 15:08:10 -0400468 /* Process protocol message sent by peer */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400469 switch (msg_type(msg)) {
470 case CONN_ACK:
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400471 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400472 p_ptr->acked += msg_msgcnt(msg);
473 if (!tipc_port_congested(p_ptr)) {
474 p_ptr->congested = 0;
475 if (wakeable)
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400476 tipc_port_wakeup(p_ptr);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400477 }
478 break;
479 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400480 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400481 break;
482 default:
483 /* CONN_PROBE_REPLY or unrecognized - no action required */
484 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 }
486 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400487 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488exit:
Per Liden4323add2006-01-18 00:38:21 +0100489 tipc_net_route_msg(r_buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400490 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491}
492
Erik Hugnedc1aed32012-06-29 00:50:23 -0400493static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900495 struct publication *publ;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400496 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497
498 if (full_id)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400499 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
500 tipc_zone(tipc_own_addr),
501 tipc_cluster(tipc_own_addr),
502 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400504 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500506 if (p_ptr->connected) {
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400507 u32 dport = tipc_port_peerport(p_ptr);
508 u32 destnode = tipc_port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509
Erik Hugnedc1aed32012-06-29 00:50:23 -0400510 ret += tipc_snprintf(buf + ret, len - ret,
511 " connected to <%u.%u.%u:%u>",
512 tipc_zone(destnode),
513 tipc_cluster(destnode),
514 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500515 if (p_ptr->conn_type != 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400516 ret += tipc_snprintf(buf + ret, len - ret,
517 " via {%u,%u}", p_ptr->conn_type,
518 p_ptr->conn_instance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500519 } else if (p_ptr->published) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400520 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900521 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 if (publ->lower == publ->upper)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400523 ret += tipc_snprintf(buf + ret, len - ret,
524 " {%u,%u}", publ->type,
525 publ->lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400527 ret += tipc_snprintf(buf + ret, len - ret,
528 " {%u,%u,%u}", publ->type,
529 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900530 }
531 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400532 ret += tipc_snprintf(buf + ret, len - ret, "\n");
533 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534}
535
Per Liden4323add2006-01-18 00:38:21 +0100536struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537{
538 struct sk_buff *buf;
539 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400540 char *pb;
541 int pb_len;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500542 struct tipc_port *p_ptr;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400543 int str_len = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544
Erik Hugnedc1aed32012-06-29 00:50:23 -0400545 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 if (!buf)
547 return NULL;
548 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400549 pb = TLV_DATA(rep_tlv);
550 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551
Per Liden4323add2006-01-18 00:38:21 +0100552 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500554 spin_lock_bh(p_ptr->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400555 str_len += port_print(p_ptr, pb, pb_len, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500556 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 }
Per Liden4323add2006-01-18 00:38:21 +0100558 spin_unlock_bh(&tipc_port_list_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400559 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 skb_put(buf, TLV_SPACE(str_len));
561 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
562
563 return buf;
564}
565
Per Liden4323add2006-01-18 00:38:21 +0100566void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500568 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 struct tipc_msg *msg;
570
Per Liden4323add2006-01-18 00:38:21 +0100571 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500573 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700574 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 msg_set_orignode(msg, tipc_own_addr);
576 }
Per Liden4323add2006-01-18 00:38:21 +0100577 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578}
579
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580void tipc_acknowledge(u32 ref, u32 ack)
581{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500582 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800583 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584
Per Liden4323add2006-01-18 00:38:21 +0100585 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 if (!p_ptr)
587 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500588 if (p_ptr->connected) {
589 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400590 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 }
Per Liden4323add2006-01-18 00:38:21 +0100592 tipc_port_unlock(p_ptr);
593 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594}
595
Ying Xue84602762013-12-27 10:18:28 +0800596int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
597 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 struct publication *publ;
600 u32 key;
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800601
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500602 if (p_ptr->connected)
Ying Xue84602762013-12-27 10:18:28 +0800603 return -EINVAL;
604 key = p_ptr->ref + p_ptr->pub_count + 1;
605 if (key == p_ptr->ref)
606 return -EADDRINUSE;
607
Per Liden4323add2006-01-18 00:38:21 +0100608 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500609 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 if (publ) {
611 list_add(&publ->pport_list, &p_ptr->publications);
612 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500613 p_ptr->published = 1;
Ying Xue84602762013-12-27 10:18:28 +0800614 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 }
Ying Xue84602762013-12-27 10:18:28 +0800616 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617}
618
Ying Xue84602762013-12-27 10:18:28 +0800619int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
620 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 struct publication *publ;
623 struct publication *tpubl;
624 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900625
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900627 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900629 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100630 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 }
Allan Stephens0e35fd52008-07-14 22:44:01 -0700632 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900634 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 &p_ptr->publications, pport_list) {
636 if (publ->scope != scope)
637 continue;
638 if (publ->type != seq->type)
639 continue;
640 if (publ->lower != seq->lower)
641 continue;
642 if (publ->upper != seq->upper)
643 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900644 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100645 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700646 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 break;
648 }
649 }
650 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500651 p_ptr->published = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 return res;
653}
654
Ying Xue247f0f32014-02-18 16:06:46 +0800655int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500657 struct tipc_port *p_ptr;
Paul Gortmakerbc879112012-11-29 13:48:40 -0500658 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659
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;
Ying Xue247f0f32014-02-18 16:06:46 +0800663 res = __tipc_port_connect(ref, p_ptr, peer);
Paul Gortmakerbc879112012-11-29 13:48:40 -0500664 tipc_port_unlock(p_ptr);
665 return res;
666}
667
668/*
Ying Xue247f0f32014-02-18 16:06:46 +0800669 * __tipc_port_connect - connect to a remote peer
Paul Gortmakerbc879112012-11-29 13:48:40 -0500670 *
671 * Port must be locked.
672 */
Ying Xue247f0f32014-02-18 16:06:46 +0800673int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
Paul Gortmakerbc879112012-11-29 13:48:40 -0500674 struct tipc_portid const *peer)
675{
676 struct tipc_msg *msg;
677 int res = -EINVAL;
678
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500679 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 goto exit;
681 if (!peer->ref)
682 goto exit;
683
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500684 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 msg_set_destnode(msg, peer->node);
686 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400688 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +0000689 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690
691 p_ptr->probing_interval = PROBING_INTERVAL;
692 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500693 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
695
Allan Stephens0e659672010-12-31 18:59:32 +0000696 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -0800697 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700699 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700exit:
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500701 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702 return res;
703}
704
Paul Gortmakerbc879112012-11-29 13:48:40 -0500705/*
706 * __tipc_disconnect - disconnect port from peer
Allan Stephens0c3141e2008-04-15 00:22:02 -0700707 *
708 * Port must be locked.
709 */
Ying Xue247f0f32014-02-18 16:06:46 +0800710int __tipc_port_disconnect(struct tipc_port *tp_ptr)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700711{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700712 if (tp_ptr->connected) {
713 tp_ptr->connected = 0;
714 /* let timer expire on it's own to avoid deadlock! */
Joe Perchese3192692012-06-03 17:41:40 +0000715 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
wangweidong0cee6bb2013-12-12 09:36:39 +0800716 return 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700717 }
wangweidong0cee6bb2013-12-12 09:36:39 +0800718
719 return -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700720}
721
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722/*
Ying Xue247f0f32014-02-18 16:06:46 +0800723 * tipc_port_disconnect(): Disconnect port form peer.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 * This is a node local operation.
725 */
Ying Xue247f0f32014-02-18 16:06:46 +0800726int tipc_port_disconnect(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500728 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700729 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730
Per Liden4323add2006-01-18 00:38:21 +0100731 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 if (!p_ptr)
733 return -EINVAL;
Ying Xue247f0f32014-02-18 16:06:46 +0800734 res = __tipc_port_disconnect(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100735 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736 return res;
737}
738
739/*
Ying Xue247f0f32014-02-18 16:06:46 +0800740 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 */
Ying Xue247f0f32014-02-18 16:06:46 +0800742int tipc_port_shutdown(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500744 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800745 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746
Per Liden4323add2006-01-18 00:38:21 +0100747 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 if (!p_ptr)
749 return -EINVAL;
750
Allan Stephense244a912011-05-31 16:10:08 -0400751 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +0100752 tipc_port_unlock(p_ptr);
753 tipc_net_route_msg(buf);
Ying Xue247f0f32014-02-18 16:06:46 +0800754 return tipc_port_disconnect(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755}
756
Allan Stephens9b641252011-11-09 13:29:18 -0500757/**
Ying Xue247f0f32014-02-18 16:06:46 +0800758 * tipc_port_rcv - receive message from lower layer and deliver to port user
Allan Stephens9b641252011-11-09 13:29:18 -0500759 */
Ying Xue247f0f32014-02-18 16:06:46 +0800760int tipc_port_rcv(struct sk_buff *buf)
Allan Stephens9b641252011-11-09 13:29:18 -0500761{
762 struct tipc_port *p_ptr;
763 struct tipc_msg *msg = buf_msg(buf);
764 u32 destport = msg_destport(msg);
765 u32 dsz = msg_data_sz(msg);
766 u32 err;
767
768 /* forward unresolved named message */
769 if (unlikely(!destport)) {
770 tipc_net_route_msg(buf);
771 return dsz;
772 }
773
774 /* validate destination & pass to port, otherwise reject message */
775 p_ptr = tipc_port_lock(destport);
776 if (likely(p_ptr)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400777 err = tipc_sk_rcv(&tipc_port_to_sock(p_ptr)->sk, buf);
Allan Stephens9b641252011-11-09 13:29:18 -0500778 tipc_port_unlock(p_ptr);
779 if (likely(!err))
780 return dsz;
781 } else {
782 err = TIPC_ERR_NO_PORT;
783 }
Allan Stephensf0712e862012-04-17 18:42:28 -0400784
Allan Stephens9b641252011-11-09 13:29:18 -0500785 return tipc_reject_msg(buf, err);
786}
787
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788/*
Ying Xue247f0f32014-02-18 16:06:46 +0800789 * tipc_port_iovec_rcv: Concatenate and deliver sectioned
790 * message for this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 */
Ying Xue247f0f32014-02-18 16:06:46 +0800792static int tipc_port_iovec_rcv(struct tipc_port *sender,
793 struct iovec const *msg_sect,
794 unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795{
796 struct sk_buff *buf;
797 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900798
Ying Xue9446b872013-10-18 07:23:15 +0200799 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 if (likely(buf))
Ying Xue247f0f32014-02-18 16:06:46 +0800801 tipc_port_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 return res;
803}
804
805/**
806 * tipc_send - send message sections on connection
807 */
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400808int tipc_send(struct tipc_port *p_ptr,
809 struct iovec const *msg_sect,
810 unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100811{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 u32 destnode;
813 int res;
814
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400815 if (!p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 return -EINVAL;
817
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500818 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +0100819 if (!tipc_port_congested(p_ptr)) {
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400820 destnode = tipc_port_peernode(p_ptr);
Allan Stephens8a55fe72012-04-18 09:27:22 -0400821 if (likely(!in_own_node(destnode)))
Ying Xue247f0f32014-02-18 16:06:46 +0800822 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
823 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 else
Ying Xue247f0f32014-02-18 16:06:46 +0800825 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826
827 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500828 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500829 if (res > 0)
830 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831 return res;
832 }
833 }
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -0400834 if (tipc_port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500835 p_ptr->congested = 0;
Ying Xue9446b872013-10-18 07:23:15 +0200836 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837 }
838 return -ELINKCONG;
839}
840
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900841/**
Allan Stephens12bae472010-11-30 12:01:02 +0000842 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 */
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400844int tipc_send2name(struct tipc_port *p_ptr,
845 struct tipc_name const *name,
846 unsigned int domain,
847 struct iovec const *msg_sect,
848 unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 struct tipc_msg *msg;
851 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +0000852 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 int res;
854
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400855 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 return -EINVAL;
857
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500858 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -0400860 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 msg_set_nametype(msg, name->type);
862 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000863 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +0100864 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 msg_set_destnode(msg, destnode);
866 msg_set_destport(msg, destport);
867
Allan Stephensbc9f8142011-11-07 17:00:54 -0500868 if (likely(destport || destnode)) {
Allan Stephens8a55fe72012-04-18 09:27:22 -0400869 if (likely(in_own_node(destnode)))
Ying Xue247f0f32014-02-18 16:06:46 +0800870 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400871 else if (tipc_own_addr)
Ying Xue247f0f32014-02-18 16:06:46 +0800872 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
873 destnode);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400874 else
Ying Xue247f0f32014-02-18 16:06:46 +0800875 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect,
876 len, TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500877 if (likely(res != -ELINKCONG)) {
878 if (res > 0)
879 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500881 }
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -0400882 if (tipc_port_unreliable(p_ptr))
Ying Xue9446b872013-10-18 07:23:15 +0200883 return len;
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -0400884
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 return -ELINKCONG;
886 }
Ying Xue247f0f32014-02-18 16:06:46 +0800887 return tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
888 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889}
890
891/**
Allan Stephens12bae472010-11-30 12:01:02 +0000892 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 */
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400894int tipc_send2port(struct tipc_port *p_ptr,
895 struct tipc_portid const *dest,
896 struct iovec const *msg_sect,
897 unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 struct tipc_msg *msg;
900 int res;
901
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400902 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 return -EINVAL;
904
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500905 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400907 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 msg_set_destnode(msg, dest->node);
909 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -0400910 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500911
Allan Stephens8a55fe72012-04-18 09:27:22 -0400912 if (in_own_node(dest->node))
Ying Xue247f0f32014-02-18 16:06:46 +0800913 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400914 else if (tipc_own_addr)
Ying Xue247f0f32014-02-18 16:06:46 +0800915 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
916 dest->node);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400917 else
Ying Xue247f0f32014-02-18 16:06:46 +0800918 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
Ying Xue9446b872013-10-18 07:23:15 +0200919 TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500920 if (likely(res != -ELINKCONG)) {
921 if (res > 0)
922 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500924 }
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -0400925 if (tipc_port_unreliable(p_ptr))
Ying Xue9446b872013-10-18 07:23:15 +0200926 return len;
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -0400927
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 return -ELINKCONG;
929}