blob: 131aca049907c16248aaf70bf5ce9e79cee097e3 [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2011-2015 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
Simon Wunderlich23721382012-01-22 20:00:19 +010018#include "bridge_loop_avoidance.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010020
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/atomic.h>
22#include <linux/byteorder/generic.h>
23#include <linux/compiler.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010024#include <linux/crc16.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/errno.h>
26#include <linux/etherdevice.h>
27#include <linux/fs.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010028#include <linux/if_arp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/if_ether.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include <linux/if_vlan.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/lockdep.h>
36#include <linux/netdevice.h>
37#include <linux/rculist.h>
38#include <linux/rcupdate.h>
39#include <linux/seq_file.h>
40#include <linux/skbuff.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/stddef.h>
44#include <linux/string.h>
45#include <linux/workqueue.h>
46#include <net/arp.h>
47
48#include "hard-interface.h"
49#include "hash.h"
50#include "originator.h"
51#include "packet.h"
52#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010053
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020054static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010055
Sven Eckelmann3b300de2012-05-12 18:33:53 +020056static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080057static void
58batadv_bla_send_announce(struct batadv_priv *bat_priv,
59 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010060
Sven Eckelmann62fe7102015-09-15 19:00:48 +020061/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010062 * batadv_choose_claim - choose the right bucket for a claim.
63 * @data: data to hash
64 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020065 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010066 * Return: the hash index of the claim
Sven Eckelmann62fe7102015-09-15 19:00:48 +020067 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020068static inline u32 batadv_choose_claim(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010069{
Marek Lindner712bbfe42012-12-25 17:03:25 +080070 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020071 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010072
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010073 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
74 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010075
76 return hash % size;
77}
78
Sven Eckelmann62fe7102015-09-15 19:00:48 +020079/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010080 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
81 * @data: data to hash
82 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020083 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010084 * Return: the hash index of the backbone gateway
Sven Eckelmann62fe7102015-09-15 19:00:48 +020085 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020086static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010087{
Marek Lindner712bbfe42012-12-25 17:03:25 +080088 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020089 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010090
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010091 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
92 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010093
94 return hash % size;
95}
96
Simon Wunderlich04e14be2015-11-06 10:45:19 +010097/**
98 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
99 * @node: list node of the first entry to compare
100 * @data2: pointer to the second backbone gateway
101 *
102 * Return: 1 if the backbones have the same data, 0 otherwise
103 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200104static int batadv_compare_backbone_gw(const struct hlist_node *node,
105 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100106{
Marek Lindnerbae98772012-12-25 17:03:24 +0800107 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +0100108 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200109 const struct batadv_bla_backbone_gw *gw1 = data1;
110 const struct batadv_bla_backbone_gw *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100111
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200112 if (!batadv_compare_eth(gw1->orig, gw2->orig))
113 return 0;
114
115 if (gw1->vid != gw2->vid)
116 return 0;
117
118 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100119}
120
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100121/**
122 * batadv_compare_backbone_gw - compare address and vid of two claims
123 * @node: list node of the first entry to compare
124 * @data2: pointer to the second claims
125 *
126 * Return: 1 if the claim have the same data, 0 otherwise
127 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200128static int batadv_compare_claim(const struct hlist_node *node,
129 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100130{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800131 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +0100132 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200133 const struct batadv_bla_claim *cl1 = data1;
134 const struct batadv_bla_claim *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100135
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200136 if (!batadv_compare_eth(cl1->addr, cl2->addr))
137 return 0;
138
139 if (cl1->vid != cl2->vid)
140 return 0;
141
142 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100143}
144
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100145/**
146 * batadv_compare_backbone_gw - free backbone gw
147 * @backbone_gw: backbone gateway to be free'd
148 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800149static void
150batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100151{
152 if (atomic_dec_and_test(&backbone_gw->refcount))
153 kfree_rcu(backbone_gw, rcu);
154}
155
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100156/**
157 * batadv_claim_release - release claim from lists and queue for free after rcu
158 * grace period
159 * @ref: kref pointer of the claim
160 */
Sven Eckelmann63b39922016-01-14 15:28:19 +0100161static void batadv_claim_release(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100162{
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200163 batadv_backbone_gw_free_ref(claim->backbone_gw);
Sven Eckelmann63b39922016-01-14 15:28:19 +0100164 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100165}
166
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100167/**
168 * batadv_claim_free_rcu - free a claim
169 * @claim: claim to be free'd
170 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800171static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100172{
173 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann63b39922016-01-14 15:28:19 +0100174 batadv_claim_release(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100175}
176
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100177/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100178 * batadv_claim_hash_find - looks for a claim in the claim hash
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100179 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100180 * @data: search data (may be local/static data)
181 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200182 * Return: claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100183 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800184static struct batadv_bla_claim
185*batadv_claim_hash_find(struct batadv_priv *bat_priv,
186 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100187{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200188 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100189 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800190 struct batadv_bla_claim *claim;
191 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100192 int index;
193
194 if (!hash)
195 return NULL;
196
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200197 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100198 head = &hash->table[index];
199
200 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800201 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200202 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100203 continue;
204
205 if (!atomic_inc_not_zero(&claim->refcount))
206 continue;
207
208 claim_tmp = claim;
209 break;
210 }
211 rcu_read_unlock();
212
213 return claim_tmp;
214}
215
Ben Hutchings2c530402012-07-10 10:55:09 +0000216/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100217 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
Ben Hutchings2c530402012-07-10 10:55:09 +0000218 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100219 * @addr: the address of the originator
220 * @vid: the VLAN ID
221 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100222 * Return: backbone gateway if found or NULL otherwise
Simon Wunderlich23721382012-01-22 20:00:19 +0100223 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800224static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200225batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
226 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100227{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200228 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100229 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800230 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
231 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100232 int index;
233
234 if (!hash)
235 return NULL;
236
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100237 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100238 search_entry.vid = vid;
239
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200240 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100241 head = &hash->table[index];
242
243 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800244 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200245 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
246 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100247 continue;
248
249 if (!atomic_inc_not_zero(&backbone_gw->refcount))
250 continue;
251
252 backbone_gw_tmp = backbone_gw;
253 break;
254 }
255 rcu_read_unlock();
256
257 return backbone_gw_tmp;
258}
259
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100260/**
261 * batadv_bla_del_backbone_claims - delete all claims for a backbone
262 * @backbone_gw: backbone gateway where the claims should be removed
263 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200264static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800265batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100266{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200267 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800268 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100269 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800270 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100271 int i;
272 spinlock_t *list_lock; /* protects write access to the hash lists */
273
Sven Eckelmann807736f2012-07-15 22:26:51 +0200274 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100275 if (!hash)
276 return;
277
278 for (i = 0; i < hash->size; i++) {
279 head = &hash->table[i];
280 list_lock = &hash->list_locks[i];
281
282 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800283 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100284 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100285 if (claim->backbone_gw != backbone_gw)
286 continue;
287
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200288 batadv_claim_free_ref(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800289 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100290 }
291 spin_unlock_bh(list_lock);
292 }
293
Antonio Quartulli3f687852014-11-02 11:29:56 +0100294 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200295 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200296 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200297 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100298}
299
Ben Hutchings2c530402012-07-10 10:55:09 +0000300/**
301 * batadv_bla_send_claim - sends a claim frame according to the provided info
302 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200303 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100304 * @vid: the VLAN ID
305 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100306 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200307static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200308 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100309{
310 struct sk_buff *skb;
311 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200312 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100313 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200314 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200315 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100316 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100317
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200318 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100319 if (!primary_if)
320 return;
321
Sven Eckelmann807736f2012-07-15 22:26:51 +0200322 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100323 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100324 local_claim_dest.type = claimtype;
325
326 soft_iface = primary_if->soft_iface;
327
328 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
329 /* IP DST: 0.0.0.0 */
330 zeroip,
331 primary_if->soft_iface,
332 /* IP SRC: 0.0.0.0 */
333 zeroip,
334 /* Ethernet DST: Broadcast */
335 NULL,
336 /* Ethernet SRC/HW SRC: originator mac */
337 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200338 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100339 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100340 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100341 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200342 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100343
344 if (!skb)
345 goto out;
346
347 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200348 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100349
350 /* now we pretend that the client would have sent this ... */
351 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200352 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100353 /* normal claim frame
354 * set Ethernet SRC to the clients mac
355 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100356 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200357 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200358 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
359 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100360 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200361 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100362 /* unclaim frame
363 * set HW SRC to the clients mac
364 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100365 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200366 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200367 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200368 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100369 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200370 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100371 /* announcement frame
372 * set HW SRC to the special mac containg the crc
373 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100374 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200375 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200376 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200377 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100378 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200379 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100380 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200381 * set HW SRC and header destination to the receiving backbone
382 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100383 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100384 ether_addr_copy(hw_src, mac);
385 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200386 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200387 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200388 ethhdr->h_source, ethhdr->h_dest,
389 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100390 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100391 }
392
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200393 if (vid & BATADV_VLAN_HAS_TAG)
394 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
395 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100396
397 skb_reset_mac_header(skb);
398 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200399 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
400 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
401 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100402 soft_iface->last_rx = jiffies;
403
404 netif_rx(skb);
405out:
406 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200407 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100408}
409
Ben Hutchings2c530402012-07-10 10:55:09 +0000410/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100411 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
Ben Hutchings2c530402012-07-10 10:55:09 +0000412 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100413 * @orig: the mac address of the originator
414 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200415 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100416 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100417 * Return: the (possibly created) backbone gateway or NULL on error
Simon Wunderlich23721382012-01-22 20:00:19 +0100418 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800419static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200420batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200421 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100422{
Marek Lindnerbae98772012-12-25 17:03:24 +0800423 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200424 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100425 int hash_added;
426
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200427 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100428
429 if (entry)
430 return entry;
431
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200432 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200433 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200434 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100435
436 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
437 if (!entry)
438 return NULL;
439
440 entry->vid = vid;
441 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200442 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100443 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200444 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100445 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200446 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100447 ether_addr_copy(entry->orig, orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100448
449 /* one for the hash, one for returning */
450 atomic_set(&entry->refcount, 2);
451
Sven Eckelmann807736f2012-07-15 22:26:51 +0200452 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200453 batadv_compare_backbone_gw,
454 batadv_choose_backbone_gw, entry,
455 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100456
457 if (unlikely(hash_added != 0)) {
458 /* hash failed, free the structure */
459 kfree(entry);
460 return NULL;
461 }
462
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200463 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200464 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100465 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200466 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200467 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200468 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100469 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200470
Simon Wunderlichd807f272012-09-09 22:27:57 +0200471 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200472 batadv_bla_send_announce(bat_priv, entry);
473
Simon Wunderlichd807f272012-09-09 22:27:57 +0200474 /* this will be decreased in the worker thread */
475 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200476 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200477 atomic_inc(&bat_priv->bla.num_requests);
478 }
479
Simon Wunderlich23721382012-01-22 20:00:19 +0100480 return entry;
481}
482
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100483/**
484 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
485 * @bat_priv: the bat priv with all the soft interface information
486 * @primary_if: the selected primary interface
487 * @vid: VLAN identifier
488 *
489 * update or add the own backbone gw to make sure we announce
Simon Wunderlich23721382012-01-22 20:00:19 +0100490 * where we receive other backbone gws
491 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200492static void
493batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
494 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200495 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100496{
Marek Lindnerbae98772012-12-25 17:03:24 +0800497 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100498
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200499 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
500 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200501 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100502 if (unlikely(!backbone_gw))
503 return;
504
505 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200506 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100507}
508
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100509/**
510 * batadv_bla_answer_request - answer a bla request by sending own claims
511 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200512 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100513 * @vid: the vid where the request came on
514 *
515 * Repeat all of our own claims, and finally send an ANNOUNCE frame
516 * to allow the requester another check if the CRC is correct now.
517 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200518static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
519 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200520 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100521{
Simon Wunderlich23721382012-01-22 20:00:19 +0100522 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200523 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800524 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800525 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100526 int i;
527
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200528 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200529 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100530
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200531 backbone_gw = batadv_backbone_hash_find(bat_priv,
532 primary_if->net_dev->dev_addr,
533 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100534 if (!backbone_gw)
535 return;
536
Sven Eckelmann807736f2012-07-15 22:26:51 +0200537 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100538 for (i = 0; i < hash->size; i++) {
539 head = &hash->table[i];
540
541 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800542 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100543 /* only own claims are interesting */
544 if (claim->backbone_gw != backbone_gw)
545 continue;
546
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200547 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200548 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100549 }
550 rcu_read_unlock();
551 }
552
553 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200554 batadv_bla_send_announce(bat_priv, backbone_gw);
555 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100556}
557
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100558/**
559 * batadv_bla_send_request - send a request to repeat claims
560 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100561 *
562 * When the crc is wrong, ask the backbone gateway for a full table update.
563 * After the request, it will repeat all of his own claims and finally
564 * send an announcement claim with which we can check again.
565 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800566static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100567{
568 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200569 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100570
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200571 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
572 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100573
574 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200575 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200576 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100577
578 /* no local broadcasts should be sent or received, for now. */
579 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200580 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100581 atomic_set(&backbone_gw->request_sent, 1);
582 }
583}
584
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100585/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100586 * batadv_bla_send_announce - Send an announcement frame
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100587 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100588 * @backbone_gw: our backbone gateway which should be announced
Simon Wunderlich23721382012-01-22 20:00:19 +0100589 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200590static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800591 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100592{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200593 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100594 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100595
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200596 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200597 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100598 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200599 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100600 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100601
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200602 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200603 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100604}
605
Ben Hutchings2c530402012-07-10 10:55:09 +0000606/**
607 * batadv_bla_add_claim - Adds a claim in the claim hash
608 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100609 * @mac: the mac address of the claim
610 * @vid: the VLAN ID of the frame
611 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100612 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200613static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200614 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800615 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100616{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800617 struct batadv_bla_claim *claim;
618 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100619 int hash_added;
620
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100621 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100622 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
625 /* create a new claim entry if it does not exist yet. */
626 if (!claim) {
627 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
628 if (!claim)
629 return;
630
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100631 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100632 claim->vid = vid;
633 claim->lasttime = jiffies;
634 claim->backbone_gw = backbone_gw;
635
636 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200637 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200638 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200639 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200640 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200641 batadv_compare_claim,
642 batadv_choose_claim, claim,
643 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100644
645 if (unlikely(hash_added != 0)) {
646 /* only local changes happened. */
647 kfree(claim);
648 return;
649 }
650 } else {
651 claim->lasttime = jiffies;
652 if (claim->backbone_gw == backbone_gw)
653 /* no need to register a new backbone */
654 goto claim_free_ref;
655
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200656 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200657 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200658 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100659
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200660 spin_lock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200661 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200662 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200663 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100664 }
665 /* set (new) backbone gw */
666 atomic_inc(&backbone_gw->refcount);
667 claim->backbone_gw = backbone_gw;
668
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200669 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100670 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200671 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100672 backbone_gw->lasttime = jiffies;
673
674claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200675 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100676}
677
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100678/**
679 * batadv_bla_del_claim - delete a claim from the claim hash
680 * @bat_priv: the bat priv with all the soft interface information
681 * @mac: mac address of the claim to be removed
682 * @vid: VLAN id for the claim to be removed
Simon Wunderlich23721382012-01-22 20:00:19 +0100683 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200684static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200685 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100686{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800687 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100688
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100689 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100690 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200691 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100692 if (!claim)
693 return;
694
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200695 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200696 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100697
Sven Eckelmann807736f2012-07-15 22:26:51 +0200698 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200699 batadv_choose_claim, claim);
700 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100701
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200702 spin_lock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100703 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200704 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100705
706 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200707 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100708}
709
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200710/**
711 * batadv_handle_announce - check for ANNOUNCE frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100712 * @bat_priv: the bat priv with all the soft interface information
713 * @an_addr: announcement mac address (ARP Sender HW address)
714 * @backbone_addr: originator address of the sender (Ethernet source MAC)
715 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200716 *
717 * Return: 1 if handled
718 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200719static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
720 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100721{
Marek Lindnerbae98772012-12-25 17:03:24 +0800722 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200723 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100724
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200725 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100726 return 0;
727
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200728 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
729 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100730
731 if (unlikely(!backbone_gw))
732 return 1;
733
Simon Wunderlich23721382012-01-22 20:00:19 +0100734 /* handle as ANNOUNCE frame */
735 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100736 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100737
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200738 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100739 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200740 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100741
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200742 spin_lock_bh(&backbone_gw->crc_lock);
743 backbone_crc = backbone_gw->crc;
744 spin_unlock_bh(&backbone_gw->crc_lock);
745
746 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200747 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100748 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200749 backbone_gw->orig,
750 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200751 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100752
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200753 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100754 } else {
755 /* if we have sent a request and the crc was OK,
756 * we can allow traffic again.
757 */
758 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200759 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100760 atomic_set(&backbone_gw->request_sent, 0);
761 }
762 }
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
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200768/**
769 * batadv_handle_request - check for REQUEST frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100770 * @bat_priv: the bat priv with all the soft interface information
771 * @primary_if: the primary hard interface of this batman soft interface
772 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
773 * @ethhdr: ethernet header of a packet
774 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200775 *
776 * Return: 1 if handled
777 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200778static int batadv_handle_request(struct batadv_priv *bat_priv,
779 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200780 u8 *backbone_addr, struct ethhdr *ethhdr,
781 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100782{
783 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200784 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100785 return 0;
786
787 /* sanity check, this should not happen on a normal switch,
788 * we ignore it in this case.
789 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200790 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100791 return 1;
792
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200793 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200794 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200795 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100796
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200797 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100798 return 1;
799}
800
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200801/**
802 * batadv_handle_unclaim - check for UNCLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100803 * @bat_priv: the bat priv with all the soft interface information
804 * @primary_if: the primary hard interface of this batman soft interface
805 * @backbone_addr: originator address of the backbone (Ethernet source)
806 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
807 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200808 *
809 * Return: 1 if handled
810 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200811static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
812 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200813 u8 *backbone_addr, u8 *claim_addr,
814 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100815{
Marek Lindnerbae98772012-12-25 17:03:24 +0800816 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100817
818 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200819 if (primary_if && batadv_compare_eth(backbone_addr,
820 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200821 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200822 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100823
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200824 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100825
826 if (!backbone_gw)
827 return 1;
828
829 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200830 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200831 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200832 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100833
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200834 batadv_bla_del_claim(bat_priv, claim_addr, vid);
835 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100836 return 1;
837}
838
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200839/**
840 * batadv_handle_claim - check for CLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100841 * @bat_priv: the bat priv with all the soft interface information
842 * @primary_if: the primary hard interface of this batman soft interface
843 * @backbone_addr: originator address of the backbone (Ethernet Source)
844 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
845 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200846 *
847 * Return: 1 if handled
848 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200849static int batadv_handle_claim(struct batadv_priv *bat_priv,
850 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200851 u8 *backbone_addr, u8 *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200852 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100853{
Marek Lindnerbae98772012-12-25 17:03:24 +0800854 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100855
856 /* register the gateway if not yet available, and add the claim. */
857
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200858 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
859 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100860
861 if (unlikely(!backbone_gw))
862 return 1;
863
864 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200865 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200866 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200867 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200868 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100869
870 /* TODO: we could call something like tt_local_del() here. */
871
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200872 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100873 return 1;
874}
875
Ben Hutchings2c530402012-07-10 10:55:09 +0000876/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100877 * batadv_check_claim_group - check for claim group membership
Ben Hutchings2c530402012-07-10 10:55:09 +0000878 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200879 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100880 * @hw_src: the Hardware source in the ARP Header
881 * @hw_dst: the Hardware destination in the ARP Header
882 * @ethhdr: pointer to the Ethernet header of the claim frame
883 *
884 * checks if it is a claim packet and if its on the same group.
885 * This function also applies the group ID of the sender
886 * if it is in the same mesh.
887 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200888 * Return:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100889 * 2 - if it is a claim packet and on the same group
890 * 1 - if is a claim packet from another group
891 * 0 - if it is not a claim packet
892 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200893static int batadv_check_claim_group(struct batadv_priv *bat_priv,
894 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200895 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200896 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100897{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200898 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200899 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200900 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100901
Sven Eckelmann96412692012-06-05 22:31:30 +0200902 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200903 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100904
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100905 /* if announcement packet, use the source,
906 * otherwise assume it is in the hw_src
907 */
908 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200909 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100910 backbone_addr = hw_src;
911 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200912 case BATADV_CLAIM_TYPE_REQUEST:
913 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200914 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100915 backbone_addr = ethhdr->h_source;
916 break;
917 default:
918 return 0;
919 }
920
921 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200922 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100923 return 0;
924
925 /* if its already the same group, it is fine. */
926 if (bla_dst->group == bla_dst_own->group)
927 return 2;
928
929 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200930 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100931
932 /* dont accept claims from gateways which are not in
933 * the same mesh or group.
934 */
935 if (!orig_node)
936 return 1;
937
938 /* if our mesh friends mac is bigger, use it for ourselves. */
939 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200940 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100941 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200942 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100943 bla_dst_own->group = bla_dst->group;
944 }
945
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200946 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100947
948 return 2;
949}
950
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100951/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100952 * batadv_bla_process_claim - Check if this is a claim frame, and process it
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100953 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200954 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +0100955 * @skb: the frame to be checked
956 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200957 * Return: 1 if it was a claim frame, otherwise return 0 to
Simon Wunderlich23721382012-01-22 20:00:19 +0100958 * tell the callee that it can use the frame on its own.
959 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200960static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
961 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200962 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100963{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200964 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200965 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200966 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200967 struct ethhdr *ethhdr;
968 struct arphdr *arphdr;
969 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200970 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200971 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +0100972 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100973 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100974
Antonio Quartullic018ad32013-06-04 12:11:39 +0200975 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200976 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100977
Antonio Quartullic018ad32013-06-04 12:11:39 +0200978 proto = ethhdr->h_proto;
979 headlen = ETH_HLEN;
980 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200981 /* Traverse the VLAN/Ethertypes.
982 *
983 * At this point it is known that the first protocol is a VLAN
984 * header, so start checking at the encapsulated protocol.
985 *
986 * The depth of the VLAN headers is recorded to drop BLA claim
987 * frames encapsulated into multiple VLAN headers (QinQ).
988 */
989 do {
990 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
991 &vhdr_buf);
992 if (!vhdr)
993 return 0;
994
995 proto = vhdr->h_vlan_encapsulated_proto;
996 headlen += VLAN_HLEN;
997 vlan_depth++;
998 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +0100999 }
1000
Antonio Quartulli293e9332013-05-19 12:55:16 +02001001 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +01001002 return 0; /* not a claim frame */
1003
1004 /* this must be a ARP frame. check if it is a claim. */
1005
1006 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1007 return 0;
1008
1009 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001010 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001011 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +01001012
1013 /* Check whether the ARP frame carries a valid
1014 * IP information
1015 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001016 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1017 return 0;
1018 if (arphdr->ar_pro != htons(ETH_P_IP))
1019 return 0;
1020 if (arphdr->ar_hln != ETH_ALEN)
1021 return 0;
1022 if (arphdr->ar_pln != 4)
1023 return 0;
1024
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001025 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001026 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +02001027 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001028 bla_dst_own = &bat_priv->bla.claim_dest;
1029
1030 /* check if it is a claim frame in general */
1031 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1032 sizeof(bla_dst->magic)) != 0)
1033 return 0;
1034
1035 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1036 * drop that, as this is not supported by BLA but should also not be
1037 * sent via the mesh.
1038 */
1039 if (vlan_depth > 1)
1040 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +01001041
1042 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001043 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1044 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001045 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001046 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001047 "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 +02001048 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1049 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001050
1051 if (ret < 2)
1052 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001053
1054 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001055 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001056
1057 /* check for the different types of claim frames ... */
1058 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001059 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001060 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1061 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001062 return 1;
1063 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001064 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001065 if (batadv_handle_unclaim(bat_priv, primary_if,
1066 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001067 return 1;
1068 break;
1069
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001070 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001071 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1072 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001073 return 1;
1074 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001075 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001076 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1077 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001078 return 1;
1079 break;
1080 }
1081
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001082 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001083 "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 +02001084 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +01001085 return 1;
1086}
1087
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001088/**
1089 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1090 * immediately
1091 * @bat_priv: the bat priv with all the soft interface information
1092 * @now: whether the whole hash shall be wiped now
1093 *
1094 * Check when we last heard from other nodes, and remove them in case of
Simon Wunderlich23721382012-01-22 20:00:19 +01001095 * a time out, or clean all backbone gws if now is set.
1096 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001097static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001098{
Marek Lindnerbae98772012-12-25 17:03:24 +08001099 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001100 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001101 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001102 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001103 spinlock_t *list_lock; /* protects write access to the hash lists */
1104 int i;
1105
Sven Eckelmann807736f2012-07-15 22:26:51 +02001106 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001107 if (!hash)
1108 return;
1109
1110 for (i = 0; i < hash->size; i++) {
1111 head = &hash->table[i];
1112 list_lock = &hash->list_locks[i];
1113
1114 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001115 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001116 head, hash_entry) {
1117 if (now)
1118 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001119 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001120 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001121 continue;
1122
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001123 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001124 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1125 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001126
1127purge_now:
1128 /* don't wait for the pending request anymore */
1129 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001130 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001131
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001132 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001133
Sasha Levinb67bfe02013-02-27 17:06:00 -08001134 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001135 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001136 }
1137 spin_unlock_bh(list_lock);
1138 }
1139}
1140
Ben Hutchings2c530402012-07-10 10:55:09 +00001141/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001142 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
Ben Hutchings2c530402012-07-10 10:55:09 +00001143 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001144 * @primary_if: the selected primary interface, may be NULL if now is set
1145 * @now: whether the whole hash shall be wiped now
1146 *
1147 * Check when we heard last time from our own claims, and remove them in case of
1148 * a time out, or clean all claims if now is set
1149 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001150static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1151 struct batadv_hard_iface *primary_if,
1152 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001153{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001154 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001155 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001156 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001157 int i;
1158
Sven Eckelmann807736f2012-07-15 22:26:51 +02001159 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001160 if (!hash)
1161 return;
1162
1163 for (i = 0; i < hash->size; i++) {
1164 head = &hash->table[i];
1165
1166 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001167 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001168 if (now)
1169 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001170 if (!batadv_compare_eth(claim->backbone_gw->orig,
1171 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001172 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001173 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001174 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001175 continue;
1176
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001177 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001178 "bla_purge_claims(): %pM, vid %d, time out\n",
1179 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001180
1181purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001182 batadv_handle_unclaim(bat_priv, primary_if,
1183 claim->backbone_gw->orig,
1184 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001185 }
1186 rcu_read_unlock();
1187 }
1188}
1189
Ben Hutchings2c530402012-07-10 10:55:09 +00001190/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001191 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1192 * originator address changes
Ben Hutchings2c530402012-07-10 10:55:09 +00001193 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001194 * @primary_if: the new selected primary_if
1195 * @oldif: the old primary interface, may be NULL
Simon Wunderlich23721382012-01-22 20:00:19 +01001196 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001197void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1198 struct batadv_hard_iface *primary_if,
1199 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001200{
Marek Lindnerbae98772012-12-25 17:03:24 +08001201 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001202 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001203 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001204 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001205 int i;
1206
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001207 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001208 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1209 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001210
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001211 /* purge everything when bridge loop avoidance is turned off */
1212 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1213 oldif = NULL;
1214
Simon Wunderlich23721382012-01-22 20:00:19 +01001215 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001216 batadv_bla_purge_claims(bat_priv, NULL, 1);
1217 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001218 return;
1219 }
1220
Sven Eckelmann807736f2012-07-15 22:26:51 +02001221 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001222 if (!hash)
1223 return;
1224
1225 for (i = 0; i < hash->size; i++) {
1226 head = &hash->table[i];
1227
1228 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001229 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001230 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001231 if (!batadv_compare_eth(backbone_gw->orig,
1232 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001233 continue;
1234
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001235 ether_addr_copy(backbone_gw->orig,
1236 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001237 /* send an announce frame so others will ask for our
1238 * claims and update their tables.
1239 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001240 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001241 }
1242 rcu_read_unlock();
1243 }
1244}
1245
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001246/**
1247 * batadv_bla_status_update - purge bla interfaces if necessary
1248 * @net_dev: the soft interface net device
1249 */
1250void batadv_bla_status_update(struct net_device *net_dev)
1251{
1252 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1253 struct batadv_hard_iface *primary_if;
1254
1255 primary_if = batadv_primary_if_get_selected(bat_priv);
1256 if (!primary_if)
1257 return;
1258
1259 /* this function already purges everything when bla is disabled,
1260 * so just call that one.
1261 */
1262 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1263 batadv_hardif_free_ref(primary_if);
1264}
1265
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001266/**
1267 * batadv_bla_periodic_work - performs periodic bla work
1268 * @work: kernel work struct
1269 *
1270 * periodic work to do:
Simon Wunderlich23721382012-01-22 20:00:19 +01001271 * * purge structures when they are too old
1272 * * send announcements
1273 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001274static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001275{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001276 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001277 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001278 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001279 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001280 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001281 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001282 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001283 int i;
1284
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001285 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001286 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1287 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001288 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001289 if (!primary_if)
1290 goto out;
1291
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001292 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1293 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001294
1295 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1296 goto out;
1297
Sven Eckelmann807736f2012-07-15 22:26:51 +02001298 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001299 if (!hash)
1300 goto out;
1301
1302 for (i = 0; i < hash->size; i++) {
1303 head = &hash->table[i];
1304
1305 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001306 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001307 if (!batadv_compare_eth(backbone_gw->orig,
1308 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001309 continue;
1310
1311 backbone_gw->lasttime = jiffies;
1312
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001313 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001314
1315 /* request_sent is only set after creation to avoid
1316 * problems when we are not yet known as backbone gw
1317 * in the backbone.
1318 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001319 * We can reset this now after we waited some periods
1320 * to give bridge forward delays and bla group forming
1321 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001322 */
1323
1324 if (atomic_read(&backbone_gw->request_sent) == 0)
1325 continue;
1326
Simon Wunderlich28709872012-09-13 18:18:46 +02001327 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1328 continue;
1329
Simon Wunderlichd807f272012-09-09 22:27:57 +02001330 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1331 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001332 }
1333 rcu_read_unlock();
1334 }
1335out:
1336 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001337 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001338
Antonio Quartulli72414442012-12-25 13:14:37 +01001339 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1340 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001341}
1342
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001343/* The hash for claim and backbone hash receive the same key because they
1344 * are getting initialized by hash_new with the same key. Reinitializing
1345 * them with to different keys to allow nested locking without generating
1346 * lockdep warnings
1347 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001348static struct lock_class_key batadv_claim_hash_lock_class_key;
1349static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001350
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001351/**
1352 * batadv_bla_init - initialize all bla structures
1353 * @bat_priv: the bat priv with all the soft interface information
1354 *
1355 * Return: 0 on success, < 0 on error.
1356 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001357int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001358{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001359 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001360 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001361 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001362 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001363 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001364
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001365 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1366
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001367 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001368
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001369 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001370 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1371 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001372 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001373 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001374 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1375 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001376 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001377 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001378 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001379 }
1380
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001381 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001382 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001383 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001384 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1385 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001386
Sven Eckelmann807736f2012-07-15 22:26:51 +02001387 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001388 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001389
Sven Eckelmann807736f2012-07-15 22:26:51 +02001390 bat_priv->bla.claim_hash = batadv_hash_new(128);
1391 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001392
Sven Eckelmann807736f2012-07-15 22:26:51 +02001393 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001394 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001395
Sven Eckelmann807736f2012-07-15 22:26:51 +02001396 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001397 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001398 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001399 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001400
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001401 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001402
Antonio Quartulli72414442012-12-25 13:14:37 +01001403 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1404
1405 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1406 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001407 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001408}
1409
Ben Hutchings2c530402012-07-10 10:55:09 +00001410/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001411 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
Ben Hutchings2c530402012-07-10 10:55:09 +00001412 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001413 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001414 *
1415 * check if it is on our broadcast list. Another gateway might
1416 * have sent the same packet because it is connected to the same backbone,
1417 * so we have to remove this duplicate.
1418 *
1419 * This is performed by checking the CRC, which will tell us
1420 * with a good chance that it is the same packet. If it is furthermore
1421 * sent by another host, drop it. We allow equal packets from
1422 * the same host however as this might be intended.
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001423 *
1424 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001425 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001426int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001427 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001428{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001429 int i, curr, ret = 0;
1430 __be32 crc;
1431 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001432 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001433
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001434 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001435
1436 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001437 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001438
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001439 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1440
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001441 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001442 curr = (bat_priv->bla.bcast_duplist_curr + i);
1443 curr %= BATADV_DUPLIST_SIZE;
1444 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001445
1446 /* we can stop searching if the entry is too old ;
1447 * later entries will be even older
1448 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001449 if (batadv_has_timed_out(entry->entrytime,
1450 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001451 break;
1452
1453 if (entry->crc != crc)
1454 continue;
1455
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001456 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001457 continue;
1458
1459 /* this entry seems to match: same crc, not too old,
1460 * and from another gw. therefore return 1 to forbid it.
1461 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001462 ret = 1;
1463 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001464 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001465 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001466 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001467 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001468 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001469 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001470 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001471 entry->crc = crc;
1472 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001473 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001474 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001475
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001476out:
1477 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1478
1479 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001480}
1481
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001482/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001483 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1484 * the VLAN identified by vid.
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001485 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001486 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001487 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001488 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001489 * Return: true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001490 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001491bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001492 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001493{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001494 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001495 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001496 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001497 int i;
1498
1499 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001500 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001501
1502 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001503 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001504
1505 for (i = 0; i < hash->size; i++) {
1506 head = &hash->table[i];
1507
1508 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001509 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001510 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1511 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001512 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001513 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001514 }
1515 }
1516 rcu_read_unlock();
1517 }
1518
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001519 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001520}
1521
Ben Hutchings2c530402012-07-10 10:55:09 +00001522/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001523 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
Ben Hutchings2c530402012-07-10 10:55:09 +00001524 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001525 * @orig_node: the orig_node of the frame
1526 * @hdr_size: maximum length of the frame
1527 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001528 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
1529 * it returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001530 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001531int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001532 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001533{
Marek Lindnerbae98772012-12-25 17:03:24 +08001534 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001535 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001536
1537 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1538 return 0;
1539
1540 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001541 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001542 return 0;
1543
Antonio Quartullic018ad32013-06-04 12:11:39 +02001544 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001545
1546 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001547 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1548 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001549 if (!backbone_gw)
1550 return 0;
1551
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001552 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001553 return 1;
1554}
1555
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001556/**
1557 * batadv_bla_init - free all bla structures
1558 * @bat_priv: the bat priv with all the soft interface information
1559 *
1560 * for softinterface free or module unload
1561 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001562void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001563{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001564 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001565
Sven Eckelmann807736f2012-07-15 22:26:51 +02001566 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001567 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001568
Sven Eckelmann807736f2012-07-15 22:26:51 +02001569 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001570 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001571 batadv_hash_destroy(bat_priv->bla.claim_hash);
1572 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001573 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001574 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001575 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001576 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1577 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001578 }
1579 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001580 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001581}
1582
Ben Hutchings2c530402012-07-10 10:55:09 +00001583/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001584 * batadv_bla_rx - check packets coming from the mesh.
Ben Hutchings2c530402012-07-10 10:55:09 +00001585 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001586 * @skb: the frame to be checked
1587 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001588 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001589 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001590 * batadv_bla_rx avoidance checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001591 * * we have to race for a claim
1592 * * if the frame is allowed on the LAN
1593 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001594 * in these cases, the skb is further handled by this function
1595 *
1596 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
Simon Wunderlich23721382012-01-22 20:00:19 +01001597 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001598 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001599int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1600 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001601{
1602 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001603 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001604 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001605 int ret;
1606
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001607 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001608
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001609 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001610 if (!primary_if)
1611 goto handled;
1612
1613 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1614 goto allow;
1615
Sven Eckelmann807736f2012-07-15 22:26:51 +02001616 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001617 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001618 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001619 goto handled;
1620
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001621 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001622 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001623 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001624
1625 if (!claim) {
1626 /* possible optimization: race for a claim */
1627 /* No claim exists yet, claim it for us!
1628 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001629 batadv_handle_claim(bat_priv, primary_if,
1630 primary_if->net_dev->dev_addr,
1631 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001632 goto allow;
1633 }
1634
1635 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001636 if (batadv_compare_eth(claim->backbone_gw->orig,
1637 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001638 /* ... allow it in any case */
1639 claim->lasttime = jiffies;
1640 goto allow;
1641 }
1642
1643 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001644 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1645 /* ... drop it. the responsible gateway is in charge.
1646 *
1647 * We need to check is_bcast because with the gateway
1648 * feature, broadcasts (like DHCP requests) may be sent
1649 * using a unicast packet type.
1650 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001651 goto handled;
1652 } else {
1653 /* seems the client considers us as its best gateway.
1654 * send a claim and update the claim table
1655 * immediately.
1656 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001657 batadv_handle_claim(bat_priv, primary_if,
1658 primary_if->net_dev->dev_addr,
1659 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001660 goto allow;
1661 }
1662allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001663 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001664 ret = 0;
1665 goto out;
1666
1667handled:
1668 kfree_skb(skb);
1669 ret = 1;
1670
1671out:
1672 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001673 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001674 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001675 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001676 return ret;
1677}
1678
Ben Hutchings2c530402012-07-10 10:55:09 +00001679/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001680 * batadv_bla_tx - check packets going into the mesh
Ben Hutchings2c530402012-07-10 10:55:09 +00001681 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001682 * @skb: the frame to be checked
1683 * @vid: the VLAN ID of the frame
1684 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001685 * batadv_bla_tx checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001686 * * a claim was received which has to be processed
1687 * * the frame is allowed on the mesh
1688 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001689 * in these cases, the skb is further handled by this function.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001690 *
1691 * This call might reallocate skb data.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001692 *
1693 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1694 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001695 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001696int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1697 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001698{
1699 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001700 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001701 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001702 int ret = 0;
1703
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001704 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001705 if (!primary_if)
1706 goto out;
1707
1708 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1709 goto allow;
1710
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001711 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001712 goto handled;
1713
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001714 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001715
Sven Eckelmann807736f2012-07-15 22:26:51 +02001716 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001717 /* don't allow broadcasts while requests are in flight */
1718 if (is_multicast_ether_addr(ethhdr->h_dest))
1719 goto handled;
1720
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001721 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001722 search_claim.vid = vid;
1723
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001724 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001725
1726 /* if no claim exists, allow it. */
1727 if (!claim)
1728 goto allow;
1729
1730 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001731 if (batadv_compare_eth(claim->backbone_gw->orig,
1732 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001733 /* if yes, the client has roamed and we have
1734 * to unclaim it.
1735 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001736 batadv_handle_unclaim(bat_priv, primary_if,
1737 primary_if->net_dev->dev_addr,
1738 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001739 goto allow;
1740 }
1741
1742 /* check if it is a multicast/broadcast frame */
1743 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1744 /* drop it. the responsible gateway has forwarded it into
1745 * the backbone network.
1746 */
1747 goto handled;
1748 } else {
1749 /* we must allow it. at least if we are
1750 * responsible for the DESTINATION.
1751 */
1752 goto allow;
1753 }
1754allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001755 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001756 ret = 0;
1757 goto out;
1758handled:
1759 ret = 1;
1760out:
1761 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001762 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001763 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001764 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001765 return ret;
1766}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001767
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001768/**
1769 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1770 * @seq: seq file to print on
1771 * @offset: not used
1772 *
1773 * Return: always 0
1774 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001775int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001776{
1777 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001778 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001779 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001780 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001781 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001782 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001783 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001784 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001785 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001786 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001787
Marek Lindner30da63a2012-08-03 17:15:46 +02001788 primary_if = batadv_seq_print_text_primary_if_get(seq);
1789 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001790 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001791
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001792 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001793 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001794 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001795 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001796 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001797 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001798 "Client", "VID", "Originator", "CRC");
1799 for (i = 0; i < hash->size; i++) {
1800 head = &hash->table[i];
1801
1802 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001803 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001804 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1805 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001806
1807 spin_lock_bh(&claim->backbone_gw->crc_lock);
1808 backbone_crc = claim->backbone_gw->crc;
1809 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001810 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001811 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001812 claim->backbone_gw->orig,
1813 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001814 backbone_crc);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001815 }
1816 rcu_read_unlock();
1817 }
1818out:
1819 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001820 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001821 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001822}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001823
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001824/**
1825 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1826 * file
1827 * @seq: seq file to print on
1828 * @offset: not used
1829 *
1830 * Return: always 0
1831 */
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001832int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1833{
1834 struct net_device *net_dev = (struct net_device *)seq->private;
1835 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001836 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001837 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001838 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001839 struct hlist_head *head;
1840 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001841 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001842 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001843 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001844 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001845
Marek Lindner30da63a2012-08-03 17:15:46 +02001846 primary_if = batadv_seq_print_text_primary_if_get(seq);
1847 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001848 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001849
1850 primary_addr = primary_if->net_dev->dev_addr;
1851 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001852 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001853 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001854 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001855 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001856 "Originator", "VID", "last seen", "CRC");
1857 for (i = 0; i < hash->size; i++) {
1858 head = &hash->table[i];
1859
1860 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001861 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001862 msecs = jiffies_to_msecs(jiffies -
1863 backbone_gw->lasttime);
1864 secs = msecs / 1000;
1865 msecs = msecs % 1000;
1866
1867 is_own = batadv_compare_eth(backbone_gw->orig,
1868 primary_addr);
1869 if (is_own)
1870 continue;
1871
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001872 spin_lock_bh(&backbone_gw->crc_lock);
1873 backbone_crc = backbone_gw->crc;
1874 spin_unlock_bh(&backbone_gw->crc_lock);
1875
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001876 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001877 backbone_gw->orig,
1878 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001879 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001880 }
1881 rcu_read_unlock();
1882 }
1883out:
1884 if (primary_if)
1885 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001886 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001887}