blob: 20b2fd9b3d72d3db36a941793ded7ff79d9797f7 [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"
53#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010054
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020055static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010056
Sven Eckelmann3b300de2012-05-12 18:33:53 +020057static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080058static void
59batadv_bla_send_announce(struct batadv_priv *bat_priv,
60 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010061
Sven Eckelmann62fe7102015-09-15 19:00:48 +020062/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010063 * batadv_choose_claim - choose the right bucket for a claim.
64 * @data: data to hash
65 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020066 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010067 * Return: the hash index of the claim
Sven Eckelmann62fe7102015-09-15 19:00:48 +020068 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020069static inline u32 batadv_choose_claim(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010070{
Marek Lindner712bbfe42012-12-25 17:03:25 +080071 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020072 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010073
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010074 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
75 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010076
77 return hash % size;
78}
79
Sven Eckelmann62fe7102015-09-15 19:00:48 +020080/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010081 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
82 * @data: data to hash
83 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020084 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010085 * Return: the hash index of the backbone gateway
Sven Eckelmann62fe7102015-09-15 19:00:48 +020086 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020087static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010088{
Marek Lindner712bbfe42012-12-25 17:03:25 +080089 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020090 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010091
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010092 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
93 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010094
95 return hash % size;
96}
97
Simon Wunderlich04e14be2015-11-06 10:45:19 +010098/**
99 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
100 * @node: list node of the first entry to compare
101 * @data2: pointer to the second backbone gateway
102 *
103 * Return: 1 if the backbones have the same data, 0 otherwise
104 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200105static int batadv_compare_backbone_gw(const struct hlist_node *node,
106 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100107{
Marek Lindnerbae98772012-12-25 17:03:24 +0800108 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +0100109 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200110 const struct batadv_bla_backbone_gw *gw1 = data1;
111 const struct batadv_bla_backbone_gw *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100112
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200113 if (!batadv_compare_eth(gw1->orig, gw2->orig))
114 return 0;
115
116 if (gw1->vid != gw2->vid)
117 return 0;
118
119 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100120}
121
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100122/**
123 * batadv_compare_backbone_gw - compare address and vid of two claims
124 * @node: list node of the first entry to compare
125 * @data2: pointer to the second claims
126 *
127 * Return: 1 if the claim have the same data, 0 otherwise
128 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200129static int batadv_compare_claim(const struct hlist_node *node,
130 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100131{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800132 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +0100133 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200134 const struct batadv_bla_claim *cl1 = data1;
135 const struct batadv_bla_claim *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100136
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200137 if (!batadv_compare_eth(cl1->addr, cl2->addr))
138 return 0;
139
140 if (cl1->vid != cl2->vid)
141 return 0;
142
143 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100144}
145
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100146/**
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100147 * batadv_backbone_gw_release - release backbone gw from lists and queue for
148 * free after rcu grace period
149 * @ref: kref pointer of the backbone gw
150 */
151static void batadv_backbone_gw_release(struct kref *ref)
152{
153 struct batadv_bla_backbone_gw *backbone_gw;
154
155 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
156 refcount);
157
158 kfree_rcu(backbone_gw, rcu);
159}
160
161/**
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100162 * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
163 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100164 * @backbone_gw: backbone gateway to be free'd
165 */
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100166static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100167{
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100168 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
Simon Wunderlich23721382012-01-22 20:00:19 +0100169}
170
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100171/**
172 * batadv_claim_release - release claim from lists and queue for free after rcu
173 * grace period
174 * @ref: kref pointer of the claim
175 */
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100176static void batadv_claim_release(struct kref *ref)
Simon Wunderlich23721382012-01-22 20:00:19 +0100177{
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100178 struct batadv_bla_claim *claim;
179
180 claim = container_of(ref, struct batadv_bla_claim, refcount);
181
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100182 batadv_backbone_gw_put(claim->backbone_gw);
Sven Eckelmann63b39922016-01-14 15:28:19 +0100183 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100184}
185
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100186/**
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100187 * batadv_claim_put - decrement the claim refcounter and possibly
Sven Eckelmannec9b83c2016-01-05 12:06:16 +0100188 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100189 * @claim: claim to be free'd
190 */
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100191static void batadv_claim_put(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100192{
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100193 kref_put(&claim->refcount, batadv_claim_release);
Simon Wunderlich23721382012-01-22 20:00:19 +0100194}
195
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100196/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100197 * batadv_claim_hash_find - looks for a claim in the claim hash
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100198 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100199 * @data: search data (may be local/static data)
200 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200201 * Return: claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100202 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800203static struct batadv_bla_claim
204*batadv_claim_hash_find(struct batadv_priv *bat_priv,
205 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100206{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200207 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100208 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800209 struct batadv_bla_claim *claim;
210 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100211 int index;
212
213 if (!hash)
214 return NULL;
215
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200216 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100217 head = &hash->table[index];
218
219 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800220 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200221 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100222 continue;
223
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100224 if (!kref_get_unless_zero(&claim->refcount))
Simon Wunderlich23721382012-01-22 20:00:19 +0100225 continue;
226
227 claim_tmp = claim;
228 break;
229 }
230 rcu_read_unlock();
231
232 return claim_tmp;
233}
234
Ben Hutchings2c530402012-07-10 10:55:09 +0000235/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100236 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
Ben Hutchings2c530402012-07-10 10:55:09 +0000237 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100238 * @addr: the address of the originator
239 * @vid: the VLAN ID
240 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100241 * Return: backbone gateway if found or NULL otherwise
Simon Wunderlich23721382012-01-22 20:00:19 +0100242 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800243static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200244batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
245 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100246{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200247 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100248 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800249 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
250 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100251 int index;
252
253 if (!hash)
254 return NULL;
255
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100256 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100257 search_entry.vid = vid;
258
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200259 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100260 head = &hash->table[index];
261
262 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800263 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200264 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
265 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100266 continue;
267
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100268 if (!kref_get_unless_zero(&backbone_gw->refcount))
Simon Wunderlich23721382012-01-22 20:00:19 +0100269 continue;
270
271 backbone_gw_tmp = backbone_gw;
272 break;
273 }
274 rcu_read_unlock();
275
276 return backbone_gw_tmp;
277}
278
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100279/**
280 * batadv_bla_del_backbone_claims - delete all claims for a backbone
281 * @backbone_gw: backbone gateway where the claims should be removed
282 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200283static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800284batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100285{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200286 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800287 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100288 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800289 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100290 int i;
291 spinlock_t *list_lock; /* protects write access to the hash lists */
292
Sven Eckelmann807736f2012-07-15 22:26:51 +0200293 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100294 if (!hash)
295 return;
296
297 for (i = 0; i < hash->size; i++) {
298 head = &hash->table[i];
299 list_lock = &hash->list_locks[i];
300
301 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800302 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100303 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100304 if (claim->backbone_gw != backbone_gw)
305 continue;
306
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100307 batadv_claim_put(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800308 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100309 }
310 spin_unlock_bh(list_lock);
311 }
312
Antonio Quartulli3f687852014-11-02 11:29:56 +0100313 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200314 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200315 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200316 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100317}
318
Ben Hutchings2c530402012-07-10 10:55:09 +0000319/**
320 * batadv_bla_send_claim - sends a claim frame according to the provided info
321 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200322 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100323 * @vid: the VLAN ID
324 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100325 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200326static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200327 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100328{
329 struct sk_buff *skb;
330 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200331 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100332 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200333 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200334 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100335 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100336
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200337 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100338 if (!primary_if)
339 return;
340
Sven Eckelmann807736f2012-07-15 22:26:51 +0200341 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100342 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100343 local_claim_dest.type = claimtype;
344
345 soft_iface = primary_if->soft_iface;
346
347 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
348 /* IP DST: 0.0.0.0 */
349 zeroip,
350 primary_if->soft_iface,
351 /* IP SRC: 0.0.0.0 */
352 zeroip,
353 /* Ethernet DST: Broadcast */
354 NULL,
355 /* Ethernet SRC/HW SRC: originator mac */
356 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200357 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100358 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100359 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100360 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200361 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100362
363 if (!skb)
364 goto out;
365
366 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200367 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100368
369 /* now we pretend that the client would have sent this ... */
370 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200371 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100372 /* normal claim frame
373 * set Ethernet SRC to the clients mac
374 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100375 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200376 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200377 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
378 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100379 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200380 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100381 /* unclaim frame
382 * set HW SRC to the clients mac
383 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100384 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200385 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200386 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200387 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100388 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200389 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100390 /* announcement frame
391 * set HW SRC to the special mac containg the crc
392 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100393 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200394 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200395 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200396 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100397 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200398 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100399 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200400 * set HW SRC and header destination to the receiving backbone
401 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100402 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100403 ether_addr_copy(hw_src, mac);
404 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200405 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200406 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200407 ethhdr->h_source, ethhdr->h_dest,
408 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100409 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100410 }
411
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200412 if (vid & BATADV_VLAN_HAS_TAG)
413 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
414 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100415
416 skb_reset_mac_header(skb);
417 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200418 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
419 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
420 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100421 soft_iface->last_rx = jiffies;
422
423 netif_rx(skb);
424out:
425 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100426 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100427}
428
Ben Hutchings2c530402012-07-10 10:55:09 +0000429/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100430 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
Ben Hutchings2c530402012-07-10 10:55:09 +0000431 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100432 * @orig: the mac address of the originator
433 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200434 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100435 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100436 * Return: the (possibly created) backbone gateway or NULL on error
Simon Wunderlich23721382012-01-22 20:00:19 +0100437 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800438static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200439batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200440 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100441{
Marek Lindnerbae98772012-12-25 17:03:24 +0800442 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200443 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100444 int hash_added;
445
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200446 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100447
448 if (entry)
449 return entry;
450
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200451 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200452 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200453 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100454
455 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
456 if (!entry)
457 return NULL;
458
459 entry->vid = vid;
460 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200461 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100462 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200463 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100464 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200465 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100466 ether_addr_copy(entry->orig, orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100467
468 /* one for the hash, one for returning */
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100469 kref_init(&entry->refcount);
470 kref_get(&entry->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100471
Sven Eckelmann807736f2012-07-15 22:26:51 +0200472 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200473 batadv_compare_backbone_gw,
474 batadv_choose_backbone_gw, entry,
475 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100476
477 if (unlikely(hash_added != 0)) {
478 /* hash failed, free the structure */
479 kfree(entry);
480 return NULL;
481 }
482
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200483 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200484 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100485 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200486 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200487 "became a backbone gateway");
Sven Eckelmann5d967312016-01-17 11:01:09 +0100488 batadv_orig_node_put(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100489 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200490
Simon Wunderlichd807f272012-09-09 22:27:57 +0200491 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200492 batadv_bla_send_announce(bat_priv, entry);
493
Simon Wunderlichd807f272012-09-09 22:27:57 +0200494 /* this will be decreased in the worker thread */
495 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200496 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200497 atomic_inc(&bat_priv->bla.num_requests);
498 }
499
Simon Wunderlich23721382012-01-22 20:00:19 +0100500 return entry;
501}
502
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100503/**
504 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
505 * @bat_priv: the bat priv with all the soft interface information
506 * @primary_if: the selected primary interface
507 * @vid: VLAN identifier
508 *
509 * update or add the own backbone gw to make sure we announce
Simon Wunderlich23721382012-01-22 20:00:19 +0100510 * where we receive other backbone gws
511 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200512static void
513batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
514 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200515 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100516{
Marek Lindnerbae98772012-12-25 17:03:24 +0800517 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100518
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200519 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
520 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200521 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100522 if (unlikely(!backbone_gw))
523 return;
524
525 backbone_gw->lasttime = jiffies;
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100526 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100527}
528
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100529/**
530 * batadv_bla_answer_request - answer a bla request by sending own claims
531 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200532 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100533 * @vid: the vid where the request came on
534 *
535 * Repeat all of our own claims, and finally send an ANNOUNCE frame
536 * to allow the requester another check if the CRC is correct now.
537 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200538static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
539 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200540 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100541{
Simon Wunderlich23721382012-01-22 20:00:19 +0100542 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200543 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800544 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800545 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100546 int i;
547
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200548 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200549 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100550
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200551 backbone_gw = batadv_backbone_hash_find(bat_priv,
552 primary_if->net_dev->dev_addr,
553 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100554 if (!backbone_gw)
555 return;
556
Sven Eckelmann807736f2012-07-15 22:26:51 +0200557 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100558 for (i = 0; i < hash->size; i++) {
559 head = &hash->table[i];
560
561 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800562 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100563 /* only own claims are interesting */
564 if (claim->backbone_gw != backbone_gw)
565 continue;
566
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200567 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200568 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100569 }
570 rcu_read_unlock();
571 }
572
573 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200574 batadv_bla_send_announce(bat_priv, backbone_gw);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100575 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100576}
577
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100578/**
579 * batadv_bla_send_request - send a request to repeat claims
580 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100581 *
582 * When the crc is wrong, ask the backbone gateway for a full table update.
583 * After the request, it will repeat all of his own claims and finally
584 * send an announcement claim with which we can check again.
585 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800586static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100587{
588 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200589 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100590
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200591 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
592 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100593
594 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200595 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200596 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100597
598 /* no local broadcasts should be sent or received, for now. */
599 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200600 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100601 atomic_set(&backbone_gw->request_sent, 1);
602 }
603}
604
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100605/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100606 * batadv_bla_send_announce - Send an announcement frame
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100607 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100608 * @backbone_gw: our backbone gateway which should be announced
Simon Wunderlich23721382012-01-22 20:00:19 +0100609 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200610static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800611 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100612{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200613 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100614 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100615
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200616 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200617 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100618 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200619 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100620 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100621
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200622 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200623 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100624}
625
Ben Hutchings2c530402012-07-10 10:55:09 +0000626/**
627 * batadv_bla_add_claim - Adds a claim in the claim hash
628 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100629 * @mac: the mac address of the claim
630 * @vid: the VLAN ID of the frame
631 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100632 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200633static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200634 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800635 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100636{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800637 struct batadv_bla_claim *claim;
638 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100639 int hash_added;
640
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100641 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100642 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200643 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100644
645 /* create a new claim entry if it does not exist yet. */
646 if (!claim) {
647 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
648 if (!claim)
649 return;
650
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100651 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100652 claim->vid = vid;
653 claim->lasttime = jiffies;
654 claim->backbone_gw = backbone_gw;
655
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100656 kref_init(&claim->refcount);
657 kref_get(&claim->refcount);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200658 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200659 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200660 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200661 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200662 batadv_compare_claim,
663 batadv_choose_claim, claim,
664 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100665
666 if (unlikely(hash_added != 0)) {
667 /* only local changes happened. */
668 kfree(claim);
669 return;
670 }
671 } else {
672 claim->lasttime = jiffies;
673 if (claim->backbone_gw == backbone_gw)
674 /* no need to register a new backbone */
675 goto claim_free_ref;
676
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200677 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200678 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200679 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100680
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200681 spin_lock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200682 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200683 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100684 batadv_backbone_gw_put(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100685 }
686 /* set (new) backbone gw */
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100687 kref_get(&backbone_gw->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100688 claim->backbone_gw = backbone_gw;
689
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200690 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100691 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200692 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100693 backbone_gw->lasttime = jiffies;
694
695claim_free_ref:
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100696 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100697}
698
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100699/**
700 * batadv_bla_del_claim - delete a claim from the claim hash
701 * @bat_priv: the bat priv with all the soft interface information
702 * @mac: mac address of the claim to be removed
703 * @vid: VLAN id for the claim to be removed
Simon Wunderlich23721382012-01-22 20:00:19 +0100704 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200705static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200706 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100707{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800708 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100709
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100710 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100711 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200712 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100713 if (!claim)
714 return;
715
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200716 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200717 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100718
Sven Eckelmann807736f2012-07-15 22:26:51 +0200719 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200720 batadv_choose_claim, claim);
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100721 batadv_claim_put(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100722
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200723 spin_lock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100724 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200725 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100726
727 /* don't need the reference from hash_find() anymore */
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100728 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100729}
730
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200731/**
732 * batadv_handle_announce - check for ANNOUNCE frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100733 * @bat_priv: the bat priv with all the soft interface information
734 * @an_addr: announcement mac address (ARP Sender HW address)
735 * @backbone_addr: originator address of the sender (Ethernet source MAC)
736 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200737 *
738 * Return: 1 if handled
739 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200740static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
741 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100742{
Marek Lindnerbae98772012-12-25 17:03:24 +0800743 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200744 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100745
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200746 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100747 return 0;
748
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200749 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
750 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100751
752 if (unlikely(!backbone_gw))
753 return 1;
754
Simon Wunderlich23721382012-01-22 20:00:19 +0100755 /* handle as ANNOUNCE frame */
756 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100757 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100758
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200759 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100760 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200761 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100762
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200763 spin_lock_bh(&backbone_gw->crc_lock);
764 backbone_crc = backbone_gw->crc;
765 spin_unlock_bh(&backbone_gw->crc_lock);
766
767 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200768 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100769 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200770 backbone_gw->orig,
771 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200772 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100773
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200774 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100775 } else {
776 /* if we have sent a request and the crc was OK,
777 * we can allow traffic again.
778 */
779 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200780 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100781 atomic_set(&backbone_gw->request_sent, 0);
782 }
783 }
784
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100785 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100786 return 1;
787}
788
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200789/**
790 * batadv_handle_request - check for REQUEST frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100791 * @bat_priv: the bat priv with all the soft interface information
792 * @primary_if: the primary hard interface of this batman soft interface
793 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
794 * @ethhdr: ethernet header of a packet
795 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200796 *
797 * Return: 1 if handled
798 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200799static int batadv_handle_request(struct batadv_priv *bat_priv,
800 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200801 u8 *backbone_addr, struct ethhdr *ethhdr,
802 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100803{
804 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200805 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100806 return 0;
807
808 /* sanity check, this should not happen on a normal switch,
809 * we ignore it in this case.
810 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200811 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100812 return 1;
813
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200814 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200815 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200816 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100817
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200818 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100819 return 1;
820}
821
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200822/**
823 * batadv_handle_unclaim - check for UNCLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100824 * @bat_priv: the bat priv with all the soft interface information
825 * @primary_if: the primary hard interface of this batman soft interface
826 * @backbone_addr: originator address of the backbone (Ethernet source)
827 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
828 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200829 *
830 * Return: 1 if handled
831 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200832static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
833 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200834 u8 *backbone_addr, u8 *claim_addr,
835 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100836{
Marek Lindnerbae98772012-12-25 17:03:24 +0800837 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100838
839 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200840 if (primary_if && batadv_compare_eth(backbone_addr,
841 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200842 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200843 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100844
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200845 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100846
847 if (!backbone_gw)
848 return 1;
849
850 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200851 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200852 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200853 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100854
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200855 batadv_bla_del_claim(bat_priv, claim_addr, vid);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100856 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100857 return 1;
858}
859
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200860/**
861 * batadv_handle_claim - check for CLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100862 * @bat_priv: the bat priv with all the soft interface information
863 * @primary_if: the primary hard interface of this batman soft interface
864 * @backbone_addr: originator address of the backbone (Ethernet Source)
865 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
866 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200867 *
868 * Return: 1 if handled
869 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200870static int batadv_handle_claim(struct batadv_priv *bat_priv,
871 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200872 u8 *backbone_addr, u8 *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200873 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100874{
Marek Lindnerbae98772012-12-25 17:03:24 +0800875 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100876
877 /* register the gateway if not yet available, and add the claim. */
878
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200879 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
880 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100881
882 if (unlikely(!backbone_gw))
883 return 1;
884
885 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200886 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200887 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200888 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200889 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100890
891 /* TODO: we could call something like tt_local_del() here. */
892
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100893 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100894 return 1;
895}
896
Ben Hutchings2c530402012-07-10 10:55:09 +0000897/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100898 * batadv_check_claim_group - check for claim group membership
Ben Hutchings2c530402012-07-10 10:55:09 +0000899 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200900 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100901 * @hw_src: the Hardware source in the ARP Header
902 * @hw_dst: the Hardware destination in the ARP Header
903 * @ethhdr: pointer to the Ethernet header of the claim frame
904 *
905 * checks if it is a claim packet and if its on the same group.
906 * This function also applies the group ID of the sender
907 * if it is in the same mesh.
908 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200909 * Return:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100910 * 2 - if it is a claim packet and on the same group
911 * 1 - if is a claim packet from another group
912 * 0 - if it is not a claim packet
913 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200914static int batadv_check_claim_group(struct batadv_priv *bat_priv,
915 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200916 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200917 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100918{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200919 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200920 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200921 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100922
Sven Eckelmann96412692012-06-05 22:31:30 +0200923 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200924 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100925
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100926 /* if announcement packet, use the source,
927 * otherwise assume it is in the hw_src
928 */
929 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200930 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100931 backbone_addr = hw_src;
932 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200933 case BATADV_CLAIM_TYPE_REQUEST:
934 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200935 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100936 backbone_addr = ethhdr->h_source;
937 break;
938 default:
939 return 0;
940 }
941
942 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200943 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100944 return 0;
945
946 /* if its already the same group, it is fine. */
947 if (bla_dst->group == bla_dst_own->group)
948 return 2;
949
950 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200951 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100952
953 /* dont accept claims from gateways which are not in
954 * the same mesh or group.
955 */
956 if (!orig_node)
957 return 1;
958
959 /* if our mesh friends mac is bigger, use it for ourselves. */
960 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200961 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100962 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200963 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100964 bla_dst_own->group = bla_dst->group;
965 }
966
Sven Eckelmann5d967312016-01-17 11:01:09 +0100967 batadv_orig_node_put(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100968
969 return 2;
970}
971
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100972/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100973 * batadv_bla_process_claim - Check if this is a claim frame, and process it
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100974 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200975 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +0100976 * @skb: the frame to be checked
977 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200978 * Return: 1 if it was a claim frame, otherwise return 0 to
Simon Wunderlich23721382012-01-22 20:00:19 +0100979 * tell the callee that it can use the frame on its own.
980 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200981static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
982 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200983 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100984{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200985 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200986 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200987 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200988 struct ethhdr *ethhdr;
989 struct arphdr *arphdr;
990 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200991 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200992 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +0100993 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100994 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100995
Antonio Quartullic018ad32013-06-04 12:11:39 +0200996 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200997 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100998
Antonio Quartullic018ad32013-06-04 12:11:39 +0200999 proto = ethhdr->h_proto;
1000 headlen = ETH_HLEN;
1001 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001002 /* Traverse the VLAN/Ethertypes.
1003 *
1004 * At this point it is known that the first protocol is a VLAN
1005 * header, so start checking at the encapsulated protocol.
1006 *
1007 * The depth of the VLAN headers is recorded to drop BLA claim
1008 * frames encapsulated into multiple VLAN headers (QinQ).
1009 */
1010 do {
1011 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1012 &vhdr_buf);
1013 if (!vhdr)
1014 return 0;
1015
1016 proto = vhdr->h_vlan_encapsulated_proto;
1017 headlen += VLAN_HLEN;
1018 vlan_depth++;
1019 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +01001020 }
1021
Antonio Quartulli293e9332013-05-19 12:55:16 +02001022 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +01001023 return 0; /* not a claim frame */
1024
1025 /* this must be a ARP frame. check if it is a claim. */
1026
1027 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1028 return 0;
1029
1030 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001031 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001032 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +01001033
1034 /* Check whether the ARP frame carries a valid
1035 * IP information
1036 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001037 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1038 return 0;
1039 if (arphdr->ar_pro != htons(ETH_P_IP))
1040 return 0;
1041 if (arphdr->ar_hln != ETH_ALEN)
1042 return 0;
1043 if (arphdr->ar_pln != 4)
1044 return 0;
1045
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001046 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001047 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +02001048 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001049 bla_dst_own = &bat_priv->bla.claim_dest;
1050
1051 /* check if it is a claim frame in general */
1052 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1053 sizeof(bla_dst->magic)) != 0)
1054 return 0;
1055
1056 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1057 * drop that, as this is not supported by BLA but should also not be
1058 * sent via the mesh.
1059 */
1060 if (vlan_depth > 1)
1061 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +01001062
1063 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001064 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1065 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001066 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001067 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001068 "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 +02001069 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1070 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001071
1072 if (ret < 2)
1073 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001074
1075 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001076 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001077
1078 /* check for the different types of claim frames ... */
1079 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001080 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001081 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1082 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001083 return 1;
1084 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001085 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001086 if (batadv_handle_unclaim(bat_priv, primary_if,
1087 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001088 return 1;
1089 break;
1090
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001091 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001092 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1093 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001094 return 1;
1095 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001096 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001097 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1098 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001099 return 1;
1100 break;
1101 }
1102
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001103 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001104 "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 +02001105 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +01001106 return 1;
1107}
1108
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001109/**
1110 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1111 * immediately
1112 * @bat_priv: the bat priv with all the soft interface information
1113 * @now: whether the whole hash shall be wiped now
1114 *
1115 * Check when we last heard from other nodes, and remove them in case of
Simon Wunderlich23721382012-01-22 20:00:19 +01001116 * a time out, or clean all backbone gws if now is set.
1117 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001118static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001119{
Marek Lindnerbae98772012-12-25 17:03:24 +08001120 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001121 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001122 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001123 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001124 spinlock_t *list_lock; /* protects write access to the hash lists */
1125 int i;
1126
Sven Eckelmann807736f2012-07-15 22:26:51 +02001127 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001128 if (!hash)
1129 return;
1130
1131 for (i = 0; i < hash->size; i++) {
1132 head = &hash->table[i];
1133 list_lock = &hash->list_locks[i];
1134
1135 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001136 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001137 head, hash_entry) {
1138 if (now)
1139 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001140 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001141 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001142 continue;
1143
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001144 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001145 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1146 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001147
1148purge_now:
1149 /* don't wait for the pending request anymore */
1150 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001151 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001152
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001153 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001154
Sasha Levinb67bfe02013-02-27 17:06:00 -08001155 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +01001156 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001157 }
1158 spin_unlock_bh(list_lock);
1159 }
1160}
1161
Ben Hutchings2c530402012-07-10 10:55:09 +00001162/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001163 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
Ben Hutchings2c530402012-07-10 10:55:09 +00001164 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001165 * @primary_if: the selected primary interface, may be NULL if now is set
1166 * @now: whether the whole hash shall be wiped now
1167 *
1168 * Check when we heard last time from our own claims, and remove them in case of
1169 * a time out, or clean all claims if now is set
1170 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001171static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1172 struct batadv_hard_iface *primary_if,
1173 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001174{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001175 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001176 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001177 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001178 int i;
1179
Sven Eckelmann807736f2012-07-15 22:26:51 +02001180 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001181 if (!hash)
1182 return;
1183
1184 for (i = 0; i < hash->size; i++) {
1185 head = &hash->table[i];
1186
1187 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001188 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001189 if (now)
1190 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001191 if (!batadv_compare_eth(claim->backbone_gw->orig,
1192 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001193 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001194 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001195 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001196 continue;
1197
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001198 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001199 "bla_purge_claims(): %pM, vid %d, time out\n",
1200 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001201
1202purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001203 batadv_handle_unclaim(bat_priv, primary_if,
1204 claim->backbone_gw->orig,
1205 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001206 }
1207 rcu_read_unlock();
1208 }
1209}
1210
Ben Hutchings2c530402012-07-10 10:55:09 +00001211/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001212 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1213 * originator address changes
Ben Hutchings2c530402012-07-10 10:55:09 +00001214 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001215 * @primary_if: the new selected primary_if
1216 * @oldif: the old primary interface, may be NULL
Simon Wunderlich23721382012-01-22 20:00:19 +01001217 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001218void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1219 struct batadv_hard_iface *primary_if,
1220 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001221{
Marek Lindnerbae98772012-12-25 17:03:24 +08001222 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001223 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001224 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001225 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001226 int i;
1227
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001228 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001229 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1230 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001231
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001232 /* purge everything when bridge loop avoidance is turned off */
1233 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1234 oldif = NULL;
1235
Simon Wunderlich23721382012-01-22 20:00:19 +01001236 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001237 batadv_bla_purge_claims(bat_priv, NULL, 1);
1238 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001239 return;
1240 }
1241
Sven Eckelmann807736f2012-07-15 22:26:51 +02001242 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001243 if (!hash)
1244 return;
1245
1246 for (i = 0; i < hash->size; i++) {
1247 head = &hash->table[i];
1248
1249 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001250 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001251 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001252 if (!batadv_compare_eth(backbone_gw->orig,
1253 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001254 continue;
1255
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001256 ether_addr_copy(backbone_gw->orig,
1257 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001258 /* send an announce frame so others will ask for our
1259 * claims and update their tables.
1260 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001261 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001262 }
1263 rcu_read_unlock();
1264 }
1265}
1266
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001267/**
1268 * batadv_bla_status_update - purge bla interfaces if necessary
1269 * @net_dev: the soft interface net device
1270 */
1271void batadv_bla_status_update(struct net_device *net_dev)
1272{
1273 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1274 struct batadv_hard_iface *primary_if;
1275
1276 primary_if = batadv_primary_if_get_selected(bat_priv);
1277 if (!primary_if)
1278 return;
1279
1280 /* this function already purges everything when bla is disabled,
1281 * so just call that one.
1282 */
1283 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001284 batadv_hardif_put(primary_if);
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001285}
1286
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001287/**
1288 * batadv_bla_periodic_work - performs periodic bla work
1289 * @work: kernel work struct
1290 *
1291 * periodic work to do:
Simon Wunderlich23721382012-01-22 20:00:19 +01001292 * * purge structures when they are too old
1293 * * send announcements
1294 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001295static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001296{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001297 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001298 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001299 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001300 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001301 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001302 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001303 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001304 int i;
1305
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001306 delayed_work = to_delayed_work(work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001307 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1308 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001309 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001310 if (!primary_if)
1311 goto out;
1312
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001313 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1314 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001315
1316 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1317 goto out;
1318
Sven Eckelmann807736f2012-07-15 22:26:51 +02001319 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001320 if (!hash)
1321 goto out;
1322
1323 for (i = 0; i < hash->size; i++) {
1324 head = &hash->table[i];
1325
1326 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001327 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001328 if (!batadv_compare_eth(backbone_gw->orig,
1329 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001330 continue;
1331
1332 backbone_gw->lasttime = jiffies;
1333
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001334 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001335
1336 /* request_sent is only set after creation to avoid
1337 * problems when we are not yet known as backbone gw
1338 * in the backbone.
1339 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001340 * We can reset this now after we waited some periods
1341 * to give bridge forward delays and bla group forming
1342 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001343 */
1344
1345 if (atomic_read(&backbone_gw->request_sent) == 0)
1346 continue;
1347
Simon Wunderlich28709872012-09-13 18:18:46 +02001348 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1349 continue;
1350
Simon Wunderlichd807f272012-09-09 22:27:57 +02001351 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1352 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001353 }
1354 rcu_read_unlock();
1355 }
1356out:
1357 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001358 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001359
Antonio Quartulli72414442012-12-25 13:14:37 +01001360 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1361 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001362}
1363
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001364/* The hash for claim and backbone hash receive the same key because they
1365 * are getting initialized by hash_new with the same key. Reinitializing
1366 * them with to different keys to allow nested locking without generating
1367 * lockdep warnings
1368 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001369static struct lock_class_key batadv_claim_hash_lock_class_key;
1370static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001371
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001372/**
1373 * batadv_bla_init - initialize all bla structures
1374 * @bat_priv: the bat priv with all the soft interface information
1375 *
1376 * Return: 0 on success, < 0 on error.
1377 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001378int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001379{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001380 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001381 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001382 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001383 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001384 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001385
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001386 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1387
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001388 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001389
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001390 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001391 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1392 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001393 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001394 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001395 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1396 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001397 batadv_hardif_put(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001398 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001399 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001400 }
1401
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001402 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001403 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001404 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001405 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1406 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001407
Sven Eckelmann807736f2012-07-15 22:26:51 +02001408 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001409 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001410
Sven Eckelmann807736f2012-07-15 22:26:51 +02001411 bat_priv->bla.claim_hash = batadv_hash_new(128);
1412 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001413
Sven Eckelmann807736f2012-07-15 22:26:51 +02001414 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001415 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001416
Sven Eckelmann807736f2012-07-15 22:26:51 +02001417 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001418 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001419 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001420 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001421
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001422 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001423
Antonio Quartulli72414442012-12-25 13:14:37 +01001424 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1425
1426 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1427 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001428 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001429}
1430
Ben Hutchings2c530402012-07-10 10:55:09 +00001431/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001432 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
Ben Hutchings2c530402012-07-10 10:55:09 +00001433 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001434 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001435 *
1436 * check if it is on our broadcast list. Another gateway might
1437 * have sent the same packet because it is connected to the same backbone,
1438 * so we have to remove this duplicate.
1439 *
1440 * This is performed by checking the CRC, which will tell us
1441 * with a good chance that it is the same packet. If it is furthermore
1442 * sent by another host, drop it. We allow equal packets from
1443 * the same host however as this might be intended.
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001444 *
1445 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001446 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001447int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001448 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001449{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001450 int i, curr, ret = 0;
1451 __be32 crc;
1452 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001453 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001454
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001455 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001456
1457 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001458 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001459
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001460 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1461
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001462 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001463 curr = (bat_priv->bla.bcast_duplist_curr + i);
1464 curr %= BATADV_DUPLIST_SIZE;
1465 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001466
1467 /* we can stop searching if the entry is too old ;
1468 * later entries will be even older
1469 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001470 if (batadv_has_timed_out(entry->entrytime,
1471 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001472 break;
1473
1474 if (entry->crc != crc)
1475 continue;
1476
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001477 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001478 continue;
1479
1480 /* this entry seems to match: same crc, not too old,
1481 * and from another gw. therefore return 1 to forbid it.
1482 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001483 ret = 1;
1484 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001485 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001486 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001487 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001488 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001489 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001490 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001491 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001492 entry->crc = crc;
1493 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001494 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001495 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001496
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001497out:
1498 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1499
1500 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001501}
1502
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001503/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001504 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1505 * the VLAN identified by vid.
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001506 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001507 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001508 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001509 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001510 * Return: true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001511 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001512bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001513 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001514{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001515 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001516 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001517 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001518 int i;
1519
1520 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001521 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001522
1523 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001524 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001525
1526 for (i = 0; i < hash->size; i++) {
1527 head = &hash->table[i];
1528
1529 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001530 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001531 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1532 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001533 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001534 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001535 }
1536 }
1537 rcu_read_unlock();
1538 }
1539
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001540 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001541}
1542
Ben Hutchings2c530402012-07-10 10:55:09 +00001543/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001544 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
Ben Hutchings2c530402012-07-10 10:55:09 +00001545 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001546 * @orig_node: the orig_node of the frame
1547 * @hdr_size: maximum length of the frame
1548 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001549 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
1550 * it returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001551 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001552int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001553 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001554{
Marek Lindnerbae98772012-12-25 17:03:24 +08001555 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001556 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001557
1558 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1559 return 0;
1560
1561 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001562 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001563 return 0;
1564
Antonio Quartullic018ad32013-06-04 12:11:39 +02001565 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001566
1567 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001568 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1569 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001570 if (!backbone_gw)
1571 return 0;
1572
Sven Eckelmannc8b86c12016-01-17 11:01:15 +01001573 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001574 return 1;
1575}
1576
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001577/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +01001578 * batadv_bla_free - free all bla structures
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001579 * @bat_priv: the bat priv with all the soft interface information
1580 *
1581 * for softinterface free or module unload
1582 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001583void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001584{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001585 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001586
Sven Eckelmann807736f2012-07-15 22:26:51 +02001587 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001588 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001589
Sven Eckelmann807736f2012-07-15 22:26:51 +02001590 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001591 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001592 batadv_hash_destroy(bat_priv->bla.claim_hash);
1593 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001594 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001595 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001596 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001597 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1598 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001599 }
1600 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001601 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001602}
1603
Ben Hutchings2c530402012-07-10 10:55:09 +00001604/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001605 * batadv_bla_rx - check packets coming from the mesh.
Ben Hutchings2c530402012-07-10 10:55:09 +00001606 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001607 * @skb: the frame to be checked
1608 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001609 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001610 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001611 * batadv_bla_rx avoidance checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001612 * * we have to race for a claim
1613 * * if the frame is allowed on the LAN
1614 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001615 * in these cases, the skb is further handled by this function
1616 *
1617 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
Simon Wunderlich23721382012-01-22 20:00:19 +01001618 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001619 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001620int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1621 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001622{
1623 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001624 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001625 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001626 int ret;
1627
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001628 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001629
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001630 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001631 if (!primary_if)
1632 goto handled;
1633
1634 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1635 goto allow;
1636
Sven Eckelmann807736f2012-07-15 22:26:51 +02001637 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001638 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001639 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001640 goto handled;
1641
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001642 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001643 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001644 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001645
1646 if (!claim) {
1647 /* possible optimization: race for a claim */
1648 /* No claim exists yet, claim it for us!
1649 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001650 batadv_handle_claim(bat_priv, primary_if,
1651 primary_if->net_dev->dev_addr,
1652 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001653 goto allow;
1654 }
1655
1656 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001657 if (batadv_compare_eth(claim->backbone_gw->orig,
1658 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001659 /* ... allow it in any case */
1660 claim->lasttime = jiffies;
1661 goto allow;
1662 }
1663
1664 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001665 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1666 /* ... drop it. the responsible gateway is in charge.
1667 *
1668 * We need to check is_bcast because with the gateway
1669 * feature, broadcasts (like DHCP requests) may be sent
1670 * using a unicast packet type.
1671 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001672 goto handled;
1673 } else {
1674 /* seems the client considers us as its best gateway.
1675 * send a claim and update the claim table
1676 * immediately.
1677 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001678 batadv_handle_claim(bat_priv, primary_if,
1679 primary_if->net_dev->dev_addr,
1680 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001681 goto allow;
1682 }
1683allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001684 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001685 ret = 0;
1686 goto out;
1687
1688handled:
1689 kfree_skb(skb);
1690 ret = 1;
1691
1692out:
1693 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001694 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001695 if (claim)
Sven Eckelmann321e3e02016-01-17 11:01:16 +01001696 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001697 return ret;
1698}
1699
Ben Hutchings2c530402012-07-10 10:55:09 +00001700/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001701 * batadv_bla_tx - check packets going into the mesh
Ben Hutchings2c530402012-07-10 10:55:09 +00001702 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001703 * @skb: the frame to be checked
1704 * @vid: the VLAN ID of the frame
1705 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001706 * batadv_bla_tx checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001707 * * a claim was received which has to be processed
1708 * * the frame is allowed on the mesh
1709 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001710 * in these cases, the skb is further handled by this function.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001711 *
1712 * This call might reallocate skb data.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001713 *
1714 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1715 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001716 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001717int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1718 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001719{
1720 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001721 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001722 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001723 int ret = 0;
1724
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001725 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001726 if (!primary_if)
1727 goto out;
1728
1729 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1730 goto allow;
1731
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001732 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001733 goto handled;
1734
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001735 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001736
Sven Eckelmann807736f2012-07-15 22:26:51 +02001737 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001738 /* don't allow broadcasts while requests are in flight */
1739 if (is_multicast_ether_addr(ethhdr->h_dest))
1740 goto handled;
1741
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001742 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001743 search_claim.vid = vid;
1744
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001745 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001746
1747 /* if no claim exists, allow it. */
1748 if (!claim)
1749 goto allow;
1750
1751 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001752 if (batadv_compare_eth(claim->backbone_gw->orig,
1753 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001754 /* if yes, the client has roamed and we have
1755 * to unclaim it.
1756 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001757 batadv_handle_unclaim(bat_priv, primary_if,
1758 primary_if->net_dev->dev_addr,
1759 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001760 goto allow;
1761 }
1762
1763 /* check if it is a multicast/broadcast frame */
1764 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1765 /* drop it. the responsible gateway has forwarded it into
1766 * the backbone network.
1767 */
1768 goto handled;
1769 } else {
1770 /* we must allow it. at least if we are
1771 * responsible for the DESTINATION.
1772 */
1773 goto allow;
1774 }
1775allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001776 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001777 ret = 0;
1778 goto out;
1779handled:
1780 ret = 1;
1781out:
1782 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001783 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001784 if (claim)
Sven Eckelmann321e3e02016-01-17 11:01:16 +01001785 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001786 return ret;
1787}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001788
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001789/**
1790 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1791 * @seq: seq file to print on
1792 * @offset: not used
1793 *
1794 * Return: always 0
1795 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001796int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001797{
1798 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001799 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001800 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001801 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001802 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001803 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001804 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001805 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001806 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001807 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001808
Marek Lindner30da63a2012-08-03 17:15:46 +02001809 primary_if = batadv_seq_print_text_primary_if_get(seq);
1810 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001811 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001812
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001813 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001814 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001815 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001816 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001817 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001818 seq_puts(seq,
1819 " Client VID Originator [o] (CRC )\n");
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001820 for (i = 0; i < hash->size; i++) {
1821 head = &hash->table[i];
1822
1823 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001824 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001825 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1826 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001827
1828 spin_lock_bh(&claim->backbone_gw->crc_lock);
1829 backbone_crc = claim->backbone_gw->crc;
1830 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001831 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001832 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001833 claim->backbone_gw->orig,
1834 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001835 backbone_crc);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001836 }
1837 rcu_read_unlock();
1838 }
1839out:
1840 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001841 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001842 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001843}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001844
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001845/**
1846 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1847 * file
1848 * @seq: seq file to print on
1849 * @offset: not used
1850 *
1851 * Return: always 0
1852 */
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001853int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1854{
1855 struct net_device *net_dev = (struct net_device *)seq->private;
1856 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001857 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001858 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001859 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001860 struct hlist_head *head;
1861 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001862 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001863 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001864 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001865 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001866
Marek Lindner30da63a2012-08-03 17:15:46 +02001867 primary_if = batadv_seq_print_text_primary_if_get(seq);
1868 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001869 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001870
1871 primary_addr = primary_if->net_dev->dev_addr;
1872 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001873 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001874 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001875 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001876 seq_puts(seq, " Originator VID last seen (CRC )\n");
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001877 for (i = 0; i < hash->size; i++) {
1878 head = &hash->table[i];
1879
1880 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001881 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001882 msecs = jiffies_to_msecs(jiffies -
1883 backbone_gw->lasttime);
1884 secs = msecs / 1000;
1885 msecs = msecs % 1000;
1886
1887 is_own = batadv_compare_eth(backbone_gw->orig,
1888 primary_addr);
1889 if (is_own)
1890 continue;
1891
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001892 spin_lock_bh(&backbone_gw->crc_lock);
1893 backbone_crc = backbone_gw->crc;
1894 spin_unlock_bh(&backbone_gw->crc_lock);
1895
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001896 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001897 backbone_gw->orig,
1898 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001899 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001900 }
1901 rcu_read_unlock();
1902 }
1903out:
1904 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001905 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001906 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001907}