Commit | Line | Data |
---|---|---|
9f6446c7 | 1 | /* Copyright (C) 2012-2015 B.A.T.M.A.N. contributors: |
d353d8d4 MH |
2 | * |
3 | * Martin Hundebøll, Jeppe Ledet-Pedersen | |
4 | * | |
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. | |
8 | * | |
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. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
ebf38fb7 | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
d353d8d4 MH |
16 | */ |
17 | ||
1e2c2a4f SE |
18 | #include "network-coding.h" |
19 | #include "main.h" | |
20 | ||
21 | #include <linux/atomic.h> | |
4635469f | 22 | #include <linux/bitops.h> |
1e2c2a4f SE |
23 | #include <linux/byteorder/generic.h> |
24 | #include <linux/compiler.h> | |
d56b1705 | 25 | #include <linux/debugfs.h> |
1e2c2a4f SE |
26 | #include <linux/errno.h> |
27 | #include <linux/etherdevice.h> | |
28 | #include <linux/fs.h> | |
29 | #include <linux/if_ether.h> | |
30 | #include <linux/if_packet.h> | |
31 | #include <linux/init.h> | |
32 | #include <linux/jhash.h> | |
33 | #include <linux/jiffies.h> | |
34 | #include <linux/kernel.h> | |
35 | #include <linux/list.h> | |
36 | #include <linux/lockdep.h> | |
37 | #include <linux/netdevice.h> | |
38 | #include <linux/printk.h> | |
39 | #include <linux/random.h> | |
40 | #include <linux/rculist.h> | |
41 | #include <linux/rcupdate.h> | |
42 | #include <linux/seq_file.h> | |
43 | #include <linux/skbuff.h> | |
44 | #include <linux/slab.h> | |
45 | #include <linux/spinlock.h> | |
46 | #include <linux/stat.h> | |
47 | #include <linux/stddef.h> | |
48 | #include <linux/string.h> | |
49 | #include <linux/workqueue.h> | |
d56b1705 | 50 | |
1e2c2a4f | 51 | #include "hard-interface.h" |
95332477 | 52 | #include "hash.h" |
d56b1705 | 53 | #include "originator.h" |
1e2c2a4f | 54 | #include "packet.h" |
2df5278b | 55 | #include "routing.h" |
1e2c2a4f | 56 | #include "send.h" |
d353d8d4 | 57 | |
95332477 | 58 | static struct lock_class_key batadv_nc_coding_hash_lock_class_key; |
612d2b4f | 59 | static struct lock_class_key batadv_nc_decoding_hash_lock_class_key; |
95332477 | 60 | |
d353d8d4 | 61 | static void batadv_nc_worker(struct work_struct *work); |
2df5278b MH |
62 | static int batadv_nc_recv_coded_packet(struct sk_buff *skb, |
63 | struct batadv_hard_iface *recv_if); | |
d353d8d4 | 64 | |
6c519bad MS |
65 | /** |
66 | * batadv_nc_init - one-time initialization for network coding | |
67 | */ | |
68 | int __init batadv_nc_init(void) | |
69 | { | |
70 | int ret; | |
71 | ||
72 | /* Register our packet type */ | |
73 | ret = batadv_recv_handler_register(BATADV_CODED, | |
74 | batadv_nc_recv_coded_packet); | |
75 | ||
76 | return ret; | |
77 | } | |
78 | ||
d353d8d4 MH |
79 | /** |
80 | * batadv_nc_start_timer - initialise the nc periodic worker | |
81 | * @bat_priv: the bat priv with all the soft interface information | |
82 | */ | |
83 | static void batadv_nc_start_timer(struct batadv_priv *bat_priv) | |
84 | { | |
85 | queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work, | |
86 | msecs_to_jiffies(10)); | |
87 | } | |
88 | ||
3f4841ff ML |
89 | /** |
90 | * batadv_nc_tvlv_container_update - update the network coding tvlv container | |
91 | * after network coding setting change | |
92 | * @bat_priv: the bat priv with all the soft interface information | |
93 | */ | |
94 | static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv) | |
95 | { | |
96 | char nc_mode; | |
97 | ||
98 | nc_mode = atomic_read(&bat_priv->network_coding); | |
99 | ||
100 | switch (nc_mode) { | |
101 | case 0: | |
102 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1); | |
103 | break; | |
104 | case 1: | |
105 | batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1, | |
106 | NULL, 0); | |
107 | break; | |
108 | } | |
109 | } | |
110 | ||
111 | /** | |
112 | * batadv_nc_status_update - update the network coding tvlv container after | |
113 | * network coding setting change | |
114 | * @net_dev: the soft interface net device | |
115 | */ | |
116 | void batadv_nc_status_update(struct net_device *net_dev) | |
117 | { | |
118 | struct batadv_priv *bat_priv = netdev_priv(net_dev); | |
f138694b | 119 | |
3f4841ff ML |
120 | batadv_nc_tvlv_container_update(bat_priv); |
121 | } | |
122 | ||
123 | /** | |
124 | * batadv_nc_tvlv_ogm_handler_v1 - process incoming nc tvlv container | |
125 | * @bat_priv: the bat priv with all the soft interface information | |
126 | * @orig: the orig_node of the ogm | |
127 | * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) | |
128 | * @tvlv_value: tvlv buffer containing the gateway data | |
129 | * @tvlv_value_len: tvlv buffer length | |
130 | */ | |
131 | static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, | |
132 | struct batadv_orig_node *orig, | |
6b5e971a SE |
133 | u8 flags, |
134 | void *tvlv_value, u16 tvlv_value_len) | |
3f4841ff ML |
135 | { |
136 | if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) | |
4635469f | 137 | clear_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities); |
3f4841ff | 138 | else |
4635469f | 139 | set_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities); |
3f4841ff ML |
140 | } |
141 | ||
d353d8d4 | 142 | /** |
6c519bad | 143 | * batadv_nc_mesh_init - initialise coding hash table and start house keeping |
d353d8d4 MH |
144 | * @bat_priv: the bat priv with all the soft interface information |
145 | */ | |
6c519bad | 146 | int batadv_nc_mesh_init(struct batadv_priv *bat_priv) |
d353d8d4 | 147 | { |
95332477 | 148 | bat_priv->nc.timestamp_fwd_flush = jiffies; |
612d2b4f | 149 | bat_priv->nc.timestamp_sniffed_purge = jiffies; |
95332477 | 150 | |
612d2b4f | 151 | if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash) |
95332477 MH |
152 | return 0; |
153 | ||
154 | bat_priv->nc.coding_hash = batadv_hash_new(128); | |
155 | if (!bat_priv->nc.coding_hash) | |
156 | goto err; | |
157 | ||
158 | batadv_hash_set_lock_class(bat_priv->nc.coding_hash, | |
159 | &batadv_nc_coding_hash_lock_class_key); | |
160 | ||
612d2b4f MH |
161 | bat_priv->nc.decoding_hash = batadv_hash_new(128); |
162 | if (!bat_priv->nc.decoding_hash) | |
163 | goto err; | |
164 | ||
f44d5407 | 165 | batadv_hash_set_lock_class(bat_priv->nc.decoding_hash, |
612d2b4f MH |
166 | &batadv_nc_decoding_hash_lock_class_key); |
167 | ||
d353d8d4 MH |
168 | INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker); |
169 | batadv_nc_start_timer(bat_priv); | |
170 | ||
3f4841ff ML |
171 | batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1, |
172 | NULL, BATADV_TVLV_NC, 1, | |
173 | BATADV_TVLV_HANDLER_OGM_CIFNOTFND); | |
174 | batadv_nc_tvlv_container_update(bat_priv); | |
d353d8d4 | 175 | return 0; |
95332477 MH |
176 | |
177 | err: | |
178 | return -ENOMEM; | |
d353d8d4 MH |
179 | } |
180 | ||
181 | /** | |
182 | * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables | |
183 | * @bat_priv: the bat priv with all the soft interface information | |
184 | */ | |
185 | void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv) | |
186 | { | |
dab7b621 | 187 | atomic_set(&bat_priv->network_coding, 0); |
d56b1705 | 188 | bat_priv->nc.min_tq = 200; |
95332477 | 189 | bat_priv->nc.max_fwd_delay = 10; |
612d2b4f | 190 | bat_priv->nc.max_buffer_time = 200; |
d56b1705 MH |
191 | } |
192 | ||
193 | /** | |
194 | * batadv_nc_init_orig - initialise the nc fields of an orig_node | |
195 | * @orig_node: the orig_node which is going to be initialised | |
196 | */ | |
197 | void batadv_nc_init_orig(struct batadv_orig_node *orig_node) | |
198 | { | |
199 | INIT_LIST_HEAD(&orig_node->in_coding_list); | |
200 | INIT_LIST_HEAD(&orig_node->out_coding_list); | |
201 | spin_lock_init(&orig_node->in_coding_list_lock); | |
202 | spin_lock_init(&orig_node->out_coding_list_lock); | |
203 | } | |
204 | ||
205 | /** | |
d3a4a363 SE |
206 | * batadv_nc_node_release - release nc_node from lists and queue for free after |
207 | * rcu grace period | |
208 | * @nc_node: the nc node to free | |
d56b1705 | 209 | */ |
d3a4a363 | 210 | static void batadv_nc_node_release(struct batadv_nc_node *nc_node) |
d56b1705 | 211 | { |
d56b1705 | 212 | batadv_orig_node_free_ref(nc_node->orig_node); |
d3a4a363 | 213 | kfree_rcu(nc_node, rcu); |
d56b1705 MH |
214 | } |
215 | ||
216 | /** | |
d3a4a363 SE |
217 | * batadv_nc_node_free_ref - decrement the nc node refcounter and possibly |
218 | * release it | |
d56b1705 MH |
219 | * @nc_node: the nc node to free |
220 | */ | |
221 | static void batadv_nc_node_free_ref(struct batadv_nc_node *nc_node) | |
222 | { | |
223 | if (atomic_dec_and_test(&nc_node->refcount)) | |
d3a4a363 | 224 | batadv_nc_node_release(nc_node); |
d56b1705 MH |
225 | } |
226 | ||
95332477 MH |
227 | /** |
228 | * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly | |
229 | * frees it | |
230 | * @nc_path: the nc node to free | |
231 | */ | |
232 | static void batadv_nc_path_free_ref(struct batadv_nc_path *nc_path) | |
233 | { | |
234 | if (atomic_dec_and_test(&nc_path->refcount)) | |
235 | kfree_rcu(nc_path, rcu); | |
236 | } | |
237 | ||
238 | /** | |
239 | * batadv_nc_packet_free - frees nc packet | |
240 | * @nc_packet: the nc packet to free | |
241 | */ | |
242 | static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet) | |
243 | { | |
244 | if (nc_packet->skb) | |
245 | kfree_skb(nc_packet->skb); | |
246 | ||
247 | batadv_nc_path_free_ref(nc_packet->nc_path); | |
248 | kfree(nc_packet); | |
249 | } | |
250 | ||
d56b1705 MH |
251 | /** |
252 | * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged | |
253 | * @bat_priv: the bat priv with all the soft interface information | |
254 | * @nc_node: the nc node to check | |
255 | * | |
256 | * Returns true if the entry has to be purged now, false otherwise | |
257 | */ | |
258 | static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv, | |
259 | struct batadv_nc_node *nc_node) | |
260 | { | |
261 | if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) | |
262 | return true; | |
263 | ||
264 | return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT); | |
265 | } | |
266 | ||
95332477 MH |
267 | /** |
268 | * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out | |
269 | * @bat_priv: the bat priv with all the soft interface information | |
270 | * @nc_path: the nc path to check | |
271 | * | |
272 | * Returns true if the entry has to be purged now, false otherwise | |
273 | */ | |
274 | static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv, | |
275 | struct batadv_nc_path *nc_path) | |
276 | { | |
277 | if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) | |
278 | return true; | |
279 | ||
280 | /* purge the path when no packets has been added for 10 times the | |
281 | * max_fwd_delay time | |
282 | */ | |
283 | return batadv_has_timed_out(nc_path->last_valid, | |
284 | bat_priv->nc.max_fwd_delay * 10); | |
285 | } | |
286 | ||
612d2b4f MH |
287 | /** |
288 | * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out | |
289 | * @bat_priv: the bat priv with all the soft interface information | |
290 | * @nc_path: the nc path to check | |
291 | * | |
292 | * Returns true if the entry has to be purged now, false otherwise | |
293 | */ | |
294 | static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv, | |
295 | struct batadv_nc_path *nc_path) | |
296 | { | |
297 | if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) | |
298 | return true; | |
299 | ||
300 | /* purge the path when no packets has been added for 10 times the | |
301 | * max_buffer time | |
302 | */ | |
303 | return batadv_has_timed_out(nc_path->last_valid, | |
fc1f8693 | 304 | bat_priv->nc.max_buffer_time * 10); |
612d2b4f MH |
305 | } |
306 | ||
d56b1705 MH |
307 | /** |
308 | * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale | |
309 | * entries | |
310 | * @bat_priv: the bat priv with all the soft interface information | |
311 | * @list: list of nc nodes | |
312 | * @lock: nc node list lock | |
313 | * @to_purge: function in charge to decide whether an entry has to be purged or | |
314 | * not. This function takes the nc node as argument and has to return | |
315 | * a boolean value: true if the entry has to be deleted, false | |
316 | * otherwise | |
317 | */ | |
318 | static void | |
319 | batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv, | |
320 | struct list_head *list, | |
321 | spinlock_t *lock, | |
322 | bool (*to_purge)(struct batadv_priv *, | |
323 | struct batadv_nc_node *)) | |
324 | { | |
325 | struct batadv_nc_node *nc_node, *nc_node_tmp; | |
326 | ||
327 | /* For each nc_node in list */ | |
328 | spin_lock_bh(lock); | |
329 | list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) { | |
330 | /* if an helper function has been passed as parameter, | |
331 | * ask it if the entry has to be purged or not | |
332 | */ | |
333 | if (to_purge && !to_purge(bat_priv, nc_node)) | |
334 | continue; | |
335 | ||
336 | batadv_dbg(BATADV_DBG_NC, bat_priv, | |
337 | "Removing nc_node %pM -> %pM\n", | |
338 | nc_node->addr, nc_node->orig_node->orig); | |
339 | list_del_rcu(&nc_node->list); | |
340 | batadv_nc_node_free_ref(nc_node); | |
341 | } | |
342 | spin_unlock_bh(lock); | |
343 | } | |
344 | ||
345 | /** | |
346 | * batadv_nc_purge_orig - purges all nc node data attached of the given | |
347 | * originator | |
348 | * @bat_priv: the bat priv with all the soft interface information | |
349 | * @orig_node: orig_node with the nc node entries to be purged | |
350 | * @to_purge: function in charge to decide whether an entry has to be purged or | |
351 | * not. This function takes the nc node as argument and has to return | |
352 | * a boolean value: true is the entry has to be deleted, false | |
353 | * otherwise | |
354 | */ | |
355 | void batadv_nc_purge_orig(struct batadv_priv *bat_priv, | |
356 | struct batadv_orig_node *orig_node, | |
357 | bool (*to_purge)(struct batadv_priv *, | |
358 | struct batadv_nc_node *)) | |
359 | { | |
360 | /* Check ingoing nc_node's of this orig_node */ | |
361 | batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list, | |
362 | &orig_node->in_coding_list_lock, | |
363 | to_purge); | |
364 | ||
365 | /* Check outgoing nc_node's of this orig_node */ | |
366 | batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list, | |
367 | &orig_node->out_coding_list_lock, | |
368 | to_purge); | |
369 | } | |
370 | ||
371 | /** | |
372 | * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they | |
373 | * have timed out nc nodes | |
374 | * @bat_priv: the bat priv with all the soft interface information | |
375 | */ | |
376 | static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv) | |
377 | { | |
378 | struct batadv_hashtable *hash = bat_priv->orig_hash; | |
379 | struct hlist_head *head; | |
380 | struct batadv_orig_node *orig_node; | |
6b5e971a | 381 | u32 i; |
d56b1705 MH |
382 | |
383 | if (!hash) | |
384 | return; | |
385 | ||
386 | /* For each orig_node */ | |
387 | for (i = 0; i < hash->size; i++) { | |
388 | head = &hash->table[i]; | |
389 | ||
390 | rcu_read_lock(); | |
391 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) | |
392 | batadv_nc_purge_orig(bat_priv, orig_node, | |
393 | batadv_nc_to_purge_nc_node); | |
394 | rcu_read_unlock(); | |
395 | } | |
d353d8d4 MH |
396 | } |
397 | ||
95332477 MH |
398 | /** |
399 | * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove | |
400 | * unused ones | |
401 | * @bat_priv: the bat priv with all the soft interface information | |
402 | * @hash: hash table containing the nc paths to check | |
403 | * @to_purge: function in charge to decide whether an entry has to be purged or | |
404 | * not. This function takes the nc node as argument and has to return | |
405 | * a boolean value: true is the entry has to be deleted, false | |
406 | * otherwise | |
407 | */ | |
408 | static void batadv_nc_purge_paths(struct batadv_priv *bat_priv, | |
409 | struct batadv_hashtable *hash, | |
410 | bool (*to_purge)(struct batadv_priv *, | |
411 | struct batadv_nc_path *)) | |
412 | { | |
413 | struct hlist_head *head; | |
414 | struct hlist_node *node_tmp; | |
415 | struct batadv_nc_path *nc_path; | |
416 | spinlock_t *lock; /* Protects lists in hash */ | |
6b5e971a | 417 | u32 i; |
95332477 MH |
418 | |
419 | for (i = 0; i < hash->size; i++) { | |
420 | head = &hash->table[i]; | |
421 | lock = &hash->list_locks[i]; | |
422 | ||
423 | /* For each nc_path in this bin */ | |
424 | spin_lock_bh(lock); | |
425 | hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) { | |
426 | /* if an helper function has been passed as parameter, | |
427 | * ask it if the entry has to be purged or not | |
428 | */ | |
429 | if (to_purge && !to_purge(bat_priv, nc_path)) | |
430 | continue; | |
431 | ||
432 | /* purging an non-empty nc_path should never happen, but | |
433 | * is observed under high CPU load. Delay the purging | |
434 | * until next iteration to allow the packet_list to be | |
435 | * emptied first. | |
436 | */ | |
437 | if (!unlikely(list_empty(&nc_path->packet_list))) { | |
438 | net_ratelimited_function(printk, | |
439 | KERN_WARNING | |
440 | "Skipping free of non-empty nc_path (%pM -> %pM)!\n", | |
441 | nc_path->prev_hop, | |
442 | nc_path->next_hop); | |
443 | continue; | |
444 | } | |
445 | ||
446 | /* nc_path is unused, so remove it */ | |
447 | batadv_dbg(BATADV_DBG_NC, bat_priv, | |
448 | "Remove nc_path %pM -> %pM\n", | |
449 | nc_path->prev_hop, nc_path->next_hop); | |
450 | hlist_del_rcu(&nc_path->hash_entry); | |
451 | batadv_nc_path_free_ref(nc_path); | |
452 | } | |
453 | spin_unlock_bh(lock); | |
454 | } | |
455 | } | |
456 | ||
457 | /** | |
458 | * batadv_nc_hash_key_gen - computes the nc_path hash key | |
459 | * @key: buffer to hold the final hash key | |
460 | * @src: source ethernet mac address going into the hash key | |
461 | * @dst: destination ethernet mac address going into the hash key | |
462 | */ | |
463 | static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src, | |
464 | const char *dst) | |
465 | { | |
466 | memcpy(key->prev_hop, src, sizeof(key->prev_hop)); | |
467 | memcpy(key->next_hop, dst, sizeof(key->next_hop)); | |
468 | } | |
469 | ||
470 | /** | |
471 | * batadv_nc_hash_choose - compute the hash value for an nc path | |
472 | * @data: data to hash | |
473 | * @size: size of the hash table | |
474 | * | |
475 | * Returns the selected index in the hash table for the given data. | |
476 | */ | |
6b5e971a | 477 | static u32 batadv_nc_hash_choose(const void *data, u32 size) |
95332477 MH |
478 | { |
479 | const struct batadv_nc_path *nc_path = data; | |
6b5e971a | 480 | u32 hash = 0; |
95332477 | 481 | |
36fd61cb SE |
482 | hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash); |
483 | hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash); | |
95332477 MH |
484 | |
485 | return hash % size; | |
486 | } | |
487 | ||
488 | /** | |
489 | * batadv_nc_hash_compare - comparing function used in the network coding hash | |
490 | * tables | |
491 | * @node: node in the local table | |
492 | * @data2: second object to compare the node to | |
493 | * | |
494 | * Returns 1 if the two entry are the same, 0 otherwise | |
495 | */ | |
496 | static int batadv_nc_hash_compare(const struct hlist_node *node, | |
497 | const void *data2) | |
498 | { | |
499 | const struct batadv_nc_path *nc_path1, *nc_path2; | |
500 | ||
501 | nc_path1 = container_of(node, struct batadv_nc_path, hash_entry); | |
502 | nc_path2 = data2; | |
503 | ||
504 | /* Return 1 if the two keys are identical */ | |
505 | if (memcmp(nc_path1->prev_hop, nc_path2->prev_hop, | |
506 | sizeof(nc_path1->prev_hop)) != 0) | |
507 | return 0; | |
508 | ||
509 | if (memcmp(nc_path1->next_hop, nc_path2->next_hop, | |
510 | sizeof(nc_path1->next_hop)) != 0) | |
511 | return 0; | |
512 | ||
513 | return 1; | |
514 | } | |
515 | ||
516 | /** | |
517 | * batadv_nc_hash_find - search for an existing nc path and return it | |
518 | * @hash: hash table containing the nc path | |
519 | * @data: search key | |
520 | * | |
521 | * Returns the nc_path if found, NULL otherwise. | |
522 | */ | |
523 | static struct batadv_nc_path * | |
524 | batadv_nc_hash_find(struct batadv_hashtable *hash, | |
525 | void *data) | |
526 | { | |
527 | struct hlist_head *head; | |
528 | struct batadv_nc_path *nc_path, *nc_path_tmp = NULL; | |
529 | int index; | |
530 | ||
531 | if (!hash) | |
532 | return NULL; | |
533 | ||
534 | index = batadv_nc_hash_choose(data, hash->size); | |
535 | head = &hash->table[index]; | |
536 | ||
537 | rcu_read_lock(); | |
538 | hlist_for_each_entry_rcu(nc_path, head, hash_entry) { | |
539 | if (!batadv_nc_hash_compare(&nc_path->hash_entry, data)) | |
540 | continue; | |
541 | ||
542 | if (!atomic_inc_not_zero(&nc_path->refcount)) | |
543 | continue; | |
544 | ||
545 | nc_path_tmp = nc_path; | |
546 | break; | |
547 | } | |
548 | rcu_read_unlock(); | |
549 | ||
550 | return nc_path_tmp; | |
551 | } | |
552 | ||
553 | /** | |
554 | * batadv_nc_send_packet - send non-coded packet and free nc_packet struct | |
555 | * @nc_packet: the nc packet to send | |
556 | */ | |
557 | static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet) | |
558 | { | |
559 | batadv_send_skb_packet(nc_packet->skb, | |
560 | nc_packet->neigh_node->if_incoming, | |
561 | nc_packet->nc_path->next_hop); | |
562 | nc_packet->skb = NULL; | |
563 | batadv_nc_packet_free(nc_packet); | |
564 | } | |
565 | ||
612d2b4f MH |
566 | /** |
567 | * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet. | |
568 | * @bat_priv: the bat priv with all the soft interface information | |
569 | * @nc_path: the nc path the packet belongs to | |
570 | * @nc_packet: the nc packet to be checked | |
571 | * | |
572 | * Checks whether the given sniffed (overheard) nc_packet has hit its buffering | |
573 | * timeout. If so, the packet is no longer kept and the entry deleted from the | |
574 | * queue. Has to be called with the appropriate locks. | |
575 | * | |
576 | * Returns false as soon as the entry in the fifo queue has not been timed out | |
577 | * yet and true otherwise. | |
578 | */ | |
579 | static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv, | |
580 | struct batadv_nc_path *nc_path, | |
581 | struct batadv_nc_packet *nc_packet) | |
582 | { | |
583 | unsigned long timeout = bat_priv->nc.max_buffer_time; | |
584 | bool res = false; | |
585 | ||
2c72d655 SE |
586 | lockdep_assert_held(&nc_path->packet_list_lock); |
587 | ||
612d2b4f MH |
588 | /* Packets are added to tail, so the remaining packets did not time |
589 | * out and we can stop processing the current queue | |
590 | */ | |
591 | if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE && | |
592 | !batadv_has_timed_out(nc_packet->timestamp, timeout)) | |
593 | goto out; | |
594 | ||
595 | /* purge nc packet */ | |
596 | list_del(&nc_packet->list); | |
597 | batadv_nc_packet_free(nc_packet); | |
598 | ||
599 | res = true; | |
600 | ||
601 | out: | |
602 | return res; | |
603 | } | |
604 | ||
95332477 MH |
605 | /** |
606 | * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet. | |
607 | * @bat_priv: the bat priv with all the soft interface information | |
608 | * @nc_path: the nc path the packet belongs to | |
609 | * @nc_packet: the nc packet to be checked | |
610 | * | |
611 | * Checks whether the given nc packet has hit its forward timeout. If so, the | |
612 | * packet is no longer delayed, immediately sent and the entry deleted from the | |
613 | * queue. Has to be called with the appropriate locks. | |
614 | * | |
615 | * Returns false as soon as the entry in the fifo queue has not been timed out | |
616 | * yet and true otherwise. | |
617 | */ | |
618 | static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv, | |
619 | struct batadv_nc_path *nc_path, | |
620 | struct batadv_nc_packet *nc_packet) | |
621 | { | |
622 | unsigned long timeout = bat_priv->nc.max_fwd_delay; | |
623 | ||
2c72d655 SE |
624 | lockdep_assert_held(&nc_path->packet_list_lock); |
625 | ||
95332477 MH |
626 | /* Packets are added to tail, so the remaining packets did not time |
627 | * out and we can stop processing the current queue | |
628 | */ | |
629 | if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE && | |
630 | !batadv_has_timed_out(nc_packet->timestamp, timeout)) | |
631 | return false; | |
632 | ||
633 | /* Send packet */ | |
634 | batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD); | |
635 | batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES, | |
636 | nc_packet->skb->len + ETH_HLEN); | |
637 | list_del(&nc_packet->list); | |
638 | batadv_nc_send_packet(nc_packet); | |
639 | ||
640 | return true; | |
641 | } | |
642 | ||
643 | /** | |
644 | * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out | |
645 | * nc packets | |
646 | * @bat_priv: the bat priv with all the soft interface information | |
647 | * @hash: to be processed hash table | |
648 | * @process_fn: Function called to process given nc packet. Should return true | |
649 | * to encourage this function to proceed with the next packet. | |
650 | * Otherwise the rest of the current queue is skipped. | |
651 | */ | |
652 | static void | |
653 | batadv_nc_process_nc_paths(struct batadv_priv *bat_priv, | |
654 | struct batadv_hashtable *hash, | |
655 | bool (*process_fn)(struct batadv_priv *, | |
656 | struct batadv_nc_path *, | |
657 | struct batadv_nc_packet *)) | |
658 | { | |
659 | struct hlist_head *head; | |
660 | struct batadv_nc_packet *nc_packet, *nc_packet_tmp; | |
661 | struct batadv_nc_path *nc_path; | |
662 | bool ret; | |
663 | int i; | |
664 | ||
665 | if (!hash) | |
666 | return; | |
667 | ||
668 | /* Loop hash table bins */ | |
669 | for (i = 0; i < hash->size; i++) { | |
670 | head = &hash->table[i]; | |
671 | ||
672 | /* Loop coding paths */ | |
673 | rcu_read_lock(); | |
674 | hlist_for_each_entry_rcu(nc_path, head, hash_entry) { | |
675 | /* Loop packets */ | |
676 | spin_lock_bh(&nc_path->packet_list_lock); | |
677 | list_for_each_entry_safe(nc_packet, nc_packet_tmp, | |
678 | &nc_path->packet_list, list) { | |
679 | ret = process_fn(bat_priv, nc_path, nc_packet); | |
680 | if (!ret) | |
681 | break; | |
682 | } | |
683 | spin_unlock_bh(&nc_path->packet_list_lock); | |
684 | } | |
685 | rcu_read_unlock(); | |
686 | } | |
687 | } | |
688 | ||
d353d8d4 MH |
689 | /** |
690 | * batadv_nc_worker - periodic task for house keeping related to network coding | |
691 | * @work: kernel work struct | |
692 | */ | |
693 | static void batadv_nc_worker(struct work_struct *work) | |
694 | { | |
695 | struct delayed_work *delayed_work; | |
696 | struct batadv_priv_nc *priv_nc; | |
697 | struct batadv_priv *bat_priv; | |
95332477 | 698 | unsigned long timeout; |
d353d8d4 MH |
699 | |
700 | delayed_work = container_of(work, struct delayed_work, work); | |
701 | priv_nc = container_of(delayed_work, struct batadv_priv_nc, work); | |
702 | bat_priv = container_of(priv_nc, struct batadv_priv, nc); | |
703 | ||
d56b1705 | 704 | batadv_nc_purge_orig_hash(bat_priv); |
95332477 MH |
705 | batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, |
706 | batadv_nc_to_purge_nc_path_coding); | |
612d2b4f MH |
707 | batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, |
708 | batadv_nc_to_purge_nc_path_decoding); | |
95332477 MH |
709 | |
710 | timeout = bat_priv->nc.max_fwd_delay; | |
711 | ||
712 | if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) { | |
713 | batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash, | |
714 | batadv_nc_fwd_flush); | |
715 | bat_priv->nc.timestamp_fwd_flush = jiffies; | |
716 | } | |
d56b1705 | 717 | |
612d2b4f MH |
718 | if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge, |
719 | bat_priv->nc.max_buffer_time)) { | |
720 | batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash, | |
721 | batadv_nc_sniffed_purge); | |
722 | bat_priv->nc.timestamp_sniffed_purge = jiffies; | |
723 | } | |
724 | ||
d353d8d4 MH |
725 | /* Schedule a new check */ |
726 | batadv_nc_start_timer(bat_priv); | |
727 | } | |
728 | ||
d56b1705 MH |
729 | /** |
730 | * batadv_can_nc_with_orig - checks whether the given orig node is suitable for | |
731 | * coding or not | |
732 | * @bat_priv: the bat priv with all the soft interface information | |
733 | * @orig_node: neighboring orig node which may be used as nc candidate | |
734 | * @ogm_packet: incoming ogm packet also used for the checks | |
735 | * | |
736 | * Returns true if: | |
737 | * 1) The OGM must have the most recent sequence number. | |
738 | * 2) The TTL must be decremented by one and only one. | |
739 | * 3) The OGM must be received from the first hop from orig_node. | |
740 | * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq. | |
741 | */ | |
742 | static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv, | |
743 | struct batadv_orig_node *orig_node, | |
744 | struct batadv_ogm_packet *ogm_packet) | |
745 | { | |
7351a482 | 746 | struct batadv_orig_ifinfo *orig_ifinfo; |
6b5e971a SE |
747 | u32 last_real_seqno; |
748 | u8 last_ttl; | |
7351a482 SW |
749 | |
750 | orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT); | |
751 | if (!orig_ifinfo) | |
d56b1705 | 752 | return false; |
7351a482 SW |
753 | |
754 | last_ttl = orig_ifinfo->last_ttl; | |
755 | last_real_seqno = orig_ifinfo->last_real_seqno; | |
756 | batadv_orig_ifinfo_free_ref(orig_ifinfo); | |
757 | ||
758 | if (last_real_seqno != ntohl(ogm_packet->seqno)) | |
759 | return false; | |
760 | if (last_ttl != ogm_packet->ttl + 1) | |
d56b1705 MH |
761 | return false; |
762 | if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender)) | |
763 | return false; | |
764 | if (ogm_packet->tq < bat_priv->nc.min_tq) | |
765 | return false; | |
766 | ||
767 | return true; | |
768 | } | |
769 | ||
770 | /** | |
771 | * batadv_nc_find_nc_node - search for an existing nc node and return it | |
772 | * @orig_node: orig node originating the ogm packet | |
773 | * @orig_neigh_node: neighboring orig node from which we received the ogm packet | |
774 | * (can be equal to orig_node) | |
775 | * @in_coding: traverse incoming or outgoing network coding list | |
776 | * | |
777 | * Returns the nc_node if found, NULL otherwise. | |
778 | */ | |
779 | static struct batadv_nc_node | |
780 | *batadv_nc_find_nc_node(struct batadv_orig_node *orig_node, | |
781 | struct batadv_orig_node *orig_neigh_node, | |
782 | bool in_coding) | |
783 | { | |
784 | struct batadv_nc_node *nc_node, *nc_node_out = NULL; | |
785 | struct list_head *list; | |
786 | ||
787 | if (in_coding) | |
788 | list = &orig_neigh_node->in_coding_list; | |
789 | else | |
790 | list = &orig_neigh_node->out_coding_list; | |
791 | ||
792 | /* Traverse list of nc_nodes to orig_node */ | |
793 | rcu_read_lock(); | |
794 | list_for_each_entry_rcu(nc_node, list, list) { | |
795 | if (!batadv_compare_eth(nc_node->addr, orig_node->orig)) | |
796 | continue; | |
797 | ||
798 | if (!atomic_inc_not_zero(&nc_node->refcount)) | |
799 | continue; | |
800 | ||
801 | /* Found a match */ | |
802 | nc_node_out = nc_node; | |
803 | break; | |
804 | } | |
805 | rcu_read_unlock(); | |
806 | ||
807 | return nc_node_out; | |
808 | } | |
809 | ||
810 | /** | |
811 | * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was | |
812 | * not found | |
813 | * @bat_priv: the bat priv with all the soft interface information | |
814 | * @orig_node: orig node originating the ogm packet | |
815 | * @orig_neigh_node: neighboring orig node from which we received the ogm packet | |
816 | * (can be equal to orig_node) | |
817 | * @in_coding: traverse incoming or outgoing network coding list | |
818 | * | |
819 | * Returns the nc_node if found or created, NULL in case of an error. | |
820 | */ | |
821 | static struct batadv_nc_node | |
822 | *batadv_nc_get_nc_node(struct batadv_priv *bat_priv, | |
823 | struct batadv_orig_node *orig_node, | |
824 | struct batadv_orig_node *orig_neigh_node, | |
825 | bool in_coding) | |
826 | { | |
827 | struct batadv_nc_node *nc_node; | |
828 | spinlock_t *lock; /* Used to lock list selected by "int in_coding" */ | |
829 | struct list_head *list; | |
830 | ||
831 | /* Check if nc_node is already added */ | |
832 | nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding); | |
833 | ||
834 | /* Node found */ | |
835 | if (nc_node) | |
836 | return nc_node; | |
837 | ||
838 | nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC); | |
839 | if (!nc_node) | |
840 | return NULL; | |
841 | ||
842 | if (!atomic_inc_not_zero(&orig_neigh_node->refcount)) | |
843 | goto free; | |
844 | ||
845 | /* Initialize nc_node */ | |
846 | INIT_LIST_HEAD(&nc_node->list); | |
8fdd0153 | 847 | ether_addr_copy(nc_node->addr, orig_node->orig); |
d56b1705 MH |
848 | nc_node->orig_node = orig_neigh_node; |
849 | atomic_set(&nc_node->refcount, 2); | |
850 | ||
851 | /* Select ingoing or outgoing coding node */ | |
852 | if (in_coding) { | |
853 | lock = &orig_neigh_node->in_coding_list_lock; | |
854 | list = &orig_neigh_node->in_coding_list; | |
855 | } else { | |
856 | lock = &orig_neigh_node->out_coding_list_lock; | |
857 | list = &orig_neigh_node->out_coding_list; | |
858 | } | |
859 | ||
860 | batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n", | |
861 | nc_node->addr, nc_node->orig_node->orig); | |
862 | ||
863 | /* Add nc_node to orig_node */ | |
864 | spin_lock_bh(lock); | |
865 | list_add_tail_rcu(&nc_node->list, list); | |
866 | spin_unlock_bh(lock); | |
867 | ||
868 | return nc_node; | |
869 | ||
870 | free: | |
871 | kfree(nc_node); | |
872 | return NULL; | |
873 | } | |
874 | ||
875 | /** | |
34473822 SE |
876 | * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node |
877 | * structs (best called on incoming OGMs) | |
d56b1705 MH |
878 | * @bat_priv: the bat priv with all the soft interface information |
879 | * @orig_node: orig node originating the ogm packet | |
880 | * @orig_neigh_node: neighboring orig node from which we received the ogm packet | |
881 | * (can be equal to orig_node) | |
882 | * @ogm_packet: incoming ogm packet | |
883 | * @is_single_hop_neigh: orig_node is a single hop neighbor | |
884 | */ | |
885 | void batadv_nc_update_nc_node(struct batadv_priv *bat_priv, | |
886 | struct batadv_orig_node *orig_node, | |
887 | struct batadv_orig_node *orig_neigh_node, | |
888 | struct batadv_ogm_packet *ogm_packet, | |
889 | int is_single_hop_neigh) | |
890 | { | |
4f248cff SE |
891 | struct batadv_nc_node *in_nc_node = NULL; |
892 | struct batadv_nc_node *out_nc_node = NULL; | |
d56b1705 MH |
893 | |
894 | /* Check if network coding is enabled */ | |
895 | if (!atomic_read(&bat_priv->network_coding)) | |
896 | goto out; | |
897 | ||
3f4841ff | 898 | /* check if orig node is network coding enabled */ |
4635469f | 899 | if (!test_bit(BATADV_ORIG_CAPA_HAS_NC, &orig_node->capabilities)) |
3f4841ff ML |
900 | goto out; |
901 | ||
d56b1705 MH |
902 | /* accept ogms from 'good' neighbors and single hop neighbors */ |
903 | if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) && | |
904 | !is_single_hop_neigh) | |
905 | goto out; | |
906 | ||
907 | /* Add orig_node as in_nc_node on hop */ | |
908 | in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node, | |
909 | orig_neigh_node, true); | |
910 | if (!in_nc_node) | |
911 | goto out; | |
912 | ||
913 | in_nc_node->last_seen = jiffies; | |
914 | ||
915 | /* Add hop as out_nc_node on orig_node */ | |
916 | out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node, | |
917 | orig_node, false); | |
918 | if (!out_nc_node) | |
919 | goto out; | |
920 | ||
921 | out_nc_node->last_seen = jiffies; | |
922 | ||
923 | out: | |
924 | if (in_nc_node) | |
925 | batadv_nc_node_free_ref(in_nc_node); | |
926 | if (out_nc_node) | |
927 | batadv_nc_node_free_ref(out_nc_node); | |
928 | } | |
929 | ||
95332477 MH |
930 | /** |
931 | * batadv_nc_get_path - get existing nc_path or allocate a new one | |
932 | * @bat_priv: the bat priv with all the soft interface information | |
933 | * @hash: hash table containing the nc path | |
934 | * @src: ethernet source address - first half of the nc path search key | |
935 | * @dst: ethernet destination address - second half of the nc path search key | |
936 | * | |
937 | * Returns pointer to nc_path if the path was found or created, returns NULL | |
938 | * on error. | |
939 | */ | |
940 | static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv, | |
941 | struct batadv_hashtable *hash, | |
6b5e971a SE |
942 | u8 *src, |
943 | u8 *dst) | |
95332477 MH |
944 | { |
945 | int hash_added; | |
946 | struct batadv_nc_path *nc_path, nc_path_key; | |
947 | ||
948 | batadv_nc_hash_key_gen(&nc_path_key, src, dst); | |
949 | ||
950 | /* Search for existing nc_path */ | |
951 | nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key); | |
952 | ||
953 | if (nc_path) { | |
954 | /* Set timestamp to delay removal of nc_path */ | |
955 | nc_path->last_valid = jiffies; | |
956 | return nc_path; | |
957 | } | |
958 | ||
959 | /* No existing nc_path was found; create a new */ | |
960 | nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC); | |
961 | ||
962 | if (!nc_path) | |
963 | return NULL; | |
964 | ||
965 | /* Initialize nc_path */ | |
966 | INIT_LIST_HEAD(&nc_path->packet_list); | |
967 | spin_lock_init(&nc_path->packet_list_lock); | |
968 | atomic_set(&nc_path->refcount, 2); | |
969 | nc_path->last_valid = jiffies; | |
8fdd0153 AQ |
970 | ether_addr_copy(nc_path->next_hop, dst); |
971 | ether_addr_copy(nc_path->prev_hop, src); | |
95332477 MH |
972 | |
973 | batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n", | |
974 | nc_path->prev_hop, | |
975 | nc_path->next_hop); | |
976 | ||
977 | /* Add nc_path to hash table */ | |
978 | hash_added = batadv_hash_add(hash, batadv_nc_hash_compare, | |
979 | batadv_nc_hash_choose, &nc_path_key, | |
980 | &nc_path->hash_entry); | |
981 | ||
982 | if (hash_added < 0) { | |
983 | kfree(nc_path); | |
984 | return NULL; | |
985 | } | |
986 | ||
987 | return nc_path; | |
988 | } | |
989 | ||
3c12de9a MH |
990 | /** |
991 | * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair | |
992 | * selection of a receiver with slightly lower TQ than the other | |
993 | * @tq: to be weighted tq value | |
994 | */ | |
6b5e971a | 995 | static u8 batadv_nc_random_weight_tq(u8 tq) |
3c12de9a | 996 | { |
6b5e971a | 997 | u8 rand_val, rand_tq; |
3c12de9a MH |
998 | |
999 | get_random_bytes(&rand_val, sizeof(rand_val)); | |
1000 | ||
1001 | /* randomize the estimated packet loss (max TQ - estimated TQ) */ | |
1002 | rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq); | |
1003 | ||
1004 | /* normalize the randomized packet loss */ | |
1005 | rand_tq /= BATADV_TQ_MAX_VALUE; | |
1006 | ||
1007 | /* convert to (randomized) estimated tq again */ | |
1008 | return BATADV_TQ_MAX_VALUE - rand_tq; | |
1009 | } | |
1010 | ||
1011 | /** | |
1012 | * batadv_nc_memxor - XOR destination with source | |
1013 | * @dst: byte array to XOR into | |
1014 | * @src: byte array to XOR from | |
1015 | * @len: length of destination array | |
1016 | */ | |
1017 | static void batadv_nc_memxor(char *dst, const char *src, unsigned int len) | |
1018 | { | |
1019 | unsigned int i; | |
1020 | ||
1021 | for (i = 0; i < len; ++i) | |
1022 | dst[i] ^= src[i]; | |
1023 | } | |
1024 | ||
1025 | /** | |
1026 | * batadv_nc_code_packets - code a received unicast_packet with an nc packet | |
1027 | * into a coded_packet and send it | |
1028 | * @bat_priv: the bat priv with all the soft interface information | |
1029 | * @skb: data skb to forward | |
1030 | * @ethhdr: pointer to the ethernet header inside the skb | |
1031 | * @nc_packet: structure containing the packet to the skb can be coded with | |
1032 | * @neigh_node: next hop to forward packet to | |
1033 | * | |
1034 | * Returns true if both packets are consumed, false otherwise. | |
1035 | */ | |
1036 | static bool batadv_nc_code_packets(struct batadv_priv *bat_priv, | |
1037 | struct sk_buff *skb, | |
1038 | struct ethhdr *ethhdr, | |
1039 | struct batadv_nc_packet *nc_packet, | |
1040 | struct batadv_neigh_node *neigh_node) | |
1041 | { | |
6b5e971a | 1042 | u8 tq_weighted_neigh, tq_weighted_coding, tq_tmp; |
3c12de9a MH |
1043 | struct sk_buff *skb_dest, *skb_src; |
1044 | struct batadv_unicast_packet *packet1; | |
1045 | struct batadv_unicast_packet *packet2; | |
1046 | struct batadv_coded_packet *coded_packet; | |
1047 | struct batadv_neigh_node *neigh_tmp, *router_neigh; | |
1048 | struct batadv_neigh_node *router_coding = NULL; | |
89652331 SW |
1049 | struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL; |
1050 | struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL; | |
6b5e971a | 1051 | u8 *first_source, *first_dest, *second_source, *second_dest; |
3c12de9a MH |
1052 | __be32 packet_id1, packet_id2; |
1053 | size_t count; | |
1054 | bool res = false; | |
1055 | int coding_len; | |
1056 | int unicast_size = sizeof(*packet1); | |
1057 | int coded_size = sizeof(*coded_packet); | |
1058 | int header_add = coded_size - unicast_size; | |
1059 | ||
7351a482 SW |
1060 | /* TODO: do we need to consider the outgoing interface for |
1061 | * coded packets? | |
1062 | */ | |
1063 | router_neigh = batadv_orig_router_get(neigh_node->orig_node, | |
1064 | BATADV_IF_DEFAULT); | |
3c12de9a MH |
1065 | if (!router_neigh) |
1066 | goto out; | |
1067 | ||
89652331 SW |
1068 | router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh, |
1069 | BATADV_IF_DEFAULT); | |
1070 | if (!router_neigh_ifinfo) | |
1071 | goto out; | |
1072 | ||
3c12de9a | 1073 | neigh_tmp = nc_packet->neigh_node; |
7351a482 SW |
1074 | router_coding = batadv_orig_router_get(neigh_tmp->orig_node, |
1075 | BATADV_IF_DEFAULT); | |
3c12de9a MH |
1076 | if (!router_coding) |
1077 | goto out; | |
1078 | ||
89652331 SW |
1079 | router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding, |
1080 | BATADV_IF_DEFAULT); | |
1081 | if (!router_coding_ifinfo) | |
1082 | goto out; | |
1083 | ||
1084 | tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg; | |
1085 | tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp); | |
1086 | tq_tmp = router_coding_ifinfo->bat_iv.tq_avg; | |
1087 | tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp); | |
3c12de9a MH |
1088 | |
1089 | /* Select one destination for the MAC-header dst-field based on | |
1090 | * weighted TQ-values. | |
1091 | */ | |
1092 | if (tq_weighted_neigh >= tq_weighted_coding) { | |
1093 | /* Destination from nc_packet is selected for MAC-header */ | |
1094 | first_dest = nc_packet->nc_path->next_hop; | |
1095 | first_source = nc_packet->nc_path->prev_hop; | |
1096 | second_dest = neigh_node->addr; | |
1097 | second_source = ethhdr->h_source; | |
1098 | packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data; | |
1099 | packet2 = (struct batadv_unicast_packet *)skb->data; | |
1100 | packet_id1 = nc_packet->packet_id; | |
1101 | packet_id2 = batadv_skb_crc32(skb, | |
1102 | skb->data + sizeof(*packet2)); | |
1103 | } else { | |
1104 | /* Destination for skb is selected for MAC-header */ | |
1105 | first_dest = neigh_node->addr; | |
1106 | first_source = ethhdr->h_source; | |
1107 | second_dest = nc_packet->nc_path->next_hop; | |
1108 | second_source = nc_packet->nc_path->prev_hop; | |
1109 | packet1 = (struct batadv_unicast_packet *)skb->data; | |
1110 | packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data; | |
1111 | packet_id1 = batadv_skb_crc32(skb, | |
1112 | skb->data + sizeof(*packet1)); | |
1113 | packet_id2 = nc_packet->packet_id; | |
1114 | } | |
1115 | ||
1116 | /* Instead of zero padding the smallest data buffer, we | |
1117 | * code into the largest. | |
1118 | */ | |
1119 | if (skb->len <= nc_packet->skb->len) { | |
1120 | skb_dest = nc_packet->skb; | |
1121 | skb_src = skb; | |
1122 | } else { | |
1123 | skb_dest = skb; | |
1124 | skb_src = nc_packet->skb; | |
1125 | } | |
1126 | ||
1127 | /* coding_len is used when decoding the packet shorter packet */ | |
1128 | coding_len = skb_src->len - unicast_size; | |
1129 | ||
1130 | if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0) | |
1131 | goto out; | |
1132 | ||
1133 | skb_push(skb_dest, header_add); | |
1134 | ||
1135 | coded_packet = (struct batadv_coded_packet *)skb_dest->data; | |
1136 | skb_reset_mac_header(skb_dest); | |
1137 | ||
a40d9b07 SW |
1138 | coded_packet->packet_type = BATADV_CODED; |
1139 | coded_packet->version = BATADV_COMPAT_VERSION; | |
1140 | coded_packet->ttl = packet1->ttl; | |
3c12de9a MH |
1141 | |
1142 | /* Info about first unicast packet */ | |
8fdd0153 AQ |
1143 | ether_addr_copy(coded_packet->first_source, first_source); |
1144 | ether_addr_copy(coded_packet->first_orig_dest, packet1->dest); | |
3c12de9a MH |
1145 | coded_packet->first_crc = packet_id1; |
1146 | coded_packet->first_ttvn = packet1->ttvn; | |
1147 | ||
1148 | /* Info about second unicast packet */ | |
8fdd0153 AQ |
1149 | ether_addr_copy(coded_packet->second_dest, second_dest); |
1150 | ether_addr_copy(coded_packet->second_source, second_source); | |
1151 | ether_addr_copy(coded_packet->second_orig_dest, packet2->dest); | |
3c12de9a | 1152 | coded_packet->second_crc = packet_id2; |
a40d9b07 | 1153 | coded_packet->second_ttl = packet2->ttl; |
3c12de9a MH |
1154 | coded_packet->second_ttvn = packet2->ttvn; |
1155 | coded_packet->coded_len = htons(coding_len); | |
1156 | ||
1157 | /* This is where the magic happens: Code skb_src into skb_dest */ | |
1158 | batadv_nc_memxor(skb_dest->data + coded_size, | |
1159 | skb_src->data + unicast_size, coding_len); | |
1160 | ||
1161 | /* Update counters accordingly */ | |
1162 | if (BATADV_SKB_CB(skb_src)->decoded && | |
1163 | BATADV_SKB_CB(skb_dest)->decoded) { | |
1164 | /* Both packets are recoded */ | |
1165 | count = skb_src->len + ETH_HLEN; | |
1166 | count += skb_dest->len + ETH_HLEN; | |
1167 | batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2); | |
1168 | batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count); | |
1169 | } else if (!BATADV_SKB_CB(skb_src)->decoded && | |
1170 | !BATADV_SKB_CB(skb_dest)->decoded) { | |
1171 | /* Both packets are newly coded */ | |
1172 | count = skb_src->len + ETH_HLEN; | |
1173 | count += skb_dest->len + ETH_HLEN; | |
1174 | batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2); | |
1175 | batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count); | |
1176 | } else if (BATADV_SKB_CB(skb_src)->decoded && | |
1177 | !BATADV_SKB_CB(skb_dest)->decoded) { | |
1178 | /* skb_src recoded and skb_dest is newly coded */ | |
1179 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE); | |
1180 | batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, | |
1181 | skb_src->len + ETH_HLEN); | |
1182 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE); | |
1183 | batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, | |
1184 | skb_dest->len + ETH_HLEN); | |
1185 | } else if (!BATADV_SKB_CB(skb_src)->decoded && | |
1186 | BATADV_SKB_CB(skb_dest)->decoded) { | |
1187 | /* skb_src is newly coded and skb_dest is recoded */ | |
1188 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE); | |
1189 | batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, | |
1190 | skb_src->len + ETH_HLEN); | |
1191 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE); | |
1192 | batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, | |
1193 | skb_dest->len + ETH_HLEN); | |
1194 | } | |
1195 | ||
1196 | /* skb_src is now coded into skb_dest, so free it */ | |
1197 | kfree_skb(skb_src); | |
1198 | ||
1199 | /* avoid duplicate free of skb from nc_packet */ | |
1200 | nc_packet->skb = NULL; | |
1201 | batadv_nc_packet_free(nc_packet); | |
1202 | ||
1203 | /* Send the coded packet and return true */ | |
1204 | batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest); | |
1205 | res = true; | |
1206 | out: | |
1207 | if (router_neigh) | |
1208 | batadv_neigh_node_free_ref(router_neigh); | |
1209 | if (router_coding) | |
1210 | batadv_neigh_node_free_ref(router_coding); | |
89652331 SW |
1211 | if (router_neigh_ifinfo) |
1212 | batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo); | |
1213 | if (router_coding_ifinfo) | |
1214 | batadv_neigh_ifinfo_free_ref(router_coding_ifinfo); | |
3c12de9a MH |
1215 | return res; |
1216 | } | |
1217 | ||
1218 | /** | |
1219 | * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst. | |
1220 | * @skb: data skb to forward | |
1221 | * @dst: destination mac address of the other skb to code with | |
1222 | * @src: source mac address of skb | |
1223 | * | |
1224 | * Whenever we network code a packet we have to check whether we received it in | |
1225 | * a network coded form. If so, we may not be able to use it for coding because | |
1226 | * some neighbors may also have received (overheard) the packet in the network | |
1227 | * coded form without being able to decode it. It is hard to know which of the | |
1228 | * neighboring nodes was able to decode the packet, therefore we can only | |
1229 | * re-code the packet if the source of the previous encoded packet is involved. | |
1230 | * Since the source encoded the packet we can be certain it has all necessary | |
1231 | * decode information. | |
1232 | * | |
1233 | * Returns true if coding of a decoded packet is allowed. | |
1234 | */ | |
6b5e971a | 1235 | static bool batadv_nc_skb_coding_possible(struct sk_buff *skb, u8 *dst, u8 *src) |
3c12de9a MH |
1236 | { |
1237 | if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src)) | |
1238 | return false; | |
24820df1 | 1239 | return true; |
3c12de9a MH |
1240 | } |
1241 | ||
1242 | /** | |
1243 | * batadv_nc_path_search - Find the coding path matching in_nc_node and | |
1244 | * out_nc_node to retrieve a buffered packet that can be used for coding. | |
1245 | * @bat_priv: the bat priv with all the soft interface information | |
1246 | * @in_nc_node: pointer to skb next hop's neighbor nc node | |
1247 | * @out_nc_node: pointer to skb source's neighbor nc node | |
1248 | * @skb: data skb to forward | |
1249 | * @eth_dst: next hop mac address of skb | |
1250 | * | |
1251 | * Returns true if coding of a decoded skb is allowed. | |
1252 | */ | |
1253 | static struct batadv_nc_packet * | |
1254 | batadv_nc_path_search(struct batadv_priv *bat_priv, | |
1255 | struct batadv_nc_node *in_nc_node, | |
1256 | struct batadv_nc_node *out_nc_node, | |
1257 | struct sk_buff *skb, | |
6b5e971a | 1258 | u8 *eth_dst) |
3c12de9a MH |
1259 | { |
1260 | struct batadv_nc_path *nc_path, nc_path_key; | |
1261 | struct batadv_nc_packet *nc_packet_out = NULL; | |
1262 | struct batadv_nc_packet *nc_packet, *nc_packet_tmp; | |
1263 | struct batadv_hashtable *hash = bat_priv->nc.coding_hash; | |
1264 | int idx; | |
1265 | ||
1266 | if (!hash) | |
1267 | return NULL; | |
1268 | ||
1269 | /* Create almost path key */ | |
1270 | batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr, | |
1271 | out_nc_node->addr); | |
1272 | idx = batadv_nc_hash_choose(&nc_path_key, hash->size); | |
1273 | ||
1274 | /* Check for coding opportunities in this nc_path */ | |
1275 | rcu_read_lock(); | |
1276 | hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) { | |
1277 | if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr)) | |
1278 | continue; | |
1279 | ||
1280 | if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr)) | |
1281 | continue; | |
1282 | ||
1283 | spin_lock_bh(&nc_path->packet_list_lock); | |
1284 | if (list_empty(&nc_path->packet_list)) { | |
1285 | spin_unlock_bh(&nc_path->packet_list_lock); | |
1286 | continue; | |
1287 | } | |
1288 | ||
1289 | list_for_each_entry_safe(nc_packet, nc_packet_tmp, | |
1290 | &nc_path->packet_list, list) { | |
1291 | if (!batadv_nc_skb_coding_possible(nc_packet->skb, | |
1292 | eth_dst, | |
1293 | in_nc_node->addr)) | |
1294 | continue; | |
1295 | ||
1296 | /* Coding opportunity is found! */ | |
1297 | list_del(&nc_packet->list); | |
1298 | nc_packet_out = nc_packet; | |
1299 | break; | |
1300 | } | |
1301 | ||
1302 | spin_unlock_bh(&nc_path->packet_list_lock); | |
1303 | break; | |
1304 | } | |
1305 | rcu_read_unlock(); | |
1306 | ||
1307 | return nc_packet_out; | |
1308 | } | |
1309 | ||
1310 | /** | |
1311 | * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the | |
1312 | * skb's sender (may be equal to the originator). | |
1313 | * @bat_priv: the bat priv with all the soft interface information | |
1314 | * @skb: data skb to forward | |
1315 | * @eth_dst: next hop mac address of skb | |
1316 | * @eth_src: source mac address of skb | |
1317 | * @in_nc_node: pointer to skb next hop's neighbor nc node | |
1318 | * | |
1319 | * Returns an nc packet if a suitable coding packet was found, NULL otherwise. | |
1320 | */ | |
1321 | static struct batadv_nc_packet * | |
1322 | batadv_nc_skb_src_search(struct batadv_priv *bat_priv, | |
1323 | struct sk_buff *skb, | |
6b5e971a SE |
1324 | u8 *eth_dst, |
1325 | u8 *eth_src, | |
3c12de9a MH |
1326 | struct batadv_nc_node *in_nc_node) |
1327 | { | |
1328 | struct batadv_orig_node *orig_node; | |
1329 | struct batadv_nc_node *out_nc_node; | |
1330 | struct batadv_nc_packet *nc_packet = NULL; | |
1331 | ||
1332 | orig_node = batadv_orig_hash_find(bat_priv, eth_src); | |
1333 | if (!orig_node) | |
1334 | return NULL; | |
1335 | ||
1336 | rcu_read_lock(); | |
1337 | list_for_each_entry_rcu(out_nc_node, | |
1338 | &orig_node->out_coding_list, list) { | |
1339 | /* Check if the skb is decoded and if recoding is possible */ | |
1340 | if (!batadv_nc_skb_coding_possible(skb, | |
1341 | out_nc_node->addr, eth_src)) | |
1342 | continue; | |
1343 | ||
1344 | /* Search for an opportunity in this nc_path */ | |
1345 | nc_packet = batadv_nc_path_search(bat_priv, in_nc_node, | |
1346 | out_nc_node, skb, eth_dst); | |
1347 | if (nc_packet) | |
1348 | break; | |
1349 | } | |
1350 | rcu_read_unlock(); | |
1351 | ||
1352 | batadv_orig_node_free_ref(orig_node); | |
1353 | return nc_packet; | |
1354 | } | |
1355 | ||
612d2b4f MH |
1356 | /** |
1357 | * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the | |
1358 | * unicast skb before it is stored for use in later decoding | |
1359 | * @bat_priv: the bat priv with all the soft interface information | |
1360 | * @skb: data skb to store | |
1361 | * @eth_dst_new: new destination mac address of skb | |
1362 | */ | |
1363 | static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv, | |
1364 | struct sk_buff *skb, | |
6b5e971a | 1365 | u8 *eth_dst_new) |
612d2b4f MH |
1366 | { |
1367 | struct ethhdr *ethhdr; | |
1368 | ||
1369 | /* Copy skb header to change the mac header */ | |
bad93e9d | 1370 | skb = pskb_copy_for_clone(skb, GFP_ATOMIC); |
612d2b4f MH |
1371 | if (!skb) |
1372 | return; | |
1373 | ||
1374 | /* Set the mac header as if we actually sent the packet uncoded */ | |
7ed4be95 | 1375 | ethhdr = eth_hdr(skb); |
8fdd0153 AQ |
1376 | ether_addr_copy(ethhdr->h_source, ethhdr->h_dest); |
1377 | ether_addr_copy(ethhdr->h_dest, eth_dst_new); | |
612d2b4f MH |
1378 | |
1379 | /* Set data pointer to MAC header to mimic packets from our tx path */ | |
1380 | skb_push(skb, ETH_HLEN); | |
1381 | ||
1382 | /* Add the packet to the decoding packet pool */ | |
1383 | batadv_nc_skb_store_for_decoding(bat_priv, skb); | |
1384 | ||
1385 | /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free | |
1386 | * our ref | |
1387 | */ | |
1388 | kfree_skb(skb); | |
1389 | } | |
1390 | ||
3c12de9a MH |
1391 | /** |
1392 | * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst. | |
1393 | * @skb: data skb to forward | |
1394 | * @neigh_node: next hop to forward packet to | |
1395 | * @ethhdr: pointer to the ethernet header inside the skb | |
1396 | * | |
1397 | * Loops through list of neighboring nodes the next hop has a good connection to | |
1398 | * (receives OGMs with a sufficient quality). We need to find a neighbor of our | |
1399 | * next hop that potentially sent a packet which our next hop also received | |
1400 | * (overheard) and has stored for later decoding. | |
1401 | * | |
1402 | * Returns true if the skb was consumed (encoded packet sent) or false otherwise | |
1403 | */ | |
1404 | static bool batadv_nc_skb_dst_search(struct sk_buff *skb, | |
1405 | struct batadv_neigh_node *neigh_node, | |
1406 | struct ethhdr *ethhdr) | |
1407 | { | |
1408 | struct net_device *netdev = neigh_node->if_incoming->soft_iface; | |
1409 | struct batadv_priv *bat_priv = netdev_priv(netdev); | |
1410 | struct batadv_orig_node *orig_node = neigh_node->orig_node; | |
1411 | struct batadv_nc_node *nc_node; | |
1412 | struct batadv_nc_packet *nc_packet = NULL; | |
1413 | ||
1414 | rcu_read_lock(); | |
1415 | list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) { | |
1416 | /* Search for coding opportunity with this in_nc_node */ | |
1417 | nc_packet = batadv_nc_skb_src_search(bat_priv, skb, | |
1418 | neigh_node->addr, | |
1419 | ethhdr->h_source, nc_node); | |
1420 | ||
1421 | /* Opportunity was found, so stop searching */ | |
1422 | if (nc_packet) | |
1423 | break; | |
1424 | } | |
1425 | rcu_read_unlock(); | |
1426 | ||
1427 | if (!nc_packet) | |
1428 | return false; | |
1429 | ||
612d2b4f MH |
1430 | /* Save packets for later decoding */ |
1431 | batadv_nc_skb_store_before_coding(bat_priv, skb, | |
1432 | neigh_node->addr); | |
1433 | batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb, | |
1434 | nc_packet->neigh_node->addr); | |
1435 | ||
3c12de9a MH |
1436 | /* Code and send packets */ |
1437 | if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet, | |
1438 | neigh_node)) | |
1439 | return true; | |
1440 | ||
1441 | /* out of mem ? Coding failed - we have to free the buffered packet | |
1442 | * to avoid memleaks. The skb passed as argument will be dealt with | |
1443 | * by the calling function. | |
1444 | */ | |
1445 | batadv_nc_send_packet(nc_packet); | |
1446 | return false; | |
1447 | } | |
1448 | ||
95332477 MH |
1449 | /** |
1450 | * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding | |
1451 | * @skb: skb to add to path | |
1452 | * @nc_path: path to add skb to | |
1453 | * @neigh_node: next hop to forward packet to | |
1454 | * @packet_id: checksum to identify packet | |
1455 | * | |
1456 | * Returns true if the packet was buffered or false in case of an error. | |
1457 | */ | |
1458 | static bool batadv_nc_skb_add_to_path(struct sk_buff *skb, | |
1459 | struct batadv_nc_path *nc_path, | |
1460 | struct batadv_neigh_node *neigh_node, | |
1461 | __be32 packet_id) | |
1462 | { | |
1463 | struct batadv_nc_packet *nc_packet; | |
1464 | ||
1465 | nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC); | |
1466 | if (!nc_packet) | |
1467 | return false; | |
1468 | ||
1469 | /* Initialize nc_packet */ | |
1470 | nc_packet->timestamp = jiffies; | |
1471 | nc_packet->packet_id = packet_id; | |
1472 | nc_packet->skb = skb; | |
1473 | nc_packet->neigh_node = neigh_node; | |
1474 | nc_packet->nc_path = nc_path; | |
1475 | ||
1476 | /* Add coding packet to list */ | |
1477 | spin_lock_bh(&nc_path->packet_list_lock); | |
1478 | list_add_tail(&nc_packet->list, &nc_path->packet_list); | |
1479 | spin_unlock_bh(&nc_path->packet_list_lock); | |
1480 | ||
1481 | return true; | |
1482 | } | |
1483 | ||
1484 | /** | |
1485 | * batadv_nc_skb_forward - try to code a packet or add it to the coding packet | |
1486 | * buffer | |
1487 | * @skb: data skb to forward | |
1488 | * @neigh_node: next hop to forward packet to | |
95332477 MH |
1489 | * |
1490 | * Returns true if the skb was consumed (encoded packet sent) or false otherwise | |
1491 | */ | |
1492 | bool batadv_nc_skb_forward(struct sk_buff *skb, | |
e91ecfc6 | 1493 | struct batadv_neigh_node *neigh_node) |
95332477 MH |
1494 | { |
1495 | const struct net_device *netdev = neigh_node->if_incoming->soft_iface; | |
1496 | struct batadv_priv *bat_priv = netdev_priv(netdev); | |
1497 | struct batadv_unicast_packet *packet; | |
1498 | struct batadv_nc_path *nc_path; | |
e91ecfc6 | 1499 | struct ethhdr *ethhdr = eth_hdr(skb); |
95332477 MH |
1500 | __be32 packet_id; |
1501 | u8 *payload; | |
1502 | ||
1503 | /* Check if network coding is enabled */ | |
1504 | if (!atomic_read(&bat_priv->network_coding)) | |
1505 | goto out; | |
1506 | ||
1507 | /* We only handle unicast packets */ | |
1508 | payload = skb_network_header(skb); | |
1509 | packet = (struct batadv_unicast_packet *)payload; | |
a40d9b07 | 1510 | if (packet->packet_type != BATADV_UNICAST) |
95332477 MH |
1511 | goto out; |
1512 | ||
3c12de9a MH |
1513 | /* Try to find a coding opportunity and send the skb if one is found */ |
1514 | if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr)) | |
1515 | return true; | |
1516 | ||
95332477 MH |
1517 | /* Find or create a nc_path for this src-dst pair */ |
1518 | nc_path = batadv_nc_get_path(bat_priv, | |
1519 | bat_priv->nc.coding_hash, | |
1520 | ethhdr->h_source, | |
1521 | neigh_node->addr); | |
1522 | ||
1523 | if (!nc_path) | |
1524 | goto out; | |
1525 | ||
1526 | /* Add skb to nc_path */ | |
1527 | packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet)); | |
1528 | if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id)) | |
1529 | goto free_nc_path; | |
1530 | ||
1531 | /* Packet is consumed */ | |
1532 | return true; | |
1533 | ||
1534 | free_nc_path: | |
1535 | batadv_nc_path_free_ref(nc_path); | |
1536 | out: | |
1537 | /* Packet is not consumed */ | |
1538 | return false; | |
1539 | } | |
1540 | ||
612d2b4f MH |
1541 | /** |
1542 | * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used | |
1543 | * when decoding coded packets | |
1544 | * @bat_priv: the bat priv with all the soft interface information | |
1545 | * @skb: data skb to store | |
1546 | */ | |
1547 | void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv, | |
1548 | struct sk_buff *skb) | |
1549 | { | |
1550 | struct batadv_unicast_packet *packet; | |
1551 | struct batadv_nc_path *nc_path; | |
7ed4be95 | 1552 | struct ethhdr *ethhdr = eth_hdr(skb); |
612d2b4f MH |
1553 | __be32 packet_id; |
1554 | u8 *payload; | |
1555 | ||
1556 | /* Check if network coding is enabled */ | |
1557 | if (!atomic_read(&bat_priv->network_coding)) | |
1558 | goto out; | |
1559 | ||
1560 | /* Check for supported packet type */ | |
1561 | payload = skb_network_header(skb); | |
1562 | packet = (struct batadv_unicast_packet *)payload; | |
a40d9b07 | 1563 | if (packet->packet_type != BATADV_UNICAST) |
612d2b4f MH |
1564 | goto out; |
1565 | ||
1566 | /* Find existing nc_path or create a new */ | |
1567 | nc_path = batadv_nc_get_path(bat_priv, | |
1568 | bat_priv->nc.decoding_hash, | |
1569 | ethhdr->h_source, | |
1570 | ethhdr->h_dest); | |
1571 | ||
1572 | if (!nc_path) | |
1573 | goto out; | |
1574 | ||
1575 | /* Clone skb and adjust skb->data to point at batman header */ | |
1576 | skb = skb_clone(skb, GFP_ATOMIC); | |
1577 | if (unlikely(!skb)) | |
1578 | goto free_nc_path; | |
1579 | ||
1580 | if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) | |
1581 | goto free_skb; | |
1582 | ||
1583 | if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN))) | |
1584 | goto free_skb; | |
1585 | ||
1586 | /* Add skb to nc_path */ | |
1587 | packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet)); | |
1588 | if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id)) | |
1589 | goto free_skb; | |
1590 | ||
1591 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER); | |
1592 | return; | |
1593 | ||
1594 | free_skb: | |
1595 | kfree_skb(skb); | |
1596 | free_nc_path: | |
1597 | batadv_nc_path_free_ref(nc_path); | |
1598 | out: | |
1599 | return; | |
1600 | } | |
1601 | ||
1602 | /** | |
1603 | * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet | |
1604 | * should be saved in the decoding buffer and, if so, store it there | |
1605 | * @bat_priv: the bat priv with all the soft interface information | |
1606 | * @skb: unicast skb to store | |
1607 | */ | |
1608 | void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv, | |
1609 | struct sk_buff *skb) | |
1610 | { | |
7ed4be95 | 1611 | struct ethhdr *ethhdr = eth_hdr(skb); |
612d2b4f | 1612 | |
6e0895c2 | 1613 | if (batadv_is_my_mac(bat_priv, ethhdr->h_dest)) |
612d2b4f MH |
1614 | return; |
1615 | ||
1616 | /* Set data pointer to MAC header to mimic packets from our tx path */ | |
1617 | skb_push(skb, ETH_HLEN); | |
1618 | ||
1619 | batadv_nc_skb_store_for_decoding(bat_priv, skb); | |
1620 | } | |
1621 | ||
2df5278b MH |
1622 | /** |
1623 | * batadv_nc_skb_decode_packet - decode given skb using the decode data stored | |
1624 | * in nc_packet | |
6e0895c2 | 1625 | * @bat_priv: the bat priv with all the soft interface information |
2df5278b MH |
1626 | * @skb: unicast skb to decode |
1627 | * @nc_packet: decode data needed to decode the skb | |
1628 | * | |
1629 | * Returns pointer to decoded unicast packet if the packet was decoded or NULL | |
1630 | * in case of an error. | |
1631 | */ | |
1632 | static struct batadv_unicast_packet * | |
6e0895c2 | 1633 | batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb, |
2df5278b MH |
1634 | struct batadv_nc_packet *nc_packet) |
1635 | { | |
1636 | const int h_size = sizeof(struct batadv_unicast_packet); | |
1637 | const int h_diff = sizeof(struct batadv_coded_packet) - h_size; | |
1638 | struct batadv_unicast_packet *unicast_packet; | |
1639 | struct batadv_coded_packet coded_packet_tmp; | |
1640 | struct ethhdr *ethhdr, ethhdr_tmp; | |
6b5e971a | 1641 | u8 *orig_dest, ttl, ttvn; |
2df5278b | 1642 | unsigned int coding_len; |
7da19971 | 1643 | int err; |
2df5278b MH |
1644 | |
1645 | /* Save headers temporarily */ | |
1646 | memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp)); | |
1647 | memcpy(ðhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp)); | |
1648 | ||
1649 | if (skb_cow(skb, 0) < 0) | |
1650 | return NULL; | |
1651 | ||
1652 | if (unlikely(!skb_pull_rcsum(skb, h_diff))) | |
1653 | return NULL; | |
1654 | ||
1655 | /* Data points to batman header, so set mac header 14 bytes before | |
1656 | * and network to data | |
1657 | */ | |
1658 | skb_set_mac_header(skb, -ETH_HLEN); | |
1659 | skb_reset_network_header(skb); | |
1660 | ||
1661 | /* Reconstruct original mac header */ | |
7ed4be95 | 1662 | ethhdr = eth_hdr(skb); |
abae9479 | 1663 | *ethhdr = ethhdr_tmp; |
2df5278b MH |
1664 | |
1665 | /* Select the correct unicast header information based on the location | |
1666 | * of our mac address in the coded_packet header | |
1667 | */ | |
6e0895c2 | 1668 | if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) { |
2df5278b MH |
1669 | /* If we are the second destination the packet was overheard, |
1670 | * so the Ethernet address must be copied to h_dest and | |
1671 | * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST | |
1672 | */ | |
8fdd0153 | 1673 | ether_addr_copy(ethhdr->h_dest, coded_packet_tmp.second_dest); |
2df5278b MH |
1674 | skb->pkt_type = PACKET_HOST; |
1675 | ||
1676 | orig_dest = coded_packet_tmp.second_orig_dest; | |
1677 | ttl = coded_packet_tmp.second_ttl; | |
1678 | ttvn = coded_packet_tmp.second_ttvn; | |
1679 | } else { | |
1680 | orig_dest = coded_packet_tmp.first_orig_dest; | |
a40d9b07 | 1681 | ttl = coded_packet_tmp.ttl; |
2df5278b MH |
1682 | ttvn = coded_packet_tmp.first_ttvn; |
1683 | } | |
1684 | ||
1685 | coding_len = ntohs(coded_packet_tmp.coded_len); | |
1686 | ||
1687 | if (coding_len > skb->len) | |
1688 | return NULL; | |
1689 | ||
1690 | /* Here the magic is reversed: | |
1691 | * extract the missing packet from the received coded packet | |
1692 | */ | |
1693 | batadv_nc_memxor(skb->data + h_size, | |
1694 | nc_packet->skb->data + h_size, | |
1695 | coding_len); | |
1696 | ||
1697 | /* Resize decoded skb if decoded with larger packet */ | |
7da19971 ML |
1698 | if (nc_packet->skb->len > coding_len + h_size) { |
1699 | err = pskb_trim_rcsum(skb, coding_len + h_size); | |
1700 | if (err) | |
1701 | return NULL; | |
1702 | } | |
2df5278b MH |
1703 | |
1704 | /* Create decoded unicast packet */ | |
1705 | unicast_packet = (struct batadv_unicast_packet *)skb->data; | |
a40d9b07 SW |
1706 | unicast_packet->packet_type = BATADV_UNICAST; |
1707 | unicast_packet->version = BATADV_COMPAT_VERSION; | |
1708 | unicast_packet->ttl = ttl; | |
8fdd0153 | 1709 | ether_addr_copy(unicast_packet->dest, orig_dest); |
2df5278b MH |
1710 | unicast_packet->ttvn = ttvn; |
1711 | ||
1712 | batadv_nc_packet_free(nc_packet); | |
1713 | return unicast_packet; | |
1714 | } | |
1715 | ||
1716 | /** | |
1717 | * batadv_nc_find_decoding_packet - search through buffered decoding data to | |
1718 | * find the data needed to decode the coded packet | |
1719 | * @bat_priv: the bat priv with all the soft interface information | |
1720 | * @ethhdr: pointer to the ethernet header inside the coded packet | |
1721 | * @coded: coded packet we try to find decode data for | |
1722 | * | |
1723 | * Returns pointer to nc packet if the needed data was found or NULL otherwise. | |
1724 | */ | |
1725 | static struct batadv_nc_packet * | |
1726 | batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv, | |
1727 | struct ethhdr *ethhdr, | |
1728 | struct batadv_coded_packet *coded) | |
1729 | { | |
1730 | struct batadv_hashtable *hash = bat_priv->nc.decoding_hash; | |
1731 | struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL; | |
1732 | struct batadv_nc_path *nc_path, nc_path_key; | |
6b5e971a | 1733 | u8 *dest, *source; |
2df5278b MH |
1734 | __be32 packet_id; |
1735 | int index; | |
1736 | ||
1737 | if (!hash) | |
1738 | return NULL; | |
1739 | ||
1740 | /* Select the correct packet id based on the location of our mac-addr */ | |
1741 | dest = ethhdr->h_source; | |
6e0895c2 | 1742 | if (!batadv_is_my_mac(bat_priv, coded->second_dest)) { |
2df5278b MH |
1743 | source = coded->second_source; |
1744 | packet_id = coded->second_crc; | |
1745 | } else { | |
1746 | source = coded->first_source; | |
1747 | packet_id = coded->first_crc; | |
1748 | } | |
1749 | ||
1750 | batadv_nc_hash_key_gen(&nc_path_key, source, dest); | |
1751 | index = batadv_nc_hash_choose(&nc_path_key, hash->size); | |
1752 | ||
1753 | /* Search for matching coding path */ | |
1754 | rcu_read_lock(); | |
1755 | hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) { | |
1756 | /* Find matching nc_packet */ | |
1757 | spin_lock_bh(&nc_path->packet_list_lock); | |
1758 | list_for_each_entry(tmp_nc_packet, | |
1759 | &nc_path->packet_list, list) { | |
1760 | if (packet_id == tmp_nc_packet->packet_id) { | |
1761 | list_del(&tmp_nc_packet->list); | |
1762 | ||
1763 | nc_packet = tmp_nc_packet; | |
1764 | break; | |
1765 | } | |
1766 | } | |
1767 | spin_unlock_bh(&nc_path->packet_list_lock); | |
1768 | ||
1769 | if (nc_packet) | |
1770 | break; | |
1771 | } | |
1772 | rcu_read_unlock(); | |
1773 | ||
1774 | if (!nc_packet) | |
1775 | batadv_dbg(BATADV_DBG_NC, bat_priv, | |
1776 | "No decoding packet found for %u\n", packet_id); | |
1777 | ||
1778 | return nc_packet; | |
1779 | } | |
1780 | ||
1781 | /** | |
1782 | * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the | |
1783 | * resulting unicast packet | |
1784 | * @skb: incoming coded packet | |
1785 | * @recv_if: pointer to interface this packet was received on | |
1786 | */ | |
1787 | static int batadv_nc_recv_coded_packet(struct sk_buff *skb, | |
1788 | struct batadv_hard_iface *recv_if) | |
1789 | { | |
1790 | struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
1791 | struct batadv_unicast_packet *unicast_packet; | |
1792 | struct batadv_coded_packet *coded_packet; | |
1793 | struct batadv_nc_packet *nc_packet; | |
1794 | struct ethhdr *ethhdr; | |
1795 | int hdr_size = sizeof(*coded_packet); | |
1796 | ||
1797 | /* Check if network coding is enabled */ | |
1798 | if (!atomic_read(&bat_priv->network_coding)) | |
1799 | return NET_RX_DROP; | |
1800 | ||
1801 | /* Make sure we can access (and remove) header */ | |
1802 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
1803 | return NET_RX_DROP; | |
1804 | ||
1805 | coded_packet = (struct batadv_coded_packet *)skb->data; | |
7ed4be95 | 1806 | ethhdr = eth_hdr(skb); |
2df5278b MH |
1807 | |
1808 | /* Verify frame is destined for us */ | |
6e0895c2 DM |
1809 | if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) && |
1810 | !batadv_is_my_mac(bat_priv, coded_packet->second_dest)) | |
2df5278b MH |
1811 | return NET_RX_DROP; |
1812 | ||
1813 | /* Update stat counter */ | |
6e0895c2 | 1814 | if (batadv_is_my_mac(bat_priv, coded_packet->second_dest)) |
2df5278b MH |
1815 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED); |
1816 | ||
1817 | nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr, | |
1818 | coded_packet); | |
1819 | if (!nc_packet) { | |
1820 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED); | |
1821 | return NET_RX_DROP; | |
1822 | } | |
1823 | ||
1824 | /* Make skb's linear, because decoding accesses the entire buffer */ | |
1825 | if (skb_linearize(skb) < 0) | |
1826 | goto free_nc_packet; | |
1827 | ||
1828 | if (skb_linearize(nc_packet->skb) < 0) | |
1829 | goto free_nc_packet; | |
1830 | ||
1831 | /* Decode the packet */ | |
6e0895c2 | 1832 | unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet); |
2df5278b MH |
1833 | if (!unicast_packet) { |
1834 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED); | |
1835 | goto free_nc_packet; | |
1836 | } | |
1837 | ||
1838 | /* Mark packet as decoded to do correct recoding when forwarding */ | |
1839 | BATADV_SKB_CB(skb)->decoded = true; | |
1840 | batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE); | |
1841 | batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES, | |
1842 | skb->len + ETH_HLEN); | |
1843 | return batadv_recv_unicast_packet(skb, recv_if); | |
1844 | ||
1845 | free_nc_packet: | |
1846 | batadv_nc_packet_free(nc_packet); | |
1847 | return NET_RX_DROP; | |
1848 | } | |
1849 | ||
d353d8d4 | 1850 | /** |
6c519bad | 1851 | * batadv_nc_mesh_free - clean up network coding memory |
d353d8d4 MH |
1852 | * @bat_priv: the bat priv with all the soft interface information |
1853 | */ | |
6c519bad | 1854 | void batadv_nc_mesh_free(struct batadv_priv *bat_priv) |
d353d8d4 | 1855 | { |
3f4841ff ML |
1856 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1); |
1857 | batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1); | |
d353d8d4 | 1858 | cancel_delayed_work_sync(&bat_priv->nc.work); |
612d2b4f | 1859 | |
95332477 MH |
1860 | batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL); |
1861 | batadv_hash_destroy(bat_priv->nc.coding_hash); | |
612d2b4f MH |
1862 | batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL); |
1863 | batadv_hash_destroy(bat_priv->nc.decoding_hash); | |
d353d8d4 | 1864 | } |
d56b1705 MH |
1865 | |
1866 | /** | |
1867 | * batadv_nc_nodes_seq_print_text - print the nc node information | |
1868 | * @seq: seq file to print on | |
1869 | * @offset: not used | |
1870 | */ | |
1871 | int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset) | |
1872 | { | |
1873 | struct net_device *net_dev = (struct net_device *)seq->private; | |
1874 | struct batadv_priv *bat_priv = netdev_priv(net_dev); | |
1875 | struct batadv_hashtable *hash = bat_priv->orig_hash; | |
1876 | struct batadv_hard_iface *primary_if; | |
1877 | struct hlist_head *head; | |
1878 | struct batadv_orig_node *orig_node; | |
1879 | struct batadv_nc_node *nc_node; | |
1880 | int i; | |
1881 | ||
1882 | primary_if = batadv_seq_print_text_primary_if_get(seq); | |
1883 | if (!primary_if) | |
1884 | goto out; | |
1885 | ||
1886 | /* Traverse list of originators */ | |
1887 | for (i = 0; i < hash->size; i++) { | |
1888 | head = &hash->table[i]; | |
1889 | ||
1890 | /* For each orig_node in this bin */ | |
1891 | rcu_read_lock(); | |
1892 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { | |
aa27c312 ML |
1893 | /* no need to print the orig node if it does not have |
1894 | * network coding neighbors | |
1895 | */ | |
1896 | if (list_empty(&orig_node->in_coding_list) && | |
1897 | list_empty(&orig_node->out_coding_list)) | |
1898 | continue; | |
1899 | ||
d56b1705 MH |
1900 | seq_printf(seq, "Node: %pM\n", orig_node->orig); |
1901 | ||
0c814653 | 1902 | seq_puts(seq, " Ingoing: "); |
d56b1705 MH |
1903 | /* For each in_nc_node to this orig_node */ |
1904 | list_for_each_entry_rcu(nc_node, | |
1905 | &orig_node->in_coding_list, | |
1906 | list) | |
1907 | seq_printf(seq, "%pM ", | |
1908 | nc_node->addr); | |
0c814653 | 1909 | seq_puts(seq, "\n"); |
d56b1705 | 1910 | |
0c814653 | 1911 | seq_puts(seq, " Outgoing: "); |
d56b1705 MH |
1912 | /* For out_nc_node to this orig_node */ |
1913 | list_for_each_entry_rcu(nc_node, | |
1914 | &orig_node->out_coding_list, | |
1915 | list) | |
1916 | seq_printf(seq, "%pM ", | |
1917 | nc_node->addr); | |
0c814653 | 1918 | seq_puts(seq, "\n\n"); |
d56b1705 MH |
1919 | } |
1920 | rcu_read_unlock(); | |
1921 | } | |
1922 | ||
1923 | out: | |
1924 | if (primary_if) | |
1925 | batadv_hardif_free_ref(primary_if); | |
1926 | return 0; | |
1927 | } | |
1928 | ||
1929 | /** | |
1930 | * batadv_nc_init_debugfs - create nc folder and related files in debugfs | |
1931 | * @bat_priv: the bat priv with all the soft interface information | |
1932 | */ | |
1933 | int batadv_nc_init_debugfs(struct batadv_priv *bat_priv) | |
1934 | { | |
1935 | struct dentry *nc_dir, *file; | |
1936 | ||
1937 | nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir); | |
1938 | if (!nc_dir) | |
1939 | goto out; | |
1940 | ||
1941 | file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir, | |
1942 | &bat_priv->nc.min_tq); | |
1943 | if (!file) | |
1944 | goto out; | |
1945 | ||
95332477 MH |
1946 | file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir, |
1947 | &bat_priv->nc.max_fwd_delay); | |
1948 | if (!file) | |
1949 | goto out; | |
1950 | ||
612d2b4f MH |
1951 | file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir, |
1952 | &bat_priv->nc.max_buffer_time); | |
1953 | if (!file) | |
1954 | goto out; | |
1955 | ||
d56b1705 MH |
1956 | return 0; |
1957 | ||
1958 | out: | |
1959 | return -ENOMEM; | |
1960 | } |