Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2011-2012 B.A.T.M.A.N. contributors: |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 2 | * |
| 3 | * Simon Wunderlich |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of version 2 of the GNU General Public |
| 7 | * License as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301, USA |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "main.h" |
| 21 | #include "hash.h" |
| 22 | #include "hard-interface.h" |
| 23 | #include "originator.h" |
| 24 | #include "bridge_loop_avoidance.h" |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 25 | #include "translation-table.h" |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 26 | #include "send.h" |
| 27 | |
| 28 | #include <linux/etherdevice.h> |
| 29 | #include <linux/crc16.h> |
| 30 | #include <linux/if_arp.h> |
| 31 | #include <net/arp.h> |
| 32 | #include <linux/if_vlan.h> |
| 33 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 34 | static const uint8_t batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05}; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 35 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 36 | static void batadv_bla_periodic_work(struct work_struct *work); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 37 | static void batadv_bla_send_announce(struct batadv_priv *bat_priv, |
| 38 | struct batadv_backbone_gw *backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 39 | |
| 40 | /* return the index of the claim */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 41 | static inline uint32_t batadv_choose_claim(const void *data, uint32_t size) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 42 | { |
| 43 | const unsigned char *key = data; |
| 44 | uint32_t hash = 0; |
| 45 | size_t i; |
| 46 | |
| 47 | for (i = 0; i < ETH_ALEN + sizeof(short); i++) { |
| 48 | hash += key[i]; |
| 49 | hash += (hash << 10); |
| 50 | hash ^= (hash >> 6); |
| 51 | } |
| 52 | |
| 53 | hash += (hash << 3); |
| 54 | hash ^= (hash >> 11); |
| 55 | hash += (hash << 15); |
| 56 | |
| 57 | return hash % size; |
| 58 | } |
| 59 | |
| 60 | /* return the index of the backbone gateway */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 61 | static inline uint32_t batadv_choose_backbone_gw(const void *data, |
| 62 | uint32_t size) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 63 | { |
| 64 | const unsigned char *key = data; |
| 65 | uint32_t hash = 0; |
| 66 | size_t i; |
| 67 | |
| 68 | for (i = 0; i < ETH_ALEN + sizeof(short); i++) { |
| 69 | hash += key[i]; |
| 70 | hash += (hash << 10); |
| 71 | hash ^= (hash >> 6); |
| 72 | } |
| 73 | |
| 74 | hash += (hash << 3); |
| 75 | hash ^= (hash >> 11); |
| 76 | hash += (hash << 15); |
| 77 | |
| 78 | return hash % size; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /* compares address and vid of two backbone gws */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 83 | static int batadv_compare_backbone_gw(const struct hlist_node *node, |
| 84 | const void *data2) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 85 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 86 | const void *data1 = container_of(node, struct batadv_backbone_gw, |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 87 | hash_entry); |
| 88 | |
| 89 | return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0); |
| 90 | } |
| 91 | |
| 92 | /* compares address and vid of two claims */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 93 | static int batadv_compare_claim(const struct hlist_node *node, |
| 94 | const void *data2) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 95 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 96 | const void *data1 = container_of(node, struct batadv_claim, |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 97 | hash_entry); |
| 98 | |
| 99 | return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0); |
| 100 | } |
| 101 | |
| 102 | /* free a backbone gw */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 103 | static void batadv_backbone_gw_free_ref(struct batadv_backbone_gw *backbone_gw) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 104 | { |
| 105 | if (atomic_dec_and_test(&backbone_gw->refcount)) |
| 106 | kfree_rcu(backbone_gw, rcu); |
| 107 | } |
| 108 | |
| 109 | /* finally deinitialize the claim */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 110 | static void batadv_claim_free_rcu(struct rcu_head *rcu) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 111 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 112 | struct batadv_claim *claim; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 113 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 114 | claim = container_of(rcu, struct batadv_claim, rcu); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 115 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 116 | batadv_backbone_gw_free_ref(claim->backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 117 | kfree(claim); |
| 118 | } |
| 119 | |
| 120 | /* free a claim, call claim_free_rcu if its the last reference */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 121 | static void batadv_claim_free_ref(struct batadv_claim *claim) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 122 | { |
| 123 | if (atomic_dec_and_test(&claim->refcount)) |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 124 | call_rcu(&claim->rcu, batadv_claim_free_rcu); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 127 | /* @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 128 | * @data: search data (may be local/static data) |
| 129 | * |
| 130 | * looks for a claim in the hash, and returns it if found |
| 131 | * or NULL otherwise. |
| 132 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 133 | static struct batadv_claim *batadv_claim_hash_find(struct batadv_priv *bat_priv, |
| 134 | struct batadv_claim *data) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 135 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 136 | struct batadv_hashtable *hash = bat_priv->claim_hash; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 137 | struct hlist_head *head; |
| 138 | struct hlist_node *node; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 139 | struct batadv_claim *claim; |
| 140 | struct batadv_claim *claim_tmp = NULL; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 141 | int index; |
| 142 | |
| 143 | if (!hash) |
| 144 | return NULL; |
| 145 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 146 | index = batadv_choose_claim(data, hash->size); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 147 | head = &hash->table[index]; |
| 148 | |
| 149 | rcu_read_lock(); |
| 150 | hlist_for_each_entry_rcu(claim, node, head, hash_entry) { |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 151 | if (!batadv_compare_claim(&claim->hash_entry, data)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 152 | continue; |
| 153 | |
| 154 | if (!atomic_inc_not_zero(&claim->refcount)) |
| 155 | continue; |
| 156 | |
| 157 | claim_tmp = claim; |
| 158 | break; |
| 159 | } |
| 160 | rcu_read_unlock(); |
| 161 | |
| 162 | return claim_tmp; |
| 163 | } |
| 164 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 165 | /** |
| 166 | * batadv_backbone_hash_find - looks for a claim in the hash |
| 167 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 168 | * @addr: the address of the originator |
| 169 | * @vid: the VLAN ID |
| 170 | * |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 171 | * Returns claim if found or NULL otherwise. |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 172 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 173 | static struct batadv_backbone_gw * |
| 174 | batadv_backbone_hash_find(struct batadv_priv *bat_priv, |
| 175 | uint8_t *addr, short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 176 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 177 | struct batadv_hashtable *hash = bat_priv->backbone_hash; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 178 | struct hlist_head *head; |
| 179 | struct hlist_node *node; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 180 | struct batadv_backbone_gw search_entry, *backbone_gw; |
| 181 | struct batadv_backbone_gw *backbone_gw_tmp = NULL; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 182 | int index; |
| 183 | |
| 184 | if (!hash) |
| 185 | return NULL; |
| 186 | |
| 187 | memcpy(search_entry.orig, addr, ETH_ALEN); |
| 188 | search_entry.vid = vid; |
| 189 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 190 | index = batadv_choose_backbone_gw(&search_entry, hash->size); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 191 | head = &hash->table[index]; |
| 192 | |
| 193 | rcu_read_lock(); |
| 194 | hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) { |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 195 | if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry, |
| 196 | &search_entry)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 197 | continue; |
| 198 | |
| 199 | if (!atomic_inc_not_zero(&backbone_gw->refcount)) |
| 200 | continue; |
| 201 | |
| 202 | backbone_gw_tmp = backbone_gw; |
| 203 | break; |
| 204 | } |
| 205 | rcu_read_unlock(); |
| 206 | |
| 207 | return backbone_gw_tmp; |
| 208 | } |
| 209 | |
| 210 | /* delete all claims for a backbone */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 211 | static void |
| 212 | batadv_bla_del_backbone_claims(struct batadv_backbone_gw *backbone_gw) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 213 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 214 | struct batadv_hashtable *hash; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 215 | struct hlist_node *node, *node_tmp; |
| 216 | struct hlist_head *head; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 217 | struct batadv_claim *claim; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 218 | int i; |
| 219 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
| 220 | |
| 221 | hash = backbone_gw->bat_priv->claim_hash; |
| 222 | if (!hash) |
| 223 | return; |
| 224 | |
| 225 | for (i = 0; i < hash->size; i++) { |
| 226 | head = &hash->table[i]; |
| 227 | list_lock = &hash->list_locks[i]; |
| 228 | |
| 229 | spin_lock_bh(list_lock); |
| 230 | hlist_for_each_entry_safe(claim, node, node_tmp, |
| 231 | head, hash_entry) { |
| 232 | |
| 233 | if (claim->backbone_gw != backbone_gw) |
| 234 | continue; |
| 235 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 236 | batadv_claim_free_ref(claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 237 | hlist_del_rcu(node); |
| 238 | } |
| 239 | spin_unlock_bh(list_lock); |
| 240 | } |
| 241 | |
| 242 | /* all claims gone, intialize CRC */ |
Sven Eckelmann | 3964f72 | 2012-06-03 22:19:10 +0200 | [diff] [blame] | 243 | backbone_gw->crc = BATADV_BLA_CRC_INIT; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 246 | /** |
| 247 | * batadv_bla_send_claim - sends a claim frame according to the provided info |
| 248 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 249 | * @orig: the mac address to be announced within the claim |
| 250 | * @vid: the VLAN ID |
| 251 | * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 252 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 253 | static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 254 | short vid, int claimtype) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 255 | { |
| 256 | struct sk_buff *skb; |
| 257 | struct ethhdr *ethhdr; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 258 | struct batadv_hard_iface *primary_if; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 259 | struct net_device *soft_iface; |
| 260 | uint8_t *hw_src; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 261 | struct batadv_bla_claim_dst local_claim_dest; |
Al Viro | 3e2f1a1 | 2012-04-22 07:47:50 +0100 | [diff] [blame] | 262 | __be32 zeroip = 0; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 263 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 264 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 265 | if (!primary_if) |
| 266 | return; |
| 267 | |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 268 | memcpy(&local_claim_dest, &bat_priv->claim_dest, |
| 269 | sizeof(local_claim_dest)); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 270 | local_claim_dest.type = claimtype; |
| 271 | |
| 272 | soft_iface = primary_if->soft_iface; |
| 273 | |
| 274 | skb = arp_create(ARPOP_REPLY, ETH_P_ARP, |
| 275 | /* IP DST: 0.0.0.0 */ |
| 276 | zeroip, |
| 277 | primary_if->soft_iface, |
| 278 | /* IP SRC: 0.0.0.0 */ |
| 279 | zeroip, |
| 280 | /* Ethernet DST: Broadcast */ |
| 281 | NULL, |
| 282 | /* Ethernet SRC/HW SRC: originator mac */ |
| 283 | primary_if->net_dev->dev_addr, |
Simon Wunderlich | 99e966f | 2012-06-23 12:34:17 +0200 | [diff] [blame] | 284 | /* HW DST: FF:43:05:XX:YY:YY |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 285 | * with XX = claim type |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 286 | * and YY:YY = group id |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 287 | */ |
| 288 | (uint8_t *)&local_claim_dest); |
| 289 | |
| 290 | if (!skb) |
| 291 | goto out; |
| 292 | |
| 293 | ethhdr = (struct ethhdr *)skb->data; |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 294 | hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 295 | |
| 296 | /* now we pretend that the client would have sent this ... */ |
| 297 | switch (claimtype) { |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 298 | case BATADV_CLAIM_TYPE_CLAIM: |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 299 | /* normal claim frame |
| 300 | * set Ethernet SRC to the clients mac |
| 301 | */ |
| 302 | memcpy(ethhdr->h_source, mac, ETH_ALEN); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 303 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 304 | "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 305 | break; |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 306 | case BATADV_CLAIM_TYPE_UNCLAIM: |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 307 | /* unclaim frame |
| 308 | * set HW SRC to the clients mac |
| 309 | */ |
| 310 | memcpy(hw_src, mac, ETH_ALEN); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 311 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 312 | "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac, |
| 313 | vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 314 | break; |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 315 | case BATADV_CLAIM_TYPE_ANNOUNCE: |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 316 | /* announcement frame |
| 317 | * set HW SRC to the special mac containg the crc |
| 318 | */ |
| 319 | memcpy(hw_src, mac, ETH_ALEN); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 320 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 321 | "bla_send_claim(): ANNOUNCE of %pM on vid %d\n", |
| 322 | ethhdr->h_source, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 323 | break; |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 324 | case BATADV_CLAIM_TYPE_REQUEST: |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 325 | /* request frame |
Simon Wunderlich | 99e966f | 2012-06-23 12:34:17 +0200 | [diff] [blame] | 326 | * set HW SRC and header destination to the receiving backbone |
| 327 | * gws mac |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 328 | */ |
| 329 | memcpy(hw_src, mac, ETH_ALEN); |
| 330 | memcpy(ethhdr->h_dest, mac, ETH_ALEN); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 331 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 332 | "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n", |
| 333 | ethhdr->h_source, ethhdr->h_dest, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 334 | break; |
| 335 | |
| 336 | } |
| 337 | |
| 338 | if (vid != -1) |
| 339 | skb = vlan_insert_tag(skb, vid); |
| 340 | |
| 341 | skb_reset_mac_header(skb); |
| 342 | skb->protocol = eth_type_trans(skb, soft_iface); |
| 343 | bat_priv->stats.rx_packets++; |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 344 | bat_priv->stats.rx_bytes += skb->len + ETH_HLEN; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 345 | soft_iface->last_rx = jiffies; |
| 346 | |
| 347 | netif_rx(skb); |
| 348 | out: |
| 349 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 350 | batadv_hardif_free_ref(primary_if); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 353 | /** |
| 354 | * batadv_bla_get_backbone_gw |
| 355 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 356 | * @orig: the mac address of the originator |
| 357 | * @vid: the VLAN ID |
| 358 | * |
| 359 | * searches for the backbone gw or creates a new one if it could not |
| 360 | * be found. |
| 361 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 362 | static struct batadv_backbone_gw * |
| 363 | batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig, |
| 364 | short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 365 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 366 | struct batadv_backbone_gw *entry; |
| 367 | struct batadv_orig_node *orig_node; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 368 | int hash_added; |
| 369 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 370 | entry = batadv_backbone_hash_find(bat_priv, orig, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 371 | |
| 372 | if (entry) |
| 373 | return entry; |
| 374 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 375 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 376 | "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n", |
| 377 | orig, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 378 | |
| 379 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
| 380 | if (!entry) |
| 381 | return NULL; |
| 382 | |
| 383 | entry->vid = vid; |
| 384 | entry->lasttime = jiffies; |
Sven Eckelmann | 3964f72 | 2012-06-03 22:19:10 +0200 | [diff] [blame] | 385 | entry->crc = BATADV_BLA_CRC_INIT; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 386 | entry->bat_priv = bat_priv; |
| 387 | atomic_set(&entry->request_sent, 0); |
| 388 | memcpy(entry->orig, orig, ETH_ALEN); |
| 389 | |
| 390 | /* one for the hash, one for returning */ |
| 391 | atomic_set(&entry->refcount, 2); |
| 392 | |
Sven Eckelmann | c0a5592 | 2012-05-12 13:48:55 +0200 | [diff] [blame] | 393 | hash_added = batadv_hash_add(bat_priv->backbone_hash, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 394 | batadv_compare_backbone_gw, |
| 395 | batadv_choose_backbone_gw, entry, |
| 396 | &entry->hash_entry); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 397 | |
| 398 | if (unlikely(hash_added != 0)) { |
| 399 | /* hash failed, free the structure */ |
| 400 | kfree(entry); |
| 401 | return NULL; |
| 402 | } |
| 403 | |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 404 | /* this is a gateway now, remove any tt entries */ |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 405 | orig_node = batadv_orig_hash_find(bat_priv, orig); |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 406 | if (orig_node) { |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 407 | batadv_tt_global_del_orig(bat_priv, orig_node, |
| 408 | "became a backbone gateway"); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 409 | batadv_orig_node_free_ref(orig_node); |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 410 | } |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 411 | return entry; |
| 412 | } |
| 413 | |
| 414 | /* update or add the own backbone gw to make sure we announce |
| 415 | * where we receive other backbone gws |
| 416 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 417 | static void |
| 418 | batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv, |
| 419 | struct batadv_hard_iface *primary_if, |
| 420 | short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 421 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 422 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 423 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 424 | backbone_gw = batadv_bla_get_backbone_gw(bat_priv, |
| 425 | primary_if->net_dev->dev_addr, |
| 426 | vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 427 | if (unlikely(!backbone_gw)) |
| 428 | return; |
| 429 | |
| 430 | backbone_gw->lasttime = jiffies; |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 431 | batadv_backbone_gw_free_ref(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 434 | /* @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 435 | * @vid: the vid where the request came on |
| 436 | * |
| 437 | * Repeat all of our own claims, and finally send an ANNOUNCE frame |
| 438 | * to allow the requester another check if the CRC is correct now. |
| 439 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 440 | static void batadv_bla_answer_request(struct batadv_priv *bat_priv, |
| 441 | struct batadv_hard_iface *primary_if, |
| 442 | short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 443 | { |
| 444 | struct hlist_node *node; |
| 445 | struct hlist_head *head; |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 446 | struct batadv_hashtable *hash; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 447 | struct batadv_claim *claim; |
| 448 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 449 | int i; |
| 450 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 451 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 452 | "bla_answer_request(): received a claim request, send all of our own claims again\n"); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 453 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 454 | backbone_gw = batadv_backbone_hash_find(bat_priv, |
| 455 | primary_if->net_dev->dev_addr, |
| 456 | vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 457 | if (!backbone_gw) |
| 458 | return; |
| 459 | |
| 460 | hash = bat_priv->claim_hash; |
| 461 | for (i = 0; i < hash->size; i++) { |
| 462 | head = &hash->table[i]; |
| 463 | |
| 464 | rcu_read_lock(); |
| 465 | hlist_for_each_entry_rcu(claim, node, head, hash_entry) { |
| 466 | /* only own claims are interesting */ |
| 467 | if (claim->backbone_gw != backbone_gw) |
| 468 | continue; |
| 469 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 470 | batadv_bla_send_claim(bat_priv, claim->addr, claim->vid, |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 471 | BATADV_CLAIM_TYPE_CLAIM); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 472 | } |
| 473 | rcu_read_unlock(); |
| 474 | } |
| 475 | |
| 476 | /* finally, send an announcement frame */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 477 | batadv_bla_send_announce(bat_priv, backbone_gw); |
| 478 | batadv_backbone_gw_free_ref(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 481 | /* @backbone_gw: the backbone gateway from whom we are out of sync |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 482 | * |
| 483 | * When the crc is wrong, ask the backbone gateway for a full table update. |
| 484 | * After the request, it will repeat all of his own claims and finally |
| 485 | * send an announcement claim with which we can check again. |
| 486 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 487 | static void batadv_bla_send_request(struct batadv_backbone_gw *backbone_gw) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 488 | { |
| 489 | /* first, remove all old entries */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 490 | batadv_bla_del_backbone_claims(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 491 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 492 | batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, |
| 493 | "Sending REQUEST to %pM\n", backbone_gw->orig); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 494 | |
| 495 | /* send request */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 496 | batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig, |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 497 | backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 498 | |
| 499 | /* no local broadcasts should be sent or received, for now. */ |
| 500 | if (!atomic_read(&backbone_gw->request_sent)) { |
| 501 | atomic_inc(&backbone_gw->bat_priv->bla_num_requests); |
| 502 | atomic_set(&backbone_gw->request_sent, 1); |
| 503 | } |
| 504 | } |
| 505 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 506 | /* @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 507 | * @backbone_gw: our backbone gateway which should be announced |
| 508 | * |
| 509 | * This function sends an announcement. It is called from multiple |
| 510 | * places. |
| 511 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 512 | static void batadv_bla_send_announce(struct batadv_priv *bat_priv, |
| 513 | struct batadv_backbone_gw *backbone_gw) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 514 | { |
| 515 | uint8_t mac[ETH_ALEN]; |
Al Viro | 3e2f1a1 | 2012-04-22 07:47:50 +0100 | [diff] [blame] | 516 | __be16 crc; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 517 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 518 | memcpy(mac, batadv_announce_mac, 4); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 519 | crc = htons(backbone_gw->crc); |
Al Viro | 1a5852d | 2012-04-22 07:50:29 +0100 | [diff] [blame] | 520 | memcpy(&mac[4], &crc, 2); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 521 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 522 | batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid, |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 523 | BATADV_CLAIM_TYPE_ANNOUNCE); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 524 | |
| 525 | } |
| 526 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 527 | /** |
| 528 | * batadv_bla_add_claim - Adds a claim in the claim hash |
| 529 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 530 | * @mac: the mac address of the claim |
| 531 | * @vid: the VLAN ID of the frame |
| 532 | * @backbone_gw: the backbone gateway which claims it |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 533 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 534 | static void batadv_bla_add_claim(struct batadv_priv *bat_priv, |
| 535 | const uint8_t *mac, const short vid, |
| 536 | struct batadv_backbone_gw *backbone_gw) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 537 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 538 | struct batadv_claim *claim; |
| 539 | struct batadv_claim search_claim; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 540 | int hash_added; |
| 541 | |
| 542 | memcpy(search_claim.addr, mac, ETH_ALEN); |
| 543 | search_claim.vid = vid; |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 544 | claim = batadv_claim_hash_find(bat_priv, &search_claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 545 | |
| 546 | /* create a new claim entry if it does not exist yet. */ |
| 547 | if (!claim) { |
| 548 | claim = kzalloc(sizeof(*claim), GFP_ATOMIC); |
| 549 | if (!claim) |
| 550 | return; |
| 551 | |
| 552 | memcpy(claim->addr, mac, ETH_ALEN); |
| 553 | claim->vid = vid; |
| 554 | claim->lasttime = jiffies; |
| 555 | claim->backbone_gw = backbone_gw; |
| 556 | |
| 557 | atomic_set(&claim->refcount, 2); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 558 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 559 | "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n", |
| 560 | mac, vid); |
Sven Eckelmann | c0a5592 | 2012-05-12 13:48:55 +0200 | [diff] [blame] | 561 | hash_added = batadv_hash_add(bat_priv->claim_hash, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 562 | batadv_compare_claim, |
| 563 | batadv_choose_claim, claim, |
| 564 | &claim->hash_entry); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 565 | |
| 566 | if (unlikely(hash_added != 0)) { |
| 567 | /* only local changes happened. */ |
| 568 | kfree(claim); |
| 569 | return; |
| 570 | } |
| 571 | } else { |
| 572 | claim->lasttime = jiffies; |
| 573 | if (claim->backbone_gw == backbone_gw) |
| 574 | /* no need to register a new backbone */ |
| 575 | goto claim_free_ref; |
| 576 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 577 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 578 | "bla_add_claim(): changing ownership for %pM, vid %d\n", |
| 579 | mac, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 580 | |
| 581 | claim->backbone_gw->crc ^= |
| 582 | crc16(0, claim->addr, ETH_ALEN); |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 583 | batadv_backbone_gw_free_ref(claim->backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 584 | |
| 585 | } |
| 586 | /* set (new) backbone gw */ |
| 587 | atomic_inc(&backbone_gw->refcount); |
| 588 | claim->backbone_gw = backbone_gw; |
| 589 | |
| 590 | backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN); |
| 591 | backbone_gw->lasttime = jiffies; |
| 592 | |
| 593 | claim_free_ref: |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 594 | batadv_claim_free_ref(claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* Delete a claim from the claim hash which has the |
| 598 | * given mac address and vid. |
| 599 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 600 | static void batadv_bla_del_claim(struct batadv_priv *bat_priv, |
| 601 | const uint8_t *mac, const short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 602 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 603 | struct batadv_claim search_claim, *claim; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 604 | |
| 605 | memcpy(search_claim.addr, mac, ETH_ALEN); |
| 606 | search_claim.vid = vid; |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 607 | claim = batadv_claim_hash_find(bat_priv, &search_claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 608 | if (!claim) |
| 609 | return; |
| 610 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 611 | batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n", |
| 612 | mac, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 613 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 614 | batadv_hash_remove(bat_priv->claim_hash, batadv_compare_claim, |
| 615 | batadv_choose_claim, claim); |
| 616 | batadv_claim_free_ref(claim); /* reference from the hash is gone */ |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 617 | |
| 618 | claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN); |
| 619 | |
| 620 | /* don't need the reference from hash_find() anymore */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 621 | batadv_claim_free_ref(claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* check for ANNOUNCE frame, return 1 if handled */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 625 | static int batadv_handle_announce(struct batadv_priv *bat_priv, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 626 | uint8_t *an_addr, uint8_t *backbone_addr, |
| 627 | short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 628 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 629 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 630 | uint16_t crc; |
| 631 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 632 | if (memcmp(an_addr, batadv_announce_mac, 4) != 0) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 633 | return 0; |
| 634 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 635 | backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 636 | |
| 637 | if (unlikely(!backbone_gw)) |
| 638 | return 1; |
| 639 | |
| 640 | |
| 641 | /* handle as ANNOUNCE frame */ |
| 642 | backbone_gw->lasttime = jiffies; |
Al Viro | 3e2f1a1 | 2012-04-22 07:47:50 +0100 | [diff] [blame] | 643 | crc = ntohs(*((__be16 *)(&an_addr[4]))); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 644 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 645 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 646 | "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n", |
| 647 | vid, backbone_gw->orig, crc); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 648 | |
| 649 | if (backbone_gw->crc != crc) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 650 | batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 651 | "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n", |
| 652 | backbone_gw->orig, backbone_gw->vid, |
| 653 | backbone_gw->crc, crc); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 654 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 655 | batadv_bla_send_request(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 656 | } else { |
| 657 | /* if we have sent a request and the crc was OK, |
| 658 | * we can allow traffic again. |
| 659 | */ |
| 660 | if (atomic_read(&backbone_gw->request_sent)) { |
| 661 | atomic_dec(&backbone_gw->bat_priv->bla_num_requests); |
| 662 | atomic_set(&backbone_gw->request_sent, 0); |
| 663 | } |
| 664 | } |
| 665 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 666 | batadv_backbone_gw_free_ref(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | /* check for REQUEST frame, return 1 if handled */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 671 | static int batadv_handle_request(struct batadv_priv *bat_priv, |
| 672 | struct batadv_hard_iface *primary_if, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 673 | uint8_t *backbone_addr, |
| 674 | struct ethhdr *ethhdr, short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 675 | { |
| 676 | /* check for REQUEST frame */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 677 | if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 678 | return 0; |
| 679 | |
| 680 | /* sanity check, this should not happen on a normal switch, |
| 681 | * we ignore it in this case. |
| 682 | */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 683 | if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 684 | return 1; |
| 685 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 686 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 687 | "handle_request(): REQUEST vid %d (sent by %pM)...\n", |
| 688 | vid, ethhdr->h_source); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 689 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 690 | batadv_bla_answer_request(bat_priv, primary_if, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 691 | return 1; |
| 692 | } |
| 693 | |
| 694 | /* check for UNCLAIM frame, return 1 if handled */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 695 | static int batadv_handle_unclaim(struct batadv_priv *bat_priv, |
| 696 | struct batadv_hard_iface *primary_if, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 697 | uint8_t *backbone_addr, |
| 698 | uint8_t *claim_addr, short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 699 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 700 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 701 | |
| 702 | /* unclaim in any case if it is our own */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 703 | if (primary_if && batadv_compare_eth(backbone_addr, |
| 704 | primary_if->net_dev->dev_addr)) |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 705 | batadv_bla_send_claim(bat_priv, claim_addr, vid, |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 706 | BATADV_CLAIM_TYPE_UNCLAIM); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 707 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 708 | backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 709 | |
| 710 | if (!backbone_gw) |
| 711 | return 1; |
| 712 | |
| 713 | /* this must be an UNCLAIM frame */ |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 714 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 715 | "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n", |
| 716 | claim_addr, vid, backbone_gw->orig); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 717 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 718 | batadv_bla_del_claim(bat_priv, claim_addr, vid); |
| 719 | batadv_backbone_gw_free_ref(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 720 | return 1; |
| 721 | } |
| 722 | |
| 723 | /* check for CLAIM frame, return 1 if handled */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 724 | static int batadv_handle_claim(struct batadv_priv *bat_priv, |
| 725 | struct batadv_hard_iface *primary_if, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 726 | uint8_t *backbone_addr, uint8_t *claim_addr, |
| 727 | short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 728 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 729 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 730 | |
| 731 | /* register the gateway if not yet available, and add the claim. */ |
| 732 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 733 | backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 734 | |
| 735 | if (unlikely(!backbone_gw)) |
| 736 | return 1; |
| 737 | |
| 738 | /* this must be a CLAIM frame */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 739 | batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw); |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 740 | if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr)) |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 741 | batadv_bla_send_claim(bat_priv, claim_addr, vid, |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 742 | BATADV_CLAIM_TYPE_CLAIM); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 743 | |
| 744 | /* TODO: we could call something like tt_local_del() here. */ |
| 745 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 746 | batadv_backbone_gw_free_ref(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 747 | return 1; |
| 748 | } |
| 749 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 750 | /** |
| 751 | * batadv_check_claim_group |
| 752 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 753 | * @hw_src: the Hardware source in the ARP Header |
| 754 | * @hw_dst: the Hardware destination in the ARP Header |
| 755 | * @ethhdr: pointer to the Ethernet header of the claim frame |
| 756 | * |
| 757 | * checks if it is a claim packet and if its on the same group. |
| 758 | * This function also applies the group ID of the sender |
| 759 | * if it is in the same mesh. |
| 760 | * |
| 761 | * returns: |
| 762 | * 2 - if it is a claim packet and on the same group |
| 763 | * 1 - if is a claim packet from another group |
| 764 | * 0 - if it is not a claim packet |
| 765 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 766 | static int batadv_check_claim_group(struct batadv_priv *bat_priv, |
| 767 | struct batadv_hard_iface *primary_if, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 768 | uint8_t *hw_src, uint8_t *hw_dst, |
| 769 | struct ethhdr *ethhdr) |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 770 | { |
| 771 | uint8_t *backbone_addr; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 772 | struct batadv_orig_node *orig_node; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 773 | struct batadv_bla_claim_dst *bla_dst, *bla_dst_own; |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 774 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 775 | bla_dst = (struct batadv_bla_claim_dst *)hw_dst; |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 776 | bla_dst_own = &bat_priv->claim_dest; |
| 777 | |
| 778 | /* check if it is a claim packet in general */ |
| 779 | if (memcmp(bla_dst->magic, bla_dst_own->magic, |
| 780 | sizeof(bla_dst->magic)) != 0) |
| 781 | return 0; |
| 782 | |
| 783 | /* if announcement packet, use the source, |
| 784 | * otherwise assume it is in the hw_src |
| 785 | */ |
| 786 | switch (bla_dst->type) { |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 787 | case BATADV_CLAIM_TYPE_CLAIM: |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 788 | backbone_addr = hw_src; |
| 789 | break; |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 790 | case BATADV_CLAIM_TYPE_REQUEST: |
| 791 | case BATADV_CLAIM_TYPE_ANNOUNCE: |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 792 | case BATADV_CLAIM_TYPE_UNCLAIM: |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 793 | backbone_addr = ethhdr->h_source; |
| 794 | break; |
| 795 | default: |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | /* don't accept claim frames from ourselves */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 800 | if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr)) |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 801 | return 0; |
| 802 | |
| 803 | /* if its already the same group, it is fine. */ |
| 804 | if (bla_dst->group == bla_dst_own->group) |
| 805 | return 2; |
| 806 | |
| 807 | /* lets see if this originator is in our mesh */ |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 808 | orig_node = batadv_orig_hash_find(bat_priv, backbone_addr); |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 809 | |
| 810 | /* dont accept claims from gateways which are not in |
| 811 | * the same mesh or group. |
| 812 | */ |
| 813 | if (!orig_node) |
| 814 | return 1; |
| 815 | |
| 816 | /* if our mesh friends mac is bigger, use it for ourselves. */ |
| 817 | if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 818 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 819 | "taking other backbones claim group: %04x\n", |
| 820 | ntohs(bla_dst->group)); |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 821 | bla_dst_own->group = bla_dst->group; |
| 822 | } |
| 823 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 824 | batadv_orig_node_free_ref(orig_node); |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 825 | |
| 826 | return 2; |
| 827 | } |
| 828 | |
| 829 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 830 | /* @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 831 | * @skb: the frame to be checked |
| 832 | * |
| 833 | * Check if this is a claim frame, and process it accordingly. |
| 834 | * |
| 835 | * returns 1 if it was a claim frame, otherwise return 0 to |
| 836 | * tell the callee that it can use the frame on its own. |
| 837 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 838 | static int batadv_bla_process_claim(struct batadv_priv *bat_priv, |
| 839 | struct batadv_hard_iface *primary_if, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 840 | struct sk_buff *skb) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 841 | { |
| 842 | struct ethhdr *ethhdr; |
| 843 | struct vlan_ethhdr *vhdr; |
| 844 | struct arphdr *arphdr; |
| 845 | uint8_t *hw_src, *hw_dst; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 846 | struct batadv_bla_claim_dst *bla_dst; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 847 | uint16_t proto; |
| 848 | int headlen; |
| 849 | short vid = -1; |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 850 | int ret; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 851 | |
| 852 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 853 | |
| 854 | if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) { |
| 855 | vhdr = (struct vlan_ethhdr *)ethhdr; |
| 856 | vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; |
| 857 | proto = ntohs(vhdr->h_vlan_encapsulated_proto); |
| 858 | headlen = sizeof(*vhdr); |
| 859 | } else { |
| 860 | proto = ntohs(ethhdr->h_proto); |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 861 | headlen = ETH_HLEN; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | if (proto != ETH_P_ARP) |
| 865 | return 0; /* not a claim frame */ |
| 866 | |
| 867 | /* this must be a ARP frame. check if it is a claim. */ |
| 868 | |
| 869 | if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev)))) |
| 870 | return 0; |
| 871 | |
| 872 | /* pskb_may_pull() may have modified the pointers, get ethhdr again */ |
| 873 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 874 | arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen); |
| 875 | |
| 876 | /* Check whether the ARP frame carries a valid |
| 877 | * IP information |
| 878 | */ |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 879 | if (arphdr->ar_hrd != htons(ARPHRD_ETHER)) |
| 880 | return 0; |
| 881 | if (arphdr->ar_pro != htons(ETH_P_IP)) |
| 882 | return 0; |
| 883 | if (arphdr->ar_hln != ETH_ALEN) |
| 884 | return 0; |
| 885 | if (arphdr->ar_pln != 4) |
| 886 | return 0; |
| 887 | |
| 888 | hw_src = (uint8_t *)arphdr + sizeof(struct arphdr); |
| 889 | hw_dst = hw_src + ETH_ALEN + 4; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 890 | bla_dst = (struct batadv_bla_claim_dst *)hw_dst; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 891 | |
| 892 | /* check if it is a claim frame. */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 893 | ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst, |
| 894 | ethhdr); |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 895 | if (ret == 1) |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 896 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 897 | "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n", |
| 898 | ethhdr->h_source, vid, hw_src, hw_dst); |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 899 | |
| 900 | if (ret < 2) |
| 901 | return ret; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 902 | |
| 903 | /* become a backbone gw ourselves on this vlan if not happened yet */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 904 | batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 905 | |
| 906 | /* check for the different types of claim frames ... */ |
| 907 | switch (bla_dst->type) { |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 908 | case BATADV_CLAIM_TYPE_CLAIM: |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 909 | if (batadv_handle_claim(bat_priv, primary_if, hw_src, |
| 910 | ethhdr->h_source, vid)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 911 | return 1; |
| 912 | break; |
Simon Wunderlich | 3eb8773 | 2012-06-23 12:34:18 +0200 | [diff] [blame^] | 913 | case BATADV_CLAIM_TYPE_UNCLAIM: |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 914 | if (batadv_handle_unclaim(bat_priv, primary_if, |
| 915 | ethhdr->h_source, hw_src, vid)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 916 | return 1; |
| 917 | break; |
| 918 | |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 919 | case BATADV_CLAIM_TYPE_ANNOUNCE: |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 920 | if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source, |
| 921 | vid)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 922 | return 1; |
| 923 | break; |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 924 | case BATADV_CLAIM_TYPE_REQUEST: |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 925 | if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr, |
| 926 | vid)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 927 | return 1; |
| 928 | break; |
| 929 | } |
| 930 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 931 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 932 | "bla_process_claim(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n", |
| 933 | ethhdr->h_source, vid, hw_src, hw_dst); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 934 | return 1; |
| 935 | } |
| 936 | |
| 937 | /* Check when we last heard from other nodes, and remove them in case of |
| 938 | * a time out, or clean all backbone gws if now is set. |
| 939 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 940 | static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 941 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 942 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 943 | struct hlist_node *node, *node_tmp; |
| 944 | struct hlist_head *head; |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 945 | struct batadv_hashtable *hash; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 946 | spinlock_t *list_lock; /* protects write access to the hash lists */ |
| 947 | int i; |
| 948 | |
| 949 | hash = bat_priv->backbone_hash; |
| 950 | if (!hash) |
| 951 | return; |
| 952 | |
| 953 | for (i = 0; i < hash->size; i++) { |
| 954 | head = &hash->table[i]; |
| 955 | list_lock = &hash->list_locks[i]; |
| 956 | |
| 957 | spin_lock_bh(list_lock); |
| 958 | hlist_for_each_entry_safe(backbone_gw, node, node_tmp, |
| 959 | head, hash_entry) { |
| 960 | if (now) |
| 961 | goto purge_now; |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 962 | if (!batadv_has_timed_out(backbone_gw->lasttime, |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 963 | BATADV_BLA_BACKBONE_TIMEOUT)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 964 | continue; |
| 965 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 966 | batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 967 | "bla_purge_backbone_gw(): backbone gw %pM timed out\n", |
| 968 | backbone_gw->orig); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 969 | |
| 970 | purge_now: |
| 971 | /* don't wait for the pending request anymore */ |
| 972 | if (atomic_read(&backbone_gw->request_sent)) |
| 973 | atomic_dec(&bat_priv->bla_num_requests); |
| 974 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 975 | batadv_bla_del_backbone_claims(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 976 | |
| 977 | hlist_del_rcu(node); |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 978 | batadv_backbone_gw_free_ref(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 979 | } |
| 980 | spin_unlock_bh(list_lock); |
| 981 | } |
| 982 | } |
| 983 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 984 | /** |
| 985 | * batadv_bla_purge_claims |
| 986 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 987 | * @primary_if: the selected primary interface, may be NULL if now is set |
| 988 | * @now: whether the whole hash shall be wiped now |
| 989 | * |
| 990 | * Check when we heard last time from our own claims, and remove them in case of |
| 991 | * a time out, or clean all claims if now is set |
| 992 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 993 | static void batadv_bla_purge_claims(struct batadv_priv *bat_priv, |
| 994 | struct batadv_hard_iface *primary_if, |
| 995 | int now) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 996 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 997 | struct batadv_claim *claim; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 998 | struct hlist_node *node; |
| 999 | struct hlist_head *head; |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1000 | struct batadv_hashtable *hash; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1001 | int i; |
| 1002 | |
| 1003 | hash = bat_priv->claim_hash; |
| 1004 | if (!hash) |
| 1005 | return; |
| 1006 | |
| 1007 | for (i = 0; i < hash->size; i++) { |
| 1008 | head = &hash->table[i]; |
| 1009 | |
| 1010 | rcu_read_lock(); |
| 1011 | hlist_for_each_entry_rcu(claim, node, head, hash_entry) { |
| 1012 | if (now) |
| 1013 | goto purge_now; |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1014 | if (!batadv_compare_eth(claim->backbone_gw->orig, |
| 1015 | primary_if->net_dev->dev_addr)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1016 | continue; |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1017 | if (!batadv_has_timed_out(claim->lasttime, |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1018 | BATADV_BLA_CLAIM_TIMEOUT)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1019 | continue; |
| 1020 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1021 | batadv_dbg(BATADV_DBG_BLA, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1022 | "bla_purge_claims(): %pM, vid %d, time out\n", |
| 1023 | claim->addr, claim->vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1024 | |
| 1025 | purge_now: |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1026 | batadv_handle_unclaim(bat_priv, primary_if, |
| 1027 | claim->backbone_gw->orig, |
| 1028 | claim->addr, claim->vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1029 | } |
| 1030 | rcu_read_unlock(); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1034 | /** |
| 1035 | * batadv_bla_update_orig_address |
| 1036 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1037 | * @primary_if: the new selected primary_if |
| 1038 | * @oldif: the old primary interface, may be NULL |
| 1039 | * |
| 1040 | * Update the backbone gateways when the own orig address changes. |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1041 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1042 | void batadv_bla_update_orig_address(struct batadv_priv *bat_priv, |
| 1043 | struct batadv_hard_iface *primary_if, |
| 1044 | struct batadv_hard_iface *oldif) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1045 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1046 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1047 | struct hlist_node *node; |
| 1048 | struct hlist_head *head; |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1049 | struct batadv_hashtable *hash; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1050 | int i; |
| 1051 | |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 1052 | /* reset bridge loop avoidance group id */ |
| 1053 | bat_priv->claim_dest.group = |
| 1054 | htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN)); |
| 1055 | |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1056 | if (!oldif) { |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1057 | batadv_bla_purge_claims(bat_priv, NULL, 1); |
| 1058 | batadv_bla_purge_backbone_gw(bat_priv, 1); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1059 | return; |
| 1060 | } |
| 1061 | |
| 1062 | hash = bat_priv->backbone_hash; |
| 1063 | if (!hash) |
| 1064 | return; |
| 1065 | |
| 1066 | for (i = 0; i < hash->size; i++) { |
| 1067 | head = &hash->table[i]; |
| 1068 | |
| 1069 | rcu_read_lock(); |
| 1070 | hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) { |
| 1071 | /* own orig still holds the old value. */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1072 | if (!batadv_compare_eth(backbone_gw->orig, |
| 1073 | oldif->net_dev->dev_addr)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1074 | continue; |
| 1075 | |
| 1076 | memcpy(backbone_gw->orig, |
| 1077 | primary_if->net_dev->dev_addr, ETH_ALEN); |
| 1078 | /* send an announce frame so others will ask for our |
| 1079 | * claims and update their tables. |
| 1080 | */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1081 | batadv_bla_send_announce(bat_priv, backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1082 | } |
| 1083 | rcu_read_unlock(); |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | /* (re)start the timer */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1090 | static void batadv_bla_start_timer(struct batadv_priv *bat_priv) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1091 | { |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1092 | INIT_DELAYED_WORK(&bat_priv->bla_work, batadv_bla_periodic_work); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1093 | queue_delayed_work(batadv_event_workqueue, &bat_priv->bla_work, |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1094 | msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH)); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /* periodic work to do: |
| 1098 | * * purge structures when they are too old |
| 1099 | * * send announcements |
| 1100 | */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1101 | static void batadv_bla_periodic_work(struct work_struct *work) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1102 | { |
| 1103 | struct delayed_work *delayed_work = |
| 1104 | container_of(work, struct delayed_work, work); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1105 | struct batadv_priv *bat_priv; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1106 | struct hlist_node *node; |
| 1107 | struct hlist_head *head; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1108 | struct batadv_backbone_gw *backbone_gw; |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1109 | struct batadv_hashtable *hash; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1110 | struct batadv_hard_iface *primary_if; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1111 | int i; |
| 1112 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1113 | bat_priv = container_of(delayed_work, struct batadv_priv, bla_work); |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1114 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1115 | if (!primary_if) |
| 1116 | goto out; |
| 1117 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1118 | batadv_bla_purge_claims(bat_priv, primary_if, 0); |
| 1119 | batadv_bla_purge_backbone_gw(bat_priv, 0); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1120 | |
| 1121 | if (!atomic_read(&bat_priv->bridge_loop_avoidance)) |
| 1122 | goto out; |
| 1123 | |
| 1124 | hash = bat_priv->backbone_hash; |
| 1125 | if (!hash) |
| 1126 | goto out; |
| 1127 | |
| 1128 | for (i = 0; i < hash->size; i++) { |
| 1129 | head = &hash->table[i]; |
| 1130 | |
| 1131 | rcu_read_lock(); |
| 1132 | hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1133 | if (!batadv_compare_eth(backbone_gw->orig, |
| 1134 | primary_if->net_dev->dev_addr)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1135 | continue; |
| 1136 | |
| 1137 | backbone_gw->lasttime = jiffies; |
| 1138 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1139 | batadv_bla_send_announce(bat_priv, backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1140 | } |
| 1141 | rcu_read_unlock(); |
| 1142 | } |
| 1143 | out: |
| 1144 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1145 | batadv_hardif_free_ref(primary_if); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1146 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1147 | batadv_bla_start_timer(bat_priv); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 1150 | /* The hash for claim and backbone hash receive the same key because they |
| 1151 | * are getting initialized by hash_new with the same key. Reinitializing |
| 1152 | * them with to different keys to allow nested locking without generating |
| 1153 | * lockdep warnings |
| 1154 | */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1155 | static struct lock_class_key batadv_claim_hash_lock_class_key; |
| 1156 | static struct lock_class_key batadv_backbone_hash_lock_class_key; |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 1157 | |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1158 | /* initialize all bla structures */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1159 | int batadv_bla_init(struct batadv_priv *bat_priv) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1160 | { |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1161 | int i; |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 1162 | uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00}; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1163 | struct batadv_hard_iface *primary_if; |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1164 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1165 | batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n"); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1166 | |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 1167 | /* setting claim destination address */ |
| 1168 | memcpy(&bat_priv->claim_dest.magic, claim_dest, 3); |
| 1169 | bat_priv->claim_dest.type = 0; |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1170 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 1171 | if (primary_if) { |
| 1172 | bat_priv->claim_dest.group = |
| 1173 | htons(crc16(0, primary_if->net_dev->dev_addr, |
| 1174 | ETH_ALEN)); |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1175 | batadv_hardif_free_ref(primary_if); |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 1176 | } else { |
| 1177 | bat_priv->claim_dest.group = 0; /* will be set later */ |
| 1178 | } |
| 1179 | |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1180 | /* initialize the duplicate list */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1181 | for (i = 0; i < BATADV_DUPLIST_SIZE; i++) |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1182 | bat_priv->bcast_duplist[i].entrytime = |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1183 | jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT); |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1184 | bat_priv->bcast_duplist_curr = 0; |
| 1185 | |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1186 | if (bat_priv->claim_hash) |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 1187 | return 0; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1188 | |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 1189 | bat_priv->claim_hash = batadv_hash_new(128); |
| 1190 | bat_priv->backbone_hash = batadv_hash_new(32); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1191 | |
| 1192 | if (!bat_priv->claim_hash || !bat_priv->backbone_hash) |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 1193 | return -ENOMEM; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1194 | |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 1195 | batadv_hash_set_lock_class(bat_priv->claim_hash, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1196 | &batadv_claim_hash_lock_class_key); |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 1197 | batadv_hash_set_lock_class(bat_priv->backbone_hash, |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1198 | &batadv_backbone_hash_lock_class_key); |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 1199 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1200 | batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n"); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1201 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1202 | batadv_bla_start_timer(bat_priv); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 1203 | return 0; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1206 | /** |
| 1207 | * batadv_bla_check_bcast_duplist |
| 1208 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1209 | * @bcast_packet: originator mac address |
| 1210 | * @hdr_size: maximum length of the frame |
| 1211 | * |
| 1212 | * check if it is on our broadcast list. Another gateway might |
| 1213 | * have sent the same packet because it is connected to the same backbone, |
| 1214 | * so we have to remove this duplicate. |
| 1215 | * |
| 1216 | * This is performed by checking the CRC, which will tell us |
| 1217 | * with a good chance that it is the same packet. If it is furthermore |
| 1218 | * sent by another host, drop it. We allow equal packets from |
| 1219 | * the same host however as this might be intended. |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1220 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1221 | int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1222 | struct batadv_bcast_packet *bcast_packet, |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 1223 | int hdr_size) |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1224 | { |
| 1225 | int i, length, curr; |
| 1226 | uint8_t *content; |
| 1227 | uint16_t crc; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1228 | struct batadv_bcast_duplist_entry *entry; |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1229 | |
| 1230 | length = hdr_size - sizeof(*bcast_packet); |
| 1231 | content = (uint8_t *)bcast_packet; |
| 1232 | content += sizeof(*bcast_packet); |
| 1233 | |
| 1234 | /* calculate the crc ... */ |
| 1235 | crc = crc16(0, content, length); |
| 1236 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1237 | for (i = 0; i < BATADV_DUPLIST_SIZE; i++) { |
| 1238 | curr = (bat_priv->bcast_duplist_curr + i) % BATADV_DUPLIST_SIZE; |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1239 | entry = &bat_priv->bcast_duplist[curr]; |
| 1240 | |
| 1241 | /* we can stop searching if the entry is too old ; |
| 1242 | * later entries will be even older |
| 1243 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1244 | if (batadv_has_timed_out(entry->entrytime, |
| 1245 | BATADV_DUPLIST_TIMEOUT)) |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1246 | break; |
| 1247 | |
| 1248 | if (entry->crc != crc) |
| 1249 | continue; |
| 1250 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1251 | if (batadv_compare_eth(entry->orig, bcast_packet->orig)) |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1252 | continue; |
| 1253 | |
| 1254 | /* this entry seems to match: same crc, not too old, |
| 1255 | * and from another gw. therefore return 1 to forbid it. |
| 1256 | */ |
| 1257 | return 1; |
| 1258 | } |
| 1259 | /* not found, add a new entry (overwrite the oldest entry) */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1260 | curr = (bat_priv->bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1); |
| 1261 | curr %= BATADV_DUPLIST_SIZE; |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1262 | entry = &bat_priv->bcast_duplist[curr]; |
| 1263 | entry->crc = crc; |
| 1264 | entry->entrytime = jiffies; |
| 1265 | memcpy(entry->orig, bcast_packet->orig, ETH_ALEN); |
| 1266 | bat_priv->bcast_duplist_curr = curr; |
| 1267 | |
| 1268 | /* allow it, its the first occurence. */ |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1274 | /* @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 1275 | * @orig: originator mac address |
| 1276 | * |
| 1277 | * check if the originator is a gateway for any VLAN ID. |
| 1278 | * |
| 1279 | * returns 1 if it is found, 0 otherwise |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 1280 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1281 | int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig) |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 1282 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1283 | struct batadv_hashtable *hash = bat_priv->backbone_hash; |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 1284 | struct hlist_head *head; |
| 1285 | struct hlist_node *node; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1286 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 1287 | int i; |
| 1288 | |
| 1289 | if (!atomic_read(&bat_priv->bridge_loop_avoidance)) |
| 1290 | return 0; |
| 1291 | |
| 1292 | if (!hash) |
| 1293 | return 0; |
| 1294 | |
| 1295 | for (i = 0; i < hash->size; i++) { |
| 1296 | head = &hash->table[i]; |
| 1297 | |
| 1298 | rcu_read_lock(); |
| 1299 | hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1300 | if (batadv_compare_eth(backbone_gw->orig, orig)) { |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 1301 | rcu_read_unlock(); |
| 1302 | return 1; |
| 1303 | } |
| 1304 | } |
| 1305 | rcu_read_unlock(); |
| 1306 | } |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1312 | /** |
| 1313 | * batadv_bla_is_backbone_gw |
| 1314 | * @skb: the frame to be checked |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1315 | * @orig_node: the orig_node of the frame |
| 1316 | * @hdr_size: maximum length of the frame |
| 1317 | * |
| 1318 | * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1 |
| 1319 | * if the orig_node is also a gateway on the soft interface, otherwise it |
| 1320 | * returns 0. |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1321 | */ |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 1322 | int batadv_bla_is_backbone_gw(struct sk_buff *skb, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1323 | struct batadv_orig_node *orig_node, int hdr_size) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1324 | { |
| 1325 | struct ethhdr *ethhdr; |
| 1326 | struct vlan_ethhdr *vhdr; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1327 | struct batadv_backbone_gw *backbone_gw; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1328 | short vid = -1; |
| 1329 | |
| 1330 | if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance)) |
| 1331 | return 0; |
| 1332 | |
| 1333 | /* first, find out the vid. */ |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 1334 | if (!pskb_may_pull(skb, hdr_size + ETH_HLEN)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1335 | return 0; |
| 1336 | |
| 1337 | ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size); |
| 1338 | |
| 1339 | if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) { |
| 1340 | if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr))) |
| 1341 | return 0; |
| 1342 | |
| 1343 | vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) + |
| 1344 | hdr_size); |
| 1345 | vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; |
| 1346 | } |
| 1347 | |
| 1348 | /* see if this originator is a backbone gw for this VLAN */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1349 | backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv, |
| 1350 | orig_node->orig, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1351 | if (!backbone_gw) |
| 1352 | return 0; |
| 1353 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1354 | batadv_backbone_gw_free_ref(backbone_gw); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1355 | return 1; |
| 1356 | } |
| 1357 | |
| 1358 | /* free all bla structures (for softinterface free or module unload) */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1359 | void batadv_bla_free(struct batadv_priv *bat_priv) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1360 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1361 | struct batadv_hard_iface *primary_if; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1362 | |
| 1363 | cancel_delayed_work_sync(&bat_priv->bla_work); |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1364 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1365 | |
| 1366 | if (bat_priv->claim_hash) { |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1367 | batadv_bla_purge_claims(bat_priv, primary_if, 1); |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 1368 | batadv_hash_destroy(bat_priv->claim_hash); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1369 | bat_priv->claim_hash = NULL; |
| 1370 | } |
| 1371 | if (bat_priv->backbone_hash) { |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1372 | batadv_bla_purge_backbone_gw(bat_priv, 1); |
Sven Eckelmann | 1a8eaf0 | 2012-05-12 02:09:32 +0200 | [diff] [blame] | 1373 | batadv_hash_destroy(bat_priv->backbone_hash); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1374 | bat_priv->backbone_hash = NULL; |
| 1375 | } |
| 1376 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1377 | batadv_hardif_free_ref(primary_if); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1378 | } |
| 1379 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1380 | /** |
| 1381 | * batadv_bla_rx |
| 1382 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1383 | * @skb: the frame to be checked |
| 1384 | * @vid: the VLAN ID of the frame |
Simon Wunderlich | 2d3f6cc | 2012-07-04 20:38:19 +0200 | [diff] [blame] | 1385 | * @is_bcast: the packet came in a broadcast packet type. |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1386 | * |
| 1387 | * bla_rx avoidance checks if: |
| 1388 | * * we have to race for a claim |
| 1389 | * * if the frame is allowed on the LAN |
| 1390 | * |
| 1391 | * in these cases, the skb is further handled by this function and |
| 1392 | * returns 1, otherwise it returns 0 and the caller shall further |
| 1393 | * process the skb. |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1394 | */ |
David S. Miller | 04c9f41 | 2012-07-10 23:56:33 -0700 | [diff] [blame] | 1395 | int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid, |
| 1396 | bool is_bcast) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1397 | { |
| 1398 | struct ethhdr *ethhdr; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1399 | struct batadv_claim search_claim, *claim = NULL; |
| 1400 | struct batadv_hard_iface *primary_if; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1401 | int ret; |
| 1402 | |
| 1403 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1404 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1405 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1406 | if (!primary_if) |
| 1407 | goto handled; |
| 1408 | |
| 1409 | if (!atomic_read(&bat_priv->bridge_loop_avoidance)) |
| 1410 | goto allow; |
| 1411 | |
| 1412 | |
| 1413 | if (unlikely(atomic_read(&bat_priv->bla_num_requests))) |
| 1414 | /* don't allow broadcasts while requests are in flight */ |
Simon Wunderlich | 2d3f6cc | 2012-07-04 20:38:19 +0200 | [diff] [blame] | 1415 | if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1416 | goto handled; |
| 1417 | |
| 1418 | memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN); |
| 1419 | search_claim.vid = vid; |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1420 | claim = batadv_claim_hash_find(bat_priv, &search_claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1421 | |
| 1422 | if (!claim) { |
| 1423 | /* possible optimization: race for a claim */ |
| 1424 | /* No claim exists yet, claim it for us! |
| 1425 | */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1426 | batadv_handle_claim(bat_priv, primary_if, |
| 1427 | primary_if->net_dev->dev_addr, |
| 1428 | ethhdr->h_source, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1429 | goto allow; |
| 1430 | } |
| 1431 | |
| 1432 | /* if it is our own claim ... */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1433 | if (batadv_compare_eth(claim->backbone_gw->orig, |
| 1434 | primary_if->net_dev->dev_addr)) { |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1435 | /* ... allow it in any case */ |
| 1436 | claim->lasttime = jiffies; |
| 1437 | goto allow; |
| 1438 | } |
| 1439 | |
| 1440 | /* if it is a broadcast ... */ |
Simon Wunderlich | 2d3f6cc | 2012-07-04 20:38:19 +0200 | [diff] [blame] | 1441 | if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) { |
| 1442 | /* ... drop it. the responsible gateway is in charge. |
| 1443 | * |
| 1444 | * We need to check is_bcast because with the gateway |
| 1445 | * feature, broadcasts (like DHCP requests) may be sent |
| 1446 | * using a unicast packet type. |
| 1447 | */ |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1448 | goto handled; |
| 1449 | } else { |
| 1450 | /* seems the client considers us as its best gateway. |
| 1451 | * send a claim and update the claim table |
| 1452 | * immediately. |
| 1453 | */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1454 | batadv_handle_claim(bat_priv, primary_if, |
| 1455 | primary_if->net_dev->dev_addr, |
| 1456 | ethhdr->h_source, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1457 | goto allow; |
| 1458 | } |
| 1459 | allow: |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1460 | batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1461 | ret = 0; |
| 1462 | goto out; |
| 1463 | |
| 1464 | handled: |
| 1465 | kfree_skb(skb); |
| 1466 | ret = 1; |
| 1467 | |
| 1468 | out: |
| 1469 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1470 | batadv_hardif_free_ref(primary_if); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1471 | if (claim) |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1472 | batadv_claim_free_ref(claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1473 | return ret; |
| 1474 | } |
| 1475 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1476 | /** |
| 1477 | * batadv_bla_tx |
| 1478 | * @bat_priv: the bat priv with all the soft interface information |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1479 | * @skb: the frame to be checked |
| 1480 | * @vid: the VLAN ID of the frame |
| 1481 | * |
| 1482 | * bla_tx checks if: |
| 1483 | * * a claim was received which has to be processed |
| 1484 | * * the frame is allowed on the mesh |
| 1485 | * |
| 1486 | * in these cases, the skb is further handled by this function and |
| 1487 | * returns 1, otherwise it returns 0 and the caller shall further |
| 1488 | * process the skb. |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1489 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1490 | int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1491 | { |
| 1492 | struct ethhdr *ethhdr; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1493 | struct batadv_claim search_claim, *claim = NULL; |
| 1494 | struct batadv_hard_iface *primary_if; |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1495 | int ret = 0; |
| 1496 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1497 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1498 | if (!primary_if) |
| 1499 | goto out; |
| 1500 | |
| 1501 | if (!atomic_read(&bat_priv->bridge_loop_avoidance)) |
| 1502 | goto allow; |
| 1503 | |
| 1504 | /* in VLAN case, the mac header might not be set. */ |
| 1505 | skb_reset_mac_header(skb); |
| 1506 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1507 | if (batadv_bla_process_claim(bat_priv, primary_if, skb)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1508 | goto handled; |
| 1509 | |
| 1510 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1511 | |
| 1512 | if (unlikely(atomic_read(&bat_priv->bla_num_requests))) |
| 1513 | /* don't allow broadcasts while requests are in flight */ |
| 1514 | if (is_multicast_ether_addr(ethhdr->h_dest)) |
| 1515 | goto handled; |
| 1516 | |
| 1517 | memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN); |
| 1518 | search_claim.vid = vid; |
| 1519 | |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1520 | claim = batadv_claim_hash_find(bat_priv, &search_claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1521 | |
| 1522 | /* if no claim exists, allow it. */ |
| 1523 | if (!claim) |
| 1524 | goto allow; |
| 1525 | |
| 1526 | /* check if we are responsible. */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1527 | if (batadv_compare_eth(claim->backbone_gw->orig, |
| 1528 | primary_if->net_dev->dev_addr)) { |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1529 | /* if yes, the client has roamed and we have |
| 1530 | * to unclaim it. |
| 1531 | */ |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1532 | batadv_handle_unclaim(bat_priv, primary_if, |
| 1533 | primary_if->net_dev->dev_addr, |
| 1534 | ethhdr->h_source, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1535 | goto allow; |
| 1536 | } |
| 1537 | |
| 1538 | /* check if it is a multicast/broadcast frame */ |
| 1539 | if (is_multicast_ether_addr(ethhdr->h_dest)) { |
| 1540 | /* drop it. the responsible gateway has forwarded it into |
| 1541 | * the backbone network. |
| 1542 | */ |
| 1543 | goto handled; |
| 1544 | } else { |
| 1545 | /* we must allow it. at least if we are |
| 1546 | * responsible for the DESTINATION. |
| 1547 | */ |
| 1548 | goto allow; |
| 1549 | } |
| 1550 | allow: |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1551 | batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1552 | ret = 0; |
| 1553 | goto out; |
| 1554 | handled: |
| 1555 | ret = 1; |
| 1556 | out: |
| 1557 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1558 | batadv_hardif_free_ref(primary_if); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1559 | if (claim) |
Sven Eckelmann | 3b300de | 2012-05-12 18:33:53 +0200 | [diff] [blame] | 1560 | batadv_claim_free_ref(claim); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1561 | return ret; |
| 1562 | } |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1563 | |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 1564 | int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset) |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1565 | { |
| 1566 | struct net_device *net_dev = (struct net_device *)seq->private; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1567 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 1568 | struct batadv_hashtable *hash = bat_priv->claim_hash; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1569 | struct batadv_claim *claim; |
| 1570 | struct batadv_hard_iface *primary_if; |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1571 | struct hlist_node *node; |
| 1572 | struct hlist_head *head; |
| 1573 | uint32_t i; |
| 1574 | bool is_own; |
| 1575 | int ret = 0; |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1576 | uint8_t *primary_addr; |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1577 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1578 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1579 | if (!primary_if) { |
| 1580 | ret = seq_printf(seq, |
| 1581 | "BATMAN mesh %s disabled - please specify interfaces to enable it\n", |
| 1582 | net_dev->name); |
| 1583 | goto out; |
| 1584 | } |
| 1585 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 1586 | if (primary_if->if_status != BATADV_IF_ACTIVE) { |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1587 | ret = seq_printf(seq, |
| 1588 | "BATMAN mesh %s disabled - primary interface not active\n", |
| 1589 | net_dev->name); |
| 1590 | goto out; |
| 1591 | } |
| 1592 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1593 | primary_addr = primary_if->net_dev->dev_addr; |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 1594 | seq_printf(seq, |
| 1595 | "Claims announced for the mesh %s (orig %pM, group id %04x)\n", |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1596 | net_dev->name, primary_addr, |
Simon Wunderlich | 38ef3d1 | 2012-01-22 20:00:26 +0100 | [diff] [blame] | 1597 | ntohs(bat_priv->claim_dest.group)); |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1598 | seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n", |
| 1599 | "Client", "VID", "Originator", "CRC"); |
| 1600 | for (i = 0; i < hash->size; i++) { |
| 1601 | head = &hash->table[i]; |
| 1602 | |
| 1603 | rcu_read_lock(); |
| 1604 | hlist_for_each_entry_rcu(claim, node, head, hash_entry) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1605 | is_own = batadv_compare_eth(claim->backbone_gw->orig, |
| 1606 | primary_addr); |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1607 | seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n", |
| 1608 | claim->addr, claim->vid, |
| 1609 | claim->backbone_gw->orig, |
| 1610 | (is_own ? 'x' : ' '), |
| 1611 | claim->backbone_gw->crc); |
| 1612 | } |
| 1613 | rcu_read_unlock(); |
| 1614 | } |
| 1615 | out: |
| 1616 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 1617 | batadv_hardif_free_ref(primary_if); |
Simon Wunderlich | 9bf8e4d | 2012-01-22 20:00:21 +0100 | [diff] [blame] | 1618 | return ret; |
| 1619 | } |
Simon Wunderlich | 536a23f | 2012-06-18 18:39:26 +0200 | [diff] [blame] | 1620 | |
| 1621 | int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset) |
| 1622 | { |
| 1623 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 1624 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
| 1625 | struct batadv_hashtable *hash = bat_priv->backbone_hash; |
| 1626 | struct batadv_backbone_gw *backbone_gw; |
| 1627 | struct batadv_hard_iface *primary_if; |
| 1628 | struct hlist_node *node; |
| 1629 | struct hlist_head *head; |
| 1630 | int secs, msecs; |
| 1631 | uint32_t i; |
| 1632 | bool is_own; |
| 1633 | int ret = 0; |
| 1634 | uint8_t *primary_addr; |
| 1635 | |
| 1636 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 1637 | if (!primary_if) { |
| 1638 | ret = seq_printf(seq, |
| 1639 | "BATMAN mesh %s disabled - please specify interfaces to enable it\n", |
| 1640 | net_dev->name); |
| 1641 | goto out; |
| 1642 | } |
| 1643 | |
| 1644 | if (primary_if->if_status != BATADV_IF_ACTIVE) { |
| 1645 | ret = seq_printf(seq, |
| 1646 | "BATMAN mesh %s disabled - primary interface not active\n", |
| 1647 | net_dev->name); |
| 1648 | goto out; |
| 1649 | } |
| 1650 | |
| 1651 | primary_addr = primary_if->net_dev->dev_addr; |
| 1652 | seq_printf(seq, |
| 1653 | "Backbones announced for the mesh %s (orig %pM, group id %04x)\n", |
| 1654 | net_dev->name, primary_addr, |
| 1655 | ntohs(bat_priv->claim_dest.group)); |
| 1656 | seq_printf(seq, " %-17s %-5s %-9s (%-4s)\n", |
| 1657 | "Originator", "VID", "last seen", "CRC"); |
| 1658 | for (i = 0; i < hash->size; i++) { |
| 1659 | head = &hash->table[i]; |
| 1660 | |
| 1661 | rcu_read_lock(); |
| 1662 | hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) { |
| 1663 | msecs = jiffies_to_msecs(jiffies - |
| 1664 | backbone_gw->lasttime); |
| 1665 | secs = msecs / 1000; |
| 1666 | msecs = msecs % 1000; |
| 1667 | |
| 1668 | is_own = batadv_compare_eth(backbone_gw->orig, |
| 1669 | primary_addr); |
| 1670 | if (is_own) |
| 1671 | continue; |
| 1672 | |
| 1673 | seq_printf(seq, |
| 1674 | " * %pM on % 5d % 4i.%03is (%04x)\n", |
| 1675 | backbone_gw->orig, backbone_gw->vid, |
| 1676 | secs, msecs, backbone_gw->crc); |
| 1677 | } |
| 1678 | rcu_read_unlock(); |
| 1679 | } |
| 1680 | out: |
| 1681 | if (primary_if) |
| 1682 | batadv_hardif_free_ref(primary_if); |
| 1683 | return ret; |
| 1684 | } |