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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include <linux/debugfs.h>
24 #include "network-coding.h"
26 #include "originator.h"
27 #include "hard-interface.h"
30 static struct lock_class_key batadv_nc_coding_hash_lock_class_key
;
31 static struct lock_class_key batadv_nc_decoding_hash_lock_class_key
;
33 static void batadv_nc_worker(struct work_struct
*work
);
34 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
35 struct batadv_hard_iface
*recv_if
);
38 * batadv_nc_init - one-time initialization for network coding
40 int __init
batadv_nc_init(void)
44 /* Register our packet type */
45 ret
= batadv_recv_handler_register(BATADV_CODED
,
46 batadv_nc_recv_coded_packet
);
52 * batadv_nc_start_timer - initialise the nc periodic worker
53 * @bat_priv: the bat priv with all the soft interface information
55 static void batadv_nc_start_timer(struct batadv_priv
*bat_priv
)
57 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->nc
.work
,
58 msecs_to_jiffies(10));
62 * batadv_nc_tvlv_container_update - update the network coding tvlv container
63 * after network coding setting change
64 * @bat_priv: the bat priv with all the soft interface information
66 static void batadv_nc_tvlv_container_update(struct batadv_priv
*bat_priv
)
70 nc_mode
= atomic_read(&bat_priv
->network_coding
);
74 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
77 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_NC
, 1,
84 * batadv_nc_status_update - update the network coding tvlv container after
85 * network coding setting change
86 * @net_dev: the soft interface net device
88 void batadv_nc_status_update(struct net_device
*net_dev
)
90 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
91 batadv_nc_tvlv_container_update(bat_priv
);
95 * batadv_nc_tvlv_ogm_handler_v1 - process incoming nc tvlv container
96 * @bat_priv: the bat priv with all the soft interface information
97 * @orig: the orig_node of the ogm
98 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
99 * @tvlv_value: tvlv buffer containing the gateway data
100 * @tvlv_value_len: tvlv buffer length
102 static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
103 struct batadv_orig_node
*orig
,
106 uint16_t tvlv_value_len
)
108 if (flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
)
109 orig
->capabilities
&= ~BATADV_ORIG_CAPA_HAS_NC
;
111 orig
->capabilities
|= BATADV_ORIG_CAPA_HAS_NC
;
115 * batadv_nc_mesh_init - initialise coding hash table and start house keeping
116 * @bat_priv: the bat priv with all the soft interface information
118 int batadv_nc_mesh_init(struct batadv_priv
*bat_priv
)
120 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
121 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
123 if (bat_priv
->nc
.coding_hash
|| bat_priv
->nc
.decoding_hash
)
126 bat_priv
->nc
.coding_hash
= batadv_hash_new(128);
127 if (!bat_priv
->nc
.coding_hash
)
130 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
131 &batadv_nc_coding_hash_lock_class_key
);
133 bat_priv
->nc
.decoding_hash
= batadv_hash_new(128);
134 if (!bat_priv
->nc
.decoding_hash
)
137 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
138 &batadv_nc_decoding_hash_lock_class_key
);
140 INIT_DELAYED_WORK(&bat_priv
->nc
.work
, batadv_nc_worker
);
141 batadv_nc_start_timer(bat_priv
);
143 batadv_tvlv_handler_register(bat_priv
, batadv_nc_tvlv_ogm_handler_v1
,
144 NULL
, BATADV_TVLV_NC
, 1,
145 BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
146 batadv_nc_tvlv_container_update(bat_priv
);
154 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
155 * @bat_priv: the bat priv with all the soft interface information
157 void batadv_nc_init_bat_priv(struct batadv_priv
*bat_priv
)
159 atomic_set(&bat_priv
->network_coding
, 1);
160 bat_priv
->nc
.min_tq
= 200;
161 bat_priv
->nc
.max_fwd_delay
= 10;
162 bat_priv
->nc
.max_buffer_time
= 200;
166 * batadv_nc_init_orig - initialise the nc fields of an orig_node
167 * @orig_node: the orig_node which is going to be initialised
169 void batadv_nc_init_orig(struct batadv_orig_node
*orig_node
)
171 INIT_LIST_HEAD(&orig_node
->in_coding_list
);
172 INIT_LIST_HEAD(&orig_node
->out_coding_list
);
173 spin_lock_init(&orig_node
->in_coding_list_lock
);
174 spin_lock_init(&orig_node
->out_coding_list_lock
);
178 * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
179 * its refcount on the orig_node
180 * @rcu: rcu pointer of the nc node
182 static void batadv_nc_node_free_rcu(struct rcu_head
*rcu
)
184 struct batadv_nc_node
*nc_node
;
186 nc_node
= container_of(rcu
, struct batadv_nc_node
, rcu
);
187 batadv_orig_node_free_ref(nc_node
->orig_node
);
192 * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
194 * @nc_node: the nc node to free
196 static void batadv_nc_node_free_ref(struct batadv_nc_node
*nc_node
)
198 if (atomic_dec_and_test(&nc_node
->refcount
))
199 call_rcu(&nc_node
->rcu
, batadv_nc_node_free_rcu
);
203 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
205 * @nc_path: the nc node to free
207 static void batadv_nc_path_free_ref(struct batadv_nc_path
*nc_path
)
209 if (atomic_dec_and_test(&nc_path
->refcount
))
210 kfree_rcu(nc_path
, rcu
);
214 * batadv_nc_packet_free - frees nc packet
215 * @nc_packet: the nc packet to free
217 static void batadv_nc_packet_free(struct batadv_nc_packet
*nc_packet
)
220 kfree_skb(nc_packet
->skb
);
222 batadv_nc_path_free_ref(nc_packet
->nc_path
);
227 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
228 * @bat_priv: the bat priv with all the soft interface information
229 * @nc_node: the nc node to check
231 * Returns true if the entry has to be purged now, false otherwise
233 static bool batadv_nc_to_purge_nc_node(struct batadv_priv
*bat_priv
,
234 struct batadv_nc_node
*nc_node
)
236 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
239 return batadv_has_timed_out(nc_node
->last_seen
, BATADV_NC_NODE_TIMEOUT
);
243 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
244 * @bat_priv: the bat priv with all the soft interface information
245 * @nc_path: the nc path to check
247 * Returns true if the entry has to be purged now, false otherwise
249 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv
*bat_priv
,
250 struct batadv_nc_path
*nc_path
)
252 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
255 /* purge the path when no packets has been added for 10 times the
258 return batadv_has_timed_out(nc_path
->last_valid
,
259 bat_priv
->nc
.max_fwd_delay
* 10);
263 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
264 * @bat_priv: the bat priv with all the soft interface information
265 * @nc_path: the nc path to check
267 * Returns true if the entry has to be purged now, false otherwise
269 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv
*bat_priv
,
270 struct batadv_nc_path
*nc_path
)
272 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
275 /* purge the path when no packets has been added for 10 times the
278 return batadv_has_timed_out(nc_path
->last_valid
,
279 bat_priv
->nc
.max_buffer_time
*10);
283 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
285 * @bat_priv: the bat priv with all the soft interface information
286 * @list: list of nc nodes
287 * @lock: nc node list lock
288 * @to_purge: function in charge to decide whether an entry has to be purged or
289 * not. This function takes the nc node as argument and has to return
290 * a boolean value: true if the entry has to be deleted, false
294 batadv_nc_purge_orig_nc_nodes(struct batadv_priv
*bat_priv
,
295 struct list_head
*list
,
297 bool (*to_purge
)(struct batadv_priv
*,
298 struct batadv_nc_node
*))
300 struct batadv_nc_node
*nc_node
, *nc_node_tmp
;
302 /* For each nc_node in list */
304 list_for_each_entry_safe(nc_node
, nc_node_tmp
, list
, list
) {
305 /* if an helper function has been passed as parameter,
306 * ask it if the entry has to be purged or not
308 if (to_purge
&& !to_purge(bat_priv
, nc_node
))
311 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
312 "Removing nc_node %pM -> %pM\n",
313 nc_node
->addr
, nc_node
->orig_node
->orig
);
314 list_del_rcu(&nc_node
->list
);
315 batadv_nc_node_free_ref(nc_node
);
317 spin_unlock_bh(lock
);
321 * batadv_nc_purge_orig - purges all nc node data attached of the given
323 * @bat_priv: the bat priv with all the soft interface information
324 * @orig_node: orig_node with the nc node entries to be purged
325 * @to_purge: function in charge to decide whether an entry has to be purged or
326 * not. This function takes the nc node as argument and has to return
327 * a boolean value: true is the entry has to be deleted, false
330 void batadv_nc_purge_orig(struct batadv_priv
*bat_priv
,
331 struct batadv_orig_node
*orig_node
,
332 bool (*to_purge
)(struct batadv_priv
*,
333 struct batadv_nc_node
*))
335 /* Check ingoing nc_node's of this orig_node */
336 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->in_coding_list
,
337 &orig_node
->in_coding_list_lock
,
340 /* Check outgoing nc_node's of this orig_node */
341 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->out_coding_list
,
342 &orig_node
->out_coding_list_lock
,
347 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
348 * have timed out nc nodes
349 * @bat_priv: the bat priv with all the soft interface information
351 static void batadv_nc_purge_orig_hash(struct batadv_priv
*bat_priv
)
353 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
354 struct hlist_head
*head
;
355 struct batadv_orig_node
*orig_node
;
361 /* For each orig_node */
362 for (i
= 0; i
< hash
->size
; i
++) {
363 head
= &hash
->table
[i
];
366 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
)
367 batadv_nc_purge_orig(bat_priv
, orig_node
,
368 batadv_nc_to_purge_nc_node
);
374 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
376 * @bat_priv: the bat priv with all the soft interface information
377 * @hash: hash table containing the nc paths to check
378 * @to_purge: function in charge to decide whether an entry has to be purged or
379 * not. This function takes the nc node as argument and has to return
380 * a boolean value: true is the entry has to be deleted, false
383 static void batadv_nc_purge_paths(struct batadv_priv
*bat_priv
,
384 struct batadv_hashtable
*hash
,
385 bool (*to_purge
)(struct batadv_priv
*,
386 struct batadv_nc_path
*))
388 struct hlist_head
*head
;
389 struct hlist_node
*node_tmp
;
390 struct batadv_nc_path
*nc_path
;
391 spinlock_t
*lock
; /* Protects lists in hash */
394 for (i
= 0; i
< hash
->size
; i
++) {
395 head
= &hash
->table
[i
];
396 lock
= &hash
->list_locks
[i
];
398 /* For each nc_path in this bin */
400 hlist_for_each_entry_safe(nc_path
, node_tmp
, head
, hash_entry
) {
401 /* if an helper function has been passed as parameter,
402 * ask it if the entry has to be purged or not
404 if (to_purge
&& !to_purge(bat_priv
, nc_path
))
407 /* purging an non-empty nc_path should never happen, but
408 * is observed under high CPU load. Delay the purging
409 * until next iteration to allow the packet_list to be
412 if (!unlikely(list_empty(&nc_path
->packet_list
))) {
413 net_ratelimited_function(printk
,
415 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
421 /* nc_path is unused, so remove it */
422 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
423 "Remove nc_path %pM -> %pM\n",
424 nc_path
->prev_hop
, nc_path
->next_hop
);
425 hlist_del_rcu(&nc_path
->hash_entry
);
426 batadv_nc_path_free_ref(nc_path
);
428 spin_unlock_bh(lock
);
433 * batadv_nc_hash_key_gen - computes the nc_path hash key
434 * @key: buffer to hold the final hash key
435 * @src: source ethernet mac address going into the hash key
436 * @dst: destination ethernet mac address going into the hash key
438 static void batadv_nc_hash_key_gen(struct batadv_nc_path
*key
, const char *src
,
441 memcpy(key
->prev_hop
, src
, sizeof(key
->prev_hop
));
442 memcpy(key
->next_hop
, dst
, sizeof(key
->next_hop
));
446 * batadv_nc_hash_choose - compute the hash value for an nc path
447 * @data: data to hash
448 * @size: size of the hash table
450 * Returns the selected index in the hash table for the given data.
452 static uint32_t batadv_nc_hash_choose(const void *data
, uint32_t size
)
454 const struct batadv_nc_path
*nc_path
= data
;
457 hash
= batadv_hash_bytes(hash
, &nc_path
->prev_hop
,
458 sizeof(nc_path
->prev_hop
));
459 hash
= batadv_hash_bytes(hash
, &nc_path
->next_hop
,
460 sizeof(nc_path
->next_hop
));
463 hash
^= (hash
>> 11);
464 hash
+= (hash
<< 15);
470 * batadv_nc_hash_compare - comparing function used in the network coding hash
472 * @node: node in the local table
473 * @data2: second object to compare the node to
475 * Returns 1 if the two entry are the same, 0 otherwise
477 static int batadv_nc_hash_compare(const struct hlist_node
*node
,
480 const struct batadv_nc_path
*nc_path1
, *nc_path2
;
482 nc_path1
= container_of(node
, struct batadv_nc_path
, hash_entry
);
485 /* Return 1 if the two keys are identical */
486 if (memcmp(nc_path1
->prev_hop
, nc_path2
->prev_hop
,
487 sizeof(nc_path1
->prev_hop
)) != 0)
490 if (memcmp(nc_path1
->next_hop
, nc_path2
->next_hop
,
491 sizeof(nc_path1
->next_hop
)) != 0)
498 * batadv_nc_hash_find - search for an existing nc path and return it
499 * @hash: hash table containing the nc path
502 * Returns the nc_path if found, NULL otherwise.
504 static struct batadv_nc_path
*
505 batadv_nc_hash_find(struct batadv_hashtable
*hash
,
508 struct hlist_head
*head
;
509 struct batadv_nc_path
*nc_path
, *nc_path_tmp
= NULL
;
515 index
= batadv_nc_hash_choose(data
, hash
->size
);
516 head
= &hash
->table
[index
];
519 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
520 if (!batadv_nc_hash_compare(&nc_path
->hash_entry
, data
))
523 if (!atomic_inc_not_zero(&nc_path
->refcount
))
526 nc_path_tmp
= nc_path
;
535 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
536 * @nc_packet: the nc packet to send
538 static void batadv_nc_send_packet(struct batadv_nc_packet
*nc_packet
)
540 batadv_send_skb_packet(nc_packet
->skb
,
541 nc_packet
->neigh_node
->if_incoming
,
542 nc_packet
->nc_path
->next_hop
);
543 nc_packet
->skb
= NULL
;
544 batadv_nc_packet_free(nc_packet
);
548 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
549 * @bat_priv: the bat priv with all the soft interface information
550 * @nc_path: the nc path the packet belongs to
551 * @nc_packet: the nc packet to be checked
553 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
554 * timeout. If so, the packet is no longer kept and the entry deleted from the
555 * queue. Has to be called with the appropriate locks.
557 * Returns false as soon as the entry in the fifo queue has not been timed out
558 * yet and true otherwise.
560 static bool batadv_nc_sniffed_purge(struct batadv_priv
*bat_priv
,
561 struct batadv_nc_path
*nc_path
,
562 struct batadv_nc_packet
*nc_packet
)
564 unsigned long timeout
= bat_priv
->nc
.max_buffer_time
;
567 /* Packets are added to tail, so the remaining packets did not time
568 * out and we can stop processing the current queue
570 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
571 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
574 /* purge nc packet */
575 list_del(&nc_packet
->list
);
576 batadv_nc_packet_free(nc_packet
);
585 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
586 * @bat_priv: the bat priv with all the soft interface information
587 * @nc_path: the nc path the packet belongs to
588 * @nc_packet: the nc packet to be checked
590 * Checks whether the given nc packet has hit its forward timeout. If so, the
591 * packet is no longer delayed, immediately sent and the entry deleted from the
592 * queue. Has to be called with the appropriate locks.
594 * Returns false as soon as the entry in the fifo queue has not been timed out
595 * yet and true otherwise.
597 static bool batadv_nc_fwd_flush(struct batadv_priv
*bat_priv
,
598 struct batadv_nc_path
*nc_path
,
599 struct batadv_nc_packet
*nc_packet
)
601 unsigned long timeout
= bat_priv
->nc
.max_fwd_delay
;
603 /* Packets are added to tail, so the remaining packets did not time
604 * out and we can stop processing the current queue
606 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
607 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
611 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
612 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
613 nc_packet
->skb
->len
+ ETH_HLEN
);
614 list_del(&nc_packet
->list
);
615 batadv_nc_send_packet(nc_packet
);
621 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
623 * @bat_priv: the bat priv with all the soft interface information
624 * @hash: to be processed hash table
625 * @process_fn: Function called to process given nc packet. Should return true
626 * to encourage this function to proceed with the next packet.
627 * Otherwise the rest of the current queue is skipped.
630 batadv_nc_process_nc_paths(struct batadv_priv
*bat_priv
,
631 struct batadv_hashtable
*hash
,
632 bool (*process_fn
)(struct batadv_priv
*,
633 struct batadv_nc_path
*,
634 struct batadv_nc_packet
*))
636 struct hlist_head
*head
;
637 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
638 struct batadv_nc_path
*nc_path
;
645 /* Loop hash table bins */
646 for (i
= 0; i
< hash
->size
; i
++) {
647 head
= &hash
->table
[i
];
649 /* Loop coding paths */
651 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
653 spin_lock_bh(&nc_path
->packet_list_lock
);
654 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
655 &nc_path
->packet_list
, list
) {
656 ret
= process_fn(bat_priv
, nc_path
, nc_packet
);
660 spin_unlock_bh(&nc_path
->packet_list_lock
);
667 * batadv_nc_worker - periodic task for house keeping related to network coding
668 * @work: kernel work struct
670 static void batadv_nc_worker(struct work_struct
*work
)
672 struct delayed_work
*delayed_work
;
673 struct batadv_priv_nc
*priv_nc
;
674 struct batadv_priv
*bat_priv
;
675 unsigned long timeout
;
677 delayed_work
= container_of(work
, struct delayed_work
, work
);
678 priv_nc
= container_of(delayed_work
, struct batadv_priv_nc
, work
);
679 bat_priv
= container_of(priv_nc
, struct batadv_priv
, nc
);
681 batadv_nc_purge_orig_hash(bat_priv
);
682 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
683 batadv_nc_to_purge_nc_path_coding
);
684 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
685 batadv_nc_to_purge_nc_path_decoding
);
687 timeout
= bat_priv
->nc
.max_fwd_delay
;
689 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_fwd_flush
, timeout
)) {
690 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
691 batadv_nc_fwd_flush
);
692 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
695 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_sniffed_purge
,
696 bat_priv
->nc
.max_buffer_time
)) {
697 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
698 batadv_nc_sniffed_purge
);
699 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
702 /* Schedule a new check */
703 batadv_nc_start_timer(bat_priv
);
707 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
709 * @bat_priv: the bat priv with all the soft interface information
710 * @orig_node: neighboring orig node which may be used as nc candidate
711 * @ogm_packet: incoming ogm packet also used for the checks
714 * 1) The OGM must have the most recent sequence number.
715 * 2) The TTL must be decremented by one and only one.
716 * 3) The OGM must be received from the first hop from orig_node.
717 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
719 static bool batadv_can_nc_with_orig(struct batadv_priv
*bat_priv
,
720 struct batadv_orig_node
*orig_node
,
721 struct batadv_ogm_packet
*ogm_packet
)
723 if (orig_node
->last_real_seqno
!= ntohl(ogm_packet
->seqno
))
725 if (orig_node
->last_ttl
!= ogm_packet
->header
.ttl
+ 1)
727 if (!batadv_compare_eth(ogm_packet
->orig
, ogm_packet
->prev_sender
))
729 if (ogm_packet
->tq
< bat_priv
->nc
.min_tq
)
736 * batadv_nc_find_nc_node - search for an existing nc node and return it
737 * @orig_node: orig node originating the ogm packet
738 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
739 * (can be equal to orig_node)
740 * @in_coding: traverse incoming or outgoing network coding list
742 * Returns the nc_node if found, NULL otherwise.
744 static struct batadv_nc_node
745 *batadv_nc_find_nc_node(struct batadv_orig_node
*orig_node
,
746 struct batadv_orig_node
*orig_neigh_node
,
749 struct batadv_nc_node
*nc_node
, *nc_node_out
= NULL
;
750 struct list_head
*list
;
753 list
= &orig_neigh_node
->in_coding_list
;
755 list
= &orig_neigh_node
->out_coding_list
;
757 /* Traverse list of nc_nodes to orig_node */
759 list_for_each_entry_rcu(nc_node
, list
, list
) {
760 if (!batadv_compare_eth(nc_node
->addr
, orig_node
->orig
))
763 if (!atomic_inc_not_zero(&nc_node
->refcount
))
767 nc_node_out
= nc_node
;
776 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
778 * @bat_priv: the bat priv with all the soft interface information
779 * @orig_node: orig node originating the ogm packet
780 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
781 * (can be equal to orig_node)
782 * @in_coding: traverse incoming or outgoing network coding list
784 * Returns the nc_node if found or created, NULL in case of an error.
786 static struct batadv_nc_node
787 *batadv_nc_get_nc_node(struct batadv_priv
*bat_priv
,
788 struct batadv_orig_node
*orig_node
,
789 struct batadv_orig_node
*orig_neigh_node
,
792 struct batadv_nc_node
*nc_node
;
793 spinlock_t
*lock
; /* Used to lock list selected by "int in_coding" */
794 struct list_head
*list
;
796 /* Check if nc_node is already added */
797 nc_node
= batadv_nc_find_nc_node(orig_node
, orig_neigh_node
, in_coding
);
803 nc_node
= kzalloc(sizeof(*nc_node
), GFP_ATOMIC
);
807 if (!atomic_inc_not_zero(&orig_neigh_node
->refcount
))
810 /* Initialize nc_node */
811 INIT_LIST_HEAD(&nc_node
->list
);
812 memcpy(nc_node
->addr
, orig_node
->orig
, ETH_ALEN
);
813 nc_node
->orig_node
= orig_neigh_node
;
814 atomic_set(&nc_node
->refcount
, 2);
816 /* Select ingoing or outgoing coding node */
818 lock
= &orig_neigh_node
->in_coding_list_lock
;
819 list
= &orig_neigh_node
->in_coding_list
;
821 lock
= &orig_neigh_node
->out_coding_list_lock
;
822 list
= &orig_neigh_node
->out_coding_list
;
825 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_node %pM -> %pM\n",
826 nc_node
->addr
, nc_node
->orig_node
->orig
);
828 /* Add nc_node to orig_node */
830 list_add_tail_rcu(&nc_node
->list
, list
);
831 spin_unlock_bh(lock
);
841 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
842 * (best called on incoming OGMs)
843 * @bat_priv: the bat priv with all the soft interface information
844 * @orig_node: orig node originating the ogm packet
845 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
846 * (can be equal to orig_node)
847 * @ogm_packet: incoming ogm packet
848 * @is_single_hop_neigh: orig_node is a single hop neighbor
850 void batadv_nc_update_nc_node(struct batadv_priv
*bat_priv
,
851 struct batadv_orig_node
*orig_node
,
852 struct batadv_orig_node
*orig_neigh_node
,
853 struct batadv_ogm_packet
*ogm_packet
,
854 int is_single_hop_neigh
)
856 struct batadv_nc_node
*in_nc_node
= NULL
, *out_nc_node
= NULL
;
858 /* Check if network coding is enabled */
859 if (!atomic_read(&bat_priv
->network_coding
))
862 /* check if orig node is network coding enabled */
863 if (!(orig_node
->capabilities
& BATADV_ORIG_CAPA_HAS_NC
))
866 /* accept ogms from 'good' neighbors and single hop neighbors */
867 if (!batadv_can_nc_with_orig(bat_priv
, orig_node
, ogm_packet
) &&
868 !is_single_hop_neigh
)
871 /* Add orig_node as in_nc_node on hop */
872 in_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_node
,
873 orig_neigh_node
, true);
877 in_nc_node
->last_seen
= jiffies
;
879 /* Add hop as out_nc_node on orig_node */
880 out_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_neigh_node
,
885 out_nc_node
->last_seen
= jiffies
;
889 batadv_nc_node_free_ref(in_nc_node
);
891 batadv_nc_node_free_ref(out_nc_node
);
895 * batadv_nc_get_path - get existing nc_path or allocate a new one
896 * @bat_priv: the bat priv with all the soft interface information
897 * @hash: hash table containing the nc path
898 * @src: ethernet source address - first half of the nc path search key
899 * @dst: ethernet destination address - second half of the nc path search key
901 * Returns pointer to nc_path if the path was found or created, returns NULL
904 static struct batadv_nc_path
*batadv_nc_get_path(struct batadv_priv
*bat_priv
,
905 struct batadv_hashtable
*hash
,
910 struct batadv_nc_path
*nc_path
, nc_path_key
;
912 batadv_nc_hash_key_gen(&nc_path_key
, src
, dst
);
914 /* Search for existing nc_path */
915 nc_path
= batadv_nc_hash_find(hash
, (void *)&nc_path_key
);
918 /* Set timestamp to delay removal of nc_path */
919 nc_path
->last_valid
= jiffies
;
923 /* No existing nc_path was found; create a new */
924 nc_path
= kzalloc(sizeof(*nc_path
), GFP_ATOMIC
);
929 /* Initialize nc_path */
930 INIT_LIST_HEAD(&nc_path
->packet_list
);
931 spin_lock_init(&nc_path
->packet_list_lock
);
932 atomic_set(&nc_path
->refcount
, 2);
933 nc_path
->last_valid
= jiffies
;
934 memcpy(nc_path
->next_hop
, dst
, ETH_ALEN
);
935 memcpy(nc_path
->prev_hop
, src
, ETH_ALEN
);
937 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_path %pM -> %pM\n",
941 /* Add nc_path to hash table */
942 hash_added
= batadv_hash_add(hash
, batadv_nc_hash_compare
,
943 batadv_nc_hash_choose
, &nc_path_key
,
944 &nc_path
->hash_entry
);
946 if (hash_added
< 0) {
955 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
956 * selection of a receiver with slightly lower TQ than the other
957 * @tq: to be weighted tq value
959 static uint8_t batadv_nc_random_weight_tq(uint8_t tq
)
961 uint8_t rand_val
, rand_tq
;
963 get_random_bytes(&rand_val
, sizeof(rand_val
));
965 /* randomize the estimated packet loss (max TQ - estimated TQ) */
966 rand_tq
= rand_val
* (BATADV_TQ_MAX_VALUE
- tq
);
968 /* normalize the randomized packet loss */
969 rand_tq
/= BATADV_TQ_MAX_VALUE
;
971 /* convert to (randomized) estimated tq again */
972 return BATADV_TQ_MAX_VALUE
- rand_tq
;
976 * batadv_nc_memxor - XOR destination with source
977 * @dst: byte array to XOR into
978 * @src: byte array to XOR from
979 * @len: length of destination array
981 static void batadv_nc_memxor(char *dst
, const char *src
, unsigned int len
)
985 for (i
= 0; i
< len
; ++i
)
990 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
991 * into a coded_packet and send it
992 * @bat_priv: the bat priv with all the soft interface information
993 * @skb: data skb to forward
994 * @ethhdr: pointer to the ethernet header inside the skb
995 * @nc_packet: structure containing the packet to the skb can be coded with
996 * @neigh_node: next hop to forward packet to
998 * Returns true if both packets are consumed, false otherwise.
1000 static bool batadv_nc_code_packets(struct batadv_priv
*bat_priv
,
1001 struct sk_buff
*skb
,
1002 struct ethhdr
*ethhdr
,
1003 struct batadv_nc_packet
*nc_packet
,
1004 struct batadv_neigh_node
*neigh_node
)
1006 uint8_t tq_weighted_neigh
, tq_weighted_coding
, tq_tmp
;
1007 struct sk_buff
*skb_dest
, *skb_src
;
1008 struct batadv_unicast_packet
*packet1
;
1009 struct batadv_unicast_packet
*packet2
;
1010 struct batadv_coded_packet
*coded_packet
;
1011 struct batadv_neigh_node
*neigh_tmp
, *router_neigh
;
1012 struct batadv_neigh_node
*router_coding
= NULL
;
1013 uint8_t *first_source
, *first_dest
, *second_source
, *second_dest
;
1014 __be32 packet_id1
, packet_id2
;
1018 int unicast_size
= sizeof(*packet1
);
1019 int coded_size
= sizeof(*coded_packet
);
1020 int header_add
= coded_size
- unicast_size
;
1022 router_neigh
= batadv_orig_node_get_router(neigh_node
->orig_node
);
1026 neigh_tmp
= nc_packet
->neigh_node
;
1027 router_coding
= batadv_orig_node_get_router(neigh_tmp
->orig_node
);
1031 tq_tmp
= batadv_nc_random_weight_tq(router_neigh
->bat_iv
.tq_avg
);
1032 tq_weighted_neigh
= tq_tmp
;
1033 tq_tmp
= batadv_nc_random_weight_tq(router_coding
->bat_iv
.tq_avg
);
1034 tq_weighted_coding
= tq_tmp
;
1036 /* Select one destination for the MAC-header dst-field based on
1037 * weighted TQ-values.
1039 if (tq_weighted_neigh
>= tq_weighted_coding
) {
1040 /* Destination from nc_packet is selected for MAC-header */
1041 first_dest
= nc_packet
->nc_path
->next_hop
;
1042 first_source
= nc_packet
->nc_path
->prev_hop
;
1043 second_dest
= neigh_node
->addr
;
1044 second_source
= ethhdr
->h_source
;
1045 packet1
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1046 packet2
= (struct batadv_unicast_packet
*)skb
->data
;
1047 packet_id1
= nc_packet
->packet_id
;
1048 packet_id2
= batadv_skb_crc32(skb
,
1049 skb
->data
+ sizeof(*packet2
));
1051 /* Destination for skb is selected for MAC-header */
1052 first_dest
= neigh_node
->addr
;
1053 first_source
= ethhdr
->h_source
;
1054 second_dest
= nc_packet
->nc_path
->next_hop
;
1055 second_source
= nc_packet
->nc_path
->prev_hop
;
1056 packet1
= (struct batadv_unicast_packet
*)skb
->data
;
1057 packet2
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
1058 packet_id1
= batadv_skb_crc32(skb
,
1059 skb
->data
+ sizeof(*packet1
));
1060 packet_id2
= nc_packet
->packet_id
;
1063 /* Instead of zero padding the smallest data buffer, we
1064 * code into the largest.
1066 if (skb
->len
<= nc_packet
->skb
->len
) {
1067 skb_dest
= nc_packet
->skb
;
1071 skb_src
= nc_packet
->skb
;
1074 /* coding_len is used when decoding the packet shorter packet */
1075 coding_len
= skb_src
->len
- unicast_size
;
1077 if (skb_linearize(skb_dest
) < 0 || skb_linearize(skb_src
) < 0)
1080 skb_push(skb_dest
, header_add
);
1082 coded_packet
= (struct batadv_coded_packet
*)skb_dest
->data
;
1083 skb_reset_mac_header(skb_dest
);
1085 coded_packet
->header
.packet_type
= BATADV_CODED
;
1086 coded_packet
->header
.version
= BATADV_COMPAT_VERSION
;
1087 coded_packet
->header
.ttl
= packet1
->header
.ttl
;
1089 /* Info about first unicast packet */
1090 memcpy(coded_packet
->first_source
, first_source
, ETH_ALEN
);
1091 memcpy(coded_packet
->first_orig_dest
, packet1
->dest
, ETH_ALEN
);
1092 coded_packet
->first_crc
= packet_id1
;
1093 coded_packet
->first_ttvn
= packet1
->ttvn
;
1095 /* Info about second unicast packet */
1096 memcpy(coded_packet
->second_dest
, second_dest
, ETH_ALEN
);
1097 memcpy(coded_packet
->second_source
, second_source
, ETH_ALEN
);
1098 memcpy(coded_packet
->second_orig_dest
, packet2
->dest
, ETH_ALEN
);
1099 coded_packet
->second_crc
= packet_id2
;
1100 coded_packet
->second_ttl
= packet2
->header
.ttl
;
1101 coded_packet
->second_ttvn
= packet2
->ttvn
;
1102 coded_packet
->coded_len
= htons(coding_len
);
1104 /* This is where the magic happens: Code skb_src into skb_dest */
1105 batadv_nc_memxor(skb_dest
->data
+ coded_size
,
1106 skb_src
->data
+ unicast_size
, coding_len
);
1108 /* Update counters accordingly */
1109 if (BATADV_SKB_CB(skb_src
)->decoded
&&
1110 BATADV_SKB_CB(skb_dest
)->decoded
) {
1111 /* Both packets are recoded */
1112 count
= skb_src
->len
+ ETH_HLEN
;
1113 count
+= skb_dest
->len
+ ETH_HLEN
;
1114 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE
, 2);
1115 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
, count
);
1116 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1117 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1118 /* Both packets are newly coded */
1119 count
= skb_src
->len
+ ETH_HLEN
;
1120 count
+= skb_dest
->len
+ ETH_HLEN
;
1121 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE
, 2);
1122 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
, count
);
1123 } else if (BATADV_SKB_CB(skb_src
)->decoded
&&
1124 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1125 /* skb_src recoded and skb_dest is newly coded */
1126 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1127 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1128 skb_src
->len
+ ETH_HLEN
);
1129 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1130 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1131 skb_dest
->len
+ ETH_HLEN
);
1132 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1133 BATADV_SKB_CB(skb_dest
)->decoded
) {
1134 /* skb_src is newly coded and skb_dest is recoded */
1135 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1136 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1137 skb_src
->len
+ ETH_HLEN
);
1138 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1139 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1140 skb_dest
->len
+ ETH_HLEN
);
1143 /* skb_src is now coded into skb_dest, so free it */
1146 /* avoid duplicate free of skb from nc_packet */
1147 nc_packet
->skb
= NULL
;
1148 batadv_nc_packet_free(nc_packet
);
1150 /* Send the coded packet and return true */
1151 batadv_send_skb_packet(skb_dest
, neigh_node
->if_incoming
, first_dest
);
1155 batadv_neigh_node_free_ref(router_neigh
);
1157 batadv_neigh_node_free_ref(router_coding
);
1162 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1163 * @skb: data skb to forward
1164 * @dst: destination mac address of the other skb to code with
1165 * @src: source mac address of skb
1167 * Whenever we network code a packet we have to check whether we received it in
1168 * a network coded form. If so, we may not be able to use it for coding because
1169 * some neighbors may also have received (overheard) the packet in the network
1170 * coded form without being able to decode it. It is hard to know which of the
1171 * neighboring nodes was able to decode the packet, therefore we can only
1172 * re-code the packet if the source of the previous encoded packet is involved.
1173 * Since the source encoded the packet we can be certain it has all necessary
1174 * decode information.
1176 * Returns true if coding of a decoded packet is allowed.
1178 static bool batadv_nc_skb_coding_possible(struct sk_buff
*skb
,
1179 uint8_t *dst
, uint8_t *src
)
1181 if (BATADV_SKB_CB(skb
)->decoded
&& !batadv_compare_eth(dst
, src
))
1188 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1189 * out_nc_node to retrieve a buffered packet that can be used for coding.
1190 * @bat_priv: the bat priv with all the soft interface information
1191 * @in_nc_node: pointer to skb next hop's neighbor nc node
1192 * @out_nc_node: pointer to skb source's neighbor nc node
1193 * @skb: data skb to forward
1194 * @eth_dst: next hop mac address of skb
1196 * Returns true if coding of a decoded skb is allowed.
1198 static struct batadv_nc_packet
*
1199 batadv_nc_path_search(struct batadv_priv
*bat_priv
,
1200 struct batadv_nc_node
*in_nc_node
,
1201 struct batadv_nc_node
*out_nc_node
,
1202 struct sk_buff
*skb
,
1205 struct batadv_nc_path
*nc_path
, nc_path_key
;
1206 struct batadv_nc_packet
*nc_packet_out
= NULL
;
1207 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
1208 struct batadv_hashtable
*hash
= bat_priv
->nc
.coding_hash
;
1214 /* Create almost path key */
1215 batadv_nc_hash_key_gen(&nc_path_key
, in_nc_node
->addr
,
1217 idx
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1219 /* Check for coding opportunities in this nc_path */
1221 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[idx
], hash_entry
) {
1222 if (!batadv_compare_eth(nc_path
->prev_hop
, in_nc_node
->addr
))
1225 if (!batadv_compare_eth(nc_path
->next_hop
, out_nc_node
->addr
))
1228 spin_lock_bh(&nc_path
->packet_list_lock
);
1229 if (list_empty(&nc_path
->packet_list
)) {
1230 spin_unlock_bh(&nc_path
->packet_list_lock
);
1234 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
1235 &nc_path
->packet_list
, list
) {
1236 if (!batadv_nc_skb_coding_possible(nc_packet
->skb
,
1241 /* Coding opportunity is found! */
1242 list_del(&nc_packet
->list
);
1243 nc_packet_out
= nc_packet
;
1247 spin_unlock_bh(&nc_path
->packet_list_lock
);
1252 return nc_packet_out
;
1256 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1257 * skb's sender (may be equal to the originator).
1258 * @bat_priv: the bat priv with all the soft interface information
1259 * @skb: data skb to forward
1260 * @eth_dst: next hop mac address of skb
1261 * @eth_src: source mac address of skb
1262 * @in_nc_node: pointer to skb next hop's neighbor nc node
1264 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1266 static struct batadv_nc_packet
*
1267 batadv_nc_skb_src_search(struct batadv_priv
*bat_priv
,
1268 struct sk_buff
*skb
,
1271 struct batadv_nc_node
*in_nc_node
)
1273 struct batadv_orig_node
*orig_node
;
1274 struct batadv_nc_node
*out_nc_node
;
1275 struct batadv_nc_packet
*nc_packet
= NULL
;
1277 orig_node
= batadv_orig_hash_find(bat_priv
, eth_src
);
1282 list_for_each_entry_rcu(out_nc_node
,
1283 &orig_node
->out_coding_list
, list
) {
1284 /* Check if the skb is decoded and if recoding is possible */
1285 if (!batadv_nc_skb_coding_possible(skb
,
1286 out_nc_node
->addr
, eth_src
))
1289 /* Search for an opportunity in this nc_path */
1290 nc_packet
= batadv_nc_path_search(bat_priv
, in_nc_node
,
1291 out_nc_node
, skb
, eth_dst
);
1297 batadv_orig_node_free_ref(orig_node
);
1302 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1303 * unicast skb before it is stored for use in later decoding
1304 * @bat_priv: the bat priv with all the soft interface information
1305 * @skb: data skb to store
1306 * @eth_dst_new: new destination mac address of skb
1308 static void batadv_nc_skb_store_before_coding(struct batadv_priv
*bat_priv
,
1309 struct sk_buff
*skb
,
1310 uint8_t *eth_dst_new
)
1312 struct ethhdr
*ethhdr
;
1314 /* Copy skb header to change the mac header */
1315 skb
= pskb_copy(skb
, GFP_ATOMIC
);
1319 /* Set the mac header as if we actually sent the packet uncoded */
1320 ethhdr
= eth_hdr(skb
);
1321 memcpy(ethhdr
->h_source
, ethhdr
->h_dest
, ETH_ALEN
);
1322 memcpy(ethhdr
->h_dest
, eth_dst_new
, ETH_ALEN
);
1324 /* Set data pointer to MAC header to mimic packets from our tx path */
1325 skb_push(skb
, ETH_HLEN
);
1327 /* Add the packet to the decoding packet pool */
1328 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1330 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1337 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1338 * @skb: data skb to forward
1339 * @neigh_node: next hop to forward packet to
1340 * @ethhdr: pointer to the ethernet header inside the skb
1342 * Loops through list of neighboring nodes the next hop has a good connection to
1343 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1344 * next hop that potentially sent a packet which our next hop also received
1345 * (overheard) and has stored for later decoding.
1347 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1349 static bool batadv_nc_skb_dst_search(struct sk_buff
*skb
,
1350 struct batadv_neigh_node
*neigh_node
,
1351 struct ethhdr
*ethhdr
)
1353 struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1354 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1355 struct batadv_orig_node
*orig_node
= neigh_node
->orig_node
;
1356 struct batadv_nc_node
*nc_node
;
1357 struct batadv_nc_packet
*nc_packet
= NULL
;
1360 list_for_each_entry_rcu(nc_node
, &orig_node
->in_coding_list
, list
) {
1361 /* Search for coding opportunity with this in_nc_node */
1362 nc_packet
= batadv_nc_skb_src_search(bat_priv
, skb
,
1364 ethhdr
->h_source
, nc_node
);
1366 /* Opportunity was found, so stop searching */
1375 /* Save packets for later decoding */
1376 batadv_nc_skb_store_before_coding(bat_priv
, skb
,
1378 batadv_nc_skb_store_before_coding(bat_priv
, nc_packet
->skb
,
1379 nc_packet
->neigh_node
->addr
);
1381 /* Code and send packets */
1382 if (batadv_nc_code_packets(bat_priv
, skb
, ethhdr
, nc_packet
,
1386 /* out of mem ? Coding failed - we have to free the buffered packet
1387 * to avoid memleaks. The skb passed as argument will be dealt with
1388 * by the calling function.
1390 batadv_nc_send_packet(nc_packet
);
1395 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1396 * @skb: skb to add to path
1397 * @nc_path: path to add skb to
1398 * @neigh_node: next hop to forward packet to
1399 * @packet_id: checksum to identify packet
1401 * Returns true if the packet was buffered or false in case of an error.
1403 static bool batadv_nc_skb_add_to_path(struct sk_buff
*skb
,
1404 struct batadv_nc_path
*nc_path
,
1405 struct batadv_neigh_node
*neigh_node
,
1408 struct batadv_nc_packet
*nc_packet
;
1410 nc_packet
= kzalloc(sizeof(*nc_packet
), GFP_ATOMIC
);
1414 /* Initialize nc_packet */
1415 nc_packet
->timestamp
= jiffies
;
1416 nc_packet
->packet_id
= packet_id
;
1417 nc_packet
->skb
= skb
;
1418 nc_packet
->neigh_node
= neigh_node
;
1419 nc_packet
->nc_path
= nc_path
;
1421 /* Add coding packet to list */
1422 spin_lock_bh(&nc_path
->packet_list_lock
);
1423 list_add_tail(&nc_packet
->list
, &nc_path
->packet_list
);
1424 spin_unlock_bh(&nc_path
->packet_list_lock
);
1430 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1432 * @skb: data skb to forward
1433 * @neigh_node: next hop to forward packet to
1435 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1437 bool batadv_nc_skb_forward(struct sk_buff
*skb
,
1438 struct batadv_neigh_node
*neigh_node
)
1440 const struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1441 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1442 struct batadv_unicast_packet
*packet
;
1443 struct batadv_nc_path
*nc_path
;
1444 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1448 /* Check if network coding is enabled */
1449 if (!atomic_read(&bat_priv
->network_coding
))
1452 /* We only handle unicast packets */
1453 payload
= skb_network_header(skb
);
1454 packet
= (struct batadv_unicast_packet
*)payload
;
1455 if (packet
->header
.packet_type
!= BATADV_UNICAST
)
1458 /* Try to find a coding opportunity and send the skb if one is found */
1459 if (batadv_nc_skb_dst_search(skb
, neigh_node
, ethhdr
))
1462 /* Find or create a nc_path for this src-dst pair */
1463 nc_path
= batadv_nc_get_path(bat_priv
,
1464 bat_priv
->nc
.coding_hash
,
1471 /* Add skb to nc_path */
1472 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1473 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, neigh_node
, packet_id
))
1476 /* Packet is consumed */
1480 batadv_nc_path_free_ref(nc_path
);
1482 /* Packet is not consumed */
1487 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1488 * when decoding coded packets
1489 * @bat_priv: the bat priv with all the soft interface information
1490 * @skb: data skb to store
1492 void batadv_nc_skb_store_for_decoding(struct batadv_priv
*bat_priv
,
1493 struct sk_buff
*skb
)
1495 struct batadv_unicast_packet
*packet
;
1496 struct batadv_nc_path
*nc_path
;
1497 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1501 /* Check if network coding is enabled */
1502 if (!atomic_read(&bat_priv
->network_coding
))
1505 /* Check for supported packet type */
1506 payload
= skb_network_header(skb
);
1507 packet
= (struct batadv_unicast_packet
*)payload
;
1508 if (packet
->header
.packet_type
!= BATADV_UNICAST
)
1511 /* Find existing nc_path or create a new */
1512 nc_path
= batadv_nc_get_path(bat_priv
,
1513 bat_priv
->nc
.decoding_hash
,
1520 /* Clone skb and adjust skb->data to point at batman header */
1521 skb
= skb_clone(skb
, GFP_ATOMIC
);
1525 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
1528 if (unlikely(!skb_pull_rcsum(skb
, ETH_HLEN
)))
1531 /* Add skb to nc_path */
1532 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1533 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, NULL
, packet_id
))
1536 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_BUFFER
);
1542 batadv_nc_path_free_ref(nc_path
);
1548 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1549 * should be saved in the decoding buffer and, if so, store it there
1550 * @bat_priv: the bat priv with all the soft interface information
1551 * @skb: unicast skb to store
1553 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv
*bat_priv
,
1554 struct sk_buff
*skb
)
1556 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1558 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1561 /* Set data pointer to MAC header to mimic packets from our tx path */
1562 skb_push(skb
, ETH_HLEN
);
1564 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1568 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1570 * @bat_priv: the bat priv with all the soft interface information
1571 * @skb: unicast skb to decode
1572 * @nc_packet: decode data needed to decode the skb
1574 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1575 * in case of an error.
1577 static struct batadv_unicast_packet
*
1578 batadv_nc_skb_decode_packet(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1579 struct batadv_nc_packet
*nc_packet
)
1581 const int h_size
= sizeof(struct batadv_unicast_packet
);
1582 const int h_diff
= sizeof(struct batadv_coded_packet
) - h_size
;
1583 struct batadv_unicast_packet
*unicast_packet
;
1584 struct batadv_coded_packet coded_packet_tmp
;
1585 struct ethhdr
*ethhdr
, ethhdr_tmp
;
1586 uint8_t *orig_dest
, ttl
, ttvn
;
1587 unsigned int coding_len
;
1590 /* Save headers temporarily */
1591 memcpy(&coded_packet_tmp
, skb
->data
, sizeof(coded_packet_tmp
));
1592 memcpy(ðhdr_tmp
, skb_mac_header(skb
), sizeof(ethhdr_tmp
));
1594 if (skb_cow(skb
, 0) < 0)
1597 if (unlikely(!skb_pull_rcsum(skb
, h_diff
)))
1600 /* Data points to batman header, so set mac header 14 bytes before
1601 * and network to data
1603 skb_set_mac_header(skb
, -ETH_HLEN
);
1604 skb_reset_network_header(skb
);
1606 /* Reconstruct original mac header */
1607 ethhdr
= eth_hdr(skb
);
1608 memcpy(ethhdr
, ðhdr_tmp
, sizeof(*ethhdr
));
1610 /* Select the correct unicast header information based on the location
1611 * of our mac address in the coded_packet header
1613 if (batadv_is_my_mac(bat_priv
, coded_packet_tmp
.second_dest
)) {
1614 /* If we are the second destination the packet was overheard,
1615 * so the Ethernet address must be copied to h_dest and
1616 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1618 memcpy(ethhdr
->h_dest
, coded_packet_tmp
.second_dest
, ETH_ALEN
);
1619 skb
->pkt_type
= PACKET_HOST
;
1621 orig_dest
= coded_packet_tmp
.second_orig_dest
;
1622 ttl
= coded_packet_tmp
.second_ttl
;
1623 ttvn
= coded_packet_tmp
.second_ttvn
;
1625 orig_dest
= coded_packet_tmp
.first_orig_dest
;
1626 ttl
= coded_packet_tmp
.header
.ttl
;
1627 ttvn
= coded_packet_tmp
.first_ttvn
;
1630 coding_len
= ntohs(coded_packet_tmp
.coded_len
);
1632 if (coding_len
> skb
->len
)
1635 /* Here the magic is reversed:
1636 * extract the missing packet from the received coded packet
1638 batadv_nc_memxor(skb
->data
+ h_size
,
1639 nc_packet
->skb
->data
+ h_size
,
1642 /* Resize decoded skb if decoded with larger packet */
1643 if (nc_packet
->skb
->len
> coding_len
+ h_size
) {
1644 err
= pskb_trim_rcsum(skb
, coding_len
+ h_size
);
1649 /* Create decoded unicast packet */
1650 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1651 unicast_packet
->header
.packet_type
= BATADV_UNICAST
;
1652 unicast_packet
->header
.version
= BATADV_COMPAT_VERSION
;
1653 unicast_packet
->header
.ttl
= ttl
;
1654 memcpy(unicast_packet
->dest
, orig_dest
, ETH_ALEN
);
1655 unicast_packet
->ttvn
= ttvn
;
1657 batadv_nc_packet_free(nc_packet
);
1658 return unicast_packet
;
1662 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1663 * find the data needed to decode the coded packet
1664 * @bat_priv: the bat priv with all the soft interface information
1665 * @ethhdr: pointer to the ethernet header inside the coded packet
1666 * @coded: coded packet we try to find decode data for
1668 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1670 static struct batadv_nc_packet
*
1671 batadv_nc_find_decoding_packet(struct batadv_priv
*bat_priv
,
1672 struct ethhdr
*ethhdr
,
1673 struct batadv_coded_packet
*coded
)
1675 struct batadv_hashtable
*hash
= bat_priv
->nc
.decoding_hash
;
1676 struct batadv_nc_packet
*tmp_nc_packet
, *nc_packet
= NULL
;
1677 struct batadv_nc_path
*nc_path
, nc_path_key
;
1678 uint8_t *dest
, *source
;
1685 /* Select the correct packet id based on the location of our mac-addr */
1686 dest
= ethhdr
->h_source
;
1687 if (!batadv_is_my_mac(bat_priv
, coded
->second_dest
)) {
1688 source
= coded
->second_source
;
1689 packet_id
= coded
->second_crc
;
1691 source
= coded
->first_source
;
1692 packet_id
= coded
->first_crc
;
1695 batadv_nc_hash_key_gen(&nc_path_key
, source
, dest
);
1696 index
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1698 /* Search for matching coding path */
1700 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[index
], hash_entry
) {
1701 /* Find matching nc_packet */
1702 spin_lock_bh(&nc_path
->packet_list_lock
);
1703 list_for_each_entry(tmp_nc_packet
,
1704 &nc_path
->packet_list
, list
) {
1705 if (packet_id
== tmp_nc_packet
->packet_id
) {
1706 list_del(&tmp_nc_packet
->list
);
1708 nc_packet
= tmp_nc_packet
;
1712 spin_unlock_bh(&nc_path
->packet_list_lock
);
1720 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
1721 "No decoding packet found for %u\n", packet_id
);
1727 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1728 * resulting unicast packet
1729 * @skb: incoming coded packet
1730 * @recv_if: pointer to interface this packet was received on
1732 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
1733 struct batadv_hard_iface
*recv_if
)
1735 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1736 struct batadv_unicast_packet
*unicast_packet
;
1737 struct batadv_coded_packet
*coded_packet
;
1738 struct batadv_nc_packet
*nc_packet
;
1739 struct ethhdr
*ethhdr
;
1740 int hdr_size
= sizeof(*coded_packet
);
1742 /* Check if network coding is enabled */
1743 if (!atomic_read(&bat_priv
->network_coding
))
1746 /* Make sure we can access (and remove) header */
1747 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1750 coded_packet
= (struct batadv_coded_packet
*)skb
->data
;
1751 ethhdr
= eth_hdr(skb
);
1753 /* Verify frame is destined for us */
1754 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
) &&
1755 !batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1758 /* Update stat counter */
1759 if (batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1760 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_SNIFFED
);
1762 nc_packet
= batadv_nc_find_decoding_packet(bat_priv
, ethhdr
,
1765 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1769 /* Make skb's linear, because decoding accesses the entire buffer */
1770 if (skb_linearize(skb
) < 0)
1771 goto free_nc_packet
;
1773 if (skb_linearize(nc_packet
->skb
) < 0)
1774 goto free_nc_packet
;
1776 /* Decode the packet */
1777 unicast_packet
= batadv_nc_skb_decode_packet(bat_priv
, skb
, nc_packet
);
1778 if (!unicast_packet
) {
1779 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1780 goto free_nc_packet
;
1783 /* Mark packet as decoded to do correct recoding when forwarding */
1784 BATADV_SKB_CB(skb
)->decoded
= true;
1785 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE
);
1786 batadv_add_counter(bat_priv
, BATADV_CNT_NC_DECODE_BYTES
,
1787 skb
->len
+ ETH_HLEN
);
1788 return batadv_recv_unicast_packet(skb
, recv_if
);
1791 batadv_nc_packet_free(nc_packet
);
1796 * batadv_nc_mesh_free - clean up network coding memory
1797 * @bat_priv: the bat priv with all the soft interface information
1799 void batadv_nc_mesh_free(struct batadv_priv
*bat_priv
)
1801 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1802 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_NC
, 1);
1803 cancel_delayed_work_sync(&bat_priv
->nc
.work
);
1805 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
, NULL
);
1806 batadv_hash_destroy(bat_priv
->nc
.coding_hash
);
1807 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
, NULL
);
1808 batadv_hash_destroy(bat_priv
->nc
.decoding_hash
);
1812 * batadv_nc_nodes_seq_print_text - print the nc node information
1813 * @seq: seq file to print on
1816 int batadv_nc_nodes_seq_print_text(struct seq_file
*seq
, void *offset
)
1818 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1819 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1820 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1821 struct batadv_hard_iface
*primary_if
;
1822 struct hlist_head
*head
;
1823 struct batadv_orig_node
*orig_node
;
1824 struct batadv_nc_node
*nc_node
;
1827 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1831 /* Traverse list of originators */
1832 for (i
= 0; i
< hash
->size
; i
++) {
1833 head
= &hash
->table
[i
];
1835 /* For each orig_node in this bin */
1837 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1838 /* no need to print the orig node if it does not have
1839 * network coding neighbors
1841 if (list_empty(&orig_node
->in_coding_list
) &&
1842 list_empty(&orig_node
->out_coding_list
))
1845 seq_printf(seq
, "Node: %pM\n", orig_node
->orig
);
1847 seq_puts(seq
, " Ingoing: ");
1848 /* For each in_nc_node to this orig_node */
1849 list_for_each_entry_rcu(nc_node
,
1850 &orig_node
->in_coding_list
,
1852 seq_printf(seq
, "%pM ",
1854 seq_puts(seq
, "\n");
1856 seq_puts(seq
, " Outgoing: ");
1857 /* For out_nc_node to this orig_node */
1858 list_for_each_entry_rcu(nc_node
,
1859 &orig_node
->out_coding_list
,
1861 seq_printf(seq
, "%pM ",
1863 seq_puts(seq
, "\n\n");
1870 batadv_hardif_free_ref(primary_if
);
1875 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1876 * @bat_priv: the bat priv with all the soft interface information
1878 int batadv_nc_init_debugfs(struct batadv_priv
*bat_priv
)
1880 struct dentry
*nc_dir
, *file
;
1882 nc_dir
= debugfs_create_dir("nc", bat_priv
->debug_dir
);
1886 file
= debugfs_create_u8("min_tq", S_IRUGO
| S_IWUSR
, nc_dir
,
1887 &bat_priv
->nc
.min_tq
);
1891 file
= debugfs_create_u32("max_fwd_delay", S_IRUGO
| S_IWUSR
, nc_dir
,
1892 &bat_priv
->nc
.max_fwd_delay
);
1896 file
= debugfs_create_u32("max_buffer_time", S_IRUGO
| S_IWUSR
, nc_dir
,
1897 &bat_priv
->nc
.max_buffer_time
);