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