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