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