xref: /linux/net/tipc/link.c (revision 6e8331ac6973435b1e7604c30f2ad394035b46e1)
1 /*
2  * net/tipc/link.c: TIPC link code
3  *
4  * Copyright (c) 1996-2006, Ericsson AB
5  * Copyright (c) 2004-2006, 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 "dbg.h"
39 #include "link.h"
40 #include "net.h"
41 #include "node.h"
42 #include "port.h"
43 #include "addr.h"
44 #include "node_subscr.h"
45 #include "name_distr.h"
46 #include "bearer.h"
47 #include "name_table.h"
48 #include "discover.h"
49 #include "config.h"
50 #include "bcast.h"
51 
52 
53 /*
54  * Limit for deferred reception queue:
55  */
56 
57 #define DEF_QUEUE_LIMIT 256u
58 
59 /*
60  * Link state events:
61  */
62 
63 #define  STARTING_EVT    856384768	/* link processing trigger */
64 #define  TRAFFIC_MSG_EVT 560815u	/* rx'd ??? */
65 #define  TIMEOUT_EVT     560817u	/* link timer expired */
66 
67 /*
68  * The following two 'message types' is really just implementation
69  * data conveniently stored in the message header.
70  * They must not be considered part of the protocol
71  */
72 #define OPEN_MSG   0
73 #define CLOSED_MSG 1
74 
75 /*
76  * State value stored in 'exp_msg_count'
77  */
78 
79 #define START_CHANGEOVER 100000u
80 
81 /**
82  * struct link_name - deconstructed link name
83  * @addr_local: network address of node at this end
84  * @if_local: name of interface at this end
85  * @addr_peer: network address of node at far end
86  * @if_peer: name of interface at far end
87  */
88 
89 struct link_name {
90 	u32 addr_local;
91 	char if_local[TIPC_MAX_IF_NAME];
92 	u32 addr_peer;
93 	char if_peer[TIPC_MAX_IF_NAME];
94 };
95 
96 #if 0
97 
98 /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
99 
100 /**
101  * struct link_event - link up/down event notification
102  */
103 
104 struct link_event {
105 	u32 addr;
106 	int up;
107 	void (*fcn)(u32, char *, int);
108 	char name[TIPC_MAX_LINK_NAME];
109 };
110 
111 #endif
112 
113 static void link_handle_out_of_seq_msg(struct link *l_ptr,
114 				       struct sk_buff *buf);
115 static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
116 static int  link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
117 static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
118 static int  link_send_sections_long(struct port *sender,
119 				    struct iovec const *msg_sect,
120 				    u32 num_sect, u32 destnode);
121 static void link_check_defragm_bufs(struct link *l_ptr);
122 static void link_state_event(struct link *l_ptr, u32 event);
123 static void link_reset_statistics(struct link *l_ptr);
124 static void link_print(struct link *l_ptr, struct print_buf *buf,
125 		       const char *str);
126 
127 /*
128  * Debugging code used by link routines only
129  *
130  * When debugging link problems on a system that has multiple links,
131  * the standard TIPC debugging routines may not be useful since they
132  * allow the output from multiple links to be intermixed.  For this reason
133  * routines of the form "dbg_link_XXX()" have been created that will capture
134  * debug info into a link's personal print buffer, which can then be dumped
135  * into the TIPC system log (LOG) upon request.
136  *
137  * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size
138  * of the print buffer used by each link.  If LINK_LOG_BUF_SIZE is set to 0,
139  * the dbg_link_XXX() routines simply send their output to the standard
140  * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful
141  * when there is only a single link in the system being debugged.
142  *
143  * Notes:
144  * - When enabled, LINK_LOG_BUF_SIZE should be set to at least 1000 (bytes)
145  * - "l_ptr" must be valid when using dbg_link_XXX() macros
146  */
147 
148 #define LINK_LOG_BUF_SIZE 0
149 
150 #define dbg_link(fmt, arg...)  do {if (LINK_LOG_BUF_SIZE) tipc_printf(&l_ptr->print_buf, fmt, ## arg); } while(0)
151 #define dbg_link_msg(msg, txt) do {if (LINK_LOG_BUF_SIZE) tipc_msg_print(&l_ptr->print_buf, msg, txt); } while(0)
152 #define dbg_link_state(txt) do {if (LINK_LOG_BUF_SIZE) link_print(l_ptr, &l_ptr->print_buf, txt); } while(0)
153 #define dbg_link_dump() do { \
154 	if (LINK_LOG_BUF_SIZE) { \
155 		tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \
156 		tipc_printbuf_move(LOG, &l_ptr->print_buf); \
157 	} \
158 } while (0)
159 
160 static void dbg_print_link(struct link *l_ptr, const char *str)
161 {
162 	if (DBG_OUTPUT)
163 		link_print(l_ptr, DBG_OUTPUT, str);
164 }
165 
166 static void dbg_print_buf_chain(struct sk_buff *root_buf)
167 {
168 	if (DBG_OUTPUT) {
169 		struct sk_buff *buf = root_buf;
170 
171 		while (buf) {
172 			msg_dbg(buf_msg(buf), "In chain: ");
173 			buf = buf->next;
174 		}
175 	}
176 }
177 
178 /*
179  *  Simple link routines
180  */
181 
182 static unsigned int align(unsigned int i)
183 {
184 	return (i + 3) & ~3u;
185 }
186 
187 static int link_working_working(struct link *l_ptr)
188 {
189 	return (l_ptr->state == WORKING_WORKING);
190 }
191 
192 static int link_working_unknown(struct link *l_ptr)
193 {
194 	return (l_ptr->state == WORKING_UNKNOWN);
195 }
196 
197 static int link_reset_unknown(struct link *l_ptr)
198 {
199 	return (l_ptr->state == RESET_UNKNOWN);
200 }
201 
202 static int link_reset_reset(struct link *l_ptr)
203 {
204 	return (l_ptr->state == RESET_RESET);
205 }
206 
207 static int link_blocked(struct link *l_ptr)
208 {
209 	return (l_ptr->exp_msg_count || l_ptr->blocked);
210 }
211 
212 static int link_congested(struct link *l_ptr)
213 {
214 	return (l_ptr->out_queue_size >= l_ptr->queue_limit[0]);
215 }
216 
217 static u32 link_max_pkt(struct link *l_ptr)
218 {
219 	return l_ptr->max_pkt;
220 }
221 
222 static void link_init_max_pkt(struct link *l_ptr)
223 {
224 	u32 max_pkt;
225 
226 	max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
227 	if (max_pkt > MAX_MSG_SIZE)
228 		max_pkt = MAX_MSG_SIZE;
229 
230         l_ptr->max_pkt_target = max_pkt;
231 	if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
232 		l_ptr->max_pkt = l_ptr->max_pkt_target;
233 	else
234 		l_ptr->max_pkt = MAX_PKT_DEFAULT;
235 
236         l_ptr->max_pkt_probes = 0;
237 }
238 
239 static u32 link_next_sent(struct link *l_ptr)
240 {
241 	if (l_ptr->next_out)
242 		return msg_seqno(buf_msg(l_ptr->next_out));
243 	return mod(l_ptr->next_out_no);
244 }
245 
246 static u32 link_last_sent(struct link *l_ptr)
247 {
248 	return mod(link_next_sent(l_ptr) - 1);
249 }
250 
251 /*
252  *  Simple non-static link routines (i.e. referenced outside this file)
253  */
254 
255 int tipc_link_is_up(struct link *l_ptr)
256 {
257 	if (!l_ptr)
258 		return 0;
259 	return (link_working_working(l_ptr) || link_working_unknown(l_ptr));
260 }
261 
262 int tipc_link_is_active(struct link *l_ptr)
263 {
264 	return ((l_ptr->owner->active_links[0] == l_ptr) ||
265 		(l_ptr->owner->active_links[1] == l_ptr));
266 }
267 
268 /**
269  * link_name_validate - validate & (optionally) deconstruct link name
270  * @name - ptr to link name string
271  * @name_parts - ptr to area for link name components (or NULL if not needed)
272  *
273  * Returns 1 if link name is valid, otherwise 0.
274  */
275 
276 static int link_name_validate(const char *name, struct link_name *name_parts)
277 {
278 	char name_copy[TIPC_MAX_LINK_NAME];
279 	char *addr_local;
280 	char *if_local;
281 	char *addr_peer;
282 	char *if_peer;
283 	char dummy;
284 	u32 z_local, c_local, n_local;
285 	u32 z_peer, c_peer, n_peer;
286 	u32 if_local_len;
287 	u32 if_peer_len;
288 
289 	/* copy link name & ensure length is OK */
290 
291 	name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
292 	/* need above in case non-Posix strncpy() doesn't pad with nulls */
293 	strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
294 	if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
295 		return 0;
296 
297 	/* ensure all component parts of link name are present */
298 
299 	addr_local = name_copy;
300 	if ((if_local = strchr(addr_local, ':')) == NULL)
301 		return 0;
302 	*(if_local++) = 0;
303 	if ((addr_peer = strchr(if_local, '-')) == NULL)
304 		return 0;
305 	*(addr_peer++) = 0;
306 	if_local_len = addr_peer - if_local;
307 	if ((if_peer = strchr(addr_peer, ':')) == NULL)
308 		return 0;
309 	*(if_peer++) = 0;
310 	if_peer_len = strlen(if_peer) + 1;
311 
312 	/* validate component parts of link name */
313 
314 	if ((sscanf(addr_local, "%u.%u.%u%c",
315 		    &z_local, &c_local, &n_local, &dummy) != 3) ||
316 	    (sscanf(addr_peer, "%u.%u.%u%c",
317 		    &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
318 	    (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
319 	    (z_peer  > 255) || (c_peer  > 4095) || (n_peer  > 4095) ||
320 	    (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
321 	    (if_peer_len  <= 1) || (if_peer_len  > TIPC_MAX_IF_NAME) ||
322 	    (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
323 	    (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
324 		return 0;
325 
326 	/* return link name components, if necessary */
327 
328 	if (name_parts) {
329 		name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
330 		strcpy(name_parts->if_local, if_local);
331 		name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
332 		strcpy(name_parts->if_peer, if_peer);
333 	}
334 	return 1;
335 }
336 
337 /**
338  * link_timeout - handle expiration of link timer
339  * @l_ptr: pointer to link
340  *
341  * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
342  * with tipc_link_delete().  (There is no risk that the node will be deleted by
343  * another thread because tipc_link_delete() always cancels the link timer before
344  * tipc_node_delete() is called.)
345  */
346 
347 static void link_timeout(struct link *l_ptr)
348 {
349 	tipc_node_lock(l_ptr->owner);
350 
351 	/* update counters used in statistical profiling of send traffic */
352 
353 	l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
354 	l_ptr->stats.queue_sz_counts++;
355 
356 	if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
357 		l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
358 
359 	if (l_ptr->first_out) {
360 		struct tipc_msg *msg = buf_msg(l_ptr->first_out);
361 		u32 length = msg_size(msg);
362 
363 		if ((msg_user(msg) == MSG_FRAGMENTER)
364 		    && (msg_type(msg) == FIRST_FRAGMENT)) {
365 			length = msg_size(msg_get_wrapped(msg));
366 		}
367 		if (length) {
368 			l_ptr->stats.msg_lengths_total += length;
369 			l_ptr->stats.msg_length_counts++;
370 			if (length <= 64)
371 				l_ptr->stats.msg_length_profile[0]++;
372 			else if (length <= 256)
373 				l_ptr->stats.msg_length_profile[1]++;
374 			else if (length <= 1024)
375 				l_ptr->stats.msg_length_profile[2]++;
376 			else if (length <= 4096)
377 				l_ptr->stats.msg_length_profile[3]++;
378 			else if (length <= 16384)
379 				l_ptr->stats.msg_length_profile[4]++;
380 			else if (length <= 32768)
381 				l_ptr->stats.msg_length_profile[5]++;
382 			else
383 				l_ptr->stats.msg_length_profile[6]++;
384 		}
385 	}
386 
387 	/* do all other link processing performed on a periodic basis */
388 
389 	link_check_defragm_bufs(l_ptr);
390 
391 	link_state_event(l_ptr, TIMEOUT_EVT);
392 
393 	if (l_ptr->next_out)
394 		tipc_link_push_queue(l_ptr);
395 
396 	tipc_node_unlock(l_ptr->owner);
397 }
398 
399 static void link_set_timer(struct link *l_ptr, u32 time)
400 {
401 	k_start_timer(&l_ptr->timer, time);
402 }
403 
404 /**
405  * tipc_link_create - create a new link
406  * @b_ptr: pointer to associated bearer
407  * @peer: network address of node at other end of link
408  * @media_addr: media address to use when sending messages over link
409  *
410  * Returns pointer to link.
411  */
412 
413 struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
414 			      const struct tipc_media_addr *media_addr)
415 {
416 	struct link *l_ptr;
417 	struct tipc_msg *msg;
418 	char *if_name;
419 
420 	l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
421 	if (!l_ptr) {
422 		warn("Link creation failed, no memory\n");
423 		return NULL;
424 	}
425 
426 	l_ptr->addr = peer;
427 	if_name = strchr(b_ptr->publ.name, ':') + 1;
428 	sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
429 		tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
430 		tipc_node(tipc_own_addr),
431 		if_name,
432 		tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
433 		/* note: peer i/f is appended to link name by reset/activate */
434 	memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
435 	k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
436 	list_add_tail(&l_ptr->link_list, &b_ptr->links);
437 	l_ptr->checkpoint = 1;
438 	l_ptr->b_ptr = b_ptr;
439 	link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
440 	l_ptr->state = RESET_UNKNOWN;
441 
442 	l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
443 	msg = l_ptr->pmsg;
444 	msg_init(msg, LINK_PROTOCOL, RESET_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
445 	msg_set_size(msg, sizeof(l_ptr->proto_msg));
446 	msg_set_session(msg, tipc_random);
447 	msg_set_bearer_id(msg, b_ptr->identity);
448 	strcpy((char *)msg_data(msg), if_name);
449 
450 	l_ptr->priority = b_ptr->priority;
451 	tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
452 
453 	link_init_max_pkt(l_ptr);
454 
455 	l_ptr->next_out_no = 1;
456 	INIT_LIST_HEAD(&l_ptr->waiting_ports);
457 
458 	link_reset_statistics(l_ptr);
459 
460 	l_ptr->owner = tipc_node_attach_link(l_ptr);
461 	if (!l_ptr->owner) {
462 		kfree(l_ptr);
463 		return NULL;
464 	}
465 
466 	if (LINK_LOG_BUF_SIZE) {
467 		char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC);
468 
469 		if (!pb) {
470 			kfree(l_ptr);
471 			warn("Link creation failed, no memory for print buffer\n");
472 			return NULL;
473 		}
474 		tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE);
475 	}
476 
477 	tipc_k_signal((Handler)tipc_link_start, (unsigned long)l_ptr);
478 
479 	dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n",
480 	    l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit);
481 
482 	return l_ptr;
483 }
484 
485 /**
486  * tipc_link_delete - delete a link
487  * @l_ptr: pointer to link
488  *
489  * Note: 'tipc_net_lock' is write_locked, bearer is locked.
490  * This routine must not grab the node lock until after link timer cancellation
491  * to avoid a potential deadlock situation.
492  */
493 
494 void tipc_link_delete(struct link *l_ptr)
495 {
496 	if (!l_ptr) {
497 		err("Attempt to delete non-existent link\n");
498 		return;
499 	}
500 
501 	dbg("tipc_link_delete()\n");
502 
503 	k_cancel_timer(&l_ptr->timer);
504 
505 	tipc_node_lock(l_ptr->owner);
506 	tipc_link_reset(l_ptr);
507 	tipc_node_detach_link(l_ptr->owner, l_ptr);
508 	tipc_link_stop(l_ptr);
509 	list_del_init(&l_ptr->link_list);
510 	if (LINK_LOG_BUF_SIZE)
511 		kfree(l_ptr->print_buf.buf);
512 	tipc_node_unlock(l_ptr->owner);
513 	k_term_timer(&l_ptr->timer);
514 	kfree(l_ptr);
515 }
516 
517 void tipc_link_start(struct link *l_ptr)
518 {
519 	dbg("tipc_link_start %x\n", l_ptr);
520 	link_state_event(l_ptr, STARTING_EVT);
521 }
522 
523 /**
524  * link_schedule_port - schedule port for deferred sending
525  * @l_ptr: pointer to link
526  * @origport: reference to sending port
527  * @sz: amount of data to be sent
528  *
529  * Schedules port for renewed sending of messages after link congestion
530  * has abated.
531  */
532 
533 static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
534 {
535 	struct port *p_ptr;
536 
537 	spin_lock_bh(&tipc_port_list_lock);
538 	p_ptr = tipc_port_lock(origport);
539 	if (p_ptr) {
540 		if (!p_ptr->wakeup)
541 			goto exit;
542 		if (!list_empty(&p_ptr->wait_list))
543 			goto exit;
544 		p_ptr->congested_link = l_ptr;
545 		p_ptr->publ.congested = 1;
546 		p_ptr->waiting_pkts = 1 + ((sz - 1) / link_max_pkt(l_ptr));
547 		list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
548 		l_ptr->stats.link_congs++;
549 exit:
550 		tipc_port_unlock(p_ptr);
551 	}
552 	spin_unlock_bh(&tipc_port_list_lock);
553 	return -ELINKCONG;
554 }
555 
556 void tipc_link_wakeup_ports(struct link *l_ptr, int all)
557 {
558 	struct port *p_ptr;
559 	struct port *temp_p_ptr;
560 	int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
561 
562 	if (all)
563 		win = 100000;
564 	if (win <= 0)
565 		return;
566 	if (!spin_trylock_bh(&tipc_port_list_lock))
567 		return;
568 	if (link_congested(l_ptr))
569 		goto exit;
570 	list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
571 				 wait_list) {
572 		if (win <= 0)
573 			break;
574 		list_del_init(&p_ptr->wait_list);
575 		p_ptr->congested_link = NULL;
576 		spin_lock_bh(p_ptr->publ.lock);
577 		p_ptr->publ.congested = 0;
578 		p_ptr->wakeup(&p_ptr->publ);
579 		win -= p_ptr->waiting_pkts;
580 		spin_unlock_bh(p_ptr->publ.lock);
581 	}
582 
583 exit:
584 	spin_unlock_bh(&tipc_port_list_lock);
585 }
586 
587 /**
588  * link_release_outqueue - purge link's outbound message queue
589  * @l_ptr: pointer to link
590  */
591 
592 static void link_release_outqueue(struct link *l_ptr)
593 {
594 	struct sk_buff *buf = l_ptr->first_out;
595 	struct sk_buff *next;
596 
597 	while (buf) {
598 		next = buf->next;
599 		buf_discard(buf);
600 		buf = next;
601 	}
602 	l_ptr->first_out = NULL;
603 	l_ptr->out_queue_size = 0;
604 }
605 
606 /**
607  * tipc_link_reset_fragments - purge link's inbound message fragments queue
608  * @l_ptr: pointer to link
609  */
610 
611 void tipc_link_reset_fragments(struct link *l_ptr)
612 {
613 	struct sk_buff *buf = l_ptr->defragm_buf;
614 	struct sk_buff *next;
615 
616 	while (buf) {
617 		next = buf->next;
618 		buf_discard(buf);
619 		buf = next;
620 	}
621 	l_ptr->defragm_buf = NULL;
622 }
623 
624 /**
625  * tipc_link_stop - purge all inbound and outbound messages associated with link
626  * @l_ptr: pointer to link
627  */
628 
629 void tipc_link_stop(struct link *l_ptr)
630 {
631 	struct sk_buff *buf;
632 	struct sk_buff *next;
633 
634 	buf = l_ptr->oldest_deferred_in;
635 	while (buf) {
636 		next = buf->next;
637 		buf_discard(buf);
638 		buf = next;
639 	}
640 
641 	buf = l_ptr->first_out;
642 	while (buf) {
643 		next = buf->next;
644 		buf_discard(buf);
645 		buf = next;
646 	}
647 
648 	tipc_link_reset_fragments(l_ptr);
649 
650 	buf_discard(l_ptr->proto_msg_queue);
651 	l_ptr->proto_msg_queue = NULL;
652 }
653 
654 #if 0
655 
656 /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
657 
658 static void link_recv_event(struct link_event *ev)
659 {
660 	ev->fcn(ev->addr, ev->name, ev->up);
661 	kfree(ev);
662 }
663 
664 static void link_send_event(void (*fcn)(u32 a, char *n, int up),
665 			    struct link *l_ptr, int up)
666 {
667 	struct link_event *ev;
668 
669 	ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
670 	if (!ev) {
671 		warn("Link event allocation failure\n");
672 		return;
673 	}
674 	ev->addr = l_ptr->addr;
675 	ev->up = up;
676 	ev->fcn = fcn;
677 	memcpy(ev->name, l_ptr->name, TIPC_MAX_LINK_NAME);
678 	tipc_k_signal((Handler)link_recv_event, (unsigned long)ev);
679 }
680 
681 #else
682 
683 #define link_send_event(fcn, l_ptr, up) do { } while (0)
684 
685 #endif
686 
687 void tipc_link_reset(struct link *l_ptr)
688 {
689 	struct sk_buff *buf;
690 	u32 prev_state = l_ptr->state;
691 	u32 checkpoint = l_ptr->next_in_no;
692 	int was_active_link = tipc_link_is_active(l_ptr);
693 
694 	msg_set_session(l_ptr->pmsg, msg_session(l_ptr->pmsg) + 1);
695 
696         /* Link is down, accept any session: */
697 	l_ptr->peer_session = 0;
698 
699         /* Prepare for max packet size negotiation */
700 	link_init_max_pkt(l_ptr);
701 
702 	l_ptr->state = RESET_UNKNOWN;
703 	dbg_link_state("Resetting Link\n");
704 
705 	if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
706 		return;
707 
708 	tipc_node_link_down(l_ptr->owner, l_ptr);
709 	tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
710 #if 0
711 	tipc_printf(TIPC_CONS, "\nReset link <%s>\n", l_ptr->name);
712 	dbg_link_dump();
713 #endif
714 	if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
715 	    l_ptr->owner->permit_changeover) {
716 		l_ptr->reset_checkpoint = checkpoint;
717 		l_ptr->exp_msg_count = START_CHANGEOVER;
718 	}
719 
720 	/* Clean up all queues: */
721 
722 	link_release_outqueue(l_ptr);
723 	buf_discard(l_ptr->proto_msg_queue);
724 	l_ptr->proto_msg_queue = NULL;
725 	buf = l_ptr->oldest_deferred_in;
726 	while (buf) {
727 		struct sk_buff *next = buf->next;
728 		buf_discard(buf);
729 		buf = next;
730 	}
731 	if (!list_empty(&l_ptr->waiting_ports))
732 		tipc_link_wakeup_ports(l_ptr, 1);
733 
734 	l_ptr->retransm_queue_head = 0;
735 	l_ptr->retransm_queue_size = 0;
736 	l_ptr->last_out = NULL;
737 	l_ptr->first_out = NULL;
738 	l_ptr->next_out = NULL;
739 	l_ptr->unacked_window = 0;
740 	l_ptr->checkpoint = 1;
741 	l_ptr->next_out_no = 1;
742 	l_ptr->deferred_inqueue_sz = 0;
743 	l_ptr->oldest_deferred_in = NULL;
744 	l_ptr->newest_deferred_in = NULL;
745 	l_ptr->fsm_msg_cnt = 0;
746 	l_ptr->stale_count = 0;
747 	link_reset_statistics(l_ptr);
748 
749 	link_send_event(tipc_cfg_link_event, l_ptr, 0);
750 	if (!in_own_cluster(l_ptr->addr))
751 		link_send_event(tipc_disc_link_event, l_ptr, 0);
752 }
753 
754 
755 static void link_activate(struct link *l_ptr)
756 {
757 	l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
758 	tipc_node_link_up(l_ptr->owner, l_ptr);
759 	tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
760 	link_send_event(tipc_cfg_link_event, l_ptr, 1);
761 	if (!in_own_cluster(l_ptr->addr))
762 		link_send_event(tipc_disc_link_event, l_ptr, 1);
763 }
764 
765 /**
766  * link_state_event - link finite state machine
767  * @l_ptr: pointer to link
768  * @event: state machine event to process
769  */
770 
771 static void link_state_event(struct link *l_ptr, unsigned event)
772 {
773 	struct link *other;
774 	u32 cont_intv = l_ptr->continuity_interval;
775 
776 	if (!l_ptr->started && (event != STARTING_EVT))
777 		return;		/* Not yet. */
778 
779 	if (link_blocked(l_ptr)) {
780 		if (event == TIMEOUT_EVT) {
781 			link_set_timer(l_ptr, cont_intv);
782 		}
783 		return;	  /* Changeover going on */
784 	}
785 	dbg_link("STATE_EV: <%s> ", l_ptr->name);
786 
787 	switch (l_ptr->state) {
788 	case WORKING_WORKING:
789 		dbg_link("WW/");
790 		switch (event) {
791 		case TRAFFIC_MSG_EVT:
792 			dbg_link("TRF-");
793 			/* fall through */
794 		case ACTIVATE_MSG:
795 			dbg_link("ACT\n");
796 			break;
797 		case TIMEOUT_EVT:
798 			dbg_link("TIM ");
799 			if (l_ptr->next_in_no != l_ptr->checkpoint) {
800 				l_ptr->checkpoint = l_ptr->next_in_no;
801 				if (tipc_bclink_acks_missing(l_ptr->owner)) {
802 					tipc_link_send_proto_msg(l_ptr, STATE_MSG,
803 								 0, 0, 0, 0, 0);
804 					l_ptr->fsm_msg_cnt++;
805 				} else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
806 					tipc_link_send_proto_msg(l_ptr, STATE_MSG,
807 								 1, 0, 0, 0, 0);
808 					l_ptr->fsm_msg_cnt++;
809 				}
810 				link_set_timer(l_ptr, cont_intv);
811 				break;
812 			}
813 			dbg_link(" -> WU\n");
814 			l_ptr->state = WORKING_UNKNOWN;
815 			l_ptr->fsm_msg_cnt = 0;
816 			tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
817 			l_ptr->fsm_msg_cnt++;
818 			link_set_timer(l_ptr, cont_intv / 4);
819 			break;
820 		case RESET_MSG:
821 			dbg_link("RES -> RR\n");
822 			info("Resetting link <%s>, requested by peer\n",
823 			     l_ptr->name);
824 			tipc_link_reset(l_ptr);
825 			l_ptr->state = RESET_RESET;
826 			l_ptr->fsm_msg_cnt = 0;
827 			tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
828 			l_ptr->fsm_msg_cnt++;
829 			link_set_timer(l_ptr, cont_intv);
830 			break;
831 		default:
832 			err("Unknown link event %u in WW state\n", event);
833 		}
834 		break;
835 	case WORKING_UNKNOWN:
836 		dbg_link("WU/");
837 		switch (event) {
838 		case TRAFFIC_MSG_EVT:
839 			dbg_link("TRF-");
840 		case ACTIVATE_MSG:
841 			dbg_link("ACT -> WW\n");
842 			l_ptr->state = WORKING_WORKING;
843 			l_ptr->fsm_msg_cnt = 0;
844 			link_set_timer(l_ptr, cont_intv);
845 			break;
846 		case RESET_MSG:
847 			dbg_link("RES -> RR\n");
848 			info("Resetting link <%s>, requested by peer "
849 			     "while probing\n", l_ptr->name);
850 			tipc_link_reset(l_ptr);
851 			l_ptr->state = RESET_RESET;
852 			l_ptr->fsm_msg_cnt = 0;
853 			tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
854 			l_ptr->fsm_msg_cnt++;
855 			link_set_timer(l_ptr, cont_intv);
856 			break;
857 		case TIMEOUT_EVT:
858 			dbg_link("TIM ");
859 			if (l_ptr->next_in_no != l_ptr->checkpoint) {
860 				dbg_link("-> WW \n");
861 				l_ptr->state = WORKING_WORKING;
862 				l_ptr->fsm_msg_cnt = 0;
863 				l_ptr->checkpoint = l_ptr->next_in_no;
864 				if (tipc_bclink_acks_missing(l_ptr->owner)) {
865 					tipc_link_send_proto_msg(l_ptr, STATE_MSG,
866 								 0, 0, 0, 0, 0);
867 					l_ptr->fsm_msg_cnt++;
868 				}
869 				link_set_timer(l_ptr, cont_intv);
870 			} else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
871 				dbg_link("Probing %u/%u,timer = %u ms)\n",
872 					 l_ptr->fsm_msg_cnt, l_ptr->abort_limit,
873 					 cont_intv / 4);
874 				tipc_link_send_proto_msg(l_ptr, STATE_MSG,
875 							 1, 0, 0, 0, 0);
876 				l_ptr->fsm_msg_cnt++;
877 				link_set_timer(l_ptr, cont_intv / 4);
878 			} else {	/* Link has failed */
879 				dbg_link("-> RU (%u probes unanswered)\n",
880 					 l_ptr->fsm_msg_cnt);
881 				warn("Resetting link <%s>, peer not responding\n",
882 				     l_ptr->name);
883 				tipc_link_reset(l_ptr);
884 				l_ptr->state = RESET_UNKNOWN;
885 				l_ptr->fsm_msg_cnt = 0;
886 				tipc_link_send_proto_msg(l_ptr, RESET_MSG,
887 							 0, 0, 0, 0, 0);
888 				l_ptr->fsm_msg_cnt++;
889 				link_set_timer(l_ptr, cont_intv);
890 			}
891 			break;
892 		default:
893 			err("Unknown link event %u in WU state\n", event);
894 		}
895 		break;
896 	case RESET_UNKNOWN:
897 		dbg_link("RU/");
898 		switch (event) {
899 		case TRAFFIC_MSG_EVT:
900 			dbg_link("TRF-\n");
901 			break;
902 		case ACTIVATE_MSG:
903 			other = l_ptr->owner->active_links[0];
904 			if (other && link_working_unknown(other)) {
905 				dbg_link("ACT\n");
906 				break;
907 			}
908 			dbg_link("ACT -> WW\n");
909 			l_ptr->state = WORKING_WORKING;
910 			l_ptr->fsm_msg_cnt = 0;
911 			link_activate(l_ptr);
912 			tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
913 			l_ptr->fsm_msg_cnt++;
914 			link_set_timer(l_ptr, cont_intv);
915 			break;
916 		case RESET_MSG:
917 			dbg_link("RES \n");
918 			dbg_link(" -> RR\n");
919 			l_ptr->state = RESET_RESET;
920 			l_ptr->fsm_msg_cnt = 0;
921 			tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
922 			l_ptr->fsm_msg_cnt++;
923 			link_set_timer(l_ptr, cont_intv);
924 			break;
925 		case STARTING_EVT:
926 			dbg_link("START-");
927 			l_ptr->started = 1;
928 			/* fall through */
929 		case TIMEOUT_EVT:
930 			dbg_link("TIM \n");
931 			tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
932 			l_ptr->fsm_msg_cnt++;
933 			link_set_timer(l_ptr, cont_intv);
934 			break;
935 		default:
936 			err("Unknown link event %u in RU state\n", event);
937 		}
938 		break;
939 	case RESET_RESET:
940 		dbg_link("RR/ ");
941 		switch (event) {
942 		case TRAFFIC_MSG_EVT:
943 			dbg_link("TRF-");
944 			/* fall through */
945 		case ACTIVATE_MSG:
946 			other = l_ptr->owner->active_links[0];
947 			if (other && link_working_unknown(other)) {
948 				dbg_link("ACT\n");
949 				break;
950 			}
951 			dbg_link("ACT -> WW\n");
952 			l_ptr->state = WORKING_WORKING;
953 			l_ptr->fsm_msg_cnt = 0;
954 			link_activate(l_ptr);
955 			tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
956 			l_ptr->fsm_msg_cnt++;
957 			link_set_timer(l_ptr, cont_intv);
958 			break;
959 		case RESET_MSG:
960 			dbg_link("RES\n");
961 			break;
962 		case TIMEOUT_EVT:
963 			dbg_link("TIM\n");
964 			tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
965 			l_ptr->fsm_msg_cnt++;
966 			link_set_timer(l_ptr, cont_intv);
967 			dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt);
968 			break;
969 		default:
970 			err("Unknown link event %u in RR state\n", event);
971 		}
972 		break;
973 	default:
974 		err("Unknown link state %u/%u\n", l_ptr->state, event);
975 	}
976 }
977 
978 /*
979  * link_bundle_buf(): Append contents of a buffer to
980  * the tail of an existing one.
981  */
982 
983 static int link_bundle_buf(struct link *l_ptr,
984 			   struct sk_buff *bundler,
985 			   struct sk_buff *buf)
986 {
987 	struct tipc_msg *bundler_msg = buf_msg(bundler);
988 	struct tipc_msg *msg = buf_msg(buf);
989 	u32 size = msg_size(msg);
990 	u32 bundle_size = msg_size(bundler_msg);
991 	u32 to_pos = align(bundle_size);
992 	u32 pad = to_pos - bundle_size;
993 
994 	if (msg_user(bundler_msg) != MSG_BUNDLER)
995 		return 0;
996 	if (msg_type(bundler_msg) != OPEN_MSG)
997 		return 0;
998 	if (skb_tailroom(bundler) < (pad + size))
999 		return 0;
1000 	if (link_max_pkt(l_ptr) < (to_pos + size))
1001 		return 0;
1002 
1003 	skb_put(bundler, pad + size);
1004 	memcpy(bundler->data + to_pos, buf->data, size);
1005 	msg_set_size(bundler_msg, to_pos + size);
1006 	msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
1007 	dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n",
1008 	    msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg));
1009 	msg_dbg(msg, "PACKD:");
1010 	buf_discard(buf);
1011 	l_ptr->stats.sent_bundled++;
1012 	return 1;
1013 }
1014 
1015 static void link_add_to_outqueue(struct link *l_ptr,
1016 				 struct sk_buff *buf,
1017 				 struct tipc_msg *msg)
1018 {
1019 	u32 ack = mod(l_ptr->next_in_no - 1);
1020 	u32 seqno = mod(l_ptr->next_out_no++);
1021 
1022 	msg_set_word(msg, 2, ((ack << 16) | seqno));
1023 	msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1024 	buf->next = NULL;
1025 	if (l_ptr->first_out) {
1026 		l_ptr->last_out->next = buf;
1027 		l_ptr->last_out = buf;
1028 	} else
1029 		l_ptr->first_out = l_ptr->last_out = buf;
1030 	l_ptr->out_queue_size++;
1031 }
1032 
1033 /*
1034  * tipc_link_send_buf() is the 'full path' for messages, called from
1035  * inside TIPC when the 'fast path' in tipc_send_buf
1036  * has failed, and from link_send()
1037  */
1038 
1039 int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
1040 {
1041 	struct tipc_msg *msg = buf_msg(buf);
1042 	u32 size = msg_size(msg);
1043 	u32 dsz = msg_data_sz(msg);
1044 	u32 queue_size = l_ptr->out_queue_size;
1045 	u32 imp = msg_tot_importance(msg);
1046 	u32 queue_limit = l_ptr->queue_limit[imp];
1047 	u32 max_packet = link_max_pkt(l_ptr);
1048 
1049 	msg_set_prevnode(msg, tipc_own_addr);	/* If routed message */
1050 
1051 	/* Match msg importance against queue limits: */
1052 
1053 	if (unlikely(queue_size >= queue_limit)) {
1054 		if (imp <= TIPC_CRITICAL_IMPORTANCE) {
1055 			return link_schedule_port(l_ptr, msg_origport(msg),
1056 						  size);
1057 		}
1058 		msg_dbg(msg, "TIPC: Congestion, throwing away\n");
1059 		buf_discard(buf);
1060 		if (imp > CONN_MANAGER) {
1061 			warn("Resetting link <%s>, send queue full", l_ptr->name);
1062 			tipc_link_reset(l_ptr);
1063 		}
1064 		return dsz;
1065 	}
1066 
1067 	/* Fragmentation needed ? */
1068 
1069 	if (size > max_packet)
1070 		return tipc_link_send_long_buf(l_ptr, buf);
1071 
1072 	/* Packet can be queued or sent: */
1073 
1074 	if (queue_size > l_ptr->stats.max_queue_sz)
1075 		l_ptr->stats.max_queue_sz = queue_size;
1076 
1077 	if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
1078 		   !link_congested(l_ptr))) {
1079 		link_add_to_outqueue(l_ptr, buf, msg);
1080 
1081 		if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
1082 			l_ptr->unacked_window = 0;
1083 		} else {
1084 			tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1085 			l_ptr->stats.bearer_congs++;
1086 			l_ptr->next_out = buf;
1087 		}
1088 		return dsz;
1089 	}
1090 	/* Congestion: can message be bundled ?: */
1091 
1092 	if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
1093 	    (msg_user(msg) != MSG_FRAGMENTER)) {
1094 
1095 		/* Try adding message to an existing bundle */
1096 
1097 		if (l_ptr->next_out &&
1098 		    link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
1099 			tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
1100 			return dsz;
1101 		}
1102 
1103 		/* Try creating a new bundle */
1104 
1105 		if (size <= max_packet * 2 / 3) {
1106 			struct sk_buff *bundler = buf_acquire(max_packet);
1107 			struct tipc_msg bundler_hdr;
1108 
1109 			if (bundler) {
1110 				msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
1111 					 TIPC_OK, INT_H_SIZE, l_ptr->addr);
1112 				memcpy(bundler->data, (unchar *)&bundler_hdr,
1113 				       INT_H_SIZE);
1114 				skb_trim(bundler, INT_H_SIZE);
1115 				link_bundle_buf(l_ptr, bundler, buf);
1116 				buf = bundler;
1117 				msg = buf_msg(buf);
1118 				l_ptr->stats.sent_bundles++;
1119 			}
1120 		}
1121 	}
1122 	if (!l_ptr->next_out)
1123 		l_ptr->next_out = buf;
1124 	link_add_to_outqueue(l_ptr, buf, msg);
1125 	tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
1126 	return dsz;
1127 }
1128 
1129 /*
1130  * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
1131  * not been selected yet, and the the owner node is not locked
1132  * Called by TIPC internal users, e.g. the name distributor
1133  */
1134 
1135 int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
1136 {
1137 	struct link *l_ptr;
1138 	struct node *n_ptr;
1139 	int res = -ELINKCONG;
1140 
1141 	read_lock_bh(&tipc_net_lock);
1142 	n_ptr = tipc_node_select(dest, selector);
1143 	if (n_ptr) {
1144 		tipc_node_lock(n_ptr);
1145 		l_ptr = n_ptr->active_links[selector & 1];
1146 		if (l_ptr) {
1147 			dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest);
1148 			res = tipc_link_send_buf(l_ptr, buf);
1149 		} else {
1150 			dbg("Attempt to send msg to unreachable node:\n");
1151 			msg_dbg(buf_msg(buf),">>>");
1152 			buf_discard(buf);
1153 		}
1154 		tipc_node_unlock(n_ptr);
1155 	} else {
1156 		dbg("Attempt to send msg to unknown node:\n");
1157 		msg_dbg(buf_msg(buf),">>>");
1158 		buf_discard(buf);
1159 	}
1160 	read_unlock_bh(&tipc_net_lock);
1161 	return res;
1162 }
1163 
1164 /*
1165  * link_send_buf_fast: Entry for data messages where the
1166  * destination link is known and the header is complete,
1167  * inclusive total message length. Very time critical.
1168  * Link is locked. Returns user data length.
1169  */
1170 
1171 static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
1172 			      u32 *used_max_pkt)
1173 {
1174 	struct tipc_msg *msg = buf_msg(buf);
1175 	int res = msg_data_sz(msg);
1176 
1177 	if (likely(!link_congested(l_ptr))) {
1178 		if (likely(msg_size(msg) <= link_max_pkt(l_ptr))) {
1179 			if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
1180 				link_add_to_outqueue(l_ptr, buf, msg);
1181 				if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
1182 							    &l_ptr->media_addr))) {
1183 					l_ptr->unacked_window = 0;
1184 					msg_dbg(msg,"SENT_FAST:");
1185 					return res;
1186 				}
1187 				dbg("failed sent fast...\n");
1188 				tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1189 				l_ptr->stats.bearer_congs++;
1190 				l_ptr->next_out = buf;
1191 				return res;
1192 			}
1193 		}
1194 		else
1195 			*used_max_pkt = link_max_pkt(l_ptr);
1196 	}
1197 	return tipc_link_send_buf(l_ptr, buf);  /* All other cases */
1198 }
1199 
1200 /*
1201  * tipc_send_buf_fast: Entry for data messages where the
1202  * destination node is known and the header is complete,
1203  * inclusive total message length.
1204  * Returns user data length.
1205  */
1206 int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
1207 {
1208 	struct link *l_ptr;
1209 	struct node *n_ptr;
1210 	int res;
1211 	u32 selector = msg_origport(buf_msg(buf)) & 1;
1212 	u32 dummy;
1213 
1214 	if (destnode == tipc_own_addr)
1215 		return tipc_port_recv_msg(buf);
1216 
1217 	read_lock_bh(&tipc_net_lock);
1218 	n_ptr = tipc_node_select(destnode, selector);
1219 	if (likely(n_ptr)) {
1220 		tipc_node_lock(n_ptr);
1221 		l_ptr = n_ptr->active_links[selector];
1222 		dbg("send_fast: buf %x selected %x, destnode = %x\n",
1223 		    buf, l_ptr, destnode);
1224 		if (likely(l_ptr)) {
1225 			res = link_send_buf_fast(l_ptr, buf, &dummy);
1226 			tipc_node_unlock(n_ptr);
1227 			read_unlock_bh(&tipc_net_lock);
1228 			return res;
1229 		}
1230 		tipc_node_unlock(n_ptr);
1231 	}
1232 	read_unlock_bh(&tipc_net_lock);
1233 	res = msg_data_sz(buf_msg(buf));
1234 	tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1235 	return res;
1236 }
1237 
1238 
1239 /*
1240  * tipc_link_send_sections_fast: Entry for messages where the
1241  * destination processor is known and the header is complete,
1242  * except for total message length.
1243  * Returns user data length or errno.
1244  */
1245 int tipc_link_send_sections_fast(struct port *sender,
1246 				 struct iovec const *msg_sect,
1247 				 const u32 num_sect,
1248 				 u32 destaddr)
1249 {
1250 	struct tipc_msg *hdr = &sender->publ.phdr;
1251 	struct link *l_ptr;
1252 	struct sk_buff *buf;
1253 	struct node *node;
1254 	int res;
1255 	u32 selector = msg_origport(hdr) & 1;
1256 
1257 again:
1258 	/*
1259 	 * Try building message using port's max_pkt hint.
1260 	 * (Must not hold any locks while building message.)
1261 	 */
1262 
1263 	res = msg_build(hdr, msg_sect, num_sect, sender->max_pkt,
1264 			!sender->user_port, &buf);
1265 
1266 	read_lock_bh(&tipc_net_lock);
1267 	node = tipc_node_select(destaddr, selector);
1268 	if (likely(node)) {
1269 		tipc_node_lock(node);
1270 		l_ptr = node->active_links[selector];
1271 		if (likely(l_ptr)) {
1272 			if (likely(buf)) {
1273 				res = link_send_buf_fast(l_ptr, buf,
1274 							 &sender->max_pkt);
1275 				if (unlikely(res < 0))
1276 					buf_discard(buf);
1277 exit:
1278 				tipc_node_unlock(node);
1279 				read_unlock_bh(&tipc_net_lock);
1280 				return res;
1281 			}
1282 
1283 			/* Exit if build request was invalid */
1284 
1285 			if (unlikely(res < 0))
1286 				goto exit;
1287 
1288 			/* Exit if link (or bearer) is congested */
1289 
1290 			if (link_congested(l_ptr) ||
1291 			    !list_empty(&l_ptr->b_ptr->cong_links)) {
1292 				res = link_schedule_port(l_ptr,
1293 							 sender->publ.ref, res);
1294 				goto exit;
1295 			}
1296 
1297 			/*
1298 			 * Message size exceeds max_pkt hint; update hint,
1299 			 * then re-try fast path or fragment the message
1300 			 */
1301 
1302 			sender->max_pkt = link_max_pkt(l_ptr);
1303 			tipc_node_unlock(node);
1304 			read_unlock_bh(&tipc_net_lock);
1305 
1306 
1307 			if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
1308 				goto again;
1309 
1310 			return link_send_sections_long(sender, msg_sect,
1311 						       num_sect, destaddr);
1312 		}
1313 		tipc_node_unlock(node);
1314 	}
1315 	read_unlock_bh(&tipc_net_lock);
1316 
1317 	/* Couldn't find a link to the destination node */
1318 
1319 	if (buf)
1320 		return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1321 	if (res >= 0)
1322 		return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1323 						 TIPC_ERR_NO_NODE);
1324 	return res;
1325 }
1326 
1327 /*
1328  * link_send_sections_long(): Entry for long messages where the
1329  * destination node is known and the header is complete,
1330  * inclusive total message length.
1331  * Link and bearer congestion status have been checked to be ok,
1332  * and are ignored if they change.
1333  *
1334  * Note that fragments do not use the full link MTU so that they won't have
1335  * to undergo refragmentation if link changeover causes them to be sent
1336  * over another link with an additional tunnel header added as prefix.
1337  * (Refragmentation will still occur if the other link has a smaller MTU.)
1338  *
1339  * Returns user data length or errno.
1340  */
1341 static int link_send_sections_long(struct port *sender,
1342 				   struct iovec const *msg_sect,
1343 				   u32 num_sect,
1344 				   u32 destaddr)
1345 {
1346 	struct link *l_ptr;
1347 	struct node *node;
1348 	struct tipc_msg *hdr = &sender->publ.phdr;
1349 	u32 dsz = msg_data_sz(hdr);
1350 	u32 max_pkt,fragm_sz,rest;
1351 	struct tipc_msg fragm_hdr;
1352 	struct sk_buff *buf,*buf_chain,*prev;
1353 	u32 fragm_crs,fragm_rest,hsz,sect_rest;
1354 	const unchar *sect_crs;
1355 	int curr_sect;
1356 	u32 fragm_no;
1357 
1358 again:
1359 	fragm_no = 1;
1360 	max_pkt = sender->max_pkt - INT_H_SIZE;
1361 		/* leave room for tunnel header in case of link changeover */
1362 	fragm_sz = max_pkt - INT_H_SIZE;
1363 		/* leave room for fragmentation header in each fragment */
1364 	rest = dsz;
1365 	fragm_crs = 0;
1366 	fragm_rest = 0;
1367 	sect_rest = 0;
1368 	sect_crs = NULL;
1369 	curr_sect = -1;
1370 
1371 	/* Prepare reusable fragment header: */
1372 
1373 	msg_dbg(hdr, ">FRAGMENTING>");
1374 	msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
1375 		 TIPC_OK, INT_H_SIZE, msg_destnode(hdr));
1376 	msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1377 	msg_set_size(&fragm_hdr, max_pkt);
1378 	msg_set_fragm_no(&fragm_hdr, 1);
1379 
1380 	/* Prepare header of first fragment: */
1381 
1382 	buf_chain = buf = buf_acquire(max_pkt);
1383 	if (!buf)
1384 		return -ENOMEM;
1385 	buf->next = NULL;
1386 	memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE);
1387 	hsz = msg_hdr_sz(hdr);
1388 	memcpy(buf->data + INT_H_SIZE, (unchar *)hdr, hsz);
1389 	msg_dbg(buf_msg(buf), ">BUILD>");
1390 
1391 	/* Chop up message: */
1392 
1393 	fragm_crs = INT_H_SIZE + hsz;
1394 	fragm_rest = fragm_sz - hsz;
1395 
1396 	do {		/* For all sections */
1397 		u32 sz;
1398 
1399 		if (!sect_rest) {
1400 			sect_rest = msg_sect[++curr_sect].iov_len;
1401 			sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1402 		}
1403 
1404 		if (sect_rest < fragm_rest)
1405 			sz = sect_rest;
1406 		else
1407 			sz = fragm_rest;
1408 
1409 		if (likely(!sender->user_port)) {
1410 			if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1411 error:
1412 				for (; buf_chain; buf_chain = buf) {
1413 					buf = buf_chain->next;
1414 					buf_discard(buf_chain);
1415 				}
1416 				return -EFAULT;
1417 			}
1418 		} else
1419 			memcpy(buf->data + fragm_crs, sect_crs, sz);
1420 
1421 		sect_crs += sz;
1422 		sect_rest -= sz;
1423 		fragm_crs += sz;
1424 		fragm_rest -= sz;
1425 		rest -= sz;
1426 
1427 		if (!fragm_rest && rest) {
1428 
1429 			/* Initiate new fragment: */
1430 			if (rest <= fragm_sz) {
1431 				fragm_sz = rest;
1432 				msg_set_type(&fragm_hdr,LAST_FRAGMENT);
1433 			} else {
1434 				msg_set_type(&fragm_hdr, FRAGMENT);
1435 			}
1436 			msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1437 			msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1438 			prev = buf;
1439 			buf = buf_acquire(fragm_sz + INT_H_SIZE);
1440 			if (!buf)
1441 				goto error;
1442 
1443 			buf->next = NULL;
1444 			prev->next = buf;
1445 			memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE);
1446 			fragm_crs = INT_H_SIZE;
1447 			fragm_rest = fragm_sz;
1448 			msg_dbg(buf_msg(buf),"  >BUILD>");
1449 		}
1450 	}
1451 	while (rest > 0);
1452 
1453 	/*
1454 	 * Now we have a buffer chain. Select a link and check
1455 	 * that packet size is still OK
1456 	 */
1457 	node = tipc_node_select(destaddr, sender->publ.ref & 1);
1458 	if (likely(node)) {
1459 		tipc_node_lock(node);
1460 		l_ptr = node->active_links[sender->publ.ref & 1];
1461 		if (!l_ptr) {
1462 			tipc_node_unlock(node);
1463 			goto reject;
1464 		}
1465 		if (link_max_pkt(l_ptr) < max_pkt) {
1466 			sender->max_pkt = link_max_pkt(l_ptr);
1467 			tipc_node_unlock(node);
1468 			for (; buf_chain; buf_chain = buf) {
1469 				buf = buf_chain->next;
1470 				buf_discard(buf_chain);
1471 			}
1472 			goto again;
1473 		}
1474 	} else {
1475 reject:
1476 		for (; buf_chain; buf_chain = buf) {
1477 			buf = buf_chain->next;
1478 			buf_discard(buf_chain);
1479 		}
1480 		return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1481 						 TIPC_ERR_NO_NODE);
1482 	}
1483 
1484 	/* Append whole chain to send queue: */
1485 
1486 	buf = buf_chain;
1487 	l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1488 	if (!l_ptr->next_out)
1489 		l_ptr->next_out = buf_chain;
1490 	l_ptr->stats.sent_fragmented++;
1491 	while (buf) {
1492 		struct sk_buff *next = buf->next;
1493 		struct tipc_msg *msg = buf_msg(buf);
1494 
1495 		l_ptr->stats.sent_fragments++;
1496 		msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1497 		link_add_to_outqueue(l_ptr, buf, msg);
1498 		msg_dbg(msg, ">ADD>");
1499 		buf = next;
1500 	}
1501 
1502 	/* Send it, if possible: */
1503 
1504 	tipc_link_push_queue(l_ptr);
1505 	tipc_node_unlock(node);
1506 	return dsz;
1507 }
1508 
1509 /*
1510  * tipc_link_push_packet: Push one unsent packet to the media
1511  */
1512 u32 tipc_link_push_packet(struct link *l_ptr)
1513 {
1514 	struct sk_buff *buf = l_ptr->first_out;
1515 	u32 r_q_size = l_ptr->retransm_queue_size;
1516 	u32 r_q_head = l_ptr->retransm_queue_head;
1517 
1518 	/* Step to position where retransmission failed, if any,    */
1519 	/* consider that buffers may have been released in meantime */
1520 
1521 	if (r_q_size && buf) {
1522 		u32 last = lesser(mod(r_q_head + r_q_size),
1523 				  link_last_sent(l_ptr));
1524 		u32 first = msg_seqno(buf_msg(buf));
1525 
1526 		while (buf && less(first, r_q_head)) {
1527 			first = mod(first + 1);
1528 			buf = buf->next;
1529 		}
1530 		l_ptr->retransm_queue_head = r_q_head = first;
1531 		l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1532 	}
1533 
1534 	/* Continue retransmission now, if there is anything: */
1535 
1536 	if (r_q_size && buf && !skb_cloned(buf)) {
1537 		msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1538 		msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
1539 		if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1540 			msg_dbg(buf_msg(buf), ">DEF-RETR>");
1541 			l_ptr->retransm_queue_head = mod(++r_q_head);
1542 			l_ptr->retransm_queue_size = --r_q_size;
1543 			l_ptr->stats.retransmitted++;
1544 			return TIPC_OK;
1545 		} else {
1546 			l_ptr->stats.bearer_congs++;
1547 			msg_dbg(buf_msg(buf), "|>DEF-RETR>");
1548 			return PUSH_FAILED;
1549 		}
1550 	}
1551 
1552 	/* Send deferred protocol message, if any: */
1553 
1554 	buf = l_ptr->proto_msg_queue;
1555 	if (buf) {
1556 		msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1557 		msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in);
1558 		if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1559 			msg_dbg(buf_msg(buf), ">DEF-PROT>");
1560 			l_ptr->unacked_window = 0;
1561 			buf_discard(buf);
1562 			l_ptr->proto_msg_queue = NULL;
1563 			return TIPC_OK;
1564 		} else {
1565 			msg_dbg(buf_msg(buf), "|>DEF-PROT>");
1566 			l_ptr->stats.bearer_congs++;
1567 			return PUSH_FAILED;
1568 		}
1569 	}
1570 
1571 	/* Send one deferred data message, if send window not full: */
1572 
1573 	buf = l_ptr->next_out;
1574 	if (buf) {
1575 		struct tipc_msg *msg = buf_msg(buf);
1576 		u32 next = msg_seqno(msg);
1577 		u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1578 
1579 		if (mod(next - first) < l_ptr->queue_limit[0]) {
1580 			msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1581 			msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1582 			if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1583 				if (msg_user(msg) == MSG_BUNDLER)
1584 					msg_set_type(msg, CLOSED_MSG);
1585 				msg_dbg(msg, ">PUSH-DATA>");
1586 				l_ptr->next_out = buf->next;
1587 				return TIPC_OK;
1588 			} else {
1589 				msg_dbg(msg, "|PUSH-DATA|");
1590 				l_ptr->stats.bearer_congs++;
1591 				return PUSH_FAILED;
1592 			}
1593 		}
1594 	}
1595 	return PUSH_FINISHED;
1596 }
1597 
1598 /*
1599  * push_queue(): push out the unsent messages of a link where
1600  *               congestion has abated. Node is locked
1601  */
1602 void tipc_link_push_queue(struct link *l_ptr)
1603 {
1604 	u32 res;
1605 
1606 	if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
1607 		return;
1608 
1609 	do {
1610 		res = tipc_link_push_packet(l_ptr);
1611 	}
1612 	while (res == TIPC_OK);
1613 	if (res == PUSH_FAILED)
1614 		tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1615 }
1616 
1617 static void link_reset_all(unsigned long addr)
1618 {
1619 	struct node *n_ptr;
1620 	char addr_string[16];
1621 	u32 i;
1622 
1623 	read_lock_bh(&tipc_net_lock);
1624 	n_ptr = tipc_node_find((u32)addr);
1625 	if (!n_ptr) {
1626 		read_unlock_bh(&tipc_net_lock);
1627 		return;	/* node no longer exists */
1628 	}
1629 
1630 	tipc_node_lock(n_ptr);
1631 
1632 	warn("Resetting all links to %s\n",
1633 	     addr_string_fill(addr_string, n_ptr->addr));
1634 
1635 	for (i = 0; i < MAX_BEARERS; i++) {
1636 		if (n_ptr->links[i]) {
1637 			link_print(n_ptr->links[i], TIPC_OUTPUT,
1638 				   "Resetting link\n");
1639 			tipc_link_reset(n_ptr->links[i]);
1640 		}
1641 	}
1642 
1643 	tipc_node_unlock(n_ptr);
1644 	read_unlock_bh(&tipc_net_lock);
1645 }
1646 
1647 static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1648 {
1649 	struct tipc_msg *msg = buf_msg(buf);
1650 
1651 	warn("Retransmission failure on link <%s>\n", l_ptr->name);
1652 	tipc_msg_print(TIPC_OUTPUT, msg, ">RETR-FAIL>");
1653 
1654 	if (l_ptr->addr) {
1655 
1656 		/* Handle failure on standard link */
1657 
1658 		link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n");
1659 		tipc_link_reset(l_ptr);
1660 
1661 	} else {
1662 
1663 		/* Handle failure on broadcast link */
1664 
1665 		struct node *n_ptr;
1666 		char addr_string[16];
1667 
1668 		tipc_printf(TIPC_OUTPUT, "Msg seq number: %u,  ", msg_seqno(msg));
1669 		tipc_printf(TIPC_OUTPUT, "Outstanding acks: %u\n", (u32)TIPC_SKB_CB(buf)->handle);
1670 
1671 		n_ptr = l_ptr->owner->next;
1672 		tipc_node_lock(n_ptr);
1673 
1674 		addr_string_fill(addr_string, n_ptr->addr);
1675 		tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string);
1676 		tipc_printf(TIPC_OUTPUT, "Supported: %d,  ", n_ptr->bclink.supported);
1677 		tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked);
1678 		tipc_printf(TIPC_OUTPUT, "Last in: %u,  ", n_ptr->bclink.last_in);
1679 		tipc_printf(TIPC_OUTPUT, "Gap after: %u,  ", n_ptr->bclink.gap_after);
1680 		tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to);
1681 		tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1682 
1683 		tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1684 
1685 		tipc_node_unlock(n_ptr);
1686 
1687 		l_ptr->stale_count = 0;
1688 	}
1689 }
1690 
1691 void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
1692 			  u32 retransmits)
1693 {
1694 	struct tipc_msg *msg;
1695 
1696 	if (!buf)
1697 		return;
1698 
1699 	msg = buf_msg(buf);
1700 
1701 	dbg("Retransmitting %u in link %x\n", retransmits, l_ptr);
1702 
1703 	if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
1704 		if (!skb_cloned(buf)) {
1705 			msg_dbg(msg, ">NO_RETR->BCONG>");
1706 			dbg_print_link(l_ptr, "   ");
1707 			l_ptr->retransm_queue_head = msg_seqno(msg);
1708 			l_ptr->retransm_queue_size = retransmits;
1709 			return;
1710 		} else {
1711 			/* Don't retransmit if driver already has the buffer */
1712 		}
1713 	} else {
1714 		/* Detect repeated retransmit failures on uncongested bearer */
1715 
1716 		if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1717 			if (++l_ptr->stale_count > 100) {
1718 				link_retransmit_failure(l_ptr, buf);
1719 				return;
1720 			}
1721 		} else {
1722 			l_ptr->last_retransmitted = msg_seqno(msg);
1723 			l_ptr->stale_count = 1;
1724 		}
1725 	}
1726 
1727 	while (retransmits && (buf != l_ptr->next_out) && buf && !skb_cloned(buf)) {
1728 		msg = buf_msg(buf);
1729 		msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1730 		msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1731 		if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1732 			msg_dbg(buf_msg(buf), ">RETR>");
1733 			buf = buf->next;
1734 			retransmits--;
1735 			l_ptr->stats.retransmitted++;
1736 		} else {
1737 			tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1738 			l_ptr->stats.bearer_congs++;
1739 			l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1740 			l_ptr->retransm_queue_size = retransmits;
1741 			return;
1742 		}
1743 	}
1744 
1745 	l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1746 }
1747 
1748 /*
1749  * link_recv_non_seq: Receive packets which are outside
1750  *                    the link sequence flow
1751  */
1752 
1753 static void link_recv_non_seq(struct sk_buff *buf)
1754 {
1755 	struct tipc_msg *msg = buf_msg(buf);
1756 
1757 	if (msg_user(msg) ==  LINK_CONFIG)
1758 		tipc_disc_recv_msg(buf);
1759 	else
1760 		tipc_bclink_recv_pkt(buf);
1761 }
1762 
1763 /**
1764  * link_insert_deferred_queue - insert deferred messages back into receive chain
1765  */
1766 
1767 static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
1768 						  struct sk_buff *buf)
1769 {
1770 	u32 seq_no;
1771 
1772 	if (l_ptr->oldest_deferred_in == NULL)
1773 		return buf;
1774 
1775 	seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1776 	if (seq_no == mod(l_ptr->next_in_no)) {
1777 		l_ptr->newest_deferred_in->next = buf;
1778 		buf = l_ptr->oldest_deferred_in;
1779 		l_ptr->oldest_deferred_in = NULL;
1780 		l_ptr->deferred_inqueue_sz = 0;
1781 	}
1782 	return buf;
1783 }
1784 
1785 void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1786 {
1787 	read_lock_bh(&tipc_net_lock);
1788 	while (head) {
1789 		struct bearer *b_ptr;
1790 		struct node *n_ptr;
1791 		struct link *l_ptr;
1792 		struct sk_buff *crs;
1793 		struct sk_buff *buf = head;
1794 		struct tipc_msg *msg = buf_msg(buf);
1795 		u32 seq_no = msg_seqno(msg);
1796 		u32 ackd = msg_ack(msg);
1797 		u32 released = 0;
1798 		int type;
1799 
1800 		b_ptr = (struct bearer *)tb_ptr;
1801 		TIPC_SKB_CB(buf)->handle = b_ptr;
1802 
1803 		head = head->next;
1804 		if (unlikely(msg_version(msg) != TIPC_VERSION))
1805 			goto cont;
1806 #if 0
1807 		if (msg_user(msg) != LINK_PROTOCOL)
1808 #endif
1809 			msg_dbg(msg,"<REC<");
1810 
1811 		if (unlikely(msg_non_seq(msg))) {
1812 			link_recv_non_seq(buf);
1813 			continue;
1814 		}
1815 
1816 		if (unlikely(!msg_short(msg) &&
1817 			     (msg_destnode(msg) != tipc_own_addr)))
1818 			goto cont;
1819 
1820 		n_ptr = tipc_node_find(msg_prevnode(msg));
1821 		if (unlikely(!n_ptr))
1822 			goto cont;
1823 
1824 		tipc_node_lock(n_ptr);
1825 		l_ptr = n_ptr->links[b_ptr->identity];
1826 		if (unlikely(!l_ptr)) {
1827 			tipc_node_unlock(n_ptr);
1828 			goto cont;
1829 		}
1830 		/*
1831 		 * Release acked messages
1832 		 */
1833 		if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
1834 			if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1835 				tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
1836 		}
1837 
1838 		crs = l_ptr->first_out;
1839 		while ((crs != l_ptr->next_out) &&
1840 		       less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1841 			struct sk_buff *next = crs->next;
1842 
1843 			buf_discard(crs);
1844 			crs = next;
1845 			released++;
1846 		}
1847 		if (released) {
1848 			l_ptr->first_out = crs;
1849 			l_ptr->out_queue_size -= released;
1850 		}
1851 		if (unlikely(l_ptr->next_out))
1852 			tipc_link_push_queue(l_ptr);
1853 		if (unlikely(!list_empty(&l_ptr->waiting_ports)))
1854 			tipc_link_wakeup_ports(l_ptr, 0);
1855 		if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1856 			l_ptr->stats.sent_acks++;
1857 			tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1858 		}
1859 
1860 protocol_check:
1861 		if (likely(link_working_working(l_ptr))) {
1862 			if (likely(seq_no == mod(l_ptr->next_in_no))) {
1863 				l_ptr->next_in_no++;
1864 				if (unlikely(l_ptr->oldest_deferred_in))
1865 					head = link_insert_deferred_queue(l_ptr,
1866 									  head);
1867 				if (likely(msg_is_dest(msg, tipc_own_addr))) {
1868 deliver:
1869 					if (likely(msg_isdata(msg))) {
1870 						tipc_node_unlock(n_ptr);
1871 						tipc_port_recv_msg(buf);
1872 						continue;
1873 					}
1874 					switch (msg_user(msg)) {
1875 					case MSG_BUNDLER:
1876 						l_ptr->stats.recv_bundles++;
1877 						l_ptr->stats.recv_bundled +=
1878 							msg_msgcnt(msg);
1879 						tipc_node_unlock(n_ptr);
1880 						tipc_link_recv_bundle(buf);
1881 						continue;
1882 					case ROUTE_DISTRIBUTOR:
1883 						tipc_node_unlock(n_ptr);
1884 						tipc_cltr_recv_routing_table(buf);
1885 						continue;
1886 					case NAME_DISTRIBUTOR:
1887 						tipc_node_unlock(n_ptr);
1888 						tipc_named_recv(buf);
1889 						continue;
1890 					case CONN_MANAGER:
1891 						tipc_node_unlock(n_ptr);
1892 						tipc_port_recv_proto_msg(buf);
1893 						continue;
1894 					case MSG_FRAGMENTER:
1895 						l_ptr->stats.recv_fragments++;
1896 						if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
1897 									    &buf, &msg)) {
1898 							l_ptr->stats.recv_fragmented++;
1899 							goto deliver;
1900 						}
1901 						break;
1902 					case CHANGEOVER_PROTOCOL:
1903 						type = msg_type(msg);
1904 						if (link_recv_changeover_msg(&l_ptr, &buf)) {
1905 							msg = buf_msg(buf);
1906 							seq_no = msg_seqno(msg);
1907 							TIPC_SKB_CB(buf)->handle
1908 								= b_ptr;
1909 							if (type == ORIGINAL_MSG)
1910 								goto deliver;
1911 							goto protocol_check;
1912 						}
1913 						break;
1914 					}
1915 				}
1916 				tipc_node_unlock(n_ptr);
1917 				tipc_net_route_msg(buf);
1918 				continue;
1919 			}
1920 			link_handle_out_of_seq_msg(l_ptr, buf);
1921 			head = link_insert_deferred_queue(l_ptr, head);
1922 			tipc_node_unlock(n_ptr);
1923 			continue;
1924 		}
1925 
1926 		if (msg_user(msg) == LINK_PROTOCOL) {
1927 			link_recv_proto_msg(l_ptr, buf);
1928 			head = link_insert_deferred_queue(l_ptr, head);
1929 			tipc_node_unlock(n_ptr);
1930 			continue;
1931 		}
1932 		msg_dbg(msg,"NSEQ<REC<");
1933 		link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1934 
1935 		if (link_working_working(l_ptr)) {
1936 			/* Re-insert in front of queue */
1937 			msg_dbg(msg,"RECV-REINS:");
1938 			buf->next = head;
1939 			head = buf;
1940 			tipc_node_unlock(n_ptr);
1941 			continue;
1942 		}
1943 		tipc_node_unlock(n_ptr);
1944 cont:
1945 		buf_discard(buf);
1946 	}
1947 	read_unlock_bh(&tipc_net_lock);
1948 }
1949 
1950 /*
1951  * link_defer_buf(): Sort a received out-of-sequence packet
1952  *                   into the deferred reception queue.
1953  * Returns the increase of the queue length,i.e. 0 or 1
1954  */
1955 
1956 u32 tipc_link_defer_pkt(struct sk_buff **head,
1957 			struct sk_buff **tail,
1958 			struct sk_buff *buf)
1959 {
1960 	struct sk_buff *prev = NULL;
1961 	struct sk_buff *crs = *head;
1962 	u32 seq_no = msg_seqno(buf_msg(buf));
1963 
1964 	buf->next = NULL;
1965 
1966 	/* Empty queue ? */
1967 	if (*head == NULL) {
1968 		*head = *tail = buf;
1969 		return 1;
1970 	}
1971 
1972 	/* Last ? */
1973 	if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1974 		(*tail)->next = buf;
1975 		*tail = buf;
1976 		return 1;
1977 	}
1978 
1979 	/* Scan through queue and sort it in */
1980 	do {
1981 		struct tipc_msg *msg = buf_msg(crs);
1982 
1983 		if (less(seq_no, msg_seqno(msg))) {
1984 			buf->next = crs;
1985 			if (prev)
1986 				prev->next = buf;
1987 			else
1988 				*head = buf;
1989 			return 1;
1990 		}
1991 		if (seq_no == msg_seqno(msg)) {
1992 			break;
1993 		}
1994 		prev = crs;
1995 		crs = crs->next;
1996 	}
1997 	while (crs);
1998 
1999 	/* Message is a duplicate of an existing message */
2000 
2001 	buf_discard(buf);
2002 	return 0;
2003 }
2004 
2005 /**
2006  * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
2007  */
2008 
2009 static void link_handle_out_of_seq_msg(struct link *l_ptr,
2010 				       struct sk_buff *buf)
2011 {
2012 	u32 seq_no = msg_seqno(buf_msg(buf));
2013 
2014 	if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
2015 		link_recv_proto_msg(l_ptr, buf);
2016 		return;
2017 	}
2018 
2019 	dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n",
2020 	    seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no);
2021 
2022 	/* Record OOS packet arrival (force mismatch on next timeout) */
2023 
2024 	l_ptr->checkpoint--;
2025 
2026 	/*
2027 	 * Discard packet if a duplicate; otherwise add it to deferred queue
2028 	 * and notify peer of gap as per protocol specification
2029 	 */
2030 
2031 	if (less(seq_no, mod(l_ptr->next_in_no))) {
2032 		l_ptr->stats.duplicates++;
2033 		buf_discard(buf);
2034 		return;
2035 	}
2036 
2037 	if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
2038 				&l_ptr->newest_deferred_in, buf)) {
2039 		l_ptr->deferred_inqueue_sz++;
2040 		l_ptr->stats.deferred_recv++;
2041 		if ((l_ptr->deferred_inqueue_sz % 16) == 1)
2042 			tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
2043 	} else
2044 		l_ptr->stats.duplicates++;
2045 }
2046 
2047 /*
2048  * Send protocol message to the other endpoint.
2049  */
2050 void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
2051 			      u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
2052 {
2053 	struct sk_buff *buf = NULL;
2054 	struct tipc_msg *msg = l_ptr->pmsg;
2055         u32 msg_size = sizeof(l_ptr->proto_msg);
2056 
2057 	if (link_blocked(l_ptr))
2058 		return;
2059 	msg_set_type(msg, msg_typ);
2060 	msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
2061 	msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
2062 	msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
2063 
2064 	if (msg_typ == STATE_MSG) {
2065 		u32 next_sent = mod(l_ptr->next_out_no);
2066 
2067 		if (!tipc_link_is_up(l_ptr))
2068 			return;
2069 		if (l_ptr->next_out)
2070 			next_sent = msg_seqno(buf_msg(l_ptr->next_out));
2071 		msg_set_next_sent(msg, next_sent);
2072 		if (l_ptr->oldest_deferred_in) {
2073 			u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2074 			gap = mod(rec - mod(l_ptr->next_in_no));
2075 		}
2076 		msg_set_seq_gap(msg, gap);
2077 		if (gap)
2078 			l_ptr->stats.sent_nacks++;
2079 		msg_set_link_tolerance(msg, tolerance);
2080 		msg_set_linkprio(msg, priority);
2081 		msg_set_max_pkt(msg, ack_mtu);
2082 		msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
2083 		msg_set_probe(msg, probe_msg != 0);
2084 		if (probe_msg) {
2085 			u32 mtu = l_ptr->max_pkt;
2086 
2087                         if ((mtu < l_ptr->max_pkt_target) &&
2088 			    link_working_working(l_ptr) &&
2089 			    l_ptr->fsm_msg_cnt) {
2090 				msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
2091                                 if (l_ptr->max_pkt_probes == 10) {
2092                                         l_ptr->max_pkt_target = (msg_size - 4);
2093                                         l_ptr->max_pkt_probes = 0;
2094 					msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
2095                                 }
2096 				l_ptr->max_pkt_probes++;
2097                         }
2098 
2099 			l_ptr->stats.sent_probes++;
2100                 }
2101 		l_ptr->stats.sent_states++;
2102 	} else {		/* RESET_MSG or ACTIVATE_MSG */
2103 		msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
2104 		msg_set_seq_gap(msg, 0);
2105 		msg_set_next_sent(msg, 1);
2106 		msg_set_link_tolerance(msg, l_ptr->tolerance);
2107 		msg_set_linkprio(msg, l_ptr->priority);
2108 		msg_set_max_pkt(msg, l_ptr->max_pkt_target);
2109 	}
2110 
2111 	if (tipc_node_has_redundant_links(l_ptr->owner)) {
2112 		msg_set_redundant_link(msg);
2113 	} else {
2114 		msg_clear_redundant_link(msg);
2115 	}
2116 	msg_set_linkprio(msg, l_ptr->priority);
2117 
2118 	/* Ensure sequence number will not fit : */
2119 
2120 	msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
2121 
2122 	/* Congestion? */
2123 
2124 	if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
2125 		if (!l_ptr->proto_msg_queue) {
2126 			l_ptr->proto_msg_queue =
2127 				buf_acquire(sizeof(l_ptr->proto_msg));
2128 		}
2129 		buf = l_ptr->proto_msg_queue;
2130 		if (!buf)
2131 			return;
2132 		memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg));
2133 		return;
2134 	}
2135 	msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
2136 
2137 	/* Message can be sent */
2138 
2139 	msg_dbg(msg, ">>");
2140 
2141 	buf = buf_acquire(msg_size);
2142 	if (!buf)
2143 		return;
2144 
2145 	memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg));
2146         msg_set_size(buf_msg(buf), msg_size);
2147 
2148 	if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
2149 		l_ptr->unacked_window = 0;
2150 		buf_discard(buf);
2151 		return;
2152 	}
2153 
2154 	/* New congestion */
2155 	tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
2156 	l_ptr->proto_msg_queue = buf;
2157 	l_ptr->stats.bearer_congs++;
2158 }
2159 
2160 /*
2161  * Receive protocol message :
2162  * Note that network plane id propagates through the network, and may
2163  * change at any time. The node with lowest address rules
2164  */
2165 
2166 static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2167 {
2168 	u32 rec_gap = 0;
2169 	u32 max_pkt_info;
2170         u32 max_pkt_ack;
2171 	u32 msg_tol;
2172 	struct tipc_msg *msg = buf_msg(buf);
2173 
2174 	dbg("AT(%u):", jiffies_to_msecs(jiffies));
2175 	msg_dbg(msg, "<<");
2176 	if (link_blocked(l_ptr))
2177 		goto exit;
2178 
2179 	/* record unnumbered packet arrival (force mismatch on next timeout) */
2180 
2181 	l_ptr->checkpoint--;
2182 
2183 	if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2184 		if (tipc_own_addr > msg_prevnode(msg))
2185 			l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2186 
2187 	l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2188 
2189 	switch (msg_type(msg)) {
2190 
2191 	case RESET_MSG:
2192 		if (!link_working_unknown(l_ptr) && l_ptr->peer_session) {
2193 			if (msg_session(msg) == l_ptr->peer_session) {
2194 				dbg("Duplicate RESET: %u<->%u\n",
2195 				    msg_session(msg), l_ptr->peer_session);
2196 				break; /* duplicate: ignore */
2197 			}
2198 		}
2199 		/* fall thru' */
2200 	case ACTIVATE_MSG:
2201 		/* Update link settings according other endpoint's values */
2202 
2203 		strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2204 
2205 		if ((msg_tol = msg_link_tolerance(msg)) &&
2206 		    (msg_tol > l_ptr->tolerance))
2207 			link_set_supervision_props(l_ptr, msg_tol);
2208 
2209 		if (msg_linkprio(msg) > l_ptr->priority)
2210 			l_ptr->priority = msg_linkprio(msg);
2211 
2212 		max_pkt_info = msg_max_pkt(msg);
2213                 if (max_pkt_info) {
2214 			if (max_pkt_info < l_ptr->max_pkt_target)
2215 				l_ptr->max_pkt_target = max_pkt_info;
2216 			if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2217 				l_ptr->max_pkt = l_ptr->max_pkt_target;
2218 		} else {
2219                         l_ptr->max_pkt = l_ptr->max_pkt_target;
2220 		}
2221 		l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2222 
2223 		link_state_event(l_ptr, msg_type(msg));
2224 
2225 		l_ptr->peer_session = msg_session(msg);
2226 		l_ptr->peer_bearer_id = msg_bearer_id(msg);
2227 
2228 		/* Synchronize broadcast sequence numbers */
2229 		if (!tipc_node_has_redundant_links(l_ptr->owner)) {
2230 			l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2231 		}
2232 		break;
2233 	case STATE_MSG:
2234 
2235 		if ((msg_tol = msg_link_tolerance(msg)))
2236 			link_set_supervision_props(l_ptr, msg_tol);
2237 
2238 		if (msg_linkprio(msg) &&
2239 		    (msg_linkprio(msg) != l_ptr->priority)) {
2240 			warn("Resetting link <%s>, priority change %u->%u\n",
2241 			     l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2242 			l_ptr->priority = msg_linkprio(msg);
2243 			tipc_link_reset(l_ptr); /* Enforce change to take effect */
2244 			break;
2245 		}
2246 		link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2247 		l_ptr->stats.recv_states++;
2248 		if (link_reset_unknown(l_ptr))
2249 			break;
2250 
2251 		if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
2252 			rec_gap = mod(msg_next_sent(msg) -
2253 				      mod(l_ptr->next_in_no));
2254 		}
2255 
2256 		max_pkt_ack = msg_max_pkt(msg);
2257                 if (max_pkt_ack > l_ptr->max_pkt) {
2258                         dbg("Link <%s> updated MTU %u -> %u\n",
2259                             l_ptr->name, l_ptr->max_pkt, max_pkt_ack);
2260                         l_ptr->max_pkt = max_pkt_ack;
2261                         l_ptr->max_pkt_probes = 0;
2262                 }
2263 
2264 		max_pkt_ack = 0;
2265                 if (msg_probe(msg)) {
2266 			l_ptr->stats.recv_probes++;
2267                         if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2268                                 max_pkt_ack = msg_size(msg);
2269                         }
2270                 }
2271 
2272 		/* Protocol message before retransmits, reduce loss risk */
2273 
2274 		tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
2275 
2276 		if (rec_gap || (msg_probe(msg))) {
2277 			tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2278 						 0, rec_gap, 0, 0, max_pkt_ack);
2279 		}
2280 		if (msg_seq_gap(msg)) {
2281 			msg_dbg(msg, "With Gap:");
2282 			l_ptr->stats.recv_nacks++;
2283 			tipc_link_retransmit(l_ptr, l_ptr->first_out,
2284 					     msg_seq_gap(msg));
2285 		}
2286 		break;
2287 	default:
2288 		msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<");
2289 	}
2290 exit:
2291 	buf_discard(buf);
2292 }
2293 
2294 
2295 /*
2296  * tipc_link_tunnel(): Send one message via a link belonging to
2297  * another bearer. Owner node is locked.
2298  */
2299 void tipc_link_tunnel(struct link *l_ptr,
2300 		      struct tipc_msg *tunnel_hdr,
2301 		      struct tipc_msg  *msg,
2302 		      u32 selector)
2303 {
2304 	struct link *tunnel;
2305 	struct sk_buff *buf;
2306 	u32 length = msg_size(msg);
2307 
2308 	tunnel = l_ptr->owner->active_links[selector & 1];
2309 	if (!tipc_link_is_up(tunnel)) {
2310 		warn("Link changeover error, "
2311 		     "tunnel link no longer available\n");
2312 		return;
2313 	}
2314 	msg_set_size(tunnel_hdr, length + INT_H_SIZE);
2315 	buf = buf_acquire(length + INT_H_SIZE);
2316 	if (!buf) {
2317 		warn("Link changeover error, "
2318 		     "unable to send tunnel msg\n");
2319 		return;
2320 	}
2321 	memcpy(buf->data, (unchar *)tunnel_hdr, INT_H_SIZE);
2322 	memcpy(buf->data + INT_H_SIZE, (unchar *)msg, length);
2323 	dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane);
2324 	msg_dbg(buf_msg(buf), ">SEND>");
2325 	tipc_link_send_buf(tunnel, buf);
2326 }
2327 
2328 
2329 
2330 /*
2331  * changeover(): Send whole message queue via the remaining link
2332  *               Owner node is locked.
2333  */
2334 
2335 void tipc_link_changeover(struct link *l_ptr)
2336 {
2337 	u32 msgcount = l_ptr->out_queue_size;
2338 	struct sk_buff *crs = l_ptr->first_out;
2339 	struct link *tunnel = l_ptr->owner->active_links[0];
2340 	struct tipc_msg tunnel_hdr;
2341 	int split_bundles;
2342 
2343 	if (!tunnel)
2344 		return;
2345 
2346 	if (!l_ptr->owner->permit_changeover) {
2347 		warn("Link changeover error, "
2348 		     "peer did not permit changeover\n");
2349 		return;
2350 	}
2351 
2352 	msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2353 		 ORIGINAL_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
2354 	msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2355 	msg_set_msgcnt(&tunnel_hdr, msgcount);
2356 	dbg("Link changeover requires %u tunnel messages\n", msgcount);
2357 
2358 	if (!l_ptr->first_out) {
2359 		struct sk_buff *buf;
2360 
2361 		buf = buf_acquire(INT_H_SIZE);
2362 		if (buf) {
2363 			memcpy(buf->data, (unchar *)&tunnel_hdr, INT_H_SIZE);
2364 			msg_set_size(&tunnel_hdr, INT_H_SIZE);
2365 			dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2366 			    tunnel->b_ptr->net_plane);
2367 			msg_dbg(&tunnel_hdr, "EMPTY>SEND>");
2368 			tipc_link_send_buf(tunnel, buf);
2369 		} else {
2370 			warn("Link changeover error, "
2371 			     "unable to send changeover msg\n");
2372 		}
2373 		return;
2374 	}
2375 
2376 	split_bundles = (l_ptr->owner->active_links[0] !=
2377 			 l_ptr->owner->active_links[1]);
2378 
2379 	while (crs) {
2380 		struct tipc_msg *msg = buf_msg(crs);
2381 
2382 		if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
2383 			u32 msgcount = msg_msgcnt(msg);
2384 			struct tipc_msg *m = msg_get_wrapped(msg);
2385 			unchar* pos = (unchar*)m;
2386 
2387 			while (msgcount--) {
2388 				msg_set_seqno(m,msg_seqno(msg));
2389 				tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2390 						 msg_link_selector(m));
2391 				pos += align(msg_size(m));
2392 				m = (struct tipc_msg *)pos;
2393 			}
2394 		} else {
2395 			tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2396 					 msg_link_selector(msg));
2397 		}
2398 		crs = crs->next;
2399 	}
2400 }
2401 
2402 void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
2403 {
2404 	struct sk_buff *iter;
2405 	struct tipc_msg tunnel_hdr;
2406 
2407 	msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2408 		 DUPLICATE_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr);
2409 	msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2410 	msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2411 	iter = l_ptr->first_out;
2412 	while (iter) {
2413 		struct sk_buff *outbuf;
2414 		struct tipc_msg *msg = buf_msg(iter);
2415 		u32 length = msg_size(msg);
2416 
2417 		if (msg_user(msg) == MSG_BUNDLER)
2418 			msg_set_type(msg, CLOSED_MSG);
2419 		msg_set_ack(msg, mod(l_ptr->next_in_no - 1));	/* Update */
2420 		msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
2421 		msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
2422 		outbuf = buf_acquire(length + INT_H_SIZE);
2423 		if (outbuf == NULL) {
2424 			warn("Link changeover error, "
2425 			     "unable to send duplicate msg\n");
2426 			return;
2427 		}
2428 		memcpy(outbuf->data, (unchar *)&tunnel_hdr, INT_H_SIZE);
2429 		memcpy(outbuf->data + INT_H_SIZE, iter->data, length);
2430 		dbg("%c->%c:", l_ptr->b_ptr->net_plane,
2431 		    tunnel->b_ptr->net_plane);
2432 		msg_dbg(buf_msg(outbuf), ">SEND>");
2433 		tipc_link_send_buf(tunnel, outbuf);
2434 		if (!tipc_link_is_up(l_ptr))
2435 			return;
2436 		iter = iter->next;
2437 	}
2438 }
2439 
2440 
2441 
2442 /**
2443  * buf_extract - extracts embedded TIPC message from another message
2444  * @skb: encapsulating message buffer
2445  * @from_pos: offset to extract from
2446  *
2447  * Returns a new message buffer containing an embedded message.  The
2448  * encapsulating message itself is left unchanged.
2449  */
2450 
2451 static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2452 {
2453 	struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2454 	u32 size = msg_size(msg);
2455 	struct sk_buff *eb;
2456 
2457 	eb = buf_acquire(size);
2458 	if (eb)
2459 		memcpy(eb->data, (unchar *)msg, size);
2460 	return eb;
2461 }
2462 
2463 /*
2464  *  link_recv_changeover_msg(): Receive tunneled packet sent
2465  *  via other link. Node is locked. Return extracted buffer.
2466  */
2467 
2468 static int link_recv_changeover_msg(struct link **l_ptr,
2469 				    struct sk_buff **buf)
2470 {
2471 	struct sk_buff *tunnel_buf = *buf;
2472 	struct link *dest_link;
2473 	struct tipc_msg *msg;
2474 	struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2475 	u32 msg_typ = msg_type(tunnel_msg);
2476 	u32 msg_count = msg_msgcnt(tunnel_msg);
2477 
2478 	dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
2479 	if (!dest_link) {
2480 		msg_dbg(tunnel_msg, "NOLINK/<REC<");
2481 		goto exit;
2482 	}
2483 	if (dest_link == *l_ptr) {
2484 		err("Unexpected changeover message on link <%s>\n",
2485 		    (*l_ptr)->name);
2486 		goto exit;
2487 	}
2488 	dbg("%c<-%c:", dest_link->b_ptr->net_plane,
2489 	    (*l_ptr)->b_ptr->net_plane);
2490 	*l_ptr = dest_link;
2491 	msg = msg_get_wrapped(tunnel_msg);
2492 
2493 	if (msg_typ == DUPLICATE_MSG) {
2494 		if (less(msg_seqno(msg), mod(dest_link->next_in_no))) {
2495 			msg_dbg(tunnel_msg, "DROP/<REC<");
2496 			goto exit;
2497 		}
2498 		*buf = buf_extract(tunnel_buf,INT_H_SIZE);
2499 		if (*buf == NULL) {
2500 			warn("Link changeover error, duplicate msg dropped\n");
2501 			goto exit;
2502 		}
2503 		msg_dbg(tunnel_msg, "TNL<REC<");
2504 		buf_discard(tunnel_buf);
2505 		return 1;
2506 	}
2507 
2508 	/* First original message ?: */
2509 
2510 	if (tipc_link_is_up(dest_link)) {
2511 		msg_dbg(tunnel_msg, "UP/FIRST/<REC<");
2512 		info("Resetting link <%s>, changeover initiated by peer\n",
2513 		     dest_link->name);
2514 		tipc_link_reset(dest_link);
2515 		dest_link->exp_msg_count = msg_count;
2516 		dbg("Expecting %u tunnelled messages\n", msg_count);
2517 		if (!msg_count)
2518 			goto exit;
2519 	} else if (dest_link->exp_msg_count == START_CHANGEOVER) {
2520 		msg_dbg(tunnel_msg, "BLK/FIRST/<REC<");
2521 		dest_link->exp_msg_count = msg_count;
2522 		dbg("Expecting %u tunnelled messages\n", msg_count);
2523 		if (!msg_count)
2524 			goto exit;
2525 	}
2526 
2527 	/* Receive original message */
2528 
2529 	if (dest_link->exp_msg_count == 0) {
2530 		warn("Link switchover error, "
2531 		     "got too many tunnelled messages\n");
2532 		msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<");
2533 		dbg_print_link(dest_link, "LINK:");
2534 		goto exit;
2535 	}
2536 	dest_link->exp_msg_count--;
2537 	if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
2538 		msg_dbg(tunnel_msg, "DROP/DUPL/<REC<");
2539 		goto exit;
2540 	} else {
2541 		*buf = buf_extract(tunnel_buf, INT_H_SIZE);
2542 		if (*buf != NULL) {
2543 			msg_dbg(tunnel_msg, "TNL<REC<");
2544 			buf_discard(tunnel_buf);
2545 			return 1;
2546 		} else {
2547 			warn("Link changeover error, original msg dropped\n");
2548 		}
2549 	}
2550 exit:
2551 	*buf = NULL;
2552 	buf_discard(tunnel_buf);
2553 	return 0;
2554 }
2555 
2556 /*
2557  *  Bundler functionality:
2558  */
2559 void tipc_link_recv_bundle(struct sk_buff *buf)
2560 {
2561 	u32 msgcount = msg_msgcnt(buf_msg(buf));
2562 	u32 pos = INT_H_SIZE;
2563 	struct sk_buff *obuf;
2564 
2565 	msg_dbg(buf_msg(buf), "<BNDL<: ");
2566 	while (msgcount--) {
2567 		obuf = buf_extract(buf, pos);
2568 		if (obuf == NULL) {
2569 			warn("Link unable to unbundle message(s)\n");
2570 			break;
2571 		};
2572 		pos += align(msg_size(buf_msg(obuf)));
2573 		msg_dbg(buf_msg(obuf), "     /");
2574 		tipc_net_route_msg(obuf);
2575 	}
2576 	buf_discard(buf);
2577 }
2578 
2579 /*
2580  *  Fragmentation/defragmentation:
2581  */
2582 
2583 
2584 /*
2585  * tipc_link_send_long_buf: Entry for buffers needing fragmentation.
2586  * The buffer is complete, inclusive total message length.
2587  * Returns user data length.
2588  */
2589 int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
2590 {
2591 	struct tipc_msg *inmsg = buf_msg(buf);
2592 	struct tipc_msg fragm_hdr;
2593 	u32 insize = msg_size(inmsg);
2594 	u32 dsz = msg_data_sz(inmsg);
2595 	unchar *crs = buf->data;
2596 	u32 rest = insize;
2597 	u32 pack_sz = link_max_pkt(l_ptr);
2598 	u32 fragm_sz = pack_sz - INT_H_SIZE;
2599 	u32 fragm_no = 1;
2600 	u32 destaddr = msg_destnode(inmsg);
2601 
2602 	if (msg_short(inmsg))
2603 		destaddr = l_ptr->addr;
2604 
2605 	if (msg_routed(inmsg))
2606 		msg_set_prevnode(inmsg, tipc_own_addr);
2607 
2608 	/* Prepare reusable fragment header: */
2609 
2610 	msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
2611 		 TIPC_OK, INT_H_SIZE, destaddr);
2612 	msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2613 	msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2614 	msg_set_fragm_no(&fragm_hdr, fragm_no);
2615 	l_ptr->stats.sent_fragmented++;
2616 
2617 	/* Chop up message: */
2618 
2619 	while (rest > 0) {
2620 		struct sk_buff *fragm;
2621 
2622 		if (rest <= fragm_sz) {
2623 			fragm_sz = rest;
2624 			msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2625 		}
2626 		fragm = buf_acquire(fragm_sz + INT_H_SIZE);
2627 		if (fragm == NULL) {
2628 			warn("Link unable to fragment message\n");
2629 			dsz = -ENOMEM;
2630 			goto exit;
2631 		}
2632 		msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
2633 		memcpy(fragm->data, (unchar *)&fragm_hdr, INT_H_SIZE);
2634 		memcpy(fragm->data + INT_H_SIZE, crs, fragm_sz);
2635 
2636 		/*  Send queued messages first, if any: */
2637 
2638 		l_ptr->stats.sent_fragments++;
2639 		tipc_link_send_buf(l_ptr, fragm);
2640 		if (!tipc_link_is_up(l_ptr))
2641 			return dsz;
2642 		msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2643 		rest -= fragm_sz;
2644 		crs += fragm_sz;
2645 		msg_set_type(&fragm_hdr, FRAGMENT);
2646 	}
2647 exit:
2648 	buf_discard(buf);
2649 	return dsz;
2650 }
2651 
2652 /*
2653  * A pending message being re-assembled must store certain values
2654  * to handle subsequent fragments correctly. The following functions
2655  * help storing these values in unused, available fields in the
2656  * pending message. This makes dynamic memory allocation unecessary.
2657  */
2658 
2659 static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
2660 {
2661 	msg_set_seqno(buf_msg(buf), seqno);
2662 }
2663 
2664 static u32 get_fragm_size(struct sk_buff *buf)
2665 {
2666 	return msg_ack(buf_msg(buf));
2667 }
2668 
2669 static void set_fragm_size(struct sk_buff *buf, u32 sz)
2670 {
2671 	msg_set_ack(buf_msg(buf), sz);
2672 }
2673 
2674 static u32 get_expected_frags(struct sk_buff *buf)
2675 {
2676 	return msg_bcast_ack(buf_msg(buf));
2677 }
2678 
2679 static void set_expected_frags(struct sk_buff *buf, u32 exp)
2680 {
2681 	msg_set_bcast_ack(buf_msg(buf), exp);
2682 }
2683 
2684 static u32 get_timer_cnt(struct sk_buff *buf)
2685 {
2686 	return msg_reroute_cnt(buf_msg(buf));
2687 }
2688 
2689 static void incr_timer_cnt(struct sk_buff *buf)
2690 {
2691 	msg_incr_reroute_cnt(buf_msg(buf));
2692 }
2693 
2694 /*
2695  * tipc_link_recv_fragment(): Called with node lock on. Returns
2696  * the reassembled buffer if message is complete.
2697  */
2698 int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
2699 			    struct tipc_msg **m)
2700 {
2701 	struct sk_buff *prev = NULL;
2702 	struct sk_buff *fbuf = *fb;
2703 	struct tipc_msg *fragm = buf_msg(fbuf);
2704 	struct sk_buff *pbuf = *pending;
2705 	u32 long_msg_seq_no = msg_long_msgno(fragm);
2706 
2707 	*fb = NULL;
2708 	msg_dbg(fragm,"FRG<REC<");
2709 
2710 	/* Is there an incomplete message waiting for this fragment? */
2711 
2712 	while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no)
2713 			|| (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
2714 		prev = pbuf;
2715 		pbuf = pbuf->next;
2716 	}
2717 
2718 	if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2719 		struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2720 		u32 msg_sz = msg_size(imsg);
2721 		u32 fragm_sz = msg_data_sz(fragm);
2722 		u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2723 		u32 max =  TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2724 		if (msg_type(imsg) == TIPC_MCAST_MSG)
2725 			max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2726 		if (msg_size(imsg) > max) {
2727 			msg_dbg(fragm,"<REC<Oversized: ");
2728 			buf_discard(fbuf);
2729 			return 0;
2730 		}
2731 		pbuf = buf_acquire(msg_size(imsg));
2732 		if (pbuf != NULL) {
2733 			pbuf->next = *pending;
2734 			*pending = pbuf;
2735 			memcpy(pbuf->data, (unchar *)imsg, msg_data_sz(fragm));
2736 
2737 			/*  Prepare buffer for subsequent fragments. */
2738 
2739 			set_long_msg_seqno(pbuf, long_msg_seq_no);
2740 			set_fragm_size(pbuf,fragm_sz);
2741 			set_expected_frags(pbuf,exp_fragm_cnt - 1);
2742 		} else {
2743 			warn("Link unable to reassemble fragmented message\n");
2744 		}
2745 		buf_discard(fbuf);
2746 		return 0;
2747 	} else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2748 		u32 dsz = msg_data_sz(fragm);
2749 		u32 fsz = get_fragm_size(pbuf);
2750 		u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2751 		u32 exp_frags = get_expected_frags(pbuf) - 1;
2752 		memcpy(pbuf->data + crs, msg_data(fragm), dsz);
2753 		buf_discard(fbuf);
2754 
2755 		/* Is message complete? */
2756 
2757 		if (exp_frags == 0) {
2758 			if (prev)
2759 				prev->next = pbuf->next;
2760 			else
2761 				*pending = pbuf->next;
2762 			msg_reset_reroute_cnt(buf_msg(pbuf));
2763 			*fb = pbuf;
2764 			*m = buf_msg(pbuf);
2765 			return 1;
2766 		}
2767 		set_expected_frags(pbuf,exp_frags);
2768 		return 0;
2769 	}
2770 	dbg(" Discarding orphan fragment %x\n",fbuf);
2771 	msg_dbg(fragm,"ORPHAN:");
2772 	dbg("Pending long buffers:\n");
2773 	dbg_print_buf_chain(*pending);
2774 	buf_discard(fbuf);
2775 	return 0;
2776 }
2777 
2778 /**
2779  * link_check_defragm_bufs - flush stale incoming message fragments
2780  * @l_ptr: pointer to link
2781  */
2782 
2783 static void link_check_defragm_bufs(struct link *l_ptr)
2784 {
2785 	struct sk_buff *prev = NULL;
2786 	struct sk_buff *next = NULL;
2787 	struct sk_buff *buf = l_ptr->defragm_buf;
2788 
2789 	if (!buf)
2790 		return;
2791 	if (!link_working_working(l_ptr))
2792 		return;
2793 	while (buf) {
2794 		u32 cnt = get_timer_cnt(buf);
2795 
2796 		next = buf->next;
2797 		if (cnt < 4) {
2798 			incr_timer_cnt(buf);
2799 			prev = buf;
2800 		} else {
2801 			dbg(" Discarding incomplete long buffer\n");
2802 			msg_dbg(buf_msg(buf), "LONG:");
2803 			dbg_print_link(l_ptr, "curr:");
2804 			dbg("Pending long buffers:\n");
2805 			dbg_print_buf_chain(l_ptr->defragm_buf);
2806 			if (prev)
2807 				prev->next = buf->next;
2808 			else
2809 				l_ptr->defragm_buf = buf->next;
2810 			buf_discard(buf);
2811 		}
2812 		buf = next;
2813 	}
2814 }
2815 
2816 
2817 
2818 static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2819 {
2820 	l_ptr->tolerance = tolerance;
2821 	l_ptr->continuity_interval =
2822 		((tolerance / 4) > 500) ? 500 : tolerance / 4;
2823 	l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2824 }
2825 
2826 
2827 void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
2828 {
2829 	/* Data messages from this node, inclusive FIRST_FRAGM */
2830 	l_ptr->queue_limit[DATA_LOW] = window;
2831 	l_ptr->queue_limit[DATA_MEDIUM] = (window / 3) * 4;
2832 	l_ptr->queue_limit[DATA_HIGH] = (window / 3) * 5;
2833 	l_ptr->queue_limit[DATA_CRITICAL] = (window / 3) * 6;
2834 	/* Transiting data messages,inclusive FIRST_FRAGM */
2835 	l_ptr->queue_limit[DATA_LOW + 4] = 300;
2836 	l_ptr->queue_limit[DATA_MEDIUM + 4] = 600;
2837 	l_ptr->queue_limit[DATA_HIGH + 4] = 900;
2838 	l_ptr->queue_limit[DATA_CRITICAL + 4] = 1200;
2839 	l_ptr->queue_limit[CONN_MANAGER] = 1200;
2840 	l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200;
2841 	l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2842 	l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2843 	/* FRAGMENT and LAST_FRAGMENT packets */
2844 	l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2845 }
2846 
2847 /**
2848  * link_find_link - locate link by name
2849  * @name - ptr to link name string
2850  * @node - ptr to area to be filled with ptr to associated node
2851  *
2852  * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
2853  * this also prevents link deletion.
2854  *
2855  * Returns pointer to link (or 0 if invalid link name).
2856  */
2857 
2858 static struct link *link_find_link(const char *name, struct node **node)
2859 {
2860 	struct link_name link_name_parts;
2861 	struct bearer *b_ptr;
2862 	struct link *l_ptr;
2863 
2864 	if (!link_name_validate(name, &link_name_parts))
2865 		return NULL;
2866 
2867 	b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
2868 	if (!b_ptr)
2869 		return NULL;
2870 
2871 	*node = tipc_node_find(link_name_parts.addr_peer);
2872 	if (!*node)
2873 		return NULL;
2874 
2875 	l_ptr = (*node)->links[b_ptr->identity];
2876 	if (!l_ptr || strcmp(l_ptr->name, name))
2877 		return NULL;
2878 
2879 	return l_ptr;
2880 }
2881 
2882 struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
2883 				     u16 cmd)
2884 {
2885 	struct tipc_link_config *args;
2886         u32 new_value;
2887 	struct link *l_ptr;
2888 	struct node *node;
2889         int res;
2890 
2891 	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
2892 		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2893 
2894 	args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2895 	new_value = ntohl(args->value);
2896 
2897 	if (!strcmp(args->name, tipc_bclink_name)) {
2898 		if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
2899 		    (tipc_bclink_set_queue_limits(new_value) == 0))
2900 			return tipc_cfg_reply_none();
2901 	       	return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
2902 						   " (cannot change setting on broadcast link)");
2903 	}
2904 
2905 	read_lock_bh(&tipc_net_lock);
2906 	l_ptr = link_find_link(args->name, &node);
2907 	if (!l_ptr) {
2908 		read_unlock_bh(&tipc_net_lock);
2909 	       	return tipc_cfg_reply_error_string("link not found");
2910 	}
2911 
2912 	tipc_node_lock(node);
2913 	res = -EINVAL;
2914 	switch (cmd) {
2915 	case TIPC_CMD_SET_LINK_TOL:
2916 		if ((new_value >= TIPC_MIN_LINK_TOL) &&
2917 		    (new_value <= TIPC_MAX_LINK_TOL)) {
2918 			link_set_supervision_props(l_ptr, new_value);
2919 			tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2920 						 0, 0, new_value, 0, 0);
2921 			res = TIPC_OK;
2922 		}
2923 		break;
2924 	case TIPC_CMD_SET_LINK_PRI:
2925 		if ((new_value >= TIPC_MIN_LINK_PRI) &&
2926 		    (new_value <= TIPC_MAX_LINK_PRI)) {
2927 			l_ptr->priority = new_value;
2928 			tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2929 						 0, 0, 0, new_value, 0);
2930 			res = TIPC_OK;
2931 		}
2932 		break;
2933 	case TIPC_CMD_SET_LINK_WINDOW:
2934 		if ((new_value >= TIPC_MIN_LINK_WIN) &&
2935 		    (new_value <= TIPC_MAX_LINK_WIN)) {
2936 			tipc_link_set_queue_limits(l_ptr, new_value);
2937 			res = TIPC_OK;
2938 		}
2939 		break;
2940 	}
2941 	tipc_node_unlock(node);
2942 
2943 	read_unlock_bh(&tipc_net_lock);
2944 	if (res)
2945 	       	return tipc_cfg_reply_error_string("cannot change link setting");
2946 
2947 	return tipc_cfg_reply_none();
2948 }
2949 
2950 /**
2951  * link_reset_statistics - reset link statistics
2952  * @l_ptr: pointer to link
2953  */
2954 
2955 static void link_reset_statistics(struct link *l_ptr)
2956 {
2957 	memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2958 	l_ptr->stats.sent_info = l_ptr->next_out_no;
2959 	l_ptr->stats.recv_info = l_ptr->next_in_no;
2960 }
2961 
2962 struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
2963 {
2964 	char *link_name;
2965 	struct link *l_ptr;
2966 	struct node *node;
2967 
2968 	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
2969 		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2970 
2971 	link_name = (char *)TLV_DATA(req_tlv_area);
2972 	if (!strcmp(link_name, tipc_bclink_name)) {
2973 		if (tipc_bclink_reset_stats())
2974 			return tipc_cfg_reply_error_string("link not found");
2975 		return tipc_cfg_reply_none();
2976 	}
2977 
2978 	read_lock_bh(&tipc_net_lock);
2979 	l_ptr = link_find_link(link_name, &node);
2980 	if (!l_ptr) {
2981 		read_unlock_bh(&tipc_net_lock);
2982 		return tipc_cfg_reply_error_string("link not found");
2983 	}
2984 
2985 	tipc_node_lock(node);
2986 	link_reset_statistics(l_ptr);
2987 	tipc_node_unlock(node);
2988 	read_unlock_bh(&tipc_net_lock);
2989 	return tipc_cfg_reply_none();
2990 }
2991 
2992 /**
2993  * percent - convert count to a percentage of total (rounding up or down)
2994  */
2995 
2996 static u32 percent(u32 count, u32 total)
2997 {
2998 	return (count * 100 + (total / 2)) / total;
2999 }
3000 
3001 /**
3002  * tipc_link_stats - print link statistics
3003  * @name: link name
3004  * @buf: print buffer area
3005  * @buf_size: size of print buffer area
3006  *
3007  * Returns length of print buffer data string (or 0 if error)
3008  */
3009 
3010 static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
3011 {
3012 	struct print_buf pb;
3013 	struct link *l_ptr;
3014 	struct node *node;
3015 	char *status;
3016 	u32 profile_total = 0;
3017 
3018 	if (!strcmp(name, tipc_bclink_name))
3019 		return tipc_bclink_stats(buf, buf_size);
3020 
3021 	tipc_printbuf_init(&pb, buf, buf_size);
3022 
3023 	read_lock_bh(&tipc_net_lock);
3024 	l_ptr = link_find_link(name, &node);
3025 	if (!l_ptr) {
3026 		read_unlock_bh(&tipc_net_lock);
3027 		return 0;
3028 	}
3029 	tipc_node_lock(node);
3030 
3031 	if (tipc_link_is_active(l_ptr))
3032 		status = "ACTIVE";
3033 	else if (tipc_link_is_up(l_ptr))
3034 		status = "STANDBY";
3035 	else
3036 		status = "DEFUNCT";
3037 	tipc_printf(&pb, "Link <%s>\n"
3038 		         "  %s  MTU:%u  Priority:%u  Tolerance:%u ms"
3039 		         "  Window:%u packets\n",
3040 		    l_ptr->name, status, link_max_pkt(l_ptr),
3041 		    l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
3042 	tipc_printf(&pb, "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
3043 		    l_ptr->next_in_no - l_ptr->stats.recv_info,
3044 		    l_ptr->stats.recv_fragments,
3045 		    l_ptr->stats.recv_fragmented,
3046 		    l_ptr->stats.recv_bundles,
3047 		    l_ptr->stats.recv_bundled);
3048 	tipc_printf(&pb, "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
3049 		    l_ptr->next_out_no - l_ptr->stats.sent_info,
3050 		    l_ptr->stats.sent_fragments,
3051 		    l_ptr->stats.sent_fragmented,
3052 		    l_ptr->stats.sent_bundles,
3053 		    l_ptr->stats.sent_bundled);
3054 	profile_total = l_ptr->stats.msg_length_counts;
3055 	if (!profile_total)
3056 		profile_total = 1;
3057 	tipc_printf(&pb, "  TX profile sample:%u packets  average:%u octets\n"
3058 		         "  0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
3059 		         "-16354:%u%% -32768:%u%% -66000:%u%%\n",
3060 		    l_ptr->stats.msg_length_counts,
3061 		    l_ptr->stats.msg_lengths_total / profile_total,
3062 		    percent(l_ptr->stats.msg_length_profile[0], profile_total),
3063 		    percent(l_ptr->stats.msg_length_profile[1], profile_total),
3064 		    percent(l_ptr->stats.msg_length_profile[2], profile_total),
3065 		    percent(l_ptr->stats.msg_length_profile[3], profile_total),
3066 		    percent(l_ptr->stats.msg_length_profile[4], profile_total),
3067 		    percent(l_ptr->stats.msg_length_profile[5], profile_total),
3068 		    percent(l_ptr->stats.msg_length_profile[6], profile_total));
3069 	tipc_printf(&pb, "  RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
3070 		    l_ptr->stats.recv_states,
3071 		    l_ptr->stats.recv_probes,
3072 		    l_ptr->stats.recv_nacks,
3073 		    l_ptr->stats.deferred_recv,
3074 		    l_ptr->stats.duplicates);
3075 	tipc_printf(&pb, "  TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
3076 		    l_ptr->stats.sent_states,
3077 		    l_ptr->stats.sent_probes,
3078 		    l_ptr->stats.sent_nacks,
3079 		    l_ptr->stats.sent_acks,
3080 		    l_ptr->stats.retransmitted);
3081 	tipc_printf(&pb, "  Congestion bearer:%u link:%u  Send queue max:%u avg:%u\n",
3082 		    l_ptr->stats.bearer_congs,
3083 		    l_ptr->stats.link_congs,
3084 		    l_ptr->stats.max_queue_sz,
3085 		    l_ptr->stats.queue_sz_counts
3086 		    ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
3087 		    : 0);
3088 
3089 	tipc_node_unlock(node);
3090 	read_unlock_bh(&tipc_net_lock);
3091 	return tipc_printbuf_validate(&pb);
3092 }
3093 
3094 #define MAX_LINK_STATS_INFO 2000
3095 
3096 struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
3097 {
3098 	struct sk_buff *buf;
3099 	struct tlv_desc *rep_tlv;
3100 	int str_len;
3101 
3102 	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
3103 		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
3104 
3105 	buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
3106 	if (!buf)
3107 		return NULL;
3108 
3109 	rep_tlv = (struct tlv_desc *)buf->data;
3110 
3111 	str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
3112 				  (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
3113 	if (!str_len) {
3114 		buf_discard(buf);
3115 	       	return tipc_cfg_reply_error_string("link not found");
3116 	}
3117 
3118 	skb_put(buf, TLV_SPACE(str_len));
3119 	TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
3120 
3121 	return buf;
3122 }
3123 
3124 #if 0
3125 int link_control(const char *name, u32 op, u32 val)
3126 {
3127 	int res = -EINVAL;
3128 	struct link *l_ptr;
3129 	u32 bearer_id;
3130 	struct node * node;
3131 	u32 a;
3132 
3133 	a = link_name2addr(name, &bearer_id);
3134 	read_lock_bh(&tipc_net_lock);
3135 	node = tipc_node_find(a);
3136 	if (node) {
3137 		tipc_node_lock(node);
3138 		l_ptr = node->links[bearer_id];
3139 		if (l_ptr) {
3140 			if (op == TIPC_REMOVE_LINK) {
3141 				struct bearer *b_ptr = l_ptr->b_ptr;
3142 				spin_lock_bh(&b_ptr->publ.lock);
3143 				tipc_link_delete(l_ptr);
3144 				spin_unlock_bh(&b_ptr->publ.lock);
3145 			}
3146 			if (op == TIPC_CMD_BLOCK_LINK) {
3147 				tipc_link_reset(l_ptr);
3148 				l_ptr->blocked = 1;
3149 			}
3150 			if (op == TIPC_CMD_UNBLOCK_LINK) {
3151 				l_ptr->blocked = 0;
3152 			}
3153 			res = TIPC_OK;
3154 		}
3155 		tipc_node_unlock(node);
3156 	}
3157 	read_unlock_bh(&tipc_net_lock);
3158 	return res;
3159 }
3160 #endif
3161 
3162 /**
3163  * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
3164  * @dest: network address of destination node
3165  * @selector: used to select from set of active links
3166  *
3167  * If no active link can be found, uses default maximum packet size.
3168  */
3169 
3170 u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
3171 {
3172 	struct node *n_ptr;
3173 	struct link *l_ptr;
3174 	u32 res = MAX_PKT_DEFAULT;
3175 
3176 	if (dest == tipc_own_addr)
3177 		return MAX_MSG_SIZE;
3178 
3179 	read_lock_bh(&tipc_net_lock);
3180 	n_ptr = tipc_node_select(dest, selector);
3181 	if (n_ptr) {
3182 		tipc_node_lock(n_ptr);
3183 		l_ptr = n_ptr->active_links[selector & 1];
3184 		if (l_ptr)
3185 			res = link_max_pkt(l_ptr);
3186 		tipc_node_unlock(n_ptr);
3187 	}
3188 	read_unlock_bh(&tipc_net_lock);
3189 	return res;
3190 }
3191 
3192 #if 0
3193 static void link_dump_rec_queue(struct link *l_ptr)
3194 {
3195 	struct sk_buff *crs;
3196 
3197 	if (!l_ptr->oldest_deferred_in) {
3198 		info("Reception queue empty\n");
3199 		return;
3200 	}
3201 	info("Contents of Reception queue:\n");
3202 	crs = l_ptr->oldest_deferred_in;
3203 	while (crs) {
3204 		if (crs->data == (void *)0x0000a3a3) {
3205 			info("buffer %x invalid\n", crs);
3206 			return;
3207 		}
3208 		msg_dbg(buf_msg(crs), "In rec queue: \n");
3209 		crs = crs->next;
3210 	}
3211 }
3212 #endif
3213 
3214 static void link_dump_send_queue(struct link *l_ptr)
3215 {
3216 	if (l_ptr->next_out) {
3217 		info("\nContents of unsent queue:\n");
3218 		dbg_print_buf_chain(l_ptr->next_out);
3219 	}
3220 	info("\nContents of send queue:\n");
3221 	if (l_ptr->first_out) {
3222 		dbg_print_buf_chain(l_ptr->first_out);
3223 	}
3224 	info("Empty send queue\n");
3225 }
3226 
3227 static void link_print(struct link *l_ptr, struct print_buf *buf,
3228 		       const char *str)
3229 {
3230 	tipc_printf(buf, str);
3231 	if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
3232 		return;
3233 	tipc_printf(buf, "Link %x<%s>:",
3234 		    l_ptr->addr, l_ptr->b_ptr->publ.name);
3235 	tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
3236 	tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
3237 	tipc_printf(buf, "SQUE");
3238 	if (l_ptr->first_out) {
3239 		tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
3240 		if (l_ptr->next_out)
3241 			tipc_printf(buf, "%u..",
3242 				    msg_seqno(buf_msg(l_ptr->next_out)));
3243 		tipc_printf(buf, "%u]",
3244 			    msg_seqno(buf_msg
3245 				      (l_ptr->last_out)), l_ptr->out_queue_size);
3246 		if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
3247 			 msg_seqno(buf_msg(l_ptr->first_out)))
3248 		     != (l_ptr->out_queue_size - 1))
3249 		    || (l_ptr->last_out->next != 0)) {
3250 			tipc_printf(buf, "\nSend queue inconsistency\n");
3251 			tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
3252 			tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
3253 			tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
3254 			link_dump_send_queue(l_ptr);
3255 		}
3256 	} else
3257 		tipc_printf(buf, "[]");
3258 	tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
3259 	if (l_ptr->oldest_deferred_in) {
3260 		u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
3261 		u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
3262 		tipc_printf(buf, ":RQUE[%u..%u]", o, n);
3263 		if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
3264 			tipc_printf(buf, ":RQSIZ(%u)",
3265 				    l_ptr->deferred_inqueue_sz);
3266 		}
3267 	}
3268 	if (link_working_unknown(l_ptr))
3269 		tipc_printf(buf, ":WU");
3270 	if (link_reset_reset(l_ptr))
3271 		tipc_printf(buf, ":RR");
3272 	if (link_reset_unknown(l_ptr))
3273 		tipc_printf(buf, ":RU");
3274 	if (link_working_working(l_ptr))
3275 		tipc_printf(buf, ":WW");
3276 	tipc_printf(buf, "\n");
3277 }
3278 
3279