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