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