tipc: Optimize creation of FIN messages
[GitHub/mt8127/android_kernel_alcatel_ttab.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
23dd4cce 5 * Copyright (c) 2004-2008, 2010-2011, 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
e3ec9c7d
AS
49static struct sk_buff *msg_queue_head;
50static struct sk_buff *msg_queue_tail;
b97bf3fd 51
34af946a
IM
52DEFINE_SPINLOCK(tipc_port_list_lock);
53static DEFINE_SPINLOCK(queue_lock);
b97bf3fd 54
4323add6 55static LIST_HEAD(ports);
b97bf3fd 56static void port_handle_node_down(unsigned long ref);
23dd4cce
AS
57static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
58static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
b97bf3fd
PL
59static void port_timeout(unsigned long ref);
60
61
23dd4cce 62static u32 port_peernode(struct tipc_port *p_ptr)
b97bf3fd 63{
23dd4cce 64 return msg_destnode(&p_ptr->phdr);
b97bf3fd
PL
65}
66
23dd4cce 67static u32 port_peerport(struct tipc_port *p_ptr)
b97bf3fd 68{
23dd4cce 69 return msg_destport(&p_ptr->phdr);
b97bf3fd
PL
70}
71
b97bf3fd
PL
72/**
73 * tipc_multicast - send a multicast message to local and remote destinations
74 */
75
38f232ea 76int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
26896904
AS
77 u32 num_sect, struct iovec const *msg_sect,
78 unsigned int total_len)
b97bf3fd
PL
79{
80 struct tipc_msg *hdr;
81 struct sk_buff *buf;
82 struct sk_buff *ibuf = NULL;
83 struct port_list dports = {0, NULL, };
23dd4cce 84 struct tipc_port *oport = tipc_port_deref(ref);
b97bf3fd
PL
85 int ext_targets;
86 int res;
87
88 if (unlikely(!oport))
89 return -EINVAL;
90
91 /* Create multicast message */
92
23dd4cce 93 hdr = &oport->phdr;
b97bf3fd 94 msg_set_type(hdr, TIPC_MCAST_MSG);
53b94364 95 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
7462b9e9
AS
96 msg_set_destport(hdr, 0);
97 msg_set_destnode(hdr, 0);
b97bf3fd
PL
98 msg_set_nametype(hdr, seq->type);
99 msg_set_namelower(hdr, seq->lower);
100 msg_set_nameupper(hdr, seq->upper);
101 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
26896904 102 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
b97bf3fd
PL
103 !oport->user_port, &buf);
104 if (unlikely(!buf))
105 return res;
106
107 /* Figure out where to send multicast message */
108
4323add6
PL
109 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
110 TIPC_NODE_SCOPE, &dports);
c4307285
YH
111
112 /* Send message to destinations (duplicate it only if necessary) */
b97bf3fd
PL
113
114 if (ext_targets) {
115 if (dports.count != 0) {
116 ibuf = skb_copy(buf, GFP_ATOMIC);
117 if (ibuf == NULL) {
4323add6 118 tipc_port_list_free(&dports);
b97bf3fd
PL
119 buf_discard(buf);
120 return -ENOMEM;
121 }
122 }
4323add6 123 res = tipc_bclink_send_msg(buf);
a016892c 124 if ((res < 0) && (dports.count != 0))
b97bf3fd 125 buf_discard(ibuf);
b97bf3fd
PL
126 } else {
127 ibuf = buf;
128 }
129
130 if (res >= 0) {
131 if (ibuf)
4323add6 132 tipc_port_recv_mcast(ibuf, &dports);
b97bf3fd 133 } else {
4323add6 134 tipc_port_list_free(&dports);
b97bf3fd
PL
135 }
136 return res;
137}
138
139/**
4323add6 140 * tipc_port_recv_mcast - deliver multicast message to all destination ports
c4307285 141 *
b97bf3fd
PL
142 * If there is no port list, perform a lookup to create one
143 */
144
4323add6 145void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
b97bf3fd 146{
0e65967e 147 struct tipc_msg *msg;
b97bf3fd
PL
148 struct port_list dports = {0, NULL, };
149 struct port_list *item = dp;
150 int cnt = 0;
151
b97bf3fd
PL
152 msg = buf_msg(buf);
153
154 /* Create destination port list, if one wasn't supplied */
155
156 if (dp == NULL) {
4323add6 157 tipc_nametbl_mc_translate(msg_nametype(msg),
b97bf3fd
PL
158 msg_namelower(msg),
159 msg_nameupper(msg),
160 TIPC_CLUSTER_SCOPE,
161 &dports);
162 item = dp = &dports;
163 }
164
165 /* Deliver a copy of message to each destination port */
166
167 if (dp->count != 0) {
7f47f5c7 168 msg_set_destnode(msg, tipc_own_addr);
b97bf3fd
PL
169 if (dp->count == 1) {
170 msg_set_destport(msg, dp->ports[0]);
4323add6
PL
171 tipc_port_recv_msg(buf);
172 tipc_port_list_free(dp);
b97bf3fd
PL
173 return;
174 }
175 for (; cnt < dp->count; cnt++) {
176 int index = cnt % PLSIZE;
177 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
178
179 if (b == NULL) {
a10bd924 180 warn("Unable to deliver multicast message(s)\n");
b97bf3fd
PL
181 goto exit;
182 }
a016892c 183 if ((index == 0) && (cnt != 0))
b97bf3fd 184 item = item->next;
0e65967e 185 msg_set_destport(buf_msg(b), item->ports[index]);
4323add6 186 tipc_port_recv_msg(b);
b97bf3fd
PL
187 }
188 }
189exit:
190 buf_discard(buf);
4323add6 191 tipc_port_list_free(dp);
b97bf3fd
PL
192}
193
194/**
7ef43eba 195 * tipc_createport_raw - create a generic TIPC port
c4307285 196 *
0ea52241 197 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
b97bf3fd
PL
198 */
199
0ea52241 200struct tipc_port *tipc_createport_raw(void *usr_handle,
b97bf3fd
PL
201 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
202 void (*wakeup)(struct tipc_port *),
0ea52241 203 const u32 importance)
b97bf3fd 204{
23dd4cce 205 struct tipc_port *p_ptr;
b97bf3fd
PL
206 struct tipc_msg *msg;
207 u32 ref;
208
0da974f4 209 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
a10bd924
AS
210 if (!p_ptr) {
211 warn("Port creation failed, no memory\n");
0ea52241 212 return NULL;
b97bf3fd 213 }
23dd4cce 214 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
b97bf3fd 215 if (!ref) {
a10bd924 216 warn("Port creation failed, reference table exhausted\n");
b97bf3fd 217 kfree(p_ptr);
0ea52241 218 return NULL;
b97bf3fd
PL
219 }
220
23dd4cce
AS
221 p_ptr->usr_handle = usr_handle;
222 p_ptr->max_pkt = MAX_PKT_DEFAULT;
223 p_ptr->ref = ref;
224 msg = &p_ptr->phdr;
741d9eb7 225 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
b97bf3fd 226 msg_set_origport(msg, ref);
b97bf3fd
PL
227 INIT_LIST_HEAD(&p_ptr->wait_list);
228 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
b97bf3fd
PL
229 p_ptr->dispatcher = dispatcher;
230 p_ptr->wakeup = wakeup;
1fc54d8f 231 p_ptr->user_port = NULL;
b97bf3fd 232 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
4323add6 233 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
234 INIT_LIST_HEAD(&p_ptr->publications);
235 INIT_LIST_HEAD(&p_ptr->port_list);
236 list_add_tail(&p_ptr->port_list, &ports);
4323add6 237 spin_unlock_bh(&tipc_port_list_lock);
23dd4cce 238 return p_ptr;
b97bf3fd
PL
239}
240
241int tipc_deleteport(u32 ref)
242{
23dd4cce 243 struct tipc_port *p_ptr;
1fc54d8f 244 struct sk_buff *buf = NULL;
b97bf3fd 245
1fc54d8f 246 tipc_withdraw(ref, 0, NULL);
4323add6 247 p_ptr = tipc_port_lock(ref);
c4307285 248 if (!p_ptr)
b97bf3fd
PL
249 return -EINVAL;
250
4323add6
PL
251 tipc_ref_discard(ref);
252 tipc_port_unlock(p_ptr);
b97bf3fd
PL
253
254 k_cancel_timer(&p_ptr->timer);
23dd4cce 255 if (p_ptr->connected) {
b97bf3fd 256 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
4323add6 257 tipc_nodesub_unsubscribe(&p_ptr->subscription);
b97bf3fd 258 }
e83504f7 259 kfree(p_ptr->user_port);
b97bf3fd 260
4323add6 261 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
262 list_del(&p_ptr->port_list);
263 list_del(&p_ptr->wait_list);
4323add6 264 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
265 k_term_timer(&p_ptr->timer);
266 kfree(p_ptr);
4323add6 267 tipc_net_route_msg(buf);
0e35fd5e 268 return 0;
b97bf3fd
PL
269}
270
23dd4cce 271static int port_unreliable(struct tipc_port *p_ptr)
b97bf3fd 272{
23dd4cce 273 return msg_src_droppable(&p_ptr->phdr);
b97bf3fd
PL
274}
275
276int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
277{
23dd4cce 278 struct tipc_port *p_ptr;
c4307285 279
4323add6 280 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
281 if (!p_ptr)
282 return -EINVAL;
283 *isunreliable = port_unreliable(p_ptr);
4cec72c8 284 tipc_port_unlock(p_ptr);
0e35fd5e 285 return 0;
b97bf3fd
PL
286}
287
288int tipc_set_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;
23dd4cce 295 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
4323add6 296 tipc_port_unlock(p_ptr);
0e35fd5e 297 return 0;
b97bf3fd
PL
298}
299
23dd4cce 300static int port_unreturnable(struct tipc_port *p_ptr)
b97bf3fd 301{
23dd4cce 302 return msg_dest_droppable(&p_ptr->phdr);
b97bf3fd
PL
303}
304
305int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
306{
23dd4cce 307 struct tipc_port *p_ptr;
c4307285 308
4323add6 309 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
310 if (!p_ptr)
311 return -EINVAL;
312 *isunrejectable = port_unreturnable(p_ptr);
4cec72c8 313 tipc_port_unlock(p_ptr);
0e35fd5e 314 return 0;
b97bf3fd
PL
315}
316
317int tipc_set_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;
23dd4cce 324 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
4323add6 325 tipc_port_unlock(p_ptr);
0e35fd5e 326 return 0;
b97bf3fd
PL
327}
328
c4307285
YH
329/*
330 * port_build_proto_msg(): build a port level protocol
331 * or a connection abortion message. Called with
b97bf3fd
PL
332 * tipc_port lock on.
333 */
334static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
335 u32 origport, u32 orignode,
c4307285 336 u32 usr, u32 type, u32 err,
741de3e9 337 u32 ack)
b97bf3fd
PL
338{
339 struct sk_buff *buf;
340 struct tipc_msg *msg;
c4307285 341
741d9eb7 342 buf = tipc_buf_acquire(INT_H_SIZE);
b97bf3fd
PL
343 if (buf) {
344 msg = buf_msg(buf);
741d9eb7 345 tipc_msg_init(msg, usr, type, INT_H_SIZE, destnode);
75715217 346 msg_set_errcode(msg, err);
b97bf3fd
PL
347 msg_set_destport(msg, destport);
348 msg_set_origport(msg, origport);
b97bf3fd 349 msg_set_orignode(msg, orignode);
b97bf3fd 350 msg_set_msgcnt(msg, ack);
b97bf3fd
PL
351 }
352 return buf;
353}
354
b97bf3fd
PL
355int tipc_reject_msg(struct sk_buff *buf, u32 err)
356{
357 struct tipc_msg *msg = buf_msg(buf);
358 struct sk_buff *rbuf;
359 struct tipc_msg *rmsg;
360 int hdr_sz;
7dd1bf28 361 u32 imp;
b97bf3fd 362 u32 data_sz = msg_data_sz(msg);
017dac31 363 u32 src_node;
7dd1bf28 364 u32 rmsg_sz;
b97bf3fd
PL
365
366 /* discard rejected message if it shouldn't be returned to sender */
76d12527
AS
367
368 if (WARN(!msg_isdata(msg),
369 "attempt to reject message with user=%u", msg_user(msg))) {
370 dump_stack();
371 goto exit;
372 }
acc631bf
AS
373 if (msg_errcode(msg) || msg_dest_droppable(msg))
374 goto exit;
b97bf3fd 375
7dd1bf28
AS
376 /*
377 * construct returned message by copying rejected message header and
378 * data (or subset), then updating header fields that need adjusting
379 */
380
381 hdr_sz = msg_hdr_sz(msg);
382 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
383
384 rbuf = tipc_buf_acquire(rmsg_sz);
acc631bf
AS
385 if (rbuf == NULL)
386 goto exit;
387
b97bf3fd 388 rmsg = buf_msg(rbuf);
7dd1bf28
AS
389 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
390
391 if (msg_connected(rmsg)) {
392 imp = msg_importance(rmsg);
393 if (imp < TIPC_CRITICAL_IMPORTANCE)
394 msg_set_importance(rmsg, ++imp);
99c14593 395 }
7dd1bf28
AS
396 msg_set_non_seq(rmsg, 0);
397 msg_set_size(rmsg, rmsg_sz);
398 msg_set_errcode(rmsg, err);
399 msg_set_prevnode(rmsg, tipc_own_addr);
400 msg_swap_words(rmsg, 4, 5);
401 if (!msg_short(rmsg))
402 msg_swap_words(rmsg, 6, 7);
b97bf3fd
PL
403
404 /* send self-abort message when rejecting on a connected port */
405 if (msg_connected(msg)) {
1fc54d8f 406 struct sk_buff *abuf = NULL;
23dd4cce 407 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd
PL
408
409 if (p_ptr) {
23dd4cce 410 if (p_ptr->connected)
b97bf3fd 411 abuf = port_build_self_abort_msg(p_ptr, err);
4323add6 412 tipc_port_unlock(p_ptr);
b97bf3fd 413 }
4323add6 414 tipc_net_route_msg(abuf);
b97bf3fd
PL
415 }
416
acc631bf
AS
417 /* send returned message & dispose of rejected message */
418
017dac31
AS
419 src_node = msg_prevnode(msg);
420 if (src_node == tipc_own_addr)
421 tipc_port_recv_msg(rbuf);
422 else
423 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
acc631bf
AS
424exit:
425 buf_discard(buf);
b97bf3fd
PL
426 return data_sz;
427}
428
23dd4cce 429int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
4323add6 430 struct iovec const *msg_sect, u32 num_sect,
26896904 431 unsigned int total_len, int err)
b97bf3fd
PL
432{
433 struct sk_buff *buf;
434 int res;
435
26896904 436 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
b97bf3fd
PL
437 !p_ptr->user_port, &buf);
438 if (!buf)
439 return res;
440
441 return tipc_reject_msg(buf, err);
442}
443
444static void port_timeout(unsigned long ref)
445{
23dd4cce 446 struct tipc_port *p_ptr = tipc_port_lock(ref);
1fc54d8f 447 struct sk_buff *buf = NULL;
b97bf3fd 448
065fd177
AS
449 if (!p_ptr)
450 return;
451
23dd4cce 452 if (!p_ptr->connected) {
065fd177 453 tipc_port_unlock(p_ptr);
b97bf3fd 454 return;
065fd177 455 }
b97bf3fd
PL
456
457 /* Last probe answered ? */
458 if (p_ptr->probing_state == PROBING) {
459 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
460 } else {
461 buf = port_build_proto_msg(port_peerport(p_ptr),
462 port_peernode(p_ptr),
23dd4cce 463 p_ptr->ref,
b97bf3fd
PL
464 tipc_own_addr,
465 CONN_MANAGER,
466 CONN_PROBE,
c4307285 467 TIPC_OK,
b97bf3fd 468 0);
b97bf3fd
PL
469 p_ptr->probing_state = PROBING;
470 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
471 }
4323add6
PL
472 tipc_port_unlock(p_ptr);
473 tipc_net_route_msg(buf);
b97bf3fd
PL
474}
475
476
477static void port_handle_node_down(unsigned long ref)
478{
23dd4cce 479 struct tipc_port *p_ptr = tipc_port_lock(ref);
0e65967e 480 struct sk_buff *buf = NULL;
b97bf3fd
PL
481
482 if (!p_ptr)
483 return;
484 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
4323add6
PL
485 tipc_port_unlock(p_ptr);
486 tipc_net_route_msg(buf);
b97bf3fd
PL
487}
488
489
23dd4cce 490static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 491{
e244a915 492 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
b97bf3fd 493
e244a915
AS
494 if (buf) {
495 struct tipc_msg *msg = buf_msg(buf);
496 msg_swap_words(msg, 4, 5);
497 msg_swap_words(msg, 6, 7);
498 }
499 return buf;
b97bf3fd
PL
500}
501
502
23dd4cce 503static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
b97bf3fd 504{
e244a915
AS
505 struct sk_buff *buf;
506 struct tipc_msg *msg;
507 u32 imp;
b97bf3fd 508
23dd4cce 509 if (!p_ptr->connected)
1fc54d8f 510 return NULL;
e244a915
AS
511
512 buf = tipc_buf_acquire(BASIC_H_SIZE);
513 if (buf) {
514 msg = buf_msg(buf);
515 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
516 msg_set_hdr_sz(msg, BASIC_H_SIZE);
517 msg_set_size(msg, BASIC_H_SIZE);
518 imp = msg_importance(msg);
519 if (imp < TIPC_CRITICAL_IMPORTANCE)
520 msg_set_importance(msg, ++imp);
521 msg_set_errcode(msg, err);
522 }
523 return buf;
b97bf3fd
PL
524}
525
4323add6 526void tipc_port_recv_proto_msg(struct sk_buff *buf)
b97bf3fd
PL
527{
528 struct tipc_msg *msg = buf_msg(buf);
23dd4cce 529 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd 530 u32 err = TIPC_OK;
1fc54d8f
SR
531 struct sk_buff *r_buf = NULL;
532 struct sk_buff *abort_buf = NULL;
b97bf3fd 533
b97bf3fd
PL
534 if (!p_ptr) {
535 err = TIPC_ERR_NO_PORT;
23dd4cce 536 } else if (p_ptr->connected) {
96d841b7
AS
537 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
538 (port_peerport(p_ptr) != msg_origport(msg))) {
b97bf3fd 539 err = TIPC_ERR_NO_PORT;
96d841b7 540 } else if (msg_type(msg) == CONN_ACK) {
c4307285 541 int wakeup = tipc_port_congested(p_ptr) &&
23dd4cce 542 p_ptr->congested &&
b97bf3fd
PL
543 p_ptr->wakeup;
544 p_ptr->acked += msg_msgcnt(msg);
4323add6 545 if (tipc_port_congested(p_ptr))
b97bf3fd 546 goto exit;
23dd4cce 547 p_ptr->congested = 0;
b97bf3fd
PL
548 if (!wakeup)
549 goto exit;
23dd4cce 550 p_ptr->wakeup(p_ptr);
b97bf3fd
PL
551 goto exit;
552 }
23dd4cce 553 } else if (p_ptr->published) {
b97bf3fd
PL
554 err = TIPC_ERR_NO_PORT;
555 }
556 if (err) {
557 r_buf = port_build_proto_msg(msg_origport(msg),
c4307285
YH
558 msg_orignode(msg),
559 msg_destport(msg),
b97bf3fd 560 tipc_own_addr,
06d82c91 561 TIPC_HIGH_IMPORTANCE,
b97bf3fd
PL
562 TIPC_CONN_MSG,
563 err,
b97bf3fd
PL
564 0);
565 goto exit;
566 }
567
568 /* All is fine */
569 if (msg_type(msg) == CONN_PROBE) {
c4307285
YH
570 r_buf = port_build_proto_msg(msg_origport(msg),
571 msg_orignode(msg),
572 msg_destport(msg),
573 tipc_own_addr,
b97bf3fd
PL
574 CONN_MANAGER,
575 CONN_PROBE_REPLY,
576 TIPC_OK,
b97bf3fd
PL
577 0);
578 }
579 p_ptr->probing_state = CONFIRMED;
b97bf3fd
PL
580exit:
581 if (p_ptr)
4323add6
PL
582 tipc_port_unlock(p_ptr);
583 tipc_net_route_msg(r_buf);
584 tipc_net_route_msg(abort_buf);
b97bf3fd
PL
585 buf_discard(buf);
586}
587
23dd4cce 588static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
b97bf3fd 589{
c4307285 590 struct publication *publ;
b97bf3fd
PL
591
592 if (full_id)
c4307285 593 tipc_printf(buf, "<%u.%u.%u:%u>:",
b97bf3fd 594 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
23dd4cce 595 tipc_node(tipc_own_addr), p_ptr->ref);
b97bf3fd 596 else
23dd4cce 597 tipc_printf(buf, "%-10u:", p_ptr->ref);
b97bf3fd 598
23dd4cce 599 if (p_ptr->connected) {
c4307285
YH
600 u32 dport = port_peerport(p_ptr);
601 u32 destnode = port_peernode(p_ptr);
602
603 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
604 tipc_zone(destnode), tipc_cluster(destnode),
605 tipc_node(destnode), dport);
23dd4cce 606 if (p_ptr->conn_type != 0)
c4307285 607 tipc_printf(buf, " via {%u,%u}",
23dd4cce
AS
608 p_ptr->conn_type,
609 p_ptr->conn_instance);
610 } else if (p_ptr->published) {
c4307285
YH
611 tipc_printf(buf, " bound to");
612 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
b97bf3fd
PL
613 if (publ->lower == publ->upper)
614 tipc_printf(buf, " {%u,%u}", publ->type,
615 publ->lower);
616 else
c4307285 617 tipc_printf(buf, " {%u,%u,%u}", publ->type,
b97bf3fd 618 publ->lower, publ->upper);
c4307285
YH
619 }
620 }
621 tipc_printf(buf, "\n");
b97bf3fd
PL
622}
623
624#define MAX_PORT_QUERY 32768
625
4323add6 626struct sk_buff *tipc_port_get_ports(void)
b97bf3fd
PL
627{
628 struct sk_buff *buf;
629 struct tlv_desc *rep_tlv;
630 struct print_buf pb;
23dd4cce 631 struct tipc_port *p_ptr;
b97bf3fd
PL
632 int str_len;
633
4323add6 634 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
b97bf3fd
PL
635 if (!buf)
636 return NULL;
637 rep_tlv = (struct tlv_desc *)buf->data;
638
4323add6
PL
639 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
640 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 641 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 642 spin_lock_bh(p_ptr->lock);
b97bf3fd 643 port_print(p_ptr, &pb, 0);
23dd4cce 644 spin_unlock_bh(p_ptr->lock);
b97bf3fd 645 }
4323add6
PL
646 spin_unlock_bh(&tipc_port_list_lock);
647 str_len = tipc_printbuf_validate(&pb);
b97bf3fd
PL
648
649 skb_put(buf, TLV_SPACE(str_len));
650 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
651
652 return buf;
653}
654
4323add6 655void tipc_port_reinit(void)
b97bf3fd 656{
23dd4cce 657 struct tipc_port *p_ptr;
b97bf3fd
PL
658 struct tipc_msg *msg;
659
4323add6 660 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd 661 list_for_each_entry(p_ptr, &ports, port_list) {
23dd4cce 662 msg = &p_ptr->phdr;
b97bf3fd
PL
663 if (msg_orignode(msg) == tipc_own_addr)
664 break;
6d4a6672 665 msg_set_prevnode(msg, tipc_own_addr);
b97bf3fd
PL
666 msg_set_orignode(msg, tipc_own_addr);
667 }
4323add6 668 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
669}
670
671
672/*
673 * port_dispatcher_sigh(): Signal handler for messages destinated
674 * to the tipc_port interface.
675 */
676
677static void port_dispatcher_sigh(void *dummy)
678{
679 struct sk_buff *buf;
680
681 spin_lock_bh(&queue_lock);
682 buf = msg_queue_head;
1fc54d8f 683 msg_queue_head = NULL;
b97bf3fd
PL
684 spin_unlock_bh(&queue_lock);
685
686 while (buf) {
23dd4cce 687 struct tipc_port *p_ptr;
b97bf3fd
PL
688 struct user_port *up_ptr;
689 struct tipc_portid orig;
690 struct tipc_name_seq dseq;
691 void *usr_handle;
692 int connected;
693 int published;
9688243b 694 u32 message_type;
b97bf3fd
PL
695
696 struct sk_buff *next = buf->next;
697 struct tipc_msg *msg = buf_msg(buf);
698 u32 dref = msg_destport(msg);
c4307285 699
9688243b
AS
700 message_type = msg_type(msg);
701 if (message_type > TIPC_DIRECT_MSG)
702 goto reject; /* Unsupported message type */
703
4323add6 704 p_ptr = tipc_port_lock(dref);
9688243b
AS
705 if (!p_ptr)
706 goto reject; /* Port deleted while msg in queue */
707
b97bf3fd
PL
708 orig.ref = msg_origport(msg);
709 orig.node = msg_orignode(msg);
710 up_ptr = p_ptr->user_port;
711 usr_handle = up_ptr->usr_handle;
23dd4cce
AS
712 connected = p_ptr->connected;
713 published = p_ptr->published;
b97bf3fd
PL
714
715 if (unlikely(msg_errcode(msg)))
716 goto err;
717
9688243b 718 switch (message_type) {
c4307285 719
b97bf3fd
PL
720 case TIPC_CONN_MSG:{
721 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
722 u32 peer_port = port_peerport(p_ptr);
723 u32 peer_node = port_peernode(p_ptr);
cb7ce914 724 u32 dsz;
b97bf3fd 725
4cec72c8 726 tipc_port_unlock(p_ptr);
5307e469
AS
727 if (unlikely(!cb))
728 goto reject;
b97bf3fd 729 if (unlikely(!connected)) {
84b07c16 730 if (tipc_connect2port(dref, &orig))
b97bf3fd 731 goto reject;
84b07c16
AS
732 } else if ((msg_origport(msg) != peer_port) ||
733 (msg_orignode(msg) != peer_node))
b97bf3fd 734 goto reject;
cb7ce914
AS
735 dsz = msg_data_sz(msg);
736 if (unlikely(dsz &&
737 (++p_ptr->conn_unacked >=
738 TIPC_FLOW_CONTROL_WIN)))
c4307285 739 tipc_acknowledge(dref,
23dd4cce 740 p_ptr->conn_unacked);
b97bf3fd 741 skb_pull(buf, msg_hdr_sz(msg));
cb7ce914 742 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
b97bf3fd
PL
743 break;
744 }
745 case TIPC_DIRECT_MSG:{
746 tipc_msg_event cb = up_ptr->msg_cb;
747
4cec72c8 748 tipc_port_unlock(p_ptr);
5307e469 749 if (unlikely(!cb || connected))
b97bf3fd
PL
750 goto reject;
751 skb_pull(buf, msg_hdr_sz(msg));
c4307285 752 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
753 msg_data_sz(msg), msg_importance(msg),
754 &orig);
755 break;
756 }
9688243b 757 case TIPC_MCAST_MSG:
b97bf3fd
PL
758 case TIPC_NAMED_MSG:{
759 tipc_named_msg_event cb = up_ptr->named_msg_cb;
760
4cec72c8 761 tipc_port_unlock(p_ptr);
5307e469 762 if (unlikely(!cb || connected || !published))
b97bf3fd
PL
763 goto reject;
764 dseq.type = msg_nametype(msg);
765 dseq.lower = msg_nameinst(msg);
9688243b
AS
766 dseq.upper = (message_type == TIPC_NAMED_MSG)
767 ? dseq.lower : msg_nameupper(msg);
b97bf3fd 768 skb_pull(buf, msg_hdr_sz(msg));
c4307285 769 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
770 msg_data_sz(msg), msg_importance(msg),
771 &orig, &dseq);
772 break;
773 }
774 }
775 if (buf)
776 buf_discard(buf);
777 buf = next;
778 continue;
779err:
9688243b 780 switch (message_type) {
c4307285 781
b97bf3fd 782 case TIPC_CONN_MSG:{
c4307285 783 tipc_conn_shutdown_event cb =
b97bf3fd
PL
784 up_ptr->conn_err_cb;
785 u32 peer_port = port_peerport(p_ptr);
786 u32 peer_node = port_peernode(p_ptr);
787
4cec72c8 788 tipc_port_unlock(p_ptr);
5307e469 789 if (!cb || !connected)
b97bf3fd 790 break;
5307e469
AS
791 if ((msg_origport(msg) != peer_port) ||
792 (msg_orignode(msg) != peer_node))
b97bf3fd
PL
793 break;
794 tipc_disconnect(dref);
795 skb_pull(buf, msg_hdr_sz(msg));
796 cb(usr_handle, dref, &buf, msg_data(msg),
797 msg_data_sz(msg), msg_errcode(msg));
798 break;
799 }
800 case TIPC_DIRECT_MSG:{
801 tipc_msg_err_event cb = up_ptr->err_cb;
802
4cec72c8 803 tipc_port_unlock(p_ptr);
5307e469 804 if (!cb || connected)
b97bf3fd
PL
805 break;
806 skb_pull(buf, msg_hdr_sz(msg));
807 cb(usr_handle, dref, &buf, msg_data(msg),
808 msg_data_sz(msg), msg_errcode(msg), &orig);
809 break;
810 }
9688243b 811 case TIPC_MCAST_MSG:
b97bf3fd 812 case TIPC_NAMED_MSG:{
c4307285 813 tipc_named_msg_err_event cb =
b97bf3fd
PL
814 up_ptr->named_err_cb;
815
4cec72c8 816 tipc_port_unlock(p_ptr);
5307e469 817 if (!cb || connected)
b97bf3fd
PL
818 break;
819 dseq.type = msg_nametype(msg);
820 dseq.lower = msg_nameinst(msg);
9688243b
AS
821 dseq.upper = (message_type == TIPC_NAMED_MSG)
822 ? dseq.lower : msg_nameupper(msg);
b97bf3fd 823 skb_pull(buf, msg_hdr_sz(msg));
c4307285 824 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
825 msg_data_sz(msg), msg_errcode(msg), &dseq);
826 break;
827 }
828 }
829 if (buf)
830 buf_discard(buf);
831 buf = next;
832 continue;
833reject:
834 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
835 buf = next;
836 }
837}
838
839/*
840 * port_dispatcher(): Dispatcher for messages destinated
841 * to the tipc_port interface. Called with port locked.
842 */
843
844static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
845{
846 buf->next = NULL;
847 spin_lock_bh(&queue_lock);
848 if (msg_queue_head) {
849 msg_queue_tail->next = buf;
850 msg_queue_tail = buf;
851 } else {
852 msg_queue_tail = msg_queue_head = buf;
4323add6 853 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
b97bf3fd
PL
854 }
855 spin_unlock_bh(&queue_lock);
0e35fd5e 856 return 0;
b97bf3fd
PL
857}
858
c4307285 859/*
b97bf3fd 860 * Wake up port after congestion: Called with port locked,
c4307285 861 *
b97bf3fd
PL
862 */
863
864static void port_wakeup_sh(unsigned long ref)
865{
23dd4cce 866 struct tipc_port *p_ptr;
b97bf3fd 867 struct user_port *up_ptr;
1fc54d8f
SR
868 tipc_continue_event cb = NULL;
869 void *uh = NULL;
b97bf3fd 870
4323add6 871 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
872 if (p_ptr) {
873 up_ptr = p_ptr->user_port;
874 if (up_ptr) {
875 cb = up_ptr->continue_event_cb;
876 uh = up_ptr->usr_handle;
877 }
4323add6 878 tipc_port_unlock(p_ptr);
b97bf3fd
PL
879 }
880 if (cb)
881 cb(uh, ref);
882}
883
884
885static void port_wakeup(struct tipc_port *p_ptr)
886{
4323add6 887 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
b97bf3fd
PL
888}
889
890void tipc_acknowledge(u32 ref, u32 ack)
891{
23dd4cce 892 struct tipc_port *p_ptr;
1fc54d8f 893 struct sk_buff *buf = NULL;
b97bf3fd 894
4323add6 895 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
896 if (!p_ptr)
897 return;
23dd4cce
AS
898 if (p_ptr->connected) {
899 p_ptr->conn_unacked -= ack;
b97bf3fd
PL
900 buf = port_build_proto_msg(port_peerport(p_ptr),
901 port_peernode(p_ptr),
902 ref,
903 tipc_own_addr,
904 CONN_MANAGER,
905 CONN_ACK,
c4307285 906 TIPC_OK,
b97bf3fd
PL
907 ack);
908 }
4323add6
PL
909 tipc_port_unlock(p_ptr);
910 tipc_net_route_msg(buf);
b97bf3fd
PL
911}
912
913/*
b0c1e928 914 * tipc_createport(): user level call.
b97bf3fd
PL
915 */
916
b0c1e928 917int tipc_createport(void *usr_handle,
c4307285
YH
918 unsigned int importance,
919 tipc_msg_err_event error_cb,
920 tipc_named_msg_err_event named_error_cb,
921 tipc_conn_shutdown_event conn_error_cb,
922 tipc_msg_event msg_cb,
923 tipc_named_msg_event named_msg_cb,
924 tipc_conn_msg_event conn_msg_cb,
b97bf3fd
PL
925 tipc_continue_event continue_event_cb,/* May be zero */
926 u32 *portref)
927{
928 struct user_port *up_ptr;
23dd4cce 929 struct tipc_port *p_ptr;
b97bf3fd 930
0da974f4 931 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
a10bd924 932 if (!up_ptr) {
a75bf874 933 warn("Port creation failed, no memory\n");
b97bf3fd
PL
934 return -ENOMEM;
935 }
23dd4cce 936 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
0ea52241
AS
937 port_wakeup, importance);
938 if (!p_ptr) {
b97bf3fd
PL
939 kfree(up_ptr);
940 return -ENOMEM;
941 }
942
943 p_ptr->user_port = up_ptr;
b97bf3fd 944 up_ptr->usr_handle = usr_handle;
23dd4cce 945 up_ptr->ref = p_ptr->ref;
b97bf3fd
PL
946 up_ptr->err_cb = error_cb;
947 up_ptr->named_err_cb = named_error_cb;
948 up_ptr->conn_err_cb = conn_error_cb;
949 up_ptr->msg_cb = msg_cb;
950 up_ptr->named_msg_cb = named_msg_cb;
951 up_ptr->conn_msg_cb = conn_msg_cb;
952 up_ptr->continue_event_cb = continue_event_cb;
23dd4cce 953 *portref = p_ptr->ref;
4323add6 954 tipc_port_unlock(p_ptr);
0e35fd5e 955 return 0;
b97bf3fd
PL
956}
957
b97bf3fd
PL
958int tipc_portimportance(u32 ref, unsigned int *importance)
959{
23dd4cce 960 struct tipc_port *p_ptr;
c4307285 961
4323add6 962 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
963 if (!p_ptr)
964 return -EINVAL;
23dd4cce 965 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
4cec72c8 966 tipc_port_unlock(p_ptr);
0e35fd5e 967 return 0;
b97bf3fd
PL
968}
969
970int tipc_set_portimportance(u32 ref, unsigned int imp)
971{
23dd4cce 972 struct tipc_port *p_ptr;
b97bf3fd
PL
973
974 if (imp > TIPC_CRITICAL_IMPORTANCE)
975 return -EINVAL;
976
4323add6 977 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
978 if (!p_ptr)
979 return -EINVAL;
23dd4cce 980 msg_set_importance(&p_ptr->phdr, (u32)imp);
4cec72c8 981 tipc_port_unlock(p_ptr);
0e35fd5e 982 return 0;
b97bf3fd
PL
983}
984
985
986int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
987{
23dd4cce 988 struct tipc_port *p_ptr;
b97bf3fd
PL
989 struct publication *publ;
990 u32 key;
991 int res = -EINVAL;
992
4323add6 993 p_ptr = tipc_port_lock(ref);
d55b4c63
AB
994 if (!p_ptr)
995 return -EINVAL;
996
23dd4cce 997 if (p_ptr->connected)
b97bf3fd
PL
998 goto exit;
999 if (seq->lower > seq->upper)
1000 goto exit;
1001 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1002 goto exit;
1003 key = ref + p_ptr->pub_count + 1;
1004 if (key == ref) {
1005 res = -EADDRINUSE;
1006 goto exit;
1007 }
4323add6 1008 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
23dd4cce 1009 scope, p_ptr->ref, key);
b97bf3fd
PL
1010 if (publ) {
1011 list_add(&publ->pport_list, &p_ptr->publications);
1012 p_ptr->pub_count++;
23dd4cce 1013 p_ptr->published = 1;
0e35fd5e 1014 res = 0;
b97bf3fd
PL
1015 }
1016exit:
4323add6 1017 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1018 return res;
1019}
1020
1021int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1022{
23dd4cce 1023 struct tipc_port *p_ptr;
b97bf3fd
PL
1024 struct publication *publ;
1025 struct publication *tpubl;
1026 int res = -EINVAL;
c4307285 1027
4323add6 1028 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1029 if (!p_ptr)
1030 return -EINVAL;
b97bf3fd 1031 if (!seq) {
c4307285 1032 list_for_each_entry_safe(publ, tpubl,
b97bf3fd 1033 &p_ptr->publications, pport_list) {
c4307285 1034 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 1035 publ->ref, publ->key);
b97bf3fd 1036 }
0e35fd5e 1037 res = 0;
b97bf3fd 1038 } else {
c4307285 1039 list_for_each_entry_safe(publ, tpubl,
b97bf3fd
PL
1040 &p_ptr->publications, pport_list) {
1041 if (publ->scope != scope)
1042 continue;
1043 if (publ->type != seq->type)
1044 continue;
1045 if (publ->lower != seq->lower)
1046 continue;
1047 if (publ->upper != seq->upper)
1048 break;
c4307285 1049 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 1050 publ->ref, publ->key);
0e35fd5e 1051 res = 0;
b97bf3fd
PL
1052 break;
1053 }
1054 }
1055 if (list_empty(&p_ptr->publications))
23dd4cce 1056 p_ptr->published = 0;
4323add6 1057 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1058 return res;
1059}
1060
1061int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1062{
23dd4cce 1063 struct tipc_port *p_ptr;
b97bf3fd
PL
1064 struct tipc_msg *msg;
1065 int res = -EINVAL;
1066
4323add6 1067 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1068 if (!p_ptr)
1069 return -EINVAL;
23dd4cce 1070 if (p_ptr->published || p_ptr->connected)
b97bf3fd
PL
1071 goto exit;
1072 if (!peer->ref)
1073 goto exit;
1074
23dd4cce 1075 msg = &p_ptr->phdr;
b97bf3fd
PL
1076 msg_set_destnode(msg, peer->node);
1077 msg_set_destport(msg, peer->ref);
1078 msg_set_orignode(msg, tipc_own_addr);
23dd4cce 1079 msg_set_origport(msg, p_ptr->ref);
b97bf3fd 1080 msg_set_type(msg, TIPC_CONN_MSG);
53b94364 1081 msg_set_lookup_scope(msg, 0);
08c80e9a 1082 msg_set_hdr_sz(msg, SHORT_H_SIZE);
b97bf3fd
PL
1083
1084 p_ptr->probing_interval = PROBING_INTERVAL;
1085 p_ptr->probing_state = CONFIRMED;
23dd4cce 1086 p_ptr->connected = 1;
b97bf3fd
PL
1087 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1088
0e65967e 1089 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
880b005f 1090 (void *)(unsigned long)ref,
b97bf3fd 1091 (net_ev_handler)port_handle_node_down);
0e35fd5e 1092 res = 0;
b97bf3fd 1093exit:
4323add6 1094 tipc_port_unlock(p_ptr);
23dd4cce 1095 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
b97bf3fd
PL
1096 return res;
1097}
1098
0c3141e9
AS
1099/**
1100 * tipc_disconnect_port - disconnect port from peer
1101 *
1102 * Port must be locked.
1103 */
1104
1105int tipc_disconnect_port(struct tipc_port *tp_ptr)
1106{
1107 int res;
1108
1109 if (tp_ptr->connected) {
1110 tp_ptr->connected = 0;
1111 /* let timer expire on it's own to avoid deadlock! */
1112 tipc_nodesub_unsubscribe(
23dd4cce 1113 &((struct tipc_port *)tp_ptr)->subscription);
0e35fd5e 1114 res = 0;
0c3141e9
AS
1115 } else {
1116 res = -ENOTCONN;
1117 }
1118 return res;
1119}
1120
b97bf3fd
PL
1121/*
1122 * tipc_disconnect(): Disconnect port form peer.
1123 * This is a node local operation.
1124 */
1125
1126int tipc_disconnect(u32 ref)
1127{
23dd4cce 1128 struct tipc_port *p_ptr;
0c3141e9 1129 int res;
b97bf3fd 1130
4323add6 1131 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1132 if (!p_ptr)
1133 return -EINVAL;
0c3141e9 1134 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
4323add6 1135 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1136 return res;
1137}
1138
1139/*
1140 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1141 */
1142int tipc_shutdown(u32 ref)
1143{
23dd4cce 1144 struct tipc_port *p_ptr;
1fc54d8f 1145 struct sk_buff *buf = NULL;
b97bf3fd 1146
4323add6 1147 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1148 if (!p_ptr)
1149 return -EINVAL;
1150
e244a915 1151 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
4323add6
PL
1152 tipc_port_unlock(p_ptr);
1153 tipc_net_route_msg(buf);
b97bf3fd
PL
1154 return tipc_disconnect(ref);
1155}
1156
b97bf3fd 1157/*
4323add6 1158 * tipc_port_recv_sections(): Concatenate and deliver sectioned
b97bf3fd
PL
1159 * message for this node.
1160 */
1161
23dd4cce 1162static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
26896904
AS
1163 struct iovec const *msg_sect,
1164 unsigned int total_len)
b97bf3fd
PL
1165{
1166 struct sk_buff *buf;
1167 int res;
c4307285 1168
26896904 1169 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
b97bf3fd
PL
1170 MAX_MSG_SIZE, !sender->user_port, &buf);
1171 if (likely(buf))
4323add6 1172 tipc_port_recv_msg(buf);
b97bf3fd
PL
1173 return res;
1174}
1175
1176/**
1177 * tipc_send - send message sections on connection
1178 */
1179
26896904
AS
1180int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1181 unsigned int total_len)
b97bf3fd 1182{
23dd4cce 1183 struct tipc_port *p_ptr;
b97bf3fd
PL
1184 u32 destnode;
1185 int res;
1186
4323add6 1187 p_ptr = tipc_port_deref(ref);
23dd4cce 1188 if (!p_ptr || !p_ptr->connected)
b97bf3fd
PL
1189 return -EINVAL;
1190
23dd4cce 1191 p_ptr->congested = 1;
4323add6 1192 if (!tipc_port_congested(p_ptr)) {
b97bf3fd
PL
1193 destnode = port_peernode(p_ptr);
1194 if (likely(destnode != tipc_own_addr))
4323add6 1195 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
26896904 1196 total_len, destnode);
b97bf3fd 1197 else
26896904
AS
1198 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1199 total_len);
b97bf3fd
PL
1200
1201 if (likely(res != -ELINKCONG)) {
23dd4cce 1202 p_ptr->congested = 0;
cb7ce914
AS
1203 if (res > 0)
1204 p_ptr->sent++;
b97bf3fd
PL
1205 return res;
1206 }
1207 }
1208 if (port_unreliable(p_ptr)) {
23dd4cce 1209 p_ptr->congested = 0;
26896904 1210 return total_len;
b97bf3fd
PL
1211 }
1212 return -ELINKCONG;
1213}
1214
b97bf3fd 1215/**
12bae479 1216 * tipc_send2name - send message sections to port name
b97bf3fd
PL
1217 */
1218
12bae479 1219int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
26896904
AS
1220 unsigned int num_sect, struct iovec const *msg_sect,
1221 unsigned int total_len)
b97bf3fd 1222{
23dd4cce 1223 struct tipc_port *p_ptr;
b97bf3fd
PL
1224 struct tipc_msg *msg;
1225 u32 destnode = domain;
9ccc2eb4 1226 u32 destport;
b97bf3fd
PL
1227 int res;
1228
4323add6 1229 p_ptr = tipc_port_deref(ref);
23dd4cce 1230 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1231 return -EINVAL;
1232
23dd4cce 1233 msg = &p_ptr->phdr;
b97bf3fd 1234 msg_set_type(msg, TIPC_NAMED_MSG);
12bae479
AS
1235 msg_set_orignode(msg, tipc_own_addr);
1236 msg_set_origport(msg, ref);
741d9eb7 1237 msg_set_hdr_sz(msg, NAMED_H_SIZE);
b97bf3fd
PL
1238 msg_set_nametype(msg, name->type);
1239 msg_set_nameinst(msg, name->instance);
c68ca7b7 1240 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
4323add6 1241 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
1242 msg_set_destnode(msg, destnode);
1243 msg_set_destport(msg, destport);
1244
5d9c54c1 1245 if (likely(destport)) {
b97bf3fd 1246 if (likely(destnode == tipc_own_addr))
cb7ce914 1247 res = tipc_port_recv_sections(p_ptr, num_sect,
26896904 1248 msg_sect, total_len);
cb7ce914
AS
1249 else
1250 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
26896904
AS
1251 num_sect, total_len,
1252 destnode);
cb7ce914
AS
1253 if (likely(res != -ELINKCONG)) {
1254 if (res > 0)
1255 p_ptr->sent++;
b97bf3fd 1256 return res;
cb7ce914 1257 }
b97bf3fd 1258 if (port_unreliable(p_ptr)) {
26896904 1259 return total_len;
b97bf3fd
PL
1260 }
1261 return -ELINKCONG;
1262 }
c4307285 1263 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
26896904 1264 total_len, TIPC_ERR_NO_NAME);
b97bf3fd
PL
1265}
1266
1267/**
12bae479 1268 * tipc_send2port - send message sections to port identity
b97bf3fd
PL
1269 */
1270
12bae479 1271int tipc_send2port(u32 ref, struct tipc_portid const *dest,
26896904
AS
1272 unsigned int num_sect, struct iovec const *msg_sect,
1273 unsigned int total_len)
b97bf3fd 1274{
23dd4cce 1275 struct tipc_port *p_ptr;
b97bf3fd
PL
1276 struct tipc_msg *msg;
1277 int res;
1278
4323add6 1279 p_ptr = tipc_port_deref(ref);
23dd4cce 1280 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1281 return -EINVAL;
1282
23dd4cce 1283 msg = &p_ptr->phdr;
b97bf3fd 1284 msg_set_type(msg, TIPC_DIRECT_MSG);
53b94364 1285 msg_set_lookup_scope(msg, 0);
12bae479
AS
1286 msg_set_orignode(msg, tipc_own_addr);
1287 msg_set_origport(msg, ref);
b97bf3fd
PL
1288 msg_set_destnode(msg, dest->node);
1289 msg_set_destport(msg, dest->ref);
741d9eb7 1290 msg_set_hdr_sz(msg, BASIC_H_SIZE);
cb7ce914 1291
b97bf3fd 1292 if (dest->node == tipc_own_addr)
26896904
AS
1293 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1294 total_len);
cb7ce914
AS
1295 else
1296 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
26896904 1297 total_len, dest->node);
cb7ce914
AS
1298 if (likely(res != -ELINKCONG)) {
1299 if (res > 0)
1300 p_ptr->sent++;
b97bf3fd 1301 return res;
cb7ce914 1302 }
b97bf3fd 1303 if (port_unreliable(p_ptr)) {
26896904 1304 return total_len;
b97bf3fd
PL
1305 }
1306 return -ELINKCONG;
1307}
1308
c4307285 1309/**
12bae479 1310 * tipc_send_buf2port - send message buffer to port identity
b97bf3fd
PL
1311 */
1312
12bae479
AS
1313int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1314 struct sk_buff *buf, unsigned int dsz)
b97bf3fd 1315{
23dd4cce 1316 struct tipc_port *p_ptr;
b97bf3fd
PL
1317 struct tipc_msg *msg;
1318 int res;
1319
23dd4cce
AS
1320 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1321 if (!p_ptr || p_ptr->connected)
b97bf3fd
PL
1322 return -EINVAL;
1323
23dd4cce 1324 msg = &p_ptr->phdr;
b97bf3fd 1325 msg_set_type(msg, TIPC_DIRECT_MSG);
12bae479
AS
1326 msg_set_orignode(msg, tipc_own_addr);
1327 msg_set_origport(msg, ref);
b97bf3fd
PL
1328 msg_set_destnode(msg, dest->node);
1329 msg_set_destport(msg, dest->ref);
741d9eb7
AS
1330 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1331 msg_set_size(msg, BASIC_H_SIZE + dsz);
1332 if (skb_cow(buf, BASIC_H_SIZE))
b97bf3fd
PL
1333 return -ENOMEM;
1334
741d9eb7
AS
1335 skb_push(buf, BASIC_H_SIZE);
1336 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
cb7ce914 1337
b97bf3fd 1338 if (dest->node == tipc_own_addr)
cb7ce914
AS
1339 res = tipc_port_recv_msg(buf);
1340 else
1341 res = tipc_send_buf_fast(buf, dest->node);
1342 if (likely(res != -ELINKCONG)) {
1343 if (res > 0)
1344 p_ptr->sent++;
b97bf3fd 1345 return res;
cb7ce914 1346 }
b97bf3fd
PL
1347 if (port_unreliable(p_ptr))
1348 return dsz;
1349 return -ELINKCONG;
1350}
1351