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