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