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