1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2011-2018 B.A.T.M.A.N. contributors:
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "distributed-arp-table.h"
22 #include <linux/atomic.h>
23 #include <linux/bitops.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
27 #include <linux/gfp.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_ether.h>
30 #include <linux/if_vlan.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/kref.h>
35 #include <linux/list.h>
36 #include <linux/netlink.h>
37 #include <linux/rculist.h>
38 #include <linux/rcupdate.h>
39 #include <linux/seq_file.h>
40 #include <linux/skbuff.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/stddef.h>
44 #include <linux/string.h>
45 #include <linux/workqueue.h>
47 #include <net/genetlink.h>
48 #include <net/netlink.h>
50 #include <uapi/linux/batman_adv.h>
52 #include "bridge_loop_avoidance.h"
53 #include "hard-interface.h"
57 #include "originator.h"
59 #include "soft-interface.h"
60 #include "translation-table.h"
63 static void batadv_dat_purge(struct work_struct *work);
66 * batadv_dat_start_timer() - initialise the DAT periodic worker
67 * @bat_priv: the bat priv with all the soft interface information
69 static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
71 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
72 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
73 msecs_to_jiffies(10000));
77 * batadv_dat_entry_release() - release dat_entry from lists and queue for free
78 * after rcu grace period
79 * @ref: kref pointer of the dat_entry
81 static void batadv_dat_entry_release(struct kref *ref)
83 struct batadv_dat_entry *dat_entry;
85 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
87 kfree_rcu(dat_entry, rcu);
91 * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
93 * @dat_entry: dat_entry to be free'd
95 static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
97 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
101 * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
102 * @dat_entry: the entry to check
104 * Return: true if the entry has to be purged now, false otherwise.
106 static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
108 return batadv_has_timed_out(dat_entry->last_update,
109 BATADV_DAT_ENTRY_TIMEOUT);
113 * __batadv_dat_purge() - delete entries from the DAT local storage
114 * @bat_priv: the bat priv with all the soft interface information
115 * @to_purge: function in charge to decide whether an entry has to be purged or
116 * not. This function takes the dat_entry as argument and has to
117 * returns a boolean value: true is the entry has to be deleted,
120 * Loops over each entry in the DAT local storage and deletes it if and only if
121 * the to_purge function passed as argument returns true.
123 static void __batadv_dat_purge(struct batadv_priv *bat_priv,
124 bool (*to_purge)(struct batadv_dat_entry *))
126 spinlock_t *list_lock; /* protects write access to the hash lists */
127 struct batadv_dat_entry *dat_entry;
128 struct hlist_node *node_tmp;
129 struct hlist_head *head;
132 if (!bat_priv->dat.hash)
135 for (i = 0; i < bat_priv->dat.hash->size; i++) {
136 head = &bat_priv->dat.hash->table[i];
137 list_lock = &bat_priv->dat.hash->list_locks[i];
139 spin_lock_bh(list_lock);
140 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
142 /* if a helper function has been passed as parameter,
143 * ask it if the entry has to be purged or not
145 if (to_purge && !to_purge(dat_entry))
148 hlist_del_rcu(&dat_entry->hash_entry);
149 batadv_dat_entry_put(dat_entry);
151 spin_unlock_bh(list_lock);
156 * batadv_dat_purge() - periodic task that deletes old entries from the local
158 * @work: kernel work struct
160 static void batadv_dat_purge(struct work_struct *work)
162 struct delayed_work *delayed_work;
163 struct batadv_priv_dat *priv_dat;
164 struct batadv_priv *bat_priv;
166 delayed_work = to_delayed_work(work);
167 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
168 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
170 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
171 batadv_dat_start_timer(bat_priv);
175 * batadv_compare_dat() - comparing function used in the local DAT hash table
176 * @node: node in the local table
177 * @data2: second object to compare the node to
179 * Return: true if the two entries are the same, false otherwise.
181 static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
183 const void *data1 = container_of(node, struct batadv_dat_entry,
186 return memcmp(data1, data2, sizeof(__be32)) == 0;
190 * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
192 * @hdr_size: size of the possible header before the ARP packet
194 * Return: the value of the hw_src field in the ARP packet.
196 static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
200 addr = (u8 *)(skb->data + hdr_size);
201 addr += ETH_HLEN + sizeof(struct arphdr);
207 * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
209 * @hdr_size: size of the possible header before the ARP packet
211 * Return: the value of the ip_src field in the ARP packet.
213 static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
215 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
219 * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
221 * @hdr_size: size of the possible header before the ARP packet
223 * Return: the value of the hw_dst field in the ARP packet.
225 static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
227 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
231 * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
233 * @hdr_size: size of the possible header before the ARP packet
235 * Return: the value of the ip_dst field in the ARP packet.
237 static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
239 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
243 * batadv_hash_dat() - compute the hash value for an IP address
244 * @data: data to hash
245 * @size: size of the hash table
247 * Return: the selected index in the hash table for the given data.
249 static u32 batadv_hash_dat(const void *data, u32 size)
252 const struct batadv_dat_entry *dat = data;
253 const unsigned char *key;
256 key = (const unsigned char *)&dat->ip;
257 for (i = 0; i < sizeof(dat->ip); i++) {
259 hash += (hash << 10);
263 key = (const unsigned char *)&dat->vid;
264 for (i = 0; i < sizeof(dat->vid); i++) {
266 hash += (hash << 10);
271 hash ^= (hash >> 11);
272 hash += (hash << 15);
278 * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
280 * @bat_priv: the bat priv with all the soft interface information
282 * @vid: VLAN identifier
284 * Return: the dat_entry if found, NULL otherwise.
286 static struct batadv_dat_entry *
287 batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
290 struct hlist_head *head;
291 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
292 struct batadv_hashtable *hash = bat_priv->dat.hash;
301 index = batadv_hash_dat(&to_find, hash->size);
302 head = &hash->table[index];
305 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
306 if (dat_entry->ip != ip)
309 if (!kref_get_unless_zero(&dat_entry->refcount))
312 dat_entry_tmp = dat_entry;
317 return dat_entry_tmp;
321 * batadv_dat_entry_add() - add a new dat entry or update it if already exists
322 * @bat_priv: the bat priv with all the soft interface information
323 * @ip: ipv4 to add/edit
324 * @mac_addr: mac address to assign to the given ipv4
325 * @vid: VLAN identifier
327 static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
328 u8 *mac_addr, unsigned short vid)
330 struct batadv_dat_entry *dat_entry;
333 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
334 /* if this entry is already known, just update it */
336 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
337 ether_addr_copy(dat_entry->mac_addr, mac_addr);
338 dat_entry->last_update = jiffies;
339 batadv_dbg(BATADV_DBG_DAT, bat_priv,
340 "Entry updated: %pI4 %pM (vid: %d)\n",
341 &dat_entry->ip, dat_entry->mac_addr,
342 batadv_print_vid(vid));
346 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
351 dat_entry->vid = vid;
352 ether_addr_copy(dat_entry->mac_addr, mac_addr);
353 dat_entry->last_update = jiffies;
354 kref_init(&dat_entry->refcount);
356 kref_get(&dat_entry->refcount);
357 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
358 batadv_hash_dat, dat_entry,
359 &dat_entry->hash_entry);
361 if (unlikely(hash_added != 0)) {
362 /* remove the reference for the hash */
363 batadv_dat_entry_put(dat_entry);
367 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
368 &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
372 batadv_dat_entry_put(dat_entry);
375 #ifdef CONFIG_BATMAN_ADV_DEBUG
378 * batadv_dbg_arp() - print a debug message containing all the ARP packet
380 * @bat_priv: the bat priv with all the soft interface information
382 * @hdr_size: size of the possible header before the ARP packet
383 * @msg: message to print together with the debugging information
385 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
386 int hdr_size, char *msg)
388 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
389 struct batadv_bcast_packet *bcast_pkt;
391 __be32 ip_src, ip_dst;
394 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
396 ip_src = batadv_arp_ip_src(skb, hdr_size);
397 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
398 batadv_dbg(BATADV_DBG_DAT, bat_priv,
399 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
400 batadv_arp_hw_src(skb, hdr_size), &ip_src,
401 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
403 if (hdr_size < sizeof(struct batadv_unicast_packet))
406 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
408 switch (unicast_4addr_packet->u.packet_type) {
410 batadv_dbg(BATADV_DBG_DAT, bat_priv,
411 "* encapsulated within a UNICAST packet\n");
413 case BATADV_UNICAST_4ADDR:
414 batadv_dbg(BATADV_DBG_DAT, bat_priv,
415 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
416 unicast_4addr_packet->src);
417 switch (unicast_4addr_packet->subtype) {
418 case BATADV_P_DAT_DHT_PUT:
419 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
421 case BATADV_P_DAT_DHT_GET:
422 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
424 case BATADV_P_DAT_CACHE_REPLY:
425 batadv_dbg(BATADV_DBG_DAT, bat_priv,
426 "* type: DAT_CACHE_REPLY\n");
429 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
432 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
433 unicast_4addr_packet->u.packet_type);
437 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
438 orig_addr = bcast_pkt->orig;
439 batadv_dbg(BATADV_DBG_DAT, bat_priv,
440 "* encapsulated within a BCAST packet (src: %pM)\n",
444 batadv_dbg(BATADV_DBG_DAT, bat_priv,
445 "* encapsulated within an unknown packet type (0x%x)\n",
446 unicast_4addr_packet->u.packet_type);
452 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
453 int hdr_size, char *msg)
457 #endif /* CONFIG_BATMAN_ADV_DEBUG */
460 * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
461 * @res: the array with the already selected candidates
462 * @select: number of already selected candidates
463 * @tmp_max: address of the currently evaluated node
464 * @max: current round max address
465 * @last_max: address of the last selected candidate
466 * @candidate: orig_node under evaluation
467 * @max_orig_node: last selected candidate
469 * Return: true if the node has been elected as next candidate or false
472 static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
473 int select, batadv_dat_addr_t tmp_max,
474 batadv_dat_addr_t max,
475 batadv_dat_addr_t last_max,
476 struct batadv_orig_node *candidate,
477 struct batadv_orig_node *max_orig_node)
482 /* check if orig node candidate is running DAT */
483 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
486 /* Check if this node has already been selected... */
487 for (j = 0; j < select; j++)
488 if (res[j].orig_node == candidate)
490 /* ..and possibly skip it */
493 /* sanity check: has it already been selected? This should not happen */
494 if (tmp_max > last_max)
496 /* check if during this iteration an originator with a closer dht
497 * address has already been found
501 /* this is an hash collision with the temporary selected node. Choose
502 * the one with the lowest address
504 if (tmp_max == max && max_orig_node &&
505 batadv_compare_eth(candidate->orig, max_orig_node->orig))
514 * batadv_choose_next_candidate() - select the next DHT candidate
515 * @bat_priv: the bat priv with all the soft interface information
516 * @cands: candidates array
517 * @select: number of candidates already present in the array
518 * @ip_key: key to look up in the DHT
519 * @last_max: pointer where the address of the selected candidate will be saved
521 static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
522 struct batadv_dat_candidate *cands,
523 int select, batadv_dat_addr_t ip_key,
524 batadv_dat_addr_t *last_max)
526 batadv_dat_addr_t max = 0;
527 batadv_dat_addr_t tmp_max = 0;
528 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
529 struct batadv_hashtable *hash = bat_priv->orig_hash;
530 struct hlist_head *head;
533 /* if no node is eligible as candidate, leave the candidate type as
536 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
538 /* iterate over the originator list and find the node with the closest
539 * dat_address which has not been selected yet
541 for (i = 0; i < hash->size; i++) {
542 head = &hash->table[i];
545 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
546 /* the dht space is a ring using unsigned addresses */
547 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
550 if (!batadv_is_orig_node_eligible(cands, select,
552 *last_max, orig_node,
556 if (!kref_get_unless_zero(&orig_node->refcount))
561 batadv_orig_node_put(max_orig_node);
562 max_orig_node = orig_node;
567 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
568 cands[select].orig_node = max_orig_node;
569 batadv_dbg(BATADV_DBG_DAT, bat_priv,
570 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
571 select, max_orig_node->orig, max_orig_node->dat_addr,
578 * batadv_dat_select_candidates() - select the nodes which the DHT message has
580 * @bat_priv: the bat priv with all the soft interface information
581 * @ip_dst: ipv4 to look up in the DHT
582 * @vid: VLAN identifier
584 * An originator O is selected if and only if its DHT_ID value is one of three
585 * closest values (from the LEFT, with wrap around if needed) then the hash
586 * value of the key. ip_dst is the key.
588 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
590 static struct batadv_dat_candidate *
591 batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
595 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
596 struct batadv_dat_candidate *res;
597 struct batadv_dat_entry dat;
599 if (!bat_priv->orig_hash)
602 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
609 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
610 BATADV_DAT_ADDR_MAX);
612 batadv_dbg(BATADV_DBG_DAT, bat_priv,
613 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
616 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
617 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
624 * batadv_dat_send_data() - send a payload to the selected candidates
625 * @bat_priv: the bat priv with all the soft interface information
626 * @skb: payload to send
628 * @vid: VLAN identifier
629 * @packet_subtype: unicast4addr packet subtype to use
631 * This function copies the skb with pskb_copy() and is sent as unicast packet
632 * to each of the selected candidates.
634 * Return: true if the packet is sent to at least one candidate, false
637 static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
638 struct sk_buff *skb, __be32 ip,
639 unsigned short vid, int packet_subtype)
644 struct batadv_neigh_node *neigh_node = NULL;
645 struct sk_buff *tmp_skb;
646 struct batadv_dat_candidate *cand;
648 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
652 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
654 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
655 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
658 neigh_node = batadv_orig_router_get(cand[i].orig_node,
663 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
664 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
671 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
672 if (send_status == NET_XMIT_SUCCESS) {
673 /* count the sent packet */
674 switch (packet_subtype) {
675 case BATADV_P_DAT_DHT_GET:
676 batadv_inc_counter(bat_priv,
677 BATADV_CNT_DAT_GET_TX);
679 case BATADV_P_DAT_DHT_PUT:
680 batadv_inc_counter(bat_priv,
681 BATADV_CNT_DAT_PUT_TX);
685 /* packet sent to a candidate: return true */
689 batadv_neigh_node_put(neigh_node);
691 batadv_orig_node_put(cand[i].orig_node);
700 * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
702 * @bat_priv: the bat priv with all the soft interface information
704 static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
708 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
712 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
715 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
722 * batadv_dat_status_update() - update the dat tvlv container after dat
724 * @net_dev: the soft interface net device
726 void batadv_dat_status_update(struct net_device *net_dev)
728 struct batadv_priv *bat_priv = netdev_priv(net_dev);
730 batadv_dat_tvlv_container_update(bat_priv);
734 * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
735 * @bat_priv: the bat priv with all the soft interface information
736 * @orig: the orig_node of the ogm
737 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
738 * @tvlv_value: tvlv buffer containing the gateway data
739 * @tvlv_value_len: tvlv buffer length
741 static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
742 struct batadv_orig_node *orig,
744 void *tvlv_value, u16 tvlv_value_len)
746 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
747 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
749 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
753 * batadv_dat_hash_free() - free the local DAT hash table
754 * @bat_priv: the bat priv with all the soft interface information
756 static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
758 if (!bat_priv->dat.hash)
761 __batadv_dat_purge(bat_priv, NULL);
763 batadv_hash_destroy(bat_priv->dat.hash);
765 bat_priv->dat.hash = NULL;
769 * batadv_dat_init() - initialise the DAT internals
770 * @bat_priv: the bat priv with all the soft interface information
772 * Return: 0 in case of success, a negative error code otherwise
774 int batadv_dat_init(struct batadv_priv *bat_priv)
776 if (bat_priv->dat.hash)
779 bat_priv->dat.hash = batadv_hash_new(1024);
781 if (!bat_priv->dat.hash)
784 batadv_dat_start_timer(bat_priv);
786 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
787 NULL, BATADV_TVLV_DAT, 1,
788 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
789 batadv_dat_tvlv_container_update(bat_priv);
794 * batadv_dat_free() - free the DAT internals
795 * @bat_priv: the bat priv with all the soft interface information
797 void batadv_dat_free(struct batadv_priv *bat_priv)
799 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
800 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
802 cancel_delayed_work_sync(&bat_priv->dat.work);
804 batadv_dat_hash_free(bat_priv);
807 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
809 * batadv_dat_cache_seq_print_text() - print the local DAT hash table
810 * @seq: seq file to print on
815 int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
817 struct net_device *net_dev = (struct net_device *)seq->private;
818 struct batadv_priv *bat_priv = netdev_priv(net_dev);
819 struct batadv_hashtable *hash = bat_priv->dat.hash;
820 struct batadv_dat_entry *dat_entry;
821 struct batadv_hard_iface *primary_if;
822 struct hlist_head *head;
823 unsigned long last_seen_jiffies;
824 int last_seen_msecs, last_seen_secs, last_seen_mins;
827 primary_if = batadv_seq_print_text_primary_if_get(seq);
831 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
833 " IPv4 MAC VID last-seen\n");
835 for (i = 0; i < hash->size; i++) {
836 head = &hash->table[i];
839 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
840 last_seen_jiffies = jiffies - dat_entry->last_update;
841 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
842 last_seen_mins = last_seen_msecs / 60000;
843 last_seen_msecs = last_seen_msecs % 60000;
844 last_seen_secs = last_seen_msecs / 1000;
846 seq_printf(seq, " * %15pI4 %pM %4i %6i:%02i\n",
847 &dat_entry->ip, dat_entry->mac_addr,
848 batadv_print_vid(dat_entry->vid),
849 last_seen_mins, last_seen_secs);
856 batadv_hardif_put(primary_if);
862 * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
864 * @msg: buffer for the message
865 * @portid: netlink port
866 * @seq: Sequence number of netlink message
867 * @dat_entry: entry to dump
869 * Return: 0 or error code.
872 batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
873 struct batadv_dat_entry *dat_entry)
878 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
879 NLM_F_MULTI, BATADV_CMD_GET_DAT_CACHE);
883 msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
885 if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
887 nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
888 dat_entry->mac_addr) ||
889 nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
890 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
891 genlmsg_cancel(msg, hdr);
895 genlmsg_end(msg, hdr);
900 * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
902 * @msg: buffer for the message
903 * @portid: netlink port
904 * @seq: Sequence number of netlink message
905 * @head: bucket to dump
906 * @idx_skip: How many entries to skip
908 * Return: 0 or error code.
911 batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
912 struct hlist_head *head, int *idx_skip)
914 struct batadv_dat_entry *dat_entry;
918 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
922 if (batadv_dat_cache_dump_entry(msg, portid, seq,
939 * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
940 * @msg: buffer for the message
941 * @cb: callback structure containing arguments
943 * Return: message length.
945 int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
947 struct batadv_hard_iface *primary_if = NULL;
948 int portid = NETLINK_CB(cb->skb).portid;
949 struct net *net = sock_net(cb->skb->sk);
950 struct net_device *soft_iface;
951 struct batadv_hashtable *hash;
952 struct batadv_priv *bat_priv;
953 int bucket = cb->args[0];
954 struct hlist_head *head;
955 int idx = cb->args[1];
959 ifindex = batadv_netlink_get_ifindex(cb->nlh,
960 BATADV_ATTR_MESH_IFINDEX);
964 soft_iface = dev_get_by_index(net, ifindex);
965 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
970 bat_priv = netdev_priv(soft_iface);
971 hash = bat_priv->dat.hash;
973 primary_if = batadv_primary_if_get_selected(bat_priv);
974 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
979 while (bucket < hash->size) {
980 head = &hash->table[bucket];
982 if (batadv_dat_cache_dump_bucket(msg, portid,
983 cb->nlh->nlmsg_seq, head,
991 cb->args[0] = bucket;
998 batadv_hardif_put(primary_if);
1001 dev_put(soft_iface);
1007 * batadv_arp_get_type() - parse an ARP packet and gets the type
1008 * @bat_priv: the bat priv with all the soft interface information
1009 * @skb: packet to analyse
1010 * @hdr_size: size of the possible header before the ARP packet in the skb
1012 * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
1014 static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1015 struct sk_buff *skb, int hdr_size)
1017 struct arphdr *arphdr;
1018 struct ethhdr *ethhdr;
1019 __be32 ip_src, ip_dst;
1020 u8 *hw_src, *hw_dst;
1023 /* pull the ethernet header */
1024 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1027 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1029 if (ethhdr->h_proto != htons(ETH_P_ARP))
1032 /* pull the ARP payload */
1033 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1034 arp_hdr_len(skb->dev))))
1037 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1039 /* check whether the ARP packet carries a valid IP information */
1040 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1043 if (arphdr->ar_pro != htons(ETH_P_IP))
1046 if (arphdr->ar_hln != ETH_ALEN)
1049 if (arphdr->ar_pln != 4)
1052 /* Check for bad reply/request. If the ARP message is not sane, DAT
1053 * will simply ignore it
1055 ip_src = batadv_arp_ip_src(skb, hdr_size);
1056 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1057 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
1058 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
1059 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
1060 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
1063 hw_src = batadv_arp_hw_src(skb, hdr_size);
1064 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1067 /* don't care about the destination MAC address in ARP requests */
1068 if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
1069 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1070 if (is_zero_ether_addr(hw_dst) ||
1071 is_multicast_ether_addr(hw_dst))
1075 type = ntohs(arphdr->ar_op);
1081 * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
1082 * @skb: the buffer containing the packet to extract the VID from
1083 * @hdr_size: the size of the batman-adv header encapsulating the packet
1085 * Return: If the packet embedded in the skb is vlan tagged this function
1086 * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1089 static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1093 vid = batadv_get_vid(skb, *hdr_size);
1095 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1096 * If the header contained in the packet is a VLAN one (which is longer)
1097 * hdr_size is updated so that the functions will still skip the
1098 * correct amount of bytes.
1100 if (vid & BATADV_VLAN_HAS_TAG)
1101 *hdr_size += VLAN_HLEN;
1107 * batadv_dat_arp_create_reply() - create an ARP Reply
1108 * @bat_priv: the bat priv with all the soft interface information
1109 * @ip_src: ARP sender IP
1110 * @ip_dst: ARP target IP
1111 * @hw_src: Ethernet source and ARP sender MAC
1112 * @hw_dst: Ethernet destination and ARP target MAC
1113 * @vid: VLAN identifier (optional, set to zero otherwise)
1115 * Creates an ARP Reply from the given values, optionally encapsulated in a
1118 * Return: An skb containing an ARP Reply.
1120 static struct sk_buff *
1121 batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1122 __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1125 struct sk_buff *skb;
1127 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
1128 ip_src, hw_dst, hw_src, hw_dst);
1132 skb_reset_mac_header(skb);
1134 if (vid & BATADV_VLAN_HAS_TAG)
1135 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1136 vid & VLAN_VID_MASK);
1142 * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
1144 * @bat_priv: the bat priv with all the soft interface information
1145 * @skb: packet to check
1147 * Return: true if the message has been sent to the dht candidates, false
1148 * otherwise. In case of a positive return value the message has to be enqueued
1149 * to permit the fallback.
1151 bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1152 struct sk_buff *skb)
1155 __be32 ip_dst, ip_src;
1158 struct batadv_dat_entry *dat_entry = NULL;
1159 struct sk_buff *skb_new;
1160 struct net_device *soft_iface = bat_priv->soft_iface;
1164 if (!atomic_read(&bat_priv->distributed_arp_table))
1167 vid = batadv_dat_get_vid(skb, &hdr_size);
1169 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1170 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1171 * message to the selected DHT candidates
1173 if (type != ARPOP_REQUEST)
1176 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
1178 ip_src = batadv_arp_ip_src(skb, hdr_size);
1179 hw_src = batadv_arp_hw_src(skb, hdr_size);
1180 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1182 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1184 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1186 /* If the ARP request is destined for a local client the local
1187 * client will answer itself. DAT would only generate a
1190 * Moreover, if the soft-interface is enslaved into a bridge, an
1191 * additional DAT answer may trigger kernel warnings about
1192 * a packet coming from the wrong port.
1194 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
1199 /* If BLA is enabled, only send ARP replies if we have claimed
1200 * the destination for the ARP request or if no one else of
1201 * the backbone gws belonging to our backbone has claimed the
1204 if (!batadv_bla_check_claim(bat_priv,
1205 dat_entry->mac_addr, vid)) {
1206 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1207 "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1208 dat_entry->mac_addr);
1213 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1214 dat_entry->mac_addr,
1219 skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1221 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1222 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1223 skb->len + ETH_HLEN + hdr_size);
1226 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1229 /* Send the request to the DHT */
1230 ret = batadv_dat_send_data(bat_priv, skb, ip_dst, vid,
1231 BATADV_P_DAT_DHT_GET);
1235 batadv_dat_entry_put(dat_entry);
1240 * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
1241 * answer using the local DAT storage
1242 * @bat_priv: the bat priv with all the soft interface information
1243 * @skb: packet to check
1244 * @hdr_size: size of the encapsulation header
1246 * Return: true if the request has been answered, false otherwise.
1248 bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1249 struct sk_buff *skb, int hdr_size)
1252 __be32 ip_src, ip_dst;
1254 struct sk_buff *skb_new;
1255 struct batadv_dat_entry *dat_entry = NULL;
1260 if (!atomic_read(&bat_priv->distributed_arp_table))
1263 vid = batadv_dat_get_vid(skb, &hdr_size);
1265 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1266 if (type != ARPOP_REQUEST)
1269 hw_src = batadv_arp_hw_src(skb, hdr_size);
1270 ip_src = batadv_arp_ip_src(skb, hdr_size);
1271 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1273 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
1275 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1277 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1281 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1282 dat_entry->mac_addr, hw_src, vid);
1286 /* To preserve backwards compatibility, the node has choose the outgoing
1287 * format based on the incoming request packet type. The assumption is
1288 * that a node not using the 4addr packet format doesn't support it.
1290 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
1291 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1292 BATADV_P_DAT_CACHE_REPLY,
1295 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
1297 if (err != NET_XMIT_DROP) {
1298 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
1303 batadv_dat_entry_put(dat_entry);
1310 * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
1311 * @bat_priv: the bat priv with all the soft interface information
1312 * @skb: packet to check
1314 void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1315 struct sk_buff *skb)
1318 __be32 ip_src, ip_dst;
1319 u8 *hw_src, *hw_dst;
1323 if (!atomic_read(&bat_priv->distributed_arp_table))
1326 vid = batadv_dat_get_vid(skb, &hdr_size);
1328 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1329 if (type != ARPOP_REPLY)
1332 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
1334 hw_src = batadv_arp_hw_src(skb, hdr_size);
1335 ip_src = batadv_arp_ip_src(skb, hdr_size);
1336 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1337 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1339 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1340 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1342 /* Send the ARP reply to the candidates for both the IP addresses that
1343 * the node obtained from the ARP reply
1345 batadv_dat_send_data(bat_priv, skb, ip_src, vid, BATADV_P_DAT_DHT_PUT);
1346 batadv_dat_send_data(bat_priv, skb, ip_dst, vid, BATADV_P_DAT_DHT_PUT);
1350 * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1351 * local DAT storage only
1352 * @bat_priv: the bat priv with all the soft interface information
1353 * @skb: packet to check
1354 * @hdr_size: size of the encapsulation header
1356 * Return: true if the packet was snooped and consumed by DAT. False if the
1357 * packet has to be delivered to the interface
1359 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1360 struct sk_buff *skb, int hdr_size)
1362 struct batadv_dat_entry *dat_entry = NULL;
1364 __be32 ip_src, ip_dst;
1365 u8 *hw_src, *hw_dst;
1366 bool dropped = false;
1369 if (!atomic_read(&bat_priv->distributed_arp_table))
1372 vid = batadv_dat_get_vid(skb, &hdr_size);
1374 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1375 if (type != ARPOP_REPLY)
1378 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
1380 hw_src = batadv_arp_hw_src(skb, hdr_size);
1381 ip_src = batadv_arp_ip_src(skb, hdr_size);
1382 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1383 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1385 /* If ip_dst is already in cache and has the right mac address,
1386 * drop this frame if this ARP reply is destined for us because it's
1387 * most probably an ARP reply generated by another node of the DHT.
1388 * We have most probably received already a reply earlier. Delivering
1389 * this frame would lead to doubled receive of an ARP reply.
1391 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1392 if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
1393 batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1394 hw_src, &ip_src, hw_dst, &ip_dst,
1395 dat_entry->mac_addr, &dat_entry->ip);
1400 /* Update our internal cache with both the IP addresses the node got
1401 * within the ARP reply
1403 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1404 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1406 /* If BLA is enabled, only forward ARP replies if we have claimed the
1407 * source of the ARP reply or if no one else of the same backbone has
1408 * already claimed that client. This prevents that different gateways
1409 * to the same backbone all forward the ARP reply leading to multiple
1410 * replies in the backbone.
1412 if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1413 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1414 "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1420 /* if this REPLY is directed to a client of mine, let's deliver the
1421 * packet to the interface
1423 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1425 /* if this REPLY is sent on behalf of a client of mine, let's drop the
1426 * packet because the client will reply by itself
1428 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
1433 batadv_dat_entry_put(dat_entry);
1434 /* if dropped == false -> deliver to the interface */
1439 * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1440 * dropped (because the node has already obtained the reply via DAT) or not
1441 * @bat_priv: the bat priv with all the soft interface information
1442 * @forw_packet: the broadcast packet
1444 * Return: true if the node can drop the packet, false otherwise.
1446 bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1447 struct batadv_forw_packet *forw_packet)
1451 struct batadv_dat_entry *dat_entry = NULL;
1453 int hdr_size = sizeof(struct batadv_bcast_packet);
1456 if (!atomic_read(&bat_priv->distributed_arp_table))
1459 /* If this packet is an ARP_REQUEST and the node already has the
1460 * information that it is going to ask, then the packet can be dropped
1462 if (batadv_forw_packet_is_rebroadcast(forw_packet))
1465 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1467 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1468 if (type != ARPOP_REQUEST)
1471 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1472 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1473 /* check if the node already got this entry */
1475 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1476 "ARP Request for %pI4: fallback\n", &ip_dst);
1480 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1481 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1486 batadv_dat_entry_put(dat_entry);