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