blob: e3e9c121afbe651fdc0effd826499b74f7290725 [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 Stephens75715212008-06-04 17:37:34 -0700247 msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 msg_set_orignode(msg, tipc_own_addr);
249 msg_set_prevnode(msg, tipc_own_addr);
250 msg_set_origport(msg, ref);
251 msg_set_importance(msg,importance);
252 p_ptr->last_in_seqno = 41;
253 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 INIT_LIST_HEAD(&p_ptr->wait_list);
255 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800256 p_ptr->congested_link = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 p_ptr->dispatcher = dispatcher;
258 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800259 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100261 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 INIT_LIST_HEAD(&p_ptr->publications);
263 INIT_LIST_HEAD(&p_ptr->port_list);
264 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100265 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700266 *tp_ptr = &p_ptr->publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 return ref;
268}
269
270int tipc_deleteport(u32 ref)
271{
272 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800273 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800275 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100276 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900277 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 return -EINVAL;
279
Per Liden4323add2006-01-18 00:38:21 +0100280 tipc_ref_discard(ref);
281 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282
283 k_cancel_timer(&p_ptr->timer);
284 if (p_ptr->publ.connected) {
285 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100286 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 }
288 if (p_ptr->user_port) {
Per Liden4323add2006-01-18 00:38:21 +0100289 tipc_reg_remove_port(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 kfree(p_ptr->user_port);
291 }
292
Per Liden4323add2006-01-18 00:38:21 +0100293 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 list_del(&p_ptr->port_list);
295 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100296 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 k_term_timer(&p_ptr->timer);
298 kfree(p_ptr);
299 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100300 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 return TIPC_OK;
302}
303
304/**
305 * tipc_get_port() - return port associated with 'ref'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900306 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 * Note: Port is not locked.
308 */
309
310struct tipc_port *tipc_get_port(const u32 ref)
311{
Per Liden4323add2006-01-18 00:38:21 +0100312 return (struct tipc_port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313}
314
315/**
316 * tipc_get_handle - return user handle associated to port 'ref'
317 */
318
319void *tipc_get_handle(const u32 ref)
320{
321 struct port *p_ptr;
322 void * handle;
323
Per Liden4323add2006-01-18 00:38:21 +0100324 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 if (!p_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800326 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 handle = p_ptr->publ.usr_handle;
Per Liden4323add2006-01-18 00:38:21 +0100328 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 return handle;
330}
331
Sam Ravnborg05790c62006-03-20 22:37:04 -0800332static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333{
334 return msg_src_droppable(&p_ptr->publ.phdr);
335}
336
337int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
338{
339 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900340
Per Liden4323add2006-01-18 00:38:21 +0100341 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 if (!p_ptr)
343 return -EINVAL;
344 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800345 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 return TIPC_OK;
347}
348
349int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
350{
351 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900352
Per Liden4323add2006-01-18 00:38:21 +0100353 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 if (!p_ptr)
355 return -EINVAL;
356 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100357 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 return TIPC_OK;
359}
360
Sam Ravnborg05790c62006-03-20 22:37:04 -0800361static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362{
363 return msg_dest_droppable(&p_ptr->publ.phdr);
364}
365
366int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
367{
368 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900369
Per Liden4323add2006-01-18 00:38:21 +0100370 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 if (!p_ptr)
372 return -EINVAL;
373 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800374 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 return TIPC_OK;
376}
377
378int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
379{
380 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900381
Per Liden4323add2006-01-18 00:38:21 +0100382 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 if (!p_ptr)
384 return -EINVAL;
385 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100386 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 return TIPC_OK;
388}
389
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900390/*
391 * port_build_proto_msg(): build a port level protocol
392 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 * tipc_port lock on.
394 */
395static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
396 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900397 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 u32 seqno, u32 ack)
399{
400 struct sk_buff *buf;
401 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900402
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 buf = buf_acquire(LONG_H_SIZE);
404 if (buf) {
405 msg = buf_msg(buf);
Allan Stephens75715212008-06-04 17:37:34 -0700406 msg_init(msg, usr, type, LONG_H_SIZE, destnode);
407 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 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);
Allan Stephens75715212008-06-04 17:37:34 -0700451 msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
452 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 msg_set_destport(rmsg, msg_origport(msg));
454 msg_set_prevnode(rmsg, tipc_own_addr);
455 msg_set_origport(rmsg, msg_destport(msg));
456 if (msg_short(msg))
457 msg_set_orignode(rmsg, tipc_own_addr);
458 else
459 msg_set_orignode(rmsg, msg_destnode(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900460 msg_set_size(rmsg, data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 msg_set_nametype(rmsg, msg_nametype(msg));
462 msg_set_nameinst(rmsg, msg_nameinst(msg));
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300463 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464
465 /* send self-abort message when rejecting on a connected port */
466 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800467 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100468 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469
470 if (p_ptr) {
471 if (p_ptr->publ.connected)
472 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100473 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 }
Per Liden4323add2006-01-18 00:38:21 +0100475 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476 }
477
478 /* send rejected message */
479 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100480 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 return data_sz;
482}
483
Per Liden4323add2006-01-18 00:38:21 +0100484int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
485 struct iovec const *msg_sect, u32 num_sect,
486 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487{
488 struct sk_buff *buf;
489 int res;
490
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900491 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 !p_ptr->user_port, &buf);
493 if (!buf)
494 return res;
495
496 return tipc_reject_msg(buf, err);
497}
498
499static void port_timeout(unsigned long ref)
500{
Per Liden4323add2006-01-18 00:38:21 +0100501 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800502 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503
Allan Stephens065fd172006-10-16 21:38:05 -0700504 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 return;
506
Allan Stephens065fd172006-10-16 21:38:05 -0700507 if (!p_ptr->publ.connected) {
508 tipc_port_unlock(p_ptr);
509 return;
510 }
511
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 /* Last probe answered ? */
513 if (p_ptr->probing_state == PROBING) {
514 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
515 } else {
516 buf = port_build_proto_msg(port_peerport(p_ptr),
517 port_peernode(p_ptr),
518 p_ptr->publ.ref,
519 tipc_own_addr,
520 CONN_MANAGER,
521 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900522 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 port_out_seqno(p_ptr),
524 0);
525 port_incr_out_seqno(p_ptr);
526 p_ptr->probing_state = PROBING;
527 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
528 }
Per Liden4323add2006-01-18 00:38:21 +0100529 tipc_port_unlock(p_ptr);
530 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531}
532
533
534static void port_handle_node_down(unsigned long ref)
535{
Per Liden4323add2006-01-18 00:38:21 +0100536 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800537 struct sk_buff* buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538
539 if (!p_ptr)
540 return;
541 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100542 tipc_port_unlock(p_ptr);
543 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544}
545
546
547static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
548{
549 u32 imp = msg_importance(&p_ptr->publ.phdr);
550
551 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800552 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 if (imp < TIPC_CRITICAL_IMPORTANCE)
554 imp++;
555 return port_build_proto_msg(p_ptr->publ.ref,
556 tipc_own_addr,
557 port_peerport(p_ptr),
558 port_peernode(p_ptr),
559 imp,
560 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 p_ptr->last_in_seqno + 1,
563 0);
564}
565
566
567static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
568{
569 u32 imp = msg_importance(&p_ptr->publ.phdr);
570
571 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800572 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 if (imp < TIPC_CRITICAL_IMPORTANCE)
574 imp++;
575 return port_build_proto_msg(port_peerport(p_ptr),
576 port_peernode(p_ptr),
577 p_ptr->publ.ref,
578 tipc_own_addr,
579 imp,
580 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 port_out_seqno(p_ptr),
583 0);
584}
585
Per Liden4323add2006-01-18 00:38:21 +0100586void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587{
588 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100589 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800591 struct sk_buff *r_buf = NULL;
592 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593
594 msg_dbg(msg, "PORT<RECV<:");
595
596 if (!p_ptr) {
597 err = TIPC_ERR_NO_PORT;
598 } else if (p_ptr->publ.connected) {
599 if (port_peernode(p_ptr) != msg_orignode(msg))
600 err = TIPC_ERR_NO_PORT;
601 if (port_peerport(p_ptr) != msg_origport(msg))
602 err = TIPC_ERR_NO_PORT;
603 if (!err && msg_routed(msg)) {
604 u32 seqno = msg_transp_seqno(msg);
605 u32 myno = ++p_ptr->last_in_seqno;
606 if (seqno != myno) {
607 err = TIPC_ERR_NO_PORT;
608 abort_buf = port_build_self_abort_msg(p_ptr, err);
609 }
610 }
611 if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 p_ptr->publ.congested &&
614 p_ptr->wakeup;
615 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100616 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 goto exit;
618 p_ptr->publ.congested = 0;
619 if (!wakeup)
620 goto exit;
621 p_ptr->wakeup(&p_ptr->publ);
622 goto exit;
623 }
624 } else if (p_ptr->publ.published) {
625 err = TIPC_ERR_NO_PORT;
626 }
627 if (err) {
628 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900629 msg_orignode(msg),
630 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800632 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 TIPC_CONN_MSG,
634 err,
635 0,
636 0);
637 goto exit;
638 }
639
640 /* All is fine */
641 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900642 r_buf = port_build_proto_msg(msg_origport(msg),
643 msg_orignode(msg),
644 msg_destport(msg),
645 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 CONN_MANAGER,
647 CONN_PROBE_REPLY,
648 TIPC_OK,
649 port_out_seqno(p_ptr),
650 0);
651 }
652 p_ptr->probing_state = CONFIRMED;
653 port_incr_out_seqno(p_ptr);
654exit:
655 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100656 tipc_port_unlock(p_ptr);
657 tipc_net_route_msg(r_buf);
658 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 buf_discard(buf);
660}
661
662static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
663{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900664 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665
666 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900667 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900669 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 else
671 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
672
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900673 if (p_ptr->publ.connected) {
674 u32 dport = port_peerport(p_ptr);
675 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
678 tipc_zone(destnode), tipc_cluster(destnode),
679 tipc_node(destnode), dport);
680 if (p_ptr->publ.conn_type != 0)
681 tipc_printf(buf, " via {%u,%u}",
682 p_ptr->publ.conn_type,
683 p_ptr->publ.conn_instance);
684 }
685 else if (p_ptr->publ.published) {
686 tipc_printf(buf, " bound to");
687 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 if (publ->lower == publ->upper)
689 tipc_printf(buf, " {%u,%u}", publ->type,
690 publ->lower);
691 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900694 }
695 }
696 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697}
698
699#define MAX_PORT_QUERY 32768
700
Per Liden4323add2006-01-18 00:38:21 +0100701struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702{
703 struct sk_buff *buf;
704 struct tlv_desc *rep_tlv;
705 struct print_buf pb;
706 struct port *p_ptr;
707 int str_len;
708
Per Liden4323add2006-01-18 00:38:21 +0100709 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 if (!buf)
711 return NULL;
712 rep_tlv = (struct tlv_desc *)buf->data;
713
Per Liden4323add2006-01-18 00:38:21 +0100714 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
715 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 list_for_each_entry(p_ptr, &ports, port_list) {
717 spin_lock_bh(p_ptr->publ.lock);
718 port_print(p_ptr, &pb, 0);
719 spin_unlock_bh(p_ptr->publ.lock);
720 }
Per Liden4323add2006-01-18 00:38:21 +0100721 spin_unlock_bh(&tipc_port_list_lock);
722 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723
724 skb_put(buf, TLV_SPACE(str_len));
725 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
726
727 return buf;
728}
729
730#if 0
731
732#define MAX_PORT_STATS 2000
733
734struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
735{
736 u32 ref;
737 struct port *p_ptr;
738 struct sk_buff *buf;
739 struct tlv_desc *rep_tlv;
740 struct print_buf pb;
741 int str_len;
742
743 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
744 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
745
746 ref = *(u32 *)TLV_DATA(req_tlv_area);
747 ref = ntohl(ref);
748
Per Liden4323add2006-01-18 00:38:21 +0100749 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 if (!p_ptr)
751 return cfg_reply_error_string("port not found");
752
Per Liden4323add2006-01-18 00:38:21 +0100753 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 if (!buf) {
Per Liden4323add2006-01-18 00:38:21 +0100755 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756 return NULL;
757 }
758 rep_tlv = (struct tlv_desc *)buf->data;
759
Per Liden4323add2006-01-18 00:38:21 +0100760 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 port_print(p_ptr, &pb, 1);
762 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
Per Liden4323add2006-01-18 00:38:21 +0100763 tipc_port_unlock(p_ptr);
764 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765
766 skb_put(buf, TLV_SPACE(str_len));
767 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
768
769 return buf;
770}
771
772#endif
773
Per Liden4323add2006-01-18 00:38:21 +0100774void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775{
776 struct port *p_ptr;
777 struct tipc_msg *msg;
778
Per Liden4323add2006-01-18 00:38:21 +0100779 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 list_for_each_entry(p_ptr, &ports, port_list) {
781 msg = &p_ptr->publ.phdr;
782 if (msg_orignode(msg) == tipc_own_addr)
783 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700784 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 msg_set_orignode(msg, tipc_own_addr);
786 }
Per Liden4323add2006-01-18 00:38:21 +0100787 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788}
789
790
791/*
792 * port_dispatcher_sigh(): Signal handler for messages destinated
793 * to the tipc_port interface.
794 */
795
796static void port_dispatcher_sigh(void *dummy)
797{
798 struct sk_buff *buf;
799
800 spin_lock_bh(&queue_lock);
801 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800802 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 spin_unlock_bh(&queue_lock);
804
805 while (buf) {
806 struct port *p_ptr;
807 struct user_port *up_ptr;
808 struct tipc_portid orig;
809 struct tipc_name_seq dseq;
810 void *usr_handle;
811 int connected;
812 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700813 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814
815 struct sk_buff *next = buf->next;
816 struct tipc_msg *msg = buf_msg(buf);
817 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900818
Allan Stephens96882432006-06-25 23:38:58 -0700819 message_type = msg_type(msg);
820 if (message_type > TIPC_DIRECT_MSG)
821 goto reject; /* Unsupported message type */
822
Per Liden4323add2006-01-18 00:38:21 +0100823 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700824 if (!p_ptr)
825 goto reject; /* Port deleted while msg in queue */
826
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 orig.ref = msg_origport(msg);
828 orig.node = msg_orignode(msg);
829 up_ptr = p_ptr->user_port;
830 usr_handle = up_ptr->usr_handle;
831 connected = p_ptr->publ.connected;
832 published = p_ptr->publ.published;
833
834 if (unlikely(msg_errcode(msg)))
835 goto err;
836
Allan Stephens96882432006-06-25 23:38:58 -0700837 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900838
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 case TIPC_CONN_MSG:{
840 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
841 u32 peer_port = port_peerport(p_ptr);
842 u32 peer_node = port_peernode(p_ptr);
843
Julia Lawall4cec72c2008-01-08 23:48:20 -0800844 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700845 if (unlikely(!cb))
846 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700848 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700850 } else if ((msg_origport(msg) != peer_port) ||
851 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900853 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900855 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 p_ptr->publ.conn_unacked);
857 skb_pull(buf, msg_hdr_sz(msg));
858 cb(usr_handle, dref, &buf, msg_data(msg),
859 msg_data_sz(msg));
860 break;
861 }
862 case TIPC_DIRECT_MSG:{
863 tipc_msg_event cb = up_ptr->msg_cb;
864
Julia Lawall4cec72c2008-01-08 23:48:20 -0800865 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700866 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867 goto reject;
868 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900869 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 msg_data_sz(msg), msg_importance(msg),
871 &orig);
872 break;
873 }
Allan Stephens96882432006-06-25 23:38:58 -0700874 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 case TIPC_NAMED_MSG:{
876 tipc_named_msg_event cb = up_ptr->named_msg_cb;
877
Julia Lawall4cec72c2008-01-08 23:48:20 -0800878 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700879 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 goto reject;
881 dseq.type = msg_nametype(msg);
882 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700883 dseq.upper = (message_type == TIPC_NAMED_MSG)
884 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900886 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 msg_data_sz(msg), msg_importance(msg),
888 &orig, &dseq);
889 break;
890 }
891 }
892 if (buf)
893 buf_discard(buf);
894 buf = next;
895 continue;
896err:
Allan Stephens96882432006-06-25 23:38:58 -0700897 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900898
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900900 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 up_ptr->conn_err_cb;
902 u32 peer_port = port_peerport(p_ptr);
903 u32 peer_node = port_peernode(p_ptr);
904
Julia Lawall4cec72c2008-01-08 23:48:20 -0800905 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700906 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700908 if ((msg_origport(msg) != peer_port) ||
909 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 break;
911 tipc_disconnect(dref);
912 skb_pull(buf, msg_hdr_sz(msg));
913 cb(usr_handle, dref, &buf, msg_data(msg),
914 msg_data_sz(msg), msg_errcode(msg));
915 break;
916 }
917 case TIPC_DIRECT_MSG:{
918 tipc_msg_err_event cb = up_ptr->err_cb;
919
Julia Lawall4cec72c2008-01-08 23:48:20 -0800920 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700921 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 break;
923 skb_pull(buf, msg_hdr_sz(msg));
924 cb(usr_handle, dref, &buf, msg_data(msg),
925 msg_data_sz(msg), msg_errcode(msg), &orig);
926 break;
927 }
Allan Stephens96882432006-06-25 23:38:58 -0700928 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 up_ptr->named_err_cb;
932
Julia Lawall4cec72c2008-01-08 23:48:20 -0800933 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700934 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 break;
936 dseq.type = msg_nametype(msg);
937 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700938 dseq.upper = (message_type == TIPC_NAMED_MSG)
939 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900941 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 msg_data_sz(msg), msg_errcode(msg), &dseq);
943 break;
944 }
945 }
946 if (buf)
947 buf_discard(buf);
948 buf = next;
949 continue;
950reject:
951 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
952 buf = next;
953 }
954}
955
956/*
957 * port_dispatcher(): Dispatcher for messages destinated
958 * to the tipc_port interface. Called with port locked.
959 */
960
961static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
962{
963 buf->next = NULL;
964 spin_lock_bh(&queue_lock);
965 if (msg_queue_head) {
966 msg_queue_tail->next = buf;
967 msg_queue_tail = buf;
968 } else {
969 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100970 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 }
972 spin_unlock_bh(&queue_lock);
973 return TIPC_OK;
974}
975
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900976/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900978 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979 */
980
981static void port_wakeup_sh(unsigned long ref)
982{
983 struct port *p_ptr;
984 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800985 tipc_continue_event cb = NULL;
986 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987
Per Liden4323add2006-01-18 00:38:21 +0100988 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 if (p_ptr) {
990 up_ptr = p_ptr->user_port;
991 if (up_ptr) {
992 cb = up_ptr->continue_event_cb;
993 uh = up_ptr->usr_handle;
994 }
Per Liden4323add2006-01-18 00:38:21 +0100995 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 }
997 if (cb)
998 cb(uh, ref);
999}
1000
1001
1002static void port_wakeup(struct tipc_port *p_ptr)
1003{
Per Liden4323add2006-01-18 00:38:21 +01001004 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005}
1006
1007void tipc_acknowledge(u32 ref, u32 ack)
1008{
1009 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001010 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011
Per Liden4323add2006-01-18 00:38:21 +01001012 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 if (!p_ptr)
1014 return;
1015 if (p_ptr->publ.connected) {
1016 p_ptr->publ.conn_unacked -= ack;
1017 buf = port_build_proto_msg(port_peerport(p_ptr),
1018 port_peernode(p_ptr),
1019 ref,
1020 tipc_own_addr,
1021 CONN_MANAGER,
1022 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 port_out_seqno(p_ptr),
1025 ack);
1026 }
Per Liden4323add2006-01-18 00:38:21 +01001027 tipc_port_unlock(p_ptr);
1028 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029}
1030
1031/*
1032 * tipc_createport(): user level call. Will add port to
1033 * registry if non-zero user_ref.
1034 */
1035
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001036int tipc_createport(u32 user_ref,
1037 void *usr_handle,
1038 unsigned int importance,
1039 tipc_msg_err_event error_cb,
1040 tipc_named_msg_err_event named_error_cb,
1041 tipc_conn_shutdown_event conn_error_cb,
1042 tipc_msg_event msg_cb,
1043 tipc_named_msg_event named_msg_cb,
1044 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 tipc_continue_event continue_event_cb,/* May be zero */
1046 u32 *portref)
1047{
1048 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001049 struct port *p_ptr;
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001050 struct tipc_port *tp_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051 u32 ref;
1052
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001053 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -07001054 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -07001055 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056 return -ENOMEM;
1057 }
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001058 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
1059 importance, &tp_ptr);
1060 if (ref == 0) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 kfree(up_ptr);
1062 return -ENOMEM;
1063 }
Allan Stephens7ef43eb2008-05-12 15:42:28 -07001064 p_ptr = (struct port *)tp_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065
1066 p_ptr->user_port = up_ptr;
1067 up_ptr->user_ref = user_ref;
1068 up_ptr->usr_handle = usr_handle;
1069 up_ptr->ref = p_ptr->publ.ref;
1070 up_ptr->err_cb = error_cb;
1071 up_ptr->named_err_cb = named_error_cb;
1072 up_ptr->conn_err_cb = conn_error_cb;
1073 up_ptr->msg_cb = msg_cb;
1074 up_ptr->named_msg_cb = named_msg_cb;
1075 up_ptr->conn_msg_cb = conn_msg_cb;
1076 up_ptr->continue_event_cb = continue_event_cb;
1077 INIT_LIST_HEAD(&up_ptr->uport_list);
Per Liden4323add2006-01-18 00:38:21 +01001078 tipc_reg_add_port(up_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 *portref = p_ptr->publ.ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001080 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
Per Liden4323add2006-01-18 00:38:21 +01001081 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 return TIPC_OK;
1083}
1084
1085int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1086{
1087 id->ref = ref;
1088 id->node = tipc_own_addr;
1089 return TIPC_OK;
1090}
1091
1092int tipc_portimportance(u32 ref, unsigned int *importance)
1093{
1094 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001095
Per Liden4323add2006-01-18 00:38:21 +01001096 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097 if (!p_ptr)
1098 return -EINVAL;
1099 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001100 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 return TIPC_OK;
1102}
1103
1104int tipc_set_portimportance(u32 ref, unsigned int imp)
1105{
1106 struct port *p_ptr;
1107
1108 if (imp > TIPC_CRITICAL_IMPORTANCE)
1109 return -EINVAL;
1110
Per Liden4323add2006-01-18 00:38:21 +01001111 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 if (!p_ptr)
1113 return -EINVAL;
1114 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001115 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 return TIPC_OK;
1117}
1118
1119
1120int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1121{
1122 struct port *p_ptr;
1123 struct publication *publ;
1124 u32 key;
1125 int res = -EINVAL;
1126
Per Liden4323add2006-01-18 00:38:21 +01001127 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001128 if (!p_ptr)
1129 return -EINVAL;
1130
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1132 "lower = %u, upper = %u\n",
1133 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 if (p_ptr->publ.connected)
1135 goto exit;
1136 if (seq->lower > seq->upper)
1137 goto exit;
1138 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1139 goto exit;
1140 key = ref + p_ptr->pub_count + 1;
1141 if (key == ref) {
1142 res = -EADDRINUSE;
1143 goto exit;
1144 }
Per Liden4323add2006-01-18 00:38:21 +01001145 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1146 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 if (publ) {
1148 list_add(&publ->pport_list, &p_ptr->publications);
1149 p_ptr->pub_count++;
1150 p_ptr->publ.published = 1;
1151 res = TIPC_OK;
1152 }
1153exit:
Per Liden4323add2006-01-18 00:38:21 +01001154 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 return res;
1156}
1157
1158int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1159{
1160 struct port *p_ptr;
1161 struct publication *publ;
1162 struct publication *tpubl;
1163 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001164
Per Liden4323add2006-01-18 00:38:21 +01001165 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 if (!p_ptr)
1167 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001169 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001171 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001172 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 }
1174 res = TIPC_OK;
1175 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001176 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 &p_ptr->publications, pport_list) {
1178 if (publ->scope != scope)
1179 continue;
1180 if (publ->type != seq->type)
1181 continue;
1182 if (publ->lower != seq->lower)
1183 continue;
1184 if (publ->upper != seq->upper)
1185 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001186 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001187 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 res = TIPC_OK;
1189 break;
1190 }
1191 }
1192 if (list_empty(&p_ptr->publications))
1193 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001194 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 return res;
1196}
1197
1198int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1199{
1200 struct port *p_ptr;
1201 struct tipc_msg *msg;
1202 int res = -EINVAL;
1203
Per Liden4323add2006-01-18 00:38:21 +01001204 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 if (!p_ptr)
1206 return -EINVAL;
1207 if (p_ptr->publ.published || p_ptr->publ.connected)
1208 goto exit;
1209 if (!peer->ref)
1210 goto exit;
1211
1212 msg = &p_ptr->publ.phdr;
1213 msg_set_destnode(msg, peer->node);
1214 msg_set_destport(msg, peer->ref);
1215 msg_set_orignode(msg, tipc_own_addr);
1216 msg_set_origport(msg, p_ptr->publ.ref);
1217 msg_set_transp_seqno(msg, 42);
1218 msg_set_type(msg, TIPC_CONN_MSG);
1219 if (!may_route(peer->node))
1220 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1221 else
1222 msg_set_hdr_sz(msg, LONG_H_SIZE);
1223
1224 p_ptr->probing_interval = PROBING_INTERVAL;
1225 p_ptr->probing_state = CONFIRMED;
1226 p_ptr->publ.connected = 1;
1227 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1228
Per Liden4323add2006-01-18 00:38:21 +01001229 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001230 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 (net_ev_handler)port_handle_node_down);
1232 res = TIPC_OK;
1233exit:
Per Liden4323add2006-01-18 00:38:21 +01001234 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001235 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 return res;
1237}
1238
Allan Stephens0c3141e2008-04-15 00:22:02 -07001239/**
1240 * tipc_disconnect_port - disconnect port from peer
1241 *
1242 * Port must be locked.
1243 */
1244
1245int tipc_disconnect_port(struct tipc_port *tp_ptr)
1246{
1247 int res;
1248
1249 if (tp_ptr->connected) {
1250 tp_ptr->connected = 0;
1251 /* let timer expire on it's own to avoid deadlock! */
1252 tipc_nodesub_unsubscribe(
1253 &((struct port *)tp_ptr)->subscription);
1254 res = TIPC_OK;
1255 } else {
1256 res = -ENOTCONN;
1257 }
1258 return res;
1259}
1260
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261/*
1262 * tipc_disconnect(): Disconnect port form peer.
1263 * This is a node local operation.
1264 */
1265
1266int tipc_disconnect(u32 ref)
1267{
1268 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001269 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270
Per Liden4323add2006-01-18 00:38:21 +01001271 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 if (!p_ptr)
1273 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001274 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001275 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 return res;
1277}
1278
1279/*
1280 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1281 */
1282int tipc_shutdown(u32 ref)
1283{
1284 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001285 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286
Per Liden4323add2006-01-18 00:38:21 +01001287 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 if (!p_ptr)
1289 return -EINVAL;
1290
1291 if (p_ptr->publ.connected) {
1292 u32 imp = msg_importance(&p_ptr->publ.phdr);
1293 if (imp < TIPC_CRITICAL_IMPORTANCE)
1294 imp++;
1295 buf = port_build_proto_msg(port_peerport(p_ptr),
1296 port_peernode(p_ptr),
1297 ref,
1298 tipc_own_addr,
1299 imp,
1300 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001301 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 port_out_seqno(p_ptr),
1303 0);
1304 }
Per Liden4323add2006-01-18 00:38:21 +01001305 tipc_port_unlock(p_ptr);
1306 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 return tipc_disconnect(ref);
1308}
1309
1310int tipc_isconnected(u32 ref, int *isconnected)
1311{
1312 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001313
Per Liden4323add2006-01-18 00:38:21 +01001314 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 if (!p_ptr)
1316 return -EINVAL;
1317 *isconnected = p_ptr->publ.connected;
Per Liden4323add2006-01-18 00:38:21 +01001318 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 return TIPC_OK;
1320}
1321
1322int tipc_peer(u32 ref, struct tipc_portid *peer)
1323{
1324 struct port *p_ptr;
1325 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001326
Per Liden4323add2006-01-18 00:38:21 +01001327 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 if (!p_ptr)
1329 return -EINVAL;
1330 if (p_ptr->publ.connected) {
1331 peer->ref = port_peerport(p_ptr);
1332 peer->node = port_peernode(p_ptr);
1333 res = TIPC_OK;
1334 } else
1335 res = -ENOTCONN;
Per Liden4323add2006-01-18 00:38:21 +01001336 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 return res;
1338}
1339
1340int tipc_ref_valid(u32 ref)
1341{
1342 /* Works irrespective of type */
Per Liden4323add2006-01-18 00:38:21 +01001343 return !!tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344}
1345
1346
1347/*
Per Liden4323add2006-01-18 00:38:21 +01001348 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 * message for this node.
1350 */
1351
Per Liden4323add2006-01-18 00:38:21 +01001352int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 struct iovec const *msg_sect)
1354{
1355 struct sk_buff *buf;
1356 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001357
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1359 MAX_MSG_SIZE, !sender->user_port, &buf);
1360 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001361 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 return res;
1363}
1364
1365/**
1366 * tipc_send - send message sections on connection
1367 */
1368
1369int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1370{
1371 struct port *p_ptr;
1372 u32 destnode;
1373 int res;
1374
Per Liden4323add2006-01-18 00:38:21 +01001375 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 if (!p_ptr || !p_ptr->publ.connected)
1377 return -EINVAL;
1378
1379 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001380 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 destnode = port_peernode(p_ptr);
1382 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001383 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1384 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 else
Per Liden4323add2006-01-18 00:38:21 +01001386 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387
1388 if (likely(res != -ELINKCONG)) {
1389 port_incr_out_seqno(p_ptr);
1390 p_ptr->publ.congested = 0;
1391 p_ptr->sent++;
1392 return res;
1393 }
1394 }
1395 if (port_unreliable(p_ptr)) {
1396 p_ptr->publ.congested = 0;
1397 /* Just calculate msg length and return */
1398 return msg_calc_data_size(msg_sect, num_sect);
1399 }
1400 return -ELINKCONG;
1401}
1402
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001403/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 * tipc_send_buf - send message buffer on connection
1405 */
1406
1407int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1408{
1409 struct port *p_ptr;
1410 struct tipc_msg *msg;
1411 u32 destnode;
1412 u32 hsz;
1413 u32 sz;
1414 u32 res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001415
Per Liden4323add2006-01-18 00:38:21 +01001416 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 if (!p_ptr || !p_ptr->publ.connected)
1418 return -EINVAL;
1419
1420 msg = &p_ptr->publ.phdr;
1421 hsz = msg_hdr_sz(msg);
1422 sz = hsz + dsz;
1423 msg_set_size(msg, sz);
1424 if (skb_cow(buf, hsz))
1425 return -ENOMEM;
1426
1427 skb_push(buf, hsz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001428 skb_copy_to_linear_data(buf, msg, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 destnode = msg_destnode(msg);
1430 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001431 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432 if (likely(destnode != tipc_own_addr))
1433 res = tipc_send_buf_fast(buf, destnode);
1434 else {
Per Liden4323add2006-01-18 00:38:21 +01001435 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 res = sz;
1437 }
1438 if (likely(res != -ELINKCONG)) {
1439 port_incr_out_seqno(p_ptr);
1440 p_ptr->sent++;
1441 p_ptr->publ.congested = 0;
1442 return res;
1443 }
1444 }
1445 if (port_unreliable(p_ptr)) {
1446 p_ptr->publ.congested = 0;
1447 return dsz;
1448 }
1449 return -ELINKCONG;
1450}
1451
1452/**
1453 * tipc_forward2name - forward message sections to port name
1454 */
1455
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001456int tipc_forward2name(u32 ref,
1457 struct tipc_name const *name,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001459 u32 num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001461 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462 unsigned int importance)
1463{
1464 struct port *p_ptr;
1465 struct tipc_msg *msg;
1466 u32 destnode = domain;
1467 u32 destport = 0;
1468 int res;
1469
Per Liden4323add2006-01-18 00:38:21 +01001470 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 if (!p_ptr || p_ptr->publ.connected)
1472 return -EINVAL;
1473
1474 msg = &p_ptr->publ.phdr;
1475 msg_set_type(msg, TIPC_NAMED_MSG);
1476 msg_set_orignode(msg, orig->node);
1477 msg_set_origport(msg, orig->ref);
1478 msg_set_hdr_sz(msg, LONG_H_SIZE);
1479 msg_set_nametype(msg, name->type);
1480 msg_set_nameinst(msg, name->instance);
1481 msg_set_lookup_scope(msg, addr_scope(domain));
1482 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1483 msg_set_importance(msg,importance);
Per Liden4323add2006-01-18 00:38:21 +01001484 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485 msg_set_destnode(msg, destnode);
1486 msg_set_destport(msg, destport);
1487
1488 if (likely(destport || destnode)) {
1489 p_ptr->sent++;
1490 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001491 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001492 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001493 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 if (likely(res != -ELINKCONG))
1495 return res;
1496 if (port_unreliable(p_ptr)) {
1497 /* Just calculate msg length and return */
1498 return msg_calc_data_size(msg_sect, num_sect);
1499 }
1500 return -ELINKCONG;
1501 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001502 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001503 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504}
1505
1506/**
1507 * tipc_send2name - send message sections to port name
1508 */
1509
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001510int tipc_send2name(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 struct tipc_name const *name,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001512 unsigned int domain,
1513 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 struct iovec const *msg_sect)
1515{
1516 struct tipc_portid orig;
1517
1518 orig.ref = ref;
1519 orig.node = tipc_own_addr;
1520 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1521 TIPC_PORT_IMPORTANCE);
1522}
1523
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001524/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525 * tipc_forward_buf2name - forward message buffer to port name
1526 */
1527
1528int tipc_forward_buf2name(u32 ref,
1529 struct tipc_name const *name,
1530 u32 domain,
1531 struct sk_buff *buf,
1532 unsigned int dsz,
1533 struct tipc_portid const *orig,
1534 unsigned int importance)
1535{
1536 struct port *p_ptr;
1537 struct tipc_msg *msg;
1538 u32 destnode = domain;
1539 u32 destport = 0;
1540 int res;
1541
Per Liden4323add2006-01-18 00:38:21 +01001542 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001543 if (!p_ptr || p_ptr->publ.connected)
1544 return -EINVAL;
1545
1546 msg = &p_ptr->publ.phdr;
1547 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1548 msg_set_importance(msg, importance);
1549 msg_set_type(msg, TIPC_NAMED_MSG);
1550 msg_set_orignode(msg, orig->node);
1551 msg_set_origport(msg, orig->ref);
1552 msg_set_nametype(msg, name->type);
1553 msg_set_nameinst(msg, name->instance);
1554 msg_set_lookup_scope(msg, addr_scope(domain));
1555 msg_set_hdr_sz(msg, LONG_H_SIZE);
1556 msg_set_size(msg, LONG_H_SIZE + dsz);
Per Liden4323add2006-01-18 00:38:21 +01001557 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558 msg_set_destnode(msg, destnode);
1559 msg_set_destport(msg, destport);
1560 msg_dbg(msg, "forw2name ==> ");
1561 if (skb_cow(buf, LONG_H_SIZE))
1562 return -ENOMEM;
1563 skb_push(buf, LONG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001564 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 msg_dbg(buf_msg(buf),"PREP:");
1566 if (likely(destport || destnode)) {
1567 p_ptr->sent++;
1568 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001569 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570 res = tipc_send_buf_fast(buf, destnode);
1571 if (likely(res != -ELINKCONG))
1572 return res;
1573 if (port_unreliable(p_ptr))
1574 return dsz;
1575 return -ELINKCONG;
1576 }
1577 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1578}
1579
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001580/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581 * tipc_send_buf2name - send message buffer to port name
1582 */
1583
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001584int tipc_send_buf2name(u32 ref,
1585 struct tipc_name const *dest,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001587 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588 unsigned int dsz)
1589{
1590 struct tipc_portid orig;
1591
1592 orig.ref = ref;
1593 orig.node = tipc_own_addr;
1594 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1595 TIPC_PORT_IMPORTANCE);
1596}
1597
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001598/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599 * tipc_forward2port - forward message sections to port identity
1600 */
1601
1602int tipc_forward2port(u32 ref,
1603 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001604 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001606 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 unsigned int importance)
1608{
1609 struct port *p_ptr;
1610 struct tipc_msg *msg;
1611 int res;
1612
Per Liden4323add2006-01-18 00:38:21 +01001613 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614 if (!p_ptr || p_ptr->publ.connected)
1615 return -EINVAL;
1616
1617 msg = &p_ptr->publ.phdr;
1618 msg_set_type(msg, TIPC_DIRECT_MSG);
1619 msg_set_orignode(msg, orig->node);
1620 msg_set_origport(msg, orig->ref);
1621 msg_set_destnode(msg, dest->node);
1622 msg_set_destport(msg, dest->ref);
1623 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1624 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1625 msg_set_importance(msg, importance);
1626 p_ptr->sent++;
1627 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001628 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1629 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 if (likely(res != -ELINKCONG))
1631 return res;
1632 if (port_unreliable(p_ptr)) {
1633 /* Just calculate msg length and return */
1634 return msg_calc_data_size(msg_sect, num_sect);
1635 }
1636 return -ELINKCONG;
1637}
1638
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001639/**
1640 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001641 */
1642
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001643int tipc_send2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001645 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646 struct iovec const *msg_sect)
1647{
1648 struct tipc_portid orig;
1649
1650 orig.ref = ref;
1651 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001652 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 TIPC_PORT_IMPORTANCE);
1654}
1655
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001656/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657 * tipc_forward_buf2port - forward message buffer to port identity
1658 */
1659int tipc_forward_buf2port(u32 ref,
1660 struct tipc_portid const *dest,
1661 struct sk_buff *buf,
1662 unsigned int dsz,
1663 struct tipc_portid const *orig,
1664 unsigned int importance)
1665{
1666 struct port *p_ptr;
1667 struct tipc_msg *msg;
1668 int res;
1669
Per Liden4323add2006-01-18 00:38:21 +01001670 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 if (!p_ptr || p_ptr->publ.connected)
1672 return -EINVAL;
1673
1674 msg = &p_ptr->publ.phdr;
1675 msg_set_type(msg, TIPC_DIRECT_MSG);
1676 msg_set_orignode(msg, orig->node);
1677 msg_set_origport(msg, orig->ref);
1678 msg_set_destnode(msg, dest->node);
1679 msg_set_destport(msg, dest->ref);
1680 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1681 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1682 msg_set_importance(msg, importance);
1683 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1684 if (skb_cow(buf, DIR_MSG_H_SIZE))
1685 return -ENOMEM;
1686
1687 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001688 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689 msg_dbg(msg, "buf2port: ");
1690 p_ptr->sent++;
1691 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001692 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 res = tipc_send_buf_fast(buf, dest->node);
1694 if (likely(res != -ELINKCONG))
1695 return res;
1696 if (port_unreliable(p_ptr))
1697 return dsz;
1698 return -ELINKCONG;
1699}
1700
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001701/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 * tipc_send_buf2port - send message buffer to port identity
1703 */
1704
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001705int tipc_send_buf2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001707 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 unsigned int dsz)
1709{
1710 struct tipc_portid orig;
1711
1712 orig.ref = ref;
1713 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001714 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 TIPC_PORT_IMPORTANCE);
1716}
1717