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