blob: d4675e922a8f717682f465fd2c5664ba6bc1e878 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/link.c: TIPC link code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloye74a3862016-03-03 14:23:21 -05004 * Copyright (c) 1996-2007, 2012-2016, Ericsson AB
Ying Xue198d73b2013-06-17 10:54:42 -04005 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -040038#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "link.h"
Richard Alpe7be57fc2014-11-20 10:29:12 +010040#include "bcast.h"
Jon Paul Maloy9816f062014-05-14 05:39:15 -040041#include "socket.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#include "discover.h"
Richard Alpe0655f6a2014-11-20 10:29:07 +010044#include "netlink.h"
Jon Paul Maloy35c55c92016-06-13 20:46:22 -040045#include "monitor.h"
Tuong Lienb4b97712018-12-19 09:17:56 +070046#include "trace.h"
Tuong Lienfc1b6d62019-11-08 12:05:11 +070047#include "crypto.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Ying Xue796c75d2013-06-17 10:54:48 -040049#include <linux/pkt_sched.h>
50
Jon Paul Maloy38206d52015-11-19 14:30:46 -050051struct tipc_stats {
Jon Paul Maloy95901122016-11-25 10:35:02 -050052 u32 sent_pkts;
53 u32 recv_pkts;
Jon Paul Maloy38206d52015-11-19 14:30:46 -050054 u32 sent_states;
55 u32 recv_states;
56 u32 sent_probes;
57 u32 recv_probes;
58 u32 sent_nacks;
59 u32 recv_nacks;
60 u32 sent_acks;
61 u32 sent_bundled;
62 u32 sent_bundles;
63 u32 recv_bundled;
64 u32 recv_bundles;
65 u32 retransmitted;
66 u32 sent_fragmented;
67 u32 sent_fragments;
68 u32 recv_fragmented;
69 u32 recv_fragments;
70 u32 link_congs; /* # port sends blocked by congestion */
71 u32 deferred_recv;
72 u32 duplicates;
73 u32 max_queue_sz; /* send queue size high water mark */
74 u32 accu_queue_sz; /* used for send queue size profiling */
75 u32 queue_sz_counts; /* used for send queue size profiling */
76 u32 msg_length_counts; /* used for message length profiling */
77 u32 msg_lengths_total; /* used for message length profiling */
78 u32 msg_length_profile[7]; /* used for msg. length profiling */
79};
80
81/**
82 * struct tipc_link - TIPC link data structure
83 * @addr: network address of link's peer node
84 * @name: link name character string
85 * @media_addr: media address to use when sending messages over link
86 * @timer: link timer
87 * @net: pointer to namespace struct
88 * @refcnt: reference counter for permanent references (owner node & timer)
89 * @peer_session: link session # being used by peer end of link
90 * @peer_bearer_id: bearer id used by link's peer endpoint
91 * @bearer_id: local bearer id used by link
92 * @tolerance: minimum link continuity loss needed to reset link [in ms]
Jon Paul Maloy38206d52015-11-19 14:30:46 -050093 * @abort_limit: # of unacknowledged continuity probes needed to reset link
94 * @state: current state of link FSM
95 * @peer_caps: bitmap describing capabilities of peer node
96 * @silent_intv_cnt: # of timer intervals without any reception from peer
97 * @proto_msg: template for control messages generated by link
98 * @pmsg: convenience pointer to "proto_msg" field
99 * @priority: current link priority
100 * @net_plane: current link network plane ('A' through 'H')
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400101 * @mon_state: cookie with information needed by link monitor
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500102 * @backlog_limit: backlog queue congestion thresholds (indexed by importance)
103 * @exp_msg_count: # of tunnelled messages expected during link changeover
104 * @reset_rcv_checkpt: seq # of last acknowledged message at time of link reset
105 * @mtu: current maximum packet size for this link
106 * @advertised_mtu: advertised own mtu when link is being established
107 * @transmitq: queue for sent, non-acked messages
108 * @backlogq: queue for messages waiting to be sent
109 * @snt_nxt: next sequence number to use for outbound messages
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500110 * @ackers: # of peers that needs to ack each packet before it can be released
111 * @acked: # last packet acked by a certain peer. Used for broadcast.
112 * @rcv_nxt: next sequence number to expect for inbound messages
113 * @deferred_queue: deferred queue saved OOS b'cast message received from node
114 * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
115 * @inputq: buffer queue for messages to be delivered upwards
116 * @namedq: buffer queue for name table messages to be delivered upwards
117 * @next_out: ptr to first unsent outbound message in queue
118 * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
119 * @long_msg_seq_no: next identifier to use for outbound fragmented messages
120 * @reasm_buf: head of partially reassembled inbound message fragments
121 * @bc_rcvr: marks that this is a broadcast receiver link
122 * @stats: collects statistics regarding link activity
123 */
124struct tipc_link {
125 u32 addr;
126 char name[TIPC_MAX_LINK_NAME];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500127 struct net *net;
128
129 /* Management and link supervision data */
Jon Maloy7ea817f2018-07-10 01:07:36 +0200130 u16 peer_session;
131 u16 session;
Jon Maloy9012de52018-07-10 01:07:35 +0200132 u16 snd_nxt_state;
133 u16 rcv_nxt_state;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500134 u32 peer_bearer_id;
135 u32 bearer_id;
136 u32 tolerance;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500137 u32 abort_limit;
138 u32 state;
139 u16 peer_caps;
Jon Maloy7ea817f2018-07-10 01:07:36 +0200140 bool in_session;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500141 bool active;
142 u32 silent_intv_cnt;
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500143 char if_name[TIPC_MAX_IF_NAME];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500144 u32 priority;
145 char net_plane;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400146 struct tipc_mon_state mon_state;
Jon Paul Maloy88e8ac72016-04-15 13:33:04 -0400147 u16 rst_cnt;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500148
149 /* Failover/synch */
150 u16 drop_point;
151 struct sk_buff *failover_reasm_skb;
Tuong Lien58ee86b2019-04-04 11:09:53 +0700152 struct sk_buff_head failover_deferdq;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500153
154 /* Max packet negotiation */
155 u16 mtu;
156 u16 advertised_mtu;
157
158 /* Sending */
159 struct sk_buff_head transmq;
160 struct sk_buff_head backlogq;
161 struct {
162 u16 len;
163 u16 limit;
Tuong Liene95584a2019-10-02 18:49:43 +0700164 struct sk_buff *target_bskb;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500165 } backlog[5];
166 u16 snd_nxt;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500167
168 /* Reception */
169 u16 rcv_nxt;
170 u32 rcv_unacked;
171 struct sk_buff_head deferdq;
172 struct sk_buff_head *inputq;
173 struct sk_buff_head *namedq;
174
175 /* Congestion handling */
176 struct sk_buff_head wakeupq;
Jon Maloy16ad3f42019-12-10 00:52:46 +0100177 u16 window;
178 u16 min_win;
179 u16 ssthresh;
180 u16 max_win;
181 u16 cong_acks;
182 u16 checkpoint;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500183
184 /* Fragmentation/reassembly */
185 struct sk_buff *reasm_buf;
Tuong Lien2320bcd2019-07-24 08:56:12 +0700186 struct sk_buff *reasm_tnlmsg;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500187
188 /* Broadcast */
189 u16 ackers;
190 u16 acked;
191 struct tipc_link *bc_rcvlink;
192 struct tipc_link *bc_sndlink;
Jon Paul Maloy7c4a54b2016-09-01 13:52:50 -0400193 u8 nack_state;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500194 bool bc_peer_is_up;
195
196 /* Statistics */
197 struct tipc_stats stats;
198};
199
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400200/*
201 * Error message prefixes
202 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400203static const char *link_co_err = "Link tunneling error, ";
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400204static const char *link_rst_msg = "Resetting link ";
Richard Alpe7be57fc2014-11-20 10:29:12 +0100205
Jon Paul Maloy52666982015-10-22 08:51:41 -0400206/* Send states for broadcast NACKs
207 */
208enum {
209 BC_NACK_SND_CONDITIONAL,
210 BC_NACK_SND_UNCONDITIONAL,
211 BC_NACK_SND_SUPPRESS,
212};
213
Jon Maloy53962bc2019-06-28 17:06:20 +0200214#define TIPC_BC_RETR_LIM (jiffies + msecs_to_jiffies(10))
Tuong Lien382f5982019-04-04 11:09:52 +0700215#define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
Jon Paul Maloy7c4a54b2016-09-01 13:52:50 -0400216
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900217/*
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400218 * Interval between NACKs when packets arrive out of order
219 */
220#define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500221
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400222/* Link FSM states:
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400223 */
224enum {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400225 LINK_ESTABLISHED = 0xe,
226 LINK_ESTABLISHING = 0xe << 4,
227 LINK_RESET = 0x1 << 8,
228 LINK_RESETTING = 0x2 << 12,
229 LINK_PEER_RESET = 0xd << 16,
230 LINK_FAILINGOVER = 0xf << 20,
231 LINK_SYNCHING = 0xc << 24
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400232};
233
234/* Link FSM state checking routines
235 */
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400236static int link_is_up(struct tipc_link *l)
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400237{
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400238 return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400239}
240
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400241static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
242 struct sk_buff_head *xmitq);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -0400243static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100244 bool probe_reply, u16 rcvgap,
245 int tolerance, int priority,
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -0400246 struct sk_buff_head *xmitq);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500247static void link_print(struct tipc_link *l, const char *str);
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -0400248static int tipc_link_build_nack_msg(struct tipc_link *l,
249 struct sk_buff_head *xmitq);
Jon Paul Maloy52666982015-10-22 08:51:41 -0400250static void tipc_link_build_bc_init_msg(struct tipc_link *l,
251 struct sk_buff_head *xmitq);
Jon Maloy16ad3f42019-12-10 00:52:46 +0100252static int tipc_link_release_pkts(struct tipc_link *l, u16 to);
Jon Maloyb7ffa042019-12-16 19:21:02 +0100253static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data, u16 gap);
Tuong Lien6a6b5c82019-06-17 12:15:42 +0700254static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
255 struct tipc_gap_ack_blks *ga,
256 struct sk_buff_head *xmitq);
Jon Maloy16ad3f42019-12-10 00:52:46 +0100257static void tipc_link_update_cwin(struct tipc_link *l, int released,
258 bool retransmitted);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800260 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 */
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400262bool tipc_link_is_up(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263{
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400264 return link_is_up(l);
265}
266
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400267bool tipc_link_peer_is_down(struct tipc_link *l)
268{
269 return l->state == LINK_PEER_RESET;
270}
271
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400272bool tipc_link_is_reset(struct tipc_link *l)
273{
274 return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
275}
276
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400277bool tipc_link_is_establishing(struct tipc_link *l)
278{
279 return l->state == LINK_ESTABLISHING;
280}
281
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400282bool tipc_link_is_synching(struct tipc_link *l)
283{
284 return l->state == LINK_SYNCHING;
285}
286
287bool tipc_link_is_failingover(struct tipc_link *l)
288{
289 return l->state == LINK_FAILINGOVER;
290}
291
292bool tipc_link_is_blocked(struct tipc_link *l)
293{
294 return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295}
296
Wu Fengguang742e0382015-10-24 22:56:01 +0800297static bool link_is_bc_sndlink(struct tipc_link *l)
Jon Paul Maloy52666982015-10-22 08:51:41 -0400298{
299 return !l->bc_sndlink;
300}
301
Wu Fengguang742e0382015-10-24 22:56:01 +0800302static bool link_is_bc_rcvlink(struct tipc_link *l)
Jon Paul Maloy52666982015-10-22 08:51:41 -0400303{
304 return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
305}
306
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400307void tipc_link_set_active(struct tipc_link *l, bool active)
308{
309 l->active = active;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310}
311
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500312u32 tipc_link_id(struct tipc_link *l)
313{
314 return l->peer_bearer_id << 16 | l->bearer_id;
315}
316
Jon Maloy16ad3f42019-12-10 00:52:46 +0100317int tipc_link_min_win(struct tipc_link *l)
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500318{
Jon Maloy16ad3f42019-12-10 00:52:46 +0100319 return l->min_win;
320}
321
322int tipc_link_max_win(struct tipc_link *l)
323{
324 return l->max_win;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500325}
326
327int tipc_link_prio(struct tipc_link *l)
328{
329 return l->priority;
330}
331
332unsigned long tipc_link_tolerance(struct tipc_link *l)
333{
334 return l->tolerance;
335}
336
337struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
338{
339 return l->inputq;
340}
341
342char tipc_link_plane(struct tipc_link *l)
343{
344 return l->net_plane;
345}
346
Jon Maloy9012de52018-07-10 01:07:35 +0200347void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
348{
349 l->peer_caps = capabilities;
350}
351
Jon Paul Maloy52666982015-10-22 08:51:41 -0400352void tipc_link_add_bc_peer(struct tipc_link *snd_l,
353 struct tipc_link *uc_l,
354 struct sk_buff_head *xmitq)
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400355{
Jon Paul Maloy52666982015-10-22 08:51:41 -0400356 struct tipc_link *rcv_l = uc_l->bc_rcvlink;
357
358 snd_l->ackers++;
359 rcv_l->acked = snd_l->snd_nxt - 1;
Jon Paul Maloy9a650832015-11-19 14:12:50 -0500360 snd_l->state = LINK_ESTABLISHED;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400361 tipc_link_build_bc_init_msg(uc_l, xmitq);
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400362}
363
Jon Paul Maloy52666982015-10-22 08:51:41 -0400364void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
365 struct tipc_link *rcv_l,
366 struct sk_buff_head *xmitq)
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400367{
Jon Paul Maloy52666982015-10-22 08:51:41 -0400368 u16 ack = snd_l->snd_nxt - 1;
369
370 snd_l->ackers--;
Jon Paul Maloya71eb722016-07-11 16:08:36 -0400371 rcv_l->bc_peer_is_up = true;
372 rcv_l->state = LINK_ESTABLISHED;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400373 tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
Tuong Lien26574db2018-12-19 09:17:57 +0700374 trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
Jon Paul Maloy52666982015-10-22 08:51:41 -0400375 tipc_link_reset(rcv_l);
376 rcv_l->state = LINK_RESET;
377 if (!snd_l->ackers) {
Tuong Lien26574db2018-12-19 09:17:57 +0700378 trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
Jon Paul Maloy52666982015-10-22 08:51:41 -0400379 tipc_link_reset(snd_l);
Jon Paul Maloy9a650832015-11-19 14:12:50 -0500380 snd_l->state = LINK_RESET;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400381 __skb_queue_purge(xmitq);
382 }
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400383}
384
385int tipc_link_bc_peers(struct tipc_link *l)
386{
387 return l->ackers;
388}
389
YueHaibinge064cce2018-07-19 17:16:59 +0800390static u16 link_bc_rcv_gap(struct tipc_link *l)
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -0400391{
392 struct sk_buff *skb = skb_peek(&l->deferdq);
393 u16 gap = 0;
394
395 if (more(l->snd_nxt, l->rcv_nxt))
396 gap = l->snd_nxt - l->rcv_nxt;
397 if (skb)
398 gap = buf_seqno(skb) - l->rcv_nxt;
399 return gap;
400}
401
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400402void tipc_link_set_mtu(struct tipc_link *l, int mtu)
403{
404 l->mtu = mtu;
405}
406
407int tipc_link_mtu(struct tipc_link *l)
408{
409 return l->mtu;
410}
411
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700412int tipc_link_mss(struct tipc_link *l)
413{
414#ifdef CONFIG_TIPC_CRYPTO
415 return l->mtu - INT_H_SIZE - EMSG_OVERHEAD;
416#else
417 return l->mtu - INT_H_SIZE;
418#endif
419}
420
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500421u16 tipc_link_rcv_nxt(struct tipc_link *l)
422{
423 return l->rcv_nxt;
424}
425
426u16 tipc_link_acked(struct tipc_link *l)
427{
428 return l->acked;
429}
430
431char *tipc_link_name(struct tipc_link *l)
432{
433 return l->name;
434}
435
LUU Duc Canhc140eb12018-09-26 21:00:54 +0200436u32 tipc_link_state(struct tipc_link *l)
437{
438 return l->state;
439}
440
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441/**
Per Liden4323add2006-01-18 00:38:21 +0100442 * tipc_link_create - create a new link
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400443 * @n: pointer to associated node
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400444 * @if_name: associated interface name
445 * @bearer_id: id (index) of associated bearer
446 * @tolerance: link tolerance to be used by link
447 * @net_plane: network plane (A,B,c..) this link belongs to
448 * @mtu: mtu to be advertised by link
449 * @priority: priority to be used by link
Jon Maloy16ad3f42019-12-10 00:52:46 +0100450 * @min_win: minimal send window to be used by link
451 * @max_win: maximal send window to be used by link
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400452 * @session: session to be used by link
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400453 * @ownnode: identity of own node
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400454 * @peer: node id of peer node
Jon Paul Maloyfd556f22015-10-22 08:51:40 -0400455 * @peer_caps: bitmap describing peer node capabilities
Jon Paul Maloy52666982015-10-22 08:51:41 -0400456 * @bc_sndlink: the namespace global link used for broadcast sending
457 * @bc_rcvlink: the peer specific link used for broadcast reception
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400458 * @inputq: queue to put messages ready for delivery
459 * @namedq: queue to put binding table update messages ready for delivery
460 * @link: return value, pointer to put the created link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900461 *
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400462 * Returns true if link was created, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 */
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400464bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400465 int tolerance, char net_plane, u32 mtu, int priority,
Jon Maloy16ad3f42019-12-10 00:52:46 +0100466 u32 min_win, u32 max_win, u32 session, u32 self,
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100467 u32 peer, u8 *peer_id, u16 peer_caps,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400468 struct tipc_link *bc_sndlink,
469 struct tipc_link *bc_rcvlink,
470 struct sk_buff_head *inputq,
471 struct sk_buff_head *namedq,
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400472 struct tipc_link **link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473{
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100474 char peer_str[NODE_ID_STR_LEN] = {0,};
475 char self_str[NODE_ID_STR_LEN] = {0,};
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400476 struct tipc_link *l;
Allan Stephens37b9c082011-02-28 11:32:27 -0500477
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400478 l = kzalloc(sizeof(*l), GFP_ATOMIC);
479 if (!l)
480 return false;
481 *link = l;
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500482 l->session = session;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400483
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100484 /* Set link name for unicast links only */
485 if (peer_id) {
486 tipc_nodeid2string(self_str, tipc_own_id(net));
487 if (strlen(self_str) > 16)
488 sprintf(self_str, "%x", self);
489 tipc_nodeid2string(peer_str, peer_id);
490 if (strlen(peer_str) > 16)
491 sprintf(peer_str, "%x", peer);
492 }
493 /* Peer i/f name will be completed by reset/activate message */
Jon Maloy7494cfa2018-03-29 23:20:45 +0200494 snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
495 self_str, if_name, peer_str);
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100496
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500497 strcpy(l->if_name, if_name);
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400498 l->addr = peer;
Jon Paul Maloyfd556f22015-10-22 08:51:40 -0400499 l->peer_caps = peer_caps;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400500 l->net = net;
Jon Maloy7ea817f2018-07-10 01:07:36 +0200501 l->in_session = false;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400502 l->bearer_id = bearer_id;
503 l->tolerance = tolerance;
Jon Maloy047491e2018-10-10 17:34:01 +0200504 if (bc_rcvlink)
505 bc_rcvlink->tolerance = tolerance;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400506 l->net_plane = net_plane;
507 l->advertised_mtu = mtu;
508 l->mtu = mtu;
509 l->priority = priority;
Jon Maloy16ad3f42019-12-10 00:52:46 +0100510 tipc_link_set_queue_limits(l, min_win, max_win);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -0400511 l->ackers = 1;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400512 l->bc_sndlink = bc_sndlink;
513 l->bc_rcvlink = bc_rcvlink;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400514 l->inputq = inputq;
515 l->namedq = namedq;
516 l->state = LINK_RESETTING;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400517 __skb_queue_head_init(&l->transmq);
518 __skb_queue_head_init(&l->backlogq);
519 __skb_queue_head_init(&l->deferdq);
Tuong Lien58ee86b2019-04-04 11:09:53 +0700520 __skb_queue_head_init(&l->failover_deferdq);
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400521 skb_queue_head_init(&l->wakeupq);
522 skb_queue_head_init(l->inputq);
523 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524}
525
Jon Paul Maloy32301902015-10-22 08:51:37 -0400526/**
527 * tipc_link_bc_create - create new link to be used for broadcast
528 * @n: pointer to associated node
Jon Maloy4c94cc22017-11-30 16:47:25 +0100529 * @mtu: mtu to be used initially if no peers
Jon Paul Maloy32301902015-10-22 08:51:37 -0400530 * @window: send window to be used
531 * @inputq: queue to put messages ready for delivery
532 * @namedq: queue to put binding table update messages ready for delivery
533 * @link: return value, pointer to put the created link
534 *
535 * Returns true if link was created, otherwise false
536 */
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400537bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
Jon Maloy16ad3f42019-12-10 00:52:46 +0100538 int mtu, u32 min_win, u32 max_win, u16 peer_caps,
Jon Paul Maloy32301902015-10-22 08:51:37 -0400539 struct sk_buff_head *inputq,
540 struct sk_buff_head *namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400541 struct tipc_link *bc_sndlink,
Jon Paul Maloy32301902015-10-22 08:51:37 -0400542 struct tipc_link **link)
543{
544 struct tipc_link *l;
545
Jon Maloy16ad3f42019-12-10 00:52:46 +0100546 if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, min_win,
547 max_win, 0, ownnode, peer, NULL, peer_caps,
548 bc_sndlink, NULL, inputq, namedq, link))
Jon Paul Maloy32301902015-10-22 08:51:37 -0400549 return false;
550
551 l = *link;
552 strcpy(l->name, tipc_bclink_name);
Tuong Lien26574db2018-12-19 09:17:57 +0700553 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
Jon Paul Maloy32301902015-10-22 08:51:37 -0400554 tipc_link_reset(l);
Jon Paul Maloy52666982015-10-22 08:51:41 -0400555 l->state = LINK_RESET;
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400556 l->ackers = 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400557 l->bc_rcvlink = l;
558
559 /* Broadcast send link is always up */
560 if (link_is_bc_sndlink(l))
561 l->state = LINK_ESTABLISHED;
562
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500563 /* Disable replicast if even a single peer doesn't support it */
564 if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
Hoang Leba5f6a82019-11-21 10:01:09 +0700565 tipc_bcast_toggle_rcast(net, false);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500566
Jon Paul Maloy32301902015-10-22 08:51:37 -0400567 return true;
568}
569
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570/**
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400571 * tipc_link_fsm_evt - link finite state machine
572 * @l: pointer to link
573 * @evt: state machine event to be processed
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400574 */
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400575int tipc_link_fsm_evt(struct tipc_link *l, int evt)
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400576{
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400577 int rc = 0;
Tuong Lien26574db2018-12-19 09:17:57 +0700578 int old_state = l->state;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400579
580 switch (l->state) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400581 case LINK_RESETTING:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400582 switch (evt) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400583 case LINK_PEER_RESET_EVT:
584 l->state = LINK_PEER_RESET;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400585 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400586 case LINK_RESET_EVT:
587 l->state = LINK_RESET;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400588 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400589 case LINK_FAILURE_EVT:
590 case LINK_FAILOVER_BEGIN_EVT:
591 case LINK_ESTABLISH_EVT:
592 case LINK_FAILOVER_END_EVT:
593 case LINK_SYNCH_BEGIN_EVT:
594 case LINK_SYNCH_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400595 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400596 goto illegal_evt;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400597 }
598 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400599 case LINK_RESET:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400600 switch (evt) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400601 case LINK_PEER_RESET_EVT:
602 l->state = LINK_ESTABLISHING;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400603 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400604 case LINK_FAILOVER_BEGIN_EVT:
605 l->state = LINK_FAILINGOVER;
606 case LINK_FAILURE_EVT:
607 case LINK_RESET_EVT:
608 case LINK_ESTABLISH_EVT:
609 case LINK_FAILOVER_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400610 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400611 case LINK_SYNCH_BEGIN_EVT:
612 case LINK_SYNCH_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400613 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400614 goto illegal_evt;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400615 }
616 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400617 case LINK_PEER_RESET:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400618 switch (evt) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400619 case LINK_RESET_EVT:
620 l->state = LINK_ESTABLISHING;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400621 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400622 case LINK_PEER_RESET_EVT:
623 case LINK_ESTABLISH_EVT:
624 case LINK_FAILURE_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400625 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400626 case LINK_SYNCH_BEGIN_EVT:
627 case LINK_SYNCH_END_EVT:
628 case LINK_FAILOVER_BEGIN_EVT:
629 case LINK_FAILOVER_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400630 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400631 goto illegal_evt;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400632 }
633 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400634 case LINK_FAILINGOVER:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400635 switch (evt) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400636 case LINK_FAILOVER_END_EVT:
637 l->state = LINK_RESET;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400638 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400639 case LINK_PEER_RESET_EVT:
640 case LINK_RESET_EVT:
641 case LINK_ESTABLISH_EVT:
642 case LINK_FAILURE_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400643 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400644 case LINK_FAILOVER_BEGIN_EVT:
645 case LINK_SYNCH_BEGIN_EVT:
646 case LINK_SYNCH_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400647 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400648 goto illegal_evt;
649 }
650 break;
651 case LINK_ESTABLISHING:
652 switch (evt) {
653 case LINK_ESTABLISH_EVT:
654 l->state = LINK_ESTABLISHED;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400655 break;
656 case LINK_FAILOVER_BEGIN_EVT:
657 l->state = LINK_FAILINGOVER;
658 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400659 case LINK_RESET_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400660 l->state = LINK_RESET;
661 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400662 case LINK_FAILURE_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400663 case LINK_PEER_RESET_EVT:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400664 case LINK_SYNCH_BEGIN_EVT:
665 case LINK_FAILOVER_END_EVT:
666 break;
667 case LINK_SYNCH_END_EVT:
668 default:
669 goto illegal_evt;
670 }
671 break;
672 case LINK_ESTABLISHED:
673 switch (evt) {
674 case LINK_PEER_RESET_EVT:
675 l->state = LINK_PEER_RESET;
676 rc |= TIPC_LINK_DOWN_EVT;
677 break;
678 case LINK_FAILURE_EVT:
679 l->state = LINK_RESETTING;
680 rc |= TIPC_LINK_DOWN_EVT;
681 break;
682 case LINK_RESET_EVT:
683 l->state = LINK_RESET;
684 break;
685 case LINK_ESTABLISH_EVT:
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400686 case LINK_SYNCH_END_EVT:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400687 break;
688 case LINK_SYNCH_BEGIN_EVT:
689 l->state = LINK_SYNCHING;
690 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400691 case LINK_FAILOVER_BEGIN_EVT:
692 case LINK_FAILOVER_END_EVT:
693 default:
694 goto illegal_evt;
695 }
696 break;
697 case LINK_SYNCHING:
698 switch (evt) {
699 case LINK_PEER_RESET_EVT:
700 l->state = LINK_PEER_RESET;
701 rc |= TIPC_LINK_DOWN_EVT;
702 break;
703 case LINK_FAILURE_EVT:
704 l->state = LINK_RESETTING;
705 rc |= TIPC_LINK_DOWN_EVT;
706 break;
707 case LINK_RESET_EVT:
708 l->state = LINK_RESET;
709 break;
710 case LINK_ESTABLISH_EVT:
711 case LINK_SYNCH_BEGIN_EVT:
712 break;
713 case LINK_SYNCH_END_EVT:
714 l->state = LINK_ESTABLISHED;
715 break;
716 case LINK_FAILOVER_BEGIN_EVT:
717 case LINK_FAILOVER_END_EVT:
718 default:
719 goto illegal_evt;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400720 }
721 break;
722 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400723 pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400724 }
Tuong Lien26574db2018-12-19 09:17:57 +0700725 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400726 return rc;
727illegal_evt:
728 pr_err("Illegal FSM event %x in state %x on link %s\n",
729 evt, l->state, l->name);
Tuong Lien26574db2018-12-19 09:17:57 +0700730 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400731 return rc;
732}
733
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400734/* link_profile_stats - update statistical profiling of traffic
735 */
736static void link_profile_stats(struct tipc_link *l)
737{
738 struct sk_buff *skb;
739 struct tipc_msg *msg;
740 int length;
741
742 /* Update counters used in statistical profiling of send traffic */
743 l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
744 l->stats.queue_sz_counts++;
745
746 skb = skb_peek(&l->transmq);
747 if (!skb)
748 return;
749 msg = buf_msg(skb);
750 length = msg_size(msg);
751
752 if (msg_user(msg) == MSG_FRAGMENTER) {
753 if (msg_type(msg) != FIRST_FRAGMENT)
754 return;
Jon Maloya7dc51a2019-06-25 19:37:00 +0200755 length = msg_size(msg_inner_hdr(msg));
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400756 }
757 l->stats.msg_lengths_total += length;
758 l->stats.msg_length_counts++;
759 if (length <= 64)
760 l->stats.msg_length_profile[0]++;
761 else if (length <= 256)
762 l->stats.msg_length_profile[1]++;
763 else if (length <= 1024)
764 l->stats.msg_length_profile[2]++;
765 else if (length <= 4096)
766 l->stats.msg_length_profile[3]++;
767 else if (length <= 16384)
768 l->stats.msg_length_profile[4]++;
769 else if (length <= 32768)
770 l->stats.msg_length_profile[5]++;
771 else
772 l->stats.msg_length_profile[6]++;
773}
774
Tuong Lien26574db2018-12-19 09:17:57 +0700775/**
776 * tipc_link_too_silent - check if link is "too silent"
777 * @l: tipc link to be checked
778 *
779 * Returns true if the link 'silent_intv_cnt' is about to reach the
780 * 'abort_limit' value, otherwise false
781 */
782bool tipc_link_too_silent(struct tipc_link *l)
783{
784 return (l->silent_intv_cnt + 2 > l->abort_limit);
785}
786
Jon Maloy16ad3f42019-12-10 00:52:46 +0100787static int tipc_link_bc_retrans(struct tipc_link *l, struct tipc_link *r,
788 u16 from, u16 to, struct sk_buff_head *xmitq);
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400789/* tipc_link_timeout - perform periodic task as instructed from node timeout
790 */
791int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
792{
Ying Xuec91522f2016-06-15 14:11:31 +0800793 int mtyp = 0;
794 int rc = 0;
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400795 bool state = false;
796 bool probe = false;
797 bool setup = false;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400798 u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
799 u16 bc_acked = l->bc_rcvlink->acked;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400800 struct tipc_mon_state *mstate = &l->mon_state;
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400801
Tuong Lien26574db2018-12-19 09:17:57 +0700802 trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
803 trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400804 switch (l->state) {
805 case LINK_ESTABLISHED:
806 case LINK_SYNCHING:
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400807 mtyp = STATE_MSG;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400808 link_profile_stats(l);
809 tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
810 if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
811 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400812 state = bc_acked != bc_snt;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400813 state |= l->bc_rcvlink->rcv_unacked;
814 state |= l->rcv_unacked;
815 state |= !skb_queue_empty(&l->transmq);
816 state |= !skb_queue_empty(&l->deferdq);
817 probe = mstate->probing;
818 probe |= l->silent_intv_cnt;
819 if (probe || mstate->monitoring)
820 l->silent_intv_cnt++;
Jon Maloy16ad3f42019-12-10 00:52:46 +0100821 if (l->snd_nxt == l->checkpoint) {
822 tipc_link_update_cwin(l, 0, 0);
823 probe = true;
824 }
825 l->checkpoint = l->snd_nxt;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400826 break;
827 case LINK_RESET:
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400828 setup = l->rst_cnt++ <= 4;
829 setup |= !(l->rst_cnt % 16);
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400830 mtyp = RESET_MSG;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400831 break;
832 case LINK_ESTABLISHING:
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400833 setup = true;
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400834 mtyp = ACTIVATE_MSG;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400835 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400836 case LINK_PEER_RESET:
Jon Paul Maloy598411d702015-07-30 18:24:23 -0400837 case LINK_RESETTING:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400838 case LINK_FAILINGOVER:
839 break;
840 default:
841 break;
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400842 }
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400843
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400844 if (state || probe || setup)
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100845 tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400846
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400847 return rc;
848}
849
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400850/**
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400851 * link_schedule_user - schedule a message sender for wakeup after congestion
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500852 * @l: congested link
853 * @hdr: header of message that is being sent
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400854 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500856static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857{
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500858 u32 dnode = tipc_own_addr(l->net);
859 u32 dport = msg_origport(hdr);
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400860 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400862 /* Create and schedule wakeup pseudo message */
863 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500864 dnode, l->addr, dport, 0, 0);
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400865 if (!skb)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400866 return -ENOBUFS;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500867 msg_set_dest_droppable(buf_msg(skb), true);
868 TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
869 skb_queue_tail(&l->wakeupq, skb);
870 l->stats.link_congs++;
Tuong Lien26574db2018-12-19 09:17:57 +0700871 trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400872 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873}
874
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400875/**
876 * link_prepare_wakeup - prepare users for wakeup after congestion
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500877 * @l: congested link
878 * Wake up a number of waiting users, as permitted by available space
879 * in the send queue
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400880 */
YueHaibinge064cce2018-07-19 17:16:59 +0800881static void link_prepare_wakeup(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882{
Jon Maloy7c5b4202019-07-30 16:23:18 +0200883 struct sk_buff_head *wakeupq = &l->wakeupq;
884 struct sk_buff_head *inputq = l->inputq;
Ying Xue58d78b32014-11-26 11:41:51 +0800885 struct sk_buff *skb, *tmp;
Jon Maloy7c5b4202019-07-30 16:23:18 +0200886 struct sk_buff_head tmpq;
887 int avail[5] = {0,};
888 int imp = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889
Jon Maloy7c5b4202019-07-30 16:23:18 +0200890 __skb_queue_head_init(&tmpq);
891
892 for (; imp <= TIPC_SYSTEM_IMPORTANCE; imp++)
893 avail[imp] = l->backlog[imp].limit - l->backlog[imp].len;
894
895 skb_queue_walk_safe(wakeupq, skb, tmp) {
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400896 imp = TIPC_SKB_CB(skb)->chain_imp;
Jon Maloy7c5b4202019-07-30 16:23:18 +0200897 if (avail[imp] <= 0)
898 continue;
899 avail[imp]--;
900 __skb_unlink(skb, wakeupq);
901 __skb_queue_tail(&tmpq, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 }
Jon Maloy7c5b4202019-07-30 16:23:18 +0200903
904 spin_lock_bh(&inputq->lock);
905 skb_queue_splice_tail(&tmpq, inputq);
906 spin_unlock_bh(&inputq->lock);
907
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908}
909
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400910void tipc_link_reset(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911{
Ying Xuea1f8dd32018-10-11 19:57:56 +0800912 struct sk_buff_head list;
Tuong Liene95584a2019-10-02 18:49:43 +0700913 u32 imp;
Ying Xuea1f8dd32018-10-11 19:57:56 +0800914
915 __skb_queue_head_init(&list);
916
Jon Maloy7ea817f2018-07-10 01:07:36 +0200917 l->in_session = false;
Tuong Lienf7a93782019-04-16 10:48:07 +0700918 /* Force re-synch of peer session number before establishing */
919 l->peer_session--;
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500920 l->session++;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400921 l->mtu = l->advertised_mtu;
Ying Xuea1f8dd32018-10-11 19:57:56 +0800922
Parthasarathy Bhuvaragan3f32d0b2018-09-25 22:09:10 +0200923 spin_lock_bh(&l->wakeupq.lock);
Ying Xuea1f8dd32018-10-11 19:57:56 +0800924 skb_queue_splice_init(&l->wakeupq, &list);
Parthasarathy Bhuvaragan3f32d0b2018-09-25 22:09:10 +0200925 spin_unlock_bh(&l->wakeupq.lock);
926
Ying Xuea1f8dd32018-10-11 19:57:56 +0800927 spin_lock_bh(&l->inputq->lock);
928 skb_queue_splice_init(&list, l->inputq);
929 spin_unlock_bh(&l->inputq->lock);
930
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400931 __skb_queue_purge(&l->transmq);
932 __skb_queue_purge(&l->deferdq);
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -0400933 __skb_queue_purge(&l->backlogq);
Tuong Lien58ee86b2019-04-04 11:09:53 +0700934 __skb_queue_purge(&l->failover_deferdq);
Tuong Liene95584a2019-10-02 18:49:43 +0700935 for (imp = 0; imp <= TIPC_SYSTEM_IMPORTANCE; imp++) {
936 l->backlog[imp].len = 0;
937 l->backlog[imp].target_bskb = NULL;
938 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400939 kfree_skb(l->reasm_buf);
Tuong Lien2320bcd2019-07-24 08:56:12 +0700940 kfree_skb(l->reasm_tnlmsg);
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400941 kfree_skb(l->failover_reasm_skb);
942 l->reasm_buf = NULL;
Tuong Lien2320bcd2019-07-24 08:56:12 +0700943 l->reasm_tnlmsg = NULL;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400944 l->failover_reasm_skb = NULL;
945 l->rcv_unacked = 0;
946 l->snd_nxt = 1;
947 l->rcv_nxt = 1;
Jon Maloy9012de52018-07-10 01:07:35 +0200948 l->snd_nxt_state = 1;
949 l->rcv_nxt_state = 1;
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -0400950 l->acked = 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400951 l->silent_intv_cnt = 0;
Jon Paul Maloy88e8ac72016-04-15 13:33:04 -0400952 l->rst_cnt = 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400953 l->bc_peer_is_up = false;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400954 memset(&l->mon_state, 0, sizeof(l->mon_state));
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500955 tipc_link_reset_stats(l);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956}
957
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958/**
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400959 * tipc_link_xmit(): enqueue buffer list according to queue situation
960 * @link: link to use
961 * @list: chain of buffers containing message
962 * @xmitq: returned list of packets to be sent by caller
963 *
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500964 * Consumes the buffer chain.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400965 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
966 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
967 */
968int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
969 struct sk_buff_head *xmitq)
970{
971 struct tipc_msg *hdr = buf_msg(skb_peek(list));
Tuong Lien06e7c702019-11-01 09:58:57 +0700972 struct sk_buff_head *backlogq = &l->backlogq;
973 struct sk_buff_head *transmq = &l->transmq;
974 struct sk_buff *skb, *_skb;
975 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400976 u16 ack = l->rcv_nxt - 1;
977 u16 seqno = l->snd_nxt;
Jon Paul Maloy95901122016-11-25 10:35:02 -0500978 int pkt_cnt = skb_queue_len(list);
Tuong Lien06e7c702019-11-01 09:58:57 +0700979 int imp = msg_importance(hdr);
Tuong Lienfc1b6d62019-11-08 12:05:11 +0700980 unsigned int mss = tipc_link_mss(l);
Jon Maloy16ad3f42019-12-10 00:52:46 +0100981 unsigned int cwin = l->window;
Tuong Lien06e7c702019-11-01 09:58:57 +0700982 unsigned int mtu = l->mtu;
983 bool new_bundle;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500984 int rc = 0;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400985
Richard Alpe4952cd32016-02-11 10:43:15 +0100986 if (unlikely(msg_size(hdr) > mtu)) {
Tuong Lien2320bcd2019-07-24 08:56:12 +0700987 pr_warn("Too large msg, purging xmit list %d %d %d %d %d!\n",
988 skb_queue_len(list), msg_user(hdr),
989 msg_type(hdr), msg_size(hdr), mtu);
Jon Maloye654f9f2019-08-15 16:42:50 +0200990 __skb_queue_purge(list);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400991 return -EMSGSIZE;
Richard Alpe4952cd32016-02-11 10:43:15 +0100992 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400993
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500994 /* Allow oversubscription of one data msg per source at congestion */
995 if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
996 if (imp == TIPC_SYSTEM_IMPORTANCE) {
997 pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
998 return -ENOBUFS;
999 }
1000 rc = link_schedule_user(l, hdr);
1001 }
1002
Jon Paul Maloy95901122016-11-25 10:35:02 -05001003 if (pkt_cnt > 1) {
1004 l->stats.sent_fragmented++;
1005 l->stats.sent_fragments += pkt_cnt;
1006 }
1007
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001008 /* Prepare each packet for sending, and add to relevant queue: */
Tuong Lien06e7c702019-11-01 09:58:57 +07001009 while ((skb = __skb_dequeue(list))) {
Jon Maloy16ad3f42019-12-10 00:52:46 +01001010 if (likely(skb_queue_len(transmq) < cwin)) {
Tuong Lien06e7c702019-11-01 09:58:57 +07001011 hdr = buf_msg(skb);
1012 msg_set_seqno(hdr, seqno);
1013 msg_set_ack(hdr, ack);
1014 msg_set_bcast_ack(hdr, bc_ack);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001015 _skb = skb_clone(skb, GFP_ATOMIC);
Richard Alpe4952cd32016-02-11 10:43:15 +01001016 if (!_skb) {
Tuong Lien06e7c702019-11-01 09:58:57 +07001017 kfree_skb(skb);
Jon Maloye654f9f2019-08-15 16:42:50 +02001018 __skb_queue_purge(list);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001019 return -ENOBUFS;
Richard Alpe4952cd32016-02-11 10:43:15 +01001020 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001021 __skb_queue_tail(transmq, skb);
Hoang Le05572272018-12-19 11:42:19 +07001022 /* next retransmit attempt */
1023 if (link_is_bc_sndlink(l))
Jon Maloy53962bc2019-06-28 17:06:20 +02001024 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001025 __skb_queue_tail(xmitq, _skb);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04001026 TIPC_SKB_CB(skb)->ackers = l->ackers;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001027 l->rcv_unacked = 0;
Jon Paul Maloy95901122016-11-25 10:35:02 -05001028 l->stats.sent_pkts++;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001029 seqno++;
1030 continue;
1031 }
Tuong Lien06e7c702019-11-01 09:58:57 +07001032 if (tipc_msg_try_bundle(l->backlog[imp].target_bskb, &skb,
Tuong Lienfc1b6d62019-11-08 12:05:11 +07001033 mss, l->addr, &new_bundle)) {
Tuong Lien06e7c702019-11-01 09:58:57 +07001034 if (skb) {
1035 /* Keep a ref. to the skb for next try */
1036 l->backlog[imp].target_bskb = skb;
1037 l->backlog[imp].len++;
1038 __skb_queue_tail(backlogq, skb);
1039 } else {
1040 if (new_bundle) {
1041 l->stats.sent_bundles++;
1042 l->stats.sent_bundled++;
1043 }
1044 l->stats.sent_bundled++;
1045 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001046 continue;
1047 }
Tuong Liene95584a2019-10-02 18:49:43 +07001048 l->backlog[imp].target_bskb = NULL;
Tuong Lien06e7c702019-11-01 09:58:57 +07001049 l->backlog[imp].len += (1 + skb_queue_len(list));
1050 __skb_queue_tail(backlogq, skb);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001051 skb_queue_splice_tail_init(list, backlogq);
1052 }
1053 l->snd_nxt = seqno;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001054 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001055}
1056
Jon Maloy16ad3f42019-12-10 00:52:46 +01001057static void tipc_link_update_cwin(struct tipc_link *l, int released,
1058 bool retransmitted)
1059{
1060 int bklog_len = skb_queue_len(&l->backlogq);
1061 struct sk_buff_head *txq = &l->transmq;
1062 int txq_len = skb_queue_len(txq);
1063 u16 cwin = l->window;
1064
1065 /* Enter fast recovery */
1066 if (unlikely(retransmitted)) {
1067 l->ssthresh = max_t(u16, l->window / 2, 300);
Tuong Lienedadedf2020-04-15 18:34:49 +07001068 l->window = min_t(u16, l->ssthresh, l->window);
Jon Maloy16ad3f42019-12-10 00:52:46 +01001069 return;
1070 }
1071 /* Enter slow start */
1072 if (unlikely(!released)) {
1073 l->ssthresh = max_t(u16, l->window / 2, 300);
1074 l->window = l->min_win;
1075 return;
1076 }
1077 /* Don't increase window if no pressure on the transmit queue */
1078 if (txq_len + bklog_len < cwin)
1079 return;
1080
1081 /* Don't increase window if there are holes the transmit queue */
1082 if (txq_len && l->snd_nxt - buf_seqno(skb_peek(txq)) != txq_len)
1083 return;
1084
1085 l->cong_acks += released;
1086
1087 /* Slow start */
1088 if (cwin <= l->ssthresh) {
1089 l->window = min_t(u16, cwin + released, l->max_win);
1090 return;
1091 }
1092 /* Congestion avoidance */
1093 if (l->cong_acks < cwin)
1094 return;
1095 l->window = min_t(u16, ++cwin, l->max_win);
1096 l->cong_acks = 0;
1097}
1098
YueHaibinge064cce2018-07-19 17:16:59 +08001099static void tipc_link_advance_backlog(struct tipc_link *l,
1100 struct sk_buff_head *xmitq)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001101{
Jon Paul Maloy52666982015-10-22 08:51:41 -04001102 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001103 struct sk_buff_head *txq = &l->transmq;
1104 struct sk_buff *skb, *_skb;
1105 u16 ack = l->rcv_nxt - 1;
1106 u16 seqno = l->snd_nxt;
1107 struct tipc_msg *hdr;
1108 u16 cwin = l->window;
Tuong Liene95584a2019-10-02 18:49:43 +07001109 u32 imp;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001110
Jon Maloy16ad3f42019-12-10 00:52:46 +01001111 while (skb_queue_len(txq) < cwin) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001112 skb = skb_peek(&l->backlogq);
1113 if (!skb)
1114 break;
1115 _skb = skb_clone(skb, GFP_ATOMIC);
1116 if (!_skb)
1117 break;
1118 __skb_dequeue(&l->backlogq);
1119 hdr = buf_msg(skb);
Tuong Liene95584a2019-10-02 18:49:43 +07001120 imp = msg_importance(hdr);
1121 l->backlog[imp].len--;
1122 if (unlikely(skb == l->backlog[imp].target_bskb))
1123 l->backlog[imp].target_bskb = NULL;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001124 __skb_queue_tail(&l->transmq, skb);
Hoang Le05572272018-12-19 11:42:19 +07001125 /* next retransmit attempt */
1126 if (link_is_bc_sndlink(l))
Jon Maloy53962bc2019-06-28 17:06:20 +02001127 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
Hoang Le05572272018-12-19 11:42:19 +07001128
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001129 __skb_queue_tail(xmitq, _skb);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04001130 TIPC_SKB_CB(skb)->ackers = l->ackers;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001131 msg_set_seqno(hdr, seqno);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001132 msg_set_ack(hdr, ack);
1133 msg_set_bcast_ack(hdr, bc_ack);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001134 l->rcv_unacked = 0;
Jon Paul Maloy95901122016-11-25 10:35:02 -05001135 l->stats.sent_pkts++;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001136 seqno++;
1137 }
1138 l->snd_nxt = seqno;
1139}
1140
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001141/**
1142 * link_retransmit_failure() - Detect repeated retransmit failures
1143 * @l: tipc link sender
1144 * @r: tipc link receiver (= l in case of unicast)
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001145 * @rc: returned code
1146 *
1147 * Return: true if the repeated retransmit failures happens, otherwise
1148 * false
1149 */
1150static bool link_retransmit_failure(struct tipc_link *l, struct tipc_link *r,
Tuong Lien71204232019-08-15 10:24:08 +07001151 int *rc)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001152{
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001153 struct sk_buff *skb = skb_peek(&l->transmq);
1154 struct tipc_msg *hdr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001155
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001156 if (!skb)
1157 return false;
Tuong Lien71204232019-08-15 10:24:08 +07001158
1159 if (!TIPC_SKB_CB(skb)->retr_cnt)
1160 return false;
1161
1162 if (!time_after(jiffies, TIPC_SKB_CB(skb)->retr_stamp +
Hoang Le426071f2019-11-06 13:26:10 +07001163 msecs_to_jiffies(r->tolerance * 10)))
Tuong Lien71204232019-08-15 10:24:08 +07001164 return false;
1165
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001166 hdr = buf_msg(skb);
Tuong Lien71204232019-08-15 10:24:08 +07001167 if (link_is_bc_sndlink(l) && !less(r->acked, msg_seqno(hdr)))
1168 return false;
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001169
Tuong Lien71204232019-08-15 10:24:08 +07001170 pr_warn("Retransmission failure on link <%s>\n", l->name);
1171 link_print(l, "State of link ");
1172 pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
1173 msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
1174 pr_info("sqno %u, prev: %x, dest: %x\n",
1175 msg_seqno(hdr), msg_prevnode(hdr), msg_destnode(hdr));
1176 pr_info("retr_stamp %d, retr_cnt %d\n",
1177 jiffies_to_msecs(TIPC_SKB_CB(skb)->retr_stamp),
1178 TIPC_SKB_CB(skb)->retr_cnt);
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001179
Tuong Lien71204232019-08-15 10:24:08 +07001180 trace_tipc_list_dump(&l->transmq, true, "retrans failure!");
1181 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "retrans failure!");
1182 trace_tipc_link_dump(r, TIPC_DUMP_NONE, "retrans failure!");
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001183
Tuong Lien71204232019-08-15 10:24:08 +07001184 if (link_is_bc_sndlink(l)) {
1185 r->state = LINK_RESET;
1186 *rc = TIPC_LINK_DOWN_EVT;
1187 } else {
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001188 *rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001189 }
1190
Tuong Lien71204232019-08-15 10:24:08 +07001191 return true;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001192}
1193
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001194/* tipc_link_bc_retrans() - retransmit zero or more packets
Jon Maloya4dc70d2018-07-06 15:22:36 +02001195 * @l: the link to transmit on
1196 * @r: the receiving link ordering the retransmit. Same as l if unicast
1197 * @from: retransmit from (inclusive) this sequence number
1198 * @to: retransmit to (inclusive) this sequence number
1199 * xmitq: queue for accumulating the retransmitted packets
1200 */
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001201static int tipc_link_bc_retrans(struct tipc_link *l, struct tipc_link *r,
1202 u16 from, u16 to, struct sk_buff_head *xmitq)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001203{
1204 struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001205 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
Jon Maloya4dc70d2018-07-06 15:22:36 +02001206 u16 ack = l->rcv_nxt - 1;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001207 int retransmitted = 0;
Jon Maloya4dc70d2018-07-06 15:22:36 +02001208 struct tipc_msg *hdr;
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001209 int rc = 0;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001210
1211 if (!skb)
1212 return 0;
LUU Duc Canh31c4f4c2018-11-10 14:23:50 -05001213 if (less(to, from))
1214 return 0;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001215
Tuong Lien26574db2018-12-19 09:17:57 +07001216 trace_tipc_link_retrans(r, from, to, &l->transmq);
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001217
Tuong Lien71204232019-08-15 10:24:08 +07001218 if (link_retransmit_failure(l, r, &rc))
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001219 return rc;
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04001220
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001221 skb_queue_walk(&l->transmq, skb) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001222 hdr = buf_msg(skb);
Jon Maloya4dc70d2018-07-06 15:22:36 +02001223 if (less(msg_seqno(hdr), from))
1224 continue;
1225 if (more(msg_seqno(hdr), to))
1226 break;
Tuong Lien71204232019-08-15 10:24:08 +07001227 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1228 continue;
1229 TIPC_SKB_CB(skb)->nxt_retr = TIPC_BC_RETR_LIM;
Tuong Lienfc1b6d62019-11-08 12:05:11 +07001230 _skb = pskb_copy(skb, GFP_ATOMIC);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001231 if (!_skb)
1232 return 0;
1233 hdr = buf_msg(_skb);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04001234 msg_set_ack(hdr, ack);
1235 msg_set_bcast_ack(hdr, bc_ack);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001236 _skb->priority = TC_PRIO_CONTROL;
1237 __skb_queue_tail(xmitq, _skb);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001238 l->stats.retransmitted++;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001239 retransmitted++;
Tuong Lien71204232019-08-15 10:24:08 +07001240 /* Increase actual retrans counter & mark first time */
1241 if (!TIPC_SKB_CB(skb)->retr_cnt++)
1242 TIPC_SKB_CB(skb)->retr_stamp = jiffies;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001243 }
Jon Maloy16ad3f42019-12-10 00:52:46 +01001244 tipc_link_update_cwin(l, 0, retransmitted);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001245 return 0;
1246}
1247
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001248/* tipc_data_input - deliver data and name distr msgs to upper layer
Erik Hugne7ae934b2014-07-01 10:22:40 +02001249 *
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001250 * Consumes buffer if message is of right type
Erik Hugne7ae934b2014-07-01 10:22:40 +02001251 * Node lock must be held
1252 */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001253static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
Jon Paul Maloy9073fb82015-07-30 18:24:25 -04001254 struct sk_buff_head *inputq)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001255{
Jon Maloy399574d2017-10-13 11:04:32 +02001256 struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001257 struct tipc_msg *hdr = buf_msg(skb);
1258
1259 switch (msg_user(hdr)) {
Erik Hugne7ae934b2014-07-01 10:22:40 +02001260 case TIPC_LOW_IMPORTANCE:
1261 case TIPC_MEDIUM_IMPORTANCE:
1262 case TIPC_HIGH_IMPORTANCE:
1263 case TIPC_CRITICAL_IMPORTANCE:
Jon Maloy2f487712017-10-13 11:04:31 +02001264 if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
Jon Maloy399574d2017-10-13 11:04:32 +02001265 skb_queue_tail(mc_inputq, skb);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001266 return true;
1267 }
Gustavo A. R. Silvaf79e3362019-01-23 01:09:31 -06001268 /* fall through */
Jon Maloy2f487712017-10-13 11:04:31 +02001269 case CONN_MANAGER:
Jon Maloy36c0a9d2017-10-16 16:04:51 +02001270 skb_queue_tail(inputq, skb);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001271 return true;
Jon Maloy399574d2017-10-13 11:04:32 +02001272 case GROUP_PROTOCOL:
1273 skb_queue_tail(mc_inputq, skb);
1274 return true;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001275 case NAME_DISTRIBUTOR:
Jon Paul Maloy52666982015-10-22 08:51:41 -04001276 l->bc_rcvlink->state = LINK_ESTABLISHED;
1277 skb_queue_tail(l->namedq, skb);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001278 return true;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001279 case MSG_BUNDLER:
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001280 case TUNNEL_PROTOCOL:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001281 case MSG_FRAGMENTER:
1282 case BCAST_PROTOCOL:
1283 return false;
1284 default:
1285 pr_warn("Dropping received illegal msg type\n");
1286 kfree_skb(skb);
Hoang Le7384b532019-02-11 09:18:28 +07001287 return true;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001288 };
1289}
1290
1291/* tipc_link_input - process packet that has passed link protocol check
1292 *
1293 * Consumes buffer
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001294 */
Jon Paul Maloy9073fb82015-07-30 18:24:25 -04001295static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
Tuong Lien58ee86b2019-04-04 11:09:53 +07001296 struct sk_buff_head *inputq,
1297 struct sk_buff **reasm_skb)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001298{
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001299 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001300 struct sk_buff *iskb;
Jon Paul Maloy9945e802015-10-15 14:52:40 -04001301 struct sk_buff_head tmpq;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001302 int usr = msg_user(hdr);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001303 int pos = 0;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001304
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001305 if (usr == MSG_BUNDLER) {
Jon Paul Maloy9945e802015-10-15 14:52:40 -04001306 skb_queue_head_init(&tmpq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001307 l->stats.recv_bundles++;
1308 l->stats.recv_bundled += msg_msgcnt(hdr);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001309 while (tipc_msg_extract(skb, &iskb, &pos))
Jon Paul Maloy9945e802015-10-15 14:52:40 -04001310 tipc_data_input(l, iskb, &tmpq);
1311 tipc_skb_queue_splice_tail(&tmpq, inputq);
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001312 return 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001313 } else if (usr == MSG_FRAGMENTER) {
1314 l->stats.recv_fragments++;
1315 if (tipc_buf_append(reasm_skb, &skb)) {
1316 l->stats.recv_fragmented++;
Jon Paul Maloy9073fb82015-07-30 18:24:25 -04001317 tipc_data_input(l, skb, inputq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001318 } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
1319 pr_warn_ratelimited("Unable to build fragment list\n");
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001320 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001321 }
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001322 return 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001323 } else if (usr == BCAST_PROTOCOL) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04001324 tipc_bcast_lock(l->net);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001325 tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04001326 tipc_bcast_unlock(l->net);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001327 }
Tuong Lien58ee86b2019-04-04 11:09:53 +07001328
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001329 kfree_skb(skb);
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001330 return 0;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001331}
1332
Tuong Lien58ee86b2019-04-04 11:09:53 +07001333/* tipc_link_tnl_rcv() - receive TUNNEL_PROTOCOL message, drop or process the
1334 * inner message along with the ones in the old link's
1335 * deferdq
1336 * @l: tunnel link
1337 * @skb: TUNNEL_PROTOCOL message
1338 * @inputq: queue to put messages ready for delivery
1339 */
1340static int tipc_link_tnl_rcv(struct tipc_link *l, struct sk_buff *skb,
1341 struct sk_buff_head *inputq)
1342{
1343 struct sk_buff **reasm_skb = &l->failover_reasm_skb;
Tuong Lien2320bcd2019-07-24 08:56:12 +07001344 struct sk_buff **reasm_tnlmsg = &l->reasm_tnlmsg;
Tuong Lien58ee86b2019-04-04 11:09:53 +07001345 struct sk_buff_head *fdefq = &l->failover_deferdq;
1346 struct tipc_msg *hdr = buf_msg(skb);
1347 struct sk_buff *iskb;
1348 int ipos = 0;
1349 int rc = 0;
1350 u16 seqno;
1351
Tuong Lien2320bcd2019-07-24 08:56:12 +07001352 if (msg_type(hdr) == SYNCH_MSG) {
1353 kfree_skb(skb);
1354 return 0;
1355 }
Tuong Lien58ee86b2019-04-04 11:09:53 +07001356
Tuong Lien2320bcd2019-07-24 08:56:12 +07001357 /* Not a fragment? */
1358 if (likely(!msg_nof_fragms(hdr))) {
1359 if (unlikely(!tipc_msg_extract(skb, &iskb, &ipos))) {
1360 pr_warn_ratelimited("Unable to extract msg, defq: %d\n",
1361 skb_queue_len(fdefq));
1362 return 0;
1363 }
1364 kfree_skb(skb);
1365 } else {
1366 /* Set fragment type for buf_append */
1367 if (msg_fragm_no(hdr) == 1)
1368 msg_set_type(hdr, FIRST_FRAGMENT);
1369 else if (msg_fragm_no(hdr) < msg_nof_fragms(hdr))
1370 msg_set_type(hdr, FRAGMENT);
1371 else
1372 msg_set_type(hdr, LAST_FRAGMENT);
1373
1374 if (!tipc_buf_append(reasm_tnlmsg, &skb)) {
1375 /* Successful but non-complete reassembly? */
1376 if (*reasm_tnlmsg || link_is_bc_rcvlink(l))
1377 return 0;
1378 pr_warn_ratelimited("Unable to reassemble tunnel msg\n");
1379 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1380 }
1381 iskb = skb;
Tuong Lien58ee86b2019-04-04 11:09:53 +07001382 }
1383
1384 do {
1385 seqno = buf_seqno(iskb);
Tuong Lien58ee86b2019-04-04 11:09:53 +07001386 if (unlikely(less(seqno, l->drop_point))) {
1387 kfree_skb(iskb);
1388 continue;
1389 }
Tuong Lien58ee86b2019-04-04 11:09:53 +07001390 if (unlikely(seqno != l->drop_point)) {
1391 __tipc_skb_queue_sorted(fdefq, seqno, iskb);
1392 continue;
1393 }
1394
1395 l->drop_point++;
Tuong Lien58ee86b2019-04-04 11:09:53 +07001396 if (!tipc_data_input(l, iskb, inputq))
1397 rc |= tipc_link_input(l, iskb, inputq, reasm_skb);
1398 if (unlikely(rc))
1399 break;
1400 } while ((iskb = __tipc_skb_dequeue(fdefq, l->drop_point)));
1401
Tuong Lien58ee86b2019-04-04 11:09:53 +07001402 return rc;
1403}
1404
Jon Maloy16ad3f42019-12-10 00:52:46 +01001405static int tipc_link_release_pkts(struct tipc_link *l, u16 acked)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001406{
Jon Maloy16ad3f42019-12-10 00:52:46 +01001407 int released = 0;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001408 struct sk_buff *skb, *tmp;
1409
1410 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1411 if (more(buf_seqno(skb), acked))
1412 break;
1413 __skb_unlink(skb, &l->transmq);
1414 kfree_skb(skb);
Jon Maloy16ad3f42019-12-10 00:52:46 +01001415 released++;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001416 }
1417 return released;
1418}
1419
Tuong Lien91959482019-04-04 11:09:51 +07001420/* tipc_build_gap_ack_blks - build Gap ACK blocks
1421 * @l: tipc link that data have come with gaps in sequence if any
1422 * @data: data buffer to store the Gap ACK blocks after built
1423 *
1424 * returns the actual allocated memory size
1425 */
Jon Maloyb7ffa042019-12-16 19:21:02 +01001426static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data, u16 gap)
Tuong Lien91959482019-04-04 11:09:51 +07001427{
1428 struct sk_buff *skb = skb_peek(&l->deferdq);
1429 struct tipc_gap_ack_blks *ga = data;
1430 u16 len, expect, seqno = 0;
1431 u8 n = 0;
1432
Jon Maloyb7ffa042019-12-16 19:21:02 +01001433 if (!skb || !gap)
Tuong Lien91959482019-04-04 11:09:51 +07001434 goto exit;
1435
1436 expect = buf_seqno(skb);
1437 skb_queue_walk(&l->deferdq, skb) {
1438 seqno = buf_seqno(skb);
1439 if (unlikely(more(seqno, expect))) {
1440 ga->gacks[n].ack = htons(expect - 1);
1441 ga->gacks[n].gap = htons(seqno - expect);
1442 if (++n >= MAX_GAP_ACK_BLKS) {
1443 pr_info_ratelimited("Too few Gap ACK blocks!\n");
1444 goto exit;
1445 }
1446 } else if (unlikely(less(seqno, expect))) {
1447 pr_warn("Unexpected skb in deferdq!\n");
1448 continue;
1449 }
1450 expect = seqno + 1;
1451 }
1452
1453 /* last block */
1454 ga->gacks[n].ack = htons(seqno);
1455 ga->gacks[n].gap = 0;
1456 n++;
1457
1458exit:
1459 len = tipc_gap_ack_blks_sz(n);
1460 ga->len = htons(len);
1461 ga->gack_cnt = n;
1462 return len;
1463}
1464
1465/* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
1466 * acked packets, also doing retransmissions if
1467 * gaps found
1468 * @l: tipc link with transmq queue to be advanced
1469 * @acked: seqno of last packet acked by peer without any gaps before
1470 * @gap: # of gap packets
1471 * @ga: buffer pointer to Gap ACK blocks from peer
1472 * @xmitq: queue for accumulating the retransmitted packets if any
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001473 *
1474 * In case of a repeated retransmit failures, the call will return shortly
1475 * with a returned code (e.g. TIPC_LINK_DOWN_EVT)
Tuong Lien91959482019-04-04 11:09:51 +07001476 */
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001477static int tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
1478 struct tipc_gap_ack_blks *ga,
1479 struct sk_buff_head *xmitq)
Tuong Lien91959482019-04-04 11:09:51 +07001480{
1481 struct sk_buff *skb, *_skb, *tmp;
1482 struct tipc_msg *hdr;
1483 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001484 bool retransmitted = false;
Tuong Lien91959482019-04-04 11:09:51 +07001485 u16 ack = l->rcv_nxt - 1;
Tuong Lien71204232019-08-15 10:24:08 +07001486 bool passed = false;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001487 u16 released = 0;
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001488 u16 seqno, n = 0;
1489 int rc = 0;
1490
Tuong Lien91959482019-04-04 11:09:51 +07001491 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1492 seqno = buf_seqno(skb);
1493
1494next_gap_ack:
1495 if (less_eq(seqno, acked)) {
1496 /* release skb */
1497 __skb_unlink(skb, &l->transmq);
1498 kfree_skb(skb);
Jon Maloy16ad3f42019-12-10 00:52:46 +01001499 released++;
Tuong Lien91959482019-04-04 11:09:51 +07001500 } else if (less_eq(seqno, acked + gap)) {
Tuong Lien71204232019-08-15 10:24:08 +07001501 /* First, check if repeated retrans failures occurs? */
1502 if (!passed && link_retransmit_failure(l, l, &rc))
1503 return rc;
1504 passed = true;
1505
1506 /* retransmit skb if unrestricted*/
Tuong Lien382f5982019-04-04 11:09:52 +07001507 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1508 continue;
1509 TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
Tuong Lienfc1b6d62019-11-08 12:05:11 +07001510 _skb = pskb_copy(skb, GFP_ATOMIC);
Tuong Lien91959482019-04-04 11:09:51 +07001511 if (!_skb)
1512 continue;
1513 hdr = buf_msg(_skb);
1514 msg_set_ack(hdr, ack);
1515 msg_set_bcast_ack(hdr, bc_ack);
1516 _skb->priority = TC_PRIO_CONTROL;
1517 __skb_queue_tail(xmitq, _skb);
1518 l->stats.retransmitted++;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001519 retransmitted = true;
Tuong Lien71204232019-08-15 10:24:08 +07001520 /* Increase actual retrans counter & mark first time */
1521 if (!TIPC_SKB_CB(skb)->retr_cnt++)
1522 TIPC_SKB_CB(skb)->retr_stamp = jiffies;
Tuong Lien91959482019-04-04 11:09:51 +07001523 } else {
1524 /* retry with Gap ACK blocks if any */
1525 if (!ga || n >= ga->gack_cnt)
1526 break;
1527 acked = ntohs(ga->gacks[n].ack);
1528 gap = ntohs(ga->gacks[n].gap);
1529 n++;
1530 goto next_gap_ack;
1531 }
1532 }
Jon Maloy16ad3f42019-12-10 00:52:46 +01001533 if (released || retransmitted)
1534 tipc_link_update_cwin(l, released, retransmitted);
1535 if (released)
1536 tipc_link_advance_backlog(l, xmitq);
Tuong Lien6a6b5c82019-06-17 12:15:42 +07001537 return 0;
Tuong Lien91959482019-04-04 11:09:51 +07001538}
1539
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001540/* tipc_link_build_state_msg: prepare link state message for transmission
Jon Paul Maloy52666982015-10-22 08:51:41 -04001541 *
1542 * Note that sending of broadcast ack is coordinated among nodes, to reduce
1543 * risk of ack storms towards the sender
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001544 */
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001545int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001546{
Jon Paul Maloy52666982015-10-22 08:51:41 -04001547 if (!l)
1548 return 0;
1549
1550 /* Broadcast ACK must be sent via a unicast link => defer to caller */
1551 if (link_is_bc_rcvlink(l)) {
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001552 if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001553 return 0;
1554 l->rcv_unacked = 0;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001555
1556 /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
1557 l->snd_nxt = l->rcv_nxt;
1558 return TIPC_LINK_SND_STATE;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001559 }
Jon Paul Maloy52666982015-10-22 08:51:41 -04001560 /* Unicast ACK */
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001561 l->rcv_unacked = 0;
1562 l->stats.sent_acks++;
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001563 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001564 return 0;
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001565}
1566
Jon Paul Maloy282b3a02015-10-15 14:52:45 -04001567/* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
1568 */
1569void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1570{
1571 int mtyp = RESET_MSG;
Jon Paul Maloy634696b2016-04-15 13:33:03 -04001572 struct sk_buff *skb;
Jon Paul Maloy282b3a02015-10-15 14:52:45 -04001573
1574 if (l->state == LINK_ESTABLISHING)
1575 mtyp = ACTIVATE_MSG;
1576
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001577 tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
Jon Paul Maloy634696b2016-04-15 13:33:03 -04001578
1579 /* Inform peer that this endpoint is going down if applicable */
1580 skb = skb_peek_tail(xmitq);
1581 if (skb && (l->state == LINK_RESET))
1582 msg_set_peer_stopping(buf_msg(skb), 1);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -04001583}
1584
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001585/* tipc_link_build_nack_msg: prepare link nack message for transmission
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001586 * Note that sending of broadcast NACK is coordinated among nodes, to
1587 * reduce the risk of NACK storms towards the sender
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001588 */
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001589static int tipc_link_build_nack_msg(struct tipc_link *l,
1590 struct sk_buff_head *xmitq)
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001591{
1592 u32 def_cnt = ++l->stats.deferred_recv;
Jon Maloy02288242019-12-10 00:52:44 +01001593 struct sk_buff_head *dfq = &l->deferdq;
1594 u32 defq_len = skb_queue_len(dfq);
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001595 int match1, match2;
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001596
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001597 if (link_is_bc_rcvlink(l)) {
1598 match1 = def_cnt & 0xf;
1599 match2 = tipc_own_addr(l->net) & 0xf;
1600 if (match1 == match2)
1601 return TIPC_LINK_SND_STATE;
1602 return 0;
1603 }
Jon Paul Maloy52666982015-10-22 08:51:41 -04001604
Jon Maloy02288242019-12-10 00:52:44 +01001605 if (defq_len >= 3 && !((defq_len - 3) % 16)) {
1606 u16 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1607
1608 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0,
1609 rcvgap, 0, 0, xmitq);
1610 }
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001611 return 0;
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001612}
1613
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001614/* tipc_link_rcv - process TIPC packets/messages arriving from off-node
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001615 * @l: the link that should handle the message
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001616 * @skb: TIPC packet
1617 * @xmitq: queue to place packets to be sent after this call
1618 */
1619int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1620 struct sk_buff_head *xmitq)
1621{
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001622 struct sk_buff_head *defq = &l->deferdq;
Tuong Lien382f5982019-04-04 11:09:52 +07001623 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001624 u16 seqno, rcv_nxt, win_lim;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001625 int released = 0;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001626 int rc = 0;
1627
Tuong Lien382f5982019-04-04 11:09:52 +07001628 /* Verify and update link state */
1629 if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
1630 return tipc_link_proto_rcv(l, skb, xmitq);
1631
1632 /* Don't send probe at next timeout expiration */
1633 l->silent_intv_cnt = 0;
1634
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001635 do {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001636 hdr = buf_msg(skb);
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001637 seqno = msg_seqno(hdr);
1638 rcv_nxt = l->rcv_nxt;
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001639 win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001640
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001641 if (unlikely(!link_is_up(l))) {
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001642 if (l->state == LINK_ESTABLISHING)
1643 rc = TIPC_LINK_UP_EVT;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001644 kfree_skb(skb);
1645 break;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001646 }
1647
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001648 /* Drop if outside receive window */
1649 if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
1650 l->stats.duplicates++;
Jon Maloy16ad3f42019-12-10 00:52:46 +01001651 kfree_skb(skb);
1652 break;
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001653 }
Jon Maloy16ad3f42019-12-10 00:52:46 +01001654 released += tipc_link_release_pkts(l, msg_ack(hdr));
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001655
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001656 /* Defer delivery if sequence gap */
1657 if (unlikely(seqno != rcv_nxt)) {
Jon Paul Maloy8306f992015-10-15 14:52:43 -04001658 __tipc_skb_queue_sorted(defq, seqno, skb);
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001659 rc |= tipc_link_build_nack_msg(l, xmitq);
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001660 break;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001661 }
1662
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001663 /* Deliver packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001664 l->rcv_nxt++;
Jon Paul Maloy95901122016-11-25 10:35:02 -05001665 l->stats.recv_pkts++;
Tuong Lien58ee86b2019-04-04 11:09:53 +07001666
1667 if (unlikely(msg_user(hdr) == TUNNEL_PROTOCOL))
1668 rc |= tipc_link_tnl_rcv(l, skb, l->inputq);
1669 else if (!tipc_data_input(l, skb, l->inputq))
1670 rc |= tipc_link_input(l, skb, l->inputq, &l->reasm_buf);
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001671 if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001672 rc |= tipc_link_build_state_msg(l, xmitq);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001673 if (unlikely(rc & ~TIPC_LINK_SND_STATE))
Jon Paul Maloy52666982015-10-22 08:51:41 -04001674 break;
Tuong Lien382f5982019-04-04 11:09:52 +07001675 } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001676
Jon Maloy16ad3f42019-12-10 00:52:46 +01001677 /* Forward queues and wake up waiting users */
1678 if (released) {
1679 tipc_link_update_cwin(l, released, 0);
1680 tipc_link_advance_backlog(l, xmitq);
1681 if (unlikely(!skb_queue_empty(&l->wakeupq)))
1682 link_prepare_wakeup(l);
1683 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001684 return rc;
1685}
1686
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001687static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001688 bool probe_reply, u16 rcvgap,
1689 int tolerance, int priority,
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001690 struct sk_buff_head *xmitq)
1691{
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001692 struct tipc_link *bcl = l->bc_rcvlink;
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001693 struct sk_buff *skb;
1694 struct tipc_msg *hdr;
1695 struct sk_buff_head *dfq = &l->deferdq;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001696 bool node_up = link_is_up(bcl);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001697 struct tipc_mon_state *mstate = &l->mon_state;
1698 int dlen = 0;
1699 void *data;
Tuong Lien91959482019-04-04 11:09:51 +07001700 u16 glen = 0;
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001701
1702 /* Don't send protocol message during reset or link failover */
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001703 if (tipc_link_is_blocked(l))
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001704 return;
1705
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001706 if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
1707 return;
1708
Jon Maloy02288242019-12-10 00:52:44 +01001709 if ((probe || probe_reply) && !skb_queue_empty(dfq))
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001710 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1711
1712 skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
Tuong Lien91959482019-04-04 11:09:51 +07001713 tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
1714 l->addr, tipc_own_addr(l->net), 0, 0, 0);
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001715 if (!skb)
1716 return;
1717
1718 hdr = buf_msg(skb);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001719 data = msg_data(hdr);
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001720 msg_set_session(hdr, l->session);
1721 msg_set_bearer_id(hdr, l->bearer_id);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001722 msg_set_net_plane(hdr, l->net_plane);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001723 msg_set_next_sent(hdr, l->snd_nxt);
1724 msg_set_ack(hdr, l->rcv_nxt - 1);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001725 msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001726 msg_set_bc_ack_invalid(hdr, !node_up);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001727 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001728 msg_set_link_tolerance(hdr, tolerance);
1729 msg_set_linkprio(hdr, priority);
1730 msg_set_redundant_link(hdr, node_up);
1731 msg_set_seq_gap(hdr, 0);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001732 msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001733
1734 if (mtyp == STATE_MSG) {
Jon Maloy9012de52018-07-10 01:07:35 +02001735 if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
1736 msg_set_seqno(hdr, l->snd_nxt_state++);
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001737 msg_set_seq_gap(hdr, rcvgap);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001738 msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001739 msg_set_probe(hdr, probe);
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001740 msg_set_is_keepalive(hdr, probe || probe_reply);
Tuong Lien91959482019-04-04 11:09:51 +07001741 if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
Jon Maloyb7ffa042019-12-16 19:21:02 +01001742 glen = tipc_build_gap_ack_blks(l, data, rcvgap);
Tuong Lien91959482019-04-04 11:09:51 +07001743 tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
1744 msg_set_size(hdr, INT_H_SIZE + glen + dlen);
1745 skb_trim(skb, INT_H_SIZE + glen + dlen);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001746 l->stats.sent_states++;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001747 l->rcv_unacked = 0;
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001748 } else {
1749 /* RESET_MSG or ACTIVATE_MSG */
Tuong Lien91986ee2019-02-11 13:29:43 +07001750 if (mtyp == ACTIVATE_MSG) {
1751 msg_set_dest_session_valid(hdr, 1);
1752 msg_set_dest_session(hdr, l->peer_session);
1753 }
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001754 msg_set_max_pkt(hdr, l->advertised_mtu);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001755 strcpy(data, l->if_name);
1756 msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
1757 skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001758 }
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001759 if (probe)
1760 l->stats.sent_probes++;
1761 if (rcvgap)
1762 l->stats.sent_nacks++;
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001763 skb->priority = TC_PRIO_CONTROL;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001764 __skb_queue_tail(xmitq, skb);
Tuong Lien26574db2018-12-19 09:17:57 +07001765 trace_tipc_proto_build(skb, false, l->name);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001766}
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767
LUU Duc Canhc140eb12018-09-26 21:00:54 +02001768void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
1769 struct sk_buff_head *xmitq)
1770{
1771 u32 onode = tipc_own_addr(l->net);
1772 struct tipc_msg *hdr, *ihdr;
1773 struct sk_buff_head tnlq;
1774 struct sk_buff *skb;
1775 u32 dnode = l->addr;
1776
Jon Maloye654f9f2019-08-15 16:42:50 +02001777 __skb_queue_head_init(&tnlq);
LUU Duc Canhc140eb12018-09-26 21:00:54 +02001778 skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
1779 INT_H_SIZE, BASIC_H_SIZE,
1780 dnode, onode, 0, 0, 0);
1781 if (!skb) {
1782 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1783 return;
1784 }
1785
1786 hdr = buf_msg(skb);
1787 msg_set_msgcnt(hdr, 1);
1788 msg_set_bearer_id(hdr, l->peer_bearer_id);
1789
1790 ihdr = (struct tipc_msg *)msg_data(hdr);
1791 tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1792 BASIC_H_SIZE, dnode);
1793 msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
1794 __skb_queue_tail(&tnlq, skb);
1795 tipc_link_xmit(l, &tnlq, xmitq);
1796}
1797
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001798/* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001799 * with contents of the link's transmit and backlog queues.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001801void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1802 int mtyp, struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803{
Tuong Lien58ee86b2019-04-04 11:09:53 +07001804 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001805 struct sk_buff *skb, *tnlskb;
1806 struct tipc_msg *hdr, tnlhdr;
1807 struct sk_buff_head *queue = &l->transmq;
Tuong Lien2320bcd2019-07-24 08:56:12 +07001808 struct sk_buff_head tmpxq, tnlq, frags;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001809 u16 pktlen, pktcnt, seqno = l->snd_nxt;
Tuong Lien2320bcd2019-07-24 08:56:12 +07001810 bool pktcnt_need_update = false;
Tuong Lien4929a932019-07-24 08:56:11 +07001811 u16 syncpt;
Tuong Lien2320bcd2019-07-24 08:56:12 +07001812 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001814 if (!tnl)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 return;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001816
Jon Maloye654f9f2019-08-15 16:42:50 +02001817 __skb_queue_head_init(&tnlq);
Tuong Lien4929a932019-07-24 08:56:11 +07001818 /* Link Synching:
1819 * From now on, send only one single ("dummy") SYNCH message
1820 * to peer. The SYNCH message does not contain any data, just
1821 * a header conveying the synch point to the peer.
1822 */
1823 if (mtyp == SYNCH_MSG && (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1824 tnlskb = tipc_msg_create(TUNNEL_PROTOCOL, SYNCH_MSG,
1825 INT_H_SIZE, 0, l->addr,
1826 tipc_own_addr(l->net),
1827 0, 0, 0);
1828 if (!tnlskb) {
1829 pr_warn("%sunable to create dummy SYNCH_MSG\n",
1830 link_co_err);
1831 return;
1832 }
1833
1834 hdr = buf_msg(tnlskb);
1835 syncpt = l->snd_nxt + skb_queue_len(&l->backlogq) - 1;
1836 msg_set_syncpt(hdr, syncpt);
1837 msg_set_bearer_id(hdr, l->peer_bearer_id);
1838 __skb_queue_tail(&tnlq, tnlskb);
1839 tipc_link_xmit(tnl, &tnlq, xmitq);
1840 return;
1841 }
1842
Tuong Liend0d605c2019-11-06 18:12:17 +07001843 __skb_queue_head_init(&tmpxq);
1844 __skb_queue_head_init(&frags);
1845 /* At least one packet required for safe algorithm => add dummy */
1846 skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1847 BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
1848 0, 0, TIPC_ERR_NO_PORT);
1849 if (!skb) {
1850 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1851 return;
1852 }
1853 __skb_queue_tail(&tnlq, skb);
1854 tipc_link_xmit(l, &tnlq, &tmpxq);
1855 __skb_queue_purge(&tmpxq);
1856
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001857 /* Initialize reusable tunnel packet header */
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001858 tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001859 mtyp, INT_H_SIZE, l->addr);
Tuong Lien58ee86b2019-04-04 11:09:53 +07001860 if (mtyp == SYNCH_MSG)
1861 pktcnt = l->snd_nxt - buf_seqno(skb_peek(&l->transmq));
1862 else
1863 pktcnt = skb_queue_len(&l->transmq);
1864 pktcnt += skb_queue_len(&l->backlogq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001865 msg_set_msgcnt(&tnlhdr, pktcnt);
1866 msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1867tnl:
1868 /* Wrap each packet into a tunnel packet */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001869 skb_queue_walk(queue, skb) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001870 hdr = buf_msg(skb);
1871 if (queue == &l->backlogq)
1872 msg_set_seqno(hdr, seqno++);
1873 pktlen = msg_size(hdr);
Tuong Lien2320bcd2019-07-24 08:56:12 +07001874
1875 /* Tunnel link MTU is not large enough? This could be
1876 * due to:
1877 * 1) Link MTU has just changed or set differently;
1878 * 2) Or FAILOVER on the top of a SYNCH message
1879 *
1880 * The 2nd case should not happen if peer supports
1881 * TIPC_TUNNEL_ENHANCED
1882 */
1883 if (pktlen > tnl->mtu - INT_H_SIZE) {
1884 if (mtyp == FAILOVER_MSG &&
1885 (tnl->peer_caps & TIPC_TUNNEL_ENHANCED)) {
1886 rc = tipc_msg_fragment(skb, &tnlhdr, tnl->mtu,
1887 &frags);
1888 if (rc) {
1889 pr_warn("%sunable to frag msg: rc %d\n",
1890 link_co_err, rc);
1891 return;
1892 }
1893 pktcnt += skb_queue_len(&frags) - 1;
1894 pktcnt_need_update = true;
1895 skb_queue_splice_tail_init(&frags, &tnlq);
1896 continue;
1897 }
1898 /* Unluckily, peer doesn't have TIPC_TUNNEL_ENHANCED
1899 * => Just warn it and return!
1900 */
1901 pr_warn_ratelimited("%stoo large msg <%d, %d>: %d!\n",
1902 link_co_err, msg_user(hdr),
1903 msg_type(hdr), msg_size(hdr));
1904 return;
1905 }
1906
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001907 msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
Parthasarathy Bhuvaragan57d5f642017-01-13 15:46:25 +01001908 tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001909 if (!tnlskb) {
1910 pr_warn("%sunable to send packet\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 return;
1912 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001913 skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
1914 skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
1915 __skb_queue_tail(&tnlq, tnlskb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001917 if (queue != &l->backlogq) {
1918 queue = &l->backlogq;
1919 goto tnl;
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -04001920 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921
Tuong Lien2320bcd2019-07-24 08:56:12 +07001922 if (pktcnt_need_update)
1923 skb_queue_walk(&tnlq, skb) {
1924 hdr = buf_msg(skb);
1925 msg_set_msgcnt(hdr, pktcnt);
1926 }
1927
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001928 tipc_link_xmit(tnl, &tnlq, xmitq);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001929
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001930 if (mtyp == FAILOVER_MSG) {
1931 tnl->drop_point = l->rcv_nxt;
1932 tnl->failover_reasm_skb = l->reasm_buf;
1933 l->reasm_buf = NULL;
Tuong Lien58ee86b2019-04-04 11:09:53 +07001934
1935 /* Failover the link's deferdq */
1936 if (unlikely(!skb_queue_empty(fdefq))) {
1937 pr_warn("Link failover deferdq not empty: %d!\n",
1938 skb_queue_len(fdefq));
1939 __skb_queue_purge(fdefq);
1940 }
1941 skb_queue_splice_init(&l->deferdq, fdefq);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001942 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001943}
1944
Tuong Lienc0b14a082019-05-02 17:23:23 +07001945/**
1946 * tipc_link_failover_prepare() - prepare tnl for link failover
1947 *
1948 * This is a special version of the precursor - tipc_link_tnl_prepare(),
1949 * see the tipc_node_link_failover() for details
1950 *
1951 * @l: failover link
1952 * @tnl: tunnel link
1953 * @xmitq: queue for messages to be xmited
1954 */
1955void tipc_link_failover_prepare(struct tipc_link *l, struct tipc_link *tnl,
1956 struct sk_buff_head *xmitq)
1957{
1958 struct sk_buff_head *fdefq = &tnl->failover_deferdq;
1959
1960 tipc_link_create_dummy_tnl_msg(tnl, xmitq);
1961
Geert Uytterhoeven8ebed8ae2019-10-24 17:30:43 +02001962 /* This failover link endpoint was never established before,
Tuong Lienc0b14a082019-05-02 17:23:23 +07001963 * so it has not received anything from peer.
1964 * Otherwise, it must be a normal failover situation or the
1965 * node has entered SELF_DOWN_PEER_LEAVING and both peer nodes
1966 * would have to start over from scratch instead.
1967 */
Tuong Lienc0b14a082019-05-02 17:23:23 +07001968 tnl->drop_point = 1;
1969 tnl->failover_reasm_skb = NULL;
1970
1971 /* Initiate the link's failover deferdq */
1972 if (unlikely(!skb_queue_empty(fdefq))) {
1973 pr_warn("Link failover deferdq not empty: %d!\n",
1974 skb_queue_len(fdefq));
1975 __skb_queue_purge(fdefq);
1976 }
1977}
1978
Jon Maloy7ea817f2018-07-10 01:07:36 +02001979/* tipc_link_validate_msg(): validate message against current link state
1980 * Returns true if message should be accepted, otherwise false
1981 */
1982bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
1983{
1984 u16 curr_session = l->peer_session;
1985 u16 session = msg_session(hdr);
1986 int mtyp = msg_type(hdr);
1987
1988 if (msg_user(hdr) != LINK_PROTOCOL)
1989 return true;
1990
1991 switch (mtyp) {
1992 case RESET_MSG:
1993 if (!l->in_session)
1994 return true;
1995 /* Accept only RESET with new session number */
1996 return more(session, curr_session);
1997 case ACTIVATE_MSG:
1998 if (!l->in_session)
1999 return true;
2000 /* Accept only ACTIVATE with new or current session number */
2001 return !less(session, curr_session);
2002 case STATE_MSG:
2003 /* Accept only STATE with current session number */
2004 if (!l->in_session)
2005 return false;
2006 if (session != curr_session)
2007 return false;
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02002008 /* Extra sanity check */
2009 if (!link_is_up(l) && msg_ack(hdr))
2010 return false;
Jon Maloy7ea817f2018-07-10 01:07:36 +02002011 if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
2012 return true;
2013 /* Accept only STATE with new sequence number */
2014 return !less(msg_seqno(hdr), l->rcv_nxt_state);
2015 default:
2016 return false;
2017 }
2018}
2019
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002020/* tipc_link_proto_rcv(): receive link level protocol message :
2021 * Note that network plane id propagates through the network, and may
2022 * change at any time. The node with lowest numerical id determines
2023 * network plane
2024 */
2025static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
2026 struct sk_buff_head *xmitq)
2027{
2028 struct tipc_msg *hdr = buf_msg(skb);
Tuong Lien91959482019-04-04 11:09:51 +07002029 struct tipc_gap_ack_blks *ga = NULL;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002030 u16 rcvgap = 0;
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04002031 u16 ack = msg_ack(hdr);
2032 u16 gap = msg_seq_gap(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002033 u16 peers_snd_nxt = msg_next_sent(hdr);
2034 u16 peers_tol = msg_link_tolerance(hdr);
2035 u16 peers_prio = msg_linkprio(hdr);
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04002036 u16 rcv_nxt = l->rcv_nxt;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04002037 u16 dlen = msg_data_sz(hdr);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04002038 int mtyp = msg_type(hdr);
Jon Maloy8d6e79d2017-11-08 09:59:26 +01002039 bool reply = msg_probe(hdr);
Tuong Lien91959482019-04-04 11:09:51 +07002040 u16 glen = 0;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04002041 void *data;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002042 char *if_name;
2043 int rc = 0;
2044
Tuong Lien26574db2018-12-19 09:17:57 +07002045 trace_tipc_proto_rcv(skb, false, l->name);
Jon Paul Maloy52666982015-10-22 08:51:41 -04002046 if (tipc_link_is_blocked(l) || !xmitq)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002047 goto exit;
2048
Jon Paul Maloye74a3862016-03-03 14:23:21 -05002049 if (tipc_own_addr(l->net) > msg_prevnode(hdr))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002050 l->net_plane = msg_net_plane(hdr);
2051
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04002052 skb_linearize(skb);
2053 hdr = buf_msg(skb);
2054 data = msg_data(hdr);
2055
Tuong Lien26574db2018-12-19 09:17:57 +07002056 if (!tipc_link_validate_msg(l, hdr)) {
2057 trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
2058 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
Jon Maloy7ea817f2018-07-10 01:07:36 +02002059 goto exit;
Tuong Lien26574db2018-12-19 09:17:57 +07002060 }
Jon Maloy7ea817f2018-07-10 01:07:36 +02002061
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04002062 switch (mtyp) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002063 case RESET_MSG:
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002064 case ACTIVATE_MSG:
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002065 /* Complete own link name with peer's interface name */
2066 if_name = strrchr(l->name, ':') + 1;
2067 if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
2068 break;
2069 if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
2070 break;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04002071 strncpy(if_name, data, TIPC_MAX_IF_NAME);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002072
2073 /* Update own tolerance if peer indicates a non-zero value */
Jon Maloy047491e2018-10-10 17:34:01 +02002074 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002075 l->tolerance = peers_tol;
Jon Maloy047491e2018-10-10 17:34:01 +02002076 l->bc_rcvlink->tolerance = peers_tol;
2077 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002078 /* Update own priority if peer's priority is higher */
2079 if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
2080 l->priority = peers_prio;
2081
Jon Maloy7ab412d2018-11-10 17:30:24 -05002082 /* If peer is going down we want full re-establish cycle */
2083 if (msg_peer_stopping(hdr)) {
Jon Paul Maloy634696b2016-04-15 13:33:03 -04002084 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
Jon Maloy7ab412d2018-11-10 17:30:24 -05002085 break;
2086 }
Tuong Lien91986ee2019-02-11 13:29:43 +07002087
2088 /* If this endpoint was re-created while peer was ESTABLISHING
2089 * it doesn't know current session number. Force re-synch.
2090 */
2091 if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
2092 l->session != msg_dest_session(hdr)) {
2093 if (less(l->session, msg_dest_session(hdr)))
2094 l->session = msg_dest_session(hdr) + 1;
2095 break;
2096 }
2097
Jon Maloy7ab412d2018-11-10 17:30:24 -05002098 /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
2099 if (mtyp == RESET_MSG || !link_is_up(l))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04002100 rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
2101
2102 /* ACTIVATE_MSG takes up link if it was already locally reset */
Jon Maloy7ab412d2018-11-10 17:30:24 -05002103 if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04002104 rc = TIPC_LINK_UP_EVT;
2105
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002106 l->peer_session = msg_session(hdr);
Jon Maloy7ea817f2018-07-10 01:07:36 +02002107 l->in_session = true;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002108 l->peer_bearer_id = msg_bearer_id(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002109 if (l->mtu > msg_max_pkt(hdr))
2110 l->mtu = msg_max_pkt(hdr);
2111 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04002112
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002113 case STATE_MSG:
Jon Maloy9012de52018-07-10 01:07:35 +02002114 l->rcv_nxt_state = msg_seqno(hdr) + 1;
2115
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002116 /* Update own tolerance if peer indicates a non-zero value */
Jon Maloy047491e2018-10-10 17:34:01 +02002117 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002118 l->tolerance = peers_tol;
Jon Maloy047491e2018-10-10 17:34:01 +02002119 l->bc_rcvlink->tolerance = peers_tol;
2120 }
Jon Paul Maloyf7967552016-11-23 21:05:26 -05002121 /* Update own prio if peer indicates a different value */
2122 if ((peers_prio != l->priority) &&
2123 in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
Richard Alpe81729812016-02-01 08:19:57 +01002124 l->priority = peers_prio;
2125 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
2126 }
2127
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002128 l->silent_intv_cnt = 0;
2129 l->stats.recv_states++;
2130 if (msg_probe(hdr))
2131 l->stats.recv_probes++;
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04002132
2133 if (!link_is_up(l)) {
2134 if (l->state == LINK_ESTABLISHING)
2135 rc = TIPC_LINK_UP_EVT;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002136 break;
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04002137 }
Tuong Lien91959482019-04-04 11:09:51 +07002138
2139 /* Receive Gap ACK blocks from peer if any */
2140 if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
2141 ga = (struct tipc_gap_ack_blks *)data;
2142 glen = ntohs(ga->len);
2143 /* sanity check: if failed, ignore Gap ACK blocks */
2144 if (glen != tipc_gap_ack_blks_sz(ga->gack_cnt))
2145 ga = NULL;
2146 }
2147
2148 tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04002149 &l->mon_state, l->bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002150
Jon Paul Maloy662921c2015-07-30 18:24:21 -04002151 /* Send NACK if peer has sent pkts we haven't received yet */
Jon Maloyd3b09992019-12-10 00:52:45 +01002152 if ((reply || msg_is_keepalive(hdr)) &&
2153 more(peers_snd_nxt, rcv_nxt) &&
2154 !tipc_link_is_synching(l) &&
2155 skb_queue_empty(&l->deferdq))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002156 rcvgap = peers_snd_nxt - l->rcv_nxt;
Jon Maloy8d6e79d2017-11-08 09:59:26 +01002157 if (rcvgap || reply)
2158 tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
2159 rcvgap, 0, 0, xmitq);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002160
Jon Maloy16ad3f42019-12-10 00:52:46 +01002161 rc |= tipc_link_advance_transmq(l, ack, gap, ga, xmitq);
Tuong Lien91959482019-04-04 11:09:51 +07002162 if (gap)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002163 l->stats.recv_nacks++;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04002164 if (unlikely(!skb_queue_empty(&l->wakeupq)))
2165 link_prepare_wakeup(l);
2166 }
2167exit:
2168 kfree_skb(skb);
2169 return rc;
2170}
2171
Jon Paul Maloy52666982015-10-22 08:51:41 -04002172/* tipc_link_build_bc_proto_msg() - create broadcast protocol message
2173 */
2174static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
2175 u16 peers_snd_nxt,
2176 struct sk_buff_head *xmitq)
2177{
2178 struct sk_buff *skb;
2179 struct tipc_msg *hdr;
2180 struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
2181 u16 ack = l->rcv_nxt - 1;
2182 u16 gap_to = peers_snd_nxt - 1;
2183
2184 skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
Jon Paul Maloye74a3862016-03-03 14:23:21 -05002185 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
Jon Paul Maloy52666982015-10-22 08:51:41 -04002186 if (!skb)
2187 return false;
2188 hdr = buf_msg(skb);
2189 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
2190 msg_set_bcast_ack(hdr, ack);
2191 msg_set_bcgap_after(hdr, ack);
2192 if (dfrd_skb)
2193 gap_to = buf_seqno(dfrd_skb) - 1;
2194 msg_set_bcgap_to(hdr, gap_to);
2195 msg_set_non_seq(hdr, bcast);
2196 __skb_queue_tail(xmitq, skb);
2197 return true;
2198}
2199
2200/* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
2201 *
2202 * Give a newly added peer node the sequence number where it should
2203 * start receiving and acking broadcast packets.
2204 */
Wu Fengguang742e0382015-10-24 22:56:01 +08002205static void tipc_link_build_bc_init_msg(struct tipc_link *l,
2206 struct sk_buff_head *xmitq)
Jon Paul Maloy52666982015-10-22 08:51:41 -04002207{
2208 struct sk_buff_head list;
2209
2210 __skb_queue_head_init(&list);
2211 if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
2212 return;
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04002213 msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
Jon Paul Maloy52666982015-10-22 08:51:41 -04002214 tipc_link_xmit(l, &list, xmitq);
2215}
2216
2217/* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
2218 */
2219void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
2220{
2221 int mtyp = msg_type(hdr);
2222 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
2223
2224 if (link_is_up(l))
2225 return;
2226
2227 if (msg_user(hdr) == BCAST_PROTOCOL) {
2228 l->rcv_nxt = peers_snd_nxt;
2229 l->state = LINK_ESTABLISHED;
2230 return;
2231 }
2232
2233 if (l->peer_caps & TIPC_BCAST_SYNCH)
2234 return;
2235
2236 if (msg_peer_node_is_up(hdr))
2237 return;
2238
2239 /* Compatibility: accept older, less safe initial synch data */
2240 if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
2241 l->rcv_nxt = peers_snd_nxt;
2242}
2243
2244/* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
2245 */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002246int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
2247 struct sk_buff_head *xmitq)
Jon Paul Maloy52666982015-10-22 08:51:41 -04002248{
Jon Paul Maloy7c4a54b2016-09-01 13:52:50 -04002249 struct tipc_link *snd_l = l->bc_sndlink;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002250 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002251 u16 from = msg_bcast_ack(hdr) + 1;
2252 u16 to = from + msg_bc_gap(hdr) - 1;
2253 int rc = 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002254
2255 if (!link_is_up(l))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002256 return rc;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002257
2258 if (!msg_peer_node_is_up(hdr))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002259 return rc;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002260
Jon Paul Maloy2d18ac42016-07-11 16:08:35 -04002261 /* Open when peer ackowledges our bcast init msg (pkt #1) */
2262 if (msg_ack(hdr))
2263 l->bc_peer_is_up = true;
2264
2265 if (!l->bc_peer_is_up)
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002266 return rc;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002267
Jon Paul Maloy7c4a54b2016-09-01 13:52:50 -04002268 l->stats.recv_nacks++;
2269
Jon Paul Maloy52666982015-10-22 08:51:41 -04002270 /* Ignore if peers_snd_nxt goes beyond receive window */
2271 if (more(peers_snd_nxt, l->rcv_nxt + l->window))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002272 return rc;
2273
Tuong Lien6a6b5c82019-06-17 12:15:42 +07002274 rc = tipc_link_bc_retrans(snd_l, l, from, to, xmitq);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002275
2276 l->snd_nxt = peers_snd_nxt;
2277 if (link_bc_rcv_gap(l))
2278 rc |= TIPC_LINK_SND_STATE;
2279
2280 /* Return now if sender supports nack via STATE messages */
2281 if (l->peer_caps & TIPC_BCAST_STATE_NACK)
2282 return rc;
2283
2284 /* Otherwise, be backwards compatible */
Jon Paul Maloy52666982015-10-22 08:51:41 -04002285
2286 if (!more(peers_snd_nxt, l->rcv_nxt)) {
2287 l->nack_state = BC_NACK_SND_CONDITIONAL;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002288 return 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002289 }
2290
2291 /* Don't NACK if one was recently sent or peeked */
2292 if (l->nack_state == BC_NACK_SND_SUPPRESS) {
2293 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002294 return 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002295 }
2296
2297 /* Conditionally delay NACK sending until next synch rcv */
2298 if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
2299 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
2300 if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002301 return 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002302 }
2303
2304 /* Send NACK now but suppress next one */
2305 tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
2306 l->nack_state = BC_NACK_SND_SUPPRESS;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002307 return 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04002308}
2309
2310void tipc_link_bc_ack_rcv(struct tipc_link *l, u16 acked,
2311 struct sk_buff_head *xmitq)
2312{
2313 struct sk_buff *skb, *tmp;
2314 struct tipc_link *snd_l = l->bc_sndlink;
2315
2316 if (!link_is_up(l) || !l->bc_peer_is_up)
2317 return;
2318
2319 if (!more(acked, l->acked))
2320 return;
2321
Tuong Lien26574db2018-12-19 09:17:57 +07002322 trace_tipc_link_bc_ack(l, l->acked, acked, &snd_l->transmq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04002323 /* Skip over packets peer has already acked */
2324 skb_queue_walk(&snd_l->transmq, skb) {
2325 if (more(buf_seqno(skb), l->acked))
2326 break;
2327 }
2328
2329 /* Update/release the packets peer is acking now */
2330 skb_queue_walk_from_safe(&snd_l->transmq, skb, tmp) {
2331 if (more(buf_seqno(skb), acked))
2332 break;
2333 if (!--TIPC_SKB_CB(skb)->ackers) {
2334 __skb_unlink(skb, &snd_l->transmq);
2335 kfree_skb(skb);
2336 }
2337 }
2338 l->acked = acked;
2339 tipc_link_advance_backlog(snd_l, xmitq);
2340 if (unlikely(!skb_queue_empty(&snd_l->wakeupq)))
2341 link_prepare_wakeup(snd_l);
2342}
2343
2344/* tipc_link_bc_nack_rcv(): receive broadcast nack message
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002345 * This function is here for backwards compatibility, since
2346 * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
Jon Paul Maloy52666982015-10-22 08:51:41 -04002347 */
2348int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
2349 struct sk_buff_head *xmitq)
2350{
2351 struct tipc_msg *hdr = buf_msg(skb);
2352 u32 dnode = msg_destnode(hdr);
2353 int mtyp = msg_type(hdr);
2354 u16 acked = msg_bcast_ack(hdr);
2355 u16 from = acked + 1;
2356 u16 to = msg_bcgap_to(hdr);
2357 u16 peers_snd_nxt = to + 1;
2358 int rc = 0;
2359
2360 kfree_skb(skb);
2361
2362 if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
2363 return 0;
2364
2365 if (mtyp != STATE_MSG)
2366 return 0;
2367
Jon Paul Maloye74a3862016-03-03 14:23:21 -05002368 if (dnode == tipc_own_addr(l->net)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04002369 tipc_link_bc_ack_rcv(l, acked, xmitq);
Tuong Lien6a6b5c82019-06-17 12:15:42 +07002370 rc = tipc_link_bc_retrans(l->bc_sndlink, l, from, to, xmitq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04002371 l->stats.recv_nacks++;
2372 return rc;
2373 }
2374
2375 /* Msg for other node => suppress own NACK at next sync if applicable */
2376 if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
2377 l->nack_state = BC_NACK_SND_SUPPRESS;
2378
2379 return 0;
2380}
2381
Jon Maloy16ad3f42019-12-10 00:52:46 +01002382void tipc_link_set_queue_limits(struct tipc_link *l, u32 min_win, u32 max_win)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002383{
Jon Maloy218527f2018-03-29 23:20:41 +02002384 int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04002385
Jon Maloy16ad3f42019-12-10 00:52:46 +01002386 l->min_win = min_win;
2387 l->ssthresh = max_win;
2388 l->max_win = max_win;
2389 l->window = min_win;
2390 l->backlog[TIPC_LOW_IMPORTANCE].limit = min_win * 2;
2391 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = min_win * 4;
2392 l->backlog[TIPC_HIGH_IMPORTANCE].limit = min_win * 6;
2393 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = min_win * 8;
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04002394 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002395}
2396
Allan Stephens5c216e12011-10-18 11:34:29 -04002397/**
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002398 * link_reset_stats - reset link statistics
Jon Paul Maloy1a906322015-11-19 14:30:47 -05002399 * @l: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +01002400 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002401void tipc_link_reset_stats(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002402{
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002403 memset(&l->stats, 0, sizeof(l->stats));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002404}
2405
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04002406static void link_print(struct tipc_link *l, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002407{
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04002408 struct sk_buff *hskb = skb_peek(&l->transmq);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04002409 u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04002410 u16 tail = l->snd_nxt - 1;
Ying Xue7a2f7d12014-04-21 10:55:46 +08002411
Jon Paul Maloy662921c2015-07-30 18:24:21 -04002412 pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04002413 pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
2414 skb_queue_len(&l->transmq), head, tail,
2415 skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002416}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002417
2418/* Parse and validate nested (link) properties valid for media, bearer and link
2419 */
2420int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2421{
2422 int err;
2423
Johannes Berg8cb08172019-04-26 14:07:28 +02002424 err = nla_parse_nested_deprecated(props, TIPC_NLA_PROP_MAX, prop,
2425 tipc_nl_prop_policy, NULL);
Richard Alpe0655f6a2014-11-20 10:29:07 +01002426 if (err)
2427 return err;
2428
2429 if (props[TIPC_NLA_PROP_PRIO]) {
2430 u32 prio;
2431
2432 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2433 if (prio > TIPC_MAX_LINK_PRI)
2434 return -EINVAL;
2435 }
2436
2437 if (props[TIPC_NLA_PROP_TOL]) {
2438 u32 tol;
2439
2440 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2441 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2442 return -EINVAL;
2443 }
2444
2445 if (props[TIPC_NLA_PROP_WIN]) {
Jon Maloy16ad3f42019-12-10 00:52:46 +01002446 u32 max_win;
Richard Alpe0655f6a2014-11-20 10:29:07 +01002447
Jon Maloy16ad3f42019-12-10 00:52:46 +01002448 max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2449 if (max_win < TIPC_DEF_LINK_WIN || max_win > TIPC_MAX_LINK_WIN)
Richard Alpe0655f6a2014-11-20 10:29:07 +01002450 return -EINVAL;
2451 }
2452
2453 return 0;
2454}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002455
Richard Alped8182802014-11-24 11:10:29 +01002456static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002457{
2458 int i;
2459 struct nlattr *stats;
2460
2461 struct nla_map {
2462 u32 key;
2463 u32 val;
2464 };
2465
2466 struct nla_map map[] = {
Jon Paul Maloy95901122016-11-25 10:35:02 -05002467 {TIPC_NLA_STATS_RX_INFO, 0},
Richard Alpe7be57fc2014-11-20 10:29:12 +01002468 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2469 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2470 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2471 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
Jon Paul Maloy95901122016-11-25 10:35:02 -05002472 {TIPC_NLA_STATS_TX_INFO, 0},
Richard Alpe7be57fc2014-11-20 10:29:12 +01002473 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2474 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2475 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2476 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2477 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2478 s->msg_length_counts : 1},
2479 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2480 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2481 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2482 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2483 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2484 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2485 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2486 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2487 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2488 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2489 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2490 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2491 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2492 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2493 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2494 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2495 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2496 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2497 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2498 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2499 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2500 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2501 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2502 };
2503
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002504 stats = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002505 if (!stats)
2506 return -EMSGSIZE;
2507
2508 for (i = 0; i < ARRAY_SIZE(map); i++)
2509 if (nla_put_u32(skb, map[i].key, map[i].val))
2510 goto msg_full;
2511
2512 nla_nest_end(skb, stats);
2513
2514 return 0;
2515msg_full:
2516 nla_nest_cancel(skb, stats);
2517
2518 return -EMSGSIZE;
2519}
2520
2521/* Caller should hold appropriate locks to protect the link */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002522int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2523 struct tipc_link *link, int nlflags)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002524{
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002525 u32 self = tipc_own_addr(net);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002526 struct nlattr *attrs;
2527 struct nlattr *prop;
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002528 void *hdr;
2529 int err;
Richard Alpe7be57fc2014-11-20 10:29:12 +01002530
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002531 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Nicolas Dichtelf2f67392015-04-28 18:33:50 +02002532 nlflags, TIPC_NL_LINK_GET);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002533 if (!hdr)
2534 return -EMSGSIZE;
2535
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002536 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002537 if (!attrs)
2538 goto msg_full;
2539
2540 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2541 goto attr_msg_full;
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002542 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002543 goto attr_msg_full;
Jon Paul Maloyed193ec2015-04-02 09:33:02 -04002544 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002545 goto attr_msg_full;
Jon Paul Maloy95901122016-11-25 10:35:02 -05002546 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002547 goto attr_msg_full;
Jon Paul Maloy95901122016-11-25 10:35:02 -05002548 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002549 goto attr_msg_full;
2550
2551 if (tipc_link_is_up(link))
2552 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2553 goto attr_msg_full;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04002554 if (link->active)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002555 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2556 goto attr_msg_full;
2557
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002558 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002559 if (!prop)
2560 goto attr_msg_full;
2561 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2562 goto prop_msg_full;
2563 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2564 goto prop_msg_full;
2565 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04002566 link->window))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002567 goto prop_msg_full;
2568 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2569 goto prop_msg_full;
2570 nla_nest_end(msg->skb, prop);
2571
2572 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2573 if (err)
2574 goto attr_msg_full;
2575
2576 nla_nest_end(msg->skb, attrs);
2577 genlmsg_end(msg->skb, hdr);
2578
2579 return 0;
2580
2581prop_msg_full:
2582 nla_nest_cancel(msg->skb, prop);
2583attr_msg_full:
2584 nla_nest_cancel(msg->skb, attrs);
2585msg_full:
2586 genlmsg_cancel(msg->skb, hdr);
2587
2588 return -EMSGSIZE;
2589}
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002590
2591static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
2592 struct tipc_stats *stats)
2593{
2594 int i;
2595 struct nlattr *nest;
2596
2597 struct nla_map {
2598 __u32 key;
2599 __u32 val;
2600 };
2601
2602 struct nla_map map[] = {
Jon Paul Maloy95901122016-11-25 10:35:02 -05002603 {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002604 {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
2605 {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
2606 {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
2607 {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
Jon Paul Maloy95901122016-11-25 10:35:02 -05002608 {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002609 {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
2610 {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
2611 {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
2612 {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
2613 {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
2614 {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
2615 {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
2616 {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
2617 {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
2618 {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
2619 {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
2620 {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
2621 {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
2622 (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
2623 };
2624
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002625 nest = nla_nest_start_noflag(skb, TIPC_NLA_LINK_STATS);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002626 if (!nest)
2627 return -EMSGSIZE;
2628
2629 for (i = 0; i < ARRAY_SIZE(map); i++)
2630 if (nla_put_u32(skb, map[i].key, map[i].val))
2631 goto msg_full;
2632
2633 nla_nest_end(skb, nest);
2634
2635 return 0;
2636msg_full:
2637 nla_nest_cancel(skb, nest);
2638
2639 return -EMSGSIZE;
2640}
2641
2642int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
2643{
2644 int err;
2645 void *hdr;
2646 struct nlattr *attrs;
2647 struct nlattr *prop;
2648 struct tipc_net *tn = net_generic(net, tipc_net_id);
Hoang Le02ec6ca2019-03-19 18:49:48 +07002649 u32 bc_mode = tipc_bcast_get_broadcast_mode(net);
2650 u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002651 struct tipc_link *bcl = tn->bcl;
2652
2653 if (!bcl)
2654 return 0;
2655
2656 tipc_bcast_lock(net);
2657
2658 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2659 NLM_F_MULTI, TIPC_NL_LINK_GET);
Insu Yunb53ce3e2016-02-17 11:47:35 -05002660 if (!hdr) {
2661 tipc_bcast_unlock(net);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002662 return -EMSGSIZE;
Insu Yunb53ce3e2016-02-17 11:47:35 -05002663 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002664
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002665 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002666 if (!attrs)
2667 goto msg_full;
2668
2669 /* The broadcast link is always up */
2670 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2671 goto attr_msg_full;
2672
2673 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
2674 goto attr_msg_full;
2675 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
2676 goto attr_msg_full;
Jon Paul Maloy95901122016-11-25 10:35:02 -05002677 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002678 goto attr_msg_full;
Jon Paul Maloy95901122016-11-25 10:35:02 -05002679 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002680 goto attr_msg_full;
2681
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002682 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_LINK_PROP);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002683 if (!prop)
2684 goto attr_msg_full;
Jon Maloy16ad3f42019-12-10 00:52:46 +01002685 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->max_win))
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002686 goto prop_msg_full;
Hoang Le02ec6ca2019-03-19 18:49:48 +07002687 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
2688 goto prop_msg_full;
2689 if (bc_mode & BCLINK_MODE_SEL)
2690 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
2691 bc_ratio))
2692 goto prop_msg_full;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002693 nla_nest_end(msg->skb, prop);
2694
2695 err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
2696 if (err)
2697 goto attr_msg_full;
2698
2699 tipc_bcast_unlock(net);
2700 nla_nest_end(msg->skb, attrs);
2701 genlmsg_end(msg->skb, hdr);
2702
2703 return 0;
2704
2705prop_msg_full:
2706 nla_nest_cancel(msg->skb, prop);
2707attr_msg_full:
2708 nla_nest_cancel(msg->skb, attrs);
2709msg_full:
2710 tipc_bcast_unlock(net);
2711 genlmsg_cancel(msg->skb, hdr);
2712
2713 return -EMSGSIZE;
2714}
2715
Richard Alped01332f2016-02-01 08:19:56 +01002716void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
2717 struct sk_buff_head *xmitq)
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002718{
2719 l->tolerance = tol;
Jon Maloy047491e2018-10-10 17:34:01 +02002720 if (l->bc_rcvlink)
2721 l->bc_rcvlink->tolerance = tol;
Jon Maloy37c64cf2018-02-14 13:34:39 +01002722 if (link_is_up(l))
2723 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002724}
2725
Richard Alped01332f2016-02-01 08:19:56 +01002726void tipc_link_set_prio(struct tipc_link *l, u32 prio,
2727 struct sk_buff_head *xmitq)
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002728{
2729 l->priority = prio;
Jon Maloy8d6e79d2017-11-08 09:59:26 +01002730 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002731}
2732
2733void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
2734{
2735 l->abort_limit = limit;
2736}
Tuong Lienb4b97712018-12-19 09:17:56 +07002737
2738char *tipc_link_name_ext(struct tipc_link *l, char *buf)
2739{
2740 if (!l)
2741 scnprintf(buf, TIPC_MAX_LINK_NAME, "null");
2742 else if (link_is_bc_sndlink(l))
2743 scnprintf(buf, TIPC_MAX_LINK_NAME, "broadcast-sender");
2744 else if (link_is_bc_rcvlink(l))
2745 scnprintf(buf, TIPC_MAX_LINK_NAME,
2746 "broadcast-receiver, peer %x", l->addr);
2747 else
2748 memcpy(buf, l->name, TIPC_MAX_LINK_NAME);
2749
2750 return buf;
2751}
2752
2753/**
2754 * tipc_link_dump - dump TIPC link data
2755 * @l: tipc link to be dumped
2756 * @dqueues: bitmask to decide if any link queue to be dumped?
2757 * - TIPC_DUMP_NONE: don't dump link queues
2758 * - TIPC_DUMP_TRANSMQ: dump link transmq queue
2759 * - TIPC_DUMP_BACKLOGQ: dump link backlog queue
2760 * - TIPC_DUMP_DEFERDQ: dump link deferd queue
2761 * - TIPC_DUMP_INPUTQ: dump link input queue
2762 * - TIPC_DUMP_WAKEUP: dump link wakeup queue
2763 * - TIPC_DUMP_ALL: dump all the link queues above
2764 * @buf: returned buffer of dump data in format
2765 */
2766int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
2767{
2768 int i = 0;
2769 size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
2770 struct sk_buff_head *list;
2771 struct sk_buff *hskb, *tskb;
2772 u32 len;
2773
2774 if (!l) {
2775 i += scnprintf(buf, sz, "link data: (null)\n");
2776 return i;
2777 }
2778
2779 i += scnprintf(buf, sz, "link data: %x", l->addr);
2780 i += scnprintf(buf + i, sz - i, " %x", l->state);
2781 i += scnprintf(buf + i, sz - i, " %u", l->in_session);
2782 i += scnprintf(buf + i, sz - i, " %u", l->session);
2783 i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
2784 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
2785 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
2786 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
2787 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
2788 i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
2789 i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
2790 i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
Tuong Lien71204232019-08-15 10:24:08 +07002791 i += scnprintf(buf + i, sz - i, " %u", 0);
Jon Maloy77cf8ed2019-06-25 17:36:43 +02002792 i += scnprintf(buf + i, sz - i, " %u", 0);
Tuong Lienb4b97712018-12-19 09:17:56 +07002793 i += scnprintf(buf + i, sz - i, " %u", l->acked);
2794
2795 list = &l->transmq;
2796 len = skb_queue_len(list);
2797 hskb = skb_peek(list);
2798 tskb = skb_peek_tail(list);
2799 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2800 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2801 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2802
2803 list = &l->deferdq;
2804 len = skb_queue_len(list);
2805 hskb = skb_peek(list);
2806 tskb = skb_peek_tail(list);
2807 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2808 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2809 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2810
2811 list = &l->backlogq;
2812 len = skb_queue_len(list);
2813 hskb = skb_peek(list);
2814 tskb = skb_peek_tail(list);
2815 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2816 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2817 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2818
2819 list = l->inputq;
2820 len = skb_queue_len(list);
2821 hskb = skb_peek(list);
2822 tskb = skb_peek_tail(list);
2823 i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
2824 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2825 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2826
2827 if (dqueues & TIPC_DUMP_TRANSMQ) {
2828 i += scnprintf(buf + i, sz - i, "transmq: ");
2829 i += tipc_list_dump(&l->transmq, false, buf + i);
2830 }
2831 if (dqueues & TIPC_DUMP_BACKLOGQ) {
2832 i += scnprintf(buf + i, sz - i,
2833 "backlogq: <%u %u %u %u %u>, ",
2834 l->backlog[TIPC_LOW_IMPORTANCE].len,
2835 l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
2836 l->backlog[TIPC_HIGH_IMPORTANCE].len,
2837 l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
2838 l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
2839 i += tipc_list_dump(&l->backlogq, false, buf + i);
2840 }
2841 if (dqueues & TIPC_DUMP_DEFERDQ) {
2842 i += scnprintf(buf + i, sz - i, "deferdq: ");
2843 i += tipc_list_dump(&l->deferdq, false, buf + i);
2844 }
2845 if (dqueues & TIPC_DUMP_INPUTQ) {
2846 i += scnprintf(buf + i, sz - i, "inputq: ");
2847 i += tipc_list_dump(l->inputq, false, buf + i);
2848 }
2849 if (dqueues & TIPC_DUMP_WAKEUP) {
2850 i += scnprintf(buf + i, sz - i, "wakeup: ");
2851 i += tipc_list_dump(&l->wakeupq, false, buf + i);
2852 }
2853
2854 return i;
2855}