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