blob: 5064ae5e9b342901603591e55447e42ca4576c47 [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2011-2016 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>
Sven Eckelmann06e56de2016-01-16 10:29:43 +010034#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/list.h>
36#include <linux/lockdep.h>
37#include <linux/netdevice.h>
38#include <linux/rculist.h>
39#include <linux/rcupdate.h>
40#include <linux/seq_file.h>
41#include <linux/skbuff.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/stddef.h>
45#include <linux/string.h>
46#include <linux/workqueue.h>
47#include <net/arp.h>
48
49#include "hard-interface.h"
50#include "hash.h"
51#include "originator.h"
52#include "packet.h"
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +010053#include "sysfs.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020054#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010055
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020056static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010057
Sven Eckelmann3b300de2012-05-12 18:33:53 +020058static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080059static void
60batadv_bla_send_announce(struct batadv_priv *bat_priv,
61 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010062
Sven Eckelmann62fe7102015-09-15 19:00:48 +020063/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010064 * batadv_choose_claim - choose the right bucket for a claim.
65 * @data: data to hash
66 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020067 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010068 * Return: the hash index of the claim
Sven Eckelmann62fe7102015-09-15 19:00:48 +020069 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020070static inline u32 batadv_choose_claim(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010071{
Marek Lindner712bbfe42012-12-25 17:03:25 +080072 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020073 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010074
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010075 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
76 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010077
78 return hash % size;
79}
80
Sven Eckelmann62fe7102015-09-15 19:00:48 +020081/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010082 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
83 * @data: data to hash
84 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020085 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010086 * Return: the hash index of the backbone gateway
Sven Eckelmann62fe7102015-09-15 19:00:48 +020087 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020088static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010089{
Marek Lindner712bbfe42012-12-25 17:03:25 +080090 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020091 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010092
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010093 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
94 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010095
96 return hash % size;
97}
98
Simon Wunderlich04e14be2015-11-06 10:45:19 +010099/**
100 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
101 * @node: list node of the first entry to compare
102 * @data2: pointer to the second backbone gateway
103 *
104 * Return: 1 if the backbones have the same data, 0 otherwise
105 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200106static int batadv_compare_backbone_gw(const struct hlist_node *node,
107 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100108{
Marek Lindnerbae98772012-12-25 17:03:24 +0800109 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +0100110 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200111 const struct batadv_bla_backbone_gw *gw1 = data1;
112 const struct batadv_bla_backbone_gw *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100113
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200114 if (!batadv_compare_eth(gw1->orig, gw2->orig))
115 return 0;
116
117 if (gw1->vid != gw2->vid)
118 return 0;
119
120 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100121}
122
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100123/**
Sven Eckelmann98a5b1d2016-02-22 18:55:32 +0100124 * batadv_compare_claim - compare address and vid of two claims
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100125 * @node: list node of the first entry to compare
126 * @data2: pointer to the second claims
127 *
128 * Return: 1 if the claim have the same data, 0 otherwise
129 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200130static int batadv_compare_claim(const struct hlist_node *node,
131 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100132{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800133 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +0100134 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200135 const struct batadv_bla_claim *cl1 = data1;
136 const struct batadv_bla_claim *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100137
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200138 if (!batadv_compare_eth(cl1->addr, cl2->addr))
139 return 0;
140
141 if (cl1->vid != cl2->vid)
142 return 0;
143
144 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100145}
146
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100147/**
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100148 * batadv_backbone_gw_release - release backbone gw from lists and queue for
149 * free after rcu grace period
150 * @ref: kref pointer of the backbone gw
151 */
152static void batadv_backbone_gw_release(struct kref *ref)
153{
154 struct batadv_bla_backbone_gw *backbone_gw;
155
156 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
157 refcount);
158
159 kfree_rcu(backbone_gw, rcu);
160}
161
162/**
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100163 * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
164 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100165 * @backbone_gw: backbone gateway to be free'd
166 */
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100167static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100168{
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100169 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
Simon Wunderlich23721382012-01-22 20:00:19 +0100170}
171
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100172/**
173 * batadv_claim_release - release claim from lists and queue for free after rcu
174 * grace period
175 * @ref: kref pointer of the claim
176 */
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100177static void batadv_claim_release(struct kref *ref)
Simon Wunderlich23721382012-01-22 20:00:19 +0100178{
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100179 struct batadv_bla_claim *claim;
180
181 claim = container_of(ref, struct batadv_bla_claim, refcount);
182
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100183 batadv_backbone_gw_put(claim->backbone_gw);
Sven Eckelmann63b39922016-01-14 15:28:19 +0100184 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100185}
186
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100187/**
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100188 * batadv_claim_put - decrement the claim refcounter and possibly
Sven Eckelmannec9b83c2016-01-05 12:06:16 +0100189 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100190 * @claim: claim to be free'd
191 */
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100192static void batadv_claim_put(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100193{
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100194 kref_put(&claim->refcount, batadv_claim_release);
Simon Wunderlich23721382012-01-22 20:00:19 +0100195}
196
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100197/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100198 * batadv_claim_hash_find - looks for a claim in the claim hash
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100199 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100200 * @data: search data (may be local/static data)
201 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200202 * Return: claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100203 */
Sven Eckelmann6fc77a52016-03-05 15:56:01 +0100204static struct batadv_bla_claim *
205batadv_claim_hash_find(struct batadv_priv *bat_priv,
206 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100207{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200208 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100209 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800210 struct batadv_bla_claim *claim;
211 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100212 int index;
213
214 if (!hash)
215 return NULL;
216
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200217 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100218 head = &hash->table[index];
219
220 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800221 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200222 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100223 continue;
224
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100225 if (!kref_get_unless_zero(&claim->refcount))
Simon Wunderlich23721382012-01-22 20:00:19 +0100226 continue;
227
228 claim_tmp = claim;
229 break;
230 }
231 rcu_read_unlock();
232
233 return claim_tmp;
234}
235
Ben Hutchings2c530402012-07-10 10:55:09 +0000236/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100237 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
Ben Hutchings2c530402012-07-10 10:55:09 +0000238 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100239 * @addr: the address of the originator
240 * @vid: the VLAN ID
241 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100242 * Return: backbone gateway if found or NULL otherwise
Simon Wunderlich23721382012-01-22 20:00:19 +0100243 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800244static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200245batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
246 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100247{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200248 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100249 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800250 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
251 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100252 int index;
253
254 if (!hash)
255 return NULL;
256
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100257 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100258 search_entry.vid = vid;
259
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200260 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100261 head = &hash->table[index];
262
263 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800264 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200265 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
266 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100267 continue;
268
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100269 if (!kref_get_unless_zero(&backbone_gw->refcount))
Simon Wunderlich23721382012-01-22 20:00:19 +0100270 continue;
271
272 backbone_gw_tmp = backbone_gw;
273 break;
274 }
275 rcu_read_unlock();
276
277 return backbone_gw_tmp;
278}
279
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100280/**
281 * batadv_bla_del_backbone_claims - delete all claims for a backbone
282 * @backbone_gw: backbone gateway where the claims should be removed
283 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200284static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800285batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100286{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200287 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800288 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100289 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800290 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100291 int i;
292 spinlock_t *list_lock; /* protects write access to the hash lists */
293
Sven Eckelmann807736f2012-07-15 22:26:51 +0200294 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100295 if (!hash)
296 return;
297
298 for (i = 0; i < hash->size; i++) {
299 head = &hash->table[i];
300 list_lock = &hash->list_locks[i];
301
302 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800303 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100304 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100305 if (claim->backbone_gw != backbone_gw)
306 continue;
307
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100308 batadv_claim_put(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800309 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100310 }
311 spin_unlock_bh(list_lock);
312 }
313
Antonio Quartulli3f687852014-11-02 11:29:56 +0100314 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200315 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200316 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200317 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100318}
319
Ben Hutchings2c530402012-07-10 10:55:09 +0000320/**
321 * batadv_bla_send_claim - sends a claim frame according to the provided info
322 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200323 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100324 * @vid: the VLAN ID
325 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100326 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200327static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200328 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100329{
330 struct sk_buff *skb;
331 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200332 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100333 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200334 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200335 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100336 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100337
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200338 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100339 if (!primary_if)
340 return;
341
Sven Eckelmann807736f2012-07-15 22:26:51 +0200342 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100343 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100344 local_claim_dest.type = claimtype;
345
346 soft_iface = primary_if->soft_iface;
347
348 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
349 /* IP DST: 0.0.0.0 */
350 zeroip,
351 primary_if->soft_iface,
352 /* IP SRC: 0.0.0.0 */
353 zeroip,
354 /* Ethernet DST: Broadcast */
355 NULL,
356 /* Ethernet SRC/HW SRC: originator mac */
357 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200358 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100359 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100360 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100361 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200362 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100363
364 if (!skb)
365 goto out;
366
367 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200368 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100369
370 /* now we pretend that the client would have sent this ... */
371 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200372 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100373 /* normal claim frame
374 * set Ethernet SRC to the clients mac
375 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100376 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200377 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200378 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
379 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100380 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200381 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100382 /* unclaim frame
383 * set HW SRC to the clients mac
384 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100385 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200386 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200387 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200388 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100389 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200390 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100391 /* announcement frame
392 * set HW SRC to the special mac containg the crc
393 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100394 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200395 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200396 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200397 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100398 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200399 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100400 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200401 * set HW SRC and header destination to the receiving backbone
402 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100403 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100404 ether_addr_copy(hw_src, mac);
405 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200406 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200407 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200408 ethhdr->h_source, ethhdr->h_dest,
409 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100410 break;
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100411 case BATADV_CLAIM_TYPE_LOOPDETECT:
412 ether_addr_copy(ethhdr->h_source, mac);
413 batadv_dbg(BATADV_DBG_BLA, bat_priv,
414 "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
415 ethhdr->h_source, ethhdr->h_dest,
416 BATADV_PRINT_VID(vid));
417
418 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100419 }
420
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200421 if (vid & BATADV_VLAN_HAS_TAG)
422 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
423 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100424
425 skb_reset_mac_header(skb);
426 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200427 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
428 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
429 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100430 soft_iface->last_rx = jiffies;
431
432 netif_rx(skb);
433out:
434 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100435 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100436}
437
Ben Hutchings2c530402012-07-10 10:55:09 +0000438/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100439 * batadv_bla_loopdetect_report - worker for reporting the loop
440 * @work: work queue item
441 *
442 * Throws an uevent, as the loopdetect check function can't do that itself
443 * since the kernel may sleep while throwing uevents.
444 */
445static void batadv_bla_loopdetect_report(struct work_struct *work)
446{
447 struct batadv_bla_backbone_gw *backbone_gw;
448 struct batadv_priv *bat_priv;
449 char vid_str[6] = { '\0' };
450
451 backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
452 report_work);
453 bat_priv = backbone_gw->bat_priv;
454
455 batadv_info(bat_priv->soft_iface,
456 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
457 BATADV_PRINT_VID(backbone_gw->vid));
458 snprintf(vid_str, sizeof(vid_str), "%d",
459 BATADV_PRINT_VID(backbone_gw->vid));
460 vid_str[sizeof(vid_str) - 1] = 0;
461
462 batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
463 vid_str);
464
465 batadv_backbone_gw_put(backbone_gw);
466}
467
468/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100469 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
Ben Hutchings2c530402012-07-10 10:55:09 +0000470 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100471 * @orig: the mac address of the originator
472 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200473 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100474 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100475 * Return: the (possibly created) backbone gateway or NULL on error
Simon Wunderlich23721382012-01-22 20:00:19 +0100476 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800477static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200478batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200479 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100480{
Marek Lindnerbae98772012-12-25 17:03:24 +0800481 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200482 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100483 int hash_added;
484
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200485 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100486
487 if (entry)
488 return entry;
489
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200490 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200491 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200492 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100493
494 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
495 if (!entry)
496 return NULL;
497
498 entry->vid = vid;
499 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200500 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100501 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200502 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100503 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200504 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100505 ether_addr_copy(entry->orig, orig);
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100506 INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
Simon Wunderlich23721382012-01-22 20:00:19 +0100507
508 /* one for the hash, one for returning */
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100509 kref_init(&entry->refcount);
510 kref_get(&entry->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100511
Sven Eckelmann807736f2012-07-15 22:26:51 +0200512 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200513 batadv_compare_backbone_gw,
514 batadv_choose_backbone_gw, entry,
515 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100516
517 if (unlikely(hash_added != 0)) {
518 /* hash failed, free the structure */
519 kfree(entry);
520 return NULL;
521 }
522
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200523 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200524 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100525 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200526 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200527 "became a backbone gateway");
Sven Eckelmann5d967312016-01-17 11:01:09 +0100528 batadv_orig_node_put(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100529 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200530
Simon Wunderlichd807f272012-09-09 22:27:57 +0200531 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200532 batadv_bla_send_announce(bat_priv, entry);
533
Simon Wunderlichd807f272012-09-09 22:27:57 +0200534 /* this will be decreased in the worker thread */
535 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200536 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200537 atomic_inc(&bat_priv->bla.num_requests);
538 }
539
Simon Wunderlich23721382012-01-22 20:00:19 +0100540 return entry;
541}
542
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100543/**
544 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
545 * @bat_priv: the bat priv with all the soft interface information
546 * @primary_if: the selected primary interface
547 * @vid: VLAN identifier
548 *
549 * update or add the own backbone gw to make sure we announce
Simon Wunderlich23721382012-01-22 20:00:19 +0100550 * where we receive other backbone gws
551 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200552static void
553batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
554 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200555 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100556{
Marek Lindnerbae98772012-12-25 17:03:24 +0800557 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100558
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200559 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
560 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200561 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100562 if (unlikely(!backbone_gw))
563 return;
564
565 backbone_gw->lasttime = jiffies;
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100566 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100567}
568
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100569/**
570 * batadv_bla_answer_request - answer a bla request by sending own claims
571 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200572 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100573 * @vid: the vid where the request came on
574 *
575 * Repeat all of our own claims, and finally send an ANNOUNCE frame
576 * to allow the requester another check if the CRC is correct now.
577 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200578static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
579 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200580 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100581{
Simon Wunderlich23721382012-01-22 20:00:19 +0100582 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200583 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800584 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800585 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100586 int i;
587
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200588 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200589 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100590
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200591 backbone_gw = batadv_backbone_hash_find(bat_priv,
592 primary_if->net_dev->dev_addr,
593 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100594 if (!backbone_gw)
595 return;
596
Sven Eckelmann807736f2012-07-15 22:26:51 +0200597 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100598 for (i = 0; i < hash->size; i++) {
599 head = &hash->table[i];
600
601 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800602 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100603 /* only own claims are interesting */
604 if (claim->backbone_gw != backbone_gw)
605 continue;
606
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200607 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200608 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100609 }
610 rcu_read_unlock();
611 }
612
613 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200614 batadv_bla_send_announce(bat_priv, backbone_gw);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100615 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100616}
617
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100618/**
619 * batadv_bla_send_request - send a request to repeat claims
620 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100621 *
622 * When the crc is wrong, ask the backbone gateway for a full table update.
623 * After the request, it will repeat all of his own claims and finally
624 * send an announcement claim with which we can check again.
625 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800626static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100627{
628 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200629 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100630
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200631 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
632 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100633
634 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200635 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200636 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100637
638 /* no local broadcasts should be sent or received, for now. */
639 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200640 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100641 atomic_set(&backbone_gw->request_sent, 1);
642 }
643}
644
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100645/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100646 * batadv_bla_send_announce - Send an announcement frame
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100647 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100648 * @backbone_gw: our backbone gateway which should be announced
Simon Wunderlich23721382012-01-22 20:00:19 +0100649 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200650static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800651 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100652{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200653 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100654 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100655
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200656 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200657 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100658 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200659 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100660 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100661
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200662 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200663 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100664}
665
Ben Hutchings2c530402012-07-10 10:55:09 +0000666/**
667 * batadv_bla_add_claim - Adds a claim in the claim hash
668 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100669 * @mac: the mac address of the claim
670 * @vid: the VLAN ID of the frame
671 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100672 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200673static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200674 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800675 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100676{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800677 struct batadv_bla_claim *claim;
678 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100679 int hash_added;
680
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100681 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100682 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200683 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100684
685 /* create a new claim entry if it does not exist yet. */
686 if (!claim) {
687 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
688 if (!claim)
689 return;
690
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100691 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100692 claim->vid = vid;
693 claim->lasttime = jiffies;
694 claim->backbone_gw = backbone_gw;
695
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100696 kref_init(&claim->refcount);
697 kref_get(&claim->refcount);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200698 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200699 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200700 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200701 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200702 batadv_compare_claim,
703 batadv_choose_claim, claim,
704 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100705
706 if (unlikely(hash_added != 0)) {
707 /* only local changes happened. */
708 kfree(claim);
709 return;
710 }
711 } else {
712 claim->lasttime = jiffies;
713 if (claim->backbone_gw == backbone_gw)
714 /* no need to register a new backbone */
715 goto claim_free_ref;
716
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200717 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200718 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200719 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100720
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200721 spin_lock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200722 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200723 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100724 batadv_backbone_gw_put(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100725 }
726 /* set (new) backbone gw */
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100727 kref_get(&backbone_gw->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100728 claim->backbone_gw = backbone_gw;
729
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200730 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100731 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200732 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100733 backbone_gw->lasttime = jiffies;
734
735claim_free_ref:
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100736 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100737}
738
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100739/**
740 * batadv_bla_del_claim - delete a claim from the claim hash
741 * @bat_priv: the bat priv with all the soft interface information
742 * @mac: mac address of the claim to be removed
743 * @vid: VLAN id for the claim to be removed
Simon Wunderlich23721382012-01-22 20:00:19 +0100744 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200745static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200746 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100747{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800748 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100749
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100750 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100751 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200752 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100753 if (!claim)
754 return;
755
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200756 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200757 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100758
Sven Eckelmann807736f2012-07-15 22:26:51 +0200759 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200760 batadv_choose_claim, claim);
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100761 batadv_claim_put(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100762
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200763 spin_lock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100764 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200765 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100766
767 /* don't need the reference from hash_find() anymore */
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100768 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100769}
770
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200771/**
772 * batadv_handle_announce - check for ANNOUNCE frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100773 * @bat_priv: the bat priv with all the soft interface information
774 * @an_addr: announcement mac address (ARP Sender HW address)
775 * @backbone_addr: originator address of the sender (Ethernet source MAC)
776 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200777 *
778 * Return: 1 if handled
779 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200780static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
781 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100782{
Marek Lindnerbae98772012-12-25 17:03:24 +0800783 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200784 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100785
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200786 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100787 return 0;
788
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200789 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
790 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100791
792 if (unlikely(!backbone_gw))
793 return 1;
794
Simon Wunderlich23721382012-01-22 20:00:19 +0100795 /* handle as ANNOUNCE frame */
796 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100797 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100798
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200799 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100800 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200801 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100802
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200803 spin_lock_bh(&backbone_gw->crc_lock);
804 backbone_crc = backbone_gw->crc;
805 spin_unlock_bh(&backbone_gw->crc_lock);
806
807 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200808 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100809 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200810 backbone_gw->orig,
811 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200812 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100813
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200814 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100815 } else {
816 /* if we have sent a request and the crc was OK,
817 * we can allow traffic again.
818 */
819 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200820 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100821 atomic_set(&backbone_gw->request_sent, 0);
822 }
823 }
824
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100825 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100826 return 1;
827}
828
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200829/**
830 * batadv_handle_request - check for REQUEST frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100831 * @bat_priv: the bat priv with all the soft interface information
832 * @primary_if: the primary hard interface of this batman soft interface
833 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
834 * @ethhdr: ethernet header of a packet
835 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200836 *
837 * Return: 1 if handled
838 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200839static int batadv_handle_request(struct batadv_priv *bat_priv,
840 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200841 u8 *backbone_addr, struct ethhdr *ethhdr,
842 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100843{
844 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200845 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100846 return 0;
847
848 /* sanity check, this should not happen on a normal switch,
849 * we ignore it in this case.
850 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200851 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100852 return 1;
853
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200854 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200855 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200856 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100857
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200858 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100859 return 1;
860}
861
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200862/**
863 * batadv_handle_unclaim - check for UNCLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100864 * @bat_priv: the bat priv with all the soft interface information
865 * @primary_if: the primary hard interface of this batman soft interface
866 * @backbone_addr: originator address of the backbone (Ethernet source)
867 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
868 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200869 *
870 * Return: 1 if handled
871 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200872static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
873 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200874 u8 *backbone_addr, u8 *claim_addr,
875 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100876{
Marek Lindnerbae98772012-12-25 17:03:24 +0800877 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100878
879 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200880 if (primary_if && batadv_compare_eth(backbone_addr,
881 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200882 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200883 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100884
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200885 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100886
887 if (!backbone_gw)
888 return 1;
889
890 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200891 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200892 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200893 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100894
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200895 batadv_bla_del_claim(bat_priv, claim_addr, vid);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100896 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100897 return 1;
898}
899
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200900/**
901 * batadv_handle_claim - check for CLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100902 * @bat_priv: the bat priv with all the soft interface information
903 * @primary_if: the primary hard interface of this batman soft interface
904 * @backbone_addr: originator address of the backbone (Ethernet Source)
905 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
906 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200907 *
908 * Return: 1 if handled
909 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200910static int batadv_handle_claim(struct batadv_priv *bat_priv,
911 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200912 u8 *backbone_addr, u8 *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200913 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100914{
Marek Lindnerbae98772012-12-25 17:03:24 +0800915 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100916
917 /* register the gateway if not yet available, and add the claim. */
918
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200919 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
920 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100921
922 if (unlikely(!backbone_gw))
923 return 1;
924
925 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200926 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200927 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200928 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200929 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100930
931 /* TODO: we could call something like tt_local_del() here. */
932
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100933 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100934 return 1;
935}
936
Ben Hutchings2c530402012-07-10 10:55:09 +0000937/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100938 * batadv_check_claim_group - check for claim group membership
Ben Hutchings2c530402012-07-10 10:55:09 +0000939 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200940 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100941 * @hw_src: the Hardware source in the ARP Header
942 * @hw_dst: the Hardware destination in the ARP Header
943 * @ethhdr: pointer to the Ethernet header of the claim frame
944 *
945 * checks if it is a claim packet and if its on the same group.
946 * This function also applies the group ID of the sender
947 * if it is in the same mesh.
948 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200949 * Return:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100950 * 2 - if it is a claim packet and on the same group
951 * 1 - if is a claim packet from another group
952 * 0 - if it is not a claim packet
953 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200954static int batadv_check_claim_group(struct batadv_priv *bat_priv,
955 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200956 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200957 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100958{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200959 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200960 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200961 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100962
Sven Eckelmann96412692012-06-05 22:31:30 +0200963 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200964 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100965
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100966 /* if announcement packet, use the source,
967 * otherwise assume it is in the hw_src
968 */
969 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200970 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100971 backbone_addr = hw_src;
972 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200973 case BATADV_CLAIM_TYPE_REQUEST:
974 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200975 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100976 backbone_addr = ethhdr->h_source;
977 break;
978 default:
979 return 0;
980 }
981
982 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200983 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100984 return 0;
985
986 /* if its already the same group, it is fine. */
987 if (bla_dst->group == bla_dst_own->group)
988 return 2;
989
990 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200991 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100992
993 /* dont accept claims from gateways which are not in
994 * the same mesh or group.
995 */
996 if (!orig_node)
997 return 1;
998
999 /* if our mesh friends mac is bigger, use it for ourselves. */
1000 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001001 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001002 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001003 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001004 bla_dst_own->group = bla_dst->group;
1005 }
1006
Sven Eckelmann5d967312016-01-17 11:01:09 +01001007 batadv_orig_node_put(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001008
1009 return 2;
1010}
1011
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001012/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001013 * batadv_bla_process_claim - Check if this is a claim frame, and process it
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001014 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +02001015 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +01001016 * @skb: the frame to be checked
1017 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001018 * Return: 1 if it was a claim frame, otherwise return 0 to
Simon Wunderlich23721382012-01-22 20:00:19 +01001019 * tell the callee that it can use the frame on its own.
1020 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001021static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
1022 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001023 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +01001024{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001025 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001026 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001027 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001028 struct ethhdr *ethhdr;
1029 struct arphdr *arphdr;
1030 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001031 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +02001032 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +01001033 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001034 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001035
Antonio Quartullic018ad32013-06-04 12:11:39 +02001036 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001037 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001038
Antonio Quartullic018ad32013-06-04 12:11:39 +02001039 proto = ethhdr->h_proto;
1040 headlen = ETH_HLEN;
1041 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001042 /* Traverse the VLAN/Ethertypes.
1043 *
1044 * At this point it is known that the first protocol is a VLAN
1045 * header, so start checking at the encapsulated protocol.
1046 *
1047 * The depth of the VLAN headers is recorded to drop BLA claim
1048 * frames encapsulated into multiple VLAN headers (QinQ).
1049 */
1050 do {
1051 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1052 &vhdr_buf);
1053 if (!vhdr)
1054 return 0;
1055
1056 proto = vhdr->h_vlan_encapsulated_proto;
1057 headlen += VLAN_HLEN;
1058 vlan_depth++;
1059 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +01001060 }
1061
Antonio Quartulli293e9332013-05-19 12:55:16 +02001062 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +01001063 return 0; /* not a claim frame */
1064
1065 /* this must be a ARP frame. check if it is a claim. */
1066
1067 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1068 return 0;
1069
1070 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001071 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001072 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +01001073
1074 /* Check whether the ARP frame carries a valid
1075 * IP information
1076 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001077 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1078 return 0;
1079 if (arphdr->ar_pro != htons(ETH_P_IP))
1080 return 0;
1081 if (arphdr->ar_hln != ETH_ALEN)
1082 return 0;
1083 if (arphdr->ar_pln != 4)
1084 return 0;
1085
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001086 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001087 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +02001088 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001089 bla_dst_own = &bat_priv->bla.claim_dest;
1090
1091 /* check if it is a claim frame in general */
1092 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1093 sizeof(bla_dst->magic)) != 0)
1094 return 0;
1095
1096 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1097 * drop that, as this is not supported by BLA but should also not be
1098 * sent via the mesh.
1099 */
1100 if (vlan_depth > 1)
1101 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +01001102
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001103 /* Let the loopdetect frames on the mesh in any case. */
1104 if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
1105 return 0;
1106
Simon Wunderlich23721382012-01-22 20:00:19 +01001107 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001108 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1109 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001110 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001111 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001112 "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 +02001113 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1114 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001115
1116 if (ret < 2)
1117 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001118
1119 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001120 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001121
1122 /* check for the different types of claim frames ... */
1123 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001124 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001125 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1126 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001127 return 1;
1128 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001129 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001130 if (batadv_handle_unclaim(bat_priv, primary_if,
1131 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001132 return 1;
1133 break;
1134
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001135 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001136 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1137 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001138 return 1;
1139 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001140 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001141 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1142 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001143 return 1;
1144 break;
1145 }
1146
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001147 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001148 "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 +02001149 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +01001150 return 1;
1151}
1152
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001153/**
1154 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1155 * immediately
1156 * @bat_priv: the bat priv with all the soft interface information
1157 * @now: whether the whole hash shall be wiped now
1158 *
1159 * Check when we last heard from other nodes, and remove them in case of
Simon Wunderlich23721382012-01-22 20:00:19 +01001160 * a time out, or clean all backbone gws if now is set.
1161 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001162static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001163{
Marek Lindnerbae98772012-12-25 17:03:24 +08001164 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001165 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001166 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001167 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001168 spinlock_t *list_lock; /* protects write access to the hash lists */
1169 int i;
1170
Sven Eckelmann807736f2012-07-15 22:26:51 +02001171 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001172 if (!hash)
1173 return;
1174
1175 for (i = 0; i < hash->size; i++) {
1176 head = &hash->table[i];
1177 list_lock = &hash->list_locks[i];
1178
1179 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001180 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001181 head, hash_entry) {
1182 if (now)
1183 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001184 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001185 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001186 continue;
1187
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001188 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001189 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1190 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001191
1192purge_now:
1193 /* don't wait for the pending request anymore */
1194 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001195 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001196
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001197 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001198
Sasha Levinb67bfe02013-02-27 17:06:00 -08001199 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +01001200 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001201 }
1202 spin_unlock_bh(list_lock);
1203 }
1204}
1205
Ben Hutchings2c530402012-07-10 10:55:09 +00001206/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001207 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
Ben Hutchings2c530402012-07-10 10:55:09 +00001208 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001209 * @primary_if: the selected primary interface, may be NULL if now is set
1210 * @now: whether the whole hash shall be wiped now
1211 *
1212 * Check when we heard last time from our own claims, and remove them in case of
1213 * a time out, or clean all claims if now is set
1214 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001215static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1216 struct batadv_hard_iface *primary_if,
1217 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001218{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001219 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001220 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001221 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001222 int i;
1223
Sven Eckelmann807736f2012-07-15 22:26:51 +02001224 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001225 if (!hash)
1226 return;
1227
1228 for (i = 0; i < hash->size; i++) {
1229 head = &hash->table[i];
1230
1231 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001232 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001233 if (now)
1234 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001235 if (!batadv_compare_eth(claim->backbone_gw->orig,
1236 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001237 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001238 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001239 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001240 continue;
1241
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001242 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001243 "bla_purge_claims(): %pM, vid %d, time out\n",
1244 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001245
1246purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001247 batadv_handle_unclaim(bat_priv, primary_if,
1248 claim->backbone_gw->orig,
1249 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001250 }
1251 rcu_read_unlock();
1252 }
1253}
1254
Ben Hutchings2c530402012-07-10 10:55:09 +00001255/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001256 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1257 * originator address changes
Ben Hutchings2c530402012-07-10 10:55:09 +00001258 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001259 * @primary_if: the new selected primary_if
1260 * @oldif: the old primary interface, may be NULL
Simon Wunderlich23721382012-01-22 20:00:19 +01001261 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001262void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1263 struct batadv_hard_iface *primary_if,
1264 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001265{
Marek Lindnerbae98772012-12-25 17:03:24 +08001266 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001267 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001268 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001269 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001270 int i;
1271
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001272 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001273 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1274 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001275
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001276 /* purge everything when bridge loop avoidance is turned off */
1277 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1278 oldif = NULL;
1279
Simon Wunderlich23721382012-01-22 20:00:19 +01001280 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001281 batadv_bla_purge_claims(bat_priv, NULL, 1);
1282 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001283 return;
1284 }
1285
Sven Eckelmann807736f2012-07-15 22:26:51 +02001286 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001287 if (!hash)
1288 return;
1289
1290 for (i = 0; i < hash->size; i++) {
1291 head = &hash->table[i];
1292
1293 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001294 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001295 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001296 if (!batadv_compare_eth(backbone_gw->orig,
1297 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001298 continue;
1299
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001300 ether_addr_copy(backbone_gw->orig,
1301 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001302 /* send an announce frame so others will ask for our
1303 * claims and update their tables.
1304 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001305 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001306 }
1307 rcu_read_unlock();
1308 }
1309}
1310
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001311/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001312 * batadv_bla_send_loopdetect - send a loopdetect frame
1313 * @bat_priv: the bat priv with all the soft interface information
1314 * @backbone_gw: the backbone gateway for which a loop should be detected
1315 *
1316 * To detect loops that the bridge loop avoidance can't handle, send a loop
1317 * detection packet on the backbone. Unlike other BLA frames, this frame will
1318 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1319 * indicates that there is a loop.
1320 */
1321static void
1322batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
1323 struct batadv_bla_backbone_gw *backbone_gw)
1324{
1325 batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
1326 backbone_gw->vid);
1327 batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
1328 backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
1329}
1330
1331/**
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001332 * batadv_bla_status_update - purge bla interfaces if necessary
1333 * @net_dev: the soft interface net device
1334 */
1335void batadv_bla_status_update(struct net_device *net_dev)
1336{
1337 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1338 struct batadv_hard_iface *primary_if;
1339
1340 primary_if = batadv_primary_if_get_selected(bat_priv);
1341 if (!primary_if)
1342 return;
1343
1344 /* this function already purges everything when bla is disabled,
1345 * so just call that one.
1346 */
1347 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001348 batadv_hardif_put(primary_if);
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001349}
1350
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001351/**
1352 * batadv_bla_periodic_work - performs periodic bla work
1353 * @work: kernel work struct
1354 *
1355 * periodic work to do:
Simon Wunderlich23721382012-01-22 20:00:19 +01001356 * * purge structures when they are too old
1357 * * send announcements
1358 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001359static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001360{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001361 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001362 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001363 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001364 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001365 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001366 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001367 struct batadv_hard_iface *primary_if;
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001368 bool send_loopdetect = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001369 int i;
1370
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001371 delayed_work = to_delayed_work(work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001372 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1373 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001374 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001375 if (!primary_if)
1376 goto out;
1377
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001378 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1379 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001380
1381 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1382 goto out;
1383
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001384 if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
1385 /* set a new random mac address for the next bridge loop
1386 * detection frames. Set the locally administered bit to avoid
1387 * collisions with users mac addresses.
1388 */
1389 random_ether_addr(bat_priv->bla.loopdetect_addr);
1390 bat_priv->bla.loopdetect_addr[0] = 0xba;
1391 bat_priv->bla.loopdetect_addr[1] = 0xbe;
1392 bat_priv->bla.loopdetect_lasttime = jiffies;
1393 atomic_set(&bat_priv->bla.loopdetect_next,
1394 BATADV_BLA_LOOPDETECT_PERIODS);
1395
1396 /* mark for sending loop detect on all VLANs */
1397 send_loopdetect = true;
1398 }
1399
Sven Eckelmann807736f2012-07-15 22:26:51 +02001400 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001401 if (!hash)
1402 goto out;
1403
1404 for (i = 0; i < hash->size; i++) {
1405 head = &hash->table[i];
1406
1407 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001408 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001409 if (!batadv_compare_eth(backbone_gw->orig,
1410 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001411 continue;
1412
1413 backbone_gw->lasttime = jiffies;
1414
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001415 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001416 if (send_loopdetect)
1417 batadv_bla_send_loopdetect(bat_priv,
1418 backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001419
1420 /* request_sent is only set after creation to avoid
1421 * problems when we are not yet known as backbone gw
1422 * in the backbone.
1423 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001424 * We can reset this now after we waited some periods
1425 * to give bridge forward delays and bla group forming
1426 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001427 */
1428
1429 if (atomic_read(&backbone_gw->request_sent) == 0)
1430 continue;
1431
Simon Wunderlich28709872012-09-13 18:18:46 +02001432 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1433 continue;
1434
Simon Wunderlichd807f272012-09-09 22:27:57 +02001435 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1436 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001437 }
1438 rcu_read_unlock();
1439 }
1440out:
1441 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001442 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001443
Antonio Quartulli72414442012-12-25 13:14:37 +01001444 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1445 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001446}
1447
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001448/* The hash for claim and backbone hash receive the same key because they
1449 * are getting initialized by hash_new with the same key. Reinitializing
1450 * them with to different keys to allow nested locking without generating
1451 * lockdep warnings
1452 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001453static struct lock_class_key batadv_claim_hash_lock_class_key;
1454static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001455
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001456/**
1457 * batadv_bla_init - initialize all bla structures
1458 * @bat_priv: the bat priv with all the soft interface information
1459 *
1460 * Return: 0 on success, < 0 on error.
1461 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001462int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001463{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001464 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001465 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001466 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001467 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001468 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001469
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001470 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1471
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001472 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001473
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001474 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001475 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1476 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001477 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001478 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001479 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1480 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001481 batadv_hardif_put(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001482 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001483 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001484 }
1485
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001486 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001487 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001488 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001489 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1490 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001491
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001492 atomic_set(&bat_priv->bla.loopdetect_next,
1493 BATADV_BLA_LOOPDETECT_PERIODS);
1494
Sven Eckelmann807736f2012-07-15 22:26:51 +02001495 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001496 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001497
Sven Eckelmann807736f2012-07-15 22:26:51 +02001498 bat_priv->bla.claim_hash = batadv_hash_new(128);
1499 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001500
Sven Eckelmann807736f2012-07-15 22:26:51 +02001501 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001502 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001503
Sven Eckelmann807736f2012-07-15 22:26:51 +02001504 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001505 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001506 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001507 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001508
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001509 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001510
Antonio Quartulli72414442012-12-25 13:14:37 +01001511 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1512
1513 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1514 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001515 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001516}
1517
Ben Hutchings2c530402012-07-10 10:55:09 +00001518/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001519 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
Ben Hutchings2c530402012-07-10 10:55:09 +00001520 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001521 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001522 *
1523 * check if it is on our broadcast list. Another gateway might
1524 * have sent the same packet because it is connected to the same backbone,
1525 * so we have to remove this duplicate.
1526 *
1527 * This is performed by checking the CRC, which will tell us
1528 * with a good chance that it is the same packet. If it is furthermore
1529 * sent by another host, drop it. We allow equal packets from
1530 * the same host however as this might be intended.
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001531 *
1532 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001533 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001534int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001535 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001536{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001537 int i, curr, ret = 0;
1538 __be32 crc;
1539 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001540 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001541
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001542 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001543
1544 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001545 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001546
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001547 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1548
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001549 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001550 curr = (bat_priv->bla.bcast_duplist_curr + i);
1551 curr %= BATADV_DUPLIST_SIZE;
1552 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001553
1554 /* we can stop searching if the entry is too old ;
1555 * later entries will be even older
1556 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001557 if (batadv_has_timed_out(entry->entrytime,
1558 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001559 break;
1560
1561 if (entry->crc != crc)
1562 continue;
1563
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001564 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001565 continue;
1566
1567 /* this entry seems to match: same crc, not too old,
1568 * and from another gw. therefore return 1 to forbid it.
1569 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001570 ret = 1;
1571 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001572 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001573 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001574 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001575 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001576 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001577 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001578 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001579 entry->crc = crc;
1580 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001581 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001582 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001583
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001584out:
1585 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1586
1587 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001588}
1589
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001590/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001591 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1592 * the VLAN identified by vid.
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001593 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001594 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001595 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001596 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001597 * Return: true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001598 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001599bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001600 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001601{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001602 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001603 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001604 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001605 int i;
1606
1607 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001608 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001609
1610 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001611 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001612
1613 for (i = 0; i < hash->size; i++) {
1614 head = &hash->table[i];
1615
1616 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001617 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001618 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1619 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001620 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001621 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001622 }
1623 }
1624 rcu_read_unlock();
1625 }
1626
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001627 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001628}
1629
Ben Hutchings2c530402012-07-10 10:55:09 +00001630/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001631 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
Ben Hutchings2c530402012-07-10 10:55:09 +00001632 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001633 * @orig_node: the orig_node of the frame
1634 * @hdr_size: maximum length of the frame
1635 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001636 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
1637 * it returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001638 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001639int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001640 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001641{
Marek Lindnerbae98772012-12-25 17:03:24 +08001642 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001643 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001644
1645 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1646 return 0;
1647
1648 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001649 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001650 return 0;
1651
Antonio Quartullic018ad32013-06-04 12:11:39 +02001652 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001653
1654 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001655 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1656 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001657 if (!backbone_gw)
1658 return 0;
1659
Sven Eckelmannc8b86c12016-01-17 11:01:15 +01001660 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001661 return 1;
1662}
1663
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001664/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +01001665 * batadv_bla_free - free all bla structures
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001666 * @bat_priv: the bat priv with all the soft interface information
1667 *
1668 * for softinterface free or module unload
1669 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001670void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001671{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001672 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001673
Sven Eckelmann807736f2012-07-15 22:26:51 +02001674 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001675 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001676
Sven Eckelmann807736f2012-07-15 22:26:51 +02001677 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001678 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001679 batadv_hash_destroy(bat_priv->bla.claim_hash);
1680 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001681 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001682 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001683 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001684 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1685 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001686 }
1687 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001688 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001689}
1690
Ben Hutchings2c530402012-07-10 10:55:09 +00001691/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001692 * batadv_bla_loopdetect_check - check and handle a detected loop
1693 * @bat_priv: the bat priv with all the soft interface information
1694 * @skb: the packet to check
1695 * @primary_if: interface where the request came on
1696 * @vid: the VLAN ID of the frame
1697 *
1698 * Checks if this packet is a loop detect frame which has been sent by us,
1699 * throw an uevent and log the event if that is the case.
1700 *
1701 * Return: true if it is a loop detect frame which is to be dropped, false
1702 * otherwise.
1703 */
1704static bool
1705batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1706 struct batadv_hard_iface *primary_if,
1707 unsigned short vid)
1708{
1709 struct batadv_bla_backbone_gw *backbone_gw;
1710 struct ethhdr *ethhdr;
1711
1712 ethhdr = eth_hdr(skb);
1713
1714 /* Only check for the MAC address and skip more checks here for
1715 * performance reasons - this function is on the hotpath, after all.
1716 */
1717 if (!batadv_compare_eth(ethhdr->h_source,
1718 bat_priv->bla.loopdetect_addr))
1719 return false;
1720
1721 /* If the packet came too late, don't forward it on the mesh
1722 * but don't consider that as loop. It might be a coincidence.
1723 */
1724 if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
1725 BATADV_BLA_LOOPDETECT_TIMEOUT))
1726 return true;
1727
1728 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1729 primary_if->net_dev->dev_addr,
1730 vid, true);
1731 if (unlikely(!backbone_gw))
1732 return true;
1733
1734 queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1735 /* backbone_gw is unreferenced in the report work function function */
1736
1737 return true;
1738}
1739
1740/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001741 * batadv_bla_rx - check packets coming from the mesh.
Ben Hutchings2c530402012-07-10 10:55:09 +00001742 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001743 * @skb: the frame to be checked
1744 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001745 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001746 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001747 * batadv_bla_rx avoidance checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001748 * * we have to race for a claim
1749 * * if the frame is allowed on the LAN
1750 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001751 * in these cases, the skb is further handled by this function
1752 *
1753 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
Simon Wunderlich23721382012-01-22 20:00:19 +01001754 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001755 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001756int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1757 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001758{
1759 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001760 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001761 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001762 int ret;
1763
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001764 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001765
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001766 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001767 if (!primary_if)
1768 goto handled;
1769
1770 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1771 goto allow;
1772
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001773 if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1774 goto handled;
1775
Sven Eckelmann807736f2012-07-15 22:26:51 +02001776 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001777 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001778 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001779 goto handled;
1780
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001781 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001782 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001783 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001784
1785 if (!claim) {
1786 /* possible optimization: race for a claim */
1787 /* No claim exists yet, claim it for us!
1788 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001789 batadv_handle_claim(bat_priv, primary_if,
1790 primary_if->net_dev->dev_addr,
1791 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001792 goto allow;
1793 }
1794
1795 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001796 if (batadv_compare_eth(claim->backbone_gw->orig,
1797 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001798 /* ... allow it in any case */
1799 claim->lasttime = jiffies;
1800 goto allow;
1801 }
1802
1803 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001804 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1805 /* ... drop it. the responsible gateway is in charge.
1806 *
1807 * We need to check is_bcast because with the gateway
1808 * feature, broadcasts (like DHCP requests) may be sent
1809 * using a unicast packet type.
1810 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001811 goto handled;
1812 } else {
1813 /* seems the client considers us as its best gateway.
1814 * send a claim and update the claim table
1815 * immediately.
1816 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001817 batadv_handle_claim(bat_priv, primary_if,
1818 primary_if->net_dev->dev_addr,
1819 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001820 goto allow;
1821 }
1822allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001823 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001824 ret = 0;
1825 goto out;
1826
1827handled:
1828 kfree_skb(skb);
1829 ret = 1;
1830
1831out:
1832 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001833 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001834 if (claim)
Sven Eckelmann321e3e02016-01-17 11:01:16 +01001835 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001836 return ret;
1837}
1838
Ben Hutchings2c530402012-07-10 10:55:09 +00001839/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001840 * batadv_bla_tx - check packets going into the mesh
Ben Hutchings2c530402012-07-10 10:55:09 +00001841 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001842 * @skb: the frame to be checked
1843 * @vid: the VLAN ID of the frame
1844 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001845 * batadv_bla_tx checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001846 * * a claim was received which has to be processed
1847 * * the frame is allowed on the mesh
1848 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001849 * in these cases, the skb is further handled by this function.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001850 *
1851 * This call might reallocate skb data.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001852 *
1853 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1854 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001855 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001856int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1857 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001858{
1859 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001860 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001861 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001862 int ret = 0;
1863
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001864 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001865 if (!primary_if)
1866 goto out;
1867
1868 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1869 goto allow;
1870
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001871 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001872 goto handled;
1873
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001874 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001875
Sven Eckelmann807736f2012-07-15 22:26:51 +02001876 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001877 /* don't allow broadcasts while requests are in flight */
1878 if (is_multicast_ether_addr(ethhdr->h_dest))
1879 goto handled;
1880
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001881 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001882 search_claim.vid = vid;
1883
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001884 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001885
1886 /* if no claim exists, allow it. */
1887 if (!claim)
1888 goto allow;
1889
1890 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001891 if (batadv_compare_eth(claim->backbone_gw->orig,
1892 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001893 /* if yes, the client has roamed and we have
1894 * to unclaim it.
1895 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001896 batadv_handle_unclaim(bat_priv, primary_if,
1897 primary_if->net_dev->dev_addr,
1898 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001899 goto allow;
1900 }
1901
1902 /* check if it is a multicast/broadcast frame */
1903 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1904 /* drop it. the responsible gateway has forwarded it into
1905 * the backbone network.
1906 */
1907 goto handled;
1908 } else {
1909 /* we must allow it. at least if we are
1910 * responsible for the DESTINATION.
1911 */
1912 goto allow;
1913 }
1914allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001915 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001916 ret = 0;
1917 goto out;
1918handled:
1919 ret = 1;
1920out:
1921 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001922 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001923 if (claim)
Sven Eckelmann321e3e02016-01-17 11:01:16 +01001924 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001925 return ret;
1926}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001927
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001928/**
1929 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1930 * @seq: seq file to print on
1931 * @offset: not used
1932 *
1933 * Return: always 0
1934 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001935int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001936{
1937 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001938 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001939 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001940 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001941 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001942 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001943 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001944 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001945 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001946 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001947
Marek Lindner30da63a2012-08-03 17:15:46 +02001948 primary_if = batadv_seq_print_text_primary_if_get(seq);
1949 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001950 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001951
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001952 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001953 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001954 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001955 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001956 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001957 seq_puts(seq,
1958 " Client VID Originator [o] (CRC )\n");
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001959 for (i = 0; i < hash->size; i++) {
1960 head = &hash->table[i];
1961
1962 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001963 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001964 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1965 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001966
1967 spin_lock_bh(&claim->backbone_gw->crc_lock);
1968 backbone_crc = claim->backbone_gw->crc;
1969 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001970 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001971 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001972 claim->backbone_gw->orig,
1973 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001974 backbone_crc);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001975 }
1976 rcu_read_unlock();
1977 }
1978out:
1979 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001980 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001981 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001982}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001983
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001984/**
1985 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1986 * file
1987 * @seq: seq file to print on
1988 * @offset: not used
1989 *
1990 * Return: always 0
1991 */
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001992int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1993{
1994 struct net_device *net_dev = (struct net_device *)seq->private;
1995 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001996 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001997 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001998 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001999 struct hlist_head *head;
2000 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002001 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002002 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002003 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002004 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002005
Marek Lindner30da63a2012-08-03 17:15:46 +02002006 primary_if = batadv_seq_print_text_primary_if_get(seq);
2007 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002008 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002009
2010 primary_addr = primary_if->net_dev->dev_addr;
2011 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002012 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002013 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002014 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002015 seq_puts(seq, " Originator VID last seen (CRC )\n");
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002016 for (i = 0; i < hash->size; i++) {
2017 head = &hash->table[i];
2018
2019 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002020 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002021 msecs = jiffies_to_msecs(jiffies -
2022 backbone_gw->lasttime);
2023 secs = msecs / 1000;
2024 msecs = msecs % 1000;
2025
2026 is_own = batadv_compare_eth(backbone_gw->orig,
2027 primary_addr);
2028 if (is_own)
2029 continue;
2030
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002031 spin_lock_bh(&backbone_gw->crc_lock);
2032 backbone_crc = backbone_gw->crc;
2033 spin_unlock_bh(&backbone_gw->crc_lock);
2034
Antonio Quartullieb2deb62013-04-19 18:07:00 +02002035 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02002036 backbone_gw->orig,
2037 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002038 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002039 }
2040 rcu_read_unlock();
2041 }
2042out:
2043 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01002044 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02002045 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002046}