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