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