blob: aec3fa56402756222cc82a93eac20d296d22bb71 [file] [log] [blame]
Martin Hundebølld353d8d2013-01-25 11:12:38 +01001/* Copyright (C) 2012-2013 B.A.T.M.A.N. contributors:
2 *
3 * Martin Hundebøll, Jeppe Ledet-Pedersen
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
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ølld56b1702013-01-25 11:12:39 +010018#include <linux/debugfs.h>
19
Martin Hundebølld353d8d2013-01-25 11:12:38 +010020#include "main.h"
Martin Hundebøll95332472013-01-25 11:12:40 +010021#include "hash.h"
Martin Hundebølld353d8d2013-01-25 11:12:38 +010022#include "network-coding.h"
Martin Hundebøll95332472013-01-25 11:12:40 +010023#include "send.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010024#include "originator.h"
25#include "hard-interface.h"
Martin Hundebøll2df52782013-01-25 11:12:43 +010026#include "routing.h"
Martin Hundebølld353d8d2013-01-25 11:12:38 +010027
Martin Hundebøll95332472013-01-25 11:12:40 +010028static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
Martin Hundebøll612d2b42013-01-25 11:12:42 +010029static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
Martin Hundebøll95332472013-01-25 11:12:40 +010030
Martin Hundebølld353d8d2013-01-25 11:12:38 +010031static void batadv_nc_worker(struct work_struct *work);
Martin Hundebøll2df52782013-01-25 11:12:43 +010032static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
33 struct batadv_hard_iface *recv_if);
Martin Hundebølld353d8d2013-01-25 11:12:38 +010034
35/**
Matthias Schiffer6c519ba2013-09-27 18:03:39 +020036 * batadv_nc_init - one-time initialization for network coding
37 */
38int __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ølld353d8d2013-01-25 11:12:38 +010050 * batadv_nc_start_timer - initialise the nc periodic worker
51 * @bat_priv: the bat priv with all the soft interface information
52 */
53static 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 Lindner3f4841f2013-04-23 21:40:00 +080060 * 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 */
64static 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 */
86void 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 */
100static 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 Schiffer6c519ba2013-09-27 18:03:39 +0200113 * batadv_nc_mesh_init - initialise coding hash table and start house keeping
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100114 * @bat_priv: the bat priv with all the soft interface information
115 */
Matthias Schiffer6c519ba2013-09-27 18:03:39 +0200116int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100117{
Martin Hundebøll95332472013-01-25 11:12:40 +0100118 bat_priv->nc.timestamp_fwd_flush = jiffies;
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100119 bat_priv->nc.timestamp_sniffed_purge = jiffies;
Martin Hundebøll95332472013-01-25 11:12:40 +0100120
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100121 if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
Martin Hundebøll95332472013-01-25 11:12:40 +0100122 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øll612d2b42013-01-25 11:12:42 +0100131 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ølld353d8d2013-01-25 11:12:38 +0100138 INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
139 batadv_nc_start_timer(bat_priv);
140
Marek Lindner3f4841f2013-04-23 21:40:00 +0800141 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ølld353d8d2013-01-25 11:12:38 +0100145 return 0;
Martin Hundebøll95332472013-01-25 11:12:40 +0100146
147err:
148 return -ENOMEM;
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100149}
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 */
155void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
156{
157 atomic_set(&bat_priv->network_coding, 1);
Martin Hundebølld56b1702013-01-25 11:12:39 +0100158 bat_priv->nc.min_tq = 200;
Martin Hundebøll95332472013-01-25 11:12:40 +0100159 bat_priv->nc.max_fwd_delay = 10;
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100160 bat_priv->nc.max_buffer_time = 200;
Martin Hundebølld56b1702013-01-25 11:12:39 +0100161}
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 */
167void 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 */
180static 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 */
194static 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øll95332472013-01-25 11:12:40 +0100201 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
202 * frees it
203 * @nc_path: the nc node to free
204 */
205static 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 */
215static 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ølld56b1702013-01-25 11:12:39 +0100225 * 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 */
231static 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øll95332472013-01-25 11:12:40 +0100241 * 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 */
247static 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øll612d2b42013-01-25 11:12:42 +0100261 * 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 */
267static 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ølld56b1702013-01-25 11:12:39 +0100281 * 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 */
291static void
292batadv_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 */
328void 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 */
349static 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ølld353d8d2013-01-25 11:12:38 +0100369}
370
371/**
Martin Hundebøll95332472013-01-25 11:12:40 +0100372 * 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 */
381static 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 */
436static 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 */
450static 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 */
475static 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 */
502static struct batadv_nc_path *
503batadv_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 */
536static 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øll612d2b42013-01-25 11:12:42 +0100546 * 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 */
558static 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
578out:
579 return res;
580}
581
582/**
Martin Hundebøll95332472013-01-25 11:12:40 +0100583 * 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 */
595static 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 */
627static void
628batadv_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ølld353d8d2013-01-25 11:12:38 +0100665 * batadv_nc_worker - periodic task for house keeping related to network coding
666 * @work: kernel work struct
667 */
668static 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øll95332472013-01-25 11:12:40 +0100673 unsigned long timeout;
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100674
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ølld56b1702013-01-25 11:12:39 +0100679 batadv_nc_purge_orig_hash(bat_priv);
Martin Hundebøll95332472013-01-25 11:12:40 +0100680 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
681 batadv_nc_to_purge_nc_path_coding);
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100682 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
683 batadv_nc_to_purge_nc_path_decoding);
Martin Hundebøll95332472013-01-25 11:12:40 +0100684
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ølld56b1702013-01-25 11:12:39 +0100692
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100693 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ølld353d8d2013-01-25 11:12:38 +0100700 /* Schedule a new check */
701 batadv_nc_start_timer(bat_priv);
702}
703
704/**
Martin Hundebølld56b1702013-01-25 11:12:39 +0100705 * 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 */
717static 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{
Martin Hundebølle6a0b492013-03-14 21:30:21 +0100721 if (orig_node->last_real_seqno != ntohl(ogm_packet->seqno))
Martin Hundebølld56b1702013-01-25 11:12:39 +0100722 return false;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100723 if (orig_node->last_ttl != ogm_packet->ttl + 1)
Martin Hundebølld56b1702013-01-25 11:12:39 +0100724 return false;
725 if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
726 return false;
727 if (ogm_packet->tq < bat_priv->nc.min_tq)
728 return false;
729
730 return true;
731}
732
733/**
734 * batadv_nc_find_nc_node - search for an existing nc node and return it
735 * @orig_node: orig node originating the ogm packet
736 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
737 * (can be equal to orig_node)
738 * @in_coding: traverse incoming or outgoing network coding list
739 *
740 * Returns the nc_node if found, NULL otherwise.
741 */
742static struct batadv_nc_node
743*batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
744 struct batadv_orig_node *orig_neigh_node,
745 bool in_coding)
746{
747 struct batadv_nc_node *nc_node, *nc_node_out = NULL;
748 struct list_head *list;
749
750 if (in_coding)
751 list = &orig_neigh_node->in_coding_list;
752 else
753 list = &orig_neigh_node->out_coding_list;
754
755 /* Traverse list of nc_nodes to orig_node */
756 rcu_read_lock();
757 list_for_each_entry_rcu(nc_node, list, list) {
758 if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
759 continue;
760
761 if (!atomic_inc_not_zero(&nc_node->refcount))
762 continue;
763
764 /* Found a match */
765 nc_node_out = nc_node;
766 break;
767 }
768 rcu_read_unlock();
769
770 return nc_node_out;
771}
772
773/**
774 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
775 * not found
776 * @bat_priv: the bat priv with all the soft interface information
777 * @orig_node: orig node originating the ogm packet
778 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
779 * (can be equal to orig_node)
780 * @in_coding: traverse incoming or outgoing network coding list
781 *
782 * Returns the nc_node if found or created, NULL in case of an error.
783 */
784static struct batadv_nc_node
785*batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
786 struct batadv_orig_node *orig_node,
787 struct batadv_orig_node *orig_neigh_node,
788 bool in_coding)
789{
790 struct batadv_nc_node *nc_node;
791 spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
792 struct list_head *list;
793
794 /* Check if nc_node is already added */
795 nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
796
797 /* Node found */
798 if (nc_node)
799 return nc_node;
800
801 nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
802 if (!nc_node)
803 return NULL;
804
805 if (!atomic_inc_not_zero(&orig_neigh_node->refcount))
806 goto free;
807
808 /* Initialize nc_node */
809 INIT_LIST_HEAD(&nc_node->list);
810 memcpy(nc_node->addr, orig_node->orig, ETH_ALEN);
811 nc_node->orig_node = orig_neigh_node;
812 atomic_set(&nc_node->refcount, 2);
813
814 /* Select ingoing or outgoing coding node */
815 if (in_coding) {
816 lock = &orig_neigh_node->in_coding_list_lock;
817 list = &orig_neigh_node->in_coding_list;
818 } else {
819 lock = &orig_neigh_node->out_coding_list_lock;
820 list = &orig_neigh_node->out_coding_list;
821 }
822
823 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
824 nc_node->addr, nc_node->orig_node->orig);
825
826 /* Add nc_node to orig_node */
827 spin_lock_bh(lock);
828 list_add_tail_rcu(&nc_node->list, list);
829 spin_unlock_bh(lock);
830
831 return nc_node;
832
833free:
834 kfree(nc_node);
835 return NULL;
836}
837
838/**
839 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
840 * (best called on incoming OGMs)
841 * @bat_priv: the bat priv with all the soft interface information
842 * @orig_node: orig node originating the ogm packet
843 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
844 * (can be equal to orig_node)
845 * @ogm_packet: incoming ogm packet
846 * @is_single_hop_neigh: orig_node is a single hop neighbor
847 */
848void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
849 struct batadv_orig_node *orig_node,
850 struct batadv_orig_node *orig_neigh_node,
851 struct batadv_ogm_packet *ogm_packet,
852 int is_single_hop_neigh)
853{
854 struct batadv_nc_node *in_nc_node = NULL, *out_nc_node = NULL;
855
856 /* Check if network coding is enabled */
857 if (!atomic_read(&bat_priv->network_coding))
858 goto out;
859
Marek Lindner3f4841f2013-04-23 21:40:00 +0800860 /* check if orig node is network coding enabled */
861 if (!(orig_node->capabilities & BATADV_ORIG_CAPA_HAS_NC))
862 goto out;
863
Martin Hundebølld56b1702013-01-25 11:12:39 +0100864 /* accept ogms from 'good' neighbors and single hop neighbors */
865 if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
866 !is_single_hop_neigh)
867 goto out;
868
869 /* Add orig_node as in_nc_node on hop */
870 in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
871 orig_neigh_node, true);
872 if (!in_nc_node)
873 goto out;
874
875 in_nc_node->last_seen = jiffies;
876
877 /* Add hop as out_nc_node on orig_node */
878 out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
879 orig_node, false);
880 if (!out_nc_node)
881 goto out;
882
883 out_nc_node->last_seen = jiffies;
884
885out:
886 if (in_nc_node)
887 batadv_nc_node_free_ref(in_nc_node);
888 if (out_nc_node)
889 batadv_nc_node_free_ref(out_nc_node);
890}
891
892/**
Martin Hundebøll95332472013-01-25 11:12:40 +0100893 * batadv_nc_get_path - get existing nc_path or allocate a new one
894 * @bat_priv: the bat priv with all the soft interface information
895 * @hash: hash table containing the nc path
896 * @src: ethernet source address - first half of the nc path search key
897 * @dst: ethernet destination address - second half of the nc path search key
898 *
899 * Returns pointer to nc_path if the path was found or created, returns NULL
900 * on error.
901 */
902static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
903 struct batadv_hashtable *hash,
904 uint8_t *src,
905 uint8_t *dst)
906{
907 int hash_added;
908 struct batadv_nc_path *nc_path, nc_path_key;
909
910 batadv_nc_hash_key_gen(&nc_path_key, src, dst);
911
912 /* Search for existing nc_path */
913 nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
914
915 if (nc_path) {
916 /* Set timestamp to delay removal of nc_path */
917 nc_path->last_valid = jiffies;
918 return nc_path;
919 }
920
921 /* No existing nc_path was found; create a new */
922 nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
923
924 if (!nc_path)
925 return NULL;
926
927 /* Initialize nc_path */
928 INIT_LIST_HEAD(&nc_path->packet_list);
929 spin_lock_init(&nc_path->packet_list_lock);
930 atomic_set(&nc_path->refcount, 2);
931 nc_path->last_valid = jiffies;
932 memcpy(nc_path->next_hop, dst, ETH_ALEN);
933 memcpy(nc_path->prev_hop, src, ETH_ALEN);
934
935 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
936 nc_path->prev_hop,
937 nc_path->next_hop);
938
939 /* Add nc_path to hash table */
940 hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
941 batadv_nc_hash_choose, &nc_path_key,
942 &nc_path->hash_entry);
943
944 if (hash_added < 0) {
945 kfree(nc_path);
946 return NULL;
947 }
948
949 return nc_path;
950}
951
952/**
Martin Hundebøll3c12de92013-01-25 11:12:41 +0100953 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
954 * selection of a receiver with slightly lower TQ than the other
955 * @tq: to be weighted tq value
956 */
957static uint8_t batadv_nc_random_weight_tq(uint8_t tq)
958{
959 uint8_t rand_val, rand_tq;
960
961 get_random_bytes(&rand_val, sizeof(rand_val));
962
963 /* randomize the estimated packet loss (max TQ - estimated TQ) */
964 rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq);
965
966 /* normalize the randomized packet loss */
967 rand_tq /= BATADV_TQ_MAX_VALUE;
968
969 /* convert to (randomized) estimated tq again */
970 return BATADV_TQ_MAX_VALUE - rand_tq;
971}
972
973/**
974 * batadv_nc_memxor - XOR destination with source
975 * @dst: byte array to XOR into
976 * @src: byte array to XOR from
977 * @len: length of destination array
978 */
979static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
980{
981 unsigned int i;
982
983 for (i = 0; i < len; ++i)
984 dst[i] ^= src[i];
985}
986
987/**
988 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
989 * into a coded_packet and send it
990 * @bat_priv: the bat priv with all the soft interface information
991 * @skb: data skb to forward
992 * @ethhdr: pointer to the ethernet header inside the skb
993 * @nc_packet: structure containing the packet to the skb can be coded with
994 * @neigh_node: next hop to forward packet to
995 *
996 * Returns true if both packets are consumed, false otherwise.
997 */
998static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
999 struct sk_buff *skb,
1000 struct ethhdr *ethhdr,
1001 struct batadv_nc_packet *nc_packet,
1002 struct batadv_neigh_node *neigh_node)
1003{
Antonio Quartulli0538f752013-09-02 12:15:01 +02001004 uint8_t tq_weighted_neigh, tq_weighted_coding, tq_tmp;
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001005 struct sk_buff *skb_dest, *skb_src;
1006 struct batadv_unicast_packet *packet1;
1007 struct batadv_unicast_packet *packet2;
1008 struct batadv_coded_packet *coded_packet;
1009 struct batadv_neigh_node *neigh_tmp, *router_neigh;
1010 struct batadv_neigh_node *router_coding = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001011 struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL;
1012 struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL;
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001013 uint8_t *first_source, *first_dest, *second_source, *second_dest;
1014 __be32 packet_id1, packet_id2;
1015 size_t count;
1016 bool res = false;
1017 int coding_len;
1018 int unicast_size = sizeof(*packet1);
1019 int coded_size = sizeof(*coded_packet);
1020 int header_add = coded_size - unicast_size;
1021
1022 router_neigh = batadv_orig_node_get_router(neigh_node->orig_node);
1023 if (!router_neigh)
1024 goto out;
1025
Simon Wunderlich89652332013-11-13 19:14:46 +01001026 router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh,
1027 BATADV_IF_DEFAULT);
1028 if (!router_neigh_ifinfo)
1029 goto out;
1030
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001031 neigh_tmp = nc_packet->neigh_node;
1032 router_coding = batadv_orig_node_get_router(neigh_tmp->orig_node);
1033 if (!router_coding)
1034 goto out;
1035
Simon Wunderlich89652332013-11-13 19:14:46 +01001036 router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding,
1037 BATADV_IF_DEFAULT);
1038 if (!router_coding_ifinfo)
1039 goto out;
1040
1041 tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg;
1042 tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp);
1043 tq_tmp = router_coding_ifinfo->bat_iv.tq_avg;
1044 tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp);
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001045
1046 /* Select one destination for the MAC-header dst-field based on
1047 * weighted TQ-values.
1048 */
1049 if (tq_weighted_neigh >= tq_weighted_coding) {
1050 /* Destination from nc_packet is selected for MAC-header */
1051 first_dest = nc_packet->nc_path->next_hop;
1052 first_source = nc_packet->nc_path->prev_hop;
1053 second_dest = neigh_node->addr;
1054 second_source = ethhdr->h_source;
1055 packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
1056 packet2 = (struct batadv_unicast_packet *)skb->data;
1057 packet_id1 = nc_packet->packet_id;
1058 packet_id2 = batadv_skb_crc32(skb,
1059 skb->data + sizeof(*packet2));
1060 } else {
1061 /* Destination for skb is selected for MAC-header */
1062 first_dest = neigh_node->addr;
1063 first_source = ethhdr->h_source;
1064 second_dest = nc_packet->nc_path->next_hop;
1065 second_source = nc_packet->nc_path->prev_hop;
1066 packet1 = (struct batadv_unicast_packet *)skb->data;
1067 packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
1068 packet_id1 = batadv_skb_crc32(skb,
1069 skb->data + sizeof(*packet1));
1070 packet_id2 = nc_packet->packet_id;
1071 }
1072
1073 /* Instead of zero padding the smallest data buffer, we
1074 * code into the largest.
1075 */
1076 if (skb->len <= nc_packet->skb->len) {
1077 skb_dest = nc_packet->skb;
1078 skb_src = skb;
1079 } else {
1080 skb_dest = skb;
1081 skb_src = nc_packet->skb;
1082 }
1083
1084 /* coding_len is used when decoding the packet shorter packet */
1085 coding_len = skb_src->len - unicast_size;
1086
1087 if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
1088 goto out;
1089
1090 skb_push(skb_dest, header_add);
1091
1092 coded_packet = (struct batadv_coded_packet *)skb_dest->data;
1093 skb_reset_mac_header(skb_dest);
1094
Simon Wunderlicha40d9b02013-12-02 20:38:31 +01001095 coded_packet->packet_type = BATADV_CODED;
1096 coded_packet->version = BATADV_COMPAT_VERSION;
1097 coded_packet->ttl = packet1->ttl;
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001098
1099 /* Info about first unicast packet */
1100 memcpy(coded_packet->first_source, first_source, ETH_ALEN);
1101 memcpy(coded_packet->first_orig_dest, packet1->dest, ETH_ALEN);
1102 coded_packet->first_crc = packet_id1;
1103 coded_packet->first_ttvn = packet1->ttvn;
1104
1105 /* Info about second unicast packet */
1106 memcpy(coded_packet->second_dest, second_dest, ETH_ALEN);
1107 memcpy(coded_packet->second_source, second_source, ETH_ALEN);
1108 memcpy(coded_packet->second_orig_dest, packet2->dest, ETH_ALEN);
1109 coded_packet->second_crc = packet_id2;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +01001110 coded_packet->second_ttl = packet2->ttl;
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001111 coded_packet->second_ttvn = packet2->ttvn;
1112 coded_packet->coded_len = htons(coding_len);
1113
1114 /* This is where the magic happens: Code skb_src into skb_dest */
1115 batadv_nc_memxor(skb_dest->data + coded_size,
1116 skb_src->data + unicast_size, coding_len);
1117
1118 /* Update counters accordingly */
1119 if (BATADV_SKB_CB(skb_src)->decoded &&
1120 BATADV_SKB_CB(skb_dest)->decoded) {
1121 /* Both packets are recoded */
1122 count = skb_src->len + ETH_HLEN;
1123 count += skb_dest->len + ETH_HLEN;
1124 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
1125 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
1126 } else if (!BATADV_SKB_CB(skb_src)->decoded &&
1127 !BATADV_SKB_CB(skb_dest)->decoded) {
1128 /* Both packets are newly coded */
1129 count = skb_src->len + ETH_HLEN;
1130 count += skb_dest->len + ETH_HLEN;
1131 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
1132 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
1133 } else if (BATADV_SKB_CB(skb_src)->decoded &&
1134 !BATADV_SKB_CB(skb_dest)->decoded) {
1135 /* skb_src recoded and skb_dest is newly coded */
1136 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1137 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1138 skb_src->len + ETH_HLEN);
1139 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1140 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1141 skb_dest->len + ETH_HLEN);
1142 } else if (!BATADV_SKB_CB(skb_src)->decoded &&
1143 BATADV_SKB_CB(skb_dest)->decoded) {
1144 /* skb_src is newly coded and skb_dest is recoded */
1145 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1146 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1147 skb_src->len + ETH_HLEN);
1148 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1149 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1150 skb_dest->len + ETH_HLEN);
1151 }
1152
1153 /* skb_src is now coded into skb_dest, so free it */
1154 kfree_skb(skb_src);
1155
1156 /* avoid duplicate free of skb from nc_packet */
1157 nc_packet->skb = NULL;
1158 batadv_nc_packet_free(nc_packet);
1159
1160 /* Send the coded packet and return true */
1161 batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest);
1162 res = true;
1163out:
1164 if (router_neigh)
1165 batadv_neigh_node_free_ref(router_neigh);
1166 if (router_coding)
1167 batadv_neigh_node_free_ref(router_coding);
Simon Wunderlich89652332013-11-13 19:14:46 +01001168 if (router_neigh_ifinfo)
1169 batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo);
1170 if (router_coding_ifinfo)
1171 batadv_neigh_ifinfo_free_ref(router_coding_ifinfo);
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001172 return res;
1173}
1174
1175/**
1176 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1177 * @skb: data skb to forward
1178 * @dst: destination mac address of the other skb to code with
1179 * @src: source mac address of skb
1180 *
1181 * Whenever we network code a packet we have to check whether we received it in
1182 * a network coded form. If so, we may not be able to use it for coding because
1183 * some neighbors may also have received (overheard) the packet in the network
1184 * coded form without being able to decode it. It is hard to know which of the
1185 * neighboring nodes was able to decode the packet, therefore we can only
1186 * re-code the packet if the source of the previous encoded packet is involved.
1187 * Since the source encoded the packet we can be certain it has all necessary
1188 * decode information.
1189 *
1190 * Returns true if coding of a decoded packet is allowed.
1191 */
1192static bool batadv_nc_skb_coding_possible(struct sk_buff *skb,
1193 uint8_t *dst, uint8_t *src)
1194{
1195 if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
1196 return false;
1197 else
1198 return true;
1199}
1200
1201/**
1202 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1203 * out_nc_node to retrieve a buffered packet that can be used for coding.
1204 * @bat_priv: the bat priv with all the soft interface information
1205 * @in_nc_node: pointer to skb next hop's neighbor nc node
1206 * @out_nc_node: pointer to skb source's neighbor nc node
1207 * @skb: data skb to forward
1208 * @eth_dst: next hop mac address of skb
1209 *
1210 * Returns true if coding of a decoded skb is allowed.
1211 */
1212static struct batadv_nc_packet *
1213batadv_nc_path_search(struct batadv_priv *bat_priv,
1214 struct batadv_nc_node *in_nc_node,
1215 struct batadv_nc_node *out_nc_node,
1216 struct sk_buff *skb,
1217 uint8_t *eth_dst)
1218{
1219 struct batadv_nc_path *nc_path, nc_path_key;
1220 struct batadv_nc_packet *nc_packet_out = NULL;
1221 struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
1222 struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
1223 int idx;
1224
1225 if (!hash)
1226 return NULL;
1227
1228 /* Create almost path key */
1229 batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
1230 out_nc_node->addr);
1231 idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
1232
1233 /* Check for coding opportunities in this nc_path */
1234 rcu_read_lock();
1235 hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
1236 if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
1237 continue;
1238
1239 if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
1240 continue;
1241
1242 spin_lock_bh(&nc_path->packet_list_lock);
1243 if (list_empty(&nc_path->packet_list)) {
1244 spin_unlock_bh(&nc_path->packet_list_lock);
1245 continue;
1246 }
1247
1248 list_for_each_entry_safe(nc_packet, nc_packet_tmp,
1249 &nc_path->packet_list, list) {
1250 if (!batadv_nc_skb_coding_possible(nc_packet->skb,
1251 eth_dst,
1252 in_nc_node->addr))
1253 continue;
1254
1255 /* Coding opportunity is found! */
1256 list_del(&nc_packet->list);
1257 nc_packet_out = nc_packet;
1258 break;
1259 }
1260
1261 spin_unlock_bh(&nc_path->packet_list_lock);
1262 break;
1263 }
1264 rcu_read_unlock();
1265
1266 return nc_packet_out;
1267}
1268
1269/**
1270 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1271 * skb's sender (may be equal to the originator).
1272 * @bat_priv: the bat priv with all the soft interface information
1273 * @skb: data skb to forward
1274 * @eth_dst: next hop mac address of skb
1275 * @eth_src: source mac address of skb
1276 * @in_nc_node: pointer to skb next hop's neighbor nc node
1277 *
1278 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1279 */
1280static struct batadv_nc_packet *
1281batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
1282 struct sk_buff *skb,
1283 uint8_t *eth_dst,
1284 uint8_t *eth_src,
1285 struct batadv_nc_node *in_nc_node)
1286{
1287 struct batadv_orig_node *orig_node;
1288 struct batadv_nc_node *out_nc_node;
1289 struct batadv_nc_packet *nc_packet = NULL;
1290
1291 orig_node = batadv_orig_hash_find(bat_priv, eth_src);
1292 if (!orig_node)
1293 return NULL;
1294
1295 rcu_read_lock();
1296 list_for_each_entry_rcu(out_nc_node,
1297 &orig_node->out_coding_list, list) {
1298 /* Check if the skb is decoded and if recoding is possible */
1299 if (!batadv_nc_skb_coding_possible(skb,
1300 out_nc_node->addr, eth_src))
1301 continue;
1302
1303 /* Search for an opportunity in this nc_path */
1304 nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
1305 out_nc_node, skb, eth_dst);
1306 if (nc_packet)
1307 break;
1308 }
1309 rcu_read_unlock();
1310
1311 batadv_orig_node_free_ref(orig_node);
1312 return nc_packet;
1313}
1314
1315/**
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001316 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1317 * unicast skb before it is stored for use in later decoding
1318 * @bat_priv: the bat priv with all the soft interface information
1319 * @skb: data skb to store
1320 * @eth_dst_new: new destination mac address of skb
1321 */
1322static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
1323 struct sk_buff *skb,
1324 uint8_t *eth_dst_new)
1325{
1326 struct ethhdr *ethhdr;
1327
1328 /* Copy skb header to change the mac header */
1329 skb = pskb_copy(skb, GFP_ATOMIC);
1330 if (!skb)
1331 return;
1332
1333 /* Set the mac header as if we actually sent the packet uncoded */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001334 ethhdr = eth_hdr(skb);
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001335 memcpy(ethhdr->h_source, ethhdr->h_dest, ETH_ALEN);
1336 memcpy(ethhdr->h_dest, eth_dst_new, ETH_ALEN);
1337
1338 /* Set data pointer to MAC header to mimic packets from our tx path */
1339 skb_push(skb, ETH_HLEN);
1340
1341 /* Add the packet to the decoding packet pool */
1342 batadv_nc_skb_store_for_decoding(bat_priv, skb);
1343
1344 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1345 * our ref
1346 */
1347 kfree_skb(skb);
1348}
1349
1350/**
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001351 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1352 * @skb: data skb to forward
1353 * @neigh_node: next hop to forward packet to
1354 * @ethhdr: pointer to the ethernet header inside the skb
1355 *
1356 * Loops through list of neighboring nodes the next hop has a good connection to
1357 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1358 * next hop that potentially sent a packet which our next hop also received
1359 * (overheard) and has stored for later decoding.
1360 *
1361 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1362 */
1363static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
1364 struct batadv_neigh_node *neigh_node,
1365 struct ethhdr *ethhdr)
1366{
1367 struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1368 struct batadv_priv *bat_priv = netdev_priv(netdev);
1369 struct batadv_orig_node *orig_node = neigh_node->orig_node;
1370 struct batadv_nc_node *nc_node;
1371 struct batadv_nc_packet *nc_packet = NULL;
1372
1373 rcu_read_lock();
1374 list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
1375 /* Search for coding opportunity with this in_nc_node */
1376 nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
1377 neigh_node->addr,
1378 ethhdr->h_source, nc_node);
1379
1380 /* Opportunity was found, so stop searching */
1381 if (nc_packet)
1382 break;
1383 }
1384 rcu_read_unlock();
1385
1386 if (!nc_packet)
1387 return false;
1388
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001389 /* Save packets for later decoding */
1390 batadv_nc_skb_store_before_coding(bat_priv, skb,
1391 neigh_node->addr);
1392 batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
1393 nc_packet->neigh_node->addr);
1394
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001395 /* Code and send packets */
1396 if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
1397 neigh_node))
1398 return true;
1399
1400 /* out of mem ? Coding failed - we have to free the buffered packet
1401 * to avoid memleaks. The skb passed as argument will be dealt with
1402 * by the calling function.
1403 */
1404 batadv_nc_send_packet(nc_packet);
1405 return false;
1406}
1407
1408/**
Martin Hundebøll95332472013-01-25 11:12:40 +01001409 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1410 * @skb: skb to add to path
1411 * @nc_path: path to add skb to
1412 * @neigh_node: next hop to forward packet to
1413 * @packet_id: checksum to identify packet
1414 *
1415 * Returns true if the packet was buffered or false in case of an error.
1416 */
1417static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
1418 struct batadv_nc_path *nc_path,
1419 struct batadv_neigh_node *neigh_node,
1420 __be32 packet_id)
1421{
1422 struct batadv_nc_packet *nc_packet;
1423
1424 nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
1425 if (!nc_packet)
1426 return false;
1427
1428 /* Initialize nc_packet */
1429 nc_packet->timestamp = jiffies;
1430 nc_packet->packet_id = packet_id;
1431 nc_packet->skb = skb;
1432 nc_packet->neigh_node = neigh_node;
1433 nc_packet->nc_path = nc_path;
1434
1435 /* Add coding packet to list */
1436 spin_lock_bh(&nc_path->packet_list_lock);
1437 list_add_tail(&nc_packet->list, &nc_path->packet_list);
1438 spin_unlock_bh(&nc_path->packet_list_lock);
1439
1440 return true;
1441}
1442
1443/**
1444 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1445 * buffer
1446 * @skb: data skb to forward
1447 * @neigh_node: next hop to forward packet to
Martin Hundebøll95332472013-01-25 11:12:40 +01001448 *
1449 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1450 */
1451bool batadv_nc_skb_forward(struct sk_buff *skb,
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02001452 struct batadv_neigh_node *neigh_node)
Martin Hundebøll95332472013-01-25 11:12:40 +01001453{
1454 const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1455 struct batadv_priv *bat_priv = netdev_priv(netdev);
1456 struct batadv_unicast_packet *packet;
1457 struct batadv_nc_path *nc_path;
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02001458 struct ethhdr *ethhdr = eth_hdr(skb);
Martin Hundebøll95332472013-01-25 11:12:40 +01001459 __be32 packet_id;
1460 u8 *payload;
1461
1462 /* Check if network coding is enabled */
1463 if (!atomic_read(&bat_priv->network_coding))
1464 goto out;
1465
1466 /* We only handle unicast packets */
1467 payload = skb_network_header(skb);
1468 packet = (struct batadv_unicast_packet *)payload;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +01001469 if (packet->packet_type != BATADV_UNICAST)
Martin Hundebøll95332472013-01-25 11:12:40 +01001470 goto out;
1471
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001472 /* Try to find a coding opportunity and send the skb if one is found */
1473 if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
1474 return true;
1475
Martin Hundebøll95332472013-01-25 11:12:40 +01001476 /* Find or create a nc_path for this src-dst pair */
1477 nc_path = batadv_nc_get_path(bat_priv,
1478 bat_priv->nc.coding_hash,
1479 ethhdr->h_source,
1480 neigh_node->addr);
1481
1482 if (!nc_path)
1483 goto out;
1484
1485 /* Add skb to nc_path */
1486 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1487 if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
1488 goto free_nc_path;
1489
1490 /* Packet is consumed */
1491 return true;
1492
1493free_nc_path:
1494 batadv_nc_path_free_ref(nc_path);
1495out:
1496 /* Packet is not consumed */
1497 return false;
1498}
1499
1500/**
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001501 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1502 * when decoding coded packets
1503 * @bat_priv: the bat priv with all the soft interface information
1504 * @skb: data skb to store
1505 */
1506void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
1507 struct sk_buff *skb)
1508{
1509 struct batadv_unicast_packet *packet;
1510 struct batadv_nc_path *nc_path;
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001511 struct ethhdr *ethhdr = eth_hdr(skb);
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001512 __be32 packet_id;
1513 u8 *payload;
1514
1515 /* Check if network coding is enabled */
1516 if (!atomic_read(&bat_priv->network_coding))
1517 goto out;
1518
1519 /* Check for supported packet type */
1520 payload = skb_network_header(skb);
1521 packet = (struct batadv_unicast_packet *)payload;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +01001522 if (packet->packet_type != BATADV_UNICAST)
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001523 goto out;
1524
1525 /* Find existing nc_path or create a new */
1526 nc_path = batadv_nc_get_path(bat_priv,
1527 bat_priv->nc.decoding_hash,
1528 ethhdr->h_source,
1529 ethhdr->h_dest);
1530
1531 if (!nc_path)
1532 goto out;
1533
1534 /* Clone skb and adjust skb->data to point at batman header */
1535 skb = skb_clone(skb, GFP_ATOMIC);
1536 if (unlikely(!skb))
1537 goto free_nc_path;
1538
1539 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
1540 goto free_skb;
1541
1542 if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
1543 goto free_skb;
1544
1545 /* Add skb to nc_path */
1546 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1547 if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
1548 goto free_skb;
1549
1550 batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
1551 return;
1552
1553free_skb:
1554 kfree_skb(skb);
1555free_nc_path:
1556 batadv_nc_path_free_ref(nc_path);
1557out:
1558 return;
1559}
1560
1561/**
1562 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1563 * should be saved in the decoding buffer and, if so, store it there
1564 * @bat_priv: the bat priv with all the soft interface information
1565 * @skb: unicast skb to store
1566 */
1567void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
1568 struct sk_buff *skb)
1569{
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001570 struct ethhdr *ethhdr = eth_hdr(skb);
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001571
David S. Miller6e0895c2013-04-22 20:32:51 -04001572 if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001573 return;
1574
1575 /* Set data pointer to MAC header to mimic packets from our tx path */
1576 skb_push(skb, ETH_HLEN);
1577
1578 batadv_nc_skb_store_for_decoding(bat_priv, skb);
1579}
1580
1581/**
Martin Hundebøll2df52782013-01-25 11:12:43 +01001582 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1583 * in nc_packet
David S. Miller6e0895c2013-04-22 20:32:51 -04001584 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebøll2df52782013-01-25 11:12:43 +01001585 * @skb: unicast skb to decode
1586 * @nc_packet: decode data needed to decode the skb
1587 *
1588 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1589 * in case of an error.
1590 */
1591static struct batadv_unicast_packet *
David S. Miller6e0895c2013-04-22 20:32:51 -04001592batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
Martin Hundebøll2df52782013-01-25 11:12:43 +01001593 struct batadv_nc_packet *nc_packet)
1594{
1595 const int h_size = sizeof(struct batadv_unicast_packet);
1596 const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
1597 struct batadv_unicast_packet *unicast_packet;
1598 struct batadv_coded_packet coded_packet_tmp;
1599 struct ethhdr *ethhdr, ethhdr_tmp;
1600 uint8_t *orig_dest, ttl, ttvn;
1601 unsigned int coding_len;
Marek Lindner7da19972013-05-07 19:25:02 +08001602 int err;
Martin Hundebøll2df52782013-01-25 11:12:43 +01001603
1604 /* Save headers temporarily */
1605 memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
1606 memcpy(&ethhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
1607
1608 if (skb_cow(skb, 0) < 0)
1609 return NULL;
1610
1611 if (unlikely(!skb_pull_rcsum(skb, h_diff)))
1612 return NULL;
1613
1614 /* Data points to batman header, so set mac header 14 bytes before
1615 * and network to data
1616 */
1617 skb_set_mac_header(skb, -ETH_HLEN);
1618 skb_reset_network_header(skb);
1619
1620 /* Reconstruct original mac header */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001621 ethhdr = eth_hdr(skb);
Martin Hundebøll2df52782013-01-25 11:12:43 +01001622 memcpy(ethhdr, &ethhdr_tmp, sizeof(*ethhdr));
1623
1624 /* Select the correct unicast header information based on the location
1625 * of our mac address in the coded_packet header
1626 */
David S. Miller6e0895c2013-04-22 20:32:51 -04001627 if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
Martin Hundebøll2df52782013-01-25 11:12:43 +01001628 /* If we are the second destination the packet was overheard,
1629 * so the Ethernet address must be copied to h_dest and
1630 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1631 */
1632 memcpy(ethhdr->h_dest, coded_packet_tmp.second_dest, ETH_ALEN);
1633 skb->pkt_type = PACKET_HOST;
1634
1635 orig_dest = coded_packet_tmp.second_orig_dest;
1636 ttl = coded_packet_tmp.second_ttl;
1637 ttvn = coded_packet_tmp.second_ttvn;
1638 } else {
1639 orig_dest = coded_packet_tmp.first_orig_dest;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +01001640 ttl = coded_packet_tmp.ttl;
Martin Hundebøll2df52782013-01-25 11:12:43 +01001641 ttvn = coded_packet_tmp.first_ttvn;
1642 }
1643
1644 coding_len = ntohs(coded_packet_tmp.coded_len);
1645
1646 if (coding_len > skb->len)
1647 return NULL;
1648
1649 /* Here the magic is reversed:
1650 * extract the missing packet from the received coded packet
1651 */
1652 batadv_nc_memxor(skb->data + h_size,
1653 nc_packet->skb->data + h_size,
1654 coding_len);
1655
1656 /* Resize decoded skb if decoded with larger packet */
Marek Lindner7da19972013-05-07 19:25:02 +08001657 if (nc_packet->skb->len > coding_len + h_size) {
1658 err = pskb_trim_rcsum(skb, coding_len + h_size);
1659 if (err)
1660 return NULL;
1661 }
Martin Hundebøll2df52782013-01-25 11:12:43 +01001662
1663 /* Create decoded unicast packet */
1664 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +01001665 unicast_packet->packet_type = BATADV_UNICAST;
1666 unicast_packet->version = BATADV_COMPAT_VERSION;
1667 unicast_packet->ttl = ttl;
Martin Hundebøll2df52782013-01-25 11:12:43 +01001668 memcpy(unicast_packet->dest, orig_dest, ETH_ALEN);
1669 unicast_packet->ttvn = ttvn;
1670
1671 batadv_nc_packet_free(nc_packet);
1672 return unicast_packet;
1673}
1674
1675/**
1676 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1677 * find the data needed to decode the coded packet
1678 * @bat_priv: the bat priv with all the soft interface information
1679 * @ethhdr: pointer to the ethernet header inside the coded packet
1680 * @coded: coded packet we try to find decode data for
1681 *
1682 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1683 */
1684static struct batadv_nc_packet *
1685batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
1686 struct ethhdr *ethhdr,
1687 struct batadv_coded_packet *coded)
1688{
1689 struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
1690 struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
1691 struct batadv_nc_path *nc_path, nc_path_key;
1692 uint8_t *dest, *source;
1693 __be32 packet_id;
1694 int index;
1695
1696 if (!hash)
1697 return NULL;
1698
1699 /* Select the correct packet id based on the location of our mac-addr */
1700 dest = ethhdr->h_source;
David S. Miller6e0895c2013-04-22 20:32:51 -04001701 if (!batadv_is_my_mac(bat_priv, coded->second_dest)) {
Martin Hundebøll2df52782013-01-25 11:12:43 +01001702 source = coded->second_source;
1703 packet_id = coded->second_crc;
1704 } else {
1705 source = coded->first_source;
1706 packet_id = coded->first_crc;
1707 }
1708
1709 batadv_nc_hash_key_gen(&nc_path_key, source, dest);
1710 index = batadv_nc_hash_choose(&nc_path_key, hash->size);
1711
1712 /* Search for matching coding path */
1713 rcu_read_lock();
1714 hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) {
1715 /* Find matching nc_packet */
1716 spin_lock_bh(&nc_path->packet_list_lock);
1717 list_for_each_entry(tmp_nc_packet,
1718 &nc_path->packet_list, list) {
1719 if (packet_id == tmp_nc_packet->packet_id) {
1720 list_del(&tmp_nc_packet->list);
1721
1722 nc_packet = tmp_nc_packet;
1723 break;
1724 }
1725 }
1726 spin_unlock_bh(&nc_path->packet_list_lock);
1727
1728 if (nc_packet)
1729 break;
1730 }
1731 rcu_read_unlock();
1732
1733 if (!nc_packet)
1734 batadv_dbg(BATADV_DBG_NC, bat_priv,
1735 "No decoding packet found for %u\n", packet_id);
1736
1737 return nc_packet;
1738}
1739
1740/**
1741 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1742 * resulting unicast packet
1743 * @skb: incoming coded packet
1744 * @recv_if: pointer to interface this packet was received on
1745 */
1746static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
1747 struct batadv_hard_iface *recv_if)
1748{
1749 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1750 struct batadv_unicast_packet *unicast_packet;
1751 struct batadv_coded_packet *coded_packet;
1752 struct batadv_nc_packet *nc_packet;
1753 struct ethhdr *ethhdr;
1754 int hdr_size = sizeof(*coded_packet);
1755
1756 /* Check if network coding is enabled */
1757 if (!atomic_read(&bat_priv->network_coding))
1758 return NET_RX_DROP;
1759
1760 /* Make sure we can access (and remove) header */
1761 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1762 return NET_RX_DROP;
1763
1764 coded_packet = (struct batadv_coded_packet *)skb->data;
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001765 ethhdr = eth_hdr(skb);
Martin Hundebøll2df52782013-01-25 11:12:43 +01001766
1767 /* Verify frame is destined for us */
David S. Miller6e0895c2013-04-22 20:32:51 -04001768 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) &&
1769 !batadv_is_my_mac(bat_priv, coded_packet->second_dest))
Martin Hundebøll2df52782013-01-25 11:12:43 +01001770 return NET_RX_DROP;
1771
1772 /* Update stat counter */
David S. Miller6e0895c2013-04-22 20:32:51 -04001773 if (batadv_is_my_mac(bat_priv, coded_packet->second_dest))
Martin Hundebøll2df52782013-01-25 11:12:43 +01001774 batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
1775
1776 nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
1777 coded_packet);
1778 if (!nc_packet) {
1779 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
1780 return NET_RX_DROP;
1781 }
1782
1783 /* Make skb's linear, because decoding accesses the entire buffer */
1784 if (skb_linearize(skb) < 0)
1785 goto free_nc_packet;
1786
1787 if (skb_linearize(nc_packet->skb) < 0)
1788 goto free_nc_packet;
1789
1790 /* Decode the packet */
David S. Miller6e0895c2013-04-22 20:32:51 -04001791 unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet);
Martin Hundebøll2df52782013-01-25 11:12:43 +01001792 if (!unicast_packet) {
1793 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
1794 goto free_nc_packet;
1795 }
1796
1797 /* Mark packet as decoded to do correct recoding when forwarding */
1798 BATADV_SKB_CB(skb)->decoded = true;
1799 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
1800 batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
1801 skb->len + ETH_HLEN);
1802 return batadv_recv_unicast_packet(skb, recv_if);
1803
1804free_nc_packet:
1805 batadv_nc_packet_free(nc_packet);
1806 return NET_RX_DROP;
1807}
1808
1809/**
Matthias Schiffer6c519ba2013-09-27 18:03:39 +02001810 * batadv_nc_mesh_free - clean up network coding memory
Martin Hundebølld353d8d2013-01-25 11:12:38 +01001811 * @bat_priv: the bat priv with all the soft interface information
1812 */
Matthias Schiffer6c519ba2013-09-27 18:03:39 +02001813void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
Martin Hundebølld353d8d2013-01-25 11:12:38 +01001814{
Marek Lindner3f4841f2013-04-23 21:40:00 +08001815 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
1816 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
Martin Hundebølld353d8d2013-01-25 11:12:38 +01001817 cancel_delayed_work_sync(&bat_priv->nc.work);
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001818
Martin Hundebøll95332472013-01-25 11:12:40 +01001819 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
1820 batadv_hash_destroy(bat_priv->nc.coding_hash);
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001821 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
1822 batadv_hash_destroy(bat_priv->nc.decoding_hash);
Martin Hundebølld353d8d2013-01-25 11:12:38 +01001823}
Martin Hundebølld56b1702013-01-25 11:12:39 +01001824
1825/**
1826 * batadv_nc_nodes_seq_print_text - print the nc node information
1827 * @seq: seq file to print on
1828 * @offset: not used
1829 */
1830int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
1831{
1832 struct net_device *net_dev = (struct net_device *)seq->private;
1833 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1834 struct batadv_hashtable *hash = bat_priv->orig_hash;
1835 struct batadv_hard_iface *primary_if;
1836 struct hlist_head *head;
1837 struct batadv_orig_node *orig_node;
1838 struct batadv_nc_node *nc_node;
1839 int i;
1840
1841 primary_if = batadv_seq_print_text_primary_if_get(seq);
1842 if (!primary_if)
1843 goto out;
1844
1845 /* Traverse list of originators */
1846 for (i = 0; i < hash->size; i++) {
1847 head = &hash->table[i];
1848
1849 /* For each orig_node in this bin */
1850 rcu_read_lock();
1851 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Marek Lindneraa27c3122013-04-18 04:56:03 +08001852 /* no need to print the orig node if it does not have
1853 * network coding neighbors
1854 */
1855 if (list_empty(&orig_node->in_coding_list) &&
1856 list_empty(&orig_node->out_coding_list))
1857 continue;
1858
Martin Hundebølld56b1702013-01-25 11:12:39 +01001859 seq_printf(seq, "Node: %pM\n", orig_node->orig);
1860
Antonio Quartulli0c814652013-03-21 09:23:29 +01001861 seq_puts(seq, " Ingoing: ");
Martin Hundebølld56b1702013-01-25 11:12:39 +01001862 /* For each in_nc_node to this orig_node */
1863 list_for_each_entry_rcu(nc_node,
1864 &orig_node->in_coding_list,
1865 list)
1866 seq_printf(seq, "%pM ",
1867 nc_node->addr);
Antonio Quartulli0c814652013-03-21 09:23:29 +01001868 seq_puts(seq, "\n");
Martin Hundebølld56b1702013-01-25 11:12:39 +01001869
Antonio Quartulli0c814652013-03-21 09:23:29 +01001870 seq_puts(seq, " Outgoing: ");
Martin Hundebølld56b1702013-01-25 11:12:39 +01001871 /* For out_nc_node to this orig_node */
1872 list_for_each_entry_rcu(nc_node,
1873 &orig_node->out_coding_list,
1874 list)
1875 seq_printf(seq, "%pM ",
1876 nc_node->addr);
Antonio Quartulli0c814652013-03-21 09:23:29 +01001877 seq_puts(seq, "\n\n");
Martin Hundebølld56b1702013-01-25 11:12:39 +01001878 }
1879 rcu_read_unlock();
1880 }
1881
1882out:
1883 if (primary_if)
1884 batadv_hardif_free_ref(primary_if);
1885 return 0;
1886}
1887
1888/**
1889 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1890 * @bat_priv: the bat priv with all the soft interface information
1891 */
1892int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
1893{
1894 struct dentry *nc_dir, *file;
1895
1896 nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
1897 if (!nc_dir)
1898 goto out;
1899
1900 file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
1901 &bat_priv->nc.min_tq);
1902 if (!file)
1903 goto out;
1904
Martin Hundebøll95332472013-01-25 11:12:40 +01001905 file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir,
1906 &bat_priv->nc.max_fwd_delay);
1907 if (!file)
1908 goto out;
1909
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001910 file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir,
1911 &bat_priv->nc.max_buffer_time);
1912 if (!file)
1913 goto out;
1914
Martin Hundebølld56b1702013-01-25 11:12:39 +01001915 return 0;
1916
1917out:
1918 return -ENOMEM;
1919}