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