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