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