blob: c68dc956a4234e5759b53d5e3e460e61d882b171 [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 */
370 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
371 buf_discard(buf);
372 return data_sz;
373 }
374
375 /* construct rejected message */
376 if (msg_mcast(msg))
377 hdr_sz = MCAST_H_SIZE;
378 else
379 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000380 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381 if (rbuf == NULL) {
382 buf_discard(buf);
383 return data_sz;
384 }
385 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000386 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700387 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700390 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700392 /* leave name type & instance as zeroes */
393 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700395 msg_set_nametype(rmsg, msg_nametype(msg));
396 msg_set_nameinst(rmsg, msg_nameinst(msg));
397 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900398 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300399 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400
401 /* send self-abort message when rejecting on a connected port */
402 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800403 struct sk_buff *abuf = NULL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500404 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405
406 if (p_ptr) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500407 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100409 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 }
Per Liden4323add2006-01-18 00:38:21 +0100411 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 }
413
414 /* send rejected message */
415 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100416 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 return data_sz;
418}
419
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500420int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100421 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500422 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423{
424 struct sk_buff *buf;
425 int res;
426
Allan Stephens26896902011-04-21 10:42:07 -0500427 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 !p_ptr->user_port, &buf);
429 if (!buf)
430 return res;
431
432 return tipc_reject_msg(buf, err);
433}
434
435static void port_timeout(unsigned long ref)
436{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500437 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800438 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439
Allan Stephens065fd172006-10-16 21:38:05 -0700440 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 return;
442
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500443 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700444 tipc_port_unlock(p_ptr);
445 return;
446 }
447
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 /* Last probe answered ? */
449 if (p_ptr->probing_state == PROBING) {
450 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
451 } else {
452 buf = port_build_proto_msg(port_peerport(p_ptr),
453 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500454 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 tipc_own_addr,
456 CONN_MANAGER,
457 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900458 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 p_ptr->probing_state = PROBING;
461 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
462 }
Per Liden4323add2006-01-18 00:38:21 +0100463 tipc_port_unlock(p_ptr);
464 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465}
466
467
468static void port_handle_node_down(unsigned long ref)
469{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500470 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000471 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472
473 if (!p_ptr)
474 return;
475 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100476 tipc_port_unlock(p_ptr);
477 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478}
479
480
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500481static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500483 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500485 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800486 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 if (imp < TIPC_CRITICAL_IMPORTANCE)
488 imp++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500489 return port_build_proto_msg(p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490 tipc_own_addr,
491 port_peerport(p_ptr),
492 port_peernode(p_ptr),
493 imp,
494 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900495 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 0);
497}
498
499
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500500static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500502 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500504 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800505 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 if (imp < TIPC_CRITICAL_IMPORTANCE)
507 imp++;
508 return port_build_proto_msg(port_peerport(p_ptr),
509 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500510 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 tipc_own_addr,
512 imp,
513 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900514 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 0);
516}
517
Per Liden4323add2006-01-18 00:38:21 +0100518void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519{
520 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500521 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800523 struct sk_buff *r_buf = NULL;
524 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 if (!p_ptr) {
527 err = TIPC_ERR_NO_PORT;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500528 } else if (p_ptr->connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000529 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
530 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000532 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900533 int wakeup = tipc_port_congested(p_ptr) &&
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500534 p_ptr->congested &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 p_ptr->wakeup;
536 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100537 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500539 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 if (!wakeup)
541 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500542 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 goto exit;
544 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500545 } else if (p_ptr->published) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 err = TIPC_ERR_NO_PORT;
547 }
548 if (err) {
549 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550 msg_orignode(msg),
551 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800553 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 TIPC_CONN_MSG,
555 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 0);
557 goto exit;
558 }
559
560 /* All is fine */
561 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 r_buf = port_build_proto_msg(msg_origport(msg),
563 msg_orignode(msg),
564 msg_destport(msg),
565 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 CONN_MANAGER,
567 CONN_PROBE_REPLY,
568 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 0);
570 }
571 p_ptr->probing_state = CONFIRMED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572exit:
573 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100574 tipc_port_unlock(p_ptr);
575 tipc_net_route_msg(r_buf);
576 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 buf_discard(buf);
578}
579
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500580static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900582 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583
584 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900585 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500587 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500589 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500591 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900592 u32 dport = port_peerport(p_ptr);
593 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
596 tipc_zone(destnode), tipc_cluster(destnode),
597 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500598 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500600 p_ptr->conn_type,
601 p_ptr->conn_instance);
602 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603 tipc_printf(buf, " bound to");
604 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 if (publ->lower == publ->upper)
606 tipc_printf(buf, " {%u,%u}", publ->type,
607 publ->lower);
608 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900609 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611 }
612 }
613 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614}
615
616#define MAX_PORT_QUERY 32768
617
Per Liden4323add2006-01-18 00:38:21 +0100618struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619{
620 struct sk_buff *buf;
621 struct tlv_desc *rep_tlv;
622 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500623 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 int str_len;
625
Per Liden4323add2006-01-18 00:38:21 +0100626 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 if (!buf)
628 return NULL;
629 rep_tlv = (struct tlv_desc *)buf->data;
630
Per Liden4323add2006-01-18 00:38:21 +0100631 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
632 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500634 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500636 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 }
Per Liden4323add2006-01-18 00:38:21 +0100638 spin_unlock_bh(&tipc_port_list_lock);
639 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640
641 skb_put(buf, TLV_SPACE(str_len));
642 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
643
644 return buf;
645}
646
Per Liden4323add2006-01-18 00:38:21 +0100647void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500649 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 struct tipc_msg *msg;
651
Per Liden4323add2006-01-18 00:38:21 +0100652 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500654 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 if (msg_orignode(msg) == tipc_own_addr)
656 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700657 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658 msg_set_orignode(msg, tipc_own_addr);
659 }
Per Liden4323add2006-01-18 00:38:21 +0100660 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661}
662
663
664/*
665 * port_dispatcher_sigh(): Signal handler for messages destinated
666 * to the tipc_port interface.
667 */
668
669static void port_dispatcher_sigh(void *dummy)
670{
671 struct sk_buff *buf;
672
673 spin_lock_bh(&queue_lock);
674 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800675 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 spin_unlock_bh(&queue_lock);
677
678 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500679 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 struct user_port *up_ptr;
681 struct tipc_portid orig;
682 struct tipc_name_seq dseq;
683 void *usr_handle;
684 int connected;
685 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700686 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687
688 struct sk_buff *next = buf->next;
689 struct tipc_msg *msg = buf_msg(buf);
690 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900691
Allan Stephens96882432006-06-25 23:38:58 -0700692 message_type = msg_type(msg);
693 if (message_type > TIPC_DIRECT_MSG)
694 goto reject; /* Unsupported message type */
695
Per Liden4323add2006-01-18 00:38:21 +0100696 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700697 if (!p_ptr)
698 goto reject; /* Port deleted while msg in queue */
699
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700 orig.ref = msg_origport(msg);
701 orig.node = msg_orignode(msg);
702 up_ptr = p_ptr->user_port;
703 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500704 connected = p_ptr->connected;
705 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706
707 if (unlikely(msg_errcode(msg)))
708 goto err;
709
Allan Stephens96882432006-06-25 23:38:58 -0700710 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900711
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 case TIPC_CONN_MSG:{
713 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
714 u32 peer_port = port_peerport(p_ptr);
715 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500716 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717
Julia Lawall4cec72c2008-01-08 23:48:20 -0800718 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700719 if (unlikely(!cb))
720 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700722 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700724 } else if ((msg_origport(msg) != peer_port) ||
725 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500727 dsz = msg_data_sz(msg);
728 if (unlikely(dsz &&
729 (++p_ptr->conn_unacked >=
730 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900731 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500732 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500734 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 break;
736 }
737 case TIPC_DIRECT_MSG:{
738 tipc_msg_event cb = up_ptr->msg_cb;
739
Julia Lawall4cec72c2008-01-08 23:48:20 -0800740 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700741 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 goto reject;
743 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900744 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 msg_data_sz(msg), msg_importance(msg),
746 &orig);
747 break;
748 }
Allan Stephens96882432006-06-25 23:38:58 -0700749 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 case TIPC_NAMED_MSG:{
751 tipc_named_msg_event cb = up_ptr->named_msg_cb;
752
Julia Lawall4cec72c2008-01-08 23:48:20 -0800753 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700754 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 goto reject;
756 dseq.type = msg_nametype(msg);
757 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700758 dseq.upper = (message_type == TIPC_NAMED_MSG)
759 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 msg_data_sz(msg), msg_importance(msg),
763 &orig, &dseq);
764 break;
765 }
766 }
767 if (buf)
768 buf_discard(buf);
769 buf = next;
770 continue;
771err:
Allan Stephens96882432006-06-25 23:38:58 -0700772 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900773
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900775 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 up_ptr->conn_err_cb;
777 u32 peer_port = port_peerport(p_ptr);
778 u32 peer_node = port_peernode(p_ptr);
779
Julia Lawall4cec72c2008-01-08 23:48:20 -0800780 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700781 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700783 if ((msg_origport(msg) != peer_port) ||
784 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 break;
786 tipc_disconnect(dref);
787 skb_pull(buf, msg_hdr_sz(msg));
788 cb(usr_handle, dref, &buf, msg_data(msg),
789 msg_data_sz(msg), msg_errcode(msg));
790 break;
791 }
792 case TIPC_DIRECT_MSG:{
793 tipc_msg_err_event cb = up_ptr->err_cb;
794
Julia Lawall4cec72c2008-01-08 23:48:20 -0800795 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700796 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797 break;
798 skb_pull(buf, msg_hdr_sz(msg));
799 cb(usr_handle, dref, &buf, msg_data(msg),
800 msg_data_sz(msg), msg_errcode(msg), &orig);
801 break;
802 }
Allan Stephens96882432006-06-25 23:38:58 -0700803 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 up_ptr->named_err_cb;
807
Julia Lawall4cec72c2008-01-08 23:48:20 -0800808 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700809 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 break;
811 dseq.type = msg_nametype(msg);
812 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700813 dseq.upper = (message_type == TIPC_NAMED_MSG)
814 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900816 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 msg_data_sz(msg), msg_errcode(msg), &dseq);
818 break;
819 }
820 }
821 if (buf)
822 buf_discard(buf);
823 buf = next;
824 continue;
825reject:
826 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
827 buf = next;
828 }
829}
830
831/*
832 * port_dispatcher(): Dispatcher for messages destinated
833 * to the tipc_port interface. Called with port locked.
834 */
835
836static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
837{
838 buf->next = NULL;
839 spin_lock_bh(&queue_lock);
840 if (msg_queue_head) {
841 msg_queue_tail->next = buf;
842 msg_queue_tail = buf;
843 } else {
844 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100845 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 }
847 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700848 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849}
850
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900851/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900853 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 */
855
856static void port_wakeup_sh(unsigned long ref)
857{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500858 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800860 tipc_continue_event cb = NULL;
861 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862
Per Liden4323add2006-01-18 00:38:21 +0100863 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864 if (p_ptr) {
865 up_ptr = p_ptr->user_port;
866 if (up_ptr) {
867 cb = up_ptr->continue_event_cb;
868 uh = up_ptr->usr_handle;
869 }
Per Liden4323add2006-01-18 00:38:21 +0100870 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 }
872 if (cb)
873 cb(uh, ref);
874}
875
876
877static void port_wakeup(struct tipc_port *p_ptr)
878{
Per Liden4323add2006-01-18 00:38:21 +0100879 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880}
881
882void tipc_acknowledge(u32 ref, u32 ack)
883{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500884 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800885 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886
Per Liden4323add2006-01-18 00:38:21 +0100887 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 if (!p_ptr)
889 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500890 if (p_ptr->connected) {
891 p_ptr->conn_unacked -= ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 buf = port_build_proto_msg(port_peerport(p_ptr),
893 port_peernode(p_ptr),
894 ref,
895 tipc_own_addr,
896 CONN_MANAGER,
897 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900898 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 ack);
900 }
Per Liden4323add2006-01-18 00:38:21 +0100901 tipc_port_unlock(p_ptr);
902 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903}
904
905/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000906 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 */
908
Allan Stephensb0c1e922010-12-31 18:59:22 +0000909int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900910 unsigned int importance,
911 tipc_msg_err_event error_cb,
912 tipc_named_msg_err_event named_error_cb,
913 tipc_conn_shutdown_event conn_error_cb,
914 tipc_msg_event msg_cb,
915 tipc_named_msg_event named_msg_cb,
916 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917 tipc_continue_event continue_event_cb,/* May be zero */
918 u32 *portref)
919{
920 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500921 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700923 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700924 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700925 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 return -ENOMEM;
927 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500928 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700929 port_wakeup, importance);
930 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 kfree(up_ptr);
932 return -ENOMEM;
933 }
934
935 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500937 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938 up_ptr->err_cb = error_cb;
939 up_ptr->named_err_cb = named_error_cb;
940 up_ptr->conn_err_cb = conn_error_cb;
941 up_ptr->msg_cb = msg_cb;
942 up_ptr->named_msg_cb = named_msg_cb;
943 up_ptr->conn_msg_cb = conn_msg_cb;
944 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500945 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100946 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700947 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948}
949
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950int tipc_portimportance(u32 ref, unsigned int *importance)
951{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500952 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900953
Per Liden4323add2006-01-18 00:38:21 +0100954 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 if (!p_ptr)
956 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500957 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800958 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700959 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960}
961
962int tipc_set_portimportance(u32 ref, unsigned int imp)
963{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500964 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965
966 if (imp > TIPC_CRITICAL_IMPORTANCE)
967 return -EINVAL;
968
Per Liden4323add2006-01-18 00:38:21 +0100969 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 if (!p_ptr)
971 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500972 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800973 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700974 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975}
976
977
978int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
979{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500980 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 struct publication *publ;
982 u32 key;
983 int res = -EINVAL;
984
Per Liden4323add2006-01-18 00:38:21 +0100985 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800986 if (!p_ptr)
987 return -EINVAL;
988
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500989 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 goto exit;
991 if (seq->lower > seq->upper)
992 goto exit;
993 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
994 goto exit;
995 key = ref + p_ptr->pub_count + 1;
996 if (key == ref) {
997 res = -EADDRINUSE;
998 goto exit;
999 }
Per Liden4323add2006-01-18 00:38:21 +01001000 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001001 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 if (publ) {
1003 list_add(&publ->pport_list, &p_ptr->publications);
1004 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001005 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001006 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 }
1008exit:
Per Liden4323add2006-01-18 00:38:21 +01001009 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 return res;
1011}
1012
1013int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1014{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001015 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 struct publication *publ;
1017 struct publication *tpubl;
1018 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019
Per Liden4323add2006-01-18 00:38:21 +01001020 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 if (!p_ptr)
1022 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001027 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001029 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 &p_ptr->publications, pport_list) {
1033 if (publ->scope != scope)
1034 continue;
1035 if (publ->type != seq->type)
1036 continue;
1037 if (publ->lower != seq->lower)
1038 continue;
1039 if (publ->upper != seq->upper)
1040 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001041 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001042 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001043 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 break;
1045 }
1046 }
1047 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001048 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001049 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 return res;
1051}
1052
1053int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1054{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001055 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056 struct tipc_msg *msg;
1057 int res = -EINVAL;
1058
Per Liden4323add2006-01-18 00:38:21 +01001059 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 if (!p_ptr)
1061 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001062 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 goto exit;
1064 if (!peer->ref)
1065 goto exit;
1066
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001067 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 msg_set_destnode(msg, peer->node);
1069 msg_set_destport(msg, peer->ref);
1070 msg_set_orignode(msg, tipc_own_addr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001071 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001073 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001074 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075
1076 p_ptr->probing_interval = PROBING_INTERVAL;
1077 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001078 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1080
Allan Stephens0e659672010-12-31 18:59:32 +00001081 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001082 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001084 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085exit:
Per Liden4323add2006-01-18 00:38:21 +01001086 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001087 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 return res;
1089}
1090
Allan Stephens0c3141e2008-04-15 00:22:02 -07001091/**
1092 * tipc_disconnect_port - disconnect port from peer
1093 *
1094 * Port must be locked.
1095 */
1096
1097int tipc_disconnect_port(struct tipc_port *tp_ptr)
1098{
1099 int res;
1100
1101 if (tp_ptr->connected) {
1102 tp_ptr->connected = 0;
1103 /* let timer expire on it's own to avoid deadlock! */
1104 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001105 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001106 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001107 } else {
1108 res = -ENOTCONN;
1109 }
1110 return res;
1111}
1112
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113/*
1114 * tipc_disconnect(): Disconnect port form peer.
1115 * This is a node local operation.
1116 */
1117
1118int tipc_disconnect(u32 ref)
1119{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001120 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001121 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122
Per Liden4323add2006-01-18 00:38:21 +01001123 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 if (!p_ptr)
1125 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001126 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001127 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 return res;
1129}
1130
1131/*
1132 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1133 */
1134int tipc_shutdown(u32 ref)
1135{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001136 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001137 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138
Per Liden4323add2006-01-18 00:38:21 +01001139 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 if (!p_ptr)
1141 return -EINVAL;
1142
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001143 if (p_ptr->connected) {
1144 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 if (imp < TIPC_CRITICAL_IMPORTANCE)
1146 imp++;
1147 buf = port_build_proto_msg(port_peerport(p_ptr),
1148 port_peernode(p_ptr),
1149 ref,
1150 tipc_own_addr,
1151 imp,
1152 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001153 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 0);
1155 }
Per Liden4323add2006-01-18 00:38:21 +01001156 tipc_port_unlock(p_ptr);
1157 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 return tipc_disconnect(ref);
1159}
1160
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161/*
Per Liden4323add2006-01-18 00:38:21 +01001162 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 * message for this node.
1164 */
1165
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001166static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001167 struct iovec const *msg_sect,
1168 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169{
1170 struct sk_buff *buf;
1171 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001172
Allan Stephens26896902011-04-21 10:42:07 -05001173 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 MAX_MSG_SIZE, !sender->user_port, &buf);
1175 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001176 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 return res;
1178}
1179
1180/**
1181 * tipc_send - send message sections on connection
1182 */
1183
Allan Stephens26896902011-04-21 10:42:07 -05001184int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1185 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001187 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 u32 destnode;
1189 int res;
1190
Per Liden4323add2006-01-18 00:38:21 +01001191 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001192 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 return -EINVAL;
1194
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001195 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001196 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 destnode = port_peernode(p_ptr);
1198 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001199 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001200 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 else
Allan Stephens26896902011-04-21 10:42:07 -05001202 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1203 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204
1205 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001206 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001207 if (res > 0)
1208 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 return res;
1210 }
1211 }
1212 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001213 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001214 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 }
1216 return -ELINKCONG;
1217}
1218
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001219/**
Allan Stephens12bae472010-11-30 12:01:02 +00001220 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 */
1222
Allan Stephens12bae472010-11-30 12:01:02 +00001223int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001224 unsigned int num_sect, struct iovec const *msg_sect,
1225 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001227 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 struct tipc_msg *msg;
1229 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001230 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 int res;
1232
Per Liden4323add2006-01-18 00:38:21 +01001233 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001234 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 return -EINVAL;
1236
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001237 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001239 msg_set_orignode(msg, tipc_own_addr);
1240 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 msg_set_hdr_sz(msg, LONG_H_SIZE);
1242 msg_set_nametype(msg, name->type);
1243 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001244 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001245 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 msg_set_destnode(msg, destnode);
1247 msg_set_destport(msg, destport);
1248
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001249 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001251 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001252 msg_sect, total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001253 else
1254 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001255 num_sect, total_len,
1256 destnode);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001257 if (likely(res != -ELINKCONG)) {
1258 if (res > 0)
1259 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001261 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001263 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 }
1265 return -ELINKCONG;
1266 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001267 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001268 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269}
1270
1271/**
Allan Stephens12bae472010-11-30 12:01:02 +00001272 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 */
1274
Allan Stephens12bae472010-11-30 12:01:02 +00001275int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001276 unsigned int num_sect, struct iovec const *msg_sect,
1277 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001279 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 struct tipc_msg *msg;
1281 int res;
1282
Per Liden4323add2006-01-18 00:38:21 +01001283 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001284 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 return -EINVAL;
1286
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001287 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001289 msg_set_lookup_scope(msg, 0);
Allan Stephens12bae472010-11-30 12:01:02 +00001290 msg_set_orignode(msg, tipc_own_addr);
1291 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 msg_set_destnode(msg, dest->node);
1293 msg_set_destport(msg, dest->ref);
1294 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001295
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 if (dest->node == tipc_own_addr)
Allan Stephens26896902011-04-21 10:42:07 -05001297 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1298 total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001299 else
1300 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001301 total_len, dest->node);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001302 if (likely(res != -ELINKCONG)) {
1303 if (res > 0)
1304 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001306 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001308 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 }
1310 return -ELINKCONG;
1311}
1312
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001313/**
Allan Stephens12bae472010-11-30 12:01:02 +00001314 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 */
1316
Allan Stephens12bae472010-11-30 12:01:02 +00001317int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1318 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001320 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 struct tipc_msg *msg;
1322 int res;
1323
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001324 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1325 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 return -EINVAL;
1327
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001328 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001330 msg_set_orignode(msg, tipc_own_addr);
1331 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 msg_set_destnode(msg, dest->node);
1333 msg_set_destport(msg, dest->ref);
1334 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1336 if (skb_cow(buf, DIR_MSG_H_SIZE))
1337 return -ENOMEM;
1338
1339 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001340 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001341
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001343 res = tipc_port_recv_msg(buf);
1344 else
1345 res = tipc_send_buf_fast(buf, dest->node);
1346 if (likely(res != -ELINKCONG)) {
1347 if (res > 0)
1348 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001350 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 if (port_unreliable(p_ptr))
1352 return dsz;
1353 return -ELINKCONG;
1354}
1355