blob: 7e096a5e770156631e701c91fc8c5ac4a34c5ce9 [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 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010045
46#define MAX_REJECT_SIZE 1024
47
Ingo Molnar34af9462006-06-27 02:53:55 -070048DEFINE_SPINLOCK(tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Per Liden4323add2006-01-18 00:38:21 +010050static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010051static void port_handle_node_down(unsigned long ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050052static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
53static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
Per Lidenb97bf3f2006-01-02 19:04:38 +010054static void port_timeout(unsigned long ref);
55
Ben Hutchings2c530402012-07-10 10:55:09 +000056/**
Allan Stephensf0712e862012-04-17 18:42:28 -040057 * tipc_port_peer_msg - verify message was sent by connected port's peer
58 *
59 * Handles cases where the node's network address has changed from
60 * the default of <0.0.0> to its configured setting.
61 */
Allan Stephensf0712e862012-04-17 18:42:28 -040062int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
63{
64 u32 peernode;
65 u32 orignode;
66
Jon Paul Maloyf9fef182014-03-12 11:31:08 -040067 if (msg_origport(msg) != tipc_port_peerport(p_ptr))
Allan Stephensf0712e862012-04-17 18:42:28 -040068 return 0;
69
70 orignode = msg_orignode(msg);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -040071 peernode = tipc_port_peernode(p_ptr);
Allan Stephensf0712e862012-04-17 18:42:28 -040072 return (orignode == peernode) ||
73 (!orignode && (peernode == tipc_own_addr)) ||
74 (!peernode && (orignode == tipc_own_addr));
75}
76
Jon Paul Maloy24be34b2014-03-12 11:31:10 -040077/* tipc_port_init - intiate TIPC port and lock it
78 *
79 * Returns obtained reference if initialization is successful, zero otherwise
80 */
81u32 tipc_port_init(struct tipc_port *p_ptr,
82 const unsigned int importance)
83{
Per Lidenb97bf3f2006-01-02 19:04:38 +010084 struct tipc_msg *msg;
85 u32 ref;
86
Allan Stephens23dd4cc2011-01-07 11:43:40 -050087 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010088 if (!ref) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -040089 pr_warn("Port registration failed, ref. table exhausted\n");
Jon Paul Maloy24be34b2014-03-12 11:31:10 -040090 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +010091 }
92
Allan Stephens23dd4cc2011-01-07 11:43:40 -050093 p_ptr->max_pkt = MAX_PKT_DEFAULT;
94 p_ptr->ref = ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +010095 INIT_LIST_HEAD(&p_ptr->wait_list);
96 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 INIT_LIST_HEAD(&p_ptr->publications);
99 INIT_LIST_HEAD(&p_ptr->port_list);
Allan Stephensf21536d2012-04-17 18:22:49 -0400100
101 /*
102 * Must hold port list lock while initializing message header template
103 * to ensure a change to node's own network address doesn't result
104 * in template containing out-dated network address information
105 */
Allan Stephensf21536d2012-04-17 18:22:49 -0400106 spin_lock_bh(&tipc_port_list_lock);
107 msg = &p_ptr->phdr;
108 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
109 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100111 spin_unlock_bh(&tipc_port_list_lock);
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400112 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113}
114
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400115void tipc_port_destroy(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800117 struct sk_buff *buf = NULL;
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500118 struct tipc_msg *msg = NULL;
119 u32 peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120
Ying Xue84602762013-12-27 10:18:28 +0800121 tipc_withdraw(p_ptr, 0, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122
Ying Xue84602762013-12-27 10:18:28 +0800123 spin_lock_bh(p_ptr->lock);
124 tipc_ref_discard(p_ptr->ref);
125 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126
127 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500128 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100130 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500131 msg = buf_msg(buf);
132 peer = msg_destnode(msg);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400133 tipc_link_xmit(buf, peer, msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 }
Per Liden4323add2006-01-18 00:38:21 +0100135 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 list_del(&p_ptr->port_list);
137 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100138 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 k_term_timer(&p_ptr->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140}
141
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900142/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400143 * port_build_proto_msg(): create connection protocol message for port
144 *
145 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400147static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
148 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149{
150 struct sk_buff *buf;
151 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152
Allan Stephens741d9eb2011-05-31 15:03:18 -0400153 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 if (buf) {
155 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400156 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400157 tipc_port_peernode(p_ptr));
158 msg_set_destport(msg, tipc_port_peerport(p_ptr));
Allan Stephense4a0aee2011-06-01 16:21:12 -0400159 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 msg_set_msgcnt(msg, ack);
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500161 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 }
163 return buf;
164}
165
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166static void port_timeout(unsigned long ref)
167{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500168 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800169 struct sk_buff *buf = NULL;
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500170 struct tipc_msg *msg = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171
Allan Stephens065fd172006-10-16 21:38:05 -0700172 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 return;
174
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500175 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700176 tipc_port_unlock(p_ptr);
177 return;
178 }
179
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 /* Last probe answered ? */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500181 if (p_ptr->probing_state == TIPC_CONN_PROBING) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
183 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400184 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500185 p_ptr->probing_state = TIPC_CONN_PROBING;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
187 }
Per Liden4323add2006-01-18 00:38:21 +0100188 tipc_port_unlock(p_ptr);
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500189 msg = buf_msg(buf);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400190 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191}
192
193
194static void port_handle_node_down(unsigned long ref)
195{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500196 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000197 struct sk_buff *buf = NULL;
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500198 struct tipc_msg *msg = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199
200 if (!p_ptr)
201 return;
202 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100203 tipc_port_unlock(p_ptr);
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500204 msg = buf_msg(buf);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400205 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206}
207
208
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500209static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210{
Allan Stephense244a912011-05-31 16:10:08 -0400211 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212
Allan Stephense244a912011-05-31 16:10:08 -0400213 if (buf) {
214 struct tipc_msg *msg = buf_msg(buf);
215 msg_swap_words(msg, 4, 5);
216 msg_swap_words(msg, 6, 7);
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500217 buf->next = NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400218 }
219 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220}
221
222
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500223static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224{
Allan Stephense244a912011-05-31 16:10:08 -0400225 struct sk_buff *buf;
226 struct tipc_msg *msg;
227 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500229 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800230 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400231
232 buf = tipc_buf_acquire(BASIC_H_SIZE);
233 if (buf) {
234 msg = buf_msg(buf);
235 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
236 msg_set_hdr_sz(msg, BASIC_H_SIZE);
237 msg_set_size(msg, BASIC_H_SIZE);
238 imp = msg_importance(msg);
239 if (imp < TIPC_CRITICAL_IMPORTANCE)
240 msg_set_importance(msg, ++imp);
241 msg_set_errcode(msg, err);
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500242 buf->next = NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400243 }
244 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245}
246
Erik Hugnedc1aed32012-06-29 00:50:23 -0400247static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900249 struct publication *publ;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400250 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251
252 if (full_id)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400253 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
254 tipc_zone(tipc_own_addr),
255 tipc_cluster(tipc_own_addr),
256 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400258 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500260 if (p_ptr->connected) {
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400261 u32 dport = tipc_port_peerport(p_ptr);
262 u32 destnode = tipc_port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263
Erik Hugnedc1aed32012-06-29 00:50:23 -0400264 ret += tipc_snprintf(buf + ret, len - ret,
265 " connected to <%u.%u.%u:%u>",
266 tipc_zone(destnode),
267 tipc_cluster(destnode),
268 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500269 if (p_ptr->conn_type != 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400270 ret += tipc_snprintf(buf + ret, len - ret,
271 " via {%u,%u}", p_ptr->conn_type,
272 p_ptr->conn_instance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500273 } else if (p_ptr->published) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400274 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900275 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 if (publ->lower == publ->upper)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400277 ret += tipc_snprintf(buf + ret, len - ret,
278 " {%u,%u}", publ->type,
279 publ->lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400281 ret += tipc_snprintf(buf + ret, len - ret,
282 " {%u,%u,%u}", publ->type,
283 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900284 }
285 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400286 ret += tipc_snprintf(buf + ret, len - ret, "\n");
287 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288}
289
Per Liden4323add2006-01-18 00:38:21 +0100290struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291{
292 struct sk_buff *buf;
293 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400294 char *pb;
295 int pb_len;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500296 struct tipc_port *p_ptr;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400297 int str_len = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298
Erik Hugnedc1aed32012-06-29 00:50:23 -0400299 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 if (!buf)
301 return NULL;
302 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400303 pb = TLV_DATA(rep_tlv);
304 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305
Per Liden4323add2006-01-18 00:38:21 +0100306 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500308 spin_lock_bh(p_ptr->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400309 str_len += port_print(p_ptr, pb, pb_len, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500310 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 }
Per Liden4323add2006-01-18 00:38:21 +0100312 spin_unlock_bh(&tipc_port_list_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400313 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 skb_put(buf, TLV_SPACE(str_len));
315 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
316
317 return buf;
318}
319
Per Liden4323add2006-01-18 00:38:21 +0100320void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500322 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 struct tipc_msg *msg;
324
Per Liden4323add2006-01-18 00:38:21 +0100325 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500327 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700328 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 msg_set_orignode(msg, tipc_own_addr);
330 }
Per Liden4323add2006-01-18 00:38:21 +0100331 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332}
333
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334void tipc_acknowledge(u32 ref, u32 ack)
335{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500336 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800337 struct sk_buff *buf = NULL;
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500338 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339
Per Liden4323add2006-01-18 00:38:21 +0100340 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 if (!p_ptr)
342 return;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500343 if (p_ptr->connected)
Allan Stephense4a0aee2011-06-01 16:21:12 -0400344 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500345
Per Liden4323add2006-01-18 00:38:21 +0100346 tipc_port_unlock(p_ptr);
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500347 if (!buf)
348 return;
349 msg = buf_msg(buf);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400350 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351}
352
Ying Xue84602762013-12-27 10:18:28 +0800353int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
354 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 struct publication *publ;
357 u32 key;
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800358
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500359 if (p_ptr->connected)
Ying Xue84602762013-12-27 10:18:28 +0800360 return -EINVAL;
361 key = p_ptr->ref + p_ptr->pub_count + 1;
362 if (key == p_ptr->ref)
363 return -EADDRINUSE;
364
Per Liden4323add2006-01-18 00:38:21 +0100365 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500366 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 if (publ) {
368 list_add(&publ->pport_list, &p_ptr->publications);
369 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500370 p_ptr->published = 1;
Ying Xue84602762013-12-27 10:18:28 +0800371 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 }
Ying Xue84602762013-12-27 10:18:28 +0800373 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374}
375
Ying Xue84602762013-12-27 10:18:28 +0800376int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
377 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 struct publication *publ;
380 struct publication *tpubl;
381 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900382
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900384 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900386 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100387 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 }
Allan Stephens0e35fd52008-07-14 22:44:01 -0700389 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900391 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 &p_ptr->publications, pport_list) {
393 if (publ->scope != scope)
394 continue;
395 if (publ->type != seq->type)
396 continue;
397 if (publ->lower != seq->lower)
398 continue;
399 if (publ->upper != seq->upper)
400 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900401 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100402 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700403 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 break;
405 }
406 }
407 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500408 p_ptr->published = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 return res;
410}
411
Ying Xue247f0f32014-02-18 16:06:46 +0800412int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500414 struct tipc_port *p_ptr;
Paul Gortmakerbc879112012-11-29 13:48:40 -0500415 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416
Per Liden4323add2006-01-18 00:38:21 +0100417 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 if (!p_ptr)
419 return -EINVAL;
Ying Xue247f0f32014-02-18 16:06:46 +0800420 res = __tipc_port_connect(ref, p_ptr, peer);
Paul Gortmakerbc879112012-11-29 13:48:40 -0500421 tipc_port_unlock(p_ptr);
422 return res;
423}
424
425/*
Ying Xue247f0f32014-02-18 16:06:46 +0800426 * __tipc_port_connect - connect to a remote peer
Paul Gortmakerbc879112012-11-29 13:48:40 -0500427 *
428 * Port must be locked.
429 */
Ying Xue247f0f32014-02-18 16:06:46 +0800430int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
Paul Gortmakerbc879112012-11-29 13:48:40 -0500431 struct tipc_portid const *peer)
432{
433 struct tipc_msg *msg;
434 int res = -EINVAL;
435
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500436 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 goto exit;
438 if (!peer->ref)
439 goto exit;
440
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500441 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 msg_set_destnode(msg, peer->node);
443 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400445 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +0000446 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447
448 p_ptr->probing_interval = PROBING_INTERVAL;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500449 p_ptr->probing_state = TIPC_CONN_OK;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500450 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
452
Allan Stephens0e659672010-12-31 18:59:32 +0000453 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -0800454 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700456 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500458 p_ptr->max_pkt = tipc_node_get_mtu(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 return res;
460}
461
Paul Gortmakerbc879112012-11-29 13:48:40 -0500462/*
463 * __tipc_disconnect - disconnect port from peer
Allan Stephens0c3141e2008-04-15 00:22:02 -0700464 *
465 * Port must be locked.
466 */
Ying Xue247f0f32014-02-18 16:06:46 +0800467int __tipc_port_disconnect(struct tipc_port *tp_ptr)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700469 if (tp_ptr->connected) {
470 tp_ptr->connected = 0;
471 /* let timer expire on it's own to avoid deadlock! */
Joe Perchese3192692012-06-03 17:41:40 +0000472 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
wangweidong0cee6bb2013-12-12 09:36:39 +0800473 return 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700474 }
wangweidong0cee6bb2013-12-12 09:36:39 +0800475
476 return -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700477}
478
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479/*
Ying Xue247f0f32014-02-18 16:06:46 +0800480 * tipc_port_disconnect(): Disconnect port form peer.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 * This is a node local operation.
482 */
Ying Xue247f0f32014-02-18 16:06:46 +0800483int tipc_port_disconnect(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500485 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700486 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487
Per Liden4323add2006-01-18 00:38:21 +0100488 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 if (!p_ptr)
490 return -EINVAL;
Ying Xue247f0f32014-02-18 16:06:46 +0800491 res = __tipc_port_disconnect(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100492 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 return res;
494}
495
496/*
Ying Xue247f0f32014-02-18 16:06:46 +0800497 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 */
Ying Xue247f0f32014-02-18 16:06:46 +0800499int tipc_port_shutdown(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500{
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500501 struct tipc_msg *msg;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500502 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800503 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504
Per Liden4323add2006-01-18 00:38:21 +0100505 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 if (!p_ptr)
507 return -EINVAL;
508
Allan Stephense244a912011-05-31 16:10:08 -0400509 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +0100510 tipc_port_unlock(p_ptr);
Jon Paul Maloyb786e2b2014-06-25 20:41:39 -0500511 msg = buf_msg(buf);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400512 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
Ying Xue247f0f32014-02-18 16:06:46 +0800513 return tipc_port_disconnect(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514}