tipc: Fix bug in connection setup via native API
[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
AS
4 * Copyright (c) 1992-2007, Ericsson AB
5 * Copyright (c) 2004-2007, 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);
119 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
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 *
7ef43eba
AS
216 * Returns port reference, or 0 if unable to create it
217 *
218 * Note: The newly created port is returned in the locked state.
b97bf3fd
PL
219 */
220
221u32 tipc_createport_raw(void *usr_handle,
222 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
223 void (*wakeup)(struct tipc_port *),
7ef43eba
AS
224 const u32 importance,
225 struct tipc_port **tp_ptr)
b97bf3fd
PL
226{
227 struct port *p_ptr;
228 struct tipc_msg *msg;
229 u32 ref;
230
0da974f4 231 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
a10bd924
AS
232 if (!p_ptr) {
233 warn("Port creation failed, no memory\n");
b97bf3fd
PL
234 return 0;
235 }
4323add6 236 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
b97bf3fd 237 if (!ref) {
a10bd924 238 warn("Port creation failed, reference table exhausted\n");
b97bf3fd
PL
239 kfree(p_ptr);
240 return 0;
241 }
242
05646c91
AS
243 p_ptr->publ.usr_handle = usr_handle;
244 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
b97bf3fd
PL
245 p_ptr->publ.ref = ref;
246 msg = &p_ptr->publ.phdr;
06d82c91
AS
247 msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE,
248 0);
b97bf3fd
PL
249 msg_set_orignode(msg, tipc_own_addr);
250 msg_set_prevnode(msg, tipc_own_addr);
251 msg_set_origport(msg, ref);
252 msg_set_importance(msg,importance);
253 p_ptr->last_in_seqno = 41;
254 p_ptr->sent = 1;
b97bf3fd
PL
255 INIT_LIST_HEAD(&p_ptr->wait_list);
256 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
1fc54d8f 257 p_ptr->congested_link = NULL;
b97bf3fd
PL
258 p_ptr->dispatcher = dispatcher;
259 p_ptr->wakeup = wakeup;
1fc54d8f 260 p_ptr->user_port = NULL;
b97bf3fd 261 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
4323add6 262 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
263 INIT_LIST_HEAD(&p_ptr->publications);
264 INIT_LIST_HEAD(&p_ptr->port_list);
265 list_add_tail(&p_ptr->port_list, &ports);
4323add6 266 spin_unlock_bh(&tipc_port_list_lock);
7ef43eba 267 *tp_ptr = &p_ptr->publ;
b97bf3fd
PL
268 return ref;
269}
270
271int tipc_deleteport(u32 ref)
272{
273 struct port *p_ptr;
1fc54d8f 274 struct sk_buff *buf = NULL;
b97bf3fd 275
1fc54d8f 276 tipc_withdraw(ref, 0, NULL);
4323add6 277 p_ptr = tipc_port_lock(ref);
c4307285 278 if (!p_ptr)
b97bf3fd
PL
279 return -EINVAL;
280
4323add6
PL
281 tipc_ref_discard(ref);
282 tipc_port_unlock(p_ptr);
b97bf3fd
PL
283
284 k_cancel_timer(&p_ptr->timer);
285 if (p_ptr->publ.connected) {
286 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
4323add6 287 tipc_nodesub_unsubscribe(&p_ptr->subscription);
b97bf3fd
PL
288 }
289 if (p_ptr->user_port) {
4323add6 290 tipc_reg_remove_port(p_ptr->user_port);
b97bf3fd
PL
291 kfree(p_ptr->user_port);
292 }
293
4323add6 294 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
295 list_del(&p_ptr->port_list);
296 list_del(&p_ptr->wait_list);
4323add6 297 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
298 k_term_timer(&p_ptr->timer);
299 kfree(p_ptr);
300 dbg("Deleted port %u\n", ref);
4323add6 301 tipc_net_route_msg(buf);
b97bf3fd
PL
302 return TIPC_OK;
303}
304
305/**
306 * tipc_get_port() - return port associated with 'ref'
c4307285 307 *
b97bf3fd
PL
308 * Note: Port is not locked.
309 */
310
311struct tipc_port *tipc_get_port(const u32 ref)
312{
4323add6 313 return (struct tipc_port *)tipc_ref_deref(ref);
b97bf3fd
PL
314}
315
316/**
317 * tipc_get_handle - return user handle associated to port 'ref'
318 */
319
320void *tipc_get_handle(const u32 ref)
321{
322 struct port *p_ptr;
323 void * handle;
324
4323add6 325 p_ptr = tipc_port_lock(ref);
b97bf3fd 326 if (!p_ptr)
1fc54d8f 327 return NULL;
b97bf3fd 328 handle = p_ptr->publ.usr_handle;
4323add6 329 tipc_port_unlock(p_ptr);
b97bf3fd
PL
330 return handle;
331}
332
05790c64 333static int port_unreliable(struct port *p_ptr)
b97bf3fd
PL
334{
335 return msg_src_droppable(&p_ptr->publ.phdr);
336}
337
338int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
339{
340 struct port *p_ptr;
c4307285 341
4323add6 342 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
343 if (!p_ptr)
344 return -EINVAL;
345 *isunreliable = port_unreliable(p_ptr);
4cec72c8 346 tipc_port_unlock(p_ptr);
b97bf3fd
PL
347 return TIPC_OK;
348}
349
350int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
351{
352 struct port *p_ptr;
c4307285 353
4323add6 354 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
355 if (!p_ptr)
356 return -EINVAL;
357 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
4323add6 358 tipc_port_unlock(p_ptr);
b97bf3fd
PL
359 return TIPC_OK;
360}
361
05790c64 362static int port_unreturnable(struct port *p_ptr)
b97bf3fd
PL
363{
364 return msg_dest_droppable(&p_ptr->publ.phdr);
365}
366
367int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
368{
369 struct port *p_ptr;
c4307285 370
4323add6 371 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
372 if (!p_ptr)
373 return -EINVAL;
374 *isunrejectable = port_unreturnable(p_ptr);
4cec72c8 375 tipc_port_unlock(p_ptr);
b97bf3fd
PL
376 return TIPC_OK;
377}
378
379int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
380{
381 struct port *p_ptr;
c4307285 382
4323add6 383 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
384 if (!p_ptr)
385 return -EINVAL;
386 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
4323add6 387 tipc_port_unlock(p_ptr);
b97bf3fd
PL
388 return TIPC_OK;
389}
390
c4307285
YH
391/*
392 * port_build_proto_msg(): build a port level protocol
393 * or a connection abortion message. Called with
b97bf3fd
PL
394 * tipc_port lock on.
395 */
396static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
397 u32 origport, u32 orignode,
c4307285 398 u32 usr, u32 type, u32 err,
b97bf3fd
PL
399 u32 seqno, u32 ack)
400{
401 struct sk_buff *buf;
402 struct tipc_msg *msg;
c4307285 403
b97bf3fd
PL
404 buf = buf_acquire(LONG_H_SIZE);
405 if (buf) {
406 msg = buf_msg(buf);
407 msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
408 msg_set_destport(msg, destport);
409 msg_set_origport(msg, origport);
410 msg_set_destnode(msg, destnode);
411 msg_set_orignode(msg, orignode);
412 msg_set_transp_seqno(msg, seqno);
413 msg_set_msgcnt(msg, ack);
414 msg_dbg(msg, "PORT>SEND>:");
415 }
416 return buf;
417}
418
b97bf3fd
PL
419int tipc_reject_msg(struct sk_buff *buf, u32 err)
420{
421 struct tipc_msg *msg = buf_msg(buf);
422 struct sk_buff *rbuf;
423 struct tipc_msg *rmsg;
424 int hdr_sz;
425 u32 imp = msg_importance(msg);
426 u32 data_sz = msg_data_sz(msg);
427
428 if (data_sz > MAX_REJECT_SIZE)
429 data_sz = MAX_REJECT_SIZE;
430 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
431 imp++;
432 msg_dbg(msg, "port->rej: ");
433
434 /* discard rejected message if it shouldn't be returned to sender */
435 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
436 buf_discard(buf);
437 return data_sz;
438 }
439
440 /* construct rejected message */
441 if (msg_mcast(msg))
442 hdr_sz = MCAST_H_SIZE;
443 else
444 hdr_sz = LONG_H_SIZE;
445 rbuf = buf_acquire(data_sz + hdr_sz);
446 if (rbuf == NULL) {
447 buf_discard(buf);
448 return data_sz;
449 }
450 rmsg = buf_msg(rbuf);
451 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
452 msg_set_destport(rmsg, msg_origport(msg));
453 msg_set_prevnode(rmsg, tipc_own_addr);
454 msg_set_origport(rmsg, msg_destport(msg));
455 if (msg_short(msg))
456 msg_set_orignode(rmsg, tipc_own_addr);
457 else
458 msg_set_orignode(rmsg, msg_destnode(msg));
c4307285 459 msg_set_size(rmsg, data_sz + hdr_sz);
b97bf3fd
PL
460 msg_set_nametype(rmsg, msg_nametype(msg));
461 msg_set_nameinst(rmsg, msg_nameinst(msg));
27d7ff46 462 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
b97bf3fd
PL
463
464 /* send self-abort message when rejecting on a connected port */
465 if (msg_connected(msg)) {
1fc54d8f 466 struct sk_buff *abuf = NULL;
4323add6 467 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd
PL
468
469 if (p_ptr) {
470 if (p_ptr->publ.connected)
471 abuf = port_build_self_abort_msg(p_ptr, err);
4323add6 472 tipc_port_unlock(p_ptr);
b97bf3fd 473 }
4323add6 474 tipc_net_route_msg(abuf);
b97bf3fd
PL
475 }
476
477 /* send rejected message */
478 buf_discard(buf);
4323add6 479 tipc_net_route_msg(rbuf);
b97bf3fd
PL
480 return data_sz;
481}
482
4323add6
PL
483int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
484 struct iovec const *msg_sect, u32 num_sect,
485 int err)
b97bf3fd
PL
486{
487 struct sk_buff *buf;
488 int res;
489
c4307285 490 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
b97bf3fd
PL
491 !p_ptr->user_port, &buf);
492 if (!buf)
493 return res;
494
495 return tipc_reject_msg(buf, err);
496}
497
498static void port_timeout(unsigned long ref)
499{
4323add6 500 struct port *p_ptr = tipc_port_lock(ref);
1fc54d8f 501 struct sk_buff *buf = NULL;
b97bf3fd 502
065fd177
AS
503 if (!p_ptr)
504 return;
505
506 if (!p_ptr->publ.connected) {
507 tipc_port_unlock(p_ptr);
b97bf3fd 508 return;
065fd177 509 }
b97bf3fd
PL
510
511 /* Last probe answered ? */
512 if (p_ptr->probing_state == PROBING) {
513 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
514 } else {
515 buf = port_build_proto_msg(port_peerport(p_ptr),
516 port_peernode(p_ptr),
517 p_ptr->publ.ref,
518 tipc_own_addr,
519 CONN_MANAGER,
520 CONN_PROBE,
c4307285 521 TIPC_OK,
b97bf3fd
PL
522 port_out_seqno(p_ptr),
523 0);
524 port_incr_out_seqno(p_ptr);
525 p_ptr->probing_state = PROBING;
526 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
527 }
4323add6
PL
528 tipc_port_unlock(p_ptr);
529 tipc_net_route_msg(buf);
b97bf3fd
PL
530}
531
532
533static void port_handle_node_down(unsigned long ref)
534{
4323add6 535 struct port *p_ptr = tipc_port_lock(ref);
1fc54d8f 536 struct sk_buff* buf = NULL;
b97bf3fd
PL
537
538 if (!p_ptr)
539 return;
540 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
4323add6
PL
541 tipc_port_unlock(p_ptr);
542 tipc_net_route_msg(buf);
b97bf3fd
PL
543}
544
545
546static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
547{
548 u32 imp = msg_importance(&p_ptr->publ.phdr);
549
550 if (!p_ptr->publ.connected)
1fc54d8f 551 return NULL;
b97bf3fd
PL
552 if (imp < TIPC_CRITICAL_IMPORTANCE)
553 imp++;
554 return port_build_proto_msg(p_ptr->publ.ref,
555 tipc_own_addr,
556 port_peerport(p_ptr),
557 port_peernode(p_ptr),
558 imp,
559 TIPC_CONN_MSG,
c4307285 560 err,
b97bf3fd
PL
561 p_ptr->last_in_seqno + 1,
562 0);
563}
564
565
566static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
567{
568 u32 imp = msg_importance(&p_ptr->publ.phdr);
569
570 if (!p_ptr->publ.connected)
1fc54d8f 571 return NULL;
b97bf3fd
PL
572 if (imp < TIPC_CRITICAL_IMPORTANCE)
573 imp++;
574 return port_build_proto_msg(port_peerport(p_ptr),
575 port_peernode(p_ptr),
576 p_ptr->publ.ref,
577 tipc_own_addr,
578 imp,
579 TIPC_CONN_MSG,
c4307285 580 err,
b97bf3fd
PL
581 port_out_seqno(p_ptr),
582 0);
583}
584
4323add6 585void tipc_port_recv_proto_msg(struct sk_buff *buf)
b97bf3fd
PL
586{
587 struct tipc_msg *msg = buf_msg(buf);
4323add6 588 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd 589 u32 err = TIPC_OK;
1fc54d8f
SR
590 struct sk_buff *r_buf = NULL;
591 struct sk_buff *abort_buf = NULL;
b97bf3fd
PL
592
593 msg_dbg(msg, "PORT<RECV<:");
594
595 if (!p_ptr) {
596 err = TIPC_ERR_NO_PORT;
597 } else if (p_ptr->publ.connected) {
598 if (port_peernode(p_ptr) != msg_orignode(msg))
599 err = TIPC_ERR_NO_PORT;
600 if (port_peerport(p_ptr) != msg_origport(msg))
601 err = TIPC_ERR_NO_PORT;
602 if (!err && msg_routed(msg)) {
603 u32 seqno = msg_transp_seqno(msg);
604 u32 myno = ++p_ptr->last_in_seqno;
605 if (seqno != myno) {
606 err = TIPC_ERR_NO_PORT;
607 abort_buf = port_build_self_abort_msg(p_ptr, err);
608 }
609 }
610 if (msg_type(msg) == CONN_ACK) {
c4307285 611 int wakeup = tipc_port_congested(p_ptr) &&
b97bf3fd
PL
612 p_ptr->publ.congested &&
613 p_ptr->wakeup;
614 p_ptr->acked += msg_msgcnt(msg);
4323add6 615 if (tipc_port_congested(p_ptr))
b97bf3fd
PL
616 goto exit;
617 p_ptr->publ.congested = 0;
618 if (!wakeup)
619 goto exit;
620 p_ptr->wakeup(&p_ptr->publ);
621 goto exit;
622 }
623 } else if (p_ptr->publ.published) {
624 err = TIPC_ERR_NO_PORT;
625 }
626 if (err) {
627 r_buf = port_build_proto_msg(msg_origport(msg),
c4307285
YH
628 msg_orignode(msg),
629 msg_destport(msg),
b97bf3fd 630 tipc_own_addr,
06d82c91 631 TIPC_HIGH_IMPORTANCE,
b97bf3fd
PL
632 TIPC_CONN_MSG,
633 err,
634 0,
635 0);
636 goto exit;
637 }
638
639 /* All is fine */
640 if (msg_type(msg) == CONN_PROBE) {
c4307285
YH
641 r_buf = port_build_proto_msg(msg_origport(msg),
642 msg_orignode(msg),
643 msg_destport(msg),
644 tipc_own_addr,
b97bf3fd
PL
645 CONN_MANAGER,
646 CONN_PROBE_REPLY,
647 TIPC_OK,
648 port_out_seqno(p_ptr),
649 0);
650 }
651 p_ptr->probing_state = CONFIRMED;
652 port_incr_out_seqno(p_ptr);
653exit:
654 if (p_ptr)
4323add6
PL
655 tipc_port_unlock(p_ptr);
656 tipc_net_route_msg(r_buf);
657 tipc_net_route_msg(abort_buf);
b97bf3fd
PL
658 buf_discard(buf);
659}
660
661static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
662{
c4307285 663 struct publication *publ;
b97bf3fd
PL
664
665 if (full_id)
c4307285 666 tipc_printf(buf, "<%u.%u.%u:%u>:",
b97bf3fd 667 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
c4307285 668 tipc_node(tipc_own_addr), p_ptr->publ.ref);
b97bf3fd
PL
669 else
670 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
671
c4307285
YH
672 if (p_ptr->publ.connected) {
673 u32 dport = port_peerport(p_ptr);
674 u32 destnode = port_peernode(p_ptr);
675
676 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
677 tipc_zone(destnode), tipc_cluster(destnode),
678 tipc_node(destnode), dport);
679 if (p_ptr->publ.conn_type != 0)
680 tipc_printf(buf, " via {%u,%u}",
681 p_ptr->publ.conn_type,
682 p_ptr->publ.conn_instance);
683 }
684 else if (p_ptr->publ.published) {
685 tipc_printf(buf, " bound to");
686 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
b97bf3fd
PL
687 if (publ->lower == publ->upper)
688 tipc_printf(buf, " {%u,%u}", publ->type,
689 publ->lower);
690 else
c4307285 691 tipc_printf(buf, " {%u,%u,%u}", publ->type,
b97bf3fd 692 publ->lower, publ->upper);
c4307285
YH
693 }
694 }
695 tipc_printf(buf, "\n");
b97bf3fd
PL
696}
697
698#define MAX_PORT_QUERY 32768
699
4323add6 700struct sk_buff *tipc_port_get_ports(void)
b97bf3fd
PL
701{
702 struct sk_buff *buf;
703 struct tlv_desc *rep_tlv;
704 struct print_buf pb;
705 struct port *p_ptr;
706 int str_len;
707
4323add6 708 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
b97bf3fd
PL
709 if (!buf)
710 return NULL;
711 rep_tlv = (struct tlv_desc *)buf->data;
712
4323add6
PL
713 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
714 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
715 list_for_each_entry(p_ptr, &ports, port_list) {
716 spin_lock_bh(p_ptr->publ.lock);
717 port_print(p_ptr, &pb, 0);
718 spin_unlock_bh(p_ptr->publ.lock);
719 }
4323add6
PL
720 spin_unlock_bh(&tipc_port_list_lock);
721 str_len = tipc_printbuf_validate(&pb);
b97bf3fd
PL
722
723 skb_put(buf, TLV_SPACE(str_len));
724 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
725
726 return buf;
727}
728
729#if 0
730
731#define MAX_PORT_STATS 2000
732
733struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
734{
735 u32 ref;
736 struct port *p_ptr;
737 struct sk_buff *buf;
738 struct tlv_desc *rep_tlv;
739 struct print_buf pb;
740 int str_len;
741
742 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
743 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
744
745 ref = *(u32 *)TLV_DATA(req_tlv_area);
746 ref = ntohl(ref);
747
4323add6 748 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
749 if (!p_ptr)
750 return cfg_reply_error_string("port not found");
751
4323add6 752 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
b97bf3fd 753 if (!buf) {
4323add6 754 tipc_port_unlock(p_ptr);
b97bf3fd
PL
755 return NULL;
756 }
757 rep_tlv = (struct tlv_desc *)buf->data;
758
4323add6 759 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
b97bf3fd
PL
760 port_print(p_ptr, &pb, 1);
761 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
4323add6
PL
762 tipc_port_unlock(p_ptr);
763 str_len = tipc_printbuf_validate(&pb);
b97bf3fd
PL
764
765 skb_put(buf, TLV_SPACE(str_len));
766 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
767
768 return buf;
769}
770
771#endif
772
4323add6 773void tipc_port_reinit(void)
b97bf3fd
PL
774{
775 struct port *p_ptr;
776 struct tipc_msg *msg;
777
4323add6 778 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
779 list_for_each_entry(p_ptr, &ports, port_list) {
780 msg = &p_ptr->publ.phdr;
781 if (msg_orignode(msg) == tipc_own_addr)
782 break;
6d4a6672 783 msg_set_prevnode(msg, tipc_own_addr);
b97bf3fd
PL
784 msg_set_orignode(msg, tipc_own_addr);
785 }
4323add6 786 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
787}
788
789
790/*
791 * port_dispatcher_sigh(): Signal handler for messages destinated
792 * to the tipc_port interface.
793 */
794
795static void port_dispatcher_sigh(void *dummy)
796{
797 struct sk_buff *buf;
798
799 spin_lock_bh(&queue_lock);
800 buf = msg_queue_head;
1fc54d8f 801 msg_queue_head = NULL;
b97bf3fd
PL
802 spin_unlock_bh(&queue_lock);
803
804 while (buf) {
805 struct port *p_ptr;
806 struct user_port *up_ptr;
807 struct tipc_portid orig;
808 struct tipc_name_seq dseq;
809 void *usr_handle;
810 int connected;
811 int published;
9688243b 812 u32 message_type;
b97bf3fd
PL
813
814 struct sk_buff *next = buf->next;
815 struct tipc_msg *msg = buf_msg(buf);
816 u32 dref = msg_destport(msg);
c4307285 817
9688243b
AS
818 message_type = msg_type(msg);
819 if (message_type > TIPC_DIRECT_MSG)
820 goto reject; /* Unsupported message type */
821
4323add6 822 p_ptr = tipc_port_lock(dref);
9688243b
AS
823 if (!p_ptr)
824 goto reject; /* Port deleted while msg in queue */
825
b97bf3fd
PL
826 orig.ref = msg_origport(msg);
827 orig.node = msg_orignode(msg);
828 up_ptr = p_ptr->user_port;
829 usr_handle = up_ptr->usr_handle;
830 connected = p_ptr->publ.connected;
831 published = p_ptr->publ.published;
832
833 if (unlikely(msg_errcode(msg)))
834 goto err;
835
9688243b 836 switch (message_type) {
c4307285 837
b97bf3fd
PL
838 case TIPC_CONN_MSG:{
839 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
840 u32 peer_port = port_peerport(p_ptr);
841 u32 peer_node = port_peernode(p_ptr);
842
4cec72c8 843 tipc_port_unlock(p_ptr);
b97bf3fd 844 if (unlikely(!connected)) {
84b07c16 845 if (tipc_connect2port(dref, &orig))
b97bf3fd 846 goto reject;
84b07c16
AS
847 } else if ((msg_origport(msg) != peer_port) ||
848 (msg_orignode(msg) != peer_node))
b97bf3fd
PL
849 goto reject;
850 if (unlikely(!cb))
851 goto reject;
c4307285 852 if (unlikely(++p_ptr->publ.conn_unacked >=
b97bf3fd 853 TIPC_FLOW_CONTROL_WIN))
c4307285 854 tipc_acknowledge(dref,
b97bf3fd
PL
855 p_ptr->publ.conn_unacked);
856 skb_pull(buf, msg_hdr_sz(msg));
857 cb(usr_handle, dref, &buf, msg_data(msg),
858 msg_data_sz(msg));
859 break;
860 }
861 case TIPC_DIRECT_MSG:{
862 tipc_msg_event cb = up_ptr->msg_cb;
863
4cec72c8 864 tipc_port_unlock(p_ptr);
b97bf3fd
PL
865 if (unlikely(connected))
866 goto reject;
867 if (unlikely(!cb))
868 goto reject;
869 skb_pull(buf, msg_hdr_sz(msg));
c4307285 870 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
871 msg_data_sz(msg), msg_importance(msg),
872 &orig);
873 break;
874 }
9688243b 875 case TIPC_MCAST_MSG:
b97bf3fd
PL
876 case TIPC_NAMED_MSG:{
877 tipc_named_msg_event cb = up_ptr->named_msg_cb;
878
4cec72c8 879 tipc_port_unlock(p_ptr);
b97bf3fd
PL
880 if (unlikely(connected))
881 goto reject;
882 if (unlikely(!cb))
883 goto reject;
884 if (unlikely(!published))
885 goto reject;
886 dseq.type = msg_nametype(msg);
887 dseq.lower = msg_nameinst(msg);
9688243b
AS
888 dseq.upper = (message_type == TIPC_NAMED_MSG)
889 ? dseq.lower : msg_nameupper(msg);
b97bf3fd 890 skb_pull(buf, msg_hdr_sz(msg));
c4307285 891 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
892 msg_data_sz(msg), msg_importance(msg),
893 &orig, &dseq);
894 break;
895 }
896 }
897 if (buf)
898 buf_discard(buf);
899 buf = next;
900 continue;
901err:
9688243b 902 switch (message_type) {
c4307285 903
b97bf3fd 904 case TIPC_CONN_MSG:{
c4307285 905 tipc_conn_shutdown_event cb =
b97bf3fd
PL
906 up_ptr->conn_err_cb;
907 u32 peer_port = port_peerport(p_ptr);
908 u32 peer_node = port_peernode(p_ptr);
909
4cec72c8 910 tipc_port_unlock(p_ptr);
b97bf3fd
PL
911 if (!connected || !cb)
912 break;
913 if (msg_origport(msg) != peer_port)
914 break;
915 if (msg_orignode(msg) != peer_node)
916 break;
917 tipc_disconnect(dref);
918 skb_pull(buf, msg_hdr_sz(msg));
919 cb(usr_handle, dref, &buf, msg_data(msg),
920 msg_data_sz(msg), msg_errcode(msg));
921 break;
922 }
923 case TIPC_DIRECT_MSG:{
924 tipc_msg_err_event cb = up_ptr->err_cb;
925
4cec72c8 926 tipc_port_unlock(p_ptr);
b97bf3fd
PL
927 if (connected || !cb)
928 break;
929 skb_pull(buf, msg_hdr_sz(msg));
930 cb(usr_handle, dref, &buf, msg_data(msg),
931 msg_data_sz(msg), msg_errcode(msg), &orig);
932 break;
933 }
9688243b 934 case TIPC_MCAST_MSG:
b97bf3fd 935 case TIPC_NAMED_MSG:{
c4307285 936 tipc_named_msg_err_event cb =
b97bf3fd
PL
937 up_ptr->named_err_cb;
938
4cec72c8 939 tipc_port_unlock(p_ptr);
b97bf3fd
PL
940 if (connected || !cb)
941 break;
942 dseq.type = msg_nametype(msg);
943 dseq.lower = msg_nameinst(msg);
9688243b
AS
944 dseq.upper = (message_type == TIPC_NAMED_MSG)
945 ? dseq.lower : msg_nameupper(msg);
b97bf3fd 946 skb_pull(buf, msg_hdr_sz(msg));
c4307285 947 cb(usr_handle, dref, &buf, msg_data(msg),
b97bf3fd
PL
948 msg_data_sz(msg), msg_errcode(msg), &dseq);
949 break;
950 }
951 }
952 if (buf)
953 buf_discard(buf);
954 buf = next;
955 continue;
956reject:
957 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
958 buf = next;
959 }
960}
961
962/*
963 * port_dispatcher(): Dispatcher for messages destinated
964 * to the tipc_port interface. Called with port locked.
965 */
966
967static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
968{
969 buf->next = NULL;
970 spin_lock_bh(&queue_lock);
971 if (msg_queue_head) {
972 msg_queue_tail->next = buf;
973 msg_queue_tail = buf;
974 } else {
975 msg_queue_tail = msg_queue_head = buf;
4323add6 976 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
b97bf3fd
PL
977 }
978 spin_unlock_bh(&queue_lock);
979 return TIPC_OK;
980}
981
c4307285 982/*
b97bf3fd 983 * Wake up port after congestion: Called with port locked,
c4307285 984 *
b97bf3fd
PL
985 */
986
987static void port_wakeup_sh(unsigned long ref)
988{
989 struct port *p_ptr;
990 struct user_port *up_ptr;
1fc54d8f
SR
991 tipc_continue_event cb = NULL;
992 void *uh = NULL;
b97bf3fd 993
4323add6 994 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
995 if (p_ptr) {
996 up_ptr = p_ptr->user_port;
997 if (up_ptr) {
998 cb = up_ptr->continue_event_cb;
999 uh = up_ptr->usr_handle;
1000 }
4323add6 1001 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1002 }
1003 if (cb)
1004 cb(uh, ref);
1005}
1006
1007
1008static void port_wakeup(struct tipc_port *p_ptr)
1009{
4323add6 1010 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
b97bf3fd
PL
1011}
1012
1013void tipc_acknowledge(u32 ref, u32 ack)
1014{
1015 struct port *p_ptr;
1fc54d8f 1016 struct sk_buff *buf = NULL;
b97bf3fd 1017
4323add6 1018 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1019 if (!p_ptr)
1020 return;
1021 if (p_ptr->publ.connected) {
1022 p_ptr->publ.conn_unacked -= ack;
1023 buf = port_build_proto_msg(port_peerport(p_ptr),
1024 port_peernode(p_ptr),
1025 ref,
1026 tipc_own_addr,
1027 CONN_MANAGER,
1028 CONN_ACK,
c4307285 1029 TIPC_OK,
b97bf3fd
PL
1030 port_out_seqno(p_ptr),
1031 ack);
1032 }
4323add6
PL
1033 tipc_port_unlock(p_ptr);
1034 tipc_net_route_msg(buf);
b97bf3fd
PL
1035}
1036
1037/*
1038 * tipc_createport(): user level call. Will add port to
1039 * registry if non-zero user_ref.
1040 */
1041
c4307285
YH
1042int tipc_createport(u32 user_ref,
1043 void *usr_handle,
1044 unsigned int importance,
1045 tipc_msg_err_event error_cb,
1046 tipc_named_msg_err_event named_error_cb,
1047 tipc_conn_shutdown_event conn_error_cb,
1048 tipc_msg_event msg_cb,
1049 tipc_named_msg_event named_msg_cb,
1050 tipc_conn_msg_event conn_msg_cb,
b97bf3fd
PL
1051 tipc_continue_event continue_event_cb,/* May be zero */
1052 u32 *portref)
1053{
1054 struct user_port *up_ptr;
c4307285 1055 struct port *p_ptr;
7ef43eba 1056 struct tipc_port *tp_ptr;
b97bf3fd
PL
1057 u32 ref;
1058
0da974f4 1059 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
a10bd924 1060 if (!up_ptr) {
a75bf874 1061 warn("Port creation failed, no memory\n");
b97bf3fd
PL
1062 return -ENOMEM;
1063 }
7ef43eba
AS
1064 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
1065 importance, &tp_ptr);
1066 if (ref == 0) {
b97bf3fd
PL
1067 kfree(up_ptr);
1068 return -ENOMEM;
1069 }
7ef43eba 1070 p_ptr = (struct port *)tp_ptr;
b97bf3fd
PL
1071
1072 p_ptr->user_port = up_ptr;
1073 up_ptr->user_ref = user_ref;
1074 up_ptr->usr_handle = usr_handle;
1075 up_ptr->ref = p_ptr->publ.ref;
1076 up_ptr->err_cb = error_cb;
1077 up_ptr->named_err_cb = named_error_cb;
1078 up_ptr->conn_err_cb = conn_error_cb;
1079 up_ptr->msg_cb = msg_cb;
1080 up_ptr->named_msg_cb = named_msg_cb;
1081 up_ptr->conn_msg_cb = conn_msg_cb;
1082 up_ptr->continue_event_cb = continue_event_cb;
1083 INIT_LIST_HEAD(&up_ptr->uport_list);
4323add6 1084 tipc_reg_add_port(up_ptr);
b97bf3fd 1085 *portref = p_ptr->publ.ref;
c4307285 1086 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
4323add6 1087 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1088 return TIPC_OK;
1089}
1090
1091int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1092{
1093 id->ref = ref;
1094 id->node = tipc_own_addr;
1095 return TIPC_OK;
1096}
1097
1098int tipc_portimportance(u32 ref, unsigned int *importance)
1099{
1100 struct port *p_ptr;
c4307285 1101
4323add6 1102 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1103 if (!p_ptr)
1104 return -EINVAL;
1105 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
4cec72c8 1106 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1107 return TIPC_OK;
1108}
1109
1110int tipc_set_portimportance(u32 ref, unsigned int imp)
1111{
1112 struct port *p_ptr;
1113
1114 if (imp > TIPC_CRITICAL_IMPORTANCE)
1115 return -EINVAL;
1116
4323add6 1117 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1118 if (!p_ptr)
1119 return -EINVAL;
1120 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
4cec72c8 1121 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1122 return TIPC_OK;
1123}
1124
1125
1126int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1127{
1128 struct port *p_ptr;
1129 struct publication *publ;
1130 u32 key;
1131 int res = -EINVAL;
1132
4323add6 1133 p_ptr = tipc_port_lock(ref);
d55b4c63
AB
1134 if (!p_ptr)
1135 return -EINVAL;
1136
b97bf3fd
PL
1137 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1138 "lower = %u, upper = %u\n",
1139 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
b97bf3fd
PL
1140 if (p_ptr->publ.connected)
1141 goto exit;
1142 if (seq->lower > seq->upper)
1143 goto exit;
1144 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1145 goto exit;
1146 key = ref + p_ptr->pub_count + 1;
1147 if (key == ref) {
1148 res = -EADDRINUSE;
1149 goto exit;
1150 }
4323add6
PL
1151 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1152 scope, p_ptr->publ.ref, key);
b97bf3fd
PL
1153 if (publ) {
1154 list_add(&publ->pport_list, &p_ptr->publications);
1155 p_ptr->pub_count++;
1156 p_ptr->publ.published = 1;
1157 res = TIPC_OK;
1158 }
1159exit:
4323add6 1160 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1161 return res;
1162}
1163
1164int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1165{
1166 struct port *p_ptr;
1167 struct publication *publ;
1168 struct publication *tpubl;
1169 int res = -EINVAL;
c4307285 1170
4323add6 1171 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1172 if (!p_ptr)
1173 return -EINVAL;
b97bf3fd 1174 if (!seq) {
c4307285 1175 list_for_each_entry_safe(publ, tpubl,
b97bf3fd 1176 &p_ptr->publications, pport_list) {
c4307285 1177 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 1178 publ->ref, publ->key);
b97bf3fd
PL
1179 }
1180 res = TIPC_OK;
1181 } else {
c4307285 1182 list_for_each_entry_safe(publ, tpubl,
b97bf3fd
PL
1183 &p_ptr->publications, pport_list) {
1184 if (publ->scope != scope)
1185 continue;
1186 if (publ->type != seq->type)
1187 continue;
1188 if (publ->lower != seq->lower)
1189 continue;
1190 if (publ->upper != seq->upper)
1191 break;
c4307285 1192 tipc_nametbl_withdraw(publ->type, publ->lower,
4323add6 1193 publ->ref, publ->key);
b97bf3fd
PL
1194 res = TIPC_OK;
1195 break;
1196 }
1197 }
1198 if (list_empty(&p_ptr->publications))
1199 p_ptr->publ.published = 0;
4323add6 1200 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1201 return res;
1202}
1203
1204int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1205{
1206 struct port *p_ptr;
1207 struct tipc_msg *msg;
1208 int res = -EINVAL;
1209
4323add6 1210 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1211 if (!p_ptr)
1212 return -EINVAL;
1213 if (p_ptr->publ.published || p_ptr->publ.connected)
1214 goto exit;
1215 if (!peer->ref)
1216 goto exit;
1217
1218 msg = &p_ptr->publ.phdr;
1219 msg_set_destnode(msg, peer->node);
1220 msg_set_destport(msg, peer->ref);
1221 msg_set_orignode(msg, tipc_own_addr);
1222 msg_set_origport(msg, p_ptr->publ.ref);
1223 msg_set_transp_seqno(msg, 42);
1224 msg_set_type(msg, TIPC_CONN_MSG);
1225 if (!may_route(peer->node))
1226 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1227 else
1228 msg_set_hdr_sz(msg, LONG_H_SIZE);
1229
1230 p_ptr->probing_interval = PROBING_INTERVAL;
1231 p_ptr->probing_state = CONFIRMED;
1232 p_ptr->publ.connected = 1;
1233 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1234
4323add6 1235 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
880b005f 1236 (void *)(unsigned long)ref,
b97bf3fd
PL
1237 (net_ev_handler)port_handle_node_down);
1238 res = TIPC_OK;
1239exit:
4323add6 1240 tipc_port_unlock(p_ptr);
05646c91 1241 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
b97bf3fd
PL
1242 return res;
1243}
1244
0c3141e9
AS
1245/**
1246 * tipc_disconnect_port - disconnect port from peer
1247 *
1248 * Port must be locked.
1249 */
1250
1251int tipc_disconnect_port(struct tipc_port *tp_ptr)
1252{
1253 int res;
1254
1255 if (tp_ptr->connected) {
1256 tp_ptr->connected = 0;
1257 /* let timer expire on it's own to avoid deadlock! */
1258 tipc_nodesub_unsubscribe(
1259 &((struct port *)tp_ptr)->subscription);
1260 res = TIPC_OK;
1261 } else {
1262 res = -ENOTCONN;
1263 }
1264 return res;
1265}
1266
b97bf3fd
PL
1267/*
1268 * tipc_disconnect(): Disconnect port form peer.
1269 * This is a node local operation.
1270 */
1271
1272int tipc_disconnect(u32 ref)
1273{
1274 struct port *p_ptr;
0c3141e9 1275 int res;
b97bf3fd 1276
4323add6 1277 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1278 if (!p_ptr)
1279 return -EINVAL;
0c3141e9 1280 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
4323add6 1281 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1282 return res;
1283}
1284
1285/*
1286 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1287 */
1288int tipc_shutdown(u32 ref)
1289{
1290 struct port *p_ptr;
1fc54d8f 1291 struct sk_buff *buf = NULL;
b97bf3fd 1292
4323add6 1293 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1294 if (!p_ptr)
1295 return -EINVAL;
1296
1297 if (p_ptr->publ.connected) {
1298 u32 imp = msg_importance(&p_ptr->publ.phdr);
1299 if (imp < TIPC_CRITICAL_IMPORTANCE)
1300 imp++;
1301 buf = port_build_proto_msg(port_peerport(p_ptr),
1302 port_peernode(p_ptr),
1303 ref,
1304 tipc_own_addr,
1305 imp,
1306 TIPC_CONN_MSG,
c4307285 1307 TIPC_CONN_SHUTDOWN,
b97bf3fd
PL
1308 port_out_seqno(p_ptr),
1309 0);
1310 }
4323add6
PL
1311 tipc_port_unlock(p_ptr);
1312 tipc_net_route_msg(buf);
b97bf3fd
PL
1313 return tipc_disconnect(ref);
1314}
1315
1316int tipc_isconnected(u32 ref, int *isconnected)
1317{
1318 struct port *p_ptr;
c4307285 1319
4323add6 1320 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1321 if (!p_ptr)
1322 return -EINVAL;
1323 *isconnected = p_ptr->publ.connected;
4323add6 1324 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1325 return TIPC_OK;
1326}
1327
1328int tipc_peer(u32 ref, struct tipc_portid *peer)
1329{
1330 struct port *p_ptr;
1331 int res;
c4307285 1332
4323add6 1333 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1334 if (!p_ptr)
1335 return -EINVAL;
1336 if (p_ptr->publ.connected) {
1337 peer->ref = port_peerport(p_ptr);
1338 peer->node = port_peernode(p_ptr);
1339 res = TIPC_OK;
1340 } else
1341 res = -ENOTCONN;
4323add6 1342 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1343 return res;
1344}
1345
1346int tipc_ref_valid(u32 ref)
1347{
1348 /* Works irrespective of type */
4323add6 1349 return !!tipc_ref_deref(ref);
b97bf3fd
PL
1350}
1351
1352
1353/*
4323add6 1354 * tipc_port_recv_sections(): Concatenate and deliver sectioned
b97bf3fd
PL
1355 * message for this node.
1356 */
1357
4323add6 1358int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
b97bf3fd
PL
1359 struct iovec const *msg_sect)
1360{
1361 struct sk_buff *buf;
1362 int res;
c4307285 1363
b97bf3fd
PL
1364 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1365 MAX_MSG_SIZE, !sender->user_port, &buf);
1366 if (likely(buf))
4323add6 1367 tipc_port_recv_msg(buf);
b97bf3fd
PL
1368 return res;
1369}
1370
1371/**
1372 * tipc_send - send message sections on connection
1373 */
1374
1375int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1376{
1377 struct port *p_ptr;
1378 u32 destnode;
1379 int res;
1380
4323add6 1381 p_ptr = tipc_port_deref(ref);
b97bf3fd
PL
1382 if (!p_ptr || !p_ptr->publ.connected)
1383 return -EINVAL;
1384
1385 p_ptr->publ.congested = 1;
4323add6 1386 if (!tipc_port_congested(p_ptr)) {
b97bf3fd
PL
1387 destnode = port_peernode(p_ptr);
1388 if (likely(destnode != tipc_own_addr))
4323add6
PL
1389 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1390 destnode);
b97bf3fd 1391 else
4323add6 1392 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
b97bf3fd
PL
1393
1394 if (likely(res != -ELINKCONG)) {
1395 port_incr_out_seqno(p_ptr);
1396 p_ptr->publ.congested = 0;
1397 p_ptr->sent++;
1398 return res;
1399 }
1400 }
1401 if (port_unreliable(p_ptr)) {
1402 p_ptr->publ.congested = 0;
1403 /* Just calculate msg length and return */
1404 return msg_calc_data_size(msg_sect, num_sect);
1405 }
1406 return -ELINKCONG;
1407}
1408
c4307285 1409/**
b97bf3fd
PL
1410 * tipc_send_buf - send message buffer on connection
1411 */
1412
1413int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1414{
1415 struct port *p_ptr;
1416 struct tipc_msg *msg;
1417 u32 destnode;
1418 u32 hsz;
1419 u32 sz;
1420 u32 res;
c4307285 1421
4323add6 1422 p_ptr = tipc_port_deref(ref);
b97bf3fd
PL
1423 if (!p_ptr || !p_ptr->publ.connected)
1424 return -EINVAL;
1425
1426 msg = &p_ptr->publ.phdr;
1427 hsz = msg_hdr_sz(msg);
1428 sz = hsz + dsz;
1429 msg_set_size(msg, sz);
1430 if (skb_cow(buf, hsz))
1431 return -ENOMEM;
1432
1433 skb_push(buf, hsz);
27d7ff46 1434 skb_copy_to_linear_data(buf, msg, hsz);
b97bf3fd
PL
1435 destnode = msg_destnode(msg);
1436 p_ptr->publ.congested = 1;
4323add6 1437 if (!tipc_port_congested(p_ptr)) {
b97bf3fd
PL
1438 if (likely(destnode != tipc_own_addr))
1439 res = tipc_send_buf_fast(buf, destnode);
1440 else {
4323add6 1441 tipc_port_recv_msg(buf);
b97bf3fd
PL
1442 res = sz;
1443 }
1444 if (likely(res != -ELINKCONG)) {
1445 port_incr_out_seqno(p_ptr);
1446 p_ptr->sent++;
1447 p_ptr->publ.congested = 0;
1448 return res;
1449 }
1450 }
1451 if (port_unreliable(p_ptr)) {
1452 p_ptr->publ.congested = 0;
1453 return dsz;
1454 }
1455 return -ELINKCONG;
1456}
1457
1458/**
1459 * tipc_forward2name - forward message sections to port name
1460 */
1461
c4307285
YH
1462int tipc_forward2name(u32 ref,
1463 struct tipc_name const *name,
b97bf3fd 1464 u32 domain,
c4307285 1465 u32 num_sect,
b97bf3fd 1466 struct iovec const *msg_sect,
c4307285 1467 struct tipc_portid const *orig,
b97bf3fd
PL
1468 unsigned int importance)
1469{
1470 struct port *p_ptr;
1471 struct tipc_msg *msg;
1472 u32 destnode = domain;
1473 u32 destport = 0;
1474 int res;
1475
4323add6 1476 p_ptr = tipc_port_deref(ref);
b97bf3fd
PL
1477 if (!p_ptr || p_ptr->publ.connected)
1478 return -EINVAL;
1479
1480 msg = &p_ptr->publ.phdr;
1481 msg_set_type(msg, TIPC_NAMED_MSG);
1482 msg_set_orignode(msg, orig->node);
1483 msg_set_origport(msg, orig->ref);
1484 msg_set_hdr_sz(msg, LONG_H_SIZE);
1485 msg_set_nametype(msg, name->type);
1486 msg_set_nameinst(msg, name->instance);
1487 msg_set_lookup_scope(msg, addr_scope(domain));
1488 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1489 msg_set_importance(msg,importance);
4323add6 1490 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
1491 msg_set_destnode(msg, destnode);
1492 msg_set_destport(msg, destport);
1493
1494 if (likely(destport || destnode)) {
1495 p_ptr->sent++;
1496 if (likely(destnode == tipc_own_addr))
4323add6 1497 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
c4307285 1498 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
4323add6 1499 destnode);
b97bf3fd
PL
1500 if (likely(res != -ELINKCONG))
1501 return res;
1502 if (port_unreliable(p_ptr)) {
1503 /* Just calculate msg length and return */
1504 return msg_calc_data_size(msg_sect, num_sect);
1505 }
1506 return -ELINKCONG;
1507 }
c4307285 1508 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
4323add6 1509 TIPC_ERR_NO_NAME);
b97bf3fd
PL
1510}
1511
1512/**
1513 * tipc_send2name - send message sections to port name
1514 */
1515
c4307285 1516int tipc_send2name(u32 ref,
b97bf3fd 1517 struct tipc_name const *name,
c4307285
YH
1518 unsigned int domain,
1519 unsigned int num_sect,
b97bf3fd
PL
1520 struct iovec const *msg_sect)
1521{
1522 struct tipc_portid orig;
1523
1524 orig.ref = ref;
1525 orig.node = tipc_own_addr;
1526 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1527 TIPC_PORT_IMPORTANCE);
1528}
1529
c4307285 1530/**
b97bf3fd
PL
1531 * tipc_forward_buf2name - forward message buffer to port name
1532 */
1533
1534int tipc_forward_buf2name(u32 ref,
1535 struct tipc_name const *name,
1536 u32 domain,
1537 struct sk_buff *buf,
1538 unsigned int dsz,
1539 struct tipc_portid const *orig,
1540 unsigned int importance)
1541{
1542 struct port *p_ptr;
1543 struct tipc_msg *msg;
1544 u32 destnode = domain;
1545 u32 destport = 0;
1546 int res;
1547
4323add6 1548 p_ptr = (struct port *)tipc_ref_deref(ref);
b97bf3fd
PL
1549 if (!p_ptr || p_ptr->publ.connected)
1550 return -EINVAL;
1551
1552 msg = &p_ptr->publ.phdr;
1553 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1554 msg_set_importance(msg, importance);
1555 msg_set_type(msg, TIPC_NAMED_MSG);
1556 msg_set_orignode(msg, orig->node);
1557 msg_set_origport(msg, orig->ref);
1558 msg_set_nametype(msg, name->type);
1559 msg_set_nameinst(msg, name->instance);
1560 msg_set_lookup_scope(msg, addr_scope(domain));
1561 msg_set_hdr_sz(msg, LONG_H_SIZE);
1562 msg_set_size(msg, LONG_H_SIZE + dsz);
4323add6 1563 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
1564 msg_set_destnode(msg, destnode);
1565 msg_set_destport(msg, destport);
1566 msg_dbg(msg, "forw2name ==> ");
1567 if (skb_cow(buf, LONG_H_SIZE))
1568 return -ENOMEM;
1569 skb_push(buf, LONG_H_SIZE);
27d7ff46 1570 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
b97bf3fd
PL
1571 msg_dbg(buf_msg(buf),"PREP:");
1572 if (likely(destport || destnode)) {
1573 p_ptr->sent++;
1574 if (destnode == tipc_own_addr)
4323add6 1575 return tipc_port_recv_msg(buf);
b97bf3fd
PL
1576 res = tipc_send_buf_fast(buf, destnode);
1577 if (likely(res != -ELINKCONG))
1578 return res;
1579 if (port_unreliable(p_ptr))
1580 return dsz;
1581 return -ELINKCONG;
1582 }
1583 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1584}
1585
c4307285 1586/**
b97bf3fd
PL
1587 * tipc_send_buf2name - send message buffer to port name
1588 */
1589
c4307285
YH
1590int tipc_send_buf2name(u32 ref,
1591 struct tipc_name const *dest,
b97bf3fd 1592 u32 domain,
c4307285 1593 struct sk_buff *buf,
b97bf3fd
PL
1594 unsigned int dsz)
1595{
1596 struct tipc_portid orig;
1597
1598 orig.ref = ref;
1599 orig.node = tipc_own_addr;
1600 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1601 TIPC_PORT_IMPORTANCE);
1602}
1603
c4307285 1604/**
b97bf3fd
PL
1605 * tipc_forward2port - forward message sections to port identity
1606 */
1607
1608int tipc_forward2port(u32 ref,
1609 struct tipc_portid const *dest,
c4307285 1610 unsigned int num_sect,
b97bf3fd 1611 struct iovec const *msg_sect,
c4307285 1612 struct tipc_portid const *orig,
b97bf3fd
PL
1613 unsigned int importance)
1614{
1615 struct port *p_ptr;
1616 struct tipc_msg *msg;
1617 int res;
1618
4323add6 1619 p_ptr = tipc_port_deref(ref);
b97bf3fd
PL
1620 if (!p_ptr || p_ptr->publ.connected)
1621 return -EINVAL;
1622
1623 msg = &p_ptr->publ.phdr;
1624 msg_set_type(msg, TIPC_DIRECT_MSG);
1625 msg_set_orignode(msg, orig->node);
1626 msg_set_origport(msg, orig->ref);
1627 msg_set_destnode(msg, dest->node);
1628 msg_set_destport(msg, dest->ref);
1629 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1630 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1631 msg_set_importance(msg, importance);
1632 p_ptr->sent++;
1633 if (dest->node == tipc_own_addr)
4323add6
PL
1634 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1635 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
b97bf3fd
PL
1636 if (likely(res != -ELINKCONG))
1637 return res;
1638 if (port_unreliable(p_ptr)) {
1639 /* Just calculate msg length and return */
1640 return msg_calc_data_size(msg_sect, num_sect);
1641 }
1642 return -ELINKCONG;
1643}
1644
c4307285
YH
1645/**
1646 * tipc_send2port - send message sections to port identity
b97bf3fd
PL
1647 */
1648
c4307285 1649int tipc_send2port(u32 ref,
b97bf3fd 1650 struct tipc_portid const *dest,
c4307285 1651 unsigned int num_sect,
b97bf3fd
PL
1652 struct iovec const *msg_sect)
1653{
1654 struct tipc_portid orig;
1655
1656 orig.ref = ref;
1657 orig.node = tipc_own_addr;
c4307285 1658 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
b97bf3fd
PL
1659 TIPC_PORT_IMPORTANCE);
1660}
1661
c4307285 1662/**
b97bf3fd
PL
1663 * tipc_forward_buf2port - forward message buffer to port identity
1664 */
1665int tipc_forward_buf2port(u32 ref,
1666 struct tipc_portid const *dest,
1667 struct sk_buff *buf,
1668 unsigned int dsz,
1669 struct tipc_portid const *orig,
1670 unsigned int importance)
1671{
1672 struct port *p_ptr;
1673 struct tipc_msg *msg;
1674 int res;
1675
4323add6 1676 p_ptr = (struct port *)tipc_ref_deref(ref);
b97bf3fd
PL
1677 if (!p_ptr || p_ptr->publ.connected)
1678 return -EINVAL;
1679
1680 msg = &p_ptr->publ.phdr;
1681 msg_set_type(msg, TIPC_DIRECT_MSG);
1682 msg_set_orignode(msg, orig->node);
1683 msg_set_origport(msg, orig->ref);
1684 msg_set_destnode(msg, dest->node);
1685 msg_set_destport(msg, dest->ref);
1686 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1687 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1688 msg_set_importance(msg, importance);
1689 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1690 if (skb_cow(buf, DIR_MSG_H_SIZE))
1691 return -ENOMEM;
1692
1693 skb_push(buf, DIR_MSG_H_SIZE);
27d7ff46 1694 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
b97bf3fd
PL
1695 msg_dbg(msg, "buf2port: ");
1696 p_ptr->sent++;
1697 if (dest->node == tipc_own_addr)
4323add6 1698 return tipc_port_recv_msg(buf);
b97bf3fd
PL
1699 res = tipc_send_buf_fast(buf, dest->node);
1700 if (likely(res != -ELINKCONG))
1701 return res;
1702 if (port_unreliable(p_ptr))
1703 return dsz;
1704 return -ELINKCONG;
1705}
1706
c4307285 1707/**
b97bf3fd
PL
1708 * tipc_send_buf2port - send message buffer to port identity
1709 */
1710
c4307285 1711int tipc_send_buf2port(u32 ref,
b97bf3fd 1712 struct tipc_portid const *dest,
c4307285 1713 struct sk_buff *buf,
b97bf3fd
PL
1714 unsigned int dsz)
1715{
1716 struct tipc_portid orig;
1717
1718 orig.ref = ref;
1719 orig.node = tipc_own_addr;
c4307285 1720 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
b97bf3fd
PL
1721 TIPC_PORT_IMPORTANCE);
1722}
1723