1 /* Copyright (C) 2012-2015 B.A.T.M.A.N. contributors: 2 * 3 * Martin Hundebøll, Jeppe Ledet-Pedersen 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of version 2 of the GNU General Public 7 * License as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "network-coding.h" 19 #include "main.h" 20 21 #include <linux/atomic.h> 22 #include <linux/bitops.h> 23 #include <linux/byteorder/generic.h> 24 #include <linux/compiler.h> 25 #include <linux/debugfs.h> 26 #include <linux/errno.h> 27 #include <linux/etherdevice.h> 28 #include <linux/fs.h> 29 #include <linux/if_ether.h> 30 #include <linux/if_packet.h> 31 #include <linux/init.h> 32 #include <linux/jhash.h> 33 #include <linux/jiffies.h> 34 #include <linux/kernel.h> 35 #include <linux/list.h> 36 #include <linux/lockdep.h> 37 #include <linux/netdevice.h> 38 #include <linux/printk.h> 39 #include <linux/random.h> 40 #include <linux/rculist.h> 41 #include <linux/rcupdate.h> 42 #include <linux/seq_file.h> 43 #include <linux/skbuff.h> 44 #include <linux/slab.h> 45 #include <linux/spinlock.h> 46 #include <linux/stat.h> 47 #include <linux/stddef.h> 48 #include <linux/string.h> 49 #include <linux/workqueue.h> 50 51 #include "hard-interface.h" 52 #include "hash.h" 53 #include "originator.h" 54 #include "packet.h" 55 #include "routing.h" 56 #include "send.h" 57 58 static struct lock_class_key batadv_nc_coding_hash_lock_class_key; 59 static struct lock_class_key batadv_nc_decoding_hash_lock_class_key; 60 61 static void batadv_nc_worker(struct work_struct *work); 62 static int batadv_nc_recv_coded_packet(struct sk_buff *skb, 63 struct batadv_hard_iface *recv_if); 64 65 /** 66 * batadv_nc_init - one-time initialization for network coding 67 */ 68 int __init batadv_nc_init(void) 69 { 70 int ret; 71 72 /* Register our packet type */ 73 ret = batadv_recv_handler_register(BATADV_CODED, 74 batadv_nc_recv_coded_packet); 75 76 return ret; 77 } 78 79 /** 80 * batadv_nc_start_timer - initialise the nc periodic worker 81 * @bat_priv: the bat priv with all the soft interface information 82 */ 83 static void batadv_nc_start_timer(struct batadv_priv *bat_priv) 84 { 85 queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work, 86 msecs_to_jiffies(10)); 87 } 88 89 /** 90 * batadv_nc_tvlv_container_update - update the network coding tvlv container 91 * after network coding setting change 92 * @bat_priv: the bat priv with all the soft interface information 93 */ 94 static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv) 95 { 96 char nc_mode; 97 98 nc_mode = atomic_read(&bat_priv->network_coding); 99 100 switch (nc_mode) { 101 case 0: 102 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1); 103 break; 104 case 1: 105 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1, 106 NULL, 0); 107 break; 108 } 109 } 110 111 /** 112 * batadv_nc_status_update - update the network coding tvlv container after 113 * network coding setting change 114 * @net_dev: the soft interface net device 115 */ 116 void batadv_nc_status_update(struct net_device *net_dev) 117 { 118 struct batadv_priv *bat_priv = netdev_priv(net_dev); 119 120 batadv_nc_tvlv_container_update(bat_priv); 121 } 122 123 /** 124 * batadv_nc_tvlv_ogm_handler_v1 - process incoming nc tvlv container 125 * @bat_priv: the bat priv with all the soft interface information 126 * @orig: the orig_node of the ogm 127 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) 128 * @tvlv_value: tvlv buffer containing the gateway data 129 * @tvlv_value_len: tvlv buffer length 130 */ 131 static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, 132 struct batadv_orig_node *orig, 133 u8 flags, 134 void *tvlv_value, u16 tvlv_value_len) 135 { 136 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) 137 clear_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities); 138 else 139 set_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities); 140 } 141 142 /** 143 * batadv_nc_mesh_init - initialise coding hash table and start house keeping 144 * @bat_priv: the bat priv with all the soft interface information 145 */ 146 int batadv_nc_mesh_init(struct batadv_priv *bat_priv) 147 { 148 bat_priv->nc.timestamp_fwd_flush = jiffies; 149 bat_priv->nc.timestamp_sniffed_purge = jiffies; 150 151 if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash) 152 return 0; 153 154 bat_priv->nc.coding_hash = batadv_hash_new(128); 155 if (!bat_priv->nc.coding_hash) 156 goto err; 157 158 batadv_hash_set_lock_class(bat_priv->nc.coding_hash, 159 &batadv_nc_coding_hash_lock_class_key); 160 161 bat_priv->nc.decoding_hash = batadv_hash_new(128); 162 if (!bat_priv->nc.decoding_hash) 163 goto err; 164 165 batadv_hash_set_lock_class(bat_priv->nc.decoding_hash, 166 &batadv_nc_decoding_hash_lock_class_key); 167 168 INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker); 169 batadv_nc_start_timer(bat_priv); 170 171 batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1, 172 NULL, BATADV_TVLV_NC, 1, 173 BATADV_TVLV_HANDLER_OGM_CIFNOTFND); 174 batadv_nc_tvlv_container_update(bat_priv); 175 return 0; 176 177 err: 178 return -ENOMEM; 179 } 180 181 /** 182 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables 183 * @bat_priv: the bat priv with all the soft interface information 184 */ 185 void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv) 186 { 187 atomic_set(&bat_priv->network_coding, 0); 188 bat_priv->nc.min_tq = 200; 189 bat_priv->nc.max_fwd_delay = 10; 190 bat_priv->nc.max_buffer_time = 200; 191 } 192 193 /** 194 * batadv_nc_init_orig - initialise the nc fields of an orig_node 195 * @orig_node: the orig_node which is going to be initialised 196 */ 197 void batadv_nc_init_orig(struct batadv_orig_node *orig_node) 198 { 199 INIT_LIST_HEAD(&orig_node->in_coding_list); 200 INIT_LIST_HEAD(&orig_node->out_coding_list); 201 spin_lock_init(&orig_node->in_coding_list_lock); 202 spin_lock_init(&orig_node->out_coding_list_lock); 203 } 204 205 /** 206 * batadv_nc_node_release - release nc_node from lists and queue for free after 207 * rcu grace period 208 * @nc_node: the nc node to free 209 */ 210 static void batadv_nc_node_release(struct batadv_nc_node *nc_node) 211 { 212 batadv_orig_node_free_ref(nc_node->orig_node); 213 kfree_rcu(nc_node, rcu); 214 } 215 216 /** 217 * batadv_nc_node_free_ref - decrement the nc node refcounter and possibly 218 * release it 219 * @nc_node: the nc node to free 220 */ 221 static void batadv_nc_node_free_ref(struct batadv_nc_node *nc_node) 222 { 223 if (atomic_dec_and_test(&nc_node->refcount)) 224 batadv_nc_node_release(nc_node); 225 } 226 227 /** 228 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly 229 * frees it 230 * @nc_path: the nc node to free 231 */ 232 static void batadv_nc_path_free_ref(struct batadv_nc_path *nc_path) 233 { 234 if (atomic_dec_and_test(&nc_path->refcount)) 235 kfree_rcu(nc_path, rcu); 236 } 237 238 /** 239 * batadv_nc_packet_free - frees nc packet 240 * @nc_packet: the nc packet to free 241 */ 242 static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet) 243 { 244 kfree_skb(nc_packet->skb); 245 batadv_nc_path_free_ref(nc_packet->nc_path); 246 kfree(nc_packet); 247 } 248 249 /** 250 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged 251 * @bat_priv: the bat priv with all the soft interface information 252 * @nc_node: the nc node to check 253 * 254 * Returns true if the entry has to be purged now, false otherwise 255 */ 256 static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv, 257 struct batadv_nc_node *nc_node) 258 { 259 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 260 return true; 261 262 return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT); 263 } 264 265 /** 266 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out 267 * @bat_priv: the bat priv with all the soft interface information 268 * @nc_path: the nc path to check 269 * 270 * Returns true if the entry has to be purged now, false otherwise 271 */ 272 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv, 273 struct batadv_nc_path *nc_path) 274 { 275 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 276 return true; 277 278 /* purge the path when no packets has been added for 10 times the 279 * max_fwd_delay time 280 */ 281 return batadv_has_timed_out(nc_path->last_valid, 282 bat_priv->nc.max_fwd_delay * 10); 283 } 284 285 /** 286 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out 287 * @bat_priv: the bat priv with all the soft interface information 288 * @nc_path: the nc path to check 289 * 290 * Returns true if the entry has to be purged now, false otherwise 291 */ 292 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv, 293 struct batadv_nc_path *nc_path) 294 { 295 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 296 return true; 297 298 /* purge the path when no packets has been added for 10 times the 299 * max_buffer time 300 */ 301 return batadv_has_timed_out(nc_path->last_valid, 302 bat_priv->nc.max_buffer_time * 10); 303 } 304 305 /** 306 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale 307 * entries 308 * @bat_priv: the bat priv with all the soft interface information 309 * @list: list of nc nodes 310 * @lock: nc node list lock 311 * @to_purge: function in charge to decide whether an entry has to be purged or 312 * not. This function takes the nc node as argument and has to return 313 * a boolean value: true if the entry has to be deleted, false 314 * otherwise 315 */ 316 static void 317 batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv, 318 struct list_head *list, 319 spinlock_t *lock, 320 bool (*to_purge)(struct batadv_priv *, 321 struct batadv_nc_node *)) 322 { 323 struct batadv_nc_node *nc_node, *nc_node_tmp; 324 325 /* For each nc_node in list */ 326 spin_lock_bh(lock); 327 list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) { 328 /* if an helper function has been passed as parameter, 329 * ask it if the entry has to be purged or not 330 */ 331 if (to_purge && !to_purge(bat_priv, nc_node)) 332 continue; 333 334 batadv_dbg(BATADV_DBG_NC, bat_priv, 335 "Removing nc_node %pM -> %pM\n", 336 nc_node->addr, nc_node->orig_node->orig); 337 list_del_rcu(&nc_node->list); 338 batadv_nc_node_free_ref(nc_node); 339 } 340 spin_unlock_bh(lock); 341 } 342 343 /** 344 * batadv_nc_purge_orig - purges all nc node data attached of the given 345 * originator 346 * @bat_priv: the bat priv with all the soft interface information 347 * @orig_node: orig_node with the nc node entries to be purged 348 * @to_purge: function in charge to decide whether an entry has to be purged or 349 * not. This function takes the nc node as argument and has to return 350 * a boolean value: true is the entry has to be deleted, false 351 * otherwise 352 */ 353 void batadv_nc_purge_orig(struct batadv_priv *bat_priv, 354 struct batadv_orig_node *orig_node, 355 bool (*to_purge)(struct batadv_priv *, 356 struct batadv_nc_node *)) 357 { 358 /* Check ingoing nc_node's of this orig_node */ 359 batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list, 360 &orig_node->in_coding_list_lock, 361 to_purge); 362 363 /* Check outgoing nc_node's of this orig_node */ 364 batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list, 365 &orig_node->out_coding_list_lock, 366 to_purge); 367 } 368 369 /** 370 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they 371 * have timed out nc nodes 372 * @bat_priv: the bat priv with all the soft interface information 373 */ 374 static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv) 375 { 376 struct batadv_hashtable *hash = bat_priv->orig_hash; 377 struct hlist_head *head; 378 struct batadv_orig_node *orig_node; 379 u32 i; 380 381 if (!hash) 382 return; 383 384 /* For each orig_node */ 385 for (i = 0; i < hash->size; i++) { 386 head = &hash->table[i]; 387 388 rcu_read_lock(); 389 hlist_for_each_entry_rcu(orig_node, head, hash_entry) 390 batadv_nc_purge_orig(bat_priv, orig_node, 391 batadv_nc_to_purge_nc_node); 392 rcu_read_unlock(); 393 } 394 } 395 396 /** 397 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove 398 * unused ones 399 * @bat_priv: the bat priv with all the soft interface information 400 * @hash: hash table containing the nc paths to check 401 * @to_purge: function in charge to decide whether an entry has to be purged or 402 * not. This function takes the nc node as argument and has to return 403 * a boolean value: true is the entry has to be deleted, false 404 * otherwise 405 */ 406 static void batadv_nc_purge_paths(struct batadv_priv *bat_priv, 407 struct batadv_hashtable *hash, 408 bool (*to_purge)(struct batadv_priv *, 409 struct batadv_nc_path *)) 410 { 411 struct hlist_head *head; 412 struct hlist_node *node_tmp; 413 struct batadv_nc_path *nc_path; 414 spinlock_t *lock; /* Protects lists in hash */ 415 u32 i; 416 417 for (i = 0; i < hash->size; i++) { 418 head = &hash->table[i]; 419 lock = &hash->list_locks[i]; 420 421 /* For each nc_path in this bin */ 422 spin_lock_bh(lock); 423 hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) { 424 /* if an helper function has been passed as parameter, 425 * ask it if the entry has to be purged or not 426 */ 427 if (to_purge && !to_purge(bat_priv, nc_path)) 428 continue; 429 430 /* purging an non-empty nc_path should never happen, but 431 * is observed under high CPU load. Delay the purging 432 * until next iteration to allow the packet_list to be 433 * emptied first. 434 */ 435 if (!unlikely(list_empty(&nc_path->packet_list))) { 436 net_ratelimited_function(printk, 437 KERN_WARNING 438 "Skipping free of non-empty nc_path (%pM -> %pM)!\n", 439 nc_path->prev_hop, 440 nc_path->next_hop); 441 continue; 442 } 443 444 /* nc_path is unused, so remove it */ 445 batadv_dbg(BATADV_DBG_NC, bat_priv, 446 "Remove nc_path %pM -> %pM\n", 447 nc_path->prev_hop, nc_path->next_hop); 448 hlist_del_rcu(&nc_path->hash_entry); 449 batadv_nc_path_free_ref(nc_path); 450 } 451 spin_unlock_bh(lock); 452 } 453 } 454 455 /** 456 * batadv_nc_hash_key_gen - computes the nc_path hash key 457 * @key: buffer to hold the final hash key 458 * @src: source ethernet mac address going into the hash key 459 * @dst: destination ethernet mac address going into the hash key 460 */ 461 static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src, 462 const char *dst) 463 { 464 memcpy(key->prev_hop, src, sizeof(key->prev_hop)); 465 memcpy(key->next_hop, dst, sizeof(key->next_hop)); 466 } 467 468 /** 469 * batadv_nc_hash_choose - compute the hash value for an nc path 470 * @data: data to hash 471 * @size: size of the hash table 472 * 473 * Returns the selected index in the hash table for the given data. 474 */ 475 static u32 batadv_nc_hash_choose(const void *data, u32 size) 476 { 477 const struct batadv_nc_path *nc_path = data; 478 u32 hash = 0; 479 480 hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash); 481 hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash); 482 483 return hash % size; 484 } 485 486 /** 487 * batadv_nc_hash_compare - comparing function used in the network coding hash 488 * tables 489 * @node: node in the local table 490 * @data2: second object to compare the node to 491 * 492 * Returns 1 if the two entry are the same, 0 otherwise 493 */ 494 static int batadv_nc_hash_compare(const struct hlist_node *node, 495 const void *data2) 496 { 497 const struct batadv_nc_path *nc_path1, *nc_path2; 498 499 nc_path1 = container_of(node, struct batadv_nc_path, hash_entry); 500 nc_path2 = data2; 501 502 /* Return 1 if the two keys are identical */ 503 if (memcmp(nc_path1->prev_hop, nc_path2->prev_hop, 504 sizeof(nc_path1->prev_hop)) != 0) 505 return 0; 506 507 if (memcmp(nc_path1->next_hop, nc_path2->next_hop, 508 sizeof(nc_path1->next_hop)) != 0) 509 return 0; 510 511 return 1; 512 } 513 514 /** 515 * batadv_nc_hash_find - search for an existing nc path and return it 516 * @hash: hash table containing the nc path 517 * @data: search key 518 * 519 * Returns the nc_path if found, NULL otherwise. 520 */ 521 static struct batadv_nc_path * 522 batadv_nc_hash_find(struct batadv_hashtable *hash, 523 void *data) 524 { 525 struct hlist_head *head; 526 struct batadv_nc_path *nc_path, *nc_path_tmp = NULL; 527 int index; 528 529 if (!hash) 530 return NULL; 531 532 index = batadv_nc_hash_choose(data, hash->size); 533 head = &hash->table[index]; 534 535 rcu_read_lock(); 536 hlist_for_each_entry_rcu(nc_path, head, hash_entry) { 537 if (!batadv_nc_hash_compare(&nc_path->hash_entry, data)) 538 continue; 539 540 if (!atomic_inc_not_zero(&nc_path->refcount)) 541 continue; 542 543 nc_path_tmp = nc_path; 544 break; 545 } 546 rcu_read_unlock(); 547 548 return nc_path_tmp; 549 } 550 551 /** 552 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct 553 * @nc_packet: the nc packet to send 554 */ 555 static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet) 556 { 557 batadv_send_skb_packet(nc_packet->skb, 558 nc_packet->neigh_node->if_incoming, 559 nc_packet->nc_path->next_hop); 560 nc_packet->skb = NULL; 561 batadv_nc_packet_free(nc_packet); 562 } 563 564 /** 565 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet. 566 * @bat_priv: the bat priv with all the soft interface information 567 * @nc_path: the nc path the packet belongs to 568 * @nc_packet: the nc packet to be checked 569 * 570 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering 571 * timeout. If so, the packet is no longer kept and the entry deleted from the 572 * queue. Has to be called with the appropriate locks. 573 * 574 * Returns false as soon as the entry in the fifo queue has not been timed out 575 * yet and true otherwise. 576 */ 577 static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv, 578 struct batadv_nc_path *nc_path, 579 struct batadv_nc_packet *nc_packet) 580 { 581 unsigned long timeout = bat_priv->nc.max_buffer_time; 582 bool res = false; 583 584 lockdep_assert_held(&nc_path->packet_list_lock); 585 586 /* Packets are added to tail, so the remaining packets did not time 587 * out and we can stop processing the current queue 588 */ 589 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE && 590 !batadv_has_timed_out(nc_packet->timestamp, timeout)) 591 goto out; 592 593 /* purge nc packet */ 594 list_del(&nc_packet->list); 595 batadv_nc_packet_free(nc_packet); 596 597 res = true; 598 599 out: 600 return res; 601 } 602 603 /** 604 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet. 605 * @bat_priv: the bat priv with all the soft interface information 606 * @nc_path: the nc path the packet belongs to 607 * @nc_packet: the nc packet to be checked 608 * 609 * Checks whether the given nc packet has hit its forward timeout. If so, the 610 * packet is no longer delayed, immediately sent and the entry deleted from the 611 * queue. Has to be called with the appropriate locks. 612 * 613 * Returns false as soon as the entry in the fifo queue has not been timed out 614 * yet and true otherwise. 615 */ 616 static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv, 617 struct batadv_nc_path *nc_path, 618 struct batadv_nc_packet *nc_packet) 619 { 620 unsigned long timeout = bat_priv->nc.max_fwd_delay; 621 622 lockdep_assert_held(&nc_path->packet_list_lock); 623 624 /* Packets are added to tail, so the remaining packets did not time 625 * out and we can stop processing the current queue 626 */ 627 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE && 628 !batadv_has_timed_out(nc_packet->timestamp, timeout)) 629 return false; 630 631 /* Send packet */ 632 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD); 633 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES, 634 nc_packet->skb->len + ETH_HLEN); 635 list_del(&nc_packet->list); 636 batadv_nc_send_packet(nc_packet); 637 638 return true; 639 } 640 641 /** 642 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out 643 * nc packets 644 * @bat_priv: the bat priv with all the soft interface information 645 * @hash: to be processed hash table 646 * @process_fn: Function called to process given nc packet. Should return true 647 * to encourage this function to proceed with the next packet. 648 * Otherwise the rest of the current queue is skipped. 649 */ 650 static void 651 batadv_nc_process_nc_paths(struct batadv_priv *bat_priv, 652 struct batadv_hashtable *hash, 653 bool (*process_fn)(struct batadv_priv *, 654 struct batadv_nc_path *, 655 struct batadv_nc_packet *)) 656 { 657 struct hlist_head *head; 658 struct batadv_nc_packet *nc_packet, *nc_packet_tmp; 659 struct batadv_nc_path *nc_path; 660 bool ret; 661 int i; 662 663 if (!hash) 664 return; 665 666 /* Loop hash table bins */ 667 for (i = 0; i < hash->size; i++) { 668 head = &hash->table[i]; 669 670 /* Loop coding paths */ 671 rcu_read_lock(); 672 hlist_for_each_entry_rcu(nc_path, head, hash_entry) { 673 /* Loop packets */ 674 spin_lock_bh(&nc_path->packet_list_lock); 675 list_for_each_entry_safe(nc_packet, nc_packet_tmp, 676 &nc_path->packet_list, list) { 677 ret = process_fn(bat_priv, nc_path, nc_packet); 678 if (!ret) 679 break; 680 } 681 spin_unlock_bh(&nc_path->packet_list_lock); 682 } 683 rcu_read_unlock(); 684 } 685 } 686 687 /** 688 * batadv_nc_worker - periodic task for house keeping related to network coding 689 * @work: kernel work struct 690 */ 691 static void batadv_nc_worker(struct work_struct *work) 692 { 693 struct delayed_work *delayed_work; 694 struct batadv_priv_nc *priv_nc; 695 struct batadv_priv *bat_priv; 696 unsigned long timeout; 697 698 delayed_work = container_of(work, struct delayed_work, work); 699 priv_nc = container_of(delayed_work, struct batadv_priv_nc, work); 700 bat_priv = container_of(priv_nc, struct batadv_priv, nc); 701 702 batadv_nc_purge_orig_hash(bat_priv); 703 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, 704 batadv_nc_to_purge_nc_path_coding); 705 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, 706 batadv_nc_to_purge_nc_path_decoding); 707 708 timeout = bat_priv->nc.max_fwd_delay; 709 710 if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) { 711 batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash, 712 batadv_nc_fwd_flush); 713 bat_priv->nc.timestamp_fwd_flush = jiffies; 714 } 715 716 if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge, 717 bat_priv->nc.max_buffer_time)) { 718 batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash, 719 batadv_nc_sniffed_purge); 720 bat_priv->nc.timestamp_sniffed_purge = jiffies; 721 } 722 723 /* Schedule a new check */ 724 batadv_nc_start_timer(bat_priv); 725 } 726 727 /** 728 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for 729 * coding or not 730 * @bat_priv: the bat priv with all the soft interface information 731 * @orig_node: neighboring orig node which may be used as nc candidate 732 * @ogm_packet: incoming ogm packet also used for the checks 733 * 734 * Returns true if: 735 * 1) The OGM must have the most recent sequence number. 736 * 2) The TTL must be decremented by one and only one. 737 * 3) The OGM must be received from the first hop from orig_node. 738 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq. 739 */ 740 static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv, 741 struct batadv_orig_node *orig_node, 742 struct batadv_ogm_packet *ogm_packet) 743 { 744 struct batadv_orig_ifinfo *orig_ifinfo; 745 u32 last_real_seqno; 746 u8 last_ttl; 747 748 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT); 749 if (!orig_ifinfo) 750 return false; 751 752 last_ttl = orig_ifinfo->last_ttl; 753 last_real_seqno = orig_ifinfo->last_real_seqno; 754 batadv_orig_ifinfo_free_ref(orig_ifinfo); 755 756 if (last_real_seqno != ntohl(ogm_packet->seqno)) 757 return false; 758 if (last_ttl != ogm_packet->ttl + 1) 759 return false; 760 if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender)) 761 return false; 762 if (ogm_packet->tq < bat_priv->nc.min_tq) 763 return false; 764 765 return true; 766 } 767 768 /** 769 * batadv_nc_find_nc_node - search for an existing nc node and return it 770 * @orig_node: orig node originating the ogm packet 771 * @orig_neigh_node: neighboring orig node from which we received the ogm packet 772 * (can be equal to orig_node) 773 * @in_coding: traverse incoming or outgoing network coding list 774 * 775 * Returns the nc_node if found, NULL otherwise. 776 */ 777 static struct batadv_nc_node 778 *batadv_nc_find_nc_node(struct batadv_orig_node *orig_node, 779 struct batadv_orig_node *orig_neigh_node, 780 bool in_coding) 781 { 782 struct batadv_nc_node *nc_node, *nc_node_out = NULL; 783 struct list_head *list; 784 785 if (in_coding) 786 list = &orig_neigh_node->in_coding_list; 787 else 788 list = &orig_neigh_node->out_coding_list; 789 790 /* Traverse list of nc_nodes to orig_node */ 791 rcu_read_lock(); 792 list_for_each_entry_rcu(nc_node, list, list) { 793 if (!batadv_compare_eth(nc_node->addr, orig_node->orig)) 794 continue; 795 796 if (!atomic_inc_not_zero(&nc_node->refcount)) 797 continue; 798 799 /* Found a match */ 800 nc_node_out = nc_node; 801 break; 802 } 803 rcu_read_unlock(); 804 805 return nc_node_out; 806 } 807 808 /** 809 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was 810 * not found 811 * @bat_priv: the bat priv with all the soft interface information 812 * @orig_node: orig node originating the ogm packet 813 * @orig_neigh_node: neighboring orig node from which we received the ogm packet 814 * (can be equal to orig_node) 815 * @in_coding: traverse incoming or outgoing network coding list 816 * 817 * Returns the nc_node if found or created, NULL in case of an error. 818 */ 819 static struct batadv_nc_node 820 *batadv_nc_get_nc_node(struct batadv_priv *bat_priv, 821 struct batadv_orig_node *orig_node, 822 struct batadv_orig_node *orig_neigh_node, 823 bool in_coding) 824 { 825 struct batadv_nc_node *nc_node; 826 spinlock_t *lock; /* Used to lock list selected by "int in_coding" */ 827 struct list_head *list; 828 829 /* Check if nc_node is already added */ 830 nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding); 831 832 /* Node found */ 833 if (nc_node) 834 return nc_node; 835 836 nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC); 837 if (!nc_node) 838 return NULL; 839 840 if (!atomic_inc_not_zero(&orig_neigh_node->refcount)) 841 goto free; 842 843 /* Initialize nc_node */ 844 INIT_LIST_HEAD(&nc_node->list); 845 ether_addr_copy(nc_node->addr, orig_node->orig); 846 nc_node->orig_node = orig_neigh_node; 847 atomic_set(&nc_node->refcount, 2); 848 849 /* Select ingoing or outgoing coding node */ 850 if (in_coding) { 851 lock = &orig_neigh_node->in_coding_list_lock; 852 list = &orig_neigh_node->in_coding_list; 853 } else { 854 lock = &orig_neigh_node->out_coding_list_lock; 855 list = &orig_neigh_node->out_coding_list; 856 } 857 858 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n", 859 nc_node->addr, nc_node->orig_node->orig); 860 861 /* Add nc_node to orig_node */ 862 spin_lock_bh(lock); 863 list_add_tail_rcu(&nc_node->list, list); 864 spin_unlock_bh(lock); 865 866 return nc_node; 867 868 free: 869 kfree(nc_node); 870 return NULL; 871 } 872 873 /** 874 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node 875 * structs (best called on incoming OGMs) 876 * @bat_priv: the bat priv with all the soft interface information 877 * @orig_node: orig node originating the ogm packet 878 * @orig_neigh_node: neighboring orig node from which we received the ogm packet 879 * (can be equal to orig_node) 880 * @ogm_packet: incoming ogm packet 881 * @is_single_hop_neigh: orig_node is a single hop neighbor 882 */ 883 void batadv_nc_update_nc_node(struct batadv_priv *bat_priv, 884 struct batadv_orig_node *orig_node, 885 struct batadv_orig_node *orig_neigh_node, 886 struct batadv_ogm_packet *ogm_packet, 887 int is_single_hop_neigh) 888 { 889 struct batadv_nc_node *in_nc_node = NULL; 890 struct batadv_nc_node *out_nc_node = NULL; 891 892 /* Check if network coding is enabled */ 893 if (!atomic_read(&bat_priv->network_coding)) 894 goto out; 895 896 /* check if orig node is network coding enabled */ 897 if (!test_bit(BATADV_ORIG_CAPA_HAS_NC, &orig_node->capabilities)) 898 goto out; 899 900 /* accept ogms from 'good' neighbors and single hop neighbors */ 901 if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) && 902 !is_single_hop_neigh) 903 goto out; 904 905 /* Add orig_node as in_nc_node on hop */ 906 in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node, 907 orig_neigh_node, true); 908 if (!in_nc_node) 909 goto out; 910 911 in_nc_node->last_seen = jiffies; 912 913 /* Add hop as out_nc_node on orig_node */ 914 out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node, 915 orig_node, false); 916 if (!out_nc_node) 917 goto out; 918 919 out_nc_node->last_seen = jiffies; 920 921 out: 922 if (in_nc_node) 923 batadv_nc_node_free_ref(in_nc_node); 924 if (out_nc_node) 925 batadv_nc_node_free_ref(out_nc_node); 926 } 927 928 /** 929 * batadv_nc_get_path - get existing nc_path or allocate a new one 930 * @bat_priv: the bat priv with all the soft interface information 931 * @hash: hash table containing the nc path 932 * @src: ethernet source address - first half of the nc path search key 933 * @dst: ethernet destination address - second half of the nc path search key 934 * 935 * Returns pointer to nc_path if the path was found or created, returns NULL 936 * on error. 937 */ 938 static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv, 939 struct batadv_hashtable *hash, 940 u8 *src, 941 u8 *dst) 942 { 943 int hash_added; 944 struct batadv_nc_path *nc_path, nc_path_key; 945 946 batadv_nc_hash_key_gen(&nc_path_key, src, dst); 947 948 /* Search for existing nc_path */ 949 nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key); 950 951 if (nc_path) { 952 /* Set timestamp to delay removal of nc_path */ 953 nc_path->last_valid = jiffies; 954 return nc_path; 955 } 956 957 /* No existing nc_path was found; create a new */ 958 nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC); 959 960 if (!nc_path) 961 return NULL; 962 963 /* Initialize nc_path */ 964 INIT_LIST_HEAD(&nc_path->packet_list); 965 spin_lock_init(&nc_path->packet_list_lock); 966 atomic_set(&nc_path->refcount, 2); 967 nc_path->last_valid = jiffies; 968 ether_addr_copy(nc_path->next_hop, dst); 969 ether_addr_copy(nc_path->prev_hop, src); 970 971 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n", 972 nc_path->prev_hop, 973 nc_path->next_hop); 974 975 /* Add nc_path to hash table */ 976 hash_added = batadv_hash_add(hash, batadv_nc_hash_compare, 977 batadv_nc_hash_choose, &nc_path_key, 978 &nc_path->hash_entry); 979 980 if (hash_added < 0) { 981 kfree(nc_path); 982 return NULL; 983 } 984 985 return nc_path; 986 } 987 988 /** 989 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair 990 * selection of a receiver with slightly lower TQ than the other 991 * @tq: to be weighted tq value 992 */ 993 static u8 batadv_nc_random_weight_tq(u8 tq) 994 { 995 u8 rand_val, rand_tq; 996 997 get_random_bytes(&rand_val, sizeof(rand_val)); 998 999 /* randomize the estimated packet loss (max TQ - estimated TQ) */ 1000 rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq); 1001 1002 /* normalize the randomized packet loss */ 1003 rand_tq /= BATADV_TQ_MAX_VALUE; 1004 1005 /* convert to (randomized) estimated tq again */ 1006 return BATADV_TQ_MAX_VALUE - rand_tq; 1007 } 1008 1009 /** 1010 * batadv_nc_memxor - XOR destination with source 1011 * @dst: byte array to XOR into 1012 * @src: byte array to XOR from 1013 * @len: length of destination array 1014 */ 1015 static void batadv_nc_memxor(char *dst, const char *src, unsigned int len) 1016 { 1017 unsigned int i; 1018 1019 for (i = 0; i < len; ++i) 1020 dst[i] ^= src[i]; 1021 } 1022 1023 /** 1024 * batadv_nc_code_packets - code a received unicast_packet with an nc packet 1025 * into a coded_packet and send it 1026 * @bat_priv: the bat priv with all the soft interface information 1027 * @skb: data skb to forward 1028 * @ethhdr: pointer to the ethernet header inside the skb 1029 * @nc_packet: structure containing the packet to the skb can be coded with 1030 * @neigh_node: next hop to forward packet to 1031 * 1032 * Returns true if both packets are consumed, false otherwise. 1033 */ 1034 static bool batadv_nc_code_packets(struct batadv_priv *bat_priv, 1035 struct sk_buff *skb, 1036 struct ethhdr *ethhdr, 1037 struct batadv_nc_packet *nc_packet, 1038 struct batadv_neigh_node *neigh_node) 1039 { 1040 u8 tq_weighted_neigh, tq_weighted_coding, tq_tmp; 1041 struct sk_buff *skb_dest, *skb_src; 1042 struct batadv_unicast_packet *packet1; 1043 struct batadv_unicast_packet *packet2; 1044 struct batadv_coded_packet *coded_packet; 1045 struct batadv_neigh_node *neigh_tmp, *router_neigh; 1046 struct batadv_neigh_node *router_coding = NULL; 1047 struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL; 1048 struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL; 1049 u8 *first_source, *first_dest, *second_source, *second_dest; 1050 __be32 packet_id1, packet_id2; 1051 size_t count; 1052 bool res = false; 1053 int coding_len; 1054 int unicast_size = sizeof(*packet1); 1055 int coded_size = sizeof(*coded_packet); 1056 int header_add = coded_size - unicast_size; 1057 1058 /* TODO: do we need to consider the outgoing interface for 1059 * coded packets? 1060 */ 1061 router_neigh = batadv_orig_router_get(neigh_node->orig_node, 1062 BATADV_IF_DEFAULT); 1063 if (!router_neigh) 1064 goto out; 1065 1066 router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh, 1067 BATADV_IF_DEFAULT); 1068 if (!router_neigh_ifinfo) 1069 goto out; 1070 1071 neigh_tmp = nc_packet->neigh_node; 1072 router_coding = batadv_orig_router_get(neigh_tmp->orig_node, 1073 BATADV_IF_DEFAULT); 1074 if (!router_coding) 1075 goto out; 1076 1077 router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding, 1078 BATADV_IF_DEFAULT); 1079 if (!router_coding_ifinfo) 1080 goto out; 1081 1082 tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg; 1083 tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp); 1084 tq_tmp = router_coding_ifinfo->bat_iv.tq_avg; 1085 tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp); 1086 1087 /* Select one destination for the MAC-header dst-field based on 1088 * weighted TQ-values. 1089 */ 1090 if (tq_weighted_neigh >= tq_weighted_coding) { 1091 /* Destination from nc_packet is selected for MAC-header */ 1092 first_dest = nc_packet->nc_path->next_hop; 1093 first_source = nc_packet->nc_path->prev_hop; 1094 second_dest = neigh_node->addr; 1095 second_source = ethhdr->h_source; 1096 packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data; 1097 packet2 = (struct batadv_unicast_packet *)skb->data; 1098 packet_id1 = nc_packet->packet_id; 1099 packet_id2 = batadv_skb_crc32(skb, 1100 skb->data + sizeof(*packet2)); 1101 } else { 1102 /* Destination for skb is selected for MAC-header */ 1103 first_dest = neigh_node->addr; 1104 first_source = ethhdr->h_source; 1105 second_dest = nc_packet->nc_path->next_hop; 1106 second_source = nc_packet->nc_path->prev_hop; 1107 packet1 = (struct batadv_unicast_packet *)skb->data; 1108 packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data; 1109 packet_id1 = batadv_skb_crc32(skb, 1110 skb->data + sizeof(*packet1)); 1111 packet_id2 = nc_packet->packet_id; 1112 } 1113 1114 /* Instead of zero padding the smallest data buffer, we 1115 * code into the largest. 1116 */ 1117 if (skb->len <= nc_packet->skb->len) { 1118 skb_dest = nc_packet->skb; 1119 skb_src = skb; 1120 } else { 1121 skb_dest = skb; 1122 skb_src = nc_packet->skb; 1123 } 1124 1125 /* coding_len is used when decoding the packet shorter packet */ 1126 coding_len = skb_src->len - unicast_size; 1127 1128 if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0) 1129 goto out; 1130 1131 skb_push(skb_dest, header_add); 1132 1133 coded_packet = (struct batadv_coded_packet *)skb_dest->data; 1134 skb_reset_mac_header(skb_dest); 1135 1136 coded_packet->packet_type = BATADV_CODED; 1137 coded_packet->version = BATADV_COMPAT_VERSION; 1138 coded_packet->ttl = packet1->ttl; 1139 1140 /* Info about first unicast packet */ 1141 ether_addr_copy(coded_packet->first_source, first_source); 1142 ether_addr_copy(coded_packet->first_orig_dest, packet1->dest); 1143 coded_packet->first_crc = packet_id1; 1144 coded_packet->first_ttvn = packet1->ttvn; 1145 1146 /* Info about second unicast packet */ 1147 ether_addr_copy(coded_packet->second_dest, second_dest); 1148 ether_addr_copy(coded_packet->second_source, second_source); 1149 ether_addr_copy(coded_packet->second_orig_dest, packet2->dest); 1150 coded_packet->second_crc = packet_id2; 1151 coded_packet->second_ttl = packet2->ttl; 1152 coded_packet->second_ttvn = packet2->ttvn; 1153 coded_packet->coded_len = htons(coding_len); 1154 1155 /* This is where the magic happens: Code skb_src into skb_dest */ 1156 batadv_nc_memxor(skb_dest->data + coded_size, 1157 skb_src->data + unicast_size, coding_len); 1158 1159 /* Update counters accordingly */ 1160 if (BATADV_SKB_CB(skb_src)->decoded && 1161 BATADV_SKB_CB(skb_dest)->decoded) { 1162 /* Both packets are recoded */ 1163 count = skb_src->len + ETH_HLEN; 1164 count += skb_dest->len + ETH_HLEN; 1165 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2); 1166 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count); 1167 } else if (!BATADV_SKB_CB(skb_src)->decoded && 1168 !BATADV_SKB_CB(skb_dest)->decoded) { 1169 /* Both packets are newly coded */ 1170 count = skb_src->len + ETH_HLEN; 1171 count += skb_dest->len + ETH_HLEN; 1172 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2); 1173 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count); 1174 } else if (BATADV_SKB_CB(skb_src)->decoded && 1175 !BATADV_SKB_CB(skb_dest)->decoded) { 1176 /* skb_src recoded and skb_dest is newly coded */ 1177 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE); 1178 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, 1179 skb_src->len + ETH_HLEN); 1180 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE); 1181 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, 1182 skb_dest->len + ETH_HLEN); 1183 } else if (!BATADV_SKB_CB(skb_src)->decoded && 1184 BATADV_SKB_CB(skb_dest)->decoded) { 1185 /* skb_src is newly coded and skb_dest is recoded */ 1186 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE); 1187 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, 1188 skb_src->len + ETH_HLEN); 1189 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE); 1190 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, 1191 skb_dest->len + ETH_HLEN); 1192 } 1193 1194 /* skb_src is now coded into skb_dest, so free it */ 1195 kfree_skb(skb_src); 1196 1197 /* avoid duplicate free of skb from nc_packet */ 1198 nc_packet->skb = NULL; 1199 batadv_nc_packet_free(nc_packet); 1200 1201 /* Send the coded packet and return true */ 1202 batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest); 1203 res = true; 1204 out: 1205 if (router_neigh) 1206 batadv_neigh_node_free_ref(router_neigh); 1207 if (router_coding) 1208 batadv_neigh_node_free_ref(router_coding); 1209 if (router_neigh_ifinfo) 1210 batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo); 1211 if (router_coding_ifinfo) 1212 batadv_neigh_ifinfo_free_ref(router_coding_ifinfo); 1213 return res; 1214 } 1215 1216 /** 1217 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst. 1218 * @skb: data skb to forward 1219 * @dst: destination mac address of the other skb to code with 1220 * @src: source mac address of skb 1221 * 1222 * Whenever we network code a packet we have to check whether we received it in 1223 * a network coded form. If so, we may not be able to use it for coding because 1224 * some neighbors may also have received (overheard) the packet in the network 1225 * coded form without being able to decode it. It is hard to know which of the 1226 * neighboring nodes was able to decode the packet, therefore we can only 1227 * re-code the packet if the source of the previous encoded packet is involved. 1228 * Since the source encoded the packet we can be certain it has all necessary 1229 * decode information. 1230 * 1231 * Returns true if coding of a decoded packet is allowed. 1232 */ 1233 static bool batadv_nc_skb_coding_possible(struct sk_buff *skb, u8 *dst, u8 *src) 1234 { 1235 if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src)) 1236 return false; 1237 return true; 1238 } 1239 1240 /** 1241 * batadv_nc_path_search - Find the coding path matching in_nc_node and 1242 * out_nc_node to retrieve a buffered packet that can be used for coding. 1243 * @bat_priv: the bat priv with all the soft interface information 1244 * @in_nc_node: pointer to skb next hop's neighbor nc node 1245 * @out_nc_node: pointer to skb source's neighbor nc node 1246 * @skb: data skb to forward 1247 * @eth_dst: next hop mac address of skb 1248 * 1249 * Returns true if coding of a decoded skb is allowed. 1250 */ 1251 static struct batadv_nc_packet * 1252 batadv_nc_path_search(struct batadv_priv *bat_priv, 1253 struct batadv_nc_node *in_nc_node, 1254 struct batadv_nc_node *out_nc_node, 1255 struct sk_buff *skb, 1256 u8 *eth_dst) 1257 { 1258 struct batadv_nc_path *nc_path, nc_path_key; 1259 struct batadv_nc_packet *nc_packet_out = NULL; 1260 struct batadv_nc_packet *nc_packet, *nc_packet_tmp; 1261 struct batadv_hashtable *hash = bat_priv->nc.coding_hash; 1262 int idx; 1263 1264 if (!hash) 1265 return NULL; 1266 1267 /* Create almost path key */ 1268 batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr, 1269 out_nc_node->addr); 1270 idx = batadv_nc_hash_choose(&nc_path_key, hash->size); 1271 1272 /* Check for coding opportunities in this nc_path */ 1273 rcu_read_lock(); 1274 hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) { 1275 if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr)) 1276 continue; 1277 1278 if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr)) 1279 continue; 1280 1281 spin_lock_bh(&nc_path->packet_list_lock); 1282 if (list_empty(&nc_path->packet_list)) { 1283 spin_unlock_bh(&nc_path->packet_list_lock); 1284 continue; 1285 } 1286 1287 list_for_each_entry_safe(nc_packet, nc_packet_tmp, 1288 &nc_path->packet_list, list) { 1289 if (!batadv_nc_skb_coding_possible(nc_packet->skb, 1290 eth_dst, 1291 in_nc_node->addr)) 1292 continue; 1293 1294 /* Coding opportunity is found! */ 1295 list_del(&nc_packet->list); 1296 nc_packet_out = nc_packet; 1297 break; 1298 } 1299 1300 spin_unlock_bh(&nc_path->packet_list_lock); 1301 break; 1302 } 1303 rcu_read_unlock(); 1304 1305 return nc_packet_out; 1306 } 1307 1308 /** 1309 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the 1310 * skb's sender (may be equal to the originator). 1311 * @bat_priv: the bat priv with all the soft interface information 1312 * @skb: data skb to forward 1313 * @eth_dst: next hop mac address of skb 1314 * @eth_src: source mac address of skb 1315 * @in_nc_node: pointer to skb next hop's neighbor nc node 1316 * 1317 * Returns an nc packet if a suitable coding packet was found, NULL otherwise. 1318 */ 1319 static struct batadv_nc_packet * 1320 batadv_nc_skb_src_search(struct batadv_priv *bat_priv, 1321 struct sk_buff *skb, 1322 u8 *eth_dst, 1323 u8 *eth_src, 1324 struct batadv_nc_node *in_nc_node) 1325 { 1326 struct batadv_orig_node *orig_node; 1327 struct batadv_nc_node *out_nc_node; 1328 struct batadv_nc_packet *nc_packet = NULL; 1329 1330 orig_node = batadv_orig_hash_find(bat_priv, eth_src); 1331 if (!orig_node) 1332 return NULL; 1333 1334 rcu_read_lock(); 1335 list_for_each_entry_rcu(out_nc_node, 1336 &orig_node->out_coding_list, list) { 1337 /* Check if the skb is decoded and if recoding is possible */ 1338 if (!batadv_nc_skb_coding_possible(skb, 1339 out_nc_node->addr, eth_src)) 1340 continue; 1341 1342 /* Search for an opportunity in this nc_path */ 1343 nc_packet = batadv_nc_path_search(bat_priv, in_nc_node, 1344 out_nc_node, skb, eth_dst); 1345 if (nc_packet) 1346 break; 1347 } 1348 rcu_read_unlock(); 1349 1350 batadv_orig_node_free_ref(orig_node); 1351 return nc_packet; 1352 } 1353 1354 /** 1355 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the 1356 * unicast skb before it is stored for use in later decoding 1357 * @bat_priv: the bat priv with all the soft interface information 1358 * @skb: data skb to store 1359 * @eth_dst_new: new destination mac address of skb 1360 */ 1361 static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv, 1362 struct sk_buff *skb, 1363 u8 *eth_dst_new) 1364 { 1365 struct ethhdr *ethhdr; 1366 1367 /* Copy skb header to change the mac header */ 1368 skb = pskb_copy_for_clone(skb, GFP_ATOMIC); 1369 if (!skb) 1370 return; 1371 1372 /* Set the mac header as if we actually sent the packet uncoded */ 1373 ethhdr = eth_hdr(skb); 1374 ether_addr_copy(ethhdr->h_source, ethhdr->h_dest); 1375 ether_addr_copy(ethhdr->h_dest, eth_dst_new); 1376 1377 /* Set data pointer to MAC header to mimic packets from our tx path */ 1378 skb_push(skb, ETH_HLEN); 1379 1380 /* Add the packet to the decoding packet pool */ 1381 batadv_nc_skb_store_for_decoding(bat_priv, skb); 1382 1383 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free 1384 * our ref 1385 */ 1386 kfree_skb(skb); 1387 } 1388 1389 /** 1390 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst. 1391 * @skb: data skb to forward 1392 * @neigh_node: next hop to forward packet to 1393 * @ethhdr: pointer to the ethernet header inside the skb 1394 * 1395 * Loops through list of neighboring nodes the next hop has a good connection to 1396 * (receives OGMs with a sufficient quality). We need to find a neighbor of our 1397 * next hop that potentially sent a packet which our next hop also received 1398 * (overheard) and has stored for later decoding. 1399 * 1400 * Returns true if the skb was consumed (encoded packet sent) or false otherwise 1401 */ 1402 static bool batadv_nc_skb_dst_search(struct sk_buff *skb, 1403 struct batadv_neigh_node *neigh_node, 1404 struct ethhdr *ethhdr) 1405 { 1406 struct net_device *netdev = neigh_node->if_incoming->soft_iface; 1407 struct batadv_priv *bat_priv = netdev_priv(netdev); 1408 struct batadv_orig_node *orig_node = neigh_node->orig_node; 1409 struct batadv_nc_node *nc_node; 1410 struct batadv_nc_packet *nc_packet = NULL; 1411 1412 rcu_read_lock(); 1413 list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) { 1414 /* Search for coding opportunity with this in_nc_node */ 1415 nc_packet = batadv_nc_skb_src_search(bat_priv, skb, 1416 neigh_node->addr, 1417 ethhdr->h_source, nc_node); 1418 1419 /* Opportunity was found, so stop searching */ 1420 if (nc_packet) 1421 break; 1422 } 1423 rcu_read_unlock(); 1424 1425 if (!nc_packet) 1426 return false; 1427 1428 /* Save packets for later decoding */ 1429 batadv_nc_skb_store_before_coding(bat_priv, skb, 1430 neigh_node->addr); 1431 batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb, 1432 nc_packet->neigh_node->addr); 1433 1434 /* Code and send packets */ 1435 if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet, 1436 neigh_node)) 1437 return true; 1438 1439 /* out of mem ? Coding failed - we have to free the buffered packet 1440 * to avoid memleaks. The skb passed as argument will be dealt with 1441 * by the calling function. 1442 */ 1443 batadv_nc_send_packet(nc_packet); 1444 return false; 1445 } 1446 1447 /** 1448 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding 1449 * @skb: skb to add to path 1450 * @nc_path: path to add skb to 1451 * @neigh_node: next hop to forward packet to 1452 * @packet_id: checksum to identify packet 1453 * 1454 * Returns true if the packet was buffered or false in case of an error. 1455 */ 1456 static bool batadv_nc_skb_add_to_path(struct sk_buff *skb, 1457 struct batadv_nc_path *nc_path, 1458 struct batadv_neigh_node *neigh_node, 1459 __be32 packet_id) 1460 { 1461 struct batadv_nc_packet *nc_packet; 1462 1463 nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC); 1464 if (!nc_packet) 1465 return false; 1466 1467 /* Initialize nc_packet */ 1468 nc_packet->timestamp = jiffies; 1469 nc_packet->packet_id = packet_id; 1470 nc_packet->skb = skb; 1471 nc_packet->neigh_node = neigh_node; 1472 nc_packet->nc_path = nc_path; 1473 1474 /* Add coding packet to list */ 1475 spin_lock_bh(&nc_path->packet_list_lock); 1476 list_add_tail(&nc_packet->list, &nc_path->packet_list); 1477 spin_unlock_bh(&nc_path->packet_list_lock); 1478 1479 return true; 1480 } 1481 1482 /** 1483 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet 1484 * buffer 1485 * @skb: data skb to forward 1486 * @neigh_node: next hop to forward packet to 1487 * 1488 * Returns true if the skb was consumed (encoded packet sent) or false otherwise 1489 */ 1490 bool batadv_nc_skb_forward(struct sk_buff *skb, 1491 struct batadv_neigh_node *neigh_node) 1492 { 1493 const struct net_device *netdev = neigh_node->if_incoming->soft_iface; 1494 struct batadv_priv *bat_priv = netdev_priv(netdev); 1495 struct batadv_unicast_packet *packet; 1496 struct batadv_nc_path *nc_path; 1497 struct ethhdr *ethhdr = eth_hdr(skb); 1498 __be32 packet_id; 1499 u8 *payload; 1500 1501 /* Check if network coding is enabled */ 1502 if (!atomic_read(&bat_priv->network_coding)) 1503 goto out; 1504 1505 /* We only handle unicast packets */ 1506 payload = skb_network_header(skb); 1507 packet = (struct batadv_unicast_packet *)payload; 1508 if (packet->packet_type != BATADV_UNICAST) 1509 goto out; 1510 1511 /* Try to find a coding opportunity and send the skb if one is found */ 1512 if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr)) 1513 return true; 1514 1515 /* Find or create a nc_path for this src-dst pair */ 1516 nc_path = batadv_nc_get_path(bat_priv, 1517 bat_priv->nc.coding_hash, 1518 ethhdr->h_source, 1519 neigh_node->addr); 1520 1521 if (!nc_path) 1522 goto out; 1523 1524 /* Add skb to nc_path */ 1525 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet)); 1526 if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id)) 1527 goto free_nc_path; 1528 1529 /* Packet is consumed */ 1530 return true; 1531 1532 free_nc_path: 1533 batadv_nc_path_free_ref(nc_path); 1534 out: 1535 /* Packet is not consumed */ 1536 return false; 1537 } 1538 1539 /** 1540 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used 1541 * when decoding coded packets 1542 * @bat_priv: the bat priv with all the soft interface information 1543 * @skb: data skb to store 1544 */ 1545 void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv, 1546 struct sk_buff *skb) 1547 { 1548 struct batadv_unicast_packet *packet; 1549 struct batadv_nc_path *nc_path; 1550 struct ethhdr *ethhdr = eth_hdr(skb); 1551 __be32 packet_id; 1552 u8 *payload; 1553 1554 /* Check if network coding is enabled */ 1555 if (!atomic_read(&bat_priv->network_coding)) 1556 goto out; 1557 1558 /* Check for supported packet type */ 1559 payload = skb_network_header(skb); 1560 packet = (struct batadv_unicast_packet *)payload; 1561 if (packet->packet_type != BATADV_UNICAST) 1562 goto out; 1563 1564 /* Find existing nc_path or create a new */ 1565 nc_path = batadv_nc_get_path(bat_priv, 1566 bat_priv->nc.decoding_hash, 1567 ethhdr->h_source, 1568 ethhdr->h_dest); 1569 1570 if (!nc_path) 1571 goto out; 1572 1573 /* Clone skb and adjust skb->data to point at batman header */ 1574 skb = skb_clone(skb, GFP_ATOMIC); 1575 if (unlikely(!skb)) 1576 goto free_nc_path; 1577 1578 if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) 1579 goto free_skb; 1580 1581 if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN))) 1582 goto free_skb; 1583 1584 /* Add skb to nc_path */ 1585 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet)); 1586 if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id)) 1587 goto free_skb; 1588 1589 batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER); 1590 return; 1591 1592 free_skb: 1593 kfree_skb(skb); 1594 free_nc_path: 1595 batadv_nc_path_free_ref(nc_path); 1596 out: 1597 return; 1598 } 1599 1600 /** 1601 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet 1602 * should be saved in the decoding buffer and, if so, store it there 1603 * @bat_priv: the bat priv with all the soft interface information 1604 * @skb: unicast skb to store 1605 */ 1606 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv, 1607 struct sk_buff *skb) 1608 { 1609 struct ethhdr *ethhdr = eth_hdr(skb); 1610 1611 if (batadv_is_my_mac(bat_priv, ethhdr->h_dest)) 1612 return; 1613 1614 /* Set data pointer to MAC header to mimic packets from our tx path */ 1615 skb_push(skb, ETH_HLEN); 1616 1617 batadv_nc_skb_store_for_decoding(bat_priv, skb); 1618 } 1619 1620 /** 1621 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored 1622 * in nc_packet 1623 * @bat_priv: the bat priv with all the soft interface information 1624 * @skb: unicast skb to decode 1625 * @nc_packet: decode data needed to decode the skb 1626 * 1627 * Returns pointer to decoded unicast packet if the packet was decoded or NULL 1628 * in case of an error. 1629 */ 1630 static struct batadv_unicast_packet * 1631 batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb, 1632 struct batadv_nc_packet *nc_packet) 1633 { 1634 const int h_size = sizeof(struct batadv_unicast_packet); 1635 const int h_diff = sizeof(struct batadv_coded_packet) - h_size; 1636 struct batadv_unicast_packet *unicast_packet; 1637 struct batadv_coded_packet coded_packet_tmp; 1638 struct ethhdr *ethhdr, ethhdr_tmp; 1639 u8 *orig_dest, ttl, ttvn; 1640 unsigned int coding_len; 1641 int err; 1642 1643 /* Save headers temporarily */ 1644 memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp)); 1645 memcpy(ðhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp)); 1646 1647 if (skb_cow(skb, 0) < 0) 1648 return NULL; 1649 1650 if (unlikely(!skb_pull_rcsum(skb, h_diff))) 1651 return NULL; 1652 1653 /* Data points to batman header, so set mac header 14 bytes before 1654 * and network to data 1655 */ 1656 skb_set_mac_header(skb, -ETH_HLEN); 1657 skb_reset_network_header(skb); 1658 1659 /* Reconstruct original mac header */ 1660 ethhdr = eth_hdr(skb); 1661 *ethhdr = ethhdr_tmp; 1662 1663 /* Select the correct unicast header information based on the location 1664 * of our mac address in the coded_packet header 1665 */ 1666 if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) { 1667 /* If we are the second destination the packet was overheard, 1668 * so the Ethernet address must be copied to h_dest and 1669 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST 1670 */ 1671 ether_addr_copy(ethhdr->h_dest, coded_packet_tmp.second_dest); 1672 skb->pkt_type = PACKET_HOST; 1673 1674 orig_dest = coded_packet_tmp.second_orig_dest; 1675 ttl = coded_packet_tmp.second_ttl; 1676 ttvn = coded_packet_tmp.second_ttvn; 1677 } else { 1678 orig_dest = coded_packet_tmp.first_orig_dest; 1679 ttl = coded_packet_tmp.ttl; 1680 ttvn = coded_packet_tmp.first_ttvn; 1681 } 1682 1683 coding_len = ntohs(coded_packet_tmp.coded_len); 1684 1685 if (coding_len > skb->len) 1686 return NULL; 1687 1688 /* Here the magic is reversed: 1689 * extract the missing packet from the received coded packet 1690 */ 1691 batadv_nc_memxor(skb->data + h_size, 1692 nc_packet->skb->data + h_size, 1693 coding_len); 1694 1695 /* Resize decoded skb if decoded with larger packet */ 1696 if (nc_packet->skb->len > coding_len + h_size) { 1697 err = pskb_trim_rcsum(skb, coding_len + h_size); 1698 if (err) 1699 return NULL; 1700 } 1701 1702 /* Create decoded unicast packet */ 1703 unicast_packet = (struct batadv_unicast_packet *)skb->data; 1704 unicast_packet->packet_type = BATADV_UNICAST; 1705 unicast_packet->version = BATADV_COMPAT_VERSION; 1706 unicast_packet->ttl = ttl; 1707 ether_addr_copy(unicast_packet->dest, orig_dest); 1708 unicast_packet->ttvn = ttvn; 1709 1710 batadv_nc_packet_free(nc_packet); 1711 return unicast_packet; 1712 } 1713 1714 /** 1715 * batadv_nc_find_decoding_packet - search through buffered decoding data to 1716 * find the data needed to decode the coded packet 1717 * @bat_priv: the bat priv with all the soft interface information 1718 * @ethhdr: pointer to the ethernet header inside the coded packet 1719 * @coded: coded packet we try to find decode data for 1720 * 1721 * Returns pointer to nc packet if the needed data was found or NULL otherwise. 1722 */ 1723 static struct batadv_nc_packet * 1724 batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv, 1725 struct ethhdr *ethhdr, 1726 struct batadv_coded_packet *coded) 1727 { 1728 struct batadv_hashtable *hash = bat_priv->nc.decoding_hash; 1729 struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL; 1730 struct batadv_nc_path *nc_path, nc_path_key; 1731 u8 *dest, *source; 1732 __be32 packet_id; 1733 int index; 1734 1735 if (!hash) 1736 return NULL; 1737 1738 /* Select the correct packet id based on the location of our mac-addr */ 1739 dest = ethhdr->h_source; 1740 if (!batadv_is_my_mac(bat_priv, coded->second_dest)) { 1741 source = coded->second_source; 1742 packet_id = coded->second_crc; 1743 } else { 1744 source = coded->first_source; 1745 packet_id = coded->first_crc; 1746 } 1747 1748 batadv_nc_hash_key_gen(&nc_path_key, source, dest); 1749 index = batadv_nc_hash_choose(&nc_path_key, hash->size); 1750 1751 /* Search for matching coding path */ 1752 rcu_read_lock(); 1753 hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) { 1754 /* Find matching nc_packet */ 1755 spin_lock_bh(&nc_path->packet_list_lock); 1756 list_for_each_entry(tmp_nc_packet, 1757 &nc_path->packet_list, list) { 1758 if (packet_id == tmp_nc_packet->packet_id) { 1759 list_del(&tmp_nc_packet->list); 1760 1761 nc_packet = tmp_nc_packet; 1762 break; 1763 } 1764 } 1765 spin_unlock_bh(&nc_path->packet_list_lock); 1766 1767 if (nc_packet) 1768 break; 1769 } 1770 rcu_read_unlock(); 1771 1772 if (!nc_packet) 1773 batadv_dbg(BATADV_DBG_NC, bat_priv, 1774 "No decoding packet found for %u\n", packet_id); 1775 1776 return nc_packet; 1777 } 1778 1779 /** 1780 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the 1781 * resulting unicast packet 1782 * @skb: incoming coded packet 1783 * @recv_if: pointer to interface this packet was received on 1784 */ 1785 static int batadv_nc_recv_coded_packet(struct sk_buff *skb, 1786 struct batadv_hard_iface *recv_if) 1787 { 1788 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 1789 struct batadv_unicast_packet *unicast_packet; 1790 struct batadv_coded_packet *coded_packet; 1791 struct batadv_nc_packet *nc_packet; 1792 struct ethhdr *ethhdr; 1793 int hdr_size = sizeof(*coded_packet); 1794 1795 /* Check if network coding is enabled */ 1796 if (!atomic_read(&bat_priv->network_coding)) 1797 return NET_RX_DROP; 1798 1799 /* Make sure we can access (and remove) header */ 1800 if (unlikely(!pskb_may_pull(skb, hdr_size))) 1801 return NET_RX_DROP; 1802 1803 coded_packet = (struct batadv_coded_packet *)skb->data; 1804 ethhdr = eth_hdr(skb); 1805 1806 /* Verify frame is destined for us */ 1807 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) && 1808 !batadv_is_my_mac(bat_priv, coded_packet->second_dest)) 1809 return NET_RX_DROP; 1810 1811 /* Update stat counter */ 1812 if (batadv_is_my_mac(bat_priv, coded_packet->second_dest)) 1813 batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED); 1814 1815 nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr, 1816 coded_packet); 1817 if (!nc_packet) { 1818 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED); 1819 return NET_RX_DROP; 1820 } 1821 1822 /* Make skb's linear, because decoding accesses the entire buffer */ 1823 if (skb_linearize(skb) < 0) 1824 goto free_nc_packet; 1825 1826 if (skb_linearize(nc_packet->skb) < 0) 1827 goto free_nc_packet; 1828 1829 /* Decode the packet */ 1830 unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet); 1831 if (!unicast_packet) { 1832 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED); 1833 goto free_nc_packet; 1834 } 1835 1836 /* Mark packet as decoded to do correct recoding when forwarding */ 1837 BATADV_SKB_CB(skb)->decoded = true; 1838 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE); 1839 batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES, 1840 skb->len + ETH_HLEN); 1841 return batadv_recv_unicast_packet(skb, recv_if); 1842 1843 free_nc_packet: 1844 batadv_nc_packet_free(nc_packet); 1845 return NET_RX_DROP; 1846 } 1847 1848 /** 1849 * batadv_nc_mesh_free - clean up network coding memory 1850 * @bat_priv: the bat priv with all the soft interface information 1851 */ 1852 void batadv_nc_mesh_free(struct batadv_priv *bat_priv) 1853 { 1854 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1); 1855 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1); 1856 cancel_delayed_work_sync(&bat_priv->nc.work); 1857 1858 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL); 1859 batadv_hash_destroy(bat_priv->nc.coding_hash); 1860 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL); 1861 batadv_hash_destroy(bat_priv->nc.decoding_hash); 1862 } 1863 1864 /** 1865 * batadv_nc_nodes_seq_print_text - print the nc node information 1866 * @seq: seq file to print on 1867 * @offset: not used 1868 */ 1869 int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset) 1870 { 1871 struct net_device *net_dev = (struct net_device *)seq->private; 1872 struct batadv_priv *bat_priv = netdev_priv(net_dev); 1873 struct batadv_hashtable *hash = bat_priv->orig_hash; 1874 struct batadv_hard_iface *primary_if; 1875 struct hlist_head *head; 1876 struct batadv_orig_node *orig_node; 1877 struct batadv_nc_node *nc_node; 1878 int i; 1879 1880 primary_if = batadv_seq_print_text_primary_if_get(seq); 1881 if (!primary_if) 1882 goto out; 1883 1884 /* Traverse list of originators */ 1885 for (i = 0; i < hash->size; i++) { 1886 head = &hash->table[i]; 1887 1888 /* For each orig_node in this bin */ 1889 rcu_read_lock(); 1890 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 1891 /* no need to print the orig node if it does not have 1892 * network coding neighbors 1893 */ 1894 if (list_empty(&orig_node->in_coding_list) && 1895 list_empty(&orig_node->out_coding_list)) 1896 continue; 1897 1898 seq_printf(seq, "Node: %pM\n", orig_node->orig); 1899 1900 seq_puts(seq, " Ingoing: "); 1901 /* For each in_nc_node to this orig_node */ 1902 list_for_each_entry_rcu(nc_node, 1903 &orig_node->in_coding_list, 1904 list) 1905 seq_printf(seq, "%pM ", 1906 nc_node->addr); 1907 seq_puts(seq, "\n"); 1908 1909 seq_puts(seq, " Outgoing: "); 1910 /* For out_nc_node to this orig_node */ 1911 list_for_each_entry_rcu(nc_node, 1912 &orig_node->out_coding_list, 1913 list) 1914 seq_printf(seq, "%pM ", 1915 nc_node->addr); 1916 seq_puts(seq, "\n\n"); 1917 } 1918 rcu_read_unlock(); 1919 } 1920 1921 out: 1922 if (primary_if) 1923 batadv_hardif_free_ref(primary_if); 1924 return 0; 1925 } 1926 1927 /** 1928 * batadv_nc_init_debugfs - create nc folder and related files in debugfs 1929 * @bat_priv: the bat priv with all the soft interface information 1930 */ 1931 int batadv_nc_init_debugfs(struct batadv_priv *bat_priv) 1932 { 1933 struct dentry *nc_dir, *file; 1934 1935 nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir); 1936 if (!nc_dir) 1937 goto out; 1938 1939 file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir, 1940 &bat_priv->nc.min_tq); 1941 if (!file) 1942 goto out; 1943 1944 file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir, 1945 &bat_priv->nc.max_fwd_delay); 1946 if (!file) 1947 goto out; 1948 1949 file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir, 1950 &bat_priv->nc.max_buffer_time); 1951 if (!file) 1952 goto out; 1953 1954 return 0; 1955 1956 out: 1957 return -ENOMEM; 1958 } 1959