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