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