]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/tipc/port.c
tipc: replace reference table rwlock with spinlock
[mirror_ubuntu-bionic-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
198d73b8 5 * Copyright (c) 2004-2008, 2010-2013, 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
34af946a 49DEFINE_SPINLOCK(tipc_port_list_lock);
b97bf3fd 50
4323add6 51static LIST_HEAD(ports);
b97bf3fd 52static void port_handle_node_down(unsigned long ref);
23dd4cce
AS
53static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
54static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
b97bf3fd
PL
55static void port_timeout(unsigned long ref);
56
57
872f24db 58static u32 port_peernode(struct tipc_port *p_ptr)
b97bf3fd 59{
23dd4cce 60 return msg_destnode(&p_ptr->phdr);
b97bf3fd
PL
61}
62
872f24db 63static u32 port_peerport(struct tipc_port *p_ptr)
b97bf3fd 64{
23dd4cce 65 return msg_destport(&p_ptr->phdr);
b97bf3fd
PL
66}
67
2c53040f 68/**
f0712e86
AS
69 * tipc_port_peer_msg - verify message was sent by connected port's peer
70 *
71 * Handles cases where the node's network address has changed from
72 * the default of <0.0.0> to its configured setting.
73 */
f0712e86
AS
74int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
75{
76 u32 peernode;
77 u32 orignode;
78
79 if (msg_origport(msg) != port_peerport(p_ptr))
80 return 0;
81
82 orignode = msg_orignode(msg);
83 peernode = port_peernode(p_ptr);
84 return (orignode == peernode) ||
85 (!orignode && (peernode == tipc_own_addr)) ||
86 (!peernode && (orignode == tipc_own_addr));
87}
88
b97bf3fd 89/**
247f0f3c
YX
90 * tipc_port_mcast_xmit - send a multicast message to local and remote
91 * destinations
b97bf3fd 92 */
247f0f3c
YX
93int tipc_port_mcast_xmit(u32 ref, struct tipc_name_seq const *seq,
94 struct iovec const *msg_sect, unsigned int len)
b97bf3fd
PL
95{
96 struct tipc_msg *hdr;
97 struct sk_buff *buf;
98 struct sk_buff *ibuf = NULL;
4584310b 99 struct tipc_port_list dports = {0, NULL, };
23dd4cce 100 struct tipc_port *oport = tipc_port_deref(ref);
b97bf3fd
PL
101 int ext_targets;
102 int res;
103
104 if (unlikely(!oport))
105 return -EINVAL;
106
107 /* Create multicast message */
23dd4cce 108 hdr = &oport->phdr;
b97bf3fd 109 msg_set_type(hdr, TIPC_MCAST_MSG);
53b94364 110 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
7462b9e9
AS
111 msg_set_destport(hdr, 0);
112 msg_set_destnode(hdr, 0);
b97bf3fd
PL
113 msg_set_nametype(hdr, seq->type);
114 msg_set_namelower(hdr, seq->lower);
115 msg_set_nameupper(hdr, seq->upper);
116 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
9446b87a 117 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
b97bf3fd
PL
118 if (unlikely(!buf))
119 return res;
120
121 /* Figure out where to send multicast message */
4323add6
PL
122 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
123 TIPC_NODE_SCOPE, &dports);
c4307285
YH
124
125 /* Send message to destinations (duplicate it only if necessary) */
b97bf3fd
PL
126 if (ext_targets) {
127 if (dports.count != 0) {
128 ibuf = skb_copy(buf, GFP_ATOMIC);
129 if (ibuf == NULL) {
4323add6 130 tipc_port_list_free(&dports);
5f6d9123 131 kfree_skb(buf);
b97bf3fd
PL
132 return -ENOMEM;
133 }
134 }
247f0f3c 135 res = tipc_bclink_xmit(buf);
a016892c 136 if ((res < 0) && (dports.count != 0))
5f6d9123 137 kfree_skb(ibuf);
b97bf3fd
PL
138 } else {
139 ibuf = buf;
140 }
141
142 if (res >= 0) {
143 if (ibuf)
247f0f3c 144 tipc_port_mcast_rcv(ibuf, &dports);
b97bf3fd 145 } else {
4323add6 146 tipc_port_list_free(&dports);
b97bf3fd
PL
147 }
148 return res;
149}
150
151/**
247f0f3c 152 * tipc_port_mcast_rcv - deliver multicast message to all destination ports
c4307285 153 *
b97bf3fd
PL
154 * If there is no port list, perform a lookup to create one
155 */
247f0f3c 156void tipc_port_mcast_rcv(struct sk_buff *buf, struct tipc_port_list *dp)
b97bf3fd 157{
0e65967e 158 struct tipc_msg *msg;
4584310b
PG
159 struct tipc_port_list dports = {0, NULL, };
160 struct tipc_port_list *item = dp;
b97bf3fd
PL
161 int cnt = 0;
162
b97bf3fd
PL
163 msg = buf_msg(buf);
164
165 /* Create destination port list, if one wasn't supplied */
b97bf3fd 166 if (dp == NULL) {
4323add6 167 tipc_nametbl_mc_translate(msg_nametype(msg),
b97bf3fd
PL
168 msg_namelower(msg),
169 msg_nameupper(msg),
170 TIPC_CLUSTER_SCOPE,
171 &dports);
172 item = dp = &dports;
173 }
174
175 /* Deliver a copy of message to each destination port */
b97bf3fd 176 if (dp->count != 0) {
7f47f5c7 177 msg_set_destnode(msg, tipc_own_addr);
b97bf3fd
PL
178 if (dp->count == 1) {
179 msg_set_destport(msg, dp->ports[0]);
247f0f3c 180 tipc_port_rcv(buf);
4323add6 181 tipc_port_list_free(dp);
b97bf3fd
PL
182 return;
183 }
184 for (; cnt < dp->count; cnt++) {
185 int index = cnt % PLSIZE;
186 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
187
188 if (b == NULL) {
2cf8aa19 189 pr_warn("Unable to deliver multicast message(s)\n");
b97bf3fd
PL
190 goto exit;
191 }
a016892c 192 if ((index == 0) && (cnt != 0))
b97bf3fd 193 item = item->next;
0e65967e 194 msg_set_destport(buf_msg(b), item->ports[index]);
247f0f3c 195 tipc_port_rcv(b);
b97bf3fd
PL
196 }
197 }
198exit:
5f6d9123 199 kfree_skb(buf);
4323add6 200 tipc_port_list_free(dp);
b97bf3fd
PL
201}
202
203/**
3c5db8e4 204 * tipc_createport - create a generic TIPC port
c4307285 205 *
0ea52241 206 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
b97bf3fd 207 */
c0fee8ac 208struct tipc_port *tipc_createport(struct sock *sk,
ae8509c4
PG
209 u32 (*dispatcher)(struct tipc_port *,
210 struct sk_buff *),
211 void (*wakeup)(struct tipc_port *),
212 const u32 importance)
b97bf3fd 213{
23dd4cce 214 struct tipc_port *p_ptr;
b97bf3fd
PL
215 struct tipc_msg *msg;
216 u32 ref;
217
0da974f4 218 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
a10bd924 219 if (!p_ptr) {
2cf8aa19 220 pr_warn("Port creation failed, no memory\n");
0ea52241 221 return NULL;
b97bf3fd 222 }
23dd4cce 223 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
b97bf3fd 224 if (!ref) {
2cf8aa19 225 pr_warn("Port creation failed, ref. table exhausted\n");
b97bf3fd 226 kfree(p_ptr);
0ea52241 227 return NULL;
b97bf3fd
PL
228 }
229
c0fee8ac 230 p_ptr->sk = sk;
23dd4cce
AS
231 p_ptr->max_pkt = MAX_PKT_DEFAULT;
232 p_ptr->ref = ref;
b97bf3fd
PL
233 INIT_LIST_HEAD(&p_ptr->wait_list);
234 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
b97bf3fd
PL
235 p_ptr->dispatcher = dispatcher;
236 p_ptr->wakeup = wakeup;
b97bf3fd 237 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
b97bf3fd
PL
238 INIT_LIST_HEAD(&p_ptr->publications);
239 INIT_LIST_HEAD(&p_ptr->port_list);
f21536d1
AS
240
241 /*
242 * Must hold port list lock while initializing message header template
243 * to ensure a change to node's own network address doesn't result
244 * in template containing out-dated network address information
245 */
f21536d1
AS
246 spin_lock_bh(&tipc_port_list_lock);
247 msg = &p_ptr->phdr;
248 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
249 msg_set_origport(msg, ref);
b97bf3fd 250 list_add_tail(&p_ptr->port_list, &ports);
4323add6 251 spin_unlock_bh(&tipc_port_list_lock);
23dd4cce 252 return p_ptr;
b97bf3fd
PL
253}
254
84602761 255int tipc_deleteport(struct tipc_port *p_ptr)
b97bf3fd 256{
1fc54d8f 257 struct sk_buff *buf = NULL;
b97bf3fd 258
84602761 259 tipc_withdraw(p_ptr, 0, NULL);
b97bf3fd 260
84602761
YX
261 spin_lock_bh(p_ptr->lock);
262 tipc_ref_discard(p_ptr->ref);
263 spin_unlock_bh(p_ptr->lock);
b97bf3fd
PL
264
265 k_cancel_timer(&p_ptr->timer);
23dd4cce 266 if (p_ptr->connected) {
b97bf3fd 267 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
4323add6 268 tipc_nodesub_unsubscribe(&p_ptr->subscription);
b97bf3fd 269 }
b97bf3fd 270
4323add6 271 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
272 list_del(&p_ptr->port_list);
273 list_del(&p_ptr->wait_list);
4323add6 274 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
275 k_term_timer(&p_ptr->timer);
276 kfree(p_ptr);
4323add6 277 tipc_net_route_msg(buf);
0e35fd5e 278 return 0;
b97bf3fd
PL
279}
280
23dd4cce 281static int port_unreliable(struct tipc_port *p_ptr)
b97bf3fd 282{
23dd4cce 283 return msg_src_droppable(&p_ptr->phdr);
b97bf3fd
PL
284}
285
286int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
287{
23dd4cce 288 struct tipc_port *p_ptr;
c4307285 289
4323add6 290 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
291 if (!p_ptr)
292 return -EINVAL;
293 *isunreliable = port_unreliable(p_ptr);
4cec72c8 294 tipc_port_unlock(p_ptr);
0e35fd5e 295 return 0;
b97bf3fd
PL
296}
297
298int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
299{
23dd4cce 300 struct tipc_port *p_ptr;
c4307285 301
4323add6 302 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
303 if (!p_ptr)
304 return -EINVAL;
23dd4cce 305 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
4323add6 306 tipc_port_unlock(p_ptr);
0e35fd5e 307 return 0;
b97bf3fd
PL
308}
309
23dd4cce 310static int port_unreturnable(struct tipc_port *p_ptr)
b97bf3fd 311{
23dd4cce 312 return msg_dest_droppable(&p_ptr->phdr);
b97bf3fd
PL
313}
314
315int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
316{
23dd4cce 317 struct tipc_port *p_ptr;
c4307285 318
4323add6 319 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
320 if (!p_ptr)
321 return -EINVAL;
322 *isunrejectable = port_unreturnable(p_ptr);
4cec72c8 323 tipc_port_unlock(p_ptr);
0e35fd5e 324 return 0;
b97bf3fd
PL
325}
326
327int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
328{
23dd4cce 329 struct tipc_port *p_ptr;
c4307285 330
4323add6 331 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
332 if (!p_ptr)
333 return -EINVAL;
23dd4cce 334 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
4323add6 335 tipc_port_unlock(p_ptr);
0e35fd5e 336 return 0;
b97bf3fd
PL
337}
338
c4307285 339/*
e4a0aee4
AS
340 * port_build_proto_msg(): create connection protocol message for port
341 *
342 * On entry the port must be locked and connected.
b97bf3fd 343 */
e4a0aee4
AS
344static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
345 u32 type, u32 ack)
b97bf3fd
PL
346{
347 struct sk_buff *buf;
348 struct tipc_msg *msg;
c4307285 349
741d9eb7 350 buf = tipc_buf_acquire(INT_H_SIZE);
b97bf3fd
PL
351 if (buf) {
352 msg = buf_msg(buf);
e4a0aee4
AS
353 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
354 port_peernode(p_ptr));
355 msg_set_destport(msg, port_peerport(p_ptr));
356 msg_set_origport(msg, p_ptr->ref);
b97bf3fd 357 msg_set_msgcnt(msg, ack);
b97bf3fd
PL
358 }
359 return buf;
360}
361
b97bf3fd
PL
362int tipc_reject_msg(struct sk_buff *buf, u32 err)
363{
364 struct tipc_msg *msg = buf_msg(buf);
365 struct sk_buff *rbuf;
366 struct tipc_msg *rmsg;
367 int hdr_sz;
7dd1bf28 368 u32 imp;
b97bf3fd 369 u32 data_sz = msg_data_sz(msg);
017dac31 370 u32 src_node;
7dd1bf28 371 u32 rmsg_sz;
b97bf3fd
PL
372
373 /* discard rejected message if it shouldn't be returned to sender */
76d12527
AS
374 if (WARN(!msg_isdata(msg),
375 "attempt to reject message with user=%u", msg_user(msg))) {
376 dump_stack();
377 goto exit;
378 }
acc631bf
AS
379 if (msg_errcode(msg) || msg_dest_droppable(msg))
380 goto exit;
b97bf3fd 381
7dd1bf28
AS
382 /*
383 * construct returned message by copying rejected message header and
384 * data (or subset), then updating header fields that need adjusting
385 */
7dd1bf28
AS
386 hdr_sz = msg_hdr_sz(msg);
387 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
388
389 rbuf = tipc_buf_acquire(rmsg_sz);
acc631bf
AS
390 if (rbuf == NULL)
391 goto exit;
392
b97bf3fd 393 rmsg = buf_msg(rbuf);
7dd1bf28
AS
394 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
395
396 if (msg_connected(rmsg)) {
397 imp = msg_importance(rmsg);
398 if (imp < TIPC_CRITICAL_IMPORTANCE)
399 msg_set_importance(rmsg, ++imp);
99c14593 400 }
7dd1bf28
AS
401 msg_set_non_seq(rmsg, 0);
402 msg_set_size(rmsg, rmsg_sz);
403 msg_set_errcode(rmsg, err);
404 msg_set_prevnode(rmsg, tipc_own_addr);
405 msg_swap_words(rmsg, 4, 5);
406 if (!msg_short(rmsg))
407 msg_swap_words(rmsg, 6, 7);
b97bf3fd
PL
408
409 /* send self-abort message when rejecting on a connected port */
410 if (msg_connected(msg)) {
23dd4cce 411 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd
PL
412
413 if (p_ptr) {
dff10e9e
AS
414 struct sk_buff *abuf = NULL;
415
23dd4cce 416 if (p_ptr->connected)
b97bf3fd 417 abuf = port_build_self_abort_msg(p_ptr, err);
4323add6 418 tipc_port_unlock(p_ptr);
dff10e9e 419 tipc_net_route_msg(abuf);
b97bf3fd 420 }
b97bf3fd
PL
421 }
422
acc631bf 423 /* send returned message & dispose of rejected message */
017dac31 424 src_node = msg_prevnode(msg);
630d920d 425 if (in_own_node(src_node))
247f0f3c 426 tipc_port_rcv(rbuf);
017dac31 427 else
247f0f3c 428 tipc_link_xmit(rbuf, src_node, msg_link_selector(rmsg));
acc631bf 429exit:
5f6d9123 430 kfree_skb(buf);
b97bf3fd
PL
431 return data_sz;
432}
433
247f0f3c
YX
434int tipc_port_iovec_reject(struct tipc_port *p_ptr, struct tipc_msg *hdr,
435 struct iovec const *msg_sect, unsigned int len,
436 int err)
b97bf3fd
PL
437{
438 struct sk_buff *buf;
439 int res;
440
9446b87a 441 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
b97bf3fd
PL
442 if (!buf)
443 return res;
444
445 return tipc_reject_msg(buf, err);
446}
447
448static void port_timeout(unsigned long ref)
449{
23dd4cce 450 struct tipc_port *p_ptr = tipc_port_lock(ref);
1fc54d8f 451 struct sk_buff *buf = NULL;
b97bf3fd 452
065fd177
AS
453 if (!p_ptr)
454 return;
455
23dd4cce 456 if (!p_ptr->connected) {
065fd177 457 tipc_port_unlock(p_ptr);
b97bf3fd 458 return;
065fd177 459 }
b97bf3fd
PL
460
461 /* Last probe answered ? */
462 if (p_ptr->probing_state == PROBING) {
463 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
464 } else {
e4a0aee4 465 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
b97bf3fd
PL
466 p_ptr->probing_state = PROBING;
467 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
468 }
4323add6
PL
469 tipc_port_unlock(p_ptr);
470 tipc_net_route_msg(buf);
b97bf3fd
PL
471}
472
473
474static void port_handle_node_down(unsigned long ref)
475{
23dd4cce 476 struct tipc_port *p_ptr = tipc_port_lock(ref);
0e65967e 477 struct sk_buff *buf = NULL;
b97bf3fd
PL
478
479 if (!p_ptr)
480 return;
481 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
4323add6
PL
482 tipc_port_unlock(p_ptr);
483 tipc_net_route_msg(buf);
b97bf3fd
PL
484}
485
486
23dd4cce 487static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 488{
e244a915 489 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
b97bf3fd 490
e244a915
AS
491 if (buf) {
492 struct tipc_msg *msg = buf_msg(buf);
493 msg_swap_words(msg, 4, 5);
494 msg_swap_words(msg, 6, 7);
495 }
496 return buf;
b97bf3fd
PL
497}
498
499
23dd4cce 500static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 501{
e244a915
AS
502 struct sk_buff *buf;
503 struct tipc_msg *msg;
504 u32 imp;
b97bf3fd 505
23dd4cce 506 if (!p_ptr->connected)
1fc54d8f 507 return NULL;
e244a915
AS
508
509 buf = tipc_buf_acquire(BASIC_H_SIZE);
510 if (buf) {
511 msg = buf_msg(buf);
512 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
513 msg_set_hdr_sz(msg, BASIC_H_SIZE);
514 msg_set_size(msg, BASIC_H_SIZE);
515 imp = msg_importance(msg);
516 if (imp < TIPC_CRITICAL_IMPORTANCE)
517 msg_set_importance(msg, ++imp);
518 msg_set_errcode(msg, err);
519 }
520 return buf;
b97bf3fd
PL
521}
522
247f0f3c 523void tipc_port_proto_rcv(struct sk_buff *buf)
b97bf3fd
PL
524{
525 struct tipc_msg *msg = buf_msg(buf);
1c1a551a 526 struct tipc_port *p_ptr;
1fc54d8f 527 struct sk_buff *r_buf = NULL;
1c1a551a
AS
528 u32 destport = msg_destport(msg);
529 int wakeable;
530
531 /* Validate connection */
1c1a551a 532 p_ptr = tipc_port_lock(destport);
f0712e86 533 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
f55b5640
AS
534 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
535 if (r_buf) {
536 msg = buf_msg(r_buf);
537 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
f0712e86 538 BASIC_H_SIZE, msg_orignode(msg));
f55b5640
AS
539 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
540 msg_set_origport(msg, destport);
f0712e86 541 msg_set_destport(msg, msg_origport(msg));
f55b5640 542 }
1c1a551a
AS
543 if (p_ptr)
544 tipc_port_unlock(p_ptr);
b97bf3fd
PL
545 goto exit;
546 }
547
1c1a551a 548 /* Process protocol message sent by peer */
1c1a551a
AS
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
dc1aed37 574static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
b97bf3fd 575{
c4307285 576 struct publication *publ;
dc1aed37 577 int ret;
b97bf3fd
PL
578
579 if (full_id)
dc1aed37
EH
580 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
581 tipc_zone(tipc_own_addr),
582 tipc_cluster(tipc_own_addr),
583 tipc_node(tipc_own_addr), p_ptr->ref);
b97bf3fd 584 else
dc1aed37 585 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
b97bf3fd 586
23dd4cce 587 if (p_ptr->connected) {
c4307285
YH
588 u32 dport = port_peerport(p_ptr);
589 u32 destnode = port_peernode(p_ptr);
590
dc1aed37
EH
591 ret += tipc_snprintf(buf + ret, len - ret,
592 " connected to <%u.%u.%u:%u>",
593 tipc_zone(destnode),
594 tipc_cluster(destnode),
595 tipc_node(destnode), dport);
23dd4cce 596 if (p_ptr->conn_type != 0)
dc1aed37
EH
597 ret += tipc_snprintf(buf + ret, len - ret,
598 " via {%u,%u}", p_ptr->conn_type,
599 p_ptr->conn_instance);
23dd4cce 600 } else if (p_ptr->published) {
dc1aed37 601 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
c4307285 602 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
b97bf3fd 603 if (publ->lower == publ->upper)
dc1aed37
EH
604 ret += tipc_snprintf(buf + ret, len - ret,
605 " {%u,%u}", publ->type,
606 publ->lower);
b97bf3fd 607 else
dc1aed37
EH
608 ret += tipc_snprintf(buf + ret, len - ret,
609 " {%u,%u,%u}", publ->type,
610 publ->lower, publ->upper);
c4307285
YH
611 }
612 }
dc1aed37
EH
613 ret += tipc_snprintf(buf + ret, len - ret, "\n");
614 return ret;
b97bf3fd
PL
615}
616
4323add6 617struct sk_buff *tipc_port_get_ports(void)
b97bf3fd
PL
618{
619 struct sk_buff *buf;
620 struct tlv_desc *rep_tlv;
dc1aed37
EH
621 char *pb;
622 int pb_len;
23dd4cce 623 struct tipc_port *p_ptr;
dc1aed37 624 int str_len = 0;
b97bf3fd 625
dc1aed37 626 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
b97bf3fd
PL
627 if (!buf)
628 return NULL;
629 rep_tlv = (struct tlv_desc *)buf->data;
dc1aed37
EH
630 pb = TLV_DATA(rep_tlv);
631 pb_len = ULTRA_STRING_MAX_LEN;
b97bf3fd 632
4323add6 633 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 634 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 635 spin_lock_bh(p_ptr->lock);
dc1aed37 636 str_len += port_print(p_ptr, pb, pb_len, 0);
23dd4cce 637 spin_unlock_bh(p_ptr->lock);
b97bf3fd 638 }
4323add6 639 spin_unlock_bh(&tipc_port_list_lock);
dc1aed37 640 str_len += 1; /* for "\0" */
b97bf3fd
PL
641 skb_put(buf, TLV_SPACE(str_len));
642 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
643
644 return buf;
645}
646
4323add6 647void tipc_port_reinit(void)
b97bf3fd 648{
23dd4cce 649 struct tipc_port *p_ptr;
b97bf3fd
PL
650 struct tipc_msg *msg;
651
4323add6 652 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 653 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 654 msg = &p_ptr->phdr;
6d4a6672 655 msg_set_prevnode(msg, tipc_own_addr);
b97bf3fd
PL
656 msg_set_orignode(msg, tipc_own_addr);
657 }
4323add6 658 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
659}
660
b97bf3fd
PL
661void tipc_acknowledge(u32 ref, u32 ack)
662{
23dd4cce 663 struct tipc_port *p_ptr;
1fc54d8f 664 struct sk_buff *buf = NULL;
b97bf3fd 665
4323add6 666 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
667 if (!p_ptr)
668 return;
23dd4cce
AS
669 if (p_ptr->connected) {
670 p_ptr->conn_unacked -= ack;
e4a0aee4 671 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
b97bf3fd 672 }
4323add6
PL
673 tipc_port_unlock(p_ptr);
674 tipc_net_route_msg(buf);
b97bf3fd
PL
675}
676
b97bf3fd
PL
677int tipc_portimportance(u32 ref, unsigned int *importance)
678{
23dd4cce 679 struct tipc_port *p_ptr;
c4307285 680
4323add6 681 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
682 if (!p_ptr)
683 return -EINVAL;
23dd4cce 684 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
4cec72c8 685 tipc_port_unlock(p_ptr);
0e35fd5e 686 return 0;
b97bf3fd
PL
687}
688
689int tipc_set_portimportance(u32 ref, unsigned int imp)
690{
23dd4cce 691 struct tipc_port *p_ptr;
b97bf3fd
PL
692
693 if (imp > TIPC_CRITICAL_IMPORTANCE)
694 return -EINVAL;
695
4323add6 696 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
697 if (!p_ptr)
698 return -EINVAL;
23dd4cce 699 msg_set_importance(&p_ptr->phdr, (u32)imp);
4cec72c8 700 tipc_port_unlock(p_ptr);
0e35fd5e 701 return 0;
b97bf3fd
PL
702}
703
704
84602761
YX
705int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
706 struct tipc_name_seq const *seq)
b97bf3fd 707{
b97bf3fd
PL
708 struct publication *publ;
709 u32 key;
b97bf3fd 710
84602761 711 if (p_ptr->connected)
d55b4c63 712 return -EINVAL;
84602761
YX
713 key = p_ptr->ref + p_ptr->pub_count + 1;
714 if (key == p_ptr->ref)
715 return -EADDRINUSE;
d55b4c63 716
4323add6 717 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
23dd4cce 718 scope, p_ptr->ref, key);
b97bf3fd
PL
719 if (publ) {
720 list_add(&publ->pport_list, &p_ptr->publications);
721 p_ptr->pub_count++;
23dd4cce 722 p_ptr->published = 1;
84602761 723 return 0;
b97bf3fd 724 }
84602761 725 return -EINVAL;
b97bf3fd
PL
726}
727
84602761
YX
728int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
729 struct tipc_name_seq const *seq)
b97bf3fd 730{
b97bf3fd
PL
731 struct publication *publ;
732 struct publication *tpubl;
733 int res = -EINVAL;
c4307285 734
b97bf3fd 735 if (!seq) {
c4307285 736 list_for_each_entry_safe(publ, tpubl,
b97bf3fd 737 &p_ptr->publications, pport_list) {
c4307285 738 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 739 publ->ref, publ->key);
b97bf3fd 740 }
0e35fd5e 741 res = 0;
b97bf3fd 742 } else {
c4307285 743 list_for_each_entry_safe(publ, tpubl,
b97bf3fd
PL
744 &p_ptr->publications, pport_list) {
745 if (publ->scope != scope)
746 continue;
747 if (publ->type != seq->type)
748 continue;
749 if (publ->lower != seq->lower)
750 continue;
751 if (publ->upper != seq->upper)
752 break;
c4307285 753 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 754 publ->ref, publ->key);
0e35fd5e 755 res = 0;
b97bf3fd
PL
756 break;
757 }
758 }
759 if (list_empty(&p_ptr->publications))
23dd4cce 760 p_ptr->published = 0;
b97bf3fd
PL
761 return res;
762}
763
247f0f3c 764int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
b97bf3fd 765{
23dd4cce 766 struct tipc_port *p_ptr;
bc879117 767 int res;
b97bf3fd 768
4323add6 769 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
770 if (!p_ptr)
771 return -EINVAL;
247f0f3c 772 res = __tipc_port_connect(ref, p_ptr, peer);
bc879117
PG
773 tipc_port_unlock(p_ptr);
774 return res;
775}
776
777/*
247f0f3c 778 * __tipc_port_connect - connect to a remote peer
bc879117
PG
779 *
780 * Port must be locked.
781 */
247f0f3c 782int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
bc879117
PG
783 struct tipc_portid const *peer)
784{
785 struct tipc_msg *msg;
786 int res = -EINVAL;
787
23dd4cce 788 if (p_ptr->published || p_ptr->connected)
b97bf3fd
PL
789 goto exit;
790 if (!peer->ref)
791 goto exit;
792
23dd4cce 793 msg = &p_ptr->phdr;
b97bf3fd
PL
794 msg_set_destnode(msg, peer->node);
795 msg_set_destport(msg, peer->ref);
b97bf3fd 796 msg_set_type(msg, TIPC_CONN_MSG);
53b94364 797 msg_set_lookup_scope(msg, 0);
08c80e9a 798 msg_set_hdr_sz(msg, SHORT_H_SIZE);
b97bf3fd
PL
799
800 p_ptr->probing_interval = PROBING_INTERVAL;
801 p_ptr->probing_state = CONFIRMED;
23dd4cce 802 p_ptr->connected = 1;
b97bf3fd
PL
803 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
804
0e65967e 805 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
880b005f 806 (void *)(unsigned long)ref,
b97bf3fd 807 (net_ev_handler)port_handle_node_down);
0e35fd5e 808 res = 0;
b97bf3fd 809exit:
23dd4cce 810 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
b97bf3fd
PL
811 return res;
812}
813
bc879117
PG
814/*
815 * __tipc_disconnect - disconnect port from peer
0c3141e9
AS
816 *
817 * Port must be locked.
818 */
247f0f3c 819int __tipc_port_disconnect(struct tipc_port *tp_ptr)
0c3141e9 820{
0c3141e9
AS
821 if (tp_ptr->connected) {
822 tp_ptr->connected = 0;
823 /* let timer expire on it's own to avoid deadlock! */
e3192690 824 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
0cee6bbe 825 return 0;
0c3141e9 826 }
0cee6bbe 827
828 return -ENOTCONN;
0c3141e9
AS
829}
830
b97bf3fd 831/*
247f0f3c 832 * tipc_port_disconnect(): Disconnect port form peer.
b97bf3fd
PL
833 * This is a node local operation.
834 */
247f0f3c 835int tipc_port_disconnect(u32 ref)
b97bf3fd 836{
23dd4cce 837 struct tipc_port *p_ptr;
0c3141e9 838 int res;
b97bf3fd 839
4323add6 840 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
841 if (!p_ptr)
842 return -EINVAL;
247f0f3c 843 res = __tipc_port_disconnect(p_ptr);
4323add6 844 tipc_port_unlock(p_ptr);
b97bf3fd
PL
845 return res;
846}
847
848/*
247f0f3c 849 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
b97bf3fd 850 */
247f0f3c 851int tipc_port_shutdown(u32 ref)
b97bf3fd 852{
23dd4cce 853 struct tipc_port *p_ptr;
1fc54d8f 854 struct sk_buff *buf = NULL;
b97bf3fd 855
4323add6 856 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
857 if (!p_ptr)
858 return -EINVAL;
859
e244a915 860 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
4323add6
PL
861 tipc_port_unlock(p_ptr);
862 tipc_net_route_msg(buf);
247f0f3c 863 return tipc_port_disconnect(ref);
b97bf3fd
PL
864}
865
9b641251 866/**
247f0f3c 867 * tipc_port_rcv - receive message from lower layer and deliver to port user
9b641251 868 */
247f0f3c 869int tipc_port_rcv(struct sk_buff *buf)
9b641251
AS
870{
871 struct tipc_port *p_ptr;
872 struct tipc_msg *msg = buf_msg(buf);
873 u32 destport = msg_destport(msg);
874 u32 dsz = msg_data_sz(msg);
875 u32 err;
876
877 /* forward unresolved named message */
878 if (unlikely(!destport)) {
879 tipc_net_route_msg(buf);
880 return dsz;
881 }
882
883 /* validate destination & pass to port, otherwise reject message */
884 p_ptr = tipc_port_lock(destport);
885 if (likely(p_ptr)) {
9b641251
AS
886 err = p_ptr->dispatcher(p_ptr, buf);
887 tipc_port_unlock(p_ptr);
888 if (likely(!err))
889 return dsz;
890 } else {
891 err = TIPC_ERR_NO_PORT;
892 }
f0712e86 893
9b641251
AS
894 return tipc_reject_msg(buf, err);
895}
896
b97bf3fd 897/*
247f0f3c
YX
898 * tipc_port_iovec_rcv: Concatenate and deliver sectioned
899 * message for this node.
b97bf3fd 900 */
247f0f3c
YX
901static int tipc_port_iovec_rcv(struct tipc_port *sender,
902 struct iovec const *msg_sect,
903 unsigned int len)
b97bf3fd
PL
904{
905 struct sk_buff *buf;
906 int res;
c4307285 907
9446b87a 908 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
b97bf3fd 909 if (likely(buf))
247f0f3c 910 tipc_port_rcv(buf);
b97bf3fd
PL
911 return res;
912}
913
914/**
915 * tipc_send - send message sections on connection
916 */
9446b87a 917int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
b97bf3fd 918{
23dd4cce 919 struct tipc_port *p_ptr;
b97bf3fd
PL
920 u32 destnode;
921 int res;
922
4323add6 923 p_ptr = tipc_port_deref(ref);
23dd4cce 924 if (!p_ptr || !p_ptr->connected)
b97bf3fd
PL
925 return -EINVAL;
926
23dd4cce 927 p_ptr->congested = 1;
4323add6 928 if (!tipc_port_congested(p_ptr)) {
b97bf3fd 929 destnode = port_peernode(p_ptr);
8a55fe74 930 if (likely(!in_own_node(destnode)))
247f0f3c
YX
931 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
932 destnode);
b97bf3fd 933 else
247f0f3c 934 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
b97bf3fd
PL
935
936 if (likely(res != -ELINKCONG)) {
23dd4cce 937 p_ptr->congested = 0;
cb7ce914
AS
938 if (res > 0)
939 p_ptr->sent++;
b97bf3fd
PL
940 return res;
941 }
942 }
943 if (port_unreliable(p_ptr)) {
23dd4cce 944 p_ptr->congested = 0;
9446b87a 945 return len;
b97bf3fd
PL
946 }
947 return -ELINKCONG;
948}
949
b97bf3fd 950/**
12bae479 951 * tipc_send2name - send message sections to port name
b97bf3fd 952 */
12bae479 953int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
9446b87a 954 struct iovec const *msg_sect, unsigned int len)
b97bf3fd 955{
23dd4cce 956 struct tipc_port *p_ptr;
b97bf3fd
PL
957 struct tipc_msg *msg;
958 u32 destnode = domain;
9ccc2eb4 959 u32 destport;
b97bf3fd
PL
960 int res;
961
4323add6 962 p_ptr = tipc_port_deref(ref);
23dd4cce 963 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
964 return -EINVAL;
965
23dd4cce 966 msg = &p_ptr->phdr;
b97bf3fd 967 msg_set_type(msg, TIPC_NAMED_MSG);
741d9eb7 968 msg_set_hdr_sz(msg, NAMED_H_SIZE);
b97bf3fd
PL
969 msg_set_nametype(msg, name->type);
970 msg_set_nameinst(msg, name->instance);
c68ca7b7 971 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
4323add6 972 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
973 msg_set_destnode(msg, destnode);
974 msg_set_destport(msg, destport);
975
bc9f8143 976 if (likely(destport || destnode)) {
8a55fe74 977 if (likely(in_own_node(destnode)))
247f0f3c 978 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
b8f683d1 979 else if (tipc_own_addr)
247f0f3c
YX
980 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
981 destnode);
b8f683d1 982 else
247f0f3c
YX
983 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect,
984 len, TIPC_ERR_NO_NODE);
cb7ce914
AS
985 if (likely(res != -ELINKCONG)) {
986 if (res > 0)
987 p_ptr->sent++;
b97bf3fd 988 return res;
cb7ce914 989 }
b97bf3fd 990 if (port_unreliable(p_ptr)) {
9446b87a 991 return len;
b97bf3fd
PL
992 }
993 return -ELINKCONG;
994 }
247f0f3c
YX
995 return tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
996 TIPC_ERR_NO_NAME);
b97bf3fd
PL
997}
998
999/**
12bae479 1000 * tipc_send2port - send message sections to port identity
b97bf3fd 1001 */
12bae479 1002int tipc_send2port(u32 ref, struct tipc_portid const *dest,
9446b87a 1003 struct iovec const *msg_sect, unsigned int len)
b97bf3fd 1004{
23dd4cce 1005 struct tipc_port *p_ptr;
b97bf3fd
PL
1006 struct tipc_msg *msg;
1007 int res;
1008
4323add6 1009 p_ptr = tipc_port_deref(ref);
23dd4cce 1010 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1011 return -EINVAL;
1012
23dd4cce 1013 msg = &p_ptr->phdr;
b97bf3fd 1014 msg_set_type(msg, TIPC_DIRECT_MSG);
53b94364 1015 msg_set_lookup_scope(msg, 0);
b97bf3fd
PL
1016 msg_set_destnode(msg, dest->node);
1017 msg_set_destport(msg, dest->ref);
741d9eb7 1018 msg_set_hdr_sz(msg, BASIC_H_SIZE);
cb7ce914 1019
8a55fe74 1020 if (in_own_node(dest->node))
247f0f3c 1021 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
b8f683d1 1022 else if (tipc_own_addr)
247f0f3c
YX
1023 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
1024 dest->node);
b8f683d1 1025 else
247f0f3c 1026 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
9446b87a 1027 TIPC_ERR_NO_NODE);
cb7ce914
AS
1028 if (likely(res != -ELINKCONG)) {
1029 if (res > 0)
1030 p_ptr->sent++;
b97bf3fd 1031 return res;
cb7ce914 1032 }
b97bf3fd 1033 if (port_unreliable(p_ptr)) {
9446b87a 1034 return len;
b97bf3fd
PL
1035 }
1036 return -ELINKCONG;
1037}