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