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