blob: 616c72fb92341ed94759a337bc2188766bb72dc1 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/port.c: TIPC port code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Allan Stephens05646c92007-06-10 17:25:24 -07004 * Copyright (c) 1992-2007, Ericsson AB
Allan Stephens23dd4cc2011-01-07 11:43:40 -05005 * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_table.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
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
Allan Stephense3ec9c72010-12-31 18:59:34 +000049static struct sk_buff *msg_queue_head;
50static struct sk_buff *msg_queue_tail;
Per Lidenb97bf3f2006-01-02 19:04:38 +010051
Ingo Molnar34af9462006-06-27 02:53:55 -070052DEFINE_SPINLOCK(tipc_port_list_lock);
53static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Per Liden4323add2006-01-18 00:38:21 +010055static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010056static void port_handle_node_down(unsigned long ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050057static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
58static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
Per Lidenb97bf3f2006-01-02 19:04:38 +010059static void port_timeout(unsigned long ref);
60
61
Allan Stephensd0e17fe2012-04-17 18:36:42 -040062static inline u32 port_peernode(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010063{
Allan Stephens23dd4cc2011-01-07 11:43:40 -050064 return msg_destnode(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010065}
66
Allan Stephensd0e17fe2012-04-17 18:36:42 -040067static inline u32 port_peerport(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010068{
Allan Stephens23dd4cc2011-01-07 11:43:40 -050069 return msg_destport(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010070}
71
Allan Stephensf0712e862012-04-17 18:42:28 -040072/*
73 * tipc_port_peer_msg - verify message was sent by connected port's peer
74 *
75 * Handles cases where the node's network address has changed from
76 * the default of <0.0.0> to its configured setting.
77 */
78
79int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
80{
81 u32 peernode;
82 u32 orignode;
83
84 if (msg_origport(msg) != port_peerport(p_ptr))
85 return 0;
86
87 orignode = msg_orignode(msg);
88 peernode = port_peernode(p_ptr);
89 return (orignode == peernode) ||
90 (!orignode && (peernode == tipc_own_addr)) ||
91 (!peernode && (orignode == tipc_own_addr));
92}
93
Per Lidenb97bf3f2006-01-02 19:04:38 +010094/**
95 * tipc_multicast - send a multicast message to local and remote destinations
96 */
97
Allan Stephens38f232e2010-11-30 12:00:59 +000098int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
Allan Stephens26896902011-04-21 10:42:07 -050099 u32 num_sect, struct iovec const *msg_sect,
100 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101{
102 struct tipc_msg *hdr;
103 struct sk_buff *buf;
104 struct sk_buff *ibuf = NULL;
Paul Gortmaker45843102011-12-29 20:33:30 -0500105 struct tipc_port_list dports = {0, NULL, };
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500106 struct tipc_port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107 int ext_targets;
108 int res;
109
110 if (unlikely(!oport))
111 return -EINVAL;
112
113 /* Create multicast message */
114
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500115 hdr = &oport->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116 msg_set_type(hdr, TIPC_MCAST_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400117 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
Allan Stephens7462b9e2011-04-18 10:08:22 -0400118 msg_set_destport(hdr, 0);
119 msg_set_destnode(hdr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 msg_set_nametype(hdr, seq->type);
121 msg_set_namelower(hdr, seq->lower);
122 msg_set_nameupper(hdr, seq->upper);
123 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephens26896902011-04-21 10:42:07 -0500124 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 !oport->user_port, &buf);
126 if (unlikely(!buf))
127 return res;
128
129 /* Figure out where to send multicast message */
130
Per Liden4323add2006-01-18 00:38:21 +0100131 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
132 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900133
134 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135
136 if (ext_targets) {
137 if (dports.count != 0) {
138 ibuf = skb_copy(buf, GFP_ATOMIC);
139 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100140 tipc_port_list_free(&dports);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400141 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 return -ENOMEM;
143 }
144 }
Per Liden4323add2006-01-18 00:38:21 +0100145 res = tipc_bclink_send_msg(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000146 if ((res < 0) && (dports.count != 0))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400147 kfree_skb(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 } else {
149 ibuf = buf;
150 }
151
152 if (res >= 0) {
153 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100154 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 } else {
Per Liden4323add2006-01-18 00:38:21 +0100156 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 }
158 return res;
159}
160
161/**
Per Liden4323add2006-01-18 00:38:21 +0100162 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900163 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 * If there is no port list, perform a lookup to create one
165 */
166
Paul Gortmaker45843102011-12-29 20:33:30 -0500167void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168{
Allan Stephens0e659672010-12-31 18:59:32 +0000169 struct tipc_msg *msg;
Paul Gortmaker45843102011-12-29 20:33:30 -0500170 struct tipc_port_list dports = {0, NULL, };
171 struct tipc_port_list *item = dp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 int cnt = 0;
173
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 msg = buf_msg(buf);
175
176 /* Create destination port list, if one wasn't supplied */
177
178 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100179 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 msg_namelower(msg),
181 msg_nameupper(msg),
182 TIPC_CLUSTER_SCOPE,
183 &dports);
184 item = dp = &dports;
185 }
186
187 /* Deliver a copy of message to each destination port */
188
189 if (dp->count != 0) {
Allan Stephens7f47f5c2011-04-18 10:14:26 -0400190 msg_set_destnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 if (dp->count == 1) {
192 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100193 tipc_port_recv_msg(buf);
194 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195 return;
196 }
197 for (; cnt < dp->count; cnt++) {
198 int index = cnt % PLSIZE;
199 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
200
201 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700202 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203 goto exit;
204 }
Allan Stephensa0168922010-12-31 18:59:35 +0000205 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000207 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100208 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 }
210 }
211exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400212 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100213 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214}
215
216/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700217 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900218 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700219 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 */
221
Allan Stephens0ea52242008-07-14 22:42:19 -0700222struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
224 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700225 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500227 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 struct tipc_msg *msg;
229 u32 ref;
230
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700231 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700232 if (!p_ptr) {
233 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700234 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500236 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700238 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700240 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 }
242
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500243 p_ptr->usr_handle = usr_handle;
244 p_ptr->max_pkt = MAX_PKT_DEFAULT;
245 p_ptr->ref = ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 INIT_LIST_HEAD(&p_ptr->wait_list);
247 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 p_ptr->dispatcher = dispatcher;
249 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800250 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 INIT_LIST_HEAD(&p_ptr->publications);
253 INIT_LIST_HEAD(&p_ptr->port_list);
Allan Stephensf21536d2012-04-17 18:22:49 -0400254
255 /*
256 * Must hold port list lock while initializing message header template
257 * to ensure a change to node's own network address doesn't result
258 * in template containing out-dated network address information
259 */
260
261 spin_lock_bh(&tipc_port_list_lock);
262 msg = &p_ptr->phdr;
263 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
264 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100266 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500267 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268}
269
270int tipc_deleteport(u32 ref)
271{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500272 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800273 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800275 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100276 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900277 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 return -EINVAL;
279
Per Liden4323add2006-01-18 00:38:21 +0100280 tipc_ref_discard(ref);
281 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282
283 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500284 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100286 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 }
Allan Stephense83504f2010-12-31 18:59:30 +0000288 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289
Per Liden4323add2006-01-18 00:38:21 +0100290 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 list_del(&p_ptr->port_list);
292 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100293 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 k_term_timer(&p_ptr->timer);
295 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100296 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700297 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298}
299
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500300static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500302 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303}
304
305int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
306{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500307 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900308
Per Liden4323add2006-01-18 00:38:21 +0100309 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 if (!p_ptr)
311 return -EINVAL;
312 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800313 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700314 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315}
316
317int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
318{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500319 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320
Per Liden4323add2006-01-18 00:38:21 +0100321 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 if (!p_ptr)
323 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500324 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100325 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700326 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327}
328
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500329static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500331 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332}
333
334int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
335{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500336 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900337
Per Liden4323add2006-01-18 00:38:21 +0100338 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 if (!p_ptr)
340 return -EINVAL;
341 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800342 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700343 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344}
345
346int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
347{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500348 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900349
Per Liden4323add2006-01-18 00:38:21 +0100350 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 if (!p_ptr)
352 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500353 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100354 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700355 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356}
357
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900358/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400359 * port_build_proto_msg(): create connection protocol message for port
360 *
361 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400363static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
364 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365{
366 struct sk_buff *buf;
367 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900368
Allan Stephens741d9eb2011-05-31 15:03:18 -0400369 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 if (buf) {
371 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400372 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
373 port_peernode(p_ptr));
374 msg_set_destport(msg, port_peerport(p_ptr));
375 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 }
378 return buf;
379}
380
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381int tipc_reject_msg(struct sk_buff *buf, u32 err)
382{
383 struct tipc_msg *msg = buf_msg(buf);
384 struct sk_buff *rbuf;
385 struct tipc_msg *rmsg;
386 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400387 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400389 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400390 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391
392 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400393
394 if (WARN(!msg_isdata(msg),
395 "attempt to reject message with user=%u", msg_user(msg))) {
396 dump_stack();
397 goto exit;
398 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400399 if (msg_errcode(msg) || msg_dest_droppable(msg))
400 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400402 /*
403 * construct returned message by copying rejected message header and
404 * data (or subset), then updating header fields that need adjusting
405 */
406
407 hdr_sz = msg_hdr_sz(msg);
408 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
409
410 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400411 if (rbuf == NULL)
412 goto exit;
413
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400415 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
416
417 if (msg_connected(rmsg)) {
418 imp = msg_importance(rmsg);
419 if (imp < TIPC_CRITICAL_IMPORTANCE)
420 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700421 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400422 msg_set_non_seq(rmsg, 0);
423 msg_set_size(rmsg, rmsg_sz);
424 msg_set_errcode(rmsg, err);
425 msg_set_prevnode(rmsg, tipc_own_addr);
426 msg_swap_words(rmsg, 4, 5);
427 if (!msg_short(rmsg))
428 msg_swap_words(rmsg, 6, 7);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429
430 /* send self-abort message when rejecting on a connected port */
431 if (msg_connected(msg)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500432 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433
434 if (p_ptr) {
Allan Stephensdff10e92011-11-02 10:32:14 -0400435 struct sk_buff *abuf = NULL;
436
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500437 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100439 tipc_port_unlock(p_ptr);
Allan Stephensdff10e92011-11-02 10:32:14 -0400440 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 }
443
Allan Stephensacc631b2011-05-23 13:47:44 -0400444 /* send returned message & dispose of rejected message */
445
Allan Stephens017dac32011-05-24 13:20:09 -0400446 src_node = msg_prevnode(msg);
447 if (src_node == tipc_own_addr)
448 tipc_port_recv_msg(rbuf);
449 else
450 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400451exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400452 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 return data_sz;
454}
455
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500456int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100457 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500458 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459{
460 struct sk_buff *buf;
461 int res;
462
Allan Stephens26896902011-04-21 10:42:07 -0500463 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 !p_ptr->user_port, &buf);
465 if (!buf)
466 return res;
467
468 return tipc_reject_msg(buf, err);
469}
470
471static void port_timeout(unsigned long ref)
472{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500473 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800474 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475
Allan Stephens065fd172006-10-16 21:38:05 -0700476 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 return;
478
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500479 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700480 tipc_port_unlock(p_ptr);
481 return;
482 }
483
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 /* Last probe answered ? */
485 if (p_ptr->probing_state == PROBING) {
486 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
487 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400488 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 p_ptr->probing_state = PROBING;
490 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
491 }
Per Liden4323add2006-01-18 00:38:21 +0100492 tipc_port_unlock(p_ptr);
493 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494}
495
496
497static void port_handle_node_down(unsigned long ref)
498{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500499 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000500 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501
502 if (!p_ptr)
503 return;
504 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100505 tipc_port_unlock(p_ptr);
506 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507}
508
509
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500510static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511{
Allan Stephense244a912011-05-31 16:10:08 -0400512 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513
Allan Stephense244a912011-05-31 16:10:08 -0400514 if (buf) {
515 struct tipc_msg *msg = buf_msg(buf);
516 msg_swap_words(msg, 4, 5);
517 msg_swap_words(msg, 6, 7);
518 }
519 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520}
521
522
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500523static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524{
Allan Stephense244a912011-05-31 16:10:08 -0400525 struct sk_buff *buf;
526 struct tipc_msg *msg;
527 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500529 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800530 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400531
532 buf = tipc_buf_acquire(BASIC_H_SIZE);
533 if (buf) {
534 msg = buf_msg(buf);
535 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
536 msg_set_hdr_sz(msg, BASIC_H_SIZE);
537 msg_set_size(msg, BASIC_H_SIZE);
538 imp = msg_importance(msg);
539 if (imp < TIPC_CRITICAL_IMPORTANCE)
540 msg_set_importance(msg, ++imp);
541 msg_set_errcode(msg, err);
542 }
543 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544}
545
Per Liden4323add2006-01-18 00:38:21 +0100546void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547{
548 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400549 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800550 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400551 u32 destport = msg_destport(msg);
552 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553
Allan Stephens1c1a5512011-06-01 15:08:10 -0400554 /* Validate connection */
555
556 p_ptr = tipc_port_lock(destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400557 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400558 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
559 if (r_buf) {
560 msg = buf_msg(r_buf);
561 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
Allan Stephensf0712e862012-04-17 18:42:28 -0400562 BASIC_H_SIZE, msg_orignode(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400563 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
564 msg_set_origport(msg, destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400565 msg_set_destport(msg, msg_origport(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400566 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400567 if (p_ptr)
568 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 goto exit;
570 }
571
Allan Stephens1c1a5512011-06-01 15:08:10 -0400572 /* Process protocol message sent by peer */
573
574 switch (msg_type(msg)) {
575 case CONN_ACK:
576 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
577 p_ptr->wakeup;
578 p_ptr->acked += msg_msgcnt(msg);
579 if (!tipc_port_congested(p_ptr)) {
580 p_ptr->congested = 0;
581 if (wakeable)
582 p_ptr->wakeup(p_ptr);
583 }
584 break;
585 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400586 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400587 break;
588 default:
589 /* CONN_PROBE_REPLY or unrecognized - no action required */
590 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 }
592 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400593 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594exit:
Per Liden4323add2006-01-18 00:38:21 +0100595 tipc_net_route_msg(r_buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400596 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597}
598
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500599static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900601 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602
603 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500606 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500608 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500610 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611 u32 dport = port_peerport(p_ptr);
612 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900614 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
615 tipc_zone(destnode), tipc_cluster(destnode),
616 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500617 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500619 p_ptr->conn_type,
620 p_ptr->conn_instance);
621 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900622 tipc_printf(buf, " bound to");
623 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 if (publ->lower == publ->upper)
625 tipc_printf(buf, " {%u,%u}", publ->type,
626 publ->lower);
627 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900628 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630 }
631 }
632 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633}
634
635#define MAX_PORT_QUERY 32768
636
Per Liden4323add2006-01-18 00:38:21 +0100637struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638{
639 struct sk_buff *buf;
640 struct tlv_desc *rep_tlv;
641 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500642 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 int str_len;
644
Per Liden4323add2006-01-18 00:38:21 +0100645 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 if (!buf)
647 return NULL;
648 rep_tlv = (struct tlv_desc *)buf->data;
649
Per Liden4323add2006-01-18 00:38:21 +0100650 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
651 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500653 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500655 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 }
Per Liden4323add2006-01-18 00:38:21 +0100657 spin_unlock_bh(&tipc_port_list_lock);
658 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659
660 skb_put(buf, TLV_SPACE(str_len));
661 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
662
663 return buf;
664}
665
Per Liden4323add2006-01-18 00:38:21 +0100666void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500668 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 struct tipc_msg *msg;
670
Per Liden4323add2006-01-18 00:38:21 +0100671 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500673 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700674 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 msg_set_orignode(msg, tipc_own_addr);
676 }
Per Liden4323add2006-01-18 00:38:21 +0100677 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678}
679
680
681/*
682 * port_dispatcher_sigh(): Signal handler for messages destinated
683 * to the tipc_port interface.
684 */
685
686static void port_dispatcher_sigh(void *dummy)
687{
688 struct sk_buff *buf;
689
690 spin_lock_bh(&queue_lock);
691 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800692 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 spin_unlock_bh(&queue_lock);
694
695 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500696 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697 struct user_port *up_ptr;
698 struct tipc_portid orig;
699 struct tipc_name_seq dseq;
700 void *usr_handle;
701 int connected;
Allan Stephensf0712e862012-04-17 18:42:28 -0400702 int peer_invalid;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700704 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705
706 struct sk_buff *next = buf->next;
707 struct tipc_msg *msg = buf_msg(buf);
708 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900709
Allan Stephens96882432006-06-25 23:38:58 -0700710 message_type = msg_type(msg);
711 if (message_type > TIPC_DIRECT_MSG)
712 goto reject; /* Unsupported message type */
713
Per Liden4323add2006-01-18 00:38:21 +0100714 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700715 if (!p_ptr)
716 goto reject; /* Port deleted while msg in queue */
717
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 orig.ref = msg_origport(msg);
719 orig.node = msg_orignode(msg);
720 up_ptr = p_ptr->user_port;
721 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500722 connected = p_ptr->connected;
Allan Stephensf0712e862012-04-17 18:42:28 -0400723 peer_invalid = connected && !tipc_port_peer_msg(p_ptr, msg);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500724 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725
726 if (unlikely(msg_errcode(msg)))
727 goto err;
728
Allan Stephens96882432006-06-25 23:38:58 -0700729 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 case TIPC_CONN_MSG:{
732 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500733 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734
Julia Lawall4cec72c2008-01-08 23:48:20 -0800735 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700736 if (unlikely(!cb))
737 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700739 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 goto reject;
Allan Stephensf0712e862012-04-17 18:42:28 -0400741 } else if (peer_invalid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500743 dsz = msg_data_sz(msg);
744 if (unlikely(dsz &&
745 (++p_ptr->conn_unacked >=
746 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900747 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500748 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500750 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 break;
752 }
753 case TIPC_DIRECT_MSG:{
754 tipc_msg_event cb = up_ptr->msg_cb;
755
Julia Lawall4cec72c2008-01-08 23:48:20 -0800756 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700757 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 goto reject;
759 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900760 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 msg_data_sz(msg), msg_importance(msg),
762 &orig);
763 break;
764 }
Allan Stephens96882432006-06-25 23:38:58 -0700765 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 case TIPC_NAMED_MSG:{
767 tipc_named_msg_event cb = up_ptr->named_msg_cb;
768
Julia Lawall4cec72c2008-01-08 23:48:20 -0800769 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700770 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 goto reject;
772 dseq.type = msg_nametype(msg);
773 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700774 dseq.upper = (message_type == TIPC_NAMED_MSG)
775 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900777 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 msg_data_sz(msg), msg_importance(msg),
779 &orig, &dseq);
780 break;
781 }
782 }
783 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400784 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 buf = next;
786 continue;
787err:
Allan Stephens96882432006-06-25 23:38:58 -0700788 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900789
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900791 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 up_ptr->conn_err_cb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793
Julia Lawall4cec72c2008-01-08 23:48:20 -0800794 tipc_port_unlock(p_ptr);
Allan Stephensf0712e862012-04-17 18:42:28 -0400795 if (!cb || !connected || peer_invalid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 break;
797 tipc_disconnect(dref);
798 skb_pull(buf, msg_hdr_sz(msg));
799 cb(usr_handle, dref, &buf, msg_data(msg),
800 msg_data_sz(msg), msg_errcode(msg));
801 break;
802 }
803 case TIPC_DIRECT_MSG:{
804 tipc_msg_err_event cb = up_ptr->err_cb;
805
Julia Lawall4cec72c2008-01-08 23:48:20 -0800806 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700807 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 break;
809 skb_pull(buf, msg_hdr_sz(msg));
810 cb(usr_handle, dref, &buf, msg_data(msg),
811 msg_data_sz(msg), msg_errcode(msg), &orig);
812 break;
813 }
Allan Stephens96882432006-06-25 23:38:58 -0700814 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900816 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 up_ptr->named_err_cb;
818
Julia Lawall4cec72c2008-01-08 23:48:20 -0800819 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700820 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 break;
822 dseq.type = msg_nametype(msg);
823 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700824 dseq.upper = (message_type == TIPC_NAMED_MSG)
825 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900827 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 msg_data_sz(msg), msg_errcode(msg), &dseq);
829 break;
830 }
831 }
832 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400833 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 buf = next;
835 continue;
836reject:
837 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
838 buf = next;
839 }
840}
841
842/*
843 * port_dispatcher(): Dispatcher for messages destinated
844 * to the tipc_port interface. Called with port locked.
845 */
846
847static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
848{
849 buf->next = NULL;
850 spin_lock_bh(&queue_lock);
851 if (msg_queue_head) {
852 msg_queue_tail->next = buf;
853 msg_queue_tail = buf;
854 } else {
855 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100856 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 }
858 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700859 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860}
861
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900862/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900864 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 */
866
867static void port_wakeup_sh(unsigned long ref)
868{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500869 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800871 tipc_continue_event cb = NULL;
872 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873
Per Liden4323add2006-01-18 00:38:21 +0100874 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 if (p_ptr) {
876 up_ptr = p_ptr->user_port;
877 if (up_ptr) {
878 cb = up_ptr->continue_event_cb;
879 uh = up_ptr->usr_handle;
880 }
Per Liden4323add2006-01-18 00:38:21 +0100881 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 }
883 if (cb)
884 cb(uh, ref);
885}
886
887
888static void port_wakeup(struct tipc_port *p_ptr)
889{
Per Liden4323add2006-01-18 00:38:21 +0100890 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891}
892
893void tipc_acknowledge(u32 ref, u32 ack)
894{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500895 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800896 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897
Per Liden4323add2006-01-18 00:38:21 +0100898 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 if (!p_ptr)
900 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500901 if (p_ptr->connected) {
902 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400903 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 }
Per Liden4323add2006-01-18 00:38:21 +0100905 tipc_port_unlock(p_ptr);
906 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907}
908
909/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000910 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 */
912
Allan Stephensb0c1e922010-12-31 18:59:22 +0000913int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900914 unsigned int importance,
915 tipc_msg_err_event error_cb,
916 tipc_named_msg_err_event named_error_cb,
917 tipc_conn_shutdown_event conn_error_cb,
918 tipc_msg_event msg_cb,
919 tipc_named_msg_event named_msg_cb,
920 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 tipc_continue_event continue_event_cb,/* May be zero */
922 u32 *portref)
923{
924 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500925 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700927 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700928 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700929 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 return -ENOMEM;
931 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500932 p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
Allan Stephens0ea52242008-07-14 22:42:19 -0700933 port_wakeup, importance);
934 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 kfree(up_ptr);
936 return -ENOMEM;
937 }
938
939 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500941 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 up_ptr->err_cb = error_cb;
943 up_ptr->named_err_cb = named_error_cb;
944 up_ptr->conn_err_cb = conn_error_cb;
945 up_ptr->msg_cb = msg_cb;
946 up_ptr->named_msg_cb = named_msg_cb;
947 up_ptr->conn_msg_cb = conn_msg_cb;
948 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500949 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100950 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700951 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952}
953
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954int tipc_portimportance(u32 ref, unsigned int *importance)
955{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500956 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900957
Per Liden4323add2006-01-18 00:38:21 +0100958 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 if (!p_ptr)
960 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500961 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800962 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700963 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964}
965
966int tipc_set_portimportance(u32 ref, unsigned int imp)
967{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500968 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969
970 if (imp > TIPC_CRITICAL_IMPORTANCE)
971 return -EINVAL;
972
Per Liden4323add2006-01-18 00:38:21 +0100973 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 if (!p_ptr)
975 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500976 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800977 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700978 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979}
980
981
982int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
983{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500984 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 struct publication *publ;
986 u32 key;
987 int res = -EINVAL;
988
Per Liden4323add2006-01-18 00:38:21 +0100989 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800990 if (!p_ptr)
991 return -EINVAL;
992
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500993 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994 goto exit;
995 if (seq->lower > seq->upper)
996 goto exit;
997 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
998 goto exit;
999 key = ref + p_ptr->pub_count + 1;
1000 if (key == ref) {
1001 res = -EADDRINUSE;
1002 goto exit;
1003 }
Per Liden4323add2006-01-18 00:38:21 +01001004 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001005 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 if (publ) {
1007 list_add(&publ->pport_list, &p_ptr->publications);
1008 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001009 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001010 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 }
1012exit:
Per Liden4323add2006-01-18 00:38:21 +01001013 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 return res;
1015}
1016
1017int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1018{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001019 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 struct publication *publ;
1021 struct publication *tpubl;
1022 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023
Per Liden4323add2006-01-18 00:38:21 +01001024 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 if (!p_ptr)
1026 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001028 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001030 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001031 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001033 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001035 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 &p_ptr->publications, pport_list) {
1037 if (publ->scope != scope)
1038 continue;
1039 if (publ->type != seq->type)
1040 continue;
1041 if (publ->lower != seq->lower)
1042 continue;
1043 if (publ->upper != seq->upper)
1044 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001045 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001046 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001047 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 break;
1049 }
1050 }
1051 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001052 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001053 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 return res;
1055}
1056
1057int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1058{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001059 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 struct tipc_msg *msg;
1061 int res = -EINVAL;
1062
Per Liden4323add2006-01-18 00:38:21 +01001063 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 if (!p_ptr)
1065 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001066 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 goto exit;
1068 if (!peer->ref)
1069 goto exit;
1070
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001071 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 msg_set_destnode(msg, peer->node);
1073 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001075 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001076 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077
1078 p_ptr->probing_interval = PROBING_INTERVAL;
1079 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001080 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1082
Allan Stephens0e659672010-12-31 18:59:32 +00001083 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001084 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001086 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087exit:
Per Liden4323add2006-01-18 00:38:21 +01001088 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001089 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 return res;
1091}
1092
Allan Stephens0c3141e2008-04-15 00:22:02 -07001093/**
1094 * tipc_disconnect_port - disconnect port from peer
1095 *
1096 * Port must be locked.
1097 */
1098
1099int tipc_disconnect_port(struct tipc_port *tp_ptr)
1100{
1101 int res;
1102
1103 if (tp_ptr->connected) {
1104 tp_ptr->connected = 0;
1105 /* let timer expire on it's own to avoid deadlock! */
1106 tipc_nodesub_unsubscribe(
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001107 &((struct tipc_port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001108 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001109 } else {
1110 res = -ENOTCONN;
1111 }
1112 return res;
1113}
1114
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115/*
1116 * tipc_disconnect(): Disconnect port form peer.
1117 * This is a node local operation.
1118 */
1119
1120int tipc_disconnect(u32 ref)
1121{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001122 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001123 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124
Per Liden4323add2006-01-18 00:38:21 +01001125 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 if (!p_ptr)
1127 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001128 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001129 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 return res;
1131}
1132
1133/*
1134 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1135 */
1136int tipc_shutdown(u32 ref)
1137{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001138 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001139 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140
Per Liden4323add2006-01-18 00:38:21 +01001141 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 if (!p_ptr)
1143 return -EINVAL;
1144
Allan Stephense244a912011-05-31 16:10:08 -04001145 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +01001146 tipc_port_unlock(p_ptr);
1147 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 return tipc_disconnect(ref);
1149}
1150
Allan Stephens9b641252011-11-09 13:29:18 -05001151/**
1152 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
1153 */
1154
1155int tipc_port_recv_msg(struct sk_buff *buf)
1156{
1157 struct tipc_port *p_ptr;
1158 struct tipc_msg *msg = buf_msg(buf);
1159 u32 destport = msg_destport(msg);
1160 u32 dsz = msg_data_sz(msg);
1161 u32 err;
1162
1163 /* forward unresolved named message */
1164 if (unlikely(!destport)) {
1165 tipc_net_route_msg(buf);
1166 return dsz;
1167 }
1168
1169 /* validate destination & pass to port, otherwise reject message */
1170 p_ptr = tipc_port_lock(destport);
1171 if (likely(p_ptr)) {
Allan Stephens9b641252011-11-09 13:29:18 -05001172 err = p_ptr->dispatcher(p_ptr, buf);
1173 tipc_port_unlock(p_ptr);
1174 if (likely(!err))
1175 return dsz;
1176 } else {
1177 err = TIPC_ERR_NO_PORT;
1178 }
Allan Stephensf0712e862012-04-17 18:42:28 -04001179
Allan Stephens9b641252011-11-09 13:29:18 -05001180 return tipc_reject_msg(buf, err);
1181}
1182
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183/*
Per Liden4323add2006-01-18 00:38:21 +01001184 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 * message for this node.
1186 */
1187
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001188static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001189 struct iovec const *msg_sect,
1190 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191{
1192 struct sk_buff *buf;
1193 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001194
Allan Stephens26896902011-04-21 10:42:07 -05001195 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 MAX_MSG_SIZE, !sender->user_port, &buf);
1197 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001198 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 return res;
1200}
1201
1202/**
1203 * tipc_send - send message sections on connection
1204 */
1205
Allan Stephens26896902011-04-21 10:42:07 -05001206int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1207 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001209 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210 u32 destnode;
1211 int res;
1212
Per Liden4323add2006-01-18 00:38:21 +01001213 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001214 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 return -EINVAL;
1216
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001217 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001218 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 destnode = port_peernode(p_ptr);
1220 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001221 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001222 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 else
Allan Stephens26896902011-04-21 10:42:07 -05001224 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1225 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226
1227 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001228 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001229 if (res > 0)
1230 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 return res;
1232 }
1233 }
1234 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001235 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001236 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 }
1238 return -ELINKCONG;
1239}
1240
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001241/**
Allan Stephens12bae472010-11-30 12:01:02 +00001242 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 */
1244
Allan Stephens12bae472010-11-30 12:01:02 +00001245int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001246 unsigned int num_sect, struct iovec const *msg_sect,
1247 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001249 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 struct tipc_msg *msg;
1251 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001252 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 int res;
1254
Per Liden4323add2006-01-18 00:38:21 +01001255 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001256 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 return -EINVAL;
1258
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001259 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001261 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 msg_set_nametype(msg, name->type);
1263 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001264 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001265 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 msg_set_destnode(msg, destnode);
1267 msg_set_destport(msg, destport);
1268
Allan Stephensbc9f8142011-11-07 17:00:54 -05001269 if (likely(destport || destnode)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 if (likely(destnode == tipc_own_addr))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001271 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001272 msg_sect, total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001273 else
1274 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001275 num_sect, total_len,
1276 destnode);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001277 if (likely(res != -ELINKCONG)) {
1278 if (res > 0)
1279 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001281 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001283 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284 }
1285 return -ELINKCONG;
1286 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001287 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001288 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289}
1290
1291/**
Allan Stephens12bae472010-11-30 12:01:02 +00001292 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 */
1294
Allan Stephens12bae472010-11-30 12:01:02 +00001295int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001296 unsigned int num_sect, struct iovec const *msg_sect,
1297 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001299 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 struct tipc_msg *msg;
1301 int res;
1302
Per Liden4323add2006-01-18 00:38:21 +01001303 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001304 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 return -EINVAL;
1306
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001307 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001309 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 msg_set_destnode(msg, dest->node);
1311 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001312 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001313
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 if (dest->node == tipc_own_addr)
Allan Stephens26896902011-04-21 10:42:07 -05001315 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1316 total_len);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001317 else
1318 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001319 total_len, dest->node);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001320 if (likely(res != -ELINKCONG)) {
1321 if (res > 0)
1322 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001324 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001326 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327 }
1328 return -ELINKCONG;
1329}
1330
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001331/**
Allan Stephens12bae472010-11-30 12:01:02 +00001332 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 */
1334
Allan Stephens12bae472010-11-30 12:01:02 +00001335int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1336 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001338 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 struct tipc_msg *msg;
1340 int res;
1341
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001342 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1343 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 return -EINVAL;
1345
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001346 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 msg_set_type(msg, TIPC_DIRECT_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 msg_set_destnode(msg, dest->node);
1349 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001350 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1351 msg_set_size(msg, BASIC_H_SIZE + dsz);
1352 if (skb_cow(buf, BASIC_H_SIZE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 return -ENOMEM;
1354
Allan Stephens741d9eb2011-05-31 15:03:18 -04001355 skb_push(buf, BASIC_H_SIZE);
1356 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001357
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 if (dest->node == tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001359 res = tipc_port_recv_msg(buf);
1360 else
1361 res = tipc_send_buf_fast(buf, dest->node);
1362 if (likely(res != -ELINKCONG)) {
1363 if (res > 0)
1364 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001366 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 if (port_unreliable(p_ptr))
1368 return dsz;
1369 return -ELINKCONG;
1370}
1371