1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2011-2019 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/errno.h>
15 #include <linux/etherdevice.h>
16 #include <linux/gfp.h>
17 #include <linux/if_arp.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_vlan.h>
22 #include <linux/jiffies.h>
23 #include <linux/kernel.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/seq_file.h>
30 #include <linux/skbuff.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/stddef.h>
34 #include <linux/string.h>
35 #include <linux/udp.h>
36 #include <linux/workqueue.h>
38 #include <net/genetlink.h>
39 #include <net/netlink.h>
41 #include <uapi/linux/batman_adv.h>
43 #include "bridge_loop_avoidance.h"
44 #include "hard-interface.h"
48 #include "originator.h"
50 #include "soft-interface.h"
51 #include "translation-table.h"
58 enum batadv_boothtype
{
59 BATADV_HTYPE_ETHERNET
= 1,
62 enum batadv_dhcpoptioncode
{
63 BATADV_DHCP_OPT_PAD
= 0,
64 BATADV_DHCP_OPT_MSG_TYPE
= 53,
65 BATADV_DHCP_OPT_END
= 255,
68 enum batadv_dhcptype
{
72 /* { 99, 130, 83, 99 } */
73 #define BATADV_DHCP_MAGIC 1669485411
75 struct batadv_dhcp_packet
{
94 #define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
95 #define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
97 static void batadv_dat_purge(struct work_struct
*work
);
100 * batadv_dat_start_timer() - initialise the DAT periodic worker
101 * @bat_priv: the bat priv with all the soft interface information
103 static void batadv_dat_start_timer(struct batadv_priv
*bat_priv
)
105 INIT_DELAYED_WORK(&bat_priv
->dat
.work
, batadv_dat_purge
);
106 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->dat
.work
,
107 msecs_to_jiffies(10000));
111 * batadv_dat_entry_release() - release dat_entry from lists and queue for free
112 * after rcu grace period
113 * @ref: kref pointer of the dat_entry
115 static void batadv_dat_entry_release(struct kref
*ref
)
117 struct batadv_dat_entry
*dat_entry
;
119 dat_entry
= container_of(ref
, struct batadv_dat_entry
, refcount
);
121 kfree_rcu(dat_entry
, rcu
);
125 * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
127 * @dat_entry: dat_entry to be free'd
129 static void batadv_dat_entry_put(struct batadv_dat_entry
*dat_entry
)
131 kref_put(&dat_entry
->refcount
, batadv_dat_entry_release
);
135 * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
136 * @dat_entry: the entry to check
138 * Return: true if the entry has to be purged now, false otherwise.
140 static bool batadv_dat_to_purge(struct batadv_dat_entry
*dat_entry
)
142 return batadv_has_timed_out(dat_entry
->last_update
,
143 BATADV_DAT_ENTRY_TIMEOUT
);
147 * __batadv_dat_purge() - delete entries from the DAT local storage
148 * @bat_priv: the bat priv with all the soft interface information
149 * @to_purge: function in charge to decide whether an entry has to be purged or
150 * not. This function takes the dat_entry as argument and has to
151 * returns a boolean value: true is the entry has to be deleted,
154 * Loops over each entry in the DAT local storage and deletes it if and only if
155 * the to_purge function passed as argument returns true.
157 static void __batadv_dat_purge(struct batadv_priv
*bat_priv
,
158 bool (*to_purge
)(struct batadv_dat_entry
*))
160 spinlock_t
*list_lock
; /* protects write access to the hash lists */
161 struct batadv_dat_entry
*dat_entry
;
162 struct hlist_node
*node_tmp
;
163 struct hlist_head
*head
;
166 if (!bat_priv
->dat
.hash
)
169 for (i
= 0; i
< bat_priv
->dat
.hash
->size
; i
++) {
170 head
= &bat_priv
->dat
.hash
->table
[i
];
171 list_lock
= &bat_priv
->dat
.hash
->list_locks
[i
];
173 spin_lock_bh(list_lock
);
174 hlist_for_each_entry_safe(dat_entry
, node_tmp
, head
,
176 /* if a helper function has been passed as parameter,
177 * ask it if the entry has to be purged or not
179 if (to_purge
&& !to_purge(dat_entry
))
182 hlist_del_rcu(&dat_entry
->hash_entry
);
183 batadv_dat_entry_put(dat_entry
);
185 spin_unlock_bh(list_lock
);
190 * batadv_dat_purge() - periodic task that deletes old entries from the local
192 * @work: kernel work struct
194 static void batadv_dat_purge(struct work_struct
*work
)
196 struct delayed_work
*delayed_work
;
197 struct batadv_priv_dat
*priv_dat
;
198 struct batadv_priv
*bat_priv
;
200 delayed_work
= to_delayed_work(work
);
201 priv_dat
= container_of(delayed_work
, struct batadv_priv_dat
, work
);
202 bat_priv
= container_of(priv_dat
, struct batadv_priv
, dat
);
204 __batadv_dat_purge(bat_priv
, batadv_dat_to_purge
);
205 batadv_dat_start_timer(bat_priv
);
209 * batadv_compare_dat() - comparing function used in the local DAT hash table
210 * @node: node in the local table
211 * @data2: second object to compare the node to
213 * Return: true if the two entries are the same, false otherwise.
215 static bool batadv_compare_dat(const struct hlist_node
*node
, const void *data2
)
217 const void *data1
= container_of(node
, struct batadv_dat_entry
,
220 return memcmp(data1
, data2
, sizeof(__be32
)) == 0;
224 * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
226 * @hdr_size: size of the possible header before the ARP packet
228 * Return: the value of the hw_src field in the ARP packet.
230 static u8
*batadv_arp_hw_src(struct sk_buff
*skb
, int hdr_size
)
234 addr
= (u8
*)(skb
->data
+ hdr_size
);
235 addr
+= ETH_HLEN
+ sizeof(struct arphdr
);
241 * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
243 * @hdr_size: size of the possible header before the ARP packet
245 * Return: the value of the ip_src field in the ARP packet.
247 static __be32
batadv_arp_ip_src(struct sk_buff
*skb
, int hdr_size
)
249 return *(__be32
*)(batadv_arp_hw_src(skb
, hdr_size
) + ETH_ALEN
);
253 * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
255 * @hdr_size: size of the possible header before the ARP packet
257 * Return: the value of the hw_dst field in the ARP packet.
259 static u8
*batadv_arp_hw_dst(struct sk_buff
*skb
, int hdr_size
)
261 return batadv_arp_hw_src(skb
, hdr_size
) + ETH_ALEN
+ 4;
265 * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
267 * @hdr_size: size of the possible header before the ARP packet
269 * Return: the value of the ip_dst field in the ARP packet.
271 static __be32
batadv_arp_ip_dst(struct sk_buff
*skb
, int hdr_size
)
273 return *(__be32
*)(batadv_arp_hw_src(skb
, hdr_size
) + ETH_ALEN
* 2 + 4);
277 * batadv_hash_dat() - compute the hash value for an IP address
278 * @data: data to hash
279 * @size: size of the hash table
281 * Return: the selected index in the hash table for the given data.
283 static u32
batadv_hash_dat(const void *data
, u32 size
)
286 const struct batadv_dat_entry
*dat
= data
;
287 const unsigned char *key
;
290 key
= (const unsigned char *)&dat
->ip
;
291 for (i
= 0; i
< sizeof(dat
->ip
); i
++) {
293 hash
+= (hash
<< 10);
297 key
= (const unsigned char *)&dat
->vid
;
298 for (i
= 0; i
< sizeof(dat
->vid
); i
++) {
300 hash
+= (hash
<< 10);
305 hash
^= (hash
>> 11);
306 hash
+= (hash
<< 15);
312 * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
314 * @bat_priv: the bat priv with all the soft interface information
316 * @vid: VLAN identifier
318 * Return: the dat_entry if found, NULL otherwise.
320 static struct batadv_dat_entry
*
321 batadv_dat_entry_hash_find(struct batadv_priv
*bat_priv
, __be32 ip
,
324 struct hlist_head
*head
;
325 struct batadv_dat_entry to_find
, *dat_entry
, *dat_entry_tmp
= NULL
;
326 struct batadv_hashtable
*hash
= bat_priv
->dat
.hash
;
335 index
= batadv_hash_dat(&to_find
, hash
->size
);
336 head
= &hash
->table
[index
];
339 hlist_for_each_entry_rcu(dat_entry
, head
, hash_entry
) {
340 if (dat_entry
->ip
!= ip
)
343 if (!kref_get_unless_zero(&dat_entry
->refcount
))
346 dat_entry_tmp
= dat_entry
;
351 return dat_entry_tmp
;
355 * batadv_dat_entry_add() - add a new dat entry or update it if already exists
356 * @bat_priv: the bat priv with all the soft interface information
357 * @ip: ipv4 to add/edit
358 * @mac_addr: mac address to assign to the given ipv4
359 * @vid: VLAN identifier
361 static void batadv_dat_entry_add(struct batadv_priv
*bat_priv
, __be32 ip
,
362 u8
*mac_addr
, unsigned short vid
)
364 struct batadv_dat_entry
*dat_entry
;
367 dat_entry
= batadv_dat_entry_hash_find(bat_priv
, ip
, vid
);
368 /* if this entry is already known, just update it */
370 if (!batadv_compare_eth(dat_entry
->mac_addr
, mac_addr
))
371 ether_addr_copy(dat_entry
->mac_addr
, mac_addr
);
372 dat_entry
->last_update
= jiffies
;
373 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
374 "Entry updated: %pI4 %pM (vid: %d)\n",
375 &dat_entry
->ip
, dat_entry
->mac_addr
,
376 batadv_print_vid(vid
));
380 dat_entry
= kmalloc(sizeof(*dat_entry
), GFP_ATOMIC
);
385 dat_entry
->vid
= vid
;
386 ether_addr_copy(dat_entry
->mac_addr
, mac_addr
);
387 dat_entry
->last_update
= jiffies
;
388 kref_init(&dat_entry
->refcount
);
390 kref_get(&dat_entry
->refcount
);
391 hash_added
= batadv_hash_add(bat_priv
->dat
.hash
, batadv_compare_dat
,
392 batadv_hash_dat
, dat_entry
,
393 &dat_entry
->hash_entry
);
395 if (unlikely(hash_added
!= 0)) {
396 /* remove the reference for the hash */
397 batadv_dat_entry_put(dat_entry
);
401 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "New entry added: %pI4 %pM (vid: %d)\n",
402 &dat_entry
->ip
, dat_entry
->mac_addr
, batadv_print_vid(vid
));
406 batadv_dat_entry_put(dat_entry
);
409 #ifdef CONFIG_BATMAN_ADV_DEBUG
412 * batadv_dbg_arp() - print a debug message containing all the ARP packet
414 * @bat_priv: the bat priv with all the soft interface information
416 * @hdr_size: size of the possible header before the ARP packet
417 * @msg: message to print together with the debugging information
419 static void batadv_dbg_arp(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
420 int hdr_size
, char *msg
)
422 struct batadv_unicast_4addr_packet
*unicast_4addr_packet
;
423 struct batadv_bcast_packet
*bcast_pkt
;
425 __be32 ip_src
, ip_dst
;
428 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "%s\n", msg
);
430 ip_src
= batadv_arp_ip_src(skb
, hdr_size
);
431 ip_dst
= batadv_arp_ip_dst(skb
, hdr_size
);
432 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
433 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
434 batadv_arp_hw_src(skb
, hdr_size
), &ip_src
,
435 batadv_arp_hw_dst(skb
, hdr_size
), &ip_dst
);
437 if (hdr_size
< sizeof(struct batadv_unicast_packet
))
440 unicast_4addr_packet
= (struct batadv_unicast_4addr_packet
*)skb
->data
;
442 switch (unicast_4addr_packet
->u
.packet_type
) {
444 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
445 "* encapsulated within a UNICAST packet\n");
447 case BATADV_UNICAST_4ADDR
:
448 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
449 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
450 unicast_4addr_packet
->src
);
451 switch (unicast_4addr_packet
->subtype
) {
452 case BATADV_P_DAT_DHT_PUT
:
453 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "* type: DAT_DHT_PUT\n");
455 case BATADV_P_DAT_DHT_GET
:
456 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "* type: DAT_DHT_GET\n");
458 case BATADV_P_DAT_CACHE_REPLY
:
459 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
460 "* type: DAT_CACHE_REPLY\n");
463 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "* type: DATA\n");
466 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "* type: Unknown (%u)!\n",
467 unicast_4addr_packet
->u
.packet_type
);
471 bcast_pkt
= (struct batadv_bcast_packet
*)unicast_4addr_packet
;
472 orig_addr
= bcast_pkt
->orig
;
473 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
474 "* encapsulated within a BCAST packet (src: %pM)\n",
478 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
479 "* encapsulated within an unknown packet type (0x%x)\n",
480 unicast_4addr_packet
->u
.packet_type
);
486 static void batadv_dbg_arp(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
487 int hdr_size
, char *msg
)
491 #endif /* CONFIG_BATMAN_ADV_DEBUG */
494 * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
495 * @res: the array with the already selected candidates
496 * @select: number of already selected candidates
497 * @tmp_max: address of the currently evaluated node
498 * @max: current round max address
499 * @last_max: address of the last selected candidate
500 * @candidate: orig_node under evaluation
501 * @max_orig_node: last selected candidate
503 * Return: true if the node has been elected as next candidate or false
506 static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate
*res
,
507 int select
, batadv_dat_addr_t tmp_max
,
508 batadv_dat_addr_t max
,
509 batadv_dat_addr_t last_max
,
510 struct batadv_orig_node
*candidate
,
511 struct batadv_orig_node
*max_orig_node
)
516 /* check if orig node candidate is running DAT */
517 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT
, &candidate
->capabilities
))
520 /* Check if this node has already been selected... */
521 for (j
= 0; j
< select
; j
++)
522 if (res
[j
].orig_node
== candidate
)
524 /* ..and possibly skip it */
527 /* sanity check: has it already been selected? This should not happen */
528 if (tmp_max
> last_max
)
530 /* check if during this iteration an originator with a closer dht
531 * address has already been found
535 /* this is an hash collision with the temporary selected node. Choose
536 * the one with the lowest address
538 if (tmp_max
== max
&& max_orig_node
&&
539 batadv_compare_eth(candidate
->orig
, max_orig_node
->orig
))
548 * batadv_choose_next_candidate() - select the next DHT candidate
549 * @bat_priv: the bat priv with all the soft interface information
550 * @cands: candidates array
551 * @select: number of candidates already present in the array
552 * @ip_key: key to look up in the DHT
553 * @last_max: pointer where the address of the selected candidate will be saved
555 static void batadv_choose_next_candidate(struct batadv_priv
*bat_priv
,
556 struct batadv_dat_candidate
*cands
,
557 int select
, batadv_dat_addr_t ip_key
,
558 batadv_dat_addr_t
*last_max
)
560 batadv_dat_addr_t max
= 0;
561 batadv_dat_addr_t tmp_max
= 0;
562 struct batadv_orig_node
*orig_node
, *max_orig_node
= NULL
;
563 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
564 struct hlist_head
*head
;
567 /* if no node is eligible as candidate, leave the candidate type as
570 cands
[select
].type
= BATADV_DAT_CANDIDATE_NOT_FOUND
;
572 /* iterate over the originator list and find the node with the closest
573 * dat_address which has not been selected yet
575 for (i
= 0; i
< hash
->size
; i
++) {
576 head
= &hash
->table
[i
];
579 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
580 /* the dht space is a ring using unsigned addresses */
581 tmp_max
= BATADV_DAT_ADDR_MAX
- orig_node
->dat_addr
+
584 if (!batadv_is_orig_node_eligible(cands
, select
,
586 *last_max
, orig_node
,
590 if (!kref_get_unless_zero(&orig_node
->refcount
))
595 batadv_orig_node_put(max_orig_node
);
596 max_orig_node
= orig_node
;
601 cands
[select
].type
= BATADV_DAT_CANDIDATE_ORIG
;
602 cands
[select
].orig_node
= max_orig_node
;
603 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
604 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
605 select
, max_orig_node
->orig
, max_orig_node
->dat_addr
,
612 * batadv_dat_select_candidates() - select the nodes which the DHT message has
614 * @bat_priv: the bat priv with all the soft interface information
615 * @ip_dst: ipv4 to look up in the DHT
616 * @vid: VLAN identifier
618 * An originator O is selected if and only if its DHT_ID value is one of three
619 * closest values (from the LEFT, with wrap around if needed) then the hash
620 * value of the key. ip_dst is the key.
622 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
624 static struct batadv_dat_candidate
*
625 batadv_dat_select_candidates(struct batadv_priv
*bat_priv
, __be32 ip_dst
,
629 batadv_dat_addr_t last_max
= BATADV_DAT_ADDR_MAX
, ip_key
;
630 struct batadv_dat_candidate
*res
;
631 struct batadv_dat_entry dat
;
633 if (!bat_priv
->orig_hash
)
636 res
= kmalloc_array(BATADV_DAT_CANDIDATES_NUM
, sizeof(*res
),
643 ip_key
= (batadv_dat_addr_t
)batadv_hash_dat(&dat
,
644 BATADV_DAT_ADDR_MAX
);
646 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
647 "%s(): IP=%pI4 hash(IP)=%u\n", __func__
, &ip_dst
,
650 for (select
= 0; select
< BATADV_DAT_CANDIDATES_NUM
; select
++)
651 batadv_choose_next_candidate(bat_priv
, res
, select
, ip_key
,
658 * batadv_dat_forward_data() - copy and send payload to the selected candidates
659 * @bat_priv: the bat priv with all the soft interface information
660 * @skb: payload to send
662 * @vid: VLAN identifier
663 * @packet_subtype: unicast4addr packet subtype to use
665 * This function copies the skb with pskb_copy() and is sent as unicast packet
666 * to each of the selected candidates.
668 * Return: true if the packet is sent to at least one candidate, false
671 static bool batadv_dat_forward_data(struct batadv_priv
*bat_priv
,
672 struct sk_buff
*skb
, __be32 ip
,
673 unsigned short vid
, int packet_subtype
)
678 struct batadv_neigh_node
*neigh_node
= NULL
;
679 struct sk_buff
*tmp_skb
;
680 struct batadv_dat_candidate
*cand
;
682 cand
= batadv_dat_select_candidates(bat_priv
, ip
, vid
);
686 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "DHT_SEND for %pI4\n", &ip
);
688 for (i
= 0; i
< BATADV_DAT_CANDIDATES_NUM
; i
++) {
689 if (cand
[i
].type
== BATADV_DAT_CANDIDATE_NOT_FOUND
)
692 neigh_node
= batadv_orig_router_get(cand
[i
].orig_node
,
697 tmp_skb
= pskb_copy_for_clone(skb
, GFP_ATOMIC
);
698 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv
, tmp_skb
,
705 send_status
= batadv_send_unicast_skb(tmp_skb
, neigh_node
);
706 if (send_status
== NET_XMIT_SUCCESS
) {
707 /* count the sent packet */
708 switch (packet_subtype
) {
709 case BATADV_P_DAT_DHT_GET
:
710 batadv_inc_counter(bat_priv
,
711 BATADV_CNT_DAT_GET_TX
);
713 case BATADV_P_DAT_DHT_PUT
:
714 batadv_inc_counter(bat_priv
,
715 BATADV_CNT_DAT_PUT_TX
);
719 /* packet sent to a candidate: return true */
723 batadv_neigh_node_put(neigh_node
);
725 batadv_orig_node_put(cand
[i
].orig_node
);
734 * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
736 * @bat_priv: the bat priv with all the soft interface information
738 static void batadv_dat_tvlv_container_update(struct batadv_priv
*bat_priv
)
742 dat_mode
= atomic_read(&bat_priv
->distributed_arp_table
);
746 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_DAT
, 1);
749 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_DAT
, 1,
756 * batadv_dat_status_update() - update the dat tvlv container after dat
758 * @net_dev: the soft interface net device
760 void batadv_dat_status_update(struct net_device
*net_dev
)
762 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
764 batadv_dat_tvlv_container_update(bat_priv
);
768 * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
769 * @bat_priv: the bat priv with all the soft interface information
770 * @orig: the orig_node of the ogm
771 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
772 * @tvlv_value: tvlv buffer containing the gateway data
773 * @tvlv_value_len: tvlv buffer length
775 static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
776 struct batadv_orig_node
*orig
,
778 void *tvlv_value
, u16 tvlv_value_len
)
780 if (flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
)
781 clear_bit(BATADV_ORIG_CAPA_HAS_DAT
, &orig
->capabilities
);
783 set_bit(BATADV_ORIG_CAPA_HAS_DAT
, &orig
->capabilities
);
787 * batadv_dat_hash_free() - free the local DAT hash table
788 * @bat_priv: the bat priv with all the soft interface information
790 static void batadv_dat_hash_free(struct batadv_priv
*bat_priv
)
792 if (!bat_priv
->dat
.hash
)
795 __batadv_dat_purge(bat_priv
, NULL
);
797 batadv_hash_destroy(bat_priv
->dat
.hash
);
799 bat_priv
->dat
.hash
= NULL
;
803 * batadv_dat_init() - initialise the DAT internals
804 * @bat_priv: the bat priv with all the soft interface information
806 * Return: 0 in case of success, a negative error code otherwise
808 int batadv_dat_init(struct batadv_priv
*bat_priv
)
810 if (bat_priv
->dat
.hash
)
813 bat_priv
->dat
.hash
= batadv_hash_new(1024);
815 if (!bat_priv
->dat
.hash
)
818 batadv_dat_start_timer(bat_priv
);
820 batadv_tvlv_handler_register(bat_priv
, batadv_dat_tvlv_ogm_handler_v1
,
821 NULL
, BATADV_TVLV_DAT
, 1,
822 BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
823 batadv_dat_tvlv_container_update(bat_priv
);
828 * batadv_dat_free() - free the DAT internals
829 * @bat_priv: the bat priv with all the soft interface information
831 void batadv_dat_free(struct batadv_priv
*bat_priv
)
833 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_DAT
, 1);
834 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_DAT
, 1);
836 cancel_delayed_work_sync(&bat_priv
->dat
.work
);
838 batadv_dat_hash_free(bat_priv
);
841 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
843 * batadv_dat_cache_seq_print_text() - print the local DAT hash table
844 * @seq: seq file to print on
849 int batadv_dat_cache_seq_print_text(struct seq_file
*seq
, void *offset
)
851 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
852 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
853 struct batadv_hashtable
*hash
= bat_priv
->dat
.hash
;
854 struct batadv_dat_entry
*dat_entry
;
855 struct batadv_hard_iface
*primary_if
;
856 struct hlist_head
*head
;
857 unsigned long last_seen_jiffies
;
858 int last_seen_msecs
, last_seen_secs
, last_seen_mins
;
861 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
865 seq_printf(seq
, "Distributed ARP Table (%s):\n", net_dev
->name
);
867 " IPv4 MAC VID last-seen\n");
869 for (i
= 0; i
< hash
->size
; i
++) {
870 head
= &hash
->table
[i
];
873 hlist_for_each_entry_rcu(dat_entry
, head
, hash_entry
) {
874 last_seen_jiffies
= jiffies
- dat_entry
->last_update
;
875 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
876 last_seen_mins
= last_seen_msecs
/ 60000;
877 last_seen_msecs
= last_seen_msecs
% 60000;
878 last_seen_secs
= last_seen_msecs
/ 1000;
880 seq_printf(seq
, " * %15pI4 %pM %4i %6i:%02i\n",
881 &dat_entry
->ip
, dat_entry
->mac_addr
,
882 batadv_print_vid(dat_entry
->vid
),
883 last_seen_mins
, last_seen_secs
);
890 batadv_hardif_put(primary_if
);
896 * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
898 * @msg: buffer for the message
899 * @portid: netlink port
900 * @cb: Control block containing additional options
901 * @dat_entry: entry to dump
903 * Return: 0 or error code.
906 batadv_dat_cache_dump_entry(struct sk_buff
*msg
, u32 portid
,
907 struct netlink_callback
*cb
,
908 struct batadv_dat_entry
*dat_entry
)
913 hdr
= genlmsg_put(msg
, portid
, cb
->nlh
->nlmsg_seq
,
914 &batadv_netlink_family
, NLM_F_MULTI
,
915 BATADV_CMD_GET_DAT_CACHE
);
919 genl_dump_check_consistent(cb
, hdr
);
921 msecs
= jiffies_to_msecs(jiffies
- dat_entry
->last_update
);
923 if (nla_put_in_addr(msg
, BATADV_ATTR_DAT_CACHE_IP4ADDRESS
,
925 nla_put(msg
, BATADV_ATTR_DAT_CACHE_HWADDRESS
, ETH_ALEN
,
926 dat_entry
->mac_addr
) ||
927 nla_put_u16(msg
, BATADV_ATTR_DAT_CACHE_VID
, dat_entry
->vid
) ||
928 nla_put_u32(msg
, BATADV_ATTR_LAST_SEEN_MSECS
, msecs
)) {
929 genlmsg_cancel(msg
, hdr
);
933 genlmsg_end(msg
, hdr
);
938 * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
940 * @msg: buffer for the message
941 * @portid: netlink port
942 * @cb: Control block containing additional options
943 * @hash: hash to dump
944 * @bucket: bucket index to dump
945 * @idx_skip: How many entries to skip
947 * Return: 0 or error code.
950 batadv_dat_cache_dump_bucket(struct sk_buff
*msg
, u32 portid
,
951 struct netlink_callback
*cb
,
952 struct batadv_hashtable
*hash
, unsigned int bucket
,
955 struct batadv_dat_entry
*dat_entry
;
958 spin_lock_bh(&hash
->list_locks
[bucket
]);
959 cb
->seq
= atomic_read(&hash
->generation
) << 1 | 1;
961 hlist_for_each_entry(dat_entry
, &hash
->table
[bucket
], hash_entry
) {
965 if (batadv_dat_cache_dump_entry(msg
, portid
, cb
, dat_entry
)) {
966 spin_unlock_bh(&hash
->list_locks
[bucket
]);
975 spin_unlock_bh(&hash
->list_locks
[bucket
]);
981 * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
982 * @msg: buffer for the message
983 * @cb: callback structure containing arguments
985 * Return: message length.
987 int batadv_dat_cache_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
)
989 struct batadv_hard_iface
*primary_if
= NULL
;
990 int portid
= NETLINK_CB(cb
->skb
).portid
;
991 struct net
*net
= sock_net(cb
->skb
->sk
);
992 struct net_device
*soft_iface
;
993 struct batadv_hashtable
*hash
;
994 struct batadv_priv
*bat_priv
;
995 int bucket
= cb
->args
[0];
996 int idx
= cb
->args
[1];
1000 ifindex
= batadv_netlink_get_ifindex(cb
->nlh
,
1001 BATADV_ATTR_MESH_IFINDEX
);
1005 soft_iface
= dev_get_by_index(net
, ifindex
);
1006 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
1011 bat_priv
= netdev_priv(soft_iface
);
1012 hash
= bat_priv
->dat
.hash
;
1014 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1015 if (!primary_if
|| primary_if
->if_status
!= BATADV_IF_ACTIVE
) {
1020 while (bucket
< hash
->size
) {
1021 if (batadv_dat_cache_dump_bucket(msg
, portid
, cb
, hash
, bucket
,
1029 cb
->args
[0] = bucket
;
1036 batadv_hardif_put(primary_if
);
1039 dev_put(soft_iface
);
1045 * batadv_arp_get_type() - parse an ARP packet and gets the type
1046 * @bat_priv: the bat priv with all the soft interface information
1047 * @skb: packet to analyse
1048 * @hdr_size: size of the possible header before the ARP packet in the skb
1050 * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
1052 static u16
batadv_arp_get_type(struct batadv_priv
*bat_priv
,
1053 struct sk_buff
*skb
, int hdr_size
)
1055 struct arphdr
*arphdr
;
1056 struct ethhdr
*ethhdr
;
1057 __be32 ip_src
, ip_dst
;
1058 u8
*hw_src
, *hw_dst
;
1061 /* pull the ethernet header */
1062 if (unlikely(!pskb_may_pull(skb
, hdr_size
+ ETH_HLEN
)))
1065 ethhdr
= (struct ethhdr
*)(skb
->data
+ hdr_size
);
1067 if (ethhdr
->h_proto
!= htons(ETH_P_ARP
))
1070 /* pull the ARP payload */
1071 if (unlikely(!pskb_may_pull(skb
, hdr_size
+ ETH_HLEN
+
1072 arp_hdr_len(skb
->dev
))))
1075 arphdr
= (struct arphdr
*)(skb
->data
+ hdr_size
+ ETH_HLEN
);
1077 /* check whether the ARP packet carries a valid IP information */
1078 if (arphdr
->ar_hrd
!= htons(ARPHRD_ETHER
))
1081 if (arphdr
->ar_pro
!= htons(ETH_P_IP
))
1084 if (arphdr
->ar_hln
!= ETH_ALEN
)
1087 if (arphdr
->ar_pln
!= 4)
1090 /* Check for bad reply/request. If the ARP message is not sane, DAT
1091 * will simply ignore it
1093 ip_src
= batadv_arp_ip_src(skb
, hdr_size
);
1094 ip_dst
= batadv_arp_ip_dst(skb
, hdr_size
);
1095 if (ipv4_is_loopback(ip_src
) || ipv4_is_multicast(ip_src
) ||
1096 ipv4_is_loopback(ip_dst
) || ipv4_is_multicast(ip_dst
) ||
1097 ipv4_is_zeronet(ip_src
) || ipv4_is_lbcast(ip_src
) ||
1098 ipv4_is_zeronet(ip_dst
) || ipv4_is_lbcast(ip_dst
))
1101 hw_src
= batadv_arp_hw_src(skb
, hdr_size
);
1102 if (is_zero_ether_addr(hw_src
) || is_multicast_ether_addr(hw_src
))
1105 /* don't care about the destination MAC address in ARP requests */
1106 if (arphdr
->ar_op
!= htons(ARPOP_REQUEST
)) {
1107 hw_dst
= batadv_arp_hw_dst(skb
, hdr_size
);
1108 if (is_zero_ether_addr(hw_dst
) ||
1109 is_multicast_ether_addr(hw_dst
))
1113 type
= ntohs(arphdr
->ar_op
);
1119 * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
1120 * @skb: the buffer containing the packet to extract the VID from
1121 * @hdr_size: the size of the batman-adv header encapsulating the packet
1123 * Return: If the packet embedded in the skb is vlan tagged this function
1124 * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1127 static unsigned short batadv_dat_get_vid(struct sk_buff
*skb
, int *hdr_size
)
1131 vid
= batadv_get_vid(skb
, *hdr_size
);
1133 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1134 * If the header contained in the packet is a VLAN one (which is longer)
1135 * hdr_size is updated so that the functions will still skip the
1136 * correct amount of bytes.
1138 if (vid
& BATADV_VLAN_HAS_TAG
)
1139 *hdr_size
+= VLAN_HLEN
;
1145 * batadv_dat_arp_create_reply() - create an ARP Reply
1146 * @bat_priv: the bat priv with all the soft interface information
1147 * @ip_src: ARP sender IP
1148 * @ip_dst: ARP target IP
1149 * @hw_src: Ethernet source and ARP sender MAC
1150 * @hw_dst: Ethernet destination and ARP target MAC
1151 * @vid: VLAN identifier (optional, set to zero otherwise)
1153 * Creates an ARP Reply from the given values, optionally encapsulated in a
1156 * Return: An skb containing an ARP Reply.
1158 static struct sk_buff
*
1159 batadv_dat_arp_create_reply(struct batadv_priv
*bat_priv
, __be32 ip_src
,
1160 __be32 ip_dst
, u8
*hw_src
, u8
*hw_dst
,
1163 struct sk_buff
*skb
;
1165 skb
= arp_create(ARPOP_REPLY
, ETH_P_ARP
, ip_dst
, bat_priv
->soft_iface
,
1166 ip_src
, hw_dst
, hw_src
, hw_dst
);
1170 skb_reset_mac_header(skb
);
1172 if (vid
& BATADV_VLAN_HAS_TAG
)
1173 skb
= vlan_insert_tag(skb
, htons(ETH_P_8021Q
),
1174 vid
& VLAN_VID_MASK
);
1180 * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
1182 * @bat_priv: the bat priv with all the soft interface information
1183 * @skb: packet to check
1185 * Return: true if the message has been sent to the dht candidates, false
1186 * otherwise. In case of a positive return value the message has to be enqueued
1187 * to permit the fallback.
1189 bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv
*bat_priv
,
1190 struct sk_buff
*skb
)
1193 __be32 ip_dst
, ip_src
;
1196 struct batadv_dat_entry
*dat_entry
= NULL
;
1197 struct sk_buff
*skb_new
;
1198 struct net_device
*soft_iface
= bat_priv
->soft_iface
;
1202 if (!atomic_read(&bat_priv
->distributed_arp_table
))
1205 vid
= batadv_dat_get_vid(skb
, &hdr_size
);
1207 type
= batadv_arp_get_type(bat_priv
, skb
, hdr_size
);
1208 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1209 * message to the selected DHT candidates
1211 if (type
!= ARPOP_REQUEST
)
1214 batadv_dbg_arp(bat_priv
, skb
, hdr_size
, "Parsing outgoing ARP REQUEST");
1216 ip_src
= batadv_arp_ip_src(skb
, hdr_size
);
1217 hw_src
= batadv_arp_hw_src(skb
, hdr_size
);
1218 ip_dst
= batadv_arp_ip_dst(skb
, hdr_size
);
1220 batadv_dat_entry_add(bat_priv
, ip_src
, hw_src
, vid
);
1222 dat_entry
= batadv_dat_entry_hash_find(bat_priv
, ip_dst
, vid
);
1224 /* If the ARP request is destined for a local client the local
1225 * client will answer itself. DAT would only generate a
1228 * Moreover, if the soft-interface is enslaved into a bridge, an
1229 * additional DAT answer may trigger kernel warnings about
1230 * a packet coming from the wrong port.
1232 if (batadv_is_my_client(bat_priv
, dat_entry
->mac_addr
, vid
)) {
1237 /* If BLA is enabled, only send ARP replies if we have claimed
1238 * the destination for the ARP request or if no one else of
1239 * the backbone gws belonging to our backbone has claimed the
1242 if (!batadv_bla_check_claim(bat_priv
,
1243 dat_entry
->mac_addr
, vid
)) {
1244 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
1245 "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1246 dat_entry
->mac_addr
);
1251 skb_new
= batadv_dat_arp_create_reply(bat_priv
, ip_dst
, ip_src
,
1252 dat_entry
->mac_addr
,
1257 skb_new
->protocol
= eth_type_trans(skb_new
, soft_iface
);
1259 batadv_inc_counter(bat_priv
, BATADV_CNT_RX
);
1260 batadv_add_counter(bat_priv
, BATADV_CNT_RX_BYTES
,
1261 skb
->len
+ ETH_HLEN
+ hdr_size
);
1264 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "ARP request replied locally\n");
1267 /* Send the request to the DHT */
1268 ret
= batadv_dat_forward_data(bat_priv
, skb
, ip_dst
, vid
,
1269 BATADV_P_DAT_DHT_GET
);
1273 batadv_dat_entry_put(dat_entry
);
1278 * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
1279 * answer using the local DAT storage
1280 * @bat_priv: the bat priv with all the soft interface information
1281 * @skb: packet to check
1282 * @hdr_size: size of the encapsulation header
1284 * Return: true if the request has been answered, false otherwise.
1286 bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv
*bat_priv
,
1287 struct sk_buff
*skb
, int hdr_size
)
1290 __be32 ip_src
, ip_dst
;
1292 struct sk_buff
*skb_new
;
1293 struct batadv_dat_entry
*dat_entry
= NULL
;
1298 if (!atomic_read(&bat_priv
->distributed_arp_table
))
1301 vid
= batadv_dat_get_vid(skb
, &hdr_size
);
1303 type
= batadv_arp_get_type(bat_priv
, skb
, hdr_size
);
1304 if (type
!= ARPOP_REQUEST
)
1307 hw_src
= batadv_arp_hw_src(skb
, hdr_size
);
1308 ip_src
= batadv_arp_ip_src(skb
, hdr_size
);
1309 ip_dst
= batadv_arp_ip_dst(skb
, hdr_size
);
1311 batadv_dbg_arp(bat_priv
, skb
, hdr_size
, "Parsing incoming ARP REQUEST");
1313 batadv_dat_entry_add(bat_priv
, ip_src
, hw_src
, vid
);
1315 dat_entry
= batadv_dat_entry_hash_find(bat_priv
, ip_dst
, vid
);
1319 skb_new
= batadv_dat_arp_create_reply(bat_priv
, ip_dst
, ip_src
,
1320 dat_entry
->mac_addr
, hw_src
, vid
);
1324 /* To preserve backwards compatibility, the node has choose the outgoing
1325 * format based on the incoming request packet type. The assumption is
1326 * that a node not using the 4addr packet format doesn't support it.
1328 if (hdr_size
== sizeof(struct batadv_unicast_4addr_packet
))
1329 err
= batadv_send_skb_via_tt_4addr(bat_priv
, skb_new
,
1330 BATADV_P_DAT_CACHE_REPLY
,
1333 err
= batadv_send_skb_via_tt(bat_priv
, skb_new
, NULL
, vid
);
1335 if (err
!= NET_XMIT_DROP
) {
1336 batadv_inc_counter(bat_priv
, BATADV_CNT_DAT_CACHED_REPLY_TX
);
1341 batadv_dat_entry_put(dat_entry
);
1348 * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
1349 * @bat_priv: the bat priv with all the soft interface information
1350 * @skb: packet to check
1352 void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv
*bat_priv
,
1353 struct sk_buff
*skb
)
1356 __be32 ip_src
, ip_dst
;
1357 u8
*hw_src
, *hw_dst
;
1361 if (!atomic_read(&bat_priv
->distributed_arp_table
))
1364 vid
= batadv_dat_get_vid(skb
, &hdr_size
);
1366 type
= batadv_arp_get_type(bat_priv
, skb
, hdr_size
);
1367 if (type
!= ARPOP_REPLY
)
1370 batadv_dbg_arp(bat_priv
, skb
, hdr_size
, "Parsing outgoing ARP REPLY");
1372 hw_src
= batadv_arp_hw_src(skb
, hdr_size
);
1373 ip_src
= batadv_arp_ip_src(skb
, hdr_size
);
1374 hw_dst
= batadv_arp_hw_dst(skb
, hdr_size
);
1375 ip_dst
= batadv_arp_ip_dst(skb
, hdr_size
);
1377 batadv_dat_entry_add(bat_priv
, ip_src
, hw_src
, vid
);
1378 batadv_dat_entry_add(bat_priv
, ip_dst
, hw_dst
, vid
);
1380 /* Send the ARP reply to the candidates for both the IP addresses that
1381 * the node obtained from the ARP reply
1383 batadv_dat_forward_data(bat_priv
, skb
, ip_src
, vid
,
1384 BATADV_P_DAT_DHT_PUT
);
1385 batadv_dat_forward_data(bat_priv
, skb
, ip_dst
, vid
,
1386 BATADV_P_DAT_DHT_PUT
);
1390 * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1391 * local DAT storage only
1392 * @bat_priv: the bat priv with all the soft interface information
1393 * @skb: packet to check
1394 * @hdr_size: size of the encapsulation header
1396 * Return: true if the packet was snooped and consumed by DAT. False if the
1397 * packet has to be delivered to the interface
1399 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv
*bat_priv
,
1400 struct sk_buff
*skb
, int hdr_size
)
1402 struct batadv_dat_entry
*dat_entry
= NULL
;
1404 __be32 ip_src
, ip_dst
;
1405 u8
*hw_src
, *hw_dst
;
1406 bool dropped
= false;
1409 if (!atomic_read(&bat_priv
->distributed_arp_table
))
1412 vid
= batadv_dat_get_vid(skb
, &hdr_size
);
1414 type
= batadv_arp_get_type(bat_priv
, skb
, hdr_size
);
1415 if (type
!= ARPOP_REPLY
)
1418 batadv_dbg_arp(bat_priv
, skb
, hdr_size
, "Parsing incoming ARP REPLY");
1420 hw_src
= batadv_arp_hw_src(skb
, hdr_size
);
1421 ip_src
= batadv_arp_ip_src(skb
, hdr_size
);
1422 hw_dst
= batadv_arp_hw_dst(skb
, hdr_size
);
1423 ip_dst
= batadv_arp_ip_dst(skb
, hdr_size
);
1425 /* If ip_dst is already in cache and has the right mac address,
1426 * drop this frame if this ARP reply is destined for us because it's
1427 * most probably an ARP reply generated by another node of the DHT.
1428 * We have most probably received already a reply earlier. Delivering
1429 * this frame would lead to doubled receive of an ARP reply.
1431 dat_entry
= batadv_dat_entry_hash_find(bat_priv
, ip_src
, vid
);
1432 if (dat_entry
&& batadv_compare_eth(hw_src
, dat_entry
->mac_addr
)) {
1433 batadv_dbg(BATADV_DBG_DAT
, bat_priv
, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1434 hw_src
, &ip_src
, hw_dst
, &ip_dst
,
1435 dat_entry
->mac_addr
, &dat_entry
->ip
);
1439 /* Update our internal cache with both the IP addresses the node got
1440 * within the ARP reply
1442 batadv_dat_entry_add(bat_priv
, ip_src
, hw_src
, vid
);
1443 batadv_dat_entry_add(bat_priv
, ip_dst
, hw_dst
, vid
);
1448 /* If BLA is enabled, only forward ARP replies if we have claimed the
1449 * source of the ARP reply or if no one else of the same backbone has
1450 * already claimed that client. This prevents that different gateways
1451 * to the same backbone all forward the ARP reply leading to multiple
1452 * replies in the backbone.
1454 if (!batadv_bla_check_claim(bat_priv
, hw_src
, vid
)) {
1455 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
1456 "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1462 /* if this REPLY is directed to a client of mine, let's deliver the
1463 * packet to the interface
1465 dropped
= !batadv_is_my_client(bat_priv
, hw_dst
, vid
);
1467 /* if this REPLY is sent on behalf of a client of mine, let's drop the
1468 * packet because the client will reply by itself
1470 dropped
|= batadv_is_my_client(bat_priv
, hw_src
, vid
);
1475 batadv_dat_entry_put(dat_entry
);
1476 /* if dropped == false -> deliver to the interface */
1481 * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
1482 * @skb: the packet to check
1483 * @ip_src: a buffer to store the IPv4 source address in
1485 * Checks whether the given skb has an IP and UDP header valid for a DHCP
1486 * message from a DHCP server. And if so, stores the IPv4 source address in
1487 * the provided buffer.
1489 * Return: True if valid, false otherwise.
1492 batadv_dat_check_dhcp_ipudp(struct sk_buff
*skb
, __be32
*ip_src
)
1494 unsigned int offset
= skb_network_offset(skb
);
1495 struct udphdr
*udphdr
, _udphdr
;
1496 struct iphdr
*iphdr
, _iphdr
;
1498 iphdr
= skb_header_pointer(skb
, offset
, sizeof(_iphdr
), &_iphdr
);
1499 if (!iphdr
|| iphdr
->version
!= 4 || iphdr
->ihl
* 4 < sizeof(_iphdr
))
1502 if (iphdr
->protocol
!= IPPROTO_UDP
)
1505 offset
+= iphdr
->ihl
* 4;
1506 skb_set_transport_header(skb
, offset
);
1508 udphdr
= skb_header_pointer(skb
, offset
, sizeof(_udphdr
), &_udphdr
);
1509 if (!udphdr
|| udphdr
->source
!= htons(67))
1512 *ip_src
= get_unaligned(&iphdr
->saddr
);
1518 * batadv_dat_check_dhcp() - examine packet for valid DHCP message
1519 * @skb: the packet to check
1520 * @proto: ethernet protocol hint (behind a potential vlan)
1521 * @ip_src: a buffer to store the IPv4 source address in
1523 * Checks whether the given skb is a valid DHCP packet. And if so, stores the
1524 * IPv4 source address in the provided buffer.
1526 * Caller needs to ensure that the skb network header is set correctly.
1528 * Return: If skb is a valid DHCP packet, then returns its op code
1529 * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
1532 batadv_dat_check_dhcp(struct sk_buff
*skb
, __be16 proto
, __be32
*ip_src
)
1534 __be32
*magic
, _magic
;
1535 unsigned int offset
;
1543 if (proto
!= htons(ETH_P_IP
))
1546 if (!batadv_dat_check_dhcp_ipudp(skb
, ip_src
))
1549 offset
= skb_transport_offset(skb
) + sizeof(struct udphdr
);
1550 if (skb
->len
< offset
+ sizeof(struct batadv_dhcp_packet
))
1553 dhcp_h
= skb_header_pointer(skb
, offset
, sizeof(_dhcp_h
), &_dhcp_h
);
1554 if (!dhcp_h
|| dhcp_h
->htype
!= BATADV_HTYPE_ETHERNET
||
1555 dhcp_h
->hlen
!= ETH_ALEN
)
1558 offset
+= offsetof(struct batadv_dhcp_packet
, magic
);
1560 magic
= skb_header_pointer(skb
, offset
, sizeof(_magic
), &_magic
);
1561 if (!magic
|| get_unaligned(magic
) != htonl(BATADV_DHCP_MAGIC
))
1568 * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
1569 * @skb: the DHCP packet to parse
1571 * Iterates over the DHCP options of the given DHCP packet to find a
1572 * DHCP Message Type option and parse it.
1574 * Caller needs to ensure that the given skb is a valid DHCP packet and
1575 * that the skb transport header is set correctly.
1577 * Return: The found DHCP message type value, if found. -EINVAL otherwise.
1579 static int batadv_dat_get_dhcp_message_type(struct sk_buff
*skb
)
1581 unsigned int offset
= skb_transport_offset(skb
) + sizeof(struct udphdr
);
1588 offset
+= sizeof(struct batadv_dhcp_packet
);
1590 while ((tl
= skb_header_pointer(skb
, offset
, sizeof(_tl
), &_tl
))) {
1591 if (tl
->type
== BATADV_DHCP_OPT_MSG_TYPE
)
1594 if (tl
->type
== BATADV_DHCP_OPT_END
)
1597 if (tl
->type
== BATADV_DHCP_OPT_PAD
)
1600 offset
+= tl
->len
+ sizeof(_tl
);
1603 /* Option Overload Code not supported */
1604 if (!tl
|| tl
->type
!= BATADV_DHCP_OPT_MSG_TYPE
||
1605 tl
->len
!= sizeof(_type
))
1608 offset
+= sizeof(_tl
);
1610 type
= skb_header_pointer(skb
, offset
, sizeof(_type
), &_type
);
1618 * batadv_dat_get_dhcp_yiaddr() - get yiaddr from a DHCP packet
1619 * @skb: the DHCP packet to parse
1620 * @buf: a buffer to store the yiaddr in
1622 * Caller needs to ensure that the given skb is a valid DHCP packet and
1623 * that the skb transport header is set correctly.
1625 * Return: True on success, false otherwise.
1627 static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff
*skb
, __be32
*buf
)
1629 unsigned int offset
= skb_transport_offset(skb
) + sizeof(struct udphdr
);
1632 offset
+= offsetof(struct batadv_dhcp_packet
, yiaddr
);
1633 yiaddr
= skb_header_pointer(skb
, offset
, BATADV_DHCP_YIADDR_LEN
, buf
);
1639 *buf
= get_unaligned(yiaddr
);
1645 * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
1646 * @skb: the DHCP packet to parse
1647 * @buf: a buffer to store the chaddr in
1649 * Caller needs to ensure that the given skb is a valid DHCP packet and
1650 * that the skb transport header is set correctly.
1652 * Return: True on success, false otherwise
1654 static bool batadv_dat_get_dhcp_chaddr(struct sk_buff
*skb
, u8
*buf
)
1656 unsigned int offset
= skb_transport_offset(skb
) + sizeof(struct udphdr
);
1659 offset
+= offsetof(struct batadv_dhcp_packet
, chaddr
);
1660 chaddr
= skb_header_pointer(skb
, offset
, BATADV_DHCP_CHADDR_LEN
, buf
);
1666 memcpy(buf
, chaddr
, BATADV_DHCP_CHADDR_LEN
);
1672 * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
1674 * @bat_priv: the bat priv with all the soft interface information
1675 * @chaddr: the DHCP client MAC address
1676 * @yiaddr: the DHCP client IP address
1677 * @hw_dst: the DHCP server MAC address
1678 * @ip_dst: the DHCP server IP address
1679 * @vid: VLAN identifier
1681 * Adds given MAC/IP pairs to the local DAT cache and propagates them further
1684 * For the DHT propagation, client MAC + IP will appear as the ARP Reply
1685 * transmitter (and hw_dst/ip_dst as the target).
1687 static void batadv_dat_put_dhcp(struct batadv_priv
*bat_priv
, u8
*chaddr
,
1688 __be32 yiaddr
, u8
*hw_dst
, __be32 ip_dst
,
1691 struct sk_buff
*skb
;
1693 skb
= batadv_dat_arp_create_reply(bat_priv
, yiaddr
, ip_dst
, chaddr
,
1698 skb_set_network_header(skb
, ETH_HLEN
);
1700 batadv_dat_entry_add(bat_priv
, yiaddr
, chaddr
, vid
);
1701 batadv_dat_entry_add(bat_priv
, ip_dst
, hw_dst
, vid
);
1703 batadv_dat_forward_data(bat_priv
, skb
, yiaddr
, vid
,
1704 BATADV_P_DAT_DHT_PUT
);
1705 batadv_dat_forward_data(bat_priv
, skb
, ip_dst
, vid
,
1706 BATADV_P_DAT_DHT_PUT
);
1710 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
1711 "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1712 &ip_dst
, hw_dst
, batadv_print_vid(vid
));
1713 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
1714 "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1715 &yiaddr
, chaddr
, batadv_print_vid(vid
));
1719 * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
1720 * @skb: the packet to check
1721 * @proto: ethernet protocol hint (behind a potential vlan)
1722 * @ip_src: a buffer to store the IPv4 source address in
1723 * @chaddr: a buffer to store the DHCP Client Hardware Address in
1724 * @yiaddr: a buffer to store the DHCP Your IP Address in
1726 * Checks whether the given skb is a valid DHCPACK. And if so, stores the
1727 * IPv4 server source address (ip_src), client MAC address (chaddr) and client
1728 * IPv4 address (yiaddr) in the provided buffers.
1730 * Caller needs to ensure that the skb network header is set correctly.
1732 * Return: True if the skb is a valid DHCPACK. False otherwise.
1735 batadv_dat_check_dhcp_ack(struct sk_buff
*skb
, __be16 proto
, __be32
*ip_src
,
1736 u8
*chaddr
, __be32
*yiaddr
)
1740 type
= batadv_dat_check_dhcp(skb
, proto
, ip_src
);
1741 if (type
!= BATADV_BOOTREPLY
)
1744 type
= batadv_dat_get_dhcp_message_type(skb
);
1745 if (type
!= BATADV_DHCPACK
)
1748 if (!batadv_dat_dhcp_get_yiaddr(skb
, yiaddr
))
1751 if (!batadv_dat_get_dhcp_chaddr(skb
, chaddr
))
1758 * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
1759 * @bat_priv: the bat priv with all the soft interface information
1760 * @skb: the packet to snoop
1761 * @proto: ethernet protocol hint (behind a potential vlan)
1762 * @vid: VLAN identifier
1764 * This function first checks whether the given skb is a valid DHCPACK. If
1765 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1766 * field and DHCP Your IP Address field are added to the local DAT cache and
1767 * propagated into the DHT.
1769 * Caller needs to ensure that the skb mac and network headers are set
1772 void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv
*bat_priv
,
1773 struct sk_buff
*skb
,
1777 u8 chaddr
[BATADV_DHCP_CHADDR_LEN
];
1778 __be32 ip_src
, yiaddr
;
1780 if (!atomic_read(&bat_priv
->distributed_arp_table
))
1783 if (!batadv_dat_check_dhcp_ack(skb
, proto
, &ip_src
, chaddr
, &yiaddr
))
1786 batadv_dat_put_dhcp(bat_priv
, chaddr
, yiaddr
, eth_hdr(skb
)->h_source
,
1791 * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
1792 * @bat_priv: the bat priv with all the soft interface information
1793 * @skb: the packet to snoop
1794 * @hdr_size: header size, up to the tail of the batman-adv header
1796 * This function first checks whether the given skb is a valid DHCPACK. If
1797 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1798 * field and DHCP Your IP Address field are added to the local DAT cache.
1800 void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv
*bat_priv
,
1801 struct sk_buff
*skb
, int hdr_size
)
1803 u8 chaddr
[BATADV_DHCP_CHADDR_LEN
];
1804 struct ethhdr
*ethhdr
;
1805 __be32 ip_src
, yiaddr
;
1810 if (!atomic_read(&bat_priv
->distributed_arp_table
))
1813 if (unlikely(!pskb_may_pull(skb
, hdr_size
+ ETH_HLEN
)))
1816 ethhdr
= (struct ethhdr
*)(skb
->data
+ hdr_size
);
1817 skb_set_network_header(skb
, hdr_size
+ ETH_HLEN
);
1818 proto
= ethhdr
->h_proto
;
1820 if (!batadv_dat_check_dhcp_ack(skb
, proto
, &ip_src
, chaddr
, &yiaddr
))
1823 hw_src
= ethhdr
->h_source
;
1824 vid
= batadv_dat_get_vid(skb
, &hdr_size
);
1826 batadv_dat_entry_add(bat_priv
, yiaddr
, chaddr
, vid
);
1827 batadv_dat_entry_add(bat_priv
, ip_src
, hw_src
, vid
);
1829 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
1830 "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1831 &ip_src
, hw_src
, batadv_print_vid(vid
));
1832 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
1833 "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1834 &yiaddr
, chaddr
, batadv_print_vid(vid
));
1838 * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1839 * dropped (because the node has already obtained the reply via DAT) or not
1840 * @bat_priv: the bat priv with all the soft interface information
1841 * @forw_packet: the broadcast packet
1843 * Return: true if the node can drop the packet, false otherwise.
1845 bool batadv_dat_drop_broadcast_packet(struct batadv_priv
*bat_priv
,
1846 struct batadv_forw_packet
*forw_packet
)
1850 struct batadv_dat_entry
*dat_entry
= NULL
;
1852 int hdr_size
= sizeof(struct batadv_bcast_packet
);
1855 if (!atomic_read(&bat_priv
->distributed_arp_table
))
1858 /* If this packet is an ARP_REQUEST and the node already has the
1859 * information that it is going to ask, then the packet can be dropped
1861 if (batadv_forw_packet_is_rebroadcast(forw_packet
))
1864 vid
= batadv_dat_get_vid(forw_packet
->skb
, &hdr_size
);
1866 type
= batadv_arp_get_type(bat_priv
, forw_packet
->skb
, hdr_size
);
1867 if (type
!= ARPOP_REQUEST
)
1870 ip_dst
= batadv_arp_ip_dst(forw_packet
->skb
, hdr_size
);
1871 dat_entry
= batadv_dat_entry_hash_find(bat_priv
, ip_dst
, vid
);
1872 /* check if the node already got this entry */
1874 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
1875 "ARP Request for %pI4: fallback\n", &ip_dst
);
1879 batadv_dbg(BATADV_DBG_DAT
, bat_priv
,
1880 "ARP Request for %pI4: fallback prevented\n", &ip_dst
);
1885 batadv_dat_entry_put(dat_entry
);