1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2011-2019 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 <asm/unaligned.h>
23 #include <linux/atomic.h>
24 #include <linux/bitops.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/errno.h>
27 #include <linux/etherdevice.h>
28 #include <linux/gfp.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/kref.h>
37 #include <linux/list.h>
38 #include <linux/netlink.h>
39 #include <linux/rculist.h>
40 #include <linux/rcupdate.h>
41 #include <linux/seq_file.h>
42 #include <linux/skbuff.h>
43 #include <linux/slab.h>
44 #include <linux/spinlock.h>
45 #include <linux/stddef.h>
46 #include <linux/string.h>
47 #include <linux/udp.h>
48 #include <linux/workqueue.h>
50 #include <net/genetlink.h>
51 #include <net/netlink.h>
53 #include <uapi/linux/batman_adv.h>
55 #include "bridge_loop_avoidance.h"
56 #include "hard-interface.h"
60 #include "originator.h"
62 #include "soft-interface.h"
63 #include "translation-table.h"
70 enum batadv_boothtype {
71 BATADV_HTYPE_ETHERNET = 1,
74 enum batadv_dhcpoptioncode {
75 BATADV_DHCP_OPT_PAD = 0,
76 BATADV_DHCP_OPT_MSG_TYPE = 53,
77 BATADV_DHCP_OPT_END = 255,
80 enum batadv_dhcptype {
84 /* { 99, 130, 83, 99 } */
85 #define BATADV_DHCP_MAGIC 1669485411
87 struct batadv_dhcp_packet {
106 #define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
107 #define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
109 static void batadv_dat_purge(struct work_struct *work);
112 * batadv_dat_start_timer() - initialise the DAT periodic worker
113 * @bat_priv: the bat priv with all the soft interface information
115 static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
117 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
118 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
119 msecs_to_jiffies(10000));
123 * batadv_dat_entry_release() - release dat_entry from lists and queue for free
124 * after rcu grace period
125 * @ref: kref pointer of the dat_entry
127 static void batadv_dat_entry_release(struct kref *ref)
129 struct batadv_dat_entry *dat_entry;
131 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
133 kfree_rcu(dat_entry, rcu);
137 * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
139 * @dat_entry: dat_entry to be free'd
141 static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
143 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
147 * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
148 * @dat_entry: the entry to check
150 * Return: true if the entry has to be purged now, false otherwise.
152 static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
154 return batadv_has_timed_out(dat_entry->last_update,
155 BATADV_DAT_ENTRY_TIMEOUT);
159 * __batadv_dat_purge() - delete entries from the DAT local storage
160 * @bat_priv: the bat priv with all the soft interface information
161 * @to_purge: function in charge to decide whether an entry has to be purged or
162 * not. This function takes the dat_entry as argument and has to
163 * returns a boolean value: true is the entry has to be deleted,
166 * Loops over each entry in the DAT local storage and deletes it if and only if
167 * the to_purge function passed as argument returns true.
169 static void __batadv_dat_purge(struct batadv_priv *bat_priv,
170 bool (*to_purge)(struct batadv_dat_entry *))
172 spinlock_t *list_lock; /* protects write access to the hash lists */
173 struct batadv_dat_entry *dat_entry;
174 struct hlist_node *node_tmp;
175 struct hlist_head *head;
178 if (!bat_priv->dat.hash)
181 for (i = 0; i < bat_priv->dat.hash->size; i++) {
182 head = &bat_priv->dat.hash->table[i];
183 list_lock = &bat_priv->dat.hash->list_locks[i];
185 spin_lock_bh(list_lock);
186 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
188 /* if a helper function has been passed as parameter,
189 * ask it if the entry has to be purged or not
191 if (to_purge && !to_purge(dat_entry))
194 hlist_del_rcu(&dat_entry->hash_entry);
195 batadv_dat_entry_put(dat_entry);
197 spin_unlock_bh(list_lock);
202 * batadv_dat_purge() - periodic task that deletes old entries from the local
204 * @work: kernel work struct
206 static void batadv_dat_purge(struct work_struct *work)
208 struct delayed_work *delayed_work;
209 struct batadv_priv_dat *priv_dat;
210 struct batadv_priv *bat_priv;
212 delayed_work = to_delayed_work(work);
213 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
214 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
216 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
217 batadv_dat_start_timer(bat_priv);
221 * batadv_compare_dat() - comparing function used in the local DAT hash table
222 * @node: node in the local table
223 * @data2: second object to compare the node to
225 * Return: true if the two entries are the same, false otherwise.
227 static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
229 const void *data1 = container_of(node, struct batadv_dat_entry,
232 return memcmp(data1, data2, sizeof(__be32)) == 0;
236 * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
238 * @hdr_size: size of the possible header before the ARP packet
240 * Return: the value of the hw_src field in the ARP packet.
242 static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
246 addr = (u8 *)(skb->data + hdr_size);
247 addr += ETH_HLEN + sizeof(struct arphdr);
253 * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
255 * @hdr_size: size of the possible header before the ARP packet
257 * Return: the value of the ip_src field in the ARP packet.
259 static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
261 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
265 * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
267 * @hdr_size: size of the possible header before the ARP packet
269 * Return: the value of the hw_dst field in the ARP packet.
271 static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
273 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
277 * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
279 * @hdr_size: size of the possible header before the ARP packet
281 * Return: the value of the ip_dst field in the ARP packet.
283 static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
285 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
289 * batadv_hash_dat() - compute the hash value for an IP address
290 * @data: data to hash
291 * @size: size of the hash table
293 * Return: the selected index in the hash table for the given data.
295 static u32 batadv_hash_dat(const void *data, u32 size)
298 const struct batadv_dat_entry *dat = data;
299 const unsigned char *key;
302 key = (const unsigned char *)&dat->ip;
303 for (i = 0; i < sizeof(dat->ip); i++) {
305 hash += (hash << 10);
309 key = (const unsigned char *)&dat->vid;
310 for (i = 0; i < sizeof(dat->vid); i++) {
312 hash += (hash << 10);
317 hash ^= (hash >> 11);
318 hash += (hash << 15);
324 * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
326 * @bat_priv: the bat priv with all the soft interface information
328 * @vid: VLAN identifier
330 * Return: the dat_entry if found, NULL otherwise.
332 static struct batadv_dat_entry *
333 batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
336 struct hlist_head *head;
337 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
338 struct batadv_hashtable *hash = bat_priv->dat.hash;
347 index = batadv_hash_dat(&to_find, hash->size);
348 head = &hash->table[index];
351 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
352 if (dat_entry->ip != ip)
355 if (!kref_get_unless_zero(&dat_entry->refcount))
358 dat_entry_tmp = dat_entry;
363 return dat_entry_tmp;
367 * batadv_dat_entry_add() - add a new dat entry or update it if already exists
368 * @bat_priv: the bat priv with all the soft interface information
369 * @ip: ipv4 to add/edit
370 * @mac_addr: mac address to assign to the given ipv4
371 * @vid: VLAN identifier
373 static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
374 u8 *mac_addr, unsigned short vid)
376 struct batadv_dat_entry *dat_entry;
379 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
380 /* if this entry is already known, just update it */
382 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
383 ether_addr_copy(dat_entry->mac_addr, mac_addr);
384 dat_entry->last_update = jiffies;
385 batadv_dbg(BATADV_DBG_DAT, bat_priv,
386 "Entry updated: %pI4 %pM (vid: %d)\n",
387 &dat_entry->ip, dat_entry->mac_addr,
388 batadv_print_vid(vid));
392 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
397 dat_entry->vid = vid;
398 ether_addr_copy(dat_entry->mac_addr, mac_addr);
399 dat_entry->last_update = jiffies;
400 kref_init(&dat_entry->refcount);
402 kref_get(&dat_entry->refcount);
403 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
404 batadv_hash_dat, dat_entry,
405 &dat_entry->hash_entry);
407 if (unlikely(hash_added != 0)) {
408 /* remove the reference for the hash */
409 batadv_dat_entry_put(dat_entry);
413 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
414 &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
418 batadv_dat_entry_put(dat_entry);
421 #ifdef CONFIG_BATMAN_ADV_DEBUG
424 * batadv_dbg_arp() - print a debug message containing all the ARP packet
426 * @bat_priv: the bat priv with all the soft interface information
428 * @hdr_size: size of the possible header before the ARP packet
429 * @msg: message to print together with the debugging information
431 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
432 int hdr_size, char *msg)
434 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
435 struct batadv_bcast_packet *bcast_pkt;
437 __be32 ip_src, ip_dst;
440 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
442 ip_src = batadv_arp_ip_src(skb, hdr_size);
443 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
444 batadv_dbg(BATADV_DBG_DAT, bat_priv,
445 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
446 batadv_arp_hw_src(skb, hdr_size), &ip_src,
447 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
449 if (hdr_size < sizeof(struct batadv_unicast_packet))
452 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
454 switch (unicast_4addr_packet->u.packet_type) {
456 batadv_dbg(BATADV_DBG_DAT, bat_priv,
457 "* encapsulated within a UNICAST packet\n");
459 case BATADV_UNICAST_4ADDR:
460 batadv_dbg(BATADV_DBG_DAT, bat_priv,
461 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
462 unicast_4addr_packet->src);
463 switch (unicast_4addr_packet->subtype) {
464 case BATADV_P_DAT_DHT_PUT:
465 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
467 case BATADV_P_DAT_DHT_GET:
468 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
470 case BATADV_P_DAT_CACHE_REPLY:
471 batadv_dbg(BATADV_DBG_DAT, bat_priv,
472 "* type: DAT_CACHE_REPLY\n");
475 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
478 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
479 unicast_4addr_packet->u.packet_type);
483 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
484 orig_addr = bcast_pkt->orig;
485 batadv_dbg(BATADV_DBG_DAT, bat_priv,
486 "* encapsulated within a BCAST packet (src: %pM)\n",
490 batadv_dbg(BATADV_DBG_DAT, bat_priv,
491 "* encapsulated within an unknown packet type (0x%x)\n",
492 unicast_4addr_packet->u.packet_type);
498 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
499 int hdr_size, char *msg)
503 #endif /* CONFIG_BATMAN_ADV_DEBUG */
506 * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
507 * @res: the array with the already selected candidates
508 * @select: number of already selected candidates
509 * @tmp_max: address of the currently evaluated node
510 * @max: current round max address
511 * @last_max: address of the last selected candidate
512 * @candidate: orig_node under evaluation
513 * @max_orig_node: last selected candidate
515 * Return: true if the node has been elected as next candidate or false
518 static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
519 int select, batadv_dat_addr_t tmp_max,
520 batadv_dat_addr_t max,
521 batadv_dat_addr_t last_max,
522 struct batadv_orig_node *candidate,
523 struct batadv_orig_node *max_orig_node)
528 /* check if orig node candidate is running DAT */
529 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
532 /* Check if this node has already been selected... */
533 for (j = 0; j < select; j++)
534 if (res[j].orig_node == candidate)
536 /* ..and possibly skip it */
539 /* sanity check: has it already been selected? This should not happen */
540 if (tmp_max > last_max)
542 /* check if during this iteration an originator with a closer dht
543 * address has already been found
547 /* this is an hash collision with the temporary selected node. Choose
548 * the one with the lowest address
550 if (tmp_max == max && max_orig_node &&
551 batadv_compare_eth(candidate->orig, max_orig_node->orig))
560 * batadv_choose_next_candidate() - select the next DHT candidate
561 * @bat_priv: the bat priv with all the soft interface information
562 * @cands: candidates array
563 * @select: number of candidates already present in the array
564 * @ip_key: key to look up in the DHT
565 * @last_max: pointer where the address of the selected candidate will be saved
567 static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
568 struct batadv_dat_candidate *cands,
569 int select, batadv_dat_addr_t ip_key,
570 batadv_dat_addr_t *last_max)
572 batadv_dat_addr_t max = 0;
573 batadv_dat_addr_t tmp_max = 0;
574 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
575 struct batadv_hashtable *hash = bat_priv->orig_hash;
576 struct hlist_head *head;
579 /* if no node is eligible as candidate, leave the candidate type as
582 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
584 /* iterate over the originator list and find the node with the closest
585 * dat_address which has not been selected yet
587 for (i = 0; i < hash->size; i++) {
588 head = &hash->table[i];
591 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
592 /* the dht space is a ring using unsigned addresses */
593 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
596 if (!batadv_is_orig_node_eligible(cands, select,
598 *last_max, orig_node,
602 if (!kref_get_unless_zero(&orig_node->refcount))
607 batadv_orig_node_put(max_orig_node);
608 max_orig_node = orig_node;
613 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
614 cands[select].orig_node = max_orig_node;
615 batadv_dbg(BATADV_DBG_DAT, bat_priv,
616 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
617 select, max_orig_node->orig, max_orig_node->dat_addr,
624 * batadv_dat_select_candidates() - select the nodes which the DHT message has
626 * @bat_priv: the bat priv with all the soft interface information
627 * @ip_dst: ipv4 to look up in the DHT
628 * @vid: VLAN identifier
630 * An originator O is selected if and only if its DHT_ID value is one of three
631 * closest values (from the LEFT, with wrap around if needed) then the hash
632 * value of the key. ip_dst is the key.
634 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
636 static struct batadv_dat_candidate *
637 batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
641 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
642 struct batadv_dat_candidate *res;
643 struct batadv_dat_entry dat;
645 if (!bat_priv->orig_hash)
648 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
655 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
656 BATADV_DAT_ADDR_MAX);
658 batadv_dbg(BATADV_DBG_DAT, bat_priv,
659 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
662 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
663 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
670 * batadv_dat_send_data() - send a payload to the selected candidates
671 * @bat_priv: the bat priv with all the soft interface information
672 * @skb: payload to send
674 * @vid: VLAN identifier
675 * @packet_subtype: unicast4addr packet subtype to use
677 * This function copies the skb with pskb_copy() and is sent as unicast packet
678 * to each of the selected candidates.
680 * Return: true if the packet is sent to at least one candidate, false
683 static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
684 struct sk_buff *skb, __be32 ip,
685 unsigned short vid, int packet_subtype)
690 struct batadv_neigh_node *neigh_node = NULL;
691 struct sk_buff *tmp_skb;
692 struct batadv_dat_candidate *cand;
694 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
698 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
700 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
701 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
704 neigh_node = batadv_orig_router_get(cand[i].orig_node,
709 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
710 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
717 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
718 if (send_status == NET_XMIT_SUCCESS) {
719 /* count the sent packet */
720 switch (packet_subtype) {
721 case BATADV_P_DAT_DHT_GET:
722 batadv_inc_counter(bat_priv,
723 BATADV_CNT_DAT_GET_TX);
725 case BATADV_P_DAT_DHT_PUT:
726 batadv_inc_counter(bat_priv,
727 BATADV_CNT_DAT_PUT_TX);
731 /* packet sent to a candidate: return true */
735 batadv_neigh_node_put(neigh_node);
737 batadv_orig_node_put(cand[i].orig_node);
746 * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
748 * @bat_priv: the bat priv with all the soft interface information
750 static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
754 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
758 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
761 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
768 * batadv_dat_status_update() - update the dat tvlv container after dat
770 * @net_dev: the soft interface net device
772 void batadv_dat_status_update(struct net_device *net_dev)
774 struct batadv_priv *bat_priv = netdev_priv(net_dev);
776 batadv_dat_tvlv_container_update(bat_priv);
780 * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
781 * @bat_priv: the bat priv with all the soft interface information
782 * @orig: the orig_node of the ogm
783 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
784 * @tvlv_value: tvlv buffer containing the gateway data
785 * @tvlv_value_len: tvlv buffer length
787 static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
788 struct batadv_orig_node *orig,
790 void *tvlv_value, u16 tvlv_value_len)
792 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
793 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
795 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
799 * batadv_dat_hash_free() - free the local DAT hash table
800 * @bat_priv: the bat priv with all the soft interface information
802 static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
804 if (!bat_priv->dat.hash)
807 __batadv_dat_purge(bat_priv, NULL);
809 batadv_hash_destroy(bat_priv->dat.hash);
811 bat_priv->dat.hash = NULL;
815 * batadv_dat_init() - initialise the DAT internals
816 * @bat_priv: the bat priv with all the soft interface information
818 * Return: 0 in case of success, a negative error code otherwise
820 int batadv_dat_init(struct batadv_priv *bat_priv)
822 if (bat_priv->dat.hash)
825 bat_priv->dat.hash = batadv_hash_new(1024);
827 if (!bat_priv->dat.hash)
830 batadv_dat_start_timer(bat_priv);
832 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
833 NULL, BATADV_TVLV_DAT, 1,
834 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
835 batadv_dat_tvlv_container_update(bat_priv);
840 * batadv_dat_free() - free the DAT internals
841 * @bat_priv: the bat priv with all the soft interface information
843 void batadv_dat_free(struct batadv_priv *bat_priv)
845 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
846 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
848 cancel_delayed_work_sync(&bat_priv->dat.work);
850 batadv_dat_hash_free(bat_priv);
853 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
855 * batadv_dat_cache_seq_print_text() - print the local DAT hash table
856 * @seq: seq file to print on
861 int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
863 struct net_device *net_dev = (struct net_device *)seq->private;
864 struct batadv_priv *bat_priv = netdev_priv(net_dev);
865 struct batadv_hashtable *hash = bat_priv->dat.hash;
866 struct batadv_dat_entry *dat_entry;
867 struct batadv_hard_iface *primary_if;
868 struct hlist_head *head;
869 unsigned long last_seen_jiffies;
870 int last_seen_msecs, last_seen_secs, last_seen_mins;
873 primary_if = batadv_seq_print_text_primary_if_get(seq);
877 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
879 " IPv4 MAC VID last-seen\n");
881 for (i = 0; i < hash->size; i++) {
882 head = &hash->table[i];
885 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
886 last_seen_jiffies = jiffies - dat_entry->last_update;
887 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
888 last_seen_mins = last_seen_msecs / 60000;
889 last_seen_msecs = last_seen_msecs % 60000;
890 last_seen_secs = last_seen_msecs / 1000;
892 seq_printf(seq, " * %15pI4 %pM %4i %6i:%02i\n",
893 &dat_entry->ip, dat_entry->mac_addr,
894 batadv_print_vid(dat_entry->vid),
895 last_seen_mins, last_seen_secs);
902 batadv_hardif_put(primary_if);
908 * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
910 * @msg: buffer for the message
911 * @portid: netlink port
912 * @cb: Control block containing additional options
913 * @dat_entry: entry to dump
915 * Return: 0 or error code.
918 batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
919 struct netlink_callback *cb,
920 struct batadv_dat_entry *dat_entry)
925 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
926 &batadv_netlink_family, NLM_F_MULTI,
927 BATADV_CMD_GET_DAT_CACHE);
931 genl_dump_check_consistent(cb, hdr);
933 msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
935 if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
937 nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
938 dat_entry->mac_addr) ||
939 nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
940 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
941 genlmsg_cancel(msg, hdr);
945 genlmsg_end(msg, hdr);
950 * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
952 * @msg: buffer for the message
953 * @portid: netlink port
954 * @cb: Control block containing additional options
955 * @hash: hash to dump
956 * @bucket: bucket index to dump
957 * @idx_skip: How many entries to skip
959 * Return: 0 or error code.
962 batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
963 struct netlink_callback *cb,
964 struct batadv_hashtable *hash, unsigned int bucket,
967 struct batadv_dat_entry *dat_entry;
970 spin_lock_bh(&hash->list_locks[bucket]);
971 cb->seq = atomic_read(&hash->generation) << 1 | 1;
973 hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
977 if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
978 spin_unlock_bh(&hash->list_locks[bucket]);
987 spin_unlock_bh(&hash->list_locks[bucket]);
993 * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
994 * @msg: buffer for the message
995 * @cb: callback structure containing arguments
997 * Return: message length.
999 int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
1001 struct batadv_hard_iface *primary_if = NULL;
1002 int portid = NETLINK_CB(cb->skb).portid;
1003 struct net *net = sock_net(cb->skb->sk);
1004 struct net_device *soft_iface;
1005 struct batadv_hashtable *hash;
1006 struct batadv_priv *bat_priv;
1007 int bucket = cb->args[0];
1008 int idx = cb->args[1];
1012 ifindex = batadv_netlink_get_ifindex(cb->nlh,
1013 BATADV_ATTR_MESH_IFINDEX);
1017 soft_iface = dev_get_by_index(net, ifindex);
1018 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1023 bat_priv = netdev_priv(soft_iface);
1024 hash = bat_priv->dat.hash;
1026 primary_if = batadv_primary_if_get_selected(bat_priv);
1027 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1032 while (bucket < hash->size) {
1033 if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
1041 cb->args[0] = bucket;
1048 batadv_hardif_put(primary_if);
1051 dev_put(soft_iface);
1057 * batadv_arp_get_type() - parse an ARP packet and gets the type
1058 * @bat_priv: the bat priv with all the soft interface information
1059 * @skb: packet to analyse
1060 * @hdr_size: size of the possible header before the ARP packet in the skb
1062 * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
1064 static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1065 struct sk_buff *skb, int hdr_size)
1067 struct arphdr *arphdr;
1068 struct ethhdr *ethhdr;
1069 __be32 ip_src, ip_dst;
1070 u8 *hw_src, *hw_dst;
1073 /* pull the ethernet header */
1074 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1077 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1079 if (ethhdr->h_proto != htons(ETH_P_ARP))
1082 /* pull the ARP payload */
1083 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1084 arp_hdr_len(skb->dev))))
1087 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1089 /* check whether the ARP packet carries a valid IP information */
1090 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1093 if (arphdr->ar_pro != htons(ETH_P_IP))
1096 if (arphdr->ar_hln != ETH_ALEN)
1099 if (arphdr->ar_pln != 4)
1102 /* Check for bad reply/request. If the ARP message is not sane, DAT
1103 * will simply ignore it
1105 ip_src = batadv_arp_ip_src(skb, hdr_size);
1106 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1107 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
1108 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
1109 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
1110 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
1113 hw_src = batadv_arp_hw_src(skb, hdr_size);
1114 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1117 /* don't care about the destination MAC address in ARP requests */
1118 if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
1119 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1120 if (is_zero_ether_addr(hw_dst) ||
1121 is_multicast_ether_addr(hw_dst))
1125 type = ntohs(arphdr->ar_op);
1131 * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
1132 * @skb: the buffer containing the packet to extract the VID from
1133 * @hdr_size: the size of the batman-adv header encapsulating the packet
1135 * Return: If the packet embedded in the skb is vlan tagged this function
1136 * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1139 static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1143 vid = batadv_get_vid(skb, *hdr_size);
1145 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1146 * If the header contained in the packet is a VLAN one (which is longer)
1147 * hdr_size is updated so that the functions will still skip the
1148 * correct amount of bytes.
1150 if (vid & BATADV_VLAN_HAS_TAG)
1151 *hdr_size += VLAN_HLEN;
1157 * batadv_dat_arp_create_reply() - create an ARP Reply
1158 * @bat_priv: the bat priv with all the soft interface information
1159 * @ip_src: ARP sender IP
1160 * @ip_dst: ARP target IP
1161 * @hw_src: Ethernet source and ARP sender MAC
1162 * @hw_dst: Ethernet destination and ARP target MAC
1163 * @vid: VLAN identifier (optional, set to zero otherwise)
1165 * Creates an ARP Reply from the given values, optionally encapsulated in a
1168 * Return: An skb containing an ARP Reply.
1170 static struct sk_buff *
1171 batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1172 __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1175 struct sk_buff *skb;
1177 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
1178 ip_src, hw_dst, hw_src, hw_dst);
1182 skb_reset_mac_header(skb);
1184 if (vid & BATADV_VLAN_HAS_TAG)
1185 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1186 vid & VLAN_VID_MASK);
1192 * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
1194 * @bat_priv: the bat priv with all the soft interface information
1195 * @skb: packet to check
1197 * Return: true if the message has been sent to the dht candidates, false
1198 * otherwise. In case of a positive return value the message has to be enqueued
1199 * to permit the fallback.
1201 bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1202 struct sk_buff *skb)
1205 __be32 ip_dst, ip_src;
1208 struct batadv_dat_entry *dat_entry = NULL;
1209 struct sk_buff *skb_new;
1210 struct net_device *soft_iface = bat_priv->soft_iface;
1214 if (!atomic_read(&bat_priv->distributed_arp_table))
1217 vid = batadv_dat_get_vid(skb, &hdr_size);
1219 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1220 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1221 * message to the selected DHT candidates
1223 if (type != ARPOP_REQUEST)
1226 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
1228 ip_src = batadv_arp_ip_src(skb, hdr_size);
1229 hw_src = batadv_arp_hw_src(skb, hdr_size);
1230 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1232 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1234 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1236 /* If the ARP request is destined for a local client the local
1237 * client will answer itself. DAT would only generate a
1240 * Moreover, if the soft-interface is enslaved into a bridge, an
1241 * additional DAT answer may trigger kernel warnings about
1242 * a packet coming from the wrong port.
1244 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
1249 /* If BLA is enabled, only send ARP replies if we have claimed
1250 * the destination for the ARP request or if no one else of
1251 * the backbone gws belonging to our backbone has claimed the
1254 if (!batadv_bla_check_claim(bat_priv,
1255 dat_entry->mac_addr, vid)) {
1256 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1257 "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1258 dat_entry->mac_addr);
1263 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1264 dat_entry->mac_addr,
1269 skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1271 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1272 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1273 skb->len + ETH_HLEN + hdr_size);
1276 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1279 /* Send the request to the DHT */
1280 ret = batadv_dat_send_data(bat_priv, skb, ip_dst, vid,
1281 BATADV_P_DAT_DHT_GET);
1285 batadv_dat_entry_put(dat_entry);
1290 * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
1291 * answer using the local DAT storage
1292 * @bat_priv: the bat priv with all the soft interface information
1293 * @skb: packet to check
1294 * @hdr_size: size of the encapsulation header
1296 * Return: true if the request has been answered, false otherwise.
1298 bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1299 struct sk_buff *skb, int hdr_size)
1302 __be32 ip_src, ip_dst;
1304 struct sk_buff *skb_new;
1305 struct batadv_dat_entry *dat_entry = NULL;
1310 if (!atomic_read(&bat_priv->distributed_arp_table))
1313 vid = batadv_dat_get_vid(skb, &hdr_size);
1315 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1316 if (type != ARPOP_REQUEST)
1319 hw_src = batadv_arp_hw_src(skb, hdr_size);
1320 ip_src = batadv_arp_ip_src(skb, hdr_size);
1321 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1323 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
1325 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1327 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1331 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1332 dat_entry->mac_addr, hw_src, vid);
1336 /* To preserve backwards compatibility, the node has choose the outgoing
1337 * format based on the incoming request packet type. The assumption is
1338 * that a node not using the 4addr packet format doesn't support it.
1340 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
1341 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1342 BATADV_P_DAT_CACHE_REPLY,
1345 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
1347 if (err != NET_XMIT_DROP) {
1348 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
1353 batadv_dat_entry_put(dat_entry);
1360 * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
1361 * @bat_priv: the bat priv with all the soft interface information
1362 * @skb: packet to check
1364 void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1365 struct sk_buff *skb)
1368 __be32 ip_src, ip_dst;
1369 u8 *hw_src, *hw_dst;
1373 if (!atomic_read(&bat_priv->distributed_arp_table))
1376 vid = batadv_dat_get_vid(skb, &hdr_size);
1378 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1379 if (type != ARPOP_REPLY)
1382 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
1384 hw_src = batadv_arp_hw_src(skb, hdr_size);
1385 ip_src = batadv_arp_ip_src(skb, hdr_size);
1386 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1387 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1389 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1390 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1392 /* Send the ARP reply to the candidates for both the IP addresses that
1393 * the node obtained from the ARP reply
1395 batadv_dat_send_data(bat_priv, skb, ip_src, vid, BATADV_P_DAT_DHT_PUT);
1396 batadv_dat_send_data(bat_priv, skb, ip_dst, vid, BATADV_P_DAT_DHT_PUT);
1400 * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1401 * local DAT storage only
1402 * @bat_priv: the bat priv with all the soft interface information
1403 * @skb: packet to check
1404 * @hdr_size: size of the encapsulation header
1406 * Return: true if the packet was snooped and consumed by DAT. False if the
1407 * packet has to be delivered to the interface
1409 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1410 struct sk_buff *skb, int hdr_size)
1412 struct batadv_dat_entry *dat_entry = NULL;
1414 __be32 ip_src, ip_dst;
1415 u8 *hw_src, *hw_dst;
1416 bool dropped = false;
1419 if (!atomic_read(&bat_priv->distributed_arp_table))
1422 vid = batadv_dat_get_vid(skb, &hdr_size);
1424 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1425 if (type != ARPOP_REPLY)
1428 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
1430 hw_src = batadv_arp_hw_src(skb, hdr_size);
1431 ip_src = batadv_arp_ip_src(skb, hdr_size);
1432 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1433 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1435 /* If ip_dst is already in cache and has the right mac address,
1436 * drop this frame if this ARP reply is destined for us because it's
1437 * most probably an ARP reply generated by another node of the DHT.
1438 * We have most probably received already a reply earlier. Delivering
1439 * this frame would lead to doubled receive of an ARP reply.
1441 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1442 if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
1443 batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1444 hw_src, &ip_src, hw_dst, &ip_dst,
1445 dat_entry->mac_addr, &dat_entry->ip);
1450 /* Update our internal cache with both the IP addresses the node got
1451 * within the ARP reply
1453 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1454 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1456 /* If BLA is enabled, only forward ARP replies if we have claimed the
1457 * source of the ARP reply or if no one else of the same backbone has
1458 * already claimed that client. This prevents that different gateways
1459 * to the same backbone all forward the ARP reply leading to multiple
1460 * replies in the backbone.
1462 if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1463 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1464 "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1470 /* if this REPLY is directed to a client of mine, let's deliver the
1471 * packet to the interface
1473 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1475 /* if this REPLY is sent on behalf of a client of mine, let's drop the
1476 * packet because the client will reply by itself
1478 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
1483 batadv_dat_entry_put(dat_entry);
1484 /* if dropped == false -> deliver to the interface */
1489 * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
1490 * @skb: the packet to check
1491 * @ip_src: a buffer to store the IPv4 source address in
1493 * Checks whether the given skb has an IP and UDP header valid for a DHCP
1494 * message from a DHCP server. And if so, stores the IPv4 source address in
1495 * the provided buffer.
1497 * Return: True if valid, false otherwise.
1500 batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
1502 unsigned int offset = skb_network_offset(skb);
1503 struct udphdr *udphdr, _udphdr;
1504 struct iphdr *iphdr, _iphdr;
1506 iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
1507 if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
1510 if (iphdr->protocol != IPPROTO_UDP)
1513 offset += iphdr->ihl * 4;
1514 skb_set_transport_header(skb, offset);
1516 udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
1517 if (!udphdr || udphdr->source != htons(67))
1520 *ip_src = get_unaligned(&iphdr->saddr);
1526 * batadv_dat_check_dhcp() - examine packet for valid DHCP message
1527 * @skb: the packet to check
1528 * @proto: ethernet protocol hint (behind a potential vlan)
1529 * @ip_src: a buffer to store the IPv4 source address in
1531 * Checks whether the given skb is a valid DHCP packet. And if so, stores the
1532 * IPv4 source address in the provided buffer.
1534 * Caller needs to ensure that the skb network header is set correctly.
1536 * Return: If skb is a valid DHCP packet, then returns its op code
1537 * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
1540 batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
1542 __be32 *magic, _magic;
1543 unsigned int offset;
1551 if (proto != htons(ETH_P_IP))
1554 if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
1557 offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1558 if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
1561 dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
1562 if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
1563 dhcp_h->hlen != ETH_ALEN)
1566 offset += offsetof(struct batadv_dhcp_packet, magic);
1568 magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
1569 if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
1576 * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
1577 * @skb: the DHCP packet to parse
1579 * Iterates over the DHCP options of the given DHCP packet to find a
1580 * DHCP Message Type option and parse it.
1582 * Caller needs to ensure that the given skb is a valid DHCP packet and
1583 * that the skb transport header is set correctly.
1585 * Return: The found DHCP message type value, if found. -EINVAL otherwise.
1587 static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
1589 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1596 offset += sizeof(struct batadv_dhcp_packet);
1598 while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
1599 if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
1602 if (tl->type == BATADV_DHCP_OPT_END)
1605 if (tl->type == BATADV_DHCP_OPT_PAD)
1608 offset += tl->len + sizeof(_tl);
1611 /* Option Overload Code not supported */
1612 if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
1613 tl->len != sizeof(_type))
1616 offset += sizeof(_tl);
1618 type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
1626 * batadv_dat_get_dhcp_yiaddr() - get yiaddr from a DHCP packet
1627 * @skb: the DHCP packet to parse
1628 * @buf: a buffer to store the yiaddr in
1630 * Caller needs to ensure that the given skb is a valid DHCP packet and
1631 * that the skb transport header is set correctly.
1633 * Return: True on success, false otherwise.
1635 static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
1637 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1640 offset += offsetof(struct batadv_dhcp_packet, yiaddr);
1641 yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
1647 *buf = get_unaligned(yiaddr);
1653 * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
1654 * @skb: the DHCP packet to parse
1655 * @buf: a buffer to store the chaddr in
1657 * Caller needs to ensure that the given skb is a valid DHCP packet and
1658 * that the skb transport header is set correctly.
1660 * Return: True on success, false otherwise
1662 static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
1664 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1667 offset += offsetof(struct batadv_dhcp_packet, chaddr);
1668 chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
1674 memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
1680 * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
1682 * @bat_priv: the bat priv with all the soft interface information
1683 * @chaddr: the DHCP client MAC address
1684 * @yiaddr: the DHCP client IP address
1685 * @hw_dst: the DHCP server MAC address
1686 * @ip_dst: the DHCP server IP address
1687 * @vid: VLAN identifier
1689 * Adds given MAC/IP pairs to the local DAT cache and propagates them further
1692 * For the DHT propagation, client MAC + IP will appear as the ARP Reply
1693 * transmitter (and hw_dst/ip_dst as the target).
1695 static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
1696 __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
1699 struct sk_buff *skb;
1701 skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
1706 skb_set_network_header(skb, ETH_HLEN);
1708 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1709 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1711 batadv_dat_send_data(bat_priv, skb, yiaddr, vid, BATADV_P_DAT_DHT_PUT);
1712 batadv_dat_send_data(bat_priv, skb, ip_dst, vid, BATADV_P_DAT_DHT_PUT);
1714 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1715 "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1716 &ip_dst, hw_dst, batadv_print_vid(vid));
1717 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1718 "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1719 &yiaddr, chaddr, batadv_print_vid(vid));
1723 * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
1724 * @skb: the packet to check
1725 * @proto: ethernet protocol hint (behind a potential vlan)
1726 * @ip_src: a buffer to store the IPv4 source address in
1727 * @chaddr: a buffer to store the DHCP Client Hardware Address in
1728 * @yiaddr: a buffer to store the DHCP Your IP Address in
1730 * Checks whether the given skb is a valid DHCPACK. And if so, stores the
1731 * IPv4 server source address (ip_src), client MAC address (chaddr) and client
1732 * IPv4 address (yiaddr) in the provided buffers.
1734 * Caller needs to ensure that the skb network header is set correctly.
1736 * Return: True if the skb is a valid DHCPACK. False otherwise.
1739 batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
1740 u8 *chaddr, __be32 *yiaddr)
1744 type = batadv_dat_check_dhcp(skb, proto, ip_src);
1745 if (type != BATADV_BOOTREPLY)
1748 type = batadv_dat_get_dhcp_message_type(skb);
1749 if (type != BATADV_DHCPACK)
1752 if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
1755 if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
1762 * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
1763 * @bat_priv: the bat priv with all the soft interface information
1764 * @skb: the packet to snoop
1765 * @proto: ethernet protocol hint (behind a potential vlan)
1766 * @vid: VLAN identifier
1768 * This function first checks whether the given skb is a valid DHCPACK. If
1769 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1770 * field and DHCP Your IP Address field are added to the local DAT cache and
1771 * propagated into the DHT.
1773 * Caller needs to ensure that the skb mac and network headers are set
1776 void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
1777 struct sk_buff *skb,
1781 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1782 __be32 ip_src, yiaddr;
1784 if (!atomic_read(&bat_priv->distributed_arp_table))
1787 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1790 batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
1795 * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
1796 * @bat_priv: the bat priv with all the soft interface information
1797 * @skb: the packet to snoop
1798 * @hdr_size: header size, up to the tail of the batman-adv header
1800 * This function first checks whether the given skb is a valid DHCPACK. If
1801 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1802 * field and DHCP Your IP Address field are added to the local DAT cache.
1804 void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
1805 struct sk_buff *skb, int hdr_size)
1807 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1808 struct ethhdr *ethhdr;
1809 __be32 ip_src, yiaddr;
1814 if (!atomic_read(&bat_priv->distributed_arp_table))
1817 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1820 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1821 skb_set_network_header(skb, hdr_size + ETH_HLEN);
1822 proto = ethhdr->h_proto;
1824 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1827 hw_src = ethhdr->h_source;
1828 vid = batadv_dat_get_vid(skb, &hdr_size);
1830 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1831 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1833 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1834 "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1835 &ip_src, hw_src, batadv_print_vid(vid));
1836 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1837 "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1838 &yiaddr, chaddr, batadv_print_vid(vid));
1842 * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1843 * dropped (because the node has already obtained the reply via DAT) or not
1844 * @bat_priv: the bat priv with all the soft interface information
1845 * @forw_packet: the broadcast packet
1847 * Return: true if the node can drop the packet, false otherwise.
1849 bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1850 struct batadv_forw_packet *forw_packet)
1854 struct batadv_dat_entry *dat_entry = NULL;
1856 int hdr_size = sizeof(struct batadv_bcast_packet);
1859 if (!atomic_read(&bat_priv->distributed_arp_table))
1862 /* If this packet is an ARP_REQUEST and the node already has the
1863 * information that it is going to ask, then the packet can be dropped
1865 if (batadv_forw_packet_is_rebroadcast(forw_packet))
1868 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1870 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1871 if (type != ARPOP_REQUEST)
1874 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1875 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1876 /* check if the node already got this entry */
1878 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1879 "ARP Request for %pI4: fallback\n", &ip_dst);
1883 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1884 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1889 batadv_dat_entry_put(dat_entry);