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