1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
7 #include "distributed-arp-table.h"
10 #include <asm/unaligned.h>
11 #include <linux/atomic.h>
12 #include <linux/bitops.h>
13 #include <linux/byteorder/generic.h>
14 #include <linux/container_of.h>
15 #include <linux/errno.h>
16 #include <linux/etherdevice.h>
17 #include <linux/gfp.h>
18 #include <linux/if_arp.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_vlan.h>
23 #include <linux/jiffies.h>
24 #include <linux/kref.h>
25 #include <linux/list.h>
26 #include <linux/netlink.h>
27 #include <linux/rculist.h>
28 #include <linux/rcupdate.h>
29 #include <linux/skbuff.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/stddef.h>
33 #include <linux/string.h>
34 #include <linux/udp.h>
35 #include <linux/workqueue.h>
37 #include <net/genetlink.h>
38 #include <net/netlink.h>
40 #include <uapi/linux/batman_adv.h>
42 #include "bridge_loop_avoidance.h"
43 #include "hard-interface.h"
47 #include "originator.h"
49 #include "soft-interface.h"
50 #include "translation-table.h"
57 enum batadv_boothtype {
58 BATADV_HTYPE_ETHERNET = 1,
61 enum batadv_dhcpoptioncode {
62 BATADV_DHCP_OPT_PAD = 0,
63 BATADV_DHCP_OPT_MSG_TYPE = 53,
64 BATADV_DHCP_OPT_END = 255,
67 enum batadv_dhcptype {
71 /* { 99, 130, 83, 99 } */
72 #define BATADV_DHCP_MAGIC 1669485411
74 struct batadv_dhcp_packet {
93 #define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
94 #define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
96 static void batadv_dat_purge(struct work_struct *work);
99 * batadv_dat_start_timer() - initialise the DAT periodic worker
100 * @bat_priv: the bat priv with all the soft interface information
102 static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
104 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
105 msecs_to_jiffies(10000));
109 * batadv_dat_entry_release() - release dat_entry from lists and queue for free
110 * after rcu grace period
111 * @ref: kref pointer of the dat_entry
113 static void batadv_dat_entry_release(struct kref *ref)
115 struct batadv_dat_entry *dat_entry;
117 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
119 kfree_rcu(dat_entry, rcu);
123 * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
125 * @dat_entry: dat_entry to be free'd
127 static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
132 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
136 * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
137 * @dat_entry: the entry to check
139 * Return: true if the entry has to be purged now, false otherwise.
141 static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
143 return batadv_has_timed_out(dat_entry->last_update,
144 BATADV_DAT_ENTRY_TIMEOUT);
148 * __batadv_dat_purge() - delete entries from the DAT local storage
149 * @bat_priv: the bat priv with all the soft interface information
150 * @to_purge: function in charge to decide whether an entry has to be purged or
151 * not. This function takes the dat_entry as argument and has to
152 * returns a boolean value: true is the entry has to be deleted,
155 * Loops over each entry in the DAT local storage and deletes it if and only if
156 * the to_purge function passed as argument returns true.
158 static void __batadv_dat_purge(struct batadv_priv *bat_priv,
159 bool (*to_purge)(struct batadv_dat_entry *))
161 spinlock_t *list_lock; /* protects write access to the hash lists */
162 struct batadv_dat_entry *dat_entry;
163 struct hlist_node *node_tmp;
164 struct hlist_head *head;
167 if (!bat_priv->dat.hash)
170 for (i = 0; i < bat_priv->dat.hash->size; i++) {
171 head = &bat_priv->dat.hash->table[i];
172 list_lock = &bat_priv->dat.hash->list_locks[i];
174 spin_lock_bh(list_lock);
175 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
177 /* if a helper function has been passed as parameter,
178 * ask it if the entry has to be purged or not
180 if (to_purge && !to_purge(dat_entry))
183 hlist_del_rcu(&dat_entry->hash_entry);
184 batadv_dat_entry_put(dat_entry);
186 spin_unlock_bh(list_lock);
191 * batadv_dat_purge() - periodic task that deletes old entries from the local
193 * @work: kernel work struct
195 static void batadv_dat_purge(struct work_struct *work)
197 struct delayed_work *delayed_work;
198 struct batadv_priv_dat *priv_dat;
199 struct batadv_priv *bat_priv;
201 delayed_work = to_delayed_work(work);
202 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
203 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
205 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
206 batadv_dat_start_timer(bat_priv);
210 * batadv_compare_dat() - comparing function used in the local DAT hash table
211 * @node: node in the local table
212 * @data2: second object to compare the node to
214 * Return: true if the two entries are the same, false otherwise.
216 static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
218 const void *data1 = container_of(node, struct batadv_dat_entry,
221 return memcmp(data1, data2, sizeof(__be32)) == 0;
225 * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
227 * @hdr_size: size of the possible header before the ARP packet
229 * Return: the value of the hw_src field in the ARP packet.
231 static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
235 addr = (u8 *)(skb->data + hdr_size);
236 addr += ETH_HLEN + sizeof(struct arphdr);
242 * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
244 * @hdr_size: size of the possible header before the ARP packet
246 * Return: the value of the ip_src field in the ARP packet.
248 static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
250 return *(__force __be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
254 * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
256 * @hdr_size: size of the possible header before the ARP packet
258 * Return: the value of the hw_dst field in the ARP packet.
260 static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
262 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
266 * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
268 * @hdr_size: size of the possible header before the ARP packet
270 * Return: the value of the ip_dst field in the ARP packet.
272 static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
274 u8 *dst = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4;
276 return *(__force __be32 *)dst;
280 * batadv_hash_dat() - compute the hash value for an IP address
281 * @data: data to hash
282 * @size: size of the hash table
284 * Return: the selected index in the hash table for the given data.
286 static u32 batadv_hash_dat(const void *data, u32 size)
289 const struct batadv_dat_entry *dat = data;
290 const unsigned char *key;
294 key = (__force const unsigned char *)&dat->ip;
295 for (i = 0; i < sizeof(dat->ip); i++) {
297 hash += (hash << 10);
301 vid = htons(dat->vid);
302 key = (__force const unsigned char *)&vid;
303 for (i = 0; i < sizeof(dat->vid); i++) {
305 hash += (hash << 10);
310 hash ^= (hash >> 11);
311 hash += (hash << 15);
317 * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
319 * @bat_priv: the bat priv with all the soft interface information
321 * @vid: VLAN identifier
323 * Return: the dat_entry if found, NULL otherwise.
325 static struct batadv_dat_entry *
326 batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
329 struct hlist_head *head;
330 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
331 struct batadv_hashtable *hash = bat_priv->dat.hash;
340 index = batadv_hash_dat(&to_find, hash->size);
341 head = &hash->table[index];
344 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
345 if (dat_entry->ip != ip)
348 if (!kref_get_unless_zero(&dat_entry->refcount))
351 dat_entry_tmp = dat_entry;
356 return dat_entry_tmp;
360 * batadv_dat_entry_add() - add a new dat entry or update it if already exists
361 * @bat_priv: the bat priv with all the soft interface information
362 * @ip: ipv4 to add/edit
363 * @mac_addr: mac address to assign to the given ipv4
364 * @vid: VLAN identifier
366 static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
367 u8 *mac_addr, unsigned short vid)
369 struct batadv_dat_entry *dat_entry;
372 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
373 /* if this entry is already known, just update it */
375 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
376 ether_addr_copy(dat_entry->mac_addr, mac_addr);
377 dat_entry->last_update = jiffies;
378 batadv_dbg(BATADV_DBG_DAT, bat_priv,
379 "Entry updated: %pI4 %pM (vid: %d)\n",
380 &dat_entry->ip, dat_entry->mac_addr,
381 batadv_print_vid(vid));
385 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
390 dat_entry->vid = vid;
391 ether_addr_copy(dat_entry->mac_addr, mac_addr);
392 dat_entry->last_update = jiffies;
393 kref_init(&dat_entry->refcount);
395 kref_get(&dat_entry->refcount);
396 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
397 batadv_hash_dat, dat_entry,
398 &dat_entry->hash_entry);
400 if (unlikely(hash_added != 0)) {
401 /* remove the reference for the hash */
402 batadv_dat_entry_put(dat_entry);
406 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
407 &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
410 batadv_dat_entry_put(dat_entry);
413 #ifdef CONFIG_BATMAN_ADV_DEBUG
416 * batadv_dbg_arp() - print a debug message containing all the ARP packet
418 * @bat_priv: the bat priv with all the soft interface information
420 * @hdr_size: size of the possible header before the ARP packet
421 * @msg: message to print together with the debugging information
423 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
424 int hdr_size, char *msg)
426 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
427 struct batadv_bcast_packet *bcast_pkt;
429 __be32 ip_src, ip_dst;
432 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
434 ip_src = batadv_arp_ip_src(skb, hdr_size);
435 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
436 batadv_dbg(BATADV_DBG_DAT, bat_priv,
437 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
438 batadv_arp_hw_src(skb, hdr_size), &ip_src,
439 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
441 if (hdr_size < sizeof(struct batadv_unicast_packet))
444 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
446 switch (unicast_4addr_packet->u.packet_type) {
448 batadv_dbg(BATADV_DBG_DAT, bat_priv,
449 "* encapsulated within a UNICAST packet\n");
451 case BATADV_UNICAST_4ADDR:
452 batadv_dbg(BATADV_DBG_DAT, bat_priv,
453 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
454 unicast_4addr_packet->src);
455 switch (unicast_4addr_packet->subtype) {
456 case BATADV_P_DAT_DHT_PUT:
457 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
459 case BATADV_P_DAT_DHT_GET:
460 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
462 case BATADV_P_DAT_CACHE_REPLY:
463 batadv_dbg(BATADV_DBG_DAT, bat_priv,
464 "* type: DAT_CACHE_REPLY\n");
467 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
470 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
471 unicast_4addr_packet->u.packet_type);
475 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
476 orig_addr = bcast_pkt->orig;
477 batadv_dbg(BATADV_DBG_DAT, bat_priv,
478 "* encapsulated within a BCAST packet (src: %pM)\n",
482 batadv_dbg(BATADV_DBG_DAT, bat_priv,
483 "* encapsulated within an unknown packet type (0x%x)\n",
484 unicast_4addr_packet->u.packet_type);
490 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
491 int hdr_size, char *msg)
495 #endif /* CONFIG_BATMAN_ADV_DEBUG */
498 * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
499 * @res: the array with the already selected candidates
500 * @select: number of already selected candidates
501 * @tmp_max: address of the currently evaluated node
502 * @max: current round max address
503 * @last_max: address of the last selected candidate
504 * @candidate: orig_node under evaluation
505 * @max_orig_node: last selected candidate
507 * Return: true if the node has been elected as next candidate or false
510 static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
511 int select, batadv_dat_addr_t tmp_max,
512 batadv_dat_addr_t max,
513 batadv_dat_addr_t last_max,
514 struct batadv_orig_node *candidate,
515 struct batadv_orig_node *max_orig_node)
520 /* check if orig node candidate is running DAT */
521 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
524 /* Check if this node has already been selected... */
525 for (j = 0; j < select; j++)
526 if (res[j].orig_node == candidate)
528 /* ..and possibly skip it */
531 /* sanity check: has it already been selected? This should not happen */
532 if (tmp_max > last_max)
534 /* check if during this iteration an originator with a closer dht
535 * address has already been found
539 /* this is an hash collision with the temporary selected node. Choose
540 * the one with the lowest address
542 if (tmp_max == max && max_orig_node &&
543 batadv_compare_eth(candidate->orig, max_orig_node->orig))
552 * batadv_choose_next_candidate() - select the next DHT candidate
553 * @bat_priv: the bat priv with all the soft interface information
554 * @cands: candidates array
555 * @select: number of candidates already present in the array
556 * @ip_key: key to look up in the DHT
557 * @last_max: pointer where the address of the selected candidate will be saved
559 static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
560 struct batadv_dat_candidate *cands,
561 int select, batadv_dat_addr_t ip_key,
562 batadv_dat_addr_t *last_max)
564 batadv_dat_addr_t max = 0;
565 batadv_dat_addr_t tmp_max = 0;
566 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
567 struct batadv_hashtable *hash = bat_priv->orig_hash;
568 struct hlist_head *head;
571 /* if no node is eligible as candidate, leave the candidate type as
574 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
576 /* iterate over the originator list and find the node with the closest
577 * dat_address which has not been selected yet
579 for (i = 0; i < hash->size; i++) {
580 head = &hash->table[i];
583 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
584 /* the dht space is a ring using unsigned addresses */
585 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
588 if (!batadv_is_orig_node_eligible(cands, select,
590 *last_max, orig_node,
594 if (!kref_get_unless_zero(&orig_node->refcount))
598 batadv_orig_node_put(max_orig_node);
599 max_orig_node = orig_node;
604 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
605 cands[select].orig_node = max_orig_node;
606 batadv_dbg(BATADV_DBG_DAT, bat_priv,
607 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
608 select, max_orig_node->orig, max_orig_node->dat_addr,
615 * batadv_dat_select_candidates() - select the nodes which the DHT message has
617 * @bat_priv: the bat priv with all the soft interface information
618 * @ip_dst: ipv4 to look up in the DHT
619 * @vid: VLAN identifier
621 * An originator O is selected if and only if its DHT_ID value is one of three
622 * closest values (from the LEFT, with wrap around if needed) then the hash
623 * value of the key. ip_dst is the key.
625 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
627 static struct batadv_dat_candidate *
628 batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
632 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
633 struct batadv_dat_candidate *res;
634 struct batadv_dat_entry dat;
636 if (!bat_priv->orig_hash)
639 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
646 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
647 BATADV_DAT_ADDR_MAX);
649 batadv_dbg(BATADV_DBG_DAT, bat_priv,
650 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
653 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
654 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
661 * batadv_dat_forward_data() - copy and send payload to the selected candidates
662 * @bat_priv: the bat priv with all the soft interface information
663 * @skb: payload to send
665 * @vid: VLAN identifier
666 * @packet_subtype: unicast4addr packet subtype to use
668 * This function copies the skb with pskb_copy() and is sent as a unicast packet
669 * to each of the selected candidates.
671 * Return: true if the packet is sent to at least one candidate, false
674 static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
675 struct sk_buff *skb, __be32 ip,
676 unsigned short vid, int packet_subtype)
681 struct batadv_neigh_node *neigh_node = NULL;
682 struct sk_buff *tmp_skb;
683 struct batadv_dat_candidate *cand;
685 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
689 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
691 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
692 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
695 neigh_node = batadv_orig_router_get(cand[i].orig_node,
700 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
701 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
708 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
709 if (send_status == NET_XMIT_SUCCESS) {
710 /* count the sent packet */
711 switch (packet_subtype) {
712 case BATADV_P_DAT_DHT_GET:
713 batadv_inc_counter(bat_priv,
714 BATADV_CNT_DAT_GET_TX);
716 case BATADV_P_DAT_DHT_PUT:
717 batadv_inc_counter(bat_priv,
718 BATADV_CNT_DAT_PUT_TX);
722 /* packet sent to a candidate: return true */
726 batadv_neigh_node_put(neigh_node);
728 batadv_orig_node_put(cand[i].orig_node);
736 * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
738 * @bat_priv: the bat priv with all the soft interface information
740 static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
744 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
748 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
751 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
758 * batadv_dat_status_update() - update the dat tvlv container after dat
760 * @net_dev: the soft interface net device
762 void batadv_dat_status_update(struct net_device *net_dev)
764 struct batadv_priv *bat_priv = netdev_priv(net_dev);
766 batadv_dat_tvlv_container_update(bat_priv);
770 * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
771 * @bat_priv: the bat priv with all the soft interface information
772 * @orig: the orig_node of the ogm
773 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
774 * @tvlv_value: tvlv buffer containing the gateway data
775 * @tvlv_value_len: tvlv buffer length
777 static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
778 struct batadv_orig_node *orig,
780 void *tvlv_value, u16 tvlv_value_len)
782 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
783 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
785 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
789 * batadv_dat_hash_free() - free the local DAT hash table
790 * @bat_priv: the bat priv with all the soft interface information
792 static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
794 if (!bat_priv->dat.hash)
797 __batadv_dat_purge(bat_priv, NULL);
799 batadv_hash_destroy(bat_priv->dat.hash);
801 bat_priv->dat.hash = NULL;
805 * batadv_dat_init() - initialise the DAT internals
806 * @bat_priv: the bat priv with all the soft interface information
808 * Return: 0 in case of success, a negative error code otherwise
810 int batadv_dat_init(struct batadv_priv *bat_priv)
812 if (bat_priv->dat.hash)
815 bat_priv->dat.hash = batadv_hash_new(1024);
817 if (!bat_priv->dat.hash)
820 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
821 batadv_dat_start_timer(bat_priv);
823 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
824 NULL, NULL, BATADV_TVLV_DAT, 1,
825 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
826 batadv_dat_tvlv_container_update(bat_priv);
831 * batadv_dat_free() - free the DAT internals
832 * @bat_priv: the bat priv with all the soft interface information
834 void batadv_dat_free(struct batadv_priv *bat_priv)
836 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
837 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
839 cancel_delayed_work_sync(&bat_priv->dat.work);
841 batadv_dat_hash_free(bat_priv);
845 * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
847 * @msg: buffer for the message
848 * @portid: netlink port
849 * @cb: Control block containing additional options
850 * @dat_entry: entry to dump
852 * Return: 0 or error code.
855 batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
856 struct netlink_callback *cb,
857 struct batadv_dat_entry *dat_entry)
862 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
863 &batadv_netlink_family, NLM_F_MULTI,
864 BATADV_CMD_GET_DAT_CACHE);
868 genl_dump_check_consistent(cb, hdr);
870 msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
872 if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
874 nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
875 dat_entry->mac_addr) ||
876 nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
877 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
878 genlmsg_cancel(msg, hdr);
882 genlmsg_end(msg, hdr);
887 * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
889 * @msg: buffer for the message
890 * @portid: netlink port
891 * @cb: Control block containing additional options
892 * @hash: hash to dump
893 * @bucket: bucket index to dump
894 * @idx_skip: How many entries to skip
896 * Return: 0 or error code.
899 batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
900 struct netlink_callback *cb,
901 struct batadv_hashtable *hash, unsigned int bucket,
904 struct batadv_dat_entry *dat_entry;
907 spin_lock_bh(&hash->list_locks[bucket]);
908 cb->seq = atomic_read(&hash->generation) << 1 | 1;
910 hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
914 if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
915 spin_unlock_bh(&hash->list_locks[bucket]);
924 spin_unlock_bh(&hash->list_locks[bucket]);
930 * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
931 * @msg: buffer for the message
932 * @cb: callback structure containing arguments
934 * Return: message length.
936 int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
938 struct batadv_hard_iface *primary_if = NULL;
939 int portid = NETLINK_CB(cb->skb).portid;
940 struct net *net = sock_net(cb->skb->sk);
941 struct net_device *soft_iface;
942 struct batadv_hashtable *hash;
943 struct batadv_priv *bat_priv;
944 int bucket = cb->args[0];
945 int idx = cb->args[1];
949 ifindex = batadv_netlink_get_ifindex(cb->nlh,
950 BATADV_ATTR_MESH_IFINDEX);
954 soft_iface = dev_get_by_index(net, ifindex);
955 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
960 bat_priv = netdev_priv(soft_iface);
961 hash = bat_priv->dat.hash;
963 primary_if = batadv_primary_if_get_selected(bat_priv);
964 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
969 while (bucket < hash->size) {
970 if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
978 cb->args[0] = bucket;
984 batadv_hardif_put(primary_if);
992 * batadv_arp_get_type() - parse an ARP packet and gets the type
993 * @bat_priv: the bat priv with all the soft interface information
994 * @skb: packet to analyse
995 * @hdr_size: size of the possible header before the ARP packet in the skb
997 * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
999 static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1000 struct sk_buff *skb, int hdr_size)
1002 struct arphdr *arphdr;
1003 struct ethhdr *ethhdr;
1004 __be32 ip_src, ip_dst;
1005 u8 *hw_src, *hw_dst;
1008 /* pull the ethernet header */
1009 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1012 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1014 if (ethhdr->h_proto != htons(ETH_P_ARP))
1017 /* pull the ARP payload */
1018 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1019 arp_hdr_len(skb->dev))))
1022 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1024 /* check whether the ARP packet carries a valid IP information */
1025 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1028 if (arphdr->ar_pro != htons(ETH_P_IP))
1031 if (arphdr->ar_hln != ETH_ALEN)
1034 if (arphdr->ar_pln != 4)
1037 /* Check for bad reply/request. If the ARP message is not sane, DAT
1038 * will simply ignore it
1040 ip_src = batadv_arp_ip_src(skb, hdr_size);
1041 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1042 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
1043 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
1044 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
1045 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
1048 hw_src = batadv_arp_hw_src(skb, hdr_size);
1049 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1052 /* don't care about the destination MAC address in ARP requests */
1053 if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
1054 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1055 if (is_zero_ether_addr(hw_dst) ||
1056 is_multicast_ether_addr(hw_dst))
1060 type = ntohs(arphdr->ar_op);
1066 * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
1067 * @skb: the buffer containing the packet to extract the VID from
1068 * @hdr_size: the size of the batman-adv header encapsulating the packet
1070 * Return: If the packet embedded in the skb is vlan tagged this function
1071 * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1074 static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1078 vid = batadv_get_vid(skb, *hdr_size);
1080 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1081 * If the header contained in the packet is a VLAN one (which is longer)
1082 * hdr_size is updated so that the functions will still skip the
1083 * correct amount of bytes.
1085 if (vid & BATADV_VLAN_HAS_TAG)
1086 *hdr_size += VLAN_HLEN;
1092 * batadv_dat_arp_create_reply() - create an ARP Reply
1093 * @bat_priv: the bat priv with all the soft interface information
1094 * @ip_src: ARP sender IP
1095 * @ip_dst: ARP target IP
1096 * @hw_src: Ethernet source and ARP sender MAC
1097 * @hw_dst: Ethernet destination and ARP target MAC
1098 * @vid: VLAN identifier (optional, set to zero otherwise)
1100 * Creates an ARP Reply from the given values, optionally encapsulated in a
1103 * Return: An skb containing an ARP Reply.
1105 static struct sk_buff *
1106 batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1107 __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1110 struct sk_buff *skb;
1112 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
1113 ip_src, hw_dst, hw_src, hw_dst);
1117 skb_reset_mac_header(skb);
1119 if (vid & BATADV_VLAN_HAS_TAG)
1120 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1121 vid & VLAN_VID_MASK);
1127 * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
1129 * @bat_priv: the bat priv with all the soft interface information
1130 * @skb: packet to check
1132 * Return: true if the message has been sent to the dht candidates, false
1133 * otherwise. In case of a positive return value the message has to be enqueued
1134 * to permit the fallback.
1136 bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1137 struct sk_buff *skb)
1140 __be32 ip_dst, ip_src;
1143 struct batadv_dat_entry *dat_entry = NULL;
1144 struct sk_buff *skb_new;
1145 struct net_device *soft_iface = bat_priv->soft_iface;
1149 if (!atomic_read(&bat_priv->distributed_arp_table))
1152 vid = batadv_dat_get_vid(skb, &hdr_size);
1154 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1155 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1156 * message to the selected DHT candidates
1158 if (type != ARPOP_REQUEST)
1161 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
1163 ip_src = batadv_arp_ip_src(skb, hdr_size);
1164 hw_src = batadv_arp_hw_src(skb, hdr_size);
1165 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1167 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1169 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1171 /* If the ARP request is destined for a local client the local
1172 * client will answer itself. DAT would only generate a
1175 * Moreover, if the soft-interface is enslaved into a bridge, an
1176 * additional DAT answer may trigger kernel warnings about
1177 * a packet coming from the wrong port.
1179 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
1184 /* If BLA is enabled, only send ARP replies if we have claimed
1185 * the destination for the ARP request or if no one else of
1186 * the backbone gws belonging to our backbone has claimed the
1189 if (!batadv_bla_check_claim(bat_priv,
1190 dat_entry->mac_addr, vid)) {
1191 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1192 "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1193 dat_entry->mac_addr);
1198 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1199 dat_entry->mac_addr,
1204 skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1206 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1207 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1208 skb->len + ETH_HLEN + hdr_size);
1211 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1214 /* Send the request to the DHT */
1215 ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1216 BATADV_P_DAT_DHT_GET);
1219 batadv_dat_entry_put(dat_entry);
1224 * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
1225 * answer using the local DAT storage
1226 * @bat_priv: the bat priv with all the soft interface information
1227 * @skb: packet to check
1228 * @hdr_size: size of the encapsulation header
1230 * Return: true if the request has been answered, false otherwise.
1232 bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1233 struct sk_buff *skb, int hdr_size)
1236 __be32 ip_src, ip_dst;
1238 struct sk_buff *skb_new;
1239 struct batadv_dat_entry *dat_entry = NULL;
1244 if (!atomic_read(&bat_priv->distributed_arp_table))
1247 vid = batadv_dat_get_vid(skb, &hdr_size);
1249 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1250 if (type != ARPOP_REQUEST)
1253 hw_src = batadv_arp_hw_src(skb, hdr_size);
1254 ip_src = batadv_arp_ip_src(skb, hdr_size);
1255 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1257 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
1259 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1261 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1265 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1266 dat_entry->mac_addr, hw_src, vid);
1270 /* To preserve backwards compatibility, the node has choose the outgoing
1271 * format based on the incoming request packet type. The assumption is
1272 * that a node not using the 4addr packet format doesn't support it.
1274 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
1275 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1276 BATADV_P_DAT_CACHE_REPLY,
1279 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
1281 if (err != NET_XMIT_DROP) {
1282 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
1286 batadv_dat_entry_put(dat_entry);
1293 * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
1294 * @bat_priv: the bat priv with all the soft interface information
1295 * @skb: packet to check
1297 void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1298 struct sk_buff *skb)
1301 __be32 ip_src, ip_dst;
1302 u8 *hw_src, *hw_dst;
1306 if (!atomic_read(&bat_priv->distributed_arp_table))
1309 vid = batadv_dat_get_vid(skb, &hdr_size);
1311 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1312 if (type != ARPOP_REPLY)
1315 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
1317 hw_src = batadv_arp_hw_src(skb, hdr_size);
1318 ip_src = batadv_arp_ip_src(skb, hdr_size);
1319 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1320 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1322 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1323 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1325 /* Send the ARP reply to the candidates for both the IP addresses that
1326 * the node obtained from the ARP reply
1328 batadv_dat_forward_data(bat_priv, skb, ip_src, vid,
1329 BATADV_P_DAT_DHT_PUT);
1330 batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1331 BATADV_P_DAT_DHT_PUT);
1335 * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1336 * local DAT storage only
1337 * @bat_priv: the bat priv with all the soft interface information
1338 * @skb: packet to check
1339 * @hdr_size: size of the encapsulation header
1341 * Return: true if the packet was snooped and consumed by DAT. False if the
1342 * packet has to be delivered to the interface
1344 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1345 struct sk_buff *skb, int hdr_size)
1347 struct batadv_dat_entry *dat_entry = NULL;
1349 __be32 ip_src, ip_dst;
1350 u8 *hw_src, *hw_dst;
1351 bool dropped = false;
1354 if (!atomic_read(&bat_priv->distributed_arp_table))
1357 vid = batadv_dat_get_vid(skb, &hdr_size);
1359 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1360 if (type != ARPOP_REPLY)
1363 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
1365 hw_src = batadv_arp_hw_src(skb, hdr_size);
1366 ip_src = batadv_arp_ip_src(skb, hdr_size);
1367 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1368 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1370 /* If ip_dst is already in cache and has the right mac address,
1371 * drop this frame if this ARP reply is destined for us because it's
1372 * most probably an ARP reply generated by another node of the DHT.
1373 * We have most probably received already a reply earlier. Delivering
1374 * this frame would lead to doubled receive of an ARP reply.
1376 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1377 if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
1378 batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1379 hw_src, &ip_src, hw_dst, &ip_dst,
1380 dat_entry->mac_addr, &dat_entry->ip);
1384 /* Update our internal cache with both the IP addresses the node got
1385 * within the ARP reply
1387 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1388 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1393 /* If BLA is enabled, only forward ARP replies if we have claimed the
1394 * source of the ARP reply or if no one else of the same backbone has
1395 * already claimed that client. This prevents that different gateways
1396 * to the same backbone all forward the ARP reply leading to multiple
1397 * replies in the backbone.
1399 if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1400 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1401 "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1407 /* if this REPLY is directed to a client of mine, let's deliver the
1408 * packet to the interface
1410 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1412 /* if this REPLY is sent on behalf of a client of mine, let's drop the
1413 * packet because the client will reply by itself
1415 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
1419 batadv_dat_entry_put(dat_entry);
1420 /* if dropped == false -> deliver to the interface */
1425 * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
1426 * @skb: the packet to check
1427 * @ip_src: a buffer to store the IPv4 source address in
1429 * Checks whether the given skb has an IP and UDP header valid for a DHCP
1430 * message from a DHCP server. And if so, stores the IPv4 source address in
1431 * the provided buffer.
1433 * Return: True if valid, false otherwise.
1436 batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
1438 unsigned int offset = skb_network_offset(skb);
1439 struct udphdr *udphdr, _udphdr;
1440 struct iphdr *iphdr, _iphdr;
1442 iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
1443 if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
1446 if (iphdr->protocol != IPPROTO_UDP)
1449 offset += iphdr->ihl * 4;
1450 skb_set_transport_header(skb, offset);
1452 udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
1453 if (!udphdr || udphdr->source != htons(67))
1456 *ip_src = get_unaligned(&iphdr->saddr);
1462 * batadv_dat_check_dhcp() - examine packet for valid DHCP message
1463 * @skb: the packet to check
1464 * @proto: ethernet protocol hint (behind a potential vlan)
1465 * @ip_src: a buffer to store the IPv4 source address in
1467 * Checks whether the given skb is a valid DHCP packet. And if so, stores the
1468 * IPv4 source address in the provided buffer.
1470 * Caller needs to ensure that the skb network header is set correctly.
1472 * Return: If skb is a valid DHCP packet, then returns its op code
1473 * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
1476 batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
1478 __be32 *magic, _magic;
1479 unsigned int offset;
1487 if (proto != htons(ETH_P_IP))
1490 if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
1493 offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1494 if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
1497 dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
1498 if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
1499 dhcp_h->hlen != ETH_ALEN)
1502 offset += offsetof(struct batadv_dhcp_packet, magic);
1504 magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
1505 if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
1512 * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
1513 * @skb: the DHCP packet to parse
1515 * Iterates over the DHCP options of the given DHCP packet to find a
1516 * DHCP Message Type option and parse it.
1518 * Caller needs to ensure that the given skb is a valid DHCP packet and
1519 * that the skb transport header is set correctly.
1521 * Return: The found DHCP message type value, if found. -EINVAL otherwise.
1523 static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
1525 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1532 offset += sizeof(struct batadv_dhcp_packet);
1534 while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
1535 if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
1538 if (tl->type == BATADV_DHCP_OPT_END)
1541 if (tl->type == BATADV_DHCP_OPT_PAD)
1544 offset += tl->len + sizeof(_tl);
1547 /* Option Overload Code not supported */
1548 if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
1549 tl->len != sizeof(_type))
1552 offset += sizeof(_tl);
1554 type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
1562 * batadv_dat_dhcp_get_yiaddr() - get yiaddr from a DHCP packet
1563 * @skb: the DHCP packet to parse
1564 * @buf: a buffer to store the yiaddr in
1566 * Caller needs to ensure that the given skb is a valid DHCP packet and
1567 * that the skb transport header is set correctly.
1569 * Return: True on success, false otherwise.
1571 static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
1573 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1576 offset += offsetof(struct batadv_dhcp_packet, yiaddr);
1577 yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
1583 *buf = get_unaligned(yiaddr);
1589 * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
1590 * @skb: the DHCP packet to parse
1591 * @buf: a buffer to store the chaddr in
1593 * Caller needs to ensure that the given skb is a valid DHCP packet and
1594 * that the skb transport header is set correctly.
1596 * Return: True on success, false otherwise
1598 static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
1600 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1603 offset += offsetof(struct batadv_dhcp_packet, chaddr);
1604 chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
1610 memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
1616 * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
1618 * @bat_priv: the bat priv with all the soft interface information
1619 * @chaddr: the DHCP client MAC address
1620 * @yiaddr: the DHCP client IP address
1621 * @hw_dst: the DHCP server MAC address
1622 * @ip_dst: the DHCP server IP address
1623 * @vid: VLAN identifier
1625 * Adds given MAC/IP pairs to the local DAT cache and propagates them further
1628 * For the DHT propagation, client MAC + IP will appear as the ARP Reply
1629 * transmitter (and hw_dst/ip_dst as the target).
1631 static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
1632 __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
1635 struct sk_buff *skb;
1637 skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
1642 skb_set_network_header(skb, ETH_HLEN);
1644 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1645 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1647 batadv_dat_forward_data(bat_priv, skb, yiaddr, vid,
1648 BATADV_P_DAT_DHT_PUT);
1649 batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1650 BATADV_P_DAT_DHT_PUT);
1654 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1655 "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1656 &ip_dst, hw_dst, batadv_print_vid(vid));
1657 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1658 "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1659 &yiaddr, chaddr, batadv_print_vid(vid));
1663 * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
1664 * @skb: the packet to check
1665 * @proto: ethernet protocol hint (behind a potential vlan)
1666 * @ip_src: a buffer to store the IPv4 source address in
1667 * @chaddr: a buffer to store the DHCP Client Hardware Address in
1668 * @yiaddr: a buffer to store the DHCP Your IP Address in
1670 * Checks whether the given skb is a valid DHCPACK. And if so, stores the
1671 * IPv4 server source address (ip_src), client MAC address (chaddr) and client
1672 * IPv4 address (yiaddr) in the provided buffers.
1674 * Caller needs to ensure that the skb network header is set correctly.
1676 * Return: True if the skb is a valid DHCPACK. False otherwise.
1679 batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
1680 u8 *chaddr, __be32 *yiaddr)
1684 type = batadv_dat_check_dhcp(skb, proto, ip_src);
1685 if (type != BATADV_BOOTREPLY)
1688 type = batadv_dat_get_dhcp_message_type(skb);
1689 if (type != BATADV_DHCPACK)
1692 if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
1695 if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
1702 * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
1703 * @bat_priv: the bat priv with all the soft interface information
1704 * @skb: the packet to snoop
1705 * @proto: ethernet protocol hint (behind a potential vlan)
1706 * @vid: VLAN identifier
1708 * This function first checks whether the given skb is a valid DHCPACK. If
1709 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1710 * field and DHCP Your IP Address field are added to the local DAT cache and
1711 * propagated into the DHT.
1713 * Caller needs to ensure that the skb mac and network headers are set
1716 void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
1717 struct sk_buff *skb,
1721 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1722 __be32 ip_src, yiaddr;
1724 if (!atomic_read(&bat_priv->distributed_arp_table))
1727 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1730 batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
1735 * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
1736 * @bat_priv: the bat priv with all the soft interface information
1737 * @skb: the packet to snoop
1738 * @hdr_size: header size, up to the tail of the batman-adv header
1740 * This function first checks whether the given skb is a valid DHCPACK. If
1741 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1742 * field and DHCP Your IP Address field are added to the local DAT cache.
1744 void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
1745 struct sk_buff *skb, int hdr_size)
1747 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1748 struct ethhdr *ethhdr;
1749 __be32 ip_src, yiaddr;
1754 if (!atomic_read(&bat_priv->distributed_arp_table))
1757 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1760 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1761 skb_set_network_header(skb, hdr_size + ETH_HLEN);
1762 proto = ethhdr->h_proto;
1764 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1767 hw_src = ethhdr->h_source;
1768 vid = batadv_dat_get_vid(skb, &hdr_size);
1770 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1771 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1773 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1774 "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1775 &ip_src, hw_src, batadv_print_vid(vid));
1776 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1777 "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1778 &yiaddr, chaddr, batadv_print_vid(vid));
1782 * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1783 * dropped (because the node has already obtained the reply via DAT) or not
1784 * @bat_priv: the bat priv with all the soft interface information
1785 * @forw_packet: the broadcast packet
1787 * Return: true if the node can drop the packet, false otherwise.
1789 bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1790 struct batadv_forw_packet *forw_packet)
1794 struct batadv_dat_entry *dat_entry = NULL;
1796 int hdr_size = sizeof(struct batadv_bcast_packet);
1799 if (!atomic_read(&bat_priv->distributed_arp_table))
1802 /* If this packet is an ARP_REQUEST and the node already has the
1803 * information that it is going to ask, then the packet can be dropped
1805 if (batadv_forw_packet_is_rebroadcast(forw_packet))
1808 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1810 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1811 if (type != ARPOP_REQUEST)
1814 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1815 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1816 /* check if the node already got this entry */
1818 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1819 "ARP Request for %pI4: fallback\n", &ip_dst);
1823 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1824 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1828 batadv_dat_entry_put(dat_entry);