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
)
165 batadv_hash_set_lock_class(bat_priv
->nc
.decoding_hash
,
166 &batadv_nc_decoding_hash_lock_class_key
);
168 INIT_DELAYED_WORK(&bat_priv
->nc
.work
, batadv_nc_worker
);
169 batadv_nc_start_timer(bat_priv
);
171 batadv_tvlv_handler_register(bat_priv
, batadv_nc_tvlv_ogm_handler_v1
,
172 NULL
, BATADV_TVLV_NC
, 1,
173 BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
174 batadv_nc_tvlv_container_update(bat_priv
);
182 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
183 * @bat_priv: the bat priv with all the soft interface information
185 void batadv_nc_init_bat_priv(struct batadv_priv
*bat_priv
)
187 atomic_set(&bat_priv
->network_coding
, 0);
188 bat_priv
->nc
.min_tq
= 200;
189 bat_priv
->nc
.max_fwd_delay
= 10;
190 bat_priv
->nc
.max_buffer_time
= 200;
194 * batadv_nc_init_orig - initialise the nc fields of an orig_node
195 * @orig_node: the orig_node which is going to be initialised
197 void batadv_nc_init_orig(struct batadv_orig_node
*orig_node
)
199 INIT_LIST_HEAD(&orig_node
->in_coding_list
);
200 INIT_LIST_HEAD(&orig_node
->out_coding_list
);
201 spin_lock_init(&orig_node
->in_coding_list_lock
);
202 spin_lock_init(&orig_node
->out_coding_list_lock
);
206 * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
207 * its refcount on the orig_node
208 * @rcu: rcu pointer of the nc node
210 static void batadv_nc_node_free_rcu(struct rcu_head
*rcu
)
212 struct batadv_nc_node
*nc_node
;
214 nc_node
= container_of(rcu
, struct batadv_nc_node
, rcu
);
215 batadv_orig_node_free_ref(nc_node
->orig_node
);
220 * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
222 * @nc_node: the nc node to free
224 static void batadv_nc_node_free_ref(struct batadv_nc_node
*nc_node
)
226 if (atomic_dec_and_test(&nc_node
->refcount
))
227 call_rcu(&nc_node
->rcu
, batadv_nc_node_free_rcu
);
231 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
233 * @nc_path: the nc node to free
235 static void batadv_nc_path_free_ref(struct batadv_nc_path
*nc_path
)
237 if (atomic_dec_and_test(&nc_path
->refcount
))
238 kfree_rcu(nc_path
, rcu
);
242 * batadv_nc_packet_free - frees nc packet
243 * @nc_packet: the nc packet to free
245 static void batadv_nc_packet_free(struct batadv_nc_packet
*nc_packet
)
248 kfree_skb(nc_packet
->skb
);
250 batadv_nc_path_free_ref(nc_packet
->nc_path
);
255 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
256 * @bat_priv: the bat priv with all the soft interface information
257 * @nc_node: the nc node to check
259 * Returns true if the entry has to be purged now, false otherwise
261 static bool batadv_nc_to_purge_nc_node(struct batadv_priv
*bat_priv
,
262 struct batadv_nc_node
*nc_node
)
264 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
267 return batadv_has_timed_out(nc_node
->last_seen
, BATADV_NC_NODE_TIMEOUT
);
271 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
272 * @bat_priv: the bat priv with all the soft interface information
273 * @nc_path: the nc path to check
275 * Returns true if the entry has to be purged now, false otherwise
277 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv
*bat_priv
,
278 struct batadv_nc_path
*nc_path
)
280 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
283 /* purge the path when no packets has been added for 10 times the
286 return batadv_has_timed_out(nc_path
->last_valid
,
287 bat_priv
->nc
.max_fwd_delay
* 10);
291 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
292 * @bat_priv: the bat priv with all the soft interface information
293 * @nc_path: the nc path to check
295 * Returns true if the entry has to be purged now, false otherwise
297 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv
*bat_priv
,
298 struct batadv_nc_path
*nc_path
)
300 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
303 /* purge the path when no packets has been added for 10 times the
306 return batadv_has_timed_out(nc_path
->last_valid
,
307 bat_priv
->nc
.max_buffer_time
* 10);
311 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
313 * @bat_priv: the bat priv with all the soft interface information
314 * @list: list of nc nodes
315 * @lock: nc node list lock
316 * @to_purge: function in charge to decide whether an entry has to be purged or
317 * not. This function takes the nc node as argument and has to return
318 * a boolean value: true if the entry has to be deleted, false
322 batadv_nc_purge_orig_nc_nodes(struct batadv_priv
*bat_priv
,
323 struct list_head
*list
,
325 bool (*to_purge
)(struct batadv_priv
*,
326 struct batadv_nc_node
*))
328 struct batadv_nc_node
*nc_node
, *nc_node_tmp
;
330 /* For each nc_node in list */
332 list_for_each_entry_safe(nc_node
, nc_node_tmp
, list
, list
) {
333 /* if an helper function has been passed as parameter,
334 * ask it if the entry has to be purged or not
336 if (to_purge
&& !to_purge(bat_priv
, nc_node
))
339 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
340 "Removing nc_node %pM -> %pM\n",
341 nc_node
->addr
, nc_node
->orig_node
->orig
);
342 list_del_rcu(&nc_node
->list
);
343 batadv_nc_node_free_ref(nc_node
);
345 spin_unlock_bh(lock
);
349 * batadv_nc_purge_orig - purges all nc node data attached of the given
351 * @bat_priv: the bat priv with all the soft interface information
352 * @orig_node: orig_node with the nc node entries to be purged
353 * @to_purge: function in charge to decide whether an entry has to be purged or
354 * not. This function takes the nc node as argument and has to return
355 * a boolean value: true is the entry has to be deleted, false
358 void batadv_nc_purge_orig(struct batadv_priv
*bat_priv
,
359 struct batadv_orig_node
*orig_node
,
360 bool (*to_purge
)(struct batadv_priv
*,
361 struct batadv_nc_node
*))
363 /* Check ingoing nc_node's of this orig_node */
364 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->in_coding_list
,
365 &orig_node
->in_coding_list_lock
,
368 /* Check outgoing nc_node's of this orig_node */
369 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->out_coding_list
,
370 &orig_node
->out_coding_list_lock
,
375 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
376 * have timed out nc nodes
377 * @bat_priv: the bat priv with all the soft interface information
379 static void batadv_nc_purge_orig_hash(struct batadv_priv
*bat_priv
)
381 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
382 struct hlist_head
*head
;
383 struct batadv_orig_node
*orig_node
;
389 /* For each orig_node */
390 for (i
= 0; i
< hash
->size
; i
++) {
391 head
= &hash
->table
[i
];
394 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
)
395 batadv_nc_purge_orig(bat_priv
, orig_node
,
396 batadv_nc_to_purge_nc_node
);
402 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
404 * @bat_priv: the bat priv with all the soft interface information
405 * @hash: hash table containing the nc paths to check
406 * @to_purge: function in charge to decide whether an entry has to be purged or
407 * not. This function takes the nc node as argument and has to return
408 * a boolean value: true is the entry has to be deleted, false
411 static void batadv_nc_purge_paths(struct batadv_priv
*bat_priv
,
412 struct batadv_hashtable
*hash
,
413 bool (*to_purge
)(struct batadv_priv
*,
414 struct batadv_nc_path
*))
416 struct hlist_head
*head
;
417 struct hlist_node
*node_tmp
;
418 struct batadv_nc_path
*nc_path
;
419 spinlock_t
*lock
; /* Protects lists in hash */
422 for (i
= 0; i
< hash
->size
; i
++) {
423 head
= &hash
->table
[i
];
424 lock
= &hash
->list_locks
[i
];
426 /* For each nc_path in this bin */
428 hlist_for_each_entry_safe(nc_path
, node_tmp
, head
, hash_entry
) {
429 /* if an helper function has been passed as parameter,
430 * ask it if the entry has to be purged or not
432 if (to_purge
&& !to_purge(bat_priv
, nc_path
))
435 /* purging an non-empty nc_path should never happen, but
436 * is observed under high CPU load. Delay the purging
437 * until next iteration to allow the packet_list to be
440 if (!unlikely(list_empty(&nc_path
->packet_list
))) {
441 net_ratelimited_function(printk
,
443 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
449 /* nc_path is unused, so remove it */
450 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
451 "Remove nc_path %pM -> %pM\n",
452 nc_path
->prev_hop
, nc_path
->next_hop
);
453 hlist_del_rcu(&nc_path
->hash_entry
);
454 batadv_nc_path_free_ref(nc_path
);
456 spin_unlock_bh(lock
);
461 * batadv_nc_hash_key_gen - computes the nc_path hash key
462 * @key: buffer to hold the final hash key
463 * @src: source ethernet mac address going into the hash key
464 * @dst: destination ethernet mac address going into the hash key
466 static void batadv_nc_hash_key_gen(struct batadv_nc_path
*key
, const char *src
,
469 memcpy(key
->prev_hop
, src
, sizeof(key
->prev_hop
));
470 memcpy(key
->next_hop
, dst
, sizeof(key
->next_hop
));
474 * batadv_nc_hash_choose - compute the hash value for an nc path
475 * @data: data to hash
476 * @size: size of the hash table
478 * Returns the selected index in the hash table for the given data.
480 static u32
batadv_nc_hash_choose(const void *data
, u32 size
)
482 const struct batadv_nc_path
*nc_path
= data
;
485 hash
= jhash(&nc_path
->prev_hop
, sizeof(nc_path
->prev_hop
), hash
);
486 hash
= jhash(&nc_path
->next_hop
, sizeof(nc_path
->next_hop
), hash
);
492 * batadv_nc_hash_compare - comparing function used in the network coding hash
494 * @node: node in the local table
495 * @data2: second object to compare the node to
497 * Returns 1 if the two entry are the same, 0 otherwise
499 static int batadv_nc_hash_compare(const struct hlist_node
*node
,
502 const struct batadv_nc_path
*nc_path1
, *nc_path2
;
504 nc_path1
= container_of(node
, struct batadv_nc_path
, hash_entry
);
507 /* Return 1 if the two keys are identical */
508 if (memcmp(nc_path1
->prev_hop
, nc_path2
->prev_hop
,
509 sizeof(nc_path1
->prev_hop
)) != 0)
512 if (memcmp(nc_path1
->next_hop
, nc_path2
->next_hop
,
513 sizeof(nc_path1
->next_hop
)) != 0)
520 * batadv_nc_hash_find - search for an existing nc path and return it
521 * @hash: hash table containing the nc path
524 * Returns the nc_path if found, NULL otherwise.
526 static struct batadv_nc_path
*
527 batadv_nc_hash_find(struct batadv_hashtable
*hash
,
530 struct hlist_head
*head
;
531 struct batadv_nc_path
*nc_path
, *nc_path_tmp
= NULL
;
537 index
= batadv_nc_hash_choose(data
, hash
->size
);
538 head
= &hash
->table
[index
];
541 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
542 if (!batadv_nc_hash_compare(&nc_path
->hash_entry
, data
))
545 if (!atomic_inc_not_zero(&nc_path
->refcount
))
548 nc_path_tmp
= nc_path
;
557 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
558 * @nc_packet: the nc packet to send
560 static void batadv_nc_send_packet(struct batadv_nc_packet
*nc_packet
)
562 batadv_send_skb_packet(nc_packet
->skb
,
563 nc_packet
->neigh_node
->if_incoming
,
564 nc_packet
->nc_path
->next_hop
);
565 nc_packet
->skb
= NULL
;
566 batadv_nc_packet_free(nc_packet
);
570 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
571 * @bat_priv: the bat priv with all the soft interface information
572 * @nc_path: the nc path the packet belongs to
573 * @nc_packet: the nc packet to be checked
575 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
576 * timeout. If so, the packet is no longer kept and the entry deleted from the
577 * queue. Has to be called with the appropriate locks.
579 * Returns false as soon as the entry in the fifo queue has not been timed out
580 * yet and true otherwise.
582 static bool batadv_nc_sniffed_purge(struct batadv_priv
*bat_priv
,
583 struct batadv_nc_path
*nc_path
,
584 struct batadv_nc_packet
*nc_packet
)
586 unsigned long timeout
= bat_priv
->nc
.max_buffer_time
;
589 lockdep_assert_held(&nc_path
->packet_list_lock
);
591 /* Packets are added to tail, so the remaining packets did not time
592 * out and we can stop processing the current queue
594 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
595 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
598 /* purge nc packet */
599 list_del(&nc_packet
->list
);
600 batadv_nc_packet_free(nc_packet
);
609 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
610 * @bat_priv: the bat priv with all the soft interface information
611 * @nc_path: the nc path the packet belongs to
612 * @nc_packet: the nc packet to be checked
614 * Checks whether the given nc packet has hit its forward timeout. If so, the
615 * packet is no longer delayed, immediately sent and the entry deleted from the
616 * queue. Has to be called with the appropriate locks.
618 * Returns false as soon as the entry in the fifo queue has not been timed out
619 * yet and true otherwise.
621 static bool batadv_nc_fwd_flush(struct batadv_priv
*bat_priv
,
622 struct batadv_nc_path
*nc_path
,
623 struct batadv_nc_packet
*nc_packet
)
625 unsigned long timeout
= bat_priv
->nc
.max_fwd_delay
;
627 lockdep_assert_held(&nc_path
->packet_list_lock
);
629 /* Packets are added to tail, so the remaining packets did not time
630 * out and we can stop processing the current queue
632 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
633 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
637 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
638 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
639 nc_packet
->skb
->len
+ ETH_HLEN
);
640 list_del(&nc_packet
->list
);
641 batadv_nc_send_packet(nc_packet
);
647 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
649 * @bat_priv: the bat priv with all the soft interface information
650 * @hash: to be processed hash table
651 * @process_fn: Function called to process given nc packet. Should return true
652 * to encourage this function to proceed with the next packet.
653 * Otherwise the rest of the current queue is skipped.
656 batadv_nc_process_nc_paths(struct batadv_priv
*bat_priv
,
657 struct batadv_hashtable
*hash
,
658 bool (*process_fn
)(struct batadv_priv
*,
659 struct batadv_nc_path
*,
660 struct batadv_nc_packet
*))
662 struct hlist_head
*head
;
663 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
664 struct batadv_nc_path
*nc_path
;
671 /* Loop hash table bins */
672 for (i
= 0; i
< hash
->size
; i
++) {
673 head
= &hash
->table
[i
];
675 /* Loop coding paths */
677 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
679 spin_lock_bh(&nc_path
->packet_list_lock
);
680 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
681 &nc_path
->packet_list
, list
) {
682 ret
= process_fn(bat_priv
, nc_path
, nc_packet
);
686 spin_unlock_bh(&nc_path
->packet_list_lock
);
693 * batadv_nc_worker - periodic task for house keeping related to network coding
694 * @work: kernel work struct
696 static void batadv_nc_worker(struct work_struct
*work
)
698 struct delayed_work
*delayed_work
;
699 struct batadv_priv_nc
*priv_nc
;
700 struct batadv_priv
*bat_priv
;
701 unsigned long timeout
;
703 delayed_work
= container_of(work
, struct delayed_work
, work
);
704 priv_nc
= container_of(delayed_work
, struct batadv_priv_nc
, work
);
705 bat_priv
= container_of(priv_nc
, struct batadv_priv
, nc
);
707 batadv_nc_purge_orig_hash(bat_priv
);
708 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
709 batadv_nc_to_purge_nc_path_coding
);
710 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
711 batadv_nc_to_purge_nc_path_decoding
);
713 timeout
= bat_priv
->nc
.max_fwd_delay
;
715 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_fwd_flush
, timeout
)) {
716 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
717 batadv_nc_fwd_flush
);
718 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
721 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_sniffed_purge
,
722 bat_priv
->nc
.max_buffer_time
)) {
723 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
724 batadv_nc_sniffed_purge
);
725 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
728 /* Schedule a new check */
729 batadv_nc_start_timer(bat_priv
);
733 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
735 * @bat_priv: the bat priv with all the soft interface information
736 * @orig_node: neighboring orig node which may be used as nc candidate
737 * @ogm_packet: incoming ogm packet also used for the checks
740 * 1) The OGM must have the most recent sequence number.
741 * 2) The TTL must be decremented by one and only one.
742 * 3) The OGM must be received from the first hop from orig_node.
743 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
745 static bool batadv_can_nc_with_orig(struct batadv_priv
*bat_priv
,
746 struct batadv_orig_node
*orig_node
,
747 struct batadv_ogm_packet
*ogm_packet
)
749 struct batadv_orig_ifinfo
*orig_ifinfo
;
753 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, BATADV_IF_DEFAULT
);
757 last_ttl
= orig_ifinfo
->last_ttl
;
758 last_real_seqno
= orig_ifinfo
->last_real_seqno
;
759 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
761 if (last_real_seqno
!= ntohl(ogm_packet
->seqno
))
763 if (last_ttl
!= ogm_packet
->ttl
+ 1)
765 if (!batadv_compare_eth(ogm_packet
->orig
, ogm_packet
->prev_sender
))
767 if (ogm_packet
->tq
< bat_priv
->nc
.min_tq
)
774 * batadv_nc_find_nc_node - search for an existing nc node and return it
775 * @orig_node: orig node originating the ogm packet
776 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
777 * (can be equal to orig_node)
778 * @in_coding: traverse incoming or outgoing network coding list
780 * Returns the nc_node if found, NULL otherwise.
782 static struct batadv_nc_node
783 *batadv_nc_find_nc_node(struct batadv_orig_node
*orig_node
,
784 struct batadv_orig_node
*orig_neigh_node
,
787 struct batadv_nc_node
*nc_node
, *nc_node_out
= NULL
;
788 struct list_head
*list
;
791 list
= &orig_neigh_node
->in_coding_list
;
793 list
= &orig_neigh_node
->out_coding_list
;
795 /* Traverse list of nc_nodes to orig_node */
797 list_for_each_entry_rcu(nc_node
, list
, list
) {
798 if (!batadv_compare_eth(nc_node
->addr
, orig_node
->orig
))
801 if (!atomic_inc_not_zero(&nc_node
->refcount
))
805 nc_node_out
= nc_node
;
814 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
816 * @bat_priv: the bat priv with all the soft interface information
817 * @orig_node: orig node originating the ogm packet
818 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
819 * (can be equal to orig_node)
820 * @in_coding: traverse incoming or outgoing network coding list
822 * Returns the nc_node if found or created, NULL in case of an error.
824 static struct batadv_nc_node
825 *batadv_nc_get_nc_node(struct batadv_priv
*bat_priv
,
826 struct batadv_orig_node
*orig_node
,
827 struct batadv_orig_node
*orig_neigh_node
,
830 struct batadv_nc_node
*nc_node
;
831 spinlock_t
*lock
; /* Used to lock list selected by "int in_coding" */
832 struct list_head
*list
;
834 /* Check if nc_node is already added */
835 nc_node
= batadv_nc_find_nc_node(orig_node
, orig_neigh_node
, in_coding
);
841 nc_node
= kzalloc(sizeof(*nc_node
), GFP_ATOMIC
);
845 if (!atomic_inc_not_zero(&orig_neigh_node
->refcount
))
848 /* Initialize nc_node */
849 INIT_LIST_HEAD(&nc_node
->list
);
850 ether_addr_copy(nc_node
->addr
, orig_node
->orig
);
851 nc_node
->orig_node
= orig_neigh_node
;
852 atomic_set(&nc_node
->refcount
, 2);
854 /* Select ingoing or outgoing coding node */
856 lock
= &orig_neigh_node
->in_coding_list_lock
;
857 list
= &orig_neigh_node
->in_coding_list
;
859 lock
= &orig_neigh_node
->out_coding_list_lock
;
860 list
= &orig_neigh_node
->out_coding_list
;
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 */
868 list_add_tail_rcu(&nc_node
->list
, list
);
869 spin_unlock_bh(lock
);
879 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node
880 * structs (best called on incoming OGMs)
881 * @bat_priv: the bat priv with all the soft interface information
882 * @orig_node: orig node originating the ogm packet
883 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
884 * (can be equal to orig_node)
885 * @ogm_packet: incoming ogm packet
886 * @is_single_hop_neigh: orig_node is a single hop neighbor
888 void batadv_nc_update_nc_node(struct batadv_priv
*bat_priv
,
889 struct batadv_orig_node
*orig_node
,
890 struct batadv_orig_node
*orig_neigh_node
,
891 struct batadv_ogm_packet
*ogm_packet
,
892 int is_single_hop_neigh
)
894 struct batadv_nc_node
*in_nc_node
= NULL
;
895 struct batadv_nc_node
*out_nc_node
= NULL
;
897 /* Check if network coding is enabled */
898 if (!atomic_read(&bat_priv
->network_coding
))
901 /* check if orig node is network coding enabled */
902 if (!test_bit(BATADV_ORIG_CAPA_HAS_NC
, &orig_node
->capabilities
))
905 /* accept ogms from 'good' neighbors and single hop neighbors */
906 if (!batadv_can_nc_with_orig(bat_priv
, orig_node
, ogm_packet
) &&
907 !is_single_hop_neigh
)
910 /* Add orig_node as in_nc_node on hop */
911 in_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_node
,
912 orig_neigh_node
, true);
916 in_nc_node
->last_seen
= jiffies
;
918 /* Add hop as out_nc_node on orig_node */
919 out_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_neigh_node
,
924 out_nc_node
->last_seen
= jiffies
;
928 batadv_nc_node_free_ref(in_nc_node
);
930 batadv_nc_node_free_ref(out_nc_node
);
934 * batadv_nc_get_path - get existing nc_path or allocate a new one
935 * @bat_priv: the bat priv with all the soft interface information
936 * @hash: hash table containing the nc path
937 * @src: ethernet source address - first half of the nc path search key
938 * @dst: ethernet destination address - second half of the nc path search key
940 * Returns pointer to nc_path if the path was found or created, returns NULL
943 static struct batadv_nc_path
*batadv_nc_get_path(struct batadv_priv
*bat_priv
,
944 struct batadv_hashtable
*hash
,
949 struct batadv_nc_path
*nc_path
, nc_path_key
;
951 batadv_nc_hash_key_gen(&nc_path_key
, src
, dst
);
953 /* Search for existing nc_path */
954 nc_path
= batadv_nc_hash_find(hash
, (void *)&nc_path_key
);
957 /* Set timestamp to delay removal of nc_path */
958 nc_path
->last_valid
= jiffies
;
962 /* No existing nc_path was found; create a new */
963 nc_path
= kzalloc(sizeof(*nc_path
), GFP_ATOMIC
);
968 /* Initialize nc_path */
969 INIT_LIST_HEAD(&nc_path
->packet_list
);
970 spin_lock_init(&nc_path
->packet_list_lock
);
971 atomic_set(&nc_path
->refcount
, 2);
972 nc_path
->last_valid
= jiffies
;
973 ether_addr_copy(nc_path
->next_hop
, dst
);
974 ether_addr_copy(nc_path
->prev_hop
, src
);
976 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_path %pM -> %pM\n",
980 /* Add nc_path to hash table */
981 hash_added
= batadv_hash_add(hash
, batadv_nc_hash_compare
,
982 batadv_nc_hash_choose
, &nc_path_key
,
983 &nc_path
->hash_entry
);
985 if (hash_added
< 0) {
994 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
995 * selection of a receiver with slightly lower TQ than the other
996 * @tq: to be weighted tq value
998 static u8
batadv_nc_random_weight_tq(u8 tq
)
1000 u8 rand_val
, rand_tq
;
1002 get_random_bytes(&rand_val
, sizeof(rand_val
));
1004 /* randomize the estimated packet loss (max TQ - estimated TQ) */
1005 rand_tq
= rand_val
* (BATADV_TQ_MAX_VALUE
- tq
);
1007 /* normalize the randomized packet loss */
1008 rand_tq
/= BATADV_TQ_MAX_VALUE
;
1010 /* convert to (randomized) estimated tq again */
1011 return BATADV_TQ_MAX_VALUE
- rand_tq
;
1015 * batadv_nc_memxor - XOR destination with source
1016 * @dst: byte array to XOR into
1017 * @src: byte array to XOR from
1018 * @len: length of destination array
1020 static void batadv_nc_memxor(char *dst
, const char *src
, unsigned int len
)
1024 for (i
= 0; i
< len
; ++i
)
1029 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
1030 * into a coded_packet and send it
1031 * @bat_priv: the bat priv with all the soft interface information
1032 * @skb: data skb to forward
1033 * @ethhdr: pointer to the ethernet header inside the skb
1034 * @nc_packet: structure containing the packet to the skb can be coded with
1035 * @neigh_node: next hop to forward packet to
1037 * Returns true if both packets are consumed, false otherwise.
1039 static bool batadv_nc_code_packets(struct batadv_priv
*bat_priv
,
1040 struct sk_buff
*skb
,
1041 struct ethhdr
*ethhdr
,
1042 struct batadv_nc_packet
*nc_packet
,
1043 struct batadv_neigh_node
*neigh_node
)
1045 u8 tq_weighted_neigh
, tq_weighted_coding
, tq_tmp
;
1046 struct sk_buff
*skb_dest
, *skb_src
;
1047 struct batadv_unicast_packet
*packet1
;
1048 struct batadv_unicast_packet
*packet2
;
1049 struct batadv_coded_packet
*coded_packet
;
1050 struct batadv_neigh_node
*neigh_tmp
, *router_neigh
;
1051 struct batadv_neigh_node
*router_coding
= NULL
;
1052 struct batadv_neigh_ifinfo
*router_neigh_ifinfo
= NULL
;
1053 struct batadv_neigh_ifinfo
*router_coding_ifinfo
= NULL
;
1054 u8
*first_source
, *first_dest
, *second_source
, *second_dest
;
1055 __be32 packet_id1
, packet_id2
;
1059 int unicast_size
= sizeof(*packet1
);
1060 int coded_size
= sizeof(*coded_packet
);
1061 int header_add
= coded_size
- unicast_size
;
1063 /* TODO: do we need to consider the outgoing interface for
1066 router_neigh
= batadv_orig_router_get(neigh_node
->orig_node
,
1071 router_neigh_ifinfo
= batadv_neigh_ifinfo_get(router_neigh
,
1073 if (!router_neigh_ifinfo
)
1076 neigh_tmp
= nc_packet
->neigh_node
;
1077 router_coding
= batadv_orig_router_get(neigh_tmp
->orig_node
,
1082 router_coding_ifinfo
= batadv_neigh_ifinfo_get(router_coding
,
1084 if (!router_coding_ifinfo
)
1087 tq_tmp
= router_neigh_ifinfo
->bat_iv
.tq_avg
;
1088 tq_weighted_neigh
= batadv_nc_random_weight_tq(tq_tmp
);
1089 tq_tmp
= router_coding_ifinfo
->bat_iv
.tq_avg
;
1090 tq_weighted_coding
= batadv_nc_random_weight_tq(tq_tmp
);
1092 /* Select one destination for the MAC-header dst-field based on
1093 * weighted TQ-values.
1095 if (tq_weighted_neigh
>= tq_weighted_coding
) {
1096 /* Destination from nc_packet is selected for MAC-header */
1097 first_dest
= nc_packet
->nc_path
->next_hop
;
1098 first_source
= nc_packet
->nc_path
->prev_hop
;
1099 second_dest
= neigh_node
->addr
;
1100 second_source
= ethhdr
->h_source
;
1101 packet1
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1102 packet2
= (struct batadv_unicast_packet
*)skb
->data
;
1103 packet_id1
= nc_packet
->packet_id
;
1104 packet_id2
= batadv_skb_crc32(skb
,
1105 skb
->data
+ sizeof(*packet2
));
1107 /* Destination for skb is selected for MAC-header */
1108 first_dest
= neigh_node
->addr
;
1109 first_source
= ethhdr
->h_source
;
1110 second_dest
= nc_packet
->nc_path
->next_hop
;
1111 second_source
= nc_packet
->nc_path
->prev_hop
;
1112 packet1
= (struct batadv_unicast_packet
*)skb
->data
;
1113 packet2
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1114 packet_id1
= batadv_skb_crc32(skb
,
1115 skb
->data
+ sizeof(*packet1
));
1116 packet_id2
= nc_packet
->packet_id
;
1119 /* Instead of zero padding the smallest data buffer, we
1120 * code into the largest.
1122 if (skb
->len
<= nc_packet
->skb
->len
) {
1123 skb_dest
= nc_packet
->skb
;
1127 skb_src
= nc_packet
->skb
;
1130 /* coding_len is used when decoding the packet shorter packet */
1131 coding_len
= skb_src
->len
- unicast_size
;
1133 if (skb_linearize(skb_dest
) < 0 || skb_linearize(skb_src
) < 0)
1136 skb_push(skb_dest
, header_add
);
1138 coded_packet
= (struct batadv_coded_packet
*)skb_dest
->data
;
1139 skb_reset_mac_header(skb_dest
);
1141 coded_packet
->packet_type
= BATADV_CODED
;
1142 coded_packet
->version
= BATADV_COMPAT_VERSION
;
1143 coded_packet
->ttl
= packet1
->ttl
;
1145 /* Info about first unicast packet */
1146 ether_addr_copy(coded_packet
->first_source
, first_source
);
1147 ether_addr_copy(coded_packet
->first_orig_dest
, packet1
->dest
);
1148 coded_packet
->first_crc
= packet_id1
;
1149 coded_packet
->first_ttvn
= packet1
->ttvn
;
1151 /* Info about second unicast packet */
1152 ether_addr_copy(coded_packet
->second_dest
, second_dest
);
1153 ether_addr_copy(coded_packet
->second_source
, second_source
);
1154 ether_addr_copy(coded_packet
->second_orig_dest
, packet2
->dest
);
1155 coded_packet
->second_crc
= packet_id2
;
1156 coded_packet
->second_ttl
= packet2
->ttl
;
1157 coded_packet
->second_ttvn
= packet2
->ttvn
;
1158 coded_packet
->coded_len
= htons(coding_len
);
1160 /* This is where the magic happens: Code skb_src into skb_dest */
1161 batadv_nc_memxor(skb_dest
->data
+ coded_size
,
1162 skb_src
->data
+ unicast_size
, coding_len
);
1164 /* Update counters accordingly */
1165 if (BATADV_SKB_CB(skb_src
)->decoded
&&
1166 BATADV_SKB_CB(skb_dest
)->decoded
) {
1167 /* Both packets are recoded */
1168 count
= skb_src
->len
+ ETH_HLEN
;
1169 count
+= skb_dest
->len
+ ETH_HLEN
;
1170 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE
, 2);
1171 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
, count
);
1172 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1173 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1174 /* Both packets are newly coded */
1175 count
= skb_src
->len
+ ETH_HLEN
;
1176 count
+= skb_dest
->len
+ ETH_HLEN
;
1177 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE
, 2);
1178 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
, count
);
1179 } else if (BATADV_SKB_CB(skb_src
)->decoded
&&
1180 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1181 /* skb_src recoded and skb_dest is newly coded */
1182 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1183 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1184 skb_src
->len
+ ETH_HLEN
);
1185 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1186 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1187 skb_dest
->len
+ ETH_HLEN
);
1188 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1189 BATADV_SKB_CB(skb_dest
)->decoded
) {
1190 /* skb_src is newly coded and skb_dest is recoded */
1191 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1192 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1193 skb_src
->len
+ ETH_HLEN
);
1194 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1195 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1196 skb_dest
->len
+ ETH_HLEN
);
1199 /* skb_src is now coded into skb_dest, so free it */
1202 /* avoid duplicate free of skb from nc_packet */
1203 nc_packet
->skb
= NULL
;
1204 batadv_nc_packet_free(nc_packet
);
1206 /* Send the coded packet and return true */
1207 batadv_send_skb_packet(skb_dest
, neigh_node
->if_incoming
, first_dest
);
1211 batadv_neigh_node_free_ref(router_neigh
);
1213 batadv_neigh_node_free_ref(router_coding
);
1214 if (router_neigh_ifinfo
)
1215 batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo
);
1216 if (router_coding_ifinfo
)
1217 batadv_neigh_ifinfo_free_ref(router_coding_ifinfo
);
1222 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1223 * @skb: data skb to forward
1224 * @dst: destination mac address of the other skb to code with
1225 * @src: source mac address of skb
1227 * Whenever we network code a packet we have to check whether we received it in
1228 * a network coded form. If so, we may not be able to use it for coding because
1229 * some neighbors may also have received (overheard) the packet in the network
1230 * coded form without being able to decode it. It is hard to know which of the
1231 * neighboring nodes was able to decode the packet, therefore we can only
1232 * re-code the packet if the source of the previous encoded packet is involved.
1233 * Since the source encoded the packet we can be certain it has all necessary
1234 * decode information.
1236 * Returns true if coding of a decoded packet is allowed.
1238 static bool batadv_nc_skb_coding_possible(struct sk_buff
*skb
, u8
*dst
, u8
*src
)
1240 if (BATADV_SKB_CB(skb
)->decoded
&& !batadv_compare_eth(dst
, src
))
1246 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1247 * out_nc_node to retrieve a buffered packet that can be used for coding.
1248 * @bat_priv: the bat priv with all the soft interface information
1249 * @in_nc_node: pointer to skb next hop's neighbor nc node
1250 * @out_nc_node: pointer to skb source's neighbor nc node
1251 * @skb: data skb to forward
1252 * @eth_dst: next hop mac address of skb
1254 * Returns true if coding of a decoded skb is allowed.
1256 static struct batadv_nc_packet
*
1257 batadv_nc_path_search(struct batadv_priv
*bat_priv
,
1258 struct batadv_nc_node
*in_nc_node
,
1259 struct batadv_nc_node
*out_nc_node
,
1260 struct sk_buff
*skb
,
1263 struct batadv_nc_path
*nc_path
, nc_path_key
;
1264 struct batadv_nc_packet
*nc_packet_out
= NULL
;
1265 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
1266 struct batadv_hashtable
*hash
= bat_priv
->nc
.coding_hash
;
1272 /* Create almost path key */
1273 batadv_nc_hash_key_gen(&nc_path_key
, in_nc_node
->addr
,
1275 idx
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1277 /* Check for coding opportunities in this nc_path */
1279 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[idx
], hash_entry
) {
1280 if (!batadv_compare_eth(nc_path
->prev_hop
, in_nc_node
->addr
))
1283 if (!batadv_compare_eth(nc_path
->next_hop
, out_nc_node
->addr
))
1286 spin_lock_bh(&nc_path
->packet_list_lock
);
1287 if (list_empty(&nc_path
->packet_list
)) {
1288 spin_unlock_bh(&nc_path
->packet_list_lock
);
1292 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
1293 &nc_path
->packet_list
, list
) {
1294 if (!batadv_nc_skb_coding_possible(nc_packet
->skb
,
1299 /* Coding opportunity is found! */
1300 list_del(&nc_packet
->list
);
1301 nc_packet_out
= nc_packet
;
1305 spin_unlock_bh(&nc_path
->packet_list_lock
);
1310 return nc_packet_out
;
1314 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1315 * skb's sender (may be equal to the originator).
1316 * @bat_priv: the bat priv with all the soft interface information
1317 * @skb: data skb to forward
1318 * @eth_dst: next hop mac address of skb
1319 * @eth_src: source mac address of skb
1320 * @in_nc_node: pointer to skb next hop's neighbor nc node
1322 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1324 static struct batadv_nc_packet
*
1325 batadv_nc_skb_src_search(struct batadv_priv
*bat_priv
,
1326 struct sk_buff
*skb
,
1329 struct batadv_nc_node
*in_nc_node
)
1331 struct batadv_orig_node
*orig_node
;
1332 struct batadv_nc_node
*out_nc_node
;
1333 struct batadv_nc_packet
*nc_packet
= NULL
;
1335 orig_node
= batadv_orig_hash_find(bat_priv
, eth_src
);
1340 list_for_each_entry_rcu(out_nc_node
,
1341 &orig_node
->out_coding_list
, list
) {
1342 /* Check if the skb is decoded and if recoding is possible */
1343 if (!batadv_nc_skb_coding_possible(skb
,
1344 out_nc_node
->addr
, eth_src
))
1347 /* Search for an opportunity in this nc_path */
1348 nc_packet
= batadv_nc_path_search(bat_priv
, in_nc_node
,
1349 out_nc_node
, skb
, eth_dst
);
1355 batadv_orig_node_free_ref(orig_node
);
1360 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1361 * unicast skb before it is stored for use in later decoding
1362 * @bat_priv: the bat priv with all the soft interface information
1363 * @skb: data skb to store
1364 * @eth_dst_new: new destination mac address of skb
1366 static void batadv_nc_skb_store_before_coding(struct batadv_priv
*bat_priv
,
1367 struct sk_buff
*skb
,
1370 struct ethhdr
*ethhdr
;
1372 /* Copy skb header to change the mac header */
1373 skb
= pskb_copy_for_clone(skb
, GFP_ATOMIC
);
1377 /* Set the mac header as if we actually sent the packet uncoded */
1378 ethhdr
= eth_hdr(skb
);
1379 ether_addr_copy(ethhdr
->h_source
, ethhdr
->h_dest
);
1380 ether_addr_copy(ethhdr
->h_dest
, eth_dst_new
);
1382 /* Set data pointer to MAC header to mimic packets from our tx path */
1383 skb_push(skb
, ETH_HLEN
);
1385 /* Add the packet to the decoding packet pool */
1386 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1388 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1395 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1396 * @skb: data skb to forward
1397 * @neigh_node: next hop to forward packet to
1398 * @ethhdr: pointer to the ethernet header inside the skb
1400 * Loops through list of neighboring nodes the next hop has a good connection to
1401 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1402 * next hop that potentially sent a packet which our next hop also received
1403 * (overheard) and has stored for later decoding.
1405 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1407 static bool batadv_nc_skb_dst_search(struct sk_buff
*skb
,
1408 struct batadv_neigh_node
*neigh_node
,
1409 struct ethhdr
*ethhdr
)
1411 struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1412 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1413 struct batadv_orig_node
*orig_node
= neigh_node
->orig_node
;
1414 struct batadv_nc_node
*nc_node
;
1415 struct batadv_nc_packet
*nc_packet
= NULL
;
1418 list_for_each_entry_rcu(nc_node
, &orig_node
->in_coding_list
, list
) {
1419 /* Search for coding opportunity with this in_nc_node */
1420 nc_packet
= batadv_nc_skb_src_search(bat_priv
, skb
,
1422 ethhdr
->h_source
, nc_node
);
1424 /* Opportunity was found, so stop searching */
1433 /* Save packets for later decoding */
1434 batadv_nc_skb_store_before_coding(bat_priv
, skb
,
1436 batadv_nc_skb_store_before_coding(bat_priv
, nc_packet
->skb
,
1437 nc_packet
->neigh_node
->addr
);
1439 /* Code and send packets */
1440 if (batadv_nc_code_packets(bat_priv
, skb
, ethhdr
, nc_packet
,
1444 /* out of mem ? Coding failed - we have to free the buffered packet
1445 * to avoid memleaks. The skb passed as argument will be dealt with
1446 * by the calling function.
1448 batadv_nc_send_packet(nc_packet
);
1453 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1454 * @skb: skb to add to path
1455 * @nc_path: path to add skb to
1456 * @neigh_node: next hop to forward packet to
1457 * @packet_id: checksum to identify packet
1459 * Returns true if the packet was buffered or false in case of an error.
1461 static bool batadv_nc_skb_add_to_path(struct sk_buff
*skb
,
1462 struct batadv_nc_path
*nc_path
,
1463 struct batadv_neigh_node
*neigh_node
,
1466 struct batadv_nc_packet
*nc_packet
;
1468 nc_packet
= kzalloc(sizeof(*nc_packet
), GFP_ATOMIC
);
1472 /* Initialize nc_packet */
1473 nc_packet
->timestamp
= jiffies
;
1474 nc_packet
->packet_id
= packet_id
;
1475 nc_packet
->skb
= skb
;
1476 nc_packet
->neigh_node
= neigh_node
;
1477 nc_packet
->nc_path
= nc_path
;
1479 /* Add coding packet to list */
1480 spin_lock_bh(&nc_path
->packet_list_lock
);
1481 list_add_tail(&nc_packet
->list
, &nc_path
->packet_list
);
1482 spin_unlock_bh(&nc_path
->packet_list_lock
);
1488 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1490 * @skb: data skb to forward
1491 * @neigh_node: next hop to forward packet to
1493 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1495 bool batadv_nc_skb_forward(struct sk_buff
*skb
,
1496 struct batadv_neigh_node
*neigh_node
)
1498 const struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1499 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1500 struct batadv_unicast_packet
*packet
;
1501 struct batadv_nc_path
*nc_path
;
1502 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1506 /* Check if network coding is enabled */
1507 if (!atomic_read(&bat_priv
->network_coding
))
1510 /* We only handle unicast packets */
1511 payload
= skb_network_header(skb
);
1512 packet
= (struct batadv_unicast_packet
*)payload
;
1513 if (packet
->packet_type
!= BATADV_UNICAST
)
1516 /* Try to find a coding opportunity and send the skb if one is found */
1517 if (batadv_nc_skb_dst_search(skb
, neigh_node
, ethhdr
))
1520 /* Find or create a nc_path for this src-dst pair */
1521 nc_path
= batadv_nc_get_path(bat_priv
,
1522 bat_priv
->nc
.coding_hash
,
1529 /* Add skb to nc_path */
1530 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1531 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, neigh_node
, packet_id
))
1534 /* Packet is consumed */
1538 batadv_nc_path_free_ref(nc_path
);
1540 /* Packet is not consumed */
1545 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1546 * when decoding coded packets
1547 * @bat_priv: the bat priv with all the soft interface information
1548 * @skb: data skb to store
1550 void batadv_nc_skb_store_for_decoding(struct batadv_priv
*bat_priv
,
1551 struct sk_buff
*skb
)
1553 struct batadv_unicast_packet
*packet
;
1554 struct batadv_nc_path
*nc_path
;
1555 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1559 /* Check if network coding is enabled */
1560 if (!atomic_read(&bat_priv
->network_coding
))
1563 /* Check for supported packet type */
1564 payload
= skb_network_header(skb
);
1565 packet
= (struct batadv_unicast_packet
*)payload
;
1566 if (packet
->packet_type
!= BATADV_UNICAST
)
1569 /* Find existing nc_path or create a new */
1570 nc_path
= batadv_nc_get_path(bat_priv
,
1571 bat_priv
->nc
.decoding_hash
,
1578 /* Clone skb and adjust skb->data to point at batman header */
1579 skb
= skb_clone(skb
, GFP_ATOMIC
);
1583 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
1586 if (unlikely(!skb_pull_rcsum(skb
, ETH_HLEN
)))
1589 /* Add skb to nc_path */
1590 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1591 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, NULL
, packet_id
))
1594 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_BUFFER
);
1600 batadv_nc_path_free_ref(nc_path
);
1606 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1607 * should be saved in the decoding buffer and, if so, store it there
1608 * @bat_priv: the bat priv with all the soft interface information
1609 * @skb: unicast skb to store
1611 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv
*bat_priv
,
1612 struct sk_buff
*skb
)
1614 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1616 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1619 /* Set data pointer to MAC header to mimic packets from our tx path */
1620 skb_push(skb
, ETH_HLEN
);
1622 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1626 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1628 * @bat_priv: the bat priv with all the soft interface information
1629 * @skb: unicast skb to decode
1630 * @nc_packet: decode data needed to decode the skb
1632 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1633 * in case of an error.
1635 static struct batadv_unicast_packet
*
1636 batadv_nc_skb_decode_packet(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1637 struct batadv_nc_packet
*nc_packet
)
1639 const int h_size
= sizeof(struct batadv_unicast_packet
);
1640 const int h_diff
= sizeof(struct batadv_coded_packet
) - h_size
;
1641 struct batadv_unicast_packet
*unicast_packet
;
1642 struct batadv_coded_packet coded_packet_tmp
;
1643 struct ethhdr
*ethhdr
, ethhdr_tmp
;
1644 u8
*orig_dest
, ttl
, ttvn
;
1645 unsigned int coding_len
;
1648 /* Save headers temporarily */
1649 memcpy(&coded_packet_tmp
, skb
->data
, sizeof(coded_packet_tmp
));
1650 memcpy(ðhdr_tmp
, skb_mac_header(skb
), sizeof(ethhdr_tmp
));
1652 if (skb_cow(skb
, 0) < 0)
1655 if (unlikely(!skb_pull_rcsum(skb
, h_diff
)))
1658 /* Data points to batman header, so set mac header 14 bytes before
1659 * and network to data
1661 skb_set_mac_header(skb
, -ETH_HLEN
);
1662 skb_reset_network_header(skb
);
1664 /* Reconstruct original mac header */
1665 ethhdr
= eth_hdr(skb
);
1666 *ethhdr
= ethhdr_tmp
;
1668 /* Select the correct unicast header information based on the location
1669 * of our mac address in the coded_packet header
1671 if (batadv_is_my_mac(bat_priv
, coded_packet_tmp
.second_dest
)) {
1672 /* If we are the second destination the packet was overheard,
1673 * so the Ethernet address must be copied to h_dest and
1674 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1676 ether_addr_copy(ethhdr
->h_dest
, coded_packet_tmp
.second_dest
);
1677 skb
->pkt_type
= PACKET_HOST
;
1679 orig_dest
= coded_packet_tmp
.second_orig_dest
;
1680 ttl
= coded_packet_tmp
.second_ttl
;
1681 ttvn
= coded_packet_tmp
.second_ttvn
;
1683 orig_dest
= coded_packet_tmp
.first_orig_dest
;
1684 ttl
= coded_packet_tmp
.ttl
;
1685 ttvn
= coded_packet_tmp
.first_ttvn
;
1688 coding_len
= ntohs(coded_packet_tmp
.coded_len
);
1690 if (coding_len
> skb
->len
)
1693 /* Here the magic is reversed:
1694 * extract the missing packet from the received coded packet
1696 batadv_nc_memxor(skb
->data
+ h_size
,
1697 nc_packet
->skb
->data
+ h_size
,
1700 /* Resize decoded skb if decoded with larger packet */
1701 if (nc_packet
->skb
->len
> coding_len
+ h_size
) {
1702 err
= pskb_trim_rcsum(skb
, coding_len
+ h_size
);
1707 /* Create decoded unicast packet */
1708 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1709 unicast_packet
->packet_type
= BATADV_UNICAST
;
1710 unicast_packet
->version
= BATADV_COMPAT_VERSION
;
1711 unicast_packet
->ttl
= ttl
;
1712 ether_addr_copy(unicast_packet
->dest
, orig_dest
);
1713 unicast_packet
->ttvn
= ttvn
;
1715 batadv_nc_packet_free(nc_packet
);
1716 return unicast_packet
;
1720 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1721 * find the data needed to decode the coded packet
1722 * @bat_priv: the bat priv with all the soft interface information
1723 * @ethhdr: pointer to the ethernet header inside the coded packet
1724 * @coded: coded packet we try to find decode data for
1726 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1728 static struct batadv_nc_packet
*
1729 batadv_nc_find_decoding_packet(struct batadv_priv
*bat_priv
,
1730 struct ethhdr
*ethhdr
,
1731 struct batadv_coded_packet
*coded
)
1733 struct batadv_hashtable
*hash
= bat_priv
->nc
.decoding_hash
;
1734 struct batadv_nc_packet
*tmp_nc_packet
, *nc_packet
= NULL
;
1735 struct batadv_nc_path
*nc_path
, nc_path_key
;
1743 /* Select the correct packet id based on the location of our mac-addr */
1744 dest
= ethhdr
->h_source
;
1745 if (!batadv_is_my_mac(bat_priv
, coded
->second_dest
)) {
1746 source
= coded
->second_source
;
1747 packet_id
= coded
->second_crc
;
1749 source
= coded
->first_source
;
1750 packet_id
= coded
->first_crc
;
1753 batadv_nc_hash_key_gen(&nc_path_key
, source
, dest
);
1754 index
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1756 /* Search for matching coding path */
1758 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[index
], hash_entry
) {
1759 /* Find matching nc_packet */
1760 spin_lock_bh(&nc_path
->packet_list_lock
);
1761 list_for_each_entry(tmp_nc_packet
,
1762 &nc_path
->packet_list
, list
) {
1763 if (packet_id
== tmp_nc_packet
->packet_id
) {
1764 list_del(&tmp_nc_packet
->list
);
1766 nc_packet
= tmp_nc_packet
;
1770 spin_unlock_bh(&nc_path
->packet_list_lock
);
1778 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
1779 "No decoding packet found for %u\n", packet_id
);
1785 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1786 * resulting unicast packet
1787 * @skb: incoming coded packet
1788 * @recv_if: pointer to interface this packet was received on
1790 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
1791 struct batadv_hard_iface
*recv_if
)
1793 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1794 struct batadv_unicast_packet
*unicast_packet
;
1795 struct batadv_coded_packet
*coded_packet
;
1796 struct batadv_nc_packet
*nc_packet
;
1797 struct ethhdr
*ethhdr
;
1798 int hdr_size
= sizeof(*coded_packet
);
1800 /* Check if network coding is enabled */
1801 if (!atomic_read(&bat_priv
->network_coding
))
1804 /* Make sure we can access (and remove) header */
1805 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1808 coded_packet
= (struct batadv_coded_packet
*)skb
->data
;
1809 ethhdr
= eth_hdr(skb
);
1811 /* Verify frame is destined for us */
1812 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
) &&
1813 !batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1816 /* Update stat counter */
1817 if (batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1818 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_SNIFFED
);
1820 nc_packet
= batadv_nc_find_decoding_packet(bat_priv
, ethhdr
,
1823 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1827 /* Make skb's linear, because decoding accesses the entire buffer */
1828 if (skb_linearize(skb
) < 0)
1829 goto free_nc_packet
;
1831 if (skb_linearize(nc_packet
->skb
) < 0)
1832 goto free_nc_packet
;
1834 /* Decode the packet */
1835 unicast_packet
= batadv_nc_skb_decode_packet(bat_priv
, skb
, nc_packet
);
1836 if (!unicast_packet
) {
1837 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1838 goto free_nc_packet
;
1841 /* Mark packet as decoded to do correct recoding when forwarding */
1842 BATADV_SKB_CB(skb
)->decoded
= true;
1843 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE
);
1844 batadv_add_counter(bat_priv
, BATADV_CNT_NC_DECODE_BYTES
,
1845 skb
->len
+ ETH_HLEN
);
1846 return batadv_recv_unicast_packet(skb
, recv_if
);
1849 batadv_nc_packet_free(nc_packet
);
1854 * batadv_nc_mesh_free - clean up network coding memory
1855 * @bat_priv: the bat priv with all the soft interface information
1857 void batadv_nc_mesh_free(struct batadv_priv
*bat_priv
)
1859 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1860 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1861 cancel_delayed_work_sync(&bat_priv
->nc
.work
);
1863 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
, NULL
);
1864 batadv_hash_destroy(bat_priv
->nc
.coding_hash
);
1865 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
, NULL
);
1866 batadv_hash_destroy(bat_priv
->nc
.decoding_hash
);
1870 * batadv_nc_nodes_seq_print_text - print the nc node information
1871 * @seq: seq file to print on
1874 int batadv_nc_nodes_seq_print_text(struct seq_file
*seq
, void *offset
)
1876 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1877 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1878 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1879 struct batadv_hard_iface
*primary_if
;
1880 struct hlist_head
*head
;
1881 struct batadv_orig_node
*orig_node
;
1882 struct batadv_nc_node
*nc_node
;
1885 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1889 /* Traverse list of originators */
1890 for (i
= 0; i
< hash
->size
; i
++) {
1891 head
= &hash
->table
[i
];
1893 /* For each orig_node in this bin */
1895 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1896 /* no need to print the orig node if it does not have
1897 * network coding neighbors
1899 if (list_empty(&orig_node
->in_coding_list
) &&
1900 list_empty(&orig_node
->out_coding_list
))
1903 seq_printf(seq
, "Node: %pM\n", orig_node
->orig
);
1905 seq_puts(seq
, " Ingoing: ");
1906 /* For each in_nc_node to this orig_node */
1907 list_for_each_entry_rcu(nc_node
,
1908 &orig_node
->in_coding_list
,
1910 seq_printf(seq
, "%pM ",
1912 seq_puts(seq
, "\n");
1914 seq_puts(seq
, " Outgoing: ");
1915 /* For out_nc_node to this orig_node */
1916 list_for_each_entry_rcu(nc_node
,
1917 &orig_node
->out_coding_list
,
1919 seq_printf(seq
, "%pM ",
1921 seq_puts(seq
, "\n\n");
1928 batadv_hardif_free_ref(primary_if
);
1933 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1934 * @bat_priv: the bat priv with all the soft interface information
1936 int batadv_nc_init_debugfs(struct batadv_priv
*bat_priv
)
1938 struct dentry
*nc_dir
, *file
;
1940 nc_dir
= debugfs_create_dir("nc", bat_priv
->debug_dir
);
1944 file
= debugfs_create_u8("min_tq", S_IRUGO
| S_IWUSR
, nc_dir
,
1945 &bat_priv
->nc
.min_tq
);
1949 file
= debugfs_create_u32("max_fwd_delay", S_IRUGO
| S_IWUSR
, nc_dir
,
1950 &bat_priv
->nc
.max_fwd_delay
);
1954 file
= debugfs_create_u32("max_buffer_time", S_IRUGO
| S_IWUSR
, nc_dir
,
1955 &bat_priv
->nc
.max_buffer_time
);