xref: /openbmc/linux/net/batman-adv/tp_meter.c (revision d9fd5a71)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3  *
4  * Edo Monticelli, Antonio Quartulli
5  */
6 
7 #include "tp_meter.h"
8 #include "main.h"
9 
10 #include <linux/atomic.h>
11 #include <linux/build_bug.h>
12 #include <linux/byteorder/generic.h>
13 #include <linux/cache.h>
14 #include <linux/compiler.h>
15 #include <linux/err.h>
16 #include <linux/etherdevice.h>
17 #include <linux/gfp.h>
18 #include <linux/if_ether.h>
19 #include <linux/init.h>
20 #include <linux/jiffies.h>
21 #include <linux/kernel.h>
22 #include <linux/kref.h>
23 #include <linux/kthread.h>
24 #include <linux/limits.h>
25 #include <linux/list.h>
26 #include <linux/minmax.h>
27 #include <linux/netdevice.h>
28 #include <linux/param.h>
29 #include <linux/printk.h>
30 #include <linux/random.h>
31 #include <linux/rculist.h>
32 #include <linux/rcupdate.h>
33 #include <linux/sched.h>
34 #include <linux/skbuff.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/stddef.h>
38 #include <linux/string.h>
39 #include <linux/timer.h>
40 #include <linux/wait.h>
41 #include <linux/workqueue.h>
42 #include <uapi/linux/batadv_packet.h>
43 #include <uapi/linux/batman_adv.h>
44 
45 #include "hard-interface.h"
46 #include "log.h"
47 #include "netlink.h"
48 #include "originator.h"
49 #include "send.h"
50 
51 /**
52  * BATADV_TP_DEF_TEST_LENGTH - Default test length if not specified by the user
53  *  in milliseconds
54  */
55 #define BATADV_TP_DEF_TEST_LENGTH 10000
56 
57 /**
58  * BATADV_TP_AWND - Advertised window by the receiver (in bytes)
59  */
60 #define BATADV_TP_AWND 0x20000000
61 
62 /**
63  * BATADV_TP_RECV_TIMEOUT - Receiver activity timeout. If the receiver does not
64  *  get anything for such amount of milliseconds, the connection is killed
65  */
66 #define BATADV_TP_RECV_TIMEOUT 1000
67 
68 /**
69  * BATADV_TP_MAX_RTO - Maximum sender timeout. If the sender RTO gets beyond
70  * such amount of milliseconds, the receiver is considered unreachable and the
71  * connection is killed
72  */
73 #define BATADV_TP_MAX_RTO 30000
74 
75 /**
76  * BATADV_TP_FIRST_SEQ - First seqno of each session. The number is rather high
77  *  in order to immediately trigger a wrap around (test purposes)
78  */
79 #define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
80 
81 /**
82  * BATADV_TP_PLEN - length of the payload (data after the batadv_unicast header)
83  *  to simulate
84  */
85 #define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
86 			sizeof(struct batadv_unicast_packet))
87 
88 static u8 batadv_tp_prerandom[4096] __read_mostly;
89 
90 /**
91  * batadv_tp_session_cookie() - generate session cookie based on session ids
92  * @session: TP session identifier
93  * @icmp_uid: icmp pseudo uid of the tp session
94  *
95  * Return: 32 bit tp_meter session cookie
96  */
97 static u32 batadv_tp_session_cookie(const u8 session[2], u8 icmp_uid)
98 {
99 	u32 cookie;
100 
101 	cookie = icmp_uid << 16;
102 	cookie |= session[0] << 8;
103 	cookie |= session[1];
104 
105 	return cookie;
106 }
107 
108 /**
109  * batadv_tp_cwnd() - compute the new cwnd size
110  * @base: base cwnd size value
111  * @increment: the value to add to base to get the new size
112  * @min: minimum cwnd value (usually MSS)
113  *
114  * Return the new cwnd size and ensure it does not exceed the Advertised
115  * Receiver Window size. It is wrapped around safely.
116  * For details refer to Section 3.1 of RFC5681
117  *
118  * Return: new congestion window size in bytes
119  */
120 static u32 batadv_tp_cwnd(u32 base, u32 increment, u32 min)
121 {
122 	u32 new_size = base + increment;
123 
124 	/* check for wrap-around */
125 	if (new_size < base)
126 		new_size = (u32)ULONG_MAX;
127 
128 	new_size = min_t(u32, new_size, BATADV_TP_AWND);
129 
130 	return max_t(u32, new_size, min);
131 }
132 
133 /**
134  * batadv_tp_update_cwnd() - update the Congestion Windows
135  * @tp_vars: the private data of the current TP meter session
136  * @mss: maximum segment size of transmission
137  *
138  * 1) if the session is in Slow Start, the CWND has to be increased by 1
139  * MSS every unique received ACK
140  * 2) if the session is in Congestion Avoidance, the CWND has to be
141  * increased by MSS * MSS / CWND for every unique received ACK
142  */
143 static void batadv_tp_update_cwnd(struct batadv_tp_vars *tp_vars, u32 mss)
144 {
145 	spin_lock_bh(&tp_vars->cwnd_lock);
146 
147 	/* slow start... */
148 	if (tp_vars->cwnd <= tp_vars->ss_threshold) {
149 		tp_vars->dec_cwnd = 0;
150 		tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
151 		spin_unlock_bh(&tp_vars->cwnd_lock);
152 		return;
153 	}
154 
155 	/* increment CWND at least of 1 (section 3.1 of RFC5681) */
156 	tp_vars->dec_cwnd += max_t(u32, 1U << 3,
157 				   ((mss * mss) << 6) / (tp_vars->cwnd << 3));
158 	if (tp_vars->dec_cwnd < (mss << 3)) {
159 		spin_unlock_bh(&tp_vars->cwnd_lock);
160 		return;
161 	}
162 
163 	tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
164 	tp_vars->dec_cwnd = 0;
165 
166 	spin_unlock_bh(&tp_vars->cwnd_lock);
167 }
168 
169 /**
170  * batadv_tp_update_rto() - calculate new retransmission timeout
171  * @tp_vars: the private data of the current TP meter session
172  * @new_rtt: new roundtrip time in msec
173  */
174 static void batadv_tp_update_rto(struct batadv_tp_vars *tp_vars,
175 				 u32 new_rtt)
176 {
177 	long m = new_rtt;
178 
179 	/* RTT update
180 	 * Details in Section 2.2 and 2.3 of RFC6298
181 	 *
182 	 * It's tricky to understand. Don't lose hair please.
183 	 * Inspired by tcp_rtt_estimator() tcp_input.c
184 	 */
185 	if (tp_vars->srtt != 0) {
186 		m -= (tp_vars->srtt >> 3); /* m is now error in rtt est */
187 		tp_vars->srtt += m; /* rtt = 7/8 srtt + 1/8 new */
188 		if (m < 0)
189 			m = -m;
190 
191 		m -= (tp_vars->rttvar >> 2);
192 		tp_vars->rttvar += m; /* mdev ~= 3/4 rttvar + 1/4 new */
193 	} else {
194 		/* first measure getting in */
195 		tp_vars->srtt = m << 3;	/* take the measured time to be srtt */
196 		tp_vars->rttvar = m << 1; /* new_rtt / 2 */
197 	}
198 
199 	/* rto = srtt + 4 * rttvar.
200 	 * rttvar is scaled by 4, therefore doesn't need to be multiplied
201 	 */
202 	tp_vars->rto = (tp_vars->srtt >> 3) + tp_vars->rttvar;
203 }
204 
205 /**
206  * batadv_tp_batctl_notify() - send client status result to client
207  * @reason: reason for tp meter session stop
208  * @dst: destination of tp_meter session
209  * @bat_priv: the bat priv with all the soft interface information
210  * @start_time: start of transmission in jiffies
211  * @total_sent: bytes acked to the receiver
212  * @cookie: cookie of tp_meter session
213  */
214 static void batadv_tp_batctl_notify(enum batadv_tp_meter_reason reason,
215 				    const u8 *dst, struct batadv_priv *bat_priv,
216 				    unsigned long start_time, u64 total_sent,
217 				    u32 cookie)
218 {
219 	u32 test_time;
220 	u8 result;
221 	u32 total_bytes;
222 
223 	if (!batadv_tp_is_error(reason)) {
224 		result = BATADV_TP_REASON_COMPLETE;
225 		test_time = jiffies_to_msecs(jiffies - start_time);
226 		total_bytes = total_sent;
227 	} else {
228 		result = reason;
229 		test_time = 0;
230 		total_bytes = 0;
231 	}
232 
233 	batadv_netlink_tpmeter_notify(bat_priv, dst, result, test_time,
234 				      total_bytes, cookie);
235 }
236 
237 /**
238  * batadv_tp_batctl_error_notify() - send client error result to client
239  * @reason: reason for tp meter session stop
240  * @dst: destination of tp_meter session
241  * @bat_priv: the bat priv with all the soft interface information
242  * @cookie: cookie of tp_meter session
243  */
244 static void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason,
245 					  const u8 *dst,
246 					  struct batadv_priv *bat_priv,
247 					  u32 cookie)
248 {
249 	batadv_tp_batctl_notify(reason, dst, bat_priv, 0, 0, cookie);
250 }
251 
252 /**
253  * batadv_tp_list_find() - find a tp_vars object in the global list
254  * @bat_priv: the bat priv with all the soft interface information
255  * @dst: the other endpoint MAC address to look for
256  *
257  * Look for a tp_vars object matching dst as end_point and return it after
258  * having increment the refcounter. Return NULL is not found
259  *
260  * Return: matching tp_vars or NULL when no tp_vars with @dst was found
261  */
262 static struct batadv_tp_vars *batadv_tp_list_find(struct batadv_priv *bat_priv,
263 						  const u8 *dst)
264 {
265 	struct batadv_tp_vars *pos, *tp_vars = NULL;
266 
267 	rcu_read_lock();
268 	hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
269 		if (!batadv_compare_eth(pos->other_end, dst))
270 			continue;
271 
272 		/* most of the time this function is invoked during the normal
273 		 * process..it makes sens to pay more when the session is
274 		 * finished and to speed the process up during the measurement
275 		 */
276 		if (unlikely(!kref_get_unless_zero(&pos->refcount)))
277 			continue;
278 
279 		tp_vars = pos;
280 		break;
281 	}
282 	rcu_read_unlock();
283 
284 	return tp_vars;
285 }
286 
287 /**
288  * batadv_tp_list_find_session() - find tp_vars session object in the global
289  *  list
290  * @bat_priv: the bat priv with all the soft interface information
291  * @dst: the other endpoint MAC address to look for
292  * @session: session identifier
293  *
294  * Look for a tp_vars object matching dst as end_point, session as tp meter
295  * session and return it after having increment the refcounter. Return NULL
296  * is not found
297  *
298  * Return: matching tp_vars or NULL when no tp_vars was found
299  */
300 static struct batadv_tp_vars *
301 batadv_tp_list_find_session(struct batadv_priv *bat_priv, const u8 *dst,
302 			    const u8 *session)
303 {
304 	struct batadv_tp_vars *pos, *tp_vars = NULL;
305 
306 	rcu_read_lock();
307 	hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
308 		if (!batadv_compare_eth(pos->other_end, dst))
309 			continue;
310 
311 		if (memcmp(pos->session, session, sizeof(pos->session)) != 0)
312 			continue;
313 
314 		/* most of the time this function is invoked during the normal
315 		 * process..it makes sense to pay more when the session is
316 		 * finished and to speed the process up during the measurement
317 		 */
318 		if (unlikely(!kref_get_unless_zero(&pos->refcount)))
319 			continue;
320 
321 		tp_vars = pos;
322 		break;
323 	}
324 	rcu_read_unlock();
325 
326 	return tp_vars;
327 }
328 
329 /**
330  * batadv_tp_vars_release() - release batadv_tp_vars from lists and queue for
331  *  free after rcu grace period
332  * @ref: kref pointer of the batadv_tp_vars
333  */
334 static void batadv_tp_vars_release(struct kref *ref)
335 {
336 	struct batadv_tp_vars *tp_vars;
337 	struct batadv_tp_unacked *un, *safe;
338 
339 	tp_vars = container_of(ref, struct batadv_tp_vars, refcount);
340 
341 	/* lock should not be needed because this object is now out of any
342 	 * context!
343 	 */
344 	spin_lock_bh(&tp_vars->unacked_lock);
345 	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
346 		list_del(&un->list);
347 		kfree(un);
348 	}
349 	spin_unlock_bh(&tp_vars->unacked_lock);
350 
351 	kfree_rcu(tp_vars, rcu);
352 }
353 
354 /**
355  * batadv_tp_vars_put() - decrement the batadv_tp_vars refcounter and possibly
356  *  release it
357  * @tp_vars: the private data of the current TP meter session to be free'd
358  */
359 static void batadv_tp_vars_put(struct batadv_tp_vars *tp_vars)
360 {
361 	kref_put(&tp_vars->refcount, batadv_tp_vars_release);
362 }
363 
364 /**
365  * batadv_tp_sender_cleanup() - cleanup sender data and drop and timer
366  * @bat_priv: the bat priv with all the soft interface information
367  * @tp_vars: the private data of the current TP meter session to cleanup
368  */
369 static void batadv_tp_sender_cleanup(struct batadv_priv *bat_priv,
370 				     struct batadv_tp_vars *tp_vars)
371 {
372 	cancel_delayed_work(&tp_vars->finish_work);
373 
374 	spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
375 	hlist_del_rcu(&tp_vars->list);
376 	spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
377 
378 	/* drop list reference */
379 	batadv_tp_vars_put(tp_vars);
380 
381 	atomic_dec(&tp_vars->bat_priv->tp_num);
382 
383 	/* kill the timer and remove its reference */
384 	del_timer_sync(&tp_vars->timer);
385 	/* the worker might have rearmed itself therefore we kill it again. Note
386 	 * that if the worker should run again before invoking the following
387 	 * del_timer(), it would not re-arm itself once again because the status
388 	 * is OFF now
389 	 */
390 	del_timer(&tp_vars->timer);
391 	batadv_tp_vars_put(tp_vars);
392 }
393 
394 /**
395  * batadv_tp_sender_end() - print info about ended session and inform client
396  * @bat_priv: the bat priv with all the soft interface information
397  * @tp_vars: the private data of the current TP meter session
398  */
399 static void batadv_tp_sender_end(struct batadv_priv *bat_priv,
400 				 struct batadv_tp_vars *tp_vars)
401 {
402 	u32 session_cookie;
403 
404 	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
405 		   "Test towards %pM finished..shutting down (reason=%d)\n",
406 		   tp_vars->other_end, tp_vars->reason);
407 
408 	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
409 		   "Last timing stats: SRTT=%ums RTTVAR=%ums RTO=%ums\n",
410 		   tp_vars->srtt >> 3, tp_vars->rttvar >> 2, tp_vars->rto);
411 
412 	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
413 		   "Final values: cwnd=%u ss_threshold=%u\n",
414 		   tp_vars->cwnd, tp_vars->ss_threshold);
415 
416 	session_cookie = batadv_tp_session_cookie(tp_vars->session,
417 						  tp_vars->icmp_uid);
418 
419 	batadv_tp_batctl_notify(tp_vars->reason,
420 				tp_vars->other_end,
421 				bat_priv,
422 				tp_vars->start_time,
423 				atomic64_read(&tp_vars->tot_sent),
424 				session_cookie);
425 }
426 
427 /**
428  * batadv_tp_sender_shutdown() - let sender thread/timer stop gracefully
429  * @tp_vars: the private data of the current TP meter session
430  * @reason: reason for tp meter session stop
431  */
432 static void batadv_tp_sender_shutdown(struct batadv_tp_vars *tp_vars,
433 				      enum batadv_tp_meter_reason reason)
434 {
435 	if (!atomic_dec_and_test(&tp_vars->sending))
436 		return;
437 
438 	tp_vars->reason = reason;
439 }
440 
441 /**
442  * batadv_tp_sender_finish() - stop sender session after test_length was reached
443  * @work: delayed work reference of the related tp_vars
444  */
445 static void batadv_tp_sender_finish(struct work_struct *work)
446 {
447 	struct delayed_work *delayed_work;
448 	struct batadv_tp_vars *tp_vars;
449 
450 	delayed_work = to_delayed_work(work);
451 	tp_vars = container_of(delayed_work, struct batadv_tp_vars,
452 			       finish_work);
453 
454 	batadv_tp_sender_shutdown(tp_vars, BATADV_TP_REASON_COMPLETE);
455 }
456 
457 /**
458  * batadv_tp_reset_sender_timer() - reschedule the sender timer
459  * @tp_vars: the private TP meter data for this session
460  *
461  * Reschedule the timer using tp_vars->rto as delay
462  */
463 static void batadv_tp_reset_sender_timer(struct batadv_tp_vars *tp_vars)
464 {
465 	/* most of the time this function is invoked while normal packet
466 	 * reception...
467 	 */
468 	if (unlikely(atomic_read(&tp_vars->sending) == 0))
469 		/* timer ref will be dropped in batadv_tp_sender_cleanup */
470 		return;
471 
472 	mod_timer(&tp_vars->timer, jiffies + msecs_to_jiffies(tp_vars->rto));
473 }
474 
475 /**
476  * batadv_tp_sender_timeout() - timer that fires in case of packet loss
477  * @t: address to timer_list inside tp_vars
478  *
479  * If fired it means that there was packet loss.
480  * Switch to Slow Start, set the ss_threshold to half of the current cwnd and
481  * reset the cwnd to 3*MSS
482  */
483 static void batadv_tp_sender_timeout(struct timer_list *t)
484 {
485 	struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
486 	struct batadv_priv *bat_priv = tp_vars->bat_priv;
487 
488 	if (atomic_read(&tp_vars->sending) == 0)
489 		return;
490 
491 	/* if the user waited long enough...shutdown the test */
492 	if (unlikely(tp_vars->rto >= BATADV_TP_MAX_RTO)) {
493 		batadv_tp_sender_shutdown(tp_vars,
494 					  BATADV_TP_REASON_DST_UNREACHABLE);
495 		return;
496 	}
497 
498 	/* RTO exponential backoff
499 	 * Details in Section 5.5 of RFC6298
500 	 */
501 	tp_vars->rto <<= 1;
502 
503 	spin_lock_bh(&tp_vars->cwnd_lock);
504 
505 	tp_vars->ss_threshold = tp_vars->cwnd >> 1;
506 	if (tp_vars->ss_threshold < BATADV_TP_PLEN * 2)
507 		tp_vars->ss_threshold = BATADV_TP_PLEN * 2;
508 
509 	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
510 		   "Meter: RTO fired during test towards %pM! cwnd=%u new ss_thr=%u, resetting last_sent to %u\n",
511 		   tp_vars->other_end, tp_vars->cwnd, tp_vars->ss_threshold,
512 		   atomic_read(&tp_vars->last_acked));
513 
514 	tp_vars->cwnd = BATADV_TP_PLEN * 3;
515 
516 	spin_unlock_bh(&tp_vars->cwnd_lock);
517 
518 	/* resend the non-ACKed packets.. */
519 	tp_vars->last_sent = atomic_read(&tp_vars->last_acked);
520 	wake_up(&tp_vars->more_bytes);
521 
522 	batadv_tp_reset_sender_timer(tp_vars);
523 }
524 
525 /**
526  * batadv_tp_fill_prerandom() - Fill buffer with prefetched random bytes
527  * @tp_vars: the private TP meter data for this session
528  * @buf: Buffer to fill with bytes
529  * @nbytes: amount of pseudorandom bytes
530  */
531 static void batadv_tp_fill_prerandom(struct batadv_tp_vars *tp_vars,
532 				     u8 *buf, size_t nbytes)
533 {
534 	u32 local_offset;
535 	size_t bytes_inbuf;
536 	size_t to_copy;
537 	size_t pos = 0;
538 
539 	spin_lock_bh(&tp_vars->prerandom_lock);
540 	local_offset = tp_vars->prerandom_offset;
541 	tp_vars->prerandom_offset += nbytes;
542 	tp_vars->prerandom_offset %= sizeof(batadv_tp_prerandom);
543 	spin_unlock_bh(&tp_vars->prerandom_lock);
544 
545 	while (nbytes) {
546 		local_offset %= sizeof(batadv_tp_prerandom);
547 		bytes_inbuf = sizeof(batadv_tp_prerandom) - local_offset;
548 		to_copy = min(nbytes, bytes_inbuf);
549 
550 		memcpy(&buf[pos], &batadv_tp_prerandom[local_offset], to_copy);
551 		pos += to_copy;
552 		nbytes -= to_copy;
553 		local_offset = 0;
554 	}
555 }
556 
557 /**
558  * batadv_tp_send_msg() - send a single message
559  * @tp_vars: the private TP meter data for this session
560  * @src: source mac address
561  * @orig_node: the originator of the destination
562  * @seqno: sequence number of this packet
563  * @len: length of the entire packet
564  * @session: session identifier
565  * @uid: local ICMP "socket" index
566  * @timestamp: timestamp in jiffies which is replied in ack
567  *
568  * Create and send a single TP Meter message.
569  *
570  * Return: 0 on success, BATADV_TP_REASON_DST_UNREACHABLE if the destination is
571  * not reachable, BATADV_TP_REASON_MEMORY_ERROR if the packet couldn't be
572  * allocated
573  */
574 static int batadv_tp_send_msg(struct batadv_tp_vars *tp_vars, const u8 *src,
575 			      struct batadv_orig_node *orig_node,
576 			      u32 seqno, size_t len, const u8 *session,
577 			      int uid, u32 timestamp)
578 {
579 	struct batadv_icmp_tp_packet *icmp;
580 	struct sk_buff *skb;
581 	int r;
582 	u8 *data;
583 	size_t data_len;
584 
585 	skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
586 	if (unlikely(!skb))
587 		return BATADV_TP_REASON_MEMORY_ERROR;
588 
589 	skb_reserve(skb, ETH_HLEN);
590 	icmp = skb_put(skb, sizeof(*icmp));
591 
592 	/* fill the icmp header */
593 	ether_addr_copy(icmp->dst, orig_node->orig);
594 	ether_addr_copy(icmp->orig, src);
595 	icmp->version = BATADV_COMPAT_VERSION;
596 	icmp->packet_type = BATADV_ICMP;
597 	icmp->ttl = BATADV_TTL;
598 	icmp->msg_type = BATADV_TP;
599 	icmp->uid = uid;
600 
601 	icmp->subtype = BATADV_TP_MSG;
602 	memcpy(icmp->session, session, sizeof(icmp->session));
603 	icmp->seqno = htonl(seqno);
604 	icmp->timestamp = htonl(timestamp);
605 
606 	data_len = len - sizeof(*icmp);
607 	data = skb_put(skb, data_len);
608 	batadv_tp_fill_prerandom(tp_vars, data, data_len);
609 
610 	r = batadv_send_skb_to_orig(skb, orig_node, NULL);
611 	if (r == NET_XMIT_SUCCESS)
612 		return 0;
613 
614 	return BATADV_TP_REASON_CANT_SEND;
615 }
616 
617 /**
618  * batadv_tp_recv_ack() - ACK receiving function
619  * @bat_priv: the bat priv with all the soft interface information
620  * @skb: the buffer containing the received packet
621  *
622  * Process a received TP ACK packet
623  */
624 static void batadv_tp_recv_ack(struct batadv_priv *bat_priv,
625 			       const struct sk_buff *skb)
626 {
627 	struct batadv_hard_iface *primary_if = NULL;
628 	struct batadv_orig_node *orig_node = NULL;
629 	const struct batadv_icmp_tp_packet *icmp;
630 	struct batadv_tp_vars *tp_vars;
631 	size_t packet_len, mss;
632 	u32 rtt, recv_ack, cwnd;
633 	unsigned char *dev_addr;
634 
635 	packet_len = BATADV_TP_PLEN;
636 	mss = BATADV_TP_PLEN;
637 	packet_len += sizeof(struct batadv_unicast_packet);
638 
639 	icmp = (struct batadv_icmp_tp_packet *)skb->data;
640 
641 	/* find the tp_vars */
642 	tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
643 					      icmp->session);
644 	if (unlikely(!tp_vars))
645 		return;
646 
647 	if (unlikely(atomic_read(&tp_vars->sending) == 0))
648 		goto out;
649 
650 	/* old ACK? silently drop it.. */
651 	if (batadv_seq_before(ntohl(icmp->seqno),
652 			      (u32)atomic_read(&tp_vars->last_acked)))
653 		goto out;
654 
655 	primary_if = batadv_primary_if_get_selected(bat_priv);
656 	if (unlikely(!primary_if))
657 		goto out;
658 
659 	orig_node = batadv_orig_hash_find(bat_priv, icmp->orig);
660 	if (unlikely(!orig_node))
661 		goto out;
662 
663 	/* update RTO with the new sampled RTT, if any */
664 	rtt = jiffies_to_msecs(jiffies) - ntohl(icmp->timestamp);
665 	if (icmp->timestamp && rtt)
666 		batadv_tp_update_rto(tp_vars, rtt);
667 
668 	/* ACK for new data... reset the timer */
669 	batadv_tp_reset_sender_timer(tp_vars);
670 
671 	recv_ack = ntohl(icmp->seqno);
672 
673 	/* check if this ACK is a duplicate */
674 	if (atomic_read(&tp_vars->last_acked) == recv_ack) {
675 		atomic_inc(&tp_vars->dup_acks);
676 		if (atomic_read(&tp_vars->dup_acks) != 3)
677 			goto out;
678 
679 		if (recv_ack >= tp_vars->recover)
680 			goto out;
681 
682 		/* if this is the third duplicate ACK do Fast Retransmit */
683 		batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
684 				   orig_node, recv_ack, packet_len,
685 				   icmp->session, icmp->uid,
686 				   jiffies_to_msecs(jiffies));
687 
688 		spin_lock_bh(&tp_vars->cwnd_lock);
689 
690 		/* Fast Recovery */
691 		tp_vars->fast_recovery = true;
692 		/* Set recover to the last outstanding seqno when Fast Recovery
693 		 * is entered. RFC6582, Section 3.2, step 1
694 		 */
695 		tp_vars->recover = tp_vars->last_sent;
696 		tp_vars->ss_threshold = tp_vars->cwnd >> 1;
697 		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
698 			   "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
699 			   tp_vars->cwnd, tp_vars->ss_threshold,
700 			   tp_vars->last_sent, recv_ack);
701 		tp_vars->cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 3 * mss,
702 					       mss);
703 		tp_vars->dec_cwnd = 0;
704 		tp_vars->last_sent = recv_ack;
705 
706 		spin_unlock_bh(&tp_vars->cwnd_lock);
707 	} else {
708 		/* count the acked data */
709 		atomic64_add(recv_ack - atomic_read(&tp_vars->last_acked),
710 			     &tp_vars->tot_sent);
711 		/* reset the duplicate ACKs counter */
712 		atomic_set(&tp_vars->dup_acks, 0);
713 
714 		if (tp_vars->fast_recovery) {
715 			/* partial ACK */
716 			if (batadv_seq_before(recv_ack, tp_vars->recover)) {
717 				/* this is another hole in the window. React
718 				 * immediately as specified by NewReno (see
719 				 * Section 3.2 of RFC6582 for details)
720 				 */
721 				dev_addr = primary_if->net_dev->dev_addr;
722 				batadv_tp_send_msg(tp_vars, dev_addr,
723 						   orig_node, recv_ack,
724 						   packet_len, icmp->session,
725 						   icmp->uid,
726 						   jiffies_to_msecs(jiffies));
727 				tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd,
728 							       mss, mss);
729 			} else {
730 				tp_vars->fast_recovery = false;
731 				/* set cwnd to the value of ss_threshold at the
732 				 * moment that Fast Recovery was entered.
733 				 * RFC6582, Section 3.2, step 3
734 				 */
735 				cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 0,
736 						      mss);
737 				tp_vars->cwnd = cwnd;
738 			}
739 			goto move_twnd;
740 		}
741 
742 		if (recv_ack - atomic_read(&tp_vars->last_acked) >= mss)
743 			batadv_tp_update_cwnd(tp_vars, mss);
744 move_twnd:
745 		/* move the Transmit Window */
746 		atomic_set(&tp_vars->last_acked, recv_ack);
747 	}
748 
749 	wake_up(&tp_vars->more_bytes);
750 out:
751 	if (likely(primary_if))
752 		batadv_hardif_put(primary_if);
753 	if (likely(orig_node))
754 		batadv_orig_node_put(orig_node);
755 	if (likely(tp_vars))
756 		batadv_tp_vars_put(tp_vars);
757 }
758 
759 /**
760  * batadv_tp_avail() - check if congestion window is not full
761  * @tp_vars: the private data of the current TP meter session
762  * @payload_len: size of the payload of a single message
763  *
764  * Return: true when congestion window is not full, false otherwise
765  */
766 static bool batadv_tp_avail(struct batadv_tp_vars *tp_vars,
767 			    size_t payload_len)
768 {
769 	u32 win_left, win_limit;
770 
771 	win_limit = atomic_read(&tp_vars->last_acked) + tp_vars->cwnd;
772 	win_left = win_limit - tp_vars->last_sent;
773 
774 	return win_left >= payload_len;
775 }
776 
777 /**
778  * batadv_tp_wait_available() - wait until congestion window becomes free or
779  *  timeout is reached
780  * @tp_vars: the private data of the current TP meter session
781  * @plen: size of the payload of a single message
782  *
783  * Return: 0 if the condition evaluated to false after the timeout elapsed,
784  *  1 if the condition evaluated to true after the timeout elapsed, the
785  *  remaining jiffies (at least 1) if the condition evaluated to true before
786  *  the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
787  */
788 static int batadv_tp_wait_available(struct batadv_tp_vars *tp_vars, size_t plen)
789 {
790 	int ret;
791 
792 	ret = wait_event_interruptible_timeout(tp_vars->more_bytes,
793 					       batadv_tp_avail(tp_vars, plen),
794 					       HZ / 10);
795 
796 	return ret;
797 }
798 
799 /**
800  * batadv_tp_send() - main sending thread of a tp meter session
801  * @arg: address of the related tp_vars
802  *
803  * Return: nothing, this function never returns
804  */
805 static int batadv_tp_send(void *arg)
806 {
807 	struct batadv_tp_vars *tp_vars = arg;
808 	struct batadv_priv *bat_priv = tp_vars->bat_priv;
809 	struct batadv_hard_iface *primary_if = NULL;
810 	struct batadv_orig_node *orig_node = NULL;
811 	size_t payload_len, packet_len;
812 	int err = 0;
813 
814 	if (unlikely(tp_vars->role != BATADV_TP_SENDER)) {
815 		err = BATADV_TP_REASON_DST_UNREACHABLE;
816 		tp_vars->reason = err;
817 		goto out;
818 	}
819 
820 	orig_node = batadv_orig_hash_find(bat_priv, tp_vars->other_end);
821 	if (unlikely(!orig_node)) {
822 		err = BATADV_TP_REASON_DST_UNREACHABLE;
823 		tp_vars->reason = err;
824 		goto out;
825 	}
826 
827 	primary_if = batadv_primary_if_get_selected(bat_priv);
828 	if (unlikely(!primary_if)) {
829 		err = BATADV_TP_REASON_DST_UNREACHABLE;
830 		tp_vars->reason = err;
831 		goto out;
832 	}
833 
834 	/* assume that all the hard_interfaces have a correctly
835 	 * configured MTU, so use the soft_iface MTU as MSS.
836 	 * This might not be true and in that case the fragmentation
837 	 * should be used.
838 	 * Now, try to send the packet as it is
839 	 */
840 	payload_len = BATADV_TP_PLEN;
841 	BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet) > BATADV_TP_PLEN);
842 
843 	batadv_tp_reset_sender_timer(tp_vars);
844 
845 	/* queue the worker in charge of terminating the test */
846 	queue_delayed_work(batadv_event_workqueue, &tp_vars->finish_work,
847 			   msecs_to_jiffies(tp_vars->test_length));
848 
849 	while (atomic_read(&tp_vars->sending) != 0) {
850 		if (unlikely(!batadv_tp_avail(tp_vars, payload_len))) {
851 			batadv_tp_wait_available(tp_vars, payload_len);
852 			continue;
853 		}
854 
855 		/* to emulate normal unicast traffic, add to the payload len
856 		 * the size of the unicast header
857 		 */
858 		packet_len = payload_len + sizeof(struct batadv_unicast_packet);
859 
860 		err = batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
861 					 orig_node, tp_vars->last_sent,
862 					 packet_len,
863 					 tp_vars->session, tp_vars->icmp_uid,
864 					 jiffies_to_msecs(jiffies));
865 
866 		/* something went wrong during the preparation/transmission */
867 		if (unlikely(err && err != BATADV_TP_REASON_CANT_SEND)) {
868 			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
869 				   "Meter: %s() cannot send packets (%d)\n",
870 				   __func__, err);
871 			/* ensure nobody else tries to stop the thread now */
872 			if (atomic_dec_and_test(&tp_vars->sending))
873 				tp_vars->reason = err;
874 			break;
875 		}
876 
877 		/* right-shift the TWND */
878 		if (!err)
879 			tp_vars->last_sent += payload_len;
880 
881 		cond_resched();
882 	}
883 
884 out:
885 	if (likely(primary_if))
886 		batadv_hardif_put(primary_if);
887 	if (likely(orig_node))
888 		batadv_orig_node_put(orig_node);
889 
890 	batadv_tp_sender_end(bat_priv, tp_vars);
891 	batadv_tp_sender_cleanup(bat_priv, tp_vars);
892 
893 	batadv_tp_vars_put(tp_vars);
894 
895 	do_exit(0);
896 }
897 
898 /**
899  * batadv_tp_start_kthread() - start new thread which manages the tp meter
900  *  sender
901  * @tp_vars: the private data of the current TP meter session
902  */
903 static void batadv_tp_start_kthread(struct batadv_tp_vars *tp_vars)
904 {
905 	struct task_struct *kthread;
906 	struct batadv_priv *bat_priv = tp_vars->bat_priv;
907 	u32 session_cookie;
908 
909 	kref_get(&tp_vars->refcount);
910 	kthread = kthread_create(batadv_tp_send, tp_vars, "kbatadv_tp_meter");
911 	if (IS_ERR(kthread)) {
912 		session_cookie = batadv_tp_session_cookie(tp_vars->session,
913 							  tp_vars->icmp_uid);
914 		pr_err("batadv: cannot create tp meter kthread\n");
915 		batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
916 					      tp_vars->other_end,
917 					      bat_priv, session_cookie);
918 
919 		/* drop reserved reference for kthread */
920 		batadv_tp_vars_put(tp_vars);
921 
922 		/* cleanup of failed tp meter variables */
923 		batadv_tp_sender_cleanup(bat_priv, tp_vars);
924 		return;
925 	}
926 
927 	wake_up_process(kthread);
928 }
929 
930 /**
931  * batadv_tp_start() - start a new tp meter session
932  * @bat_priv: the bat priv with all the soft interface information
933  * @dst: the receiver MAC address
934  * @test_length: test length in milliseconds
935  * @cookie: session cookie
936  */
937 void batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
938 		     u32 test_length, u32 *cookie)
939 {
940 	struct batadv_tp_vars *tp_vars;
941 	u8 session_id[2];
942 	u8 icmp_uid;
943 	u32 session_cookie;
944 
945 	get_random_bytes(session_id, sizeof(session_id));
946 	get_random_bytes(&icmp_uid, 1);
947 	session_cookie = batadv_tp_session_cookie(session_id, icmp_uid);
948 	*cookie = session_cookie;
949 
950 	/* look for an already existing test towards this node */
951 	spin_lock_bh(&bat_priv->tp_list_lock);
952 	tp_vars = batadv_tp_list_find(bat_priv, dst);
953 	if (tp_vars) {
954 		spin_unlock_bh(&bat_priv->tp_list_lock);
955 		batadv_tp_vars_put(tp_vars);
956 		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
957 			   "Meter: test to or from the same node already ongoing, aborting\n");
958 		batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING,
959 					      dst, bat_priv, session_cookie);
960 		return;
961 	}
962 
963 	if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
964 		spin_unlock_bh(&bat_priv->tp_list_lock);
965 		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
966 			   "Meter: too many ongoing sessions, aborting (SEND)\n");
967 		batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY, dst,
968 					      bat_priv, session_cookie);
969 		return;
970 	}
971 
972 	tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
973 	if (!tp_vars) {
974 		spin_unlock_bh(&bat_priv->tp_list_lock);
975 		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
976 			   "Meter: %s cannot allocate list elements\n",
977 			   __func__);
978 		batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
979 					      dst, bat_priv, session_cookie);
980 		return;
981 	}
982 
983 	/* initialize tp_vars */
984 	ether_addr_copy(tp_vars->other_end, dst);
985 	kref_init(&tp_vars->refcount);
986 	tp_vars->role = BATADV_TP_SENDER;
987 	atomic_set(&tp_vars->sending, 1);
988 	memcpy(tp_vars->session, session_id, sizeof(session_id));
989 	tp_vars->icmp_uid = icmp_uid;
990 
991 	tp_vars->last_sent = BATADV_TP_FIRST_SEQ;
992 	atomic_set(&tp_vars->last_acked, BATADV_TP_FIRST_SEQ);
993 	tp_vars->fast_recovery = false;
994 	tp_vars->recover = BATADV_TP_FIRST_SEQ;
995 
996 	/* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
997 	 * For batman-adv the MSS is the size of the payload received by the
998 	 * soft_interface, hence its MTU
999 	 */
1000 	tp_vars->cwnd = BATADV_TP_PLEN * 3;
1001 	/* at the beginning initialise the SS threshold to the biggest possible
1002 	 * window size, hence the AWND size
1003 	 */
1004 	tp_vars->ss_threshold = BATADV_TP_AWND;
1005 
1006 	/* RTO initial value is 3 seconds.
1007 	 * Details in Section 2.1 of RFC6298
1008 	 */
1009 	tp_vars->rto = 1000;
1010 	tp_vars->srtt = 0;
1011 	tp_vars->rttvar = 0;
1012 
1013 	atomic64_set(&tp_vars->tot_sent, 0);
1014 
1015 	kref_get(&tp_vars->refcount);
1016 	timer_setup(&tp_vars->timer, batadv_tp_sender_timeout, 0);
1017 
1018 	tp_vars->bat_priv = bat_priv;
1019 	tp_vars->start_time = jiffies;
1020 
1021 	init_waitqueue_head(&tp_vars->more_bytes);
1022 
1023 	spin_lock_init(&tp_vars->unacked_lock);
1024 	INIT_LIST_HEAD(&tp_vars->unacked_list);
1025 
1026 	spin_lock_init(&tp_vars->cwnd_lock);
1027 
1028 	tp_vars->prerandom_offset = 0;
1029 	spin_lock_init(&tp_vars->prerandom_lock);
1030 
1031 	kref_get(&tp_vars->refcount);
1032 	hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
1033 	spin_unlock_bh(&bat_priv->tp_list_lock);
1034 
1035 	tp_vars->test_length = test_length;
1036 	if (!tp_vars->test_length)
1037 		tp_vars->test_length = BATADV_TP_DEF_TEST_LENGTH;
1038 
1039 	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1040 		   "Meter: starting throughput meter towards %pM (length=%ums)\n",
1041 		   dst, test_length);
1042 
1043 	/* init work item for finished tp tests */
1044 	INIT_DELAYED_WORK(&tp_vars->finish_work, batadv_tp_sender_finish);
1045 
1046 	/* start tp kthread. This way the write() call issued from userspace can
1047 	 * happily return and avoid to block
1048 	 */
1049 	batadv_tp_start_kthread(tp_vars);
1050 
1051 	/* don't return reference to new tp_vars */
1052 	batadv_tp_vars_put(tp_vars);
1053 }
1054 
1055 /**
1056  * batadv_tp_stop() - stop currently running tp meter session
1057  * @bat_priv: the bat priv with all the soft interface information
1058  * @dst: the receiver MAC address
1059  * @return_value: reason for tp meter session stop
1060  */
1061 void batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
1062 		    u8 return_value)
1063 {
1064 	struct batadv_orig_node *orig_node;
1065 	struct batadv_tp_vars *tp_vars;
1066 
1067 	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1068 		   "Meter: stopping test towards %pM\n", dst);
1069 
1070 	orig_node = batadv_orig_hash_find(bat_priv, dst);
1071 	if (!orig_node)
1072 		return;
1073 
1074 	tp_vars = batadv_tp_list_find(bat_priv, orig_node->orig);
1075 	if (!tp_vars) {
1076 		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1077 			   "Meter: trying to interrupt an already over connection\n");
1078 		goto out;
1079 	}
1080 
1081 	batadv_tp_sender_shutdown(tp_vars, return_value);
1082 	batadv_tp_vars_put(tp_vars);
1083 out:
1084 	batadv_orig_node_put(orig_node);
1085 }
1086 
1087 /**
1088  * batadv_tp_reset_receiver_timer() - reset the receiver shutdown timer
1089  * @tp_vars: the private data of the current TP meter session
1090  *
1091  * start the receiver shutdown timer or reset it if already started
1092  */
1093 static void batadv_tp_reset_receiver_timer(struct batadv_tp_vars *tp_vars)
1094 {
1095 	mod_timer(&tp_vars->timer,
1096 		  jiffies + msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT));
1097 }
1098 
1099 /**
1100  * batadv_tp_receiver_shutdown() - stop a tp meter receiver when timeout is
1101  *  reached without received ack
1102  * @t: address to timer_list inside tp_vars
1103  */
1104 static void batadv_tp_receiver_shutdown(struct timer_list *t)
1105 {
1106 	struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
1107 	struct batadv_tp_unacked *un, *safe;
1108 	struct batadv_priv *bat_priv;
1109 
1110 	bat_priv = tp_vars->bat_priv;
1111 
1112 	/* if there is recent activity rearm the timer */
1113 	if (!batadv_has_timed_out(tp_vars->last_recv_time,
1114 				  BATADV_TP_RECV_TIMEOUT)) {
1115 		/* reset the receiver shutdown timer */
1116 		batadv_tp_reset_receiver_timer(tp_vars);
1117 		return;
1118 	}
1119 
1120 	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1121 		   "Shutting down for inactivity (more than %dms) from %pM\n",
1122 		   BATADV_TP_RECV_TIMEOUT, tp_vars->other_end);
1123 
1124 	spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
1125 	hlist_del_rcu(&tp_vars->list);
1126 	spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
1127 
1128 	/* drop list reference */
1129 	batadv_tp_vars_put(tp_vars);
1130 
1131 	atomic_dec(&bat_priv->tp_num);
1132 
1133 	spin_lock_bh(&tp_vars->unacked_lock);
1134 	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
1135 		list_del(&un->list);
1136 		kfree(un);
1137 	}
1138 	spin_unlock_bh(&tp_vars->unacked_lock);
1139 
1140 	/* drop reference of timer */
1141 	batadv_tp_vars_put(tp_vars);
1142 }
1143 
1144 /**
1145  * batadv_tp_send_ack() - send an ACK packet
1146  * @bat_priv: the bat priv with all the soft interface information
1147  * @dst: the mac address of the destination originator
1148  * @seq: the sequence number to ACK
1149  * @timestamp: the timestamp to echo back in the ACK
1150  * @session: session identifier
1151  * @socket_index: local ICMP socket identifier
1152  *
1153  * Return: 0 on success, a positive integer representing the reason of the
1154  * failure otherwise
1155  */
1156 static int batadv_tp_send_ack(struct batadv_priv *bat_priv, const u8 *dst,
1157 			      u32 seq, __be32 timestamp, const u8 *session,
1158 			      int socket_index)
1159 {
1160 	struct batadv_hard_iface *primary_if = NULL;
1161 	struct batadv_orig_node *orig_node;
1162 	struct batadv_icmp_tp_packet *icmp;
1163 	struct sk_buff *skb;
1164 	int r, ret;
1165 
1166 	orig_node = batadv_orig_hash_find(bat_priv, dst);
1167 	if (unlikely(!orig_node)) {
1168 		ret = BATADV_TP_REASON_DST_UNREACHABLE;
1169 		goto out;
1170 	}
1171 
1172 	primary_if = batadv_primary_if_get_selected(bat_priv);
1173 	if (unlikely(!primary_if)) {
1174 		ret = BATADV_TP_REASON_DST_UNREACHABLE;
1175 		goto out;
1176 	}
1177 
1178 	skb = netdev_alloc_skb_ip_align(NULL, sizeof(*icmp) + ETH_HLEN);
1179 	if (unlikely(!skb)) {
1180 		ret = BATADV_TP_REASON_MEMORY_ERROR;
1181 		goto out;
1182 	}
1183 
1184 	skb_reserve(skb, ETH_HLEN);
1185 	icmp = skb_put(skb, sizeof(*icmp));
1186 	icmp->packet_type = BATADV_ICMP;
1187 	icmp->version = BATADV_COMPAT_VERSION;
1188 	icmp->ttl = BATADV_TTL;
1189 	icmp->msg_type = BATADV_TP;
1190 	ether_addr_copy(icmp->dst, orig_node->orig);
1191 	ether_addr_copy(icmp->orig, primary_if->net_dev->dev_addr);
1192 	icmp->uid = socket_index;
1193 
1194 	icmp->subtype = BATADV_TP_ACK;
1195 	memcpy(icmp->session, session, sizeof(icmp->session));
1196 	icmp->seqno = htonl(seq);
1197 	icmp->timestamp = timestamp;
1198 
1199 	/* send the ack */
1200 	r = batadv_send_skb_to_orig(skb, orig_node, NULL);
1201 	if (unlikely(r < 0) || r == NET_XMIT_DROP) {
1202 		ret = BATADV_TP_REASON_DST_UNREACHABLE;
1203 		goto out;
1204 	}
1205 	ret = 0;
1206 
1207 out:
1208 	if (likely(orig_node))
1209 		batadv_orig_node_put(orig_node);
1210 	if (likely(primary_if))
1211 		batadv_hardif_put(primary_if);
1212 
1213 	return ret;
1214 }
1215 
1216 /**
1217  * batadv_tp_handle_out_of_order() - store an out of order packet
1218  * @tp_vars: the private data of the current TP meter session
1219  * @skb: the buffer containing the received packet
1220  *
1221  * Store the out of order packet in the unacked list for late processing. This
1222  * packets are kept in this list so that they can be ACKed at once as soon as
1223  * all the previous packets have been received
1224  *
1225  * Return: true if the packed has been successfully processed, false otherwise
1226  */
1227 static bool batadv_tp_handle_out_of_order(struct batadv_tp_vars *tp_vars,
1228 					  const struct sk_buff *skb)
1229 {
1230 	const struct batadv_icmp_tp_packet *icmp;
1231 	struct batadv_tp_unacked *un, *new;
1232 	u32 payload_len;
1233 	bool added = false;
1234 
1235 	new = kmalloc(sizeof(*new), GFP_ATOMIC);
1236 	if (unlikely(!new))
1237 		return false;
1238 
1239 	icmp = (struct batadv_icmp_tp_packet *)skb->data;
1240 
1241 	new->seqno = ntohl(icmp->seqno);
1242 	payload_len = skb->len - sizeof(struct batadv_unicast_packet);
1243 	new->len = payload_len;
1244 
1245 	spin_lock_bh(&tp_vars->unacked_lock);
1246 	/* if the list is empty immediately attach this new object */
1247 	if (list_empty(&tp_vars->unacked_list)) {
1248 		list_add(&new->list, &tp_vars->unacked_list);
1249 		goto out;
1250 	}
1251 
1252 	/* otherwise loop over the list and either drop the packet because this
1253 	 * is a duplicate or store it at the right position.
1254 	 *
1255 	 * The iteration is done in the reverse way because it is likely that
1256 	 * the last received packet (the one being processed now) has a bigger
1257 	 * seqno than all the others already stored.
1258 	 */
1259 	list_for_each_entry_reverse(un, &tp_vars->unacked_list, list) {
1260 		/* check for duplicates */
1261 		if (new->seqno == un->seqno) {
1262 			if (new->len > un->len)
1263 				un->len = new->len;
1264 			kfree(new);
1265 			added = true;
1266 			break;
1267 		}
1268 
1269 		/* look for the right position */
1270 		if (batadv_seq_before(new->seqno, un->seqno))
1271 			continue;
1272 
1273 		/* as soon as an entry having a bigger seqno is found, the new
1274 		 * one is attached _after_ it. In this way the list is kept in
1275 		 * ascending order
1276 		 */
1277 		list_add_tail(&new->list, &un->list);
1278 		added = true;
1279 		break;
1280 	}
1281 
1282 	/* received packet with smallest seqno out of order; add it to front */
1283 	if (!added)
1284 		list_add(&new->list, &tp_vars->unacked_list);
1285 
1286 out:
1287 	spin_unlock_bh(&tp_vars->unacked_lock);
1288 
1289 	return true;
1290 }
1291 
1292 /**
1293  * batadv_tp_ack_unordered() - update number received bytes in current stream
1294  *  without gaps
1295  * @tp_vars: the private data of the current TP meter session
1296  */
1297 static void batadv_tp_ack_unordered(struct batadv_tp_vars *tp_vars)
1298 {
1299 	struct batadv_tp_unacked *un, *safe;
1300 	u32 to_ack;
1301 
1302 	/* go through the unacked packet list and possibly ACK them as
1303 	 * well
1304 	 */
1305 	spin_lock_bh(&tp_vars->unacked_lock);
1306 	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
1307 		/* the list is ordered, therefore it is possible to stop as soon
1308 		 * there is a gap between the last acked seqno and the seqno of
1309 		 * the packet under inspection
1310 		 */
1311 		if (batadv_seq_before(tp_vars->last_recv, un->seqno))
1312 			break;
1313 
1314 		to_ack = un->seqno + un->len - tp_vars->last_recv;
1315 
1316 		if (batadv_seq_before(tp_vars->last_recv, un->seqno + un->len))
1317 			tp_vars->last_recv += to_ack;
1318 
1319 		list_del(&un->list);
1320 		kfree(un);
1321 	}
1322 	spin_unlock_bh(&tp_vars->unacked_lock);
1323 }
1324 
1325 /**
1326  * batadv_tp_init_recv() - return matching or create new receiver tp_vars
1327  * @bat_priv: the bat priv with all the soft interface information
1328  * @icmp: received icmp tp msg
1329  *
1330  * Return: corresponding tp_vars or NULL on errors
1331  */
1332 static struct batadv_tp_vars *
1333 batadv_tp_init_recv(struct batadv_priv *bat_priv,
1334 		    const struct batadv_icmp_tp_packet *icmp)
1335 {
1336 	struct batadv_tp_vars *tp_vars;
1337 
1338 	spin_lock_bh(&bat_priv->tp_list_lock);
1339 	tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
1340 					      icmp->session);
1341 	if (tp_vars)
1342 		goto out_unlock;
1343 
1344 	if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
1345 		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1346 			   "Meter: too many ongoing sessions, aborting (RECV)\n");
1347 		goto out_unlock;
1348 	}
1349 
1350 	tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
1351 	if (!tp_vars)
1352 		goto out_unlock;
1353 
1354 	ether_addr_copy(tp_vars->other_end, icmp->orig);
1355 	tp_vars->role = BATADV_TP_RECEIVER;
1356 	memcpy(tp_vars->session, icmp->session, sizeof(tp_vars->session));
1357 	tp_vars->last_recv = BATADV_TP_FIRST_SEQ;
1358 	tp_vars->bat_priv = bat_priv;
1359 	kref_init(&tp_vars->refcount);
1360 
1361 	spin_lock_init(&tp_vars->unacked_lock);
1362 	INIT_LIST_HEAD(&tp_vars->unacked_list);
1363 
1364 	kref_get(&tp_vars->refcount);
1365 	hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
1366 
1367 	kref_get(&tp_vars->refcount);
1368 	timer_setup(&tp_vars->timer, batadv_tp_receiver_shutdown, 0);
1369 
1370 	batadv_tp_reset_receiver_timer(tp_vars);
1371 
1372 out_unlock:
1373 	spin_unlock_bh(&bat_priv->tp_list_lock);
1374 
1375 	return tp_vars;
1376 }
1377 
1378 /**
1379  * batadv_tp_recv_msg() - process a single data message
1380  * @bat_priv: the bat priv with all the soft interface information
1381  * @skb: the buffer containing the received packet
1382  *
1383  * Process a received TP MSG packet
1384  */
1385 static void batadv_tp_recv_msg(struct batadv_priv *bat_priv,
1386 			       const struct sk_buff *skb)
1387 {
1388 	const struct batadv_icmp_tp_packet *icmp;
1389 	struct batadv_tp_vars *tp_vars;
1390 	size_t packet_size;
1391 	u32 seqno;
1392 
1393 	icmp = (struct batadv_icmp_tp_packet *)skb->data;
1394 
1395 	seqno = ntohl(icmp->seqno);
1396 	/* check if this is the first seqno. This means that if the
1397 	 * first packet is lost, the tp meter does not work anymore!
1398 	 */
1399 	if (seqno == BATADV_TP_FIRST_SEQ) {
1400 		tp_vars = batadv_tp_init_recv(bat_priv, icmp);
1401 		if (!tp_vars) {
1402 			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1403 				   "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
1404 			goto out;
1405 		}
1406 	} else {
1407 		tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
1408 						      icmp->session);
1409 		if (!tp_vars) {
1410 			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1411 				   "Unexpected packet from %pM!\n",
1412 				   icmp->orig);
1413 			goto out;
1414 		}
1415 	}
1416 
1417 	if (unlikely(tp_vars->role != BATADV_TP_RECEIVER)) {
1418 		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1419 			   "Meter: dropping packet: not expected (role=%u)\n",
1420 			   tp_vars->role);
1421 		goto out;
1422 	}
1423 
1424 	tp_vars->last_recv_time = jiffies;
1425 
1426 	/* if the packet is a duplicate, it may be the case that an ACK has been
1427 	 * lost. Resend the ACK
1428 	 */
1429 	if (batadv_seq_before(seqno, tp_vars->last_recv))
1430 		goto send_ack;
1431 
1432 	/* if the packet is out of order enqueue it */
1433 	if (ntohl(icmp->seqno) != tp_vars->last_recv) {
1434 		/* exit immediately (and do not send any ACK) if the packet has
1435 		 * not been enqueued correctly
1436 		 */
1437 		if (!batadv_tp_handle_out_of_order(tp_vars, skb))
1438 			goto out;
1439 
1440 		/* send a duplicate ACK */
1441 		goto send_ack;
1442 	}
1443 
1444 	/* if everything was fine count the ACKed bytes */
1445 	packet_size = skb->len - sizeof(struct batadv_unicast_packet);
1446 	tp_vars->last_recv += packet_size;
1447 
1448 	/* check if this ordered message filled a gap.... */
1449 	batadv_tp_ack_unordered(tp_vars);
1450 
1451 send_ack:
1452 	/* send the ACK. If the received packet was out of order, the ACK that
1453 	 * is going to be sent is a duplicate (the sender will count them and
1454 	 * possibly enter Fast Retransmit as soon as it has reached 3)
1455 	 */
1456 	batadv_tp_send_ack(bat_priv, icmp->orig, tp_vars->last_recv,
1457 			   icmp->timestamp, icmp->session, icmp->uid);
1458 out:
1459 	if (likely(tp_vars))
1460 		batadv_tp_vars_put(tp_vars);
1461 }
1462 
1463 /**
1464  * batadv_tp_meter_recv() - main TP Meter receiving function
1465  * @bat_priv: the bat priv with all the soft interface information
1466  * @skb: the buffer containing the received packet
1467  */
1468 void batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb)
1469 {
1470 	struct batadv_icmp_tp_packet *icmp;
1471 
1472 	icmp = (struct batadv_icmp_tp_packet *)skb->data;
1473 
1474 	switch (icmp->subtype) {
1475 	case BATADV_TP_MSG:
1476 		batadv_tp_recv_msg(bat_priv, skb);
1477 		break;
1478 	case BATADV_TP_ACK:
1479 		batadv_tp_recv_ack(bat_priv, skb);
1480 		break;
1481 	default:
1482 		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1483 			   "Received unknown TP Metric packet type %u\n",
1484 			   icmp->subtype);
1485 	}
1486 	consume_skb(skb);
1487 }
1488 
1489 /**
1490  * batadv_tp_meter_init() - initialize global tp_meter structures
1491  */
1492 void __init batadv_tp_meter_init(void)
1493 {
1494 	get_random_bytes(batadv_tp_prerandom, sizeof(batadv_tp_prerandom));
1495 }
1496