blob: 78fcb75bb082247592f3886cb50de0e9518e5677 [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
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080049static struct sk_buff *msg_queue_head = NULL;
50static struct sk_buff *msg_queue_tail = NULL;
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);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 if ((res < 0) && (dports.count != 0)) {
135 buf_discard(ibuf);
136 }
137 } else {
138 ibuf = buf;
139 }
140
141 if (res >= 0) {
142 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100143 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 } else {
Per Liden4323add2006-01-18 00:38:21 +0100145 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 }
147 return res;
148}
149
150/**
Per Liden4323add2006-01-18 00:38:21 +0100151 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 * If there is no port list, perform a lookup to create one
154 */
155
Per Liden4323add2006-01-18 00:38:21 +0100156void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157{
Allan Stephens0e659672010-12-31 18:59:32 +0000158 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 struct port_list dports = {0, NULL, };
160 struct port_list *item = dp;
161 int cnt = 0;
162
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 msg = buf_msg(buf);
164
165 /* Create destination port list, if one wasn't supplied */
166
167 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100168 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 msg_namelower(msg),
170 msg_nameupper(msg),
171 TIPC_CLUSTER_SCOPE,
172 &dports);
173 item = dp = &dports;
174 }
175
176 /* Deliver a copy of message to each destination port */
177
178 if (dp->count != 0) {
179 if (dp->count == 1) {
180 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100181 tipc_port_recv_msg(buf);
182 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 return;
184 }
185 for (; cnt < dp->count; cnt++) {
186 int index = cnt % PLSIZE;
187 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
188
189 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700190 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 goto exit;
192 }
193 if ((index == 0) && (cnt != 0)) {
194 item = item->next;
195 }
Allan Stephens0e659672010-12-31 18:59:32 +0000196 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100197 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 }
199 }
200exit:
201 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100202 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203}
204
205/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700206 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900207 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700208 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 */
210
Allan Stephens0ea52242008-07-14 22:42:19 -0700211struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
213 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700214 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215{
216 struct port *p_ptr;
217 struct tipc_msg *msg;
218 u32 ref;
219
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700220 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700221 if (!p_ptr) {
222 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700223 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 }
Per Liden4323add2006-01-18 00:38:21 +0100225 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700227 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700229 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 }
231
Allan Stephens05646c92007-06-10 17:25:24 -0700232 p_ptr->publ.usr_handle = usr_handle;
233 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 p_ptr->publ.ref = ref;
235 msg = &p_ptr->publ.phdr;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000236 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 p_ptr->last_in_seqno = 41;
239 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 INIT_LIST_HEAD(&p_ptr->wait_list);
241 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 p_ptr->dispatcher = dispatcher;
243 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800244 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100246 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 INIT_LIST_HEAD(&p_ptr->publications);
248 INIT_LIST_HEAD(&p_ptr->port_list);
249 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100250 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens0ea52242008-07-14 22:42:19 -0700251 return &(p_ptr->publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252}
253
254int tipc_deleteport(u32 ref)
255{
256 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800257 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800259 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100260 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900261 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 return -EINVAL;
263
Per Liden4323add2006-01-18 00:38:21 +0100264 tipc_ref_discard(ref);
265 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266
267 k_cancel_timer(&p_ptr->timer);
268 if (p_ptr->publ.connected) {
269 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100270 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 }
Allan Stephense83504f2010-12-31 18:59:30 +0000272 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273
Per Liden4323add2006-01-18 00:38:21 +0100274 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 list_del(&p_ptr->port_list);
276 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100277 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 k_term_timer(&p_ptr->timer);
279 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100280 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700281 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282}
283
Sam Ravnborg05790c62006-03-20 22:37:04 -0800284static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285{
286 return msg_src_droppable(&p_ptr->publ.phdr);
287}
288
289int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
290{
291 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900292
Per Liden4323add2006-01-18 00:38:21 +0100293 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 if (!p_ptr)
295 return -EINVAL;
296 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800297 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700298 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299}
300
301int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
302{
303 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900304
Per Liden4323add2006-01-18 00:38:21 +0100305 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 if (!p_ptr)
307 return -EINVAL;
308 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100309 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700310 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311}
312
Sam Ravnborg05790c62006-03-20 22:37:04 -0800313static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314{
315 return msg_dest_droppable(&p_ptr->publ.phdr);
316}
317
318int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
319{
320 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900321
Per Liden4323add2006-01-18 00:38:21 +0100322 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 if (!p_ptr)
324 return -EINVAL;
325 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800326 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700327 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328}
329
330int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
331{
332 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900333
Per Liden4323add2006-01-18 00:38:21 +0100334 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 if (!p_ptr)
336 return -EINVAL;
337 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100338 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700339 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340}
341
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900342/*
343 * port_build_proto_msg(): build a port level protocol
344 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 * tipc_port lock on.
346 */
347static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
348 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900349 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 u32 seqno, u32 ack)
351{
352 struct sk_buff *buf;
353 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900354
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000355 buf = tipc_buf_acquire(LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 if (buf) {
357 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000358 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700359 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 msg_set_destport(msg, destport);
361 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 msg_set_orignode(msg, orignode);
363 msg_set_transp_seqno(msg, seqno);
364 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 }
366 return buf;
367}
368
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369int tipc_reject_msg(struct sk_buff *buf, u32 err)
370{
371 struct tipc_msg *msg = buf_msg(buf);
372 struct sk_buff *rbuf;
373 struct tipc_msg *rmsg;
374 int hdr_sz;
375 u32 imp = msg_importance(msg);
376 u32 data_sz = msg_data_sz(msg);
377
378 if (data_sz > MAX_REJECT_SIZE)
379 data_sz = MAX_REJECT_SIZE;
380 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
381 imp++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382
383 /* discard rejected message if it shouldn't be returned to sender */
384 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
385 buf_discard(buf);
386 return data_sz;
387 }
388
389 /* construct rejected message */
390 if (msg_mcast(msg))
391 hdr_sz = MCAST_H_SIZE;
392 else
393 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000394 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 if (rbuf == NULL) {
396 buf_discard(buf);
397 return data_sz;
398 }
399 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000400 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700401 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700404 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700406 /* leave name type & instance as zeroes */
407 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700409 msg_set_nametype(rmsg, msg_nametype(msg));
410 msg_set_nameinst(rmsg, msg_nameinst(msg));
411 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900412 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300413 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414
415 /* send self-abort message when rejecting on a connected port */
416 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800417 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100418 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419
420 if (p_ptr) {
421 if (p_ptr->publ.connected)
422 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100423 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 }
Per Liden4323add2006-01-18 00:38:21 +0100425 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 }
427
428 /* send rejected message */
429 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100430 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 return data_sz;
432}
433
Per Liden4323add2006-01-18 00:38:21 +0100434int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
435 struct iovec const *msg_sect, u32 num_sect,
436 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437{
438 struct sk_buff *buf;
439 int res;
440
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000441 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 !p_ptr->user_port, &buf);
443 if (!buf)
444 return res;
445
446 return tipc_reject_msg(buf, err);
447}
448
449static void port_timeout(unsigned long ref)
450{
Per Liden4323add2006-01-18 00:38:21 +0100451 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800452 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453
Allan Stephens065fd172006-10-16 21:38:05 -0700454 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 return;
456
Allan Stephens065fd172006-10-16 21:38:05 -0700457 if (!p_ptr->publ.connected) {
458 tipc_port_unlock(p_ptr);
459 return;
460 }
461
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 /* Last probe answered ? */
463 if (p_ptr->probing_state == PROBING) {
464 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
465 } else {
466 buf = port_build_proto_msg(port_peerport(p_ptr),
467 port_peernode(p_ptr),
468 p_ptr->publ.ref,
469 tipc_own_addr,
470 CONN_MANAGER,
471 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900472 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 port_out_seqno(p_ptr),
474 0);
475 port_incr_out_seqno(p_ptr);
476 p_ptr->probing_state = PROBING;
477 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
478 }
Per Liden4323add2006-01-18 00:38:21 +0100479 tipc_port_unlock(p_ptr);
480 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481}
482
483
484static void port_handle_node_down(unsigned long ref)
485{
Per Liden4323add2006-01-18 00:38:21 +0100486 struct port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000487 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488
489 if (!p_ptr)
490 return;
491 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100492 tipc_port_unlock(p_ptr);
493 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494}
495
496
497static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
498{
499 u32 imp = msg_importance(&p_ptr->publ.phdr);
500
501 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800502 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 if (imp < TIPC_CRITICAL_IMPORTANCE)
504 imp++;
505 return port_build_proto_msg(p_ptr->publ.ref,
506 tipc_own_addr,
507 port_peerport(p_ptr),
508 port_peernode(p_ptr),
509 imp,
510 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900511 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 p_ptr->last_in_seqno + 1,
513 0);
514}
515
516
517static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
518{
519 u32 imp = msg_importance(&p_ptr->publ.phdr);
520
521 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800522 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 if (imp < TIPC_CRITICAL_IMPORTANCE)
524 imp++;
525 return port_build_proto_msg(port_peerport(p_ptr),
526 port_peernode(p_ptr),
527 p_ptr->publ.ref,
528 tipc_own_addr,
529 imp,
530 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900531 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 port_out_seqno(p_ptr),
533 0);
534}
535
Per Liden4323add2006-01-18 00:38:21 +0100536void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537{
538 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100539 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800541 struct sk_buff *r_buf = NULL;
542 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 if (!p_ptr) {
545 err = TIPC_ERR_NO_PORT;
546 } else if (p_ptr->publ.connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000547 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
548 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000550 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900551 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 p_ptr->publ.congested &&
553 p_ptr->wakeup;
554 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100555 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 goto exit;
557 p_ptr->publ.congested = 0;
558 if (!wakeup)
559 goto exit;
560 p_ptr->wakeup(&p_ptr->publ);
561 goto exit;
562 }
563 } else if (p_ptr->publ.published) {
564 err = TIPC_ERR_NO_PORT;
565 }
566 if (err) {
567 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900568 msg_orignode(msg),
569 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800571 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 TIPC_CONN_MSG,
573 err,
574 0,
575 0);
576 goto exit;
577 }
578
579 /* All is fine */
580 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 r_buf = port_build_proto_msg(msg_origport(msg),
582 msg_orignode(msg),
583 msg_destport(msg),
584 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 CONN_MANAGER,
586 CONN_PROBE_REPLY,
587 TIPC_OK,
588 port_out_seqno(p_ptr),
589 0);
590 }
591 p_ptr->probing_state = CONFIRMED;
592 port_incr_out_seqno(p_ptr);
593exit:
594 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100595 tipc_port_unlock(p_ptr);
596 tipc_net_route_msg(r_buf);
597 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 buf_discard(buf);
599}
600
601static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
602{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604
605 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 else
610 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
611
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 if (p_ptr->publ.connected) {
613 u32 dport = port_peerport(p_ptr);
614 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900616 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
617 tipc_zone(destnode), tipc_cluster(destnode),
618 tipc_node(destnode), dport);
619 if (p_ptr->publ.conn_type != 0)
620 tipc_printf(buf, " via {%u,%u}",
621 p_ptr->publ.conn_type,
622 p_ptr->publ.conn_instance);
Allan Stephens0e659672010-12-31 18:59:32 +0000623 } else if (p_ptr->publ.published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624 tipc_printf(buf, " bound to");
625 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 if (publ->lower == publ->upper)
627 tipc_printf(buf, " {%u,%u}", publ->type,
628 publ->lower);
629 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900632 }
633 }
634 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635}
636
637#define MAX_PORT_QUERY 32768
638
Per Liden4323add2006-01-18 00:38:21 +0100639struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640{
641 struct sk_buff *buf;
642 struct tlv_desc *rep_tlv;
643 struct print_buf pb;
644 struct port *p_ptr;
645 int str_len;
646
Per Liden4323add2006-01-18 00:38:21 +0100647 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 if (!buf)
649 return NULL;
650 rep_tlv = (struct tlv_desc *)buf->data;
651
Per Liden4323add2006-01-18 00:38:21 +0100652 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
653 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 list_for_each_entry(p_ptr, &ports, port_list) {
655 spin_lock_bh(p_ptr->publ.lock);
656 port_print(p_ptr, &pb, 0);
657 spin_unlock_bh(p_ptr->publ.lock);
658 }
Per Liden4323add2006-01-18 00:38:21 +0100659 spin_unlock_bh(&tipc_port_list_lock);
660 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661
662 skb_put(buf, TLV_SPACE(str_len));
663 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
664
665 return buf;
666}
667
Per Liden4323add2006-01-18 00:38:21 +0100668void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669{
670 struct port *p_ptr;
671 struct tipc_msg *msg;
672
Per Liden4323add2006-01-18 00:38:21 +0100673 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 list_for_each_entry(p_ptr, &ports, port_list) {
675 msg = &p_ptr->publ.phdr;
676 if (msg_orignode(msg) == tipc_own_addr)
677 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700678 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 msg_set_orignode(msg, tipc_own_addr);
680 }
Per Liden4323add2006-01-18 00:38:21 +0100681 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682}
683
684
685/*
686 * port_dispatcher_sigh(): Signal handler for messages destinated
687 * to the tipc_port interface.
688 */
689
690static void port_dispatcher_sigh(void *dummy)
691{
692 struct sk_buff *buf;
693
694 spin_lock_bh(&queue_lock);
695 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800696 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697 spin_unlock_bh(&queue_lock);
698
699 while (buf) {
700 struct port *p_ptr;
701 struct user_port *up_ptr;
702 struct tipc_portid orig;
703 struct tipc_name_seq dseq;
704 void *usr_handle;
705 int connected;
706 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700707 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708
709 struct sk_buff *next = buf->next;
710 struct tipc_msg *msg = buf_msg(buf);
711 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900712
Allan Stephens96882432006-06-25 23:38:58 -0700713 message_type = msg_type(msg);
714 if (message_type > TIPC_DIRECT_MSG)
715 goto reject; /* Unsupported message type */
716
Per Liden4323add2006-01-18 00:38:21 +0100717 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700718 if (!p_ptr)
719 goto reject; /* Port deleted while msg in queue */
720
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 orig.ref = msg_origport(msg);
722 orig.node = msg_orignode(msg);
723 up_ptr = p_ptr->user_port;
724 usr_handle = up_ptr->usr_handle;
725 connected = p_ptr->publ.connected;
726 published = p_ptr->publ.published;
727
728 if (unlikely(msg_errcode(msg)))
729 goto err;
730
Allan Stephens96882432006-06-25 23:38:58 -0700731 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900732
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 case TIPC_CONN_MSG:{
734 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
735 u32 peer_port = port_peerport(p_ptr);
736 u32 peer_node = port_peernode(p_ptr);
737
Julia Lawall4cec72c2008-01-08 23:48:20 -0800738 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700739 if (unlikely(!cb))
740 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700742 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700744 } else if ((msg_origport(msg) != peer_port) ||
745 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900747 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900749 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 p_ptr->publ.conn_unacked);
751 skb_pull(buf, msg_hdr_sz(msg));
752 cb(usr_handle, dref, &buf, msg_data(msg),
753 msg_data_sz(msg));
754 break;
755 }
756 case TIPC_DIRECT_MSG:{
757 tipc_msg_event cb = up_ptr->msg_cb;
758
Julia Lawall4cec72c2008-01-08 23:48:20 -0800759 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700760 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 goto reject;
762 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900763 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 msg_data_sz(msg), msg_importance(msg),
765 &orig);
766 break;
767 }
Allan Stephens96882432006-06-25 23:38:58 -0700768 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 case TIPC_NAMED_MSG:{
770 tipc_named_msg_event cb = up_ptr->named_msg_cb;
771
Julia Lawall4cec72c2008-01-08 23:48:20 -0800772 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700773 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 goto reject;
775 dseq.type = msg_nametype(msg);
776 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700777 dseq.upper = (message_type == TIPC_NAMED_MSG)
778 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900780 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 msg_data_sz(msg), msg_importance(msg),
782 &orig, &dseq);
783 break;
784 }
785 }
786 if (buf)
787 buf_discard(buf);
788 buf = next;
789 continue;
790err:
Allan Stephens96882432006-06-25 23:38:58 -0700791 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900792
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900794 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 up_ptr->conn_err_cb;
796 u32 peer_port = port_peerport(p_ptr);
797 u32 peer_node = port_peernode(p_ptr);
798
Julia Lawall4cec72c2008-01-08 23:48:20 -0800799 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700800 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700802 if ((msg_origport(msg) != peer_port) ||
803 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 break;
805 tipc_disconnect(dref);
806 skb_pull(buf, msg_hdr_sz(msg));
807 cb(usr_handle, dref, &buf, msg_data(msg),
808 msg_data_sz(msg), msg_errcode(msg));
809 break;
810 }
811 case TIPC_DIRECT_MSG:{
812 tipc_msg_err_event cb = up_ptr->err_cb;
813
Julia Lawall4cec72c2008-01-08 23:48:20 -0800814 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700815 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 break;
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), &orig);
820 break;
821 }
Allan Stephens96882432006-06-25 23:38:58 -0700822 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900824 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 up_ptr->named_err_cb;
826
Julia Lawall4cec72c2008-01-08 23:48:20 -0800827 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700828 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 break;
830 dseq.type = msg_nametype(msg);
831 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700832 dseq.upper = (message_type == TIPC_NAMED_MSG)
833 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 msg_data_sz(msg), msg_errcode(msg), &dseq);
837 break;
838 }
839 }
840 if (buf)
841 buf_discard(buf);
842 buf = next;
843 continue;
844reject:
845 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
846 buf = next;
847 }
848}
849
850/*
851 * port_dispatcher(): Dispatcher for messages destinated
852 * to the tipc_port interface. Called with port locked.
853 */
854
855static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
856{
857 buf->next = NULL;
858 spin_lock_bh(&queue_lock);
859 if (msg_queue_head) {
860 msg_queue_tail->next = buf;
861 msg_queue_tail = buf;
862 } else {
863 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100864 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 }
866 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700867 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868}
869
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900870/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900872 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 */
874
875static void port_wakeup_sh(unsigned long ref)
876{
877 struct port *p_ptr;
878 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800879 tipc_continue_event cb = NULL;
880 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881
Per Liden4323add2006-01-18 00:38:21 +0100882 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 if (p_ptr) {
884 up_ptr = p_ptr->user_port;
885 if (up_ptr) {
886 cb = up_ptr->continue_event_cb;
887 uh = up_ptr->usr_handle;
888 }
Per Liden4323add2006-01-18 00:38:21 +0100889 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 }
891 if (cb)
892 cb(uh, ref);
893}
894
895
896static void port_wakeup(struct tipc_port *p_ptr)
897{
Per Liden4323add2006-01-18 00:38:21 +0100898 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899}
900
901void tipc_acknowledge(u32 ref, u32 ack)
902{
903 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800904 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905
Per Liden4323add2006-01-18 00:38:21 +0100906 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 if (!p_ptr)
908 return;
909 if (p_ptr->publ.connected) {
910 p_ptr->publ.conn_unacked -= ack;
911 buf = port_build_proto_msg(port_peerport(p_ptr),
912 port_peernode(p_ptr),
913 ref,
914 tipc_own_addr,
915 CONN_MANAGER,
916 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900917 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 port_out_seqno(p_ptr),
919 ack);
920 }
Per Liden4323add2006-01-18 00:38:21 +0100921 tipc_port_unlock(p_ptr);
922 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923}
924
925/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000926 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 */
928
Allan Stephensb0c1e922010-12-31 18:59:22 +0000929int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930 unsigned int importance,
931 tipc_msg_err_event error_cb,
932 tipc_named_msg_err_event named_error_cb,
933 tipc_conn_shutdown_event conn_error_cb,
934 tipc_msg_event msg_cb,
935 tipc_named_msg_event named_msg_cb,
936 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 tipc_continue_event continue_event_cb,/* May be zero */
938 u32 *portref)
939{
940 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900941 struct port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700943 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700944 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700945 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 return -ENOMEM;
947 }
Allan Stephens0ea52242008-07-14 22:42:19 -0700948 p_ptr = (struct port *)tipc_createport_raw(NULL, port_dispatcher,
949 port_wakeup, importance);
950 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 kfree(up_ptr);
952 return -ENOMEM;
953 }
954
955 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 up_ptr->usr_handle = usr_handle;
957 up_ptr->ref = p_ptr->publ.ref;
958 up_ptr->err_cb = error_cb;
959 up_ptr->named_err_cb = named_error_cb;
960 up_ptr->conn_err_cb = conn_error_cb;
961 up_ptr->msg_cb = msg_cb;
962 up_ptr->named_msg_cb = named_msg_cb;
963 up_ptr->conn_msg_cb = conn_msg_cb;
964 up_ptr->continue_event_cb = continue_event_cb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 *portref = p_ptr->publ.ref;
Per Liden4323add2006-01-18 00:38:21 +0100966 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700967 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968}
969
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970int tipc_portimportance(u32 ref, unsigned int *importance)
971{
972 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900973
Per Liden4323add2006-01-18 00:38:21 +0100974 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 if (!p_ptr)
976 return -EINVAL;
977 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800978 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700979 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980}
981
982int tipc_set_portimportance(u32 ref, unsigned int imp)
983{
984 struct port *p_ptr;
985
986 if (imp > TIPC_CRITICAL_IMPORTANCE)
987 return -EINVAL;
988
Per Liden4323add2006-01-18 00:38:21 +0100989 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 if (!p_ptr)
991 return -EINVAL;
992 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800993 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700994 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995}
996
997
998int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
999{
1000 struct port *p_ptr;
1001 struct publication *publ;
1002 u32 key;
1003 int res = -EINVAL;
1004
Per Liden4323add2006-01-18 00:38:21 +01001005 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001006 if (!p_ptr)
1007 return -EINVAL;
1008
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 if (p_ptr->publ.connected)
1010 goto exit;
1011 if (seq->lower > seq->upper)
1012 goto exit;
1013 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1014 goto exit;
1015 key = ref + p_ptr->pub_count + 1;
1016 if (key == ref) {
1017 res = -EADDRINUSE;
1018 goto exit;
1019 }
Per Liden4323add2006-01-18 00:38:21 +01001020 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1021 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 if (publ) {
1023 list_add(&publ->pport_list, &p_ptr->publications);
1024 p_ptr->pub_count++;
1025 p_ptr->publ.published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001026 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 }
1028exit:
Per Liden4323add2006-01-18 00:38:21 +01001029 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 return res;
1031}
1032
1033int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1034{
1035 struct port *p_ptr;
1036 struct publication *publ;
1037 struct publication *tpubl;
1038 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001039
Per Liden4323add2006-01-18 00:38:21 +01001040 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 if (!p_ptr)
1042 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001044 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001046 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001047 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001049 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001051 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 &p_ptr->publications, pport_list) {
1053 if (publ->scope != scope)
1054 continue;
1055 if (publ->type != seq->type)
1056 continue;
1057 if (publ->lower != seq->lower)
1058 continue;
1059 if (publ->upper != seq->upper)
1060 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001061 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001062 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001063 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 break;
1065 }
1066 }
1067 if (list_empty(&p_ptr->publications))
1068 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001069 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 return res;
1071}
1072
1073int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1074{
1075 struct port *p_ptr;
1076 struct tipc_msg *msg;
1077 int res = -EINVAL;
1078
Per Liden4323add2006-01-18 00:38:21 +01001079 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080 if (!p_ptr)
1081 return -EINVAL;
1082 if (p_ptr->publ.published || p_ptr->publ.connected)
1083 goto exit;
1084 if (!peer->ref)
1085 goto exit;
1086
1087 msg = &p_ptr->publ.phdr;
1088 msg_set_destnode(msg, peer->node);
1089 msg_set_destport(msg, peer->ref);
1090 msg_set_orignode(msg, tipc_own_addr);
1091 msg_set_origport(msg, p_ptr->publ.ref);
1092 msg_set_transp_seqno(msg, 42);
1093 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens08c80e92010-12-31 18:59:17 +00001094 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095
1096 p_ptr->probing_interval = PROBING_INTERVAL;
1097 p_ptr->probing_state = CONFIRMED;
1098 p_ptr->publ.connected = 1;
1099 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1100
Allan Stephens0e659672010-12-31 18:59:32 +00001101 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001102 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001104 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105exit:
Per Liden4323add2006-01-18 00:38:21 +01001106 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001107 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 return res;
1109}
1110
Allan Stephens0c3141e2008-04-15 00:22:02 -07001111/**
1112 * tipc_disconnect_port - disconnect port from peer
1113 *
1114 * Port must be locked.
1115 */
1116
1117int tipc_disconnect_port(struct tipc_port *tp_ptr)
1118{
1119 int res;
1120
1121 if (tp_ptr->connected) {
1122 tp_ptr->connected = 0;
1123 /* let timer expire on it's own to avoid deadlock! */
1124 tipc_nodesub_unsubscribe(
1125 &((struct port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001126 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001127 } else {
1128 res = -ENOTCONN;
1129 }
1130 return res;
1131}
1132
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133/*
1134 * tipc_disconnect(): Disconnect port form peer.
1135 * This is a node local operation.
1136 */
1137
1138int tipc_disconnect(u32 ref)
1139{
1140 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001141 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142
Per Liden4323add2006-01-18 00:38:21 +01001143 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 if (!p_ptr)
1145 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001146 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001147 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 return res;
1149}
1150
1151/*
1152 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1153 */
1154int tipc_shutdown(u32 ref)
1155{
1156 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001157 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158
Per Liden4323add2006-01-18 00:38:21 +01001159 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 if (!p_ptr)
1161 return -EINVAL;
1162
1163 if (p_ptr->publ.connected) {
1164 u32 imp = msg_importance(&p_ptr->publ.phdr);
1165 if (imp < TIPC_CRITICAL_IMPORTANCE)
1166 imp++;
1167 buf = port_build_proto_msg(port_peerport(p_ptr),
1168 port_peernode(p_ptr),
1169 ref,
1170 tipc_own_addr,
1171 imp,
1172 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001173 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 port_out_seqno(p_ptr),
1175 0);
1176 }
Per Liden4323add2006-01-18 00:38:21 +01001177 tipc_port_unlock(p_ptr);
1178 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 return tipc_disconnect(ref);
1180}
1181
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182/*
Per Liden4323add2006-01-18 00:38:21 +01001183 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 * message for this node.
1185 */
1186
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001187static int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1188 struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189{
1190 struct sk_buff *buf;
1191 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001192
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001193 res = tipc_msg_build(&sender->publ.phdr, msg_sect, num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 MAX_MSG_SIZE, !sender->user_port, &buf);
1195 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001196 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 return res;
1198}
1199
1200/**
1201 * tipc_send - send message sections on connection
1202 */
1203
1204int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1205{
1206 struct port *p_ptr;
1207 u32 destnode;
1208 int res;
1209
Per Liden4323add2006-01-18 00:38:21 +01001210 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 if (!p_ptr || !p_ptr->publ.connected)
1212 return -EINVAL;
1213
1214 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001215 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 destnode = port_peernode(p_ptr);
1217 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001218 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1219 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 else
Per Liden4323add2006-01-18 00:38:21 +01001221 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222
1223 if (likely(res != -ELINKCONG)) {
1224 port_incr_out_seqno(p_ptr);
1225 p_ptr->publ.congested = 0;
1226 p_ptr->sent++;
1227 return res;
1228 }
1229 }
1230 if (port_unreliable(p_ptr)) {
1231 p_ptr->publ.congested = 0;
1232 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001233 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 }
1235 return -ELINKCONG;
1236}
1237
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001238/**
Allan Stephens12bae472010-11-30 12:01:02 +00001239 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 */
1241
Allan Stephens12bae472010-11-30 12:01:02 +00001242int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1243 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244{
1245 struct port *p_ptr;
1246 struct tipc_msg *msg;
1247 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001248 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 int res;
1250
Per Liden4323add2006-01-18 00:38:21 +01001251 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 if (!p_ptr || p_ptr->publ.connected)
1253 return -EINVAL;
1254
1255 msg = &p_ptr->publ.phdr;
1256 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001257 msg_set_orignode(msg, tipc_own_addr);
1258 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 msg_set_hdr_sz(msg, LONG_H_SIZE);
1260 msg_set_nametype(msg, name->type);
1261 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001262 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001263 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 msg_set_destnode(msg, destnode);
1265 msg_set_destport(msg, destport);
1266
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001267 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 p_ptr->sent++;
1269 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001270 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001271 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001272 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 if (likely(res != -ELINKCONG))
1274 return res;
1275 if (port_unreliable(p_ptr)) {
1276 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001277 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 }
1279 return -ELINKCONG;
1280 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001281 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001282 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283}
1284
1285/**
Allan Stephens12bae472010-11-30 12:01:02 +00001286 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 */
1288
Allan Stephens12bae472010-11-30 12:01:02 +00001289int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1290 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291{
1292 struct port *p_ptr;
1293 struct tipc_msg *msg;
1294 int res;
1295
Per Liden4323add2006-01-18 00:38:21 +01001296 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 if (!p_ptr || p_ptr->publ.connected)
1298 return -EINVAL;
1299
1300 msg = &p_ptr->publ.phdr;
1301 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001302 msg_set_orignode(msg, tipc_own_addr);
1303 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 msg_set_destnode(msg, dest->node);
1305 msg_set_destport(msg, dest->ref);
1306 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 p_ptr->sent++;
1308 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001309 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1310 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 if (likely(res != -ELINKCONG))
1312 return res;
1313 if (port_unreliable(p_ptr)) {
1314 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001315 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 }
1317 return -ELINKCONG;
1318}
1319
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001320/**
Allan Stephens12bae472010-11-30 12:01:02 +00001321 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 */
1323
Allan Stephens12bae472010-11-30 12:01:02 +00001324int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1325 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326{
1327 struct port *p_ptr;
1328 struct tipc_msg *msg;
1329 int res;
1330
Per Liden4323add2006-01-18 00:38:21 +01001331 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 if (!p_ptr || p_ptr->publ.connected)
1333 return -EINVAL;
1334
1335 msg = &p_ptr->publ.phdr;
1336 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001337 msg_set_orignode(msg, tipc_own_addr);
1338 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 msg_set_destnode(msg, dest->node);
1340 msg_set_destport(msg, dest->ref);
1341 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1343 if (skb_cow(buf, DIR_MSG_H_SIZE))
1344 return -ENOMEM;
1345
1346 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001347 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 p_ptr->sent++;
1349 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001350 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 res = tipc_send_buf_fast(buf, dest->node);
1352 if (likely(res != -ELINKCONG))
1353 return res;
1354 if (port_unreliable(p_ptr))
1355 return dsz;
1356 return -ELINKCONG;
1357}
1358