blob: f1f6b33f761c5ef7c32e321ff7a44e7c36df8999 [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 Stephensd0e17fe2012-04-17 18:36:42 -040062static inline 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 Stephensd0e17fe2012-04-17 18:36:42 -040067static inline 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;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 INIT_LIST_HEAD(&p_ptr->wait_list);
225 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 p_ptr->dispatcher = dispatcher;
227 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800228 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 INIT_LIST_HEAD(&p_ptr->publications);
231 INIT_LIST_HEAD(&p_ptr->port_list);
Allan Stephensf21536d2012-04-17 18:22:49 -0400232
233 /*
234 * Must hold port list lock while initializing message header template
235 * to ensure a change to node's own network address doesn't result
236 * in template containing out-dated network address information
237 */
238
239 spin_lock_bh(&tipc_port_list_lock);
240 msg = &p_ptr->phdr;
241 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
242 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100244 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500245 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246}
247
248int tipc_deleteport(u32 ref)
249{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500250 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800251 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800253 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100254 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900255 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 return -EINVAL;
257
Per Liden4323add2006-01-18 00:38:21 +0100258 tipc_ref_discard(ref);
259 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260
261 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500262 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100264 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 }
Allan Stephense83504f2010-12-31 18:59:30 +0000266 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267
Per Liden4323add2006-01-18 00:38:21 +0100268 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269 list_del(&p_ptr->port_list);
270 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100271 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272 k_term_timer(&p_ptr->timer);
273 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100274 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700275 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276}
277
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500278static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500280 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281}
282
283int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
284{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500285 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900286
Per Liden4323add2006-01-18 00:38:21 +0100287 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288 if (!p_ptr)
289 return -EINVAL;
290 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800291 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700292 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293}
294
295int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
296{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500297 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298
Per Liden4323add2006-01-18 00:38:21 +0100299 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 if (!p_ptr)
301 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500302 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100303 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700304 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305}
306
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500307static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500309 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310}
311
312int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
313{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500314 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900315
Per Liden4323add2006-01-18 00:38:21 +0100316 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 if (!p_ptr)
318 return -EINVAL;
319 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800320 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700321 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322}
323
324int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
325{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500326 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900327
Per Liden4323add2006-01-18 00:38:21 +0100328 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 if (!p_ptr)
330 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500331 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100332 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700333 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334}
335
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900336/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400337 * port_build_proto_msg(): create connection protocol message for port
338 *
339 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400341static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
342 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343{
344 struct sk_buff *buf;
345 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900346
Allan Stephens741d9eb2011-05-31 15:03:18 -0400347 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 if (buf) {
349 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400350 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
351 port_peernode(p_ptr));
352 msg_set_destport(msg, port_peerport(p_ptr));
353 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 }
356 return buf;
357}
358
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359int tipc_reject_msg(struct sk_buff *buf, u32 err)
360{
361 struct tipc_msg *msg = buf_msg(buf);
362 struct sk_buff *rbuf;
363 struct tipc_msg *rmsg;
364 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400365 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400367 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400368 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369
370 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400371
372 if (WARN(!msg_isdata(msg),
373 "attempt to reject message with user=%u", msg_user(msg))) {
374 dump_stack();
375 goto exit;
376 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400377 if (msg_errcode(msg) || msg_dest_droppable(msg))
378 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400380 /*
381 * construct returned message by copying rejected message header and
382 * data (or subset), then updating header fields that need adjusting
383 */
384
385 hdr_sz = msg_hdr_sz(msg);
386 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
387
388 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400389 if (rbuf == NULL)
390 goto exit;
391
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400393 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
394
395 if (msg_connected(rmsg)) {
396 imp = msg_importance(rmsg);
397 if (imp < TIPC_CRITICAL_IMPORTANCE)
398 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700399 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400400 msg_set_non_seq(rmsg, 0);
401 msg_set_size(rmsg, rmsg_sz);
402 msg_set_errcode(rmsg, err);
403 msg_set_prevnode(rmsg, tipc_own_addr);
404 msg_swap_words(rmsg, 4, 5);
405 if (!msg_short(rmsg))
406 msg_swap_words(rmsg, 6, 7);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407
408 /* send self-abort message when rejecting on a connected port */
409 if (msg_connected(msg)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500410 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411
412 if (p_ptr) {
Allan Stephensdff10e92011-11-02 10:32:14 -0400413 struct sk_buff *abuf = NULL;
414
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500415 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100417 tipc_port_unlock(p_ptr);
Allan Stephensdff10e92011-11-02 10:32:14 -0400418 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 }
421
Allan Stephensacc631b2011-05-23 13:47:44 -0400422 /* send returned message & dispose of rejected message */
423
Allan Stephens017dac32011-05-24 13:20:09 -0400424 src_node = msg_prevnode(msg);
425 if (src_node == tipc_own_addr)
426 tipc_port_recv_msg(rbuf);
427 else
428 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400429exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400430 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 return data_sz;
432}
433
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500434int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100435 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500436 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437{
438 struct sk_buff *buf;
439 int res;
440
Allan Stephens26896902011-04-21 10:42:07 -0500441 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 !p_ptr->user_port, &buf);
443 if (!buf)
444 return res;
445
446 return tipc_reject_msg(buf, err);
447}
448
449static void port_timeout(unsigned long ref)
450{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500451 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800452 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453
Allan Stephens065fd172006-10-16 21:38:05 -0700454 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 return;
456
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500457 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700458 tipc_port_unlock(p_ptr);
459 return;
460 }
461
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 /* Last probe answered ? */
463 if (p_ptr->probing_state == PROBING) {
464 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
465 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400466 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 p_ptr->probing_state = PROBING;
468 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
469 }
Per Liden4323add2006-01-18 00:38:21 +0100470 tipc_port_unlock(p_ptr);
471 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472}
473
474
475static void port_handle_node_down(unsigned long ref)
476{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500477 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000478 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
480 if (!p_ptr)
481 return;
482 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100483 tipc_port_unlock(p_ptr);
484 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485}
486
487
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500488static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489{
Allan Stephense244a912011-05-31 16:10:08 -0400490 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491
Allan Stephense244a912011-05-31 16:10:08 -0400492 if (buf) {
493 struct tipc_msg *msg = buf_msg(buf);
494 msg_swap_words(msg, 4, 5);
495 msg_swap_words(msg, 6, 7);
496 }
497 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498}
499
500
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500501static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502{
Allan Stephense244a912011-05-31 16:10:08 -0400503 struct sk_buff *buf;
504 struct tipc_msg *msg;
505 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500507 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800508 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400509
510 buf = tipc_buf_acquire(BASIC_H_SIZE);
511 if (buf) {
512 msg = buf_msg(buf);
513 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
514 msg_set_hdr_sz(msg, BASIC_H_SIZE);
515 msg_set_size(msg, BASIC_H_SIZE);
516 imp = msg_importance(msg);
517 if (imp < TIPC_CRITICAL_IMPORTANCE)
518 msg_set_importance(msg, ++imp);
519 msg_set_errcode(msg, err);
520 }
521 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522}
523
Per Liden4323add2006-01-18 00:38:21 +0100524void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525{
526 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400527 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800528 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400529 u32 orignode = msg_orignode(msg);
530 u32 origport = msg_origport(msg);
531 u32 destport = msg_destport(msg);
532 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533
Allan Stephens1c1a5512011-06-01 15:08:10 -0400534 /* Validate connection */
535
536 p_ptr = tipc_port_lock(destport);
537 if (!p_ptr || !p_ptr->connected ||
538 (port_peernode(p_ptr) != orignode) ||
539 (port_peerport(p_ptr) != origport)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400540 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
541 if (r_buf) {
542 msg = buf_msg(r_buf);
543 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
544 BASIC_H_SIZE, orignode);
545 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
546 msg_set_origport(msg, destport);
547 msg_set_destport(msg, origport);
548 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400549 if (p_ptr)
550 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 goto exit;
552 }
553
Allan Stephens1c1a5512011-06-01 15:08:10 -0400554 /* Process protocol message sent by peer */
555
556 switch (msg_type(msg)) {
557 case CONN_ACK:
558 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
559 p_ptr->wakeup;
560 p_ptr->acked += msg_msgcnt(msg);
561 if (!tipc_port_congested(p_ptr)) {
562 p_ptr->congested = 0;
563 if (wakeable)
564 p_ptr->wakeup(p_ptr);
565 }
566 break;
567 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400568 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400569 break;
570 default:
571 /* CONN_PROBE_REPLY or unrecognized - no action required */
572 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 }
574 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400575 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576exit:
Per Liden4323add2006-01-18 00:38:21 +0100577 tipc_net_route_msg(r_buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400578 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579}
580
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500581static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584
585 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500588 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500590 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500592 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593 u32 dport = port_peerport(p_ptr);
594 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
597 tipc_zone(destnode), tipc_cluster(destnode),
598 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500599 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900600 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500601 p_ptr->conn_type,
602 p_ptr->conn_instance);
603 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 tipc_printf(buf, " bound to");
605 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 if (publ->lower == publ->upper)
607 tipc_printf(buf, " {%u,%u}", publ->type,
608 publ->lower);
609 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 }
613 }
614 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615}
616
617#define MAX_PORT_QUERY 32768
618
Per Liden4323add2006-01-18 00:38:21 +0100619struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620{
621 struct sk_buff *buf;
622 struct tlv_desc *rep_tlv;
623 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500624 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 int str_len;
626
Per Liden4323add2006-01-18 00:38:21 +0100627 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 if (!buf)
629 return NULL;
630 rep_tlv = (struct tlv_desc *)buf->data;
631
Per Liden4323add2006-01-18 00:38:21 +0100632 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
633 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500635 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500637 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 }
Per Liden4323add2006-01-18 00:38:21 +0100639 spin_unlock_bh(&tipc_port_list_lock);
640 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641
642 skb_put(buf, TLV_SPACE(str_len));
643 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
644
645 return buf;
646}
647
Per Liden4323add2006-01-18 00:38:21 +0100648void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500650 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 struct tipc_msg *msg;
652
Per Liden4323add2006-01-18 00:38:21 +0100653 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500655 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700656 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 msg_set_orignode(msg, tipc_own_addr);
658 }
Per Liden4323add2006-01-18 00:38:21 +0100659 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660}
661
662
663/*
664 * port_dispatcher_sigh(): Signal handler for messages destinated
665 * to the tipc_port interface.
666 */
667
668static void port_dispatcher_sigh(void *dummy)
669{
670 struct sk_buff *buf;
671
672 spin_lock_bh(&queue_lock);
673 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800674 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 spin_unlock_bh(&queue_lock);
676
677 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500678 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 struct user_port *up_ptr;
680 struct tipc_portid orig;
681 struct tipc_name_seq dseq;
682 void *usr_handle;
683 int connected;
684 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700685 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686
687 struct sk_buff *next = buf->next;
688 struct tipc_msg *msg = buf_msg(buf);
689 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900690
Allan Stephens96882432006-06-25 23:38:58 -0700691 message_type = msg_type(msg);
692 if (message_type > TIPC_DIRECT_MSG)
693 goto reject; /* Unsupported message type */
694
Per Liden4323add2006-01-18 00:38:21 +0100695 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700696 if (!p_ptr)
697 goto reject; /* Port deleted while msg in queue */
698
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 orig.ref = msg_origport(msg);
700 orig.node = msg_orignode(msg);
701 up_ptr = p_ptr->user_port;
702 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500703 connected = p_ptr->connected;
704 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705
706 if (unlikely(msg_errcode(msg)))
707 goto err;
708
Allan Stephens96882432006-06-25 23:38:58 -0700709 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900710
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 case TIPC_CONN_MSG:{
712 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
713 u32 peer_port = port_peerport(p_ptr);
714 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500715 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716
Julia Lawall4cec72c2008-01-08 23:48:20 -0800717 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700718 if (unlikely(!cb))
719 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700721 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700723 } else if ((msg_origport(msg) != peer_port) ||
724 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500726 dsz = msg_data_sz(msg);
727 if (unlikely(dsz &&
728 (++p_ptr->conn_unacked >=
729 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500731 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500733 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 break;
735 }
736 case TIPC_DIRECT_MSG:{
737 tipc_msg_event cb = up_ptr->msg_cb;
738
Julia Lawall4cec72c2008-01-08 23:48:20 -0800739 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700740 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 goto reject;
742 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900743 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 msg_data_sz(msg), msg_importance(msg),
745 &orig);
746 break;
747 }
Allan Stephens96882432006-06-25 23:38:58 -0700748 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 case TIPC_NAMED_MSG:{
750 tipc_named_msg_event cb = up_ptr->named_msg_cb;
751
Julia Lawall4cec72c2008-01-08 23:48:20 -0800752 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700753 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 goto reject;
755 dseq.type = msg_nametype(msg);
756 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700757 dseq.upper = (message_type == TIPC_NAMED_MSG)
758 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900760 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 msg_data_sz(msg), msg_importance(msg),
762 &orig, &dseq);
763 break;
764 }
765 }
766 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400767 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 buf = next;
769 continue;
770err:
Allan Stephens96882432006-06-25 23:38:58 -0700771 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900774 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 up_ptr->conn_err_cb;
776 u32 peer_port = port_peerport(p_ptr);
777 u32 peer_node = port_peernode(p_ptr);
778
Julia Lawall4cec72c2008-01-08 23:48:20 -0800779 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700780 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700782 if ((msg_origport(msg) != peer_port) ||
783 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 break;
785 tipc_disconnect(dref);
786 skb_pull(buf, msg_hdr_sz(msg));
787 cb(usr_handle, dref, &buf, msg_data(msg),
788 msg_data_sz(msg), msg_errcode(msg));
789 break;
790 }
791 case TIPC_DIRECT_MSG:{
792 tipc_msg_err_event cb = up_ptr->err_cb;
793
Julia Lawall4cec72c2008-01-08 23:48:20 -0800794 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700795 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 break;
797 skb_pull(buf, msg_hdr_sz(msg));
798 cb(usr_handle, dref, &buf, msg_data(msg),
799 msg_data_sz(msg), msg_errcode(msg), &orig);
800 break;
801 }
Allan Stephens96882432006-06-25 23:38:58 -0700802 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900804 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 up_ptr->named_err_cb;
806
Julia Lawall4cec72c2008-01-08 23:48:20 -0800807 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700808 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 break;
810 dseq.type = msg_nametype(msg);
811 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700812 dseq.upper = (message_type == TIPC_NAMED_MSG)
813 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900815 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 msg_data_sz(msg), msg_errcode(msg), &dseq);
817 break;
818 }
819 }
820 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400821 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 buf = next;
823 continue;
824reject:
825 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
826 buf = next;
827 }
828}
829
830/*
831 * port_dispatcher(): Dispatcher for messages destinated
832 * to the tipc_port interface. Called with port locked.
833 */
834
835static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
836{
837 buf->next = NULL;
838 spin_lock_bh(&queue_lock);
839 if (msg_queue_head) {
840 msg_queue_tail->next = buf;
841 msg_queue_tail = buf;
842 } else {
843 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100844 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 }
846 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700847 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848}
849
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900850/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900852 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 */
854
855static void port_wakeup_sh(unsigned long ref)
856{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500857 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800859 tipc_continue_event cb = NULL;
860 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861
Per Liden4323add2006-01-18 00:38:21 +0100862 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 if (p_ptr) {
864 up_ptr = p_ptr->user_port;
865 if (up_ptr) {
866 cb = up_ptr->continue_event_cb;
867 uh = up_ptr->usr_handle;
868 }
Per Liden4323add2006-01-18 00:38:21 +0100869 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 }
871 if (cb)
872 cb(uh, ref);
873}
874
875
876static void port_wakeup(struct tipc_port *p_ptr)
877{
Per Liden4323add2006-01-18 00:38:21 +0100878 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879}
880
881void tipc_acknowledge(u32 ref, u32 ack)
882{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500883 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800884 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885
Per Liden4323add2006-01-18 00:38:21 +0100886 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 if (!p_ptr)
888 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500889 if (p_ptr->connected) {
890 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400891 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 }
Per Liden4323add2006-01-18 00:38:21 +0100893 tipc_port_unlock(p_ptr);
894 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895}
896
897/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000898 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 */
900
Allan Stephensb0c1e922010-12-31 18:59:22 +0000901int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900902 unsigned int importance,
903 tipc_msg_err_event error_cb,
904 tipc_named_msg_err_event named_error_cb,
905 tipc_conn_shutdown_event conn_error_cb,
906 tipc_msg_event msg_cb,
907 tipc_named_msg_event named_msg_cb,
908 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 tipc_continue_event continue_event_cb,/* May be zero */
910 u32 *portref)
911{
912 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500913 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700915 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700916 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700917 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 return -ENOMEM;
919 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500920 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700921 port_wakeup, importance);
922 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 kfree(up_ptr);
924 return -ENOMEM;
925 }
926
927 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500929 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 up_ptr->err_cb = error_cb;
931 up_ptr->named_err_cb = named_error_cb;
932 up_ptr->conn_err_cb = conn_error_cb;
933 up_ptr->msg_cb = msg_cb;
934 up_ptr->named_msg_cb = named_msg_cb;
935 up_ptr->conn_msg_cb = conn_msg_cb;
936 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500937 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100938 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700939 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940}
941
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942int tipc_portimportance(u32 ref, unsigned int *importance)
943{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500944 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900945
Per Liden4323add2006-01-18 00:38:21 +0100946 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 if (!p_ptr)
948 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500949 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800950 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700951 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952}
953
954int tipc_set_portimportance(u32 ref, unsigned int imp)
955{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500956 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957
958 if (imp > TIPC_CRITICAL_IMPORTANCE)
959 return -EINVAL;
960
Per Liden4323add2006-01-18 00:38:21 +0100961 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962 if (!p_ptr)
963 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500964 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800965 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700966 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967}
968
969
970int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
971{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500972 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 struct publication *publ;
974 u32 key;
975 int res = -EINVAL;
976
Per Liden4323add2006-01-18 00:38:21 +0100977 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800978 if (!p_ptr)
979 return -EINVAL;
980
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500981 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 goto exit;
983 if (seq->lower > seq->upper)
984 goto exit;
985 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
986 goto exit;
987 key = ref + p_ptr->pub_count + 1;
988 if (key == ref) {
989 res = -EADDRINUSE;
990 goto exit;
991 }
Per Liden4323add2006-01-18 00:38:21 +0100992 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500993 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994 if (publ) {
995 list_add(&publ->pport_list, &p_ptr->publications);
996 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500997 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -0700998 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 }
1000exit:
Per Liden4323add2006-01-18 00:38:21 +01001001 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 return res;
1003}
1004
1005int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1006{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001007 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 struct publication *publ;
1009 struct publication *tpubl;
1010 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001011
Per Liden4323add2006-01-18 00:38:21 +01001012 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 if (!p_ptr)
1014 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 if (!seq) {
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) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001018 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001019 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001021 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 &p_ptr->publications, pport_list) {
1025 if (publ->scope != scope)
1026 continue;
1027 if (publ->type != seq->type)
1028 continue;
1029 if (publ->lower != seq->lower)
1030 continue;
1031 if (publ->upper != seq->upper)
1032 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001034 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001035 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 break;
1037 }
1038 }
1039 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001040 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001041 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 return res;
1043}
1044
1045int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1046{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001047 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 struct tipc_msg *msg;
1049 int res = -EINVAL;
1050
Per Liden4323add2006-01-18 00:38:21 +01001051 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 if (!p_ptr)
1053 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001054 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 goto exit;
1056 if (!peer->ref)
1057 goto exit;
1058
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001059 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 msg_set_destnode(msg, peer->node);
1061 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001063 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001064 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065
1066 p_ptr->probing_interval = PROBING_INTERVAL;
1067 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001068 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1070
Allan Stephens0e659672010-12-31 18:59:32 +00001071 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001072 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001074 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075exit:
Per Liden4323add2006-01-18 00:38:21 +01001076 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001077 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 return res;
1079}
1080
Allan Stephens0c3141e2008-04-15 00:22:02 -07001081/**
1082 * tipc_disconnect_port - disconnect port from peer
1083 *
1084 * Port must be locked.
1085 */
1086
1087int tipc_disconnect_port(struct tipc_port *tp_ptr)
1088{
1089 int res;
1090
1091 if (tp_ptr->connected) {
1092 tp_ptr->connected = 0;
1093 /* let timer expire on it's own to avoid deadlock! */
1094 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001095 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001096 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001097 } else {
1098 res = -ENOTCONN;
1099 }
1100 return res;
1101}
1102
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103/*
1104 * tipc_disconnect(): Disconnect port form peer.
1105 * This is a node local operation.
1106 */
1107
1108int tipc_disconnect(u32 ref)
1109{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001110 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001111 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112
Per Liden4323add2006-01-18 00:38:21 +01001113 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 if (!p_ptr)
1115 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001116 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001117 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 return res;
1119}
1120
1121/*
1122 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1123 */
1124int tipc_shutdown(u32 ref)
1125{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001126 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001127 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128
Per Liden4323add2006-01-18 00:38:21 +01001129 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 if (!p_ptr)
1131 return -EINVAL;
1132
Allan Stephense244a912011-05-31 16:10:08 -04001133 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +01001134 tipc_port_unlock(p_ptr);
1135 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 return tipc_disconnect(ref);
1137}
1138
Allan Stephens9b641252011-11-09 13:29:18 -05001139/**
1140 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
1141 */
1142
1143int tipc_port_recv_msg(struct sk_buff *buf)
1144{
1145 struct tipc_port *p_ptr;
1146 struct tipc_msg *msg = buf_msg(buf);
1147 u32 destport = msg_destport(msg);
1148 u32 dsz = msg_data_sz(msg);
1149 u32 err;
1150
1151 /* forward unresolved named message */
1152 if (unlikely(!destport)) {
1153 tipc_net_route_msg(buf);
1154 return dsz;
1155 }
1156
1157 /* validate destination & pass to port, otherwise reject message */
1158 p_ptr = tipc_port_lock(destport);
1159 if (likely(p_ptr)) {
1160 if (likely(p_ptr->connected)) {
1161 if ((unlikely(msg_origport(msg) !=
Allan Stephensd0e17fe2012-04-17 18:36:42 -04001162 port_peerport(p_ptr))) ||
Allan Stephens9b641252011-11-09 13:29:18 -05001163 (unlikely(msg_orignode(msg) !=
Allan Stephensd0e17fe2012-04-17 18:36:42 -04001164 port_peernode(p_ptr))) ||
Allan Stephens9b641252011-11-09 13:29:18 -05001165 (unlikely(!msg_connected(msg)))) {
1166 err = TIPC_ERR_NO_PORT;
1167 tipc_port_unlock(p_ptr);
1168 goto reject;
1169 }
1170 }
1171 err = p_ptr->dispatcher(p_ptr, buf);
1172 tipc_port_unlock(p_ptr);
1173 if (likely(!err))
1174 return dsz;
1175 } else {
1176 err = TIPC_ERR_NO_PORT;
1177 }
1178reject:
1179 return tipc_reject_msg(buf, err);
1180}
1181
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182/*
Per Liden4323add2006-01-18 00:38:21 +01001183 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 * message for this node.
1185 */
1186
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001187static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001188 struct iovec const *msg_sect,
1189 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190{
1191 struct sk_buff *buf;
1192 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001193
Allan Stephens26896902011-04-21 10:42:07 -05001194 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 MAX_MSG_SIZE, !sender->user_port, &buf);
1196 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001197 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 return res;
1199}
1200
1201/**
1202 * tipc_send - send message sections on connection
1203 */
1204
Allan Stephens26896902011-04-21 10:42:07 -05001205int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1206 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001208 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 u32 destnode;
1210 int res;
1211
Per Liden4323add2006-01-18 00:38:21 +01001212 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001213 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 return -EINVAL;
1215
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001216 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001217 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 destnode = port_peernode(p_ptr);
1219 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001220 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001221 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 else
Allan Stephens26896902011-04-21 10:42:07 -05001223 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1224 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225
1226 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001227 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001228 if (res > 0)
1229 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 return res;
1231 }
1232 }
1233 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001234 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001235 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 }
1237 return -ELINKCONG;
1238}
1239
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001240/**
Allan Stephens12bae472010-11-30 12:01:02 +00001241 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 */
1243
Allan Stephens12bae472010-11-30 12:01:02 +00001244int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001245 unsigned int num_sect, struct iovec const *msg_sect,
1246 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001248 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 struct tipc_msg *msg;
1250 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001251 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 int res;
1253
Per Liden4323add2006-01-18 00:38:21 +01001254 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001255 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 return -EINVAL;
1257
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001258 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001260 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 msg_set_nametype(msg, name->type);
1262 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001263 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001264 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265 msg_set_destnode(msg, destnode);
1266 msg_set_destport(msg, destport);
1267
Allan Stephensbc9f8142011-11-07 17:00:54 -05001268 if (likely(destport || destnode)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001270 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001271 msg_sect, total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001272 else
1273 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001274 num_sect, total_len,
1275 destnode);
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 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001286 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001287 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288}
1289
1290/**
Allan Stephens12bae472010-11-30 12:01:02 +00001291 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 */
1293
Allan Stephens12bae472010-11-30 12:01:02 +00001294int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001295 unsigned int num_sect, struct iovec const *msg_sect,
1296 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001298 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001299 struct tipc_msg *msg;
1300 int res;
1301
Per Liden4323add2006-01-18 00:38:21 +01001302 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001303 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 return -EINVAL;
1305
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001306 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001308 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 msg_set_destnode(msg, dest->node);
1310 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001311 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001312
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 if (dest->node == tipc_own_addr)
Allan Stephens26896902011-04-21 10:42:07 -05001314 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1315 total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001316 else
1317 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001318 total_len, dest->node);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001319 if (likely(res != -ELINKCONG)) {
1320 if (res > 0)
1321 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001323 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001325 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 }
1327 return -ELINKCONG;
1328}
1329
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001330/**
Allan Stephens12bae472010-11-30 12:01:02 +00001331 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 */
1333
Allan Stephens12bae472010-11-30 12:01:02 +00001334int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1335 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001337 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 struct tipc_msg *msg;
1339 int res;
1340
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001341 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1342 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 return -EINVAL;
1344
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001345 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 msg_set_type(msg, TIPC_DIRECT_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 msg_set_destnode(msg, dest->node);
1348 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001349 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1350 msg_set_size(msg, BASIC_H_SIZE + dsz);
1351 if (skb_cow(buf, BASIC_H_SIZE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 return -ENOMEM;
1353
Allan Stephens741d9eb2011-05-31 15:03:18 -04001354 skb_push(buf, BASIC_H_SIZE);
1355 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001356
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001358 res = tipc_port_recv_msg(buf);
1359 else
1360 res = tipc_send_buf_fast(buf, dest->node);
1361 if (likely(res != -ELINKCONG)) {
1362 if (res > 0)
1363 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001365 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 if (port_unreliable(p_ptr))
1367 return dsz;
1368 return -ELINKCONG;
1369}
1370