blob: 9f2ff12cf4366bc02e1d6d8572599248bf682e8a [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,
Per Lidenb97bf3f2006-01-02 19:04:38 +010077 u32 num_sect, struct iovec const *msg_sect)
78{
79 struct tipc_msg *hdr;
80 struct sk_buff *buf;
81 struct sk_buff *ibuf = NULL;
82 struct port_list dports = {0, NULL, };
Allan Stephens23dd4cc2011-01-07 11:43:40 -050083 struct tipc_port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010084 int ext_targets;
85 int res;
86
87 if (unlikely(!oport))
88 return -EINVAL;
89
90 /* Create multicast message */
91
Allan Stephens23dd4cc2011-01-07 11:43:40 -050092 hdr = &oport->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +010093 msg_set_type(hdr, TIPC_MCAST_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -040094 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
Allan Stephens7462b9e2011-04-18 10:08:22 -040095 msg_set_destport(hdr, 0);
96 msg_set_destnode(hdr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 msg_set_nametype(hdr, seq->type);
98 msg_set_namelower(hdr, seq->lower);
99 msg_set_nameupper(hdr, seq->upper);
100 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000101 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102 !oport->user_port, &buf);
103 if (unlikely(!buf))
104 return res;
105
106 /* Figure out where to send multicast message */
107
Per Liden4323add2006-01-18 00:38:21 +0100108 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
109 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900110
111 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112
113 if (ext_targets) {
114 if (dports.count != 0) {
115 ibuf = skb_copy(buf, GFP_ATOMIC);
116 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100117 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 buf_discard(buf);
119 return -ENOMEM;
120 }
121 }
Per Liden4323add2006-01-18 00:38:21 +0100122 res = tipc_bclink_send_msg(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000123 if ((res < 0) && (dports.count != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 buf_discard(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 } else {
126 ibuf = buf;
127 }
128
129 if (res >= 0) {
130 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100131 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 } else {
Per Liden4323add2006-01-18 00:38:21 +0100133 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 }
135 return res;
136}
137
138/**
Per Liden4323add2006-01-18 00:38:21 +0100139 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900140 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 * If there is no port list, perform a lookup to create one
142 */
143
Per Liden4323add2006-01-18 00:38:21 +0100144void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145{
Allan Stephens0e659672010-12-31 18:59:32 +0000146 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 struct port_list dports = {0, NULL, };
148 struct port_list *item = dp;
149 int cnt = 0;
150
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 msg = buf_msg(buf);
152
153 /* Create destination port list, if one wasn't supplied */
154
155 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100156 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 msg_namelower(msg),
158 msg_nameupper(msg),
159 TIPC_CLUSTER_SCOPE,
160 &dports);
161 item = dp = &dports;
162 }
163
164 /* Deliver a copy of message to each destination port */
165
166 if (dp->count != 0) {
Allan Stephens7f47f5c2011-04-18 10:14:26 -0400167 msg_set_destnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 if (dp->count == 1) {
169 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100170 tipc_port_recv_msg(buf);
171 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 return;
173 }
174 for (; cnt < dp->count; cnt++) {
175 int index = cnt % PLSIZE;
176 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
177
178 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700179 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 goto exit;
181 }
Allan Stephensa0168922010-12-31 18:59:35 +0000182 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000184 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100185 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 }
187 }
188exit:
189 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100190 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191}
192
193/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700194 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900195 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700196 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 */
198
Allan Stephens0ea52242008-07-14 22:42:19 -0700199struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
201 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700202 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500204 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205 struct tipc_msg *msg;
206 u32 ref;
207
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700208 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700209 if (!p_ptr) {
210 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700211 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500213 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700215 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700217 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 }
219
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500220 p_ptr->usr_handle = usr_handle;
221 p_ptr->max_pkt = MAX_PKT_DEFAULT;
222 p_ptr->ref = ref;
223 msg = &p_ptr->phdr;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000224 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 INIT_LIST_HEAD(&p_ptr->wait_list);
227 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 p_ptr->dispatcher = dispatcher;
229 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800230 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100232 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233 INIT_LIST_HEAD(&p_ptr->publications);
234 INIT_LIST_HEAD(&p_ptr->port_list);
235 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100236 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500237 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238}
239
240int tipc_deleteport(u32 ref)
241{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500242 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800243 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800245 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100246 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900247 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 return -EINVAL;
249
Per Liden4323add2006-01-18 00:38:21 +0100250 tipc_ref_discard(ref);
251 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252
253 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500254 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100256 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 }
Allan Stephense83504f2010-12-31 18:59:30 +0000258 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259
Per Liden4323add2006-01-18 00:38:21 +0100260 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 list_del(&p_ptr->port_list);
262 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100263 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 k_term_timer(&p_ptr->timer);
265 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100266 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700267 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268}
269
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500270static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500272 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273}
274
275int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
276{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500277 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900278
Per Liden4323add2006-01-18 00:38:21 +0100279 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 if (!p_ptr)
281 return -EINVAL;
282 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800283 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700284 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285}
286
287int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
288{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500289 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900290
Per Liden4323add2006-01-18 00:38:21 +0100291 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 if (!p_ptr)
293 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500294 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100295 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700296 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297}
298
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500299static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500301 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302}
303
304int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
305{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500306 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900307
Per Liden4323add2006-01-18 00:38:21 +0100308 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 if (!p_ptr)
310 return -EINVAL;
311 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800312 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700313 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314}
315
316int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
317{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500318 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900319
Per Liden4323add2006-01-18 00:38:21 +0100320 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 if (!p_ptr)
322 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500323 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100324 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700325 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326}
327
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900328/*
329 * port_build_proto_msg(): build a port level protocol
330 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 * tipc_port lock on.
332 */
333static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
334 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335 u32 usr, u32 type, u32 err,
Allan Stephens741de3e2011-01-25 13:33:31 -0500336 u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337{
338 struct sk_buff *buf;
339 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900340
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000341 buf = tipc_buf_acquire(LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 if (buf) {
343 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000344 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700345 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 msg_set_destport(msg, destport);
347 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 msg_set_orignode(msg, orignode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 }
351 return buf;
352}
353
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354int tipc_reject_msg(struct sk_buff *buf, u32 err)
355{
356 struct tipc_msg *msg = buf_msg(buf);
357 struct sk_buff *rbuf;
358 struct tipc_msg *rmsg;
359 int hdr_sz;
360 u32 imp = msg_importance(msg);
361 u32 data_sz = msg_data_sz(msg);
362
363 if (data_sz > MAX_REJECT_SIZE)
364 data_sz = MAX_REJECT_SIZE;
365 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
366 imp++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367
368 /* discard rejected message if it shouldn't be returned to sender */
369 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
370 buf_discard(buf);
371 return data_sz;
372 }
373
374 /* construct rejected message */
375 if (msg_mcast(msg))
376 hdr_sz = MCAST_H_SIZE;
377 else
378 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000379 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 if (rbuf == NULL) {
381 buf_discard(buf);
382 return data_sz;
383 }
384 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000385 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700386 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700389 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700391 /* leave name type & instance as zeroes */
392 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700394 msg_set_nametype(rmsg, msg_nametype(msg));
395 msg_set_nameinst(rmsg, msg_nameinst(msg));
396 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900397 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300398 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399
400 /* send self-abort message when rejecting on a connected port */
401 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800402 struct sk_buff *abuf = NULL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500403 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404
405 if (p_ptr) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500406 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100408 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 }
Per Liden4323add2006-01-18 00:38:21 +0100410 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 }
412
413 /* send rejected message */
414 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100415 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 return data_sz;
417}
418
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500419int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100420 struct iovec const *msg_sect, u32 num_sect,
421 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422{
423 struct sk_buff *buf;
424 int res;
425
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000426 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 !p_ptr->user_port, &buf);
428 if (!buf)
429 return res;
430
431 return tipc_reject_msg(buf, err);
432}
433
434static void port_timeout(unsigned long ref)
435{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500436 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800437 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438
Allan Stephens065fd172006-10-16 21:38:05 -0700439 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 return;
441
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500442 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700443 tipc_port_unlock(p_ptr);
444 return;
445 }
446
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 /* Last probe answered ? */
448 if (p_ptr->probing_state == PROBING) {
449 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
450 } else {
451 buf = port_build_proto_msg(port_peerport(p_ptr),
452 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500453 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 tipc_own_addr,
455 CONN_MANAGER,
456 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900457 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 p_ptr->probing_state = PROBING;
460 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
461 }
Per Liden4323add2006-01-18 00:38:21 +0100462 tipc_port_unlock(p_ptr);
463 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464}
465
466
467static void port_handle_node_down(unsigned long ref)
468{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500469 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000470 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471
472 if (!p_ptr)
473 return;
474 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100475 tipc_port_unlock(p_ptr);
476 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477}
478
479
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500480static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500482 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500484 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800485 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 if (imp < TIPC_CRITICAL_IMPORTANCE)
487 imp++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500488 return port_build_proto_msg(p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 tipc_own_addr,
490 port_peerport(p_ptr),
491 port_peernode(p_ptr),
492 imp,
493 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900494 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 0);
496}
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 Stephens23dd4cc2011-01-07 11:43:40 -0500501 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500503 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800504 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 if (imp < TIPC_CRITICAL_IMPORTANCE)
506 imp++;
507 return port_build_proto_msg(port_peerport(p_ptr),
508 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500509 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 tipc_own_addr,
511 imp,
512 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900513 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 0);
515}
516
Per Liden4323add2006-01-18 00:38:21 +0100517void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518{
519 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500520 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800522 struct sk_buff *r_buf = NULL;
523 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 if (!p_ptr) {
526 err = TIPC_ERR_NO_PORT;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500527 } else if (p_ptr->connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000528 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
529 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000531 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900532 int wakeup = tipc_port_congested(p_ptr) &&
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500533 p_ptr->congested &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 p_ptr->wakeup;
535 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100536 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500538 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 if (!wakeup)
540 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500541 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 goto exit;
543 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500544 } else if (p_ptr->published) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 err = TIPC_ERR_NO_PORT;
546 }
547 if (err) {
548 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 msg_orignode(msg),
550 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800552 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 TIPC_CONN_MSG,
554 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 0);
556 goto exit;
557 }
558
559 /* All is fine */
560 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561 r_buf = port_build_proto_msg(msg_origport(msg),
562 msg_orignode(msg),
563 msg_destport(msg),
564 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 CONN_MANAGER,
566 CONN_PROBE_REPLY,
567 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 0);
569 }
570 p_ptr->probing_state = CONFIRMED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571exit:
572 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100573 tipc_port_unlock(p_ptr);
574 tipc_net_route_msg(r_buf);
575 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 buf_discard(buf);
577}
578
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500579static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582
583 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500586 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500588 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500590 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900591 u32 dport = port_peerport(p_ptr);
592 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900594 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
595 tipc_zone(destnode), tipc_cluster(destnode),
596 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500597 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900598 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500599 p_ptr->conn_type,
600 p_ptr->conn_instance);
601 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602 tipc_printf(buf, " bound to");
603 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 if (publ->lower == publ->upper)
605 tipc_printf(buf, " {%u,%u}", publ->type,
606 publ->lower);
607 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 }
611 }
612 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613}
614
615#define MAX_PORT_QUERY 32768
616
Per Liden4323add2006-01-18 00:38:21 +0100617struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618{
619 struct sk_buff *buf;
620 struct tlv_desc *rep_tlv;
621 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500622 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 int str_len;
624
Per Liden4323add2006-01-18 00:38:21 +0100625 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 if (!buf)
627 return NULL;
628 rep_tlv = (struct tlv_desc *)buf->data;
629
Per Liden4323add2006-01-18 00:38:21 +0100630 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
631 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500633 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500635 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 }
Per Liden4323add2006-01-18 00:38:21 +0100637 spin_unlock_bh(&tipc_port_list_lock);
638 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639
640 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;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 if (msg_orignode(msg) == tipc_own_addr)
655 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700656 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 msg_set_orignode(msg, tipc_own_addr);
658 }
Per Liden4323add2006-01-18 00:38:21 +0100659 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660}
661
662
663/*
664 * port_dispatcher_sigh(): Signal handler for messages destinated
665 * to the tipc_port interface.
666 */
667
668static void port_dispatcher_sigh(void *dummy)
669{
670 struct sk_buff *buf;
671
672 spin_lock_bh(&queue_lock);
673 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800674 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 spin_unlock_bh(&queue_lock);
676
677 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500678 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 struct user_port *up_ptr;
680 struct tipc_portid orig;
681 struct tipc_name_seq dseq;
682 void *usr_handle;
683 int connected;
684 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700685 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686
687 struct sk_buff *next = buf->next;
688 struct tipc_msg *msg = buf_msg(buf);
689 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900690
Allan Stephens96882432006-06-25 23:38:58 -0700691 message_type = msg_type(msg);
692 if (message_type > TIPC_DIRECT_MSG)
693 goto reject; /* Unsupported message type */
694
Per Liden4323add2006-01-18 00:38:21 +0100695 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700696 if (!p_ptr)
697 goto reject; /* Port deleted while msg in queue */
698
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 orig.ref = msg_origport(msg);
700 orig.node = msg_orignode(msg);
701 up_ptr = p_ptr->user_port;
702 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500703 connected = p_ptr->connected;
704 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705
706 if (unlikely(msg_errcode(msg)))
707 goto err;
708
Allan Stephens96882432006-06-25 23:38:58 -0700709 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900710
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 case TIPC_CONN_MSG:{
712 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
713 u32 peer_port = port_peerport(p_ptr);
714 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500715 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716
Julia Lawall4cec72c2008-01-08 23:48:20 -0800717 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700718 if (unlikely(!cb))
719 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700721 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700723 } else if ((msg_origport(msg) != peer_port) ||
724 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500726 dsz = msg_data_sz(msg);
727 if (unlikely(dsz &&
728 (++p_ptr->conn_unacked >=
729 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500731 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500733 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 break;
735 }
736 case TIPC_DIRECT_MSG:{
737 tipc_msg_event cb = up_ptr->msg_cb;
738
Julia Lawall4cec72c2008-01-08 23:48:20 -0800739 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700740 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 goto reject;
742 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900743 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 msg_data_sz(msg), msg_importance(msg),
745 &orig);
746 break;
747 }
Allan Stephens96882432006-06-25 23:38:58 -0700748 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 case TIPC_NAMED_MSG:{
750 tipc_named_msg_event cb = up_ptr->named_msg_cb;
751
Julia Lawall4cec72c2008-01-08 23:48:20 -0800752 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700753 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 goto reject;
755 dseq.type = msg_nametype(msg);
756 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700757 dseq.upper = (message_type == TIPC_NAMED_MSG)
758 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900760 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 msg_data_sz(msg), msg_importance(msg),
762 &orig, &dseq);
763 break;
764 }
765 }
766 if (buf)
767 buf_discard(buf);
768 buf = next;
769 continue;
770err:
Allan Stephens96882432006-06-25 23:38:58 -0700771 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900774 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 up_ptr->conn_err_cb;
776 u32 peer_port = port_peerport(p_ptr);
777 u32 peer_node = port_peernode(p_ptr);
778
Julia Lawall4cec72c2008-01-08 23:48:20 -0800779 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700780 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700782 if ((msg_origport(msg) != peer_port) ||
783 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 break;
785 tipc_disconnect(dref);
786 skb_pull(buf, msg_hdr_sz(msg));
787 cb(usr_handle, dref, &buf, msg_data(msg),
788 msg_data_sz(msg), msg_errcode(msg));
789 break;
790 }
791 case TIPC_DIRECT_MSG:{
792 tipc_msg_err_event cb = up_ptr->err_cb;
793
Julia Lawall4cec72c2008-01-08 23:48:20 -0800794 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700795 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 break;
797 skb_pull(buf, msg_hdr_sz(msg));
798 cb(usr_handle, dref, &buf, msg_data(msg),
799 msg_data_sz(msg), msg_errcode(msg), &orig);
800 break;
801 }
Allan Stephens96882432006-06-25 23:38:58 -0700802 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900804 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 up_ptr->named_err_cb;
806
Julia Lawall4cec72c2008-01-08 23:48:20 -0800807 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700808 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 break;
810 dseq.type = msg_nametype(msg);
811 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700812 dseq.upper = (message_type == TIPC_NAMED_MSG)
813 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900815 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 msg_data_sz(msg), msg_errcode(msg), &dseq);
817 break;
818 }
819 }
820 if (buf)
821 buf_discard(buf);
822 buf = next;
823 continue;
824reject:
825 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
826 buf = next;
827 }
828}
829
830/*
831 * port_dispatcher(): Dispatcher for messages destinated
832 * to the tipc_port interface. Called with port locked.
833 */
834
835static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
836{
837 buf->next = NULL;
838 spin_lock_bh(&queue_lock);
839 if (msg_queue_head) {
840 msg_queue_tail->next = buf;
841 msg_queue_tail = buf;
842 } else {
843 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100844 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 }
846 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700847 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848}
849
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900850/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900852 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 */
854
855static void port_wakeup_sh(unsigned long ref)
856{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500857 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800859 tipc_continue_event cb = NULL;
860 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861
Per Liden4323add2006-01-18 00:38:21 +0100862 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 if (p_ptr) {
864 up_ptr = p_ptr->user_port;
865 if (up_ptr) {
866 cb = up_ptr->continue_event_cb;
867 uh = up_ptr->usr_handle;
868 }
Per Liden4323add2006-01-18 00:38:21 +0100869 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 }
871 if (cb)
872 cb(uh, ref);
873}
874
875
876static void port_wakeup(struct tipc_port *p_ptr)
877{
Per Liden4323add2006-01-18 00:38:21 +0100878 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879}
880
881void tipc_acknowledge(u32 ref, u32 ack)
882{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500883 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800884 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885
Per Liden4323add2006-01-18 00:38:21 +0100886 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 if (!p_ptr)
888 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500889 if (p_ptr->connected) {
890 p_ptr->conn_unacked -= ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 buf = port_build_proto_msg(port_peerport(p_ptr),
892 port_peernode(p_ptr),
893 ref,
894 tipc_own_addr,
895 CONN_MANAGER,
896 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900897 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 ack);
899 }
Per Liden4323add2006-01-18 00:38:21 +0100900 tipc_port_unlock(p_ptr);
901 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902}
903
904/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000905 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906 */
907
Allan Stephensb0c1e922010-12-31 18:59:22 +0000908int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900909 unsigned int importance,
910 tipc_msg_err_event error_cb,
911 tipc_named_msg_err_event named_error_cb,
912 tipc_conn_shutdown_event conn_error_cb,
913 tipc_msg_event msg_cb,
914 tipc_named_msg_event named_msg_cb,
915 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 tipc_continue_event continue_event_cb,/* May be zero */
917 u32 *portref)
918{
919 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500920 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700922 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700923 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700924 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 return -ENOMEM;
926 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500927 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700928 port_wakeup, importance);
929 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 kfree(up_ptr);
931 return -ENOMEM;
932 }
933
934 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500936 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 up_ptr->err_cb = error_cb;
938 up_ptr->named_err_cb = named_error_cb;
939 up_ptr->conn_err_cb = conn_error_cb;
940 up_ptr->msg_cb = msg_cb;
941 up_ptr->named_msg_cb = named_msg_cb;
942 up_ptr->conn_msg_cb = conn_msg_cb;
943 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500944 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100945 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700946 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947}
948
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949int tipc_portimportance(u32 ref, unsigned int *importance)
950{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500951 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900952
Per Liden4323add2006-01-18 00:38:21 +0100953 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 if (!p_ptr)
955 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500956 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800957 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700958 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959}
960
961int tipc_set_portimportance(u32 ref, unsigned int imp)
962{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500963 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964
965 if (imp > TIPC_CRITICAL_IMPORTANCE)
966 return -EINVAL;
967
Per Liden4323add2006-01-18 00:38:21 +0100968 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 if (!p_ptr)
970 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500971 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800972 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700973 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974}
975
976
977int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
978{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500979 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 struct publication *publ;
981 u32 key;
982 int res = -EINVAL;
983
Per Liden4323add2006-01-18 00:38:21 +0100984 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800985 if (!p_ptr)
986 return -EINVAL;
987
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500988 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 goto exit;
990 if (seq->lower > seq->upper)
991 goto exit;
992 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
993 goto exit;
994 key = ref + p_ptr->pub_count + 1;
995 if (key == ref) {
996 res = -EADDRINUSE;
997 goto exit;
998 }
Per Liden4323add2006-01-18 00:38:21 +0100999 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001000 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 if (publ) {
1002 list_add(&publ->pport_list, &p_ptr->publications);
1003 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001004 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001005 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 }
1007exit:
Per Liden4323add2006-01-18 00:38:21 +01001008 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 return res;
1010}
1011
1012int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1013{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001014 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 struct publication *publ;
1016 struct publication *tpubl;
1017 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001018
Per Liden4323add2006-01-18 00:38:21 +01001019 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 if (!p_ptr)
1021 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001025 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001026 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001028 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001030 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 &p_ptr->publications, pport_list) {
1032 if (publ->scope != scope)
1033 continue;
1034 if (publ->type != seq->type)
1035 continue;
1036 if (publ->lower != seq->lower)
1037 continue;
1038 if (publ->upper != seq->upper)
1039 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001040 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001041 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001042 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 break;
1044 }
1045 }
1046 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001047 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001048 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 return res;
1050}
1051
1052int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1053{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001054 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 struct tipc_msg *msg;
1056 int res = -EINVAL;
1057
Per Liden4323add2006-01-18 00:38:21 +01001058 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 if (!p_ptr)
1060 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001061 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 goto exit;
1063 if (!peer->ref)
1064 goto exit;
1065
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001066 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 msg_set_destnode(msg, peer->node);
1068 msg_set_destport(msg, peer->ref);
1069 msg_set_orignode(msg, tipc_own_addr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001070 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001072 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001073 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074
1075 p_ptr->probing_interval = PROBING_INTERVAL;
1076 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001077 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1079
Allan Stephens0e659672010-12-31 18:59:32 +00001080 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001081 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001083 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084exit:
Per Liden4323add2006-01-18 00:38:21 +01001085 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001086 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087 return res;
1088}
1089
Allan Stephens0c3141e2008-04-15 00:22:02 -07001090/**
1091 * tipc_disconnect_port - disconnect port from peer
1092 *
1093 * Port must be locked.
1094 */
1095
1096int tipc_disconnect_port(struct tipc_port *tp_ptr)
1097{
1098 int res;
1099
1100 if (tp_ptr->connected) {
1101 tp_ptr->connected = 0;
1102 /* let timer expire on it's own to avoid deadlock! */
1103 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001104 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001105 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001106 } else {
1107 res = -ENOTCONN;
1108 }
1109 return res;
1110}
1111
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112/*
1113 * tipc_disconnect(): Disconnect port form peer.
1114 * This is a node local operation.
1115 */
1116
1117int tipc_disconnect(u32 ref)
1118{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001119 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001120 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121
Per Liden4323add2006-01-18 00:38:21 +01001122 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 if (!p_ptr)
1124 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001125 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001126 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 return res;
1128}
1129
1130/*
1131 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1132 */
1133int tipc_shutdown(u32 ref)
1134{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001135 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001136 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137
Per Liden4323add2006-01-18 00:38:21 +01001138 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 if (!p_ptr)
1140 return -EINVAL;
1141
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001142 if (p_ptr->connected) {
1143 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 if (imp < TIPC_CRITICAL_IMPORTANCE)
1145 imp++;
1146 buf = port_build_proto_msg(port_peerport(p_ptr),
1147 port_peernode(p_ptr),
1148 ref,
1149 tipc_own_addr,
1150 imp,
1151 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001152 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 0);
1154 }
Per Liden4323add2006-01-18 00:38:21 +01001155 tipc_port_unlock(p_ptr);
1156 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 return tipc_disconnect(ref);
1158}
1159
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160/*
Per Liden4323add2006-01-18 00:38:21 +01001161 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 * message for this node.
1163 */
1164
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001165static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001166 struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167{
1168 struct sk_buff *buf;
1169 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001170
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001171 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 MAX_MSG_SIZE, !sender->user_port, &buf);
1173 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001174 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 return res;
1176}
1177
1178/**
1179 * tipc_send - send message sections on connection
1180 */
1181
1182int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1183{
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,
1197 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 else
Per Liden4323add2006-01-18 00:38:21 +01001199 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200
1201 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001202 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001203 if (res > 0)
1204 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 return res;
1206 }
1207 }
1208 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001209 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001211 return tipc_msg_calc_data_size(msg_sect, num_sect);
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,
1221 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001223 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224 struct tipc_msg *msg;
1225 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001226 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 int res;
1228
Per Liden4323add2006-01-18 00:38:21 +01001229 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001230 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 return -EINVAL;
1232
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001233 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001235 msg_set_orignode(msg, tipc_own_addr);
1236 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 msg_set_hdr_sz(msg, LONG_H_SIZE);
1238 msg_set_nametype(msg, name->type);
1239 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001240 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001241 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 msg_set_destnode(msg, destnode);
1243 msg_set_destport(msg, destport);
1244
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001245 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001247 res = tipc_port_recv_sections(p_ptr, num_sect,
1248 msg_sect);
1249 else
1250 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1251 num_sect, destnode);
1252 if (likely(res != -ELINKCONG)) {
1253 if (res > 0)
1254 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001256 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 if (port_unreliable(p_ptr)) {
1258 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001259 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 }
1261 return -ELINKCONG;
1262 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001263 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001264 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265}
1266
1267/**
Allan Stephens12bae472010-11-30 12:01:02 +00001268 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269 */
1270
Allan Stephens12bae472010-11-30 12:01:02 +00001271int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1272 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001274 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275 struct tipc_msg *msg;
1276 int res;
1277
Per Liden4323add2006-01-18 00:38:21 +01001278 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001279 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 return -EINVAL;
1281
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001282 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001284 msg_set_lookup_scope(msg, 0);
Allan Stephens12bae472010-11-30 12:01:02 +00001285 msg_set_orignode(msg, tipc_own_addr);
1286 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 msg_set_destnode(msg, dest->node);
1288 msg_set_destport(msg, dest->ref);
1289 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001290
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001292 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1293 else
1294 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1295 dest->node);
1296 if (likely(res != -ELINKCONG)) {
1297 if (res > 0)
1298 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001299 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001300 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001301 if (port_unreliable(p_ptr)) {
1302 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001303 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 }
1305 return -ELINKCONG;
1306}
1307
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001308/**
Allan Stephens12bae472010-11-30 12:01:02 +00001309 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 */
1311
Allan Stephens12bae472010-11-30 12:01:02 +00001312int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1313 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001315 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 struct tipc_msg *msg;
1317 int res;
1318
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001319 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1320 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 return -EINVAL;
1322
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001323 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001325 msg_set_orignode(msg, tipc_own_addr);
1326 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327 msg_set_destnode(msg, dest->node);
1328 msg_set_destport(msg, dest->ref);
1329 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1331 if (skb_cow(buf, DIR_MSG_H_SIZE))
1332 return -ENOMEM;
1333
1334 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001335 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001336
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001338 res = tipc_port_recv_msg(buf);
1339 else
1340 res = tipc_send_buf_fast(buf, dest->node);
1341 if (likely(res != -ELINKCONG)) {
1342 if (res > 0)
1343 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001345 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 if (port_unreliable(p_ptr))
1347 return dsz;
1348 return -ELINKCONG;
1349}
1350