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