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