blob: ad2ffe16d29f47edc1c1604f45ae58aa2375501a [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"
Sven Eckelmannba412082016-05-15 23:48:31 +020051#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020052#include "originator.h"
53#include "packet.h"
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +010054#include "sysfs.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020055#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010056
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020057static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010058
Sven Eckelmann3b300de2012-05-12 18:33:53 +020059static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080060static void
61batadv_bla_send_announce(struct batadv_priv *bat_priv,
62 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010063
Sven Eckelmann62fe7102015-09-15 19:00:48 +020064/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010065 * batadv_choose_claim - choose the right bucket for a claim.
66 * @data: data to hash
67 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020068 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010069 * Return: the hash index of the claim
Sven Eckelmann62fe7102015-09-15 19:00:48 +020070 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020071static inline u32 batadv_choose_claim(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010072{
Marek Lindner712bbfe42012-12-25 17:03:25 +080073 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020074 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010075
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010076 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
77 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010078
79 return hash % size;
80}
81
Sven Eckelmann62fe7102015-09-15 19:00:48 +020082/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010083 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
84 * @data: data to hash
85 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020086 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010087 * Return: the hash index of the backbone gateway
Sven Eckelmann62fe7102015-09-15 19:00:48 +020088 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020089static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010090{
Marek Lindner712bbfe42012-12-25 17:03:25 +080091 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020092 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010093
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010094 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
95 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010096
97 return hash % size;
98}
99
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100100/**
101 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
102 * @node: list node of the first entry to compare
103 * @data2: pointer to the second backbone gateway
104 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100105 * Return: true if the backbones have the same data, false otherwise
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100106 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100107static bool batadv_compare_backbone_gw(const struct hlist_node *node,
108 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100109{
Marek Lindnerbae98772012-12-25 17:03:24 +0800110 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +0100111 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200112 const struct batadv_bla_backbone_gw *gw1 = data1;
113 const struct batadv_bla_backbone_gw *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100114
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200115 if (!batadv_compare_eth(gw1->orig, gw2->orig))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100116 return false;
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200117
118 if (gw1->vid != gw2->vid)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100119 return false;
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200120
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100121 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100122}
123
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100124/**
Sven Eckelmann98a5b1d2016-02-22 18:55:32 +0100125 * batadv_compare_claim - compare address and vid of two claims
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100126 * @node: list node of the first entry to compare
127 * @data2: pointer to the second claims
128 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100129 * Return: true if the claim have the same data, 0 otherwise
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100130 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100131static bool batadv_compare_claim(const struct hlist_node *node,
132 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100133{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800134 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +0100135 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200136 const struct batadv_bla_claim *cl1 = data1;
137 const struct batadv_bla_claim *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100138
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200139 if (!batadv_compare_eth(cl1->addr, cl2->addr))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100140 return false;
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200141
142 if (cl1->vid != cl2->vid)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100143 return false;
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200144
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100145 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100146}
147
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100148/**
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100149 * batadv_backbone_gw_release - release backbone gw from lists and queue for
150 * free after rcu grace period
151 * @ref: kref pointer of the backbone gw
152 */
153static void batadv_backbone_gw_release(struct kref *ref)
154{
155 struct batadv_bla_backbone_gw *backbone_gw;
156
157 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
158 refcount);
159
160 kfree_rcu(backbone_gw, rcu);
161}
162
163/**
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100164 * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
165 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100166 * @backbone_gw: backbone gateway to be free'd
167 */
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100168static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100169{
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100170 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
Simon Wunderlich23721382012-01-22 20:00:19 +0100171}
172
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100173/**
174 * batadv_claim_release - release claim from lists and queue for free after rcu
175 * grace period
176 * @ref: kref pointer of the claim
177 */
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100178static void batadv_claim_release(struct kref *ref)
Simon Wunderlich23721382012-01-22 20:00:19 +0100179{
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100180 struct batadv_bla_claim *claim;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200181 struct batadv_bla_backbone_gw *old_backbone_gw;
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100182
183 claim = container_of(ref, struct batadv_bla_claim, refcount);
184
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200185 spin_lock_bh(&claim->backbone_lock);
186 old_backbone_gw = claim->backbone_gw;
187 claim->backbone_gw = NULL;
188 spin_unlock_bh(&claim->backbone_lock);
189
190 spin_lock_bh(&old_backbone_gw->crc_lock);
191 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
192 spin_unlock_bh(&old_backbone_gw->crc_lock);
193
194 batadv_backbone_gw_put(old_backbone_gw);
195
Sven Eckelmann63b39922016-01-14 15:28:19 +0100196 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100197}
198
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100199/**
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100200 * batadv_claim_put - decrement the claim refcounter and possibly
Sven Eckelmannec9b83c2016-01-05 12:06:16 +0100201 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100202 * @claim: claim to be free'd
203 */
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100204static void batadv_claim_put(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100205{
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100206 kref_put(&claim->refcount, batadv_claim_release);
Simon Wunderlich23721382012-01-22 20:00:19 +0100207}
208
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100209/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100210 * batadv_claim_hash_find - looks for a claim in the claim hash
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100211 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100212 * @data: search data (may be local/static data)
213 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200214 * Return: claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100215 */
Sven Eckelmann6fc77a52016-03-05 15:56:01 +0100216static struct batadv_bla_claim *
217batadv_claim_hash_find(struct batadv_priv *bat_priv,
218 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100219{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200220 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100221 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800222 struct batadv_bla_claim *claim;
223 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100224 int index;
225
226 if (!hash)
227 return NULL;
228
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200229 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100230 head = &hash->table[index];
231
232 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800233 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200234 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100235 continue;
236
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100237 if (!kref_get_unless_zero(&claim->refcount))
Simon Wunderlich23721382012-01-22 20:00:19 +0100238 continue;
239
240 claim_tmp = claim;
241 break;
242 }
243 rcu_read_unlock();
244
245 return claim_tmp;
246}
247
Ben Hutchings2c530402012-07-10 10:55:09 +0000248/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100249 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
Ben Hutchings2c530402012-07-10 10:55:09 +0000250 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100251 * @addr: the address of the originator
252 * @vid: the VLAN ID
253 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100254 * Return: backbone gateway if found or NULL otherwise
Simon Wunderlich23721382012-01-22 20:00:19 +0100255 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800256static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200257batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
258 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100259{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200260 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100261 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800262 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
263 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100264 int index;
265
266 if (!hash)
267 return NULL;
268
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100269 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100270 search_entry.vid = vid;
271
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200272 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100273 head = &hash->table[index];
274
275 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800276 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200277 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
278 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100279 continue;
280
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100281 if (!kref_get_unless_zero(&backbone_gw->refcount))
Simon Wunderlich23721382012-01-22 20:00:19 +0100282 continue;
283
284 backbone_gw_tmp = backbone_gw;
285 break;
286 }
287 rcu_read_unlock();
288
289 return backbone_gw_tmp;
290}
291
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100292/**
293 * batadv_bla_del_backbone_claims - delete all claims for a backbone
294 * @backbone_gw: backbone gateway where the claims should be removed
295 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200296static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800297batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100298{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200299 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800300 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100301 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800302 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100303 int i;
304 spinlock_t *list_lock; /* protects write access to the hash lists */
305
Sven Eckelmann807736f2012-07-15 22:26:51 +0200306 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100307 if (!hash)
308 return;
309
310 for (i = 0; i < hash->size; i++) {
311 head = &hash->table[i];
312 list_lock = &hash->list_locks[i];
313
314 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800315 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100316 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100317 if (claim->backbone_gw != backbone_gw)
318 continue;
319
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100320 batadv_claim_put(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800321 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100322 }
323 spin_unlock_bh(list_lock);
324 }
325
Antonio Quartulli3f687852014-11-02 11:29:56 +0100326 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200327 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200328 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200329 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100330}
331
Ben Hutchings2c530402012-07-10 10:55:09 +0000332/**
333 * batadv_bla_send_claim - sends a claim frame according to the provided info
334 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200335 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100336 * @vid: the VLAN ID
337 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100338 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200339static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200340 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100341{
342 struct sk_buff *skb;
343 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200344 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100345 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200346 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200347 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100348 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100349
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200350 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100351 if (!primary_if)
352 return;
353
Sven Eckelmann807736f2012-07-15 22:26:51 +0200354 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100355 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100356 local_claim_dest.type = claimtype;
357
358 soft_iface = primary_if->soft_iface;
359
360 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
361 /* IP DST: 0.0.0.0 */
362 zeroip,
363 primary_if->soft_iface,
364 /* IP SRC: 0.0.0.0 */
365 zeroip,
366 /* Ethernet DST: Broadcast */
367 NULL,
368 /* Ethernet SRC/HW SRC: originator mac */
369 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200370 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100371 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100372 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100373 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200374 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100375
376 if (!skb)
377 goto out;
378
379 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200380 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100381
382 /* now we pretend that the client would have sent this ... */
383 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200384 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100385 /* normal claim frame
386 * set Ethernet SRC to the clients mac
387 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100388 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200389 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200390 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
391 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100392 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200393 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100394 /* unclaim frame
395 * set HW SRC to the clients mac
396 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100397 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200398 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200399 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200400 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100401 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200402 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100403 /* announcement frame
404 * set HW SRC to the special mac containg the crc
405 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100406 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200407 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200408 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200409 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100410 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200411 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100412 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200413 * set HW SRC and header destination to the receiving backbone
414 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100415 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100416 ether_addr_copy(hw_src, mac);
417 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200418 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200419 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200420 ethhdr->h_source, ethhdr->h_dest,
421 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100422 break;
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100423 case BATADV_CLAIM_TYPE_LOOPDETECT:
424 ether_addr_copy(ethhdr->h_source, mac);
425 batadv_dbg(BATADV_DBG_BLA, bat_priv,
426 "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
427 ethhdr->h_source, ethhdr->h_dest,
428 BATADV_PRINT_VID(vid));
429
430 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100431 }
432
Sven Eckelmann10c78f52016-07-02 09:52:13 +0200433 if (vid & BATADV_VLAN_HAS_TAG) {
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200434 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
435 vid & VLAN_VID_MASK);
Sven Eckelmann10c78f52016-07-02 09:52:13 +0200436 if (!skb)
437 goto out;
438 }
Simon Wunderlich23721382012-01-22 20:00:19 +0100439
440 skb_reset_mac_header(skb);
441 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200442 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
443 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
444 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100445 soft_iface->last_rx = jiffies;
446
447 netif_rx(skb);
448out:
449 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100450 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100451}
452
Ben Hutchings2c530402012-07-10 10:55:09 +0000453/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100454 * batadv_bla_loopdetect_report - worker for reporting the loop
455 * @work: work queue item
456 *
457 * Throws an uevent, as the loopdetect check function can't do that itself
458 * since the kernel may sleep while throwing uevents.
459 */
460static void batadv_bla_loopdetect_report(struct work_struct *work)
461{
462 struct batadv_bla_backbone_gw *backbone_gw;
463 struct batadv_priv *bat_priv;
464 char vid_str[6] = { '\0' };
465
466 backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
467 report_work);
468 bat_priv = backbone_gw->bat_priv;
469
470 batadv_info(bat_priv->soft_iface,
471 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
472 BATADV_PRINT_VID(backbone_gw->vid));
473 snprintf(vid_str, sizeof(vid_str), "%d",
474 BATADV_PRINT_VID(backbone_gw->vid));
475 vid_str[sizeof(vid_str) - 1] = 0;
476
477 batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
478 vid_str);
479
480 batadv_backbone_gw_put(backbone_gw);
481}
482
483/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100484 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
Ben Hutchings2c530402012-07-10 10:55:09 +0000485 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100486 * @orig: the mac address of the originator
487 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200488 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100489 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100490 * Return: the (possibly created) backbone gateway or NULL on error
Simon Wunderlich23721382012-01-22 20:00:19 +0100491 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800492static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200493batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200494 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100495{
Marek Lindnerbae98772012-12-25 17:03:24 +0800496 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200497 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100498 int hash_added;
499
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200500 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100501
502 if (entry)
503 return entry;
504
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200505 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200506 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200507 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100508
509 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
510 if (!entry)
511 return NULL;
512
513 entry->vid = vid;
514 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200515 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100516 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200517 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100518 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200519 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100520 ether_addr_copy(entry->orig, orig);
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +0100521 INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
Simon Wunderlich23721382012-01-22 20:00:19 +0100522
523 /* one for the hash, one for returning */
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100524 kref_init(&entry->refcount);
525 kref_get(&entry->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100526
Sven Eckelmann807736f2012-07-15 22:26:51 +0200527 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200528 batadv_compare_backbone_gw,
529 batadv_choose_backbone_gw, entry,
530 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100531
532 if (unlikely(hash_added != 0)) {
533 /* hash failed, free the structure */
534 kfree(entry);
535 return NULL;
536 }
537
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200538 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200539 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100540 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200541 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200542 "became a backbone gateway");
Sven Eckelmann5d967312016-01-17 11:01:09 +0100543 batadv_orig_node_put(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100544 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200545
Simon Wunderlichd807f272012-09-09 22:27:57 +0200546 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200547 batadv_bla_send_announce(bat_priv, entry);
548
Simon Wunderlichd807f272012-09-09 22:27:57 +0200549 /* this will be decreased in the worker thread */
550 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200551 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200552 atomic_inc(&bat_priv->bla.num_requests);
553 }
554
Simon Wunderlich23721382012-01-22 20:00:19 +0100555 return entry;
556}
557
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100558/**
559 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
560 * @bat_priv: the bat priv with all the soft interface information
561 * @primary_if: the selected primary interface
562 * @vid: VLAN identifier
563 *
564 * update or add the own backbone gw to make sure we announce
Simon Wunderlich23721382012-01-22 20:00:19 +0100565 * where we receive other backbone gws
566 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200567static void
568batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
569 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200570 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100571{
Marek Lindnerbae98772012-12-25 17:03:24 +0800572 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100573
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200574 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
575 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200576 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100577 if (unlikely(!backbone_gw))
578 return;
579
580 backbone_gw->lasttime = jiffies;
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100581 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100582}
583
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100584/**
585 * batadv_bla_answer_request - answer a bla request by sending own claims
586 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200587 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100588 * @vid: the vid where the request came on
589 *
590 * Repeat all of our own claims, and finally send an ANNOUNCE frame
591 * to allow the requester another check if the CRC is correct now.
592 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200593static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
594 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200595 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100596{
Simon Wunderlich23721382012-01-22 20:00:19 +0100597 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200598 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800599 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800600 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100601 int i;
602
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200603 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200604 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100605
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200606 backbone_gw = batadv_backbone_hash_find(bat_priv,
607 primary_if->net_dev->dev_addr,
608 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100609 if (!backbone_gw)
610 return;
611
Sven Eckelmann807736f2012-07-15 22:26:51 +0200612 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100613 for (i = 0; i < hash->size; i++) {
614 head = &hash->table[i];
615
616 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800617 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100618 /* only own claims are interesting */
619 if (claim->backbone_gw != backbone_gw)
620 continue;
621
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200622 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200623 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100624 }
625 rcu_read_unlock();
626 }
627
628 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200629 batadv_bla_send_announce(bat_priv, backbone_gw);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100630 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100631}
632
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100633/**
634 * batadv_bla_send_request - send a request to repeat claims
635 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100636 *
637 * When the crc is wrong, ask the backbone gateway for a full table update.
638 * After the request, it will repeat all of his own claims and finally
639 * send an announcement claim with which we can check again.
640 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800641static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100642{
643 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200644 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100645
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200646 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
647 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100648
649 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200650 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200651 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100652
653 /* no local broadcasts should be sent or received, for now. */
654 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200655 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100656 atomic_set(&backbone_gw->request_sent, 1);
657 }
658}
659
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100660/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100661 * batadv_bla_send_announce - Send an announcement frame
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100662 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100663 * @backbone_gw: our backbone gateway which should be announced
Simon Wunderlich23721382012-01-22 20:00:19 +0100664 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200665static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800666 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100667{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200668 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100669 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100670
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200671 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200672 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100673 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200674 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100675 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100676
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200677 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200678 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100679}
680
Ben Hutchings2c530402012-07-10 10:55:09 +0000681/**
682 * batadv_bla_add_claim - Adds a claim in the claim hash
683 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100684 * @mac: the mac address of the claim
685 * @vid: the VLAN ID of the frame
686 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100687 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200688static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200689 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800690 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100691{
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200692 struct batadv_bla_backbone_gw *old_backbone_gw;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800693 struct batadv_bla_claim *claim;
694 struct batadv_bla_claim search_claim;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200695 bool remove_crc = false;
Simon Wunderlich23721382012-01-22 20:00:19 +0100696 int hash_added;
697
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100698 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100699 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200700 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100701
702 /* create a new claim entry if it does not exist yet. */
703 if (!claim) {
704 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
705 if (!claim)
706 return;
707
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100708 ether_addr_copy(claim->addr, mac);
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200709 spin_lock_init(&claim->backbone_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100710 claim->vid = vid;
711 claim->lasttime = jiffies;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200712 kref_get(&backbone_gw->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100713 claim->backbone_gw = backbone_gw;
714
Sven Eckelmann71b7e3d2016-01-16 10:29:44 +0100715 kref_init(&claim->refcount);
716 kref_get(&claim->refcount);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200717 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200718 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200719 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200720 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200721 batadv_compare_claim,
722 batadv_choose_claim, claim,
723 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100724
725 if (unlikely(hash_added != 0)) {
726 /* only local changes happened. */
727 kfree(claim);
728 return;
729 }
730 } else {
731 claim->lasttime = jiffies;
732 if (claim->backbone_gw == backbone_gw)
733 /* no need to register a new backbone */
734 goto claim_free_ref;
735
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200736 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200737 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200738 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100739
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200740 remove_crc = true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100741 }
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200742
743 /* replace backbone_gw atomically and adjust reference counters */
744 spin_lock_bh(&claim->backbone_lock);
745 old_backbone_gw = claim->backbone_gw;
Sven Eckelmann06e56de2016-01-16 10:29:43 +0100746 kref_get(&backbone_gw->refcount);
Simon Wunderlich23721382012-01-22 20:00:19 +0100747 claim->backbone_gw = backbone_gw;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200748 spin_unlock_bh(&claim->backbone_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100749
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200750 if (remove_crc) {
751 /* remove claim address from old backbone_gw */
752 spin_lock_bh(&old_backbone_gw->crc_lock);
753 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
754 spin_unlock_bh(&old_backbone_gw->crc_lock);
755 }
756
757 batadv_backbone_gw_put(old_backbone_gw);
758
759 /* add claim address to new backbone_gw */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200760 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100761 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200762 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100763 backbone_gw->lasttime = jiffies;
764
765claim_free_ref:
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100766 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100767}
768
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100769/**
Sven Eckelmann3db0dec2016-07-01 15:49:43 +0200770 * batadv_bla_claim_get_backbone_gw - Get valid reference for backbone_gw of
771 * claim
772 * @claim: claim whose backbone_gw should be returned
773 *
774 * Return: valid reference to claim::backbone_gw
775 */
776static struct batadv_bla_backbone_gw *
777batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
778{
779 struct batadv_bla_backbone_gw *backbone_gw;
780
781 spin_lock_bh(&claim->backbone_lock);
782 backbone_gw = claim->backbone_gw;
783 kref_get(&backbone_gw->refcount);
784 spin_unlock_bh(&claim->backbone_lock);
785
786 return backbone_gw;
787}
788
789/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100790 * batadv_bla_del_claim - delete a claim from the claim hash
791 * @bat_priv: the bat priv with all the soft interface information
792 * @mac: mac address of the claim to be removed
793 * @vid: VLAN id for the claim to be removed
Simon Wunderlich23721382012-01-22 20:00:19 +0100794 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200795static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200796 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100797{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800798 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100799
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100800 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100801 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200802 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100803 if (!claim)
804 return;
805
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200806 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200807 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100808
Sven Eckelmann807736f2012-07-15 22:26:51 +0200809 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200810 batadv_choose_claim, claim);
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100811 batadv_claim_put(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100812
Simon Wunderlich23721382012-01-22 20:00:19 +0100813 /* don't need the reference from hash_find() anymore */
Sven Eckelmann321e3e02016-01-17 11:01:16 +0100814 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100815}
816
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200817/**
818 * batadv_handle_announce - check for ANNOUNCE frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100819 * @bat_priv: the bat priv with all the soft interface information
820 * @an_addr: announcement mac address (ARP Sender HW address)
821 * @backbone_addr: originator address of the sender (Ethernet source MAC)
822 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200823 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100824 * Return: true if handled
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200825 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100826static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
827 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100828{
Marek Lindnerbae98772012-12-25 17:03:24 +0800829 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200830 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100831
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200832 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100833 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +0100834
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200835 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
836 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100837
838 if (unlikely(!backbone_gw))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100839 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100840
Simon Wunderlich23721382012-01-22 20:00:19 +0100841 /* handle as ANNOUNCE frame */
842 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100843 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100844
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200845 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100846 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200847 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100848
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200849 spin_lock_bh(&backbone_gw->crc_lock);
850 backbone_crc = backbone_gw->crc;
851 spin_unlock_bh(&backbone_gw->crc_lock);
852
853 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200854 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100855 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200856 backbone_gw->orig,
857 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200858 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100859
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200860 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100861 } else {
862 /* if we have sent a request and the crc was OK,
863 * we can allow traffic again.
864 */
865 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200866 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100867 atomic_set(&backbone_gw->request_sent, 0);
868 }
869 }
870
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100871 batadv_backbone_gw_put(backbone_gw);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100872 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100873}
874
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200875/**
876 * batadv_handle_request - check for REQUEST frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100877 * @bat_priv: the bat priv with all the soft interface information
878 * @primary_if: the primary hard interface of this batman soft interface
879 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
880 * @ethhdr: ethernet header of a packet
881 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200882 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100883 * Return: true if handled
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200884 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100885static bool batadv_handle_request(struct batadv_priv *bat_priv,
886 struct batadv_hard_iface *primary_if,
887 u8 *backbone_addr, struct ethhdr *ethhdr,
888 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100889{
890 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200891 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100892 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +0100893
894 /* sanity check, this should not happen on a normal switch,
895 * we ignore it in this case.
896 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200897 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100898 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100899
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200900 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200901 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200902 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100903
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200904 batadv_bla_answer_request(bat_priv, primary_if, vid);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100905 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100906}
907
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200908/**
909 * batadv_handle_unclaim - check for UNCLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100910 * @bat_priv: the bat priv with all the soft interface information
911 * @primary_if: the primary hard interface of this batman soft interface
912 * @backbone_addr: originator address of the backbone (Ethernet source)
913 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
914 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200915 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100916 * Return: true if handled
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200917 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100918static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
919 struct batadv_hard_iface *primary_if,
920 u8 *backbone_addr, u8 *claim_addr,
921 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100922{
Marek Lindnerbae98772012-12-25 17:03:24 +0800923 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100924
925 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200926 if (primary_if && batadv_compare_eth(backbone_addr,
927 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200928 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200929 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100930
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200931 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100932
933 if (!backbone_gw)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100934 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100935
936 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200937 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200938 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200939 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100940
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200941 batadv_bla_del_claim(bat_priv, claim_addr, vid);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100942 batadv_backbone_gw_put(backbone_gw);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100943 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100944}
945
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200946/**
947 * batadv_handle_claim - check for CLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100948 * @bat_priv: the bat priv with all the soft interface information
949 * @primary_if: the primary hard interface of this batman soft interface
950 * @backbone_addr: originator address of the backbone (Ethernet Source)
951 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
952 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200953 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100954 * Return: true if handled
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200955 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100956static bool batadv_handle_claim(struct batadv_priv *bat_priv,
957 struct batadv_hard_iface *primary_if,
958 u8 *backbone_addr, u8 *claim_addr,
959 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100960{
Marek Lindnerbae98772012-12-25 17:03:24 +0800961 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100962
963 /* register the gateway if not yet available, and add the claim. */
964
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200965 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
966 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100967
968 if (unlikely(!backbone_gw))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100969 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100970
971 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200972 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200973 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200974 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200975 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100976
977 /* TODO: we could call something like tt_local_del() here. */
978
Sven Eckelmannc8b86c12016-01-17 11:01:15 +0100979 batadv_backbone_gw_put(backbone_gw);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100980 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +0100981}
982
Ben Hutchings2c530402012-07-10 10:55:09 +0000983/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100984 * batadv_check_claim_group - check for claim group membership
Ben Hutchings2c530402012-07-10 10:55:09 +0000985 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200986 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100987 * @hw_src: the Hardware source in the ARP Header
988 * @hw_dst: the Hardware destination in the ARP Header
989 * @ethhdr: pointer to the Ethernet header of the claim frame
990 *
991 * checks if it is a claim packet and if its on the same group.
992 * This function also applies the group ID of the sender
993 * if it is in the same mesh.
994 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200995 * Return:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100996 * 2 - if it is a claim packet and on the same group
997 * 1 - if is a claim packet from another group
998 * 0 - if it is not a claim packet
999 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001000static int batadv_check_claim_group(struct batadv_priv *bat_priv,
1001 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001002 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001003 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001004{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001005 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001006 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +02001007 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001008
Sven Eckelmann96412692012-06-05 22:31:30 +02001009 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001010 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001011
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001012 /* if announcement packet, use the source,
1013 * otherwise assume it is in the hw_src
1014 */
1015 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001016 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001017 backbone_addr = hw_src;
1018 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001019 case BATADV_CLAIM_TYPE_REQUEST:
1020 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001021 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001022 backbone_addr = ethhdr->h_source;
1023 break;
1024 default:
1025 return 0;
1026 }
1027
1028 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001029 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001030 return 0;
1031
1032 /* if its already the same group, it is fine. */
1033 if (bla_dst->group == bla_dst_own->group)
1034 return 2;
1035
1036 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +02001037 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001038
1039 /* dont accept claims from gateways which are not in
1040 * the same mesh or group.
1041 */
1042 if (!orig_node)
1043 return 1;
1044
1045 /* if our mesh friends mac is bigger, use it for ourselves. */
1046 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001047 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001048 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001049 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001050 bla_dst_own->group = bla_dst->group;
1051 }
1052
Sven Eckelmann5d967312016-01-17 11:01:09 +01001053 batadv_orig_node_put(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001054
1055 return 2;
1056}
1057
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001058/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001059 * batadv_bla_process_claim - Check if this is a claim frame, and process it
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001060 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +02001061 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +01001062 * @skb: the frame to be checked
1063 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001064 * Return: true if it was a claim frame, otherwise return false to
Simon Wunderlich23721382012-01-22 20:00:19 +01001065 * tell the callee that it can use the frame on its own.
1066 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001067static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
1068 struct batadv_hard_iface *primary_if,
1069 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +01001070{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001071 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001072 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001073 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001074 struct ethhdr *ethhdr;
1075 struct arphdr *arphdr;
1076 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001077 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +02001078 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +01001079 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001080 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001081
Antonio Quartullic018ad32013-06-04 12:11:39 +02001082 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001083 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001084
Antonio Quartullic018ad32013-06-04 12:11:39 +02001085 proto = ethhdr->h_proto;
1086 headlen = ETH_HLEN;
1087 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001088 /* Traverse the VLAN/Ethertypes.
1089 *
1090 * At this point it is known that the first protocol is a VLAN
1091 * header, so start checking at the encapsulated protocol.
1092 *
1093 * The depth of the VLAN headers is recorded to drop BLA claim
1094 * frames encapsulated into multiple VLAN headers (QinQ).
1095 */
1096 do {
1097 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1098 &vhdr_buf);
1099 if (!vhdr)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001100 return false;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001101
1102 proto = vhdr->h_vlan_encapsulated_proto;
1103 headlen += VLAN_HLEN;
1104 vlan_depth++;
1105 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +01001106 }
1107
Antonio Quartulli293e9332013-05-19 12:55:16 +02001108 if (proto != htons(ETH_P_ARP))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001109 return false; /* not a claim frame */
Simon Wunderlich23721382012-01-22 20:00:19 +01001110
1111 /* this must be a ARP frame. check if it is a claim. */
1112
1113 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001114 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001115
1116 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001117 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001118 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +01001119
1120 /* Check whether the ARP frame carries a valid
1121 * IP information
1122 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001123 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001124 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001125 if (arphdr->ar_pro != htons(ETH_P_IP))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001126 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001127 if (arphdr->ar_hln != ETH_ALEN)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001128 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001129 if (arphdr->ar_pln != 4)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001130 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001131
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001132 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001133 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +02001134 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001135 bla_dst_own = &bat_priv->bla.claim_dest;
1136
1137 /* check if it is a claim frame in general */
1138 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1139 sizeof(bla_dst->magic)) != 0)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001140 return false;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001141
1142 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1143 * drop that, as this is not supported by BLA but should also not be
1144 * sent via the mesh.
1145 */
1146 if (vlan_depth > 1)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001147 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001148
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001149 /* Let the loopdetect frames on the mesh in any case. */
1150 if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
1151 return 0;
1152
Simon Wunderlich23721382012-01-22 20:00:19 +01001153 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001154 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1155 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001156 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001157 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001158 "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 +02001159 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1160 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001161
1162 if (ret < 2)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001163 return !!ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001164
1165 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001166 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001167
1168 /* check for the different types of claim frames ... */
1169 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001170 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001171 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1172 ethhdr->h_source, vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001173 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001174 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001175 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001176 if (batadv_handle_unclaim(bat_priv, primary_if,
1177 ethhdr->h_source, hw_src, vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001178 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001179 break;
1180
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001181 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001182 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1183 vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001184 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001185 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001186 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001187 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1188 vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001189 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001190 break;
1191 }
1192
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001193 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001194 "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 +02001195 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001196 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001197}
1198
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001199/**
1200 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1201 * immediately
1202 * @bat_priv: the bat priv with all the soft interface information
1203 * @now: whether the whole hash shall be wiped now
1204 *
1205 * Check when we last heard from other nodes, and remove them in case of
Simon Wunderlich23721382012-01-22 20:00:19 +01001206 * a time out, or clean all backbone gws if now is set.
1207 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001208static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001209{
Marek Lindnerbae98772012-12-25 17:03:24 +08001210 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001211 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001212 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001213 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001214 spinlock_t *list_lock; /* protects write access to the hash lists */
1215 int i;
1216
Sven Eckelmann807736f2012-07-15 22:26:51 +02001217 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001218 if (!hash)
1219 return;
1220
1221 for (i = 0; i < hash->size; i++) {
1222 head = &hash->table[i];
1223 list_lock = &hash->list_locks[i];
1224
1225 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001226 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001227 head, hash_entry) {
1228 if (now)
1229 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001230 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001231 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001232 continue;
1233
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001234 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001235 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1236 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001237
1238purge_now:
1239 /* don't wait for the pending request anymore */
1240 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001241 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001242
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001243 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001244
Sasha Levinb67bfe02013-02-27 17:06:00 -08001245 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmannc8b86c12016-01-17 11:01:15 +01001246 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001247 }
1248 spin_unlock_bh(list_lock);
1249 }
1250}
1251
Ben Hutchings2c530402012-07-10 10:55:09 +00001252/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001253 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
Ben Hutchings2c530402012-07-10 10:55:09 +00001254 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001255 * @primary_if: the selected primary interface, may be NULL if now is set
1256 * @now: whether the whole hash shall be wiped now
1257 *
1258 * Check when we heard last time from our own claims, and remove them in case of
1259 * a time out, or clean all claims if now is set
1260 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001261static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1262 struct batadv_hard_iface *primary_if,
1263 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001264{
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001265 struct batadv_bla_backbone_gw *backbone_gw;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001266 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001267 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001268 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001269 int i;
1270
Sven Eckelmann807736f2012-07-15 22:26:51 +02001271 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001272 if (!hash)
1273 return;
1274
1275 for (i = 0; i < hash->size; i++) {
1276 head = &hash->table[i];
1277
1278 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001279 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001280 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001281 if (now)
1282 goto purge_now;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001283
1284 if (!batadv_compare_eth(backbone_gw->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001285 primary_if->net_dev->dev_addr))
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001286 goto skip;
1287
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001288 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001289 BATADV_BLA_CLAIM_TIMEOUT))
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001290 goto skip;
Simon Wunderlich23721382012-01-22 20:00:19 +01001291
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001292 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001293 "bla_purge_claims(): %pM, vid %d, time out\n",
1294 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001295
1296purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001297 batadv_handle_unclaim(bat_priv, primary_if,
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001298 backbone_gw->orig,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001299 claim->addr, claim->vid);
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001300skip:
1301 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001302 }
1303 rcu_read_unlock();
1304 }
1305}
1306
Ben Hutchings2c530402012-07-10 10:55:09 +00001307/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001308 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1309 * originator address changes
Ben Hutchings2c530402012-07-10 10:55:09 +00001310 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001311 * @primary_if: the new selected primary_if
1312 * @oldif: the old primary interface, may be NULL
Simon Wunderlich23721382012-01-22 20:00:19 +01001313 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001314void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1315 struct batadv_hard_iface *primary_if,
1316 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001317{
Marek Lindnerbae98772012-12-25 17:03:24 +08001318 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001319 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001320 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001321 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001322 int i;
1323
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001324 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001325 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1326 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001327
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001328 /* purge everything when bridge loop avoidance is turned off */
1329 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1330 oldif = NULL;
1331
Simon Wunderlich23721382012-01-22 20:00:19 +01001332 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001333 batadv_bla_purge_claims(bat_priv, NULL, 1);
1334 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001335 return;
1336 }
1337
Sven Eckelmann807736f2012-07-15 22:26:51 +02001338 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001339 if (!hash)
1340 return;
1341
1342 for (i = 0; i < hash->size; i++) {
1343 head = &hash->table[i];
1344
1345 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001346 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001347 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001348 if (!batadv_compare_eth(backbone_gw->orig,
1349 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001350 continue;
1351
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001352 ether_addr_copy(backbone_gw->orig,
1353 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001354 /* send an announce frame so others will ask for our
1355 * claims and update their tables.
1356 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001357 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001358 }
1359 rcu_read_unlock();
1360 }
1361}
1362
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001363/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001364 * batadv_bla_send_loopdetect - send a loopdetect frame
1365 * @bat_priv: the bat priv with all the soft interface information
1366 * @backbone_gw: the backbone gateway for which a loop should be detected
1367 *
1368 * To detect loops that the bridge loop avoidance can't handle, send a loop
1369 * detection packet on the backbone. Unlike other BLA frames, this frame will
1370 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1371 * indicates that there is a loop.
1372 */
1373static void
1374batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
1375 struct batadv_bla_backbone_gw *backbone_gw)
1376{
1377 batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
1378 backbone_gw->vid);
1379 batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
1380 backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
1381}
1382
1383/**
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001384 * batadv_bla_status_update - purge bla interfaces if necessary
1385 * @net_dev: the soft interface net device
1386 */
1387void batadv_bla_status_update(struct net_device *net_dev)
1388{
1389 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1390 struct batadv_hard_iface *primary_if;
1391
1392 primary_if = batadv_primary_if_get_selected(bat_priv);
1393 if (!primary_if)
1394 return;
1395
1396 /* this function already purges everything when bla is disabled,
1397 * so just call that one.
1398 */
1399 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001400 batadv_hardif_put(primary_if);
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001401}
1402
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001403/**
1404 * batadv_bla_periodic_work - performs periodic bla work
1405 * @work: kernel work struct
1406 *
1407 * periodic work to do:
Simon Wunderlich23721382012-01-22 20:00:19 +01001408 * * purge structures when they are too old
1409 * * send announcements
1410 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001411static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001412{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001413 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001414 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001415 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001416 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001417 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001418 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001419 struct batadv_hard_iface *primary_if;
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001420 bool send_loopdetect = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001421 int i;
1422
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001423 delayed_work = to_delayed_work(work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001424 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1425 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001426 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001427 if (!primary_if)
1428 goto out;
1429
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001430 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1431 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001432
1433 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1434 goto out;
1435
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001436 if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
1437 /* set a new random mac address for the next bridge loop
1438 * detection frames. Set the locally administered bit to avoid
1439 * collisions with users mac addresses.
1440 */
1441 random_ether_addr(bat_priv->bla.loopdetect_addr);
1442 bat_priv->bla.loopdetect_addr[0] = 0xba;
1443 bat_priv->bla.loopdetect_addr[1] = 0xbe;
1444 bat_priv->bla.loopdetect_lasttime = jiffies;
1445 atomic_set(&bat_priv->bla.loopdetect_next,
1446 BATADV_BLA_LOOPDETECT_PERIODS);
1447
1448 /* mark for sending loop detect on all VLANs */
1449 send_loopdetect = true;
1450 }
1451
Sven Eckelmann807736f2012-07-15 22:26:51 +02001452 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001453 if (!hash)
1454 goto out;
1455
1456 for (i = 0; i < hash->size; i++) {
1457 head = &hash->table[i];
1458
1459 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001460 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001461 if (!batadv_compare_eth(backbone_gw->orig,
1462 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001463 continue;
1464
1465 backbone_gw->lasttime = jiffies;
1466
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001467 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001468 if (send_loopdetect)
1469 batadv_bla_send_loopdetect(bat_priv,
1470 backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001471
1472 /* request_sent is only set after creation to avoid
1473 * problems when we are not yet known as backbone gw
1474 * in the backbone.
1475 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001476 * We can reset this now after we waited some periods
1477 * to give bridge forward delays and bla group forming
1478 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001479 */
1480
1481 if (atomic_read(&backbone_gw->request_sent) == 0)
1482 continue;
1483
Simon Wunderlich28709872012-09-13 18:18:46 +02001484 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1485 continue;
1486
Simon Wunderlichd807f272012-09-09 22:27:57 +02001487 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1488 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001489 }
1490 rcu_read_unlock();
1491 }
1492out:
1493 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001494 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001495
Antonio Quartulli72414442012-12-25 13:14:37 +01001496 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1497 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001498}
1499
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001500/* The hash for claim and backbone hash receive the same key because they
1501 * are getting initialized by hash_new with the same key. Reinitializing
1502 * them with to different keys to allow nested locking without generating
1503 * lockdep warnings
1504 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001505static struct lock_class_key batadv_claim_hash_lock_class_key;
1506static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001507
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001508/**
1509 * batadv_bla_init - initialize all bla structures
1510 * @bat_priv: the bat priv with all the soft interface information
1511 *
1512 * Return: 0 on success, < 0 on error.
1513 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001514int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001515{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001516 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001517 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001518 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001519 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001520 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001521
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001522 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1523
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001524 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001525
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001526 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001527 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1528 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001529 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001530 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001531 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1532 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001533 batadv_hardif_put(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001534 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001535 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001536 }
1537
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001538 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001539 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001540 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001541 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1542 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001543
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001544 atomic_set(&bat_priv->bla.loopdetect_next,
1545 BATADV_BLA_LOOPDETECT_PERIODS);
1546
Sven Eckelmann807736f2012-07-15 22:26:51 +02001547 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001548 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001549
Sven Eckelmann807736f2012-07-15 22:26:51 +02001550 bat_priv->bla.claim_hash = batadv_hash_new(128);
1551 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001552
Sven Eckelmann807736f2012-07-15 22:26:51 +02001553 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001554 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001555
Sven Eckelmann807736f2012-07-15 22:26:51 +02001556 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001557 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001558 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001559 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001560
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001561 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001562
Antonio Quartulli72414442012-12-25 13:14:37 +01001563 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1564
1565 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1566 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001567 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001568}
1569
Ben Hutchings2c530402012-07-10 10:55:09 +00001570/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001571 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
Ben Hutchings2c530402012-07-10 10:55:09 +00001572 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001573 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001574 *
1575 * check if it is on our broadcast list. Another gateway might
1576 * have sent the same packet because it is connected to the same backbone,
1577 * so we have to remove this duplicate.
1578 *
1579 * This is performed by checking the CRC, which will tell us
1580 * with a good chance that it is the same packet. If it is furthermore
1581 * sent by another host, drop it. We allow equal packets from
1582 * the same host however as this might be intended.
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001583 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001584 * Return: true if a packet is in the duplicate list, false otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001585 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001586bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1587 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001588{
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001589 int i, curr;
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001590 __be32 crc;
1591 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001592 struct batadv_bcast_duplist_entry *entry;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001593 bool ret = false;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001594
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001595 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001596
1597 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001598 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001599
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001600 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1601
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001602 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001603 curr = (bat_priv->bla.bcast_duplist_curr + i);
1604 curr %= BATADV_DUPLIST_SIZE;
1605 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001606
1607 /* we can stop searching if the entry is too old ;
1608 * later entries will be even older
1609 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001610 if (batadv_has_timed_out(entry->entrytime,
1611 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001612 break;
1613
1614 if (entry->crc != crc)
1615 continue;
1616
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001617 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001618 continue;
1619
1620 /* this entry seems to match: same crc, not too old,
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001621 * and from another gw. therefore return true to forbid it.
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001622 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001623 ret = true;
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001624 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001625 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001626 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001627 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001628 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001629 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001630 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001631 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001632 entry->crc = crc;
1633 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001634 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001635 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001636
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001637out:
1638 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1639
1640 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001641}
1642
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001643/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001644 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1645 * the VLAN identified by vid.
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001646 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001647 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001648 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001649 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001650 * Return: true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001651 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001652bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001653 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001654{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001655 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001656 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001657 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001658 int i;
1659
1660 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001661 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001662
1663 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001664 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001665
1666 for (i = 0; i < hash->size; i++) {
1667 head = &hash->table[i];
1668
1669 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001670 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001671 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1672 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001673 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001674 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001675 }
1676 }
1677 rcu_read_unlock();
1678 }
1679
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001680 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001681}
1682
Ben Hutchings2c530402012-07-10 10:55:09 +00001683/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001684 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
Ben Hutchings2c530402012-07-10 10:55:09 +00001685 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001686 * @orig_node: the orig_node of the frame
1687 * @hdr_size: maximum length of the frame
1688 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001689 * Return: true if the orig_node is also a gateway on the soft interface,
1690 * otherwise it returns false.
Simon Wunderlich23721382012-01-22 20:00:19 +01001691 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001692bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
1693 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001694{
Marek Lindnerbae98772012-12-25 17:03:24 +08001695 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001696 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001697
1698 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001699 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001700
1701 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001702 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001703 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001704
Antonio Quartullic018ad32013-06-04 12:11:39 +02001705 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001706
1707 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001708 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1709 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001710 if (!backbone_gw)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001711 return false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001712
Sven Eckelmannc8b86c12016-01-17 11:01:15 +01001713 batadv_backbone_gw_put(backbone_gw);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001714 return true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001715}
1716
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001717/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +01001718 * batadv_bla_free - free all bla structures
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001719 * @bat_priv: the bat priv with all the soft interface information
1720 *
1721 * for softinterface free or module unload
1722 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001723void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001724{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001725 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001726
Sven Eckelmann807736f2012-07-15 22:26:51 +02001727 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001728 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001729
Sven Eckelmann807736f2012-07-15 22:26:51 +02001730 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001731 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001732 batadv_hash_destroy(bat_priv->bla.claim_hash);
1733 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001734 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001735 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001736 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001737 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1738 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001739 }
1740 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001741 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001742}
1743
Ben Hutchings2c530402012-07-10 10:55:09 +00001744/**
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001745 * batadv_bla_loopdetect_check - check and handle a detected loop
1746 * @bat_priv: the bat priv with all the soft interface information
1747 * @skb: the packet to check
1748 * @primary_if: interface where the request came on
1749 * @vid: the VLAN ID of the frame
1750 *
1751 * Checks if this packet is a loop detect frame which has been sent by us,
1752 * throw an uevent and log the event if that is the case.
1753 *
1754 * Return: true if it is a loop detect frame which is to be dropped, false
1755 * otherwise.
1756 */
1757static bool
1758batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1759 struct batadv_hard_iface *primary_if,
1760 unsigned short vid)
1761{
1762 struct batadv_bla_backbone_gw *backbone_gw;
1763 struct ethhdr *ethhdr;
1764
1765 ethhdr = eth_hdr(skb);
1766
1767 /* Only check for the MAC address and skip more checks here for
1768 * performance reasons - this function is on the hotpath, after all.
1769 */
1770 if (!batadv_compare_eth(ethhdr->h_source,
1771 bat_priv->bla.loopdetect_addr))
1772 return false;
1773
1774 /* If the packet came too late, don't forward it on the mesh
1775 * but don't consider that as loop. It might be a coincidence.
1776 */
1777 if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
1778 BATADV_BLA_LOOPDETECT_TIMEOUT))
1779 return true;
1780
1781 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1782 primary_if->net_dev->dev_addr,
1783 vid, true);
1784 if (unlikely(!backbone_gw))
1785 return true;
1786
1787 queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1788 /* backbone_gw is unreferenced in the report work function function */
1789
1790 return true;
1791}
1792
1793/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001794 * batadv_bla_rx - check packets coming from the mesh.
Ben Hutchings2c530402012-07-10 10:55:09 +00001795 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001796 * @skb: the frame to be checked
1797 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001798 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001799 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001800 * batadv_bla_rx avoidance checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001801 * * we have to race for a claim
1802 * * if the frame is allowed on the LAN
1803 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001804 * in these cases, the skb is further handled by this function
1805 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001806 * Return: true if handled, otherwise it returns false and the caller shall
1807 * further process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001808 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001809bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1810 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001811{
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001812 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001813 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001814 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001815 struct batadv_hard_iface *primary_if;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001816 bool own_claim;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001817 bool ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001818
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001819 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001820
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001821 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001822 if (!primary_if)
1823 goto handled;
1824
1825 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1826 goto allow;
1827
Simon Wunderlichcd9c7bf2016-03-12 10:49:33 +01001828 if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1829 goto handled;
1830
Sven Eckelmann807736f2012-07-15 22:26:51 +02001831 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001832 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001833 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001834 goto handled;
1835
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001836 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001837 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001838 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001839
1840 if (!claim) {
1841 /* possible optimization: race for a claim */
1842 /* No claim exists yet, claim it for us!
1843 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001844 batadv_handle_claim(bat_priv, primary_if,
1845 primary_if->net_dev->dev_addr,
1846 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001847 goto allow;
1848 }
1849
1850 /* if it is our own claim ... */
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001851 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1852 own_claim = batadv_compare_eth(backbone_gw->orig,
1853 primary_if->net_dev->dev_addr);
1854 batadv_backbone_gw_put(backbone_gw);
1855
1856 if (own_claim) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001857 /* ... allow it in any case */
1858 claim->lasttime = jiffies;
1859 goto allow;
1860 }
1861
1862 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001863 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1864 /* ... drop it. the responsible gateway is in charge.
1865 *
1866 * We need to check is_bcast because with the gateway
1867 * feature, broadcasts (like DHCP requests) may be sent
1868 * using a unicast packet type.
1869 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001870 goto handled;
1871 } else {
1872 /* seems the client considers us as its best gateway.
1873 * send a claim and update the claim table
1874 * immediately.
1875 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001876 batadv_handle_claim(bat_priv, primary_if,
1877 primary_if->net_dev->dev_addr,
1878 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001879 goto allow;
1880 }
1881allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001882 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001883 ret = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001884 goto out;
1885
1886handled:
1887 kfree_skb(skb);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001888 ret = true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001889
1890out:
1891 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001892 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001893 if (claim)
Sven Eckelmann321e3e02016-01-17 11:01:16 +01001894 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001895 return ret;
1896}
1897
Ben Hutchings2c530402012-07-10 10:55:09 +00001898/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001899 * batadv_bla_tx - check packets going into the mesh
Ben Hutchings2c530402012-07-10 10:55:09 +00001900 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001901 * @skb: the frame to be checked
1902 * @vid: the VLAN ID of the frame
1903 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001904 * batadv_bla_tx checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001905 * * a claim was received which has to be processed
1906 * * the frame is allowed on the mesh
1907 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001908 * in these cases, the skb is further handled by this function.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001909 *
1910 * This call might reallocate skb data.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001911 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001912 * Return: true if handled, otherwise it returns false and the caller shall
1913 * further process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001914 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001915bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1916 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001917{
1918 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001919 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001920 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001921 struct batadv_hard_iface *primary_if;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001922 bool client_roamed;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001923 bool ret = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001924
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001925 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001926 if (!primary_if)
1927 goto out;
1928
1929 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1930 goto allow;
1931
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001932 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001933 goto handled;
1934
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001935 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001936
Sven Eckelmann807736f2012-07-15 22:26:51 +02001937 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001938 /* don't allow broadcasts while requests are in flight */
1939 if (is_multicast_ether_addr(ethhdr->h_dest))
1940 goto handled;
1941
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001942 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001943 search_claim.vid = vid;
1944
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001945 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001946
1947 /* if no claim exists, allow it. */
1948 if (!claim)
1949 goto allow;
1950
1951 /* check if we are responsible. */
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02001952 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1953 client_roamed = batadv_compare_eth(backbone_gw->orig,
1954 primary_if->net_dev->dev_addr);
1955 batadv_backbone_gw_put(backbone_gw);
1956
1957 if (client_roamed) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001958 /* if yes, the client has roamed and we have
1959 * to unclaim it.
1960 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001961 batadv_handle_unclaim(bat_priv, primary_if,
1962 primary_if->net_dev->dev_addr,
1963 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001964 goto allow;
1965 }
1966
1967 /* check if it is a multicast/broadcast frame */
1968 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1969 /* drop it. the responsible gateway has forwarded it into
1970 * the backbone network.
1971 */
1972 goto handled;
1973 } else {
1974 /* we must allow it. at least if we are
1975 * responsible for the DESTINATION.
1976 */
1977 goto allow;
1978 }
1979allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001980 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001981 ret = false;
Simon Wunderlich23721382012-01-22 20:00:19 +01001982 goto out;
1983handled:
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001984 ret = true;
Simon Wunderlich23721382012-01-22 20:00:19 +01001985out:
1986 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001987 batadv_hardif_put(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001988 if (claim)
Sven Eckelmann321e3e02016-01-17 11:01:16 +01001989 batadv_claim_put(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001990 return ret;
1991}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001992
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001993/**
1994 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1995 * @seq: seq file to print on
1996 * @offset: not used
1997 *
1998 * Return: always 0
1999 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02002000int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002001{
2002 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002003 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002004 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002005 struct batadv_bla_backbone_gw *backbone_gw;
Marek Lindner712bbfe42012-12-25 17:03:25 +08002006 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002007 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002008 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002009 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002010 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002011 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002012 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002013
Marek Lindner30da63a2012-08-03 17:15:46 +02002014 primary_if = batadv_seq_print_text_primary_if_get(seq);
2015 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002016 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002017
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002018 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01002019 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002020 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002021 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002022 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002023 seq_puts(seq,
2024 " Client VID Originator [o] (CRC )\n");
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002025 for (i = 0; i < hash->size; i++) {
2026 head = &hash->table[i];
2027
2028 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002029 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002030 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2031
2032 is_own = batadv_compare_eth(backbone_gw->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002033 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002034
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002035 spin_lock_bh(&backbone_gw->crc_lock);
2036 backbone_crc = backbone_gw->crc;
2037 spin_unlock_bh(&backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02002038 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02002039 claim->addr, BATADV_PRINT_VID(claim->vid),
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002040 backbone_gw->orig,
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002041 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002042 backbone_crc);
Sven Eckelmann3db0dec2016-07-01 15:49:43 +02002043
2044 batadv_backbone_gw_put(backbone_gw);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002045 }
2046 rcu_read_unlock();
2047 }
2048out:
2049 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01002050 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02002051 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01002052}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002053
Simon Wunderlich04e14be2015-11-06 10:45:19 +01002054/**
2055 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
2056 * file
2057 * @seq: seq file to print on
2058 * @offset: not used
2059 *
2060 * Return: always 0
2061 */
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002062int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
2063{
2064 struct net_device *net_dev = (struct net_device *)seq->private;
2065 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002066 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08002067 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002068 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002069 struct hlist_head *head;
2070 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002071 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002072 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002073 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002074 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002075
Marek Lindner30da63a2012-08-03 17:15:46 +02002076 primary_if = batadv_seq_print_text_primary_if_get(seq);
2077 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002078 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002079
2080 primary_addr = primary_if->net_dev->dev_addr;
2081 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01002082 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002083 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002084 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002085 seq_puts(seq, " Originator VID last seen (CRC )\n");
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002086 for (i = 0; i < hash->size; i++) {
2087 head = &hash->table[i];
2088
2089 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002090 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002091 msecs = jiffies_to_msecs(jiffies -
2092 backbone_gw->lasttime);
2093 secs = msecs / 1000;
2094 msecs = msecs % 1000;
2095
2096 is_own = batadv_compare_eth(backbone_gw->orig,
2097 primary_addr);
2098 if (is_own)
2099 continue;
2100
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002101 spin_lock_bh(&backbone_gw->crc_lock);
2102 backbone_crc = backbone_gw->crc;
2103 spin_unlock_bh(&backbone_gw->crc_lock);
2104
Antonio Quartullieb2deb62013-04-19 18:07:00 +02002105 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02002106 backbone_gw->orig,
2107 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02002108 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002109 }
2110 rcu_read_unlock();
2111 }
2112out:
2113 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01002114 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02002115 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02002116}