blob: 07c42fba672be17a4a127b8907f52a3520801f1f [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
Paul Gortmaker872f24d2012-04-23 04:49:13 +000062static 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
Paul Gortmaker872f24d2012-04-23 04:49:13 +000067static 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
Ben Hutchings2c530402012-07-10 10:55:09 +000072/**
Allan Stephensf0712e862012-04-17 18:42:28 -040073 * 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 */
Allan Stephensf0712e862012-04-17 18:42:28 -040078int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
79{
80 u32 peernode;
81 u32 orignode;
82
83 if (msg_origport(msg) != port_peerport(p_ptr))
84 return 0;
85
86 orignode = msg_orignode(msg);
87 peernode = port_peernode(p_ptr);
88 return (orignode == peernode) ||
89 (!orignode && (peernode == tipc_own_addr)) ||
90 (!peernode && (orignode == tipc_own_addr));
91}
92
Per Lidenb97bf3f2006-01-02 19:04:38 +010093/**
94 * tipc_multicast - send a multicast message to local and remote destinations
95 */
Allan Stephens38f232e2010-11-30 12:00:59 +000096int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
Allan Stephens26896902011-04-21 10:42:07 -050097 u32 num_sect, struct iovec const *msg_sect,
98 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +010099{
100 struct tipc_msg *hdr;
101 struct sk_buff *buf;
102 struct sk_buff *ibuf = NULL;
Paul Gortmaker45843102011-12-29 20:33:30 -0500103 struct tipc_port_list dports = {0, NULL, };
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500104 struct tipc_port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 int ext_targets;
106 int res;
107
108 if (unlikely(!oport))
109 return -EINVAL;
110
111 /* Create multicast message */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500112 hdr = &oport->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113 msg_set_type(hdr, TIPC_MCAST_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -0400114 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
Allan Stephens7462b9e2011-04-18 10:08:22 -0400115 msg_set_destport(hdr, 0);
116 msg_set_destnode(hdr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117 msg_set_nametype(hdr, seq->type);
118 msg_set_namelower(hdr, seq->lower);
119 msg_set_nameupper(hdr, seq->upper);
120 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephens26896902011-04-21 10:42:07 -0500121 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 !oport->user_port, &buf);
123 if (unlikely(!buf))
124 return res;
125
126 /* Figure out where to send multicast message */
Per Liden4323add2006-01-18 00:38:21 +0100127 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
128 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900129
130 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 if (ext_targets) {
132 if (dports.count != 0) {
133 ibuf = skb_copy(buf, GFP_ATOMIC);
134 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100135 tipc_port_list_free(&dports);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400136 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 return -ENOMEM;
138 }
139 }
Per Liden4323add2006-01-18 00:38:21 +0100140 res = tipc_bclink_send_msg(buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000141 if ((res < 0) && (dports.count != 0))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400142 kfree_skb(ibuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 } else {
144 ibuf = buf;
145 }
146
147 if (res >= 0) {
148 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100149 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 } else {
Per Liden4323add2006-01-18 00:38:21 +0100151 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 }
153 return res;
154}
155
156/**
Per Liden4323add2006-01-18 00:38:21 +0100157 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900158 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 * If there is no port list, perform a lookup to create one
160 */
Paul Gortmaker45843102011-12-29 20:33:30 -0500161void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162{
Allan Stephens0e659672010-12-31 18:59:32 +0000163 struct tipc_msg *msg;
Paul Gortmaker45843102011-12-29 20:33:30 -0500164 struct tipc_port_list dports = {0, NULL, };
165 struct tipc_port_list *item = dp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 int cnt = 0;
167
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 msg = buf_msg(buf);
169
170 /* Create destination port list, if one wasn't supplied */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100172 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 msg_namelower(msg),
174 msg_nameupper(msg),
175 TIPC_CLUSTER_SCOPE,
176 &dports);
177 item = dp = &dports;
178 }
179
180 /* Deliver a copy of message to each destination port */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 if (dp->count != 0) {
Allan Stephens7f47f5c2011-04-18 10:14:26 -0400182 msg_set_destnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 if (dp->count == 1) {
184 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100185 tipc_port_recv_msg(buf);
186 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 return;
188 }
189 for (; cnt < dp->count; cnt++) {
190 int index = cnt % PLSIZE;
191 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
192
193 if (b == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400194 pr_warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195 goto exit;
196 }
Allan Stephensa0168922010-12-31 18:59:35 +0000197 if ((index == 0) && (cnt != 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 item = item->next;
Allan Stephens0e659672010-12-31 18:59:32 +0000199 msg_set_destport(buf_msg(b), item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100200 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 }
202 }
203exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400204 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100205 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206}
207
208/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700209 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900210 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700211 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 */
Allan Stephens0ea52242008-07-14 22:42:19 -0700213struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
215 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700216 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500218 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 struct tipc_msg *msg;
220 u32 ref;
221
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700222 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700223 if (!p_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400224 pr_warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700225 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 }
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500227 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 if (!ref) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400229 pr_warn("Port creation failed, ref. table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700231 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 }
233
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500234 p_ptr->usr_handle = usr_handle;
235 p_ptr->max_pkt = MAX_PKT_DEFAULT;
236 p_ptr->ref = ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237 INIT_LIST_HEAD(&p_ptr->wait_list);
238 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 p_ptr->dispatcher = dispatcher;
240 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800241 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 INIT_LIST_HEAD(&p_ptr->publications);
244 INIT_LIST_HEAD(&p_ptr->port_list);
Allan Stephensf21536d2012-04-17 18:22:49 -0400245
246 /*
247 * Must hold port list lock while initializing message header template
248 * to ensure a change to node's own network address doesn't result
249 * in template containing out-dated network address information
250 */
Allan Stephensf21536d2012-04-17 18:22:49 -0400251 spin_lock_bh(&tipc_port_list_lock);
252 msg = &p_ptr->phdr;
253 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
254 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100256 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500257 return p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258}
259
260int tipc_deleteport(u32 ref)
261{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500262 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800263 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800265 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100266 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900267 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 return -EINVAL;
269
Per Liden4323add2006-01-18 00:38:21 +0100270 tipc_ref_discard(ref);
271 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272
273 k_cancel_timer(&p_ptr->timer);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500274 if (p_ptr->connected) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100276 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 }
Allan Stephense83504f2010-12-31 18:59:30 +0000278 kfree(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279
Per Liden4323add2006-01-18 00:38:21 +0100280 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 list_del(&p_ptr->port_list);
282 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100283 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 k_term_timer(&p_ptr->timer);
285 kfree(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100286 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700287 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288}
289
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500290static int port_unreliable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500292 return msg_src_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293}
294
295int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
296{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500297 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298
Per Liden4323add2006-01-18 00:38:21 +0100299 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 if (!p_ptr)
301 return -EINVAL;
302 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800303 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700304 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305}
306
307int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
308{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500309 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900310
Per Liden4323add2006-01-18 00:38:21 +0100311 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 if (!p_ptr)
313 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500314 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100315 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700316 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317}
318
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500319static int port_unreturnable(struct tipc_port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500321 return msg_dest_droppable(&p_ptr->phdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322}
323
324int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
325{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500326 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900327
Per Liden4323add2006-01-18 00:38:21 +0100328 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 if (!p_ptr)
330 return -EINVAL;
331 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800332 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700333 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334}
335
336int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
337{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500338 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339
Per Liden4323add2006-01-18 00:38:21 +0100340 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 if (!p_ptr)
342 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500343 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100344 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700345 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346}
347
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348/*
Allan Stephense4a0aee2011-06-01 16:21:12 -0400349 * port_build_proto_msg(): create connection protocol message for port
350 *
351 * On entry the port must be locked and connected.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 */
Allan Stephense4a0aee2011-06-01 16:21:12 -0400353static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
354 u32 type, u32 ack)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
356 struct sk_buff *buf;
357 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900358
Allan Stephens741d9eb2011-05-31 15:03:18 -0400359 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 if (buf) {
361 msg = buf_msg(buf);
Allan Stephense4a0aee2011-06-01 16:21:12 -0400362 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
363 port_peernode(p_ptr));
364 msg_set_destport(msg, port_peerport(p_ptr));
365 msg_set_origport(msg, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 msg_set_msgcnt(msg, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 }
368 return buf;
369}
370
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371int tipc_reject_msg(struct sk_buff *buf, u32 err)
372{
373 struct tipc_msg *msg = buf_msg(buf);
374 struct sk_buff *rbuf;
375 struct tipc_msg *rmsg;
376 int hdr_sz;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400377 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 u32 data_sz = msg_data_sz(msg);
Allan Stephens017dac32011-05-24 13:20:09 -0400379 u32 src_node;
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400380 u32 rmsg_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381
382 /* discard rejected message if it shouldn't be returned to sender */
Allan Stephens76d12522011-05-23 13:57:25 -0400383 if (WARN(!msg_isdata(msg),
384 "attempt to reject message with user=%u", msg_user(msg))) {
385 dump_stack();
386 goto exit;
387 }
Allan Stephensacc631b2011-05-23 13:47:44 -0400388 if (msg_errcode(msg) || msg_dest_droppable(msg))
389 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400391 /*
392 * construct returned message by copying rejected message header and
393 * data (or subset), then updating header fields that need adjusting
394 */
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400395 hdr_sz = msg_hdr_sz(msg);
396 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
397
398 rbuf = tipc_buf_acquire(rmsg_sz);
Allan Stephensacc631b2011-05-23 13:47:44 -0400399 if (rbuf == NULL)
400 goto exit;
401
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 rmsg = buf_msg(rbuf);
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400403 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
404
405 if (msg_connected(rmsg)) {
406 imp = msg_importance(rmsg);
407 if (imp < TIPC_CRITICAL_IMPORTANCE)
408 msg_set_importance(rmsg, ++imp);
Allan Stephens99c14592008-06-04 17:48:25 -0700409 }
Allan Stephens7dd1bf22011-05-23 16:23:32 -0400410 msg_set_non_seq(rmsg, 0);
411 msg_set_size(rmsg, rmsg_sz);
412 msg_set_errcode(rmsg, err);
413 msg_set_prevnode(rmsg, tipc_own_addr);
414 msg_swap_words(rmsg, 4, 5);
415 if (!msg_short(rmsg))
416 msg_swap_words(rmsg, 6, 7);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417
418 /* send self-abort message when rejecting on a connected port */
419 if (msg_connected(msg)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500420 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421
422 if (p_ptr) {
Allan Stephensdff10e92011-11-02 10:32:14 -0400423 struct sk_buff *abuf = NULL;
424
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500425 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100427 tipc_port_unlock(p_ptr);
Allan Stephensdff10e92011-11-02 10:32:14 -0400428 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 }
431
Allan Stephensacc631b2011-05-23 13:47:44 -0400432 /* send returned message & dispose of rejected message */
Allan Stephens017dac32011-05-24 13:20:09 -0400433 src_node = msg_prevnode(msg);
Allan Stephens630d9202012-04-18 09:42:29 -0400434 if (in_own_node(src_node))
Allan Stephens017dac32011-05-24 13:20:09 -0400435 tipc_port_recv_msg(rbuf);
436 else
437 tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
Allan Stephensacc631b2011-05-23 13:47:44 -0400438exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -0400439 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 return data_sz;
441}
442
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500443int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
Per Liden4323add2006-01-18 00:38:21 +0100444 struct iovec const *msg_sect, u32 num_sect,
Allan Stephens26896902011-04-21 10:42:07 -0500445 unsigned int total_len, int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446{
447 struct sk_buff *buf;
448 int res;
449
Allan Stephens26896902011-04-21 10:42:07 -0500450 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 !p_ptr->user_port, &buf);
452 if (!buf)
453 return res;
454
455 return tipc_reject_msg(buf, err);
456}
457
458static void port_timeout(unsigned long ref)
459{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500460 struct tipc_port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800461 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462
Allan Stephens065fd172006-10-16 21:38:05 -0700463 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 return;
465
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500466 if (!p_ptr->connected) {
Allan Stephens065fd172006-10-16 21:38:05 -0700467 tipc_port_unlock(p_ptr);
468 return;
469 }
470
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 /* Last probe answered ? */
472 if (p_ptr->probing_state == PROBING) {
473 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
474 } else {
Allan Stephense4a0aee2011-06-01 16:21:12 -0400475 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476 p_ptr->probing_state = PROBING;
477 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
478 }
Per Liden4323add2006-01-18 00:38:21 +0100479 tipc_port_unlock(p_ptr);
480 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481}
482
483
484static void port_handle_node_down(unsigned long ref)
485{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500486 struct tipc_port *p_ptr = tipc_port_lock(ref);
Allan Stephens0e659672010-12-31 18:59:32 +0000487 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488
489 if (!p_ptr)
490 return;
491 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
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
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500497static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498{
Allan Stephense244a912011-05-31 16:10:08 -0400499 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500
Allan Stephense244a912011-05-31 16:10:08 -0400501 if (buf) {
502 struct tipc_msg *msg = buf_msg(buf);
503 msg_swap_words(msg, 4, 5);
504 msg_swap_words(msg, 6, 7);
505 }
506 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507}
508
509
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500510static struct sk_buff *port_build_peer_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;
513 struct tipc_msg *msg;
514 u32 imp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500516 if (!p_ptr->connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800517 return NULL;
Allan Stephense244a912011-05-31 16:10:08 -0400518
519 buf = tipc_buf_acquire(BASIC_H_SIZE);
520 if (buf) {
521 msg = buf_msg(buf);
522 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
523 msg_set_hdr_sz(msg, BASIC_H_SIZE);
524 msg_set_size(msg, BASIC_H_SIZE);
525 imp = msg_importance(msg);
526 if (imp < TIPC_CRITICAL_IMPORTANCE)
527 msg_set_importance(msg, ++imp);
528 msg_set_errcode(msg, err);
529 }
530 return buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531}
532
Per Liden4323add2006-01-18 00:38:21 +0100533void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534{
535 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400536 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800537 struct sk_buff *r_buf = NULL;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400538 u32 destport = msg_destport(msg);
539 int wakeable;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540
Allan Stephens1c1a5512011-06-01 15:08:10 -0400541 /* Validate connection */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400542 p_ptr = tipc_port_lock(destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400543 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
Allan Stephensf55b5642011-06-01 15:48:42 -0400544 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
545 if (r_buf) {
546 msg = buf_msg(r_buf);
547 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
Allan Stephensf0712e862012-04-17 18:42:28 -0400548 BASIC_H_SIZE, msg_orignode(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400549 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
550 msg_set_origport(msg, destport);
Allan Stephensf0712e862012-04-17 18:42:28 -0400551 msg_set_destport(msg, msg_origport(msg));
Allan Stephensf55b5642011-06-01 15:48:42 -0400552 }
Allan Stephens1c1a5512011-06-01 15:08:10 -0400553 if (p_ptr)
554 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 goto exit;
556 }
557
Allan Stephens1c1a5512011-06-01 15:08:10 -0400558 /* Process protocol message sent by peer */
Allan Stephens1c1a5512011-06-01 15:08:10 -0400559 switch (msg_type(msg)) {
560 case CONN_ACK:
561 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
562 p_ptr->wakeup;
563 p_ptr->acked += msg_msgcnt(msg);
564 if (!tipc_port_congested(p_ptr)) {
565 p_ptr->congested = 0;
566 if (wakeable)
567 p_ptr->wakeup(p_ptr);
568 }
569 break;
570 case CONN_PROBE:
Allan Stephense4a0aee2011-06-01 16:21:12 -0400571 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
Allan Stephens1c1a5512011-06-01 15:08:10 -0400572 break;
573 default:
574 /* CONN_PROBE_REPLY or unrecognized - no action required */
575 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 }
577 p_ptr->probing_state = CONFIRMED;
Allan Stephens1c1a5512011-06-01 15:08:10 -0400578 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579exit:
Per Liden4323add2006-01-18 00:38:21 +0100580 tipc_net_route_msg(r_buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400581 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582}
583
Erik Hugnedc1aed32012-06-29 00:50:23 -0400584static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 struct publication *publ;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400587 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588
589 if (full_id)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400590 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
591 tipc_zone(tipc_own_addr),
592 tipc_cluster(tipc_own_addr),
593 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400595 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500597 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900598 u32 dport = port_peerport(p_ptr);
599 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600
Erik Hugnedc1aed32012-06-29 00:50:23 -0400601 ret += tipc_snprintf(buf + ret, len - ret,
602 " connected to <%u.%u.%u:%u>",
603 tipc_zone(destnode),
604 tipc_cluster(destnode),
605 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500606 if (p_ptr->conn_type != 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400607 ret += tipc_snprintf(buf + ret, len - ret,
608 " via {%u,%u}", p_ptr->conn_type,
609 p_ptr->conn_instance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500610 } else if (p_ptr->published) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400611 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 if (publ->lower == publ->upper)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400614 ret += tipc_snprintf(buf + ret, len - ret,
615 " {%u,%u}", publ->type,
616 publ->lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 else
Erik Hugnedc1aed32012-06-29 00:50:23 -0400618 ret += tipc_snprintf(buf + ret, len - ret,
619 " {%u,%u,%u}", publ->type,
620 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900621 }
622 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400623 ret += tipc_snprintf(buf + ret, len - ret, "\n");
624 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625}
626
Per Liden4323add2006-01-18 00:38:21 +0100627struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628{
629 struct sk_buff *buf;
630 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400631 char *pb;
632 int pb_len;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500633 struct tipc_port *p_ptr;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400634 int str_len = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635
Erik Hugnedc1aed32012-06-29 00:50:23 -0400636 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 if (!buf)
638 return NULL;
639 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400640 pb = TLV_DATA(rep_tlv);
641 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642
Per Liden4323add2006-01-18 00:38:21 +0100643 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500645 spin_lock_bh(p_ptr->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400646 str_len += port_print(p_ptr, pb, pb_len, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500647 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 }
Per Liden4323add2006-01-18 00:38:21 +0100649 spin_unlock_bh(&tipc_port_list_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400650 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 skb_put(buf, TLV_SPACE(str_len));
652 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
653
654 return buf;
655}
656
Per Liden4323add2006-01-18 00:38:21 +0100657void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500659 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 struct tipc_msg *msg;
661
Per Liden4323add2006-01-18 00:38:21 +0100662 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500664 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700665 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 msg_set_orignode(msg, tipc_own_addr);
667 }
Per Liden4323add2006-01-18 00:38:21 +0100668 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669}
670
671
672/*
673 * port_dispatcher_sigh(): Signal handler for messages destinated
674 * to the tipc_port interface.
675 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676static void port_dispatcher_sigh(void *dummy)
677{
678 struct sk_buff *buf;
679
680 spin_lock_bh(&queue_lock);
681 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800682 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 spin_unlock_bh(&queue_lock);
684
685 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500686 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 struct user_port *up_ptr;
688 struct tipc_portid orig;
689 struct tipc_name_seq dseq;
690 void *usr_handle;
691 int connected;
Allan Stephensf0712e862012-04-17 18:42:28 -0400692 int peer_invalid;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700694 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695
696 struct sk_buff *next = buf->next;
697 struct tipc_msg *msg = buf_msg(buf);
698 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900699
Allan Stephens96882432006-06-25 23:38:58 -0700700 message_type = msg_type(msg);
701 if (message_type > TIPC_DIRECT_MSG)
702 goto reject; /* Unsupported message type */
703
Per Liden4323add2006-01-18 00:38:21 +0100704 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700705 if (!p_ptr)
706 goto reject; /* Port deleted while msg in queue */
707
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 orig.ref = msg_origport(msg);
709 orig.node = msg_orignode(msg);
710 up_ptr = p_ptr->user_port;
711 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500712 connected = p_ptr->connected;
Allan Stephensf0712e862012-04-17 18:42:28 -0400713 peer_invalid = connected && !tipc_port_peer_msg(p_ptr, msg);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500714 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715
716 if (unlikely(msg_errcode(msg)))
717 goto err;
718
Allan Stephens96882432006-06-25 23:38:58 -0700719 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900720
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 case TIPC_CONN_MSG:{
722 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500723 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724
Julia Lawall4cec72c2008-01-08 23:48:20 -0800725 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700726 if (unlikely(!cb))
727 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700729 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 goto reject;
Allan Stephensf0712e862012-04-17 18:42:28 -0400731 } else if (peer_invalid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500733 dsz = msg_data_sz(msg);
734 if (unlikely(dsz &&
735 (++p_ptr->conn_unacked >=
736 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500738 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500740 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 break;
742 }
743 case TIPC_DIRECT_MSG:{
744 tipc_msg_event cb = up_ptr->msg_cb;
745
Julia Lawall4cec72c2008-01-08 23:48:20 -0800746 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700747 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 goto reject;
749 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900750 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 msg_data_sz(msg), msg_importance(msg),
752 &orig);
753 break;
754 }
Allan Stephens96882432006-06-25 23:38:58 -0700755 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756 case TIPC_NAMED_MSG:{
757 tipc_named_msg_event cb = up_ptr->named_msg_cb;
758
Julia Lawall4cec72c2008-01-08 23:48:20 -0800759 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700760 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 goto reject;
762 dseq.type = msg_nametype(msg);
763 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700764 dseq.upper = (message_type == TIPC_NAMED_MSG)
765 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900767 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 msg_data_sz(msg), msg_importance(msg),
769 &orig, &dseq);
770 break;
771 }
772 }
773 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400774 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 buf = next;
776 continue;
777err:
Allan Stephens96882432006-06-25 23:38:58 -0700778 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900779
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900781 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 up_ptr->conn_err_cb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783
Julia Lawall4cec72c2008-01-08 23:48:20 -0800784 tipc_port_unlock(p_ptr);
Allan Stephensf0712e862012-04-17 18:42:28 -0400785 if (!cb || !connected || peer_invalid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 break;
787 tipc_disconnect(dref);
788 skb_pull(buf, msg_hdr_sz(msg));
789 cb(usr_handle, dref, &buf, msg_data(msg),
790 msg_data_sz(msg), msg_errcode(msg));
791 break;
792 }
793 case TIPC_DIRECT_MSG:{
794 tipc_msg_err_event cb = up_ptr->err_cb;
795
Julia Lawall4cec72c2008-01-08 23:48:20 -0800796 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700797 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 break;
799 skb_pull(buf, msg_hdr_sz(msg));
800 cb(usr_handle, dref, &buf, msg_data(msg),
801 msg_data_sz(msg), msg_errcode(msg), &orig);
802 break;
803 }
Allan Stephens96882432006-06-25 23:38:58 -0700804 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900806 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 up_ptr->named_err_cb;
808
Julia Lawall4cec72c2008-01-08 23:48:20 -0800809 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700810 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100811 break;
812 dseq.type = msg_nametype(msg);
813 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700814 dseq.upper = (message_type == TIPC_NAMED_MSG)
815 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900817 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 msg_data_sz(msg), msg_errcode(msg), &dseq);
819 break;
820 }
821 }
822 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400823 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 buf = next;
825 continue;
826reject:
827 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
828 buf = next;
829 }
830}
831
832/*
833 * port_dispatcher(): Dispatcher for messages destinated
834 * to the tipc_port interface. Called with port locked.
835 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
837{
838 buf->next = NULL;
839 spin_lock_bh(&queue_lock);
840 if (msg_queue_head) {
841 msg_queue_tail->next = buf;
842 msg_queue_tail = buf;
843 } else {
844 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100845 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 }
847 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700848 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849}
850
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900851/*
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400852 * Wake up port after congestion: Called with port locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854static void port_wakeup_sh(unsigned long ref)
855{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500856 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800858 tipc_continue_event cb = NULL;
859 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860
Per Liden4323add2006-01-18 00:38:21 +0100861 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 if (p_ptr) {
863 up_ptr = p_ptr->user_port;
864 if (up_ptr) {
865 cb = up_ptr->continue_event_cb;
866 uh = up_ptr->usr_handle;
867 }
Per Liden4323add2006-01-18 00:38:21 +0100868 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 }
870 if (cb)
871 cb(uh, ref);
872}
873
874
875static void port_wakeup(struct tipc_port *p_ptr)
876{
Per Liden4323add2006-01-18 00:38:21 +0100877 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878}
879
880void tipc_acknowledge(u32 ref, u32 ack)
881{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500882 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800883 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884
Per Liden4323add2006-01-18 00:38:21 +0100885 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 if (!p_ptr)
887 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500888 if (p_ptr->connected) {
889 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400890 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 }
Per Liden4323add2006-01-18 00:38:21 +0100892 tipc_port_unlock(p_ptr);
893 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894}
895
896/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000897 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 */
Allan Stephensb0c1e922010-12-31 18:59:22 +0000899int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900900 unsigned int importance,
901 tipc_msg_err_event error_cb,
902 tipc_named_msg_err_event named_error_cb,
903 tipc_conn_shutdown_event conn_error_cb,
904 tipc_msg_event msg_cb,
905 tipc_named_msg_event named_msg_cb,
906 tipc_conn_msg_event conn_msg_cb,
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400907 tipc_continue_event continue_event_cb, /* May be zero */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 u32 *portref)
909{
910 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500911 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700913 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700914 if (!up_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400915 pr_warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 return -ENOMEM;
917 }
Joe Perchese3192692012-06-03 17:41:40 +0000918 p_ptr = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
919 importance);
Allan Stephens0ea52242008-07-14 22:42:19 -0700920 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 kfree(up_ptr);
922 return -ENOMEM;
923 }
924
925 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500927 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 up_ptr->err_cb = error_cb;
929 up_ptr->named_err_cb = named_error_cb;
930 up_ptr->conn_err_cb = conn_error_cb;
931 up_ptr->msg_cb = msg_cb;
932 up_ptr->named_msg_cb = named_msg_cb;
933 up_ptr->conn_msg_cb = conn_msg_cb;
934 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500935 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100936 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700937 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938}
939
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940int tipc_portimportance(u32 ref, unsigned int *importance)
941{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500942 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900943
Per Liden4323add2006-01-18 00:38:21 +0100944 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 if (!p_ptr)
946 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500947 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800948 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700949 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950}
951
952int tipc_set_portimportance(u32 ref, unsigned int imp)
953{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500954 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955
956 if (imp > TIPC_CRITICAL_IMPORTANCE)
957 return -EINVAL;
958
Per Liden4323add2006-01-18 00:38:21 +0100959 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 if (!p_ptr)
961 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500962 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800963 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700964 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965}
966
967
968int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
969{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500970 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 struct publication *publ;
972 u32 key;
973 int res = -EINVAL;
974
Per Liden4323add2006-01-18 00:38:21 +0100975 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800976 if (!p_ptr)
977 return -EINVAL;
978
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500979 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 key = ref + p_ptr->pub_count + 1;
982 if (key == ref) {
983 res = -EADDRINUSE;
984 goto exit;
985 }
Per Liden4323add2006-01-18 00:38:21 +0100986 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500987 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 if (publ) {
989 list_add(&publ->pport_list, &p_ptr->publications);
990 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500991 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -0700992 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 }
994exit:
Per Liden4323add2006-01-18 00:38:21 +0100995 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 return res;
997}
998
999int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1000{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001001 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 struct publication *publ;
1003 struct publication *tpubl;
1004 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001005
Per Liden4323add2006-01-18 00:38:21 +01001006 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 if (!p_ptr)
1008 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001010 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001012 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001013 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001015 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001017 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 &p_ptr->publications, pport_list) {
1019 if (publ->scope != scope)
1020 continue;
1021 if (publ->type != seq->type)
1022 continue;
1023 if (publ->lower != seq->lower)
1024 continue;
1025 if (publ->upper != seq->upper)
1026 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001028 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001029 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 break;
1031 }
1032 }
1033 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001034 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001035 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 return res;
1037}
1038
1039int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1040{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001041 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 struct tipc_msg *msg;
1043 int res = -EINVAL;
1044
Per Liden4323add2006-01-18 00:38:21 +01001045 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 if (!p_ptr)
1047 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001048 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 goto exit;
1050 if (!peer->ref)
1051 goto exit;
1052
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001053 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 msg_set_destnode(msg, peer->node);
1055 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001057 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001058 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059
1060 p_ptr->probing_interval = PROBING_INTERVAL;
1061 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001062 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1064
Allan Stephens0e659672010-12-31 18:59:32 +00001065 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001066 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001068 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069exit:
Per Liden4323add2006-01-18 00:38:21 +01001070 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001071 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 return res;
1073}
1074
Allan Stephens0c3141e2008-04-15 00:22:02 -07001075/**
1076 * tipc_disconnect_port - disconnect port from peer
1077 *
1078 * Port must be locked.
1079 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001080int tipc_disconnect_port(struct tipc_port *tp_ptr)
1081{
1082 int res;
1083
1084 if (tp_ptr->connected) {
1085 tp_ptr->connected = 0;
1086 /* let timer expire on it's own to avoid deadlock! */
Joe Perchese3192692012-06-03 17:41:40 +00001087 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001088 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001089 } else {
1090 res = -ENOTCONN;
1091 }
1092 return res;
1093}
1094
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095/*
1096 * tipc_disconnect(): Disconnect port form peer.
1097 * This is a node local operation.
1098 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099int tipc_disconnect(u32 ref)
1100{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001101 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001102 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103
Per Liden4323add2006-01-18 00:38:21 +01001104 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105 if (!p_ptr)
1106 return -EINVAL;
Joe Perchese3192692012-06-03 17:41:40 +00001107 res = tipc_disconnect_port(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001108 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 return res;
1110}
1111
1112/*
1113 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1114 */
1115int tipc_shutdown(u32 ref)
1116{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001117 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001118 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119
Per Liden4323add2006-01-18 00:38:21 +01001120 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 if (!p_ptr)
1122 return -EINVAL;
1123
Allan Stephense244a912011-05-31 16:10:08 -04001124 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +01001125 tipc_port_unlock(p_ptr);
1126 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 return tipc_disconnect(ref);
1128}
1129
Allan Stephens9b641252011-11-09 13:29:18 -05001130/**
1131 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
1132 */
Allan Stephens9b641252011-11-09 13:29:18 -05001133int tipc_port_recv_msg(struct sk_buff *buf)
1134{
1135 struct tipc_port *p_ptr;
1136 struct tipc_msg *msg = buf_msg(buf);
1137 u32 destport = msg_destport(msg);
1138 u32 dsz = msg_data_sz(msg);
1139 u32 err;
1140
1141 /* forward unresolved named message */
1142 if (unlikely(!destport)) {
1143 tipc_net_route_msg(buf);
1144 return dsz;
1145 }
1146
1147 /* validate destination & pass to port, otherwise reject message */
1148 p_ptr = tipc_port_lock(destport);
1149 if (likely(p_ptr)) {
Allan Stephens9b641252011-11-09 13:29:18 -05001150 err = p_ptr->dispatcher(p_ptr, buf);
1151 tipc_port_unlock(p_ptr);
1152 if (likely(!err))
1153 return dsz;
1154 } else {
1155 err = TIPC_ERR_NO_PORT;
1156 }
Allan Stephensf0712e862012-04-17 18:42:28 -04001157
Allan Stephens9b641252011-11-09 13:29:18 -05001158 return tipc_reject_msg(buf, err);
1159}
1160
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161/*
Per Liden4323add2006-01-18 00:38:21 +01001162 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 * message for this node.
1164 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001165static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001166 struct iovec const *msg_sect,
1167 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168{
1169 struct sk_buff *buf;
1170 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001171
Allan Stephens26896902011-04-21 10:42:07 -05001172 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 MAX_MSG_SIZE, !sender->user_port, &buf);
1174 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001175 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 return res;
1177}
1178
1179/**
1180 * tipc_send - send message sections on connection
1181 */
Allan Stephens26896902011-04-21 10:42:07 -05001182int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1183 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001185 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 u32 destnode;
1187 int res;
1188
Per Liden4323add2006-01-18 00:38:21 +01001189 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001190 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 return -EINVAL;
1192
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001193 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001194 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 destnode = port_peernode(p_ptr);
Allan Stephens8a55fe72012-04-18 09:27:22 -04001196 if (likely(!in_own_node(destnode)))
Per Liden4323add2006-01-18 00:38:21 +01001197 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001198 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 else
Allan Stephens26896902011-04-21 10:42:07 -05001200 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1201 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202
1203 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001204 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001205 if (res > 0)
1206 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 return res;
1208 }
1209 }
1210 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001211 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001212 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 }
1214 return -ELINKCONG;
1215}
1216
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001217/**
Allan Stephens12bae472010-11-30 12:01:02 +00001218 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 */
Allan Stephens12bae472010-11-30 12:01:02 +00001220int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001221 unsigned int num_sect, struct iovec const *msg_sect,
1222 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001224 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 struct tipc_msg *msg;
1226 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001227 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 int res;
1229
Per Liden4323add2006-01-18 00:38:21 +01001230 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001231 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 return -EINVAL;
1233
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001234 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001236 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 msg_set_nametype(msg, name->type);
1238 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001239 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001240 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 msg_set_destnode(msg, destnode);
1242 msg_set_destport(msg, destport);
1243
Allan Stephensbc9f8142011-11-07 17:00:54 -05001244 if (likely(destport || destnode)) {
Allan Stephens8a55fe72012-04-18 09:27:22 -04001245 if (likely(in_own_node(destnode)))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001246 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001247 msg_sect, total_len);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001248 else if (tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001249 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001250 num_sect, total_len,
1251 destnode);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001252 else
1253 res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
1254 num_sect, total_len,
1255 TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001256 if (likely(res != -ELINKCONG)) {
1257 if (res > 0)
1258 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001260 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001262 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 }
1264 return -ELINKCONG;
1265 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001266 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001267 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268}
1269
1270/**
Allan Stephens12bae472010-11-30 12:01:02 +00001271 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 */
Allan Stephens12bae472010-11-30 12:01:02 +00001273int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001274 unsigned int num_sect, struct iovec const *msg_sect,
1275 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001277 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 struct tipc_msg *msg;
1279 int res;
1280
Per Liden4323add2006-01-18 00:38:21 +01001281 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001282 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 return -EINVAL;
1284
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001285 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001287 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 msg_set_destnode(msg, dest->node);
1289 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001290 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001291
Allan Stephens8a55fe72012-04-18 09:27:22 -04001292 if (in_own_node(dest->node))
Allan Stephens26896902011-04-21 10:42:07 -05001293 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1294 total_len);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001295 else if (tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001296 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001297 total_len, dest->node);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001298 else
1299 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1300 total_len, TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001301 if (likely(res != -ELINKCONG)) {
1302 if (res > 0)
1303 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001305 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001307 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 }
1309 return -ELINKCONG;
1310}
1311
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001312/**
Allan Stephens12bae472010-11-30 12:01:02 +00001313 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 */
Allan Stephens12bae472010-11-30 12:01:02 +00001315int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1316 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001318 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 struct tipc_msg *msg;
1320 int res;
1321
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001322 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1323 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 return -EINVAL;
1325
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001326 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327 msg_set_type(msg, TIPC_DIRECT_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 msg_set_destnode(msg, dest->node);
1329 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001330 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1331 msg_set_size(msg, BASIC_H_SIZE + dsz);
1332 if (skb_cow(buf, BASIC_H_SIZE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 return -ENOMEM;
1334
Allan Stephens741d9eb2011-05-31 15:03:18 -04001335 skb_push(buf, BASIC_H_SIZE);
1336 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001337
Allan Stephens8a55fe72012-04-18 09:27:22 -04001338 if (in_own_node(dest->node))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001339 res = tipc_port_recv_msg(buf);
1340 else
1341 res = tipc_send_buf_fast(buf, dest->node);
1342 if (likely(res != -ELINKCONG)) {
1343 if (res > 0)
1344 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001346 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 if (port_unreliable(p_ptr))
1348 return dsz;
1349 return -ELINKCONG;
1350}