blob: 4dfef9e798e3e13aa47ed8b8071f47bef18809c7 [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
5 * Copyright (c) 2004-2007, 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);
119 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
120 !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 Stephens7ef43eb2008-05-12 15:42:28 -0700216 * Returns port reference, or 0 if unable to create it
217 *
218 * Note: The newly created port is returned in the locked state.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 */
220
221u32 tipc_createport_raw(void *usr_handle,
222 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
223 void (*wakeup)(struct tipc_port *),
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700224 const u32 importance,
225 struct tipc_port **tp_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226{
227 struct port *p_ptr;
228 struct tipc_msg *msg;
229 u32 ref;
230
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700231 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700232 if (!p_ptr) {
233 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 return 0;
235 }
Per Liden4323add2006-01-18 00:38:21 +0100236 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700238 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 kfree(p_ptr);
240 return 0;
241 }
242
Allan Stephens05646c92007-06-10 17:25:24 -0700243 p_ptr->publ.usr_handle = usr_handle;
244 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 p_ptr->publ.ref = ref;
246 msg = &p_ptr->publ.phdr;
Allan Stephens06d82c92008-03-06 15:06:55 -0800247 msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE,
248 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 msg_set_orignode(msg, tipc_own_addr);
250 msg_set_prevnode(msg, tipc_own_addr);
251 msg_set_origport(msg, ref);
252 msg_set_importance(msg,importance);
253 p_ptr->last_in_seqno = 41;
254 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 INIT_LIST_HEAD(&p_ptr->wait_list);
256 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800257 p_ptr->congested_link = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 p_ptr->dispatcher = dispatcher;
259 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800260 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100262 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 INIT_LIST_HEAD(&p_ptr->publications);
264 INIT_LIST_HEAD(&p_ptr->port_list);
265 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100266 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700267 *tp_ptr = &p_ptr->publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 return ref;
269}
270
271int tipc_deleteport(u32 ref)
272{
273 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800274 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800276 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100277 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900278 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279 return -EINVAL;
280
Per Liden4323add2006-01-18 00:38:21 +0100281 tipc_ref_discard(ref);
282 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283
284 k_cancel_timer(&p_ptr->timer);
285 if (p_ptr->publ.connected) {
286 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100287 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288 }
289 if (p_ptr->user_port) {
Per Liden4323add2006-01-18 00:38:21 +0100290 tipc_reg_remove_port(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 kfree(p_ptr->user_port);
292 }
293
Per Liden4323add2006-01-18 00:38:21 +0100294 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295 list_del(&p_ptr->port_list);
296 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100297 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 k_term_timer(&p_ptr->timer);
299 kfree(p_ptr);
300 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100301 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 return TIPC_OK;
303}
304
305/**
306 * tipc_get_port() - return port associated with 'ref'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900307 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 * Note: Port is not locked.
309 */
310
311struct tipc_port *tipc_get_port(const u32 ref)
312{
Per Liden4323add2006-01-18 00:38:21 +0100313 return (struct tipc_port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314}
315
316/**
317 * tipc_get_handle - return user handle associated to port 'ref'
318 */
319
320void *tipc_get_handle(const u32 ref)
321{
322 struct port *p_ptr;
323 void * handle;
324
Per Liden4323add2006-01-18 00:38:21 +0100325 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 if (!p_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800327 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 handle = p_ptr->publ.usr_handle;
Per Liden4323add2006-01-18 00:38:21 +0100329 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 return handle;
331}
332
Sam Ravnborg05790c62006-03-20 22:37:04 -0800333static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334{
335 return msg_src_droppable(&p_ptr->publ.phdr);
336}
337
338int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
339{
340 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900341
Per Liden4323add2006-01-18 00:38:21 +0100342 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 if (!p_ptr)
344 return -EINVAL;
345 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800346 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 return TIPC_OK;
348}
349
350int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
351{
352 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900353
Per Liden4323add2006-01-18 00:38:21 +0100354 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 if (!p_ptr)
356 return -EINVAL;
357 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100358 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 return TIPC_OK;
360}
361
Sam Ravnborg05790c62006-03-20 22:37:04 -0800362static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363{
364 return msg_dest_droppable(&p_ptr->publ.phdr);
365}
366
367int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
368{
369 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900370
Per Liden4323add2006-01-18 00:38:21 +0100371 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 if (!p_ptr)
373 return -EINVAL;
374 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800375 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 return TIPC_OK;
377}
378
379int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
380{
381 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900382
Per Liden4323add2006-01-18 00:38:21 +0100383 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 if (!p_ptr)
385 return -EINVAL;
386 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100387 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 return TIPC_OK;
389}
390
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900391/*
392 * port_build_proto_msg(): build a port level protocol
393 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 * tipc_port lock on.
395 */
396static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
397 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900398 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 u32 seqno, u32 ack)
400{
401 struct sk_buff *buf;
402 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900403
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 buf = buf_acquire(LONG_H_SIZE);
405 if (buf) {
406 msg = buf_msg(buf);
407 msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
408 msg_set_destport(msg, destport);
409 msg_set_origport(msg, origport);
410 msg_set_destnode(msg, destnode);
411 msg_set_orignode(msg, orignode);
412 msg_set_transp_seqno(msg, seqno);
413 msg_set_msgcnt(msg, ack);
414 msg_dbg(msg, "PORT>SEND>:");
415 }
416 return buf;
417}
418
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419int tipc_reject_msg(struct sk_buff *buf, u32 err)
420{
421 struct tipc_msg *msg = buf_msg(buf);
422 struct sk_buff *rbuf;
423 struct tipc_msg *rmsg;
424 int hdr_sz;
425 u32 imp = msg_importance(msg);
426 u32 data_sz = msg_data_sz(msg);
427
428 if (data_sz > MAX_REJECT_SIZE)
429 data_sz = MAX_REJECT_SIZE;
430 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
431 imp++;
432 msg_dbg(msg, "port->rej: ");
433
434 /* discard rejected message if it shouldn't be returned to sender */
435 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
436 buf_discard(buf);
437 return data_sz;
438 }
439
440 /* construct rejected message */
441 if (msg_mcast(msg))
442 hdr_sz = MCAST_H_SIZE;
443 else
444 hdr_sz = LONG_H_SIZE;
445 rbuf = buf_acquire(data_sz + hdr_sz);
446 if (rbuf == NULL) {
447 buf_discard(buf);
448 return data_sz;
449 }
450 rmsg = buf_msg(rbuf);
451 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
452 msg_set_destport(rmsg, msg_origport(msg));
453 msg_set_prevnode(rmsg, tipc_own_addr);
454 msg_set_origport(rmsg, msg_destport(msg));
455 if (msg_short(msg))
456 msg_set_orignode(rmsg, tipc_own_addr);
457 else
458 msg_set_orignode(rmsg, msg_destnode(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459 msg_set_size(rmsg, data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 msg_set_nametype(rmsg, msg_nametype(msg));
461 msg_set_nameinst(rmsg, msg_nameinst(msg));
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300462 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463
464 /* send self-abort message when rejecting on a connected port */
465 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800466 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100467 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468
469 if (p_ptr) {
470 if (p_ptr->publ.connected)
471 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100472 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 }
Per Liden4323add2006-01-18 00:38:21 +0100474 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 }
476
477 /* send rejected message */
478 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100479 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 return data_sz;
481}
482
Per Liden4323add2006-01-18 00:38:21 +0100483int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
484 struct iovec const *msg_sect, u32 num_sect,
485 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486{
487 struct sk_buff *buf;
488 int res;
489
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 !p_ptr->user_port, &buf);
492 if (!buf)
493 return res;
494
495 return tipc_reject_msg(buf, err);
496}
497
498static void port_timeout(unsigned long ref)
499{
Per Liden4323add2006-01-18 00:38:21 +0100500 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800501 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502
Allan Stephens065fd172006-10-16 21:38:05 -0700503 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 return;
505
Allan Stephens065fd172006-10-16 21:38:05 -0700506 if (!p_ptr->publ.connected) {
507 tipc_port_unlock(p_ptr);
508 return;
509 }
510
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 /* Last probe answered ? */
512 if (p_ptr->probing_state == PROBING) {
513 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
514 } else {
515 buf = port_build_proto_msg(port_peerport(p_ptr),
516 port_peernode(p_ptr),
517 p_ptr->publ.ref,
518 tipc_own_addr,
519 CONN_MANAGER,
520 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900521 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 port_out_seqno(p_ptr),
523 0);
524 port_incr_out_seqno(p_ptr);
525 p_ptr->probing_state = PROBING;
526 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
527 }
Per Liden4323add2006-01-18 00:38:21 +0100528 tipc_port_unlock(p_ptr);
529 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530}
531
532
533static void port_handle_node_down(unsigned long ref)
534{
Per Liden4323add2006-01-18 00:38:21 +0100535 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800536 struct sk_buff* buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537
538 if (!p_ptr)
539 return;
540 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100541 tipc_port_unlock(p_ptr);
542 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543}
544
545
546static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
547{
548 u32 imp = msg_importance(&p_ptr->publ.phdr);
549
550 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800551 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 if (imp < TIPC_CRITICAL_IMPORTANCE)
553 imp++;
554 return port_build_proto_msg(p_ptr->publ.ref,
555 tipc_own_addr,
556 port_peerport(p_ptr),
557 port_peernode(p_ptr),
558 imp,
559 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 p_ptr->last_in_seqno + 1,
562 0);
563}
564
565
566static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
567{
568 u32 imp = msg_importance(&p_ptr->publ.phdr);
569
570 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800571 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 if (imp < TIPC_CRITICAL_IMPORTANCE)
573 imp++;
574 return port_build_proto_msg(port_peerport(p_ptr),
575 port_peernode(p_ptr),
576 p_ptr->publ.ref,
577 tipc_own_addr,
578 imp,
579 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900580 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 port_out_seqno(p_ptr),
582 0);
583}
584
Per Liden4323add2006-01-18 00:38:21 +0100585void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586{
587 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100588 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800590 struct sk_buff *r_buf = NULL;
591 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592
593 msg_dbg(msg, "PORT<RECV<:");
594
595 if (!p_ptr) {
596 err = TIPC_ERR_NO_PORT;
597 } else if (p_ptr->publ.connected) {
598 if (port_peernode(p_ptr) != msg_orignode(msg))
599 err = TIPC_ERR_NO_PORT;
600 if (port_peerport(p_ptr) != msg_origport(msg))
601 err = TIPC_ERR_NO_PORT;
602 if (!err && msg_routed(msg)) {
603 u32 seqno = msg_transp_seqno(msg);
604 u32 myno = ++p_ptr->last_in_seqno;
605 if (seqno != myno) {
606 err = TIPC_ERR_NO_PORT;
607 abort_buf = port_build_self_abort_msg(p_ptr, err);
608 }
609 }
610 if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 p_ptr->publ.congested &&
613 p_ptr->wakeup;
614 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100615 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 goto exit;
617 p_ptr->publ.congested = 0;
618 if (!wakeup)
619 goto exit;
620 p_ptr->wakeup(&p_ptr->publ);
621 goto exit;
622 }
623 } else if (p_ptr->publ.published) {
624 err = TIPC_ERR_NO_PORT;
625 }
626 if (err) {
627 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900628 msg_orignode(msg),
629 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800631 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 TIPC_CONN_MSG,
633 err,
634 0,
635 0);
636 goto exit;
637 }
638
639 /* All is fine */
640 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 r_buf = port_build_proto_msg(msg_origport(msg),
642 msg_orignode(msg),
643 msg_destport(msg),
644 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 CONN_MANAGER,
646 CONN_PROBE_REPLY,
647 TIPC_OK,
648 port_out_seqno(p_ptr),
649 0);
650 }
651 p_ptr->probing_state = CONFIRMED;
652 port_incr_out_seqno(p_ptr);
653exit:
654 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100655 tipc_port_unlock(p_ptr);
656 tipc_net_route_msg(r_buf);
657 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658 buf_discard(buf);
659}
660
661static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
662{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664
665 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900666 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900668 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 else
670 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
671
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900672 if (p_ptr->publ.connected) {
673 u32 dport = port_peerport(p_ptr);
674 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900676 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
677 tipc_zone(destnode), tipc_cluster(destnode),
678 tipc_node(destnode), dport);
679 if (p_ptr->publ.conn_type != 0)
680 tipc_printf(buf, " via {%u,%u}",
681 p_ptr->publ.conn_type,
682 p_ptr->publ.conn_instance);
683 }
684 else if (p_ptr->publ.published) {
685 tipc_printf(buf, " bound to");
686 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 if (publ->lower == publ->upper)
688 tipc_printf(buf, " {%u,%u}", publ->type,
689 publ->lower);
690 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900691 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693 }
694 }
695 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696}
697
698#define MAX_PORT_QUERY 32768
699
Per Liden4323add2006-01-18 00:38:21 +0100700struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701{
702 struct sk_buff *buf;
703 struct tlv_desc *rep_tlv;
704 struct print_buf pb;
705 struct port *p_ptr;
706 int str_len;
707
Per Liden4323add2006-01-18 00:38:21 +0100708 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 if (!buf)
710 return NULL;
711 rep_tlv = (struct tlv_desc *)buf->data;
712
Per Liden4323add2006-01-18 00:38:21 +0100713 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
714 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 list_for_each_entry(p_ptr, &ports, port_list) {
716 spin_lock_bh(p_ptr->publ.lock);
717 port_print(p_ptr, &pb, 0);
718 spin_unlock_bh(p_ptr->publ.lock);
719 }
Per Liden4323add2006-01-18 00:38:21 +0100720 spin_unlock_bh(&tipc_port_list_lock);
721 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722
723 skb_put(buf, TLV_SPACE(str_len));
724 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
725
726 return buf;
727}
728
729#if 0
730
731#define MAX_PORT_STATS 2000
732
733struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
734{
735 u32 ref;
736 struct port *p_ptr;
737 struct sk_buff *buf;
738 struct tlv_desc *rep_tlv;
739 struct print_buf pb;
740 int str_len;
741
742 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
743 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
744
745 ref = *(u32 *)TLV_DATA(req_tlv_area);
746 ref = ntohl(ref);
747
Per Liden4323add2006-01-18 00:38:21 +0100748 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 if (!p_ptr)
750 return cfg_reply_error_string("port not found");
751
Per Liden4323add2006-01-18 00:38:21 +0100752 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 if (!buf) {
Per Liden4323add2006-01-18 00:38:21 +0100754 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 return NULL;
756 }
757 rep_tlv = (struct tlv_desc *)buf->data;
758
Per Liden4323add2006-01-18 00:38:21 +0100759 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 port_print(p_ptr, &pb, 1);
761 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
Per Liden4323add2006-01-18 00:38:21 +0100762 tipc_port_unlock(p_ptr);
763 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764
765 skb_put(buf, TLV_SPACE(str_len));
766 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
767
768 return buf;
769}
770
771#endif
772
Per Liden4323add2006-01-18 00:38:21 +0100773void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774{
775 struct port *p_ptr;
776 struct tipc_msg *msg;
777
Per Liden4323add2006-01-18 00:38:21 +0100778 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 list_for_each_entry(p_ptr, &ports, port_list) {
780 msg = &p_ptr->publ.phdr;
781 if (msg_orignode(msg) == tipc_own_addr)
782 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700783 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 msg_set_orignode(msg, tipc_own_addr);
785 }
Per Liden4323add2006-01-18 00:38:21 +0100786 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787}
788
789
790/*
791 * port_dispatcher_sigh(): Signal handler for messages destinated
792 * to the tipc_port interface.
793 */
794
795static void port_dispatcher_sigh(void *dummy)
796{
797 struct sk_buff *buf;
798
799 spin_lock_bh(&queue_lock);
800 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800801 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 spin_unlock_bh(&queue_lock);
803
804 while (buf) {
805 struct port *p_ptr;
806 struct user_port *up_ptr;
807 struct tipc_portid orig;
808 struct tipc_name_seq dseq;
809 void *usr_handle;
810 int connected;
811 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700812 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813
814 struct sk_buff *next = buf->next;
815 struct tipc_msg *msg = buf_msg(buf);
816 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900817
Allan Stephens96882432006-06-25 23:38:58 -0700818 message_type = msg_type(msg);
819 if (message_type > TIPC_DIRECT_MSG)
820 goto reject; /* Unsupported message type */
821
Per Liden4323add2006-01-18 00:38:21 +0100822 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700823 if (!p_ptr)
824 goto reject; /* Port deleted while msg in queue */
825
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 orig.ref = msg_origport(msg);
827 orig.node = msg_orignode(msg);
828 up_ptr = p_ptr->user_port;
829 usr_handle = up_ptr->usr_handle;
830 connected = p_ptr->publ.connected;
831 published = p_ptr->publ.published;
832
833 if (unlikely(msg_errcode(msg)))
834 goto err;
835
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:{
839 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
840 u32 peer_port = port_peerport(p_ptr);
841 u32 peer_node = port_peernode(p_ptr);
842
Julia Lawall4cec72c2008-01-08 23:48:20 -0800843 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700844 if (unlikely(!cb))
845 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700847 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700849 } else if ((msg_origport(msg) != peer_port) ||
850 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900852 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900854 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 p_ptr->publ.conn_unacked);
856 skb_pull(buf, msg_hdr_sz(msg));
857 cb(usr_handle, dref, &buf, msg_data(msg),
858 msg_data_sz(msg));
859 break;
860 }
861 case TIPC_DIRECT_MSG:{
862 tipc_msg_event cb = up_ptr->msg_cb;
863
Julia Lawall4cec72c2008-01-08 23:48:20 -0800864 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700865 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 goto reject;
867 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900868 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 msg_data_sz(msg), msg_importance(msg),
870 &orig);
871 break;
872 }
Allan Stephens96882432006-06-25 23:38:58 -0700873 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 case TIPC_NAMED_MSG:{
875 tipc_named_msg_event cb = up_ptr->named_msg_cb;
876
Julia Lawall4cec72c2008-01-08 23:48:20 -0800877 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700878 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 goto reject;
880 dseq.type = msg_nametype(msg);
881 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700882 dseq.upper = (message_type == TIPC_NAMED_MSG)
883 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900885 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 msg_data_sz(msg), msg_importance(msg),
887 &orig, &dseq);
888 break;
889 }
890 }
891 if (buf)
892 buf_discard(buf);
893 buf = next;
894 continue;
895err:
Allan Stephens96882432006-06-25 23:38:58 -0700896 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900897
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900899 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 up_ptr->conn_err_cb;
901 u32 peer_port = port_peerport(p_ptr);
902 u32 peer_node = port_peernode(p_ptr);
903
Julia Lawall4cec72c2008-01-08 23:48:20 -0800904 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700905 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700907 if ((msg_origport(msg) != peer_port) ||
908 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 break;
910 tipc_disconnect(dref);
911 skb_pull(buf, msg_hdr_sz(msg));
912 cb(usr_handle, dref, &buf, msg_data(msg),
913 msg_data_sz(msg), msg_errcode(msg));
914 break;
915 }
916 case TIPC_DIRECT_MSG:{
917 tipc_msg_err_event cb = up_ptr->err_cb;
918
Julia Lawall4cec72c2008-01-08 23:48:20 -0800919 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700920 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 break;
922 skb_pull(buf, msg_hdr_sz(msg));
923 cb(usr_handle, dref, &buf, msg_data(msg),
924 msg_data_sz(msg), msg_errcode(msg), &orig);
925 break;
926 }
Allan Stephens96882432006-06-25 23:38:58 -0700927 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900929 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 up_ptr->named_err_cb;
931
Julia Lawall4cec72c2008-01-08 23:48:20 -0800932 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700933 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 break;
935 dseq.type = msg_nametype(msg);
936 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700937 dseq.upper = (message_type == TIPC_NAMED_MSG)
938 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900940 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 msg_data_sz(msg), msg_errcode(msg), &dseq);
942 break;
943 }
944 }
945 if (buf)
946 buf_discard(buf);
947 buf = next;
948 continue;
949reject:
950 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
951 buf = next;
952 }
953}
954
955/*
956 * port_dispatcher(): Dispatcher for messages destinated
957 * to the tipc_port interface. Called with port locked.
958 */
959
960static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
961{
962 buf->next = NULL;
963 spin_lock_bh(&queue_lock);
964 if (msg_queue_head) {
965 msg_queue_tail->next = buf;
966 msg_queue_tail = buf;
967 } else {
968 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100969 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 }
971 spin_unlock_bh(&queue_lock);
972 return TIPC_OK;
973}
974
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900975/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900977 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 */
979
980static void port_wakeup_sh(unsigned long ref)
981{
982 struct port *p_ptr;
983 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800984 tipc_continue_event cb = NULL;
985 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986
Per Liden4323add2006-01-18 00:38:21 +0100987 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 if (p_ptr) {
989 up_ptr = p_ptr->user_port;
990 if (up_ptr) {
991 cb = up_ptr->continue_event_cb;
992 uh = up_ptr->usr_handle;
993 }
Per Liden4323add2006-01-18 00:38:21 +0100994 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 }
996 if (cb)
997 cb(uh, ref);
998}
999
1000
1001static void port_wakeup(struct tipc_port *p_ptr)
1002{
Per Liden4323add2006-01-18 00:38:21 +01001003 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004}
1005
1006void tipc_acknowledge(u32 ref, u32 ack)
1007{
1008 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001009 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010
Per Liden4323add2006-01-18 00:38:21 +01001011 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 if (!p_ptr)
1013 return;
1014 if (p_ptr->publ.connected) {
1015 p_ptr->publ.conn_unacked -= ack;
1016 buf = port_build_proto_msg(port_peerport(p_ptr),
1017 port_peernode(p_ptr),
1018 ref,
1019 tipc_own_addr,
1020 CONN_MANAGER,
1021 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001022 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 port_out_seqno(p_ptr),
1024 ack);
1025 }
Per Liden4323add2006-01-18 00:38:21 +01001026 tipc_port_unlock(p_ptr);
1027 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028}
1029
1030/*
1031 * tipc_createport(): user level call. Will add port to
1032 * registry if non-zero user_ref.
1033 */
1034
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001035int tipc_createport(u32 user_ref,
1036 void *usr_handle,
1037 unsigned int importance,
1038 tipc_msg_err_event error_cb,
1039 tipc_named_msg_err_event named_error_cb,
1040 tipc_conn_shutdown_event conn_error_cb,
1041 tipc_msg_event msg_cb,
1042 tipc_named_msg_event named_msg_cb,
1043 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 tipc_continue_event continue_event_cb,/* May be zero */
1045 u32 *portref)
1046{
1047 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001048 struct port *p_ptr;
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001049 struct tipc_port *tp_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 u32 ref;
1051
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001052 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -07001053 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -07001054 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 return -ENOMEM;
1056 }
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001057 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
1058 importance, &tp_ptr);
1059 if (ref == 0) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 kfree(up_ptr);
1061 return -ENOMEM;
1062 }
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001063 p_ptr = (struct port *)tp_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064
1065 p_ptr->user_port = up_ptr;
1066 up_ptr->user_ref = user_ref;
1067 up_ptr->usr_handle = usr_handle;
1068 up_ptr->ref = p_ptr->publ.ref;
1069 up_ptr->err_cb = error_cb;
1070 up_ptr->named_err_cb = named_error_cb;
1071 up_ptr->conn_err_cb = conn_error_cb;
1072 up_ptr->msg_cb = msg_cb;
1073 up_ptr->named_msg_cb = named_msg_cb;
1074 up_ptr->conn_msg_cb = conn_msg_cb;
1075 up_ptr->continue_event_cb = continue_event_cb;
1076 INIT_LIST_HEAD(&up_ptr->uport_list);
Per Liden4323add2006-01-18 00:38:21 +01001077 tipc_reg_add_port(up_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 *portref = p_ptr->publ.ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001079 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
Per Liden4323add2006-01-18 00:38:21 +01001080 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 return TIPC_OK;
1082}
1083
1084int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1085{
1086 id->ref = ref;
1087 id->node = tipc_own_addr;
1088 return TIPC_OK;
1089}
1090
1091int tipc_portimportance(u32 ref, unsigned int *importance)
1092{
1093 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001094
Per Liden4323add2006-01-18 00:38:21 +01001095 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 if (!p_ptr)
1097 return -EINVAL;
1098 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001099 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 return TIPC_OK;
1101}
1102
1103int tipc_set_portimportance(u32 ref, unsigned int imp)
1104{
1105 struct port *p_ptr;
1106
1107 if (imp > TIPC_CRITICAL_IMPORTANCE)
1108 return -EINVAL;
1109
Per Liden4323add2006-01-18 00:38:21 +01001110 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 if (!p_ptr)
1112 return -EINVAL;
1113 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001114 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 return TIPC_OK;
1116}
1117
1118
1119int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1120{
1121 struct port *p_ptr;
1122 struct publication *publ;
1123 u32 key;
1124 int res = -EINVAL;
1125
Per Liden4323add2006-01-18 00:38:21 +01001126 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001127 if (!p_ptr)
1128 return -EINVAL;
1129
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1131 "lower = %u, upper = %u\n",
1132 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 if (p_ptr->publ.connected)
1134 goto exit;
1135 if (seq->lower > seq->upper)
1136 goto exit;
1137 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1138 goto exit;
1139 key = ref + p_ptr->pub_count + 1;
1140 if (key == ref) {
1141 res = -EADDRINUSE;
1142 goto exit;
1143 }
Per Liden4323add2006-01-18 00:38:21 +01001144 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1145 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 if (publ) {
1147 list_add(&publ->pport_list, &p_ptr->publications);
1148 p_ptr->pub_count++;
1149 p_ptr->publ.published = 1;
1150 res = TIPC_OK;
1151 }
1152exit:
Per Liden4323add2006-01-18 00:38:21 +01001153 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 return res;
1155}
1156
1157int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1158{
1159 struct port *p_ptr;
1160 struct publication *publ;
1161 struct publication *tpubl;
1162 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001163
Per Liden4323add2006-01-18 00:38:21 +01001164 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 if (!p_ptr)
1166 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001168 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001170 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001171 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 }
1173 res = TIPC_OK;
1174 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001175 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 &p_ptr->publications, pport_list) {
1177 if (publ->scope != scope)
1178 continue;
1179 if (publ->type != seq->type)
1180 continue;
1181 if (publ->lower != seq->lower)
1182 continue;
1183 if (publ->upper != seq->upper)
1184 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001185 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001186 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 res = TIPC_OK;
1188 break;
1189 }
1190 }
1191 if (list_empty(&p_ptr->publications))
1192 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001193 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 return res;
1195}
1196
1197int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1198{
1199 struct port *p_ptr;
1200 struct tipc_msg *msg;
1201 int res = -EINVAL;
1202
Per Liden4323add2006-01-18 00:38:21 +01001203 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 if (!p_ptr)
1205 return -EINVAL;
1206 if (p_ptr->publ.published || p_ptr->publ.connected)
1207 goto exit;
1208 if (!peer->ref)
1209 goto exit;
1210
1211 msg = &p_ptr->publ.phdr;
1212 msg_set_destnode(msg, peer->node);
1213 msg_set_destport(msg, peer->ref);
1214 msg_set_orignode(msg, tipc_own_addr);
1215 msg_set_origport(msg, p_ptr->publ.ref);
1216 msg_set_transp_seqno(msg, 42);
1217 msg_set_type(msg, TIPC_CONN_MSG);
1218 if (!may_route(peer->node))
1219 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1220 else
1221 msg_set_hdr_sz(msg, LONG_H_SIZE);
1222
1223 p_ptr->probing_interval = PROBING_INTERVAL;
1224 p_ptr->probing_state = CONFIRMED;
1225 p_ptr->publ.connected = 1;
1226 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1227
Per Liden4323add2006-01-18 00:38:21 +01001228 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001229 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 (net_ev_handler)port_handle_node_down);
1231 res = TIPC_OK;
1232exit:
Per Liden4323add2006-01-18 00:38:21 +01001233 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001234 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 return res;
1236}
1237
Allan Stephens0c3141e2008-04-15 00:22:02 -07001238/**
1239 * tipc_disconnect_port - disconnect port from peer
1240 *
1241 * Port must be locked.
1242 */
1243
1244int tipc_disconnect_port(struct tipc_port *tp_ptr)
1245{
1246 int res;
1247
1248 if (tp_ptr->connected) {
1249 tp_ptr->connected = 0;
1250 /* let timer expire on it's own to avoid deadlock! */
1251 tipc_nodesub_unsubscribe(
1252 &((struct port *)tp_ptr)->subscription);
1253 res = TIPC_OK;
1254 } else {
1255 res = -ENOTCONN;
1256 }
1257 return res;
1258}
1259
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260/*
1261 * tipc_disconnect(): Disconnect port form peer.
1262 * This is a node local operation.
1263 */
1264
1265int tipc_disconnect(u32 ref)
1266{
1267 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001268 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269
Per Liden4323add2006-01-18 00:38:21 +01001270 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 if (!p_ptr)
1272 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001273 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001274 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275 return res;
1276}
1277
1278/*
1279 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1280 */
1281int tipc_shutdown(u32 ref)
1282{
1283 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001284 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285
Per Liden4323add2006-01-18 00:38:21 +01001286 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 if (!p_ptr)
1288 return -EINVAL;
1289
1290 if (p_ptr->publ.connected) {
1291 u32 imp = msg_importance(&p_ptr->publ.phdr);
1292 if (imp < TIPC_CRITICAL_IMPORTANCE)
1293 imp++;
1294 buf = port_build_proto_msg(port_peerport(p_ptr),
1295 port_peernode(p_ptr),
1296 ref,
1297 tipc_own_addr,
1298 imp,
1299 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001300 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001301 port_out_seqno(p_ptr),
1302 0);
1303 }
Per Liden4323add2006-01-18 00:38:21 +01001304 tipc_port_unlock(p_ptr);
1305 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 return tipc_disconnect(ref);
1307}
1308
1309int tipc_isconnected(u32 ref, int *isconnected)
1310{
1311 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001312
Per Liden4323add2006-01-18 00:38:21 +01001313 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 if (!p_ptr)
1315 return -EINVAL;
1316 *isconnected = p_ptr->publ.connected;
Per Liden4323add2006-01-18 00:38:21 +01001317 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 return TIPC_OK;
1319}
1320
1321int tipc_peer(u32 ref, struct tipc_portid *peer)
1322{
1323 struct port *p_ptr;
1324 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001325
Per Liden4323add2006-01-18 00:38:21 +01001326 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327 if (!p_ptr)
1328 return -EINVAL;
1329 if (p_ptr->publ.connected) {
1330 peer->ref = port_peerport(p_ptr);
1331 peer->node = port_peernode(p_ptr);
1332 res = TIPC_OK;
1333 } else
1334 res = -ENOTCONN;
Per Liden4323add2006-01-18 00:38:21 +01001335 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 return res;
1337}
1338
1339int tipc_ref_valid(u32 ref)
1340{
1341 /* Works irrespective of type */
Per Liden4323add2006-01-18 00:38:21 +01001342 return !!tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343}
1344
1345
1346/*
Per Liden4323add2006-01-18 00:38:21 +01001347 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 * message for this node.
1349 */
1350
Per Liden4323add2006-01-18 00:38:21 +01001351int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 struct iovec const *msg_sect)
1353{
1354 struct sk_buff *buf;
1355 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001356
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1358 MAX_MSG_SIZE, !sender->user_port, &buf);
1359 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001360 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 return res;
1362}
1363
1364/**
1365 * tipc_send - send message sections on connection
1366 */
1367
1368int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1369{
1370 struct port *p_ptr;
1371 u32 destnode;
1372 int res;
1373
Per Liden4323add2006-01-18 00:38:21 +01001374 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 if (!p_ptr || !p_ptr->publ.connected)
1376 return -EINVAL;
1377
1378 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001379 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 destnode = port_peernode(p_ptr);
1381 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001382 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1383 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 else
Per Liden4323add2006-01-18 00:38:21 +01001385 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386
1387 if (likely(res != -ELINKCONG)) {
1388 port_incr_out_seqno(p_ptr);
1389 p_ptr->publ.congested = 0;
1390 p_ptr->sent++;
1391 return res;
1392 }
1393 }
1394 if (port_unreliable(p_ptr)) {
1395 p_ptr->publ.congested = 0;
1396 /* Just calculate msg length and return */
1397 return msg_calc_data_size(msg_sect, num_sect);
1398 }
1399 return -ELINKCONG;
1400}
1401
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001402/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 * tipc_send_buf - send message buffer on connection
1404 */
1405
1406int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1407{
1408 struct port *p_ptr;
1409 struct tipc_msg *msg;
1410 u32 destnode;
1411 u32 hsz;
1412 u32 sz;
1413 u32 res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001414
Per Liden4323add2006-01-18 00:38:21 +01001415 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 if (!p_ptr || !p_ptr->publ.connected)
1417 return -EINVAL;
1418
1419 msg = &p_ptr->publ.phdr;
1420 hsz = msg_hdr_sz(msg);
1421 sz = hsz + dsz;
1422 msg_set_size(msg, sz);
1423 if (skb_cow(buf, hsz))
1424 return -ENOMEM;
1425
1426 skb_push(buf, hsz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001427 skb_copy_to_linear_data(buf, msg, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 destnode = msg_destnode(msg);
1429 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001430 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 if (likely(destnode != tipc_own_addr))
1432 res = tipc_send_buf_fast(buf, destnode);
1433 else {
Per Liden4323add2006-01-18 00:38:21 +01001434 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 res = sz;
1436 }
1437 if (likely(res != -ELINKCONG)) {
1438 port_incr_out_seqno(p_ptr);
1439 p_ptr->sent++;
1440 p_ptr->publ.congested = 0;
1441 return res;
1442 }
1443 }
1444 if (port_unreliable(p_ptr)) {
1445 p_ptr->publ.congested = 0;
1446 return dsz;
1447 }
1448 return -ELINKCONG;
1449}
1450
1451/**
1452 * tipc_forward2name - forward message sections to port name
1453 */
1454
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001455int tipc_forward2name(u32 ref,
1456 struct tipc_name const *name,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001458 u32 num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001460 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 unsigned int importance)
1462{
1463 struct port *p_ptr;
1464 struct tipc_msg *msg;
1465 u32 destnode = domain;
1466 u32 destport = 0;
1467 int res;
1468
Per Liden4323add2006-01-18 00:38:21 +01001469 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 if (!p_ptr || p_ptr->publ.connected)
1471 return -EINVAL;
1472
1473 msg = &p_ptr->publ.phdr;
1474 msg_set_type(msg, TIPC_NAMED_MSG);
1475 msg_set_orignode(msg, orig->node);
1476 msg_set_origport(msg, orig->ref);
1477 msg_set_hdr_sz(msg, LONG_H_SIZE);
1478 msg_set_nametype(msg, name->type);
1479 msg_set_nameinst(msg, name->instance);
1480 msg_set_lookup_scope(msg, addr_scope(domain));
1481 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1482 msg_set_importance(msg,importance);
Per Liden4323add2006-01-18 00:38:21 +01001483 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 msg_set_destnode(msg, destnode);
1485 msg_set_destport(msg, destport);
1486
1487 if (likely(destport || destnode)) {
1488 p_ptr->sent++;
1489 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001490 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001491 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001492 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493 if (likely(res != -ELINKCONG))
1494 return res;
1495 if (port_unreliable(p_ptr)) {
1496 /* Just calculate msg length and return */
1497 return msg_calc_data_size(msg_sect, num_sect);
1498 }
1499 return -ELINKCONG;
1500 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001501 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001502 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503}
1504
1505/**
1506 * tipc_send2name - send message sections to port name
1507 */
1508
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509int tipc_send2name(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 struct tipc_name const *name,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001511 unsigned int domain,
1512 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513 struct iovec const *msg_sect)
1514{
1515 struct tipc_portid orig;
1516
1517 orig.ref = ref;
1518 orig.node = tipc_own_addr;
1519 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1520 TIPC_PORT_IMPORTANCE);
1521}
1522
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001523/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524 * tipc_forward_buf2name - forward message buffer to port name
1525 */
1526
1527int tipc_forward_buf2name(u32 ref,
1528 struct tipc_name const *name,
1529 u32 domain,
1530 struct sk_buff *buf,
1531 unsigned int dsz,
1532 struct tipc_portid const *orig,
1533 unsigned int importance)
1534{
1535 struct port *p_ptr;
1536 struct tipc_msg *msg;
1537 u32 destnode = domain;
1538 u32 destport = 0;
1539 int res;
1540
Per Liden4323add2006-01-18 00:38:21 +01001541 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542 if (!p_ptr || p_ptr->publ.connected)
1543 return -EINVAL;
1544
1545 msg = &p_ptr->publ.phdr;
1546 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1547 msg_set_importance(msg, importance);
1548 msg_set_type(msg, TIPC_NAMED_MSG);
1549 msg_set_orignode(msg, orig->node);
1550 msg_set_origport(msg, orig->ref);
1551 msg_set_nametype(msg, name->type);
1552 msg_set_nameinst(msg, name->instance);
1553 msg_set_lookup_scope(msg, addr_scope(domain));
1554 msg_set_hdr_sz(msg, LONG_H_SIZE);
1555 msg_set_size(msg, LONG_H_SIZE + dsz);
Per Liden4323add2006-01-18 00:38:21 +01001556 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557 msg_set_destnode(msg, destnode);
1558 msg_set_destport(msg, destport);
1559 msg_dbg(msg, "forw2name ==> ");
1560 if (skb_cow(buf, LONG_H_SIZE))
1561 return -ENOMEM;
1562 skb_push(buf, LONG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001563 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001564 msg_dbg(buf_msg(buf),"PREP:");
1565 if (likely(destport || destnode)) {
1566 p_ptr->sent++;
1567 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001568 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569 res = tipc_send_buf_fast(buf, destnode);
1570 if (likely(res != -ELINKCONG))
1571 return res;
1572 if (port_unreliable(p_ptr))
1573 return dsz;
1574 return -ELINKCONG;
1575 }
1576 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1577}
1578
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001579/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 * tipc_send_buf2name - send message buffer to port name
1581 */
1582
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001583int tipc_send_buf2name(u32 ref,
1584 struct tipc_name const *dest,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001585 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001586 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 unsigned int dsz)
1588{
1589 struct tipc_portid orig;
1590
1591 orig.ref = ref;
1592 orig.node = tipc_own_addr;
1593 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1594 TIPC_PORT_IMPORTANCE);
1595}
1596
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001597/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 * tipc_forward2port - forward message sections to port identity
1599 */
1600
1601int tipc_forward2port(u32 ref,
1602 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001603 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001605 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 unsigned int importance)
1607{
1608 struct port *p_ptr;
1609 struct tipc_msg *msg;
1610 int res;
1611
Per Liden4323add2006-01-18 00:38:21 +01001612 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 if (!p_ptr || p_ptr->publ.connected)
1614 return -EINVAL;
1615
1616 msg = &p_ptr->publ.phdr;
1617 msg_set_type(msg, TIPC_DIRECT_MSG);
1618 msg_set_orignode(msg, orig->node);
1619 msg_set_origport(msg, orig->ref);
1620 msg_set_destnode(msg, dest->node);
1621 msg_set_destport(msg, dest->ref);
1622 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1623 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1624 msg_set_importance(msg, importance);
1625 p_ptr->sent++;
1626 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001627 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1628 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001629 if (likely(res != -ELINKCONG))
1630 return res;
1631 if (port_unreliable(p_ptr)) {
1632 /* Just calculate msg length and return */
1633 return msg_calc_data_size(msg_sect, num_sect);
1634 }
1635 return -ELINKCONG;
1636}
1637
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001638/**
1639 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 */
1641
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001642int tipc_send2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001644 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645 struct iovec const *msg_sect)
1646{
1647 struct tipc_portid orig;
1648
1649 orig.ref = ref;
1650 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001651 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 TIPC_PORT_IMPORTANCE);
1653}
1654
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001655/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 * tipc_forward_buf2port - forward message buffer to port identity
1657 */
1658int tipc_forward_buf2port(u32 ref,
1659 struct tipc_portid const *dest,
1660 struct sk_buff *buf,
1661 unsigned int dsz,
1662 struct tipc_portid const *orig,
1663 unsigned int importance)
1664{
1665 struct port *p_ptr;
1666 struct tipc_msg *msg;
1667 int res;
1668
Per Liden4323add2006-01-18 00:38:21 +01001669 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 if (!p_ptr || p_ptr->publ.connected)
1671 return -EINVAL;
1672
1673 msg = &p_ptr->publ.phdr;
1674 msg_set_type(msg, TIPC_DIRECT_MSG);
1675 msg_set_orignode(msg, orig->node);
1676 msg_set_origport(msg, orig->ref);
1677 msg_set_destnode(msg, dest->node);
1678 msg_set_destport(msg, dest->ref);
1679 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1680 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1681 msg_set_importance(msg, importance);
1682 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1683 if (skb_cow(buf, DIR_MSG_H_SIZE))
1684 return -ENOMEM;
1685
1686 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001687 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 msg_dbg(msg, "buf2port: ");
1689 p_ptr->sent++;
1690 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001691 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 res = tipc_send_buf_fast(buf, dest->node);
1693 if (likely(res != -ELINKCONG))
1694 return res;
1695 if (port_unreliable(p_ptr))
1696 return dsz;
1697 return -ELINKCONG;
1698}
1699
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001700/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 * tipc_send_buf2port - send message buffer to port identity
1702 */
1703
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001704int tipc_send_buf2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001706 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 unsigned int dsz)
1708{
1709 struct tipc_portid orig;
1710
1711 orig.ref = ref;
1712 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001713 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714 TIPC_PORT_IMPORTANCE);
1715}
1716