b1e17953eeea0838ef33209cfc7aa8105c1689a4
[firefly-linux-kernel-4.4.55.git] / net / tipc / link.c
1 /*
2  * net/tipc/link.c: TIPC link code
3  *
4  * Copyright (c) 1996-2007, 2012-2015, Ericsson AB
5  * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the names of the copyright holders nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include "core.h"
38 #include "subscr.h"
39 #include "link.h"
40 #include "bcast.h"
41 #include "socket.h"
42 #include "name_distr.h"
43 #include "discover.h"
44 #include "netlink.h"
45
46 #include <linux/pkt_sched.h>
47
48 /*
49  * Error message prefixes
50  */
51 static const char *link_co_err = "Link changeover error, ";
52 static const char *link_rst_msg = "Resetting link ";
53 static const char *link_unk_evt = "Unknown link event ";
54
55 static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
56         [TIPC_NLA_LINK_UNSPEC]          = { .type = NLA_UNSPEC },
57         [TIPC_NLA_LINK_NAME] = {
58                 .type = NLA_STRING,
59                 .len = TIPC_MAX_LINK_NAME
60         },
61         [TIPC_NLA_LINK_MTU]             = { .type = NLA_U32 },
62         [TIPC_NLA_LINK_BROADCAST]       = { .type = NLA_FLAG },
63         [TIPC_NLA_LINK_UP]              = { .type = NLA_FLAG },
64         [TIPC_NLA_LINK_ACTIVE]          = { .type = NLA_FLAG },
65         [TIPC_NLA_LINK_PROP]            = { .type = NLA_NESTED },
66         [TIPC_NLA_LINK_STATS]           = { .type = NLA_NESTED },
67         [TIPC_NLA_LINK_RX]              = { .type = NLA_U32 },
68         [TIPC_NLA_LINK_TX]              = { .type = NLA_U32 }
69 };
70
71 /* Properties valid for media, bearar and link */
72 static const struct nla_policy tipc_nl_prop_policy[TIPC_NLA_PROP_MAX + 1] = {
73         [TIPC_NLA_PROP_UNSPEC]          = { .type = NLA_UNSPEC },
74         [TIPC_NLA_PROP_PRIO]            = { .type = NLA_U32 },
75         [TIPC_NLA_PROP_TOL]             = { .type = NLA_U32 },
76         [TIPC_NLA_PROP_WIN]             = { .type = NLA_U32 }
77 };
78
79 /*
80  * Out-of-range value for link session numbers
81  */
82 #define INVALID_SESSION 0x10000
83
84 /*
85  * Link state events:
86  */
87 #define  STARTING_EVT    856384768      /* link processing trigger */
88 #define  TRAFFIC_MSG_EVT 560815u        /* rx'd ??? */
89 #define  TIMEOUT_EVT     560817u        /* link timer expired */
90
91 /*
92  * State value stored in 'failover_pkts'
93  */
94 #define FIRST_FAILOVER 0xffffu
95
96 static void link_handle_out_of_seq_msg(struct tipc_link *link,
97                                        struct sk_buff *skb);
98 static void tipc_link_proto_rcv(struct tipc_link *link,
99                                 struct sk_buff *skb);
100 static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol);
101 static void link_state_event(struct tipc_link *l_ptr, u32 event);
102 static void link_reset_statistics(struct tipc_link *l_ptr);
103 static void link_print(struct tipc_link *l_ptr, const char *str);
104 static void tipc_link_sync_xmit(struct tipc_link *l);
105 static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
106 static void tipc_link_input(struct tipc_link *l, struct sk_buff *skb);
107 static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb);
108 static bool tipc_link_failover_rcv(struct tipc_link *l, struct sk_buff **skb);
109 /*
110  *  Simple link routines
111  */
112 static unsigned int align(unsigned int i)
113 {
114         return (i + 3) & ~3u;
115 }
116
117 static void tipc_link_release(struct kref *kref)
118 {
119         kfree(container_of(kref, struct tipc_link, ref));
120 }
121
122 static void tipc_link_get(struct tipc_link *l_ptr)
123 {
124         kref_get(&l_ptr->ref);
125 }
126
127 static void tipc_link_put(struct tipc_link *l_ptr)
128 {
129         kref_put(&l_ptr->ref, tipc_link_release);
130 }
131
132 static struct tipc_link *tipc_parallel_link(struct tipc_link *l)
133 {
134         if (l->owner->active_links[0] != l)
135                 return l->owner->active_links[0];
136         return l->owner->active_links[1];
137 }
138
139 static void link_init_max_pkt(struct tipc_link *l_ptr)
140 {
141         struct tipc_node *node = l_ptr->owner;
142         struct tipc_net *tn = net_generic(node->net, tipc_net_id);
143         struct tipc_bearer *b_ptr;
144         u32 max_pkt;
145
146         rcu_read_lock();
147         b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
148         if (!b_ptr) {
149                 rcu_read_unlock();
150                 return;
151         }
152         max_pkt = (b_ptr->mtu & ~3);
153         rcu_read_unlock();
154
155         if (max_pkt > MAX_MSG_SIZE)
156                 max_pkt = MAX_MSG_SIZE;
157
158         l_ptr->max_pkt_target = max_pkt;
159         if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
160                 l_ptr->max_pkt = l_ptr->max_pkt_target;
161         else
162                 l_ptr->max_pkt = MAX_PKT_DEFAULT;
163
164         l_ptr->max_pkt_probes = 0;
165 }
166
167 /*
168  *  Simple non-static link routines (i.e. referenced outside this file)
169  */
170 int tipc_link_is_up(struct tipc_link *l_ptr)
171 {
172         if (!l_ptr)
173                 return 0;
174         return link_working_working(l_ptr) || link_working_unknown(l_ptr);
175 }
176
177 int tipc_link_is_active(struct tipc_link *l_ptr)
178 {
179         return  (l_ptr->owner->active_links[0] == l_ptr) ||
180                 (l_ptr->owner->active_links[1] == l_ptr);
181 }
182
183 /**
184  * link_timeout - handle expiration of link timer
185  * @l_ptr: pointer to link
186  */
187 static void link_timeout(unsigned long data)
188 {
189         struct tipc_link *l_ptr = (struct tipc_link *)data;
190         struct sk_buff *skb;
191
192         tipc_node_lock(l_ptr->owner);
193
194         /* update counters used in statistical profiling of send traffic */
195         l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->transmq);
196         l_ptr->stats.queue_sz_counts++;
197
198         skb = skb_peek(&l_ptr->transmq);
199         if (skb) {
200                 struct tipc_msg *msg = buf_msg(skb);
201                 u32 length = msg_size(msg);
202
203                 if ((msg_user(msg) == MSG_FRAGMENTER) &&
204                     (msg_type(msg) == FIRST_FRAGMENT)) {
205                         length = msg_size(msg_get_wrapped(msg));
206                 }
207                 if (length) {
208                         l_ptr->stats.msg_lengths_total += length;
209                         l_ptr->stats.msg_length_counts++;
210                         if (length <= 64)
211                                 l_ptr->stats.msg_length_profile[0]++;
212                         else if (length <= 256)
213                                 l_ptr->stats.msg_length_profile[1]++;
214                         else if (length <= 1024)
215                                 l_ptr->stats.msg_length_profile[2]++;
216                         else if (length <= 4096)
217                                 l_ptr->stats.msg_length_profile[3]++;
218                         else if (length <= 16384)
219                                 l_ptr->stats.msg_length_profile[4]++;
220                         else if (length <= 32768)
221                                 l_ptr->stats.msg_length_profile[5]++;
222                         else
223                                 l_ptr->stats.msg_length_profile[6]++;
224                 }
225         }
226
227         /* do all other link processing performed on a periodic basis */
228         link_state_event(l_ptr, TIMEOUT_EVT);
229
230         if (skb_queue_len(&l_ptr->backlogq))
231                 tipc_link_push_packets(l_ptr);
232
233         tipc_node_unlock(l_ptr->owner);
234         tipc_link_put(l_ptr);
235 }
236
237 static void link_set_timer(struct tipc_link *link, unsigned long time)
238 {
239         if (!mod_timer(&link->timer, jiffies + time))
240                 tipc_link_get(link);
241 }
242
243 /**
244  * tipc_link_create - create a new link
245  * @n_ptr: pointer to associated node
246  * @b_ptr: pointer to associated bearer
247  * @media_addr: media address to use when sending messages over link
248  *
249  * Returns pointer to link.
250  */
251 struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
252                                    struct tipc_bearer *b_ptr,
253                                    const struct tipc_media_addr *media_addr)
254 {
255         struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
256         struct tipc_link *l_ptr;
257         struct tipc_msg *msg;
258         char *if_name;
259         char addr_string[16];
260         u32 peer = n_ptr->addr;
261
262         if (n_ptr->link_cnt >= MAX_BEARERS) {
263                 tipc_addr_string_fill(addr_string, n_ptr->addr);
264                 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
265                         n_ptr->link_cnt, addr_string, MAX_BEARERS);
266                 return NULL;
267         }
268
269         if (n_ptr->links[b_ptr->identity]) {
270                 tipc_addr_string_fill(addr_string, n_ptr->addr);
271                 pr_err("Attempt to establish second link on <%s> to %s\n",
272                        b_ptr->name, addr_string);
273                 return NULL;
274         }
275
276         l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
277         if (!l_ptr) {
278                 pr_warn("Link creation failed, no memory\n");
279                 return NULL;
280         }
281         kref_init(&l_ptr->ref);
282         l_ptr->addr = peer;
283         if_name = strchr(b_ptr->name, ':') + 1;
284         sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
285                 tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
286                 tipc_node(tn->own_addr),
287                 if_name,
288                 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
289                 /* note: peer i/f name is updated by reset/activate message */
290         memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
291         l_ptr->owner = n_ptr;
292         l_ptr->checkpoint = 1;
293         l_ptr->peer_session = INVALID_SESSION;
294         l_ptr->bearer_id = b_ptr->identity;
295         link_set_supervision_props(l_ptr, b_ptr->tolerance);
296         l_ptr->state = RESET_UNKNOWN;
297
298         l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
299         msg = l_ptr->pmsg;
300         tipc_msg_init(tn->own_addr, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
301                       l_ptr->addr);
302         msg_set_size(msg, sizeof(l_ptr->proto_msg));
303         msg_set_session(msg, (tn->random & 0xffff));
304         msg_set_bearer_id(msg, b_ptr->identity);
305         strcpy((char *)msg_data(msg), if_name);
306         l_ptr->net_plane = b_ptr->net_plane;
307         link_init_max_pkt(l_ptr);
308         l_ptr->priority = b_ptr->priority;
309         tipc_link_set_queue_limits(l_ptr, b_ptr->window);
310         l_ptr->next_out_no = 1;
311         __skb_queue_head_init(&l_ptr->transmq);
312         __skb_queue_head_init(&l_ptr->backlogq);
313         __skb_queue_head_init(&l_ptr->deferdq);
314         skb_queue_head_init(&l_ptr->wakeupq);
315         skb_queue_head_init(&l_ptr->inputq);
316         skb_queue_head_init(&l_ptr->namedq);
317         link_reset_statistics(l_ptr);
318         tipc_node_attach_link(n_ptr, l_ptr);
319         setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
320         link_state_event(l_ptr, STARTING_EVT);
321
322         return l_ptr;
323 }
324
325 /**
326  * tipc_link_delete - Delete a link
327  * @l: link to be deleted
328  */
329 void tipc_link_delete(struct tipc_link *l)
330 {
331         tipc_link_reset(l);
332         if (del_timer(&l->timer))
333                 tipc_link_put(l);
334         l->flags |= LINK_STOPPED;
335         /* Delete link now, or when timer is finished: */
336         tipc_link_reset_fragments(l);
337         tipc_node_detach_link(l->owner, l);
338         tipc_link_put(l);
339 }
340
341 void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
342                            bool shutting_down)
343 {
344         struct tipc_net *tn = net_generic(net, tipc_net_id);
345         struct tipc_link *link;
346         struct tipc_node *node;
347
348         rcu_read_lock();
349         list_for_each_entry_rcu(node, &tn->node_list, list) {
350                 tipc_node_lock(node);
351                 link = node->links[bearer_id];
352                 if (link)
353                         tipc_link_delete(link);
354                 tipc_node_unlock(node);
355         }
356         rcu_read_unlock();
357 }
358
359 /**
360  * link_schedule_user - schedule a message sender for wakeup after congestion
361  * @link: congested link
362  * @list: message that was attempted sent
363  * Create pseudo msg to send back to user when congestion abates
364  * Only consumes message if there is an error
365  */
366 static int link_schedule_user(struct tipc_link *link, struct sk_buff_head *list)
367 {
368         struct tipc_msg *msg = buf_msg(skb_peek(list));
369         int imp = msg_importance(msg);
370         u32 oport = msg_origport(msg);
371         u32 addr = link_own_addr(link);
372         struct sk_buff *skb;
373
374         /* This really cannot happen...  */
375         if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
376                 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
377                 tipc_link_reset(link);
378                 goto err;
379         }
380         /* Non-blocking sender: */
381         if (TIPC_SKB_CB(skb_peek(list))->wakeup_pending)
382                 return -ELINKCONG;
383
384         /* Create and schedule wakeup pseudo message */
385         skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
386                               addr, addr, oport, 0, 0);
387         if (!skb)
388                 goto err;
389         TIPC_SKB_CB(skb)->chain_sz = skb_queue_len(list);
390         TIPC_SKB_CB(skb)->chain_imp = imp;
391         skb_queue_tail(&link->wakeupq, skb);
392         link->stats.link_congs++;
393         return -ELINKCONG;
394 err:
395         __skb_queue_purge(list);
396         return -ENOBUFS;
397 }
398
399 /**
400  * link_prepare_wakeup - prepare users for wakeup after congestion
401  * @link: congested link
402  * Move a number of waiting users, as permitted by available space in
403  * the send queue, from link wait queue to node wait queue for wakeup
404  */
405 void link_prepare_wakeup(struct tipc_link *l)
406 {
407         int pnd[TIPC_SYSTEM_IMPORTANCE + 1] = {0,};
408         int imp, lim;
409         struct sk_buff *skb, *tmp;
410
411         skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
412                 imp = TIPC_SKB_CB(skb)->chain_imp;
413                 lim = l->window + l->backlog[imp].limit;
414                 pnd[imp] += TIPC_SKB_CB(skb)->chain_sz;
415                 if ((pnd[imp] + l->backlog[imp].len) >= lim)
416                         break;
417                 skb_unlink(skb, &l->wakeupq);
418                 skb_queue_tail(&l->inputq, skb);
419                 l->owner->inputq = &l->inputq;
420                 l->owner->action_flags |= TIPC_MSG_EVT;
421         }
422 }
423
424 /**
425  * tipc_link_reset_fragments - purge link's inbound message fragments queue
426  * @l_ptr: pointer to link
427  */
428 void tipc_link_reset_fragments(struct tipc_link *l_ptr)
429 {
430         kfree_skb(l_ptr->reasm_buf);
431         l_ptr->reasm_buf = NULL;
432 }
433
434 static void tipc_link_purge_backlog(struct tipc_link *l)
435 {
436         __skb_queue_purge(&l->backlogq);
437         l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
438         l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
439         l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
440         l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
441         l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
442 }
443
444 /**
445  * tipc_link_purge_queues - purge all pkt queues associated with link
446  * @l_ptr: pointer to link
447  */
448 void tipc_link_purge_queues(struct tipc_link *l_ptr)
449 {
450         __skb_queue_purge(&l_ptr->deferdq);
451         __skb_queue_purge(&l_ptr->transmq);
452         tipc_link_purge_backlog(l_ptr);
453         tipc_link_reset_fragments(l_ptr);
454 }
455
456 void tipc_link_reset(struct tipc_link *l_ptr)
457 {
458         u32 prev_state = l_ptr->state;
459         int was_active_link = tipc_link_is_active(l_ptr);
460         struct tipc_node *owner = l_ptr->owner;
461         struct tipc_link *pl = tipc_parallel_link(l_ptr);
462
463         msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
464
465         /* Link is down, accept any session */
466         l_ptr->peer_session = INVALID_SESSION;
467
468         /* Prepare for max packet size negotiation */
469         link_init_max_pkt(l_ptr);
470
471         l_ptr->state = RESET_UNKNOWN;
472
473         if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
474                 return;
475
476         tipc_node_link_down(l_ptr->owner, l_ptr);
477         tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
478
479         if (was_active_link && tipc_node_is_up(l_ptr->owner) && (pl != l_ptr)) {
480                 l_ptr->flags |= LINK_FAILINGOVER;
481                 l_ptr->failover_checkpt = l_ptr->next_in_no;
482                 pl->failover_pkts = FIRST_FAILOVER;
483                 pl->failover_checkpt = l_ptr->next_in_no;
484                 pl->failover_skb = l_ptr->reasm_buf;
485         } else {
486                 kfree_skb(l_ptr->reasm_buf);
487         }
488         /* Clean up all queues, except inputq: */
489         __skb_queue_purge(&l_ptr->transmq);
490         __skb_queue_purge(&l_ptr->deferdq);
491         if (!owner->inputq)
492                 owner->inputq = &l_ptr->inputq;
493         skb_queue_splice_init(&l_ptr->wakeupq, owner->inputq);
494         if (!skb_queue_empty(owner->inputq))
495                 owner->action_flags |= TIPC_MSG_EVT;
496         tipc_link_purge_backlog(l_ptr);
497         l_ptr->reasm_buf = NULL;
498         l_ptr->rcv_unacked = 0;
499         l_ptr->checkpoint = 1;
500         l_ptr->next_out_no = 1;
501         l_ptr->fsm_msg_cnt = 0;
502         l_ptr->stale_count = 0;
503         link_reset_statistics(l_ptr);
504 }
505
506 void tipc_link_reset_list(struct net *net, unsigned int bearer_id)
507 {
508         struct tipc_net *tn = net_generic(net, tipc_net_id);
509         struct tipc_link *l_ptr;
510         struct tipc_node *n_ptr;
511
512         rcu_read_lock();
513         list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
514                 tipc_node_lock(n_ptr);
515                 l_ptr = n_ptr->links[bearer_id];
516                 if (l_ptr)
517                         tipc_link_reset(l_ptr);
518                 tipc_node_unlock(n_ptr);
519         }
520         rcu_read_unlock();
521 }
522
523 static void link_activate(struct tipc_link *link)
524 {
525         struct tipc_node *node = link->owner;
526
527         link->next_in_no = 1;
528         link->stats.recv_info = 1;
529         tipc_node_link_up(node, link);
530         tipc_bearer_add_dest(node->net, link->bearer_id, link->addr);
531 }
532
533 /**
534  * link_state_event - link finite state machine
535  * @l_ptr: pointer to link
536  * @event: state machine event to process
537  */
538 static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
539 {
540         struct tipc_link *other;
541         unsigned long cont_intv = l_ptr->cont_intv;
542
543         if (l_ptr->flags & LINK_STOPPED)
544                 return;
545
546         if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
547                 return;         /* Not yet. */
548
549         if (l_ptr->flags & LINK_FAILINGOVER) {
550                 if (event == TIMEOUT_EVT)
551                         link_set_timer(l_ptr, cont_intv);
552                 return;
553         }
554
555         switch (l_ptr->state) {
556         case WORKING_WORKING:
557                 switch (event) {
558                 case TRAFFIC_MSG_EVT:
559                 case ACTIVATE_MSG:
560                         break;
561                 case TIMEOUT_EVT:
562                         if (l_ptr->next_in_no != l_ptr->checkpoint) {
563                                 l_ptr->checkpoint = l_ptr->next_in_no;
564                                 if (tipc_bclink_acks_missing(l_ptr->owner)) {
565                                         tipc_link_proto_xmit(l_ptr, STATE_MSG,
566                                                              0, 0, 0, 0, 0);
567                                         l_ptr->fsm_msg_cnt++;
568                                 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
569                                         tipc_link_proto_xmit(l_ptr, STATE_MSG,
570                                                              1, 0, 0, 0, 0);
571                                         l_ptr->fsm_msg_cnt++;
572                                 }
573                                 link_set_timer(l_ptr, cont_intv);
574                                 break;
575                         }
576                         l_ptr->state = WORKING_UNKNOWN;
577                         l_ptr->fsm_msg_cnt = 0;
578                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
579                         l_ptr->fsm_msg_cnt++;
580                         link_set_timer(l_ptr, cont_intv / 4);
581                         break;
582                 case RESET_MSG:
583                         pr_debug("%s<%s>, requested by peer\n",
584                                  link_rst_msg, l_ptr->name);
585                         tipc_link_reset(l_ptr);
586                         l_ptr->state = RESET_RESET;
587                         l_ptr->fsm_msg_cnt = 0;
588                         tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
589                                              0, 0, 0, 0, 0);
590                         l_ptr->fsm_msg_cnt++;
591                         link_set_timer(l_ptr, cont_intv);
592                         break;
593                 default:
594                         pr_debug("%s%u in WW state\n", link_unk_evt, event);
595                 }
596                 break;
597         case WORKING_UNKNOWN:
598                 switch (event) {
599                 case TRAFFIC_MSG_EVT:
600                 case ACTIVATE_MSG:
601                         l_ptr->state = WORKING_WORKING;
602                         l_ptr->fsm_msg_cnt = 0;
603                         link_set_timer(l_ptr, cont_intv);
604                         break;
605                 case RESET_MSG:
606                         pr_debug("%s<%s>, requested by peer while probing\n",
607                                  link_rst_msg, l_ptr->name);
608                         tipc_link_reset(l_ptr);
609                         l_ptr->state = RESET_RESET;
610                         l_ptr->fsm_msg_cnt = 0;
611                         tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
612                                              0, 0, 0, 0, 0);
613                         l_ptr->fsm_msg_cnt++;
614                         link_set_timer(l_ptr, cont_intv);
615                         break;
616                 case TIMEOUT_EVT:
617                         if (l_ptr->next_in_no != l_ptr->checkpoint) {
618                                 l_ptr->state = WORKING_WORKING;
619                                 l_ptr->fsm_msg_cnt = 0;
620                                 l_ptr->checkpoint = l_ptr->next_in_no;
621                                 if (tipc_bclink_acks_missing(l_ptr->owner)) {
622                                         tipc_link_proto_xmit(l_ptr, STATE_MSG,
623                                                              0, 0, 0, 0, 0);
624                                         l_ptr->fsm_msg_cnt++;
625                                 }
626                                 link_set_timer(l_ptr, cont_intv);
627                         } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
628                                 tipc_link_proto_xmit(l_ptr, STATE_MSG,
629                                                      1, 0, 0, 0, 0);
630                                 l_ptr->fsm_msg_cnt++;
631                                 link_set_timer(l_ptr, cont_intv / 4);
632                         } else {        /* Link has failed */
633                                 pr_debug("%s<%s>, peer not responding\n",
634                                          link_rst_msg, l_ptr->name);
635                                 tipc_link_reset(l_ptr);
636                                 l_ptr->state = RESET_UNKNOWN;
637                                 l_ptr->fsm_msg_cnt = 0;
638                                 tipc_link_proto_xmit(l_ptr, RESET_MSG,
639                                                      0, 0, 0, 0, 0);
640                                 l_ptr->fsm_msg_cnt++;
641                                 link_set_timer(l_ptr, cont_intv);
642                         }
643                         break;
644                 default:
645                         pr_err("%s%u in WU state\n", link_unk_evt, event);
646                 }
647                 break;
648         case RESET_UNKNOWN:
649                 switch (event) {
650                 case TRAFFIC_MSG_EVT:
651                         break;
652                 case ACTIVATE_MSG:
653                         other = l_ptr->owner->active_links[0];
654                         if (other && link_working_unknown(other))
655                                 break;
656                         l_ptr->state = WORKING_WORKING;
657                         l_ptr->fsm_msg_cnt = 0;
658                         link_activate(l_ptr);
659                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
660                         l_ptr->fsm_msg_cnt++;
661                         if (l_ptr->owner->working_links == 1)
662                                 tipc_link_sync_xmit(l_ptr);
663                         link_set_timer(l_ptr, cont_intv);
664                         break;
665                 case RESET_MSG:
666                         l_ptr->state = RESET_RESET;
667                         l_ptr->fsm_msg_cnt = 0;
668                         tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
669                                              1, 0, 0, 0, 0);
670                         l_ptr->fsm_msg_cnt++;
671                         link_set_timer(l_ptr, cont_intv);
672                         break;
673                 case STARTING_EVT:
674                         l_ptr->flags |= LINK_STARTED;
675                         l_ptr->fsm_msg_cnt++;
676                         link_set_timer(l_ptr, cont_intv);
677                         break;
678                 case TIMEOUT_EVT:
679                         tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
680                         l_ptr->fsm_msg_cnt++;
681                         link_set_timer(l_ptr, cont_intv);
682                         break;
683                 default:
684                         pr_err("%s%u in RU state\n", link_unk_evt, event);
685                 }
686                 break;
687         case RESET_RESET:
688                 switch (event) {
689                 case TRAFFIC_MSG_EVT:
690                 case ACTIVATE_MSG:
691                         other = l_ptr->owner->active_links[0];
692                         if (other && link_working_unknown(other))
693                                 break;
694                         l_ptr->state = WORKING_WORKING;
695                         l_ptr->fsm_msg_cnt = 0;
696                         link_activate(l_ptr);
697                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
698                         l_ptr->fsm_msg_cnt++;
699                         if (l_ptr->owner->working_links == 1)
700                                 tipc_link_sync_xmit(l_ptr);
701                         link_set_timer(l_ptr, cont_intv);
702                         break;
703                 case RESET_MSG:
704                         break;
705                 case TIMEOUT_EVT:
706                         tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
707                                              0, 0, 0, 0, 0);
708                         l_ptr->fsm_msg_cnt++;
709                         link_set_timer(l_ptr, cont_intv);
710                         break;
711                 default:
712                         pr_err("%s%u in RR state\n", link_unk_evt, event);
713                 }
714                 break;
715         default:
716                 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
717         }
718 }
719
720 /**
721  * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
722  * @link: link to use
723  * @list: chain of buffers containing message
724  *
725  * Consumes the buffer chain, except when returning -ELINKCONG,
726  * since the caller then may want to make more send attempts.
727  * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
728  * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
729  */
730 int __tipc_link_xmit(struct net *net, struct tipc_link *link,
731                      struct sk_buff_head *list)
732 {
733         struct tipc_msg *msg = buf_msg(skb_peek(list));
734         unsigned int maxwin = link->window;
735         unsigned int imp = msg_importance(msg);
736         uint mtu = link->max_pkt;
737         uint ack = mod(link->next_in_no - 1);
738         uint seqno = link->next_out_no;
739         uint bc_last_in = link->owner->bclink.last_in;
740         struct tipc_media_addr *addr = &link->media_addr;
741         struct sk_buff_head *transmq = &link->transmq;
742         struct sk_buff_head *backlogq = &link->backlogq;
743         struct sk_buff *skb, *tmp;
744
745         /* Match backlog limit against msg importance: */
746         if (unlikely(link->backlog[imp].len >= link->backlog[imp].limit))
747                 return link_schedule_user(link, list);
748
749         if (unlikely(msg_size(msg) > mtu)) {
750                 __skb_queue_purge(list);
751                 return -EMSGSIZE;
752         }
753         /* Prepare each packet for sending, and add to relevant queue: */
754         skb_queue_walk_safe(list, skb, tmp) {
755                 __skb_unlink(skb, list);
756                 msg = buf_msg(skb);
757                 msg_set_seqno(msg, seqno);
758                 msg_set_ack(msg, ack);
759                 msg_set_bcast_ack(msg, bc_last_in);
760
761                 if (likely(skb_queue_len(transmq) < maxwin)) {
762                         __skb_queue_tail(transmq, skb);
763                         tipc_bearer_send(net, link->bearer_id, skb, addr);
764                         link->rcv_unacked = 0;
765                         seqno++;
766                         continue;
767                 }
768                 if (tipc_msg_bundle(skb_peek_tail(backlogq), skb, mtu)) {
769                         link->stats.sent_bundled++;
770                         continue;
771                 }
772                 if (tipc_msg_make_bundle(&skb, mtu, link->addr)) {
773                         link->stats.sent_bundled++;
774                         link->stats.sent_bundles++;
775                         imp = msg_importance(buf_msg(skb));
776                 }
777                 __skb_queue_tail(backlogq, skb);
778                 link->backlog[imp].len++;
779                 seqno++;
780         }
781         link->next_out_no = seqno;
782         return 0;
783 }
784
785 static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
786 {
787         skb_queue_head_init(list);
788         __skb_queue_tail(list, skb);
789 }
790
791 static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
792 {
793         struct sk_buff_head head;
794
795         skb2list(skb, &head);
796         return __tipc_link_xmit(link->owner->net, link, &head);
797 }
798
799 /* tipc_link_xmit_skb(): send single buffer to destination
800  * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
801  * messages, which will not be rejected
802  * The only exception is datagram messages rerouted after secondary
803  * lookup, which are rare and safe to dispose of anyway.
804  * TODO: Return real return value, and let callers use
805  * tipc_wait_for_sendpkt() where applicable
806  */
807 int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
808                        u32 selector)
809 {
810         struct sk_buff_head head;
811         int rc;
812
813         skb2list(skb, &head);
814         rc = tipc_link_xmit(net, &head, dnode, selector);
815         if (rc == -ELINKCONG)
816                 kfree_skb(skb);
817         return 0;
818 }
819
820 /**
821  * tipc_link_xmit() is the general link level function for message sending
822  * @net: the applicable net namespace
823  * @list: chain of buffers containing message
824  * @dsz: amount of user data to be sent
825  * @dnode: address of destination node
826  * @selector: a number used for deterministic link selection
827  * Consumes the buffer chain, except when returning -ELINKCONG
828  * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
829  */
830 int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
831                    u32 selector)
832 {
833         struct tipc_link *link = NULL;
834         struct tipc_node *node;
835         int rc = -EHOSTUNREACH;
836
837         node = tipc_node_find(net, dnode);
838         if (node) {
839                 tipc_node_lock(node);
840                 link = node->active_links[selector & 1];
841                 if (link)
842                         rc = __tipc_link_xmit(net, link, list);
843                 tipc_node_unlock(node);
844                 tipc_node_put(node);
845         }
846         if (link)
847                 return rc;
848
849         if (likely(in_own_node(net, dnode))) {
850                 tipc_sk_rcv(net, list);
851                 return 0;
852         }
853
854         __skb_queue_purge(list);
855         return rc;
856 }
857
858 /*
859  * tipc_link_sync_xmit - synchronize broadcast link endpoints.
860  *
861  * Give a newly added peer node the sequence number where it should
862  * start receiving and acking broadcast packets.
863  *
864  * Called with node locked
865  */
866 static void tipc_link_sync_xmit(struct tipc_link *link)
867 {
868         struct sk_buff *skb;
869         struct tipc_msg *msg;
870
871         skb = tipc_buf_acquire(INT_H_SIZE);
872         if (!skb)
873                 return;
874
875         msg = buf_msg(skb);
876         tipc_msg_init(link_own_addr(link), msg, BCAST_PROTOCOL, STATE_MSG,
877                       INT_H_SIZE, link->addr);
878         msg_set_last_bcast(msg, link->owner->bclink.acked);
879         __tipc_link_xmit_skb(link, skb);
880 }
881
882 /*
883  * tipc_link_sync_rcv - synchronize broadcast link endpoints.
884  * Receive the sequence number where we should start receiving and
885  * acking broadcast packets from a newly added peer node, and open
886  * up for reception of such packets.
887  *
888  * Called with node locked
889  */
890 static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
891 {
892         struct tipc_msg *msg = buf_msg(buf);
893
894         n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
895         n->bclink.recv_permitted = true;
896         kfree_skb(buf);
897 }
898
899 /*
900  * tipc_link_push_packets - push unsent packets to bearer
901  *
902  * Push out the unsent messages of a link where congestion
903  * has abated. Node is locked.
904  *
905  * Called with node locked
906  */
907 void tipc_link_push_packets(struct tipc_link *link)
908 {
909         struct sk_buff *skb;
910         struct tipc_msg *msg;
911         unsigned int ack = mod(link->next_in_no - 1);
912
913         while (skb_queue_len(&link->transmq) < link->window) {
914                 skb = __skb_dequeue(&link->backlogq);
915                 if (!skb)
916                         break;
917                 msg = buf_msg(skb);
918                 link->backlog[msg_importance(msg)].len--;
919                 msg_set_ack(msg, ack);
920                 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
921                 link->rcv_unacked = 0;
922                 __skb_queue_tail(&link->transmq, skb);
923                 tipc_bearer_send(link->owner->net, link->bearer_id,
924                                  skb, &link->media_addr);
925         }
926 }
927
928 void tipc_link_reset_all(struct tipc_node *node)
929 {
930         char addr_string[16];
931         u32 i;
932
933         tipc_node_lock(node);
934
935         pr_warn("Resetting all links to %s\n",
936                 tipc_addr_string_fill(addr_string, node->addr));
937
938         for (i = 0; i < MAX_BEARERS; i++) {
939                 if (node->links[i]) {
940                         link_print(node->links[i], "Resetting link\n");
941                         tipc_link_reset(node->links[i]);
942                 }
943         }
944
945         tipc_node_unlock(node);
946 }
947
948 static void link_retransmit_failure(struct tipc_link *l_ptr,
949                                     struct sk_buff *buf)
950 {
951         struct tipc_msg *msg = buf_msg(buf);
952         struct net *net = l_ptr->owner->net;
953
954         pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
955
956         if (l_ptr->addr) {
957                 /* Handle failure on standard link */
958                 link_print(l_ptr, "Resetting link\n");
959                 tipc_link_reset(l_ptr);
960
961         } else {
962                 /* Handle failure on broadcast link */
963                 struct tipc_node *n_ptr;
964                 char addr_string[16];
965
966                 pr_info("Msg seq number: %u,  ", msg_seqno(msg));
967                 pr_cont("Outstanding acks: %lu\n",
968                         (unsigned long) TIPC_SKB_CB(buf)->handle);
969
970                 n_ptr = tipc_bclink_retransmit_to(net);
971
972                 tipc_addr_string_fill(addr_string, n_ptr->addr);
973                 pr_info("Broadcast link info for %s\n", addr_string);
974                 pr_info("Reception permitted: %d,  Acked: %u\n",
975                         n_ptr->bclink.recv_permitted,
976                         n_ptr->bclink.acked);
977                 pr_info("Last in: %u,  Oos state: %u,  Last sent: %u\n",
978                         n_ptr->bclink.last_in,
979                         n_ptr->bclink.oos_state,
980                         n_ptr->bclink.last_sent);
981
982                 n_ptr->action_flags |= TIPC_BCAST_RESET;
983                 l_ptr->stale_count = 0;
984         }
985 }
986
987 void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
988                           u32 retransmits)
989 {
990         struct tipc_msg *msg;
991
992         if (!skb)
993                 return;
994
995         msg = buf_msg(skb);
996
997         /* Detect repeated retransmit failures */
998         if (l_ptr->last_retransmitted == msg_seqno(msg)) {
999                 if (++l_ptr->stale_count > 100) {
1000                         link_retransmit_failure(l_ptr, skb);
1001                         return;
1002                 }
1003         } else {
1004                 l_ptr->last_retransmitted = msg_seqno(msg);
1005                 l_ptr->stale_count = 1;
1006         }
1007
1008         skb_queue_walk_from(&l_ptr->transmq, skb) {
1009                 if (!retransmits)
1010                         break;
1011                 msg = buf_msg(skb);
1012                 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1013                 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1014                 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
1015                                  &l_ptr->media_addr);
1016                 retransmits--;
1017                 l_ptr->stats.retransmitted++;
1018         }
1019 }
1020
1021 /* link_synch(): check if all packets arrived before the synch
1022  *               point have been consumed
1023  * Returns true if the parallel links are synched, otherwise false
1024  */
1025 static bool link_synch(struct tipc_link *l)
1026 {
1027         unsigned int post_synch;
1028         struct tipc_link *pl;
1029
1030         pl  = tipc_parallel_link(l);
1031         if (pl == l)
1032                 goto synched;
1033
1034         /* Was last pre-synch packet added to input queue ? */
1035         if (less_eq(pl->next_in_no, l->synch_point))
1036                 return false;
1037
1038         /* Is it still in the input queue ? */
1039         post_synch = mod(pl->next_in_no - l->synch_point) - 1;
1040         if (skb_queue_len(&pl->inputq) > post_synch)
1041                 return false;
1042 synched:
1043         l->flags &= ~LINK_SYNCHING;
1044         return true;
1045 }
1046
1047 static void link_retrieve_defq(struct tipc_link *link,
1048                                struct sk_buff_head *list)
1049 {
1050         u32 seq_no;
1051
1052         if (skb_queue_empty(&link->deferdq))
1053                 return;
1054
1055         seq_no = buf_seqno(skb_peek(&link->deferdq));
1056         if (seq_no == mod(link->next_in_no))
1057                 skb_queue_splice_tail_init(&link->deferdq, list);
1058 }
1059
1060 /**
1061  * tipc_rcv - process TIPC packets/messages arriving from off-node
1062  * @net: the applicable net namespace
1063  * @skb: TIPC packet
1064  * @b_ptr: pointer to bearer message arrived on
1065  *
1066  * Invoked with no locks held.  Bearer pointer must point to a valid bearer
1067  * structure (i.e. cannot be NULL), but bearer can be inactive.
1068  */
1069 void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
1070 {
1071         struct tipc_net *tn = net_generic(net, tipc_net_id);
1072         struct sk_buff_head head;
1073         struct tipc_node *n_ptr;
1074         struct tipc_link *l_ptr;
1075         struct sk_buff *skb1, *tmp;
1076         struct tipc_msg *msg;
1077         u32 seq_no;
1078         u32 ackd;
1079         u32 released;
1080
1081         skb2list(skb, &head);
1082
1083         while ((skb = __skb_dequeue(&head))) {
1084                 /* Ensure message is well-formed */
1085                 if (unlikely(!tipc_msg_validate(skb)))
1086                         goto discard;
1087
1088                 /* Handle arrival of a non-unicast link message */
1089                 msg = buf_msg(skb);
1090                 if (unlikely(msg_non_seq(msg))) {
1091                         if (msg_user(msg) ==  LINK_CONFIG)
1092                                 tipc_disc_rcv(net, skb, b_ptr);
1093                         else
1094                                 tipc_bclink_rcv(net, skb);
1095                         continue;
1096                 }
1097
1098                 /* Discard unicast link messages destined for another node */
1099                 if (unlikely(!msg_short(msg) &&
1100                              (msg_destnode(msg) != tn->own_addr)))
1101                         goto discard;
1102
1103                 /* Locate neighboring node that sent message */
1104                 n_ptr = tipc_node_find(net, msg_prevnode(msg));
1105                 if (unlikely(!n_ptr))
1106                         goto discard;
1107
1108                 tipc_node_lock(n_ptr);
1109                 /* Locate unicast link endpoint that should handle message */
1110                 l_ptr = n_ptr->links[b_ptr->identity];
1111                 if (unlikely(!l_ptr))
1112                         goto unlock;
1113
1114                 /* Verify that communication with node is currently allowed */
1115                 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
1116                     msg_user(msg) == LINK_PROTOCOL &&
1117                     (msg_type(msg) == RESET_MSG ||
1118                     msg_type(msg) == ACTIVATE_MSG) &&
1119                     !msg_redundant_link(msg))
1120                         n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
1121
1122                 if (tipc_node_blocked(n_ptr))
1123                         goto unlock;
1124
1125                 /* Validate message sequence number info */
1126                 seq_no = msg_seqno(msg);
1127                 ackd = msg_ack(msg);
1128
1129                 /* Release acked messages */
1130                 if (unlikely(n_ptr->bclink.acked != msg_bcast_ack(msg)))
1131                         tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
1132
1133                 released = 0;
1134                 skb_queue_walk_safe(&l_ptr->transmq, skb1, tmp) {
1135                         if (more(buf_seqno(skb1), ackd))
1136                                 break;
1137                          __skb_unlink(skb1, &l_ptr->transmq);
1138                          kfree_skb(skb1);
1139                          released = 1;
1140                 }
1141
1142                 /* Try sending any messages link endpoint has pending */
1143                 if (unlikely(skb_queue_len(&l_ptr->backlogq)))
1144                         tipc_link_push_packets(l_ptr);
1145
1146                 if (released && !skb_queue_empty(&l_ptr->wakeupq))
1147                         link_prepare_wakeup(l_ptr);
1148
1149                 /* Process the incoming packet */
1150                 if (unlikely(!link_working_working(l_ptr))) {
1151                         if (msg_user(msg) == LINK_PROTOCOL) {
1152                                 tipc_link_proto_rcv(l_ptr, skb);
1153                                 link_retrieve_defq(l_ptr, &head);
1154                                 skb = NULL;
1155                                 goto unlock;
1156                         }
1157
1158                         /* Traffic message. Conditionally activate link */
1159                         link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1160
1161                         if (link_working_working(l_ptr)) {
1162                                 /* Re-insert buffer in front of queue */
1163                                 __skb_queue_head(&head, skb);
1164                                 skb = NULL;
1165                                 goto unlock;
1166                         }
1167                         goto unlock;
1168                 }
1169
1170                 /* Link is now in state WORKING_WORKING */
1171                 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
1172                         link_handle_out_of_seq_msg(l_ptr, skb);
1173                         link_retrieve_defq(l_ptr, &head);
1174                         skb = NULL;
1175                         goto unlock;
1176                 }
1177                 /* Synchronize with parallel link if applicable */
1178                 if (unlikely((l_ptr->flags & LINK_SYNCHING) && !msg_dup(msg))) {
1179                         link_handle_out_of_seq_msg(l_ptr, skb);
1180                         if (link_synch(l_ptr))
1181                                 link_retrieve_defq(l_ptr, &head);
1182                         skb = NULL;
1183                         goto unlock;
1184                 }
1185                 l_ptr->next_in_no++;
1186                 if (unlikely(!skb_queue_empty(&l_ptr->deferdq)))
1187                         link_retrieve_defq(l_ptr, &head);
1188                 if (unlikely(++l_ptr->rcv_unacked >= TIPC_MIN_LINK_WIN)) {
1189                         l_ptr->stats.sent_acks++;
1190                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1191                 }
1192                 tipc_link_input(l_ptr, skb);
1193                 skb = NULL;
1194 unlock:
1195                 tipc_node_unlock(n_ptr);
1196                 tipc_node_put(n_ptr);
1197 discard:
1198                 if (unlikely(skb))
1199                         kfree_skb(skb);
1200         }
1201 }
1202
1203 /* tipc_data_input - deliver data and name distr msgs to upper layer
1204  *
1205  * Consumes buffer if message is of right type
1206  * Node lock must be held
1207  */
1208 static bool tipc_data_input(struct tipc_link *link, struct sk_buff *skb)
1209 {
1210         struct tipc_node *node = link->owner;
1211         struct tipc_msg *msg = buf_msg(skb);
1212         u32 dport = msg_destport(msg);
1213
1214         switch (msg_user(msg)) {
1215         case TIPC_LOW_IMPORTANCE:
1216         case TIPC_MEDIUM_IMPORTANCE:
1217         case TIPC_HIGH_IMPORTANCE:
1218         case TIPC_CRITICAL_IMPORTANCE:
1219         case CONN_MANAGER:
1220                 if (tipc_skb_queue_tail(&link->inputq, skb, dport)) {
1221                         node->inputq = &link->inputq;
1222                         node->action_flags |= TIPC_MSG_EVT;
1223                 }
1224                 return true;
1225         case NAME_DISTRIBUTOR:
1226                 node->bclink.recv_permitted = true;
1227                 node->namedq = &link->namedq;
1228                 skb_queue_tail(&link->namedq, skb);
1229                 if (skb_queue_len(&link->namedq) == 1)
1230                         node->action_flags |= TIPC_NAMED_MSG_EVT;
1231                 return true;
1232         case MSG_BUNDLER:
1233         case TUNNEL_PROTOCOL:
1234         case MSG_FRAGMENTER:
1235         case BCAST_PROTOCOL:
1236                 return false;
1237         default:
1238                 pr_warn("Dropping received illegal msg type\n");
1239                 kfree_skb(skb);
1240                 return false;
1241         };
1242 }
1243
1244 /* tipc_link_input - process packet that has passed link protocol check
1245  *
1246  * Consumes buffer
1247  * Node lock must be held
1248  */
1249 static void tipc_link_input(struct tipc_link *link, struct sk_buff *skb)
1250 {
1251         struct tipc_node *node = link->owner;
1252         struct tipc_msg *msg = buf_msg(skb);
1253         struct sk_buff *iskb;
1254         int pos = 0;
1255
1256         if (likely(tipc_data_input(link, skb)))
1257                 return;
1258
1259         switch (msg_user(msg)) {
1260         case TUNNEL_PROTOCOL:
1261                 if (msg_dup(msg)) {
1262                         link->flags |= LINK_SYNCHING;
1263                         link->synch_point = msg_seqno(msg_get_wrapped(msg));
1264                         kfree_skb(skb);
1265                         break;
1266                 }
1267                 if (!tipc_link_failover_rcv(link, &skb))
1268                         break;
1269                 if (msg_user(buf_msg(skb)) != MSG_BUNDLER) {
1270                         tipc_data_input(link, skb);
1271                         break;
1272                 }
1273         case MSG_BUNDLER:
1274                 link->stats.recv_bundles++;
1275                 link->stats.recv_bundled += msg_msgcnt(msg);
1276
1277                 while (tipc_msg_extract(skb, &iskb, &pos))
1278                         tipc_data_input(link, iskb);
1279                 break;
1280         case MSG_FRAGMENTER:
1281                 link->stats.recv_fragments++;
1282                 if (tipc_buf_append(&link->reasm_buf, &skb)) {
1283                         link->stats.recv_fragmented++;
1284                         tipc_data_input(link, skb);
1285                 } else if (!link->reasm_buf) {
1286                         tipc_link_reset(link);
1287                 }
1288                 break;
1289         case BCAST_PROTOCOL:
1290                 tipc_link_sync_rcv(node, skb);
1291                 break;
1292         default:
1293                 break;
1294         };
1295 }
1296
1297 /**
1298  * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1299  *
1300  * Returns increase in queue length (i.e. 0 or 1)
1301  */
1302 u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
1303 {
1304         struct sk_buff *skb1;
1305         u32 seq_no = buf_seqno(skb);
1306
1307         /* Empty queue ? */
1308         if (skb_queue_empty(list)) {
1309                 __skb_queue_tail(list, skb);
1310                 return 1;
1311         }
1312
1313         /* Last ? */
1314         if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1315                 __skb_queue_tail(list, skb);
1316                 return 1;
1317         }
1318
1319         /* Locate insertion point in queue, then insert; discard if duplicate */
1320         skb_queue_walk(list, skb1) {
1321                 u32 curr_seqno = buf_seqno(skb1);
1322
1323                 if (seq_no == curr_seqno) {
1324                         kfree_skb(skb);
1325                         return 0;
1326                 }
1327
1328                 if (less(seq_no, curr_seqno))
1329                         break;
1330         }
1331
1332         __skb_queue_before(list, skb1, skb);
1333         return 1;
1334 }
1335
1336 /*
1337  * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1338  */
1339 static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
1340                                        struct sk_buff *buf)
1341 {
1342         u32 seq_no = buf_seqno(buf);
1343
1344         if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1345                 tipc_link_proto_rcv(l_ptr, buf);
1346                 return;
1347         }
1348
1349         /* Record OOS packet arrival (force mismatch on next timeout) */
1350         l_ptr->checkpoint--;
1351
1352         /*
1353          * Discard packet if a duplicate; otherwise add it to deferred queue
1354          * and notify peer of gap as per protocol specification
1355          */
1356         if (less(seq_no, mod(l_ptr->next_in_no))) {
1357                 l_ptr->stats.duplicates++;
1358                 kfree_skb(buf);
1359                 return;
1360         }
1361
1362         if (tipc_link_defer_pkt(&l_ptr->deferdq, buf)) {
1363                 l_ptr->stats.deferred_recv++;
1364                 if ((skb_queue_len(&l_ptr->deferdq) % TIPC_MIN_LINK_WIN) == 1)
1365                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1366         } else {
1367                 l_ptr->stats.duplicates++;
1368         }
1369 }
1370
1371 /*
1372  * Send protocol message to the other endpoint.
1373  */
1374 void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1375                           u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
1376 {
1377         struct sk_buff *buf = NULL;
1378         struct tipc_msg *msg = l_ptr->pmsg;
1379         u32 msg_size = sizeof(l_ptr->proto_msg);
1380         int r_flag;
1381
1382         /* Don't send protocol message during link failover */
1383         if (l_ptr->flags & LINK_FAILINGOVER)
1384                 return;
1385
1386         /* Abort non-RESET send if communication with node is prohibited */
1387         if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
1388                 return;
1389
1390         /* Create protocol message with "out-of-sequence" sequence number */
1391         msg_set_type(msg, msg_typ);
1392         msg_set_net_plane(msg, l_ptr->net_plane);
1393         msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1394         msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
1395
1396         if (msg_typ == STATE_MSG) {
1397                 u32 next_sent = mod(l_ptr->next_out_no);
1398
1399                 if (!tipc_link_is_up(l_ptr))
1400                         return;
1401                 if (skb_queue_len(&l_ptr->backlogq))
1402                         next_sent = buf_seqno(skb_peek(&l_ptr->backlogq));
1403                 msg_set_next_sent(msg, next_sent);
1404                 if (!skb_queue_empty(&l_ptr->deferdq)) {
1405                         u32 rec = buf_seqno(skb_peek(&l_ptr->deferdq));
1406                         gap = mod(rec - mod(l_ptr->next_in_no));
1407                 }
1408                 msg_set_seq_gap(msg, gap);
1409                 if (gap)
1410                         l_ptr->stats.sent_nacks++;
1411                 msg_set_link_tolerance(msg, tolerance);
1412                 msg_set_linkprio(msg, priority);
1413                 msg_set_max_pkt(msg, ack_mtu);
1414                 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1415                 msg_set_probe(msg, probe_msg != 0);
1416                 if (probe_msg) {
1417                         u32 mtu = l_ptr->max_pkt;
1418
1419                         if ((mtu < l_ptr->max_pkt_target) &&
1420                             link_working_working(l_ptr) &&
1421                             l_ptr->fsm_msg_cnt) {
1422                                 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1423                                 if (l_ptr->max_pkt_probes == 10) {
1424                                         l_ptr->max_pkt_target = (msg_size - 4);
1425                                         l_ptr->max_pkt_probes = 0;
1426                                         msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1427                                 }
1428                                 l_ptr->max_pkt_probes++;
1429                         }
1430
1431                         l_ptr->stats.sent_probes++;
1432                 }
1433                 l_ptr->stats.sent_states++;
1434         } else {                /* RESET_MSG or ACTIVATE_MSG */
1435                 msg_set_ack(msg, mod(l_ptr->failover_checkpt - 1));
1436                 msg_set_seq_gap(msg, 0);
1437                 msg_set_next_sent(msg, 1);
1438                 msg_set_probe(msg, 0);
1439                 msg_set_link_tolerance(msg, l_ptr->tolerance);
1440                 msg_set_linkprio(msg, l_ptr->priority);
1441                 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1442         }
1443
1444         r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1445         msg_set_redundant_link(msg, r_flag);
1446         msg_set_linkprio(msg, l_ptr->priority);
1447         msg_set_size(msg, msg_size);
1448
1449         msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1450
1451         buf = tipc_buf_acquire(msg_size);
1452         if (!buf)
1453                 return;
1454
1455         skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
1456         buf->priority = TC_PRIO_CONTROL;
1457         tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
1458                          &l_ptr->media_addr);
1459         l_ptr->rcv_unacked = 0;
1460         kfree_skb(buf);
1461 }
1462
1463 /*
1464  * Receive protocol message :
1465  * Note that network plane id propagates through the network, and may
1466  * change at any time. The node with lowest address rules
1467  */
1468 static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
1469                                 struct sk_buff *buf)
1470 {
1471         u32 rec_gap = 0;
1472         u32 max_pkt_info;
1473         u32 max_pkt_ack;
1474         u32 msg_tol;
1475         struct tipc_msg *msg = buf_msg(buf);
1476
1477         if (l_ptr->flags & LINK_FAILINGOVER)
1478                 goto exit;
1479
1480         if (l_ptr->net_plane != msg_net_plane(msg))
1481                 if (link_own_addr(l_ptr) > msg_prevnode(msg))
1482                         l_ptr->net_plane = msg_net_plane(msg);
1483
1484         switch (msg_type(msg)) {
1485
1486         case RESET_MSG:
1487                 if (!link_working_unknown(l_ptr) &&
1488                     (l_ptr->peer_session != INVALID_SESSION)) {
1489                         if (less_eq(msg_session(msg), l_ptr->peer_session))
1490                                 break; /* duplicate or old reset: ignore */
1491                 }
1492
1493                 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1494                                 link_working_unknown(l_ptr))) {
1495                         /*
1496                          * peer has lost contact -- don't allow peer's links
1497                          * to reactivate before we recognize loss & clean up
1498                          */
1499                         l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
1500                 }
1501
1502                 link_state_event(l_ptr, RESET_MSG);
1503
1504                 /* fall thru' */
1505         case ACTIVATE_MSG:
1506                 /* Update link settings according other endpoint's values */
1507                 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1508
1509                 msg_tol = msg_link_tolerance(msg);
1510                 if (msg_tol > l_ptr->tolerance)
1511                         link_set_supervision_props(l_ptr, msg_tol);
1512
1513                 if (msg_linkprio(msg) > l_ptr->priority)
1514                         l_ptr->priority = msg_linkprio(msg);
1515
1516                 max_pkt_info = msg_max_pkt(msg);
1517                 if (max_pkt_info) {
1518                         if (max_pkt_info < l_ptr->max_pkt_target)
1519                                 l_ptr->max_pkt_target = max_pkt_info;
1520                         if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1521                                 l_ptr->max_pkt = l_ptr->max_pkt_target;
1522                 } else {
1523                         l_ptr->max_pkt = l_ptr->max_pkt_target;
1524                 }
1525
1526                 /* Synchronize broadcast link info, if not done previously */
1527                 if (!tipc_node_is_up(l_ptr->owner)) {
1528                         l_ptr->owner->bclink.last_sent =
1529                                 l_ptr->owner->bclink.last_in =
1530                                 msg_last_bcast(msg);
1531                         l_ptr->owner->bclink.oos_state = 0;
1532                 }
1533
1534                 l_ptr->peer_session = msg_session(msg);
1535                 l_ptr->peer_bearer_id = msg_bearer_id(msg);
1536
1537                 if (msg_type(msg) == ACTIVATE_MSG)
1538                         link_state_event(l_ptr, ACTIVATE_MSG);
1539                 break;
1540         case STATE_MSG:
1541
1542                 msg_tol = msg_link_tolerance(msg);
1543                 if (msg_tol)
1544                         link_set_supervision_props(l_ptr, msg_tol);
1545
1546                 if (msg_linkprio(msg) &&
1547                     (msg_linkprio(msg) != l_ptr->priority)) {
1548                         pr_debug("%s<%s>, priority change %u->%u\n",
1549                                  link_rst_msg, l_ptr->name,
1550                                  l_ptr->priority, msg_linkprio(msg));
1551                         l_ptr->priority = msg_linkprio(msg);
1552                         tipc_link_reset(l_ptr); /* Enforce change to take effect */
1553                         break;
1554                 }
1555
1556                 /* Record reception; force mismatch at next timeout: */
1557                 l_ptr->checkpoint--;
1558
1559                 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1560                 l_ptr->stats.recv_states++;
1561                 if (link_reset_unknown(l_ptr))
1562                         break;
1563
1564                 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
1565                         rec_gap = mod(msg_next_sent(msg) -
1566                                       mod(l_ptr->next_in_no));
1567                 }
1568
1569                 max_pkt_ack = msg_max_pkt(msg);
1570                 if (max_pkt_ack > l_ptr->max_pkt) {
1571                         l_ptr->max_pkt = max_pkt_ack;
1572                         l_ptr->max_pkt_probes = 0;
1573                 }
1574
1575                 max_pkt_ack = 0;
1576                 if (msg_probe(msg)) {
1577                         l_ptr->stats.recv_probes++;
1578                         if (msg_size(msg) > sizeof(l_ptr->proto_msg))
1579                                 max_pkt_ack = msg_size(msg);
1580                 }
1581
1582                 /* Protocol message before retransmits, reduce loss risk */
1583                 if (l_ptr->owner->bclink.recv_permitted)
1584                         tipc_bclink_update_link_state(l_ptr->owner,
1585                                                       msg_last_bcast(msg));
1586
1587                 if (rec_gap || (msg_probe(msg))) {
1588                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1589                                              0, max_pkt_ack);
1590                 }
1591                 if (msg_seq_gap(msg)) {
1592                         l_ptr->stats.recv_nacks++;
1593                         tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->transmq),
1594                                              msg_seq_gap(msg));
1595                 }
1596                 break;
1597         }
1598 exit:
1599         kfree_skb(buf);
1600 }
1601
1602
1603 /* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1604  * a different bearer. Owner node is locked.
1605  */
1606 static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1607                                   struct tipc_msg *tunnel_hdr,
1608                                   struct tipc_msg *msg,
1609                                   u32 selector)
1610 {
1611         struct tipc_link *tunnel;
1612         struct sk_buff *skb;
1613         u32 length = msg_size(msg);
1614
1615         tunnel = l_ptr->owner->active_links[selector & 1];
1616         if (!tipc_link_is_up(tunnel)) {
1617                 pr_warn("%stunnel link no longer available\n", link_co_err);
1618                 return;
1619         }
1620         msg_set_size(tunnel_hdr, length + INT_H_SIZE);
1621         skb = tipc_buf_acquire(length + INT_H_SIZE);
1622         if (!skb) {
1623                 pr_warn("%sunable to send tunnel msg\n", link_co_err);
1624                 return;
1625         }
1626         skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
1627         skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
1628         __tipc_link_xmit_skb(tunnel, skb);
1629 }
1630
1631
1632 /* tipc_link_failover_send_queue(): A link has gone down, but a second
1633  * link is still active. We can do failover. Tunnel the failing link's
1634  * whole send queue via the remaining link. This way, we don't lose
1635  * any packets, and sequence order is preserved for subsequent traffic
1636  * sent over the remaining link. Owner node is locked.
1637  */
1638 void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
1639 {
1640         int msgcount;
1641         struct tipc_link *tunnel = l_ptr->owner->active_links[0];
1642         struct tipc_msg tunnel_hdr;
1643         struct sk_buff *skb;
1644         int split_bundles;
1645
1646         if (!tunnel)
1647                 return;
1648
1649         tipc_msg_init(link_own_addr(l_ptr), &tunnel_hdr, TUNNEL_PROTOCOL,
1650                       FAILOVER_MSG, INT_H_SIZE, l_ptr->addr);
1651         skb_queue_splice_tail_init(&l_ptr->backlogq, &l_ptr->transmq);
1652         tipc_link_purge_backlog(l_ptr);
1653         msgcount = skb_queue_len(&l_ptr->transmq);
1654         msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1655         msg_set_msgcnt(&tunnel_hdr, msgcount);
1656
1657         if (skb_queue_empty(&l_ptr->transmq)) {
1658                 skb = tipc_buf_acquire(INT_H_SIZE);
1659                 if (skb) {
1660                         skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
1661                         msg_set_size(&tunnel_hdr, INT_H_SIZE);
1662                         __tipc_link_xmit_skb(tunnel, skb);
1663                 } else {
1664                         pr_warn("%sunable to send changeover msg\n",
1665                                 link_co_err);
1666                 }
1667                 return;
1668         }
1669
1670         split_bundles = (l_ptr->owner->active_links[0] !=
1671                          l_ptr->owner->active_links[1]);
1672
1673         skb_queue_walk(&l_ptr->transmq, skb) {
1674                 struct tipc_msg *msg = buf_msg(skb);
1675
1676                 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
1677                         struct tipc_msg *m = msg_get_wrapped(msg);
1678                         unchar *pos = (unchar *)m;
1679
1680                         msgcount = msg_msgcnt(msg);
1681                         while (msgcount--) {
1682                                 msg_set_seqno(m, msg_seqno(msg));
1683                                 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1684                                                       msg_link_selector(m));
1685                                 pos += align(msg_size(m));
1686                                 m = (struct tipc_msg *)pos;
1687                         }
1688                 } else {
1689                         tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1690                                               msg_link_selector(msg));
1691                 }
1692         }
1693 }
1694
1695 /* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
1696  * duplicate of the first link's send queue via the new link. This way, we
1697  * are guaranteed that currently queued packets from a socket are delivered
1698  * before future traffic from the same socket, even if this is using the
1699  * new link. The last arriving copy of each duplicate packet is dropped at
1700  * the receiving end by the regular protocol check, so packet cardinality
1701  * and sequence order is preserved per sender/receiver socket pair.
1702  * Owner node is locked.
1703  */
1704 void tipc_link_dup_queue_xmit(struct tipc_link *link,
1705                               struct tipc_link *tnl)
1706 {
1707         struct sk_buff *skb;
1708         struct tipc_msg tnl_hdr;
1709         struct sk_buff_head *queue = &link->transmq;
1710         int mcnt;
1711
1712         tipc_msg_init(link_own_addr(link), &tnl_hdr, TUNNEL_PROTOCOL,
1713                       SYNCH_MSG, INT_H_SIZE, link->addr);
1714         mcnt = skb_queue_len(&link->transmq) + skb_queue_len(&link->backlogq);
1715         msg_set_msgcnt(&tnl_hdr, mcnt);
1716         msg_set_bearer_id(&tnl_hdr, link->peer_bearer_id);
1717
1718 tunnel_queue:
1719         skb_queue_walk(queue, skb) {
1720                 struct sk_buff *outskb;
1721                 struct tipc_msg *msg = buf_msg(skb);
1722                 u32 len = msg_size(msg);
1723
1724                 msg_set_ack(msg, mod(link->next_in_no - 1));
1725                 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
1726                 msg_set_size(&tnl_hdr, len + INT_H_SIZE);
1727                 outskb = tipc_buf_acquire(len + INT_H_SIZE);
1728                 if (outskb == NULL) {
1729                         pr_warn("%sunable to send duplicate msg\n",
1730                                 link_co_err);
1731                         return;
1732                 }
1733                 skb_copy_to_linear_data(outskb, &tnl_hdr, INT_H_SIZE);
1734                 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE,
1735                                                skb->data, len);
1736                 __tipc_link_xmit_skb(tnl, outskb);
1737                 if (!tipc_link_is_up(link))
1738                         return;
1739         }
1740         if (queue == &link->backlogq)
1741                 return;
1742         queue = &link->backlogq;
1743         goto tunnel_queue;
1744 }
1745
1746 /*  tipc_link_failover_rcv(): Receive a tunnelled FAILOVER_MSG packet
1747  *  Owner node is locked.
1748  */
1749 static bool tipc_link_failover_rcv(struct tipc_link *link,
1750                                    struct sk_buff **skb)
1751 {
1752         struct tipc_msg *msg = buf_msg(*skb);
1753         struct sk_buff *iskb = NULL;
1754         struct tipc_link *pl = NULL;
1755         int bearer_id = msg_bearer_id(msg);
1756         int pos = 0;
1757
1758         if (msg_type(msg) != FAILOVER_MSG) {
1759                 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
1760                 goto exit;
1761         }
1762         if (bearer_id >= MAX_BEARERS)
1763                 goto exit;
1764
1765         if (bearer_id == link->bearer_id)
1766                 goto exit;
1767
1768         pl = link->owner->links[bearer_id];
1769         if (pl && tipc_link_is_up(pl))
1770                 tipc_link_reset(pl);
1771
1772         if (link->failover_pkts == FIRST_FAILOVER)
1773                 link->failover_pkts = msg_msgcnt(msg);
1774
1775         /* Should we expect an inner packet? */
1776         if (!link->failover_pkts)
1777                 goto exit;
1778
1779         if (!tipc_msg_extract(*skb, &iskb, &pos)) {
1780                 pr_warn("%sno inner failover pkt\n", link_co_err);
1781                 *skb = NULL;
1782                 goto exit;
1783         }
1784         link->failover_pkts--;
1785         *skb = NULL;
1786
1787         /* Was this packet already delivered? */
1788         if (less(buf_seqno(iskb), link->failover_checkpt)) {
1789                 kfree_skb(iskb);
1790                 iskb = NULL;
1791                 goto exit;
1792         }
1793         if (msg_user(buf_msg(iskb)) == MSG_FRAGMENTER) {
1794                 link->stats.recv_fragments++;
1795                 tipc_buf_append(&link->failover_skb, &iskb);
1796         }
1797 exit:
1798         if (!link->failover_pkts && pl)
1799                 pl->flags &= ~LINK_FAILINGOVER;
1800         kfree_skb(*skb);
1801         *skb = iskb;
1802         return *skb;
1803 }
1804
1805 static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
1806 {
1807         unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
1808
1809         if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
1810                 return;
1811
1812         l_ptr->tolerance = tol;
1813         l_ptr->cont_intv = msecs_to_jiffies(intv);
1814         l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
1815 }
1816
1817 void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
1818 {
1819         int max_bulk = TIPC_MAX_PUBLICATIONS / (l->max_pkt / ITEM_SIZE);
1820
1821         l->window = win;
1822         l->backlog[TIPC_LOW_IMPORTANCE].limit      = win / 2;
1823         l->backlog[TIPC_MEDIUM_IMPORTANCE].limit   = win;
1824         l->backlog[TIPC_HIGH_IMPORTANCE].limit     = win / 2 * 3;
1825         l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
1826         l->backlog[TIPC_SYSTEM_IMPORTANCE].limit   = max_bulk;
1827 }
1828
1829 /* tipc_link_find_owner - locate owner node of link by link's name
1830  * @net: the applicable net namespace
1831  * @name: pointer to link name string
1832  * @bearer_id: pointer to index in 'node->links' array where the link was found.
1833  *
1834  * Returns pointer to node owning the link, or 0 if no matching link is found.
1835  */
1836 static struct tipc_node *tipc_link_find_owner(struct net *net,
1837                                               const char *link_name,
1838                                               unsigned int *bearer_id)
1839 {
1840         struct tipc_net *tn = net_generic(net, tipc_net_id);
1841         struct tipc_link *l_ptr;
1842         struct tipc_node *n_ptr;
1843         struct tipc_node *found_node = NULL;
1844         int i;
1845
1846         *bearer_id = 0;
1847         rcu_read_lock();
1848         list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
1849                 tipc_node_lock(n_ptr);
1850                 for (i = 0; i < MAX_BEARERS; i++) {
1851                         l_ptr = n_ptr->links[i];
1852                         if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1853                                 *bearer_id = i;
1854                                 found_node = n_ptr;
1855                                 break;
1856                         }
1857                 }
1858                 tipc_node_unlock(n_ptr);
1859                 if (found_node)
1860                         break;
1861         }
1862         rcu_read_unlock();
1863
1864         return found_node;
1865 }
1866
1867 /**
1868  * link_reset_statistics - reset link statistics
1869  * @l_ptr: pointer to link
1870  */
1871 static void link_reset_statistics(struct tipc_link *l_ptr)
1872 {
1873         memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
1874         l_ptr->stats.sent_info = l_ptr->next_out_no;
1875         l_ptr->stats.recv_info = l_ptr->next_in_no;
1876 }
1877
1878 static void link_print(struct tipc_link *l_ptr, const char *str)
1879 {
1880         struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
1881         struct tipc_bearer *b_ptr;
1882
1883         rcu_read_lock();
1884         b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
1885         if (b_ptr)
1886                 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
1887         rcu_read_unlock();
1888
1889         if (link_working_unknown(l_ptr))
1890                 pr_cont(":WU\n");
1891         else if (link_reset_reset(l_ptr))
1892                 pr_cont(":RR\n");
1893         else if (link_reset_unknown(l_ptr))
1894                 pr_cont(":RU\n");
1895         else if (link_working_working(l_ptr))
1896                 pr_cont(":WW\n");
1897         else
1898                 pr_cont("\n");
1899 }
1900
1901 /* Parse and validate nested (link) properties valid for media, bearer and link
1902  */
1903 int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
1904 {
1905         int err;
1906
1907         err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
1908                                tipc_nl_prop_policy);
1909         if (err)
1910                 return err;
1911
1912         if (props[TIPC_NLA_PROP_PRIO]) {
1913                 u32 prio;
1914
1915                 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1916                 if (prio > TIPC_MAX_LINK_PRI)
1917                         return -EINVAL;
1918         }
1919
1920         if (props[TIPC_NLA_PROP_TOL]) {
1921                 u32 tol;
1922
1923                 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1924                 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
1925                         return -EINVAL;
1926         }
1927
1928         if (props[TIPC_NLA_PROP_WIN]) {
1929                 u32 win;
1930
1931                 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1932                 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
1933                         return -EINVAL;
1934         }
1935
1936         return 0;
1937 }
1938
1939 int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
1940 {
1941         int err;
1942         int res = 0;
1943         int bearer_id;
1944         char *name;
1945         struct tipc_link *link;
1946         struct tipc_node *node;
1947         struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
1948         struct net *net = sock_net(skb->sk);
1949
1950         if (!info->attrs[TIPC_NLA_LINK])
1951                 return -EINVAL;
1952
1953         err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1954                                info->attrs[TIPC_NLA_LINK],
1955                                tipc_nl_link_policy);
1956         if (err)
1957                 return err;
1958
1959         if (!attrs[TIPC_NLA_LINK_NAME])
1960                 return -EINVAL;
1961
1962         name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1963
1964         node = tipc_link_find_owner(net, name, &bearer_id);
1965         if (!node)
1966                 return -EINVAL;
1967
1968         tipc_node_lock(node);
1969
1970         link = node->links[bearer_id];
1971         if (!link) {
1972                 res = -EINVAL;
1973                 goto out;
1974         }
1975
1976         if (attrs[TIPC_NLA_LINK_PROP]) {
1977                 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1978
1979                 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1980                                               props);
1981                 if (err) {
1982                         res = err;
1983                         goto out;
1984                 }
1985
1986                 if (props[TIPC_NLA_PROP_TOL]) {
1987                         u32 tol;
1988
1989                         tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1990                         link_set_supervision_props(link, tol);
1991                         tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
1992                 }
1993                 if (props[TIPC_NLA_PROP_PRIO]) {
1994                         u32 prio;
1995
1996                         prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1997                         link->priority = prio;
1998                         tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
1999                 }
2000                 if (props[TIPC_NLA_PROP_WIN]) {
2001                         u32 win;
2002
2003                         win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2004                         tipc_link_set_queue_limits(link, win);
2005                 }
2006         }
2007
2008 out:
2009         tipc_node_unlock(node);
2010
2011         return res;
2012 }
2013
2014 static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
2015 {
2016         int i;
2017         struct nlattr *stats;
2018
2019         struct nla_map {
2020                 u32 key;
2021                 u32 val;
2022         };
2023
2024         struct nla_map map[] = {
2025                 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2026                 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2027                 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2028                 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2029                 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2030                 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2031                 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2032                 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2033                 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2034                 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2035                 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2036                         s->msg_length_counts : 1},
2037                 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2038                 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2039                 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2040                 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2041                 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2042                 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2043                 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2044                 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2045                 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2046                 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2047                 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2048                 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2049                 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2050                 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2051                 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2052                 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2053                 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2054                 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2055                 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2056                 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2057                 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2058                 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2059                         (s->accu_queue_sz / s->queue_sz_counts) : 0}
2060         };
2061
2062         stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2063         if (!stats)
2064                 return -EMSGSIZE;
2065
2066         for (i = 0; i <  ARRAY_SIZE(map); i++)
2067                 if (nla_put_u32(skb, map[i].key, map[i].val))
2068                         goto msg_full;
2069
2070         nla_nest_end(skb, stats);
2071
2072         return 0;
2073 msg_full:
2074         nla_nest_cancel(skb, stats);
2075
2076         return -EMSGSIZE;
2077 }
2078
2079 /* Caller should hold appropriate locks to protect the link */
2080 static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2081                               struct tipc_link *link)
2082 {
2083         int err;
2084         void *hdr;
2085         struct nlattr *attrs;
2086         struct nlattr *prop;
2087         struct tipc_net *tn = net_generic(net, tipc_net_id);
2088
2089         hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2090                           NLM_F_MULTI, TIPC_NL_LINK_GET);
2091         if (!hdr)
2092                 return -EMSGSIZE;
2093
2094         attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2095         if (!attrs)
2096                 goto msg_full;
2097
2098         if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2099                 goto attr_msg_full;
2100         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2101                         tipc_cluster_mask(tn->own_addr)))
2102                 goto attr_msg_full;
2103         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2104                 goto attr_msg_full;
2105         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2106                 goto attr_msg_full;
2107         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2108                 goto attr_msg_full;
2109
2110         if (tipc_link_is_up(link))
2111                 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2112                         goto attr_msg_full;
2113         if (tipc_link_is_active(link))
2114                 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2115                         goto attr_msg_full;
2116
2117         prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2118         if (!prop)
2119                 goto attr_msg_full;
2120         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2121                 goto prop_msg_full;
2122         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2123                 goto prop_msg_full;
2124         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2125                         link->window))
2126                 goto prop_msg_full;
2127         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2128                 goto prop_msg_full;
2129         nla_nest_end(msg->skb, prop);
2130
2131         err = __tipc_nl_add_stats(msg->skb, &link->stats);
2132         if (err)
2133                 goto attr_msg_full;
2134
2135         nla_nest_end(msg->skb, attrs);
2136         genlmsg_end(msg->skb, hdr);
2137
2138         return 0;
2139
2140 prop_msg_full:
2141         nla_nest_cancel(msg->skb, prop);
2142 attr_msg_full:
2143         nla_nest_cancel(msg->skb, attrs);
2144 msg_full:
2145         genlmsg_cancel(msg->skb, hdr);
2146
2147         return -EMSGSIZE;
2148 }
2149
2150 /* Caller should hold node lock  */
2151 static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2152                                     struct tipc_node *node, u32 *prev_link)
2153 {
2154         u32 i;
2155         int err;
2156
2157         for (i = *prev_link; i < MAX_BEARERS; i++) {
2158                 *prev_link = i;
2159
2160                 if (!node->links[i])
2161                         continue;
2162
2163                 err = __tipc_nl_add_link(net, msg, node->links[i]);
2164                 if (err)
2165                         return err;
2166         }
2167         *prev_link = 0;
2168
2169         return 0;
2170 }
2171
2172 int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2173 {
2174         struct net *net = sock_net(skb->sk);
2175         struct tipc_net *tn = net_generic(net, tipc_net_id);
2176         struct tipc_node *node;
2177         struct tipc_nl_msg msg;
2178         u32 prev_node = cb->args[0];
2179         u32 prev_link = cb->args[1];
2180         int done = cb->args[2];
2181         int err;
2182
2183         if (done)
2184                 return 0;
2185
2186         msg.skb = skb;
2187         msg.portid = NETLINK_CB(cb->skb).portid;
2188         msg.seq = cb->nlh->nlmsg_seq;
2189
2190         rcu_read_lock();
2191         if (prev_node) {
2192                 node = tipc_node_find(net, prev_node);
2193                 if (!node) {
2194                         /* We never set seq or call nl_dump_check_consistent()
2195                          * this means that setting prev_seq here will cause the
2196                          * consistence check to fail in the netlink callback
2197                          * handler. Resulting in the last NLMSG_DONE message
2198                          * having the NLM_F_DUMP_INTR flag set.
2199                          */
2200                         cb->prev_seq = 1;
2201                         goto out;
2202                 }
2203                 tipc_node_put(node);
2204
2205                 list_for_each_entry_continue_rcu(node, &tn->node_list,
2206                                                  list) {
2207                         tipc_node_lock(node);
2208                         err = __tipc_nl_add_node_links(net, &msg, node,
2209                                                        &prev_link);
2210                         tipc_node_unlock(node);
2211                         tipc_node_put(node);
2212                         if (err)
2213                                 goto out;
2214
2215                         prev_node = node->addr;
2216                 }
2217         } else {
2218                 err = tipc_nl_add_bc_link(net, &msg);
2219                 if (err)
2220                         goto out;
2221
2222                 list_for_each_entry_rcu(node, &tn->node_list, list) {
2223                         tipc_node_lock(node);
2224                         err = __tipc_nl_add_node_links(net, &msg, node,
2225                                                        &prev_link);
2226                         tipc_node_unlock(node);
2227                         if (err)
2228                                 goto out;
2229
2230                         prev_node = node->addr;
2231                 }
2232         }
2233         done = 1;
2234 out:
2235         rcu_read_unlock();
2236
2237         cb->args[0] = prev_node;
2238         cb->args[1] = prev_link;
2239         cb->args[2] = done;
2240
2241         return skb->len;
2242 }
2243
2244 int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2245 {
2246         struct net *net = genl_info_net(info);
2247         struct sk_buff *ans_skb;
2248         struct tipc_nl_msg msg;
2249         struct tipc_link *link;
2250         struct tipc_node *node;
2251         char *name;
2252         int bearer_id;
2253         int err;
2254
2255         if (!info->attrs[TIPC_NLA_LINK_NAME])
2256                 return -EINVAL;
2257
2258         name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
2259         node = tipc_link_find_owner(net, name, &bearer_id);
2260         if (!node)
2261                 return -EINVAL;
2262
2263         ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2264         if (!ans_skb)
2265                 return -ENOMEM;
2266
2267         msg.skb = ans_skb;
2268         msg.portid = info->snd_portid;
2269         msg.seq = info->snd_seq;
2270
2271         tipc_node_lock(node);
2272         link = node->links[bearer_id];
2273         if (!link) {
2274                 err = -EINVAL;
2275                 goto err_out;
2276         }
2277
2278         err = __tipc_nl_add_link(net, &msg, link);
2279         if (err)
2280                 goto err_out;
2281
2282         tipc_node_unlock(node);
2283
2284         return genlmsg_reply(ans_skb, info);
2285
2286 err_out:
2287         tipc_node_unlock(node);
2288         nlmsg_free(ans_skb);
2289
2290         return err;
2291 }
2292
2293 int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2294 {
2295         int err;
2296         char *link_name;
2297         unsigned int bearer_id;
2298         struct tipc_link *link;
2299         struct tipc_node *node;
2300         struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2301         struct net *net = sock_net(skb->sk);
2302
2303         if (!info->attrs[TIPC_NLA_LINK])
2304                 return -EINVAL;
2305
2306         err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2307                                info->attrs[TIPC_NLA_LINK],
2308                                tipc_nl_link_policy);
2309         if (err)
2310                 return err;
2311
2312         if (!attrs[TIPC_NLA_LINK_NAME])
2313                 return -EINVAL;
2314
2315         link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2316
2317         if (strcmp(link_name, tipc_bclink_name) == 0) {
2318                 err = tipc_bclink_reset_stats(net);
2319                 if (err)
2320                         return err;
2321                 return 0;
2322         }
2323
2324         node = tipc_link_find_owner(net, link_name, &bearer_id);
2325         if (!node)
2326                 return -EINVAL;
2327
2328         tipc_node_lock(node);
2329
2330         link = node->links[bearer_id];
2331         if (!link) {
2332                 tipc_node_unlock(node);
2333                 return -EINVAL;
2334         }
2335
2336         link_reset_statistics(link);
2337
2338         tipc_node_unlock(node);
2339
2340         return 0;
2341 }