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