blob: 2cbac3956fc96839f4e36bff2f4f8de43e2b9e24 [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
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500584static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587
588 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900589 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500591 tipc_node(tipc_own_addr), p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 else
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500593 tipc_printf(buf, "%-10u:", p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500595 if (p_ptr->connected) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 u32 dport = port_peerport(p_ptr);
597 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
600 tipc_zone(destnode), tipc_cluster(destnode),
601 tipc_node(destnode), dport);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500602 if (p_ptr->conn_type != 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603 tipc_printf(buf, " via {%u,%u}",
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500604 p_ptr->conn_type,
605 p_ptr->conn_instance);
606 } else if (p_ptr->published) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900607 tipc_printf(buf, " bound to");
608 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 if (publ->lower == publ->upper)
610 tipc_printf(buf, " {%u,%u}", publ->type,
611 publ->lower);
612 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900613 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900615 }
616 }
617 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618}
619
620#define MAX_PORT_QUERY 32768
621
Per Liden4323add2006-01-18 00:38:21 +0100622struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623{
624 struct sk_buff *buf;
625 struct tlv_desc *rep_tlv;
626 struct print_buf pb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500627 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 int str_len;
629
Per Liden4323add2006-01-18 00:38:21 +0100630 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 if (!buf)
632 return NULL;
633 rep_tlv = (struct tlv_desc *)buf->data;
634
Per Liden4323add2006-01-18 00:38:21 +0100635 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
636 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500638 spin_lock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639 port_print(p_ptr, &pb, 0);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500640 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 }
Per Liden4323add2006-01-18 00:38:21 +0100642 spin_unlock_bh(&tipc_port_list_lock);
643 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644
645 skb_put(buf, TLV_SPACE(str_len));
646 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
647
648 return buf;
649}
650
Per Liden4323add2006-01-18 00:38:21 +0100651void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500653 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 struct tipc_msg *msg;
655
Per Liden4323add2006-01-18 00:38:21 +0100656 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 list_for_each_entry(p_ptr, &ports, port_list) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500658 msg = &p_ptr->phdr;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700659 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 msg_set_orignode(msg, tipc_own_addr);
661 }
Per Liden4323add2006-01-18 00:38:21 +0100662 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663}
664
665
666/*
667 * port_dispatcher_sigh(): Signal handler for messages destinated
668 * to the tipc_port interface.
669 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670static void port_dispatcher_sigh(void *dummy)
671{
672 struct sk_buff *buf;
673
674 spin_lock_bh(&queue_lock);
675 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800676 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677 spin_unlock_bh(&queue_lock);
678
679 while (buf) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500680 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 struct user_port *up_ptr;
682 struct tipc_portid orig;
683 struct tipc_name_seq dseq;
684 void *usr_handle;
685 int connected;
Allan Stephensf0712e862012-04-17 18:42:28 -0400686 int peer_invalid;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700688 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689
690 struct sk_buff *next = buf->next;
691 struct tipc_msg *msg = buf_msg(buf);
692 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693
Allan Stephens96882432006-06-25 23:38:58 -0700694 message_type = msg_type(msg);
695 if (message_type > TIPC_DIRECT_MSG)
696 goto reject; /* Unsupported message type */
697
Per Liden4323add2006-01-18 00:38:21 +0100698 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700699 if (!p_ptr)
700 goto reject; /* Port deleted while msg in queue */
701
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702 orig.ref = msg_origport(msg);
703 orig.node = msg_orignode(msg);
704 up_ptr = p_ptr->user_port;
705 usr_handle = up_ptr->usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500706 connected = p_ptr->connected;
Allan Stephensf0712e862012-04-17 18:42:28 -0400707 peer_invalid = connected && !tipc_port_peer_msg(p_ptr, msg);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500708 published = p_ptr->published;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709
710 if (unlikely(msg_errcode(msg)))
711 goto err;
712
Allan Stephens96882432006-06-25 23:38:58 -0700713 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900714
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 case TIPC_CONN_MSG:{
716 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500717 u32 dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718
Julia Lawall4cec72c2008-01-08 23:48:20 -0800719 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700720 if (unlikely(!cb))
721 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700723 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 goto reject;
Allan Stephensf0712e862012-04-17 18:42:28 -0400725 } else if (peer_invalid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 goto reject;
Allan Stephenscb7ce912011-01-24 15:02:14 -0500727 dsz = msg_data_sz(msg);
728 if (unlikely(dsz &&
729 (++p_ptr->conn_unacked >=
730 TIPC_FLOW_CONTROL_WIN)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900731 tipc_acknowledge(dref,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500732 p_ptr->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 skb_pull(buf, msg_hdr_sz(msg));
Allan Stephenscb7ce912011-01-24 15:02:14 -0500734 cb(usr_handle, dref, &buf, msg_data(msg), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 break;
736 }
737 case TIPC_DIRECT_MSG:{
738 tipc_msg_event cb = up_ptr->msg_cb;
739
Julia Lawall4cec72c2008-01-08 23:48:20 -0800740 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700741 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 goto reject;
743 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900744 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 msg_data_sz(msg), msg_importance(msg),
746 &orig);
747 break;
748 }
Allan Stephens96882432006-06-25 23:38:58 -0700749 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 case TIPC_NAMED_MSG:{
751 tipc_named_msg_event cb = up_ptr->named_msg_cb;
752
Julia Lawall4cec72c2008-01-08 23:48:20 -0800753 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700754 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 goto reject;
756 dseq.type = msg_nametype(msg);
757 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700758 dseq.upper = (message_type == TIPC_NAMED_MSG)
759 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 msg_data_sz(msg), msg_importance(msg),
763 &orig, &dseq);
764 break;
765 }
766 }
767 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400768 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 buf = next;
770 continue;
771err:
Allan Stephens96882432006-06-25 23:38:58 -0700772 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900773
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900775 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 up_ptr->conn_err_cb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777
Julia Lawall4cec72c2008-01-08 23:48:20 -0800778 tipc_port_unlock(p_ptr);
Allan Stephensf0712e862012-04-17 18:42:28 -0400779 if (!cb || !connected || peer_invalid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 break;
781 tipc_disconnect(dref);
782 skb_pull(buf, msg_hdr_sz(msg));
783 cb(usr_handle, dref, &buf, msg_data(msg),
784 msg_data_sz(msg), msg_errcode(msg));
785 break;
786 }
787 case TIPC_DIRECT_MSG:{
788 tipc_msg_err_event cb = up_ptr->err_cb;
789
Julia Lawall4cec72c2008-01-08 23:48:20 -0800790 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700791 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 break;
793 skb_pull(buf, msg_hdr_sz(msg));
794 cb(usr_handle, dref, &buf, msg_data(msg),
795 msg_data_sz(msg), msg_errcode(msg), &orig);
796 break;
797 }
Allan Stephens96882432006-06-25 23:38:58 -0700798 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900800 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 up_ptr->named_err_cb;
802
Julia Lawall4cec72c2008-01-08 23:48:20 -0800803 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700804 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 break;
806 dseq.type = msg_nametype(msg);
807 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700808 dseq.upper = (message_type == TIPC_NAMED_MSG)
809 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 msg_data_sz(msg), msg_errcode(msg), &dseq);
813 break;
814 }
815 }
816 if (buf)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400817 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 buf = next;
819 continue;
820reject:
821 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
822 buf = next;
823 }
824}
825
826/*
827 * port_dispatcher(): Dispatcher for messages destinated
828 * to the tipc_port interface. Called with port locked.
829 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
831{
832 buf->next = NULL;
833 spin_lock_bh(&queue_lock);
834 if (msg_queue_head) {
835 msg_queue_tail->next = buf;
836 msg_queue_tail = buf;
837 } else {
838 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100839 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 }
841 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700842 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843}
844
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900845/*
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400846 * Wake up port after congestion: Called with port locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848static void port_wakeup_sh(unsigned long ref)
849{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500850 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800852 tipc_continue_event cb = NULL;
853 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854
Per Liden4323add2006-01-18 00:38:21 +0100855 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 if (p_ptr) {
857 up_ptr = p_ptr->user_port;
858 if (up_ptr) {
859 cb = up_ptr->continue_event_cb;
860 uh = up_ptr->usr_handle;
861 }
Per Liden4323add2006-01-18 00:38:21 +0100862 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 }
864 if (cb)
865 cb(uh, ref);
866}
867
868
869static void port_wakeup(struct tipc_port *p_ptr)
870{
Per Liden4323add2006-01-18 00:38:21 +0100871 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872}
873
874void tipc_acknowledge(u32 ref, u32 ack)
875{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500876 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800877 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878
Per Liden4323add2006-01-18 00:38:21 +0100879 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 if (!p_ptr)
881 return;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500882 if (p_ptr->connected) {
883 p_ptr->conn_unacked -= ack;
Allan Stephense4a0aee2011-06-01 16:21:12 -0400884 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 }
Per Liden4323add2006-01-18 00:38:21 +0100886 tipc_port_unlock(p_ptr);
887 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888}
889
890/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000891 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 */
Allan Stephensb0c1e922010-12-31 18:59:22 +0000893int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900894 unsigned int importance,
895 tipc_msg_err_event error_cb,
896 tipc_named_msg_err_event named_error_cb,
897 tipc_conn_shutdown_event conn_error_cb,
898 tipc_msg_event msg_cb,
899 tipc_named_msg_event named_msg_cb,
900 tipc_conn_msg_event conn_msg_cb,
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400901 tipc_continue_event continue_event_cb, /* May be zero */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 u32 *portref)
903{
904 struct user_port *up_ptr;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500905 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700907 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700908 if (!up_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400909 pr_warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 return -ENOMEM;
911 }
Joe Perchese3192692012-06-03 17:41:40 +0000912 p_ptr = tipc_createport_raw(NULL, port_dispatcher, port_wakeup,
913 importance);
Allan Stephens0ea52242008-07-14 22:42:19 -0700914 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 kfree(up_ptr);
916 return -ENOMEM;
917 }
918
919 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 up_ptr->usr_handle = usr_handle;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500921 up_ptr->ref = p_ptr->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 up_ptr->err_cb = error_cb;
923 up_ptr->named_err_cb = named_error_cb;
924 up_ptr->conn_err_cb = conn_error_cb;
925 up_ptr->msg_cb = msg_cb;
926 up_ptr->named_msg_cb = named_msg_cb;
927 up_ptr->conn_msg_cb = conn_msg_cb;
928 up_ptr->continue_event_cb = continue_event_cb;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500929 *portref = p_ptr->ref;
Per Liden4323add2006-01-18 00:38:21 +0100930 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700931 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932}
933
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934int tipc_portimportance(u32 ref, unsigned int *importance)
935{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500936 struct tipc_port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900937
Per Liden4323add2006-01-18 00:38:21 +0100938 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 if (!p_ptr)
940 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500941 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800942 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700943 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944}
945
946int tipc_set_portimportance(u32 ref, unsigned int imp)
947{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500948 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949
950 if (imp > TIPC_CRITICAL_IMPORTANCE)
951 return -EINVAL;
952
Per Liden4323add2006-01-18 00:38:21 +0100953 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 if (!p_ptr)
955 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500956 msg_set_importance(&p_ptr->phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800957 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700958 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959}
960
961
962int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
963{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500964 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 struct publication *publ;
966 u32 key;
967 int res = -EINVAL;
968
Per Liden4323add2006-01-18 00:38:21 +0100969 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -0800970 if (!p_ptr)
971 return -EINVAL;
972
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500973 if (p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 key = ref + p_ptr->pub_count + 1;
976 if (key == ref) {
977 res = -EADDRINUSE;
978 goto exit;
979 }
Per Liden4323add2006-01-18 00:38:21 +0100980 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500981 scope, p_ptr->ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 if (publ) {
983 list_add(&publ->pport_list, &p_ptr->publications);
984 p_ptr->pub_count++;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500985 p_ptr->published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -0700986 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 }
988exit:
Per Liden4323add2006-01-18 00:38:21 +0100989 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 return res;
991}
992
993int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
994{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500995 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 struct publication *publ;
997 struct publication *tpubl;
998 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900999
Per Liden4323add2006-01-18 00:38:21 +01001000 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 if (!p_ptr)
1002 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001004 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001006 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001007 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001009 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001011 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 &p_ptr->publications, pport_list) {
1013 if (publ->scope != scope)
1014 continue;
1015 if (publ->type != seq->type)
1016 continue;
1017 if (publ->lower != seq->lower)
1018 continue;
1019 if (publ->upper != seq->upper)
1020 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001021 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001022 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001023 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 break;
1025 }
1026 }
1027 if (list_empty(&p_ptr->publications))
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001028 p_ptr->published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001029 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 return res;
1031}
1032
1033int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1034{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001035 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 struct tipc_msg *msg;
1037 int res = -EINVAL;
1038
Per Liden4323add2006-01-18 00:38:21 +01001039 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 if (!p_ptr)
1041 return -EINVAL;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001042 if (p_ptr->published || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 goto exit;
1044 if (!peer->ref)
1045 goto exit;
1046
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001047 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 msg_set_destnode(msg, peer->node);
1049 msg_set_destport(msg, peer->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001051 msg_set_lookup_scope(msg, 0);
Allan Stephens08c80e92010-12-31 18:59:17 +00001052 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053
1054 p_ptr->probing_interval = PROBING_INTERVAL;
1055 p_ptr->probing_state = CONFIRMED;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001056 p_ptr->connected = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1058
Allan Stephens0e659672010-12-31 18:59:32 +00001059 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001060 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001062 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063exit:
Per Liden4323add2006-01-18 00:38:21 +01001064 tipc_port_unlock(p_ptr);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001065 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 return res;
1067}
1068
Allan Stephens0c3141e2008-04-15 00:22:02 -07001069/**
1070 * tipc_disconnect_port - disconnect port from peer
1071 *
1072 * Port must be locked.
1073 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001074int tipc_disconnect_port(struct tipc_port *tp_ptr)
1075{
1076 int res;
1077
1078 if (tp_ptr->connected) {
1079 tp_ptr->connected = 0;
1080 /* let timer expire on it's own to avoid deadlock! */
Joe Perchese3192692012-06-03 17:41:40 +00001081 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001082 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001083 } else {
1084 res = -ENOTCONN;
1085 }
1086 return res;
1087}
1088
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089/*
1090 * tipc_disconnect(): Disconnect port form peer.
1091 * This is a node local operation.
1092 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093int tipc_disconnect(u32 ref)
1094{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001095 struct tipc_port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001096 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097
Per Liden4323add2006-01-18 00:38:21 +01001098 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099 if (!p_ptr)
1100 return -EINVAL;
Joe Perchese3192692012-06-03 17:41:40 +00001101 res = tipc_disconnect_port(p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001102 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 return res;
1104}
1105
1106/*
1107 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1108 */
1109int tipc_shutdown(u32 ref)
1110{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001111 struct tipc_port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001112 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113
Per Liden4323add2006-01-18 00:38:21 +01001114 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 if (!p_ptr)
1116 return -EINVAL;
1117
Allan Stephense244a912011-05-31 16:10:08 -04001118 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
Per Liden4323add2006-01-18 00:38:21 +01001119 tipc_port_unlock(p_ptr);
1120 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 return tipc_disconnect(ref);
1122}
1123
Allan Stephens9b641252011-11-09 13:29:18 -05001124/**
1125 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
1126 */
Allan Stephens9b641252011-11-09 13:29:18 -05001127int tipc_port_recv_msg(struct sk_buff *buf)
1128{
1129 struct tipc_port *p_ptr;
1130 struct tipc_msg *msg = buf_msg(buf);
1131 u32 destport = msg_destport(msg);
1132 u32 dsz = msg_data_sz(msg);
1133 u32 err;
1134
1135 /* forward unresolved named message */
1136 if (unlikely(!destport)) {
1137 tipc_net_route_msg(buf);
1138 return dsz;
1139 }
1140
1141 /* validate destination & pass to port, otherwise reject message */
1142 p_ptr = tipc_port_lock(destport);
1143 if (likely(p_ptr)) {
Allan Stephens9b641252011-11-09 13:29:18 -05001144 err = p_ptr->dispatcher(p_ptr, buf);
1145 tipc_port_unlock(p_ptr);
1146 if (likely(!err))
1147 return dsz;
1148 } else {
1149 err = TIPC_ERR_NO_PORT;
1150 }
Allan Stephensf0712e862012-04-17 18:42:28 -04001151
Allan Stephens9b641252011-11-09 13:29:18 -05001152 return tipc_reject_msg(buf, err);
1153}
1154
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155/*
Per Liden4323add2006-01-18 00:38:21 +01001156 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 * message for this node.
1158 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001159static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001160 struct iovec const *msg_sect,
1161 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162{
1163 struct sk_buff *buf;
1164 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001165
Allan Stephens26896902011-04-21 10:42:07 -05001166 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 MAX_MSG_SIZE, !sender->user_port, &buf);
1168 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001169 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 return res;
1171}
1172
1173/**
1174 * tipc_send - send message sections on connection
1175 */
Allan Stephens26896902011-04-21 10:42:07 -05001176int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1177 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001179 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 u32 destnode;
1181 int res;
1182
Per Liden4323add2006-01-18 00:38:21 +01001183 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001184 if (!p_ptr || !p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 return -EINVAL;
1186
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001187 p_ptr->congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001188 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 destnode = port_peernode(p_ptr);
Allan Stephens8a55fe72012-04-18 09:27:22 -04001190 if (likely(!in_own_node(destnode)))
Per Liden4323add2006-01-18 00:38:21 +01001191 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001192 total_len, destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 else
Allan Stephens26896902011-04-21 10:42:07 -05001194 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1195 total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196
1197 if (likely(res != -ELINKCONG)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001198 p_ptr->congested = 0;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001199 if (res > 0)
1200 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 return res;
1202 }
1203 }
1204 if (port_unreliable(p_ptr)) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001205 p_ptr->congested = 0;
Allan Stephens26896902011-04-21 10:42:07 -05001206 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 }
1208 return -ELINKCONG;
1209}
1210
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001211/**
Allan Stephens12bae472010-11-30 12:01:02 +00001212 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 */
Allan Stephens12bae472010-11-30 12:01:02 +00001214int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
Allan Stephens26896902011-04-21 10:42:07 -05001215 unsigned int num_sect, struct iovec const *msg_sect,
1216 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001218 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 struct tipc_msg *msg;
1220 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001221 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 int res;
1223
Per Liden4323add2006-01-18 00:38:21 +01001224 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001225 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 return -EINVAL;
1227
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001228 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001230 msg_set_hdr_sz(msg, NAMED_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 msg_set_nametype(msg, name->type);
1232 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001233 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001234 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 msg_set_destnode(msg, destnode);
1236 msg_set_destport(msg, destport);
1237
Allan Stephensbc9f8142011-11-07 17:00:54 -05001238 if (likely(destport || destnode)) {
Allan Stephens8a55fe72012-04-18 09:27:22 -04001239 if (likely(in_own_node(destnode)))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001240 res = tipc_port_recv_sections(p_ptr, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001241 msg_sect, total_len);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001242 else if (tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001243 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001244 num_sect, total_len,
1245 destnode);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001246 else
1247 res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
1248 num_sect, total_len,
1249 TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001250 if (likely(res != -ELINKCONG)) {
1251 if (res > 0)
1252 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001254 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001256 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 }
1258 return -ELINKCONG;
1259 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001260 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001261 total_len, TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262}
1263
1264/**
Allan Stephens12bae472010-11-30 12:01:02 +00001265 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 */
Allan Stephens12bae472010-11-30 12:01:02 +00001267int tipc_send2port(u32 ref, struct tipc_portid const *dest,
Allan Stephens26896902011-04-21 10:42:07 -05001268 unsigned int num_sect, struct iovec const *msg_sect,
1269 unsigned int total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001271 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 struct tipc_msg *msg;
1273 int res;
1274
Per Liden4323add2006-01-18 00:38:21 +01001275 p_ptr = tipc_port_deref(ref);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001276 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 return -EINVAL;
1278
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001279 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens53b94362011-04-17 16:02:11 -04001281 msg_set_lookup_scope(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282 msg_set_destnode(msg, dest->node);
1283 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001284 msg_set_hdr_sz(msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001285
Allan Stephens8a55fe72012-04-18 09:27:22 -04001286 if (in_own_node(dest->node))
Allan Stephens26896902011-04-21 10:42:07 -05001287 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1288 total_len);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001289 else if (tipc_own_addr)
Allan Stephenscb7ce912011-01-24 15:02:14 -05001290 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Allan Stephens26896902011-04-21 10:42:07 -05001291 total_len, dest->node);
Allan Stephensb8f683d2012-04-18 09:22:56 -04001292 else
1293 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1294 total_len, TIPC_ERR_NO_NODE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001295 if (likely(res != -ELINKCONG)) {
1296 if (res > 0)
1297 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001299 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 if (port_unreliable(p_ptr)) {
Allan Stephens26896902011-04-21 10:42:07 -05001301 return total_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 }
1303 return -ELINKCONG;
1304}
1305
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001306/**
Allan Stephens12bae472010-11-30 12:01:02 +00001307 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 */
Allan Stephens12bae472010-11-30 12:01:02 +00001309int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1310 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001312 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 struct tipc_msg *msg;
1314 int res;
1315
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001316 p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1317 if (!p_ptr || p_ptr->connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 return -EINVAL;
1319
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001320 msg = &p_ptr->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 msg_set_type(msg, TIPC_DIRECT_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 msg_set_destnode(msg, dest->node);
1323 msg_set_destport(msg, dest->ref);
Allan Stephens741d9eb2011-05-31 15:03:18 -04001324 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1325 msg_set_size(msg, BASIC_H_SIZE + dsz);
1326 if (skb_cow(buf, BASIC_H_SIZE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327 return -ENOMEM;
1328
Allan Stephens741d9eb2011-05-31 15:03:18 -04001329 skb_push(buf, BASIC_H_SIZE);
1330 skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
Allan Stephenscb7ce912011-01-24 15:02:14 -05001331
Allan Stephens8a55fe72012-04-18 09:27:22 -04001332 if (in_own_node(dest->node))
Allan Stephenscb7ce912011-01-24 15:02:14 -05001333 res = tipc_port_recv_msg(buf);
1334 else
1335 res = tipc_send_buf_fast(buf, dest->node);
1336 if (likely(res != -ELINKCONG)) {
1337 if (res > 0)
1338 p_ptr->sent++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 return res;
Allan Stephenscb7ce912011-01-24 15:02:14 -05001340 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 if (port_unreliable(p_ptr))
1342 return dsz;
1343 return -ELINKCONG;
1344}