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