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