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