Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[cascardo/linux.git] / net / tipc / port.c
1 /*
2  * net/tipc/port.c: TIPC port code
3  *
4  * Copyright (c) 1992-2007, 2014, Ericsson AB
5  * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the names of the copyright holders nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include "core.h"
38 #include "config.h"
39 #include "port.h"
40 #include "name_table.h"
41 #include "socket.h"
42
43 /* Connection management: */
44 #define PROBING_INTERVAL 3600000        /* [ms] => 1 h */
45
46 #define MAX_REJECT_SIZE 1024
47
48 DEFINE_SPINLOCK(tipc_port_list_lock);
49
50 static LIST_HEAD(ports);
51 static void port_handle_node_down(unsigned long ref);
52 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
53 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
54 static void port_timeout(unsigned long ref);
55
56 /**
57  * tipc_port_peer_msg - verify message was sent by connected port's peer
58  *
59  * Handles cases where the node's network address has changed from
60  * the default of <0.0.0> to its configured setting.
61  */
62 int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
63 {
64         u32 peernode;
65         u32 orignode;
66
67         if (msg_origport(msg) != tipc_port_peerport(p_ptr))
68                 return 0;
69
70         orignode = msg_orignode(msg);
71         peernode = tipc_port_peernode(p_ptr);
72         return (orignode == peernode) ||
73                 (!orignode && (peernode == tipc_own_addr)) ||
74                 (!peernode && (orignode == tipc_own_addr));
75 }
76
77 /* tipc_port_init - intiate TIPC port and lock it
78  *
79  * Returns obtained reference if initialization is successful, zero otherwise
80  */
81 u32 tipc_port_init(struct tipc_port *p_ptr,
82                    const unsigned int importance)
83 {
84         struct tipc_msg *msg;
85         u32 ref;
86
87         ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
88         if (!ref) {
89                 pr_warn("Port registration failed, ref. table exhausted\n");
90                 return 0;
91         }
92
93         p_ptr->max_pkt = MAX_PKT_DEFAULT;
94         p_ptr->ref = ref;
95         INIT_LIST_HEAD(&p_ptr->wait_list);
96         INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
97         k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
98         INIT_LIST_HEAD(&p_ptr->publications);
99         INIT_LIST_HEAD(&p_ptr->port_list);
100
101         /*
102          * Must hold port list lock while initializing message header template
103          * to ensure a change to node's own network address doesn't result
104          * in template containing out-dated network address information
105          */
106         spin_lock_bh(&tipc_port_list_lock);
107         msg = &p_ptr->phdr;
108         tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
109         msg_set_origport(msg, ref);
110         list_add_tail(&p_ptr->port_list, &ports);
111         spin_unlock_bh(&tipc_port_list_lock);
112         return ref;
113 }
114
115 void tipc_port_destroy(struct tipc_port *p_ptr)
116 {
117         struct sk_buff *buf = NULL;
118         struct tipc_msg *msg = NULL;
119         u32 peer;
120
121         tipc_withdraw(p_ptr, 0, NULL);
122
123         spin_lock_bh(p_ptr->lock);
124         tipc_ref_discard(p_ptr->ref);
125         spin_unlock_bh(p_ptr->lock);
126
127         k_cancel_timer(&p_ptr->timer);
128         if (p_ptr->connected) {
129                 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
130                 tipc_nodesub_unsubscribe(&p_ptr->subscription);
131                 msg = buf_msg(buf);
132                 peer = msg_destnode(msg);
133                 tipc_link_xmit(buf, peer, msg_link_selector(msg));
134         }
135         spin_lock_bh(&tipc_port_list_lock);
136         list_del(&p_ptr->port_list);
137         list_del(&p_ptr->wait_list);
138         spin_unlock_bh(&tipc_port_list_lock);
139         k_term_timer(&p_ptr->timer);
140 }
141
142 /*
143  * port_build_proto_msg(): create connection protocol message for port
144  *
145  * On entry the port must be locked and connected.
146  */
147 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
148                                             u32 type, u32 ack)
149 {
150         struct sk_buff *buf;
151         struct tipc_msg *msg;
152
153         buf = tipc_buf_acquire(INT_H_SIZE);
154         if (buf) {
155                 msg = buf_msg(buf);
156                 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
157                               tipc_port_peernode(p_ptr));
158                 msg_set_destport(msg, tipc_port_peerport(p_ptr));
159                 msg_set_origport(msg, p_ptr->ref);
160                 msg_set_msgcnt(msg, ack);
161                 buf->next = NULL;
162         }
163         return buf;
164 }
165
166 static void port_timeout(unsigned long ref)
167 {
168         struct tipc_port *p_ptr = tipc_port_lock(ref);
169         struct sk_buff *buf = NULL;
170         struct tipc_msg *msg = NULL;
171
172         if (!p_ptr)
173                 return;
174
175         if (!p_ptr->connected) {
176                 tipc_port_unlock(p_ptr);
177                 return;
178         }
179
180         /* Last probe answered ? */
181         if (p_ptr->probing_state == TIPC_CONN_PROBING) {
182                 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
183         } else {
184                 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
185                 p_ptr->probing_state = TIPC_CONN_PROBING;
186                 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
187         }
188         tipc_port_unlock(p_ptr);
189         msg = buf_msg(buf);
190         tipc_link_xmit(buf, msg_destnode(msg),  msg_link_selector(msg));
191 }
192
193
194 static void port_handle_node_down(unsigned long ref)
195 {
196         struct tipc_port *p_ptr = tipc_port_lock(ref);
197         struct sk_buff *buf = NULL;
198         struct tipc_msg *msg = NULL;
199
200         if (!p_ptr)
201                 return;
202         buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
203         tipc_port_unlock(p_ptr);
204         msg = buf_msg(buf);
205         tipc_link_xmit(buf, msg_destnode(msg),  msg_link_selector(msg));
206 }
207
208
209 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
210 {
211         struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
212
213         if (buf) {
214                 struct tipc_msg *msg = buf_msg(buf);
215                 msg_swap_words(msg, 4, 5);
216                 msg_swap_words(msg, 6, 7);
217                 buf->next = NULL;
218         }
219         return buf;
220 }
221
222
223 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
224 {
225         struct sk_buff *buf;
226         struct tipc_msg *msg;
227         u32 imp;
228
229         if (!p_ptr->connected)
230                 return NULL;
231
232         buf = tipc_buf_acquire(BASIC_H_SIZE);
233         if (buf) {
234                 msg = buf_msg(buf);
235                 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
236                 msg_set_hdr_sz(msg, BASIC_H_SIZE);
237                 msg_set_size(msg, BASIC_H_SIZE);
238                 imp = msg_importance(msg);
239                 if (imp < TIPC_CRITICAL_IMPORTANCE)
240                         msg_set_importance(msg, ++imp);
241                 msg_set_errcode(msg, err);
242                 buf->next = NULL;
243         }
244         return buf;
245 }
246
247 static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
248 {
249         struct publication *publ;
250         int ret;
251
252         if (full_id)
253                 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
254                                     tipc_zone(tipc_own_addr),
255                                     tipc_cluster(tipc_own_addr),
256                                     tipc_node(tipc_own_addr), p_ptr->ref);
257         else
258                 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
259
260         if (p_ptr->connected) {
261                 u32 dport = tipc_port_peerport(p_ptr);
262                 u32 destnode = tipc_port_peernode(p_ptr);
263
264                 ret += tipc_snprintf(buf + ret, len - ret,
265                                      " connected to <%u.%u.%u:%u>",
266                                      tipc_zone(destnode),
267                                      tipc_cluster(destnode),
268                                      tipc_node(destnode), dport);
269                 if (p_ptr->conn_type != 0)
270                         ret += tipc_snprintf(buf + ret, len - ret,
271                                              " via {%u,%u}", p_ptr->conn_type,
272                                              p_ptr->conn_instance);
273         } else if (p_ptr->published) {
274                 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
275                 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
276                         if (publ->lower == publ->upper)
277                                 ret += tipc_snprintf(buf + ret, len - ret,
278                                                      " {%u,%u}", publ->type,
279                                                      publ->lower);
280                         else
281                                 ret += tipc_snprintf(buf + ret, len - ret,
282                                                      " {%u,%u,%u}", publ->type,
283                                                      publ->lower, publ->upper);
284                 }
285         }
286         ret += tipc_snprintf(buf + ret, len - ret, "\n");
287         return ret;
288 }
289
290 struct sk_buff *tipc_port_get_ports(void)
291 {
292         struct sk_buff *buf;
293         struct tlv_desc *rep_tlv;
294         char *pb;
295         int pb_len;
296         struct tipc_port *p_ptr;
297         int str_len = 0;
298
299         buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
300         if (!buf)
301                 return NULL;
302         rep_tlv = (struct tlv_desc *)buf->data;
303         pb = TLV_DATA(rep_tlv);
304         pb_len = ULTRA_STRING_MAX_LEN;
305
306         spin_lock_bh(&tipc_port_list_lock);
307         list_for_each_entry(p_ptr, &ports, port_list) {
308                 spin_lock_bh(p_ptr->lock);
309                 str_len += port_print(p_ptr, pb, pb_len, 0);
310                 spin_unlock_bh(p_ptr->lock);
311         }
312         spin_unlock_bh(&tipc_port_list_lock);
313         str_len += 1;   /* for "\0" */
314         skb_put(buf, TLV_SPACE(str_len));
315         TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
316
317         return buf;
318 }
319
320 void tipc_port_reinit(void)
321 {
322         struct tipc_port *p_ptr;
323         struct tipc_msg *msg;
324
325         spin_lock_bh(&tipc_port_list_lock);
326         list_for_each_entry(p_ptr, &ports, port_list) {
327                 msg = &p_ptr->phdr;
328                 msg_set_prevnode(msg, tipc_own_addr);
329                 msg_set_orignode(msg, tipc_own_addr);
330         }
331         spin_unlock_bh(&tipc_port_list_lock);
332 }
333
334 void tipc_acknowledge(u32 ref, u32 ack)
335 {
336         struct tipc_port *p_ptr;
337         struct sk_buff *buf = NULL;
338         struct tipc_msg *msg;
339
340         p_ptr = tipc_port_lock(ref);
341         if (!p_ptr)
342                 return;
343         if (p_ptr->connected)
344                 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
345
346         tipc_port_unlock(p_ptr);
347         if (!buf)
348                 return;
349         msg = buf_msg(buf);
350         tipc_link_xmit(buf, msg_destnode(msg),  msg_link_selector(msg));
351 }
352
353 int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
354                  struct tipc_name_seq const *seq)
355 {
356         struct publication *publ;
357         u32 key;
358
359         if (p_ptr->connected)
360                 return -EINVAL;
361         key = p_ptr->ref + p_ptr->pub_count + 1;
362         if (key == p_ptr->ref)
363                 return -EADDRINUSE;
364
365         publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
366                                     scope, p_ptr->ref, key);
367         if (publ) {
368                 list_add(&publ->pport_list, &p_ptr->publications);
369                 p_ptr->pub_count++;
370                 p_ptr->published = 1;
371                 return 0;
372         }
373         return -EINVAL;
374 }
375
376 int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
377                   struct tipc_name_seq const *seq)
378 {
379         struct publication *publ;
380         struct publication *tpubl;
381         int res = -EINVAL;
382
383         if (!seq) {
384                 list_for_each_entry_safe(publ, tpubl,
385                                          &p_ptr->publications, pport_list) {
386                         tipc_nametbl_withdraw(publ->type, publ->lower,
387                                               publ->ref, publ->key);
388                 }
389                 res = 0;
390         } else {
391                 list_for_each_entry_safe(publ, tpubl,
392                                          &p_ptr->publications, pport_list) {
393                         if (publ->scope != scope)
394                                 continue;
395                         if (publ->type != seq->type)
396                                 continue;
397                         if (publ->lower != seq->lower)
398                                 continue;
399                         if (publ->upper != seq->upper)
400                                 break;
401                         tipc_nametbl_withdraw(publ->type, publ->lower,
402                                               publ->ref, publ->key);
403                         res = 0;
404                         break;
405                 }
406         }
407         if (list_empty(&p_ptr->publications))
408                 p_ptr->published = 0;
409         return res;
410 }
411
412 int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
413 {
414         struct tipc_port *p_ptr;
415         int res;
416
417         p_ptr = tipc_port_lock(ref);
418         if (!p_ptr)
419                 return -EINVAL;
420         res = __tipc_port_connect(ref, p_ptr, peer);
421         tipc_port_unlock(p_ptr);
422         return res;
423 }
424
425 /*
426  * __tipc_port_connect - connect to a remote peer
427  *
428  * Port must be locked.
429  */
430 int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
431                         struct tipc_portid const *peer)
432 {
433         struct tipc_msg *msg;
434         int res = -EINVAL;
435
436         if (p_ptr->published || p_ptr->connected)
437                 goto exit;
438         if (!peer->ref)
439                 goto exit;
440
441         msg = &p_ptr->phdr;
442         msg_set_destnode(msg, peer->node);
443         msg_set_destport(msg, peer->ref);
444         msg_set_type(msg, TIPC_CONN_MSG);
445         msg_set_lookup_scope(msg, 0);
446         msg_set_hdr_sz(msg, SHORT_H_SIZE);
447
448         p_ptr->probing_interval = PROBING_INTERVAL;
449         p_ptr->probing_state = TIPC_CONN_OK;
450         p_ptr->connected = 1;
451         k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
452
453         tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
454                           (void *)(unsigned long)ref,
455                           (net_ev_handler)port_handle_node_down);
456         res = 0;
457 exit:
458         p_ptr->max_pkt = tipc_node_get_mtu(peer->node, ref);
459         return res;
460 }
461
462 /*
463  * __tipc_disconnect - disconnect port from peer
464  *
465  * Port must be locked.
466  */
467 int __tipc_port_disconnect(struct tipc_port *tp_ptr)
468 {
469         if (tp_ptr->connected) {
470                 tp_ptr->connected = 0;
471                 /* let timer expire on it's own to avoid deadlock! */
472                 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
473                 return 0;
474         }
475
476         return -ENOTCONN;
477 }
478
479 /*
480  * tipc_port_disconnect(): Disconnect port form peer.
481  *                    This is a node local operation.
482  */
483 int tipc_port_disconnect(u32 ref)
484 {
485         struct tipc_port *p_ptr;
486         int res;
487
488         p_ptr = tipc_port_lock(ref);
489         if (!p_ptr)
490                 return -EINVAL;
491         res = __tipc_port_disconnect(p_ptr);
492         tipc_port_unlock(p_ptr);
493         return res;
494 }
495
496 /*
497  * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
498  */
499 int tipc_port_shutdown(u32 ref)
500 {
501         struct tipc_msg *msg;
502         struct tipc_port *p_ptr;
503         struct sk_buff *buf = NULL;
504
505         p_ptr = tipc_port_lock(ref);
506         if (!p_ptr)
507                 return -EINVAL;
508
509         buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
510         tipc_port_unlock(p_ptr);
511         msg = buf_msg(buf);
512         tipc_link_xmit(buf, msg_destnode(msg),  msg_link_selector(msg));
513         return tipc_port_disconnect(ref);
514 }