blob: 6ff78f9c7d65a83623e870aa16522c3ca6219c3c [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);
94 msg_set_nametype(hdr, seq->type);
95 msg_set_namelower(hdr, seq->lower);
96 msg_set_nameupper(hdr, seq->upper);
97 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephensc68ca7b2010-05-11 14:30:12 +000098 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +010099 !oport->user_port, &buf);
100 if (unlikely(!buf))
101 return res;
102
103 /* Figure out where to send multicast message */
104
Per Liden4323add2006-01-18 00:38:21 +0100105 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
106 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900107
108 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109
110 if (ext_targets) {
111 if (dports.count != 0) {
112 ibuf = skb_copy(buf, GFP_ATOMIC);
113 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100114 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115 buf_discard(buf);
116 return -ENOMEM;
117 }
118 }
Per Liden4323add2006-01-18 00:38:21 +0100119 res = tipc_bclink_send_msg(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000120 if ((res < 0) && (dports.count != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 buf_discard(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 } else {
123 ibuf = buf;
124 }
125
126 if (res >= 0) {
127 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100128 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 } else {
Per Liden4323add2006-01-18 00:38:21 +0100130 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 }
132 return res;
133}
134
135/**
Per Liden4323add2006-01-18 00:38:21 +0100136 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900137 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138 * If there is no port list, perform a lookup to create one
139 */
140
Per Liden4323add2006-01-18 00:38:21 +0100141void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142{
Allan Stephens0e659672010-12-31 18:59:32 +0000143 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 struct port_list dports = {0, NULL, };
145 struct port_list *item = dp;
146 int cnt = 0;
147
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 msg = buf_msg(buf);
149
150 /* Create destination port list, if one wasn't supplied */
151
152 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100153 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 msg_namelower(msg),
155 msg_nameupper(msg),
156 TIPC_CLUSTER_SCOPE,
157 &dports);
158 item = dp = &dports;
159 }
160
161 /* Deliver a copy of message to each destination port */
162
163 if (dp->count != 0) {
164 if (dp->count == 1) {
165 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100166 tipc_port_recv_msg(buf);
167 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 return;
169 }
170 for (; cnt < dp->count; cnt++) {
171 int index = cnt % PLSIZE;
172 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
173
174 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700175 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 goto exit;
177 }
Allan Stephensa0168922010-12-31 18:59:35 +0000178 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000180 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100181 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 }
183 }
184exit:
185 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100186 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187}
188
189/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700190 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900191 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700192 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 */
194
Allan Stephens0ea52242008-07-14 22:42:19 -0700195struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
197 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700198 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500200 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 struct tipc_msg *msg;
202 u32 ref;
203
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700204 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700205 if (!p_ptr) {
206 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700207 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500209 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700211 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700213 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 }
215
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500216 p_ptr->usr_handle = usr_handle;
217 p_ptr->max_pkt = MAX_PKT_DEFAULT;
218 p_ptr->ref = ref;
219 msg = &p_ptr->phdr;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000220 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 INIT_LIST_HEAD(&p_ptr->wait_list);
223 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 p_ptr->dispatcher = dispatcher;
225 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800226 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100228 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 INIT_LIST_HEAD(&p_ptr->publications);
230 INIT_LIST_HEAD(&p_ptr->port_list);
231 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100232 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500233 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234}
235
236int tipc_deleteport(u32 ref)
237{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500238 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800239 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800241 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100242 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900243 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 return -EINVAL;
245
Per Liden4323add2006-01-18 00:38:21 +0100246 tipc_ref_discard(ref);
247 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248
249 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500250 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100252 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 }
Allan Stephense83504f2010-12-31 18:59:30 +0000254 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255
Per Liden4323add2006-01-18 00:38:21 +0100256 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 list_del(&p_ptr->port_list);
258 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100259 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 k_term_timer(&p_ptr->timer);
261 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100262 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700263 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264}
265
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500266static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500268 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269}
270
271int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
272{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500273 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900274
Per Liden4323add2006-01-18 00:38:21 +0100275 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 if (!p_ptr)
277 return -EINVAL;
278 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800279 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700280 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281}
282
283int tipc_set_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;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500290 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100291 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700292 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293}
294
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500295static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500297 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298}
299
300int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
301{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500302 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900303
Per Liden4323add2006-01-18 00:38:21 +0100304 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 if (!p_ptr)
306 return -EINVAL;
307 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800308 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700309 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310}
311
312int tipc_set_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;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500319 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100320 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700321 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322}
323
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900324/*
325 * port_build_proto_msg(): build a port level protocol
326 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 * tipc_port lock on.
328 */
329static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
330 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900331 u32 usr, u32 type, u32 err,
Allan Stephens741de3e2011-01-25 13:33:31 -0500332 u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333{
334 struct sk_buff *buf;
335 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900336
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000337 buf = tipc_buf_acquire(LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 if (buf) {
339 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000340 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700341 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 msg_set_destport(msg, destport);
343 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 msg_set_orignode(msg, orignode);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 }
347 return buf;
348}
349
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350int tipc_reject_msg(struct sk_buff *buf, u32 err)
351{
352 struct tipc_msg *msg = buf_msg(buf);
353 struct sk_buff *rbuf;
354 struct tipc_msg *rmsg;
355 int hdr_sz;
356 u32 imp = msg_importance(msg);
357 u32 data_sz = msg_data_sz(msg);
358
359 if (data_sz > MAX_REJECT_SIZE)
360 data_sz = MAX_REJECT_SIZE;
361 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
362 imp++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363
364 /* discard rejected message if it shouldn't be returned to sender */
365 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
366 buf_discard(buf);
367 return data_sz;
368 }
369
370 /* construct rejected message */
371 if (msg_mcast(msg))
372 hdr_sz = MCAST_H_SIZE;
373 else
374 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000375 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 if (rbuf == NULL) {
377 buf_discard(buf);
378 return data_sz;
379 }
380 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000381 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700382 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700385 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700387 /* leave name type & instance as zeroes */
388 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700390 msg_set_nametype(rmsg, msg_nametype(msg));
391 msg_set_nameinst(rmsg, msg_nameinst(msg));
392 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900393 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300394 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395
396 /* send self-abort message when rejecting on a connected port */
397 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800398 struct sk_buff *abuf = NULL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500399 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400
401 if (p_ptr) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500402 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100404 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 }
Per Liden4323add2006-01-18 00:38:21 +0100406 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 }
408
409 /* send rejected message */
410 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100411 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 return data_sz;
413}
414
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500415int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100416 struct iovec const *msg_sect, u32 num_sect,
417 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418{
419 struct sk_buff *buf;
420 int res;
421
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000422 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 !p_ptr->user_port, &buf);
424 if (!buf)
425 return res;
426
427 return tipc_reject_msg(buf, err);
428}
429
430static void port_timeout(unsigned long ref)
431{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500432 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800433 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434
Allan Stephens065fd172006-10-16 21:38:05 -0700435 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 return;
437
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500438 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700439 tipc_port_unlock(p_ptr);
440 return;
441 }
442
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 /* Last probe answered ? */
444 if (p_ptr->probing_state == PROBING) {
445 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
446 } else {
447 buf = port_build_proto_msg(port_peerport(p_ptr),
448 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500449 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 tipc_own_addr,
451 CONN_MANAGER,
452 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900453 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 p_ptr->probing_state = PROBING;
456 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
457 }
Per Liden4323add2006-01-18 00:38:21 +0100458 tipc_port_unlock(p_ptr);
459 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460}
461
462
463static void port_handle_node_down(unsigned long ref)
464{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500465 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000466 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467
468 if (!p_ptr)
469 return;
470 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100471 tipc_port_unlock(p_ptr);
472 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473}
474
475
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500476static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500478 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500480 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800481 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 if (imp < TIPC_CRITICAL_IMPORTANCE)
483 imp++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500484 return port_build_proto_msg(p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 tipc_own_addr,
486 port_peerport(p_ptr),
487 port_peernode(p_ptr),
488 imp,
489 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 0);
492}
493
494
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500495static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500497 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500499 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800500 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 if (imp < TIPC_CRITICAL_IMPORTANCE)
502 imp++;
503 return port_build_proto_msg(port_peerport(p_ptr),
504 port_peernode(p_ptr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500505 p_ptr->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 tipc_own_addr,
507 imp,
508 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 0);
511}
512
Per Liden4323add2006-01-18 00:38:21 +0100513void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514{
515 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500516 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800518 struct sk_buff *r_buf = NULL;
519 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 if (!p_ptr) {
522 err = TIPC_ERR_NO_PORT;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500523 } else if (p_ptr->connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000524 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
525 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000527 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900528 int wakeup = tipc_port_congested(p_ptr) &&
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500529 p_ptr->congested &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 p_ptr->wakeup;
531 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100532 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500534 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 if (!wakeup)
536 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500537 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 goto exit;
539 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500540 } else if (p_ptr->published) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 err = TIPC_ERR_NO_PORT;
542 }
543 if (err) {
544 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900545 msg_orignode(msg),
546 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800548 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 TIPC_CONN_MSG,
550 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 0);
552 goto exit;
553 }
554
555 /* All is fine */
556 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900557 r_buf = port_build_proto_msg(msg_origport(msg),
558 msg_orignode(msg),
559 msg_destport(msg),
560 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 CONN_MANAGER,
562 CONN_PROBE_REPLY,
563 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 0);
565 }
566 p_ptr->probing_state = CONFIRMED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567exit:
568 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100569 tipc_port_unlock(p_ptr);
570 tipc_net_route_msg(r_buf);
571 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 buf_discard(buf);
573}
574
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500575static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900577 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578
579 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900580 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500582 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500584 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500586 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587 u32 dport = port_peerport(p_ptr);
588 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
591 tipc_zone(destnode), tipc_cluster(destnode),
592 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500593 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900594 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500595 p_ptr->conn_type,
596 p_ptr->conn_instance);
597 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900598 tipc_printf(buf, " bound to");
599 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 if (publ->lower == publ->upper)
601 tipc_printf(buf, " {%u,%u}", publ->type,
602 publ->lower);
603 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 }
607 }
608 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609}
610
611#define MAX_PORT_QUERY 32768
612
Per Liden4323add2006-01-18 00:38:21 +0100613struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614{
615 struct sk_buff *buf;
616 struct tlv_desc *rep_tlv;
617 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500618 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 int str_len;
620
Per Liden4323add2006-01-18 00:38:21 +0100621 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 if (!buf)
623 return NULL;
624 rep_tlv = (struct tlv_desc *)buf->data;
625
Per Liden4323add2006-01-18 00:38:21 +0100626 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
627 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500629 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500631 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 }
Per Liden4323add2006-01-18 00:38:21 +0100633 spin_unlock_bh(&tipc_port_list_lock);
634 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635
636 skb_put(buf, TLV_SPACE(str_len));
637 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
638
639 return buf;
640}
641
Per Liden4323add2006-01-18 00:38:21 +0100642void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500644 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 struct tipc_msg *msg;
646
Per Liden4323add2006-01-18 00:38:21 +0100647 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500649 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 if (msg_orignode(msg) == tipc_own_addr)
651 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700652 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 msg_set_orignode(msg, tipc_own_addr);
654 }
Per Liden4323add2006-01-18 00:38:21 +0100655 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656}
657
658
659/*
660 * port_dispatcher_sigh(): Signal handler for messages destinated
661 * to the tipc_port interface.
662 */
663
664static void port_dispatcher_sigh(void *dummy)
665{
666 struct sk_buff *buf;
667
668 spin_lock_bh(&queue_lock);
669 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800670 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 spin_unlock_bh(&queue_lock);
672
673 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500674 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 struct user_port *up_ptr;
676 struct tipc_portid orig;
677 struct tipc_name_seq dseq;
678 void *usr_handle;
679 int connected;
680 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700681 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682
683 struct sk_buff *next = buf->next;
684 struct tipc_msg *msg = buf_msg(buf);
685 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900686
Allan Stephens96882432006-06-25 23:38:58 -0700687 message_type = msg_type(msg);
688 if (message_type > TIPC_DIRECT_MSG)
689 goto reject; /* Unsupported message type */
690
Per Liden4323add2006-01-18 00:38:21 +0100691 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700692 if (!p_ptr)
693 goto reject; /* Port deleted while msg in queue */
694
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695 orig.ref = msg_origport(msg);
696 orig.node = msg_orignode(msg);
697 up_ptr = p_ptr->user_port;
698 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500699 connected = p_ptr->connected;
700 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701
702 if (unlikely(msg_errcode(msg)))
703 goto err;
704
Allan Stephens96882432006-06-25 23:38:58 -0700705 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900706
Per Lidenb97bf3f2006-01-02 19:04:38 +0100707 case TIPC_CONN_MSG:{
708 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
709 u32 peer_port = port_peerport(p_ptr);
710 u32 peer_node = port_peernode(p_ptr);
Allan Stephenscb7ce912011-01-24 15:02:14 -0500711 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712
Julia Lawall4cec72c2008-01-08 23:48:20 -0800713 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700714 if (unlikely(!cb))
715 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700717 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700719 } else if ((msg_origport(msg) != peer_port) ||
720 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500722 dsz = msg_data_sz(msg);
723 if (unlikely(dsz &&
724 (++p_ptr->conn_unacked >=
725 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900726 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500727 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500729 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 break;
731 }
732 case TIPC_DIRECT_MSG:{
733 tipc_msg_event cb = up_ptr->msg_cb;
734
Julia Lawall4cec72c2008-01-08 23:48:20 -0800735 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700736 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 goto reject;
738 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900739 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 msg_data_sz(msg), msg_importance(msg),
741 &orig);
742 break;
743 }
Allan Stephens96882432006-06-25 23:38:58 -0700744 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 case TIPC_NAMED_MSG:{
746 tipc_named_msg_event cb = up_ptr->named_msg_cb;
747
Julia Lawall4cec72c2008-01-08 23:48:20 -0800748 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700749 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 goto reject;
751 dseq.type = msg_nametype(msg);
752 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700753 dseq.upper = (message_type == TIPC_NAMED_MSG)
754 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900756 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757 msg_data_sz(msg), msg_importance(msg),
758 &orig, &dseq);
759 break;
760 }
761 }
762 if (buf)
763 buf_discard(buf);
764 buf = next;
765 continue;
766err:
Allan Stephens96882432006-06-25 23:38:58 -0700767 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900768
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900770 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 up_ptr->conn_err_cb;
772 u32 peer_port = port_peerport(p_ptr);
773 u32 peer_node = port_peernode(p_ptr);
774
Julia Lawall4cec72c2008-01-08 23:48:20 -0800775 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700776 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700778 if ((msg_origport(msg) != peer_port) ||
779 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 break;
781 tipc_disconnect(dref);
782 skb_pull(buf, msg_hdr_sz(msg));
783 cb(usr_handle, dref, &buf, msg_data(msg),
784 msg_data_sz(msg), msg_errcode(msg));
785 break;
786 }
787 case TIPC_DIRECT_MSG:{
788 tipc_msg_err_event cb = up_ptr->err_cb;
789
Julia Lawall4cec72c2008-01-08 23:48:20 -0800790 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700791 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 break;
793 skb_pull(buf, msg_hdr_sz(msg));
794 cb(usr_handle, dref, &buf, msg_data(msg),
795 msg_data_sz(msg), msg_errcode(msg), &orig);
796 break;
797 }
Allan Stephens96882432006-06-25 23:38:58 -0700798 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900800 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 up_ptr->named_err_cb;
802
Julia Lawall4cec72c2008-01-08 23:48:20 -0800803 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700804 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 break;
806 dseq.type = msg_nametype(msg);
807 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700808 dseq.upper = (message_type == TIPC_NAMED_MSG)
809 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 msg_data_sz(msg), msg_errcode(msg), &dseq);
813 break;
814 }
815 }
816 if (buf)
817 buf_discard(buf);
818 buf = next;
819 continue;
820reject:
821 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
822 buf = next;
823 }
824}
825
826/*
827 * port_dispatcher(): Dispatcher for messages destinated
828 * to the tipc_port interface. Called with port locked.
829 */
830
831static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
832{
833 buf->next = NULL;
834 spin_lock_bh(&queue_lock);
835 if (msg_queue_head) {
836 msg_queue_tail->next = buf;
837 msg_queue_tail = buf;
838 } else {
839 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100840 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 }
842 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700843 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844}
845
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900846/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900848 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 */
850
851static void port_wakeup_sh(unsigned long ref)
852{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500853 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800855 tipc_continue_event cb = NULL;
856 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857
Per Liden4323add2006-01-18 00:38:21 +0100858 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 if (p_ptr) {
860 up_ptr = p_ptr->user_port;
861 if (up_ptr) {
862 cb = up_ptr->continue_event_cb;
863 uh = up_ptr->usr_handle;
864 }
Per Liden4323add2006-01-18 00:38:21 +0100865 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 }
867 if (cb)
868 cb(uh, ref);
869}
870
871
872static void port_wakeup(struct tipc_port *p_ptr)
873{
Per Liden4323add2006-01-18 00:38:21 +0100874 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875}
876
877void tipc_acknowledge(u32 ref, u32 ack)
878{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500879 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800880 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881
Per Liden4323add2006-01-18 00:38:21 +0100882 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 if (!p_ptr)
884 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500885 if (p_ptr->connected) {
886 p_ptr->conn_unacked -= ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 buf = port_build_proto_msg(port_peerport(p_ptr),
888 port_peernode(p_ptr),
889 ref,
890 tipc_own_addr,
891 CONN_MANAGER,
892 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900893 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 ack);
895 }
Per Liden4323add2006-01-18 00:38:21 +0100896 tipc_port_unlock(p_ptr);
897 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898}
899
900/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000901 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 */
903
Allan Stephensb0c1e922010-12-31 18:59:22 +0000904int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900905 unsigned int importance,
906 tipc_msg_err_event error_cb,
907 tipc_named_msg_err_event named_error_cb,
908 tipc_conn_shutdown_event conn_error_cb,
909 tipc_msg_event msg_cb,
910 tipc_named_msg_event named_msg_cb,
911 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 tipc_continue_event continue_event_cb,/* May be zero */
913 u32 *portref)
914{
915 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500916 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700918 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700919 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700920 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 return -ENOMEM;
922 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500923 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700924 port_wakeup, importance);
925 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 kfree(up_ptr);
927 return -ENOMEM;
928 }
929
930 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500932 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 up_ptr->err_cb = error_cb;
934 up_ptr->named_err_cb = named_error_cb;
935 up_ptr->conn_err_cb = conn_error_cb;
936 up_ptr->msg_cb = msg_cb;
937 up_ptr->named_msg_cb = named_msg_cb;
938 up_ptr->conn_msg_cb = conn_msg_cb;
939 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500940 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100941 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700942 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943}
944
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945int tipc_portimportance(u32 ref, unsigned int *importance)
946{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500947 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900948
Per Liden4323add2006-01-18 00:38:21 +0100949 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 if (!p_ptr)
951 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500952 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800953 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700954 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955}
956
957int tipc_set_portimportance(u32 ref, unsigned int imp)
958{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500959 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960
961 if (imp > TIPC_CRITICAL_IMPORTANCE)
962 return -EINVAL;
963
Per Liden4323add2006-01-18 00:38:21 +0100964 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 if (!p_ptr)
966 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500967 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800968 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700969 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970}
971
972
973int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
974{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500975 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 struct publication *publ;
977 u32 key;
978 int res = -EINVAL;
979
Per Liden4323add2006-01-18 00:38:21 +0100980 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800981 if (!p_ptr)
982 return -EINVAL;
983
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500984 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 goto exit;
986 if (seq->lower > seq->upper)
987 goto exit;
988 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
989 goto exit;
990 key = ref + p_ptr->pub_count + 1;
991 if (key == ref) {
992 res = -EADDRINUSE;
993 goto exit;
994 }
Per Liden4323add2006-01-18 00:38:21 +0100995 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500996 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997 if (publ) {
998 list_add(&publ->pport_list, &p_ptr->publications);
999 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001000 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001001 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 }
1003exit:
Per Liden4323add2006-01-18 00:38:21 +01001004 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 return res;
1006}
1007
1008int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1009{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001010 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 struct publication *publ;
1012 struct publication *tpubl;
1013 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001014
Per Liden4323add2006-01-18 00:38:21 +01001015 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 if (!p_ptr)
1017 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001021 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001022 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001024 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 &p_ptr->publications, pport_list) {
1028 if (publ->scope != scope)
1029 continue;
1030 if (publ->type != seq->type)
1031 continue;
1032 if (publ->lower != seq->lower)
1033 continue;
1034 if (publ->upper != seq->upper)
1035 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001036 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001037 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001038 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 break;
1040 }
1041 }
1042 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001043 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001044 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 return res;
1046}
1047
1048int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1049{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001050 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051 struct tipc_msg *msg;
1052 int res = -EINVAL;
1053
Per Liden4323add2006-01-18 00:38:21 +01001054 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 if (!p_ptr)
1056 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001057 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 goto exit;
1059 if (!peer->ref)
1060 goto exit;
1061
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001062 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 msg_set_destnode(msg, peer->node);
1064 msg_set_destport(msg, peer->ref);
1065 msg_set_orignode(msg, tipc_own_addr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001066 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens08c80e92010-12-31 18:59:17 +00001068 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069
1070 p_ptr->probing_interval = PROBING_INTERVAL;
1071 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001072 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1074
Allan Stephens0e659672010-12-31 18:59:32 +00001075 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001076 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001078 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079exit:
Per Liden4323add2006-01-18 00:38:21 +01001080 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001081 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 return res;
1083}
1084
Allan Stephens0c3141e2008-04-15 00:22:02 -07001085/**
1086 * tipc_disconnect_port - disconnect port from peer
1087 *
1088 * Port must be locked.
1089 */
1090
1091int tipc_disconnect_port(struct tipc_port *tp_ptr)
1092{
1093 int res;
1094
1095 if (tp_ptr->connected) {
1096 tp_ptr->connected = 0;
1097 /* let timer expire on it's own to avoid deadlock! */
1098 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001099 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001100 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001101 } else {
1102 res = -ENOTCONN;
1103 }
1104 return res;
1105}
1106
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107/*
1108 * tipc_disconnect(): Disconnect port form peer.
1109 * This is a node local operation.
1110 */
1111
1112int tipc_disconnect(u32 ref)
1113{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001114 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001115 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116
Per Liden4323add2006-01-18 00:38:21 +01001117 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 if (!p_ptr)
1119 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001120 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001121 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 return res;
1123}
1124
1125/*
1126 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1127 */
1128int tipc_shutdown(u32 ref)
1129{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001130 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001131 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132
Per Liden4323add2006-01-18 00:38:21 +01001133 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 if (!p_ptr)
1135 return -EINVAL;
1136
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001137 if (p_ptr->connected) {
1138 u32 imp = msg_importance(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 if (imp < TIPC_CRITICAL_IMPORTANCE)
1140 imp++;
1141 buf = port_build_proto_msg(port_peerport(p_ptr),
1142 port_peernode(p_ptr),
1143 ref,
1144 tipc_own_addr,
1145 imp,
1146 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001147 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 0);
1149 }
Per Liden4323add2006-01-18 00:38:21 +01001150 tipc_port_unlock(p_ptr);
1151 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 return tipc_disconnect(ref);
1153}
1154
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155/*
Per Liden4323add2006-01-18 00:38:21 +01001156 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 * message for this node.
1158 */
1159
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001160static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001161 struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162{
1163 struct sk_buff *buf;
1164 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001165
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001166 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 MAX_MSG_SIZE, !sender->user_port, &buf);
1168 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001169 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 return res;
1171}
1172
1173/**
1174 * tipc_send - send message sections on connection
1175 */
1176
1177int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1178{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001179 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 u32 destnode;
1181 int res;
1182
Per Liden4323add2006-01-18 00:38:21 +01001183 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001184 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 return -EINVAL;
1186
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001187 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001188 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 destnode = port_peernode(p_ptr);
1190 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001191 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1192 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 else
Per Liden4323add2006-01-18 00:38:21 +01001194 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195
1196 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001197 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001198 if (res > 0)
1199 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 return res;
1201 }
1202 }
1203 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001204 p_ptr->congested = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001206 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 }
1208 return -ELINKCONG;
1209}
1210
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001211/**
Allan Stephens12bae472010-11-30 12:01:02 +00001212 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 */
1214
Allan Stephens12bae472010-11-30 12:01:02 +00001215int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1216 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001218 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 struct tipc_msg *msg;
1220 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001221 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 int res;
1223
Per Liden4323add2006-01-18 00:38:21 +01001224 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001225 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 return -EINVAL;
1227
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001228 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001230 msg_set_orignode(msg, tipc_own_addr);
1231 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 msg_set_hdr_sz(msg, LONG_H_SIZE);
1233 msg_set_nametype(msg, name->type);
1234 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001235 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001236 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 msg_set_destnode(msg, destnode);
1238 msg_set_destport(msg, destport);
1239
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001240 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001242 res = tipc_port_recv_sections(p_ptr, num_sect,
1243 msg_sect);
1244 else
1245 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1246 num_sect, destnode);
1247 if (likely(res != -ELINKCONG)) {
1248 if (res > 0)
1249 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001251 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 if (port_unreliable(p_ptr)) {
1253 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001254 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255 }
1256 return -ELINKCONG;
1257 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001258 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001259 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260}
1261
1262/**
Allan Stephens12bae472010-11-30 12:01:02 +00001263 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 */
1265
Allan Stephens12bae472010-11-30 12:01:02 +00001266int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1267 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001269 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 struct tipc_msg *msg;
1271 int res;
1272
Per Liden4323add2006-01-18 00:38:21 +01001273 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001274 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275 return -EINVAL;
1276
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001277 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001279 msg_set_orignode(msg, tipc_own_addr);
1280 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 msg_set_destnode(msg, dest->node);
1282 msg_set_destport(msg, dest->ref);
1283 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001284
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001286 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1287 else
1288 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1289 dest->node);
1290 if (likely(res != -ELINKCONG)) {
1291 if (res > 0)
1292 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001294 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 if (port_unreliable(p_ptr)) {
1296 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001297 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 }
1299 return -ELINKCONG;
1300}
1301
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001302/**
Allan Stephens12bae472010-11-30 12:01:02 +00001303 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 */
1305
Allan Stephens12bae472010-11-30 12:01:02 +00001306int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1307 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001309 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 struct tipc_msg *msg;
1311 int res;
1312
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001313 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1314 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 return -EINVAL;
1316
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001317 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001319 msg_set_orignode(msg, tipc_own_addr);
1320 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 msg_set_destnode(msg, dest->node);
1322 msg_set_destport(msg, dest->ref);
1323 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1325 if (skb_cow(buf, DIR_MSG_H_SIZE))
1326 return -ENOMEM;
1327
1328 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001329 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001330
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001332 res = tipc_port_recv_msg(buf);
1333 else
1334 res = tipc_send_buf_fast(buf, dest->node);
1335 if (likely(res != -ELINKCONG)) {
1336 if (res > 0)
1337 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001339 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 if (port_unreliable(p_ptr))
1341 return dsz;
1342 return -ELINKCONG;
1343}
1344