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