blob: 52c0d637d581cd791fd4c47cea3d39dd16f21211 [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
258 primary_if = primary_if_get_selected(bat_priv);
259 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);
297 bat_dbg(DBG_BLA, bat_priv,
298 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
299 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);
305 bat_dbg(DBG_BLA, bat_priv,
306 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac, vid);
307 break;
308 case CLAIM_TYPE_ANNOUNCE:
309 /* announcement frame
310 * set HW SRC to the special mac containg the crc
311 */
312 memcpy(hw_src, mac, ETH_ALEN);
313 bat_dbg(DBG_BLA, bat_priv,
314 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
315 ethhdr->h_source, vid);
316 break;
317 case CLAIM_TYPE_REQUEST:
318 /* request frame
319 * set HW SRC to the special mac containg the crc
320 */
321 memcpy(hw_src, mac, ETH_ALEN);
322 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
323 bat_dbg(DBG_BLA, bat_priv,
324 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
325 ethhdr->h_source, ethhdr->h_dest, vid);
326 break;
327
328 }
329
330 if (vid != -1)
331 skb = vlan_insert_tag(skb, vid);
332
333 skb_reset_mac_header(skb);
334 skb->protocol = eth_type_trans(skb, soft_iface);
335 bat_priv->stats.rx_packets++;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100336 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
Simon Wunderlich23721382012-01-22 20:00:19 +0100337 soft_iface->last_rx = jiffies;
338
339 netif_rx(skb);
340out:
341 if (primary_if)
342 hardif_free_ref(primary_if);
343}
344
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200345/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100346 * @orig: the mac address of the originator
347 * @vid: the VLAN ID
348 *
349 * searches for the backbone gw or creates a new one if it could not
350 * be found.
351 */
352static struct backbone_gw *bla_get_backbone_gw(struct bat_priv *bat_priv,
353 uint8_t *orig, short vid)
354{
355 struct backbone_gw *entry;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100356 struct orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100357 int hash_added;
358
359 entry = backbone_hash_find(bat_priv, orig, vid);
360
361 if (entry)
362 return entry;
363
364 bat_dbg(DBG_BLA, bat_priv,
365 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
366 orig, vid);
367
368 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
369 if (!entry)
370 return NULL;
371
372 entry->vid = vid;
373 entry->lasttime = jiffies;
374 entry->crc = BLA_CRC_INIT;
375 entry->bat_priv = bat_priv;
376 atomic_set(&entry->request_sent, 0);
377 memcpy(entry->orig, orig, ETH_ALEN);
378
379 /* one for the hash, one for returning */
380 atomic_set(&entry->refcount, 2);
381
382 hash_added = hash_add(bat_priv->backbone_hash, compare_backbone_gw,
383 choose_backbone_gw, entry, &entry->hash_entry);
384
385 if (unlikely(hash_added != 0)) {
386 /* hash failed, free the structure */
387 kfree(entry);
388 return NULL;
389 }
390
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100391 /* this is a gateway now, remove any tt entries */
392 orig_node = orig_hash_find(bat_priv, orig);
393 if (orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200394 batadv_tt_global_del_orig(bat_priv, orig_node,
395 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200396 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100397 }
Simon Wunderlich23721382012-01-22 20:00:19 +0100398 return entry;
399}
400
401/* update or add the own backbone gw to make sure we announce
402 * where we receive other backbone gws
403 */
404static void bla_update_own_backbone_gw(struct bat_priv *bat_priv,
405 struct hard_iface *primary_if,
406 short vid)
407{
408 struct backbone_gw *backbone_gw;
409
410 backbone_gw = bla_get_backbone_gw(bat_priv,
411 primary_if->net_dev->dev_addr, vid);
412 if (unlikely(!backbone_gw))
413 return;
414
415 backbone_gw->lasttime = jiffies;
416 backbone_gw_free_ref(backbone_gw);
417}
418
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200419/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100420 * @vid: the vid where the request came on
421 *
422 * Repeat all of our own claims, and finally send an ANNOUNCE frame
423 * to allow the requester another check if the CRC is correct now.
424 */
425static void bla_answer_request(struct bat_priv *bat_priv,
426 struct hard_iface *primary_if, short vid)
427{
428 struct hlist_node *node;
429 struct hlist_head *head;
430 struct hashtable_t *hash;
431 struct claim *claim;
432 struct backbone_gw *backbone_gw;
433 int i;
434
435 bat_dbg(DBG_BLA, bat_priv,
436 "bla_answer_request(): received a claim request, send all of our own claims again\n");
437
438 backbone_gw = backbone_hash_find(bat_priv,
439 primary_if->net_dev->dev_addr, vid);
440 if (!backbone_gw)
441 return;
442
443 hash = bat_priv->claim_hash;
444 for (i = 0; i < hash->size; i++) {
445 head = &hash->table[i];
446
447 rcu_read_lock();
448 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
449 /* only own claims are interesting */
450 if (claim->backbone_gw != backbone_gw)
451 continue;
452
453 bla_send_claim(bat_priv, claim->addr, claim->vid,
454 CLAIM_TYPE_ADD);
455 }
456 rcu_read_unlock();
457 }
458
459 /* finally, send an announcement frame */
460 bla_send_announce(bat_priv, backbone_gw);
461 backbone_gw_free_ref(backbone_gw);
462}
463
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200464/* @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100465 *
466 * When the crc is wrong, ask the backbone gateway for a full table update.
467 * After the request, it will repeat all of his own claims and finally
468 * send an announcement claim with which we can check again.
469 */
470static void bla_send_request(struct backbone_gw *backbone_gw)
471{
472 /* first, remove all old entries */
473 bla_del_backbone_claims(backbone_gw);
474
475 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
476 "Sending REQUEST to %pM\n",
477 backbone_gw->orig);
478
479 /* send request */
480 bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
481 backbone_gw->vid, CLAIM_TYPE_REQUEST);
482
483 /* no local broadcasts should be sent or received, for now. */
484 if (!atomic_read(&backbone_gw->request_sent)) {
485 atomic_inc(&backbone_gw->bat_priv->bla_num_requests);
486 atomic_set(&backbone_gw->request_sent, 1);
487 }
488}
489
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200490/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100491 * @backbone_gw: our backbone gateway which should be announced
492 *
493 * This function sends an announcement. It is called from multiple
494 * places.
495 */
496static void bla_send_announce(struct bat_priv *bat_priv,
497 struct backbone_gw *backbone_gw)
498{
499 uint8_t mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100500 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100501
502 memcpy(mac, announce_mac, 4);
503 crc = htons(backbone_gw->crc);
Al Viro1a5852d2012-04-22 07:50:29 +0100504 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100505
506 bla_send_claim(bat_priv, mac, backbone_gw->vid, CLAIM_TYPE_ANNOUNCE);
507
508}
509
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200510/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100511 * @mac: the mac address of the claim
512 * @vid: the VLAN ID of the frame
513 * @backbone_gw: the backbone gateway which claims it
514 *
515 * Adds a claim in the claim hash.
516 */
517static void bla_add_claim(struct bat_priv *bat_priv, const uint8_t *mac,
518 const short vid, struct backbone_gw *backbone_gw)
519{
520 struct claim *claim;
521 struct claim search_claim;
522 int hash_added;
523
524 memcpy(search_claim.addr, mac, ETH_ALEN);
525 search_claim.vid = vid;
526 claim = claim_hash_find(bat_priv, &search_claim);
527
528 /* create a new claim entry if it does not exist yet. */
529 if (!claim) {
530 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
531 if (!claim)
532 return;
533
534 memcpy(claim->addr, mac, ETH_ALEN);
535 claim->vid = vid;
536 claim->lasttime = jiffies;
537 claim->backbone_gw = backbone_gw;
538
539 atomic_set(&claim->refcount, 2);
540 bat_dbg(DBG_BLA, bat_priv,
541 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
542 mac, vid);
543 hash_added = hash_add(bat_priv->claim_hash, compare_claim,
544 choose_claim, claim, &claim->hash_entry);
545
546 if (unlikely(hash_added != 0)) {
547 /* only local changes happened. */
548 kfree(claim);
549 return;
550 }
551 } else {
552 claim->lasttime = jiffies;
553 if (claim->backbone_gw == backbone_gw)
554 /* no need to register a new backbone */
555 goto claim_free_ref;
556
557 bat_dbg(DBG_BLA, bat_priv,
558 "bla_add_claim(): changing ownership for %pM, vid %d\n",
559 mac, vid);
560
561 claim->backbone_gw->crc ^=
562 crc16(0, claim->addr, ETH_ALEN);
563 backbone_gw_free_ref(claim->backbone_gw);
564
565 }
566 /* set (new) backbone gw */
567 atomic_inc(&backbone_gw->refcount);
568 claim->backbone_gw = backbone_gw;
569
570 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
571 backbone_gw->lasttime = jiffies;
572
573claim_free_ref:
574 claim_free_ref(claim);
575}
576
577/* Delete a claim from the claim hash which has the
578 * given mac address and vid.
579 */
580static void bla_del_claim(struct bat_priv *bat_priv, const uint8_t *mac,
581 const short vid)
582{
583 struct claim search_claim, *claim;
584
585 memcpy(search_claim.addr, mac, ETH_ALEN);
586 search_claim.vid = vid;
587 claim = claim_hash_find(bat_priv, &search_claim);
588 if (!claim)
589 return;
590
591 bat_dbg(DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n", mac, vid);
592
593 hash_remove(bat_priv->claim_hash, compare_claim, choose_claim, claim);
594 claim_free_ref(claim); /* reference from the hash is gone */
595
596 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
597
598 /* don't need the reference from hash_find() anymore */
599 claim_free_ref(claim);
600}
601
602/* check for ANNOUNCE frame, return 1 if handled */
603static int handle_announce(struct bat_priv *bat_priv,
604 uint8_t *an_addr, uint8_t *backbone_addr, short vid)
605{
606 struct backbone_gw *backbone_gw;
607 uint16_t crc;
608
609 if (memcmp(an_addr, announce_mac, 4) != 0)
610 return 0;
611
612 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
613
614 if (unlikely(!backbone_gw))
615 return 1;
616
617
618 /* handle as ANNOUNCE frame */
619 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100620 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100621
622 bat_dbg(DBG_BLA, bat_priv,
623 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
624 vid, backbone_gw->orig, crc);
625
626 if (backbone_gw->crc != crc) {
627 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
628 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
629 backbone_gw->orig, backbone_gw->vid, backbone_gw->crc,
630 crc);
631
632 bla_send_request(backbone_gw);
633 } else {
634 /* if we have sent a request and the crc was OK,
635 * we can allow traffic again.
636 */
637 if (atomic_read(&backbone_gw->request_sent)) {
638 atomic_dec(&backbone_gw->bat_priv->bla_num_requests);
639 atomic_set(&backbone_gw->request_sent, 0);
640 }
641 }
642
643 backbone_gw_free_ref(backbone_gw);
644 return 1;
645}
646
647/* check for REQUEST frame, return 1 if handled */
648static int handle_request(struct bat_priv *bat_priv,
649 struct hard_iface *primary_if,
650 uint8_t *backbone_addr,
651 struct ethhdr *ethhdr, short vid)
652{
653 /* check for REQUEST frame */
654 if (!compare_eth(backbone_addr, ethhdr->h_dest))
655 return 0;
656
657 /* sanity check, this should not happen on a normal switch,
658 * we ignore it in this case.
659 */
660 if (!compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
661 return 1;
662
663 bat_dbg(DBG_BLA, bat_priv,
664 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
665 vid, ethhdr->h_source);
666
667 bla_answer_request(bat_priv, primary_if, vid);
668 return 1;
669}
670
671/* check for UNCLAIM frame, return 1 if handled */
672static int handle_unclaim(struct bat_priv *bat_priv,
673 struct hard_iface *primary_if,
674 uint8_t *backbone_addr,
675 uint8_t *claim_addr, short vid)
676{
677 struct backbone_gw *backbone_gw;
678
679 /* unclaim in any case if it is our own */
680 if (primary_if && compare_eth(backbone_addr,
681 primary_if->net_dev->dev_addr))
682 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_DEL);
683
684 backbone_gw = backbone_hash_find(bat_priv, backbone_addr, vid);
685
686 if (!backbone_gw)
687 return 1;
688
689 /* this must be an UNCLAIM frame */
690 bat_dbg(DBG_BLA, bat_priv,
691 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
692 claim_addr, vid, backbone_gw->orig);
693
694 bla_del_claim(bat_priv, claim_addr, vid);
695 backbone_gw_free_ref(backbone_gw);
696 return 1;
697}
698
699/* check for CLAIM frame, return 1 if handled */
700static int handle_claim(struct bat_priv *bat_priv,
701 struct hard_iface *primary_if, uint8_t *backbone_addr,
702 uint8_t *claim_addr, short vid)
703{
704 struct backbone_gw *backbone_gw;
705
706 /* register the gateway if not yet available, and add the claim. */
707
708 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
709
710 if (unlikely(!backbone_gw))
711 return 1;
712
713 /* this must be a CLAIM frame */
714 bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
715 if (compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
716 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_ADD);
717
718 /* TODO: we could call something like tt_local_del() here. */
719
720 backbone_gw_free_ref(backbone_gw);
721 return 1;
722}
723
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200724/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100725 * @hw_src: the Hardware source in the ARP Header
726 * @hw_dst: the Hardware destination in the ARP Header
727 * @ethhdr: pointer to the Ethernet header of the claim frame
728 *
729 * checks if it is a claim packet and if its on the same group.
730 * This function also applies the group ID of the sender
731 * if it is in the same mesh.
732 *
733 * returns:
734 * 2 - if it is a claim packet and on the same group
735 * 1 - if is a claim packet from another group
736 * 0 - if it is not a claim packet
737 */
738static int check_claim_group(struct bat_priv *bat_priv,
739 struct hard_iface *primary_if,
740 uint8_t *hw_src, uint8_t *hw_dst,
741 struct ethhdr *ethhdr)
742{
743 uint8_t *backbone_addr;
744 struct orig_node *orig_node;
745 struct bla_claim_dst *bla_dst, *bla_dst_own;
746
747 bla_dst = (struct bla_claim_dst *)hw_dst;
748 bla_dst_own = &bat_priv->claim_dest;
749
750 /* check if it is a claim packet in general */
751 if (memcmp(bla_dst->magic, bla_dst_own->magic,
752 sizeof(bla_dst->magic)) != 0)
753 return 0;
754
755 /* if announcement packet, use the source,
756 * otherwise assume it is in the hw_src
757 */
758 switch (bla_dst->type) {
759 case CLAIM_TYPE_ADD:
760 backbone_addr = hw_src;
761 break;
762 case CLAIM_TYPE_REQUEST:
763 case CLAIM_TYPE_ANNOUNCE:
764 case CLAIM_TYPE_DEL:
765 backbone_addr = ethhdr->h_source;
766 break;
767 default:
768 return 0;
769 }
770
771 /* don't accept claim frames from ourselves */
772 if (compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
773 return 0;
774
775 /* if its already the same group, it is fine. */
776 if (bla_dst->group == bla_dst_own->group)
777 return 2;
778
779 /* lets see if this originator is in our mesh */
780 orig_node = orig_hash_find(bat_priv, backbone_addr);
781
782 /* dont accept claims from gateways which are not in
783 * the same mesh or group.
784 */
785 if (!orig_node)
786 return 1;
787
788 /* if our mesh friends mac is bigger, use it for ourselves. */
789 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
790 bat_dbg(DBG_BLA, bat_priv,
791 "taking other backbones claim group: %04x\n",
792 ntohs(bla_dst->group));
793 bla_dst_own->group = bla_dst->group;
794 }
795
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200796 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100797
798 return 2;
799}
800
801
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200802/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100803 * @skb: the frame to be checked
804 *
805 * Check if this is a claim frame, and process it accordingly.
806 *
807 * returns 1 if it was a claim frame, otherwise return 0 to
808 * tell the callee that it can use the frame on its own.
809 */
810static int bla_process_claim(struct bat_priv *bat_priv,
811 struct hard_iface *primary_if,
812 struct sk_buff *skb)
813{
814 struct ethhdr *ethhdr;
815 struct vlan_ethhdr *vhdr;
816 struct arphdr *arphdr;
817 uint8_t *hw_src, *hw_dst;
818 struct bla_claim_dst *bla_dst;
819 uint16_t proto;
820 int headlen;
821 short vid = -1;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100822 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100823
824 ethhdr = (struct ethhdr *)skb_mac_header(skb);
825
826 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
827 vhdr = (struct vlan_ethhdr *)ethhdr;
828 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
829 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
830 headlen = sizeof(*vhdr);
831 } else {
832 proto = ntohs(ethhdr->h_proto);
Antonio Quartulli0d125072012-02-18 11:27:34 +0100833 headlen = ETH_HLEN;
Simon Wunderlich23721382012-01-22 20:00:19 +0100834 }
835
836 if (proto != ETH_P_ARP)
837 return 0; /* not a claim frame */
838
839 /* this must be a ARP frame. check if it is a claim. */
840
841 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
842 return 0;
843
844 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
845 ethhdr = (struct ethhdr *)skb_mac_header(skb);
846 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
847
848 /* Check whether the ARP frame carries a valid
849 * IP information
850 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100851 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
852 return 0;
853 if (arphdr->ar_pro != htons(ETH_P_IP))
854 return 0;
855 if (arphdr->ar_hln != ETH_ALEN)
856 return 0;
857 if (arphdr->ar_pln != 4)
858 return 0;
859
860 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
861 hw_dst = hw_src + ETH_ALEN + 4;
862 bla_dst = (struct bla_claim_dst *)hw_dst;
863
864 /* check if it is a claim frame. */
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100865 ret = check_claim_group(bat_priv, primary_if, hw_src, hw_dst, ethhdr);
866 if (ret == 1)
867 bat_dbg(DBG_BLA, bat_priv,
868 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
869 ethhdr->h_source, vid, hw_src, hw_dst);
870
871 if (ret < 2)
872 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100873
874 /* become a backbone gw ourselves on this vlan if not happened yet */
875 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
876
877 /* check for the different types of claim frames ... */
878 switch (bla_dst->type) {
879 case CLAIM_TYPE_ADD:
880 if (handle_claim(bat_priv, primary_if, hw_src,
881 ethhdr->h_source, vid))
882 return 1;
883 break;
884 case CLAIM_TYPE_DEL:
885 if (handle_unclaim(bat_priv, primary_if,
886 ethhdr->h_source, hw_src, vid))
887 return 1;
888 break;
889
890 case CLAIM_TYPE_ANNOUNCE:
891 if (handle_announce(bat_priv, hw_src, ethhdr->h_source, vid))
892 return 1;
893 break;
894 case CLAIM_TYPE_REQUEST:
895 if (handle_request(bat_priv, primary_if, hw_src, ethhdr, vid))
896 return 1;
897 break;
898 }
899
900 bat_dbg(DBG_BLA, bat_priv,
901 "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",
902 ethhdr->h_source, vid, hw_src, hw_dst);
903 return 1;
904}
905
906/* Check when we last heard from other nodes, and remove them in case of
907 * a time out, or clean all backbone gws if now is set.
908 */
909static void bla_purge_backbone_gw(struct bat_priv *bat_priv, int now)
910{
911 struct backbone_gw *backbone_gw;
912 struct hlist_node *node, *node_tmp;
913 struct hlist_head *head;
914 struct hashtable_t *hash;
915 spinlock_t *list_lock; /* protects write access to the hash lists */
916 int i;
917
918 hash = bat_priv->backbone_hash;
919 if (!hash)
920 return;
921
922 for (i = 0; i < hash->size; i++) {
923 head = &hash->table[i];
924 list_lock = &hash->list_locks[i];
925
926 spin_lock_bh(list_lock);
927 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
928 head, hash_entry) {
929 if (now)
930 goto purge_now;
931 if (!has_timed_out(backbone_gw->lasttime,
932 BLA_BACKBONE_TIMEOUT))
933 continue;
934
935 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
936 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
937 backbone_gw->orig);
938
939purge_now:
940 /* don't wait for the pending request anymore */
941 if (atomic_read(&backbone_gw->request_sent))
942 atomic_dec(&bat_priv->bla_num_requests);
943
944 bla_del_backbone_claims(backbone_gw);
945
946 hlist_del_rcu(node);
947 backbone_gw_free_ref(backbone_gw);
948 }
949 spin_unlock_bh(list_lock);
950 }
951}
952
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200953/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100954 * @primary_if: the selected primary interface, may be NULL if now is set
955 * @now: whether the whole hash shall be wiped now
956 *
957 * Check when we heard last time from our own claims, and remove them in case of
958 * a time out, or clean all claims if now is set
959 */
960static void bla_purge_claims(struct bat_priv *bat_priv,
961 struct hard_iface *primary_if, int now)
962{
963 struct claim *claim;
964 struct hlist_node *node;
965 struct hlist_head *head;
966 struct hashtable_t *hash;
967 int i;
968
969 hash = bat_priv->claim_hash;
970 if (!hash)
971 return;
972
973 for (i = 0; i < hash->size; i++) {
974 head = &hash->table[i];
975
976 rcu_read_lock();
977 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
978 if (now)
979 goto purge_now;
980 if (!compare_eth(claim->backbone_gw->orig,
981 primary_if->net_dev->dev_addr))
982 continue;
983 if (!has_timed_out(claim->lasttime,
984 BLA_CLAIM_TIMEOUT))
985 continue;
986
987 bat_dbg(DBG_BLA, bat_priv,
988 "bla_purge_claims(): %pM, vid %d, time out\n",
989 claim->addr, claim->vid);
990
991purge_now:
992 handle_unclaim(bat_priv, primary_if,
993 claim->backbone_gw->orig,
994 claim->addr, claim->vid);
995 }
996 rcu_read_unlock();
997 }
998}
999
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001000/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001001 * @primary_if: the new selected primary_if
1002 * @oldif: the old primary interface, may be NULL
1003 *
1004 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001005 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001006void batadv_bla_update_orig_address(struct bat_priv *bat_priv,
1007 struct hard_iface *primary_if,
1008 struct hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001009{
1010 struct backbone_gw *backbone_gw;
1011 struct hlist_node *node;
1012 struct hlist_head *head;
1013 struct hashtable_t *hash;
1014 int i;
1015
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001016 /* reset bridge loop avoidance group id */
1017 bat_priv->claim_dest.group =
1018 htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1019
Simon Wunderlich23721382012-01-22 20:00:19 +01001020 if (!oldif) {
1021 bla_purge_claims(bat_priv, NULL, 1);
1022 bla_purge_backbone_gw(bat_priv, 1);
1023 return;
1024 }
1025
1026 hash = bat_priv->backbone_hash;
1027 if (!hash)
1028 return;
1029
1030 for (i = 0; i < hash->size; i++) {
1031 head = &hash->table[i];
1032
1033 rcu_read_lock();
1034 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1035 /* own orig still holds the old value. */
1036 if (!compare_eth(backbone_gw->orig,
1037 oldif->net_dev->dev_addr))
1038 continue;
1039
1040 memcpy(backbone_gw->orig,
1041 primary_if->net_dev->dev_addr, ETH_ALEN);
1042 /* send an announce frame so others will ask for our
1043 * claims and update their tables.
1044 */
1045 bla_send_announce(bat_priv, backbone_gw);
1046 }
1047 rcu_read_unlock();
1048 }
1049}
1050
1051
1052
1053/* (re)start the timer */
1054static void bla_start_timer(struct bat_priv *bat_priv)
1055{
1056 INIT_DELAYED_WORK(&bat_priv->bla_work, bla_periodic_work);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001057 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla_work,
Simon Wunderlich23721382012-01-22 20:00:19 +01001058 msecs_to_jiffies(BLA_PERIOD_LENGTH));
1059}
1060
1061/* periodic work to do:
1062 * * purge structures when they are too old
1063 * * send announcements
1064 */
1065static void bla_periodic_work(struct work_struct *work)
1066{
1067 struct delayed_work *delayed_work =
1068 container_of(work, struct delayed_work, work);
1069 struct bat_priv *bat_priv =
1070 container_of(delayed_work, struct bat_priv, bla_work);
1071 struct hlist_node *node;
1072 struct hlist_head *head;
1073 struct backbone_gw *backbone_gw;
1074 struct hashtable_t *hash;
1075 struct hard_iface *primary_if;
1076 int i;
1077
1078 primary_if = primary_if_get_selected(bat_priv);
1079 if (!primary_if)
1080 goto out;
1081
1082 bla_purge_claims(bat_priv, primary_if, 0);
1083 bla_purge_backbone_gw(bat_priv, 0);
1084
1085 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1086 goto out;
1087
1088 hash = bat_priv->backbone_hash;
1089 if (!hash)
1090 goto out;
1091
1092 for (i = 0; i < hash->size; i++) {
1093 head = &hash->table[i];
1094
1095 rcu_read_lock();
1096 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1097 if (!compare_eth(backbone_gw->orig,
1098 primary_if->net_dev->dev_addr))
1099 continue;
1100
1101 backbone_gw->lasttime = jiffies;
1102
1103 bla_send_announce(bat_priv, backbone_gw);
1104 }
1105 rcu_read_unlock();
1106 }
1107out:
1108 if (primary_if)
1109 hardif_free_ref(primary_if);
1110
1111 bla_start_timer(bat_priv);
1112}
1113
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001114/* The hash for claim and backbone hash receive the same key because they
1115 * are getting initialized by hash_new with the same key. Reinitializing
1116 * them with to different keys to allow nested locking without generating
1117 * lockdep warnings
1118 */
1119static struct lock_class_key claim_hash_lock_class_key;
1120static struct lock_class_key backbone_hash_lock_class_key;
1121
Simon Wunderlich23721382012-01-22 20:00:19 +01001122/* initialize all bla structures */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001123int batadv_bla_init(struct bat_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001124{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001125 int i;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001126 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1127 struct hard_iface *primary_if;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001128
Simon Wunderlich23721382012-01-22 20:00:19 +01001129 bat_dbg(DBG_BLA, bat_priv, "bla hash registering\n");
1130
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001131 /* setting claim destination address */
1132 memcpy(&bat_priv->claim_dest.magic, claim_dest, 3);
1133 bat_priv->claim_dest.type = 0;
1134 primary_if = primary_if_get_selected(bat_priv);
1135 if (primary_if) {
1136 bat_priv->claim_dest.group =
1137 htons(crc16(0, primary_if->net_dev->dev_addr,
1138 ETH_ALEN));
1139 hardif_free_ref(primary_if);
1140 } else {
1141 bat_priv->claim_dest.group = 0; /* will be set later */
1142 }
1143
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001144 /* initialize the duplicate list */
1145 for (i = 0; i < DUPLIST_SIZE; i++)
1146 bat_priv->bcast_duplist[i].entrytime =
1147 jiffies - msecs_to_jiffies(DUPLIST_TIMEOUT);
1148 bat_priv->bcast_duplist_curr = 0;
1149
Simon Wunderlich23721382012-01-22 20:00:19 +01001150 if (bat_priv->claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001151 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001152
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001153 bat_priv->claim_hash = batadv_hash_new(128);
1154 bat_priv->backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001155
1156 if (!bat_priv->claim_hash || !bat_priv->backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001157 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001158
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001159 batadv_hash_set_lock_class(bat_priv->claim_hash,
1160 &claim_hash_lock_class_key);
1161 batadv_hash_set_lock_class(bat_priv->backbone_hash,
1162 &backbone_hash_lock_class_key);
1163
Simon Wunderlich23721382012-01-22 20:00:19 +01001164 bat_dbg(DBG_BLA, bat_priv, "bla hashes initialized\n");
1165
1166 bla_start_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001167 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001168}
1169
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001170/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001171 * @bcast_packet: originator mac address
1172 * @hdr_size: maximum length of the frame
1173 *
1174 * check if it is on our broadcast list. Another gateway might
1175 * have sent the same packet because it is connected to the same backbone,
1176 * so we have to remove this duplicate.
1177 *
1178 * This is performed by checking the CRC, which will tell us
1179 * with a good chance that it is the same packet. If it is furthermore
1180 * sent by another host, drop it. We allow equal packets from
1181 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001182 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001183int batadv_bla_check_bcast_duplist(struct bat_priv *bat_priv,
1184 struct bcast_packet *bcast_packet,
1185 int hdr_size)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001186{
1187 int i, length, curr;
1188 uint8_t *content;
1189 uint16_t crc;
1190 struct bcast_duplist_entry *entry;
1191
1192 length = hdr_size - sizeof(*bcast_packet);
1193 content = (uint8_t *)bcast_packet;
1194 content += sizeof(*bcast_packet);
1195
1196 /* calculate the crc ... */
1197 crc = crc16(0, content, length);
1198
1199 for (i = 0 ; i < DUPLIST_SIZE; i++) {
1200 curr = (bat_priv->bcast_duplist_curr + i) % DUPLIST_SIZE;
1201 entry = &bat_priv->bcast_duplist[curr];
1202
1203 /* we can stop searching if the entry is too old ;
1204 * later entries will be even older
1205 */
1206 if (has_timed_out(entry->entrytime, DUPLIST_TIMEOUT))
1207 break;
1208
1209 if (entry->crc != crc)
1210 continue;
1211
1212 if (compare_eth(entry->orig, bcast_packet->orig))
1213 continue;
1214
1215 /* this entry seems to match: same crc, not too old,
1216 * and from another gw. therefore return 1 to forbid it.
1217 */
1218 return 1;
1219 }
1220 /* not found, add a new entry (overwrite the oldest entry) */
1221 curr = (bat_priv->bcast_duplist_curr + DUPLIST_SIZE - 1) % DUPLIST_SIZE;
1222 entry = &bat_priv->bcast_duplist[curr];
1223 entry->crc = crc;
1224 entry->entrytime = jiffies;
1225 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
1226 bat_priv->bcast_duplist_curr = curr;
1227
1228 /* allow it, its the first occurence. */
1229 return 0;
1230}
1231
1232
1233
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001234/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001235 * @orig: originator mac address
1236 *
1237 * check if the originator is a gateway for any VLAN ID.
1238 *
1239 * returns 1 if it is found, 0 otherwise
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001240 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001241int batadv_bla_is_backbone_gw_orig(struct bat_priv *bat_priv, uint8_t *orig)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001242{
1243 struct hashtable_t *hash = bat_priv->backbone_hash;
1244 struct hlist_head *head;
1245 struct hlist_node *node;
1246 struct backbone_gw *backbone_gw;
1247 int i;
1248
1249 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1250 return 0;
1251
1252 if (!hash)
1253 return 0;
1254
1255 for (i = 0; i < hash->size; i++) {
1256 head = &hash->table[i];
1257
1258 rcu_read_lock();
1259 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1260 if (compare_eth(backbone_gw->orig, orig)) {
1261 rcu_read_unlock();
1262 return 1;
1263 }
1264 }
1265 rcu_read_unlock();
1266 }
1267
1268 return 0;
1269}
1270
1271
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001272/* @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001273 * @orig_node: the orig_node of the frame
1274 * @hdr_size: maximum length of the frame
1275 *
1276 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1277 * if the orig_node is also a gateway on the soft interface, otherwise it
1278 * returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001279 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001280int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1281 struct orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001282{
1283 struct ethhdr *ethhdr;
1284 struct vlan_ethhdr *vhdr;
1285 struct backbone_gw *backbone_gw;
1286 short vid = -1;
1287
1288 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1289 return 0;
1290
1291 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001292 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001293 return 0;
1294
1295 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1296
1297 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1298 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1299 return 0;
1300
1301 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1302 hdr_size);
1303 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1304 }
1305
1306 /* see if this originator is a backbone gw for this VLAN */
Simon Wunderlich23721382012-01-22 20:00:19 +01001307 backbone_gw = backbone_hash_find(orig_node->bat_priv,
1308 orig_node->orig, vid);
1309 if (!backbone_gw)
1310 return 0;
1311
1312 backbone_gw_free_ref(backbone_gw);
1313 return 1;
1314}
1315
1316/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001317void batadv_bla_free(struct bat_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001318{
1319 struct hard_iface *primary_if;
1320
1321 cancel_delayed_work_sync(&bat_priv->bla_work);
1322 primary_if = primary_if_get_selected(bat_priv);
1323
1324 if (bat_priv->claim_hash) {
1325 bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001326 batadv_hash_destroy(bat_priv->claim_hash);
Simon Wunderlich23721382012-01-22 20:00:19 +01001327 bat_priv->claim_hash = NULL;
1328 }
1329 if (bat_priv->backbone_hash) {
1330 bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001331 batadv_hash_destroy(bat_priv->backbone_hash);
Simon Wunderlich23721382012-01-22 20:00:19 +01001332 bat_priv->backbone_hash = NULL;
1333 }
1334 if (primary_if)
1335 hardif_free_ref(primary_if);
1336}
1337
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001338/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001339 * @skb: the frame to be checked
1340 * @vid: the VLAN ID of the frame
1341 *
1342 * bla_rx avoidance checks if:
1343 * * we have to race for a claim
1344 * * if the frame is allowed on the LAN
1345 *
1346 * in these cases, the skb is further handled by this function and
1347 * returns 1, otherwise it returns 0 and the caller shall further
1348 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001349 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001350int batadv_bla_rx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001351{
1352 struct ethhdr *ethhdr;
1353 struct claim search_claim, *claim = NULL;
1354 struct hard_iface *primary_if;
1355 int ret;
1356
1357 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1358
1359 primary_if = primary_if_get_selected(bat_priv);
1360 if (!primary_if)
1361 goto handled;
1362
1363 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1364 goto allow;
1365
1366
1367 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1368 /* don't allow broadcasts while requests are in flight */
1369 if (is_multicast_ether_addr(ethhdr->h_dest))
1370 goto handled;
1371
1372 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1373 search_claim.vid = vid;
1374 claim = claim_hash_find(bat_priv, &search_claim);
1375
1376 if (!claim) {
1377 /* possible optimization: race for a claim */
1378 /* No claim exists yet, claim it for us!
1379 */
1380 handle_claim(bat_priv, primary_if,
1381 primary_if->net_dev->dev_addr,
1382 ethhdr->h_source, vid);
1383 goto allow;
1384 }
1385
1386 /* if it is our own claim ... */
1387 if (compare_eth(claim->backbone_gw->orig,
1388 primary_if->net_dev->dev_addr)) {
1389 /* ... allow it in any case */
1390 claim->lasttime = jiffies;
1391 goto allow;
1392 }
1393
1394 /* if it is a broadcast ... */
1395 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1396 /* ... drop it. the responsible gateway is in charge. */
1397 goto handled;
1398 } else {
1399 /* seems the client considers us as its best gateway.
1400 * send a claim and update the claim table
1401 * immediately.
1402 */
1403 handle_claim(bat_priv, primary_if,
1404 primary_if->net_dev->dev_addr,
1405 ethhdr->h_source, vid);
1406 goto allow;
1407 }
1408allow:
1409 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1410 ret = 0;
1411 goto out;
1412
1413handled:
1414 kfree_skb(skb);
1415 ret = 1;
1416
1417out:
1418 if (primary_if)
1419 hardif_free_ref(primary_if);
1420 if (claim)
1421 claim_free_ref(claim);
1422 return ret;
1423}
1424
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001425/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001426 * @skb: the frame to be checked
1427 * @vid: the VLAN ID of the frame
1428 *
1429 * bla_tx checks if:
1430 * * a claim was received which has to be processed
1431 * * the frame is allowed on the mesh
1432 *
1433 * in these cases, the skb is further handled by this function and
1434 * returns 1, otherwise it returns 0 and the caller shall further
1435 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001436 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001437int batadv_bla_tx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001438{
1439 struct ethhdr *ethhdr;
1440 struct claim search_claim, *claim = NULL;
1441 struct hard_iface *primary_if;
1442 int ret = 0;
1443
1444 primary_if = primary_if_get_selected(bat_priv);
1445 if (!primary_if)
1446 goto out;
1447
1448 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1449 goto allow;
1450
1451 /* in VLAN case, the mac header might not be set. */
1452 skb_reset_mac_header(skb);
1453
1454 if (bla_process_claim(bat_priv, primary_if, skb))
1455 goto handled;
1456
1457 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1458
1459 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1460 /* don't allow broadcasts while requests are in flight */
1461 if (is_multicast_ether_addr(ethhdr->h_dest))
1462 goto handled;
1463
1464 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1465 search_claim.vid = vid;
1466
1467 claim = claim_hash_find(bat_priv, &search_claim);
1468
1469 /* if no claim exists, allow it. */
1470 if (!claim)
1471 goto allow;
1472
1473 /* check if we are responsible. */
1474 if (compare_eth(claim->backbone_gw->orig,
1475 primary_if->net_dev->dev_addr)) {
1476 /* if yes, the client has roamed and we have
1477 * to unclaim it.
1478 */
1479 handle_unclaim(bat_priv, primary_if,
1480 primary_if->net_dev->dev_addr,
1481 ethhdr->h_source, vid);
1482 goto allow;
1483 }
1484
1485 /* check if it is a multicast/broadcast frame */
1486 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1487 /* drop it. the responsible gateway has forwarded it into
1488 * the backbone network.
1489 */
1490 goto handled;
1491 } else {
1492 /* we must allow it. at least if we are
1493 * responsible for the DESTINATION.
1494 */
1495 goto allow;
1496 }
1497allow:
1498 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1499 ret = 0;
1500 goto out;
1501handled:
1502 ret = 1;
1503out:
1504 if (primary_if)
1505 hardif_free_ref(primary_if);
1506 if (claim)
1507 claim_free_ref(claim);
1508 return ret;
1509}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001510
Sven Eckelmann08adf152012-05-12 13:38:47 +02001511int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001512{
1513 struct net_device *net_dev = (struct net_device *)seq->private;
1514 struct bat_priv *bat_priv = netdev_priv(net_dev);
1515 struct hashtable_t *hash = bat_priv->claim_hash;
1516 struct claim *claim;
1517 struct hard_iface *primary_if;
1518 struct hlist_node *node;
1519 struct hlist_head *head;
1520 uint32_t i;
1521 bool is_own;
1522 int ret = 0;
1523
1524 primary_if = primary_if_get_selected(bat_priv);
1525 if (!primary_if) {
1526 ret = seq_printf(seq,
1527 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1528 net_dev->name);
1529 goto out;
1530 }
1531
1532 if (primary_if->if_status != IF_ACTIVE) {
1533 ret = seq_printf(seq,
1534 "BATMAN mesh %s disabled - primary interface not active\n",
1535 net_dev->name);
1536 goto out;
1537 }
1538
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001539 seq_printf(seq,
1540 "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
1541 net_dev->name, primary_if->net_dev->dev_addr,
1542 ntohs(bat_priv->claim_dest.group));
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001543 seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
1544 "Client", "VID", "Originator", "CRC");
1545 for (i = 0; i < hash->size; i++) {
1546 head = &hash->table[i];
1547
1548 rcu_read_lock();
1549 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1550 is_own = compare_eth(claim->backbone_gw->orig,
1551 primary_if->net_dev->dev_addr);
1552 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
1553 claim->addr, claim->vid,
1554 claim->backbone_gw->orig,
1555 (is_own ? 'x' : ' '),
1556 claim->backbone_gw->crc);
1557 }
1558 rcu_read_unlock();
1559 }
1560out:
1561 if (primary_if)
1562 hardif_free_ref(primary_if);
1563 return ret;
1564}