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_start_timer - initialise the nc periodic worker
39 * @bat_priv: the bat priv with all the soft interface information
41 static void batadv_nc_start_timer(struct batadv_priv
*bat_priv
)
43 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->nc
.work
,
44 msecs_to_jiffies(10));
48 * batadv_nc_init - initialise coding hash table and start house keeping
49 * @bat_priv: the bat priv with all the soft interface information
51 int batadv_nc_init(struct batadv_priv
*bat_priv
)
53 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
54 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
56 if (bat_priv
->nc
.coding_hash
|| bat_priv
->nc
.decoding_hash
)
59 bat_priv
->nc
.coding_hash
= batadv_hash_new(128);
60 if (!bat_priv
->nc
.coding_hash
)
63 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
64 &batadv_nc_coding_hash_lock_class_key
);
66 bat_priv
->nc
.decoding_hash
= batadv_hash_new(128);
67 if (!bat_priv
->nc
.decoding_hash
)
70 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
71 &batadv_nc_decoding_hash_lock_class_key
);
73 /* Register our packet type */
74 if (batadv_recv_handler_register(BATADV_CODED
,
75 batadv_nc_recv_coded_packet
) < 0)
78 INIT_DELAYED_WORK(&bat_priv
->nc
.work
, batadv_nc_worker
);
79 batadv_nc_start_timer(bat_priv
);
88 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
89 * @bat_priv: the bat priv with all the soft interface information
91 void batadv_nc_init_bat_priv(struct batadv_priv
*bat_priv
)
93 atomic_set(&bat_priv
->network_coding
, 1);
94 bat_priv
->nc
.min_tq
= 200;
95 bat_priv
->nc
.max_fwd_delay
= 10;
96 bat_priv
->nc
.max_buffer_time
= 200;
100 * batadv_nc_init_orig - initialise the nc fields of an orig_node
101 * @orig_node: the orig_node which is going to be initialised
103 void batadv_nc_init_orig(struct batadv_orig_node
*orig_node
)
105 INIT_LIST_HEAD(&orig_node
->in_coding_list
);
106 INIT_LIST_HEAD(&orig_node
->out_coding_list
);
107 spin_lock_init(&orig_node
->in_coding_list_lock
);
108 spin_lock_init(&orig_node
->out_coding_list_lock
);
112 * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
113 * its refcount on the orig_node
114 * @rcu: rcu pointer of the nc node
116 static void batadv_nc_node_free_rcu(struct rcu_head
*rcu
)
118 struct batadv_nc_node
*nc_node
;
120 nc_node
= container_of(rcu
, struct batadv_nc_node
, rcu
);
121 batadv_orig_node_free_ref(nc_node
->orig_node
);
126 * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
128 * @nc_node: the nc node to free
130 static void batadv_nc_node_free_ref(struct batadv_nc_node
*nc_node
)
132 if (atomic_dec_and_test(&nc_node
->refcount
))
133 call_rcu(&nc_node
->rcu
, batadv_nc_node_free_rcu
);
137 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
139 * @nc_path: the nc node to free
141 static void batadv_nc_path_free_ref(struct batadv_nc_path
*nc_path
)
143 if (atomic_dec_and_test(&nc_path
->refcount
))
144 kfree_rcu(nc_path
, rcu
);
148 * batadv_nc_packet_free - frees nc packet
149 * @nc_packet: the nc packet to free
151 static void batadv_nc_packet_free(struct batadv_nc_packet
*nc_packet
)
154 kfree_skb(nc_packet
->skb
);
156 batadv_nc_path_free_ref(nc_packet
->nc_path
);
161 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
162 * @bat_priv: the bat priv with all the soft interface information
163 * @nc_node: the nc node to check
165 * Returns true if the entry has to be purged now, false otherwise
167 static bool batadv_nc_to_purge_nc_node(struct batadv_priv
*bat_priv
,
168 struct batadv_nc_node
*nc_node
)
170 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
173 return batadv_has_timed_out(nc_node
->last_seen
, BATADV_NC_NODE_TIMEOUT
);
177 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
178 * @bat_priv: the bat priv with all the soft interface information
179 * @nc_path: the nc path to check
181 * Returns true if the entry has to be purged now, false otherwise
183 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv
*bat_priv
,
184 struct batadv_nc_path
*nc_path
)
186 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
189 /* purge the path when no packets has been added for 10 times the
192 return batadv_has_timed_out(nc_path
->last_valid
,
193 bat_priv
->nc
.max_fwd_delay
* 10);
197 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
198 * @bat_priv: the bat priv with all the soft interface information
199 * @nc_path: the nc path to check
201 * Returns true if the entry has to be purged now, false otherwise
203 static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv
*bat_priv
,
204 struct batadv_nc_path
*nc_path
)
206 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
209 /* purge the path when no packets has been added for 10 times the
212 return batadv_has_timed_out(nc_path
->last_valid
,
213 bat_priv
->nc
.max_buffer_time
*10);
217 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
219 * @bat_priv: the bat priv with all the soft interface information
220 * @list: list of nc nodes
221 * @lock: nc node list lock
222 * @to_purge: function in charge to decide whether an entry has to be purged or
223 * not. This function takes the nc node as argument and has to return
224 * a boolean value: true if the entry has to be deleted, false
228 batadv_nc_purge_orig_nc_nodes(struct batadv_priv
*bat_priv
,
229 struct list_head
*list
,
231 bool (*to_purge
)(struct batadv_priv
*,
232 struct batadv_nc_node
*))
234 struct batadv_nc_node
*nc_node
, *nc_node_tmp
;
236 /* For each nc_node in list */
238 list_for_each_entry_safe(nc_node
, nc_node_tmp
, list
, list
) {
239 /* if an helper function has been passed as parameter,
240 * ask it if the entry has to be purged or not
242 if (to_purge
&& !to_purge(bat_priv
, nc_node
))
245 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
246 "Removing nc_node %pM -> %pM\n",
247 nc_node
->addr
, nc_node
->orig_node
->orig
);
248 list_del_rcu(&nc_node
->list
);
249 batadv_nc_node_free_ref(nc_node
);
251 spin_unlock_bh(lock
);
255 * batadv_nc_purge_orig - purges all nc node data attached of the given
257 * @bat_priv: the bat priv with all the soft interface information
258 * @orig_node: orig_node with the nc node entries to be purged
259 * @to_purge: function in charge to decide whether an entry has to be purged or
260 * not. This function takes the nc node as argument and has to return
261 * a boolean value: true is the entry has to be deleted, false
264 void batadv_nc_purge_orig(struct batadv_priv
*bat_priv
,
265 struct batadv_orig_node
*orig_node
,
266 bool (*to_purge
)(struct batadv_priv
*,
267 struct batadv_nc_node
*))
269 /* Check ingoing nc_node's of this orig_node */
270 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->in_coding_list
,
271 &orig_node
->in_coding_list_lock
,
274 /* Check outgoing nc_node's of this orig_node */
275 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->out_coding_list
,
276 &orig_node
->out_coding_list_lock
,
281 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
282 * have timed out nc nodes
283 * @bat_priv: the bat priv with all the soft interface information
285 static void batadv_nc_purge_orig_hash(struct batadv_priv
*bat_priv
)
287 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
288 struct hlist_head
*head
;
289 struct batadv_orig_node
*orig_node
;
295 /* For each orig_node */
296 for (i
= 0; i
< hash
->size
; i
++) {
297 head
= &hash
->table
[i
];
300 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
)
301 batadv_nc_purge_orig(bat_priv
, orig_node
,
302 batadv_nc_to_purge_nc_node
);
308 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
310 * @bat_priv: the bat priv with all the soft interface information
311 * @hash: hash table containing the nc paths to check
312 * @to_purge: function in charge to decide whether an entry has to be purged or
313 * not. This function takes the nc node as argument and has to return
314 * a boolean value: true is the entry has to be deleted, false
317 static void batadv_nc_purge_paths(struct batadv_priv
*bat_priv
,
318 struct batadv_hashtable
*hash
,
319 bool (*to_purge
)(struct batadv_priv
*,
320 struct batadv_nc_path
*))
322 struct hlist_head
*head
;
323 struct hlist_node
*node_tmp
;
324 struct batadv_nc_path
*nc_path
;
325 spinlock_t
*lock
; /* Protects lists in hash */
328 for (i
= 0; i
< hash
->size
; i
++) {
329 head
= &hash
->table
[i
];
330 lock
= &hash
->list_locks
[i
];
332 /* For each nc_path in this bin */
334 hlist_for_each_entry_safe(nc_path
, node_tmp
, head
, hash_entry
) {
335 /* if an helper function has been passed as parameter,
336 * ask it if the entry has to be purged or not
338 if (to_purge
&& !to_purge(bat_priv
, nc_path
))
341 /* purging an non-empty nc_path should never happen, but
342 * is observed under high CPU load. Delay the purging
343 * until next iteration to allow the packet_list to be
346 if (!unlikely(list_empty(&nc_path
->packet_list
))) {
347 net_ratelimited_function(printk
,
349 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
355 /* nc_path is unused, so remove it */
356 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
357 "Remove nc_path %pM -> %pM\n",
358 nc_path
->prev_hop
, nc_path
->next_hop
);
359 hlist_del_rcu(&nc_path
->hash_entry
);
360 batadv_nc_path_free_ref(nc_path
);
362 spin_unlock_bh(lock
);
367 * batadv_nc_hash_key_gen - computes the nc_path hash key
368 * @key: buffer to hold the final hash key
369 * @src: source ethernet mac address going into the hash key
370 * @dst: destination ethernet mac address going into the hash key
372 static void batadv_nc_hash_key_gen(struct batadv_nc_path
*key
, const char *src
,
375 memcpy(key
->prev_hop
, src
, sizeof(key
->prev_hop
));
376 memcpy(key
->next_hop
, dst
, sizeof(key
->next_hop
));
380 * batadv_nc_hash_choose - compute the hash value for an nc path
381 * @data: data to hash
382 * @size: size of the hash table
384 * Returns the selected index in the hash table for the given data.
386 static uint32_t batadv_nc_hash_choose(const void *data
, uint32_t size
)
388 const struct batadv_nc_path
*nc_path
= data
;
391 hash
= batadv_hash_bytes(hash
, &nc_path
->prev_hop
,
392 sizeof(nc_path
->prev_hop
));
393 hash
= batadv_hash_bytes(hash
, &nc_path
->next_hop
,
394 sizeof(nc_path
->next_hop
));
397 hash
^= (hash
>> 11);
398 hash
+= (hash
<< 15);
404 * batadv_nc_hash_compare - comparing function used in the network coding hash
406 * @node: node in the local table
407 * @data2: second object to compare the node to
409 * Returns 1 if the two entry are the same, 0 otherwise
411 static int batadv_nc_hash_compare(const struct hlist_node
*node
,
414 const struct batadv_nc_path
*nc_path1
, *nc_path2
;
416 nc_path1
= container_of(node
, struct batadv_nc_path
, hash_entry
);
419 /* Return 1 if the two keys are identical */
420 if (memcmp(nc_path1
->prev_hop
, nc_path2
->prev_hop
,
421 sizeof(nc_path1
->prev_hop
)) != 0)
424 if (memcmp(nc_path1
->next_hop
, nc_path2
->next_hop
,
425 sizeof(nc_path1
->next_hop
)) != 0)
432 * batadv_nc_hash_find - search for an existing nc path and return it
433 * @hash: hash table containing the nc path
436 * Returns the nc_path if found, NULL otherwise.
438 static struct batadv_nc_path
*
439 batadv_nc_hash_find(struct batadv_hashtable
*hash
,
442 struct hlist_head
*head
;
443 struct batadv_nc_path
*nc_path
, *nc_path_tmp
= NULL
;
449 index
= batadv_nc_hash_choose(data
, hash
->size
);
450 head
= &hash
->table
[index
];
453 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
454 if (!batadv_nc_hash_compare(&nc_path
->hash_entry
, data
))
457 if (!atomic_inc_not_zero(&nc_path
->refcount
))
460 nc_path_tmp
= nc_path
;
469 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
470 * @nc_packet: the nc packet to send
472 static void batadv_nc_send_packet(struct batadv_nc_packet
*nc_packet
)
474 batadv_send_skb_packet(nc_packet
->skb
,
475 nc_packet
->neigh_node
->if_incoming
,
476 nc_packet
->nc_path
->next_hop
);
477 nc_packet
->skb
= NULL
;
478 batadv_nc_packet_free(nc_packet
);
482 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
483 * @bat_priv: the bat priv with all the soft interface information
484 * @nc_path: the nc path the packet belongs to
485 * @nc_packet: the nc packet to be checked
487 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
488 * timeout. If so, the packet is no longer kept and the entry deleted from the
489 * queue. Has to be called with the appropriate locks.
491 * Returns false as soon as the entry in the fifo queue has not been timed out
492 * yet and true otherwise.
494 static bool batadv_nc_sniffed_purge(struct batadv_priv
*bat_priv
,
495 struct batadv_nc_path
*nc_path
,
496 struct batadv_nc_packet
*nc_packet
)
498 unsigned long timeout
= bat_priv
->nc
.max_buffer_time
;
501 /* Packets are added to tail, so the remaining packets did not time
502 * out and we can stop processing the current queue
504 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
505 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
508 /* purge nc packet */
509 list_del(&nc_packet
->list
);
510 batadv_nc_packet_free(nc_packet
);
519 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
520 * @bat_priv: the bat priv with all the soft interface information
521 * @nc_path: the nc path the packet belongs to
522 * @nc_packet: the nc packet to be checked
524 * Checks whether the given nc packet has hit its forward timeout. If so, the
525 * packet is no longer delayed, immediately sent and the entry deleted from the
526 * queue. Has to be called with the appropriate locks.
528 * Returns false as soon as the entry in the fifo queue has not been timed out
529 * yet and true otherwise.
531 static bool batadv_nc_fwd_flush(struct batadv_priv
*bat_priv
,
532 struct batadv_nc_path
*nc_path
,
533 struct batadv_nc_packet
*nc_packet
)
535 unsigned long timeout
= bat_priv
->nc
.max_fwd_delay
;
537 /* Packets are added to tail, so the remaining packets did not time
538 * out and we can stop processing the current queue
540 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
541 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
545 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
546 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
547 nc_packet
->skb
->len
+ ETH_HLEN
);
548 list_del(&nc_packet
->list
);
549 batadv_nc_send_packet(nc_packet
);
555 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
557 * @bat_priv: the bat priv with all the soft interface information
558 * @hash: to be processed hash table
559 * @process_fn: Function called to process given nc packet. Should return true
560 * to encourage this function to proceed with the next packet.
561 * Otherwise the rest of the current queue is skipped.
564 batadv_nc_process_nc_paths(struct batadv_priv
*bat_priv
,
565 struct batadv_hashtable
*hash
,
566 bool (*process_fn
)(struct batadv_priv
*,
567 struct batadv_nc_path
*,
568 struct batadv_nc_packet
*))
570 struct hlist_head
*head
;
571 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
572 struct batadv_nc_path
*nc_path
;
579 /* Loop hash table bins */
580 for (i
= 0; i
< hash
->size
; i
++) {
581 head
= &hash
->table
[i
];
583 /* Loop coding paths */
585 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
587 spin_lock_bh(&nc_path
->packet_list_lock
);
588 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
589 &nc_path
->packet_list
, list
) {
590 ret
= process_fn(bat_priv
, nc_path
, nc_packet
);
594 spin_unlock_bh(&nc_path
->packet_list_lock
);
601 * batadv_nc_worker - periodic task for house keeping related to network coding
602 * @work: kernel work struct
604 static void batadv_nc_worker(struct work_struct
*work
)
606 struct delayed_work
*delayed_work
;
607 struct batadv_priv_nc
*priv_nc
;
608 struct batadv_priv
*bat_priv
;
609 unsigned long timeout
;
611 delayed_work
= container_of(work
, struct delayed_work
, work
);
612 priv_nc
= container_of(delayed_work
, struct batadv_priv_nc
, work
);
613 bat_priv
= container_of(priv_nc
, struct batadv_priv
, nc
);
615 batadv_nc_purge_orig_hash(bat_priv
);
616 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
617 batadv_nc_to_purge_nc_path_coding
);
618 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
619 batadv_nc_to_purge_nc_path_decoding
);
621 timeout
= bat_priv
->nc
.max_fwd_delay
;
623 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_fwd_flush
, timeout
)) {
624 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
625 batadv_nc_fwd_flush
);
626 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
629 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_sniffed_purge
,
630 bat_priv
->nc
.max_buffer_time
)) {
631 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.decoding_hash
,
632 batadv_nc_sniffed_purge
);
633 bat_priv
->nc
.timestamp_sniffed_purge
= jiffies
;
636 /* Schedule a new check */
637 batadv_nc_start_timer(bat_priv
);
641 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
643 * @bat_priv: the bat priv with all the soft interface information
644 * @orig_node: neighboring orig node which may be used as nc candidate
645 * @ogm_packet: incoming ogm packet also used for the checks
648 * 1) The OGM must have the most recent sequence number.
649 * 2) The TTL must be decremented by one and only one.
650 * 3) The OGM must be received from the first hop from orig_node.
651 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
653 static bool batadv_can_nc_with_orig(struct batadv_priv
*bat_priv
,
654 struct batadv_orig_node
*orig_node
,
655 struct batadv_ogm_packet
*ogm_packet
)
657 if (orig_node
->last_real_seqno
!= ntohl(ogm_packet
->seqno
))
659 if (orig_node
->last_ttl
!= ogm_packet
->header
.ttl
+ 1)
661 if (!batadv_compare_eth(ogm_packet
->orig
, ogm_packet
->prev_sender
))
663 if (ogm_packet
->tq
< bat_priv
->nc
.min_tq
)
670 * batadv_nc_find_nc_node - search for an existing nc node and return it
671 * @orig_node: orig node originating the ogm packet
672 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
673 * (can be equal to orig_node)
674 * @in_coding: traverse incoming or outgoing network coding list
676 * Returns the nc_node if found, NULL otherwise.
678 static struct batadv_nc_node
679 *batadv_nc_find_nc_node(struct batadv_orig_node
*orig_node
,
680 struct batadv_orig_node
*orig_neigh_node
,
683 struct batadv_nc_node
*nc_node
, *nc_node_out
= NULL
;
684 struct list_head
*list
;
687 list
= &orig_neigh_node
->in_coding_list
;
689 list
= &orig_neigh_node
->out_coding_list
;
691 /* Traverse list of nc_nodes to orig_node */
693 list_for_each_entry_rcu(nc_node
, list
, list
) {
694 if (!batadv_compare_eth(nc_node
->addr
, orig_node
->orig
))
697 if (!atomic_inc_not_zero(&nc_node
->refcount
))
701 nc_node_out
= nc_node
;
710 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
712 * @bat_priv: the bat priv with all the soft interface information
713 * @orig_node: orig node originating the ogm packet
714 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
715 * (can be equal to orig_node)
716 * @in_coding: traverse incoming or outgoing network coding list
718 * Returns the nc_node if found or created, NULL in case of an error.
720 static struct batadv_nc_node
721 *batadv_nc_get_nc_node(struct batadv_priv
*bat_priv
,
722 struct batadv_orig_node
*orig_node
,
723 struct batadv_orig_node
*orig_neigh_node
,
726 struct batadv_nc_node
*nc_node
;
727 spinlock_t
*lock
; /* Used to lock list selected by "int in_coding" */
728 struct list_head
*list
;
730 /* Check if nc_node is already added */
731 nc_node
= batadv_nc_find_nc_node(orig_node
, orig_neigh_node
, in_coding
);
737 nc_node
= kzalloc(sizeof(*nc_node
), GFP_ATOMIC
);
741 if (!atomic_inc_not_zero(&orig_neigh_node
->refcount
))
744 /* Initialize nc_node */
745 INIT_LIST_HEAD(&nc_node
->list
);
746 memcpy(nc_node
->addr
, orig_node
->orig
, ETH_ALEN
);
747 nc_node
->orig_node
= orig_neigh_node
;
748 atomic_set(&nc_node
->refcount
, 2);
750 /* Select ingoing or outgoing coding node */
752 lock
= &orig_neigh_node
->in_coding_list_lock
;
753 list
= &orig_neigh_node
->in_coding_list
;
755 lock
= &orig_neigh_node
->out_coding_list_lock
;
756 list
= &orig_neigh_node
->out_coding_list
;
759 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_node %pM -> %pM\n",
760 nc_node
->addr
, nc_node
->orig_node
->orig
);
762 /* Add nc_node to orig_node */
764 list_add_tail_rcu(&nc_node
->list
, list
);
765 spin_unlock_bh(lock
);
775 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
776 * (best called on incoming OGMs)
777 * @bat_priv: the bat priv with all the soft interface information
778 * @orig_node: orig node originating the ogm packet
779 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
780 * (can be equal to orig_node)
781 * @ogm_packet: incoming ogm packet
782 * @is_single_hop_neigh: orig_node is a single hop neighbor
784 void batadv_nc_update_nc_node(struct batadv_priv
*bat_priv
,
785 struct batadv_orig_node
*orig_node
,
786 struct batadv_orig_node
*orig_neigh_node
,
787 struct batadv_ogm_packet
*ogm_packet
,
788 int is_single_hop_neigh
)
790 struct batadv_nc_node
*in_nc_node
= NULL
, *out_nc_node
= NULL
;
792 /* Check if network coding is enabled */
793 if (!atomic_read(&bat_priv
->network_coding
))
796 /* accept ogms from 'good' neighbors and single hop neighbors */
797 if (!batadv_can_nc_with_orig(bat_priv
, orig_node
, ogm_packet
) &&
798 !is_single_hop_neigh
)
801 /* Add orig_node as in_nc_node on hop */
802 in_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_node
,
803 orig_neigh_node
, true);
807 in_nc_node
->last_seen
= jiffies
;
809 /* Add hop as out_nc_node on orig_node */
810 out_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_neigh_node
,
815 out_nc_node
->last_seen
= jiffies
;
819 batadv_nc_node_free_ref(in_nc_node
);
821 batadv_nc_node_free_ref(out_nc_node
);
825 * batadv_nc_get_path - get existing nc_path or allocate a new one
826 * @bat_priv: the bat priv with all the soft interface information
827 * @hash: hash table containing the nc path
828 * @src: ethernet source address - first half of the nc path search key
829 * @dst: ethernet destination address - second half of the nc path search key
831 * Returns pointer to nc_path if the path was found or created, returns NULL
834 static struct batadv_nc_path
*batadv_nc_get_path(struct batadv_priv
*bat_priv
,
835 struct batadv_hashtable
*hash
,
840 struct batadv_nc_path
*nc_path
, nc_path_key
;
842 batadv_nc_hash_key_gen(&nc_path_key
, src
, dst
);
844 /* Search for existing nc_path */
845 nc_path
= batadv_nc_hash_find(hash
, (void *)&nc_path_key
);
848 /* Set timestamp to delay removal of nc_path */
849 nc_path
->last_valid
= jiffies
;
853 /* No existing nc_path was found; create a new */
854 nc_path
= kzalloc(sizeof(*nc_path
), GFP_ATOMIC
);
859 /* Initialize nc_path */
860 INIT_LIST_HEAD(&nc_path
->packet_list
);
861 spin_lock_init(&nc_path
->packet_list_lock
);
862 atomic_set(&nc_path
->refcount
, 2);
863 nc_path
->last_valid
= jiffies
;
864 memcpy(nc_path
->next_hop
, dst
, ETH_ALEN
);
865 memcpy(nc_path
->prev_hop
, src
, ETH_ALEN
);
867 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_path %pM -> %pM\n",
871 /* Add nc_path to hash table */
872 hash_added
= batadv_hash_add(hash
, batadv_nc_hash_compare
,
873 batadv_nc_hash_choose
, &nc_path_key
,
874 &nc_path
->hash_entry
);
876 if (hash_added
< 0) {
885 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
886 * selection of a receiver with slightly lower TQ than the other
887 * @tq: to be weighted tq value
889 static uint8_t batadv_nc_random_weight_tq(uint8_t tq
)
891 uint8_t rand_val
, rand_tq
;
893 get_random_bytes(&rand_val
, sizeof(rand_val
));
895 /* randomize the estimated packet loss (max TQ - estimated TQ) */
896 rand_tq
= rand_val
* (BATADV_TQ_MAX_VALUE
- tq
);
898 /* normalize the randomized packet loss */
899 rand_tq
/= BATADV_TQ_MAX_VALUE
;
901 /* convert to (randomized) estimated tq again */
902 return BATADV_TQ_MAX_VALUE
- rand_tq
;
906 * batadv_nc_memxor - XOR destination with source
907 * @dst: byte array to XOR into
908 * @src: byte array to XOR from
909 * @len: length of destination array
911 static void batadv_nc_memxor(char *dst
, const char *src
, unsigned int len
)
915 for (i
= 0; i
< len
; ++i
)
920 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
921 * into a coded_packet and send it
922 * @bat_priv: the bat priv with all the soft interface information
923 * @skb: data skb to forward
924 * @ethhdr: pointer to the ethernet header inside the skb
925 * @nc_packet: structure containing the packet to the skb can be coded with
926 * @neigh_node: next hop to forward packet to
928 * Returns true if both packets are consumed, false otherwise.
930 static bool batadv_nc_code_packets(struct batadv_priv
*bat_priv
,
932 struct ethhdr
*ethhdr
,
933 struct batadv_nc_packet
*nc_packet
,
934 struct batadv_neigh_node
*neigh_node
)
936 uint8_t tq_weighted_neigh
, tq_weighted_coding
;
937 struct sk_buff
*skb_dest
, *skb_src
;
938 struct batadv_unicast_packet
*packet1
;
939 struct batadv_unicast_packet
*packet2
;
940 struct batadv_coded_packet
*coded_packet
;
941 struct batadv_neigh_node
*neigh_tmp
, *router_neigh
;
942 struct batadv_neigh_node
*router_coding
= NULL
;
943 uint8_t *first_source
, *first_dest
, *second_source
, *second_dest
;
944 __be32 packet_id1
, packet_id2
;
948 int unicast_size
= sizeof(*packet1
);
949 int coded_size
= sizeof(*coded_packet
);
950 int header_add
= coded_size
- unicast_size
;
952 router_neigh
= batadv_orig_node_get_router(neigh_node
->orig_node
);
956 neigh_tmp
= nc_packet
->neigh_node
;
957 router_coding
= batadv_orig_node_get_router(neigh_tmp
->orig_node
);
961 tq_weighted_neigh
= batadv_nc_random_weight_tq(router_neigh
->tq_avg
);
962 tq_weighted_coding
= batadv_nc_random_weight_tq(router_coding
->tq_avg
);
964 /* Select one destination for the MAC-header dst-field based on
965 * weighted TQ-values.
967 if (tq_weighted_neigh
>= tq_weighted_coding
) {
968 /* Destination from nc_packet is selected for MAC-header */
969 first_dest
= nc_packet
->nc_path
->next_hop
;
970 first_source
= nc_packet
->nc_path
->prev_hop
;
971 second_dest
= neigh_node
->addr
;
972 second_source
= ethhdr
->h_source
;
973 packet1
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
974 packet2
= (struct batadv_unicast_packet
*)skb
->data
;
975 packet_id1
= nc_packet
->packet_id
;
976 packet_id2
= batadv_skb_crc32(skb
,
977 skb
->data
+ sizeof(*packet2
));
979 /* Destination for skb is selected for MAC-header */
980 first_dest
= neigh_node
->addr
;
981 first_source
= ethhdr
->h_source
;
982 second_dest
= nc_packet
->nc_path
->next_hop
;
983 second_source
= nc_packet
->nc_path
->prev_hop
;
984 packet1
= (struct batadv_unicast_packet
*)skb
->data
;
985 packet2
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
986 packet_id1
= batadv_skb_crc32(skb
,
987 skb
->data
+ sizeof(*packet1
));
988 packet_id2
= nc_packet
->packet_id
;
991 /* Instead of zero padding the smallest data buffer, we
992 * code into the largest.
994 if (skb
->len
<= nc_packet
->skb
->len
) {
995 skb_dest
= nc_packet
->skb
;
999 skb_src
= nc_packet
->skb
;
1002 /* coding_len is used when decoding the packet shorter packet */
1003 coding_len
= skb_src
->len
- unicast_size
;
1005 if (skb_linearize(skb_dest
) < 0 || skb_linearize(skb_src
) < 0)
1008 skb_push(skb_dest
, header_add
);
1010 coded_packet
= (struct batadv_coded_packet
*)skb_dest
->data
;
1011 skb_reset_mac_header(skb_dest
);
1013 coded_packet
->header
.packet_type
= BATADV_CODED
;
1014 coded_packet
->header
.version
= BATADV_COMPAT_VERSION
;
1015 coded_packet
->header
.ttl
= packet1
->header
.ttl
;
1017 /* Info about first unicast packet */
1018 memcpy(coded_packet
->first_source
, first_source
, ETH_ALEN
);
1019 memcpy(coded_packet
->first_orig_dest
, packet1
->dest
, ETH_ALEN
);
1020 coded_packet
->first_crc
= packet_id1
;
1021 coded_packet
->first_ttvn
= packet1
->ttvn
;
1023 /* Info about second unicast packet */
1024 memcpy(coded_packet
->second_dest
, second_dest
, ETH_ALEN
);
1025 memcpy(coded_packet
->second_source
, second_source
, ETH_ALEN
);
1026 memcpy(coded_packet
->second_orig_dest
, packet2
->dest
, ETH_ALEN
);
1027 coded_packet
->second_crc
= packet_id2
;
1028 coded_packet
->second_ttl
= packet2
->header
.ttl
;
1029 coded_packet
->second_ttvn
= packet2
->ttvn
;
1030 coded_packet
->coded_len
= htons(coding_len
);
1032 /* This is where the magic happens: Code skb_src into skb_dest */
1033 batadv_nc_memxor(skb_dest
->data
+ coded_size
,
1034 skb_src
->data
+ unicast_size
, coding_len
);
1036 /* Update counters accordingly */
1037 if (BATADV_SKB_CB(skb_src
)->decoded
&&
1038 BATADV_SKB_CB(skb_dest
)->decoded
) {
1039 /* Both packets are recoded */
1040 count
= skb_src
->len
+ ETH_HLEN
;
1041 count
+= skb_dest
->len
+ ETH_HLEN
;
1042 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE
, 2);
1043 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
, count
);
1044 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1045 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1046 /* Both packets are newly coded */
1047 count
= skb_src
->len
+ ETH_HLEN
;
1048 count
+= skb_dest
->len
+ ETH_HLEN
;
1049 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE
, 2);
1050 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
, count
);
1051 } else if (BATADV_SKB_CB(skb_src
)->decoded
&&
1052 !BATADV_SKB_CB(skb_dest
)->decoded
) {
1053 /* skb_src recoded and skb_dest is newly coded */
1054 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1055 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1056 skb_src
->len
+ ETH_HLEN
);
1057 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1058 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1059 skb_dest
->len
+ ETH_HLEN
);
1060 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
1061 BATADV_SKB_CB(skb_dest
)->decoded
) {
1062 /* skb_src is newly coded and skb_dest is recoded */
1063 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
1064 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
1065 skb_src
->len
+ ETH_HLEN
);
1066 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
1067 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
1068 skb_dest
->len
+ ETH_HLEN
);
1071 /* skb_src is now coded into skb_dest, so free it */
1074 /* avoid duplicate free of skb from nc_packet */
1075 nc_packet
->skb
= NULL
;
1076 batadv_nc_packet_free(nc_packet
);
1078 /* Send the coded packet and return true */
1079 batadv_send_skb_packet(skb_dest
, neigh_node
->if_incoming
, first_dest
);
1083 batadv_neigh_node_free_ref(router_neigh
);
1085 batadv_neigh_node_free_ref(router_coding
);
1090 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1091 * @skb: data skb to forward
1092 * @dst: destination mac address of the other skb to code with
1093 * @src: source mac address of skb
1095 * Whenever we network code a packet we have to check whether we received it in
1096 * a network coded form. If so, we may not be able to use it for coding because
1097 * some neighbors may also have received (overheard) the packet in the network
1098 * coded form without being able to decode it. It is hard to know which of the
1099 * neighboring nodes was able to decode the packet, therefore we can only
1100 * re-code the packet if the source of the previous encoded packet is involved.
1101 * Since the source encoded the packet we can be certain it has all necessary
1102 * decode information.
1104 * Returns true if coding of a decoded packet is allowed.
1106 static bool batadv_nc_skb_coding_possible(struct sk_buff
*skb
,
1107 uint8_t *dst
, uint8_t *src
)
1109 if (BATADV_SKB_CB(skb
)->decoded
&& !batadv_compare_eth(dst
, src
))
1116 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1117 * out_nc_node to retrieve a buffered packet that can be used for coding.
1118 * @bat_priv: the bat priv with all the soft interface information
1119 * @in_nc_node: pointer to skb next hop's neighbor nc node
1120 * @out_nc_node: pointer to skb source's neighbor nc node
1121 * @skb: data skb to forward
1122 * @eth_dst: next hop mac address of skb
1124 * Returns true if coding of a decoded skb is allowed.
1126 static struct batadv_nc_packet
*
1127 batadv_nc_path_search(struct batadv_priv
*bat_priv
,
1128 struct batadv_nc_node
*in_nc_node
,
1129 struct batadv_nc_node
*out_nc_node
,
1130 struct sk_buff
*skb
,
1133 struct batadv_nc_path
*nc_path
, nc_path_key
;
1134 struct batadv_nc_packet
*nc_packet_out
= NULL
;
1135 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
1136 struct batadv_hashtable
*hash
= bat_priv
->nc
.coding_hash
;
1142 /* Create almost path key */
1143 batadv_nc_hash_key_gen(&nc_path_key
, in_nc_node
->addr
,
1145 idx
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1147 /* Check for coding opportunities in this nc_path */
1149 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[idx
], hash_entry
) {
1150 if (!batadv_compare_eth(nc_path
->prev_hop
, in_nc_node
->addr
))
1153 if (!batadv_compare_eth(nc_path
->next_hop
, out_nc_node
->addr
))
1156 spin_lock_bh(&nc_path
->packet_list_lock
);
1157 if (list_empty(&nc_path
->packet_list
)) {
1158 spin_unlock_bh(&nc_path
->packet_list_lock
);
1162 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
1163 &nc_path
->packet_list
, list
) {
1164 if (!batadv_nc_skb_coding_possible(nc_packet
->skb
,
1169 /* Coding opportunity is found! */
1170 list_del(&nc_packet
->list
);
1171 nc_packet_out
= nc_packet
;
1175 spin_unlock_bh(&nc_path
->packet_list_lock
);
1180 return nc_packet_out
;
1184 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1185 * skb's sender (may be equal to the originator).
1186 * @bat_priv: the bat priv with all the soft interface information
1187 * @skb: data skb to forward
1188 * @eth_dst: next hop mac address of skb
1189 * @eth_src: source mac address of skb
1190 * @in_nc_node: pointer to skb next hop's neighbor nc node
1192 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1194 static struct batadv_nc_packet
*
1195 batadv_nc_skb_src_search(struct batadv_priv
*bat_priv
,
1196 struct sk_buff
*skb
,
1199 struct batadv_nc_node
*in_nc_node
)
1201 struct batadv_orig_node
*orig_node
;
1202 struct batadv_nc_node
*out_nc_node
;
1203 struct batadv_nc_packet
*nc_packet
= NULL
;
1205 orig_node
= batadv_orig_hash_find(bat_priv
, eth_src
);
1210 list_for_each_entry_rcu(out_nc_node
,
1211 &orig_node
->out_coding_list
, list
) {
1212 /* Check if the skb is decoded and if recoding is possible */
1213 if (!batadv_nc_skb_coding_possible(skb
,
1214 out_nc_node
->addr
, eth_src
))
1217 /* Search for an opportunity in this nc_path */
1218 nc_packet
= batadv_nc_path_search(bat_priv
, in_nc_node
,
1219 out_nc_node
, skb
, eth_dst
);
1225 batadv_orig_node_free_ref(orig_node
);
1230 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1231 * unicast skb before it is stored for use in later decoding
1232 * @bat_priv: the bat priv with all the soft interface information
1233 * @skb: data skb to store
1234 * @eth_dst_new: new destination mac address of skb
1236 static void batadv_nc_skb_store_before_coding(struct batadv_priv
*bat_priv
,
1237 struct sk_buff
*skb
,
1238 uint8_t *eth_dst_new
)
1240 struct ethhdr
*ethhdr
;
1242 /* Copy skb header to change the mac header */
1243 skb
= pskb_copy(skb
, GFP_ATOMIC
);
1247 /* Set the mac header as if we actually sent the packet uncoded */
1248 ethhdr
= eth_hdr(skb
);
1249 memcpy(ethhdr
->h_source
, ethhdr
->h_dest
, ETH_ALEN
);
1250 memcpy(ethhdr
->h_dest
, eth_dst_new
, ETH_ALEN
);
1252 /* Set data pointer to MAC header to mimic packets from our tx path */
1253 skb_push(skb
, ETH_HLEN
);
1255 /* Add the packet to the decoding packet pool */
1256 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1258 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1265 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1266 * @skb: data skb to forward
1267 * @neigh_node: next hop to forward packet to
1268 * @ethhdr: pointer to the ethernet header inside the skb
1270 * Loops through list of neighboring nodes the next hop has a good connection to
1271 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1272 * next hop that potentially sent a packet which our next hop also received
1273 * (overheard) and has stored for later decoding.
1275 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1277 static bool batadv_nc_skb_dst_search(struct sk_buff
*skb
,
1278 struct batadv_neigh_node
*neigh_node
,
1279 struct ethhdr
*ethhdr
)
1281 struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1282 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1283 struct batadv_orig_node
*orig_node
= neigh_node
->orig_node
;
1284 struct batadv_nc_node
*nc_node
;
1285 struct batadv_nc_packet
*nc_packet
= NULL
;
1288 list_for_each_entry_rcu(nc_node
, &orig_node
->in_coding_list
, list
) {
1289 /* Search for coding opportunity with this in_nc_node */
1290 nc_packet
= batadv_nc_skb_src_search(bat_priv
, skb
,
1292 ethhdr
->h_source
, nc_node
);
1294 /* Opportunity was found, so stop searching */
1303 /* Save packets for later decoding */
1304 batadv_nc_skb_store_before_coding(bat_priv
, skb
,
1306 batadv_nc_skb_store_before_coding(bat_priv
, nc_packet
->skb
,
1307 nc_packet
->neigh_node
->addr
);
1309 /* Code and send packets */
1310 if (batadv_nc_code_packets(bat_priv
, skb
, ethhdr
, nc_packet
,
1314 /* out of mem ? Coding failed - we have to free the buffered packet
1315 * to avoid memleaks. The skb passed as argument will be dealt with
1316 * by the calling function.
1318 batadv_nc_send_packet(nc_packet
);
1323 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1324 * @skb: skb to add to path
1325 * @nc_path: path to add skb to
1326 * @neigh_node: next hop to forward packet to
1327 * @packet_id: checksum to identify packet
1329 * Returns true if the packet was buffered or false in case of an error.
1331 static bool batadv_nc_skb_add_to_path(struct sk_buff
*skb
,
1332 struct batadv_nc_path
*nc_path
,
1333 struct batadv_neigh_node
*neigh_node
,
1336 struct batadv_nc_packet
*nc_packet
;
1338 nc_packet
= kzalloc(sizeof(*nc_packet
), GFP_ATOMIC
);
1342 /* Initialize nc_packet */
1343 nc_packet
->timestamp
= jiffies
;
1344 nc_packet
->packet_id
= packet_id
;
1345 nc_packet
->skb
= skb
;
1346 nc_packet
->neigh_node
= neigh_node
;
1347 nc_packet
->nc_path
= nc_path
;
1349 /* Add coding packet to list */
1350 spin_lock_bh(&nc_path
->packet_list_lock
);
1351 list_add_tail(&nc_packet
->list
, &nc_path
->packet_list
);
1352 spin_unlock_bh(&nc_path
->packet_list_lock
);
1358 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1360 * @skb: data skb to forward
1361 * @neigh_node: next hop to forward packet to
1363 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1365 bool batadv_nc_skb_forward(struct sk_buff
*skb
,
1366 struct batadv_neigh_node
*neigh_node
)
1368 const struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1369 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1370 struct batadv_unicast_packet
*packet
;
1371 struct batadv_nc_path
*nc_path
;
1372 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1376 /* Check if network coding is enabled */
1377 if (!atomic_read(&bat_priv
->network_coding
))
1380 /* We only handle unicast packets */
1381 payload
= skb_network_header(skb
);
1382 packet
= (struct batadv_unicast_packet
*)payload
;
1383 if (packet
->header
.packet_type
!= BATADV_UNICAST
)
1386 /* Try to find a coding opportunity and send the skb if one is found */
1387 if (batadv_nc_skb_dst_search(skb
, neigh_node
, ethhdr
))
1390 /* Find or create a nc_path for this src-dst pair */
1391 nc_path
= batadv_nc_get_path(bat_priv
,
1392 bat_priv
->nc
.coding_hash
,
1399 /* Add skb to nc_path */
1400 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1401 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, neigh_node
, packet_id
))
1404 /* Packet is consumed */
1408 batadv_nc_path_free_ref(nc_path
);
1410 /* Packet is not consumed */
1415 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1416 * when decoding coded packets
1417 * @bat_priv: the bat priv with all the soft interface information
1418 * @skb: data skb to store
1420 void batadv_nc_skb_store_for_decoding(struct batadv_priv
*bat_priv
,
1421 struct sk_buff
*skb
)
1423 struct batadv_unicast_packet
*packet
;
1424 struct batadv_nc_path
*nc_path
;
1425 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1429 /* Check if network coding is enabled */
1430 if (!atomic_read(&bat_priv
->network_coding
))
1433 /* Check for supported packet type */
1434 payload
= skb_network_header(skb
);
1435 packet
= (struct batadv_unicast_packet
*)payload
;
1436 if (packet
->header
.packet_type
!= BATADV_UNICAST
)
1439 /* Find existing nc_path or create a new */
1440 nc_path
= batadv_nc_get_path(bat_priv
,
1441 bat_priv
->nc
.decoding_hash
,
1448 /* Clone skb and adjust skb->data to point at batman header */
1449 skb
= skb_clone(skb
, GFP_ATOMIC
);
1453 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
1456 if (unlikely(!skb_pull_rcsum(skb
, ETH_HLEN
)))
1459 /* Add skb to nc_path */
1460 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1461 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, NULL
, packet_id
))
1464 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_BUFFER
);
1470 batadv_nc_path_free_ref(nc_path
);
1476 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1477 * should be saved in the decoding buffer and, if so, store it there
1478 * @bat_priv: the bat priv with all the soft interface information
1479 * @skb: unicast skb to store
1481 void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv
*bat_priv
,
1482 struct sk_buff
*skb
)
1484 struct ethhdr
*ethhdr
= eth_hdr(skb
);
1486 if (batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
))
1489 /* Set data pointer to MAC header to mimic packets from our tx path */
1490 skb_push(skb
, ETH_HLEN
);
1492 batadv_nc_skb_store_for_decoding(bat_priv
, skb
);
1496 * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
1498 * @bat_priv: the bat priv with all the soft interface information
1499 * @skb: unicast skb to decode
1500 * @nc_packet: decode data needed to decode the skb
1502 * Returns pointer to decoded unicast packet if the packet was decoded or NULL
1503 * in case of an error.
1505 static struct batadv_unicast_packet
*
1506 batadv_nc_skb_decode_packet(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1507 struct batadv_nc_packet
*nc_packet
)
1509 const int h_size
= sizeof(struct batadv_unicast_packet
);
1510 const int h_diff
= sizeof(struct batadv_coded_packet
) - h_size
;
1511 struct batadv_unicast_packet
*unicast_packet
;
1512 struct batadv_coded_packet coded_packet_tmp
;
1513 struct ethhdr
*ethhdr
, ethhdr_tmp
;
1514 uint8_t *orig_dest
, ttl
, ttvn
;
1515 unsigned int coding_len
;
1518 /* Save headers temporarily */
1519 memcpy(&coded_packet_tmp
, skb
->data
, sizeof(coded_packet_tmp
));
1520 memcpy(ðhdr_tmp
, skb_mac_header(skb
), sizeof(ethhdr_tmp
));
1522 if (skb_cow(skb
, 0) < 0)
1525 if (unlikely(!skb_pull_rcsum(skb
, h_diff
)))
1528 /* Data points to batman header, so set mac header 14 bytes before
1529 * and network to data
1531 skb_set_mac_header(skb
, -ETH_HLEN
);
1532 skb_reset_network_header(skb
);
1534 /* Reconstruct original mac header */
1535 ethhdr
= eth_hdr(skb
);
1536 memcpy(ethhdr
, ðhdr_tmp
, sizeof(*ethhdr
));
1538 /* Select the correct unicast header information based on the location
1539 * of our mac address in the coded_packet header
1541 if (batadv_is_my_mac(bat_priv
, coded_packet_tmp
.second_dest
)) {
1542 /* If we are the second destination the packet was overheard,
1543 * so the Ethernet address must be copied to h_dest and
1544 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
1546 memcpy(ethhdr
->h_dest
, coded_packet_tmp
.second_dest
, ETH_ALEN
);
1547 skb
->pkt_type
= PACKET_HOST
;
1549 orig_dest
= coded_packet_tmp
.second_orig_dest
;
1550 ttl
= coded_packet_tmp
.second_ttl
;
1551 ttvn
= coded_packet_tmp
.second_ttvn
;
1553 orig_dest
= coded_packet_tmp
.first_orig_dest
;
1554 ttl
= coded_packet_tmp
.header
.ttl
;
1555 ttvn
= coded_packet_tmp
.first_ttvn
;
1558 coding_len
= ntohs(coded_packet_tmp
.coded_len
);
1560 if (coding_len
> skb
->len
)
1563 /* Here the magic is reversed:
1564 * extract the missing packet from the received coded packet
1566 batadv_nc_memxor(skb
->data
+ h_size
,
1567 nc_packet
->skb
->data
+ h_size
,
1570 /* Resize decoded skb if decoded with larger packet */
1571 if (nc_packet
->skb
->len
> coding_len
+ h_size
) {
1572 err
= pskb_trim_rcsum(skb
, coding_len
+ h_size
);
1577 /* Create decoded unicast packet */
1578 unicast_packet
= (struct batadv_unicast_packet
*)skb
->data
;
1579 unicast_packet
->header
.packet_type
= BATADV_UNICAST
;
1580 unicast_packet
->header
.version
= BATADV_COMPAT_VERSION
;
1581 unicast_packet
->header
.ttl
= ttl
;
1582 memcpy(unicast_packet
->dest
, orig_dest
, ETH_ALEN
);
1583 unicast_packet
->ttvn
= ttvn
;
1585 batadv_nc_packet_free(nc_packet
);
1586 return unicast_packet
;
1590 * batadv_nc_find_decoding_packet - search through buffered decoding data to
1591 * find the data needed to decode the coded packet
1592 * @bat_priv: the bat priv with all the soft interface information
1593 * @ethhdr: pointer to the ethernet header inside the coded packet
1594 * @coded: coded packet we try to find decode data for
1596 * Returns pointer to nc packet if the needed data was found or NULL otherwise.
1598 static struct batadv_nc_packet
*
1599 batadv_nc_find_decoding_packet(struct batadv_priv
*bat_priv
,
1600 struct ethhdr
*ethhdr
,
1601 struct batadv_coded_packet
*coded
)
1603 struct batadv_hashtable
*hash
= bat_priv
->nc
.decoding_hash
;
1604 struct batadv_nc_packet
*tmp_nc_packet
, *nc_packet
= NULL
;
1605 struct batadv_nc_path
*nc_path
, nc_path_key
;
1606 uint8_t *dest
, *source
;
1613 /* Select the correct packet id based on the location of our mac-addr */
1614 dest
= ethhdr
->h_source
;
1615 if (!batadv_is_my_mac(bat_priv
, coded
->second_dest
)) {
1616 source
= coded
->second_source
;
1617 packet_id
= coded
->second_crc
;
1619 source
= coded
->first_source
;
1620 packet_id
= coded
->first_crc
;
1623 batadv_nc_hash_key_gen(&nc_path_key
, source
, dest
);
1624 index
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1626 /* Search for matching coding path */
1628 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[index
], hash_entry
) {
1629 /* Find matching nc_packet */
1630 spin_lock_bh(&nc_path
->packet_list_lock
);
1631 list_for_each_entry(tmp_nc_packet
,
1632 &nc_path
->packet_list
, list
) {
1633 if (packet_id
== tmp_nc_packet
->packet_id
) {
1634 list_del(&tmp_nc_packet
->list
);
1636 nc_packet
= tmp_nc_packet
;
1640 spin_unlock_bh(&nc_path
->packet_list_lock
);
1648 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
1649 "No decoding packet found for %u\n", packet_id
);
1655 * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
1656 * resulting unicast packet
1657 * @skb: incoming coded packet
1658 * @recv_if: pointer to interface this packet was received on
1660 static int batadv_nc_recv_coded_packet(struct sk_buff
*skb
,
1661 struct batadv_hard_iface
*recv_if
)
1663 struct batadv_priv
*bat_priv
= netdev_priv(recv_if
->soft_iface
);
1664 struct batadv_unicast_packet
*unicast_packet
;
1665 struct batadv_coded_packet
*coded_packet
;
1666 struct batadv_nc_packet
*nc_packet
;
1667 struct ethhdr
*ethhdr
;
1668 int hdr_size
= sizeof(*coded_packet
);
1670 /* Check if network coding is enabled */
1671 if (!atomic_read(&bat_priv
->network_coding
))
1674 /* Make sure we can access (and remove) header */
1675 if (unlikely(!pskb_may_pull(skb
, hdr_size
)))
1678 coded_packet
= (struct batadv_coded_packet
*)skb
->data
;
1679 ethhdr
= eth_hdr(skb
);
1681 /* Verify frame is destined for us */
1682 if (!batadv_is_my_mac(bat_priv
, ethhdr
->h_dest
) &&
1683 !batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1686 /* Update stat counter */
1687 if (batadv_is_my_mac(bat_priv
, coded_packet
->second_dest
))
1688 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_SNIFFED
);
1690 nc_packet
= batadv_nc_find_decoding_packet(bat_priv
, ethhdr
,
1693 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1697 /* Make skb's linear, because decoding accesses the entire buffer */
1698 if (skb_linearize(skb
) < 0)
1699 goto free_nc_packet
;
1701 if (skb_linearize(nc_packet
->skb
) < 0)
1702 goto free_nc_packet
;
1704 /* Decode the packet */
1705 unicast_packet
= batadv_nc_skb_decode_packet(bat_priv
, skb
, nc_packet
);
1706 if (!unicast_packet
) {
1707 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE_FAILED
);
1708 goto free_nc_packet
;
1711 /* Mark packet as decoded to do correct recoding when forwarding */
1712 BATADV_SKB_CB(skb
)->decoded
= true;
1713 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_DECODE
);
1714 batadv_add_counter(bat_priv
, BATADV_CNT_NC_DECODE_BYTES
,
1715 skb
->len
+ ETH_HLEN
);
1716 return batadv_recv_unicast_packet(skb
, recv_if
);
1719 batadv_nc_packet_free(nc_packet
);
1724 * batadv_nc_free - clean up network coding memory
1725 * @bat_priv: the bat priv with all the soft interface information
1727 void batadv_nc_free(struct batadv_priv
*bat_priv
)
1729 batadv_recv_handler_unregister(BATADV_CODED
);
1730 cancel_delayed_work_sync(&bat_priv
->nc
.work
);
1732 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
, NULL
);
1733 batadv_hash_destroy(bat_priv
->nc
.coding_hash
);
1734 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.decoding_hash
, NULL
);
1735 batadv_hash_destroy(bat_priv
->nc
.decoding_hash
);
1739 * batadv_nc_nodes_seq_print_text - print the nc node information
1740 * @seq: seq file to print on
1743 int batadv_nc_nodes_seq_print_text(struct seq_file
*seq
, void *offset
)
1745 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1746 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1747 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1748 struct batadv_hard_iface
*primary_if
;
1749 struct hlist_head
*head
;
1750 struct batadv_orig_node
*orig_node
;
1751 struct batadv_nc_node
*nc_node
;
1754 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1758 /* Traverse list of originators */
1759 for (i
= 0; i
< hash
->size
; i
++) {
1760 head
= &hash
->table
[i
];
1762 /* For each orig_node in this bin */
1764 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1765 /* no need to print the orig node if it does not have
1766 * network coding neighbors
1768 if (list_empty(&orig_node
->in_coding_list
) &&
1769 list_empty(&orig_node
->out_coding_list
))
1772 seq_printf(seq
, "Node: %pM\n", orig_node
->orig
);
1774 seq_puts(seq
, " Ingoing: ");
1775 /* For each in_nc_node to this orig_node */
1776 list_for_each_entry_rcu(nc_node
,
1777 &orig_node
->in_coding_list
,
1779 seq_printf(seq
, "%pM ",
1781 seq_puts(seq
, "\n");
1783 seq_puts(seq
, " Outgoing: ");
1784 /* For out_nc_node to this orig_node */
1785 list_for_each_entry_rcu(nc_node
,
1786 &orig_node
->out_coding_list
,
1788 seq_printf(seq
, "%pM ",
1790 seq_puts(seq
, "\n\n");
1797 batadv_hardif_free_ref(primary_if
);
1802 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1803 * @bat_priv: the bat priv with all the soft interface information
1805 int batadv_nc_init_debugfs(struct batadv_priv
*bat_priv
)
1807 struct dentry
*nc_dir
, *file
;
1809 nc_dir
= debugfs_create_dir("nc", bat_priv
->debug_dir
);
1813 file
= debugfs_create_u8("min_tq", S_IRUGO
| S_IWUSR
, nc_dir
,
1814 &bat_priv
->nc
.min_tq
);
1818 file
= debugfs_create_u32("max_fwd_delay", S_IRUGO
| S_IWUSR
, nc_dir
,
1819 &bat_priv
->nc
.max_fwd_delay
);
1823 file
= debugfs_create_u32("max_buffer_time", S_IRUGO
| S_IWUSR
, nc_dir
,
1824 &bat_priv
->nc
.max_buffer_time
);