blob: c033cb87b964233815227ea2c16fe8ddbc5ac406 [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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_table.h"
41#include "user_reg.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042
43/* Connection management: */
44#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
45#define CONFIRMED 0
46#define PROBING 1
47
48#define MAX_REJECT_SIZE 1024
49
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080050static struct sk_buff *msg_queue_head = NULL;
51static struct sk_buff *msg_queue_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
Ingo Molnar34af9462006-06-27 02:53:55 -070053DEFINE_SPINLOCK(tipc_port_list_lock);
54static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010055
Per Liden4323add2006-01-18 00:38:21 +010056static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010057static void port_handle_node_down(unsigned long ref);
58static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
59static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
60static void port_timeout(unsigned long ref);
61
62
Sam Ravnborg05790c62006-03-20 22:37:04 -080063static u32 port_peernode(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010064{
65 return msg_destnode(&p_ptr->publ.phdr);
66}
67
Sam Ravnborg05790c62006-03-20 22:37:04 -080068static u32 port_peerport(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010069{
70 return msg_destport(&p_ptr->publ.phdr);
71}
72
Sam Ravnborg05790c62006-03-20 22:37:04 -080073static u32 port_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010074{
75 return msg_transp_seqno(&p_ptr->publ.phdr);
76}
77
Sam Ravnborg05790c62006-03-20 22:37:04 -080078static void port_incr_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010079{
80 struct tipc_msg *m = &p_ptr->publ.phdr;
81
82 if (likely(!msg_routed(m)))
83 return;
84 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
85}
86
87/**
88 * tipc_multicast - send a multicast message to local and remote destinations
89 */
90
Allan Stephens38f232e2010-11-30 12:00:59 +000091int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
Per Lidenb97bf3f2006-01-02 19:04:38 +010092 u32 num_sect, struct iovec const *msg_sect)
93{
94 struct tipc_msg *hdr;
95 struct sk_buff *buf;
96 struct sk_buff *ibuf = NULL;
97 struct port_list dports = {0, NULL, };
Per Liden4323add2006-01-18 00:38:21 +010098 struct port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010099 int ext_targets;
100 int res;
101
102 if (unlikely(!oport))
103 return -EINVAL;
104
105 /* Create multicast message */
106
107 hdr = &oport->publ.phdr;
108 msg_set_type(hdr, TIPC_MCAST_MSG);
109 msg_set_nametype(hdr, seq->type);
110 msg_set_namelower(hdr, seq->lower);
111 msg_set_nameupper(hdr, seq->upper);
112 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000113 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 !oport->user_port, &buf);
115 if (unlikely(!buf))
116 return res;
117
118 /* Figure out where to send multicast message */
119
Per Liden4323add2006-01-18 00:38:21 +0100120 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
121 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900122
123 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124
125 if (ext_targets) {
126 if (dports.count != 0) {
127 ibuf = skb_copy(buf, GFP_ATOMIC);
128 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100129 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130 buf_discard(buf);
131 return -ENOMEM;
132 }
133 }
Per Liden4323add2006-01-18 00:38:21 +0100134 res = tipc_bclink_send_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 if ((res < 0) && (dports.count != 0)) {
136 buf_discard(ibuf);
137 }
138 } else {
139 ibuf = buf;
140 }
141
142 if (res >= 0) {
143 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100144 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 } else {
Per Liden4323add2006-01-18 00:38:21 +0100146 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 }
148 return res;
149}
150
151/**
Per Liden4323add2006-01-18 00:38:21 +0100152 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900153 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 * If there is no port list, perform a lookup to create one
155 */
156
Per Liden4323add2006-01-18 00:38:21 +0100157void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158{
159 struct tipc_msg* msg;
160 struct port_list dports = {0, NULL, };
161 struct port_list *item = dp;
162 int cnt = 0;
163
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 msg = buf_msg(buf);
165
166 /* Create destination port list, if one wasn't supplied */
167
168 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100169 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 msg_namelower(msg),
171 msg_nameupper(msg),
172 TIPC_CLUSTER_SCOPE,
173 &dports);
174 item = dp = &dports;
175 }
176
177 /* Deliver a copy of message to each destination port */
178
179 if (dp->count != 0) {
180 if (dp->count == 1) {
181 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100182 tipc_port_recv_msg(buf);
183 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 return;
185 }
186 for (; cnt < dp->count; cnt++) {
187 int index = cnt % PLSIZE;
188 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
189
190 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700191 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 msg_dbg(msg, "LOST:");
193 goto exit;
194 }
195 if ((index == 0) && (cnt != 0)) {
196 item = item->next;
197 }
198 msg_set_destport(buf_msg(b),item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100199 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 }
201 }
202exit:
203 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100204 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205}
206
207/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700208 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900209 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700210 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 */
212
Allan Stephens0ea52242008-07-14 22:42:19 -0700213struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
215 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700216 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217{
218 struct port *p_ptr;
219 struct tipc_msg *msg;
220 u32 ref;
221
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700222 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700223 if (!p_ptr) {
224 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700225 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 }
Per Liden4323add2006-01-18 00:38:21 +0100227 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700229 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700231 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 }
233
Allan Stephens05646c92007-06-10 17:25:24 -0700234 p_ptr->publ.usr_handle = usr_handle;
235 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 p_ptr->publ.ref = ref;
237 msg = &p_ptr->publ.phdr;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000238 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 p_ptr->last_in_seqno = 41;
241 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 INIT_LIST_HEAD(&p_ptr->wait_list);
243 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 p_ptr->dispatcher = dispatcher;
245 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800246 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100248 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 INIT_LIST_HEAD(&p_ptr->publications);
250 INIT_LIST_HEAD(&p_ptr->port_list);
251 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100252 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens0ea52242008-07-14 22:42:19 -0700253 return &(p_ptr->publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254}
255
256int tipc_deleteport(u32 ref)
257{
258 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800259 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800261 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100262 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900263 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 return -EINVAL;
265
Per Liden4323add2006-01-18 00:38:21 +0100266 tipc_ref_discard(ref);
267 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268
269 k_cancel_timer(&p_ptr->timer);
270 if (p_ptr->publ.connected) {
271 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100272 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 }
274 if (p_ptr->user_port) {
Per Liden4323add2006-01-18 00:38:21 +0100275 tipc_reg_remove_port(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 kfree(p_ptr->user_port);
277 }
278
Per Liden4323add2006-01-18 00:38:21 +0100279 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 list_del(&p_ptr->port_list);
281 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100282 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 k_term_timer(&p_ptr->timer);
284 kfree(p_ptr);
285 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100286 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700287 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288}
289
Sam Ravnborg05790c62006-03-20 22:37:04 -0800290static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291{
292 return msg_src_droppable(&p_ptr->publ.phdr);
293}
294
295int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
296{
297 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298
Per Liden4323add2006-01-18 00:38:21 +0100299 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 if (!p_ptr)
301 return -EINVAL;
302 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800303 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700304 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305}
306
307int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
308{
309 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900310
Per Liden4323add2006-01-18 00:38:21 +0100311 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 if (!p_ptr)
313 return -EINVAL;
314 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100315 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700316 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317}
318
Sam Ravnborg05790c62006-03-20 22:37:04 -0800319static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320{
321 return msg_dest_droppable(&p_ptr->publ.phdr);
322}
323
324int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
325{
326 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900327
Per Liden4323add2006-01-18 00:38:21 +0100328 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 if (!p_ptr)
330 return -EINVAL;
331 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800332 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700333 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334}
335
336int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
337{
338 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339
Per Liden4323add2006-01-18 00:38:21 +0100340 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 if (!p_ptr)
342 return -EINVAL;
343 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100344 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700345 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346}
347
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348/*
349 * port_build_proto_msg(): build a port level protocol
350 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 * tipc_port lock on.
352 */
353static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
354 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900355 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 u32 seqno, u32 ack)
357{
358 struct sk_buff *buf;
359 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900360
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000361 buf = tipc_buf_acquire(LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 if (buf) {
363 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000364 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700365 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 msg_set_destport(msg, destport);
367 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 msg_set_orignode(msg, orignode);
369 msg_set_transp_seqno(msg, seqno);
370 msg_set_msgcnt(msg, ack);
371 msg_dbg(msg, "PORT>SEND>:");
372 }
373 return buf;
374}
375
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376int tipc_reject_msg(struct sk_buff *buf, u32 err)
377{
378 struct tipc_msg *msg = buf_msg(buf);
379 struct sk_buff *rbuf;
380 struct tipc_msg *rmsg;
381 int hdr_sz;
382 u32 imp = msg_importance(msg);
383 u32 data_sz = msg_data_sz(msg);
384
385 if (data_sz > MAX_REJECT_SIZE)
386 data_sz = MAX_REJECT_SIZE;
387 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
388 imp++;
389 msg_dbg(msg, "port->rej: ");
390
391 /* discard rejected message if it shouldn't be returned to sender */
392 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
393 buf_discard(buf);
394 return data_sz;
395 }
396
397 /* construct rejected message */
398 if (msg_mcast(msg))
399 hdr_sz = MCAST_H_SIZE;
400 else
401 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000402 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 if (rbuf == NULL) {
404 buf_discard(buf);
405 return data_sz;
406 }
407 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000408 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700409 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700412 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700414 /* leave name type & instance as zeroes */
415 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700417 msg_set_nametype(rmsg, msg_nametype(msg));
418 msg_set_nameinst(rmsg, msg_nameinst(msg));
419 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900420 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300421 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422
423 /* send self-abort message when rejecting on a connected port */
424 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800425 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100426 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427
428 if (p_ptr) {
429 if (p_ptr->publ.connected)
430 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100431 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 }
Per Liden4323add2006-01-18 00:38:21 +0100433 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 }
435
436 /* send rejected message */
437 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100438 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 return data_sz;
440}
441
Per Liden4323add2006-01-18 00:38:21 +0100442int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
443 struct iovec const *msg_sect, u32 num_sect,
444 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445{
446 struct sk_buff *buf;
447 int res;
448
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000449 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 !p_ptr->user_port, &buf);
451 if (!buf)
452 return res;
453
454 return tipc_reject_msg(buf, err);
455}
456
457static void port_timeout(unsigned long ref)
458{
Per Liden4323add2006-01-18 00:38:21 +0100459 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800460 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461
Allan Stephens065fd172006-10-16 21:38:05 -0700462 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 return;
464
Allan Stephens065fd172006-10-16 21:38:05 -0700465 if (!p_ptr->publ.connected) {
466 tipc_port_unlock(p_ptr);
467 return;
468 }
469
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 /* Last probe answered ? */
471 if (p_ptr->probing_state == PROBING) {
472 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
473 } else {
474 buf = port_build_proto_msg(port_peerport(p_ptr),
475 port_peernode(p_ptr),
476 p_ptr->publ.ref,
477 tipc_own_addr,
478 CONN_MANAGER,
479 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900480 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 port_out_seqno(p_ptr),
482 0);
483 port_incr_out_seqno(p_ptr);
484 p_ptr->probing_state = PROBING;
485 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
486 }
Per Liden4323add2006-01-18 00:38:21 +0100487 tipc_port_unlock(p_ptr);
488 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489}
490
491
492static void port_handle_node_down(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
497 if (!p_ptr)
498 return;
499 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100500 tipc_port_unlock(p_ptr);
501 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502}
503
504
505static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
506{
507 u32 imp = msg_importance(&p_ptr->publ.phdr);
508
509 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800510 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 if (imp < TIPC_CRITICAL_IMPORTANCE)
512 imp++;
513 return port_build_proto_msg(p_ptr->publ.ref,
514 tipc_own_addr,
515 port_peerport(p_ptr),
516 port_peernode(p_ptr),
517 imp,
518 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900519 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 p_ptr->last_in_seqno + 1,
521 0);
522}
523
524
525static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
526{
527 u32 imp = msg_importance(&p_ptr->publ.phdr);
528
529 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800530 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 if (imp < TIPC_CRITICAL_IMPORTANCE)
532 imp++;
533 return port_build_proto_msg(port_peerport(p_ptr),
534 port_peernode(p_ptr),
535 p_ptr->publ.ref,
536 tipc_own_addr,
537 imp,
538 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900539 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 port_out_seqno(p_ptr),
541 0);
542}
543
Per Liden4323add2006-01-18 00:38:21 +0100544void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545{
546 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100547 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800549 struct sk_buff *r_buf = NULL;
550 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551
552 msg_dbg(msg, "PORT<RECV<:");
553
554 if (!p_ptr) {
555 err = TIPC_ERR_NO_PORT;
556 } else if (p_ptr->publ.connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000557 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
558 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000560 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 p_ptr->publ.congested &&
563 p_ptr->wakeup;
564 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100565 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 goto exit;
567 p_ptr->publ.congested = 0;
568 if (!wakeup)
569 goto exit;
570 p_ptr->wakeup(&p_ptr->publ);
571 goto exit;
572 }
573 } else if (p_ptr->publ.published) {
574 err = TIPC_ERR_NO_PORT;
575 }
576 if (err) {
577 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900578 msg_orignode(msg),
579 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800581 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 TIPC_CONN_MSG,
583 err,
584 0,
585 0);
586 goto exit;
587 }
588
589 /* All is fine */
590 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900591 r_buf = port_build_proto_msg(msg_origport(msg),
592 msg_orignode(msg),
593 msg_destport(msg),
594 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 CONN_MANAGER,
596 CONN_PROBE_REPLY,
597 TIPC_OK,
598 port_out_seqno(p_ptr),
599 0);
600 }
601 p_ptr->probing_state = CONFIRMED;
602 port_incr_out_seqno(p_ptr);
603exit:
604 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100605 tipc_port_unlock(p_ptr);
606 tipc_net_route_msg(r_buf);
607 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 buf_discard(buf);
609}
610
611static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
612{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900613 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614
615 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900616 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 else
620 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
621
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900622 if (p_ptr->publ.connected) {
623 u32 dport = port_peerport(p_ptr);
624 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900626 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
627 tipc_zone(destnode), tipc_cluster(destnode),
628 tipc_node(destnode), dport);
629 if (p_ptr->publ.conn_type != 0)
630 tipc_printf(buf, " via {%u,%u}",
631 p_ptr->publ.conn_type,
632 p_ptr->publ.conn_instance);
633 }
634 else if (p_ptr->publ.published) {
635 tipc_printf(buf, " bound to");
636 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 if (publ->lower == publ->upper)
638 tipc_printf(buf, " {%u,%u}", publ->type,
639 publ->lower);
640 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900643 }
644 }
645 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646}
647
648#define MAX_PORT_QUERY 32768
649
Per Liden4323add2006-01-18 00:38:21 +0100650struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651{
652 struct sk_buff *buf;
653 struct tlv_desc *rep_tlv;
654 struct print_buf pb;
655 struct port *p_ptr;
656 int str_len;
657
Per Liden4323add2006-01-18 00:38:21 +0100658 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 if (!buf)
660 return NULL;
661 rep_tlv = (struct tlv_desc *)buf->data;
662
Per Liden4323add2006-01-18 00:38:21 +0100663 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
664 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 list_for_each_entry(p_ptr, &ports, port_list) {
666 spin_lock_bh(p_ptr->publ.lock);
667 port_print(p_ptr, &pb, 0);
668 spin_unlock_bh(p_ptr->publ.lock);
669 }
Per Liden4323add2006-01-18 00:38:21 +0100670 spin_unlock_bh(&tipc_port_list_lock);
671 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672
673 skb_put(buf, TLV_SPACE(str_len));
674 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
675
676 return buf;
677}
678
Per Liden4323add2006-01-18 00:38:21 +0100679void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680{
681 struct port *p_ptr;
682 struct tipc_msg *msg;
683
Per Liden4323add2006-01-18 00:38:21 +0100684 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 list_for_each_entry(p_ptr, &ports, port_list) {
686 msg = &p_ptr->publ.phdr;
687 if (msg_orignode(msg) == tipc_own_addr)
688 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700689 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 msg_set_orignode(msg, tipc_own_addr);
691 }
Per Liden4323add2006-01-18 00:38:21 +0100692 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693}
694
695
696/*
697 * port_dispatcher_sigh(): Signal handler for messages destinated
698 * to the tipc_port interface.
699 */
700
701static void port_dispatcher_sigh(void *dummy)
702{
703 struct sk_buff *buf;
704
705 spin_lock_bh(&queue_lock);
706 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800707 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 spin_unlock_bh(&queue_lock);
709
710 while (buf) {
711 struct port *p_ptr;
712 struct user_port *up_ptr;
713 struct tipc_portid orig;
714 struct tipc_name_seq dseq;
715 void *usr_handle;
716 int connected;
717 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700718 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719
720 struct sk_buff *next = buf->next;
721 struct tipc_msg *msg = buf_msg(buf);
722 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723
Allan Stephens96882432006-06-25 23:38:58 -0700724 message_type = msg_type(msg);
725 if (message_type > TIPC_DIRECT_MSG)
726 goto reject; /* Unsupported message type */
727
Per Liden4323add2006-01-18 00:38:21 +0100728 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700729 if (!p_ptr)
730 goto reject; /* Port deleted while msg in queue */
731
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 orig.ref = msg_origport(msg);
733 orig.node = msg_orignode(msg);
734 up_ptr = p_ptr->user_port;
735 usr_handle = up_ptr->usr_handle;
736 connected = p_ptr->publ.connected;
737 published = p_ptr->publ.published;
738
739 if (unlikely(msg_errcode(msg)))
740 goto err;
741
Allan Stephens96882432006-06-25 23:38:58 -0700742 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900743
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 case TIPC_CONN_MSG:{
745 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
746 u32 peer_port = port_peerport(p_ptr);
747 u32 peer_node = port_peernode(p_ptr);
748
Julia Lawall4cec72c2008-01-08 23:48:20 -0800749 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700750 if (unlikely(!cb))
751 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700753 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700755 } else if ((msg_origport(msg) != peer_port) ||
756 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900758 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900760 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 p_ptr->publ.conn_unacked);
762 skb_pull(buf, msg_hdr_sz(msg));
763 cb(usr_handle, dref, &buf, msg_data(msg),
764 msg_data_sz(msg));
765 break;
766 }
767 case TIPC_DIRECT_MSG:{
768 tipc_msg_event cb = up_ptr->msg_cb;
769
Julia Lawall4cec72c2008-01-08 23:48:20 -0800770 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700771 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 goto reject;
773 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900774 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 msg_data_sz(msg), msg_importance(msg),
776 &orig);
777 break;
778 }
Allan Stephens96882432006-06-25 23:38:58 -0700779 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 case TIPC_NAMED_MSG:{
781 tipc_named_msg_event cb = up_ptr->named_msg_cb;
782
Julia Lawall4cec72c2008-01-08 23:48:20 -0800783 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700784 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 goto reject;
786 dseq.type = msg_nametype(msg);
787 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700788 dseq.upper = (message_type == TIPC_NAMED_MSG)
789 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900791 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 msg_data_sz(msg), msg_importance(msg),
793 &orig, &dseq);
794 break;
795 }
796 }
797 if (buf)
798 buf_discard(buf);
799 buf = next;
800 continue;
801err:
Allan Stephens96882432006-06-25 23:38:58 -0700802 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900803
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 up_ptr->conn_err_cb;
807 u32 peer_port = port_peerport(p_ptr);
808 u32 peer_node = port_peernode(p_ptr);
809
Julia Lawall4cec72c2008-01-08 23:48:20 -0800810 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700811 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700813 if ((msg_origport(msg) != peer_port) ||
814 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 break;
816 tipc_disconnect(dref);
817 skb_pull(buf, msg_hdr_sz(msg));
818 cb(usr_handle, dref, &buf, msg_data(msg),
819 msg_data_sz(msg), msg_errcode(msg));
820 break;
821 }
822 case TIPC_DIRECT_MSG:{
823 tipc_msg_err_event cb = up_ptr->err_cb;
824
Julia Lawall4cec72c2008-01-08 23:48:20 -0800825 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700826 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 break;
828 skb_pull(buf, msg_hdr_sz(msg));
829 cb(usr_handle, dref, &buf, msg_data(msg),
830 msg_data_sz(msg), msg_errcode(msg), &orig);
831 break;
832 }
Allan Stephens96882432006-06-25 23:38:58 -0700833 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 up_ptr->named_err_cb;
837
Julia Lawall4cec72c2008-01-08 23:48:20 -0800838 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700839 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 break;
841 dseq.type = msg_nametype(msg);
842 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700843 dseq.upper = (message_type == TIPC_NAMED_MSG)
844 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900846 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 msg_data_sz(msg), msg_errcode(msg), &dseq);
848 break;
849 }
850 }
851 if (buf)
852 buf_discard(buf);
853 buf = next;
854 continue;
855reject:
856 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
857 buf = next;
858 }
859}
860
861/*
862 * port_dispatcher(): Dispatcher for messages destinated
863 * to the tipc_port interface. Called with port locked.
864 */
865
866static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
867{
868 buf->next = NULL;
869 spin_lock_bh(&queue_lock);
870 if (msg_queue_head) {
871 msg_queue_tail->next = buf;
872 msg_queue_tail = buf;
873 } else {
874 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100875 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 }
877 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700878 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879}
880
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900883 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884 */
885
886static void port_wakeup_sh(unsigned long ref)
887{
888 struct port *p_ptr;
889 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800890 tipc_continue_event cb = NULL;
891 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892
Per Liden4323add2006-01-18 00:38:21 +0100893 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 if (p_ptr) {
895 up_ptr = p_ptr->user_port;
896 if (up_ptr) {
897 cb = up_ptr->continue_event_cb;
898 uh = up_ptr->usr_handle;
899 }
Per Liden4323add2006-01-18 00:38:21 +0100900 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 }
902 if (cb)
903 cb(uh, ref);
904}
905
906
907static void port_wakeup(struct tipc_port *p_ptr)
908{
Per Liden4323add2006-01-18 00:38:21 +0100909 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910}
911
912void tipc_acknowledge(u32 ref, u32 ack)
913{
914 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800915 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916
Per Liden4323add2006-01-18 00:38:21 +0100917 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 if (!p_ptr)
919 return;
920 if (p_ptr->publ.connected) {
921 p_ptr->publ.conn_unacked -= ack;
922 buf = port_build_proto_msg(port_peerport(p_ptr),
923 port_peernode(p_ptr),
924 ref,
925 tipc_own_addr,
926 CONN_MANAGER,
927 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900928 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929 port_out_seqno(p_ptr),
930 ack);
931 }
Per Liden4323add2006-01-18 00:38:21 +0100932 tipc_port_unlock(p_ptr);
933 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934}
935
936/*
937 * tipc_createport(): user level call. Will add port to
938 * registry if non-zero user_ref.
939 */
940
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900941int tipc_createport(u32 user_ref,
942 void *usr_handle,
943 unsigned int importance,
944 tipc_msg_err_event error_cb,
945 tipc_named_msg_err_event named_error_cb,
946 tipc_conn_shutdown_event conn_error_cb,
947 tipc_msg_event msg_cb,
948 tipc_named_msg_event named_msg_cb,
949 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 tipc_continue_event continue_event_cb,/* May be zero */
951 u32 *portref)
952{
953 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900954 struct port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700956 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700957 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700958 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 return -ENOMEM;
960 }
Allan Stephens0ea52242008-07-14 22:42:19 -0700961 p_ptr = (struct port *)tipc_createport_raw(NULL, port_dispatcher,
962 port_wakeup, importance);
963 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 kfree(up_ptr);
965 return -ENOMEM;
966 }
967
968 p_ptr->user_port = up_ptr;
969 up_ptr->user_ref = user_ref;
970 up_ptr->usr_handle = usr_handle;
971 up_ptr->ref = p_ptr->publ.ref;
972 up_ptr->err_cb = error_cb;
973 up_ptr->named_err_cb = named_error_cb;
974 up_ptr->conn_err_cb = conn_error_cb;
975 up_ptr->msg_cb = msg_cb;
976 up_ptr->named_msg_cb = named_msg_cb;
977 up_ptr->conn_msg_cb = conn_msg_cb;
978 up_ptr->continue_event_cb = continue_event_cb;
979 INIT_LIST_HEAD(&up_ptr->uport_list);
Per Liden4323add2006-01-18 00:38:21 +0100980 tipc_reg_add_port(up_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 *portref = p_ptr->publ.ref;
Per Liden4323add2006-01-18 00:38:21 +0100982 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700983 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984}
985
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986int tipc_portimportance(u32 ref, unsigned int *importance)
987{
988 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900989
Per Liden4323add2006-01-18 00:38:21 +0100990 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 if (!p_ptr)
992 return -EINVAL;
993 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800994 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700995 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996}
997
998int tipc_set_portimportance(u32 ref, unsigned int imp)
999{
1000 struct port *p_ptr;
1001
1002 if (imp > TIPC_CRITICAL_IMPORTANCE)
1003 return -EINVAL;
1004
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 -EINVAL;
1008 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001009 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001010 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011}
1012
1013
1014int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1015{
1016 struct port *p_ptr;
1017 struct publication *publ;
1018 u32 key;
1019 int res = -EINVAL;
1020
Per Liden4323add2006-01-18 00:38:21 +01001021 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001022 if (!p_ptr)
1023 return -EINVAL;
1024
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1026 "lower = %u, upper = %u\n",
1027 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 if (p_ptr->publ.connected)
1029 goto exit;
1030 if (seq->lower > seq->upper)
1031 goto exit;
1032 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1033 goto exit;
1034 key = ref + p_ptr->pub_count + 1;
1035 if (key == ref) {
1036 res = -EADDRINUSE;
1037 goto exit;
1038 }
Per Liden4323add2006-01-18 00:38:21 +01001039 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1040 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 if (publ) {
1042 list_add(&publ->pport_list, &p_ptr->publications);
1043 p_ptr->pub_count++;
1044 p_ptr->publ.published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001045 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 }
1047exit:
Per Liden4323add2006-01-18 00:38:21 +01001048 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 return res;
1050}
1051
1052int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1053{
1054 struct port *p_ptr;
1055 struct publication *publ;
1056 struct publication *tpubl;
1057 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001058
Per Liden4323add2006-01-18 00:38:21 +01001059 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 if (!p_ptr)
1061 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001063 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001065 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001066 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001068 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001070 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 &p_ptr->publications, pport_list) {
1072 if (publ->scope != scope)
1073 continue;
1074 if (publ->type != seq->type)
1075 continue;
1076 if (publ->lower != seq->lower)
1077 continue;
1078 if (publ->upper != seq->upper)
1079 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001080 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001081 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001082 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 break;
1084 }
1085 }
1086 if (list_empty(&p_ptr->publications))
1087 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001088 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 return res;
1090}
1091
1092int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1093{
1094 struct port *p_ptr;
1095 struct tipc_msg *msg;
1096 int res = -EINVAL;
1097
Per Liden4323add2006-01-18 00:38:21 +01001098 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099 if (!p_ptr)
1100 return -EINVAL;
1101 if (p_ptr->publ.published || p_ptr->publ.connected)
1102 goto exit;
1103 if (!peer->ref)
1104 goto exit;
1105
1106 msg = &p_ptr->publ.phdr;
1107 msg_set_destnode(msg, peer->node);
1108 msg_set_destport(msg, peer->ref);
1109 msg_set_orignode(msg, tipc_own_addr);
1110 msg_set_origport(msg, p_ptr->publ.ref);
1111 msg_set_transp_seqno(msg, 42);
1112 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens08c80e92010-12-31 18:59:17 +00001113 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114
1115 p_ptr->probing_interval = PROBING_INTERVAL;
1116 p_ptr->probing_state = CONFIRMED;
1117 p_ptr->publ.connected = 1;
1118 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1119
Per Liden4323add2006-01-18 00:38:21 +01001120 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001121 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001123 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124exit:
Per Liden4323add2006-01-18 00:38:21 +01001125 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001126 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 return res;
1128}
1129
Allan Stephens0c3141e2008-04-15 00:22:02 -07001130/**
1131 * tipc_disconnect_port - disconnect port from peer
1132 *
1133 * Port must be locked.
1134 */
1135
1136int tipc_disconnect_port(struct tipc_port *tp_ptr)
1137{
1138 int res;
1139
1140 if (tp_ptr->connected) {
1141 tp_ptr->connected = 0;
1142 /* let timer expire on it's own to avoid deadlock! */
1143 tipc_nodesub_unsubscribe(
1144 &((struct port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001145 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001146 } else {
1147 res = -ENOTCONN;
1148 }
1149 return res;
1150}
1151
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152/*
1153 * tipc_disconnect(): Disconnect port form peer.
1154 * This is a node local operation.
1155 */
1156
1157int tipc_disconnect(u32 ref)
1158{
1159 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001160 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161
Per Liden4323add2006-01-18 00:38:21 +01001162 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 if (!p_ptr)
1164 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001165 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001166 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 return res;
1168}
1169
1170/*
1171 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1172 */
1173int tipc_shutdown(u32 ref)
1174{
1175 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001176 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177
Per Liden4323add2006-01-18 00:38:21 +01001178 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 if (!p_ptr)
1180 return -EINVAL;
1181
1182 if (p_ptr->publ.connected) {
1183 u32 imp = msg_importance(&p_ptr->publ.phdr);
1184 if (imp < TIPC_CRITICAL_IMPORTANCE)
1185 imp++;
1186 buf = port_build_proto_msg(port_peerport(p_ptr),
1187 port_peernode(p_ptr),
1188 ref,
1189 tipc_own_addr,
1190 imp,
1191 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001192 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 port_out_seqno(p_ptr),
1194 0);
1195 }
Per Liden4323add2006-01-18 00:38:21 +01001196 tipc_port_unlock(p_ptr);
1197 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 return tipc_disconnect(ref);
1199}
1200
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201/*
Per Liden4323add2006-01-18 00:38:21 +01001202 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 * message for this node.
1204 */
1205
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001206static int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1207 struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208{
1209 struct sk_buff *buf;
1210 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001211
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001212 res = tipc_msg_build(&sender->publ.phdr, msg_sect, num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 MAX_MSG_SIZE, !sender->user_port, &buf);
1214 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001215 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 return res;
1217}
1218
1219/**
1220 * tipc_send - send message sections on connection
1221 */
1222
1223int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1224{
1225 struct port *p_ptr;
1226 u32 destnode;
1227 int res;
1228
Per Liden4323add2006-01-18 00:38:21 +01001229 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 if (!p_ptr || !p_ptr->publ.connected)
1231 return -EINVAL;
1232
1233 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001234 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 destnode = port_peernode(p_ptr);
1236 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001237 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1238 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 else
Per Liden4323add2006-01-18 00:38:21 +01001240 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241
1242 if (likely(res != -ELINKCONG)) {
1243 port_incr_out_seqno(p_ptr);
1244 p_ptr->publ.congested = 0;
1245 p_ptr->sent++;
1246 return res;
1247 }
1248 }
1249 if (port_unreliable(p_ptr)) {
1250 p_ptr->publ.congested = 0;
1251 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001252 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 }
1254 return -ELINKCONG;
1255}
1256
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001257/**
Allan Stephens12bae472010-11-30 12:01:02 +00001258 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 */
1260
Allan Stephens12bae472010-11-30 12:01:02 +00001261int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1262 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263{
1264 struct port *p_ptr;
1265 struct tipc_msg *msg;
1266 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001267 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 int res;
1269
Per Liden4323add2006-01-18 00:38:21 +01001270 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 if (!p_ptr || p_ptr->publ.connected)
1272 return -EINVAL;
1273
1274 msg = &p_ptr->publ.phdr;
1275 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001276 msg_set_orignode(msg, tipc_own_addr);
1277 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 msg_set_hdr_sz(msg, LONG_H_SIZE);
1279 msg_set_nametype(msg, name->type);
1280 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001281 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001282 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 msg_set_destnode(msg, destnode);
1284 msg_set_destport(msg, destport);
1285
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001286 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 p_ptr->sent++;
1288 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001289 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001290 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001291 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 if (likely(res != -ELINKCONG))
1293 return res;
1294 if (port_unreliable(p_ptr)) {
1295 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001296 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 }
1298 return -ELINKCONG;
1299 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001300 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001301 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302}
1303
1304/**
Allan Stephens12bae472010-11-30 12:01:02 +00001305 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 */
1307
Allan Stephens12bae472010-11-30 12:01:02 +00001308int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1309 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310{
1311 struct port *p_ptr;
1312 struct tipc_msg *msg;
1313 int res;
1314
Per Liden4323add2006-01-18 00:38:21 +01001315 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 if (!p_ptr || p_ptr->publ.connected)
1317 return -EINVAL;
1318
1319 msg = &p_ptr->publ.phdr;
1320 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001321 msg_set_orignode(msg, tipc_own_addr);
1322 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 msg_set_destnode(msg, dest->node);
1324 msg_set_destport(msg, dest->ref);
1325 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 p_ptr->sent++;
1327 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001328 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1329 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330 if (likely(res != -ELINKCONG))
1331 return res;
1332 if (port_unreliable(p_ptr)) {
1333 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001334 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 }
1336 return -ELINKCONG;
1337}
1338
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001339/**
Allan Stephens12bae472010-11-30 12:01:02 +00001340 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 */
1342
Allan Stephens12bae472010-11-30 12:01:02 +00001343int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1344 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345{
1346 struct port *p_ptr;
1347 struct tipc_msg *msg;
1348 int res;
1349
Per Liden4323add2006-01-18 00:38:21 +01001350 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 if (!p_ptr || p_ptr->publ.connected)
1352 return -EINVAL;
1353
1354 msg = &p_ptr->publ.phdr;
1355 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001356 msg_set_orignode(msg, tipc_own_addr);
1357 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 msg_set_destnode(msg, dest->node);
1359 msg_set_destport(msg, dest->ref);
1360 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1362 if (skb_cow(buf, DIR_MSG_H_SIZE))
1363 return -ENOMEM;
1364
1365 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001366 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 msg_dbg(msg, "buf2port: ");
1368 p_ptr->sent++;
1369 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001370 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 res = tipc_send_buf_fast(buf, dest->node);
1372 if (likely(res != -ELINKCONG))
1373 return res;
1374 if (port_unreliable(p_ptr))
1375 return dsz;
1376 return -ELINKCONG;
1377}
1378