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