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