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