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