blob: b7d70845aa4ac99b867bcb140e80b1ee54f2993c [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2011-2012 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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Simon Wunderlich23721382012-01-22 20:00:19 +010018 */
19
20#include "main.h"
21#include "hash.h"
22#include "hard-interface.h"
23#include "originator.h"
24#include "bridge_loop_avoidance.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010025#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010026#include "send.h"
27
28#include <linux/etherdevice.h>
29#include <linux/crc16.h>
30#include <linux/if_arp.h>
31#include <net/arp.h>
32#include <linux/if_vlan.h>
33
Simon Wunderlich23721382012-01-22 20:00:19 +010034static const uint8_t announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
35
36static void bla_periodic_work(struct work_struct *work);
37static void bla_send_announce(struct bat_priv *bat_priv,
38 struct backbone_gw *backbone_gw);
39
40/* return the index of the claim */
41static inline uint32_t choose_claim(const void *data, uint32_t size)
42{
43 const unsigned char *key = data;
44 uint32_t hash = 0;
45 size_t i;
46
47 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
48 hash += key[i];
49 hash += (hash << 10);
50 hash ^= (hash >> 6);
51 }
52
53 hash += (hash << 3);
54 hash ^= (hash >> 11);
55 hash += (hash << 15);
56
57 return hash % size;
58}
59
60/* return the index of the backbone gateway */
61static inline uint32_t choose_backbone_gw(const void *data, uint32_t size)
62{
63 const unsigned char *key = data;
64 uint32_t hash = 0;
65 size_t i;
66
67 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
68 hash += key[i];
69 hash += (hash << 10);
70 hash ^= (hash >> 6);
71 }
72
73 hash += (hash << 3);
74 hash ^= (hash >> 11);
75 hash += (hash << 15);
76
77 return hash % size;
78}
79
80
81/* compares address and vid of two backbone gws */
82static int compare_backbone_gw(const struct hlist_node *node, const void *data2)
83{
84 const void *data1 = container_of(node, struct backbone_gw,
85 hash_entry);
86
87 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
88}
89
90/* compares address and vid of two claims */
91static int compare_claim(const struct hlist_node *node, const void *data2)
92{
93 const void *data1 = container_of(node, struct claim,
94 hash_entry);
95
96 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
97}
98
99/* free a backbone gw */
100static void backbone_gw_free_ref(struct backbone_gw *backbone_gw)
101{
102 if (atomic_dec_and_test(&backbone_gw->refcount))
103 kfree_rcu(backbone_gw, rcu);
104}
105
106/* finally deinitialize the claim */
107static void claim_free_rcu(struct rcu_head *rcu)
108{
109 struct claim *claim;
110
111 claim = container_of(rcu, struct claim, rcu);
112
113 backbone_gw_free_ref(claim->backbone_gw);
114 kfree(claim);
115}
116
117/* free a claim, call claim_free_rcu if its the last reference */
118static void claim_free_ref(struct claim *claim)
119{
120 if (atomic_dec_and_test(&claim->refcount))
121 call_rcu(&claim->rcu, claim_free_rcu);
122}
123
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200124/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100125 * @data: search data (may be local/static data)
126 *
127 * looks for a claim in the hash, and returns it if found
128 * or NULL otherwise.
129 */
130static struct claim *claim_hash_find(struct bat_priv *bat_priv,
131 struct claim *data)
132{
133 struct hashtable_t *hash = bat_priv->claim_hash;
134 struct hlist_head *head;
135 struct hlist_node *node;
136 struct claim *claim;
137 struct claim *claim_tmp = NULL;
138 int index;
139
140 if (!hash)
141 return NULL;
142
143 index = choose_claim(data, hash->size);
144 head = &hash->table[index];
145
146 rcu_read_lock();
147 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
148 if (!compare_claim(&claim->hash_entry, data))
149 continue;
150
151 if (!atomic_inc_not_zero(&claim->refcount))
152 continue;
153
154 claim_tmp = claim;
155 break;
156 }
157 rcu_read_unlock();
158
159 return claim_tmp;
160}
161
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200162/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100163 * @addr: the address of the originator
164 * @vid: the VLAN ID
165 *
166 * looks for a claim in the hash, and returns it if found
167 * or NULL otherwise.
168 */
169static struct backbone_gw *backbone_hash_find(struct bat_priv *bat_priv,
170 uint8_t *addr, short vid)
171{
172 struct hashtable_t *hash = bat_priv->backbone_hash;
173 struct hlist_head *head;
174 struct hlist_node *node;
175 struct backbone_gw search_entry, *backbone_gw;
176 struct backbone_gw *backbone_gw_tmp = NULL;
177 int index;
178
179 if (!hash)
180 return NULL;
181
182 memcpy(search_entry.orig, addr, ETH_ALEN);
183 search_entry.vid = vid;
184
185 index = choose_backbone_gw(&search_entry, hash->size);
186 head = &hash->table[index];
187
188 rcu_read_lock();
189 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
190 if (!compare_backbone_gw(&backbone_gw->hash_entry,
191 &search_entry))
192 continue;
193
194 if (!atomic_inc_not_zero(&backbone_gw->refcount))
195 continue;
196
197 backbone_gw_tmp = backbone_gw;
198 break;
199 }
200 rcu_read_unlock();
201
202 return backbone_gw_tmp;
203}
204
205/* delete all claims for a backbone */
206static void bla_del_backbone_claims(struct backbone_gw *backbone_gw)
207{
208 struct hashtable_t *hash;
209 struct hlist_node *node, *node_tmp;
210 struct hlist_head *head;
211 struct claim *claim;
212 int i;
213 spinlock_t *list_lock; /* protects write access to the hash lists */
214
215 hash = backbone_gw->bat_priv->claim_hash;
216 if (!hash)
217 return;
218
219 for (i = 0; i < hash->size; i++) {
220 head = &hash->table[i];
221 list_lock = &hash->list_locks[i];
222
223 spin_lock_bh(list_lock);
224 hlist_for_each_entry_safe(claim, node, node_tmp,
225 head, hash_entry) {
226
227 if (claim->backbone_gw != backbone_gw)
228 continue;
229
230 claim_free_ref(claim);
231 hlist_del_rcu(node);
232 }
233 spin_unlock_bh(list_lock);
234 }
235
236 /* all claims gone, intialize CRC */
237 backbone_gw->crc = BLA_CRC_INIT;
238}
239
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200240/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100241 * @orig: the mac address to be announced within the claim
242 * @vid: the VLAN ID
243 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
244 *
245 * sends a claim frame according to the provided info.
246 */
247static void bla_send_claim(struct bat_priv *bat_priv, uint8_t *mac,
248 short vid, int claimtype)
249{
250 struct sk_buff *skb;
251 struct ethhdr *ethhdr;
252 struct hard_iface *primary_if;
253 struct net_device *soft_iface;
254 uint8_t *hw_src;
255 struct bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100256 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100257
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200258 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100259 if (!primary_if)
260 return;
261
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100262 memcpy(&local_claim_dest, &bat_priv->claim_dest,
263 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100264 local_claim_dest.type = claimtype;
265
266 soft_iface = primary_if->soft_iface;
267
268 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
269 /* IP DST: 0.0.0.0 */
270 zeroip,
271 primary_if->soft_iface,
272 /* IP SRC: 0.0.0.0 */
273 zeroip,
274 /* Ethernet DST: Broadcast */
275 NULL,
276 /* Ethernet SRC/HW SRC: originator mac */
277 primary_if->net_dev->dev_addr,
278 /* HW DST: FF:43:05:XX:00:00
279 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100280 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100281 */
282 (uint8_t *)&local_claim_dest);
283
284 if (!skb)
285 goto out;
286
287 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100288 hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100289
290 /* now we pretend that the client would have sent this ... */
291 switch (claimtype) {
292 case CLAIM_TYPE_ADD:
293 /* normal claim frame
294 * set Ethernet SRC to the clients mac
295 */
296 memcpy(ethhdr->h_source, mac, ETH_ALEN);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200297 batadv_dbg(DBG_BLA, bat_priv,
298 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100299 break;
300 case CLAIM_TYPE_DEL:
301 /* unclaim frame
302 * set HW SRC to the clients mac
303 */
304 memcpy(hw_src, mac, ETH_ALEN);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200305 batadv_dbg(DBG_BLA, bat_priv,
306 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
307 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100308 break;
309 case CLAIM_TYPE_ANNOUNCE:
310 /* announcement frame
311 * set HW SRC to the special mac containg the crc
312 */
313 memcpy(hw_src, mac, ETH_ALEN);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200314 batadv_dbg(DBG_BLA, bat_priv,
315 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
316 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100317 break;
318 case CLAIM_TYPE_REQUEST:
319 /* request frame
320 * set HW SRC to the special mac containg the crc
321 */
322 memcpy(hw_src, mac, ETH_ALEN);
323 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200324 batadv_dbg(DBG_BLA, bat_priv,
325 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
326 ethhdr->h_source, ethhdr->h_dest, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100327 break;
328
329 }
330
331 if (vid != -1)
332 skb = vlan_insert_tag(skb, vid);
333
334 skb_reset_mac_header(skb);
335 skb->protocol = eth_type_trans(skb, soft_iface);
336 bat_priv->stats.rx_packets++;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100337 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
Simon Wunderlich23721382012-01-22 20:00:19 +0100338 soft_iface->last_rx = jiffies;
339
340 netif_rx(skb);
341out:
342 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200343 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100344}
345
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200346/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100347 * @orig: the mac address of the originator
348 * @vid: the VLAN ID
349 *
350 * searches for the backbone gw or creates a new one if it could not
351 * be found.
352 */
353static struct backbone_gw *bla_get_backbone_gw(struct bat_priv *bat_priv,
354 uint8_t *orig, short vid)
355{
356 struct backbone_gw *entry;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100357 struct orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100358 int hash_added;
359
360 entry = backbone_hash_find(bat_priv, orig, vid);
361
362 if (entry)
363 return entry;
364
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200365 batadv_dbg(DBG_BLA, bat_priv,
366 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
367 orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100368
369 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
370 if (!entry)
371 return NULL;
372
373 entry->vid = vid;
374 entry->lasttime = jiffies;
375 entry->crc = BLA_CRC_INIT;
376 entry->bat_priv = bat_priv;
377 atomic_set(&entry->request_sent, 0);
378 memcpy(entry->orig, orig, ETH_ALEN);
379
380 /* one for the hash, one for returning */
381 atomic_set(&entry->refcount, 2);
382
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200383 hash_added = batadv_hash_add(bat_priv->backbone_hash,
384 compare_backbone_gw, choose_backbone_gw,
385 entry, &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100386
387 if (unlikely(hash_added != 0)) {
388 /* hash failed, free the structure */
389 kfree(entry);
390 return NULL;
391 }
392
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100393 /* this is a gateway now, remove any tt entries */
Sven Eckelmannda641192012-05-12 13:48:56 +0200394 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100395 if (orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200396 batadv_tt_global_del_orig(bat_priv, orig_node,
397 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200398 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100399 }
Simon Wunderlich23721382012-01-22 20:00:19 +0100400 return entry;
401}
402
403/* update or add the own backbone gw to make sure we announce
404 * where we receive other backbone gws
405 */
406static void bla_update_own_backbone_gw(struct bat_priv *bat_priv,
407 struct hard_iface *primary_if,
408 short vid)
409{
410 struct backbone_gw *backbone_gw;
411
412 backbone_gw = bla_get_backbone_gw(bat_priv,
413 primary_if->net_dev->dev_addr, vid);
414 if (unlikely(!backbone_gw))
415 return;
416
417 backbone_gw->lasttime = jiffies;
418 backbone_gw_free_ref(backbone_gw);
419}
420
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200421/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100422 * @vid: the vid where the request came on
423 *
424 * Repeat all of our own claims, and finally send an ANNOUNCE frame
425 * to allow the requester another check if the CRC is correct now.
426 */
427static void bla_answer_request(struct bat_priv *bat_priv,
428 struct hard_iface *primary_if, short vid)
429{
430 struct hlist_node *node;
431 struct hlist_head *head;
432 struct hashtable_t *hash;
433 struct claim *claim;
434 struct backbone_gw *backbone_gw;
435 int i;
436
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200437 batadv_dbg(DBG_BLA, bat_priv,
438 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100439
440 backbone_gw = backbone_hash_find(bat_priv,
441 primary_if->net_dev->dev_addr, vid);
442 if (!backbone_gw)
443 return;
444
445 hash = bat_priv->claim_hash;
446 for (i = 0; i < hash->size; i++) {
447 head = &hash->table[i];
448
449 rcu_read_lock();
450 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
451 /* only own claims are interesting */
452 if (claim->backbone_gw != backbone_gw)
453 continue;
454
455 bla_send_claim(bat_priv, claim->addr, claim->vid,
456 CLAIM_TYPE_ADD);
457 }
458 rcu_read_unlock();
459 }
460
461 /* finally, send an announcement frame */
462 bla_send_announce(bat_priv, backbone_gw);
463 backbone_gw_free_ref(backbone_gw);
464}
465
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200466/* @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100467 *
468 * When the crc is wrong, ask the backbone gateway for a full table update.
469 * After the request, it will repeat all of his own claims and finally
470 * send an announcement claim with which we can check again.
471 */
472static void bla_send_request(struct backbone_gw *backbone_gw)
473{
474 /* first, remove all old entries */
475 bla_del_backbone_claims(backbone_gw);
476
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200477 batadv_dbg(DBG_BLA, backbone_gw->bat_priv, "Sending REQUEST to %pM\n",
478 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100479
480 /* send request */
481 bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
482 backbone_gw->vid, CLAIM_TYPE_REQUEST);
483
484 /* no local broadcasts should be sent or received, for now. */
485 if (!atomic_read(&backbone_gw->request_sent)) {
486 atomic_inc(&backbone_gw->bat_priv->bla_num_requests);
487 atomic_set(&backbone_gw->request_sent, 1);
488 }
489}
490
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200491/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100492 * @backbone_gw: our backbone gateway which should be announced
493 *
494 * This function sends an announcement. It is called from multiple
495 * places.
496 */
497static void bla_send_announce(struct bat_priv *bat_priv,
498 struct backbone_gw *backbone_gw)
499{
500 uint8_t mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100501 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100502
503 memcpy(mac, announce_mac, 4);
504 crc = htons(backbone_gw->crc);
Al Viro1a5852d2012-04-22 07:50:29 +0100505 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100506
507 bla_send_claim(bat_priv, mac, backbone_gw->vid, CLAIM_TYPE_ANNOUNCE);
508
509}
510
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200511/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100512 * @mac: the mac address of the claim
513 * @vid: the VLAN ID of the frame
514 * @backbone_gw: the backbone gateway which claims it
515 *
516 * Adds a claim in the claim hash.
517 */
518static void bla_add_claim(struct bat_priv *bat_priv, const uint8_t *mac,
519 const short vid, struct backbone_gw *backbone_gw)
520{
521 struct claim *claim;
522 struct claim search_claim;
523 int hash_added;
524
525 memcpy(search_claim.addr, mac, ETH_ALEN);
526 search_claim.vid = vid;
527 claim = claim_hash_find(bat_priv, &search_claim);
528
529 /* create a new claim entry if it does not exist yet. */
530 if (!claim) {
531 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
532 if (!claim)
533 return;
534
535 memcpy(claim->addr, mac, ETH_ALEN);
536 claim->vid = vid;
537 claim->lasttime = jiffies;
538 claim->backbone_gw = backbone_gw;
539
540 atomic_set(&claim->refcount, 2);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200541 batadv_dbg(DBG_BLA, bat_priv,
542 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
543 mac, vid);
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200544 hash_added = batadv_hash_add(bat_priv->claim_hash,
545 compare_claim, choose_claim,
546 claim, &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100547
548 if (unlikely(hash_added != 0)) {
549 /* only local changes happened. */
550 kfree(claim);
551 return;
552 }
553 } else {
554 claim->lasttime = jiffies;
555 if (claim->backbone_gw == backbone_gw)
556 /* no need to register a new backbone */
557 goto claim_free_ref;
558
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200559 batadv_dbg(DBG_BLA, bat_priv,
560 "bla_add_claim(): changing ownership for %pM, vid %d\n",
561 mac, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100562
563 claim->backbone_gw->crc ^=
564 crc16(0, claim->addr, ETH_ALEN);
565 backbone_gw_free_ref(claim->backbone_gw);
566
567 }
568 /* set (new) backbone gw */
569 atomic_inc(&backbone_gw->refcount);
570 claim->backbone_gw = backbone_gw;
571
572 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
573 backbone_gw->lasttime = jiffies;
574
575claim_free_ref:
576 claim_free_ref(claim);
577}
578
579/* Delete a claim from the claim hash which has the
580 * given mac address and vid.
581 */
582static void bla_del_claim(struct bat_priv *bat_priv, const uint8_t *mac,
583 const short vid)
584{
585 struct claim search_claim, *claim;
586
587 memcpy(search_claim.addr, mac, ETH_ALEN);
588 search_claim.vid = vid;
589 claim = claim_hash_find(bat_priv, &search_claim);
590 if (!claim)
591 return;
592
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200593 batadv_dbg(DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n", mac,
594 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100595
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200596 batadv_hash_remove(bat_priv->claim_hash, compare_claim, choose_claim,
597 claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100598 claim_free_ref(claim); /* reference from the hash is gone */
599
600 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
601
602 /* don't need the reference from hash_find() anymore */
603 claim_free_ref(claim);
604}
605
606/* check for ANNOUNCE frame, return 1 if handled */
607static int handle_announce(struct bat_priv *bat_priv,
608 uint8_t *an_addr, uint8_t *backbone_addr, short vid)
609{
610 struct backbone_gw *backbone_gw;
611 uint16_t crc;
612
613 if (memcmp(an_addr, announce_mac, 4) != 0)
614 return 0;
615
616 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
617
618 if (unlikely(!backbone_gw))
619 return 1;
620
621
622 /* handle as ANNOUNCE frame */
623 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100624 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100625
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200626 batadv_dbg(DBG_BLA, bat_priv,
627 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
628 vid, backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100629
630 if (backbone_gw->crc != crc) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200631 batadv_dbg(DBG_BLA, backbone_gw->bat_priv,
632 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
633 backbone_gw->orig, backbone_gw->vid,
634 backbone_gw->crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100635
636 bla_send_request(backbone_gw);
637 } else {
638 /* if we have sent a request and the crc was OK,
639 * we can allow traffic again.
640 */
641 if (atomic_read(&backbone_gw->request_sent)) {
642 atomic_dec(&backbone_gw->bat_priv->bla_num_requests);
643 atomic_set(&backbone_gw->request_sent, 0);
644 }
645 }
646
647 backbone_gw_free_ref(backbone_gw);
648 return 1;
649}
650
651/* check for REQUEST frame, return 1 if handled */
652static int handle_request(struct bat_priv *bat_priv,
653 struct hard_iface *primary_if,
654 uint8_t *backbone_addr,
655 struct ethhdr *ethhdr, short vid)
656{
657 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200658 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100659 return 0;
660
661 /* sanity check, this should not happen on a normal switch,
662 * we ignore it in this case.
663 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200664 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100665 return 1;
666
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200667 batadv_dbg(DBG_BLA, bat_priv,
668 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
669 vid, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100670
671 bla_answer_request(bat_priv, primary_if, vid);
672 return 1;
673}
674
675/* check for UNCLAIM frame, return 1 if handled */
676static int handle_unclaim(struct bat_priv *bat_priv,
677 struct hard_iface *primary_if,
678 uint8_t *backbone_addr,
679 uint8_t *claim_addr, short vid)
680{
681 struct backbone_gw *backbone_gw;
682
683 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200684 if (primary_if && batadv_compare_eth(backbone_addr,
685 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100686 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_DEL);
687
688 backbone_gw = backbone_hash_find(bat_priv, backbone_addr, vid);
689
690 if (!backbone_gw)
691 return 1;
692
693 /* this must be an UNCLAIM frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200694 batadv_dbg(DBG_BLA, bat_priv,
695 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
696 claim_addr, vid, backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100697
698 bla_del_claim(bat_priv, claim_addr, vid);
699 backbone_gw_free_ref(backbone_gw);
700 return 1;
701}
702
703/* check for CLAIM frame, return 1 if handled */
704static int handle_claim(struct bat_priv *bat_priv,
705 struct hard_iface *primary_if, uint8_t *backbone_addr,
706 uint8_t *claim_addr, short vid)
707{
708 struct backbone_gw *backbone_gw;
709
710 /* register the gateway if not yet available, and add the claim. */
711
712 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
713
714 if (unlikely(!backbone_gw))
715 return 1;
716
717 /* this must be a CLAIM frame */
718 bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200719 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100720 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_ADD);
721
722 /* TODO: we could call something like tt_local_del() here. */
723
724 backbone_gw_free_ref(backbone_gw);
725 return 1;
726}
727
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200728/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100729 * @hw_src: the Hardware source in the ARP Header
730 * @hw_dst: the Hardware destination in the ARP Header
731 * @ethhdr: pointer to the Ethernet header of the claim frame
732 *
733 * checks if it is a claim packet and if its on the same group.
734 * This function also applies the group ID of the sender
735 * if it is in the same mesh.
736 *
737 * returns:
738 * 2 - if it is a claim packet and on the same group
739 * 1 - if is a claim packet from another group
740 * 0 - if it is not a claim packet
741 */
742static int check_claim_group(struct bat_priv *bat_priv,
743 struct hard_iface *primary_if,
744 uint8_t *hw_src, uint8_t *hw_dst,
745 struct ethhdr *ethhdr)
746{
747 uint8_t *backbone_addr;
748 struct orig_node *orig_node;
749 struct bla_claim_dst *bla_dst, *bla_dst_own;
750
751 bla_dst = (struct bla_claim_dst *)hw_dst;
752 bla_dst_own = &bat_priv->claim_dest;
753
754 /* check if it is a claim packet in general */
755 if (memcmp(bla_dst->magic, bla_dst_own->magic,
756 sizeof(bla_dst->magic)) != 0)
757 return 0;
758
759 /* if announcement packet, use the source,
760 * otherwise assume it is in the hw_src
761 */
762 switch (bla_dst->type) {
763 case CLAIM_TYPE_ADD:
764 backbone_addr = hw_src;
765 break;
766 case CLAIM_TYPE_REQUEST:
767 case CLAIM_TYPE_ANNOUNCE:
768 case CLAIM_TYPE_DEL:
769 backbone_addr = ethhdr->h_source;
770 break;
771 default:
772 return 0;
773 }
774
775 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200776 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100777 return 0;
778
779 /* if its already the same group, it is fine. */
780 if (bla_dst->group == bla_dst_own->group)
781 return 2;
782
783 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200784 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100785
786 /* dont accept claims from gateways which are not in
787 * the same mesh or group.
788 */
789 if (!orig_node)
790 return 1;
791
792 /* if our mesh friends mac is bigger, use it for ourselves. */
793 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200794 batadv_dbg(DBG_BLA, bat_priv,
795 "taking other backbones claim group: %04x\n",
796 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100797 bla_dst_own->group = bla_dst->group;
798 }
799
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200800 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100801
802 return 2;
803}
804
805
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200806/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100807 * @skb: the frame to be checked
808 *
809 * Check if this is a claim frame, and process it accordingly.
810 *
811 * returns 1 if it was a claim frame, otherwise return 0 to
812 * tell the callee that it can use the frame on its own.
813 */
814static int bla_process_claim(struct bat_priv *bat_priv,
815 struct hard_iface *primary_if,
816 struct sk_buff *skb)
817{
818 struct ethhdr *ethhdr;
819 struct vlan_ethhdr *vhdr;
820 struct arphdr *arphdr;
821 uint8_t *hw_src, *hw_dst;
822 struct bla_claim_dst *bla_dst;
823 uint16_t proto;
824 int headlen;
825 short vid = -1;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100826 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100827
828 ethhdr = (struct ethhdr *)skb_mac_header(skb);
829
830 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
831 vhdr = (struct vlan_ethhdr *)ethhdr;
832 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
833 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
834 headlen = sizeof(*vhdr);
835 } else {
836 proto = ntohs(ethhdr->h_proto);
Antonio Quartulli0d125072012-02-18 11:27:34 +0100837 headlen = ETH_HLEN;
Simon Wunderlich23721382012-01-22 20:00:19 +0100838 }
839
840 if (proto != ETH_P_ARP)
841 return 0; /* not a claim frame */
842
843 /* this must be a ARP frame. check if it is a claim. */
844
845 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
846 return 0;
847
848 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
849 ethhdr = (struct ethhdr *)skb_mac_header(skb);
850 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
851
852 /* Check whether the ARP frame carries a valid
853 * IP information
854 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100855 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
856 return 0;
857 if (arphdr->ar_pro != htons(ETH_P_IP))
858 return 0;
859 if (arphdr->ar_hln != ETH_ALEN)
860 return 0;
861 if (arphdr->ar_pln != 4)
862 return 0;
863
864 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
865 hw_dst = hw_src + ETH_ALEN + 4;
866 bla_dst = (struct bla_claim_dst *)hw_dst;
867
868 /* check if it is a claim frame. */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100869 ret = check_claim_group(bat_priv, primary_if, hw_src, hw_dst, ethhdr);
870 if (ret == 1)
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200871 batadv_dbg(DBG_BLA, bat_priv,
872 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
873 ethhdr->h_source, vid, hw_src, hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100874
875 if (ret < 2)
876 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100877
878 /* become a backbone gw ourselves on this vlan if not happened yet */
879 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
880
881 /* check for the different types of claim frames ... */
882 switch (bla_dst->type) {
883 case CLAIM_TYPE_ADD:
884 if (handle_claim(bat_priv, primary_if, hw_src,
885 ethhdr->h_source, vid))
886 return 1;
887 break;
888 case CLAIM_TYPE_DEL:
889 if (handle_unclaim(bat_priv, primary_if,
890 ethhdr->h_source, hw_src, vid))
891 return 1;
892 break;
893
894 case CLAIM_TYPE_ANNOUNCE:
895 if (handle_announce(bat_priv, hw_src, ethhdr->h_source, vid))
896 return 1;
897 break;
898 case CLAIM_TYPE_REQUEST:
899 if (handle_request(bat_priv, primary_if, hw_src, ethhdr, vid))
900 return 1;
901 break;
902 }
903
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200904 batadv_dbg(DBG_BLA, bat_priv,
905 "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",
906 ethhdr->h_source, vid, hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +0100907 return 1;
908}
909
910/* Check when we last heard from other nodes, and remove them in case of
911 * a time out, or clean all backbone gws if now is set.
912 */
913static void bla_purge_backbone_gw(struct bat_priv *bat_priv, int now)
914{
915 struct backbone_gw *backbone_gw;
916 struct hlist_node *node, *node_tmp;
917 struct hlist_head *head;
918 struct hashtable_t *hash;
919 spinlock_t *list_lock; /* protects write access to the hash lists */
920 int i;
921
922 hash = bat_priv->backbone_hash;
923 if (!hash)
924 return;
925
926 for (i = 0; i < hash->size; i++) {
927 head = &hash->table[i];
928 list_lock = &hash->list_locks[i];
929
930 spin_lock_bh(list_lock);
931 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
932 head, hash_entry) {
933 if (now)
934 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200935 if (!batadv_has_timed_out(backbone_gw->lasttime,
936 BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +0100937 continue;
938
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200939 batadv_dbg(DBG_BLA, backbone_gw->bat_priv,
940 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
941 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100942
943purge_now:
944 /* don't wait for the pending request anymore */
945 if (atomic_read(&backbone_gw->request_sent))
946 atomic_dec(&bat_priv->bla_num_requests);
947
948 bla_del_backbone_claims(backbone_gw);
949
950 hlist_del_rcu(node);
951 backbone_gw_free_ref(backbone_gw);
952 }
953 spin_unlock_bh(list_lock);
954 }
955}
956
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200957/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100958 * @primary_if: the selected primary interface, may be NULL if now is set
959 * @now: whether the whole hash shall be wiped now
960 *
961 * Check when we heard last time from our own claims, and remove them in case of
962 * a time out, or clean all claims if now is set
963 */
964static void bla_purge_claims(struct bat_priv *bat_priv,
965 struct hard_iface *primary_if, int now)
966{
967 struct claim *claim;
968 struct hlist_node *node;
969 struct hlist_head *head;
970 struct hashtable_t *hash;
971 int i;
972
973 hash = bat_priv->claim_hash;
974 if (!hash)
975 return;
976
977 for (i = 0; i < hash->size; i++) {
978 head = &hash->table[i];
979
980 rcu_read_lock();
981 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
982 if (now)
983 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200984 if (!batadv_compare_eth(claim->backbone_gw->orig,
985 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100986 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200987 if (!batadv_has_timed_out(claim->lasttime,
988 BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +0100989 continue;
990
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200991 batadv_dbg(DBG_BLA, bat_priv,
992 "bla_purge_claims(): %pM, vid %d, time out\n",
993 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100994
995purge_now:
996 handle_unclaim(bat_priv, primary_if,
997 claim->backbone_gw->orig,
998 claim->addr, claim->vid);
999 }
1000 rcu_read_unlock();
1001 }
1002}
1003
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001004/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001005 * @primary_if: the new selected primary_if
1006 * @oldif: the old primary interface, may be NULL
1007 *
1008 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001009 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001010void batadv_bla_update_orig_address(struct bat_priv *bat_priv,
1011 struct hard_iface *primary_if,
1012 struct hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001013{
1014 struct backbone_gw *backbone_gw;
1015 struct hlist_node *node;
1016 struct hlist_head *head;
1017 struct hashtable_t *hash;
1018 int i;
1019
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001020 /* reset bridge loop avoidance group id */
1021 bat_priv->claim_dest.group =
1022 htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1023
Simon Wunderlich23721382012-01-22 20:00:19 +01001024 if (!oldif) {
1025 bla_purge_claims(bat_priv, NULL, 1);
1026 bla_purge_backbone_gw(bat_priv, 1);
1027 return;
1028 }
1029
1030 hash = bat_priv->backbone_hash;
1031 if (!hash)
1032 return;
1033
1034 for (i = 0; i < hash->size; i++) {
1035 head = &hash->table[i];
1036
1037 rcu_read_lock();
1038 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1039 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001040 if (!batadv_compare_eth(backbone_gw->orig,
1041 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001042 continue;
1043
1044 memcpy(backbone_gw->orig,
1045 primary_if->net_dev->dev_addr, ETH_ALEN);
1046 /* send an announce frame so others will ask for our
1047 * claims and update their tables.
1048 */
1049 bla_send_announce(bat_priv, backbone_gw);
1050 }
1051 rcu_read_unlock();
1052 }
1053}
1054
1055
1056
1057/* (re)start the timer */
1058static void bla_start_timer(struct bat_priv *bat_priv)
1059{
1060 INIT_DELAYED_WORK(&bat_priv->bla_work, bla_periodic_work);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001061 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla_work,
Simon Wunderlich23721382012-01-22 20:00:19 +01001062 msecs_to_jiffies(BLA_PERIOD_LENGTH));
1063}
1064
1065/* periodic work to do:
1066 * * purge structures when they are too old
1067 * * send announcements
1068 */
1069static void bla_periodic_work(struct work_struct *work)
1070{
1071 struct delayed_work *delayed_work =
1072 container_of(work, struct delayed_work, work);
1073 struct bat_priv *bat_priv =
1074 container_of(delayed_work, struct bat_priv, bla_work);
1075 struct hlist_node *node;
1076 struct hlist_head *head;
1077 struct backbone_gw *backbone_gw;
1078 struct hashtable_t *hash;
1079 struct hard_iface *primary_if;
1080 int i;
1081
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001082 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001083 if (!primary_if)
1084 goto out;
1085
1086 bla_purge_claims(bat_priv, primary_if, 0);
1087 bla_purge_backbone_gw(bat_priv, 0);
1088
1089 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1090 goto out;
1091
1092 hash = bat_priv->backbone_hash;
1093 if (!hash)
1094 goto out;
1095
1096 for (i = 0; i < hash->size; i++) {
1097 head = &hash->table[i];
1098
1099 rcu_read_lock();
1100 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001101 if (!batadv_compare_eth(backbone_gw->orig,
1102 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001103 continue;
1104
1105 backbone_gw->lasttime = jiffies;
1106
1107 bla_send_announce(bat_priv, backbone_gw);
1108 }
1109 rcu_read_unlock();
1110 }
1111out:
1112 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001113 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001114
1115 bla_start_timer(bat_priv);
1116}
1117
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001118/* The hash for claim and backbone hash receive the same key because they
1119 * are getting initialized by hash_new with the same key. Reinitializing
1120 * them with to different keys to allow nested locking without generating
1121 * lockdep warnings
1122 */
1123static struct lock_class_key claim_hash_lock_class_key;
1124static struct lock_class_key backbone_hash_lock_class_key;
1125
Simon Wunderlich23721382012-01-22 20:00:19 +01001126/* initialize all bla structures */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001127int batadv_bla_init(struct bat_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001128{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001129 int i;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001130 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1131 struct hard_iface *primary_if;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001132
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001133 batadv_dbg(DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001134
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001135 /* setting claim destination address */
1136 memcpy(&bat_priv->claim_dest.magic, claim_dest, 3);
1137 bat_priv->claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001138 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001139 if (primary_if) {
1140 bat_priv->claim_dest.group =
1141 htons(crc16(0, primary_if->net_dev->dev_addr,
1142 ETH_ALEN));
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001143 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001144 } else {
1145 bat_priv->claim_dest.group = 0; /* will be set later */
1146 }
1147
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001148 /* initialize the duplicate list */
1149 for (i = 0; i < DUPLIST_SIZE; i++)
1150 bat_priv->bcast_duplist[i].entrytime =
1151 jiffies - msecs_to_jiffies(DUPLIST_TIMEOUT);
1152 bat_priv->bcast_duplist_curr = 0;
1153
Simon Wunderlich23721382012-01-22 20:00:19 +01001154 if (bat_priv->claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001155 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001156
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001157 bat_priv->claim_hash = batadv_hash_new(128);
1158 bat_priv->backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001159
1160 if (!bat_priv->claim_hash || !bat_priv->backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001161 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001162
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001163 batadv_hash_set_lock_class(bat_priv->claim_hash,
1164 &claim_hash_lock_class_key);
1165 batadv_hash_set_lock_class(bat_priv->backbone_hash,
1166 &backbone_hash_lock_class_key);
1167
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001168 batadv_dbg(DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001169
1170 bla_start_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001171 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001172}
1173
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001174/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001175 * @bcast_packet: originator mac address
1176 * @hdr_size: maximum length of the frame
1177 *
1178 * check if it is on our broadcast list. Another gateway might
1179 * have sent the same packet because it is connected to the same backbone,
1180 * so we have to remove this duplicate.
1181 *
1182 * This is performed by checking the CRC, which will tell us
1183 * with a good chance that it is the same packet. If it is furthermore
1184 * sent by another host, drop it. We allow equal packets from
1185 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001186 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001187int batadv_bla_check_bcast_duplist(struct bat_priv *bat_priv,
1188 struct bcast_packet *bcast_packet,
1189 int hdr_size)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001190{
1191 int i, length, curr;
1192 uint8_t *content;
1193 uint16_t crc;
1194 struct bcast_duplist_entry *entry;
1195
1196 length = hdr_size - sizeof(*bcast_packet);
1197 content = (uint8_t *)bcast_packet;
1198 content += sizeof(*bcast_packet);
1199
1200 /* calculate the crc ... */
1201 crc = crc16(0, content, length);
1202
1203 for (i = 0 ; i < DUPLIST_SIZE; i++) {
1204 curr = (bat_priv->bcast_duplist_curr + i) % DUPLIST_SIZE;
1205 entry = &bat_priv->bcast_duplist[curr];
1206
1207 /* we can stop searching if the entry is too old ;
1208 * later entries will be even older
1209 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001210 if (batadv_has_timed_out(entry->entrytime, DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001211 break;
1212
1213 if (entry->crc != crc)
1214 continue;
1215
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001216 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001217 continue;
1218
1219 /* this entry seems to match: same crc, not too old,
1220 * and from another gw. therefore return 1 to forbid it.
1221 */
1222 return 1;
1223 }
1224 /* not found, add a new entry (overwrite the oldest entry) */
1225 curr = (bat_priv->bcast_duplist_curr + DUPLIST_SIZE - 1) % DUPLIST_SIZE;
1226 entry = &bat_priv->bcast_duplist[curr];
1227 entry->crc = crc;
1228 entry->entrytime = jiffies;
1229 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
1230 bat_priv->bcast_duplist_curr = curr;
1231
1232 /* allow it, its the first occurence. */
1233 return 0;
1234}
1235
1236
1237
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001238/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001239 * @orig: originator mac address
1240 *
1241 * check if the originator is a gateway for any VLAN ID.
1242 *
1243 * returns 1 if it is found, 0 otherwise
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001244 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001245int batadv_bla_is_backbone_gw_orig(struct bat_priv *bat_priv, uint8_t *orig)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001246{
1247 struct hashtable_t *hash = bat_priv->backbone_hash;
1248 struct hlist_head *head;
1249 struct hlist_node *node;
1250 struct backbone_gw *backbone_gw;
1251 int i;
1252
1253 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1254 return 0;
1255
1256 if (!hash)
1257 return 0;
1258
1259 for (i = 0; i < hash->size; i++) {
1260 head = &hash->table[i];
1261
1262 rcu_read_lock();
1263 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001264 if (batadv_compare_eth(backbone_gw->orig, orig)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001265 rcu_read_unlock();
1266 return 1;
1267 }
1268 }
1269 rcu_read_unlock();
1270 }
1271
1272 return 0;
1273}
1274
1275
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001276/* @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001277 * @orig_node: the orig_node of the frame
1278 * @hdr_size: maximum length of the frame
1279 *
1280 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1281 * if the orig_node is also a gateway on the soft interface, otherwise it
1282 * returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001283 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001284int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1285 struct orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001286{
1287 struct ethhdr *ethhdr;
1288 struct vlan_ethhdr *vhdr;
1289 struct backbone_gw *backbone_gw;
1290 short vid = -1;
1291
1292 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1293 return 0;
1294
1295 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001296 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001297 return 0;
1298
1299 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1300
1301 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1302 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1303 return 0;
1304
1305 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1306 hdr_size);
1307 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1308 }
1309
1310 /* see if this originator is a backbone gw for this VLAN */
Simon Wunderlich23721382012-01-22 20:00:19 +01001311 backbone_gw = backbone_hash_find(orig_node->bat_priv,
1312 orig_node->orig, vid);
1313 if (!backbone_gw)
1314 return 0;
1315
1316 backbone_gw_free_ref(backbone_gw);
1317 return 1;
1318}
1319
1320/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001321void batadv_bla_free(struct bat_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001322{
1323 struct hard_iface *primary_if;
1324
1325 cancel_delayed_work_sync(&bat_priv->bla_work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001326 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001327
1328 if (bat_priv->claim_hash) {
1329 bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001330 batadv_hash_destroy(bat_priv->claim_hash);
Simon Wunderlich23721382012-01-22 20:00:19 +01001331 bat_priv->claim_hash = NULL;
1332 }
1333 if (bat_priv->backbone_hash) {
1334 bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001335 batadv_hash_destroy(bat_priv->backbone_hash);
Simon Wunderlich23721382012-01-22 20:00:19 +01001336 bat_priv->backbone_hash = NULL;
1337 }
1338 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001339 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001340}
1341
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001342/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001343 * @skb: the frame to be checked
1344 * @vid: the VLAN ID of the frame
1345 *
1346 * bla_rx avoidance checks if:
1347 * * we have to race for a claim
1348 * * if the frame is allowed on the LAN
1349 *
1350 * in these cases, the skb is further handled by this function and
1351 * returns 1, otherwise it returns 0 and the caller shall further
1352 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001353 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001354int batadv_bla_rx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001355{
1356 struct ethhdr *ethhdr;
1357 struct claim search_claim, *claim = NULL;
1358 struct hard_iface *primary_if;
1359 int ret;
1360
1361 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1362
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001363 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001364 if (!primary_if)
1365 goto handled;
1366
1367 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1368 goto allow;
1369
1370
1371 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1372 /* don't allow broadcasts while requests are in flight */
1373 if (is_multicast_ether_addr(ethhdr->h_dest))
1374 goto handled;
1375
1376 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1377 search_claim.vid = vid;
1378 claim = claim_hash_find(bat_priv, &search_claim);
1379
1380 if (!claim) {
1381 /* possible optimization: race for a claim */
1382 /* No claim exists yet, claim it for us!
1383 */
1384 handle_claim(bat_priv, primary_if,
1385 primary_if->net_dev->dev_addr,
1386 ethhdr->h_source, vid);
1387 goto allow;
1388 }
1389
1390 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001391 if (batadv_compare_eth(claim->backbone_gw->orig,
1392 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001393 /* ... allow it in any case */
1394 claim->lasttime = jiffies;
1395 goto allow;
1396 }
1397
1398 /* if it is a broadcast ... */
1399 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1400 /* ... drop it. the responsible gateway is in charge. */
1401 goto handled;
1402 } else {
1403 /* seems the client considers us as its best gateway.
1404 * send a claim and update the claim table
1405 * immediately.
1406 */
1407 handle_claim(bat_priv, primary_if,
1408 primary_if->net_dev->dev_addr,
1409 ethhdr->h_source, vid);
1410 goto allow;
1411 }
1412allow:
1413 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1414 ret = 0;
1415 goto out;
1416
1417handled:
1418 kfree_skb(skb);
1419 ret = 1;
1420
1421out:
1422 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001423 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001424 if (claim)
1425 claim_free_ref(claim);
1426 return ret;
1427}
1428
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001429/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001430 * @skb: the frame to be checked
1431 * @vid: the VLAN ID of the frame
1432 *
1433 * bla_tx checks if:
1434 * * a claim was received which has to be processed
1435 * * the frame is allowed on the mesh
1436 *
1437 * in these cases, the skb is further handled by this function and
1438 * returns 1, otherwise it returns 0 and the caller shall further
1439 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001440 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001441int batadv_bla_tx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001442{
1443 struct ethhdr *ethhdr;
1444 struct claim search_claim, *claim = NULL;
1445 struct hard_iface *primary_if;
1446 int ret = 0;
1447
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001448 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001449 if (!primary_if)
1450 goto out;
1451
1452 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1453 goto allow;
1454
1455 /* in VLAN case, the mac header might not be set. */
1456 skb_reset_mac_header(skb);
1457
1458 if (bla_process_claim(bat_priv, primary_if, skb))
1459 goto handled;
1460
1461 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1462
1463 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1464 /* don't allow broadcasts while requests are in flight */
1465 if (is_multicast_ether_addr(ethhdr->h_dest))
1466 goto handled;
1467
1468 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1469 search_claim.vid = vid;
1470
1471 claim = claim_hash_find(bat_priv, &search_claim);
1472
1473 /* if no claim exists, allow it. */
1474 if (!claim)
1475 goto allow;
1476
1477 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001478 if (batadv_compare_eth(claim->backbone_gw->orig,
1479 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001480 /* if yes, the client has roamed and we have
1481 * to unclaim it.
1482 */
1483 handle_unclaim(bat_priv, primary_if,
1484 primary_if->net_dev->dev_addr,
1485 ethhdr->h_source, vid);
1486 goto allow;
1487 }
1488
1489 /* check if it is a multicast/broadcast frame */
1490 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1491 /* drop it. the responsible gateway has forwarded it into
1492 * the backbone network.
1493 */
1494 goto handled;
1495 } else {
1496 /* we must allow it. at least if we are
1497 * responsible for the DESTINATION.
1498 */
1499 goto allow;
1500 }
1501allow:
1502 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1503 ret = 0;
1504 goto out;
1505handled:
1506 ret = 1;
1507out:
1508 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001509 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001510 if (claim)
1511 claim_free_ref(claim);
1512 return ret;
1513}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001514
Sven Eckelmann08adf152012-05-12 13:38:47 +02001515int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001516{
1517 struct net_device *net_dev = (struct net_device *)seq->private;
1518 struct bat_priv *bat_priv = netdev_priv(net_dev);
1519 struct hashtable_t *hash = bat_priv->claim_hash;
1520 struct claim *claim;
1521 struct hard_iface *primary_if;
1522 struct hlist_node *node;
1523 struct hlist_head *head;
1524 uint32_t i;
1525 bool is_own;
1526 int ret = 0;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001527 uint8_t *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001528
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001529 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001530 if (!primary_if) {
1531 ret = seq_printf(seq,
1532 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1533 net_dev->name);
1534 goto out;
1535 }
1536
1537 if (primary_if->if_status != IF_ACTIVE) {
1538 ret = seq_printf(seq,
1539 "BATMAN mesh %s disabled - primary interface not active\n",
1540 net_dev->name);
1541 goto out;
1542 }
1543
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001544 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001545 seq_printf(seq,
1546 "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001547 net_dev->name, primary_addr,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001548 ntohs(bat_priv->claim_dest.group));
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001549 seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
1550 "Client", "VID", "Originator", "CRC");
1551 for (i = 0; i < hash->size; i++) {
1552 head = &hash->table[i];
1553
1554 rcu_read_lock();
1555 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001556 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1557 primary_addr);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001558 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
1559 claim->addr, claim->vid,
1560 claim->backbone_gw->orig,
1561 (is_own ? 'x' : ' '),
1562 claim->backbone_gw->crc);
1563 }
1564 rcu_read_unlock();
1565 }
1566out:
1567 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001568 batadv_hardif_free_ref(primary_if);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001569 return ret;
1570}