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