blob: 825a5cdf4382d08ff3b90c7499fd605f940b150b [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2011-2016 B.A.T.M.A.N. contributors:
Simon Wunderlich23721382012-01-22 20:00:19 +01002 *
3 * Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Simon Wunderlich23721382012-01-22 20:00:19 +010016 */
17
Simon Wunderlich23721382012-01-22 20:00:19 +010018#include "bridge_loop_avoidance.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010020
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/atomic.h>
22#include <linux/byteorder/generic.h>
23#include <linux/compiler.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010024#include <linux/crc16.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/errno.h>
26#include <linux/etherdevice.h>
27#include <linux/fs.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010028#include <linux/if_arp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/if_ether.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include <linux/if_vlan.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
Sven Eckelmann06e56de2016-01-16 10:29:43 +010034#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/list.h>
36#include <linux/lockdep.h>
37#include <linux/netdevice.h>
38#include <linux/rculist.h>
39#include <linux/rcupdate.h>
40#include <linux/seq_file.h>
41#include <linux/skbuff.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/stddef.h>
45#include <linux/string.h>
46#include <linux/workqueue.h>
47#include <net/arp.h>
48
49#include "hard-interface.h"
50#include "hash.h"
51#include "originator.h"
52#include "packet.h"
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +010053#include "sysfs.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020054#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010055
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020056static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010057
Sven Eckelmann3b300de2012-05-12 18:33:53 +020058static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080059static void
60batadv_bla_send_announce(struct batadv_priv *bat_priv,
61 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010062
Sven Eckelmann62fe7102015-09-15 19:00:48 +020063/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010064 * batadv_choose_claim - choose the right bucket for a claim.
65 * @data: data to hash
66 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020067 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010068 * Return: the hash index of the claim
Sven Eckelmann62fe7102015-09-15 19:00:48 +020069 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020070static inline u32 batadv_choose_claim(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010071{
Marek Lindner712bbfe42012-12-25 17:03:25 +080072 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020073 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010074
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010075 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
76 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010077
78 return hash % size;
79}
80
Sven Eckelmann62fe7102015-09-15 19:00:48 +020081/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010082 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
83 * @data: data to hash
84 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020085 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010086 * Return: the hash index of the backbone gateway
Sven Eckelmann62fe7102015-09-15 19:00:48 +020087 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020088static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010089{
Marek Lindner712bbfe42012-12-25 17:03:25 +080090 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020091 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010092
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010093 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
94 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010095
96 return hash % size;
97}
98
Simon Wunderlich04e14be2015-11-06 10:45:19 +010099/**
100 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
101 * @node: list node of the first entry to compare
102 * @data2: pointer to the second backbone gateway
103 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100104 * Return: true if the backbones have the same data, false otherwise
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100105 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100106static bool batadv_compare_backbone_gw(const struct hlist_node *node,
107 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100108{
Marek Lindnerbae98772012-12-25 17:03:24 +0800109 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +0100110 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200111 const struct batadv_bla_backbone_gw *gw1 = data1;
112 const struct batadv_bla_backbone_gw *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100113
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200114 if (!batadv_compare_eth(gw1->orig, gw2->orig))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100115 return false;
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200116
117 if (gw1->vid != gw2->vid)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100118 return false;
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200119
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100120 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100121}
122
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100123/**
Sven Eckelmann98a5b1d2016-02-22 18:55:32 +0100124 * batadv_compare_claim - compare address and vid of two claims
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100125 * @node: list node of the first entry to compare
126 * @data2: pointer to the second claims
127 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100128 * Return: true if the claim have the same data, 0 otherwise
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100129 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100130static bool batadv_compare_claim(const struct hlist_node *node,
131 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100132{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800133 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +0100134 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200135 const struct batadv_bla_claim *cl1 = data1;
136 const struct batadv_bla_claim *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100137
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200138 if (!batadv_compare_eth(cl1->addr, cl2->addr))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100139 return false;
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200140
141 if (cl1->vid != cl2->vid)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100142 return false;
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200143
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100144 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100145}
146
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100147/**
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100148 * batadv_backbone_gw_release - release backbone gw from lists and queue for
149 * free after rcu grace period
150 * @ref: kref pointer of the backbone gw
151 */
152static void batadv_backbone_gw_release(struct kref *ref)
153{
154 struct batadv_bla_backbone_gw *backbone_gw;
155
156 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
157 refcount);
158
159 kfree_rcu(backbone_gw, rcu);
160}
161
162/**
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100163 * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
164 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100165 * @backbone_gw: backbone gateway to be free'd
166 */
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100167static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100168{
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100169 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
Simon Wunderlich23721382012-01-22 20:00:19 +0100170}
171
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100172/**
173 * batadv_claim_release - release claim from lists and queue for free after rcu
174 * grace period
175 * @ref: kref pointer of the claim
176 */
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100177static void batadv_claim_release(struct kref *ref)
Simon Wunderlich23721382012-01-22 20:00:19 +0100178{
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100179 struct batadv_bla_claim *claim;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200180 struct batadv_bla_backbone_gw *old_backbone_gw;
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100181
182 claim = container_of(ref, struct batadv_bla_claim, refcount);
183
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200184 spin_lock_bh(&claim->backbone_lock);
185 old_backbone_gw = claim->backbone_gw;
186 claim->backbone_gw = NULL;
187 spin_unlock_bh(&claim->backbone_lock);
188
189 spin_lock_bh(&old_backbone_gw->crc_lock);
190 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
191 spin_unlock_bh(&old_backbone_gw->crc_lock);
192
193 batadv_backbone_gw_put(old_backbone_gw);
194
Sven Eckelmann63b39922016-01-14 15:28:19 +0100195 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100196}
197
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100198/**
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100199 * batadv_claim_put - decrement the claim refcounter and possibly
Sven Eckelmannec9b83c2016-01-05 12:06:16 +0100200 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100201 * @claim: claim to be free'd
202 */
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100203static void batadv_claim_put(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100204{
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100205 kref_put(&claim->refcount, batadv_claim_release);
Simon Wunderlich23721382012-01-22 20:00:19 +0100206}
207
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100208/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100209 * batadv_claim_hash_find - looks for a claim in the claim hash
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100210 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100211 * @data: search data (may be local/static data)
212 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200213 * Return: claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100214 */
Sven Eckelmann6fc77a52016-03-05 15:56:01 +0100215static struct batadv_bla_claim *
216batadv_claim_hash_find(struct batadv_priv *bat_priv,
217 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100218{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200219 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100220 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800221 struct batadv_bla_claim *claim;
222 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100223 int index;
224
225 if (!hash)
226 return NULL;
227
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200228 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100229 head = &hash->table[index];
230
231 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800232 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200233 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100234 continue;
235
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100236 if (!kref_get_unless_zero(&claim->refcount))
Simon Wunderlich23721382012-01-22 20:00:19 +0100237 continue;
238
239 claim_tmp = claim;
240 break;
241 }
242 rcu_read_unlock();
243
244 return claim_tmp;
245}
246
Ben Hutchings2c530402012-07-10 10:55:09 +0000247/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100248 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
Ben Hutchings2c530402012-07-10 10:55:09 +0000249 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100250 * @addr: the address of the originator
251 * @vid: the VLAN ID
252 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100253 * Return: backbone gateway if found or NULL otherwise
Simon Wunderlich23721382012-01-22 20:00:19 +0100254 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800255static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200256batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
257 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100258{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200259 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100260 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800261 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
262 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100263 int index;
264
265 if (!hash)
266 return NULL;
267
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100268 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100269 search_entry.vid = vid;
270
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200271 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100272 head = &hash->table[index];
273
274 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800275 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200276 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
277 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100278 continue;
279
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100280 if (!kref_get_unless_zero(&backbone_gw->refcount))
Simon Wunderlich23721382012-01-22 20:00:19 +0100281 continue;
282
283 backbone_gw_tmp = backbone_gw;
284 break;
285 }
286 rcu_read_unlock();
287
288 return backbone_gw_tmp;
289}
290
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100291/**
292 * batadv_bla_del_backbone_claims - delete all claims for a backbone
293 * @backbone_gw: backbone gateway where the claims should be removed
294 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200295static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800296batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100297{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200298 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800299 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100300 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800301 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100302 int i;
303 spinlock_t *list_lock; /* protects write access to the hash lists */
304
Sven Eckelmann807736f2012-07-15 22:26:51 +0200305 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100306 if (!hash)
307 return;
308
309 for (i = 0; i < hash->size; i++) {
310 head = &hash->table[i];
311 list_lock = &hash->list_locks[i];
312
313 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800314 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100315 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100316 if (claim->backbone_gw != backbone_gw)
317 continue;
318
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100319 batadv_claim_put(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800320 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100321 }
322 spin_unlock_bh(list_lock);
323 }
324
Antonio Quartulli3f687852014-11-02 11:29:56 +0100325 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200326 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200327 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200328 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100329}
330
Ben Hutchings2c530402012-07-10 10:55:09 +0000331/**
332 * batadv_bla_send_claim - sends a claim frame according to the provided info
333 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200334 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100335 * @vid: the VLAN ID
336 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100337 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200338static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200339 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100340{
341 struct sk_buff *skb;
342 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200343 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100344 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200345 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200346 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100347 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100348
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200349 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100350 if (!primary_if)
351 return;
352
Sven Eckelmann807736f2012-07-15 22:26:51 +0200353 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100354 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100355 local_claim_dest.type = claimtype;
356
357 soft_iface = primary_if->soft_iface;
358
359 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
360 /* IP DST: 0.0.0.0 */
361 zeroip,
362 primary_if->soft_iface,
363 /* IP SRC: 0.0.0.0 */
364 zeroip,
365 /* Ethernet DST: Broadcast */
366 NULL,
367 /* Ethernet SRC/HW SRC: originator mac */
368 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200369 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100370 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100371 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100372 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200373 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100374
375 if (!skb)
376 goto out;
377
378 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200379 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100380
381 /* now we pretend that the client would have sent this ... */
382 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200383 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100384 /* normal claim frame
385 * set Ethernet SRC to the clients mac
386 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100387 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200388 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200389 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
390 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100391 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200392 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100393 /* unclaim frame
394 * set HW SRC to the clients mac
395 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100396 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200397 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200398 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200399 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100400 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200401 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100402 /* announcement frame
403 * set HW SRC to the special mac containg the crc
404 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100405 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200406 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200407 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200408 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100409 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200410 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100411 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200412 * set HW SRC and header destination to the receiving backbone
413 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100414 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100415 ether_addr_copy(hw_src, mac);
416 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200417 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200418 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200419 ethhdr->h_source, ethhdr->h_dest,
420 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100421 break;
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100422 case BATADV_CLAIM_TYPE_LOOPDETECT:
423 ether_addr_copy(ethhdr->h_source, mac);
424 batadv_dbg(BATADV_DBG_BLA, bat_priv,
425 "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
426 ethhdr->h_source, ethhdr->h_dest,
427 BATADV_PRINT_VID(vid));
428
429 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100430 }
431
Sven Eckelmann10c78f52016-07-02 09:52:13 +0200432 if (vid & BATADV_VLAN_HAS_TAG) {
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200433 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
434 vid & VLAN_VID_MASK);
Sven Eckelmann10c78f52016-07-02 09:52:13 +0200435 if (!skb)
436 goto out;
437 }
Simon Wunderlich23721382012-01-22 20:00:19 +0100438
439 skb_reset_mac_header(skb);
440 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200441 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
442 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
443 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100444 soft_iface->last_rx = jiffies;
445
446 netif_rx(skb);
447out:
448 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100449 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100450}
451
Ben Hutchings2c530402012-07-10 10:55:09 +0000452/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100453 * batadv_bla_loopdetect_report - worker for reporting the loop
454 * @work: work queue item
455 *
456 * Throws an uevent, as the loopdetect check function can't do that itself
457 * since the kernel may sleep while throwing uevents.
458 */
459static void batadv_bla_loopdetect_report(struct work_struct *work)
460{
461 struct batadv_bla_backbone_gw *backbone_gw;
462 struct batadv_priv *bat_priv;
463 char vid_str[6] = { '\0' };
464
465 backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
466 report_work);
467 bat_priv = backbone_gw->bat_priv;
468
469 batadv_info(bat_priv->soft_iface,
470 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
471 BATADV_PRINT_VID(backbone_gw->vid));
472 snprintf(vid_str, sizeof(vid_str), "%d",
473 BATADV_PRINT_VID(backbone_gw->vid));
474 vid_str[sizeof(vid_str) - 1] = 0;
475
476 batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
477 vid_str);
478
479 batadv_backbone_gw_put(backbone_gw);
480}
481
482/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100483 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
Ben Hutchings2c530402012-07-10 10:55:09 +0000484 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100485 * @orig: the mac address of the originator
486 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200487 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100488 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100489 * Return: the (possibly created) backbone gateway or NULL on error
Simon Wunderlich23721382012-01-22 20:00:19 +0100490 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800491static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200492batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200493 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100494{
Marek Lindnerbae98772012-12-25 17:03:24 +0800495 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200496 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100497 int hash_added;
498
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200499 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100500
501 if (entry)
502 return entry;
503
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200504 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200505 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200506 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100507
508 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
509 if (!entry)
510 return NULL;
511
512 entry->vid = vid;
513 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200514 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100515 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200516 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100517 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200518 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100519 ether_addr_copy(entry->orig, orig);
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100520 INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
Simon Wunderlich23721382012-01-22 20:00:19 +0100521
522 /* one for the hash, one for returning */
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100523 kref_init(&entry->refcount);
524 kref_get(&entry->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100525
Sven Eckelmann807736f2012-07-15 22:26:51 +0200526 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200527 batadv_compare_backbone_gw,
528 batadv_choose_backbone_gw, entry,
529 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100530
531 if (unlikely(hash_added != 0)) {
532 /* hash failed, free the structure */
533 kfree(entry);
534 return NULL;
535 }
536
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200537 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200538 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100539 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200540 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200541 "became a backbone gateway");
Sven Eckelmann5d967312016-01-17 11:01:09 +0100542 batadv_orig_node_put(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100543 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200544
Simon Wunderlichd807f272012-09-09 22:27:57 +0200545 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200546 batadv_bla_send_announce(bat_priv, entry);
547
Simon Wunderlichd807f272012-09-09 22:27:57 +0200548 /* this will be decreased in the worker thread */
549 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200550 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200551 atomic_inc(&bat_priv->bla.num_requests);
552 }
553
Simon Wunderlich23721382012-01-22 20:00:19 +0100554 return entry;
555}
556
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100557/**
558 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
559 * @bat_priv: the bat priv with all the soft interface information
560 * @primary_if: the selected primary interface
561 * @vid: VLAN identifier
562 *
563 * update or add the own backbone gw to make sure we announce
Simon Wunderlich23721382012-01-22 20:00:19 +0100564 * where we receive other backbone gws
565 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200566static void
567batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
568 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200569 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100570{
Marek Lindnerbae98772012-12-25 17:03:24 +0800571 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100572
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200573 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
574 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200575 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100576 if (unlikely(!backbone_gw))
577 return;
578
579 backbone_gw->lasttime = jiffies;
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100580 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100581}
582
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100583/**
584 * batadv_bla_answer_request - answer a bla request by sending own claims
585 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200586 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100587 * @vid: the vid where the request came on
588 *
589 * Repeat all of our own claims, and finally send an ANNOUNCE frame
590 * to allow the requester another check if the CRC is correct now.
591 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200592static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
593 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200594 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100595{
Simon Wunderlich23721382012-01-22 20:00:19 +0100596 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200597 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800598 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800599 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100600 int i;
601
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200602 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200603 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100604
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200605 backbone_gw = batadv_backbone_hash_find(bat_priv,
606 primary_if->net_dev->dev_addr,
607 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100608 if (!backbone_gw)
609 return;
610
Sven Eckelmann807736f2012-07-15 22:26:51 +0200611 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100612 for (i = 0; i < hash->size; i++) {
613 head = &hash->table[i];
614
615 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800616 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100617 /* only own claims are interesting */
618 if (claim->backbone_gw != backbone_gw)
619 continue;
620
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200621 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200622 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100623 }
624 rcu_read_unlock();
625 }
626
627 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200628 batadv_bla_send_announce(bat_priv, backbone_gw);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100629 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100630}
631
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100632/**
633 * batadv_bla_send_request - send a request to repeat claims
634 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100635 *
636 * When the crc is wrong, ask the backbone gateway for a full table update.
637 * After the request, it will repeat all of his own claims and finally
638 * send an announcement claim with which we can check again.
639 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800640static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100641{
642 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200643 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100644
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200645 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
646 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100647
648 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200649 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200650 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100651
652 /* no local broadcasts should be sent or received, for now. */
653 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200654 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100655 atomic_set(&backbone_gw->request_sent, 1);
656 }
657}
658
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100659/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100660 * batadv_bla_send_announce - Send an announcement frame
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100661 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100662 * @backbone_gw: our backbone gateway which should be announced
Simon Wunderlich23721382012-01-22 20:00:19 +0100663 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200664static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800665 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100666{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200667 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100668 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100669
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200670 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200671 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100672 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200673 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100674 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100675
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200676 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200677 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100678}
679
Ben Hutchings2c530402012-07-10 10:55:09 +0000680/**
681 * batadv_bla_add_claim - Adds a claim in the claim hash
682 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100683 * @mac: the mac address of the claim
684 * @vid: the VLAN ID of the frame
685 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100686 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200687static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200688 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800689 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100690{
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200691 struct batadv_bla_backbone_gw *old_backbone_gw;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800692 struct batadv_bla_claim *claim;
693 struct batadv_bla_claim search_claim;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200694 bool remove_crc = false;
Simon Wunderlich23721382012-01-22 20:00:19 +0100695 int hash_added;
696
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100697 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100698 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200699 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100700
701 /* create a new claim entry if it does not exist yet. */
702 if (!claim) {
703 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
704 if (!claim)
705 return;
706
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100707 ether_addr_copy(claim->addr, mac);
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200708 spin_lock_init(&claim->backbone_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100709 claim->vid = vid;
710 claim->lasttime = jiffies;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200711 kref_get(&backbone_gw->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100712 claim->backbone_gw = backbone_gw;
713
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100714 kref_init(&claim->refcount);
715 kref_get(&claim->refcount);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200716 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200717 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200718 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200719 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200720 batadv_compare_claim,
721 batadv_choose_claim, claim,
722 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100723
724 if (unlikely(hash_added != 0)) {
725 /* only local changes happened. */
726 kfree(claim);
727 return;
728 }
729 } else {
730 claim->lasttime = jiffies;
731 if (claim->backbone_gw == backbone_gw)
732 /* no need to register a new backbone */
733 goto claim_free_ref;
734
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200735 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200736 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200737 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100738
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200739 remove_crc = true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100740 }
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200741
742 /* replace backbone_gw atomically and adjust reference counters */
743 spin_lock_bh(&claim->backbone_lock);
744 old_backbone_gw = claim->backbone_gw;
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100745 kref_get(&backbone_gw->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100746 claim->backbone_gw = backbone_gw;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200747 spin_unlock_bh(&claim->backbone_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100748
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200749 if (remove_crc) {
750 /* remove claim address from old backbone_gw */
751 spin_lock_bh(&old_backbone_gw->crc_lock);
752 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
753 spin_unlock_bh(&old_backbone_gw->crc_lock);
754 }
755
756 batadv_backbone_gw_put(old_backbone_gw);
757
758 /* add claim address to new backbone_gw */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200759 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100760 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200761 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100762 backbone_gw->lasttime = jiffies;
763
764claim_free_ref:
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100765 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100766}
767
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100768/**
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200769 * batadv_bla_claim_get_backbone_gw - Get valid reference for backbone_gw of
770 * claim
771 * @claim: claim whose backbone_gw should be returned
772 *
773 * Return: valid reference to claim::backbone_gw
774 */
775static struct batadv_bla_backbone_gw *
776batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
777{
778 struct batadv_bla_backbone_gw *backbone_gw;
779
780 spin_lock_bh(&claim->backbone_lock);
781 backbone_gw = claim->backbone_gw;
782 kref_get(&backbone_gw->refcount);
783 spin_unlock_bh(&claim->backbone_lock);
784
785 return backbone_gw;
786}
787
788/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100789 * batadv_bla_del_claim - delete a claim from the claim hash
790 * @bat_priv: the bat priv with all the soft interface information
791 * @mac: mac address of the claim to be removed
792 * @vid: VLAN id for the claim to be removed
Simon Wunderlich23721382012-01-22 20:00:19 +0100793 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200794static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200795 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100796{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800797 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100798
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100799 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100800 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200801 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100802 if (!claim)
803 return;
804
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200805 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200806 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100807
Sven Eckelmann807736f2012-07-15 22:26:51 +0200808 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200809 batadv_choose_claim, claim);
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100810 batadv_claim_put(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100811
Simon Wunderlich23721382012-01-22 20:00:19 +0100812 /* don't need the reference from hash_find() anymore */
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100813 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100814}
815
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200816/**
817 * batadv_handle_announce - check for ANNOUNCE frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100818 * @bat_priv: the bat priv with all the soft interface information
819 * @an_addr: announcement mac address (ARP Sender HW address)
820 * @backbone_addr: originator address of the sender (Ethernet source MAC)
821 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200822 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100823 * Return: true if handled
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200824 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100825static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
826 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100827{
Marek Lindnerbae98772012-12-25 17:03:24 +0800828 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200829 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100830
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200831 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100832 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +0100833
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200834 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
835 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100836
837 if (unlikely(!backbone_gw))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100838 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100839
Simon Wunderlich23721382012-01-22 20:00:19 +0100840 /* handle as ANNOUNCE frame */
841 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100842 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100843
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200844 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100845 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200846 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100847
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200848 spin_lock_bh(&backbone_gw->crc_lock);
849 backbone_crc = backbone_gw->crc;
850 spin_unlock_bh(&backbone_gw->crc_lock);
851
852 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200853 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100854 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200855 backbone_gw->orig,
856 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200857 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100858
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200859 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100860 } else {
861 /* if we have sent a request and the crc was OK,
862 * we can allow traffic again.
863 */
864 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200865 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100866 atomic_set(&backbone_gw->request_sent, 0);
867 }
868 }
869
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100870 batadv_backbone_gw_put(backbone_gw);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100871 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100872}
873
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200874/**
875 * batadv_handle_request - check for REQUEST frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100876 * @bat_priv: the bat priv with all the soft interface information
877 * @primary_if: the primary hard interface of this batman soft interface
878 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
879 * @ethhdr: ethernet header of a packet
880 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200881 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100882 * Return: true if handled
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200883 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100884static bool batadv_handle_request(struct batadv_priv *bat_priv,
885 struct batadv_hard_iface *primary_if,
886 u8 *backbone_addr, struct ethhdr *ethhdr,
887 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100888{
889 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200890 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100891 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +0100892
893 /* sanity check, this should not happen on a normal switch,
894 * we ignore it in this case.
895 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200896 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100897 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100898
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200899 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200900 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200901 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100902
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200903 batadv_bla_answer_request(bat_priv, primary_if, vid);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100904 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100905}
906
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200907/**
908 * batadv_handle_unclaim - check for UNCLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100909 * @bat_priv: the bat priv with all the soft interface information
910 * @primary_if: the primary hard interface of this batman soft interface
911 * @backbone_addr: originator address of the backbone (Ethernet source)
912 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
913 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200914 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100915 * Return: true if handled
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200916 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100917static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
918 struct batadv_hard_iface *primary_if,
919 u8 *backbone_addr, u8 *claim_addr,
920 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100921{
Marek Lindnerbae98772012-12-25 17:03:24 +0800922 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100923
924 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200925 if (primary_if && batadv_compare_eth(backbone_addr,
926 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200927 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200928 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100929
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200930 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100931
932 if (!backbone_gw)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100933 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100934
935 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200936 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200937 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200938 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100939
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200940 batadv_bla_del_claim(bat_priv, claim_addr, vid);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100941 batadv_backbone_gw_put(backbone_gw);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100942 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100943}
944
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200945/**
946 * batadv_handle_claim - check for CLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100947 * @bat_priv: the bat priv with all the soft interface information
948 * @primary_if: the primary hard interface of this batman soft interface
949 * @backbone_addr: originator address of the backbone (Ethernet Source)
950 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
951 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200952 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100953 * Return: true if handled
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200954 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100955static bool batadv_handle_claim(struct batadv_priv *bat_priv,
956 struct batadv_hard_iface *primary_if,
957 u8 *backbone_addr, u8 *claim_addr,
958 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100959{
Marek Lindnerbae98772012-12-25 17:03:24 +0800960 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100961
962 /* register the gateway if not yet available, and add the claim. */
963
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200964 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
965 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100966
967 if (unlikely(!backbone_gw))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100968 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100969
970 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200971 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200972 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200973 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200974 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100975
976 /* TODO: we could call something like tt_local_del() here. */
977
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100978 batadv_backbone_gw_put(backbone_gw);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100979 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100980}
981
Ben Hutchings2c530402012-07-10 10:55:09 +0000982/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100983 * batadv_check_claim_group - check for claim group membership
Ben Hutchings2c530402012-07-10 10:55:09 +0000984 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200985 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100986 * @hw_src: the Hardware source in the ARP Header
987 * @hw_dst: the Hardware destination in the ARP Header
988 * @ethhdr: pointer to the Ethernet header of the claim frame
989 *
990 * checks if it is a claim packet and if its on the same group.
991 * This function also applies the group ID of the sender
992 * if it is in the same mesh.
993 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200994 * Return:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100995 * 2 - if it is a claim packet and on the same group
996 * 1 - if is a claim packet from another group
997 * 0 - if it is not a claim packet
998 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200999static int batadv_check_claim_group(struct batadv_priv *bat_priv,
1000 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001001 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001002 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001003{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001004 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001005 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +02001006 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001007
Sven Eckelmann96412692012-06-05 22:31:30 +02001008 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001009 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001010
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001011 /* if announcement packet, use the source,
1012 * otherwise assume it is in the hw_src
1013 */
1014 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001015 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001016 backbone_addr = hw_src;
1017 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001018 case BATADV_CLAIM_TYPE_REQUEST:
1019 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001020 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001021 backbone_addr = ethhdr->h_source;
1022 break;
1023 default:
1024 return 0;
1025 }
1026
1027 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001028 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001029 return 0;
1030
1031 /* if its already the same group, it is fine. */
1032 if (bla_dst->group == bla_dst_own->group)
1033 return 2;
1034
1035 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +02001036 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001037
1038 /* dont accept claims from gateways which are not in
1039 * the same mesh or group.
1040 */
1041 if (!orig_node)
1042 return 1;
1043
1044 /* if our mesh friends mac is bigger, use it for ourselves. */
1045 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001046 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001047 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001048 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001049 bla_dst_own->group = bla_dst->group;
1050 }
1051
Sven Eckelmann5d967312016-01-17 11:01:09 +01001052 batadv_orig_node_put(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001053
1054 return 2;
1055}
1056
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001057/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001058 * batadv_bla_process_claim - Check if this is a claim frame, and process it
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001059 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +02001060 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +01001061 * @skb: the frame to be checked
1062 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001063 * Return: true if it was a claim frame, otherwise return false to
Simon Wunderlich23721382012-01-22 20:00:19 +01001064 * tell the callee that it can use the frame on its own.
1065 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001066static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
1067 struct batadv_hard_iface *primary_if,
1068 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +01001069{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001070 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001071 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001072 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001073 struct ethhdr *ethhdr;
1074 struct arphdr *arphdr;
1075 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001076 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +02001077 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +01001078 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001079 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001080
Antonio Quartullic018ad32013-06-04 12:11:39 +02001081 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001082 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001083
Antonio Quartullic018ad32013-06-04 12:11:39 +02001084 proto = ethhdr->h_proto;
1085 headlen = ETH_HLEN;
1086 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001087 /* Traverse the VLAN/Ethertypes.
1088 *
1089 * At this point it is known that the first protocol is a VLAN
1090 * header, so start checking at the encapsulated protocol.
1091 *
1092 * The depth of the VLAN headers is recorded to drop BLA claim
1093 * frames encapsulated into multiple VLAN headers (QinQ).
1094 */
1095 do {
1096 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1097 &vhdr_buf);
1098 if (!vhdr)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001099 return false;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001100
1101 proto = vhdr->h_vlan_encapsulated_proto;
1102 headlen += VLAN_HLEN;
1103 vlan_depth++;
1104 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +01001105 }
1106
Antonio Quartulli293e9332013-05-19 12:55:16 +02001107 if (proto != htons(ETH_P_ARP))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001108 return false; /* not a claim frame */
Simon Wunderlich23721382012-01-22 20:00:19 +01001109
1110 /* this must be a ARP frame. check if it is a claim. */
1111
1112 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001113 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001114
1115 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001116 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001117 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +01001118
1119 /* Check whether the ARP frame carries a valid
1120 * IP information
1121 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001122 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001123 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001124 if (arphdr->ar_pro != htons(ETH_P_IP))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001125 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001126 if (arphdr->ar_hln != ETH_ALEN)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001127 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001128 if (arphdr->ar_pln != 4)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001129 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001130
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001131 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001132 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +02001133 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001134 bla_dst_own = &bat_priv->bla.claim_dest;
1135
1136 /* check if it is a claim frame in general */
1137 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1138 sizeof(bla_dst->magic)) != 0)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001139 return false;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001140
1141 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1142 * drop that, as this is not supported by BLA but should also not be
1143 * sent via the mesh.
1144 */
1145 if (vlan_depth > 1)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001146 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001147
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001148 /* Let the loopdetect frames on the mesh in any case. */
1149 if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
1150 return 0;
1151
Simon Wunderlich23721382012-01-22 20:00:19 +01001152 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001153 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1154 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001155 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001156 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001157 "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 +02001158 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1159 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001160
1161 if (ret < 2)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001162 return !!ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001163
1164 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001165 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001166
1167 /* check for the different types of claim frames ... */
1168 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001169 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001170 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1171 ethhdr->h_source, vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001172 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001173 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001174 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001175 if (batadv_handle_unclaim(bat_priv, primary_if,
1176 ethhdr->h_source, hw_src, vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001177 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001178 break;
1179
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001180 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001181 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1182 vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001183 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001184 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001185 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001186 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1187 vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001188 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001189 break;
1190 }
1191
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001192 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001193 "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 +02001194 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001195 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001196}
1197
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001198/**
1199 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1200 * immediately
1201 * @bat_priv: the bat priv with all the soft interface information
1202 * @now: whether the whole hash shall be wiped now
1203 *
1204 * Check when we last heard from other nodes, and remove them in case of
Simon Wunderlich23721382012-01-22 20:00:19 +01001205 * a time out, or clean all backbone gws if now is set.
1206 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001207static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001208{
Marek Lindnerbae98772012-12-25 17:03:24 +08001209 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001210 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001211 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001212 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001213 spinlock_t *list_lock; /* protects write access to the hash lists */
1214 int i;
1215
Sven Eckelmann807736f2012-07-15 22:26:51 +02001216 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001217 if (!hash)
1218 return;
1219
1220 for (i = 0; i < hash->size; i++) {
1221 head = &hash->table[i];
1222 list_lock = &hash->list_locks[i];
1223
1224 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001225 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001226 head, hash_entry) {
1227 if (now)
1228 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001229 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001230 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001231 continue;
1232
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001233 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001234 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1235 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001236
1237purge_now:
1238 /* don't wait for the pending request anymore */
1239 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001240 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001241
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001242 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001243
Sasha Levinb67bfe02013-02-27 17:06:00 -08001244 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +01001245 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001246 }
1247 spin_unlock_bh(list_lock);
1248 }
1249}
1250
Ben Hutchings2c530402012-07-10 10:55:09 +00001251/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001252 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
Ben Hutchings2c530402012-07-10 10:55:09 +00001253 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001254 * @primary_if: the selected primary interface, may be NULL if now is set
1255 * @now: whether the whole hash shall be wiped now
1256 *
1257 * Check when we heard last time from our own claims, and remove them in case of
1258 * a time out, or clean all claims if now is set
1259 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001260static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1261 struct batadv_hard_iface *primary_if,
1262 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001263{
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001264 struct batadv_bla_backbone_gw *backbone_gw;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001265 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001266 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001267 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001268 int i;
1269
Sven Eckelmann807736f2012-07-15 22:26:51 +02001270 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001271 if (!hash)
1272 return;
1273
1274 for (i = 0; i < hash->size; i++) {
1275 head = &hash->table[i];
1276
1277 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001278 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001279 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001280 if (now)
1281 goto purge_now;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001282
1283 if (!batadv_compare_eth(backbone_gw->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001284 primary_if->net_dev->dev_addr))
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001285 goto skip;
1286
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001287 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001288 BATADV_BLA_CLAIM_TIMEOUT))
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001289 goto skip;
Simon Wunderlich23721382012-01-22 20:00:19 +01001290
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001291 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001292 "bla_purge_claims(): %pM, vid %d, time out\n",
1293 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001294
1295purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001296 batadv_handle_unclaim(bat_priv, primary_if,
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001297 backbone_gw->orig,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001298 claim->addr, claim->vid);
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001299skip:
1300 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001301 }
1302 rcu_read_unlock();
1303 }
1304}
1305
Ben Hutchings2c530402012-07-10 10:55:09 +00001306/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001307 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1308 * originator address changes
Ben Hutchings2c530402012-07-10 10:55:09 +00001309 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001310 * @primary_if: the new selected primary_if
1311 * @oldif: the old primary interface, may be NULL
Simon Wunderlich23721382012-01-22 20:00:19 +01001312 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001313void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1314 struct batadv_hard_iface *primary_if,
1315 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001316{
Marek Lindnerbae98772012-12-25 17:03:24 +08001317 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001318 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001319 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001320 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001321 int i;
1322
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001323 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001324 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1325 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001326
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001327 /* purge everything when bridge loop avoidance is turned off */
1328 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1329 oldif = NULL;
1330
Simon Wunderlich23721382012-01-22 20:00:19 +01001331 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001332 batadv_bla_purge_claims(bat_priv, NULL, 1);
1333 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001334 return;
1335 }
1336
Sven Eckelmann807736f2012-07-15 22:26:51 +02001337 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001338 if (!hash)
1339 return;
1340
1341 for (i = 0; i < hash->size; i++) {
1342 head = &hash->table[i];
1343
1344 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001345 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001346 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001347 if (!batadv_compare_eth(backbone_gw->orig,
1348 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001349 continue;
1350
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001351 ether_addr_copy(backbone_gw->orig,
1352 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001353 /* send an announce frame so others will ask for our
1354 * claims and update their tables.
1355 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001356 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001357 }
1358 rcu_read_unlock();
1359 }
1360}
1361
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001362/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001363 * batadv_bla_send_loopdetect - send a loopdetect frame
1364 * @bat_priv: the bat priv with all the soft interface information
1365 * @backbone_gw: the backbone gateway for which a loop should be detected
1366 *
1367 * To detect loops that the bridge loop avoidance can't handle, send a loop
1368 * detection packet on the backbone. Unlike other BLA frames, this frame will
1369 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1370 * indicates that there is a loop.
1371 */
1372static void
1373batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
1374 struct batadv_bla_backbone_gw *backbone_gw)
1375{
1376 batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
1377 backbone_gw->vid);
1378 batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
1379 backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
1380}
1381
1382/**
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001383 * batadv_bla_status_update - purge bla interfaces if necessary
1384 * @net_dev: the soft interface net device
1385 */
1386void batadv_bla_status_update(struct net_device *net_dev)
1387{
1388 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1389 struct batadv_hard_iface *primary_if;
1390
1391 primary_if = batadv_primary_if_get_selected(bat_priv);
1392 if (!primary_if)
1393 return;
1394
1395 /* this function already purges everything when bla is disabled,
1396 * so just call that one.
1397 */
1398 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001399 batadv_hardif_put(primary_if);
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001400}
1401
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001402/**
1403 * batadv_bla_periodic_work - performs periodic bla work
1404 * @work: kernel work struct
1405 *
1406 * periodic work to do:
Simon Wunderlich23721382012-01-22 20:00:19 +01001407 * * purge structures when they are too old
1408 * * send announcements
1409 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001410static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001411{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001412 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001413 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001414 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001415 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001416 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001417 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001418 struct batadv_hard_iface *primary_if;
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001419 bool send_loopdetect = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001420 int i;
1421
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001422 delayed_work = to_delayed_work(work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001423 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1424 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001425 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001426 if (!primary_if)
1427 goto out;
1428
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001429 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1430 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001431
1432 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1433 goto out;
1434
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001435 if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
1436 /* set a new random mac address for the next bridge loop
1437 * detection frames. Set the locally administered bit to avoid
1438 * collisions with users mac addresses.
1439 */
1440 random_ether_addr(bat_priv->bla.loopdetect_addr);
1441 bat_priv->bla.loopdetect_addr[0] = 0xba;
1442 bat_priv->bla.loopdetect_addr[1] = 0xbe;
1443 bat_priv->bla.loopdetect_lasttime = jiffies;
1444 atomic_set(&bat_priv->bla.loopdetect_next,
1445 BATADV_BLA_LOOPDETECT_PERIODS);
1446
1447 /* mark for sending loop detect on all VLANs */
1448 send_loopdetect = true;
1449 }
1450
Sven Eckelmann807736f2012-07-15 22:26:51 +02001451 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001452 if (!hash)
1453 goto out;
1454
1455 for (i = 0; i < hash->size; i++) {
1456 head = &hash->table[i];
1457
1458 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001459 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001460 if (!batadv_compare_eth(backbone_gw->orig,
1461 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001462 continue;
1463
1464 backbone_gw->lasttime = jiffies;
1465
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001466 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001467 if (send_loopdetect)
1468 batadv_bla_send_loopdetect(bat_priv,
1469 backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001470
1471 /* request_sent is only set after creation to avoid
1472 * problems when we are not yet known as backbone gw
1473 * in the backbone.
1474 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001475 * We can reset this now after we waited some periods
1476 * to give bridge forward delays and bla group forming
1477 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001478 */
1479
1480 if (atomic_read(&backbone_gw->request_sent) == 0)
1481 continue;
1482
Simon Wunderlich28709872012-09-13 18:18:46 +02001483 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1484 continue;
1485
Simon Wunderlichd807f272012-09-09 22:27:57 +02001486 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1487 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001488 }
1489 rcu_read_unlock();
1490 }
1491out:
1492 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001493 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001494
Antonio Quartulli72414442012-12-25 13:14:37 +01001495 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1496 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001497}
1498
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001499/* The hash for claim and backbone hash receive the same key because they
1500 * are getting initialized by hash_new with the same key. Reinitializing
1501 * them with to different keys to allow nested locking without generating
1502 * lockdep warnings
1503 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001504static struct lock_class_key batadv_claim_hash_lock_class_key;
1505static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001506
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001507/**
1508 * batadv_bla_init - initialize all bla structures
1509 * @bat_priv: the bat priv with all the soft interface information
1510 *
1511 * Return: 0 on success, < 0 on error.
1512 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001513int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001514{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001515 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001516 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001517 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001518 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001519 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001520
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001521 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1522
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001523 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001524
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001525 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001526 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1527 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001528 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001529 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001530 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1531 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001532 batadv_hardif_put(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001533 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001534 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001535 }
1536
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001537 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001538 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001539 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001540 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1541 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001542
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001543 atomic_set(&bat_priv->bla.loopdetect_next,
1544 BATADV_BLA_LOOPDETECT_PERIODS);
1545
Sven Eckelmann807736f2012-07-15 22:26:51 +02001546 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001547 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001548
Sven Eckelmann807736f2012-07-15 22:26:51 +02001549 bat_priv->bla.claim_hash = batadv_hash_new(128);
1550 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001551
Sven Eckelmann807736f2012-07-15 22:26:51 +02001552 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001553 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001554
Sven Eckelmann807736f2012-07-15 22:26:51 +02001555 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001556 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001557 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001558 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001559
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001560 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001561
Antonio Quartulli72414442012-12-25 13:14:37 +01001562 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1563
1564 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1565 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001566 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001567}
1568
Ben Hutchings2c530402012-07-10 10:55:09 +00001569/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001570 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
Ben Hutchings2c530402012-07-10 10:55:09 +00001571 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001572 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001573 *
1574 * check if it is on our broadcast list. Another gateway might
1575 * have sent the same packet because it is connected to the same backbone,
1576 * so we have to remove this duplicate.
1577 *
1578 * This is performed by checking the CRC, which will tell us
1579 * with a good chance that it is the same packet. If it is furthermore
1580 * sent by another host, drop it. We allow equal packets from
1581 * the same host however as this might be intended.
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001582 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001583 * Return: true if a packet is in the duplicate list, false otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001584 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001585bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1586 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001587{
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001588 int i, curr;
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001589 __be32 crc;
1590 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001591 struct batadv_bcast_duplist_entry *entry;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001592 bool ret = false;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001593
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001594 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001595
1596 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001597 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001598
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001599 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1600
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001601 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001602 curr = (bat_priv->bla.bcast_duplist_curr + i);
1603 curr %= BATADV_DUPLIST_SIZE;
1604 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001605
1606 /* we can stop searching if the entry is too old ;
1607 * later entries will be even older
1608 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001609 if (batadv_has_timed_out(entry->entrytime,
1610 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001611 break;
1612
1613 if (entry->crc != crc)
1614 continue;
1615
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001616 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001617 continue;
1618
1619 /* this entry seems to match: same crc, not too old,
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001620 * and from another gw. therefore return true to forbid it.
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001621 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001622 ret = true;
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001623 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001624 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001625 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001626 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001627 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001628 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001629 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001630 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001631 entry->crc = crc;
1632 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001633 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001634 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001635
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001636out:
1637 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1638
1639 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001640}
1641
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001642/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001643 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1644 * the VLAN identified by vid.
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001645 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001646 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001647 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001648 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001649 * Return: true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001650 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001651bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001652 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001653{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001654 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001655 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001656 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001657 int i;
1658
1659 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001660 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001661
1662 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001663 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001664
1665 for (i = 0; i < hash->size; i++) {
1666 head = &hash->table[i];
1667
1668 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001669 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001670 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1671 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001672 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001673 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001674 }
1675 }
1676 rcu_read_unlock();
1677 }
1678
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001679 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001680}
1681
Ben Hutchings2c530402012-07-10 10:55:09 +00001682/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001683 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
Ben Hutchings2c530402012-07-10 10:55:09 +00001684 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001685 * @orig_node: the orig_node of the frame
1686 * @hdr_size: maximum length of the frame
1687 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001688 * Return: true if the orig_node is also a gateway on the soft interface,
1689 * otherwise it returns false.
Simon Wunderlich23721382012-01-22 20:00:19 +01001690 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001691bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
1692 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001693{
Marek Lindnerbae98772012-12-25 17:03:24 +08001694 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001695 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001696
1697 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001698 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001699
1700 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001701 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001702 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001703
Antonio Quartullic018ad32013-06-04 12:11:39 +02001704 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001705
1706 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001707 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1708 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001709 if (!backbone_gw)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001710 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001711
Sven Eckelmannc8b86c12016-01-17 11:01:15 +01001712 batadv_backbone_gw_put(backbone_gw);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001713 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001714}
1715
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001716/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +01001717 * batadv_bla_free - free all bla structures
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001718 * @bat_priv: the bat priv with all the soft interface information
1719 *
1720 * for softinterface free or module unload
1721 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001722void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001723{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001724 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001725
Sven Eckelmann807736f2012-07-15 22:26:51 +02001726 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001727 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001728
Sven Eckelmann807736f2012-07-15 22:26:51 +02001729 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001730 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001731 batadv_hash_destroy(bat_priv->bla.claim_hash);
1732 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001733 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001734 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001735 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001736 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1737 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001738 }
1739 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001740 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001741}
1742
Ben Hutchings2c530402012-07-10 10:55:09 +00001743/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001744 * batadv_bla_loopdetect_check - check and handle a detected loop
1745 * @bat_priv: the bat priv with all the soft interface information
1746 * @skb: the packet to check
1747 * @primary_if: interface where the request came on
1748 * @vid: the VLAN ID of the frame
1749 *
1750 * Checks if this packet is a loop detect frame which has been sent by us,
1751 * throw an uevent and log the event if that is the case.
1752 *
1753 * Return: true if it is a loop detect frame which is to be dropped, false
1754 * otherwise.
1755 */
1756static bool
1757batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1758 struct batadv_hard_iface *primary_if,
1759 unsigned short vid)
1760{
1761 struct batadv_bla_backbone_gw *backbone_gw;
1762 struct ethhdr *ethhdr;
1763
1764 ethhdr = eth_hdr(skb);
1765
1766 /* Only check for the MAC address and skip more checks here for
1767 * performance reasons - this function is on the hotpath, after all.
1768 */
1769 if (!batadv_compare_eth(ethhdr->h_source,
1770 bat_priv->bla.loopdetect_addr))
1771 return false;
1772
1773 /* If the packet came too late, don't forward it on the mesh
1774 * but don't consider that as loop. It might be a coincidence.
1775 */
1776 if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
1777 BATADV_BLA_LOOPDETECT_TIMEOUT))
1778 return true;
1779
1780 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1781 primary_if->net_dev->dev_addr,
1782 vid, true);
1783 if (unlikely(!backbone_gw))
1784 return true;
1785
1786 queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1787 /* backbone_gw is unreferenced in the report work function function */
1788
1789 return true;
1790}
1791
1792/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001793 * batadv_bla_rx - check packets coming from the mesh.
Ben Hutchings2c530402012-07-10 10:55:09 +00001794 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001795 * @skb: the frame to be checked
1796 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001797 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001798 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001799 * batadv_bla_rx avoidance checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001800 * * we have to race for a claim
1801 * * if the frame is allowed on the LAN
1802 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001803 * in these cases, the skb is further handled by this function
1804 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001805 * Return: true if handled, otherwise it returns false and the caller shall
1806 * further process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001807 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001808bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1809 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001810{
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001811 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001812 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001813 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001814 struct batadv_hard_iface *primary_if;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001815 bool own_claim;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001816 bool ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001817
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001818 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001819
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001820 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001821 if (!primary_if)
1822 goto handled;
1823
1824 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1825 goto allow;
1826
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001827 if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1828 goto handled;
1829
Sven Eckelmann807736f2012-07-15 22:26:51 +02001830 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001831 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001832 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001833 goto handled;
1834
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001835 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001836 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001837 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001838
1839 if (!claim) {
1840 /* possible optimization: race for a claim */
1841 /* No claim exists yet, claim it for us!
1842 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001843 batadv_handle_claim(bat_priv, primary_if,
1844 primary_if->net_dev->dev_addr,
1845 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001846 goto allow;
1847 }
1848
1849 /* if it is our own claim ... */
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001850 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1851 own_claim = batadv_compare_eth(backbone_gw->orig,
1852 primary_if->net_dev->dev_addr);
1853 batadv_backbone_gw_put(backbone_gw);
1854
1855 if (own_claim) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001856 /* ... allow it in any case */
1857 claim->lasttime = jiffies;
1858 goto allow;
1859 }
1860
1861 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001862 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1863 /* ... drop it. the responsible gateway is in charge.
1864 *
1865 * We need to check is_bcast because with the gateway
1866 * feature, broadcasts (like DHCP requests) may be sent
1867 * using a unicast packet type.
1868 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001869 goto handled;
1870 } else {
1871 /* seems the client considers us as its best gateway.
1872 * send a claim and update the claim table
1873 * immediately.
1874 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001875 batadv_handle_claim(bat_priv, primary_if,
1876 primary_if->net_dev->dev_addr,
1877 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001878 goto allow;
1879 }
1880allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001881 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001882 ret = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001883 goto out;
1884
1885handled:
1886 kfree_skb(skb);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001887 ret = true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001888
1889out:
1890 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001891 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001892 if (claim)
Sven Eckelmann321e3e02016-01-17 11:01:16 +01001893 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001894 return ret;
1895}
1896
Ben Hutchings2c530402012-07-10 10:55:09 +00001897/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001898 * batadv_bla_tx - check packets going into the mesh
Ben Hutchings2c530402012-07-10 10:55:09 +00001899 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001900 * @skb: the frame to be checked
1901 * @vid: the VLAN ID of the frame
1902 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001903 * batadv_bla_tx checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001904 * * a claim was received which has to be processed
1905 * * the frame is allowed on the mesh
1906 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001907 * in these cases, the skb is further handled by this function.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001908 *
1909 * This call might reallocate skb data.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001910 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001911 * Return: true if handled, otherwise it returns false and the caller shall
1912 * further process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001913 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001914bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1915 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001916{
1917 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001918 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001919 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001920 struct batadv_hard_iface *primary_if;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001921 bool client_roamed;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001922 bool ret = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001923
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001924 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001925 if (!primary_if)
1926 goto out;
1927
1928 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1929 goto allow;
1930
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001931 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001932 goto handled;
1933
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001934 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001935
Sven Eckelmann807736f2012-07-15 22:26:51 +02001936 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001937 /* don't allow broadcasts while requests are in flight */
1938 if (is_multicast_ether_addr(ethhdr->h_dest))
1939 goto handled;
1940
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001941 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001942 search_claim.vid = vid;
1943
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001944 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001945
1946 /* if no claim exists, allow it. */
1947 if (!claim)
1948 goto allow;
1949
1950 /* check if we are responsible. */
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001951 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1952 client_roamed = batadv_compare_eth(backbone_gw->orig,
1953 primary_if->net_dev->dev_addr);
1954 batadv_backbone_gw_put(backbone_gw);
1955
1956 if (client_roamed) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001957 /* if yes, the client has roamed and we have
1958 * to unclaim it.
1959 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001960 batadv_handle_unclaim(bat_priv, primary_if,
1961 primary_if->net_dev->dev_addr,
1962 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001963 goto allow;
1964 }
1965
1966 /* check if it is a multicast/broadcast frame */
1967 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1968 /* drop it. the responsible gateway has forwarded it into
1969 * the backbone network.
1970 */
1971 goto handled;
1972 } else {
1973 /* we must allow it. at least if we are
1974 * responsible for the DESTINATION.
1975 */
1976 goto allow;
1977 }
1978allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001979 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001980 ret = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001981 goto out;
1982handled:
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001983 ret = true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001984out:
1985 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001986 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001987 if (claim)
Sven Eckelmann321e3e02016-01-17 11:01:16 +01001988 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001989 return ret;
1990}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001991
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001992/**
1993 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1994 * @seq: seq file to print on
1995 * @offset: not used
1996 *
1997 * Return: always 0
1998 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001999int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002000{
2001 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002002 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002003 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002004 struct batadv_bla_backbone_gw *backbone_gw;
Marek Lindner712bbfe42012-12-25 17:03:25 +08002005 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002006 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002007 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002008 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002009 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002010 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002011 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002012
Marek Lindner30da63a2012-08-03 17:15:46 +02002013 primary_if = batadv_seq_print_text_primary_if_get(seq);
2014 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002015 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002016
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002017 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01002018 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002019 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002020 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002021 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002022 seq_puts(seq,
2023 " Client VID Originator [o] (CRC )\n");
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002024 for (i = 0; i < hash->size; i++) {
2025 head = &hash->table[i];
2026
2027 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002028 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002029 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2030
2031 is_own = batadv_compare_eth(backbone_gw->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002032 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002033
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002034 spin_lock_bh(&backbone_gw->crc_lock);
2035 backbone_crc = backbone_gw->crc;
2036 spin_unlock_bh(&backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02002037 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02002038 claim->addr, BATADV_PRINT_VID(claim->vid),
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002039 backbone_gw->orig,
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002040 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002041 backbone_crc);
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002042
2043 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002044 }
2045 rcu_read_unlock();
2046 }
2047out:
2048 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01002049 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02002050 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002051}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002052
Simon Wunderlich04e14be2015-11-06 10:45:19 +01002053/**
2054 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
2055 * file
2056 * @seq: seq file to print on
2057 * @offset: not used
2058 *
2059 * Return: always 0
2060 */
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002061int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
2062{
2063 struct net_device *net_dev = (struct net_device *)seq->private;
2064 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002065 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08002066 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002067 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002068 struct hlist_head *head;
2069 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002070 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002071 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002072 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002073 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002074
Marek Lindner30da63a2012-08-03 17:15:46 +02002075 primary_if = batadv_seq_print_text_primary_if_get(seq);
2076 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002077 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002078
2079 primary_addr = primary_if->net_dev->dev_addr;
2080 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002081 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002082 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002083 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002084 seq_puts(seq, " Originator VID last seen (CRC )\n");
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002085 for (i = 0; i < hash->size; i++) {
2086 head = &hash->table[i];
2087
2088 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002089 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002090 msecs = jiffies_to_msecs(jiffies -
2091 backbone_gw->lasttime);
2092 secs = msecs / 1000;
2093 msecs = msecs % 1000;
2094
2095 is_own = batadv_compare_eth(backbone_gw->orig,
2096 primary_addr);
2097 if (is_own)
2098 continue;
2099
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002100 spin_lock_bh(&backbone_gw->crc_lock);
2101 backbone_crc = backbone_gw->crc;
2102 spin_unlock_bh(&backbone_gw->crc_lock);
2103
Antonio Quartullieb2deb62013-04-19 18:07:00 +02002104 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02002105 backbone_gw->orig,
2106 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002107 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002108 }
2109 rcu_read_unlock();
2110 }
2111out:
2112 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01002113 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02002114 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002115}