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