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