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