blob: 067bab2a0b982864d70cf365d6ac3fbfe522568f [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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
42/* Connection management: */
43#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
44#define CONFIRMED 0
45#define PROBING 1
46
47#define MAX_REJECT_SIZE 1024
48
Allan Stephense3ec9c72010-12-31 18:59:34 +000049static struct sk_buff *msg_queue_head;
50static struct sk_buff *msg_queue_tail;
Per Lidenb97bf3f2006-01-02 19:04:38 +010051
Ingo Molnar34af9462006-06-27 02:53:55 -070052DEFINE_SPINLOCK(tipc_port_list_lock);
53static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Per Liden4323add2006-01-18 00:38:21 +010055static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010056static void port_handle_node_down(unsigned long ref);
Allan Stephens0e659672010-12-31 18:59:32 +000057static struct sk_buff *port_build_self_abort_msg(struct port *, u32 err);
58static struct sk_buff *port_build_peer_abort_msg(struct port *, u32 err);
Per Lidenb97bf3f2006-01-02 19:04:38 +010059static void port_timeout(unsigned long ref);
60
61
Sam Ravnborg05790c62006-03-20 22:37:04 -080062static u32 port_peernode(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010063{
64 return msg_destnode(&p_ptr->publ.phdr);
65}
66
Sam Ravnborg05790c62006-03-20 22:37:04 -080067static u32 port_peerport(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010068{
69 return msg_destport(&p_ptr->publ.phdr);
70}
71
Sam Ravnborg05790c62006-03-20 22:37:04 -080072static u32 port_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010073{
74 return msg_transp_seqno(&p_ptr->publ.phdr);
75}
76
Sam Ravnborg05790c62006-03-20 22:37:04 -080077static void port_incr_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010078{
79 struct tipc_msg *m = &p_ptr->publ.phdr;
80
81 if (likely(!msg_routed(m)))
82 return;
83 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
84}
85
86/**
87 * tipc_multicast - send a multicast message to local and remote destinations
88 */
89
Allan Stephens38f232e2010-11-30 12:00:59 +000090int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
Per Lidenb97bf3f2006-01-02 19:04:38 +010091 u32 num_sect, struct iovec const *msg_sect)
92{
93 struct tipc_msg *hdr;
94 struct sk_buff *buf;
95 struct sk_buff *ibuf = NULL;
96 struct port_list dports = {0, NULL, };
Per Liden4323add2006-01-18 00:38:21 +010097 struct port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 int ext_targets;
99 int res;
100
101 if (unlikely(!oport))
102 return -EINVAL;
103
104 /* Create multicast message */
105
106 hdr = &oport->publ.phdr;
107 msg_set_type(hdr, TIPC_MCAST_MSG);
108 msg_set_nametype(hdr, seq->type);
109 msg_set_namelower(hdr, seq->lower);
110 msg_set_nameupper(hdr, seq->upper);
111 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000112 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113 !oport->user_port, &buf);
114 if (unlikely(!buf))
115 return res;
116
117 /* Figure out where to send multicast message */
118
Per Liden4323add2006-01-18 00:38:21 +0100119 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
120 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900121
122 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123
124 if (ext_targets) {
125 if (dports.count != 0) {
126 ibuf = skb_copy(buf, GFP_ATOMIC);
127 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100128 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 buf_discard(buf);
130 return -ENOMEM;
131 }
132 }
Per Liden4323add2006-01-18 00:38:21 +0100133 res = tipc_bclink_send_msg(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000134 if ((res < 0) && (dports.count != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 buf_discard(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 } else {
137 ibuf = buf;
138 }
139
140 if (res >= 0) {
141 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100142 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 } else {
Per Liden4323add2006-01-18 00:38:21 +0100144 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 }
146 return res;
147}
148
149/**
Per Liden4323add2006-01-18 00:38:21 +0100150 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900151 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 * If there is no port list, perform a lookup to create one
153 */
154
Per Liden4323add2006-01-18 00:38:21 +0100155void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156{
Allan Stephens0e659672010-12-31 18:59:32 +0000157 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 struct port_list dports = {0, NULL, };
159 struct port_list *item = dp;
160 int cnt = 0;
161
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 msg = buf_msg(buf);
163
164 /* Create destination port list, if one wasn't supplied */
165
166 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100167 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 msg_namelower(msg),
169 msg_nameupper(msg),
170 TIPC_CLUSTER_SCOPE,
171 &dports);
172 item = dp = &dports;
173 }
174
175 /* Deliver a copy of message to each destination port */
176
177 if (dp->count != 0) {
178 if (dp->count == 1) {
179 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100180 tipc_port_recv_msg(buf);
181 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 return;
183 }
184 for (; cnt < dp->count; cnt++) {
185 int index = cnt % PLSIZE;
186 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
187
188 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700189 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 goto exit;
191 }
Allan Stephensa0168922010-12-31 18:59:35 +0000192 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000194 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100195 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 }
197 }
198exit:
199 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100200 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201}
202
203/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700204 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900205 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700206 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 */
208
Allan Stephens0ea52242008-07-14 22:42:19 -0700209struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
211 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700212 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213{
214 struct port *p_ptr;
215 struct tipc_msg *msg;
216 u32 ref;
217
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700218 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700219 if (!p_ptr) {
220 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700221 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 }
Per Liden4323add2006-01-18 00:38:21 +0100223 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700225 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700227 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 }
229
Allan Stephens05646c92007-06-10 17:25:24 -0700230 p_ptr->publ.usr_handle = usr_handle;
231 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 p_ptr->publ.ref = ref;
233 msg = &p_ptr->publ.phdr;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000234 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 p_ptr->last_in_seqno = 41;
237 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 INIT_LIST_HEAD(&p_ptr->wait_list);
239 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 p_ptr->dispatcher = dispatcher;
241 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800242 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100244 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 INIT_LIST_HEAD(&p_ptr->publications);
246 INIT_LIST_HEAD(&p_ptr->port_list);
247 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100248 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens0ea52242008-07-14 22:42:19 -0700249 return &(p_ptr->publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250}
251
252int tipc_deleteport(u32 ref)
253{
254 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800255 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800257 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100258 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900259 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 return -EINVAL;
261
Per Liden4323add2006-01-18 00:38:21 +0100262 tipc_ref_discard(ref);
263 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264
265 k_cancel_timer(&p_ptr->timer);
266 if (p_ptr->publ.connected) {
267 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100268 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269 }
Allan Stephense83504f2010-12-31 18:59:30 +0000270 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271
Per Liden4323add2006-01-18 00:38:21 +0100272 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 list_del(&p_ptr->port_list);
274 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100275 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 k_term_timer(&p_ptr->timer);
277 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100278 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700279 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280}
281
Sam Ravnborg05790c62006-03-20 22:37:04 -0800282static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283{
284 return msg_src_droppable(&p_ptr->publ.phdr);
285}
286
287int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
288{
289 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900290
Per Liden4323add2006-01-18 00:38:21 +0100291 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 if (!p_ptr)
293 return -EINVAL;
294 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800295 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700296 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297}
298
299int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
300{
301 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900302
Per Liden4323add2006-01-18 00:38:21 +0100303 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 if (!p_ptr)
305 return -EINVAL;
306 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100307 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700308 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309}
310
Sam Ravnborg05790c62006-03-20 22:37:04 -0800311static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312{
313 return msg_dest_droppable(&p_ptr->publ.phdr);
314}
315
316int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
317{
318 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900319
Per Liden4323add2006-01-18 00:38:21 +0100320 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 if (!p_ptr)
322 return -EINVAL;
323 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800324 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700325 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326}
327
328int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
329{
330 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900331
Per Liden4323add2006-01-18 00:38:21 +0100332 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 if (!p_ptr)
334 return -EINVAL;
335 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100336 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700337 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338}
339
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900340/*
341 * port_build_proto_msg(): build a port level protocol
342 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 * tipc_port lock on.
344 */
345static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
346 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900347 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 u32 seqno, u32 ack)
349{
350 struct sk_buff *buf;
351 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900352
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000353 buf = tipc_buf_acquire(LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 if (buf) {
355 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000356 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700357 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 msg_set_destport(msg, destport);
359 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 msg_set_orignode(msg, orignode);
361 msg_set_transp_seqno(msg, seqno);
362 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 }
364 return buf;
365}
366
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367int tipc_reject_msg(struct sk_buff *buf, u32 err)
368{
369 struct tipc_msg *msg = buf_msg(buf);
370 struct sk_buff *rbuf;
371 struct tipc_msg *rmsg;
372 int hdr_sz;
373 u32 imp = msg_importance(msg);
374 u32 data_sz = msg_data_sz(msg);
375
376 if (data_sz > MAX_REJECT_SIZE)
377 data_sz = MAX_REJECT_SIZE;
378 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
379 imp++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380
381 /* discard rejected message if it shouldn't be returned to sender */
382 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
383 buf_discard(buf);
384 return data_sz;
385 }
386
387 /* construct rejected message */
388 if (msg_mcast(msg))
389 hdr_sz = MCAST_H_SIZE;
390 else
391 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000392 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 if (rbuf == NULL) {
394 buf_discard(buf);
395 return data_sz;
396 }
397 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000398 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700399 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700402 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700404 /* leave name type & instance as zeroes */
405 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700407 msg_set_nametype(rmsg, msg_nametype(msg));
408 msg_set_nameinst(rmsg, msg_nameinst(msg));
409 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900410 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300411 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412
413 /* send self-abort message when rejecting on a connected port */
414 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800415 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100416 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417
418 if (p_ptr) {
419 if (p_ptr->publ.connected)
420 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100421 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 }
Per Liden4323add2006-01-18 00:38:21 +0100423 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 }
425
426 /* send rejected message */
427 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100428 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 return data_sz;
430}
431
Per Liden4323add2006-01-18 00:38:21 +0100432int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
433 struct iovec const *msg_sect, u32 num_sect,
434 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435{
436 struct sk_buff *buf;
437 int res;
438
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000439 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 !p_ptr->user_port, &buf);
441 if (!buf)
442 return res;
443
444 return tipc_reject_msg(buf, err);
445}
446
447static void port_timeout(unsigned long ref)
448{
Per Liden4323add2006-01-18 00:38:21 +0100449 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800450 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451
Allan Stephens065fd172006-10-16 21:38:05 -0700452 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 return;
454
Allan Stephens065fd172006-10-16 21:38:05 -0700455 if (!p_ptr->publ.connected) {
456 tipc_port_unlock(p_ptr);
457 return;
458 }
459
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 /* Last probe answered ? */
461 if (p_ptr->probing_state == PROBING) {
462 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
463 } else {
464 buf = port_build_proto_msg(port_peerport(p_ptr),
465 port_peernode(p_ptr),
466 p_ptr->publ.ref,
467 tipc_own_addr,
468 CONN_MANAGER,
469 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900470 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 port_out_seqno(p_ptr),
472 0);
473 port_incr_out_seqno(p_ptr);
474 p_ptr->probing_state = PROBING;
475 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
476 }
Per Liden4323add2006-01-18 00:38:21 +0100477 tipc_port_unlock(p_ptr);
478 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479}
480
481
482static void port_handle_node_down(unsigned long ref)
483{
Per Liden4323add2006-01-18 00:38:21 +0100484 struct port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000485 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486
487 if (!p_ptr)
488 return;
489 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100490 tipc_port_unlock(p_ptr);
491 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492}
493
494
495static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
496{
497 u32 imp = msg_importance(&p_ptr->publ.phdr);
498
499 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800500 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 if (imp < TIPC_CRITICAL_IMPORTANCE)
502 imp++;
503 return port_build_proto_msg(p_ptr->publ.ref,
504 tipc_own_addr,
505 port_peerport(p_ptr),
506 port_peernode(p_ptr),
507 imp,
508 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 p_ptr->last_in_seqno + 1,
511 0);
512}
513
514
515static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
516{
517 u32 imp = msg_importance(&p_ptr->publ.phdr);
518
519 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800520 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 if (imp < TIPC_CRITICAL_IMPORTANCE)
522 imp++;
523 return port_build_proto_msg(port_peerport(p_ptr),
524 port_peernode(p_ptr),
525 p_ptr->publ.ref,
526 tipc_own_addr,
527 imp,
528 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900529 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 port_out_seqno(p_ptr),
531 0);
532}
533
Per Liden4323add2006-01-18 00:38:21 +0100534void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535{
536 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100537 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800539 struct sk_buff *r_buf = NULL;
540 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 if (!p_ptr) {
543 err = TIPC_ERR_NO_PORT;
544 } else if (p_ptr->publ.connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000545 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
546 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000548 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 p_ptr->publ.congested &&
551 p_ptr->wakeup;
552 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100553 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 goto exit;
555 p_ptr->publ.congested = 0;
556 if (!wakeup)
557 goto exit;
558 p_ptr->wakeup(&p_ptr->publ);
559 goto exit;
560 }
561 } else if (p_ptr->publ.published) {
562 err = TIPC_ERR_NO_PORT;
563 }
564 if (err) {
565 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566 msg_orignode(msg),
567 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800569 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 TIPC_CONN_MSG,
571 err,
572 0,
573 0);
574 goto exit;
575 }
576
577 /* All is fine */
578 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 r_buf = port_build_proto_msg(msg_origport(msg),
580 msg_orignode(msg),
581 msg_destport(msg),
582 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583 CONN_MANAGER,
584 CONN_PROBE_REPLY,
585 TIPC_OK,
586 port_out_seqno(p_ptr),
587 0);
588 }
589 p_ptr->probing_state = CONFIRMED;
590 port_incr_out_seqno(p_ptr);
591exit:
592 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100593 tipc_port_unlock(p_ptr);
594 tipc_net_route_msg(r_buf);
595 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 buf_discard(buf);
597}
598
599static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
600{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900601 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602
603 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 else
608 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
609
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 if (p_ptr->publ.connected) {
611 u32 dport = port_peerport(p_ptr);
612 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900614 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
615 tipc_zone(destnode), tipc_cluster(destnode),
616 tipc_node(destnode), dport);
617 if (p_ptr->publ.conn_type != 0)
618 tipc_printf(buf, " via {%u,%u}",
619 p_ptr->publ.conn_type,
620 p_ptr->publ.conn_instance);
Allan Stephens0e659672010-12-31 18:59:32 +0000621 } else if (p_ptr->publ.published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900622 tipc_printf(buf, " bound to");
623 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 if (publ->lower == publ->upper)
625 tipc_printf(buf, " {%u,%u}", publ->type,
626 publ->lower);
627 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900628 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630 }
631 }
632 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633}
634
635#define MAX_PORT_QUERY 32768
636
Per Liden4323add2006-01-18 00:38:21 +0100637struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638{
639 struct sk_buff *buf;
640 struct tlv_desc *rep_tlv;
641 struct print_buf pb;
642 struct port *p_ptr;
643 int str_len;
644
Per Liden4323add2006-01-18 00:38:21 +0100645 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 if (!buf)
647 return NULL;
648 rep_tlv = (struct tlv_desc *)buf->data;
649
Per Liden4323add2006-01-18 00:38:21 +0100650 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
651 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 list_for_each_entry(p_ptr, &ports, port_list) {
653 spin_lock_bh(p_ptr->publ.lock);
654 port_print(p_ptr, &pb, 0);
655 spin_unlock_bh(p_ptr->publ.lock);
656 }
Per Liden4323add2006-01-18 00:38:21 +0100657 spin_unlock_bh(&tipc_port_list_lock);
658 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659
660 skb_put(buf, TLV_SPACE(str_len));
661 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
662
663 return buf;
664}
665
Per Liden4323add2006-01-18 00:38:21 +0100666void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667{
668 struct port *p_ptr;
669 struct tipc_msg *msg;
670
Per Liden4323add2006-01-18 00:38:21 +0100671 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 list_for_each_entry(p_ptr, &ports, port_list) {
673 msg = &p_ptr->publ.phdr;
674 if (msg_orignode(msg) == tipc_own_addr)
675 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700676 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677 msg_set_orignode(msg, tipc_own_addr);
678 }
Per Liden4323add2006-01-18 00:38:21 +0100679 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680}
681
682
683/*
684 * port_dispatcher_sigh(): Signal handler for messages destinated
685 * to the tipc_port interface.
686 */
687
688static void port_dispatcher_sigh(void *dummy)
689{
690 struct sk_buff *buf;
691
692 spin_lock_bh(&queue_lock);
693 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800694 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695 spin_unlock_bh(&queue_lock);
696
697 while (buf) {
698 struct port *p_ptr;
699 struct user_port *up_ptr;
700 struct tipc_portid orig;
701 struct tipc_name_seq dseq;
702 void *usr_handle;
703 int connected;
704 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700705 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706
707 struct sk_buff *next = buf->next;
708 struct tipc_msg *msg = buf_msg(buf);
709 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900710
Allan Stephens96882432006-06-25 23:38:58 -0700711 message_type = msg_type(msg);
712 if (message_type > TIPC_DIRECT_MSG)
713 goto reject; /* Unsupported message type */
714
Per Liden4323add2006-01-18 00:38:21 +0100715 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700716 if (!p_ptr)
717 goto reject; /* Port deleted while msg in queue */
718
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 orig.ref = msg_origport(msg);
720 orig.node = msg_orignode(msg);
721 up_ptr = p_ptr->user_port;
722 usr_handle = up_ptr->usr_handle;
723 connected = p_ptr->publ.connected;
724 published = p_ptr->publ.published;
725
726 if (unlikely(msg_errcode(msg)))
727 goto err;
728
Allan Stephens96882432006-06-25 23:38:58 -0700729 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 case TIPC_CONN_MSG:{
732 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
733 u32 peer_port = port_peerport(p_ptr);
734 u32 peer_node = port_peernode(p_ptr);
735
Julia Lawall4cec72c2008-01-08 23:48:20 -0800736 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700737 if (unlikely(!cb))
738 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700740 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700742 } else if ((msg_origport(msg) != peer_port) ||
743 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900745 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900747 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 p_ptr->publ.conn_unacked);
749 skb_pull(buf, msg_hdr_sz(msg));
750 cb(usr_handle, dref, &buf, msg_data(msg),
751 msg_data_sz(msg));
752 break;
753 }
754 case TIPC_DIRECT_MSG:{
755 tipc_msg_event cb = up_ptr->msg_cb;
756
Julia Lawall4cec72c2008-01-08 23:48:20 -0800757 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700758 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 goto reject;
760 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 msg_data_sz(msg), msg_importance(msg),
763 &orig);
764 break;
765 }
Allan Stephens96882432006-06-25 23:38:58 -0700766 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 case TIPC_NAMED_MSG:{
768 tipc_named_msg_event cb = up_ptr->named_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 || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 goto reject;
773 dseq.type = msg_nametype(msg);
774 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700775 dseq.upper = (message_type == TIPC_NAMED_MSG)
776 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900778 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 msg_data_sz(msg), msg_importance(msg),
780 &orig, &dseq);
781 break;
782 }
783 }
784 if (buf)
785 buf_discard(buf);
786 buf = next;
787 continue;
788err:
Allan Stephens96882432006-06-25 23:38:58 -0700789 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900790
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900792 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 up_ptr->conn_err_cb;
794 u32 peer_port = port_peerport(p_ptr);
795 u32 peer_node = port_peernode(p_ptr);
796
Julia Lawall4cec72c2008-01-08 23:48:20 -0800797 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700798 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700800 if ((msg_origport(msg) != peer_port) ||
801 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 break;
803 tipc_disconnect(dref);
804 skb_pull(buf, msg_hdr_sz(msg));
805 cb(usr_handle, dref, &buf, msg_data(msg),
806 msg_data_sz(msg), msg_errcode(msg));
807 break;
808 }
809 case TIPC_DIRECT_MSG:{
810 tipc_msg_err_event cb = up_ptr->err_cb;
811
Julia Lawall4cec72c2008-01-08 23:48:20 -0800812 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700813 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 break;
815 skb_pull(buf, msg_hdr_sz(msg));
816 cb(usr_handle, dref, &buf, msg_data(msg),
817 msg_data_sz(msg), msg_errcode(msg), &orig);
818 break;
819 }
Allan Stephens96882432006-06-25 23:38:58 -0700820 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900822 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 up_ptr->named_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 dseq.type = msg_nametype(msg);
829 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700830 dseq.upper = (message_type == TIPC_NAMED_MSG)
831 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 msg_data_sz(msg), msg_errcode(msg), &dseq);
835 break;
836 }
837 }
838 if (buf)
839 buf_discard(buf);
840 buf = next;
841 continue;
842reject:
843 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
844 buf = next;
845 }
846}
847
848/*
849 * port_dispatcher(): Dispatcher for messages destinated
850 * to the tipc_port interface. Called with port locked.
851 */
852
853static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
854{
855 buf->next = NULL;
856 spin_lock_bh(&queue_lock);
857 if (msg_queue_head) {
858 msg_queue_tail->next = buf;
859 msg_queue_tail = buf;
860 } else {
861 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100862 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 }
864 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700865 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866}
867
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900868/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900870 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 */
872
873static void port_wakeup_sh(unsigned long ref)
874{
875 struct port *p_ptr;
876 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800877 tipc_continue_event cb = NULL;
878 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879
Per Liden4323add2006-01-18 00:38:21 +0100880 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 if (p_ptr) {
882 up_ptr = p_ptr->user_port;
883 if (up_ptr) {
884 cb = up_ptr->continue_event_cb;
885 uh = up_ptr->usr_handle;
886 }
Per Liden4323add2006-01-18 00:38:21 +0100887 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 }
889 if (cb)
890 cb(uh, ref);
891}
892
893
894static void port_wakeup(struct tipc_port *p_ptr)
895{
Per Liden4323add2006-01-18 00:38:21 +0100896 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897}
898
899void tipc_acknowledge(u32 ref, u32 ack)
900{
901 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800902 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903
Per Liden4323add2006-01-18 00:38:21 +0100904 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 if (!p_ptr)
906 return;
907 if (p_ptr->publ.connected) {
908 p_ptr->publ.conn_unacked -= ack;
909 buf = port_build_proto_msg(port_peerport(p_ptr),
910 port_peernode(p_ptr),
911 ref,
912 tipc_own_addr,
913 CONN_MANAGER,
914 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900915 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 port_out_seqno(p_ptr),
917 ack);
918 }
Per Liden4323add2006-01-18 00:38:21 +0100919 tipc_port_unlock(p_ptr);
920 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921}
922
923/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000924 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 */
926
Allan Stephensb0c1e922010-12-31 18:59:22 +0000927int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900928 unsigned int importance,
929 tipc_msg_err_event error_cb,
930 tipc_named_msg_err_event named_error_cb,
931 tipc_conn_shutdown_event conn_error_cb,
932 tipc_msg_event msg_cb,
933 tipc_named_msg_event named_msg_cb,
934 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 tipc_continue_event continue_event_cb,/* May be zero */
936 u32 *portref)
937{
938 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900939 struct port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700941 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700942 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700943 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944 return -ENOMEM;
945 }
Allan Stephens0ea52242008-07-14 22:42:19 -0700946 p_ptr = (struct port *)tipc_createport_raw(NULL, port_dispatcher,
947 port_wakeup, importance);
948 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949 kfree(up_ptr);
950 return -ENOMEM;
951 }
952
953 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 up_ptr->usr_handle = usr_handle;
955 up_ptr->ref = p_ptr->publ.ref;
956 up_ptr->err_cb = error_cb;
957 up_ptr->named_err_cb = named_error_cb;
958 up_ptr->conn_err_cb = conn_error_cb;
959 up_ptr->msg_cb = msg_cb;
960 up_ptr->named_msg_cb = named_msg_cb;
961 up_ptr->conn_msg_cb = conn_msg_cb;
962 up_ptr->continue_event_cb = continue_event_cb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 *portref = p_ptr->publ.ref;
Per Liden4323add2006-01-18 00:38:21 +0100964 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700965 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966}
967
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968int tipc_portimportance(u32 ref, unsigned int *importance)
969{
970 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900971
Per Liden4323add2006-01-18 00:38:21 +0100972 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 if (!p_ptr)
974 return -EINVAL;
975 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800976 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700977 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978}
979
980int tipc_set_portimportance(u32 ref, unsigned int imp)
981{
982 struct port *p_ptr;
983
984 if (imp > TIPC_CRITICAL_IMPORTANCE)
985 return -EINVAL;
986
Per Liden4323add2006-01-18 00:38:21 +0100987 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 if (!p_ptr)
989 return -EINVAL;
990 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800991 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700992 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993}
994
995
996int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
997{
998 struct port *p_ptr;
999 struct publication *publ;
1000 u32 key;
1001 int res = -EINVAL;
1002
Per Liden4323add2006-01-18 00:38:21 +01001003 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001004 if (!p_ptr)
1005 return -EINVAL;
1006
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 if (p_ptr->publ.connected)
1008 goto exit;
1009 if (seq->lower > seq->upper)
1010 goto exit;
1011 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1012 goto exit;
1013 key = ref + p_ptr->pub_count + 1;
1014 if (key == ref) {
1015 res = -EADDRINUSE;
1016 goto exit;
1017 }
Per Liden4323add2006-01-18 00:38:21 +01001018 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1019 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 if (publ) {
1021 list_add(&publ->pport_list, &p_ptr->publications);
1022 p_ptr->pub_count++;
1023 p_ptr->publ.published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001024 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 }
1026exit:
Per Liden4323add2006-01-18 00:38:21 +01001027 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 return res;
1029}
1030
1031int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1032{
1033 struct port *p_ptr;
1034 struct publication *publ;
1035 struct publication *tpubl;
1036 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001037
Per Liden4323add2006-01-18 00:38:21 +01001038 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 if (!p_ptr)
1040 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001042 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001044 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001045 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001047 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001049 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 &p_ptr->publications, pport_list) {
1051 if (publ->scope != scope)
1052 continue;
1053 if (publ->type != seq->type)
1054 continue;
1055 if (publ->lower != seq->lower)
1056 continue;
1057 if (publ->upper != seq->upper)
1058 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001059 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001060 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001061 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 break;
1063 }
1064 }
1065 if (list_empty(&p_ptr->publications))
1066 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001067 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 return res;
1069}
1070
1071int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1072{
1073 struct port *p_ptr;
1074 struct tipc_msg *msg;
1075 int res = -EINVAL;
1076
Per Liden4323add2006-01-18 00:38:21 +01001077 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 if (!p_ptr)
1079 return -EINVAL;
1080 if (p_ptr->publ.published || p_ptr->publ.connected)
1081 goto exit;
1082 if (!peer->ref)
1083 goto exit;
1084
1085 msg = &p_ptr->publ.phdr;
1086 msg_set_destnode(msg, peer->node);
1087 msg_set_destport(msg, peer->ref);
1088 msg_set_orignode(msg, tipc_own_addr);
1089 msg_set_origport(msg, p_ptr->publ.ref);
1090 msg_set_transp_seqno(msg, 42);
1091 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens08c80e92010-12-31 18:59:17 +00001092 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093
1094 p_ptr->probing_interval = PROBING_INTERVAL;
1095 p_ptr->probing_state = CONFIRMED;
1096 p_ptr->publ.connected = 1;
1097 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1098
Allan Stephens0e659672010-12-31 18:59:32 +00001099 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001100 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001102 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103exit:
Per Liden4323add2006-01-18 00:38:21 +01001104 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001105 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 return res;
1107}
1108
Allan Stephens0c3141e2008-04-15 00:22:02 -07001109/**
1110 * tipc_disconnect_port - disconnect port from peer
1111 *
1112 * Port must be locked.
1113 */
1114
1115int tipc_disconnect_port(struct tipc_port *tp_ptr)
1116{
1117 int res;
1118
1119 if (tp_ptr->connected) {
1120 tp_ptr->connected = 0;
1121 /* let timer expire on it's own to avoid deadlock! */
1122 tipc_nodesub_unsubscribe(
1123 &((struct port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001124 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001125 } else {
1126 res = -ENOTCONN;
1127 }
1128 return res;
1129}
1130
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131/*
1132 * tipc_disconnect(): Disconnect port form peer.
1133 * This is a node local operation.
1134 */
1135
1136int tipc_disconnect(u32 ref)
1137{
1138 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001139 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140
Per Liden4323add2006-01-18 00:38:21 +01001141 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 if (!p_ptr)
1143 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001144 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001145 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 return res;
1147}
1148
1149/*
1150 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1151 */
1152int tipc_shutdown(u32 ref)
1153{
1154 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001155 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156
Per Liden4323add2006-01-18 00:38:21 +01001157 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 if (!p_ptr)
1159 return -EINVAL;
1160
1161 if (p_ptr->publ.connected) {
1162 u32 imp = msg_importance(&p_ptr->publ.phdr);
1163 if (imp < TIPC_CRITICAL_IMPORTANCE)
1164 imp++;
1165 buf = port_build_proto_msg(port_peerport(p_ptr),
1166 port_peernode(p_ptr),
1167 ref,
1168 tipc_own_addr,
1169 imp,
1170 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001171 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 port_out_seqno(p_ptr),
1173 0);
1174 }
Per Liden4323add2006-01-18 00:38:21 +01001175 tipc_port_unlock(p_ptr);
1176 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 return tipc_disconnect(ref);
1178}
1179
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180/*
Per Liden4323add2006-01-18 00:38:21 +01001181 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 * message for this node.
1183 */
1184
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001185static int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1186 struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187{
1188 struct sk_buff *buf;
1189 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001190
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001191 res = tipc_msg_build(&sender->publ.phdr, msg_sect, num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 MAX_MSG_SIZE, !sender->user_port, &buf);
1193 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001194 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 return res;
1196}
1197
1198/**
1199 * tipc_send - send message sections on connection
1200 */
1201
1202int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1203{
1204 struct port *p_ptr;
1205 u32 destnode;
1206 int res;
1207
Per Liden4323add2006-01-18 00:38:21 +01001208 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 if (!p_ptr || !p_ptr->publ.connected)
1210 return -EINVAL;
1211
1212 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001213 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 destnode = port_peernode(p_ptr);
1215 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001216 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1217 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 else
Per Liden4323add2006-01-18 00:38:21 +01001219 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220
1221 if (likely(res != -ELINKCONG)) {
1222 port_incr_out_seqno(p_ptr);
1223 p_ptr->publ.congested = 0;
1224 p_ptr->sent++;
1225 return res;
1226 }
1227 }
1228 if (port_unreliable(p_ptr)) {
1229 p_ptr->publ.congested = 0;
1230 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001231 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 }
1233 return -ELINKCONG;
1234}
1235
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001236/**
Allan Stephens12bae4792010-11-30 12:01:02 +00001237 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 */
1239
Allan Stephens12bae4792010-11-30 12:01:02 +00001240int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1241 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242{
1243 struct port *p_ptr;
1244 struct tipc_msg *msg;
1245 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001246 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 int res;
1248
Per Liden4323add2006-01-18 00:38:21 +01001249 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 if (!p_ptr || p_ptr->publ.connected)
1251 return -EINVAL;
1252
1253 msg = &p_ptr->publ.phdr;
1254 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae4792010-11-30 12:01:02 +00001255 msg_set_orignode(msg, tipc_own_addr);
1256 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 msg_set_hdr_sz(msg, LONG_H_SIZE);
1258 msg_set_nametype(msg, name->type);
1259 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001260 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001261 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 msg_set_destnode(msg, destnode);
1263 msg_set_destport(msg, destport);
1264
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001265 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 p_ptr->sent++;
1267 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001268 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001269 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001270 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 if (likely(res != -ELINKCONG))
1272 return res;
1273 if (port_unreliable(p_ptr)) {
1274 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001275 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 }
1277 return -ELINKCONG;
1278 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001279 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001280 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281}
1282
1283/**
Allan Stephens12bae4792010-11-30 12:01:02 +00001284 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 */
1286
Allan Stephens12bae4792010-11-30 12:01:02 +00001287int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1288 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289{
1290 struct port *p_ptr;
1291 struct tipc_msg *msg;
1292 int res;
1293
Per Liden4323add2006-01-18 00:38:21 +01001294 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 if (!p_ptr || p_ptr->publ.connected)
1296 return -EINVAL;
1297
1298 msg = &p_ptr->publ.phdr;
1299 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae4792010-11-30 12:01:02 +00001300 msg_set_orignode(msg, tipc_own_addr);
1301 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 msg_set_destnode(msg, dest->node);
1303 msg_set_destport(msg, dest->ref);
1304 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 p_ptr->sent++;
1306 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001307 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1308 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 if (likely(res != -ELINKCONG))
1310 return res;
1311 if (port_unreliable(p_ptr)) {
1312 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001313 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 }
1315 return -ELINKCONG;
1316}
1317
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001318/**
Allan Stephens12bae4792010-11-30 12:01:02 +00001319 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 */
1321
Allan Stephens12bae4792010-11-30 12:01:02 +00001322int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1323 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324{
1325 struct port *p_ptr;
1326 struct tipc_msg *msg;
1327 int res;
1328
Per Liden4323add2006-01-18 00:38:21 +01001329 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330 if (!p_ptr || p_ptr->publ.connected)
1331 return -EINVAL;
1332
1333 msg = &p_ptr->publ.phdr;
1334 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae4792010-11-30 12:01:02 +00001335 msg_set_orignode(msg, tipc_own_addr);
1336 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 msg_set_destnode(msg, dest->node);
1338 msg_set_destport(msg, dest->ref);
1339 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1341 if (skb_cow(buf, DIR_MSG_H_SIZE))
1342 return -ENOMEM;
1343
1344 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001345 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 p_ptr->sent++;
1347 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001348 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 res = tipc_send_buf_fast(buf, dest->node);
1350 if (likely(res != -ELINKCONG))
1351 return res;
1352 if (port_unreliable(p_ptr))
1353 return dsz;
1354 return -ELINKCONG;
1355}
1356