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