Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 567db7b | 2012-01-01 00:41:38 +0100 | [diff] [blame] | 2 | * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Marek Lindner, 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 "translation-table.h" |
| 24 | #include "soft-interface.h" |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 25 | #include "hard-interface.h" |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 26 | #include "send.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 27 | #include "hash.h" |
| 28 | #include "originator.h" |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 29 | #include "routing.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 30 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 31 | #include <linux/crc16.h> |
| 32 | |
Sven Eckelmann | de7aae6 | 2012-02-05 18:55:22 +0100 | [diff] [blame] | 33 | static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client, |
| 34 | struct orig_node *orig_node); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 35 | static void tt_purge(struct work_struct *work); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 36 | static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 37 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 38 | /* returns 1 if they are the same mac addr */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 39 | static int compare_tt(const struct hlist_node *node, const void *data2) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 40 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 41 | const void *data1 = container_of(node, struct tt_common_entry, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 42 | hash_entry); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 43 | |
| 44 | return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); |
| 45 | } |
| 46 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 47 | static void tt_start_timer(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 48 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 49 | INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge); |
| 50 | queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work, |
| 51 | msecs_to_jiffies(5000)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 54 | static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash, |
| 55 | const void *data) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 56 | { |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 57 | struct hlist_head *head; |
| 58 | struct hlist_node *node; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 59 | struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 60 | uint32_t index; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 61 | |
| 62 | if (!hash) |
| 63 | return NULL; |
| 64 | |
| 65 | index = choose_orig(data, hash->size); |
| 66 | head = &hash->table[index]; |
| 67 | |
| 68 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 69 | hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) { |
| 70 | if (!compare_eth(tt_common_entry, data)) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 71 | continue; |
| 72 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 73 | if (!atomic_inc_not_zero(&tt_common_entry->refcount)) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 74 | continue; |
| 75 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 76 | tt_common_entry_tmp = tt_common_entry; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | rcu_read_unlock(); |
| 80 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 81 | return tt_common_entry_tmp; |
| 82 | } |
| 83 | |
| 84 | static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv, |
| 85 | const void *data) |
| 86 | { |
| 87 | struct tt_common_entry *tt_common_entry; |
| 88 | struct tt_local_entry *tt_local_entry = NULL; |
| 89 | |
| 90 | tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data); |
| 91 | if (tt_common_entry) |
| 92 | tt_local_entry = container_of(tt_common_entry, |
| 93 | struct tt_local_entry, common); |
| 94 | return tt_local_entry; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 97 | static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 98 | const void *data) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 99 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 100 | struct tt_common_entry *tt_common_entry; |
| 101 | struct tt_global_entry *tt_global_entry = NULL; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 102 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 103 | tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data); |
| 104 | if (tt_common_entry) |
| 105 | tt_global_entry = container_of(tt_common_entry, |
| 106 | struct tt_global_entry, common); |
| 107 | return tt_global_entry; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 108 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 111 | static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry) |
| 112 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 113 | if (atomic_dec_and_test(&tt_local_entry->common.refcount)) |
| 114 | kfree_rcu(tt_local_entry, common.rcu); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Simon Wunderlich | 531027f | 2011-10-19 11:02:25 +0200 | [diff] [blame] | 117 | static void tt_global_entry_free_rcu(struct rcu_head *rcu) |
| 118 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 119 | struct tt_common_entry *tt_common_entry; |
Simon Wunderlich | 531027f | 2011-10-19 11:02:25 +0200 | [diff] [blame] | 120 | struct tt_global_entry *tt_global_entry; |
| 121 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 122 | tt_common_entry = container_of(rcu, struct tt_common_entry, rcu); |
| 123 | tt_global_entry = container_of(tt_common_entry, struct tt_global_entry, |
| 124 | common); |
Simon Wunderlich | 531027f | 2011-10-19 11:02:25 +0200 | [diff] [blame] | 125 | |
Simon Wunderlich | 531027f | 2011-10-19 11:02:25 +0200 | [diff] [blame] | 126 | kfree(tt_global_entry); |
| 127 | } |
| 128 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 129 | static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry) |
| 130 | { |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 131 | if (atomic_dec_and_test(&tt_global_entry->common.refcount)) { |
| 132 | tt_global_del_orig_list(tt_global_entry); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 133 | call_rcu(&tt_global_entry->common.rcu, |
| 134 | tt_global_entry_free_rcu); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 135 | } |
| 136 | } |
| 137 | |
| 138 | static void tt_orig_list_entry_free_rcu(struct rcu_head *rcu) |
| 139 | { |
| 140 | struct tt_orig_list_entry *orig_entry; |
| 141 | |
| 142 | orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu); |
| 143 | atomic_dec(&orig_entry->orig_node->tt_size); |
| 144 | orig_node_free_ref(orig_entry->orig_node); |
| 145 | kfree(orig_entry); |
| 146 | } |
| 147 | |
| 148 | static void tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry) |
| 149 | { |
| 150 | call_rcu(&orig_entry->rcu, tt_orig_list_entry_free_rcu); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Antonio Quartulli | ff66c97 | 2011-06-30 01:14:00 +0200 | [diff] [blame] | 153 | static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr, |
| 154 | uint8_t flags) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 155 | { |
| 156 | struct tt_change_node *tt_change_node; |
| 157 | |
| 158 | tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC); |
| 159 | |
| 160 | if (!tt_change_node) |
| 161 | return; |
| 162 | |
Antonio Quartulli | ff66c97 | 2011-06-30 01:14:00 +0200 | [diff] [blame] | 163 | tt_change_node->change.flags = flags; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 164 | memcpy(tt_change_node->change.addr, addr, ETH_ALEN); |
| 165 | |
| 166 | spin_lock_bh(&bat_priv->tt_changes_list_lock); |
| 167 | /* track the change in the OGMinterval list */ |
| 168 | list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list); |
| 169 | atomic_inc(&bat_priv->tt_local_changes); |
| 170 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); |
| 171 | |
| 172 | atomic_set(&bat_priv->tt_ogm_append_cnt, 0); |
| 173 | } |
| 174 | |
| 175 | int tt_len(int changes_num) |
| 176 | { |
| 177 | return changes_num * sizeof(struct tt_change); |
| 178 | } |
| 179 | |
| 180 | static int tt_local_init(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 181 | { |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 182 | if (bat_priv->tt_local_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 183 | return 1; |
| 184 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 185 | bat_priv->tt_local_hash = hash_new(1024); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 186 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 187 | if (!bat_priv->tt_local_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 190 | return 1; |
| 191 | } |
| 192 | |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 193 | void tt_local_add(struct net_device *soft_iface, const uint8_t *addr, |
| 194 | int ifindex) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 195 | { |
| 196 | struct bat_priv *bat_priv = netdev_priv(soft_iface); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 197 | struct tt_local_entry *tt_local_entry = NULL; |
| 198 | struct tt_global_entry *tt_global_entry = NULL; |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 199 | struct hlist_head *head; |
| 200 | struct hlist_node *node; |
| 201 | struct tt_orig_list_entry *orig_entry; |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 202 | int hash_added; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 203 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 204 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 205 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 206 | if (tt_local_entry) { |
| 207 | tt_local_entry->last_seen = jiffies; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 208 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 211 | tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 212 | if (!tt_local_entry) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 213 | goto out; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 214 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 215 | bat_dbg(DBG_TT, bat_priv, |
| 216 | "Creating new local tt entry: %pM (ttvn: %d)\n", addr, |
| 217 | (uint8_t)atomic_read(&bat_priv->ttvn)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 218 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 219 | memcpy(tt_local_entry->common.addr, addr, ETH_ALEN); |
| 220 | tt_local_entry->common.flags = NO_FLAGS; |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 221 | if (is_wifi_iface(ifindex)) |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 222 | tt_local_entry->common.flags |= TT_CLIENT_WIFI; |
| 223 | atomic_set(&tt_local_entry->common.refcount, 2); |
| 224 | tt_local_entry->last_seen = jiffies; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 225 | |
| 226 | /* the batman interface mac address should never be purged */ |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 227 | if (compare_eth(addr, soft_iface->dev_addr)) |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 228 | tt_local_entry->common.flags |= TT_CLIENT_NOPURGE; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 229 | |
Antonio Quartulli | c40ed2b | 2012-01-06 21:31:33 +0100 | [diff] [blame] | 230 | /* The local entry has to be marked as NEW to avoid to send it in |
| 231 | * a full table response going out before the next ttvn increment |
| 232 | * (consistency check) */ |
| 233 | tt_local_entry->common.flags |= TT_CLIENT_NEW; |
| 234 | |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 235 | hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig, |
| 236 | &tt_local_entry->common, |
| 237 | &tt_local_entry->common.hash_entry); |
| 238 | |
| 239 | if (unlikely(hash_added != 0)) { |
| 240 | /* remove the reference for the hash */ |
| 241 | tt_local_entry_free_ref(tt_local_entry); |
| 242 | goto out; |
| 243 | } |
| 244 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 245 | tt_local_event(bat_priv, addr, tt_local_entry->common.flags); |
Antonio Quartulli | ff66c97 | 2011-06-30 01:14:00 +0200 | [diff] [blame] | 246 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 247 | /* remove address from global hash if present */ |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 248 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 249 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 250 | /* Check whether it is a roaming! */ |
| 251 | if (tt_global_entry) { |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 252 | /* These node are probably going to update their tt table */ |
| 253 | head = &tt_global_entry->orig_list; |
| 254 | rcu_read_lock(); |
| 255 | hlist_for_each_entry_rcu(orig_entry, node, head, list) { |
| 256 | orig_entry->orig_node->tt_poss_change = true; |
| 257 | |
| 258 | send_roam_adv(bat_priv, tt_global_entry->common.addr, |
| 259 | orig_entry->orig_node); |
| 260 | } |
| 261 | rcu_read_unlock(); |
| 262 | /* The global entry has to be marked as ROAMING and |
| 263 | * has to be kept for consistency purpose |
| 264 | */ |
David S. Miller | 220b07e | 2011-12-16 15:07:28 -0500 | [diff] [blame] | 265 | tt_global_entry->common.flags |= TT_CLIENT_ROAM; |
Antonio Quartulli | 03fc307 | 2011-12-04 12:26:50 +0100 | [diff] [blame] | 266 | tt_global_entry->roam_at = jiffies; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 267 | } |
| 268 | out: |
| 269 | if (tt_local_entry) |
| 270 | tt_local_entry_free_ref(tt_local_entry); |
| 271 | if (tt_global_entry) |
| 272 | tt_global_entry_free_ref(tt_global_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 275 | int tt_changes_fill_buffer(struct bat_priv *bat_priv, |
| 276 | unsigned char *buff, int buff_len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 277 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 278 | int count = 0, tot_changes = 0; |
| 279 | struct tt_change_node *entry, *safe; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 280 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 281 | if (buff_len > 0) |
| 282 | tot_changes = buff_len / tt_len(1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 283 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 284 | spin_lock_bh(&bat_priv->tt_changes_list_lock); |
| 285 | atomic_set(&bat_priv->tt_local_changes, 0); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 286 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 287 | list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list, |
Sven Eckelmann | 7c64fd9 | 2012-02-28 10:55:36 +0100 | [diff] [blame] | 288 | list) { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 289 | if (count < tot_changes) { |
| 290 | memcpy(buff + tt_len(count), |
| 291 | &entry->change, sizeof(struct tt_change)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 292 | count++; |
| 293 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 294 | list_del(&entry->list); |
| 295 | kfree(entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 296 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 297 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 298 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 299 | /* Keep the buffer for possible tt_request */ |
| 300 | spin_lock_bh(&bat_priv->tt_buff_lock); |
| 301 | kfree(bat_priv->tt_buff); |
| 302 | bat_priv->tt_buff_len = 0; |
| 303 | bat_priv->tt_buff = NULL; |
| 304 | /* We check whether this new OGM has no changes due to size |
| 305 | * problems */ |
| 306 | if (buff_len > 0) { |
| 307 | /** |
| 308 | * if kmalloc() fails we will reply with the full table |
| 309 | * instead of providing the diff |
| 310 | */ |
| 311 | bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC); |
| 312 | if (bat_priv->tt_buff) { |
| 313 | memcpy(bat_priv->tt_buff, buff, buff_len); |
| 314 | bat_priv->tt_buff_len = buff_len; |
| 315 | } |
| 316 | } |
| 317 | spin_unlock_bh(&bat_priv->tt_buff_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 318 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 319 | return tot_changes; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 322 | int tt_local_seq_print_text(struct seq_file *seq, void *offset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 323 | { |
| 324 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 325 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 326 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 327 | struct tt_common_entry *tt_common_entry; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 328 | struct hard_iface *primary_if; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 329 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 330 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 331 | uint32_t i; |
| 332 | int ret = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 333 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 334 | primary_if = primary_if_get_selected(bat_priv); |
| 335 | if (!primary_if) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 336 | ret = seq_printf(seq, |
| 337 | "BATMAN mesh %s disabled - please specify interfaces to enable it\n", |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 338 | net_dev->name); |
| 339 | goto out; |
| 340 | } |
| 341 | |
| 342 | if (primary_if->if_status != IF_ACTIVE) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 343 | ret = seq_printf(seq, |
| 344 | "BATMAN mesh %s disabled - primary interface not active\n", |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 345 | net_dev->name); |
| 346 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 349 | seq_printf(seq, |
| 350 | "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n", |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 351 | net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 352 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 353 | for (i = 0; i < hash->size; i++) { |
| 354 | head = &hash->table[i]; |
| 355 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 356 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 357 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 358 | head, hash_entry) { |
Simon Wunderlich | d099c2c | 2011-10-22 18:15:26 +0200 | [diff] [blame] | 359 | seq_printf(seq, " * %pM [%c%c%c%c%c]\n", |
Sven Eckelmann | 7c64fd9 | 2012-02-28 10:55:36 +0100 | [diff] [blame] | 360 | tt_common_entry->addr, |
| 361 | (tt_common_entry->flags & |
| 362 | TT_CLIENT_ROAM ? 'R' : '.'), |
| 363 | (tt_common_entry->flags & |
| 364 | TT_CLIENT_NOPURGE ? 'P' : '.'), |
| 365 | (tt_common_entry->flags & |
| 366 | TT_CLIENT_NEW ? 'N' : '.'), |
| 367 | (tt_common_entry->flags & |
| 368 | TT_CLIENT_PENDING ? 'X' : '.'), |
| 369 | (tt_common_entry->flags & |
| 370 | TT_CLIENT_WIFI ? 'W' : '.')); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 371 | } |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 372 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 373 | } |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 374 | out: |
| 375 | if (primary_if) |
| 376 | hardif_free_ref(primary_if); |
| 377 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 380 | static void tt_local_set_pending(struct bat_priv *bat_priv, |
| 381 | struct tt_local_entry *tt_local_entry, |
Antonio Quartulli | c566dbb | 2012-01-06 21:31:34 +0100 | [diff] [blame] | 382 | uint16_t flags, const char *message) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 383 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 384 | tt_local_event(bat_priv, tt_local_entry->common.addr, |
| 385 | tt_local_entry->common.flags | flags); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 386 | |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 387 | /* The local client has to be marked as "pending to be removed" but has |
| 388 | * to be kept in the table in order to send it in a full table |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 389 | * response issued before the net ttvn increment (consistency check) */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 390 | tt_local_entry->common.flags |= TT_CLIENT_PENDING; |
Antonio Quartulli | c566dbb | 2012-01-06 21:31:34 +0100 | [diff] [blame] | 391 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 392 | bat_dbg(DBG_TT, bat_priv, |
| 393 | "Local tt entry (%pM) pending to be removed: %s\n", |
| 394 | tt_local_entry->common.addr, message); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 397 | void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr, |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 398 | const char *message, bool roaming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 399 | { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 400 | struct tt_local_entry *tt_local_entry = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 401 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 402 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 403 | if (!tt_local_entry) |
| 404 | goto out; |
| 405 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 406 | tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL | |
Antonio Quartulli | c566dbb | 2012-01-06 21:31:34 +0100 | [diff] [blame] | 407 | (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 408 | out: |
| 409 | if (tt_local_entry) |
| 410 | tt_local_entry_free_ref(tt_local_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 413 | static void tt_local_purge(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 414 | { |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 415 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
| 416 | struct tt_local_entry *tt_local_entry; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 417 | struct tt_common_entry *tt_common_entry; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 418 | struct hlist_node *node, *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 419 | struct hlist_head *head; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 420 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 421 | uint32_t i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 422 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 423 | for (i = 0; i < hash->size; i++) { |
| 424 | head = &hash->table[i]; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 425 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 426 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 427 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 428 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 429 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 430 | tt_local_entry = container_of(tt_common_entry, |
| 431 | struct tt_local_entry, |
| 432 | common); |
| 433 | if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 434 | continue; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 435 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 436 | /* entry already marked for deletion */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 437 | if (tt_local_entry->common.flags & TT_CLIENT_PENDING) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 438 | continue; |
| 439 | |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame] | 440 | if (!has_timed_out(tt_local_entry->last_seen, |
Marek Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 441 | TT_LOCAL_TIMEOUT)) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 442 | continue; |
| 443 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 444 | tt_local_set_pending(bat_priv, tt_local_entry, |
Antonio Quartulli | c566dbb | 2012-01-06 21:31:34 +0100 | [diff] [blame] | 445 | TT_CLIENT_DEL, "timed out"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 446 | } |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 447 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 452 | static void tt_local_table_free(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 453 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 454 | struct hashtable_t *hash; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 455 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 456 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 457 | struct tt_local_entry *tt_local_entry; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 458 | struct hlist_node *node, *node_tmp; |
| 459 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 460 | uint32_t i; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 461 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 462 | if (!bat_priv->tt_local_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 463 | return; |
| 464 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 465 | hash = bat_priv->tt_local_hash; |
| 466 | |
| 467 | for (i = 0; i < hash->size; i++) { |
| 468 | head = &hash->table[i]; |
| 469 | list_lock = &hash->list_locks[i]; |
| 470 | |
| 471 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 472 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 473 | head, hash_entry) { |
| 474 | hlist_del_rcu(node); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 475 | tt_local_entry = container_of(tt_common_entry, |
| 476 | struct tt_local_entry, |
| 477 | common); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 478 | tt_local_entry_free_ref(tt_local_entry); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 479 | } |
| 480 | spin_unlock_bh(list_lock); |
| 481 | } |
| 482 | |
| 483 | hash_destroy(hash); |
| 484 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 485 | bat_priv->tt_local_hash = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 488 | static int tt_global_init(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 489 | { |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 490 | if (bat_priv->tt_global_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 491 | return 1; |
| 492 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 493 | bat_priv->tt_global_hash = hash_new(1024); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 494 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 495 | if (!bat_priv->tt_global_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 496 | return 0; |
| 497 | |
| 498 | return 1; |
| 499 | } |
| 500 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 501 | static void tt_changes_list_free(struct bat_priv *bat_priv) |
| 502 | { |
| 503 | struct tt_change_node *entry, *safe; |
| 504 | |
| 505 | spin_lock_bh(&bat_priv->tt_changes_list_lock); |
| 506 | |
| 507 | list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list, |
| 508 | list) { |
| 509 | list_del(&entry->list); |
| 510 | kfree(entry); |
| 511 | } |
| 512 | |
| 513 | atomic_set(&bat_priv->tt_local_changes, 0); |
| 514 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); |
| 515 | } |
| 516 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 517 | /* find out if an orig_node is already in the list of a tt_global_entry. |
| 518 | * returns 1 if found, 0 otherwise |
| 519 | */ |
| 520 | static bool tt_global_entry_has_orig(const struct tt_global_entry *entry, |
| 521 | const struct orig_node *orig_node) |
| 522 | { |
| 523 | struct tt_orig_list_entry *tmp_orig_entry; |
| 524 | const struct hlist_head *head; |
| 525 | struct hlist_node *node; |
| 526 | bool found = false; |
| 527 | |
| 528 | rcu_read_lock(); |
| 529 | head = &entry->orig_list; |
| 530 | hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) { |
| 531 | if (tmp_orig_entry->orig_node == orig_node) { |
| 532 | found = true; |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | rcu_read_unlock(); |
| 537 | return found; |
| 538 | } |
| 539 | |
| 540 | static void tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry, |
| 541 | struct orig_node *orig_node, |
| 542 | int ttvn) |
| 543 | { |
| 544 | struct tt_orig_list_entry *orig_entry; |
| 545 | |
| 546 | orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC); |
| 547 | if (!orig_entry) |
| 548 | return; |
| 549 | |
| 550 | INIT_HLIST_NODE(&orig_entry->list); |
| 551 | atomic_inc(&orig_node->refcount); |
| 552 | atomic_inc(&orig_node->tt_size); |
| 553 | orig_entry->orig_node = orig_node; |
| 554 | orig_entry->ttvn = ttvn; |
| 555 | |
| 556 | spin_lock_bh(&tt_global_entry->list_lock); |
| 557 | hlist_add_head_rcu(&orig_entry->list, |
| 558 | &tt_global_entry->orig_list); |
| 559 | spin_unlock_bh(&tt_global_entry->list_lock); |
| 560 | } |
| 561 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 562 | /* caller must hold orig_node refcount */ |
| 563 | int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node, |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 564 | const unsigned char *tt_addr, uint8_t ttvn, bool roaming, |
| 565 | bool wifi) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 566 | { |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 567 | struct tt_global_entry *tt_global_entry = NULL; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 568 | int ret = 0; |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 569 | int hash_added; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 570 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 571 | tt_global_entry = tt_global_hash_find(bat_priv, tt_addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 572 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 573 | if (!tt_global_entry) { |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 574 | tt_global_entry = kzalloc(sizeof(*tt_global_entry), |
| 575 | GFP_ATOMIC); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 576 | if (!tt_global_entry) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 577 | goto out; |
| 578 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 579 | memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 580 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 581 | tt_global_entry->common.flags = NO_FLAGS; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 582 | tt_global_entry->roam_at = 0; |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 583 | atomic_set(&tt_global_entry->common.refcount, 2); |
| 584 | |
| 585 | INIT_HLIST_HEAD(&tt_global_entry->orig_list); |
| 586 | spin_lock_init(&tt_global_entry->list_lock); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 587 | |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 588 | hash_added = hash_add(bat_priv->tt_global_hash, compare_tt, |
| 589 | choose_orig, &tt_global_entry->common, |
| 590 | &tt_global_entry->common.hash_entry); |
| 591 | |
| 592 | if (unlikely(hash_added != 0)) { |
| 593 | /* remove the reference for the hash */ |
| 594 | tt_global_entry_free_ref(tt_global_entry); |
| 595 | goto out_remove; |
| 596 | } |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 597 | |
| 598 | tt_global_add_orig_entry(tt_global_entry, orig_node, ttvn); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 599 | } else { |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 600 | /* there is already a global entry, use this one. */ |
| 601 | |
| 602 | /* If there is the TT_CLIENT_ROAM flag set, there is only one |
| 603 | * originator left in the list and we previously received a |
| 604 | * delete + roaming change for this originator. |
| 605 | * |
| 606 | * We should first delete the old originator before adding the |
| 607 | * new one. |
| 608 | */ |
| 609 | if (tt_global_entry->common.flags & TT_CLIENT_ROAM) { |
| 610 | tt_global_del_orig_list(tt_global_entry); |
| 611 | tt_global_entry->common.flags &= ~TT_CLIENT_ROAM; |
| 612 | tt_global_entry->roam_at = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 613 | } |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 614 | |
| 615 | if (!tt_global_entry_has_orig(tt_global_entry, orig_node)) |
| 616 | tt_global_add_orig_entry(tt_global_entry, orig_node, |
| 617 | ttvn); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 618 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 619 | |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 620 | if (wifi) |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 621 | tt_global_entry->common.flags |= TT_CLIENT_WIFI; |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 622 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 623 | bat_dbg(DBG_TT, bat_priv, |
| 624 | "Creating new global tt entry: %pM (via %pM)\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 625 | tt_global_entry->common.addr, orig_node->orig); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 626 | |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 627 | out_remove: |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 628 | /* remove address from local hash if present */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 629 | tt_local_remove(bat_priv, tt_global_entry->common.addr, |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 630 | "global tt received", roaming); |
| 631 | ret = 1; |
| 632 | out: |
| 633 | if (tt_global_entry) |
| 634 | tt_global_entry_free_ref(tt_global_entry); |
| 635 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 638 | /* print all orig nodes who announce the address for this global entry. |
| 639 | * it is assumed that the caller holds rcu_read_lock(); |
| 640 | */ |
| 641 | static void tt_global_print_entry(struct tt_global_entry *tt_global_entry, |
| 642 | struct seq_file *seq) |
| 643 | { |
| 644 | struct hlist_head *head; |
| 645 | struct hlist_node *node; |
| 646 | struct tt_orig_list_entry *orig_entry; |
| 647 | struct tt_common_entry *tt_common_entry; |
| 648 | uint16_t flags; |
| 649 | uint8_t last_ttvn; |
| 650 | |
| 651 | tt_common_entry = &tt_global_entry->common; |
| 652 | |
| 653 | head = &tt_global_entry->orig_list; |
| 654 | |
| 655 | hlist_for_each_entry_rcu(orig_entry, node, head, list) { |
| 656 | flags = tt_common_entry->flags; |
| 657 | last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn); |
| 658 | seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n", |
| 659 | tt_global_entry->common.addr, orig_entry->ttvn, |
| 660 | orig_entry->orig_node->orig, last_ttvn, |
| 661 | (flags & TT_CLIENT_ROAM ? 'R' : '.'), |
| 662 | (flags & TT_CLIENT_WIFI ? 'W' : '.')); |
| 663 | } |
| 664 | } |
| 665 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 666 | int tt_global_seq_print_text(struct seq_file *seq, void *offset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 667 | { |
| 668 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 669 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 670 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 671 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 672 | struct tt_global_entry *tt_global_entry; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 673 | struct hard_iface *primary_if; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 674 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 675 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 676 | uint32_t i; |
| 677 | int ret = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 678 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 679 | primary_if = primary_if_get_selected(bat_priv); |
| 680 | if (!primary_if) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 681 | ret = seq_printf(seq, |
| 682 | "BATMAN mesh %s disabled - please specify interfaces to enable it\n", |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 683 | net_dev->name); |
| 684 | goto out; |
| 685 | } |
| 686 | |
| 687 | if (primary_if->if_status != IF_ACTIVE) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 688 | ret = seq_printf(seq, |
| 689 | "BATMAN mesh %s disabled - primary interface not active\n", |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 690 | net_dev->name); |
| 691 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 694 | seq_printf(seq, |
| 695 | "Globally announced TT entries received via the mesh %s\n", |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 696 | net_dev->name); |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 697 | seq_printf(seq, " %-13s %s %-15s %s %s\n", |
| 698 | "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 699 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 700 | for (i = 0; i < hash->size; i++) { |
| 701 | head = &hash->table[i]; |
| 702 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 703 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 704 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 705 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 706 | tt_global_entry = container_of(tt_common_entry, |
| 707 | struct tt_global_entry, |
| 708 | common); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 709 | tt_global_print_entry(tt_global_entry, seq); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 710 | } |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 711 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 712 | } |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 713 | out: |
| 714 | if (primary_if) |
| 715 | hardif_free_ref(primary_if); |
| 716 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 719 | /* deletes the orig list of a tt_global_entry */ |
| 720 | static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 721 | { |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 722 | struct hlist_head *head; |
| 723 | struct hlist_node *node, *safe; |
| 724 | struct tt_orig_list_entry *orig_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 725 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 726 | spin_lock_bh(&tt_global_entry->list_lock); |
| 727 | head = &tt_global_entry->orig_list; |
| 728 | hlist_for_each_entry_safe(orig_entry, node, safe, head, list) { |
| 729 | hlist_del_rcu(node); |
| 730 | tt_orig_list_entry_free_ref(orig_entry); |
| 731 | } |
| 732 | spin_unlock_bh(&tt_global_entry->list_lock); |
| 733 | |
| 734 | } |
| 735 | |
| 736 | static void tt_global_del_orig_entry(struct bat_priv *bat_priv, |
| 737 | struct tt_global_entry *tt_global_entry, |
| 738 | struct orig_node *orig_node, |
| 739 | const char *message) |
| 740 | { |
| 741 | struct hlist_head *head; |
| 742 | struct hlist_node *node, *safe; |
| 743 | struct tt_orig_list_entry *orig_entry; |
| 744 | |
| 745 | spin_lock_bh(&tt_global_entry->list_lock); |
| 746 | head = &tt_global_entry->orig_list; |
| 747 | hlist_for_each_entry_safe(orig_entry, node, safe, head, list) { |
| 748 | if (orig_entry->orig_node == orig_node) { |
| 749 | bat_dbg(DBG_TT, bat_priv, |
| 750 | "Deleting %pM from global tt entry %pM: %s\n", |
| 751 | orig_node->orig, tt_global_entry->common.addr, |
| 752 | message); |
| 753 | hlist_del_rcu(node); |
| 754 | tt_orig_list_entry_free_ref(orig_entry); |
| 755 | } |
| 756 | } |
| 757 | spin_unlock_bh(&tt_global_entry->list_lock); |
| 758 | } |
| 759 | |
| 760 | static void tt_global_del_struct(struct bat_priv *bat_priv, |
| 761 | struct tt_global_entry *tt_global_entry, |
| 762 | const char *message) |
| 763 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 764 | bat_dbg(DBG_TT, bat_priv, |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 765 | "Deleting global tt entry %pM: %s\n", |
| 766 | tt_global_entry->common.addr, message); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 767 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 768 | hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig, |
| 769 | tt_global_entry->common.addr); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 770 | tt_global_entry_free_ref(tt_global_entry); |
| 771 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 774 | /* If the client is to be deleted, we check if it is the last origantor entry |
| 775 | * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer, |
| 776 | * otherwise we simply remove the originator scheduled for deletion. |
| 777 | */ |
| 778 | static void tt_global_del_roaming(struct bat_priv *bat_priv, |
| 779 | struct tt_global_entry *tt_global_entry, |
| 780 | struct orig_node *orig_node, |
| 781 | const char *message) |
| 782 | { |
| 783 | bool last_entry = true; |
| 784 | struct hlist_head *head; |
| 785 | struct hlist_node *node; |
| 786 | struct tt_orig_list_entry *orig_entry; |
| 787 | |
| 788 | /* no local entry exists, case 1: |
| 789 | * Check if this is the last one or if other entries exist. |
| 790 | */ |
| 791 | |
| 792 | rcu_read_lock(); |
| 793 | head = &tt_global_entry->orig_list; |
| 794 | hlist_for_each_entry_rcu(orig_entry, node, head, list) { |
| 795 | if (orig_entry->orig_node != orig_node) { |
| 796 | last_entry = false; |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | rcu_read_unlock(); |
| 801 | |
| 802 | if (last_entry) { |
| 803 | /* its the last one, mark for roaming. */ |
| 804 | tt_global_entry->common.flags |= TT_CLIENT_ROAM; |
| 805 | tt_global_entry->roam_at = jiffies; |
| 806 | } else |
| 807 | /* there is another entry, we can simply delete this |
| 808 | * one and can still use the other one. |
| 809 | */ |
| 810 | tt_global_del_orig_entry(bat_priv, tt_global_entry, |
| 811 | orig_node, message); |
| 812 | } |
| 813 | |
| 814 | |
| 815 | |
Sven Eckelmann | de7aae6 | 2012-02-05 18:55:22 +0100 | [diff] [blame] | 816 | static void tt_global_del(struct bat_priv *bat_priv, |
| 817 | struct orig_node *orig_node, |
| 818 | const unsigned char *addr, |
| 819 | const char *message, bool roaming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 820 | { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 821 | struct tt_global_entry *tt_global_entry = NULL; |
Antonio Quartulli | 797399b | 2011-12-04 22:38:27 +0100 | [diff] [blame] | 822 | struct tt_local_entry *tt_local_entry = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 823 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 824 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 825 | if (!tt_global_entry) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 826 | goto out; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 827 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 828 | if (!roaming) { |
| 829 | tt_global_del_orig_entry(bat_priv, tt_global_entry, orig_node, |
| 830 | message); |
Sven Eckelmann | 92f90f5 | 2011-12-22 20:31:12 +0800 | [diff] [blame] | 831 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 832 | if (hlist_empty(&tt_global_entry->orig_list)) |
| 833 | tt_global_del_struct(bat_priv, tt_global_entry, |
| 834 | message); |
| 835 | |
Sven Eckelmann | 92f90f5 | 2011-12-22 20:31:12 +0800 | [diff] [blame] | 836 | goto out; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 837 | } |
Sven Eckelmann | 92f90f5 | 2011-12-22 20:31:12 +0800 | [diff] [blame] | 838 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 839 | /* if we are deleting a global entry due to a roam |
| 840 | * event, there are two possibilities: |
| 841 | * 1) the client roamed from node A to node B => if there |
| 842 | * is only one originator left for this client, we mark |
| 843 | * it with TT_CLIENT_ROAM, we start a timer and we |
| 844 | * wait for node B to claim it. In case of timeout |
| 845 | * the entry is purged. |
| 846 | * |
| 847 | * If there are other originators left, we directly delete |
| 848 | * the originator. |
| 849 | * 2) the client roamed to us => we can directly delete |
| 850 | * the global entry, since it is useless now. */ |
| 851 | |
| 852 | tt_local_entry = tt_local_hash_find(bat_priv, |
| 853 | tt_global_entry->common.addr); |
| 854 | if (tt_local_entry) { |
| 855 | /* local entry exists, case 2: client roamed to us. */ |
| 856 | tt_global_del_orig_list(tt_global_entry); |
| 857 | tt_global_del_struct(bat_priv, tt_global_entry, message); |
| 858 | } else |
| 859 | /* no local entry exists, case 1: check for roaming */ |
| 860 | tt_global_del_roaming(bat_priv, tt_global_entry, orig_node, |
| 861 | message); |
| 862 | |
Sven Eckelmann | 92f90f5 | 2011-12-22 20:31:12 +0800 | [diff] [blame] | 863 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 864 | out: |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 865 | if (tt_global_entry) |
| 866 | tt_global_entry_free_ref(tt_global_entry); |
Antonio Quartulli | 797399b | 2011-12-04 22:38:27 +0100 | [diff] [blame] | 867 | if (tt_local_entry) |
| 868 | tt_local_entry_free_ref(tt_local_entry); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | void tt_global_del_orig(struct bat_priv *bat_priv, |
| 872 | struct orig_node *orig_node, const char *message) |
| 873 | { |
| 874 | struct tt_global_entry *tt_global_entry; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 875 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 876 | uint32_t i; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 877 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
| 878 | struct hlist_node *node, *safe; |
| 879 | struct hlist_head *head; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 880 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 881 | |
Simon Wunderlich | 6e80149 | 2011-10-19 10:28:26 +0200 | [diff] [blame] | 882 | if (!hash) |
| 883 | return; |
| 884 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 885 | for (i = 0; i < hash->size; i++) { |
| 886 | head = &hash->table[i]; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 887 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 888 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 889 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 890 | hlist_for_each_entry_safe(tt_common_entry, node, safe, |
Sven Eckelmann | 7c64fd9 | 2012-02-28 10:55:36 +0100 | [diff] [blame] | 891 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 892 | tt_global_entry = container_of(tt_common_entry, |
| 893 | struct tt_global_entry, |
| 894 | common); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 895 | |
| 896 | tt_global_del_orig_entry(bat_priv, tt_global_entry, |
| 897 | orig_node, message); |
| 898 | |
| 899 | if (hlist_empty(&tt_global_entry->orig_list)) { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 900 | bat_dbg(DBG_TT, bat_priv, |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 901 | "Deleting global tt entry %pM: %s\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 902 | tt_global_entry->common.addr, |
Antonio Quartulli | 8794497 | 2011-09-19 12:29:19 +0200 | [diff] [blame] | 903 | message); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 904 | hlist_del_rcu(node); |
| 905 | tt_global_entry_free_ref(tt_global_entry); |
| 906 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 907 | } |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 908 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 909 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 910 | atomic_set(&orig_node->tt_size, 0); |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 911 | orig_node->tt_initialised = false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 914 | static void tt_global_roam_purge(struct bat_priv *bat_priv) |
| 915 | { |
| 916 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 917 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 918 | struct tt_global_entry *tt_global_entry; |
| 919 | struct hlist_node *node, *node_tmp; |
| 920 | struct hlist_head *head; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 921 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 922 | uint32_t i; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 923 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 924 | for (i = 0; i < hash->size; i++) { |
| 925 | head = &hash->table[i]; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 926 | list_lock = &hash->list_locks[i]; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 927 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 928 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 929 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 930 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 931 | tt_global_entry = container_of(tt_common_entry, |
| 932 | struct tt_global_entry, |
| 933 | common); |
| 934 | if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM)) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 935 | continue; |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame] | 936 | if (!has_timed_out(tt_global_entry->roam_at, |
Marek Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 937 | TT_CLIENT_ROAM_TIMEOUT)) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 938 | continue; |
| 939 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 940 | bat_dbg(DBG_TT, bat_priv, |
| 941 | "Deleting global tt entry (%pM): Roaming timeout\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 942 | tt_global_entry->common.addr); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 943 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 944 | hlist_del_rcu(node); |
| 945 | tt_global_entry_free_ref(tt_global_entry); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 946 | } |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 947 | spin_unlock_bh(list_lock); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 948 | } |
| 949 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 952 | static void tt_global_table_free(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 953 | { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 954 | struct hashtable_t *hash; |
| 955 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 956 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 957 | struct tt_global_entry *tt_global_entry; |
| 958 | struct hlist_node *node, *node_tmp; |
| 959 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 960 | uint32_t i; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 961 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 962 | if (!bat_priv->tt_global_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 963 | return; |
| 964 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 965 | hash = bat_priv->tt_global_hash; |
| 966 | |
| 967 | for (i = 0; i < hash->size; i++) { |
| 968 | head = &hash->table[i]; |
| 969 | list_lock = &hash->list_locks[i]; |
| 970 | |
| 971 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 972 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 973 | head, hash_entry) { |
| 974 | hlist_del_rcu(node); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 975 | tt_global_entry = container_of(tt_common_entry, |
| 976 | struct tt_global_entry, |
| 977 | common); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 978 | tt_global_entry_free_ref(tt_global_entry); |
| 979 | } |
| 980 | spin_unlock_bh(list_lock); |
| 981 | } |
| 982 | |
| 983 | hash_destroy(hash); |
| 984 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 985 | bat_priv->tt_global_hash = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 988 | static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry, |
| 989 | struct tt_global_entry *tt_global_entry) |
| 990 | { |
| 991 | bool ret = false; |
| 992 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 993 | if (tt_local_entry->common.flags & TT_CLIENT_WIFI && |
| 994 | tt_global_entry->common.flags & TT_CLIENT_WIFI) |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 995 | ret = true; |
| 996 | |
| 997 | return ret; |
| 998 | } |
| 999 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 1000 | struct orig_node *transtable_search(struct bat_priv *bat_priv, |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 1001 | const uint8_t *src, const uint8_t *addr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1002 | { |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 1003 | struct tt_local_entry *tt_local_entry = NULL; |
| 1004 | struct tt_global_entry *tt_global_entry = NULL; |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1005 | struct orig_node *orig_node = NULL; |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 1006 | struct neigh_node *router = NULL; |
| 1007 | struct hlist_head *head; |
| 1008 | struct hlist_node *node; |
| 1009 | struct tt_orig_list_entry *orig_entry; |
| 1010 | int best_tq; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1011 | |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 1012 | if (src && atomic_read(&bat_priv->ap_isolation)) { |
| 1013 | tt_local_entry = tt_local_hash_find(bat_priv, src); |
| 1014 | if (!tt_local_entry) |
| 1015 | goto out; |
| 1016 | } |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 1017 | |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 1018 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 1019 | if (!tt_global_entry) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1020 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1021 | |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 1022 | /* check whether the clients should not communicate due to AP |
| 1023 | * isolation */ |
| 1024 | if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry)) |
| 1025 | goto out; |
| 1026 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 1027 | best_tq = 0; |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1028 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 1029 | rcu_read_lock(); |
| 1030 | head = &tt_global_entry->orig_list; |
| 1031 | hlist_for_each_entry_rcu(orig_entry, node, head, list) { |
| 1032 | router = orig_node_get_router(orig_entry->orig_node); |
| 1033 | if (!router) |
| 1034 | continue; |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1035 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 1036 | if (router->tq_avg > best_tq) { |
| 1037 | orig_node = orig_entry->orig_node; |
| 1038 | best_tq = router->tq_avg; |
| 1039 | } |
| 1040 | neigh_node_free_ref(router); |
| 1041 | } |
| 1042 | /* found anything? */ |
| 1043 | if (orig_node && !atomic_inc_not_zero(&orig_node->refcount)) |
| 1044 | orig_node = NULL; |
| 1045 | rcu_read_unlock(); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1046 | out: |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 1047 | if (tt_global_entry) |
| 1048 | tt_global_entry_free_ref(tt_global_entry); |
| 1049 | if (tt_local_entry) |
| 1050 | tt_local_entry_free_ref(tt_local_entry); |
| 1051 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1052 | return orig_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1053 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1054 | |
| 1055 | /* Calculates the checksum of the local table of a given orig_node */ |
Sven Eckelmann | de7aae6 | 2012-02-05 18:55:22 +0100 | [diff] [blame] | 1056 | static uint16_t tt_global_crc(struct bat_priv *bat_priv, |
| 1057 | struct orig_node *orig_node) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1058 | { |
| 1059 | uint16_t total = 0, total_one; |
| 1060 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1061 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1062 | struct tt_global_entry *tt_global_entry; |
| 1063 | struct hlist_node *node; |
| 1064 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1065 | uint32_t i; |
| 1066 | int j; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1067 | |
| 1068 | for (i = 0; i < hash->size; i++) { |
| 1069 | head = &hash->table[i]; |
| 1070 | |
| 1071 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1072 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1073 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1074 | tt_global_entry = container_of(tt_common_entry, |
| 1075 | struct tt_global_entry, |
| 1076 | common); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 1077 | /* Roaming clients are in the global table for |
| 1078 | * consistency only. They don't have to be |
| 1079 | * taken into account while computing the |
| 1080 | * global crc |
| 1081 | */ |
| 1082 | if (tt_global_entry->common.flags & TT_CLIENT_ROAM) |
| 1083 | continue; |
| 1084 | |
| 1085 | /* find out if this global entry is announced by this |
| 1086 | * originator |
| 1087 | */ |
| 1088 | if (!tt_global_entry_has_orig(tt_global_entry, |
| 1089 | orig_node)) |
| 1090 | continue; |
| 1091 | |
| 1092 | total_one = 0; |
| 1093 | for (j = 0; j < ETH_ALEN; j++) |
| 1094 | total_one = crc16_byte(total_one, |
| 1095 | tt_global_entry->common.addr[j]); |
| 1096 | total ^= total_one; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1097 | } |
| 1098 | rcu_read_unlock(); |
| 1099 | } |
| 1100 | |
| 1101 | return total; |
| 1102 | } |
| 1103 | |
| 1104 | /* Calculates the checksum of the local table */ |
| 1105 | uint16_t tt_local_crc(struct bat_priv *bat_priv) |
| 1106 | { |
| 1107 | uint16_t total = 0, total_one; |
| 1108 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1109 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1110 | struct hlist_node *node; |
| 1111 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1112 | uint32_t i; |
| 1113 | int j; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1114 | |
| 1115 | for (i = 0; i < hash->size; i++) { |
| 1116 | head = &hash->table[i]; |
| 1117 | |
| 1118 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1119 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1120 | head, hash_entry) { |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1121 | /* not yet committed clients have not to be taken into |
| 1122 | * account while computing the CRC */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1123 | if (tt_common_entry->flags & TT_CLIENT_NEW) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1124 | continue; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1125 | total_one = 0; |
| 1126 | for (j = 0; j < ETH_ALEN; j++) |
| 1127 | total_one = crc16_byte(total_one, |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1128 | tt_common_entry->addr[j]); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1129 | total ^= total_one; |
| 1130 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1131 | rcu_read_unlock(); |
| 1132 | } |
| 1133 | |
| 1134 | return total; |
| 1135 | } |
| 1136 | |
| 1137 | static void tt_req_list_free(struct bat_priv *bat_priv) |
| 1138 | { |
| 1139 | struct tt_req_node *node, *safe; |
| 1140 | |
| 1141 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 1142 | |
| 1143 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { |
| 1144 | list_del(&node->list); |
| 1145 | kfree(node); |
| 1146 | } |
| 1147 | |
| 1148 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 1149 | } |
| 1150 | |
Sven Eckelmann | de7aae6 | 2012-02-05 18:55:22 +0100 | [diff] [blame] | 1151 | static void tt_save_orig_buffer(struct bat_priv *bat_priv, |
| 1152 | struct orig_node *orig_node, |
| 1153 | const unsigned char *tt_buff, |
| 1154 | uint8_t tt_num_changes) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1155 | { |
| 1156 | uint16_t tt_buff_len = tt_len(tt_num_changes); |
| 1157 | |
| 1158 | /* Replace the old buffer only if I received something in the |
| 1159 | * last OGM (the OGM could carry no changes) */ |
| 1160 | spin_lock_bh(&orig_node->tt_buff_lock); |
| 1161 | if (tt_buff_len > 0) { |
| 1162 | kfree(orig_node->tt_buff); |
| 1163 | orig_node->tt_buff_len = 0; |
| 1164 | orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC); |
| 1165 | if (orig_node->tt_buff) { |
| 1166 | memcpy(orig_node->tt_buff, tt_buff, tt_buff_len); |
| 1167 | orig_node->tt_buff_len = tt_buff_len; |
| 1168 | } |
| 1169 | } |
| 1170 | spin_unlock_bh(&orig_node->tt_buff_lock); |
| 1171 | } |
| 1172 | |
| 1173 | static void tt_req_purge(struct bat_priv *bat_priv) |
| 1174 | { |
| 1175 | struct tt_req_node *node, *safe; |
| 1176 | |
| 1177 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 1178 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { |
Marek Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 1179 | if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1180 | list_del(&node->list); |
| 1181 | kfree(node); |
| 1182 | } |
| 1183 | } |
| 1184 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 1185 | } |
| 1186 | |
| 1187 | /* returns the pointer to the new tt_req_node struct if no request |
| 1188 | * has already been issued for this orig_node, NULL otherwise */ |
| 1189 | static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv, |
| 1190 | struct orig_node *orig_node) |
| 1191 | { |
| 1192 | struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL; |
| 1193 | |
| 1194 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 1195 | list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) { |
| 1196 | if (compare_eth(tt_req_node_tmp, orig_node) && |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame] | 1197 | !has_timed_out(tt_req_node_tmp->issued_at, |
Marek Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 1198 | TT_REQUEST_TIMEOUT)) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1199 | goto unlock; |
| 1200 | } |
| 1201 | |
| 1202 | tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC); |
| 1203 | if (!tt_req_node) |
| 1204 | goto unlock; |
| 1205 | |
| 1206 | memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN); |
| 1207 | tt_req_node->issued_at = jiffies; |
| 1208 | |
| 1209 | list_add(&tt_req_node->list, &bat_priv->tt_req_list); |
| 1210 | unlock: |
| 1211 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 1212 | return tt_req_node; |
| 1213 | } |
| 1214 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1215 | /* data_ptr is useless here, but has to be kept to respect the prototype */ |
| 1216 | static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr) |
| 1217 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1218 | const struct tt_common_entry *tt_common_entry = entry_ptr; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1219 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1220 | if (tt_common_entry->flags & TT_CLIENT_NEW) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1221 | return 0; |
| 1222 | return 1; |
| 1223 | } |
| 1224 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1225 | static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr) |
| 1226 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1227 | const struct tt_common_entry *tt_common_entry = entry_ptr; |
| 1228 | const struct tt_global_entry *tt_global_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1229 | const struct orig_node *orig_node = data_ptr; |
| 1230 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1231 | if (tt_common_entry->flags & TT_CLIENT_ROAM) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1232 | return 0; |
| 1233 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1234 | tt_global_entry = container_of(tt_common_entry, struct tt_global_entry, |
| 1235 | common); |
| 1236 | |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 1237 | return tt_global_entry_has_orig(tt_global_entry, orig_node); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn, |
| 1241 | struct hashtable_t *hash, |
| 1242 | struct hard_iface *primary_if, |
| 1243 | int (*valid_cb)(const void *, |
| 1244 | const void *), |
| 1245 | void *cb_data) |
| 1246 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1247 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1248 | struct tt_query_packet *tt_response; |
| 1249 | struct tt_change *tt_change; |
| 1250 | struct hlist_node *node; |
| 1251 | struct hlist_head *head; |
| 1252 | struct sk_buff *skb = NULL; |
| 1253 | uint16_t tt_tot, tt_count; |
| 1254 | ssize_t tt_query_size = sizeof(struct tt_query_packet); |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1255 | uint32_t i; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1256 | |
| 1257 | if (tt_query_size + tt_len > primary_if->soft_iface->mtu) { |
| 1258 | tt_len = primary_if->soft_iface->mtu - tt_query_size; |
| 1259 | tt_len -= tt_len % sizeof(struct tt_change); |
| 1260 | } |
| 1261 | tt_tot = tt_len / sizeof(struct tt_change); |
| 1262 | |
| 1263 | skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN); |
| 1264 | if (!skb) |
| 1265 | goto out; |
| 1266 | |
| 1267 | skb_reserve(skb, ETH_HLEN); |
| 1268 | tt_response = (struct tt_query_packet *)skb_put(skb, |
| 1269 | tt_query_size + tt_len); |
| 1270 | tt_response->ttvn = ttvn; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1271 | |
| 1272 | tt_change = (struct tt_change *)(skb->data + tt_query_size); |
| 1273 | tt_count = 0; |
| 1274 | |
| 1275 | rcu_read_lock(); |
| 1276 | for (i = 0; i < hash->size; i++) { |
| 1277 | head = &hash->table[i]; |
| 1278 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1279 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1280 | head, hash_entry) { |
| 1281 | if (tt_count == tt_tot) |
| 1282 | break; |
| 1283 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1284 | if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data))) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1285 | continue; |
| 1286 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1287 | memcpy(tt_change->addr, tt_common_entry->addr, |
| 1288 | ETH_ALEN); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1289 | tt_change->flags = NO_FLAGS; |
| 1290 | |
| 1291 | tt_count++; |
| 1292 | tt_change++; |
| 1293 | } |
| 1294 | } |
| 1295 | rcu_read_unlock(); |
| 1296 | |
Antonio Quartulli | 9d85239 | 2011-10-17 14:25:13 +0200 | [diff] [blame] | 1297 | /* store in the message the number of entries we have successfully |
| 1298 | * copied */ |
| 1299 | tt_response->tt_data = htons(tt_count); |
| 1300 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1301 | out: |
| 1302 | return skb; |
| 1303 | } |
| 1304 | |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 1305 | static int send_tt_request(struct bat_priv *bat_priv, |
| 1306 | struct orig_node *dst_orig_node, |
| 1307 | uint8_t ttvn, uint16_t tt_crc, bool full_table) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1308 | { |
| 1309 | struct sk_buff *skb = NULL; |
| 1310 | struct tt_query_packet *tt_request; |
| 1311 | struct neigh_node *neigh_node = NULL; |
| 1312 | struct hard_iface *primary_if; |
| 1313 | struct tt_req_node *tt_req_node = NULL; |
| 1314 | int ret = 1; |
| 1315 | |
| 1316 | primary_if = primary_if_get_selected(bat_priv); |
| 1317 | if (!primary_if) |
| 1318 | goto out; |
| 1319 | |
| 1320 | /* The new tt_req will be issued only if I'm not waiting for a |
| 1321 | * reply from the same orig_node yet */ |
| 1322 | tt_req_node = new_tt_req_node(bat_priv, dst_orig_node); |
| 1323 | if (!tt_req_node) |
| 1324 | goto out; |
| 1325 | |
| 1326 | skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN); |
| 1327 | if (!skb) |
| 1328 | goto out; |
| 1329 | |
| 1330 | skb_reserve(skb, ETH_HLEN); |
| 1331 | |
| 1332 | tt_request = (struct tt_query_packet *)skb_put(skb, |
| 1333 | sizeof(struct tt_query_packet)); |
| 1334 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1335 | tt_request->header.packet_type = BAT_TT_QUERY; |
| 1336 | tt_request->header.version = COMPAT_VERSION; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1337 | memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN); |
| 1338 | memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN); |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1339 | tt_request->header.ttl = TTL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1340 | tt_request->ttvn = ttvn; |
| 1341 | tt_request->tt_data = tt_crc; |
| 1342 | tt_request->flags = TT_REQUEST; |
| 1343 | |
| 1344 | if (full_table) |
| 1345 | tt_request->flags |= TT_FULL_TABLE; |
| 1346 | |
| 1347 | neigh_node = orig_node_get_router(dst_orig_node); |
| 1348 | if (!neigh_node) |
| 1349 | goto out; |
| 1350 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1351 | bat_dbg(DBG_TT, bat_priv, |
| 1352 | "Sending TT_REQUEST to %pM via %pM [%c]\n", |
| 1353 | dst_orig_node->orig, neigh_node->addr, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1354 | (full_table ? 'F' : '.')); |
| 1355 | |
| 1356 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
| 1357 | ret = 0; |
| 1358 | |
| 1359 | out: |
| 1360 | if (neigh_node) |
| 1361 | neigh_node_free_ref(neigh_node); |
| 1362 | if (primary_if) |
| 1363 | hardif_free_ref(primary_if); |
| 1364 | if (ret) |
| 1365 | kfree_skb(skb); |
| 1366 | if (ret && tt_req_node) { |
| 1367 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 1368 | list_del(&tt_req_node->list); |
| 1369 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 1370 | kfree(tt_req_node); |
| 1371 | } |
| 1372 | return ret; |
| 1373 | } |
| 1374 | |
| 1375 | static bool send_other_tt_response(struct bat_priv *bat_priv, |
| 1376 | struct tt_query_packet *tt_request) |
| 1377 | { |
| 1378 | struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL; |
| 1379 | struct neigh_node *neigh_node = NULL; |
| 1380 | struct hard_iface *primary_if = NULL; |
| 1381 | uint8_t orig_ttvn, req_ttvn, ttvn; |
| 1382 | int ret = false; |
| 1383 | unsigned char *tt_buff; |
| 1384 | bool full_table; |
| 1385 | uint16_t tt_len, tt_tot; |
| 1386 | struct sk_buff *skb = NULL; |
| 1387 | struct tt_query_packet *tt_response; |
| 1388 | |
| 1389 | bat_dbg(DBG_TT, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1390 | "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n", |
| 1391 | tt_request->src, tt_request->ttvn, tt_request->dst, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1392 | (tt_request->flags & TT_FULL_TABLE ? 'F' : '.')); |
| 1393 | |
| 1394 | /* Let's get the orig node of the REAL destination */ |
Antonio Quartulli | eb7e2a1 | 2011-10-12 14:54:50 +0200 | [diff] [blame] | 1395 | req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1396 | if (!req_dst_orig_node) |
| 1397 | goto out; |
| 1398 | |
Antonio Quartulli | eb7e2a1 | 2011-10-12 14:54:50 +0200 | [diff] [blame] | 1399 | res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1400 | if (!res_dst_orig_node) |
| 1401 | goto out; |
| 1402 | |
| 1403 | neigh_node = orig_node_get_router(res_dst_orig_node); |
| 1404 | if (!neigh_node) |
| 1405 | goto out; |
| 1406 | |
| 1407 | primary_if = primary_if_get_selected(bat_priv); |
| 1408 | if (!primary_if) |
| 1409 | goto out; |
| 1410 | |
| 1411 | orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); |
| 1412 | req_ttvn = tt_request->ttvn; |
| 1413 | |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 1414 | /* I don't have the requested data */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1415 | if (orig_ttvn != req_ttvn || |
| 1416 | tt_request->tt_data != req_dst_orig_node->tt_crc) |
| 1417 | goto out; |
| 1418 | |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 1419 | /* If the full table has been explicitly requested */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1420 | if (tt_request->flags & TT_FULL_TABLE || |
| 1421 | !req_dst_orig_node->tt_buff) |
| 1422 | full_table = true; |
| 1423 | else |
| 1424 | full_table = false; |
| 1425 | |
| 1426 | /* In this version, fragmentation is not implemented, then |
| 1427 | * I'll send only one packet with as much TT entries as I can */ |
| 1428 | if (!full_table) { |
| 1429 | spin_lock_bh(&req_dst_orig_node->tt_buff_lock); |
| 1430 | tt_len = req_dst_orig_node->tt_buff_len; |
| 1431 | tt_tot = tt_len / sizeof(struct tt_change); |
| 1432 | |
| 1433 | skb = dev_alloc_skb(sizeof(struct tt_query_packet) + |
| 1434 | tt_len + ETH_HLEN); |
| 1435 | if (!skb) |
| 1436 | goto unlock; |
| 1437 | |
| 1438 | skb_reserve(skb, ETH_HLEN); |
| 1439 | tt_response = (struct tt_query_packet *)skb_put(skb, |
| 1440 | sizeof(struct tt_query_packet) + tt_len); |
| 1441 | tt_response->ttvn = req_ttvn; |
| 1442 | tt_response->tt_data = htons(tt_tot); |
| 1443 | |
| 1444 | tt_buff = skb->data + sizeof(struct tt_query_packet); |
| 1445 | /* Copy the last orig_node's OGM buffer */ |
| 1446 | memcpy(tt_buff, req_dst_orig_node->tt_buff, |
| 1447 | req_dst_orig_node->tt_buff_len); |
| 1448 | |
| 1449 | spin_unlock_bh(&req_dst_orig_node->tt_buff_lock); |
| 1450 | } else { |
| 1451 | tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) * |
| 1452 | sizeof(struct tt_change); |
| 1453 | ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); |
| 1454 | |
| 1455 | skb = tt_response_fill_table(tt_len, ttvn, |
| 1456 | bat_priv->tt_global_hash, |
| 1457 | primary_if, tt_global_valid_entry, |
| 1458 | req_dst_orig_node); |
| 1459 | if (!skb) |
| 1460 | goto out; |
| 1461 | |
| 1462 | tt_response = (struct tt_query_packet *)skb->data; |
| 1463 | } |
| 1464 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1465 | tt_response->header.packet_type = BAT_TT_QUERY; |
| 1466 | tt_response->header.version = COMPAT_VERSION; |
| 1467 | tt_response->header.ttl = TTL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1468 | memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN); |
| 1469 | memcpy(tt_response->dst, tt_request->src, ETH_ALEN); |
| 1470 | tt_response->flags = TT_RESPONSE; |
| 1471 | |
| 1472 | if (full_table) |
| 1473 | tt_response->flags |= TT_FULL_TABLE; |
| 1474 | |
| 1475 | bat_dbg(DBG_TT, bat_priv, |
| 1476 | "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n", |
| 1477 | res_dst_orig_node->orig, neigh_node->addr, |
| 1478 | req_dst_orig_node->orig, req_ttvn); |
| 1479 | |
| 1480 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
| 1481 | ret = true; |
| 1482 | goto out; |
| 1483 | |
| 1484 | unlock: |
| 1485 | spin_unlock_bh(&req_dst_orig_node->tt_buff_lock); |
| 1486 | |
| 1487 | out: |
| 1488 | if (res_dst_orig_node) |
| 1489 | orig_node_free_ref(res_dst_orig_node); |
| 1490 | if (req_dst_orig_node) |
| 1491 | orig_node_free_ref(req_dst_orig_node); |
| 1492 | if (neigh_node) |
| 1493 | neigh_node_free_ref(neigh_node); |
| 1494 | if (primary_if) |
| 1495 | hardif_free_ref(primary_if); |
| 1496 | if (!ret) |
| 1497 | kfree_skb(skb); |
| 1498 | return ret; |
| 1499 | |
| 1500 | } |
| 1501 | static bool send_my_tt_response(struct bat_priv *bat_priv, |
| 1502 | struct tt_query_packet *tt_request) |
| 1503 | { |
| 1504 | struct orig_node *orig_node = NULL; |
| 1505 | struct neigh_node *neigh_node = NULL; |
| 1506 | struct hard_iface *primary_if = NULL; |
| 1507 | uint8_t my_ttvn, req_ttvn, ttvn; |
| 1508 | int ret = false; |
| 1509 | unsigned char *tt_buff; |
| 1510 | bool full_table; |
| 1511 | uint16_t tt_len, tt_tot; |
| 1512 | struct sk_buff *skb = NULL; |
| 1513 | struct tt_query_packet *tt_response; |
| 1514 | |
| 1515 | bat_dbg(DBG_TT, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1516 | "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n", |
| 1517 | tt_request->src, tt_request->ttvn, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1518 | (tt_request->flags & TT_FULL_TABLE ? 'F' : '.')); |
| 1519 | |
| 1520 | |
| 1521 | my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn); |
| 1522 | req_ttvn = tt_request->ttvn; |
| 1523 | |
Antonio Quartulli | eb7e2a1 | 2011-10-12 14:54:50 +0200 | [diff] [blame] | 1524 | orig_node = orig_hash_find(bat_priv, tt_request->src); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1525 | if (!orig_node) |
| 1526 | goto out; |
| 1527 | |
| 1528 | neigh_node = orig_node_get_router(orig_node); |
| 1529 | if (!neigh_node) |
| 1530 | goto out; |
| 1531 | |
| 1532 | primary_if = primary_if_get_selected(bat_priv); |
| 1533 | if (!primary_if) |
| 1534 | goto out; |
| 1535 | |
| 1536 | /* If the full table has been explicitly requested or the gap |
| 1537 | * is too big send the whole local translation table */ |
| 1538 | if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn || |
| 1539 | !bat_priv->tt_buff) |
| 1540 | full_table = true; |
| 1541 | else |
| 1542 | full_table = false; |
| 1543 | |
| 1544 | /* In this version, fragmentation is not implemented, then |
| 1545 | * I'll send only one packet with as much TT entries as I can */ |
| 1546 | if (!full_table) { |
| 1547 | spin_lock_bh(&bat_priv->tt_buff_lock); |
| 1548 | tt_len = bat_priv->tt_buff_len; |
| 1549 | tt_tot = tt_len / sizeof(struct tt_change); |
| 1550 | |
| 1551 | skb = dev_alloc_skb(sizeof(struct tt_query_packet) + |
| 1552 | tt_len + ETH_HLEN); |
| 1553 | if (!skb) |
| 1554 | goto unlock; |
| 1555 | |
| 1556 | skb_reserve(skb, ETH_HLEN); |
| 1557 | tt_response = (struct tt_query_packet *)skb_put(skb, |
| 1558 | sizeof(struct tt_query_packet) + tt_len); |
| 1559 | tt_response->ttvn = req_ttvn; |
| 1560 | tt_response->tt_data = htons(tt_tot); |
| 1561 | |
| 1562 | tt_buff = skb->data + sizeof(struct tt_query_packet); |
| 1563 | memcpy(tt_buff, bat_priv->tt_buff, |
| 1564 | bat_priv->tt_buff_len); |
| 1565 | spin_unlock_bh(&bat_priv->tt_buff_lock); |
| 1566 | } else { |
| 1567 | tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) * |
| 1568 | sizeof(struct tt_change); |
| 1569 | ttvn = (uint8_t)atomic_read(&bat_priv->ttvn); |
| 1570 | |
| 1571 | skb = tt_response_fill_table(tt_len, ttvn, |
| 1572 | bat_priv->tt_local_hash, |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1573 | primary_if, tt_local_valid_entry, |
| 1574 | NULL); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1575 | if (!skb) |
| 1576 | goto out; |
| 1577 | |
| 1578 | tt_response = (struct tt_query_packet *)skb->data; |
| 1579 | } |
| 1580 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1581 | tt_response->header.packet_type = BAT_TT_QUERY; |
| 1582 | tt_response->header.version = COMPAT_VERSION; |
| 1583 | tt_response->header.ttl = TTL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1584 | memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN); |
| 1585 | memcpy(tt_response->dst, tt_request->src, ETH_ALEN); |
| 1586 | tt_response->flags = TT_RESPONSE; |
| 1587 | |
| 1588 | if (full_table) |
| 1589 | tt_response->flags |= TT_FULL_TABLE; |
| 1590 | |
| 1591 | bat_dbg(DBG_TT, bat_priv, |
| 1592 | "Sending TT_RESPONSE to %pM via %pM [%c]\n", |
| 1593 | orig_node->orig, neigh_node->addr, |
| 1594 | (tt_response->flags & TT_FULL_TABLE ? 'F' : '.')); |
| 1595 | |
| 1596 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
| 1597 | ret = true; |
| 1598 | goto out; |
| 1599 | |
| 1600 | unlock: |
| 1601 | spin_unlock_bh(&bat_priv->tt_buff_lock); |
| 1602 | out: |
| 1603 | if (orig_node) |
| 1604 | orig_node_free_ref(orig_node); |
| 1605 | if (neigh_node) |
| 1606 | neigh_node_free_ref(neigh_node); |
| 1607 | if (primary_if) |
| 1608 | hardif_free_ref(primary_if); |
| 1609 | if (!ret) |
| 1610 | kfree_skb(skb); |
| 1611 | /* This packet was for me, so it doesn't need to be re-routed */ |
| 1612 | return true; |
| 1613 | } |
| 1614 | |
| 1615 | bool send_tt_response(struct bat_priv *bat_priv, |
| 1616 | struct tt_query_packet *tt_request) |
| 1617 | { |
| 1618 | if (is_my_mac(tt_request->dst)) |
| 1619 | return send_my_tt_response(bat_priv, tt_request); |
| 1620 | else |
| 1621 | return send_other_tt_response(bat_priv, tt_request); |
| 1622 | } |
| 1623 | |
| 1624 | static void _tt_update_changes(struct bat_priv *bat_priv, |
| 1625 | struct orig_node *orig_node, |
| 1626 | struct tt_change *tt_change, |
| 1627 | uint16_t tt_num_changes, uint8_t ttvn) |
| 1628 | { |
| 1629 | int i; |
| 1630 | |
| 1631 | for (i = 0; i < tt_num_changes; i++) { |
Antonio Quartulli | 5fbc159 | 2011-06-17 16:11:27 +0200 | [diff] [blame] | 1632 | if ((tt_change + i)->flags & TT_CLIENT_DEL) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1633 | tt_global_del(bat_priv, orig_node, |
| 1634 | (tt_change + i)->addr, |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1635 | "tt removed by changes", |
| 1636 | (tt_change + i)->flags & TT_CLIENT_ROAM); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1637 | else |
| 1638 | if (!tt_global_add(bat_priv, orig_node, |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 1639 | (tt_change + i)->addr, ttvn, false, |
| 1640 | (tt_change + i)->flags & |
| 1641 | TT_CLIENT_WIFI)) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1642 | /* In case of problem while storing a |
| 1643 | * global_entry, we stop the updating |
| 1644 | * procedure without committing the |
| 1645 | * ttvn change. This will avoid to send |
| 1646 | * corrupted data on tt_request |
| 1647 | */ |
| 1648 | return; |
| 1649 | } |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 1650 | orig_node->tt_initialised = true; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | static void tt_fill_gtable(struct bat_priv *bat_priv, |
| 1654 | struct tt_query_packet *tt_response) |
| 1655 | { |
| 1656 | struct orig_node *orig_node = NULL; |
| 1657 | |
| 1658 | orig_node = orig_hash_find(bat_priv, tt_response->src); |
| 1659 | if (!orig_node) |
| 1660 | goto out; |
| 1661 | |
| 1662 | /* Purge the old table first.. */ |
| 1663 | tt_global_del_orig(bat_priv, orig_node, "Received full table"); |
| 1664 | |
| 1665 | _tt_update_changes(bat_priv, orig_node, |
| 1666 | (struct tt_change *)(tt_response + 1), |
| 1667 | tt_response->tt_data, tt_response->ttvn); |
| 1668 | |
| 1669 | spin_lock_bh(&orig_node->tt_buff_lock); |
| 1670 | kfree(orig_node->tt_buff); |
| 1671 | orig_node->tt_buff_len = 0; |
| 1672 | orig_node->tt_buff = NULL; |
| 1673 | spin_unlock_bh(&orig_node->tt_buff_lock); |
| 1674 | |
| 1675 | atomic_set(&orig_node->last_ttvn, tt_response->ttvn); |
| 1676 | |
| 1677 | out: |
| 1678 | if (orig_node) |
| 1679 | orig_node_free_ref(orig_node); |
| 1680 | } |
| 1681 | |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 1682 | static void tt_update_changes(struct bat_priv *bat_priv, |
| 1683 | struct orig_node *orig_node, |
| 1684 | uint16_t tt_num_changes, uint8_t ttvn, |
| 1685 | struct tt_change *tt_change) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1686 | { |
| 1687 | _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes, |
| 1688 | ttvn); |
| 1689 | |
| 1690 | tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change, |
| 1691 | tt_num_changes); |
| 1692 | atomic_set(&orig_node->last_ttvn, ttvn); |
| 1693 | } |
| 1694 | |
| 1695 | bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr) |
| 1696 | { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 1697 | struct tt_local_entry *tt_local_entry = NULL; |
| 1698 | bool ret = false; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1699 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1700 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 1701 | if (!tt_local_entry) |
| 1702 | goto out; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1703 | /* Check if the client has been logically deleted (but is kept for |
| 1704 | * consistency purpose) */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1705 | if (tt_local_entry->common.flags & TT_CLIENT_PENDING) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1706 | goto out; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 1707 | ret = true; |
| 1708 | out: |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1709 | if (tt_local_entry) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 1710 | tt_local_entry_free_ref(tt_local_entry); |
| 1711 | return ret; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | void handle_tt_response(struct bat_priv *bat_priv, |
| 1715 | struct tt_query_packet *tt_response) |
| 1716 | { |
| 1717 | struct tt_req_node *node, *safe; |
| 1718 | struct orig_node *orig_node = NULL; |
| 1719 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1720 | bat_dbg(DBG_TT, bat_priv, |
| 1721 | "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n", |
| 1722 | tt_response->src, tt_response->ttvn, tt_response->tt_data, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1723 | (tt_response->flags & TT_FULL_TABLE ? 'F' : '.')); |
| 1724 | |
| 1725 | orig_node = orig_hash_find(bat_priv, tt_response->src); |
| 1726 | if (!orig_node) |
| 1727 | goto out; |
| 1728 | |
| 1729 | if (tt_response->flags & TT_FULL_TABLE) |
| 1730 | tt_fill_gtable(bat_priv, tt_response); |
| 1731 | else |
| 1732 | tt_update_changes(bat_priv, orig_node, tt_response->tt_data, |
| 1733 | tt_response->ttvn, |
| 1734 | (struct tt_change *)(tt_response + 1)); |
| 1735 | |
| 1736 | /* Delete the tt_req_node from pending tt_requests list */ |
| 1737 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 1738 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { |
| 1739 | if (!compare_eth(node->addr, tt_response->src)) |
| 1740 | continue; |
| 1741 | list_del(&node->list); |
| 1742 | kfree(node); |
| 1743 | } |
| 1744 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 1745 | |
| 1746 | /* Recalculate the CRC for this orig_node and store it */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1747 | orig_node->tt_crc = tt_global_crc(bat_priv, orig_node); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1748 | /* Roaming phase is over: tables are in sync again. I can |
| 1749 | * unset the flag */ |
| 1750 | orig_node->tt_poss_change = false; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1751 | out: |
| 1752 | if (orig_node) |
| 1753 | orig_node_free_ref(orig_node); |
| 1754 | } |
| 1755 | |
| 1756 | int tt_init(struct bat_priv *bat_priv) |
| 1757 | { |
| 1758 | if (!tt_local_init(bat_priv)) |
| 1759 | return 0; |
| 1760 | |
| 1761 | if (!tt_global_init(bat_priv)) |
| 1762 | return 0; |
| 1763 | |
| 1764 | tt_start_timer(bat_priv); |
| 1765 | |
| 1766 | return 1; |
| 1767 | } |
| 1768 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1769 | static void tt_roam_list_free(struct bat_priv *bat_priv) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1770 | { |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1771 | struct tt_roam_node *node, *safe; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1772 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1773 | spin_lock_bh(&bat_priv->tt_roam_list_lock); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1774 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1775 | list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) { |
| 1776 | list_del(&node->list); |
| 1777 | kfree(node); |
| 1778 | } |
| 1779 | |
| 1780 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); |
| 1781 | } |
| 1782 | |
| 1783 | static void tt_roam_purge(struct bat_priv *bat_priv) |
| 1784 | { |
| 1785 | struct tt_roam_node *node, *safe; |
| 1786 | |
| 1787 | spin_lock_bh(&bat_priv->tt_roam_list_lock); |
| 1788 | list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) { |
Marek Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 1789 | if (!has_timed_out(node->first_time, ROAMING_MAX_TIME)) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1790 | continue; |
| 1791 | |
| 1792 | list_del(&node->list); |
| 1793 | kfree(node); |
| 1794 | } |
| 1795 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); |
| 1796 | } |
| 1797 | |
| 1798 | /* This function checks whether the client already reached the |
| 1799 | * maximum number of possible roaming phases. In this case the ROAMING_ADV |
| 1800 | * will not be sent. |
| 1801 | * |
| 1802 | * returns true if the ROAMING_ADV can be sent, false otherwise */ |
| 1803 | static bool tt_check_roam_count(struct bat_priv *bat_priv, |
| 1804 | uint8_t *client) |
| 1805 | { |
| 1806 | struct tt_roam_node *tt_roam_node; |
| 1807 | bool ret = false; |
| 1808 | |
| 1809 | spin_lock_bh(&bat_priv->tt_roam_list_lock); |
| 1810 | /* The new tt_req will be issued only if I'm not waiting for a |
| 1811 | * reply from the same orig_node yet */ |
| 1812 | list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) { |
| 1813 | if (!compare_eth(tt_roam_node->addr, client)) |
| 1814 | continue; |
| 1815 | |
Marek Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 1816 | if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME)) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1817 | continue; |
| 1818 | |
| 1819 | if (!atomic_dec_not_zero(&tt_roam_node->counter)) |
| 1820 | /* Sorry, you roamed too many times! */ |
| 1821 | goto unlock; |
| 1822 | ret = true; |
| 1823 | break; |
| 1824 | } |
| 1825 | |
| 1826 | if (!ret) { |
| 1827 | tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC); |
| 1828 | if (!tt_roam_node) |
| 1829 | goto unlock; |
| 1830 | |
| 1831 | tt_roam_node->first_time = jiffies; |
| 1832 | atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1); |
| 1833 | memcpy(tt_roam_node->addr, client, ETH_ALEN); |
| 1834 | |
| 1835 | list_add(&tt_roam_node->list, &bat_priv->tt_roam_list); |
| 1836 | ret = true; |
| 1837 | } |
| 1838 | |
| 1839 | unlock: |
| 1840 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); |
| 1841 | return ret; |
| 1842 | } |
| 1843 | |
Sven Eckelmann | de7aae6 | 2012-02-05 18:55:22 +0100 | [diff] [blame] | 1844 | static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client, |
| 1845 | struct orig_node *orig_node) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1846 | { |
| 1847 | struct neigh_node *neigh_node = NULL; |
| 1848 | struct sk_buff *skb = NULL; |
| 1849 | struct roam_adv_packet *roam_adv_packet; |
| 1850 | int ret = 1; |
| 1851 | struct hard_iface *primary_if; |
| 1852 | |
| 1853 | /* before going on we have to check whether the client has |
| 1854 | * already roamed to us too many times */ |
| 1855 | if (!tt_check_roam_count(bat_priv, client)) |
| 1856 | goto out; |
| 1857 | |
| 1858 | skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN); |
| 1859 | if (!skb) |
| 1860 | goto out; |
| 1861 | |
| 1862 | skb_reserve(skb, ETH_HLEN); |
| 1863 | |
| 1864 | roam_adv_packet = (struct roam_adv_packet *)skb_put(skb, |
| 1865 | sizeof(struct roam_adv_packet)); |
| 1866 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1867 | roam_adv_packet->header.packet_type = BAT_ROAM_ADV; |
| 1868 | roam_adv_packet->header.version = COMPAT_VERSION; |
| 1869 | roam_adv_packet->header.ttl = TTL; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1870 | primary_if = primary_if_get_selected(bat_priv); |
| 1871 | if (!primary_if) |
| 1872 | goto out; |
| 1873 | memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN); |
| 1874 | hardif_free_ref(primary_if); |
| 1875 | memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN); |
| 1876 | memcpy(roam_adv_packet->client, client, ETH_ALEN); |
| 1877 | |
| 1878 | neigh_node = orig_node_get_router(orig_node); |
| 1879 | if (!neigh_node) |
| 1880 | goto out; |
| 1881 | |
| 1882 | bat_dbg(DBG_TT, bat_priv, |
| 1883 | "Sending ROAMING_ADV to %pM (client %pM) via %pM\n", |
| 1884 | orig_node->orig, client, neigh_node->addr); |
| 1885 | |
| 1886 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
| 1887 | ret = 0; |
| 1888 | |
| 1889 | out: |
| 1890 | if (neigh_node) |
| 1891 | neigh_node_free_ref(neigh_node); |
| 1892 | if (ret) |
| 1893 | kfree_skb(skb); |
| 1894 | return; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | static void tt_purge(struct work_struct *work) |
| 1898 | { |
| 1899 | struct delayed_work *delayed_work = |
| 1900 | container_of(work, struct delayed_work, work); |
| 1901 | struct bat_priv *bat_priv = |
| 1902 | container_of(delayed_work, struct bat_priv, tt_work); |
| 1903 | |
| 1904 | tt_local_purge(bat_priv); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1905 | tt_global_roam_purge(bat_priv); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1906 | tt_req_purge(bat_priv); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1907 | tt_roam_purge(bat_priv); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1908 | |
| 1909 | tt_start_timer(bat_priv); |
| 1910 | } |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1911 | |
| 1912 | void tt_free(struct bat_priv *bat_priv) |
| 1913 | { |
| 1914 | cancel_delayed_work_sync(&bat_priv->tt_work); |
| 1915 | |
| 1916 | tt_local_table_free(bat_priv); |
| 1917 | tt_global_table_free(bat_priv); |
| 1918 | tt_req_list_free(bat_priv); |
| 1919 | tt_changes_list_free(bat_priv); |
| 1920 | tt_roam_list_free(bat_priv); |
| 1921 | |
| 1922 | kfree(bat_priv->tt_buff); |
| 1923 | } |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1924 | |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1925 | /* This function will enable or disable the specified flags for all the entries |
| 1926 | * in the given hash table and returns the number of modified entries */ |
| 1927 | static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags, |
| 1928 | bool enable) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1929 | { |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1930 | uint32_t i; |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1931 | uint16_t changed_num = 0; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1932 | struct hlist_head *head; |
| 1933 | struct hlist_node *node; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1934 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1935 | |
| 1936 | if (!hash) |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1937 | goto out; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1938 | |
| 1939 | for (i = 0; i < hash->size; i++) { |
| 1940 | head = &hash->table[i]; |
| 1941 | |
| 1942 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1943 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1944 | head, hash_entry) { |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1945 | if (enable) { |
| 1946 | if ((tt_common_entry->flags & flags) == flags) |
| 1947 | continue; |
| 1948 | tt_common_entry->flags |= flags; |
| 1949 | } else { |
| 1950 | if (!(tt_common_entry->flags & flags)) |
| 1951 | continue; |
| 1952 | tt_common_entry->flags &= ~flags; |
| 1953 | } |
| 1954 | changed_num++; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1955 | } |
| 1956 | rcu_read_unlock(); |
| 1957 | } |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1958 | out: |
| 1959 | return changed_num; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */ |
| 1963 | static void tt_local_purge_pending_clients(struct bat_priv *bat_priv) |
| 1964 | { |
| 1965 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1966 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1967 | struct tt_local_entry *tt_local_entry; |
| 1968 | struct hlist_node *node, *node_tmp; |
| 1969 | struct hlist_head *head; |
| 1970 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1971 | uint32_t i; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1972 | |
| 1973 | if (!hash) |
| 1974 | return; |
| 1975 | |
| 1976 | for (i = 0; i < hash->size; i++) { |
| 1977 | head = &hash->table[i]; |
| 1978 | list_lock = &hash->list_locks[i]; |
| 1979 | |
| 1980 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1981 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1982 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1983 | if (!(tt_common_entry->flags & TT_CLIENT_PENDING)) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1984 | continue; |
| 1985 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 1986 | bat_dbg(DBG_TT, bat_priv, |
| 1987 | "Deleting local tt entry (%pM): pending\n", |
| 1988 | tt_common_entry->addr); |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1989 | |
| 1990 | atomic_dec(&bat_priv->num_local_tt); |
| 1991 | hlist_del_rcu(node); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1992 | tt_local_entry = container_of(tt_common_entry, |
| 1993 | struct tt_local_entry, |
| 1994 | common); |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1995 | tt_local_entry_free_ref(tt_local_entry); |
| 1996 | } |
| 1997 | spin_unlock_bh(list_lock); |
| 1998 | } |
| 1999 | |
| 2000 | } |
| 2001 | |
| 2002 | void tt_commit_changes(struct bat_priv *bat_priv) |
| 2003 | { |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 2004 | uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash, |
| 2005 | TT_CLIENT_NEW, false); |
| 2006 | /* all the reset entries have now to be effectively counted as local |
| 2007 | * entries */ |
| 2008 | atomic_add(changed_num, &bat_priv->num_local_tt); |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 2009 | tt_local_purge_pending_clients(bat_priv); |
| 2010 | |
| 2011 | /* Increment the TTVN only once per OGM interval */ |
| 2012 | atomic_inc(&bat_priv->ttvn); |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 2013 | bat_dbg(DBG_TT, bat_priv, "Local changes committed, updating to ttvn %u\n", |
| 2014 | (uint8_t)atomic_read(&bat_priv->ttvn)); |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 2015 | bat_priv->tt_poss_change = false; |
| 2016 | } |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 2017 | |
| 2018 | bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst) |
| 2019 | { |
| 2020 | struct tt_local_entry *tt_local_entry = NULL; |
| 2021 | struct tt_global_entry *tt_global_entry = NULL; |
| 2022 | bool ret = true; |
| 2023 | |
| 2024 | if (!atomic_read(&bat_priv->ap_isolation)) |
| 2025 | return false; |
| 2026 | |
| 2027 | tt_local_entry = tt_local_hash_find(bat_priv, dst); |
| 2028 | if (!tt_local_entry) |
| 2029 | goto out; |
| 2030 | |
| 2031 | tt_global_entry = tt_global_hash_find(bat_priv, src); |
| 2032 | if (!tt_global_entry) |
| 2033 | goto out; |
| 2034 | |
| 2035 | if (_is_ap_isolated(tt_local_entry, tt_global_entry)) |
| 2036 | goto out; |
| 2037 | |
| 2038 | ret = false; |
| 2039 | |
| 2040 | out: |
| 2041 | if (tt_global_entry) |
| 2042 | tt_global_entry_free_ref(tt_global_entry); |
| 2043 | if (tt_local_entry) |
| 2044 | tt_local_entry_free_ref(tt_local_entry); |
| 2045 | return ret; |
| 2046 | } |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 2047 | |
| 2048 | void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node, |
| 2049 | const unsigned char *tt_buff, uint8_t tt_num_changes, |
| 2050 | uint8_t ttvn, uint16_t tt_crc) |
| 2051 | { |
| 2052 | uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); |
| 2053 | bool full_table = true; |
| 2054 | |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 2055 | /* orig table not initialised AND first diff is in the OGM OR the ttvn |
| 2056 | * increased by one -> we can apply the attached changes */ |
| 2057 | if ((!orig_node->tt_initialised && ttvn == 1) || |
| 2058 | ttvn - orig_ttvn == 1) { |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 2059 | /* the OGM could not contain the changes due to their size or |
| 2060 | * because they have already been sent TT_OGM_APPEND_MAX times. |
| 2061 | * In this case send a tt request */ |
| 2062 | if (!tt_num_changes) { |
| 2063 | full_table = false; |
| 2064 | goto request_table; |
| 2065 | } |
| 2066 | |
| 2067 | tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn, |
| 2068 | (struct tt_change *)tt_buff); |
| 2069 | |
| 2070 | /* Even if we received the precomputed crc with the OGM, we |
| 2071 | * prefer to recompute it to spot any possible inconsistency |
| 2072 | * in the global table */ |
| 2073 | orig_node->tt_crc = tt_global_crc(bat_priv, orig_node); |
| 2074 | |
| 2075 | /* The ttvn alone is not enough to guarantee consistency |
| 2076 | * because a single value could represent different states |
| 2077 | * (due to the wrap around). Thus a node has to check whether |
| 2078 | * the resulting table (after applying the changes) is still |
| 2079 | * consistent or not. E.g. a node could disconnect while its |
| 2080 | * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case |
| 2081 | * checking the CRC value is mandatory to detect the |
| 2082 | * inconsistency */ |
| 2083 | if (orig_node->tt_crc != tt_crc) |
| 2084 | goto request_table; |
| 2085 | |
| 2086 | /* Roaming phase is over: tables are in sync again. I can |
| 2087 | * unset the flag */ |
| 2088 | orig_node->tt_poss_change = false; |
| 2089 | } else { |
| 2090 | /* if we missed more than one change or our tables are not |
| 2091 | * in sync anymore -> request fresh tt data */ |
Simon Wunderlich | db08e6e | 2011-10-22 20:12:51 +0200 | [diff] [blame^] | 2092 | |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 2093 | if (!orig_node->tt_initialised || ttvn != orig_ttvn || |
| 2094 | orig_node->tt_crc != tt_crc) { |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 2095 | request_table: |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 2096 | bat_dbg(DBG_TT, bat_priv, |
| 2097 | "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n", |
| 2098 | orig_node->orig, ttvn, orig_ttvn, tt_crc, |
| 2099 | orig_node->tt_crc, tt_num_changes); |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 2100 | send_tt_request(bat_priv, orig_node, ttvn, tt_crc, |
| 2101 | full_table); |
| 2102 | return; |
| 2103 | } |
| 2104 | } |
| 2105 | } |