]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/tipc/port.c
tipc: same receive code path for connection protocol and data messages
[karo-tx-linux.git] / net / tipc / port.c
1 /*
2  * net/tipc/port.c: TIPC port code
3  *
4  * Copyright (c) 1992-2007, 2014, Ericsson AB
5  * Copyright (c) 2004-2008, 2010-2013, 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 "socket.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 DEFINE_SPINLOCK(tipc_port_list_lock);
51
52 static LIST_HEAD(ports);
53 static void port_handle_node_down(unsigned long ref);
54 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
55 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
56 static void port_timeout(unsigned long ref);
57
58 /**
59  * tipc_port_peer_msg - verify message was sent by connected port's peer
60  *
61  * Handles cases where the node's network address has changed from
62  * the default of <0.0.0> to its configured setting.
63  */
64 int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
65 {
66         u32 peernode;
67         u32 orignode;
68
69         if (msg_origport(msg) != tipc_port_peerport(p_ptr))
70                 return 0;
71
72         orignode = msg_orignode(msg);
73         peernode = tipc_port_peernode(p_ptr);
74         return (orignode == peernode) ||
75                 (!orignode && (peernode == tipc_own_addr)) ||
76                 (!peernode && (orignode == tipc_own_addr));
77 }
78
79 /**
80  * tipc_port_mcast_xmit - send a multicast message to local and remote
81  * destinations
82  */
83 int tipc_port_mcast_xmit(struct tipc_port *oport,
84                          struct tipc_name_seq const *seq,
85                          struct iovec const *msg_sect,
86                          unsigned int len)
87 {
88         struct tipc_msg *hdr;
89         struct sk_buff *buf;
90         struct sk_buff *ibuf = NULL;
91         struct tipc_port_list dports = {0, NULL, };
92         int ext_targets;
93         int res;
94
95         /* Create multicast message */
96         hdr = &oport->phdr;
97         msg_set_type(hdr, TIPC_MCAST_MSG);
98         msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
99         msg_set_destport(hdr, 0);
100         msg_set_destnode(hdr, 0);
101         msg_set_nametype(hdr, seq->type);
102         msg_set_namelower(hdr, seq->lower);
103         msg_set_nameupper(hdr, seq->upper);
104         msg_set_hdr_sz(hdr, MCAST_H_SIZE);
105         res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
106         if (unlikely(!buf))
107                 return res;
108
109         /* Figure out where to send multicast message */
110         ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
111                                                 TIPC_NODE_SCOPE, &dports);
112
113         /* Send message to destinations (duplicate it only if necessary) */
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                                 kfree_skb(buf);
120                                 return -ENOMEM;
121                         }
122                 }
123                 res = tipc_bclink_xmit(buf);
124                 if ((res < 0) && (dports.count != 0))
125                         kfree_skb(ibuf);
126         } else {
127                 ibuf = buf;
128         }
129
130         if (res >= 0) {
131                 if (ibuf)
132                         tipc_port_mcast_rcv(ibuf, &dports);
133         } else {
134                 tipc_port_list_free(&dports);
135         }
136         return res;
137 }
138
139 /**
140  * tipc_port_mcast_rcv - deliver multicast message to all destination ports
141  *
142  * If there is no port list, perform a lookup to create one
143  */
144 void tipc_port_mcast_rcv(struct sk_buff *buf, struct tipc_port_list *dp)
145 {
146         struct tipc_msg *msg;
147         struct tipc_port_list dports = {0, NULL, };
148         struct tipc_port_list *item = dp;
149         int cnt = 0;
150
151         msg = buf_msg(buf);
152
153         /* Create destination port list, if one wasn't supplied */
154         if (dp == NULL) {
155                 tipc_nametbl_mc_translate(msg_nametype(msg),
156                                      msg_namelower(msg),
157                                      msg_nameupper(msg),
158                                      TIPC_CLUSTER_SCOPE,
159                                      &dports);
160                 item = dp = &dports;
161         }
162
163         /* Deliver a copy of message to each destination port */
164         if (dp->count != 0) {
165                 msg_set_destnode(msg, tipc_own_addr);
166                 if (dp->count == 1) {
167                         msg_set_destport(msg, dp->ports[0]);
168                         tipc_sk_rcv(buf);
169                         tipc_port_list_free(dp);
170                         return;
171                 }
172                 for (; cnt < dp->count; cnt++) {
173                         int index = cnt % PLSIZE;
174                         struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
175
176                         if (b == NULL) {
177                                 pr_warn("Unable to deliver multicast message(s)\n");
178                                 goto exit;
179                         }
180                         if ((index == 0) && (cnt != 0))
181                                 item = item->next;
182                         msg_set_destport(buf_msg(b), item->ports[index]);
183                         tipc_sk_rcv(b);
184                 }
185         }
186 exit:
187         kfree_skb(buf);
188         tipc_port_list_free(dp);
189 }
190
191
192 void tipc_port_wakeup(struct tipc_port *port)
193 {
194         tipc_sock_wakeup(tipc_port_to_sock(port));
195 }
196
197 /* tipc_port_init - intiate TIPC port and lock it
198  *
199  * Returns obtained reference if initialization is successful, zero otherwise
200  */
201 u32 tipc_port_init(struct tipc_port *p_ptr,
202                    const unsigned int importance)
203 {
204         struct tipc_msg *msg;
205         u32 ref;
206
207         ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
208         if (!ref) {
209                 pr_warn("Port registration failed, ref. table exhausted\n");
210                 return 0;
211         }
212
213         p_ptr->max_pkt = MAX_PKT_DEFAULT;
214         p_ptr->sent = 1;
215         p_ptr->ref = ref;
216         INIT_LIST_HEAD(&p_ptr->wait_list);
217         INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
218         k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
219         INIT_LIST_HEAD(&p_ptr->publications);
220         INIT_LIST_HEAD(&p_ptr->port_list);
221
222         /*
223          * Must hold port list lock while initializing message header template
224          * to ensure a change to node's own network address doesn't result
225          * in template containing out-dated network address information
226          */
227         spin_lock_bh(&tipc_port_list_lock);
228         msg = &p_ptr->phdr;
229         tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
230         msg_set_origport(msg, ref);
231         list_add_tail(&p_ptr->port_list, &ports);
232         spin_unlock_bh(&tipc_port_list_lock);
233         return ref;
234 }
235
236 void tipc_port_destroy(struct tipc_port *p_ptr)
237 {
238         struct sk_buff *buf = NULL;
239         struct tipc_msg *msg = NULL;
240         u32 peer;
241
242         tipc_withdraw(p_ptr, 0, NULL);
243
244         spin_lock_bh(p_ptr->lock);
245         tipc_ref_discard(p_ptr->ref);
246         spin_unlock_bh(p_ptr->lock);
247
248         k_cancel_timer(&p_ptr->timer);
249         if (p_ptr->connected) {
250                 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
251                 tipc_nodesub_unsubscribe(&p_ptr->subscription);
252                 msg = buf_msg(buf);
253                 peer = msg_destnode(msg);
254                 tipc_link_xmit2(buf, peer, msg_link_selector(msg));
255         }
256         spin_lock_bh(&tipc_port_list_lock);
257         list_del(&p_ptr->port_list);
258         list_del(&p_ptr->wait_list);
259         spin_unlock_bh(&tipc_port_list_lock);
260         k_term_timer(&p_ptr->timer);
261 }
262
263 /*
264  * port_build_proto_msg(): create connection protocol message for port
265  *
266  * On entry the port must be locked and connected.
267  */
268 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
269                                             u32 type, u32 ack)
270 {
271         struct sk_buff *buf;
272         struct tipc_msg *msg;
273
274         buf = tipc_buf_acquire(INT_H_SIZE);
275         if (buf) {
276                 msg = buf_msg(buf);
277                 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
278                               tipc_port_peernode(p_ptr));
279                 msg_set_destport(msg, tipc_port_peerport(p_ptr));
280                 msg_set_origport(msg, p_ptr->ref);
281                 msg_set_msgcnt(msg, ack);
282                 buf->next = NULL;
283         }
284         return buf;
285 }
286
287 static void port_timeout(unsigned long ref)
288 {
289         struct tipc_port *p_ptr = tipc_port_lock(ref);
290         struct sk_buff *buf = NULL;
291         struct tipc_msg *msg = NULL;
292
293         if (!p_ptr)
294                 return;
295
296         if (!p_ptr->connected) {
297                 tipc_port_unlock(p_ptr);
298                 return;
299         }
300
301         /* Last probe answered ? */
302         if (p_ptr->probing_state == PROBING) {
303                 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
304         } else {
305                 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
306                 p_ptr->probing_state = PROBING;
307                 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
308         }
309         tipc_port_unlock(p_ptr);
310         msg = buf_msg(buf);
311         tipc_link_xmit2(buf, msg_destnode(msg), msg_link_selector(msg));
312 }
313
314
315 static void port_handle_node_down(unsigned long ref)
316 {
317         struct tipc_port *p_ptr = tipc_port_lock(ref);
318         struct sk_buff *buf = NULL;
319         struct tipc_msg *msg = NULL;
320
321         if (!p_ptr)
322                 return;
323         buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
324         tipc_port_unlock(p_ptr);
325         msg = buf_msg(buf);
326         tipc_link_xmit2(buf, msg_destnode(msg), msg_link_selector(msg));
327 }
328
329
330 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
331 {
332         struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
333
334         if (buf) {
335                 struct tipc_msg *msg = buf_msg(buf);
336                 msg_swap_words(msg, 4, 5);
337                 msg_swap_words(msg, 6, 7);
338                 buf->next = NULL;
339         }
340         return buf;
341 }
342
343
344 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
345 {
346         struct sk_buff *buf;
347         struct tipc_msg *msg;
348         u32 imp;
349
350         if (!p_ptr->connected)
351                 return NULL;
352
353         buf = tipc_buf_acquire(BASIC_H_SIZE);
354         if (buf) {
355                 msg = buf_msg(buf);
356                 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
357                 msg_set_hdr_sz(msg, BASIC_H_SIZE);
358                 msg_set_size(msg, BASIC_H_SIZE);
359                 imp = msg_importance(msg);
360                 if (imp < TIPC_CRITICAL_IMPORTANCE)
361                         msg_set_importance(msg, ++imp);
362                 msg_set_errcode(msg, err);
363                 buf->next = NULL;
364         }
365         return buf;
366 }
367
368 void tipc_port_proto_rcv(struct tipc_port *p_ptr, struct sk_buff *buf)
369 {
370         struct tipc_msg *msg = buf_msg(buf);
371         struct sk_buff *r_buf = NULL;
372         u32 destport = msg_destport(msg);
373         int wakeable;
374
375         /* Validate connection */
376         if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
377                 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
378                 if (r_buf) {
379                         msg = buf_msg(r_buf);
380                         tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
381                                       BASIC_H_SIZE, msg_orignode(msg));
382                         msg_set_errcode(msg, TIPC_ERR_NO_PORT);
383                         msg_set_origport(msg, destport);
384                         msg_set_destport(msg, msg_origport(msg));
385                 }
386                 goto exit;
387         }
388
389         /* Process protocol message sent by peer */
390         switch (msg_type(msg)) {
391         case CONN_ACK:
392                 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested;
393                 p_ptr->acked += msg_msgcnt(msg);
394                 if (!tipc_port_congested(p_ptr)) {
395                         p_ptr->congested = 0;
396                         if (wakeable)
397                                 tipc_port_wakeup(p_ptr);
398                 }
399                 break;
400         case CONN_PROBE:
401                 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
402                 break;
403         default:
404                 /* CONN_PROBE_REPLY or unrecognized - no action required */
405                 break;
406         }
407         p_ptr->probing_state = CONFIRMED;
408 exit:
409         tipc_link_xmit2(r_buf, msg_destnode(msg), msg_link_selector(msg));
410         kfree_skb(buf);
411 }
412
413 static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
414 {
415         struct publication *publ;
416         int ret;
417
418         if (full_id)
419                 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
420                                     tipc_zone(tipc_own_addr),
421                                     tipc_cluster(tipc_own_addr),
422                                     tipc_node(tipc_own_addr), p_ptr->ref);
423         else
424                 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
425
426         if (p_ptr->connected) {
427                 u32 dport = tipc_port_peerport(p_ptr);
428                 u32 destnode = tipc_port_peernode(p_ptr);
429
430                 ret += tipc_snprintf(buf + ret, len - ret,
431                                      " connected to <%u.%u.%u:%u>",
432                                      tipc_zone(destnode),
433                                      tipc_cluster(destnode),
434                                      tipc_node(destnode), dport);
435                 if (p_ptr->conn_type != 0)
436                         ret += tipc_snprintf(buf + ret, len - ret,
437                                              " via {%u,%u}", p_ptr->conn_type,
438                                              p_ptr->conn_instance);
439         } else if (p_ptr->published) {
440                 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
441                 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
442                         if (publ->lower == publ->upper)
443                                 ret += tipc_snprintf(buf + ret, len - ret,
444                                                      " {%u,%u}", publ->type,
445                                                      publ->lower);
446                         else
447                                 ret += tipc_snprintf(buf + ret, len - ret,
448                                                      " {%u,%u,%u}", publ->type,
449                                                      publ->lower, publ->upper);
450                 }
451         }
452         ret += tipc_snprintf(buf + ret, len - ret, "\n");
453         return ret;
454 }
455
456 struct sk_buff *tipc_port_get_ports(void)
457 {
458         struct sk_buff *buf;
459         struct tlv_desc *rep_tlv;
460         char *pb;
461         int pb_len;
462         struct tipc_port *p_ptr;
463         int str_len = 0;
464
465         buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
466         if (!buf)
467                 return NULL;
468         rep_tlv = (struct tlv_desc *)buf->data;
469         pb = TLV_DATA(rep_tlv);
470         pb_len = ULTRA_STRING_MAX_LEN;
471
472         spin_lock_bh(&tipc_port_list_lock);
473         list_for_each_entry(p_ptr, &ports, port_list) {
474                 spin_lock_bh(p_ptr->lock);
475                 str_len += port_print(p_ptr, pb, pb_len, 0);
476                 spin_unlock_bh(p_ptr->lock);
477         }
478         spin_unlock_bh(&tipc_port_list_lock);
479         str_len += 1;   /* for "\0" */
480         skb_put(buf, TLV_SPACE(str_len));
481         TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
482
483         return buf;
484 }
485
486 void tipc_port_reinit(void)
487 {
488         struct tipc_port *p_ptr;
489         struct tipc_msg *msg;
490
491         spin_lock_bh(&tipc_port_list_lock);
492         list_for_each_entry(p_ptr, &ports, port_list) {
493                 msg = &p_ptr->phdr;
494                 msg_set_prevnode(msg, tipc_own_addr);
495                 msg_set_orignode(msg, tipc_own_addr);
496         }
497         spin_unlock_bh(&tipc_port_list_lock);
498 }
499
500 void tipc_acknowledge(u32 ref, u32 ack)
501 {
502         struct tipc_port *p_ptr;
503         struct sk_buff *buf = NULL;
504         struct tipc_msg *msg;
505
506         p_ptr = tipc_port_lock(ref);
507         if (!p_ptr)
508                 return;
509         if (p_ptr->connected) {
510                 p_ptr->conn_unacked -= ack;
511                 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
512         }
513         tipc_port_unlock(p_ptr);
514         if (!buf)
515                 return;
516         msg = buf_msg(buf);
517         tipc_link_xmit2(buf, msg_destnode(msg), msg_link_selector(msg));
518 }
519
520 int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
521                  struct tipc_name_seq const *seq)
522 {
523         struct publication *publ;
524         u32 key;
525
526         if (p_ptr->connected)
527                 return -EINVAL;
528         key = p_ptr->ref + p_ptr->pub_count + 1;
529         if (key == p_ptr->ref)
530                 return -EADDRINUSE;
531
532         publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
533                                     scope, p_ptr->ref, key);
534         if (publ) {
535                 list_add(&publ->pport_list, &p_ptr->publications);
536                 p_ptr->pub_count++;
537                 p_ptr->published = 1;
538                 return 0;
539         }
540         return -EINVAL;
541 }
542
543 int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
544                   struct tipc_name_seq const *seq)
545 {
546         struct publication *publ;
547         struct publication *tpubl;
548         int res = -EINVAL;
549
550         if (!seq) {
551                 list_for_each_entry_safe(publ, tpubl,
552                                          &p_ptr->publications, pport_list) {
553                         tipc_nametbl_withdraw(publ->type, publ->lower,
554                                               publ->ref, publ->key);
555                 }
556                 res = 0;
557         } else {
558                 list_for_each_entry_safe(publ, tpubl,
559                                          &p_ptr->publications, pport_list) {
560                         if (publ->scope != scope)
561                                 continue;
562                         if (publ->type != seq->type)
563                                 continue;
564                         if (publ->lower != seq->lower)
565                                 continue;
566                         if (publ->upper != seq->upper)
567                                 break;
568                         tipc_nametbl_withdraw(publ->type, publ->lower,
569                                               publ->ref, publ->key);
570                         res = 0;
571                         break;
572                 }
573         }
574         if (list_empty(&p_ptr->publications))
575                 p_ptr->published = 0;
576         return res;
577 }
578
579 int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
580 {
581         struct tipc_port *p_ptr;
582         int res;
583
584         p_ptr = tipc_port_lock(ref);
585         if (!p_ptr)
586                 return -EINVAL;
587         res = __tipc_port_connect(ref, p_ptr, peer);
588         tipc_port_unlock(p_ptr);
589         return res;
590 }
591
592 /*
593  * __tipc_port_connect - connect to a remote peer
594  *
595  * Port must be locked.
596  */
597 int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
598                         struct tipc_portid const *peer)
599 {
600         struct tipc_msg *msg;
601         int res = -EINVAL;
602
603         if (p_ptr->published || p_ptr->connected)
604                 goto exit;
605         if (!peer->ref)
606                 goto exit;
607
608         msg = &p_ptr->phdr;
609         msg_set_destnode(msg, peer->node);
610         msg_set_destport(msg, peer->ref);
611         msg_set_type(msg, TIPC_CONN_MSG);
612         msg_set_lookup_scope(msg, 0);
613         msg_set_hdr_sz(msg, SHORT_H_SIZE);
614
615         p_ptr->probing_interval = PROBING_INTERVAL;
616         p_ptr->probing_state = CONFIRMED;
617         p_ptr->connected = 1;
618         k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
619
620         tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
621                           (void *)(unsigned long)ref,
622                           (net_ev_handler)port_handle_node_down);
623         res = 0;
624 exit:
625         p_ptr->max_pkt = tipc_node_get_mtu(peer->node, ref);
626         return res;
627 }
628
629 /*
630  * __tipc_disconnect - disconnect port from peer
631  *
632  * Port must be locked.
633  */
634 int __tipc_port_disconnect(struct tipc_port *tp_ptr)
635 {
636         if (tp_ptr->connected) {
637                 tp_ptr->connected = 0;
638                 /* let timer expire on it's own to avoid deadlock! */
639                 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
640                 return 0;
641         }
642
643         return -ENOTCONN;
644 }
645
646 /*
647  * tipc_port_disconnect(): Disconnect port form peer.
648  *                    This is a node local operation.
649  */
650 int tipc_port_disconnect(u32 ref)
651 {
652         struct tipc_port *p_ptr;
653         int res;
654
655         p_ptr = tipc_port_lock(ref);
656         if (!p_ptr)
657                 return -EINVAL;
658         res = __tipc_port_disconnect(p_ptr);
659         tipc_port_unlock(p_ptr);
660         return res;
661 }
662
663 /*
664  * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
665  */
666 int tipc_port_shutdown(u32 ref)
667 {
668         struct tipc_msg *msg;
669         struct tipc_port *p_ptr;
670         struct sk_buff *buf = NULL;
671
672         p_ptr = tipc_port_lock(ref);
673         if (!p_ptr)
674                 return -EINVAL;
675
676         buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
677         tipc_port_unlock(p_ptr);
678         msg = buf_msg(buf);
679         tipc_link_xmit2(buf, msg_destnode(msg), msg_link_selector(msg));
680         return tipc_port_disconnect(ref);
681 }