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