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