blob: 3d2ed2fe54217a313fdeae10a667271da10c8999 [file] [log] [blame]
Martin Hundebølld353d8d2013-01-25 11:12:38 +01001/* Copyright (C) 2012-2013 B.A.T.M.A.N. contributors:
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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
Martin Hundebølld56b1702013-01-25 11:12:39 +010020#include <linux/debugfs.h>
21
Martin Hundebølld353d8d2013-01-25 11:12:38 +010022#include "main.h"
Martin Hundebøll95332472013-01-25 11:12:40 +010023#include "hash.h"
Martin Hundebølld353d8d2013-01-25 11:12:38 +010024#include "network-coding.h"
Martin Hundebøll95332472013-01-25 11:12:40 +010025#include "send.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010026#include "originator.h"
27#include "hard-interface.h"
Martin Hundebølld353d8d2013-01-25 11:12:38 +010028
Martin Hundebøll95332472013-01-25 11:12:40 +010029static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
Martin Hundebøll612d2b42013-01-25 11:12:42 +010030static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
Martin Hundebøll95332472013-01-25 11:12:40 +010031
Martin Hundebølld353d8d2013-01-25 11:12:38 +010032static void batadv_nc_worker(struct work_struct *work);
33
34/**
35 * batadv_nc_start_timer - initialise the nc periodic worker
36 * @bat_priv: the bat priv with all the soft interface information
37 */
38static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
39{
40 queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
41 msecs_to_jiffies(10));
42}
43
44/**
45 * batadv_nc_init - initialise coding hash table and start house keeping
46 * @bat_priv: the bat priv with all the soft interface information
47 */
48int batadv_nc_init(struct batadv_priv *bat_priv)
49{
Martin Hundebøll95332472013-01-25 11:12:40 +010050 bat_priv->nc.timestamp_fwd_flush = jiffies;
Martin Hundebøll612d2b42013-01-25 11:12:42 +010051 bat_priv->nc.timestamp_sniffed_purge = jiffies;
Martin Hundebøll95332472013-01-25 11:12:40 +010052
Martin Hundebøll612d2b42013-01-25 11:12:42 +010053 if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
Martin Hundebøll95332472013-01-25 11:12:40 +010054 return 0;
55
56 bat_priv->nc.coding_hash = batadv_hash_new(128);
57 if (!bat_priv->nc.coding_hash)
58 goto err;
59
60 batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
61 &batadv_nc_coding_hash_lock_class_key);
62
Martin Hundebøll612d2b42013-01-25 11:12:42 +010063 bat_priv->nc.decoding_hash = batadv_hash_new(128);
64 if (!bat_priv->nc.decoding_hash)
65 goto err;
66
67 batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
68 &batadv_nc_decoding_hash_lock_class_key);
69
Martin Hundebølld353d8d2013-01-25 11:12:38 +010070 INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
71 batadv_nc_start_timer(bat_priv);
72
73 return 0;
Martin Hundebøll95332472013-01-25 11:12:40 +010074
75err:
76 return -ENOMEM;
Martin Hundebølld353d8d2013-01-25 11:12:38 +010077}
78
79/**
80 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
81 * @bat_priv: the bat priv with all the soft interface information
82 */
83void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
84{
85 atomic_set(&bat_priv->network_coding, 1);
Martin Hundebølld56b1702013-01-25 11:12:39 +010086 bat_priv->nc.min_tq = 200;
Martin Hundebøll95332472013-01-25 11:12:40 +010087 bat_priv->nc.max_fwd_delay = 10;
Martin Hundebøll612d2b42013-01-25 11:12:42 +010088 bat_priv->nc.max_buffer_time = 200;
Martin Hundebølld56b1702013-01-25 11:12:39 +010089}
90
91/**
92 * batadv_nc_init_orig - initialise the nc fields of an orig_node
93 * @orig_node: the orig_node which is going to be initialised
94 */
95void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
96{
97 INIT_LIST_HEAD(&orig_node->in_coding_list);
98 INIT_LIST_HEAD(&orig_node->out_coding_list);
99 spin_lock_init(&orig_node->in_coding_list_lock);
100 spin_lock_init(&orig_node->out_coding_list_lock);
101}
102
103/**
104 * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
105 * its refcount on the orig_node
106 * @rcu: rcu pointer of the nc node
107 */
108static void batadv_nc_node_free_rcu(struct rcu_head *rcu)
109{
110 struct batadv_nc_node *nc_node;
111
112 nc_node = container_of(rcu, struct batadv_nc_node, rcu);
113 batadv_orig_node_free_ref(nc_node->orig_node);
114 kfree(nc_node);
115}
116
117/**
118 * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
119 * frees it
120 * @nc_node: the nc node to free
121 */
122static void batadv_nc_node_free_ref(struct batadv_nc_node *nc_node)
123{
124 if (atomic_dec_and_test(&nc_node->refcount))
125 call_rcu(&nc_node->rcu, batadv_nc_node_free_rcu);
126}
127
128/**
Martin Hundebøll95332472013-01-25 11:12:40 +0100129 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
130 * frees it
131 * @nc_path: the nc node to free
132 */
133static void batadv_nc_path_free_ref(struct batadv_nc_path *nc_path)
134{
135 if (atomic_dec_and_test(&nc_path->refcount))
136 kfree_rcu(nc_path, rcu);
137}
138
139/**
140 * batadv_nc_packet_free - frees nc packet
141 * @nc_packet: the nc packet to free
142 */
143static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet)
144{
145 if (nc_packet->skb)
146 kfree_skb(nc_packet->skb);
147
148 batadv_nc_path_free_ref(nc_packet->nc_path);
149 kfree(nc_packet);
150}
151
152/**
Martin Hundebølld56b1702013-01-25 11:12:39 +0100153 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
154 * @bat_priv: the bat priv with all the soft interface information
155 * @nc_node: the nc node to check
156 *
157 * Returns true if the entry has to be purged now, false otherwise
158 */
159static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
160 struct batadv_nc_node *nc_node)
161{
162 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
163 return true;
164
165 return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
166}
167
168/**
Martin Hundebøll95332472013-01-25 11:12:40 +0100169 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
170 * @bat_priv: the bat priv with all the soft interface information
171 * @nc_path: the nc path to check
172 *
173 * Returns true if the entry has to be purged now, false otherwise
174 */
175static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
176 struct batadv_nc_path *nc_path)
177{
178 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
179 return true;
180
181 /* purge the path when no packets has been added for 10 times the
182 * max_fwd_delay time
183 */
184 return batadv_has_timed_out(nc_path->last_valid,
185 bat_priv->nc.max_fwd_delay * 10);
186}
187
188/**
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100189 * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
190 * @bat_priv: the bat priv with all the soft interface information
191 * @nc_path: the nc path to check
192 *
193 * Returns true if the entry has to be purged now, false otherwise
194 */
195static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
196 struct batadv_nc_path *nc_path)
197{
198 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
199 return true;
200
201 /* purge the path when no packets has been added for 10 times the
202 * max_buffer time
203 */
204 return batadv_has_timed_out(nc_path->last_valid,
205 bat_priv->nc.max_buffer_time*10);
206}
207
208/**
Martin Hundebølld56b1702013-01-25 11:12:39 +0100209 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
210 * entries
211 * @bat_priv: the bat priv with all the soft interface information
212 * @list: list of nc nodes
213 * @lock: nc node list lock
214 * @to_purge: function in charge to decide whether an entry has to be purged or
215 * not. This function takes the nc node as argument and has to return
216 * a boolean value: true if the entry has to be deleted, false
217 * otherwise
218 */
219static void
220batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
221 struct list_head *list,
222 spinlock_t *lock,
223 bool (*to_purge)(struct batadv_priv *,
224 struct batadv_nc_node *))
225{
226 struct batadv_nc_node *nc_node, *nc_node_tmp;
227
228 /* For each nc_node in list */
229 spin_lock_bh(lock);
230 list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
231 /* if an helper function has been passed as parameter,
232 * ask it if the entry has to be purged or not
233 */
234 if (to_purge && !to_purge(bat_priv, nc_node))
235 continue;
236
237 batadv_dbg(BATADV_DBG_NC, bat_priv,
238 "Removing nc_node %pM -> %pM\n",
239 nc_node->addr, nc_node->orig_node->orig);
240 list_del_rcu(&nc_node->list);
241 batadv_nc_node_free_ref(nc_node);
242 }
243 spin_unlock_bh(lock);
244}
245
246/**
247 * batadv_nc_purge_orig - purges all nc node data attached of the given
248 * originator
249 * @bat_priv: the bat priv with all the soft interface information
250 * @orig_node: orig_node with the nc node entries to be purged
251 * @to_purge: function in charge to decide whether an entry has to be purged or
252 * not. This function takes the nc node as argument and has to return
253 * a boolean value: true is the entry has to be deleted, false
254 * otherwise
255 */
256void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
257 struct batadv_orig_node *orig_node,
258 bool (*to_purge)(struct batadv_priv *,
259 struct batadv_nc_node *))
260{
261 /* Check ingoing nc_node's of this orig_node */
262 batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
263 &orig_node->in_coding_list_lock,
264 to_purge);
265
266 /* Check outgoing nc_node's of this orig_node */
267 batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
268 &orig_node->out_coding_list_lock,
269 to_purge);
270}
271
272/**
273 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
274 * have timed out nc nodes
275 * @bat_priv: the bat priv with all the soft interface information
276 */
277static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
278{
279 struct batadv_hashtable *hash = bat_priv->orig_hash;
280 struct hlist_head *head;
281 struct batadv_orig_node *orig_node;
282 uint32_t i;
283
284 if (!hash)
285 return;
286
287 /* For each orig_node */
288 for (i = 0; i < hash->size; i++) {
289 head = &hash->table[i];
290
291 rcu_read_lock();
292 hlist_for_each_entry_rcu(orig_node, head, hash_entry)
293 batadv_nc_purge_orig(bat_priv, orig_node,
294 batadv_nc_to_purge_nc_node);
295 rcu_read_unlock();
296 }
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100297}
298
299/**
Martin Hundebøll95332472013-01-25 11:12:40 +0100300 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
301 * unused ones
302 * @bat_priv: the bat priv with all the soft interface information
303 * @hash: hash table containing the nc paths to check
304 * @to_purge: function in charge to decide whether an entry has to be purged or
305 * not. This function takes the nc node as argument and has to return
306 * a boolean value: true is the entry has to be deleted, false
307 * otherwise
308 */
309static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
310 struct batadv_hashtable *hash,
311 bool (*to_purge)(struct batadv_priv *,
312 struct batadv_nc_path *))
313{
314 struct hlist_head *head;
315 struct hlist_node *node_tmp;
316 struct batadv_nc_path *nc_path;
317 spinlock_t *lock; /* Protects lists in hash */
318 uint32_t i;
319
320 for (i = 0; i < hash->size; i++) {
321 head = &hash->table[i];
322 lock = &hash->list_locks[i];
323
324 /* For each nc_path in this bin */
325 spin_lock_bh(lock);
326 hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
327 /* if an helper function has been passed as parameter,
328 * ask it if the entry has to be purged or not
329 */
330 if (to_purge && !to_purge(bat_priv, nc_path))
331 continue;
332
333 /* purging an non-empty nc_path should never happen, but
334 * is observed under high CPU load. Delay the purging
335 * until next iteration to allow the packet_list to be
336 * emptied first.
337 */
338 if (!unlikely(list_empty(&nc_path->packet_list))) {
339 net_ratelimited_function(printk,
340 KERN_WARNING
341 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
342 nc_path->prev_hop,
343 nc_path->next_hop);
344 continue;
345 }
346
347 /* nc_path is unused, so remove it */
348 batadv_dbg(BATADV_DBG_NC, bat_priv,
349 "Remove nc_path %pM -> %pM\n",
350 nc_path->prev_hop, nc_path->next_hop);
351 hlist_del_rcu(&nc_path->hash_entry);
352 batadv_nc_path_free_ref(nc_path);
353 }
354 spin_unlock_bh(lock);
355 }
356}
357
358/**
359 * batadv_nc_hash_key_gen - computes the nc_path hash key
360 * @key: buffer to hold the final hash key
361 * @src: source ethernet mac address going into the hash key
362 * @dst: destination ethernet mac address going into the hash key
363 */
364static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
365 const char *dst)
366{
367 memcpy(key->prev_hop, src, sizeof(key->prev_hop));
368 memcpy(key->next_hop, dst, sizeof(key->next_hop));
369}
370
371/**
372 * batadv_nc_hash_choose - compute the hash value for an nc path
373 * @data: data to hash
374 * @size: size of the hash table
375 *
376 * Returns the selected index in the hash table for the given data.
377 */
378static uint32_t batadv_nc_hash_choose(const void *data, uint32_t size)
379{
380 const struct batadv_nc_path *nc_path = data;
381 uint32_t hash = 0;
382
383 hash = batadv_hash_bytes(hash, &nc_path->prev_hop,
384 sizeof(nc_path->prev_hop));
385 hash = batadv_hash_bytes(hash, &nc_path->next_hop,
386 sizeof(nc_path->next_hop));
387
388 hash += (hash << 3);
389 hash ^= (hash >> 11);
390 hash += (hash << 15);
391
392 return hash % size;
393}
394
395/**
396 * batadv_nc_hash_compare - comparing function used in the network coding hash
397 * tables
398 * @node: node in the local table
399 * @data2: second object to compare the node to
400 *
401 * Returns 1 if the two entry are the same, 0 otherwise
402 */
403static int batadv_nc_hash_compare(const struct hlist_node *node,
404 const void *data2)
405{
406 const struct batadv_nc_path *nc_path1, *nc_path2;
407
408 nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
409 nc_path2 = data2;
410
411 /* Return 1 if the two keys are identical */
412 if (memcmp(nc_path1->prev_hop, nc_path2->prev_hop,
413 sizeof(nc_path1->prev_hop)) != 0)
414 return 0;
415
416 if (memcmp(nc_path1->next_hop, nc_path2->next_hop,
417 sizeof(nc_path1->next_hop)) != 0)
418 return 0;
419
420 return 1;
421}
422
423/**
424 * batadv_nc_hash_find - search for an existing nc path and return it
425 * @hash: hash table containing the nc path
426 * @data: search key
427 *
428 * Returns the nc_path if found, NULL otherwise.
429 */
430static struct batadv_nc_path *
431batadv_nc_hash_find(struct batadv_hashtable *hash,
432 void *data)
433{
434 struct hlist_head *head;
435 struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
436 int index;
437
438 if (!hash)
439 return NULL;
440
441 index = batadv_nc_hash_choose(data, hash->size);
442 head = &hash->table[index];
443
444 rcu_read_lock();
445 hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
446 if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
447 continue;
448
449 if (!atomic_inc_not_zero(&nc_path->refcount))
450 continue;
451
452 nc_path_tmp = nc_path;
453 break;
454 }
455 rcu_read_unlock();
456
457 return nc_path_tmp;
458}
459
460/**
461 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
462 * @nc_packet: the nc packet to send
463 */
464static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
465{
466 batadv_send_skb_packet(nc_packet->skb,
467 nc_packet->neigh_node->if_incoming,
468 nc_packet->nc_path->next_hop);
469 nc_packet->skb = NULL;
470 batadv_nc_packet_free(nc_packet);
471}
472
473/**
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100474 * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
475 * @bat_priv: the bat priv with all the soft interface information
476 * @nc_path: the nc path the packet belongs to
477 * @nc_packet: the nc packet to be checked
478 *
479 * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
480 * timeout. If so, the packet is no longer kept and the entry deleted from the
481 * queue. Has to be called with the appropriate locks.
482 *
483 * Returns false as soon as the entry in the fifo queue has not been timed out
484 * yet and true otherwise.
485 */
486static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
487 struct batadv_nc_path *nc_path,
488 struct batadv_nc_packet *nc_packet)
489{
490 unsigned long timeout = bat_priv->nc.max_buffer_time;
491 bool res = false;
492
493 /* Packets are added to tail, so the remaining packets did not time
494 * out and we can stop processing the current queue
495 */
496 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
497 !batadv_has_timed_out(nc_packet->timestamp, timeout))
498 goto out;
499
500 /* purge nc packet */
501 list_del(&nc_packet->list);
502 batadv_nc_packet_free(nc_packet);
503
504 res = true;
505
506out:
507 return res;
508}
509
510/**
Martin Hundebøll95332472013-01-25 11:12:40 +0100511 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
512 * @bat_priv: the bat priv with all the soft interface information
513 * @nc_path: the nc path the packet belongs to
514 * @nc_packet: the nc packet to be checked
515 *
516 * Checks whether the given nc packet has hit its forward timeout. If so, the
517 * packet is no longer delayed, immediately sent and the entry deleted from the
518 * queue. Has to be called with the appropriate locks.
519 *
520 * Returns false as soon as the entry in the fifo queue has not been timed out
521 * yet and true otherwise.
522 */
523static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
524 struct batadv_nc_path *nc_path,
525 struct batadv_nc_packet *nc_packet)
526{
527 unsigned long timeout = bat_priv->nc.max_fwd_delay;
528
529 /* Packets are added to tail, so the remaining packets did not time
530 * out and we can stop processing the current queue
531 */
532 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
533 !batadv_has_timed_out(nc_packet->timestamp, timeout))
534 return false;
535
536 /* Send packet */
537 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
538 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
539 nc_packet->skb->len + ETH_HLEN);
540 list_del(&nc_packet->list);
541 batadv_nc_send_packet(nc_packet);
542
543 return true;
544}
545
546/**
547 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
548 * nc packets
549 * @bat_priv: the bat priv with all the soft interface information
550 * @hash: to be processed hash table
551 * @process_fn: Function called to process given nc packet. Should return true
552 * to encourage this function to proceed with the next packet.
553 * Otherwise the rest of the current queue is skipped.
554 */
555static void
556batadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
557 struct batadv_hashtable *hash,
558 bool (*process_fn)(struct batadv_priv *,
559 struct batadv_nc_path *,
560 struct batadv_nc_packet *))
561{
562 struct hlist_head *head;
563 struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
564 struct batadv_nc_path *nc_path;
565 bool ret;
566 int i;
567
568 if (!hash)
569 return;
570
571 /* Loop hash table bins */
572 for (i = 0; i < hash->size; i++) {
573 head = &hash->table[i];
574
575 /* Loop coding paths */
576 rcu_read_lock();
577 hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
578 /* Loop packets */
579 spin_lock_bh(&nc_path->packet_list_lock);
580 list_for_each_entry_safe(nc_packet, nc_packet_tmp,
581 &nc_path->packet_list, list) {
582 ret = process_fn(bat_priv, nc_path, nc_packet);
583 if (!ret)
584 break;
585 }
586 spin_unlock_bh(&nc_path->packet_list_lock);
587 }
588 rcu_read_unlock();
589 }
590}
591
592/**
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100593 * batadv_nc_worker - periodic task for house keeping related to network coding
594 * @work: kernel work struct
595 */
596static void batadv_nc_worker(struct work_struct *work)
597{
598 struct delayed_work *delayed_work;
599 struct batadv_priv_nc *priv_nc;
600 struct batadv_priv *bat_priv;
Martin Hundebøll95332472013-01-25 11:12:40 +0100601 unsigned long timeout;
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100602
603 delayed_work = container_of(work, struct delayed_work, work);
604 priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
605 bat_priv = container_of(priv_nc, struct batadv_priv, nc);
606
Martin Hundebølld56b1702013-01-25 11:12:39 +0100607 batadv_nc_purge_orig_hash(bat_priv);
Martin Hundebøll95332472013-01-25 11:12:40 +0100608 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
609 batadv_nc_to_purge_nc_path_coding);
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100610 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
611 batadv_nc_to_purge_nc_path_decoding);
Martin Hundebøll95332472013-01-25 11:12:40 +0100612
613 timeout = bat_priv->nc.max_fwd_delay;
614
615 if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
616 batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
617 batadv_nc_fwd_flush);
618 bat_priv->nc.timestamp_fwd_flush = jiffies;
619 }
Martin Hundebølld56b1702013-01-25 11:12:39 +0100620
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100621 if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
622 bat_priv->nc.max_buffer_time)) {
623 batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
624 batadv_nc_sniffed_purge);
625 bat_priv->nc.timestamp_sniffed_purge = jiffies;
626 }
627
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100628 /* Schedule a new check */
629 batadv_nc_start_timer(bat_priv);
630}
631
632/**
Martin Hundebølld56b1702013-01-25 11:12:39 +0100633 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
634 * coding or not
635 * @bat_priv: the bat priv with all the soft interface information
636 * @orig_node: neighboring orig node which may be used as nc candidate
637 * @ogm_packet: incoming ogm packet also used for the checks
638 *
639 * Returns true if:
640 * 1) The OGM must have the most recent sequence number.
641 * 2) The TTL must be decremented by one and only one.
642 * 3) The OGM must be received from the first hop from orig_node.
643 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
644 */
645static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
646 struct batadv_orig_node *orig_node,
647 struct batadv_ogm_packet *ogm_packet)
648{
649 if (orig_node->last_real_seqno != ogm_packet->seqno)
650 return false;
651 if (orig_node->last_ttl != ogm_packet->header.ttl + 1)
652 return false;
653 if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
654 return false;
655 if (ogm_packet->tq < bat_priv->nc.min_tq)
656 return false;
657
658 return true;
659}
660
661/**
662 * batadv_nc_find_nc_node - search for an existing nc node and return it
663 * @orig_node: orig node originating the ogm packet
664 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
665 * (can be equal to orig_node)
666 * @in_coding: traverse incoming or outgoing network coding list
667 *
668 * Returns the nc_node if found, NULL otherwise.
669 */
670static struct batadv_nc_node
671*batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
672 struct batadv_orig_node *orig_neigh_node,
673 bool in_coding)
674{
675 struct batadv_nc_node *nc_node, *nc_node_out = NULL;
676 struct list_head *list;
677
678 if (in_coding)
679 list = &orig_neigh_node->in_coding_list;
680 else
681 list = &orig_neigh_node->out_coding_list;
682
683 /* Traverse list of nc_nodes to orig_node */
684 rcu_read_lock();
685 list_for_each_entry_rcu(nc_node, list, list) {
686 if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
687 continue;
688
689 if (!atomic_inc_not_zero(&nc_node->refcount))
690 continue;
691
692 /* Found a match */
693 nc_node_out = nc_node;
694 break;
695 }
696 rcu_read_unlock();
697
698 return nc_node_out;
699}
700
701/**
702 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
703 * not found
704 * @bat_priv: the bat priv with all the soft interface information
705 * @orig_node: orig node originating the ogm packet
706 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
707 * (can be equal to orig_node)
708 * @in_coding: traverse incoming or outgoing network coding list
709 *
710 * Returns the nc_node if found or created, NULL in case of an error.
711 */
712static struct batadv_nc_node
713*batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
714 struct batadv_orig_node *orig_node,
715 struct batadv_orig_node *orig_neigh_node,
716 bool in_coding)
717{
718 struct batadv_nc_node *nc_node;
719 spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
720 struct list_head *list;
721
722 /* Check if nc_node is already added */
723 nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
724
725 /* Node found */
726 if (nc_node)
727 return nc_node;
728
729 nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
730 if (!nc_node)
731 return NULL;
732
733 if (!atomic_inc_not_zero(&orig_neigh_node->refcount))
734 goto free;
735
736 /* Initialize nc_node */
737 INIT_LIST_HEAD(&nc_node->list);
738 memcpy(nc_node->addr, orig_node->orig, ETH_ALEN);
739 nc_node->orig_node = orig_neigh_node;
740 atomic_set(&nc_node->refcount, 2);
741
742 /* Select ingoing or outgoing coding node */
743 if (in_coding) {
744 lock = &orig_neigh_node->in_coding_list_lock;
745 list = &orig_neigh_node->in_coding_list;
746 } else {
747 lock = &orig_neigh_node->out_coding_list_lock;
748 list = &orig_neigh_node->out_coding_list;
749 }
750
751 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
752 nc_node->addr, nc_node->orig_node->orig);
753
754 /* Add nc_node to orig_node */
755 spin_lock_bh(lock);
756 list_add_tail_rcu(&nc_node->list, list);
757 spin_unlock_bh(lock);
758
759 return nc_node;
760
761free:
762 kfree(nc_node);
763 return NULL;
764}
765
766/**
767 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
768 * (best called on incoming OGMs)
769 * @bat_priv: the bat priv with all the soft interface information
770 * @orig_node: orig node originating the ogm packet
771 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
772 * (can be equal to orig_node)
773 * @ogm_packet: incoming ogm packet
774 * @is_single_hop_neigh: orig_node is a single hop neighbor
775 */
776void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
777 struct batadv_orig_node *orig_node,
778 struct batadv_orig_node *orig_neigh_node,
779 struct batadv_ogm_packet *ogm_packet,
780 int is_single_hop_neigh)
781{
782 struct batadv_nc_node *in_nc_node = NULL, *out_nc_node = NULL;
783
784 /* Check if network coding is enabled */
785 if (!atomic_read(&bat_priv->network_coding))
786 goto out;
787
788 /* accept ogms from 'good' neighbors and single hop neighbors */
789 if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
790 !is_single_hop_neigh)
791 goto out;
792
793 /* Add orig_node as in_nc_node on hop */
794 in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
795 orig_neigh_node, true);
796 if (!in_nc_node)
797 goto out;
798
799 in_nc_node->last_seen = jiffies;
800
801 /* Add hop as out_nc_node on orig_node */
802 out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
803 orig_node, false);
804 if (!out_nc_node)
805 goto out;
806
807 out_nc_node->last_seen = jiffies;
808
809out:
810 if (in_nc_node)
811 batadv_nc_node_free_ref(in_nc_node);
812 if (out_nc_node)
813 batadv_nc_node_free_ref(out_nc_node);
814}
815
816/**
Martin Hundebøll95332472013-01-25 11:12:40 +0100817 * batadv_nc_get_path - get existing nc_path or allocate a new one
818 * @bat_priv: the bat priv with all the soft interface information
819 * @hash: hash table containing the nc path
820 * @src: ethernet source address - first half of the nc path search key
821 * @dst: ethernet destination address - second half of the nc path search key
822 *
823 * Returns pointer to nc_path if the path was found or created, returns NULL
824 * on error.
825 */
826static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
827 struct batadv_hashtable *hash,
828 uint8_t *src,
829 uint8_t *dst)
830{
831 int hash_added;
832 struct batadv_nc_path *nc_path, nc_path_key;
833
834 batadv_nc_hash_key_gen(&nc_path_key, src, dst);
835
836 /* Search for existing nc_path */
837 nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
838
839 if (nc_path) {
840 /* Set timestamp to delay removal of nc_path */
841 nc_path->last_valid = jiffies;
842 return nc_path;
843 }
844
845 /* No existing nc_path was found; create a new */
846 nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
847
848 if (!nc_path)
849 return NULL;
850
851 /* Initialize nc_path */
852 INIT_LIST_HEAD(&nc_path->packet_list);
853 spin_lock_init(&nc_path->packet_list_lock);
854 atomic_set(&nc_path->refcount, 2);
855 nc_path->last_valid = jiffies;
856 memcpy(nc_path->next_hop, dst, ETH_ALEN);
857 memcpy(nc_path->prev_hop, src, ETH_ALEN);
858
859 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
860 nc_path->prev_hop,
861 nc_path->next_hop);
862
863 /* Add nc_path to hash table */
864 hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
865 batadv_nc_hash_choose, &nc_path_key,
866 &nc_path->hash_entry);
867
868 if (hash_added < 0) {
869 kfree(nc_path);
870 return NULL;
871 }
872
873 return nc_path;
874}
875
876/**
Martin Hundebøll3c12de92013-01-25 11:12:41 +0100877 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
878 * selection of a receiver with slightly lower TQ than the other
879 * @tq: to be weighted tq value
880 */
881static uint8_t batadv_nc_random_weight_tq(uint8_t tq)
882{
883 uint8_t rand_val, rand_tq;
884
885 get_random_bytes(&rand_val, sizeof(rand_val));
886
887 /* randomize the estimated packet loss (max TQ - estimated TQ) */
888 rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq);
889
890 /* normalize the randomized packet loss */
891 rand_tq /= BATADV_TQ_MAX_VALUE;
892
893 /* convert to (randomized) estimated tq again */
894 return BATADV_TQ_MAX_VALUE - rand_tq;
895}
896
897/**
898 * batadv_nc_memxor - XOR destination with source
899 * @dst: byte array to XOR into
900 * @src: byte array to XOR from
901 * @len: length of destination array
902 */
903static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
904{
905 unsigned int i;
906
907 for (i = 0; i < len; ++i)
908 dst[i] ^= src[i];
909}
910
911/**
912 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
913 * into a coded_packet and send it
914 * @bat_priv: the bat priv with all the soft interface information
915 * @skb: data skb to forward
916 * @ethhdr: pointer to the ethernet header inside the skb
917 * @nc_packet: structure containing the packet to the skb can be coded with
918 * @neigh_node: next hop to forward packet to
919 *
920 * Returns true if both packets are consumed, false otherwise.
921 */
922static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
923 struct sk_buff *skb,
924 struct ethhdr *ethhdr,
925 struct batadv_nc_packet *nc_packet,
926 struct batadv_neigh_node *neigh_node)
927{
928 uint8_t tq_weighted_neigh, tq_weighted_coding;
929 struct sk_buff *skb_dest, *skb_src;
930 struct batadv_unicast_packet *packet1;
931 struct batadv_unicast_packet *packet2;
932 struct batadv_coded_packet *coded_packet;
933 struct batadv_neigh_node *neigh_tmp, *router_neigh;
934 struct batadv_neigh_node *router_coding = NULL;
935 uint8_t *first_source, *first_dest, *second_source, *second_dest;
936 __be32 packet_id1, packet_id2;
937 size_t count;
938 bool res = false;
939 int coding_len;
940 int unicast_size = sizeof(*packet1);
941 int coded_size = sizeof(*coded_packet);
942 int header_add = coded_size - unicast_size;
943
944 router_neigh = batadv_orig_node_get_router(neigh_node->orig_node);
945 if (!router_neigh)
946 goto out;
947
948 neigh_tmp = nc_packet->neigh_node;
949 router_coding = batadv_orig_node_get_router(neigh_tmp->orig_node);
950 if (!router_coding)
951 goto out;
952
953 tq_weighted_neigh = batadv_nc_random_weight_tq(router_neigh->tq_avg);
954 tq_weighted_coding = batadv_nc_random_weight_tq(router_coding->tq_avg);
955
956 /* Select one destination for the MAC-header dst-field based on
957 * weighted TQ-values.
958 */
959 if (tq_weighted_neigh >= tq_weighted_coding) {
960 /* Destination from nc_packet is selected for MAC-header */
961 first_dest = nc_packet->nc_path->next_hop;
962 first_source = nc_packet->nc_path->prev_hop;
963 second_dest = neigh_node->addr;
964 second_source = ethhdr->h_source;
965 packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
966 packet2 = (struct batadv_unicast_packet *)skb->data;
967 packet_id1 = nc_packet->packet_id;
968 packet_id2 = batadv_skb_crc32(skb,
969 skb->data + sizeof(*packet2));
970 } else {
971 /* Destination for skb is selected for MAC-header */
972 first_dest = neigh_node->addr;
973 first_source = ethhdr->h_source;
974 second_dest = nc_packet->nc_path->next_hop;
975 second_source = nc_packet->nc_path->prev_hop;
976 packet1 = (struct batadv_unicast_packet *)skb->data;
977 packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
978 packet_id1 = batadv_skb_crc32(skb,
979 skb->data + sizeof(*packet1));
980 packet_id2 = nc_packet->packet_id;
981 }
982
983 /* Instead of zero padding the smallest data buffer, we
984 * code into the largest.
985 */
986 if (skb->len <= nc_packet->skb->len) {
987 skb_dest = nc_packet->skb;
988 skb_src = skb;
989 } else {
990 skb_dest = skb;
991 skb_src = nc_packet->skb;
992 }
993
994 /* coding_len is used when decoding the packet shorter packet */
995 coding_len = skb_src->len - unicast_size;
996
997 if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
998 goto out;
999
1000 skb_push(skb_dest, header_add);
1001
1002 coded_packet = (struct batadv_coded_packet *)skb_dest->data;
1003 skb_reset_mac_header(skb_dest);
1004
1005 coded_packet->header.packet_type = BATADV_CODED;
1006 coded_packet->header.version = BATADV_COMPAT_VERSION;
1007 coded_packet->header.ttl = packet1->header.ttl;
1008
1009 /* Info about first unicast packet */
1010 memcpy(coded_packet->first_source, first_source, ETH_ALEN);
1011 memcpy(coded_packet->first_orig_dest, packet1->dest, ETH_ALEN);
1012 coded_packet->first_crc = packet_id1;
1013 coded_packet->first_ttvn = packet1->ttvn;
1014
1015 /* Info about second unicast packet */
1016 memcpy(coded_packet->second_dest, second_dest, ETH_ALEN);
1017 memcpy(coded_packet->second_source, second_source, ETH_ALEN);
1018 memcpy(coded_packet->second_orig_dest, packet2->dest, ETH_ALEN);
1019 coded_packet->second_crc = packet_id2;
1020 coded_packet->second_ttl = packet2->header.ttl;
1021 coded_packet->second_ttvn = packet2->ttvn;
1022 coded_packet->coded_len = htons(coding_len);
1023
1024 /* This is where the magic happens: Code skb_src into skb_dest */
1025 batadv_nc_memxor(skb_dest->data + coded_size,
1026 skb_src->data + unicast_size, coding_len);
1027
1028 /* Update counters accordingly */
1029 if (BATADV_SKB_CB(skb_src)->decoded &&
1030 BATADV_SKB_CB(skb_dest)->decoded) {
1031 /* Both packets are recoded */
1032 count = skb_src->len + ETH_HLEN;
1033 count += skb_dest->len + ETH_HLEN;
1034 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
1035 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
1036 } else if (!BATADV_SKB_CB(skb_src)->decoded &&
1037 !BATADV_SKB_CB(skb_dest)->decoded) {
1038 /* Both packets are newly coded */
1039 count = skb_src->len + ETH_HLEN;
1040 count += skb_dest->len + ETH_HLEN;
1041 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
1042 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
1043 } else if (BATADV_SKB_CB(skb_src)->decoded &&
1044 !BATADV_SKB_CB(skb_dest)->decoded) {
1045 /* skb_src recoded and skb_dest is newly coded */
1046 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1047 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1048 skb_src->len + ETH_HLEN);
1049 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1050 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1051 skb_dest->len + ETH_HLEN);
1052 } else if (!BATADV_SKB_CB(skb_src)->decoded &&
1053 BATADV_SKB_CB(skb_dest)->decoded) {
1054 /* skb_src is newly coded and skb_dest is recoded */
1055 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1056 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1057 skb_src->len + ETH_HLEN);
1058 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1059 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1060 skb_dest->len + ETH_HLEN);
1061 }
1062
1063 /* skb_src is now coded into skb_dest, so free it */
1064 kfree_skb(skb_src);
1065
1066 /* avoid duplicate free of skb from nc_packet */
1067 nc_packet->skb = NULL;
1068 batadv_nc_packet_free(nc_packet);
1069
1070 /* Send the coded packet and return true */
1071 batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest);
1072 res = true;
1073out:
1074 if (router_neigh)
1075 batadv_neigh_node_free_ref(router_neigh);
1076 if (router_coding)
1077 batadv_neigh_node_free_ref(router_coding);
1078 return res;
1079}
1080
1081/**
1082 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1083 * @skb: data skb to forward
1084 * @dst: destination mac address of the other skb to code with
1085 * @src: source mac address of skb
1086 *
1087 * Whenever we network code a packet we have to check whether we received it in
1088 * a network coded form. If so, we may not be able to use it for coding because
1089 * some neighbors may also have received (overheard) the packet in the network
1090 * coded form without being able to decode it. It is hard to know which of the
1091 * neighboring nodes was able to decode the packet, therefore we can only
1092 * re-code the packet if the source of the previous encoded packet is involved.
1093 * Since the source encoded the packet we can be certain it has all necessary
1094 * decode information.
1095 *
1096 * Returns true if coding of a decoded packet is allowed.
1097 */
1098static bool batadv_nc_skb_coding_possible(struct sk_buff *skb,
1099 uint8_t *dst, uint8_t *src)
1100{
1101 if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
1102 return false;
1103 else
1104 return true;
1105}
1106
1107/**
1108 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1109 * out_nc_node to retrieve a buffered packet that can be used for coding.
1110 * @bat_priv: the bat priv with all the soft interface information
1111 * @in_nc_node: pointer to skb next hop's neighbor nc node
1112 * @out_nc_node: pointer to skb source's neighbor nc node
1113 * @skb: data skb to forward
1114 * @eth_dst: next hop mac address of skb
1115 *
1116 * Returns true if coding of a decoded skb is allowed.
1117 */
1118static struct batadv_nc_packet *
1119batadv_nc_path_search(struct batadv_priv *bat_priv,
1120 struct batadv_nc_node *in_nc_node,
1121 struct batadv_nc_node *out_nc_node,
1122 struct sk_buff *skb,
1123 uint8_t *eth_dst)
1124{
1125 struct batadv_nc_path *nc_path, nc_path_key;
1126 struct batadv_nc_packet *nc_packet_out = NULL;
1127 struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
1128 struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
1129 int idx;
1130
1131 if (!hash)
1132 return NULL;
1133
1134 /* Create almost path key */
1135 batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
1136 out_nc_node->addr);
1137 idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
1138
1139 /* Check for coding opportunities in this nc_path */
1140 rcu_read_lock();
1141 hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
1142 if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
1143 continue;
1144
1145 if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
1146 continue;
1147
1148 spin_lock_bh(&nc_path->packet_list_lock);
1149 if (list_empty(&nc_path->packet_list)) {
1150 spin_unlock_bh(&nc_path->packet_list_lock);
1151 continue;
1152 }
1153
1154 list_for_each_entry_safe(nc_packet, nc_packet_tmp,
1155 &nc_path->packet_list, list) {
1156 if (!batadv_nc_skb_coding_possible(nc_packet->skb,
1157 eth_dst,
1158 in_nc_node->addr))
1159 continue;
1160
1161 /* Coding opportunity is found! */
1162 list_del(&nc_packet->list);
1163 nc_packet_out = nc_packet;
1164 break;
1165 }
1166
1167 spin_unlock_bh(&nc_path->packet_list_lock);
1168 break;
1169 }
1170 rcu_read_unlock();
1171
1172 return nc_packet_out;
1173}
1174
1175/**
1176 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1177 * skb's sender (may be equal to the originator).
1178 * @bat_priv: the bat priv with all the soft interface information
1179 * @skb: data skb to forward
1180 * @eth_dst: next hop mac address of skb
1181 * @eth_src: source mac address of skb
1182 * @in_nc_node: pointer to skb next hop's neighbor nc node
1183 *
1184 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1185 */
1186static struct batadv_nc_packet *
1187batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
1188 struct sk_buff *skb,
1189 uint8_t *eth_dst,
1190 uint8_t *eth_src,
1191 struct batadv_nc_node *in_nc_node)
1192{
1193 struct batadv_orig_node *orig_node;
1194 struct batadv_nc_node *out_nc_node;
1195 struct batadv_nc_packet *nc_packet = NULL;
1196
1197 orig_node = batadv_orig_hash_find(bat_priv, eth_src);
1198 if (!orig_node)
1199 return NULL;
1200
1201 rcu_read_lock();
1202 list_for_each_entry_rcu(out_nc_node,
1203 &orig_node->out_coding_list, list) {
1204 /* Check if the skb is decoded and if recoding is possible */
1205 if (!batadv_nc_skb_coding_possible(skb,
1206 out_nc_node->addr, eth_src))
1207 continue;
1208
1209 /* Search for an opportunity in this nc_path */
1210 nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
1211 out_nc_node, skb, eth_dst);
1212 if (nc_packet)
1213 break;
1214 }
1215 rcu_read_unlock();
1216
1217 batadv_orig_node_free_ref(orig_node);
1218 return nc_packet;
1219}
1220
1221/**
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001222 * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
1223 * unicast skb before it is stored for use in later decoding
1224 * @bat_priv: the bat priv with all the soft interface information
1225 * @skb: data skb to store
1226 * @eth_dst_new: new destination mac address of skb
1227 */
1228static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
1229 struct sk_buff *skb,
1230 uint8_t *eth_dst_new)
1231{
1232 struct ethhdr *ethhdr;
1233
1234 /* Copy skb header to change the mac header */
1235 skb = pskb_copy(skb, GFP_ATOMIC);
1236 if (!skb)
1237 return;
1238
1239 /* Set the mac header as if we actually sent the packet uncoded */
1240 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1241 memcpy(ethhdr->h_source, ethhdr->h_dest, ETH_ALEN);
1242 memcpy(ethhdr->h_dest, eth_dst_new, ETH_ALEN);
1243
1244 /* Set data pointer to MAC header to mimic packets from our tx path */
1245 skb_push(skb, ETH_HLEN);
1246
1247 /* Add the packet to the decoding packet pool */
1248 batadv_nc_skb_store_for_decoding(bat_priv, skb);
1249
1250 /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
1251 * our ref
1252 */
1253 kfree_skb(skb);
1254}
1255
1256/**
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001257 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1258 * @skb: data skb to forward
1259 * @neigh_node: next hop to forward packet to
1260 * @ethhdr: pointer to the ethernet header inside the skb
1261 *
1262 * Loops through list of neighboring nodes the next hop has a good connection to
1263 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1264 * next hop that potentially sent a packet which our next hop also received
1265 * (overheard) and has stored for later decoding.
1266 *
1267 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1268 */
1269static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
1270 struct batadv_neigh_node *neigh_node,
1271 struct ethhdr *ethhdr)
1272{
1273 struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1274 struct batadv_priv *bat_priv = netdev_priv(netdev);
1275 struct batadv_orig_node *orig_node = neigh_node->orig_node;
1276 struct batadv_nc_node *nc_node;
1277 struct batadv_nc_packet *nc_packet = NULL;
1278
1279 rcu_read_lock();
1280 list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
1281 /* Search for coding opportunity with this in_nc_node */
1282 nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
1283 neigh_node->addr,
1284 ethhdr->h_source, nc_node);
1285
1286 /* Opportunity was found, so stop searching */
1287 if (nc_packet)
1288 break;
1289 }
1290 rcu_read_unlock();
1291
1292 if (!nc_packet)
1293 return false;
1294
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001295 /* Save packets for later decoding */
1296 batadv_nc_skb_store_before_coding(bat_priv, skb,
1297 neigh_node->addr);
1298 batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
1299 nc_packet->neigh_node->addr);
1300
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001301 /* Code and send packets */
1302 if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
1303 neigh_node))
1304 return true;
1305
1306 /* out of mem ? Coding failed - we have to free the buffered packet
1307 * to avoid memleaks. The skb passed as argument will be dealt with
1308 * by the calling function.
1309 */
1310 batadv_nc_send_packet(nc_packet);
1311 return false;
1312}
1313
1314/**
Martin Hundebøll95332472013-01-25 11:12:40 +01001315 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1316 * @skb: skb to add to path
1317 * @nc_path: path to add skb to
1318 * @neigh_node: next hop to forward packet to
1319 * @packet_id: checksum to identify packet
1320 *
1321 * Returns true if the packet was buffered or false in case of an error.
1322 */
1323static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
1324 struct batadv_nc_path *nc_path,
1325 struct batadv_neigh_node *neigh_node,
1326 __be32 packet_id)
1327{
1328 struct batadv_nc_packet *nc_packet;
1329
1330 nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
1331 if (!nc_packet)
1332 return false;
1333
1334 /* Initialize nc_packet */
1335 nc_packet->timestamp = jiffies;
1336 nc_packet->packet_id = packet_id;
1337 nc_packet->skb = skb;
1338 nc_packet->neigh_node = neigh_node;
1339 nc_packet->nc_path = nc_path;
1340
1341 /* Add coding packet to list */
1342 spin_lock_bh(&nc_path->packet_list_lock);
1343 list_add_tail(&nc_packet->list, &nc_path->packet_list);
1344 spin_unlock_bh(&nc_path->packet_list_lock);
1345
1346 return true;
1347}
1348
1349/**
1350 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1351 * buffer
1352 * @skb: data skb to forward
1353 * @neigh_node: next hop to forward packet to
1354 * @ethhdr: pointer to the ethernet header inside the skb
1355 *
1356 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1357 */
1358bool batadv_nc_skb_forward(struct sk_buff *skb,
1359 struct batadv_neigh_node *neigh_node,
1360 struct ethhdr *ethhdr)
1361{
1362 const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1363 struct batadv_priv *bat_priv = netdev_priv(netdev);
1364 struct batadv_unicast_packet *packet;
1365 struct batadv_nc_path *nc_path;
1366 __be32 packet_id;
1367 u8 *payload;
1368
1369 /* Check if network coding is enabled */
1370 if (!atomic_read(&bat_priv->network_coding))
1371 goto out;
1372
1373 /* We only handle unicast packets */
1374 payload = skb_network_header(skb);
1375 packet = (struct batadv_unicast_packet *)payload;
1376 if (packet->header.packet_type != BATADV_UNICAST)
1377 goto out;
1378
Martin Hundebøll3c12de92013-01-25 11:12:41 +01001379 /* Try to find a coding opportunity and send the skb if one is found */
1380 if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
1381 return true;
1382
Martin Hundebøll95332472013-01-25 11:12:40 +01001383 /* Find or create a nc_path for this src-dst pair */
1384 nc_path = batadv_nc_get_path(bat_priv,
1385 bat_priv->nc.coding_hash,
1386 ethhdr->h_source,
1387 neigh_node->addr);
1388
1389 if (!nc_path)
1390 goto out;
1391
1392 /* Add skb to nc_path */
1393 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1394 if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
1395 goto free_nc_path;
1396
1397 /* Packet is consumed */
1398 return true;
1399
1400free_nc_path:
1401 batadv_nc_path_free_ref(nc_path);
1402out:
1403 /* Packet is not consumed */
1404 return false;
1405}
1406
1407/**
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001408 * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
1409 * when decoding coded packets
1410 * @bat_priv: the bat priv with all the soft interface information
1411 * @skb: data skb to store
1412 */
1413void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
1414 struct sk_buff *skb)
1415{
1416 struct batadv_unicast_packet *packet;
1417 struct batadv_nc_path *nc_path;
1418 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1419 __be32 packet_id;
1420 u8 *payload;
1421
1422 /* Check if network coding is enabled */
1423 if (!atomic_read(&bat_priv->network_coding))
1424 goto out;
1425
1426 /* Check for supported packet type */
1427 payload = skb_network_header(skb);
1428 packet = (struct batadv_unicast_packet *)payload;
1429 if (packet->header.packet_type != BATADV_UNICAST)
1430 goto out;
1431
1432 /* Find existing nc_path or create a new */
1433 nc_path = batadv_nc_get_path(bat_priv,
1434 bat_priv->nc.decoding_hash,
1435 ethhdr->h_source,
1436 ethhdr->h_dest);
1437
1438 if (!nc_path)
1439 goto out;
1440
1441 /* Clone skb and adjust skb->data to point at batman header */
1442 skb = skb_clone(skb, GFP_ATOMIC);
1443 if (unlikely(!skb))
1444 goto free_nc_path;
1445
1446 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
1447 goto free_skb;
1448
1449 if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
1450 goto free_skb;
1451
1452 /* Add skb to nc_path */
1453 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1454 if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
1455 goto free_skb;
1456
1457 batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
1458 return;
1459
1460free_skb:
1461 kfree_skb(skb);
1462free_nc_path:
1463 batadv_nc_path_free_ref(nc_path);
1464out:
1465 return;
1466}
1467
1468/**
1469 * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
1470 * should be saved in the decoding buffer and, if so, store it there
1471 * @bat_priv: the bat priv with all the soft interface information
1472 * @skb: unicast skb to store
1473 */
1474void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
1475 struct sk_buff *skb)
1476{
1477 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1478
1479 if (batadv_is_my_mac(ethhdr->h_dest))
1480 return;
1481
1482 /* Set data pointer to MAC header to mimic packets from our tx path */
1483 skb_push(skb, ETH_HLEN);
1484
1485 batadv_nc_skb_store_for_decoding(bat_priv, skb);
1486}
1487
1488/**
Martin Hundebølld353d8d2013-01-25 11:12:38 +01001489 * batadv_nc_free - clean up network coding memory
1490 * @bat_priv: the bat priv with all the soft interface information
1491 */
1492void batadv_nc_free(struct batadv_priv *bat_priv)
1493{
1494 cancel_delayed_work_sync(&bat_priv->nc.work);
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001495
Martin Hundebøll95332472013-01-25 11:12:40 +01001496 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
1497 batadv_hash_destroy(bat_priv->nc.coding_hash);
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001498 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
1499 batadv_hash_destroy(bat_priv->nc.decoding_hash);
Martin Hundebølld353d8d2013-01-25 11:12:38 +01001500}
Martin Hundebølld56b1702013-01-25 11:12:39 +01001501
1502/**
1503 * batadv_nc_nodes_seq_print_text - print the nc node information
1504 * @seq: seq file to print on
1505 * @offset: not used
1506 */
1507int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
1508{
1509 struct net_device *net_dev = (struct net_device *)seq->private;
1510 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1511 struct batadv_hashtable *hash = bat_priv->orig_hash;
1512 struct batadv_hard_iface *primary_if;
1513 struct hlist_head *head;
1514 struct batadv_orig_node *orig_node;
1515 struct batadv_nc_node *nc_node;
1516 int i;
1517
1518 primary_if = batadv_seq_print_text_primary_if_get(seq);
1519 if (!primary_if)
1520 goto out;
1521
1522 /* Traverse list of originators */
1523 for (i = 0; i < hash->size; i++) {
1524 head = &hash->table[i];
1525
1526 /* For each orig_node in this bin */
1527 rcu_read_lock();
1528 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1529 seq_printf(seq, "Node: %pM\n", orig_node->orig);
1530
1531 seq_printf(seq, " Ingoing: ");
1532 /* For each in_nc_node to this orig_node */
1533 list_for_each_entry_rcu(nc_node,
1534 &orig_node->in_coding_list,
1535 list)
1536 seq_printf(seq, "%pM ",
1537 nc_node->addr);
1538 seq_printf(seq, "\n");
1539
1540 seq_printf(seq, " Outgoing: ");
1541 /* For out_nc_node to this orig_node */
1542 list_for_each_entry_rcu(nc_node,
1543 &orig_node->out_coding_list,
1544 list)
1545 seq_printf(seq, "%pM ",
1546 nc_node->addr);
1547 seq_printf(seq, "\n\n");
1548 }
1549 rcu_read_unlock();
1550 }
1551
1552out:
1553 if (primary_if)
1554 batadv_hardif_free_ref(primary_if);
1555 return 0;
1556}
1557
1558/**
1559 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1560 * @bat_priv: the bat priv with all the soft interface information
1561 */
1562int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
1563{
1564 struct dentry *nc_dir, *file;
1565
1566 nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
1567 if (!nc_dir)
1568 goto out;
1569
1570 file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
1571 &bat_priv->nc.min_tq);
1572 if (!file)
1573 goto out;
1574
Martin Hundebøll95332472013-01-25 11:12:40 +01001575 file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir,
1576 &bat_priv->nc.max_fwd_delay);
1577 if (!file)
1578 goto out;
1579
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001580 file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir,
1581 &bat_priv->nc.max_buffer_time);
1582 if (!file)
1583 goto out;
1584
Martin Hundebølld56b1702013-01-25 11:12:39 +01001585 return 0;
1586
1587out:
1588 return -ENOMEM;
1589}