]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/tipc/port.c
[NETFILTER]: Fix build with CONFIG_NETFILTER=y/m on IA64
[mirror_ubuntu-artful-kernel.git] / net / tipc / port.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/port.c: TIPC port code
3 *
593a5f22 4 * Copyright (c) 1992-2006, Ericsson AB
b97bf3fd 5 * Copyright (c) 2004-2005, 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"
44#include "port.h"
45#include "name_table.h"
46#include "user_reg.h"
47#include "msg.h"
48#include "bcast.h"
49
50/* Connection management: */
51#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
52#define CONFIRMED 0
53#define PROBING 1
54
55#define MAX_REJECT_SIZE 1024
56
1fc54d8f
SR
57static struct sk_buff *msg_queue_head = NULL;
58static struct sk_buff *msg_queue_tail = NULL;
b97bf3fd 59
4323add6 60spinlock_t tipc_port_list_lock = SPIN_LOCK_UNLOCKED;
b97bf3fd
PL
61static spinlock_t queue_lock = SPIN_LOCK_UNLOCKED;
62
4323add6 63static LIST_HEAD(ports);
b97bf3fd
PL
64static void port_handle_node_down(unsigned long ref);
65static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
66static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
67static void port_timeout(unsigned long ref);
68
69
05790c64 70static u32 port_peernode(struct port *p_ptr)
b97bf3fd
PL
71{
72 return msg_destnode(&p_ptr->publ.phdr);
73}
74
05790c64 75static u32 port_peerport(struct port *p_ptr)
b97bf3fd
PL
76{
77 return msg_destport(&p_ptr->publ.phdr);
78}
79
05790c64 80static u32 port_out_seqno(struct port *p_ptr)
b97bf3fd
PL
81{
82 return msg_transp_seqno(&p_ptr->publ.phdr);
83}
84
05790c64 85static void port_incr_out_seqno(struct port *p_ptr)
b97bf3fd
PL
86{
87 struct tipc_msg *m = &p_ptr->publ.phdr;
88
89 if (likely(!msg_routed(m)))
90 return;
91 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
92}
93
94/**
95 * tipc_multicast - send a multicast message to local and remote destinations
96 */
97
98int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
99 u32 num_sect, struct iovec const *msg_sect)
100{
101 struct tipc_msg *hdr;
102 struct sk_buff *buf;
103 struct sk_buff *ibuf = NULL;
104 struct port_list dports = {0, NULL, };
4323add6 105 struct port *oport = tipc_port_deref(ref);
b97bf3fd
PL
106 int ext_targets;
107 int res;
108
109 if (unlikely(!oport))
110 return -EINVAL;
111
112 /* Create multicast message */
113
114 hdr = &oport->publ.phdr;
115 msg_set_type(hdr, TIPC_MCAST_MSG);
116 msg_set_nametype(hdr, seq->type);
117 msg_set_namelower(hdr, seq->lower);
118 msg_set_nameupper(hdr, seq->upper);
119 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
120 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
121 !oport->user_port, &buf);
122 if (unlikely(!buf))
123 return res;
124
125 /* Figure out where to send multicast message */
126
4323add6
PL
127 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
128 TIPC_NODE_SCOPE, &dports);
b97bf3fd
PL
129
130 /* Send message to destinations (duplicate it only if necessary) */
131
132 if (ext_targets) {
133 if (dports.count != 0) {
134 ibuf = skb_copy(buf, GFP_ATOMIC);
135 if (ibuf == NULL) {
4323add6 136 tipc_port_list_free(&dports);
b97bf3fd
PL
137 buf_discard(buf);
138 return -ENOMEM;
139 }
140 }
4323add6 141 res = tipc_bclink_send_msg(buf);
b97bf3fd
PL
142 if ((res < 0) && (dports.count != 0)) {
143 buf_discard(ibuf);
144 }
145 } else {
146 ibuf = buf;
147 }
148
149 if (res >= 0) {
150 if (ibuf)
4323add6 151 tipc_port_recv_mcast(ibuf, &dports);
b97bf3fd 152 } else {
4323add6 153 tipc_port_list_free(&dports);
b97bf3fd
PL
154 }
155 return res;
156}
157
158/**
4323add6 159 * tipc_port_recv_mcast - deliver multicast message to all destination ports
b97bf3fd
PL
160 *
161 * If there is no port list, perform a lookup to create one
162 */
163
4323add6 164void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
b97bf3fd
PL
165{
166 struct tipc_msg* msg;
167 struct port_list dports = {0, NULL, };
168 struct port_list *item = dp;
169 int cnt = 0;
170
171 assert(buf);
172 msg = buf_msg(buf);
173
174 /* Create destination port list, if one wasn't supplied */
175
176 if (dp == NULL) {
4323add6 177 tipc_nametbl_mc_translate(msg_nametype(msg),
b97bf3fd
PL
178 msg_namelower(msg),
179 msg_nameupper(msg),
180 TIPC_CLUSTER_SCOPE,
181 &dports);
182 item = dp = &dports;
183 }
184
185 /* Deliver a copy of message to each destination port */
186
187 if (dp->count != 0) {
188 if (dp->count == 1) {
189 msg_set_destport(msg, dp->ports[0]);
4323add6
PL
190 tipc_port_recv_msg(buf);
191 tipc_port_list_free(dp);
b97bf3fd
PL
192 return;
193 }
194 for (; cnt < dp->count; cnt++) {
195 int index = cnt % PLSIZE;
196 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
197
198 if (b == NULL) {
199 warn("Buffer allocation failure\n");
200 msg_dbg(msg, "LOST:");
201 goto exit;
202 }
203 if ((index == 0) && (cnt != 0)) {
204 item = item->next;
205 }
206 msg_set_destport(buf_msg(b),item->ports[index]);
4323add6 207 tipc_port_recv_msg(b);
b97bf3fd
PL
208 }
209 }
210exit:
211 buf_discard(buf);
4323add6 212 tipc_port_list_free(dp);
b97bf3fd
PL
213}
214
215/**
216 * tipc_createport_raw - create a native TIPC port
217 *
218 * Returns local port reference
219 */
220
221u32 tipc_createport_raw(void *usr_handle,
222 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
223 void (*wakeup)(struct tipc_port *),
224 const u32 importance)
225{
226 struct port *p_ptr;
227 struct tipc_msg *msg;
228 u32 ref;
229
230 p_ptr = kmalloc(sizeof(*p_ptr), GFP_ATOMIC);
231 if (p_ptr == NULL) {
232 warn("Memory squeeze; failed to create port\n");
233 return 0;
234 }
235 memset(p_ptr, 0, sizeof(*p_ptr));
4323add6 236 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
b97bf3fd
PL
237 if (!ref) {
238 warn("Reference Table Exhausted\n");
239 kfree(p_ptr);
240 return 0;
241 }
242
4323add6 243 tipc_port_lock(ref);
b97bf3fd
PL
244 p_ptr->publ.ref = ref;
245 msg = &p_ptr->publ.phdr;
246 msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
247 msg_set_orignode(msg, tipc_own_addr);
248 msg_set_prevnode(msg, tipc_own_addr);
249 msg_set_origport(msg, ref);
250 msg_set_importance(msg,importance);
251 p_ptr->last_in_seqno = 41;
252 p_ptr->sent = 1;
253 p_ptr->publ.usr_handle = usr_handle;
254 INIT_LIST_HEAD(&p_ptr->wait_list);
255 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
1fc54d8f 256 p_ptr->congested_link = NULL;
b97bf3fd
PL
257 p_ptr->max_pkt = MAX_PKT_DEFAULT;
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
PL
266 spin_unlock_bh(&tipc_port_list_lock);
267 tipc_port_unlock(p_ptr);
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);
b97bf3fd
PL
278 if (!p_ptr)
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'
307 *
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;
341
4323add6 342 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
343 if (!p_ptr)
344 return -EINVAL;
345 *isunreliable = port_unreliable(p_ptr);
346 spin_unlock_bh(p_ptr->publ.lock);
347 return TIPC_OK;
348}
349
350int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
351{
352 struct port *p_ptr;
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;
370
4323add6 371 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
372 if (!p_ptr)
373 return -EINVAL;
374 *isunrejectable = port_unreturnable(p_ptr);
375 spin_unlock_bh(p_ptr->publ.lock);
376 return TIPC_OK;
377}
378
379int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
380{
381 struct port *p_ptr;
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
391/*
392 * port_build_proto_msg(): build a port level protocol
393 * or a connection abortion message. Called with
394 * tipc_port lock on.
395 */
396static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
397 u32 origport, u32 orignode,
398 u32 usr, u32 type, u32 err,
399 u32 seqno, u32 ack)
400{
401 struct sk_buff *buf;
402 struct tipc_msg *msg;
403
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
419int tipc_set_msg_option(struct tipc_port *tp_ptr, const char *opt, const u32 sz)
420{
421 msg_expand(&tp_ptr->phdr, msg_destnode(&tp_ptr->phdr));
422 msg_set_options(&tp_ptr->phdr, opt, sz);
423 return TIPC_OK;
424}
425
426int tipc_reject_msg(struct sk_buff *buf, u32 err)
427{
428 struct tipc_msg *msg = buf_msg(buf);
429 struct sk_buff *rbuf;
430 struct tipc_msg *rmsg;
431 int hdr_sz;
432 u32 imp = msg_importance(msg);
433 u32 data_sz = msg_data_sz(msg);
434
435 if (data_sz > MAX_REJECT_SIZE)
436 data_sz = MAX_REJECT_SIZE;
437 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
438 imp++;
439 msg_dbg(msg, "port->rej: ");
440
441 /* discard rejected message if it shouldn't be returned to sender */
442 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
443 buf_discard(buf);
444 return data_sz;
445 }
446
447 /* construct rejected message */
448 if (msg_mcast(msg))
449 hdr_sz = MCAST_H_SIZE;
450 else
451 hdr_sz = LONG_H_SIZE;
452 rbuf = buf_acquire(data_sz + hdr_sz);
453 if (rbuf == NULL) {
454 buf_discard(buf);
455 return data_sz;
456 }
457 rmsg = buf_msg(rbuf);
458 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
459 msg_set_destport(rmsg, msg_origport(msg));
460 msg_set_prevnode(rmsg, tipc_own_addr);
461 msg_set_origport(rmsg, msg_destport(msg));
462 if (msg_short(msg))
463 msg_set_orignode(rmsg, tipc_own_addr);
464 else
465 msg_set_orignode(rmsg, msg_destnode(msg));
466 msg_set_size(rmsg, data_sz + hdr_sz);
467 msg_set_nametype(rmsg, msg_nametype(msg));
468 msg_set_nameinst(rmsg, msg_nameinst(msg));
469 memcpy(rbuf->data + hdr_sz, msg_data(msg), data_sz);
470
471 /* send self-abort message when rejecting on a connected port */
472 if (msg_connected(msg)) {
1fc54d8f 473 struct sk_buff *abuf = NULL;
4323add6 474 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd
PL
475
476 if (p_ptr) {
477 if (p_ptr->publ.connected)
478 abuf = port_build_self_abort_msg(p_ptr, err);
4323add6 479 tipc_port_unlock(p_ptr);
b97bf3fd 480 }
4323add6 481 tipc_net_route_msg(abuf);
b97bf3fd
PL
482 }
483
484 /* send rejected message */
485 buf_discard(buf);
4323add6 486 tipc_net_route_msg(rbuf);
b97bf3fd
PL
487 return data_sz;
488}
489
4323add6
PL
490int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
491 struct iovec const *msg_sect, u32 num_sect,
492 int err)
b97bf3fd
PL
493{
494 struct sk_buff *buf;
495 int res;
496
497 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
498 !p_ptr->user_port, &buf);
499 if (!buf)
500 return res;
501
502 return tipc_reject_msg(buf, err);
503}
504
505static void port_timeout(unsigned long ref)
506{
4323add6 507 struct port *p_ptr = tipc_port_lock(ref);
1fc54d8f 508 struct sk_buff *buf = NULL;
b97bf3fd
PL
509
510 if (!p_ptr || !p_ptr->publ.connected)
511 return;
512
513 /* Last probe answered ? */
514 if (p_ptr->probing_state == PROBING) {
515 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
516 } else {
517 buf = port_build_proto_msg(port_peerport(p_ptr),
518 port_peernode(p_ptr),
519 p_ptr->publ.ref,
520 tipc_own_addr,
521 CONN_MANAGER,
522 CONN_PROBE,
523 TIPC_OK,
524 port_out_seqno(p_ptr),
525 0);
526 port_incr_out_seqno(p_ptr);
527 p_ptr->probing_state = PROBING;
528 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
529 }
4323add6
PL
530 tipc_port_unlock(p_ptr);
531 tipc_net_route_msg(buf);
b97bf3fd
PL
532}
533
534
535static void port_handle_node_down(unsigned long ref)
536{
4323add6 537 struct port *p_ptr = tipc_port_lock(ref);
1fc54d8f 538 struct sk_buff* buf = NULL;
b97bf3fd
PL
539
540 if (!p_ptr)
541 return;
542 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
4323add6
PL
543 tipc_port_unlock(p_ptr);
544 tipc_net_route_msg(buf);
b97bf3fd
PL
545}
546
547
548static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
549{
550 u32 imp = msg_importance(&p_ptr->publ.phdr);
551
552 if (!p_ptr->publ.connected)
1fc54d8f 553 return NULL;
b97bf3fd
PL
554 if (imp < TIPC_CRITICAL_IMPORTANCE)
555 imp++;
556 return port_build_proto_msg(p_ptr->publ.ref,
557 tipc_own_addr,
558 port_peerport(p_ptr),
559 port_peernode(p_ptr),
560 imp,
561 TIPC_CONN_MSG,
562 err,
563 p_ptr->last_in_seqno + 1,
564 0);
565}
566
567
568static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
569{
570 u32 imp = msg_importance(&p_ptr->publ.phdr);
571
572 if (!p_ptr->publ.connected)
1fc54d8f 573 return NULL;
b97bf3fd
PL
574 if (imp < TIPC_CRITICAL_IMPORTANCE)
575 imp++;
576 return port_build_proto_msg(port_peerport(p_ptr),
577 port_peernode(p_ptr),
578 p_ptr->publ.ref,
579 tipc_own_addr,
580 imp,
581 TIPC_CONN_MSG,
582 err,
583 port_out_seqno(p_ptr),
584 0);
585}
586
4323add6 587void tipc_port_recv_proto_msg(struct sk_buff *buf)
b97bf3fd
PL
588{
589 struct tipc_msg *msg = buf_msg(buf);
4323add6 590 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
b97bf3fd 591 u32 err = TIPC_OK;
1fc54d8f
SR
592 struct sk_buff *r_buf = NULL;
593 struct sk_buff *abort_buf = NULL;
b97bf3fd
PL
594
595 msg_dbg(msg, "PORT<RECV<:");
596
597 if (!p_ptr) {
598 err = TIPC_ERR_NO_PORT;
599 } else if (p_ptr->publ.connected) {
600 if (port_peernode(p_ptr) != msg_orignode(msg))
601 err = TIPC_ERR_NO_PORT;
602 if (port_peerport(p_ptr) != msg_origport(msg))
603 err = TIPC_ERR_NO_PORT;
604 if (!err && msg_routed(msg)) {
605 u32 seqno = msg_transp_seqno(msg);
606 u32 myno = ++p_ptr->last_in_seqno;
607 if (seqno != myno) {
608 err = TIPC_ERR_NO_PORT;
609 abort_buf = port_build_self_abort_msg(p_ptr, err);
610 }
611 }
612 if (msg_type(msg) == CONN_ACK) {
4323add6 613 int wakeup = tipc_port_congested(p_ptr) &&
b97bf3fd
PL
614 p_ptr->publ.congested &&
615 p_ptr->wakeup;
616 p_ptr->acked += msg_msgcnt(msg);
4323add6 617 if (tipc_port_congested(p_ptr))
b97bf3fd
PL
618 goto exit;
619 p_ptr->publ.congested = 0;
620 if (!wakeup)
621 goto exit;
622 p_ptr->wakeup(&p_ptr->publ);
623 goto exit;
624 }
625 } else if (p_ptr->publ.published) {
626 err = TIPC_ERR_NO_PORT;
627 }
628 if (err) {
629 r_buf = port_build_proto_msg(msg_origport(msg),
630 msg_orignode(msg),
631 msg_destport(msg),
632 tipc_own_addr,
633 DATA_HIGH,
634 TIPC_CONN_MSG,
635 err,
636 0,
637 0);
638 goto exit;
639 }
640
641 /* All is fine */
642 if (msg_type(msg) == CONN_PROBE) {
643 r_buf = port_build_proto_msg(msg_origport(msg),
644 msg_orignode(msg),
645 msg_destport(msg),
646 tipc_own_addr,
647 CONN_MANAGER,
648 CONN_PROBE_REPLY,
649 TIPC_OK,
650 port_out_seqno(p_ptr),
651 0);
652 }
653 p_ptr->probing_state = CONFIRMED;
654 port_incr_out_seqno(p_ptr);
655exit:
656 if (p_ptr)
4323add6
PL
657 tipc_port_unlock(p_ptr);
658 tipc_net_route_msg(r_buf);
659 tipc_net_route_msg(abort_buf);
b97bf3fd
PL
660 buf_discard(buf);
661}
662
663static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
664{
665 struct publication *publ;
666
667 if (full_id)
668 tipc_printf(buf, "<%u.%u.%u:%u>:",
669 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
670 tipc_node(tipc_own_addr), p_ptr->publ.ref);
671 else
672 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
673
674 if (p_ptr->publ.connected) {
675 u32 dport = port_peerport(p_ptr);
676 u32 destnode = port_peernode(p_ptr);
677
678 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
679 tipc_zone(destnode), tipc_cluster(destnode),
680 tipc_node(destnode), dport);
681 if (p_ptr->publ.conn_type != 0)
682 tipc_printf(buf, " via {%u,%u}",
683 p_ptr->publ.conn_type,
684 p_ptr->publ.conn_instance);
685 }
686 else if (p_ptr->publ.published) {
687 tipc_printf(buf, " bound to");
688 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
689 if (publ->lower == publ->upper)
690 tipc_printf(buf, " {%u,%u}", publ->type,
691 publ->lower);
692 else
693 tipc_printf(buf, " {%u,%u,%u}", publ->type,
694 publ->lower, publ->upper);
695 }
696 }
697 tipc_printf(buf, "\n");
698}
699
700#define MAX_PORT_QUERY 32768
701
4323add6 702struct sk_buff *tipc_port_get_ports(void)
b97bf3fd
PL
703{
704 struct sk_buff *buf;
705 struct tlv_desc *rep_tlv;
706 struct print_buf pb;
707 struct port *p_ptr;
708 int str_len;
709
4323add6 710 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
b97bf3fd
PL
711 if (!buf)
712 return NULL;
713 rep_tlv = (struct tlv_desc *)buf->data;
714
4323add6
PL
715 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
716 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
717 list_for_each_entry(p_ptr, &ports, port_list) {
718 spin_lock_bh(p_ptr->publ.lock);
719 port_print(p_ptr, &pb, 0);
720 spin_unlock_bh(p_ptr->publ.lock);
721 }
4323add6
PL
722 spin_unlock_bh(&tipc_port_list_lock);
723 str_len = tipc_printbuf_validate(&pb);
b97bf3fd
PL
724
725 skb_put(buf, TLV_SPACE(str_len));
726 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
727
728 return buf;
729}
730
731#if 0
732
733#define MAX_PORT_STATS 2000
734
735struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
736{
737 u32 ref;
738 struct port *p_ptr;
739 struct sk_buff *buf;
740 struct tlv_desc *rep_tlv;
741 struct print_buf pb;
742 int str_len;
743
744 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
745 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
746
747 ref = *(u32 *)TLV_DATA(req_tlv_area);
748 ref = ntohl(ref);
749
4323add6 750 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
751 if (!p_ptr)
752 return cfg_reply_error_string("port not found");
753
4323add6 754 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
b97bf3fd 755 if (!buf) {
4323add6 756 tipc_port_unlock(p_ptr);
b97bf3fd
PL
757 return NULL;
758 }
759 rep_tlv = (struct tlv_desc *)buf->data;
760
4323add6 761 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
b97bf3fd
PL
762 port_print(p_ptr, &pb, 1);
763 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
4323add6
PL
764 tipc_port_unlock(p_ptr);
765 str_len = tipc_printbuf_validate(&pb);
b97bf3fd
PL
766
767 skb_put(buf, TLV_SPACE(str_len));
768 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
769
770 return buf;
771}
772
773#endif
774
4323add6 775void tipc_port_reinit(void)
b97bf3fd
PL
776{
777 struct port *p_ptr;
778 struct tipc_msg *msg;
779
4323add6 780 spin_lock_bh(&tipc_port_list_lock);
b97bf3fd
PL
781 list_for_each_entry(p_ptr, &ports, port_list) {
782 msg = &p_ptr->publ.phdr;
783 if (msg_orignode(msg) == tipc_own_addr)
784 break;
785 msg_set_orignode(msg, tipc_own_addr);
786 }
4323add6 787 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
788}
789
790
791/*
792 * port_dispatcher_sigh(): Signal handler for messages destinated
793 * to the tipc_port interface.
794 */
795
796static void port_dispatcher_sigh(void *dummy)
797{
798 struct sk_buff *buf;
799
800 spin_lock_bh(&queue_lock);
801 buf = msg_queue_head;
1fc54d8f 802 msg_queue_head = NULL;
b97bf3fd
PL
803 spin_unlock_bh(&queue_lock);
804
805 while (buf) {
806 struct port *p_ptr;
807 struct user_port *up_ptr;
808 struct tipc_portid orig;
809 struct tipc_name_seq dseq;
810 void *usr_handle;
811 int connected;
812 int published;
813
814 struct sk_buff *next = buf->next;
815 struct tipc_msg *msg = buf_msg(buf);
816 u32 dref = msg_destport(msg);
817
4323add6 818 p_ptr = tipc_port_lock(dref);
b97bf3fd
PL
819 if (!p_ptr) {
820 /* Port deleted while msg in queue */
821 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
822 buf = next;
823 continue;
824 }
825 orig.ref = msg_origport(msg);
826 orig.node = msg_orignode(msg);
827 up_ptr = p_ptr->user_port;
828 usr_handle = up_ptr->usr_handle;
829 connected = p_ptr->publ.connected;
830 published = p_ptr->publ.published;
831
832 if (unlikely(msg_errcode(msg)))
833 goto err;
834
835 switch (msg_type(msg)) {
836
837 case TIPC_CONN_MSG:{
838 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
839 u32 peer_port = port_peerport(p_ptr);
840 u32 peer_node = port_peernode(p_ptr);
841
842 spin_unlock_bh(p_ptr->publ.lock);
843 if (unlikely(!connected)) {
844 if (unlikely(published))
845 goto reject;
846 tipc_connect2port(dref,&orig);
847 }
848 if (unlikely(msg_origport(msg) != peer_port))
849 goto reject;
850 if (unlikely(msg_orignode(msg) != peer_node))
851 goto reject;
852 if (unlikely(!cb))
853 goto reject;
854 if (unlikely(++p_ptr->publ.conn_unacked >=
855 TIPC_FLOW_CONTROL_WIN))
856 tipc_acknowledge(dref,
857 p_ptr->publ.conn_unacked);
858 skb_pull(buf, msg_hdr_sz(msg));
859 cb(usr_handle, dref, &buf, msg_data(msg),
860 msg_data_sz(msg));
861 break;
862 }
863 case TIPC_DIRECT_MSG:{
864 tipc_msg_event cb = up_ptr->msg_cb;
865
866 spin_unlock_bh(p_ptr->publ.lock);
867 if (unlikely(connected))
868 goto reject;
869 if (unlikely(!cb))
870 goto reject;
871 skb_pull(buf, msg_hdr_sz(msg));
872 cb(usr_handle, dref, &buf, msg_data(msg),
873 msg_data_sz(msg), msg_importance(msg),
874 &orig);
875 break;
876 }
877 case TIPC_NAMED_MSG:{
878 tipc_named_msg_event cb = up_ptr->named_msg_cb;
879
880 spin_unlock_bh(p_ptr->publ.lock);
881 if (unlikely(connected))
882 goto reject;
883 if (unlikely(!cb))
884 goto reject;
885 if (unlikely(!published))
886 goto reject;
887 dseq.type = msg_nametype(msg);
888 dseq.lower = msg_nameinst(msg);
889 dseq.upper = dseq.lower;
890 skb_pull(buf, msg_hdr_sz(msg));
891 cb(usr_handle, dref, &buf, msg_data(msg),
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:
902 switch (msg_type(msg)) {
903
904 case TIPC_CONN_MSG:{
905 tipc_conn_shutdown_event cb =
906 up_ptr->conn_err_cb;
907 u32 peer_port = port_peerport(p_ptr);
908 u32 peer_node = port_peernode(p_ptr);
909
910 spin_unlock_bh(p_ptr->publ.lock);
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
926 spin_unlock_bh(p_ptr->publ.lock);
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 }
934 case TIPC_NAMED_MSG:{
935 tipc_named_msg_err_event cb =
936 up_ptr->named_err_cb;
937
938 spin_unlock_bh(p_ptr->publ.lock);
939 if (connected || !cb)
940 break;
941 dseq.type = msg_nametype(msg);
942 dseq.lower = msg_nameinst(msg);
943 dseq.upper = dseq.lower;
944 skb_pull(buf, msg_hdr_sz(msg));
945 cb(usr_handle, dref, &buf, msg_data(msg),
946 msg_data_sz(msg), msg_errcode(msg), &dseq);
947 break;
948 }
949 }
950 if (buf)
951 buf_discard(buf);
952 buf = next;
953 continue;
954reject:
955 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
956 buf = next;
957 }
958}
959
960/*
961 * port_dispatcher(): Dispatcher for messages destinated
962 * to the tipc_port interface. Called with port locked.
963 */
964
965static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
966{
967 buf->next = NULL;
968 spin_lock_bh(&queue_lock);
969 if (msg_queue_head) {
970 msg_queue_tail->next = buf;
971 msg_queue_tail = buf;
972 } else {
973 msg_queue_tail = msg_queue_head = buf;
4323add6 974 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
b97bf3fd
PL
975 }
976 spin_unlock_bh(&queue_lock);
977 return TIPC_OK;
978}
979
980/*
981 * Wake up port after congestion: Called with port locked,
982 *
983 */
984
985static void port_wakeup_sh(unsigned long ref)
986{
987 struct port *p_ptr;
988 struct user_port *up_ptr;
1fc54d8f
SR
989 tipc_continue_event cb = NULL;
990 void *uh = NULL;
b97bf3fd 991
4323add6 992 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
993 if (p_ptr) {
994 up_ptr = p_ptr->user_port;
995 if (up_ptr) {
996 cb = up_ptr->continue_event_cb;
997 uh = up_ptr->usr_handle;
998 }
4323add6 999 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1000 }
1001 if (cb)
1002 cb(uh, ref);
1003}
1004
1005
1006static void port_wakeup(struct tipc_port *p_ptr)
1007{
4323add6 1008 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
b97bf3fd
PL
1009}
1010
1011void tipc_acknowledge(u32 ref, u32 ack)
1012{
1013 struct port *p_ptr;
1fc54d8f 1014 struct sk_buff *buf = NULL;
b97bf3fd 1015
4323add6 1016 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1017 if (!p_ptr)
1018 return;
1019 if (p_ptr->publ.connected) {
1020 p_ptr->publ.conn_unacked -= ack;
1021 buf = port_build_proto_msg(port_peerport(p_ptr),
1022 port_peernode(p_ptr),
1023 ref,
1024 tipc_own_addr,
1025 CONN_MANAGER,
1026 CONN_ACK,
1027 TIPC_OK,
1028 port_out_seqno(p_ptr),
1029 ack);
1030 }
4323add6
PL
1031 tipc_port_unlock(p_ptr);
1032 tipc_net_route_msg(buf);
b97bf3fd
PL
1033}
1034
1035/*
1036 * tipc_createport(): user level call. Will add port to
1037 * registry if non-zero user_ref.
1038 */
1039
1040int tipc_createport(u32 user_ref,
1041 void *usr_handle,
1042 unsigned int importance,
1043 tipc_msg_err_event error_cb,
1044 tipc_named_msg_err_event named_error_cb,
1045 tipc_conn_shutdown_event conn_error_cb,
1046 tipc_msg_event msg_cb,
1047 tipc_named_msg_event named_msg_cb,
1048 tipc_conn_msg_event conn_msg_cb,
1049 tipc_continue_event continue_event_cb,/* May be zero */
1050 u32 *portref)
1051{
1052 struct user_port *up_ptr;
1053 struct port *p_ptr;
1054 u32 ref;
1055
1056 up_ptr = (struct user_port *)kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
1057 if (up_ptr == NULL) {
1058 return -ENOMEM;
1059 }
1fc54d8f 1060 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
4323add6 1061 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1062 if (!p_ptr) {
1063 kfree(up_ptr);
1064 return -ENOMEM;
1065 }
1066
1067 p_ptr->user_port = up_ptr;
1068 up_ptr->user_ref = user_ref;
1069 up_ptr->usr_handle = usr_handle;
1070 up_ptr->ref = p_ptr->publ.ref;
1071 up_ptr->err_cb = error_cb;
1072 up_ptr->named_err_cb = named_error_cb;
1073 up_ptr->conn_err_cb = conn_error_cb;
1074 up_ptr->msg_cb = msg_cb;
1075 up_ptr->named_msg_cb = named_msg_cb;
1076 up_ptr->conn_msg_cb = conn_msg_cb;
1077 up_ptr->continue_event_cb = continue_event_cb;
1078 INIT_LIST_HEAD(&up_ptr->uport_list);
4323add6 1079 tipc_reg_add_port(up_ptr);
b97bf3fd
PL
1080 *portref = p_ptr->publ.ref;
1081 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
4323add6 1082 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1083 return TIPC_OK;
1084}
1085
1086int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1087{
1088 id->ref = ref;
1089 id->node = tipc_own_addr;
1090 return TIPC_OK;
1091}
1092
1093int tipc_portimportance(u32 ref, unsigned int *importance)
1094{
1095 struct port *p_ptr;
1096
4323add6 1097 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1098 if (!p_ptr)
1099 return -EINVAL;
1100 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1101 spin_unlock_bh(p_ptr->publ.lock);
1102 return TIPC_OK;
1103}
1104
1105int tipc_set_portimportance(u32 ref, unsigned int imp)
1106{
1107 struct port *p_ptr;
1108
1109 if (imp > TIPC_CRITICAL_IMPORTANCE)
1110 return -EINVAL;
1111
4323add6 1112 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1113 if (!p_ptr)
1114 return -EINVAL;
1115 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1116 spin_unlock_bh(p_ptr->publ.lock);
1117 return TIPC_OK;
1118}
1119
1120
1121int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1122{
1123 struct port *p_ptr;
1124 struct publication *publ;
1125 u32 key;
1126 int res = -EINVAL;
1127
4323add6 1128 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1129 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1130 "lower = %u, upper = %u\n",
1131 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1132 if (!p_ptr)
1133 return -EINVAL;
1134 if (p_ptr->publ.connected)
1135 goto exit;
1136 if (seq->lower > seq->upper)
1137 goto exit;
1138 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1139 goto exit;
1140 key = ref + p_ptr->pub_count + 1;
1141 if (key == ref) {
1142 res = -EADDRINUSE;
1143 goto exit;
1144 }
4323add6
PL
1145 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1146 scope, p_ptr->publ.ref, key);
b97bf3fd
PL
1147 if (publ) {
1148 list_add(&publ->pport_list, &p_ptr->publications);
1149 p_ptr->pub_count++;
1150 p_ptr->publ.published = 1;
1151 res = TIPC_OK;
1152 }
1153exit:
4323add6 1154 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1155 return res;
1156}
1157
1158int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1159{
1160 struct port *p_ptr;
1161 struct publication *publ;
1162 struct publication *tpubl;
1163 int res = -EINVAL;
1164
4323add6 1165 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1166 if (!p_ptr)
1167 return -EINVAL;
1168 if (!p_ptr->publ.published)
1169 goto exit;
1170 if (!seq) {
1171 list_for_each_entry_safe(publ, tpubl,
1172 &p_ptr->publications, pport_list) {
4323add6
PL
1173 tipc_nametbl_withdraw(publ->type, publ->lower,
1174 publ->ref, publ->key);
b97bf3fd
PL
1175 }
1176 res = TIPC_OK;
1177 } else {
1178 list_for_each_entry_safe(publ, tpubl,
1179 &p_ptr->publications, pport_list) {
1180 if (publ->scope != scope)
1181 continue;
1182 if (publ->type != seq->type)
1183 continue;
1184 if (publ->lower != seq->lower)
1185 continue;
1186 if (publ->upper != seq->upper)
1187 break;
4323add6
PL
1188 tipc_nametbl_withdraw(publ->type, publ->lower,
1189 publ->ref, publ->key);
b97bf3fd
PL
1190 res = TIPC_OK;
1191 break;
1192 }
1193 }
1194 if (list_empty(&p_ptr->publications))
1195 p_ptr->publ.published = 0;
1196exit:
4323add6 1197 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1198 return res;
1199}
1200
1201int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1202{
1203 struct port *p_ptr;
1204 struct tipc_msg *msg;
1205 int res = -EINVAL;
1206
4323add6 1207 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1208 if (!p_ptr)
1209 return -EINVAL;
1210 if (p_ptr->publ.published || p_ptr->publ.connected)
1211 goto exit;
1212 if (!peer->ref)
1213 goto exit;
1214
1215 msg = &p_ptr->publ.phdr;
1216 msg_set_destnode(msg, peer->node);
1217 msg_set_destport(msg, peer->ref);
1218 msg_set_orignode(msg, tipc_own_addr);
1219 msg_set_origport(msg, p_ptr->publ.ref);
1220 msg_set_transp_seqno(msg, 42);
1221 msg_set_type(msg, TIPC_CONN_MSG);
1222 if (!may_route(peer->node))
1223 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1224 else
1225 msg_set_hdr_sz(msg, LONG_H_SIZE);
1226
1227 p_ptr->probing_interval = PROBING_INTERVAL;
1228 p_ptr->probing_state = CONFIRMED;
1229 p_ptr->publ.connected = 1;
1230 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1231
4323add6 1232 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
880b005f 1233 (void *)(unsigned long)ref,
b97bf3fd
PL
1234 (net_ev_handler)port_handle_node_down);
1235 res = TIPC_OK;
1236exit:
4323add6
PL
1237 tipc_port_unlock(p_ptr);
1238 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
b97bf3fd
PL
1239 return res;
1240}
1241
1242/*
1243 * tipc_disconnect(): Disconnect port form peer.
1244 * This is a node local operation.
1245 */
1246
1247int tipc_disconnect(u32 ref)
1248{
1249 struct port *p_ptr;
1250 int res = -ENOTCONN;
1251
4323add6 1252 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1253 if (!p_ptr)
1254 return -EINVAL;
1255 if (p_ptr->publ.connected) {
1256 p_ptr->publ.connected = 0;
1257 /* let timer expire on it's own to avoid deadlock! */
4323add6 1258 tipc_nodesub_unsubscribe(&p_ptr->subscription);
b97bf3fd
PL
1259 res = TIPC_OK;
1260 }
4323add6 1261 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1262 return res;
1263}
1264
1265/*
1266 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1267 */
1268int tipc_shutdown(u32 ref)
1269{
1270 struct port *p_ptr;
1fc54d8f 1271 struct sk_buff *buf = NULL;
b97bf3fd 1272
4323add6 1273 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1274 if (!p_ptr)
1275 return -EINVAL;
1276
1277 if (p_ptr->publ.connected) {
1278 u32 imp = msg_importance(&p_ptr->publ.phdr);
1279 if (imp < TIPC_CRITICAL_IMPORTANCE)
1280 imp++;
1281 buf = port_build_proto_msg(port_peerport(p_ptr),
1282 port_peernode(p_ptr),
1283 ref,
1284 tipc_own_addr,
1285 imp,
1286 TIPC_CONN_MSG,
1287 TIPC_CONN_SHUTDOWN,
1288 port_out_seqno(p_ptr),
1289 0);
1290 }
4323add6
PL
1291 tipc_port_unlock(p_ptr);
1292 tipc_net_route_msg(buf);
b97bf3fd
PL
1293 return tipc_disconnect(ref);
1294}
1295
1296int tipc_isconnected(u32 ref, int *isconnected)
1297{
1298 struct port *p_ptr;
1299
4323add6 1300 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1301 if (!p_ptr)
1302 return -EINVAL;
1303 *isconnected = p_ptr->publ.connected;
4323add6 1304 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1305 return TIPC_OK;
1306}
1307
1308int tipc_peer(u32 ref, struct tipc_portid *peer)
1309{
1310 struct port *p_ptr;
1311 int res;
1312
4323add6 1313 p_ptr = tipc_port_lock(ref);
b97bf3fd
PL
1314 if (!p_ptr)
1315 return -EINVAL;
1316 if (p_ptr->publ.connected) {
1317 peer->ref = port_peerport(p_ptr);
1318 peer->node = port_peernode(p_ptr);
1319 res = TIPC_OK;
1320 } else
1321 res = -ENOTCONN;
4323add6 1322 tipc_port_unlock(p_ptr);
b97bf3fd
PL
1323 return res;
1324}
1325
1326int tipc_ref_valid(u32 ref)
1327{
1328 /* Works irrespective of type */
4323add6 1329 return !!tipc_ref_deref(ref);
b97bf3fd
PL
1330}
1331
1332
1333/*
4323add6 1334 * tipc_port_recv_sections(): Concatenate and deliver sectioned
b97bf3fd
PL
1335 * message for this node.
1336 */
1337
4323add6 1338int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
b97bf3fd
PL
1339 struct iovec const *msg_sect)
1340{
1341 struct sk_buff *buf;
1342 int res;
1343
1344 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1345 MAX_MSG_SIZE, !sender->user_port, &buf);
1346 if (likely(buf))
4323add6 1347 tipc_port_recv_msg(buf);
b97bf3fd
PL
1348 return res;
1349}
1350
1351/**
1352 * tipc_send - send message sections on connection
1353 */
1354
1355int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1356{
1357 struct port *p_ptr;
1358 u32 destnode;
1359 int res;
1360
4323add6 1361 p_ptr = tipc_port_deref(ref);
b97bf3fd
PL
1362 if (!p_ptr || !p_ptr->publ.connected)
1363 return -EINVAL;
1364
1365 p_ptr->publ.congested = 1;
4323add6 1366 if (!tipc_port_congested(p_ptr)) {
b97bf3fd
PL
1367 destnode = port_peernode(p_ptr);
1368 if (likely(destnode != tipc_own_addr))
4323add6
PL
1369 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1370 destnode);
b97bf3fd 1371 else
4323add6 1372 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
b97bf3fd
PL
1373
1374 if (likely(res != -ELINKCONG)) {
1375 port_incr_out_seqno(p_ptr);
1376 p_ptr->publ.congested = 0;
1377 p_ptr->sent++;
1378 return res;
1379 }
1380 }
1381 if (port_unreliable(p_ptr)) {
1382 p_ptr->publ.congested = 0;
1383 /* Just calculate msg length and return */
1384 return msg_calc_data_size(msg_sect, num_sect);
1385 }
1386 return -ELINKCONG;
1387}
1388
1389/**
1390 * tipc_send_buf - send message buffer on connection
1391 */
1392
1393int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1394{
1395 struct port *p_ptr;
1396 struct tipc_msg *msg;
1397 u32 destnode;
1398 u32 hsz;
1399 u32 sz;
1400 u32 res;
1401
4323add6 1402 p_ptr = tipc_port_deref(ref);
b97bf3fd
PL
1403 if (!p_ptr || !p_ptr->publ.connected)
1404 return -EINVAL;
1405
1406 msg = &p_ptr->publ.phdr;
1407 hsz = msg_hdr_sz(msg);
1408 sz = hsz + dsz;
1409 msg_set_size(msg, sz);
1410 if (skb_cow(buf, hsz))
1411 return -ENOMEM;
1412
1413 skb_push(buf, hsz);
1414 memcpy(buf->data, (unchar *)msg, hsz);
1415 destnode = msg_destnode(msg);
1416 p_ptr->publ.congested = 1;
4323add6 1417 if (!tipc_port_congested(p_ptr)) {
b97bf3fd
PL
1418 if (likely(destnode != tipc_own_addr))
1419 res = tipc_send_buf_fast(buf, destnode);
1420 else {
4323add6 1421 tipc_port_recv_msg(buf);
b97bf3fd
PL
1422 res = sz;
1423 }
1424 if (likely(res != -ELINKCONG)) {
1425 port_incr_out_seqno(p_ptr);
1426 p_ptr->sent++;
1427 p_ptr->publ.congested = 0;
1428 return res;
1429 }
1430 }
1431 if (port_unreliable(p_ptr)) {
1432 p_ptr->publ.congested = 0;
1433 return dsz;
1434 }
1435 return -ELINKCONG;
1436}
1437
1438/**
1439 * tipc_forward2name - forward message sections to port name
1440 */
1441
1442int tipc_forward2name(u32 ref,
1443 struct tipc_name const *name,
1444 u32 domain,
1445 u32 num_sect,
1446 struct iovec const *msg_sect,
1447 struct tipc_portid const *orig,
1448 unsigned int importance)
1449{
1450 struct port *p_ptr;
1451 struct tipc_msg *msg;
1452 u32 destnode = domain;
1453 u32 destport = 0;
1454 int res;
1455
4323add6 1456 p_ptr = tipc_port_deref(ref);
b97bf3fd
PL
1457 if (!p_ptr || p_ptr->publ.connected)
1458 return -EINVAL;
1459
1460 msg = &p_ptr->publ.phdr;
1461 msg_set_type(msg, TIPC_NAMED_MSG);
1462 msg_set_orignode(msg, orig->node);
1463 msg_set_origport(msg, orig->ref);
1464 msg_set_hdr_sz(msg, LONG_H_SIZE);
1465 msg_set_nametype(msg, name->type);
1466 msg_set_nameinst(msg, name->instance);
1467 msg_set_lookup_scope(msg, addr_scope(domain));
1468 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1469 msg_set_importance(msg,importance);
4323add6 1470 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
1471 msg_set_destnode(msg, destnode);
1472 msg_set_destport(msg, destport);
1473
1474 if (likely(destport || destnode)) {
1475 p_ptr->sent++;
1476 if (likely(destnode == tipc_own_addr))
4323add6
PL
1477 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1478 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1479 destnode);
b97bf3fd
PL
1480 if (likely(res != -ELINKCONG))
1481 return res;
1482 if (port_unreliable(p_ptr)) {
1483 /* Just calculate msg length and return */
1484 return msg_calc_data_size(msg_sect, num_sect);
1485 }
1486 return -ELINKCONG;
1487 }
4323add6
PL
1488 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1489 TIPC_ERR_NO_NAME);
b97bf3fd
PL
1490}
1491
1492/**
1493 * tipc_send2name - send message sections to port name
1494 */
1495
1496int tipc_send2name(u32 ref,
1497 struct tipc_name const *name,
1498 unsigned int domain,
1499 unsigned int num_sect,
1500 struct iovec const *msg_sect)
1501{
1502 struct tipc_portid orig;
1503
1504 orig.ref = ref;
1505 orig.node = tipc_own_addr;
1506 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1507 TIPC_PORT_IMPORTANCE);
1508}
1509
1510/**
1511 * tipc_forward_buf2name - forward message buffer to port name
1512 */
1513
1514int tipc_forward_buf2name(u32 ref,
1515 struct tipc_name const *name,
1516 u32 domain,
1517 struct sk_buff *buf,
1518 unsigned int dsz,
1519 struct tipc_portid const *orig,
1520 unsigned int importance)
1521{
1522 struct port *p_ptr;
1523 struct tipc_msg *msg;
1524 u32 destnode = domain;
1525 u32 destport = 0;
1526 int res;
1527
4323add6 1528 p_ptr = (struct port *)tipc_ref_deref(ref);
b97bf3fd
PL
1529 if (!p_ptr || p_ptr->publ.connected)
1530 return -EINVAL;
1531
1532 msg = &p_ptr->publ.phdr;
1533 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1534 msg_set_importance(msg, importance);
1535 msg_set_type(msg, TIPC_NAMED_MSG);
1536 msg_set_orignode(msg, orig->node);
1537 msg_set_origport(msg, orig->ref);
1538 msg_set_nametype(msg, name->type);
1539 msg_set_nameinst(msg, name->instance);
1540 msg_set_lookup_scope(msg, addr_scope(domain));
1541 msg_set_hdr_sz(msg, LONG_H_SIZE);
1542 msg_set_size(msg, LONG_H_SIZE + dsz);
4323add6 1543 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
b97bf3fd
PL
1544 msg_set_destnode(msg, destnode);
1545 msg_set_destport(msg, destport);
1546 msg_dbg(msg, "forw2name ==> ");
1547 if (skb_cow(buf, LONG_H_SIZE))
1548 return -ENOMEM;
1549 skb_push(buf, LONG_H_SIZE);
1550 memcpy(buf->data, (unchar *)msg, LONG_H_SIZE);
1551 msg_dbg(buf_msg(buf),"PREP:");
1552 if (likely(destport || destnode)) {
1553 p_ptr->sent++;
1554 if (destnode == tipc_own_addr)
4323add6 1555 return tipc_port_recv_msg(buf);
b97bf3fd
PL
1556 res = tipc_send_buf_fast(buf, destnode);
1557 if (likely(res != -ELINKCONG))
1558 return res;
1559 if (port_unreliable(p_ptr))
1560 return dsz;
1561 return -ELINKCONG;
1562 }
1563 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1564}
1565
1566/**
1567 * tipc_send_buf2name - send message buffer to port name
1568 */
1569
1570int tipc_send_buf2name(u32 ref,
1571 struct tipc_name const *dest,
1572 u32 domain,
1573 struct sk_buff *buf,
1574 unsigned int dsz)
1575{
1576 struct tipc_portid orig;
1577
1578 orig.ref = ref;
1579 orig.node = tipc_own_addr;
1580 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1581 TIPC_PORT_IMPORTANCE);
1582}
1583
1584/**
1585 * tipc_forward2port - forward message sections to port identity
1586 */
1587
1588int tipc_forward2port(u32 ref,
1589 struct tipc_portid const *dest,
1590 unsigned int num_sect,
1591 struct iovec const *msg_sect,
1592 struct tipc_portid const *orig,
1593 unsigned int importance)
1594{
1595 struct port *p_ptr;
1596 struct tipc_msg *msg;
1597 int res;
1598
4323add6 1599 p_ptr = tipc_port_deref(ref);
b97bf3fd
PL
1600 if (!p_ptr || p_ptr->publ.connected)
1601 return -EINVAL;
1602
1603 msg = &p_ptr->publ.phdr;
1604 msg_set_type(msg, TIPC_DIRECT_MSG);
1605 msg_set_orignode(msg, orig->node);
1606 msg_set_origport(msg, orig->ref);
1607 msg_set_destnode(msg, dest->node);
1608 msg_set_destport(msg, dest->ref);
1609 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1610 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1611 msg_set_importance(msg, importance);
1612 p_ptr->sent++;
1613 if (dest->node == tipc_own_addr)
4323add6
PL
1614 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1615 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
b97bf3fd
PL
1616 if (likely(res != -ELINKCONG))
1617 return res;
1618 if (port_unreliable(p_ptr)) {
1619 /* Just calculate msg length and return */
1620 return msg_calc_data_size(msg_sect, num_sect);
1621 }
1622 return -ELINKCONG;
1623}
1624
1625/**
1626 * tipc_send2port - send message sections to port identity
1627 */
1628
1629int tipc_send2port(u32 ref,
1630 struct tipc_portid const *dest,
1631 unsigned int num_sect,
1632 struct iovec const *msg_sect)
1633{
1634 struct tipc_portid orig;
1635
1636 orig.ref = ref;
1637 orig.node = tipc_own_addr;
1638 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1639 TIPC_PORT_IMPORTANCE);
1640}
1641
1642/**
1643 * tipc_forward_buf2port - forward message buffer to port identity
1644 */
1645int tipc_forward_buf2port(u32 ref,
1646 struct tipc_portid const *dest,
1647 struct sk_buff *buf,
1648 unsigned int dsz,
1649 struct tipc_portid const *orig,
1650 unsigned int importance)
1651{
1652 struct port *p_ptr;
1653 struct tipc_msg *msg;
1654 int res;
1655
4323add6 1656 p_ptr = (struct port *)tipc_ref_deref(ref);
b97bf3fd
PL
1657 if (!p_ptr || p_ptr->publ.connected)
1658 return -EINVAL;
1659
1660 msg = &p_ptr->publ.phdr;
1661 msg_set_type(msg, TIPC_DIRECT_MSG);
1662 msg_set_orignode(msg, orig->node);
1663 msg_set_origport(msg, orig->ref);
1664 msg_set_destnode(msg, dest->node);
1665 msg_set_destport(msg, dest->ref);
1666 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1667 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1668 msg_set_importance(msg, importance);
1669 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1670 if (skb_cow(buf, DIR_MSG_H_SIZE))
1671 return -ENOMEM;
1672
1673 skb_push(buf, DIR_MSG_H_SIZE);
1674 memcpy(buf->data, (unchar *)msg, DIR_MSG_H_SIZE);
1675 msg_dbg(msg, "buf2port: ");
1676 p_ptr->sent++;
1677 if (dest->node == tipc_own_addr)
4323add6 1678 return tipc_port_recv_msg(buf);
b97bf3fd
PL
1679 res = tipc_send_buf_fast(buf, dest->node);
1680 if (likely(res != -ELINKCONG))
1681 return res;
1682 if (port_unreliable(p_ptr))
1683 return dsz;
1684 return -ELINKCONG;
1685}
1686
1687/**
1688 * tipc_send_buf2port - send message buffer to port identity
1689 */
1690
1691int tipc_send_buf2port(u32 ref,
1692 struct tipc_portid const *dest,
1693 struct sk_buff *buf,
1694 unsigned int dsz)
1695{
1696 struct tipc_portid orig;
1697
1698 orig.ref = ref;
1699 orig.node = tipc_own_addr;
1700 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1701 TIPC_PORT_IMPORTANCE);
1702}
1703