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 */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 39 | static int compare_tt(const struct hlist_node *node, const void *data2) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 40 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 41 | const void *data1 = container_of(node, struct tt_common_entry, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 42 | hash_entry); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 43 | |
| 44 | return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); |
| 45 | } |
| 46 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 47 | static void tt_start_timer(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 48 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 49 | INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge); |
| 50 | queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work, |
| 51 | msecs_to_jiffies(5000)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 54 | static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash, |
| 55 | const void *data) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 56 | { |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 57 | struct hlist_head *head; |
| 58 | struct hlist_node *node; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 59 | struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 60 | uint32_t index; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 61 | |
| 62 | if (!hash) |
| 63 | return NULL; |
| 64 | |
| 65 | index = choose_orig(data, hash->size); |
| 66 | head = &hash->table[index]; |
| 67 | |
| 68 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 69 | hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) { |
| 70 | if (!compare_eth(tt_common_entry, data)) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 71 | continue; |
| 72 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 73 | if (!atomic_inc_not_zero(&tt_common_entry->refcount)) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 74 | continue; |
| 75 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 76 | tt_common_entry_tmp = tt_common_entry; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | rcu_read_unlock(); |
| 80 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 81 | return tt_common_entry_tmp; |
| 82 | } |
| 83 | |
| 84 | static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv, |
| 85 | const void *data) |
| 86 | { |
| 87 | struct tt_common_entry *tt_common_entry; |
| 88 | struct tt_local_entry *tt_local_entry = NULL; |
| 89 | |
| 90 | tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data); |
| 91 | if (tt_common_entry) |
| 92 | tt_local_entry = container_of(tt_common_entry, |
| 93 | struct tt_local_entry, common); |
| 94 | return tt_local_entry; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 97 | static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 98 | const void *data) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 99 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 100 | struct tt_common_entry *tt_common_entry; |
| 101 | struct tt_global_entry *tt_global_entry = NULL; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 102 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 103 | tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data); |
| 104 | if (tt_common_entry) |
| 105 | tt_global_entry = container_of(tt_common_entry, |
| 106 | struct tt_global_entry, common); |
| 107 | return tt_global_entry; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 108 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 111 | static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry) |
| 112 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 113 | if (atomic_dec_and_test(&tt_local_entry->common.refcount)) |
| 114 | kfree_rcu(tt_local_entry, common.rcu); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Simon Wunderlich | 531027f | 2011-10-19 11:02:25 +0200 | [diff] [blame] | 117 | static void tt_global_entry_free_rcu(struct rcu_head *rcu) |
| 118 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 119 | struct tt_common_entry *tt_common_entry; |
Simon Wunderlich | 531027f | 2011-10-19 11:02:25 +0200 | [diff] [blame] | 120 | struct tt_global_entry *tt_global_entry; |
| 121 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 122 | tt_common_entry = container_of(rcu, struct tt_common_entry, rcu); |
| 123 | tt_global_entry = container_of(tt_common_entry, struct tt_global_entry, |
| 124 | common); |
Simon Wunderlich | 531027f | 2011-10-19 11:02:25 +0200 | [diff] [blame] | 125 | |
| 126 | if (tt_global_entry->orig_node) |
| 127 | orig_node_free_ref(tt_global_entry->orig_node); |
| 128 | |
| 129 | kfree(tt_global_entry); |
| 130 | } |
| 131 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 132 | static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry) |
| 133 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 134 | if (atomic_dec_and_test(&tt_global_entry->common.refcount)) |
| 135 | call_rcu(&tt_global_entry->common.rcu, |
| 136 | tt_global_entry_free_rcu); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Antonio Quartulli | ff66c97 | 2011-06-30 01:14:00 +0200 | [diff] [blame] | 139 | static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr, |
| 140 | uint8_t flags) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 141 | { |
| 142 | struct tt_change_node *tt_change_node; |
| 143 | |
| 144 | tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC); |
| 145 | |
| 146 | if (!tt_change_node) |
| 147 | return; |
| 148 | |
Antonio Quartulli | ff66c97 | 2011-06-30 01:14:00 +0200 | [diff] [blame] | 149 | tt_change_node->change.flags = flags; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 150 | memcpy(tt_change_node->change.addr, addr, ETH_ALEN); |
| 151 | |
| 152 | spin_lock_bh(&bat_priv->tt_changes_list_lock); |
| 153 | /* track the change in the OGMinterval list */ |
| 154 | list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list); |
| 155 | atomic_inc(&bat_priv->tt_local_changes); |
| 156 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); |
| 157 | |
| 158 | atomic_set(&bat_priv->tt_ogm_append_cnt, 0); |
| 159 | } |
| 160 | |
| 161 | int tt_len(int changes_num) |
| 162 | { |
| 163 | return changes_num * sizeof(struct tt_change); |
| 164 | } |
| 165 | |
| 166 | static int tt_local_init(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 167 | { |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 168 | if (bat_priv->tt_local_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 169 | return 1; |
| 170 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 171 | bat_priv->tt_local_hash = hash_new(1024); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 172 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 173 | if (!bat_priv->tt_local_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 174 | return 0; |
| 175 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 176 | return 1; |
| 177 | } |
| 178 | |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 179 | void tt_local_add(struct net_device *soft_iface, const uint8_t *addr, |
| 180 | int ifindex) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 181 | { |
| 182 | struct bat_priv *bat_priv = netdev_priv(soft_iface); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 183 | struct tt_local_entry *tt_local_entry = NULL; |
| 184 | struct tt_global_entry *tt_global_entry = NULL; |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 185 | int hash_added; |
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 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 188 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 189 | if (tt_local_entry) { |
| 190 | tt_local_entry->last_seen = jiffies; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 191 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 194 | tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 195 | if (!tt_local_entry) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 196 | goto out; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 197 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 198 | bat_dbg(DBG_TT, bat_priv, |
| 199 | "Creating new local tt entry: %pM (ttvn: %d)\n", addr, |
| 200 | (uint8_t)atomic_read(&bat_priv->ttvn)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 201 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 202 | memcpy(tt_local_entry->common.addr, addr, ETH_ALEN); |
| 203 | tt_local_entry->common.flags = NO_FLAGS; |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 204 | if (is_wifi_iface(ifindex)) |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 205 | tt_local_entry->common.flags |= TT_CLIENT_WIFI; |
| 206 | atomic_set(&tt_local_entry->common.refcount, 2); |
| 207 | tt_local_entry->last_seen = jiffies; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 208 | |
| 209 | /* the batman interface mac address should never be purged */ |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 210 | if (compare_eth(addr, soft_iface->dev_addr)) |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 211 | tt_local_entry->common.flags |= TT_CLIENT_NOPURGE; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 212 | |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 213 | hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig, |
| 214 | &tt_local_entry->common, |
| 215 | &tt_local_entry->common.hash_entry); |
| 216 | |
| 217 | if (unlikely(hash_added != 0)) { |
| 218 | /* remove the reference for the hash */ |
| 219 | tt_local_entry_free_ref(tt_local_entry); |
| 220 | goto out; |
| 221 | } |
| 222 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 223 | tt_local_event(bat_priv, addr, tt_local_entry->common.flags); |
Antonio Quartulli | ff66c97 | 2011-06-30 01:14:00 +0200 | [diff] [blame] | 224 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 225 | /* The local entry has to be marked as NEW to avoid to send it in |
| 226 | * a full table response going out before the next ttvn increment |
| 227 | * (consistency check) */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 228 | tt_local_entry->common.flags |= TT_CLIENT_NEW; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 229 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 230 | /* remove address from global hash if present */ |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 231 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 232 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 233 | /* Check whether it is a roaming! */ |
| 234 | if (tt_global_entry) { |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 235 | /* This node is probably going to update its tt table */ |
| 236 | tt_global_entry->orig_node->tt_poss_change = true; |
Antonio Quartulli | 03fc307 | 2011-12-04 12:26:50 +0100 | [diff] [blame] | 237 | /* The global entry has to be marked as ROAMING and has to be |
Antonio Quartulli | 980d55b | 2011-07-07 01:40:59 +0200 | [diff] [blame] | 238 | * kept for consistency purpose */ |
David S. Miller | 220b07e | 2011-12-16 15:07:28 -0500 | [diff] [blame] | 239 | tt_global_entry->common.flags |= TT_CLIENT_ROAM; |
Antonio Quartulli | 03fc307 | 2011-12-04 12:26:50 +0100 | [diff] [blame] | 240 | tt_global_entry->roam_at = jiffies; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 241 | send_roam_adv(bat_priv, tt_global_entry->common.addr, |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 242 | tt_global_entry->orig_node); |
| 243 | } |
| 244 | out: |
| 245 | if (tt_local_entry) |
| 246 | tt_local_entry_free_ref(tt_local_entry); |
| 247 | if (tt_global_entry) |
| 248 | tt_global_entry_free_ref(tt_global_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 251 | int tt_changes_fill_buffer(struct bat_priv *bat_priv, |
| 252 | unsigned char *buff, int buff_len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 253 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 254 | int count = 0, tot_changes = 0; |
| 255 | struct tt_change_node *entry, *safe; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 256 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 257 | if (buff_len > 0) |
| 258 | tot_changes = buff_len / tt_len(1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 259 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 260 | spin_lock_bh(&bat_priv->tt_changes_list_lock); |
| 261 | atomic_set(&bat_priv->tt_local_changes, 0); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 262 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 263 | list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list, |
| 264 | list) { |
| 265 | if (count < tot_changes) { |
| 266 | memcpy(buff + tt_len(count), |
| 267 | &entry->change, sizeof(struct tt_change)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 268 | count++; |
| 269 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 270 | list_del(&entry->list); |
| 271 | kfree(entry); |
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 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 274 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 275 | /* Keep the buffer for possible tt_request */ |
| 276 | spin_lock_bh(&bat_priv->tt_buff_lock); |
| 277 | kfree(bat_priv->tt_buff); |
| 278 | bat_priv->tt_buff_len = 0; |
| 279 | bat_priv->tt_buff = NULL; |
| 280 | /* We check whether this new OGM has no changes due to size |
| 281 | * problems */ |
| 282 | if (buff_len > 0) { |
| 283 | /** |
| 284 | * if kmalloc() fails we will reply with the full table |
| 285 | * instead of providing the diff |
| 286 | */ |
| 287 | bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC); |
| 288 | if (bat_priv->tt_buff) { |
| 289 | memcpy(bat_priv->tt_buff, buff, buff_len); |
| 290 | bat_priv->tt_buff_len = buff_len; |
| 291 | } |
| 292 | } |
| 293 | spin_unlock_bh(&bat_priv->tt_buff_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 294 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 295 | return tot_changes; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 298 | int tt_local_seq_print_text(struct seq_file *seq, void *offset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 299 | { |
| 300 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 301 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 302 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 303 | struct tt_common_entry *tt_common_entry; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 304 | struct hard_iface *primary_if; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 305 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 306 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 307 | uint32_t i; |
| 308 | int ret = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 309 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 310 | primary_if = primary_if_get_selected(bat_priv); |
| 311 | if (!primary_if) { |
| 312 | ret = seq_printf(seq, "BATMAN mesh %s disabled - " |
| 313 | "please specify interfaces to enable it\n", |
| 314 | net_dev->name); |
| 315 | goto out; |
| 316 | } |
| 317 | |
| 318 | if (primary_if->if_status != IF_ACTIVE) { |
| 319 | ret = seq_printf(seq, "BATMAN mesh %s disabled - " |
| 320 | "primary interface not active\n", |
| 321 | net_dev->name); |
| 322 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | seq_printf(seq, "Locally retrieved addresses (from %s) " |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 326 | "announced via TT (TTVN: %u):\n", |
| 327 | net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 328 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 329 | for (i = 0; i < hash->size; i++) { |
| 330 | head = &hash->table[i]; |
| 331 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 332 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 333 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 334 | head, hash_entry) { |
Simon Wunderlich | d099c2c | 2011-10-22 18:15:26 +0200 | [diff] [blame] | 335 | seq_printf(seq, " * %pM [%c%c%c%c%c]\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 336 | tt_common_entry->addr, |
| 337 | (tt_common_entry->flags & |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 338 | TT_CLIENT_ROAM ? 'R' : '.'), |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 339 | (tt_common_entry->flags & |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 340 | TT_CLIENT_NOPURGE ? 'P' : '.'), |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 341 | (tt_common_entry->flags & |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 342 | TT_CLIENT_NEW ? 'N' : '.'), |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 343 | (tt_common_entry->flags & |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 344 | TT_CLIENT_PENDING ? 'X' : '.'), |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 345 | (tt_common_entry->flags & |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 346 | TT_CLIENT_WIFI ? 'W' : '.')); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 347 | } |
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 | } |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 350 | out: |
| 351 | if (primary_if) |
| 352 | hardif_free_ref(primary_if); |
| 353 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 356 | static void tt_local_set_pending(struct bat_priv *bat_priv, |
| 357 | struct tt_local_entry *tt_local_entry, |
| 358 | uint16_t flags) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 359 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 360 | tt_local_event(bat_priv, tt_local_entry->common.addr, |
| 361 | tt_local_entry->common.flags | flags); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 362 | |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 363 | /* The local client has to be marked as "pending to be removed" but has |
| 364 | * 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] | 365 | * response issued before the net ttvn increment (consistency check) */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 366 | tt_local_entry->common.flags |= TT_CLIENT_PENDING; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 369 | 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] | 370 | const char *message, bool roaming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 371 | { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 372 | struct tt_local_entry *tt_local_entry = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 373 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 374 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 375 | if (!tt_local_entry) |
| 376 | goto out; |
| 377 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 378 | tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL | |
| 379 | (roaming ? TT_CLIENT_ROAM : NO_FLAGS)); |
| 380 | |
| 381 | bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: " |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 382 | "%s\n", tt_local_entry->common.addr, message); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 383 | out: |
| 384 | if (tt_local_entry) |
| 385 | tt_local_entry_free_ref(tt_local_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 388 | static void tt_local_purge(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 389 | { |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 390 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
| 391 | struct tt_local_entry *tt_local_entry; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 392 | struct tt_common_entry *tt_common_entry; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 393 | struct hlist_node *node, *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 394 | struct hlist_head *head; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 395 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 396 | uint32_t i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 397 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 398 | for (i = 0; i < hash->size; i++) { |
| 399 | head = &hash->table[i]; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 400 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 401 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 402 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 403 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 404 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 405 | tt_local_entry = container_of(tt_common_entry, |
| 406 | struct tt_local_entry, |
| 407 | common); |
| 408 | if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 409 | continue; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 410 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 411 | /* entry already marked for deletion */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 412 | if (tt_local_entry->common.flags & TT_CLIENT_PENDING) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 413 | continue; |
| 414 | |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame^] | 415 | if (!has_timed_out(tt_local_entry->last_seen, |
| 416 | TT_LOCAL_TIMEOUT * 1000)) |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 417 | continue; |
| 418 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 419 | tt_local_set_pending(bat_priv, tt_local_entry, |
| 420 | TT_CLIENT_DEL); |
| 421 | bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) " |
| 422 | "pending to be removed: timed out\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 423 | tt_local_entry->common.addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 424 | } |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 425 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 430 | static void tt_local_table_free(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 431 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 432 | struct hashtable_t *hash; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 433 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 434 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 435 | struct tt_local_entry *tt_local_entry; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 436 | struct hlist_node *node, *node_tmp; |
| 437 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 438 | uint32_t i; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 439 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 440 | if (!bat_priv->tt_local_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 441 | return; |
| 442 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 443 | hash = bat_priv->tt_local_hash; |
| 444 | |
| 445 | for (i = 0; i < hash->size; i++) { |
| 446 | head = &hash->table[i]; |
| 447 | list_lock = &hash->list_locks[i]; |
| 448 | |
| 449 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 450 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 451 | head, hash_entry) { |
| 452 | hlist_del_rcu(node); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 453 | tt_local_entry = container_of(tt_common_entry, |
| 454 | struct tt_local_entry, |
| 455 | common); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 456 | tt_local_entry_free_ref(tt_local_entry); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 457 | } |
| 458 | spin_unlock_bh(list_lock); |
| 459 | } |
| 460 | |
| 461 | hash_destroy(hash); |
| 462 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 463 | bat_priv->tt_local_hash = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 466 | static int tt_global_init(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 467 | { |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 468 | if (bat_priv->tt_global_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 469 | return 1; |
| 470 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 471 | bat_priv->tt_global_hash = hash_new(1024); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 472 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 473 | if (!bat_priv->tt_global_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | |
| 476 | return 1; |
| 477 | } |
| 478 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 479 | static void tt_changes_list_free(struct bat_priv *bat_priv) |
| 480 | { |
| 481 | struct tt_change_node *entry, *safe; |
| 482 | |
| 483 | spin_lock_bh(&bat_priv->tt_changes_list_lock); |
| 484 | |
| 485 | list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list, |
| 486 | list) { |
| 487 | list_del(&entry->list); |
| 488 | kfree(entry); |
| 489 | } |
| 490 | |
| 491 | atomic_set(&bat_priv->tt_local_changes, 0); |
| 492 | spin_unlock_bh(&bat_priv->tt_changes_list_lock); |
| 493 | } |
| 494 | |
| 495 | /* caller must hold orig_node refcount */ |
| 496 | 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] | 497 | const unsigned char *tt_addr, uint8_t ttvn, bool roaming, |
| 498 | bool wifi) |
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 | struct tt_global_entry *tt_global_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 501 | struct orig_node *orig_node_tmp; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 502 | int ret = 0; |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 503 | int hash_added; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 504 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 505 | tt_global_entry = tt_global_hash_find(bat_priv, tt_addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 506 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 507 | if (!tt_global_entry) { |
| 508 | tt_global_entry = |
| 509 | kmalloc(sizeof(*tt_global_entry), |
| 510 | GFP_ATOMIC); |
| 511 | if (!tt_global_entry) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 512 | goto out; |
| 513 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 514 | memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN); |
| 515 | tt_global_entry->common.flags = NO_FLAGS; |
| 516 | atomic_set(&tt_global_entry->common.refcount, 2); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 517 | /* Assign the new orig_node */ |
| 518 | atomic_inc(&orig_node->refcount); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 519 | tt_global_entry->orig_node = orig_node; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 520 | tt_global_entry->ttvn = ttvn; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 521 | tt_global_entry->roam_at = 0; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 522 | |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 523 | hash_added = hash_add(bat_priv->tt_global_hash, compare_tt, |
| 524 | choose_orig, &tt_global_entry->common, |
| 525 | &tt_global_entry->common.hash_entry); |
| 526 | |
| 527 | if (unlikely(hash_added != 0)) { |
| 528 | /* remove the reference for the hash */ |
| 529 | tt_global_entry_free_ref(tt_global_entry); |
| 530 | goto out_remove; |
| 531 | } |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 532 | atomic_inc(&orig_node->tt_size); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 533 | } else { |
| 534 | if (tt_global_entry->orig_node != orig_node) { |
| 535 | atomic_dec(&tt_global_entry->orig_node->tt_size); |
| 536 | orig_node_tmp = tt_global_entry->orig_node; |
| 537 | atomic_inc(&orig_node->refcount); |
| 538 | tt_global_entry->orig_node = orig_node; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 539 | orig_node_free_ref(orig_node_tmp); |
| 540 | atomic_inc(&orig_node->tt_size); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 541 | } |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 542 | tt_global_entry->common.flags = NO_FLAGS; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 543 | tt_global_entry->ttvn = ttvn; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 544 | tt_global_entry->roam_at = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 545 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 546 | |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 547 | if (wifi) |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 548 | tt_global_entry->common.flags |= TT_CLIENT_WIFI; |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 549 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 550 | bat_dbg(DBG_TT, bat_priv, |
| 551 | "Creating new global tt entry: %pM (via %pM)\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 552 | tt_global_entry->common.addr, orig_node->orig); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 553 | |
Simon Wunderlich | 80b3f58 | 2011-11-02 20:26:45 +0100 | [diff] [blame] | 554 | out_remove: |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 555 | /* remove address from local hash if present */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 556 | tt_local_remove(bat_priv, tt_global_entry->common.addr, |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 557 | "global tt received", roaming); |
| 558 | ret = 1; |
| 559 | out: |
| 560 | if (tt_global_entry) |
| 561 | tt_global_entry_free_ref(tt_global_entry); |
| 562 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 565 | int tt_global_seq_print_text(struct seq_file *seq, void *offset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 566 | { |
| 567 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 568 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 569 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 570 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 571 | struct tt_global_entry *tt_global_entry; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 572 | struct hard_iface *primary_if; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 573 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 574 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 575 | uint32_t i; |
| 576 | int ret = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 577 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 578 | primary_if = primary_if_get_selected(bat_priv); |
| 579 | if (!primary_if) { |
| 580 | ret = seq_printf(seq, "BATMAN mesh %s disabled - please " |
| 581 | "specify interfaces to enable it\n", |
| 582 | net_dev->name); |
| 583 | goto out; |
| 584 | } |
| 585 | |
| 586 | if (primary_if->if_status != IF_ACTIVE) { |
| 587 | ret = seq_printf(seq, "BATMAN mesh %s disabled - " |
| 588 | "primary interface not active\n", |
| 589 | net_dev->name); |
| 590 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 593 | seq_printf(seq, |
| 594 | "Globally announced TT entries received via the mesh %s\n", |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 595 | net_dev->name); |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 596 | seq_printf(seq, " %-13s %s %-15s %s %s\n", |
| 597 | "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 598 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 599 | for (i = 0; i < hash->size; i++) { |
| 600 | head = &hash->table[i]; |
| 601 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 602 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 603 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 604 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 605 | tt_global_entry = container_of(tt_common_entry, |
| 606 | struct tt_global_entry, |
| 607 | common); |
Simon Wunderlich | d099c2c | 2011-10-22 18:15:26 +0200 | [diff] [blame] | 608 | seq_printf(seq, " * %pM (%3u) via %pM (%3u) " |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 609 | "[%c%c%c]\n", |
| 610 | tt_global_entry->common.addr, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 611 | tt_global_entry->ttvn, |
| 612 | tt_global_entry->orig_node->orig, |
| 613 | (uint8_t) atomic_read( |
| 614 | &tt_global_entry->orig_node-> |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 615 | last_ttvn), |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 616 | (tt_global_entry->common.flags & |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 617 | TT_CLIENT_ROAM ? 'R' : '.'), |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 618 | (tt_global_entry->common.flags & |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 619 | TT_CLIENT_PENDING ? 'X' : '.'), |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 620 | (tt_global_entry->common.flags & |
Antonio Quartulli | df6edb9 | 2011-07-07 15:35:38 +0200 | [diff] [blame] | 621 | TT_CLIENT_WIFI ? 'W' : '.')); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 622 | } |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 623 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 624 | } |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 625 | out: |
| 626 | if (primary_if) |
| 627 | hardif_free_ref(primary_if); |
| 628 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 631 | static void _tt_global_del(struct bat_priv *bat_priv, |
| 632 | struct tt_global_entry *tt_global_entry, |
| 633 | const char *message) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 634 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 635 | if (!tt_global_entry) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 636 | goto out; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 637 | |
| 638 | bat_dbg(DBG_TT, bat_priv, |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 639 | "Deleting global tt entry %pM (via %pM): %s\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 640 | tt_global_entry->common.addr, tt_global_entry->orig_node->orig, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 641 | message); |
| 642 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 643 | atomic_dec(&tt_global_entry->orig_node->tt_size); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 644 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 645 | hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig, |
| 646 | tt_global_entry->common.addr); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 647 | out: |
| 648 | if (tt_global_entry) |
| 649 | tt_global_entry_free_ref(tt_global_entry); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 652 | void tt_global_del(struct bat_priv *bat_priv, |
| 653 | struct orig_node *orig_node, const unsigned char *addr, |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 654 | const char *message, bool roaming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 655 | { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 656 | struct tt_global_entry *tt_global_entry = NULL; |
Antonio Quartulli | 797399b | 2011-12-04 22:38:27 +0100 | [diff] [blame] | 657 | struct tt_local_entry *tt_local_entry = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 658 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 659 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 660 | if (!tt_global_entry) |
| 661 | goto out; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 662 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 663 | if (tt_global_entry->orig_node == orig_node) { |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 664 | if (roaming) { |
Antonio Quartulli | 797399b | 2011-12-04 22:38:27 +0100 | [diff] [blame] | 665 | /* if we are deleting a global entry due to a roam |
| 666 | * event, there are two possibilities: |
| 667 | * 1) the client roamed from node A to node B => we mark |
| 668 | * it with TT_CLIENT_ROAM, we start a timer and we |
| 669 | * wait for node B to claim it. In case of timeout |
| 670 | * the entry is purged. |
| 671 | * 2) the client roamed to us => we can directly delete |
| 672 | * the global entry, since it is useless now. */ |
| 673 | tt_local_entry = tt_local_hash_find(bat_priv, |
David S. Miller | b26e478 | 2011-12-16 02:11:14 -0500 | [diff] [blame] | 674 | tt_global_entry->common.addr); |
Antonio Quartulli | 797399b | 2011-12-04 22:38:27 +0100 | [diff] [blame] | 675 | if (!tt_local_entry) { |
David S. Miller | b26e478 | 2011-12-16 02:11:14 -0500 | [diff] [blame] | 676 | tt_global_entry->common.flags |= TT_CLIENT_ROAM; |
Antonio Quartulli | 797399b | 2011-12-04 22:38:27 +0100 | [diff] [blame] | 677 | tt_global_entry->roam_at = jiffies; |
| 678 | goto out; |
| 679 | } |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 680 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 681 | _tt_global_del(bat_priv, tt_global_entry, message); |
| 682 | } |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 683 | out: |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 684 | if (tt_global_entry) |
| 685 | tt_global_entry_free_ref(tt_global_entry); |
Antonio Quartulli | 797399b | 2011-12-04 22:38:27 +0100 | [diff] [blame] | 686 | if (tt_local_entry) |
| 687 | tt_local_entry_free_ref(tt_local_entry); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | void tt_global_del_orig(struct bat_priv *bat_priv, |
| 691 | struct orig_node *orig_node, const char *message) |
| 692 | { |
| 693 | struct tt_global_entry *tt_global_entry; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 694 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 695 | uint32_t i; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 696 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
| 697 | struct hlist_node *node, *safe; |
| 698 | struct hlist_head *head; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 699 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 700 | |
Simon Wunderlich | 6e80149 | 2011-10-19 10:28:26 +0200 | [diff] [blame] | 701 | if (!hash) |
| 702 | return; |
| 703 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 704 | for (i = 0; i < hash->size; i++) { |
| 705 | head = &hash->table[i]; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 706 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 707 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 708 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 709 | hlist_for_each_entry_safe(tt_common_entry, node, safe, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 710 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 711 | tt_global_entry = container_of(tt_common_entry, |
| 712 | struct tt_global_entry, |
| 713 | common); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 714 | if (tt_global_entry->orig_node == orig_node) { |
| 715 | bat_dbg(DBG_TT, bat_priv, |
| 716 | "Deleting global tt entry %pM " |
Antonio Quartulli | 8794497 | 2011-09-19 12:29:19 +0200 | [diff] [blame] | 717 | "(via %pM): %s\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 718 | tt_global_entry->common.addr, |
Antonio Quartulli | 8794497 | 2011-09-19 12:29:19 +0200 | [diff] [blame] | 719 | tt_global_entry->orig_node->orig, |
| 720 | message); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 721 | hlist_del_rcu(node); |
| 722 | tt_global_entry_free_ref(tt_global_entry); |
| 723 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 724 | } |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 725 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 726 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 727 | atomic_set(&orig_node->tt_size, 0); |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 728 | orig_node->tt_initialised = false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 731 | static void tt_global_roam_purge(struct bat_priv *bat_priv) |
| 732 | { |
| 733 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 734 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 735 | struct tt_global_entry *tt_global_entry; |
| 736 | struct hlist_node *node, *node_tmp; |
| 737 | struct hlist_head *head; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 738 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 739 | uint32_t i; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 740 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 741 | for (i = 0; i < hash->size; i++) { |
| 742 | head = &hash->table[i]; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 743 | list_lock = &hash->list_locks[i]; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 744 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 745 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 746 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 747 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 748 | tt_global_entry = container_of(tt_common_entry, |
| 749 | struct tt_global_entry, |
| 750 | common); |
| 751 | if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM)) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 752 | continue; |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame^] | 753 | if (!has_timed_out(tt_global_entry->roam_at, |
| 754 | TT_CLIENT_ROAM_TIMEOUT * 1000)) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 755 | continue; |
| 756 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 757 | bat_dbg(DBG_TT, bat_priv, "Deleting global " |
| 758 | "tt entry (%pM): Roaming timeout\n", |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 759 | tt_global_entry->common.addr); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 760 | atomic_dec(&tt_global_entry->orig_node->tt_size); |
| 761 | hlist_del_rcu(node); |
| 762 | tt_global_entry_free_ref(tt_global_entry); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 763 | } |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 764 | spin_unlock_bh(list_lock); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 767 | } |
| 768 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 769 | static void tt_global_table_free(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 770 | { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 771 | struct hashtable_t *hash; |
| 772 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 773 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 774 | struct tt_global_entry *tt_global_entry; |
| 775 | struct hlist_node *node, *node_tmp; |
| 776 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 777 | uint32_t i; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 778 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 779 | if (!bat_priv->tt_global_hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 780 | return; |
| 781 | |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 782 | hash = bat_priv->tt_global_hash; |
| 783 | |
| 784 | for (i = 0; i < hash->size; i++) { |
| 785 | head = &hash->table[i]; |
| 786 | list_lock = &hash->list_locks[i]; |
| 787 | |
| 788 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 789 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 790 | head, hash_entry) { |
| 791 | hlist_del_rcu(node); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 792 | tt_global_entry = container_of(tt_common_entry, |
| 793 | struct tt_global_entry, |
| 794 | common); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 795 | tt_global_entry_free_ref(tt_global_entry); |
| 796 | } |
| 797 | spin_unlock_bh(list_lock); |
| 798 | } |
| 799 | |
| 800 | hash_destroy(hash); |
| 801 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 802 | bat_priv->tt_global_hash = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 805 | static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry, |
| 806 | struct tt_global_entry *tt_global_entry) |
| 807 | { |
| 808 | bool ret = false; |
| 809 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 810 | if (tt_local_entry->common.flags & TT_CLIENT_WIFI && |
| 811 | tt_global_entry->common.flags & TT_CLIENT_WIFI) |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 812 | ret = true; |
| 813 | |
| 814 | return ret; |
| 815 | } |
| 816 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 817 | struct orig_node *transtable_search(struct bat_priv *bat_priv, |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 818 | const uint8_t *src, const uint8_t *addr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 819 | { |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 820 | struct tt_local_entry *tt_local_entry = NULL; |
| 821 | struct tt_global_entry *tt_global_entry = NULL; |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 822 | struct orig_node *orig_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 823 | |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 824 | if (src && atomic_read(&bat_priv->ap_isolation)) { |
| 825 | tt_local_entry = tt_local_hash_find(bat_priv, src); |
| 826 | if (!tt_local_entry) |
| 827 | goto out; |
| 828 | } |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 829 | |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 830 | tt_global_entry = tt_global_hash_find(bat_priv, addr); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 831 | if (!tt_global_entry) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 832 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 833 | |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 834 | /* check whether the clients should not communicate due to AP |
| 835 | * isolation */ |
| 836 | if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry)) |
| 837 | goto out; |
| 838 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 839 | if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount)) |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 840 | goto out; |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 841 | |
Antonio Quartulli | 980d55b | 2011-07-07 01:40:59 +0200 | [diff] [blame] | 842 | /* A global client marked as PENDING has already moved from that |
| 843 | * originator */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 844 | if (tt_global_entry->common.flags & TT_CLIENT_PENDING) |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 845 | goto out; |
Antonio Quartulli | 980d55b | 2011-07-07 01:40:59 +0200 | [diff] [blame] | 846 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 847 | orig_node = tt_global_entry->orig_node; |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 848 | |
| 849 | out: |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 850 | if (tt_global_entry) |
| 851 | tt_global_entry_free_ref(tt_global_entry); |
| 852 | if (tt_local_entry) |
| 853 | tt_local_entry_free_ref(tt_local_entry); |
| 854 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 855 | return orig_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 856 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 857 | |
| 858 | /* Calculates the checksum of the local table of a given orig_node */ |
| 859 | uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node) |
| 860 | { |
| 861 | uint16_t total = 0, total_one; |
| 862 | struct hashtable_t *hash = bat_priv->tt_global_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 863 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 864 | struct tt_global_entry *tt_global_entry; |
| 865 | struct hlist_node *node; |
| 866 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 867 | uint32_t i; |
| 868 | int j; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 869 | |
| 870 | for (i = 0; i < hash->size; i++) { |
| 871 | head = &hash->table[i]; |
| 872 | |
| 873 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 874 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 875 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 876 | tt_global_entry = container_of(tt_common_entry, |
| 877 | struct tt_global_entry, |
| 878 | common); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 879 | if (compare_eth(tt_global_entry->orig_node, |
| 880 | orig_node)) { |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 881 | /* Roaming clients are in the global table for |
| 882 | * consistency only. They don't have to be |
| 883 | * taken into account while computing the |
| 884 | * global crc */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 885 | if (tt_common_entry->flags & TT_CLIENT_ROAM) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 886 | continue; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 887 | total_one = 0; |
| 888 | for (j = 0; j < ETH_ALEN; j++) |
| 889 | total_one = crc16_byte(total_one, |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 890 | tt_common_entry->addr[j]); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 891 | total ^= total_one; |
| 892 | } |
| 893 | } |
| 894 | rcu_read_unlock(); |
| 895 | } |
| 896 | |
| 897 | return total; |
| 898 | } |
| 899 | |
| 900 | /* Calculates the checksum of the local table */ |
| 901 | uint16_t tt_local_crc(struct bat_priv *bat_priv) |
| 902 | { |
| 903 | uint16_t total = 0, total_one; |
| 904 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 905 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 906 | struct hlist_node *node; |
| 907 | struct hlist_head *head; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 908 | uint32_t i; |
| 909 | int j; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 910 | |
| 911 | for (i = 0; i < hash->size; i++) { |
| 912 | head = &hash->table[i]; |
| 913 | |
| 914 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 915 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 916 | head, hash_entry) { |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 917 | /* not yet committed clients have not to be taken into |
| 918 | * account while computing the CRC */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 919 | if (tt_common_entry->flags & TT_CLIENT_NEW) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 920 | continue; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 921 | total_one = 0; |
| 922 | for (j = 0; j < ETH_ALEN; j++) |
| 923 | total_one = crc16_byte(total_one, |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 924 | tt_common_entry->addr[j]); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 925 | total ^= total_one; |
| 926 | } |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 927 | rcu_read_unlock(); |
| 928 | } |
| 929 | |
| 930 | return total; |
| 931 | } |
| 932 | |
| 933 | static void tt_req_list_free(struct bat_priv *bat_priv) |
| 934 | { |
| 935 | struct tt_req_node *node, *safe; |
| 936 | |
| 937 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 938 | |
| 939 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { |
| 940 | list_del(&node->list); |
| 941 | kfree(node); |
| 942 | } |
| 943 | |
| 944 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 945 | } |
| 946 | |
| 947 | void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node, |
| 948 | const unsigned char *tt_buff, uint8_t tt_num_changes) |
| 949 | { |
| 950 | uint16_t tt_buff_len = tt_len(tt_num_changes); |
| 951 | |
| 952 | /* Replace the old buffer only if I received something in the |
| 953 | * last OGM (the OGM could carry no changes) */ |
| 954 | spin_lock_bh(&orig_node->tt_buff_lock); |
| 955 | if (tt_buff_len > 0) { |
| 956 | kfree(orig_node->tt_buff); |
| 957 | orig_node->tt_buff_len = 0; |
| 958 | orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC); |
| 959 | if (orig_node->tt_buff) { |
| 960 | memcpy(orig_node->tt_buff, tt_buff, tt_buff_len); |
| 961 | orig_node->tt_buff_len = tt_buff_len; |
| 962 | } |
| 963 | } |
| 964 | spin_unlock_bh(&orig_node->tt_buff_lock); |
| 965 | } |
| 966 | |
| 967 | static void tt_req_purge(struct bat_priv *bat_priv) |
| 968 | { |
| 969 | struct tt_req_node *node, *safe; |
| 970 | |
| 971 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 972 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame^] | 973 | if (has_timed_out(node->issued_at, |
| 974 | TT_REQUEST_TIMEOUT * 1000)) { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 975 | list_del(&node->list); |
| 976 | kfree(node); |
| 977 | } |
| 978 | } |
| 979 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 980 | } |
| 981 | |
| 982 | /* returns the pointer to the new tt_req_node struct if no request |
| 983 | * has already been issued for this orig_node, NULL otherwise */ |
| 984 | static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv, |
| 985 | struct orig_node *orig_node) |
| 986 | { |
| 987 | struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL; |
| 988 | |
| 989 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 990 | list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) { |
| 991 | if (compare_eth(tt_req_node_tmp, orig_node) && |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame^] | 992 | !has_timed_out(tt_req_node_tmp->issued_at, |
| 993 | TT_REQUEST_TIMEOUT * 1000)) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 994 | goto unlock; |
| 995 | } |
| 996 | |
| 997 | tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC); |
| 998 | if (!tt_req_node) |
| 999 | goto unlock; |
| 1000 | |
| 1001 | memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN); |
| 1002 | tt_req_node->issued_at = jiffies; |
| 1003 | |
| 1004 | list_add(&tt_req_node->list, &bat_priv->tt_req_list); |
| 1005 | unlock: |
| 1006 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 1007 | return tt_req_node; |
| 1008 | } |
| 1009 | |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1010 | /* data_ptr is useless here, but has to be kept to respect the prototype */ |
| 1011 | static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr) |
| 1012 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1013 | const struct tt_common_entry *tt_common_entry = entry_ptr; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1014 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1015 | if (tt_common_entry->flags & TT_CLIENT_NEW) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1016 | return 0; |
| 1017 | return 1; |
| 1018 | } |
| 1019 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1020 | static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr) |
| 1021 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1022 | const struct tt_common_entry *tt_common_entry = entry_ptr; |
| 1023 | const struct tt_global_entry *tt_global_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1024 | const struct orig_node *orig_node = data_ptr; |
| 1025 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1026 | if (tt_common_entry->flags & TT_CLIENT_ROAM) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1027 | return 0; |
| 1028 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1029 | tt_global_entry = container_of(tt_common_entry, struct tt_global_entry, |
| 1030 | common); |
| 1031 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1032 | return (tt_global_entry->orig_node == orig_node); |
| 1033 | } |
| 1034 | |
| 1035 | static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn, |
| 1036 | struct hashtable_t *hash, |
| 1037 | struct hard_iface *primary_if, |
| 1038 | int (*valid_cb)(const void *, |
| 1039 | const void *), |
| 1040 | void *cb_data) |
| 1041 | { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1042 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1043 | struct tt_query_packet *tt_response; |
| 1044 | struct tt_change *tt_change; |
| 1045 | struct hlist_node *node; |
| 1046 | struct hlist_head *head; |
| 1047 | struct sk_buff *skb = NULL; |
| 1048 | uint16_t tt_tot, tt_count; |
| 1049 | ssize_t tt_query_size = sizeof(struct tt_query_packet); |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1050 | uint32_t i; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1051 | |
| 1052 | if (tt_query_size + tt_len > primary_if->soft_iface->mtu) { |
| 1053 | tt_len = primary_if->soft_iface->mtu - tt_query_size; |
| 1054 | tt_len -= tt_len % sizeof(struct tt_change); |
| 1055 | } |
| 1056 | tt_tot = tt_len / sizeof(struct tt_change); |
| 1057 | |
| 1058 | skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN); |
| 1059 | if (!skb) |
| 1060 | goto out; |
| 1061 | |
| 1062 | skb_reserve(skb, ETH_HLEN); |
| 1063 | tt_response = (struct tt_query_packet *)skb_put(skb, |
| 1064 | tt_query_size + tt_len); |
| 1065 | tt_response->ttvn = ttvn; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1066 | |
| 1067 | tt_change = (struct tt_change *)(skb->data + tt_query_size); |
| 1068 | tt_count = 0; |
| 1069 | |
| 1070 | rcu_read_lock(); |
| 1071 | for (i = 0; i < hash->size; i++) { |
| 1072 | head = &hash->table[i]; |
| 1073 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1074 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1075 | head, hash_entry) { |
| 1076 | if (tt_count == tt_tot) |
| 1077 | break; |
| 1078 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1079 | if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data))) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1080 | continue; |
| 1081 | |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1082 | memcpy(tt_change->addr, tt_common_entry->addr, |
| 1083 | ETH_ALEN); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 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 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1130 | tt_request->header.packet_type = BAT_TT_QUERY; |
| 1131 | tt_request->header.version = COMPAT_VERSION; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1132 | memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN); |
| 1133 | memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN); |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1134 | tt_request->header.ttl = TTL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 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 */ |
Antonio Quartulli | eb7e2a1 | 2011-10-12 14:54:50 +0200 | [diff] [blame] | 1190 | req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1191 | if (!req_dst_orig_node) |
| 1192 | goto out; |
| 1193 | |
Antonio Quartulli | eb7e2a1 | 2011-10-12 14:54:50 +0200 | [diff] [blame] | 1194 | res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 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 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1260 | tt_response->header.packet_type = BAT_TT_QUERY; |
| 1261 | tt_response->header.version = COMPAT_VERSION; |
| 1262 | tt_response->header.ttl = TTL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 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 | |
Antonio Quartulli | eb7e2a1 | 2011-10-12 14:54:50 +0200 | [diff] [blame] | 1320 | orig_node = orig_hash_find(bat_priv, tt_request->src); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 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 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1377 | tt_response->header.packet_type = BAT_TT_QUERY; |
| 1378 | tt_response->header.version = COMPAT_VERSION; |
| 1379 | tt_response->header.ttl = TTL; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 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 | } |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 1446 | orig_node->tt_initialised = true; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | static void tt_fill_gtable(struct bat_priv *bat_priv, |
| 1450 | struct tt_query_packet *tt_response) |
| 1451 | { |
| 1452 | struct orig_node *orig_node = NULL; |
| 1453 | |
| 1454 | orig_node = orig_hash_find(bat_priv, tt_response->src); |
| 1455 | if (!orig_node) |
| 1456 | goto out; |
| 1457 | |
| 1458 | /* Purge the old table first.. */ |
| 1459 | tt_global_del_orig(bat_priv, orig_node, "Received full table"); |
| 1460 | |
| 1461 | _tt_update_changes(bat_priv, orig_node, |
| 1462 | (struct tt_change *)(tt_response + 1), |
| 1463 | tt_response->tt_data, tt_response->ttvn); |
| 1464 | |
| 1465 | spin_lock_bh(&orig_node->tt_buff_lock); |
| 1466 | kfree(orig_node->tt_buff); |
| 1467 | orig_node->tt_buff_len = 0; |
| 1468 | orig_node->tt_buff = NULL; |
| 1469 | spin_unlock_bh(&orig_node->tt_buff_lock); |
| 1470 | |
| 1471 | atomic_set(&orig_node->last_ttvn, tt_response->ttvn); |
| 1472 | |
| 1473 | out: |
| 1474 | if (orig_node) |
| 1475 | orig_node_free_ref(orig_node); |
| 1476 | } |
| 1477 | |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 1478 | static void tt_update_changes(struct bat_priv *bat_priv, |
| 1479 | struct orig_node *orig_node, |
| 1480 | uint16_t tt_num_changes, uint8_t ttvn, |
| 1481 | struct tt_change *tt_change) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1482 | { |
| 1483 | _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes, |
| 1484 | ttvn); |
| 1485 | |
| 1486 | tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change, |
| 1487 | tt_num_changes); |
| 1488 | atomic_set(&orig_node->last_ttvn, ttvn); |
| 1489 | } |
| 1490 | |
| 1491 | bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr) |
| 1492 | { |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 1493 | struct tt_local_entry *tt_local_entry = NULL; |
| 1494 | bool ret = false; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1495 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1496 | tt_local_entry = tt_local_hash_find(bat_priv, addr); |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 1497 | if (!tt_local_entry) |
| 1498 | goto out; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1499 | /* Check if the client has been logically deleted (but is kept for |
| 1500 | * consistency purpose) */ |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1501 | if (tt_local_entry->common.flags & TT_CLIENT_PENDING) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1502 | goto out; |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 1503 | ret = true; |
| 1504 | out: |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1505 | if (tt_local_entry) |
Antonio Quartulli | 7683fdc | 2011-04-27 14:28:07 +0200 | [diff] [blame] | 1506 | tt_local_entry_free_ref(tt_local_entry); |
| 1507 | return ret; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | void handle_tt_response(struct bat_priv *bat_priv, |
| 1511 | struct tt_query_packet *tt_response) |
| 1512 | { |
| 1513 | struct tt_req_node *node, *safe; |
| 1514 | struct orig_node *orig_node = NULL; |
| 1515 | |
| 1516 | bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for " |
| 1517 | "ttvn %d t_size: %d [%c]\n", |
| 1518 | tt_response->src, tt_response->ttvn, |
| 1519 | tt_response->tt_data, |
| 1520 | (tt_response->flags & TT_FULL_TABLE ? 'F' : '.')); |
| 1521 | |
| 1522 | orig_node = orig_hash_find(bat_priv, tt_response->src); |
| 1523 | if (!orig_node) |
| 1524 | goto out; |
| 1525 | |
| 1526 | if (tt_response->flags & TT_FULL_TABLE) |
| 1527 | tt_fill_gtable(bat_priv, tt_response); |
| 1528 | else |
| 1529 | tt_update_changes(bat_priv, orig_node, tt_response->tt_data, |
| 1530 | tt_response->ttvn, |
| 1531 | (struct tt_change *)(tt_response + 1)); |
| 1532 | |
| 1533 | /* Delete the tt_req_node from pending tt_requests list */ |
| 1534 | spin_lock_bh(&bat_priv->tt_req_list_lock); |
| 1535 | list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) { |
| 1536 | if (!compare_eth(node->addr, tt_response->src)) |
| 1537 | continue; |
| 1538 | list_del(&node->list); |
| 1539 | kfree(node); |
| 1540 | } |
| 1541 | spin_unlock_bh(&bat_priv->tt_req_list_lock); |
| 1542 | |
| 1543 | /* Recalculate the CRC for this orig_node and store it */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1544 | orig_node->tt_crc = tt_global_crc(bat_priv, orig_node); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1545 | /* Roaming phase is over: tables are in sync again. I can |
| 1546 | * unset the flag */ |
| 1547 | orig_node->tt_poss_change = false; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1548 | out: |
| 1549 | if (orig_node) |
| 1550 | orig_node_free_ref(orig_node); |
| 1551 | } |
| 1552 | |
| 1553 | int tt_init(struct bat_priv *bat_priv) |
| 1554 | { |
| 1555 | if (!tt_local_init(bat_priv)) |
| 1556 | return 0; |
| 1557 | |
| 1558 | if (!tt_global_init(bat_priv)) |
| 1559 | return 0; |
| 1560 | |
| 1561 | tt_start_timer(bat_priv); |
| 1562 | |
| 1563 | return 1; |
| 1564 | } |
| 1565 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1566 | static void tt_roam_list_free(struct bat_priv *bat_priv) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1567 | { |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1568 | struct tt_roam_node *node, *safe; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1569 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1570 | spin_lock_bh(&bat_priv->tt_roam_list_lock); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1571 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1572 | list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) { |
| 1573 | list_del(&node->list); |
| 1574 | kfree(node); |
| 1575 | } |
| 1576 | |
| 1577 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); |
| 1578 | } |
| 1579 | |
| 1580 | static void tt_roam_purge(struct bat_priv *bat_priv) |
| 1581 | { |
| 1582 | struct tt_roam_node *node, *safe; |
| 1583 | |
| 1584 | spin_lock_bh(&bat_priv->tt_roam_list_lock); |
| 1585 | list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) { |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame^] | 1586 | if (!has_timed_out(node->first_time, |
| 1587 | ROAMING_MAX_TIME * 1000)) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1588 | continue; |
| 1589 | |
| 1590 | list_del(&node->list); |
| 1591 | kfree(node); |
| 1592 | } |
| 1593 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); |
| 1594 | } |
| 1595 | |
| 1596 | /* This function checks whether the client already reached the |
| 1597 | * maximum number of possible roaming phases. In this case the ROAMING_ADV |
| 1598 | * will not be sent. |
| 1599 | * |
| 1600 | * returns true if the ROAMING_ADV can be sent, false otherwise */ |
| 1601 | static bool tt_check_roam_count(struct bat_priv *bat_priv, |
| 1602 | uint8_t *client) |
| 1603 | { |
| 1604 | struct tt_roam_node *tt_roam_node; |
| 1605 | bool ret = false; |
| 1606 | |
| 1607 | spin_lock_bh(&bat_priv->tt_roam_list_lock); |
| 1608 | /* The new tt_req will be issued only if I'm not waiting for a |
| 1609 | * reply from the same orig_node yet */ |
| 1610 | list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) { |
| 1611 | if (!compare_eth(tt_roam_node->addr, client)) |
| 1612 | continue; |
| 1613 | |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame^] | 1614 | if (has_timed_out(tt_roam_node->first_time, |
| 1615 | ROAMING_MAX_TIME * 1000)) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1616 | continue; |
| 1617 | |
| 1618 | if (!atomic_dec_not_zero(&tt_roam_node->counter)) |
| 1619 | /* Sorry, you roamed too many times! */ |
| 1620 | goto unlock; |
| 1621 | ret = true; |
| 1622 | break; |
| 1623 | } |
| 1624 | |
| 1625 | if (!ret) { |
| 1626 | tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC); |
| 1627 | if (!tt_roam_node) |
| 1628 | goto unlock; |
| 1629 | |
| 1630 | tt_roam_node->first_time = jiffies; |
| 1631 | atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1); |
| 1632 | memcpy(tt_roam_node->addr, client, ETH_ALEN); |
| 1633 | |
| 1634 | list_add(&tt_roam_node->list, &bat_priv->tt_roam_list); |
| 1635 | ret = true; |
| 1636 | } |
| 1637 | |
| 1638 | unlock: |
| 1639 | spin_unlock_bh(&bat_priv->tt_roam_list_lock); |
| 1640 | return ret; |
| 1641 | } |
| 1642 | |
| 1643 | void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client, |
| 1644 | struct orig_node *orig_node) |
| 1645 | { |
| 1646 | struct neigh_node *neigh_node = NULL; |
| 1647 | struct sk_buff *skb = NULL; |
| 1648 | struct roam_adv_packet *roam_adv_packet; |
| 1649 | int ret = 1; |
| 1650 | struct hard_iface *primary_if; |
| 1651 | |
| 1652 | /* before going on we have to check whether the client has |
| 1653 | * already roamed to us too many times */ |
| 1654 | if (!tt_check_roam_count(bat_priv, client)) |
| 1655 | goto out; |
| 1656 | |
| 1657 | skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN); |
| 1658 | if (!skb) |
| 1659 | goto out; |
| 1660 | |
| 1661 | skb_reserve(skb, ETH_HLEN); |
| 1662 | |
| 1663 | roam_adv_packet = (struct roam_adv_packet *)skb_put(skb, |
| 1664 | sizeof(struct roam_adv_packet)); |
| 1665 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1666 | roam_adv_packet->header.packet_type = BAT_ROAM_ADV; |
| 1667 | roam_adv_packet->header.version = COMPAT_VERSION; |
| 1668 | roam_adv_packet->header.ttl = TTL; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1669 | primary_if = primary_if_get_selected(bat_priv); |
| 1670 | if (!primary_if) |
| 1671 | goto out; |
| 1672 | memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN); |
| 1673 | hardif_free_ref(primary_if); |
| 1674 | memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN); |
| 1675 | memcpy(roam_adv_packet->client, client, ETH_ALEN); |
| 1676 | |
| 1677 | neigh_node = orig_node_get_router(orig_node); |
| 1678 | if (!neigh_node) |
| 1679 | goto out; |
| 1680 | |
| 1681 | bat_dbg(DBG_TT, bat_priv, |
| 1682 | "Sending ROAMING_ADV to %pM (client %pM) via %pM\n", |
| 1683 | orig_node->orig, client, neigh_node->addr); |
| 1684 | |
| 1685 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
| 1686 | ret = 0; |
| 1687 | |
| 1688 | out: |
| 1689 | if (neigh_node) |
| 1690 | neigh_node_free_ref(neigh_node); |
| 1691 | if (ret) |
| 1692 | kfree_skb(skb); |
| 1693 | return; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | static void tt_purge(struct work_struct *work) |
| 1697 | { |
| 1698 | struct delayed_work *delayed_work = |
| 1699 | container_of(work, struct delayed_work, work); |
| 1700 | struct bat_priv *bat_priv = |
| 1701 | container_of(delayed_work, struct bat_priv, tt_work); |
| 1702 | |
| 1703 | tt_local_purge(bat_priv); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1704 | tt_global_roam_purge(bat_priv); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1705 | tt_req_purge(bat_priv); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1706 | tt_roam_purge(bat_priv); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1707 | |
| 1708 | tt_start_timer(bat_priv); |
| 1709 | } |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1710 | |
| 1711 | void tt_free(struct bat_priv *bat_priv) |
| 1712 | { |
| 1713 | cancel_delayed_work_sync(&bat_priv->tt_work); |
| 1714 | |
| 1715 | tt_local_table_free(bat_priv); |
| 1716 | tt_global_table_free(bat_priv); |
| 1717 | tt_req_list_free(bat_priv); |
| 1718 | tt_changes_list_free(bat_priv); |
| 1719 | tt_roam_list_free(bat_priv); |
| 1720 | |
| 1721 | kfree(bat_priv->tt_buff); |
| 1722 | } |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1723 | |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1724 | /* This function will enable or disable the specified flags for all the entries |
| 1725 | * in the given hash table and returns the number of modified entries */ |
| 1726 | static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags, |
| 1727 | bool enable) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1728 | { |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1729 | uint32_t i; |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1730 | uint16_t changed_num = 0; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1731 | struct hlist_head *head; |
| 1732 | struct hlist_node *node; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1733 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1734 | |
| 1735 | if (!hash) |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1736 | goto out; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1737 | |
| 1738 | for (i = 0; i < hash->size; i++) { |
| 1739 | head = &hash->table[i]; |
| 1740 | |
| 1741 | rcu_read_lock(); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1742 | hlist_for_each_entry_rcu(tt_common_entry, node, |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1743 | head, hash_entry) { |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1744 | if (enable) { |
| 1745 | if ((tt_common_entry->flags & flags) == flags) |
| 1746 | continue; |
| 1747 | tt_common_entry->flags |= flags; |
| 1748 | } else { |
| 1749 | if (!(tt_common_entry->flags & flags)) |
| 1750 | continue; |
| 1751 | tt_common_entry->flags &= ~flags; |
| 1752 | } |
| 1753 | changed_num++; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1754 | } |
| 1755 | rcu_read_unlock(); |
| 1756 | } |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1757 | out: |
| 1758 | return changed_num; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */ |
| 1762 | static void tt_local_purge_pending_clients(struct bat_priv *bat_priv) |
| 1763 | { |
| 1764 | struct hashtable_t *hash = bat_priv->tt_local_hash; |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1765 | struct tt_common_entry *tt_common_entry; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1766 | struct tt_local_entry *tt_local_entry; |
| 1767 | struct hlist_node *node, *node_tmp; |
| 1768 | struct hlist_head *head; |
| 1769 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 1770 | uint32_t i; |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1771 | |
| 1772 | if (!hash) |
| 1773 | return; |
| 1774 | |
| 1775 | for (i = 0; i < hash->size; i++) { |
| 1776 | head = &hash->table[i]; |
| 1777 | list_lock = &hash->list_locks[i]; |
| 1778 | |
| 1779 | spin_lock_bh(list_lock); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1780 | hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1781 | head, hash_entry) { |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1782 | if (!(tt_common_entry->flags & TT_CLIENT_PENDING)) |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1783 | continue; |
| 1784 | |
| 1785 | bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry " |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1786 | "(%pM): pending\n", tt_common_entry->addr); |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1787 | |
| 1788 | atomic_dec(&bat_priv->num_local_tt); |
| 1789 | hlist_del_rcu(node); |
Antonio Quartulli | 48100ba | 2011-10-30 12:17:33 +0100 | [diff] [blame] | 1790 | tt_local_entry = container_of(tt_common_entry, |
| 1791 | struct tt_local_entry, |
| 1792 | common); |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1793 | tt_local_entry_free_ref(tt_local_entry); |
| 1794 | } |
| 1795 | spin_unlock_bh(list_lock); |
| 1796 | } |
| 1797 | |
| 1798 | } |
| 1799 | |
| 1800 | void tt_commit_changes(struct bat_priv *bat_priv) |
| 1801 | { |
Antonio Quartulli | 697f253 | 2011-11-07 16:47:01 +0100 | [diff] [blame] | 1802 | uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash, |
| 1803 | TT_CLIENT_NEW, false); |
| 1804 | /* all the reset entries have now to be effectively counted as local |
| 1805 | * entries */ |
| 1806 | atomic_add(changed_num, &bat_priv->num_local_tt); |
Antonio Quartulli | 058d0e2 | 2011-07-07 01:40:58 +0200 | [diff] [blame] | 1807 | tt_local_purge_pending_clients(bat_priv); |
| 1808 | |
| 1809 | /* Increment the TTVN only once per OGM interval */ |
| 1810 | atomic_inc(&bat_priv->ttvn); |
| 1811 | bat_priv->tt_poss_change = false; |
| 1812 | } |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 1813 | |
| 1814 | bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst) |
| 1815 | { |
| 1816 | struct tt_local_entry *tt_local_entry = NULL; |
| 1817 | struct tt_global_entry *tt_global_entry = NULL; |
| 1818 | bool ret = true; |
| 1819 | |
| 1820 | if (!atomic_read(&bat_priv->ap_isolation)) |
| 1821 | return false; |
| 1822 | |
| 1823 | tt_local_entry = tt_local_hash_find(bat_priv, dst); |
| 1824 | if (!tt_local_entry) |
| 1825 | goto out; |
| 1826 | |
| 1827 | tt_global_entry = tt_global_hash_find(bat_priv, src); |
| 1828 | if (!tt_global_entry) |
| 1829 | goto out; |
| 1830 | |
| 1831 | if (_is_ap_isolated(tt_local_entry, tt_global_entry)) |
| 1832 | goto out; |
| 1833 | |
| 1834 | ret = false; |
| 1835 | |
| 1836 | out: |
| 1837 | if (tt_global_entry) |
| 1838 | tt_global_entry_free_ref(tt_global_entry); |
| 1839 | if (tt_local_entry) |
| 1840 | tt_local_entry_free_ref(tt_local_entry); |
| 1841 | return ret; |
| 1842 | } |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 1843 | |
| 1844 | void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node, |
| 1845 | const unsigned char *tt_buff, uint8_t tt_num_changes, |
| 1846 | uint8_t ttvn, uint16_t tt_crc) |
| 1847 | { |
| 1848 | uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); |
| 1849 | bool full_table = true; |
| 1850 | |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 1851 | /* orig table not initialised AND first diff is in the OGM OR the ttvn |
| 1852 | * increased by one -> we can apply the attached changes */ |
| 1853 | if ((!orig_node->tt_initialised && ttvn == 1) || |
| 1854 | ttvn - orig_ttvn == 1) { |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 1855 | /* the OGM could not contain the changes due to their size or |
| 1856 | * because they have already been sent TT_OGM_APPEND_MAX times. |
| 1857 | * In this case send a tt request */ |
| 1858 | if (!tt_num_changes) { |
| 1859 | full_table = false; |
| 1860 | goto request_table; |
| 1861 | } |
| 1862 | |
| 1863 | tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn, |
| 1864 | (struct tt_change *)tt_buff); |
| 1865 | |
| 1866 | /* Even if we received the precomputed crc with the OGM, we |
| 1867 | * prefer to recompute it to spot any possible inconsistency |
| 1868 | * in the global table */ |
| 1869 | orig_node->tt_crc = tt_global_crc(bat_priv, orig_node); |
| 1870 | |
| 1871 | /* The ttvn alone is not enough to guarantee consistency |
| 1872 | * because a single value could represent different states |
| 1873 | * (due to the wrap around). Thus a node has to check whether |
| 1874 | * the resulting table (after applying the changes) is still |
| 1875 | * consistent or not. E.g. a node could disconnect while its |
| 1876 | * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case |
| 1877 | * checking the CRC value is mandatory to detect the |
| 1878 | * inconsistency */ |
| 1879 | if (orig_node->tt_crc != tt_crc) |
| 1880 | goto request_table; |
| 1881 | |
| 1882 | /* Roaming phase is over: tables are in sync again. I can |
| 1883 | * unset the flag */ |
| 1884 | orig_node->tt_poss_change = false; |
| 1885 | } else { |
| 1886 | /* if we missed more than one change or our tables are not |
| 1887 | * in sync anymore -> request fresh tt data */ |
Antonio Quartulli | 1707157 | 2011-11-07 16:36:40 +0100 | [diff] [blame] | 1888 | if (!orig_node->tt_initialised || ttvn != orig_ttvn || |
| 1889 | orig_node->tt_crc != tt_crc) { |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 1890 | request_table: |
| 1891 | bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. " |
| 1892 | "Need to retrieve the correct information " |
| 1893 | "(ttvn: %u last_ttvn: %u crc: %u last_crc: " |
| 1894 | "%u num_changes: %u)\n", orig_node->orig, ttvn, |
| 1895 | orig_ttvn, tt_crc, orig_node->tt_crc, |
| 1896 | tt_num_changes); |
| 1897 | send_tt_request(bat_priv, orig_node, ttvn, tt_crc, |
| 1898 | full_table); |
| 1899 | return; |
| 1900 | } |
| 1901 | } |
| 1902 | } |