1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3 *
4 * Antonio Quartulli
5 */
6
7 #include "distributed-arp-table.h"
8 #include "main.h"
9
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/bug.h>
13 #include <linux/byteorder/generic.h>
14 #include <linux/container_of.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/etherdevice.h>
18 #include <linux/gfp.h>
19 #include <linux/if_arp.h>
20 #include <linux/if_ether.h>
21 #include <linux/if_vlan.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/jiffies.h>
25 #include <linux/kref.h>
26 #include <linux/list.h>
27 #include <linux/netlink.h>
28 #include <linux/rculist.h>
29 #include <linux/rcupdate.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/unaligned.h>
37 #include <linux/workqueue.h>
38 #include <net/arp.h>
39 #include <net/genetlink.h>
40 #include <net/netlink.h>
41 #include <uapi/linux/batman_adv.h>
42
43 #include "bridge_loop_avoidance.h"
44 #include "hard-interface.h"
45 #include "hash.h"
46 #include "log.h"
47 #include "netlink.h"
48 #include "originator.h"
49 #include "send.h"
50 #include "translation-table.h"
51 #include "tvlv.h"
52
53 /**
54 * enum batadv_bootpop - BOOTP/DHCP message op codes
55 */
56 enum batadv_bootpop {
57 /** @BATADV_BOOTREPLY: server-to-client reply */
58 BATADV_BOOTREPLY = 2,
59 };
60
61 /**
62 * enum batadv_boothtype - BOOTP/DHCP hardware address types
63 */
64 enum batadv_boothtype {
65 /** @BATADV_HTYPE_ETHERNET: Ethernet (10Mb) */
66 BATADV_HTYPE_ETHERNET = 1,
67 };
68
69 /**
70 * enum batadv_dhcpoptioncode - DHCP option codes relevant for batman-adv DAT
71 */
72 enum batadv_dhcpoptioncode {
73 /** @BATADV_DHCP_OPT_PAD: pad option */
74 BATADV_DHCP_OPT_PAD = 0,
75
76 /** @BATADV_DHCP_OPT_MSG_TYPE: DHCP message type option */
77 BATADV_DHCP_OPT_MSG_TYPE = 53,
78
79 /** @BATADV_DHCP_OPT_END: end of options marker */
80 BATADV_DHCP_OPT_END = 255,
81 };
82
83 /**
84 * enum batadv_dhcptype - DHCP message types relevant for batman-adv DAT
85 */
86 enum batadv_dhcptype {
87 /** @BATADV_DHCPACK: DHCPACK message */
88 BATADV_DHCPACK = 5,
89 };
90
91 /* { 99, 130, 83, 99 } */
92 #define BATADV_DHCP_MAGIC 1669485411
93
94 /**
95 * struct batadv_dhcp_packet - BOOTP/DHCP packet header
96 * @op: message op code / message type
97 * @htype: hardware address type
98 * @hlen: hardware address length
99 * @hops: number of relay hops
100 * @xid: transaction identifier
101 * @secs: seconds elapsed since client started trying to boot
102 * @flags: BOOTP/DHCP flags
103 * @ciaddr: client IP address
104 * @yiaddr: "your" (client) IP address as assigned by the server
105 * @siaddr: IP address of next server to use in bootstrap
106 * @giaddr: relay agent IP address
107 * @chaddr: client hardware address
108 * @sname: optional server host name
109 * @file: boot file name
110 * @magic: BOOTP/DHCP magic cookie identifying the start of options
111 */
112 struct batadv_dhcp_packet {
113 __u8 op;
114 __u8 htype;
115 __u8 hlen;
116 __u8 hops;
117 __be32 xid;
118 __be16 secs;
119 __be16 flags;
120 __be32 ciaddr;
121 __be32 yiaddr;
122 __be32 siaddr;
123 __be32 giaddr;
124 __u8 chaddr[16];
125 __u8 sname[64];
126 __u8 file[128];
127 __be32 magic;
128 /* __u8 options[]; */
129 };
130
131 /**
132 * struct batadv_dhcp_header - minimal BOOTP/DHCP packet header
133 */
134 struct batadv_dhcp_header {
135 /** @op: message op code / message type */
136 __u8 op;
137
138 /** @htype: hardware address type */
139 __u8 htype;
140
141 /** @hlen: hardware address length */
142 __u8 hlen;
143
144 /** @hops: number of relay hops */
145 __u8 hops;
146 };
147
148 /**
149 * struct batadv_dhcp_option_header - BOOTP/DHCP option header
150 */
151 struct batadv_dhcp_option_header {
152 /** @type: type of option */
153 __u8 type;
154
155 /** @len: length of option */
156 __u8 len;
157 };
158
159 #define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
160 #define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
161
162 static void batadv_dat_purge(struct work_struct *work);
163
164 /**
165 * batadv_dat_start_timer() - initialise the DAT periodic worker
166 * @bat_priv: the bat priv with all the mesh interface information
167 */
batadv_dat_start_timer(struct batadv_priv * bat_priv)168 static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
169 {
170 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
171 msecs_to_jiffies(10000));
172 }
173
174 /**
175 * batadv_dat_entry_release() - release dat_entry from lists and queue for free
176 * after rcu grace period
177 * @ref: kref pointer of the dat_entry
178 */
batadv_dat_entry_release(struct kref * ref)179 static void batadv_dat_entry_release(struct kref *ref)
180 {
181 struct batadv_dat_entry *dat_entry;
182
183 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
184
185 kfree_rcu(dat_entry, rcu);
186 }
187
188 /**
189 * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
190 * release it
191 * @dat_entry: dat_entry to be free'd
192 */
batadv_dat_entry_put(struct batadv_dat_entry * dat_entry)193 static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
194 {
195 if (!dat_entry)
196 return;
197
198 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
199 }
200
201 /**
202 * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
203 * @dat_entry: the entry to check
204 *
205 * Return: true if the entry has to be purged now, false otherwise.
206 */
batadv_dat_to_purge(struct batadv_dat_entry * dat_entry)207 static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
208 {
209 return batadv_has_timed_out(dat_entry->last_update,
210 BATADV_DAT_ENTRY_TIMEOUT);
211 }
212
213 /**
214 * __batadv_dat_purge() - delete entries from the DAT local storage
215 * @bat_priv: the bat priv with all the mesh interface information
216 * @to_purge: function in charge to decide whether an entry has to be purged or
217 * not. This function takes the dat_entry as argument and has to
218 * returns a boolean value: true is the entry has to be deleted,
219 * false otherwise
220 *
221 * Loops over each entry in the DAT local storage and deletes it if and only if
222 * the to_purge function passed as argument returns true.
223 */
__batadv_dat_purge(struct batadv_priv * bat_priv,bool (* to_purge)(struct batadv_dat_entry *))224 static void __batadv_dat_purge(struct batadv_priv *bat_priv,
225 bool (*to_purge)(struct batadv_dat_entry *))
226 {
227 spinlock_t *list_lock; /* protects write access to the hash lists */
228 struct batadv_dat_entry *dat_entry;
229 struct hlist_node *node_tmp;
230 struct hlist_head *head;
231 u32 i;
232
233 if (!bat_priv->dat.hash)
234 return;
235
236 for (i = 0; i < bat_priv->dat.hash->size; i++) {
237 head = &bat_priv->dat.hash->table[i];
238 list_lock = &bat_priv->dat.hash->list_locks[i];
239
240 spin_lock_bh(list_lock);
241 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
242 hash_entry) {
243 /* if a helper function has been passed as parameter,
244 * ask it if the entry has to be purged or not
245 */
246 if (to_purge && !to_purge(dat_entry))
247 continue;
248
249 hlist_del_rcu(&dat_entry->hash_entry);
250 batadv_dat_entry_put(dat_entry);
251 }
252 spin_unlock_bh(list_lock);
253 }
254 }
255
256 /**
257 * batadv_dat_purge() - periodic task that deletes old entries from the local
258 * DAT hash table
259 * @work: kernel work struct
260 */
batadv_dat_purge(struct work_struct * work)261 static void batadv_dat_purge(struct work_struct *work)
262 {
263 struct delayed_work *delayed_work;
264 struct batadv_priv_dat *priv_dat;
265 struct batadv_priv *bat_priv;
266
267 delayed_work = to_delayed_work(work);
268 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
269 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
270
271 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
272 batadv_dat_start_timer(bat_priv);
273 }
274
275 /**
276 * batadv_compare_dat() - comparing function used in the local DAT hash table
277 * @node: node in the local table
278 * @data2: second object to compare the node to
279 *
280 * Return: true if the two entries are the same, false otherwise.
281 */
batadv_compare_dat(const struct hlist_node * node,const void * data2)282 static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
283 {
284 const struct batadv_dat_entry *entry1;
285 const struct batadv_dat_entry *entry2;
286
287 entry1 = container_of(node, struct batadv_dat_entry, hash_entry);
288 entry2 = data2;
289
290 return entry1->ip == entry2->ip && entry1->vid == entry2->vid;
291 }
292
293 /**
294 * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
295 * @skb: ARP packet
296 * @hdr_size: size of the possible header before the ARP packet
297 *
298 * Return: the value of the hw_src field in the ARP packet.
299 */
batadv_arp_hw_src(struct sk_buff * skb,int hdr_size)300 static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
301 {
302 u8 *addr;
303
304 addr = (u8 *)(skb->data + hdr_size);
305 addr += ETH_HLEN + sizeof(struct arphdr);
306
307 return addr;
308 }
309
310 /**
311 * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
312 * @skb: ARP packet
313 * @hdr_size: size of the possible header before the ARP packet
314 *
315 * Return: the value of the ip_src field in the ARP packet.
316 */
batadv_arp_ip_src(struct sk_buff * skb,int hdr_size)317 static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
318 {
319 u8 *src = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN;
320 __be32 *ip = (__force __be32 *)src;
321
322 return get_unaligned(ip);
323 }
324
325 /**
326 * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
327 * @skb: ARP packet
328 * @hdr_size: size of the possible header before the ARP packet
329 *
330 * Return: the value of the hw_dst field in the ARP packet.
331 */
batadv_arp_hw_dst(struct sk_buff * skb,int hdr_size)332 static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
333 {
334 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
335 }
336
337 /**
338 * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
339 * @skb: ARP packet
340 * @hdr_size: size of the possible header before the ARP packet
341 *
342 * Return: the value of the ip_dst field in the ARP packet.
343 */
batadv_arp_ip_dst(struct sk_buff * skb,int hdr_size)344 static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
345 {
346 u8 *dst = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4;
347 __be32 *ip = (__force __be32 *)dst;
348
349 return get_unaligned(ip);
350 }
351
352 /**
353 * batadv_hash_dat() - compute the hash value for an IP address
354 * @data: data to hash
355 * @size: size of the hash table
356 *
357 * Return: the selected index in the hash table for the given data.
358 */
batadv_hash_dat(const void * data,u32 size)359 static u32 batadv_hash_dat(const void *data, u32 size)
360 {
361 const struct batadv_dat_entry *dat = data;
362 const unsigned char *key;
363 u32 hash = 0;
364 __be16 vid;
365 u32 i;
366
367 key = (__force const unsigned char *)&dat->ip;
368 for (i = 0; i < sizeof(dat->ip); i++) {
369 hash += key[i];
370 hash += (hash << 10);
371 hash ^= (hash >> 6);
372 }
373
374 vid = htons(dat->vid);
375 key = (__force const unsigned char *)&vid;
376 for (i = 0; i < sizeof(dat->vid); i++) {
377 hash += key[i];
378 hash += (hash << 10);
379 hash ^= (hash >> 6);
380 }
381
382 hash += (hash << 3);
383 hash ^= (hash >> 11);
384 hash += (hash << 15);
385
386 return hash % size;
387 }
388
389 /**
390 * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
391 * table
392 * @bat_priv: the bat priv with all the mesh interface information
393 * @ip: search key
394 * @vid: VLAN identifier
395 *
396 * Return: the dat_entry if found, NULL otherwise.
397 */
398 static struct batadv_dat_entry *
batadv_dat_entry_hash_find(struct batadv_priv * bat_priv,__be32 ip,unsigned short vid)399 batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
400 unsigned short vid)
401 {
402 struct batadv_hashtable *hash = bat_priv->dat.hash;
403 struct batadv_dat_entry *dat_entry_tmp = NULL;
404 struct batadv_dat_entry *dat_entry;
405 struct batadv_dat_entry to_find;
406 struct hlist_head *head;
407 u32 index;
408
409 if (!hash)
410 return NULL;
411
412 to_find.ip = ip;
413 to_find.vid = vid;
414
415 index = batadv_hash_dat(&to_find, hash->size);
416 head = &hash->table[index];
417
418 rcu_read_lock();
419 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
420 if (dat_entry->ip != ip)
421 continue;
422
423 if (dat_entry->vid != vid)
424 continue;
425
426 if (!kref_get_unless_zero(&dat_entry->refcount))
427 continue;
428
429 dat_entry_tmp = dat_entry;
430 break;
431 }
432 rcu_read_unlock();
433
434 return dat_entry_tmp;
435 }
436
437 /**
438 * batadv_dat_entry_add() - add a new dat entry or update it if already exists
439 * @bat_priv: the bat priv with all the mesh interface information
440 * @ip: ipv4 to add/edit
441 * @mac_addr: mac address to assign to the given ipv4
442 * @vid: VLAN identifier
443 */
batadv_dat_entry_add(struct batadv_priv * bat_priv,__be32 ip,u8 * mac_addr,unsigned short vid)444 static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
445 u8 *mac_addr, unsigned short vid)
446 {
447 u64 u64_mac = ether_addr_to_u64(mac_addr);
448 struct batadv_dat_entry *dat_entry;
449 int hash_added;
450
451 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
452 /* if this entry is already known, just update it */
453 if (dat_entry) {
454 atomic64_set(&dat_entry->mac_addr, u64_mac);
455
456 dat_entry->last_update = jiffies;
457 batadv_dbg(BATADV_DBG_DAT, bat_priv,
458 "Entry updated: %pI4 %pM (vid: %d)\n",
459 &dat_entry->ip, mac_addr,
460 batadv_print_vid(vid));
461 goto out;
462 }
463
464 dat_entry = kmalloc_obj(*dat_entry, GFP_ATOMIC);
465 if (!dat_entry)
466 goto out;
467
468 dat_entry->ip = ip;
469 dat_entry->vid = vid;
470 atomic64_set(&dat_entry->mac_addr, u64_mac);
471 dat_entry->last_update = jiffies;
472 kref_init(&dat_entry->refcount);
473
474 kref_get(&dat_entry->refcount);
475 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
476 batadv_hash_dat, dat_entry,
477 &dat_entry->hash_entry);
478
479 if (unlikely(hash_added != 0)) {
480 /* remove the reference for the hash */
481 batadv_dat_entry_put(dat_entry);
482 goto out;
483 }
484
485 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
486 &dat_entry->ip, mac_addr, batadv_print_vid(vid));
487
488 out:
489 batadv_dat_entry_put(dat_entry);
490 }
491
492 #ifdef CONFIG_BATMAN_ADV_DEBUG
493
494 /**
495 * batadv_dbg_arp() - print a debug message containing all the ARP packet
496 * details
497 * @bat_priv: the bat priv with all the mesh interface information
498 * @skb: ARP packet
499 * @hdr_size: size of the possible header before the ARP packet
500 * @msg: message to print together with the debugging information
501 */
batadv_dbg_arp(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size,char * msg)502 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
503 int hdr_size, char *msg)
504 {
505 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
506 struct batadv_bcast_packet *bcast_pkt;
507 u8 *orig_addr;
508 __be32 ip_src;
509 __be32 ip_dst;
510
511 if (msg)
512 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
513
514 ip_src = batadv_arp_ip_src(skb, hdr_size);
515 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
516 batadv_dbg(BATADV_DBG_DAT, bat_priv,
517 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
518 batadv_arp_hw_src(skb, hdr_size), &ip_src,
519 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
520
521 if (hdr_size < sizeof(struct batadv_unicast_packet))
522 return;
523
524 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
525
526 switch (unicast_4addr_packet->u.packet_type) {
527 case BATADV_UNICAST:
528 batadv_dbg(BATADV_DBG_DAT, bat_priv,
529 "* encapsulated within a UNICAST packet\n");
530 break;
531 case BATADV_UNICAST_4ADDR:
532 batadv_dbg(BATADV_DBG_DAT, bat_priv,
533 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
534 unicast_4addr_packet->src);
535 switch (unicast_4addr_packet->subtype) {
536 case BATADV_P_DAT_DHT_PUT:
537 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
538 break;
539 case BATADV_P_DAT_DHT_GET:
540 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
541 break;
542 case BATADV_P_DAT_CACHE_REPLY:
543 batadv_dbg(BATADV_DBG_DAT, bat_priv,
544 "* type: DAT_CACHE_REPLY\n");
545 break;
546 case BATADV_P_DATA:
547 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
548 break;
549 default:
550 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
551 unicast_4addr_packet->u.packet_type);
552 }
553 break;
554 case BATADV_BCAST:
555 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
556 orig_addr = bcast_pkt->orig;
557 batadv_dbg(BATADV_DBG_DAT, bat_priv,
558 "* encapsulated within a BCAST packet (src: %pM)\n",
559 orig_addr);
560 break;
561 default:
562 batadv_dbg(BATADV_DBG_DAT, bat_priv,
563 "* encapsulated within an unknown packet type (0x%x)\n",
564 unicast_4addr_packet->u.packet_type);
565 }
566 }
567
568 #else
569
batadv_dbg_arp(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size,char * msg)570 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
571 int hdr_size, char *msg)
572 {
573 }
574
575 #endif /* CONFIG_BATMAN_ADV_DEBUG */
576
577 /**
578 * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
579 * @res: the array with the already selected candidates
580 * @select: number of already selected candidates
581 * @tmp_max: address of the currently evaluated node
582 * @max: current round max address
583 * @last_max: address of the last selected candidate
584 * @candidate: orig_node under evaluation
585 * @max_orig_node: last selected candidate
586 *
587 * Return: true if the node has been elected as next candidate or false
588 * otherwise.
589 */
batadv_is_orig_node_eligible(struct batadv_dat_candidate * res,int select,batadv_dat_addr_t tmp_max,batadv_dat_addr_t max,batadv_dat_addr_t last_max,struct batadv_orig_node * candidate,struct batadv_orig_node * max_orig_node)590 static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
591 int select, batadv_dat_addr_t tmp_max,
592 batadv_dat_addr_t max,
593 batadv_dat_addr_t last_max,
594 struct batadv_orig_node *candidate,
595 struct batadv_orig_node *max_orig_node)
596 {
597 bool ret = false;
598 int j;
599
600 /* check if orig node candidate is running DAT */
601 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
602 goto out;
603
604 /* Check if this node has already been selected... */
605 for (j = 0; j < select; j++)
606 if (res[j].orig_node == candidate)
607 break;
608 /* ..and possibly skip it */
609 if (j < select)
610 goto out;
611 /* sanity check: has it already been selected? This should not happen */
612 if (tmp_max > last_max)
613 goto out;
614 /* check if during this iteration an originator with a closer dht
615 * address has already been found
616 */
617 if (tmp_max < max)
618 goto out;
619 /* this is an hash collision with the temporary selected node. Choose
620 * the one with the lowest address
621 */
622 if (tmp_max == max && max_orig_node &&
623 memcmp(candidate->orig, max_orig_node->orig, ETH_ALEN) >= 0)
624 goto out;
625
626 ret = true;
627 out:
628 return ret;
629 }
630
631 /**
632 * batadv_choose_next_candidate() - select the next DHT candidate
633 * @bat_priv: the bat priv with all the mesh interface information
634 * @cands: candidates array
635 * @select: number of candidates already present in the array
636 * @ip_key: key to look up in the DHT
637 * @last_max: pointer where the address of the selected candidate will be saved
638 */
batadv_choose_next_candidate(struct batadv_priv * bat_priv,struct batadv_dat_candidate * cands,int select,batadv_dat_addr_t ip_key,batadv_dat_addr_t * last_max)639 static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
640 struct batadv_dat_candidate *cands,
641 int select, batadv_dat_addr_t ip_key,
642 batadv_dat_addr_t *last_max)
643 {
644 struct batadv_hashtable *hash = bat_priv->orig_hash;
645 struct batadv_orig_node *max_orig_node = NULL;
646 struct batadv_orig_node *orig_node;
647 batadv_dat_addr_t tmp_max = 0;
648 batadv_dat_addr_t max = 0;
649 struct hlist_head *head;
650 int i;
651
652 /* if no node is eligible as candidate, leave the candidate type as
653 * NOT_FOUND
654 */
655 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
656
657 /* iterate over the originator list and find the node with the closest
658 * dat_address which has not been selected yet
659 */
660 for (i = 0; i < hash->size; i++) {
661 head = &hash->table[i];
662
663 rcu_read_lock();
664 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
665 /* the dht space is a ring using unsigned addresses */
666 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
667 ip_key;
668
669 if (!batadv_is_orig_node_eligible(cands, select,
670 tmp_max, max,
671 *last_max, orig_node,
672 max_orig_node))
673 continue;
674
675 if (!kref_get_unless_zero(&orig_node->refcount))
676 continue;
677
678 max = tmp_max;
679 batadv_orig_node_put(max_orig_node);
680 max_orig_node = orig_node;
681 }
682 rcu_read_unlock();
683 }
684 if (max_orig_node) {
685 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
686 cands[select].orig_node = max_orig_node;
687 batadv_dbg(BATADV_DBG_DAT, bat_priv,
688 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
689 select, max_orig_node->orig, max_orig_node->dat_addr,
690 max);
691 }
692 *last_max = max;
693 }
694
695 /**
696 * batadv_dat_select_candidates() - select the nodes which the DHT message has
697 * to be sent to
698 * @bat_priv: the bat priv with all the mesh interface information
699 * @ip_dst: ipv4 to look up in the DHT
700 * @vid: VLAN identifier
701 *
702 * An originator O is selected if and only if its DHT_ID value is one of three
703 * closest values (from the LEFT, with wrap around if needed) then the hash
704 * value of the key. ip_dst is the key.
705 *
706 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
707 */
708 static struct batadv_dat_candidate *
batadv_dat_select_candidates(struct batadv_priv * bat_priv,__be32 ip_dst,unsigned short vid)709 batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
710 unsigned short vid)
711 {
712 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX;
713 struct batadv_dat_candidate *res;
714 struct batadv_dat_entry dat;
715 batadv_dat_addr_t ip_key;
716 int select;
717
718 if (!bat_priv->orig_hash)
719 return NULL;
720
721 res = kmalloc_objs(*res, BATADV_DAT_CANDIDATES_NUM, GFP_ATOMIC);
722 if (!res)
723 return NULL;
724
725 dat.ip = ip_dst;
726 dat.vid = vid;
727 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
728 BATADV_DAT_ADDR_MAX);
729
730 batadv_dbg(BATADV_DBG_DAT, bat_priv,
731 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
732 ip_key);
733
734 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
735 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
736 &last_max);
737
738 return res;
739 }
740
741 /**
742 * batadv_dat_forward_data() - copy and send payload to the selected candidates
743 * @bat_priv: the bat priv with all the mesh interface information
744 * @skb: payload to send
745 * @ip: the DHT key
746 * @vid: VLAN identifier
747 * @packet_subtype: unicast4addr packet subtype to use
748 *
749 * This function copies the skb with pskb_copy() and is sent as a unicast packet
750 * to each of the selected candidates.
751 *
752 * Return: true if the packet is sent to at least one candidate, false
753 * otherwise.
754 */
batadv_dat_forward_data(struct batadv_priv * bat_priv,struct sk_buff * skb,__be32 ip,unsigned short vid,int packet_subtype)755 static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
756 struct sk_buff *skb, __be32 ip,
757 unsigned short vid, int packet_subtype)
758 {
759 struct batadv_neigh_node *neigh_node = NULL;
760 struct batadv_dat_candidate *cand;
761 struct sk_buff *tmp_skb;
762 bool ret = false;
763 int send_status;
764 int i;
765
766 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
767 if (!cand)
768 return ret;
769
770 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
771
772 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
773 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
774 continue;
775
776 neigh_node = batadv_orig_router_get(cand[i].orig_node,
777 BATADV_IF_DEFAULT);
778 if (!neigh_node)
779 goto free_orig;
780
781 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
782 if (!tmp_skb)
783 goto free_neigh;
784
785 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
786 cand[i].orig_node,
787 packet_subtype)) {
788 kfree_skb(tmp_skb);
789 goto free_neigh;
790 }
791
792 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
793 if (send_status == NET_XMIT_SUCCESS) {
794 /* count the sent packet */
795 switch (packet_subtype) {
796 case BATADV_P_DAT_DHT_GET:
797 batadv_inc_counter(bat_priv,
798 BATADV_CNT_DAT_GET_TX);
799 break;
800 case BATADV_P_DAT_DHT_PUT:
801 batadv_inc_counter(bat_priv,
802 BATADV_CNT_DAT_PUT_TX);
803 break;
804 }
805
806 /* packet sent to a candidate: return true */
807 ret = true;
808 }
809 free_neigh:
810 batadv_neigh_node_put(neigh_node);
811 free_orig:
812 batadv_orig_node_put(cand[i].orig_node);
813 }
814
815 kfree(cand);
816 return ret;
817 }
818
819 /**
820 * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
821 * setting change
822 * @bat_priv: the bat priv with all the mesh interface information
823 */
batadv_dat_tvlv_container_update(struct batadv_priv * bat_priv)824 static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
825 {
826 char dat_mode;
827
828 dat_mode = READ_ONCE(bat_priv->distributed_arp_table);
829
830 switch (dat_mode) {
831 case 0:
832 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
833 break;
834 case 1:
835 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
836 NULL, 0);
837 break;
838 }
839 }
840
841 /**
842 * batadv_dat_status_update() - update the dat tvlv container after dat
843 * setting change
844 * @net_dev: the mesh interface net device
845 */
batadv_dat_status_update(struct net_device * net_dev)846 void batadv_dat_status_update(struct net_device *net_dev)
847 {
848 struct batadv_priv *bat_priv = netdev_priv(net_dev);
849
850 batadv_dat_tvlv_container_update(bat_priv);
851 }
852
853 /**
854 * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
855 * @bat_priv: the bat priv with all the mesh interface information
856 * @orig: the orig_node of the ogm
857 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
858 * @tvlv_value: tvlv buffer containing the gateway data
859 * @tvlv_value_len: tvlv buffer length
860 */
batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv * bat_priv,struct batadv_orig_node * orig,u8 flags,void * tvlv_value,u16 tvlv_value_len)861 static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
862 struct batadv_orig_node *orig,
863 u8 flags,
864 void *tvlv_value, u16 tvlv_value_len)
865 {
866 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
867 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
868 else
869 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
870 }
871
872 /**
873 * batadv_dat_hash_free() - free the local DAT hash table
874 * @bat_priv: the bat priv with all the mesh interface information
875 */
batadv_dat_hash_free(struct batadv_priv * bat_priv)876 static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
877 {
878 if (!bat_priv->dat.hash)
879 return;
880
881 __batadv_dat_purge(bat_priv, NULL);
882
883 batadv_hash_destroy(bat_priv->dat.hash);
884
885 bat_priv->dat.hash = NULL;
886 }
887
888 /**
889 * batadv_dat_init() - initialise the DAT internals
890 * @bat_priv: the bat priv with all the mesh interface information
891 *
892 * Return: 0 in case of success, a negative error code otherwise
893 */
batadv_dat_init(struct batadv_priv * bat_priv)894 int batadv_dat_init(struct batadv_priv *bat_priv)
895 {
896 if (bat_priv->dat.hash)
897 return 0;
898
899 bat_priv->dat.hash = batadv_hash_new(1024);
900
901 if (!bat_priv->dat.hash)
902 return -ENOMEM;
903
904 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
905 batadv_dat_start_timer(bat_priv);
906
907 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
908 NULL, NULL, BATADV_TVLV_DAT, 1,
909 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
910 batadv_dat_tvlv_container_update(bat_priv);
911 return 0;
912 }
913
914 /**
915 * batadv_dat_free() - free the DAT internals
916 * @bat_priv: the bat priv with all the mesh interface information
917 */
batadv_dat_free(struct batadv_priv * bat_priv)918 void batadv_dat_free(struct batadv_priv *bat_priv)
919 {
920 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
921 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
922
923 disable_delayed_work_sync(&bat_priv->dat.work);
924
925 batadv_dat_hash_free(bat_priv);
926 }
927
928 /**
929 * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
930 * netlink socket
931 * @msg: buffer for the message
932 * @portid: netlink port
933 * @cb: Control block containing additional options
934 * @dat_entry: entry to dump
935 *
936 * Return: 0 or error code.
937 */
938 static int
batadv_dat_cache_dump_entry(struct sk_buff * msg,u32 portid,struct netlink_callback * cb,struct batadv_dat_entry * dat_entry)939 batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
940 struct netlink_callback *cb,
941 struct batadv_dat_entry *dat_entry)
942 {
943 u8 mac[ETH_ALEN];
944 u64 u64_mac;
945 int msecs;
946 void *hdr;
947
948 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
949 &batadv_netlink_family, NLM_F_MULTI,
950 BATADV_CMD_GET_DAT_CACHE);
951 if (!hdr)
952 return -ENOBUFS;
953
954 genl_dump_check_consistent(cb, hdr);
955
956 msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
957 u64_mac = atomic64_read(&dat_entry->mac_addr);
958 u64_to_ether_addr(u64_mac, mac);
959
960 if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
961 dat_entry->ip) ||
962 nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN, mac) ||
963 nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
964 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
965 genlmsg_cancel(msg, hdr);
966 return -EMSGSIZE;
967 }
968
969 genlmsg_end(msg, hdr);
970 return 0;
971 }
972
973 /**
974 * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
975 * a netlink socket
976 * @msg: buffer for the message
977 * @portid: netlink port
978 * @cb: Control block containing additional options
979 * @hash: hash to dump
980 * @bucket: bucket index to dump
981 * @idx_skip: How many entries to skip
982 *
983 * Return: 0 or error code.
984 */
985 static int
batadv_dat_cache_dump_bucket(struct sk_buff * msg,u32 portid,struct netlink_callback * cb,struct batadv_hashtable * hash,unsigned int bucket,int * idx_skip)986 batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
987 struct netlink_callback *cb,
988 struct batadv_hashtable *hash, unsigned int bucket,
989 int *idx_skip)
990 {
991 struct batadv_dat_entry *dat_entry;
992 int idx = 0;
993
994 spin_lock_bh(&hash->list_locks[bucket]);
995 cb->seq = atomic_read(&hash->generation) << 1 | 1;
996
997 hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
998 if (idx < *idx_skip)
999 goto skip;
1000
1001 if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
1002 spin_unlock_bh(&hash->list_locks[bucket]);
1003 *idx_skip = idx;
1004
1005 return -EMSGSIZE;
1006 }
1007
1008 skip:
1009 idx++;
1010 }
1011 spin_unlock_bh(&hash->list_locks[bucket]);
1012
1013 return 0;
1014 }
1015
1016 /**
1017 * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
1018 * @msg: buffer for the message
1019 * @cb: callback structure containing arguments
1020 *
1021 * Return: message length.
1022 */
batadv_dat_cache_dump(struct sk_buff * msg,struct netlink_callback * cb)1023 int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
1024 {
1025 struct batadv_hard_iface *primary_if = NULL;
1026 int portid = NETLINK_CB(cb->skb).portid;
1027 struct net_device *mesh_iface;
1028 struct batadv_hashtable *hash;
1029 struct batadv_priv *bat_priv;
1030 int bucket = cb->args[0];
1031 int idx = cb->args[1];
1032 int ret = 0;
1033
1034 mesh_iface = batadv_netlink_get_meshif(cb);
1035 if (IS_ERR(mesh_iface))
1036 return PTR_ERR(mesh_iface);
1037
1038 bat_priv = netdev_priv(mesh_iface);
1039 hash = bat_priv->dat.hash;
1040
1041 primary_if = batadv_primary_if_get_selected(bat_priv);
1042 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1043 ret = -ENOENT;
1044 goto out;
1045 }
1046
1047 while (bucket < hash->size) {
1048 if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
1049 &idx))
1050 break;
1051
1052 bucket++;
1053 idx = 0;
1054 }
1055
1056 cb->args[0] = bucket;
1057 cb->args[1] = idx;
1058
1059 ret = msg->len;
1060
1061 out:
1062 batadv_hardif_put(primary_if);
1063
1064 dev_put(mesh_iface);
1065
1066 return ret;
1067 }
1068
1069 /**
1070 * batadv_arp_get_type() - parse an ARP packet and gets the type
1071 * @bat_priv: the bat priv with all the mesh interface information
1072 * @skb: packet to analyse
1073 * @hdr_size: size of the possible header before the ARP packet in the skb
1074 *
1075 * Warning: This function may reallocate the skb data buffer via
1076 * pskb_may_pull()/... Any pointer into the skb data (e.g. obtained from skb->data
1077 * or eth_hdr()) before this call must be considered invalid afterwards and has
1078 * to be reacquired.
1079 *
1080 * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
1081 */
batadv_arp_get_type(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)1082 static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1083 struct sk_buff *skb, int hdr_size)
1084 {
1085 struct arphdr *arphdr;
1086 struct ethhdr *ethhdr;
1087 __be32 ip_src;
1088 __be32 ip_dst;
1089 u16 type = 0;
1090 u8 *hw_src;
1091 u8 *hw_dst;
1092
1093 /* pull the ethernet header */
1094 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1095 goto out;
1096
1097 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1098
1099 if (ethhdr->h_proto != htons(ETH_P_ARP))
1100 goto out;
1101
1102 /* pull the ARP payload */
1103 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1104 arp_hdr_len(skb->dev))))
1105 goto out;
1106
1107 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1108
1109 /* check whether the ARP packet carries a valid IP information */
1110 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1111 goto out;
1112
1113 if (arphdr->ar_pro != htons(ETH_P_IP))
1114 goto out;
1115
1116 if (arphdr->ar_hln != ETH_ALEN)
1117 goto out;
1118
1119 if (arphdr->ar_pln != 4)
1120 goto out;
1121
1122 /* Check for bad reply/request. If the ARP message is not sane, DAT
1123 * will simply ignore it
1124 */
1125 ip_src = batadv_arp_ip_src(skb, hdr_size);
1126 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1127 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
1128 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
1129 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
1130 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
1131 goto out;
1132
1133 hw_src = batadv_arp_hw_src(skb, hdr_size);
1134 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1135 goto out;
1136
1137 /* don't care about the destination MAC address in ARP requests */
1138 if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
1139 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1140 if (is_zero_ether_addr(hw_dst) ||
1141 is_multicast_ether_addr(hw_dst))
1142 goto out;
1143 }
1144
1145 type = ntohs(arphdr->ar_op);
1146 out:
1147 return type;
1148 }
1149
1150 /**
1151 * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
1152 * @skb: the buffer containing the packet to extract the VID from
1153 * @hdr_size: the size of the batman-adv header encapsulating the packet
1154 *
1155 * The caller must ensure that at least @hdr_size + ETH_HLEN bytes are
1156 * accessible after skb->data.
1157 *
1158 * Warning: This function calls batadv_get_vid() and may therefore reallocate
1159 * the skb data buffer. Any pointer into the skb data (e.g. obtained from
1160 * skb->data or eth_hdr()) before this call must be considered invalid
1161 * afterwards and has to be reacquired.
1162 *
1163 * Return: If the packet embedded in the skb is vlan tagged this function
1164 * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1165 * is returned.
1166 */
batadv_dat_get_vid(struct sk_buff * skb,int * hdr_size)1167 static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1168 {
1169 unsigned short vid;
1170
1171 vid = batadv_get_vid(skb, *hdr_size);
1172
1173 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1174 * If the header contained in the packet is a VLAN one (which is longer)
1175 * hdr_size is updated so that the functions will still skip the
1176 * correct amount of bytes.
1177 */
1178 if (vid & BATADV_VLAN_HAS_TAG)
1179 *hdr_size += VLAN_HLEN;
1180
1181 return vid;
1182 }
1183
1184 /**
1185 * batadv_dat_arp_create_reply() - create an ARP Reply
1186 * @bat_priv: the bat priv with all the mesh interface information
1187 * @ip_src: ARP sender IP
1188 * @ip_dst: ARP target IP
1189 * @hw_src: Ethernet source and ARP sender MAC
1190 * @hw_dst: Ethernet destination and ARP target MAC
1191 * @vid: VLAN identifier (optional, set to zero otherwise)
1192 *
1193 * Creates an ARP Reply from the given values, optionally encapsulated in a
1194 * VLAN header.
1195 *
1196 * Return: An skb containing an ARP Reply.
1197 */
1198 static struct sk_buff *
batadv_dat_arp_create_reply(struct batadv_priv * bat_priv,__be32 ip_src,__be32 ip_dst,u8 * hw_src,u8 * hw_dst,unsigned short vid)1199 batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1200 __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1201 unsigned short vid)
1202 {
1203 struct sk_buff *skb;
1204
1205 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->mesh_iface,
1206 ip_src, hw_dst, hw_src, hw_dst);
1207 if (!skb)
1208 return NULL;
1209
1210 skb_reset_mac_header(skb);
1211
1212 if (vid & BATADV_VLAN_HAS_TAG)
1213 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1214 vid & VLAN_VID_MASK);
1215
1216 return skb;
1217 }
1218
1219 /**
1220 * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
1221 * answer using DAT
1222 * @bat_priv: the bat priv with all the mesh interface information
1223 * @skb: packet to check
1224 *
1225 * Warning: This function may reallocate the skb data buffer via
1226 * batadv_dat_get_vid()/.... Any pointer into the skb data (e.g. obtained
1227 * from skb->data or eth_hdr()) before this call must be considered
1228 * invalid afterwards and has to be reacquired.
1229 *
1230 * Return: true if the message has been sent to the dht candidates, false
1231 * otherwise. In case of a positive return value the message has to be enqueued
1232 * to permit the fallback.
1233 */
batadv_dat_snoop_outgoing_arp_request(struct batadv_priv * bat_priv,struct sk_buff * skb)1234 bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1235 struct sk_buff *skb)
1236 {
1237 struct net_device *mesh_iface = bat_priv->mesh_iface;
1238 struct batadv_dat_entry *dat_entry = NULL;
1239 struct sk_buff *skb_new;
1240 unsigned short vid;
1241 bool ret = false;
1242 u8 mac[ETH_ALEN];
1243 int hdr_size = 0;
1244 __be32 ip_dst;
1245 __be32 ip_src;
1246 u16 type = 0;
1247 u64 u64_mac;
1248 u8 *hw_src;
1249
1250 if (!READ_ONCE(bat_priv->distributed_arp_table))
1251 goto out;
1252
1253 /* first, find out the vid. */
1254 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1255 goto out;
1256
1257 vid = batadv_dat_get_vid(skb, &hdr_size);
1258
1259 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1260 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1261 * message to the selected DHT candidates
1262 */
1263 if (type != ARPOP_REQUEST)
1264 goto out;
1265
1266 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
1267
1268 ip_src = batadv_arp_ip_src(skb, hdr_size);
1269 hw_src = batadv_arp_hw_src(skb, hdr_size);
1270 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1271
1272 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1273
1274 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1275 if (dat_entry) {
1276 u64_mac = atomic64_read(&dat_entry->mac_addr);
1277 u64_to_ether_addr(u64_mac, mac);
1278
1279 /* If the ARP request is destined for a local client the local
1280 * client will answer itself. DAT would only generate a
1281 * duplicate packet.
1282 *
1283 * Moreover, if the mesh-interface is enslaved into a bridge, an
1284 * additional DAT answer may trigger kernel warnings about
1285 * a packet coming from the wrong port.
1286 */
1287 if (batadv_is_my_client(bat_priv, mac, vid)) {
1288 ret = true;
1289 goto out;
1290 }
1291
1292 /* If BLA is enabled, only send ARP replies if we have claimed
1293 * the destination for the ARP request or if no one else of
1294 * the backbone gws belonging to our backbone has claimed the
1295 * destination.
1296 */
1297 if (!batadv_bla_check_claim(bat_priv, mac, vid)) {
1298 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1299 "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1300 mac);
1301 ret = true;
1302 goto out;
1303 }
1304
1305 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1306 mac, hw_src, vid);
1307 if (!skb_new)
1308 goto out;
1309
1310 skb_new->protocol = eth_type_trans(skb_new, mesh_iface);
1311
1312 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1313 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1314 skb->len + ETH_HLEN + hdr_size);
1315
1316 netif_rx(skb_new);
1317 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1318 ret = true;
1319 } else {
1320 /* Send the request to the DHT */
1321 ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1322 BATADV_P_DAT_DHT_GET);
1323 }
1324 out:
1325 batadv_dat_entry_put(dat_entry);
1326 return ret;
1327 }
1328
1329 /**
1330 * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
1331 * answer using the local DAT storage
1332 * @bat_priv: the bat priv with all the mesh interface information
1333 * @skb: packet to check
1334 * @hdr_size: size of the encapsulation header
1335 *
1336 * Warning: This function may reallocate the skb data buffer via
1337 * batadv_dat_get_vid()/... Any pointer into the skb data (e.g. obtained
1338 * from skb->data or eth_hdr()) before this call must be considered
1339 * invalid afterwards and has to be reacquired.
1340 *
1341 * Return: true if the request has been answered, false otherwise.
1342 */
batadv_dat_snoop_incoming_arp_request(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)1343 bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1344 struct sk_buff *skb, int hdr_size)
1345 {
1346 struct batadv_dat_entry *dat_entry = NULL;
1347 struct sk_buff *skb_new;
1348 unsigned short vid;
1349 bool ret = false;
1350 u8 mac[ETH_ALEN];
1351 __be32 ip_src;
1352 __be32 ip_dst;
1353 u64 u64_mac;
1354 u8 *hw_src;
1355 u16 type;
1356 int err;
1357
1358 if (!READ_ONCE(bat_priv->distributed_arp_table))
1359 goto out;
1360
1361 /* first, find out the vid. */
1362 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1363 goto out;
1364
1365 vid = batadv_dat_get_vid(skb, &hdr_size);
1366
1367 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1368 if (type != ARPOP_REQUEST)
1369 goto out;
1370
1371 hw_src = batadv_arp_hw_src(skb, hdr_size);
1372 ip_src = batadv_arp_ip_src(skb, hdr_size);
1373 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1374
1375 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
1376
1377 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1378
1379 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1380 if (!dat_entry)
1381 goto out;
1382
1383 u64_mac = atomic64_read(&dat_entry->mac_addr);
1384 u64_to_ether_addr(u64_mac, mac);
1385
1386 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1387 mac, hw_src, vid);
1388 if (!skb_new)
1389 goto out;
1390
1391 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1392 BATADV_P_DAT_CACHE_REPLY,
1393 NULL, vid);
1394 if (err != NET_XMIT_DROP) {
1395 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
1396 ret = true;
1397 }
1398 out:
1399 batadv_dat_entry_put(dat_entry);
1400 if (ret)
1401 kfree_skb(skb);
1402 return ret;
1403 }
1404
1405 /**
1406 * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
1407 * @bat_priv: the bat priv with all the mesh interface information
1408 * @skb: packet to check
1409 *
1410 * Warning: This function may reallocate the skb data buffer via
1411 * batadv_dat_get_vid()/... Any pointer into the skb data (e.g. obtained
1412 * from skb->data or eth_hdr()) before this call must be considered
1413 * invalid afterwards and has to be reacquired.
1414 */
batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv * bat_priv,struct sk_buff * skb)1415 void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1416 struct sk_buff *skb)
1417 {
1418 unsigned short vid;
1419 int hdr_size = 0;
1420 __be32 ip_src;
1421 __be32 ip_dst;
1422 u8 *hw_src;
1423 u8 *hw_dst;
1424 u16 type;
1425
1426 if (!READ_ONCE(bat_priv->distributed_arp_table))
1427 return;
1428
1429 /* first, find out the vid. */
1430 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1431 return;
1432
1433 vid = batadv_dat_get_vid(skb, &hdr_size);
1434
1435 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1436 if (type != ARPOP_REPLY)
1437 return;
1438
1439 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
1440
1441 hw_src = batadv_arp_hw_src(skb, hdr_size);
1442 ip_src = batadv_arp_ip_src(skb, hdr_size);
1443 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1444 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1445
1446 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1447 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1448
1449 /* Send the ARP reply to the candidates for both the IP addresses that
1450 * the node obtained from the ARP reply
1451 */
1452 batadv_dat_forward_data(bat_priv, skb, ip_src, vid,
1453 BATADV_P_DAT_DHT_PUT);
1454 batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1455 BATADV_P_DAT_DHT_PUT);
1456 }
1457
1458 /**
1459 * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1460 * local DAT storage only
1461 * @bat_priv: the bat priv with all the mesh interface information
1462 * @skb: packet to check
1463 * @hdr_size: size of the encapsulation header
1464 *
1465 * Warning: This function may reallocate the skb data buffer via
1466 * batadv_dat_get_vid()/... Any pointer into the skb data (e.g. obtained
1467 * from skb->data or eth_hdr()) before this call must be considered
1468 * invalid afterwards and has to be reacquired.
1469 *
1470 * Return: true if the packet was snooped and consumed by DAT. False if the
1471 * packet has to be delivered to the interface
1472 */
batadv_dat_snoop_incoming_arp_reply(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)1473 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1474 struct sk_buff *skb, int hdr_size)
1475 {
1476 struct batadv_dat_entry *dat_entry = NULL;
1477 bool dropped = false;
1478 unsigned short vid;
1479 u8 mac[ETH_ALEN];
1480 __be32 ip_src;
1481 __be32 ip_dst;
1482 u64 u64_mac;
1483 u8 *hw_src;
1484 u8 *hw_dst;
1485 u16 type;
1486
1487 if (!READ_ONCE(bat_priv->distributed_arp_table))
1488 goto out;
1489
1490 /* first, find out the vid. */
1491 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1492 goto out;
1493
1494 vid = batadv_dat_get_vid(skb, &hdr_size);
1495
1496 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1497 if (type != ARPOP_REPLY)
1498 goto out;
1499
1500 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
1501
1502 hw_src = batadv_arp_hw_src(skb, hdr_size);
1503 ip_src = batadv_arp_ip_src(skb, hdr_size);
1504 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1505 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1506
1507 /* If ip_dst is already in cache and has the right mac address,
1508 * drop this frame if this ARP reply is destined for us because it's
1509 * most probably an ARP reply generated by another node of the DHT.
1510 * We have most probably received already a reply earlier. Delivering
1511 * this frame would lead to doubled receive of an ARP reply.
1512 */
1513 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1514 if (dat_entry) {
1515 u64_mac = atomic64_read(&dat_entry->mac_addr);
1516 u64_to_ether_addr(u64_mac, mac);
1517
1518 if (batadv_compare_eth(hw_src, mac)) {
1519 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1520 "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1521 hw_src, &ip_src, hw_dst, &ip_dst,
1522 mac, &dat_entry->ip);
1523 dropped = true;
1524 }
1525 }
1526
1527 /* Update our internal cache with both the IP addresses the node got
1528 * within the ARP reply
1529 */
1530 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1531 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1532
1533 if (dropped)
1534 goto out;
1535
1536 /* If BLA is enabled, only forward ARP replies if we have claimed the
1537 * source of the ARP reply or if no one else of the same backbone has
1538 * already claimed that client. This prevents that different gateways
1539 * to the same backbone all forward the ARP reply leading to multiple
1540 * replies in the backbone.
1541 */
1542 if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1543 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1544 "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1545 hw_src);
1546 dropped = true;
1547 goto out;
1548 }
1549
1550 /* if this REPLY is directed to a client of mine, let's deliver the
1551 * packet to the interface
1552 */
1553 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1554
1555 /* if this REPLY is sent on behalf of a client of mine, let's drop the
1556 * packet because the client will reply by itself
1557 */
1558 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
1559 out:
1560 if (dropped)
1561 kfree_skb(skb);
1562 batadv_dat_entry_put(dat_entry);
1563 /* if dropped == false -> deliver to the interface */
1564 return dropped;
1565 }
1566
1567 /**
1568 * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
1569 * @skb: the packet to check
1570 * @ip_src: a buffer to store the IPv4 source address in
1571 *
1572 * Checks whether the given skb has an IP and UDP header valid for a DHCP
1573 * message from a DHCP server. And if so, stores the IPv4 source address in
1574 * the provided buffer.
1575 *
1576 * Return: True if valid, false otherwise.
1577 */
1578 static bool
batadv_dat_check_dhcp_ipudp(struct sk_buff * skb,__be32 * ip_src)1579 batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
1580 {
1581 unsigned int offset = skb_network_offset(skb);
1582 struct udphdr *udphdr;
1583 struct udphdr _udphdr;
1584 struct iphdr *iphdr;
1585 struct iphdr _iphdr;
1586
1587 iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
1588 if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
1589 return false;
1590
1591 if (iphdr->protocol != IPPROTO_UDP)
1592 return false;
1593
1594 offset += iphdr->ihl * 4;
1595 skb_set_transport_header(skb, offset);
1596
1597 udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
1598 if (!udphdr || udphdr->source != htons(67))
1599 return false;
1600
1601 *ip_src = get_unaligned(&iphdr->saddr);
1602
1603 return true;
1604 }
1605
1606 /**
1607 * batadv_dat_check_dhcp() - examine packet for valid DHCP message
1608 * @skb: the packet to check
1609 * @proto: ethernet protocol hint (behind a potential vlan)
1610 * @ip_src: a buffer to store the IPv4 source address in
1611 *
1612 * Checks whether the given skb is a valid DHCP packet. And if so, stores the
1613 * IPv4 source address in the provided buffer.
1614 *
1615 * Caller needs to ensure that the skb network header is set correctly.
1616 *
1617 * Return: If skb is a valid DHCP packet, then returns its op code
1618 * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
1619 */
1620 static int
batadv_dat_check_dhcp(struct sk_buff * skb,__be16 proto,__be32 * ip_src)1621 batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
1622 {
1623 struct batadv_dhcp_header *dhcp_h;
1624 struct batadv_dhcp_header _dhcp_h;
1625 unsigned int offset;
1626 __be32 *magic;
1627 __be32 _magic;
1628
1629 if (proto != htons(ETH_P_IP))
1630 return -EINVAL;
1631
1632 if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
1633 return -EINVAL;
1634
1635 offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1636 if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
1637 return -EINVAL;
1638
1639 dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
1640 if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
1641 dhcp_h->hlen != ETH_ALEN)
1642 return -EINVAL;
1643
1644 offset += offsetof(struct batadv_dhcp_packet, magic);
1645
1646 magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
1647 if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
1648 return -EINVAL;
1649
1650 return dhcp_h->op;
1651 }
1652
1653 /**
1654 * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
1655 * @skb: the DHCP packet to parse
1656 *
1657 * Iterates over the DHCP options of the given DHCP packet to find a
1658 * DHCP Message Type option and parse it.
1659 *
1660 * Caller needs to ensure that the given skb is a valid DHCP packet and
1661 * that the skb transport header is set correctly.
1662 *
1663 * Return: The found DHCP message type value, if found. -EINVAL otherwise.
1664 */
batadv_dat_get_dhcp_message_type(struct sk_buff * skb)1665 static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
1666 {
1667 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1668 struct batadv_dhcp_option_header *tl;
1669 struct batadv_dhcp_option_header _tl;
1670 u8 *type;
1671 u8 _type;
1672
1673 offset += sizeof(struct batadv_dhcp_packet);
1674
1675 while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
1676 if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
1677 break;
1678
1679 if (tl->type == BATADV_DHCP_OPT_END)
1680 break;
1681
1682 if (tl->type == BATADV_DHCP_OPT_PAD)
1683 offset++;
1684 else
1685 offset += tl->len + sizeof(_tl);
1686 }
1687
1688 /* Option Overload Code not supported */
1689 if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
1690 tl->len != sizeof(_type))
1691 return -EINVAL;
1692
1693 offset += sizeof(_tl);
1694
1695 type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
1696 if (!type)
1697 return -EINVAL;
1698
1699 return *type;
1700 }
1701
1702 /**
1703 * batadv_dat_dhcp_get_yiaddr() - get yiaddr from a DHCP packet
1704 * @skb: the DHCP packet to parse
1705 * @buf: a buffer to store the yiaddr in
1706 *
1707 * Caller needs to ensure that the given skb is a valid DHCP packet and
1708 * that the skb transport header is set correctly.
1709 *
1710 * Return: True on success, false otherwise.
1711 */
batadv_dat_dhcp_get_yiaddr(struct sk_buff * skb,__be32 * buf)1712 static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
1713 {
1714 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1715 __be32 *yiaddr;
1716
1717 offset += offsetof(struct batadv_dhcp_packet, yiaddr);
1718 yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
1719
1720 if (!yiaddr)
1721 return false;
1722
1723 if (yiaddr != buf)
1724 *buf = get_unaligned(yiaddr);
1725
1726 return true;
1727 }
1728
1729 /**
1730 * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
1731 * @skb: the DHCP packet to parse
1732 * @buf: a buffer to store the chaddr in
1733 *
1734 * Caller needs to ensure that the given skb is a valid DHCP packet and
1735 * that the skb transport header is set correctly.
1736 *
1737 * Return: True on success, false otherwise
1738 */
batadv_dat_get_dhcp_chaddr(struct sk_buff * skb,u8 * buf)1739 static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
1740 {
1741 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1742 u8 *chaddr;
1743
1744 offset += offsetof(struct batadv_dhcp_packet, chaddr);
1745 chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
1746
1747 if (!chaddr)
1748 return false;
1749
1750 if (chaddr != buf)
1751 memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
1752
1753 return true;
1754 }
1755
1756 /**
1757 * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
1758 * DAT cache
1759 * @bat_priv: the bat priv with all the mesh interface information
1760 * @chaddr: the DHCP client MAC address
1761 * @yiaddr: the DHCP client IP address
1762 * @hw_dst: the DHCP server MAC address
1763 * @ip_dst: the DHCP server IP address
1764 * @vid: VLAN identifier
1765 *
1766 * Adds given MAC/IP pairs to the local DAT cache and propagates them further
1767 * into the DHT.
1768 *
1769 * For the DHT propagation, client MAC + IP will appear as the ARP Reply
1770 * transmitter (and hw_dst/ip_dst as the target).
1771 */
batadv_dat_put_dhcp(struct batadv_priv * bat_priv,u8 * chaddr,__be32 yiaddr,u8 * hw_dst,__be32 ip_dst,unsigned short vid)1772 static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
1773 __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
1774 unsigned short vid)
1775 {
1776 struct sk_buff *skb;
1777
1778 skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
1779 hw_dst, vid);
1780 if (!skb)
1781 return;
1782
1783 skb_set_network_header(skb, ETH_HLEN);
1784
1785 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1786 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1787
1788 batadv_dat_forward_data(bat_priv, skb, yiaddr, vid,
1789 BATADV_P_DAT_DHT_PUT);
1790 batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1791 BATADV_P_DAT_DHT_PUT);
1792
1793 consume_skb(skb);
1794
1795 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1796 "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1797 &ip_dst, hw_dst, batadv_print_vid(vid));
1798 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1799 "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1800 &yiaddr, chaddr, batadv_print_vid(vid));
1801 }
1802
1803 /**
1804 * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
1805 * @skb: the packet to check
1806 * @proto: ethernet protocol hint (behind a potential vlan)
1807 * @ip_src: a buffer to store the IPv4 source address in
1808 * @chaddr: a buffer to store the DHCP Client Hardware Address in
1809 * @yiaddr: a buffer to store the DHCP Your IP Address in
1810 *
1811 * Checks whether the given skb is a valid DHCPACK. And if so, stores the
1812 * IPv4 server source address (ip_src), client MAC address (chaddr) and client
1813 * IPv4 address (yiaddr) in the provided buffers.
1814 *
1815 * Caller needs to ensure that the skb network header is set correctly.
1816 *
1817 * Return: True if the skb is a valid DHCPACK. False otherwise.
1818 */
1819 static bool
batadv_dat_check_dhcp_ack(struct sk_buff * skb,__be16 proto,__be32 * ip_src,u8 * chaddr,__be32 * yiaddr)1820 batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
1821 u8 *chaddr, __be32 *yiaddr)
1822 {
1823 int type;
1824
1825 type = batadv_dat_check_dhcp(skb, proto, ip_src);
1826 if (type != BATADV_BOOTREPLY)
1827 return false;
1828
1829 type = batadv_dat_get_dhcp_message_type(skb);
1830 if (type != BATADV_DHCPACK)
1831 return false;
1832
1833 if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
1834 return false;
1835
1836 if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
1837 return false;
1838
1839 return true;
1840 }
1841
1842 /**
1843 * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
1844 * @bat_priv: the bat priv with all the mesh interface information
1845 * @skb: the packet to snoop
1846 * @proto: ethernet protocol hint (behind a potential vlan)
1847 * @vid: VLAN identifier
1848 *
1849 * This function first checks whether the given skb is a valid DHCPACK. If
1850 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1851 * field and DHCP Your IP Address field are added to the local DAT cache and
1852 * propagated into the DHT.
1853 *
1854 * Caller needs to ensure that the skb mac and network headers are set
1855 * correctly.
1856 */
batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv * bat_priv,struct sk_buff * skb,__be16 proto,unsigned short vid)1857 void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
1858 struct sk_buff *skb,
1859 __be16 proto,
1860 unsigned short vid)
1861 {
1862 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1863 __be32 ip_src;
1864 __be32 yiaddr;
1865
1866 if (!READ_ONCE(bat_priv->distributed_arp_table))
1867 return;
1868
1869 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1870 return;
1871
1872 batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
1873 ip_src, vid);
1874 }
1875
1876 /**
1877 * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
1878 * @bat_priv: the bat priv with all the mesh interface information
1879 * @skb: the packet to snoop
1880 * @hdr_size: header size, up to the tail of the batman-adv header
1881 *
1882 * This function first checks whether the given skb is a valid DHCPACK. If
1883 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1884 * field and DHCP Your IP Address field are added to the local DAT cache.
1885 *
1886 * Warning: This function may reallocate the skb data buffer via
1887 * pskb_may_pull()/batadv_dat_get_vid()/... Any pointer into the skb data
1888 * (e.g.obtained from skb->data or eth_hdr()) before this call must be
1889 * considered invalid afterwards and has to be reacquired.
1890 */
batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)1891 void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
1892 struct sk_buff *skb, int hdr_size)
1893 {
1894 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1895 struct ethhdr *ethhdr;
1896 unsigned short vid;
1897 int hdr_size_tmp;
1898 __be32 ip_src;
1899 __be32 yiaddr;
1900 __be16 proto;
1901 u8 *hw_src;
1902
1903 if (!READ_ONCE(bat_priv->distributed_arp_table))
1904 return;
1905
1906 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1907 return;
1908
1909 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1910 skb_set_network_header(skb, hdr_size + ETH_HLEN);
1911 proto = ethhdr->h_proto;
1912
1913 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1914 return;
1915
1916 hdr_size_tmp = hdr_size;
1917 vid = batadv_dat_get_vid(skb, &hdr_size_tmp);
1918 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1919 hw_src = ethhdr->h_source;
1920
1921 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1922 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1923
1924 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1925 "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1926 &ip_src, hw_src, batadv_print_vid(vid));
1927 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1928 "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1929 &yiaddr, chaddr, batadv_print_vid(vid));
1930 }
1931
1932 /**
1933 * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1934 * dropped (because the node has already obtained the reply via DAT) or not
1935 * @bat_priv: the bat priv with all the mesh interface information
1936 * @forw_packet: the broadcast packet
1937 *
1938 * Warning: This function may reallocate the skb data buffer via
1939 * batadv_dat_get_vid()/... Any pointer into the skb data (e.g. obtained
1940 * from skb->data or eth_hdr()) before this call must be considered
1941 * invalid afterwards and has to be reacquired.
1942 *
1943 * Return: true if the node can drop the packet, false otherwise.
1944 */
batadv_dat_drop_broadcast_packet(struct batadv_priv * bat_priv,struct batadv_forw_packet * forw_packet)1945 bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1946 struct batadv_forw_packet *forw_packet)
1947 {
1948 int hdr_size = sizeof(struct batadv_bcast_packet);
1949 struct batadv_dat_entry *dat_entry = NULL;
1950 unsigned short vid;
1951 bool ret = false;
1952 __be32 ip_dst;
1953 u16 type;
1954
1955 if (!READ_ONCE(bat_priv->distributed_arp_table))
1956 goto out;
1957
1958 /* If this packet is an ARP_REQUEST and the node already has the
1959 * information that it is going to ask, then the packet can be dropped
1960 */
1961 if (batadv_forw_packet_is_rebroadcast(forw_packet))
1962 goto out;
1963
1964 /* first, find out the vid. */
1965 if (!pskb_may_pull(forw_packet->skb, hdr_size + ETH_HLEN))
1966 goto out;
1967
1968 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1969
1970 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1971 if (type != ARPOP_REQUEST)
1972 goto out;
1973
1974 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1975 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1976 /* check if the node already got this entry */
1977 if (!dat_entry) {
1978 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1979 "ARP Request for %pI4: fallback\n", &ip_dst);
1980 goto out;
1981 }
1982
1983 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1984 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1985 ret = true;
1986
1987 out:
1988 batadv_dat_entry_put(dat_entry);
1989 return ret;
1990 }
1991