tipc: ensure that first packets on link are sent in order
[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 #include "netlink.h"
45
46 #define INVALID_NODE_SIG        0x10000
47
48 /* Flags used to take different actions according to flag type
49  * TIPC_NOTIFY_NODE_DOWN: notify node is down
50  * TIPC_NOTIFY_NODE_UP: notify node is up
51  * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
52  */
53 enum {
54         TIPC_NOTIFY_NODE_DOWN           = (1 << 3),
55         TIPC_NOTIFY_NODE_UP             = (1 << 4),
56         TIPC_NOTIFY_LINK_UP             = (1 << 6),
57         TIPC_NOTIFY_LINK_DOWN           = (1 << 7)
58 };
59
60 struct tipc_link_entry {
61         struct tipc_link *link;
62         spinlock_t lock; /* per link */
63         u32 mtu;
64         struct sk_buff_head inputq;
65         struct tipc_media_addr maddr;
66 };
67
68 struct tipc_bclink_entry {
69         struct tipc_link *link;
70         struct sk_buff_head inputq1;
71         struct sk_buff_head arrvq;
72         struct sk_buff_head inputq2;
73         struct sk_buff_head namedq;
74 };
75
76 /**
77  * struct tipc_node - TIPC node structure
78  * @addr: network address of node
79  * @ref: reference counter to node object
80  * @lock: rwlock governing access to structure
81  * @net: the applicable net namespace
82  * @hash: links to adjacent nodes in unsorted hash chain
83  * @inputq: pointer to input queue containing messages for msg event
84  * @namedq: pointer to name table input queue with name table messages
85  * @active_links: bearer ids of active links, used as index into links[] array
86  * @links: array containing references to all links to node
87  * @action_flags: bit mask of different types of node actions
88  * @state: connectivity state vs peer node
89  * @sync_point: sequence number where synch/failover is finished
90  * @list: links to adjacent nodes in sorted list of cluster's nodes
91  * @working_links: number of working links to node (both active and standby)
92  * @link_cnt: number of links to node
93  * @capabilities: bitmap, indicating peer node's functional capabilities
94  * @signature: node instance identifier
95  * @link_id: local and remote bearer ids of changing link, if any
96  * @publ_list: list of publications
97  * @rcu: rcu struct for tipc_node
98  */
99 struct tipc_node {
100         u32 addr;
101         struct kref kref;
102         rwlock_t lock;
103         struct net *net;
104         struct hlist_node hash;
105         int active_links[2];
106         struct tipc_link_entry links[MAX_BEARERS];
107         struct tipc_bclink_entry bc_entry;
108         int action_flags;
109         struct list_head list;
110         int state;
111         u16 sync_point;
112         int link_cnt;
113         u16 working_links;
114         u16 capabilities;
115         u32 signature;
116         u32 link_id;
117         struct list_head publ_list;
118         struct list_head conn_sks;
119         unsigned long keepalive_intv;
120         struct timer_list timer;
121         struct rcu_head rcu;
122 };
123
124 /* Node FSM states and events:
125  */
126 enum {
127         SELF_DOWN_PEER_DOWN    = 0xdd,
128         SELF_UP_PEER_UP        = 0xaa,
129         SELF_DOWN_PEER_LEAVING = 0xd1,
130         SELF_UP_PEER_COMING    = 0xac,
131         SELF_COMING_PEER_UP    = 0xca,
132         SELF_LEAVING_PEER_DOWN = 0x1d,
133         NODE_FAILINGOVER       = 0xf0,
134         NODE_SYNCHING          = 0xcc
135 };
136
137 enum {
138         SELF_ESTABL_CONTACT_EVT = 0xece,
139         SELF_LOST_CONTACT_EVT   = 0x1ce,
140         PEER_ESTABL_CONTACT_EVT = 0x9ece,
141         PEER_LOST_CONTACT_EVT   = 0x91ce,
142         NODE_FAILOVER_BEGIN_EVT = 0xfbe,
143         NODE_FAILOVER_END_EVT   = 0xfee,
144         NODE_SYNCH_BEGIN_EVT    = 0xcbe,
145         NODE_SYNCH_END_EVT      = 0xcee
146 };
147
148 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
149                                   struct sk_buff_head *xmitq,
150                                   struct tipc_media_addr **maddr);
151 static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
152                                 bool delete);
153 static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
154 static void tipc_node_delete(struct tipc_node *node);
155 static void tipc_node_timeout(unsigned long data);
156 static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
157 static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
158 static void tipc_node_put(struct tipc_node *node);
159 static bool tipc_node_is_up(struct tipc_node *n);
160
161 struct tipc_sock_conn {
162         u32 port;
163         u32 peer_port;
164         u32 peer_node;
165         struct list_head list;
166 };
167
168 static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
169 {
170         int bearer_id = n->active_links[sel & 1];
171
172         if (unlikely(bearer_id == INVALID_BEARER_ID))
173                 return NULL;
174
175         return n->links[bearer_id].link;
176 }
177
178 int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
179 {
180         struct tipc_node *n;
181         int bearer_id;
182         unsigned int mtu = MAX_MSG_SIZE;
183
184         n = tipc_node_find(net, addr);
185         if (unlikely(!n))
186                 return mtu;
187
188         bearer_id = n->active_links[sel & 1];
189         if (likely(bearer_id != INVALID_BEARER_ID))
190                 mtu = n->links[bearer_id].mtu;
191         tipc_node_put(n);
192         return mtu;
193 }
194 /*
195  * A trivial power-of-two bitmask technique is used for speed, since this
196  * operation is done for every incoming TIPC packet. The number of hash table
197  * entries has been chosen so that no hash chain exceeds 8 nodes and will
198  * usually be much smaller (typically only a single node).
199  */
200 static unsigned int tipc_hashfn(u32 addr)
201 {
202         return addr & (NODE_HTABLE_SIZE - 1);
203 }
204
205 static void tipc_node_kref_release(struct kref *kref)
206 {
207         struct tipc_node *n = container_of(kref, struct tipc_node, kref);
208
209         kfree(n->bc_entry.link);
210         kfree_rcu(n, rcu);
211 }
212
213 static void tipc_node_put(struct tipc_node *node)
214 {
215         kref_put(&node->kref, tipc_node_kref_release);
216 }
217
218 static void tipc_node_get(struct tipc_node *node)
219 {
220         kref_get(&node->kref);
221 }
222
223 /*
224  * tipc_node_find - locate specified node object, if it exists
225  */
226 static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
227 {
228         struct tipc_net *tn = tipc_net(net);
229         struct tipc_node *node;
230         unsigned int thash = tipc_hashfn(addr);
231
232         if (unlikely(!in_own_cluster_exact(net, addr)))
233                 return NULL;
234
235         rcu_read_lock();
236         hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
237                 if (node->addr != addr)
238                         continue;
239                 if (!kref_get_unless_zero(&node->kref))
240                         node = NULL;
241                 break;
242         }
243         rcu_read_unlock();
244         return node;
245 }
246
247 static void tipc_node_read_lock(struct tipc_node *n)
248 {
249         read_lock_bh(&n->lock);
250 }
251
252 static void tipc_node_read_unlock(struct tipc_node *n)
253 {
254         read_unlock_bh(&n->lock);
255 }
256
257 static void tipc_node_write_lock(struct tipc_node *n)
258 {
259         write_lock_bh(&n->lock);
260 }
261
262 static void tipc_node_write_unlock(struct tipc_node *n)
263 {
264         struct net *net = n->net;
265         u32 addr = 0;
266         u32 flags = n->action_flags;
267         u32 link_id = 0;
268         struct list_head *publ_list;
269
270         if (likely(!flags)) {
271                 write_unlock_bh(&n->lock);
272                 return;
273         }
274
275         addr = n->addr;
276         link_id = n->link_id;
277         publ_list = &n->publ_list;
278
279         n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
280                              TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
281
282         write_unlock_bh(&n->lock);
283
284         if (flags & TIPC_NOTIFY_NODE_DOWN)
285                 tipc_publ_notify(net, publ_list, addr);
286
287         if (flags & TIPC_NOTIFY_NODE_UP)
288                 tipc_named_node_up(net, addr);
289
290         if (flags & TIPC_NOTIFY_LINK_UP)
291                 tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
292                                      TIPC_NODE_SCOPE, link_id, addr);
293
294         if (flags & TIPC_NOTIFY_LINK_DOWN)
295                 tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
296                                       link_id, addr);
297 }
298
299 struct tipc_node *tipc_node_create(struct net *net, u32 addr, u16 capabilities)
300 {
301         struct tipc_net *tn = net_generic(net, tipc_net_id);
302         struct tipc_node *n, *temp_node;
303         int i;
304
305         spin_lock_bh(&tn->node_list_lock);
306         n = tipc_node_find(net, addr);
307         if (n)
308                 goto exit;
309         n = kzalloc(sizeof(*n), GFP_ATOMIC);
310         if (!n) {
311                 pr_warn("Node creation failed, no memory\n");
312                 goto exit;
313         }
314         n->addr = addr;
315         n->net = net;
316         n->capabilities = capabilities;
317         kref_init(&n->kref);
318         rwlock_init(&n->lock);
319         INIT_HLIST_NODE(&n->hash);
320         INIT_LIST_HEAD(&n->list);
321         INIT_LIST_HEAD(&n->publ_list);
322         INIT_LIST_HEAD(&n->conn_sks);
323         skb_queue_head_init(&n->bc_entry.namedq);
324         skb_queue_head_init(&n->bc_entry.inputq1);
325         __skb_queue_head_init(&n->bc_entry.arrvq);
326         skb_queue_head_init(&n->bc_entry.inputq2);
327         for (i = 0; i < MAX_BEARERS; i++)
328                 spin_lock_init(&n->links[i].lock);
329         n->state = SELF_DOWN_PEER_LEAVING;
330         n->signature = INVALID_NODE_SIG;
331         n->active_links[0] = INVALID_BEARER_ID;
332         n->active_links[1] = INVALID_BEARER_ID;
333         if (!tipc_link_bc_create(net, tipc_own_addr(net), n->addr,
334                                  U16_MAX,
335                                  tipc_link_window(tipc_bc_sndlink(net)),
336                                  n->capabilities,
337                                  &n->bc_entry.inputq1,
338                                  &n->bc_entry.namedq,
339                                  tipc_bc_sndlink(net),
340                                  &n->bc_entry.link)) {
341                 pr_warn("Broadcast rcv link creation failed, no memory\n");
342                 kfree(n);
343                 n = NULL;
344                 goto exit;
345         }
346         tipc_node_get(n);
347         setup_timer(&n->timer, tipc_node_timeout, (unsigned long)n);
348         n->keepalive_intv = U32_MAX;
349         hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
350         list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
351                 if (n->addr < temp_node->addr)
352                         break;
353         }
354         list_add_tail_rcu(&n->list, &temp_node->list);
355 exit:
356         spin_unlock_bh(&tn->node_list_lock);
357         return n;
358 }
359
360 static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
361 {
362         unsigned long tol = tipc_link_tolerance(l);
363         unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
364         unsigned long keepalive_intv = msecs_to_jiffies(intv);
365
366         /* Link with lowest tolerance determines timer interval */
367         if (keepalive_intv < n->keepalive_intv)
368                 n->keepalive_intv = keepalive_intv;
369
370         /* Ensure link's abort limit corresponds to current interval */
371         tipc_link_set_abort_limit(l, tol / jiffies_to_msecs(n->keepalive_intv));
372 }
373
374 static void tipc_node_delete(struct tipc_node *node)
375 {
376         list_del_rcu(&node->list);
377         hlist_del_rcu(&node->hash);
378         tipc_node_put(node);
379
380         del_timer_sync(&node->timer);
381         tipc_node_put(node);
382 }
383
384 void tipc_node_stop(struct net *net)
385 {
386         struct tipc_net *tn = tipc_net(net);
387         struct tipc_node *node, *t_node;
388
389         spin_lock_bh(&tn->node_list_lock);
390         list_for_each_entry_safe(node, t_node, &tn->node_list, list)
391                 tipc_node_delete(node);
392         spin_unlock_bh(&tn->node_list_lock);
393 }
394
395 void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
396 {
397         struct tipc_node *n;
398
399         if (in_own_node(net, addr))
400                 return;
401
402         n = tipc_node_find(net, addr);
403         if (!n) {
404                 pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
405                 return;
406         }
407         tipc_node_write_lock(n);
408         list_add_tail(subscr, &n->publ_list);
409         tipc_node_write_unlock(n);
410         tipc_node_put(n);
411 }
412
413 void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
414 {
415         struct tipc_node *n;
416
417         if (in_own_node(net, addr))
418                 return;
419
420         n = tipc_node_find(net, addr);
421         if (!n) {
422                 pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
423                 return;
424         }
425         tipc_node_write_lock(n);
426         list_del_init(subscr);
427         tipc_node_write_unlock(n);
428         tipc_node_put(n);
429 }
430
431 int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
432 {
433         struct tipc_node *node;
434         struct tipc_sock_conn *conn;
435         int err = 0;
436
437         if (in_own_node(net, dnode))
438                 return 0;
439
440         node = tipc_node_find(net, dnode);
441         if (!node) {
442                 pr_warn("Connecting sock to node 0x%x failed\n", dnode);
443                 return -EHOSTUNREACH;
444         }
445         conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
446         if (!conn) {
447                 err = -EHOSTUNREACH;
448                 goto exit;
449         }
450         conn->peer_node = dnode;
451         conn->port = port;
452         conn->peer_port = peer_port;
453
454         tipc_node_write_lock(node);
455         list_add_tail(&conn->list, &node->conn_sks);
456         tipc_node_write_unlock(node);
457 exit:
458         tipc_node_put(node);
459         return err;
460 }
461
462 void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
463 {
464         struct tipc_node *node;
465         struct tipc_sock_conn *conn, *safe;
466
467         if (in_own_node(net, dnode))
468                 return;
469
470         node = tipc_node_find(net, dnode);
471         if (!node)
472                 return;
473
474         tipc_node_write_lock(node);
475         list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
476                 if (port != conn->port)
477                         continue;
478                 list_del(&conn->list);
479                 kfree(conn);
480         }
481         tipc_node_write_unlock(node);
482         tipc_node_put(node);
483 }
484
485 /* tipc_node_timeout - handle expiration of node timer
486  */
487 static void tipc_node_timeout(unsigned long data)
488 {
489         struct tipc_node *n = (struct tipc_node *)data;
490         struct tipc_link_entry *le;
491         struct sk_buff_head xmitq;
492         int bearer_id;
493         int rc = 0;
494
495         __skb_queue_head_init(&xmitq);
496
497         for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
498                 tipc_node_read_lock(n);
499                 le = &n->links[bearer_id];
500                 spin_lock_bh(&le->lock);
501                 if (le->link) {
502                         /* Link tolerance may change asynchronously: */
503                         tipc_node_calculate_timer(n, le->link);
504                         rc = tipc_link_timeout(le->link, &xmitq);
505                 }
506                 spin_unlock_bh(&le->lock);
507                 tipc_node_read_unlock(n);
508                 tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
509                 if (rc & TIPC_LINK_DOWN_EVT)
510                         tipc_node_link_down(n, bearer_id, false);
511         }
512         mod_timer(&n->timer, jiffies + n->keepalive_intv);
513 }
514
515 /**
516  * __tipc_node_link_up - handle addition of link
517  * Node lock must be held by caller
518  * Link becomes active (alone or shared) or standby, depending on its priority.
519  */
520 static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
521                                 struct sk_buff_head *xmitq)
522 {
523         int *slot0 = &n->active_links[0];
524         int *slot1 = &n->active_links[1];
525         struct tipc_link *ol = node_active_link(n, 0);
526         struct tipc_link *nl = n->links[bearer_id].link;
527
528         if (!nl)
529                 return;
530
531         tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
532         if (!tipc_link_is_up(nl))
533                 return;
534
535         n->working_links++;
536         n->action_flags |= TIPC_NOTIFY_LINK_UP;
537         n->link_id = tipc_link_id(nl);
538
539         /* Leave room for tunnel header when returning 'mtu' to users: */
540         n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
541
542         tipc_bearer_add_dest(n->net, bearer_id, n->addr);
543         tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
544
545         pr_debug("Established link <%s> on network plane %c\n",
546                  tipc_link_name(nl), tipc_link_plane(nl));
547
548         /* First link? => give it both slots */
549         if (!ol) {
550                 *slot0 = bearer_id;
551                 *slot1 = bearer_id;
552                 tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
553                 n->action_flags |= TIPC_NOTIFY_NODE_UP;
554                 tipc_bcast_add_peer(n->net, nl, xmitq);
555                 return;
556         }
557
558         /* Second link => redistribute slots */
559         if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
560                 pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
561                 *slot0 = bearer_id;
562                 *slot1 = bearer_id;
563                 tipc_link_set_active(nl, true);
564                 tipc_link_set_active(ol, false);
565         } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
566                 tipc_link_set_active(nl, true);
567                 *slot1 = bearer_id;
568         } else {
569                 pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
570         }
571
572         /* Prepare synchronization with first link */
573         tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
574 }
575
576 /**
577  * tipc_node_link_up - handle addition of link
578  *
579  * Link becomes active (alone or shared) or standby, depending on its priority.
580  */
581 static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
582                               struct sk_buff_head *xmitq)
583 {
584         struct tipc_media_addr *maddr;
585
586         tipc_node_write_lock(n);
587         __tipc_node_link_up(n, bearer_id, xmitq);
588         maddr = &n->links[bearer_id].maddr;
589         tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
590         tipc_node_write_unlock(n);
591 }
592
593 /**
594  * __tipc_node_link_down - handle loss of link
595  */
596 static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
597                                   struct sk_buff_head *xmitq,
598                                   struct tipc_media_addr **maddr)
599 {
600         struct tipc_link_entry *le = &n->links[*bearer_id];
601         int *slot0 = &n->active_links[0];
602         int *slot1 = &n->active_links[1];
603         int i, highest = 0, prio;
604         struct tipc_link *l, *_l, *tnl;
605
606         l = n->links[*bearer_id].link;
607         if (!l || tipc_link_is_reset(l))
608                 return;
609
610         n->working_links--;
611         n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
612         n->link_id = tipc_link_id(l);
613
614         tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
615
616         pr_debug("Lost link <%s> on network plane %c\n",
617                  tipc_link_name(l), tipc_link_plane(l));
618
619         /* Select new active link if any available */
620         *slot0 = INVALID_BEARER_ID;
621         *slot1 = INVALID_BEARER_ID;
622         for (i = 0; i < MAX_BEARERS; i++) {
623                 _l = n->links[i].link;
624                 if (!_l || !tipc_link_is_up(_l))
625                         continue;
626                 if (_l == l)
627                         continue;
628                 prio = tipc_link_prio(_l);
629                 if (prio < highest)
630                         continue;
631                 if (prio > highest) {
632                         highest = prio;
633                         *slot0 = i;
634                         *slot1 = i;
635                         continue;
636                 }
637                 *slot1 = i;
638         }
639
640         if (!tipc_node_is_up(n)) {
641                 if (tipc_link_peer_is_down(l))
642                         tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
643                 tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
644                 tipc_link_fsm_evt(l, LINK_RESET_EVT);
645                 tipc_link_reset(l);
646                 tipc_link_build_reset_msg(l, xmitq);
647                 *maddr = &n->links[*bearer_id].maddr;
648                 node_lost_contact(n, &le->inputq);
649                 tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
650                 return;
651         }
652         tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
653
654         /* There is still a working link => initiate failover */
655         *bearer_id = n->active_links[0];
656         tnl = n->links[*bearer_id].link;
657         tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
658         tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
659         n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
660         tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
661         tipc_link_reset(l);
662         tipc_link_fsm_evt(l, LINK_RESET_EVT);
663         tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
664         tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
665         *maddr = &n->links[*bearer_id].maddr;
666 }
667
668 static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
669 {
670         struct tipc_link_entry *le = &n->links[bearer_id];
671         struct tipc_link *l = le->link;
672         struct tipc_media_addr *maddr;
673         struct sk_buff_head xmitq;
674
675         if (!l)
676                 return;
677
678         __skb_queue_head_init(&xmitq);
679
680         tipc_node_write_lock(n);
681         if (!tipc_link_is_establishing(l)) {
682                 __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
683                 if (delete) {
684                         kfree(l);
685                         le->link = NULL;
686                         n->link_cnt--;
687                 }
688         } else {
689                 /* Defuse pending tipc_node_link_up() */
690                 tipc_link_fsm_evt(l, LINK_RESET_EVT);
691         }
692         tipc_node_write_unlock(n);
693         tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
694         tipc_sk_rcv(n->net, &le->inputq);
695 }
696
697 static bool tipc_node_is_up(struct tipc_node *n)
698 {
699         return n->active_links[0] != INVALID_BEARER_ID;
700 }
701
702 void tipc_node_check_dest(struct net *net, u32 onode,
703                           struct tipc_bearer *b,
704                           u16 capabilities, u32 signature,
705                           struct tipc_media_addr *maddr,
706                           bool *respond, bool *dupl_addr)
707 {
708         struct tipc_node *n;
709         struct tipc_link *l;
710         struct tipc_link_entry *le;
711         bool addr_match = false;
712         bool sign_match = false;
713         bool link_up = false;
714         bool accept_addr = false;
715         bool reset = true;
716         char *if_name;
717
718         *dupl_addr = false;
719         *respond = false;
720
721         n = tipc_node_create(net, onode, capabilities);
722         if (!n)
723                 return;
724
725         tipc_node_write_lock(n);
726
727         le = &n->links[b->identity];
728
729         /* Prepare to validate requesting node's signature and media address */
730         l = le->link;
731         link_up = l && tipc_link_is_up(l);
732         addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
733         sign_match = (signature == n->signature);
734
735         /* These three flags give us eight permutations: */
736
737         if (sign_match && addr_match && link_up) {
738                 /* All is fine. Do nothing. */
739                 reset = false;
740         } else if (sign_match && addr_match && !link_up) {
741                 /* Respond. The link will come up in due time */
742                 *respond = true;
743         } else if (sign_match && !addr_match && link_up) {
744                 /* Peer has changed i/f address without rebooting.
745                  * If so, the link will reset soon, and the next
746                  * discovery will be accepted. So we can ignore it.
747                  * It may also be an cloned or malicious peer having
748                  * chosen the same node address and signature as an
749                  * existing one.
750                  * Ignore requests until the link goes down, if ever.
751                  */
752                 *dupl_addr = true;
753         } else if (sign_match && !addr_match && !link_up) {
754                 /* Peer link has changed i/f address without rebooting.
755                  * It may also be a cloned or malicious peer; we can't
756                  * distinguish between the two.
757                  * The signature is correct, so we must accept.
758                  */
759                 accept_addr = true;
760                 *respond = true;
761         } else if (!sign_match && addr_match && link_up) {
762                 /* Peer node rebooted. Two possibilities:
763                  *  - Delayed re-discovery; this link endpoint has already
764                  *    reset and re-established contact with the peer, before
765                  *    receiving a discovery message from that node.
766                  *    (The peer happened to receive one from this node first).
767                  *  - The peer came back so fast that our side has not
768                  *    discovered it yet. Probing from this side will soon
769                  *    reset the link, since there can be no working link
770                  *    endpoint at the peer end, and the link will re-establish.
771                  *  Accept the signature, since it comes from a known peer.
772                  */
773                 n->signature = signature;
774         } else if (!sign_match && addr_match && !link_up) {
775                 /*  The peer node has rebooted.
776                  *  Accept signature, since it is a known peer.
777                  */
778                 n->signature = signature;
779                 *respond = true;
780         } else if (!sign_match && !addr_match && link_up) {
781                 /* Peer rebooted with new address, or a new/duplicate peer.
782                  * Ignore until the link goes down, if ever.
783                  */
784                 *dupl_addr = true;
785         } else if (!sign_match && !addr_match && !link_up) {
786                 /* Peer rebooted with new address, or it is a new peer.
787                  * Accept signature and address.
788                  */
789                 n->signature = signature;
790                 accept_addr = true;
791                 *respond = true;
792         }
793
794         if (!accept_addr)
795                 goto exit;
796
797         /* Now create new link if not already existing */
798         if (!l) {
799                 if (n->link_cnt == 2) {
800                         pr_warn("Cannot establish 3rd link to %x\n", n->addr);
801                         goto exit;
802                 }
803                 if_name = strchr(b->name, ':') + 1;
804                 if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
805                                       b->net_plane, b->mtu, b->priority,
806                                       b->window, mod(tipc_net(net)->random),
807                                       tipc_own_addr(net), onode,
808                                       n->capabilities,
809                                       tipc_bc_sndlink(n->net), n->bc_entry.link,
810                                       &le->inputq,
811                                       &n->bc_entry.namedq, &l)) {
812                         *respond = false;
813                         goto exit;
814                 }
815                 tipc_link_reset(l);
816                 tipc_link_fsm_evt(l, LINK_RESET_EVT);
817                 if (n->state == NODE_FAILINGOVER)
818                         tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
819                 le->link = l;
820                 n->link_cnt++;
821                 tipc_node_calculate_timer(n, l);
822                 if (n->link_cnt == 1)
823                         if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
824                                 tipc_node_get(n);
825         }
826         memcpy(&le->maddr, maddr, sizeof(*maddr));
827 exit:
828         tipc_node_write_unlock(n);
829         if (reset && l && !tipc_link_is_reset(l))
830                 tipc_node_link_down(n, b->identity, false);
831         tipc_node_put(n);
832 }
833
834 void tipc_node_delete_links(struct net *net, int bearer_id)
835 {
836         struct tipc_net *tn = net_generic(net, tipc_net_id);
837         struct tipc_node *n;
838
839         rcu_read_lock();
840         list_for_each_entry_rcu(n, &tn->node_list, list) {
841                 tipc_node_link_down(n, bearer_id, true);
842         }
843         rcu_read_unlock();
844 }
845
846 static void tipc_node_reset_links(struct tipc_node *n)
847 {
848         char addr_string[16];
849         int i;
850
851         pr_warn("Resetting all links to %s\n",
852                 tipc_addr_string_fill(addr_string, n->addr));
853
854         for (i = 0; i < MAX_BEARERS; i++) {
855                 tipc_node_link_down(n, i, false);
856         }
857 }
858
859 /* tipc_node_fsm_evt - node finite state machine
860  * Determines when contact is allowed with peer node
861  */
862 static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
863 {
864         int state = n->state;
865
866         switch (state) {
867         case SELF_DOWN_PEER_DOWN:
868                 switch (evt) {
869                 case SELF_ESTABL_CONTACT_EVT:
870                         state = SELF_UP_PEER_COMING;
871                         break;
872                 case PEER_ESTABL_CONTACT_EVT:
873                         state = SELF_COMING_PEER_UP;
874                         break;
875                 case SELF_LOST_CONTACT_EVT:
876                 case PEER_LOST_CONTACT_EVT:
877                         break;
878                 case NODE_SYNCH_END_EVT:
879                 case NODE_SYNCH_BEGIN_EVT:
880                 case NODE_FAILOVER_BEGIN_EVT:
881                 case NODE_FAILOVER_END_EVT:
882                 default:
883                         goto illegal_evt;
884                 }
885                 break;
886         case SELF_UP_PEER_UP:
887                 switch (evt) {
888                 case SELF_LOST_CONTACT_EVT:
889                         state = SELF_DOWN_PEER_LEAVING;
890                         break;
891                 case PEER_LOST_CONTACT_EVT:
892                         state = SELF_LEAVING_PEER_DOWN;
893                         break;
894                 case NODE_SYNCH_BEGIN_EVT:
895                         state = NODE_SYNCHING;
896                         break;
897                 case NODE_FAILOVER_BEGIN_EVT:
898                         state = NODE_FAILINGOVER;
899                         break;
900                 case SELF_ESTABL_CONTACT_EVT:
901                 case PEER_ESTABL_CONTACT_EVT:
902                 case NODE_SYNCH_END_EVT:
903                 case NODE_FAILOVER_END_EVT:
904                         break;
905                 default:
906                         goto illegal_evt;
907                 }
908                 break;
909         case SELF_DOWN_PEER_LEAVING:
910                 switch (evt) {
911                 case PEER_LOST_CONTACT_EVT:
912                         state = SELF_DOWN_PEER_DOWN;
913                         break;
914                 case SELF_ESTABL_CONTACT_EVT:
915                 case PEER_ESTABL_CONTACT_EVT:
916                 case SELF_LOST_CONTACT_EVT:
917                         break;
918                 case NODE_SYNCH_END_EVT:
919                 case NODE_SYNCH_BEGIN_EVT:
920                 case NODE_FAILOVER_BEGIN_EVT:
921                 case NODE_FAILOVER_END_EVT:
922                 default:
923                         goto illegal_evt;
924                 }
925                 break;
926         case SELF_UP_PEER_COMING:
927                 switch (evt) {
928                 case PEER_ESTABL_CONTACT_EVT:
929                         state = SELF_UP_PEER_UP;
930                         break;
931                 case SELF_LOST_CONTACT_EVT:
932                         state = SELF_DOWN_PEER_LEAVING;
933                         break;
934                 case SELF_ESTABL_CONTACT_EVT:
935                 case PEER_LOST_CONTACT_EVT:
936                 case NODE_SYNCH_END_EVT:
937                 case NODE_FAILOVER_BEGIN_EVT:
938                         break;
939                 case NODE_SYNCH_BEGIN_EVT:
940                 case NODE_FAILOVER_END_EVT:
941                 default:
942                         goto illegal_evt;
943                 }
944                 break;
945         case SELF_COMING_PEER_UP:
946                 switch (evt) {
947                 case SELF_ESTABL_CONTACT_EVT:
948                         state = SELF_UP_PEER_UP;
949                         break;
950                 case PEER_LOST_CONTACT_EVT:
951                         state = SELF_LEAVING_PEER_DOWN;
952                         break;
953                 case SELF_LOST_CONTACT_EVT:
954                 case PEER_ESTABL_CONTACT_EVT:
955                         break;
956                 case NODE_SYNCH_END_EVT:
957                 case NODE_SYNCH_BEGIN_EVT:
958                 case NODE_FAILOVER_BEGIN_EVT:
959                 case NODE_FAILOVER_END_EVT:
960                 default:
961                         goto illegal_evt;
962                 }
963                 break;
964         case SELF_LEAVING_PEER_DOWN:
965                 switch (evt) {
966                 case SELF_LOST_CONTACT_EVT:
967                         state = SELF_DOWN_PEER_DOWN;
968                         break;
969                 case SELF_ESTABL_CONTACT_EVT:
970                 case PEER_ESTABL_CONTACT_EVT:
971                 case PEER_LOST_CONTACT_EVT:
972                         break;
973                 case NODE_SYNCH_END_EVT:
974                 case NODE_SYNCH_BEGIN_EVT:
975                 case NODE_FAILOVER_BEGIN_EVT:
976                 case NODE_FAILOVER_END_EVT:
977                 default:
978                         goto illegal_evt;
979                 }
980                 break;
981         case NODE_FAILINGOVER:
982                 switch (evt) {
983                 case SELF_LOST_CONTACT_EVT:
984                         state = SELF_DOWN_PEER_LEAVING;
985                         break;
986                 case PEER_LOST_CONTACT_EVT:
987                         state = SELF_LEAVING_PEER_DOWN;
988                         break;
989                 case NODE_FAILOVER_END_EVT:
990                         state = SELF_UP_PEER_UP;
991                         break;
992                 case NODE_FAILOVER_BEGIN_EVT:
993                 case SELF_ESTABL_CONTACT_EVT:
994                 case PEER_ESTABL_CONTACT_EVT:
995                         break;
996                 case NODE_SYNCH_BEGIN_EVT:
997                 case NODE_SYNCH_END_EVT:
998                 default:
999                         goto illegal_evt;
1000                 }
1001                 break;
1002         case NODE_SYNCHING:
1003                 switch (evt) {
1004                 case SELF_LOST_CONTACT_EVT:
1005                         state = SELF_DOWN_PEER_LEAVING;
1006                         break;
1007                 case PEER_LOST_CONTACT_EVT:
1008                         state = SELF_LEAVING_PEER_DOWN;
1009                         break;
1010                 case NODE_SYNCH_END_EVT:
1011                         state = SELF_UP_PEER_UP;
1012                         break;
1013                 case NODE_FAILOVER_BEGIN_EVT:
1014                         state = NODE_FAILINGOVER;
1015                         break;
1016                 case NODE_SYNCH_BEGIN_EVT:
1017                 case SELF_ESTABL_CONTACT_EVT:
1018                 case PEER_ESTABL_CONTACT_EVT:
1019                         break;
1020                 case NODE_FAILOVER_END_EVT:
1021                 default:
1022                         goto illegal_evt;
1023                 }
1024                 break;
1025         default:
1026                 pr_err("Unknown node fsm state %x\n", state);
1027                 break;
1028         }
1029         n->state = state;
1030         return;
1031
1032 illegal_evt:
1033         pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
1034 }
1035
1036 static void node_lost_contact(struct tipc_node *n,
1037                               struct sk_buff_head *inputq)
1038 {
1039         char addr_string[16];
1040         struct tipc_sock_conn *conn, *safe;
1041         struct tipc_link *l;
1042         struct list_head *conns = &n->conn_sks;
1043         struct sk_buff *skb;
1044         uint i;
1045
1046         pr_debug("Lost contact with %s\n",
1047                  tipc_addr_string_fill(addr_string, n->addr));
1048
1049         /* Clean up broadcast state */
1050         tipc_bcast_remove_peer(n->net, n->bc_entry.link);
1051
1052         /* Abort any ongoing link failover */
1053         for (i = 0; i < MAX_BEARERS; i++) {
1054                 l = n->links[i].link;
1055                 if (l)
1056                         tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
1057         }
1058
1059         /* Notify publications from this node */
1060         n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
1061
1062         /* Notify sockets connected to node */
1063         list_for_each_entry_safe(conn, safe, conns, list) {
1064                 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1065                                       SHORT_H_SIZE, 0, tipc_own_addr(n->net),
1066                                       conn->peer_node, conn->port,
1067                                       conn->peer_port, TIPC_ERR_NO_NODE);
1068                 if (likely(skb))
1069                         skb_queue_tail(inputq, skb);
1070                 list_del(&conn->list);
1071                 kfree(conn);
1072         }
1073 }
1074
1075 /**
1076  * tipc_node_get_linkname - get the name of a link
1077  *
1078  * @bearer_id: id of the bearer
1079  * @node: peer node address
1080  * @linkname: link name output buffer
1081  *
1082  * Returns 0 on success
1083  */
1084 int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
1085                            char *linkname, size_t len)
1086 {
1087         struct tipc_link *link;
1088         int err = -EINVAL;
1089         struct tipc_node *node = tipc_node_find(net, addr);
1090
1091         if (!node)
1092                 return err;
1093
1094         if (bearer_id >= MAX_BEARERS)
1095                 goto exit;
1096
1097         tipc_node_read_lock(node);
1098         link = node->links[bearer_id].link;
1099         if (link) {
1100                 strncpy(linkname, tipc_link_name(link), len);
1101                 err = 0;
1102         }
1103 exit:
1104         tipc_node_read_unlock(node);
1105         tipc_node_put(node);
1106         return err;
1107 }
1108
1109 /* Caller should hold node lock for the passed node */
1110 static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
1111 {
1112         void *hdr;
1113         struct nlattr *attrs;
1114
1115         hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1116                           NLM_F_MULTI, TIPC_NL_NODE_GET);
1117         if (!hdr)
1118                 return -EMSGSIZE;
1119
1120         attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
1121         if (!attrs)
1122                 goto msg_full;
1123
1124         if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
1125                 goto attr_msg_full;
1126         if (tipc_node_is_up(node))
1127                 if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
1128                         goto attr_msg_full;
1129
1130         nla_nest_end(msg->skb, attrs);
1131         genlmsg_end(msg->skb, hdr);
1132
1133         return 0;
1134
1135 attr_msg_full:
1136         nla_nest_cancel(msg->skb, attrs);
1137 msg_full:
1138         genlmsg_cancel(msg->skb, hdr);
1139
1140         return -EMSGSIZE;
1141 }
1142
1143 /**
1144  * tipc_node_xmit() is the general link level function for message sending
1145  * @net: the applicable net namespace
1146  * @list: chain of buffers containing message
1147  * @dnode: address of destination node
1148  * @selector: a number used for deterministic link selection
1149  * Consumes the buffer chain, except when returning -ELINKCONG
1150  * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
1151  */
1152 int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
1153                    u32 dnode, int selector)
1154 {
1155         struct tipc_link_entry *le = NULL;
1156         struct tipc_node *n;
1157         struct sk_buff_head xmitq;
1158         int bearer_id;
1159         int rc;
1160
1161         if (in_own_node(net, dnode)) {
1162                 tipc_sk_rcv(net, list);
1163                 return 0;
1164         }
1165
1166         n = tipc_node_find(net, dnode);
1167         if (unlikely(!n)) {
1168                 skb_queue_purge(list);
1169                 return -EHOSTUNREACH;
1170         }
1171
1172         tipc_node_read_lock(n);
1173         bearer_id = n->active_links[selector & 1];
1174         if (unlikely(bearer_id == INVALID_BEARER_ID)) {
1175                 tipc_node_read_unlock(n);
1176                 tipc_node_put(n);
1177                 skb_queue_purge(list);
1178                 return -EHOSTUNREACH;
1179         }
1180
1181         __skb_queue_head_init(&xmitq);
1182         le = &n->links[bearer_id];
1183         spin_lock_bh(&le->lock);
1184         rc = tipc_link_xmit(le->link, list, &xmitq);
1185         spin_unlock_bh(&le->lock);
1186         tipc_node_read_unlock(n);
1187
1188         if (likely(rc == 0))
1189                 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1190         else if (rc == -ENOBUFS)
1191                 tipc_node_link_down(n, bearer_id, false);
1192
1193         tipc_node_put(n);
1194
1195         return rc;
1196 }
1197
1198 /* tipc_node_xmit_skb(): send single buffer to destination
1199  * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
1200  * messages, which will not be rejected
1201  * The only exception is datagram messages rerouted after secondary
1202  * lookup, which are rare and safe to dispose of anyway.
1203  * TODO: Return real return value, and let callers use
1204  * tipc_wait_for_sendpkt() where applicable
1205  */
1206 int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
1207                        u32 selector)
1208 {
1209         struct sk_buff_head head;
1210         int rc;
1211
1212         skb_queue_head_init(&head);
1213         __skb_queue_tail(&head, skb);
1214         rc = tipc_node_xmit(net, &head, dnode, selector);
1215         if (rc == -ELINKCONG)
1216                 kfree_skb(skb);
1217         return 0;
1218 }
1219
1220 void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
1221 {
1222         struct sk_buff *txskb;
1223         struct tipc_node *n;
1224         u32 dst;
1225
1226         rcu_read_lock();
1227         list_for_each_entry_rcu(n, tipc_nodes(net), list) {
1228                 dst = n->addr;
1229                 if (in_own_node(net, dst))
1230                         continue;
1231                 if (!tipc_node_is_up(n))
1232                         continue;
1233                 txskb = pskb_copy(skb, GFP_ATOMIC);
1234                 if (!txskb)
1235                         break;
1236                 msg_set_destnode(buf_msg(txskb), dst);
1237                 tipc_node_xmit_skb(net, txskb, dst, 0);
1238         }
1239         rcu_read_unlock();
1240
1241         kfree_skb(skb);
1242 }
1243
1244 /**
1245  * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
1246  * @net: the applicable net namespace
1247  * @skb: TIPC packet
1248  * @bearer_id: id of bearer message arrived on
1249  *
1250  * Invoked with no locks held.
1251  */
1252 static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
1253 {
1254         int rc;
1255         struct sk_buff_head xmitq;
1256         struct tipc_bclink_entry *be;
1257         struct tipc_link_entry *le;
1258         struct tipc_msg *hdr = buf_msg(skb);
1259         int usr = msg_user(hdr);
1260         u32 dnode = msg_destnode(hdr);
1261         struct tipc_node *n;
1262
1263         __skb_queue_head_init(&xmitq);
1264
1265         /* If NACK for other node, let rcv link for that node peek into it */
1266         if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
1267                 n = tipc_node_find(net, dnode);
1268         else
1269                 n = tipc_node_find(net, msg_prevnode(hdr));
1270         if (!n) {
1271                 kfree_skb(skb);
1272                 return;
1273         }
1274         be = &n->bc_entry;
1275         le = &n->links[bearer_id];
1276
1277         rc = tipc_bcast_rcv(net, be->link, skb);
1278
1279         /* Broadcast link reset may happen at reassembly failure */
1280         if (rc & TIPC_LINK_DOWN_EVT)
1281                 tipc_node_reset_links(n);
1282
1283         /* Broadcast ACKs are sent on a unicast link */
1284         if (rc & TIPC_LINK_SND_BC_ACK) {
1285                 tipc_node_read_lock(n);
1286                 tipc_link_build_ack_msg(le->link, &xmitq);
1287                 tipc_node_read_unlock(n);
1288         }
1289
1290         if (!skb_queue_empty(&xmitq))
1291                 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1292
1293         /* Deliver. 'arrvq' is under inputq2's lock protection */
1294         if (!skb_queue_empty(&be->inputq1)) {
1295                 spin_lock_bh(&be->inputq2.lock);
1296                 spin_lock_bh(&be->inputq1.lock);
1297                 skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
1298                 spin_unlock_bh(&be->inputq1.lock);
1299                 spin_unlock_bh(&be->inputq2.lock);
1300                 tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
1301         }
1302         tipc_node_put(n);
1303 }
1304
1305 /**
1306  * tipc_node_check_state - check and if necessary update node state
1307  * @skb: TIPC packet
1308  * @bearer_id: identity of bearer delivering the packet
1309  * Returns true if state is ok, otherwise consumes buffer and returns false
1310  */
1311 static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
1312                                   int bearer_id, struct sk_buff_head *xmitq)
1313 {
1314         struct tipc_msg *hdr = buf_msg(skb);
1315         int usr = msg_user(hdr);
1316         int mtyp = msg_type(hdr);
1317         u16 oseqno = msg_seqno(hdr);
1318         u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
1319         u16 exp_pkts = msg_msgcnt(hdr);
1320         u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
1321         int state = n->state;
1322         struct tipc_link *l, *tnl, *pl = NULL;
1323         struct tipc_media_addr *maddr;
1324         int pb_id;
1325
1326         l = n->links[bearer_id].link;
1327         if (!l)
1328                 return false;
1329         rcv_nxt = tipc_link_rcv_nxt(l);
1330
1331
1332         if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
1333                 return true;
1334
1335         /* Find parallel link, if any */
1336         for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
1337                 if ((pb_id != bearer_id) && n->links[pb_id].link) {
1338                         pl = n->links[pb_id].link;
1339                         break;
1340                 }
1341         }
1342
1343         /* Check and update node accesibility if applicable */
1344         if (state == SELF_UP_PEER_COMING) {
1345                 if (!tipc_link_is_up(l))
1346                         return true;
1347                 if (!msg_peer_link_is_up(hdr))
1348                         return true;
1349                 tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
1350         }
1351
1352         if (state == SELF_DOWN_PEER_LEAVING) {
1353                 if (msg_peer_node_is_up(hdr))
1354                         return false;
1355                 tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
1356                 return true;
1357         }
1358
1359         if (state == SELF_LEAVING_PEER_DOWN)
1360                 return false;
1361
1362         /* Ignore duplicate packets */
1363         if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
1364                 return true;
1365
1366         /* Initiate or update failover mode if applicable */
1367         if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
1368                 syncpt = oseqno + exp_pkts - 1;
1369                 if (pl && tipc_link_is_up(pl)) {
1370                         __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
1371                         tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
1372                                                         tipc_link_inputq(l));
1373                 }
1374                 /* If pkts arrive out of order, use lowest calculated syncpt */
1375                 if (less(syncpt, n->sync_point))
1376                         n->sync_point = syncpt;
1377         }
1378
1379         /* Open parallel link when tunnel link reaches synch point */
1380         if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
1381                 if (!more(rcv_nxt, n->sync_point))
1382                         return true;
1383                 tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
1384                 if (pl)
1385                         tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
1386                 return true;
1387         }
1388
1389         /* No synching needed if only one link */
1390         if (!pl || !tipc_link_is_up(pl))
1391                 return true;
1392
1393         /* Initiate synch mode if applicable */
1394         if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
1395                 syncpt = iseqno + exp_pkts - 1;
1396                 if (!tipc_link_is_up(l)) {
1397                         tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
1398                         __tipc_node_link_up(n, bearer_id, xmitq);
1399                 }
1400                 if (n->state == SELF_UP_PEER_UP) {
1401                         n->sync_point = syncpt;
1402                         tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
1403                         tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
1404                 }
1405         }
1406
1407         /* Open tunnel link when parallel link reaches synch point */
1408         if (n->state == NODE_SYNCHING) {
1409                 if (tipc_link_is_synching(l)) {
1410                         tnl = l;
1411                 } else {
1412                         tnl = pl;
1413                         pl = l;
1414                 }
1415                 inputq_len = skb_queue_len(tipc_link_inputq(pl));
1416                 dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
1417                 if (more(dlv_nxt, n->sync_point)) {
1418                         tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
1419                         tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
1420                         return true;
1421                 }
1422                 if (l == pl)
1423                         return true;
1424                 if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
1425                         return true;
1426                 if (usr == LINK_PROTOCOL)
1427                         return true;
1428                 return false;
1429         }
1430         return true;
1431 }
1432
1433 /**
1434  * tipc_rcv - process TIPC packets/messages arriving from off-node
1435  * @net: the applicable net namespace
1436  * @skb: TIPC packet
1437  * @bearer: pointer to bearer message arrived on
1438  *
1439  * Invoked with no locks held. Bearer pointer must point to a valid bearer
1440  * structure (i.e. cannot be NULL), but bearer can be inactive.
1441  */
1442 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
1443 {
1444         struct sk_buff_head xmitq;
1445         struct tipc_node *n;
1446         struct tipc_msg *hdr = buf_msg(skb);
1447         int usr = msg_user(hdr);
1448         int bearer_id = b->identity;
1449         struct tipc_link_entry *le;
1450         u16 bc_ack = msg_bcast_ack(hdr);
1451         int rc = 0;
1452
1453         __skb_queue_head_init(&xmitq);
1454
1455         /* Ensure message is well-formed */
1456         if (unlikely(!tipc_msg_validate(skb)))
1457                 goto discard;
1458
1459         /* Handle arrival of discovery or broadcast packet */
1460         if (unlikely(msg_non_seq(hdr))) {
1461                 if (unlikely(usr == LINK_CONFIG))
1462                         return tipc_disc_rcv(net, skb, b);
1463                 else
1464                         return tipc_node_bc_rcv(net, skb, bearer_id);
1465         }
1466
1467         /* Locate neighboring node that sent packet */
1468         n = tipc_node_find(net, msg_prevnode(hdr));
1469         if (unlikely(!n))
1470                 goto discard;
1471         le = &n->links[bearer_id];
1472
1473         /* Ensure broadcast reception is in synch with peer's send state */
1474         if (unlikely(usr == LINK_PROTOCOL))
1475                 tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
1476         else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
1477                 tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
1478
1479         /* Receive packet directly if conditions permit */
1480         tipc_node_read_lock(n);
1481         if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
1482                 spin_lock_bh(&le->lock);
1483                 if (le->link) {
1484                         rc = tipc_link_rcv(le->link, skb, &xmitq);
1485                         skb = NULL;
1486                 }
1487                 spin_unlock_bh(&le->lock);
1488         }
1489         tipc_node_read_unlock(n);
1490
1491         /* Check/update node state before receiving */
1492         if (unlikely(skb)) {
1493                 tipc_node_write_lock(n);
1494                 if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
1495                         if (le->link) {
1496                                 rc = tipc_link_rcv(le->link, skb, &xmitq);
1497                                 skb = NULL;
1498                         }
1499                 }
1500                 tipc_node_write_unlock(n);
1501         }
1502
1503         if (unlikely(rc & TIPC_LINK_UP_EVT))
1504                 tipc_node_link_up(n, bearer_id, &xmitq);
1505
1506         if (unlikely(rc & TIPC_LINK_DOWN_EVT))
1507                 tipc_node_link_down(n, bearer_id, false);
1508
1509         if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
1510                 tipc_named_rcv(net, &n->bc_entry.namedq);
1511
1512         if (!skb_queue_empty(&le->inputq))
1513                 tipc_sk_rcv(net, &le->inputq);
1514
1515         if (!skb_queue_empty(&xmitq))
1516                 tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
1517
1518         tipc_node_put(n);
1519 discard:
1520         kfree_skb(skb);
1521 }
1522
1523 int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
1524 {
1525         int err;
1526         struct net *net = sock_net(skb->sk);
1527         struct tipc_net *tn = net_generic(net, tipc_net_id);
1528         int done = cb->args[0];
1529         int last_addr = cb->args[1];
1530         struct tipc_node *node;
1531         struct tipc_nl_msg msg;
1532
1533         if (done)
1534                 return 0;
1535
1536         msg.skb = skb;
1537         msg.portid = NETLINK_CB(cb->skb).portid;
1538         msg.seq = cb->nlh->nlmsg_seq;
1539
1540         rcu_read_lock();
1541         if (last_addr) {
1542                 node = tipc_node_find(net, last_addr);
1543                 if (!node) {
1544                         rcu_read_unlock();
1545                         /* We never set seq or call nl_dump_check_consistent()
1546                          * this means that setting prev_seq here will cause the
1547                          * consistence check to fail in the netlink callback
1548                          * handler. Resulting in the NLMSG_DONE message having
1549                          * the NLM_F_DUMP_INTR flag set if the node state
1550                          * changed while we released the lock.
1551                          */
1552                         cb->prev_seq = 1;
1553                         return -EPIPE;
1554                 }
1555                 tipc_node_put(node);
1556         }
1557
1558         list_for_each_entry_rcu(node, &tn->node_list, list) {
1559                 if (last_addr) {
1560                         if (node->addr == last_addr)
1561                                 last_addr = 0;
1562                         else
1563                                 continue;
1564                 }
1565
1566                 tipc_node_read_lock(node);
1567                 err = __tipc_nl_add_node(&msg, node);
1568                 if (err) {
1569                         last_addr = node->addr;
1570                         tipc_node_read_unlock(node);
1571                         goto out;
1572                 }
1573
1574                 tipc_node_read_unlock(node);
1575         }
1576         done = 1;
1577 out:
1578         cb->args[0] = done;
1579         cb->args[1] = last_addr;
1580         rcu_read_unlock();
1581
1582         return skb->len;
1583 }
1584
1585 /* tipc_node_find_by_name - locate owner node of link by link's name
1586  * @net: the applicable net namespace
1587  * @name: pointer to link name string
1588  * @bearer_id: pointer to index in 'node->links' array where the link was found.
1589  *
1590  * Returns pointer to node owning the link, or 0 if no matching link is found.
1591  */
1592 static struct tipc_node *tipc_node_find_by_name(struct net *net,
1593                                                 const char *link_name,
1594                                                 unsigned int *bearer_id)
1595 {
1596         struct tipc_net *tn = net_generic(net, tipc_net_id);
1597         struct tipc_link *l;
1598         struct tipc_node *n;
1599         struct tipc_node *found_node = NULL;
1600         int i;
1601
1602         *bearer_id = 0;
1603         rcu_read_lock();
1604         list_for_each_entry_rcu(n, &tn->node_list, list) {
1605                 tipc_node_read_lock(n);
1606                 for (i = 0; i < MAX_BEARERS; i++) {
1607                         l = n->links[i].link;
1608                         if (l && !strcmp(tipc_link_name(l), link_name)) {
1609                                 *bearer_id = i;
1610                                 found_node = n;
1611                                 break;
1612                         }
1613                 }
1614                 tipc_node_read_unlock(n);
1615                 if (found_node)
1616                         break;
1617         }
1618         rcu_read_unlock();
1619
1620         return found_node;
1621 }
1622
1623 int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
1624 {
1625         int err;
1626         int res = 0;
1627         int bearer_id;
1628         char *name;
1629         struct tipc_link *link;
1630         struct tipc_node *node;
1631         struct sk_buff_head xmitq;
1632         struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1633         struct net *net = sock_net(skb->sk);
1634
1635         __skb_queue_head_init(&xmitq);
1636
1637         if (!info->attrs[TIPC_NLA_LINK])
1638                 return -EINVAL;
1639
1640         err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1641                                info->attrs[TIPC_NLA_LINK],
1642                                tipc_nl_link_policy);
1643         if (err)
1644                 return err;
1645
1646         if (!attrs[TIPC_NLA_LINK_NAME])
1647                 return -EINVAL;
1648
1649         name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1650
1651         if (strcmp(name, tipc_bclink_name) == 0)
1652                 return tipc_nl_bc_link_set(net, attrs);
1653
1654         node = tipc_node_find_by_name(net, name, &bearer_id);
1655         if (!node)
1656                 return -EINVAL;
1657
1658         tipc_node_read_lock(node);
1659
1660         link = node->links[bearer_id].link;
1661         if (!link) {
1662                 res = -EINVAL;
1663                 goto out;
1664         }
1665
1666         if (attrs[TIPC_NLA_LINK_PROP]) {
1667                 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1668
1669                 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1670                                               props);
1671                 if (err) {
1672                         res = err;
1673                         goto out;
1674                 }
1675
1676                 if (props[TIPC_NLA_PROP_TOL]) {
1677                         u32 tol;
1678
1679                         tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1680                         tipc_link_set_tolerance(link, tol, &xmitq);
1681                 }
1682                 if (props[TIPC_NLA_PROP_PRIO]) {
1683                         u32 prio;
1684
1685                         prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1686                         tipc_link_set_prio(link, prio, &xmitq);
1687                 }
1688                 if (props[TIPC_NLA_PROP_WIN]) {
1689                         u32 win;
1690
1691                         win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1692                         tipc_link_set_queue_limits(link, win);
1693                 }
1694         }
1695
1696 out:
1697         tipc_node_read_unlock(node);
1698         tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
1699         return res;
1700 }
1701
1702 int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
1703 {
1704         struct net *net = genl_info_net(info);
1705         struct tipc_nl_msg msg;
1706         char *name;
1707         int err;
1708
1709         msg.portid = info->snd_portid;
1710         msg.seq = info->snd_seq;
1711
1712         if (!info->attrs[TIPC_NLA_LINK_NAME])
1713                 return -EINVAL;
1714         name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
1715
1716         msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1717         if (!msg.skb)
1718                 return -ENOMEM;
1719
1720         if (strcmp(name, tipc_bclink_name) == 0) {
1721                 err = tipc_nl_add_bc_link(net, &msg);
1722                 if (err) {
1723                         nlmsg_free(msg.skb);
1724                         return err;
1725                 }
1726         } else {
1727                 int bearer_id;
1728                 struct tipc_node *node;
1729                 struct tipc_link *link;
1730
1731                 node = tipc_node_find_by_name(net, name, &bearer_id);
1732                 if (!node)
1733                         return -EINVAL;
1734
1735                 tipc_node_read_lock(node);
1736                 link = node->links[bearer_id].link;
1737                 if (!link) {
1738                         tipc_node_read_unlock(node);
1739                         nlmsg_free(msg.skb);
1740                         return -EINVAL;
1741                 }
1742
1743                 err = __tipc_nl_add_link(net, &msg, link, 0);
1744                 tipc_node_read_unlock(node);
1745                 if (err) {
1746                         nlmsg_free(msg.skb);
1747                         return err;
1748                 }
1749         }
1750
1751         return genlmsg_reply(msg.skb, info);
1752 }
1753
1754 int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
1755 {
1756         int err;
1757         char *link_name;
1758         unsigned int bearer_id;
1759         struct tipc_link *link;
1760         struct tipc_node *node;
1761         struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1762         struct net *net = sock_net(skb->sk);
1763         struct tipc_link_entry *le;
1764
1765         if (!info->attrs[TIPC_NLA_LINK])
1766                 return -EINVAL;
1767
1768         err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1769                                info->attrs[TIPC_NLA_LINK],
1770                                tipc_nl_link_policy);
1771         if (err)
1772                 return err;
1773
1774         if (!attrs[TIPC_NLA_LINK_NAME])
1775                 return -EINVAL;
1776
1777         link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1778
1779         if (strcmp(link_name, tipc_bclink_name) == 0) {
1780                 err = tipc_bclink_reset_stats(net);
1781                 if (err)
1782                         return err;
1783                 return 0;
1784         }
1785
1786         node = tipc_node_find_by_name(net, link_name, &bearer_id);
1787         if (!node)
1788                 return -EINVAL;
1789
1790         le = &node->links[bearer_id];
1791         tipc_node_read_lock(node);
1792         spin_lock_bh(&le->lock);
1793         link = node->links[bearer_id].link;
1794         if (!link) {
1795                 spin_unlock_bh(&le->lock);
1796                 tipc_node_read_unlock(node);
1797                 return -EINVAL;
1798         }
1799         tipc_link_reset_stats(link);
1800         spin_unlock_bh(&le->lock);
1801         tipc_node_read_unlock(node);
1802         return 0;
1803 }
1804
1805 /* Caller should hold node lock  */
1806 static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
1807                                     struct tipc_node *node, u32 *prev_link)
1808 {
1809         u32 i;
1810         int err;
1811
1812         for (i = *prev_link; i < MAX_BEARERS; i++) {
1813                 *prev_link = i;
1814
1815                 if (!node->links[i].link)
1816                         continue;
1817
1818                 err = __tipc_nl_add_link(net, msg,
1819                                          node->links[i].link, NLM_F_MULTI);
1820                 if (err)
1821                         return err;
1822         }
1823         *prev_link = 0;
1824
1825         return 0;
1826 }
1827
1828 int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
1829 {
1830         struct net *net = sock_net(skb->sk);
1831         struct tipc_net *tn = net_generic(net, tipc_net_id);
1832         struct tipc_node *node;
1833         struct tipc_nl_msg msg;
1834         u32 prev_node = cb->args[0];
1835         u32 prev_link = cb->args[1];
1836         int done = cb->args[2];
1837         int err;
1838
1839         if (done)
1840                 return 0;
1841
1842         msg.skb = skb;
1843         msg.portid = NETLINK_CB(cb->skb).portid;
1844         msg.seq = cb->nlh->nlmsg_seq;
1845
1846         rcu_read_lock();
1847         if (prev_node) {
1848                 node = tipc_node_find(net, prev_node);
1849                 if (!node) {
1850                         /* We never set seq or call nl_dump_check_consistent()
1851                          * this means that setting prev_seq here will cause the
1852                          * consistence check to fail in the netlink callback
1853                          * handler. Resulting in the last NLMSG_DONE message
1854                          * having the NLM_F_DUMP_INTR flag set.
1855                          */
1856                         cb->prev_seq = 1;
1857                         goto out;
1858                 }
1859                 tipc_node_put(node);
1860
1861                 list_for_each_entry_continue_rcu(node, &tn->node_list,
1862                                                  list) {
1863                         tipc_node_read_lock(node);
1864                         err = __tipc_nl_add_node_links(net, &msg, node,
1865                                                        &prev_link);
1866                         tipc_node_read_unlock(node);
1867                         if (err)
1868                                 goto out;
1869
1870                         prev_node = node->addr;
1871                 }
1872         } else {
1873                 err = tipc_nl_add_bc_link(net, &msg);
1874                 if (err)
1875                         goto out;
1876
1877                 list_for_each_entry_rcu(node, &tn->node_list, list) {
1878                         tipc_node_read_lock(node);
1879                         err = __tipc_nl_add_node_links(net, &msg, node,
1880                                                        &prev_link);
1881                         tipc_node_read_unlock(node);
1882                         if (err)
1883                                 goto out;
1884
1885                         prev_node = node->addr;
1886                 }
1887         }
1888         done = 1;
1889 out:
1890         rcu_read_unlock();
1891
1892         cb->args[0] = prev_node;
1893         cb->args[1] = prev_link;
1894         cb->args[2] = done;
1895
1896         return skb->len;
1897 }