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
)
247 kfree_skb(nc_packet
->skb
);
248 batadv_nc_path_free_ref(nc_packet
->nc_path
);
253 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
254 * @bat_priv: the bat priv with all the soft interface information
255 * @nc_node: the nc node to check
257 * Returns true if the entry has to be purged now, false otherwise
259 static bool batadv_nc_to_purge_nc_node(struct batadv_priv
*bat_priv
,
260 struct batadv_nc_node
*nc_node
)
262 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
265 return batadv_has_timed_out(nc_node
->last_seen
, BATADV_NC_NODE_TIMEOUT
);
269 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
270 * @bat_priv: the bat priv with all the soft interface information
271 * @nc_path: the nc path to check
273 * Returns true if the entry has to be purged now, false otherwise
275 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv
*bat_priv
,
276 struct batadv_nc_path
*nc_path
)
278 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
281 /* purge the path when no packets has been added for 10 times the
284 return batadv_has_timed_out(nc_path
->last_valid
,
285 bat_priv
->nc
.max_fwd_delay
* 10);
289 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
290 * @bat_priv: the bat priv with all the soft interface information
291 * @nc_path: the nc path to check
293 * Returns true if the entry has to be purged now, false otherwise
295 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv
*bat_priv
,
296 struct batadv_nc_path
*nc_path
)
298 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
301 /* purge the path when no packets has been added for 10 times the
304 return batadv_has_timed_out(nc_path
->last_valid
,
305 bat_priv
->nc
.max_buffer_time
* 10);
309 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
311 * @bat_priv: the bat priv with all the soft interface information
312 * @list: list of nc nodes
313 * @lock: nc node list lock
314 * @to_purge: function in charge to decide whether an entry has to be purged or
315 * not. This function takes the nc node as argument and has to return
316 * a boolean value: true if the entry has to be deleted, false
320 batadv_nc_purge_orig_nc_nodes(struct batadv_priv
*bat_priv
,
321 struct list_head
*list
,
323 bool (*to_purge
)(struct batadv_priv
*,
324 struct batadv_nc_node
*))
326 struct batadv_nc_node
*nc_node
, *nc_node_tmp
;
328 /* For each nc_node in list */
330 list_for_each_entry_safe(nc_node
, nc_node_tmp
, list
, list
) {
331 /* if an helper function has been passed as parameter,
332 * ask it if the entry has to be purged or not
334 if (to_purge
&& !to_purge(bat_priv
, nc_node
))
337 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
338 "Removing nc_node %pM -> %pM\n",
339 nc_node
->addr
, nc_node
->orig_node
->orig
);
340 list_del_rcu(&nc_node
->list
);
341 batadv_nc_node_free_ref(nc_node
);
343 spin_unlock_bh(lock
);
347 * batadv_nc_purge_orig - purges all nc node data attached of the given
349 * @bat_priv: the bat priv with all the soft interface information
350 * @orig_node: orig_node with the nc node entries to be purged
351 * @to_purge: function in charge to decide whether an entry has to be purged or
352 * not. This function takes the nc node as argument and has to return
353 * a boolean value: true is the entry has to be deleted, false
356 void batadv_nc_purge_orig(struct batadv_priv
*bat_priv
,
357 struct batadv_orig_node
*orig_node
,
358 bool (*to_purge
)(struct batadv_priv
*,
359 struct batadv_nc_node
*))
361 /* Check ingoing nc_node's of this orig_node */
362 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->in_coding_list
,
363 &orig_node
->in_coding_list_lock
,
366 /* Check outgoing nc_node's of this orig_node */
367 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->out_coding_list
,
368 &orig_node
->out_coding_list_lock
,
373 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
374 * have timed out nc nodes
375 * @bat_priv: the bat priv with all the soft interface information
377 static void batadv_nc_purge_orig_hash(struct batadv_priv
*bat_priv
)
379 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
380 struct hlist_head
*head
;
381 struct batadv_orig_node
*orig_node
;
387 /* For each orig_node */
388 for (i
= 0; i
< hash
->size
; i
++) {
389 head
= &hash
->table
[i
];
392 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
)
393 batadv_nc_purge_orig(bat_priv
, orig_node
,
394 batadv_nc_to_purge_nc_node
);
400 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
402 * @bat_priv: the bat priv with all the soft interface information
403 * @hash: hash table containing the nc paths to check
404 * @to_purge: function in charge to decide whether an entry has to be purged or
405 * not. This function takes the nc node as argument and has to return
406 * a boolean value: true is the entry has to be deleted, false
409 static void batadv_nc_purge_paths(struct batadv_priv
*bat_priv
,
410 struct batadv_hashtable
*hash
,
411 bool (*to_purge
)(struct batadv_priv
*,
412 struct batadv_nc_path
*))
414 struct hlist_head
*head
;
415 struct hlist_node
*node_tmp
;
416 struct batadv_nc_path
*nc_path
;
417 spinlock_t
*lock
; /* Protects lists in hash */
420 for (i
= 0; i
< hash
->size
; i
++) {
421 head
= &hash
->table
[i
];
422 lock
= &hash
->list_locks
[i
];
424 /* For each nc_path in this bin */
426 hlist_for_each_entry_safe(nc_path
, node_tmp
, head
, hash_entry
) {
427 /* if an helper function has been passed as parameter,
428 * ask it if the entry has to be purged or not
430 if (to_purge
&& !to_purge(bat_priv
, nc_path
))
433 /* purging an non-empty nc_path should never happen, but
434 * is observed under high CPU load. Delay the purging
435 * until next iteration to allow the packet_list to be
438 if (!unlikely(list_empty(&nc_path
->packet_list
))) {
439 net_ratelimited_function(printk
,
441 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
447 /* nc_path is unused, so remove it */
448 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
449 "Remove nc_path %pM -> %pM\n",
450 nc_path
->prev_hop
, nc_path
->next_hop
);
451 hlist_del_rcu(&nc_path
->hash_entry
);
452 batadv_nc_path_free_ref(nc_path
);
454 spin_unlock_bh(lock
);
459 * batadv_nc_hash_key_gen - computes the nc_path hash key
460 * @key: buffer to hold the final hash key
461 * @src: source ethernet mac address going into the hash key
462 * @dst: destination ethernet mac address going into the hash key
464 static void batadv_nc_hash_key_gen(struct batadv_nc_path
*key
, const char *src
,
467 memcpy(key
->prev_hop
, src
, sizeof(key
->prev_hop
));
468 memcpy(key
->next_hop
, dst
, sizeof(key
->next_hop
));
472 * batadv_nc_hash_choose - compute the hash value for an nc path
473 * @data: data to hash
474 * @size: size of the hash table
476 * Returns the selected index in the hash table for the given data.
478 static u32
batadv_nc_hash_choose(const void *data
, u32 size
)
480 const struct batadv_nc_path
*nc_path
= data
;
483 hash
= jhash(&nc_path
->prev_hop
, sizeof(nc_path
->prev_hop
), hash
);
484 hash
= jhash(&nc_path
->next_hop
, sizeof(nc_path
->next_hop
), hash
);
490 * batadv_nc_hash_compare - comparing function used in the network coding hash
492 * @node: node in the local table
493 * @data2: second object to compare the node to
495 * Returns 1 if the two entry are the same, 0 otherwise
497 static int batadv_nc_hash_compare(const struct hlist_node
*node
,
500 const struct batadv_nc_path
*nc_path1
, *nc_path2
;
502 nc_path1
= container_of(node
, struct batadv_nc_path
, hash_entry
);
505 /* Return 1 if the two keys are identical */
506 if (memcmp(nc_path1
->prev_hop
, nc_path2
->prev_hop
,
507 sizeof(nc_path1
->prev_hop
)) != 0)
510 if (memcmp(nc_path1
->next_hop
, nc_path2
->next_hop
,
511 sizeof(nc_path1
->next_hop
)) != 0)
518 * batadv_nc_hash_find - search for an existing nc path and return it
519 * @hash: hash table containing the nc path
522 * Returns the nc_path if found, NULL otherwise.
524 static struct batadv_nc_path
*
525 batadv_nc_hash_find(struct batadv_hashtable
*hash
,
528 struct hlist_head
*head
;
529 struct batadv_nc_path
*nc_path
, *nc_path_tmp
= NULL
;
535 index
= batadv_nc_hash_choose(data
, hash
->size
);
536 head
= &hash
->table
[index
];
539 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
540 if (!batadv_nc_hash_compare(&nc_path
->hash_entry
, data
))
543 if (!atomic_inc_not_zero(&nc_path
->refcount
))
546 nc_path_tmp
= nc_path
;
555 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
556 * @nc_packet: the nc packet to send
558 static void batadv_nc_send_packet(struct batadv_nc_packet
*nc_packet
)
560 batadv_send_skb_packet(nc_packet
->skb
,
561 nc_packet
->neigh_node
->if_incoming
,
562 nc_packet
->nc_path
->next_hop
);
563 nc_packet
->skb
= NULL
;
564 batadv_nc_packet_free(nc_packet
);
568 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
569 * @bat_priv: the bat priv with all the soft interface information
570 * @nc_path: the nc path the packet belongs to
571 * @nc_packet: the nc packet to be checked
573 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
574 * timeout. If so, the packet is no longer kept and the entry deleted from the
575 * queue. Has to be called with the appropriate locks.
577 * Returns false as soon as the entry in the fifo queue has not been timed out
578 * yet and true otherwise.
580 static bool batadv_nc_sniffed_purge(struct batadv_priv
*bat_priv
,
581 struct batadv_nc_path
*nc_path
,
582 struct batadv_nc_packet
*nc_packet
)
584 unsigned long timeout
= bat_priv
->nc
.max_buffer_time
;
587 lockdep_assert_held(&nc_path
->packet_list_lock
);
589 /* Packets are added to tail, so the remaining packets did not time
590 * out and we can stop processing the current queue
592 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
593 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
596 /* purge nc packet */
597 list_del(&nc_packet
->list
);
598 batadv_nc_packet_free(nc_packet
);
607 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
608 * @bat_priv: the bat priv with all the soft interface information
609 * @nc_path: the nc path the packet belongs to
610 * @nc_packet: the nc packet to be checked
612 * Checks whether the given nc packet has hit its forward timeout. If so, the
613 * packet is no longer delayed, immediately sent and the entry deleted from the
614 * queue. Has to be called with the appropriate locks.
616 * Returns false as soon as the entry in the fifo queue has not been timed out
617 * yet and true otherwise.
619 static bool batadv_nc_fwd_flush(struct batadv_priv
*bat_priv
,
620 struct batadv_nc_path
*nc_path
,
621 struct batadv_nc_packet
*nc_packet
)
623 unsigned long timeout
= bat_priv
->nc
.max_fwd_delay
;
625 lockdep_assert_held(&nc_path
->packet_list_lock
);
627 /* Packets are added to tail, so the remaining packets did not time
628 * out and we can stop processing the current queue
630 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
631 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
635 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
636 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
637 nc_packet
->skb
->len
+ ETH_HLEN
);
638 list_del(&nc_packet
->list
);
639 batadv_nc_send_packet(nc_packet
);
645 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
647 * @bat_priv: the bat priv with all the soft interface information
648 * @hash: to be processed hash table
649 * @process_fn: Function called to process given nc packet. Should return true
650 * to encourage this function to proceed with the next packet.
651 * Otherwise the rest of the current queue is skipped.
654 batadv_nc_process_nc_paths(struct batadv_priv
*bat_priv
,
655 struct batadv_hashtable
*hash
,
656 bool (*process_fn
)(struct batadv_priv
*,
657 struct batadv_nc_path
*,
658 struct batadv_nc_packet
*))
660 struct hlist_head
*head
;
661 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
662 struct batadv_nc_path
*nc_path
;
669 /* Loop hash table bins */
670 for (i
= 0; i
< hash
->size
; i
++) {
671 head
= &hash
->table
[i
];
673 /* Loop coding paths */
675 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
677 spin_lock_bh(&nc_path
->packet_list_lock
);
678 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
679 &nc_path
->packet_list
, list
) {
680 ret
= process_fn(bat_priv
, nc_path
, nc_packet
);
684 spin_unlock_bh(&nc_path
->packet_list_lock
);
691 * batadv_nc_worker - periodic task for house keeping related to network coding
692 * @work: kernel work struct
694 static void batadv_nc_worker(struct work_struct
*work
)
696 struct delayed_work
*delayed_work
;
697 struct batadv_priv_nc
*priv_nc
;
698 struct batadv_priv
*bat_priv
;
699 unsigned long timeout
;
701 delayed_work
= container_of(work
, struct delayed_work
, work
);
702 priv_nc
= container_of(delayed_work
, struct batadv_priv_nc
, work
);
703 bat_priv
= container_of(priv_nc
, struct batadv_priv
, nc
);
705 batadv_nc_purge_orig_hash(bat_priv
);
706 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
707 batadv_nc_to_purge_nc_path_coding
);
708 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
709 batadv_nc_to_purge_nc_path_decoding
);
711 timeout
= bat_priv
->nc
.max_fwd_delay
;
713 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_fwd_flush
, timeout
)) {
714 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
715 batadv_nc_fwd_flush
);
716 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
719 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_sniffed_purge
,
720 bat_priv
->nc
.max_buffer_time
)) {
721 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
722 batadv_nc_sniffed_purge
);
723 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
726 /* Schedule a new check */
727 batadv_nc_start_timer(bat_priv
);
731 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
733 * @bat_priv: the bat priv with all the soft interface information
734 * @orig_node: neighboring orig node which may be used as nc candidate
735 * @ogm_packet: incoming ogm packet also used for the checks
738 * 1) The OGM must have the most recent sequence number.
739 * 2) The TTL must be decremented by one and only one.
740 * 3) The OGM must be received from the first hop from orig_node.
741 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
743 static bool batadv_can_nc_with_orig(struct batadv_priv
*bat_priv
,
744 struct batadv_orig_node
*orig_node
,
745 struct batadv_ogm_packet
*ogm_packet
)
747 struct batadv_orig_ifinfo
*orig_ifinfo
;
751 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, BATADV_IF_DEFAULT
);
755 last_ttl
= orig_ifinfo
->last_ttl
;
756 last_real_seqno
= orig_ifinfo
->last_real_seqno
;
757 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
759 if (last_real_seqno
!= ntohl(ogm_packet
->seqno
))
761 if (last_ttl
!= ogm_packet
->ttl
+ 1)
763 if (!batadv_compare_eth(ogm_packet
->orig
, ogm_packet
->prev_sender
))
765 if (ogm_packet
->tq
< bat_priv
->nc
.min_tq
)
772 * batadv_nc_find_nc_node - search for an existing nc node and return it
773 * @orig_node: orig node originating the ogm packet
774 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
775 * (can be equal to orig_node)
776 * @in_coding: traverse incoming or outgoing network coding list
778 * Returns the nc_node if found, NULL otherwise.
780 static struct batadv_nc_node
781 *batadv_nc_find_nc_node(struct batadv_orig_node
*orig_node
,
782 struct batadv_orig_node
*orig_neigh_node
,
785 struct batadv_nc_node
*nc_node
, *nc_node_out
= NULL
;
786 struct list_head
*list
;
789 list
= &orig_neigh_node
->in_coding_list
;
791 list
= &orig_neigh_node
->out_coding_list
;
793 /* Traverse list of nc_nodes to orig_node */
795 list_for_each_entry_rcu(nc_node
, list
, list
) {
796 if (!batadv_compare_eth(nc_node
->addr
, orig_node
->orig
))
799 if (!atomic_inc_not_zero(&nc_node
->refcount
))
803 nc_node_out
= nc_node
;
812 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
814 * @bat_priv: the bat priv with all the soft interface information
815 * @orig_node: orig node originating the ogm packet
816 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
817 * (can be equal to orig_node)
818 * @in_coding: traverse incoming or outgoing network coding list
820 * Returns the nc_node if found or created, NULL in case of an error.
822 static struct batadv_nc_node
823 *batadv_nc_get_nc_node(struct batadv_priv
*bat_priv
,
824 struct batadv_orig_node
*orig_node
,
825 struct batadv_orig_node
*orig_neigh_node
,
828 struct batadv_nc_node
*nc_node
;
829 spinlock_t
*lock
; /* Used to lock list selected by "int in_coding" */
830 struct list_head
*list
;
832 /* Check if nc_node is already added */
833 nc_node
= batadv_nc_find_nc_node(orig_node
, orig_neigh_node
, in_coding
);
839 nc_node
= kzalloc(sizeof(*nc_node
), GFP_ATOMIC
);
843 if (!atomic_inc_not_zero(&orig_neigh_node
->refcount
))
846 /* Initialize nc_node */
847 INIT_LIST_HEAD(&nc_node
->list
);
848 ether_addr_copy(nc_node
->addr
, orig_node
->orig
);
849 nc_node
->orig_node
= orig_neigh_node
;
850 atomic_set(&nc_node
->refcount
, 2);
852 /* Select ingoing or outgoing coding node */
854 lock
= &orig_neigh_node
->in_coding_list_lock
;
855 list
= &orig_neigh_node
->in_coding_list
;
857 lock
= &orig_neigh_node
->out_coding_list_lock
;
858 list
= &orig_neigh_node
->out_coding_list
;
861 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_node %pM -> %pM\n",
862 nc_node
->addr
, nc_node
->orig_node
->orig
);
864 /* Add nc_node to orig_node */
866 list_add_tail_rcu(&nc_node
->list
, list
);
867 spin_unlock_bh(lock
);
877 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node
878 * structs (best called on incoming OGMs)
879 * @bat_priv: the bat priv with all the soft interface information
880 * @orig_node: orig node originating the ogm packet
881 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
882 * (can be equal to orig_node)
883 * @ogm_packet: incoming ogm packet
884 * @is_single_hop_neigh: orig_node is a single hop neighbor
886 void batadv_nc_update_nc_node(struct batadv_priv
*bat_priv
,
887 struct batadv_orig_node
*orig_node
,
888 struct batadv_orig_node
*orig_neigh_node
,
889 struct batadv_ogm_packet
*ogm_packet
,
890 int is_single_hop_neigh
)
892 struct batadv_nc_node
*in_nc_node
= NULL
;
893 struct batadv_nc_node
*out_nc_node
= NULL
;
895 /* Check if network coding is enabled */
896 if (!atomic_read(&bat_priv
->network_coding
))
899 /* check if orig node is network coding enabled */
900 if (!test_bit(BATADV_ORIG_CAPA_HAS_NC
, &orig_node
->capabilities
))
903 /* accept ogms from 'good' neighbors and single hop neighbors */
904 if (!batadv_can_nc_with_orig(bat_priv
, orig_node
, ogm_packet
) &&
905 !is_single_hop_neigh
)
908 /* Add orig_node as in_nc_node on hop */
909 in_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_node
,
910 orig_neigh_node
, true);
914 in_nc_node
->last_seen
= jiffies
;
916 /* Add hop as out_nc_node on orig_node */
917 out_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_neigh_node
,
922 out_nc_node
->last_seen
= jiffies
;
926 batadv_nc_node_free_ref(in_nc_node
);
928 batadv_nc_node_free_ref(out_nc_node
);
932 * batadv_nc_get_path - get existing nc_path or allocate a new one
933 * @bat_priv: the bat priv with all the soft interface information
934 * @hash: hash table containing the nc path
935 * @src: ethernet source address - first half of the nc path search key
936 * @dst: ethernet destination address - second half of the nc path search key
938 * Returns pointer to nc_path if the path was found or created, returns NULL
941 static struct batadv_nc_path
*batadv_nc_get_path(struct batadv_priv
*bat_priv
,
942 struct batadv_hashtable
*hash
,
947 struct batadv_nc_path
*nc_path
, nc_path_key
;
949 batadv_nc_hash_key_gen(&nc_path_key
, src
, dst
);
951 /* Search for existing nc_path */
952 nc_path
= batadv_nc_hash_find(hash
, (void *)&nc_path_key
);
955 /* Set timestamp to delay removal of nc_path */
956 nc_path
->last_valid
= jiffies
;
960 /* No existing nc_path was found; create a new */
961 nc_path
= kzalloc(sizeof(*nc_path
), GFP_ATOMIC
);
966 /* Initialize nc_path */
967 INIT_LIST_HEAD(&nc_path
->packet_list
);
968 spin_lock_init(&nc_path
->packet_list_lock
);
969 atomic_set(&nc_path
->refcount
, 2);
970 nc_path
->last_valid
= jiffies
;
971 ether_addr_copy(nc_path
->next_hop
, dst
);
972 ether_addr_copy(nc_path
->prev_hop
, src
);
974 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_path %pM -> %pM\n",
978 /* Add nc_path to hash table */
979 hash_added
= batadv_hash_add(hash
, batadv_nc_hash_compare
,
980 batadv_nc_hash_choose
, &nc_path_key
,
981 &nc_path
->hash_entry
);
983 if (hash_added
< 0) {
992 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
993 * selection of a receiver with slightly lower TQ than the other
994 * @tq: to be weighted tq value
996 static u8
batadv_nc_random_weight_tq(u8 tq
)
998 u8 rand_val
, rand_tq
;
1000 get_random_bytes(&rand_val
, sizeof(rand_val
));
1002 /* randomize the estimated packet loss (max TQ - estimated TQ) */
1003 rand_tq
= rand_val
* (BATADV_TQ_MAX_VALUE
- tq
);
1005 /* normalize the randomized packet loss */
1006 rand_tq
/= BATADV_TQ_MAX_VALUE
;
1008 /* convert to (randomized) estimated tq again */
1009 return BATADV_TQ_MAX_VALUE
- rand_tq
;
1013 * batadv_nc_memxor - XOR destination with source
1014 * @dst: byte array to XOR into
1015 * @src: byte array to XOR from
1016 * @len: length of destination array
1018 static void batadv_nc_memxor(char *dst
, const char *src
, unsigned int len
)
1022 for (i
= 0; i
< len
; ++i
)
1027 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
1028 * into a coded_packet and send it
1029 * @bat_priv: the bat priv with all the soft interface information
1030 * @skb: data skb to forward
1031 * @ethhdr: pointer to the ethernet header inside the skb
1032 * @nc_packet: structure containing the packet to the skb can be coded with
1033 * @neigh_node: next hop to forward packet to
1035 * Returns true if both packets are consumed, false otherwise.
1037 static bool batadv_nc_code_packets(struct batadv_priv
*bat_priv
,
1038 struct sk_buff
*skb
,
1039 struct ethhdr
*ethhdr
,
1040 struct batadv_nc_packet
*nc_packet
,
1041 struct batadv_neigh_node
*neigh_node
)
1043 u8 tq_weighted_neigh
, tq_weighted_coding
, tq_tmp
;
1044 struct sk_buff
*skb_dest
, *skb_src
;
1045 struct batadv_unicast_packet
*packet1
;
1046 struct batadv_unicast_packet
*packet2
;
1047 struct batadv_coded_packet
*coded_packet
;
1048 struct batadv_neigh_node
*neigh_tmp
, *router_neigh
;
1049 struct batadv_neigh_node
*router_coding
= NULL
;
1050 struct batadv_neigh_ifinfo
*router_neigh_ifinfo
= NULL
;
1051 struct batadv_neigh_ifinfo
*router_coding_ifinfo
= NULL
;
1052 u8
*first_source
, *first_dest
, *second_source
, *second_dest
;
1053 __be32 packet_id1
, packet_id2
;
1057 int unicast_size
= sizeof(*packet1
);
1058 int coded_size
= sizeof(*coded_packet
);
1059 int header_add
= coded_size
- unicast_size
;
1061 /* TODO: do we need to consider the outgoing interface for
1064 router_neigh
= batadv_orig_router_get(neigh_node
->orig_node
,
1069 router_neigh_ifinfo
= batadv_neigh_ifinfo_get(router_neigh
,
1071 if (!router_neigh_ifinfo
)
1074 neigh_tmp
= nc_packet
->neigh_node
;
1075 router_coding
= batadv_orig_router_get(neigh_tmp
->orig_node
,
1080 router_coding_ifinfo
= batadv_neigh_ifinfo_get(router_coding
,
1082 if (!router_coding_ifinfo
)
1085 tq_tmp
= router_neigh_ifinfo
->bat_iv
.tq_avg
;
1086 tq_weighted_neigh
= batadv_nc_random_weight_tq(tq_tmp
);
1087 tq_tmp
= router_coding_ifinfo
->bat_iv
.tq_avg
;
1088 tq_weighted_coding
= batadv_nc_random_weight_tq(tq_tmp
);
1090 /* Select one destination for the MAC-header dst-field based on
1091 * weighted TQ-values.
1093 if (tq_weighted_neigh
>= tq_weighted_coding
) {
1094 /* Destination from nc_packet is selected for MAC-header */
1095 first_dest
= nc_packet
->nc_path
->next_hop
;
1096 first_source
= nc_packet
->nc_path
->prev_hop
;
1097 second_dest
= neigh_node
->addr
;
1098 second_source
= ethhdr
->h_source
;
1099 packet1
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1100 packet2
= (struct batadv_unicast_packet
*)skb
->data
;
1101 packet_id1
= nc_packet
->packet_id
;
1102 packet_id2
= batadv_skb_crc32(skb
,
1103 skb
->data
+ sizeof(*packet2
));
1105 /* Destination for skb is selected for MAC-header */
1106 first_dest
= neigh_node
->addr
;
1107 first_source
= ethhdr
->h_source
;
1108 second_dest
= nc_packet
->nc_path
->next_hop
;
1109 second_source
= nc_packet
->nc_path
->prev_hop
;
1110 packet1
= (struct batadv_unicast_packet
*)skb
->data
;
1111 packet2
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1112 packet_id1
= batadv_skb_crc32(skb
,
1113 skb
->data
+ sizeof(*packet1
));
1114 packet_id2
= nc_packet
->packet_id
;
1117 /* Instead of zero padding the smallest data buffer, we
1118 * code into the largest.
1120 if (skb
->len
<= nc_packet
->skb
->len
) {
1121 skb_dest
= nc_packet
->skb
;
1125 skb_src
= nc_packet
->skb
;
1128 /* coding_len is used when decoding the packet shorter packet */
1129 coding_len
= skb_src
->len
- unicast_size
;
1131 if (skb_linearize(skb_dest
) < 0 || skb_linearize(skb_src
) < 0)
1134 skb_push(skb_dest
, header_add
);
1136 coded_packet
= (struct batadv_coded_packet
*)skb_dest
->data
;
1137 skb_reset_mac_header(skb_dest
);
1139 coded_packet
->packet_type
= BATADV_CODED
;
1140 coded_packet
->version
= BATADV_COMPAT_VERSION
;
1141 coded_packet
->ttl
= packet1
->ttl
;
1143 /* Info about first unicast packet */
1144 ether_addr_copy(coded_packet
->first_source
, first_source
);
1145 ether_addr_copy(coded_packet
->first_orig_dest
, packet1
->dest
);
1146 coded_packet
->first_crc
= packet_id1
;
1147 coded_packet
->first_ttvn
= packet1
->ttvn
;
1149 /* Info about second unicast packet */
1150 ether_addr_copy(coded_packet
->second_dest
, second_dest
);
1151 ether_addr_copy(coded_packet
->second_source
, second_source
);
1152 ether_addr_copy(coded_packet
->second_orig_dest
, packet2
->dest
);
1153 coded_packet
->second_crc
= packet_id2
;
1154 coded_packet
->second_ttl
= packet2
->ttl
;
1155 coded_packet
->second_ttvn
= packet2
->ttvn
;
1156 coded_packet
->coded_len
= htons(coding_len
);
1158 /* This is where the magic happens: Code skb_src into skb_dest */
1159 batadv_nc_memxor(skb_dest
->data
+ coded_size
,
1160 skb_src
->data
+ unicast_size
, coding_len
);
1162 /* Update counters accordingly */
1163 if (BATADV_SKB_CB(skb_src
)->decoded
&&
1164 BATADV_SKB_CB(skb_dest
)->decoded
) {
1165 /* Both packets are recoded */
1166 count
= skb_src
->len
+ ETH_HLEN
;
1167 count
+= skb_dest
->len
+ ETH_HLEN
;
1168 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE
, 2);
1169 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
, count
);
1170 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1171 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1172 /* Both packets are newly coded */
1173 count
= skb_src
->len
+ ETH_HLEN
;
1174 count
+= skb_dest
->len
+ ETH_HLEN
;
1175 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE
, 2);
1176 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
, count
);
1177 } else if (BATADV_SKB_CB(skb_src
)->decoded
&&
1178 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1179 /* skb_src recoded and skb_dest is newly coded */
1180 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1181 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1182 skb_src
->len
+ ETH_HLEN
);
1183 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1184 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1185 skb_dest
->len
+ ETH_HLEN
);
1186 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1187 BATADV_SKB_CB(skb_dest
)->decoded
) {
1188 /* skb_src is newly coded and skb_dest is recoded */
1189 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1190 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1191 skb_src
->len
+ ETH_HLEN
);
1192 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1193 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1194 skb_dest
->len
+ ETH_HLEN
);
1197 /* skb_src is now coded into skb_dest, so free it */
1200 /* avoid duplicate free of skb from nc_packet */
1201 nc_packet
->skb
= NULL
;
1202 batadv_nc_packet_free(nc_packet
);
1204 /* Send the coded packet and return true */
1205 batadv_send_skb_packet(skb_dest
, neigh_node
->if_incoming
, first_dest
);
1209 batadv_neigh_node_free_ref(router_neigh
);
1211 batadv_neigh_node_free_ref(router_coding
);
1212 if (router_neigh_ifinfo
)
1213 batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo
);
1214 if (router_coding_ifinfo
)
1215 batadv_neigh_ifinfo_free_ref(router_coding_ifinfo
);
1220 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1221 * @skb: data skb to forward
1222 * @dst: destination mac address of the other skb to code with
1223 * @src: source mac address of skb
1225 * Whenever we network code a packet we have to check whether we received it in
1226 * a network coded form. If so, we may not be able to use it for coding because
1227 * some neighbors may also have received (overheard) the packet in the network
1228 * coded form without being able to decode it. It is hard to know which of the
1229 * neighboring nodes was able to decode the packet, therefore we can only
1230 * re-code the packet if the source of the previous encoded packet is involved.
1231 * Since the source encoded the packet we can be certain it has all necessary
1232 * decode information.
1234 * Returns true if coding of a decoded packet is allowed.
1236 static bool batadv_nc_skb_coding_possible(struct sk_buff
*skb
, u8
*dst
, u8
*src
)
1238 if (BATADV_SKB_CB(skb
)->decoded
&& !batadv_compare_eth(dst
, src
))
1244 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1245 * out_nc_node to retrieve a buffered packet that can be used for coding.
1246 * @bat_priv: the bat priv with all the soft interface information
1247 * @in_nc_node: pointer to skb next hop's neighbor nc node
1248 * @out_nc_node: pointer to skb source's neighbor nc node
1249 * @skb: data skb to forward
1250 * @eth_dst: next hop mac address of skb
1252 * Returns true if coding of a decoded skb is allowed.
1254 static struct batadv_nc_packet
*
1255 batadv_nc_path_search(struct batadv_priv
*bat_priv
,
1256 struct batadv_nc_node
*in_nc_node
,
1257 struct batadv_nc_node
*out_nc_node
,
1258 struct sk_buff
*skb
,
1261 struct batadv_nc_path
*nc_path
, nc_path_key
;
1262 struct batadv_nc_packet
*nc_packet_out
= NULL
;
1263 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
1264 struct batadv_hashtable
*hash
= bat_priv
->nc
.coding_hash
;
1270 /* Create almost path key */
1271 batadv_nc_hash_key_gen(&nc_path_key
, in_nc_node
->addr
,
1273 idx
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1275 /* Check for coding opportunities in this nc_path */
1277 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[idx
], hash_entry
) {
1278 if (!batadv_compare_eth(nc_path
->prev_hop
, in_nc_node
->addr
))
1281 if (!batadv_compare_eth(nc_path
->next_hop
, out_nc_node
->addr
))
1284 spin_lock_bh(&nc_path
->packet_list_lock
);
1285 if (list_empty(&nc_path
->packet_list
)) {
1286 spin_unlock_bh(&nc_path
->packet_list_lock
);
1290 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
1291 &nc_path
->packet_list
, list
) {
1292 if (!batadv_nc_skb_coding_possible(nc_packet
->skb
,
1297 /* Coding opportunity is found! */
1298 list_del(&nc_packet
->list
);
1299 nc_packet_out
= nc_packet
;
1303 spin_unlock_bh(&nc_path
->packet_list_lock
);
1308 return nc_packet_out
;
1312 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1313 * skb's sender (may be equal to the originator).
1314 * @bat_priv: the bat priv with all the soft interface information
1315 * @skb: data skb to forward
1316 * @eth_dst: next hop mac address of skb
1317 * @eth_src: source mac address of skb
1318 * @in_nc_node: pointer to skb next hop's neighbor nc node
1320 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1322 static struct batadv_nc_packet
*
1323 batadv_nc_skb_src_search(struct batadv_priv
*bat_priv
,
1324 struct sk_buff
*skb
,
1327 struct batadv_nc_node
*in_nc_node
)
1329 struct batadv_orig_node
*orig_node
;
1330 struct batadv_nc_node
*out_nc_node
;
1331 struct batadv_nc_packet
*nc_packet
= NULL
;
1333 orig_node
= batadv_orig_hash_find(bat_priv
, eth_src
);
1338 list_for_each_entry_rcu(out_nc_node
,
1339 &orig_node
->out_coding_list
, list
) {
1340 /* Check if the skb is decoded and if recoding is possible */
1341 if (!batadv_nc_skb_coding_possible(skb
,
1342 out_nc_node
->addr
, eth_src
))
1345 /* Search for an opportunity in this nc_path */
1346 nc_packet
= batadv_nc_path_search(bat_priv
, in_nc_node
,
1347 out_nc_node
, skb
, eth_dst
);
1353 batadv_orig_node_free_ref(orig_node
);
1358 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1359 * unicast skb before it is stored for use in later decoding
1360 * @bat_priv: the bat priv with all the soft interface information
1361 * @skb: data skb to store
1362 * @eth_dst_new: new destination mac address of skb
1364 static void batadv_nc_skb_store_before_coding(struct batadv_priv
*bat_priv
,
1365 struct sk_buff
*skb
,
1368 struct ethhdr
*ethhdr
;
1370 /* Copy skb header to change the mac header */
1371 skb
= pskb_copy_for_clone(skb
, GFP_ATOMIC
);
1375 /* Set the mac header as if we actually sent the packet uncoded */
1376 ethhdr
= eth_hdr(skb
);
1377 ether_addr_copy(ethhdr
->h_source
, ethhdr
->h_dest
);
1378 ether_addr_copy(ethhdr
->h_dest
, eth_dst_new
);
1380 /* Set data pointer to MAC header to mimic packets from our tx path */
1381 skb_push(skb
, ETH_HLEN
);
1383 /* Add the packet to the decoding packet pool */
1384 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1386 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1393 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1394 * @skb: data skb to forward
1395 * @neigh_node: next hop to forward packet to
1396 * @ethhdr: pointer to the ethernet header inside the skb
1398 * Loops through list of neighboring nodes the next hop has a good connection to
1399 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1400 * next hop that potentially sent a packet which our next hop also received
1401 * (overheard) and has stored for later decoding.
1403 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1405 static bool batadv_nc_skb_dst_search(struct sk_buff
*skb
,
1406 struct batadv_neigh_node
*neigh_node
,
1407 struct ethhdr
*ethhdr
)
1409 struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1410 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1411 struct batadv_orig_node
*orig_node
= neigh_node
->orig_node
;
1412 struct batadv_nc_node
*nc_node
;
1413 struct batadv_nc_packet
*nc_packet
= NULL
;
1416 list_for_each_entry_rcu(nc_node
, &orig_node
->in_coding_list
, list
) {
1417 /* Search for coding opportunity with this in_nc_node */
1418 nc_packet
= batadv_nc_skb_src_search(bat_priv
, skb
,
1420 ethhdr
->h_source
, nc_node
);
1422 /* Opportunity was found, so stop searching */
1431 /* Save packets for later decoding */
1432 batadv_nc_skb_store_before_coding(bat_priv
, skb
,
1434 batadv_nc_skb_store_before_coding(bat_priv
, nc_packet
->skb
,
1435 nc_packet
->neigh_node
->addr
);
1437 /* Code and send packets */
1438 if (batadv_nc_code_packets(bat_priv
, skb
, ethhdr
, nc_packet
,
1442 /* out of mem ? Coding failed - we have to free the buffered packet
1443 * to avoid memleaks. The skb passed as argument will be dealt with
1444 * by the calling function.
1446 batadv_nc_send_packet(nc_packet
);
1451 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1452 * @skb: skb to add to path
1453 * @nc_path: path to add skb to
1454 * @neigh_node: next hop to forward packet to
1455 * @packet_id: checksum to identify packet
1457 * Returns true if the packet was buffered or false in case of an error.
1459 static bool batadv_nc_skb_add_to_path(struct sk_buff
*skb
,
1460 struct batadv_nc_path
*nc_path
,
1461 struct batadv_neigh_node
*neigh_node
,
1464 struct batadv_nc_packet
*nc_packet
;
1466 nc_packet
= kzalloc(sizeof(*nc_packet
), GFP_ATOMIC
);
1470 /* Initialize nc_packet */
1471 nc_packet
->timestamp
= jiffies
;
1472 nc_packet
->packet_id
= packet_id
;
1473 nc_packet
->skb
= skb
;
1474 nc_packet
->neigh_node
= neigh_node
;
1475 nc_packet
->nc_path
= nc_path
;
1477 /* Add coding packet to list */
1478 spin_lock_bh(&nc_path
->packet_list_lock
);
1479 list_add_tail(&nc_packet
->list
, &nc_path
->packet_list
);
1480 spin_unlock_bh(&nc_path
->packet_list_lock
);
1486 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1488 * @skb: data skb to forward
1489 * @neigh_node: next hop to forward packet to
1491 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1493 bool batadv_nc_skb_forward(struct sk_buff
*skb
,
1494 struct batadv_neigh_node
*neigh_node
)
1496 const struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1497 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1498 struct batadv_unicast_packet
*packet
;
1499 struct batadv_nc_path
*nc_path
;
1500 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1504 /* Check if network coding is enabled */
1505 if (!atomic_read(&bat_priv
->network_coding
))
1508 /* We only handle unicast packets */
1509 payload
= skb_network_header(skb
);
1510 packet
= (struct batadv_unicast_packet
*)payload
;
1511 if (packet
->packet_type
!= BATADV_UNICAST
)
1514 /* Try to find a coding opportunity and send the skb if one is found */
1515 if (batadv_nc_skb_dst_search(skb
, neigh_node
, ethhdr
))
1518 /* Find or create a nc_path for this src-dst pair */
1519 nc_path
= batadv_nc_get_path(bat_priv
,
1520 bat_priv
->nc
.coding_hash
,
1527 /* Add skb to nc_path */
1528 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1529 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, neigh_node
, packet_id
))
1532 /* Packet is consumed */
1536 batadv_nc_path_free_ref(nc_path
);
1538 /* Packet is not consumed */
1543 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1544 * when decoding coded packets
1545 * @bat_priv: the bat priv with all the soft interface information
1546 * @skb: data skb to store
1548 void batadv_nc_skb_store_for_decoding(struct batadv_priv
*bat_priv
,
1549 struct sk_buff
*skb
)
1551 struct batadv_unicast_packet
*packet
;
1552 struct batadv_nc_path
*nc_path
;
1553 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1557 /* Check if network coding is enabled */
1558 if (!atomic_read(&bat_priv
->network_coding
))
1561 /* Check for supported packet type */
1562 payload
= skb_network_header(skb
);
1563 packet
= (struct batadv_unicast_packet
*)payload
;
1564 if (packet
->packet_type
!= BATADV_UNICAST
)
1567 /* Find existing nc_path or create a new */
1568 nc_path
= batadv_nc_get_path(bat_priv
,
1569 bat_priv
->nc
.decoding_hash
,
1576 /* Clone skb and adjust skb->data to point at batman header */
1577 skb
= skb_clone(skb
, GFP_ATOMIC
);
1581 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
1584 if (unlikely(!skb_pull_rcsum(skb
, ETH_HLEN
)))
1587 /* Add skb to nc_path */
1588 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1589 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, NULL
, packet_id
))
1592 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_BUFFER
);
1598 batadv_nc_path_free_ref(nc_path
);
1604 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1605 * should be saved in the decoding buffer and, if so, store it there
1606 * @bat_priv: the bat priv with all the soft interface information
1607 * @skb: unicast skb to store
1609 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv
*bat_priv
,
1610 struct sk_buff
*skb
)
1612 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1614 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1617 /* Set data pointer to MAC header to mimic packets from our tx path */
1618 skb_push(skb
, ETH_HLEN
);
1620 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1624 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1626 * @bat_priv: the bat priv with all the soft interface information
1627 * @skb: unicast skb to decode
1628 * @nc_packet: decode data needed to decode the skb
1630 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1631 * in case of an error.
1633 static struct batadv_unicast_packet
*
1634 batadv_nc_skb_decode_packet(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1635 struct batadv_nc_packet
*nc_packet
)
1637 const int h_size
= sizeof(struct batadv_unicast_packet
);
1638 const int h_diff
= sizeof(struct batadv_coded_packet
) - h_size
;
1639 struct batadv_unicast_packet
*unicast_packet
;
1640 struct batadv_coded_packet coded_packet_tmp
;
1641 struct ethhdr
*ethhdr
, ethhdr_tmp
;
1642 u8
*orig_dest
, ttl
, ttvn
;
1643 unsigned int coding_len
;
1646 /* Save headers temporarily */
1647 memcpy(&coded_packet_tmp
, skb
->data
, sizeof(coded_packet_tmp
));
1648 memcpy(ðhdr_tmp
, skb_mac_header(skb
), sizeof(ethhdr_tmp
));
1650 if (skb_cow(skb
, 0) < 0)
1653 if (unlikely(!skb_pull_rcsum(skb
, h_diff
)))
1656 /* Data points to batman header, so set mac header 14 bytes before
1657 * and network to data
1659 skb_set_mac_header(skb
, -ETH_HLEN
);
1660 skb_reset_network_header(skb
);
1662 /* Reconstruct original mac header */
1663 ethhdr
= eth_hdr(skb
);
1664 *ethhdr
= ethhdr_tmp
;
1666 /* Select the correct unicast header information based on the location
1667 * of our mac address in the coded_packet header
1669 if (batadv_is_my_mac(bat_priv
, coded_packet_tmp
.second_dest
)) {
1670 /* If we are the second destination the packet was overheard,
1671 * so the Ethernet address must be copied to h_dest and
1672 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1674 ether_addr_copy(ethhdr
->h_dest
, coded_packet_tmp
.second_dest
);
1675 skb
->pkt_type
= PACKET_HOST
;
1677 orig_dest
= coded_packet_tmp
.second_orig_dest
;
1678 ttl
= coded_packet_tmp
.second_ttl
;
1679 ttvn
= coded_packet_tmp
.second_ttvn
;
1681 orig_dest
= coded_packet_tmp
.first_orig_dest
;
1682 ttl
= coded_packet_tmp
.ttl
;
1683 ttvn
= coded_packet_tmp
.first_ttvn
;
1686 coding_len
= ntohs(coded_packet_tmp
.coded_len
);
1688 if (coding_len
> skb
->len
)
1691 /* Here the magic is reversed:
1692 * extract the missing packet from the received coded packet
1694 batadv_nc_memxor(skb
->data
+ h_size
,
1695 nc_packet
->skb
->data
+ h_size
,
1698 /* Resize decoded skb if decoded with larger packet */
1699 if (nc_packet
->skb
->len
> coding_len
+ h_size
) {
1700 err
= pskb_trim_rcsum(skb
, coding_len
+ h_size
);
1705 /* Create decoded unicast packet */
1706 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1707 unicast_packet
->packet_type
= BATADV_UNICAST
;
1708 unicast_packet
->version
= BATADV_COMPAT_VERSION
;
1709 unicast_packet
->ttl
= ttl
;
1710 ether_addr_copy(unicast_packet
->dest
, orig_dest
);
1711 unicast_packet
->ttvn
= ttvn
;
1713 batadv_nc_packet_free(nc_packet
);
1714 return unicast_packet
;
1718 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1719 * find the data needed to decode the coded packet
1720 * @bat_priv: the bat priv with all the soft interface information
1721 * @ethhdr: pointer to the ethernet header inside the coded packet
1722 * @coded: coded packet we try to find decode data for
1724 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1726 static struct batadv_nc_packet
*
1727 batadv_nc_find_decoding_packet(struct batadv_priv
*bat_priv
,
1728 struct ethhdr
*ethhdr
,
1729 struct batadv_coded_packet
*coded
)
1731 struct batadv_hashtable
*hash
= bat_priv
->nc
.decoding_hash
;
1732 struct batadv_nc_packet
*tmp_nc_packet
, *nc_packet
= NULL
;
1733 struct batadv_nc_path
*nc_path
, nc_path_key
;
1741 /* Select the correct packet id based on the location of our mac-addr */
1742 dest
= ethhdr
->h_source
;
1743 if (!batadv_is_my_mac(bat_priv
, coded
->second_dest
)) {
1744 source
= coded
->second_source
;
1745 packet_id
= coded
->second_crc
;
1747 source
= coded
->first_source
;
1748 packet_id
= coded
->first_crc
;
1751 batadv_nc_hash_key_gen(&nc_path_key
, source
, dest
);
1752 index
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1754 /* Search for matching coding path */
1756 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[index
], hash_entry
) {
1757 /* Find matching nc_packet */
1758 spin_lock_bh(&nc_path
->packet_list_lock
);
1759 list_for_each_entry(tmp_nc_packet
,
1760 &nc_path
->packet_list
, list
) {
1761 if (packet_id
== tmp_nc_packet
->packet_id
) {
1762 list_del(&tmp_nc_packet
->list
);
1764 nc_packet
= tmp_nc_packet
;
1768 spin_unlock_bh(&nc_path
->packet_list_lock
);
1776 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
1777 "No decoding packet found for %u\n", packet_id
);
1783 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1784 * resulting unicast packet
1785 * @skb: incoming coded packet
1786 * @recv_if: pointer to interface this packet was received on
1788 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
1789 struct batadv_hard_iface
*recv_if
)
1791 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1792 struct batadv_unicast_packet
*unicast_packet
;
1793 struct batadv_coded_packet
*coded_packet
;
1794 struct batadv_nc_packet
*nc_packet
;
1795 struct ethhdr
*ethhdr
;
1796 int hdr_size
= sizeof(*coded_packet
);
1798 /* Check if network coding is enabled */
1799 if (!atomic_read(&bat_priv
->network_coding
))
1802 /* Make sure we can access (and remove) header */
1803 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1806 coded_packet
= (struct batadv_coded_packet
*)skb
->data
;
1807 ethhdr
= eth_hdr(skb
);
1809 /* Verify frame is destined for us */
1810 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
) &&
1811 !batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1814 /* Update stat counter */
1815 if (batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1816 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_SNIFFED
);
1818 nc_packet
= batadv_nc_find_decoding_packet(bat_priv
, ethhdr
,
1821 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1825 /* Make skb's linear, because decoding accesses the entire buffer */
1826 if (skb_linearize(skb
) < 0)
1827 goto free_nc_packet
;
1829 if (skb_linearize(nc_packet
->skb
) < 0)
1830 goto free_nc_packet
;
1832 /* Decode the packet */
1833 unicast_packet
= batadv_nc_skb_decode_packet(bat_priv
, skb
, nc_packet
);
1834 if (!unicast_packet
) {
1835 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1836 goto free_nc_packet
;
1839 /* Mark packet as decoded to do correct recoding when forwarding */
1840 BATADV_SKB_CB(skb
)->decoded
= true;
1841 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE
);
1842 batadv_add_counter(bat_priv
, BATADV_CNT_NC_DECODE_BYTES
,
1843 skb
->len
+ ETH_HLEN
);
1844 return batadv_recv_unicast_packet(skb
, recv_if
);
1847 batadv_nc_packet_free(nc_packet
);
1852 * batadv_nc_mesh_free - clean up network coding memory
1853 * @bat_priv: the bat priv with all the soft interface information
1855 void batadv_nc_mesh_free(struct batadv_priv
*bat_priv
)
1857 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1858 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1859 cancel_delayed_work_sync(&bat_priv
->nc
.work
);
1861 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
, NULL
);
1862 batadv_hash_destroy(bat_priv
->nc
.coding_hash
);
1863 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
, NULL
);
1864 batadv_hash_destroy(bat_priv
->nc
.decoding_hash
);
1868 * batadv_nc_nodes_seq_print_text - print the nc node information
1869 * @seq: seq file to print on
1872 int batadv_nc_nodes_seq_print_text(struct seq_file
*seq
, void *offset
)
1874 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1875 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1876 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1877 struct batadv_hard_iface
*primary_if
;
1878 struct hlist_head
*head
;
1879 struct batadv_orig_node
*orig_node
;
1880 struct batadv_nc_node
*nc_node
;
1883 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1887 /* Traverse list of originators */
1888 for (i
= 0; i
< hash
->size
; i
++) {
1889 head
= &hash
->table
[i
];
1891 /* For each orig_node in this bin */
1893 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1894 /* no need to print the orig node if it does not have
1895 * network coding neighbors
1897 if (list_empty(&orig_node
->in_coding_list
) &&
1898 list_empty(&orig_node
->out_coding_list
))
1901 seq_printf(seq
, "Node: %pM\n", orig_node
->orig
);
1903 seq_puts(seq
, " Ingoing: ");
1904 /* For each in_nc_node to this orig_node */
1905 list_for_each_entry_rcu(nc_node
,
1906 &orig_node
->in_coding_list
,
1908 seq_printf(seq
, "%pM ",
1910 seq_puts(seq
, "\n");
1912 seq_puts(seq
, " Outgoing: ");
1913 /* For out_nc_node to this orig_node */
1914 list_for_each_entry_rcu(nc_node
,
1915 &orig_node
->out_coding_list
,
1917 seq_printf(seq
, "%pM ",
1919 seq_puts(seq
, "\n\n");
1926 batadv_hardif_free_ref(primary_if
);
1931 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1932 * @bat_priv: the bat priv with all the soft interface information
1934 int batadv_nc_init_debugfs(struct batadv_priv
*bat_priv
)
1936 struct dentry
*nc_dir
, *file
;
1938 nc_dir
= debugfs_create_dir("nc", bat_priv
->debug_dir
);
1942 file
= debugfs_create_u8("min_tq", S_IRUGO
| S_IWUSR
, nc_dir
,
1943 &bat_priv
->nc
.min_tq
);
1947 file
= debugfs_create_u32("max_fwd_delay", S_IRUGO
| S_IWUSR
, nc_dir
,
1948 &bat_priv
->nc
.max_fwd_delay
);
1952 file
= debugfs_create_u32("max_buffer_time", S_IRUGO
| S_IWUSR
, nc_dir
,
1953 &bat_priv
->nc
.max_buffer_time
);