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