tipc: make name table distributor use new send function
[cascardo/linux.git] / net / tipc / link.c
1 /*
2  * net/tipc/link.c: TIPC link code
3  *
4  * Copyright (c) 1996-2007, 2012-2014, 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 "link.h"
39 #include "port.h"
40 #include "socket.h"
41 #include "name_distr.h"
42 #include "discover.h"
43 #include "config.h"
44
45 #include <linux/pkt_sched.h>
46
47 /*
48  * Error message prefixes
49  */
50 static const char *link_co_err = "Link changeover error, ";
51 static const char *link_rst_msg = "Resetting link ";
52 static const char *link_unk_evt = "Unknown link event ";
53
54 /*
55  * Out-of-range value for link session numbers
56  */
57 #define INVALID_SESSION 0x10000
58
59 /*
60  * Link state events:
61  */
62 #define  STARTING_EVT    856384768      /* link processing trigger */
63 #define  TRAFFIC_MSG_EVT 560815u        /* rx'd ??? */
64 #define  TIMEOUT_EVT     560817u        /* link timer expired */
65
66 /*
67  * The following two 'message types' is really just implementation
68  * data conveniently stored in the message header.
69  * They must not be considered part of the protocol
70  */
71 #define OPEN_MSG   0
72 #define CLOSED_MSG 1
73
74 /*
75  * State value stored in 'exp_msg_count'
76  */
77 #define START_CHANGEOVER 100000u
78
79 static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
80                                        struct sk_buff *buf);
81 static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf);
82 static int  tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
83                                  struct sk_buff **buf);
84 static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
85 static void link_state_event(struct tipc_link *l_ptr, u32 event);
86 static void link_reset_statistics(struct tipc_link *l_ptr);
87 static void link_print(struct tipc_link *l_ptr, const char *str);
88 static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf);
89 static void tipc_link_sync_xmit(struct tipc_link *l);
90 static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
91 static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf);
92 static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf);
93
94 /*
95  *  Simple link routines
96  */
97 static unsigned int align(unsigned int i)
98 {
99         return (i + 3) & ~3u;
100 }
101
102 static void link_init_max_pkt(struct tipc_link *l_ptr)
103 {
104         struct tipc_bearer *b_ptr;
105         u32 max_pkt;
106
107         rcu_read_lock();
108         b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
109         if (!b_ptr) {
110                 rcu_read_unlock();
111                 return;
112         }
113         max_pkt = (b_ptr->mtu & ~3);
114         rcu_read_unlock();
115
116         if (max_pkt > MAX_MSG_SIZE)
117                 max_pkt = MAX_MSG_SIZE;
118
119         l_ptr->max_pkt_target = max_pkt;
120         if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
121                 l_ptr->max_pkt = l_ptr->max_pkt_target;
122         else
123                 l_ptr->max_pkt = MAX_PKT_DEFAULT;
124
125         l_ptr->max_pkt_probes = 0;
126 }
127
128 static u32 link_next_sent(struct tipc_link *l_ptr)
129 {
130         if (l_ptr->next_out)
131                 return buf_seqno(l_ptr->next_out);
132         return mod(l_ptr->next_out_no);
133 }
134
135 static u32 link_last_sent(struct tipc_link *l_ptr)
136 {
137         return mod(link_next_sent(l_ptr) - 1);
138 }
139
140 /*
141  *  Simple non-static link routines (i.e. referenced outside this file)
142  */
143 int tipc_link_is_up(struct tipc_link *l_ptr)
144 {
145         if (!l_ptr)
146                 return 0;
147         return link_working_working(l_ptr) || link_working_unknown(l_ptr);
148 }
149
150 int tipc_link_is_active(struct tipc_link *l_ptr)
151 {
152         return  (l_ptr->owner->active_links[0] == l_ptr) ||
153                 (l_ptr->owner->active_links[1] == l_ptr);
154 }
155
156 /**
157  * link_timeout - handle expiration of link timer
158  * @l_ptr: pointer to link
159  */
160 static void link_timeout(struct tipc_link *l_ptr)
161 {
162         tipc_node_lock(l_ptr->owner);
163
164         /* update counters used in statistical profiling of send traffic */
165         l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
166         l_ptr->stats.queue_sz_counts++;
167
168         if (l_ptr->first_out) {
169                 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
170                 u32 length = msg_size(msg);
171
172                 if ((msg_user(msg) == MSG_FRAGMENTER) &&
173                     (msg_type(msg) == FIRST_FRAGMENT)) {
174                         length = msg_size(msg_get_wrapped(msg));
175                 }
176                 if (length) {
177                         l_ptr->stats.msg_lengths_total += length;
178                         l_ptr->stats.msg_length_counts++;
179                         if (length <= 64)
180                                 l_ptr->stats.msg_length_profile[0]++;
181                         else if (length <= 256)
182                                 l_ptr->stats.msg_length_profile[1]++;
183                         else if (length <= 1024)
184                                 l_ptr->stats.msg_length_profile[2]++;
185                         else if (length <= 4096)
186                                 l_ptr->stats.msg_length_profile[3]++;
187                         else if (length <= 16384)
188                                 l_ptr->stats.msg_length_profile[4]++;
189                         else if (length <= 32768)
190                                 l_ptr->stats.msg_length_profile[5]++;
191                         else
192                                 l_ptr->stats.msg_length_profile[6]++;
193                 }
194         }
195
196         /* do all other link processing performed on a periodic basis */
197
198         link_state_event(l_ptr, TIMEOUT_EVT);
199
200         if (l_ptr->next_out)
201                 tipc_link_push_queue(l_ptr);
202
203         tipc_node_unlock(l_ptr->owner);
204 }
205
206 static void link_set_timer(struct tipc_link *l_ptr, u32 time)
207 {
208         k_start_timer(&l_ptr->timer, time);
209 }
210
211 /**
212  * tipc_link_create - create a new link
213  * @n_ptr: pointer to associated node
214  * @b_ptr: pointer to associated bearer
215  * @media_addr: media address to use when sending messages over link
216  *
217  * Returns pointer to link.
218  */
219 struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
220                                    struct tipc_bearer *b_ptr,
221                                    const struct tipc_media_addr *media_addr)
222 {
223         struct tipc_link *l_ptr;
224         struct tipc_msg *msg;
225         char *if_name;
226         char addr_string[16];
227         u32 peer = n_ptr->addr;
228
229         if (n_ptr->link_cnt >= 2) {
230                 tipc_addr_string_fill(addr_string, n_ptr->addr);
231                 pr_err("Attempt to establish third link to %s\n", addr_string);
232                 return NULL;
233         }
234
235         if (n_ptr->links[b_ptr->identity]) {
236                 tipc_addr_string_fill(addr_string, n_ptr->addr);
237                 pr_err("Attempt to establish second link on <%s> to %s\n",
238                        b_ptr->name, addr_string);
239                 return NULL;
240         }
241
242         l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
243         if (!l_ptr) {
244                 pr_warn("Link creation failed, no memory\n");
245                 return NULL;
246         }
247
248         l_ptr->addr = peer;
249         if_name = strchr(b_ptr->name, ':') + 1;
250         sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
251                 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
252                 tipc_node(tipc_own_addr),
253                 if_name,
254                 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
255                 /* note: peer i/f name is updated by reset/activate message */
256         memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
257         l_ptr->owner = n_ptr;
258         l_ptr->checkpoint = 1;
259         l_ptr->peer_session = INVALID_SESSION;
260         l_ptr->bearer_id = b_ptr->identity;
261         link_set_supervision_props(l_ptr, b_ptr->tolerance);
262         l_ptr->state = RESET_UNKNOWN;
263
264         l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
265         msg = l_ptr->pmsg;
266         tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
267         msg_set_size(msg, sizeof(l_ptr->proto_msg));
268         msg_set_session(msg, (tipc_random & 0xffff));
269         msg_set_bearer_id(msg, b_ptr->identity);
270         strcpy((char *)msg_data(msg), if_name);
271
272         l_ptr->priority = b_ptr->priority;
273         tipc_link_set_queue_limits(l_ptr, b_ptr->window);
274
275         l_ptr->net_plane = b_ptr->net_plane;
276         link_init_max_pkt(l_ptr);
277
278         l_ptr->next_out_no = 1;
279         INIT_LIST_HEAD(&l_ptr->waiting_ports);
280
281         link_reset_statistics(l_ptr);
282
283         tipc_node_attach_link(n_ptr, l_ptr);
284
285         k_init_timer(&l_ptr->timer, (Handler)link_timeout,
286                      (unsigned long)l_ptr);
287
288         link_state_event(l_ptr, STARTING_EVT);
289
290         return l_ptr;
291 }
292
293 void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
294 {
295         struct tipc_link *l_ptr;
296         struct tipc_node *n_ptr;
297
298         rcu_read_lock();
299         list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
300                 tipc_node_lock(n_ptr);
301                 l_ptr = n_ptr->links[bearer_id];
302                 if (l_ptr) {
303                         tipc_link_reset(l_ptr);
304                         if (shutting_down || !tipc_node_is_up(n_ptr)) {
305                                 tipc_node_detach_link(l_ptr->owner, l_ptr);
306                                 tipc_link_reset_fragments(l_ptr);
307                                 tipc_node_unlock(n_ptr);
308
309                                 /* Nobody else can access this link now: */
310                                 del_timer_sync(&l_ptr->timer);
311                                 kfree(l_ptr);
312                         } else {
313                                 /* Detach/delete when failover is finished: */
314                                 l_ptr->flags |= LINK_STOPPED;
315                                 tipc_node_unlock(n_ptr);
316                                 del_timer_sync(&l_ptr->timer);
317                         }
318                         continue;
319                 }
320                 tipc_node_unlock(n_ptr);
321         }
322         rcu_read_unlock();
323 }
324
325 /**
326  * link_schedule_port - schedule port for deferred sending
327  * @l_ptr: pointer to link
328  * @origport: reference to sending port
329  * @sz: amount of data to be sent
330  *
331  * Schedules port for renewed sending of messages after link congestion
332  * has abated.
333  */
334 static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
335 {
336         struct tipc_port *p_ptr;
337         struct tipc_sock *tsk;
338
339         spin_lock_bh(&tipc_port_list_lock);
340         p_ptr = tipc_port_lock(origport);
341         if (p_ptr) {
342                 if (!list_empty(&p_ptr->wait_list))
343                         goto exit;
344                 tsk = tipc_port_to_sock(p_ptr);
345                 tsk->link_cong = 1;
346                 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
347                 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
348                 l_ptr->stats.link_congs++;
349 exit:
350                 tipc_port_unlock(p_ptr);
351         }
352         spin_unlock_bh(&tipc_port_list_lock);
353         return -ELINKCONG;
354 }
355
356 void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
357 {
358         struct tipc_port *p_ptr;
359         struct tipc_sock *tsk;
360         struct tipc_port *temp_p_ptr;
361         int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
362
363         if (all)
364                 win = 100000;
365         if (win <= 0)
366                 return;
367         if (!spin_trylock_bh(&tipc_port_list_lock))
368                 return;
369         if (link_congested(l_ptr))
370                 goto exit;
371         list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
372                                  wait_list) {
373                 if (win <= 0)
374                         break;
375                 tsk = tipc_port_to_sock(p_ptr);
376                 list_del_init(&p_ptr->wait_list);
377                 spin_lock_bh(p_ptr->lock);
378                 tsk->link_cong = 0;
379                 tipc_sock_wakeup(tsk);
380                 win -= p_ptr->waiting_pkts;
381                 spin_unlock_bh(p_ptr->lock);
382         }
383
384 exit:
385         spin_unlock_bh(&tipc_port_list_lock);
386 }
387
388 /**
389  * link_release_outqueue - purge link's outbound message queue
390  * @l_ptr: pointer to link
391  */
392 static void link_release_outqueue(struct tipc_link *l_ptr)
393 {
394         kfree_skb_list(l_ptr->first_out);
395         l_ptr->first_out = NULL;
396         l_ptr->out_queue_size = 0;
397 }
398
399 /**
400  * tipc_link_reset_fragments - purge link's inbound message fragments queue
401  * @l_ptr: pointer to link
402  */
403 void tipc_link_reset_fragments(struct tipc_link *l_ptr)
404 {
405         kfree_skb(l_ptr->reasm_buf);
406         l_ptr->reasm_buf = NULL;
407 }
408
409 /**
410  * tipc_link_purge_queues - purge all pkt queues associated with link
411  * @l_ptr: pointer to link
412  */
413 void tipc_link_purge_queues(struct tipc_link *l_ptr)
414 {
415         kfree_skb_list(l_ptr->oldest_deferred_in);
416         kfree_skb_list(l_ptr->first_out);
417         tipc_link_reset_fragments(l_ptr);
418         kfree_skb(l_ptr->proto_msg_queue);
419         l_ptr->proto_msg_queue = NULL;
420 }
421
422 void tipc_link_reset(struct tipc_link *l_ptr)
423 {
424         u32 prev_state = l_ptr->state;
425         u32 checkpoint = l_ptr->next_in_no;
426         int was_active_link = tipc_link_is_active(l_ptr);
427
428         msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
429
430         /* Link is down, accept any session */
431         l_ptr->peer_session = INVALID_SESSION;
432
433         /* Prepare for max packet size negotiation */
434         link_init_max_pkt(l_ptr);
435
436         l_ptr->state = RESET_UNKNOWN;
437
438         if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
439                 return;
440
441         tipc_node_link_down(l_ptr->owner, l_ptr);
442         tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
443
444         if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
445                 l_ptr->reset_checkpoint = checkpoint;
446                 l_ptr->exp_msg_count = START_CHANGEOVER;
447         }
448
449         /* Clean up all queues: */
450         link_release_outqueue(l_ptr);
451         kfree_skb(l_ptr->proto_msg_queue);
452         l_ptr->proto_msg_queue = NULL;
453         kfree_skb_list(l_ptr->oldest_deferred_in);
454         if (!list_empty(&l_ptr->waiting_ports))
455                 tipc_link_wakeup_ports(l_ptr, 1);
456
457         l_ptr->retransm_queue_head = 0;
458         l_ptr->retransm_queue_size = 0;
459         l_ptr->last_out = NULL;
460         l_ptr->first_out = NULL;
461         l_ptr->next_out = NULL;
462         l_ptr->unacked_window = 0;
463         l_ptr->checkpoint = 1;
464         l_ptr->next_out_no = 1;
465         l_ptr->deferred_inqueue_sz = 0;
466         l_ptr->oldest_deferred_in = NULL;
467         l_ptr->newest_deferred_in = NULL;
468         l_ptr->fsm_msg_cnt = 0;
469         l_ptr->stale_count = 0;
470         link_reset_statistics(l_ptr);
471 }
472
473 void tipc_link_reset_list(unsigned int bearer_id)
474 {
475         struct tipc_link *l_ptr;
476         struct tipc_node *n_ptr;
477
478         rcu_read_lock();
479         list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
480                 tipc_node_lock(n_ptr);
481                 l_ptr = n_ptr->links[bearer_id];
482                 if (l_ptr)
483                         tipc_link_reset(l_ptr);
484                 tipc_node_unlock(n_ptr);
485         }
486         rcu_read_unlock();
487 }
488
489 static void link_activate(struct tipc_link *l_ptr)
490 {
491         l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
492         tipc_node_link_up(l_ptr->owner, l_ptr);
493         tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
494 }
495
496 /**
497  * link_state_event - link finite state machine
498  * @l_ptr: pointer to link
499  * @event: state machine event to process
500  */
501 static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
502 {
503         struct tipc_link *other;
504         u32 cont_intv = l_ptr->continuity_interval;
505
506         if (l_ptr->flags & LINK_STOPPED)
507                 return;
508
509         if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
510                 return;         /* Not yet. */
511
512         /* Check whether changeover is going on */
513         if (l_ptr->exp_msg_count) {
514                 if (event == TIMEOUT_EVT)
515                         link_set_timer(l_ptr, cont_intv);
516                 return;
517         }
518
519         switch (l_ptr->state) {
520         case WORKING_WORKING:
521                 switch (event) {
522                 case TRAFFIC_MSG_EVT:
523                 case ACTIVATE_MSG:
524                         break;
525                 case TIMEOUT_EVT:
526                         if (l_ptr->next_in_no != l_ptr->checkpoint) {
527                                 l_ptr->checkpoint = l_ptr->next_in_no;
528                                 if (tipc_bclink_acks_missing(l_ptr->owner)) {
529                                         tipc_link_proto_xmit(l_ptr, STATE_MSG,
530                                                              0, 0, 0, 0, 0);
531                                         l_ptr->fsm_msg_cnt++;
532                                 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
533                                         tipc_link_proto_xmit(l_ptr, STATE_MSG,
534                                                              1, 0, 0, 0, 0);
535                                         l_ptr->fsm_msg_cnt++;
536                                 }
537                                 link_set_timer(l_ptr, cont_intv);
538                                 break;
539                         }
540                         l_ptr->state = WORKING_UNKNOWN;
541                         l_ptr->fsm_msg_cnt = 0;
542                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
543                         l_ptr->fsm_msg_cnt++;
544                         link_set_timer(l_ptr, cont_intv / 4);
545                         break;
546                 case RESET_MSG:
547                         pr_info("%s<%s>, requested by peer\n", link_rst_msg,
548                                 l_ptr->name);
549                         tipc_link_reset(l_ptr);
550                         l_ptr->state = RESET_RESET;
551                         l_ptr->fsm_msg_cnt = 0;
552                         tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
553                                              0, 0, 0, 0, 0);
554                         l_ptr->fsm_msg_cnt++;
555                         link_set_timer(l_ptr, cont_intv);
556                         break;
557                 default:
558                         pr_err("%s%u in WW state\n", link_unk_evt, event);
559                 }
560                 break;
561         case WORKING_UNKNOWN:
562                 switch (event) {
563                 case TRAFFIC_MSG_EVT:
564                 case ACTIVATE_MSG:
565                         l_ptr->state = WORKING_WORKING;
566                         l_ptr->fsm_msg_cnt = 0;
567                         link_set_timer(l_ptr, cont_intv);
568                         break;
569                 case RESET_MSG:
570                         pr_info("%s<%s>, requested by peer while probing\n",
571                                 link_rst_msg, l_ptr->name);
572                         tipc_link_reset(l_ptr);
573                         l_ptr->state = RESET_RESET;
574                         l_ptr->fsm_msg_cnt = 0;
575                         tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
576                                              0, 0, 0, 0, 0);
577                         l_ptr->fsm_msg_cnt++;
578                         link_set_timer(l_ptr, cont_intv);
579                         break;
580                 case TIMEOUT_EVT:
581                         if (l_ptr->next_in_no != l_ptr->checkpoint) {
582                                 l_ptr->state = WORKING_WORKING;
583                                 l_ptr->fsm_msg_cnt = 0;
584                                 l_ptr->checkpoint = l_ptr->next_in_no;
585                                 if (tipc_bclink_acks_missing(l_ptr->owner)) {
586                                         tipc_link_proto_xmit(l_ptr, STATE_MSG,
587                                                              0, 0, 0, 0, 0);
588                                         l_ptr->fsm_msg_cnt++;
589                                 }
590                                 link_set_timer(l_ptr, cont_intv);
591                         } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
592                                 tipc_link_proto_xmit(l_ptr, STATE_MSG,
593                                                      1, 0, 0, 0, 0);
594                                 l_ptr->fsm_msg_cnt++;
595                                 link_set_timer(l_ptr, cont_intv / 4);
596                         } else {        /* Link has failed */
597                                 pr_warn("%s<%s>, peer not responding\n",
598                                         link_rst_msg, l_ptr->name);
599                                 tipc_link_reset(l_ptr);
600                                 l_ptr->state = RESET_UNKNOWN;
601                                 l_ptr->fsm_msg_cnt = 0;
602                                 tipc_link_proto_xmit(l_ptr, RESET_MSG,
603                                                      0, 0, 0, 0, 0);
604                                 l_ptr->fsm_msg_cnt++;
605                                 link_set_timer(l_ptr, cont_intv);
606                         }
607                         break;
608                 default:
609                         pr_err("%s%u in WU state\n", link_unk_evt, event);
610                 }
611                 break;
612         case RESET_UNKNOWN:
613                 switch (event) {
614                 case TRAFFIC_MSG_EVT:
615                         break;
616                 case ACTIVATE_MSG:
617                         other = l_ptr->owner->active_links[0];
618                         if (other && link_working_unknown(other))
619                                 break;
620                         l_ptr->state = WORKING_WORKING;
621                         l_ptr->fsm_msg_cnt = 0;
622                         link_activate(l_ptr);
623                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
624                         l_ptr->fsm_msg_cnt++;
625                         if (l_ptr->owner->working_links == 1)
626                                 tipc_link_sync_xmit(l_ptr);
627                         link_set_timer(l_ptr, cont_intv);
628                         break;
629                 case RESET_MSG:
630                         l_ptr->state = RESET_RESET;
631                         l_ptr->fsm_msg_cnt = 0;
632                         tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
633                                              1, 0, 0, 0, 0);
634                         l_ptr->fsm_msg_cnt++;
635                         link_set_timer(l_ptr, cont_intv);
636                         break;
637                 case STARTING_EVT:
638                         l_ptr->flags |= LINK_STARTED;
639                         /* fall through */
640                 case TIMEOUT_EVT:
641                         tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
642                         l_ptr->fsm_msg_cnt++;
643                         link_set_timer(l_ptr, cont_intv);
644                         break;
645                 default:
646                         pr_err("%s%u in RU state\n", link_unk_evt, event);
647                 }
648                 break;
649         case RESET_RESET:
650                 switch (event) {
651                 case TRAFFIC_MSG_EVT:
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                         break;
667                 case TIMEOUT_EVT:
668                         tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
669                                              0, 0, 0, 0, 0);
670                         l_ptr->fsm_msg_cnt++;
671                         link_set_timer(l_ptr, cont_intv);
672                         break;
673                 default:
674                         pr_err("%s%u in RR state\n", link_unk_evt, event);
675                 }
676                 break;
677         default:
678                 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
679         }
680 }
681
682 /*
683  * link_bundle_buf(): Append contents of a buffer to
684  * the tail of an existing one.
685  */
686 static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
687                            struct sk_buff *buf)
688 {
689         struct tipc_msg *bundler_msg = buf_msg(bundler);
690         struct tipc_msg *msg = buf_msg(buf);
691         u32 size = msg_size(msg);
692         u32 bundle_size = msg_size(bundler_msg);
693         u32 to_pos = align(bundle_size);
694         u32 pad = to_pos - bundle_size;
695
696         if (msg_user(bundler_msg) != MSG_BUNDLER)
697                 return 0;
698         if (msg_type(bundler_msg) != OPEN_MSG)
699                 return 0;
700         if (skb_tailroom(bundler) < (pad + size))
701                 return 0;
702         if (l_ptr->max_pkt < (to_pos + size))
703                 return 0;
704
705         skb_put(bundler, pad + size);
706         skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
707         msg_set_size(bundler_msg, to_pos + size);
708         msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
709         kfree_skb(buf);
710         l_ptr->stats.sent_bundled++;
711         return 1;
712 }
713
714 static void link_add_to_outqueue(struct tipc_link *l_ptr,
715                                  struct sk_buff *buf,
716                                  struct tipc_msg *msg)
717 {
718         u32 ack = mod(l_ptr->next_in_no - 1);
719         u32 seqno = mod(l_ptr->next_out_no++);
720
721         msg_set_word(msg, 2, ((ack << 16) | seqno));
722         msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
723         buf->next = NULL;
724         if (l_ptr->first_out) {
725                 l_ptr->last_out->next = buf;
726                 l_ptr->last_out = buf;
727         } else
728                 l_ptr->first_out = l_ptr->last_out = buf;
729
730         l_ptr->out_queue_size++;
731         if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
732                 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
733 }
734
735 static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
736                                        struct sk_buff *buf_chain,
737                                        u32 long_msgno)
738 {
739         struct sk_buff *buf;
740         struct tipc_msg *msg;
741
742         if (!l_ptr->next_out)
743                 l_ptr->next_out = buf_chain;
744         while (buf_chain) {
745                 buf = buf_chain;
746                 buf_chain = buf_chain->next;
747
748                 msg = buf_msg(buf);
749                 msg_set_long_msgno(msg, long_msgno);
750                 link_add_to_outqueue(l_ptr, buf, msg);
751         }
752 }
753
754 /*
755  * tipc_link_xmit() is the 'full path' for messages, called from
756  * inside TIPC when the 'fast path' in tipc_send_xmit
757  * has failed, and from link_send()
758  */
759 int __tipc_link_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
760 {
761         struct tipc_msg *msg = buf_msg(buf);
762         u32 size = msg_size(msg);
763         u32 dsz = msg_data_sz(msg);
764         u32 queue_size = l_ptr->out_queue_size;
765         u32 imp = tipc_msg_tot_importance(msg);
766         u32 queue_limit = l_ptr->queue_limit[imp];
767         u32 max_packet = l_ptr->max_pkt;
768
769         /* Match msg importance against queue limits: */
770         if (unlikely(queue_size >= queue_limit)) {
771                 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
772                         link_schedule_port(l_ptr, msg_origport(msg), size);
773                         kfree_skb(buf);
774                         return -ELINKCONG;
775                 }
776                 kfree_skb(buf);
777                 if (imp > CONN_MANAGER) {
778                         pr_warn("%s<%s>, send queue full", link_rst_msg,
779                                 l_ptr->name);
780                         tipc_link_reset(l_ptr);
781                 }
782                 return dsz;
783         }
784
785         /* Fragmentation needed ? */
786         if (size > max_packet)
787                 return tipc_link_frag_xmit(l_ptr, buf);
788
789         /* Packet can be queued or sent. */
790         if (likely(!link_congested(l_ptr))) {
791                 link_add_to_outqueue(l_ptr, buf, msg);
792
793                 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
794                 l_ptr->unacked_window = 0;
795                 return dsz;
796         }
797         /* Congestion: can message be bundled ? */
798         if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
799             (msg_user(msg) != MSG_FRAGMENTER)) {
800
801                 /* Try adding message to an existing bundle */
802                 if (l_ptr->next_out &&
803                     link_bundle_buf(l_ptr, l_ptr->last_out, buf))
804                         return dsz;
805
806                 /* Try creating a new bundle */
807                 if (size <= max_packet * 2 / 3) {
808                         struct sk_buff *bundler = tipc_buf_acquire(max_packet);
809                         struct tipc_msg bundler_hdr;
810
811                         if (bundler) {
812                                 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
813                                          INT_H_SIZE, l_ptr->addr);
814                                 skb_copy_to_linear_data(bundler, &bundler_hdr,
815                                                         INT_H_SIZE);
816                                 skb_trim(bundler, INT_H_SIZE);
817                                 link_bundle_buf(l_ptr, bundler, buf);
818                                 buf = bundler;
819                                 msg = buf_msg(buf);
820                                 l_ptr->stats.sent_bundles++;
821                         }
822                 }
823         }
824         if (!l_ptr->next_out)
825                 l_ptr->next_out = buf;
826         link_add_to_outqueue(l_ptr, buf, msg);
827         return dsz;
828 }
829
830 /*
831  * tipc_link_xmit(): same as __tipc_link_xmit(), but the link to use
832  * has not been selected yet, and the the owner node is not locked
833  * Called by TIPC internal users, e.g. the name distributor
834  */
835 int tipc_link_xmit(struct sk_buff *buf, u32 dest, u32 selector)
836 {
837         struct tipc_link *l_ptr;
838         struct tipc_node *n_ptr;
839         int res = -ELINKCONG;
840
841         n_ptr = tipc_node_find(dest);
842         if (n_ptr) {
843                 tipc_node_lock(n_ptr);
844                 l_ptr = n_ptr->active_links[selector & 1];
845                 if (l_ptr)
846                         res = __tipc_link_xmit(l_ptr, buf);
847                 else
848                         kfree_skb(buf);
849                 tipc_node_unlock(n_ptr);
850         } else {
851                 kfree_skb(buf);
852         }
853         return res;
854 }
855
856 /* tipc_link_cong: determine return value and how to treat the
857  * sent buffer during link congestion.
858  * - For plain, errorless user data messages we keep the buffer and
859  *   return -ELINKONG.
860  * - For all other messages we discard the buffer and return -EHOSTUNREACH
861  * - For TIPC internal messages we also reset the link
862  */
863 static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
864 {
865         struct tipc_msg *msg = buf_msg(buf);
866         uint psz = msg_size(msg);
867         uint imp = tipc_msg_tot_importance(msg);
868         u32 oport = msg_tot_origport(msg);
869
870         if (likely(imp <= TIPC_CRITICAL_IMPORTANCE)) {
871                 if (!msg_errcode(msg) && !msg_reroute_cnt(msg)) {
872                         link_schedule_port(link, oport, psz);
873                         return -ELINKCONG;
874                 }
875         } else {
876                 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
877                 tipc_link_reset(link);
878         }
879         kfree_skb_list(buf);
880         return -EHOSTUNREACH;
881 }
882
883 /**
884  * __tipc_link_xmit2(): same as tipc_link_xmit2, but destlink is known & locked
885  * @link: link to use
886  * @buf: chain of buffers containing message
887  * Consumes the buffer chain, except when returning -ELINKCONG
888  * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
889  * user data messages) or -EHOSTUNREACH (all other messages/senders)
890  * Only the socket functions tipc_send_stream() and tipc_send_packet() need
891  * to act on the return value, since they may need to do more send attempts.
892  */
893 int __tipc_link_xmit2(struct tipc_link *link, struct sk_buff *buf)
894 {
895         struct tipc_msg *msg = buf_msg(buf);
896         uint psz = msg_size(msg);
897         uint qsz = link->out_queue_size;
898         uint sndlim = link->queue_limit[0];
899         uint imp = tipc_msg_tot_importance(msg);
900         uint mtu = link->max_pkt;
901         uint ack = mod(link->next_in_no - 1);
902         uint seqno = link->next_out_no;
903         uint bc_last_in = link->owner->bclink.last_in;
904         struct tipc_media_addr *addr = &link->media_addr;
905         struct sk_buff *next = buf->next;
906
907         /* Match queue limits against msg importance: */
908         if (unlikely(qsz >= link->queue_limit[imp]))
909                 return tipc_link_cong(link, buf);
910
911         /* Has valid packet limit been used ? */
912         if (unlikely(psz > mtu)) {
913                 kfree_skb_list(buf);
914                 return -EMSGSIZE;
915         }
916
917         /* Prepare each packet for sending, and add to outqueue: */
918         while (buf) {
919                 next = buf->next;
920                 msg = buf_msg(buf);
921                 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
922                 msg_set_bcast_ack(msg, bc_last_in);
923
924                 if (!link->first_out) {
925                         link->first_out = buf;
926                 } else if (qsz < sndlim) {
927                         link->last_out->next = buf;
928                 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
929                         link->stats.sent_bundled++;
930                         buf = next;
931                         next = buf->next;
932                         continue;
933                 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
934                         link->stats.sent_bundled++;
935                         link->stats.sent_bundles++;
936                         link->last_out->next = buf;
937                         if (!link->next_out)
938                                 link->next_out = buf;
939                 } else {
940                         link->last_out->next = buf;
941                         if (!link->next_out)
942                                 link->next_out = buf;
943                 }
944
945                 /* Send packet if possible: */
946                 if (likely(++qsz <= sndlim)) {
947                         tipc_bearer_send(link->bearer_id, buf, addr);
948                         link->next_out = next;
949                         link->unacked_window = 0;
950                 }
951                 seqno++;
952                 link->last_out = buf;
953                 buf = next;
954         }
955         link->next_out_no = seqno;
956         link->out_queue_size = qsz;
957         return 0;
958 }
959
960 /**
961  * tipc_link_xmit2() is the general link level function for message sending
962  * @buf: chain of buffers containing message
963  * @dsz: amount of user data to be sent
964  * @dnode: address of destination node
965  * @selector: a number used for deterministic link selection
966  * Consumes the buffer chain, except when returning -ELINKCONG
967  * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
968  */
969 int tipc_link_xmit2(struct sk_buff *buf, u32 dnode, u32 selector)
970 {
971         struct tipc_link *link = NULL;
972         struct tipc_node *node;
973         int rc = -EHOSTUNREACH;
974
975         node = tipc_node_find(dnode);
976         if (node) {
977                 tipc_node_lock(node);
978                 link = node->active_links[selector & 1];
979                 if (link)
980                         rc = __tipc_link_xmit2(link, buf);
981                 tipc_node_unlock(node);
982         }
983
984         if (link)
985                 return rc;
986
987         if (likely(in_own_node(dnode)))
988                 return tipc_sk_rcv(buf);
989
990         kfree_skb_list(buf);
991         return rc;
992 }
993
994 /*
995  * tipc_link_sync_xmit - synchronize broadcast link endpoints.
996  *
997  * Give a newly added peer node the sequence number where it should
998  * start receiving and acking broadcast packets.
999  *
1000  * Called with node locked
1001  */
1002 static void tipc_link_sync_xmit(struct tipc_link *l)
1003 {
1004         struct sk_buff *buf;
1005         struct tipc_msg *msg;
1006
1007         buf = tipc_buf_acquire(INT_H_SIZE);
1008         if (!buf)
1009                 return;
1010
1011         msg = buf_msg(buf);
1012         tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
1013         msg_set_last_bcast(msg, l->owner->bclink.acked);
1014         link_add_chain_to_outqueue(l, buf, 0);
1015         tipc_link_push_queue(l);
1016 }
1017
1018 /*
1019  * tipc_link_sync_rcv - synchronize broadcast link endpoints.
1020  * Receive the sequence number where we should start receiving and
1021  * acking broadcast packets from a newly added peer node, and open
1022  * up for reception of such packets.
1023  *
1024  * Called with node locked
1025  */
1026 static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
1027 {
1028         struct tipc_msg *msg = buf_msg(buf);
1029
1030         n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
1031         n->bclink.recv_permitted = true;
1032         kfree_skb(buf);
1033 }
1034
1035 /*
1036  * tipc_link_push_packet: Push one unsent packet to the media
1037  */
1038 static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
1039 {
1040         struct sk_buff *buf = l_ptr->first_out;
1041         u32 r_q_size = l_ptr->retransm_queue_size;
1042         u32 r_q_head = l_ptr->retransm_queue_head;
1043
1044         /* Step to position where retransmission failed, if any,    */
1045         /* consider that buffers may have been released in meantime */
1046         if (r_q_size && buf) {
1047                 u32 last = lesser(mod(r_q_head + r_q_size),
1048                                   link_last_sent(l_ptr));
1049                 u32 first = buf_seqno(buf);
1050
1051                 while (buf && less(first, r_q_head)) {
1052                         first = mod(first + 1);
1053                         buf = buf->next;
1054                 }
1055                 l_ptr->retransm_queue_head = r_q_head = first;
1056                 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1057         }
1058
1059         /* Continue retransmission now, if there is anything: */
1060         if (r_q_size && buf) {
1061                 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1062                 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
1063                 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
1064                 l_ptr->retransm_queue_head = mod(++r_q_head);
1065                 l_ptr->retransm_queue_size = --r_q_size;
1066                 l_ptr->stats.retransmitted++;
1067                 return 0;
1068         }
1069
1070         /* Send deferred protocol message, if any: */
1071         buf = l_ptr->proto_msg_queue;
1072         if (buf) {
1073                 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1074                 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
1075                 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
1076                 l_ptr->unacked_window = 0;
1077                 kfree_skb(buf);
1078                 l_ptr->proto_msg_queue = NULL;
1079                 return 0;
1080         }
1081
1082         /* Send one deferred data message, if send window not full: */
1083         buf = l_ptr->next_out;
1084         if (buf) {
1085                 struct tipc_msg *msg = buf_msg(buf);
1086                 u32 next = msg_seqno(msg);
1087                 u32 first = buf_seqno(l_ptr->first_out);
1088
1089                 if (mod(next - first) < l_ptr->queue_limit[0]) {
1090                         msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1091                         msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1092                         tipc_bearer_send(l_ptr->bearer_id, buf,
1093                                          &l_ptr->media_addr);
1094                         if (msg_user(msg) == MSG_BUNDLER)
1095                                 msg_set_type(msg, BUNDLE_CLOSED);
1096                         l_ptr->next_out = buf->next;
1097                         return 0;
1098                 }
1099         }
1100         return 1;
1101 }
1102
1103 /*
1104  * push_queue(): push out the unsent messages of a link where
1105  *               congestion has abated. Node is locked
1106  */
1107 void tipc_link_push_queue(struct tipc_link *l_ptr)
1108 {
1109         u32 res;
1110
1111         do {
1112                 res = tipc_link_push_packet(l_ptr);
1113         } while (!res);
1114 }
1115
1116 void tipc_link_reset_all(struct tipc_node *node)
1117 {
1118         char addr_string[16];
1119         u32 i;
1120
1121         tipc_node_lock(node);
1122
1123         pr_warn("Resetting all links to %s\n",
1124                 tipc_addr_string_fill(addr_string, node->addr));
1125
1126         for (i = 0; i < MAX_BEARERS; i++) {
1127                 if (node->links[i]) {
1128                         link_print(node->links[i], "Resetting link\n");
1129                         tipc_link_reset(node->links[i]);
1130                 }
1131         }
1132
1133         tipc_node_unlock(node);
1134 }
1135
1136 static void link_retransmit_failure(struct tipc_link *l_ptr,
1137                                     struct sk_buff *buf)
1138 {
1139         struct tipc_msg *msg = buf_msg(buf);
1140
1141         pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
1142
1143         if (l_ptr->addr) {
1144                 /* Handle failure on standard link */
1145                 link_print(l_ptr, "Resetting link\n");
1146                 tipc_link_reset(l_ptr);
1147
1148         } else {
1149                 /* Handle failure on broadcast link */
1150                 struct tipc_node *n_ptr;
1151                 char addr_string[16];
1152
1153                 pr_info("Msg seq number: %u,  ", msg_seqno(msg));
1154                 pr_cont("Outstanding acks: %lu\n",
1155                         (unsigned long) TIPC_SKB_CB(buf)->handle);
1156
1157                 n_ptr = tipc_bclink_retransmit_to();
1158                 tipc_node_lock(n_ptr);
1159
1160                 tipc_addr_string_fill(addr_string, n_ptr->addr);
1161                 pr_info("Broadcast link info for %s\n", addr_string);
1162                 pr_info("Reception permitted: %d,  Acked: %u\n",
1163                         n_ptr->bclink.recv_permitted,
1164                         n_ptr->bclink.acked);
1165                 pr_info("Last in: %u,  Oos state: %u,  Last sent: %u\n",
1166                         n_ptr->bclink.last_in,
1167                         n_ptr->bclink.oos_state,
1168                         n_ptr->bclink.last_sent);
1169
1170                 tipc_node_unlock(n_ptr);
1171
1172                 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
1173                 l_ptr->stale_count = 0;
1174         }
1175 }
1176
1177 void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
1178                           u32 retransmits)
1179 {
1180         struct tipc_msg *msg;
1181
1182         if (!buf)
1183                 return;
1184
1185         msg = buf_msg(buf);
1186
1187         /* Detect repeated retransmit failures */
1188         if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1189                 if (++l_ptr->stale_count > 100) {
1190                         link_retransmit_failure(l_ptr, buf);
1191                         return;
1192                 }
1193         } else {
1194                 l_ptr->last_retransmitted = msg_seqno(msg);
1195                 l_ptr->stale_count = 1;
1196         }
1197
1198         while (retransmits && (buf != l_ptr->next_out) && buf) {
1199                 msg = buf_msg(buf);
1200                 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1201                 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1202                 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
1203                 buf = buf->next;
1204                 retransmits--;
1205                 l_ptr->stats.retransmitted++;
1206         }
1207
1208         l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1209 }
1210
1211 /**
1212  * link_insert_deferred_queue - insert deferred messages back into receive chain
1213  */
1214 static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
1215                                                   struct sk_buff *buf)
1216 {
1217         u32 seq_no;
1218
1219         if (l_ptr->oldest_deferred_in == NULL)
1220                 return buf;
1221
1222         seq_no = buf_seqno(l_ptr->oldest_deferred_in);
1223         if (seq_no == mod(l_ptr->next_in_no)) {
1224                 l_ptr->newest_deferred_in->next = buf;
1225                 buf = l_ptr->oldest_deferred_in;
1226                 l_ptr->oldest_deferred_in = NULL;
1227                 l_ptr->deferred_inqueue_sz = 0;
1228         }
1229         return buf;
1230 }
1231
1232 /**
1233  * link_recv_buf_validate - validate basic format of received message
1234  *
1235  * This routine ensures a TIPC message has an acceptable header, and at least
1236  * as much data as the header indicates it should.  The routine also ensures
1237  * that the entire message header is stored in the main fragment of the message
1238  * buffer, to simplify future access to message header fields.
1239  *
1240  * Note: Having extra info present in the message header or data areas is OK.
1241  * TIPC will ignore the excess, under the assumption that it is optional info
1242  * introduced by a later release of the protocol.
1243  */
1244 static int link_recv_buf_validate(struct sk_buff *buf)
1245 {
1246         static u32 min_data_hdr_size[8] = {
1247                 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
1248                 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1249                 };
1250
1251         struct tipc_msg *msg;
1252         u32 tipc_hdr[2];
1253         u32 size;
1254         u32 hdr_size;
1255         u32 min_hdr_size;
1256
1257         /* If this packet comes from the defer queue, the skb has already
1258          * been validated
1259          */
1260         if (unlikely(TIPC_SKB_CB(buf)->deferred))
1261                 return 1;
1262
1263         if (unlikely(buf->len < MIN_H_SIZE))
1264                 return 0;
1265
1266         msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1267         if (msg == NULL)
1268                 return 0;
1269
1270         if (unlikely(msg_version(msg) != TIPC_VERSION))
1271                 return 0;
1272
1273         size = msg_size(msg);
1274         hdr_size = msg_hdr_sz(msg);
1275         min_hdr_size = msg_isdata(msg) ?
1276                 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1277
1278         if (unlikely((hdr_size < min_hdr_size) ||
1279                      (size < hdr_size) ||
1280                      (buf->len < size) ||
1281                      (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1282                 return 0;
1283
1284         return pskb_may_pull(buf, hdr_size);
1285 }
1286
1287 /**
1288  * tipc_rcv - process TIPC packets/messages arriving from off-node
1289  * @head: pointer to message buffer chain
1290  * @b_ptr: pointer to bearer message arrived on
1291  *
1292  * Invoked with no locks held.  Bearer pointer must point to a valid bearer
1293  * structure (i.e. cannot be NULL), but bearer can be inactive.
1294  */
1295 void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
1296 {
1297         while (head) {
1298                 struct tipc_node *n_ptr;
1299                 struct tipc_link *l_ptr;
1300                 struct sk_buff *crs;
1301                 struct sk_buff *buf = head;
1302                 struct tipc_msg *msg;
1303                 u32 seq_no;
1304                 u32 ackd;
1305                 u32 released = 0;
1306
1307                 head = head->next;
1308                 buf->next = NULL;
1309
1310                 /* Ensure message is well-formed */
1311                 if (unlikely(!link_recv_buf_validate(buf)))
1312                         goto discard;
1313
1314                 /* Ensure message data is a single contiguous unit */
1315                 if (unlikely(skb_linearize(buf)))
1316                         goto discard;
1317
1318                 /* Handle arrival of a non-unicast link message */
1319                 msg = buf_msg(buf);
1320
1321                 if (unlikely(msg_non_seq(msg))) {
1322                         if (msg_user(msg) ==  LINK_CONFIG)
1323                                 tipc_disc_rcv(buf, b_ptr);
1324                         else
1325                                 tipc_bclink_rcv(buf);
1326                         continue;
1327                 }
1328
1329                 /* Discard unicast link messages destined for another node */
1330                 if (unlikely(!msg_short(msg) &&
1331                              (msg_destnode(msg) != tipc_own_addr)))
1332                         goto discard;
1333
1334                 /* Locate neighboring node that sent message */
1335                 n_ptr = tipc_node_find(msg_prevnode(msg));
1336                 if (unlikely(!n_ptr))
1337                         goto discard;
1338                 tipc_node_lock(n_ptr);
1339
1340                 /* Locate unicast link endpoint that should handle message */
1341                 l_ptr = n_ptr->links[b_ptr->identity];
1342                 if (unlikely(!l_ptr))
1343                         goto unlock_discard;
1344
1345                 /* Verify that communication with node is currently allowed */
1346                 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
1347                     msg_user(msg) == LINK_PROTOCOL &&
1348                     (msg_type(msg) == RESET_MSG ||
1349                     msg_type(msg) == ACTIVATE_MSG) &&
1350                     !msg_redundant_link(msg))
1351                         n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
1352
1353                 if (tipc_node_blocked(n_ptr))
1354                         goto unlock_discard;
1355
1356                 /* Validate message sequence number info */
1357                 seq_no = msg_seqno(msg);
1358                 ackd = msg_ack(msg);
1359
1360                 /* Release acked messages */
1361                 if (n_ptr->bclink.recv_permitted)
1362                         tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
1363
1364                 crs = l_ptr->first_out;
1365                 while ((crs != l_ptr->next_out) &&
1366                        less_eq(buf_seqno(crs), ackd)) {
1367                         struct sk_buff *next = crs->next;
1368                         kfree_skb(crs);
1369                         crs = next;
1370                         released++;
1371                 }
1372                 if (released) {
1373                         l_ptr->first_out = crs;
1374                         l_ptr->out_queue_size -= released;
1375                 }
1376
1377                 /* Try sending any messages link endpoint has pending */
1378                 if (unlikely(l_ptr->next_out))
1379                         tipc_link_push_queue(l_ptr);
1380
1381                 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
1382                         tipc_link_wakeup_ports(l_ptr, 0);
1383
1384                 /* Process the incoming packet */
1385                 if (unlikely(!link_working_working(l_ptr))) {
1386                         if (msg_user(msg) == LINK_PROTOCOL) {
1387                                 tipc_link_proto_rcv(l_ptr, buf);
1388                                 head = link_insert_deferred_queue(l_ptr, head);
1389                                 tipc_node_unlock(n_ptr);
1390                                 continue;
1391                         }
1392
1393                         /* Traffic message. Conditionally activate link */
1394                         link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1395
1396                         if (link_working_working(l_ptr)) {
1397                                 /* Re-insert buffer in front of queue */
1398                                 buf->next = head;
1399                                 head = buf;
1400                                 tipc_node_unlock(n_ptr);
1401                                 continue;
1402                         }
1403                         goto unlock_discard;
1404                 }
1405
1406                 /* Link is now in state WORKING_WORKING */
1407                 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
1408                         link_handle_out_of_seq_msg(l_ptr, buf);
1409                         head = link_insert_deferred_queue(l_ptr, head);
1410                         tipc_node_unlock(n_ptr);
1411                         continue;
1412                 }
1413                 l_ptr->next_in_no++;
1414                 if (unlikely(l_ptr->oldest_deferred_in))
1415                         head = link_insert_deferred_queue(l_ptr, head);
1416
1417                 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1418                         l_ptr->stats.sent_acks++;
1419                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1420                 }
1421
1422                 if (tipc_link_prepare_input(l_ptr, &buf)) {
1423                         tipc_node_unlock(n_ptr);
1424                         continue;
1425                 }
1426                 tipc_node_unlock(n_ptr);
1427                 msg = buf_msg(buf);
1428                 if (tipc_link_input(l_ptr, buf) != 0)
1429                         goto discard;
1430                 continue;
1431 unlock_discard:
1432                 tipc_node_unlock(n_ptr);
1433 discard:
1434                 kfree_skb(buf);
1435         }
1436 }
1437
1438 /**
1439  * tipc_link_prepare_input - process TIPC link messages
1440  *
1441  * returns nonzero if the message was consumed
1442  *
1443  * Node lock must be held
1444  */
1445 static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1446 {
1447         struct tipc_node *n;
1448         struct tipc_msg *msg;
1449         int res = -EINVAL;
1450
1451         n = l->owner;
1452         msg = buf_msg(*buf);
1453         switch (msg_user(msg)) {
1454         case CHANGEOVER_PROTOCOL:
1455                 if (tipc_link_tunnel_rcv(n, buf))
1456                         res = 0;
1457                 break;
1458         case MSG_FRAGMENTER:
1459                 l->stats.recv_fragments++;
1460                 if (tipc_buf_append(&l->reasm_buf, buf)) {
1461                         l->stats.recv_fragmented++;
1462                         res = 0;
1463                 } else if (!l->reasm_buf) {
1464                         tipc_link_reset(l);
1465                 }
1466                 break;
1467         case MSG_BUNDLER:
1468                 l->stats.recv_bundles++;
1469                 l->stats.recv_bundled += msg_msgcnt(msg);
1470                 res = 0;
1471                 break;
1472         case NAME_DISTRIBUTOR:
1473                 n->bclink.recv_permitted = true;
1474                 res = 0;
1475                 break;
1476         case BCAST_PROTOCOL:
1477                 tipc_link_sync_rcv(n, *buf);
1478                 break;
1479         default:
1480                 res = 0;
1481         }
1482         return res;
1483 }
1484 /**
1485  * tipc_link_input - Deliver message too higher layers
1486  */
1487 static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1488 {
1489         struct tipc_msg *msg = buf_msg(buf);
1490         int res = 0;
1491
1492         switch (msg_user(msg)) {
1493         case TIPC_LOW_IMPORTANCE:
1494         case TIPC_MEDIUM_IMPORTANCE:
1495         case TIPC_HIGH_IMPORTANCE:
1496         case TIPC_CRITICAL_IMPORTANCE:
1497         case CONN_MANAGER:
1498                 tipc_sk_rcv(buf);
1499                 break;
1500         case NAME_DISTRIBUTOR:
1501                 tipc_named_rcv(buf);
1502                 break;
1503         case MSG_BUNDLER:
1504                 tipc_link_bundle_rcv(buf);
1505                 break;
1506         default:
1507                 res = -EINVAL;
1508         }
1509         return res;
1510 }
1511
1512 /**
1513  * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1514  *
1515  * Returns increase in queue length (i.e. 0 or 1)
1516  */
1517 u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
1518                         struct sk_buff *buf)
1519 {
1520         struct sk_buff *queue_buf;
1521         struct sk_buff **prev;
1522         u32 seq_no = buf_seqno(buf);
1523
1524         buf->next = NULL;
1525
1526         /* Empty queue ? */
1527         if (*head == NULL) {
1528                 *head = *tail = buf;
1529                 return 1;
1530         }
1531
1532         /* Last ? */
1533         if (less(buf_seqno(*tail), seq_no)) {
1534                 (*tail)->next = buf;
1535                 *tail = buf;
1536                 return 1;
1537         }
1538
1539         /* Locate insertion point in queue, then insert; discard if duplicate */
1540         prev = head;
1541         queue_buf = *head;
1542         for (;;) {
1543                 u32 curr_seqno = buf_seqno(queue_buf);
1544
1545                 if (seq_no == curr_seqno) {
1546                         kfree_skb(buf);
1547                         return 0;
1548                 }
1549
1550                 if (less(seq_no, curr_seqno))
1551                         break;
1552
1553                 prev = &queue_buf->next;
1554                 queue_buf = queue_buf->next;
1555         }
1556
1557         buf->next = queue_buf;
1558         *prev = buf;
1559         return 1;
1560 }
1561
1562 /*
1563  * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1564  */
1565 static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
1566                                        struct sk_buff *buf)
1567 {
1568         u32 seq_no = buf_seqno(buf);
1569
1570         if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1571                 tipc_link_proto_rcv(l_ptr, buf);
1572                 return;
1573         }
1574
1575         /* Record OOS packet arrival (force mismatch on next timeout) */
1576         l_ptr->checkpoint--;
1577
1578         /*
1579          * Discard packet if a duplicate; otherwise add it to deferred queue
1580          * and notify peer of gap as per protocol specification
1581          */
1582         if (less(seq_no, mod(l_ptr->next_in_no))) {
1583                 l_ptr->stats.duplicates++;
1584                 kfree_skb(buf);
1585                 return;
1586         }
1587
1588         if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1589                                 &l_ptr->newest_deferred_in, buf)) {
1590                 l_ptr->deferred_inqueue_sz++;
1591                 l_ptr->stats.deferred_recv++;
1592                 TIPC_SKB_CB(buf)->deferred = true;
1593                 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
1594                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1595         } else
1596                 l_ptr->stats.duplicates++;
1597 }
1598
1599 /*
1600  * Send protocol message to the other endpoint.
1601  */
1602 void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1603                           u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
1604 {
1605         struct sk_buff *buf = NULL;
1606         struct tipc_msg *msg = l_ptr->pmsg;
1607         u32 msg_size = sizeof(l_ptr->proto_msg);
1608         int r_flag;
1609
1610         /* Discard any previous message that was deferred due to congestion */
1611         if (l_ptr->proto_msg_queue) {
1612                 kfree_skb(l_ptr->proto_msg_queue);
1613                 l_ptr->proto_msg_queue = NULL;
1614         }
1615
1616         /* Don't send protocol message during link changeover */
1617         if (l_ptr->exp_msg_count)
1618                 return;
1619
1620         /* Abort non-RESET send if communication with node is prohibited */
1621         if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
1622                 return;
1623
1624         /* Create protocol message with "out-of-sequence" sequence number */
1625         msg_set_type(msg, msg_typ);
1626         msg_set_net_plane(msg, l_ptr->net_plane);
1627         msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1628         msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
1629
1630         if (msg_typ == STATE_MSG) {
1631                 u32 next_sent = mod(l_ptr->next_out_no);
1632
1633                 if (!tipc_link_is_up(l_ptr))
1634                         return;
1635                 if (l_ptr->next_out)
1636                         next_sent = buf_seqno(l_ptr->next_out);
1637                 msg_set_next_sent(msg, next_sent);
1638                 if (l_ptr->oldest_deferred_in) {
1639                         u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
1640                         gap = mod(rec - mod(l_ptr->next_in_no));
1641                 }
1642                 msg_set_seq_gap(msg, gap);
1643                 if (gap)
1644                         l_ptr->stats.sent_nacks++;
1645                 msg_set_link_tolerance(msg, tolerance);
1646                 msg_set_linkprio(msg, priority);
1647                 msg_set_max_pkt(msg, ack_mtu);
1648                 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1649                 msg_set_probe(msg, probe_msg != 0);
1650                 if (probe_msg) {
1651                         u32 mtu = l_ptr->max_pkt;
1652
1653                         if ((mtu < l_ptr->max_pkt_target) &&
1654                             link_working_working(l_ptr) &&
1655                             l_ptr->fsm_msg_cnt) {
1656                                 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1657                                 if (l_ptr->max_pkt_probes == 10) {
1658                                         l_ptr->max_pkt_target = (msg_size - 4);
1659                                         l_ptr->max_pkt_probes = 0;
1660                                         msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1661                                 }
1662                                 l_ptr->max_pkt_probes++;
1663                         }
1664
1665                         l_ptr->stats.sent_probes++;
1666                 }
1667                 l_ptr->stats.sent_states++;
1668         } else {                /* RESET_MSG or ACTIVATE_MSG */
1669                 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1670                 msg_set_seq_gap(msg, 0);
1671                 msg_set_next_sent(msg, 1);
1672                 msg_set_probe(msg, 0);
1673                 msg_set_link_tolerance(msg, l_ptr->tolerance);
1674                 msg_set_linkprio(msg, l_ptr->priority);
1675                 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1676         }
1677
1678         r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1679         msg_set_redundant_link(msg, r_flag);
1680         msg_set_linkprio(msg, l_ptr->priority);
1681         msg_set_size(msg, msg_size);
1682
1683         msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1684
1685         buf = tipc_buf_acquire(msg_size);
1686         if (!buf)
1687                 return;
1688
1689         skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
1690         buf->priority = TC_PRIO_CONTROL;
1691
1692         tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
1693         l_ptr->unacked_window = 0;
1694         kfree_skb(buf);
1695 }
1696
1697 /*
1698  * Receive protocol message :
1699  * Note that network plane id propagates through the network, and may
1700  * change at any time. The node with lowest address rules
1701  */
1702 static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
1703 {
1704         u32 rec_gap = 0;
1705         u32 max_pkt_info;
1706         u32 max_pkt_ack;
1707         u32 msg_tol;
1708         struct tipc_msg *msg = buf_msg(buf);
1709
1710         /* Discard protocol message during link changeover */
1711         if (l_ptr->exp_msg_count)
1712                 goto exit;
1713
1714         if (l_ptr->net_plane != msg_net_plane(msg))
1715                 if (tipc_own_addr > msg_prevnode(msg))
1716                         l_ptr->net_plane = msg_net_plane(msg);
1717
1718         switch (msg_type(msg)) {
1719
1720         case RESET_MSG:
1721                 if (!link_working_unknown(l_ptr) &&
1722                     (l_ptr->peer_session != INVALID_SESSION)) {
1723                         if (less_eq(msg_session(msg), l_ptr->peer_session))
1724                                 break; /* duplicate or old reset: ignore */
1725                 }
1726
1727                 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1728                                 link_working_unknown(l_ptr))) {
1729                         /*
1730                          * peer has lost contact -- don't allow peer's links
1731                          * to reactivate before we recognize loss & clean up
1732                          */
1733                         l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
1734                 }
1735
1736                 link_state_event(l_ptr, RESET_MSG);
1737
1738                 /* fall thru' */
1739         case ACTIVATE_MSG:
1740                 /* Update link settings according other endpoint's values */
1741                 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1742
1743                 msg_tol = msg_link_tolerance(msg);
1744                 if (msg_tol > l_ptr->tolerance)
1745                         link_set_supervision_props(l_ptr, msg_tol);
1746
1747                 if (msg_linkprio(msg) > l_ptr->priority)
1748                         l_ptr->priority = msg_linkprio(msg);
1749
1750                 max_pkt_info = msg_max_pkt(msg);
1751                 if (max_pkt_info) {
1752                         if (max_pkt_info < l_ptr->max_pkt_target)
1753                                 l_ptr->max_pkt_target = max_pkt_info;
1754                         if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1755                                 l_ptr->max_pkt = l_ptr->max_pkt_target;
1756                 } else {
1757                         l_ptr->max_pkt = l_ptr->max_pkt_target;
1758                 }
1759
1760                 /* Synchronize broadcast link info, if not done previously */
1761                 if (!tipc_node_is_up(l_ptr->owner)) {
1762                         l_ptr->owner->bclink.last_sent =
1763                                 l_ptr->owner->bclink.last_in =
1764                                 msg_last_bcast(msg);
1765                         l_ptr->owner->bclink.oos_state = 0;
1766                 }
1767
1768                 l_ptr->peer_session = msg_session(msg);
1769                 l_ptr->peer_bearer_id = msg_bearer_id(msg);
1770
1771                 if (msg_type(msg) == ACTIVATE_MSG)
1772                         link_state_event(l_ptr, ACTIVATE_MSG);
1773                 break;
1774         case STATE_MSG:
1775
1776                 msg_tol = msg_link_tolerance(msg);
1777                 if (msg_tol)
1778                         link_set_supervision_props(l_ptr, msg_tol);
1779
1780                 if (msg_linkprio(msg) &&
1781                     (msg_linkprio(msg) != l_ptr->priority)) {
1782                         pr_warn("%s<%s>, priority change %u->%u\n",
1783                                 link_rst_msg, l_ptr->name, l_ptr->priority,
1784                                 msg_linkprio(msg));
1785                         l_ptr->priority = msg_linkprio(msg);
1786                         tipc_link_reset(l_ptr); /* Enforce change to take effect */
1787                         break;
1788                 }
1789
1790                 /* Record reception; force mismatch at next timeout: */
1791                 l_ptr->checkpoint--;
1792
1793                 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1794                 l_ptr->stats.recv_states++;
1795                 if (link_reset_unknown(l_ptr))
1796                         break;
1797
1798                 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
1799                         rec_gap = mod(msg_next_sent(msg) -
1800                                       mod(l_ptr->next_in_no));
1801                 }
1802
1803                 max_pkt_ack = msg_max_pkt(msg);
1804                 if (max_pkt_ack > l_ptr->max_pkt) {
1805                         l_ptr->max_pkt = max_pkt_ack;
1806                         l_ptr->max_pkt_probes = 0;
1807                 }
1808
1809                 max_pkt_ack = 0;
1810                 if (msg_probe(msg)) {
1811                         l_ptr->stats.recv_probes++;
1812                         if (msg_size(msg) > sizeof(l_ptr->proto_msg))
1813                                 max_pkt_ack = msg_size(msg);
1814                 }
1815
1816                 /* Protocol message before retransmits, reduce loss risk */
1817                 if (l_ptr->owner->bclink.recv_permitted)
1818                         tipc_bclink_update_link_state(l_ptr->owner,
1819                                                       msg_last_bcast(msg));
1820
1821                 if (rec_gap || (msg_probe(msg))) {
1822                         tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1823                                              0, max_pkt_ack);
1824                 }
1825                 if (msg_seq_gap(msg)) {
1826                         l_ptr->stats.recv_nacks++;
1827                         tipc_link_retransmit(l_ptr, l_ptr->first_out,
1828                                              msg_seq_gap(msg));
1829                 }
1830                 break;
1831         }
1832 exit:
1833         kfree_skb(buf);
1834 }
1835
1836
1837 /* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1838  * a different bearer. Owner node is locked.
1839  */
1840 static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1841                                   struct tipc_msg *tunnel_hdr,
1842                                   struct tipc_msg *msg,
1843                                   u32 selector)
1844 {
1845         struct tipc_link *tunnel;
1846         struct sk_buff *buf;
1847         u32 length = msg_size(msg);
1848
1849         tunnel = l_ptr->owner->active_links[selector & 1];
1850         if (!tipc_link_is_up(tunnel)) {
1851                 pr_warn("%stunnel link no longer available\n", link_co_err);
1852                 return;
1853         }
1854         msg_set_size(tunnel_hdr, length + INT_H_SIZE);
1855         buf = tipc_buf_acquire(length + INT_H_SIZE);
1856         if (!buf) {
1857                 pr_warn("%sunable to send tunnel msg\n", link_co_err);
1858                 return;
1859         }
1860         skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1861         skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
1862         __tipc_link_xmit(tunnel, buf);
1863 }
1864
1865
1866 /* tipc_link_failover_send_queue(): A link has gone down, but a second
1867  * link is still active. We can do failover. Tunnel the failing link's
1868  * whole send queue via the remaining link. This way, we don't lose
1869  * any packets, and sequence order is preserved for subsequent traffic
1870  * sent over the remaining link. Owner node is locked.
1871  */
1872 void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
1873 {
1874         u32 msgcount = l_ptr->out_queue_size;
1875         struct sk_buff *crs = l_ptr->first_out;
1876         struct tipc_link *tunnel = l_ptr->owner->active_links[0];
1877         struct tipc_msg tunnel_hdr;
1878         int split_bundles;
1879
1880         if (!tunnel)
1881                 return;
1882
1883         tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
1884                  ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
1885         msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1886         msg_set_msgcnt(&tunnel_hdr, msgcount);
1887
1888         if (!l_ptr->first_out) {
1889                 struct sk_buff *buf;
1890
1891                 buf = tipc_buf_acquire(INT_H_SIZE);
1892                 if (buf) {
1893                         skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
1894                         msg_set_size(&tunnel_hdr, INT_H_SIZE);
1895                         __tipc_link_xmit(tunnel, buf);
1896                 } else {
1897                         pr_warn("%sunable to send changeover msg\n",
1898                                 link_co_err);
1899                 }
1900                 return;
1901         }
1902
1903         split_bundles = (l_ptr->owner->active_links[0] !=
1904                          l_ptr->owner->active_links[1]);
1905
1906         while (crs) {
1907                 struct tipc_msg *msg = buf_msg(crs);
1908
1909                 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
1910                         struct tipc_msg *m = msg_get_wrapped(msg);
1911                         unchar *pos = (unchar *)m;
1912
1913                         msgcount = msg_msgcnt(msg);
1914                         while (msgcount--) {
1915                                 msg_set_seqno(m, msg_seqno(msg));
1916                                 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1917                                                       msg_link_selector(m));
1918                                 pos += align(msg_size(m));
1919                                 m = (struct tipc_msg *)pos;
1920                         }
1921                 } else {
1922                         tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1923                                               msg_link_selector(msg));
1924                 }
1925                 crs = crs->next;
1926         }
1927 }
1928
1929 /* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
1930  * duplicate of the first link's send queue via the new link. This way, we
1931  * are guaranteed that currently queued packets from a socket are delivered
1932  * before future traffic from the same socket, even if this is using the
1933  * new link. The last arriving copy of each duplicate packet is dropped at
1934  * the receiving end by the regular protocol check, so packet cardinality
1935  * and sequence order is preserved per sender/receiver socket pair.
1936  * Owner node is locked.
1937  */
1938 void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
1939                               struct tipc_link *tunnel)
1940 {
1941         struct sk_buff *iter;
1942         struct tipc_msg tunnel_hdr;
1943
1944         tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
1945                  DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
1946         msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1947         msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1948         iter = l_ptr->first_out;
1949         while (iter) {
1950                 struct sk_buff *outbuf;
1951                 struct tipc_msg *msg = buf_msg(iter);
1952                 u32 length = msg_size(msg);
1953
1954                 if (msg_user(msg) == MSG_BUNDLER)
1955                         msg_set_type(msg, CLOSED_MSG);
1956                 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));   /* Update */
1957                 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1958                 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
1959                 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
1960                 if (outbuf == NULL) {
1961                         pr_warn("%sunable to send duplicate msg\n",
1962                                 link_co_err);
1963                         return;
1964                 }
1965                 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1966                 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1967                                                length);
1968                 __tipc_link_xmit(tunnel, outbuf);
1969                 if (!tipc_link_is_up(l_ptr))
1970                         return;
1971                 iter = iter->next;
1972         }
1973 }
1974
1975 /**
1976  * buf_extract - extracts embedded TIPC message from another message
1977  * @skb: encapsulating message buffer
1978  * @from_pos: offset to extract from
1979  *
1980  * Returns a new message buffer containing an embedded message.  The
1981  * encapsulating message itself is left unchanged.
1982  */
1983 static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1984 {
1985         struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1986         u32 size = msg_size(msg);
1987         struct sk_buff *eb;
1988
1989         eb = tipc_buf_acquire(size);
1990         if (eb)
1991                 skb_copy_to_linear_data(eb, msg, size);
1992         return eb;
1993 }
1994
1995
1996
1997 /* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1998  * Owner node is locked.
1999  */
2000 static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
2001                               struct sk_buff *t_buf)
2002 {
2003         struct sk_buff *buf;
2004
2005         if (!tipc_link_is_up(l_ptr))
2006                 return;
2007
2008         buf = buf_extract(t_buf, INT_H_SIZE);
2009         if (buf == NULL) {
2010                 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
2011                 return;
2012         }
2013
2014         /* Add buffer to deferred queue, if applicable: */
2015         link_handle_out_of_seq_msg(l_ptr, buf);
2016 }
2017
2018 /*  tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
2019  *  Owner node is locked.
2020  */
2021 static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
2022                                               struct sk_buff *t_buf)
2023 {
2024         struct tipc_msg *t_msg = buf_msg(t_buf);
2025         struct sk_buff *buf = NULL;
2026         struct tipc_msg *msg;
2027
2028         if (tipc_link_is_up(l_ptr))
2029                 tipc_link_reset(l_ptr);
2030
2031         /* First failover packet? */
2032         if (l_ptr->exp_msg_count == START_CHANGEOVER)
2033                 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
2034
2035         /* Should there be an inner packet? */
2036         if (l_ptr->exp_msg_count) {
2037                 l_ptr->exp_msg_count--;
2038                 buf = buf_extract(t_buf, INT_H_SIZE);
2039                 if (buf == NULL) {
2040                         pr_warn("%sno inner failover pkt\n", link_co_err);
2041                         goto exit;
2042                 }
2043                 msg = buf_msg(buf);
2044
2045                 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
2046                         kfree_skb(buf);
2047                         buf = NULL;
2048                         goto exit;
2049                 }
2050                 if (msg_user(msg) == MSG_FRAGMENTER) {
2051                         l_ptr->stats.recv_fragments++;
2052                         tipc_buf_append(&l_ptr->reasm_buf, &buf);
2053                 }
2054         }
2055 exit:
2056         if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
2057                 tipc_node_detach_link(l_ptr->owner, l_ptr);
2058                 kfree(l_ptr);
2059         }
2060         return buf;
2061 }
2062
2063 /*  tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
2064  *  via other link as result of a failover (ORIGINAL_MSG) or
2065  *  a new active link (DUPLICATE_MSG). Failover packets are
2066  *  returned to the active link for delivery upwards.
2067  *  Owner node is locked.
2068  */
2069 static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
2070                                 struct sk_buff **buf)
2071 {
2072         struct sk_buff *t_buf = *buf;
2073         struct tipc_link *l_ptr;
2074         struct tipc_msg *t_msg = buf_msg(t_buf);
2075         u32 bearer_id = msg_bearer_id(t_msg);
2076
2077         *buf = NULL;
2078
2079         if (bearer_id >= MAX_BEARERS)
2080                 goto exit;
2081
2082         l_ptr = n_ptr->links[bearer_id];
2083         if (!l_ptr)
2084                 goto exit;
2085
2086         if (msg_type(t_msg) == DUPLICATE_MSG)
2087                 tipc_link_dup_rcv(l_ptr, t_buf);
2088         else if (msg_type(t_msg) == ORIGINAL_MSG)
2089                 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
2090         else
2091                 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
2092 exit:
2093         kfree_skb(t_buf);
2094         return *buf != NULL;
2095 }
2096
2097 /*
2098  *  Bundler functionality:
2099  */
2100 void tipc_link_bundle_rcv(struct sk_buff *buf)
2101 {
2102         u32 msgcount = msg_msgcnt(buf_msg(buf));
2103         u32 pos = INT_H_SIZE;
2104         struct sk_buff *obuf;
2105         struct tipc_msg *omsg;
2106
2107         while (msgcount--) {
2108                 obuf = buf_extract(buf, pos);
2109                 if (obuf == NULL) {
2110                         pr_warn("Link unable to unbundle message(s)\n");
2111                         break;
2112                 }
2113                 omsg = buf_msg(obuf);
2114                 pos += align(msg_size(omsg));
2115                 if (msg_isdata(omsg) || (msg_user(omsg) == CONN_MANAGER)) {
2116                         tipc_sk_rcv(obuf);
2117                 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
2118                         tipc_named_rcv(obuf);
2119                 } else {
2120                         pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
2121                         kfree_skb(obuf);
2122                 }
2123         }
2124         kfree_skb(buf);
2125 }
2126
2127 /*
2128  *  Fragmentation/defragmentation:
2129  */
2130
2131 /*
2132  * tipc_link_frag_xmit: Entry for buffers needing fragmentation.
2133  * The buffer is complete, inclusive total message length.
2134  * Returns user data length.
2135  */
2136 static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
2137 {
2138         struct sk_buff *buf_chain = NULL;
2139         struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
2140         struct tipc_msg *inmsg = buf_msg(buf);
2141         struct tipc_msg fragm_hdr;
2142         u32 insize = msg_size(inmsg);
2143         u32 dsz = msg_data_sz(inmsg);
2144         unchar *crs = buf->data;
2145         u32 rest = insize;
2146         u32 pack_sz = l_ptr->max_pkt;
2147         u32 fragm_sz = pack_sz - INT_H_SIZE;
2148         u32 fragm_no = 0;
2149         u32 destaddr;
2150
2151         if (msg_short(inmsg))
2152                 destaddr = l_ptr->addr;
2153         else
2154                 destaddr = msg_destnode(inmsg);
2155
2156         /* Prepare reusable fragment header: */
2157         tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
2158                  INT_H_SIZE, destaddr);
2159
2160         /* Chop up message: */
2161         while (rest > 0) {
2162                 struct sk_buff *fragm;
2163
2164                 if (rest <= fragm_sz) {
2165                         fragm_sz = rest;
2166                         msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2167                 }
2168                 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
2169                 if (fragm == NULL) {
2170                         kfree_skb(buf);
2171                         kfree_skb_list(buf_chain);
2172                         return -ENOMEM;
2173                 }
2174                 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
2175                 fragm_no++;
2176                 msg_set_fragm_no(&fragm_hdr, fragm_no);
2177                 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2178                 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2179                                                fragm_sz);
2180                 buf_chain_tail->next = fragm;
2181                 buf_chain_tail = fragm;
2182
2183                 rest -= fragm_sz;
2184                 crs += fragm_sz;
2185                 msg_set_type(&fragm_hdr, FRAGMENT);
2186         }
2187         kfree_skb(buf);
2188
2189         /* Append chain of fragments to send queue & send them */
2190         l_ptr->long_msg_seq_no++;
2191         link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2192         l_ptr->stats.sent_fragments += fragm_no;
2193         l_ptr->stats.sent_fragmented++;
2194         tipc_link_push_queue(l_ptr);
2195
2196         return dsz;
2197 }
2198
2199 static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
2200 {
2201         if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2202                 return;
2203
2204         l_ptr->tolerance = tolerance;
2205         l_ptr->continuity_interval =
2206                 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2207         l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2208 }
2209
2210 void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
2211 {
2212         /* Data messages from this node, inclusive FIRST_FRAGM */
2213         l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2214         l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2215         l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2216         l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
2217         /* Transiting data messages,inclusive FIRST_FRAGM */
2218         l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2219         l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2220         l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2221         l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
2222         l_ptr->queue_limit[CONN_MANAGER] = 1200;
2223         l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2224         l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2225         /* FRAGMENT and LAST_FRAGMENT packets */
2226         l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2227 }
2228
2229 /* tipc_link_find_owner - locate owner node of link by link's name
2230  * @name: pointer to link name string
2231  * @bearer_id: pointer to index in 'node->links' array where the link was found.
2232  *
2233  * Returns pointer to node owning the link, or 0 if no matching link is found.
2234  */
2235 static struct tipc_node *tipc_link_find_owner(const char *link_name,
2236                                               unsigned int *bearer_id)
2237 {
2238         struct tipc_link *l_ptr;
2239         struct tipc_node *n_ptr;
2240         struct tipc_node *found_node = 0;
2241         int i;
2242
2243         *bearer_id = 0;
2244         rcu_read_lock();
2245         list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
2246                 tipc_node_lock(n_ptr);
2247                 for (i = 0; i < MAX_BEARERS; i++) {
2248                         l_ptr = n_ptr->links[i];
2249                         if (l_ptr && !strcmp(l_ptr->name, link_name)) {
2250                                 *bearer_id = i;
2251                                 found_node = n_ptr;
2252                                 break;
2253                         }
2254                 }
2255                 tipc_node_unlock(n_ptr);
2256                 if (found_node)
2257                         break;
2258         }
2259         rcu_read_unlock();
2260
2261         return found_node;
2262 }
2263
2264 /**
2265  * link_value_is_valid -- validate proposed link tolerance/priority/window
2266  *
2267  * @cmd: value type (TIPC_CMD_SET_LINK_*)
2268  * @new_value: the new value
2269  *
2270  * Returns 1 if value is within range, 0 if not.
2271  */
2272 static int link_value_is_valid(u16 cmd, u32 new_value)
2273 {
2274         switch (cmd) {
2275         case TIPC_CMD_SET_LINK_TOL:
2276                 return (new_value >= TIPC_MIN_LINK_TOL) &&
2277                         (new_value <= TIPC_MAX_LINK_TOL);
2278         case TIPC_CMD_SET_LINK_PRI:
2279                 return (new_value <= TIPC_MAX_LINK_PRI);
2280         case TIPC_CMD_SET_LINK_WINDOW:
2281                 return (new_value >= TIPC_MIN_LINK_WIN) &&
2282                         (new_value <= TIPC_MAX_LINK_WIN);
2283         }
2284         return 0;
2285 }
2286
2287 /**
2288  * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
2289  * @name: ptr to link, bearer, or media name
2290  * @new_value: new value of link, bearer, or media setting
2291  * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
2292  *
2293  * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
2294  *
2295  * Returns 0 if value updated and negative value on error.
2296  */
2297 static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2298 {
2299         struct tipc_node *node;
2300         struct tipc_link *l_ptr;
2301         struct tipc_bearer *b_ptr;
2302         struct tipc_media *m_ptr;
2303         int bearer_id;
2304         int res = 0;
2305
2306         node = tipc_link_find_owner(name, &bearer_id);
2307         if (node) {
2308                 tipc_node_lock(node);
2309                 l_ptr = node->links[bearer_id];
2310
2311                 if (l_ptr) {
2312                         switch (cmd) {
2313                         case TIPC_CMD_SET_LINK_TOL:
2314                                 link_set_supervision_props(l_ptr, new_value);
2315                                 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2316                                                      new_value, 0, 0);
2317                                 break;
2318                         case TIPC_CMD_SET_LINK_PRI:
2319                                 l_ptr->priority = new_value;
2320                                 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2321                                                      0, new_value, 0);
2322                                 break;
2323                         case TIPC_CMD_SET_LINK_WINDOW:
2324                                 tipc_link_set_queue_limits(l_ptr, new_value);
2325                                 break;
2326                         default:
2327                                 res = -EINVAL;
2328                                 break;
2329                         }
2330                 }
2331                 tipc_node_unlock(node);
2332                 return res;
2333         }
2334
2335         b_ptr = tipc_bearer_find(name);
2336         if (b_ptr) {
2337                 switch (cmd) {
2338                 case TIPC_CMD_SET_LINK_TOL:
2339                         b_ptr->tolerance = new_value;
2340                         break;
2341                 case TIPC_CMD_SET_LINK_PRI:
2342                         b_ptr->priority = new_value;
2343                         break;
2344                 case TIPC_CMD_SET_LINK_WINDOW:
2345                         b_ptr->window = new_value;
2346                         break;
2347                 default:
2348                         res = -EINVAL;
2349                         break;
2350                 }
2351                 return res;
2352         }
2353
2354         m_ptr = tipc_media_find(name);
2355         if (!m_ptr)
2356                 return -ENODEV;
2357         switch (cmd) {
2358         case TIPC_CMD_SET_LINK_TOL:
2359                 m_ptr->tolerance = new_value;
2360                 break;
2361         case TIPC_CMD_SET_LINK_PRI:
2362                 m_ptr->priority = new_value;
2363                 break;
2364         case TIPC_CMD_SET_LINK_WINDOW:
2365                 m_ptr->window = new_value;
2366                 break;
2367         default:
2368                 res = -EINVAL;
2369                 break;
2370         }
2371         return res;
2372 }
2373
2374 struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
2375                                      u16 cmd)
2376 {
2377         struct tipc_link_config *args;
2378         u32 new_value;
2379         int res;
2380
2381         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
2382                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2383
2384         args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2385         new_value = ntohl(args->value);
2386
2387         if (!link_value_is_valid(cmd, new_value))
2388                 return tipc_cfg_reply_error_string(
2389                         "cannot change, value invalid");
2390
2391         if (!strcmp(args->name, tipc_bclink_name)) {
2392                 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
2393                     (tipc_bclink_set_queue_limits(new_value) == 0))
2394                         return tipc_cfg_reply_none();
2395                 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
2396                                                    " (cannot change setting on broadcast link)");
2397         }
2398
2399         res = link_cmd_set_value(args->name, new_value, cmd);
2400         if (res)
2401                 return tipc_cfg_reply_error_string("cannot change link setting");
2402
2403         return tipc_cfg_reply_none();
2404 }
2405
2406 /**
2407  * link_reset_statistics - reset link statistics
2408  * @l_ptr: pointer to link
2409  */
2410 static void link_reset_statistics(struct tipc_link *l_ptr)
2411 {
2412         memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2413         l_ptr->stats.sent_info = l_ptr->next_out_no;
2414         l_ptr->stats.recv_info = l_ptr->next_in_no;
2415 }
2416
2417 struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
2418 {
2419         char *link_name;
2420         struct tipc_link *l_ptr;
2421         struct tipc_node *node;
2422         unsigned int bearer_id;
2423
2424         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
2425                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2426
2427         link_name = (char *)TLV_DATA(req_tlv_area);
2428         if (!strcmp(link_name, tipc_bclink_name)) {
2429                 if (tipc_bclink_reset_stats())
2430                         return tipc_cfg_reply_error_string("link not found");
2431                 return tipc_cfg_reply_none();
2432         }
2433         node = tipc_link_find_owner(link_name, &bearer_id);
2434         if (!node)
2435                 return tipc_cfg_reply_error_string("link not found");
2436
2437         tipc_node_lock(node);
2438         l_ptr = node->links[bearer_id];
2439         if (!l_ptr) {
2440                 tipc_node_unlock(node);
2441                 return tipc_cfg_reply_error_string("link not found");
2442         }
2443         link_reset_statistics(l_ptr);
2444         tipc_node_unlock(node);
2445         return tipc_cfg_reply_none();
2446 }
2447
2448 /**
2449  * percent - convert count to a percentage of total (rounding up or down)
2450  */
2451 static u32 percent(u32 count, u32 total)
2452 {
2453         return (count * 100 + (total / 2)) / total;
2454 }
2455
2456 /**
2457  * tipc_link_stats - print link statistics
2458  * @name: link name
2459  * @buf: print buffer area
2460  * @buf_size: size of print buffer area
2461  *
2462  * Returns length of print buffer data string (or 0 if error)
2463  */
2464 static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
2465 {
2466         struct tipc_link *l;
2467         struct tipc_stats *s;
2468         struct tipc_node *node;
2469         char *status;
2470         u32 profile_total = 0;
2471         unsigned int bearer_id;
2472         int ret;
2473
2474         if (!strcmp(name, tipc_bclink_name))
2475                 return tipc_bclink_stats(buf, buf_size);
2476
2477         node = tipc_link_find_owner(name, &bearer_id);
2478         if (!node)
2479                 return 0;
2480
2481         tipc_node_lock(node);
2482
2483         l = node->links[bearer_id];
2484         if (!l) {
2485                 tipc_node_unlock(node);
2486                 return 0;
2487         }
2488
2489         s = &l->stats;
2490
2491         if (tipc_link_is_active(l))
2492                 status = "ACTIVE";
2493         else if (tipc_link_is_up(l))
2494                 status = "STANDBY";
2495         else
2496                 status = "DEFUNCT";
2497
2498         ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2499                             "  %s  MTU:%u  Priority:%u  Tolerance:%u ms"
2500                             "  Window:%u packets\n",
2501                             l->name, status, l->max_pkt, l->priority,
2502                             l->tolerance, l->queue_limit[0]);
2503
2504         ret += tipc_snprintf(buf + ret, buf_size - ret,
2505                              "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2506                              l->next_in_no - s->recv_info, s->recv_fragments,
2507                              s->recv_fragmented, s->recv_bundles,
2508                              s->recv_bundled);
2509
2510         ret += tipc_snprintf(buf + ret, buf_size - ret,
2511                              "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2512                              l->next_out_no - s->sent_info, s->sent_fragments,
2513                              s->sent_fragmented, s->sent_bundles,
2514                              s->sent_bundled);
2515
2516         profile_total = s->msg_length_counts;
2517         if (!profile_total)
2518                 profile_total = 1;
2519
2520         ret += tipc_snprintf(buf + ret, buf_size - ret,
2521                              "  TX profile sample:%u packets  average:%u octets\n"
2522                              "  0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2523                              "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2524                              s->msg_length_counts,
2525                              s->msg_lengths_total / profile_total,
2526                              percent(s->msg_length_profile[0], profile_total),
2527                              percent(s->msg_length_profile[1], profile_total),
2528                              percent(s->msg_length_profile[2], profile_total),
2529                              percent(s->msg_length_profile[3], profile_total),
2530                              percent(s->msg_length_profile[4], profile_total),
2531                              percent(s->msg_length_profile[5], profile_total),
2532                              percent(s->msg_length_profile[6], profile_total));
2533
2534         ret += tipc_snprintf(buf + ret, buf_size - ret,
2535                              "  RX states:%u probes:%u naks:%u defs:%u"
2536                              " dups:%u\n", s->recv_states, s->recv_probes,
2537                              s->recv_nacks, s->deferred_recv, s->duplicates);
2538
2539         ret += tipc_snprintf(buf + ret, buf_size - ret,
2540                              "  TX states:%u probes:%u naks:%u acks:%u"
2541                              " dups:%u\n", s->sent_states, s->sent_probes,
2542                              s->sent_nacks, s->sent_acks, s->retransmitted);
2543
2544         ret += tipc_snprintf(buf + ret, buf_size - ret,
2545                              "  Congestion link:%u  Send queue"
2546                              " max:%u avg:%u\n", s->link_congs,
2547                              s->max_queue_sz, s->queue_sz_counts ?
2548                              (s->accu_queue_sz / s->queue_sz_counts) : 0);
2549
2550         tipc_node_unlock(node);
2551         return ret;
2552 }
2553
2554 struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
2555 {
2556         struct sk_buff *buf;
2557         struct tlv_desc *rep_tlv;
2558         int str_len;
2559         int pb_len;
2560         char *pb;
2561
2562         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
2563                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2564
2565         buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2566         if (!buf)
2567                 return NULL;
2568
2569         rep_tlv = (struct tlv_desc *)buf->data;
2570         pb = TLV_DATA(rep_tlv);
2571         pb_len = ULTRA_STRING_MAX_LEN;
2572         str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
2573                                   pb, pb_len);
2574         if (!str_len) {
2575                 kfree_skb(buf);
2576                 return tipc_cfg_reply_error_string("link not found");
2577         }
2578         str_len += 1;   /* for "\0" */
2579         skb_put(buf, TLV_SPACE(str_len));
2580         TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2581
2582         return buf;
2583 }
2584
2585 /**
2586  * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
2587  * @dest: network address of destination node
2588  * @selector: used to select from set of active links
2589  *
2590  * If no active link can be found, uses default maximum packet size.
2591  */
2592 u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
2593 {
2594         struct tipc_node *n_ptr;
2595         struct tipc_link *l_ptr;
2596         u32 res = MAX_PKT_DEFAULT;
2597
2598         if (dest == tipc_own_addr)
2599                 return MAX_MSG_SIZE;
2600
2601         n_ptr = tipc_node_find(dest);
2602         if (n_ptr) {
2603                 tipc_node_lock(n_ptr);
2604                 l_ptr = n_ptr->active_links[selector & 1];
2605                 if (l_ptr)
2606                         res = l_ptr->max_pkt;
2607                 tipc_node_unlock(n_ptr);
2608         }
2609         return res;
2610 }
2611
2612 static void link_print(struct tipc_link *l_ptr, const char *str)
2613 {
2614         struct tipc_bearer *b_ptr;
2615
2616         rcu_read_lock();
2617         b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2618         if (b_ptr)
2619                 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2620         rcu_read_unlock();
2621
2622         if (link_working_unknown(l_ptr))
2623                 pr_cont(":WU\n");
2624         else if (link_reset_reset(l_ptr))
2625                 pr_cont(":RR\n");
2626         else if (link_reset_unknown(l_ptr))
2627                 pr_cont(":RU\n");
2628         else if (link_working_working(l_ptr))
2629                 pr_cont(":WW\n");
2630         else
2631                 pr_cont("\n");
2632 }