1 /* Copyright (C) 2012-2014 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
);
90 batadv_nc_tvlv_container_update(bat_priv
);
94 * batadv_nc_tvlv_ogm_handler_v1 - process incoming nc tvlv container
95 * @bat_priv: the bat priv with all the soft interface information
96 * @orig: the orig_node of the ogm
97 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
98 * @tvlv_value: tvlv buffer containing the gateway data
99 * @tvlv_value_len: tvlv buffer length
101 static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
102 struct batadv_orig_node
*orig
,
105 uint16_t tvlv_value_len
)
107 if (flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
)
108 orig
->capabilities
&= ~BATADV_ORIG_CAPA_HAS_NC
;
110 orig
->capabilities
|= BATADV_ORIG_CAPA_HAS_NC
;
114 * batadv_nc_mesh_init - initialise coding hash table and start house keeping
115 * @bat_priv: the bat priv with all the soft interface information
117 int batadv_nc_mesh_init(struct batadv_priv
*bat_priv
)
119 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
120 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
122 if (bat_priv
->nc
.coding_hash
|| bat_priv
->nc
.decoding_hash
)
125 bat_priv
->nc
.coding_hash
= batadv_hash_new(128);
126 if (!bat_priv
->nc
.coding_hash
)
129 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
130 &batadv_nc_coding_hash_lock_class_key
);
132 bat_priv
->nc
.decoding_hash
= batadv_hash_new(128);
133 if (!bat_priv
->nc
.decoding_hash
)
136 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
137 &batadv_nc_decoding_hash_lock_class_key
);
139 INIT_DELAYED_WORK(&bat_priv
->nc
.work
, batadv_nc_worker
);
140 batadv_nc_start_timer(bat_priv
);
142 batadv_tvlv_handler_register(bat_priv
, batadv_nc_tvlv_ogm_handler_v1
,
143 NULL
, BATADV_TVLV_NC
, 1,
144 BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
145 batadv_nc_tvlv_container_update(bat_priv
);
153 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
154 * @bat_priv: the bat priv with all the soft interface information
156 void batadv_nc_init_bat_priv(struct batadv_priv
*bat_priv
)
158 atomic_set(&bat_priv
->network_coding
, 1);
159 bat_priv
->nc
.min_tq
= 200;
160 bat_priv
->nc
.max_fwd_delay
= 10;
161 bat_priv
->nc
.max_buffer_time
= 200;
165 * batadv_nc_init_orig - initialise the nc fields of an orig_node
166 * @orig_node: the orig_node which is going to be initialised
168 void batadv_nc_init_orig(struct batadv_orig_node
*orig_node
)
170 INIT_LIST_HEAD(&orig_node
->in_coding_list
);
171 INIT_LIST_HEAD(&orig_node
->out_coding_list
);
172 spin_lock_init(&orig_node
->in_coding_list_lock
);
173 spin_lock_init(&orig_node
->out_coding_list_lock
);
177 * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
178 * its refcount on the orig_node
179 * @rcu: rcu pointer of the nc node
181 static void batadv_nc_node_free_rcu(struct rcu_head
*rcu
)
183 struct batadv_nc_node
*nc_node
;
185 nc_node
= container_of(rcu
, struct batadv_nc_node
, rcu
);
186 batadv_orig_node_free_ref(nc_node
->orig_node
);
191 * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
193 * @nc_node: the nc node to free
195 static void batadv_nc_node_free_ref(struct batadv_nc_node
*nc_node
)
197 if (atomic_dec_and_test(&nc_node
->refcount
))
198 call_rcu(&nc_node
->rcu
, batadv_nc_node_free_rcu
);
202 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
204 * @nc_path: the nc node to free
206 static void batadv_nc_path_free_ref(struct batadv_nc_path
*nc_path
)
208 if (atomic_dec_and_test(&nc_path
->refcount
))
209 kfree_rcu(nc_path
, rcu
);
213 * batadv_nc_packet_free - frees nc packet
214 * @nc_packet: the nc packet to free
216 static void batadv_nc_packet_free(struct batadv_nc_packet
*nc_packet
)
219 kfree_skb(nc_packet
->skb
);
221 batadv_nc_path_free_ref(nc_packet
->nc_path
);
226 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
227 * @bat_priv: the bat priv with all the soft interface information
228 * @nc_node: the nc node to check
230 * Returns true if the entry has to be purged now, false otherwise
232 static bool batadv_nc_to_purge_nc_node(struct batadv_priv
*bat_priv
,
233 struct batadv_nc_node
*nc_node
)
235 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
238 return batadv_has_timed_out(nc_node
->last_seen
, BATADV_NC_NODE_TIMEOUT
);
242 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
243 * @bat_priv: the bat priv with all the soft interface information
244 * @nc_path: the nc path to check
246 * Returns true if the entry has to be purged now, false otherwise
248 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv
*bat_priv
,
249 struct batadv_nc_path
*nc_path
)
251 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
254 /* purge the path when no packets has been added for 10 times the
257 return batadv_has_timed_out(nc_path
->last_valid
,
258 bat_priv
->nc
.max_fwd_delay
* 10);
262 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
263 * @bat_priv: the bat priv with all the soft interface information
264 * @nc_path: the nc path to check
266 * Returns true if the entry has to be purged now, false otherwise
268 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv
*bat_priv
,
269 struct batadv_nc_path
*nc_path
)
271 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
274 /* purge the path when no packets has been added for 10 times the
277 return batadv_has_timed_out(nc_path
->last_valid
,
278 bat_priv
->nc
.max_buffer_time
*10);
282 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
284 * @bat_priv: the bat priv with all the soft interface information
285 * @list: list of nc nodes
286 * @lock: nc node list lock
287 * @to_purge: function in charge to decide whether an entry has to be purged or
288 * not. This function takes the nc node as argument and has to return
289 * a boolean value: true if the entry has to be deleted, false
293 batadv_nc_purge_orig_nc_nodes(struct batadv_priv
*bat_priv
,
294 struct list_head
*list
,
296 bool (*to_purge
)(struct batadv_priv
*,
297 struct batadv_nc_node
*))
299 struct batadv_nc_node
*nc_node
, *nc_node_tmp
;
301 /* For each nc_node in list */
303 list_for_each_entry_safe(nc_node
, nc_node_tmp
, list
, list
) {
304 /* if an helper function has been passed as parameter,
305 * ask it if the entry has to be purged or not
307 if (to_purge
&& !to_purge(bat_priv
, nc_node
))
310 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
311 "Removing nc_node %pM -> %pM\n",
312 nc_node
->addr
, nc_node
->orig_node
->orig
);
313 list_del_rcu(&nc_node
->list
);
314 batadv_nc_node_free_ref(nc_node
);
316 spin_unlock_bh(lock
);
320 * batadv_nc_purge_orig - purges all nc node data attached of the given
322 * @bat_priv: the bat priv with all the soft interface information
323 * @orig_node: orig_node with the nc node entries to be purged
324 * @to_purge: function in charge to decide whether an entry has to be purged or
325 * not. This function takes the nc node as argument and has to return
326 * a boolean value: true is the entry has to be deleted, false
329 void batadv_nc_purge_orig(struct batadv_priv
*bat_priv
,
330 struct batadv_orig_node
*orig_node
,
331 bool (*to_purge
)(struct batadv_priv
*,
332 struct batadv_nc_node
*))
334 /* Check ingoing nc_node's of this orig_node */
335 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->in_coding_list
,
336 &orig_node
->in_coding_list_lock
,
339 /* Check outgoing nc_node's of this orig_node */
340 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->out_coding_list
,
341 &orig_node
->out_coding_list_lock
,
346 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
347 * have timed out nc nodes
348 * @bat_priv: the bat priv with all the soft interface information
350 static void batadv_nc_purge_orig_hash(struct batadv_priv
*bat_priv
)
352 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
353 struct hlist_head
*head
;
354 struct batadv_orig_node
*orig_node
;
360 /* For each orig_node */
361 for (i
= 0; i
< hash
->size
; i
++) {
362 head
= &hash
->table
[i
];
365 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
)
366 batadv_nc_purge_orig(bat_priv
, orig_node
,
367 batadv_nc_to_purge_nc_node
);
373 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
375 * @bat_priv: the bat priv with all the soft interface information
376 * @hash: hash table containing the nc paths to check
377 * @to_purge: function in charge to decide whether an entry has to be purged or
378 * not. This function takes the nc node as argument and has to return
379 * a boolean value: true is the entry has to be deleted, false
382 static void batadv_nc_purge_paths(struct batadv_priv
*bat_priv
,
383 struct batadv_hashtable
*hash
,
384 bool (*to_purge
)(struct batadv_priv
*,
385 struct batadv_nc_path
*))
387 struct hlist_head
*head
;
388 struct hlist_node
*node_tmp
;
389 struct batadv_nc_path
*nc_path
;
390 spinlock_t
*lock
; /* Protects lists in hash */
393 for (i
= 0; i
< hash
->size
; i
++) {
394 head
= &hash
->table
[i
];
395 lock
= &hash
->list_locks
[i
];
397 /* For each nc_path in this bin */
399 hlist_for_each_entry_safe(nc_path
, node_tmp
, head
, hash_entry
) {
400 /* if an helper function has been passed as parameter,
401 * ask it if the entry has to be purged or not
403 if (to_purge
&& !to_purge(bat_priv
, nc_path
))
406 /* purging an non-empty nc_path should never happen, but
407 * is observed under high CPU load. Delay the purging
408 * until next iteration to allow the packet_list to be
411 if (!unlikely(list_empty(&nc_path
->packet_list
))) {
412 net_ratelimited_function(printk
,
414 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
420 /* nc_path is unused, so remove it */
421 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
422 "Remove nc_path %pM -> %pM\n",
423 nc_path
->prev_hop
, nc_path
->next_hop
);
424 hlist_del_rcu(&nc_path
->hash_entry
);
425 batadv_nc_path_free_ref(nc_path
);
427 spin_unlock_bh(lock
);
432 * batadv_nc_hash_key_gen - computes the nc_path hash key
433 * @key: buffer to hold the final hash key
434 * @src: source ethernet mac address going into the hash key
435 * @dst: destination ethernet mac address going into the hash key
437 static void batadv_nc_hash_key_gen(struct batadv_nc_path
*key
, const char *src
,
440 memcpy(key
->prev_hop
, src
, sizeof(key
->prev_hop
));
441 memcpy(key
->next_hop
, dst
, sizeof(key
->next_hop
));
445 * batadv_nc_hash_choose - compute the hash value for an nc path
446 * @data: data to hash
447 * @size: size of the hash table
449 * Returns the selected index in the hash table for the given data.
451 static uint32_t batadv_nc_hash_choose(const void *data
, uint32_t size
)
453 const struct batadv_nc_path
*nc_path
= data
;
456 hash
= batadv_hash_bytes(hash
, &nc_path
->prev_hop
,
457 sizeof(nc_path
->prev_hop
));
458 hash
= batadv_hash_bytes(hash
, &nc_path
->next_hop
,
459 sizeof(nc_path
->next_hop
));
462 hash
^= (hash
>> 11);
463 hash
+= (hash
<< 15);
469 * batadv_nc_hash_compare - comparing function used in the network coding hash
471 * @node: node in the local table
472 * @data2: second object to compare the node to
474 * Returns 1 if the two entry are the same, 0 otherwise
476 static int batadv_nc_hash_compare(const struct hlist_node
*node
,
479 const struct batadv_nc_path
*nc_path1
, *nc_path2
;
481 nc_path1
= container_of(node
, struct batadv_nc_path
, hash_entry
);
484 /* Return 1 if the two keys are identical */
485 if (memcmp(nc_path1
->prev_hop
, nc_path2
->prev_hop
,
486 sizeof(nc_path1
->prev_hop
)) != 0)
489 if (memcmp(nc_path1
->next_hop
, nc_path2
->next_hop
,
490 sizeof(nc_path1
->next_hop
)) != 0)
497 * batadv_nc_hash_find - search for an existing nc path and return it
498 * @hash: hash table containing the nc path
501 * Returns the nc_path if found, NULL otherwise.
503 static struct batadv_nc_path
*
504 batadv_nc_hash_find(struct batadv_hashtable
*hash
,
507 struct hlist_head
*head
;
508 struct batadv_nc_path
*nc_path
, *nc_path_tmp
= NULL
;
514 index
= batadv_nc_hash_choose(data
, hash
->size
);
515 head
= &hash
->table
[index
];
518 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
519 if (!batadv_nc_hash_compare(&nc_path
->hash_entry
, data
))
522 if (!atomic_inc_not_zero(&nc_path
->refcount
))
525 nc_path_tmp
= nc_path
;
534 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
535 * @nc_packet: the nc packet to send
537 static void batadv_nc_send_packet(struct batadv_nc_packet
*nc_packet
)
539 batadv_send_skb_packet(nc_packet
->skb
,
540 nc_packet
->neigh_node
->if_incoming
,
541 nc_packet
->nc_path
->next_hop
);
542 nc_packet
->skb
= NULL
;
543 batadv_nc_packet_free(nc_packet
);
547 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
548 * @bat_priv: the bat priv with all the soft interface information
549 * @nc_path: the nc path the packet belongs to
550 * @nc_packet: the nc packet to be checked
552 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
553 * timeout. If so, the packet is no longer kept and the entry deleted from the
554 * queue. Has to be called with the appropriate locks.
556 * Returns false as soon as the entry in the fifo queue has not been timed out
557 * yet and true otherwise.
559 static bool batadv_nc_sniffed_purge(struct batadv_priv
*bat_priv
,
560 struct batadv_nc_path
*nc_path
,
561 struct batadv_nc_packet
*nc_packet
)
563 unsigned long timeout
= bat_priv
->nc
.max_buffer_time
;
566 /* Packets are added to tail, so the remaining packets did not time
567 * out and we can stop processing the current queue
569 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
570 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
573 /* purge nc packet */
574 list_del(&nc_packet
->list
);
575 batadv_nc_packet_free(nc_packet
);
584 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
585 * @bat_priv: the bat priv with all the soft interface information
586 * @nc_path: the nc path the packet belongs to
587 * @nc_packet: the nc packet to be checked
589 * Checks whether the given nc packet has hit its forward timeout. If so, the
590 * packet is no longer delayed, immediately sent and the entry deleted from the
591 * queue. Has to be called with the appropriate locks.
593 * Returns false as soon as the entry in the fifo queue has not been timed out
594 * yet and true otherwise.
596 static bool batadv_nc_fwd_flush(struct batadv_priv
*bat_priv
,
597 struct batadv_nc_path
*nc_path
,
598 struct batadv_nc_packet
*nc_packet
)
600 unsigned long timeout
= bat_priv
->nc
.max_fwd_delay
;
602 /* Packets are added to tail, so the remaining packets did not time
603 * out and we can stop processing the current queue
605 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
606 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
610 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
611 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
612 nc_packet
->skb
->len
+ ETH_HLEN
);
613 list_del(&nc_packet
->list
);
614 batadv_nc_send_packet(nc_packet
);
620 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
622 * @bat_priv: the bat priv with all the soft interface information
623 * @hash: to be processed hash table
624 * @process_fn: Function called to process given nc packet. Should return true
625 * to encourage this function to proceed with the next packet.
626 * Otherwise the rest of the current queue is skipped.
629 batadv_nc_process_nc_paths(struct batadv_priv
*bat_priv
,
630 struct batadv_hashtable
*hash
,
631 bool (*process_fn
)(struct batadv_priv
*,
632 struct batadv_nc_path
*,
633 struct batadv_nc_packet
*))
635 struct hlist_head
*head
;
636 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
637 struct batadv_nc_path
*nc_path
;
644 /* Loop hash table bins */
645 for (i
= 0; i
< hash
->size
; i
++) {
646 head
= &hash
->table
[i
];
648 /* Loop coding paths */
650 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
652 spin_lock_bh(&nc_path
->packet_list_lock
);
653 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
654 &nc_path
->packet_list
, list
) {
655 ret
= process_fn(bat_priv
, nc_path
, nc_packet
);
659 spin_unlock_bh(&nc_path
->packet_list_lock
);
666 * batadv_nc_worker - periodic task for house keeping related to network coding
667 * @work: kernel work struct
669 static void batadv_nc_worker(struct work_struct
*work
)
671 struct delayed_work
*delayed_work
;
672 struct batadv_priv_nc
*priv_nc
;
673 struct batadv_priv
*bat_priv
;
674 unsigned long timeout
;
676 delayed_work
= container_of(work
, struct delayed_work
, work
);
677 priv_nc
= container_of(delayed_work
, struct batadv_priv_nc
, work
);
678 bat_priv
= container_of(priv_nc
, struct batadv_priv
, nc
);
680 batadv_nc_purge_orig_hash(bat_priv
);
681 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
682 batadv_nc_to_purge_nc_path_coding
);
683 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
684 batadv_nc_to_purge_nc_path_decoding
);
686 timeout
= bat_priv
->nc
.max_fwd_delay
;
688 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_fwd_flush
, timeout
)) {
689 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
690 batadv_nc_fwd_flush
);
691 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
694 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_sniffed_purge
,
695 bat_priv
->nc
.max_buffer_time
)) {
696 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
697 batadv_nc_sniffed_purge
);
698 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
701 /* Schedule a new check */
702 batadv_nc_start_timer(bat_priv
);
706 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
708 * @bat_priv: the bat priv with all the soft interface information
709 * @orig_node: neighboring orig node which may be used as nc candidate
710 * @ogm_packet: incoming ogm packet also used for the checks
713 * 1) The OGM must have the most recent sequence number.
714 * 2) The TTL must be decremented by one and only one.
715 * 3) The OGM must be received from the first hop from orig_node.
716 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
718 static bool batadv_can_nc_with_orig(struct batadv_priv
*bat_priv
,
719 struct batadv_orig_node
*orig_node
,
720 struct batadv_ogm_packet
*ogm_packet
)
722 struct batadv_orig_ifinfo
*orig_ifinfo
;
723 uint32_t last_real_seqno
;
726 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, BATADV_IF_DEFAULT
);
730 last_ttl
= orig_ifinfo
->last_ttl
;
731 last_real_seqno
= orig_ifinfo
->last_real_seqno
;
732 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
734 if (last_real_seqno
!= ntohl(ogm_packet
->seqno
))
736 if (last_ttl
!= ogm_packet
->ttl
+ 1)
738 if (!batadv_compare_eth(ogm_packet
->orig
, ogm_packet
->prev_sender
))
740 if (ogm_packet
->tq
< bat_priv
->nc
.min_tq
)
747 * batadv_nc_find_nc_node - search for an existing nc node and return it
748 * @orig_node: orig node originating the ogm packet
749 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
750 * (can be equal to orig_node)
751 * @in_coding: traverse incoming or outgoing network coding list
753 * Returns the nc_node if found, NULL otherwise.
755 static struct batadv_nc_node
756 *batadv_nc_find_nc_node(struct batadv_orig_node
*orig_node
,
757 struct batadv_orig_node
*orig_neigh_node
,
760 struct batadv_nc_node
*nc_node
, *nc_node_out
= NULL
;
761 struct list_head
*list
;
764 list
= &orig_neigh_node
->in_coding_list
;
766 list
= &orig_neigh_node
->out_coding_list
;
768 /* Traverse list of nc_nodes to orig_node */
770 list_for_each_entry_rcu(nc_node
, list
, list
) {
771 if (!batadv_compare_eth(nc_node
->addr
, orig_node
->orig
))
774 if (!atomic_inc_not_zero(&nc_node
->refcount
))
778 nc_node_out
= nc_node
;
787 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
789 * @bat_priv: the bat priv with all the soft interface information
790 * @orig_node: orig node originating the ogm packet
791 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
792 * (can be equal to orig_node)
793 * @in_coding: traverse incoming or outgoing network coding list
795 * Returns the nc_node if found or created, NULL in case of an error.
797 static struct batadv_nc_node
798 *batadv_nc_get_nc_node(struct batadv_priv
*bat_priv
,
799 struct batadv_orig_node
*orig_node
,
800 struct batadv_orig_node
*orig_neigh_node
,
803 struct batadv_nc_node
*nc_node
;
804 spinlock_t
*lock
; /* Used to lock list selected by "int in_coding" */
805 struct list_head
*list
;
807 /* Check if nc_node is already added */
808 nc_node
= batadv_nc_find_nc_node(orig_node
, orig_neigh_node
, in_coding
);
814 nc_node
= kzalloc(sizeof(*nc_node
), GFP_ATOMIC
);
818 if (!atomic_inc_not_zero(&orig_neigh_node
->refcount
))
821 /* Initialize nc_node */
822 INIT_LIST_HEAD(&nc_node
->list
);
823 ether_addr_copy(nc_node
->addr
, orig_node
->orig
);
824 nc_node
->orig_node
= orig_neigh_node
;
825 atomic_set(&nc_node
->refcount
, 2);
827 /* Select ingoing or outgoing coding node */
829 lock
= &orig_neigh_node
->in_coding_list_lock
;
830 list
= &orig_neigh_node
->in_coding_list
;
832 lock
= &orig_neigh_node
->out_coding_list_lock
;
833 list
= &orig_neigh_node
->out_coding_list
;
836 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_node %pM -> %pM\n",
837 nc_node
->addr
, nc_node
->orig_node
->orig
);
839 /* Add nc_node to orig_node */
841 list_add_tail_rcu(&nc_node
->list
, list
);
842 spin_unlock_bh(lock
);
852 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
853 * (best called on incoming OGMs)
854 * @bat_priv: the bat priv with all the soft interface information
855 * @orig_node: orig node originating the ogm packet
856 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
857 * (can be equal to orig_node)
858 * @ogm_packet: incoming ogm packet
859 * @is_single_hop_neigh: orig_node is a single hop neighbor
861 void batadv_nc_update_nc_node(struct batadv_priv
*bat_priv
,
862 struct batadv_orig_node
*orig_node
,
863 struct batadv_orig_node
*orig_neigh_node
,
864 struct batadv_ogm_packet
*ogm_packet
,
865 int is_single_hop_neigh
)
867 struct batadv_nc_node
*in_nc_node
= NULL
, *out_nc_node
= NULL
;
869 /* Check if network coding is enabled */
870 if (!atomic_read(&bat_priv
->network_coding
))
873 /* check if orig node is network coding enabled */
874 if (!(orig_node
->capabilities
& BATADV_ORIG_CAPA_HAS_NC
))
877 /* accept ogms from 'good' neighbors and single hop neighbors */
878 if (!batadv_can_nc_with_orig(bat_priv
, orig_node
, ogm_packet
) &&
879 !is_single_hop_neigh
)
882 /* Add orig_node as in_nc_node on hop */
883 in_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_node
,
884 orig_neigh_node
, true);
888 in_nc_node
->last_seen
= jiffies
;
890 /* Add hop as out_nc_node on orig_node */
891 out_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_neigh_node
,
896 out_nc_node
->last_seen
= jiffies
;
900 batadv_nc_node_free_ref(in_nc_node
);
902 batadv_nc_node_free_ref(out_nc_node
);
906 * batadv_nc_get_path - get existing nc_path or allocate a new one
907 * @bat_priv: the bat priv with all the soft interface information
908 * @hash: hash table containing the nc path
909 * @src: ethernet source address - first half of the nc path search key
910 * @dst: ethernet destination address - second half of the nc path search key
912 * Returns pointer to nc_path if the path was found or created, returns NULL
915 static struct batadv_nc_path
*batadv_nc_get_path(struct batadv_priv
*bat_priv
,
916 struct batadv_hashtable
*hash
,
921 struct batadv_nc_path
*nc_path
, nc_path_key
;
923 batadv_nc_hash_key_gen(&nc_path_key
, src
, dst
);
925 /* Search for existing nc_path */
926 nc_path
= batadv_nc_hash_find(hash
, (void *)&nc_path_key
);
929 /* Set timestamp to delay removal of nc_path */
930 nc_path
->last_valid
= jiffies
;
934 /* No existing nc_path was found; create a new */
935 nc_path
= kzalloc(sizeof(*nc_path
), GFP_ATOMIC
);
940 /* Initialize nc_path */
941 INIT_LIST_HEAD(&nc_path
->packet_list
);
942 spin_lock_init(&nc_path
->packet_list_lock
);
943 atomic_set(&nc_path
->refcount
, 2);
944 nc_path
->last_valid
= jiffies
;
945 ether_addr_copy(nc_path
->next_hop
, dst
);
946 ether_addr_copy(nc_path
->prev_hop
, src
);
948 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_path %pM -> %pM\n",
952 /* Add nc_path to hash table */
953 hash_added
= batadv_hash_add(hash
, batadv_nc_hash_compare
,
954 batadv_nc_hash_choose
, &nc_path_key
,
955 &nc_path
->hash_entry
);
957 if (hash_added
< 0) {
966 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
967 * selection of a receiver with slightly lower TQ than the other
968 * @tq: to be weighted tq value
970 static uint8_t batadv_nc_random_weight_tq(uint8_t tq
)
972 uint8_t rand_val
, rand_tq
;
974 get_random_bytes(&rand_val
, sizeof(rand_val
));
976 /* randomize the estimated packet loss (max TQ - estimated TQ) */
977 rand_tq
= rand_val
* (BATADV_TQ_MAX_VALUE
- tq
);
979 /* normalize the randomized packet loss */
980 rand_tq
/= BATADV_TQ_MAX_VALUE
;
982 /* convert to (randomized) estimated tq again */
983 return BATADV_TQ_MAX_VALUE
- rand_tq
;
987 * batadv_nc_memxor - XOR destination with source
988 * @dst: byte array to XOR into
989 * @src: byte array to XOR from
990 * @len: length of destination array
992 static void batadv_nc_memxor(char *dst
, const char *src
, unsigned int len
)
996 for (i
= 0; i
< len
; ++i
)
1001 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
1002 * into a coded_packet and send it
1003 * @bat_priv: the bat priv with all the soft interface information
1004 * @skb: data skb to forward
1005 * @ethhdr: pointer to the ethernet header inside the skb
1006 * @nc_packet: structure containing the packet to the skb can be coded with
1007 * @neigh_node: next hop to forward packet to
1009 * Returns true if both packets are consumed, false otherwise.
1011 static bool batadv_nc_code_packets(struct batadv_priv
*bat_priv
,
1012 struct sk_buff
*skb
,
1013 struct ethhdr
*ethhdr
,
1014 struct batadv_nc_packet
*nc_packet
,
1015 struct batadv_neigh_node
*neigh_node
)
1017 uint8_t tq_weighted_neigh
, tq_weighted_coding
, tq_tmp
;
1018 struct sk_buff
*skb_dest
, *skb_src
;
1019 struct batadv_unicast_packet
*packet1
;
1020 struct batadv_unicast_packet
*packet2
;
1021 struct batadv_coded_packet
*coded_packet
;
1022 struct batadv_neigh_node
*neigh_tmp
, *router_neigh
;
1023 struct batadv_neigh_node
*router_coding
= NULL
;
1024 struct batadv_neigh_ifinfo
*router_neigh_ifinfo
= NULL
;
1025 struct batadv_neigh_ifinfo
*router_coding_ifinfo
= NULL
;
1026 uint8_t *first_source
, *first_dest
, *second_source
, *second_dest
;
1027 __be32 packet_id1
, packet_id2
;
1031 int unicast_size
= sizeof(*packet1
);
1032 int coded_size
= sizeof(*coded_packet
);
1033 int header_add
= coded_size
- unicast_size
;
1035 /* TODO: do we need to consider the outgoing interface for
1038 router_neigh
= batadv_orig_router_get(neigh_node
->orig_node
,
1043 router_neigh_ifinfo
= batadv_neigh_ifinfo_get(router_neigh
,
1045 if (!router_neigh_ifinfo
)
1048 neigh_tmp
= nc_packet
->neigh_node
;
1049 router_coding
= batadv_orig_router_get(neigh_tmp
->orig_node
,
1054 router_coding_ifinfo
= batadv_neigh_ifinfo_get(router_coding
,
1056 if (!router_coding_ifinfo
)
1059 tq_tmp
= router_neigh_ifinfo
->bat_iv
.tq_avg
;
1060 tq_weighted_neigh
= batadv_nc_random_weight_tq(tq_tmp
);
1061 tq_tmp
= router_coding_ifinfo
->bat_iv
.tq_avg
;
1062 tq_weighted_coding
= batadv_nc_random_weight_tq(tq_tmp
);
1064 /* Select one destination for the MAC-header dst-field based on
1065 * weighted TQ-values.
1067 if (tq_weighted_neigh
>= tq_weighted_coding
) {
1068 /* Destination from nc_packet is selected for MAC-header */
1069 first_dest
= nc_packet
->nc_path
->next_hop
;
1070 first_source
= nc_packet
->nc_path
->prev_hop
;
1071 second_dest
= neigh_node
->addr
;
1072 second_source
= ethhdr
->h_source
;
1073 packet1
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1074 packet2
= (struct batadv_unicast_packet
*)skb
->data
;
1075 packet_id1
= nc_packet
->packet_id
;
1076 packet_id2
= batadv_skb_crc32(skb
,
1077 skb
->data
+ sizeof(*packet2
));
1079 /* Destination for skb is selected for MAC-header */
1080 first_dest
= neigh_node
->addr
;
1081 first_source
= ethhdr
->h_source
;
1082 second_dest
= nc_packet
->nc_path
->next_hop
;
1083 second_source
= nc_packet
->nc_path
->prev_hop
;
1084 packet1
= (struct batadv_unicast_packet
*)skb
->data
;
1085 packet2
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1086 packet_id1
= batadv_skb_crc32(skb
,
1087 skb
->data
+ sizeof(*packet1
));
1088 packet_id2
= nc_packet
->packet_id
;
1091 /* Instead of zero padding the smallest data buffer, we
1092 * code into the largest.
1094 if (skb
->len
<= nc_packet
->skb
->len
) {
1095 skb_dest
= nc_packet
->skb
;
1099 skb_src
= nc_packet
->skb
;
1102 /* coding_len is used when decoding the packet shorter packet */
1103 coding_len
= skb_src
->len
- unicast_size
;
1105 if (skb_linearize(skb_dest
) < 0 || skb_linearize(skb_src
) < 0)
1108 skb_push(skb_dest
, header_add
);
1110 coded_packet
= (struct batadv_coded_packet
*)skb_dest
->data
;
1111 skb_reset_mac_header(skb_dest
);
1113 coded_packet
->packet_type
= BATADV_CODED
;
1114 coded_packet
->version
= BATADV_COMPAT_VERSION
;
1115 coded_packet
->ttl
= packet1
->ttl
;
1117 /* Info about first unicast packet */
1118 ether_addr_copy(coded_packet
->first_source
, first_source
);
1119 ether_addr_copy(coded_packet
->first_orig_dest
, packet1
->dest
);
1120 coded_packet
->first_crc
= packet_id1
;
1121 coded_packet
->first_ttvn
= packet1
->ttvn
;
1123 /* Info about second unicast packet */
1124 ether_addr_copy(coded_packet
->second_dest
, second_dest
);
1125 ether_addr_copy(coded_packet
->second_source
, second_source
);
1126 ether_addr_copy(coded_packet
->second_orig_dest
, packet2
->dest
);
1127 coded_packet
->second_crc
= packet_id2
;
1128 coded_packet
->second_ttl
= packet2
->ttl
;
1129 coded_packet
->second_ttvn
= packet2
->ttvn
;
1130 coded_packet
->coded_len
= htons(coding_len
);
1132 /* This is where the magic happens: Code skb_src into skb_dest */
1133 batadv_nc_memxor(skb_dest
->data
+ coded_size
,
1134 skb_src
->data
+ unicast_size
, coding_len
);
1136 /* Update counters accordingly */
1137 if (BATADV_SKB_CB(skb_src
)->decoded
&&
1138 BATADV_SKB_CB(skb_dest
)->decoded
) {
1139 /* Both packets are recoded */
1140 count
= skb_src
->len
+ ETH_HLEN
;
1141 count
+= skb_dest
->len
+ ETH_HLEN
;
1142 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE
, 2);
1143 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
, count
);
1144 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1145 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1146 /* Both packets are newly coded */
1147 count
= skb_src
->len
+ ETH_HLEN
;
1148 count
+= skb_dest
->len
+ ETH_HLEN
;
1149 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE
, 2);
1150 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
, count
);
1151 } else if (BATADV_SKB_CB(skb_src
)->decoded
&&
1152 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1153 /* skb_src recoded and skb_dest is newly coded */
1154 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1155 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1156 skb_src
->len
+ ETH_HLEN
);
1157 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1158 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1159 skb_dest
->len
+ ETH_HLEN
);
1160 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1161 BATADV_SKB_CB(skb_dest
)->decoded
) {
1162 /* skb_src is newly coded and skb_dest is recoded */
1163 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1164 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1165 skb_src
->len
+ ETH_HLEN
);
1166 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1167 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1168 skb_dest
->len
+ ETH_HLEN
);
1171 /* skb_src is now coded into skb_dest, so free it */
1174 /* avoid duplicate free of skb from nc_packet */
1175 nc_packet
->skb
= NULL
;
1176 batadv_nc_packet_free(nc_packet
);
1178 /* Send the coded packet and return true */
1179 batadv_send_skb_packet(skb_dest
, neigh_node
->if_incoming
, first_dest
);
1183 batadv_neigh_node_free_ref(router_neigh
);
1185 batadv_neigh_node_free_ref(router_coding
);
1186 if (router_neigh_ifinfo
)
1187 batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo
);
1188 if (router_coding_ifinfo
)
1189 batadv_neigh_ifinfo_free_ref(router_coding_ifinfo
);
1194 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1195 * @skb: data skb to forward
1196 * @dst: destination mac address of the other skb to code with
1197 * @src: source mac address of skb
1199 * Whenever we network code a packet we have to check whether we received it in
1200 * a network coded form. If so, we may not be able to use it for coding because
1201 * some neighbors may also have received (overheard) the packet in the network
1202 * coded form without being able to decode it. It is hard to know which of the
1203 * neighboring nodes was able to decode the packet, therefore we can only
1204 * re-code the packet if the source of the previous encoded packet is involved.
1205 * Since the source encoded the packet we can be certain it has all necessary
1206 * decode information.
1208 * Returns true if coding of a decoded packet is allowed.
1210 static bool batadv_nc_skb_coding_possible(struct sk_buff
*skb
,
1211 uint8_t *dst
, uint8_t *src
)
1213 if (BATADV_SKB_CB(skb
)->decoded
&& !batadv_compare_eth(dst
, src
))
1220 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1221 * out_nc_node to retrieve a buffered packet that can be used for coding.
1222 * @bat_priv: the bat priv with all the soft interface information
1223 * @in_nc_node: pointer to skb next hop's neighbor nc node
1224 * @out_nc_node: pointer to skb source's neighbor nc node
1225 * @skb: data skb to forward
1226 * @eth_dst: next hop mac address of skb
1228 * Returns true if coding of a decoded skb is allowed.
1230 static struct batadv_nc_packet
*
1231 batadv_nc_path_search(struct batadv_priv
*bat_priv
,
1232 struct batadv_nc_node
*in_nc_node
,
1233 struct batadv_nc_node
*out_nc_node
,
1234 struct sk_buff
*skb
,
1237 struct batadv_nc_path
*nc_path
, nc_path_key
;
1238 struct batadv_nc_packet
*nc_packet_out
= NULL
;
1239 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
1240 struct batadv_hashtable
*hash
= bat_priv
->nc
.coding_hash
;
1246 /* Create almost path key */
1247 batadv_nc_hash_key_gen(&nc_path_key
, in_nc_node
->addr
,
1249 idx
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1251 /* Check for coding opportunities in this nc_path */
1253 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[idx
], hash_entry
) {
1254 if (!batadv_compare_eth(nc_path
->prev_hop
, in_nc_node
->addr
))
1257 if (!batadv_compare_eth(nc_path
->next_hop
, out_nc_node
->addr
))
1260 spin_lock_bh(&nc_path
->packet_list_lock
);
1261 if (list_empty(&nc_path
->packet_list
)) {
1262 spin_unlock_bh(&nc_path
->packet_list_lock
);
1266 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
1267 &nc_path
->packet_list
, list
) {
1268 if (!batadv_nc_skb_coding_possible(nc_packet
->skb
,
1273 /* Coding opportunity is found! */
1274 list_del(&nc_packet
->list
);
1275 nc_packet_out
= nc_packet
;
1279 spin_unlock_bh(&nc_path
->packet_list_lock
);
1284 return nc_packet_out
;
1288 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1289 * skb's sender (may be equal to the originator).
1290 * @bat_priv: the bat priv with all the soft interface information
1291 * @skb: data skb to forward
1292 * @eth_dst: next hop mac address of skb
1293 * @eth_src: source mac address of skb
1294 * @in_nc_node: pointer to skb next hop's neighbor nc node
1296 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1298 static struct batadv_nc_packet
*
1299 batadv_nc_skb_src_search(struct batadv_priv
*bat_priv
,
1300 struct sk_buff
*skb
,
1303 struct batadv_nc_node
*in_nc_node
)
1305 struct batadv_orig_node
*orig_node
;
1306 struct batadv_nc_node
*out_nc_node
;
1307 struct batadv_nc_packet
*nc_packet
= NULL
;
1309 orig_node
= batadv_orig_hash_find(bat_priv
, eth_src
);
1314 list_for_each_entry_rcu(out_nc_node
,
1315 &orig_node
->out_coding_list
, list
) {
1316 /* Check if the skb is decoded and if recoding is possible */
1317 if (!batadv_nc_skb_coding_possible(skb
,
1318 out_nc_node
->addr
, eth_src
))
1321 /* Search for an opportunity in this nc_path */
1322 nc_packet
= batadv_nc_path_search(bat_priv
, in_nc_node
,
1323 out_nc_node
, skb
, eth_dst
);
1329 batadv_orig_node_free_ref(orig_node
);
1334 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1335 * unicast skb before it is stored for use in later decoding
1336 * @bat_priv: the bat priv with all the soft interface information
1337 * @skb: data skb to store
1338 * @eth_dst_new: new destination mac address of skb
1340 static void batadv_nc_skb_store_before_coding(struct batadv_priv
*bat_priv
,
1341 struct sk_buff
*skb
,
1342 uint8_t *eth_dst_new
)
1344 struct ethhdr
*ethhdr
;
1346 /* Copy skb header to change the mac header */
1347 skb
= pskb_copy_for_clone(skb
, GFP_ATOMIC
);
1351 /* Set the mac header as if we actually sent the packet uncoded */
1352 ethhdr
= eth_hdr(skb
);
1353 ether_addr_copy(ethhdr
->h_source
, ethhdr
->h_dest
);
1354 ether_addr_copy(ethhdr
->h_dest
, eth_dst_new
);
1356 /* Set data pointer to MAC header to mimic packets from our tx path */
1357 skb_push(skb
, ETH_HLEN
);
1359 /* Add the packet to the decoding packet pool */
1360 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1362 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1369 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1370 * @skb: data skb to forward
1371 * @neigh_node: next hop to forward packet to
1372 * @ethhdr: pointer to the ethernet header inside the skb
1374 * Loops through list of neighboring nodes the next hop has a good connection to
1375 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1376 * next hop that potentially sent a packet which our next hop also received
1377 * (overheard) and has stored for later decoding.
1379 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1381 static bool batadv_nc_skb_dst_search(struct sk_buff
*skb
,
1382 struct batadv_neigh_node
*neigh_node
,
1383 struct ethhdr
*ethhdr
)
1385 struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1386 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1387 struct batadv_orig_node
*orig_node
= neigh_node
->orig_node
;
1388 struct batadv_nc_node
*nc_node
;
1389 struct batadv_nc_packet
*nc_packet
= NULL
;
1392 list_for_each_entry_rcu(nc_node
, &orig_node
->in_coding_list
, list
) {
1393 /* Search for coding opportunity with this in_nc_node */
1394 nc_packet
= batadv_nc_skb_src_search(bat_priv
, skb
,
1396 ethhdr
->h_source
, nc_node
);
1398 /* Opportunity was found, so stop searching */
1407 /* Save packets for later decoding */
1408 batadv_nc_skb_store_before_coding(bat_priv
, skb
,
1410 batadv_nc_skb_store_before_coding(bat_priv
, nc_packet
->skb
,
1411 nc_packet
->neigh_node
->addr
);
1413 /* Code and send packets */
1414 if (batadv_nc_code_packets(bat_priv
, skb
, ethhdr
, nc_packet
,
1418 /* out of mem ? Coding failed - we have to free the buffered packet
1419 * to avoid memleaks. The skb passed as argument will be dealt with
1420 * by the calling function.
1422 batadv_nc_send_packet(nc_packet
);
1427 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1428 * @skb: skb to add to path
1429 * @nc_path: path to add skb to
1430 * @neigh_node: next hop to forward packet to
1431 * @packet_id: checksum to identify packet
1433 * Returns true if the packet was buffered or false in case of an error.
1435 static bool batadv_nc_skb_add_to_path(struct sk_buff
*skb
,
1436 struct batadv_nc_path
*nc_path
,
1437 struct batadv_neigh_node
*neigh_node
,
1440 struct batadv_nc_packet
*nc_packet
;
1442 nc_packet
= kzalloc(sizeof(*nc_packet
), GFP_ATOMIC
);
1446 /* Initialize nc_packet */
1447 nc_packet
->timestamp
= jiffies
;
1448 nc_packet
->packet_id
= packet_id
;
1449 nc_packet
->skb
= skb
;
1450 nc_packet
->neigh_node
= neigh_node
;
1451 nc_packet
->nc_path
= nc_path
;
1453 /* Add coding packet to list */
1454 spin_lock_bh(&nc_path
->packet_list_lock
);
1455 list_add_tail(&nc_packet
->list
, &nc_path
->packet_list
);
1456 spin_unlock_bh(&nc_path
->packet_list_lock
);
1462 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1464 * @skb: data skb to forward
1465 * @neigh_node: next hop to forward packet to
1467 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1469 bool batadv_nc_skb_forward(struct sk_buff
*skb
,
1470 struct batadv_neigh_node
*neigh_node
)
1472 const struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1473 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1474 struct batadv_unicast_packet
*packet
;
1475 struct batadv_nc_path
*nc_path
;
1476 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1480 /* Check if network coding is enabled */
1481 if (!atomic_read(&bat_priv
->network_coding
))
1484 /* We only handle unicast packets */
1485 payload
= skb_network_header(skb
);
1486 packet
= (struct batadv_unicast_packet
*)payload
;
1487 if (packet
->packet_type
!= BATADV_UNICAST
)
1490 /* Try to find a coding opportunity and send the skb if one is found */
1491 if (batadv_nc_skb_dst_search(skb
, neigh_node
, ethhdr
))
1494 /* Find or create a nc_path for this src-dst pair */
1495 nc_path
= batadv_nc_get_path(bat_priv
,
1496 bat_priv
->nc
.coding_hash
,
1503 /* Add skb to nc_path */
1504 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1505 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, neigh_node
, packet_id
))
1508 /* Packet is consumed */
1512 batadv_nc_path_free_ref(nc_path
);
1514 /* Packet is not consumed */
1519 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1520 * when decoding coded packets
1521 * @bat_priv: the bat priv with all the soft interface information
1522 * @skb: data skb to store
1524 void batadv_nc_skb_store_for_decoding(struct batadv_priv
*bat_priv
,
1525 struct sk_buff
*skb
)
1527 struct batadv_unicast_packet
*packet
;
1528 struct batadv_nc_path
*nc_path
;
1529 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1533 /* Check if network coding is enabled */
1534 if (!atomic_read(&bat_priv
->network_coding
))
1537 /* Check for supported packet type */
1538 payload
= skb_network_header(skb
);
1539 packet
= (struct batadv_unicast_packet
*)payload
;
1540 if (packet
->packet_type
!= BATADV_UNICAST
)
1543 /* Find existing nc_path or create a new */
1544 nc_path
= batadv_nc_get_path(bat_priv
,
1545 bat_priv
->nc
.decoding_hash
,
1552 /* Clone skb and adjust skb->data to point at batman header */
1553 skb
= skb_clone(skb
, GFP_ATOMIC
);
1557 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
1560 if (unlikely(!skb_pull_rcsum(skb
, ETH_HLEN
)))
1563 /* Add skb to nc_path */
1564 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1565 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, NULL
, packet_id
))
1568 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_BUFFER
);
1574 batadv_nc_path_free_ref(nc_path
);
1580 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1581 * should be saved in the decoding buffer and, if so, store it there
1582 * @bat_priv: the bat priv with all the soft interface information
1583 * @skb: unicast skb to store
1585 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv
*bat_priv
,
1586 struct sk_buff
*skb
)
1588 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1590 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1593 /* Set data pointer to MAC header to mimic packets from our tx path */
1594 skb_push(skb
, ETH_HLEN
);
1596 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1600 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1602 * @bat_priv: the bat priv with all the soft interface information
1603 * @skb: unicast skb to decode
1604 * @nc_packet: decode data needed to decode the skb
1606 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1607 * in case of an error.
1609 static struct batadv_unicast_packet
*
1610 batadv_nc_skb_decode_packet(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1611 struct batadv_nc_packet
*nc_packet
)
1613 const int h_size
= sizeof(struct batadv_unicast_packet
);
1614 const int h_diff
= sizeof(struct batadv_coded_packet
) - h_size
;
1615 struct batadv_unicast_packet
*unicast_packet
;
1616 struct batadv_coded_packet coded_packet_tmp
;
1617 struct ethhdr
*ethhdr
, ethhdr_tmp
;
1618 uint8_t *orig_dest
, ttl
, ttvn
;
1619 unsigned int coding_len
;
1622 /* Save headers temporarily */
1623 memcpy(&coded_packet_tmp
, skb
->data
, sizeof(coded_packet_tmp
));
1624 memcpy(ðhdr_tmp
, skb_mac_header(skb
), sizeof(ethhdr_tmp
));
1626 if (skb_cow(skb
, 0) < 0)
1629 if (unlikely(!skb_pull_rcsum(skb
, h_diff
)))
1632 /* Data points to batman header, so set mac header 14 bytes before
1633 * and network to data
1635 skb_set_mac_header(skb
, -ETH_HLEN
);
1636 skb_reset_network_header(skb
);
1638 /* Reconstruct original mac header */
1639 ethhdr
= eth_hdr(skb
);
1640 *ethhdr
= ethhdr_tmp
;
1642 /* Select the correct unicast header information based on the location
1643 * of our mac address in the coded_packet header
1645 if (batadv_is_my_mac(bat_priv
, coded_packet_tmp
.second_dest
)) {
1646 /* If we are the second destination the packet was overheard,
1647 * so the Ethernet address must be copied to h_dest and
1648 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1650 ether_addr_copy(ethhdr
->h_dest
, coded_packet_tmp
.second_dest
);
1651 skb
->pkt_type
= PACKET_HOST
;
1653 orig_dest
= coded_packet_tmp
.second_orig_dest
;
1654 ttl
= coded_packet_tmp
.second_ttl
;
1655 ttvn
= coded_packet_tmp
.second_ttvn
;
1657 orig_dest
= coded_packet_tmp
.first_orig_dest
;
1658 ttl
= coded_packet_tmp
.ttl
;
1659 ttvn
= coded_packet_tmp
.first_ttvn
;
1662 coding_len
= ntohs(coded_packet_tmp
.coded_len
);
1664 if (coding_len
> skb
->len
)
1667 /* Here the magic is reversed:
1668 * extract the missing packet from the received coded packet
1670 batadv_nc_memxor(skb
->data
+ h_size
,
1671 nc_packet
->skb
->data
+ h_size
,
1674 /* Resize decoded skb if decoded with larger packet */
1675 if (nc_packet
->skb
->len
> coding_len
+ h_size
) {
1676 err
= pskb_trim_rcsum(skb
, coding_len
+ h_size
);
1681 /* Create decoded unicast packet */
1682 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1683 unicast_packet
->packet_type
= BATADV_UNICAST
;
1684 unicast_packet
->version
= BATADV_COMPAT_VERSION
;
1685 unicast_packet
->ttl
= ttl
;
1686 ether_addr_copy(unicast_packet
->dest
, orig_dest
);
1687 unicast_packet
->ttvn
= ttvn
;
1689 batadv_nc_packet_free(nc_packet
);
1690 return unicast_packet
;
1694 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1695 * find the data needed to decode the coded packet
1696 * @bat_priv: the bat priv with all the soft interface information
1697 * @ethhdr: pointer to the ethernet header inside the coded packet
1698 * @coded: coded packet we try to find decode data for
1700 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1702 static struct batadv_nc_packet
*
1703 batadv_nc_find_decoding_packet(struct batadv_priv
*bat_priv
,
1704 struct ethhdr
*ethhdr
,
1705 struct batadv_coded_packet
*coded
)
1707 struct batadv_hashtable
*hash
= bat_priv
->nc
.decoding_hash
;
1708 struct batadv_nc_packet
*tmp_nc_packet
, *nc_packet
= NULL
;
1709 struct batadv_nc_path
*nc_path
, nc_path_key
;
1710 uint8_t *dest
, *source
;
1717 /* Select the correct packet id based on the location of our mac-addr */
1718 dest
= ethhdr
->h_source
;
1719 if (!batadv_is_my_mac(bat_priv
, coded
->second_dest
)) {
1720 source
= coded
->second_source
;
1721 packet_id
= coded
->second_crc
;
1723 source
= coded
->first_source
;
1724 packet_id
= coded
->first_crc
;
1727 batadv_nc_hash_key_gen(&nc_path_key
, source
, dest
);
1728 index
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1730 /* Search for matching coding path */
1732 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[index
], hash_entry
) {
1733 /* Find matching nc_packet */
1734 spin_lock_bh(&nc_path
->packet_list_lock
);
1735 list_for_each_entry(tmp_nc_packet
,
1736 &nc_path
->packet_list
, list
) {
1737 if (packet_id
== tmp_nc_packet
->packet_id
) {
1738 list_del(&tmp_nc_packet
->list
);
1740 nc_packet
= tmp_nc_packet
;
1744 spin_unlock_bh(&nc_path
->packet_list_lock
);
1752 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
1753 "No decoding packet found for %u\n", packet_id
);
1759 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1760 * resulting unicast packet
1761 * @skb: incoming coded packet
1762 * @recv_if: pointer to interface this packet was received on
1764 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
1765 struct batadv_hard_iface
*recv_if
)
1767 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1768 struct batadv_unicast_packet
*unicast_packet
;
1769 struct batadv_coded_packet
*coded_packet
;
1770 struct batadv_nc_packet
*nc_packet
;
1771 struct ethhdr
*ethhdr
;
1772 int hdr_size
= sizeof(*coded_packet
);
1774 /* Check if network coding is enabled */
1775 if (!atomic_read(&bat_priv
->network_coding
))
1778 /* Make sure we can access (and remove) header */
1779 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1782 coded_packet
= (struct batadv_coded_packet
*)skb
->data
;
1783 ethhdr
= eth_hdr(skb
);
1785 /* Verify frame is destined for us */
1786 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
) &&
1787 !batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1790 /* Update stat counter */
1791 if (batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1792 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_SNIFFED
);
1794 nc_packet
= batadv_nc_find_decoding_packet(bat_priv
, ethhdr
,
1797 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1801 /* Make skb's linear, because decoding accesses the entire buffer */
1802 if (skb_linearize(skb
) < 0)
1803 goto free_nc_packet
;
1805 if (skb_linearize(nc_packet
->skb
) < 0)
1806 goto free_nc_packet
;
1808 /* Decode the packet */
1809 unicast_packet
= batadv_nc_skb_decode_packet(bat_priv
, skb
, nc_packet
);
1810 if (!unicast_packet
) {
1811 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1812 goto free_nc_packet
;
1815 /* Mark packet as decoded to do correct recoding when forwarding */
1816 BATADV_SKB_CB(skb
)->decoded
= true;
1817 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE
);
1818 batadv_add_counter(bat_priv
, BATADV_CNT_NC_DECODE_BYTES
,
1819 skb
->len
+ ETH_HLEN
);
1820 return batadv_recv_unicast_packet(skb
, recv_if
);
1823 batadv_nc_packet_free(nc_packet
);
1828 * batadv_nc_mesh_free - clean up network coding memory
1829 * @bat_priv: the bat priv with all the soft interface information
1831 void batadv_nc_mesh_free(struct batadv_priv
*bat_priv
)
1833 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1834 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1835 cancel_delayed_work_sync(&bat_priv
->nc
.work
);
1837 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
, NULL
);
1838 batadv_hash_destroy(bat_priv
->nc
.coding_hash
);
1839 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
, NULL
);
1840 batadv_hash_destroy(bat_priv
->nc
.decoding_hash
);
1844 * batadv_nc_nodes_seq_print_text - print the nc node information
1845 * @seq: seq file to print on
1848 int batadv_nc_nodes_seq_print_text(struct seq_file
*seq
, void *offset
)
1850 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1851 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1852 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1853 struct batadv_hard_iface
*primary_if
;
1854 struct hlist_head
*head
;
1855 struct batadv_orig_node
*orig_node
;
1856 struct batadv_nc_node
*nc_node
;
1859 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1863 /* Traverse list of originators */
1864 for (i
= 0; i
< hash
->size
; i
++) {
1865 head
= &hash
->table
[i
];
1867 /* For each orig_node in this bin */
1869 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1870 /* no need to print the orig node if it does not have
1871 * network coding neighbors
1873 if (list_empty(&orig_node
->in_coding_list
) &&
1874 list_empty(&orig_node
->out_coding_list
))
1877 seq_printf(seq
, "Node: %pM\n", orig_node
->orig
);
1879 seq_puts(seq
, " Ingoing: ");
1880 /* For each in_nc_node to this orig_node */
1881 list_for_each_entry_rcu(nc_node
,
1882 &orig_node
->in_coding_list
,
1884 seq_printf(seq
, "%pM ",
1886 seq_puts(seq
, "\n");
1888 seq_puts(seq
, " Outgoing: ");
1889 /* For out_nc_node to this orig_node */
1890 list_for_each_entry_rcu(nc_node
,
1891 &orig_node
->out_coding_list
,
1893 seq_printf(seq
, "%pM ",
1895 seq_puts(seq
, "\n\n");
1902 batadv_hardif_free_ref(primary_if
);
1907 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1908 * @bat_priv: the bat priv with all the soft interface information
1910 int batadv_nc_init_debugfs(struct batadv_priv
*bat_priv
)
1912 struct dentry
*nc_dir
, *file
;
1914 nc_dir
= debugfs_create_dir("nc", bat_priv
->debug_dir
);
1918 file
= debugfs_create_u8("min_tq", S_IRUGO
| S_IWUSR
, nc_dir
,
1919 &bat_priv
->nc
.min_tq
);
1923 file
= debugfs_create_u32("max_fwd_delay", S_IRUGO
| S_IWUSR
, nc_dir
,
1924 &bat_priv
->nc
.max_fwd_delay
);
1928 file
= debugfs_create_u32("max_buffer_time", S_IRUGO
| S_IWUSR
, nc_dir
,
1929 &bat_priv
->nc
.max_buffer_time
);