tipc: Eliminate support for tipc_mode global variable
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / tipc / port.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/port.c: TIPC port code
c4307285 3 *
05646c91 4 * Copyright (c) 1992-2007, Ericsson AB
23dd4cce 5 * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 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.
b97bf3fd 19 *
9ea1fd3c
PL
20 * 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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
b97bf3fd 39#include "port.h"
b97bf3fd 40#include "name_table.h"
b97bf3fd
PL
41
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
e3ec9c7d
AS
49static struct sk_buff *msg_queue_head;
50static struct sk_buff *msg_queue_tail;
b97bf3fd 51
34af946a
IM
52DEFINE_SPINLOCK(tipc_port_list_lock);
53static DEFINE_SPINLOCK(queue_lock);
b97bf3fd 54
4323add6 55static LIST_HEAD(ports);
b97bf3fd 56static void port_handle_node_down(unsigned long ref);
23dd4cce
AS
57static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
58static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
b97bf3fd
PL
59static void port_timeout(unsigned long ref);
60
61
23dd4cce 62static u32 port_peernode(struct tipc_port *p_ptr)
b97bf3fd 63{
23dd4cce 64 return msg_destnode(&p_ptr->phdr);
b97bf3fd
PL
65}
66
23dd4cce 67static u32 port_peerport(struct tipc_port *p_ptr)
b97bf3fd 68{
23dd4cce 69 return msg_destport(&p_ptr->phdr);
b97bf3fd
PL
70}
71
b97bf3fd
PL
72/**
73 * tipc_multicast - send a multicast message to local and remote destinations
74 */
75
38f232ea 76int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
26896904
AS
77 u32 num_sect, struct iovec const *msg_sect,
78 unsigned int total_len)
b97bf3fd
PL
79{
80 struct tipc_msg *hdr;
81 struct sk_buff *buf;
82 struct sk_buff *ibuf = NULL;
4584310b 83 struct tipc_port_list dports = {0, NULL, };
23dd4cce 84 struct tipc_port *oport = tipc_port_deref(ref);
b97bf3fd
PL
85 int ext_targets;
86 int res;
87
88 if (unlikely(!oport))
89 return -EINVAL;
90
91 /* Create multicast message */
92
23dd4cce 93 hdr = &oport->phdr;
b97bf3fd 94 msg_set_type(hdr, TIPC_MCAST_MSG);
53b94364 95 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
7462b9e9
AS
96 msg_set_destport(hdr, 0);
97 msg_set_destnode(hdr, 0);
b97bf3fd
PL
98 msg_set_nametype(hdr, seq->type);
99 msg_set_namelower(hdr, seq->lower);
100 msg_set_nameupper(hdr, seq->upper);
101 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
26896904 102 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
b97bf3fd
PL
103 !oport->user_port, &buf);
104 if (unlikely(!buf))
105 return res;
106
107 /* Figure out where to send multicast message */
108
4323add6
PL
109 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
110 TIPC_NODE_SCOPE, &dports);
c4307285
YH
111
112 /* Send message to destinations (duplicate it only if necessary) */
b97bf3fd
PL
113
114 if (ext_targets) {
115 if (dports.count != 0) {
116 ibuf = skb_copy(buf, GFP_ATOMIC);
117 if (ibuf == NULL) {
4323add6 118 tipc_port_list_free(&dports);
5f6d9123 119 kfree_skb(buf);
b97bf3fd
PL
120 return -ENOMEM;
121 }
122 }
4323add6 123 res = tipc_bclink_send_msg(buf);
a016892c 124 if ((res < 0) && (dports.count != 0))
5f6d9123 125 kfree_skb(ibuf);
b97bf3fd
PL
126 } else {
127 ibuf = buf;
128 }
129
130 if (res >= 0) {
131 if (ibuf)
4323add6 132 tipc_port_recv_mcast(ibuf, &dports);
b97bf3fd 133 } else {
4323add6 134 tipc_port_list_free(&dports);
b97bf3fd
PL
135 }
136 return res;
137}
138
139/**
4323add6 140 * tipc_port_recv_mcast - deliver multicast message to all destination ports
c4307285 141 *
b97bf3fd
PL
142 * If there is no port list, perform a lookup to create one
143 */
144
4584310b 145void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
b97bf3fd 146{
0e65967e 147 struct tipc_msg *msg;
4584310b
PG
148 struct tipc_port_list dports = {0, NULL, };
149 struct tipc_port_list *item = dp;
b97bf3fd
PL
150 int cnt = 0;
151
b97bf3fd
PL
152 msg = buf_msg(buf);
153
154 /* Create destination port list, if one wasn't supplied */
155
156 if (dp == NULL) {
4323add6 157 tipc_nametbl_mc_translate(msg_nametype(msg),
b97bf3fd
PL
158 msg_namelower(msg),
159 msg_nameupper(msg),
160 TIPC_CLUSTER_SCOPE,
161 &dports);
162 item = dp = &dports;
163 }
164
165 /* Deliver a copy of message to each destination port */
166
167 if (dp->count != 0) {
7f47f5c7 168 msg_set_destnode(msg, tipc_own_addr);
b97bf3fd
PL
169 if (dp->count == 1) {
170 msg_set_destport(msg, dp->ports[0]);
4323add6
PL
171 tipc_port_recv_msg(buf);
172 tipc_port_list_free(dp);
b97bf3fd
PL
173 return;
174 }
175 for (; cnt < dp->count; cnt++) {
176 int index = cnt % PLSIZE;
177 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
178
179 if (b == NULL) {
a10bd924 180 warn("Unable to deliver multicast message(s)\n");
b97bf3fd
PL
181 goto exit;
182 }
a016892c 183 if ((index == 0) && (cnt != 0))
b97bf3fd 184 item = item->next;
0e65967e 185 msg_set_destport(buf_msg(b), item->ports[index]);
4323add6 186 tipc_port_recv_msg(b);
b97bf3fd
PL
187 }
188 }
189exit:
5f6d9123 190 kfree_skb(buf);
4323add6 191 tipc_port_list_free(dp);
b97bf3fd
PL
192}
193
194/**
7ef43eba 195 * tipc_createport_raw - create a generic TIPC port
c4307285 196 *
0ea52241 197 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
b97bf3fd
PL
198 */
199
0ea52241 200struct tipc_port *tipc_createport_raw(void *usr_handle,
b97bf3fd
PL
201 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
202 void (*wakeup)(struct tipc_port *),
0ea52241 203 const u32 importance)
b97bf3fd 204{
23dd4cce 205 struct tipc_port *p_ptr;
b97bf3fd
PL
206 struct tipc_msg *msg;
207 u32 ref;
208
0da974f4 209 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
a10bd924
AS
210 if (!p_ptr) {
211 warn("Port creation failed, no memory\n");
0ea52241 212 return NULL;
b97bf3fd 213 }
23dd4cce 214 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
b97bf3fd 215 if (!ref) {
a10bd924 216 warn("Port creation failed, reference table exhausted\n");
b97bf3fd 217 kfree(p_ptr);
0ea52241 218 return NULL;
b97bf3fd
PL
219 }
220
23dd4cce
AS
221 p_ptr->usr_handle = usr_handle;
222 p_ptr->max_pkt = MAX_PKT_DEFAULT;
223 p_ptr->ref = ref;
224 msg = &p_ptr->phdr;
741d9eb7 225 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
b97bf3fd 226 msg_set_origport(msg, ref);
b97bf3fd
PL
227 INIT_LIST_HEAD(&p_ptr->wait_list);
228 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
b97bf3fd
PL
229 p_ptr->dispatcher = dispatcher;
230 p_ptr->wakeup = wakeup;
1fc54d8f 231 p_ptr->user_port = NULL;
b97bf3fd 232 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
4323add6 233 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
234 INIT_LIST_HEAD(&p_ptr->publications);
235 INIT_LIST_HEAD(&p_ptr->port_list);
236 list_add_tail(&p_ptr->port_list, &ports);
4323add6 237 spin_unlock_bh(&tipc_port_list_lock);
23dd4cce 238 return p_ptr;
b97bf3fd
PL
239}
240
241int tipc_deleteport(u32 ref)
242{
23dd4cce 243 struct tipc_port *p_ptr;
1fc54d8f 244 struct sk_buff *buf = NULL;
b97bf3fd 245
1fc54d8f 246 tipc_withdraw(ref, 0, NULL);
4323add6 247 p_ptr = tipc_port_lock(ref);
c4307285 248 if (!p_ptr)
b97bf3fd
PL
249 return -EINVAL;
250
4323add6
PL
251 tipc_ref_discard(ref);
252 tipc_port_unlock(p_ptr);
b97bf3fd
PL
253
254 k_cancel_timer(&p_ptr->timer);
23dd4cce 255 if (p_ptr->connected) {
b97bf3fd 256 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
4323add6 257 tipc_nodesub_unsubscribe(&p_ptr->subscription);
b97bf3fd 258 }
e83504f7 259 kfree(p_ptr->user_port);
b97bf3fd 260
4323add6 261 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
262 list_del(&p_ptr->port_list);
263 list_del(&p_ptr->wait_list);
4323add6 264 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
265 k_term_timer(&p_ptr->timer);
266 kfree(p_ptr);
4323add6 267 tipc_net_route_msg(buf);
0e35fd5e 268 return 0;
b97bf3fd
PL
269}
270
23dd4cce 271static int port_unreliable(struct tipc_port *p_ptr)
b97bf3fd 272{
23dd4cce 273 return msg_src_droppable(&p_ptr->phdr);
b97bf3fd
PL
274}
275
276int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
277{
23dd4cce 278 struct tipc_port *p_ptr;
c4307285 279
4323add6 280 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
281 if (!p_ptr)
282 return -EINVAL;
283 *isunreliable = port_unreliable(p_ptr);
4cec72c8 284 tipc_port_unlock(p_ptr);
0e35fd5e 285 return 0;
b97bf3fd
PL
286}
287
288int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
289{
23dd4cce 290 struct tipc_port *p_ptr;
c4307285 291
4323add6 292 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
293 if (!p_ptr)
294 return -EINVAL;
23dd4cce 295 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
4323add6 296 tipc_port_unlock(p_ptr);
0e35fd5e 297 return 0;
b97bf3fd
PL
298}
299
23dd4cce 300static int port_unreturnable(struct tipc_port *p_ptr)
b97bf3fd 301{
23dd4cce 302 return msg_dest_droppable(&p_ptr->phdr);
b97bf3fd
PL
303}
304
305int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
306{
23dd4cce 307 struct tipc_port *p_ptr;
c4307285 308
4323add6 309 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
310 if (!p_ptr)
311 return -EINVAL;
312 *isunrejectable = port_unreturnable(p_ptr);
4cec72c8 313 tipc_port_unlock(p_ptr);
0e35fd5e 314 return 0;
b97bf3fd
PL
315}
316
317int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
318{
23dd4cce 319 struct tipc_port *p_ptr;
c4307285 320
4323add6 321 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
322 if (!p_ptr)
323 return -EINVAL;
23dd4cce 324 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
4323add6 325 tipc_port_unlock(p_ptr);
0e35fd5e 326 return 0;
b97bf3fd
PL
327}
328
c4307285 329/*
e4a0aee4
AS
330 * port_build_proto_msg(): create connection protocol message for port
331 *
332 * On entry the port must be locked and connected.
b97bf3fd 333 */
e4a0aee4
AS
334static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
335 u32 type, u32 ack)
b97bf3fd
PL
336{
337 struct sk_buff *buf;
338 struct tipc_msg *msg;
c4307285 339
741d9eb7 340 buf = tipc_buf_acquire(INT_H_SIZE);
b97bf3fd
PL
341 if (buf) {
342 msg = buf_msg(buf);
e4a0aee4
AS
343 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
344 port_peernode(p_ptr));
345 msg_set_destport(msg, port_peerport(p_ptr));
346 msg_set_origport(msg, p_ptr->ref);
b97bf3fd 347 msg_set_msgcnt(msg, ack);
b97bf3fd
PL
348 }
349 return buf;
350}
351
b97bf3fd
PL
352int tipc_reject_msg(struct sk_buff *buf, u32 err)
353{
354 struct tipc_msg *msg = buf_msg(buf);
355 struct sk_buff *rbuf;
356 struct tipc_msg *rmsg;
357 int hdr_sz;
7dd1bf28 358 u32 imp;
b97bf3fd 359 u32 data_sz = msg_data_sz(msg);
017dac31 360 u32 src_node;
7dd1bf28 361 u32 rmsg_sz;
b97bf3fd
PL
362
363 /* discard rejected message if it shouldn't be returned to sender */
76d12527
AS
364
365 if (WARN(!msg_isdata(msg),
366 "attempt to reject message with user=%u", msg_user(msg))) {
367 dump_stack();
368 goto exit;
369 }
acc631bf
AS
370 if (msg_errcode(msg) || msg_dest_droppable(msg))
371 goto exit;
b97bf3fd 372
7dd1bf28
AS
373 /*
374 * construct returned message by copying rejected message header and
375 * data (or subset), then updating header fields that need adjusting
376 */
377
378 hdr_sz = msg_hdr_sz(msg);
379 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
380
381 rbuf = tipc_buf_acquire(rmsg_sz);
acc631bf
AS
382 if (rbuf == NULL)
383 goto exit;
384
b97bf3fd 385 rmsg = buf_msg(rbuf);
7dd1bf28
AS
386 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
387
388 if (msg_connected(rmsg)) {
389 imp = msg_importance(rmsg);
390 if (imp < TIPC_CRITICAL_IMPORTANCE)
391 msg_set_importance(rmsg, ++imp);
99c14593 392 }
7dd1bf28
AS
393 msg_set_non_seq(rmsg, 0);
394 msg_set_size(rmsg, rmsg_sz);
395 msg_set_errcode(rmsg, err);
396 msg_set_prevnode(rmsg, tipc_own_addr);
397 msg_swap_words(rmsg, 4, 5);
398 if (!msg_short(rmsg))
399 msg_swap_words(rmsg, 6, 7);
b97bf3fd
PL
400
401 /* send self-abort message when rejecting on a connected port */
402 if (msg_connected(msg)) {
23dd4cce 403 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd
PL
404
405 if (p_ptr) {
dff10e9e
AS
406 struct sk_buff *abuf = NULL;
407
23dd4cce 408 if (p_ptr->connected)
b97bf3fd 409 abuf = port_build_self_abort_msg(p_ptr, err);
4323add6 410 tipc_port_unlock(p_ptr);
dff10e9e 411 tipc_net_route_msg(abuf);
b97bf3fd 412 }
b97bf3fd
PL
413 }
414
acc631bf
AS
415 /* send returned message & dispose of rejected message */
416
017dac31
AS
417 src_node = msg_prevnode(msg);
418 if (src_node == tipc_own_addr)
419 tipc_port_recv_msg(rbuf);
420 else
421 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
acc631bf 422exit:
5f6d9123 423 kfree_skb(buf);
b97bf3fd
PL
424 return data_sz;
425}
426
23dd4cce 427int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
4323add6 428 struct iovec const *msg_sect, u32 num_sect,
26896904 429 unsigned int total_len, int err)
b97bf3fd
PL
430{
431 struct sk_buff *buf;
432 int res;
433
26896904 434 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
b97bf3fd
PL
435 !p_ptr->user_port, &buf);
436 if (!buf)
437 return res;
438
439 return tipc_reject_msg(buf, err);
440}
441
442static void port_timeout(unsigned long ref)
443{
23dd4cce 444 struct tipc_port *p_ptr = tipc_port_lock(ref);
1fc54d8f 445 struct sk_buff *buf = NULL;
b97bf3fd 446
065fd177
AS
447 if (!p_ptr)
448 return;
449
23dd4cce 450 if (!p_ptr->connected) {
065fd177 451 tipc_port_unlock(p_ptr);
b97bf3fd 452 return;
065fd177 453 }
b97bf3fd
PL
454
455 /* Last probe answered ? */
456 if (p_ptr->probing_state == PROBING) {
457 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
458 } else {
e4a0aee4 459 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
b97bf3fd
PL
460 p_ptr->probing_state = PROBING;
461 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
462 }
4323add6
PL
463 tipc_port_unlock(p_ptr);
464 tipc_net_route_msg(buf);
b97bf3fd
PL
465}
466
467
468static void port_handle_node_down(unsigned long ref)
469{
23dd4cce 470 struct tipc_port *p_ptr = tipc_port_lock(ref);
0e65967e 471 struct sk_buff *buf = NULL;
b97bf3fd
PL
472
473 if (!p_ptr)
474 return;
475 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
4323add6
PL
476 tipc_port_unlock(p_ptr);
477 tipc_net_route_msg(buf);
b97bf3fd
PL
478}
479
480
23dd4cce 481static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 482{
e244a915 483 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
b97bf3fd 484
e244a915
AS
485 if (buf) {
486 struct tipc_msg *msg = buf_msg(buf);
487 msg_swap_words(msg, 4, 5);
488 msg_swap_words(msg, 6, 7);
489 }
490 return buf;
b97bf3fd
PL
491}
492
493
23dd4cce 494static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 495{
e244a915
AS
496 struct sk_buff *buf;
497 struct tipc_msg *msg;
498 u32 imp;
b97bf3fd 499
23dd4cce 500 if (!p_ptr->connected)
1fc54d8f 501 return NULL;
e244a915
AS
502
503 buf = tipc_buf_acquire(BASIC_H_SIZE);
504 if (buf) {
505 msg = buf_msg(buf);
506 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
507 msg_set_hdr_sz(msg, BASIC_H_SIZE);
508 msg_set_size(msg, BASIC_H_SIZE);
509 imp = msg_importance(msg);
510 if (imp < TIPC_CRITICAL_IMPORTANCE)
511 msg_set_importance(msg, ++imp);
512 msg_set_errcode(msg, err);
513 }
514 return buf;
b97bf3fd
PL
515}
516
4323add6 517void tipc_port_recv_proto_msg(struct sk_buff *buf)
b97bf3fd
PL
518{
519 struct tipc_msg *msg = buf_msg(buf);
1c1a551a 520 struct tipc_port *p_ptr;
1fc54d8f 521 struct sk_buff *r_buf = NULL;
1c1a551a
AS
522 u32 orignode = msg_orignode(msg);
523 u32 origport = msg_origport(msg);
524 u32 destport = msg_destport(msg);
525 int wakeable;
526
527 /* Validate connection */
528
529 p_ptr = tipc_port_lock(destport);
530 if (!p_ptr || !p_ptr->connected ||
531 (port_peernode(p_ptr) != orignode) ||
532 (port_peerport(p_ptr) != origport)) {
f55b5640
AS
533 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
534 if (r_buf) {
535 msg = buf_msg(r_buf);
536 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
537 BASIC_H_SIZE, orignode);
538 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
539 msg_set_origport(msg, destport);
540 msg_set_destport(msg, origport);
541 }
1c1a551a
AS
542 if (p_ptr)
543 tipc_port_unlock(p_ptr);
b97bf3fd
PL
544 goto exit;
545 }
546
1c1a551a
AS
547 /* Process protocol message sent by peer */
548
549 switch (msg_type(msg)) {
550 case CONN_ACK:
551 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
552 p_ptr->wakeup;
553 p_ptr->acked += msg_msgcnt(msg);
554 if (!tipc_port_congested(p_ptr)) {
555 p_ptr->congested = 0;
556 if (wakeable)
557 p_ptr->wakeup(p_ptr);
558 }
559 break;
560 case CONN_PROBE:
e4a0aee4 561 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
1c1a551a
AS
562 break;
563 default:
564 /* CONN_PROBE_REPLY or unrecognized - no action required */
565 break;
b97bf3fd
PL
566 }
567 p_ptr->probing_state = CONFIRMED;
1c1a551a 568 tipc_port_unlock(p_ptr);
b97bf3fd 569exit:
4323add6 570 tipc_net_route_msg(r_buf);
5f6d9123 571 kfree_skb(buf);
b97bf3fd
PL
572}
573
23dd4cce 574static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
b97bf3fd 575{
c4307285 576 struct publication *publ;
b97bf3fd
PL
577
578 if (full_id)
c4307285 579 tipc_printf(buf, "<%u.%u.%u:%u>:",
b97bf3fd 580 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
23dd4cce 581 tipc_node(tipc_own_addr), p_ptr->ref);
b97bf3fd 582 else
23dd4cce 583 tipc_printf(buf, "%-10u:", p_ptr->ref);
b97bf3fd 584
23dd4cce 585 if (p_ptr->connected) {
c4307285
YH
586 u32 dport = port_peerport(p_ptr);
587 u32 destnode = port_peernode(p_ptr);
588
589 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
590 tipc_zone(destnode), tipc_cluster(destnode),
591 tipc_node(destnode), dport);
23dd4cce 592 if (p_ptr->conn_type != 0)
c4307285 593 tipc_printf(buf, " via {%u,%u}",
23dd4cce
AS
594 p_ptr->conn_type,
595 p_ptr->conn_instance);
596 } else if (p_ptr->published) {
c4307285
YH
597 tipc_printf(buf, " bound to");
598 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
b97bf3fd
PL
599 if (publ->lower == publ->upper)
600 tipc_printf(buf, " {%u,%u}", publ->type,
601 publ->lower);
602 else
c4307285 603 tipc_printf(buf, " {%u,%u,%u}", publ->type,
b97bf3fd 604 publ->lower, publ->upper);
c4307285
YH
605 }
606 }
607 tipc_printf(buf, "\n");
b97bf3fd
PL
608}
609
610#define MAX_PORT_QUERY 32768
611
4323add6 612struct sk_buff *tipc_port_get_ports(void)
b97bf3fd
PL
613{
614 struct sk_buff *buf;
615 struct tlv_desc *rep_tlv;
616 struct print_buf pb;
23dd4cce 617 struct tipc_port *p_ptr;
b97bf3fd
PL
618 int str_len;
619
4323add6 620 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
b97bf3fd
PL
621 if (!buf)
622 return NULL;
623 rep_tlv = (struct tlv_desc *)buf->data;
624
4323add6
PL
625 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
626 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 627 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 628 spin_lock_bh(p_ptr->lock);
b97bf3fd 629 port_print(p_ptr, &pb, 0);
23dd4cce 630 spin_unlock_bh(p_ptr->lock);
b97bf3fd 631 }
4323add6
PL
632 spin_unlock_bh(&tipc_port_list_lock);
633 str_len = tipc_printbuf_validate(&pb);
b97bf3fd
PL
634
635 skb_put(buf, TLV_SPACE(str_len));
636 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
637
638 return buf;
639}
640
4323add6 641void tipc_port_reinit(void)
b97bf3fd 642{
23dd4cce 643 struct tipc_port *p_ptr;
b97bf3fd
PL
644 struct tipc_msg *msg;
645
4323add6 646 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 647 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 648 msg = &p_ptr->phdr;
b97bf3fd
PL
649 if (msg_orignode(msg) == tipc_own_addr)
650 break;
6d4a6672 651 msg_set_prevnode(msg, tipc_own_addr);
b97bf3fd
PL
652 msg_set_orignode(msg, tipc_own_addr);
653 }
4323add6 654 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
655}
656
657
658/*
659 * port_dispatcher_sigh(): Signal handler for messages destinated
660 * to the tipc_port interface.
661 */
662
663static void port_dispatcher_sigh(void *dummy)
664{
665 struct sk_buff *buf;
666
667 spin_lock_bh(&queue_lock);
668 buf = msg_queue_head;
1fc54d8f 669 msg_queue_head = NULL;
b97bf3fd
PL
670 spin_unlock_bh(&queue_lock);
671
672 while (buf) {
23dd4cce 673 struct tipc_port *p_ptr;
b97bf3fd
PL
674 struct user_port *up_ptr;
675 struct tipc_portid orig;
676 struct tipc_name_seq dseq;
677 void *usr_handle;
678 int connected;
679 int published;
9688243b 680 u32 message_type;
b97bf3fd
PL
681
682 struct sk_buff *next = buf->next;
683 struct tipc_msg *msg = buf_msg(buf);
684 u32 dref = msg_destport(msg);
c4307285 685
9688243b
AS
686 message_type = msg_type(msg);
687 if (message_type > TIPC_DIRECT_MSG)
688 goto reject; /* Unsupported message type */
689
4323add6 690 p_ptr = tipc_port_lock(dref);
9688243b
AS
691 if (!p_ptr)
692 goto reject; /* Port deleted while msg in queue */
693
b97bf3fd
PL
694 orig.ref = msg_origport(msg);
695 orig.node = msg_orignode(msg);
696 up_ptr = p_ptr->user_port;
697 usr_handle = up_ptr->usr_handle;
23dd4cce
AS
698 connected = p_ptr->connected;
699 published = p_ptr->published;
b97bf3fd
PL
700
701 if (unlikely(msg_errcode(msg)))
702 goto err;
703
9688243b 704 switch (message_type) {
c4307285 705
b97bf3fd
PL
706 case TIPC_CONN_MSG:{
707 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
708 u32 peer_port = port_peerport(p_ptr);
709 u32 peer_node = port_peernode(p_ptr);
cb7ce914 710 u32 dsz;
b97bf3fd 711
4cec72c8 712 tipc_port_unlock(p_ptr);
5307e469
AS
713 if (unlikely(!cb))
714 goto reject;
b97bf3fd 715 if (unlikely(!connected)) {
84b07c16 716 if (tipc_connect2port(dref, &orig))
b97bf3fd 717 goto reject;
84b07c16
AS
718 } else if ((msg_origport(msg) != peer_port) ||
719 (msg_orignode(msg) != peer_node))
b97bf3fd 720 goto reject;
cb7ce914
AS
721 dsz = msg_data_sz(msg);
722 if (unlikely(dsz &&
723 (++p_ptr->conn_unacked >=
724 TIPC_FLOW_CONTROL_WIN)))
c4307285 725 tipc_acknowledge(dref,
23dd4cce 726 p_ptr->conn_unacked);
b97bf3fd 727 skb_pull(buf, msg_hdr_sz(msg));
cb7ce914 728 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
b97bf3fd
PL
729 break;
730 }
731 case TIPC_DIRECT_MSG:{
732 tipc_msg_event cb = up_ptr->msg_cb;
733
4cec72c8 734 tipc_port_unlock(p_ptr);
5307e469 735 if (unlikely(!cb || connected))
b97bf3fd
PL
736 goto reject;
737 skb_pull(buf, msg_hdr_sz(msg));
c4307285 738 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
739 msg_data_sz(msg), msg_importance(msg),
740 &orig);
741 break;
742 }
9688243b 743 case TIPC_MCAST_MSG:
b97bf3fd
PL
744 case TIPC_NAMED_MSG:{
745 tipc_named_msg_event cb = up_ptr->named_msg_cb;
746
4cec72c8 747 tipc_port_unlock(p_ptr);
5307e469 748 if (unlikely(!cb || connected || !published))
b97bf3fd
PL
749 goto reject;
750 dseq.type = msg_nametype(msg);
751 dseq.lower = msg_nameinst(msg);
9688243b
AS
752 dseq.upper = (message_type == TIPC_NAMED_MSG)
753 ? dseq.lower : msg_nameupper(msg);
b97bf3fd 754 skb_pull(buf, msg_hdr_sz(msg));
c4307285 755 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
756 msg_data_sz(msg), msg_importance(msg),
757 &orig, &dseq);
758 break;
759 }
760 }
761 if (buf)
5f6d9123 762 kfree_skb(buf);
b97bf3fd
PL
763 buf = next;
764 continue;
765err:
9688243b 766 switch (message_type) {
c4307285 767
b97bf3fd 768 case TIPC_CONN_MSG:{
c4307285 769 tipc_conn_shutdown_event cb =
b97bf3fd
PL
770 up_ptr->conn_err_cb;
771 u32 peer_port = port_peerport(p_ptr);
772 u32 peer_node = port_peernode(p_ptr);
773
4cec72c8 774 tipc_port_unlock(p_ptr);
5307e469 775 if (!cb || !connected)
b97bf3fd 776 break;
5307e469
AS
777 if ((msg_origport(msg) != peer_port) ||
778 (msg_orignode(msg) != peer_node))
b97bf3fd
PL
779 break;
780 tipc_disconnect(dref);
781 skb_pull(buf, msg_hdr_sz(msg));
782 cb(usr_handle, dref, &buf, msg_data(msg),
783 msg_data_sz(msg), msg_errcode(msg));
784 break;
785 }
786 case TIPC_DIRECT_MSG:{
787 tipc_msg_err_event cb = up_ptr->err_cb;
788
4cec72c8 789 tipc_port_unlock(p_ptr);
5307e469 790 if (!cb || connected)
b97bf3fd
PL
791 break;
792 skb_pull(buf, msg_hdr_sz(msg));
793 cb(usr_handle, dref, &buf, msg_data(msg),
794 msg_data_sz(msg), msg_errcode(msg), &orig);
795 break;
796 }
9688243b 797 case TIPC_MCAST_MSG:
b97bf3fd 798 case TIPC_NAMED_MSG:{
c4307285 799 tipc_named_msg_err_event cb =
b97bf3fd
PL
800 up_ptr->named_err_cb;
801
4cec72c8 802 tipc_port_unlock(p_ptr);
5307e469 803 if (!cb || connected)
b97bf3fd
PL
804 break;
805 dseq.type = msg_nametype(msg);
806 dseq.lower = msg_nameinst(msg);
9688243b
AS
807 dseq.upper = (message_type == TIPC_NAMED_MSG)
808 ? dseq.lower : msg_nameupper(msg);
b97bf3fd 809 skb_pull(buf, msg_hdr_sz(msg));
c4307285 810 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
811 msg_data_sz(msg), msg_errcode(msg), &dseq);
812 break;
813 }
814 }
815 if (buf)
5f6d9123 816 kfree_skb(buf);
b97bf3fd
PL
817 buf = next;
818 continue;
819reject:
820 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
821 buf = next;
822 }
823}
824
825/*
826 * port_dispatcher(): Dispatcher for messages destinated
827 * to the tipc_port interface. Called with port locked.
828 */
829
830static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
831{
832 buf->next = NULL;
833 spin_lock_bh(&queue_lock);
834 if (msg_queue_head) {
835 msg_queue_tail->next = buf;
836 msg_queue_tail = buf;
837 } else {
838 msg_queue_tail = msg_queue_head = buf;
4323add6 839 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
b97bf3fd
PL
840 }
841 spin_unlock_bh(&queue_lock);
0e35fd5e 842 return 0;
b97bf3fd
PL
843}
844
c4307285 845/*
b97bf3fd 846 * Wake up port after congestion: Called with port locked,
c4307285 847 *
b97bf3fd
PL
848 */
849
850static void port_wakeup_sh(unsigned long ref)
851{
23dd4cce 852 struct tipc_port *p_ptr;
b97bf3fd 853 struct user_port *up_ptr;
1fc54d8f
SR
854 tipc_continue_event cb = NULL;
855 void *uh = NULL;
b97bf3fd 856
4323add6 857 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
858 if (p_ptr) {
859 up_ptr = p_ptr->user_port;
860 if (up_ptr) {
861 cb = up_ptr->continue_event_cb;
862 uh = up_ptr->usr_handle;
863 }
4323add6 864 tipc_port_unlock(p_ptr);
b97bf3fd
PL
865 }
866 if (cb)
867 cb(uh, ref);
868}
869
870
871static void port_wakeup(struct tipc_port *p_ptr)
872{
4323add6 873 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
b97bf3fd
PL
874}
875
876void tipc_acknowledge(u32 ref, u32 ack)
877{
23dd4cce 878 struct tipc_port *p_ptr;
1fc54d8f 879 struct sk_buff *buf = NULL;
b97bf3fd 880
4323add6 881 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
882 if (!p_ptr)
883 return;
23dd4cce
AS
884 if (p_ptr->connected) {
885 p_ptr->conn_unacked -= ack;
e4a0aee4 886 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
b97bf3fd 887 }
4323add6
PL
888 tipc_port_unlock(p_ptr);
889 tipc_net_route_msg(buf);
b97bf3fd
PL
890}
891
892/*
b0c1e928 893 * tipc_createport(): user level call.
b97bf3fd
PL
894 */
895
b0c1e928 896int tipc_createport(void *usr_handle,
c4307285
YH
897 unsigned int importance,
898 tipc_msg_err_event error_cb,
899 tipc_named_msg_err_event named_error_cb,
900 tipc_conn_shutdown_event conn_error_cb,
901 tipc_msg_event msg_cb,
902 tipc_named_msg_event named_msg_cb,
903 tipc_conn_msg_event conn_msg_cb,
b97bf3fd
PL
904 tipc_continue_event continue_event_cb,/* May be zero */
905 u32 *portref)
906{
907 struct user_port *up_ptr;
23dd4cce 908 struct tipc_port *p_ptr;
b97bf3fd 909
0da974f4 910 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
a10bd924 911 if (!up_ptr) {
a75bf874 912 warn("Port creation failed, no memory\n");
b97bf3fd
PL
913 return -ENOMEM;
914 }
23dd4cce 915 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
0ea52241
AS
916 port_wakeup, importance);
917 if (!p_ptr) {
b97bf3fd
PL
918 kfree(up_ptr);
919 return -ENOMEM;
920 }
921
922 p_ptr->user_port = up_ptr;
b97bf3fd 923 up_ptr->usr_handle = usr_handle;
23dd4cce 924 up_ptr->ref = p_ptr->ref;
b97bf3fd
PL
925 up_ptr->err_cb = error_cb;
926 up_ptr->named_err_cb = named_error_cb;
927 up_ptr->conn_err_cb = conn_error_cb;
928 up_ptr->msg_cb = msg_cb;
929 up_ptr->named_msg_cb = named_msg_cb;
930 up_ptr->conn_msg_cb = conn_msg_cb;
931 up_ptr->continue_event_cb = continue_event_cb;
23dd4cce 932 *portref = p_ptr->ref;
4323add6 933 tipc_port_unlock(p_ptr);
0e35fd5e 934 return 0;
b97bf3fd
PL
935}
936
b97bf3fd
PL
937int tipc_portimportance(u32 ref, unsigned int *importance)
938{
23dd4cce 939 struct tipc_port *p_ptr;
c4307285 940
4323add6 941 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
942 if (!p_ptr)
943 return -EINVAL;
23dd4cce 944 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
4cec72c8 945 tipc_port_unlock(p_ptr);
0e35fd5e 946 return 0;
b97bf3fd
PL
947}
948
949int tipc_set_portimportance(u32 ref, unsigned int imp)
950{
23dd4cce 951 struct tipc_port *p_ptr;
b97bf3fd
PL
952
953 if (imp > TIPC_CRITICAL_IMPORTANCE)
954 return -EINVAL;
955
4323add6 956 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
957 if (!p_ptr)
958 return -EINVAL;
23dd4cce 959 msg_set_importance(&p_ptr->phdr, (u32)imp);
4cec72c8 960 tipc_port_unlock(p_ptr);
0e35fd5e 961 return 0;
b97bf3fd
PL
962}
963
964
965int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
966{
23dd4cce 967 struct tipc_port *p_ptr;
b97bf3fd
PL
968 struct publication *publ;
969 u32 key;
970 int res = -EINVAL;
971
4323add6 972 p_ptr = tipc_port_lock(ref);
d55b4c63
AB
973 if (!p_ptr)
974 return -EINVAL;
975
23dd4cce 976 if (p_ptr->connected)
b97bf3fd
PL
977 goto exit;
978 if (seq->lower > seq->upper)
979 goto exit;
980 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
981 goto exit;
982 key = ref + p_ptr->pub_count + 1;
983 if (key == ref) {
984 res = -EADDRINUSE;
985 goto exit;
986 }
4323add6 987 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
23dd4cce 988 scope, p_ptr->ref, key);
b97bf3fd
PL
989 if (publ) {
990 list_add(&publ->pport_list, &p_ptr->publications);
991 p_ptr->pub_count++;
23dd4cce 992 p_ptr->published = 1;
0e35fd5e 993 res = 0;
b97bf3fd
PL
994 }
995exit:
4323add6 996 tipc_port_unlock(p_ptr);
b97bf3fd
PL
997 return res;
998}
999
1000int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1001{
23dd4cce 1002 struct tipc_port *p_ptr;
b97bf3fd
PL
1003 struct publication *publ;
1004 struct publication *tpubl;
1005 int res = -EINVAL;
c4307285 1006
4323add6 1007 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1008 if (!p_ptr)
1009 return -EINVAL;
b97bf3fd 1010 if (!seq) {
c4307285 1011 list_for_each_entry_safe(publ, tpubl,
b97bf3fd 1012 &p_ptr->publications, pport_list) {
c4307285 1013 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 1014 publ->ref, publ->key);
b97bf3fd 1015 }
0e35fd5e 1016 res = 0;
b97bf3fd 1017 } else {
c4307285 1018 list_for_each_entry_safe(publ, tpubl,
b97bf3fd
PL
1019 &p_ptr->publications, pport_list) {
1020 if (publ->scope != scope)
1021 continue;
1022 if (publ->type != seq->type)
1023 continue;
1024 if (publ->lower != seq->lower)
1025 continue;
1026 if (publ->upper != seq->upper)
1027 break;
c4307285 1028 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 1029 publ->ref, publ->key);
0e35fd5e 1030 res = 0;
b97bf3fd
PL
1031 break;
1032 }
1033 }
1034 if (list_empty(&p_ptr->publications))
23dd4cce 1035 p_ptr->published = 0;
4323add6 1036 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1037 return res;
1038}
1039
1040int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1041{
23dd4cce 1042 struct tipc_port *p_ptr;
b97bf3fd
PL
1043 struct tipc_msg *msg;
1044 int res = -EINVAL;
1045
4323add6 1046 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1047 if (!p_ptr)
1048 return -EINVAL;
23dd4cce 1049 if (p_ptr->published || p_ptr->connected)
b97bf3fd
PL
1050 goto exit;
1051 if (!peer->ref)
1052 goto exit;
1053
23dd4cce 1054 msg = &p_ptr->phdr;
b97bf3fd
PL
1055 msg_set_destnode(msg, peer->node);
1056 msg_set_destport(msg, peer->ref);
1057 msg_set_orignode(msg, tipc_own_addr);
23dd4cce 1058 msg_set_origport(msg, p_ptr->ref);
b97bf3fd 1059 msg_set_type(msg, TIPC_CONN_MSG);
53b94364 1060 msg_set_lookup_scope(msg, 0);
08c80e9a 1061 msg_set_hdr_sz(msg, SHORT_H_SIZE);
b97bf3fd
PL
1062
1063 p_ptr->probing_interval = PROBING_INTERVAL;
1064 p_ptr->probing_state = CONFIRMED;
23dd4cce 1065 p_ptr->connected = 1;
b97bf3fd
PL
1066 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1067
0e65967e 1068 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
880b005f 1069 (void *)(unsigned long)ref,
b97bf3fd 1070 (net_ev_handler)port_handle_node_down);
0e35fd5e 1071 res = 0;
b97bf3fd 1072exit:
4323add6 1073 tipc_port_unlock(p_ptr);
23dd4cce 1074 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
b97bf3fd
PL
1075 return res;
1076}
1077
0c3141e9
AS
1078/**
1079 * tipc_disconnect_port - disconnect port from peer
1080 *
1081 * Port must be locked.
1082 */
1083
1084int tipc_disconnect_port(struct tipc_port *tp_ptr)
1085{
1086 int res;
1087
1088 if (tp_ptr->connected) {
1089 tp_ptr->connected = 0;
1090 /* let timer expire on it's own to avoid deadlock! */
1091 tipc_nodesub_unsubscribe(
23dd4cce 1092 &((struct tipc_port *)tp_ptr)->subscription);
0e35fd5e 1093 res = 0;
0c3141e9
AS
1094 } else {
1095 res = -ENOTCONN;
1096 }
1097 return res;
1098}
1099
b97bf3fd
PL
1100/*
1101 * tipc_disconnect(): Disconnect port form peer.
1102 * This is a node local operation.
1103 */
1104
1105int tipc_disconnect(u32 ref)
1106{
23dd4cce 1107 struct tipc_port *p_ptr;
0c3141e9 1108 int res;
b97bf3fd 1109
4323add6 1110 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1111 if (!p_ptr)
1112 return -EINVAL;
0c3141e9 1113 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
4323add6 1114 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1115 return res;
1116}
1117
1118/*
1119 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1120 */
1121int tipc_shutdown(u32 ref)
1122{
23dd4cce 1123 struct tipc_port *p_ptr;
1fc54d8f 1124 struct sk_buff *buf = NULL;
b97bf3fd 1125
4323add6 1126 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1127 if (!p_ptr)
1128 return -EINVAL;
1129
e244a915 1130 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
4323add6
PL
1131 tipc_port_unlock(p_ptr);
1132 tipc_net_route_msg(buf);
b97bf3fd
PL
1133 return tipc_disconnect(ref);
1134}
1135
b97bf3fd 1136/*
4323add6 1137 * tipc_port_recv_sections(): Concatenate and deliver sectioned
b97bf3fd
PL
1138 * message for this node.
1139 */
1140
23dd4cce 1141static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
26896904
AS
1142 struct iovec const *msg_sect,
1143 unsigned int total_len)
b97bf3fd
PL
1144{
1145 struct sk_buff *buf;
1146 int res;
c4307285 1147
26896904 1148 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
b97bf3fd
PL
1149 MAX_MSG_SIZE, !sender->user_port, &buf);
1150 if (likely(buf))
4323add6 1151 tipc_port_recv_msg(buf);
b97bf3fd
PL
1152 return res;
1153}
1154
1155/**
1156 * tipc_send - send message sections on connection
1157 */
1158
26896904
AS
1159int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1160 unsigned int total_len)
b97bf3fd 1161{
23dd4cce 1162 struct tipc_port *p_ptr;
b97bf3fd
PL
1163 u32 destnode;
1164 int res;
1165
4323add6 1166 p_ptr = tipc_port_deref(ref);
23dd4cce 1167 if (!p_ptr || !p_ptr->connected)
b97bf3fd
PL
1168 return -EINVAL;
1169
23dd4cce 1170 p_ptr->congested = 1;
4323add6 1171 if (!tipc_port_congested(p_ptr)) {
b97bf3fd
PL
1172 destnode = port_peernode(p_ptr);
1173 if (likely(destnode != tipc_own_addr))
4323add6 1174 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
26896904 1175 total_len, destnode);
b97bf3fd 1176 else
26896904
AS
1177 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1178 total_len);
b97bf3fd
PL
1179
1180 if (likely(res != -ELINKCONG)) {
23dd4cce 1181 p_ptr->congested = 0;
cb7ce914
AS
1182 if (res > 0)
1183 p_ptr->sent++;
b97bf3fd
PL
1184 return res;
1185 }
1186 }
1187 if (port_unreliable(p_ptr)) {
23dd4cce 1188 p_ptr->congested = 0;
26896904 1189 return total_len;
b97bf3fd
PL
1190 }
1191 return -ELINKCONG;
1192}
1193
b97bf3fd 1194/**
12bae479 1195 * tipc_send2name - send message sections to port name
b97bf3fd
PL
1196 */
1197
12bae479 1198int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
26896904
AS
1199 unsigned int num_sect, struct iovec const *msg_sect,
1200 unsigned int total_len)
b97bf3fd 1201{
23dd4cce 1202 struct tipc_port *p_ptr;
b97bf3fd
PL
1203 struct tipc_msg *msg;
1204 u32 destnode = domain;
9ccc2eb4 1205 u32 destport;
b97bf3fd
PL
1206 int res;
1207
4323add6 1208 p_ptr = tipc_port_deref(ref);
23dd4cce 1209 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1210 return -EINVAL;
1211
23dd4cce 1212 msg = &p_ptr->phdr;
b97bf3fd 1213 msg_set_type(msg, TIPC_NAMED_MSG);
12bae479
AS
1214 msg_set_orignode(msg, tipc_own_addr);
1215 msg_set_origport(msg, ref);
741d9eb7 1216 msg_set_hdr_sz(msg, NAMED_H_SIZE);
b97bf3fd
PL
1217 msg_set_nametype(msg, name->type);
1218 msg_set_nameinst(msg, name->instance);
c68ca7b7 1219 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
4323add6 1220 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
1221 msg_set_destnode(msg, destnode);
1222 msg_set_destport(msg, destport);
1223
bc9f8143 1224 if (likely(destport || destnode)) {
b97bf3fd 1225 if (likely(destnode == tipc_own_addr))
cb7ce914 1226 res = tipc_port_recv_sections(p_ptr, num_sect,
26896904 1227 msg_sect, total_len);
cb7ce914
AS
1228 else
1229 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
26896904
AS
1230 num_sect, total_len,
1231 destnode);
cb7ce914
AS
1232 if (likely(res != -ELINKCONG)) {
1233 if (res > 0)
1234 p_ptr->sent++;
b97bf3fd 1235 return res;
cb7ce914 1236 }
b97bf3fd 1237 if (port_unreliable(p_ptr)) {
26896904 1238 return total_len;
b97bf3fd
PL
1239 }
1240 return -ELINKCONG;
1241 }
c4307285 1242 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
26896904 1243 total_len, TIPC_ERR_NO_NAME);
b97bf3fd
PL
1244}
1245
1246/**
12bae479 1247 * tipc_send2port - send message sections to port identity
b97bf3fd
PL
1248 */
1249
12bae479 1250int tipc_send2port(u32 ref, struct tipc_portid const *dest,
26896904
AS
1251 unsigned int num_sect, struct iovec const *msg_sect,
1252 unsigned int total_len)
b97bf3fd 1253{
23dd4cce 1254 struct tipc_port *p_ptr;
b97bf3fd
PL
1255 struct tipc_msg *msg;
1256 int res;
1257
4323add6 1258 p_ptr = tipc_port_deref(ref);
23dd4cce 1259 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1260 return -EINVAL;
1261
23dd4cce 1262 msg = &p_ptr->phdr;
b97bf3fd 1263 msg_set_type(msg, TIPC_DIRECT_MSG);
53b94364 1264 msg_set_lookup_scope(msg, 0);
12bae479
AS
1265 msg_set_orignode(msg, tipc_own_addr);
1266 msg_set_origport(msg, ref);
b97bf3fd
PL
1267 msg_set_destnode(msg, dest->node);
1268 msg_set_destport(msg, dest->ref);
741d9eb7 1269 msg_set_hdr_sz(msg, BASIC_H_SIZE);
cb7ce914 1270
b97bf3fd 1271 if (dest->node == tipc_own_addr)
26896904
AS
1272 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1273 total_len);
cb7ce914
AS
1274 else
1275 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
26896904 1276 total_len, dest->node);
cb7ce914
AS
1277 if (likely(res != -ELINKCONG)) {
1278 if (res > 0)
1279 p_ptr->sent++;
b97bf3fd 1280 return res;
cb7ce914 1281 }
b97bf3fd 1282 if (port_unreliable(p_ptr)) {
26896904 1283 return total_len;
b97bf3fd
PL
1284 }
1285 return -ELINKCONG;
1286}
1287
c4307285 1288/**
12bae479 1289 * tipc_send_buf2port - send message buffer to port identity
b97bf3fd
PL
1290 */
1291
12bae479
AS
1292int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1293 struct sk_buff *buf, unsigned int dsz)
b97bf3fd 1294{
23dd4cce 1295 struct tipc_port *p_ptr;
b97bf3fd
PL
1296 struct tipc_msg *msg;
1297 int res;
1298
23dd4cce
AS
1299 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1300 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1301 return -EINVAL;
1302
23dd4cce 1303 msg = &p_ptr->phdr;
b97bf3fd 1304 msg_set_type(msg, TIPC_DIRECT_MSG);
12bae479
AS
1305 msg_set_orignode(msg, tipc_own_addr);
1306 msg_set_origport(msg, ref);
b97bf3fd
PL
1307 msg_set_destnode(msg, dest->node);
1308 msg_set_destport(msg, dest->ref);
741d9eb7
AS
1309 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1310 msg_set_size(msg, BASIC_H_SIZE + dsz);
1311 if (skb_cow(buf, BASIC_H_SIZE))
b97bf3fd
PL
1312 return -ENOMEM;
1313
741d9eb7
AS
1314 skb_push(buf, BASIC_H_SIZE);
1315 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
cb7ce914 1316
b97bf3fd 1317 if (dest->node == tipc_own_addr)
cb7ce914
AS
1318 res = tipc_port_recv_msg(buf);
1319 else
1320 res = tipc_send_buf_fast(buf, dest->node);
1321 if (likely(res != -ELINKCONG)) {
1322 if (res > 0)
1323 p_ptr->sent++;
b97bf3fd 1324 return res;
cb7ce914 1325 }
b97bf3fd
PL
1326 if (port_unreliable(p_ptr))
1327 return dsz;
1328 return -ELINKCONG;
1329}
1330