blob: f84c892be7ea0ba5fb116784bb61b89b5318e2fb [file] [log] [blame]
Simon Wunderlich23721382012-01-22 20:00:19 +01001/*
2 * Copyright (C) 2011 B.A.T.M.A.N. contributors:
3 *
4 * Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "hash.h"
24#include "hard-interface.h"
25#include "originator.h"
26#include "bridge_loop_avoidance.h"
27#include "send.h"
28
29#include <linux/etherdevice.h>
30#include <linux/crc16.h>
31#include <linux/if_arp.h>
32#include <net/arp.h>
33#include <linux/if_vlan.h>
34
35static const uint8_t claim_dest[6] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
36static const uint8_t announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
37
38static void bla_periodic_work(struct work_struct *work);
39static void bla_send_announce(struct bat_priv *bat_priv,
40 struct backbone_gw *backbone_gw);
41
42/* return the index of the claim */
43static inline uint32_t choose_claim(const void *data, uint32_t size)
44{
45 const unsigned char *key = data;
46 uint32_t hash = 0;
47 size_t i;
48
49 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
50 hash += key[i];
51 hash += (hash << 10);
52 hash ^= (hash >> 6);
53 }
54
55 hash += (hash << 3);
56 hash ^= (hash >> 11);
57 hash += (hash << 15);
58
59 return hash % size;
60}
61
62/* return the index of the backbone gateway */
63static inline uint32_t choose_backbone_gw(const void *data, uint32_t size)
64{
65 const unsigned char *key = data;
66 uint32_t hash = 0;
67 size_t i;
68
69 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
70 hash += key[i];
71 hash += (hash << 10);
72 hash ^= (hash >> 6);
73 }
74
75 hash += (hash << 3);
76 hash ^= (hash >> 11);
77 hash += (hash << 15);
78
79 return hash % size;
80}
81
82
83/* compares address and vid of two backbone gws */
84static int compare_backbone_gw(const struct hlist_node *node, const void *data2)
85{
86 const void *data1 = container_of(node, struct backbone_gw,
87 hash_entry);
88
89 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
90}
91
92/* compares address and vid of two claims */
93static int compare_claim(const struct hlist_node *node, const void *data2)
94{
95 const void *data1 = container_of(node, struct claim,
96 hash_entry);
97
98 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
99}
100
101/* free a backbone gw */
102static void backbone_gw_free_ref(struct backbone_gw *backbone_gw)
103{
104 if (atomic_dec_and_test(&backbone_gw->refcount))
105 kfree_rcu(backbone_gw, rcu);
106}
107
108/* finally deinitialize the claim */
109static void claim_free_rcu(struct rcu_head *rcu)
110{
111 struct claim *claim;
112
113 claim = container_of(rcu, struct claim, rcu);
114
115 backbone_gw_free_ref(claim->backbone_gw);
116 kfree(claim);
117}
118
119/* free a claim, call claim_free_rcu if its the last reference */
120static void claim_free_ref(struct claim *claim)
121{
122 if (atomic_dec_and_test(&claim->refcount))
123 call_rcu(&claim->rcu, claim_free_rcu);
124}
125
126/**
127 * @bat_priv: the bat priv with all the soft interface information
128 * @data: search data (may be local/static data)
129 *
130 * looks for a claim in the hash, and returns it if found
131 * or NULL otherwise.
132 */
133static struct claim *claim_hash_find(struct bat_priv *bat_priv,
134 struct claim *data)
135{
136 struct hashtable_t *hash = bat_priv->claim_hash;
137 struct hlist_head *head;
138 struct hlist_node *node;
139 struct claim *claim;
140 struct claim *claim_tmp = NULL;
141 int index;
142
143 if (!hash)
144 return NULL;
145
146 index = choose_claim(data, hash->size);
147 head = &hash->table[index];
148
149 rcu_read_lock();
150 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
151 if (!compare_claim(&claim->hash_entry, data))
152 continue;
153
154 if (!atomic_inc_not_zero(&claim->refcount))
155 continue;
156
157 claim_tmp = claim;
158 break;
159 }
160 rcu_read_unlock();
161
162 return claim_tmp;
163}
164
165/**
166 * @bat_priv: the bat priv with all the soft interface information
167 * @addr: the address of the originator
168 * @vid: the VLAN ID
169 *
170 * looks for a claim in the hash, and returns it if found
171 * or NULL otherwise.
172 */
173static struct backbone_gw *backbone_hash_find(struct bat_priv *bat_priv,
174 uint8_t *addr, short vid)
175{
176 struct hashtable_t *hash = bat_priv->backbone_hash;
177 struct hlist_head *head;
178 struct hlist_node *node;
179 struct backbone_gw search_entry, *backbone_gw;
180 struct backbone_gw *backbone_gw_tmp = NULL;
181 int index;
182
183 if (!hash)
184 return NULL;
185
186 memcpy(search_entry.orig, addr, ETH_ALEN);
187 search_entry.vid = vid;
188
189 index = choose_backbone_gw(&search_entry, hash->size);
190 head = &hash->table[index];
191
192 rcu_read_lock();
193 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
194 if (!compare_backbone_gw(&backbone_gw->hash_entry,
195 &search_entry))
196 continue;
197
198 if (!atomic_inc_not_zero(&backbone_gw->refcount))
199 continue;
200
201 backbone_gw_tmp = backbone_gw;
202 break;
203 }
204 rcu_read_unlock();
205
206 return backbone_gw_tmp;
207}
208
209/* delete all claims for a backbone */
210static void bla_del_backbone_claims(struct backbone_gw *backbone_gw)
211{
212 struct hashtable_t *hash;
213 struct hlist_node *node, *node_tmp;
214 struct hlist_head *head;
215 struct claim *claim;
216 int i;
217 spinlock_t *list_lock; /* protects write access to the hash lists */
218
219 hash = backbone_gw->bat_priv->claim_hash;
220 if (!hash)
221 return;
222
223 for (i = 0; i < hash->size; i++) {
224 head = &hash->table[i];
225 list_lock = &hash->list_locks[i];
226
227 spin_lock_bh(list_lock);
228 hlist_for_each_entry_safe(claim, node, node_tmp,
229 head, hash_entry) {
230
231 if (claim->backbone_gw != backbone_gw)
232 continue;
233
234 claim_free_ref(claim);
235 hlist_del_rcu(node);
236 }
237 spin_unlock_bh(list_lock);
238 }
239
240 /* all claims gone, intialize CRC */
241 backbone_gw->crc = BLA_CRC_INIT;
242}
243
244/**
245 * @bat_priv: the bat priv with all the soft interface information
246 * @orig: the mac address to be announced within the claim
247 * @vid: the VLAN ID
248 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
249 *
250 * sends a claim frame according to the provided info.
251 */
252static void bla_send_claim(struct bat_priv *bat_priv, uint8_t *mac,
253 short vid, int claimtype)
254{
255 struct sk_buff *skb;
256 struct ethhdr *ethhdr;
257 struct hard_iface *primary_if;
258 struct net_device *soft_iface;
259 uint8_t *hw_src;
260 struct bla_claim_dst local_claim_dest;
261 uint32_t zeroip = 0;
262
263 primary_if = primary_if_get_selected(bat_priv);
264 if (!primary_if)
265 return;
266
267 memcpy(&local_claim_dest, claim_dest, sizeof(local_claim_dest));
268 local_claim_dest.type = claimtype;
269
270 soft_iface = primary_if->soft_iface;
271
272 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
273 /* IP DST: 0.0.0.0 */
274 zeroip,
275 primary_if->soft_iface,
276 /* IP SRC: 0.0.0.0 */
277 zeroip,
278 /* Ethernet DST: Broadcast */
279 NULL,
280 /* Ethernet SRC/HW SRC: originator mac */
281 primary_if->net_dev->dev_addr,
282 /* HW DST: FF:43:05:XX:00:00
283 * with XX = claim type
284 */
285 (uint8_t *)&local_claim_dest);
286
287 if (!skb)
288 goto out;
289
290 ethhdr = (struct ethhdr *)skb->data;
291 hw_src = (uint8_t *)ethhdr +
292 sizeof(struct ethhdr) +
293 sizeof(struct arphdr);
294
295 /* now we pretend that the client would have sent this ... */
296 switch (claimtype) {
297 case CLAIM_TYPE_ADD:
298 /* normal claim frame
299 * set Ethernet SRC to the clients mac
300 */
301 memcpy(ethhdr->h_source, mac, ETH_ALEN);
302 bat_dbg(DBG_BLA, bat_priv,
303 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
304 break;
305 case CLAIM_TYPE_DEL:
306 /* unclaim frame
307 * set HW SRC to the clients mac
308 */
309 memcpy(hw_src, mac, ETH_ALEN);
310 bat_dbg(DBG_BLA, bat_priv,
311 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac, vid);
312 break;
313 case CLAIM_TYPE_ANNOUNCE:
314 /* announcement frame
315 * set HW SRC to the special mac containg the crc
316 */
317 memcpy(hw_src, mac, ETH_ALEN);
318 bat_dbg(DBG_BLA, bat_priv,
319 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
320 ethhdr->h_source, vid);
321 break;
322 case CLAIM_TYPE_REQUEST:
323 /* request frame
324 * set HW SRC to the special mac containg the crc
325 */
326 memcpy(hw_src, mac, ETH_ALEN);
327 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
328 bat_dbg(DBG_BLA, bat_priv,
329 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
330 ethhdr->h_source, ethhdr->h_dest, vid);
331 break;
332
333 }
334
335 if (vid != -1)
336 skb = vlan_insert_tag(skb, vid);
337
338 skb_reset_mac_header(skb);
339 skb->protocol = eth_type_trans(skb, soft_iface);
340 bat_priv->stats.rx_packets++;
341 bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
342 soft_iface->last_rx = jiffies;
343
344 netif_rx(skb);
345out:
346 if (primary_if)
347 hardif_free_ref(primary_if);
348}
349
350/**
351 * @bat_priv: the bat priv with all the soft interface information
352 * @orig: the mac address of the originator
353 * @vid: the VLAN ID
354 *
355 * searches for the backbone gw or creates a new one if it could not
356 * be found.
357 */
358static struct backbone_gw *bla_get_backbone_gw(struct bat_priv *bat_priv,
359 uint8_t *orig, short vid)
360{
361 struct backbone_gw *entry;
362 int hash_added;
363
364 entry = backbone_hash_find(bat_priv, orig, vid);
365
366 if (entry)
367 return entry;
368
369 bat_dbg(DBG_BLA, bat_priv,
370 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
371 orig, vid);
372
373 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
374 if (!entry)
375 return NULL;
376
377 entry->vid = vid;
378 entry->lasttime = jiffies;
379 entry->crc = BLA_CRC_INIT;
380 entry->bat_priv = bat_priv;
381 atomic_set(&entry->request_sent, 0);
382 memcpy(entry->orig, orig, ETH_ALEN);
383
384 /* one for the hash, one for returning */
385 atomic_set(&entry->refcount, 2);
386
387 hash_added = hash_add(bat_priv->backbone_hash, compare_backbone_gw,
388 choose_backbone_gw, entry, &entry->hash_entry);
389
390 if (unlikely(hash_added != 0)) {
391 /* hash failed, free the structure */
392 kfree(entry);
393 return NULL;
394 }
395
396 return entry;
397}
398
399/* update or add the own backbone gw to make sure we announce
400 * where we receive other backbone gws
401 */
402static void bla_update_own_backbone_gw(struct bat_priv *bat_priv,
403 struct hard_iface *primary_if,
404 short vid)
405{
406 struct backbone_gw *backbone_gw;
407
408 backbone_gw = bla_get_backbone_gw(bat_priv,
409 primary_if->net_dev->dev_addr, vid);
410 if (unlikely(!backbone_gw))
411 return;
412
413 backbone_gw->lasttime = jiffies;
414 backbone_gw_free_ref(backbone_gw);
415}
416
417/**
418 * @bat_priv: the bat priv with all the soft interface information
419 * @vid: the vid where the request came on
420 *
421 * Repeat all of our own claims, and finally send an ANNOUNCE frame
422 * to allow the requester another check if the CRC is correct now.
423 */
424static void bla_answer_request(struct bat_priv *bat_priv,
425 struct hard_iface *primary_if, short vid)
426{
427 struct hlist_node *node;
428 struct hlist_head *head;
429 struct hashtable_t *hash;
430 struct claim *claim;
431 struct backbone_gw *backbone_gw;
432 int i;
433
434 bat_dbg(DBG_BLA, bat_priv,
435 "bla_answer_request(): received a claim request, send all of our own claims again\n");
436
437 backbone_gw = backbone_hash_find(bat_priv,
438 primary_if->net_dev->dev_addr, vid);
439 if (!backbone_gw)
440 return;
441
442 hash = bat_priv->claim_hash;
443 for (i = 0; i < hash->size; i++) {
444 head = &hash->table[i];
445
446 rcu_read_lock();
447 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
448 /* only own claims are interesting */
449 if (claim->backbone_gw != backbone_gw)
450 continue;
451
452 bla_send_claim(bat_priv, claim->addr, claim->vid,
453 CLAIM_TYPE_ADD);
454 }
455 rcu_read_unlock();
456 }
457
458 /* finally, send an announcement frame */
459 bla_send_announce(bat_priv, backbone_gw);
460 backbone_gw_free_ref(backbone_gw);
461}
462
463/**
464 * @backbone_gw: the backbone gateway from whom we are out of sync
465 *
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
490/**
491 * @bat_priv: the bat priv with all the soft interface information
492 * @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];
501 uint16_t crc;
502
503 memcpy(mac, announce_mac, 4);
504 crc = htons(backbone_gw->crc);
505 memcpy(&mac[4], (uint8_t *)&crc, 2);
506
507 bla_send_claim(bat_priv, mac, backbone_gw->vid, CLAIM_TYPE_ANNOUNCE);
508
509}
510
511/**
512 * @bat_priv: the bat priv with all the soft interface information
513 * @mac: the mac address of the claim
514 * @vid: the VLAN ID of the frame
515 * @backbone_gw: the backbone gateway which claims it
516 *
517 * Adds a claim in the claim hash.
518 */
519static void bla_add_claim(struct bat_priv *bat_priv, const uint8_t *mac,
520 const short vid, struct backbone_gw *backbone_gw)
521{
522 struct claim *claim;
523 struct claim search_claim;
524 int hash_added;
525
526 memcpy(search_claim.addr, mac, ETH_ALEN);
527 search_claim.vid = vid;
528 claim = claim_hash_find(bat_priv, &search_claim);
529
530 /* create a new claim entry if it does not exist yet. */
531 if (!claim) {
532 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
533 if (!claim)
534 return;
535
536 memcpy(claim->addr, mac, ETH_ALEN);
537 claim->vid = vid;
538 claim->lasttime = jiffies;
539 claim->backbone_gw = backbone_gw;
540
541 atomic_set(&claim->refcount, 2);
542 bat_dbg(DBG_BLA, bat_priv,
543 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
544 mac, vid);
545 hash_added = hash_add(bat_priv->claim_hash, compare_claim,
546 choose_claim, claim, &claim->hash_entry);
547
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
559 bat_dbg(DBG_BLA, bat_priv,
560 "bla_add_claim(): changing ownership for %pM, vid %d\n",
561 mac, vid);
562
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
593 bat_dbg(DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n", mac, vid);
594
595 hash_remove(bat_priv->claim_hash, compare_claim, choose_claim, claim);
596 claim_free_ref(claim); /* reference from the hash is gone */
597
598 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
599
600 /* don't need the reference from hash_find() anymore */
601 claim_free_ref(claim);
602}
603
604/* check for ANNOUNCE frame, return 1 if handled */
605static int handle_announce(struct bat_priv *bat_priv,
606 uint8_t *an_addr, uint8_t *backbone_addr, short vid)
607{
608 struct backbone_gw *backbone_gw;
609 uint16_t crc;
610
611 if (memcmp(an_addr, announce_mac, 4) != 0)
612 return 0;
613
614 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
615
616 if (unlikely(!backbone_gw))
617 return 1;
618
619
620 /* handle as ANNOUNCE frame */
621 backbone_gw->lasttime = jiffies;
622 crc = ntohs(*((uint16_t *)(&an_addr[4])));
623
624 bat_dbg(DBG_BLA, bat_priv,
625 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
626 vid, backbone_gw->orig, crc);
627
628 if (backbone_gw->crc != crc) {
629 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
630 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
631 backbone_gw->orig, backbone_gw->vid, backbone_gw->crc,
632 crc);
633
634 bla_send_request(backbone_gw);
635 } else {
636 /* if we have sent a request and the crc was OK,
637 * we can allow traffic again.
638 */
639 if (atomic_read(&backbone_gw->request_sent)) {
640 atomic_dec(&backbone_gw->bat_priv->bla_num_requests);
641 atomic_set(&backbone_gw->request_sent, 0);
642 }
643 }
644
645 backbone_gw_free_ref(backbone_gw);
646 return 1;
647}
648
649/* check for REQUEST frame, return 1 if handled */
650static int handle_request(struct bat_priv *bat_priv,
651 struct hard_iface *primary_if,
652 uint8_t *backbone_addr,
653 struct ethhdr *ethhdr, short vid)
654{
655 /* check for REQUEST frame */
656 if (!compare_eth(backbone_addr, ethhdr->h_dest))
657 return 0;
658
659 /* sanity check, this should not happen on a normal switch,
660 * we ignore it in this case.
661 */
662 if (!compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
663 return 1;
664
665 bat_dbg(DBG_BLA, bat_priv,
666 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
667 vid, ethhdr->h_source);
668
669 bla_answer_request(bat_priv, primary_if, vid);
670 return 1;
671}
672
673/* check for UNCLAIM frame, return 1 if handled */
674static int handle_unclaim(struct bat_priv *bat_priv,
675 struct hard_iface *primary_if,
676 uint8_t *backbone_addr,
677 uint8_t *claim_addr, short vid)
678{
679 struct backbone_gw *backbone_gw;
680
681 /* unclaim in any case if it is our own */
682 if (primary_if && compare_eth(backbone_addr,
683 primary_if->net_dev->dev_addr))
684 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_DEL);
685
686 backbone_gw = backbone_hash_find(bat_priv, backbone_addr, vid);
687
688 if (!backbone_gw)
689 return 1;
690
691 /* this must be an UNCLAIM frame */
692 bat_dbg(DBG_BLA, bat_priv,
693 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
694 claim_addr, vid, backbone_gw->orig);
695
696 bla_del_claim(bat_priv, claim_addr, vid);
697 backbone_gw_free_ref(backbone_gw);
698 return 1;
699}
700
701/* check for CLAIM frame, return 1 if handled */
702static int handle_claim(struct bat_priv *bat_priv,
703 struct hard_iface *primary_if, uint8_t *backbone_addr,
704 uint8_t *claim_addr, short vid)
705{
706 struct backbone_gw *backbone_gw;
707
708 /* register the gateway if not yet available, and add the claim. */
709
710 backbone_gw = bla_get_backbone_gw(bat_priv, backbone_addr, vid);
711
712 if (unlikely(!backbone_gw))
713 return 1;
714
715 /* this must be a CLAIM frame */
716 bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
717 if (compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
718 bla_send_claim(bat_priv, claim_addr, vid, CLAIM_TYPE_ADD);
719
720 /* TODO: we could call something like tt_local_del() here. */
721
722 backbone_gw_free_ref(backbone_gw);
723 return 1;
724}
725
726/**
727 * @bat_priv: the bat priv with all the soft interface information
728 * @skb: the frame to be checked
729 *
730 * Check if this is a claim frame, and process it accordingly.
731 *
732 * returns 1 if it was a claim frame, otherwise return 0 to
733 * tell the callee that it can use the frame on its own.
734 */
735static int bla_process_claim(struct bat_priv *bat_priv,
736 struct hard_iface *primary_if,
737 struct sk_buff *skb)
738{
739 struct ethhdr *ethhdr;
740 struct vlan_ethhdr *vhdr;
741 struct arphdr *arphdr;
742 uint8_t *hw_src, *hw_dst;
743 struct bla_claim_dst *bla_dst;
744 uint16_t proto;
745 int headlen;
746 short vid = -1;
747
748 ethhdr = (struct ethhdr *)skb_mac_header(skb);
749
750 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
751 vhdr = (struct vlan_ethhdr *)ethhdr;
752 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
753 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
754 headlen = sizeof(*vhdr);
755 } else {
756 proto = ntohs(ethhdr->h_proto);
757 headlen = sizeof(*ethhdr);
758 }
759
760 if (proto != ETH_P_ARP)
761 return 0; /* not a claim frame */
762
763 /* this must be a ARP frame. check if it is a claim. */
764
765 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
766 return 0;
767
768 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
769 ethhdr = (struct ethhdr *)skb_mac_header(skb);
770 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
771
772 /* Check whether the ARP frame carries a valid
773 * IP information
774 */
775
776 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
777 return 0;
778 if (arphdr->ar_pro != htons(ETH_P_IP))
779 return 0;
780 if (arphdr->ar_hln != ETH_ALEN)
781 return 0;
782 if (arphdr->ar_pln != 4)
783 return 0;
784
785 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
786 hw_dst = hw_src + ETH_ALEN + 4;
787 bla_dst = (struct bla_claim_dst *)hw_dst;
788
789 /* check if it is a claim frame. */
790 if (memcmp(hw_dst, claim_dest, 3) != 0)
791 return 0;
792
793 /* become a backbone gw ourselves on this vlan if not happened yet */
794 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
795
796 /* check for the different types of claim frames ... */
797 switch (bla_dst->type) {
798 case CLAIM_TYPE_ADD:
799 if (handle_claim(bat_priv, primary_if, hw_src,
800 ethhdr->h_source, vid))
801 return 1;
802 break;
803 case CLAIM_TYPE_DEL:
804 if (handle_unclaim(bat_priv, primary_if,
805 ethhdr->h_source, hw_src, vid))
806 return 1;
807 break;
808
809 case CLAIM_TYPE_ANNOUNCE:
810 if (handle_announce(bat_priv, hw_src, ethhdr->h_source, vid))
811 return 1;
812 break;
813 case CLAIM_TYPE_REQUEST:
814 if (handle_request(bat_priv, primary_if, hw_src, ethhdr, vid))
815 return 1;
816 break;
817 }
818
819 bat_dbg(DBG_BLA, bat_priv,
820 "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",
821 ethhdr->h_source, vid, hw_src, hw_dst);
822 return 1;
823}
824
825/* Check when we last heard from other nodes, and remove them in case of
826 * a time out, or clean all backbone gws if now is set.
827 */
828static void bla_purge_backbone_gw(struct bat_priv *bat_priv, int now)
829{
830 struct backbone_gw *backbone_gw;
831 struct hlist_node *node, *node_tmp;
832 struct hlist_head *head;
833 struct hashtable_t *hash;
834 spinlock_t *list_lock; /* protects write access to the hash lists */
835 int i;
836
837 hash = bat_priv->backbone_hash;
838 if (!hash)
839 return;
840
841 for (i = 0; i < hash->size; i++) {
842 head = &hash->table[i];
843 list_lock = &hash->list_locks[i];
844
845 spin_lock_bh(list_lock);
846 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
847 head, hash_entry) {
848 if (now)
849 goto purge_now;
850 if (!has_timed_out(backbone_gw->lasttime,
851 BLA_BACKBONE_TIMEOUT))
852 continue;
853
854 bat_dbg(DBG_BLA, backbone_gw->bat_priv,
855 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
856 backbone_gw->orig);
857
858purge_now:
859 /* don't wait for the pending request anymore */
860 if (atomic_read(&backbone_gw->request_sent))
861 atomic_dec(&bat_priv->bla_num_requests);
862
863 bla_del_backbone_claims(backbone_gw);
864
865 hlist_del_rcu(node);
866 backbone_gw_free_ref(backbone_gw);
867 }
868 spin_unlock_bh(list_lock);
869 }
870}
871
872/**
873 * @bat_priv: the bat priv with all the soft interface information
874 * @primary_if: the selected primary interface, may be NULL if now is set
875 * @now: whether the whole hash shall be wiped now
876 *
877 * Check when we heard last time from our own claims, and remove them in case of
878 * a time out, or clean all claims if now is set
879 */
880static void bla_purge_claims(struct bat_priv *bat_priv,
881 struct hard_iface *primary_if, int now)
882{
883 struct claim *claim;
884 struct hlist_node *node;
885 struct hlist_head *head;
886 struct hashtable_t *hash;
887 int i;
888
889 hash = bat_priv->claim_hash;
890 if (!hash)
891 return;
892
893 for (i = 0; i < hash->size; i++) {
894 head = &hash->table[i];
895
896 rcu_read_lock();
897 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
898 if (now)
899 goto purge_now;
900 if (!compare_eth(claim->backbone_gw->orig,
901 primary_if->net_dev->dev_addr))
902 continue;
903 if (!has_timed_out(claim->lasttime,
904 BLA_CLAIM_TIMEOUT))
905 continue;
906
907 bat_dbg(DBG_BLA, bat_priv,
908 "bla_purge_claims(): %pM, vid %d, time out\n",
909 claim->addr, claim->vid);
910
911purge_now:
912 handle_unclaim(bat_priv, primary_if,
913 claim->backbone_gw->orig,
914 claim->addr, claim->vid);
915 }
916 rcu_read_unlock();
917 }
918}
919
920/**
921 * @bat_priv: the bat priv with all the soft interface information
922 * @primary_if: the new selected primary_if
923 * @oldif: the old primary interface, may be NULL
924 *
925 * Update the backbone gateways when the own orig address changes.
926 *
927 */
928void bla_update_orig_address(struct bat_priv *bat_priv,
929 struct hard_iface *primary_if,
930 struct hard_iface *oldif)
931{
932 struct backbone_gw *backbone_gw;
933 struct hlist_node *node;
934 struct hlist_head *head;
935 struct hashtable_t *hash;
936 int i;
937
938 if (!oldif) {
939 bla_purge_claims(bat_priv, NULL, 1);
940 bla_purge_backbone_gw(bat_priv, 1);
941 return;
942 }
943
944 hash = bat_priv->backbone_hash;
945 if (!hash)
946 return;
947
948 for (i = 0; i < hash->size; i++) {
949 head = &hash->table[i];
950
951 rcu_read_lock();
952 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
953 /* own orig still holds the old value. */
954 if (!compare_eth(backbone_gw->orig,
955 oldif->net_dev->dev_addr))
956 continue;
957
958 memcpy(backbone_gw->orig,
959 primary_if->net_dev->dev_addr, ETH_ALEN);
960 /* send an announce frame so others will ask for our
961 * claims and update their tables.
962 */
963 bla_send_announce(bat_priv, backbone_gw);
964 }
965 rcu_read_unlock();
966 }
967}
968
969
970
971/* (re)start the timer */
972static void bla_start_timer(struct bat_priv *bat_priv)
973{
974 INIT_DELAYED_WORK(&bat_priv->bla_work, bla_periodic_work);
975 queue_delayed_work(bat_event_workqueue, &bat_priv->bla_work,
976 msecs_to_jiffies(BLA_PERIOD_LENGTH));
977}
978
979/* periodic work to do:
980 * * purge structures when they are too old
981 * * send announcements
982 */
983static void bla_periodic_work(struct work_struct *work)
984{
985 struct delayed_work *delayed_work =
986 container_of(work, struct delayed_work, work);
987 struct bat_priv *bat_priv =
988 container_of(delayed_work, struct bat_priv, bla_work);
989 struct hlist_node *node;
990 struct hlist_head *head;
991 struct backbone_gw *backbone_gw;
992 struct hashtable_t *hash;
993 struct hard_iface *primary_if;
994 int i;
995
996 primary_if = primary_if_get_selected(bat_priv);
997 if (!primary_if)
998 goto out;
999
1000 bla_purge_claims(bat_priv, primary_if, 0);
1001 bla_purge_backbone_gw(bat_priv, 0);
1002
1003 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1004 goto out;
1005
1006 hash = bat_priv->backbone_hash;
1007 if (!hash)
1008 goto out;
1009
1010 for (i = 0; i < hash->size; i++) {
1011 head = &hash->table[i];
1012
1013 rcu_read_lock();
1014 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1015 if (!compare_eth(backbone_gw->orig,
1016 primary_if->net_dev->dev_addr))
1017 continue;
1018
1019 backbone_gw->lasttime = jiffies;
1020
1021 bla_send_announce(bat_priv, backbone_gw);
1022 }
1023 rcu_read_unlock();
1024 }
1025out:
1026 if (primary_if)
1027 hardif_free_ref(primary_if);
1028
1029 bla_start_timer(bat_priv);
1030}
1031
1032/* initialize all bla structures */
1033int bla_init(struct bat_priv *bat_priv)
1034{
1035 bat_dbg(DBG_BLA, bat_priv, "bla hash registering\n");
1036
1037 if (bat_priv->claim_hash)
1038 return 1;
1039
1040 bat_priv->claim_hash = hash_new(128);
1041 bat_priv->backbone_hash = hash_new(32);
1042
1043 if (!bat_priv->claim_hash || !bat_priv->backbone_hash)
1044 return -1;
1045
1046 bat_dbg(DBG_BLA, bat_priv, "bla hashes initialized\n");
1047
1048 bla_start_timer(bat_priv);
1049 return 1;
1050}
1051
1052/**
1053 * @skb: the frame to be checked
1054 * @orig_node: the orig_node of the frame
1055 * @hdr_size: maximum length of the frame
1056 *
1057 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1058 * if the orig_node is also a gateway on the soft interface, otherwise it
1059 * returns 0.
1060 *
1061 */
1062int bla_is_backbone_gw(struct sk_buff *skb,
1063 struct orig_node *orig_node, int hdr_size)
1064{
1065 struct ethhdr *ethhdr;
1066 struct vlan_ethhdr *vhdr;
1067 struct backbone_gw *backbone_gw;
1068 short vid = -1;
1069
1070 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1071 return 0;
1072
1073 /* first, find out the vid. */
1074 if (!pskb_may_pull(skb, hdr_size + sizeof(struct ethhdr)))
1075 return 0;
1076
1077 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1078
1079 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1080 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1081 return 0;
1082
1083 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1084 hdr_size);
1085 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1086 }
1087
1088 /* see if this originator is a backbone gw for this VLAN */
1089
1090 backbone_gw = backbone_hash_find(orig_node->bat_priv,
1091 orig_node->orig, vid);
1092 if (!backbone_gw)
1093 return 0;
1094
1095 backbone_gw_free_ref(backbone_gw);
1096 return 1;
1097}
1098
1099/* free all bla structures (for softinterface free or module unload) */
1100void bla_free(struct bat_priv *bat_priv)
1101{
1102 struct hard_iface *primary_if;
1103
1104 cancel_delayed_work_sync(&bat_priv->bla_work);
1105 primary_if = primary_if_get_selected(bat_priv);
1106
1107 if (bat_priv->claim_hash) {
1108 bla_purge_claims(bat_priv, primary_if, 1);
1109 hash_destroy(bat_priv->claim_hash);
1110 bat_priv->claim_hash = NULL;
1111 }
1112 if (bat_priv->backbone_hash) {
1113 bla_purge_backbone_gw(bat_priv, 1);
1114 hash_destroy(bat_priv->backbone_hash);
1115 bat_priv->backbone_hash = NULL;
1116 }
1117 if (primary_if)
1118 hardif_free_ref(primary_if);
1119}
1120
1121/**
1122 * @bat_priv: the bat priv with all the soft interface information
1123 * @skb: the frame to be checked
1124 * @vid: the VLAN ID of the frame
1125 *
1126 * bla_rx avoidance checks if:
1127 * * we have to race for a claim
1128 * * if the frame is allowed on the LAN
1129 *
1130 * in these cases, the skb is further handled by this function and
1131 * returns 1, otherwise it returns 0 and the caller shall further
1132 * process the skb.
1133 *
1134 */
1135int bla_rx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1136{
1137 struct ethhdr *ethhdr;
1138 struct claim search_claim, *claim = NULL;
1139 struct hard_iface *primary_if;
1140 int ret;
1141
1142 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1143
1144 primary_if = primary_if_get_selected(bat_priv);
1145 if (!primary_if)
1146 goto handled;
1147
1148 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1149 goto allow;
1150
1151
1152 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1153 /* don't allow broadcasts while requests are in flight */
1154 if (is_multicast_ether_addr(ethhdr->h_dest))
1155 goto handled;
1156
1157 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1158 search_claim.vid = vid;
1159 claim = claim_hash_find(bat_priv, &search_claim);
1160
1161 if (!claim) {
1162 /* possible optimization: race for a claim */
1163 /* No claim exists yet, claim it for us!
1164 */
1165 handle_claim(bat_priv, primary_if,
1166 primary_if->net_dev->dev_addr,
1167 ethhdr->h_source, vid);
1168 goto allow;
1169 }
1170
1171 /* if it is our own claim ... */
1172 if (compare_eth(claim->backbone_gw->orig,
1173 primary_if->net_dev->dev_addr)) {
1174 /* ... allow it in any case */
1175 claim->lasttime = jiffies;
1176 goto allow;
1177 }
1178
1179 /* if it is a broadcast ... */
1180 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1181 /* ... drop it. the responsible gateway is in charge. */
1182 goto handled;
1183 } else {
1184 /* seems the client considers us as its best gateway.
1185 * send a claim and update the claim table
1186 * immediately.
1187 */
1188 handle_claim(bat_priv, primary_if,
1189 primary_if->net_dev->dev_addr,
1190 ethhdr->h_source, vid);
1191 goto allow;
1192 }
1193allow:
1194 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1195 ret = 0;
1196 goto out;
1197
1198handled:
1199 kfree_skb(skb);
1200 ret = 1;
1201
1202out:
1203 if (primary_if)
1204 hardif_free_ref(primary_if);
1205 if (claim)
1206 claim_free_ref(claim);
1207 return ret;
1208}
1209
1210/**
1211 * @bat_priv: the bat priv with all the soft interface information
1212 * @skb: the frame to be checked
1213 * @vid: the VLAN ID of the frame
1214 *
1215 * bla_tx checks if:
1216 * * a claim was received which has to be processed
1217 * * the frame is allowed on the mesh
1218 *
1219 * in these cases, the skb is further handled by this function and
1220 * returns 1, otherwise it returns 0 and the caller shall further
1221 * process the skb.
1222 *
1223 */
1224int bla_tx(struct bat_priv *bat_priv, struct sk_buff *skb, short vid)
1225{
1226 struct ethhdr *ethhdr;
1227 struct claim search_claim, *claim = NULL;
1228 struct hard_iface *primary_if;
1229 int ret = 0;
1230
1231 primary_if = primary_if_get_selected(bat_priv);
1232 if (!primary_if)
1233 goto out;
1234
1235 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1236 goto allow;
1237
1238 /* in VLAN case, the mac header might not be set. */
1239 skb_reset_mac_header(skb);
1240
1241 if (bla_process_claim(bat_priv, primary_if, skb))
1242 goto handled;
1243
1244 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1245
1246 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1247 /* don't allow broadcasts while requests are in flight */
1248 if (is_multicast_ether_addr(ethhdr->h_dest))
1249 goto handled;
1250
1251 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1252 search_claim.vid = vid;
1253
1254 claim = claim_hash_find(bat_priv, &search_claim);
1255
1256 /* if no claim exists, allow it. */
1257 if (!claim)
1258 goto allow;
1259
1260 /* check if we are responsible. */
1261 if (compare_eth(claim->backbone_gw->orig,
1262 primary_if->net_dev->dev_addr)) {
1263 /* if yes, the client has roamed and we have
1264 * to unclaim it.
1265 */
1266 handle_unclaim(bat_priv, primary_if,
1267 primary_if->net_dev->dev_addr,
1268 ethhdr->h_source, vid);
1269 goto allow;
1270 }
1271
1272 /* check if it is a multicast/broadcast frame */
1273 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1274 /* drop it. the responsible gateway has forwarded it into
1275 * the backbone network.
1276 */
1277 goto handled;
1278 } else {
1279 /* we must allow it. at least if we are
1280 * responsible for the DESTINATION.
1281 */
1282 goto allow;
1283 }
1284allow:
1285 bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1286 ret = 0;
1287 goto out;
1288handled:
1289 ret = 1;
1290out:
1291 if (primary_if)
1292 hardif_free_ref(primary_if);
1293 if (claim)
1294 claim_free_ref(claim);
1295 return ret;
1296}