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