]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/tipc/port.c
Merge branch 'docs/docbook/drm' of git://github.com/mfwitten/linux into docs-move
[mirror_ubuntu-zesty-kernel.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;
83 struct 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);
b97bf3fd
PL
119 buf_discard(buf);
120 return -ENOMEM;
121 }
122 }
4323add6 123 res = tipc_bclink_send_msg(buf);
a016892c 124 if ((res < 0) && (dports.count != 0))
b97bf3fd 125 buf_discard(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
4323add6 145void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
b97bf3fd 146{
0e65967e 147 struct tipc_msg *msg;
b97bf3fd
PL
148 struct port_list dports = {0, NULL, };
149 struct port_list *item = dp;
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:
190 buf_discard(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)) {
1fc54d8f 403 struct sk_buff *abuf = NULL;
23dd4cce 404 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd
PL
405
406 if (p_ptr) {
23dd4cce 407 if (p_ptr->connected)
b97bf3fd 408 abuf = port_build_self_abort_msg(p_ptr, err);
4323add6 409 tipc_port_unlock(p_ptr);
b97bf3fd 410 }
4323add6 411 tipc_net_route_msg(abuf);
b97bf3fd
PL
412 }
413
acc631bf
AS
414 /* send returned message & dispose of rejected message */
415
017dac31
AS
416 src_node = msg_prevnode(msg);
417 if (src_node == tipc_own_addr)
418 tipc_port_recv_msg(rbuf);
419 else
420 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
acc631bf
AS
421exit:
422 buf_discard(buf);
b97bf3fd
PL
423 return data_sz;
424}
425
23dd4cce 426int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
4323add6 427 struct iovec const *msg_sect, u32 num_sect,
26896904 428 unsigned int total_len, int err)
b97bf3fd
PL
429{
430 struct sk_buff *buf;
431 int res;
432
26896904 433 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
b97bf3fd
PL
434 !p_ptr->user_port, &buf);
435 if (!buf)
436 return res;
437
438 return tipc_reject_msg(buf, err);
439}
440
441static void port_timeout(unsigned long ref)
442{
23dd4cce 443 struct tipc_port *p_ptr = tipc_port_lock(ref);
1fc54d8f 444 struct sk_buff *buf = NULL;
b97bf3fd 445
065fd177
AS
446 if (!p_ptr)
447 return;
448
23dd4cce 449 if (!p_ptr->connected) {
065fd177 450 tipc_port_unlock(p_ptr);
b97bf3fd 451 return;
065fd177 452 }
b97bf3fd
PL
453
454 /* Last probe answered ? */
455 if (p_ptr->probing_state == PROBING) {
456 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
457 } else {
e4a0aee4 458 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
b97bf3fd
PL
459 p_ptr->probing_state = PROBING;
460 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
461 }
4323add6
PL
462 tipc_port_unlock(p_ptr);
463 tipc_net_route_msg(buf);
b97bf3fd
PL
464}
465
466
467static void port_handle_node_down(unsigned long ref)
468{
23dd4cce 469 struct tipc_port *p_ptr = tipc_port_lock(ref);
0e65967e 470 struct sk_buff *buf = NULL;
b97bf3fd
PL
471
472 if (!p_ptr)
473 return;
474 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
4323add6
PL
475 tipc_port_unlock(p_ptr);
476 tipc_net_route_msg(buf);
b97bf3fd
PL
477}
478
479
23dd4cce 480static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 481{
e244a915 482 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
b97bf3fd 483
e244a915
AS
484 if (buf) {
485 struct tipc_msg *msg = buf_msg(buf);
486 msg_swap_words(msg, 4, 5);
487 msg_swap_words(msg, 6, 7);
488 }
489 return buf;
b97bf3fd
PL
490}
491
492
23dd4cce 493static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 494{
e244a915
AS
495 struct sk_buff *buf;
496 struct tipc_msg *msg;
497 u32 imp;
b97bf3fd 498
23dd4cce 499 if (!p_ptr->connected)
1fc54d8f 500 return NULL;
e244a915
AS
501
502 buf = tipc_buf_acquire(BASIC_H_SIZE);
503 if (buf) {
504 msg = buf_msg(buf);
505 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
506 msg_set_hdr_sz(msg, BASIC_H_SIZE);
507 msg_set_size(msg, BASIC_H_SIZE);
508 imp = msg_importance(msg);
509 if (imp < TIPC_CRITICAL_IMPORTANCE)
510 msg_set_importance(msg, ++imp);
511 msg_set_errcode(msg, err);
512 }
513 return buf;
b97bf3fd
PL
514}
515
4323add6 516void tipc_port_recv_proto_msg(struct sk_buff *buf)
b97bf3fd
PL
517{
518 struct tipc_msg *msg = buf_msg(buf);
1c1a551a 519 struct tipc_port *p_ptr;
1fc54d8f 520 struct sk_buff *r_buf = NULL;
1c1a551a
AS
521 u32 orignode = msg_orignode(msg);
522 u32 origport = msg_origport(msg);
523 u32 destport = msg_destport(msg);
524 int wakeable;
525
526 /* Validate connection */
527
528 p_ptr = tipc_port_lock(destport);
529 if (!p_ptr || !p_ptr->connected ||
530 (port_peernode(p_ptr) != orignode) ||
531 (port_peerport(p_ptr) != origport)) {
f55b5640
AS
532 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
533 if (r_buf) {
534 msg = buf_msg(r_buf);
535 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
536 BASIC_H_SIZE, orignode);
537 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
538 msg_set_origport(msg, destport);
539 msg_set_destport(msg, origport);
540 }
1c1a551a
AS
541 if (p_ptr)
542 tipc_port_unlock(p_ptr);
b97bf3fd
PL
543 goto exit;
544 }
545
1c1a551a
AS
546 /* Process protocol message sent by peer */
547
548 switch (msg_type(msg)) {
549 case CONN_ACK:
550 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
551 p_ptr->wakeup;
552 p_ptr->acked += msg_msgcnt(msg);
553 if (!tipc_port_congested(p_ptr)) {
554 p_ptr->congested = 0;
555 if (wakeable)
556 p_ptr->wakeup(p_ptr);
557 }
558 break;
559 case CONN_PROBE:
e4a0aee4 560 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
1c1a551a
AS
561 break;
562 default:
563 /* CONN_PROBE_REPLY or unrecognized - no action required */
564 break;
b97bf3fd
PL
565 }
566 p_ptr->probing_state = CONFIRMED;
1c1a551a 567 tipc_port_unlock(p_ptr);
b97bf3fd 568exit:
4323add6 569 tipc_net_route_msg(r_buf);
b97bf3fd
PL
570 buf_discard(buf);
571}
572
23dd4cce 573static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
b97bf3fd 574{
c4307285 575 struct publication *publ;
b97bf3fd
PL
576
577 if (full_id)
c4307285 578 tipc_printf(buf, "<%u.%u.%u:%u>:",
b97bf3fd 579 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
23dd4cce 580 tipc_node(tipc_own_addr), p_ptr->ref);
b97bf3fd 581 else
23dd4cce 582 tipc_printf(buf, "%-10u:", p_ptr->ref);
b97bf3fd 583
23dd4cce 584 if (p_ptr->connected) {
c4307285
YH
585 u32 dport = port_peerport(p_ptr);
586 u32 destnode = port_peernode(p_ptr);
587
588 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
589 tipc_zone(destnode), tipc_cluster(destnode),
590 tipc_node(destnode), dport);
23dd4cce 591 if (p_ptr->conn_type != 0)
c4307285 592 tipc_printf(buf, " via {%u,%u}",
23dd4cce
AS
593 p_ptr->conn_type,
594 p_ptr->conn_instance);
595 } else if (p_ptr->published) {
c4307285
YH
596 tipc_printf(buf, " bound to");
597 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
b97bf3fd
PL
598 if (publ->lower == publ->upper)
599 tipc_printf(buf, " {%u,%u}", publ->type,
600 publ->lower);
601 else
c4307285 602 tipc_printf(buf, " {%u,%u,%u}", publ->type,
b97bf3fd 603 publ->lower, publ->upper);
c4307285
YH
604 }
605 }
606 tipc_printf(buf, "\n");
b97bf3fd
PL
607}
608
609#define MAX_PORT_QUERY 32768
610
4323add6 611struct sk_buff *tipc_port_get_ports(void)
b97bf3fd
PL
612{
613 struct sk_buff *buf;
614 struct tlv_desc *rep_tlv;
615 struct print_buf pb;
23dd4cce 616 struct tipc_port *p_ptr;
b97bf3fd
PL
617 int str_len;
618
4323add6 619 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
b97bf3fd
PL
620 if (!buf)
621 return NULL;
622 rep_tlv = (struct tlv_desc *)buf->data;
623
4323add6
PL
624 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
625 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 626 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 627 spin_lock_bh(p_ptr->lock);
b97bf3fd 628 port_print(p_ptr, &pb, 0);
23dd4cce 629 spin_unlock_bh(p_ptr->lock);
b97bf3fd 630 }
4323add6
PL
631 spin_unlock_bh(&tipc_port_list_lock);
632 str_len = tipc_printbuf_validate(&pb);
b97bf3fd
PL
633
634 skb_put(buf, TLV_SPACE(str_len));
635 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
636
637 return buf;
638}
639
4323add6 640void tipc_port_reinit(void)
b97bf3fd 641{
23dd4cce 642 struct tipc_port *p_ptr;
b97bf3fd
PL
643 struct tipc_msg *msg;
644
4323add6 645 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 646 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 647 msg = &p_ptr->phdr;
b97bf3fd
PL
648 if (msg_orignode(msg) == tipc_own_addr)
649 break;
6d4a6672 650 msg_set_prevnode(msg, tipc_own_addr);
b97bf3fd
PL
651 msg_set_orignode(msg, tipc_own_addr);
652 }
4323add6 653 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
654}
655
656
657/*
658 * port_dispatcher_sigh(): Signal handler for messages destinated
659 * to the tipc_port interface.
660 */
661
662static void port_dispatcher_sigh(void *dummy)
663{
664 struct sk_buff *buf;
665
666 spin_lock_bh(&queue_lock);
667 buf = msg_queue_head;
1fc54d8f 668 msg_queue_head = NULL;
b97bf3fd
PL
669 spin_unlock_bh(&queue_lock);
670
671 while (buf) {
23dd4cce 672 struct tipc_port *p_ptr;
b97bf3fd
PL
673 struct user_port *up_ptr;
674 struct tipc_portid orig;
675 struct tipc_name_seq dseq;
676 void *usr_handle;
677 int connected;
678 int published;
9688243b 679 u32 message_type;
b97bf3fd
PL
680
681 struct sk_buff *next = buf->next;
682 struct tipc_msg *msg = buf_msg(buf);
683 u32 dref = msg_destport(msg);
c4307285 684
9688243b
AS
685 message_type = msg_type(msg);
686 if (message_type > TIPC_DIRECT_MSG)
687 goto reject; /* Unsupported message type */
688
4323add6 689 p_ptr = tipc_port_lock(dref);
9688243b
AS
690 if (!p_ptr)
691 goto reject; /* Port deleted while msg in queue */
692
b97bf3fd
PL
693 orig.ref = msg_origport(msg);
694 orig.node = msg_orignode(msg);
695 up_ptr = p_ptr->user_port;
696 usr_handle = up_ptr->usr_handle;
23dd4cce
AS
697 connected = p_ptr->connected;
698 published = p_ptr->published;
b97bf3fd
PL
699
700 if (unlikely(msg_errcode(msg)))
701 goto err;
702
9688243b 703 switch (message_type) {
c4307285 704
b97bf3fd
PL
705 case TIPC_CONN_MSG:{
706 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
707 u32 peer_port = port_peerport(p_ptr);
708 u32 peer_node = port_peernode(p_ptr);
cb7ce914 709 u32 dsz;
b97bf3fd 710
4cec72c8 711 tipc_port_unlock(p_ptr);
5307e469
AS
712 if (unlikely(!cb))
713 goto reject;
b97bf3fd 714 if (unlikely(!connected)) {
84b07c16 715 if (tipc_connect2port(dref, &orig))
b97bf3fd 716 goto reject;
84b07c16
AS
717 } else if ((msg_origport(msg) != peer_port) ||
718 (msg_orignode(msg) != peer_node))
b97bf3fd 719 goto reject;
cb7ce914
AS
720 dsz = msg_data_sz(msg);
721 if (unlikely(dsz &&
722 (++p_ptr->conn_unacked >=
723 TIPC_FLOW_CONTROL_WIN)))
c4307285 724 tipc_acknowledge(dref,
23dd4cce 725 p_ptr->conn_unacked);
b97bf3fd 726 skb_pull(buf, msg_hdr_sz(msg));
cb7ce914 727 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
b97bf3fd
PL
728 break;
729 }
730 case TIPC_DIRECT_MSG:{
731 tipc_msg_event cb = up_ptr->msg_cb;
732
4cec72c8 733 tipc_port_unlock(p_ptr);
5307e469 734 if (unlikely(!cb || connected))
b97bf3fd
PL
735 goto reject;
736 skb_pull(buf, msg_hdr_sz(msg));
c4307285 737 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
738 msg_data_sz(msg), msg_importance(msg),
739 &orig);
740 break;
741 }
9688243b 742 case TIPC_MCAST_MSG:
b97bf3fd
PL
743 case TIPC_NAMED_MSG:{
744 tipc_named_msg_event cb = up_ptr->named_msg_cb;
745
4cec72c8 746 tipc_port_unlock(p_ptr);
5307e469 747 if (unlikely(!cb || connected || !published))
b97bf3fd
PL
748 goto reject;
749 dseq.type = msg_nametype(msg);
750 dseq.lower = msg_nameinst(msg);
9688243b
AS
751 dseq.upper = (message_type == TIPC_NAMED_MSG)
752 ? dseq.lower : msg_nameupper(msg);
b97bf3fd 753 skb_pull(buf, msg_hdr_sz(msg));
c4307285 754 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
755 msg_data_sz(msg), msg_importance(msg),
756 &orig, &dseq);
757 break;
758 }
759 }
760 if (buf)
761 buf_discard(buf);
762 buf = next;
763 continue;
764err:
9688243b 765 switch (message_type) {
c4307285 766
b97bf3fd 767 case TIPC_CONN_MSG:{
c4307285 768 tipc_conn_shutdown_event cb =
b97bf3fd
PL
769 up_ptr->conn_err_cb;
770 u32 peer_port = port_peerport(p_ptr);
771 u32 peer_node = port_peernode(p_ptr);
772
4cec72c8 773 tipc_port_unlock(p_ptr);
5307e469 774 if (!cb || !connected)
b97bf3fd 775 break;
5307e469
AS
776 if ((msg_origport(msg) != peer_port) ||
777 (msg_orignode(msg) != peer_node))
b97bf3fd
PL
778 break;
779 tipc_disconnect(dref);
780 skb_pull(buf, msg_hdr_sz(msg));
781 cb(usr_handle, dref, &buf, msg_data(msg),
782 msg_data_sz(msg), msg_errcode(msg));
783 break;
784 }
785 case TIPC_DIRECT_MSG:{
786 tipc_msg_err_event cb = up_ptr->err_cb;
787
4cec72c8 788 tipc_port_unlock(p_ptr);
5307e469 789 if (!cb || connected)
b97bf3fd
PL
790 break;
791 skb_pull(buf, msg_hdr_sz(msg));
792 cb(usr_handle, dref, &buf, msg_data(msg),
793 msg_data_sz(msg), msg_errcode(msg), &orig);
794 break;
795 }
9688243b 796 case TIPC_MCAST_MSG:
b97bf3fd 797 case TIPC_NAMED_MSG:{
c4307285 798 tipc_named_msg_err_event cb =
b97bf3fd
PL
799 up_ptr->named_err_cb;
800
4cec72c8 801 tipc_port_unlock(p_ptr);
5307e469 802 if (!cb || connected)
b97bf3fd
PL
803 break;
804 dseq.type = msg_nametype(msg);
805 dseq.lower = msg_nameinst(msg);
9688243b
AS
806 dseq.upper = (message_type == TIPC_NAMED_MSG)
807 ? dseq.lower : msg_nameupper(msg);
b97bf3fd 808 skb_pull(buf, msg_hdr_sz(msg));
c4307285 809 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
810 msg_data_sz(msg), msg_errcode(msg), &dseq);
811 break;
812 }
813 }
814 if (buf)
815 buf_discard(buf);
816 buf = next;
817 continue;
818reject:
819 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
820 buf = next;
821 }
822}
823
824/*
825 * port_dispatcher(): Dispatcher for messages destinated
826 * to the tipc_port interface. Called with port locked.
827 */
828
829static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
830{
831 buf->next = NULL;
832 spin_lock_bh(&queue_lock);
833 if (msg_queue_head) {
834 msg_queue_tail->next = buf;
835 msg_queue_tail = buf;
836 } else {
837 msg_queue_tail = msg_queue_head = buf;
4323add6 838 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
b97bf3fd
PL
839 }
840 spin_unlock_bh(&queue_lock);
0e35fd5e 841 return 0;
b97bf3fd
PL
842}
843
c4307285 844/*
b97bf3fd 845 * Wake up port after congestion: Called with port locked,
c4307285 846 *
b97bf3fd
PL
847 */
848
849static void port_wakeup_sh(unsigned long ref)
850{
23dd4cce 851 struct tipc_port *p_ptr;
b97bf3fd 852 struct user_port *up_ptr;
1fc54d8f
SR
853 tipc_continue_event cb = NULL;
854 void *uh = NULL;
b97bf3fd 855
4323add6 856 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
857 if (p_ptr) {
858 up_ptr = p_ptr->user_port;
859 if (up_ptr) {
860 cb = up_ptr->continue_event_cb;
861 uh = up_ptr->usr_handle;
862 }
4323add6 863 tipc_port_unlock(p_ptr);
b97bf3fd
PL
864 }
865 if (cb)
866 cb(uh, ref);
867}
868
869
870static void port_wakeup(struct tipc_port *p_ptr)
871{
4323add6 872 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
b97bf3fd
PL
873}
874
875void tipc_acknowledge(u32 ref, u32 ack)
876{
23dd4cce 877 struct tipc_port *p_ptr;
1fc54d8f 878 struct sk_buff *buf = NULL;
b97bf3fd 879
4323add6 880 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
881 if (!p_ptr)
882 return;
23dd4cce
AS
883 if (p_ptr->connected) {
884 p_ptr->conn_unacked -= ack;
e4a0aee4 885 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
b97bf3fd 886 }
4323add6
PL
887 tipc_port_unlock(p_ptr);
888 tipc_net_route_msg(buf);
b97bf3fd
PL
889}
890
891/*
b0c1e928 892 * tipc_createport(): user level call.
b97bf3fd
PL
893 */
894
b0c1e928 895int tipc_createport(void *usr_handle,
c4307285
YH
896 unsigned int importance,
897 tipc_msg_err_event error_cb,
898 tipc_named_msg_err_event named_error_cb,
899 tipc_conn_shutdown_event conn_error_cb,
900 tipc_msg_event msg_cb,
901 tipc_named_msg_event named_msg_cb,
902 tipc_conn_msg_event conn_msg_cb,
b97bf3fd
PL
903 tipc_continue_event continue_event_cb,/* May be zero */
904 u32 *portref)
905{
906 struct user_port *up_ptr;
23dd4cce 907 struct tipc_port *p_ptr;
b97bf3fd 908
0da974f4 909 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
a10bd924 910 if (!up_ptr) {
a75bf874 911 warn("Port creation failed, no memory\n");
b97bf3fd
PL
912 return -ENOMEM;
913 }
23dd4cce 914 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
0ea52241
AS
915 port_wakeup, importance);
916 if (!p_ptr) {
b97bf3fd
PL
917 kfree(up_ptr);
918 return -ENOMEM;
919 }
920
921 p_ptr->user_port = up_ptr;
b97bf3fd 922 up_ptr->usr_handle = usr_handle;
23dd4cce 923 up_ptr->ref = p_ptr->ref;
b97bf3fd
PL
924 up_ptr->err_cb = error_cb;
925 up_ptr->named_err_cb = named_error_cb;
926 up_ptr->conn_err_cb = conn_error_cb;
927 up_ptr->msg_cb = msg_cb;
928 up_ptr->named_msg_cb = named_msg_cb;
929 up_ptr->conn_msg_cb = conn_msg_cb;
930 up_ptr->continue_event_cb = continue_event_cb;
23dd4cce 931 *portref = p_ptr->ref;
4323add6 932 tipc_port_unlock(p_ptr);
0e35fd5e 933 return 0;
b97bf3fd
PL
934}
935
b97bf3fd
PL
936int tipc_portimportance(u32 ref, unsigned int *importance)
937{
23dd4cce 938 struct tipc_port *p_ptr;
c4307285 939
4323add6 940 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
941 if (!p_ptr)
942 return -EINVAL;
23dd4cce 943 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
4cec72c8 944 tipc_port_unlock(p_ptr);
0e35fd5e 945 return 0;
b97bf3fd
PL
946}
947
948int tipc_set_portimportance(u32 ref, unsigned int imp)
949{
23dd4cce 950 struct tipc_port *p_ptr;
b97bf3fd
PL
951
952 if (imp > TIPC_CRITICAL_IMPORTANCE)
953 return -EINVAL;
954
4323add6 955 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
956 if (!p_ptr)
957 return -EINVAL;
23dd4cce 958 msg_set_importance(&p_ptr->phdr, (u32)imp);
4cec72c8 959 tipc_port_unlock(p_ptr);
0e35fd5e 960 return 0;
b97bf3fd
PL
961}
962
963
964int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
965{
23dd4cce 966 struct tipc_port *p_ptr;
b97bf3fd
PL
967 struct publication *publ;
968 u32 key;
969 int res = -EINVAL;
970
4323add6 971 p_ptr = tipc_port_lock(ref);
d55b4c63
AB
972 if (!p_ptr)
973 return -EINVAL;
974
23dd4cce 975 if (p_ptr->connected)
b97bf3fd
PL
976 goto exit;
977 if (seq->lower > seq->upper)
978 goto exit;
979 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
980 goto exit;
981 key = ref + p_ptr->pub_count + 1;
982 if (key == ref) {
983 res = -EADDRINUSE;
984 goto exit;
985 }
4323add6 986 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
23dd4cce 987 scope, p_ptr->ref, key);
b97bf3fd
PL
988 if (publ) {
989 list_add(&publ->pport_list, &p_ptr->publications);
990 p_ptr->pub_count++;
23dd4cce 991 p_ptr->published = 1;
0e35fd5e 992 res = 0;
b97bf3fd
PL
993 }
994exit:
4323add6 995 tipc_port_unlock(p_ptr);
b97bf3fd
PL
996 return res;
997}
998
999int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1000{
23dd4cce 1001 struct tipc_port *p_ptr;
b97bf3fd
PL
1002 struct publication *publ;
1003 struct publication *tpubl;
1004 int res = -EINVAL;
c4307285 1005
4323add6 1006 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1007 if (!p_ptr)
1008 return -EINVAL;
b97bf3fd 1009 if (!seq) {
c4307285 1010 list_for_each_entry_safe(publ, tpubl,
b97bf3fd 1011 &p_ptr->publications, pport_list) {
c4307285 1012 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 1013 publ->ref, publ->key);
b97bf3fd 1014 }
0e35fd5e 1015 res = 0;
b97bf3fd 1016 } else {
c4307285 1017 list_for_each_entry_safe(publ, tpubl,
b97bf3fd
PL
1018 &p_ptr->publications, pport_list) {
1019 if (publ->scope != scope)
1020 continue;
1021 if (publ->type != seq->type)
1022 continue;
1023 if (publ->lower != seq->lower)
1024 continue;
1025 if (publ->upper != seq->upper)
1026 break;
c4307285 1027 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 1028 publ->ref, publ->key);
0e35fd5e 1029 res = 0;
b97bf3fd
PL
1030 break;
1031 }
1032 }
1033 if (list_empty(&p_ptr->publications))
23dd4cce 1034 p_ptr->published = 0;
4323add6 1035 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1036 return res;
1037}
1038
1039int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1040{
23dd4cce 1041 struct tipc_port *p_ptr;
b97bf3fd
PL
1042 struct tipc_msg *msg;
1043 int res = -EINVAL;
1044
4323add6 1045 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1046 if (!p_ptr)
1047 return -EINVAL;
23dd4cce 1048 if (p_ptr->published || p_ptr->connected)
b97bf3fd
PL
1049 goto exit;
1050 if (!peer->ref)
1051 goto exit;
1052
23dd4cce 1053 msg = &p_ptr->phdr;
b97bf3fd
PL
1054 msg_set_destnode(msg, peer->node);
1055 msg_set_destport(msg, peer->ref);
1056 msg_set_orignode(msg, tipc_own_addr);
23dd4cce 1057 msg_set_origport(msg, p_ptr->ref);
b97bf3fd 1058 msg_set_type(msg, TIPC_CONN_MSG);
53b94364 1059 msg_set_lookup_scope(msg, 0);
08c80e9a 1060 msg_set_hdr_sz(msg, SHORT_H_SIZE);
b97bf3fd
PL
1061
1062 p_ptr->probing_interval = PROBING_INTERVAL;
1063 p_ptr->probing_state = CONFIRMED;
23dd4cce 1064 p_ptr->connected = 1;
b97bf3fd
PL
1065 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1066
0e65967e 1067 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
880b005f 1068 (void *)(unsigned long)ref,
b97bf3fd 1069 (net_ev_handler)port_handle_node_down);
0e35fd5e 1070 res = 0;
b97bf3fd 1071exit:
4323add6 1072 tipc_port_unlock(p_ptr);
23dd4cce 1073 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
b97bf3fd
PL
1074 return res;
1075}
1076
0c3141e9
AS
1077/**
1078 * tipc_disconnect_port - disconnect port from peer
1079 *
1080 * Port must be locked.
1081 */
1082
1083int tipc_disconnect_port(struct tipc_port *tp_ptr)
1084{
1085 int res;
1086
1087 if (tp_ptr->connected) {
1088 tp_ptr->connected = 0;
1089 /* let timer expire on it's own to avoid deadlock! */
1090 tipc_nodesub_unsubscribe(
23dd4cce 1091 &((struct tipc_port *)tp_ptr)->subscription);
0e35fd5e 1092 res = 0;
0c3141e9
AS
1093 } else {
1094 res = -ENOTCONN;
1095 }
1096 return res;
1097}
1098
b97bf3fd
PL
1099/*
1100 * tipc_disconnect(): Disconnect port form peer.
1101 * This is a node local operation.
1102 */
1103
1104int tipc_disconnect(u32 ref)
1105{
23dd4cce 1106 struct tipc_port *p_ptr;
0c3141e9 1107 int res;
b97bf3fd 1108
4323add6 1109 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1110 if (!p_ptr)
1111 return -EINVAL;
0c3141e9 1112 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
4323add6 1113 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1114 return res;
1115}
1116
1117/*
1118 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1119 */
1120int tipc_shutdown(u32 ref)
1121{
23dd4cce 1122 struct tipc_port *p_ptr;
1fc54d8f 1123 struct sk_buff *buf = NULL;
b97bf3fd 1124
4323add6 1125 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1126 if (!p_ptr)
1127 return -EINVAL;
1128
e244a915 1129 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
4323add6
PL
1130 tipc_port_unlock(p_ptr);
1131 tipc_net_route_msg(buf);
b97bf3fd
PL
1132 return tipc_disconnect(ref);
1133}
1134
b97bf3fd 1135/*
4323add6 1136 * tipc_port_recv_sections(): Concatenate and deliver sectioned
b97bf3fd
PL
1137 * message for this node.
1138 */
1139
23dd4cce 1140static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
26896904
AS
1141 struct iovec const *msg_sect,
1142 unsigned int total_len)
b97bf3fd
PL
1143{
1144 struct sk_buff *buf;
1145 int res;
c4307285 1146
26896904 1147 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
b97bf3fd
PL
1148 MAX_MSG_SIZE, !sender->user_port, &buf);
1149 if (likely(buf))
4323add6 1150 tipc_port_recv_msg(buf);
b97bf3fd
PL
1151 return res;
1152}
1153
1154/**
1155 * tipc_send - send message sections on connection
1156 */
1157
26896904
AS
1158int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1159 unsigned int total_len)
b97bf3fd 1160{
23dd4cce 1161 struct tipc_port *p_ptr;
b97bf3fd
PL
1162 u32 destnode;
1163 int res;
1164
4323add6 1165 p_ptr = tipc_port_deref(ref);
23dd4cce 1166 if (!p_ptr || !p_ptr->connected)
b97bf3fd
PL
1167 return -EINVAL;
1168
23dd4cce 1169 p_ptr->congested = 1;
4323add6 1170 if (!tipc_port_congested(p_ptr)) {
b97bf3fd
PL
1171 destnode = port_peernode(p_ptr);
1172 if (likely(destnode != tipc_own_addr))
4323add6 1173 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
26896904 1174 total_len, destnode);
b97bf3fd 1175 else
26896904
AS
1176 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1177 total_len);
b97bf3fd
PL
1178
1179 if (likely(res != -ELINKCONG)) {
23dd4cce 1180 p_ptr->congested = 0;
cb7ce914
AS
1181 if (res > 0)
1182 p_ptr->sent++;
b97bf3fd
PL
1183 return res;
1184 }
1185 }
1186 if (port_unreliable(p_ptr)) {
23dd4cce 1187 p_ptr->congested = 0;
26896904 1188 return total_len;
b97bf3fd
PL
1189 }
1190 return -ELINKCONG;
1191}
1192
b97bf3fd 1193/**
12bae479 1194 * tipc_send2name - send message sections to port name
b97bf3fd
PL
1195 */
1196
12bae479 1197int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
26896904
AS
1198 unsigned int num_sect, struct iovec const *msg_sect,
1199 unsigned int total_len)
b97bf3fd 1200{
23dd4cce 1201 struct tipc_port *p_ptr;
b97bf3fd
PL
1202 struct tipc_msg *msg;
1203 u32 destnode = domain;
9ccc2eb4 1204 u32 destport;
b97bf3fd
PL
1205 int res;
1206
4323add6 1207 p_ptr = tipc_port_deref(ref);
23dd4cce 1208 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1209 return -EINVAL;
1210
23dd4cce 1211 msg = &p_ptr->phdr;
b97bf3fd 1212 msg_set_type(msg, TIPC_NAMED_MSG);
12bae479
AS
1213 msg_set_orignode(msg, tipc_own_addr);
1214 msg_set_origport(msg, ref);
741d9eb7 1215 msg_set_hdr_sz(msg, NAMED_H_SIZE);
b97bf3fd
PL
1216 msg_set_nametype(msg, name->type);
1217 msg_set_nameinst(msg, name->instance);
c68ca7b7 1218 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
4323add6 1219 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
1220 msg_set_destnode(msg, destnode);
1221 msg_set_destport(msg, destport);
1222
5d9c54c1 1223 if (likely(destport)) {
b97bf3fd 1224 if (likely(destnode == tipc_own_addr))
cb7ce914 1225 res = tipc_port_recv_sections(p_ptr, num_sect,
26896904 1226 msg_sect, total_len);
cb7ce914
AS
1227 else
1228 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
26896904
AS
1229 num_sect, total_len,
1230 destnode);
cb7ce914
AS
1231 if (likely(res != -ELINKCONG)) {
1232 if (res > 0)
1233 p_ptr->sent++;
b97bf3fd 1234 return res;
cb7ce914 1235 }
b97bf3fd 1236 if (port_unreliable(p_ptr)) {
26896904 1237 return total_len;
b97bf3fd
PL
1238 }
1239 return -ELINKCONG;
1240 }
c4307285 1241 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
26896904 1242 total_len, TIPC_ERR_NO_NAME);
b97bf3fd
PL
1243}
1244
1245/**
12bae479 1246 * tipc_send2port - send message sections to port identity
b97bf3fd
PL
1247 */
1248
12bae479 1249int tipc_send2port(u32 ref, struct tipc_portid const *dest,
26896904
AS
1250 unsigned int num_sect, struct iovec const *msg_sect,
1251 unsigned int total_len)
b97bf3fd 1252{
23dd4cce 1253 struct tipc_port *p_ptr;
b97bf3fd
PL
1254 struct tipc_msg *msg;
1255 int res;
1256
4323add6 1257 p_ptr = tipc_port_deref(ref);
23dd4cce 1258 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1259 return -EINVAL;
1260
23dd4cce 1261 msg = &p_ptr->phdr;
b97bf3fd 1262 msg_set_type(msg, TIPC_DIRECT_MSG);
53b94364 1263 msg_set_lookup_scope(msg, 0);
12bae479
AS
1264 msg_set_orignode(msg, tipc_own_addr);
1265 msg_set_origport(msg, ref);
b97bf3fd
PL
1266 msg_set_destnode(msg, dest->node);
1267 msg_set_destport(msg, dest->ref);
741d9eb7 1268 msg_set_hdr_sz(msg, BASIC_H_SIZE);
cb7ce914 1269
b97bf3fd 1270 if (dest->node == tipc_own_addr)
26896904
AS
1271 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1272 total_len);
cb7ce914
AS
1273 else
1274 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
26896904 1275 total_len, dest->node);
cb7ce914
AS
1276 if (likely(res != -ELINKCONG)) {
1277 if (res > 0)
1278 p_ptr->sent++;
b97bf3fd 1279 return res;
cb7ce914 1280 }
b97bf3fd 1281 if (port_unreliable(p_ptr)) {
26896904 1282 return total_len;
b97bf3fd
PL
1283 }
1284 return -ELINKCONG;
1285}
1286
c4307285 1287/**
12bae479 1288 * tipc_send_buf2port - send message buffer to port identity
b97bf3fd
PL
1289 */
1290
12bae479
AS
1291int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1292 struct sk_buff *buf, unsigned int dsz)
b97bf3fd 1293{
23dd4cce 1294 struct tipc_port *p_ptr;
b97bf3fd
PL
1295 struct tipc_msg *msg;
1296 int res;
1297
23dd4cce
AS
1298 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1299 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1300 return -EINVAL;
1301
23dd4cce 1302 msg = &p_ptr->phdr;
b97bf3fd 1303 msg_set_type(msg, TIPC_DIRECT_MSG);
12bae479
AS
1304 msg_set_orignode(msg, tipc_own_addr);
1305 msg_set_origport(msg, ref);
b97bf3fd
PL
1306 msg_set_destnode(msg, dest->node);
1307 msg_set_destport(msg, dest->ref);
741d9eb7
AS
1308 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1309 msg_set_size(msg, BASIC_H_SIZE + dsz);
1310 if (skb_cow(buf, BASIC_H_SIZE))
b97bf3fd
PL
1311 return -ENOMEM;
1312
741d9eb7
AS
1313 skb_push(buf, BASIC_H_SIZE);
1314 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
cb7ce914 1315
b97bf3fd 1316 if (dest->node == tipc_own_addr)
cb7ce914
AS
1317 res = tipc_port_recv_msg(buf);
1318 else
1319 res = tipc_send_buf_fast(buf, dest->node);
1320 if (likely(res != -ELINKCONG)) {
1321 if (res > 0)
1322 p_ptr->sent++;
b97bf3fd 1323 return res;
cb7ce914 1324 }
b97bf3fd
PL
1325 if (port_unreliable(p_ptr))
1326 return dsz;
1327 return -ELINKCONG;
1328}
1329