pinctrl-baytrail: show pin label with the reset of the gpio debug data
[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
254int tipc_deleteport(u32 ref)
255{
23dd4cce 256 struct tipc_port *p_ptr;
1fc54d8f 257 struct sk_buff *buf = NULL;
b97bf3fd 258
1fc54d8f 259 tipc_withdraw(ref, 0, NULL);
4323add6 260 p_ptr = tipc_port_lock(ref);
c4307285 261 if (!p_ptr)
b97bf3fd
PL
262 return -EINVAL;
263
4323add6
PL
264 tipc_ref_discard(ref);
265 tipc_port_unlock(p_ptr);
b97bf3fd
PL
266
267 k_cancel_timer(&p_ptr->timer);
23dd4cce 268 if (p_ptr->connected) {
b97bf3fd 269 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
4323add6 270 tipc_nodesub_unsubscribe(&p_ptr->subscription);
b97bf3fd 271 }
b97bf3fd 272
4323add6 273 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
274 list_del(&p_ptr->port_list);
275 list_del(&p_ptr->wait_list);
4323add6 276 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
277 k_term_timer(&p_ptr->timer);
278 kfree(p_ptr);
4323add6 279 tipc_net_route_msg(buf);
0e35fd5e 280 return 0;
b97bf3fd
PL
281}
282
23dd4cce 283static int port_unreliable(struct tipc_port *p_ptr)
b97bf3fd 284{
23dd4cce 285 return msg_src_droppable(&p_ptr->phdr);
b97bf3fd
PL
286}
287
288int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
289{
23dd4cce 290 struct tipc_port *p_ptr;
c4307285 291
4323add6 292 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
293 if (!p_ptr)
294 return -EINVAL;
295 *isunreliable = port_unreliable(p_ptr);
4cec72c8 296 tipc_port_unlock(p_ptr);
0e35fd5e 297 return 0;
b97bf3fd
PL
298}
299
300int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
301{
23dd4cce 302 struct tipc_port *p_ptr;
c4307285 303
4323add6 304 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
305 if (!p_ptr)
306 return -EINVAL;
23dd4cce 307 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
4323add6 308 tipc_port_unlock(p_ptr);
0e35fd5e 309 return 0;
b97bf3fd
PL
310}
311
23dd4cce 312static int port_unreturnable(struct tipc_port *p_ptr)
b97bf3fd 313{
23dd4cce 314 return msg_dest_droppable(&p_ptr->phdr);
b97bf3fd
PL
315}
316
317int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
318{
23dd4cce 319 struct tipc_port *p_ptr;
c4307285 320
4323add6 321 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
322 if (!p_ptr)
323 return -EINVAL;
324 *isunrejectable = port_unreturnable(p_ptr);
4cec72c8 325 tipc_port_unlock(p_ptr);
0e35fd5e 326 return 0;
b97bf3fd
PL
327}
328
329int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
330{
23dd4cce 331 struct tipc_port *p_ptr;
c4307285 332
4323add6 333 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
334 if (!p_ptr)
335 return -EINVAL;
23dd4cce 336 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
4323add6 337 tipc_port_unlock(p_ptr);
0e35fd5e 338 return 0;
b97bf3fd
PL
339}
340
c4307285 341/*
e4a0aee4
AS
342 * port_build_proto_msg(): create connection protocol message for port
343 *
344 * On entry the port must be locked and connected.
b97bf3fd 345 */
e4a0aee4
AS
346static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
347 u32 type, u32 ack)
b97bf3fd
PL
348{
349 struct sk_buff *buf;
350 struct tipc_msg *msg;
c4307285 351
741d9eb7 352 buf = tipc_buf_acquire(INT_H_SIZE);
b97bf3fd
PL
353 if (buf) {
354 msg = buf_msg(buf);
e4a0aee4
AS
355 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
356 port_peernode(p_ptr));
357 msg_set_destport(msg, port_peerport(p_ptr));
358 msg_set_origport(msg, p_ptr->ref);
b97bf3fd 359 msg_set_msgcnt(msg, ack);
b97bf3fd
PL
360 }
361 return buf;
362}
363
b97bf3fd
PL
364int tipc_reject_msg(struct sk_buff *buf, u32 err)
365{
366 struct tipc_msg *msg = buf_msg(buf);
367 struct sk_buff *rbuf;
368 struct tipc_msg *rmsg;
369 int hdr_sz;
7dd1bf28 370 u32 imp;
b97bf3fd 371 u32 data_sz = msg_data_sz(msg);
017dac31 372 u32 src_node;
7dd1bf28 373 u32 rmsg_sz;
b97bf3fd
PL
374
375 /* discard rejected message if it shouldn't be returned to sender */
76d12527
AS
376 if (WARN(!msg_isdata(msg),
377 "attempt to reject message with user=%u", msg_user(msg))) {
378 dump_stack();
379 goto exit;
380 }
acc631bf
AS
381 if (msg_errcode(msg) || msg_dest_droppable(msg))
382 goto exit;
b97bf3fd 383
7dd1bf28
AS
384 /*
385 * construct returned message by copying rejected message header and
386 * data (or subset), then updating header fields that need adjusting
387 */
7dd1bf28
AS
388 hdr_sz = msg_hdr_sz(msg);
389 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
390
391 rbuf = tipc_buf_acquire(rmsg_sz);
acc631bf
AS
392 if (rbuf == NULL)
393 goto exit;
394
b97bf3fd 395 rmsg = buf_msg(rbuf);
7dd1bf28
AS
396 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
397
398 if (msg_connected(rmsg)) {
399 imp = msg_importance(rmsg);
400 if (imp < TIPC_CRITICAL_IMPORTANCE)
401 msg_set_importance(rmsg, ++imp);
99c14593 402 }
7dd1bf28
AS
403 msg_set_non_seq(rmsg, 0);
404 msg_set_size(rmsg, rmsg_sz);
405 msg_set_errcode(rmsg, err);
406 msg_set_prevnode(rmsg, tipc_own_addr);
407 msg_swap_words(rmsg, 4, 5);
408 if (!msg_short(rmsg))
409 msg_swap_words(rmsg, 6, 7);
b97bf3fd
PL
410
411 /* send self-abort message when rejecting on a connected port */
412 if (msg_connected(msg)) {
23dd4cce 413 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd
PL
414
415 if (p_ptr) {
dff10e9e
AS
416 struct sk_buff *abuf = NULL;
417
23dd4cce 418 if (p_ptr->connected)
b97bf3fd 419 abuf = port_build_self_abort_msg(p_ptr, err);
4323add6 420 tipc_port_unlock(p_ptr);
dff10e9e 421 tipc_net_route_msg(abuf);
b97bf3fd 422 }
b97bf3fd
PL
423 }
424
acc631bf 425 /* send returned message & dispose of rejected message */
017dac31 426 src_node = msg_prevnode(msg);
630d920d 427 if (in_own_node(src_node))
017dac31
AS
428 tipc_port_recv_msg(rbuf);
429 else
430 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
acc631bf 431exit:
5f6d9123 432 kfree_skb(buf);
b97bf3fd
PL
433 return data_sz;
434}
435
23dd4cce 436int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
9446b87a
YX
437 struct iovec const *msg_sect, unsigned int len,
438 int err)
b97bf3fd
PL
439{
440 struct sk_buff *buf;
441 int res;
442
9446b87a 443 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
b97bf3fd
PL
444 if (!buf)
445 return res;
446
447 return tipc_reject_msg(buf, err);
448}
449
450static void port_timeout(unsigned long ref)
451{
23dd4cce 452 struct tipc_port *p_ptr = tipc_port_lock(ref);
1fc54d8f 453 struct sk_buff *buf = NULL;
b97bf3fd 454
065fd177
AS
455 if (!p_ptr)
456 return;
457
23dd4cce 458 if (!p_ptr->connected) {
065fd177 459 tipc_port_unlock(p_ptr);
b97bf3fd 460 return;
065fd177 461 }
b97bf3fd
PL
462
463 /* Last probe answered ? */
464 if (p_ptr->probing_state == PROBING) {
465 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
466 } else {
e4a0aee4 467 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
b97bf3fd
PL
468 p_ptr->probing_state = PROBING;
469 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
470 }
4323add6
PL
471 tipc_port_unlock(p_ptr);
472 tipc_net_route_msg(buf);
b97bf3fd
PL
473}
474
475
476static void port_handle_node_down(unsigned long ref)
477{
23dd4cce 478 struct tipc_port *p_ptr = tipc_port_lock(ref);
0e65967e 479 struct sk_buff *buf = NULL;
b97bf3fd
PL
480
481 if (!p_ptr)
482 return;
483 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
4323add6
PL
484 tipc_port_unlock(p_ptr);
485 tipc_net_route_msg(buf);
b97bf3fd
PL
486}
487
488
23dd4cce 489static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 490{
e244a915 491 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
b97bf3fd 492
e244a915
AS
493 if (buf) {
494 struct tipc_msg *msg = buf_msg(buf);
495 msg_swap_words(msg, 4, 5);
496 msg_swap_words(msg, 6, 7);
497 }
498 return buf;
b97bf3fd
PL
499}
500
501
23dd4cce 502static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 503{
e244a915
AS
504 struct sk_buff *buf;
505 struct tipc_msg *msg;
506 u32 imp;
b97bf3fd 507
23dd4cce 508 if (!p_ptr->connected)
1fc54d8f 509 return NULL;
e244a915
AS
510
511 buf = tipc_buf_acquire(BASIC_H_SIZE);
512 if (buf) {
513 msg = buf_msg(buf);
514 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
515 msg_set_hdr_sz(msg, BASIC_H_SIZE);
516 msg_set_size(msg, BASIC_H_SIZE);
517 imp = msg_importance(msg);
518 if (imp < TIPC_CRITICAL_IMPORTANCE)
519 msg_set_importance(msg, ++imp);
520 msg_set_errcode(msg, err);
521 }
522 return buf;
b97bf3fd
PL
523}
524
4323add6 525void tipc_port_recv_proto_msg(struct sk_buff *buf)
b97bf3fd
PL
526{
527 struct tipc_msg *msg = buf_msg(buf);
1c1a551a 528 struct tipc_port *p_ptr;
1fc54d8f 529 struct sk_buff *r_buf = NULL;
1c1a551a
AS
530 u32 destport = msg_destport(msg);
531 int wakeable;
532
533 /* Validate connection */
1c1a551a 534 p_ptr = tipc_port_lock(destport);
f0712e86 535 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
f55b5640
AS
536 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
537 if (r_buf) {
538 msg = buf_msg(r_buf);
539 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
f0712e86 540 BASIC_H_SIZE, msg_orignode(msg));
f55b5640
AS
541 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
542 msg_set_origport(msg, destport);
f0712e86 543 msg_set_destport(msg, msg_origport(msg));
f55b5640 544 }
1c1a551a
AS
545 if (p_ptr)
546 tipc_port_unlock(p_ptr);
b97bf3fd
PL
547 goto exit;
548 }
549
1c1a551a 550 /* Process protocol message sent by peer */
1c1a551a
AS
551 switch (msg_type(msg)) {
552 case CONN_ACK:
553 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
554 p_ptr->wakeup;
555 p_ptr->acked += msg_msgcnt(msg);
556 if (!tipc_port_congested(p_ptr)) {
557 p_ptr->congested = 0;
558 if (wakeable)
559 p_ptr->wakeup(p_ptr);
560 }
561 break;
562 case CONN_PROBE:
e4a0aee4 563 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
1c1a551a
AS
564 break;
565 default:
566 /* CONN_PROBE_REPLY or unrecognized - no action required */
567 break;
b97bf3fd
PL
568 }
569 p_ptr->probing_state = CONFIRMED;
1c1a551a 570 tipc_port_unlock(p_ptr);
b97bf3fd 571exit:
4323add6 572 tipc_net_route_msg(r_buf);
5f6d9123 573 kfree_skb(buf);
b97bf3fd
PL
574}
575
dc1aed37 576static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
b97bf3fd 577{
c4307285 578 struct publication *publ;
dc1aed37 579 int ret;
b97bf3fd
PL
580
581 if (full_id)
dc1aed37
EH
582 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
583 tipc_zone(tipc_own_addr),
584 tipc_cluster(tipc_own_addr),
585 tipc_node(tipc_own_addr), p_ptr->ref);
b97bf3fd 586 else
dc1aed37 587 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
b97bf3fd 588
23dd4cce 589 if (p_ptr->connected) {
c4307285
YH
590 u32 dport = port_peerport(p_ptr);
591 u32 destnode = port_peernode(p_ptr);
592
dc1aed37
EH
593 ret += tipc_snprintf(buf + ret, len - ret,
594 " connected to <%u.%u.%u:%u>",
595 tipc_zone(destnode),
596 tipc_cluster(destnode),
597 tipc_node(destnode), dport);
23dd4cce 598 if (p_ptr->conn_type != 0)
dc1aed37
EH
599 ret += tipc_snprintf(buf + ret, len - ret,
600 " via {%u,%u}", p_ptr->conn_type,
601 p_ptr->conn_instance);
23dd4cce 602 } else if (p_ptr->published) {
dc1aed37 603 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
c4307285 604 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
b97bf3fd 605 if (publ->lower == publ->upper)
dc1aed37
EH
606 ret += tipc_snprintf(buf + ret, len - ret,
607 " {%u,%u}", publ->type,
608 publ->lower);
b97bf3fd 609 else
dc1aed37
EH
610 ret += tipc_snprintf(buf + ret, len - ret,
611 " {%u,%u,%u}", publ->type,
612 publ->lower, publ->upper);
c4307285
YH
613 }
614 }
dc1aed37
EH
615 ret += tipc_snprintf(buf + ret, len - ret, "\n");
616 return ret;
b97bf3fd
PL
617}
618
4323add6 619struct sk_buff *tipc_port_get_ports(void)
b97bf3fd
PL
620{
621 struct sk_buff *buf;
622 struct tlv_desc *rep_tlv;
dc1aed37
EH
623 char *pb;
624 int pb_len;
23dd4cce 625 struct tipc_port *p_ptr;
dc1aed37 626 int str_len = 0;
b97bf3fd 627
dc1aed37 628 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
b97bf3fd
PL
629 if (!buf)
630 return NULL;
631 rep_tlv = (struct tlv_desc *)buf->data;
dc1aed37
EH
632 pb = TLV_DATA(rep_tlv);
633 pb_len = ULTRA_STRING_MAX_LEN;
b97bf3fd 634
4323add6 635 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 636 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 637 spin_lock_bh(p_ptr->lock);
dc1aed37 638 str_len += port_print(p_ptr, pb, pb_len, 0);
23dd4cce 639 spin_unlock_bh(p_ptr->lock);
b97bf3fd 640 }
4323add6 641 spin_unlock_bh(&tipc_port_list_lock);
dc1aed37 642 str_len += 1; /* for "\0" */
b97bf3fd
PL
643 skb_put(buf, TLV_SPACE(str_len));
644 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
645
646 return buf;
647}
648
4323add6 649void tipc_port_reinit(void)
b97bf3fd 650{
23dd4cce 651 struct tipc_port *p_ptr;
b97bf3fd
PL
652 struct tipc_msg *msg;
653
4323add6 654 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 655 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 656 msg = &p_ptr->phdr;
6d4a6672 657 msg_set_prevnode(msg, tipc_own_addr);
b97bf3fd
PL
658 msg_set_orignode(msg, tipc_own_addr);
659 }
4323add6 660 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
661}
662
b97bf3fd
PL
663void tipc_acknowledge(u32 ref, u32 ack)
664{
23dd4cce 665 struct tipc_port *p_ptr;
1fc54d8f 666 struct sk_buff *buf = NULL;
b97bf3fd 667
4323add6 668 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
669 if (!p_ptr)
670 return;
23dd4cce
AS
671 if (p_ptr->connected) {
672 p_ptr->conn_unacked -= ack;
e4a0aee4 673 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
b97bf3fd 674 }
4323add6
PL
675 tipc_port_unlock(p_ptr);
676 tipc_net_route_msg(buf);
b97bf3fd
PL
677}
678
b97bf3fd
PL
679int tipc_portimportance(u32 ref, unsigned int *importance)
680{
23dd4cce 681 struct tipc_port *p_ptr;
c4307285 682
4323add6 683 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
684 if (!p_ptr)
685 return -EINVAL;
23dd4cce 686 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
4cec72c8 687 tipc_port_unlock(p_ptr);
0e35fd5e 688 return 0;
b97bf3fd
PL
689}
690
691int tipc_set_portimportance(u32 ref, unsigned int imp)
692{
23dd4cce 693 struct tipc_port *p_ptr;
b97bf3fd
PL
694
695 if (imp > TIPC_CRITICAL_IMPORTANCE)
696 return -EINVAL;
697
4323add6 698 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
699 if (!p_ptr)
700 return -EINVAL;
23dd4cce 701 msg_set_importance(&p_ptr->phdr, (u32)imp);
4cec72c8 702 tipc_port_unlock(p_ptr);
0e35fd5e 703 return 0;
b97bf3fd
PL
704}
705
706
707int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
708{
23dd4cce 709 struct tipc_port *p_ptr;
b97bf3fd
PL
710 struct publication *publ;
711 u32 key;
712 int res = -EINVAL;
713
4323add6 714 p_ptr = tipc_port_lock(ref);
d55b4c63
AB
715 if (!p_ptr)
716 return -EINVAL;
717
23dd4cce 718 if (p_ptr->connected)
b97bf3fd 719 goto exit;
b97bf3fd
PL
720 key = ref + p_ptr->pub_count + 1;
721 if (key == ref) {
722 res = -EADDRINUSE;
723 goto exit;
724 }
4323add6 725 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
23dd4cce 726 scope, p_ptr->ref, key);
b97bf3fd
PL
727 if (publ) {
728 list_add(&publ->pport_list, &p_ptr->publications);
729 p_ptr->pub_count++;
23dd4cce 730 p_ptr->published = 1;
0e35fd5e 731 res = 0;
b97bf3fd
PL
732 }
733exit:
4323add6 734 tipc_port_unlock(p_ptr);
b97bf3fd
PL
735 return res;
736}
737
738int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
739{
23dd4cce 740 struct tipc_port *p_ptr;
b97bf3fd
PL
741 struct publication *publ;
742 struct publication *tpubl;
743 int res = -EINVAL;
c4307285 744
4323add6 745 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
746 if (!p_ptr)
747 return -EINVAL;
b97bf3fd 748 if (!seq) {
c4307285 749 list_for_each_entry_safe(publ, tpubl,
b97bf3fd 750 &p_ptr->publications, pport_list) {
c4307285 751 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 752 publ->ref, publ->key);
b97bf3fd 753 }
0e35fd5e 754 res = 0;
b97bf3fd 755 } else {
c4307285 756 list_for_each_entry_safe(publ, tpubl,
b97bf3fd
PL
757 &p_ptr->publications, pport_list) {
758 if (publ->scope != scope)
759 continue;
760 if (publ->type != seq->type)
761 continue;
762 if (publ->lower != seq->lower)
763 continue;
764 if (publ->upper != seq->upper)
765 break;
c4307285 766 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 767 publ->ref, publ->key);
0e35fd5e 768 res = 0;
b97bf3fd
PL
769 break;
770 }
771 }
772 if (list_empty(&p_ptr->publications))
23dd4cce 773 p_ptr->published = 0;
4323add6 774 tipc_port_unlock(p_ptr);
b97bf3fd
PL
775 return res;
776}
777
bc879117 778int tipc_connect(u32 ref, struct tipc_portid const *peer)
b97bf3fd 779{
23dd4cce 780 struct tipc_port *p_ptr;
bc879117 781 int res;
b97bf3fd 782
4323add6 783 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
784 if (!p_ptr)
785 return -EINVAL;
bc879117
PG
786 res = __tipc_connect(ref, p_ptr, peer);
787 tipc_port_unlock(p_ptr);
788 return res;
789}
790
791/*
792 * __tipc_connect - connect to a remote peer
793 *
794 * Port must be locked.
795 */
796int __tipc_connect(u32 ref, struct tipc_port *p_ptr,
797 struct tipc_portid const *peer)
798{
799 struct tipc_msg *msg;
800 int res = -EINVAL;
801
23dd4cce 802 if (p_ptr->published || p_ptr->connected)
b97bf3fd
PL
803 goto exit;
804 if (!peer->ref)
805 goto exit;
806
23dd4cce 807 msg = &p_ptr->phdr;
b97bf3fd
PL
808 msg_set_destnode(msg, peer->node);
809 msg_set_destport(msg, peer->ref);
b97bf3fd 810 msg_set_type(msg, TIPC_CONN_MSG);
53b94364 811 msg_set_lookup_scope(msg, 0);
08c80e9a 812 msg_set_hdr_sz(msg, SHORT_H_SIZE);
b97bf3fd
PL
813
814 p_ptr->probing_interval = PROBING_INTERVAL;
815 p_ptr->probing_state = CONFIRMED;
23dd4cce 816 p_ptr->connected = 1;
b97bf3fd
PL
817 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
818
0e65967e 819 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
880b005f 820 (void *)(unsigned long)ref,
b97bf3fd 821 (net_ev_handler)port_handle_node_down);
0e35fd5e 822 res = 0;
b97bf3fd 823exit:
23dd4cce 824 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
b97bf3fd
PL
825 return res;
826}
827
bc879117
PG
828/*
829 * __tipc_disconnect - disconnect port from peer
0c3141e9
AS
830 *
831 * Port must be locked.
832 */
bc879117 833int __tipc_disconnect(struct tipc_port *tp_ptr)
0c3141e9
AS
834{
835 int res;
836
837 if (tp_ptr->connected) {
838 tp_ptr->connected = 0;
839 /* let timer expire on it's own to avoid deadlock! */
e3192690 840 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
0e35fd5e 841 res = 0;
0c3141e9
AS
842 } else {
843 res = -ENOTCONN;
844 }
845 return res;
846}
847
b97bf3fd
PL
848/*
849 * tipc_disconnect(): Disconnect port form peer.
850 * This is a node local operation.
851 */
b97bf3fd
PL
852int tipc_disconnect(u32 ref)
853{
23dd4cce 854 struct tipc_port *p_ptr;
0c3141e9 855 int res;
b97bf3fd 856
4323add6 857 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
858 if (!p_ptr)
859 return -EINVAL;
bc879117 860 res = __tipc_disconnect(p_ptr);
4323add6 861 tipc_port_unlock(p_ptr);
b97bf3fd
PL
862 return res;
863}
864
865/*
866 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
867 */
868int tipc_shutdown(u32 ref)
869{
23dd4cce 870 struct tipc_port *p_ptr;
1fc54d8f 871 struct sk_buff *buf = NULL;
b97bf3fd 872
4323add6 873 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
874 if (!p_ptr)
875 return -EINVAL;
876
e244a915 877 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
4323add6
PL
878 tipc_port_unlock(p_ptr);
879 tipc_net_route_msg(buf);
b97bf3fd
PL
880 return tipc_disconnect(ref);
881}
882
9b641251
AS
883/**
884 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
885 */
9b641251
AS
886int tipc_port_recv_msg(struct sk_buff *buf)
887{
888 struct tipc_port *p_ptr;
889 struct tipc_msg *msg = buf_msg(buf);
890 u32 destport = msg_destport(msg);
891 u32 dsz = msg_data_sz(msg);
892 u32 err;
893
894 /* forward unresolved named message */
895 if (unlikely(!destport)) {
896 tipc_net_route_msg(buf);
897 return dsz;
898 }
899
900 /* validate destination & pass to port, otherwise reject message */
901 p_ptr = tipc_port_lock(destport);
902 if (likely(p_ptr)) {
9b641251
AS
903 err = p_ptr->dispatcher(p_ptr, buf);
904 tipc_port_unlock(p_ptr);
905 if (likely(!err))
906 return dsz;
907 } else {
908 err = TIPC_ERR_NO_PORT;
909 }
f0712e86 910
9b641251
AS
911 return tipc_reject_msg(buf, err);
912}
913
b97bf3fd 914/*
4323add6 915 * tipc_port_recv_sections(): Concatenate and deliver sectioned
b97bf3fd
PL
916 * message for this node.
917 */
9446b87a 918static int tipc_port_recv_sections(struct tipc_port *sender,
26896904 919 struct iovec const *msg_sect,
9446b87a 920 unsigned int len)
b97bf3fd
PL
921{
922 struct sk_buff *buf;
923 int res;
c4307285 924
9446b87a 925 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
b97bf3fd 926 if (likely(buf))
4323add6 927 tipc_port_recv_msg(buf);
b97bf3fd
PL
928 return res;
929}
930
931/**
932 * tipc_send - send message sections on connection
933 */
9446b87a 934int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
b97bf3fd 935{
23dd4cce 936 struct tipc_port *p_ptr;
b97bf3fd
PL
937 u32 destnode;
938 int res;
939
4323add6 940 p_ptr = tipc_port_deref(ref);
23dd4cce 941 if (!p_ptr || !p_ptr->connected)
b97bf3fd
PL
942 return -EINVAL;
943
23dd4cce 944 p_ptr->congested = 1;
4323add6 945 if (!tipc_port_congested(p_ptr)) {
b97bf3fd 946 destnode = port_peernode(p_ptr);
8a55fe74 947 if (likely(!in_own_node(destnode)))
9446b87a
YX
948 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
949 len, destnode);
b97bf3fd 950 else
9446b87a 951 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
b97bf3fd
PL
952
953 if (likely(res != -ELINKCONG)) {
23dd4cce 954 p_ptr->congested = 0;
cb7ce914
AS
955 if (res > 0)
956 p_ptr->sent++;
b97bf3fd
PL
957 return res;
958 }
959 }
960 if (port_unreliable(p_ptr)) {
23dd4cce 961 p_ptr->congested = 0;
9446b87a 962 return len;
b97bf3fd
PL
963 }
964 return -ELINKCONG;
965}
966
b97bf3fd 967/**
12bae479 968 * tipc_send2name - send message sections to port name
b97bf3fd 969 */
12bae479 970int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
9446b87a 971 struct iovec const *msg_sect, unsigned int len)
b97bf3fd 972{
23dd4cce 973 struct tipc_port *p_ptr;
b97bf3fd
PL
974 struct tipc_msg *msg;
975 u32 destnode = domain;
9ccc2eb4 976 u32 destport;
b97bf3fd
PL
977 int res;
978
4323add6 979 p_ptr = tipc_port_deref(ref);
23dd4cce 980 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
981 return -EINVAL;
982
23dd4cce 983 msg = &p_ptr->phdr;
b97bf3fd 984 msg_set_type(msg, TIPC_NAMED_MSG);
741d9eb7 985 msg_set_hdr_sz(msg, NAMED_H_SIZE);
b97bf3fd
PL
986 msg_set_nametype(msg, name->type);
987 msg_set_nameinst(msg, name->instance);
c68ca7b7 988 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
4323add6 989 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
990 msg_set_destnode(msg, destnode);
991 msg_set_destport(msg, destport);
992
bc9f8143 993 if (likely(destport || destnode)) {
8a55fe74 994 if (likely(in_own_node(destnode)))
9446b87a 995 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
b8f683d1 996 else if (tipc_own_addr)
cb7ce914 997 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
9446b87a 998 len, destnode);
b8f683d1
AS
999 else
1000 res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
9446b87a 1001 len, TIPC_ERR_NO_NODE);
cb7ce914
AS
1002 if (likely(res != -ELINKCONG)) {
1003 if (res > 0)
1004 p_ptr->sent++;
b97bf3fd 1005 return res;
cb7ce914 1006 }
b97bf3fd 1007 if (port_unreliable(p_ptr)) {
9446b87a 1008 return len;
b97bf3fd
PL
1009 }
1010 return -ELINKCONG;
1011 }
9446b87a
YX
1012 return tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
1013 TIPC_ERR_NO_NAME);
b97bf3fd
PL
1014}
1015
1016/**
12bae479 1017 * tipc_send2port - send message sections to port identity
b97bf3fd 1018 */
12bae479 1019int tipc_send2port(u32 ref, struct tipc_portid const *dest,
9446b87a 1020 struct iovec const *msg_sect, unsigned int len)
b97bf3fd 1021{
23dd4cce 1022 struct tipc_port *p_ptr;
b97bf3fd
PL
1023 struct tipc_msg *msg;
1024 int res;
1025
4323add6 1026 p_ptr = tipc_port_deref(ref);
23dd4cce 1027 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1028 return -EINVAL;
1029
23dd4cce 1030 msg = &p_ptr->phdr;
b97bf3fd 1031 msg_set_type(msg, TIPC_DIRECT_MSG);
53b94364 1032 msg_set_lookup_scope(msg, 0);
b97bf3fd
PL
1033 msg_set_destnode(msg, dest->node);
1034 msg_set_destport(msg, dest->ref);
741d9eb7 1035 msg_set_hdr_sz(msg, BASIC_H_SIZE);
cb7ce914 1036
8a55fe74 1037 if (in_own_node(dest->node))
9446b87a 1038 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
b8f683d1 1039 else if (tipc_own_addr)
9446b87a
YX
1040 res = tipc_link_send_sections_fast(p_ptr, msg_sect, len,
1041 dest->node);
b8f683d1 1042 else
9446b87a
YX
1043 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
1044 TIPC_ERR_NO_NODE);
cb7ce914
AS
1045 if (likely(res != -ELINKCONG)) {
1046 if (res > 0)
1047 p_ptr->sent++;
b97bf3fd 1048 return res;
cb7ce914 1049 }
b97bf3fd 1050 if (port_unreliable(p_ptr)) {
9446b87a 1051 return len;
b97bf3fd
PL
1052 }
1053 return -ELINKCONG;
1054}
This page took 0.788676 seconds and 5 git commands to generate.