blob: b3a72e29a1677347bec3e94ac00a76d22823f0f3 [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2011-2015 B.A.T.M.A.N. contributors:
Simon Wunderlich23721382012-01-22 20:00:19 +01002 *
3 * Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Simon Wunderlich23721382012-01-22 20:00:19 +010016 */
17
Simon Wunderlich23721382012-01-22 20:00:19 +010018#include "bridge_loop_avoidance.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010020
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/atomic.h>
22#include <linux/byteorder/generic.h>
23#include <linux/compiler.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010024#include <linux/crc16.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/errno.h>
26#include <linux/etherdevice.h>
27#include <linux/fs.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010028#include <linux/if_arp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/if_ether.h>
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include <linux/if_vlan.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/lockdep.h>
36#include <linux/netdevice.h>
37#include <linux/rculist.h>
38#include <linux/rcupdate.h>
39#include <linux/seq_file.h>
40#include <linux/skbuff.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/stddef.h>
44#include <linux/string.h>
45#include <linux/workqueue.h>
46#include <net/arp.h>
47
48#include "hard-interface.h"
49#include "hash.h"
50#include "originator.h"
51#include "packet.h"
52#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010053
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020054static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010055
Sven Eckelmann3b300de2012-05-12 18:33:53 +020056static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080057static void
58batadv_bla_send_announce(struct batadv_priv *bat_priv,
59 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010060
Sven Eckelmann62fe7102015-09-15 19:00:48 +020061/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010062 * batadv_choose_claim - choose the right bucket for a claim.
63 * @data: data to hash
64 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020065 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010066 * Return: the hash index of the claim
Sven Eckelmann62fe7102015-09-15 19:00:48 +020067 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020068static inline u32 batadv_choose_claim(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010069{
Marek Lindner712bbfe42012-12-25 17:03:25 +080070 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020071 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010072
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010073 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
74 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010075
76 return hash % size;
77}
78
Sven Eckelmann62fe7102015-09-15 19:00:48 +020079/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +010080 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
81 * @data: data to hash
82 * @size: size of the hash table
Sven Eckelmann62fe7102015-09-15 19:00:48 +020083 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +010084 * Return: the hash index of the backbone gateway
Sven Eckelmann62fe7102015-09-15 19:00:48 +020085 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020086static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
Simon Wunderlich23721382012-01-22 20:00:19 +010087{
Marek Lindner712bbfe42012-12-25 17:03:25 +080088 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020089 u32 hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010090
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010091 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
92 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010093
94 return hash % size;
95}
96
Simon Wunderlich04e14be2015-11-06 10:45:19 +010097/**
98 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
99 * @node: list node of the first entry to compare
100 * @data2: pointer to the second backbone gateway
101 *
102 * Return: 1 if the backbones have the same data, 0 otherwise
103 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200104static int batadv_compare_backbone_gw(const struct hlist_node *node,
105 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100106{
Marek Lindnerbae98772012-12-25 17:03:24 +0800107 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +0100108 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200109 const struct batadv_bla_backbone_gw *gw1 = data1;
110 const struct batadv_bla_backbone_gw *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100111
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200112 if (!batadv_compare_eth(gw1->orig, gw2->orig))
113 return 0;
114
115 if (gw1->vid != gw2->vid)
116 return 0;
117
118 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100119}
120
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100121/**
122 * batadv_compare_backbone_gw - compare address and vid of two claims
123 * @node: list node of the first entry to compare
124 * @data2: pointer to the second claims
125 *
126 * Return: 1 if the claim have the same data, 0 otherwise
127 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200128static int batadv_compare_claim(const struct hlist_node *node,
129 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +0100130{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800131 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +0100132 hash_entry);
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200133 const struct batadv_bla_claim *cl1 = data1;
134 const struct batadv_bla_claim *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +0100135
Simon Wunderlichc76d1522012-10-15 22:38:04 +0200136 if (!batadv_compare_eth(cl1->addr, cl2->addr))
137 return 0;
138
139 if (cl1->vid != cl2->vid)
140 return 0;
141
142 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100143}
144
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100145/**
146 * batadv_compare_backbone_gw - free backbone gw
147 * @backbone_gw: backbone gateway to be free'd
148 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800149static void
150batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100151{
152 if (atomic_dec_and_test(&backbone_gw->refcount))
153 kfree_rcu(backbone_gw, rcu);
154}
155
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100156/**
157 * batadv_claim_release - release claim from lists and queue for free after rcu
158 * grace period
159 * @ref: kref pointer of the claim
160 */
Sven Eckelmann63b39922016-01-14 15:28:19 +0100161static void batadv_claim_release(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100162{
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200163 batadv_backbone_gw_free_ref(claim->backbone_gw);
Sven Eckelmann63b39922016-01-14 15:28:19 +0100164 kfree_rcu(claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100165}
166
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100167/**
Sven Eckelmannec9b83c2016-01-05 12:06:16 +0100168 * batadv_claim_free_ref - decrement the claim refcounter and possibly
169 * release it
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100170 * @claim: claim to be free'd
171 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800172static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100173{
174 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann63b39922016-01-14 15:28:19 +0100175 batadv_claim_release(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100176}
177
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100178/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100179 * batadv_claim_hash_find - looks for a claim in the claim hash
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100180 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100181 * @data: search data (may be local/static data)
182 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200183 * Return: claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100184 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800185static struct batadv_bla_claim
186*batadv_claim_hash_find(struct batadv_priv *bat_priv,
187 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100188{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200189 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100190 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800191 struct batadv_bla_claim *claim;
192 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100193 int index;
194
195 if (!hash)
196 return NULL;
197
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200198 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100199 head = &hash->table[index];
200
201 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800202 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200203 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100204 continue;
205
206 if (!atomic_inc_not_zero(&claim->refcount))
207 continue;
208
209 claim_tmp = claim;
210 break;
211 }
212 rcu_read_unlock();
213
214 return claim_tmp;
215}
216
Ben Hutchings2c530402012-07-10 10:55:09 +0000217/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100218 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
Ben Hutchings2c530402012-07-10 10:55:09 +0000219 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100220 * @addr: the address of the originator
221 * @vid: the VLAN ID
222 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100223 * Return: backbone gateway if found or NULL otherwise
Simon Wunderlich23721382012-01-22 20:00:19 +0100224 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800225static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200226batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
227 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100228{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200229 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100230 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800231 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
232 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100233 int index;
234
235 if (!hash)
236 return NULL;
237
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100238 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100239 search_entry.vid = vid;
240
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200241 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100242 head = &hash->table[index];
243
244 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800245 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200246 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
247 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100248 continue;
249
250 if (!atomic_inc_not_zero(&backbone_gw->refcount))
251 continue;
252
253 backbone_gw_tmp = backbone_gw;
254 break;
255 }
256 rcu_read_unlock();
257
258 return backbone_gw_tmp;
259}
260
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100261/**
262 * batadv_bla_del_backbone_claims - delete all claims for a backbone
263 * @backbone_gw: backbone gateway where the claims should be removed
264 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200265static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800266batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100267{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200268 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800269 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100270 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800271 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100272 int i;
273 spinlock_t *list_lock; /* protects write access to the hash lists */
274
Sven Eckelmann807736f2012-07-15 22:26:51 +0200275 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100276 if (!hash)
277 return;
278
279 for (i = 0; i < hash->size; i++) {
280 head = &hash->table[i];
281 list_lock = &hash->list_locks[i];
282
283 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800284 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100285 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100286 if (claim->backbone_gw != backbone_gw)
287 continue;
288
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200289 batadv_claim_free_ref(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800290 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100291 }
292 spin_unlock_bh(list_lock);
293 }
294
Antonio Quartulli3f687852014-11-02 11:29:56 +0100295 /* all claims gone, initialize CRC */
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200296 spin_lock_bh(&backbone_gw->crc_lock);
Sven Eckelmann3964f722012-06-03 22:19:10 +0200297 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200298 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100299}
300
Ben Hutchings2c530402012-07-10 10:55:09 +0000301/**
302 * batadv_bla_send_claim - sends a claim frame according to the provided info
303 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200304 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100305 * @vid: the VLAN ID
306 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100307 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200308static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200309 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100310{
311 struct sk_buff *skb;
312 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200313 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100314 struct net_device *soft_iface;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200315 u8 *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200316 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100317 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100318
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200319 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100320 if (!primary_if)
321 return;
322
Sven Eckelmann807736f2012-07-15 22:26:51 +0200323 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100324 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100325 local_claim_dest.type = claimtype;
326
327 soft_iface = primary_if->soft_iface;
328
329 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
330 /* IP DST: 0.0.0.0 */
331 zeroip,
332 primary_if->soft_iface,
333 /* IP SRC: 0.0.0.0 */
334 zeroip,
335 /* Ethernet DST: Broadcast */
336 NULL,
337 /* Ethernet SRC/HW SRC: originator mac */
338 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200339 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100340 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100341 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100342 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200343 (u8 *)&local_claim_dest);
Simon Wunderlich23721382012-01-22 20:00:19 +0100344
345 if (!skb)
346 goto out;
347
348 ethhdr = (struct ethhdr *)skb->data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200349 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100350
351 /* now we pretend that the client would have sent this ... */
352 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200353 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100354 /* normal claim frame
355 * set Ethernet SRC to the clients mac
356 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100357 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200358 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200359 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
360 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100361 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200362 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100363 /* unclaim frame
364 * set HW SRC to the clients mac
365 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100366 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200367 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200368 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200369 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100370 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200371 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100372 /* announcement frame
373 * set HW SRC to the special mac containg the crc
374 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100375 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200376 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200377 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200378 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100379 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200380 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100381 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200382 * set HW SRC and header destination to the receiving backbone
383 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100384 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100385 ether_addr_copy(hw_src, mac);
386 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200387 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200388 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200389 ethhdr->h_source, ethhdr->h_dest,
390 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100391 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100392 }
393
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200394 if (vid & BATADV_VLAN_HAS_TAG)
395 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
396 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100397
398 skb_reset_mac_header(skb);
399 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200400 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
401 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
402 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100403 soft_iface->last_rx = jiffies;
404
405 netif_rx(skb);
406out:
407 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200408 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100409}
410
Ben Hutchings2c530402012-07-10 10:55:09 +0000411/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100412 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
Ben Hutchings2c530402012-07-10 10:55:09 +0000413 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100414 * @orig: the mac address of the originator
415 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200416 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100417 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100418 * Return: the (possibly created) backbone gateway or NULL on error
Simon Wunderlich23721382012-01-22 20:00:19 +0100419 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800420static struct batadv_bla_backbone_gw *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200421batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200422 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100423{
Marek Lindnerbae98772012-12-25 17:03:24 +0800424 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200425 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100426 int hash_added;
427
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200428 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100429
430 if (entry)
431 return entry;
432
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200433 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200434 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200435 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100436
437 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
438 if (!entry)
439 return NULL;
440
441 entry->vid = vid;
442 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200443 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100444 entry->bat_priv = bat_priv;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200445 spin_lock_init(&entry->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100446 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200447 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100448 ether_addr_copy(entry->orig, orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100449
450 /* one for the hash, one for returning */
451 atomic_set(&entry->refcount, 2);
452
Sven Eckelmann807736f2012-07-15 22:26:51 +0200453 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200454 batadv_compare_backbone_gw,
455 batadv_choose_backbone_gw, entry,
456 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100457
458 if (unlikely(hash_added != 0)) {
459 /* hash failed, free the structure */
460 kfree(entry);
461 return NULL;
462 }
463
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200464 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200465 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100466 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200467 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200468 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200469 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100470 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200471
Simon Wunderlichd807f272012-09-09 22:27:57 +0200472 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200473 batadv_bla_send_announce(bat_priv, entry);
474
Simon Wunderlichd807f272012-09-09 22:27:57 +0200475 /* this will be decreased in the worker thread */
476 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200477 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200478 atomic_inc(&bat_priv->bla.num_requests);
479 }
480
Simon Wunderlich23721382012-01-22 20:00:19 +0100481 return entry;
482}
483
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100484/**
485 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
486 * @bat_priv: the bat priv with all the soft interface information
487 * @primary_if: the selected primary interface
488 * @vid: VLAN identifier
489 *
490 * update or add the own backbone gw to make sure we announce
Simon Wunderlich23721382012-01-22 20:00:19 +0100491 * where we receive other backbone gws
492 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200493static void
494batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
495 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200496 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100497{
Marek Lindnerbae98772012-12-25 17:03:24 +0800498 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100499
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200500 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
501 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200502 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100503 if (unlikely(!backbone_gw))
504 return;
505
506 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200507 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100508}
509
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100510/**
511 * batadv_bla_answer_request - answer a bla request by sending own claims
512 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200513 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100514 * @vid: the vid where the request came on
515 *
516 * Repeat all of our own claims, and finally send an ANNOUNCE frame
517 * to allow the requester another check if the CRC is correct now.
518 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200519static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
520 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200521 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100522{
Simon Wunderlich23721382012-01-22 20:00:19 +0100523 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200524 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800525 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800526 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100527 int i;
528
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200529 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200530 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100531
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200532 backbone_gw = batadv_backbone_hash_find(bat_priv,
533 primary_if->net_dev->dev_addr,
534 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100535 if (!backbone_gw)
536 return;
537
Sven Eckelmann807736f2012-07-15 22:26:51 +0200538 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100539 for (i = 0; i < hash->size; i++) {
540 head = &hash->table[i];
541
542 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800543 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100544 /* only own claims are interesting */
545 if (claim->backbone_gw != backbone_gw)
546 continue;
547
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200548 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200549 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100550 }
551 rcu_read_unlock();
552 }
553
554 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200555 batadv_bla_send_announce(bat_priv, backbone_gw);
556 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100557}
558
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100559/**
560 * batadv_bla_send_request - send a request to repeat claims
561 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100562 *
563 * When the crc is wrong, ask the backbone gateway for a full table update.
564 * After the request, it will repeat all of his own claims and finally
565 * send an announcement claim with which we can check again.
566 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800567static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100568{
569 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200570 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100571
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200572 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
573 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100574
575 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200576 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200577 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100578
579 /* no local broadcasts should be sent or received, for now. */
580 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200581 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100582 atomic_set(&backbone_gw->request_sent, 1);
583 }
584}
585
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100586/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100587 * batadv_bla_send_announce - Send an announcement frame
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100588 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100589 * @backbone_gw: our backbone gateway which should be announced
Simon Wunderlich23721382012-01-22 20:00:19 +0100590 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200591static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800592 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100593{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200594 u8 mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100595 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100596
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200597 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200598 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100599 crc = htons(backbone_gw->crc);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200600 spin_unlock_bh(&backbone_gw->crc_lock);
Al Viro1a5852d2012-04-22 07:50:29 +0100601 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100602
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200603 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200604 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100605}
606
Ben Hutchings2c530402012-07-10 10:55:09 +0000607/**
608 * batadv_bla_add_claim - Adds a claim in the claim hash
609 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100610 * @mac: the mac address of the claim
611 * @vid: the VLAN ID of the frame
612 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100613 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200614static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200615 const u8 *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800616 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100617{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800618 struct batadv_bla_claim *claim;
619 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100620 int hash_added;
621
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100622 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100623 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200624 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100625
626 /* create a new claim entry if it does not exist yet. */
627 if (!claim) {
628 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
629 if (!claim)
630 return;
631
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100632 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100633 claim->vid = vid;
634 claim->lasttime = jiffies;
635 claim->backbone_gw = backbone_gw;
636
637 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200638 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200639 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200640 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200641 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200642 batadv_compare_claim,
643 batadv_choose_claim, claim,
644 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100645
646 if (unlikely(hash_added != 0)) {
647 /* only local changes happened. */
648 kfree(claim);
649 return;
650 }
651 } else {
652 claim->lasttime = jiffies;
653 if (claim->backbone_gw == backbone_gw)
654 /* no need to register a new backbone */
655 goto claim_free_ref;
656
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200657 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200658 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200659 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100660
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200661 spin_lock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200662 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200663 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200664 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100665 }
666 /* set (new) backbone gw */
667 atomic_inc(&backbone_gw->refcount);
668 claim->backbone_gw = backbone_gw;
669
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200670 spin_lock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100671 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200672 spin_unlock_bh(&backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100673 backbone_gw->lasttime = jiffies;
674
675claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200676 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100677}
678
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100679/**
680 * batadv_bla_del_claim - delete a claim from the claim hash
681 * @bat_priv: the bat priv with all the soft interface information
682 * @mac: mac address of the claim to be removed
683 * @vid: VLAN id for the claim to be removed
Simon Wunderlich23721382012-01-22 20:00:19 +0100684 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200685static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200686 const u8 *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100687{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800688 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100689
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100690 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100691 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200692 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100693 if (!claim)
694 return;
695
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200696 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200697 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100698
Sven Eckelmann807736f2012-07-15 22:26:51 +0200699 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200700 batadv_choose_claim, claim);
701 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100702
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200703 spin_lock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100704 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200705 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Simon Wunderlich23721382012-01-22 20:00:19 +0100706
707 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200708 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100709}
710
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200711/**
712 * batadv_handle_announce - check for ANNOUNCE frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100713 * @bat_priv: the bat priv with all the soft interface information
714 * @an_addr: announcement mac address (ARP Sender HW address)
715 * @backbone_addr: originator address of the sender (Ethernet source MAC)
716 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200717 *
718 * Return: 1 if handled
719 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200720static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
721 u8 *backbone_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100722{
Marek Lindnerbae98772012-12-25 17:03:24 +0800723 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200724 u16 backbone_crc, crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100725
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200726 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100727 return 0;
728
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200729 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
730 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100731
732 if (unlikely(!backbone_gw))
733 return 1;
734
Simon Wunderlich23721382012-01-22 20:00:19 +0100735 /* handle as ANNOUNCE frame */
736 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100737 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100738
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200739 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100740 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200741 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100742
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200743 spin_lock_bh(&backbone_gw->crc_lock);
744 backbone_crc = backbone_gw->crc;
745 spin_unlock_bh(&backbone_gw->crc_lock);
746
747 if (backbone_crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200748 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100749 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200750 backbone_gw->orig,
751 BATADV_PRINT_VID(backbone_gw->vid),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +0200752 backbone_crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100753
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200754 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100755 } else {
756 /* if we have sent a request and the crc was OK,
757 * we can allow traffic again.
758 */
759 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200760 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100761 atomic_set(&backbone_gw->request_sent, 0);
762 }
763 }
764
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200765 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100766 return 1;
767}
768
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200769/**
770 * batadv_handle_request - check for REQUEST frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100771 * @bat_priv: the bat priv with all the soft interface information
772 * @primary_if: the primary hard interface of this batman soft interface
773 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
774 * @ethhdr: ethernet header of a packet
775 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200776 *
777 * Return: 1 if handled
778 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200779static int batadv_handle_request(struct batadv_priv *bat_priv,
780 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200781 u8 *backbone_addr, struct ethhdr *ethhdr,
782 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100783{
784 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200785 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100786 return 0;
787
788 /* sanity check, this should not happen on a normal switch,
789 * we ignore it in this case.
790 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200791 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100792 return 1;
793
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200794 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200795 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200796 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100797
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200798 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100799 return 1;
800}
801
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200802/**
803 * batadv_handle_unclaim - check for UNCLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100804 * @bat_priv: the bat priv with all the soft interface information
805 * @primary_if: the primary hard interface of this batman soft interface
806 * @backbone_addr: originator address of the backbone (Ethernet source)
807 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
808 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200809 *
810 * Return: 1 if handled
811 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200812static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
813 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200814 u8 *backbone_addr, u8 *claim_addr,
815 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100816{
Marek Lindnerbae98772012-12-25 17:03:24 +0800817 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100818
819 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200820 if (primary_if && batadv_compare_eth(backbone_addr,
821 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200822 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200823 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100824
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200825 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100826
827 if (!backbone_gw)
828 return 1;
829
830 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200831 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200832 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200833 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100834
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200835 batadv_bla_del_claim(bat_priv, claim_addr, vid);
836 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100837 return 1;
838}
839
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200840/**
841 * batadv_handle_claim - check for CLAIM frame
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100842 * @bat_priv: the bat priv with all the soft interface information
843 * @primary_if: the primary hard interface of this batman soft interface
844 * @backbone_addr: originator address of the backbone (Ethernet Source)
845 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
846 * @vid: the VLAN ID of the frame
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200847 *
848 * Return: 1 if handled
849 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200850static int batadv_handle_claim(struct batadv_priv *bat_priv,
851 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200852 u8 *backbone_addr, u8 *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200853 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100854{
Marek Lindnerbae98772012-12-25 17:03:24 +0800855 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100856
857 /* register the gateway if not yet available, and add the claim. */
858
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200859 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
860 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100861
862 if (unlikely(!backbone_gw))
863 return 1;
864
865 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200866 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200867 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200868 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200869 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100870
871 /* TODO: we could call something like tt_local_del() here. */
872
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200873 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100874 return 1;
875}
876
Ben Hutchings2c530402012-07-10 10:55:09 +0000877/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100878 * batadv_check_claim_group - check for claim group membership
Ben Hutchings2c530402012-07-10 10:55:09 +0000879 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200880 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100881 * @hw_src: the Hardware source in the ARP Header
882 * @hw_dst: the Hardware destination in the ARP Header
883 * @ethhdr: pointer to the Ethernet header of the claim frame
884 *
885 * checks if it is a claim packet and if its on the same group.
886 * This function also applies the group ID of the sender
887 * if it is in the same mesh.
888 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200889 * Return:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100890 * 2 - if it is a claim packet and on the same group
891 * 1 - if is a claim packet from another group
892 * 0 - if it is not a claim packet
893 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200894static int batadv_check_claim_group(struct batadv_priv *bat_priv,
895 struct batadv_hard_iface *primary_if,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200896 u8 *hw_src, u8 *hw_dst,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200897 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100898{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200899 u8 *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200900 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200901 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100902
Sven Eckelmann96412692012-06-05 22:31:30 +0200903 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200904 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100905
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100906 /* if announcement packet, use the source,
907 * otherwise assume it is in the hw_src
908 */
909 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200910 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100911 backbone_addr = hw_src;
912 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200913 case BATADV_CLAIM_TYPE_REQUEST:
914 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200915 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100916 backbone_addr = ethhdr->h_source;
917 break;
918 default:
919 return 0;
920 }
921
922 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200923 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100924 return 0;
925
926 /* if its already the same group, it is fine. */
927 if (bla_dst->group == bla_dst_own->group)
928 return 2;
929
930 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200931 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100932
933 /* dont accept claims from gateways which are not in
934 * the same mesh or group.
935 */
936 if (!orig_node)
937 return 1;
938
939 /* if our mesh friends mac is bigger, use it for ourselves. */
940 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200941 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100942 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200943 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100944 bla_dst_own->group = bla_dst->group;
945 }
946
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200947 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100948
949 return 2;
950}
951
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100952/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +0100953 * batadv_bla_process_claim - Check if this is a claim frame, and process it
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100954 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200955 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +0100956 * @skb: the frame to be checked
957 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200958 * Return: 1 if it was a claim frame, otherwise return 0 to
Simon Wunderlich23721382012-01-22 20:00:19 +0100959 * tell the callee that it can use the frame on its own.
960 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200961static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
962 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200963 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100964{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200965 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200966 u8 *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200967 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200968 struct ethhdr *ethhdr;
969 struct arphdr *arphdr;
970 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200971 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200972 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +0100973 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100974 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100975
Antonio Quartullic018ad32013-06-04 12:11:39 +0200976 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200977 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100978
Antonio Quartullic018ad32013-06-04 12:11:39 +0200979 proto = ethhdr->h_proto;
980 headlen = ETH_HLEN;
981 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200982 /* Traverse the VLAN/Ethertypes.
983 *
984 * At this point it is known that the first protocol is a VLAN
985 * header, so start checking at the encapsulated protocol.
986 *
987 * The depth of the VLAN headers is recorded to drop BLA claim
988 * frames encapsulated into multiple VLAN headers (QinQ).
989 */
990 do {
991 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
992 &vhdr_buf);
993 if (!vhdr)
994 return 0;
995
996 proto = vhdr->h_vlan_encapsulated_proto;
997 headlen += VLAN_HLEN;
998 vlan_depth++;
999 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +01001000 }
1001
Antonio Quartulli293e9332013-05-19 12:55:16 +02001002 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +01001003 return 0; /* not a claim frame */
1004
1005 /* this must be a ARP frame. check if it is a claim. */
1006
1007 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1008 return 0;
1009
1010 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001011 ethhdr = eth_hdr(skb);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001012 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
Simon Wunderlich23721382012-01-22 20:00:19 +01001013
1014 /* Check whether the ARP frame carries a valid
1015 * IP information
1016 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001017 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1018 return 0;
1019 if (arphdr->ar_pro != htons(ETH_P_IP))
1020 return 0;
1021 if (arphdr->ar_hln != ETH_ALEN)
1022 return 0;
1023 if (arphdr->ar_pln != 4)
1024 return 0;
1025
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001026 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001027 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +02001028 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +02001029 bla_dst_own = &bat_priv->bla.claim_dest;
1030
1031 /* check if it is a claim frame in general */
1032 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1033 sizeof(bla_dst->magic)) != 0)
1034 return 0;
1035
1036 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1037 * drop that, as this is not supported by BLA but should also not be
1038 * sent via the mesh.
1039 */
1040 if (vlan_depth > 1)
1041 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +01001042
1043 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001044 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1045 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001046 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001047 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001048 "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 +02001049 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1050 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001051
1052 if (ret < 2)
1053 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +01001054
1055 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001056 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001057
1058 /* check for the different types of claim frames ... */
1059 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001060 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001061 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1062 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001063 return 1;
1064 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +02001065 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001066 if (batadv_handle_unclaim(bat_priv, primary_if,
1067 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001068 return 1;
1069 break;
1070
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001071 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001072 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1073 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001074 return 1;
1075 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001076 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001077 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1078 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +01001079 return 1;
1080 break;
1081 }
1082
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001083 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001084 "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 +02001085 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +01001086 return 1;
1087}
1088
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001089/**
1090 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1091 * immediately
1092 * @bat_priv: the bat priv with all the soft interface information
1093 * @now: whether the whole hash shall be wiped now
1094 *
1095 * Check when we last heard from other nodes, and remove them in case of
Simon Wunderlich23721382012-01-22 20:00:19 +01001096 * a time out, or clean all backbone gws if now is set.
1097 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001098static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001099{
Marek Lindnerbae98772012-12-25 17:03:24 +08001100 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001101 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +01001102 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001103 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001104 spinlock_t *list_lock; /* protects write access to the hash lists */
1105 int i;
1106
Sven Eckelmann807736f2012-07-15 22:26:51 +02001107 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001108 if (!hash)
1109 return;
1110
1111 for (i = 0; i < hash->size; i++) {
1112 head = &hash->table[i];
1113 list_lock = &hash->list_locks[i];
1114
1115 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001116 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001117 head, hash_entry) {
1118 if (now)
1119 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001120 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001121 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001122 continue;
1123
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001124 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001125 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1126 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001127
1128purge_now:
1129 /* don't wait for the pending request anymore */
1130 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001131 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001132
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001133 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001134
Sasha Levinb67bfe02013-02-27 17:06:00 -08001135 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001136 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001137 }
1138 spin_unlock_bh(list_lock);
1139 }
1140}
1141
Ben Hutchings2c530402012-07-10 10:55:09 +00001142/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001143 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
Ben Hutchings2c530402012-07-10 10:55:09 +00001144 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001145 * @primary_if: the selected primary interface, may be NULL if now is set
1146 * @now: whether the whole hash shall be wiped now
1147 *
1148 * Check when we heard last time from our own claims, and remove them in case of
1149 * a time out, or clean all claims if now is set
1150 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001151static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1152 struct batadv_hard_iface *primary_if,
1153 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001154{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001155 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001156 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001157 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001158 int i;
1159
Sven Eckelmann807736f2012-07-15 22:26:51 +02001160 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001161 if (!hash)
1162 return;
1163
1164 for (i = 0; i < hash->size; i++) {
1165 head = &hash->table[i];
1166
1167 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001168 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001169 if (now)
1170 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001171 if (!batadv_compare_eth(claim->backbone_gw->orig,
1172 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001173 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001174 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001175 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001176 continue;
1177
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001178 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001179 "bla_purge_claims(): %pM, vid %d, time out\n",
1180 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001181
1182purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001183 batadv_handle_unclaim(bat_priv, primary_if,
1184 claim->backbone_gw->orig,
1185 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001186 }
1187 rcu_read_unlock();
1188 }
1189}
1190
Ben Hutchings2c530402012-07-10 10:55:09 +00001191/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001192 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1193 * originator address changes
Ben Hutchings2c530402012-07-10 10:55:09 +00001194 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001195 * @primary_if: the new selected primary_if
1196 * @oldif: the old primary interface, may be NULL
Simon Wunderlich23721382012-01-22 20:00:19 +01001197 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001198void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1199 struct batadv_hard_iface *primary_if,
1200 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001201{
Marek Lindnerbae98772012-12-25 17:03:24 +08001202 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001203 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001204 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001205 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001206 int i;
1207
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001208 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001209 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1210 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001211
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001212 /* purge everything when bridge loop avoidance is turned off */
1213 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1214 oldif = NULL;
1215
Simon Wunderlich23721382012-01-22 20:00:19 +01001216 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001217 batadv_bla_purge_claims(bat_priv, NULL, 1);
1218 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001219 return;
1220 }
1221
Sven Eckelmann807736f2012-07-15 22:26:51 +02001222 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001223 if (!hash)
1224 return;
1225
1226 for (i = 0; i < hash->size; i++) {
1227 head = &hash->table[i];
1228
1229 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001230 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001231 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001232 if (!batadv_compare_eth(backbone_gw->orig,
1233 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001234 continue;
1235
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001236 ether_addr_copy(backbone_gw->orig,
1237 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001238 /* send an announce frame so others will ask for our
1239 * claims and update their tables.
1240 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001241 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001242 }
1243 rcu_read_unlock();
1244 }
1245}
1246
Simon Wunderlichd68081a2015-11-09 16:20:52 +01001247/**
1248 * batadv_bla_status_update - purge bla interfaces if necessary
1249 * @net_dev: the soft interface net device
1250 */
1251void batadv_bla_status_update(struct net_device *net_dev)
1252{
1253 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1254 struct batadv_hard_iface *primary_if;
1255
1256 primary_if = batadv_primary_if_get_selected(bat_priv);
1257 if (!primary_if)
1258 return;
1259
1260 /* this function already purges everything when bla is disabled,
1261 * so just call that one.
1262 */
1263 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1264 batadv_hardif_free_ref(primary_if);
1265}
1266
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001267/**
1268 * batadv_bla_periodic_work - performs periodic bla work
1269 * @work: kernel work struct
1270 *
1271 * periodic work to do:
Simon Wunderlich23721382012-01-22 20:00:19 +01001272 * * purge structures when they are too old
1273 * * send announcements
1274 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001275static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001276{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001277 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001278 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001279 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001280 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001281 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001282 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001283 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001284 int i;
1285
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001286 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001287 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1288 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001289 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001290 if (!primary_if)
1291 goto out;
1292
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001293 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1294 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001295
1296 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1297 goto out;
1298
Sven Eckelmann807736f2012-07-15 22:26:51 +02001299 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001300 if (!hash)
1301 goto out;
1302
1303 for (i = 0; i < hash->size; i++) {
1304 head = &hash->table[i];
1305
1306 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001307 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001308 if (!batadv_compare_eth(backbone_gw->orig,
1309 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001310 continue;
1311
1312 backbone_gw->lasttime = jiffies;
1313
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001314 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001315
1316 /* request_sent is only set after creation to avoid
1317 * problems when we are not yet known as backbone gw
1318 * in the backbone.
1319 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001320 * We can reset this now after we waited some periods
1321 * to give bridge forward delays and bla group forming
1322 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001323 */
1324
1325 if (atomic_read(&backbone_gw->request_sent) == 0)
1326 continue;
1327
Simon Wunderlich28709872012-09-13 18:18:46 +02001328 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1329 continue;
1330
Simon Wunderlichd807f272012-09-09 22:27:57 +02001331 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1332 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001333 }
1334 rcu_read_unlock();
1335 }
1336out:
1337 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001338 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001339
Antonio Quartulli72414442012-12-25 13:14:37 +01001340 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1341 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001342}
1343
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001344/* The hash for claim and backbone hash receive the same key because they
1345 * are getting initialized by hash_new with the same key. Reinitializing
1346 * them with to different keys to allow nested locking without generating
1347 * lockdep warnings
1348 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001349static struct lock_class_key batadv_claim_hash_lock_class_key;
1350static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001351
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001352/**
1353 * batadv_bla_init - initialize all bla structures
1354 * @bat_priv: the bat priv with all the soft interface information
1355 *
1356 * Return: 0 on success, < 0 on error.
1357 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001358int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001359{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001360 int i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001361 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001362 struct batadv_hard_iface *primary_if;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001363 u16 crc;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001364 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001365
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001366 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1367
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001368 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001369
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001370 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001371 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1372 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001373 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001374 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001375 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1376 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001377 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001378 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001379 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001380 }
1381
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001382 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001383 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001384 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001385 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1386 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001387
Sven Eckelmann807736f2012-07-15 22:26:51 +02001388 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001389 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001390
Sven Eckelmann807736f2012-07-15 22:26:51 +02001391 bat_priv->bla.claim_hash = batadv_hash_new(128);
1392 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001393
Sven Eckelmann807736f2012-07-15 22:26:51 +02001394 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001395 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001396
Sven Eckelmann807736f2012-07-15 22:26:51 +02001397 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001398 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001399 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001400 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001401
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001402 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001403
Antonio Quartulli72414442012-12-25 13:14:37 +01001404 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1405
1406 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1407 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001408 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001409}
1410
Ben Hutchings2c530402012-07-10 10:55:09 +00001411/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001412 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
Ben Hutchings2c530402012-07-10 10:55:09 +00001413 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001414 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001415 *
1416 * check if it is on our broadcast list. Another gateway might
1417 * have sent the same packet because it is connected to the same backbone,
1418 * so we have to remove this duplicate.
1419 *
1420 * This is performed by checking the CRC, which will tell us
1421 * with a good chance that it is the same packet. If it is furthermore
1422 * sent by another host, drop it. We allow equal packets from
1423 * the same host however as this might be intended.
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001424 *
1425 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001426 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001427int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001428 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001429{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001430 int i, curr, ret = 0;
1431 __be32 crc;
1432 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001433 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001434
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001435 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001436
1437 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001438 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001439
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001440 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1441
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001442 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001443 curr = (bat_priv->bla.bcast_duplist_curr + i);
1444 curr %= BATADV_DUPLIST_SIZE;
1445 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001446
1447 /* we can stop searching if the entry is too old ;
1448 * later entries will be even older
1449 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001450 if (batadv_has_timed_out(entry->entrytime,
1451 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001452 break;
1453
1454 if (entry->crc != crc)
1455 continue;
1456
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001457 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001458 continue;
1459
1460 /* this entry seems to match: same crc, not too old,
1461 * and from another gw. therefore return 1 to forbid it.
1462 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001463 ret = 1;
1464 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001465 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001466 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001467 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001468 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001469 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001470 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001471 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001472 entry->crc = crc;
1473 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001474 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001475 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001476
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001477out:
1478 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1479
1480 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001481}
1482
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001483/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001484 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1485 * the VLAN identified by vid.
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001486 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001487 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001488 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001489 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001490 * Return: true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001491 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001492bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001493 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001494{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001495 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001496 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001497 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001498 int i;
1499
1500 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001501 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001502
1503 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001504 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001505
1506 for (i = 0; i < hash->size; i++) {
1507 head = &hash->table[i];
1508
1509 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001510 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001511 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1512 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001513 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001514 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001515 }
1516 }
1517 rcu_read_unlock();
1518 }
1519
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001520 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001521}
1522
Ben Hutchings2c530402012-07-10 10:55:09 +00001523/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001524 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
Ben Hutchings2c530402012-07-10 10:55:09 +00001525 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001526 * @orig_node: the orig_node of the frame
1527 * @hdr_size: maximum length of the frame
1528 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001529 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
1530 * it returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001531 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001532int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001533 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001534{
Marek Lindnerbae98772012-12-25 17:03:24 +08001535 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001536 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001537
1538 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1539 return 0;
1540
1541 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001542 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001543 return 0;
1544
Antonio Quartullic018ad32013-06-04 12:11:39 +02001545 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001546
1547 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001548 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1549 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001550 if (!backbone_gw)
1551 return 0;
1552
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001553 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001554 return 1;
1555}
1556
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001557/**
1558 * batadv_bla_init - free all bla structures
1559 * @bat_priv: the bat priv with all the soft interface information
1560 *
1561 * for softinterface free or module unload
1562 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001563void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001564{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001565 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001566
Sven Eckelmann807736f2012-07-15 22:26:51 +02001567 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001568 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001569
Sven Eckelmann807736f2012-07-15 22:26:51 +02001570 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001571 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001572 batadv_hash_destroy(bat_priv->bla.claim_hash);
1573 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001574 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001575 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001576 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001577 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1578 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001579 }
1580 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001581 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001582}
1583
Ben Hutchings2c530402012-07-10 10:55:09 +00001584/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001585 * batadv_bla_rx - check packets coming from the mesh.
Ben Hutchings2c530402012-07-10 10:55:09 +00001586 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001587 * @skb: the frame to be checked
1588 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001589 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001590 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001591 * batadv_bla_rx avoidance checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001592 * * we have to race for a claim
1593 * * if the frame is allowed on the LAN
1594 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001595 * in these cases, the skb is further handled by this function
1596 *
1597 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
Simon Wunderlich23721382012-01-22 20:00:19 +01001598 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001599 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001600int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1601 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001602{
1603 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001604 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001605 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001606 int ret;
1607
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001608 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001609
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001610 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001611 if (!primary_if)
1612 goto handled;
1613
1614 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1615 goto allow;
1616
Sven Eckelmann807736f2012-07-15 22:26:51 +02001617 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001618 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001619 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001620 goto handled;
1621
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001622 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001623 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001624 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001625
1626 if (!claim) {
1627 /* possible optimization: race for a claim */
1628 /* No claim exists yet, claim it for us!
1629 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001630 batadv_handle_claim(bat_priv, primary_if,
1631 primary_if->net_dev->dev_addr,
1632 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001633 goto allow;
1634 }
1635
1636 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001637 if (batadv_compare_eth(claim->backbone_gw->orig,
1638 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001639 /* ... allow it in any case */
1640 claim->lasttime = jiffies;
1641 goto allow;
1642 }
1643
1644 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001645 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1646 /* ... drop it. the responsible gateway is in charge.
1647 *
1648 * We need to check is_bcast because with the gateway
1649 * feature, broadcasts (like DHCP requests) may be sent
1650 * using a unicast packet type.
1651 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001652 goto handled;
1653 } else {
1654 /* seems the client considers us as its best gateway.
1655 * send a claim and update the claim table
1656 * immediately.
1657 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001658 batadv_handle_claim(bat_priv, primary_if,
1659 primary_if->net_dev->dev_addr,
1660 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001661 goto allow;
1662 }
1663allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001664 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001665 ret = 0;
1666 goto out;
1667
1668handled:
1669 kfree_skb(skb);
1670 ret = 1;
1671
1672out:
1673 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001674 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001675 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001676 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001677 return ret;
1678}
1679
Ben Hutchings2c530402012-07-10 10:55:09 +00001680/**
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001681 * batadv_bla_tx - check packets going into the mesh
Ben Hutchings2c530402012-07-10 10:55:09 +00001682 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001683 * @skb: the frame to be checked
1684 * @vid: the VLAN ID of the frame
1685 *
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001686 * batadv_bla_tx checks if:
Simon Wunderlich23721382012-01-22 20:00:19 +01001687 * * a claim was received which has to be processed
1688 * * the frame is allowed on the mesh
1689 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001690 * in these cases, the skb is further handled by this function.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001691 *
1692 * This call might reallocate skb data.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001693 *
1694 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1695 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001696 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001697int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1698 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001699{
1700 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001701 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001702 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001703 int ret = 0;
1704
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001705 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001706 if (!primary_if)
1707 goto out;
1708
1709 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1710 goto allow;
1711
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001712 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001713 goto handled;
1714
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001715 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001716
Sven Eckelmann807736f2012-07-15 22:26:51 +02001717 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001718 /* don't allow broadcasts while requests are in flight */
1719 if (is_multicast_ether_addr(ethhdr->h_dest))
1720 goto handled;
1721
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001722 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001723 search_claim.vid = vid;
1724
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001725 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001726
1727 /* if no claim exists, allow it. */
1728 if (!claim)
1729 goto allow;
1730
1731 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001732 if (batadv_compare_eth(claim->backbone_gw->orig,
1733 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001734 /* if yes, the client has roamed and we have
1735 * to unclaim it.
1736 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001737 batadv_handle_unclaim(bat_priv, primary_if,
1738 primary_if->net_dev->dev_addr,
1739 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001740 goto allow;
1741 }
1742
1743 /* check if it is a multicast/broadcast frame */
1744 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1745 /* drop it. the responsible gateway has forwarded it into
1746 * the backbone network.
1747 */
1748 goto handled;
1749 } else {
1750 /* we must allow it. at least if we are
1751 * responsible for the DESTINATION.
1752 */
1753 goto allow;
1754 }
1755allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001756 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001757 ret = 0;
1758 goto out;
1759handled:
1760 ret = 1;
1761out:
1762 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001763 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001764 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001765 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001766 return ret;
1767}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001768
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001769/**
1770 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1771 * @seq: seq file to print on
1772 * @offset: not used
1773 *
1774 * Return: always 0
1775 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001776int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001777{
1778 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001779 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001780 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001781 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001782 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001783 struct hlist_head *head;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001784 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001785 u32 i;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001786 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001787 u8 *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001788
Marek Lindner30da63a2012-08-03 17:15:46 +02001789 primary_if = batadv_seq_print_text_primary_if_get(seq);
1790 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001791 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001792
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001793 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001794 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001795 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001796 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001797 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001798 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001799 "Client", "VID", "Originator", "CRC");
1800 for (i = 0; i < hash->size; i++) {
1801 head = &hash->table[i];
1802
1803 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001804 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001805 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1806 primary_addr);
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001807
1808 spin_lock_bh(&claim->backbone_gw->crc_lock);
1809 backbone_crc = claim->backbone_gw->crc;
1810 spin_unlock_bh(&claim->backbone_gw->crc_lock);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001811 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001812 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001813 claim->backbone_gw->orig,
1814 (is_own ? 'x' : ' '),
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001815 backbone_crc);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001816 }
1817 rcu_read_unlock();
1818 }
1819out:
1820 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001821 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001822 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001823}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001824
Simon Wunderlich04e14be2015-11-06 10:45:19 +01001825/**
1826 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1827 * file
1828 * @seq: seq file to print on
1829 * @offset: not used
1830 *
1831 * Return: always 0
1832 */
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001833int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1834{
1835 struct net_device *net_dev = (struct net_device *)seq->private;
1836 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001837 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001838 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001839 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001840 struct hlist_head *head;
1841 int secs, msecs;
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001842 u16 backbone_crc;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001843 u32 i;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001844 bool is_own;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001845 u8 *primary_addr;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001846
Marek Lindner30da63a2012-08-03 17:15:46 +02001847 primary_if = batadv_seq_print_text_primary_if_get(seq);
1848 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001849 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001850
1851 primary_addr = primary_if->net_dev->dev_addr;
1852 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001853 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001854 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001855 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001856 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001857 "Originator", "VID", "last seen", "CRC");
1858 for (i = 0; i < hash->size; i++) {
1859 head = &hash->table[i];
1860
1861 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001862 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001863 msecs = jiffies_to_msecs(jiffies -
1864 backbone_gw->lasttime);
1865 secs = msecs / 1000;
1866 msecs = msecs % 1000;
1867
1868 is_own = batadv_compare_eth(backbone_gw->orig,
1869 primary_addr);
1870 if (is_own)
1871 continue;
1872
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001873 spin_lock_bh(&backbone_gw->crc_lock);
1874 backbone_crc = backbone_gw->crc;
1875 spin_unlock_bh(&backbone_gw->crc_lock);
1876
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001877 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001878 backbone_gw->orig,
1879 BATADV_PRINT_VID(backbone_gw->vid), secs,
Simon Wunderlich5a1dd8a2015-09-11 18:04:13 +02001880 msecs, backbone_crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001881 }
1882 rcu_read_unlock();
1883 }
1884out:
1885 if (primary_if)
1886 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001887 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001888}