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