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