2828#include "types.h"
2929#include "unicast.h"
3030
31+ static 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+ */
37+ static 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+ */
49+ static 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+ */
61+ static 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+ */
78+ static 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+ */
115+ static 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+ */
136+ static 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+
31144/**
32145 * batadv_hash_dat - compute the hash value for an IP address
33146 * @data: data to hash
@@ -54,6 +167,96 @@ static uint32_t batadv_hash_dat(const void *data, uint32_t size)
54167 return hash % size ;
55168}
56169
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+ */
178+ static struct batadv_dat_entry *
179+ batadv_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+ */
215+ static 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+
255+ out :
256+ if (dat_entry )
257+ batadv_dat_entry_free_ref (dat_entry );
258+ }
259+
57260/**
58261 * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate
59262 * @res: the array with the already selected candidates
@@ -268,3 +471,96 @@ static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
268471 kfree (cand );
269472 return ret ;
270473}
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+ */
479+ static 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+ */
492+ int 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+ */
511+ void 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+ */
523+ int 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+
562+ out :
563+ if (primary_if )
564+ batadv_hardif_free_ref (primary_if );
565+ return 0 ;
566+ }
0 commit comments