blob: ac64037514f3230d3ad04b56a7b90f96ec723121 [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,
Per Lidenb97bf3f2006-01-02 19:04:38 +010077 u32 num_sect, struct iovec const *msg_sect)
78{
79 struct tipc_msg *hdr;
80 struct sk_buff *buf;
81 struct sk_buff *ibuf = NULL;
82 struct port_list dports = {0, NULL, };
Allan Stephens23dd4cc2011-01-07 11:43:40 -050083 struct tipc_port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010084 int ext_targets;
85 int res;
86
87 if (unlikely(!oport))
88 return -EINVAL;
89
90 /* Create multicast message */
91
Allan Stephens23dd4cc2011-01-07 11:43:40 -050092 hdr = &oport->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +010093 msg_set_type(hdr, TIPC_MCAST_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -040094 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
Allan Stephens7462b9e2011-04-18 10:08:22 -040095 msg_set_destport(hdr, 0);
96 msg_set_destnode(hdr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 msg_set_nametype(hdr, seq->type);
98 msg_set_namelower(hdr, seq->lower);
99 msg_set_nameupper(hdr, seq->upper);
100 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000101 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102 !oport->user_port, &buf);
103 if (unlikely(!buf))
104 return res;
105
106 /* Figure out where to send multicast message */
107
Per Liden4323add2006-01-18 00:38:21 +0100108 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
109 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900110
111 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112
113 if (ext_targets) {
114 if (dports.count != 0) {
115 ibuf = skb_copy(buf, GFP_ATOMIC);
116 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100117 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 buf_discard(buf);
119 return -ENOMEM;
120 }
121 }
Per Liden4323add2006-01-18 00:38:21 +0100122 res = tipc_bclink_send_msg(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000123 if ((res < 0) && (dports.count != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 buf_discard(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 } else {
126 ibuf = buf;
127 }
128
129 if (res >= 0) {
130 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100131 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 } else {
Per Liden4323add2006-01-18 00:38:21 +0100133 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 }
135 return res;
136}
137
138/**
Per Liden4323add2006-01-18 00:38:21 +0100139 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900140 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 * If there is no port list, perform a lookup to create one
142 */
143
Per Liden4323add2006-01-18 00:38:21 +0100144void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145{
Allan Stephens0e659672010-12-31 18:59:32 +0000146 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 struct port_list dports = {0, NULL, };
148 struct port_list *item = dp;
149 int cnt = 0;
150
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 msg = buf_msg(buf);
152
153 /* Create destination port list, if one wasn't supplied */
154
155 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100156 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 msg_namelower(msg),
158 msg_nameupper(msg),
159 TIPC_CLUSTER_SCOPE,
160 &dports);
161 item = dp = &dports;
162 }
163
164 /* Deliver a copy of message to each destination port */
165
166 if (dp->count != 0) {
167 if (dp->count == 1) {
168 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100169 tipc_port_recv_msg(buf);
170 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 return;
172 }
173 for (; cnt < dp->count; cnt++) {
174 int index = cnt % PLSIZE;
175 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
176
177 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700178 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 goto exit;
180 }
Allan Stephensa0168922010-12-31 18:59:35 +0000181 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000183 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100184 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 }
186 }
187exit:
188 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100189 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190}
191
192/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700193 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900194 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700195 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 */
197
Allan Stephens0ea52242008-07-14 22:42:19 -0700198struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
200 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700201 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500203 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 struct tipc_msg *msg;
205 u32 ref;
206
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700207 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700208 if (!p_ptr) {
209 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700210 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500212 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700214 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700216 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 }
218
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500219 p_ptr->usr_handle = usr_handle;
220 p_ptr->max_pkt = MAX_PKT_DEFAULT;
221 p_ptr->ref = ref;
222 msg = &p_ptr->phdr;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000223 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 INIT_LIST_HEAD(&p_ptr->wait_list);
226 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 p_ptr->dispatcher = dispatcher;
228 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800229 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100231 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 INIT_LIST_HEAD(&p_ptr->publications);
233 INIT_LIST_HEAD(&p_ptr->port_list);
234 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100235 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500236 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237}
238
239int tipc_deleteport(u32 ref)
240{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500241 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800242 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800244 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100245 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900246 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 return -EINVAL;
248
Per Liden4323add2006-01-18 00:38:21 +0100249 tipc_ref_discard(ref);
250 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251
252 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500253 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100255 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 }
Allan Stephense83504f2010-12-31 18:59:30 +0000257 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258
Per Liden4323add2006-01-18 00:38:21 +0100259 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 list_del(&p_ptr->port_list);
261 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100262 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 k_term_timer(&p_ptr->timer);
264 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100265 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700266 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267}
268
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500269static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500271 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272}
273
274int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
275{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500276 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900277
Per Liden4323add2006-01-18 00:38:21 +0100278 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279 if (!p_ptr)
280 return -EINVAL;
281 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800282 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700283 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284}
285
286int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
287{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500288 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900289
Per Liden4323add2006-01-18 00:38:21 +0100290 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 if (!p_ptr)
292 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500293 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100294 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700295 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296}
297
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500298static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500300 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301}
302
303int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
304{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500305 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900306
Per Liden4323add2006-01-18 00:38:21 +0100307 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 if (!p_ptr)
309 return -EINVAL;
310 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800311 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700312 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313}
314
315int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
316{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500317 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900318
Per Liden4323add2006-01-18 00:38:21 +0100319 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 if (!p_ptr)
321 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500322 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100323 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700324 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325}
326
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900327/*
328 * port_build_proto_msg(): build a port level protocol
329 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 * tipc_port lock on.
331 */
332static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
333 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900334 u32 usr, u32 type, u32 err,
Allan Stephens741de3e2011-01-25 13:33:31 -0500335 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
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000340 buf = tipc_buf_acquire(LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 if (buf) {
342 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000343 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700344 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 msg_set_destport(msg, destport);
346 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 msg_set_orignode(msg, orignode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 }
350 return buf;
351}
352
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353int tipc_reject_msg(struct sk_buff *buf, u32 err)
354{
355 struct tipc_msg *msg = buf_msg(buf);
356 struct sk_buff *rbuf;
357 struct tipc_msg *rmsg;
358 int hdr_sz;
359 u32 imp = msg_importance(msg);
360 u32 data_sz = msg_data_sz(msg);
361
362 if (data_sz > MAX_REJECT_SIZE)
363 data_sz = MAX_REJECT_SIZE;
364 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
365 imp++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366
367 /* discard rejected message if it shouldn't be returned to sender */
368 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
369 buf_discard(buf);
370 return data_sz;
371 }
372
373 /* construct rejected message */
374 if (msg_mcast(msg))
375 hdr_sz = MCAST_H_SIZE;
376 else
377 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000378 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 if (rbuf == NULL) {
380 buf_discard(buf);
381 return data_sz;
382 }
383 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000384 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700385 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700388 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700390 /* leave name type & instance as zeroes */
391 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700393 msg_set_nametype(rmsg, msg_nametype(msg));
394 msg_set_nameinst(rmsg, msg_nameinst(msg));
395 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900396 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300397 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398
399 /* send self-abort message when rejecting on a connected port */
400 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800401 struct sk_buff *abuf = NULL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500402 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403
404 if (p_ptr) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500405 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100407 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 }
Per Liden4323add2006-01-18 00:38:21 +0100409 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 }
411
412 /* send rejected message */
413 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100414 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 return data_sz;
416}
417
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500418int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100419 struct iovec const *msg_sect, u32 num_sect,
420 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421{
422 struct sk_buff *buf;
423 int res;
424
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000425 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 !p_ptr->user_port, &buf);
427 if (!buf)
428 return res;
429
430 return tipc_reject_msg(buf, err);
431}
432
433static void port_timeout(unsigned long ref)
434{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500435 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800436 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437
Allan Stephens065fd172006-10-16 21:38:05 -0700438 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 return;
440
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500441 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700442 tipc_port_unlock(p_ptr);
443 return;
444 }
445
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 /* Last probe answered ? */
447 if (p_ptr->probing_state == PROBING) {
448 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
449 } else {
450 buf = port_build_proto_msg(port_peerport(p_ptr),
451 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500452 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 tipc_own_addr,
454 CONN_MANAGER,
455 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900456 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 p_ptr->probing_state = PROBING;
459 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
460 }
Per Liden4323add2006-01-18 00:38:21 +0100461 tipc_port_unlock(p_ptr);
462 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463}
464
465
466static void port_handle_node_down(unsigned long ref)
467{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500468 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000469 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470
471 if (!p_ptr)
472 return;
473 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100474 tipc_port_unlock(p_ptr);
475 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476}
477
478
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500479static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500481 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500483 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800484 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 if (imp < TIPC_CRITICAL_IMPORTANCE)
486 imp++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500487 return port_build_proto_msg(p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 tipc_own_addr,
489 port_peerport(p_ptr),
490 port_peernode(p_ptr),
491 imp,
492 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900493 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 0);
495}
496
497
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500498static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500500 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500502 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800503 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 if (imp < TIPC_CRITICAL_IMPORTANCE)
505 imp++;
506 return port_build_proto_msg(port_peerport(p_ptr),
507 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500508 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 tipc_own_addr,
510 imp,
511 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900512 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 0);
514}
515
Per Liden4323add2006-01-18 00:38:21 +0100516void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517{
518 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500519 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800521 struct sk_buff *r_buf = NULL;
522 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 if (!p_ptr) {
525 err = TIPC_ERR_NO_PORT;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500526 } else if (p_ptr->connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000527 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
528 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000530 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900531 int wakeup = tipc_port_congested(p_ptr) &&
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500532 p_ptr->congested &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 p_ptr->wakeup;
534 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100535 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500537 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 if (!wakeup)
539 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500540 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 goto exit;
542 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500543 } else if (p_ptr->published) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 err = TIPC_ERR_NO_PORT;
545 }
546 if (err) {
547 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900548 msg_orignode(msg),
549 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800551 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 TIPC_CONN_MSG,
553 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 0);
555 goto exit;
556 }
557
558 /* All is fine */
559 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 r_buf = port_build_proto_msg(msg_origport(msg),
561 msg_orignode(msg),
562 msg_destport(msg),
563 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 CONN_MANAGER,
565 CONN_PROBE_REPLY,
566 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 0);
568 }
569 p_ptr->probing_state = CONFIRMED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570exit:
571 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100572 tipc_port_unlock(p_ptr);
573 tipc_net_route_msg(r_buf);
574 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 buf_discard(buf);
576}
577
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500578static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900580 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581
582 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500585 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500587 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500589 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 u32 dport = port_peerport(p_ptr);
591 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
594 tipc_zone(destnode), tipc_cluster(destnode),
595 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500596 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500598 p_ptr->conn_type,
599 p_ptr->conn_instance);
600 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900601 tipc_printf(buf, " bound to");
602 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 if (publ->lower == publ->upper)
604 tipc_printf(buf, " {%u,%u}", publ->type,
605 publ->lower);
606 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900607 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900609 }
610 }
611 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612}
613
614#define MAX_PORT_QUERY 32768
615
Per Liden4323add2006-01-18 00:38:21 +0100616struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617{
618 struct sk_buff *buf;
619 struct tlv_desc *rep_tlv;
620 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500621 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 int str_len;
623
Per Liden4323add2006-01-18 00:38:21 +0100624 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 if (!buf)
626 return NULL;
627 rep_tlv = (struct tlv_desc *)buf->data;
628
Per Liden4323add2006-01-18 00:38:21 +0100629 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
630 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500632 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500634 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 }
Per Liden4323add2006-01-18 00:38:21 +0100636 spin_unlock_bh(&tipc_port_list_lock);
637 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638
639 skb_put(buf, TLV_SPACE(str_len));
640 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
641
642 return buf;
643}
644
Per Liden4323add2006-01-18 00:38:21 +0100645void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500647 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 struct tipc_msg *msg;
649
Per Liden4323add2006-01-18 00:38:21 +0100650 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500652 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 if (msg_orignode(msg) == tipc_own_addr)
654 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700655 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 msg_set_orignode(msg, tipc_own_addr);
657 }
Per Liden4323add2006-01-18 00:38:21 +0100658 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659}
660
661
662/*
663 * port_dispatcher_sigh(): Signal handler for messages destinated
664 * to the tipc_port interface.
665 */
666
667static void port_dispatcher_sigh(void *dummy)
668{
669 struct sk_buff *buf;
670
671 spin_lock_bh(&queue_lock);
672 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800673 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 spin_unlock_bh(&queue_lock);
675
676 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500677 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 struct user_port *up_ptr;
679 struct tipc_portid orig;
680 struct tipc_name_seq dseq;
681 void *usr_handle;
682 int connected;
683 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700684 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685
686 struct sk_buff *next = buf->next;
687 struct tipc_msg *msg = buf_msg(buf);
688 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900689
Allan Stephens96882432006-06-25 23:38:58 -0700690 message_type = msg_type(msg);
691 if (message_type > TIPC_DIRECT_MSG)
692 goto reject; /* Unsupported message type */
693
Per Liden4323add2006-01-18 00:38:21 +0100694 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700695 if (!p_ptr)
696 goto reject; /* Port deleted while msg in queue */
697
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698 orig.ref = msg_origport(msg);
699 orig.node = msg_orignode(msg);
700 up_ptr = p_ptr->user_port;
701 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500702 connected = p_ptr->connected;
703 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704
705 if (unlikely(msg_errcode(msg)))
706 goto err;
707
Allan Stephens96882432006-06-25 23:38:58 -0700708 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900709
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 case TIPC_CONN_MSG:{
711 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
712 u32 peer_port = port_peerport(p_ptr);
713 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500714 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715
Julia Lawall4cec72c2008-01-08 23:48:20 -0800716 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700717 if (unlikely(!cb))
718 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700720 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700722 } else if ((msg_origport(msg) != peer_port) ||
723 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500725 dsz = msg_data_sz(msg);
726 if (unlikely(dsz &&
727 (++p_ptr->conn_unacked >=
728 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900729 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500730 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500732 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 break;
734 }
735 case TIPC_DIRECT_MSG:{
736 tipc_msg_event cb = up_ptr->msg_cb;
737
Julia Lawall4cec72c2008-01-08 23:48:20 -0800738 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700739 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 goto reject;
741 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900742 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 msg_data_sz(msg), msg_importance(msg),
744 &orig);
745 break;
746 }
Allan Stephens96882432006-06-25 23:38:58 -0700747 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 case TIPC_NAMED_MSG:{
749 tipc_named_msg_event cb = up_ptr->named_msg_cb;
750
Julia Lawall4cec72c2008-01-08 23:48:20 -0800751 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700752 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 goto reject;
754 dseq.type = msg_nametype(msg);
755 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700756 dseq.upper = (message_type == TIPC_NAMED_MSG)
757 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900759 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 msg_data_sz(msg), msg_importance(msg),
761 &orig, &dseq);
762 break;
763 }
764 }
765 if (buf)
766 buf_discard(buf);
767 buf = next;
768 continue;
769err:
Allan Stephens96882432006-06-25 23:38:58 -0700770 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900771
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900773 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 up_ptr->conn_err_cb;
775 u32 peer_port = port_peerport(p_ptr);
776 u32 peer_node = port_peernode(p_ptr);
777
Julia Lawall4cec72c2008-01-08 23:48:20 -0800778 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700779 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700781 if ((msg_origport(msg) != peer_port) ||
782 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 break;
784 tipc_disconnect(dref);
785 skb_pull(buf, msg_hdr_sz(msg));
786 cb(usr_handle, dref, &buf, msg_data(msg),
787 msg_data_sz(msg), msg_errcode(msg));
788 break;
789 }
790 case TIPC_DIRECT_MSG:{
791 tipc_msg_err_event cb = up_ptr->err_cb;
792
Julia Lawall4cec72c2008-01-08 23:48:20 -0800793 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700794 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 break;
796 skb_pull(buf, msg_hdr_sz(msg));
797 cb(usr_handle, dref, &buf, msg_data(msg),
798 msg_data_sz(msg), msg_errcode(msg), &orig);
799 break;
800 }
Allan Stephens96882432006-06-25 23:38:58 -0700801 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900803 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 up_ptr->named_err_cb;
805
Julia Lawall4cec72c2008-01-08 23:48:20 -0800806 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700807 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 break;
809 dseq.type = msg_nametype(msg);
810 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700811 dseq.upper = (message_type == TIPC_NAMED_MSG)
812 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900814 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 msg_data_sz(msg), msg_errcode(msg), &dseq);
816 break;
817 }
818 }
819 if (buf)
820 buf_discard(buf);
821 buf = next;
822 continue;
823reject:
824 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
825 buf = next;
826 }
827}
828
829/*
830 * port_dispatcher(): Dispatcher for messages destinated
831 * to the tipc_port interface. Called with port locked.
832 */
833
834static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
835{
836 buf->next = NULL;
837 spin_lock_bh(&queue_lock);
838 if (msg_queue_head) {
839 msg_queue_tail->next = buf;
840 msg_queue_tail = buf;
841 } else {
842 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100843 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844 }
845 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700846 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847}
848
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900849/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900851 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852 */
853
854static void port_wakeup_sh(unsigned long ref)
855{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500856 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800858 tipc_continue_event cb = NULL;
859 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860
Per Liden4323add2006-01-18 00:38:21 +0100861 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 if (p_ptr) {
863 up_ptr = p_ptr->user_port;
864 if (up_ptr) {
865 cb = up_ptr->continue_event_cb;
866 uh = up_ptr->usr_handle;
867 }
Per Liden4323add2006-01-18 00:38:21 +0100868 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 }
870 if (cb)
871 cb(uh, ref);
872}
873
874
875static void port_wakeup(struct tipc_port *p_ptr)
876{
Per Liden4323add2006-01-18 00:38:21 +0100877 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878}
879
880void tipc_acknowledge(u32 ref, u32 ack)
881{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500882 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800883 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884
Per Liden4323add2006-01-18 00:38:21 +0100885 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 if (!p_ptr)
887 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500888 if (p_ptr->connected) {
889 p_ptr->conn_unacked -= ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 buf = port_build_proto_msg(port_peerport(p_ptr),
891 port_peernode(p_ptr),
892 ref,
893 tipc_own_addr,
894 CONN_MANAGER,
895 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900896 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 ack);
898 }
Per Liden4323add2006-01-18 00:38:21 +0100899 tipc_port_unlock(p_ptr);
900 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901}
902
903/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000904 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 */
906
Allan Stephensb0c1e922010-12-31 18:59:22 +0000907int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900908 unsigned int importance,
909 tipc_msg_err_event error_cb,
910 tipc_named_msg_err_event named_error_cb,
911 tipc_conn_shutdown_event conn_error_cb,
912 tipc_msg_event msg_cb,
913 tipc_named_msg_event named_msg_cb,
914 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 tipc_continue_event continue_event_cb,/* May be zero */
916 u32 *portref)
917{
918 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500919 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700921 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700922 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700923 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 return -ENOMEM;
925 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500926 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700927 port_wakeup, importance);
928 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929 kfree(up_ptr);
930 return -ENOMEM;
931 }
932
933 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500935 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 up_ptr->err_cb = error_cb;
937 up_ptr->named_err_cb = named_error_cb;
938 up_ptr->conn_err_cb = conn_error_cb;
939 up_ptr->msg_cb = msg_cb;
940 up_ptr->named_msg_cb = named_msg_cb;
941 up_ptr->conn_msg_cb = conn_msg_cb;
942 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500943 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100944 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700945 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946}
947
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948int tipc_portimportance(u32 ref, unsigned int *importance)
949{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500950 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900951
Per Liden4323add2006-01-18 00:38:21 +0100952 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953 if (!p_ptr)
954 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500955 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800956 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700957 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958}
959
960int tipc_set_portimportance(u32 ref, unsigned int imp)
961{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500962 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963
964 if (imp > TIPC_CRITICAL_IMPORTANCE)
965 return -EINVAL;
966
Per Liden4323add2006-01-18 00:38:21 +0100967 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 if (!p_ptr)
969 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500970 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800971 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700972 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973}
974
975
976int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
977{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500978 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979 struct publication *publ;
980 u32 key;
981 int res = -EINVAL;
982
Per Liden4323add2006-01-18 00:38:21 +0100983 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800984 if (!p_ptr)
985 return -EINVAL;
986
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500987 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 goto exit;
989 if (seq->lower > seq->upper)
990 goto exit;
991 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
992 goto exit;
993 key = ref + p_ptr->pub_count + 1;
994 if (key == ref) {
995 res = -EADDRINUSE;
996 goto exit;
997 }
Per Liden4323add2006-01-18 00:38:21 +0100998 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500999 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 if (publ) {
1001 list_add(&publ->pport_list, &p_ptr->publications);
1002 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001003 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001004 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 }
1006exit:
Per Liden4323add2006-01-18 00:38:21 +01001007 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 return res;
1009}
1010
1011int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1012{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001013 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 struct publication *publ;
1015 struct publication *tpubl;
1016 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001017
Per Liden4323add2006-01-18 00:38:21 +01001018 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 if (!p_ptr)
1020 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001022 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001025 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001027 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 &p_ptr->publications, pport_list) {
1031 if (publ->scope != scope)
1032 continue;
1033 if (publ->type != seq->type)
1034 continue;
1035 if (publ->lower != seq->lower)
1036 continue;
1037 if (publ->upper != seq->upper)
1038 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001039 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001040 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001041 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 break;
1043 }
1044 }
1045 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001046 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001047 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 return res;
1049}
1050
1051int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1052{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001053 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 struct tipc_msg *msg;
1055 int res = -EINVAL;
1056
Per Liden4323add2006-01-18 00:38:21 +01001057 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 if (!p_ptr)
1059 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001060 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 goto exit;
1062 if (!peer->ref)
1063 goto exit;
1064
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001065 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 msg_set_destnode(msg, peer->node);
1067 msg_set_destport(msg, peer->ref);
1068 msg_set_orignode(msg, tipc_own_addr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001069 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001071 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001072 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073
1074 p_ptr->probing_interval = PROBING_INTERVAL;
1075 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001076 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1078
Allan Stephens0e659672010-12-31 18:59:32 +00001079 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001080 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001082 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083exit:
Per Liden4323add2006-01-18 00:38:21 +01001084 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001085 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 return res;
1087}
1088
Allan Stephens0c3141e2008-04-15 00:22:02 -07001089/**
1090 * tipc_disconnect_port - disconnect port from peer
1091 *
1092 * Port must be locked.
1093 */
1094
1095int tipc_disconnect_port(struct tipc_port *tp_ptr)
1096{
1097 int res;
1098
1099 if (tp_ptr->connected) {
1100 tp_ptr->connected = 0;
1101 /* let timer expire on it's own to avoid deadlock! */
1102 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001103 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001104 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001105 } else {
1106 res = -ENOTCONN;
1107 }
1108 return res;
1109}
1110
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111/*
1112 * tipc_disconnect(): Disconnect port form peer.
1113 * This is a node local operation.
1114 */
1115
1116int tipc_disconnect(u32 ref)
1117{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001118 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001119 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120
Per Liden4323add2006-01-18 00:38:21 +01001121 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 if (!p_ptr)
1123 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001124 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001125 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 return res;
1127}
1128
1129/*
1130 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1131 */
1132int tipc_shutdown(u32 ref)
1133{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001134 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001135 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136
Per Liden4323add2006-01-18 00:38:21 +01001137 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 if (!p_ptr)
1139 return -EINVAL;
1140
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001141 if (p_ptr->connected) {
1142 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 if (imp < TIPC_CRITICAL_IMPORTANCE)
1144 imp++;
1145 buf = port_build_proto_msg(port_peerport(p_ptr),
1146 port_peernode(p_ptr),
1147 ref,
1148 tipc_own_addr,
1149 imp,
1150 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001151 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 0);
1153 }
Per Liden4323add2006-01-18 00:38:21 +01001154 tipc_port_unlock(p_ptr);
1155 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 return tipc_disconnect(ref);
1157}
1158
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159/*
Per Liden4323add2006-01-18 00:38:21 +01001160 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 * message for this node.
1162 */
1163
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001164static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001165 struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166{
1167 struct sk_buff *buf;
1168 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001169
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001170 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 MAX_MSG_SIZE, !sender->user_port, &buf);
1172 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001173 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 return res;
1175}
1176
1177/**
1178 * tipc_send - send message sections on connection
1179 */
1180
1181int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1182{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001183 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 u32 destnode;
1185 int res;
1186
Per Liden4323add2006-01-18 00:38:21 +01001187 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001188 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 return -EINVAL;
1190
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001191 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001192 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 destnode = port_peernode(p_ptr);
1194 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001195 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1196 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 else
Per Liden4323add2006-01-18 00:38:21 +01001198 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199
1200 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001201 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001202 if (res > 0)
1203 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 return res;
1205 }
1206 }
1207 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001208 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001210 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 }
1212 return -ELINKCONG;
1213}
1214
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001215/**
Allan Stephens12bae472010-11-30 12:01:02 +00001216 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 */
1218
Allan Stephens12bae472010-11-30 12:01:02 +00001219int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1220 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001222 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 struct tipc_msg *msg;
1224 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001225 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 int res;
1227
Per Liden4323add2006-01-18 00:38:21 +01001228 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001229 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 return -EINVAL;
1231
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001232 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001234 msg_set_orignode(msg, tipc_own_addr);
1235 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 msg_set_hdr_sz(msg, LONG_H_SIZE);
1237 msg_set_nametype(msg, name->type);
1238 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001239 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001240 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 msg_set_destnode(msg, destnode);
1242 msg_set_destport(msg, destport);
1243
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001244 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001246 res = tipc_port_recv_sections(p_ptr, num_sect,
1247 msg_sect);
1248 else
1249 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1250 num_sect, destnode);
1251 if (likely(res != -ELINKCONG)) {
1252 if (res > 0)
1253 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001255 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 if (port_unreliable(p_ptr)) {
1257 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001258 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 }
1260 return -ELINKCONG;
1261 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001262 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001263 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264}
1265
1266/**
Allan Stephens12bae472010-11-30 12:01:02 +00001267 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 */
1269
Allan Stephens12bae472010-11-30 12:01:02 +00001270int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1271 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001273 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274 struct tipc_msg *msg;
1275 int res;
1276
Per Liden4323add2006-01-18 00:38:21 +01001277 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001278 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 return -EINVAL;
1280
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001281 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001283 msg_set_lookup_scope(msg, 0);
Allan Stephens12bae472010-11-30 12:01:02 +00001284 msg_set_orignode(msg, tipc_own_addr);
1285 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 msg_set_destnode(msg, dest->node);
1287 msg_set_destport(msg, dest->ref);
1288 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001289
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001291 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1292 else
1293 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1294 dest->node);
1295 if (likely(res != -ELINKCONG)) {
1296 if (res > 0)
1297 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001299 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 if (port_unreliable(p_ptr)) {
1301 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001302 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 }
1304 return -ELINKCONG;
1305}
1306
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001307/**
Allan Stephens12bae472010-11-30 12:01:02 +00001308 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 */
1310
Allan Stephens12bae472010-11-30 12:01:02 +00001311int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1312 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001314 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 struct tipc_msg *msg;
1316 int res;
1317
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001318 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1319 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 return -EINVAL;
1321
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001322 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001324 msg_set_orignode(msg, tipc_own_addr);
1325 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 msg_set_destnode(msg, dest->node);
1327 msg_set_destport(msg, dest->ref);
1328 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1330 if (skb_cow(buf, DIR_MSG_H_SIZE))
1331 return -ENOMEM;
1332
1333 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001334 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001335
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001337 res = tipc_port_recv_msg(buf);
1338 else
1339 res = tipc_send_buf_fast(buf, dest->node);
1340 if (likely(res != -ELINKCONG)) {
1341 if (res > 0)
1342 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001344 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 if (port_unreliable(p_ptr))
1346 return dsz;
1347 return -ELINKCONG;
1348}
1349