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