blob: 5c4285b2d5552c0d04a757ba548139d2ee3f57c7 [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 Stephens0ea52242008-07-14 22:42:19 -07005 * Copyright (c) 2004-2008, 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"
39#include "dbg.h"
40#include "port.h"
41#include "addr.h"
42#include "link.h"
43#include "node.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#include "name_table.h"
45#include "user_reg.h"
46#include "msg.h"
47#include "bcast.h"
48
49/* Connection management: */
50#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
51#define CONFIRMED 0
52#define PROBING 1
53
54#define MAX_REJECT_SIZE 1024
55
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080056static struct sk_buff *msg_queue_head = NULL;
57static struct sk_buff *msg_queue_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Ingo Molnar34af9462006-06-27 02:53:55 -070059DEFINE_SPINLOCK(tipc_port_list_lock);
60static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010061
Per Liden4323add2006-01-18 00:38:21 +010062static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010063static void port_handle_node_down(unsigned long ref);
64static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
65static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
66static void port_timeout(unsigned long ref);
67
68
Sam Ravnborg05790c62006-03-20 22:37:04 -080069static u32 port_peernode(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010070{
71 return msg_destnode(&p_ptr->publ.phdr);
72}
73
Sam Ravnborg05790c62006-03-20 22:37:04 -080074static u32 port_peerport(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010075{
76 return msg_destport(&p_ptr->publ.phdr);
77}
78
Sam Ravnborg05790c62006-03-20 22:37:04 -080079static u32 port_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010080{
81 return msg_transp_seqno(&p_ptr->publ.phdr);
82}
83
Sam Ravnborg05790c62006-03-20 22:37:04 -080084static void port_incr_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010085{
86 struct tipc_msg *m = &p_ptr->publ.phdr;
87
88 if (likely(!msg_routed(m)))
89 return;
90 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
91}
92
93/**
94 * tipc_multicast - send a multicast message to local and remote destinations
95 */
96
97int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
98 u32 num_sect, struct iovec const *msg_sect)
99{
100 struct tipc_msg *hdr;
101 struct sk_buff *buf;
102 struct sk_buff *ibuf = NULL;
103 struct port_list dports = {0, NULL, };
Per Liden4323add2006-01-18 00:38:21 +0100104 struct port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 int ext_targets;
106 int res;
107
108 if (unlikely(!oport))
109 return -EINVAL;
110
111 /* Create multicast message */
112
113 hdr = &oport->publ.phdr;
114 msg_set_type(hdr, TIPC_MCAST_MSG);
115 msg_set_nametype(hdr, seq->type);
116 msg_set_namelower(hdr, seq->lower);
117 msg_set_nameupper(hdr, seq->upper);
118 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000119 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 !oport->user_port, &buf);
121 if (unlikely(!buf))
122 return res;
123
124 /* Figure out where to send multicast message */
125
Per Liden4323add2006-01-18 00:38:21 +0100126 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
127 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900128
129 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130
131 if (ext_targets) {
132 if (dports.count != 0) {
133 ibuf = skb_copy(buf, GFP_ATOMIC);
134 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100135 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 buf_discard(buf);
137 return -ENOMEM;
138 }
139 }
Per Liden4323add2006-01-18 00:38:21 +0100140 res = tipc_bclink_send_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 if ((res < 0) && (dports.count != 0)) {
142 buf_discard(ibuf);
143 }
144 } else {
145 ibuf = buf;
146 }
147
148 if (res >= 0) {
149 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100150 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 } else {
Per Liden4323add2006-01-18 00:38:21 +0100152 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 }
154 return res;
155}
156
157/**
Per Liden4323add2006-01-18 00:38:21 +0100158 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900159 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 * If there is no port list, perform a lookup to create one
161 */
162
Per Liden4323add2006-01-18 00:38:21 +0100163void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164{
165 struct tipc_msg* msg;
166 struct port_list dports = {0, NULL, };
167 struct port_list *item = dp;
168 int cnt = 0;
169
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 msg = buf_msg(buf);
171
172 /* Create destination port list, if one wasn't supplied */
173
174 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100175 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 msg_namelower(msg),
177 msg_nameupper(msg),
178 TIPC_CLUSTER_SCOPE,
179 &dports);
180 item = dp = &dports;
181 }
182
183 /* Deliver a copy of message to each destination port */
184
185 if (dp->count != 0) {
186 if (dp->count == 1) {
187 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100188 tipc_port_recv_msg(buf);
189 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 return;
191 }
192 for (; cnt < dp->count; cnt++) {
193 int index = cnt % PLSIZE;
194 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
195
196 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700197 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 msg_dbg(msg, "LOST:");
199 goto exit;
200 }
201 if ((index == 0) && (cnt != 0)) {
202 item = item->next;
203 }
204 msg_set_destport(buf_msg(b),item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100205 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 }
207 }
208exit:
209 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100210 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211}
212
213/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700214 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900215 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700216 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 */
218
Allan Stephens0ea52242008-07-14 22:42:19 -0700219struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
221 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700222 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223{
224 struct port *p_ptr;
225 struct tipc_msg *msg;
226 u32 ref;
227
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700228 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700229 if (!p_ptr) {
230 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700231 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 }
Per Liden4323add2006-01-18 00:38:21 +0100233 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700235 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700237 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 }
239
Allan Stephens05646c92007-06-10 17:25:24 -0700240 p_ptr->publ.usr_handle = usr_handle;
241 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 p_ptr->publ.ref = ref;
243 msg = &p_ptr->publ.phdr;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000244 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 p_ptr->last_in_seqno = 41;
247 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 INIT_LIST_HEAD(&p_ptr->wait_list);
249 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250 p_ptr->dispatcher = dispatcher;
251 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800252 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100254 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 INIT_LIST_HEAD(&p_ptr->publications);
256 INIT_LIST_HEAD(&p_ptr->port_list);
257 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100258 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens0ea52242008-07-14 22:42:19 -0700259 return &(p_ptr->publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260}
261
262int tipc_deleteport(u32 ref)
263{
264 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800265 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800267 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100268 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900269 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 return -EINVAL;
271
Per Liden4323add2006-01-18 00:38:21 +0100272 tipc_ref_discard(ref);
273 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274
275 k_cancel_timer(&p_ptr->timer);
276 if (p_ptr->publ.connected) {
277 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100278 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279 }
280 if (p_ptr->user_port) {
Per Liden4323add2006-01-18 00:38:21 +0100281 tipc_reg_remove_port(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 kfree(p_ptr->user_port);
283 }
284
Per Liden4323add2006-01-18 00:38:21 +0100285 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 list_del(&p_ptr->port_list);
287 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100288 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 k_term_timer(&p_ptr->timer);
290 kfree(p_ptr);
291 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100292 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700293 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294}
295
296/**
297 * tipc_get_port() - return port associated with 'ref'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 * Note: Port is not locked.
300 */
301
302struct tipc_port *tipc_get_port(const u32 ref)
303{
Per Liden4323add2006-01-18 00:38:21 +0100304 return (struct tipc_port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305}
306
307/**
308 * tipc_get_handle - return user handle associated to port 'ref'
309 */
310
311void *tipc_get_handle(const u32 ref)
312{
313 struct port *p_ptr;
314 void * handle;
315
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)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800318 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 handle = p_ptr->publ.usr_handle;
Per Liden4323add2006-01-18 00:38:21 +0100320 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 return handle;
322}
323
Sam Ravnborg05790c62006-03-20 22:37:04 -0800324static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325{
326 return msg_src_droppable(&p_ptr->publ.phdr);
327}
328
329int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
330{
331 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900332
Per Liden4323add2006-01-18 00:38:21 +0100333 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 if (!p_ptr)
335 return -EINVAL;
336 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800337 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700338 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339}
340
341int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
342{
343 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900344
Per Liden4323add2006-01-18 00:38:21 +0100345 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 if (!p_ptr)
347 return -EINVAL;
348 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100349 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700350 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351}
352
Sam Ravnborg05790c62006-03-20 22:37:04 -0800353static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354{
355 return msg_dest_droppable(&p_ptr->publ.phdr);
356}
357
358int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
359{
360 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900361
Per Liden4323add2006-01-18 00:38:21 +0100362 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 if (!p_ptr)
364 return -EINVAL;
365 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800366 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700367 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368}
369
370int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
371{
372 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900373
Per Liden4323add2006-01-18 00:38:21 +0100374 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 if (!p_ptr)
376 return -EINVAL;
377 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100378 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700379 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380}
381
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900382/*
383 * port_build_proto_msg(): build a port level protocol
384 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 * tipc_port lock on.
386 */
387static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
388 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900389 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 u32 seqno, u32 ack)
391{
392 struct sk_buff *buf;
393 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900394
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 buf = buf_acquire(LONG_H_SIZE);
396 if (buf) {
397 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000398 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700399 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 msg_set_destport(msg, destport);
401 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 msg_set_orignode(msg, orignode);
403 msg_set_transp_seqno(msg, seqno);
404 msg_set_msgcnt(msg, ack);
405 msg_dbg(msg, "PORT>SEND>:");
406 }
407 return buf;
408}
409
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410int tipc_reject_msg(struct sk_buff *buf, u32 err)
411{
412 struct tipc_msg *msg = buf_msg(buf);
413 struct sk_buff *rbuf;
414 struct tipc_msg *rmsg;
415 int hdr_sz;
416 u32 imp = msg_importance(msg);
417 u32 data_sz = msg_data_sz(msg);
418
419 if (data_sz > MAX_REJECT_SIZE)
420 data_sz = MAX_REJECT_SIZE;
421 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
422 imp++;
423 msg_dbg(msg, "port->rej: ");
424
425 /* discard rejected message if it shouldn't be returned to sender */
426 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
427 buf_discard(buf);
428 return data_sz;
429 }
430
431 /* construct rejected message */
432 if (msg_mcast(msg))
433 hdr_sz = MCAST_H_SIZE;
434 else
435 hdr_sz = LONG_H_SIZE;
436 rbuf = buf_acquire(data_sz + hdr_sz);
437 if (rbuf == NULL) {
438 buf_discard(buf);
439 return data_sz;
440 }
441 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000442 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700443 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700446 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700448 /* leave name type & instance as zeroes */
449 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700451 msg_set_nametype(rmsg, msg_nametype(msg));
452 msg_set_nameinst(rmsg, msg_nameinst(msg));
453 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900454 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300455 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456
457 /* send self-abort message when rejecting on a connected port */
458 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800459 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100460 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461
462 if (p_ptr) {
463 if (p_ptr->publ.connected)
464 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100465 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 }
Per Liden4323add2006-01-18 00:38:21 +0100467 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 }
469
470 /* send rejected message */
471 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100472 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 return data_sz;
474}
475
Per Liden4323add2006-01-18 00:38:21 +0100476int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
477 struct iovec const *msg_sect, u32 num_sect,
478 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479{
480 struct sk_buff *buf;
481 int res;
482
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000483 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 !p_ptr->user_port, &buf);
485 if (!buf)
486 return res;
487
488 return tipc_reject_msg(buf, err);
489}
490
491static void port_timeout(unsigned long ref)
492{
Per Liden4323add2006-01-18 00:38:21 +0100493 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800494 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495
Allan Stephens065fd172006-10-16 21:38:05 -0700496 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 return;
498
Allan Stephens065fd172006-10-16 21:38:05 -0700499 if (!p_ptr->publ.connected) {
500 tipc_port_unlock(p_ptr);
501 return;
502 }
503
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 /* Last probe answered ? */
505 if (p_ptr->probing_state == PROBING) {
506 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
507 } else {
508 buf = port_build_proto_msg(port_peerport(p_ptr),
509 port_peernode(p_ptr),
510 p_ptr->publ.ref,
511 tipc_own_addr,
512 CONN_MANAGER,
513 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900514 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 port_out_seqno(p_ptr),
516 0);
517 port_incr_out_seqno(p_ptr);
518 p_ptr->probing_state = PROBING;
519 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
520 }
Per Liden4323add2006-01-18 00:38:21 +0100521 tipc_port_unlock(p_ptr);
522 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523}
524
525
526static void port_handle_node_down(unsigned long ref)
527{
Per Liden4323add2006-01-18 00:38:21 +0100528 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800529 struct sk_buff* buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530
531 if (!p_ptr)
532 return;
533 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100534 tipc_port_unlock(p_ptr);
535 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536}
537
538
539static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
540{
541 u32 imp = msg_importance(&p_ptr->publ.phdr);
542
543 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800544 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 if (imp < TIPC_CRITICAL_IMPORTANCE)
546 imp++;
547 return port_build_proto_msg(p_ptr->publ.ref,
548 tipc_own_addr,
549 port_peerport(p_ptr),
550 port_peernode(p_ptr),
551 imp,
552 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900553 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 p_ptr->last_in_seqno + 1,
555 0);
556}
557
558
559static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
560{
561 u32 imp = msg_importance(&p_ptr->publ.phdr);
562
563 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800564 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 if (imp < TIPC_CRITICAL_IMPORTANCE)
566 imp++;
567 return port_build_proto_msg(port_peerport(p_ptr),
568 port_peernode(p_ptr),
569 p_ptr->publ.ref,
570 tipc_own_addr,
571 imp,
572 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900573 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 port_out_seqno(p_ptr),
575 0);
576}
577
Per Liden4323add2006-01-18 00:38:21 +0100578void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579{
580 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100581 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800583 struct sk_buff *r_buf = NULL;
584 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585
586 msg_dbg(msg, "PORT<RECV<:");
587
588 if (!p_ptr) {
589 err = TIPC_ERR_NO_PORT;
590 } else if (p_ptr->publ.connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000591 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
592 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000594 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 p_ptr->publ.congested &&
597 p_ptr->wakeup;
598 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100599 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 goto exit;
601 p_ptr->publ.congested = 0;
602 if (!wakeup)
603 goto exit;
604 p_ptr->wakeup(&p_ptr->publ);
605 goto exit;
606 }
607 } else if (p_ptr->publ.published) {
608 err = TIPC_ERR_NO_PORT;
609 }
610 if (err) {
611 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 msg_orignode(msg),
613 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800615 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 TIPC_CONN_MSG,
617 err,
618 0,
619 0);
620 goto exit;
621 }
622
623 /* All is fine */
624 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900625 r_buf = port_build_proto_msg(msg_origport(msg),
626 msg_orignode(msg),
627 msg_destport(msg),
628 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 CONN_MANAGER,
630 CONN_PROBE_REPLY,
631 TIPC_OK,
632 port_out_seqno(p_ptr),
633 0);
634 }
635 p_ptr->probing_state = CONFIRMED;
636 port_incr_out_seqno(p_ptr);
637exit:
638 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100639 tipc_port_unlock(p_ptr);
640 tipc_net_route_msg(r_buf);
641 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 buf_discard(buf);
643}
644
645static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
646{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900647 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648
649 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900650 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900652 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 else
654 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
655
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 if (p_ptr->publ.connected) {
657 u32 dport = port_peerport(p_ptr);
658 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900660 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
661 tipc_zone(destnode), tipc_cluster(destnode),
662 tipc_node(destnode), dport);
663 if (p_ptr->publ.conn_type != 0)
664 tipc_printf(buf, " via {%u,%u}",
665 p_ptr->publ.conn_type,
666 p_ptr->publ.conn_instance);
667 }
668 else if (p_ptr->publ.published) {
669 tipc_printf(buf, " bound to");
670 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 if (publ->lower == publ->upper)
672 tipc_printf(buf, " {%u,%u}", publ->type,
673 publ->lower);
674 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900675 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 }
678 }
679 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680}
681
682#define MAX_PORT_QUERY 32768
683
Per Liden4323add2006-01-18 00:38:21 +0100684struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685{
686 struct sk_buff *buf;
687 struct tlv_desc *rep_tlv;
688 struct print_buf pb;
689 struct port *p_ptr;
690 int str_len;
691
Per Liden4323add2006-01-18 00:38:21 +0100692 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 if (!buf)
694 return NULL;
695 rep_tlv = (struct tlv_desc *)buf->data;
696
Per Liden4323add2006-01-18 00:38:21 +0100697 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
698 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 list_for_each_entry(p_ptr, &ports, port_list) {
700 spin_lock_bh(p_ptr->publ.lock);
701 port_print(p_ptr, &pb, 0);
702 spin_unlock_bh(p_ptr->publ.lock);
703 }
Per Liden4323add2006-01-18 00:38:21 +0100704 spin_unlock_bh(&tipc_port_list_lock);
705 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706
707 skb_put(buf, TLV_SPACE(str_len));
708 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
709
710 return buf;
711}
712
Per Liden4323add2006-01-18 00:38:21 +0100713void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714{
715 struct port *p_ptr;
716 struct tipc_msg *msg;
717
Per Liden4323add2006-01-18 00:38:21 +0100718 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 list_for_each_entry(p_ptr, &ports, port_list) {
720 msg = &p_ptr->publ.phdr;
721 if (msg_orignode(msg) == tipc_own_addr)
722 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700723 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 msg_set_orignode(msg, tipc_own_addr);
725 }
Per Liden4323add2006-01-18 00:38:21 +0100726 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727}
728
729
730/*
731 * port_dispatcher_sigh(): Signal handler for messages destinated
732 * to the tipc_port interface.
733 */
734
735static void port_dispatcher_sigh(void *dummy)
736{
737 struct sk_buff *buf;
738
739 spin_lock_bh(&queue_lock);
740 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800741 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 spin_unlock_bh(&queue_lock);
743
744 while (buf) {
745 struct port *p_ptr;
746 struct user_port *up_ptr;
747 struct tipc_portid orig;
748 struct tipc_name_seq dseq;
749 void *usr_handle;
750 int connected;
751 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700752 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753
754 struct sk_buff *next = buf->next;
755 struct tipc_msg *msg = buf_msg(buf);
756 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900757
Allan Stephens96882432006-06-25 23:38:58 -0700758 message_type = msg_type(msg);
759 if (message_type > TIPC_DIRECT_MSG)
760 goto reject; /* Unsupported message type */
761
Per Liden4323add2006-01-18 00:38:21 +0100762 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700763 if (!p_ptr)
764 goto reject; /* Port deleted while msg in queue */
765
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 orig.ref = msg_origport(msg);
767 orig.node = msg_orignode(msg);
768 up_ptr = p_ptr->user_port;
769 usr_handle = up_ptr->usr_handle;
770 connected = p_ptr->publ.connected;
771 published = p_ptr->publ.published;
772
773 if (unlikely(msg_errcode(msg)))
774 goto err;
775
Allan Stephens96882432006-06-25 23:38:58 -0700776 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900777
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 case TIPC_CONN_MSG:{
779 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
780 u32 peer_port = port_peerport(p_ptr);
781 u32 peer_node = port_peernode(p_ptr);
782
Julia Lawall4cec72c2008-01-08 23:48:20 -0800783 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700784 if (unlikely(!cb))
785 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700787 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700789 } else if ((msg_origport(msg) != peer_port) ||
790 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900792 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900794 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 p_ptr->publ.conn_unacked);
796 skb_pull(buf, msg_hdr_sz(msg));
797 cb(usr_handle, dref, &buf, msg_data(msg),
798 msg_data_sz(msg));
799 break;
800 }
801 case TIPC_DIRECT_MSG:{
802 tipc_msg_event cb = up_ptr->msg_cb;
803
Julia Lawall4cec72c2008-01-08 23:48:20 -0800804 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700805 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 goto reject;
807 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900808 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 msg_data_sz(msg), msg_importance(msg),
810 &orig);
811 break;
812 }
Allan Stephens96882432006-06-25 23:38:58 -0700813 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 case TIPC_NAMED_MSG:{
815 tipc_named_msg_event cb = up_ptr->named_msg_cb;
816
Julia Lawall4cec72c2008-01-08 23:48:20 -0800817 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700818 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 goto reject;
820 dseq.type = msg_nametype(msg);
821 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700822 dseq.upper = (message_type == TIPC_NAMED_MSG)
823 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900825 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 msg_data_sz(msg), msg_importance(msg),
827 &orig, &dseq);
828 break;
829 }
830 }
831 if (buf)
832 buf_discard(buf);
833 buf = next;
834 continue;
835err:
Allan Stephens96882432006-06-25 23:38:58 -0700836 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900837
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900839 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 up_ptr->conn_err_cb;
841 u32 peer_port = port_peerport(p_ptr);
842 u32 peer_node = port_peernode(p_ptr);
843
Julia Lawall4cec72c2008-01-08 23:48:20 -0800844 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700845 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700847 if ((msg_origport(msg) != peer_port) ||
848 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 break;
850 tipc_disconnect(dref);
851 skb_pull(buf, msg_hdr_sz(msg));
852 cb(usr_handle, dref, &buf, msg_data(msg),
853 msg_data_sz(msg), msg_errcode(msg));
854 break;
855 }
856 case TIPC_DIRECT_MSG:{
857 tipc_msg_err_event cb = up_ptr->err_cb;
858
Julia Lawall4cec72c2008-01-08 23:48:20 -0800859 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700860 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 break;
862 skb_pull(buf, msg_hdr_sz(msg));
863 cb(usr_handle, dref, &buf, msg_data(msg),
864 msg_data_sz(msg), msg_errcode(msg), &orig);
865 break;
866 }
Allan Stephens96882432006-06-25 23:38:58 -0700867 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900869 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 up_ptr->named_err_cb;
871
Julia Lawall4cec72c2008-01-08 23:48:20 -0800872 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700873 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 break;
875 dseq.type = msg_nametype(msg);
876 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700877 dseq.upper = (message_type == TIPC_NAMED_MSG)
878 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900880 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 msg_data_sz(msg), msg_errcode(msg), &dseq);
882 break;
883 }
884 }
885 if (buf)
886 buf_discard(buf);
887 buf = next;
888 continue;
889reject:
890 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
891 buf = next;
892 }
893}
894
895/*
896 * port_dispatcher(): Dispatcher for messages destinated
897 * to the tipc_port interface. Called with port locked.
898 */
899
900static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
901{
902 buf->next = NULL;
903 spin_lock_bh(&queue_lock);
904 if (msg_queue_head) {
905 msg_queue_tail->next = buf;
906 msg_queue_tail = buf;
907 } else {
908 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100909 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 }
911 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700912 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913}
914
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900915/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900917 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 */
919
920static void port_wakeup_sh(unsigned long ref)
921{
922 struct port *p_ptr;
923 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800924 tipc_continue_event cb = NULL;
925 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926
Per Liden4323add2006-01-18 00:38:21 +0100927 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 if (p_ptr) {
929 up_ptr = p_ptr->user_port;
930 if (up_ptr) {
931 cb = up_ptr->continue_event_cb;
932 uh = up_ptr->usr_handle;
933 }
Per Liden4323add2006-01-18 00:38:21 +0100934 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 }
936 if (cb)
937 cb(uh, ref);
938}
939
940
941static void port_wakeup(struct tipc_port *p_ptr)
942{
Per Liden4323add2006-01-18 00:38:21 +0100943 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944}
945
946void tipc_acknowledge(u32 ref, u32 ack)
947{
948 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800949 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950
Per Liden4323add2006-01-18 00:38:21 +0100951 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 if (!p_ptr)
953 return;
954 if (p_ptr->publ.connected) {
955 p_ptr->publ.conn_unacked -= ack;
956 buf = port_build_proto_msg(port_peerport(p_ptr),
957 port_peernode(p_ptr),
958 ref,
959 tipc_own_addr,
960 CONN_MANAGER,
961 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900962 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 port_out_seqno(p_ptr),
964 ack);
965 }
Per Liden4323add2006-01-18 00:38:21 +0100966 tipc_port_unlock(p_ptr);
967 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968}
969
970/*
971 * tipc_createport(): user level call. Will add port to
972 * registry if non-zero user_ref.
973 */
974
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900975int tipc_createport(u32 user_ref,
976 void *usr_handle,
977 unsigned int importance,
978 tipc_msg_err_event error_cb,
979 tipc_named_msg_err_event named_error_cb,
980 tipc_conn_shutdown_event conn_error_cb,
981 tipc_msg_event msg_cb,
982 tipc_named_msg_event named_msg_cb,
983 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 tipc_continue_event continue_event_cb,/* May be zero */
985 u32 *portref)
986{
987 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900988 struct port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700990 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700991 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700992 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 return -ENOMEM;
994 }
Allan Stephens0ea52242008-07-14 22:42:19 -0700995 p_ptr = (struct port *)tipc_createport_raw(NULL, port_dispatcher,
996 port_wakeup, importance);
997 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998 kfree(up_ptr);
999 return -ENOMEM;
1000 }
1001
1002 p_ptr->user_port = up_ptr;
1003 up_ptr->user_ref = user_ref;
1004 up_ptr->usr_handle = usr_handle;
1005 up_ptr->ref = p_ptr->publ.ref;
1006 up_ptr->err_cb = error_cb;
1007 up_ptr->named_err_cb = named_error_cb;
1008 up_ptr->conn_err_cb = conn_error_cb;
1009 up_ptr->msg_cb = msg_cb;
1010 up_ptr->named_msg_cb = named_msg_cb;
1011 up_ptr->conn_msg_cb = conn_msg_cb;
1012 up_ptr->continue_event_cb = continue_event_cb;
1013 INIT_LIST_HEAD(&up_ptr->uport_list);
Per Liden4323add2006-01-18 00:38:21 +01001014 tipc_reg_add_port(up_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 *portref = p_ptr->publ.ref;
Per Liden4323add2006-01-18 00:38:21 +01001016 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001017 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018}
1019
1020int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1021{
1022 id->ref = ref;
1023 id->node = tipc_own_addr;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001024 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025}
1026
1027int tipc_portimportance(u32 ref, unsigned int *importance)
1028{
1029 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001030
Per Liden4323add2006-01-18 00:38:21 +01001031 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 if (!p_ptr)
1033 return -EINVAL;
1034 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001035 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001036 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037}
1038
1039int tipc_set_portimportance(u32 ref, unsigned int imp)
1040{
1041 struct port *p_ptr;
1042
1043 if (imp > TIPC_CRITICAL_IMPORTANCE)
1044 return -EINVAL;
1045
Per Liden4323add2006-01-18 00:38:21 +01001046 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 if (!p_ptr)
1048 return -EINVAL;
1049 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001050 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001051 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052}
1053
1054
1055int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1056{
1057 struct port *p_ptr;
1058 struct publication *publ;
1059 u32 key;
1060 int res = -EINVAL;
1061
Per Liden4323add2006-01-18 00:38:21 +01001062 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001063 if (!p_ptr)
1064 return -EINVAL;
1065
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1067 "lower = %u, upper = %u\n",
1068 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 if (p_ptr->publ.connected)
1070 goto exit;
1071 if (seq->lower > seq->upper)
1072 goto exit;
1073 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1074 goto exit;
1075 key = ref + p_ptr->pub_count + 1;
1076 if (key == ref) {
1077 res = -EADDRINUSE;
1078 goto exit;
1079 }
Per Liden4323add2006-01-18 00:38:21 +01001080 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1081 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 if (publ) {
1083 list_add(&publ->pport_list, &p_ptr->publications);
1084 p_ptr->pub_count++;
1085 p_ptr->publ.published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001086 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087 }
1088exit:
Per Liden4323add2006-01-18 00:38:21 +01001089 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 return res;
1091}
1092
1093int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1094{
1095 struct port *p_ptr;
1096 struct publication *publ;
1097 struct publication *tpubl;
1098 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001099
Per Liden4323add2006-01-18 00:38:21 +01001100 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 if (!p_ptr)
1102 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001104 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001106 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001107 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001109 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001111 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 &p_ptr->publications, pport_list) {
1113 if (publ->scope != scope)
1114 continue;
1115 if (publ->type != seq->type)
1116 continue;
1117 if (publ->lower != seq->lower)
1118 continue;
1119 if (publ->upper != seq->upper)
1120 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001121 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001122 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001123 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 break;
1125 }
1126 }
1127 if (list_empty(&p_ptr->publications))
1128 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001129 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 return res;
1131}
1132
1133int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1134{
1135 struct port *p_ptr;
1136 struct tipc_msg *msg;
1137 int res = -EINVAL;
1138
Per Liden4323add2006-01-18 00:38:21 +01001139 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 if (!p_ptr)
1141 return -EINVAL;
1142 if (p_ptr->publ.published || p_ptr->publ.connected)
1143 goto exit;
1144 if (!peer->ref)
1145 goto exit;
1146
1147 msg = &p_ptr->publ.phdr;
1148 msg_set_destnode(msg, peer->node);
1149 msg_set_destport(msg, peer->ref);
1150 msg_set_orignode(msg, tipc_own_addr);
1151 msg_set_origport(msg, p_ptr->publ.ref);
1152 msg_set_transp_seqno(msg, 42);
1153 msg_set_type(msg, TIPC_CONN_MSG);
1154 if (!may_route(peer->node))
1155 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1156 else
1157 msg_set_hdr_sz(msg, LONG_H_SIZE);
1158
1159 p_ptr->probing_interval = PROBING_INTERVAL;
1160 p_ptr->probing_state = CONFIRMED;
1161 p_ptr->publ.connected = 1;
1162 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1163
Per Liden4323add2006-01-18 00:38:21 +01001164 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001165 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001167 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168exit:
Per Liden4323add2006-01-18 00:38:21 +01001169 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001170 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 return res;
1172}
1173
Allan Stephens0c3141e2008-04-15 00:22:02 -07001174/**
1175 * tipc_disconnect_port - disconnect port from peer
1176 *
1177 * Port must be locked.
1178 */
1179
1180int tipc_disconnect_port(struct tipc_port *tp_ptr)
1181{
1182 int res;
1183
1184 if (tp_ptr->connected) {
1185 tp_ptr->connected = 0;
1186 /* let timer expire on it's own to avoid deadlock! */
1187 tipc_nodesub_unsubscribe(
1188 &((struct port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001189 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001190 } else {
1191 res = -ENOTCONN;
1192 }
1193 return res;
1194}
1195
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196/*
1197 * tipc_disconnect(): Disconnect port form peer.
1198 * This is a node local operation.
1199 */
1200
1201int tipc_disconnect(u32 ref)
1202{
1203 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001204 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205
Per Liden4323add2006-01-18 00:38:21 +01001206 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 if (!p_ptr)
1208 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001209 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001210 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 return res;
1212}
1213
1214/*
1215 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1216 */
1217int tipc_shutdown(u32 ref)
1218{
1219 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001220 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221
Per Liden4323add2006-01-18 00:38:21 +01001222 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 if (!p_ptr)
1224 return -EINVAL;
1225
1226 if (p_ptr->publ.connected) {
1227 u32 imp = msg_importance(&p_ptr->publ.phdr);
1228 if (imp < TIPC_CRITICAL_IMPORTANCE)
1229 imp++;
1230 buf = port_build_proto_msg(port_peerport(p_ptr),
1231 port_peernode(p_ptr),
1232 ref,
1233 tipc_own_addr,
1234 imp,
1235 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001236 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 port_out_seqno(p_ptr),
1238 0);
1239 }
Per Liden4323add2006-01-18 00:38:21 +01001240 tipc_port_unlock(p_ptr);
1241 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 return tipc_disconnect(ref);
1243}
1244
1245int tipc_isconnected(u32 ref, int *isconnected)
1246{
1247 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001248
Per Liden4323add2006-01-18 00:38:21 +01001249 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 if (!p_ptr)
1251 return -EINVAL;
1252 *isconnected = p_ptr->publ.connected;
Per Liden4323add2006-01-18 00:38:21 +01001253 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001254 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255}
1256
1257int tipc_peer(u32 ref, struct tipc_portid *peer)
1258{
1259 struct port *p_ptr;
1260 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001261
Per Liden4323add2006-01-18 00:38:21 +01001262 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 if (!p_ptr)
1264 return -EINVAL;
1265 if (p_ptr->publ.connected) {
1266 peer->ref = port_peerport(p_ptr);
1267 peer->node = port_peernode(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001268 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269 } else
1270 res = -ENOTCONN;
Per Liden4323add2006-01-18 00:38:21 +01001271 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 return res;
1273}
1274
1275int tipc_ref_valid(u32 ref)
1276{
1277 /* Works irrespective of type */
Per Liden4323add2006-01-18 00:38:21 +01001278 return !!tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279}
1280
1281
1282/*
Per Liden4323add2006-01-18 00:38:21 +01001283 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284 * message for this node.
1285 */
1286
Per Liden4323add2006-01-18 00:38:21 +01001287int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 struct iovec const *msg_sect)
1289{
1290 struct sk_buff *buf;
1291 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001292
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001293 res = tipc_msg_build(&sender->publ.phdr, msg_sect, num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001294 MAX_MSG_SIZE, !sender->user_port, &buf);
1295 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001296 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 return res;
1298}
1299
1300/**
1301 * tipc_send - send message sections on connection
1302 */
1303
1304int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1305{
1306 struct port *p_ptr;
1307 u32 destnode;
1308 int res;
1309
Per Liden4323add2006-01-18 00:38:21 +01001310 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 if (!p_ptr || !p_ptr->publ.connected)
1312 return -EINVAL;
1313
1314 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001315 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 destnode = port_peernode(p_ptr);
1317 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001318 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1319 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 else
Per Liden4323add2006-01-18 00:38:21 +01001321 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322
1323 if (likely(res != -ELINKCONG)) {
1324 port_incr_out_seqno(p_ptr);
1325 p_ptr->publ.congested = 0;
1326 p_ptr->sent++;
1327 return res;
1328 }
1329 }
1330 if (port_unreliable(p_ptr)) {
1331 p_ptr->publ.congested = 0;
1332 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001333 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 }
1335 return -ELINKCONG;
1336}
1337
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001338/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 * tipc_send_buf - send message buffer on connection
1340 */
1341
1342int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1343{
1344 struct port *p_ptr;
1345 struct tipc_msg *msg;
1346 u32 destnode;
1347 u32 hsz;
1348 u32 sz;
1349 u32 res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001350
Per Liden4323add2006-01-18 00:38:21 +01001351 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 if (!p_ptr || !p_ptr->publ.connected)
1353 return -EINVAL;
1354
1355 msg = &p_ptr->publ.phdr;
1356 hsz = msg_hdr_sz(msg);
1357 sz = hsz + dsz;
1358 msg_set_size(msg, sz);
1359 if (skb_cow(buf, hsz))
1360 return -ENOMEM;
1361
1362 skb_push(buf, hsz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001363 skb_copy_to_linear_data(buf, msg, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 destnode = msg_destnode(msg);
1365 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001366 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 if (likely(destnode != tipc_own_addr))
1368 res = tipc_send_buf_fast(buf, destnode);
1369 else {
Per Liden4323add2006-01-18 00:38:21 +01001370 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 res = sz;
1372 }
1373 if (likely(res != -ELINKCONG)) {
1374 port_incr_out_seqno(p_ptr);
1375 p_ptr->sent++;
1376 p_ptr->publ.congested = 0;
1377 return res;
1378 }
1379 }
1380 if (port_unreliable(p_ptr)) {
1381 p_ptr->publ.congested = 0;
1382 return dsz;
1383 }
1384 return -ELINKCONG;
1385}
1386
1387/**
1388 * tipc_forward2name - forward message sections to port name
1389 */
1390
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391int tipc_forward2name(u32 ref,
1392 struct tipc_name const *name,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001394 u32 num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001396 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 unsigned int importance)
1398{
1399 struct port *p_ptr;
1400 struct tipc_msg *msg;
1401 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001402 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 int res;
1404
Per Liden4323add2006-01-18 00:38:21 +01001405 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 if (!p_ptr || p_ptr->publ.connected)
1407 return -EINVAL;
1408
1409 msg = &p_ptr->publ.phdr;
1410 msg_set_type(msg, TIPC_NAMED_MSG);
1411 msg_set_orignode(msg, orig->node);
1412 msg_set_origport(msg, orig->ref);
1413 msg_set_hdr_sz(msg, LONG_H_SIZE);
1414 msg_set_nametype(msg, name->type);
1415 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001416 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1418 msg_set_importance(msg,importance);
Per Liden4323add2006-01-18 00:38:21 +01001419 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 msg_set_destnode(msg, destnode);
1421 msg_set_destport(msg, destport);
1422
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001423 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 p_ptr->sent++;
1425 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001426 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001427 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001428 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 if (likely(res != -ELINKCONG))
1430 return res;
1431 if (port_unreliable(p_ptr)) {
1432 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001433 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 }
1435 return -ELINKCONG;
1436 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001437 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001438 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439}
1440
1441/**
1442 * tipc_send2name - send message sections to port name
1443 */
1444
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001445int tipc_send2name(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 struct tipc_name const *name,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001447 unsigned int domain,
1448 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 struct iovec const *msg_sect)
1450{
1451 struct tipc_portid orig;
1452
1453 orig.ref = ref;
1454 orig.node = tipc_own_addr;
1455 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1456 TIPC_PORT_IMPORTANCE);
1457}
1458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001459/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 * tipc_forward_buf2name - forward message buffer to port name
1461 */
1462
1463int tipc_forward_buf2name(u32 ref,
1464 struct tipc_name const *name,
1465 u32 domain,
1466 struct sk_buff *buf,
1467 unsigned int dsz,
1468 struct tipc_portid const *orig,
1469 unsigned int importance)
1470{
1471 struct port *p_ptr;
1472 struct tipc_msg *msg;
1473 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001474 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 int res;
1476
Per Liden4323add2006-01-18 00:38:21 +01001477 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 if (!p_ptr || p_ptr->publ.connected)
1479 return -EINVAL;
1480
1481 msg = &p_ptr->publ.phdr;
1482 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1483 msg_set_importance(msg, importance);
1484 msg_set_type(msg, TIPC_NAMED_MSG);
1485 msg_set_orignode(msg, orig->node);
1486 msg_set_origport(msg, orig->ref);
1487 msg_set_nametype(msg, name->type);
1488 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001489 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490 msg_set_hdr_sz(msg, LONG_H_SIZE);
1491 msg_set_size(msg, LONG_H_SIZE + dsz);
Per Liden4323add2006-01-18 00:38:21 +01001492 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493 msg_set_destnode(msg, destnode);
1494 msg_set_destport(msg, destport);
1495 msg_dbg(msg, "forw2name ==> ");
1496 if (skb_cow(buf, LONG_H_SIZE))
1497 return -ENOMEM;
1498 skb_push(buf, LONG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001499 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 msg_dbg(buf_msg(buf),"PREP:");
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001501 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 p_ptr->sent++;
1503 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001504 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 res = tipc_send_buf_fast(buf, destnode);
1506 if (likely(res != -ELINKCONG))
1507 return res;
1508 if (port_unreliable(p_ptr))
1509 return dsz;
1510 return -ELINKCONG;
1511 }
1512 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1513}
1514
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001515/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 * tipc_send_buf2name - send message buffer to port name
1517 */
1518
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001519int tipc_send_buf2name(u32 ref,
1520 struct tipc_name const *dest,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001522 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001523 unsigned int dsz)
1524{
1525 struct tipc_portid orig;
1526
1527 orig.ref = ref;
1528 orig.node = tipc_own_addr;
1529 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1530 TIPC_PORT_IMPORTANCE);
1531}
1532
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001533/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 * tipc_forward2port - forward message sections to port identity
1535 */
1536
1537int tipc_forward2port(u32 ref,
1538 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001539 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001541 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542 unsigned int importance)
1543{
1544 struct port *p_ptr;
1545 struct tipc_msg *msg;
1546 int res;
1547
Per Liden4323add2006-01-18 00:38:21 +01001548 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001549 if (!p_ptr || p_ptr->publ.connected)
1550 return -EINVAL;
1551
1552 msg = &p_ptr->publ.phdr;
1553 msg_set_type(msg, TIPC_DIRECT_MSG);
1554 msg_set_orignode(msg, orig->node);
1555 msg_set_origport(msg, orig->ref);
1556 msg_set_destnode(msg, dest->node);
1557 msg_set_destport(msg, dest->ref);
1558 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1559 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1560 msg_set_importance(msg, importance);
1561 p_ptr->sent++;
1562 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001563 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1564 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 if (likely(res != -ELINKCONG))
1566 return res;
1567 if (port_unreliable(p_ptr)) {
1568 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001569 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570 }
1571 return -ELINKCONG;
1572}
1573
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001574/**
1575 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576 */
1577
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001578int tipc_send2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001580 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581 struct iovec const *msg_sect)
1582{
1583 struct tipc_portid orig;
1584
1585 orig.ref = ref;
1586 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001587 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588 TIPC_PORT_IMPORTANCE);
1589}
1590
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001591/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001592 * tipc_forward_buf2port - forward message buffer to port identity
1593 */
1594int tipc_forward_buf2port(u32 ref,
1595 struct tipc_portid const *dest,
1596 struct sk_buff *buf,
1597 unsigned int dsz,
1598 struct tipc_portid const *orig,
1599 unsigned int importance)
1600{
1601 struct port *p_ptr;
1602 struct tipc_msg *msg;
1603 int res;
1604
Per Liden4323add2006-01-18 00:38:21 +01001605 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 if (!p_ptr || p_ptr->publ.connected)
1607 return -EINVAL;
1608
1609 msg = &p_ptr->publ.phdr;
1610 msg_set_type(msg, TIPC_DIRECT_MSG);
1611 msg_set_orignode(msg, orig->node);
1612 msg_set_origport(msg, orig->ref);
1613 msg_set_destnode(msg, dest->node);
1614 msg_set_destport(msg, dest->ref);
1615 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1616 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1617 msg_set_importance(msg, importance);
1618 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1619 if (skb_cow(buf, DIR_MSG_H_SIZE))
1620 return -ENOMEM;
1621
1622 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001623 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 msg_dbg(msg, "buf2port: ");
1625 p_ptr->sent++;
1626 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001627 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628 res = tipc_send_buf_fast(buf, dest->node);
1629 if (likely(res != -ELINKCONG))
1630 return res;
1631 if (port_unreliable(p_ptr))
1632 return dsz;
1633 return -ELINKCONG;
1634}
1635
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001636/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637 * tipc_send_buf2port - send message buffer to port identity
1638 */
1639
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001640int tipc_send_buf2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001641 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001642 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 unsigned int dsz)
1644{
1645 struct tipc_portid orig;
1646
1647 orig.ref = ref;
1648 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001649 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001650 TIPC_PORT_IMPORTANCE);
1651}
1652