blob: 9971892bcbf83f18ac5e032703722765756d567f [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2011-2012 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
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 Wunderlich23721382012-01-22 20:00:19 +010018 */
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 Wunderlich20ff9d52012-01-22 20:00:23 +010025#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010026#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 Eckelmann3b300de2012-05-12 18:33:53 +020034static const uint8_t batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010035
Sven Eckelmann3b300de2012-05-12 18:33:53 +020036static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080037static void
38batadv_bla_send_announce(struct batadv_priv *bat_priv,
39 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010040
41/* return the index of the claim */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020042static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
Simon Wunderlich23721382012-01-22 20:00:19 +010043{
Marek Lindner712bbfe42012-12-25 17:03:25 +080044 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Simon Wunderlich23721382012-01-22 20:00:19 +010045 uint32_t hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010046
Simon Wunderlich07568d02012-08-30 18:22:27 +020047 hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
48 hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));
Simon Wunderlich23721382012-01-22 20:00:19 +010049
50 hash += (hash << 3);
51 hash ^= (hash >> 11);
52 hash += (hash << 15);
53
54 return hash % size;
55}
56
57/* return the index of the backbone gateway */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020058static inline uint32_t batadv_choose_backbone_gw(const void *data,
59 uint32_t size)
Simon Wunderlich23721382012-01-22 20:00:19 +010060{
Marek Lindner712bbfe42012-12-25 17:03:25 +080061 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Simon Wunderlich23721382012-01-22 20:00:19 +010062 uint32_t hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010063
Simon Wunderlich07568d02012-08-30 18:22:27 +020064 hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
65 hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));
Simon Wunderlich23721382012-01-22 20:00:19 +010066
67 hash += (hash << 3);
68 hash ^= (hash >> 11);
69 hash += (hash << 15);
70
71 return hash % size;
72}
73
74
75/* compares address and vid of two backbone gws */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020076static int batadv_compare_backbone_gw(const struct hlist_node *node,
77 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010078{
Marek Lindnerbae98772012-12-25 17:03:24 +080079 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +010080 hash_entry);
Marek Lindnerbae98772012-12-25 17:03:24 +080081 const struct batadv_bla_backbone_gw *gw1 = data1, *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +010082
Simon Wunderlichc76d1522012-10-15 22:38:04 +020083 if (!batadv_compare_eth(gw1->orig, gw2->orig))
84 return 0;
85
86 if (gw1->vid != gw2->vid)
87 return 0;
88
89 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +010090}
91
92/* compares address and vid of two claims */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020093static int batadv_compare_claim(const struct hlist_node *node,
94 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010095{
Marek Lindner712bbfe42012-12-25 17:03:25 +080096 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +010097 hash_entry);
Marek Lindner712bbfe42012-12-25 17:03:25 +080098 const struct batadv_bla_claim *cl1 = data1, *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +010099
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200100 if (!batadv_compare_eth(cl1->addr, cl2->addr))
101 return 0;
102
103 if (cl1->vid != cl2->vid)
104 return 0;
105
106 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100107}
108
109/* free a backbone gw */
Marek Lindnerbae98772012-12-25 17:03:24 +0800110static void
111batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100112{
113 if (atomic_dec_and_test(&backbone_gw->refcount))
114 kfree_rcu(backbone_gw, rcu);
115}
116
117/* finally deinitialize the claim */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200118static void batadv_claim_free_rcu(struct rcu_head *rcu)
Simon Wunderlich23721382012-01-22 20:00:19 +0100119{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800120 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100121
Marek Lindner712bbfe42012-12-25 17:03:25 +0800122 claim = container_of(rcu, struct batadv_bla_claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100123
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200124 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100125 kfree(claim);
126}
127
128/* free a claim, call claim_free_rcu if its the last reference */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800129static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100130{
131 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200132 call_rcu(&claim->rcu, batadv_claim_free_rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100133}
134
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200135/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100136 * @data: search data (may be local/static data)
137 *
138 * looks for a claim in the hash, and returns it if found
139 * or NULL otherwise.
140 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800141static struct batadv_bla_claim
142*batadv_claim_hash_find(struct batadv_priv *bat_priv,
143 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100144{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200145 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100146 struct hlist_head *head;
147 struct hlist_node *node;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800148 struct batadv_bla_claim *claim;
149 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100150 int index;
151
152 if (!hash)
153 return NULL;
154
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200155 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100156 head = &hash->table[index];
157
158 rcu_read_lock();
159 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200160 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100161 continue;
162
163 if (!atomic_inc_not_zero(&claim->refcount))
164 continue;
165
166 claim_tmp = claim;
167 break;
168 }
169 rcu_read_unlock();
170
171 return claim_tmp;
172}
173
Ben Hutchings2c530402012-07-10 10:55:09 +0000174/**
175 * batadv_backbone_hash_find - looks for a claim in the hash
176 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100177 * @addr: the address of the originator
178 * @vid: the VLAN ID
179 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000180 * Returns claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100181 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800182static struct batadv_bla_backbone_gw *
Sven Eckelmann56303d32012-06-05 22:31:31 +0200183batadv_backbone_hash_find(struct batadv_priv *bat_priv,
184 uint8_t *addr, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100185{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200186 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100187 struct hlist_head *head;
188 struct hlist_node *node;
Marek Lindnerbae98772012-12-25 17:03:24 +0800189 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
190 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100191 int index;
192
193 if (!hash)
194 return NULL;
195
196 memcpy(search_entry.orig, addr, ETH_ALEN);
197 search_entry.vid = vid;
198
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200199 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100200 head = &hash->table[index];
201
202 rcu_read_lock();
203 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200204 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
205 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100206 continue;
207
208 if (!atomic_inc_not_zero(&backbone_gw->refcount))
209 continue;
210
211 backbone_gw_tmp = backbone_gw;
212 break;
213 }
214 rcu_read_unlock();
215
216 return backbone_gw_tmp;
217}
218
219/* delete all claims for a backbone */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200220static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800221batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100222{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200223 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100224 struct hlist_node *node, *node_tmp;
225 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800226 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100227 int i;
228 spinlock_t *list_lock; /* protects write access to the hash lists */
229
Sven Eckelmann807736f2012-07-15 22:26:51 +0200230 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100231 if (!hash)
232 return;
233
234 for (i = 0; i < hash->size; i++) {
235 head = &hash->table[i];
236 list_lock = &hash->list_locks[i];
237
238 spin_lock_bh(list_lock);
239 hlist_for_each_entry_safe(claim, node, node_tmp,
240 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100241 if (claim->backbone_gw != backbone_gw)
242 continue;
243
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200244 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100245 hlist_del_rcu(node);
246 }
247 spin_unlock_bh(list_lock);
248 }
249
250 /* all claims gone, intialize CRC */
Sven Eckelmann3964f722012-06-03 22:19:10 +0200251 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100252}
253
Ben Hutchings2c530402012-07-10 10:55:09 +0000254/**
255 * batadv_bla_send_claim - sends a claim frame according to the provided info
256 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100257 * @orig: the mac address to be announced within the claim
258 * @vid: the VLAN ID
259 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100260 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200261static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200262 short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100263{
264 struct sk_buff *skb;
265 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200266 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100267 struct net_device *soft_iface;
268 uint8_t *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200269 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100270 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100271
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200272 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100273 if (!primary_if)
274 return;
275
Sven Eckelmann807736f2012-07-15 22:26:51 +0200276 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100277 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100278 local_claim_dest.type = claimtype;
279
280 soft_iface = primary_if->soft_iface;
281
282 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
283 /* IP DST: 0.0.0.0 */
284 zeroip,
285 primary_if->soft_iface,
286 /* IP SRC: 0.0.0.0 */
287 zeroip,
288 /* Ethernet DST: Broadcast */
289 NULL,
290 /* Ethernet SRC/HW SRC: originator mac */
291 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200292 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100293 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100294 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100295 */
296 (uint8_t *)&local_claim_dest);
297
298 if (!skb)
299 goto out;
300
301 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100302 hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100303
304 /* now we pretend that the client would have sent this ... */
305 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200306 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100307 /* normal claim frame
308 * set Ethernet SRC to the clients mac
309 */
310 memcpy(ethhdr->h_source, mac, ETH_ALEN);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200311 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200312 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100313 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200314 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100315 /* unclaim frame
316 * set HW SRC to the clients mac
317 */
318 memcpy(hw_src, mac, ETH_ALEN);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200319 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200320 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
321 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100322 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200323 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100324 /* announcement frame
325 * set HW SRC to the special mac containg the crc
326 */
327 memcpy(hw_src, mac, ETH_ALEN);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200328 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200329 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
330 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100331 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200332 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100333 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200334 * set HW SRC and header destination to the receiving backbone
335 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100336 */
337 memcpy(hw_src, mac, ETH_ALEN);
338 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200339 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200340 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
341 ethhdr->h_source, ethhdr->h_dest, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100342 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100343 }
344
345 if (vid != -1)
346 skb = vlan_insert_tag(skb, vid);
347
348 skb_reset_mac_header(skb);
349 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200350 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
351 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
352 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100353 soft_iface->last_rx = jiffies;
354
355 netif_rx(skb);
356out:
357 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200358 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100359}
360
Ben Hutchings2c530402012-07-10 10:55:09 +0000361/**
362 * batadv_bla_get_backbone_gw
363 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100364 * @orig: the mac address of the originator
365 * @vid: the VLAN ID
366 *
367 * searches for the backbone gw or creates a new one if it could not
368 * be found.
369 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800370static struct batadv_bla_backbone_gw *
Sven Eckelmann56303d32012-06-05 22:31:31 +0200371batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200372 short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100373{
Marek Lindnerbae98772012-12-25 17:03:24 +0800374 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200375 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100376 int hash_added;
377
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200378 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100379
380 if (entry)
381 return entry;
382
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200383 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200384 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
385 orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100386
387 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
388 if (!entry)
389 return NULL;
390
391 entry->vid = vid;
392 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200393 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100394 entry->bat_priv = bat_priv;
395 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200396 atomic_set(&entry->wait_periods, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +0100397 memcpy(entry->orig, orig, ETH_ALEN);
398
399 /* one for the hash, one for returning */
400 atomic_set(&entry->refcount, 2);
401
Sven Eckelmann807736f2012-07-15 22:26:51 +0200402 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200403 batadv_compare_backbone_gw,
404 batadv_choose_backbone_gw, entry,
405 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100406
407 if (unlikely(hash_added != 0)) {
408 /* hash failed, free the structure */
409 kfree(entry);
410 return NULL;
411 }
412
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100413 /* this is a gateway now, remove any tt entries */
Sven Eckelmannda641192012-05-12 13:48:56 +0200414 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100415 if (orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200416 batadv_tt_global_del_orig(bat_priv, orig_node,
417 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200418 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100419 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200420
Simon Wunderlichd807f272012-09-09 22:27:57 +0200421 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200422 batadv_bla_send_announce(bat_priv, entry);
423
Simon Wunderlichd807f272012-09-09 22:27:57 +0200424 /* this will be decreased in the worker thread */
425 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200426 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200427 atomic_inc(&bat_priv->bla.num_requests);
428 }
429
Simon Wunderlich23721382012-01-22 20:00:19 +0100430 return entry;
431}
432
433/* update or add the own backbone gw to make sure we announce
434 * where we receive other backbone gws
435 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200436static void
437batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
438 struct batadv_hard_iface *primary_if,
439 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100440{
Marek Lindnerbae98772012-12-25 17:03:24 +0800441 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100442
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200443 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
444 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200445 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100446 if (unlikely(!backbone_gw))
447 return;
448
449 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200450 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100451}
452
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200453/* @bat_priv: the bat priv with all the soft interface information
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,
461 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100462{
463 struct hlist_node *node;
464 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200465 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800466 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800467 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100468 int i;
469
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200470 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200471 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100472
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200473 backbone_gw = batadv_backbone_hash_find(bat_priv,
474 primary_if->net_dev->dev_addr,
475 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100476 if (!backbone_gw)
477 return;
478
Sven Eckelmann807736f2012-07-15 22:26:51 +0200479 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100480 for (i = 0; i < hash->size; i++) {
481 head = &hash->table[i];
482
483 rcu_read_lock();
484 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
485 /* only own claims are interesting */
486 if (claim->backbone_gw != backbone_gw)
487 continue;
488
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200489 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200490 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100491 }
492 rcu_read_unlock();
493 }
494
495 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200496 batadv_bla_send_announce(bat_priv, backbone_gw);
497 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100498}
499
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200500/* @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100501 *
502 * When the crc is wrong, ask the backbone gateway for a full table update.
503 * After the request, it will repeat all of his own claims and finally
504 * send an announcement claim with which we can check again.
505 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800506static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100507{
508 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200509 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100510
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200511 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
512 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100513
514 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200515 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200516 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100517
518 /* no local broadcasts should be sent or received, for now. */
519 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200520 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100521 atomic_set(&backbone_gw->request_sent, 1);
522 }
523}
524
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200525/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100526 * @backbone_gw: our backbone gateway which should be announced
527 *
528 * This function sends an announcement. It is called from multiple
529 * places.
530 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200531static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800532 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100533{
534 uint8_t mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100535 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100536
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200537 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich23721382012-01-22 20:00:19 +0100538 crc = htons(backbone_gw->crc);
Al Viro1a5852d2012-04-22 07:50:29 +0100539 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100540
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200541 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200542 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100543}
544
Ben Hutchings2c530402012-07-10 10:55:09 +0000545/**
546 * batadv_bla_add_claim - Adds a claim in the claim hash
547 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100548 * @mac: the mac address of the claim
549 * @vid: the VLAN ID of the frame
550 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100551 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200552static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
553 const uint8_t *mac, const short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800554 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100555{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800556 struct batadv_bla_claim *claim;
557 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100558 int hash_added;
559
560 memcpy(search_claim.addr, mac, ETH_ALEN);
561 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200562 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100563
564 /* create a new claim entry if it does not exist yet. */
565 if (!claim) {
566 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
567 if (!claim)
568 return;
569
570 memcpy(claim->addr, mac, ETH_ALEN);
571 claim->vid = vid;
572 claim->lasttime = jiffies;
573 claim->backbone_gw = backbone_gw;
574
575 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200576 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200577 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
578 mac, vid);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200579 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200580 batadv_compare_claim,
581 batadv_choose_claim, claim,
582 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100583
584 if (unlikely(hash_added != 0)) {
585 /* only local changes happened. */
586 kfree(claim);
587 return;
588 }
589 } else {
590 claim->lasttime = jiffies;
591 if (claim->backbone_gw == backbone_gw)
592 /* no need to register a new backbone */
593 goto claim_free_ref;
594
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200595 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200596 "bla_add_claim(): changing ownership for %pM, vid %d\n",
597 mac, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100598
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200599 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200600 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100601 }
602 /* set (new) backbone gw */
603 atomic_inc(&backbone_gw->refcount);
604 claim->backbone_gw = backbone_gw;
605
606 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
607 backbone_gw->lasttime = jiffies;
608
609claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200610 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100611}
612
613/* Delete a claim from the claim hash which has the
614 * given mac address and vid.
615 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200616static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
617 const uint8_t *mac, const short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100618{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800619 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100620
621 memcpy(search_claim.addr, mac, ETH_ALEN);
622 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200623 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100624 if (!claim)
625 return;
626
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200627 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
628 mac, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100629
Sven Eckelmann807736f2012-07-15 22:26:51 +0200630 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200631 batadv_choose_claim, claim);
632 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100633
634 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
635
636 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200637 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100638}
639
640/* check for ANNOUNCE frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200641static int batadv_handle_announce(struct batadv_priv *bat_priv,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200642 uint8_t *an_addr, uint8_t *backbone_addr,
643 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100644{
Marek Lindnerbae98772012-12-25 17:03:24 +0800645 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100646 uint16_t crc;
647
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200648 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100649 return 0;
650
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200651 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
652 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100653
654 if (unlikely(!backbone_gw))
655 return 1;
656
657
658 /* handle as ANNOUNCE frame */
659 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100660 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100661
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200662 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100663 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200664 vid, backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100665
666 if (backbone_gw->crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200667 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100668 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200669 backbone_gw->orig, backbone_gw->vid,
670 backbone_gw->crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100671
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200672 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100673 } else {
674 /* if we have sent a request and the crc was OK,
675 * we can allow traffic again.
676 */
677 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200678 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100679 atomic_set(&backbone_gw->request_sent, 0);
680 }
681 }
682
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200683 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100684 return 1;
685}
686
687/* check for REQUEST frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200688static int batadv_handle_request(struct batadv_priv *bat_priv,
689 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200690 uint8_t *backbone_addr,
691 struct ethhdr *ethhdr, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100692{
693 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200694 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100695 return 0;
696
697 /* sanity check, this should not happen on a normal switch,
698 * we ignore it in this case.
699 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200700 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100701 return 1;
702
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200703 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200704 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
705 vid, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100706
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200707 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100708 return 1;
709}
710
711/* check for UNCLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200712static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
713 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200714 uint8_t *backbone_addr,
715 uint8_t *claim_addr, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100716{
Marek Lindnerbae98772012-12-25 17:03:24 +0800717 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100718
719 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200720 if (primary_if && batadv_compare_eth(backbone_addr,
721 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200722 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200723 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100724
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200725 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100726
727 if (!backbone_gw)
728 return 1;
729
730 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200731 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200732 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
733 claim_addr, vid, backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100734
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200735 batadv_bla_del_claim(bat_priv, claim_addr, vid);
736 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100737 return 1;
738}
739
740/* check for CLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200741static int batadv_handle_claim(struct batadv_priv *bat_priv,
742 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200743 uint8_t *backbone_addr, uint8_t *claim_addr,
744 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100745{
Marek Lindnerbae98772012-12-25 17:03:24 +0800746 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100747
748 /* register the gateway if not yet available, and add the claim. */
749
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200750 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
751 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100752
753 if (unlikely(!backbone_gw))
754 return 1;
755
756 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200757 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200758 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200759 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200760 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100761
762 /* TODO: we could call something like tt_local_del() here. */
763
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200764 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100765 return 1;
766}
767
Ben Hutchings2c530402012-07-10 10:55:09 +0000768/**
769 * batadv_check_claim_group
770 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100771 * @hw_src: the Hardware source in the ARP Header
772 * @hw_dst: the Hardware destination in the ARP Header
773 * @ethhdr: pointer to the Ethernet header of the claim frame
774 *
775 * checks if it is a claim packet and if its on the same group.
776 * This function also applies the group ID of the sender
777 * if it is in the same mesh.
778 *
779 * returns:
780 * 2 - if it is a claim packet and on the same group
781 * 1 - if is a claim packet from another group
782 * 0 - if it is not a claim packet
783 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200784static int batadv_check_claim_group(struct batadv_priv *bat_priv,
785 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200786 uint8_t *hw_src, uint8_t *hw_dst,
787 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100788{
789 uint8_t *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200790 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200791 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100792
Sven Eckelmann96412692012-06-05 22:31:30 +0200793 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200794 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100795
796 /* check if it is a claim packet in general */
797 if (memcmp(bla_dst->magic, bla_dst_own->magic,
798 sizeof(bla_dst->magic)) != 0)
799 return 0;
800
801 /* if announcement packet, use the source,
802 * otherwise assume it is in the hw_src
803 */
804 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200805 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100806 backbone_addr = hw_src;
807 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200808 case BATADV_CLAIM_TYPE_REQUEST:
809 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200810 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100811 backbone_addr = ethhdr->h_source;
812 break;
813 default:
814 return 0;
815 }
816
817 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200818 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100819 return 0;
820
821 /* if its already the same group, it is fine. */
822 if (bla_dst->group == bla_dst_own->group)
823 return 2;
824
825 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200826 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100827
828 /* dont accept claims from gateways which are not in
829 * the same mesh or group.
830 */
831 if (!orig_node)
832 return 1;
833
834 /* if our mesh friends mac is bigger, use it for ourselves. */
835 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200836 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100837 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200838 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100839 bla_dst_own->group = bla_dst->group;
840 }
841
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200842 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100843
844 return 2;
845}
846
847
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200848/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100849 * @skb: the frame to be checked
850 *
851 * Check if this is a claim frame, and process it accordingly.
852 *
853 * returns 1 if it was a claim frame, otherwise return 0 to
854 * tell the callee that it can use the frame on its own.
855 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200856static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
857 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200858 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100859{
860 struct ethhdr *ethhdr;
861 struct vlan_ethhdr *vhdr;
862 struct arphdr *arphdr;
863 uint8_t *hw_src, *hw_dst;
Sven Eckelmann96412692012-06-05 22:31:30 +0200864 struct batadv_bla_claim_dst *bla_dst;
Simon Wunderlich23721382012-01-22 20:00:19 +0100865 uint16_t proto;
866 int headlen;
867 short vid = -1;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100868 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100869
870 ethhdr = (struct ethhdr *)skb_mac_header(skb);
871
872 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
873 vhdr = (struct vlan_ethhdr *)ethhdr;
874 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
875 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
876 headlen = sizeof(*vhdr);
877 } else {
878 proto = ntohs(ethhdr->h_proto);
Antonio Quartulli0d125072012-02-18 11:27:34 +0100879 headlen = ETH_HLEN;
Simon Wunderlich23721382012-01-22 20:00:19 +0100880 }
881
882 if (proto != ETH_P_ARP)
883 return 0; /* not a claim frame */
884
885 /* this must be a ARP frame. check if it is a claim. */
886
887 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
888 return 0;
889
890 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
891 ethhdr = (struct ethhdr *)skb_mac_header(skb);
892 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
893
894 /* Check whether the ARP frame carries a valid
895 * IP information
896 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100897 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
898 return 0;
899 if (arphdr->ar_pro != htons(ETH_P_IP))
900 return 0;
901 if (arphdr->ar_hln != ETH_ALEN)
902 return 0;
903 if (arphdr->ar_pln != 4)
904 return 0;
905
906 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
907 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +0200908 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlich23721382012-01-22 20:00:19 +0100909
910 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200911 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
912 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100913 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200914 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200915 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
916 ethhdr->h_source, vid, hw_src, hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100917
918 if (ret < 2)
919 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100920
921 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200922 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100923
924 /* check for the different types of claim frames ... */
925 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200926 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200927 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
928 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100929 return 1;
930 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200931 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200932 if (batadv_handle_unclaim(bat_priv, primary_if,
933 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100934 return 1;
935 break;
936
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200937 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200938 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
939 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100940 return 1;
941 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200942 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200943 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
944 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100945 return 1;
946 break;
947 }
948
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200949 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200950 "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",
951 ethhdr->h_source, vid, hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +0100952 return 1;
953}
954
955/* Check when we last heard from other nodes, and remove them in case of
956 * a time out, or clean all backbone gws if now is set.
957 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200958static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +0100959{
Marek Lindnerbae98772012-12-25 17:03:24 +0800960 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100961 struct hlist_node *node, *node_tmp;
962 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200963 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100964 spinlock_t *list_lock; /* protects write access to the hash lists */
965 int i;
966
Sven Eckelmann807736f2012-07-15 22:26:51 +0200967 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100968 if (!hash)
969 return;
970
971 for (i = 0; i < hash->size; i++) {
972 head = &hash->table[i];
973 list_lock = &hash->list_locks[i];
974
975 spin_lock_bh(list_lock);
976 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
977 head, hash_entry) {
978 if (now)
979 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200980 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200981 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +0100982 continue;
983
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200984 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200985 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
986 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100987
988purge_now:
989 /* don't wait for the pending request anymore */
990 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +0200991 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100992
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200993 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100994
995 hlist_del_rcu(node);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200996 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100997 }
998 spin_unlock_bh(list_lock);
999 }
1000}
1001
Ben Hutchings2c530402012-07-10 10:55:09 +00001002/**
1003 * batadv_bla_purge_claims
1004 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001005 * @primary_if: the selected primary interface, may be NULL if now is set
1006 * @now: whether the whole hash shall be wiped now
1007 *
1008 * Check when we heard last time from our own claims, and remove them in case of
1009 * a time out, or clean all claims if now is set
1010 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001011static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1012 struct batadv_hard_iface *primary_if,
1013 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001014{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001015 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001016 struct hlist_node *node;
1017 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001018 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001019 int i;
1020
Sven Eckelmann807736f2012-07-15 22:26:51 +02001021 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001022 if (!hash)
1023 return;
1024
1025 for (i = 0; i < hash->size; i++) {
1026 head = &hash->table[i];
1027
1028 rcu_read_lock();
1029 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1030 if (now)
1031 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001032 if (!batadv_compare_eth(claim->backbone_gw->orig,
1033 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001034 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001035 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001036 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001037 continue;
1038
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001039 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001040 "bla_purge_claims(): %pM, vid %d, time out\n",
1041 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001042
1043purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001044 batadv_handle_unclaim(bat_priv, primary_if,
1045 claim->backbone_gw->orig,
1046 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001047 }
1048 rcu_read_unlock();
1049 }
1050}
1051
Ben Hutchings2c530402012-07-10 10:55:09 +00001052/**
1053 * batadv_bla_update_orig_address
1054 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001055 * @primary_if: the new selected primary_if
1056 * @oldif: the old primary interface, may be NULL
1057 *
1058 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001059 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001060void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1061 struct batadv_hard_iface *primary_if,
1062 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001063{
Marek Lindnerbae98772012-12-25 17:03:24 +08001064 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001065 struct hlist_node *node;
1066 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001067 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001068 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001069 int i;
1070
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001071 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001072 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1073 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001074
Simon Wunderlich23721382012-01-22 20:00:19 +01001075 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001076 batadv_bla_purge_claims(bat_priv, NULL, 1);
1077 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001078 return;
1079 }
1080
Sven Eckelmann807736f2012-07-15 22:26:51 +02001081 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001082 if (!hash)
1083 return;
1084
1085 for (i = 0; i < hash->size; i++) {
1086 head = &hash->table[i];
1087
1088 rcu_read_lock();
1089 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1090 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001091 if (!batadv_compare_eth(backbone_gw->orig,
1092 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001093 continue;
1094
1095 memcpy(backbone_gw->orig,
1096 primary_if->net_dev->dev_addr, ETH_ALEN);
1097 /* send an announce frame so others will ask for our
1098 * claims and update their tables.
1099 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001100 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001101 }
1102 rcu_read_unlock();
1103 }
1104}
1105
Simon Wunderlich23721382012-01-22 20:00:19 +01001106/* periodic work to do:
1107 * * purge structures when they are too old
1108 * * send announcements
1109 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001110static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001111{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001112 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001113 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001114 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001115 struct hlist_node *node;
1116 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001117 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001118 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001119 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001120 int i;
1121
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001122 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001123 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1124 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001125 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001126 if (!primary_if)
1127 goto out;
1128
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001129 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1130 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001131
1132 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1133 goto out;
1134
Sven Eckelmann807736f2012-07-15 22:26:51 +02001135 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001136 if (!hash)
1137 goto out;
1138
1139 for (i = 0; i < hash->size; i++) {
1140 head = &hash->table[i];
1141
1142 rcu_read_lock();
1143 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001144 if (!batadv_compare_eth(backbone_gw->orig,
1145 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001146 continue;
1147
1148 backbone_gw->lasttime = jiffies;
1149
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001150 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001151
1152 /* request_sent is only set after creation to avoid
1153 * problems when we are not yet known as backbone gw
1154 * in the backbone.
1155 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001156 * We can reset this now after we waited some periods
1157 * to give bridge forward delays and bla group forming
1158 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001159 */
1160
1161 if (atomic_read(&backbone_gw->request_sent) == 0)
1162 continue;
1163
Simon Wunderlich28709872012-09-13 18:18:46 +02001164 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1165 continue;
1166
Simon Wunderlichd807f272012-09-09 22:27:57 +02001167 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1168 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001169 }
1170 rcu_read_unlock();
1171 }
1172out:
1173 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001174 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001175
Antonio Quartulli72414442012-12-25 13:14:37 +01001176 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1177 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001178}
1179
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001180/* The hash for claim and backbone hash receive the same key because they
1181 * are getting initialized by hash_new with the same key. Reinitializing
1182 * them with to different keys to allow nested locking without generating
1183 * lockdep warnings
1184 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001185static struct lock_class_key batadv_claim_hash_lock_class_key;
1186static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001187
Simon Wunderlich23721382012-01-22 20:00:19 +01001188/* initialize all bla structures */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001189int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001190{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001191 int i;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001192 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001193 struct batadv_hard_iface *primary_if;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001194 uint16_t crc;
1195 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001196
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001197 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1198
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001199 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001200
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001201 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001202 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1203 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001204 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001205 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001206 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1207 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001208 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001209 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001210 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001211 }
1212
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001213 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001214 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001215 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001216 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1217 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001218
Sven Eckelmann807736f2012-07-15 22:26:51 +02001219 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001220 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001221
Sven Eckelmann807736f2012-07-15 22:26:51 +02001222 bat_priv->bla.claim_hash = batadv_hash_new(128);
1223 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001224
Sven Eckelmann807736f2012-07-15 22:26:51 +02001225 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001226 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001227
Sven Eckelmann807736f2012-07-15 22:26:51 +02001228 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001229 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001230 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001231 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001232
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001233 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001234
Antonio Quartulli72414442012-12-25 13:14:37 +01001235 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1236
1237 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1238 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001239 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001240}
1241
Ben Hutchings2c530402012-07-10 10:55:09 +00001242/**
1243 * batadv_bla_check_bcast_duplist
1244 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001245 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001246 *
1247 * check if it is on our broadcast list. Another gateway might
1248 * have sent the same packet because it is connected to the same backbone,
1249 * so we have to remove this duplicate.
1250 *
1251 * This is performed by checking the CRC, which will tell us
1252 * with a good chance that it is the same packet. If it is furthermore
1253 * sent by another host, drop it. We allow equal packets from
1254 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001255 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001256int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001257 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001258{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001259 int i, curr, ret = 0;
1260 __be32 crc;
1261 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001262 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001263
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001264 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001265
1266 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001267 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001268
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001269 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1270
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001271 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001272 curr = (bat_priv->bla.bcast_duplist_curr + i);
1273 curr %= BATADV_DUPLIST_SIZE;
1274 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001275
1276 /* we can stop searching if the entry is too old ;
1277 * later entries will be even older
1278 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001279 if (batadv_has_timed_out(entry->entrytime,
1280 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001281 break;
1282
1283 if (entry->crc != crc)
1284 continue;
1285
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001286 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001287 continue;
1288
1289 /* this entry seems to match: same crc, not too old,
1290 * and from another gw. therefore return 1 to forbid it.
1291 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001292 ret = 1;
1293 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001294 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001295 /* not found, add a new entry (overwrite the oldest entry)
1296 * and allow it, its the first occurence.
1297 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001298 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001299 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001300 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001301 entry->crc = crc;
1302 entry->entrytime = jiffies;
1303 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001304 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001305
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001306out:
1307 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1308
1309 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001310}
1311
1312
1313
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001314/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001315 * @orig: originator mac address
1316 *
1317 * check if the originator is a gateway for any VLAN ID.
1318 *
1319 * returns 1 if it is found, 0 otherwise
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001320 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001322{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001323 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001324 struct hlist_head *head;
1325 struct hlist_node *node;
Marek Lindnerbae98772012-12-25 17:03:24 +08001326 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001327 int i;
1328
1329 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1330 return 0;
1331
1332 if (!hash)
1333 return 0;
1334
1335 for (i = 0; i < hash->size; i++) {
1336 head = &hash->table[i];
1337
1338 rcu_read_lock();
1339 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001340 if (batadv_compare_eth(backbone_gw->orig, orig)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001341 rcu_read_unlock();
1342 return 1;
1343 }
1344 }
1345 rcu_read_unlock();
1346 }
1347
1348 return 0;
1349}
1350
1351
Ben Hutchings2c530402012-07-10 10:55:09 +00001352/**
1353 * batadv_bla_is_backbone_gw
1354 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001355 * @orig_node: the orig_node of the frame
1356 * @hdr_size: maximum length of the frame
1357 *
1358 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1359 * if the orig_node is also a gateway on the soft interface, otherwise it
1360 * returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001361 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001362int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001363 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001364{
1365 struct ethhdr *ethhdr;
1366 struct vlan_ethhdr *vhdr;
Marek Lindnerbae98772012-12-25 17:03:24 +08001367 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001368 short vid = -1;
1369
1370 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1371 return 0;
1372
1373 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001374 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001375 return 0;
1376
1377 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1378
1379 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1380 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1381 return 0;
1382
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001383 vhdr = (struct vlan_ethhdr *)(skb->data + hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001384 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1385 }
1386
1387 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001388 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1389 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001390 if (!backbone_gw)
1391 return 0;
1392
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001393 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001394 return 1;
1395}
1396
1397/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001398void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001399{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001400 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001401
Sven Eckelmann807736f2012-07-15 22:26:51 +02001402 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001403 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001404
Sven Eckelmann807736f2012-07-15 22:26:51 +02001405 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001406 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001407 batadv_hash_destroy(bat_priv->bla.claim_hash);
1408 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001409 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001410 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001411 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001412 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1413 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001414 }
1415 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001416 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001417}
1418
Ben Hutchings2c530402012-07-10 10:55:09 +00001419/**
1420 * batadv_bla_rx
1421 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001422 * @skb: the frame to be checked
1423 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001424 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001425 *
1426 * bla_rx avoidance checks if:
1427 * * we have to race for a claim
1428 * * if the frame is allowed on the LAN
1429 *
1430 * in these cases, the skb is further handled by this function and
1431 * returns 1, otherwise it returns 0 and the caller shall further
1432 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001433 */
David S. Miller04c9f412012-07-10 23:56:33 -07001434int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid,
1435 bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001436{
1437 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001438 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001439 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001440 int ret;
1441
1442 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1443
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001444 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001445 if (!primary_if)
1446 goto handled;
1447
1448 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1449 goto allow;
1450
1451
Sven Eckelmann807736f2012-07-15 22:26:51 +02001452 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001453 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001454 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001455 goto handled;
1456
1457 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1458 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001459 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001460
1461 if (!claim) {
1462 /* possible optimization: race for a claim */
1463 /* No claim exists yet, claim it for us!
1464 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001465 batadv_handle_claim(bat_priv, primary_if,
1466 primary_if->net_dev->dev_addr,
1467 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001468 goto allow;
1469 }
1470
1471 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001472 if (batadv_compare_eth(claim->backbone_gw->orig,
1473 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001474 /* ... allow it in any case */
1475 claim->lasttime = jiffies;
1476 goto allow;
1477 }
1478
1479 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001480 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1481 /* ... drop it. the responsible gateway is in charge.
1482 *
1483 * We need to check is_bcast because with the gateway
1484 * feature, broadcasts (like DHCP requests) may be sent
1485 * using a unicast packet type.
1486 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001487 goto handled;
1488 } else {
1489 /* seems the client considers us as its best gateway.
1490 * send a claim and update the claim table
1491 * immediately.
1492 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001493 batadv_handle_claim(bat_priv, primary_if,
1494 primary_if->net_dev->dev_addr,
1495 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001496 goto allow;
1497 }
1498allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001499 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001500 ret = 0;
1501 goto out;
1502
1503handled:
1504 kfree_skb(skb);
1505 ret = 1;
1506
1507out:
1508 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001509 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001510 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001511 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001512 return ret;
1513}
1514
Ben Hutchings2c530402012-07-10 10:55:09 +00001515/**
1516 * batadv_bla_tx
1517 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001518 * @skb: the frame to be checked
1519 * @vid: the VLAN ID of the frame
1520 *
1521 * bla_tx checks if:
1522 * * a claim was received which has to be processed
1523 * * the frame is allowed on the mesh
1524 *
1525 * in these cases, the skb is further handled by this function and
1526 * returns 1, otherwise it returns 0 and the caller shall further
1527 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001528 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001529int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001530{
1531 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001532 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001533 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001534 int ret = 0;
1535
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001536 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001537 if (!primary_if)
1538 goto out;
1539
1540 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1541 goto allow;
1542
1543 /* in VLAN case, the mac header might not be set. */
1544 skb_reset_mac_header(skb);
1545
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001546 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001547 goto handled;
1548
1549 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1550
Sven Eckelmann807736f2012-07-15 22:26:51 +02001551 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001552 /* don't allow broadcasts while requests are in flight */
1553 if (is_multicast_ether_addr(ethhdr->h_dest))
1554 goto handled;
1555
1556 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1557 search_claim.vid = vid;
1558
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001559 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001560
1561 /* if no claim exists, allow it. */
1562 if (!claim)
1563 goto allow;
1564
1565 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001566 if (batadv_compare_eth(claim->backbone_gw->orig,
1567 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001568 /* if yes, the client has roamed and we have
1569 * to unclaim it.
1570 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001571 batadv_handle_unclaim(bat_priv, primary_if,
1572 primary_if->net_dev->dev_addr,
1573 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001574 goto allow;
1575 }
1576
1577 /* check if it is a multicast/broadcast frame */
1578 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1579 /* drop it. the responsible gateway has forwarded it into
1580 * the backbone network.
1581 */
1582 goto handled;
1583 } else {
1584 /* we must allow it. at least if we are
1585 * responsible for the DESTINATION.
1586 */
1587 goto allow;
1588 }
1589allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001590 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001591 ret = 0;
1592 goto out;
1593handled:
1594 ret = 1;
1595out:
1596 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001597 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001598 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001599 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001600 return ret;
1601}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001602
Sven Eckelmann08adf152012-05-12 13:38:47 +02001603int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001604{
1605 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001606 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001607 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001608 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001609 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001610 struct hlist_node *node;
1611 struct hlist_head *head;
1612 uint32_t i;
1613 bool is_own;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001614 uint8_t *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001615
Marek Lindner30da63a2012-08-03 17:15:46 +02001616 primary_if = batadv_seq_print_text_primary_if_get(seq);
1617 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001618 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001619
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001620 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001621 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001622 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001623 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001624 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001625 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001626 "Client", "VID", "Originator", "CRC");
1627 for (i = 0; i < hash->size; i++) {
1628 head = &hash->table[i];
1629
1630 rcu_read_lock();
1631 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001632 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1633 primary_addr);
Antonio Quartulli39a32992012-11-19 09:01:43 +01001634 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%#.4x)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001635 claim->addr, claim->vid,
1636 claim->backbone_gw->orig,
1637 (is_own ? 'x' : ' '),
1638 claim->backbone_gw->crc);
1639 }
1640 rcu_read_unlock();
1641 }
1642out:
1643 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001644 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001645 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001646}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001647
1648int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1649{
1650 struct net_device *net_dev = (struct net_device *)seq->private;
1651 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001652 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001653 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001654 struct batadv_hard_iface *primary_if;
1655 struct hlist_node *node;
1656 struct hlist_head *head;
1657 int secs, msecs;
1658 uint32_t i;
1659 bool is_own;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001660 uint8_t *primary_addr;
1661
Marek Lindner30da63a2012-08-03 17:15:46 +02001662 primary_if = batadv_seq_print_text_primary_if_get(seq);
1663 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001664 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001665
1666 primary_addr = primary_if->net_dev->dev_addr;
1667 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001668 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001669 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001670 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001671 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001672 "Originator", "VID", "last seen", "CRC");
1673 for (i = 0; i < hash->size; i++) {
1674 head = &hash->table[i];
1675
1676 rcu_read_lock();
1677 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1678 msecs = jiffies_to_msecs(jiffies -
1679 backbone_gw->lasttime);
1680 secs = msecs / 1000;
1681 msecs = msecs % 1000;
1682
1683 is_own = batadv_compare_eth(backbone_gw->orig,
1684 primary_addr);
1685 if (is_own)
1686 continue;
1687
1688 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001689 " * %pM on % 5d % 4i.%03is (%#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001690 backbone_gw->orig, backbone_gw->vid,
1691 secs, msecs, backbone_gw->crc);
1692 }
1693 rcu_read_unlock();
1694 }
1695out:
1696 if (primary_if)
1697 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001698 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001699}