blob: 756e64cbff96d3718aac12326d74de2d62ef561a [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 Stephensc68ca7b2010-05-11 14:30:12 +0000225 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_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
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000342 buf = tipc_buf_acquire(LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 if (buf) {
344 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000345 tipc_msg_init(msg, usr, type, LONG_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;
361 u32 imp = msg_importance(msg);
362 u32 data_sz = msg_data_sz(msg);
363
364 if (data_sz > MAX_REJECT_SIZE)
365 data_sz = MAX_REJECT_SIZE;
366 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
367 imp++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368
369 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400370
371 if (WARN(!msg_isdata(msg),
372 "attempt to reject message with user=%u", msg_user(msg))) {
373 dump_stack();
374 goto exit;
375 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400376 if (msg_errcode(msg) || msg_dest_droppable(msg))
377 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378
379 /* construct rejected message */
380 if (msg_mcast(msg))
381 hdr_sz = MCAST_H_SIZE;
382 else
383 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000384 rbuf = tipc_buf_acquire(data_sz + hdr_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 Stephensc68ca7b2010-05-11 14:30:12 +0000389 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700390 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700393 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700395 /* leave name type & instance as zeroes */
396 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700398 msg_set_nametype(rmsg, msg_nametype(msg));
399 msg_set_nameinst(rmsg, msg_nameinst(msg));
400 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900401 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300402 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
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
Per Liden4323add2006-01-18 00:38:21 +0100419 tipc_net_route_msg(rbuf);
Allan Stephensacc631b2011-05-23 13:47:44 -0400420exit:
421 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 return data_sz;
423}
424
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500425int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100426 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500427 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428{
429 struct sk_buff *buf;
430 int res;
431
Allan Stephens26896902011-04-21 10:42:07 -0500432 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 !p_ptr->user_port, &buf);
434 if (!buf)
435 return res;
436
437 return tipc_reject_msg(buf, err);
438}
439
440static void port_timeout(unsigned long ref)
441{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500442 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800443 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444
Allan Stephens065fd172006-10-16 21:38:05 -0700445 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 return;
447
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500448 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700449 tipc_port_unlock(p_ptr);
450 return;
451 }
452
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 /* Last probe answered ? */
454 if (p_ptr->probing_state == PROBING) {
455 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
456 } else {
457 buf = port_build_proto_msg(port_peerport(p_ptr),
458 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500459 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 tipc_own_addr,
461 CONN_MANAGER,
462 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 p_ptr->probing_state = PROBING;
466 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
467 }
Per Liden4323add2006-01-18 00:38:21 +0100468 tipc_port_unlock(p_ptr);
469 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470}
471
472
473static void port_handle_node_down(unsigned long ref)
474{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500475 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000476 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477
478 if (!p_ptr)
479 return;
480 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100481 tipc_port_unlock(p_ptr);
482 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483}
484
485
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500486static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500488 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500490 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800491 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 if (imp < TIPC_CRITICAL_IMPORTANCE)
493 imp++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500494 return port_build_proto_msg(p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 tipc_own_addr,
496 port_peerport(p_ptr),
497 port_peernode(p_ptr),
498 imp,
499 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900500 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 0);
502}
503
504
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500505static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500507 u32 imp = msg_importance(&p_ptr->phdr);
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;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 if (imp < TIPC_CRITICAL_IMPORTANCE)
512 imp++;
513 return port_build_proto_msg(port_peerport(p_ptr),
514 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500515 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 tipc_own_addr,
517 imp,
518 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900519 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 0);
521}
522
Per Liden4323add2006-01-18 00:38:21 +0100523void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524{
525 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500526 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800528 struct sk_buff *r_buf = NULL;
529 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 if (!p_ptr) {
532 err = TIPC_ERR_NO_PORT;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500533 } else if (p_ptr->connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000534 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
535 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000537 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900538 int wakeup = tipc_port_congested(p_ptr) &&
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500539 p_ptr->congested &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 p_ptr->wakeup;
541 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100542 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500544 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 if (!wakeup)
546 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500547 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 goto exit;
549 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500550 } else if (p_ptr->published) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 err = TIPC_ERR_NO_PORT;
552 }
553 if (err) {
554 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900555 msg_orignode(msg),
556 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800558 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 TIPC_CONN_MSG,
560 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 0);
562 goto exit;
563 }
564
565 /* All is fine */
566 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900567 r_buf = port_build_proto_msg(msg_origport(msg),
568 msg_orignode(msg),
569 msg_destport(msg),
570 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 CONN_MANAGER,
572 CONN_PROBE_REPLY,
573 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 0);
575 }
576 p_ptr->probing_state = CONFIRMED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577exit:
578 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100579 tipc_port_unlock(p_ptr);
580 tipc_net_route_msg(r_buf);
581 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 buf_discard(buf);
583}
584
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500585static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588
589 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500592 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500594 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500596 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 u32 dport = port_peerport(p_ptr);
598 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900600 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
601 tipc_zone(destnode), tipc_cluster(destnode),
602 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500603 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500605 p_ptr->conn_type,
606 p_ptr->conn_instance);
607 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 tipc_printf(buf, " bound to");
609 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 if (publ->lower == publ->upper)
611 tipc_printf(buf, " {%u,%u}", publ->type,
612 publ->lower);
613 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900614 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900616 }
617 }
618 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619}
620
621#define MAX_PORT_QUERY 32768
622
Per Liden4323add2006-01-18 00:38:21 +0100623struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624{
625 struct sk_buff *buf;
626 struct tlv_desc *rep_tlv;
627 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500628 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 int str_len;
630
Per Liden4323add2006-01-18 00:38:21 +0100631 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 if (!buf)
633 return NULL;
634 rep_tlv = (struct tlv_desc *)buf->data;
635
Per Liden4323add2006-01-18 00:38:21 +0100636 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
637 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500639 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500641 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 }
Per Liden4323add2006-01-18 00:38:21 +0100643 spin_unlock_bh(&tipc_port_list_lock);
644 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645
646 skb_put(buf, TLV_SPACE(str_len));
647 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
648
649 return buf;
650}
651
Per Liden4323add2006-01-18 00:38:21 +0100652void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500654 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 struct tipc_msg *msg;
656
Per Liden4323add2006-01-18 00:38:21 +0100657 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500659 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 if (msg_orignode(msg) == tipc_own_addr)
661 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700662 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 msg_set_orignode(msg, tipc_own_addr);
664 }
Per Liden4323add2006-01-18 00:38:21 +0100665 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666}
667
668
669/*
670 * port_dispatcher_sigh(): Signal handler for messages destinated
671 * to the tipc_port interface.
672 */
673
674static void port_dispatcher_sigh(void *dummy)
675{
676 struct sk_buff *buf;
677
678 spin_lock_bh(&queue_lock);
679 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800680 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 spin_unlock_bh(&queue_lock);
682
683 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500684 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 struct user_port *up_ptr;
686 struct tipc_portid orig;
687 struct tipc_name_seq dseq;
688 void *usr_handle;
689 int connected;
690 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700691 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692
693 struct sk_buff *next = buf->next;
694 struct tipc_msg *msg = buf_msg(buf);
695 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696
Allan Stephens96882432006-06-25 23:38:58 -0700697 message_type = msg_type(msg);
698 if (message_type > TIPC_DIRECT_MSG)
699 goto reject; /* Unsupported message type */
700
Per Liden4323add2006-01-18 00:38:21 +0100701 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700702 if (!p_ptr)
703 goto reject; /* Port deleted while msg in queue */
704
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705 orig.ref = msg_origport(msg);
706 orig.node = msg_orignode(msg);
707 up_ptr = p_ptr->user_port;
708 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500709 connected = p_ptr->connected;
710 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711
712 if (unlikely(msg_errcode(msg)))
713 goto err;
714
Allan Stephens96882432006-06-25 23:38:58 -0700715 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900716
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717 case TIPC_CONN_MSG:{
718 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
719 u32 peer_port = port_peerport(p_ptr);
720 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500721 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722
Julia Lawall4cec72c2008-01-08 23:48:20 -0800723 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700724 if (unlikely(!cb))
725 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700727 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700729 } else if ((msg_origport(msg) != peer_port) ||
730 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500732 dsz = msg_data_sz(msg);
733 if (unlikely(dsz &&
734 (++p_ptr->conn_unacked >=
735 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900736 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500737 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500739 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 break;
741 }
742 case TIPC_DIRECT_MSG:{
743 tipc_msg_event cb = up_ptr->msg_cb;
744
Julia Lawall4cec72c2008-01-08 23:48:20 -0800745 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700746 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 goto reject;
748 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900749 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 msg_data_sz(msg), msg_importance(msg),
751 &orig);
752 break;
753 }
Allan Stephens96882432006-06-25 23:38:58 -0700754 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 case TIPC_NAMED_MSG:{
756 tipc_named_msg_event cb = up_ptr->named_msg_cb;
757
Julia Lawall4cec72c2008-01-08 23:48:20 -0800758 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700759 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 goto reject;
761 dseq.type = msg_nametype(msg);
762 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700763 dseq.upper = (message_type == TIPC_NAMED_MSG)
764 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900766 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 msg_data_sz(msg), msg_importance(msg),
768 &orig, &dseq);
769 break;
770 }
771 }
772 if (buf)
773 buf_discard(buf);
774 buf = next;
775 continue;
776err:
Allan Stephens96882432006-06-25 23:38:58 -0700777 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900778
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900780 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 up_ptr->conn_err_cb;
782 u32 peer_port = port_peerport(p_ptr);
783 u32 peer_node = port_peernode(p_ptr);
784
Julia Lawall4cec72c2008-01-08 23:48:20 -0800785 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700786 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700788 if ((msg_origport(msg) != peer_port) ||
789 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 break;
791 tipc_disconnect(dref);
792 skb_pull(buf, msg_hdr_sz(msg));
793 cb(usr_handle, dref, &buf, msg_data(msg),
794 msg_data_sz(msg), msg_errcode(msg));
795 break;
796 }
797 case TIPC_DIRECT_MSG:{
798 tipc_msg_err_event cb = up_ptr->err_cb;
799
Julia Lawall4cec72c2008-01-08 23:48:20 -0800800 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700801 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 break;
803 skb_pull(buf, msg_hdr_sz(msg));
804 cb(usr_handle, dref, &buf, msg_data(msg),
805 msg_data_sz(msg), msg_errcode(msg), &orig);
806 break;
807 }
Allan Stephens96882432006-06-25 23:38:58 -0700808 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900810 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100811 up_ptr->named_err_cb;
812
Julia Lawall4cec72c2008-01-08 23:48:20 -0800813 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700814 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 break;
816 dseq.type = msg_nametype(msg);
817 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700818 dseq.upper = (message_type == TIPC_NAMED_MSG)
819 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900821 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 msg_data_sz(msg), msg_errcode(msg), &dseq);
823 break;
824 }
825 }
826 if (buf)
827 buf_discard(buf);
828 buf = next;
829 continue;
830reject:
831 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
832 buf = next;
833 }
834}
835
836/*
837 * port_dispatcher(): Dispatcher for messages destinated
838 * to the tipc_port interface. Called with port locked.
839 */
840
841static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
842{
843 buf->next = NULL;
844 spin_lock_bh(&queue_lock);
845 if (msg_queue_head) {
846 msg_queue_tail->next = buf;
847 msg_queue_tail = buf;
848 } else {
849 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100850 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 }
852 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700853 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854}
855
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900856/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900858 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 */
860
861static void port_wakeup_sh(unsigned long ref)
862{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500863 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800865 tipc_continue_event cb = NULL;
866 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867
Per Liden4323add2006-01-18 00:38:21 +0100868 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 if (p_ptr) {
870 up_ptr = p_ptr->user_port;
871 if (up_ptr) {
872 cb = up_ptr->continue_event_cb;
873 uh = up_ptr->usr_handle;
874 }
Per Liden4323add2006-01-18 00:38:21 +0100875 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 }
877 if (cb)
878 cb(uh, ref);
879}
880
881
882static void port_wakeup(struct tipc_port *p_ptr)
883{
Per Liden4323add2006-01-18 00:38:21 +0100884 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885}
886
887void tipc_acknowledge(u32 ref, u32 ack)
888{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500889 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800890 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891
Per Liden4323add2006-01-18 00:38:21 +0100892 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 if (!p_ptr)
894 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500895 if (p_ptr->connected) {
896 p_ptr->conn_unacked -= ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 buf = port_build_proto_msg(port_peerport(p_ptr),
898 port_peernode(p_ptr),
899 ref,
900 tipc_own_addr,
901 CONN_MANAGER,
902 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900903 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 ack);
905 }
Per Liden4323add2006-01-18 00:38:21 +0100906 tipc_port_unlock(p_ptr);
907 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908}
909
910/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000911 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 */
913
Allan Stephensb0c1e922010-12-31 18:59:22 +0000914int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900915 unsigned int importance,
916 tipc_msg_err_event error_cb,
917 tipc_named_msg_err_event named_error_cb,
918 tipc_conn_shutdown_event conn_error_cb,
919 tipc_msg_event msg_cb,
920 tipc_named_msg_event named_msg_cb,
921 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 tipc_continue_event continue_event_cb,/* May be zero */
923 u32 *portref)
924{
925 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500926 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700928 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700929 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700930 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 return -ENOMEM;
932 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500933 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700934 port_wakeup, importance);
935 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 kfree(up_ptr);
937 return -ENOMEM;
938 }
939
940 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500942 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943 up_ptr->err_cb = error_cb;
944 up_ptr->named_err_cb = named_error_cb;
945 up_ptr->conn_err_cb = conn_error_cb;
946 up_ptr->msg_cb = msg_cb;
947 up_ptr->named_msg_cb = named_msg_cb;
948 up_ptr->conn_msg_cb = conn_msg_cb;
949 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500950 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100951 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700952 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953}
954
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955int tipc_portimportance(u32 ref, unsigned int *importance)
956{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500957 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900958
Per Liden4323add2006-01-18 00:38:21 +0100959 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 if (!p_ptr)
961 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500962 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800963 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700964 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965}
966
967int tipc_set_portimportance(u32 ref, unsigned int imp)
968{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500969 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970
971 if (imp > TIPC_CRITICAL_IMPORTANCE)
972 return -EINVAL;
973
Per Liden4323add2006-01-18 00:38:21 +0100974 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 if (!p_ptr)
976 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500977 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800978 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700979 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980}
981
982
983int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
984{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500985 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986 struct publication *publ;
987 u32 key;
988 int res = -EINVAL;
989
Per Liden4323add2006-01-18 00:38:21 +0100990 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800991 if (!p_ptr)
992 return -EINVAL;
993
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500994 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 goto exit;
996 if (seq->lower > seq->upper)
997 goto exit;
998 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
999 goto exit;
1000 key = ref + p_ptr->pub_count + 1;
1001 if (key == ref) {
1002 res = -EADDRINUSE;
1003 goto exit;
1004 }
Per Liden4323add2006-01-18 00:38:21 +01001005 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001006 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 if (publ) {
1008 list_add(&publ->pport_list, &p_ptr->publications);
1009 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001010 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001011 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 }
1013exit:
Per Liden4323add2006-01-18 00:38:21 +01001014 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 return res;
1016}
1017
1018int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1019{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001020 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 struct publication *publ;
1022 struct publication *tpubl;
1023 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024
Per Liden4323add2006-01-18 00:38:21 +01001025 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 if (!p_ptr)
1027 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001032 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001034 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001036 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 &p_ptr->publications, pport_list) {
1038 if (publ->scope != scope)
1039 continue;
1040 if (publ->type != seq->type)
1041 continue;
1042 if (publ->lower != seq->lower)
1043 continue;
1044 if (publ->upper != seq->upper)
1045 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001046 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001047 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001048 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 break;
1050 }
1051 }
1052 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001053 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001054 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 return res;
1056}
1057
1058int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1059{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001060 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 struct tipc_msg *msg;
1062 int res = -EINVAL;
1063
Per Liden4323add2006-01-18 00:38:21 +01001064 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 if (!p_ptr)
1066 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001067 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 goto exit;
1069 if (!peer->ref)
1070 goto exit;
1071
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001072 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 msg_set_destnode(msg, peer->node);
1074 msg_set_destport(msg, peer->ref);
1075 msg_set_orignode(msg, tipc_own_addr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001076 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001078 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001079 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080
1081 p_ptr->probing_interval = PROBING_INTERVAL;
1082 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001083 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1085
Allan Stephens0e659672010-12-31 18:59:32 +00001086 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001087 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001089 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090exit:
Per Liden4323add2006-01-18 00:38:21 +01001091 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001092 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 return res;
1094}
1095
Allan Stephens0c3141e2008-04-15 00:22:02 -07001096/**
1097 * tipc_disconnect_port - disconnect port from peer
1098 *
1099 * Port must be locked.
1100 */
1101
1102int tipc_disconnect_port(struct tipc_port *tp_ptr)
1103{
1104 int res;
1105
1106 if (tp_ptr->connected) {
1107 tp_ptr->connected = 0;
1108 /* let timer expire on it's own to avoid deadlock! */
1109 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001110 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001111 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001112 } else {
1113 res = -ENOTCONN;
1114 }
1115 return res;
1116}
1117
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118/*
1119 * tipc_disconnect(): Disconnect port form peer.
1120 * This is a node local operation.
1121 */
1122
1123int tipc_disconnect(u32 ref)
1124{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001125 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001126 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127
Per Liden4323add2006-01-18 00:38:21 +01001128 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 if (!p_ptr)
1130 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001131 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001132 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 return res;
1134}
1135
1136/*
1137 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1138 */
1139int tipc_shutdown(u32 ref)
1140{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001141 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001142 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143
Per Liden4323add2006-01-18 00:38:21 +01001144 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 if (!p_ptr)
1146 return -EINVAL;
1147
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001148 if (p_ptr->connected) {
1149 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 if (imp < TIPC_CRITICAL_IMPORTANCE)
1151 imp++;
1152 buf = port_build_proto_msg(port_peerport(p_ptr),
1153 port_peernode(p_ptr),
1154 ref,
1155 tipc_own_addr,
1156 imp,
1157 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001158 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 0);
1160 }
Per Liden4323add2006-01-18 00:38:21 +01001161 tipc_port_unlock(p_ptr);
1162 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 return tipc_disconnect(ref);
1164}
1165
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166/*
Per Liden4323add2006-01-18 00:38:21 +01001167 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 * message for this node.
1169 */
1170
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001171static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001172 struct iovec const *msg_sect,
1173 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174{
1175 struct sk_buff *buf;
1176 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001177
Allan Stephens26896902011-04-21 10:42:07 -05001178 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 MAX_MSG_SIZE, !sender->user_port, &buf);
1180 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001181 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 return res;
1183}
1184
1185/**
1186 * tipc_send - send message sections on connection
1187 */
1188
Allan Stephens26896902011-04-21 10:42:07 -05001189int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1190 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001192 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 u32 destnode;
1194 int res;
1195
Per Liden4323add2006-01-18 00:38:21 +01001196 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001197 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 return -EINVAL;
1199
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001200 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001201 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 destnode = port_peernode(p_ptr);
1203 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001204 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001205 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 else
Allan Stephens26896902011-04-21 10:42:07 -05001207 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1208 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209
1210 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001211 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001212 if (res > 0)
1213 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 return res;
1215 }
1216 }
1217 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001218 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001219 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 }
1221 return -ELINKCONG;
1222}
1223
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001224/**
Allan Stephens12bae472010-11-30 12:01:02 +00001225 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 */
1227
Allan Stephens12bae472010-11-30 12:01:02 +00001228int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001229 unsigned int num_sect, struct iovec const *msg_sect,
1230 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001232 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 struct tipc_msg *msg;
1234 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001235 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 int res;
1237
Per Liden4323add2006-01-18 00:38:21 +01001238 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001239 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 return -EINVAL;
1241
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001242 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001244 msg_set_orignode(msg, tipc_own_addr);
1245 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 msg_set_hdr_sz(msg, LONG_H_SIZE);
1247 msg_set_nametype(msg, name->type);
1248 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001249 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001250 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 msg_set_destnode(msg, destnode);
1252 msg_set_destport(msg, destport);
1253
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001254 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001256 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001257 msg_sect, total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001258 else
1259 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001260 num_sect, total_len,
1261 destnode);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001262 if (likely(res != -ELINKCONG)) {
1263 if (res > 0)
1264 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001266 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001268 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269 }
1270 return -ELINKCONG;
1271 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001272 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001273 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274}
1275
1276/**
Allan Stephens12bae472010-11-30 12:01:02 +00001277 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 */
1279
Allan Stephens12bae472010-11-30 12:01:02 +00001280int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001281 unsigned int num_sect, struct iovec const *msg_sect,
1282 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001284 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 struct tipc_msg *msg;
1286 int res;
1287
Per Liden4323add2006-01-18 00:38:21 +01001288 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001289 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 return -EINVAL;
1291
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001292 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001294 msg_set_lookup_scope(msg, 0);
Allan Stephens12bae472010-11-30 12:01:02 +00001295 msg_set_orignode(msg, tipc_own_addr);
1296 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 msg_set_destnode(msg, dest->node);
1298 msg_set_destport(msg, dest->ref);
1299 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001300
Per Lidenb97bf3f2006-01-02 19:04:38 +01001301 if (dest->node == tipc_own_addr)
Allan Stephens26896902011-04-21 10:42:07 -05001302 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1303 total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001304 else
1305 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001306 total_len, dest->node);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001307 if (likely(res != -ELINKCONG)) {
1308 if (res > 0)
1309 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001311 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001313 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 }
1315 return -ELINKCONG;
1316}
1317
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001318/**
Allan Stephens12bae472010-11-30 12:01:02 +00001319 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 */
1321
Allan Stephens12bae472010-11-30 12:01:02 +00001322int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1323 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001325 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 struct tipc_msg *msg;
1327 int res;
1328
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001329 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1330 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 return -EINVAL;
1332
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001333 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001335 msg_set_orignode(msg, tipc_own_addr);
1336 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 msg_set_destnode(msg, dest->node);
1338 msg_set_destport(msg, dest->ref);
1339 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1341 if (skb_cow(buf, DIR_MSG_H_SIZE))
1342 return -ENOMEM;
1343
1344 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001345 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001346
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001348 res = tipc_port_recv_msg(buf);
1349 else
1350 res = tipc_send_buf_fast(buf, dest->node);
1351 if (likely(res != -ELINKCONG)) {
1352 if (res > 0)
1353 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001355 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 if (port_unreliable(p_ptr))
1357 return dsz;
1358 return -ELINKCONG;
1359}
1360