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