blob: d91efc69e6f9b776868798b220e301f279e3eaf6 [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;
Paul Gortmaker45843102011-12-29 20:33:30 -050083 struct tipc_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
Paul Gortmaker45843102011-12-29 20:33:30 -0500145void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146{
Allan Stephens0e659672010-12-31 18:59:32 +0000147 struct tipc_msg *msg;
Paul Gortmaker45843102011-12-29 20:33:30 -0500148 struct tipc_port_list dports = {0, NULL, };
149 struct tipc_port_list *item = dp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 int cnt = 0;
151
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 msg = buf_msg(buf);
153
154 /* Create destination port list, if one wasn't supplied */
155
156 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100157 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 msg_namelower(msg),
159 msg_nameupper(msg),
160 TIPC_CLUSTER_SCOPE,
161 &dports);
162 item = dp = &dports;
163 }
164
165 /* Deliver a copy of message to each destination port */
166
167 if (dp->count != 0) {
Allan Stephens7f47f5c2011-04-18 10:14:26 -0400168 msg_set_destnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 if (dp->count == 1) {
170 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100171 tipc_port_recv_msg(buf);
172 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 return;
174 }
175 for (; cnt < dp->count; cnt++) {
176 int index = cnt % PLSIZE;
177 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
178
179 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700180 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 goto exit;
182 }
Allan Stephensa0168922010-12-31 18:59:35 +0000183 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000185 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100186 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 }
188 }
189exit:
190 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100191 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192}
193
194/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700195 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900196 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700197 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 */
199
Allan Stephens0ea52242008-07-14 22:42:19 -0700200struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
202 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700203 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500205 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 struct tipc_msg *msg;
207 u32 ref;
208
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700209 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700210 if (!p_ptr) {
211 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700212 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500214 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700216 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700218 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 }
220
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500221 p_ptr->usr_handle = usr_handle;
222 p_ptr->max_pkt = MAX_PKT_DEFAULT;
223 p_ptr->ref = ref;
224 msg = &p_ptr->phdr;
Allan Stephens741d9eb2011-05-31 15:03:18 -0400225 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 INIT_LIST_HEAD(&p_ptr->wait_list);
228 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 p_ptr->dispatcher = dispatcher;
230 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800231 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100233 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 INIT_LIST_HEAD(&p_ptr->publications);
235 INIT_LIST_HEAD(&p_ptr->port_list);
236 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100237 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500238 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239}
240
241int tipc_deleteport(u32 ref)
242{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500243 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800244 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800246 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100247 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900248 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 return -EINVAL;
250
Per Liden4323add2006-01-18 00:38:21 +0100251 tipc_ref_discard(ref);
252 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253
254 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500255 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100257 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 }
Allan Stephense83504f2010-12-31 18:59:30 +0000259 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260
Per Liden4323add2006-01-18 00:38:21 +0100261 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 list_del(&p_ptr->port_list);
263 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100264 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 k_term_timer(&p_ptr->timer);
266 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100267 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700268 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269}
270
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500271static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500273 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274}
275
276int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
277{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500278 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900279
Per Liden4323add2006-01-18 00:38:21 +0100280 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 if (!p_ptr)
282 return -EINVAL;
283 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800284 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700285 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286}
287
288int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
289{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500290 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900291
Per Liden4323add2006-01-18 00:38:21 +0100292 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 if (!p_ptr)
294 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500295 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100296 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700297 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298}
299
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500300static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500302 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303}
304
305int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
306{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500307 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900308
Per Liden4323add2006-01-18 00:38:21 +0100309 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 if (!p_ptr)
311 return -EINVAL;
312 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800313 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700314 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315}
316
317int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
318{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500319 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320
Per Liden4323add2006-01-18 00:38:21 +0100321 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 if (!p_ptr)
323 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500324 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100325 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700326 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327}
328
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900329/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400330 * port_build_proto_msg(): create connection protocol message for port
331 *
332 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400334static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
335 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336{
337 struct sk_buff *buf;
338 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339
Allan Stephens741d9eb2011-05-31 15:03:18 -0400340 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 if (buf) {
342 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400343 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
344 port_peernode(p_ptr));
345 msg_set_destport(msg, port_peerport(p_ptr));
346 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 }
349 return buf;
350}
351
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352int tipc_reject_msg(struct sk_buff *buf, u32 err)
353{
354 struct tipc_msg *msg = buf_msg(buf);
355 struct sk_buff *rbuf;
356 struct tipc_msg *rmsg;
357 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400358 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400360 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400361 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362
363 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400364
365 if (WARN(!msg_isdata(msg),
366 "attempt to reject message with user=%u", msg_user(msg))) {
367 dump_stack();
368 goto exit;
369 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400370 if (msg_errcode(msg) || msg_dest_droppable(msg))
371 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400373 /*
374 * construct returned message by copying rejected message header and
375 * data (or subset), then updating header fields that need adjusting
376 */
377
378 hdr_sz = msg_hdr_sz(msg);
379 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
380
381 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400382 if (rbuf == NULL)
383 goto exit;
384
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400386 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
387
388 if (msg_connected(rmsg)) {
389 imp = msg_importance(rmsg);
390 if (imp < TIPC_CRITICAL_IMPORTANCE)
391 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700392 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400393 msg_set_non_seq(rmsg, 0);
394 msg_set_size(rmsg, rmsg_sz);
395 msg_set_errcode(rmsg, err);
396 msg_set_prevnode(rmsg, tipc_own_addr);
397 msg_swap_words(rmsg, 4, 5);
398 if (!msg_short(rmsg))
399 msg_swap_words(rmsg, 6, 7);
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
Allan Stephensacc631b2011-05-23 13:47:44 -0400414 /* send returned message & dispose of rejected message */
415
Allan Stephens017dac32011-05-24 13:20:09 -0400416 src_node = msg_prevnode(msg);
417 if (src_node == tipc_own_addr)
418 tipc_port_recv_msg(rbuf);
419 else
420 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400421exit:
422 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 return data_sz;
424}
425
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500426int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100427 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500428 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429{
430 struct sk_buff *buf;
431 int res;
432
Allan Stephens26896902011-04-21 10:42:07 -0500433 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 !p_ptr->user_port, &buf);
435 if (!buf)
436 return res;
437
438 return tipc_reject_msg(buf, err);
439}
440
441static void port_timeout(unsigned long ref)
442{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500443 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800444 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445
Allan Stephens065fd172006-10-16 21:38:05 -0700446 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 return;
448
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500449 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700450 tipc_port_unlock(p_ptr);
451 return;
452 }
453
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 /* Last probe answered ? */
455 if (p_ptr->probing_state == PROBING) {
456 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
457 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400458 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 p_ptr->probing_state = PROBING;
460 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
461 }
Per Liden4323add2006-01-18 00:38:21 +0100462 tipc_port_unlock(p_ptr);
463 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464}
465
466
467static void port_handle_node_down(unsigned long ref)
468{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500469 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000470 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471
472 if (!p_ptr)
473 return;
474 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100475 tipc_port_unlock(p_ptr);
476 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477}
478
479
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500480static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481{
Allan Stephense244a912011-05-31 16:10:08 -0400482 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483
Allan Stephense244a912011-05-31 16:10:08 -0400484 if (buf) {
485 struct tipc_msg *msg = buf_msg(buf);
486 msg_swap_words(msg, 4, 5);
487 msg_swap_words(msg, 6, 7);
488 }
489 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490}
491
492
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500493static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494{
Allan Stephense244a912011-05-31 16:10:08 -0400495 struct sk_buff *buf;
496 struct tipc_msg *msg;
497 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500499 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800500 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400501
502 buf = tipc_buf_acquire(BASIC_H_SIZE);
503 if (buf) {
504 msg = buf_msg(buf);
505 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
506 msg_set_hdr_sz(msg, BASIC_H_SIZE);
507 msg_set_size(msg, BASIC_H_SIZE);
508 imp = msg_importance(msg);
509 if (imp < TIPC_CRITICAL_IMPORTANCE)
510 msg_set_importance(msg, ++imp);
511 msg_set_errcode(msg, err);
512 }
513 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514}
515
Per Liden4323add2006-01-18 00:38:21 +0100516void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517{
518 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400519 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800520 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400521 u32 orignode = msg_orignode(msg);
522 u32 origport = msg_origport(msg);
523 u32 destport = msg_destport(msg);
524 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525
Allan Stephens1c1a5512011-06-01 15:08:10 -0400526 /* Validate connection */
527
528 p_ptr = tipc_port_lock(destport);
529 if (!p_ptr || !p_ptr->connected ||
530 (port_peernode(p_ptr) != orignode) ||
531 (port_peerport(p_ptr) != origport)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400532 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
533 if (r_buf) {
534 msg = buf_msg(r_buf);
535 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
536 BASIC_H_SIZE, orignode);
537 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
538 msg_set_origport(msg, destport);
539 msg_set_destport(msg, origport);
540 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400541 if (p_ptr)
542 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 goto exit;
544 }
545
Allan Stephens1c1a5512011-06-01 15:08:10 -0400546 /* Process protocol message sent by peer */
547
548 switch (msg_type(msg)) {
549 case CONN_ACK:
550 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
551 p_ptr->wakeup;
552 p_ptr->acked += msg_msgcnt(msg);
553 if (!tipc_port_congested(p_ptr)) {
554 p_ptr->congested = 0;
555 if (wakeable)
556 p_ptr->wakeup(p_ptr);
557 }
558 break;
559 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400560 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400561 break;
562 default:
563 /* CONN_PROBE_REPLY or unrecognized - no action required */
564 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 }
566 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400567 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568exit:
Per Liden4323add2006-01-18 00:38:21 +0100569 tipc_net_route_msg(r_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 buf_discard(buf);
571}
572
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500573static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900575 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576
577 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900578 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500580 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500582 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500584 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900585 u32 dport = port_peerport(p_ptr);
586 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900588 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
589 tipc_zone(destnode), tipc_cluster(destnode),
590 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500591 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900592 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500593 p_ptr->conn_type,
594 p_ptr->conn_instance);
595 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 tipc_printf(buf, " bound to");
597 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 if (publ->lower == publ->upper)
599 tipc_printf(buf, " {%u,%u}", publ->type,
600 publ->lower);
601 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 }
605 }
606 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607}
608
609#define MAX_PORT_QUERY 32768
610
Per Liden4323add2006-01-18 00:38:21 +0100611struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612{
613 struct sk_buff *buf;
614 struct tlv_desc *rep_tlv;
615 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500616 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 int str_len;
618
Per Liden4323add2006-01-18 00:38:21 +0100619 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 if (!buf)
621 return NULL;
622 rep_tlv = (struct tlv_desc *)buf->data;
623
Per Liden4323add2006-01-18 00:38:21 +0100624 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
625 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500627 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500629 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 }
Per Liden4323add2006-01-18 00:38:21 +0100631 spin_unlock_bh(&tipc_port_list_lock);
632 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633
634 skb_put(buf, TLV_SPACE(str_len));
635 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
636
637 return buf;
638}
639
Per Liden4323add2006-01-18 00:38:21 +0100640void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500642 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 struct tipc_msg *msg;
644
Per Liden4323add2006-01-18 00:38:21 +0100645 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500647 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 if (msg_orignode(msg) == tipc_own_addr)
649 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700650 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 msg_set_orignode(msg, tipc_own_addr);
652 }
Per Liden4323add2006-01-18 00:38:21 +0100653 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654}
655
656
657/*
658 * port_dispatcher_sigh(): Signal handler for messages destinated
659 * to the tipc_port interface.
660 */
661
662static void port_dispatcher_sigh(void *dummy)
663{
664 struct sk_buff *buf;
665
666 spin_lock_bh(&queue_lock);
667 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800668 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 spin_unlock_bh(&queue_lock);
670
671 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500672 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 struct user_port *up_ptr;
674 struct tipc_portid orig;
675 struct tipc_name_seq dseq;
676 void *usr_handle;
677 int connected;
678 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700679 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680
681 struct sk_buff *next = buf->next;
682 struct tipc_msg *msg = buf_msg(buf);
683 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900684
Allan Stephens96882432006-06-25 23:38:58 -0700685 message_type = msg_type(msg);
686 if (message_type > TIPC_DIRECT_MSG)
687 goto reject; /* Unsupported message type */
688
Per Liden4323add2006-01-18 00:38:21 +0100689 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700690 if (!p_ptr)
691 goto reject; /* Port deleted while msg in queue */
692
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 orig.ref = msg_origport(msg);
694 orig.node = msg_orignode(msg);
695 up_ptr = p_ptr->user_port;
696 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500697 connected = p_ptr->connected;
698 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699
700 if (unlikely(msg_errcode(msg)))
701 goto err;
702
Allan Stephens96882432006-06-25 23:38:58 -0700703 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900704
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705 case TIPC_CONN_MSG:{
706 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
707 u32 peer_port = port_peerport(p_ptr);
708 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500709 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710
Julia Lawall4cec72c2008-01-08 23:48:20 -0800711 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700712 if (unlikely(!cb))
713 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700715 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700717 } else if ((msg_origport(msg) != peer_port) ||
718 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500720 dsz = msg_data_sz(msg);
721 if (unlikely(dsz &&
722 (++p_ptr->conn_unacked >=
723 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900724 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500725 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500727 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 break;
729 }
730 case TIPC_DIRECT_MSG:{
731 tipc_msg_event cb = up_ptr->msg_cb;
732
Julia Lawall4cec72c2008-01-08 23:48:20 -0800733 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700734 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 goto reject;
736 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 msg_data_sz(msg), msg_importance(msg),
739 &orig);
740 break;
741 }
Allan Stephens96882432006-06-25 23:38:58 -0700742 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 case TIPC_NAMED_MSG:{
744 tipc_named_msg_event cb = up_ptr->named_msg_cb;
745
Julia Lawall4cec72c2008-01-08 23:48:20 -0800746 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700747 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 goto reject;
749 dseq.type = msg_nametype(msg);
750 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700751 dseq.upper = (message_type == TIPC_NAMED_MSG)
752 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 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, &dseq);
757 break;
758 }
759 }
760 if (buf)
761 buf_discard(buf);
762 buf = next;
763 continue;
764err:
Allan Stephens96882432006-06-25 23:38:58 -0700765 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900766
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900768 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 up_ptr->conn_err_cb;
770 u32 peer_port = port_peerport(p_ptr);
771 u32 peer_node = port_peernode(p_ptr);
772
Julia Lawall4cec72c2008-01-08 23:48:20 -0800773 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700774 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700776 if ((msg_origport(msg) != peer_port) ||
777 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 break;
779 tipc_disconnect(dref);
780 skb_pull(buf, msg_hdr_sz(msg));
781 cb(usr_handle, dref, &buf, msg_data(msg),
782 msg_data_sz(msg), msg_errcode(msg));
783 break;
784 }
785 case TIPC_DIRECT_MSG:{
786 tipc_msg_err_event cb = up_ptr->err_cb;
787
Julia Lawall4cec72c2008-01-08 23:48:20 -0800788 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700789 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 break;
791 skb_pull(buf, msg_hdr_sz(msg));
792 cb(usr_handle, dref, &buf, msg_data(msg),
793 msg_data_sz(msg), msg_errcode(msg), &orig);
794 break;
795 }
Allan Stephens96882432006-06-25 23:38:58 -0700796 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900798 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 up_ptr->named_err_cb;
800
Julia Lawall4cec72c2008-01-08 23:48:20 -0800801 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700802 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 break;
804 dseq.type = msg_nametype(msg);
805 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700806 dseq.upper = (message_type == TIPC_NAMED_MSG)
807 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900809 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 msg_data_sz(msg), msg_errcode(msg), &dseq);
811 break;
812 }
813 }
814 if (buf)
815 buf_discard(buf);
816 buf = next;
817 continue;
818reject:
819 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
820 buf = next;
821 }
822}
823
824/*
825 * port_dispatcher(): Dispatcher for messages destinated
826 * to the tipc_port interface. Called with port locked.
827 */
828
829static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
830{
831 buf->next = NULL;
832 spin_lock_bh(&queue_lock);
833 if (msg_queue_head) {
834 msg_queue_tail->next = buf;
835 msg_queue_tail = buf;
836 } else {
837 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100838 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 }
840 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700841 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842}
843
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900844/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900846 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 */
848
849static void port_wakeup_sh(unsigned long ref)
850{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500851 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800853 tipc_continue_event cb = NULL;
854 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855
Per Liden4323add2006-01-18 00:38:21 +0100856 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 if (p_ptr) {
858 up_ptr = p_ptr->user_port;
859 if (up_ptr) {
860 cb = up_ptr->continue_event_cb;
861 uh = up_ptr->usr_handle;
862 }
Per Liden4323add2006-01-18 00:38:21 +0100863 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864 }
865 if (cb)
866 cb(uh, ref);
867}
868
869
870static void port_wakeup(struct tipc_port *p_ptr)
871{
Per Liden4323add2006-01-18 00:38:21 +0100872 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873}
874
875void tipc_acknowledge(u32 ref, u32 ack)
876{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500877 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800878 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879
Per Liden4323add2006-01-18 00:38:21 +0100880 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 if (!p_ptr)
882 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500883 if (p_ptr->connected) {
884 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400885 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 }
Per Liden4323add2006-01-18 00:38:21 +0100887 tipc_port_unlock(p_ptr);
888 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889}
890
891/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000892 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 */
894
Allan Stephensb0c1e922010-12-31 18:59:22 +0000895int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900896 unsigned int importance,
897 tipc_msg_err_event error_cb,
898 tipc_named_msg_err_event named_error_cb,
899 tipc_conn_shutdown_event conn_error_cb,
900 tipc_msg_event msg_cb,
901 tipc_named_msg_event named_msg_cb,
902 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 tipc_continue_event continue_event_cb,/* May be zero */
904 u32 *portref)
905{
906 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500907 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700909 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700910 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700911 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 return -ENOMEM;
913 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500914 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700915 port_wakeup, importance);
916 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917 kfree(up_ptr);
918 return -ENOMEM;
919 }
920
921 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500923 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 up_ptr->err_cb = error_cb;
925 up_ptr->named_err_cb = named_error_cb;
926 up_ptr->conn_err_cb = conn_error_cb;
927 up_ptr->msg_cb = msg_cb;
928 up_ptr->named_msg_cb = named_msg_cb;
929 up_ptr->conn_msg_cb = conn_msg_cb;
930 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500931 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100932 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700933 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934}
935
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936int tipc_portimportance(u32 ref, unsigned int *importance)
937{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500938 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900939
Per Liden4323add2006-01-18 00:38:21 +0100940 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 if (!p_ptr)
942 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500943 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800944 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700945 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946}
947
948int tipc_set_portimportance(u32 ref, unsigned int imp)
949{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500950 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951
952 if (imp > TIPC_CRITICAL_IMPORTANCE)
953 return -EINVAL;
954
Per Liden4323add2006-01-18 00:38:21 +0100955 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 if (!p_ptr)
957 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500958 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800959 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700960 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961}
962
963
964int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
965{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500966 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967 struct publication *publ;
968 u32 key;
969 int res = -EINVAL;
970
Per Liden4323add2006-01-18 00:38:21 +0100971 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800972 if (!p_ptr)
973 return -EINVAL;
974
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500975 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 goto exit;
977 if (seq->lower > seq->upper)
978 goto exit;
979 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
980 goto exit;
981 key = ref + p_ptr->pub_count + 1;
982 if (key == ref) {
983 res = -EADDRINUSE;
984 goto exit;
985 }
Per Liden4323add2006-01-18 00:38:21 +0100986 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500987 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 if (publ) {
989 list_add(&publ->pport_list, &p_ptr->publications);
990 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500991 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -0700992 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 }
994exit:
Per Liden4323add2006-01-18 00:38:21 +0100995 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 return res;
997}
998
999int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1000{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001001 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 struct publication *publ;
1003 struct publication *tpubl;
1004 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001005
Per Liden4323add2006-01-18 00:38:21 +01001006 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 if (!p_ptr)
1008 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001010 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001012 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001013 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001015 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001017 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 &p_ptr->publications, pport_list) {
1019 if (publ->scope != scope)
1020 continue;
1021 if (publ->type != seq->type)
1022 continue;
1023 if (publ->lower != seq->lower)
1024 continue;
1025 if (publ->upper != seq->upper)
1026 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001028 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001029 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 break;
1031 }
1032 }
1033 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001034 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001035 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 return res;
1037}
1038
1039int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1040{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001041 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 struct tipc_msg *msg;
1043 int res = -EINVAL;
1044
Per Liden4323add2006-01-18 00:38:21 +01001045 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 if (!p_ptr)
1047 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001048 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 goto exit;
1050 if (!peer->ref)
1051 goto exit;
1052
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001053 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 msg_set_destnode(msg, peer->node);
1055 msg_set_destport(msg, peer->ref);
1056 msg_set_orignode(msg, tipc_own_addr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001057 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001059 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001060 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061
1062 p_ptr->probing_interval = PROBING_INTERVAL;
1063 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001064 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1066
Allan Stephens0e659672010-12-31 18:59:32 +00001067 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001068 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001070 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071exit:
Per Liden4323add2006-01-18 00:38:21 +01001072 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001073 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 return res;
1075}
1076
Allan Stephens0c3141e2008-04-15 00:22:02 -07001077/**
1078 * tipc_disconnect_port - disconnect port from peer
1079 *
1080 * Port must be locked.
1081 */
1082
1083int tipc_disconnect_port(struct tipc_port *tp_ptr)
1084{
1085 int res;
1086
1087 if (tp_ptr->connected) {
1088 tp_ptr->connected = 0;
1089 /* let timer expire on it's own to avoid deadlock! */
1090 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001091 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001092 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001093 } else {
1094 res = -ENOTCONN;
1095 }
1096 return res;
1097}
1098
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099/*
1100 * tipc_disconnect(): Disconnect port form peer.
1101 * This is a node local operation.
1102 */
1103
1104int tipc_disconnect(u32 ref)
1105{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001106 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001107 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108
Per Liden4323add2006-01-18 00:38:21 +01001109 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110 if (!p_ptr)
1111 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001112 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001113 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 return res;
1115}
1116
1117/*
1118 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1119 */
1120int tipc_shutdown(u32 ref)
1121{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001122 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001123 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124
Per Liden4323add2006-01-18 00:38:21 +01001125 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 if (!p_ptr)
1127 return -EINVAL;
1128
Allan Stephense244a912011-05-31 16:10:08 -04001129 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +01001130 tipc_port_unlock(p_ptr);
1131 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 return tipc_disconnect(ref);
1133}
1134
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135/*
Per Liden4323add2006-01-18 00:38:21 +01001136 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 * message for this node.
1138 */
1139
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001140static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001141 struct iovec const *msg_sect,
1142 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143{
1144 struct sk_buff *buf;
1145 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001146
Allan Stephens26896902011-04-21 10:42:07 -05001147 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 MAX_MSG_SIZE, !sender->user_port, &buf);
1149 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001150 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 return res;
1152}
1153
1154/**
1155 * tipc_send - send message sections on connection
1156 */
1157
Allan Stephens26896902011-04-21 10:42:07 -05001158int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1159 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001161 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 u32 destnode;
1163 int res;
1164
Per Liden4323add2006-01-18 00:38:21 +01001165 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001166 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 return -EINVAL;
1168
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001169 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001170 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 destnode = port_peernode(p_ptr);
1172 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001173 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001174 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 else
Allan Stephens26896902011-04-21 10:42:07 -05001176 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1177 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178
1179 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001180 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001181 if (res > 0)
1182 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 return res;
1184 }
1185 }
1186 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001187 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001188 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 }
1190 return -ELINKCONG;
1191}
1192
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001193/**
Allan Stephens12bae472010-11-30 12:01:02 +00001194 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 */
1196
Allan Stephens12bae472010-11-30 12:01:02 +00001197int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001198 unsigned int num_sect, struct iovec const *msg_sect,
1199 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001201 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 struct tipc_msg *msg;
1203 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001204 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 int res;
1206
Per Liden4323add2006-01-18 00:38:21 +01001207 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001208 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 return -EINVAL;
1210
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001211 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001213 msg_set_orignode(msg, tipc_own_addr);
1214 msg_set_origport(msg, ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001215 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 msg_set_nametype(msg, name->type);
1217 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001218 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001219 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 msg_set_destnode(msg, destnode);
1221 msg_set_destport(msg, destport);
1222
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001223 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001225 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001226 msg_sect, total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001227 else
1228 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001229 num_sect, total_len,
1230 destnode);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001231 if (likely(res != -ELINKCONG)) {
1232 if (res > 0)
1233 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001235 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001237 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 }
1239 return -ELINKCONG;
1240 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001241 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001242 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243}
1244
1245/**
Allan Stephens12bae472010-11-30 12:01:02 +00001246 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 */
1248
Allan Stephens12bae472010-11-30 12:01:02 +00001249int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001250 unsigned int num_sect, struct iovec const *msg_sect,
1251 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001253 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 struct tipc_msg *msg;
1255 int res;
1256
Per Liden4323add2006-01-18 00:38:21 +01001257 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001258 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 return -EINVAL;
1260
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001261 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001263 msg_set_lookup_scope(msg, 0);
Allan Stephens12bae472010-11-30 12:01:02 +00001264 msg_set_orignode(msg, tipc_own_addr);
1265 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 msg_set_destnode(msg, dest->node);
1267 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001268 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001269
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 if (dest->node == tipc_own_addr)
Allan Stephens26896902011-04-21 10:42:07 -05001271 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1272 total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001273 else
1274 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001275 total_len, dest->node);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001276 if (likely(res != -ELINKCONG)) {
1277 if (res > 0)
1278 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001280 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001282 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 }
1284 return -ELINKCONG;
1285}
1286
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001287/**
Allan Stephens12bae472010-11-30 12:01:02 +00001288 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289 */
1290
Allan Stephens12bae472010-11-30 12:01:02 +00001291int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1292 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001294 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 struct tipc_msg *msg;
1296 int res;
1297
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001298 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1299 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 return -EINVAL;
1301
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001302 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001304 msg_set_orignode(msg, tipc_own_addr);
1305 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 msg_set_destnode(msg, dest->node);
1307 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001308 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1309 msg_set_size(msg, BASIC_H_SIZE + dsz);
1310 if (skb_cow(buf, BASIC_H_SIZE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 return -ENOMEM;
1312
Allan Stephens741d9eb2011-05-31 15:03:18 -04001313 skb_push(buf, BASIC_H_SIZE);
1314 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001315
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001317 res = tipc_port_recv_msg(buf);
1318 else
1319 res = tipc_send_buf_fast(buf, dest->node);
1320 if (likely(res != -ELINKCONG)) {
1321 if (res > 0)
1322 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001324 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 if (port_unreliable(p_ptr))
1326 return dsz;
1327 return -ELINKCONG;
1328}
1329