]> asedeno.scripts.mit.edu Git - linux.git/blob - net/tipc/link.c
Merge branch 'tipc-link-changeover-issues'
[linux.git] / net / tipc / link.c
1 /*
2  * net/tipc/link.c: TIPC link code
3  *
4  * Copyright (c) 1996-2007, 2012-2016, Ericsson AB
5  * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the names of the copyright holders nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include "core.h"
38 #include "subscr.h"
39 #include "link.h"
40 #include "bcast.h"
41 #include "socket.h"
42 #include "name_distr.h"
43 #include "discover.h"
44 #include "netlink.h"
45 #include "monitor.h"
46 #include "trace.h"
47
48 #include <linux/pkt_sched.h>
49
50 struct tipc_stats {
51         u32 sent_pkts;
52         u32 recv_pkts;
53         u32 sent_states;
54         u32 recv_states;
55         u32 sent_probes;
56         u32 recv_probes;
57         u32 sent_nacks;
58         u32 recv_nacks;
59         u32 sent_acks;
60         u32 sent_bundled;
61         u32 sent_bundles;
62         u32 recv_bundled;
63         u32 recv_bundles;
64         u32 retransmitted;
65         u32 sent_fragmented;
66         u32 sent_fragments;
67         u32 recv_fragmented;
68         u32 recv_fragments;
69         u32 link_congs;         /* # port sends blocked by congestion */
70         u32 deferred_recv;
71         u32 duplicates;
72         u32 max_queue_sz;       /* send queue size high water mark */
73         u32 accu_queue_sz;      /* used for send queue size profiling */
74         u32 queue_sz_counts;    /* used for send queue size profiling */
75         u32 msg_length_counts;  /* used for message length profiling */
76         u32 msg_lengths_total;  /* used for message length profiling */
77         u32 msg_length_profile[7]; /* used for msg. length profiling */
78 };
79
80 /**
81  * struct tipc_link - TIPC link data structure
82  * @addr: network address of link's peer node
83  * @name: link name character string
84  * @media_addr: media address to use when sending messages over link
85  * @timer: link timer
86  * @net: pointer to namespace struct
87  * @refcnt: reference counter for permanent references (owner node & timer)
88  * @peer_session: link session # being used by peer end of link
89  * @peer_bearer_id: bearer id used by link's peer endpoint
90  * @bearer_id: local bearer id used by link
91  * @tolerance: minimum link continuity loss needed to reset link [in ms]
92  * @abort_limit: # of unacknowledged continuity probes needed to reset link
93  * @state: current state of link FSM
94  * @peer_caps: bitmap describing capabilities of peer node
95  * @silent_intv_cnt: # of timer intervals without any reception from peer
96  * @proto_msg: template for control messages generated by link
97  * @pmsg: convenience pointer to "proto_msg" field
98  * @priority: current link priority
99  * @net_plane: current link network plane ('A' through 'H')
100  * @mon_state: cookie with information needed by link monitor
101  * @backlog_limit: backlog queue congestion thresholds (indexed by importance)
102  * @exp_msg_count: # of tunnelled messages expected during link changeover
103  * @reset_rcv_checkpt: seq # of last acknowledged message at time of link reset
104  * @mtu: current maximum packet size for this link
105  * @advertised_mtu: advertised own mtu when link is being established
106  * @transmitq: queue for sent, non-acked messages
107  * @backlogq: queue for messages waiting to be sent
108  * @snt_nxt: next sequence number to use for outbound messages
109  * @prev_from: sequence number of most previous retransmission request
110  * @stale_limit: time when repeated identical retransmits must force link reset
111  * @ackers: # of peers that needs to ack each packet before it can be released
112  * @acked: # last packet acked by a certain peer. Used for broadcast.
113  * @rcv_nxt: next sequence number to expect for inbound messages
114  * @deferred_queue: deferred queue saved OOS b'cast message received from node
115  * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
116  * @inputq: buffer queue for messages to be delivered upwards
117  * @namedq: buffer queue for name table messages to be delivered upwards
118  * @next_out: ptr to first unsent outbound message in queue
119  * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
120  * @long_msg_seq_no: next identifier to use for outbound fragmented messages
121  * @reasm_buf: head of partially reassembled inbound message fragments
122  * @bc_rcvr: marks that this is a broadcast receiver link
123  * @stats: collects statistics regarding link activity
124  */
125 struct tipc_link {
126         u32 addr;
127         char name[TIPC_MAX_LINK_NAME];
128         struct net *net;
129
130         /* Management and link supervision data */
131         u16 peer_session;
132         u16 session;
133         u16 snd_nxt_state;
134         u16 rcv_nxt_state;
135         u32 peer_bearer_id;
136         u32 bearer_id;
137         u32 tolerance;
138         u32 abort_limit;
139         u32 state;
140         u16 peer_caps;
141         bool in_session;
142         bool active;
143         u32 silent_intv_cnt;
144         char if_name[TIPC_MAX_IF_NAME];
145         u32 priority;
146         char net_plane;
147         struct tipc_mon_state mon_state;
148         u16 rst_cnt;
149
150         /* Failover/synch */
151         u16 drop_point;
152         struct sk_buff *failover_reasm_skb;
153         struct sk_buff_head failover_deferdq;
154
155         /* Max packet negotiation */
156         u16 mtu;
157         u16 advertised_mtu;
158
159         /* Sending */
160         struct sk_buff_head transmq;
161         struct sk_buff_head backlogq;
162         struct {
163                 u16 len;
164                 u16 limit;
165         } backlog[5];
166         u16 snd_nxt;
167         u16 prev_from;
168         u16 window;
169         unsigned long stale_limit;
170
171         /* Reception */
172         u16 rcv_nxt;
173         u32 rcv_unacked;
174         struct sk_buff_head deferdq;
175         struct sk_buff_head *inputq;
176         struct sk_buff_head *namedq;
177
178         /* Congestion handling */
179         struct sk_buff_head wakeupq;
180
181         /* Fragmentation/reassembly */
182         struct sk_buff *reasm_buf;
183         struct sk_buff *reasm_tnlmsg;
184
185         /* Broadcast */
186         u16 ackers;
187         u16 acked;
188         struct tipc_link *bc_rcvlink;
189         struct tipc_link *bc_sndlink;
190         u8 nack_state;
191         bool bc_peer_is_up;
192
193         /* Statistics */
194         struct tipc_stats stats;
195 };
196
197 /*
198  * Error message prefixes
199  */
200 static const char *link_co_err = "Link tunneling error, ";
201 static const char *link_rst_msg = "Resetting link ";
202
203 /* Send states for broadcast NACKs
204  */
205 enum {
206         BC_NACK_SND_CONDITIONAL,
207         BC_NACK_SND_UNCONDITIONAL,
208         BC_NACK_SND_SUPPRESS,
209 };
210
211 #define TIPC_BC_RETR_LIM  (jiffies + msecs_to_jiffies(10))
212 #define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
213
214 /*
215  * Interval between NACKs when packets arrive out of order
216  */
217 #define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
218
219 /* Link FSM states:
220  */
221 enum {
222         LINK_ESTABLISHED     = 0xe,
223         LINK_ESTABLISHING    = 0xe  << 4,
224         LINK_RESET           = 0x1  << 8,
225         LINK_RESETTING       = 0x2  << 12,
226         LINK_PEER_RESET      = 0xd  << 16,
227         LINK_FAILINGOVER     = 0xf  << 20,
228         LINK_SYNCHING        = 0xc  << 24
229 };
230
231 /* Link FSM state checking routines
232  */
233 static int link_is_up(struct tipc_link *l)
234 {
235         return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
236 }
237
238 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
239                                struct sk_buff_head *xmitq);
240 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
241                                       bool probe_reply, u16 rcvgap,
242                                       int tolerance, int priority,
243                                       struct sk_buff_head *xmitq);
244 static void link_print(struct tipc_link *l, const char *str);
245 static int tipc_link_build_nack_msg(struct tipc_link *l,
246                                     struct sk_buff_head *xmitq);
247 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
248                                         struct sk_buff_head *xmitq);
249 static bool tipc_link_release_pkts(struct tipc_link *l, u16 to);
250 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data);
251 static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
252                                      struct tipc_gap_ack_blks *ga,
253                                      struct sk_buff_head *xmitq);
254
255 /*
256  *  Simple non-static link routines (i.e. referenced outside this file)
257  */
258 bool tipc_link_is_up(struct tipc_link *l)
259 {
260         return link_is_up(l);
261 }
262
263 bool tipc_link_peer_is_down(struct tipc_link *l)
264 {
265         return l->state == LINK_PEER_RESET;
266 }
267
268 bool tipc_link_is_reset(struct tipc_link *l)
269 {
270         return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
271 }
272
273 bool tipc_link_is_establishing(struct tipc_link *l)
274 {
275         return l->state == LINK_ESTABLISHING;
276 }
277
278 bool tipc_link_is_synching(struct tipc_link *l)
279 {
280         return l->state == LINK_SYNCHING;
281 }
282
283 bool tipc_link_is_failingover(struct tipc_link *l)
284 {
285         return l->state == LINK_FAILINGOVER;
286 }
287
288 bool tipc_link_is_blocked(struct tipc_link *l)
289 {
290         return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
291 }
292
293 static bool link_is_bc_sndlink(struct tipc_link *l)
294 {
295         return !l->bc_sndlink;
296 }
297
298 static bool link_is_bc_rcvlink(struct tipc_link *l)
299 {
300         return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
301 }
302
303 void tipc_link_set_active(struct tipc_link *l, bool active)
304 {
305         l->active = active;
306 }
307
308 u32 tipc_link_id(struct tipc_link *l)
309 {
310         return l->peer_bearer_id << 16 | l->bearer_id;
311 }
312
313 int tipc_link_window(struct tipc_link *l)
314 {
315         return l->window;
316 }
317
318 int tipc_link_prio(struct tipc_link *l)
319 {
320         return l->priority;
321 }
322
323 unsigned long tipc_link_tolerance(struct tipc_link *l)
324 {
325         return l->tolerance;
326 }
327
328 struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
329 {
330         return l->inputq;
331 }
332
333 char tipc_link_plane(struct tipc_link *l)
334 {
335         return l->net_plane;
336 }
337
338 void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
339 {
340         l->peer_caps = capabilities;
341 }
342
343 void tipc_link_add_bc_peer(struct tipc_link *snd_l,
344                            struct tipc_link *uc_l,
345                            struct sk_buff_head *xmitq)
346 {
347         struct tipc_link *rcv_l = uc_l->bc_rcvlink;
348
349         snd_l->ackers++;
350         rcv_l->acked = snd_l->snd_nxt - 1;
351         snd_l->state = LINK_ESTABLISHED;
352         tipc_link_build_bc_init_msg(uc_l, xmitq);
353 }
354
355 void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
356                               struct tipc_link *rcv_l,
357                               struct sk_buff_head *xmitq)
358 {
359         u16 ack = snd_l->snd_nxt - 1;
360
361         snd_l->ackers--;
362         rcv_l->bc_peer_is_up = true;
363         rcv_l->state = LINK_ESTABLISHED;
364         tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
365         trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
366         tipc_link_reset(rcv_l);
367         rcv_l->state = LINK_RESET;
368         if (!snd_l->ackers) {
369                 trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
370                 tipc_link_reset(snd_l);
371                 snd_l->state = LINK_RESET;
372                 __skb_queue_purge(xmitq);
373         }
374 }
375
376 int tipc_link_bc_peers(struct tipc_link *l)
377 {
378         return l->ackers;
379 }
380
381 static u16 link_bc_rcv_gap(struct tipc_link *l)
382 {
383         struct sk_buff *skb = skb_peek(&l->deferdq);
384         u16 gap = 0;
385
386         if (more(l->snd_nxt, l->rcv_nxt))
387                 gap = l->snd_nxt - l->rcv_nxt;
388         if (skb)
389                 gap = buf_seqno(skb) - l->rcv_nxt;
390         return gap;
391 }
392
393 void tipc_link_set_mtu(struct tipc_link *l, int mtu)
394 {
395         l->mtu = mtu;
396 }
397
398 int tipc_link_mtu(struct tipc_link *l)
399 {
400         return l->mtu;
401 }
402
403 u16 tipc_link_rcv_nxt(struct tipc_link *l)
404 {
405         return l->rcv_nxt;
406 }
407
408 u16 tipc_link_acked(struct tipc_link *l)
409 {
410         return l->acked;
411 }
412
413 char *tipc_link_name(struct tipc_link *l)
414 {
415         return l->name;
416 }
417
418 u32 tipc_link_state(struct tipc_link *l)
419 {
420         return l->state;
421 }
422
423 /**
424  * tipc_link_create - create a new link
425  * @n: pointer to associated node
426  * @if_name: associated interface name
427  * @bearer_id: id (index) of associated bearer
428  * @tolerance: link tolerance to be used by link
429  * @net_plane: network plane (A,B,c..) this link belongs to
430  * @mtu: mtu to be advertised by link
431  * @priority: priority to be used by link
432  * @window: send window to be used by link
433  * @session: session to be used by link
434  * @ownnode: identity of own node
435  * @peer: node id of peer node
436  * @peer_caps: bitmap describing peer node capabilities
437  * @bc_sndlink: the namespace global link used for broadcast sending
438  * @bc_rcvlink: the peer specific link used for broadcast reception
439  * @inputq: queue to put messages ready for delivery
440  * @namedq: queue to put binding table update messages ready for delivery
441  * @link: return value, pointer to put the created link
442  *
443  * Returns true if link was created, otherwise false
444  */
445 bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
446                       int tolerance, char net_plane, u32 mtu, int priority,
447                       int window, u32 session, u32 self,
448                       u32 peer, u8 *peer_id, u16 peer_caps,
449                       struct tipc_link *bc_sndlink,
450                       struct tipc_link *bc_rcvlink,
451                       struct sk_buff_head *inputq,
452                       struct sk_buff_head *namedq,
453                       struct tipc_link **link)
454 {
455         char peer_str[NODE_ID_STR_LEN] = {0,};
456         char self_str[NODE_ID_STR_LEN] = {0,};
457         struct tipc_link *l;
458
459         l = kzalloc(sizeof(*l), GFP_ATOMIC);
460         if (!l)
461                 return false;
462         *link = l;
463         l->session = session;
464
465         /* Set link name for unicast links only */
466         if (peer_id) {
467                 tipc_nodeid2string(self_str, tipc_own_id(net));
468                 if (strlen(self_str) > 16)
469                         sprintf(self_str, "%x", self);
470                 tipc_nodeid2string(peer_str, peer_id);
471                 if (strlen(peer_str) > 16)
472                         sprintf(peer_str, "%x", peer);
473         }
474         /* Peer i/f name will be completed by reset/activate message */
475         snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
476                  self_str, if_name, peer_str);
477
478         strcpy(l->if_name, if_name);
479         l->addr = peer;
480         l->peer_caps = peer_caps;
481         l->net = net;
482         l->in_session = false;
483         l->bearer_id = bearer_id;
484         l->tolerance = tolerance;
485         if (bc_rcvlink)
486                 bc_rcvlink->tolerance = tolerance;
487         l->net_plane = net_plane;
488         l->advertised_mtu = mtu;
489         l->mtu = mtu;
490         l->priority = priority;
491         tipc_link_set_queue_limits(l, window);
492         l->ackers = 1;
493         l->bc_sndlink = bc_sndlink;
494         l->bc_rcvlink = bc_rcvlink;
495         l->inputq = inputq;
496         l->namedq = namedq;
497         l->state = LINK_RESETTING;
498         __skb_queue_head_init(&l->transmq);
499         __skb_queue_head_init(&l->backlogq);
500         __skb_queue_head_init(&l->deferdq);
501         __skb_queue_head_init(&l->failover_deferdq);
502         skb_queue_head_init(&l->wakeupq);
503         skb_queue_head_init(l->inputq);
504         return true;
505 }
506
507 /**
508  * tipc_link_bc_create - create new link to be used for broadcast
509  * @n: pointer to associated node
510  * @mtu: mtu to be used initially if no peers
511  * @window: send window to be used
512  * @inputq: queue to put messages ready for delivery
513  * @namedq: queue to put binding table update messages ready for delivery
514  * @link: return value, pointer to put the created link
515  *
516  * Returns true if link was created, otherwise false
517  */
518 bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
519                          int mtu, int window, u16 peer_caps,
520                          struct sk_buff_head *inputq,
521                          struct sk_buff_head *namedq,
522                          struct tipc_link *bc_sndlink,
523                          struct tipc_link **link)
524 {
525         struct tipc_link *l;
526
527         if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
528                               0, ownnode, peer, NULL, peer_caps, bc_sndlink,
529                               NULL, inputq, namedq, link))
530                 return false;
531
532         l = *link;
533         strcpy(l->name, tipc_bclink_name);
534         trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
535         tipc_link_reset(l);
536         l->state = LINK_RESET;
537         l->ackers = 0;
538         l->bc_rcvlink = l;
539
540         /* Broadcast send link is always up */
541         if (link_is_bc_sndlink(l))
542                 l->state = LINK_ESTABLISHED;
543
544         /* Disable replicast if even a single peer doesn't support it */
545         if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
546                 tipc_bcast_disable_rcast(net);
547
548         return true;
549 }
550
551 /**
552  * tipc_link_fsm_evt - link finite state machine
553  * @l: pointer to link
554  * @evt: state machine event to be processed
555  */
556 int tipc_link_fsm_evt(struct tipc_link *l, int evt)
557 {
558         int rc = 0;
559         int old_state = l->state;
560
561         switch (l->state) {
562         case LINK_RESETTING:
563                 switch (evt) {
564                 case LINK_PEER_RESET_EVT:
565                         l->state = LINK_PEER_RESET;
566                         break;
567                 case LINK_RESET_EVT:
568                         l->state = LINK_RESET;
569                         break;
570                 case LINK_FAILURE_EVT:
571                 case LINK_FAILOVER_BEGIN_EVT:
572                 case LINK_ESTABLISH_EVT:
573                 case LINK_FAILOVER_END_EVT:
574                 case LINK_SYNCH_BEGIN_EVT:
575                 case LINK_SYNCH_END_EVT:
576                 default:
577                         goto illegal_evt;
578                 }
579                 break;
580         case LINK_RESET:
581                 switch (evt) {
582                 case LINK_PEER_RESET_EVT:
583                         l->state = LINK_ESTABLISHING;
584                         break;
585                 case LINK_FAILOVER_BEGIN_EVT:
586                         l->state = LINK_FAILINGOVER;
587                 case LINK_FAILURE_EVT:
588                 case LINK_RESET_EVT:
589                 case LINK_ESTABLISH_EVT:
590                 case LINK_FAILOVER_END_EVT:
591                         break;
592                 case LINK_SYNCH_BEGIN_EVT:
593                 case LINK_SYNCH_END_EVT:
594                 default:
595                         goto illegal_evt;
596                 }
597                 break;
598         case LINK_PEER_RESET:
599                 switch (evt) {
600                 case LINK_RESET_EVT:
601                         l->state = LINK_ESTABLISHING;
602                         break;
603                 case LINK_PEER_RESET_EVT:
604                 case LINK_ESTABLISH_EVT:
605                 case LINK_FAILURE_EVT:
606                         break;
607                 case LINK_SYNCH_BEGIN_EVT:
608                 case LINK_SYNCH_END_EVT:
609                 case LINK_FAILOVER_BEGIN_EVT:
610                 case LINK_FAILOVER_END_EVT:
611                 default:
612                         goto illegal_evt;
613                 }
614                 break;
615         case LINK_FAILINGOVER:
616                 switch (evt) {
617                 case LINK_FAILOVER_END_EVT:
618                         l->state = LINK_RESET;
619                         break;
620                 case LINK_PEER_RESET_EVT:
621                 case LINK_RESET_EVT:
622                 case LINK_ESTABLISH_EVT:
623                 case LINK_FAILURE_EVT:
624                         break;
625                 case LINK_FAILOVER_BEGIN_EVT:
626                 case LINK_SYNCH_BEGIN_EVT:
627                 case LINK_SYNCH_END_EVT:
628                 default:
629                         goto illegal_evt;
630                 }
631                 break;
632         case LINK_ESTABLISHING:
633                 switch (evt) {
634                 case LINK_ESTABLISH_EVT:
635                         l->state = LINK_ESTABLISHED;
636                         break;
637                 case LINK_FAILOVER_BEGIN_EVT:
638                         l->state = LINK_FAILINGOVER;
639                         break;
640                 case LINK_RESET_EVT:
641                         l->state = LINK_RESET;
642                         break;
643                 case LINK_FAILURE_EVT:
644                 case LINK_PEER_RESET_EVT:
645                 case LINK_SYNCH_BEGIN_EVT:
646                 case LINK_FAILOVER_END_EVT:
647                         break;
648                 case LINK_SYNCH_END_EVT:
649                 default:
650                         goto illegal_evt;
651                 }
652                 break;
653         case LINK_ESTABLISHED:
654                 switch (evt) {
655                 case LINK_PEER_RESET_EVT:
656                         l->state = LINK_PEER_RESET;
657                         rc |= TIPC_LINK_DOWN_EVT;
658                         break;
659                 case LINK_FAILURE_EVT:
660                         l->state = LINK_RESETTING;
661                         rc |= TIPC_LINK_DOWN_EVT;
662                         break;
663                 case LINK_RESET_EVT:
664                         l->state = LINK_RESET;
665                         break;
666                 case LINK_ESTABLISH_EVT:
667                 case LINK_SYNCH_END_EVT:
668                         break;
669                 case LINK_SYNCH_BEGIN_EVT:
670                         l->state = LINK_SYNCHING;
671                         break;
672                 case LINK_FAILOVER_BEGIN_EVT:
673                 case LINK_FAILOVER_END_EVT:
674                 default:
675                         goto illegal_evt;
676                 }
677                 break;
678         case LINK_SYNCHING:
679                 switch (evt) {
680                 case LINK_PEER_RESET_EVT:
681                         l->state = LINK_PEER_RESET;
682                         rc |= TIPC_LINK_DOWN_EVT;
683                         break;
684                 case LINK_FAILURE_EVT:
685                         l->state = LINK_RESETTING;
686                         rc |= TIPC_LINK_DOWN_EVT;
687                         break;
688                 case LINK_RESET_EVT:
689                         l->state = LINK_RESET;
690                         break;
691                 case LINK_ESTABLISH_EVT:
692                 case LINK_SYNCH_BEGIN_EVT:
693                         break;
694                 case LINK_SYNCH_END_EVT:
695                         l->state = LINK_ESTABLISHED;
696                         break;
697                 case LINK_FAILOVER_BEGIN_EVT:
698                 case LINK_FAILOVER_END_EVT:
699                 default:
700                         goto illegal_evt;
701                 }
702                 break;
703         default:
704                 pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
705         }
706         trace_tipc_link_fsm(l->name, old_state, l->state, evt);
707         return rc;
708 illegal_evt:
709         pr_err("Illegal FSM event %x in state %x on link %s\n",
710                evt, l->state, l->name);
711         trace_tipc_link_fsm(l->name, old_state, l->state, evt);
712         return rc;
713 }
714
715 /* link_profile_stats - update statistical profiling of traffic
716  */
717 static void link_profile_stats(struct tipc_link *l)
718 {
719         struct sk_buff *skb;
720         struct tipc_msg *msg;
721         int length;
722
723         /* Update counters used in statistical profiling of send traffic */
724         l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
725         l->stats.queue_sz_counts++;
726
727         skb = skb_peek(&l->transmq);
728         if (!skb)
729                 return;
730         msg = buf_msg(skb);
731         length = msg_size(msg);
732
733         if (msg_user(msg) == MSG_FRAGMENTER) {
734                 if (msg_type(msg) != FIRST_FRAGMENT)
735                         return;
736                 length = msg_size(msg_inner_hdr(msg));
737         }
738         l->stats.msg_lengths_total += length;
739         l->stats.msg_length_counts++;
740         if (length <= 64)
741                 l->stats.msg_length_profile[0]++;
742         else if (length <= 256)
743                 l->stats.msg_length_profile[1]++;
744         else if (length <= 1024)
745                 l->stats.msg_length_profile[2]++;
746         else if (length <= 4096)
747                 l->stats.msg_length_profile[3]++;
748         else if (length <= 16384)
749                 l->stats.msg_length_profile[4]++;
750         else if (length <= 32768)
751                 l->stats.msg_length_profile[5]++;
752         else
753                 l->stats.msg_length_profile[6]++;
754 }
755
756 /**
757  * tipc_link_too_silent - check if link is "too silent"
758  * @l: tipc link to be checked
759  *
760  * Returns true if the link 'silent_intv_cnt' is about to reach the
761  * 'abort_limit' value, otherwise false
762  */
763 bool tipc_link_too_silent(struct tipc_link *l)
764 {
765         return (l->silent_intv_cnt + 2 > l->abort_limit);
766 }
767
768 /* tipc_link_timeout - perform periodic task as instructed from node timeout
769  */
770 int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
771 {
772         int mtyp = 0;
773         int rc = 0;
774         bool state = false;
775         bool probe = false;
776         bool setup = false;
777         u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
778         u16 bc_acked = l->bc_rcvlink->acked;
779         struct tipc_mon_state *mstate = &l->mon_state;
780
781         trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
782         trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
783         switch (l->state) {
784         case LINK_ESTABLISHED:
785         case LINK_SYNCHING:
786                 mtyp = STATE_MSG;
787                 link_profile_stats(l);
788                 tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
789                 if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
790                         return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
791                 state = bc_acked != bc_snt;
792                 state |= l->bc_rcvlink->rcv_unacked;
793                 state |= l->rcv_unacked;
794                 state |= !skb_queue_empty(&l->transmq);
795                 state |= !skb_queue_empty(&l->deferdq);
796                 probe = mstate->probing;
797                 probe |= l->silent_intv_cnt;
798                 if (probe || mstate->monitoring)
799                         l->silent_intv_cnt++;
800                 break;
801         case LINK_RESET:
802                 setup = l->rst_cnt++ <= 4;
803                 setup |= !(l->rst_cnt % 16);
804                 mtyp = RESET_MSG;
805                 break;
806         case LINK_ESTABLISHING:
807                 setup = true;
808                 mtyp = ACTIVATE_MSG;
809                 break;
810         case LINK_PEER_RESET:
811         case LINK_RESETTING:
812         case LINK_FAILINGOVER:
813                 break;
814         default:
815                 break;
816         }
817
818         if (state || probe || setup)
819                 tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
820
821         return rc;
822 }
823
824 /**
825  * link_schedule_user - schedule a message sender for wakeup after congestion
826  * @l: congested link
827  * @hdr: header of message that is being sent
828  * Create pseudo msg to send back to user when congestion abates
829  */
830 static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
831 {
832         u32 dnode = tipc_own_addr(l->net);
833         u32 dport = msg_origport(hdr);
834         struct sk_buff *skb;
835
836         /* Create and schedule wakeup pseudo message */
837         skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
838                               dnode, l->addr, dport, 0, 0);
839         if (!skb)
840                 return -ENOBUFS;
841         msg_set_dest_droppable(buf_msg(skb), true);
842         TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
843         skb_queue_tail(&l->wakeupq, skb);
844         l->stats.link_congs++;
845         trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
846         return -ELINKCONG;
847 }
848
849 /**
850  * link_prepare_wakeup - prepare users for wakeup after congestion
851  * @l: congested link
852  * Wake up a number of waiting users, as permitted by available space
853  * in the send queue
854  */
855 static void link_prepare_wakeup(struct tipc_link *l)
856 {
857         struct sk_buff *skb, *tmp;
858         int imp, i = 0;
859
860         skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
861                 imp = TIPC_SKB_CB(skb)->chain_imp;
862                 if (l->backlog[imp].len < l->backlog[imp].limit) {
863                         skb_unlink(skb, &l->wakeupq);
864                         skb_queue_tail(l->inputq, skb);
865                 } else if (i++ > 10) {
866                         break;
867                 }
868         }
869 }
870
871 void tipc_link_reset(struct tipc_link *l)
872 {
873         struct sk_buff_head list;
874
875         __skb_queue_head_init(&list);
876
877         l->in_session = false;
878         /* Force re-synch of peer session number before establishing */
879         l->peer_session--;
880         l->session++;
881         l->mtu = l->advertised_mtu;
882
883         spin_lock_bh(&l->wakeupq.lock);
884         skb_queue_splice_init(&l->wakeupq, &list);
885         spin_unlock_bh(&l->wakeupq.lock);
886
887         spin_lock_bh(&l->inputq->lock);
888         skb_queue_splice_init(&list, l->inputq);
889         spin_unlock_bh(&l->inputq->lock);
890
891         __skb_queue_purge(&l->transmq);
892         __skb_queue_purge(&l->deferdq);
893         __skb_queue_purge(&l->backlogq);
894         __skb_queue_purge(&l->failover_deferdq);
895         l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
896         l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
897         l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
898         l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
899         l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
900         kfree_skb(l->reasm_buf);
901         kfree_skb(l->reasm_tnlmsg);
902         kfree_skb(l->failover_reasm_skb);
903         l->reasm_buf = NULL;
904         l->reasm_tnlmsg = NULL;
905         l->failover_reasm_skb = NULL;
906         l->rcv_unacked = 0;
907         l->snd_nxt = 1;
908         l->rcv_nxt = 1;
909         l->snd_nxt_state = 1;
910         l->rcv_nxt_state = 1;
911         l->acked = 0;
912         l->silent_intv_cnt = 0;
913         l->rst_cnt = 0;
914         l->bc_peer_is_up = false;
915         memset(&l->mon_state, 0, sizeof(l->mon_state));
916         tipc_link_reset_stats(l);
917 }
918
919 /**
920  * tipc_link_xmit(): enqueue buffer list according to queue situation
921  * @link: link to use
922  * @list: chain of buffers containing message
923  * @xmitq: returned list of packets to be sent by caller
924  *
925  * Consumes the buffer chain.
926  * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
927  * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
928  */
929 int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
930                    struct sk_buff_head *xmitq)
931 {
932         struct tipc_msg *hdr = buf_msg(skb_peek(list));
933         unsigned int maxwin = l->window;
934         int imp = msg_importance(hdr);
935         unsigned int mtu = l->mtu;
936         u16 ack = l->rcv_nxt - 1;
937         u16 seqno = l->snd_nxt;
938         u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
939         struct sk_buff_head *transmq = &l->transmq;
940         struct sk_buff_head *backlogq = &l->backlogq;
941         struct sk_buff *skb, *_skb, *bskb;
942         int pkt_cnt = skb_queue_len(list);
943         int rc = 0;
944
945         if (unlikely(msg_size(hdr) > mtu)) {
946                 pr_warn("Too large msg, purging xmit list %d %d %d %d %d!\n",
947                         skb_queue_len(list), msg_user(hdr),
948                         msg_type(hdr), msg_size(hdr), mtu);
949                 skb_queue_purge(list);
950                 return -EMSGSIZE;
951         }
952
953         /* Allow oversubscription of one data msg per source at congestion */
954         if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
955                 if (imp == TIPC_SYSTEM_IMPORTANCE) {
956                         pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
957                         return -ENOBUFS;
958                 }
959                 rc = link_schedule_user(l, hdr);
960         }
961
962         if (pkt_cnt > 1) {
963                 l->stats.sent_fragmented++;
964                 l->stats.sent_fragments += pkt_cnt;
965         }
966
967         /* Prepare each packet for sending, and add to relevant queue: */
968         while (skb_queue_len(list)) {
969                 skb = skb_peek(list);
970                 hdr = buf_msg(skb);
971                 msg_set_seqno(hdr, seqno);
972                 msg_set_ack(hdr, ack);
973                 msg_set_bcast_ack(hdr, bc_ack);
974
975                 if (likely(skb_queue_len(transmq) < maxwin)) {
976                         _skb = skb_clone(skb, GFP_ATOMIC);
977                         if (!_skb) {
978                                 skb_queue_purge(list);
979                                 return -ENOBUFS;
980                         }
981                         __skb_dequeue(list);
982                         __skb_queue_tail(transmq, skb);
983                         /* next retransmit attempt */
984                         if (link_is_bc_sndlink(l))
985                                 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
986                         __skb_queue_tail(xmitq, _skb);
987                         TIPC_SKB_CB(skb)->ackers = l->ackers;
988                         l->rcv_unacked = 0;
989                         l->stats.sent_pkts++;
990                         seqno++;
991                         continue;
992                 }
993                 if (tipc_msg_bundle(skb_peek_tail(backlogq), hdr, mtu)) {
994                         kfree_skb(__skb_dequeue(list));
995                         l->stats.sent_bundled++;
996                         continue;
997                 }
998                 if (tipc_msg_make_bundle(&bskb, hdr, mtu, l->addr)) {
999                         kfree_skb(__skb_dequeue(list));
1000                         __skb_queue_tail(backlogq, bskb);
1001                         l->backlog[msg_importance(buf_msg(bskb))].len++;
1002                         l->stats.sent_bundled++;
1003                         l->stats.sent_bundles++;
1004                         continue;
1005                 }
1006                 l->backlog[imp].len += skb_queue_len(list);
1007                 skb_queue_splice_tail_init(list, backlogq);
1008         }
1009         l->snd_nxt = seqno;
1010         return rc;
1011 }
1012
1013 static void tipc_link_advance_backlog(struct tipc_link *l,
1014                                       struct sk_buff_head *xmitq)
1015 {
1016         struct sk_buff *skb, *_skb;
1017         struct tipc_msg *hdr;
1018         u16 seqno = l->snd_nxt;
1019         u16 ack = l->rcv_nxt - 1;
1020         u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1021
1022         while (skb_queue_len(&l->transmq) < l->window) {
1023                 skb = skb_peek(&l->backlogq);
1024                 if (!skb)
1025                         break;
1026                 _skb = skb_clone(skb, GFP_ATOMIC);
1027                 if (!_skb)
1028                         break;
1029                 __skb_dequeue(&l->backlogq);
1030                 hdr = buf_msg(skb);
1031                 l->backlog[msg_importance(hdr)].len--;
1032                 __skb_queue_tail(&l->transmq, skb);
1033                 /* next retransmit attempt */
1034                 if (link_is_bc_sndlink(l))
1035                         TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1036
1037                 __skb_queue_tail(xmitq, _skb);
1038                 TIPC_SKB_CB(skb)->ackers = l->ackers;
1039                 msg_set_seqno(hdr, seqno);
1040                 msg_set_ack(hdr, ack);
1041                 msg_set_bcast_ack(hdr, bc_ack);
1042                 l->rcv_unacked = 0;
1043                 l->stats.sent_pkts++;
1044                 seqno++;
1045         }
1046         l->snd_nxt = seqno;
1047 }
1048
1049 /**
1050  * link_retransmit_failure() - Detect repeated retransmit failures
1051  * @l: tipc link sender
1052  * @r: tipc link receiver (= l in case of unicast)
1053  * @from: seqno of the 1st packet in retransmit request
1054  * @rc: returned code
1055  *
1056  * Return: true if the repeated retransmit failures happens, otherwise
1057  * false
1058  */
1059 static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
1060                                     u16 from, int *rc)
1061 {
1062         struct sk_buff *skb = skb_peek(&l->transmq);
1063         struct tipc_msg *hdr;
1064
1065         if (!skb)
1066                 return false;
1067         hdr = buf_msg(skb);
1068
1069         /* Detect repeated retransmit failures on same packet */
1070         if (r->prev_from != from) {
1071                 r->prev_from = from;
1072                 r->stale_limit = jiffies + msecs_to_jiffies(r->tolerance);
1073         } else if (time_after(jiffies, r->stale_limit)) {
1074                 pr_warn("Retransmission failure on link <%s>\n", l->name);
1075                 link_print(l, "State of link ");
1076                 pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
1077                         msg_user(hdr), msg_type(hdr), msg_size(hdr),
1078                         msg_errcode(hdr));
1079                 pr_info("sqno %u, prev: %x, src: %x\n",
1080                         msg_seqno(hdr), msg_prevnode(hdr), msg_orignode(hdr));
1081
1082                 trace_tipc_list_dump(&l->transmq, true, "retrans failure!");
1083                 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "retrans failure!");
1084                 trace_tipc_link_dump(r, TIPC_DUMP_NONE, "retrans failure!");
1085
1086                 if (link_is_bc_sndlink(l))
1087                         *rc = TIPC_LINK_DOWN_EVT;
1088
1089                 *rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1090                 return true;
1091         }
1092
1093         return false;
1094 }
1095
1096 /* tipc_link_bc_retrans() - retransmit zero or more packets
1097  * @l: the link to transmit on
1098  * @r: the receiving link ordering the retransmit. Same as l if unicast
1099  * @from: retransmit from (inclusive) this sequence number
1100  * @to: retransmit to (inclusive) this sequence number
1101  * xmitq: queue for accumulating the retransmitted packets
1102  */
1103 static int tipc_link_bc_retrans(struct tipc_link *l, struct tipc_link *r,
1104                                 u16 from, u16 to, struct sk_buff_head *xmitq)
1105 {
1106         struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
1107         u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1108         u16 ack = l->rcv_nxt - 1;
1109         struct tipc_msg *hdr;
1110         int rc = 0;
1111
1112         if (!skb)
1113                 return 0;
1114         if (less(to, from))
1115                 return 0;
1116
1117         trace_tipc_link_retrans(r, from, to, &l->transmq);
1118
1119         if (link_retransmit_failure(l, r, from, &rc))
1120                 return rc;
1121
1122         skb_queue_walk(&l->transmq, skb) {
1123                 hdr = buf_msg(skb);
1124                 if (less(msg_seqno(hdr), from))
1125                         continue;
1126                 if (more(msg_seqno(hdr), to))
1127                         break;
1128                 if (link_is_bc_sndlink(l)) {
1129                         if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1130                                 continue;
1131                         TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
1132                 }
1133                 _skb = __pskb_copy(skb, LL_MAX_HEADER + MIN_H_SIZE, GFP_ATOMIC);
1134                 if (!_skb)
1135                         return 0;
1136                 hdr = buf_msg(_skb);
1137                 msg_set_ack(hdr, ack);
1138                 msg_set_bcast_ack(hdr, bc_ack);
1139                 _skb->priority = TC_PRIO_CONTROL;
1140                 __skb_queue_tail(xmitq, _skb);
1141                 l->stats.retransmitted++;
1142         }
1143         return 0;
1144 }
1145
1146 /* tipc_data_input - deliver data and name distr msgs to upper layer
1147  *
1148  * Consumes buffer if message is of right type
1149  * Node lock must be held
1150  */
1151 static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
1152                             struct sk_buff_head *inputq)
1153 {
1154         struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
1155         struct tipc_msg *hdr = buf_msg(skb);
1156
1157         switch (msg_user(hdr)) {
1158         case TIPC_LOW_IMPORTANCE:
1159         case TIPC_MEDIUM_IMPORTANCE:
1160         case TIPC_HIGH_IMPORTANCE:
1161         case TIPC_CRITICAL_IMPORTANCE:
1162                 if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
1163                         skb_queue_tail(mc_inputq, skb);
1164                         return true;
1165                 }
1166                 /* fall through */
1167         case CONN_MANAGER:
1168                 skb_queue_tail(inputq, skb);
1169                 return true;
1170         case GROUP_PROTOCOL:
1171                 skb_queue_tail(mc_inputq, skb);
1172                 return true;
1173         case NAME_DISTRIBUTOR:
1174                 l->bc_rcvlink->state = LINK_ESTABLISHED;
1175                 skb_queue_tail(l->namedq, skb);
1176                 return true;
1177         case MSG_BUNDLER:
1178         case TUNNEL_PROTOCOL:
1179         case MSG_FRAGMENTER:
1180         case BCAST_PROTOCOL:
1181                 return false;
1182         default:
1183                 pr_warn("Dropping received illegal msg type\n");
1184                 kfree_skb(skb);
1185                 return true;
1186         };
1187 }
1188
1189 /* tipc_link_input - process packet that has passed link protocol check
1190  *
1191  * Consumes buffer
1192  */
1193 static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
1194                            struct sk_buff_head *inputq,
1195                            struct sk_buff **reasm_skb)
1196 {
1197         struct tipc_msg *hdr = buf_msg(skb);
1198         struct sk_buff *iskb;
1199         struct sk_buff_head tmpq;
1200         int usr = msg_user(hdr);
1201         int pos = 0;
1202
1203         if (usr == MSG_BUNDLER) {
1204                 skb_queue_head_init(&tmpq);
1205                 l->stats.recv_bundles++;
1206                 l->stats.recv_bundled += msg_msgcnt(hdr);
1207                 while (tipc_msg_extract(skb, &iskb, &pos))
1208                         tipc_data_input(l, iskb, &tmpq);
1209                 tipc_skb_queue_splice_tail(&tmpq, inputq);
1210                 return 0;
1211         } else if (usr == MSG_FRAGMENTER) {
1212                 l->stats.recv_fragments++;
1213                 if (tipc_buf_append(reasm_skb, &skb)) {
1214                         l->stats.recv_fragmented++;
1215                         tipc_data_input(l, skb, inputq);
1216                 } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
1217                         pr_warn_ratelimited("Unable to build fragment list\n");
1218                         return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1219                 }
1220                 return 0;
1221         } else if (usr == BCAST_PROTOCOL) {
1222                 tipc_bcast_lock(l->net);
1223                 tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
1224                 tipc_bcast_unlock(l->net);
1225         }
1226
1227         kfree_skb(skb);
1228         return 0;
1229 }
1230
1231 /* tipc_link_tnl_rcv() - receive TUNNEL_PROTOCOL message, drop or process the
1232  *                       inner message along with the ones in the old link's
1233  *                       deferdq
1234  * @l: tunnel link
1235  * @skb: TUNNEL_PROTOCOL message
1236  * @inputq: queue to put messages ready for delivery
1237  */
1238 static int tipc_link_tnl_rcv(struct tipc_link *l, struct sk_buff *skb,
1239                              struct sk_buff_head *inputq)
1240 {
1241         struct sk_buff **reasm_skb = &l->failover_reasm_skb;
1242         struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
1243         struct sk_buff_head *fdefq = &l->failover_deferdq;
1244         struct tipc_msg *hdr = buf_msg(skb);
1245         struct sk_buff *iskb;
1246         int ipos = 0;
1247         int rc = 0;
1248         u16 seqno;
1249
1250         if (msg_type(hdr) == SYNCH_MSG) {
1251                 kfree_skb(skb);
1252                 return 0;
1253         }
1254
1255         /* Not a fragment? */
1256         if (likely(!msg_nof_fragms(hdr))) {
1257                 if (unlikely(!tipc_msg_extract(skb, &iskb, &ipos))) {
1258                         pr_warn_ratelimited("Unable to extract msg, defq: %d\n",
1259                                             skb_queue_len(fdefq));
1260                         return 0;
1261                 }
1262                 kfree_skb(skb);
1263         } else {
1264                 /* Set fragment type for buf_append */
1265                 if (msg_fragm_no(hdr) == 1)
1266                         msg_set_type(hdr, FIRST_FRAGMENT);
1267                 else if (msg_fragm_no(hdr) < msg_nof_fragms(hdr))
1268                         msg_set_type(hdr, FRAGMENT);
1269                 else
1270                         msg_set_type(hdr, LAST_FRAGMENT);
1271
1272                 if (!tipc_buf_append(reasm_tnlmsg, &skb)) {
1273                         /* Successful but non-complete reassembly? */
1274                         if (*reasm_tnlmsg || link_is_bc_rcvlink(l))
1275                                 return 0;
1276                         pr_warn_ratelimited("Unable to reassemble tunnel msg\n");
1277                         return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1278                 }
1279                 iskb = skb;
1280         }
1281
1282         do {
1283                 seqno = buf_seqno(iskb);
1284                 if (unlikely(less(seqno, l->drop_point))) {
1285                         kfree_skb(iskb);
1286                         continue;
1287                 }
1288                 if (unlikely(seqno != l->drop_point)) {
1289                         __tipc_skb_queue_sorted(fdefq, seqno, iskb);
1290                         continue;
1291                 }
1292
1293                 l->drop_point++;
1294                 if (!tipc_data_input(l, iskb, inputq))
1295                         rc |= tipc_link_input(l, iskb, inputq, reasm_skb);
1296                 if (unlikely(rc))
1297                         break;
1298         } while ((iskb = __tipc_skb_dequeue(fdefq, l->drop_point)));
1299
1300         return rc;
1301 }
1302
1303 static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
1304 {
1305         bool released = false;
1306         struct sk_buff *skb, *tmp;
1307
1308         skb_queue_walk_safe(&l->transmq, skb, tmp) {
1309                 if (more(buf_seqno(skb), acked))
1310                         break;
1311                 __skb_unlink(skb, &l->transmq);
1312                 kfree_skb(skb);
1313                 released = true;
1314         }
1315         return released;
1316 }
1317
1318 /* tipc_build_gap_ack_blks - build Gap ACK blocks
1319  * @l: tipc link that data have come with gaps in sequence if any
1320  * @data: data buffer to store the Gap ACK blocks after built
1321  *
1322  * returns the actual allocated memory size
1323  */
1324 static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data)
1325 {
1326         struct sk_buff *skb = skb_peek(&l->deferdq);
1327         struct tipc_gap_ack_blks *ga = data;
1328         u16 len, expect, seqno = 0;
1329         u8 n = 0;
1330
1331         if (!skb)
1332                 goto exit;
1333
1334         expect = buf_seqno(skb);
1335         skb_queue_walk(&l->deferdq, skb) {
1336                 seqno = buf_seqno(skb);
1337                 if (unlikely(more(seqno, expect))) {
1338                         ga->gacks[n].ack = htons(expect - 1);
1339                         ga->gacks[n].gap = htons(seqno - expect);
1340                         if (++n >= MAX_GAP_ACK_BLKS) {
1341                                 pr_info_ratelimited("Too few Gap ACK blocks!\n");
1342                                 goto exit;
1343                         }
1344                 } else if (unlikely(less(seqno, expect))) {
1345                         pr_warn("Unexpected skb in deferdq!\n");
1346                         continue;
1347                 }
1348                 expect = seqno + 1;
1349         }
1350
1351         /* last block */
1352         ga->gacks[n].ack = htons(seqno);
1353         ga->gacks[n].gap = 0;
1354         n++;
1355
1356 exit:
1357         len = tipc_gap_ack_blks_sz(n);
1358         ga->len = htons(len);
1359         ga->gack_cnt = n;
1360         return len;
1361 }
1362
1363 /* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
1364  *                             acked packets, also doing retransmissions if
1365  *                             gaps found
1366  * @l: tipc link with transmq queue to be advanced
1367  * @acked: seqno of last packet acked by peer without any gaps before
1368  * @gap: # of gap packets
1369  * @ga: buffer pointer to Gap ACK blocks from peer
1370  * @xmitq: queue for accumulating the retransmitted packets if any
1371  *
1372  * In case of a repeated retransmit failures, the call will return shortly
1373  * with a returned code (e.g. TIPC_LINK_DOWN_EVT)
1374  */
1375 static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
1376                                      struct tipc_gap_ack_blks *ga,
1377                                      struct sk_buff_head *xmitq)
1378 {
1379         struct sk_buff *skb, *_skb, *tmp;
1380         struct tipc_msg *hdr;
1381         u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1382         u16 ack = l->rcv_nxt - 1;
1383         u16 seqno, n = 0;
1384         int rc = 0;
1385
1386         if (gap && link_retransmit_failure(l, l, acked + 1, &rc))
1387                 return rc;
1388
1389         skb_queue_walk_safe(&l->transmq, skb, tmp) {
1390                 seqno = buf_seqno(skb);
1391
1392 next_gap_ack:
1393                 if (less_eq(seqno, acked)) {
1394                         /* release skb */
1395                         __skb_unlink(skb, &l->transmq);
1396                         kfree_skb(skb);
1397                 } else if (less_eq(seqno, acked + gap)) {
1398                         /* retransmit skb */
1399                         if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1400                                 continue;
1401                         TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
1402
1403                         _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
1404                         if (!_skb)
1405                                 continue;
1406                         hdr = buf_msg(_skb);
1407                         msg_set_ack(hdr, ack);
1408                         msg_set_bcast_ack(hdr, bc_ack);
1409                         _skb->priority = TC_PRIO_CONTROL;
1410                         __skb_queue_tail(xmitq, _skb);
1411                         l->stats.retransmitted++;
1412                 } else {
1413                         /* retry with Gap ACK blocks if any */
1414                         if (!ga || n >= ga->gack_cnt)
1415                                 break;
1416                         acked = ntohs(ga->gacks[n].ack);
1417                         gap = ntohs(ga->gacks[n].gap);
1418                         n++;
1419                         goto next_gap_ack;
1420                 }
1421         }
1422
1423         return 0;
1424 }
1425
1426 /* tipc_link_build_state_msg: prepare link state message for transmission
1427  *
1428  * Note that sending of broadcast ack is coordinated among nodes, to reduce
1429  * risk of ack storms towards the sender
1430  */
1431 int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1432 {
1433         if (!l)
1434                 return 0;
1435
1436         /* Broadcast ACK must be sent via a unicast link => defer to caller */
1437         if (link_is_bc_rcvlink(l)) {
1438                 if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
1439                         return 0;
1440                 l->rcv_unacked = 0;
1441
1442                 /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
1443                 l->snd_nxt = l->rcv_nxt;
1444                 return TIPC_LINK_SND_STATE;
1445         }
1446
1447         /* Unicast ACK */
1448         l->rcv_unacked = 0;
1449         l->stats.sent_acks++;
1450         tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
1451         return 0;
1452 }
1453
1454 /* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
1455  */
1456 void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1457 {
1458         int mtyp = RESET_MSG;
1459         struct sk_buff *skb;
1460
1461         if (l->state == LINK_ESTABLISHING)
1462                 mtyp = ACTIVATE_MSG;
1463
1464         tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
1465
1466         /* Inform peer that this endpoint is going down if applicable */
1467         skb = skb_peek_tail(xmitq);
1468         if (skb && (l->state == LINK_RESET))
1469                 msg_set_peer_stopping(buf_msg(skb), 1);
1470 }
1471
1472 /* tipc_link_build_nack_msg: prepare link nack message for transmission
1473  * Note that sending of broadcast NACK is coordinated among nodes, to
1474  * reduce the risk of NACK storms towards the sender
1475  */
1476 static int tipc_link_build_nack_msg(struct tipc_link *l,
1477                                     struct sk_buff_head *xmitq)
1478 {
1479         u32 def_cnt = ++l->stats.deferred_recv;
1480         u32 defq_len = skb_queue_len(&l->deferdq);
1481         int match1, match2;
1482
1483         if (link_is_bc_rcvlink(l)) {
1484                 match1 = def_cnt & 0xf;
1485                 match2 = tipc_own_addr(l->net) & 0xf;
1486                 if (match1 == match2)
1487                         return TIPC_LINK_SND_STATE;
1488                 return 0;
1489         }
1490
1491         if (defq_len >= 3 && !((defq_len - 3) % 16))
1492                 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
1493         return 0;
1494 }
1495
1496 /* tipc_link_rcv - process TIPC packets/messages arriving from off-node
1497  * @l: the link that should handle the message
1498  * @skb: TIPC packet
1499  * @xmitq: queue to place packets to be sent after this call
1500  */
1501 int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1502                   struct sk_buff_head *xmitq)
1503 {
1504         struct sk_buff_head *defq = &l->deferdq;
1505         struct tipc_msg *hdr = buf_msg(skb);
1506         u16 seqno, rcv_nxt, win_lim;
1507         int rc = 0;
1508
1509         /* Verify and update link state */
1510         if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
1511                 return tipc_link_proto_rcv(l, skb, xmitq);
1512
1513         /* Don't send probe at next timeout expiration */
1514         l->silent_intv_cnt = 0;
1515
1516         do {
1517                 hdr = buf_msg(skb);
1518                 seqno = msg_seqno(hdr);
1519                 rcv_nxt = l->rcv_nxt;
1520                 win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
1521
1522                 if (unlikely(!link_is_up(l))) {
1523                         if (l->state == LINK_ESTABLISHING)
1524                                 rc = TIPC_LINK_UP_EVT;
1525                         goto drop;
1526                 }
1527
1528                 /* Drop if outside receive window */
1529                 if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
1530                         l->stats.duplicates++;
1531                         goto drop;
1532                 }
1533
1534                 /* Forward queues and wake up waiting users */
1535                 if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
1536                         tipc_link_advance_backlog(l, xmitq);
1537                         if (unlikely(!skb_queue_empty(&l->wakeupq)))
1538                                 link_prepare_wakeup(l);
1539                 }
1540
1541                 /* Defer delivery if sequence gap */
1542                 if (unlikely(seqno != rcv_nxt)) {
1543                         __tipc_skb_queue_sorted(defq, seqno, skb);
1544                         rc |= tipc_link_build_nack_msg(l, xmitq);
1545                         break;
1546                 }
1547
1548                 /* Deliver packet */
1549                 l->rcv_nxt++;
1550                 l->stats.recv_pkts++;
1551
1552                 if (unlikely(msg_user(hdr) == TUNNEL_PROTOCOL))
1553                         rc |= tipc_link_tnl_rcv(l, skb, l->inputq);
1554                 else if (!tipc_data_input(l, skb, l->inputq))
1555                         rc |= tipc_link_input(l, skb, l->inputq, &l->reasm_buf);
1556                 if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
1557                         rc |= tipc_link_build_state_msg(l, xmitq);
1558                 if (unlikely(rc & ~TIPC_LINK_SND_STATE))
1559                         break;
1560         } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
1561
1562         return rc;
1563 drop:
1564         kfree_skb(skb);
1565         return rc;
1566 }
1567
1568 static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
1569                                       bool probe_reply, u16 rcvgap,
1570                                       int tolerance, int priority,
1571                                       struct sk_buff_head *xmitq)
1572 {
1573         struct tipc_link *bcl = l->bc_rcvlink;
1574         struct sk_buff *skb;
1575         struct tipc_msg *hdr;
1576         struct sk_buff_head *dfq = &l->deferdq;
1577         bool node_up = link_is_up(bcl);
1578         struct tipc_mon_state *mstate = &l->mon_state;
1579         int dlen = 0;
1580         void *data;
1581         u16 glen = 0;
1582
1583         /* Don't send protocol message during reset or link failover */
1584         if (tipc_link_is_blocked(l))
1585                 return;
1586
1587         if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
1588                 return;
1589
1590         if (!skb_queue_empty(dfq))
1591                 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1592
1593         skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
1594                               tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
1595                               l->addr, tipc_own_addr(l->net), 0, 0, 0);
1596         if (!skb)
1597                 return;
1598
1599         hdr = buf_msg(skb);
1600         data = msg_data(hdr);
1601         msg_set_session(hdr, l->session);
1602         msg_set_bearer_id(hdr, l->bearer_id);
1603         msg_set_net_plane(hdr, l->net_plane);
1604         msg_set_next_sent(hdr, l->snd_nxt);
1605         msg_set_ack(hdr, l->rcv_nxt - 1);
1606         msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
1607         msg_set_bc_ack_invalid(hdr, !node_up);
1608         msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
1609         msg_set_link_tolerance(hdr, tolerance);
1610         msg_set_linkprio(hdr, priority);
1611         msg_set_redundant_link(hdr, node_up);
1612         msg_set_seq_gap(hdr, 0);
1613         msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
1614
1615         if (mtyp == STATE_MSG) {
1616                 if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
1617                         msg_set_seqno(hdr, l->snd_nxt_state++);
1618                 msg_set_seq_gap(hdr, rcvgap);
1619                 msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
1620                 msg_set_probe(hdr, probe);
1621                 msg_set_is_keepalive(hdr, probe || probe_reply);
1622                 if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
1623                         glen = tipc_build_gap_ack_blks(l, data);
1624                 tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
1625                 msg_set_size(hdr, INT_H_SIZE + glen + dlen);
1626                 skb_trim(skb, INT_H_SIZE + glen + dlen);
1627                 l->stats.sent_states++;
1628                 l->rcv_unacked = 0;
1629         } else {
1630                 /* RESET_MSG or ACTIVATE_MSG */
1631                 if (mtyp == ACTIVATE_MSG) {
1632                         msg_set_dest_session_valid(hdr, 1);
1633                         msg_set_dest_session(hdr, l->peer_session);
1634                 }
1635                 msg_set_max_pkt(hdr, l->advertised_mtu);
1636                 strcpy(data, l->if_name);
1637                 msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
1638                 skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
1639         }
1640         if (probe)
1641                 l->stats.sent_probes++;
1642         if (rcvgap)
1643                 l->stats.sent_nacks++;
1644         skb->priority = TC_PRIO_CONTROL;
1645         __skb_queue_tail(xmitq, skb);
1646         trace_tipc_proto_build(skb, false, l->name);
1647 }
1648
1649 void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
1650                                     struct sk_buff_head *xmitq)
1651 {
1652         u32 onode = tipc_own_addr(l->net);
1653         struct tipc_msg *hdr, *ihdr;
1654         struct sk_buff_head tnlq;
1655         struct sk_buff *skb;
1656         u32 dnode = l->addr;
1657
1658         skb_queue_head_init(&tnlq);
1659         skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
1660                               INT_H_SIZE, BASIC_H_SIZE,
1661                               dnode, onode, 0, 0, 0);
1662         if (!skb) {
1663                 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1664                 return;
1665         }
1666
1667         hdr = buf_msg(skb);
1668         msg_set_msgcnt(hdr, 1);
1669         msg_set_bearer_id(hdr, l->peer_bearer_id);
1670
1671         ihdr = (struct tipc_msg *)msg_data(hdr);
1672         tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1673                       BASIC_H_SIZE, dnode);
1674         msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
1675         __skb_queue_tail(&tnlq, skb);
1676         tipc_link_xmit(l, &tnlq, xmitq);
1677 }
1678
1679 /* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
1680  * with contents of the link's transmit and backlog queues.
1681  */
1682 void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1683                            int mtyp, struct sk_buff_head *xmitq)
1684 {
1685         struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1686         struct sk_buff *skb, *tnlskb;
1687         struct tipc_msg *hdr, tnlhdr;
1688         struct sk_buff_head *queue = &l->transmq;
1689         struct sk_buff_head tmpxq, tnlq, frags;
1690         u16 pktlen, pktcnt, seqno = l->snd_nxt;
1691         bool pktcnt_need_update = false;
1692         u16 syncpt;
1693         int rc;
1694
1695         if (!tnl)
1696                 return;
1697
1698         skb_queue_head_init(&tnlq);
1699         skb_queue_head_init(&tmpxq);
1700         skb_queue_head_init(&frags);
1701
1702         /* At least one packet required for safe algorithm => add dummy */
1703         skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1704                               BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
1705                               0, 0, TIPC_ERR_NO_PORT);
1706         if (!skb) {
1707                 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1708                 return;
1709         }
1710         skb_queue_tail(&tnlq, skb);
1711         tipc_link_xmit(l, &tnlq, &tmpxq);
1712         __skb_queue_purge(&tmpxq);
1713
1714         /* Link Synching:
1715          * From now on, send only one single ("dummy") SYNCH message
1716          * to peer. The SYNCH message does not contain any data, just
1717          * a header conveying the synch point to the peer.
1718          */
1719         if (mtyp == SYNCH_MSG && (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1720                 tnlskb = tipc_msg_create(TUNNEL_PROTOCOL, SYNCH_MSG,
1721                                          INT_H_SIZE, 0, l->addr,
1722                                          tipc_own_addr(l->net),
1723                                          0, 0, 0);
1724                 if (!tnlskb) {
1725                         pr_warn("%sunable to create dummy SYNCH_MSG\n",
1726                                 link_co_err);
1727                         return;
1728                 }
1729
1730                 hdr = buf_msg(tnlskb);
1731                 syncpt = l->snd_nxt + skb_queue_len(&l->backlogq) - 1;
1732                 msg_set_syncpt(hdr, syncpt);
1733                 msg_set_bearer_id(hdr, l->peer_bearer_id);
1734                 __skb_queue_tail(&tnlq, tnlskb);
1735                 tipc_link_xmit(tnl, &tnlq, xmitq);
1736                 return;
1737         }
1738
1739         /* Initialize reusable tunnel packet header */
1740         tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
1741                       mtyp, INT_H_SIZE, l->addr);
1742         if (mtyp == SYNCH_MSG)
1743                 pktcnt = l->snd_nxt - buf_seqno(skb_peek(&l->transmq));
1744         else
1745                 pktcnt = skb_queue_len(&l->transmq);
1746         pktcnt += skb_queue_len(&l->backlogq);
1747         msg_set_msgcnt(&tnlhdr, pktcnt);
1748         msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1749 tnl:
1750         /* Wrap each packet into a tunnel packet */
1751         skb_queue_walk(queue, skb) {
1752                 hdr = buf_msg(skb);
1753                 if (queue == &l->backlogq)
1754                         msg_set_seqno(hdr, seqno++);
1755                 pktlen = msg_size(hdr);
1756
1757                 /* Tunnel link MTU is not large enough? This could be
1758                  * due to:
1759                  * 1) Link MTU has just changed or set differently;
1760                  * 2) Or FAILOVER on the top of a SYNCH message
1761                  *
1762                  * The 2nd case should not happen if peer supports
1763                  * TIPC_TUNNEL_ENHANCED
1764                  */
1765                 if (pktlen > tnl->mtu - INT_H_SIZE) {
1766                         if (mtyp == FAILOVER_MSG &&
1767                             (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1768                                 rc = tipc_msg_fragment(skb, &tnlhdr, tnl->mtu,
1769                                                        &frags);
1770                                 if (rc) {
1771                                         pr_warn("%sunable to frag msg: rc %d\n",
1772                                                 link_co_err, rc);
1773                                         return;
1774                                 }
1775                                 pktcnt += skb_queue_len(&frags) - 1;
1776                                 pktcnt_need_update = true;
1777                                 skb_queue_splice_tail_init(&frags, &tnlq);
1778                                 continue;
1779                         }
1780                         /* Unluckily, peer doesn't have TIPC_TUNNEL_ENHANCED
1781                          * => Just warn it and return!
1782                          */
1783                         pr_warn_ratelimited("%stoo large msg <%d, %d>: %d!\n",
1784                                             link_co_err, msg_user(hdr),
1785                                             msg_type(hdr), msg_size(hdr));
1786                         return;
1787                 }
1788
1789                 msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
1790                 tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
1791                 if (!tnlskb) {
1792                         pr_warn("%sunable to send packet\n", link_co_err);
1793                         return;
1794                 }
1795                 skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
1796                 skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
1797                 __skb_queue_tail(&tnlq, tnlskb);
1798         }
1799         if (queue != &l->backlogq) {
1800                 queue = &l->backlogq;
1801                 goto tnl;
1802         }
1803
1804         if (pktcnt_need_update)
1805                 skb_queue_walk(&tnlq, skb) {
1806                         hdr = buf_msg(skb);
1807                         msg_set_msgcnt(hdr, pktcnt);
1808                 }
1809
1810         tipc_link_xmit(tnl, &tnlq, xmitq);
1811
1812         if (mtyp == FAILOVER_MSG) {
1813                 tnl->drop_point = l->rcv_nxt;
1814                 tnl->failover_reasm_skb = l->reasm_buf;
1815                 l->reasm_buf = NULL;
1816
1817                 /* Failover the link's deferdq */
1818                 if (unlikely(!skb_queue_empty(fdefq))) {
1819                         pr_warn("Link failover deferdq not empty: %d!\n",
1820                                 skb_queue_len(fdefq));
1821                         __skb_queue_purge(fdefq);
1822                 }
1823                 skb_queue_splice_init(&l->deferdq, fdefq);
1824         }
1825 }
1826
1827 /**
1828  * tipc_link_failover_prepare() - prepare tnl for link failover
1829  *
1830  * This is a special version of the precursor - tipc_link_tnl_prepare(),
1831  * see the tipc_node_link_failover() for details
1832  *
1833  * @l: failover link
1834  * @tnl: tunnel link
1835  * @xmitq: queue for messages to be xmited
1836  */
1837 void tipc_link_failover_prepare(struct tipc_link *l, struct tipc_link *tnl,
1838                                 struct sk_buff_head *xmitq)
1839 {
1840         struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1841
1842         tipc_link_create_dummy_tnl_msg(tnl, xmitq);
1843
1844         /* This failover link enpoint was never established before,
1845          * so it has not received anything from peer.
1846          * Otherwise, it must be a normal failover situation or the
1847          * node has entered SELF_DOWN_PEER_LEAVING and both peer nodes
1848          * would have to start over from scratch instead.
1849          */
1850         tnl->drop_point = 1;
1851         tnl->failover_reasm_skb = NULL;
1852
1853         /* Initiate the link's failover deferdq */
1854         if (unlikely(!skb_queue_empty(fdefq))) {
1855                 pr_warn("Link failover deferdq not empty: %d!\n",
1856                         skb_queue_len(fdefq));
1857                 __skb_queue_purge(fdefq);
1858         }
1859 }
1860
1861 /* tipc_link_validate_msg(): validate message against current link state
1862  * Returns true if message should be accepted, otherwise false
1863  */
1864 bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
1865 {
1866         u16 curr_session = l->peer_session;
1867         u16 session = msg_session(hdr);
1868         int mtyp = msg_type(hdr);
1869
1870         if (msg_user(hdr) != LINK_PROTOCOL)
1871                 return true;
1872
1873         switch (mtyp) {
1874         case RESET_MSG:
1875                 if (!l->in_session)
1876                         return true;
1877                 /* Accept only RESET with new session number */
1878                 return more(session, curr_session);
1879         case ACTIVATE_MSG:
1880                 if (!l->in_session)
1881                         return true;
1882                 /* Accept only ACTIVATE with new or current session number */
1883                 return !less(session, curr_session);
1884         case STATE_MSG:
1885                 /* Accept only STATE with current session number */
1886                 if (!l->in_session)
1887                         return false;
1888                 if (session != curr_session)
1889                         return false;
1890                 /* Extra sanity check */
1891                 if (!link_is_up(l) && msg_ack(hdr))
1892                         return false;
1893                 if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
1894                         return true;
1895                 /* Accept only STATE with new sequence number */
1896                 return !less(msg_seqno(hdr), l->rcv_nxt_state);
1897         default:
1898                 return false;
1899         }
1900 }
1901
1902 /* tipc_link_proto_rcv(): receive link level protocol message :
1903  * Note that network plane id propagates through the network, and may
1904  * change at any time. The node with lowest numerical id determines
1905  * network plane
1906  */
1907 static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
1908                                struct sk_buff_head *xmitq)
1909 {
1910         struct tipc_msg *hdr = buf_msg(skb);
1911         struct tipc_gap_ack_blks *ga = NULL;
1912         u16 rcvgap = 0;
1913         u16 ack = msg_ack(hdr);
1914         u16 gap = msg_seq_gap(hdr);
1915         u16 peers_snd_nxt =  msg_next_sent(hdr);
1916         u16 peers_tol = msg_link_tolerance(hdr);
1917         u16 peers_prio = msg_linkprio(hdr);
1918         u16 rcv_nxt = l->rcv_nxt;
1919         u16 dlen = msg_data_sz(hdr);
1920         int mtyp = msg_type(hdr);
1921         bool reply = msg_probe(hdr);
1922         u16 glen = 0;
1923         void *data;
1924         char *if_name;
1925         int rc = 0;
1926
1927         trace_tipc_proto_rcv(skb, false, l->name);
1928         if (tipc_link_is_blocked(l) || !xmitq)
1929                 goto exit;
1930
1931         if (tipc_own_addr(l->net) > msg_prevnode(hdr))
1932                 l->net_plane = msg_net_plane(hdr);
1933
1934         skb_linearize(skb);
1935         hdr = buf_msg(skb);
1936         data = msg_data(hdr);
1937
1938         if (!tipc_link_validate_msg(l, hdr)) {
1939                 trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
1940                 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
1941                 goto exit;
1942         }
1943
1944         switch (mtyp) {
1945         case RESET_MSG:
1946         case ACTIVATE_MSG:
1947                 /* Complete own link name with peer's interface name */
1948                 if_name =  strrchr(l->name, ':') + 1;
1949                 if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
1950                         break;
1951                 if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
1952                         break;
1953                 strncpy(if_name, data, TIPC_MAX_IF_NAME);
1954
1955                 /* Update own tolerance if peer indicates a non-zero value */
1956                 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
1957                         l->tolerance = peers_tol;
1958                         l->bc_rcvlink->tolerance = peers_tol;
1959                 }
1960                 /* Update own priority if peer's priority is higher */
1961                 if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
1962                         l->priority = peers_prio;
1963
1964                 /* If peer is going down we want full re-establish cycle */
1965                 if (msg_peer_stopping(hdr)) {
1966                         rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1967                         break;
1968                 }
1969
1970                 /* If this endpoint was re-created while peer was ESTABLISHING
1971                  * it doesn't know current session number. Force re-synch.
1972                  */
1973                 if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
1974                     l->session != msg_dest_session(hdr)) {
1975                         if (less(l->session, msg_dest_session(hdr)))
1976                                 l->session = msg_dest_session(hdr) + 1;
1977                         break;
1978                 }
1979
1980                 /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
1981                 if (mtyp == RESET_MSG || !link_is_up(l))
1982                         rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
1983
1984                 /* ACTIVATE_MSG takes up link if it was already locally reset */
1985                 if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
1986                         rc = TIPC_LINK_UP_EVT;
1987
1988                 l->peer_session = msg_session(hdr);
1989                 l->in_session = true;
1990                 l->peer_bearer_id = msg_bearer_id(hdr);
1991                 if (l->mtu > msg_max_pkt(hdr))
1992                         l->mtu = msg_max_pkt(hdr);
1993                 break;
1994
1995         case STATE_MSG:
1996                 l->rcv_nxt_state = msg_seqno(hdr) + 1;
1997
1998                 /* Update own tolerance if peer indicates a non-zero value */
1999                 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
2000                         l->tolerance = peers_tol;
2001                         l->bc_rcvlink->tolerance = peers_tol;
2002                 }
2003                 /* Update own prio if peer indicates a different value */
2004                 if ((peers_prio != l->priority) &&
2005                     in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
2006                         l->priority = peers_prio;
2007                         rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2008                 }
2009
2010                 l->silent_intv_cnt = 0;
2011                 l->stats.recv_states++;
2012                 if (msg_probe(hdr))
2013                         l->stats.recv_probes++;
2014
2015                 if (!link_is_up(l)) {
2016                         if (l->state == LINK_ESTABLISHING)
2017                                 rc = TIPC_LINK_UP_EVT;
2018                         break;
2019                 }
2020
2021                 /* Receive Gap ACK blocks from peer if any */
2022                 if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
2023                         ga = (struct tipc_gap_ack_blks *)data;
2024                         glen = ntohs(ga->len);
2025                         /* sanity check: if failed, ignore Gap ACK blocks */
2026                         if (glen != tipc_gap_ack_blks_sz(ga->gack_cnt))
2027                                 ga = NULL;
2028                 }
2029
2030                 tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
2031                              &l->mon_state, l->bearer_id);
2032
2033                 /* Send NACK if peer has sent pkts we haven't received yet */
2034                 if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
2035                         rcvgap = peers_snd_nxt - l->rcv_nxt;
2036                 if (rcvgap || reply)
2037                         tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
2038                                                   rcvgap, 0, 0, xmitq);
2039
2040                 rc |= tipc_link_advance_transmq(l, ack, gap, ga, xmitq);
2041
2042                 /* If NACK, retransmit will now start at right position */
2043                 if (gap)
2044                         l->stats.recv_nacks++;
2045
2046                 tipc_link_advance_backlog(l, xmitq);
2047                 if (unlikely(!skb_queue_empty(&l->wakeupq)))
2048                         link_prepare_wakeup(l);
2049         }
2050 exit:
2051         kfree_skb(skb);
2052         return rc;
2053 }
2054
2055 /* tipc_link_build_bc_proto_msg() - create broadcast protocol message
2056  */
2057 static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
2058                                          u16 peers_snd_nxt,
2059                                          struct sk_buff_head *xmitq)
2060 {
2061         struct sk_buff *skb;
2062         struct tipc_msg *hdr;
2063         struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
2064         u16 ack = l->rcv_nxt - 1;
2065         u16 gap_to = peers_snd_nxt - 1;
2066
2067         skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
2068                               0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
2069         if (!skb)
2070                 return false;
2071         hdr = buf_msg(skb);
2072         msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
2073         msg_set_bcast_ack(hdr, ack);
2074         msg_set_bcgap_after(hdr, ack);
2075         if (dfrd_skb)
2076                 gap_to = buf_seqno(dfrd_skb) - 1;
2077         msg_set_bcgap_to(hdr, gap_to);
2078         msg_set_non_seq(hdr, bcast);
2079         __skb_queue_tail(xmitq, skb);
2080         return true;
2081 }
2082
2083 /* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
2084  *
2085  * Give a newly added peer node the sequence number where it should
2086  * start receiving and acking broadcast packets.
2087  */
2088 static void tipc_link_build_bc_init_msg(struct tipc_link *l,
2089                                         struct sk_buff_head *xmitq)
2090 {
2091         struct sk_buff_head list;
2092
2093         __skb_queue_head_init(&list);
2094         if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
2095                 return;
2096         msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
2097         tipc_link_xmit(l, &list, xmitq);
2098 }
2099
2100 /* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
2101  */
2102 void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
2103 {
2104         int mtyp = msg_type(hdr);
2105         u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2106
2107         if (link_is_up(l))
2108                 return;
2109
2110         if (msg_user(hdr) == BCAST_PROTOCOL) {
2111                 l->rcv_nxt = peers_snd_nxt;
2112                 l->state = LINK_ESTABLISHED;
2113                 return;
2114         }
2115
2116         if (l->peer_caps & TIPC_BCAST_SYNCH)
2117                 return;
2118
2119         if (msg_peer_node_is_up(hdr))
2120                 return;
2121
2122         /* Compatibility: accept older, less safe initial synch data */
2123         if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
2124                 l->rcv_nxt = peers_snd_nxt;
2125 }
2126
2127 /* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
2128  */
2129 int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
2130                           struct sk_buff_head *xmitq)
2131 {
2132         struct tipc_link *snd_l = l->bc_sndlink;
2133         u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2134         u16 from = msg_bcast_ack(hdr) + 1;
2135         u16 to = from + msg_bc_gap(hdr) - 1;
2136         int rc = 0;
2137
2138         if (!link_is_up(l))
2139                 return rc;
2140
2141         if (!msg_peer_node_is_up(hdr))
2142                 return rc;
2143
2144         /* Open when peer ackowledges our bcast init msg (pkt #1) */
2145         if (msg_ack(hdr))
2146                 l->bc_peer_is_up = true;
2147
2148         if (!l->bc_peer_is_up)
2149                 return rc;
2150
2151         l->stats.recv_nacks++;
2152
2153         /* Ignore if peers_snd_nxt goes beyond receive window */
2154         if (more(peers_snd_nxt, l->rcv_nxt + l->window))
2155                 return rc;
2156
2157         rc = tipc_link_bc_retrans(snd_l, l, from, to, xmitq);
2158
2159         l->snd_nxt = peers_snd_nxt;
2160         if (link_bc_rcv_gap(l))
2161                 rc |= TIPC_LINK_SND_STATE;
2162
2163         /* Return now if sender supports nack via STATE messages */
2164         if (l->peer_caps & TIPC_BCAST_STATE_NACK)
2165                 return rc;
2166
2167         /* Otherwise, be backwards compatible */
2168
2169         if (!more(peers_snd_nxt, l->rcv_nxt)) {
2170                 l->nack_state = BC_NACK_SND_CONDITIONAL;
2171                 return 0;
2172         }
2173
2174         /* Don't NACK if one was recently sent or peeked */
2175         if (l->nack_state == BC_NACK_SND_SUPPRESS) {
2176                 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2177                 return 0;
2178         }
2179
2180         /* Conditionally delay NACK sending until next synch rcv */
2181         if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
2182                 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2183                 if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
2184                         return 0;
2185         }
2186
2187         /* Send NACK now but suppress next one */
2188         tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
2189         l->nack_state = BC_NACK_SND_SUPPRESS;
2190         return 0;
2191 }
2192
2193 void tipc_link_bc_ack_rcv(struct tipc_link *l, u16 acked,
2194                           struct sk_buff_head *xmitq)
2195 {
2196         struct sk_buff *skb, *tmp;
2197         struct tipc_link *snd_l = l->bc_sndlink;
2198
2199         if (!link_is_up(l) || !l->bc_peer_is_up)
2200                 return;
2201
2202         if (!more(acked, l->acked))
2203                 return;
2204
2205         trace_tipc_link_bc_ack(l, l->acked, acked, &snd_l->transmq);
2206         /* Skip over packets peer has already acked */
2207         skb_queue_walk(&snd_l->transmq, skb) {
2208                 if (more(buf_seqno(skb), l->acked))
2209                         break;
2210         }
2211
2212         /* Update/release the packets peer is acking now */
2213         skb_queue_walk_from_safe(&snd_l->transmq, skb, tmp) {
2214                 if (more(buf_seqno(skb), acked))
2215                         break;
2216                 if (!--TIPC_SKB_CB(skb)->ackers) {
2217                         __skb_unlink(skb, &snd_l->transmq);
2218                         kfree_skb(skb);
2219                 }
2220         }
2221         l->acked = acked;
2222         tipc_link_advance_backlog(snd_l, xmitq);
2223         if (unlikely(!skb_queue_empty(&snd_l->wakeupq)))
2224                 link_prepare_wakeup(snd_l);
2225 }
2226
2227 /* tipc_link_bc_nack_rcv(): receive broadcast nack message
2228  * This function is here for backwards compatibility, since
2229  * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
2230  */
2231 int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
2232                           struct sk_buff_head *xmitq)
2233 {
2234         struct tipc_msg *hdr = buf_msg(skb);
2235         u32 dnode = msg_destnode(hdr);
2236         int mtyp = msg_type(hdr);
2237         u16 acked = msg_bcast_ack(hdr);
2238         u16 from = acked + 1;
2239         u16 to = msg_bcgap_to(hdr);
2240         u16 peers_snd_nxt = to + 1;
2241         int rc = 0;
2242
2243         kfree_skb(skb);
2244
2245         if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
2246                 return 0;
2247
2248         if (mtyp != STATE_MSG)
2249                 return 0;
2250
2251         if (dnode == tipc_own_addr(l->net)) {
2252                 tipc_link_bc_ack_rcv(l, acked, xmitq);
2253                 rc = tipc_link_bc_retrans(l->bc_sndlink, l, from, to, xmitq);
2254                 l->stats.recv_nacks++;
2255                 return rc;
2256         }
2257
2258         /* Msg for other node => suppress own NACK at next sync if applicable */
2259         if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
2260                 l->nack_state = BC_NACK_SND_SUPPRESS;
2261
2262         return 0;
2263 }
2264
2265 void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
2266 {
2267         int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
2268
2269         l->window = win;
2270         l->backlog[TIPC_LOW_IMPORTANCE].limit      = max_t(u16, 50, win);
2271         l->backlog[TIPC_MEDIUM_IMPORTANCE].limit   = max_t(u16, 100, win * 2);
2272         l->backlog[TIPC_HIGH_IMPORTANCE].limit     = max_t(u16, 150, win * 3);
2273         l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = max_t(u16, 200, win * 4);
2274         l->backlog[TIPC_SYSTEM_IMPORTANCE].limit   = max_bulk;
2275 }
2276
2277 /**
2278  * link_reset_stats - reset link statistics
2279  * @l: pointer to link
2280  */
2281 void tipc_link_reset_stats(struct tipc_link *l)
2282 {
2283         memset(&l->stats, 0, sizeof(l->stats));
2284 }
2285
2286 static void link_print(struct tipc_link *l, const char *str)
2287 {
2288         struct sk_buff *hskb = skb_peek(&l->transmq);
2289         u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
2290         u16 tail = l->snd_nxt - 1;
2291
2292         pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
2293         pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
2294                 skb_queue_len(&l->transmq), head, tail,
2295                 skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
2296 }
2297
2298 /* Parse and validate nested (link) properties valid for media, bearer and link
2299  */
2300 int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2301 {
2302         int err;
2303
2304         err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
2305                                           tipc_nl_prop_policy, NULL);
2306         if (err)
2307                 return err;
2308
2309         if (props[TIPC_NLA_PROP_PRIO]) {
2310                 u32 prio;
2311
2312                 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2313                 if (prio > TIPC_MAX_LINK_PRI)
2314                         return -EINVAL;
2315         }
2316
2317         if (props[TIPC_NLA_PROP_TOL]) {
2318                 u32 tol;
2319
2320                 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2321                 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2322                         return -EINVAL;
2323         }
2324
2325         if (props[TIPC_NLA_PROP_WIN]) {
2326                 u32 win;
2327
2328                 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2329                 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2330                         return -EINVAL;
2331         }
2332
2333         return 0;
2334 }
2335
2336 static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
2337 {
2338         int i;
2339         struct nlattr *stats;
2340
2341         struct nla_map {
2342                 u32 key;
2343                 u32 val;
2344         };
2345
2346         struct nla_map map[] = {
2347                 {TIPC_NLA_STATS_RX_INFO, 0},
2348                 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2349                 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2350                 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2351                 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2352                 {TIPC_NLA_STATS_TX_INFO, 0},
2353                 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2354                 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2355                 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2356                 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2357                 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2358                         s->msg_length_counts : 1},
2359                 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2360                 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2361                 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2362                 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2363                 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2364                 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2365                 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2366                 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2367                 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2368                 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2369                 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2370                 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2371                 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2372                 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2373                 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2374                 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2375                 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2376                 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2377                 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2378                 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2379                 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2380                 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2381                         (s->accu_queue_sz / s->queue_sz_counts) : 0}
2382         };
2383
2384         stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2385         if (!stats)
2386                 return -EMSGSIZE;
2387
2388         for (i = 0; i <  ARRAY_SIZE(map); i++)
2389                 if (nla_put_u32(skb, map[i].key, map[i].val))
2390                         goto msg_full;
2391
2392         nla_nest_end(skb, stats);
2393
2394         return 0;
2395 msg_full:
2396         nla_nest_cancel(skb, stats);
2397
2398         return -EMSGSIZE;
2399 }
2400
2401 /* Caller should hold appropriate locks to protect the link */
2402 int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2403                        struct tipc_link *link, int nlflags)
2404 {
2405         u32 self = tipc_own_addr(net);
2406         struct nlattr *attrs;
2407         struct nlattr *prop;
2408         void *hdr;
2409         int err;
2410
2411         hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2412                           nlflags, TIPC_NL_LINK_GET);
2413         if (!hdr)
2414                 return -EMSGSIZE;
2415
2416         attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2417         if (!attrs)
2418                 goto msg_full;
2419
2420         if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2421                 goto attr_msg_full;
2422         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
2423                 goto attr_msg_full;
2424         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
2425                 goto attr_msg_full;
2426         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
2427                 goto attr_msg_full;
2428         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
2429                 goto attr_msg_full;
2430
2431         if (tipc_link_is_up(link))
2432                 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2433                         goto attr_msg_full;
2434         if (link->active)
2435                 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2436                         goto attr_msg_full;
2437
2438         prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2439         if (!prop)
2440                 goto attr_msg_full;
2441         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2442                 goto prop_msg_full;
2443         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2444                 goto prop_msg_full;
2445         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2446                         link->window))
2447                 goto prop_msg_full;
2448         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2449                 goto prop_msg_full;
2450         nla_nest_end(msg->skb, prop);
2451
2452         err = __tipc_nl_add_stats(msg->skb, &link->stats);
2453         if (err)
2454                 goto attr_msg_full;
2455
2456         nla_nest_end(msg->skb, attrs);
2457         genlmsg_end(msg->skb, hdr);
2458
2459         return 0;
2460
2461 prop_msg_full:
2462         nla_nest_cancel(msg->skb, prop);
2463 attr_msg_full:
2464         nla_nest_cancel(msg->skb, attrs);
2465 msg_full:
2466         genlmsg_cancel(msg->skb, hdr);
2467
2468         return -EMSGSIZE;
2469 }
2470
2471 static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
2472                                       struct tipc_stats *stats)
2473 {
2474         int i;
2475         struct nlattr *nest;
2476
2477         struct nla_map {
2478                 __u32 key;
2479                 __u32 val;
2480         };
2481
2482         struct nla_map map[] = {
2483                 {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
2484                 {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
2485                 {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
2486                 {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
2487                 {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
2488                 {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
2489                 {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
2490                 {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
2491                 {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
2492                 {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
2493                 {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
2494                 {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
2495                 {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
2496                 {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
2497                 {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
2498                 {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
2499                 {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
2500                 {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
2501                 {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
2502                         (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
2503         };
2504
2505         nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
2506         if (!nest)
2507                 return -EMSGSIZE;
2508
2509         for (i = 0; i <  ARRAY_SIZE(map); i++)
2510                 if (nla_put_u32(skb, map[i].key, map[i].val))
2511                         goto msg_full;
2512
2513         nla_nest_end(skb, nest);
2514
2515         return 0;
2516 msg_full:
2517         nla_nest_cancel(skb, nest);
2518
2519         return -EMSGSIZE;
2520 }
2521
2522 int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
2523 {
2524         int err;
2525         void *hdr;
2526         struct nlattr *attrs;
2527         struct nlattr *prop;
2528         struct tipc_net *tn = net_generic(net, tipc_net_id);
2529         u32 bc_mode = tipc_bcast_get_broadcast_mode(net);
2530         u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
2531         struct tipc_link *bcl = tn->bcl;
2532
2533         if (!bcl)
2534                 return 0;
2535
2536         tipc_bcast_lock(net);
2537
2538         hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2539                           NLM_F_MULTI, TIPC_NL_LINK_GET);
2540         if (!hdr) {
2541                 tipc_bcast_unlock(net);
2542                 return -EMSGSIZE;
2543         }
2544
2545         attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
2546         if (!attrs)
2547                 goto msg_full;
2548
2549         /* The broadcast link is always up */
2550         if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2551                 goto attr_msg_full;
2552
2553         if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
2554                 goto attr_msg_full;
2555         if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
2556                 goto attr_msg_full;
2557         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
2558                 goto attr_msg_full;
2559         if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
2560                 goto attr_msg_full;
2561
2562         prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
2563         if (!prop)
2564                 goto attr_msg_full;
2565         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
2566                 goto prop_msg_full;
2567         if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
2568                 goto prop_msg_full;
2569         if (bc_mode & BCLINK_MODE_SEL)
2570                 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
2571                                 bc_ratio))
2572                         goto prop_msg_full;
2573         nla_nest_end(msg->skb, prop);
2574
2575         err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
2576         if (err)
2577                 goto attr_msg_full;
2578
2579         tipc_bcast_unlock(net);
2580         nla_nest_end(msg->skb, attrs);
2581         genlmsg_end(msg->skb, hdr);
2582
2583         return 0;
2584
2585 prop_msg_full:
2586         nla_nest_cancel(msg->skb, prop);
2587 attr_msg_full:
2588         nla_nest_cancel(msg->skb, attrs);
2589 msg_full:
2590         tipc_bcast_unlock(net);
2591         genlmsg_cancel(msg->skb, hdr);
2592
2593         return -EMSGSIZE;
2594 }
2595
2596 void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
2597                              struct sk_buff_head *xmitq)
2598 {
2599         l->tolerance = tol;
2600         if (l->bc_rcvlink)
2601                 l->bc_rcvlink->tolerance = tol;
2602         if (link_is_up(l))
2603                 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
2604 }
2605
2606 void tipc_link_set_prio(struct tipc_link *l, u32 prio,
2607                         struct sk_buff_head *xmitq)
2608 {
2609         l->priority = prio;
2610         tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
2611 }
2612
2613 void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
2614 {
2615         l->abort_limit = limit;
2616 }
2617
2618 char *tipc_link_name_ext(struct tipc_link *l, char *buf)
2619 {
2620         if (!l)
2621                 scnprintf(buf, TIPC_MAX_LINK_NAME, "null");
2622         else if (link_is_bc_sndlink(l))
2623                 scnprintf(buf, TIPC_MAX_LINK_NAME, "broadcast-sender");
2624         else if (link_is_bc_rcvlink(l))
2625                 scnprintf(buf, TIPC_MAX_LINK_NAME,
2626                           "broadcast-receiver, peer %x", l->addr);
2627         else
2628                 memcpy(buf, l->name, TIPC_MAX_LINK_NAME);
2629
2630         return buf;
2631 }
2632
2633 /**
2634  * tipc_link_dump - dump TIPC link data
2635  * @l: tipc link to be dumped
2636  * @dqueues: bitmask to decide if any link queue to be dumped?
2637  *           - TIPC_DUMP_NONE: don't dump link queues
2638  *           - TIPC_DUMP_TRANSMQ: dump link transmq queue
2639  *           - TIPC_DUMP_BACKLOGQ: dump link backlog queue
2640  *           - TIPC_DUMP_DEFERDQ: dump link deferd queue
2641  *           - TIPC_DUMP_INPUTQ: dump link input queue
2642  *           - TIPC_DUMP_WAKEUP: dump link wakeup queue
2643  *           - TIPC_DUMP_ALL: dump all the link queues above
2644  * @buf: returned buffer of dump data in format
2645  */
2646 int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
2647 {
2648         int i = 0;
2649         size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
2650         struct sk_buff_head *list;
2651         struct sk_buff *hskb, *tskb;
2652         u32 len;
2653
2654         if (!l) {
2655                 i += scnprintf(buf, sz, "link data: (null)\n");
2656                 return i;
2657         }
2658
2659         i += scnprintf(buf, sz, "link data: %x", l->addr);
2660         i += scnprintf(buf + i, sz - i, " %x", l->state);
2661         i += scnprintf(buf + i, sz - i, " %u", l->in_session);
2662         i += scnprintf(buf + i, sz - i, " %u", l->session);
2663         i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
2664         i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
2665         i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
2666         i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
2667         i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
2668         i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
2669         i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
2670         i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
2671         i += scnprintf(buf + i, sz - i, " %u", l->prev_from);
2672         i += scnprintf(buf + i, sz - i, " %u", 0);
2673         i += scnprintf(buf + i, sz - i, " %u", l->acked);
2674
2675         list = &l->transmq;
2676         len = skb_queue_len(list);
2677         hskb = skb_peek(list);
2678         tskb = skb_peek_tail(list);
2679         i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2680                        (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2681                        (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2682
2683         list = &l->deferdq;
2684         len = skb_queue_len(list);
2685         hskb = skb_peek(list);
2686         tskb = skb_peek_tail(list);
2687         i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2688                        (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2689                        (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2690
2691         list = &l->backlogq;
2692         len = skb_queue_len(list);
2693         hskb = skb_peek(list);
2694         tskb = skb_peek_tail(list);
2695         i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2696                        (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2697                        (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2698
2699         list = l->inputq;
2700         len = skb_queue_len(list);
2701         hskb = skb_peek(list);
2702         tskb = skb_peek_tail(list);
2703         i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
2704                        (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2705                        (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2706
2707         if (dqueues & TIPC_DUMP_TRANSMQ) {
2708                 i += scnprintf(buf + i, sz - i, "transmq: ");
2709                 i += tipc_list_dump(&l->transmq, false, buf + i);
2710         }
2711         if (dqueues & TIPC_DUMP_BACKLOGQ) {
2712                 i += scnprintf(buf + i, sz - i,
2713                                "backlogq: <%u %u %u %u %u>, ",
2714                                l->backlog[TIPC_LOW_IMPORTANCE].len,
2715                                l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
2716                                l->backlog[TIPC_HIGH_IMPORTANCE].len,
2717                                l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
2718                                l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
2719                 i += tipc_list_dump(&l->backlogq, false, buf + i);
2720         }
2721         if (dqueues & TIPC_DUMP_DEFERDQ) {
2722                 i += scnprintf(buf + i, sz - i, "deferdq: ");
2723                 i += tipc_list_dump(&l->deferdq, false, buf + i);
2724         }
2725         if (dqueues & TIPC_DUMP_INPUTQ) {
2726                 i += scnprintf(buf + i, sz - i, "inputq: ");
2727                 i += tipc_list_dump(l->inputq, false, buf + i);
2728         }
2729         if (dqueues & TIPC_DUMP_WAKEUP) {
2730                 i += scnprintf(buf + i, sz - i, "wakeup: ");
2731                 i += tipc_list_dump(&l->wakeupq, false, buf + i);
2732         }
2733
2734         return i;
2735 }