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, tq_tmp;
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_tmp = batadv_nc_random_weight_tq(router_neigh->bat_iv.tq_avg);
1032 	tq_weighted_neigh = tq_tmp;
1033 	tq_tmp = batadv_nc_random_weight_tq(router_coding->bat_iv.tq_avg);
1034 	tq_weighted_coding = tq_tmp;
1035 
1036 	/* Select one destination for the MAC-header dst-field based on
1037 	 * weighted TQ-values.
1038 	 */
1039 	if (tq_weighted_neigh >= tq_weighted_coding) {
1040 		/* Destination from nc_packet is selected for MAC-header */
1041 		first_dest = nc_packet->nc_path->next_hop;
1042 		first_source = nc_packet->nc_path->prev_hop;
1043 		second_dest = neigh_node->addr;
1044 		second_source = ethhdr->h_source;
1045 		packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
1046 		packet2 = (struct batadv_unicast_packet *)skb->data;
1047 		packet_id1 = nc_packet->packet_id;
1048 		packet_id2 = batadv_skb_crc32(skb,
1049 					      skb->data + sizeof(*packet2));
1050 	} else {
1051 		/* Destination for skb is selected for MAC-header */
1052 		first_dest = neigh_node->addr;
1053 		first_source = ethhdr->h_source;
1054 		second_dest = nc_packet->nc_path->next_hop;
1055 		second_source = nc_packet->nc_path->prev_hop;
1056 		packet1 = (struct batadv_unicast_packet *)skb->data;
1057 		packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
1058 		packet_id1 = batadv_skb_crc32(skb,
1059 					      skb->data + sizeof(*packet1));
1060 		packet_id2 = nc_packet->packet_id;
1061 	}
1062 
1063 	/* Instead of zero padding the smallest data buffer, we
1064 	 * code into the largest.
1065 	 */
1066 	if (skb->len <= nc_packet->skb->len) {
1067 		skb_dest = nc_packet->skb;
1068 		skb_src = skb;
1069 	} else {
1070 		skb_dest = skb;
1071 		skb_src = nc_packet->skb;
1072 	}
1073 
1074 	/* coding_len is used when decoding the packet shorter packet */
1075 	coding_len = skb_src->len - unicast_size;
1076 
1077 	if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
1078 		goto out;
1079 
1080 	skb_push(skb_dest, header_add);
1081 
1082 	coded_packet = (struct batadv_coded_packet *)skb_dest->data;
1083 	skb_reset_mac_header(skb_dest);
1084 
1085 	coded_packet->header.packet_type = BATADV_CODED;
1086 	coded_packet->header.version = BATADV_COMPAT_VERSION;
1087 	coded_packet->header.ttl = packet1->header.ttl;
1088 
1089 	/* Info about first unicast packet */
1090 	memcpy(coded_packet->first_source, first_source, ETH_ALEN);
1091 	memcpy(coded_packet->first_orig_dest, packet1->dest, ETH_ALEN);
1092 	coded_packet->first_crc = packet_id1;
1093 	coded_packet->first_ttvn = packet1->ttvn;
1094 
1095 	/* Info about second unicast packet */
1096 	memcpy(coded_packet->second_dest, second_dest, ETH_ALEN);
1097 	memcpy(coded_packet->second_source, second_source, ETH_ALEN);
1098 	memcpy(coded_packet->second_orig_dest, packet2->dest, ETH_ALEN);
1099 	coded_packet->second_crc = packet_id2;
1100 	coded_packet->second_ttl = packet2->header.ttl;
1101 	coded_packet->second_ttvn = packet2->ttvn;
1102 	coded_packet->coded_len = htons(coding_len);
1103 
1104 	/* This is where the magic happens: Code skb_src into skb_dest */
1105 	batadv_nc_memxor(skb_dest->data + coded_size,
1106 			 skb_src->data + unicast_size, coding_len);
1107 
1108 	/* Update counters accordingly */
1109 	if (BATADV_SKB_CB(skb_src)->decoded &&
1110 	    BATADV_SKB_CB(skb_dest)->decoded) {
1111 		/* Both packets are recoded */
1112 		count = skb_src->len + ETH_HLEN;
1113 		count += skb_dest->len + ETH_HLEN;
1114 		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
1115 		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
1116 	} else if (!BATADV_SKB_CB(skb_src)->decoded &&
1117 		   !BATADV_SKB_CB(skb_dest)->decoded) {
1118 		/* Both packets are newly coded */
1119 		count = skb_src->len + ETH_HLEN;
1120 		count += skb_dest->len + ETH_HLEN;
1121 		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
1122 		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
1123 	} else if (BATADV_SKB_CB(skb_src)->decoded &&
1124 		   !BATADV_SKB_CB(skb_dest)->decoded) {
1125 		/* skb_src recoded and skb_dest is newly coded */
1126 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1127 		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1128 				   skb_src->len + ETH_HLEN);
1129 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1130 		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1131 				   skb_dest->len + ETH_HLEN);
1132 	} else if (!BATADV_SKB_CB(skb_src)->decoded &&
1133 		   BATADV_SKB_CB(skb_dest)->decoded) {
1134 		/* skb_src is newly coded and skb_dest is recoded */
1135 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1136 		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1137 				   skb_src->len + ETH_HLEN);
1138 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1139 		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1140 				   skb_dest->len + ETH_HLEN);
1141 	}
1142 
1143 	/* skb_src is now coded into skb_dest, so free it */
1144 	kfree_skb(skb_src);
1145 
1146 	/* avoid duplicate free of skb from nc_packet */
1147 	nc_packet->skb = NULL;
1148 	batadv_nc_packet_free(nc_packet);
1149 
1150 	/* Send the coded packet and return true */
1151 	batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest);
1152 	res = true;
1153 out:
1154 	if (router_neigh)
1155 		batadv_neigh_node_free_ref(router_neigh);
1156 	if (router_coding)
1157 		batadv_neigh_node_free_ref(router_coding);
1158 	return res;
1159 }
1160 
1161 /**
1162  * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1163  * @skb: data skb to forward
1164  * @dst: destination mac address of the other skb to code with
1165  * @src: source mac address of skb
1166  *
1167  * Whenever we network code a packet we have to check whether we received it in
1168  * a network coded form. If so, we may not be able to use it for coding because
1169  * some neighbors may also have received (overheard) the packet in the network
1170  * coded form without being able to decode it. It is hard to know which of the
1171  * neighboring nodes was able to decode the packet, therefore we can only
1172  * re-code the packet if the source of the previous encoded packet is involved.
1173  * Since the source encoded the packet we can be certain it has all necessary
1174  * decode information.
1175  *
1176  * Returns true if coding of a decoded packet is allowed.
1177  */
1178 static bool batadv_nc_skb_coding_possible(struct sk_buff *skb,
1179 					  uint8_t *dst, uint8_t *src)
1180 {
1181 	if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
1182 		return false;
1183 	else
1184 		return true;
1185 }
1186 
1187 /**
1188  * batadv_nc_path_search - Find the coding path matching in_nc_node and
1189  *  out_nc_node to retrieve a buffered packet that can be used for coding.
1190  * @bat_priv: the bat priv with all the soft interface information
1191  * @in_nc_node: pointer to skb next hop's neighbor nc node
1192  * @out_nc_node: pointer to skb source's neighbor nc node
1193  * @skb: data skb to forward
1194  * @eth_dst: next hop mac address of skb
1195  *
1196  * Returns true if coding of a decoded skb is allowed.
1197  */
1198 static struct batadv_nc_packet *
1199 batadv_nc_path_search(struct batadv_priv *bat_priv,
1200 		      struct batadv_nc_node *in_nc_node,
1201 		      struct batadv_nc_node *out_nc_node,
1202 		      struct sk_buff *skb,
1203 		      uint8_t *eth_dst)
1204 {
1205 	struct batadv_nc_path *nc_path, nc_path_key;
1206 	struct batadv_nc_packet *nc_packet_out = NULL;
1207 	struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
1208 	struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
1209 	int idx;
1210 
1211 	if (!hash)
1212 		return NULL;
1213 
1214 	/* Create almost path key */
1215 	batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
1216 			       out_nc_node->addr);
1217 	idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
1218 
1219 	/* Check for coding opportunities in this nc_path */
1220 	rcu_read_lock();
1221 	hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
1222 		if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
1223 			continue;
1224 
1225 		if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
1226 			continue;
1227 
1228 		spin_lock_bh(&nc_path->packet_list_lock);
1229 		if (list_empty(&nc_path->packet_list)) {
1230 			spin_unlock_bh(&nc_path->packet_list_lock);
1231 			continue;
1232 		}
1233 
1234 		list_for_each_entry_safe(nc_packet, nc_packet_tmp,
1235 					 &nc_path->packet_list, list) {
1236 			if (!batadv_nc_skb_coding_possible(nc_packet->skb,
1237 							   eth_dst,
1238 							   in_nc_node->addr))
1239 				continue;
1240 
1241 			/* Coding opportunity is found! */
1242 			list_del(&nc_packet->list);
1243 			nc_packet_out = nc_packet;
1244 			break;
1245 		}
1246 
1247 		spin_unlock_bh(&nc_path->packet_list_lock);
1248 		break;
1249 	}
1250 	rcu_read_unlock();
1251 
1252 	return nc_packet_out;
1253 }
1254 
1255 /**
1256  * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1257  *  skb's sender (may be equal to the originator).
1258  * @bat_priv: the bat priv with all the soft interface information
1259  * @skb: data skb to forward
1260  * @eth_dst: next hop mac address of skb
1261  * @eth_src: source mac address of skb
1262  * @in_nc_node: pointer to skb next hop's neighbor nc node
1263  *
1264  * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1265  */
1266 static struct batadv_nc_packet *
1267 batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
1268 			 struct sk_buff *skb,
1269 			 uint8_t *eth_dst,
1270 			 uint8_t *eth_src,
1271 			 struct batadv_nc_node *in_nc_node)
1272 {
1273 	struct batadv_orig_node *orig_node;
1274 	struct batadv_nc_node *out_nc_node;
1275 	struct batadv_nc_packet *nc_packet = NULL;
1276 
1277 	orig_node = batadv_orig_hash_find(bat_priv, eth_src);
1278 	if (!orig_node)
1279 		return NULL;
1280 
1281 	rcu_read_lock();
1282 	list_for_each_entry_rcu(out_nc_node,
1283 				&orig_node->out_coding_list, list) {
1284 		/* Check if the skb is decoded and if recoding is possible */
1285 		if (!batadv_nc_skb_coding_possible(skb,
1286 						   out_nc_node->addr, eth_src))
1287 			continue;
1288 
1289 		/* Search for an opportunity in this nc_path */
1290 		nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
1291 						  out_nc_node, skb, eth_dst);
1292 		if (nc_packet)
1293 			break;
1294 	}
1295 	rcu_read_unlock();
1296 
1297 	batadv_orig_node_free_ref(orig_node);
1298 	return nc_packet;
1299 }
1300 
1301 /**
1302  * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1303  *  unicast skb before it is stored for use in later decoding
1304  * @bat_priv: the bat priv with all the soft interface information
1305  * @skb: data skb to store
1306  * @eth_dst_new: new destination mac address of skb
1307  */
1308 static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
1309 					      struct sk_buff *skb,
1310 					      uint8_t *eth_dst_new)
1311 {
1312 	struct ethhdr *ethhdr;
1313 
1314 	/* Copy skb header to change the mac header */
1315 	skb = pskb_copy(skb, GFP_ATOMIC);
1316 	if (!skb)
1317 		return;
1318 
1319 	/* Set the mac header as if we actually sent the packet uncoded */
1320 	ethhdr = eth_hdr(skb);
1321 	memcpy(ethhdr->h_source, ethhdr->h_dest, ETH_ALEN);
1322 	memcpy(ethhdr->h_dest, eth_dst_new, ETH_ALEN);
1323 
1324 	/* Set data pointer to MAC header to mimic packets from our tx path */
1325 	skb_push(skb, ETH_HLEN);
1326 
1327 	/* Add the packet to the decoding packet pool */
1328 	batadv_nc_skb_store_for_decoding(bat_priv, skb);
1329 
1330 	/* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1331 	 * our ref
1332 	 */
1333 	kfree_skb(skb);
1334 }
1335 
1336 /**
1337  * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1338  * @skb: data skb to forward
1339  * @neigh_node: next hop to forward packet to
1340  * @ethhdr: pointer to the ethernet header inside the skb
1341  *
1342  * Loops through list of neighboring nodes the next hop has a good connection to
1343  * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1344  * next hop that potentially sent a packet which our next hop also received
1345  * (overheard) and has stored for later decoding.
1346  *
1347  * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1348  */
1349 static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
1350 				     struct batadv_neigh_node *neigh_node,
1351 				     struct ethhdr *ethhdr)
1352 {
1353 	struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1354 	struct batadv_priv *bat_priv = netdev_priv(netdev);
1355 	struct batadv_orig_node *orig_node = neigh_node->orig_node;
1356 	struct batadv_nc_node *nc_node;
1357 	struct batadv_nc_packet *nc_packet = NULL;
1358 
1359 	rcu_read_lock();
1360 	list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
1361 		/* Search for coding opportunity with this in_nc_node */
1362 		nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
1363 						     neigh_node->addr,
1364 						     ethhdr->h_source, nc_node);
1365 
1366 		/* Opportunity was found, so stop searching */
1367 		if (nc_packet)
1368 			break;
1369 	}
1370 	rcu_read_unlock();
1371 
1372 	if (!nc_packet)
1373 		return false;
1374 
1375 	/* Save packets for later decoding */
1376 	batadv_nc_skb_store_before_coding(bat_priv, skb,
1377 					  neigh_node->addr);
1378 	batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
1379 					  nc_packet->neigh_node->addr);
1380 
1381 	/* Code and send packets */
1382 	if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
1383 				   neigh_node))
1384 		return true;
1385 
1386 	/* out of mem ? Coding failed - we have to free the buffered packet
1387 	 * to avoid memleaks. The skb passed as argument will be dealt with
1388 	 * by the calling function.
1389 	 */
1390 	batadv_nc_send_packet(nc_packet);
1391 	return false;
1392 }
1393 
1394 /**
1395  * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1396  * @skb: skb to add to path
1397  * @nc_path: path to add skb to
1398  * @neigh_node: next hop to forward packet to
1399  * @packet_id: checksum to identify packet
1400  *
1401  * Returns true if the packet was buffered or false in case of an error.
1402  */
1403 static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
1404 				      struct batadv_nc_path *nc_path,
1405 				      struct batadv_neigh_node *neigh_node,
1406 				      __be32 packet_id)
1407 {
1408 	struct batadv_nc_packet *nc_packet;
1409 
1410 	nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
1411 	if (!nc_packet)
1412 		return false;
1413 
1414 	/* Initialize nc_packet */
1415 	nc_packet->timestamp = jiffies;
1416 	nc_packet->packet_id = packet_id;
1417 	nc_packet->skb = skb;
1418 	nc_packet->neigh_node = neigh_node;
1419 	nc_packet->nc_path = nc_path;
1420 
1421 	/* Add coding packet to list */
1422 	spin_lock_bh(&nc_path->packet_list_lock);
1423 	list_add_tail(&nc_packet->list, &nc_path->packet_list);
1424 	spin_unlock_bh(&nc_path->packet_list_lock);
1425 
1426 	return true;
1427 }
1428 
1429 /**
1430  * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1431  *  buffer
1432  * @skb: data skb to forward
1433  * @neigh_node: next hop to forward packet to
1434  *
1435  * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1436  */
1437 bool batadv_nc_skb_forward(struct sk_buff *skb,
1438 			   struct batadv_neigh_node *neigh_node)
1439 {
1440 	const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1441 	struct batadv_priv *bat_priv = netdev_priv(netdev);
1442 	struct batadv_unicast_packet *packet;
1443 	struct batadv_nc_path *nc_path;
1444 	struct ethhdr *ethhdr = eth_hdr(skb);
1445 	__be32 packet_id;
1446 	u8 *payload;
1447 
1448 	/* Check if network coding is enabled */
1449 	if (!atomic_read(&bat_priv->network_coding))
1450 		goto out;
1451 
1452 	/* We only handle unicast packets */
1453 	payload = skb_network_header(skb);
1454 	packet = (struct batadv_unicast_packet *)payload;
1455 	if (packet->header.packet_type != BATADV_UNICAST)
1456 		goto out;
1457 
1458 	/* Try to find a coding opportunity and send the skb if one is found */
1459 	if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
1460 		return true;
1461 
1462 	/* Find or create a nc_path for this src-dst pair */
1463 	nc_path = batadv_nc_get_path(bat_priv,
1464 				     bat_priv->nc.coding_hash,
1465 				     ethhdr->h_source,
1466 				     neigh_node->addr);
1467 
1468 	if (!nc_path)
1469 		goto out;
1470 
1471 	/* Add skb to nc_path */
1472 	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1473 	if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
1474 		goto free_nc_path;
1475 
1476 	/* Packet is consumed */
1477 	return true;
1478 
1479 free_nc_path:
1480 	batadv_nc_path_free_ref(nc_path);
1481 out:
1482 	/* Packet is not consumed */
1483 	return false;
1484 }
1485 
1486 /**
1487  * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1488  *  when decoding coded packets
1489  * @bat_priv: the bat priv with all the soft interface information
1490  * @skb: data skb to store
1491  */
1492 void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
1493 				      struct sk_buff *skb)
1494 {
1495 	struct batadv_unicast_packet *packet;
1496 	struct batadv_nc_path *nc_path;
1497 	struct ethhdr *ethhdr = eth_hdr(skb);
1498 	__be32 packet_id;
1499 	u8 *payload;
1500 
1501 	/* Check if network coding is enabled */
1502 	if (!atomic_read(&bat_priv->network_coding))
1503 		goto out;
1504 
1505 	/* Check for supported packet type */
1506 	payload = skb_network_header(skb);
1507 	packet = (struct batadv_unicast_packet *)payload;
1508 	if (packet->header.packet_type != BATADV_UNICAST)
1509 		goto out;
1510 
1511 	/* Find existing nc_path or create a new */
1512 	nc_path = batadv_nc_get_path(bat_priv,
1513 				     bat_priv->nc.decoding_hash,
1514 				     ethhdr->h_source,
1515 				     ethhdr->h_dest);
1516 
1517 	if (!nc_path)
1518 		goto out;
1519 
1520 	/* Clone skb and adjust skb->data to point at batman header */
1521 	skb = skb_clone(skb, GFP_ATOMIC);
1522 	if (unlikely(!skb))
1523 		goto free_nc_path;
1524 
1525 	if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
1526 		goto free_skb;
1527 
1528 	if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
1529 		goto free_skb;
1530 
1531 	/* Add skb to nc_path */
1532 	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1533 	if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
1534 		goto free_skb;
1535 
1536 	batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
1537 	return;
1538 
1539 free_skb:
1540 	kfree_skb(skb);
1541 free_nc_path:
1542 	batadv_nc_path_free_ref(nc_path);
1543 out:
1544 	return;
1545 }
1546 
1547 /**
1548  * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1549  *  should be saved in the decoding buffer and, if so, store it there
1550  * @bat_priv: the bat priv with all the soft interface information
1551  * @skb: unicast skb to store
1552  */
1553 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
1554 					 struct sk_buff *skb)
1555 {
1556 	struct ethhdr *ethhdr = eth_hdr(skb);
1557 
1558 	if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
1559 		return;
1560 
1561 	/* Set data pointer to MAC header to mimic packets from our tx path */
1562 	skb_push(skb, ETH_HLEN);
1563 
1564 	batadv_nc_skb_store_for_decoding(bat_priv, skb);
1565 }
1566 
1567 /**
1568  * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1569  *  in nc_packet
1570  * @bat_priv: the bat priv with all the soft interface information
1571  * @skb: unicast skb to decode
1572  * @nc_packet: decode data needed to decode the skb
1573  *
1574  * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1575  * in case of an error.
1576  */
1577 static struct batadv_unicast_packet *
1578 batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
1579 			    struct batadv_nc_packet *nc_packet)
1580 {
1581 	const int h_size = sizeof(struct batadv_unicast_packet);
1582 	const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
1583 	struct batadv_unicast_packet *unicast_packet;
1584 	struct batadv_coded_packet coded_packet_tmp;
1585 	struct ethhdr *ethhdr, ethhdr_tmp;
1586 	uint8_t *orig_dest, ttl, ttvn;
1587 	unsigned int coding_len;
1588 	int err;
1589 
1590 	/* Save headers temporarily */
1591 	memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
1592 	memcpy(&ethhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
1593 
1594 	if (skb_cow(skb, 0) < 0)
1595 		return NULL;
1596 
1597 	if (unlikely(!skb_pull_rcsum(skb, h_diff)))
1598 		return NULL;
1599 
1600 	/* Data points to batman header, so set mac header 14 bytes before
1601 	 * and network to data
1602 	 */
1603 	skb_set_mac_header(skb, -ETH_HLEN);
1604 	skb_reset_network_header(skb);
1605 
1606 	/* Reconstruct original mac header */
1607 	ethhdr = eth_hdr(skb);
1608 	memcpy(ethhdr, &ethhdr_tmp, sizeof(*ethhdr));
1609 
1610 	/* Select the correct unicast header information based on the location
1611 	 * of our mac address in the coded_packet header
1612 	 */
1613 	if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
1614 		/* If we are the second destination the packet was overheard,
1615 		 * so the Ethernet address must be copied to h_dest and
1616 		 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1617 		 */
1618 		memcpy(ethhdr->h_dest, coded_packet_tmp.second_dest, ETH_ALEN);
1619 		skb->pkt_type = PACKET_HOST;
1620 
1621 		orig_dest = coded_packet_tmp.second_orig_dest;
1622 		ttl = coded_packet_tmp.second_ttl;
1623 		ttvn = coded_packet_tmp.second_ttvn;
1624 	} else {
1625 		orig_dest = coded_packet_tmp.first_orig_dest;
1626 		ttl = coded_packet_tmp.header.ttl;
1627 		ttvn = coded_packet_tmp.first_ttvn;
1628 	}
1629 
1630 	coding_len = ntohs(coded_packet_tmp.coded_len);
1631 
1632 	if (coding_len > skb->len)
1633 		return NULL;
1634 
1635 	/* Here the magic is reversed:
1636 	 *   extract the missing packet from the received coded packet
1637 	 */
1638 	batadv_nc_memxor(skb->data + h_size,
1639 			 nc_packet->skb->data + h_size,
1640 			 coding_len);
1641 
1642 	/* Resize decoded skb if decoded with larger packet */
1643 	if (nc_packet->skb->len > coding_len + h_size) {
1644 		err = pskb_trim_rcsum(skb, coding_len + h_size);
1645 		if (err)
1646 			return NULL;
1647 	}
1648 
1649 	/* Create decoded unicast packet */
1650 	unicast_packet = (struct batadv_unicast_packet *)skb->data;
1651 	unicast_packet->header.packet_type = BATADV_UNICAST;
1652 	unicast_packet->header.version = BATADV_COMPAT_VERSION;
1653 	unicast_packet->header.ttl = ttl;
1654 	memcpy(unicast_packet->dest, orig_dest, ETH_ALEN);
1655 	unicast_packet->ttvn = ttvn;
1656 
1657 	batadv_nc_packet_free(nc_packet);
1658 	return unicast_packet;
1659 }
1660 
1661 /**
1662  * batadv_nc_find_decoding_packet - search through buffered decoding data to
1663  *  find the data needed to decode the coded packet
1664  * @bat_priv: the bat priv with all the soft interface information
1665  * @ethhdr: pointer to the ethernet header inside the coded packet
1666  * @coded: coded packet we try to find decode data for
1667  *
1668  * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1669  */
1670 static struct batadv_nc_packet *
1671 batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
1672 			       struct ethhdr *ethhdr,
1673 			       struct batadv_coded_packet *coded)
1674 {
1675 	struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
1676 	struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
1677 	struct batadv_nc_path *nc_path, nc_path_key;
1678 	uint8_t *dest, *source;
1679 	__be32 packet_id;
1680 	int index;
1681 
1682 	if (!hash)
1683 		return NULL;
1684 
1685 	/* Select the correct packet id based on the location of our mac-addr */
1686 	dest = ethhdr->h_source;
1687 	if (!batadv_is_my_mac(bat_priv, coded->second_dest)) {
1688 		source = coded->second_source;
1689 		packet_id = coded->second_crc;
1690 	} else {
1691 		source = coded->first_source;
1692 		packet_id = coded->first_crc;
1693 	}
1694 
1695 	batadv_nc_hash_key_gen(&nc_path_key, source, dest);
1696 	index = batadv_nc_hash_choose(&nc_path_key, hash->size);
1697 
1698 	/* Search for matching coding path */
1699 	rcu_read_lock();
1700 	hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) {
1701 		/* Find matching nc_packet */
1702 		spin_lock_bh(&nc_path->packet_list_lock);
1703 		list_for_each_entry(tmp_nc_packet,
1704 				    &nc_path->packet_list, list) {
1705 			if (packet_id == tmp_nc_packet->packet_id) {
1706 				list_del(&tmp_nc_packet->list);
1707 
1708 				nc_packet = tmp_nc_packet;
1709 				break;
1710 			}
1711 		}
1712 		spin_unlock_bh(&nc_path->packet_list_lock);
1713 
1714 		if (nc_packet)
1715 			break;
1716 	}
1717 	rcu_read_unlock();
1718 
1719 	if (!nc_packet)
1720 		batadv_dbg(BATADV_DBG_NC, bat_priv,
1721 			   "No decoding packet found for %u\n", packet_id);
1722 
1723 	return nc_packet;
1724 }
1725 
1726 /**
1727  * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1728  *  resulting unicast packet
1729  * @skb: incoming coded packet
1730  * @recv_if: pointer to interface this packet was received on
1731  */
1732 static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
1733 				       struct batadv_hard_iface *recv_if)
1734 {
1735 	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1736 	struct batadv_unicast_packet *unicast_packet;
1737 	struct batadv_coded_packet *coded_packet;
1738 	struct batadv_nc_packet *nc_packet;
1739 	struct ethhdr *ethhdr;
1740 	int hdr_size = sizeof(*coded_packet);
1741 
1742 	/* Check if network coding is enabled */
1743 	if (!atomic_read(&bat_priv->network_coding))
1744 		return NET_RX_DROP;
1745 
1746 	/* Make sure we can access (and remove) header */
1747 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
1748 		return NET_RX_DROP;
1749 
1750 	coded_packet = (struct batadv_coded_packet *)skb->data;
1751 	ethhdr = eth_hdr(skb);
1752 
1753 	/* Verify frame is destined for us */
1754 	if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) &&
1755 	    !batadv_is_my_mac(bat_priv, coded_packet->second_dest))
1756 		return NET_RX_DROP;
1757 
1758 	/* Update stat counter */
1759 	if (batadv_is_my_mac(bat_priv, coded_packet->second_dest))
1760 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
1761 
1762 	nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
1763 						   coded_packet);
1764 	if (!nc_packet) {
1765 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
1766 		return NET_RX_DROP;
1767 	}
1768 
1769 	/* Make skb's linear, because decoding accesses the entire buffer */
1770 	if (skb_linearize(skb) < 0)
1771 		goto free_nc_packet;
1772 
1773 	if (skb_linearize(nc_packet->skb) < 0)
1774 		goto free_nc_packet;
1775 
1776 	/* Decode the packet */
1777 	unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet);
1778 	if (!unicast_packet) {
1779 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
1780 		goto free_nc_packet;
1781 	}
1782 
1783 	/* Mark packet as decoded to do correct recoding when forwarding */
1784 	BATADV_SKB_CB(skb)->decoded = true;
1785 	batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
1786 	batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
1787 			   skb->len + ETH_HLEN);
1788 	return batadv_recv_unicast_packet(skb, recv_if);
1789 
1790 free_nc_packet:
1791 	batadv_nc_packet_free(nc_packet);
1792 	return NET_RX_DROP;
1793 }
1794 
1795 /**
1796  * batadv_nc_mesh_free - clean up network coding memory
1797  * @bat_priv: the bat priv with all the soft interface information
1798  */
1799 void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
1800 {
1801 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
1802 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
1803 	cancel_delayed_work_sync(&bat_priv->nc.work);
1804 
1805 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
1806 	batadv_hash_destroy(bat_priv->nc.coding_hash);
1807 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
1808 	batadv_hash_destroy(bat_priv->nc.decoding_hash);
1809 }
1810 
1811 /**
1812  * batadv_nc_nodes_seq_print_text - print the nc node information
1813  * @seq: seq file to print on
1814  * @offset: not used
1815  */
1816 int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
1817 {
1818 	struct net_device *net_dev = (struct net_device *)seq->private;
1819 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1820 	struct batadv_hashtable *hash = bat_priv->orig_hash;
1821 	struct batadv_hard_iface *primary_if;
1822 	struct hlist_head *head;
1823 	struct batadv_orig_node *orig_node;
1824 	struct batadv_nc_node *nc_node;
1825 	int i;
1826 
1827 	primary_if = batadv_seq_print_text_primary_if_get(seq);
1828 	if (!primary_if)
1829 		goto out;
1830 
1831 	/* Traverse list of originators */
1832 	for (i = 0; i < hash->size; i++) {
1833 		head = &hash->table[i];
1834 
1835 		/* For each orig_node in this bin */
1836 		rcu_read_lock();
1837 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1838 			/* no need to print the orig node if it does not have
1839 			 * network coding neighbors
1840 			 */
1841 			if (list_empty(&orig_node->in_coding_list) &&
1842 			    list_empty(&orig_node->out_coding_list))
1843 				continue;
1844 
1845 			seq_printf(seq, "Node:      %pM\n", orig_node->orig);
1846 
1847 			seq_puts(seq, " Ingoing:  ");
1848 			/* For each in_nc_node to this orig_node */
1849 			list_for_each_entry_rcu(nc_node,
1850 						&orig_node->in_coding_list,
1851 						list)
1852 				seq_printf(seq, "%pM ",
1853 					   nc_node->addr);
1854 			seq_puts(seq, "\n");
1855 
1856 			seq_puts(seq, " Outgoing: ");
1857 			/* For out_nc_node to this orig_node */
1858 			list_for_each_entry_rcu(nc_node,
1859 						&orig_node->out_coding_list,
1860 						list)
1861 				seq_printf(seq, "%pM ",
1862 					   nc_node->addr);
1863 			seq_puts(seq, "\n\n");
1864 		}
1865 		rcu_read_unlock();
1866 	}
1867 
1868 out:
1869 	if (primary_if)
1870 		batadv_hardif_free_ref(primary_if);
1871 	return 0;
1872 }
1873 
1874 /**
1875  * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1876  * @bat_priv: the bat priv with all the soft interface information
1877  */
1878 int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
1879 {
1880 	struct dentry *nc_dir, *file;
1881 
1882 	nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
1883 	if (!nc_dir)
1884 		goto out;
1885 
1886 	file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
1887 				 &bat_priv->nc.min_tq);
1888 	if (!file)
1889 		goto out;
1890 
1891 	file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir,
1892 				  &bat_priv->nc.max_fwd_delay);
1893 	if (!file)
1894 		goto out;
1895 
1896 	file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir,
1897 				  &bat_priv->nc.max_buffer_time);
1898 	if (!file)
1899 		goto out;
1900 
1901 	return 0;
1902 
1903 out:
1904 	return -ENOMEM;
1905 }
1906