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