b0372bb107f67531533402a5848be3ade62d3b32
[cascardo/linux.git] / net / tipc / node.c
1 /*
2  * net/tipc/node.c: TIPC node management routines
3  *
4  * Copyright (c) 2000-2006, 2012-2015, Ericsson AB
5  * Copyright (c) 2005-2006, 2010-2014, 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 "link.h"
39 #include "node.h"
40 #include "name_distr.h"
41 #include "socket.h"
42 #include "bcast.h"
43 #include "discover.h"
44
45 /* Node FSM states and events:
46  */
47 enum {
48         SELF_DOWN_PEER_DOWN    = 0xdd,
49         SELF_UP_PEER_UP        = 0xaa,
50         SELF_DOWN_PEER_LEAVING = 0xd1,
51         SELF_UP_PEER_COMING    = 0xac,
52         SELF_COMING_PEER_UP    = 0xca,
53         SELF_LEAVING_PEER_DOWN = 0x1d,
54         NODE_FAILINGOVER       = 0xf0,
55         NODE_SYNCHING          = 0xcc
56 };
57
58 enum {
59         SELF_ESTABL_CONTACT_EVT = 0xece,
60         SELF_LOST_CONTACT_EVT   = 0x1ce,
61         PEER_ESTABL_CONTACT_EVT = 0x9ece,
62         PEER_LOST_CONTACT_EVT   = 0x91ce,
63         NODE_FAILOVER_BEGIN_EVT = 0xfbe,
64         NODE_FAILOVER_END_EVT   = 0xfee,
65         NODE_SYNCH_BEGIN_EVT    = 0xcbe,
66         NODE_SYNCH_END_EVT      = 0xcee
67 };
68
69 static void tipc_node_link_down(struct tipc_node *n, int bearer_id);
70 static void node_lost_contact(struct tipc_node *n_ptr);
71 static void node_established_contact(struct tipc_node *n_ptr);
72 static void tipc_node_delete(struct tipc_node *node);
73 static void tipc_node_timeout(unsigned long data);
74 static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
75
76 struct tipc_sock_conn {
77         u32 port;
78         u32 peer_port;
79         u32 peer_node;
80         struct list_head list;
81 };
82
83 static const struct nla_policy tipc_nl_node_policy[TIPC_NLA_NODE_MAX + 1] = {
84         [TIPC_NLA_NODE_UNSPEC]          = { .type = NLA_UNSPEC },
85         [TIPC_NLA_NODE_ADDR]            = { .type = NLA_U32 },
86         [TIPC_NLA_NODE_UP]              = { .type = NLA_FLAG }
87 };
88
89 /*
90  * A trivial power-of-two bitmask technique is used for speed, since this
91  * operation is done for every incoming TIPC packet. The number of hash table
92  * entries has been chosen so that no hash chain exceeds 8 nodes and will
93  * usually be much smaller (typically only a single node).
94  */
95 static unsigned int tipc_hashfn(u32 addr)
96 {
97         return addr & (NODE_HTABLE_SIZE - 1);
98 }
99
100 static void tipc_node_kref_release(struct kref *kref)
101 {
102         struct tipc_node *node = container_of(kref, struct tipc_node, kref);
103
104         tipc_node_delete(node);
105 }
106
107 void tipc_node_put(struct tipc_node *node)
108 {
109         kref_put(&node->kref, tipc_node_kref_release);
110 }
111
112 static void tipc_node_get(struct tipc_node *node)
113 {
114         kref_get(&node->kref);
115 }
116
117 /*
118  * tipc_node_find - locate specified node object, if it exists
119  */
120 struct tipc_node *tipc_node_find(struct net *net, u32 addr)
121 {
122         struct tipc_net *tn = net_generic(net, tipc_net_id);
123         struct tipc_node *node;
124
125         if (unlikely(!in_own_cluster_exact(net, addr)))
126                 return NULL;
127
128         rcu_read_lock();
129         hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)],
130                                  hash) {
131                 if (node->addr == addr) {
132                         tipc_node_get(node);
133                         rcu_read_unlock();
134                         return node;
135                 }
136         }
137         rcu_read_unlock();
138         return NULL;
139 }
140
141 struct tipc_node *tipc_node_create(struct net *net, u32 addr)
142 {
143         struct tipc_net *tn = net_generic(net, tipc_net_id);
144         struct tipc_node *n_ptr, *temp_node;
145
146         spin_lock_bh(&tn->node_list_lock);
147         n_ptr = tipc_node_find(net, addr);
148         if (n_ptr)
149                 goto exit;
150         n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
151         if (!n_ptr) {
152                 pr_warn("Node creation failed, no memory\n");
153                 goto exit;
154         }
155         n_ptr->addr = addr;
156         n_ptr->net = net;
157         kref_init(&n_ptr->kref);
158         spin_lock_init(&n_ptr->lock);
159         INIT_HLIST_NODE(&n_ptr->hash);
160         INIT_LIST_HEAD(&n_ptr->list);
161         INIT_LIST_HEAD(&n_ptr->publ_list);
162         INIT_LIST_HEAD(&n_ptr->conn_sks);
163         skb_queue_head_init(&n_ptr->bclink.namedq);
164         __skb_queue_head_init(&n_ptr->bclink.deferdq);
165         hlist_add_head_rcu(&n_ptr->hash, &tn->node_htable[tipc_hashfn(addr)]);
166         list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
167                 if (n_ptr->addr < temp_node->addr)
168                         break;
169         }
170         list_add_tail_rcu(&n_ptr->list, &temp_node->list);
171         n_ptr->state = SELF_DOWN_PEER_LEAVING;
172         n_ptr->signature = INVALID_NODE_SIG;
173         n_ptr->active_links[0] = INVALID_BEARER_ID;
174         n_ptr->active_links[1] = INVALID_BEARER_ID;
175         tipc_node_get(n_ptr);
176         setup_timer(&n_ptr->timer, tipc_node_timeout, (unsigned long)n_ptr);
177         n_ptr->keepalive_intv = U32_MAX;
178 exit:
179         spin_unlock_bh(&tn->node_list_lock);
180         return n_ptr;
181 }
182
183 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
184 {
185         unsigned long tol = l->tolerance;
186         unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
187         unsigned long keepalive_intv = msecs_to_jiffies(intv);
188
189         /* Link with lowest tolerance determines timer interval */
190         if (keepalive_intv < n->keepalive_intv)
191                 n->keepalive_intv = keepalive_intv;
192
193         /* Ensure link's abort limit corresponds to current interval */
194         l->abort_limit = l->tolerance / jiffies_to_msecs(n->keepalive_intv);
195 }
196
197 static void tipc_node_delete(struct tipc_node *node)
198 {
199         list_del_rcu(&node->list);
200         hlist_del_rcu(&node->hash);
201         kfree_rcu(node, rcu);
202 }
203
204 void tipc_node_stop(struct net *net)
205 {
206         struct tipc_net *tn = net_generic(net, tipc_net_id);
207         struct tipc_node *node, *t_node;
208
209         spin_lock_bh(&tn->node_list_lock);
210         list_for_each_entry_safe(node, t_node, &tn->node_list, list) {
211                 if (del_timer(&node->timer))
212                         tipc_node_put(node);
213                 tipc_node_put(node);
214         }
215         spin_unlock_bh(&tn->node_list_lock);
216 }
217
218 int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
219 {
220         struct tipc_node *node;
221         struct tipc_sock_conn *conn;
222         int err = 0;
223
224         if (in_own_node(net, dnode))
225                 return 0;
226
227         node = tipc_node_find(net, dnode);
228         if (!node) {
229                 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
230                 return -EHOSTUNREACH;
231         }
232         conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
233         if (!conn) {
234                 err = -EHOSTUNREACH;
235                 goto exit;
236         }
237         conn->peer_node = dnode;
238         conn->port = port;
239         conn->peer_port = peer_port;
240
241         tipc_node_lock(node);
242         list_add_tail(&conn->list, &node->conn_sks);
243         tipc_node_unlock(node);
244 exit:
245         tipc_node_put(node);
246         return err;
247 }
248
249 void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
250 {
251         struct tipc_node *node;
252         struct tipc_sock_conn *conn, *safe;
253
254         if (in_own_node(net, dnode))
255                 return;
256
257         node = tipc_node_find(net, dnode);
258         if (!node)
259                 return;
260
261         tipc_node_lock(node);
262         list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
263                 if (port != conn->port)
264                         continue;
265                 list_del(&conn->list);
266                 kfree(conn);
267         }
268         tipc_node_unlock(node);
269         tipc_node_put(node);
270 }
271
272 /* tipc_node_timeout - handle expiration of node timer
273  */
274 static void tipc_node_timeout(unsigned long data)
275 {
276         struct tipc_node *n = (struct tipc_node *)data;
277         struct sk_buff_head xmitq;
278         struct tipc_link *l;
279         struct tipc_media_addr *maddr;
280         int bearer_id;
281         int rc = 0;
282
283         __skb_queue_head_init(&xmitq);
284
285         for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
286                 tipc_node_lock(n);
287                 l = n->links[bearer_id].link;
288                 if (l) {
289                         /* Link tolerance may change asynchronously: */
290                         tipc_node_calculate_timer(n, l);
291                         rc = tipc_link_timeout(l, &xmitq);
292                         if (rc & TIPC_LINK_DOWN_EVT)
293                                 tipc_node_link_down(n, bearer_id);
294                 }
295                 tipc_node_unlock(n);
296                 maddr = &n->links[bearer_id].maddr;
297                 tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
298         }
299         if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
300                 tipc_node_get(n);
301         tipc_node_put(n);
302 }
303
304 /**
305  * tipc_node_link_up - handle addition of link
306  *
307  * Link becomes active (alone or shared) or standby, depending on its priority.
308  */
309 static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
310                               struct sk_buff_head *xmitq)
311 {
312         int *slot0 = &n->active_links[0];
313         int *slot1 = &n->active_links[1];
314         struct tipc_link *ol = node_active_link(n, 0);
315         struct tipc_link *nl = n->links[bearer_id].link;
316
317         if (n->working_links > 1) {
318                 pr_warn("Attempt to establish 3rd link to %x\n", n->addr);
319                 return;
320         }
321         n->working_links++;
322         n->action_flags |= TIPC_NOTIFY_LINK_UP;
323         n->link_id = nl->peer_bearer_id << 16 | bearer_id;
324
325         /* Leave room for tunnel header when returning 'mtu' to users: */
326         n->links[bearer_id].mtu = nl->mtu - INT_H_SIZE;
327
328         tipc_bearer_add_dest(n->net, bearer_id, n->addr);
329
330         pr_debug("Established link <%s> on network plane %c\n",
331                  nl->name, nl->net_plane);
332
333         /* First link? => give it both slots */
334         if (!ol) {
335                 *slot0 = bearer_id;
336                 *slot1 = bearer_id;
337                 nl->exec_mode = TIPC_LINK_OPEN;
338                 node_established_contact(n);
339                 return;
340         }
341
342         /* Second link => redistribute slots */
343         if (nl->priority > ol->priority) {
344                 pr_debug("Old link <%s> becomes standby\n", ol->name);
345                 *slot0 = bearer_id;
346                 *slot1 = bearer_id;
347         } else if (nl->priority == ol->priority) {
348                 *slot0 = bearer_id;
349         } else {
350                 pr_debug("New link <%s> is standby\n", nl->name);
351         }
352
353         /* Prepare synchronization with first link */
354         tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
355 }
356
357 /**
358  * tipc_node_link_down - handle loss of link
359  */
360 static void tipc_node_link_down(struct tipc_node *n, int bearer_id)
361 {
362         int *slot0 = &n->active_links[0];
363         int *slot1 = &n->active_links[1];
364         struct tipc_media_addr *maddr = &n->links[bearer_id].maddr;
365         int i, highest = 0;
366         struct tipc_link *l, *_l, *tnl;
367         struct sk_buff_head xmitq;
368
369         l = n->links[bearer_id].link;
370         if (!l || !tipc_link_is_up(l))
371                 return;
372
373         __skb_queue_head_init(&xmitq);
374
375         n->working_links--;
376         n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
377         n->link_id = l->peer_bearer_id << 16 | bearer_id;
378
379         tipc_bearer_remove_dest(n->net, l->bearer_id, n->addr);
380
381         pr_debug("Lost link <%s> on network plane %c\n",
382                  l->name, l->net_plane);
383
384         /* Select new active link if any available */
385         *slot0 = INVALID_BEARER_ID;
386         *slot1 = INVALID_BEARER_ID;
387         for (i = 0; i < MAX_BEARERS; i++) {
388                 _l = n->links[i].link;
389                 if (!_l || !tipc_link_is_up(_l))
390                         continue;
391                 if (_l == l)
392                         continue;
393                 if (_l->priority < highest)
394                         continue;
395                 if (_l->priority > highest) {
396                         highest = _l->priority;
397                         *slot0 = i;
398                         *slot1 = i;
399                         continue;
400                 }
401                 *slot1 = i;
402         }
403
404         if (!tipc_node_is_up(n)) {
405                 tipc_link_reset(l);
406                 node_lost_contact(n);
407                 return;
408         }
409
410         /* There is still a working link => initiate failover */
411         tnl = node_active_link(n, 0);
412         tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
413         n->sync_point = tnl->rcv_nxt + (U16_MAX / 2 - 1);
414         tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, &xmitq);
415         tipc_link_reset(l);
416         tipc_bearer_xmit(n->net, tnl->bearer_id, &xmitq, maddr);
417 }
418
419 bool tipc_node_is_up(struct tipc_node *n)
420 {
421         return n->active_links[0] != INVALID_BEARER_ID;
422 }
423
424 void tipc_node_check_dest(struct tipc_node *n, struct tipc_bearer *b,
425                           bool *link_up, bool *addr_match,
426                           struct tipc_media_addr *maddr)
427 {
428         struct tipc_link *l = n->links[b->identity].link;
429         struct tipc_media_addr *curr = &n->links[b->identity].maddr;
430
431         *link_up = l && tipc_link_is_up(l);
432         *addr_match = l && !memcmp(curr, maddr, sizeof(*maddr));
433 }
434
435 bool tipc_node_update_dest(struct tipc_node *n,  struct tipc_bearer *b,
436                            struct tipc_media_addr *maddr)
437 {
438         struct tipc_link *l = n->links[b->identity].link;
439         struct tipc_media_addr *curr = &n->links[b->identity].maddr;
440         struct sk_buff_head *inputq = &n->links[b->identity].inputq;
441
442         if (!l) {
443                 l = tipc_link_create(n, b, maddr, inputq, &n->bclink.namedq);
444                 if (!l)
445                         return false;
446                 tipc_node_calculate_timer(n, l);
447                 if (n->link_cnt == 1) {
448                         if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
449                                 tipc_node_get(n);
450                 }
451         }
452         memcpy(&l->media_addr, maddr, sizeof(*maddr));
453         memcpy(curr, maddr, sizeof(*maddr));
454         tipc_node_link_down(n, b->identity);
455         return true;
456 }
457
458 void tipc_node_delete_links(struct net *net, int bearer_id)
459 {
460         struct tipc_net *tn = net_generic(net, tipc_net_id);
461         struct tipc_link *l;
462         struct tipc_node *n;
463
464         rcu_read_lock();
465         list_for_each_entry_rcu(n, &tn->node_list, list) {
466                 tipc_node_lock(n);
467                 l = n->links[bearer_id].link;
468                 if (l) {
469                         tipc_node_link_down(n, bearer_id);
470                         n->links[bearer_id].link = NULL;
471                         n->link_cnt--;
472                 }
473                 tipc_node_unlock(n);
474                 kfree(l);
475         }
476         rcu_read_unlock();
477 }
478
479 static void tipc_node_reset_links(struct tipc_node *n)
480 {
481         char addr_string[16];
482         u32 i;
483
484         tipc_node_lock(n);
485
486         pr_warn("Resetting all links to %s\n",
487                 tipc_addr_string_fill(addr_string, n->addr));
488
489         for (i = 0; i < MAX_BEARERS; i++) {
490                 if (!n->links[i].link)
491                         continue;
492                 tipc_node_link_down(n, i);
493         }
494         tipc_node_unlock(n);
495 }
496
497 void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
498 {
499         n_ptr->links[l_ptr->bearer_id].link = l_ptr;
500         n_ptr->link_cnt++;
501 }
502
503 void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
504 {
505         int i;
506
507         for (i = 0; i < MAX_BEARERS; i++) {
508                 if (l_ptr != n_ptr->links[i].link)
509                         continue;
510                 n_ptr->links[i].link = NULL;
511                 n_ptr->link_cnt--;
512         }
513 }
514
515 /* tipc_node_fsm_evt - node finite state machine
516  * Determines when contact is allowed with peer node
517  */
518 static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
519 {
520         int state = n->state;
521
522         switch (state) {
523         case SELF_DOWN_PEER_DOWN:
524                 switch (evt) {
525                 case SELF_ESTABL_CONTACT_EVT:
526                         state = SELF_UP_PEER_COMING;
527                         break;
528                 case PEER_ESTABL_CONTACT_EVT:
529                         state = SELF_COMING_PEER_UP;
530                         break;
531                 case SELF_LOST_CONTACT_EVT:
532                 case PEER_LOST_CONTACT_EVT:
533                         break;
534                 case NODE_SYNCH_END_EVT:
535                 case NODE_SYNCH_BEGIN_EVT:
536                 case NODE_FAILOVER_BEGIN_EVT:
537                 case NODE_FAILOVER_END_EVT:
538                 default:
539                         goto illegal_evt;
540                 }
541                 break;
542         case SELF_UP_PEER_UP:
543                 switch (evt) {
544                 case SELF_LOST_CONTACT_EVT:
545                         state = SELF_DOWN_PEER_LEAVING;
546                         break;
547                 case PEER_LOST_CONTACT_EVT:
548                         state = SELF_LEAVING_PEER_DOWN;
549                         break;
550                 case NODE_SYNCH_BEGIN_EVT:
551                         state = NODE_SYNCHING;
552                         break;
553                 case NODE_FAILOVER_BEGIN_EVT:
554                         state = NODE_FAILINGOVER;
555                         break;
556                 case SELF_ESTABL_CONTACT_EVT:
557                 case PEER_ESTABL_CONTACT_EVT:
558                 case NODE_SYNCH_END_EVT:
559                 case NODE_FAILOVER_END_EVT:
560                         break;
561                 default:
562                         goto illegal_evt;
563                 }
564                 break;
565         case SELF_DOWN_PEER_LEAVING:
566                 switch (evt) {
567                 case PEER_LOST_CONTACT_EVT:
568                         state = SELF_DOWN_PEER_DOWN;
569                         break;
570                 case SELF_ESTABL_CONTACT_EVT:
571                 case PEER_ESTABL_CONTACT_EVT:
572                 case SELF_LOST_CONTACT_EVT:
573                         break;
574                 case NODE_SYNCH_END_EVT:
575                 case NODE_SYNCH_BEGIN_EVT:
576                 case NODE_FAILOVER_BEGIN_EVT:
577                 case NODE_FAILOVER_END_EVT:
578                 default:
579                         goto illegal_evt;
580                 }
581                 break;
582         case SELF_UP_PEER_COMING:
583                 switch (evt) {
584                 case PEER_ESTABL_CONTACT_EVT:
585                         state = SELF_UP_PEER_UP;
586                         break;
587                 case SELF_LOST_CONTACT_EVT:
588                         state = SELF_DOWN_PEER_LEAVING;
589                         break;
590                 case SELF_ESTABL_CONTACT_EVT:
591                 case PEER_LOST_CONTACT_EVT:
592                         break;
593                 case NODE_SYNCH_END_EVT:
594                 case NODE_SYNCH_BEGIN_EVT:
595                 case NODE_FAILOVER_BEGIN_EVT:
596                 case NODE_FAILOVER_END_EVT:
597                 default:
598                         goto illegal_evt;
599                 }
600                 break;
601         case SELF_COMING_PEER_UP:
602                 switch (evt) {
603                 case SELF_ESTABL_CONTACT_EVT:
604                         state = SELF_UP_PEER_UP;
605                         break;
606                 case PEER_LOST_CONTACT_EVT:
607                         state = SELF_LEAVING_PEER_DOWN;
608                         break;
609                 case SELF_LOST_CONTACT_EVT:
610                 case PEER_ESTABL_CONTACT_EVT:
611                         break;
612                 case NODE_SYNCH_END_EVT:
613                 case NODE_SYNCH_BEGIN_EVT:
614                 case NODE_FAILOVER_BEGIN_EVT:
615                 case NODE_FAILOVER_END_EVT:
616                 default:
617                         goto illegal_evt;
618                 }
619                 break;
620         case SELF_LEAVING_PEER_DOWN:
621                 switch (evt) {
622                 case SELF_LOST_CONTACT_EVT:
623                         state = SELF_DOWN_PEER_DOWN;
624                         break;
625                 case SELF_ESTABL_CONTACT_EVT:
626                 case PEER_ESTABL_CONTACT_EVT:
627                 case PEER_LOST_CONTACT_EVT:
628                         break;
629                 case NODE_SYNCH_END_EVT:
630                 case NODE_SYNCH_BEGIN_EVT:
631                 case NODE_FAILOVER_BEGIN_EVT:
632                 case NODE_FAILOVER_END_EVT:
633                 default:
634                         goto illegal_evt;
635                 }
636                 break;
637         case NODE_FAILINGOVER:
638                 switch (evt) {
639                 case SELF_LOST_CONTACT_EVT:
640                         state = SELF_DOWN_PEER_LEAVING;
641                         break;
642                 case PEER_LOST_CONTACT_EVT:
643                         state = SELF_LEAVING_PEER_DOWN;
644                         break;
645                 case NODE_FAILOVER_END_EVT:
646                         state = SELF_UP_PEER_UP;
647                         break;
648                 case NODE_FAILOVER_BEGIN_EVT:
649                 case SELF_ESTABL_CONTACT_EVT:
650                 case PEER_ESTABL_CONTACT_EVT:
651                         break;
652                 case NODE_SYNCH_BEGIN_EVT:
653                 case NODE_SYNCH_END_EVT:
654                 default:
655                         goto illegal_evt;
656                 }
657                 break;
658         case NODE_SYNCHING:
659                 switch (evt) {
660                 case SELF_LOST_CONTACT_EVT:
661                         state = SELF_DOWN_PEER_LEAVING;
662                         break;
663                 case PEER_LOST_CONTACT_EVT:
664                         state = SELF_LEAVING_PEER_DOWN;
665                         break;
666                 case NODE_SYNCH_END_EVT:
667                         state = SELF_UP_PEER_UP;
668                         break;
669                 case NODE_FAILOVER_BEGIN_EVT:
670                         state = NODE_FAILINGOVER;
671                         break;
672                 case NODE_SYNCH_BEGIN_EVT:
673                 case SELF_ESTABL_CONTACT_EVT:
674                 case PEER_ESTABL_CONTACT_EVT:
675                         break;
676                 case NODE_FAILOVER_END_EVT:
677                 default:
678                         goto illegal_evt;
679                 }
680                 break;
681         default:
682                 pr_err("Unknown node fsm state %x\n", state);
683                 break;
684         }
685         n->state = state;
686         return;
687
688 illegal_evt:
689         pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
690 }
691
692 bool tipc_node_filter_pkt(struct tipc_node *n, struct tipc_msg *hdr)
693 {
694         int state = n->state;
695
696         if (likely(state == SELF_UP_PEER_UP))
697                 return true;
698
699         if (state == SELF_LEAVING_PEER_DOWN)
700                 return false;
701
702         if (state == SELF_DOWN_PEER_LEAVING) {
703                 if (msg_peer_node_is_up(hdr))
704                         return false;
705         }
706
707         return true;
708 }
709
710 static void node_established_contact(struct tipc_node *n_ptr)
711 {
712         tipc_node_fsm_evt(n_ptr, SELF_ESTABL_CONTACT_EVT);
713         n_ptr->action_flags |= TIPC_NOTIFY_NODE_UP;
714         n_ptr->bclink.oos_state = 0;
715         n_ptr->bclink.acked = tipc_bclink_get_last_sent(n_ptr->net);
716         tipc_bclink_add_node(n_ptr->net, n_ptr->addr);
717 }
718
719 static void node_lost_contact(struct tipc_node *n_ptr)
720 {
721         char addr_string[16];
722         struct tipc_sock_conn *conn, *safe;
723         struct list_head *conns = &n_ptr->conn_sks;
724         struct sk_buff *skb;
725         struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
726         uint i;
727
728         pr_debug("Lost contact with %s\n",
729                  tipc_addr_string_fill(addr_string, n_ptr->addr));
730
731         /* Flush broadcast link info associated with lost node */
732         if (n_ptr->bclink.recv_permitted) {
733                 __skb_queue_purge(&n_ptr->bclink.deferdq);
734
735                 if (n_ptr->bclink.reasm_buf) {
736                         kfree_skb(n_ptr->bclink.reasm_buf);
737                         n_ptr->bclink.reasm_buf = NULL;
738                 }
739
740                 tipc_bclink_remove_node(n_ptr->net, n_ptr->addr);
741                 tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ);
742
743                 n_ptr->bclink.recv_permitted = false;
744         }
745
746         /* Abort any ongoing link failover */
747         for (i = 0; i < MAX_BEARERS; i++) {
748                 struct tipc_link *l_ptr = n_ptr->links[i].link;
749                 if (!l_ptr)
750                         continue;
751                 l_ptr->exec_mode = TIPC_LINK_OPEN;
752                 kfree_skb(l_ptr->failover_reasm_skb);
753                 l_ptr->failover_reasm_skb = NULL;
754                 tipc_link_reset_fragments(l_ptr);
755         }
756         /* Prevent re-contact with node until cleanup is done */
757         tipc_node_fsm_evt(n_ptr, SELF_LOST_CONTACT_EVT);
758
759         /* Notify publications from this node */
760         n_ptr->action_flags |= TIPC_NOTIFY_NODE_DOWN;
761
762         /* Notify sockets connected to node */
763         list_for_each_entry_safe(conn, safe, conns, list) {
764                 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
765                                       SHORT_H_SIZE, 0, tn->own_addr,
766                                       conn->peer_node, conn->port,
767                                       conn->peer_port, TIPC_ERR_NO_NODE);
768                 if (likely(skb)) {
769                         skb_queue_tail(n_ptr->inputq, skb);
770                         n_ptr->action_flags |= TIPC_MSG_EVT;
771                 }
772                 list_del(&conn->list);
773                 kfree(conn);
774         }
775 }
776
777 /**
778  * tipc_node_get_linkname - get the name of a link
779  *
780  * @bearer_id: id of the bearer
781  * @node: peer node address
782  * @linkname: link name output buffer
783  *
784  * Returns 0 on success
785  */
786 int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
787                            char *linkname, size_t len)
788 {
789         struct tipc_link *link;
790         int err = -EINVAL;
791         struct tipc_node *node = tipc_node_find(net, addr);
792
793         if (!node)
794                 return err;
795
796         if (bearer_id >= MAX_BEARERS)
797                 goto exit;
798
799         tipc_node_lock(node);
800         link = node->links[bearer_id].link;
801         if (link) {
802                 strncpy(linkname, link->name, len);
803                 err = 0;
804         }
805 exit:
806         tipc_node_unlock(node);
807         tipc_node_put(node);
808         return err;
809 }
810
811 void tipc_node_unlock(struct tipc_node *node)
812 {
813         struct net *net = node->net;
814         u32 addr = 0;
815         u32 flags = node->action_flags;
816         u32 link_id = 0;
817         struct list_head *publ_list;
818         struct sk_buff_head *inputq = node->inputq;
819         struct sk_buff_head *namedq;
820
821         if (likely(!flags || (flags == TIPC_MSG_EVT))) {
822                 node->action_flags = 0;
823                 spin_unlock_bh(&node->lock);
824                 if (flags == TIPC_MSG_EVT)
825                         tipc_sk_rcv(net, inputq);
826                 return;
827         }
828
829         addr = node->addr;
830         link_id = node->link_id;
831         namedq = node->namedq;
832         publ_list = &node->publ_list;
833
834         node->action_flags &= ~(TIPC_MSG_EVT |
835                                 TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
836                                 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP |
837                                 TIPC_WAKEUP_BCAST_USERS | TIPC_BCAST_MSG_EVT |
838                                 TIPC_NAMED_MSG_EVT | TIPC_BCAST_RESET);
839
840         spin_unlock_bh(&node->lock);
841
842         if (flags & TIPC_NOTIFY_NODE_DOWN)
843                 tipc_publ_notify(net, publ_list, addr);
844
845         if (flags & TIPC_WAKEUP_BCAST_USERS)
846                 tipc_bclink_wakeup_users(net);
847
848         if (flags & TIPC_NOTIFY_NODE_UP)
849                 tipc_named_node_up(net, addr);
850
851         if (flags & TIPC_NOTIFY_LINK_UP)
852                 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
853                                      TIPC_NODE_SCOPE, link_id, addr);
854
855         if (flags & TIPC_NOTIFY_LINK_DOWN)
856                 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
857                                       link_id, addr);
858
859         if (flags & TIPC_MSG_EVT)
860                 tipc_sk_rcv(net, inputq);
861
862         if (flags & TIPC_NAMED_MSG_EVT)
863                 tipc_named_rcv(net, namedq);
864
865         if (flags & TIPC_BCAST_MSG_EVT)
866                 tipc_bclink_input(net);
867
868         if (flags & TIPC_BCAST_RESET)
869                 tipc_node_reset_links(node);
870 }
871
872 /* Caller should hold node lock for the passed node */
873 static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
874 {
875         void *hdr;
876         struct nlattr *attrs;
877
878         hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
879                           NLM_F_MULTI, TIPC_NL_NODE_GET);
880         if (!hdr)
881                 return -EMSGSIZE;
882
883         attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
884         if (!attrs)
885                 goto msg_full;
886
887         if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
888                 goto attr_msg_full;
889         if (tipc_node_is_up(node))
890                 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
891                         goto attr_msg_full;
892
893         nla_nest_end(msg->skb, attrs);
894         genlmsg_end(msg->skb, hdr);
895
896         return 0;
897
898 attr_msg_full:
899         nla_nest_cancel(msg->skb, attrs);
900 msg_full:
901         genlmsg_cancel(msg->skb, hdr);
902
903         return -EMSGSIZE;
904 }
905
906 static struct tipc_link *tipc_node_select_link(struct tipc_node *n, int sel,
907                                                int *bearer_id,
908                                                struct tipc_media_addr **maddr)
909 {
910         int id = n->active_links[sel & 1];
911
912         if (unlikely(id < 0))
913                 return NULL;
914
915         *bearer_id = id;
916         *maddr = &n->links[id].maddr;
917         return n->links[id].link;
918 }
919
920 /**
921  * tipc_node_xmit() is the general link level function for message sending
922  * @net: the applicable net namespace
923  * @list: chain of buffers containing message
924  * @dnode: address of destination node
925  * @selector: a number used for deterministic link selection
926  * Consumes the buffer chain, except when returning -ELINKCONG
927  * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
928  */
929 int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
930                    u32 dnode, int selector)
931 {
932         struct tipc_link *l = NULL;
933         struct tipc_node *n;
934         struct sk_buff_head xmitq;
935         struct tipc_media_addr *maddr;
936         int bearer_id;
937         int rc = -EHOSTUNREACH;
938
939         __skb_queue_head_init(&xmitq);
940         n = tipc_node_find(net, dnode);
941         if (likely(n)) {
942                 tipc_node_lock(n);
943                 l = tipc_node_select_link(n, selector, &bearer_id, &maddr);
944                 if (likely(l))
945                         rc = tipc_link_xmit(l, list, &xmitq);
946                 if (unlikely(rc == -ENOBUFS))
947                         tipc_node_link_down(n, bearer_id);
948                 tipc_node_unlock(n);
949                 tipc_node_put(n);
950         }
951         if (likely(!rc)) {
952                 tipc_bearer_xmit(net, bearer_id, &xmitq, maddr);
953                 return 0;
954         }
955         if (likely(in_own_node(net, dnode))) {
956                 tipc_sk_rcv(net, list);
957                 return 0;
958         }
959         return rc;
960 }
961
962 /* tipc_node_xmit_skb(): send single buffer to destination
963  * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
964  * messages, which will not be rejected
965  * The only exception is datagram messages rerouted after secondary
966  * lookup, which are rare and safe to dispose of anyway.
967  * TODO: Return real return value, and let callers use
968  * tipc_wait_for_sendpkt() where applicable
969  */
970 int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
971                        u32 selector)
972 {
973         struct sk_buff_head head;
974         int rc;
975
976         skb_queue_head_init(&head);
977         __skb_queue_tail(&head, skb);
978         rc = tipc_node_xmit(net, &head, dnode, selector);
979         if (rc == -ELINKCONG)
980                 kfree_skb(skb);
981         return 0;
982 }
983
984 /**
985  * tipc_node_check_state - check and if necessary update node state
986  * @skb: TIPC packet
987  * @bearer_id: identity of bearer delivering the packet
988  * Returns true if state is ok, otherwise consumes buffer and returns false
989  */
990 static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
991                                   int bearer_id)
992 {
993         struct tipc_msg *hdr = buf_msg(skb);
994         int usr = msg_user(hdr);
995         int mtyp = msg_type(hdr);
996         u16 oseqno = msg_seqno(hdr);
997         u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
998         u16 exp_pkts = msg_msgcnt(hdr);
999         u16 rcv_nxt, syncpt, dlv_nxt;
1000         int state = n->state;
1001         struct tipc_link *l, *pl = NULL;
1002         struct sk_buff_head;
1003         int i;
1004
1005         l = n->links[bearer_id].link;
1006         if (!l)
1007                 return false;
1008         rcv_nxt = l->rcv_nxt;
1009
1010
1011         if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1012                 return true;
1013
1014         /* Find parallel link, if any */
1015         for (i = 0; i < MAX_BEARERS; i++) {
1016                 if ((i != bearer_id) && n->links[i].link) {
1017                         pl = n->links[i].link;
1018                         break;
1019                 }
1020         }
1021
1022         /* Update node accesibility if applicable */
1023         if (state == SELF_UP_PEER_COMING) {
1024                 if (!tipc_link_is_up(l))
1025                         return true;
1026                 if (!msg_peer_link_is_up(hdr))
1027                         return true;
1028                 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1029         }
1030
1031         if (state == SELF_DOWN_PEER_LEAVING) {
1032                 if (msg_peer_node_is_up(hdr))
1033                         return false;
1034                 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1035         }
1036
1037         /* Ignore duplicate packets */
1038         if (less(oseqno, rcv_nxt))
1039                 return true;
1040
1041         /* Initiate or update failover mode if applicable */
1042         if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1043                 syncpt = oseqno + exp_pkts - 1;
1044                 if (pl && tipc_link_is_up(pl)) {
1045                         tipc_node_link_down(n, pl->bearer_id);
1046                         pl->exec_mode = TIPC_LINK_BLOCKED;
1047                 }
1048                 /* If pkts arrive out of order, use lowest calculated syncpt */
1049                 if (less(syncpt, n->sync_point))
1050                         n->sync_point = syncpt;
1051         }
1052
1053         /* Open parallel link when tunnel link reaches synch point */
1054         if ((n->state == NODE_FAILINGOVER) && (more(rcv_nxt, n->sync_point))) {
1055                 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1056                 if (pl)
1057                         pl->exec_mode = TIPC_LINK_OPEN;
1058                 return true;
1059         }
1060
1061         /* Initiate or update synch mode if applicable */
1062         if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG)) {
1063                 syncpt = iseqno + exp_pkts - 1;
1064                 if (n->state == SELF_UP_PEER_UP) {
1065                         n->sync_point = syncpt;
1066                         tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1067                 }
1068                 l->exec_mode = TIPC_LINK_TUNNEL;
1069                 if (less(syncpt, n->sync_point))
1070                         n->sync_point = syncpt;
1071         }
1072
1073         /* Open tunnel link when parallel link reaches synch point */
1074         if ((n->state == NODE_SYNCHING) && (l->exec_mode == TIPC_LINK_TUNNEL)) {
1075                 if (pl)
1076                         dlv_nxt = mod(pl->rcv_nxt - skb_queue_len(pl->inputq));
1077                 if (!pl || more(dlv_nxt, n->sync_point)) {
1078                         tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
1079                         l->exec_mode = TIPC_LINK_OPEN;
1080                         return true;
1081                 }
1082                 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1083                         return true;
1084                 if (usr == LINK_PROTOCOL)
1085                         return true;
1086                 return false;
1087         }
1088         return true;
1089 }
1090
1091 /**
1092  * tipc_rcv - process TIPC packets/messages arriving from off-node
1093  * @net: the applicable net namespace
1094  * @skb: TIPC packet
1095  * @bearer: pointer to bearer message arrived on
1096  *
1097  * Invoked with no locks held. Bearer pointer must point to a valid bearer
1098  * structure (i.e. cannot be NULL), but bearer can be inactive.
1099  */
1100 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1101 {
1102         struct sk_buff_head xmitq;
1103         struct tipc_node *n;
1104         struct tipc_msg *hdr = buf_msg(skb);
1105         int usr = msg_user(hdr);
1106         int bearer_id = b->identity;
1107         struct tipc_link_entry *le;
1108         int rc = 0;
1109
1110         __skb_queue_head_init(&xmitq);
1111
1112         /* Ensure message is well-formed */
1113         if (unlikely(!tipc_msg_validate(skb)))
1114                 goto discard;
1115
1116         /* Handle arrival of a non-unicast link packet */
1117         if (unlikely(msg_non_seq(hdr))) {
1118                 if (usr ==  LINK_CONFIG)
1119                         tipc_disc_rcv(net, skb, b);
1120                 else
1121                         tipc_bclink_rcv(net, skb);
1122                 return;
1123         }
1124
1125         /* Locate neighboring node that sent packet */
1126         n = tipc_node_find(net, msg_prevnode(hdr));
1127         if (unlikely(!n))
1128                 goto discard;
1129         le = &n->links[bearer_id];
1130
1131         tipc_node_lock(n);
1132
1133         /* Is reception permitted at the moment ? */
1134         if (!tipc_node_filter_pkt(n, hdr))
1135                 goto unlock;
1136
1137         if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
1138                 tipc_bclink_sync_state(n, hdr);
1139
1140         /* Release acked broadcast messages */
1141         if (unlikely(n->bclink.acked != msg_bcast_ack(hdr)))
1142                 tipc_bclink_acknowledge(n, msg_bcast_ack(hdr));
1143
1144         /* Check and if necessary update node state */
1145         if (likely(tipc_node_check_state(n, skb, bearer_id))) {
1146                 rc = tipc_link_rcv(le->link, skb, &xmitq);
1147                 skb = NULL;
1148         }
1149
1150         if (unlikely(rc & TIPC_LINK_UP_EVT))
1151                 tipc_node_link_up(n, bearer_id, &xmitq);
1152
1153         if (unlikely(rc & TIPC_LINK_DOWN_EVT))
1154                 tipc_node_link_down(n, bearer_id);
1155 unlock:
1156         tipc_node_unlock(n);
1157
1158         if (!skb_queue_empty(&le->inputq))
1159                 tipc_sk_rcv(net, &le->inputq);
1160
1161         if (!skb_queue_empty(&xmitq))
1162                 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1163
1164         tipc_node_put(n);
1165 discard:
1166         kfree_skb(skb);
1167 }
1168
1169 int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1170 {
1171         int err;
1172         struct net *net = sock_net(skb->sk);
1173         struct tipc_net *tn = net_generic(net, tipc_net_id);
1174         int done = cb->args[0];
1175         int last_addr = cb->args[1];
1176         struct tipc_node *node;
1177         struct tipc_nl_msg msg;
1178
1179         if (done)
1180                 return 0;
1181
1182         msg.skb = skb;
1183         msg.portid = NETLINK_CB(cb->skb).portid;
1184         msg.seq = cb->nlh->nlmsg_seq;
1185
1186         rcu_read_lock();
1187         if (last_addr) {
1188                 node = tipc_node_find(net, last_addr);
1189                 if (!node) {
1190                         rcu_read_unlock();
1191                         /* We never set seq or call nl_dump_check_consistent()
1192                          * this means that setting prev_seq here will cause the
1193                          * consistence check to fail in the netlink callback
1194                          * handler. Resulting in the NLMSG_DONE message having
1195                          * the NLM_F_DUMP_INTR flag set if the node state
1196                          * changed while we released the lock.
1197                          */
1198                         cb->prev_seq = 1;
1199                         return -EPIPE;
1200                 }
1201                 tipc_node_put(node);
1202         }
1203
1204         list_for_each_entry_rcu(node, &tn->node_list, list) {
1205                 if (last_addr) {
1206                         if (node->addr == last_addr)
1207                                 last_addr = 0;
1208                         else
1209                                 continue;
1210                 }
1211
1212                 tipc_node_lock(node);
1213                 err = __tipc_nl_add_node(&msg, node);
1214                 if (err) {
1215                         last_addr = node->addr;
1216                         tipc_node_unlock(node);
1217                         goto out;
1218                 }
1219
1220                 tipc_node_unlock(node);
1221         }
1222         done = 1;
1223 out:
1224         cb->args[0] = done;
1225         cb->args[1] = last_addr;
1226         rcu_read_unlock();
1227
1228         return skb->len;
1229 }