blob: 93014f9bc95fd0981042c76855fb9c7c5604d1c2 [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 Stephens9bef5432008-06-04 17:47:55 -0700247 msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 p_ptr->last_in_seqno = 41;
250 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 INIT_LIST_HEAD(&p_ptr->wait_list);
252 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800253 p_ptr->congested_link = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 p_ptr->dispatcher = dispatcher;
255 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800256 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100258 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 INIT_LIST_HEAD(&p_ptr->publications);
260 INIT_LIST_HEAD(&p_ptr->port_list);
261 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100262 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700263 *tp_ptr = &p_ptr->publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 return ref;
265}
266
267int tipc_deleteport(u32 ref)
268{
269 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800270 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800272 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100273 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900274 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 return -EINVAL;
276
Per Liden4323add2006-01-18 00:38:21 +0100277 tipc_ref_discard(ref);
278 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279
280 k_cancel_timer(&p_ptr->timer);
281 if (p_ptr->publ.connected) {
282 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100283 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 }
285 if (p_ptr->user_port) {
Per Liden4323add2006-01-18 00:38:21 +0100286 tipc_reg_remove_port(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 kfree(p_ptr->user_port);
288 }
289
Per Liden4323add2006-01-18 00:38:21 +0100290 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 list_del(&p_ptr->port_list);
292 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100293 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 k_term_timer(&p_ptr->timer);
295 kfree(p_ptr);
296 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100297 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 return TIPC_OK;
299}
300
301/**
302 * tipc_get_port() - return port associated with 'ref'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900303 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 * Note: Port is not locked.
305 */
306
307struct tipc_port *tipc_get_port(const u32 ref)
308{
Per Liden4323add2006-01-18 00:38:21 +0100309 return (struct tipc_port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310}
311
312/**
313 * tipc_get_handle - return user handle associated to port 'ref'
314 */
315
316void *tipc_get_handle(const u32 ref)
317{
318 struct port *p_ptr;
319 void * handle;
320
Per Liden4323add2006-01-18 00:38:21 +0100321 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 if (!p_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800323 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 handle = p_ptr->publ.usr_handle;
Per Liden4323add2006-01-18 00:38:21 +0100325 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 return handle;
327}
328
Sam Ravnborg05790c62006-03-20 22:37:04 -0800329static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330{
331 return msg_src_droppable(&p_ptr->publ.phdr);
332}
333
334int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
335{
336 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900337
Per Liden4323add2006-01-18 00:38:21 +0100338 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 if (!p_ptr)
340 return -EINVAL;
341 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800342 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 return TIPC_OK;
344}
345
346int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
347{
348 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900349
Per Liden4323add2006-01-18 00:38:21 +0100350 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 if (!p_ptr)
352 return -EINVAL;
353 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100354 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 return TIPC_OK;
356}
357
Sam Ravnborg05790c62006-03-20 22:37:04 -0800358static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359{
360 return msg_dest_droppable(&p_ptr->publ.phdr);
361}
362
363int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
364{
365 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900366
Per Liden4323add2006-01-18 00:38:21 +0100367 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 if (!p_ptr)
369 return -EINVAL;
370 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800371 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 return TIPC_OK;
373}
374
375int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
376{
377 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900378
Per Liden4323add2006-01-18 00:38:21 +0100379 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 if (!p_ptr)
381 return -EINVAL;
382 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100383 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 return TIPC_OK;
385}
386
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900387/*
388 * port_build_proto_msg(): build a port level protocol
389 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 * tipc_port lock on.
391 */
392static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
393 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900394 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 u32 seqno, u32 ack)
396{
397 struct sk_buff *buf;
398 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900399
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 buf = buf_acquire(LONG_H_SIZE);
401 if (buf) {
402 msg = buf_msg(buf);
Allan Stephens75715212008-06-04 17:37:34 -0700403 msg_init(msg, usr, type, LONG_H_SIZE, destnode);
404 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 msg_set_destport(msg, destport);
406 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 msg_set_orignode(msg, orignode);
408 msg_set_transp_seqno(msg, seqno);
409 msg_set_msgcnt(msg, ack);
410 msg_dbg(msg, "PORT>SEND>:");
411 }
412 return buf;
413}
414
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415int tipc_reject_msg(struct sk_buff *buf, u32 err)
416{
417 struct tipc_msg *msg = buf_msg(buf);
418 struct sk_buff *rbuf;
419 struct tipc_msg *rmsg;
420 int hdr_sz;
421 u32 imp = msg_importance(msg);
422 u32 data_sz = msg_data_sz(msg);
423
424 if (data_sz > MAX_REJECT_SIZE)
425 data_sz = MAX_REJECT_SIZE;
426 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
427 imp++;
428 msg_dbg(msg, "port->rej: ");
429
430 /* discard rejected message if it shouldn't be returned to sender */
431 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
432 buf_discard(buf);
433 return data_sz;
434 }
435
436 /* construct rejected message */
437 if (msg_mcast(msg))
438 hdr_sz = MCAST_H_SIZE;
439 else
440 hdr_sz = LONG_H_SIZE;
441 rbuf = buf_acquire(data_sz + hdr_sz);
442 if (rbuf == NULL) {
443 buf_discard(buf);
444 return data_sz;
445 }
446 rmsg = buf_msg(rbuf);
Allan Stephens75715212008-06-04 17:37:34 -0700447 msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
448 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 msg_set_origport(rmsg, msg_destport(msg));
451 if (msg_short(msg))
452 msg_set_orignode(rmsg, tipc_own_addr);
453 else
454 msg_set_orignode(rmsg, msg_destnode(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900455 msg_set_size(rmsg, data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 msg_set_nametype(rmsg, msg_nametype(msg));
457 msg_set_nameinst(rmsg, msg_nameinst(msg));
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300458 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459
460 /* send self-abort message when rejecting on a connected port */
461 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800462 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100463 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464
465 if (p_ptr) {
466 if (p_ptr->publ.connected)
467 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100468 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 }
Per Liden4323add2006-01-18 00:38:21 +0100470 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 }
472
473 /* send rejected message */
474 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100475 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476 return data_sz;
477}
478
Per Liden4323add2006-01-18 00:38:21 +0100479int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
480 struct iovec const *msg_sect, u32 num_sect,
481 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482{
483 struct sk_buff *buf;
484 int res;
485
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 !p_ptr->user_port, &buf);
488 if (!buf)
489 return res;
490
491 return tipc_reject_msg(buf, err);
492}
493
494static void port_timeout(unsigned long ref)
495{
Per Liden4323add2006-01-18 00:38:21 +0100496 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800497 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
Allan Stephens065fd172006-10-16 21:38:05 -0700499 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 return;
501
Allan Stephens065fd172006-10-16 21:38:05 -0700502 if (!p_ptr->publ.connected) {
503 tipc_port_unlock(p_ptr);
504 return;
505 }
506
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 /* Last probe answered ? */
508 if (p_ptr->probing_state == PROBING) {
509 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
510 } else {
511 buf = port_build_proto_msg(port_peerport(p_ptr),
512 port_peernode(p_ptr),
513 p_ptr->publ.ref,
514 tipc_own_addr,
515 CONN_MANAGER,
516 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900517 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 port_out_seqno(p_ptr),
519 0);
520 port_incr_out_seqno(p_ptr);
521 p_ptr->probing_state = PROBING;
522 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
523 }
Per Liden4323add2006-01-18 00:38:21 +0100524 tipc_port_unlock(p_ptr);
525 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526}
527
528
529static void port_handle_node_down(unsigned long ref)
530{
Per Liden4323add2006-01-18 00:38:21 +0100531 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800532 struct sk_buff* buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533
534 if (!p_ptr)
535 return;
536 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100537 tipc_port_unlock(p_ptr);
538 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539}
540
541
542static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
543{
544 u32 imp = msg_importance(&p_ptr->publ.phdr);
545
546 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800547 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 if (imp < TIPC_CRITICAL_IMPORTANCE)
549 imp++;
550 return port_build_proto_msg(p_ptr->publ.ref,
551 tipc_own_addr,
552 port_peerport(p_ptr),
553 port_peernode(p_ptr),
554 imp,
555 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900556 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 p_ptr->last_in_seqno + 1,
558 0);
559}
560
561
562static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
563{
564 u32 imp = msg_importance(&p_ptr->publ.phdr);
565
566 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800567 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 if (imp < TIPC_CRITICAL_IMPORTANCE)
569 imp++;
570 return port_build_proto_msg(port_peerport(p_ptr),
571 port_peernode(p_ptr),
572 p_ptr->publ.ref,
573 tipc_own_addr,
574 imp,
575 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 port_out_seqno(p_ptr),
578 0);
579}
580
Per Liden4323add2006-01-18 00:38:21 +0100581void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582{
583 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100584 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800586 struct sk_buff *r_buf = NULL;
587 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588
589 msg_dbg(msg, "PORT<RECV<:");
590
591 if (!p_ptr) {
592 err = TIPC_ERR_NO_PORT;
593 } else if (p_ptr->publ.connected) {
594 if (port_peernode(p_ptr) != msg_orignode(msg))
595 err = TIPC_ERR_NO_PORT;
596 if (port_peerport(p_ptr) != msg_origport(msg))
597 err = TIPC_ERR_NO_PORT;
598 if (!err && msg_routed(msg)) {
599 u32 seqno = msg_transp_seqno(msg);
600 u32 myno = ++p_ptr->last_in_seqno;
601 if (seqno != myno) {
602 err = TIPC_ERR_NO_PORT;
603 abort_buf = port_build_self_abort_msg(p_ptr, err);
604 }
605 }
606 if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900607 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 p_ptr->publ.congested &&
609 p_ptr->wakeup;
610 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100611 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 goto exit;
613 p_ptr->publ.congested = 0;
614 if (!wakeup)
615 goto exit;
616 p_ptr->wakeup(&p_ptr->publ);
617 goto exit;
618 }
619 } else if (p_ptr->publ.published) {
620 err = TIPC_ERR_NO_PORT;
621 }
622 if (err) {
623 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624 msg_orignode(msg),
625 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800627 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 TIPC_CONN_MSG,
629 err,
630 0,
631 0);
632 goto exit;
633 }
634
635 /* All is fine */
636 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900637 r_buf = port_build_proto_msg(msg_origport(msg),
638 msg_orignode(msg),
639 msg_destport(msg),
640 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 CONN_MANAGER,
642 CONN_PROBE_REPLY,
643 TIPC_OK,
644 port_out_seqno(p_ptr),
645 0);
646 }
647 p_ptr->probing_state = CONFIRMED;
648 port_incr_out_seqno(p_ptr);
649exit:
650 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100651 tipc_port_unlock(p_ptr);
652 tipc_net_route_msg(r_buf);
653 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 buf_discard(buf);
655}
656
657static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
658{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900659 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660
661 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900664 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 else
666 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
667
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900668 if (p_ptr->publ.connected) {
669 u32 dport = port_peerport(p_ptr);
670 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900672 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
673 tipc_zone(destnode), tipc_cluster(destnode),
674 tipc_node(destnode), dport);
675 if (p_ptr->publ.conn_type != 0)
676 tipc_printf(buf, " via {%u,%u}",
677 p_ptr->publ.conn_type,
678 p_ptr->publ.conn_instance);
679 }
680 else if (p_ptr->publ.published) {
681 tipc_printf(buf, " bound to");
682 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 if (publ->lower == publ->upper)
684 tipc_printf(buf, " {%u,%u}", publ->type,
685 publ->lower);
686 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900687 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900689 }
690 }
691 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692}
693
694#define MAX_PORT_QUERY 32768
695
Per Liden4323add2006-01-18 00:38:21 +0100696struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697{
698 struct sk_buff *buf;
699 struct tlv_desc *rep_tlv;
700 struct print_buf pb;
701 struct port *p_ptr;
702 int str_len;
703
Per Liden4323add2006-01-18 00:38:21 +0100704 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705 if (!buf)
706 return NULL;
707 rep_tlv = (struct tlv_desc *)buf->data;
708
Per Liden4323add2006-01-18 00:38:21 +0100709 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
710 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 list_for_each_entry(p_ptr, &ports, port_list) {
712 spin_lock_bh(p_ptr->publ.lock);
713 port_print(p_ptr, &pb, 0);
714 spin_unlock_bh(p_ptr->publ.lock);
715 }
Per Liden4323add2006-01-18 00:38:21 +0100716 spin_unlock_bh(&tipc_port_list_lock);
717 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718
719 skb_put(buf, TLV_SPACE(str_len));
720 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
721
722 return buf;
723}
724
725#if 0
726
727#define MAX_PORT_STATS 2000
728
729struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
730{
731 u32 ref;
732 struct port *p_ptr;
733 struct sk_buff *buf;
734 struct tlv_desc *rep_tlv;
735 struct print_buf pb;
736 int str_len;
737
738 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
739 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
740
741 ref = *(u32 *)TLV_DATA(req_tlv_area);
742 ref = ntohl(ref);
743
Per Liden4323add2006-01-18 00:38:21 +0100744 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 if (!p_ptr)
746 return cfg_reply_error_string("port not found");
747
Per Liden4323add2006-01-18 00:38:21 +0100748 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 if (!buf) {
Per Liden4323add2006-01-18 00:38:21 +0100750 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 return NULL;
752 }
753 rep_tlv = (struct tlv_desc *)buf->data;
754
Per Liden4323add2006-01-18 00:38:21 +0100755 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756 port_print(p_ptr, &pb, 1);
757 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
Per Liden4323add2006-01-18 00:38:21 +0100758 tipc_port_unlock(p_ptr);
759 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760
761 skb_put(buf, TLV_SPACE(str_len));
762 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
763
764 return buf;
765}
766
767#endif
768
Per Liden4323add2006-01-18 00:38:21 +0100769void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770{
771 struct port *p_ptr;
772 struct tipc_msg *msg;
773
Per Liden4323add2006-01-18 00:38:21 +0100774 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 list_for_each_entry(p_ptr, &ports, port_list) {
776 msg = &p_ptr->publ.phdr;
777 if (msg_orignode(msg) == tipc_own_addr)
778 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700779 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 msg_set_orignode(msg, tipc_own_addr);
781 }
Per Liden4323add2006-01-18 00:38:21 +0100782 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783}
784
785
786/*
787 * port_dispatcher_sigh(): Signal handler for messages destinated
788 * to the tipc_port interface.
789 */
790
791static void port_dispatcher_sigh(void *dummy)
792{
793 struct sk_buff *buf;
794
795 spin_lock_bh(&queue_lock);
796 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800797 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 spin_unlock_bh(&queue_lock);
799
800 while (buf) {
801 struct port *p_ptr;
802 struct user_port *up_ptr;
803 struct tipc_portid orig;
804 struct tipc_name_seq dseq;
805 void *usr_handle;
806 int connected;
807 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700808 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809
810 struct sk_buff *next = buf->next;
811 struct tipc_msg *msg = buf_msg(buf);
812 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900813
Allan Stephens96882432006-06-25 23:38:58 -0700814 message_type = msg_type(msg);
815 if (message_type > TIPC_DIRECT_MSG)
816 goto reject; /* Unsupported message type */
817
Per Liden4323add2006-01-18 00:38:21 +0100818 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700819 if (!p_ptr)
820 goto reject; /* Port deleted while msg in queue */
821
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 orig.ref = msg_origport(msg);
823 orig.node = msg_orignode(msg);
824 up_ptr = p_ptr->user_port;
825 usr_handle = up_ptr->usr_handle;
826 connected = p_ptr->publ.connected;
827 published = p_ptr->publ.published;
828
829 if (unlikely(msg_errcode(msg)))
830 goto err;
831
Allan Stephens96882432006-06-25 23:38:58 -0700832 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 case TIPC_CONN_MSG:{
835 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
836 u32 peer_port = port_peerport(p_ptr);
837 u32 peer_node = port_peernode(p_ptr);
838
Julia Lawall4cec72c2008-01-08 23:48:20 -0800839 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700840 if (unlikely(!cb))
841 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700843 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700845 } else if ((msg_origport(msg) != peer_port) ||
846 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900848 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900850 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 p_ptr->publ.conn_unacked);
852 skb_pull(buf, msg_hdr_sz(msg));
853 cb(usr_handle, dref, &buf, msg_data(msg),
854 msg_data_sz(msg));
855 break;
856 }
857 case TIPC_DIRECT_MSG:{
858 tipc_msg_event cb = up_ptr->msg_cb;
859
Julia Lawall4cec72c2008-01-08 23:48:20 -0800860 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700861 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 goto reject;
863 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900864 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 msg_data_sz(msg), msg_importance(msg),
866 &orig);
867 break;
868 }
Allan Stephens96882432006-06-25 23:38:58 -0700869 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 case TIPC_NAMED_MSG:{
871 tipc_named_msg_event cb = up_ptr->named_msg_cb;
872
Julia Lawall4cec72c2008-01-08 23:48:20 -0800873 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700874 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 goto reject;
876 dseq.type = msg_nametype(msg);
877 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700878 dseq.upper = (message_type == TIPC_NAMED_MSG)
879 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 msg_data_sz(msg), msg_importance(msg),
883 &orig, &dseq);
884 break;
885 }
886 }
887 if (buf)
888 buf_discard(buf);
889 buf = next;
890 continue;
891err:
Allan Stephens96882432006-06-25 23:38:58 -0700892 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900893
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900895 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 up_ptr->conn_err_cb;
897 u32 peer_port = port_peerport(p_ptr);
898 u32 peer_node = port_peernode(p_ptr);
899
Julia Lawall4cec72c2008-01-08 23:48:20 -0800900 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700901 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700903 if ((msg_origport(msg) != peer_port) ||
904 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 break;
906 tipc_disconnect(dref);
907 skb_pull(buf, msg_hdr_sz(msg));
908 cb(usr_handle, dref, &buf, msg_data(msg),
909 msg_data_sz(msg), msg_errcode(msg));
910 break;
911 }
912 case TIPC_DIRECT_MSG:{
913 tipc_msg_err_event cb = up_ptr->err_cb;
914
Julia Lawall4cec72c2008-01-08 23:48:20 -0800915 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700916 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917 break;
918 skb_pull(buf, msg_hdr_sz(msg));
919 cb(usr_handle, dref, &buf, msg_data(msg),
920 msg_data_sz(msg), msg_errcode(msg), &orig);
921 break;
922 }
Allan Stephens96882432006-06-25 23:38:58 -0700923 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900925 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 up_ptr->named_err_cb;
927
Julia Lawall4cec72c2008-01-08 23:48:20 -0800928 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700929 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 break;
931 dseq.type = msg_nametype(msg);
932 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700933 dseq.upper = (message_type == TIPC_NAMED_MSG)
934 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900936 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 msg_data_sz(msg), msg_errcode(msg), &dseq);
938 break;
939 }
940 }
941 if (buf)
942 buf_discard(buf);
943 buf = next;
944 continue;
945reject:
946 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
947 buf = next;
948 }
949}
950
951/*
952 * port_dispatcher(): Dispatcher for messages destinated
953 * to the tipc_port interface. Called with port locked.
954 */
955
956static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
957{
958 buf->next = NULL;
959 spin_lock_bh(&queue_lock);
960 if (msg_queue_head) {
961 msg_queue_tail->next = buf;
962 msg_queue_tail = buf;
963 } else {
964 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100965 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 }
967 spin_unlock_bh(&queue_lock);
968 return TIPC_OK;
969}
970
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900971/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900973 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 */
975
976static void port_wakeup_sh(unsigned long ref)
977{
978 struct port *p_ptr;
979 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800980 tipc_continue_event cb = NULL;
981 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982
Per Liden4323add2006-01-18 00:38:21 +0100983 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 if (p_ptr) {
985 up_ptr = p_ptr->user_port;
986 if (up_ptr) {
987 cb = up_ptr->continue_event_cb;
988 uh = up_ptr->usr_handle;
989 }
Per Liden4323add2006-01-18 00:38:21 +0100990 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 }
992 if (cb)
993 cb(uh, ref);
994}
995
996
997static void port_wakeup(struct tipc_port *p_ptr)
998{
Per Liden4323add2006-01-18 00:38:21 +0100999 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000}
1001
1002void tipc_acknowledge(u32 ref, u32 ack)
1003{
1004 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001005 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006
Per Liden4323add2006-01-18 00:38:21 +01001007 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 if (!p_ptr)
1009 return;
1010 if (p_ptr->publ.connected) {
1011 p_ptr->publ.conn_unacked -= ack;
1012 buf = port_build_proto_msg(port_peerport(p_ptr),
1013 port_peernode(p_ptr),
1014 ref,
1015 tipc_own_addr,
1016 CONN_MANAGER,
1017 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001018 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 port_out_seqno(p_ptr),
1020 ack);
1021 }
Per Liden4323add2006-01-18 00:38:21 +01001022 tipc_port_unlock(p_ptr);
1023 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024}
1025
1026/*
1027 * tipc_createport(): user level call. Will add port to
1028 * registry if non-zero user_ref.
1029 */
1030
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031int tipc_createport(u32 user_ref,
1032 void *usr_handle,
1033 unsigned int importance,
1034 tipc_msg_err_event error_cb,
1035 tipc_named_msg_err_event named_error_cb,
1036 tipc_conn_shutdown_event conn_error_cb,
1037 tipc_msg_event msg_cb,
1038 tipc_named_msg_event named_msg_cb,
1039 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 tipc_continue_event continue_event_cb,/* May be zero */
1041 u32 *portref)
1042{
1043 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001044 struct port *p_ptr;
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001045 struct tipc_port *tp_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 u32 ref;
1047
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001048 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -07001049 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -07001050 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051 return -ENOMEM;
1052 }
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001053 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
1054 importance, &tp_ptr);
1055 if (ref == 0) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056 kfree(up_ptr);
1057 return -ENOMEM;
1058 }
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001059 p_ptr = (struct port *)tp_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060
1061 p_ptr->user_port = up_ptr;
1062 up_ptr->user_ref = user_ref;
1063 up_ptr->usr_handle = usr_handle;
1064 up_ptr->ref = p_ptr->publ.ref;
1065 up_ptr->err_cb = error_cb;
1066 up_ptr->named_err_cb = named_error_cb;
1067 up_ptr->conn_err_cb = conn_error_cb;
1068 up_ptr->msg_cb = msg_cb;
1069 up_ptr->named_msg_cb = named_msg_cb;
1070 up_ptr->conn_msg_cb = conn_msg_cb;
1071 up_ptr->continue_event_cb = continue_event_cb;
1072 INIT_LIST_HEAD(&up_ptr->uport_list);
Per Liden4323add2006-01-18 00:38:21 +01001073 tipc_reg_add_port(up_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 *portref = p_ptr->publ.ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001075 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
Per Liden4323add2006-01-18 00:38:21 +01001076 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 return TIPC_OK;
1078}
1079
1080int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1081{
1082 id->ref = ref;
1083 id->node = tipc_own_addr;
1084 return TIPC_OK;
1085}
1086
1087int tipc_portimportance(u32 ref, unsigned int *importance)
1088{
1089 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001090
Per Liden4323add2006-01-18 00:38:21 +01001091 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 if (!p_ptr)
1093 return -EINVAL;
1094 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001095 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 return TIPC_OK;
1097}
1098
1099int tipc_set_portimportance(u32 ref, unsigned int imp)
1100{
1101 struct port *p_ptr;
1102
1103 if (imp > TIPC_CRITICAL_IMPORTANCE)
1104 return -EINVAL;
1105
Per Liden4323add2006-01-18 00:38:21 +01001106 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 if (!p_ptr)
1108 return -EINVAL;
1109 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001110 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 return TIPC_OK;
1112}
1113
1114
1115int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1116{
1117 struct port *p_ptr;
1118 struct publication *publ;
1119 u32 key;
1120 int res = -EINVAL;
1121
Per Liden4323add2006-01-18 00:38:21 +01001122 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001123 if (!p_ptr)
1124 return -EINVAL;
1125
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1127 "lower = %u, upper = %u\n",
1128 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 if (p_ptr->publ.connected)
1130 goto exit;
1131 if (seq->lower > seq->upper)
1132 goto exit;
1133 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1134 goto exit;
1135 key = ref + p_ptr->pub_count + 1;
1136 if (key == ref) {
1137 res = -EADDRINUSE;
1138 goto exit;
1139 }
Per Liden4323add2006-01-18 00:38:21 +01001140 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1141 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 if (publ) {
1143 list_add(&publ->pport_list, &p_ptr->publications);
1144 p_ptr->pub_count++;
1145 p_ptr->publ.published = 1;
1146 res = TIPC_OK;
1147 }
1148exit:
Per Liden4323add2006-01-18 00:38:21 +01001149 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 return res;
1151}
1152
1153int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1154{
1155 struct port *p_ptr;
1156 struct publication *publ;
1157 struct publication *tpubl;
1158 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001159
Per Liden4323add2006-01-18 00:38:21 +01001160 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 if (!p_ptr)
1162 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001164 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001166 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001167 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 }
1169 res = TIPC_OK;
1170 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001171 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 &p_ptr->publications, pport_list) {
1173 if (publ->scope != scope)
1174 continue;
1175 if (publ->type != seq->type)
1176 continue;
1177 if (publ->lower != seq->lower)
1178 continue;
1179 if (publ->upper != seq->upper)
1180 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001181 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001182 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 res = TIPC_OK;
1184 break;
1185 }
1186 }
1187 if (list_empty(&p_ptr->publications))
1188 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001189 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 return res;
1191}
1192
1193int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1194{
1195 struct port *p_ptr;
1196 struct tipc_msg *msg;
1197 int res = -EINVAL;
1198
Per Liden4323add2006-01-18 00:38:21 +01001199 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 if (!p_ptr)
1201 return -EINVAL;
1202 if (p_ptr->publ.published || p_ptr->publ.connected)
1203 goto exit;
1204 if (!peer->ref)
1205 goto exit;
1206
1207 msg = &p_ptr->publ.phdr;
1208 msg_set_destnode(msg, peer->node);
1209 msg_set_destport(msg, peer->ref);
1210 msg_set_orignode(msg, tipc_own_addr);
1211 msg_set_origport(msg, p_ptr->publ.ref);
1212 msg_set_transp_seqno(msg, 42);
1213 msg_set_type(msg, TIPC_CONN_MSG);
1214 if (!may_route(peer->node))
1215 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1216 else
1217 msg_set_hdr_sz(msg, LONG_H_SIZE);
1218
1219 p_ptr->probing_interval = PROBING_INTERVAL;
1220 p_ptr->probing_state = CONFIRMED;
1221 p_ptr->publ.connected = 1;
1222 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1223
Per Liden4323add2006-01-18 00:38:21 +01001224 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001225 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 (net_ev_handler)port_handle_node_down);
1227 res = TIPC_OK;
1228exit:
Per Liden4323add2006-01-18 00:38:21 +01001229 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001230 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 return res;
1232}
1233
Allan Stephens0c3141e2008-04-15 00:22:02 -07001234/**
1235 * tipc_disconnect_port - disconnect port from peer
1236 *
1237 * Port must be locked.
1238 */
1239
1240int tipc_disconnect_port(struct tipc_port *tp_ptr)
1241{
1242 int res;
1243
1244 if (tp_ptr->connected) {
1245 tp_ptr->connected = 0;
1246 /* let timer expire on it's own to avoid deadlock! */
1247 tipc_nodesub_unsubscribe(
1248 &((struct port *)tp_ptr)->subscription);
1249 res = TIPC_OK;
1250 } else {
1251 res = -ENOTCONN;
1252 }
1253 return res;
1254}
1255
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256/*
1257 * tipc_disconnect(): Disconnect port form peer.
1258 * This is a node local operation.
1259 */
1260
1261int tipc_disconnect(u32 ref)
1262{
1263 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001264 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265
Per Liden4323add2006-01-18 00:38:21 +01001266 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267 if (!p_ptr)
1268 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001269 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001270 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 return res;
1272}
1273
1274/*
1275 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1276 */
1277int tipc_shutdown(u32 ref)
1278{
1279 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001280 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281
Per Liden4323add2006-01-18 00:38:21 +01001282 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 if (!p_ptr)
1284 return -EINVAL;
1285
1286 if (p_ptr->publ.connected) {
1287 u32 imp = msg_importance(&p_ptr->publ.phdr);
1288 if (imp < TIPC_CRITICAL_IMPORTANCE)
1289 imp++;
1290 buf = port_build_proto_msg(port_peerport(p_ptr),
1291 port_peernode(p_ptr),
1292 ref,
1293 tipc_own_addr,
1294 imp,
1295 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001296 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 port_out_seqno(p_ptr),
1298 0);
1299 }
Per Liden4323add2006-01-18 00:38:21 +01001300 tipc_port_unlock(p_ptr);
1301 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 return tipc_disconnect(ref);
1303}
1304
1305int tipc_isconnected(u32 ref, int *isconnected)
1306{
1307 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001308
Per Liden4323add2006-01-18 00:38:21 +01001309 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 if (!p_ptr)
1311 return -EINVAL;
1312 *isconnected = p_ptr->publ.connected;
Per Liden4323add2006-01-18 00:38:21 +01001313 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 return TIPC_OK;
1315}
1316
1317int tipc_peer(u32 ref, struct tipc_portid *peer)
1318{
1319 struct port *p_ptr;
1320 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001321
Per Liden4323add2006-01-18 00:38:21 +01001322 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 if (!p_ptr)
1324 return -EINVAL;
1325 if (p_ptr->publ.connected) {
1326 peer->ref = port_peerport(p_ptr);
1327 peer->node = port_peernode(p_ptr);
1328 res = TIPC_OK;
1329 } else
1330 res = -ENOTCONN;
Per Liden4323add2006-01-18 00:38:21 +01001331 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 return res;
1333}
1334
1335int tipc_ref_valid(u32 ref)
1336{
1337 /* Works irrespective of type */
Per Liden4323add2006-01-18 00:38:21 +01001338 return !!tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339}
1340
1341
1342/*
Per Liden4323add2006-01-18 00:38:21 +01001343 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 * message for this node.
1345 */
1346
Per Liden4323add2006-01-18 00:38:21 +01001347int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 struct iovec const *msg_sect)
1349{
1350 struct sk_buff *buf;
1351 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001352
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1354 MAX_MSG_SIZE, !sender->user_port, &buf);
1355 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001356 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 return res;
1358}
1359
1360/**
1361 * tipc_send - send message sections on connection
1362 */
1363
1364int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1365{
1366 struct port *p_ptr;
1367 u32 destnode;
1368 int res;
1369
Per Liden4323add2006-01-18 00:38:21 +01001370 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 if (!p_ptr || !p_ptr->publ.connected)
1372 return -EINVAL;
1373
1374 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001375 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 destnode = port_peernode(p_ptr);
1377 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001378 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1379 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 else
Per Liden4323add2006-01-18 00:38:21 +01001381 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382
1383 if (likely(res != -ELINKCONG)) {
1384 port_incr_out_seqno(p_ptr);
1385 p_ptr->publ.congested = 0;
1386 p_ptr->sent++;
1387 return res;
1388 }
1389 }
1390 if (port_unreliable(p_ptr)) {
1391 p_ptr->publ.congested = 0;
1392 /* Just calculate msg length and return */
1393 return msg_calc_data_size(msg_sect, num_sect);
1394 }
1395 return -ELINKCONG;
1396}
1397
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001398/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399 * tipc_send_buf - send message buffer on connection
1400 */
1401
1402int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1403{
1404 struct port *p_ptr;
1405 struct tipc_msg *msg;
1406 u32 destnode;
1407 u32 hsz;
1408 u32 sz;
1409 u32 res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001410
Per Liden4323add2006-01-18 00:38:21 +01001411 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412 if (!p_ptr || !p_ptr->publ.connected)
1413 return -EINVAL;
1414
1415 msg = &p_ptr->publ.phdr;
1416 hsz = msg_hdr_sz(msg);
1417 sz = hsz + dsz;
1418 msg_set_size(msg, sz);
1419 if (skb_cow(buf, hsz))
1420 return -ENOMEM;
1421
1422 skb_push(buf, hsz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001423 skb_copy_to_linear_data(buf, msg, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 destnode = msg_destnode(msg);
1425 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001426 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 if (likely(destnode != tipc_own_addr))
1428 res = tipc_send_buf_fast(buf, destnode);
1429 else {
Per Liden4323add2006-01-18 00:38:21 +01001430 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 res = sz;
1432 }
1433 if (likely(res != -ELINKCONG)) {
1434 port_incr_out_seqno(p_ptr);
1435 p_ptr->sent++;
1436 p_ptr->publ.congested = 0;
1437 return res;
1438 }
1439 }
1440 if (port_unreliable(p_ptr)) {
1441 p_ptr->publ.congested = 0;
1442 return dsz;
1443 }
1444 return -ELINKCONG;
1445}
1446
1447/**
1448 * tipc_forward2name - forward message sections to port name
1449 */
1450
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001451int tipc_forward2name(u32 ref,
1452 struct tipc_name const *name,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001454 u32 num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001456 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 unsigned int importance)
1458{
1459 struct port *p_ptr;
1460 struct tipc_msg *msg;
1461 u32 destnode = domain;
1462 u32 destport = 0;
1463 int res;
1464
Per Liden4323add2006-01-18 00:38:21 +01001465 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 if (!p_ptr || p_ptr->publ.connected)
1467 return -EINVAL;
1468
1469 msg = &p_ptr->publ.phdr;
1470 msg_set_type(msg, TIPC_NAMED_MSG);
1471 msg_set_orignode(msg, orig->node);
1472 msg_set_origport(msg, orig->ref);
1473 msg_set_hdr_sz(msg, LONG_H_SIZE);
1474 msg_set_nametype(msg, name->type);
1475 msg_set_nameinst(msg, name->instance);
1476 msg_set_lookup_scope(msg, addr_scope(domain));
1477 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1478 msg_set_importance(msg,importance);
Per Liden4323add2006-01-18 00:38:21 +01001479 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 msg_set_destnode(msg, destnode);
1481 msg_set_destport(msg, destport);
1482
1483 if (likely(destport || destnode)) {
1484 p_ptr->sent++;
1485 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001486 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001487 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001488 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489 if (likely(res != -ELINKCONG))
1490 return res;
1491 if (port_unreliable(p_ptr)) {
1492 /* Just calculate msg length and return */
1493 return msg_calc_data_size(msg_sect, num_sect);
1494 }
1495 return -ELINKCONG;
1496 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001497 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001498 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499}
1500
1501/**
1502 * tipc_send2name - send message sections to port name
1503 */
1504
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001505int tipc_send2name(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506 struct tipc_name const *name,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001507 unsigned int domain,
1508 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001509 struct iovec const *msg_sect)
1510{
1511 struct tipc_portid orig;
1512
1513 orig.ref = ref;
1514 orig.node = tipc_own_addr;
1515 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1516 TIPC_PORT_IMPORTANCE);
1517}
1518
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001519/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 * tipc_forward_buf2name - forward message buffer to port name
1521 */
1522
1523int tipc_forward_buf2name(u32 ref,
1524 struct tipc_name const *name,
1525 u32 domain,
1526 struct sk_buff *buf,
1527 unsigned int dsz,
1528 struct tipc_portid const *orig,
1529 unsigned int importance)
1530{
1531 struct port *p_ptr;
1532 struct tipc_msg *msg;
1533 u32 destnode = domain;
1534 u32 destport = 0;
1535 int res;
1536
Per Liden4323add2006-01-18 00:38:21 +01001537 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538 if (!p_ptr || p_ptr->publ.connected)
1539 return -EINVAL;
1540
1541 msg = &p_ptr->publ.phdr;
1542 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1543 msg_set_importance(msg, importance);
1544 msg_set_type(msg, TIPC_NAMED_MSG);
1545 msg_set_orignode(msg, orig->node);
1546 msg_set_origport(msg, orig->ref);
1547 msg_set_nametype(msg, name->type);
1548 msg_set_nameinst(msg, name->instance);
1549 msg_set_lookup_scope(msg, addr_scope(domain));
1550 msg_set_hdr_sz(msg, LONG_H_SIZE);
1551 msg_set_size(msg, LONG_H_SIZE + dsz);
Per Liden4323add2006-01-18 00:38:21 +01001552 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 msg_set_destnode(msg, destnode);
1554 msg_set_destport(msg, destport);
1555 msg_dbg(msg, "forw2name ==> ");
1556 if (skb_cow(buf, LONG_H_SIZE))
1557 return -ENOMEM;
1558 skb_push(buf, LONG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001559 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001560 msg_dbg(buf_msg(buf),"PREP:");
1561 if (likely(destport || destnode)) {
1562 p_ptr->sent++;
1563 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001564 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 res = tipc_send_buf_fast(buf, destnode);
1566 if (likely(res != -ELINKCONG))
1567 return res;
1568 if (port_unreliable(p_ptr))
1569 return dsz;
1570 return -ELINKCONG;
1571 }
1572 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1573}
1574
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001575/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576 * tipc_send_buf2name - send message buffer to port name
1577 */
1578
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001579int tipc_send_buf2name(u32 ref,
1580 struct tipc_name const *dest,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001582 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 unsigned int dsz)
1584{
1585 struct tipc_portid orig;
1586
1587 orig.ref = ref;
1588 orig.node = tipc_own_addr;
1589 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1590 TIPC_PORT_IMPORTANCE);
1591}
1592
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001593/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594 * tipc_forward2port - forward message sections to port identity
1595 */
1596
1597int tipc_forward2port(u32 ref,
1598 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001599 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001601 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602 unsigned int importance)
1603{
1604 struct port *p_ptr;
1605 struct tipc_msg *msg;
1606 int res;
1607
Per Liden4323add2006-01-18 00:38:21 +01001608 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609 if (!p_ptr || p_ptr->publ.connected)
1610 return -EINVAL;
1611
1612 msg = &p_ptr->publ.phdr;
1613 msg_set_type(msg, TIPC_DIRECT_MSG);
1614 msg_set_orignode(msg, orig->node);
1615 msg_set_origport(msg, orig->ref);
1616 msg_set_destnode(msg, dest->node);
1617 msg_set_destport(msg, dest->ref);
1618 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1619 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1620 msg_set_importance(msg, importance);
1621 p_ptr->sent++;
1622 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001623 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1624 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001625 if (likely(res != -ELINKCONG))
1626 return res;
1627 if (port_unreliable(p_ptr)) {
1628 /* Just calculate msg length and return */
1629 return msg_calc_data_size(msg_sect, num_sect);
1630 }
1631 return -ELINKCONG;
1632}
1633
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001634/**
1635 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636 */
1637
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001638int tipc_send2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001640 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001641 struct iovec const *msg_sect)
1642{
1643 struct tipc_portid orig;
1644
1645 orig.ref = ref;
1646 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001647 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648 TIPC_PORT_IMPORTANCE);
1649}
1650
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001651/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 * tipc_forward_buf2port - forward message buffer to port identity
1653 */
1654int tipc_forward_buf2port(u32 ref,
1655 struct tipc_portid const *dest,
1656 struct sk_buff *buf,
1657 unsigned int dsz,
1658 struct tipc_portid const *orig,
1659 unsigned int importance)
1660{
1661 struct port *p_ptr;
1662 struct tipc_msg *msg;
1663 int res;
1664
Per Liden4323add2006-01-18 00:38:21 +01001665 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 if (!p_ptr || p_ptr->publ.connected)
1667 return -EINVAL;
1668
1669 msg = &p_ptr->publ.phdr;
1670 msg_set_type(msg, TIPC_DIRECT_MSG);
1671 msg_set_orignode(msg, orig->node);
1672 msg_set_origport(msg, orig->ref);
1673 msg_set_destnode(msg, dest->node);
1674 msg_set_destport(msg, dest->ref);
1675 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1676 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1677 msg_set_importance(msg, importance);
1678 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1679 if (skb_cow(buf, DIR_MSG_H_SIZE))
1680 return -ENOMEM;
1681
1682 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001683 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 msg_dbg(msg, "buf2port: ");
1685 p_ptr->sent++;
1686 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001687 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 res = tipc_send_buf_fast(buf, dest->node);
1689 if (likely(res != -ELINKCONG))
1690 return res;
1691 if (port_unreliable(p_ptr))
1692 return dsz;
1693 return -ELINKCONG;
1694}
1695
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001696/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 * tipc_send_buf2port - send message buffer to port identity
1698 */
1699
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001700int tipc_send_buf2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001702 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 unsigned int dsz)
1704{
1705 struct tipc_portid orig;
1706
1707 orig.ref = ref;
1708 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001709 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 TIPC_PORT_IMPORTANCE);
1711}
1712