1 /* Copyright (C) 2012-2013 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 <linux/debugfs.h>
22 #include "network-coding.h"
24 #include "originator.h"
25 #include "hard-interface.h"
28 static struct lock_class_key batadv_nc_coding_hash_lock_class_key
;
29 static struct lock_class_key batadv_nc_decoding_hash_lock_class_key
;
31 static void batadv_nc_worker(struct work_struct
*work
);
32 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
33 struct batadv_hard_iface
*recv_if
);
36 * batadv_nc_init - one-time initialization for network coding
38 int __init
batadv_nc_init(void)
42 /* Register our packet type */
43 ret
= batadv_recv_handler_register(BATADV_CODED
,
44 batadv_nc_recv_coded_packet
);
50 * batadv_nc_start_timer - initialise the nc periodic worker
51 * @bat_priv: the bat priv with all the soft interface information
53 static void batadv_nc_start_timer(struct batadv_priv
*bat_priv
)
55 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->nc
.work
,
56 msecs_to_jiffies(10));
60 * batadv_nc_tvlv_container_update - update the network coding tvlv container
61 * after network coding setting change
62 * @bat_priv: the bat priv with all the soft interface information
64 static void batadv_nc_tvlv_container_update(struct batadv_priv
*bat_priv
)
68 nc_mode
= atomic_read(&bat_priv
->network_coding
);
72 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
75 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_NC
, 1,
82 * batadv_nc_status_update - update the network coding tvlv container after
83 * network coding setting change
84 * @net_dev: the soft interface net device
86 void batadv_nc_status_update(struct net_device
*net_dev
)
88 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
89 batadv_nc_tvlv_container_update(bat_priv
);
93 * batadv_nc_tvlv_ogm_handler_v1 - process incoming nc tvlv container
94 * @bat_priv: the bat priv with all the soft interface information
95 * @orig: the orig_node of the ogm
96 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
97 * @tvlv_value: tvlv buffer containing the gateway data
98 * @tvlv_value_len: tvlv buffer length
100 static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
101 struct batadv_orig_node
*orig
,
104 uint16_t tvlv_value_len
)
106 if (flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
)
107 orig
->capabilities
&= ~BATADV_ORIG_CAPA_HAS_NC
;
109 orig
->capabilities
|= BATADV_ORIG_CAPA_HAS_NC
;
113 * batadv_nc_mesh_init - initialise coding hash table and start house keeping
114 * @bat_priv: the bat priv with all the soft interface information
116 int batadv_nc_mesh_init(struct batadv_priv
*bat_priv
)
118 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
119 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
121 if (bat_priv
->nc
.coding_hash
|| bat_priv
->nc
.decoding_hash
)
124 bat_priv
->nc
.coding_hash
= batadv_hash_new(128);
125 if (!bat_priv
->nc
.coding_hash
)
128 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
129 &batadv_nc_coding_hash_lock_class_key
);
131 bat_priv
->nc
.decoding_hash
= batadv_hash_new(128);
132 if (!bat_priv
->nc
.decoding_hash
)
135 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
136 &batadv_nc_decoding_hash_lock_class_key
);
138 INIT_DELAYED_WORK(&bat_priv
->nc
.work
, batadv_nc_worker
);
139 batadv_nc_start_timer(bat_priv
);
141 batadv_tvlv_handler_register(bat_priv
, batadv_nc_tvlv_ogm_handler_v1
,
142 NULL
, BATADV_TVLV_NC
, 1,
143 BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
144 batadv_nc_tvlv_container_update(bat_priv
);
152 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
153 * @bat_priv: the bat priv with all the soft interface information
155 void batadv_nc_init_bat_priv(struct batadv_priv
*bat_priv
)
157 atomic_set(&bat_priv
->network_coding
, 1);
158 bat_priv
->nc
.min_tq
= 200;
159 bat_priv
->nc
.max_fwd_delay
= 10;
160 bat_priv
->nc
.max_buffer_time
= 200;
164 * batadv_nc_init_orig - initialise the nc fields of an orig_node
165 * @orig_node: the orig_node which is going to be initialised
167 void batadv_nc_init_orig(struct batadv_orig_node
*orig_node
)
169 INIT_LIST_HEAD(&orig_node
->in_coding_list
);
170 INIT_LIST_HEAD(&orig_node
->out_coding_list
);
171 spin_lock_init(&orig_node
->in_coding_list_lock
);
172 spin_lock_init(&orig_node
->out_coding_list_lock
);
176 * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
177 * its refcount on the orig_node
178 * @rcu: rcu pointer of the nc node
180 static void batadv_nc_node_free_rcu(struct rcu_head
*rcu
)
182 struct batadv_nc_node
*nc_node
;
184 nc_node
= container_of(rcu
, struct batadv_nc_node
, rcu
);
185 batadv_orig_node_free_ref(nc_node
->orig_node
);
190 * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
192 * @nc_node: the nc node to free
194 static void batadv_nc_node_free_ref(struct batadv_nc_node
*nc_node
)
196 if (atomic_dec_and_test(&nc_node
->refcount
))
197 call_rcu(&nc_node
->rcu
, batadv_nc_node_free_rcu
);
201 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
203 * @nc_path: the nc node to free
205 static void batadv_nc_path_free_ref(struct batadv_nc_path
*nc_path
)
207 if (atomic_dec_and_test(&nc_path
->refcount
))
208 kfree_rcu(nc_path
, rcu
);
212 * batadv_nc_packet_free - frees nc packet
213 * @nc_packet: the nc packet to free
215 static void batadv_nc_packet_free(struct batadv_nc_packet
*nc_packet
)
218 kfree_skb(nc_packet
->skb
);
220 batadv_nc_path_free_ref(nc_packet
->nc_path
);
225 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
226 * @bat_priv: the bat priv with all the soft interface information
227 * @nc_node: the nc node to check
229 * Returns true if the entry has to be purged now, false otherwise
231 static bool batadv_nc_to_purge_nc_node(struct batadv_priv
*bat_priv
,
232 struct batadv_nc_node
*nc_node
)
234 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
237 return batadv_has_timed_out(nc_node
->last_seen
, BATADV_NC_NODE_TIMEOUT
);
241 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
242 * @bat_priv: the bat priv with all the soft interface information
243 * @nc_path: the nc path to check
245 * Returns true if the entry has to be purged now, false otherwise
247 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv
*bat_priv
,
248 struct batadv_nc_path
*nc_path
)
250 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
253 /* purge the path when no packets has been added for 10 times the
256 return batadv_has_timed_out(nc_path
->last_valid
,
257 bat_priv
->nc
.max_fwd_delay
* 10);
261 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
262 * @bat_priv: the bat priv with all the soft interface information
263 * @nc_path: the nc path to check
265 * Returns true if the entry has to be purged now, false otherwise
267 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv
*bat_priv
,
268 struct batadv_nc_path
*nc_path
)
270 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
273 /* purge the path when no packets has been added for 10 times the
276 return batadv_has_timed_out(nc_path
->last_valid
,
277 bat_priv
->nc
.max_buffer_time
*10);
281 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
283 * @bat_priv: the bat priv with all the soft interface information
284 * @list: list of nc nodes
285 * @lock: nc node list lock
286 * @to_purge: function in charge to decide whether an entry has to be purged or
287 * not. This function takes the nc node as argument and has to return
288 * a boolean value: true if the entry has to be deleted, false
292 batadv_nc_purge_orig_nc_nodes(struct batadv_priv
*bat_priv
,
293 struct list_head
*list
,
295 bool (*to_purge
)(struct batadv_priv
*,
296 struct batadv_nc_node
*))
298 struct batadv_nc_node
*nc_node
, *nc_node_tmp
;
300 /* For each nc_node in list */
302 list_for_each_entry_safe(nc_node
, nc_node_tmp
, list
, list
) {
303 /* if an helper function has been passed as parameter,
304 * ask it if the entry has to be purged or not
306 if (to_purge
&& !to_purge(bat_priv
, nc_node
))
309 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
310 "Removing nc_node %pM -> %pM\n",
311 nc_node
->addr
, nc_node
->orig_node
->orig
);
312 list_del_rcu(&nc_node
->list
);
313 batadv_nc_node_free_ref(nc_node
);
315 spin_unlock_bh(lock
);
319 * batadv_nc_purge_orig - purges all nc node data attached of the given
321 * @bat_priv: the bat priv with all the soft interface information
322 * @orig_node: orig_node with the nc node entries to be purged
323 * @to_purge: function in charge to decide whether an entry has to be purged or
324 * not. This function takes the nc node as argument and has to return
325 * a boolean value: true is the entry has to be deleted, false
328 void batadv_nc_purge_orig(struct batadv_priv
*bat_priv
,
329 struct batadv_orig_node
*orig_node
,
330 bool (*to_purge
)(struct batadv_priv
*,
331 struct batadv_nc_node
*))
333 /* Check ingoing nc_node's of this orig_node */
334 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->in_coding_list
,
335 &orig_node
->in_coding_list_lock
,
338 /* Check outgoing nc_node's of this orig_node */
339 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->out_coding_list
,
340 &orig_node
->out_coding_list_lock
,
345 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
346 * have timed out nc nodes
347 * @bat_priv: the bat priv with all the soft interface information
349 static void batadv_nc_purge_orig_hash(struct batadv_priv
*bat_priv
)
351 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
352 struct hlist_head
*head
;
353 struct batadv_orig_node
*orig_node
;
359 /* For each orig_node */
360 for (i
= 0; i
< hash
->size
; i
++) {
361 head
= &hash
->table
[i
];
364 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
)
365 batadv_nc_purge_orig(bat_priv
, orig_node
,
366 batadv_nc_to_purge_nc_node
);
372 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
374 * @bat_priv: the bat priv with all the soft interface information
375 * @hash: hash table containing the nc paths to check
376 * @to_purge: function in charge to decide whether an entry has to be purged or
377 * not. This function takes the nc node as argument and has to return
378 * a boolean value: true is the entry has to be deleted, false
381 static void batadv_nc_purge_paths(struct batadv_priv
*bat_priv
,
382 struct batadv_hashtable
*hash
,
383 bool (*to_purge
)(struct batadv_priv
*,
384 struct batadv_nc_path
*))
386 struct hlist_head
*head
;
387 struct hlist_node
*node_tmp
;
388 struct batadv_nc_path
*nc_path
;
389 spinlock_t
*lock
; /* Protects lists in hash */
392 for (i
= 0; i
< hash
->size
; i
++) {
393 head
= &hash
->table
[i
];
394 lock
= &hash
->list_locks
[i
];
396 /* For each nc_path in this bin */
398 hlist_for_each_entry_safe(nc_path
, node_tmp
, head
, hash_entry
) {
399 /* if an helper function has been passed as parameter,
400 * ask it if the entry has to be purged or not
402 if (to_purge
&& !to_purge(bat_priv
, nc_path
))
405 /* purging an non-empty nc_path should never happen, but
406 * is observed under high CPU load. Delay the purging
407 * until next iteration to allow the packet_list to be
410 if (!unlikely(list_empty(&nc_path
->packet_list
))) {
411 net_ratelimited_function(printk
,
413 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
419 /* nc_path is unused, so remove it */
420 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
421 "Remove nc_path %pM -> %pM\n",
422 nc_path
->prev_hop
, nc_path
->next_hop
);
423 hlist_del_rcu(&nc_path
->hash_entry
);
424 batadv_nc_path_free_ref(nc_path
);
426 spin_unlock_bh(lock
);
431 * batadv_nc_hash_key_gen - computes the nc_path hash key
432 * @key: buffer to hold the final hash key
433 * @src: source ethernet mac address going into the hash key
434 * @dst: destination ethernet mac address going into the hash key
436 static void batadv_nc_hash_key_gen(struct batadv_nc_path
*key
, const char *src
,
439 memcpy(key
->prev_hop
, src
, sizeof(key
->prev_hop
));
440 memcpy(key
->next_hop
, dst
, sizeof(key
->next_hop
));
444 * batadv_nc_hash_choose - compute the hash value for an nc path
445 * @data: data to hash
446 * @size: size of the hash table
448 * Returns the selected index in the hash table for the given data.
450 static uint32_t batadv_nc_hash_choose(const void *data
, uint32_t size
)
452 const struct batadv_nc_path
*nc_path
= data
;
455 hash
= batadv_hash_bytes(hash
, &nc_path
->prev_hop
,
456 sizeof(nc_path
->prev_hop
));
457 hash
= batadv_hash_bytes(hash
, &nc_path
->next_hop
,
458 sizeof(nc_path
->next_hop
));
461 hash
^= (hash
>> 11);
462 hash
+= (hash
<< 15);
468 * batadv_nc_hash_compare - comparing function used in the network coding hash
470 * @node: node in the local table
471 * @data2: second object to compare the node to
473 * Returns 1 if the two entry are the same, 0 otherwise
475 static int batadv_nc_hash_compare(const struct hlist_node
*node
,
478 const struct batadv_nc_path
*nc_path1
, *nc_path2
;
480 nc_path1
= container_of(node
, struct batadv_nc_path
, hash_entry
);
483 /* Return 1 if the two keys are identical */
484 if (memcmp(nc_path1
->prev_hop
, nc_path2
->prev_hop
,
485 sizeof(nc_path1
->prev_hop
)) != 0)
488 if (memcmp(nc_path1
->next_hop
, nc_path2
->next_hop
,
489 sizeof(nc_path1
->next_hop
)) != 0)
496 * batadv_nc_hash_find - search for an existing nc path and return it
497 * @hash: hash table containing the nc path
500 * Returns the nc_path if found, NULL otherwise.
502 static struct batadv_nc_path
*
503 batadv_nc_hash_find(struct batadv_hashtable
*hash
,
506 struct hlist_head
*head
;
507 struct batadv_nc_path
*nc_path
, *nc_path_tmp
= NULL
;
513 index
= batadv_nc_hash_choose(data
, hash
->size
);
514 head
= &hash
->table
[index
];
517 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
518 if (!batadv_nc_hash_compare(&nc_path
->hash_entry
, data
))
521 if (!atomic_inc_not_zero(&nc_path
->refcount
))
524 nc_path_tmp
= nc_path
;
533 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
534 * @nc_packet: the nc packet to send
536 static void batadv_nc_send_packet(struct batadv_nc_packet
*nc_packet
)
538 batadv_send_skb_packet(nc_packet
->skb
,
539 nc_packet
->neigh_node
->if_incoming
,
540 nc_packet
->nc_path
->next_hop
);
541 nc_packet
->skb
= NULL
;
542 batadv_nc_packet_free(nc_packet
);
546 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
547 * @bat_priv: the bat priv with all the soft interface information
548 * @nc_path: the nc path the packet belongs to
549 * @nc_packet: the nc packet to be checked
551 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
552 * timeout. If so, the packet is no longer kept and the entry deleted from the
553 * queue. Has to be called with the appropriate locks.
555 * Returns false as soon as the entry in the fifo queue has not been timed out
556 * yet and true otherwise.
558 static bool batadv_nc_sniffed_purge(struct batadv_priv
*bat_priv
,
559 struct batadv_nc_path
*nc_path
,
560 struct batadv_nc_packet
*nc_packet
)
562 unsigned long timeout
= bat_priv
->nc
.max_buffer_time
;
565 /* Packets are added to tail, so the remaining packets did not time
566 * out and we can stop processing the current queue
568 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
569 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
572 /* purge nc packet */
573 list_del(&nc_packet
->list
);
574 batadv_nc_packet_free(nc_packet
);
583 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
584 * @bat_priv: the bat priv with all the soft interface information
585 * @nc_path: the nc path the packet belongs to
586 * @nc_packet: the nc packet to be checked
588 * Checks whether the given nc packet has hit its forward timeout. If so, the
589 * packet is no longer delayed, immediately sent and the entry deleted from the
590 * queue. Has to be called with the appropriate locks.
592 * Returns false as soon as the entry in the fifo queue has not been timed out
593 * yet and true otherwise.
595 static bool batadv_nc_fwd_flush(struct batadv_priv
*bat_priv
,
596 struct batadv_nc_path
*nc_path
,
597 struct batadv_nc_packet
*nc_packet
)
599 unsigned long timeout
= bat_priv
->nc
.max_fwd_delay
;
601 /* Packets are added to tail, so the remaining packets did not time
602 * out and we can stop processing the current queue
604 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
605 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
609 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
610 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
611 nc_packet
->skb
->len
+ ETH_HLEN
);
612 list_del(&nc_packet
->list
);
613 batadv_nc_send_packet(nc_packet
);
619 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
621 * @bat_priv: the bat priv with all the soft interface information
622 * @hash: to be processed hash table
623 * @process_fn: Function called to process given nc packet. Should return true
624 * to encourage this function to proceed with the next packet.
625 * Otherwise the rest of the current queue is skipped.
628 batadv_nc_process_nc_paths(struct batadv_priv
*bat_priv
,
629 struct batadv_hashtable
*hash
,
630 bool (*process_fn
)(struct batadv_priv
*,
631 struct batadv_nc_path
*,
632 struct batadv_nc_packet
*))
634 struct hlist_head
*head
;
635 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
636 struct batadv_nc_path
*nc_path
;
643 /* Loop hash table bins */
644 for (i
= 0; i
< hash
->size
; i
++) {
645 head
= &hash
->table
[i
];
647 /* Loop coding paths */
649 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
651 spin_lock_bh(&nc_path
->packet_list_lock
);
652 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
653 &nc_path
->packet_list
, list
) {
654 ret
= process_fn(bat_priv
, nc_path
, nc_packet
);
658 spin_unlock_bh(&nc_path
->packet_list_lock
);
665 * batadv_nc_worker - periodic task for house keeping related to network coding
666 * @work: kernel work struct
668 static void batadv_nc_worker(struct work_struct
*work
)
670 struct delayed_work
*delayed_work
;
671 struct batadv_priv_nc
*priv_nc
;
672 struct batadv_priv
*bat_priv
;
673 unsigned long timeout
;
675 delayed_work
= container_of(work
, struct delayed_work
, work
);
676 priv_nc
= container_of(delayed_work
, struct batadv_priv_nc
, work
);
677 bat_priv
= container_of(priv_nc
, struct batadv_priv
, nc
);
679 batadv_nc_purge_orig_hash(bat_priv
);
680 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
681 batadv_nc_to_purge_nc_path_coding
);
682 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
683 batadv_nc_to_purge_nc_path_decoding
);
685 timeout
= bat_priv
->nc
.max_fwd_delay
;
687 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_fwd_flush
, timeout
)) {
688 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
689 batadv_nc_fwd_flush
);
690 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
693 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_sniffed_purge
,
694 bat_priv
->nc
.max_buffer_time
)) {
695 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
696 batadv_nc_sniffed_purge
);
697 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
700 /* Schedule a new check */
701 batadv_nc_start_timer(bat_priv
);
705 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
707 * @bat_priv: the bat priv with all the soft interface information
708 * @orig_node: neighboring orig node which may be used as nc candidate
709 * @ogm_packet: incoming ogm packet also used for the checks
712 * 1) The OGM must have the most recent sequence number.
713 * 2) The TTL must be decremented by one and only one.
714 * 3) The OGM must be received from the first hop from orig_node.
715 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
717 static bool batadv_can_nc_with_orig(struct batadv_priv
*bat_priv
,
718 struct batadv_orig_node
*orig_node
,
719 struct batadv_ogm_packet
*ogm_packet
)
721 if (orig_node
->last_real_seqno
!= ntohl(ogm_packet
->seqno
))
723 if (orig_node
->last_ttl
!= ogm_packet
->ttl
+ 1)
725 if (!batadv_compare_eth(ogm_packet
->orig
, ogm_packet
->prev_sender
))
727 if (ogm_packet
->tq
< bat_priv
->nc
.min_tq
)
734 * batadv_nc_find_nc_node - search for an existing nc node and return it
735 * @orig_node: orig node originating the ogm packet
736 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
737 * (can be equal to orig_node)
738 * @in_coding: traverse incoming or outgoing network coding list
740 * Returns the nc_node if found, NULL otherwise.
742 static struct batadv_nc_node
743 *batadv_nc_find_nc_node(struct batadv_orig_node
*orig_node
,
744 struct batadv_orig_node
*orig_neigh_node
,
747 struct batadv_nc_node
*nc_node
, *nc_node_out
= NULL
;
748 struct list_head
*list
;
751 list
= &orig_neigh_node
->in_coding_list
;
753 list
= &orig_neigh_node
->out_coding_list
;
755 /* Traverse list of nc_nodes to orig_node */
757 list_for_each_entry_rcu(nc_node
, list
, list
) {
758 if (!batadv_compare_eth(nc_node
->addr
, orig_node
->orig
))
761 if (!atomic_inc_not_zero(&nc_node
->refcount
))
765 nc_node_out
= nc_node
;
774 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
776 * @bat_priv: the bat priv with all the soft interface information
777 * @orig_node: orig node originating the ogm packet
778 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
779 * (can be equal to orig_node)
780 * @in_coding: traverse incoming or outgoing network coding list
782 * Returns the nc_node if found or created, NULL in case of an error.
784 static struct batadv_nc_node
785 *batadv_nc_get_nc_node(struct batadv_priv
*bat_priv
,
786 struct batadv_orig_node
*orig_node
,
787 struct batadv_orig_node
*orig_neigh_node
,
790 struct batadv_nc_node
*nc_node
;
791 spinlock_t
*lock
; /* Used to lock list selected by "int in_coding" */
792 struct list_head
*list
;
794 /* Check if nc_node is already added */
795 nc_node
= batadv_nc_find_nc_node(orig_node
, orig_neigh_node
, in_coding
);
801 nc_node
= kzalloc(sizeof(*nc_node
), GFP_ATOMIC
);
805 if (!atomic_inc_not_zero(&orig_neigh_node
->refcount
))
808 /* Initialize nc_node */
809 INIT_LIST_HEAD(&nc_node
->list
);
810 memcpy(nc_node
->addr
, orig_node
->orig
, ETH_ALEN
);
811 nc_node
->orig_node
= orig_neigh_node
;
812 atomic_set(&nc_node
->refcount
, 2);
814 /* Select ingoing or outgoing coding node */
816 lock
= &orig_neigh_node
->in_coding_list_lock
;
817 list
= &orig_neigh_node
->in_coding_list
;
819 lock
= &orig_neigh_node
->out_coding_list_lock
;
820 list
= &orig_neigh_node
->out_coding_list
;
823 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_node %pM -> %pM\n",
824 nc_node
->addr
, nc_node
->orig_node
->orig
);
826 /* Add nc_node to orig_node */
828 list_add_tail_rcu(&nc_node
->list
, list
);
829 spin_unlock_bh(lock
);
839 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
840 * (best called on incoming OGMs)
841 * @bat_priv: the bat priv with all the soft interface information
842 * @orig_node: orig node originating the ogm packet
843 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
844 * (can be equal to orig_node)
845 * @ogm_packet: incoming ogm packet
846 * @is_single_hop_neigh: orig_node is a single hop neighbor
848 void batadv_nc_update_nc_node(struct batadv_priv
*bat_priv
,
849 struct batadv_orig_node
*orig_node
,
850 struct batadv_orig_node
*orig_neigh_node
,
851 struct batadv_ogm_packet
*ogm_packet
,
852 int is_single_hop_neigh
)
854 struct batadv_nc_node
*in_nc_node
= NULL
, *out_nc_node
= NULL
;
856 /* Check if network coding is enabled */
857 if (!atomic_read(&bat_priv
->network_coding
))
860 /* check if orig node is network coding enabled */
861 if (!(orig_node
->capabilities
& BATADV_ORIG_CAPA_HAS_NC
))
864 /* accept ogms from 'good' neighbors and single hop neighbors */
865 if (!batadv_can_nc_with_orig(bat_priv
, orig_node
, ogm_packet
) &&
866 !is_single_hop_neigh
)
869 /* Add orig_node as in_nc_node on hop */
870 in_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_node
,
871 orig_neigh_node
, true);
875 in_nc_node
->last_seen
= jiffies
;
877 /* Add hop as out_nc_node on orig_node */
878 out_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_neigh_node
,
883 out_nc_node
->last_seen
= jiffies
;
887 batadv_nc_node_free_ref(in_nc_node
);
889 batadv_nc_node_free_ref(out_nc_node
);
893 * batadv_nc_get_path - get existing nc_path or allocate a new one
894 * @bat_priv: the bat priv with all the soft interface information
895 * @hash: hash table containing the nc path
896 * @src: ethernet source address - first half of the nc path search key
897 * @dst: ethernet destination address - second half of the nc path search key
899 * Returns pointer to nc_path if the path was found or created, returns NULL
902 static struct batadv_nc_path
*batadv_nc_get_path(struct batadv_priv
*bat_priv
,
903 struct batadv_hashtable
*hash
,
908 struct batadv_nc_path
*nc_path
, nc_path_key
;
910 batadv_nc_hash_key_gen(&nc_path_key
, src
, dst
);
912 /* Search for existing nc_path */
913 nc_path
= batadv_nc_hash_find(hash
, (void *)&nc_path_key
);
916 /* Set timestamp to delay removal of nc_path */
917 nc_path
->last_valid
= jiffies
;
921 /* No existing nc_path was found; create a new */
922 nc_path
= kzalloc(sizeof(*nc_path
), GFP_ATOMIC
);
927 /* Initialize nc_path */
928 INIT_LIST_HEAD(&nc_path
->packet_list
);
929 spin_lock_init(&nc_path
->packet_list_lock
);
930 atomic_set(&nc_path
->refcount
, 2);
931 nc_path
->last_valid
= jiffies
;
932 memcpy(nc_path
->next_hop
, dst
, ETH_ALEN
);
933 memcpy(nc_path
->prev_hop
, src
, ETH_ALEN
);
935 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_path %pM -> %pM\n",
939 /* Add nc_path to hash table */
940 hash_added
= batadv_hash_add(hash
, batadv_nc_hash_compare
,
941 batadv_nc_hash_choose
, &nc_path_key
,
942 &nc_path
->hash_entry
);
944 if (hash_added
< 0) {
953 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
954 * selection of a receiver with slightly lower TQ than the other
955 * @tq: to be weighted tq value
957 static uint8_t batadv_nc_random_weight_tq(uint8_t tq
)
959 uint8_t rand_val
, rand_tq
;
961 get_random_bytes(&rand_val
, sizeof(rand_val
));
963 /* randomize the estimated packet loss (max TQ - estimated TQ) */
964 rand_tq
= rand_val
* (BATADV_TQ_MAX_VALUE
- tq
);
966 /* normalize the randomized packet loss */
967 rand_tq
/= BATADV_TQ_MAX_VALUE
;
969 /* convert to (randomized) estimated tq again */
970 return BATADV_TQ_MAX_VALUE
- rand_tq
;
974 * batadv_nc_memxor - XOR destination with source
975 * @dst: byte array to XOR into
976 * @src: byte array to XOR from
977 * @len: length of destination array
979 static void batadv_nc_memxor(char *dst
, const char *src
, unsigned int len
)
983 for (i
= 0; i
< len
; ++i
)
988 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
989 * into a coded_packet and send it
990 * @bat_priv: the bat priv with all the soft interface information
991 * @skb: data skb to forward
992 * @ethhdr: pointer to the ethernet header inside the skb
993 * @nc_packet: structure containing the packet to the skb can be coded with
994 * @neigh_node: next hop to forward packet to
996 * Returns true if both packets are consumed, false otherwise.
998 static bool batadv_nc_code_packets(struct batadv_priv
*bat_priv
,
1000 struct ethhdr
*ethhdr
,
1001 struct batadv_nc_packet
*nc_packet
,
1002 struct batadv_neigh_node
*neigh_node
)
1004 uint8_t tq_weighted_neigh
, tq_weighted_coding
, tq_tmp
;
1005 struct sk_buff
*skb_dest
, *skb_src
;
1006 struct batadv_unicast_packet
*packet1
;
1007 struct batadv_unicast_packet
*packet2
;
1008 struct batadv_coded_packet
*coded_packet
;
1009 struct batadv_neigh_node
*neigh_tmp
, *router_neigh
;
1010 struct batadv_neigh_node
*router_coding
= NULL
;
1011 uint8_t *first_source
, *first_dest
, *second_source
, *second_dest
;
1012 __be32 packet_id1
, packet_id2
;
1016 int unicast_size
= sizeof(*packet1
);
1017 int coded_size
= sizeof(*coded_packet
);
1018 int header_add
= coded_size
- unicast_size
;
1020 router_neigh
= batadv_orig_node_get_router(neigh_node
->orig_node
);
1024 neigh_tmp
= nc_packet
->neigh_node
;
1025 router_coding
= batadv_orig_node_get_router(neigh_tmp
->orig_node
);
1029 tq_tmp
= batadv_nc_random_weight_tq(router_neigh
->bat_iv
.tq_avg
);
1030 tq_weighted_neigh
= tq_tmp
;
1031 tq_tmp
= batadv_nc_random_weight_tq(router_coding
->bat_iv
.tq_avg
);
1032 tq_weighted_coding
= tq_tmp
;
1034 /* Select one destination for the MAC-header dst-field based on
1035 * weighted TQ-values.
1037 if (tq_weighted_neigh
>= tq_weighted_coding
) {
1038 /* Destination from nc_packet is selected for MAC-header */
1039 first_dest
= nc_packet
->nc_path
->next_hop
;
1040 first_source
= nc_packet
->nc_path
->prev_hop
;
1041 second_dest
= neigh_node
->addr
;
1042 second_source
= ethhdr
->h_source
;
1043 packet1
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1044 packet2
= (struct batadv_unicast_packet
*)skb
->data
;
1045 packet_id1
= nc_packet
->packet_id
;
1046 packet_id2
= batadv_skb_crc32(skb
,
1047 skb
->data
+ sizeof(*packet2
));
1049 /* Destination for skb is selected for MAC-header */
1050 first_dest
= neigh_node
->addr
;
1051 first_source
= ethhdr
->h_source
;
1052 second_dest
= nc_packet
->nc_path
->next_hop
;
1053 second_source
= nc_packet
->nc_path
->prev_hop
;
1054 packet1
= (struct batadv_unicast_packet
*)skb
->data
;
1055 packet2
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1056 packet_id1
= batadv_skb_crc32(skb
,
1057 skb
->data
+ sizeof(*packet1
));
1058 packet_id2
= nc_packet
->packet_id
;
1061 /* Instead of zero padding the smallest data buffer, we
1062 * code into the largest.
1064 if (skb
->len
<= nc_packet
->skb
->len
) {
1065 skb_dest
= nc_packet
->skb
;
1069 skb_src
= nc_packet
->skb
;
1072 /* coding_len is used when decoding the packet shorter packet */
1073 coding_len
= skb_src
->len
- unicast_size
;
1075 if (skb_linearize(skb_dest
) < 0 || skb_linearize(skb_src
) < 0)
1078 skb_push(skb_dest
, header_add
);
1080 coded_packet
= (struct batadv_coded_packet
*)skb_dest
->data
;
1081 skb_reset_mac_header(skb_dest
);
1083 coded_packet
->packet_type
= BATADV_CODED
;
1084 coded_packet
->version
= BATADV_COMPAT_VERSION
;
1085 coded_packet
->ttl
= packet1
->ttl
;
1087 /* Info about first unicast packet */
1088 memcpy(coded_packet
->first_source
, first_source
, ETH_ALEN
);
1089 memcpy(coded_packet
->first_orig_dest
, packet1
->dest
, ETH_ALEN
);
1090 coded_packet
->first_crc
= packet_id1
;
1091 coded_packet
->first_ttvn
= packet1
->ttvn
;
1093 /* Info about second unicast packet */
1094 memcpy(coded_packet
->second_dest
, second_dest
, ETH_ALEN
);
1095 memcpy(coded_packet
->second_source
, second_source
, ETH_ALEN
);
1096 memcpy(coded_packet
->second_orig_dest
, packet2
->dest
, ETH_ALEN
);
1097 coded_packet
->second_crc
= packet_id2
;
1098 coded_packet
->second_ttl
= packet2
->ttl
;
1099 coded_packet
->second_ttvn
= packet2
->ttvn
;
1100 coded_packet
->coded_len
= htons(coding_len
);
1102 /* This is where the magic happens: Code skb_src into skb_dest */
1103 batadv_nc_memxor(skb_dest
->data
+ coded_size
,
1104 skb_src
->data
+ unicast_size
, coding_len
);
1106 /* Update counters accordingly */
1107 if (BATADV_SKB_CB(skb_src
)->decoded
&&
1108 BATADV_SKB_CB(skb_dest
)->decoded
) {
1109 /* Both packets are recoded */
1110 count
= skb_src
->len
+ ETH_HLEN
;
1111 count
+= skb_dest
->len
+ ETH_HLEN
;
1112 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE
, 2);
1113 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
, count
);
1114 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1115 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1116 /* Both packets are newly coded */
1117 count
= skb_src
->len
+ ETH_HLEN
;
1118 count
+= skb_dest
->len
+ ETH_HLEN
;
1119 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE
, 2);
1120 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
, count
);
1121 } else if (BATADV_SKB_CB(skb_src
)->decoded
&&
1122 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1123 /* skb_src recoded and skb_dest is newly coded */
1124 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1125 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1126 skb_src
->len
+ ETH_HLEN
);
1127 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1128 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1129 skb_dest
->len
+ ETH_HLEN
);
1130 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1131 BATADV_SKB_CB(skb_dest
)->decoded
) {
1132 /* skb_src is newly coded and skb_dest is recoded */
1133 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1134 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1135 skb_src
->len
+ ETH_HLEN
);
1136 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1137 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1138 skb_dest
->len
+ ETH_HLEN
);
1141 /* skb_src is now coded into skb_dest, so free it */
1144 /* avoid duplicate free of skb from nc_packet */
1145 nc_packet
->skb
= NULL
;
1146 batadv_nc_packet_free(nc_packet
);
1148 /* Send the coded packet and return true */
1149 batadv_send_skb_packet(skb_dest
, neigh_node
->if_incoming
, first_dest
);
1153 batadv_neigh_node_free_ref(router_neigh
);
1155 batadv_neigh_node_free_ref(router_coding
);
1160 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1161 * @skb: data skb to forward
1162 * @dst: destination mac address of the other skb to code with
1163 * @src: source mac address of skb
1165 * Whenever we network code a packet we have to check whether we received it in
1166 * a network coded form. If so, we may not be able to use it for coding because
1167 * some neighbors may also have received (overheard) the packet in the network
1168 * coded form without being able to decode it. It is hard to know which of the
1169 * neighboring nodes was able to decode the packet, therefore we can only
1170 * re-code the packet if the source of the previous encoded packet is involved.
1171 * Since the source encoded the packet we can be certain it has all necessary
1172 * decode information.
1174 * Returns true if coding of a decoded packet is allowed.
1176 static bool batadv_nc_skb_coding_possible(struct sk_buff
*skb
,
1177 uint8_t *dst
, uint8_t *src
)
1179 if (BATADV_SKB_CB(skb
)->decoded
&& !batadv_compare_eth(dst
, src
))
1186 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1187 * out_nc_node to retrieve a buffered packet that can be used for coding.
1188 * @bat_priv: the bat priv with all the soft interface information
1189 * @in_nc_node: pointer to skb next hop's neighbor nc node
1190 * @out_nc_node: pointer to skb source's neighbor nc node
1191 * @skb: data skb to forward
1192 * @eth_dst: next hop mac address of skb
1194 * Returns true if coding of a decoded skb is allowed.
1196 static struct batadv_nc_packet
*
1197 batadv_nc_path_search(struct batadv_priv
*bat_priv
,
1198 struct batadv_nc_node
*in_nc_node
,
1199 struct batadv_nc_node
*out_nc_node
,
1200 struct sk_buff
*skb
,
1203 struct batadv_nc_path
*nc_path
, nc_path_key
;
1204 struct batadv_nc_packet
*nc_packet_out
= NULL
;
1205 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
1206 struct batadv_hashtable
*hash
= bat_priv
->nc
.coding_hash
;
1212 /* Create almost path key */
1213 batadv_nc_hash_key_gen(&nc_path_key
, in_nc_node
->addr
,
1215 idx
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1217 /* Check for coding opportunities in this nc_path */
1219 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[idx
], hash_entry
) {
1220 if (!batadv_compare_eth(nc_path
->prev_hop
, in_nc_node
->addr
))
1223 if (!batadv_compare_eth(nc_path
->next_hop
, out_nc_node
->addr
))
1226 spin_lock_bh(&nc_path
->packet_list_lock
);
1227 if (list_empty(&nc_path
->packet_list
)) {
1228 spin_unlock_bh(&nc_path
->packet_list_lock
);
1232 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
1233 &nc_path
->packet_list
, list
) {
1234 if (!batadv_nc_skb_coding_possible(nc_packet
->skb
,
1239 /* Coding opportunity is found! */
1240 list_del(&nc_packet
->list
);
1241 nc_packet_out
= nc_packet
;
1245 spin_unlock_bh(&nc_path
->packet_list_lock
);
1250 return nc_packet_out
;
1254 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1255 * skb's sender (may be equal to the originator).
1256 * @bat_priv: the bat priv with all the soft interface information
1257 * @skb: data skb to forward
1258 * @eth_dst: next hop mac address of skb
1259 * @eth_src: source mac address of skb
1260 * @in_nc_node: pointer to skb next hop's neighbor nc node
1262 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1264 static struct batadv_nc_packet
*
1265 batadv_nc_skb_src_search(struct batadv_priv
*bat_priv
,
1266 struct sk_buff
*skb
,
1269 struct batadv_nc_node
*in_nc_node
)
1271 struct batadv_orig_node
*orig_node
;
1272 struct batadv_nc_node
*out_nc_node
;
1273 struct batadv_nc_packet
*nc_packet
= NULL
;
1275 orig_node
= batadv_orig_hash_find(bat_priv
, eth_src
);
1280 list_for_each_entry_rcu(out_nc_node
,
1281 &orig_node
->out_coding_list
, list
) {
1282 /* Check if the skb is decoded and if recoding is possible */
1283 if (!batadv_nc_skb_coding_possible(skb
,
1284 out_nc_node
->addr
, eth_src
))
1287 /* Search for an opportunity in this nc_path */
1288 nc_packet
= batadv_nc_path_search(bat_priv
, in_nc_node
,
1289 out_nc_node
, skb
, eth_dst
);
1295 batadv_orig_node_free_ref(orig_node
);
1300 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1301 * unicast skb before it is stored for use in later decoding
1302 * @bat_priv: the bat priv with all the soft interface information
1303 * @skb: data skb to store
1304 * @eth_dst_new: new destination mac address of skb
1306 static void batadv_nc_skb_store_before_coding(struct batadv_priv
*bat_priv
,
1307 struct sk_buff
*skb
,
1308 uint8_t *eth_dst_new
)
1310 struct ethhdr
*ethhdr
;
1312 /* Copy skb header to change the mac header */
1313 skb
= pskb_copy(skb
, GFP_ATOMIC
);
1317 /* Set the mac header as if we actually sent the packet uncoded */
1318 ethhdr
= eth_hdr(skb
);
1319 memcpy(ethhdr
->h_source
, ethhdr
->h_dest
, ETH_ALEN
);
1320 memcpy(ethhdr
->h_dest
, eth_dst_new
, ETH_ALEN
);
1322 /* Set data pointer to MAC header to mimic packets from our tx path */
1323 skb_push(skb
, ETH_HLEN
);
1325 /* Add the packet to the decoding packet pool */
1326 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1328 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1335 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1336 * @skb: data skb to forward
1337 * @neigh_node: next hop to forward packet to
1338 * @ethhdr: pointer to the ethernet header inside the skb
1340 * Loops through list of neighboring nodes the next hop has a good connection to
1341 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1342 * next hop that potentially sent a packet which our next hop also received
1343 * (overheard) and has stored for later decoding.
1345 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1347 static bool batadv_nc_skb_dst_search(struct sk_buff
*skb
,
1348 struct batadv_neigh_node
*neigh_node
,
1349 struct ethhdr
*ethhdr
)
1351 struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1352 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1353 struct batadv_orig_node
*orig_node
= neigh_node
->orig_node
;
1354 struct batadv_nc_node
*nc_node
;
1355 struct batadv_nc_packet
*nc_packet
= NULL
;
1358 list_for_each_entry_rcu(nc_node
, &orig_node
->in_coding_list
, list
) {
1359 /* Search for coding opportunity with this in_nc_node */
1360 nc_packet
= batadv_nc_skb_src_search(bat_priv
, skb
,
1362 ethhdr
->h_source
, nc_node
);
1364 /* Opportunity was found, so stop searching */
1373 /* Save packets for later decoding */
1374 batadv_nc_skb_store_before_coding(bat_priv
, skb
,
1376 batadv_nc_skb_store_before_coding(bat_priv
, nc_packet
->skb
,
1377 nc_packet
->neigh_node
->addr
);
1379 /* Code and send packets */
1380 if (batadv_nc_code_packets(bat_priv
, skb
, ethhdr
, nc_packet
,
1384 /* out of mem ? Coding failed - we have to free the buffered packet
1385 * to avoid memleaks. The skb passed as argument will be dealt with
1386 * by the calling function.
1388 batadv_nc_send_packet(nc_packet
);
1393 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1394 * @skb: skb to add to path
1395 * @nc_path: path to add skb to
1396 * @neigh_node: next hop to forward packet to
1397 * @packet_id: checksum to identify packet
1399 * Returns true if the packet was buffered or false in case of an error.
1401 static bool batadv_nc_skb_add_to_path(struct sk_buff
*skb
,
1402 struct batadv_nc_path
*nc_path
,
1403 struct batadv_neigh_node
*neigh_node
,
1406 struct batadv_nc_packet
*nc_packet
;
1408 nc_packet
= kzalloc(sizeof(*nc_packet
), GFP_ATOMIC
);
1412 /* Initialize nc_packet */
1413 nc_packet
->timestamp
= jiffies
;
1414 nc_packet
->packet_id
= packet_id
;
1415 nc_packet
->skb
= skb
;
1416 nc_packet
->neigh_node
= neigh_node
;
1417 nc_packet
->nc_path
= nc_path
;
1419 /* Add coding packet to list */
1420 spin_lock_bh(&nc_path
->packet_list_lock
);
1421 list_add_tail(&nc_packet
->list
, &nc_path
->packet_list
);
1422 spin_unlock_bh(&nc_path
->packet_list_lock
);
1428 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1430 * @skb: data skb to forward
1431 * @neigh_node: next hop to forward packet to
1433 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1435 bool batadv_nc_skb_forward(struct sk_buff
*skb
,
1436 struct batadv_neigh_node
*neigh_node
)
1438 const struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1439 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1440 struct batadv_unicast_packet
*packet
;
1441 struct batadv_nc_path
*nc_path
;
1442 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1446 /* Check if network coding is enabled */
1447 if (!atomic_read(&bat_priv
->network_coding
))
1450 /* We only handle unicast packets */
1451 payload
= skb_network_header(skb
);
1452 packet
= (struct batadv_unicast_packet
*)payload
;
1453 if (packet
->packet_type
!= BATADV_UNICAST
)
1456 /* Try to find a coding opportunity and send the skb if one is found */
1457 if (batadv_nc_skb_dst_search(skb
, neigh_node
, ethhdr
))
1460 /* Find or create a nc_path for this src-dst pair */
1461 nc_path
= batadv_nc_get_path(bat_priv
,
1462 bat_priv
->nc
.coding_hash
,
1469 /* Add skb to nc_path */
1470 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1471 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, neigh_node
, packet_id
))
1474 /* Packet is consumed */
1478 batadv_nc_path_free_ref(nc_path
);
1480 /* Packet is not consumed */
1485 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1486 * when decoding coded packets
1487 * @bat_priv: the bat priv with all the soft interface information
1488 * @skb: data skb to store
1490 void batadv_nc_skb_store_for_decoding(struct batadv_priv
*bat_priv
,
1491 struct sk_buff
*skb
)
1493 struct batadv_unicast_packet
*packet
;
1494 struct batadv_nc_path
*nc_path
;
1495 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1499 /* Check if network coding is enabled */
1500 if (!atomic_read(&bat_priv
->network_coding
))
1503 /* Check for supported packet type */
1504 payload
= skb_network_header(skb
);
1505 packet
= (struct batadv_unicast_packet
*)payload
;
1506 if (packet
->packet_type
!= BATADV_UNICAST
)
1509 /* Find existing nc_path or create a new */
1510 nc_path
= batadv_nc_get_path(bat_priv
,
1511 bat_priv
->nc
.decoding_hash
,
1518 /* Clone skb and adjust skb->data to point at batman header */
1519 skb
= skb_clone(skb
, GFP_ATOMIC
);
1523 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
1526 if (unlikely(!skb_pull_rcsum(skb
, ETH_HLEN
)))
1529 /* Add skb to nc_path */
1530 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1531 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, NULL
, packet_id
))
1534 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_BUFFER
);
1540 batadv_nc_path_free_ref(nc_path
);
1546 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1547 * should be saved in the decoding buffer and, if so, store it there
1548 * @bat_priv: the bat priv with all the soft interface information
1549 * @skb: unicast skb to store
1551 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv
*bat_priv
,
1552 struct sk_buff
*skb
)
1554 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1556 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1559 /* Set data pointer to MAC header to mimic packets from our tx path */
1560 skb_push(skb
, ETH_HLEN
);
1562 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1566 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1568 * @bat_priv: the bat priv with all the soft interface information
1569 * @skb: unicast skb to decode
1570 * @nc_packet: decode data needed to decode the skb
1572 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1573 * in case of an error.
1575 static struct batadv_unicast_packet
*
1576 batadv_nc_skb_decode_packet(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1577 struct batadv_nc_packet
*nc_packet
)
1579 const int h_size
= sizeof(struct batadv_unicast_packet
);
1580 const int h_diff
= sizeof(struct batadv_coded_packet
) - h_size
;
1581 struct batadv_unicast_packet
*unicast_packet
;
1582 struct batadv_coded_packet coded_packet_tmp
;
1583 struct ethhdr
*ethhdr
, ethhdr_tmp
;
1584 uint8_t *orig_dest
, ttl
, ttvn
;
1585 unsigned int coding_len
;
1588 /* Save headers temporarily */
1589 memcpy(&coded_packet_tmp
, skb
->data
, sizeof(coded_packet_tmp
));
1590 memcpy(ðhdr_tmp
, skb_mac_header(skb
), sizeof(ethhdr_tmp
));
1592 if (skb_cow(skb
, 0) < 0)
1595 if (unlikely(!skb_pull_rcsum(skb
, h_diff
)))
1598 /* Data points to batman header, so set mac header 14 bytes before
1599 * and network to data
1601 skb_set_mac_header(skb
, -ETH_HLEN
);
1602 skb_reset_network_header(skb
);
1604 /* Reconstruct original mac header */
1605 ethhdr
= eth_hdr(skb
);
1606 memcpy(ethhdr
, ðhdr_tmp
, sizeof(*ethhdr
));
1608 /* Select the correct unicast header information based on the location
1609 * of our mac address in the coded_packet header
1611 if (batadv_is_my_mac(bat_priv
, coded_packet_tmp
.second_dest
)) {
1612 /* If we are the second destination the packet was overheard,
1613 * so the Ethernet address must be copied to h_dest and
1614 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1616 memcpy(ethhdr
->h_dest
, coded_packet_tmp
.second_dest
, ETH_ALEN
);
1617 skb
->pkt_type
= PACKET_HOST
;
1619 orig_dest
= coded_packet_tmp
.second_orig_dest
;
1620 ttl
= coded_packet_tmp
.second_ttl
;
1621 ttvn
= coded_packet_tmp
.second_ttvn
;
1623 orig_dest
= coded_packet_tmp
.first_orig_dest
;
1624 ttl
= coded_packet_tmp
.ttl
;
1625 ttvn
= coded_packet_tmp
.first_ttvn
;
1628 coding_len
= ntohs(coded_packet_tmp
.coded_len
);
1630 if (coding_len
> skb
->len
)
1633 /* Here the magic is reversed:
1634 * extract the missing packet from the received coded packet
1636 batadv_nc_memxor(skb
->data
+ h_size
,
1637 nc_packet
->skb
->data
+ h_size
,
1640 /* Resize decoded skb if decoded with larger packet */
1641 if (nc_packet
->skb
->len
> coding_len
+ h_size
) {
1642 err
= pskb_trim_rcsum(skb
, coding_len
+ h_size
);
1647 /* Create decoded unicast packet */
1648 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1649 unicast_packet
->packet_type
= BATADV_UNICAST
;
1650 unicast_packet
->version
= BATADV_COMPAT_VERSION
;
1651 unicast_packet
->ttl
= ttl
;
1652 memcpy(unicast_packet
->dest
, orig_dest
, ETH_ALEN
);
1653 unicast_packet
->ttvn
= ttvn
;
1655 batadv_nc_packet_free(nc_packet
);
1656 return unicast_packet
;
1660 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1661 * find the data needed to decode the coded packet
1662 * @bat_priv: the bat priv with all the soft interface information
1663 * @ethhdr: pointer to the ethernet header inside the coded packet
1664 * @coded: coded packet we try to find decode data for
1666 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1668 static struct batadv_nc_packet
*
1669 batadv_nc_find_decoding_packet(struct batadv_priv
*bat_priv
,
1670 struct ethhdr
*ethhdr
,
1671 struct batadv_coded_packet
*coded
)
1673 struct batadv_hashtable
*hash
= bat_priv
->nc
.decoding_hash
;
1674 struct batadv_nc_packet
*tmp_nc_packet
, *nc_packet
= NULL
;
1675 struct batadv_nc_path
*nc_path
, nc_path_key
;
1676 uint8_t *dest
, *source
;
1683 /* Select the correct packet id based on the location of our mac-addr */
1684 dest
= ethhdr
->h_source
;
1685 if (!batadv_is_my_mac(bat_priv
, coded
->second_dest
)) {
1686 source
= coded
->second_source
;
1687 packet_id
= coded
->second_crc
;
1689 source
= coded
->first_source
;
1690 packet_id
= coded
->first_crc
;
1693 batadv_nc_hash_key_gen(&nc_path_key
, source
, dest
);
1694 index
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1696 /* Search for matching coding path */
1698 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[index
], hash_entry
) {
1699 /* Find matching nc_packet */
1700 spin_lock_bh(&nc_path
->packet_list_lock
);
1701 list_for_each_entry(tmp_nc_packet
,
1702 &nc_path
->packet_list
, list
) {
1703 if (packet_id
== tmp_nc_packet
->packet_id
) {
1704 list_del(&tmp_nc_packet
->list
);
1706 nc_packet
= tmp_nc_packet
;
1710 spin_unlock_bh(&nc_path
->packet_list_lock
);
1718 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
1719 "No decoding packet found for %u\n", packet_id
);
1725 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1726 * resulting unicast packet
1727 * @skb: incoming coded packet
1728 * @recv_if: pointer to interface this packet was received on
1730 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
1731 struct batadv_hard_iface
*recv_if
)
1733 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1734 struct batadv_unicast_packet
*unicast_packet
;
1735 struct batadv_coded_packet
*coded_packet
;
1736 struct batadv_nc_packet
*nc_packet
;
1737 struct ethhdr
*ethhdr
;
1738 int hdr_size
= sizeof(*coded_packet
);
1740 /* Check if network coding is enabled */
1741 if (!atomic_read(&bat_priv
->network_coding
))
1744 /* Make sure we can access (and remove) header */
1745 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1748 coded_packet
= (struct batadv_coded_packet
*)skb
->data
;
1749 ethhdr
= eth_hdr(skb
);
1751 /* Verify frame is destined for us */
1752 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
) &&
1753 !batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1756 /* Update stat counter */
1757 if (batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1758 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_SNIFFED
);
1760 nc_packet
= batadv_nc_find_decoding_packet(bat_priv
, ethhdr
,
1763 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1767 /* Make skb's linear, because decoding accesses the entire buffer */
1768 if (skb_linearize(skb
) < 0)
1769 goto free_nc_packet
;
1771 if (skb_linearize(nc_packet
->skb
) < 0)
1772 goto free_nc_packet
;
1774 /* Decode the packet */
1775 unicast_packet
= batadv_nc_skb_decode_packet(bat_priv
, skb
, nc_packet
);
1776 if (!unicast_packet
) {
1777 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1778 goto free_nc_packet
;
1781 /* Mark packet as decoded to do correct recoding when forwarding */
1782 BATADV_SKB_CB(skb
)->decoded
= true;
1783 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE
);
1784 batadv_add_counter(bat_priv
, BATADV_CNT_NC_DECODE_BYTES
,
1785 skb
->len
+ ETH_HLEN
);
1786 return batadv_recv_unicast_packet(skb
, recv_if
);
1789 batadv_nc_packet_free(nc_packet
);
1794 * batadv_nc_mesh_free - clean up network coding memory
1795 * @bat_priv: the bat priv with all the soft interface information
1797 void batadv_nc_mesh_free(struct batadv_priv
*bat_priv
)
1799 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1800 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1801 cancel_delayed_work_sync(&bat_priv
->nc
.work
);
1803 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
, NULL
);
1804 batadv_hash_destroy(bat_priv
->nc
.coding_hash
);
1805 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
, NULL
);
1806 batadv_hash_destroy(bat_priv
->nc
.decoding_hash
);
1810 * batadv_nc_nodes_seq_print_text - print the nc node information
1811 * @seq: seq file to print on
1814 int batadv_nc_nodes_seq_print_text(struct seq_file
*seq
, void *offset
)
1816 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1817 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1818 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1819 struct batadv_hard_iface
*primary_if
;
1820 struct hlist_head
*head
;
1821 struct batadv_orig_node
*orig_node
;
1822 struct batadv_nc_node
*nc_node
;
1825 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1829 /* Traverse list of originators */
1830 for (i
= 0; i
< hash
->size
; i
++) {
1831 head
= &hash
->table
[i
];
1833 /* For each orig_node in this bin */
1835 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1836 /* no need to print the orig node if it does not have
1837 * network coding neighbors
1839 if (list_empty(&orig_node
->in_coding_list
) &&
1840 list_empty(&orig_node
->out_coding_list
))
1843 seq_printf(seq
, "Node: %pM\n", orig_node
->orig
);
1845 seq_puts(seq
, " Ingoing: ");
1846 /* For each in_nc_node to this orig_node */
1847 list_for_each_entry_rcu(nc_node
,
1848 &orig_node
->in_coding_list
,
1850 seq_printf(seq
, "%pM ",
1852 seq_puts(seq
, "\n");
1854 seq_puts(seq
, " Outgoing: ");
1855 /* For out_nc_node to this orig_node */
1856 list_for_each_entry_rcu(nc_node
,
1857 &orig_node
->out_coding_list
,
1859 seq_printf(seq
, "%pM ",
1861 seq_puts(seq
, "\n\n");
1868 batadv_hardif_free_ref(primary_if
);
1873 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1874 * @bat_priv: the bat priv with all the soft interface information
1876 int batadv_nc_init_debugfs(struct batadv_priv
*bat_priv
)
1878 struct dentry
*nc_dir
, *file
;
1880 nc_dir
= debugfs_create_dir("nc", bat_priv
->debug_dir
);
1884 file
= debugfs_create_u8("min_tq", S_IRUGO
| S_IWUSR
, nc_dir
,
1885 &bat_priv
->nc
.min_tq
);
1889 file
= debugfs_create_u32("max_fwd_delay", S_IRUGO
| S_IWUSR
, nc_dir
,
1890 &bat_priv
->nc
.max_fwd_delay
);
1894 file
= debugfs_create_u32("max_buffer_time", S_IRUGO
| S_IWUSR
, nc_dir
,
1895 &bat_priv
->nc
.max_buffer_time
);