blob: 70ecdfdf6e3a14324cb877053f6ae116175fc12f [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);
Allan Stephens017dac32011-05-24 13:20:09 -0400363 u32 src_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364
365 if (data_sz > MAX_REJECT_SIZE)
366 data_sz = MAX_REJECT_SIZE;
367 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
368 imp++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369
370 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400371
372 if (WARN(!msg_isdata(msg),
373 "attempt to reject message with user=%u", msg_user(msg))) {
374 dump_stack();
375 goto exit;
376 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400377 if (msg_errcode(msg) || msg_dest_droppable(msg))
378 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379
380 /* construct rejected message */
381 if (msg_mcast(msg))
382 hdr_sz = MCAST_H_SIZE;
383 else
384 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000385 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400386 if (rbuf == NULL)
387 goto exit;
388
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000390 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700391 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700394 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700396 /* leave name type & instance as zeroes */
397 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700399 msg_set_nametype(rmsg, msg_nametype(msg));
400 msg_set_nameinst(rmsg, msg_nameinst(msg));
401 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900402 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300403 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404
405 /* send self-abort message when rejecting on a connected port */
406 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800407 struct sk_buff *abuf = NULL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500408 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409
410 if (p_ptr) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500411 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100413 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 }
Per Liden4323add2006-01-18 00:38:21 +0100415 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 }
417
Allan Stephensacc631b2011-05-23 13:47:44 -0400418 /* send returned message & dispose of rejected message */
419
Allan Stephens017dac32011-05-24 13:20:09 -0400420 src_node = msg_prevnode(msg);
421 if (src_node == tipc_own_addr)
422 tipc_port_recv_msg(rbuf);
423 else
424 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400425exit:
426 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 return data_sz;
428}
429
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500430int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100431 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500432 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433{
434 struct sk_buff *buf;
435 int res;
436
Allan Stephens26896902011-04-21 10:42:07 -0500437 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 !p_ptr->user_port, &buf);
439 if (!buf)
440 return res;
441
442 return tipc_reject_msg(buf, err);
443}
444
445static void port_timeout(unsigned long ref)
446{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500447 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800448 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449
Allan Stephens065fd172006-10-16 21:38:05 -0700450 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 return;
452
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500453 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700454 tipc_port_unlock(p_ptr);
455 return;
456 }
457
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 /* Last probe answered ? */
459 if (p_ptr->probing_state == PROBING) {
460 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
461 } else {
462 buf = port_build_proto_msg(port_peerport(p_ptr),
463 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500464 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 tipc_own_addr,
466 CONN_MANAGER,
467 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900468 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 p_ptr->probing_state = PROBING;
471 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
472 }
Per Liden4323add2006-01-18 00:38:21 +0100473 tipc_port_unlock(p_ptr);
474 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475}
476
477
478static void port_handle_node_down(unsigned long ref)
479{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500480 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000481 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482
483 if (!p_ptr)
484 return;
485 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100486 tipc_port_unlock(p_ptr);
487 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488}
489
490
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500491static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500493 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500495 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800496 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 if (imp < TIPC_CRITICAL_IMPORTANCE)
498 imp++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500499 return port_build_proto_msg(p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 tipc_own_addr,
501 port_peerport(p_ptr),
502 port_peernode(p_ptr),
503 imp,
504 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900505 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 0);
507}
508
509
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500510static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500512 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500514 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800515 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 if (imp < TIPC_CRITICAL_IMPORTANCE)
517 imp++;
518 return port_build_proto_msg(port_peerport(p_ptr),
519 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500520 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 tipc_own_addr,
522 imp,
523 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900524 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 0);
526}
527
Per Liden4323add2006-01-18 00:38:21 +0100528void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529{
530 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500531 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800533 struct sk_buff *r_buf = NULL;
534 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 if (!p_ptr) {
537 err = TIPC_ERR_NO_PORT;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500538 } else if (p_ptr->connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000539 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
540 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000542 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900543 int wakeup = tipc_port_congested(p_ptr) &&
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500544 p_ptr->congested &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 p_ptr->wakeup;
546 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100547 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500549 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 if (!wakeup)
551 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500552 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 goto exit;
554 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500555 } else if (p_ptr->published) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 err = TIPC_ERR_NO_PORT;
557 }
558 if (err) {
559 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 msg_orignode(msg),
561 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800563 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 TIPC_CONN_MSG,
565 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 0);
567 goto exit;
568 }
569
570 /* All is fine */
571 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900572 r_buf = port_build_proto_msg(msg_origport(msg),
573 msg_orignode(msg),
574 msg_destport(msg),
575 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 CONN_MANAGER,
577 CONN_PROBE_REPLY,
578 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 0);
580 }
581 p_ptr->probing_state = CONFIRMED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582exit:
583 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100584 tipc_port_unlock(p_ptr);
585 tipc_net_route_msg(r_buf);
586 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 buf_discard(buf);
588}
589
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500590static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900592 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593
594 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500597 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500599 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500601 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602 u32 dport = port_peerport(p_ptr);
603 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900605 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
606 tipc_zone(destnode), tipc_cluster(destnode),
607 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500608 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900609 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500610 p_ptr->conn_type,
611 p_ptr->conn_instance);
612 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900613 tipc_printf(buf, " bound to");
614 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 if (publ->lower == publ->upper)
616 tipc_printf(buf, " {%u,%u}", publ->type,
617 publ->lower);
618 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900619 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900621 }
622 }
623 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624}
625
626#define MAX_PORT_QUERY 32768
627
Per Liden4323add2006-01-18 00:38:21 +0100628struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629{
630 struct sk_buff *buf;
631 struct tlv_desc *rep_tlv;
632 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500633 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 int str_len;
635
Per Liden4323add2006-01-18 00:38:21 +0100636 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 if (!buf)
638 return NULL;
639 rep_tlv = (struct tlv_desc *)buf->data;
640
Per Liden4323add2006-01-18 00:38:21 +0100641 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
642 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500644 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500646 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 }
Per Liden4323add2006-01-18 00:38:21 +0100648 spin_unlock_bh(&tipc_port_list_lock);
649 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650
651 skb_put(buf, TLV_SPACE(str_len));
652 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
653
654 return buf;
655}
656
Per Liden4323add2006-01-18 00:38:21 +0100657void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500659 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 struct tipc_msg *msg;
661
Per Liden4323add2006-01-18 00:38:21 +0100662 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500664 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 if (msg_orignode(msg) == tipc_own_addr)
666 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700667 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 msg_set_orignode(msg, tipc_own_addr);
669 }
Per Liden4323add2006-01-18 00:38:21 +0100670 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671}
672
673
674/*
675 * port_dispatcher_sigh(): Signal handler for messages destinated
676 * to the tipc_port interface.
677 */
678
679static void port_dispatcher_sigh(void *dummy)
680{
681 struct sk_buff *buf;
682
683 spin_lock_bh(&queue_lock);
684 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800685 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 spin_unlock_bh(&queue_lock);
687
688 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500689 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 struct user_port *up_ptr;
691 struct tipc_portid orig;
692 struct tipc_name_seq dseq;
693 void *usr_handle;
694 int connected;
695 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700696 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697
698 struct sk_buff *next = buf->next;
699 struct tipc_msg *msg = buf_msg(buf);
700 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900701
Allan Stephens96882432006-06-25 23:38:58 -0700702 message_type = msg_type(msg);
703 if (message_type > TIPC_DIRECT_MSG)
704 goto reject; /* Unsupported message type */
705
Per Liden4323add2006-01-18 00:38:21 +0100706 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700707 if (!p_ptr)
708 goto reject; /* Port deleted while msg in queue */
709
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 orig.ref = msg_origport(msg);
711 orig.node = msg_orignode(msg);
712 up_ptr = p_ptr->user_port;
713 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500714 connected = p_ptr->connected;
715 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716
717 if (unlikely(msg_errcode(msg)))
718 goto err;
719
Allan Stephens96882432006-06-25 23:38:58 -0700720 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900721
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 case TIPC_CONN_MSG:{
723 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
724 u32 peer_port = port_peerport(p_ptr);
725 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500726 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727
Julia Lawall4cec72c2008-01-08 23:48:20 -0800728 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700729 if (unlikely(!cb))
730 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700732 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700734 } else if ((msg_origport(msg) != peer_port) ||
735 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500737 dsz = msg_data_sz(msg);
738 if (unlikely(dsz &&
739 (++p_ptr->conn_unacked >=
740 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900741 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500742 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500744 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 break;
746 }
747 case TIPC_DIRECT_MSG:{
748 tipc_msg_event cb = up_ptr->msg_cb;
749
Julia Lawall4cec72c2008-01-08 23:48:20 -0800750 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700751 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 goto reject;
753 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900754 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 msg_data_sz(msg), msg_importance(msg),
756 &orig);
757 break;
758 }
Allan Stephens96882432006-06-25 23:38:58 -0700759 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 case TIPC_NAMED_MSG:{
761 tipc_named_msg_event cb = up_ptr->named_msg_cb;
762
Julia Lawall4cec72c2008-01-08 23:48:20 -0800763 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700764 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765 goto reject;
766 dseq.type = msg_nametype(msg);
767 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700768 dseq.upper = (message_type == TIPC_NAMED_MSG)
769 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900771 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 msg_data_sz(msg), msg_importance(msg),
773 &orig, &dseq);
774 break;
775 }
776 }
777 if (buf)
778 buf_discard(buf);
779 buf = next;
780 continue;
781err:
Allan Stephens96882432006-06-25 23:38:58 -0700782 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900785 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 up_ptr->conn_err_cb;
787 u32 peer_port = port_peerport(p_ptr);
788 u32 peer_node = port_peernode(p_ptr);
789
Julia Lawall4cec72c2008-01-08 23:48:20 -0800790 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700791 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700793 if ((msg_origport(msg) != peer_port) ||
794 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 break;
796 tipc_disconnect(dref);
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));
800 break;
801 }
802 case TIPC_DIRECT_MSG:{
803 tipc_msg_err_event cb = up_ptr->err_cb;
804
Julia Lawall4cec72c2008-01-08 23:48:20 -0800805 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700806 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 break;
808 skb_pull(buf, msg_hdr_sz(msg));
809 cb(usr_handle, dref, &buf, msg_data(msg),
810 msg_data_sz(msg), msg_errcode(msg), &orig);
811 break;
812 }
Allan Stephens96882432006-06-25 23:38:58 -0700813 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900815 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 up_ptr->named_err_cb;
817
Julia Lawall4cec72c2008-01-08 23:48:20 -0800818 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700819 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 break;
821 dseq.type = msg_nametype(msg);
822 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700823 dseq.upper = (message_type == TIPC_NAMED_MSG)
824 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900826 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 msg_data_sz(msg), msg_errcode(msg), &dseq);
828 break;
829 }
830 }
831 if (buf)
832 buf_discard(buf);
833 buf = next;
834 continue;
835reject:
836 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
837 buf = next;
838 }
839}
840
841/*
842 * port_dispatcher(): Dispatcher for messages destinated
843 * to the tipc_port interface. Called with port locked.
844 */
845
846static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
847{
848 buf->next = NULL;
849 spin_lock_bh(&queue_lock);
850 if (msg_queue_head) {
851 msg_queue_tail->next = buf;
852 msg_queue_tail = buf;
853 } else {
854 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100855 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 }
857 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700858 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859}
860
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900861/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900863 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864 */
865
866static void port_wakeup_sh(unsigned long ref)
867{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500868 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800870 tipc_continue_event cb = NULL;
871 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872
Per Liden4323add2006-01-18 00:38:21 +0100873 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 if (p_ptr) {
875 up_ptr = p_ptr->user_port;
876 if (up_ptr) {
877 cb = up_ptr->continue_event_cb;
878 uh = up_ptr->usr_handle;
879 }
Per Liden4323add2006-01-18 00:38:21 +0100880 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 }
882 if (cb)
883 cb(uh, ref);
884}
885
886
887static void port_wakeup(struct tipc_port *p_ptr)
888{
Per Liden4323add2006-01-18 00:38:21 +0100889 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890}
891
892void tipc_acknowledge(u32 ref, u32 ack)
893{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500894 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800895 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896
Per Liden4323add2006-01-18 00:38:21 +0100897 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 if (!p_ptr)
899 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500900 if (p_ptr->connected) {
901 p_ptr->conn_unacked -= ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 buf = port_build_proto_msg(port_peerport(p_ptr),
903 port_peernode(p_ptr),
904 ref,
905 tipc_own_addr,
906 CONN_MANAGER,
907 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900908 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 ack);
910 }
Per Liden4323add2006-01-18 00:38:21 +0100911 tipc_port_unlock(p_ptr);
912 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913}
914
915/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000916 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917 */
918
Allan Stephensb0c1e922010-12-31 18:59:22 +0000919int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900920 unsigned int importance,
921 tipc_msg_err_event error_cb,
922 tipc_named_msg_err_event named_error_cb,
923 tipc_conn_shutdown_event conn_error_cb,
924 tipc_msg_event msg_cb,
925 tipc_named_msg_event named_msg_cb,
926 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 tipc_continue_event continue_event_cb,/* May be zero */
928 u32 *portref)
929{
930 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500931 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700933 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700934 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700935 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 return -ENOMEM;
937 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500938 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700939 port_wakeup, importance);
940 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 kfree(up_ptr);
942 return -ENOMEM;
943 }
944
945 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500947 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 up_ptr->err_cb = error_cb;
949 up_ptr->named_err_cb = named_error_cb;
950 up_ptr->conn_err_cb = conn_error_cb;
951 up_ptr->msg_cb = msg_cb;
952 up_ptr->named_msg_cb = named_msg_cb;
953 up_ptr->conn_msg_cb = conn_msg_cb;
954 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500955 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100956 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700957 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958}
959
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960int tipc_portimportance(u32 ref, unsigned int *importance)
961{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500962 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900963
Per Liden4323add2006-01-18 00:38:21 +0100964 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 if (!p_ptr)
966 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500967 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800968 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700969 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970}
971
972int tipc_set_portimportance(u32 ref, unsigned int imp)
973{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500974 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975
976 if (imp > TIPC_CRITICAL_IMPORTANCE)
977 return -EINVAL;
978
Per Liden4323add2006-01-18 00:38:21 +0100979 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 if (!p_ptr)
981 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500982 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800983 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700984 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985}
986
987
988int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
989{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500990 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 struct publication *publ;
992 u32 key;
993 int res = -EINVAL;
994
Per Liden4323add2006-01-18 00:38:21 +0100995 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800996 if (!p_ptr)
997 return -EINVAL;
998
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500999 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 goto exit;
1001 if (seq->lower > seq->upper)
1002 goto exit;
1003 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1004 goto exit;
1005 key = ref + p_ptr->pub_count + 1;
1006 if (key == ref) {
1007 res = -EADDRINUSE;
1008 goto exit;
1009 }
Per Liden4323add2006-01-18 00:38:21 +01001010 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001011 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 if (publ) {
1013 list_add(&publ->pport_list, &p_ptr->publications);
1014 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001015 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001016 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 }
1018exit:
Per Liden4323add2006-01-18 00:38:21 +01001019 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 return res;
1021}
1022
1023int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1024{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001025 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 struct publication *publ;
1027 struct publication *tpubl;
1028 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029
Per Liden4323add2006-01-18 00:38:21 +01001030 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 if (!p_ptr)
1032 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001034 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001036 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001037 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001039 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001041 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 &p_ptr->publications, pport_list) {
1043 if (publ->scope != scope)
1044 continue;
1045 if (publ->type != seq->type)
1046 continue;
1047 if (publ->lower != seq->lower)
1048 continue;
1049 if (publ->upper != seq->upper)
1050 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001051 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001052 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001053 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 break;
1055 }
1056 }
1057 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001058 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001059 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 return res;
1061}
1062
1063int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1064{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001065 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 struct tipc_msg *msg;
1067 int res = -EINVAL;
1068
Per Liden4323add2006-01-18 00:38:21 +01001069 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 if (!p_ptr)
1071 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001072 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 goto exit;
1074 if (!peer->ref)
1075 goto exit;
1076
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001077 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 msg_set_destnode(msg, peer->node);
1079 msg_set_destport(msg, peer->ref);
1080 msg_set_orignode(msg, tipc_own_addr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001081 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001083 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001084 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085
1086 p_ptr->probing_interval = PROBING_INTERVAL;
1087 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001088 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1090
Allan Stephens0e659672010-12-31 18:59:32 +00001091 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001092 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001094 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095exit:
Per Liden4323add2006-01-18 00:38:21 +01001096 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001097 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 return res;
1099}
1100
Allan Stephens0c3141e2008-04-15 00:22:02 -07001101/**
1102 * tipc_disconnect_port - disconnect port from peer
1103 *
1104 * Port must be locked.
1105 */
1106
1107int tipc_disconnect_port(struct tipc_port *tp_ptr)
1108{
1109 int res;
1110
1111 if (tp_ptr->connected) {
1112 tp_ptr->connected = 0;
1113 /* let timer expire on it's own to avoid deadlock! */
1114 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001115 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001116 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001117 } else {
1118 res = -ENOTCONN;
1119 }
1120 return res;
1121}
1122
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123/*
1124 * tipc_disconnect(): Disconnect port form peer.
1125 * This is a node local operation.
1126 */
1127
1128int tipc_disconnect(u32 ref)
1129{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001130 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001131 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132
Per Liden4323add2006-01-18 00:38:21 +01001133 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 if (!p_ptr)
1135 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001136 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001137 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 return res;
1139}
1140
1141/*
1142 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1143 */
1144int tipc_shutdown(u32 ref)
1145{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001146 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001147 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148
Per Liden4323add2006-01-18 00:38:21 +01001149 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 if (!p_ptr)
1151 return -EINVAL;
1152
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001153 if (p_ptr->connected) {
1154 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 if (imp < TIPC_CRITICAL_IMPORTANCE)
1156 imp++;
1157 buf = port_build_proto_msg(port_peerport(p_ptr),
1158 port_peernode(p_ptr),
1159 ref,
1160 tipc_own_addr,
1161 imp,
1162 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001163 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 0);
1165 }
Per Liden4323add2006-01-18 00:38:21 +01001166 tipc_port_unlock(p_ptr);
1167 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 return tipc_disconnect(ref);
1169}
1170
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171/*
Per Liden4323add2006-01-18 00:38:21 +01001172 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 * message for this node.
1174 */
1175
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001176static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001177 struct iovec const *msg_sect,
1178 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179{
1180 struct sk_buff *buf;
1181 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001182
Allan Stephens26896902011-04-21 10:42:07 -05001183 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 MAX_MSG_SIZE, !sender->user_port, &buf);
1185 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001186 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 return res;
1188}
1189
1190/**
1191 * tipc_send - send message sections on connection
1192 */
1193
Allan Stephens26896902011-04-21 10:42:07 -05001194int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1195 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001197 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 u32 destnode;
1199 int res;
1200
Per Liden4323add2006-01-18 00:38:21 +01001201 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001202 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 return -EINVAL;
1204
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001205 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001206 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 destnode = port_peernode(p_ptr);
1208 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001209 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001210 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 else
Allan Stephens26896902011-04-21 10:42:07 -05001212 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1213 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214
1215 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001216 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001217 if (res > 0)
1218 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 return res;
1220 }
1221 }
1222 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001223 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001224 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 }
1226 return -ELINKCONG;
1227}
1228
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001229/**
Allan Stephens12bae472010-11-30 12:01:02 +00001230 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 */
1232
Allan Stephens12bae472010-11-30 12:01:02 +00001233int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001234 unsigned int num_sect, struct iovec const *msg_sect,
1235 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001237 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 struct tipc_msg *msg;
1239 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001240 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 int res;
1242
Per Liden4323add2006-01-18 00:38:21 +01001243 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001244 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 return -EINVAL;
1246
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001247 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001249 msg_set_orignode(msg, tipc_own_addr);
1250 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 msg_set_hdr_sz(msg, LONG_H_SIZE);
1252 msg_set_nametype(msg, name->type);
1253 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001254 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001255 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 msg_set_destnode(msg, destnode);
1257 msg_set_destport(msg, destport);
1258
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001259 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001261 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001262 msg_sect, total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001263 else
1264 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001265 num_sect, total_len,
1266 destnode);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001267 if (likely(res != -ELINKCONG)) {
1268 if (res > 0)
1269 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001271 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001273 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274 }
1275 return -ELINKCONG;
1276 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001277 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001278 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279}
1280
1281/**
Allan Stephens12bae472010-11-30 12:01:02 +00001282 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 */
1284
Allan Stephens12bae472010-11-30 12:01:02 +00001285int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001286 unsigned int num_sect, struct iovec const *msg_sect,
1287 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001289 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 struct tipc_msg *msg;
1291 int res;
1292
Per Liden4323add2006-01-18 00:38:21 +01001293 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001294 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 return -EINVAL;
1296
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001297 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001299 msg_set_lookup_scope(msg, 0);
Allan Stephens12bae472010-11-30 12:01:02 +00001300 msg_set_orignode(msg, tipc_own_addr);
1301 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 msg_set_destnode(msg, dest->node);
1303 msg_set_destport(msg, dest->ref);
1304 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001305
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 if (dest->node == tipc_own_addr)
Allan Stephens26896902011-04-21 10:42:07 -05001307 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1308 total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001309 else
1310 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001311 total_len, dest->node);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001312 if (likely(res != -ELINKCONG)) {
1313 if (res > 0)
1314 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001316 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001318 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 }
1320 return -ELINKCONG;
1321}
1322
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001323/**
Allan Stephens12bae472010-11-30 12:01:02 +00001324 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 */
1326
Allan Stephens12bae472010-11-30 12:01:02 +00001327int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1328 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001330 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 struct tipc_msg *msg;
1332 int res;
1333
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001334 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1335 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 return -EINVAL;
1337
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001338 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001340 msg_set_orignode(msg, tipc_own_addr);
1341 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 msg_set_destnode(msg, dest->node);
1343 msg_set_destport(msg, dest->ref);
1344 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1346 if (skb_cow(buf, DIR_MSG_H_SIZE))
1347 return -ENOMEM;
1348
1349 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001350 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001351
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001353 res = tipc_port_recv_msg(buf);
1354 else
1355 res = tipc_send_buf_fast(buf, dest->node);
1356 if (likely(res != -ELINKCONG)) {
1357 if (res > 0)
1358 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001360 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 if (port_unreliable(p_ptr))
1362 return dsz;
1363 return -ELINKCONG;
1364}
1365