blob: 1b20b963a2fc9f8644e6ada2e408caaf3dfa3213 [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
Allan Stephens23dd4cc2011-01-07 11:43:40 -05005 * Copyright (c) 2004-2008, 2010-2011, 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
Allan Stephense3ec9c72010-12-31 18:59:34 +000049static struct sk_buff *msg_queue_head;
50static struct sk_buff *msg_queue_tail;
Per Lidenb97bf3f2006-01-02 19:04:38 +010051
Ingo Molnar34af9462006-06-27 02:53:55 -070052DEFINE_SPINLOCK(tipc_port_list_lock);
53static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Per Liden4323add2006-01-18 00:38:21 +010055static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010056static void port_handle_node_down(unsigned long ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050057static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
58static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
Per Lidenb97bf3f2006-01-02 19:04:38 +010059static void port_timeout(unsigned long ref);
60
61
Allan Stephens23dd4cc2011-01-07 11:43:40 -050062static u32 port_peernode(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010063{
Allan Stephens23dd4cc2011-01-07 11:43:40 -050064 return msg_destnode(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010065}
66
Allan Stephens23dd4cc2011-01-07 11:43:40 -050067static u32 port_peerport(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010068{
Allan Stephens23dd4cc2011-01-07 11:43:40 -050069 return msg_destport(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010070}
71
Per Lidenb97bf3f2006-01-02 19:04:38 +010072/**
73 * tipc_multicast - send a multicast message to local and remote destinations
74 */
75
Allan Stephens38f232e2010-11-30 12:00:59 +000076int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
Allan Stephens26896902011-04-21 10:42:07 -050077 u32 num_sect, struct iovec const *msg_sect,
78 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +010079{
80 struct tipc_msg *hdr;
81 struct sk_buff *buf;
82 struct sk_buff *ibuf = NULL;
83 struct port_list dports = {0, NULL, };
Allan Stephens23dd4cc2011-01-07 11:43:40 -050084 struct tipc_port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010085 int ext_targets;
86 int res;
87
88 if (unlikely(!oport))
89 return -EINVAL;
90
91 /* Create multicast message */
92
Allan Stephens23dd4cc2011-01-07 11:43:40 -050093 hdr = &oport->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 msg_set_type(hdr, TIPC_MCAST_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -040095 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
Allan Stephens7462b9e2011-04-18 10:08:22 -040096 msg_set_destport(hdr, 0);
97 msg_set_destnode(hdr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 msg_set_nametype(hdr, seq->type);
99 msg_set_namelower(hdr, seq->lower);
100 msg_set_nameupper(hdr, seq->upper);
101 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephens26896902011-04-21 10:42:07 -0500102 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 !oport->user_port, &buf);
104 if (unlikely(!buf))
105 return res;
106
107 /* Figure out where to send multicast message */
108
Per Liden4323add2006-01-18 00:38:21 +0100109 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
110 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900111
112 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113
114 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);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 buf_discard(buf);
120 return -ENOMEM;
121 }
122 }
Per Liden4323add2006-01-18 00:38:21 +0100123 res = tipc_bclink_send_msg(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000124 if ((res < 0) && (dports.count != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 buf_discard(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126 } else {
127 ibuf = buf;
128 }
129
130 if (res >= 0) {
131 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100132 tipc_port_recv_mcast(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/**
Per Liden4323add2006-01-18 00:38:21 +0100140 * tipc_port_recv_mcast - 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 */
144
Per Liden4323add2006-01-18 00:38:21 +0100145void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146{
Allan Stephens0e659672010-12-31 18:59:32 +0000147 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 struct port_list dports = {0, NULL, };
149 struct port_list *item = dp;
150 int cnt = 0;
151
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 msg = buf_msg(buf);
153
154 /* Create destination port list, if one wasn't supplied */
155
156 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100157 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 msg_namelower(msg),
159 msg_nameupper(msg),
160 TIPC_CLUSTER_SCOPE,
161 &dports);
162 item = dp = &dports;
163 }
164
165 /* Deliver a copy of message to each destination port */
166
167 if (dp->count != 0) {
Allan Stephens7f47f5c2011-04-18 10:14:26 -0400168 msg_set_destnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 if (dp->count == 1) {
170 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100171 tipc_port_recv_msg(buf);
172 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 return;
174 }
175 for (; cnt < dp->count; cnt++) {
176 int index = cnt % PLSIZE;
177 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
178
179 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700180 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 goto exit;
182 }
Allan Stephensa0168922010-12-31 18:59:35 +0000183 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000185 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100186 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 }
188 }
189exit:
190 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100191 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192}
193
194/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700195 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900196 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700197 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 */
199
Allan Stephens0ea52242008-07-14 22:42:19 -0700200struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
202 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700203 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500205 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 struct tipc_msg *msg;
207 u32 ref;
208
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700209 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700210 if (!p_ptr) {
211 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700212 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500214 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700216 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700218 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 }
220
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500221 p_ptr->usr_handle = usr_handle;
222 p_ptr->max_pkt = MAX_PKT_DEFAULT;
223 p_ptr->ref = ref;
224 msg = &p_ptr->phdr;
Allan Stephens741d9eb2011-05-31 15:03:18 -0400225 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 INIT_LIST_HEAD(&p_ptr->wait_list);
228 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 p_ptr->dispatcher = dispatcher;
230 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800231 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100233 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 INIT_LIST_HEAD(&p_ptr->publications);
235 INIT_LIST_HEAD(&p_ptr->port_list);
236 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100237 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500238 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239}
240
241int tipc_deleteport(u32 ref)
242{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500243 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800244 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800246 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100247 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900248 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 return -EINVAL;
250
Per Liden4323add2006-01-18 00:38:21 +0100251 tipc_ref_discard(ref);
252 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253
254 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500255 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100257 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 }
Allan Stephense83504f2010-12-31 18:59:30 +0000259 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260
Per Liden4323add2006-01-18 00:38:21 +0100261 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 list_del(&p_ptr->port_list);
263 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100264 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 k_term_timer(&p_ptr->timer);
266 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100267 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700268 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269}
270
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500271static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500273 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274}
275
276int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
277{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500278 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900279
Per Liden4323add2006-01-18 00:38:21 +0100280 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 if (!p_ptr)
282 return -EINVAL;
283 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800284 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700285 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286}
287
288int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
289{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500290 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900291
Per Liden4323add2006-01-18 00:38:21 +0100292 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 if (!p_ptr)
294 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500295 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100296 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700297 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298}
299
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500300static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500302 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303}
304
305int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
306{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500307 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900308
Per Liden4323add2006-01-18 00:38:21 +0100309 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 if (!p_ptr)
311 return -EINVAL;
312 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800313 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700314 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315}
316
317int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
318{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500319 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320
Per Liden4323add2006-01-18 00:38:21 +0100321 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 if (!p_ptr)
323 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500324 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100325 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700326 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327}
328
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900329/*
330 * port_build_proto_msg(): build a port level protocol
331 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 * tipc_port lock on.
333 */
334static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
335 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900336 u32 usr, u32 type, u32 err,
Allan Stephens741de3e2011-01-25 13:33:31 -0500337 u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338{
339 struct sk_buff *buf;
340 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900341
Allan Stephens741d9eb2011-05-31 15:03:18 -0400342 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 if (buf) {
344 msg = buf_msg(buf);
Allan Stephens741d9eb2011-05-31 15:03:18 -0400345 tipc_msg_init(msg, usr, type, INT_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700346 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 msg_set_destport(msg, destport);
348 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 msg_set_orignode(msg, orignode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 }
352 return buf;
353}
354
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355int tipc_reject_msg(struct sk_buff *buf, u32 err)
356{
357 struct tipc_msg *msg = buf_msg(buf);
358 struct sk_buff *rbuf;
359 struct tipc_msg *rmsg;
360 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400361 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400363 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400364 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365
366 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400367
368 if (WARN(!msg_isdata(msg),
369 "attempt to reject message with user=%u", msg_user(msg))) {
370 dump_stack();
371 goto exit;
372 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400373 if (msg_errcode(msg) || msg_dest_droppable(msg))
374 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400376 /*
377 * construct returned message by copying rejected message header and
378 * data (or subset), then updating header fields that need adjusting
379 */
380
381 hdr_sz = msg_hdr_sz(msg);
382 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
383
384 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400385 if (rbuf == NULL)
386 goto exit;
387
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400389 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
390
391 if (msg_connected(rmsg)) {
392 imp = msg_importance(rmsg);
393 if (imp < TIPC_CRITICAL_IMPORTANCE)
394 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700395 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400396 msg_set_non_seq(rmsg, 0);
397 msg_set_size(rmsg, rmsg_sz);
398 msg_set_errcode(rmsg, err);
399 msg_set_prevnode(rmsg, tipc_own_addr);
400 msg_swap_words(rmsg, 4, 5);
401 if (!msg_short(rmsg))
402 msg_swap_words(rmsg, 6, 7);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403
404 /* send self-abort message when rejecting on a connected port */
405 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800406 struct sk_buff *abuf = NULL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500407 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408
409 if (p_ptr) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500410 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100412 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 }
Per Liden4323add2006-01-18 00:38:21 +0100414 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 }
416
Allan Stephensacc631b2011-05-23 13:47:44 -0400417 /* send returned message & dispose of rejected message */
418
Allan Stephens017dac32011-05-24 13:20:09 -0400419 src_node = msg_prevnode(msg);
420 if (src_node == tipc_own_addr)
421 tipc_port_recv_msg(rbuf);
422 else
423 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400424exit:
425 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 return data_sz;
427}
428
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500429int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100430 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500431 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432{
433 struct sk_buff *buf;
434 int res;
435
Allan Stephens26896902011-04-21 10:42:07 -0500436 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 !p_ptr->user_port, &buf);
438 if (!buf)
439 return res;
440
441 return tipc_reject_msg(buf, err);
442}
443
444static void port_timeout(unsigned long ref)
445{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500446 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800447 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448
Allan Stephens065fd172006-10-16 21:38:05 -0700449 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 return;
451
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500452 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700453 tipc_port_unlock(p_ptr);
454 return;
455 }
456
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 /* Last probe answered ? */
458 if (p_ptr->probing_state == PROBING) {
459 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
460 } else {
461 buf = port_build_proto_msg(port_peerport(p_ptr),
462 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500463 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 tipc_own_addr,
465 CONN_MANAGER,
466 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900467 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 p_ptr->probing_state = PROBING;
470 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
471 }
Per Liden4323add2006-01-18 00:38:21 +0100472 tipc_port_unlock(p_ptr);
473 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474}
475
476
477static void port_handle_node_down(unsigned long ref)
478{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500479 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000480 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481
482 if (!p_ptr)
483 return;
484 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100485 tipc_port_unlock(p_ptr);
486 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487}
488
489
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500490static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491{
Allan Stephense244a912011-05-31 16:10:08 -0400492 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493
Allan Stephense244a912011-05-31 16:10:08 -0400494 if (buf) {
495 struct tipc_msg *msg = buf_msg(buf);
496 msg_swap_words(msg, 4, 5);
497 msg_swap_words(msg, 6, 7);
498 }
499 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500}
501
502
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500503static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504{
Allan Stephense244a912011-05-31 16:10:08 -0400505 struct sk_buff *buf;
506 struct tipc_msg *msg;
507 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500509 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800510 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400511
512 buf = tipc_buf_acquire(BASIC_H_SIZE);
513 if (buf) {
514 msg = buf_msg(buf);
515 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
516 msg_set_hdr_sz(msg, BASIC_H_SIZE);
517 msg_set_size(msg, BASIC_H_SIZE);
518 imp = msg_importance(msg);
519 if (imp < TIPC_CRITICAL_IMPORTANCE)
520 msg_set_importance(msg, ++imp);
521 msg_set_errcode(msg, err);
522 }
523 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524}
525
Per Liden4323add2006-01-18 00:38:21 +0100526void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527{
528 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400529 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800530 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400531 u32 orignode = msg_orignode(msg);
532 u32 origport = msg_origport(msg);
533 u32 destport = msg_destport(msg);
534 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535
Allan Stephens1c1a5512011-06-01 15:08:10 -0400536 /* Validate connection */
537
538 p_ptr = tipc_port_lock(destport);
539 if (!p_ptr || !p_ptr->connected ||
540 (port_peernode(p_ptr) != orignode) ||
541 (port_peerport(p_ptr) != origport)) {
542 r_buf = port_build_proto_msg(origport,
543 orignode,
544 destport,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800546 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 TIPC_CONN_MSG,
Allan Stephens1c1a5512011-06-01 15:08:10 -0400548 TIPC_ERR_NO_PORT,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400550 if (p_ptr)
551 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 goto exit;
553 }
554
Allan Stephens1c1a5512011-06-01 15:08:10 -0400555 /* Process protocol message sent by peer */
556
557 switch (msg_type(msg)) {
558 case CONN_ACK:
559 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
560 p_ptr->wakeup;
561 p_ptr->acked += msg_msgcnt(msg);
562 if (!tipc_port_congested(p_ptr)) {
563 p_ptr->congested = 0;
564 if (wakeable)
565 p_ptr->wakeup(p_ptr);
566 }
567 break;
568 case CONN_PROBE:
569 r_buf = port_build_proto_msg(origport,
570 orignode,
571 destport,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900572 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 CONN_MANAGER,
574 CONN_PROBE_REPLY,
575 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400577 break;
578 default:
579 /* CONN_PROBE_REPLY or unrecognized - no action required */
580 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 }
582 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400583 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584exit:
Per Liden4323add2006-01-18 00:38:21 +0100585 tipc_net_route_msg(r_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 buf_discard(buf);
587}
588
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500589static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900591 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592
593 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900594 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500596 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500598 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500600 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900601 u32 dport = port_peerport(p_ptr);
602 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
605 tipc_zone(destnode), tipc_cluster(destnode),
606 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500607 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500609 p_ptr->conn_type,
610 p_ptr->conn_instance);
611 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 tipc_printf(buf, " bound to");
613 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 if (publ->lower == publ->upper)
615 tipc_printf(buf, " {%u,%u}", publ->type,
616 publ->lower);
617 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 }
621 }
622 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623}
624
625#define MAX_PORT_QUERY 32768
626
Per Liden4323add2006-01-18 00:38:21 +0100627struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628{
629 struct sk_buff *buf;
630 struct tlv_desc *rep_tlv;
631 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500632 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 int str_len;
634
Per Liden4323add2006-01-18 00:38:21 +0100635 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 if (!buf)
637 return NULL;
638 rep_tlv = (struct tlv_desc *)buf->data;
639
Per Liden4323add2006-01-18 00:38:21 +0100640 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
641 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500643 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500645 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 }
Per Liden4323add2006-01-18 00:38:21 +0100647 spin_unlock_bh(&tipc_port_list_lock);
648 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649
650 skb_put(buf, TLV_SPACE(str_len));
651 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
652
653 return buf;
654}
655
Per Liden4323add2006-01-18 00:38:21 +0100656void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500658 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 struct tipc_msg *msg;
660
Per Liden4323add2006-01-18 00:38:21 +0100661 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500663 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 if (msg_orignode(msg) == tipc_own_addr)
665 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700666 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 msg_set_orignode(msg, tipc_own_addr);
668 }
Per Liden4323add2006-01-18 00:38:21 +0100669 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670}
671
672
673/*
674 * port_dispatcher_sigh(): Signal handler for messages destinated
675 * to the tipc_port interface.
676 */
677
678static void port_dispatcher_sigh(void *dummy)
679{
680 struct sk_buff *buf;
681
682 spin_lock_bh(&queue_lock);
683 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800684 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 spin_unlock_bh(&queue_lock);
686
687 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500688 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 struct user_port *up_ptr;
690 struct tipc_portid orig;
691 struct tipc_name_seq dseq;
692 void *usr_handle;
693 int connected;
694 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700695 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696
697 struct sk_buff *next = buf->next;
698 struct tipc_msg *msg = buf_msg(buf);
699 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900700
Allan Stephens96882432006-06-25 23:38:58 -0700701 message_type = msg_type(msg);
702 if (message_type > TIPC_DIRECT_MSG)
703 goto reject; /* Unsupported message type */
704
Per Liden4323add2006-01-18 00:38:21 +0100705 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700706 if (!p_ptr)
707 goto reject; /* Port deleted while msg in queue */
708
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 orig.ref = msg_origport(msg);
710 orig.node = msg_orignode(msg);
711 up_ptr = p_ptr->user_port;
712 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500713 connected = p_ptr->connected;
714 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715
716 if (unlikely(msg_errcode(msg)))
717 goto err;
718
Allan Stephens96882432006-06-25 23:38:58 -0700719 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900720
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 case TIPC_CONN_MSG:{
722 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
723 u32 peer_port = port_peerport(p_ptr);
724 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500725 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726
Julia Lawall4cec72c2008-01-08 23:48:20 -0800727 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700728 if (unlikely(!cb))
729 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700731 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700733 } else if ((msg_origport(msg) != peer_port) ||
734 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500736 dsz = msg_data_sz(msg);
737 if (unlikely(dsz &&
738 (++p_ptr->conn_unacked >=
739 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900740 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500741 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500743 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 break;
745 }
746 case TIPC_DIRECT_MSG:{
747 tipc_msg_event cb = up_ptr->msg_cb;
748
Julia Lawall4cec72c2008-01-08 23:48:20 -0800749 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700750 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 goto reject;
752 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900753 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 msg_data_sz(msg), msg_importance(msg),
755 &orig);
756 break;
757 }
Allan Stephens96882432006-06-25 23:38:58 -0700758 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 case TIPC_NAMED_MSG:{
760 tipc_named_msg_event cb = up_ptr->named_msg_cb;
761
Julia Lawall4cec72c2008-01-08 23:48:20 -0800762 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700763 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 goto reject;
765 dseq.type = msg_nametype(msg);
766 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700767 dseq.upper = (message_type == TIPC_NAMED_MSG)
768 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900770 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 msg_data_sz(msg), msg_importance(msg),
772 &orig, &dseq);
773 break;
774 }
775 }
776 if (buf)
777 buf_discard(buf);
778 buf = next;
779 continue;
780err:
Allan Stephens96882432006-06-25 23:38:58 -0700781 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900782
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900784 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 up_ptr->conn_err_cb;
786 u32 peer_port = port_peerport(p_ptr);
787 u32 peer_node = port_peernode(p_ptr);
788
Julia Lawall4cec72c2008-01-08 23:48:20 -0800789 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700790 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700792 if ((msg_origport(msg) != peer_port) ||
793 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 break;
795 tipc_disconnect(dref);
796 skb_pull(buf, msg_hdr_sz(msg));
797 cb(usr_handle, dref, &buf, msg_data(msg),
798 msg_data_sz(msg), msg_errcode(msg));
799 break;
800 }
801 case TIPC_DIRECT_MSG:{
802 tipc_msg_err_event cb = up_ptr->err_cb;
803
Julia Lawall4cec72c2008-01-08 23:48:20 -0800804 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700805 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 break;
807 skb_pull(buf, msg_hdr_sz(msg));
808 cb(usr_handle, dref, &buf, msg_data(msg),
809 msg_data_sz(msg), msg_errcode(msg), &orig);
810 break;
811 }
Allan Stephens96882432006-06-25 23:38:58 -0700812 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900814 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 up_ptr->named_err_cb;
816
Julia Lawall4cec72c2008-01-08 23:48:20 -0800817 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700818 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 break;
820 dseq.type = msg_nametype(msg);
821 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700822 dseq.upper = (message_type == TIPC_NAMED_MSG)
823 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900825 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 msg_data_sz(msg), msg_errcode(msg), &dseq);
827 break;
828 }
829 }
830 if (buf)
831 buf_discard(buf);
832 buf = next;
833 continue;
834reject:
835 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
836 buf = next;
837 }
838}
839
840/*
841 * port_dispatcher(): Dispatcher for messages destinated
842 * to the tipc_port interface. Called with port locked.
843 */
844
845static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
846{
847 buf->next = NULL;
848 spin_lock_bh(&queue_lock);
849 if (msg_queue_head) {
850 msg_queue_tail->next = buf;
851 msg_queue_tail = buf;
852 } else {
853 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100854 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 }
856 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700857 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858}
859
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900860/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900862 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 */
864
865static void port_wakeup_sh(unsigned long ref)
866{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500867 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800869 tipc_continue_event cb = NULL;
870 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871
Per Liden4323add2006-01-18 00:38:21 +0100872 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 if (p_ptr) {
874 up_ptr = p_ptr->user_port;
875 if (up_ptr) {
876 cb = up_ptr->continue_event_cb;
877 uh = up_ptr->usr_handle;
878 }
Per Liden4323add2006-01-18 00:38:21 +0100879 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 }
881 if (cb)
882 cb(uh, ref);
883}
884
885
886static void port_wakeup(struct tipc_port *p_ptr)
887{
Per Liden4323add2006-01-18 00:38:21 +0100888 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889}
890
891void tipc_acknowledge(u32 ref, u32 ack)
892{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500893 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800894 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895
Per Liden4323add2006-01-18 00:38:21 +0100896 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 if (!p_ptr)
898 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500899 if (p_ptr->connected) {
900 p_ptr->conn_unacked -= ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 buf = port_build_proto_msg(port_peerport(p_ptr),
902 port_peernode(p_ptr),
903 ref,
904 tipc_own_addr,
905 CONN_MANAGER,
906 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900907 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 ack);
909 }
Per Liden4323add2006-01-18 00:38:21 +0100910 tipc_port_unlock(p_ptr);
911 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912}
913
914/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000915 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 */
917
Allan Stephensb0c1e922010-12-31 18:59:22 +0000918int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900919 unsigned int importance,
920 tipc_msg_err_event error_cb,
921 tipc_named_msg_err_event named_error_cb,
922 tipc_conn_shutdown_event conn_error_cb,
923 tipc_msg_event msg_cb,
924 tipc_named_msg_event named_msg_cb,
925 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 tipc_continue_event continue_event_cb,/* May be zero */
927 u32 *portref)
928{
929 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500930 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700932 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700933 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700934 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 return -ENOMEM;
936 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500937 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700938 port_wakeup, importance);
939 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 kfree(up_ptr);
941 return -ENOMEM;
942 }
943
944 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500946 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 up_ptr->err_cb = error_cb;
948 up_ptr->named_err_cb = named_error_cb;
949 up_ptr->conn_err_cb = conn_error_cb;
950 up_ptr->msg_cb = msg_cb;
951 up_ptr->named_msg_cb = named_msg_cb;
952 up_ptr->conn_msg_cb = conn_msg_cb;
953 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500954 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100955 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700956 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957}
958
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959int tipc_portimportance(u32 ref, unsigned int *importance)
960{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500961 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900962
Per Liden4323add2006-01-18 00:38:21 +0100963 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 if (!p_ptr)
965 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500966 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800967 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700968 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969}
970
971int tipc_set_portimportance(u32 ref, unsigned int imp)
972{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500973 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974
975 if (imp > TIPC_CRITICAL_IMPORTANCE)
976 return -EINVAL;
977
Per Liden4323add2006-01-18 00:38:21 +0100978 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979 if (!p_ptr)
980 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500981 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800982 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700983 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984}
985
986
987int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
988{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500989 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 struct publication *publ;
991 u32 key;
992 int res = -EINVAL;
993
Per Liden4323add2006-01-18 00:38:21 +0100994 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800995 if (!p_ptr)
996 return -EINVAL;
997
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500998 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 goto exit;
1000 if (seq->lower > seq->upper)
1001 goto exit;
1002 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1003 goto exit;
1004 key = ref + p_ptr->pub_count + 1;
1005 if (key == ref) {
1006 res = -EADDRINUSE;
1007 goto exit;
1008 }
Per Liden4323add2006-01-18 00:38:21 +01001009 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001010 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 if (publ) {
1012 list_add(&publ->pport_list, &p_ptr->publications);
1013 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001014 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001015 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 }
1017exit:
Per Liden4323add2006-01-18 00:38:21 +01001018 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 return res;
1020}
1021
1022int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1023{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001024 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 struct publication *publ;
1026 struct publication *tpubl;
1027 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001028
Per Liden4323add2006-01-18 00:38:21 +01001029 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 if (!p_ptr)
1031 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001035 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001036 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001038 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001040 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 &p_ptr->publications, pport_list) {
1042 if (publ->scope != scope)
1043 continue;
1044 if (publ->type != seq->type)
1045 continue;
1046 if (publ->lower != seq->lower)
1047 continue;
1048 if (publ->upper != seq->upper)
1049 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001050 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001051 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001052 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 break;
1054 }
1055 }
1056 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001057 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001058 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 return res;
1060}
1061
1062int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1063{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001064 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 struct tipc_msg *msg;
1066 int res = -EINVAL;
1067
Per Liden4323add2006-01-18 00:38:21 +01001068 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 if (!p_ptr)
1070 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001071 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 goto exit;
1073 if (!peer->ref)
1074 goto exit;
1075
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001076 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 msg_set_destnode(msg, peer->node);
1078 msg_set_destport(msg, peer->ref);
1079 msg_set_orignode(msg, tipc_own_addr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001080 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001082 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001083 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084
1085 p_ptr->probing_interval = PROBING_INTERVAL;
1086 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001087 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1089
Allan Stephens0e659672010-12-31 18:59:32 +00001090 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001091 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001093 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094exit:
Per Liden4323add2006-01-18 00:38:21 +01001095 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001096 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097 return res;
1098}
1099
Allan Stephens0c3141e2008-04-15 00:22:02 -07001100/**
1101 * tipc_disconnect_port - disconnect port from peer
1102 *
1103 * Port must be locked.
1104 */
1105
1106int tipc_disconnect_port(struct tipc_port *tp_ptr)
1107{
1108 int res;
1109
1110 if (tp_ptr->connected) {
1111 tp_ptr->connected = 0;
1112 /* let timer expire on it's own to avoid deadlock! */
1113 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001114 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001115 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001116 } else {
1117 res = -ENOTCONN;
1118 }
1119 return res;
1120}
1121
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122/*
1123 * tipc_disconnect(): Disconnect port form peer.
1124 * This is a node local operation.
1125 */
1126
1127int tipc_disconnect(u32 ref)
1128{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001129 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001130 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131
Per Liden4323add2006-01-18 00:38:21 +01001132 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 if (!p_ptr)
1134 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001135 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001136 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 return res;
1138}
1139
1140/*
1141 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1142 */
1143int tipc_shutdown(u32 ref)
1144{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001145 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001146 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147
Per Liden4323add2006-01-18 00:38:21 +01001148 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 if (!p_ptr)
1150 return -EINVAL;
1151
Allan Stephense244a912011-05-31 16:10:08 -04001152 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +01001153 tipc_port_unlock(p_ptr);
1154 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 return tipc_disconnect(ref);
1156}
1157
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158/*
Per Liden4323add2006-01-18 00:38:21 +01001159 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 * message for this node.
1161 */
1162
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001163static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001164 struct iovec const *msg_sect,
1165 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166{
1167 struct sk_buff *buf;
1168 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001169
Allan Stephens26896902011-04-21 10:42:07 -05001170 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 MAX_MSG_SIZE, !sender->user_port, &buf);
1172 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001173 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 return res;
1175}
1176
1177/**
1178 * tipc_send - send message sections on connection
1179 */
1180
Allan Stephens26896902011-04-21 10:42:07 -05001181int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1182 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001184 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 u32 destnode;
1186 int res;
1187
Per Liden4323add2006-01-18 00:38:21 +01001188 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001189 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 return -EINVAL;
1191
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001192 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001193 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 destnode = port_peernode(p_ptr);
1195 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001196 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001197 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 else
Allan Stephens26896902011-04-21 10:42:07 -05001199 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1200 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201
1202 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001203 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001204 if (res > 0)
1205 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 return res;
1207 }
1208 }
1209 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001210 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001211 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 }
1213 return -ELINKCONG;
1214}
1215
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001216/**
Allan Stephens12bae472010-11-30 12:01:02 +00001217 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 */
1219
Allan Stephens12bae472010-11-30 12:01:02 +00001220int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001221 unsigned int num_sect, struct iovec const *msg_sect,
1222 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001224 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 struct tipc_msg *msg;
1226 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001227 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 int res;
1229
Per Liden4323add2006-01-18 00:38:21 +01001230 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001231 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 return -EINVAL;
1233
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001234 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001236 msg_set_orignode(msg, tipc_own_addr);
1237 msg_set_origport(msg, ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001238 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 msg_set_nametype(msg, name->type);
1240 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001241 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001242 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 msg_set_destnode(msg, destnode);
1244 msg_set_destport(msg, destport);
1245
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001246 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001248 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001249 msg_sect, total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001250 else
1251 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001252 num_sect, total_len,
1253 destnode);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001254 if (likely(res != -ELINKCONG)) {
1255 if (res > 0)
1256 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001258 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001260 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 }
1262 return -ELINKCONG;
1263 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001264 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001265 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266}
1267
1268/**
Allan Stephens12bae472010-11-30 12:01:02 +00001269 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 */
1271
Allan Stephens12bae472010-11-30 12:01:02 +00001272int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001273 unsigned int num_sect, struct iovec const *msg_sect,
1274 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001276 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 struct tipc_msg *msg;
1278 int res;
1279
Per Liden4323add2006-01-18 00:38:21 +01001280 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001281 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282 return -EINVAL;
1283
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001284 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001286 msg_set_lookup_scope(msg, 0);
Allan Stephens12bae472010-11-30 12:01:02 +00001287 msg_set_orignode(msg, tipc_own_addr);
1288 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289 msg_set_destnode(msg, dest->node);
1290 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001291 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001292
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 if (dest->node == tipc_own_addr)
Allan Stephens26896902011-04-21 10:42:07 -05001294 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1295 total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001296 else
1297 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001298 total_len, dest->node);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001299 if (likely(res != -ELINKCONG)) {
1300 if (res > 0)
1301 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001303 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001305 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 }
1307 return -ELINKCONG;
1308}
1309
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001310/**
Allan Stephens12bae472010-11-30 12:01:02 +00001311 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 */
1313
Allan Stephens12bae472010-11-30 12:01:02 +00001314int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1315 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001317 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 struct tipc_msg *msg;
1319 int res;
1320
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001321 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1322 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 return -EINVAL;
1324
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001325 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001327 msg_set_orignode(msg, tipc_own_addr);
1328 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 msg_set_destnode(msg, dest->node);
1330 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001331 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1332 msg_set_size(msg, BASIC_H_SIZE + dsz);
1333 if (skb_cow(buf, BASIC_H_SIZE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 return -ENOMEM;
1335
Allan Stephens741d9eb2011-05-31 15:03:18 -04001336 skb_push(buf, BASIC_H_SIZE);
1337 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001338
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001340 res = tipc_port_recv_msg(buf);
1341 else
1342 res = tipc_send_buf_fast(buf, dest->node);
1343 if (likely(res != -ELINKCONG)) {
1344 if (res > 0)
1345 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001347 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 if (port_unreliable(p_ptr))
1349 return dsz;
1350 return -ELINKCONG;
1351}
1352