blob: e70d27ea6578a3138c34761e5bba9414a2bd6af5 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/port.c: TIPC port code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Allan Stephens05646c92007-06-10 17:25:24 -07004 * Copyright (c) 1992-2007, Ericsson AB
Allan Stephens0ea52242008-07-14 22:42:19 -07005 * Copyright (c) 2004-2008, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
39#include "dbg.h"
40#include "port.h"
41#include "addr.h"
42#include "link.h"
43#include "node.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#include "name_table.h"
45#include "user_reg.h"
46#include "msg.h"
47#include "bcast.h"
48
49/* Connection management: */
50#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
51#define CONFIRMED 0
52#define PROBING 1
53
54#define MAX_REJECT_SIZE 1024
55
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080056static struct sk_buff *msg_queue_head = NULL;
57static struct sk_buff *msg_queue_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Ingo Molnar34af9462006-06-27 02:53:55 -070059DEFINE_SPINLOCK(tipc_port_list_lock);
60static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010061
Per Liden4323add2006-01-18 00:38:21 +010062static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010063static void port_handle_node_down(unsigned long ref);
64static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
65static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
66static void port_timeout(unsigned long ref);
67
68
Sam Ravnborg05790c62006-03-20 22:37:04 -080069static u32 port_peernode(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010070{
71 return msg_destnode(&p_ptr->publ.phdr);
72}
73
Sam Ravnborg05790c62006-03-20 22:37:04 -080074static u32 port_peerport(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010075{
76 return msg_destport(&p_ptr->publ.phdr);
77}
78
Sam Ravnborg05790c62006-03-20 22:37:04 -080079static u32 port_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010080{
81 return msg_transp_seqno(&p_ptr->publ.phdr);
82}
83
Sam Ravnborg05790c62006-03-20 22:37:04 -080084static void port_incr_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010085{
86 struct tipc_msg *m = &p_ptr->publ.phdr;
87
88 if (likely(!msg_routed(m)))
89 return;
90 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
91}
92
93/**
94 * tipc_multicast - send a multicast message to local and remote destinations
95 */
96
97int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
98 u32 num_sect, struct iovec const *msg_sect)
99{
100 struct tipc_msg *hdr;
101 struct sk_buff *buf;
102 struct sk_buff *ibuf = NULL;
103 struct port_list dports = {0, NULL, };
Per Liden4323add2006-01-18 00:38:21 +0100104 struct port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 int ext_targets;
106 int res;
107
108 if (unlikely(!oport))
109 return -EINVAL;
110
111 /* Create multicast message */
112
113 hdr = &oport->publ.phdr;
114 msg_set_type(hdr, TIPC_MCAST_MSG);
115 msg_set_nametype(hdr, seq->type);
116 msg_set_namelower(hdr, seq->lower);
117 msg_set_nameupper(hdr, seq->upper);
118 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
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 Stephens0ea52242008-07-14 22:42:19 -0700216 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 */
218
Allan Stephens0ea52242008-07-14 22:42:19 -0700219struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
221 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700222 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223{
224 struct port *p_ptr;
225 struct tipc_msg *msg;
226 u32 ref;
227
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700228 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700229 if (!p_ptr) {
230 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700231 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 }
Per Liden4323add2006-01-18 00:38:21 +0100233 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700235 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700237 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 }
239
Allan Stephens05646c92007-06-10 17:25:24 -0700240 p_ptr->publ.usr_handle = usr_handle;
241 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 p_ptr->publ.ref = ref;
243 msg = &p_ptr->publ.phdr;
Allan Stephens9bef5432008-06-04 17:47:55 -0700244 msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 p_ptr->last_in_seqno = 41;
247 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 INIT_LIST_HEAD(&p_ptr->wait_list);
249 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800250 p_ptr->congested_link = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 p_ptr->dispatcher = dispatcher;
252 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800253 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100255 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 INIT_LIST_HEAD(&p_ptr->publications);
257 INIT_LIST_HEAD(&p_ptr->port_list);
258 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100259 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens0ea52242008-07-14 22:42:19 -0700260 return &(p_ptr->publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261}
262
263int tipc_deleteport(u32 ref)
264{
265 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800266 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800268 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100269 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900270 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 return -EINVAL;
272
Per Liden4323add2006-01-18 00:38:21 +0100273 tipc_ref_discard(ref);
274 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275
276 k_cancel_timer(&p_ptr->timer);
277 if (p_ptr->publ.connected) {
278 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100279 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 }
281 if (p_ptr->user_port) {
Per Liden4323add2006-01-18 00:38:21 +0100282 tipc_reg_remove_port(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 kfree(p_ptr->user_port);
284 }
285
Per Liden4323add2006-01-18 00:38:21 +0100286 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 list_del(&p_ptr->port_list);
288 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100289 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 k_term_timer(&p_ptr->timer);
291 kfree(p_ptr);
292 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100293 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700294 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295}
296
297/**
298 * tipc_get_port() - return port associated with 'ref'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900299 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 * Note: Port is not locked.
301 */
302
303struct tipc_port *tipc_get_port(const u32 ref)
304{
Per Liden4323add2006-01-18 00:38:21 +0100305 return (struct tipc_port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306}
307
308/**
309 * tipc_get_handle - return user handle associated to port 'ref'
310 */
311
312void *tipc_get_handle(const u32 ref)
313{
314 struct port *p_ptr;
315 void * handle;
316
Per Liden4323add2006-01-18 00:38:21 +0100317 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 if (!p_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800319 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 handle = p_ptr->publ.usr_handle;
Per Liden4323add2006-01-18 00:38:21 +0100321 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 return handle;
323}
324
Sam Ravnborg05790c62006-03-20 22:37:04 -0800325static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326{
327 return msg_src_droppable(&p_ptr->publ.phdr);
328}
329
330int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
331{
332 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900333
Per Liden4323add2006-01-18 00:38:21 +0100334 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 if (!p_ptr)
336 return -EINVAL;
337 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800338 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700339 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340}
341
342int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
343{
344 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900345
Per Liden4323add2006-01-18 00:38:21 +0100346 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 if (!p_ptr)
348 return -EINVAL;
349 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100350 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700351 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352}
353
Sam Ravnborg05790c62006-03-20 22:37:04 -0800354static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
356 return msg_dest_droppable(&p_ptr->publ.phdr);
357}
358
359int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
360{
361 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900362
Per Liden4323add2006-01-18 00:38:21 +0100363 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 if (!p_ptr)
365 return -EINVAL;
366 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800367 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700368 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369}
370
371int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
372{
373 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900374
Per Liden4323add2006-01-18 00:38:21 +0100375 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 if (!p_ptr)
377 return -EINVAL;
378 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100379 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700380 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381}
382
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900383/*
384 * port_build_proto_msg(): build a port level protocol
385 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 * tipc_port lock on.
387 */
388static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
389 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900390 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 u32 seqno, u32 ack)
392{
393 struct sk_buff *buf;
394 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900395
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396 buf = buf_acquire(LONG_H_SIZE);
397 if (buf) {
398 msg = buf_msg(buf);
Allan Stephens75715212008-06-04 17:37:34 -0700399 msg_init(msg, usr, type, LONG_H_SIZE, destnode);
400 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 msg_set_destport(msg, destport);
402 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 msg_set_orignode(msg, orignode);
404 msg_set_transp_seqno(msg, seqno);
405 msg_set_msgcnt(msg, ack);
406 msg_dbg(msg, "PORT>SEND>:");
407 }
408 return buf;
409}
410
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411int tipc_reject_msg(struct sk_buff *buf, u32 err)
412{
413 struct tipc_msg *msg = buf_msg(buf);
414 struct sk_buff *rbuf;
415 struct tipc_msg *rmsg;
416 int hdr_sz;
417 u32 imp = msg_importance(msg);
418 u32 data_sz = msg_data_sz(msg);
419
420 if (data_sz > MAX_REJECT_SIZE)
421 data_sz = MAX_REJECT_SIZE;
422 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
423 imp++;
424 msg_dbg(msg, "port->rej: ");
425
426 /* discard rejected message if it shouldn't be returned to sender */
427 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
428 buf_discard(buf);
429 return data_sz;
430 }
431
432 /* construct rejected message */
433 if (msg_mcast(msg))
434 hdr_sz = MCAST_H_SIZE;
435 else
436 hdr_sz = LONG_H_SIZE;
437 rbuf = buf_acquire(data_sz + hdr_sz);
438 if (rbuf == NULL) {
439 buf_discard(buf);
440 return data_sz;
441 }
442 rmsg = buf_msg(rbuf);
Allan Stephens75715212008-06-04 17:37:34 -0700443 msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
444 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700447 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700449 /* leave name type & instance as zeroes */
450 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700452 msg_set_nametype(rmsg, msg_nametype(msg));
453 msg_set_nameinst(rmsg, msg_nameinst(msg));
454 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900455 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300456 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457
458 /* send self-abort message when rejecting on a connected port */
459 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800460 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100461 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462
463 if (p_ptr) {
464 if (p_ptr->publ.connected)
465 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100466 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 }
Per Liden4323add2006-01-18 00:38:21 +0100468 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 }
470
471 /* send rejected message */
472 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100473 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 return data_sz;
475}
476
Per Liden4323add2006-01-18 00:38:21 +0100477int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
478 struct iovec const *msg_sect, u32 num_sect,
479 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480{
481 struct sk_buff *buf;
482 int res;
483
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900484 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 !p_ptr->user_port, &buf);
486 if (!buf)
487 return res;
488
489 return tipc_reject_msg(buf, err);
490}
491
492static void port_timeout(unsigned long ref)
493{
Per Liden4323add2006-01-18 00:38:21 +0100494 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800495 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496
Allan Stephens065fd172006-10-16 21:38:05 -0700497 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 return;
499
Allan Stephens065fd172006-10-16 21:38:05 -0700500 if (!p_ptr->publ.connected) {
501 tipc_port_unlock(p_ptr);
502 return;
503 }
504
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 /* Last probe answered ? */
506 if (p_ptr->probing_state == PROBING) {
507 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
508 } else {
509 buf = port_build_proto_msg(port_peerport(p_ptr),
510 port_peernode(p_ptr),
511 p_ptr->publ.ref,
512 tipc_own_addr,
513 CONN_MANAGER,
514 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900515 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 port_out_seqno(p_ptr),
517 0);
518 port_incr_out_seqno(p_ptr);
519 p_ptr->probing_state = PROBING;
520 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
521 }
Per Liden4323add2006-01-18 00:38:21 +0100522 tipc_port_unlock(p_ptr);
523 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524}
525
526
527static void port_handle_node_down(unsigned long ref)
528{
Per Liden4323add2006-01-18 00:38:21 +0100529 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800530 struct sk_buff* buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531
532 if (!p_ptr)
533 return;
534 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100535 tipc_port_unlock(p_ptr);
536 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537}
538
539
540static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
541{
542 u32 imp = msg_importance(&p_ptr->publ.phdr);
543
544 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800545 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 if (imp < TIPC_CRITICAL_IMPORTANCE)
547 imp++;
548 return port_build_proto_msg(p_ptr->publ.ref,
549 tipc_own_addr,
550 port_peerport(p_ptr),
551 port_peernode(p_ptr),
552 imp,
553 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900554 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 p_ptr->last_in_seqno + 1,
556 0);
557}
558
559
560static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
561{
562 u32 imp = msg_importance(&p_ptr->publ.phdr);
563
564 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800565 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 if (imp < TIPC_CRITICAL_IMPORTANCE)
567 imp++;
568 return port_build_proto_msg(port_peerport(p_ptr),
569 port_peernode(p_ptr),
570 p_ptr->publ.ref,
571 tipc_own_addr,
572 imp,
573 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900574 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 port_out_seqno(p_ptr),
576 0);
577}
578
Per Liden4323add2006-01-18 00:38:21 +0100579void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580{
581 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100582 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800584 struct sk_buff *r_buf = NULL;
585 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586
587 msg_dbg(msg, "PORT<RECV<:");
588
589 if (!p_ptr) {
590 err = TIPC_ERR_NO_PORT;
591 } else if (p_ptr->publ.connected) {
592 if (port_peernode(p_ptr) != msg_orignode(msg))
593 err = TIPC_ERR_NO_PORT;
594 if (port_peerport(p_ptr) != msg_origport(msg))
595 err = TIPC_ERR_NO_PORT;
596 if (!err && msg_routed(msg)) {
597 u32 seqno = msg_transp_seqno(msg);
598 u32 myno = ++p_ptr->last_in_seqno;
599 if (seqno != myno) {
600 err = TIPC_ERR_NO_PORT;
601 abort_buf = port_build_self_abort_msg(p_ptr, err);
602 }
603 }
604 if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900605 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 p_ptr->publ.congested &&
607 p_ptr->wakeup;
608 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100609 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 goto exit;
611 p_ptr->publ.congested = 0;
612 if (!wakeup)
613 goto exit;
614 p_ptr->wakeup(&p_ptr->publ);
615 goto exit;
616 }
617 } else if (p_ptr->publ.published) {
618 err = TIPC_ERR_NO_PORT;
619 }
620 if (err) {
621 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900622 msg_orignode(msg),
623 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800625 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 TIPC_CONN_MSG,
627 err,
628 0,
629 0);
630 goto exit;
631 }
632
633 /* All is fine */
634 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900635 r_buf = port_build_proto_msg(msg_origport(msg),
636 msg_orignode(msg),
637 msg_destport(msg),
638 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639 CONN_MANAGER,
640 CONN_PROBE_REPLY,
641 TIPC_OK,
642 port_out_seqno(p_ptr),
643 0);
644 }
645 p_ptr->probing_state = CONFIRMED;
646 port_incr_out_seqno(p_ptr);
647exit:
648 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100649 tipc_port_unlock(p_ptr);
650 tipc_net_route_msg(r_buf);
651 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 buf_discard(buf);
653}
654
655static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
656{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900657 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658
659 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900660 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 else
664 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
665
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900666 if (p_ptr->publ.connected) {
667 u32 dport = port_peerport(p_ptr);
668 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900670 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
671 tipc_zone(destnode), tipc_cluster(destnode),
672 tipc_node(destnode), dport);
673 if (p_ptr->publ.conn_type != 0)
674 tipc_printf(buf, " via {%u,%u}",
675 p_ptr->publ.conn_type,
676 p_ptr->publ.conn_instance);
677 }
678 else if (p_ptr->publ.published) {
679 tipc_printf(buf, " bound to");
680 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 if (publ->lower == publ->upper)
682 tipc_printf(buf, " {%u,%u}", publ->type,
683 publ->lower);
684 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900685 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900687 }
688 }
689 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690}
691
692#define MAX_PORT_QUERY 32768
693
Per Liden4323add2006-01-18 00:38:21 +0100694struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695{
696 struct sk_buff *buf;
697 struct tlv_desc *rep_tlv;
698 struct print_buf pb;
699 struct port *p_ptr;
700 int str_len;
701
Per Liden4323add2006-01-18 00:38:21 +0100702 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 if (!buf)
704 return NULL;
705 rep_tlv = (struct tlv_desc *)buf->data;
706
Per Liden4323add2006-01-18 00:38:21 +0100707 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
708 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 list_for_each_entry(p_ptr, &ports, port_list) {
710 spin_lock_bh(p_ptr->publ.lock);
711 port_print(p_ptr, &pb, 0);
712 spin_unlock_bh(p_ptr->publ.lock);
713 }
Per Liden4323add2006-01-18 00:38:21 +0100714 spin_unlock_bh(&tipc_port_list_lock);
715 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716
717 skb_put(buf, TLV_SPACE(str_len));
718 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
719
720 return buf;
721}
722
723#if 0
724
725#define MAX_PORT_STATS 2000
726
727struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
728{
729 u32 ref;
730 struct port *p_ptr;
731 struct sk_buff *buf;
732 struct tlv_desc *rep_tlv;
733 struct print_buf pb;
734 int str_len;
735
736 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
737 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
738
739 ref = *(u32 *)TLV_DATA(req_tlv_area);
740 ref = ntohl(ref);
741
Per Liden4323add2006-01-18 00:38:21 +0100742 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 if (!p_ptr)
744 return cfg_reply_error_string("port not found");
745
Per Liden4323add2006-01-18 00:38:21 +0100746 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 if (!buf) {
Per Liden4323add2006-01-18 00:38:21 +0100748 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 return NULL;
750 }
751 rep_tlv = (struct tlv_desc *)buf->data;
752
Per Liden4323add2006-01-18 00:38:21 +0100753 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 port_print(p_ptr, &pb, 1);
755 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
Per Liden4323add2006-01-18 00:38:21 +0100756 tipc_port_unlock(p_ptr);
757 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758
759 skb_put(buf, TLV_SPACE(str_len));
760 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
761
762 return buf;
763}
764
765#endif
766
Per Liden4323add2006-01-18 00:38:21 +0100767void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768{
769 struct port *p_ptr;
770 struct tipc_msg *msg;
771
Per Liden4323add2006-01-18 00:38:21 +0100772 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 list_for_each_entry(p_ptr, &ports, port_list) {
774 msg = &p_ptr->publ.phdr;
775 if (msg_orignode(msg) == tipc_own_addr)
776 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700777 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 msg_set_orignode(msg, tipc_own_addr);
779 }
Per Liden4323add2006-01-18 00:38:21 +0100780 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781}
782
783
784/*
785 * port_dispatcher_sigh(): Signal handler for messages destinated
786 * to the tipc_port interface.
787 */
788
789static void port_dispatcher_sigh(void *dummy)
790{
791 struct sk_buff *buf;
792
793 spin_lock_bh(&queue_lock);
794 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800795 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 spin_unlock_bh(&queue_lock);
797
798 while (buf) {
799 struct port *p_ptr;
800 struct user_port *up_ptr;
801 struct tipc_portid orig;
802 struct tipc_name_seq dseq;
803 void *usr_handle;
804 int connected;
805 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700806 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807
808 struct sk_buff *next = buf->next;
809 struct tipc_msg *msg = buf_msg(buf);
810 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811
Allan Stephens96882432006-06-25 23:38:58 -0700812 message_type = msg_type(msg);
813 if (message_type > TIPC_DIRECT_MSG)
814 goto reject; /* Unsupported message type */
815
Per Liden4323add2006-01-18 00:38:21 +0100816 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700817 if (!p_ptr)
818 goto reject; /* Port deleted while msg in queue */
819
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 orig.ref = msg_origport(msg);
821 orig.node = msg_orignode(msg);
822 up_ptr = p_ptr->user_port;
823 usr_handle = up_ptr->usr_handle;
824 connected = p_ptr->publ.connected;
825 published = p_ptr->publ.published;
826
827 if (unlikely(msg_errcode(msg)))
828 goto err;
829
Allan Stephens96882432006-06-25 23:38:58 -0700830 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900831
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 case TIPC_CONN_MSG:{
833 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
834 u32 peer_port = port_peerport(p_ptr);
835 u32 peer_node = port_peernode(p_ptr);
836
Julia Lawall4cec72c2008-01-08 23:48:20 -0800837 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700838 if (unlikely(!cb))
839 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700841 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700843 } else if ((msg_origport(msg) != peer_port) ||
844 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900846 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900848 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 p_ptr->publ.conn_unacked);
850 skb_pull(buf, msg_hdr_sz(msg));
851 cb(usr_handle, dref, &buf, msg_data(msg),
852 msg_data_sz(msg));
853 break;
854 }
855 case TIPC_DIRECT_MSG:{
856 tipc_msg_event cb = up_ptr->msg_cb;
857
Julia Lawall4cec72c2008-01-08 23:48:20 -0800858 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700859 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860 goto reject;
861 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900862 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 msg_data_sz(msg), msg_importance(msg),
864 &orig);
865 break;
866 }
Allan Stephens96882432006-06-25 23:38:58 -0700867 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 case TIPC_NAMED_MSG:{
869 tipc_named_msg_event cb = up_ptr->named_msg_cb;
870
Julia Lawall4cec72c2008-01-08 23:48:20 -0800871 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700872 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 goto reject;
874 dseq.type = msg_nametype(msg);
875 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700876 dseq.upper = (message_type == TIPC_NAMED_MSG)
877 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 msg_data_sz(msg), msg_importance(msg),
881 &orig, &dseq);
882 break;
883 }
884 }
885 if (buf)
886 buf_discard(buf);
887 buf = next;
888 continue;
889err:
Allan Stephens96882432006-06-25 23:38:58 -0700890 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900891
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900893 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 up_ptr->conn_err_cb;
895 u32 peer_port = port_peerport(p_ptr);
896 u32 peer_node = port_peernode(p_ptr);
897
Julia Lawall4cec72c2008-01-08 23:48:20 -0800898 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700899 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700901 if ((msg_origport(msg) != peer_port) ||
902 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 break;
904 tipc_disconnect(dref);
905 skb_pull(buf, msg_hdr_sz(msg));
906 cb(usr_handle, dref, &buf, msg_data(msg),
907 msg_data_sz(msg), msg_errcode(msg));
908 break;
909 }
910 case TIPC_DIRECT_MSG:{
911 tipc_msg_err_event cb = up_ptr->err_cb;
912
Julia Lawall4cec72c2008-01-08 23:48:20 -0800913 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700914 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 break;
916 skb_pull(buf, msg_hdr_sz(msg));
917 cb(usr_handle, dref, &buf, msg_data(msg),
918 msg_data_sz(msg), msg_errcode(msg), &orig);
919 break;
920 }
Allan Stephens96882432006-06-25 23:38:58 -0700921 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900923 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 up_ptr->named_err_cb;
925
Julia Lawall4cec72c2008-01-08 23:48:20 -0800926 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700927 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 break;
929 dseq.type = msg_nametype(msg);
930 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700931 dseq.upper = (message_type == TIPC_NAMED_MSG)
932 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900934 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 msg_data_sz(msg), msg_errcode(msg), &dseq);
936 break;
937 }
938 }
939 if (buf)
940 buf_discard(buf);
941 buf = next;
942 continue;
943reject:
944 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
945 buf = next;
946 }
947}
948
949/*
950 * port_dispatcher(): Dispatcher for messages destinated
951 * to the tipc_port interface. Called with port locked.
952 */
953
954static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
955{
956 buf->next = NULL;
957 spin_lock_bh(&queue_lock);
958 if (msg_queue_head) {
959 msg_queue_tail->next = buf;
960 msg_queue_tail = buf;
961 } else {
962 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100963 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 }
965 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700966 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967}
968
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900969/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900971 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 */
973
974static void port_wakeup_sh(unsigned long ref)
975{
976 struct port *p_ptr;
977 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800978 tipc_continue_event cb = NULL;
979 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980
Per Liden4323add2006-01-18 00:38:21 +0100981 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 if (p_ptr) {
983 up_ptr = p_ptr->user_port;
984 if (up_ptr) {
985 cb = up_ptr->continue_event_cb;
986 uh = up_ptr->usr_handle;
987 }
Per Liden4323add2006-01-18 00:38:21 +0100988 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 }
990 if (cb)
991 cb(uh, ref);
992}
993
994
995static void port_wakeup(struct tipc_port *p_ptr)
996{
Per Liden4323add2006-01-18 00:38:21 +0100997 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998}
999
1000void tipc_acknowledge(u32 ref, u32 ack)
1001{
1002 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001003 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004
Per Liden4323add2006-01-18 00:38:21 +01001005 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 if (!p_ptr)
1007 return;
1008 if (p_ptr->publ.connected) {
1009 p_ptr->publ.conn_unacked -= ack;
1010 buf = port_build_proto_msg(port_peerport(p_ptr),
1011 port_peernode(p_ptr),
1012 ref,
1013 tipc_own_addr,
1014 CONN_MANAGER,
1015 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001016 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 port_out_seqno(p_ptr),
1018 ack);
1019 }
Per Liden4323add2006-01-18 00:38:21 +01001020 tipc_port_unlock(p_ptr);
1021 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022}
1023
1024/*
1025 * tipc_createport(): user level call. Will add port to
1026 * registry if non-zero user_ref.
1027 */
1028
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029int tipc_createport(u32 user_ref,
1030 void *usr_handle,
1031 unsigned int importance,
1032 tipc_msg_err_event error_cb,
1033 tipc_named_msg_err_event named_error_cb,
1034 tipc_conn_shutdown_event conn_error_cb,
1035 tipc_msg_event msg_cb,
1036 tipc_named_msg_event named_msg_cb,
1037 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038 tipc_continue_event continue_event_cb,/* May be zero */
1039 u32 *portref)
1040{
1041 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001042 struct port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001044 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -07001045 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -07001046 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 return -ENOMEM;
1048 }
Allan Stephens0ea52242008-07-14 22:42:19 -07001049 p_ptr = (struct port *)tipc_createport_raw(NULL, port_dispatcher,
1050 port_wakeup, importance);
1051 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 kfree(up_ptr);
1053 return -ENOMEM;
1054 }
1055
1056 p_ptr->user_port = up_ptr;
1057 up_ptr->user_ref = user_ref;
1058 up_ptr->usr_handle = usr_handle;
1059 up_ptr->ref = p_ptr->publ.ref;
1060 up_ptr->err_cb = error_cb;
1061 up_ptr->named_err_cb = named_error_cb;
1062 up_ptr->conn_err_cb = conn_error_cb;
1063 up_ptr->msg_cb = msg_cb;
1064 up_ptr->named_msg_cb = named_msg_cb;
1065 up_ptr->conn_msg_cb = conn_msg_cb;
1066 up_ptr->continue_event_cb = continue_event_cb;
1067 INIT_LIST_HEAD(&up_ptr->uport_list);
Per Liden4323add2006-01-18 00:38:21 +01001068 tipc_reg_add_port(up_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 *portref = p_ptr->publ.ref;
Per Liden4323add2006-01-18 00:38:21 +01001070 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001071 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072}
1073
1074int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1075{
1076 id->ref = ref;
1077 id->node = tipc_own_addr;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001078 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079}
1080
1081int tipc_portimportance(u32 ref, unsigned int *importance)
1082{
1083 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001084
Per Liden4323add2006-01-18 00:38:21 +01001085 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 if (!p_ptr)
1087 return -EINVAL;
1088 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001089 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001090 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091}
1092
1093int tipc_set_portimportance(u32 ref, unsigned int imp)
1094{
1095 struct port *p_ptr;
1096
1097 if (imp > TIPC_CRITICAL_IMPORTANCE)
1098 return -EINVAL;
1099
Per Liden4323add2006-01-18 00:38:21 +01001100 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 if (!p_ptr)
1102 return -EINVAL;
1103 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001104 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001105 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106}
1107
1108
1109int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1110{
1111 struct port *p_ptr;
1112 struct publication *publ;
1113 u32 key;
1114 int res = -EINVAL;
1115
Per Liden4323add2006-01-18 00:38:21 +01001116 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001117 if (!p_ptr)
1118 return -EINVAL;
1119
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1121 "lower = %u, upper = %u\n",
1122 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 if (p_ptr->publ.connected)
1124 goto exit;
1125 if (seq->lower > seq->upper)
1126 goto exit;
1127 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1128 goto exit;
1129 key = ref + p_ptr->pub_count + 1;
1130 if (key == ref) {
1131 res = -EADDRINUSE;
1132 goto exit;
1133 }
Per Liden4323add2006-01-18 00:38:21 +01001134 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1135 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 if (publ) {
1137 list_add(&publ->pport_list, &p_ptr->publications);
1138 p_ptr->pub_count++;
1139 p_ptr->publ.published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001140 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 }
1142exit:
Per Liden4323add2006-01-18 00:38:21 +01001143 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 return res;
1145}
1146
1147int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1148{
1149 struct port *p_ptr;
1150 struct publication *publ;
1151 struct publication *tpubl;
1152 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001153
Per Liden4323add2006-01-18 00:38:21 +01001154 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 if (!p_ptr)
1156 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001158 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001160 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001161 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001163 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001165 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 &p_ptr->publications, pport_list) {
1167 if (publ->scope != scope)
1168 continue;
1169 if (publ->type != seq->type)
1170 continue;
1171 if (publ->lower != seq->lower)
1172 continue;
1173 if (publ->upper != seq->upper)
1174 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001175 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001176 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001177 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 break;
1179 }
1180 }
1181 if (list_empty(&p_ptr->publications))
1182 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001183 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 return res;
1185}
1186
1187int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1188{
1189 struct port *p_ptr;
1190 struct tipc_msg *msg;
1191 int res = -EINVAL;
1192
Per Liden4323add2006-01-18 00:38:21 +01001193 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 if (!p_ptr)
1195 return -EINVAL;
1196 if (p_ptr->publ.published || p_ptr->publ.connected)
1197 goto exit;
1198 if (!peer->ref)
1199 goto exit;
1200
1201 msg = &p_ptr->publ.phdr;
1202 msg_set_destnode(msg, peer->node);
1203 msg_set_destport(msg, peer->ref);
1204 msg_set_orignode(msg, tipc_own_addr);
1205 msg_set_origport(msg, p_ptr->publ.ref);
1206 msg_set_transp_seqno(msg, 42);
1207 msg_set_type(msg, TIPC_CONN_MSG);
1208 if (!may_route(peer->node))
1209 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1210 else
1211 msg_set_hdr_sz(msg, LONG_H_SIZE);
1212
1213 p_ptr->probing_interval = PROBING_INTERVAL;
1214 p_ptr->probing_state = CONFIRMED;
1215 p_ptr->publ.connected = 1;
1216 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1217
Per Liden4323add2006-01-18 00:38:21 +01001218 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001219 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001221 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222exit:
Per Liden4323add2006-01-18 00:38:21 +01001223 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001224 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 return res;
1226}
1227
Allan Stephens0c3141e2008-04-15 00:22:02 -07001228/**
1229 * tipc_disconnect_port - disconnect port from peer
1230 *
1231 * Port must be locked.
1232 */
1233
1234int tipc_disconnect_port(struct tipc_port *tp_ptr)
1235{
1236 int res;
1237
1238 if (tp_ptr->connected) {
1239 tp_ptr->connected = 0;
1240 /* let timer expire on it's own to avoid deadlock! */
1241 tipc_nodesub_unsubscribe(
1242 &((struct port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001243 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001244 } else {
1245 res = -ENOTCONN;
1246 }
1247 return res;
1248}
1249
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250/*
1251 * tipc_disconnect(): Disconnect port form peer.
1252 * This is a node local operation.
1253 */
1254
1255int tipc_disconnect(u32 ref)
1256{
1257 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001258 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259
Per Liden4323add2006-01-18 00:38:21 +01001260 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 if (!p_ptr)
1262 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001263 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001264 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265 return res;
1266}
1267
1268/*
1269 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1270 */
1271int tipc_shutdown(u32 ref)
1272{
1273 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001274 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275
Per Liden4323add2006-01-18 00:38:21 +01001276 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 if (!p_ptr)
1278 return -EINVAL;
1279
1280 if (p_ptr->publ.connected) {
1281 u32 imp = msg_importance(&p_ptr->publ.phdr);
1282 if (imp < TIPC_CRITICAL_IMPORTANCE)
1283 imp++;
1284 buf = port_build_proto_msg(port_peerport(p_ptr),
1285 port_peernode(p_ptr),
1286 ref,
1287 tipc_own_addr,
1288 imp,
1289 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001290 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291 port_out_seqno(p_ptr),
1292 0);
1293 }
Per Liden4323add2006-01-18 00:38:21 +01001294 tipc_port_unlock(p_ptr);
1295 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 return tipc_disconnect(ref);
1297}
1298
1299int tipc_isconnected(u32 ref, int *isconnected)
1300{
1301 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001302
Per Liden4323add2006-01-18 00:38:21 +01001303 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 if (!p_ptr)
1305 return -EINVAL;
1306 *isconnected = p_ptr->publ.connected;
Per Liden4323add2006-01-18 00:38:21 +01001307 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001308 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309}
1310
1311int tipc_peer(u32 ref, struct tipc_portid *peer)
1312{
1313 struct port *p_ptr;
1314 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001315
Per Liden4323add2006-01-18 00:38:21 +01001316 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 if (!p_ptr)
1318 return -EINVAL;
1319 if (p_ptr->publ.connected) {
1320 peer->ref = port_peerport(p_ptr);
1321 peer->node = port_peernode(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001322 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 } else
1324 res = -ENOTCONN;
Per Liden4323add2006-01-18 00:38:21 +01001325 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 return res;
1327}
1328
1329int tipc_ref_valid(u32 ref)
1330{
1331 /* Works irrespective of type */
Per Liden4323add2006-01-18 00:38:21 +01001332 return !!tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333}
1334
1335
1336/*
Per Liden4323add2006-01-18 00:38:21 +01001337 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 * message for this node.
1339 */
1340
Per Liden4323add2006-01-18 00:38:21 +01001341int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 struct iovec const *msg_sect)
1343{
1344 struct sk_buff *buf;
1345 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001346
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1348 MAX_MSG_SIZE, !sender->user_port, &buf);
1349 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001350 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 return res;
1352}
1353
1354/**
1355 * tipc_send - send message sections on connection
1356 */
1357
1358int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1359{
1360 struct port *p_ptr;
1361 u32 destnode;
1362 int res;
1363
Per Liden4323add2006-01-18 00:38:21 +01001364 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 if (!p_ptr || !p_ptr->publ.connected)
1366 return -EINVAL;
1367
1368 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001369 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 destnode = port_peernode(p_ptr);
1371 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001372 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1373 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 else
Per Liden4323add2006-01-18 00:38:21 +01001375 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376
1377 if (likely(res != -ELINKCONG)) {
1378 port_incr_out_seqno(p_ptr);
1379 p_ptr->publ.congested = 0;
1380 p_ptr->sent++;
1381 return res;
1382 }
1383 }
1384 if (port_unreliable(p_ptr)) {
1385 p_ptr->publ.congested = 0;
1386 /* Just calculate msg length and return */
1387 return msg_calc_data_size(msg_sect, num_sect);
1388 }
1389 return -ELINKCONG;
1390}
1391
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001392/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 * tipc_send_buf - send message buffer on connection
1394 */
1395
1396int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1397{
1398 struct port *p_ptr;
1399 struct tipc_msg *msg;
1400 u32 destnode;
1401 u32 hsz;
1402 u32 sz;
1403 u32 res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001404
Per Liden4323add2006-01-18 00:38:21 +01001405 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 if (!p_ptr || !p_ptr->publ.connected)
1407 return -EINVAL;
1408
1409 msg = &p_ptr->publ.phdr;
1410 hsz = msg_hdr_sz(msg);
1411 sz = hsz + dsz;
1412 msg_set_size(msg, sz);
1413 if (skb_cow(buf, hsz))
1414 return -ENOMEM;
1415
1416 skb_push(buf, hsz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001417 skb_copy_to_linear_data(buf, msg, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 destnode = msg_destnode(msg);
1419 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001420 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 if (likely(destnode != tipc_own_addr))
1422 res = tipc_send_buf_fast(buf, destnode);
1423 else {
Per Liden4323add2006-01-18 00:38:21 +01001424 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425 res = sz;
1426 }
1427 if (likely(res != -ELINKCONG)) {
1428 port_incr_out_seqno(p_ptr);
1429 p_ptr->sent++;
1430 p_ptr->publ.congested = 0;
1431 return res;
1432 }
1433 }
1434 if (port_unreliable(p_ptr)) {
1435 p_ptr->publ.congested = 0;
1436 return dsz;
1437 }
1438 return -ELINKCONG;
1439}
1440
1441/**
1442 * tipc_forward2name - forward message sections to port name
1443 */
1444
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001445int tipc_forward2name(u32 ref,
1446 struct tipc_name const *name,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001448 u32 num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001450 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 unsigned int importance)
1452{
1453 struct port *p_ptr;
1454 struct tipc_msg *msg;
1455 u32 destnode = domain;
1456 u32 destport = 0;
1457 int res;
1458
Per Liden4323add2006-01-18 00:38:21 +01001459 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 if (!p_ptr || p_ptr->publ.connected)
1461 return -EINVAL;
1462
1463 msg = &p_ptr->publ.phdr;
1464 msg_set_type(msg, TIPC_NAMED_MSG);
1465 msg_set_orignode(msg, orig->node);
1466 msg_set_origport(msg, orig->ref);
1467 msg_set_hdr_sz(msg, LONG_H_SIZE);
1468 msg_set_nametype(msg, name->type);
1469 msg_set_nameinst(msg, name->instance);
1470 msg_set_lookup_scope(msg, addr_scope(domain));
1471 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1472 msg_set_importance(msg,importance);
Per Liden4323add2006-01-18 00:38:21 +01001473 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 msg_set_destnode(msg, destnode);
1475 msg_set_destport(msg, destport);
1476
1477 if (likely(destport || destnode)) {
1478 p_ptr->sent++;
1479 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001480 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001481 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001482 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 if (likely(res != -ELINKCONG))
1484 return res;
1485 if (port_unreliable(p_ptr)) {
1486 /* Just calculate msg length and return */
1487 return msg_calc_data_size(msg_sect, num_sect);
1488 }
1489 return -ELINKCONG;
1490 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001491 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001492 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493}
1494
1495/**
1496 * tipc_send2name - send message sections to port name
1497 */
1498
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001499int tipc_send2name(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 struct tipc_name const *name,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001501 unsigned int domain,
1502 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503 struct iovec const *msg_sect)
1504{
1505 struct tipc_portid orig;
1506
1507 orig.ref = ref;
1508 orig.node = tipc_own_addr;
1509 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1510 TIPC_PORT_IMPORTANCE);
1511}
1512
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001513/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 * tipc_forward_buf2name - forward message buffer to port name
1515 */
1516
1517int tipc_forward_buf2name(u32 ref,
1518 struct tipc_name const *name,
1519 u32 domain,
1520 struct sk_buff *buf,
1521 unsigned int dsz,
1522 struct tipc_portid const *orig,
1523 unsigned int importance)
1524{
1525 struct port *p_ptr;
1526 struct tipc_msg *msg;
1527 u32 destnode = domain;
1528 u32 destport = 0;
1529 int res;
1530
Per Liden4323add2006-01-18 00:38:21 +01001531 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 if (!p_ptr || p_ptr->publ.connected)
1533 return -EINVAL;
1534
1535 msg = &p_ptr->publ.phdr;
1536 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1537 msg_set_importance(msg, importance);
1538 msg_set_type(msg, TIPC_NAMED_MSG);
1539 msg_set_orignode(msg, orig->node);
1540 msg_set_origport(msg, orig->ref);
1541 msg_set_nametype(msg, name->type);
1542 msg_set_nameinst(msg, name->instance);
1543 msg_set_lookup_scope(msg, addr_scope(domain));
1544 msg_set_hdr_sz(msg, LONG_H_SIZE);
1545 msg_set_size(msg, LONG_H_SIZE + dsz);
Per Liden4323add2006-01-18 00:38:21 +01001546 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001547 msg_set_destnode(msg, destnode);
1548 msg_set_destport(msg, destport);
1549 msg_dbg(msg, "forw2name ==> ");
1550 if (skb_cow(buf, LONG_H_SIZE))
1551 return -ENOMEM;
1552 skb_push(buf, LONG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001553 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554 msg_dbg(buf_msg(buf),"PREP:");
1555 if (likely(destport || destnode)) {
1556 p_ptr->sent++;
1557 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001558 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 res = tipc_send_buf_fast(buf, destnode);
1560 if (likely(res != -ELINKCONG))
1561 return res;
1562 if (port_unreliable(p_ptr))
1563 return dsz;
1564 return -ELINKCONG;
1565 }
1566 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1567}
1568
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001569/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570 * tipc_send_buf2name - send message buffer to port name
1571 */
1572
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001573int tipc_send_buf2name(u32 ref,
1574 struct tipc_name const *dest,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001576 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 unsigned int dsz)
1578{
1579 struct tipc_portid orig;
1580
1581 orig.ref = ref;
1582 orig.node = tipc_own_addr;
1583 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1584 TIPC_PORT_IMPORTANCE);
1585}
1586
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001587/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588 * tipc_forward2port - forward message sections to port identity
1589 */
1590
1591int tipc_forward2port(u32 ref,
1592 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001593 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001595 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596 unsigned int importance)
1597{
1598 struct port *p_ptr;
1599 struct tipc_msg *msg;
1600 int res;
1601
Per Liden4323add2006-01-18 00:38:21 +01001602 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603 if (!p_ptr || p_ptr->publ.connected)
1604 return -EINVAL;
1605
1606 msg = &p_ptr->publ.phdr;
1607 msg_set_type(msg, TIPC_DIRECT_MSG);
1608 msg_set_orignode(msg, orig->node);
1609 msg_set_origport(msg, orig->ref);
1610 msg_set_destnode(msg, dest->node);
1611 msg_set_destport(msg, dest->ref);
1612 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1613 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1614 msg_set_importance(msg, importance);
1615 p_ptr->sent++;
1616 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001617 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1618 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 if (likely(res != -ELINKCONG))
1620 return res;
1621 if (port_unreliable(p_ptr)) {
1622 /* Just calculate msg length and return */
1623 return msg_calc_data_size(msg_sect, num_sect);
1624 }
1625 return -ELINKCONG;
1626}
1627
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001628/**
1629 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 */
1631
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001632int tipc_send2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001634 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635 struct iovec const *msg_sect)
1636{
1637 struct tipc_portid orig;
1638
1639 orig.ref = ref;
1640 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001641 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 TIPC_PORT_IMPORTANCE);
1643}
1644
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001645/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646 * tipc_forward_buf2port - forward message buffer to port identity
1647 */
1648int tipc_forward_buf2port(u32 ref,
1649 struct tipc_portid const *dest,
1650 struct sk_buff *buf,
1651 unsigned int dsz,
1652 struct tipc_portid const *orig,
1653 unsigned int importance)
1654{
1655 struct port *p_ptr;
1656 struct tipc_msg *msg;
1657 int res;
1658
Per Liden4323add2006-01-18 00:38:21 +01001659 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 if (!p_ptr || p_ptr->publ.connected)
1661 return -EINVAL;
1662
1663 msg = &p_ptr->publ.phdr;
1664 msg_set_type(msg, TIPC_DIRECT_MSG);
1665 msg_set_orignode(msg, orig->node);
1666 msg_set_origport(msg, orig->ref);
1667 msg_set_destnode(msg, dest->node);
1668 msg_set_destport(msg, dest->ref);
1669 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1670 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1671 msg_set_importance(msg, importance);
1672 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1673 if (skb_cow(buf, DIR_MSG_H_SIZE))
1674 return -ENOMEM;
1675
1676 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001677 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 msg_dbg(msg, "buf2port: ");
1679 p_ptr->sent++;
1680 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001681 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 res = tipc_send_buf_fast(buf, dest->node);
1683 if (likely(res != -ELINKCONG))
1684 return res;
1685 if (port_unreliable(p_ptr))
1686 return dsz;
1687 return -ELINKCONG;
1688}
1689
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001690/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 * tipc_send_buf2port - send message buffer to port identity
1692 */
1693
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001694int tipc_send_buf2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001696 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 unsigned int dsz)
1698{
1699 struct tipc_portid orig;
1700
1701 orig.ref = ref;
1702 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001703 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704 TIPC_PORT_IMPORTANCE);
1705}
1706