batman-adv: Distributed ARP Table - implement local storage
[linux-2.6-block.git] / net / batman-adv / distributed-arp-table.c
CommitLineData
785ea114
AQ
1/* Copyright (C) 2011-2012 B.A.T.M.A.N. contributors:
2 *
3 * Antonio Quartulli
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
20#include <linux/if_ether.h>
21#include <linux/if_arp.h>
22
23#include "main.h"
24#include "distributed-arp-table.h"
25#include "hard-interface.h"
26#include "originator.h"
27#include "send.h"
28#include "types.h"
29#include "unicast.h"
30
2f1dfbe1
AQ
31static void batadv_dat_purge(struct work_struct *work);
32
33/**
34 * batadv_dat_start_timer - initialise the DAT periodic worker
35 * @bat_priv: the bat priv with all the soft interface information
36 */
37static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
38{
39 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
40 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
41 msecs_to_jiffies(10000));
42}
43
44/**
45 * batadv_dat_entry_free_ref - decrements the dat_entry refcounter and possibly
46 * free it
47 * @dat_entry: the oentry to free
48 */
49static void batadv_dat_entry_free_ref(struct batadv_dat_entry *dat_entry)
50{
51 if (atomic_dec_and_test(&dat_entry->refcount))
52 kfree_rcu(dat_entry, rcu);
53}
54
55/**
56 * batadv_dat_to_purge - checks whether a dat_entry has to be purged or not
57 * @dat_entry: the entry to check
58 *
59 * Returns true if the entry has to be purged now, false otherwise
60 */
61static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
62{
63 return batadv_has_timed_out(dat_entry->last_update,
64 BATADV_DAT_ENTRY_TIMEOUT);
65}
66
67/**
68 * __batadv_dat_purge - delete entries from the DAT local storage
69 * @bat_priv: the bat priv with all the soft interface information
70 * @to_purge: function in charge to decide whether an entry has to be purged or
71 * not. This function takes the dat_entry as argument and has to
72 * returns a boolean value: true is the entry has to be deleted,
73 * false otherwise
74 *
75 * Loops over each entry in the DAT local storage and delete it if and only if
76 * the to_purge function passed as argument returns true
77 */
78static void __batadv_dat_purge(struct batadv_priv *bat_priv,
79 bool (*to_purge)(struct batadv_dat_entry *))
80{
81 spinlock_t *list_lock; /* protects write access to the hash lists */
82 struct batadv_dat_entry *dat_entry;
83 struct hlist_node *node, *node_tmp;
84 struct hlist_head *head;
85 uint32_t i;
86
87 if (!bat_priv->dat.hash)
88 return;
89
90 for (i = 0; i < bat_priv->dat.hash->size; i++) {
91 head = &bat_priv->dat.hash->table[i];
92 list_lock = &bat_priv->dat.hash->list_locks[i];
93
94 spin_lock_bh(list_lock);
95 hlist_for_each_entry_safe(dat_entry, node, node_tmp, head,
96 hash_entry) {
97 /* if an helper function has been passed as parameter,
98 * ask it if the entry has to be purged or not
99 */
100 if (to_purge && !to_purge(dat_entry))
101 continue;
102
103 hlist_del_rcu(node);
104 batadv_dat_entry_free_ref(dat_entry);
105 }
106 spin_unlock_bh(list_lock);
107 }
108}
109
110/**
111 * batadv_dat_purge - periodic task that deletes old entries from the local DAT
112 * hash table
113 * @work: kernel work struct
114 */
115static void batadv_dat_purge(struct work_struct *work)
116{
117 struct delayed_work *delayed_work;
118 struct batadv_priv_dat *priv_dat;
119 struct batadv_priv *bat_priv;
120
121 delayed_work = container_of(work, struct delayed_work, work);
122 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
123 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
124
125 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
126 batadv_dat_start_timer(bat_priv);
127}
128
129/**
130 * batadv_compare_dat - comparing function used in the local DAT hash table
131 * @node: node in the local table
132 * @data2: second object to compare the node to
133 *
134 * Returns 1 if the two entry are the same, 0 otherwise
135 */
136static int batadv_compare_dat(const struct hlist_node *node, const void *data2)
137{
138 const void *data1 = container_of(node, struct batadv_dat_entry,
139 hash_entry);
140
141 return (memcmp(data1, data2, sizeof(__be32)) == 0 ? 1 : 0);
142}
143
785ea114
AQ
144/**
145 * batadv_hash_dat - compute the hash value for an IP address
146 * @data: data to hash
147 * @size: size of the hash table
148 *
149 * Returns the selected index in the hash table for the given data
150 */
151static uint32_t batadv_hash_dat(const void *data, uint32_t size)
152{
153 const unsigned char *key = data;
154 uint32_t hash = 0;
155 size_t i;
156
157 for (i = 0; i < 4; i++) {
158 hash += key[i];
159 hash += (hash << 10);
160 hash ^= (hash >> 6);
161 }
162
163 hash += (hash << 3);
164 hash ^= (hash >> 11);
165 hash += (hash << 15);
166
167 return hash % size;
168}
169
2f1dfbe1
AQ
170/**
171 * batadv_dat_entry_hash_find - looks for a given dat_entry in the local hash
172 * table
173 * @bat_priv: the bat priv with all the soft interface information
174 * @ip: search key
175 *
176 * Returns the dat_entry if found, NULL otherwise
177 */
178static struct batadv_dat_entry *
179batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip)
180{
181 struct hlist_head *head;
182 struct hlist_node *node;
183 struct batadv_dat_entry *dat_entry, *dat_entry_tmp = NULL;
184 struct batadv_hashtable *hash = bat_priv->dat.hash;
185 uint32_t index;
186
187 if (!hash)
188 return NULL;
189
190 index = batadv_hash_dat(&ip, hash->size);
191 head = &hash->table[index];
192
193 rcu_read_lock();
194 hlist_for_each_entry_rcu(dat_entry, node, head, hash_entry) {
195 if (dat_entry->ip != ip)
196 continue;
197
198 if (!atomic_inc_not_zero(&dat_entry->refcount))
199 continue;
200
201 dat_entry_tmp = dat_entry;
202 break;
203 }
204 rcu_read_unlock();
205
206 return dat_entry_tmp;
207}
208
209/**
210 * batadv_dat_entry_add - add a new dat entry or update it if already exists
211 * @bat_priv: the bat priv with all the soft interface information
212 * @ip: ipv4 to add/edit
213 * @mac_addr: mac address to assign to the given ipv4
214 */
215static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
216 uint8_t *mac_addr)
217{
218 struct batadv_dat_entry *dat_entry;
219 int hash_added;
220
221 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip);
222 /* if this entry is already known, just update it */
223 if (dat_entry) {
224 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
225 memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
226 dat_entry->last_update = jiffies;
227 batadv_dbg(BATADV_DBG_DAT, bat_priv,
228 "Entry updated: %pI4 %pM\n", &dat_entry->ip,
229 dat_entry->mac_addr);
230 goto out;
231 }
232
233 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
234 if (!dat_entry)
235 goto out;
236
237 dat_entry->ip = ip;
238 memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
239 dat_entry->last_update = jiffies;
240 atomic_set(&dat_entry->refcount, 2);
241
242 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
243 batadv_hash_dat, &dat_entry->ip,
244 &dat_entry->hash_entry);
245
246 if (unlikely(hash_added != 0)) {
247 /* remove the reference for the hash */
248 batadv_dat_entry_free_ref(dat_entry);
249 goto out;
250 }
251
252 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM\n",
253 &dat_entry->ip, dat_entry->mac_addr);
254
255out:
256 if (dat_entry)
257 batadv_dat_entry_free_ref(dat_entry);
258}
259
785ea114
AQ
260/**
261 * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate
262 * @res: the array with the already selected candidates
263 * @select: number of already selected candidates
264 * @tmp_max: address of the currently evaluated node
265 * @max: current round max address
266 * @last_max: address of the last selected candidate
267 * @candidate: orig_node under evaluation
268 * @max_orig_node: last selected candidate
269 *
270 * Returns true if the node has been elected as next candidate or false othrwise
271 */
272static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
273 int select, batadv_dat_addr_t tmp_max,
274 batadv_dat_addr_t max,
275 batadv_dat_addr_t last_max,
276 struct batadv_orig_node *candidate,
277 struct batadv_orig_node *max_orig_node)
278{
279 bool ret = false;
280 int j;
281
282 /* Check if this node has already been selected... */
283 for (j = 0; j < select; j++)
284 if (res[j].orig_node == candidate)
285 break;
286 /* ..and possibly skip it */
287 if (j < select)
288 goto out;
289 /* sanity check: has it already been selected? This should not happen */
290 if (tmp_max > last_max)
291 goto out;
292 /* check if during this iteration an originator with a closer dht
293 * address has already been found
294 */
295 if (tmp_max < max)
296 goto out;
297 /* this is an hash collision with the temporary selected node. Choose
298 * the one with the lowest address
299 */
300 if ((tmp_max == max) &&
301 (batadv_compare_eth(candidate->orig, max_orig_node->orig) > 0))
302 goto out;
303
304 ret = true;
305out:
306 return ret;
307}
308
309/**
310 * batadv_choose_next_candidate - select the next DHT candidate
311 * @bat_priv: the bat priv with all the soft interface information
312 * @cands: candidates array
313 * @select: number of candidates already present in the array
314 * @ip_key: key to look up in the DHT
315 * @last_max: pointer where the address of the selected candidate will be saved
316 */
317static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
318 struct batadv_dat_candidate *cands,
319 int select, batadv_dat_addr_t ip_key,
320 batadv_dat_addr_t *last_max)
321{
322 batadv_dat_addr_t max = 0, tmp_max = 0;
323 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
324 struct batadv_hashtable *hash = bat_priv->orig_hash;
325 struct hlist_node *node;
326 struct hlist_head *head;
327 int i;
328
329 /* if no node is eligible as candidate, leave the candidate type as
330 * NOT_FOUND
331 */
332 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
333
334 /* iterate over the originator list and find the node with closest
335 * dat_address which has not been selected yet
336 */
337 for (i = 0; i < hash->size; i++) {
338 head = &hash->table[i];
339
340 rcu_read_lock();
341 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
342 /* the dht space is a ring and addresses are unsigned */
343 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
344 ip_key;
345
346 if (!batadv_is_orig_node_eligible(cands, select,
347 tmp_max, max,
348 *last_max, orig_node,
349 max_orig_node))
350 continue;
351
352 if (!atomic_inc_not_zero(&orig_node->refcount))
353 continue;
354
355 max = tmp_max;
356 if (max_orig_node)
357 batadv_orig_node_free_ref(max_orig_node);
358 max_orig_node = orig_node;
359 }
360 rcu_read_unlock();
361 }
362 if (max_orig_node) {
363 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
364 cands[select].orig_node = max_orig_node;
365 batadv_dbg(BATADV_DBG_DAT, bat_priv,
366 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
367 select, max_orig_node->orig, max_orig_node->dat_addr,
368 max);
369 }
370 *last_max = max;
371}
372
373/**
374 * batadv_dat_select_candidates - selects the nodes which the DHT message has to
375 * be sent to
376 * @bat_priv: the bat priv with all the soft interface information
377 * @ip_dst: ipv4 to look up in the DHT
378 *
379 * An originator O is selected if and only if its DHT_ID value is one of three
380 * closest values (from the LEFT, with wrap around if needed) then the hash
381 * value of the key. ip_dst is the key.
382 *
383 * Returns the candidate array of size BATADV_DAT_CANDIDATE_NUM
384 */
385static struct batadv_dat_candidate *
386batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst)
387{
388 int select;
389 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
390 struct batadv_dat_candidate *res;
391
392 if (!bat_priv->orig_hash)
393 return NULL;
394
395 res = kmalloc(BATADV_DAT_CANDIDATES_NUM * sizeof(*res), GFP_ATOMIC);
396 if (!res)
397 return NULL;
398
399 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&ip_dst,
400 BATADV_DAT_ADDR_MAX);
401
402 batadv_dbg(BATADV_DBG_DAT, bat_priv,
403 "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &ip_dst,
404 ip_key);
405
406 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
407 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
408 &last_max);
409
410 return res;
411}
412
413/**
414 * batadv_dat_send_data - send a payload to the selected candidates
415 * @bat_priv: the bat priv with all the soft interface information
416 * @skb: payload to send
417 * @ip: the DHT key
418 * @packet_subtype: unicast4addr packet subtype to use
419 *
420 * In this function the skb is copied by means of pskb_copy() and is sent as
421 * unicast packet to each of the selected candidates
422 *
423 * Returns true if the packet is sent to at least one candidate, false otherwise
424 */
425static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
426 struct sk_buff *skb, __be32 ip,
427 int packet_subtype)
428{
429 int i;
430 bool ret = false;
431 int send_status;
432 struct batadv_neigh_node *neigh_node = NULL;
433 struct sk_buff *tmp_skb;
434 struct batadv_dat_candidate *cand;
435
436 cand = batadv_dat_select_candidates(bat_priv, ip);
437 if (!cand)
438 goto out;
439
440 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
441
442 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
443 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
444 continue;
445
446 neigh_node = batadv_orig_node_get_router(cand[i].orig_node);
447 if (!neigh_node)
448 goto free_orig;
449
450 tmp_skb = pskb_copy(skb, GFP_ATOMIC);
451 if (!batadv_unicast_4addr_prepare_skb(bat_priv, tmp_skb,
452 cand[i].orig_node,
453 packet_subtype)) {
454 kfree_skb(tmp_skb);
455 goto free_neigh;
456 }
457
458 send_status = batadv_send_skb_packet(tmp_skb,
459 neigh_node->if_incoming,
460 neigh_node->addr);
461 if (send_status == NET_XMIT_SUCCESS)
462 /* packet sent to a candidate: return true */
463 ret = true;
464free_neigh:
465 batadv_neigh_node_free_ref(neigh_node);
466free_orig:
467 batadv_orig_node_free_ref(cand[i].orig_node);
468 }
469
470out:
471 kfree(cand);
472 return ret;
473}
2f1dfbe1
AQ
474
475/**
476 * batadv_dat_hash_free - free the local DAT hash table
477 * @bat_priv: the bat priv with all the soft interface information
478 */
479static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
480{
481 __batadv_dat_purge(bat_priv, NULL);
482
483 batadv_hash_destroy(bat_priv->dat.hash);
484
485 bat_priv->dat.hash = NULL;
486}
487
488/**
489 * batadv_dat_init - initialise the DAT internals
490 * @bat_priv: the bat priv with all the soft interface information
491 */
492int batadv_dat_init(struct batadv_priv *bat_priv)
493{
494 if (bat_priv->dat.hash)
495 return 0;
496
497 bat_priv->dat.hash = batadv_hash_new(1024);
498
499 if (!bat_priv->dat.hash)
500 return -ENOMEM;
501
502 batadv_dat_start_timer(bat_priv);
503
504 return 0;
505}
506
507/**
508 * batadv_dat_free - free the DAT internals
509 * @bat_priv: the bat priv with all the soft interface information
510 */
511void batadv_dat_free(struct batadv_priv *bat_priv)
512{
513 cancel_delayed_work_sync(&bat_priv->dat.work);
514
515 batadv_dat_hash_free(bat_priv);
516}
517
518/**
519 * batadv_dat_cache_seq_print_text - print the local DAT hash table
520 * @seq: seq file to print on
521 * @offset: not used
522 */
523int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
524{
525 struct net_device *net_dev = (struct net_device *)seq->private;
526 struct batadv_priv *bat_priv = netdev_priv(net_dev);
527 struct batadv_hashtable *hash = bat_priv->dat.hash;
528 struct batadv_dat_entry *dat_entry;
529 struct batadv_hard_iface *primary_if;
530 struct hlist_node *node;
531 struct hlist_head *head;
532 unsigned long last_seen_jiffies;
533 int last_seen_msecs, last_seen_secs, last_seen_mins;
534 uint32_t i;
535
536 primary_if = batadv_seq_print_text_primary_if_get(seq);
537 if (!primary_if)
538 goto out;
539
540 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
541 seq_printf(seq, " %-7s %-13s %5s\n", "IPv4", "MAC",
542 "last-seen");
543
544 for (i = 0; i < hash->size; i++) {
545 head = &hash->table[i];
546
547 rcu_read_lock();
548 hlist_for_each_entry_rcu(dat_entry, node, head, hash_entry) {
549 last_seen_jiffies = jiffies - dat_entry->last_update;
550 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
551 last_seen_mins = last_seen_msecs / 60000;
552 last_seen_msecs = last_seen_msecs % 60000;
553 last_seen_secs = last_seen_msecs / 1000;
554
555 seq_printf(seq, " * %15pI4 %14pM %6i:%02i\n",
556 &dat_entry->ip, dat_entry->mac_addr,
557 last_seen_mins, last_seen_secs);
558 }
559 rcu_read_unlock();
560 }
561
562out:
563 if (primary_if)
564 batadv_hardif_free_ref(primary_if);
565 return 0;
566}