blob: f2ac903e091efb1be56f48dc31f49c6116a90f02 [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
18#include "main.h"
19#include "hash.h"
20#include "hard-interface.h"
21#include "originator.h"
22#include "bridge_loop_avoidance.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010023#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010024#include "send.h"
25
26#include <linux/etherdevice.h>
27#include <linux/crc16.h>
28#include <linux/if_arp.h>
29#include <net/arp.h>
30#include <linux/if_vlan.h>
31
Sven Eckelmann3b300de2012-05-12 18:33:53 +020032static const uint8_t batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010033
Sven Eckelmann3b300de2012-05-12 18:33:53 +020034static void batadv_bla_periodic_work(struct work_struct *work);
Marek Lindnerbae98772012-12-25 17:03:24 +080035static void
36batadv_bla_send_announce(struct batadv_priv *bat_priv,
37 struct batadv_bla_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010038
39/* return the index of the claim */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020040static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
Simon Wunderlich23721382012-01-22 20:00:19 +010041{
Marek Lindner712bbfe42012-12-25 17:03:25 +080042 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Simon Wunderlich23721382012-01-22 20:00:19 +010043 uint32_t hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010044
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010045 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
46 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010047
48 return hash % size;
49}
50
51/* return the index of the backbone gateway */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020052static inline uint32_t batadv_choose_backbone_gw(const void *data,
53 uint32_t size)
Simon Wunderlich23721382012-01-22 20:00:19 +010054{
Marek Lindner712bbfe42012-12-25 17:03:25 +080055 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
Simon Wunderlich23721382012-01-22 20:00:19 +010056 uint32_t hash = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +010057
Sven Eckelmann36fd61c2015-03-01 09:46:18 +010058 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
59 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
Simon Wunderlich23721382012-01-22 20:00:19 +010060
61 return hash % size;
62}
63
Simon Wunderlich23721382012-01-22 20:00:19 +010064/* compares address and vid of two backbone gws */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020065static int batadv_compare_backbone_gw(const struct hlist_node *node,
66 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010067{
Marek Lindnerbae98772012-12-25 17:03:24 +080068 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +010069 hash_entry);
Marek Lindnerbae98772012-12-25 17:03:24 +080070 const struct batadv_bla_backbone_gw *gw1 = data1, *gw2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +010071
Simon Wunderlichc76d1522012-10-15 22:38:04 +020072 if (!batadv_compare_eth(gw1->orig, gw2->orig))
73 return 0;
74
75 if (gw1->vid != gw2->vid)
76 return 0;
77
78 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +010079}
80
81/* compares address and vid of two claims */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020082static int batadv_compare_claim(const struct hlist_node *node,
83 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010084{
Marek Lindner712bbfe42012-12-25 17:03:25 +080085 const void *data1 = container_of(node, struct batadv_bla_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +010086 hash_entry);
Marek Lindner712bbfe42012-12-25 17:03:25 +080087 const struct batadv_bla_claim *cl1 = data1, *cl2 = data2;
Simon Wunderlich23721382012-01-22 20:00:19 +010088
Simon Wunderlichc76d1522012-10-15 22:38:04 +020089 if (!batadv_compare_eth(cl1->addr, cl2->addr))
90 return 0;
91
92 if (cl1->vid != cl2->vid)
93 return 0;
94
95 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +010096}
97
98/* free a backbone gw */
Marek Lindnerbae98772012-12-25 17:03:24 +080099static void
100batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100101{
102 if (atomic_dec_and_test(&backbone_gw->refcount))
103 kfree_rcu(backbone_gw, rcu);
104}
105
106/* finally deinitialize the claim */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200107static void batadv_claim_free_rcu(struct rcu_head *rcu)
Simon Wunderlich23721382012-01-22 20:00:19 +0100108{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800109 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100110
Marek Lindner712bbfe42012-12-25 17:03:25 +0800111 claim = container_of(rcu, struct batadv_bla_claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100112
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200113 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100114 kfree(claim);
115}
116
117/* free a claim, call claim_free_rcu if its the last reference */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800118static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100119{
120 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200121 call_rcu(&claim->rcu, batadv_claim_free_rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100122}
123
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100124/**
125 * batadv_claim_hash_find
126 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100127 * @data: search data (may be local/static data)
128 *
129 * looks for a claim in the hash, and returns it if found
130 * or NULL otherwise.
131 */
Marek Lindner712bbfe42012-12-25 17:03:25 +0800132static struct batadv_bla_claim
133*batadv_claim_hash_find(struct batadv_priv *bat_priv,
134 struct batadv_bla_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100135{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200136 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100137 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800138 struct batadv_bla_claim *claim;
139 struct batadv_bla_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100140 int index;
141
142 if (!hash)
143 return NULL;
144
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200145 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100146 head = &hash->table[index];
147
148 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800149 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200150 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100151 continue;
152
153 if (!atomic_inc_not_zero(&claim->refcount))
154 continue;
155
156 claim_tmp = claim;
157 break;
158 }
159 rcu_read_unlock();
160
161 return claim_tmp;
162}
163
Ben Hutchings2c530402012-07-10 10:55:09 +0000164/**
165 * batadv_backbone_hash_find - looks for a claim in the hash
166 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100167 * @addr: the address of the originator
168 * @vid: the VLAN ID
169 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000170 * Returns claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100171 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800172static struct batadv_bla_backbone_gw *
Sven Eckelmann56303d32012-06-05 22:31:31 +0200173batadv_backbone_hash_find(struct batadv_priv *bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200174 uint8_t *addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100175{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200176 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100177 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +0800178 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
179 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100180 int index;
181
182 if (!hash)
183 return NULL;
184
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100185 ether_addr_copy(search_entry.orig, addr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100186 search_entry.vid = vid;
187
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200188 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100189 head = &hash->table[index];
190
191 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800192 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200193 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
194 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100195 continue;
196
197 if (!atomic_inc_not_zero(&backbone_gw->refcount))
198 continue;
199
200 backbone_gw_tmp = backbone_gw;
201 break;
202 }
203 rcu_read_unlock();
204
205 return backbone_gw_tmp;
206}
207
208/* delete all claims for a backbone */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200209static void
Marek Lindnerbae98772012-12-25 17:03:24 +0800210batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100211{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200212 struct batadv_hashtable *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800213 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100214 struct hlist_head *head;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800215 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100216 int i;
217 spinlock_t *list_lock; /* protects write access to the hash lists */
218
Sven Eckelmann807736f2012-07-15 22:26:51 +0200219 hash = backbone_gw->bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100220 if (!hash)
221 return;
222
223 for (i = 0; i < hash->size; i++) {
224 head = &hash->table[i];
225 list_lock = &hash->list_locks[i];
226
227 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800228 hlist_for_each_entry_safe(claim, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +0100229 head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100230 if (claim->backbone_gw != backbone_gw)
231 continue;
232
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200233 batadv_claim_free_ref(claim);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800234 hlist_del_rcu(&claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100235 }
236 spin_unlock_bh(list_lock);
237 }
238
Antonio Quartulli3f687852014-11-02 11:29:56 +0100239 /* all claims gone, initialize CRC */
Sven Eckelmann3964f722012-06-03 22:19:10 +0200240 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100241}
242
Ben Hutchings2c530402012-07-10 10:55:09 +0000243/**
244 * batadv_bla_send_claim - sends a claim frame according to the provided info
245 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200246 * @mac: the mac address to be announced within the claim
Simon Wunderlich23721382012-01-22 20:00:19 +0100247 * @vid: the VLAN ID
248 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100249 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200250static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200251 unsigned short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100252{
253 struct sk_buff *skb;
254 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200255 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100256 struct net_device *soft_iface;
257 uint8_t *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200258 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100259 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100260
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200261 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100262 if (!primary_if)
263 return;
264
Sven Eckelmann807736f2012-07-15 22:26:51 +0200265 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100266 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100267 local_claim_dest.type = claimtype;
268
269 soft_iface = primary_if->soft_iface;
270
271 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
272 /* IP DST: 0.0.0.0 */
273 zeroip,
274 primary_if->soft_iface,
275 /* IP SRC: 0.0.0.0 */
276 zeroip,
277 /* Ethernet DST: Broadcast */
278 NULL,
279 /* Ethernet SRC/HW SRC: originator mac */
280 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200281 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100282 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100283 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100284 */
285 (uint8_t *)&local_claim_dest);
286
287 if (!skb)
288 goto out;
289
290 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100291 hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100292
293 /* now we pretend that the client would have sent this ... */
294 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200295 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100296 /* normal claim frame
297 * set Ethernet SRC to the clients mac
298 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100299 ether_addr_copy(ethhdr->h_source, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200300 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200301 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
302 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100303 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200304 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100305 /* unclaim frame
306 * set HW SRC to the clients mac
307 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100308 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200309 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200310 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200311 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100312 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200313 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100314 /* announcement frame
315 * set HW SRC to the special mac containg the crc
316 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100317 ether_addr_copy(hw_src, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200318 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200319 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200320 ethhdr->h_source, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100321 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200322 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100323 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200324 * set HW SRC and header destination to the receiving backbone
325 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100326 */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100327 ether_addr_copy(hw_src, mac);
328 ether_addr_copy(ethhdr->h_dest, mac);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200329 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200330 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200331 ethhdr->h_source, ethhdr->h_dest,
332 BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100333 break;
Simon Wunderlich23721382012-01-22 20:00:19 +0100334 }
335
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200336 if (vid & BATADV_VLAN_HAS_TAG)
337 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
338 vid & VLAN_VID_MASK);
Simon Wunderlich23721382012-01-22 20:00:19 +0100339
340 skb_reset_mac_header(skb);
341 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200342 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
343 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
344 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100345 soft_iface->last_rx = jiffies;
346
347 netif_rx(skb);
348out:
349 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200350 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100351}
352
Ben Hutchings2c530402012-07-10 10:55:09 +0000353/**
354 * batadv_bla_get_backbone_gw
355 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100356 * @orig: the mac address of the originator
357 * @vid: the VLAN ID
Martin Hundebølle3357182014-07-15 09:41:06 +0200358 * @own_backbone: set if the requested backbone is local
Simon Wunderlich23721382012-01-22 20:00:19 +0100359 *
360 * searches for the backbone gw or creates a new one if it could not
361 * be found.
362 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800363static struct batadv_bla_backbone_gw *
Sven Eckelmann56303d32012-06-05 22:31:31 +0200364batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200365 unsigned short vid, bool own_backbone)
Simon Wunderlich23721382012-01-22 20:00:19 +0100366{
Marek Lindnerbae98772012-12-25 17:03:24 +0800367 struct batadv_bla_backbone_gw *entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200368 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100369 int hash_added;
370
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200371 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100372
373 if (entry)
374 return entry;
375
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200376 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200377 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200378 orig, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100379
380 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
381 if (!entry)
382 return NULL;
383
384 entry->vid = vid;
385 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200386 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100387 entry->bat_priv = bat_priv;
388 atomic_set(&entry->request_sent, 0);
Simon Wunderlich28709872012-09-13 18:18:46 +0200389 atomic_set(&entry->wait_periods, 0);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100390 ether_addr_copy(entry->orig, orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100391
392 /* one for the hash, one for returning */
393 atomic_set(&entry->refcount, 2);
394
Sven Eckelmann807736f2012-07-15 22:26:51 +0200395 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200396 batadv_compare_backbone_gw,
397 batadv_choose_backbone_gw, entry,
398 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100399
400 if (unlikely(hash_added != 0)) {
401 /* hash failed, free the structure */
402 kfree(entry);
403 return NULL;
404 }
405
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200406 /* this is a gateway now, remove any TT entry on this VLAN */
Sven Eckelmannda641192012-05-12 13:48:56 +0200407 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100408 if (orig_node) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200409 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200410 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200411 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100412 }
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200413
Simon Wunderlichd807f272012-09-09 22:27:57 +0200414 if (own_backbone) {
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200415 batadv_bla_send_announce(bat_priv, entry);
416
Simon Wunderlichd807f272012-09-09 22:27:57 +0200417 /* this will be decreased in the worker thread */
418 atomic_inc(&entry->request_sent);
Simon Wunderlich28709872012-09-13 18:18:46 +0200419 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
Simon Wunderlichd807f272012-09-09 22:27:57 +0200420 atomic_inc(&bat_priv->bla.num_requests);
421 }
422
Simon Wunderlich23721382012-01-22 20:00:19 +0100423 return entry;
424}
425
426/* update or add the own backbone gw to make sure we announce
427 * where we receive other backbone gws
428 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200429static void
430batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
431 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200432 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100433{
Marek Lindnerbae98772012-12-25 17:03:24 +0800434 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100435
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200436 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
437 primary_if->net_dev->dev_addr,
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200438 vid, true);
Simon Wunderlich23721382012-01-22 20:00:19 +0100439 if (unlikely(!backbone_gw))
440 return;
441
442 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200443 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100444}
445
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100446/**
447 * batadv_bla_answer_request - answer a bla request by sending own claims
448 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200449 * @primary_if: interface where the request came on
Simon Wunderlich23721382012-01-22 20:00:19 +0100450 * @vid: the vid where the request came on
451 *
452 * Repeat all of our own claims, and finally send an ANNOUNCE frame
453 * to allow the requester another check if the CRC is correct now.
454 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200455static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
456 struct batadv_hard_iface *primary_if,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200457 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100458{
Simon Wunderlich23721382012-01-22 20:00:19 +0100459 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200460 struct batadv_hashtable *hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +0800461 struct batadv_bla_claim *claim;
Marek Lindnerbae98772012-12-25 17:03:24 +0800462 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100463 int i;
464
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200465 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200466 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100467
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200468 backbone_gw = batadv_backbone_hash_find(bat_priv,
469 primary_if->net_dev->dev_addr,
470 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100471 if (!backbone_gw)
472 return;
473
Sven Eckelmann807736f2012-07-15 22:26:51 +0200474 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100475 for (i = 0; i < hash->size; i++) {
476 head = &hash->table[i];
477
478 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800479 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +0100480 /* only own claims are interesting */
481 if (claim->backbone_gw != backbone_gw)
482 continue;
483
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200484 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200485 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100486 }
487 rcu_read_unlock();
488 }
489
490 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200491 batadv_bla_send_announce(bat_priv, backbone_gw);
492 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100493}
494
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100495/**
496 * batadv_bla_send_request - send a request to repeat claims
497 * @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100498 *
499 * When the crc is wrong, ask the backbone gateway for a full table update.
500 * After the request, it will repeat all of his own claims and finally
501 * send an announcement claim with which we can check again.
502 */
Marek Lindnerbae98772012-12-25 17:03:24 +0800503static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100504{
505 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200506 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100507
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200508 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
509 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100510
511 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200512 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200513 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100514
515 /* no local broadcasts should be sent or received, for now. */
516 if (!atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200517 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100518 atomic_set(&backbone_gw->request_sent, 1);
519 }
520}
521
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100522/**
523 * batadv_bla_send_announce
524 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100525 * @backbone_gw: our backbone gateway which should be announced
526 *
527 * This function sends an announcement. It is called from multiple
528 * places.
529 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200530static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
Marek Lindnerbae98772012-12-25 17:03:24 +0800531 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100532{
533 uint8_t mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100534 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100535
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200536 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich23721382012-01-22 20:00:19 +0100537 crc = htons(backbone_gw->crc);
Al Viro1a5852d2012-04-22 07:50:29 +0100538 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100539
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200540 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200541 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100542}
543
Ben Hutchings2c530402012-07-10 10:55:09 +0000544/**
545 * batadv_bla_add_claim - Adds a claim in the claim hash
546 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100547 * @mac: the mac address of the claim
548 * @vid: the VLAN ID of the frame
549 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100550 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200551static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200552 const uint8_t *mac, const unsigned short vid,
Marek Lindnerbae98772012-12-25 17:03:24 +0800553 struct batadv_bla_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100554{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800555 struct batadv_bla_claim *claim;
556 struct batadv_bla_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100557 int hash_added;
558
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100559 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100560 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200561 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100562
563 /* create a new claim entry if it does not exist yet. */
564 if (!claim) {
565 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
566 if (!claim)
567 return;
568
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100569 ether_addr_copy(claim->addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100570 claim->vid = vid;
571 claim->lasttime = jiffies;
572 claim->backbone_gw = backbone_gw;
573
574 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200575 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200576 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200577 mac, BATADV_PRINT_VID(vid));
Sven Eckelmann807736f2012-07-15 22:26:51 +0200578 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200579 batadv_compare_claim,
580 batadv_choose_claim, claim,
581 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100582
583 if (unlikely(hash_added != 0)) {
584 /* only local changes happened. */
585 kfree(claim);
586 return;
587 }
588 } else {
589 claim->lasttime = jiffies;
590 if (claim->backbone_gw == backbone_gw)
591 /* no need to register a new backbone */
592 goto claim_free_ref;
593
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200594 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200595 "bla_add_claim(): changing ownership for %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200596 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100597
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200598 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200599 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100600 }
601 /* set (new) backbone gw */
602 atomic_inc(&backbone_gw->refcount);
603 claim->backbone_gw = backbone_gw;
604
605 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
606 backbone_gw->lasttime = jiffies;
607
608claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200609 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100610}
611
612/* Delete a claim from the claim hash which has the
613 * given mac address and vid.
614 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200615static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200616 const uint8_t *mac, const unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100617{
Marek Lindner712bbfe42012-12-25 17:03:25 +0800618 struct batadv_bla_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100619
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100620 ether_addr_copy(search_claim.addr, mac);
Simon Wunderlich23721382012-01-22 20:00:19 +0100621 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200622 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100623 if (!claim)
624 return;
625
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200626 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200627 mac, BATADV_PRINT_VID(vid));
Simon Wunderlich23721382012-01-22 20:00:19 +0100628
Sven Eckelmann807736f2012-07-15 22:26:51 +0200629 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200630 batadv_choose_claim, claim);
631 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100632
633 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
634
635 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200636 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100637}
638
639/* check for ANNOUNCE frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200640static int batadv_handle_announce(struct batadv_priv *bat_priv,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200641 uint8_t *an_addr, uint8_t *backbone_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200642 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100643{
Marek Lindnerbae98772012-12-25 17:03:24 +0800644 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100645 uint16_t crc;
646
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200647 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100648 return 0;
649
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200650 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
651 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100652
653 if (unlikely(!backbone_gw))
654 return 1;
655
Simon Wunderlich23721382012-01-22 20:00:19 +0100656 /* handle as ANNOUNCE frame */
657 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100658 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100659
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200660 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100661 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200662 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100663
664 if (backbone_gw->crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200665 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100666 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200667 backbone_gw->orig,
668 BATADV_PRINT_VID(backbone_gw->vid),
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200669 backbone_gw->crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100670
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200671 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100672 } else {
673 /* if we have sent a request and the crc was OK,
674 * we can allow traffic again.
675 */
676 if (atomic_read(&backbone_gw->request_sent)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200677 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +0100678 atomic_set(&backbone_gw->request_sent, 0);
679 }
680 }
681
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200682 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100683 return 1;
684}
685
686/* check for REQUEST frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200687static int batadv_handle_request(struct batadv_priv *bat_priv,
688 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200689 uint8_t *backbone_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200690 struct ethhdr *ethhdr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100691{
692 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200693 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100694 return 0;
695
696 /* sanity check, this should not happen on a normal switch,
697 * we ignore it in this case.
698 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200699 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100700 return 1;
701
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200702 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200703 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200704 BATADV_PRINT_VID(vid), ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100705
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200706 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100707 return 1;
708}
709
710/* check for UNCLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200711static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
712 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200713 uint8_t *backbone_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200714 uint8_t *claim_addr, unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100715{
Marek Lindnerbae98772012-12-25 17:03:24 +0800716 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100717
718 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200719 if (primary_if && batadv_compare_eth(backbone_addr,
720 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200721 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200722 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100723
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200724 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100725
726 if (!backbone_gw)
727 return 1;
728
729 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200730 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200731 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +0200732 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100733
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200734 batadv_bla_del_claim(bat_priv, claim_addr, vid);
735 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100736 return 1;
737}
738
739/* check for CLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200740static int batadv_handle_claim(struct batadv_priv *bat_priv,
741 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200742 uint8_t *backbone_addr, uint8_t *claim_addr,
Antonio Quartullieb2deb62013-04-19 18:07:00 +0200743 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100744{
Marek Lindnerbae98772012-12-25 17:03:24 +0800745 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100746
747 /* register the gateway if not yet available, and add the claim. */
748
Simon Wunderlich52aebd62012-09-08 18:02:53 +0200749 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
750 false);
Simon Wunderlich23721382012-01-22 20:00:19 +0100751
752 if (unlikely(!backbone_gw))
753 return 1;
754
755 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200756 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200757 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200758 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200759 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100760
761 /* TODO: we could call something like tt_local_del() here. */
762
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200763 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100764 return 1;
765}
766
Ben Hutchings2c530402012-07-10 10:55:09 +0000767/**
768 * batadv_check_claim_group
769 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200770 * @primary_if: the primary interface of this batman interface
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100771 * @hw_src: the Hardware source in the ARP Header
772 * @hw_dst: the Hardware destination in the ARP Header
773 * @ethhdr: pointer to the Ethernet header of the claim frame
774 *
775 * checks if it is a claim packet and if its on the same group.
776 * This function also applies the group ID of the sender
777 * if it is in the same mesh.
778 *
779 * returns:
780 * 2 - if it is a claim packet and on the same group
781 * 1 - if is a claim packet from another group
782 * 0 - if it is not a claim packet
783 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200784static int batadv_check_claim_group(struct batadv_priv *bat_priv,
785 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200786 uint8_t *hw_src, uint8_t *hw_dst,
787 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100788{
789 uint8_t *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200790 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200791 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100792
Sven Eckelmann96412692012-06-05 22:31:30 +0200793 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200794 bla_dst_own = &bat_priv->bla.claim_dest;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100795
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100796 /* if announcement packet, use the source,
797 * otherwise assume it is in the hw_src
798 */
799 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200800 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100801 backbone_addr = hw_src;
802 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200803 case BATADV_CLAIM_TYPE_REQUEST:
804 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200805 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100806 backbone_addr = ethhdr->h_source;
807 break;
808 default:
809 return 0;
810 }
811
812 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200813 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100814 return 0;
815
816 /* if its already the same group, it is fine. */
817 if (bla_dst->group == bla_dst_own->group)
818 return 2;
819
820 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200821 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100822
823 /* dont accept claims from gateways which are not in
824 * the same mesh or group.
825 */
826 if (!orig_node)
827 return 1;
828
829 /* if our mesh friends mac is bigger, use it for ourselves. */
830 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200831 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Antonio Quartulli39a32992012-11-19 09:01:43 +0100832 "taking other backbones claim group: %#.4x\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200833 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100834 bla_dst_own->group = bla_dst->group;
835 }
836
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200837 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100838
839 return 2;
840}
841
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100842/**
843 * batadv_bla_process_claim
844 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebølle3357182014-07-15 09:41:06 +0200845 * @primary_if: the primary hard interface of this batman soft interface
Simon Wunderlich23721382012-01-22 20:00:19 +0100846 * @skb: the frame to be checked
847 *
848 * Check if this is a claim frame, and process it accordingly.
849 *
850 * returns 1 if it was a claim frame, otherwise return 0 to
851 * tell the callee that it can use the frame on its own.
852 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200853static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
854 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200855 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100856{
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200857 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200858 uint8_t *hw_src, *hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200859 struct vlan_hdr *vhdr, vhdr_buf;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200860 struct ethhdr *ethhdr;
861 struct arphdr *arphdr;
862 unsigned short vid;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200863 int vlan_depth = 0;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200864 __be16 proto;
Simon Wunderlich23721382012-01-22 20:00:19 +0100865 int headlen;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100866 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100867
Antonio Quartullic018ad32013-06-04 12:11:39 +0200868 vid = batadv_get_vid(skb, 0);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200869 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100870
Antonio Quartullic018ad32013-06-04 12:11:39 +0200871 proto = ethhdr->h_proto;
872 headlen = ETH_HLEN;
873 if (vid & BATADV_VLAN_HAS_TAG) {
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200874 /* Traverse the VLAN/Ethertypes.
875 *
876 * At this point it is known that the first protocol is a VLAN
877 * header, so start checking at the encapsulated protocol.
878 *
879 * The depth of the VLAN headers is recorded to drop BLA claim
880 * frames encapsulated into multiple VLAN headers (QinQ).
881 */
882 do {
883 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
884 &vhdr_buf);
885 if (!vhdr)
886 return 0;
887
888 proto = vhdr->h_vlan_encapsulated_proto;
889 headlen += VLAN_HLEN;
890 vlan_depth++;
891 } while (proto == htons(ETH_P_8021Q));
Simon Wunderlich23721382012-01-22 20:00:19 +0100892 }
893
Antonio Quartulli293e9332013-05-19 12:55:16 +0200894 if (proto != htons(ETH_P_ARP))
Simon Wunderlich23721382012-01-22 20:00:19 +0100895 return 0; /* not a claim frame */
896
897 /* this must be a ARP frame. check if it is a claim. */
898
899 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
900 return 0;
901
902 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200903 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +0100904 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
905
906 /* Check whether the ARP frame carries a valid
907 * IP information
908 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100909 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
910 return 0;
911 if (arphdr->ar_pro != htons(ETH_P_IP))
912 return 0;
913 if (arphdr->ar_hln != ETH_ALEN)
914 return 0;
915 if (arphdr->ar_pln != 4)
916 return 0;
917
918 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
919 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +0200920 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlichd46b6bf2014-06-23 15:55:36 +0200921 bla_dst_own = &bat_priv->bla.claim_dest;
922
923 /* check if it is a claim frame in general */
924 if (memcmp(bla_dst->magic, bla_dst_own->magic,
925 sizeof(bla_dst->magic)) != 0)
926 return 0;
927
928 /* check if there is a claim frame encapsulated deeper in (QinQ) and
929 * drop that, as this is not supported by BLA but should also not be
930 * sent via the mesh.
931 */
932 if (vlan_depth > 1)
933 return 1;
Simon Wunderlich23721382012-01-22 20:00:19 +0100934
935 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200936 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
937 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100938 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200939 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200940 "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 +0200941 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
942 hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100943
944 if (ret < 2)
945 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100946
947 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200948 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100949
950 /* check for the different types of claim frames ... */
951 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200952 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200953 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
954 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100955 return 1;
956 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200957 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200958 if (batadv_handle_unclaim(bat_priv, primary_if,
959 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100960 return 1;
961 break;
962
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200963 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200964 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
965 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100966 return 1;
967 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200968 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200969 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
970 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100971 return 1;
972 break;
973 }
974
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200975 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200976 "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 +0200977 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +0100978 return 1;
979}
980
981/* Check when we last heard from other nodes, and remove them in case of
982 * a time out, or clean all backbone gws if now is set.
983 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200984static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +0100985{
Marek Lindnerbae98772012-12-25 17:03:24 +0800986 struct batadv_bla_backbone_gw *backbone_gw;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800987 struct hlist_node *node_tmp;
Simon Wunderlich23721382012-01-22 20:00:19 +0100988 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200989 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100990 spinlock_t *list_lock; /* protects write access to the hash lists */
991 int i;
992
Sven Eckelmann807736f2012-07-15 22:26:51 +0200993 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100994 if (!hash)
995 return;
996
997 for (i = 0; i < hash->size; i++) {
998 head = &hash->table[i];
999 list_lock = &hash->list_locks[i];
1000
1001 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001002 hlist_for_each_entry_safe(backbone_gw, node_tmp,
Simon Wunderlich23721382012-01-22 20:00:19 +01001003 head, hash_entry) {
1004 if (now)
1005 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001006 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001007 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001008 continue;
1009
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001010 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001011 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1012 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +01001013
1014purge_now:
1015 /* don't wait for the pending request anymore */
1016 if (atomic_read(&backbone_gw->request_sent))
Sven Eckelmann807736f2012-07-15 22:26:51 +02001017 atomic_dec(&bat_priv->bla.num_requests);
Simon Wunderlich23721382012-01-22 20:00:19 +01001018
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001019 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001020
Sasha Levinb67bfe02013-02-27 17:06:00 -08001021 hlist_del_rcu(&backbone_gw->hash_entry);
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001022 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001023 }
1024 spin_unlock_bh(list_lock);
1025 }
1026}
1027
Ben Hutchings2c530402012-07-10 10:55:09 +00001028/**
1029 * batadv_bla_purge_claims
1030 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001031 * @primary_if: the selected primary interface, may be NULL if now is set
1032 * @now: whether the whole hash shall be wiped now
1033 *
1034 * Check when we heard last time from our own claims, and remove them in case of
1035 * a time out, or clean all claims if now is set
1036 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001037static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1038 struct batadv_hard_iface *primary_if,
1039 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +01001040{
Marek Lindner712bbfe42012-12-25 17:03:25 +08001041 struct batadv_bla_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +01001042 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001043 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001044 int i;
1045
Sven Eckelmann807736f2012-07-15 22:26:51 +02001046 hash = bat_priv->bla.claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001047 if (!hash)
1048 return;
1049
1050 for (i = 0; i < hash->size; i++) {
1051 head = &hash->table[i];
1052
1053 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001054 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001055 if (now)
1056 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001057 if (!batadv_compare_eth(claim->backbone_gw->orig,
1058 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001059 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001060 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001061 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001062 continue;
1063
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001064 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001065 "bla_purge_claims(): %pM, vid %d, time out\n",
1066 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001067
1068purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001069 batadv_handle_unclaim(bat_priv, primary_if,
1070 claim->backbone_gw->orig,
1071 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001072 }
1073 rcu_read_unlock();
1074 }
1075}
1076
Ben Hutchings2c530402012-07-10 10:55:09 +00001077/**
1078 * batadv_bla_update_orig_address
1079 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001080 * @primary_if: the new selected primary_if
1081 * @oldif: the old primary interface, may be NULL
1082 *
1083 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001084 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001085void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1086 struct batadv_hard_iface *primary_if,
1087 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001088{
Marek Lindnerbae98772012-12-25 17:03:24 +08001089 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001090 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001091 struct batadv_hashtable *hash;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001092 __be16 group;
Simon Wunderlich23721382012-01-22 20:00:19 +01001093 int i;
1094
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001095 /* reset bridge loop avoidance group id */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001096 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1097 bat_priv->bla.claim_dest.group = group;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001098
Simon Wunderlichd5b4c932013-06-07 16:52:05 +02001099 /* purge everything when bridge loop avoidance is turned off */
1100 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1101 oldif = NULL;
1102
Simon Wunderlich23721382012-01-22 20:00:19 +01001103 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001104 batadv_bla_purge_claims(bat_priv, NULL, 1);
1105 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001106 return;
1107 }
1108
Sven Eckelmann807736f2012-07-15 22:26:51 +02001109 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001110 if (!hash)
1111 return;
1112
1113 for (i = 0; i < hash->size; i++) {
1114 head = &hash->table[i];
1115
1116 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001117 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001118 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001119 if (!batadv_compare_eth(backbone_gw->orig,
1120 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001121 continue;
1122
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001123 ether_addr_copy(backbone_gw->orig,
1124 primary_if->net_dev->dev_addr);
Simon Wunderlich23721382012-01-22 20:00:19 +01001125 /* send an announce frame so others will ask for our
1126 * claims and update their tables.
1127 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001128 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001129 }
1130 rcu_read_unlock();
1131 }
1132}
1133
Simon Wunderlich23721382012-01-22 20:00:19 +01001134/* periodic work to do:
1135 * * purge structures when they are too old
1136 * * send announcements
1137 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001138static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001139{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001140 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001141 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001142 struct batadv_priv_bla *priv_bla;
Simon Wunderlich23721382012-01-22 20:00:19 +01001143 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001144 struct batadv_bla_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001145 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001146 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001147 int i;
1148
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001149 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001150 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1151 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001152 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001153 if (!primary_if)
1154 goto out;
1155
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001156 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1157 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001158
1159 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1160 goto out;
1161
Sven Eckelmann807736f2012-07-15 22:26:51 +02001162 hash = bat_priv->bla.backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001163 if (!hash)
1164 goto out;
1165
1166 for (i = 0; i < hash->size; i++) {
1167 head = &hash->table[i];
1168
1169 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001170 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001171 if (!batadv_compare_eth(backbone_gw->orig,
1172 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001173 continue;
1174
1175 backbone_gw->lasttime = jiffies;
1176
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001177 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlichd807f272012-09-09 22:27:57 +02001178
1179 /* request_sent is only set after creation to avoid
1180 * problems when we are not yet known as backbone gw
1181 * in the backbone.
1182 *
Simon Wunderlich28709872012-09-13 18:18:46 +02001183 * We can reset this now after we waited some periods
1184 * to give bridge forward delays and bla group forming
1185 * some grace time.
Simon Wunderlichd807f272012-09-09 22:27:57 +02001186 */
1187
1188 if (atomic_read(&backbone_gw->request_sent) == 0)
1189 continue;
1190
Simon Wunderlich28709872012-09-13 18:18:46 +02001191 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1192 continue;
1193
Simon Wunderlichd807f272012-09-09 22:27:57 +02001194 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1195 atomic_set(&backbone_gw->request_sent, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001196 }
1197 rcu_read_unlock();
1198 }
1199out:
1200 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001201 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001202
Antonio Quartulli72414442012-12-25 13:14:37 +01001203 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1204 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001205}
1206
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001207/* The hash for claim and backbone hash receive the same key because they
1208 * are getting initialized by hash_new with the same key. Reinitializing
1209 * them with to different keys to allow nested locking without generating
1210 * lockdep warnings
1211 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001212static struct lock_class_key batadv_claim_hash_lock_class_key;
1213static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001214
Simon Wunderlich23721382012-01-22 20:00:19 +01001215/* initialize all bla structures */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001216int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001217{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001218 int i;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001219 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001220 struct batadv_hard_iface *primary_if;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001221 uint16_t crc;
1222 unsigned long entrytime;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001223
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001224 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1225
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001226 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001227
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001228 /* setting claim destination address */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001229 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1230 bat_priv->bla.claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001231 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001232 if (primary_if) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001233 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1234 bat_priv->bla.claim_dest.group = htons(crc);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001235 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001236 } else {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001237 bat_priv->bla.claim_dest.group = 0; /* will be set later */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001238 }
1239
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001240 /* initialize the duplicate list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001241 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001242 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Sven Eckelmann807736f2012-07-15 22:26:51 +02001243 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1244 bat_priv->bla.bcast_duplist_curr = 0;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001245
Sven Eckelmann807736f2012-07-15 22:26:51 +02001246 if (bat_priv->bla.claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001247 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001248
Sven Eckelmann807736f2012-07-15 22:26:51 +02001249 bat_priv->bla.claim_hash = batadv_hash_new(128);
1250 bat_priv->bla.backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001251
Sven Eckelmann807736f2012-07-15 22:26:51 +02001252 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001253 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001254
Sven Eckelmann807736f2012-07-15 22:26:51 +02001255 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001256 &batadv_claim_hash_lock_class_key);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001257 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001258 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001259
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001260 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001261
Antonio Quartulli72414442012-12-25 13:14:37 +01001262 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1263
1264 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1265 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Sven Eckelmann5346c352012-05-05 13:27:28 +02001266 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001267}
1268
Ben Hutchings2c530402012-07-10 10:55:09 +00001269/**
1270 * batadv_bla_check_bcast_duplist
1271 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001272 * @skb: contains the bcast_packet to be checked
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001273 *
1274 * check if it is on our broadcast list. Another gateway might
1275 * have sent the same packet because it is connected to the same backbone,
1276 * so we have to remove this duplicate.
1277 *
1278 * This is performed by checking the CRC, which will tell us
1279 * with a good chance that it is the same packet. If it is furthermore
1280 * sent by another host, drop it. We allow equal packets from
1281 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001282 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001283int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001284 struct sk_buff *skb)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001285{
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001286 int i, curr, ret = 0;
1287 __be32 crc;
1288 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001289 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001290
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001291 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001292
1293 /* calculate the crc ... */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001294 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001295
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001296 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1297
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001298 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02001299 curr = (bat_priv->bla.bcast_duplist_curr + i);
1300 curr %= BATADV_DUPLIST_SIZE;
1301 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001302
1303 /* we can stop searching if the entry is too old ;
1304 * later entries will be even older
1305 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001306 if (batadv_has_timed_out(entry->entrytime,
1307 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001308 break;
1309
1310 if (entry->crc != crc)
1311 continue;
1312
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001313 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001314 continue;
1315
1316 /* this entry seems to match: same crc, not too old,
1317 * and from another gw. therefore return 1 to forbid it.
1318 */
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001319 ret = 1;
1320 goto out;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001321 }
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001322 /* not found, add a new entry (overwrite the oldest entry)
Antonio Quartulli3f687852014-11-02 11:29:56 +01001323 * and allow it, its the first occurrence.
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001324 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001325 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001326 curr %= BATADV_DUPLIST_SIZE;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001327 entry = &bat_priv->bla.bcast_duplist[curr];
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001328 entry->crc = crc;
1329 entry->entrytime = jiffies;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001330 ether_addr_copy(entry->orig, bcast_packet->orig);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001331 bat_priv->bla.bcast_duplist_curr = curr;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001332
Linus Lüssing7dac7b72012-10-17 14:53:05 +02001333out:
1334 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1335
1336 return ret;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001337}
1338
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001339/**
1340 * batadv_bla_is_backbone_gw_orig
1341 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001342 * @orig: originator mac address
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001343 * @vid: VLAN identifier
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001344 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001345 * Check if the originator is a gateway for the VLAN identified by vid.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001346 *
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001347 * Returns true if orig is a backbone for this vid, false otherwise.
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001348 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001349bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig,
1350 unsigned short vid)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001351{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001352 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001353 struct hlist_head *head;
Marek Lindnerbae98772012-12-25 17:03:24 +08001354 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001355 int i;
1356
1357 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001358 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001359
1360 if (!hash)
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001361 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001362
1363 for (i = 0; i < hash->size; i++) {
1364 head = &hash->table[i];
1365
1366 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001367 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001368 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1369 backbone_gw->vid == vid) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001370 rcu_read_unlock();
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001371 return true;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001372 }
1373 }
1374 rcu_read_unlock();
1375 }
1376
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001377 return false;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001378}
1379
Ben Hutchings2c530402012-07-10 10:55:09 +00001380/**
1381 * batadv_bla_is_backbone_gw
1382 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001383 * @orig_node: the orig_node of the frame
1384 * @hdr_size: maximum length of the frame
1385 *
1386 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1387 * if the orig_node is also a gateway on the soft interface, otherwise it
1388 * returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001389 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001390int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001391 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001392{
Marek Lindnerbae98772012-12-25 17:03:24 +08001393 struct batadv_bla_backbone_gw *backbone_gw;
Antonio Quartullic018ad32013-06-04 12:11:39 +02001394 unsigned short vid;
Simon Wunderlich23721382012-01-22 20:00:19 +01001395
1396 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1397 return 0;
1398
1399 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001400 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001401 return 0;
1402
Antonio Quartullic018ad32013-06-04 12:11:39 +02001403 vid = batadv_get_vid(skb, hdr_size);
Simon Wunderlich23721382012-01-22 20:00:19 +01001404
1405 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001406 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1407 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001408 if (!backbone_gw)
1409 return 0;
1410
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001411 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001412 return 1;
1413}
1414
1415/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001416void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001417{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001418 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001419
Sven Eckelmann807736f2012-07-15 22:26:51 +02001420 cancel_delayed_work_sync(&bat_priv->bla.work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001421 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001422
Sven Eckelmann807736f2012-07-15 22:26:51 +02001423 if (bat_priv->bla.claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001424 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001425 batadv_hash_destroy(bat_priv->bla.claim_hash);
1426 bat_priv->bla.claim_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001427 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001428 if (bat_priv->bla.backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001429 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001430 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1431 bat_priv->bla.backbone_hash = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +01001432 }
1433 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001434 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001435}
1436
Ben Hutchings2c530402012-07-10 10:55:09 +00001437/**
1438 * batadv_bla_rx
1439 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001440 * @skb: the frame to be checked
1441 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001442 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001443 *
1444 * bla_rx avoidance checks if:
1445 * * we have to race for a claim
1446 * * if the frame is allowed on the LAN
1447 *
1448 * in these cases, the skb is further handled by this function and
1449 * returns 1, otherwise it returns 0 and the caller shall further
1450 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001451 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001452int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1453 unsigned short vid, bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001454{
1455 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001456 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001457 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001458 int ret;
1459
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001460 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001461
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001462 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001463 if (!primary_if)
1464 goto handled;
1465
1466 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1467 goto allow;
1468
Sven Eckelmann807736f2012-07-15 22:26:51 +02001469 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001470 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001471 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001472 goto handled;
1473
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001474 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001475 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001476 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001477
1478 if (!claim) {
1479 /* possible optimization: race for a claim */
1480 /* No claim exists yet, claim it for us!
1481 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001482 batadv_handle_claim(bat_priv, primary_if,
1483 primary_if->net_dev->dev_addr,
1484 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001485 goto allow;
1486 }
1487
1488 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001489 if (batadv_compare_eth(claim->backbone_gw->orig,
1490 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001491 /* ... allow it in any case */
1492 claim->lasttime = jiffies;
1493 goto allow;
1494 }
1495
1496 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001497 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1498 /* ... drop it. the responsible gateway is in charge.
1499 *
1500 * We need to check is_bcast because with the gateway
1501 * feature, broadcasts (like DHCP requests) may be sent
1502 * using a unicast packet type.
1503 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001504 goto handled;
1505 } else {
1506 /* seems the client considers us as its best gateway.
1507 * send a claim and update the claim table
1508 * immediately.
1509 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001510 batadv_handle_claim(bat_priv, primary_if,
1511 primary_if->net_dev->dev_addr,
1512 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001513 goto allow;
1514 }
1515allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001516 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001517 ret = 0;
1518 goto out;
1519
1520handled:
1521 kfree_skb(skb);
1522 ret = 1;
1523
1524out:
1525 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001526 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001527 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001528 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001529 return ret;
1530}
1531
Ben Hutchings2c530402012-07-10 10:55:09 +00001532/**
1533 * batadv_bla_tx
1534 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001535 * @skb: the frame to be checked
1536 * @vid: the VLAN ID of the frame
1537 *
1538 * bla_tx checks if:
1539 * * a claim was received which has to be processed
1540 * * the frame is allowed on the mesh
1541 *
1542 * in these cases, the skb is further handled by this function and
1543 * returns 1, otherwise it returns 0 and the caller shall further
1544 * process the skb.
Linus Lüssing9d2c9482013-08-06 20:21:15 +02001545 *
1546 * This call might reallocate skb data.
Simon Wunderlich23721382012-01-22 20:00:19 +01001547 */
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001548int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1549 unsigned short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001550{
1551 struct ethhdr *ethhdr;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001552 struct batadv_bla_claim search_claim, *claim = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001553 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001554 int ret = 0;
1555
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001556 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001557 if (!primary_if)
1558 goto out;
1559
1560 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1561 goto allow;
1562
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001563 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001564 goto handled;
1565
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001566 ethhdr = eth_hdr(skb);
Simon Wunderlich23721382012-01-22 20:00:19 +01001567
Sven Eckelmann807736f2012-07-15 22:26:51 +02001568 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
Simon Wunderlich23721382012-01-22 20:00:19 +01001569 /* don't allow broadcasts while requests are in flight */
1570 if (is_multicast_ether_addr(ethhdr->h_dest))
1571 goto handled;
1572
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001573 ether_addr_copy(search_claim.addr, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +01001574 search_claim.vid = vid;
1575
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001576 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001577
1578 /* if no claim exists, allow it. */
1579 if (!claim)
1580 goto allow;
1581
1582 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001583 if (batadv_compare_eth(claim->backbone_gw->orig,
1584 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001585 /* if yes, the client has roamed and we have
1586 * to unclaim it.
1587 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001588 batadv_handle_unclaim(bat_priv, primary_if,
1589 primary_if->net_dev->dev_addr,
1590 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001591 goto allow;
1592 }
1593
1594 /* check if it is a multicast/broadcast frame */
1595 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1596 /* drop it. the responsible gateway has forwarded it into
1597 * the backbone network.
1598 */
1599 goto handled;
1600 } else {
1601 /* we must allow it. at least if we are
1602 * responsible for the DESTINATION.
1603 */
1604 goto allow;
1605 }
1606allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001607 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001608 ret = 0;
1609 goto out;
1610handled:
1611 ret = 1;
1612out:
1613 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001614 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001615 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001616 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001617 return ret;
1618}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001619
Sven Eckelmann08adf152012-05-12 13:38:47 +02001620int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001621{
1622 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001623 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001624 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
Marek Lindner712bbfe42012-12-25 17:03:25 +08001625 struct batadv_bla_claim *claim;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001626 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001627 struct hlist_head *head;
1628 uint32_t i;
1629 bool is_own;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001630 uint8_t *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001631
Marek Lindner30da63a2012-08-03 17:15:46 +02001632 primary_if = batadv_seq_print_text_primary_if_get(seq);
1633 if (!primary_if)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001634 goto out;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001635
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001636 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001637 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001638 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001639 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001640 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001641 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001642 "Client", "VID", "Originator", "CRC");
1643 for (i = 0; i < hash->size; i++) {
1644 head = &hash->table[i];
1645
1646 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001647 hlist_for_each_entry_rcu(claim, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001648 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1649 primary_addr);
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001650 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001651 claim->addr, BATADV_PRINT_VID(claim->vid),
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001652 claim->backbone_gw->orig,
1653 (is_own ? 'x' : ' '),
1654 claim->backbone_gw->crc);
1655 }
1656 rcu_read_unlock();
1657 }
1658out:
1659 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001660 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001661 return 0;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001662}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001663
1664int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1665{
1666 struct net_device *net_dev = (struct net_device *)seq->private;
1667 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001668 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
Marek Lindnerbae98772012-12-25 17:03:24 +08001669 struct batadv_bla_backbone_gw *backbone_gw;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001670 struct batadv_hard_iface *primary_if;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001671 struct hlist_head *head;
1672 int secs, msecs;
1673 uint32_t i;
1674 bool is_own;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001675 uint8_t *primary_addr;
1676
Marek Lindner30da63a2012-08-03 17:15:46 +02001677 primary_if = batadv_seq_print_text_primary_if_get(seq);
1678 if (!primary_if)
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001679 goto out;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001680
1681 primary_addr = primary_if->net_dev->dev_addr;
1682 seq_printf(seq,
Antonio Quartulli39a32992012-11-19 09:01:43 +01001683 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001684 net_dev->name, primary_addr,
Sven Eckelmann807736f2012-07-15 22:26:51 +02001685 ntohs(bat_priv->bla.claim_dest.group));
Antonio Quartulli39a32992012-11-19 09:01:43 +01001686 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001687 "Originator", "VID", "last seen", "CRC");
1688 for (i = 0; i < hash->size; i++) {
1689 head = &hash->table[i];
1690
1691 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001692 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001693 msecs = jiffies_to_msecs(jiffies -
1694 backbone_gw->lasttime);
1695 secs = msecs / 1000;
1696 msecs = msecs % 1000;
1697
1698 is_own = batadv_compare_eth(backbone_gw->orig,
1699 primary_addr);
1700 if (is_own)
1701 continue;
1702
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001703 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
Antonio Quartulli5f80df62013-04-19 18:07:01 +02001704 backbone_gw->orig,
1705 BATADV_PRINT_VID(backbone_gw->vid), secs,
Antonio Quartullieb2deb62013-04-19 18:07:00 +02001706 msecs, backbone_gw->crc);
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001707 }
1708 rcu_read_unlock();
1709 }
1710out:
1711 if (primary_if)
1712 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001713 return 0;
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001714}