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