1 /* Copyright (C) 2012-2015 B.A.T.M.A.N. contributors:
3 * Martin Hundebøll, Jeppe Ledet-Pedersen
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.
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.
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/>.
18 #include "network-coding.h"
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>
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>
51 #include "hard-interface.h"
53 #include "originator.h"
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;
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);
66 * batadv_nc_init - one-time initialization for network coding
68 int __init batadv_nc_init(void)
72 /* Register our packet type */
73 ret = batadv_recv_handler_register(BATADV_CODED,
74 batadv_nc_recv_coded_packet);
80 * batadv_nc_start_timer - initialise the nc periodic worker
81 * @bat_priv: the bat priv with all the soft interface information
83 static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
85 queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
86 msecs_to_jiffies(10));
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
94 static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv)
98 nc_mode = atomic_read(&bat_priv->network_coding);
102 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
105 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1,
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
116 void batadv_nc_status_update(struct net_device *net_dev)
118 struct batadv_priv *bat_priv = netdev_priv(net_dev);
120 batadv_nc_tvlv_container_update(bat_priv);
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
131 static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
132 struct batadv_orig_node *orig,
134 void *tvlv_value, u16 tvlv_value_len)
136 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
137 clear_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
139 set_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
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
146 int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
148 bat_priv->nc.timestamp_fwd_flush = jiffies;
149 bat_priv->nc.timestamp_sniffed_purge = jiffies;
151 if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
154 bat_priv->nc.coding_hash = batadv_hash_new(128);
155 if (!bat_priv->nc.coding_hash)
158 batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
159 &batadv_nc_coding_hash_lock_class_key);
161 bat_priv->nc.decoding_hash = batadv_hash_new(128);
162 if (!bat_priv->nc.decoding_hash) {
163 batadv_hash_destroy(bat_priv->nc.coding_hash);
167 batadv_hash_set_lock_class(bat_priv->nc.decoding_hash,
168 &batadv_nc_decoding_hash_lock_class_key);
170 INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
171 batadv_nc_start_timer(bat_priv);
173 batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1,
174 NULL, BATADV_TVLV_NC, 1,
175 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
176 batadv_nc_tvlv_container_update(bat_priv);
184 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
185 * @bat_priv: the bat priv with all the soft interface information
187 void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
189 atomic_set(&bat_priv->network_coding, 0);
190 bat_priv->nc.min_tq = 200;
191 bat_priv->nc.max_fwd_delay = 10;
192 bat_priv->nc.max_buffer_time = 200;
196 * batadv_nc_init_orig - initialise the nc fields of an orig_node
197 * @orig_node: the orig_node which is going to be initialised
199 void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
201 INIT_LIST_HEAD(&orig_node->in_coding_list);
202 INIT_LIST_HEAD(&orig_node->out_coding_list);
203 spin_lock_init(&orig_node->in_coding_list_lock);
204 spin_lock_init(&orig_node->out_coding_list_lock);
208 * batadv_nc_node_release - release nc_node from lists and queue for free after
210 * @nc_node: the nc node to free
212 static void batadv_nc_node_release(struct batadv_nc_node *nc_node)
214 batadv_orig_node_free_ref(nc_node->orig_node);
215 kfree_rcu(nc_node, rcu);
219 * batadv_nc_node_free_ref - decrement the nc node refcounter and possibly
221 * @nc_node: the nc node to free
223 static void batadv_nc_node_free_ref(struct batadv_nc_node *nc_node)
225 if (atomic_dec_and_test(&nc_node->refcount))
226 batadv_nc_node_release(nc_node);
230 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
232 * @nc_path: the nc node to free
234 static void batadv_nc_path_free_ref(struct batadv_nc_path *nc_path)
236 if (atomic_dec_and_test(&nc_path->refcount))
237 kfree_rcu(nc_path, rcu);
241 * batadv_nc_packet_free - frees nc packet
242 * @nc_packet: the nc packet to free
244 static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet)
247 kfree_skb(nc_packet->skb);
249 batadv_nc_path_free_ref(nc_packet->nc_path);
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
258 * Returns true if the entry has to be purged now, false otherwise
260 static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
261 struct batadv_nc_node *nc_node)
263 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
266 return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
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
274 * Returns true if the entry has to be purged now, false otherwise
276 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
277 struct batadv_nc_path *nc_path)
279 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
282 /* purge the path when no packets has been added for 10 times the
285 return batadv_has_timed_out(nc_path->last_valid,
286 bat_priv->nc.max_fwd_delay * 10);
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
294 * Returns true if the entry has to be purged now, false otherwise
296 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
297 struct batadv_nc_path *nc_path)
299 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
302 /* purge the path when no packets has been added for 10 times the
305 return batadv_has_timed_out(nc_path->last_valid,
306 bat_priv->nc.max_buffer_time * 10);
310 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
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
321 batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
322 struct list_head *list,
324 bool (*to_purge)(struct batadv_priv *,
325 struct batadv_nc_node *))
327 struct batadv_nc_node *nc_node, *nc_node_tmp;
329 /* For each nc_node in list */
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
335 if (to_purge && !to_purge(bat_priv, nc_node))
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);
344 spin_unlock_bh(lock);
348 * batadv_nc_purge_orig - purges all nc node data attached of the given
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
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 *))
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,
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,
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
378 static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
380 struct batadv_hashtable *hash = bat_priv->orig_hash;
381 struct hlist_head *head;
382 struct batadv_orig_node *orig_node;
388 /* For each orig_node */
389 for (i = 0; i < hash->size; i++) {
390 head = &hash->table[i];
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);
401 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
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
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 *))
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 */
421 for (i = 0; i < hash->size; i++) {
422 head = &hash->table[i];
423 lock = &hash->list_locks[i];
425 /* For each nc_path in this bin */
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
431 if (to_purge && !to_purge(bat_priv, nc_path))
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
439 if (!unlikely(list_empty(&nc_path->packet_list))) {
440 net_ratelimited_function(printk,
442 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
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);
455 spin_unlock_bh(lock);
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
465 static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
468 memcpy(key->prev_hop, src, sizeof(key->prev_hop));
469 memcpy(key->next_hop, dst, sizeof(key->next_hop));
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
477 * Returns the selected index in the hash table for the given data.
479 static u32 batadv_nc_hash_choose(const void *data, u32 size)
481 const struct batadv_nc_path *nc_path = data;
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);
491 * batadv_nc_hash_compare - comparing function used in the network coding hash
493 * @node: node in the local table
494 * @data2: second object to compare the node to
496 * Returns 1 if the two entry are the same, 0 otherwise
498 static int batadv_nc_hash_compare(const struct hlist_node *node,
501 const struct batadv_nc_path *nc_path1, *nc_path2;
503 nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
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)
511 if (memcmp(nc_path1->next_hop, nc_path2->next_hop,
512 sizeof(nc_path1->next_hop)) != 0)
519 * batadv_nc_hash_find - search for an existing nc path and return it
520 * @hash: hash table containing the nc path
523 * Returns the nc_path if found, NULL otherwise.
525 static struct batadv_nc_path *
526 batadv_nc_hash_find(struct batadv_hashtable *hash,
529 struct hlist_head *head;
530 struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
536 index = batadv_nc_hash_choose(data, hash->size);
537 head = &hash->table[index];
540 hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
541 if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
544 if (!atomic_inc_not_zero(&nc_path->refcount))
547 nc_path_tmp = nc_path;
556 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
557 * @nc_packet: the nc packet to send
559 static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
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);
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
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.
578 * Returns false as soon as the entry in the fifo queue has not been timed out
579 * yet and true otherwise.
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)
585 unsigned long timeout = bat_priv->nc.max_buffer_time;
588 lockdep_assert_held(&nc_path->packet_list_lock);
590 /* Packets are added to tail, so the remaining packets did not time
591 * out and we can stop processing the current queue
593 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
594 !batadv_has_timed_out(nc_packet->timestamp, timeout))
597 /* purge nc packet */
598 list_del(&nc_packet->list);
599 batadv_nc_packet_free(nc_packet);
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
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.
617 * Returns false as soon as the entry in the fifo queue has not been timed out
618 * yet and true otherwise.
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)
624 unsigned long timeout = bat_priv->nc.max_fwd_delay;
626 lockdep_assert_held(&nc_path->packet_list_lock);
628 /* Packets are added to tail, so the remaining packets did not time
629 * out and we can stop processing the current queue
631 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
632 !batadv_has_timed_out(nc_packet->timestamp, timeout))
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);
646 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
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.
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 *))
661 struct hlist_head *head;
662 struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
663 struct batadv_nc_path *nc_path;
670 /* Loop hash table bins */
671 for (i = 0; i < hash->size; i++) {
672 head = &hash->table[i];
674 /* Loop coding paths */
676 hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
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);
685 spin_unlock_bh(&nc_path->packet_list_lock);
692 * batadv_nc_worker - periodic task for house keeping related to network coding
693 * @work: kernel work struct
695 static void batadv_nc_worker(struct work_struct *work)
697 struct delayed_work *delayed_work;
698 struct batadv_priv_nc *priv_nc;
699 struct batadv_priv *bat_priv;
700 unsigned long timeout;
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);
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);
712 timeout = bat_priv->nc.max_fwd_delay;
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;
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;
727 /* Schedule a new check */
728 batadv_nc_start_timer(bat_priv);
732 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
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
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.
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)
748 struct batadv_orig_ifinfo *orig_ifinfo;
752 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT);
756 last_ttl = orig_ifinfo->last_ttl;
757 last_real_seqno = orig_ifinfo->last_real_seqno;
758 batadv_orig_ifinfo_free_ref(orig_ifinfo);
760 if (last_real_seqno != ntohl(ogm_packet->seqno))
762 if (last_ttl != ogm_packet->ttl + 1)
764 if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
766 if (ogm_packet->tq < bat_priv->nc.min_tq)
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
779 * Returns the nc_node if found, NULL otherwise.
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,
786 struct batadv_nc_node *nc_node, *nc_node_out = NULL;
787 struct list_head *list;
790 list = &orig_neigh_node->in_coding_list;
792 list = &orig_neigh_node->out_coding_list;
794 /* Traverse list of nc_nodes to orig_node */
796 list_for_each_entry_rcu(nc_node, list, list) {
797 if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
800 if (!atomic_inc_not_zero(&nc_node->refcount))
804 nc_node_out = nc_node;
813 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
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
821 * Returns the nc_node if found or created, NULL in case of an error.
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,
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;
833 /* Select ingoing or outgoing coding node */
835 lock = &orig_neigh_node->in_coding_list_lock;
836 list = &orig_neigh_node->in_coding_list;
838 lock = &orig_neigh_node->out_coding_list_lock;
839 list = &orig_neigh_node->out_coding_list;
844 /* Check if nc_node is already added */
845 nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
851 nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
855 atomic_inc(&orig_neigh_node->refcount);
857 /* Initialize nc_node */
858 INIT_LIST_HEAD(&nc_node->list);
859 ether_addr_copy(nc_node->addr, orig_node->orig);
860 nc_node->orig_node = orig_neigh_node;
861 atomic_set(&nc_node->refcount, 2);
863 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
864 nc_node->addr, nc_node->orig_node->orig);
866 /* Add nc_node to orig_node */
867 list_add_tail_rcu(&nc_node->list, list);
869 spin_unlock_bh(lock);
875 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node
876 * structs (best called on incoming OGMs)
877 * @bat_priv: the bat priv with all the soft interface information
878 * @orig_node: orig node originating the ogm packet
879 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
880 * (can be equal to orig_node)
881 * @ogm_packet: incoming ogm packet
882 * @is_single_hop_neigh: orig_node is a single hop neighbor
884 void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
885 struct batadv_orig_node *orig_node,
886 struct batadv_orig_node *orig_neigh_node,
887 struct batadv_ogm_packet *ogm_packet,
888 int is_single_hop_neigh)
890 struct batadv_nc_node *in_nc_node = NULL;
891 struct batadv_nc_node *out_nc_node = NULL;
893 /* Check if network coding is enabled */
894 if (!atomic_read(&bat_priv->network_coding))
897 /* check if orig node is network coding enabled */
898 if (!test_bit(BATADV_ORIG_CAPA_HAS_NC, &orig_node->capabilities))
901 /* accept ogms from 'good' neighbors and single hop neighbors */
902 if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
903 !is_single_hop_neigh)
906 /* Add orig_node as in_nc_node on hop */
907 in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
908 orig_neigh_node, true);
912 in_nc_node->last_seen = jiffies;
914 /* Add hop as out_nc_node on orig_node */
915 out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
920 out_nc_node->last_seen = jiffies;
924 batadv_nc_node_free_ref(in_nc_node);
926 batadv_nc_node_free_ref(out_nc_node);
930 * batadv_nc_get_path - get existing nc_path or allocate a new one
931 * @bat_priv: the bat priv with all the soft interface information
932 * @hash: hash table containing the nc path
933 * @src: ethernet source address - first half of the nc path search key
934 * @dst: ethernet destination address - second half of the nc path search key
936 * Returns pointer to nc_path if the path was found or created, returns NULL
939 static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
940 struct batadv_hashtable *hash,
945 struct batadv_nc_path *nc_path, nc_path_key;
947 batadv_nc_hash_key_gen(&nc_path_key, src, dst);
949 /* Search for existing nc_path */
950 nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
953 /* Set timestamp to delay removal of nc_path */
954 nc_path->last_valid = jiffies;
958 /* No existing nc_path was found; create a new */
959 nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
964 /* Initialize nc_path */
965 INIT_LIST_HEAD(&nc_path->packet_list);
966 spin_lock_init(&nc_path->packet_list_lock);
967 atomic_set(&nc_path->refcount, 2);
968 nc_path->last_valid = jiffies;
969 ether_addr_copy(nc_path->next_hop, dst);
970 ether_addr_copy(nc_path->prev_hop, src);
972 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
976 /* Add nc_path to hash table */
977 hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
978 batadv_nc_hash_choose, &nc_path_key,
979 &nc_path->hash_entry);
981 if (hash_added < 0) {
990 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
991 * selection of a receiver with slightly lower TQ than the other
992 * @tq: to be weighted tq value
994 static u8 batadv_nc_random_weight_tq(u8 tq)
996 /* randomize the estimated packet loss (max TQ - estimated TQ) */
997 u8 rand_tq = prandom_u32_max(BATADV_TQ_MAX_VALUE + 1 - tq);
999 /* convert to (randomized) estimated tq again */
1000 return BATADV_TQ_MAX_VALUE - rand_tq;
1004 * batadv_nc_memxor - XOR destination with source
1005 * @dst: byte array to XOR into
1006 * @src: byte array to XOR from
1007 * @len: length of destination array
1009 static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
1013 for (i = 0; i < len; ++i)
1018 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
1019 * into a coded_packet and send it
1020 * @bat_priv: the bat priv with all the soft interface information
1021 * @skb: data skb to forward
1022 * @ethhdr: pointer to the ethernet header inside the skb
1023 * @nc_packet: structure containing the packet to the skb can be coded with
1024 * @neigh_node: next hop to forward packet to
1026 * Returns true if both packets are consumed, false otherwise.
1028 static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
1029 struct sk_buff *skb,
1030 struct ethhdr *ethhdr,
1031 struct batadv_nc_packet *nc_packet,
1032 struct batadv_neigh_node *neigh_node)
1034 u8 tq_weighted_neigh, tq_weighted_coding, tq_tmp;
1035 struct sk_buff *skb_dest, *skb_src;
1036 struct batadv_unicast_packet *packet1;
1037 struct batadv_unicast_packet *packet2;
1038 struct batadv_coded_packet *coded_packet;
1039 struct batadv_neigh_node *neigh_tmp, *router_neigh;
1040 struct batadv_neigh_node *router_coding = NULL;
1041 struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL;
1042 struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL;
1043 u8 *first_source, *first_dest, *second_source, *second_dest;
1044 __be32 packet_id1, packet_id2;
1048 int unicast_size = sizeof(*packet1);
1049 int coded_size = sizeof(*coded_packet);
1050 int header_add = coded_size - unicast_size;
1052 /* TODO: do we need to consider the outgoing interface for
1055 router_neigh = batadv_orig_router_get(neigh_node->orig_node,
1060 router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh,
1062 if (!router_neigh_ifinfo)
1065 neigh_tmp = nc_packet->neigh_node;
1066 router_coding = batadv_orig_router_get(neigh_tmp->orig_node,
1071 router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding,
1073 if (!router_coding_ifinfo)
1076 tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg;
1077 tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp);
1078 tq_tmp = router_coding_ifinfo->bat_iv.tq_avg;
1079 tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp);
1081 /* Select one destination for the MAC-header dst-field based on
1082 * weighted TQ-values.
1084 if (tq_weighted_neigh >= tq_weighted_coding) {
1085 /* Destination from nc_packet is selected for MAC-header */
1086 first_dest = nc_packet->nc_path->next_hop;
1087 first_source = nc_packet->nc_path->prev_hop;
1088 second_dest = neigh_node->addr;
1089 second_source = ethhdr->h_source;
1090 packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
1091 packet2 = (struct batadv_unicast_packet *)skb->data;
1092 packet_id1 = nc_packet->packet_id;
1093 packet_id2 = batadv_skb_crc32(skb,
1094 skb->data + sizeof(*packet2));
1096 /* Destination for skb is selected for MAC-header */
1097 first_dest = neigh_node->addr;
1098 first_source = ethhdr->h_source;
1099 second_dest = nc_packet->nc_path->next_hop;
1100 second_source = nc_packet->nc_path->prev_hop;
1101 packet1 = (struct batadv_unicast_packet *)skb->data;
1102 packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
1103 packet_id1 = batadv_skb_crc32(skb,
1104 skb->data + sizeof(*packet1));
1105 packet_id2 = nc_packet->packet_id;
1108 /* Instead of zero padding the smallest data buffer, we
1109 * code into the largest.
1111 if (skb->len <= nc_packet->skb->len) {
1112 skb_dest = nc_packet->skb;
1116 skb_src = nc_packet->skb;
1119 /* coding_len is used when decoding the packet shorter packet */
1120 coding_len = skb_src->len - unicast_size;
1122 if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
1125 skb_push(skb_dest, header_add);
1127 coded_packet = (struct batadv_coded_packet *)skb_dest->data;
1128 skb_reset_mac_header(skb_dest);
1130 coded_packet->packet_type = BATADV_CODED;
1131 coded_packet->version = BATADV_COMPAT_VERSION;
1132 coded_packet->ttl = packet1->ttl;
1134 /* Info about first unicast packet */
1135 ether_addr_copy(coded_packet->first_source, first_source);
1136 ether_addr_copy(coded_packet->first_orig_dest, packet1->dest);
1137 coded_packet->first_crc = packet_id1;
1138 coded_packet->first_ttvn = packet1->ttvn;
1140 /* Info about second unicast packet */
1141 ether_addr_copy(coded_packet->second_dest, second_dest);
1142 ether_addr_copy(coded_packet->second_source, second_source);
1143 ether_addr_copy(coded_packet->second_orig_dest, packet2->dest);
1144 coded_packet->second_crc = packet_id2;
1145 coded_packet->second_ttl = packet2->ttl;
1146 coded_packet->second_ttvn = packet2->ttvn;
1147 coded_packet->coded_len = htons(coding_len);
1149 /* This is where the magic happens: Code skb_src into skb_dest */
1150 batadv_nc_memxor(skb_dest->data + coded_size,
1151 skb_src->data + unicast_size, coding_len);
1153 /* Update counters accordingly */
1154 if (BATADV_SKB_CB(skb_src)->decoded &&
1155 BATADV_SKB_CB(skb_dest)->decoded) {
1156 /* Both packets are recoded */
1157 count = skb_src->len + ETH_HLEN;
1158 count += skb_dest->len + ETH_HLEN;
1159 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
1160 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
1161 } else if (!BATADV_SKB_CB(skb_src)->decoded &&
1162 !BATADV_SKB_CB(skb_dest)->decoded) {
1163 /* Both packets are newly coded */
1164 count = skb_src->len + ETH_HLEN;
1165 count += skb_dest->len + ETH_HLEN;
1166 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
1167 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
1168 } else if (BATADV_SKB_CB(skb_src)->decoded &&
1169 !BATADV_SKB_CB(skb_dest)->decoded) {
1170 /* skb_src recoded and skb_dest is newly coded */
1171 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1172 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1173 skb_src->len + ETH_HLEN);
1174 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1175 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1176 skb_dest->len + ETH_HLEN);
1177 } else if (!BATADV_SKB_CB(skb_src)->decoded &&
1178 BATADV_SKB_CB(skb_dest)->decoded) {
1179 /* skb_src is newly coded and skb_dest is recoded */
1180 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1181 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1182 skb_src->len + ETH_HLEN);
1183 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1184 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1185 skb_dest->len + ETH_HLEN);
1188 /* skb_src is now coded into skb_dest, so free it */
1191 /* avoid duplicate free of skb from nc_packet */
1192 nc_packet->skb = NULL;
1193 batadv_nc_packet_free(nc_packet);
1195 /* Send the coded packet and return true */
1196 batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest);
1200 batadv_neigh_node_free_ref(router_neigh);
1202 batadv_neigh_node_free_ref(router_coding);
1203 if (router_neigh_ifinfo)
1204 batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo);
1205 if (router_coding_ifinfo)
1206 batadv_neigh_ifinfo_free_ref(router_coding_ifinfo);
1211 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1212 * @skb: data skb to forward
1213 * @dst: destination mac address of the other skb to code with
1214 * @src: source mac address of skb
1216 * Whenever we network code a packet we have to check whether we received it in
1217 * a network coded form. If so, we may not be able to use it for coding because
1218 * some neighbors may also have received (overheard) the packet in the network
1219 * coded form without being able to decode it. It is hard to know which of the
1220 * neighboring nodes was able to decode the packet, therefore we can only
1221 * re-code the packet if the source of the previous encoded packet is involved.
1222 * Since the source encoded the packet we can be certain it has all necessary
1223 * decode information.
1225 * Returns true if coding of a decoded packet is allowed.
1227 static bool batadv_nc_skb_coding_possible(struct sk_buff *skb, u8 *dst, u8 *src)
1229 if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
1235 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1236 * out_nc_node to retrieve a buffered packet that can be used for coding.
1237 * @bat_priv: the bat priv with all the soft interface information
1238 * @in_nc_node: pointer to skb next hop's neighbor nc node
1239 * @out_nc_node: pointer to skb source's neighbor nc node
1240 * @skb: data skb to forward
1241 * @eth_dst: next hop mac address of skb
1243 * Returns true if coding of a decoded skb is allowed.
1245 static struct batadv_nc_packet *
1246 batadv_nc_path_search(struct batadv_priv *bat_priv,
1247 struct batadv_nc_node *in_nc_node,
1248 struct batadv_nc_node *out_nc_node,
1249 struct sk_buff *skb,
1252 struct batadv_nc_path *nc_path, nc_path_key;
1253 struct batadv_nc_packet *nc_packet_out = NULL;
1254 struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
1255 struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
1261 /* Create almost path key */
1262 batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
1264 idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
1266 /* Check for coding opportunities in this nc_path */
1268 hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
1269 if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
1272 if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
1275 spin_lock_bh(&nc_path->packet_list_lock);
1276 if (list_empty(&nc_path->packet_list)) {
1277 spin_unlock_bh(&nc_path->packet_list_lock);
1281 list_for_each_entry_safe(nc_packet, nc_packet_tmp,
1282 &nc_path->packet_list, list) {
1283 if (!batadv_nc_skb_coding_possible(nc_packet->skb,
1288 /* Coding opportunity is found! */
1289 list_del(&nc_packet->list);
1290 nc_packet_out = nc_packet;
1294 spin_unlock_bh(&nc_path->packet_list_lock);
1299 return nc_packet_out;
1303 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1304 * skb's sender (may be equal to the originator).
1305 * @bat_priv: the bat priv with all the soft interface information
1306 * @skb: data skb to forward
1307 * @eth_dst: next hop mac address of skb
1308 * @eth_src: source mac address of skb
1309 * @in_nc_node: pointer to skb next hop's neighbor nc node
1311 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1313 static struct batadv_nc_packet *
1314 batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
1315 struct sk_buff *skb,
1318 struct batadv_nc_node *in_nc_node)
1320 struct batadv_orig_node *orig_node;
1321 struct batadv_nc_node *out_nc_node;
1322 struct batadv_nc_packet *nc_packet = NULL;
1324 orig_node = batadv_orig_hash_find(bat_priv, eth_src);
1329 list_for_each_entry_rcu(out_nc_node,
1330 &orig_node->out_coding_list, list) {
1331 /* Check if the skb is decoded and if recoding is possible */
1332 if (!batadv_nc_skb_coding_possible(skb,
1333 out_nc_node->addr, eth_src))
1336 /* Search for an opportunity in this nc_path */
1337 nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
1338 out_nc_node, skb, eth_dst);
1344 batadv_orig_node_free_ref(orig_node);
1349 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1350 * unicast skb before it is stored for use in later decoding
1351 * @bat_priv: the bat priv with all the soft interface information
1352 * @skb: data skb to store
1353 * @eth_dst_new: new destination mac address of skb
1355 static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
1356 struct sk_buff *skb,
1359 struct ethhdr *ethhdr;
1361 /* Copy skb header to change the mac header */
1362 skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
1366 /* Set the mac header as if we actually sent the packet uncoded */
1367 ethhdr = eth_hdr(skb);
1368 ether_addr_copy(ethhdr->h_source, ethhdr->h_dest);
1369 ether_addr_copy(ethhdr->h_dest, eth_dst_new);
1371 /* Set data pointer to MAC header to mimic packets from our tx path */
1372 skb_push(skb, ETH_HLEN);
1374 /* Add the packet to the decoding packet pool */
1375 batadv_nc_skb_store_for_decoding(bat_priv, skb);
1377 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1384 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1385 * @skb: data skb to forward
1386 * @neigh_node: next hop to forward packet to
1387 * @ethhdr: pointer to the ethernet header inside the skb
1389 * Loops through list of neighboring nodes the next hop has a good connection to
1390 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1391 * next hop that potentially sent a packet which our next hop also received
1392 * (overheard) and has stored for later decoding.
1394 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1396 static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
1397 struct batadv_neigh_node *neigh_node,
1398 struct ethhdr *ethhdr)
1400 struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1401 struct batadv_priv *bat_priv = netdev_priv(netdev);
1402 struct batadv_orig_node *orig_node = neigh_node->orig_node;
1403 struct batadv_nc_node *nc_node;
1404 struct batadv_nc_packet *nc_packet = NULL;
1407 list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
1408 /* Search for coding opportunity with this in_nc_node */
1409 nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
1411 ethhdr->h_source, nc_node);
1413 /* Opportunity was found, so stop searching */
1422 /* Save packets for later decoding */
1423 batadv_nc_skb_store_before_coding(bat_priv, skb,
1425 batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
1426 nc_packet->neigh_node->addr);
1428 /* Code and send packets */
1429 if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
1433 /* out of mem ? Coding failed - we have to free the buffered packet
1434 * to avoid memleaks. The skb passed as argument will be dealt with
1435 * by the calling function.
1437 batadv_nc_send_packet(nc_packet);
1442 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1443 * @skb: skb to add to path
1444 * @nc_path: path to add skb to
1445 * @neigh_node: next hop to forward packet to
1446 * @packet_id: checksum to identify packet
1448 * Returns true if the packet was buffered or false in case of an error.
1450 static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
1451 struct batadv_nc_path *nc_path,
1452 struct batadv_neigh_node *neigh_node,
1455 struct batadv_nc_packet *nc_packet;
1457 nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
1461 /* Initialize nc_packet */
1462 nc_packet->timestamp = jiffies;
1463 nc_packet->packet_id = packet_id;
1464 nc_packet->skb = skb;
1465 nc_packet->neigh_node = neigh_node;
1466 nc_packet->nc_path = nc_path;
1468 /* Add coding packet to list */
1469 spin_lock_bh(&nc_path->packet_list_lock);
1470 list_add_tail(&nc_packet->list, &nc_path->packet_list);
1471 spin_unlock_bh(&nc_path->packet_list_lock);
1477 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1479 * @skb: data skb to forward
1480 * @neigh_node: next hop to forward packet to
1482 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1484 bool batadv_nc_skb_forward(struct sk_buff *skb,
1485 struct batadv_neigh_node *neigh_node)
1487 const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1488 struct batadv_priv *bat_priv = netdev_priv(netdev);
1489 struct batadv_unicast_packet *packet;
1490 struct batadv_nc_path *nc_path;
1491 struct ethhdr *ethhdr = eth_hdr(skb);
1495 /* Check if network coding is enabled */
1496 if (!atomic_read(&bat_priv->network_coding))
1499 /* We only handle unicast packets */
1500 payload = skb_network_header(skb);
1501 packet = (struct batadv_unicast_packet *)payload;
1502 if (packet->packet_type != BATADV_UNICAST)
1505 /* Try to find a coding opportunity and send the skb if one is found */
1506 if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
1509 /* Find or create a nc_path for this src-dst pair */
1510 nc_path = batadv_nc_get_path(bat_priv,
1511 bat_priv->nc.coding_hash,
1518 /* Add skb to nc_path */
1519 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1520 if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
1523 /* Packet is consumed */
1527 batadv_nc_path_free_ref(nc_path);
1529 /* Packet is not consumed */
1534 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1535 * when decoding coded packets
1536 * @bat_priv: the bat priv with all the soft interface information
1537 * @skb: data skb to store
1539 void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
1540 struct sk_buff *skb)
1542 struct batadv_unicast_packet *packet;
1543 struct batadv_nc_path *nc_path;
1544 struct ethhdr *ethhdr = eth_hdr(skb);
1548 /* Check if network coding is enabled */
1549 if (!atomic_read(&bat_priv->network_coding))
1552 /* Check for supported packet type */
1553 payload = skb_network_header(skb);
1554 packet = (struct batadv_unicast_packet *)payload;
1555 if (packet->packet_type != BATADV_UNICAST)
1558 /* Find existing nc_path or create a new */
1559 nc_path = batadv_nc_get_path(bat_priv,
1560 bat_priv->nc.decoding_hash,
1567 /* Clone skb and adjust skb->data to point at batman header */
1568 skb = skb_clone(skb, GFP_ATOMIC);
1572 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
1575 if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
1578 /* Add skb to nc_path */
1579 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1580 if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
1583 batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
1589 batadv_nc_path_free_ref(nc_path);
1595 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1596 * should be saved in the decoding buffer and, if so, store it there
1597 * @bat_priv: the bat priv with all the soft interface information
1598 * @skb: unicast skb to store
1600 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
1601 struct sk_buff *skb)
1603 struct ethhdr *ethhdr = eth_hdr(skb);
1605 if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
1608 /* Set data pointer to MAC header to mimic packets from our tx path */
1609 skb_push(skb, ETH_HLEN);
1611 batadv_nc_skb_store_for_decoding(bat_priv, skb);
1615 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1617 * @bat_priv: the bat priv with all the soft interface information
1618 * @skb: unicast skb to decode
1619 * @nc_packet: decode data needed to decode the skb
1621 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1622 * in case of an error.
1624 static struct batadv_unicast_packet *
1625 batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
1626 struct batadv_nc_packet *nc_packet)
1628 const int h_size = sizeof(struct batadv_unicast_packet);
1629 const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
1630 struct batadv_unicast_packet *unicast_packet;
1631 struct batadv_coded_packet coded_packet_tmp;
1632 struct ethhdr *ethhdr, ethhdr_tmp;
1633 u8 *orig_dest, ttl, ttvn;
1634 unsigned int coding_len;
1637 /* Save headers temporarily */
1638 memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
1639 memcpy(ðhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
1641 if (skb_cow(skb, 0) < 0)
1644 if (unlikely(!skb_pull_rcsum(skb, h_diff)))
1647 /* Data points to batman header, so set mac header 14 bytes before
1648 * and network to data
1650 skb_set_mac_header(skb, -ETH_HLEN);
1651 skb_reset_network_header(skb);
1653 /* Reconstruct original mac header */
1654 ethhdr = eth_hdr(skb);
1655 *ethhdr = ethhdr_tmp;
1657 /* Select the correct unicast header information based on the location
1658 * of our mac address in the coded_packet header
1660 if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
1661 /* If we are the second destination the packet was overheard,
1662 * so the Ethernet address must be copied to h_dest and
1663 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1665 ether_addr_copy(ethhdr->h_dest, coded_packet_tmp.second_dest);
1666 skb->pkt_type = PACKET_HOST;
1668 orig_dest = coded_packet_tmp.second_orig_dest;
1669 ttl = coded_packet_tmp.second_ttl;
1670 ttvn = coded_packet_tmp.second_ttvn;
1672 orig_dest = coded_packet_tmp.first_orig_dest;
1673 ttl = coded_packet_tmp.ttl;
1674 ttvn = coded_packet_tmp.first_ttvn;
1677 coding_len = ntohs(coded_packet_tmp.coded_len);
1679 if (coding_len > skb->len)
1682 /* Here the magic is reversed:
1683 * extract the missing packet from the received coded packet
1685 batadv_nc_memxor(skb->data + h_size,
1686 nc_packet->skb->data + h_size,
1689 /* Resize decoded skb if decoded with larger packet */
1690 if (nc_packet->skb->len > coding_len + h_size) {
1691 err = pskb_trim_rcsum(skb, coding_len + h_size);
1696 /* Create decoded unicast packet */
1697 unicast_packet = (struct batadv_unicast_packet *)skb->data;
1698 unicast_packet->packet_type = BATADV_UNICAST;
1699 unicast_packet->version = BATADV_COMPAT_VERSION;
1700 unicast_packet->ttl = ttl;
1701 ether_addr_copy(unicast_packet->dest, orig_dest);
1702 unicast_packet->ttvn = ttvn;
1704 batadv_nc_packet_free(nc_packet);
1705 return unicast_packet;
1709 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1710 * find the data needed to decode the coded packet
1711 * @bat_priv: the bat priv with all the soft interface information
1712 * @ethhdr: pointer to the ethernet header inside the coded packet
1713 * @coded: coded packet we try to find decode data for
1715 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1717 static struct batadv_nc_packet *
1718 batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
1719 struct ethhdr *ethhdr,
1720 struct batadv_coded_packet *coded)
1722 struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
1723 struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
1724 struct batadv_nc_path *nc_path, nc_path_key;
1732 /* Select the correct packet id based on the location of our mac-addr */
1733 dest = ethhdr->h_source;
1734 if (!batadv_is_my_mac(bat_priv, coded->second_dest)) {
1735 source = coded->second_source;
1736 packet_id = coded->second_crc;
1738 source = coded->first_source;
1739 packet_id = coded->first_crc;
1742 batadv_nc_hash_key_gen(&nc_path_key, source, dest);
1743 index = batadv_nc_hash_choose(&nc_path_key, hash->size);
1745 /* Search for matching coding path */
1747 hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) {
1748 /* Find matching nc_packet */
1749 spin_lock_bh(&nc_path->packet_list_lock);
1750 list_for_each_entry(tmp_nc_packet,
1751 &nc_path->packet_list, list) {
1752 if (packet_id == tmp_nc_packet->packet_id) {
1753 list_del(&tmp_nc_packet->list);
1755 nc_packet = tmp_nc_packet;
1759 spin_unlock_bh(&nc_path->packet_list_lock);
1767 batadv_dbg(BATADV_DBG_NC, bat_priv,
1768 "No decoding packet found for %u\n", packet_id);
1774 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1775 * resulting unicast packet
1776 * @skb: incoming coded packet
1777 * @recv_if: pointer to interface this packet was received on
1779 static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
1780 struct batadv_hard_iface *recv_if)
1782 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1783 struct batadv_unicast_packet *unicast_packet;
1784 struct batadv_coded_packet *coded_packet;
1785 struct batadv_nc_packet *nc_packet;
1786 struct ethhdr *ethhdr;
1787 int hdr_size = sizeof(*coded_packet);
1789 /* Check if network coding is enabled */
1790 if (!atomic_read(&bat_priv->network_coding))
1793 /* Make sure we can access (and remove) header */
1794 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1797 coded_packet = (struct batadv_coded_packet *)skb->data;
1798 ethhdr = eth_hdr(skb);
1800 /* Verify frame is destined for us */
1801 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) &&
1802 !batadv_is_my_mac(bat_priv, coded_packet->second_dest))
1805 /* Update stat counter */
1806 if (batadv_is_my_mac(bat_priv, coded_packet->second_dest))
1807 batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
1809 nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
1812 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
1816 /* Make skb's linear, because decoding accesses the entire buffer */
1817 if (skb_linearize(skb) < 0)
1818 goto free_nc_packet;
1820 if (skb_linearize(nc_packet->skb) < 0)
1821 goto free_nc_packet;
1823 /* Decode the packet */
1824 unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet);
1825 if (!unicast_packet) {
1826 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
1827 goto free_nc_packet;
1830 /* Mark packet as decoded to do correct recoding when forwarding */
1831 BATADV_SKB_CB(skb)->decoded = true;
1832 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
1833 batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
1834 skb->len + ETH_HLEN);
1835 return batadv_recv_unicast_packet(skb, recv_if);
1838 batadv_nc_packet_free(nc_packet);
1843 * batadv_nc_mesh_free - clean up network coding memory
1844 * @bat_priv: the bat priv with all the soft interface information
1846 void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
1848 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
1849 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
1850 cancel_delayed_work_sync(&bat_priv->nc.work);
1852 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
1853 batadv_hash_destroy(bat_priv->nc.coding_hash);
1854 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
1855 batadv_hash_destroy(bat_priv->nc.decoding_hash);
1859 * batadv_nc_nodes_seq_print_text - print the nc node information
1860 * @seq: seq file to print on
1863 int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
1865 struct net_device *net_dev = (struct net_device *)seq->private;
1866 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1867 struct batadv_hashtable *hash = bat_priv->orig_hash;
1868 struct batadv_hard_iface *primary_if;
1869 struct hlist_head *head;
1870 struct batadv_orig_node *orig_node;
1871 struct batadv_nc_node *nc_node;
1874 primary_if = batadv_seq_print_text_primary_if_get(seq);
1878 /* Traverse list of originators */
1879 for (i = 0; i < hash->size; i++) {
1880 head = &hash->table[i];
1882 /* For each orig_node in this bin */
1884 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1885 /* no need to print the orig node if it does not have
1886 * network coding neighbors
1888 if (list_empty(&orig_node->in_coding_list) &&
1889 list_empty(&orig_node->out_coding_list))
1892 seq_printf(seq, "Node: %pM\n", orig_node->orig);
1894 seq_puts(seq, " Ingoing: ");
1895 /* For each in_nc_node to this orig_node */
1896 list_for_each_entry_rcu(nc_node,
1897 &orig_node->in_coding_list,
1899 seq_printf(seq, "%pM ",
1901 seq_puts(seq, "\n");
1903 seq_puts(seq, " Outgoing: ");
1904 /* For out_nc_node to this orig_node */
1905 list_for_each_entry_rcu(nc_node,
1906 &orig_node->out_coding_list,
1908 seq_printf(seq, "%pM ",
1910 seq_puts(seq, "\n\n");
1917 batadv_hardif_free_ref(primary_if);
1922 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1923 * @bat_priv: the bat priv with all the soft interface information
1925 int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
1927 struct dentry *nc_dir, *file;
1929 nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
1933 file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
1934 &bat_priv->nc.min_tq);
1938 file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir,
1939 &bat_priv->nc.max_fwd_delay);
1943 file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir,
1944 &bat_priv->nc.max_buffer_time);