blob: bd3357e69c5cb7a559b3b3d6ec6387eba3951ac6 [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2011-2014 B.A.T.M.A.N. contributors:
Simon Wunderlich23721382012-01-22 20:00:19 +01002 *
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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Simon Wunderlich23721382012-01-22 20:00:19 +010016 */
17
18#include "main.h"
19#include "hash.h"
20#include "hard-interface.h"
21#include "originator.h"
22#include "bridge_loop_avoidance.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010023#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010024#include "send.h"
25
26#include <linux/etherdevice.h>
27#include <linux/crc16.h>
28#include <linux/if_arp.h>
29#include <net/arp.h>
30#include <linux/if_vlan.h>
31
Sven Eckelmann3b300de2012-05-12 18:33:53 +020032static const uint8_t batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010033
Sven Eckelmann3b300de2012-05-12 18:33:53 +020034static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080035static void
36batadv_bla_send_announce(struct batadv_priv *bat_priv,
37 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010038
39/* return the index of the claim */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020040static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
Simon Wunderlich23721382012-01-22 20:00:19 +010041{
Marek Lindner712bbfe42012-12-25 17:03:25 +080042 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Simon Wunderlich23721382012-01-22 20:00:19 +010043 uint32_t hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010044
Simon Wunderlich07568d02012-08-30 18:22:27 +020045 hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
46 hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));
Simon Wunderlich23721382012-01-22 20:00:19 +010047
48 hash += (hash << 3);
49 hash ^= (hash >> 11);
50 hash += (hash << 15);
51
52 return hash % size;
53}
54
55/* return the index of the backbone gateway */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020056static inline uint32_t batadv_choose_backbone_gw(const void *data,
57 uint32_t size)
Simon Wunderlich23721382012-01-22 20:00:19 +010058{
Marek Lindner712bbfe42012-12-25 17:03:25 +080059 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Simon Wunderlich23721382012-01-22 20:00:19 +010060 uint32_t hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010061
Simon Wunderlich07568d02012-08-30 18:22:27 +020062 hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
63 hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));
Simon Wunderlich23721382012-01-22 20:00:19 +010064
65 hash += (hash << 3);
66 hash ^= (hash >> 11);
67 hash += (hash << 15);
68
69 return hash % size;
70}
71
Simon Wunderlich23721382012-01-22 20:00:19 +010072/* compares address and vid of two backbone gws */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020073static int batadv_compare_backbone_gw(const struct hlist_node *node,
74 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010075{
Marek Lindnerbae98772012-12-25 17:03:24 +080076 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +010077 hash_entry);
Marek Lindnerbae98772012-12-25 17:03:24 +080078 const struct batadv_bla_backbone_gw *gw1 = data1, *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +010079
Simon Wunderlichc76d1522012-10-15 22:38:04 +020080 if (!batadv_compare_eth(gw1->orig, gw2->orig))
81 return 0;
82
83 if (gw1->vid != gw2->vid)
84 return 0;
85
86 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +010087}
88
89/* compares address and vid of two claims */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020090static int batadv_compare_claim(const struct hlist_node *node,
91 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010092{
Marek Lindner712bbfe42012-12-25 17:03:25 +080093 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +010094 hash_entry);
Marek Lindner712bbfe42012-12-25 17:03:25 +080095 const struct batadv_bla_claim *cl1 = data1, *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +010096
Simon Wunderlichc76d1522012-10-15 22:38:04 +020097 if (!batadv_compare_eth(cl1->addr, cl2->addr))
98 return 0;
99
100 if (cl1->vid != cl2->vid)
101 return 0;
102
103 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100104}
105
106/* free a backbone gw */
Marek Lindnerbae98772012-12-25 17:03:24 +0800107static void
108batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100109{
110 if (atomic_dec_and_test(&backbone_gw->refcount))
111 kfree_rcu(backbone_gw, rcu);
112}
113
114/* finally deinitialize the claim */
Sven Eckelmann016cb1d2016-01-14 15:28:19 +0100115static void batadv_claim_release(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100116{
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200117 batadv_backbone_gw_free_ref(claim->backbone_gw);
Sven Eckelmann016cb1d2016-01-14 15:28:19 +0100118 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100119}
120
121/* free a claim, call claim_free_rcu if its the last reference */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800122static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100123{
124 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann016cb1d2016-01-14 15:28:19 +0100125 batadv_claim_release(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100126}
127
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100128/**
129 * batadv_claim_hash_find
130 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100131 * @data: search data (may be local/static data)
132 *
133 * looks for a claim in the hash, and returns it if found
134 * or NULL otherwise.
135 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800136static struct batadv_bla_claim
137*batadv_claim_hash_find(struct batadv_priv *bat_priv,
138 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100139{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200140 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100141 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800142 struct batadv_bla_claim *claim;
143 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100144 int index;
145
146 if (!hash)
147 return NULL;
148
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200149 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100150 head = &hash->table[index];
151
152 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800153 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200154 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100155 continue;
156
157 if (!atomic_inc_not_zero(&claim->refcount))
158 continue;
159
160 claim_tmp = claim;
161 break;
162 }
163 rcu_read_unlock();
164
165 return claim_tmp;
166}
167
Ben Hutchings2c530402012-07-10 10:55:09 +0000168/**
169 * batadv_backbone_hash_find - looks for a claim in the hash
170 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100171 * @addr: the address of the originator
172 * @vid: the VLAN ID
173 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000174 * Returns claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100175 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800176static struct batadv_bla_backbone_gw *
Sven Eckelmann56303d32012-06-05 22:31:31 +0200177batadv_backbone_hash_find(struct batadv_priv *bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200178 uint8_t *addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100179{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200180 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100181 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800182 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
183 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100184 int index;
185
186 if (!hash)
187 return NULL;
188
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100189 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100190 search_entry.vid = vid;
191
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200192 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100193 head = &hash->table[index];
194
195 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800196 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200197 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
198 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100199 continue;
200
201 if (!atomic_inc_not_zero(&backbone_gw->refcount))
202 continue;
203
204 backbone_gw_tmp = backbone_gw;
205 break;
206 }
207 rcu_read_unlock();
208
209 return backbone_gw_tmp;
210}
211
212/* delete all claims for a backbone */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200213static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800214batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100215{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200216 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800217 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100218 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800219 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100220 int i;
221 spinlock_t *list_lock; /* protects write access to the hash lists */
222
Sven Eckelmann807736f2012-07-15 22:26:51 +0200223 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100224 if (!hash)
225 return;
226
227 for (i = 0; i < hash->size; i++) {
228 head = &hash->table[i];
229 list_lock = &hash->list_locks[i];
230
231 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800232 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100233 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100234 if (claim->backbone_gw != backbone_gw)
235 continue;
236
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200237 batadv_claim_free_ref(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800238 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100239 }
240 spin_unlock_bh(list_lock);
241 }
242
Antonio Quartulli3f687852014-11-02 11:29:56 +0100243 /* all claims gone, initialize CRC */
Sven Eckelmann3964f722012-06-03 22:19:10 +0200244 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100245}
246
Ben Hutchings2c530402012-07-10 10:55:09 +0000247/**
248 * batadv_bla_send_claim - sends a claim frame according to the provided info
249 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200250 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100251 * @vid: the VLAN ID
252 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100253 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200254static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200255 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100256{
257 struct sk_buff *skb;
258 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200259 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100260 struct net_device *soft_iface;
261 uint8_t *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200262 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100263 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100264
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200265 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100266 if (!primary_if)
267 return;
268
Sven Eckelmann807736f2012-07-15 22:26:51 +0200269 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100270 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100271 local_claim_dest.type = claimtype;
272
273 soft_iface = primary_if->soft_iface;
274
275 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
276 /* IP DST: 0.0.0.0 */
277 zeroip,
278 primary_if->soft_iface,
279 /* IP SRC: 0.0.0.0 */
280 zeroip,
281 /* Ethernet DST: Broadcast */
282 NULL,
283 /* Ethernet SRC/HW SRC: originator mac */
284 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200285 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100286 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100287 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100288 */
289 (uint8_t *)&local_claim_dest);
290
291 if (!skb)
292 goto out;
293
294 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100295 hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100296
297 /* now we pretend that the client would have sent this ... */
298 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200299 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100300 /* normal claim frame
301 * set Ethernet SRC to the clients mac
302 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100303 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200304 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200305 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
306 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100307 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200308 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100309 /* unclaim frame
310 * set HW SRC to the clients mac
311 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100312 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200313 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200314 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200315 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100316 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200317 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100318 /* announcement frame
319 * set HW SRC to the special mac containg the crc
320 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100321 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200322 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200323 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200324 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100325 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200326 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100327 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200328 * set HW SRC and header destination to the receiving backbone
329 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100330 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100331 ether_addr_copy(hw_src, mac);
332 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200333 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200334 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200335 ethhdr->h_source, ethhdr->h_dest,
336 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100337 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100338 }
339
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200340 if (vid & BATADV_VLAN_HAS_TAG)
341 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
342 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100343
344 skb_reset_mac_header(skb);
345 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200346 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
347 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
348 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100349 soft_iface->last_rx = jiffies;
350
351 netif_rx(skb);
352out:
353 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200354 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100355}
356
Ben Hutchings2c530402012-07-10 10:55:09 +0000357/**
358 * batadv_bla_get_backbone_gw
359 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100360 * @orig: the mac address of the originator
361 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200362 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100363 *
364 * searches for the backbone gw or creates a new one if it could not
365 * be found.
366 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800367static struct batadv_bla_backbone_gw *
Sven Eckelmann56303d32012-06-05 22:31:31 +0200368batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200369 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100370{
Marek Lindnerbae98772012-12-25 17:03:24 +0800371 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200372 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100373 int hash_added;
374
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200375 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100376
377 if (entry)
378 return entry;
379
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200380 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200381 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200382 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100383
384 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
385 if (!entry)
386 return NULL;
387
388 entry->vid = vid;
389 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200390 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100391 entry->bat_priv = bat_priv;
392 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200393 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100394 ether_addr_copy(entry->orig, orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100395
396 /* one for the hash, one for returning */
397 atomic_set(&entry->refcount, 2);
398
Sven Eckelmann807736f2012-07-15 22:26:51 +0200399 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200400 batadv_compare_backbone_gw,
401 batadv_choose_backbone_gw, entry,
402 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100403
404 if (unlikely(hash_added != 0)) {
405 /* hash failed, free the structure */
406 kfree(entry);
407 return NULL;
408 }
409
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200410 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200411 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100412 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200413 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200414 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200415 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100416 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200417
Simon Wunderlichd807f272012-09-09 22:27:57 +0200418 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200419 batadv_bla_send_announce(bat_priv, entry);
420
Simon Wunderlichd807f272012-09-09 22:27:57 +0200421 /* this will be decreased in the worker thread */
422 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200423 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200424 atomic_inc(&bat_priv->bla.num_requests);
425 }
426
Simon Wunderlich23721382012-01-22 20:00:19 +0100427 return entry;
428}
429
430/* update or add the own backbone gw to make sure we announce
431 * where we receive other backbone gws
432 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200433static void
434batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
435 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200436 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100437{
Marek Lindnerbae98772012-12-25 17:03:24 +0800438 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100439
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200440 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
441 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200442 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100443 if (unlikely(!backbone_gw))
444 return;
445
446 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200447 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100448}
449
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100450/**
451 * batadv_bla_answer_request - answer a bla request by sending own claims
452 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200453 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100454 * @vid: the vid where the request came on
455 *
456 * Repeat all of our own claims, and finally send an ANNOUNCE frame
457 * to allow the requester another check if the CRC is correct now.
458 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200459static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
460 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200461 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100462{
Simon Wunderlich23721382012-01-22 20:00:19 +0100463 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200464 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800465 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800466 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100467 int i;
468
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200469 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200470 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100471
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200472 backbone_gw = batadv_backbone_hash_find(bat_priv,
473 primary_if->net_dev->dev_addr,
474 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100475 if (!backbone_gw)
476 return;
477
Sven Eckelmann807736f2012-07-15 22:26:51 +0200478 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100479 for (i = 0; i < hash->size; i++) {
480 head = &hash->table[i];
481
482 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800483 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100484 /* only own claims are interesting */
485 if (claim->backbone_gw != backbone_gw)
486 continue;
487
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200488 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200489 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100490 }
491 rcu_read_unlock();
492 }
493
494 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200495 batadv_bla_send_announce(bat_priv, backbone_gw);
496 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100497}
498
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100499/**
500 * batadv_bla_send_request - send a request to repeat claims
501 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100502 *
503 * When the crc is wrong, ask the backbone gateway for a full table update.
504 * After the request, it will repeat all of his own claims and finally
505 * send an announcement claim with which we can check again.
506 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800507static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100508{
509 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200510 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100511
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200512 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
513 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100514
515 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200516 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200517 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100518
519 /* no local broadcasts should be sent or received, for now. */
520 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200521 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100522 atomic_set(&backbone_gw->request_sent, 1);
523 }
524}
525
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100526/**
527 * batadv_bla_send_announce
528 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100529 * @backbone_gw: our backbone gateway which should be announced
530 *
531 * This function sends an announcement. It is called from multiple
532 * places.
533 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200534static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800535 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100536{
537 uint8_t mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100538 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100539
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200540 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich23721382012-01-22 20:00:19 +0100541 crc = htons(backbone_gw->crc);
Al Viro1a5852d2012-04-22 07:50:29 +0100542 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100543
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200544 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200545 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100546}
547
Ben Hutchings2c530402012-07-10 10:55:09 +0000548/**
549 * batadv_bla_add_claim - Adds a claim in the claim hash
550 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100551 * @mac: the mac address of the claim
552 * @vid: the VLAN ID of the frame
553 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100554 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200555static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200556 const uint8_t *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800557 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100558{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800559 struct batadv_bla_claim *claim;
560 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100561 int hash_added;
562
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100563 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100564 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200565 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100566
567 /* create a new claim entry if it does not exist yet. */
568 if (!claim) {
569 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
570 if (!claim)
571 return;
572
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100573 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100574 claim->vid = vid;
575 claim->lasttime = jiffies;
576 claim->backbone_gw = backbone_gw;
577
578 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200579 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200580 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200581 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200582 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200583 batadv_compare_claim,
584 batadv_choose_claim, claim,
585 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100586
587 if (unlikely(hash_added != 0)) {
588 /* only local changes happened. */
589 kfree(claim);
590 return;
591 }
592 } else {
593 claim->lasttime = jiffies;
594 if (claim->backbone_gw == backbone_gw)
595 /* no need to register a new backbone */
596 goto claim_free_ref;
597
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200598 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200599 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200600 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100601
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200602 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200603 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100604 }
605 /* set (new) backbone gw */
606 atomic_inc(&backbone_gw->refcount);
607 claim->backbone_gw = backbone_gw;
608
609 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
610 backbone_gw->lasttime = jiffies;
611
612claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200613 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100614}
615
616/* Delete a claim from the claim hash which has the
617 * given mac address and vid.
618 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200619static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200620 const uint8_t *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100621{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800622 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100623
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100624 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100625 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200626 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100627 if (!claim)
628 return;
629
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200630 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200631 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100632
Sven Eckelmann807736f2012-07-15 22:26:51 +0200633 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200634 batadv_choose_claim, claim);
635 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100636
637 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
638
639 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200640 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100641}
642
643/* check for ANNOUNCE frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200644static int batadv_handle_announce(struct batadv_priv *bat_priv,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200645 uint8_t *an_addr, uint8_t *backbone_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200646 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100647{
Marek Lindnerbae98772012-12-25 17:03:24 +0800648 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100649 uint16_t crc;
650
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200651 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100652 return 0;
653
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200654 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
655 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100656
657 if (unlikely(!backbone_gw))
658 return 1;
659
Simon Wunderlich23721382012-01-22 20:00:19 +0100660 /* handle as ANNOUNCE frame */
661 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100662 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100663
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200664 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100665 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200666 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100667
668 if (backbone_gw->crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200669 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100670 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200671 backbone_gw->orig,
672 BATADV_PRINT_VID(backbone_gw->vid),
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200673 backbone_gw->crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100674
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200675 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100676 } else {
677 /* if we have sent a request and the crc was OK,
678 * we can allow traffic again.
679 */
680 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200681 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100682 atomic_set(&backbone_gw->request_sent, 0);
683 }
684 }
685
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200686 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100687 return 1;
688}
689
690/* check for REQUEST frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200691static int batadv_handle_request(struct batadv_priv *bat_priv,
692 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200693 uint8_t *backbone_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200694 struct ethhdr *ethhdr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100695{
696 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200697 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100698 return 0;
699
700 /* sanity check, this should not happen on a normal switch,
701 * we ignore it in this case.
702 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200703 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100704 return 1;
705
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200706 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200707 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200708 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100709
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200710 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100711 return 1;
712}
713
714/* check for UNCLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200715static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
716 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200717 uint8_t *backbone_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200718 uint8_t *claim_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100719{
Marek Lindnerbae98772012-12-25 17:03:24 +0800720 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100721
722 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200723 if (primary_if && batadv_compare_eth(backbone_addr,
724 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200725 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200726 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100727
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200728 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100729
730 if (!backbone_gw)
731 return 1;
732
733 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200734 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200735 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200736 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100737
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200738 batadv_bla_del_claim(bat_priv, claim_addr, vid);
739 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100740 return 1;
741}
742
743/* check for CLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200744static int batadv_handle_claim(struct batadv_priv *bat_priv,
745 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200746 uint8_t *backbone_addr, uint8_t *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200747 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100748{
Marek Lindnerbae98772012-12-25 17:03:24 +0800749 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100750
751 /* register the gateway if not yet available, and add the claim. */
752
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200753 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
754 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100755
756 if (unlikely(!backbone_gw))
757 return 1;
758
759 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200760 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200761 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200762 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200763 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100764
765 /* TODO: we could call something like tt_local_del() here. */
766
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200767 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100768 return 1;
769}
770
Ben Hutchings2c530402012-07-10 10:55:09 +0000771/**
772 * batadv_check_claim_group
773 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200774 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100775 * @hw_src: the Hardware source in the ARP Header
776 * @hw_dst: the Hardware destination in the ARP Header
777 * @ethhdr: pointer to the Ethernet header of the claim frame
778 *
779 * checks if it is a claim packet and if its on the same group.
780 * This function also applies the group ID of the sender
781 * if it is in the same mesh.
782 *
783 * returns:
784 * 2 - if it is a claim packet and on the same group
785 * 1 - if is a claim packet from another group
786 * 0 - if it is not a claim packet
787 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200788static int batadv_check_claim_group(struct batadv_priv *bat_priv,
789 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200790 uint8_t *hw_src, uint8_t *hw_dst,
791 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100792{
793 uint8_t *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200794 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200795 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100796
Sven Eckelmann96412692012-06-05 22:31:30 +0200797 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200798 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100799
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100800 /* if announcement packet, use the source,
801 * otherwise assume it is in the hw_src
802 */
803 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200804 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100805 backbone_addr = hw_src;
806 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200807 case BATADV_CLAIM_TYPE_REQUEST:
808 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200809 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100810 backbone_addr = ethhdr->h_source;
811 break;
812 default:
813 return 0;
814 }
815
816 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200817 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100818 return 0;
819
820 /* if its already the same group, it is fine. */
821 if (bla_dst->group == bla_dst_own->group)
822 return 2;
823
824 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200825 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100826
827 /* dont accept claims from gateways which are not in
828 * the same mesh or group.
829 */
830 if (!orig_node)
831 return 1;
832
833 /* if our mesh friends mac is bigger, use it for ourselves. */
834 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200835 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100836 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200837 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100838 bla_dst_own->group = bla_dst->group;
839 }
840
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200841 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100842
843 return 2;
844}
845
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100846/**
847 * batadv_bla_process_claim
848 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200849 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +0100850 * @skb: the frame to be checked
851 *
852 * Check if this is a claim frame, and process it accordingly.
853 *
854 * returns 1 if it was a claim frame, otherwise return 0 to
855 * tell the callee that it can use the frame on its own.
856 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200857static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
858 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200859 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100860{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200861 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200862 uint8_t *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200863 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200864 struct ethhdr *ethhdr;
865 struct arphdr *arphdr;
866 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200867 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200868 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +0100869 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100870 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100871
Antonio Quartullic018ad32013-06-04 12:11:39 +0200872 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200873 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100874
Antonio Quartullic018ad32013-06-04 12:11:39 +0200875 proto = ethhdr->h_proto;
876 headlen = ETH_HLEN;
877 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200878 /* Traverse the VLAN/Ethertypes.
879 *
880 * At this point it is known that the first protocol is a VLAN
881 * header, so start checking at the encapsulated protocol.
882 *
883 * The depth of the VLAN headers is recorded to drop BLA claim
884 * frames encapsulated into multiple VLAN headers (QinQ).
885 */
886 do {
887 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
888 &vhdr_buf);
889 if (!vhdr)
890 return 0;
891
892 proto = vhdr->h_vlan_encapsulated_proto;
893 headlen += VLAN_HLEN;
894 vlan_depth++;
895 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +0100896 }
897
Antonio Quartulli293e9332013-05-19 12:55:16 +0200898 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +0100899 return 0; /* not a claim frame */
900
901 /* this must be a ARP frame. check if it is a claim. */
902
903 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
904 return 0;
905
906 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200907 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100908 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
909
910 /* Check whether the ARP frame carries a valid
911 * IP information
912 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100913 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
914 return 0;
915 if (arphdr->ar_pro != htons(ETH_P_IP))
916 return 0;
917 if (arphdr->ar_hln != ETH_ALEN)
918 return 0;
919 if (arphdr->ar_pln != 4)
920 return 0;
921
922 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
923 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +0200924 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200925 bla_dst_own = &bat_priv->bla.claim_dest;
926
927 /* check if it is a claim frame in general */
928 if (memcmp(bla_dst->magic, bla_dst_own->magic,
929 sizeof(bla_dst->magic)) != 0)
930 return 0;
931
932 /* check if there is a claim frame encapsulated deeper in (QinQ) and
933 * drop that, as this is not supported by BLA but should also not be
934 * sent via the mesh.
935 */
936 if (vlan_depth > 1)
937 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100938
939 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200940 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
941 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100942 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200943 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200944 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200945 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
946 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100947
948 if (ret < 2)
949 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100950
951 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200952 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100953
954 /* check for the different types of claim frames ... */
955 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200956 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200957 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
958 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100959 return 1;
960 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200961 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200962 if (batadv_handle_unclaim(bat_priv, primary_if,
963 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100964 return 1;
965 break;
966
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200967 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200968 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
969 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100970 return 1;
971 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200972 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200973 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
974 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100975 return 1;
976 break;
977 }
978
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200979 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200980 "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",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200981 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +0100982 return 1;
983}
984
985/* Check when we last heard from other nodes, and remove them in case of
986 * a time out, or clean all backbone gws if now is set.
987 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200988static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +0100989{
Marek Lindnerbae98772012-12-25 17:03:24 +0800990 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800991 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100992 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200993 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100994 spinlock_t *list_lock; /* protects write access to the hash lists */
995 int i;
996
Sven Eckelmann807736f2012-07-15 22:26:51 +0200997 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100998 if (!hash)
999 return;
1000
1001 for (i = 0; i < hash->size; i++) {
1002 head = &hash->table[i];
1003 list_lock = &hash->list_locks[i];
1004
1005 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001006 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001007 head, hash_entry) {
1008 if (now)
1009 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001010 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001011 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001012 continue;
1013
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001014 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001015 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1016 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001017
1018purge_now:
1019 /* don't wait for the pending request anymore */
1020 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001021 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001022
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001023 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001024
Sasha Levinb67bfe02013-02-27 17:06:00 -08001025 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001026 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001027 }
1028 spin_unlock_bh(list_lock);
1029 }
1030}
1031
Ben Hutchings2c530402012-07-10 10:55:09 +00001032/**
1033 * batadv_bla_purge_claims
1034 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001035 * @primary_if: the selected primary interface, may be NULL if now is set
1036 * @now: whether the whole hash shall be wiped now
1037 *
1038 * Check when we heard last time from our own claims, and remove them in case of
1039 * a time out, or clean all claims if now is set
1040 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001041static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1042 struct batadv_hard_iface *primary_if,
1043 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001044{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001045 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001046 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001047 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001048 int i;
1049
Sven Eckelmann807736f2012-07-15 22:26:51 +02001050 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001051 if (!hash)
1052 return;
1053
1054 for (i = 0; i < hash->size; i++) {
1055 head = &hash->table[i];
1056
1057 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001058 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001059 if (now)
1060 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001061 if (!batadv_compare_eth(claim->backbone_gw->orig,
1062 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001063 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001064 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001065 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001066 continue;
1067
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001068 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001069 "bla_purge_claims(): %pM, vid %d, time out\n",
1070 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001071
1072purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001073 batadv_handle_unclaim(bat_priv, primary_if,
1074 claim->backbone_gw->orig,
1075 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001076 }
1077 rcu_read_unlock();
1078 }
1079}
1080
Ben Hutchings2c530402012-07-10 10:55:09 +00001081/**
1082 * batadv_bla_update_orig_address
1083 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001084 * @primary_if: the new selected primary_if
1085 * @oldif: the old primary interface, may be NULL
1086 *
1087 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001088 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001089void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1090 struct batadv_hard_iface *primary_if,
1091 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001092{
Marek Lindnerbae98772012-12-25 17:03:24 +08001093 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001094 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001095 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001096 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001097 int i;
1098
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001099 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001100 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1101 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001102
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001103 /* purge everything when bridge loop avoidance is turned off */
1104 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1105 oldif = NULL;
1106
Simon Wunderlich23721382012-01-22 20:00:19 +01001107 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001108 batadv_bla_purge_claims(bat_priv, NULL, 1);
1109 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001110 return;
1111 }
1112
Sven Eckelmann807736f2012-07-15 22:26:51 +02001113 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001114 if (!hash)
1115 return;
1116
1117 for (i = 0; i < hash->size; i++) {
1118 head = &hash->table[i];
1119
1120 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001121 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001122 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001123 if (!batadv_compare_eth(backbone_gw->orig,
1124 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001125 continue;
1126
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001127 ether_addr_copy(backbone_gw->orig,
1128 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001129 /* send an announce frame so others will ask for our
1130 * claims and update their tables.
1131 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001132 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001133 }
1134 rcu_read_unlock();
1135 }
1136}
1137
Simon Wunderlich23721382012-01-22 20:00:19 +01001138/* periodic work to do:
1139 * * purge structures when they are too old
1140 * * send announcements
1141 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001142static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001143{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001144 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001145 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001146 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001147 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001148 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001149 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001150 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001151 int i;
1152
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001153 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001154 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1155 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001156 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001157 if (!primary_if)
1158 goto out;
1159
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001160 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1161 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001162
1163 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1164 goto out;
1165
Sven Eckelmann807736f2012-07-15 22:26:51 +02001166 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001167 if (!hash)
1168 goto out;
1169
1170 for (i = 0; i < hash->size; i++) {
1171 head = &hash->table[i];
1172
1173 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001174 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001175 if (!batadv_compare_eth(backbone_gw->orig,
1176 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001177 continue;
1178
1179 backbone_gw->lasttime = jiffies;
1180
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001181 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001182
1183 /* request_sent is only set after creation to avoid
1184 * problems when we are not yet known as backbone gw
1185 * in the backbone.
1186 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001187 * We can reset this now after we waited some periods
1188 * to give bridge forward delays and bla group forming
1189 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001190 */
1191
1192 if (atomic_read(&backbone_gw->request_sent) == 0)
1193 continue;
1194
Simon Wunderlich28709872012-09-13 18:18:46 +02001195 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1196 continue;
1197
Simon Wunderlichd807f272012-09-09 22:27:57 +02001198 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1199 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001200 }
1201 rcu_read_unlock();
1202 }
1203out:
1204 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001205 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001206
Antonio Quartulli72414442012-12-25 13:14:37 +01001207 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1208 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001209}
1210
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001211/* The hash for claim and backbone hash receive the same key because they
1212 * are getting initialized by hash_new with the same key. Reinitializing
1213 * them with to different keys to allow nested locking without generating
1214 * lockdep warnings
1215 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001216static struct lock_class_key batadv_claim_hash_lock_class_key;
1217static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001218
Simon Wunderlich23721382012-01-22 20:00:19 +01001219/* initialize all bla structures */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001220int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001221{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001222 int i;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001223 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001224 struct batadv_hard_iface *primary_if;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001225 uint16_t crc;
1226 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001227
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001228 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1229
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001230 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001231
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001232 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001233 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1234 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001235 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001236 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001237 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1238 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001239 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001240 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001241 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001242 }
1243
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001244 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001245 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001246 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001247 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1248 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001249
Sven Eckelmann807736f2012-07-15 22:26:51 +02001250 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001251 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001252
Sven Eckelmann807736f2012-07-15 22:26:51 +02001253 bat_priv->bla.claim_hash = batadv_hash_new(128);
1254 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001255
Sven Eckelmann807736f2012-07-15 22:26:51 +02001256 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001257 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001258
Sven Eckelmann807736f2012-07-15 22:26:51 +02001259 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001260 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001261 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001262 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001263
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001264 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001265
Antonio Quartulli72414442012-12-25 13:14:37 +01001266 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1267
1268 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1269 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001270 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001271}
1272
Ben Hutchings2c530402012-07-10 10:55:09 +00001273/**
1274 * batadv_bla_check_bcast_duplist
1275 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001276 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001277 *
1278 * check if it is on our broadcast list. Another gateway might
1279 * have sent the same packet because it is connected to the same backbone,
1280 * so we have to remove this duplicate.
1281 *
1282 * This is performed by checking the CRC, which will tell us
1283 * with a good chance that it is the same packet. If it is furthermore
1284 * sent by another host, drop it. We allow equal packets from
1285 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001286 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001287int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001288 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001289{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001290 int i, curr, ret = 0;
1291 __be32 crc;
1292 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001293 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001294
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001295 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001296
1297 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001298 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001299
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001300 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1301
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001302 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001303 curr = (bat_priv->bla.bcast_duplist_curr + i);
1304 curr %= BATADV_DUPLIST_SIZE;
1305 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001306
1307 /* we can stop searching if the entry is too old ;
1308 * later entries will be even older
1309 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001310 if (batadv_has_timed_out(entry->entrytime,
1311 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001312 break;
1313
1314 if (entry->crc != crc)
1315 continue;
1316
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001317 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001318 continue;
1319
1320 /* this entry seems to match: same crc, not too old,
1321 * and from another gw. therefore return 1 to forbid it.
1322 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001323 ret = 1;
1324 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001325 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001326 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001327 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001328 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001329 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001330 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001331 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001332 entry->crc = crc;
1333 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001334 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001335 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001336
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001337out:
1338 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1339
1340 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001341}
1342
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001343/**
1344 * batadv_bla_is_backbone_gw_orig
1345 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001346 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001347 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001348 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001349 * Check if the originator is a gateway for the VLAN identified by vid.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001350 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001351 * Returns true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001352 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001353bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig,
1354 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001355{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001356 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001357 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001358 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001359 int i;
1360
1361 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001362 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001363
1364 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001365 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001366
1367 for (i = 0; i < hash->size; i++) {
1368 head = &hash->table[i];
1369
1370 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001371 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001372 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1373 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001374 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001375 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001376 }
1377 }
1378 rcu_read_unlock();
1379 }
1380
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001381 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001382}
1383
Ben Hutchings2c530402012-07-10 10:55:09 +00001384/**
1385 * batadv_bla_is_backbone_gw
1386 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001387 * @orig_node: the orig_node of the frame
1388 * @hdr_size: maximum length of the frame
1389 *
1390 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1391 * if the orig_node is also a gateway on the soft interface, otherwise it
1392 * returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001393 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001394int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001395 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001396{
Marek Lindnerbae98772012-12-25 17:03:24 +08001397 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001398 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001399
1400 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1401 return 0;
1402
1403 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001404 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001405 return 0;
1406
Antonio Quartullic018ad32013-06-04 12:11:39 +02001407 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001408
1409 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001410 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1411 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001412 if (!backbone_gw)
1413 return 0;
1414
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001415 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001416 return 1;
1417}
1418
1419/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001420void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001421{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001422 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001423
Sven Eckelmann807736f2012-07-15 22:26:51 +02001424 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001425 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001426
Sven Eckelmann807736f2012-07-15 22:26:51 +02001427 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001428 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001429 batadv_hash_destroy(bat_priv->bla.claim_hash);
1430 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001431 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001432 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001433 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001434 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1435 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001436 }
1437 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001438 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001439}
1440
Ben Hutchings2c530402012-07-10 10:55:09 +00001441/**
1442 * batadv_bla_rx
1443 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001444 * @skb: the frame to be checked
1445 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001446 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001447 *
1448 * bla_rx avoidance checks if:
1449 * * we have to race for a claim
1450 * * if the frame is allowed on the LAN
1451 *
1452 * in these cases, the skb is further handled by this function and
1453 * returns 1, otherwise it returns 0 and the caller shall further
1454 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001455 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001456int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1457 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001458{
1459 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001460 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001461 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001462 int ret;
1463
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001464 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001465
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001466 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001467 if (!primary_if)
1468 goto handled;
1469
1470 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1471 goto allow;
1472
Sven Eckelmann807736f2012-07-15 22:26:51 +02001473 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001474 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001475 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001476 goto handled;
1477
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001478 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001479 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001480 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001481
1482 if (!claim) {
1483 /* possible optimization: race for a claim */
1484 /* No claim exists yet, claim it for us!
1485 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001486 batadv_handle_claim(bat_priv, primary_if,
1487 primary_if->net_dev->dev_addr,
1488 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001489 goto allow;
1490 }
1491
1492 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001493 if (batadv_compare_eth(claim->backbone_gw->orig,
1494 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001495 /* ... allow it in any case */
1496 claim->lasttime = jiffies;
1497 goto allow;
1498 }
1499
1500 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001501 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1502 /* ... drop it. the responsible gateway is in charge.
1503 *
1504 * We need to check is_bcast because with the gateway
1505 * feature, broadcasts (like DHCP requests) may be sent
1506 * using a unicast packet type.
1507 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001508 goto handled;
1509 } else {
1510 /* seems the client considers us as its best gateway.
1511 * send a claim and update the claim table
1512 * immediately.
1513 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001514 batadv_handle_claim(bat_priv, primary_if,
1515 primary_if->net_dev->dev_addr,
1516 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001517 goto allow;
1518 }
1519allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001520 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001521 ret = 0;
1522 goto out;
1523
1524handled:
1525 kfree_skb(skb);
1526 ret = 1;
1527
1528out:
1529 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001530 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001531 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001532 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001533 return ret;
1534}
1535
Ben Hutchings2c530402012-07-10 10:55:09 +00001536/**
1537 * batadv_bla_tx
1538 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001539 * @skb: the frame to be checked
1540 * @vid: the VLAN ID of the frame
1541 *
1542 * bla_tx checks if:
1543 * * a claim was received which has to be processed
1544 * * the frame is allowed on the mesh
1545 *
1546 * in these cases, the skb is further handled by this function and
1547 * returns 1, otherwise it returns 0 and the caller shall further
1548 * process the skb.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001549 *
1550 * This call might reallocate skb data.
Simon Wunderlich23721382012-01-22 20:00:19 +01001551 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001552int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1553 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001554{
1555 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001556 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001557 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001558 int ret = 0;
1559
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001560 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001561 if (!primary_if)
1562 goto out;
1563
1564 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1565 goto allow;
1566
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001567 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001568 goto handled;
1569
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001570 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001571
Sven Eckelmann807736f2012-07-15 22:26:51 +02001572 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001573 /* don't allow broadcasts while requests are in flight */
1574 if (is_multicast_ether_addr(ethhdr->h_dest))
1575 goto handled;
1576
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001577 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001578 search_claim.vid = vid;
1579
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001580 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001581
1582 /* if no claim exists, allow it. */
1583 if (!claim)
1584 goto allow;
1585
1586 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001587 if (batadv_compare_eth(claim->backbone_gw->orig,
1588 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001589 /* if yes, the client has roamed and we have
1590 * to unclaim it.
1591 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001592 batadv_handle_unclaim(bat_priv, primary_if,
1593 primary_if->net_dev->dev_addr,
1594 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001595 goto allow;
1596 }
1597
1598 /* check if it is a multicast/broadcast frame */
1599 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1600 /* drop it. the responsible gateway has forwarded it into
1601 * the backbone network.
1602 */
1603 goto handled;
1604 } else {
1605 /* we must allow it. at least if we are
1606 * responsible for the DESTINATION.
1607 */
1608 goto allow;
1609 }
1610allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001611 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001612 ret = 0;
1613 goto out;
1614handled:
1615 ret = 1;
1616out:
1617 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001618 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001619 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001620 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001621 return ret;
1622}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001623
Sven Eckelmann08adf152012-05-12 13:38:47 +02001624int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001625{
1626 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001627 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001628 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001629 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001630 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001631 struct hlist_head *head;
1632 uint32_t i;
1633 bool is_own;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001634 uint8_t *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001635
Marek Lindner30da63a2012-08-03 17:15:46 +02001636 primary_if = batadv_seq_print_text_primary_if_get(seq);
1637 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001638 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001639
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001640 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001641 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001642 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001643 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001644 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001645 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001646 "Client", "VID", "Originator", "CRC");
1647 for (i = 0; i < hash->size; i++) {
1648 head = &hash->table[i];
1649
1650 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001651 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001652 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1653 primary_addr);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001654 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001655 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001656 claim->backbone_gw->orig,
1657 (is_own ? 'x' : ' '),
1658 claim->backbone_gw->crc);
1659 }
1660 rcu_read_unlock();
1661 }
1662out:
1663 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001664 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001665 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001666}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001667
1668int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1669{
1670 struct net_device *net_dev = (struct net_device *)seq->private;
1671 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001672 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001673 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001674 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001675 struct hlist_head *head;
1676 int secs, msecs;
1677 uint32_t i;
1678 bool is_own;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001679 uint8_t *primary_addr;
1680
Marek Lindner30da63a2012-08-03 17:15:46 +02001681 primary_if = batadv_seq_print_text_primary_if_get(seq);
1682 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001683 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001684
1685 primary_addr = primary_if->net_dev->dev_addr;
1686 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001687 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001688 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001689 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001690 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001691 "Originator", "VID", "last seen", "CRC");
1692 for (i = 0; i < hash->size; i++) {
1693 head = &hash->table[i];
1694
1695 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001696 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001697 msecs = jiffies_to_msecs(jiffies -
1698 backbone_gw->lasttime);
1699 secs = msecs / 1000;
1700 msecs = msecs % 1000;
1701
1702 is_own = batadv_compare_eth(backbone_gw->orig,
1703 primary_addr);
1704 if (is_own)
1705 continue;
1706
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001707 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001708 backbone_gw->orig,
1709 BATADV_PRINT_VID(backbone_gw->vid), secs,
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001710 msecs, backbone_gw->crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001711 }
1712 rcu_read_unlock();
1713 }
1714out:
1715 if (primary_if)
1716 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001717 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001718}