blob: d43f3182b1d4cc1628817d882c9d006006e27e1e [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 *
Allan Stephens05646c92007-06-10 17:25:24 -07004 * Copyright (c) 1992-2007, 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
42/* Connection management: */
43#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
44#define CONFIRMED 0
45#define PROBING 1
46
47#define MAX_REJECT_SIZE 1024
48
Ingo Molnar34af9462006-06-27 02:53:55 -070049DEFINE_SPINLOCK(tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
Per Liden4323add2006-01-18 00:38:21 +010051static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010052static void port_handle_node_down(unsigned long ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050053static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
54static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
Per Lidenb97bf3f2006-01-02 19:04:38 +010055static void port_timeout(unsigned long ref);
56
57
Paul Gortmaker872f24d2012-04-23 04:49:13 +000058static u32 port_peernode(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010059{
Allan Stephens23dd4cc2011-01-07 11:43:40 -050060 return msg_destnode(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010061}
62
Paul Gortmaker872f24d2012-04-23 04:49:13 +000063static u32 port_peerport(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010064{
Allan Stephens23dd4cc2011-01-07 11:43:40 -050065 return msg_destport(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010066}
67
Ben Hutchings2c530402012-07-10 10:55:09 +000068/**
Allan Stephensf0712e862012-04-17 18:42:28 -040069 * tipc_port_peer_msg - verify message was sent by connected port's peer
70 *
71 * Handles cases where the node's network address has changed from
72 * the default of <0.0.0> to its configured setting.
73 */
Allan Stephensf0712e862012-04-17 18:42:28 -040074int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
75{
76 u32 peernode;
77 u32 orignode;
78
79 if (msg_origport(msg) != port_peerport(p_ptr))
80 return 0;
81
82 orignode = msg_orignode(msg);
83 peernode = port_peernode(p_ptr);
84 return (orignode == peernode) ||
85 (!orignode && (peernode == tipc_own_addr)) ||
86 (!peernode && (orignode == tipc_own_addr));
87}
88
Per Lidenb97bf3f2006-01-02 19:04:38 +010089/**
90 * tipc_multicast - send a multicast message to local and remote destinations
91 */
Allan Stephens38f232e2010-11-30 12:00:59 +000092int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
Ying Xue9446b872013-10-18 07:23:15 +020093 struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +010094{
95 struct tipc_msg *hdr;
96 struct sk_buff *buf;
97 struct sk_buff *ibuf = NULL;
Paul Gortmaker45843102011-12-29 20:33:30 -050098 struct tipc_port_list dports = {0, NULL, };
Allan Stephens23dd4cc2011-01-07 11:43:40 -050099 struct tipc_port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100 int ext_targets;
101 int res;
102
103 if (unlikely(!oport))
104 return -EINVAL;
105
106 /* Create multicast message */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500107 hdr = &oport->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108 msg_set_type(hdr, TIPC_MCAST_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400109 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
Allan Stephens7462b9e2011-04-18 10:08:22 -0400110 msg_set_destport(hdr, 0);
111 msg_set_destnode(hdr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 msg_set_nametype(hdr, seq->type);
113 msg_set_namelower(hdr, seq->lower);
114 msg_set_nameupper(hdr, seq->upper);
115 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Ying Xue9446b872013-10-18 07:23:15 +0200116 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117 if (unlikely(!buf))
118 return res;
119
120 /* Figure out where to send multicast message */
Per Liden4323add2006-01-18 00:38:21 +0100121 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
122 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900123
124 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 if (ext_targets) {
126 if (dports.count != 0) {
127 ibuf = skb_copy(buf, GFP_ATOMIC);
128 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100129 tipc_port_list_free(&dports);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400130 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 return -ENOMEM;
132 }
133 }
Per Liden4323add2006-01-18 00:38:21 +0100134 res = tipc_bclink_send_msg(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000135 if ((res < 0) && (dports.count != 0))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400136 kfree_skb(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 } else {
138 ibuf = buf;
139 }
140
141 if (res >= 0) {
142 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100143 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 } else {
Per Liden4323add2006-01-18 00:38:21 +0100145 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 }
147 return res;
148}
149
150/**
Per Liden4323add2006-01-18 00:38:21 +0100151 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 * If there is no port list, perform a lookup to create one
154 */
Paul Gortmaker45843102011-12-29 20:33:30 -0500155void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156{
Allan Stephens0e659672010-12-31 18:59:32 +0000157 struct tipc_msg *msg;
Paul Gortmaker45843102011-12-29 20:33:30 -0500158 struct tipc_port_list dports = {0, NULL, };
159 struct tipc_port_list *item = dp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 int cnt = 0;
161
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 msg = buf_msg(buf);
163
164 /* Create destination port list, if one wasn't supplied */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100166 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 msg_namelower(msg),
168 msg_nameupper(msg),
169 TIPC_CLUSTER_SCOPE,
170 &dports);
171 item = dp = &dports;
172 }
173
174 /* Deliver a copy of message to each destination port */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 if (dp->count != 0) {
Allan Stephens7f47f5c2011-04-18 10:14:26 -0400176 msg_set_destnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 if (dp->count == 1) {
178 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100179 tipc_port_recv_msg(buf);
180 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 return;
182 }
183 for (; cnt < dp->count; cnt++) {
184 int index = cnt % PLSIZE;
185 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
186
187 if (b == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400188 pr_warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 goto exit;
190 }
Allan Stephensa0168922010-12-31 18:59:35 +0000191 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000193 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100194 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195 }
196 }
197exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400198 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100199 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200}
201
202/**
Ying Xue3c5db8e2013-06-17 10:54:44 -0400203 * tipc_createport - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900204 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700205 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 */
Ying Xuec0fee8a2013-06-17 10:54:46 -0400207struct tipc_port *tipc_createport(struct sock *sk,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400208 u32 (*dispatcher)(struct tipc_port *,
209 struct sk_buff *),
210 void (*wakeup)(struct tipc_port *),
211 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500213 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 struct tipc_msg *msg;
215 u32 ref;
216
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700217 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700218 if (!p_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400219 pr_warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700220 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500222 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 if (!ref) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400224 pr_warn("Port creation failed, ref. table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700226 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 }
228
Ying Xuec0fee8a2013-06-17 10:54:46 -0400229 p_ptr->sk = sk;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500230 p_ptr->max_pkt = MAX_PKT_DEFAULT;
231 p_ptr->ref = ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 INIT_LIST_HEAD(&p_ptr->wait_list);
233 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 p_ptr->dispatcher = dispatcher;
235 p_ptr->wakeup = wakeup;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237 INIT_LIST_HEAD(&p_ptr->publications);
238 INIT_LIST_HEAD(&p_ptr->port_list);
Allan Stephensf21536d2012-04-17 18:22:49 -0400239
240 /*
241 * Must hold port list lock while initializing message header template
242 * to ensure a change to node's own network address doesn't result
243 * in template containing out-dated network address information
244 */
Allan Stephensf21536d2012-04-17 18:22:49 -0400245 spin_lock_bh(&tipc_port_list_lock);
246 msg = &p_ptr->phdr;
247 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
248 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100250 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500251 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252}
253
Ying Xue84602762013-12-27 10:18:28 +0800254int tipc_deleteport(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800256 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257
Ying Xue84602762013-12-27 10:18:28 +0800258 tipc_withdraw(p_ptr, 0, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259
Ying Xue84602762013-12-27 10:18:28 +0800260 spin_lock_bh(p_ptr->lock);
261 tipc_ref_discard(p_ptr->ref);
262 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263
264 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500265 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100267 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269
Per Liden4323add2006-01-18 00:38:21 +0100270 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 list_del(&p_ptr->port_list);
272 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100273 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274 k_term_timer(&p_ptr->timer);
275 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100276 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700277 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278}
279
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500280static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500282 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283}
284
285int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
286{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500287 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900288
Per Liden4323add2006-01-18 00:38:21 +0100289 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 if (!p_ptr)
291 return -EINVAL;
292 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800293 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700294 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295}
296
297int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
298{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500299 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900300
Per Liden4323add2006-01-18 00:38:21 +0100301 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 if (!p_ptr)
303 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500304 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100305 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700306 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307}
308
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500309static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500311 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312}
313
314int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
315{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500316 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900317
Per Liden4323add2006-01-18 00:38:21 +0100318 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 if (!p_ptr)
320 return -EINVAL;
321 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800322 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700323 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324}
325
326int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
327{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500328 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900329
Per Liden4323add2006-01-18 00:38:21 +0100330 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 if (!p_ptr)
332 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500333 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100334 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700335 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336}
337
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900338/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400339 * port_build_proto_msg(): create connection protocol message for port
340 *
341 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400343static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
344 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345{
346 struct sk_buff *buf;
347 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348
Allan Stephens741d9eb2011-05-31 15:03:18 -0400349 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 if (buf) {
351 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400352 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
353 port_peernode(p_ptr));
354 msg_set_destport(msg, port_peerport(p_ptr));
355 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 }
358 return buf;
359}
360
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361int tipc_reject_msg(struct sk_buff *buf, u32 err)
362{
363 struct tipc_msg *msg = buf_msg(buf);
364 struct sk_buff *rbuf;
365 struct tipc_msg *rmsg;
366 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400367 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400369 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400370 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371
372 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400373 if (WARN(!msg_isdata(msg),
374 "attempt to reject message with user=%u", msg_user(msg))) {
375 dump_stack();
376 goto exit;
377 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400378 if (msg_errcode(msg) || msg_dest_droppable(msg))
379 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400381 /*
382 * construct returned message by copying rejected message header and
383 * data (or subset), then updating header fields that need adjusting
384 */
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400385 hdr_sz = msg_hdr_sz(msg);
386 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
387
388 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400389 if (rbuf == NULL)
390 goto exit;
391
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400393 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
394
395 if (msg_connected(rmsg)) {
396 imp = msg_importance(rmsg);
397 if (imp < TIPC_CRITICAL_IMPORTANCE)
398 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700399 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400400 msg_set_non_seq(rmsg, 0);
401 msg_set_size(rmsg, rmsg_sz);
402 msg_set_errcode(rmsg, err);
403 msg_set_prevnode(rmsg, tipc_own_addr);
404 msg_swap_words(rmsg, 4, 5);
405 if (!msg_short(rmsg))
406 msg_swap_words(rmsg, 6, 7);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407
408 /* send self-abort message when rejecting on a connected port */
409 if (msg_connected(msg)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500410 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411
412 if (p_ptr) {
Allan Stephensdff10e92011-11-02 10:32:14 -0400413 struct sk_buff *abuf = NULL;
414
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500415 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100417 tipc_port_unlock(p_ptr);
Allan Stephensdff10e92011-11-02 10:32:14 -0400418 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 }
421
Allan Stephensacc631b2011-05-23 13:47:44 -0400422 /* send returned message & dispose of rejected message */
Allan Stephens017dac32011-05-24 13:20:09 -0400423 src_node = msg_prevnode(msg);
Allan Stephens630d9202012-04-18 09:42:29 -0400424 if (in_own_node(src_node))
Allan Stephens017dac32011-05-24 13:20:09 -0400425 tipc_port_recv_msg(rbuf);
426 else
427 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400428exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400429 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 return data_sz;
431}
432
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500433int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Ying Xue9446b872013-10-18 07:23:15 +0200434 struct iovec const *msg_sect, unsigned int len,
435 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436{
437 struct sk_buff *buf;
438 int res;
439
Ying Xue9446b872013-10-18 07:23:15 +0200440 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 if (!buf)
442 return res;
443
444 return tipc_reject_msg(buf, err);
445}
446
447static void port_timeout(unsigned long ref)
448{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500449 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800450 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451
Allan Stephens065fd172006-10-16 21:38:05 -0700452 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 return;
454
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500455 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700456 tipc_port_unlock(p_ptr);
457 return;
458 }
459
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 /* Last probe answered ? */
461 if (p_ptr->probing_state == PROBING) {
462 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
463 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400464 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 p_ptr->probing_state = PROBING;
466 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
467 }
Per Liden4323add2006-01-18 00:38:21 +0100468 tipc_port_unlock(p_ptr);
469 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470}
471
472
473static void port_handle_node_down(unsigned long ref)
474{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500475 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000476 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477
478 if (!p_ptr)
479 return;
480 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100481 tipc_port_unlock(p_ptr);
482 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483}
484
485
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500486static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487{
Allan Stephense244a912011-05-31 16:10:08 -0400488 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489
Allan Stephense244a912011-05-31 16:10:08 -0400490 if (buf) {
491 struct tipc_msg *msg = buf_msg(buf);
492 msg_swap_words(msg, 4, 5);
493 msg_swap_words(msg, 6, 7);
494 }
495 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496}
497
498
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500499static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500{
Allan Stephense244a912011-05-31 16:10:08 -0400501 struct sk_buff *buf;
502 struct tipc_msg *msg;
503 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500505 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800506 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400507
508 buf = tipc_buf_acquire(BASIC_H_SIZE);
509 if (buf) {
510 msg = buf_msg(buf);
511 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
512 msg_set_hdr_sz(msg, BASIC_H_SIZE);
513 msg_set_size(msg, BASIC_H_SIZE);
514 imp = msg_importance(msg);
515 if (imp < TIPC_CRITICAL_IMPORTANCE)
516 msg_set_importance(msg, ++imp);
517 msg_set_errcode(msg, err);
518 }
519 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520}
521
Per Liden4323add2006-01-18 00:38:21 +0100522void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523{
524 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400525 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800526 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400527 u32 destport = msg_destport(msg);
528 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529
Allan Stephens1c1a5512011-06-01 15:08:10 -0400530 /* Validate connection */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400531 p_ptr = tipc_port_lock(destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400532 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400533 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
534 if (r_buf) {
535 msg = buf_msg(r_buf);
536 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
Allan Stephensf0712e862012-04-17 18:42:28 -0400537 BASIC_H_SIZE, msg_orignode(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400538 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
539 msg_set_origport(msg, destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400540 msg_set_destport(msg, msg_origport(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400541 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400542 if (p_ptr)
543 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 goto exit;
545 }
546
Allan Stephens1c1a5512011-06-01 15:08:10 -0400547 /* Process protocol message sent by peer */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400548 switch (msg_type(msg)) {
549 case CONN_ACK:
550 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
551 p_ptr->wakeup;
552 p_ptr->acked += msg_msgcnt(msg);
553 if (!tipc_port_congested(p_ptr)) {
554 p_ptr->congested = 0;
555 if (wakeable)
556 p_ptr->wakeup(p_ptr);
557 }
558 break;
559 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400560 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400561 break;
562 default:
563 /* CONN_PROBE_REPLY or unrecognized - no action required */
564 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 }
566 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400567 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568exit:
Per Liden4323add2006-01-18 00:38:21 +0100569 tipc_net_route_msg(r_buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400570 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571}
572
Erik Hugnedc1aed32012-06-29 00:50:23 -0400573static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900575 struct publication *publ;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400576 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577
578 if (full_id)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400579 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
580 tipc_zone(tipc_own_addr),
581 tipc_cluster(tipc_own_addr),
582 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400584 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500586 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587 u32 dport = port_peerport(p_ptr);
588 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589
Erik Hugnedc1aed32012-06-29 00:50:23 -0400590 ret += tipc_snprintf(buf + ret, len - ret,
591 " connected to <%u.%u.%u:%u>",
592 tipc_zone(destnode),
593 tipc_cluster(destnode),
594 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500595 if (p_ptr->conn_type != 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400596 ret += tipc_snprintf(buf + ret, len - ret,
597 " via {%u,%u}", p_ptr->conn_type,
598 p_ptr->conn_instance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500599 } else if (p_ptr->published) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400600 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900601 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 if (publ->lower == publ->upper)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400603 ret += tipc_snprintf(buf + ret, len - ret,
604 " {%u,%u}", publ->type,
605 publ->lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400607 ret += tipc_snprintf(buf + ret, len - ret,
608 " {%u,%u,%u}", publ->type,
609 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 }
611 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400612 ret += tipc_snprintf(buf + ret, len - ret, "\n");
613 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614}
615
Per Liden4323add2006-01-18 00:38:21 +0100616struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617{
618 struct sk_buff *buf;
619 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400620 char *pb;
621 int pb_len;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500622 struct tipc_port *p_ptr;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400623 int str_len = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624
Erik Hugnedc1aed32012-06-29 00:50:23 -0400625 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 if (!buf)
627 return NULL;
628 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400629 pb = TLV_DATA(rep_tlv);
630 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631
Per Liden4323add2006-01-18 00:38:21 +0100632 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500634 spin_lock_bh(p_ptr->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400635 str_len += port_print(p_ptr, pb, pb_len, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500636 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 }
Per Liden4323add2006-01-18 00:38:21 +0100638 spin_unlock_bh(&tipc_port_list_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400639 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 skb_put(buf, TLV_SPACE(str_len));
641 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
642
643 return buf;
644}
645
Per Liden4323add2006-01-18 00:38:21 +0100646void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500648 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 struct tipc_msg *msg;
650
Per Liden4323add2006-01-18 00:38:21 +0100651 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500653 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700654 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 msg_set_orignode(msg, tipc_own_addr);
656 }
Per Liden4323add2006-01-18 00:38:21 +0100657 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658}
659
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660void tipc_acknowledge(u32 ref, u32 ack)
661{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500662 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800663 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664
Per Liden4323add2006-01-18 00:38:21 +0100665 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 if (!p_ptr)
667 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500668 if (p_ptr->connected) {
669 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400670 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 }
Per Liden4323add2006-01-18 00:38:21 +0100672 tipc_port_unlock(p_ptr);
673 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674}
675
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676int tipc_portimportance(u32 ref, unsigned int *importance)
677{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500678 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900679
Per Liden4323add2006-01-18 00:38:21 +0100680 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 if (!p_ptr)
682 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500683 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800684 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700685 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686}
687
688int tipc_set_portimportance(u32 ref, unsigned int imp)
689{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500690 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691
692 if (imp > TIPC_CRITICAL_IMPORTANCE)
693 return -EINVAL;
694
Per Liden4323add2006-01-18 00:38:21 +0100695 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 if (!p_ptr)
697 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500698 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800699 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700700 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701}
702
703
Ying Xue84602762013-12-27 10:18:28 +0800704int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
705 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100707 struct publication *publ;
708 u32 key;
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800709
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500710 if (p_ptr->connected)
Ying Xue84602762013-12-27 10:18:28 +0800711 return -EINVAL;
712 key = p_ptr->ref + p_ptr->pub_count + 1;
713 if (key == p_ptr->ref)
714 return -EADDRINUSE;
715
Per Liden4323add2006-01-18 00:38:21 +0100716 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500717 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 if (publ) {
719 list_add(&publ->pport_list, &p_ptr->publications);
720 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500721 p_ptr->published = 1;
Ying Xue84602762013-12-27 10:18:28 +0800722 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723 }
Ying Xue84602762013-12-27 10:18:28 +0800724 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725}
726
Ying Xue84602762013-12-27 10:18:28 +0800727int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
728 struct tipc_name_seq const *seq)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 struct publication *publ;
731 struct publication *tpubl;
732 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900733
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900735 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100738 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 }
Allan Stephens0e35fd52008-07-14 22:44:01 -0700740 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900742 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 &p_ptr->publications, pport_list) {
744 if (publ->scope != scope)
745 continue;
746 if (publ->type != seq->type)
747 continue;
748 if (publ->lower != seq->lower)
749 continue;
750 if (publ->upper != seq->upper)
751 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900752 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +0100753 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700754 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 break;
756 }
757 }
758 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500759 p_ptr->published = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 return res;
761}
762
Paul Gortmakerbc879112012-11-29 13:48:40 -0500763int tipc_connect(u32 ref, struct tipc_portid const *peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500765 struct tipc_port *p_ptr;
Paul Gortmakerbc879112012-11-29 13:48:40 -0500766 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767
Per Liden4323add2006-01-18 00:38:21 +0100768 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 if (!p_ptr)
770 return -EINVAL;
Paul Gortmakerbc879112012-11-29 13:48:40 -0500771 res = __tipc_connect(ref, p_ptr, peer);
772 tipc_port_unlock(p_ptr);
773 return res;
774}
775
776/*
777 * __tipc_connect - connect to a remote peer
778 *
779 * Port must be locked.
780 */
781int __tipc_connect(u32 ref, struct tipc_port *p_ptr,
782 struct tipc_portid const *peer)
783{
784 struct tipc_msg *msg;
785 int res = -EINVAL;
786
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500787 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 goto exit;
789 if (!peer->ref)
790 goto exit;
791
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500792 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 msg_set_destnode(msg, peer->node);
794 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400796 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +0000797 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798
799 p_ptr->probing_interval = PROBING_INTERVAL;
800 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500801 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
803
Allan Stephens0e659672010-12-31 18:59:32 +0000804 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -0800805 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700807 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808exit:
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500809 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 return res;
811}
812
Paul Gortmakerbc879112012-11-29 13:48:40 -0500813/*
814 * __tipc_disconnect - disconnect port from peer
Allan Stephens0c3141e2008-04-15 00:22:02 -0700815 *
816 * Port must be locked.
817 */
Paul Gortmakerbc879112012-11-29 13:48:40 -0500818int __tipc_disconnect(struct tipc_port *tp_ptr)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700819{
820 int res;
821
822 if (tp_ptr->connected) {
823 tp_ptr->connected = 0;
824 /* let timer expire on it's own to avoid deadlock! */
Joe Perchese3192692012-06-03 17:41:40 +0000825 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700826 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700827 } else {
828 res = -ENOTCONN;
829 }
830 return res;
831}
832
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833/*
834 * tipc_disconnect(): Disconnect port form peer.
835 * This is a node local operation.
836 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837int tipc_disconnect(u32 ref)
838{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500839 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700840 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841
Per Liden4323add2006-01-18 00:38:21 +0100842 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 if (!p_ptr)
844 return -EINVAL;
Paul Gortmakerbc879112012-11-29 13:48:40 -0500845 res = __tipc_disconnect(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100846 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 return res;
848}
849
850/*
851 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
852 */
853int tipc_shutdown(u32 ref)
854{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500855 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800856 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857
Per Liden4323add2006-01-18 00:38:21 +0100858 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 if (!p_ptr)
860 return -EINVAL;
861
Allan Stephense244a912011-05-31 16:10:08 -0400862 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +0100863 tipc_port_unlock(p_ptr);
864 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 return tipc_disconnect(ref);
866}
867
Allan Stephens9b641252011-11-09 13:29:18 -0500868/**
869 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
870 */
Allan Stephens9b641252011-11-09 13:29:18 -0500871int tipc_port_recv_msg(struct sk_buff *buf)
872{
873 struct tipc_port *p_ptr;
874 struct tipc_msg *msg = buf_msg(buf);
875 u32 destport = msg_destport(msg);
876 u32 dsz = msg_data_sz(msg);
877 u32 err;
878
879 /* forward unresolved named message */
880 if (unlikely(!destport)) {
881 tipc_net_route_msg(buf);
882 return dsz;
883 }
884
885 /* validate destination & pass to port, otherwise reject message */
886 p_ptr = tipc_port_lock(destport);
887 if (likely(p_ptr)) {
Allan Stephens9b641252011-11-09 13:29:18 -0500888 err = p_ptr->dispatcher(p_ptr, buf);
889 tipc_port_unlock(p_ptr);
890 if (likely(!err))
891 return dsz;
892 } else {
893 err = TIPC_ERR_NO_PORT;
894 }
Allan Stephensf0712e862012-04-17 18:42:28 -0400895
Allan Stephens9b641252011-11-09 13:29:18 -0500896 return tipc_reject_msg(buf, err);
897}
898
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899/*
Per Liden4323add2006-01-18 00:38:21 +0100900 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 * message for this node.
902 */
Ying Xue9446b872013-10-18 07:23:15 +0200903static int tipc_port_recv_sections(struct tipc_port *sender,
Allan Stephens26896902011-04-21 10:42:07 -0500904 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +0200905 unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906{
907 struct sk_buff *buf;
908 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900909
Ying Xue9446b872013-10-18 07:23:15 +0200910 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +0100912 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 return res;
914}
915
916/**
917 * tipc_send - send message sections on connection
918 */
Ying Xue9446b872013-10-18 07:23:15 +0200919int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500921 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 u32 destnode;
923 int res;
924
Per Liden4323add2006-01-18 00:38:21 +0100925 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500926 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 return -EINVAL;
928
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500929 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +0100930 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 destnode = port_peernode(p_ptr);
Allan Stephens8a55fe72012-04-18 09:27:22 -0400932 if (likely(!in_own_node(destnode)))
Ying Xue9446b872013-10-18 07:23:15 +0200933 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
934 len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 else
Ying Xue9446b872013-10-18 07:23:15 +0200936 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937
938 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500939 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500940 if (res > 0)
941 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 return res;
943 }
944 }
945 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500946 p_ptr->congested = 0;
Ying Xue9446b872013-10-18 07:23:15 +0200947 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 }
949 return -ELINKCONG;
950}
951
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900952/**
Allan Stephens12bae472010-11-30 12:01:02 +0000953 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 */
Allan Stephens12bae472010-11-30 12:01:02 +0000955int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Ying Xue9446b872013-10-18 07:23:15 +0200956 struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500958 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 struct tipc_msg *msg;
960 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +0000961 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962 int res;
963
Per Liden4323add2006-01-18 00:38:21 +0100964 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500965 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 return -EINVAL;
967
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500968 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -0400970 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 msg_set_nametype(msg, name->type);
972 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000973 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +0100974 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 msg_set_destnode(msg, destnode);
976 msg_set_destport(msg, destport);
977
Allan Stephensbc9f8142011-11-07 17:00:54 -0500978 if (likely(destport || destnode)) {
Allan Stephens8a55fe72012-04-18 09:27:22 -0400979 if (likely(in_own_node(destnode)))
Ying Xue9446b872013-10-18 07:23:15 +0200980 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400981 else if (tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -0500982 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +0200983 len, destnode);
Allan Stephensb8f683d2012-04-18 09:22:56 -0400984 else
985 res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +0200986 len, TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500987 if (likely(res != -ELINKCONG)) {
988 if (res > 0)
989 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500991 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 if (port_unreliable(p_ptr)) {
Ying Xue9446b872013-10-18 07:23:15 +0200993 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994 }
995 return -ELINKCONG;
996 }
Ying Xue9446b872013-10-18 07:23:15 +0200997 return tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
998 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999}
1000
1001/**
Allan Stephens12bae472010-11-30 12:01:02 +00001002 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 */
Allan Stephens12bae472010-11-30 12:01:02 +00001004int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Ying Xue9446b872013-10-18 07:23:15 +02001005 struct iovec const *msg_sect, unsigned int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001007 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 struct tipc_msg *msg;
1009 int res;
1010
Per Liden4323add2006-01-18 00:38:21 +01001011 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001012 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 return -EINVAL;
1014
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001015 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001017 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 msg_set_destnode(msg, dest->node);
1019 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001020 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001021
Allan Stephens8a55fe72012-04-18 09:27:22 -04001022 if (in_own_node(dest->node))
Ying Xue9446b872013-10-18 07:23:15 +02001023 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001024 else if (tipc_own_addr)
Ying Xue9446b872013-10-18 07:23:15 +02001025 res = tipc_link_send_sections_fast(p_ptr, msg_sect, len,
1026 dest->node);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001027 else
Ying Xue9446b872013-10-18 07:23:15 +02001028 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
1029 TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001030 if (likely(res != -ELINKCONG)) {
1031 if (res > 0)
1032 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001034 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 if (port_unreliable(p_ptr)) {
Ying Xue9446b872013-10-18 07:23:15 +02001036 return len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 }
1038 return -ELINKCONG;
1039}