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