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