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_release - release nc_node from lists and queue for free after
208 * @nc_node: the nc node to free
210 static void batadv_nc_node_release(struct batadv_nc_node
*nc_node
)
212 batadv_orig_node_free_ref(nc_node
->orig_node
);
213 kfree_rcu(nc_node
, rcu
);
217 * batadv_nc_node_free_ref - decrement the nc node refcounter and possibly
219 * @nc_node: the nc node to free
221 static void batadv_nc_node_free_ref(struct batadv_nc_node
*nc_node
)
223 if (atomic_dec_and_test(&nc_node
->refcount
))
224 batadv_nc_node_release(nc_node
);
228 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
230 * @nc_path: the nc node to free
232 static void batadv_nc_path_free_ref(struct batadv_nc_path
*nc_path
)
234 if (atomic_dec_and_test(&nc_path
->refcount
))
235 kfree_rcu(nc_path
, rcu
);
239 * batadv_nc_packet_free - frees nc packet
240 * @nc_packet: the nc packet to free
242 static void batadv_nc_packet_free(struct batadv_nc_packet
*nc_packet
)
245 kfree_skb(nc_packet
->skb
);
247 batadv_nc_path_free_ref(nc_packet
->nc_path
);
252 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
253 * @bat_priv: the bat priv with all the soft interface information
254 * @nc_node: the nc node to check
256 * Returns true if the entry has to be purged now, false otherwise
258 static bool batadv_nc_to_purge_nc_node(struct batadv_priv
*bat_priv
,
259 struct batadv_nc_node
*nc_node
)
261 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
264 return batadv_has_timed_out(nc_node
->last_seen
, BATADV_NC_NODE_TIMEOUT
);
268 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
269 * @bat_priv: the bat priv with all the soft interface information
270 * @nc_path: the nc path to check
272 * Returns true if the entry has to be purged now, false otherwise
274 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv
*bat_priv
,
275 struct batadv_nc_path
*nc_path
)
277 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
280 /* purge the path when no packets has been added for 10 times the
283 return batadv_has_timed_out(nc_path
->last_valid
,
284 bat_priv
->nc
.max_fwd_delay
* 10);
288 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
289 * @bat_priv: the bat priv with all the soft interface information
290 * @nc_path: the nc path to check
292 * Returns true if the entry has to be purged now, false otherwise
294 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv
*bat_priv
,
295 struct batadv_nc_path
*nc_path
)
297 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
300 /* purge the path when no packets has been added for 10 times the
303 return batadv_has_timed_out(nc_path
->last_valid
,
304 bat_priv
->nc
.max_buffer_time
* 10);
308 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
310 * @bat_priv: the bat priv with all the soft interface information
311 * @list: list of nc nodes
312 * @lock: nc node list lock
313 * @to_purge: function in charge to decide whether an entry has to be purged or
314 * not. This function takes the nc node as argument and has to return
315 * a boolean value: true if the entry has to be deleted, false
319 batadv_nc_purge_orig_nc_nodes(struct batadv_priv
*bat_priv
,
320 struct list_head
*list
,
322 bool (*to_purge
)(struct batadv_priv
*,
323 struct batadv_nc_node
*))
325 struct batadv_nc_node
*nc_node
, *nc_node_tmp
;
327 /* For each nc_node in list */
329 list_for_each_entry_safe(nc_node
, nc_node_tmp
, list
, list
) {
330 /* if an helper function has been passed as parameter,
331 * ask it if the entry has to be purged or not
333 if (to_purge
&& !to_purge(bat_priv
, nc_node
))
336 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
337 "Removing nc_node %pM -> %pM\n",
338 nc_node
->addr
, nc_node
->orig_node
->orig
);
339 list_del_rcu(&nc_node
->list
);
340 batadv_nc_node_free_ref(nc_node
);
342 spin_unlock_bh(lock
);
346 * batadv_nc_purge_orig - purges all nc node data attached of the given
348 * @bat_priv: the bat priv with all the soft interface information
349 * @orig_node: orig_node with the nc node entries to be purged
350 * @to_purge: function in charge to decide whether an entry has to be purged or
351 * not. This function takes the nc node as argument and has to return
352 * a boolean value: true is the entry has to be deleted, false
355 void batadv_nc_purge_orig(struct batadv_priv
*bat_priv
,
356 struct batadv_orig_node
*orig_node
,
357 bool (*to_purge
)(struct batadv_priv
*,
358 struct batadv_nc_node
*))
360 /* Check ingoing nc_node's of this orig_node */
361 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->in_coding_list
,
362 &orig_node
->in_coding_list_lock
,
365 /* Check outgoing nc_node's of this orig_node */
366 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->out_coding_list
,
367 &orig_node
->out_coding_list_lock
,
372 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
373 * have timed out nc nodes
374 * @bat_priv: the bat priv with all the soft interface information
376 static void batadv_nc_purge_orig_hash(struct batadv_priv
*bat_priv
)
378 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
379 struct hlist_head
*head
;
380 struct batadv_orig_node
*orig_node
;
386 /* For each orig_node */
387 for (i
= 0; i
< hash
->size
; i
++) {
388 head
= &hash
->table
[i
];
391 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
)
392 batadv_nc_purge_orig(bat_priv
, orig_node
,
393 batadv_nc_to_purge_nc_node
);
399 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
401 * @bat_priv: the bat priv with all the soft interface information
402 * @hash: hash table containing the nc paths to check
403 * @to_purge: function in charge to decide whether an entry has to be purged or
404 * not. This function takes the nc node as argument and has to return
405 * a boolean value: true is the entry has to be deleted, false
408 static void batadv_nc_purge_paths(struct batadv_priv
*bat_priv
,
409 struct batadv_hashtable
*hash
,
410 bool (*to_purge
)(struct batadv_priv
*,
411 struct batadv_nc_path
*))
413 struct hlist_head
*head
;
414 struct hlist_node
*node_tmp
;
415 struct batadv_nc_path
*nc_path
;
416 spinlock_t
*lock
; /* Protects lists in hash */
419 for (i
= 0; i
< hash
->size
; i
++) {
420 head
= &hash
->table
[i
];
421 lock
= &hash
->list_locks
[i
];
423 /* For each nc_path in this bin */
425 hlist_for_each_entry_safe(nc_path
, node_tmp
, head
, hash_entry
) {
426 /* if an helper function has been passed as parameter,
427 * ask it if the entry has to be purged or not
429 if (to_purge
&& !to_purge(bat_priv
, nc_path
))
432 /* purging an non-empty nc_path should never happen, but
433 * is observed under high CPU load. Delay the purging
434 * until next iteration to allow the packet_list to be
437 if (!unlikely(list_empty(&nc_path
->packet_list
))) {
438 net_ratelimited_function(printk
,
440 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
446 /* nc_path is unused, so remove it */
447 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
448 "Remove nc_path %pM -> %pM\n",
449 nc_path
->prev_hop
, nc_path
->next_hop
);
450 hlist_del_rcu(&nc_path
->hash_entry
);
451 batadv_nc_path_free_ref(nc_path
);
453 spin_unlock_bh(lock
);
458 * batadv_nc_hash_key_gen - computes the nc_path hash key
459 * @key: buffer to hold the final hash key
460 * @src: source ethernet mac address going into the hash key
461 * @dst: destination ethernet mac address going into the hash key
463 static void batadv_nc_hash_key_gen(struct batadv_nc_path
*key
, const char *src
,
466 memcpy(key
->prev_hop
, src
, sizeof(key
->prev_hop
));
467 memcpy(key
->next_hop
, dst
, sizeof(key
->next_hop
));
471 * batadv_nc_hash_choose - compute the hash value for an nc path
472 * @data: data to hash
473 * @size: size of the hash table
475 * Returns the selected index in the hash table for the given data.
477 static u32
batadv_nc_hash_choose(const void *data
, u32 size
)
479 const struct batadv_nc_path
*nc_path
= data
;
482 hash
= jhash(&nc_path
->prev_hop
, sizeof(nc_path
->prev_hop
), hash
);
483 hash
= jhash(&nc_path
->next_hop
, sizeof(nc_path
->next_hop
), hash
);
489 * batadv_nc_hash_compare - comparing function used in the network coding hash
491 * @node: node in the local table
492 * @data2: second object to compare the node to
494 * Returns 1 if the two entry are the same, 0 otherwise
496 static int batadv_nc_hash_compare(const struct hlist_node
*node
,
499 const struct batadv_nc_path
*nc_path1
, *nc_path2
;
501 nc_path1
= container_of(node
, struct batadv_nc_path
, hash_entry
);
504 /* Return 1 if the two keys are identical */
505 if (memcmp(nc_path1
->prev_hop
, nc_path2
->prev_hop
,
506 sizeof(nc_path1
->prev_hop
)) != 0)
509 if (memcmp(nc_path1
->next_hop
, nc_path2
->next_hop
,
510 sizeof(nc_path1
->next_hop
)) != 0)
517 * batadv_nc_hash_find - search for an existing nc path and return it
518 * @hash: hash table containing the nc path
521 * Returns the nc_path if found, NULL otherwise.
523 static struct batadv_nc_path
*
524 batadv_nc_hash_find(struct batadv_hashtable
*hash
,
527 struct hlist_head
*head
;
528 struct batadv_nc_path
*nc_path
, *nc_path_tmp
= NULL
;
534 index
= batadv_nc_hash_choose(data
, hash
->size
);
535 head
= &hash
->table
[index
];
538 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
539 if (!batadv_nc_hash_compare(&nc_path
->hash_entry
, data
))
542 if (!atomic_inc_not_zero(&nc_path
->refcount
))
545 nc_path_tmp
= nc_path
;
554 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
555 * @nc_packet: the nc packet to send
557 static void batadv_nc_send_packet(struct batadv_nc_packet
*nc_packet
)
559 batadv_send_skb_packet(nc_packet
->skb
,
560 nc_packet
->neigh_node
->if_incoming
,
561 nc_packet
->nc_path
->next_hop
);
562 nc_packet
->skb
= NULL
;
563 batadv_nc_packet_free(nc_packet
);
567 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
568 * @bat_priv: the bat priv with all the soft interface information
569 * @nc_path: the nc path the packet belongs to
570 * @nc_packet: the nc packet to be checked
572 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
573 * timeout. If so, the packet is no longer kept and the entry deleted from the
574 * queue. Has to be called with the appropriate locks.
576 * Returns false as soon as the entry in the fifo queue has not been timed out
577 * yet and true otherwise.
579 static bool batadv_nc_sniffed_purge(struct batadv_priv
*bat_priv
,
580 struct batadv_nc_path
*nc_path
,
581 struct batadv_nc_packet
*nc_packet
)
583 unsigned long timeout
= bat_priv
->nc
.max_buffer_time
;
586 lockdep_assert_held(&nc_path
->packet_list_lock
);
588 /* Packets are added to tail, so the remaining packets did not time
589 * out and we can stop processing the current queue
591 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
592 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
595 /* purge nc packet */
596 list_del(&nc_packet
->list
);
597 batadv_nc_packet_free(nc_packet
);
606 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
607 * @bat_priv: the bat priv with all the soft interface information
608 * @nc_path: the nc path the packet belongs to
609 * @nc_packet: the nc packet to be checked
611 * Checks whether the given nc packet has hit its forward timeout. If so, the
612 * packet is no longer delayed, immediately sent and the entry deleted from the
613 * queue. Has to be called with the appropriate locks.
615 * Returns false as soon as the entry in the fifo queue has not been timed out
616 * yet and true otherwise.
618 static bool batadv_nc_fwd_flush(struct batadv_priv
*bat_priv
,
619 struct batadv_nc_path
*nc_path
,
620 struct batadv_nc_packet
*nc_packet
)
622 unsigned long timeout
= bat_priv
->nc
.max_fwd_delay
;
624 lockdep_assert_held(&nc_path
->packet_list_lock
);
626 /* Packets are added to tail, so the remaining packets did not time
627 * out and we can stop processing the current queue
629 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
630 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
634 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
635 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
636 nc_packet
->skb
->len
+ ETH_HLEN
);
637 list_del(&nc_packet
->list
);
638 batadv_nc_send_packet(nc_packet
);
644 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
646 * @bat_priv: the bat priv with all the soft interface information
647 * @hash: to be processed hash table
648 * @process_fn: Function called to process given nc packet. Should return true
649 * to encourage this function to proceed with the next packet.
650 * Otherwise the rest of the current queue is skipped.
653 batadv_nc_process_nc_paths(struct batadv_priv
*bat_priv
,
654 struct batadv_hashtable
*hash
,
655 bool (*process_fn
)(struct batadv_priv
*,
656 struct batadv_nc_path
*,
657 struct batadv_nc_packet
*))
659 struct hlist_head
*head
;
660 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
661 struct batadv_nc_path
*nc_path
;
668 /* Loop hash table bins */
669 for (i
= 0; i
< hash
->size
; i
++) {
670 head
= &hash
->table
[i
];
672 /* Loop coding paths */
674 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
676 spin_lock_bh(&nc_path
->packet_list_lock
);
677 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
678 &nc_path
->packet_list
, list
) {
679 ret
= process_fn(bat_priv
, nc_path
, nc_packet
);
683 spin_unlock_bh(&nc_path
->packet_list_lock
);
690 * batadv_nc_worker - periodic task for house keeping related to network coding
691 * @work: kernel work struct
693 static void batadv_nc_worker(struct work_struct
*work
)
695 struct delayed_work
*delayed_work
;
696 struct batadv_priv_nc
*priv_nc
;
697 struct batadv_priv
*bat_priv
;
698 unsigned long timeout
;
700 delayed_work
= container_of(work
, struct delayed_work
, work
);
701 priv_nc
= container_of(delayed_work
, struct batadv_priv_nc
, work
);
702 bat_priv
= container_of(priv_nc
, struct batadv_priv
, nc
);
704 batadv_nc_purge_orig_hash(bat_priv
);
705 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
706 batadv_nc_to_purge_nc_path_coding
);
707 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
708 batadv_nc_to_purge_nc_path_decoding
);
710 timeout
= bat_priv
->nc
.max_fwd_delay
;
712 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_fwd_flush
, timeout
)) {
713 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
714 batadv_nc_fwd_flush
);
715 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
718 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_sniffed_purge
,
719 bat_priv
->nc
.max_buffer_time
)) {
720 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
721 batadv_nc_sniffed_purge
);
722 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
725 /* Schedule a new check */
726 batadv_nc_start_timer(bat_priv
);
730 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
732 * @bat_priv: the bat priv with all the soft interface information
733 * @orig_node: neighboring orig node which may be used as nc candidate
734 * @ogm_packet: incoming ogm packet also used for the checks
737 * 1) The OGM must have the most recent sequence number.
738 * 2) The TTL must be decremented by one and only one.
739 * 3) The OGM must be received from the first hop from orig_node.
740 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
742 static bool batadv_can_nc_with_orig(struct batadv_priv
*bat_priv
,
743 struct batadv_orig_node
*orig_node
,
744 struct batadv_ogm_packet
*ogm_packet
)
746 struct batadv_orig_ifinfo
*orig_ifinfo
;
750 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, BATADV_IF_DEFAULT
);
754 last_ttl
= orig_ifinfo
->last_ttl
;
755 last_real_seqno
= orig_ifinfo
->last_real_seqno
;
756 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
758 if (last_real_seqno
!= ntohl(ogm_packet
->seqno
))
760 if (last_ttl
!= ogm_packet
->ttl
+ 1)
762 if (!batadv_compare_eth(ogm_packet
->orig
, ogm_packet
->prev_sender
))
764 if (ogm_packet
->tq
< bat_priv
->nc
.min_tq
)
771 * batadv_nc_find_nc_node - search for an existing nc node and return it
772 * @orig_node: orig node originating the ogm packet
773 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
774 * (can be equal to orig_node)
775 * @in_coding: traverse incoming or outgoing network coding list
777 * Returns the nc_node if found, NULL otherwise.
779 static struct batadv_nc_node
780 *batadv_nc_find_nc_node(struct batadv_orig_node
*orig_node
,
781 struct batadv_orig_node
*orig_neigh_node
,
784 struct batadv_nc_node
*nc_node
, *nc_node_out
= NULL
;
785 struct list_head
*list
;
788 list
= &orig_neigh_node
->in_coding_list
;
790 list
= &orig_neigh_node
->out_coding_list
;
792 /* Traverse list of nc_nodes to orig_node */
794 list_for_each_entry_rcu(nc_node
, list
, list
) {
795 if (!batadv_compare_eth(nc_node
->addr
, orig_node
->orig
))
798 if (!atomic_inc_not_zero(&nc_node
->refcount
))
802 nc_node_out
= nc_node
;
811 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
813 * @bat_priv: the bat priv with all the soft interface information
814 * @orig_node: orig node originating the ogm packet
815 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
816 * (can be equal to orig_node)
817 * @in_coding: traverse incoming or outgoing network coding list
819 * Returns the nc_node if found or created, NULL in case of an error.
821 static struct batadv_nc_node
822 *batadv_nc_get_nc_node(struct batadv_priv
*bat_priv
,
823 struct batadv_orig_node
*orig_node
,
824 struct batadv_orig_node
*orig_neigh_node
,
827 struct batadv_nc_node
*nc_node
;
828 spinlock_t
*lock
; /* Used to lock list selected by "int in_coding" */
829 struct list_head
*list
;
831 /* Check if nc_node is already added */
832 nc_node
= batadv_nc_find_nc_node(orig_node
, orig_neigh_node
, in_coding
);
838 nc_node
= kzalloc(sizeof(*nc_node
), GFP_ATOMIC
);
842 if (!atomic_inc_not_zero(&orig_neigh_node
->refcount
))
845 /* Initialize nc_node */
846 INIT_LIST_HEAD(&nc_node
->list
);
847 ether_addr_copy(nc_node
->addr
, orig_node
->orig
);
848 nc_node
->orig_node
= orig_neigh_node
;
849 atomic_set(&nc_node
->refcount
, 2);
851 /* Select ingoing or outgoing coding node */
853 lock
= &orig_neigh_node
->in_coding_list_lock
;
854 list
= &orig_neigh_node
->in_coding_list
;
856 lock
= &orig_neigh_node
->out_coding_list_lock
;
857 list
= &orig_neigh_node
->out_coding_list
;
860 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_node %pM -> %pM\n",
861 nc_node
->addr
, nc_node
->orig_node
->orig
);
863 /* Add nc_node to orig_node */
865 list_add_tail_rcu(&nc_node
->list
, list
);
866 spin_unlock_bh(lock
);
876 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node
877 * structs (best called on incoming OGMs)
878 * @bat_priv: the bat priv with all the soft interface information
879 * @orig_node: orig node originating the ogm packet
880 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
881 * (can be equal to orig_node)
882 * @ogm_packet: incoming ogm packet
883 * @is_single_hop_neigh: orig_node is a single hop neighbor
885 void batadv_nc_update_nc_node(struct batadv_priv
*bat_priv
,
886 struct batadv_orig_node
*orig_node
,
887 struct batadv_orig_node
*orig_neigh_node
,
888 struct batadv_ogm_packet
*ogm_packet
,
889 int is_single_hop_neigh
)
891 struct batadv_nc_node
*in_nc_node
= NULL
;
892 struct batadv_nc_node
*out_nc_node
= NULL
;
894 /* Check if network coding is enabled */
895 if (!atomic_read(&bat_priv
->network_coding
))
898 /* check if orig node is network coding enabled */
899 if (!test_bit(BATADV_ORIG_CAPA_HAS_NC
, &orig_node
->capabilities
))
902 /* accept ogms from 'good' neighbors and single hop neighbors */
903 if (!batadv_can_nc_with_orig(bat_priv
, orig_node
, ogm_packet
) &&
904 !is_single_hop_neigh
)
907 /* Add orig_node as in_nc_node on hop */
908 in_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_node
,
909 orig_neigh_node
, true);
913 in_nc_node
->last_seen
= jiffies
;
915 /* Add hop as out_nc_node on orig_node */
916 out_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_neigh_node
,
921 out_nc_node
->last_seen
= jiffies
;
925 batadv_nc_node_free_ref(in_nc_node
);
927 batadv_nc_node_free_ref(out_nc_node
);
931 * batadv_nc_get_path - get existing nc_path or allocate a new one
932 * @bat_priv: the bat priv with all the soft interface information
933 * @hash: hash table containing the nc path
934 * @src: ethernet source address - first half of the nc path search key
935 * @dst: ethernet destination address - second half of the nc path search key
937 * Returns pointer to nc_path if the path was found or created, returns NULL
940 static struct batadv_nc_path
*batadv_nc_get_path(struct batadv_priv
*bat_priv
,
941 struct batadv_hashtable
*hash
,
946 struct batadv_nc_path
*nc_path
, nc_path_key
;
948 batadv_nc_hash_key_gen(&nc_path_key
, src
, dst
);
950 /* Search for existing nc_path */
951 nc_path
= batadv_nc_hash_find(hash
, (void *)&nc_path_key
);
954 /* Set timestamp to delay removal of nc_path */
955 nc_path
->last_valid
= jiffies
;
959 /* No existing nc_path was found; create a new */
960 nc_path
= kzalloc(sizeof(*nc_path
), GFP_ATOMIC
);
965 /* Initialize nc_path */
966 INIT_LIST_HEAD(&nc_path
->packet_list
);
967 spin_lock_init(&nc_path
->packet_list_lock
);
968 atomic_set(&nc_path
->refcount
, 2);
969 nc_path
->last_valid
= jiffies
;
970 ether_addr_copy(nc_path
->next_hop
, dst
);
971 ether_addr_copy(nc_path
->prev_hop
, src
);
973 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_path %pM -> %pM\n",
977 /* Add nc_path to hash table */
978 hash_added
= batadv_hash_add(hash
, batadv_nc_hash_compare
,
979 batadv_nc_hash_choose
, &nc_path_key
,
980 &nc_path
->hash_entry
);
982 if (hash_added
< 0) {
991 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
992 * selection of a receiver with slightly lower TQ than the other
993 * @tq: to be weighted tq value
995 static u8
batadv_nc_random_weight_tq(u8 tq
)
997 u8 rand_val
, rand_tq
;
999 get_random_bytes(&rand_val
, sizeof(rand_val
));
1001 /* randomize the estimated packet loss (max TQ - estimated TQ) */
1002 rand_tq
= rand_val
* (BATADV_TQ_MAX_VALUE
- tq
);
1004 /* normalize the randomized packet loss */
1005 rand_tq
/= BATADV_TQ_MAX_VALUE
;
1007 /* convert to (randomized) estimated tq again */
1008 return BATADV_TQ_MAX_VALUE
- rand_tq
;
1012 * batadv_nc_memxor - XOR destination with source
1013 * @dst: byte array to XOR into
1014 * @src: byte array to XOR from
1015 * @len: length of destination array
1017 static void batadv_nc_memxor(char *dst
, const char *src
, unsigned int len
)
1021 for (i
= 0; i
< len
; ++i
)
1026 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
1027 * into a coded_packet and send it
1028 * @bat_priv: the bat priv with all the soft interface information
1029 * @skb: data skb to forward
1030 * @ethhdr: pointer to the ethernet header inside the skb
1031 * @nc_packet: structure containing the packet to the skb can be coded with
1032 * @neigh_node: next hop to forward packet to
1034 * Returns true if both packets are consumed, false otherwise.
1036 static bool batadv_nc_code_packets(struct batadv_priv
*bat_priv
,
1037 struct sk_buff
*skb
,
1038 struct ethhdr
*ethhdr
,
1039 struct batadv_nc_packet
*nc_packet
,
1040 struct batadv_neigh_node
*neigh_node
)
1042 u8 tq_weighted_neigh
, tq_weighted_coding
, tq_tmp
;
1043 struct sk_buff
*skb_dest
, *skb_src
;
1044 struct batadv_unicast_packet
*packet1
;
1045 struct batadv_unicast_packet
*packet2
;
1046 struct batadv_coded_packet
*coded_packet
;
1047 struct batadv_neigh_node
*neigh_tmp
, *router_neigh
;
1048 struct batadv_neigh_node
*router_coding
= NULL
;
1049 struct batadv_neigh_ifinfo
*router_neigh_ifinfo
= NULL
;
1050 struct batadv_neigh_ifinfo
*router_coding_ifinfo
= NULL
;
1051 u8
*first_source
, *first_dest
, *second_source
, *second_dest
;
1052 __be32 packet_id1
, packet_id2
;
1056 int unicast_size
= sizeof(*packet1
);
1057 int coded_size
= sizeof(*coded_packet
);
1058 int header_add
= coded_size
- unicast_size
;
1060 /* TODO: do we need to consider the outgoing interface for
1063 router_neigh
= batadv_orig_router_get(neigh_node
->orig_node
,
1068 router_neigh_ifinfo
= batadv_neigh_ifinfo_get(router_neigh
,
1070 if (!router_neigh_ifinfo
)
1073 neigh_tmp
= nc_packet
->neigh_node
;
1074 router_coding
= batadv_orig_router_get(neigh_tmp
->orig_node
,
1079 router_coding_ifinfo
= batadv_neigh_ifinfo_get(router_coding
,
1081 if (!router_coding_ifinfo
)
1084 tq_tmp
= router_neigh_ifinfo
->bat_iv
.tq_avg
;
1085 tq_weighted_neigh
= batadv_nc_random_weight_tq(tq_tmp
);
1086 tq_tmp
= router_coding_ifinfo
->bat_iv
.tq_avg
;
1087 tq_weighted_coding
= batadv_nc_random_weight_tq(tq_tmp
);
1089 /* Select one destination for the MAC-header dst-field based on
1090 * weighted TQ-values.
1092 if (tq_weighted_neigh
>= tq_weighted_coding
) {
1093 /* Destination from nc_packet is selected for MAC-header */
1094 first_dest
= nc_packet
->nc_path
->next_hop
;
1095 first_source
= nc_packet
->nc_path
->prev_hop
;
1096 second_dest
= neigh_node
->addr
;
1097 second_source
= ethhdr
->h_source
;
1098 packet1
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1099 packet2
= (struct batadv_unicast_packet
*)skb
->data
;
1100 packet_id1
= nc_packet
->packet_id
;
1101 packet_id2
= batadv_skb_crc32(skb
,
1102 skb
->data
+ sizeof(*packet2
));
1104 /* Destination for skb is selected for MAC-header */
1105 first_dest
= neigh_node
->addr
;
1106 first_source
= ethhdr
->h_source
;
1107 second_dest
= nc_packet
->nc_path
->next_hop
;
1108 second_source
= nc_packet
->nc_path
->prev_hop
;
1109 packet1
= (struct batadv_unicast_packet
*)skb
->data
;
1110 packet2
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1111 packet_id1
= batadv_skb_crc32(skb
,
1112 skb
->data
+ sizeof(*packet1
));
1113 packet_id2
= nc_packet
->packet_id
;
1116 /* Instead of zero padding the smallest data buffer, we
1117 * code into the largest.
1119 if (skb
->len
<= nc_packet
->skb
->len
) {
1120 skb_dest
= nc_packet
->skb
;
1124 skb_src
= nc_packet
->skb
;
1127 /* coding_len is used when decoding the packet shorter packet */
1128 coding_len
= skb_src
->len
- unicast_size
;
1130 if (skb_linearize(skb_dest
) < 0 || skb_linearize(skb_src
) < 0)
1133 skb_push(skb_dest
, header_add
);
1135 coded_packet
= (struct batadv_coded_packet
*)skb_dest
->data
;
1136 skb_reset_mac_header(skb_dest
);
1138 coded_packet
->packet_type
= BATADV_CODED
;
1139 coded_packet
->version
= BATADV_COMPAT_VERSION
;
1140 coded_packet
->ttl
= packet1
->ttl
;
1142 /* Info about first unicast packet */
1143 ether_addr_copy(coded_packet
->first_source
, first_source
);
1144 ether_addr_copy(coded_packet
->first_orig_dest
, packet1
->dest
);
1145 coded_packet
->first_crc
= packet_id1
;
1146 coded_packet
->first_ttvn
= packet1
->ttvn
;
1148 /* Info about second unicast packet */
1149 ether_addr_copy(coded_packet
->second_dest
, second_dest
);
1150 ether_addr_copy(coded_packet
->second_source
, second_source
);
1151 ether_addr_copy(coded_packet
->second_orig_dest
, packet2
->dest
);
1152 coded_packet
->second_crc
= packet_id2
;
1153 coded_packet
->second_ttl
= packet2
->ttl
;
1154 coded_packet
->second_ttvn
= packet2
->ttvn
;
1155 coded_packet
->coded_len
= htons(coding_len
);
1157 /* This is where the magic happens: Code skb_src into skb_dest */
1158 batadv_nc_memxor(skb_dest
->data
+ coded_size
,
1159 skb_src
->data
+ unicast_size
, coding_len
);
1161 /* Update counters accordingly */
1162 if (BATADV_SKB_CB(skb_src
)->decoded
&&
1163 BATADV_SKB_CB(skb_dest
)->decoded
) {
1164 /* Both packets are recoded */
1165 count
= skb_src
->len
+ ETH_HLEN
;
1166 count
+= skb_dest
->len
+ ETH_HLEN
;
1167 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE
, 2);
1168 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
, count
);
1169 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1170 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1171 /* Both packets are newly coded */
1172 count
= skb_src
->len
+ ETH_HLEN
;
1173 count
+= skb_dest
->len
+ ETH_HLEN
;
1174 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE
, 2);
1175 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
, count
);
1176 } else if (BATADV_SKB_CB(skb_src
)->decoded
&&
1177 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1178 /* skb_src recoded and skb_dest is newly coded */
1179 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1180 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1181 skb_src
->len
+ ETH_HLEN
);
1182 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1183 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1184 skb_dest
->len
+ ETH_HLEN
);
1185 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1186 BATADV_SKB_CB(skb_dest
)->decoded
) {
1187 /* skb_src is newly coded and skb_dest is recoded */
1188 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1189 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1190 skb_src
->len
+ ETH_HLEN
);
1191 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1192 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1193 skb_dest
->len
+ ETH_HLEN
);
1196 /* skb_src is now coded into skb_dest, so free it */
1199 /* avoid duplicate free of skb from nc_packet */
1200 nc_packet
->skb
= NULL
;
1201 batadv_nc_packet_free(nc_packet
);
1203 /* Send the coded packet and return true */
1204 batadv_send_skb_packet(skb_dest
, neigh_node
->if_incoming
, first_dest
);
1208 batadv_neigh_node_free_ref(router_neigh
);
1210 batadv_neigh_node_free_ref(router_coding
);
1211 if (router_neigh_ifinfo
)
1212 batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo
);
1213 if (router_coding_ifinfo
)
1214 batadv_neigh_ifinfo_free_ref(router_coding_ifinfo
);
1219 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1220 * @skb: data skb to forward
1221 * @dst: destination mac address of the other skb to code with
1222 * @src: source mac address of skb
1224 * Whenever we network code a packet we have to check whether we received it in
1225 * a network coded form. If so, we may not be able to use it for coding because
1226 * some neighbors may also have received (overheard) the packet in the network
1227 * coded form without being able to decode it. It is hard to know which of the
1228 * neighboring nodes was able to decode the packet, therefore we can only
1229 * re-code the packet if the source of the previous encoded packet is involved.
1230 * Since the source encoded the packet we can be certain it has all necessary
1231 * decode information.
1233 * Returns true if coding of a decoded packet is allowed.
1235 static bool batadv_nc_skb_coding_possible(struct sk_buff
*skb
, u8
*dst
, u8
*src
)
1237 if (BATADV_SKB_CB(skb
)->decoded
&& !batadv_compare_eth(dst
, src
))
1243 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1244 * out_nc_node to retrieve a buffered packet that can be used for coding.
1245 * @bat_priv: the bat priv with all the soft interface information
1246 * @in_nc_node: pointer to skb next hop's neighbor nc node
1247 * @out_nc_node: pointer to skb source's neighbor nc node
1248 * @skb: data skb to forward
1249 * @eth_dst: next hop mac address of skb
1251 * Returns true if coding of a decoded skb is allowed.
1253 static struct batadv_nc_packet
*
1254 batadv_nc_path_search(struct batadv_priv
*bat_priv
,
1255 struct batadv_nc_node
*in_nc_node
,
1256 struct batadv_nc_node
*out_nc_node
,
1257 struct sk_buff
*skb
,
1260 struct batadv_nc_path
*nc_path
, nc_path_key
;
1261 struct batadv_nc_packet
*nc_packet_out
= NULL
;
1262 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
1263 struct batadv_hashtable
*hash
= bat_priv
->nc
.coding_hash
;
1269 /* Create almost path key */
1270 batadv_nc_hash_key_gen(&nc_path_key
, in_nc_node
->addr
,
1272 idx
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1274 /* Check for coding opportunities in this nc_path */
1276 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[idx
], hash_entry
) {
1277 if (!batadv_compare_eth(nc_path
->prev_hop
, in_nc_node
->addr
))
1280 if (!batadv_compare_eth(nc_path
->next_hop
, out_nc_node
->addr
))
1283 spin_lock_bh(&nc_path
->packet_list_lock
);
1284 if (list_empty(&nc_path
->packet_list
)) {
1285 spin_unlock_bh(&nc_path
->packet_list_lock
);
1289 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
1290 &nc_path
->packet_list
, list
) {
1291 if (!batadv_nc_skb_coding_possible(nc_packet
->skb
,
1296 /* Coding opportunity is found! */
1297 list_del(&nc_packet
->list
);
1298 nc_packet_out
= nc_packet
;
1302 spin_unlock_bh(&nc_path
->packet_list_lock
);
1307 return nc_packet_out
;
1311 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1312 * skb's sender (may be equal to the originator).
1313 * @bat_priv: the bat priv with all the soft interface information
1314 * @skb: data skb to forward
1315 * @eth_dst: next hop mac address of skb
1316 * @eth_src: source mac address of skb
1317 * @in_nc_node: pointer to skb next hop's neighbor nc node
1319 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1321 static struct batadv_nc_packet
*
1322 batadv_nc_skb_src_search(struct batadv_priv
*bat_priv
,
1323 struct sk_buff
*skb
,
1326 struct batadv_nc_node
*in_nc_node
)
1328 struct batadv_orig_node
*orig_node
;
1329 struct batadv_nc_node
*out_nc_node
;
1330 struct batadv_nc_packet
*nc_packet
= NULL
;
1332 orig_node
= batadv_orig_hash_find(bat_priv
, eth_src
);
1337 list_for_each_entry_rcu(out_nc_node
,
1338 &orig_node
->out_coding_list
, list
) {
1339 /* Check if the skb is decoded and if recoding is possible */
1340 if (!batadv_nc_skb_coding_possible(skb
,
1341 out_nc_node
->addr
, eth_src
))
1344 /* Search for an opportunity in this nc_path */
1345 nc_packet
= batadv_nc_path_search(bat_priv
, in_nc_node
,
1346 out_nc_node
, skb
, eth_dst
);
1352 batadv_orig_node_free_ref(orig_node
);
1357 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1358 * unicast skb before it is stored for use in later decoding
1359 * @bat_priv: the bat priv with all the soft interface information
1360 * @skb: data skb to store
1361 * @eth_dst_new: new destination mac address of skb
1363 static void batadv_nc_skb_store_before_coding(struct batadv_priv
*bat_priv
,
1364 struct sk_buff
*skb
,
1367 struct ethhdr
*ethhdr
;
1369 /* Copy skb header to change the mac header */
1370 skb
= pskb_copy_for_clone(skb
, GFP_ATOMIC
);
1374 /* Set the mac header as if we actually sent the packet uncoded */
1375 ethhdr
= eth_hdr(skb
);
1376 ether_addr_copy(ethhdr
->h_source
, ethhdr
->h_dest
);
1377 ether_addr_copy(ethhdr
->h_dest
, eth_dst_new
);
1379 /* Set data pointer to MAC header to mimic packets from our tx path */
1380 skb_push(skb
, ETH_HLEN
);
1382 /* Add the packet to the decoding packet pool */
1383 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1385 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1392 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1393 * @skb: data skb to forward
1394 * @neigh_node: next hop to forward packet to
1395 * @ethhdr: pointer to the ethernet header inside the skb
1397 * Loops through list of neighboring nodes the next hop has a good connection to
1398 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1399 * next hop that potentially sent a packet which our next hop also received
1400 * (overheard) and has stored for later decoding.
1402 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1404 static bool batadv_nc_skb_dst_search(struct sk_buff
*skb
,
1405 struct batadv_neigh_node
*neigh_node
,
1406 struct ethhdr
*ethhdr
)
1408 struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1409 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1410 struct batadv_orig_node
*orig_node
= neigh_node
->orig_node
;
1411 struct batadv_nc_node
*nc_node
;
1412 struct batadv_nc_packet
*nc_packet
= NULL
;
1415 list_for_each_entry_rcu(nc_node
, &orig_node
->in_coding_list
, list
) {
1416 /* Search for coding opportunity with this in_nc_node */
1417 nc_packet
= batadv_nc_skb_src_search(bat_priv
, skb
,
1419 ethhdr
->h_source
, nc_node
);
1421 /* Opportunity was found, so stop searching */
1430 /* Save packets for later decoding */
1431 batadv_nc_skb_store_before_coding(bat_priv
, skb
,
1433 batadv_nc_skb_store_before_coding(bat_priv
, nc_packet
->skb
,
1434 nc_packet
->neigh_node
->addr
);
1436 /* Code and send packets */
1437 if (batadv_nc_code_packets(bat_priv
, skb
, ethhdr
, nc_packet
,
1441 /* out of mem ? Coding failed - we have to free the buffered packet
1442 * to avoid memleaks. The skb passed as argument will be dealt with
1443 * by the calling function.
1445 batadv_nc_send_packet(nc_packet
);
1450 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1451 * @skb: skb to add to path
1452 * @nc_path: path to add skb to
1453 * @neigh_node: next hop to forward packet to
1454 * @packet_id: checksum to identify packet
1456 * Returns true if the packet was buffered or false in case of an error.
1458 static bool batadv_nc_skb_add_to_path(struct sk_buff
*skb
,
1459 struct batadv_nc_path
*nc_path
,
1460 struct batadv_neigh_node
*neigh_node
,
1463 struct batadv_nc_packet
*nc_packet
;
1465 nc_packet
= kzalloc(sizeof(*nc_packet
), GFP_ATOMIC
);
1469 /* Initialize nc_packet */
1470 nc_packet
->timestamp
= jiffies
;
1471 nc_packet
->packet_id
= packet_id
;
1472 nc_packet
->skb
= skb
;
1473 nc_packet
->neigh_node
= neigh_node
;
1474 nc_packet
->nc_path
= nc_path
;
1476 /* Add coding packet to list */
1477 spin_lock_bh(&nc_path
->packet_list_lock
);
1478 list_add_tail(&nc_packet
->list
, &nc_path
->packet_list
);
1479 spin_unlock_bh(&nc_path
->packet_list_lock
);
1485 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1487 * @skb: data skb to forward
1488 * @neigh_node: next hop to forward packet to
1490 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1492 bool batadv_nc_skb_forward(struct sk_buff
*skb
,
1493 struct batadv_neigh_node
*neigh_node
)
1495 const struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1496 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1497 struct batadv_unicast_packet
*packet
;
1498 struct batadv_nc_path
*nc_path
;
1499 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1503 /* Check if network coding is enabled */
1504 if (!atomic_read(&bat_priv
->network_coding
))
1507 /* We only handle unicast packets */
1508 payload
= skb_network_header(skb
);
1509 packet
= (struct batadv_unicast_packet
*)payload
;
1510 if (packet
->packet_type
!= BATADV_UNICAST
)
1513 /* Try to find a coding opportunity and send the skb if one is found */
1514 if (batadv_nc_skb_dst_search(skb
, neigh_node
, ethhdr
))
1517 /* Find or create a nc_path for this src-dst pair */
1518 nc_path
= batadv_nc_get_path(bat_priv
,
1519 bat_priv
->nc
.coding_hash
,
1526 /* Add skb to nc_path */
1527 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1528 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, neigh_node
, packet_id
))
1531 /* Packet is consumed */
1535 batadv_nc_path_free_ref(nc_path
);
1537 /* Packet is not consumed */
1542 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1543 * when decoding coded packets
1544 * @bat_priv: the bat priv with all the soft interface information
1545 * @skb: data skb to store
1547 void batadv_nc_skb_store_for_decoding(struct batadv_priv
*bat_priv
,
1548 struct sk_buff
*skb
)
1550 struct batadv_unicast_packet
*packet
;
1551 struct batadv_nc_path
*nc_path
;
1552 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1556 /* Check if network coding is enabled */
1557 if (!atomic_read(&bat_priv
->network_coding
))
1560 /* Check for supported packet type */
1561 payload
= skb_network_header(skb
);
1562 packet
= (struct batadv_unicast_packet
*)payload
;
1563 if (packet
->packet_type
!= BATADV_UNICAST
)
1566 /* Find existing nc_path or create a new */
1567 nc_path
= batadv_nc_get_path(bat_priv
,
1568 bat_priv
->nc
.decoding_hash
,
1575 /* Clone skb and adjust skb->data to point at batman header */
1576 skb
= skb_clone(skb
, GFP_ATOMIC
);
1580 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
1583 if (unlikely(!skb_pull_rcsum(skb
, ETH_HLEN
)))
1586 /* Add skb to nc_path */
1587 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1588 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, NULL
, packet_id
))
1591 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_BUFFER
);
1597 batadv_nc_path_free_ref(nc_path
);
1603 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1604 * should be saved in the decoding buffer and, if so, store it there
1605 * @bat_priv: the bat priv with all the soft interface information
1606 * @skb: unicast skb to store
1608 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv
*bat_priv
,
1609 struct sk_buff
*skb
)
1611 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1613 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1616 /* Set data pointer to MAC header to mimic packets from our tx path */
1617 skb_push(skb
, ETH_HLEN
);
1619 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1623 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1625 * @bat_priv: the bat priv with all the soft interface information
1626 * @skb: unicast skb to decode
1627 * @nc_packet: decode data needed to decode the skb
1629 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1630 * in case of an error.
1632 static struct batadv_unicast_packet
*
1633 batadv_nc_skb_decode_packet(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1634 struct batadv_nc_packet
*nc_packet
)
1636 const int h_size
= sizeof(struct batadv_unicast_packet
);
1637 const int h_diff
= sizeof(struct batadv_coded_packet
) - h_size
;
1638 struct batadv_unicast_packet
*unicast_packet
;
1639 struct batadv_coded_packet coded_packet_tmp
;
1640 struct ethhdr
*ethhdr
, ethhdr_tmp
;
1641 u8
*orig_dest
, ttl
, ttvn
;
1642 unsigned int coding_len
;
1645 /* Save headers temporarily */
1646 memcpy(&coded_packet_tmp
, skb
->data
, sizeof(coded_packet_tmp
));
1647 memcpy(ðhdr_tmp
, skb_mac_header(skb
), sizeof(ethhdr_tmp
));
1649 if (skb_cow(skb
, 0) < 0)
1652 if (unlikely(!skb_pull_rcsum(skb
, h_diff
)))
1655 /* Data points to batman header, so set mac header 14 bytes before
1656 * and network to data
1658 skb_set_mac_header(skb
, -ETH_HLEN
);
1659 skb_reset_network_header(skb
);
1661 /* Reconstruct original mac header */
1662 ethhdr
= eth_hdr(skb
);
1663 *ethhdr
= ethhdr_tmp
;
1665 /* Select the correct unicast header information based on the location
1666 * of our mac address in the coded_packet header
1668 if (batadv_is_my_mac(bat_priv
, coded_packet_tmp
.second_dest
)) {
1669 /* If we are the second destination the packet was overheard,
1670 * so the Ethernet address must be copied to h_dest and
1671 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1673 ether_addr_copy(ethhdr
->h_dest
, coded_packet_tmp
.second_dest
);
1674 skb
->pkt_type
= PACKET_HOST
;
1676 orig_dest
= coded_packet_tmp
.second_orig_dest
;
1677 ttl
= coded_packet_tmp
.second_ttl
;
1678 ttvn
= coded_packet_tmp
.second_ttvn
;
1680 orig_dest
= coded_packet_tmp
.first_orig_dest
;
1681 ttl
= coded_packet_tmp
.ttl
;
1682 ttvn
= coded_packet_tmp
.first_ttvn
;
1685 coding_len
= ntohs(coded_packet_tmp
.coded_len
);
1687 if (coding_len
> skb
->len
)
1690 /* Here the magic is reversed:
1691 * extract the missing packet from the received coded packet
1693 batadv_nc_memxor(skb
->data
+ h_size
,
1694 nc_packet
->skb
->data
+ h_size
,
1697 /* Resize decoded skb if decoded with larger packet */
1698 if (nc_packet
->skb
->len
> coding_len
+ h_size
) {
1699 err
= pskb_trim_rcsum(skb
, coding_len
+ h_size
);
1704 /* Create decoded unicast packet */
1705 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1706 unicast_packet
->packet_type
= BATADV_UNICAST
;
1707 unicast_packet
->version
= BATADV_COMPAT_VERSION
;
1708 unicast_packet
->ttl
= ttl
;
1709 ether_addr_copy(unicast_packet
->dest
, orig_dest
);
1710 unicast_packet
->ttvn
= ttvn
;
1712 batadv_nc_packet_free(nc_packet
);
1713 return unicast_packet
;
1717 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1718 * find the data needed to decode the coded packet
1719 * @bat_priv: the bat priv with all the soft interface information
1720 * @ethhdr: pointer to the ethernet header inside the coded packet
1721 * @coded: coded packet we try to find decode data for
1723 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1725 static struct batadv_nc_packet
*
1726 batadv_nc_find_decoding_packet(struct batadv_priv
*bat_priv
,
1727 struct ethhdr
*ethhdr
,
1728 struct batadv_coded_packet
*coded
)
1730 struct batadv_hashtable
*hash
= bat_priv
->nc
.decoding_hash
;
1731 struct batadv_nc_packet
*tmp_nc_packet
, *nc_packet
= NULL
;
1732 struct batadv_nc_path
*nc_path
, nc_path_key
;
1740 /* Select the correct packet id based on the location of our mac-addr */
1741 dest
= ethhdr
->h_source
;
1742 if (!batadv_is_my_mac(bat_priv
, coded
->second_dest
)) {
1743 source
= coded
->second_source
;
1744 packet_id
= coded
->second_crc
;
1746 source
= coded
->first_source
;
1747 packet_id
= coded
->first_crc
;
1750 batadv_nc_hash_key_gen(&nc_path_key
, source
, dest
);
1751 index
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1753 /* Search for matching coding path */
1755 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[index
], hash_entry
) {
1756 /* Find matching nc_packet */
1757 spin_lock_bh(&nc_path
->packet_list_lock
);
1758 list_for_each_entry(tmp_nc_packet
,
1759 &nc_path
->packet_list
, list
) {
1760 if (packet_id
== tmp_nc_packet
->packet_id
) {
1761 list_del(&tmp_nc_packet
->list
);
1763 nc_packet
= tmp_nc_packet
;
1767 spin_unlock_bh(&nc_path
->packet_list_lock
);
1775 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
1776 "No decoding packet found for %u\n", packet_id
);
1782 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1783 * resulting unicast packet
1784 * @skb: incoming coded packet
1785 * @recv_if: pointer to interface this packet was received on
1787 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
1788 struct batadv_hard_iface
*recv_if
)
1790 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1791 struct batadv_unicast_packet
*unicast_packet
;
1792 struct batadv_coded_packet
*coded_packet
;
1793 struct batadv_nc_packet
*nc_packet
;
1794 struct ethhdr
*ethhdr
;
1795 int hdr_size
= sizeof(*coded_packet
);
1797 /* Check if network coding is enabled */
1798 if (!atomic_read(&bat_priv
->network_coding
))
1801 /* Make sure we can access (and remove) header */
1802 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1805 coded_packet
= (struct batadv_coded_packet
*)skb
->data
;
1806 ethhdr
= eth_hdr(skb
);
1808 /* Verify frame is destined for us */
1809 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
) &&
1810 !batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1813 /* Update stat counter */
1814 if (batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1815 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_SNIFFED
);
1817 nc_packet
= batadv_nc_find_decoding_packet(bat_priv
, ethhdr
,
1820 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1824 /* Make skb's linear, because decoding accesses the entire buffer */
1825 if (skb_linearize(skb
) < 0)
1826 goto free_nc_packet
;
1828 if (skb_linearize(nc_packet
->skb
) < 0)
1829 goto free_nc_packet
;
1831 /* Decode the packet */
1832 unicast_packet
= batadv_nc_skb_decode_packet(bat_priv
, skb
, nc_packet
);
1833 if (!unicast_packet
) {
1834 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1835 goto free_nc_packet
;
1838 /* Mark packet as decoded to do correct recoding when forwarding */
1839 BATADV_SKB_CB(skb
)->decoded
= true;
1840 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE
);
1841 batadv_add_counter(bat_priv
, BATADV_CNT_NC_DECODE_BYTES
,
1842 skb
->len
+ ETH_HLEN
);
1843 return batadv_recv_unicast_packet(skb
, recv_if
);
1846 batadv_nc_packet_free(nc_packet
);
1851 * batadv_nc_mesh_free - clean up network coding memory
1852 * @bat_priv: the bat priv with all the soft interface information
1854 void batadv_nc_mesh_free(struct batadv_priv
*bat_priv
)
1856 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1857 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1858 cancel_delayed_work_sync(&bat_priv
->nc
.work
);
1860 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
, NULL
);
1861 batadv_hash_destroy(bat_priv
->nc
.coding_hash
);
1862 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
, NULL
);
1863 batadv_hash_destroy(bat_priv
->nc
.decoding_hash
);
1867 * batadv_nc_nodes_seq_print_text - print the nc node information
1868 * @seq: seq file to print on
1871 int batadv_nc_nodes_seq_print_text(struct seq_file
*seq
, void *offset
)
1873 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1874 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1875 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1876 struct batadv_hard_iface
*primary_if
;
1877 struct hlist_head
*head
;
1878 struct batadv_orig_node
*orig_node
;
1879 struct batadv_nc_node
*nc_node
;
1882 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1886 /* Traverse list of originators */
1887 for (i
= 0; i
< hash
->size
; i
++) {
1888 head
= &hash
->table
[i
];
1890 /* For each orig_node in this bin */
1892 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1893 /* no need to print the orig node if it does not have
1894 * network coding neighbors
1896 if (list_empty(&orig_node
->in_coding_list
) &&
1897 list_empty(&orig_node
->out_coding_list
))
1900 seq_printf(seq
, "Node: %pM\n", orig_node
->orig
);
1902 seq_puts(seq
, " Ingoing: ");
1903 /* For each in_nc_node to this orig_node */
1904 list_for_each_entry_rcu(nc_node
,
1905 &orig_node
->in_coding_list
,
1907 seq_printf(seq
, "%pM ",
1909 seq_puts(seq
, "\n");
1911 seq_puts(seq
, " Outgoing: ");
1912 /* For out_nc_node to this orig_node */
1913 list_for_each_entry_rcu(nc_node
,
1914 &orig_node
->out_coding_list
,
1916 seq_printf(seq
, "%pM ",
1918 seq_puts(seq
, "\n\n");
1925 batadv_hardif_free_ref(primary_if
);
1930 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1931 * @bat_priv: the bat priv with all the soft interface information
1933 int batadv_nc_init_debugfs(struct batadv_priv
*bat_priv
)
1935 struct dentry
*nc_dir
, *file
;
1937 nc_dir
= debugfs_create_dir("nc", bat_priv
->debug_dir
);
1941 file
= debugfs_create_u8("min_tq", S_IRUGO
| S_IWUSR
, nc_dir
,
1942 &bat_priv
->nc
.min_tq
);
1946 file
= debugfs_create_u32("max_fwd_delay", S_IRUGO
| S_IWUSR
, nc_dir
,
1947 &bat_priv
->nc
.max_fwd_delay
);
1951 file
= debugfs_create_u32("max_buffer_time", S_IRUGO
| S_IWUSR
, nc_dir
,
1952 &bat_priv
->nc
.max_buffer_time
);