blob: 6156c6740f676313218544fb9d51818b2645d141 [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);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400119 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 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))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400125 kfree_skb(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:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400190 kfree_skb(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)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500403 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404
405 if (p_ptr) {
Allan Stephensdff10e92011-11-02 10:32:14 -0400406 struct sk_buff *abuf = NULL;
407
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500408 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100410 tipc_port_unlock(p_ptr);
Allan Stephensdff10e92011-11-02 10:32:14 -0400411 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 }
414
Allan Stephensacc631b2011-05-23 13:47:44 -0400415 /* send returned message & dispose of rejected message */
416
Allan Stephens017dac32011-05-24 13:20:09 -0400417 src_node = msg_prevnode(msg);
418 if (src_node == tipc_own_addr)
419 tipc_port_recv_msg(rbuf);
420 else
421 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400422exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400423 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 return data_sz;
425}
426
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500427int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100428 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500429 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430{
431 struct sk_buff *buf;
432 int res;
433
Allan Stephens26896902011-04-21 10:42:07 -0500434 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 !p_ptr->user_port, &buf);
436 if (!buf)
437 return res;
438
439 return tipc_reject_msg(buf, err);
440}
441
442static void port_timeout(unsigned long ref)
443{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500444 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800445 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446
Allan Stephens065fd172006-10-16 21:38:05 -0700447 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 return;
449
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500450 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700451 tipc_port_unlock(p_ptr);
452 return;
453 }
454
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 /* Last probe answered ? */
456 if (p_ptr->probing_state == PROBING) {
457 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
458 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400459 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 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 Stephense244a912011-05-31 16:10:08 -0400483 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484
Allan Stephense244a912011-05-31 16:10:08 -0400485 if (buf) {
486 struct tipc_msg *msg = buf_msg(buf);
487 msg_swap_words(msg, 4, 5);
488 msg_swap_words(msg, 6, 7);
489 }
490 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491}
492
493
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500494static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495{
Allan Stephense244a912011-05-31 16:10:08 -0400496 struct sk_buff *buf;
497 struct tipc_msg *msg;
498 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500500 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800501 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400502
503 buf = tipc_buf_acquire(BASIC_H_SIZE);
504 if (buf) {
505 msg = buf_msg(buf);
506 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
507 msg_set_hdr_sz(msg, BASIC_H_SIZE);
508 msg_set_size(msg, BASIC_H_SIZE);
509 imp = msg_importance(msg);
510 if (imp < TIPC_CRITICAL_IMPORTANCE)
511 msg_set_importance(msg, ++imp);
512 msg_set_errcode(msg, err);
513 }
514 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515}
516
Per Liden4323add2006-01-18 00:38:21 +0100517void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518{
519 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400520 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800521 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400522 u32 orignode = msg_orignode(msg);
523 u32 origport = msg_origport(msg);
524 u32 destport = msg_destport(msg);
525 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526
Allan Stephens1c1a5512011-06-01 15:08:10 -0400527 /* Validate connection */
528
529 p_ptr = tipc_port_lock(destport);
530 if (!p_ptr || !p_ptr->connected ||
531 (port_peernode(p_ptr) != orignode) ||
532 (port_peerport(p_ptr) != origport)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400533 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
534 if (r_buf) {
535 msg = buf_msg(r_buf);
536 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
537 BASIC_H_SIZE, orignode);
538 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
539 msg_set_origport(msg, destport);
540 msg_set_destport(msg, origport);
541 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400542 if (p_ptr)
543 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 goto exit;
545 }
546
Allan Stephens1c1a5512011-06-01 15:08:10 -0400547 /* Process protocol message sent by peer */
548
549 switch (msg_type(msg)) {
550 case CONN_ACK:
551 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
552 p_ptr->wakeup;
553 p_ptr->acked += msg_msgcnt(msg);
554 if (!tipc_port_congested(p_ptr)) {
555 p_ptr->congested = 0;
556 if (wakeable)
557 p_ptr->wakeup(p_ptr);
558 }
559 break;
560 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400561 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400562 break;
563 default:
564 /* CONN_PROBE_REPLY or unrecognized - no action required */
565 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 }
567 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400568 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569exit:
Per Liden4323add2006-01-18 00:38:21 +0100570 tipc_net_route_msg(r_buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400571 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572}
573
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500574static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577
578 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500581 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500583 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500585 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 u32 dport = port_peerport(p_ptr);
587 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900589 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
590 tipc_zone(destnode), tipc_cluster(destnode),
591 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500592 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500594 p_ptr->conn_type,
595 p_ptr->conn_instance);
596 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 tipc_printf(buf, " bound to");
598 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 if (publ->lower == publ->upper)
600 tipc_printf(buf, " {%u,%u}", publ->type,
601 publ->lower);
602 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900605 }
606 }
607 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608}
609
610#define MAX_PORT_QUERY 32768
611
Per Liden4323add2006-01-18 00:38:21 +0100612struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613{
614 struct sk_buff *buf;
615 struct tlv_desc *rep_tlv;
616 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500617 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 int str_len;
619
Per Liden4323add2006-01-18 00:38:21 +0100620 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 if (!buf)
622 return NULL;
623 rep_tlv = (struct tlv_desc *)buf->data;
624
Per Liden4323add2006-01-18 00:38:21 +0100625 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
626 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500628 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500630 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 }
Per Liden4323add2006-01-18 00:38:21 +0100632 spin_unlock_bh(&tipc_port_list_lock);
633 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634
635 skb_put(buf, TLV_SPACE(str_len));
636 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
637
638 return buf;
639}
640
Per Liden4323add2006-01-18 00:38:21 +0100641void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500643 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 struct tipc_msg *msg;
645
Per Liden4323add2006-01-18 00:38:21 +0100646 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500648 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700649 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 msg_set_orignode(msg, tipc_own_addr);
651 }
Per Liden4323add2006-01-18 00:38:21 +0100652 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653}
654
655
656/*
657 * port_dispatcher_sigh(): Signal handler for messages destinated
658 * to the tipc_port interface.
659 */
660
661static void port_dispatcher_sigh(void *dummy)
662{
663 struct sk_buff *buf;
664
665 spin_lock_bh(&queue_lock);
666 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800667 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 spin_unlock_bh(&queue_lock);
669
670 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500671 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 struct user_port *up_ptr;
673 struct tipc_portid orig;
674 struct tipc_name_seq dseq;
675 void *usr_handle;
676 int connected;
677 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700678 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679
680 struct sk_buff *next = buf->next;
681 struct tipc_msg *msg = buf_msg(buf);
682 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900683
Allan Stephens96882432006-06-25 23:38:58 -0700684 message_type = msg_type(msg);
685 if (message_type > TIPC_DIRECT_MSG)
686 goto reject; /* Unsupported message type */
687
Per Liden4323add2006-01-18 00:38:21 +0100688 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700689 if (!p_ptr)
690 goto reject; /* Port deleted while msg in queue */
691
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692 orig.ref = msg_origport(msg);
693 orig.node = msg_orignode(msg);
694 up_ptr = p_ptr->user_port;
695 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500696 connected = p_ptr->connected;
697 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698
699 if (unlikely(msg_errcode(msg)))
700 goto err;
701
Allan Stephens96882432006-06-25 23:38:58 -0700702 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900703
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704 case TIPC_CONN_MSG:{
705 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
706 u32 peer_port = port_peerport(p_ptr);
707 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500708 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709
Julia Lawall4cec72c2008-01-08 23:48:20 -0800710 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700711 if (unlikely(!cb))
712 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700714 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700716 } else if ((msg_origport(msg) != peer_port) ||
717 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500719 dsz = msg_data_sz(msg);
720 if (unlikely(dsz &&
721 (++p_ptr->conn_unacked >=
722 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500724 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500726 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727 break;
728 }
729 case TIPC_DIRECT_MSG:{
730 tipc_msg_event cb = up_ptr->msg_cb;
731
Julia Lawall4cec72c2008-01-08 23:48:20 -0800732 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700733 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 goto reject;
735 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900736 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 msg_data_sz(msg), msg_importance(msg),
738 &orig);
739 break;
740 }
Allan Stephens96882432006-06-25 23:38:58 -0700741 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 case TIPC_NAMED_MSG:{
743 tipc_named_msg_event cb = up_ptr->named_msg_cb;
744
Julia Lawall4cec72c2008-01-08 23:48:20 -0800745 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700746 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 goto reject;
748 dseq.type = msg_nametype(msg);
749 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700750 dseq.upper = (message_type == TIPC_NAMED_MSG)
751 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900753 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 msg_data_sz(msg), msg_importance(msg),
755 &orig, &dseq);
756 break;
757 }
758 }
759 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400760 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 buf = next;
762 continue;
763err:
Allan Stephens96882432006-06-25 23:38:58 -0700764 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900765
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900767 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 up_ptr->conn_err_cb;
769 u32 peer_port = port_peerport(p_ptr);
770 u32 peer_node = port_peernode(p_ptr);
771
Julia Lawall4cec72c2008-01-08 23:48:20 -0800772 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700773 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700775 if ((msg_origport(msg) != peer_port) ||
776 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 break;
778 tipc_disconnect(dref);
779 skb_pull(buf, msg_hdr_sz(msg));
780 cb(usr_handle, dref, &buf, msg_data(msg),
781 msg_data_sz(msg), msg_errcode(msg));
782 break;
783 }
784 case TIPC_DIRECT_MSG:{
785 tipc_msg_err_event cb = up_ptr->err_cb;
786
Julia Lawall4cec72c2008-01-08 23:48:20 -0800787 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700788 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 break;
790 skb_pull(buf, msg_hdr_sz(msg));
791 cb(usr_handle, dref, &buf, msg_data(msg),
792 msg_data_sz(msg), msg_errcode(msg), &orig);
793 break;
794 }
Allan Stephens96882432006-06-25 23:38:58 -0700795 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900797 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 up_ptr->named_err_cb;
799
Julia Lawall4cec72c2008-01-08 23:48:20 -0800800 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700801 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 break;
803 dseq.type = msg_nametype(msg);
804 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700805 dseq.upper = (message_type == TIPC_NAMED_MSG)
806 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900808 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 msg_data_sz(msg), msg_errcode(msg), &dseq);
810 break;
811 }
812 }
813 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400814 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 buf = next;
816 continue;
817reject:
818 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
819 buf = next;
820 }
821}
822
823/*
824 * port_dispatcher(): Dispatcher for messages destinated
825 * to the tipc_port interface. Called with port locked.
826 */
827
828static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
829{
830 buf->next = NULL;
831 spin_lock_bh(&queue_lock);
832 if (msg_queue_head) {
833 msg_queue_tail->next = buf;
834 msg_queue_tail = buf;
835 } else {
836 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100837 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 }
839 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700840 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841}
842
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900843/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900845 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 */
847
848static void port_wakeup_sh(unsigned long ref)
849{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500850 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800852 tipc_continue_event cb = NULL;
853 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854
Per Liden4323add2006-01-18 00:38:21 +0100855 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 if (p_ptr) {
857 up_ptr = p_ptr->user_port;
858 if (up_ptr) {
859 cb = up_ptr->continue_event_cb;
860 uh = up_ptr->usr_handle;
861 }
Per Liden4323add2006-01-18 00:38:21 +0100862 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 }
864 if (cb)
865 cb(uh, ref);
866}
867
868
869static void port_wakeup(struct tipc_port *p_ptr)
870{
Per Liden4323add2006-01-18 00:38:21 +0100871 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872}
873
874void tipc_acknowledge(u32 ref, u32 ack)
875{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500876 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800877 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878
Per Liden4323add2006-01-18 00:38:21 +0100879 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 if (!p_ptr)
881 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500882 if (p_ptr->connected) {
883 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400884 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 }
Per Liden4323add2006-01-18 00:38:21 +0100886 tipc_port_unlock(p_ptr);
887 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888}
889
890/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000891 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 */
893
Allan Stephensb0c1e922010-12-31 18:59:22 +0000894int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900895 unsigned int importance,
896 tipc_msg_err_event error_cb,
897 tipc_named_msg_err_event named_error_cb,
898 tipc_conn_shutdown_event conn_error_cb,
899 tipc_msg_event msg_cb,
900 tipc_named_msg_event named_msg_cb,
901 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 tipc_continue_event continue_event_cb,/* May be zero */
903 u32 *portref)
904{
905 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500906 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700908 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700909 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700910 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 return -ENOMEM;
912 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500913 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700914 port_wakeup, importance);
915 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 kfree(up_ptr);
917 return -ENOMEM;
918 }
919
920 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500922 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 up_ptr->err_cb = error_cb;
924 up_ptr->named_err_cb = named_error_cb;
925 up_ptr->conn_err_cb = conn_error_cb;
926 up_ptr->msg_cb = msg_cb;
927 up_ptr->named_msg_cb = named_msg_cb;
928 up_ptr->conn_msg_cb = conn_msg_cb;
929 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500930 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100931 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700932 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933}
934
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935int tipc_portimportance(u32 ref, unsigned int *importance)
936{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500937 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900938
Per Liden4323add2006-01-18 00:38:21 +0100939 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 if (!p_ptr)
941 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500942 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800943 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700944 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945}
946
947int tipc_set_portimportance(u32 ref, unsigned int imp)
948{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500949 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950
951 if (imp > TIPC_CRITICAL_IMPORTANCE)
952 return -EINVAL;
953
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 msg_set_importance(&p_ptr->phdr, (u32)imp);
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
962
963int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
964{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500965 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 struct publication *publ;
967 u32 key;
968 int res = -EINVAL;
969
Per Liden4323add2006-01-18 00:38:21 +0100970 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800971 if (!p_ptr)
972 return -EINVAL;
973
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500974 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 goto exit;
976 if (seq->lower > seq->upper)
977 goto exit;
978 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
979 goto exit;
980 key = ref + p_ptr->pub_count + 1;
981 if (key == ref) {
982 res = -EADDRINUSE;
983 goto exit;
984 }
Per Liden4323add2006-01-18 00:38:21 +0100985 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500986 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 if (publ) {
988 list_add(&publ->pport_list, &p_ptr->publications);
989 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500990 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -0700991 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 }
993exit:
Per Liden4323add2006-01-18 00:38:21 +0100994 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 return res;
996}
997
998int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
999{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001000 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 struct publication *publ;
1002 struct publication *tpubl;
1003 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001004
Per Liden4323add2006-01-18 00:38:21 +01001005 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 if (!p_ptr)
1007 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001009 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001011 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001012 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001014 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001016 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 &p_ptr->publications, pport_list) {
1018 if (publ->scope != scope)
1019 continue;
1020 if (publ->type != seq->type)
1021 continue;
1022 if (publ->lower != seq->lower)
1023 continue;
1024 if (publ->upper != seq->upper)
1025 break;
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);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001028 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 break;
1030 }
1031 }
1032 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001033 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001034 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 return res;
1036}
1037
1038int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1039{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001040 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 struct tipc_msg *msg;
1042 int res = -EINVAL;
1043
Per Liden4323add2006-01-18 00:38:21 +01001044 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 if (!p_ptr)
1046 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001047 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 goto exit;
1049 if (!peer->ref)
1050 goto exit;
1051
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001052 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 msg_set_destnode(msg, peer->node);
1054 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001056 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001057 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058
1059 p_ptr->probing_interval = PROBING_INTERVAL;
1060 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001061 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1063
Allan Stephens0e659672010-12-31 18:59:32 +00001064 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001065 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001067 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068exit:
Per Liden4323add2006-01-18 00:38:21 +01001069 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001070 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 return res;
1072}
1073
Allan Stephens0c3141e2008-04-15 00:22:02 -07001074/**
1075 * tipc_disconnect_port - disconnect port from peer
1076 *
1077 * Port must be locked.
1078 */
1079
1080int tipc_disconnect_port(struct tipc_port *tp_ptr)
1081{
1082 int res;
1083
1084 if (tp_ptr->connected) {
1085 tp_ptr->connected = 0;
1086 /* let timer expire on it's own to avoid deadlock! */
1087 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001088 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001089 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001090 } else {
1091 res = -ENOTCONN;
1092 }
1093 return res;
1094}
1095
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096/*
1097 * tipc_disconnect(): Disconnect port form peer.
1098 * This is a node local operation.
1099 */
1100
1101int tipc_disconnect(u32 ref)
1102{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001103 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001104 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105
Per Liden4323add2006-01-18 00:38:21 +01001106 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 if (!p_ptr)
1108 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001109 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001110 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 return res;
1112}
1113
1114/*
1115 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1116 */
1117int tipc_shutdown(u32 ref)
1118{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001119 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001120 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121
Per Liden4323add2006-01-18 00:38:21 +01001122 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 if (!p_ptr)
1124 return -EINVAL;
1125
Allan Stephense244a912011-05-31 16:10:08 -04001126 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +01001127 tipc_port_unlock(p_ptr);
1128 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 return tipc_disconnect(ref);
1130}
1131
Allan Stephens9b641252011-11-09 13:29:18 -05001132/**
1133 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
1134 */
1135
1136int tipc_port_recv_msg(struct sk_buff *buf)
1137{
1138 struct tipc_port *p_ptr;
1139 struct tipc_msg *msg = buf_msg(buf);
1140 u32 destport = msg_destport(msg);
1141 u32 dsz = msg_data_sz(msg);
1142 u32 err;
1143
1144 /* forward unresolved named message */
1145 if (unlikely(!destport)) {
1146 tipc_net_route_msg(buf);
1147 return dsz;
1148 }
1149
1150 /* validate destination & pass to port, otherwise reject message */
1151 p_ptr = tipc_port_lock(destport);
1152 if (likely(p_ptr)) {
1153 if (likely(p_ptr->connected)) {
1154 if ((unlikely(msg_origport(msg) !=
1155 tipc_peer_port(p_ptr))) ||
1156 (unlikely(msg_orignode(msg) !=
1157 tipc_peer_node(p_ptr))) ||
1158 (unlikely(!msg_connected(msg)))) {
1159 err = TIPC_ERR_NO_PORT;
1160 tipc_port_unlock(p_ptr);
1161 goto reject;
1162 }
1163 }
1164 err = p_ptr->dispatcher(p_ptr, buf);
1165 tipc_port_unlock(p_ptr);
1166 if (likely(!err))
1167 return dsz;
1168 } else {
1169 err = TIPC_ERR_NO_PORT;
1170 }
1171reject:
1172 return tipc_reject_msg(buf, err);
1173}
1174
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175/*
Per Liden4323add2006-01-18 00:38:21 +01001176 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 * message for this node.
1178 */
1179
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001180static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001181 struct iovec const *msg_sect,
1182 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183{
1184 struct sk_buff *buf;
1185 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001186
Allan Stephens26896902011-04-21 10:42:07 -05001187 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 MAX_MSG_SIZE, !sender->user_port, &buf);
1189 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001190 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 return res;
1192}
1193
1194/**
1195 * tipc_send - send message sections on connection
1196 */
1197
Allan Stephens26896902011-04-21 10:42:07 -05001198int tipc_send(u32 ref, 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 u32 destnode;
1203 int res;
1204
Per Liden4323add2006-01-18 00:38:21 +01001205 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001206 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 return -EINVAL;
1208
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001209 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001210 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 destnode = port_peernode(p_ptr);
1212 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001213 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001214 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 else
Allan Stephens26896902011-04-21 10:42:07 -05001216 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1217 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218
1219 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001220 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001221 if (res > 0)
1222 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 return res;
1224 }
1225 }
1226 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001227 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001228 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 }
1230 return -ELINKCONG;
1231}
1232
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001233/**
Allan Stephens12bae472010-11-30 12:01:02 +00001234 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 */
1236
Allan Stephens12bae472010-11-30 12:01:02 +00001237int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001238 unsigned int num_sect, struct iovec const *msg_sect,
1239 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001241 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 struct tipc_msg *msg;
1243 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001244 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 int res;
1246
Per Liden4323add2006-01-18 00:38:21 +01001247 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001248 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 return -EINVAL;
1250
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001251 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001253 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 msg_set_nametype(msg, name->type);
1255 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001256 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001257 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258 msg_set_destnode(msg, destnode);
1259 msg_set_destport(msg, destport);
1260
Allan Stephensbc9f8142011-11-07 17:00:54 -05001261 if (likely(destport || destnode)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001263 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001264 msg_sect, total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001265 else
1266 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001267 num_sect, total_len,
1268 destnode);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001269 if (likely(res != -ELINKCONG)) {
1270 if (res > 0)
1271 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001273 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001275 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 }
1277 return -ELINKCONG;
1278 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001279 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001280 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281}
1282
1283/**
Allan Stephens12bae472010-11-30 12:01:02 +00001284 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 */
1286
Allan Stephens12bae472010-11-30 12:01:02 +00001287int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001288 unsigned int num_sect, struct iovec const *msg_sect,
1289 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001291 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 struct tipc_msg *msg;
1293 int res;
1294
Per Liden4323add2006-01-18 00:38:21 +01001295 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001296 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 return -EINVAL;
1298
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001299 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001301 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 msg_set_destnode(msg, dest->node);
1303 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001304 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001305
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 if (dest->node == tipc_own_addr)
Allan Stephens26896902011-04-21 10:42:07 -05001307 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1308 total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001309 else
1310 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001311 total_len, dest->node);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001312 if (likely(res != -ELINKCONG)) {
1313 if (res > 0)
1314 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001316 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001318 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 }
1320 return -ELINKCONG;
1321}
1322
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001323/**
Allan Stephens12bae472010-11-30 12:01:02 +00001324 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 */
1326
Allan Stephens12bae472010-11-30 12:01:02 +00001327int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1328 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001330 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 struct tipc_msg *msg;
1332 int res;
1333
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001334 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1335 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 return -EINVAL;
1337
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001338 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 msg_set_type(msg, TIPC_DIRECT_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 msg_set_destnode(msg, dest->node);
1341 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001342 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1343 msg_set_size(msg, BASIC_H_SIZE + dsz);
1344 if (skb_cow(buf, BASIC_H_SIZE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 return -ENOMEM;
1346
Allan Stephens741d9eb2011-05-31 15:03:18 -04001347 skb_push(buf, BASIC_H_SIZE);
1348 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001349
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001351 res = tipc_port_recv_msg(buf);
1352 else
1353 res = tipc_send_buf_fast(buf, dest->node);
1354 if (likely(res != -ELINKCONG)) {
1355 if (res > 0)
1356 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001358 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 if (port_unreliable(p_ptr))
1360 return dsz;
1361 return -ELINKCONG;
1362}
1363