]>
Commit | Line | Data |
---|---|---|
9cfc7bd6 | 1 | /* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
2 | * |
3 | * Simon Wunderlich | |
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 | |
c6c8fea2 SE |
18 | */ |
19 | ||
20 | #include "main.h" | |
21 | #include "send.h" | |
22 | #include "translation-table.h" | |
23 | #include "vis.h" | |
24 | #include "soft-interface.h" | |
25 | #include "hard-interface.h" | |
26 | #include "hash.h" | |
27 | #include "originator.h" | |
28 | ||
347c80f0 | 29 | #define BATADV_MAX_VIS_PACKET_SIZE 1000 |
c6c8fea2 | 30 | |
56303d34 | 31 | static void batadv_start_vis_timer(struct batadv_priv *bat_priv); |
c6c8fea2 SE |
32 | |
33 | /* free the info */ | |
eaad8ad9 | 34 | static void batadv_free_info(struct kref *ref) |
c6c8fea2 | 35 | { |
56303d34 SE |
36 | struct batadv_vis_info *info; |
37 | struct batadv_priv *bat_priv; | |
38 | struct batadv_recvlist_node *entry, *tmp; | |
39 | ||
40 | info = container_of(ref, struct batadv_vis_info, refcount); | |
41 | bat_priv = info->bat_priv; | |
c6c8fea2 SE |
42 | |
43 | list_del_init(&info->send_list); | |
807736f6 | 44 | spin_lock_bh(&bat_priv->vis.list_lock); |
c6c8fea2 SE |
45 | list_for_each_entry_safe(entry, tmp, &info->recv_list, list) { |
46 | list_del(&entry->list); | |
47 | kfree(entry); | |
48 | } | |
49 | ||
807736f6 | 50 | spin_unlock_bh(&bat_priv->vis.list_lock); |
c6c8fea2 | 51 | kfree_skb(info->skb_packet); |
dda9fc6b | 52 | kfree(info); |
c6c8fea2 SE |
53 | } |
54 | ||
55 | /* Compare two vis packets, used by the hashing algorithm */ | |
eaad8ad9 | 56 | static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2) |
c6c8fea2 | 57 | { |
56303d34 | 58 | const struct batadv_vis_info *d1, *d2; |
96412690 | 59 | const struct batadv_vis_packet *p1, *p2; |
7aadf889 | 60 | |
56303d34 | 61 | d1 = container_of(node, struct batadv_vis_info, hash_entry); |
c6c8fea2 | 62 | d2 = data2; |
96412690 SE |
63 | p1 = (struct batadv_vis_packet *)d1->skb_packet->data; |
64 | p2 = (struct batadv_vis_packet *)d2->skb_packet->data; | |
1eda58bf | 65 | return batadv_compare_eth(p1->vis_orig, p2->vis_orig); |
c6c8fea2 SE |
66 | } |
67 | ||
9cfc7bd6 SE |
68 | /* hash function to choose an entry in a hash table of given size |
69 | * hash algorithm from http://en.wikipedia.org/wiki/Hash_table | |
70 | */ | |
eaad8ad9 | 71 | static uint32_t batadv_vis_info_choose(const void *data, uint32_t size) |
c6c8fea2 | 72 | { |
56303d34 | 73 | const struct batadv_vis_info *vis_info = data; |
96412690 | 74 | const struct batadv_vis_packet *packet; |
747e4221 | 75 | const unsigned char *key; |
c6c8fea2 SE |
76 | uint32_t hash = 0; |
77 | size_t i; | |
78 | ||
96412690 | 79 | packet = (struct batadv_vis_packet *)vis_info->skb_packet->data; |
c6c8fea2 SE |
80 | key = packet->vis_orig; |
81 | for (i = 0; i < ETH_ALEN; i++) { | |
82 | hash += key[i]; | |
83 | hash += (hash << 10); | |
84 | hash ^= (hash >> 6); | |
85 | } | |
86 | ||
87 | hash += (hash << 3); | |
88 | hash ^= (hash >> 11); | |
89 | hash += (hash << 15); | |
90 | ||
91 | return hash % size; | |
92 | } | |
93 | ||
56303d34 SE |
94 | static struct batadv_vis_info * |
95 | batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data) | |
7aadf889 | 96 | { |
807736f6 | 97 | struct batadv_hashtable *hash = bat_priv->vis.hash; |
7aadf889 ML |
98 | struct hlist_head *head; |
99 | struct hlist_node *node; | |
56303d34 | 100 | struct batadv_vis_info *vis_info, *vis_info_tmp = NULL; |
c90681b8 | 101 | uint32_t index; |
7aadf889 ML |
102 | |
103 | if (!hash) | |
104 | return NULL; | |
105 | ||
eaad8ad9 | 106 | index = batadv_vis_info_choose(data, hash->size); |
7aadf889 ML |
107 | head = &hash->table[index]; |
108 | ||
109 | rcu_read_lock(); | |
110 | hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) { | |
eaad8ad9 | 111 | if (!batadv_vis_info_cmp(node, data)) |
7aadf889 ML |
112 | continue; |
113 | ||
114 | vis_info_tmp = vis_info; | |
115 | break; | |
116 | } | |
117 | rcu_read_unlock(); | |
118 | ||
119 | return vis_info_tmp; | |
120 | } | |
121 | ||
c6c8fea2 | 122 | /* insert interface to the list of interfaces of one originator, if it |
9cfc7bd6 SE |
123 | * does not already exist in the list |
124 | */ | |
eaad8ad9 SE |
125 | static void batadv_vis_data_insert_interface(const uint8_t *interface, |
126 | struct hlist_head *if_list, | |
127 | bool primary) | |
c6c8fea2 | 128 | { |
56303d34 | 129 | struct batadv_if_list_entry *entry; |
c6c8fea2 SE |
130 | struct hlist_node *pos; |
131 | ||
132 | hlist_for_each_entry(entry, pos, if_list, list) { | |
1eda58bf | 133 | if (batadv_compare_eth(entry->addr, interface)) |
c6c8fea2 SE |
134 | return; |
135 | } | |
136 | ||
015758d0 | 137 | /* it's a new address, add it to the list */ |
c6c8fea2 SE |
138 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
139 | if (!entry) | |
140 | return; | |
141 | memcpy(entry->addr, interface, ETH_ALEN); | |
142 | entry->primary = primary; | |
143 | hlist_add_head(&entry->list, if_list); | |
144 | } | |
145 | ||
28afd3c0 SE |
146 | static void batadv_vis_data_read_prim_sec(struct seq_file *seq, |
147 | const struct hlist_head *if_list) | |
c6c8fea2 | 148 | { |
56303d34 | 149 | struct batadv_if_list_entry *entry; |
c6c8fea2 | 150 | struct hlist_node *pos; |
c6c8fea2 SE |
151 | |
152 | hlist_for_each_entry(entry, pos, if_list, list) { | |
153 | if (entry->primary) | |
28afd3c0 | 154 | seq_printf(seq, "PRIMARY, "); |
c6c8fea2 | 155 | else |
28afd3c0 | 156 | seq_printf(seq, "SEC %pM, ", entry->addr); |
c6c8fea2 | 157 | } |
28afd3c0 SE |
158 | } |
159 | ||
160 | /* read an entry */ | |
56303d34 SE |
161 | static ssize_t |
162 | batadv_vis_data_read_entry(struct seq_file *seq, | |
163 | const struct batadv_vis_info_entry *entry, | |
164 | const uint8_t *src, bool primary) | |
28afd3c0 SE |
165 | { |
166 | if (primary && entry->quality == 0) | |
167 | return seq_printf(seq, "TT %pM, ", entry->dest); | |
168 | else if (batadv_compare_eth(entry->src, src)) | |
169 | return seq_printf(seq, "TQ %pM %d, ", entry->dest, | |
170 | entry->quality); | |
c6c8fea2 | 171 | |
28afd3c0 | 172 | return 0; |
c6c8fea2 SE |
173 | } |
174 | ||
56303d34 SE |
175 | static void |
176 | batadv_vis_data_insert_interfaces(struct hlist_head *list, | |
177 | struct batadv_vis_packet *packet, | |
178 | struct batadv_vis_info_entry *entries) | |
c6c8fea2 | 179 | { |
28afd3c0 SE |
180 | int i; |
181 | ||
182 | for (i = 0; i < packet->entries; i++) { | |
183 | if (entries[i].quality == 0) | |
184 | continue; | |
185 | ||
186 | if (batadv_compare_eth(entries[i].src, packet->vis_orig)) | |
187 | continue; | |
188 | ||
189 | batadv_vis_data_insert_interface(entries[i].src, list, false); | |
190 | } | |
191 | } | |
192 | ||
193 | static void batadv_vis_data_read_entries(struct seq_file *seq, | |
194 | struct hlist_head *list, | |
96412690 | 195 | struct batadv_vis_packet *packet, |
56303d34 | 196 | struct batadv_vis_info_entry *entries) |
28afd3c0 SE |
197 | { |
198 | int i; | |
56303d34 | 199 | struct batadv_if_list_entry *entry; |
c6c8fea2 | 200 | struct hlist_node *pos; |
c6c8fea2 | 201 | |
28afd3c0 SE |
202 | hlist_for_each_entry(entry, pos, list, list) { |
203 | seq_printf(seq, "%pM,", entry->addr); | |
204 | ||
205 | for (i = 0; i < packet->entries; i++) | |
206 | batadv_vis_data_read_entry(seq, &entries[i], | |
207 | entry->addr, entry->primary); | |
208 | ||
209 | /* add primary/secondary records */ | |
210 | if (batadv_compare_eth(entry->addr, packet->vis_orig)) | |
211 | batadv_vis_data_read_prim_sec(seq, list); | |
c6c8fea2 | 212 | |
28afd3c0 SE |
213 | seq_printf(seq, "\n"); |
214 | } | |
c6c8fea2 SE |
215 | } |
216 | ||
28afd3c0 SE |
217 | static void batadv_vis_seq_print_text_bucket(struct seq_file *seq, |
218 | const struct hlist_head *head) | |
c6c8fea2 | 219 | { |
28afd3c0 | 220 | struct hlist_node *node; |
56303d34 | 221 | struct batadv_vis_info *info; |
96412690 | 222 | struct batadv_vis_packet *packet; |
28afd3c0 | 223 | uint8_t *entries_pos; |
56303d34 SE |
224 | struct batadv_vis_info_entry *entries; |
225 | struct batadv_if_list_entry *entry; | |
28afd3c0 | 226 | struct hlist_node *pos, *n; |
c6c8fea2 | 227 | |
28afd3c0 SE |
228 | HLIST_HEAD(vis_if_list); |
229 | ||
230 | hlist_for_each_entry_rcu(info, node, head, hash_entry) { | |
96412690 | 231 | packet = (struct batadv_vis_packet *)info->skb_packet->data; |
28afd3c0 | 232 | entries_pos = (uint8_t *)packet + sizeof(*packet); |
56303d34 | 233 | entries = (struct batadv_vis_info_entry *)entries_pos; |
28afd3c0 SE |
234 | |
235 | batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list, | |
236 | true); | |
237 | batadv_vis_data_insert_interfaces(&vis_if_list, packet, | |
238 | entries); | |
239 | batadv_vis_data_read_entries(seq, &vis_if_list, packet, | |
240 | entries); | |
241 | ||
242 | hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) { | |
243 | hlist_del(&entry->list); | |
244 | kfree(entry); | |
245 | } | |
246 | } | |
c6c8fea2 SE |
247 | } |
248 | ||
d0f714f4 | 249 | int batadv_vis_seq_print_text(struct seq_file *seq, void *offset) |
c6c8fea2 | 250 | { |
56303d34 | 251 | struct batadv_hard_iface *primary_if; |
c6c8fea2 | 252 | struct hlist_head *head; |
c6c8fea2 | 253 | struct net_device *net_dev = (struct net_device *)seq->private; |
56303d34 | 254 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
807736f6 | 255 | struct batadv_hashtable *hash = bat_priv->vis.hash; |
c90681b8 | 256 | uint32_t i; |
28afd3c0 | 257 | int ret = 0; |
c6c8fea2 | 258 | int vis_server = atomic_read(&bat_priv->vis_mode); |
c6c8fea2 | 259 | |
e5d89254 | 260 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
261 | if (!primary_if) |
262 | goto out; | |
263 | ||
acd34afa | 264 | if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE) |
32ae9b22 | 265 | goto out; |
c6c8fea2 | 266 | |
807736f6 | 267 | spin_lock_bh(&bat_priv->vis.hash_lock); |
c6c8fea2 SE |
268 | for (i = 0; i < hash->size; i++) { |
269 | head = &hash->table[i]; | |
28afd3c0 | 270 | batadv_vis_seq_print_text_bucket(seq, head); |
c6c8fea2 | 271 | } |
807736f6 | 272 | spin_unlock_bh(&bat_priv->vis.hash_lock); |
c6c8fea2 | 273 | |
32ae9b22 ML |
274 | out: |
275 | if (primary_if) | |
e5d89254 | 276 | batadv_hardif_free_ref(primary_if); |
32ae9b22 | 277 | return ret; |
c6c8fea2 SE |
278 | } |
279 | ||
280 | /* add the info packet to the send list, if it was not | |
9cfc7bd6 SE |
281 | * already linked in. |
282 | */ | |
56303d34 SE |
283 | static void batadv_send_list_add(struct batadv_priv *bat_priv, |
284 | struct batadv_vis_info *info) | |
c6c8fea2 SE |
285 | { |
286 | if (list_empty(&info->send_list)) { | |
287 | kref_get(&info->refcount); | |
807736f6 | 288 | list_add_tail(&info->send_list, &bat_priv->vis.send_list); |
c6c8fea2 SE |
289 | } |
290 | } | |
291 | ||
292 | /* delete the info packet from the send list, if it was | |
9cfc7bd6 SE |
293 | * linked in. |
294 | */ | |
56303d34 | 295 | static void batadv_send_list_del(struct batadv_vis_info *info) |
c6c8fea2 SE |
296 | { |
297 | if (!list_empty(&info->send_list)) { | |
298 | list_del_init(&info->send_list); | |
eaad8ad9 | 299 | kref_put(&info->refcount, batadv_free_info); |
c6c8fea2 SE |
300 | } |
301 | } | |
302 | ||
303 | /* tries to add one entry to the receive list. */ | |
56303d34 | 304 | static void batadv_recv_list_add(struct batadv_priv *bat_priv, |
eaad8ad9 | 305 | struct list_head *recv_list, const char *mac) |
c6c8fea2 | 306 | { |
56303d34 | 307 | struct batadv_recvlist_node *entry; |
c6c8fea2 | 308 | |
704509b8 | 309 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
c6c8fea2 SE |
310 | if (!entry) |
311 | return; | |
312 | ||
313 | memcpy(entry->mac, mac, ETH_ALEN); | |
807736f6 | 314 | spin_lock_bh(&bat_priv->vis.list_lock); |
c6c8fea2 | 315 | list_add_tail(&entry->list, recv_list); |
807736f6 | 316 | spin_unlock_bh(&bat_priv->vis.list_lock); |
c6c8fea2 SE |
317 | } |
318 | ||
319 | /* returns 1 if this mac is in the recv_list */ | |
56303d34 | 320 | static int batadv_recv_list_is_in(struct batadv_priv *bat_priv, |
eaad8ad9 SE |
321 | const struct list_head *recv_list, |
322 | const char *mac) | |
c6c8fea2 | 323 | { |
56303d34 | 324 | const struct batadv_recvlist_node *entry; |
c6c8fea2 | 325 | |
807736f6 | 326 | spin_lock_bh(&bat_priv->vis.list_lock); |
c6c8fea2 | 327 | list_for_each_entry(entry, recv_list, list) { |
1eda58bf | 328 | if (batadv_compare_eth(entry->mac, mac)) { |
807736f6 | 329 | spin_unlock_bh(&bat_priv->vis.list_lock); |
c6c8fea2 SE |
330 | return 1; |
331 | } | |
332 | } | |
807736f6 | 333 | spin_unlock_bh(&bat_priv->vis.list_lock); |
c6c8fea2 SE |
334 | return 0; |
335 | } | |
336 | ||
337 | /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old, | |
338 | * broken.. ). vis hash must be locked outside. is_new is set when the packet | |
9cfc7bd6 SE |
339 | * is newer than old entries in the hash. |
340 | */ | |
56303d34 SE |
341 | static struct batadv_vis_info * |
342 | batadv_add_packet(struct batadv_priv *bat_priv, | |
343 | struct batadv_vis_packet *vis_packet, int vis_info_len, | |
344 | int *is_new, int make_broadcast) | |
c6c8fea2 | 345 | { |
56303d34 | 346 | struct batadv_vis_info *info, *old_info; |
96412690 | 347 | struct batadv_vis_packet *search_packet, *old_packet; |
56303d34 | 348 | struct batadv_vis_info search_elem; |
96412690 SE |
349 | struct batadv_vis_packet *packet; |
350 | struct sk_buff *tmp_skb; | |
c6c8fea2 | 351 | int hash_added; |
96412690 | 352 | size_t len; |
56303d34 | 353 | size_t max_entries; |
c6c8fea2 SE |
354 | |
355 | *is_new = 0; | |
356 | /* sanity check */ | |
807736f6 | 357 | if (!bat_priv->vis.hash) |
c6c8fea2 SE |
358 | return NULL; |
359 | ||
360 | /* see if the packet is already in vis_hash */ | |
704509b8 | 361 | search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet)); |
c6c8fea2 SE |
362 | if (!search_elem.skb_packet) |
363 | return NULL; | |
96412690 SE |
364 | len = sizeof(*search_packet); |
365 | tmp_skb = search_elem.skb_packet; | |
366 | search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len); | |
c6c8fea2 SE |
367 | |
368 | memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN); | |
eaad8ad9 | 369 | old_info = batadv_vis_hash_find(bat_priv, &search_elem); |
c6c8fea2 SE |
370 | kfree_skb(search_elem.skb_packet); |
371 | ||
372 | if (old_info) { | |
96412690 SE |
373 | tmp_skb = old_info->skb_packet; |
374 | old_packet = (struct batadv_vis_packet *)tmp_skb->data; | |
3e34819e SE |
375 | if (!batadv_seq_after(ntohl(vis_packet->seqno), |
376 | ntohl(old_packet->seqno))) { | |
c6c8fea2 | 377 | if (old_packet->seqno == vis_packet->seqno) { |
eaad8ad9 SE |
378 | batadv_recv_list_add(bat_priv, |
379 | &old_info->recv_list, | |
380 | vis_packet->sender_orig); | |
c6c8fea2 SE |
381 | return old_info; |
382 | } else { | |
383 | /* newer packet is already in hash. */ | |
384 | return NULL; | |
385 | } | |
386 | } | |
387 | /* remove old entry */ | |
807736f6 | 388 | batadv_hash_remove(bat_priv->vis.hash, batadv_vis_info_cmp, |
eaad8ad9 SE |
389 | batadv_vis_info_choose, old_info); |
390 | batadv_send_list_del(old_info); | |
391 | kref_put(&old_info->refcount, batadv_free_info); | |
c6c8fea2 SE |
392 | } |
393 | ||
704509b8 | 394 | info = kmalloc(sizeof(*info), GFP_ATOMIC); |
c6c8fea2 SE |
395 | if (!info) |
396 | return NULL; | |
397 | ||
96412690 | 398 | len = sizeof(*packet) + vis_info_len; |
5b246574 | 399 | info->skb_packet = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN); |
c6c8fea2 SE |
400 | if (!info->skb_packet) { |
401 | kfree(info); | |
402 | return NULL; | |
403 | } | |
5b246574 | 404 | skb_reserve(info->skb_packet, ETH_HLEN + NET_IP_ALIGN); |
96412690 | 405 | packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len); |
c6c8fea2 SE |
406 | |
407 | kref_init(&info->refcount); | |
408 | INIT_LIST_HEAD(&info->send_list); | |
409 | INIT_LIST_HEAD(&info->recv_list); | |
410 | info->first_seen = jiffies; | |
411 | info->bat_priv = bat_priv; | |
96412690 | 412 | memcpy(packet, vis_packet, len); |
c6c8fea2 SE |
413 | |
414 | /* initialize and add new packet. */ | |
415 | *is_new = 1; | |
416 | ||
417 | /* Make it a broadcast packet, if required */ | |
418 | if (make_broadcast) | |
3193e8fd | 419 | memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN); |
c6c8fea2 SE |
420 | |
421 | /* repair if entries is longer than packet. */ | |
56303d34 SE |
422 | max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry); |
423 | if (packet->entries > max_entries) | |
424 | packet->entries = max_entries; | |
c6c8fea2 | 425 | |
eaad8ad9 | 426 | batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig); |
c6c8fea2 SE |
427 | |
428 | /* try to add it */ | |
807736f6 | 429 | hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp, |
eaad8ad9 SE |
430 | batadv_vis_info_choose, info, |
431 | &info->hash_entry); | |
1a1f37d9 | 432 | if (hash_added != 0) { |
c6c8fea2 | 433 | /* did not work (for some reason) */ |
eaad8ad9 | 434 | kref_put(&info->refcount, batadv_free_info); |
c6c8fea2 SE |
435 | info = NULL; |
436 | } | |
437 | ||
438 | return info; | |
439 | } | |
440 | ||
441 | /* handle the server sync packet, forward if needed. */ | |
56303d34 | 442 | void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv, |
96412690 | 443 | struct batadv_vis_packet *vis_packet, |
d0f714f4 | 444 | int vis_info_len) |
c6c8fea2 | 445 | { |
56303d34 | 446 | struct batadv_vis_info *info; |
c6c8fea2 SE |
447 | int is_new, make_broadcast; |
448 | int vis_server = atomic_read(&bat_priv->vis_mode); | |
449 | ||
acd34afa | 450 | make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC); |
c6c8fea2 | 451 | |
807736f6 | 452 | spin_lock_bh(&bat_priv->vis.hash_lock); |
eaad8ad9 SE |
453 | info = batadv_add_packet(bat_priv, vis_packet, vis_info_len, |
454 | &is_new, make_broadcast); | |
c6c8fea2 SE |
455 | if (!info) |
456 | goto end; | |
457 | ||
458 | /* only if we are server ourselves and packet is newer than the one in | |
9cfc7bd6 SE |
459 | * hash. |
460 | */ | |
acd34afa | 461 | if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new) |
eaad8ad9 | 462 | batadv_send_list_add(bat_priv, info); |
c6c8fea2 | 463 | end: |
807736f6 | 464 | spin_unlock_bh(&bat_priv->vis.hash_lock); |
c6c8fea2 SE |
465 | } |
466 | ||
467 | /* handle an incoming client update packet and schedule forward if needed. */ | |
56303d34 | 468 | void batadv_receive_client_update_packet(struct batadv_priv *bat_priv, |
96412690 | 469 | struct batadv_vis_packet *vis_packet, |
d0f714f4 | 470 | int vis_info_len) |
c6c8fea2 | 471 | { |
56303d34 | 472 | struct batadv_vis_info *info; |
96412690 | 473 | struct batadv_vis_packet *packet; |
c6c8fea2 SE |
474 | int is_new; |
475 | int vis_server = atomic_read(&bat_priv->vis_mode); | |
476 | int are_target = 0; | |
477 | ||
478 | /* clients shall not broadcast. */ | |
479 | if (is_broadcast_ether_addr(vis_packet->target_orig)) | |
480 | return; | |
481 | ||
482 | /* Are we the target for this VIS packet? */ | |
acd34afa | 483 | if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && |
3193e8fd | 484 | batadv_is_my_mac(vis_packet->target_orig)) |
c6c8fea2 SE |
485 | are_target = 1; |
486 | ||
807736f6 | 487 | spin_lock_bh(&bat_priv->vis.hash_lock); |
eaad8ad9 SE |
488 | info = batadv_add_packet(bat_priv, vis_packet, vis_info_len, |
489 | &is_new, are_target); | |
c6c8fea2 SE |
490 | |
491 | if (!info) | |
492 | goto end; | |
493 | /* note that outdated packets will be dropped at this point. */ | |
494 | ||
96412690 | 495 | packet = (struct batadv_vis_packet *)info->skb_packet->data; |
c6c8fea2 SE |
496 | |
497 | /* send only if we're the target server or ... */ | |
498 | if (are_target && is_new) { | |
acd34afa | 499 | packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */ |
eaad8ad9 | 500 | batadv_send_list_add(bat_priv, info); |
c6c8fea2 SE |
501 | |
502 | /* ... we're not the recipient (and thus need to forward). */ | |
3193e8fd | 503 | } else if (!batadv_is_my_mac(packet->target_orig)) { |
eaad8ad9 | 504 | batadv_send_list_add(bat_priv, info); |
c6c8fea2 SE |
505 | } |
506 | ||
507 | end: | |
807736f6 | 508 | spin_unlock_bh(&bat_priv->vis.hash_lock); |
c6c8fea2 SE |
509 | } |
510 | ||
511 | /* Walk the originators and find the VIS server with the best tq. Set the packet | |
512 | * address to its address and return the best_tq. | |
513 | * | |
9cfc7bd6 SE |
514 | * Must be called with the originator hash locked |
515 | */ | |
56303d34 SE |
516 | static int batadv_find_best_vis_server(struct batadv_priv *bat_priv, |
517 | struct batadv_vis_info *info) | |
c6c8fea2 | 518 | { |
5bf74e9c | 519 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
56303d34 | 520 | struct batadv_neigh_node *router; |
7aadf889 | 521 | struct hlist_node *node; |
c6c8fea2 | 522 | struct hlist_head *head; |
56303d34 | 523 | struct batadv_orig_node *orig_node; |
96412690 | 524 | struct batadv_vis_packet *packet; |
c90681b8 AQ |
525 | int best_tq = -1; |
526 | uint32_t i; | |
c6c8fea2 | 527 | |
96412690 | 528 | packet = (struct batadv_vis_packet *)info->skb_packet->data; |
c6c8fea2 SE |
529 | |
530 | for (i = 0; i < hash->size; i++) { | |
531 | head = &hash->table[i]; | |
532 | ||
fb778ea1 | 533 | rcu_read_lock(); |
7aadf889 | 534 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
7d211efc | 535 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f LL |
536 | if (!router) |
537 | continue; | |
538 | ||
acd34afa | 539 | if ((orig_node->flags & BATADV_VIS_SERVER) && |
e1a5382f LL |
540 | (router->tq_avg > best_tq)) { |
541 | best_tq = router->tq_avg; | |
c6c8fea2 SE |
542 | memcpy(packet->target_orig, orig_node->orig, |
543 | ETH_ALEN); | |
544 | } | |
7d211efc | 545 | batadv_neigh_node_free_ref(router); |
c6c8fea2 | 546 | } |
fb778ea1 | 547 | rcu_read_unlock(); |
c6c8fea2 SE |
548 | } |
549 | ||
550 | return best_tq; | |
551 | } | |
552 | ||
553 | /* Return true if the vis packet is full. */ | |
56303d34 | 554 | static bool batadv_vis_packet_full(const struct batadv_vis_info *info) |
c6c8fea2 | 555 | { |
96412690 | 556 | const struct batadv_vis_packet *packet; |
56303d34 | 557 | size_t num; |
347c80f0 | 558 | |
96412690 | 559 | packet = (struct batadv_vis_packet *)info->skb_packet->data; |
56303d34 | 560 | num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry); |
c6c8fea2 | 561 | |
56303d34 | 562 | if (num < packet->entries + 1) |
c6c8fea2 SE |
563 | return true; |
564 | return false; | |
565 | } | |
566 | ||
567 | /* generates a packet of own vis data, | |
9cfc7bd6 SE |
568 | * returns 0 on success, -1 if no packet could be generated |
569 | */ | |
56303d34 | 570 | static int batadv_generate_vis_packet(struct batadv_priv *bat_priv) |
c6c8fea2 | 571 | { |
5bf74e9c | 572 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
7aadf889 | 573 | struct hlist_node *node; |
c6c8fea2 | 574 | struct hlist_head *head; |
56303d34 SE |
575 | struct batadv_orig_node *orig_node; |
576 | struct batadv_neigh_node *router; | |
807736f6 | 577 | struct batadv_vis_info *info = bat_priv->vis.my_info; |
96412690 | 578 | struct batadv_vis_packet *packet; |
56303d34 SE |
579 | struct batadv_vis_info_entry *entry; |
580 | struct batadv_tt_common_entry *tt_common_entry; | |
c67893d1 | 581 | uint8_t *packet_pos; |
c90681b8 AQ |
582 | int best_tq = -1; |
583 | uint32_t i; | |
c6c8fea2 SE |
584 | |
585 | info->first_seen = jiffies; | |
96412690 | 586 | packet = (struct batadv_vis_packet *)info->skb_packet->data; |
c6c8fea2 SE |
587 | packet->vis_type = atomic_read(&bat_priv->vis_mode); |
588 | ||
3193e8fd | 589 | memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN); |
42d0b044 | 590 | packet->header.ttl = BATADV_TTL; |
c6c8fea2 SE |
591 | packet->seqno = htonl(ntohl(packet->seqno) + 1); |
592 | packet->entries = 0; | |
162d549c | 593 | packet->reserved = 0; |
704509b8 | 594 | skb_trim(info->skb_packet, sizeof(*packet)); |
c6c8fea2 | 595 | |
acd34afa | 596 | if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) { |
eaad8ad9 | 597 | best_tq = batadv_find_best_vis_server(bat_priv, info); |
c6c8fea2 | 598 | |
d0072609 | 599 | if (best_tq < 0) |
5346c35e | 600 | return best_tq; |
c6c8fea2 SE |
601 | } |
602 | ||
603 | for (i = 0; i < hash->size; i++) { | |
604 | head = &hash->table[i]; | |
605 | ||
fb778ea1 | 606 | rcu_read_lock(); |
7aadf889 | 607 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
7d211efc | 608 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f | 609 | if (!router) |
c6c8fea2 SE |
610 | continue; |
611 | ||
1eda58bf | 612 | if (!batadv_compare_eth(router->addr, orig_node->orig)) |
e1a5382f | 613 | goto next; |
c6c8fea2 | 614 | |
e9a4f295 | 615 | if (router->if_incoming->if_status != BATADV_IF_ACTIVE) |
e1a5382f | 616 | goto next; |
c6c8fea2 | 617 | |
e1a5382f LL |
618 | if (router->tq_avg < 1) |
619 | goto next; | |
c6c8fea2 SE |
620 | |
621 | /* fill one entry into buffer. */ | |
c67893d1 SE |
622 | packet_pos = skb_put(info->skb_packet, sizeof(*entry)); |
623 | entry = (struct batadv_vis_info_entry *)packet_pos; | |
c6c8fea2 | 624 | memcpy(entry->src, |
e1a5382f | 625 | router->if_incoming->net_dev->dev_addr, |
c6c8fea2 SE |
626 | ETH_ALEN); |
627 | memcpy(entry->dest, orig_node->orig, ETH_ALEN); | |
e1a5382f | 628 | entry->quality = router->tq_avg; |
c6c8fea2 SE |
629 | packet->entries++; |
630 | ||
e1a5382f | 631 | next: |
7d211efc | 632 | batadv_neigh_node_free_ref(router); |
e1a5382f | 633 | |
eaad8ad9 | 634 | if (batadv_vis_packet_full(info)) |
d0072609 | 635 | goto unlock; |
c6c8fea2 | 636 | } |
fb778ea1 | 637 | rcu_read_unlock(); |
c6c8fea2 SE |
638 | } |
639 | ||
807736f6 | 640 | hash = bat_priv->tt.local_hash; |
c6c8fea2 | 641 | |
c6c8fea2 SE |
642 | for (i = 0; i < hash->size; i++) { |
643 | head = &hash->table[i]; | |
644 | ||
7683fdc1 | 645 | rcu_read_lock(); |
48100bac | 646 | hlist_for_each_entry_rcu(tt_common_entry, node, head, |
7683fdc1 | 647 | hash_entry) { |
c67893d1 SE |
648 | packet_pos = skb_put(info->skb_packet, sizeof(*entry)); |
649 | entry = (struct batadv_vis_info_entry *)packet_pos; | |
c6c8fea2 | 650 | memset(entry->src, 0, ETH_ALEN); |
48100bac | 651 | memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN); |
2dafb49d | 652 | entry->quality = 0; /* 0 means TT */ |
c6c8fea2 SE |
653 | packet->entries++; |
654 | ||
eaad8ad9 | 655 | if (batadv_vis_packet_full(info)) |
7683fdc1 | 656 | goto unlock; |
c6c8fea2 | 657 | } |
7683fdc1 | 658 | rcu_read_unlock(); |
c6c8fea2 SE |
659 | } |
660 | ||
c6c8fea2 | 661 | return 0; |
d0072609 ML |
662 | |
663 | unlock: | |
664 | rcu_read_unlock(); | |
665 | return 0; | |
c6c8fea2 SE |
666 | } |
667 | ||
668 | /* free old vis packets. Must be called with this vis_hash_lock | |
9cfc7bd6 SE |
669 | * held |
670 | */ | |
56303d34 | 671 | static void batadv_purge_vis_packets(struct batadv_priv *bat_priv) |
c6c8fea2 | 672 | { |
c90681b8 | 673 | uint32_t i; |
807736f6 | 674 | struct batadv_hashtable *hash = bat_priv->vis.hash; |
7aadf889 | 675 | struct hlist_node *node, *node_tmp; |
c6c8fea2 | 676 | struct hlist_head *head; |
56303d34 | 677 | struct batadv_vis_info *info; |
c6c8fea2 SE |
678 | |
679 | for (i = 0; i < hash->size; i++) { | |
680 | head = &hash->table[i]; | |
681 | ||
7aadf889 ML |
682 | hlist_for_each_entry_safe(info, node, node_tmp, |
683 | head, hash_entry) { | |
c6c8fea2 | 684 | /* never purge own data. */ |
807736f6 | 685 | if (info == bat_priv->vis.my_info) |
c6c8fea2 SE |
686 | continue; |
687 | ||
1eda58bf | 688 | if (batadv_has_timed_out(info->first_seen, |
edbf7ff7 | 689 | BATADV_VIS_TIMEOUT)) { |
7aadf889 | 690 | hlist_del(node); |
eaad8ad9 SE |
691 | batadv_send_list_del(info); |
692 | kref_put(&info->refcount, batadv_free_info); | |
c6c8fea2 SE |
693 | } |
694 | } | |
695 | } | |
696 | } | |
697 | ||
56303d34 SE |
698 | static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv, |
699 | struct batadv_vis_info *info) | |
c6c8fea2 | 700 | { |
56303d34 | 701 | struct batadv_neigh_node *router; |
5bf74e9c | 702 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
7aadf889 | 703 | struct hlist_node *node; |
c6c8fea2 | 704 | struct hlist_head *head; |
56303d34 | 705 | struct batadv_orig_node *orig_node; |
96412690 | 706 | struct batadv_vis_packet *packet; |
c6c8fea2 | 707 | struct sk_buff *skb; |
56303d34 | 708 | struct batadv_hard_iface *hard_iface; |
c6c8fea2 | 709 | uint8_t dstaddr[ETH_ALEN]; |
c90681b8 | 710 | uint32_t i; |
c6c8fea2 SE |
711 | |
712 | ||
96412690 | 713 | packet = (struct batadv_vis_packet *)info->skb_packet->data; |
c6c8fea2 SE |
714 | |
715 | /* send to all routers in range. */ | |
716 | for (i = 0; i < hash->size; i++) { | |
717 | head = &hash->table[i]; | |
718 | ||
fb778ea1 | 719 | rcu_read_lock(); |
7aadf889 | 720 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
c6c8fea2 | 721 | /* if it's a vis server and reachable, send it. */ |
acd34afa | 722 | if (!(orig_node->flags & BATADV_VIS_SERVER)) |
c6c8fea2 | 723 | continue; |
e1a5382f | 724 | |
7d211efc | 725 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f LL |
726 | if (!router) |
727 | continue; | |
728 | ||
c6c8fea2 | 729 | /* don't send it if we already received the packet from |
9cfc7bd6 SE |
730 | * this node. |
731 | */ | |
eaad8ad9 SE |
732 | if (batadv_recv_list_is_in(bat_priv, &info->recv_list, |
733 | orig_node->orig)) { | |
7d211efc | 734 | batadv_neigh_node_free_ref(router); |
c6c8fea2 | 735 | continue; |
e1a5382f | 736 | } |
c6c8fea2 SE |
737 | |
738 | memcpy(packet->target_orig, orig_node->orig, ETH_ALEN); | |
e1a5382f LL |
739 | hard_iface = router->if_incoming; |
740 | memcpy(dstaddr, router->addr, ETH_ALEN); | |
741 | ||
7d211efc | 742 | batadv_neigh_node_free_ref(router); |
c6c8fea2 SE |
743 | |
744 | skb = skb_clone(info->skb_packet, GFP_ATOMIC); | |
745 | if (skb) | |
9455e34c SE |
746 | batadv_send_skb_packet(skb, hard_iface, |
747 | dstaddr); | |
c6c8fea2 | 748 | |
c6c8fea2 | 749 | } |
fb778ea1 | 750 | rcu_read_unlock(); |
c6c8fea2 | 751 | } |
c6c8fea2 SE |
752 | } |
753 | ||
56303d34 SE |
754 | static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv, |
755 | struct batadv_vis_info *info) | |
c6c8fea2 | 756 | { |
56303d34 SE |
757 | struct batadv_orig_node *orig_node; |
758 | struct batadv_neigh_node *router = NULL; | |
c6c8fea2 | 759 | struct sk_buff *skb; |
96412690 | 760 | struct batadv_vis_packet *packet; |
c6c8fea2 | 761 | |
96412690 | 762 | packet = (struct batadv_vis_packet *)info->skb_packet->data; |
c6c8fea2 | 763 | |
da641193 | 764 | orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig); |
44524fcd | 765 | if (!orig_node) |
e1a5382f | 766 | goto out; |
44524fcd | 767 | |
7d211efc | 768 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f LL |
769 | if (!router) |
770 | goto out; | |
c6c8fea2 SE |
771 | |
772 | skb = skb_clone(info->skb_packet, GFP_ATOMIC); | |
773 | if (skb) | |
9455e34c | 774 | batadv_send_skb_packet(skb, router->if_incoming, router->addr); |
c6c8fea2 SE |
775 | |
776 | out: | |
e1a5382f | 777 | if (router) |
7d211efc | 778 | batadv_neigh_node_free_ref(router); |
44524fcd | 779 | if (orig_node) |
7d211efc | 780 | batadv_orig_node_free_ref(orig_node); |
c6c8fea2 SE |
781 | } |
782 | ||
eaad8ad9 | 783 | /* only send one vis packet. called from batadv_send_vis_packets() */ |
56303d34 SE |
784 | static void batadv_send_vis_packet(struct batadv_priv *bat_priv, |
785 | struct batadv_vis_info *info) | |
c6c8fea2 | 786 | { |
56303d34 | 787 | struct batadv_hard_iface *primary_if; |
96412690 | 788 | struct batadv_vis_packet *packet; |
c6c8fea2 | 789 | |
e5d89254 | 790 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
791 | if (!primary_if) |
792 | goto out; | |
793 | ||
96412690 | 794 | packet = (struct batadv_vis_packet *)info->skb_packet->data; |
76543d14 | 795 | if (packet->header.ttl < 2) { |
c6c8fea2 | 796 | pr_debug("Error - can't send vis packet: ttl exceeded\n"); |
32ae9b22 | 797 | goto out; |
c6c8fea2 SE |
798 | } |
799 | ||
32ae9b22 | 800 | memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
76543d14 | 801 | packet->header.ttl--; |
c6c8fea2 SE |
802 | |
803 | if (is_broadcast_ether_addr(packet->target_orig)) | |
eaad8ad9 | 804 | batadv_broadcast_vis_packet(bat_priv, info); |
c6c8fea2 | 805 | else |
eaad8ad9 | 806 | batadv_unicast_vis_packet(bat_priv, info); |
76543d14 | 807 | packet->header.ttl++; /* restore TTL */ |
32ae9b22 ML |
808 | |
809 | out: | |
810 | if (primary_if) | |
e5d89254 | 811 | batadv_hardif_free_ref(primary_if); |
c6c8fea2 SE |
812 | } |
813 | ||
814 | /* called from timer; send (and maybe generate) vis packet. */ | |
eaad8ad9 | 815 | static void batadv_send_vis_packets(struct work_struct *work) |
c6c8fea2 | 816 | { |
bbb1f90e | 817 | struct delayed_work *delayed_work; |
56303d34 | 818 | struct batadv_priv *bat_priv; |
807736f6 | 819 | struct batadv_priv_vis *priv_vis; |
56303d34 | 820 | struct batadv_vis_info *info; |
c6c8fea2 | 821 | |
bbb1f90e | 822 | delayed_work = container_of(work, struct delayed_work, work); |
807736f6 SE |
823 | priv_vis = container_of(delayed_work, struct batadv_priv_vis, work); |
824 | bat_priv = container_of(priv_vis, struct batadv_priv, vis); | |
825 | spin_lock_bh(&bat_priv->vis.hash_lock); | |
eaad8ad9 | 826 | batadv_purge_vis_packets(bat_priv); |
c6c8fea2 | 827 | |
eaad8ad9 | 828 | if (batadv_generate_vis_packet(bat_priv) == 0) { |
c6c8fea2 | 829 | /* schedule if generation was successful */ |
807736f6 | 830 | batadv_send_list_add(bat_priv, bat_priv->vis.my_info); |
c6c8fea2 SE |
831 | } |
832 | ||
807736f6 SE |
833 | while (!list_empty(&bat_priv->vis.send_list)) { |
834 | info = list_first_entry(&bat_priv->vis.send_list, | |
1181e1da | 835 | typeof(*info), send_list); |
c6c8fea2 SE |
836 | |
837 | kref_get(&info->refcount); | |
807736f6 | 838 | spin_unlock_bh(&bat_priv->vis.hash_lock); |
c6c8fea2 | 839 | |
eaad8ad9 | 840 | batadv_send_vis_packet(bat_priv, info); |
c6c8fea2 | 841 | |
807736f6 | 842 | spin_lock_bh(&bat_priv->vis.hash_lock); |
eaad8ad9 SE |
843 | batadv_send_list_del(info); |
844 | kref_put(&info->refcount, batadv_free_info); | |
c6c8fea2 | 845 | } |
807736f6 | 846 | spin_unlock_bh(&bat_priv->vis.hash_lock); |
eaad8ad9 | 847 | batadv_start_vis_timer(bat_priv); |
c6c8fea2 SE |
848 | } |
849 | ||
850 | /* init the vis server. this may only be called when if_list is already | |
9cfc7bd6 SE |
851 | * initialized (e.g. bat0 is initialized, interfaces have been added) |
852 | */ | |
56303d34 | 853 | int batadv_vis_init(struct batadv_priv *bat_priv) |
c6c8fea2 | 854 | { |
96412690 | 855 | struct batadv_vis_packet *packet; |
c6c8fea2 | 856 | int hash_added; |
347c80f0 | 857 | unsigned int len; |
42d0b044 | 858 | unsigned long first_seen; |
96412690 | 859 | struct sk_buff *tmp_skb; |
c6c8fea2 | 860 | |
807736f6 | 861 | if (bat_priv->vis.hash) |
5346c35e | 862 | return 0; |
c6c8fea2 | 863 | |
807736f6 | 864 | spin_lock_bh(&bat_priv->vis.hash_lock); |
c6c8fea2 | 865 | |
807736f6 SE |
866 | bat_priv->vis.hash = batadv_hash_new(256); |
867 | if (!bat_priv->vis.hash) { | |
c6c8fea2 SE |
868 | pr_err("Can't initialize vis_hash\n"); |
869 | goto err; | |
870 | } | |
871 | ||
807736f6 SE |
872 | bat_priv->vis.my_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC); |
873 | if (!bat_priv->vis.my_info) | |
c6c8fea2 | 874 | goto err; |
c6c8fea2 | 875 | |
5b246574 SE |
876 | len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE; |
877 | len += ETH_HLEN + NET_IP_ALIGN; | |
807736f6 SE |
878 | bat_priv->vis.my_info->skb_packet = dev_alloc_skb(len); |
879 | if (!bat_priv->vis.my_info->skb_packet) | |
c6c8fea2 SE |
880 | goto free_info; |
881 | ||
5b246574 | 882 | skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN + NET_IP_ALIGN); |
807736f6 | 883 | tmp_skb = bat_priv->vis.my_info->skb_packet; |
96412690 | 884 | packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet)); |
c6c8fea2 SE |
885 | |
886 | /* prefill the vis info */ | |
42d0b044 | 887 | first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL); |
807736f6 SE |
888 | bat_priv->vis.my_info->first_seen = first_seen; |
889 | INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list); | |
890 | INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list); | |
891 | kref_init(&bat_priv->vis.my_info->refcount); | |
892 | bat_priv->vis.my_info->bat_priv = bat_priv; | |
7e071c79 | 893 | packet->header.version = BATADV_COMPAT_VERSION; |
acd34afa | 894 | packet->header.packet_type = BATADV_VIS; |
42d0b044 | 895 | packet->header.ttl = BATADV_TTL; |
c6c8fea2 | 896 | packet->seqno = 0; |
162d549c | 897 | packet->reserved = 0; |
c6c8fea2 SE |
898 | packet->entries = 0; |
899 | ||
807736f6 | 900 | INIT_LIST_HEAD(&bat_priv->vis.send_list); |
c6c8fea2 | 901 | |
807736f6 | 902 | hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp, |
eaad8ad9 | 903 | batadv_vis_info_choose, |
807736f6 SE |
904 | bat_priv->vis.my_info, |
905 | &bat_priv->vis.my_info->hash_entry); | |
1a1f37d9 | 906 | if (hash_added != 0) { |
c6c8fea2 SE |
907 | pr_err("Can't add own vis packet into hash\n"); |
908 | /* not in hash, need to remove it manually. */ | |
807736f6 | 909 | kref_put(&bat_priv->vis.my_info->refcount, batadv_free_info); |
c6c8fea2 SE |
910 | goto err; |
911 | } | |
912 | ||
807736f6 | 913 | spin_unlock_bh(&bat_priv->vis.hash_lock); |
eaad8ad9 | 914 | batadv_start_vis_timer(bat_priv); |
5346c35e | 915 | return 0; |
c6c8fea2 SE |
916 | |
917 | free_info: | |
807736f6 SE |
918 | kfree(bat_priv->vis.my_info); |
919 | bat_priv->vis.my_info = NULL; | |
c6c8fea2 | 920 | err: |
807736f6 | 921 | spin_unlock_bh(&bat_priv->vis.hash_lock); |
d0f714f4 | 922 | batadv_vis_quit(bat_priv); |
5346c35e | 923 | return -ENOMEM; |
c6c8fea2 SE |
924 | } |
925 | ||
926 | /* Decrease the reference count on a hash item info */ | |
eaad8ad9 | 927 | static void batadv_free_info_ref(struct hlist_node *node, void *arg) |
c6c8fea2 | 928 | { |
56303d34 | 929 | struct batadv_vis_info *info; |
c6c8fea2 | 930 | |
56303d34 | 931 | info = container_of(node, struct batadv_vis_info, hash_entry); |
eaad8ad9 SE |
932 | batadv_send_list_del(info); |
933 | kref_put(&info->refcount, batadv_free_info); | |
c6c8fea2 SE |
934 | } |
935 | ||
936 | /* shutdown vis-server */ | |
56303d34 | 937 | void batadv_vis_quit(struct batadv_priv *bat_priv) |
c6c8fea2 | 938 | { |
807736f6 | 939 | if (!bat_priv->vis.hash) |
c6c8fea2 SE |
940 | return; |
941 | ||
807736f6 | 942 | cancel_delayed_work_sync(&bat_priv->vis.work); |
c6c8fea2 | 943 | |
807736f6 | 944 | spin_lock_bh(&bat_priv->vis.hash_lock); |
c6c8fea2 | 945 | /* properly remove, kill timers ... */ |
807736f6 SE |
946 | batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL); |
947 | bat_priv->vis.hash = NULL; | |
948 | bat_priv->vis.my_info = NULL; | |
949 | spin_unlock_bh(&bat_priv->vis.hash_lock); | |
c6c8fea2 SE |
950 | } |
951 | ||
952 | /* schedule packets for (re)transmission */ | |
56303d34 | 953 | static void batadv_start_vis_timer(struct batadv_priv *bat_priv) |
c6c8fea2 | 954 | { |
807736f6 SE |
955 | INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets); |
956 | queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work, | |
42d0b044 | 957 | msecs_to_jiffies(BATADV_VIS_INTERVAL)); |
c6c8fea2 | 958 | } |