1
2
3
4
5
6
7#include "distributed-arp-table.h"
8#include "main.h"
9
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>
20#include <linux/in.h>
21#include <linux/ip.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>
37#include <net/arp.h>
38#include <net/genetlink.h>
39#include <net/netlink.h>
40#include <net/sock.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 "soft-interface.h"
51#include "translation-table.h"
52#include "tvlv.h"
53
54enum batadv_bootpop {
55 BATADV_BOOTREPLY = 2,
56};
57
58enum batadv_boothtype {
59 BATADV_HTYPE_ETHERNET = 1,
60};
61
62enum batadv_dhcpoptioncode {
63 BATADV_DHCP_OPT_PAD = 0,
64 BATADV_DHCP_OPT_MSG_TYPE = 53,
65 BATADV_DHCP_OPT_END = 255,
66};
67
68enum batadv_dhcptype {
69 BATADV_DHCPACK = 5,
70};
71
72
73#define BATADV_DHCP_MAGIC 1669485411
74
75struct batadv_dhcp_packet {
76 __u8 op;
77 __u8 htype;
78 __u8 hlen;
79 __u8 hops;
80 __be32 xid;
81 __be16 secs;
82 __be16 flags;
83 __be32 ciaddr;
84 __be32 yiaddr;
85 __be32 siaddr;
86 __be32 giaddr;
87 __u8 chaddr[16];
88 __u8 sname[64];
89 __u8 file[128];
90 __be32 magic;
91 __u8 options[0];
92};
93
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)
96
97static void batadv_dat_purge(struct work_struct *work);
98
99
100
101
102
103static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
104{
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));
108}
109
110
111
112
113
114
115static void batadv_dat_entry_release(struct kref *ref)
116{
117 struct batadv_dat_entry *dat_entry;
118
119 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
120
121 kfree_rcu(dat_entry, rcu);
122}
123
124
125
126
127
128
129static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
130{
131 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
132}
133
134
135
136
137
138
139
140static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
141{
142 return batadv_has_timed_out(dat_entry->last_update,
143 BATADV_DAT_ENTRY_TIMEOUT);
144}
145
146
147
148
149
150
151
152
153
154
155
156
157static void __batadv_dat_purge(struct batadv_priv *bat_priv,
158 bool (*to_purge)(struct batadv_dat_entry *))
159{
160 spinlock_t *list_lock;
161 struct batadv_dat_entry *dat_entry;
162 struct hlist_node *node_tmp;
163 struct hlist_head *head;
164 u32 i;
165
166 if (!bat_priv->dat.hash)
167 return;
168
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];
172
173 spin_lock_bh(list_lock);
174 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
175 hash_entry) {
176
177
178
179 if (to_purge && !to_purge(dat_entry))
180 continue;
181
182 hlist_del_rcu(&dat_entry->hash_entry);
183 batadv_dat_entry_put(dat_entry);
184 }
185 spin_unlock_bh(list_lock);
186 }
187}
188
189
190
191
192
193
194static void batadv_dat_purge(struct work_struct *work)
195{
196 struct delayed_work *delayed_work;
197 struct batadv_priv_dat *priv_dat;
198 struct batadv_priv *bat_priv;
199
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);
203
204 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
205 batadv_dat_start_timer(bat_priv);
206}
207
208
209
210
211
212
213
214
215static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
216{
217 const void *data1 = container_of(node, struct batadv_dat_entry,
218 hash_entry);
219
220 return memcmp(data1, data2, sizeof(__be32)) == 0;
221}
222
223
224
225
226
227
228
229
230static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
231{
232 u8 *addr;
233
234 addr = (u8 *)(skb->data + hdr_size);
235 addr += ETH_HLEN + sizeof(struct arphdr);
236
237 return addr;
238}
239
240
241
242
243
244
245
246
247static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
248{
249 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
250}
251
252
253
254
255
256
257
258
259static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
260{
261 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
262}
263
264
265
266
267
268
269
270
271static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
272{
273 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
274}
275
276
277
278
279
280
281
282
283static u32 batadv_hash_dat(const void *data, u32 size)
284{
285 u32 hash = 0;
286 const struct batadv_dat_entry *dat = data;
287 const unsigned char *key;
288 u32 i;
289
290 key = (const unsigned char *)&dat->ip;
291 for (i = 0; i < sizeof(dat->ip); i++) {
292 hash += key[i];
293 hash += (hash << 10);
294 hash ^= (hash >> 6);
295 }
296
297 key = (const unsigned char *)&dat->vid;
298 for (i = 0; i < sizeof(dat->vid); i++) {
299 hash += key[i];
300 hash += (hash << 10);
301 hash ^= (hash >> 6);
302 }
303
304 hash += (hash << 3);
305 hash ^= (hash >> 11);
306 hash += (hash << 15);
307
308 return hash % size;
309}
310
311
312
313
314
315
316
317
318
319
320static struct batadv_dat_entry *
321batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
322 unsigned short vid)
323{
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;
327 u32 index;
328
329 if (!hash)
330 return NULL;
331
332 to_find.ip = ip;
333 to_find.vid = vid;
334
335 index = batadv_hash_dat(&to_find, hash->size);
336 head = &hash->table[index];
337
338 rcu_read_lock();
339 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
340 if (dat_entry->ip != ip)
341 continue;
342
343 if (!kref_get_unless_zero(&dat_entry->refcount))
344 continue;
345
346 dat_entry_tmp = dat_entry;
347 break;
348 }
349 rcu_read_unlock();
350
351 return dat_entry_tmp;
352}
353
354
355
356
357
358
359
360
361static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
362 u8 *mac_addr, unsigned short vid)
363{
364 struct batadv_dat_entry *dat_entry;
365 int hash_added;
366
367 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
368
369 if (dat_entry) {
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));
377 goto out;
378 }
379
380 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
381 if (!dat_entry)
382 goto out;
383
384 dat_entry->ip = ip;
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);
389
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);
394
395 if (unlikely(hash_added != 0)) {
396
397 batadv_dat_entry_put(dat_entry);
398 goto out;
399 }
400
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));
403
404out:
405 if (dat_entry)
406 batadv_dat_entry_put(dat_entry);
407}
408
409#ifdef CONFIG_BATMAN_ADV_DEBUG
410
411
412
413
414
415
416
417
418
419static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
420 int hdr_size, char *msg)
421{
422 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
423 struct batadv_bcast_packet *bcast_pkt;
424 u8 *orig_addr;
425 __be32 ip_src, ip_dst;
426
427 if (msg)
428 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
429
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);
436
437 if (hdr_size < sizeof(struct batadv_unicast_packet))
438 return;
439
440 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
441
442 switch (unicast_4addr_packet->u.packet_type) {
443 case BATADV_UNICAST:
444 batadv_dbg(BATADV_DBG_DAT, bat_priv,
445 "* encapsulated within a UNICAST packet\n");
446 break;
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");
454 break;
455 case BATADV_P_DAT_DHT_GET:
456 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
457 break;
458 case BATADV_P_DAT_CACHE_REPLY:
459 batadv_dbg(BATADV_DBG_DAT, bat_priv,
460 "* type: DAT_CACHE_REPLY\n");
461 break;
462 case BATADV_P_DATA:
463 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
464 break;
465 default:
466 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
467 unicast_4addr_packet->u.packet_type);
468 }
469 break;
470 case BATADV_BCAST:
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",
475 orig_addr);
476 break;
477 default:
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);
481 }
482}
483
484#else
485
486static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
487 int hdr_size, char *msg)
488{
489}
490
491#endif
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506static 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)
512{
513 bool ret = false;
514 int j;
515
516
517 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
518 goto out;
519
520
521 for (j = 0; j < select; j++)
522 if (res[j].orig_node == candidate)
523 break;
524
525 if (j < select)
526 goto out;
527
528 if (tmp_max > last_max)
529 goto out;
530
531
532
533 if (tmp_max < max)
534 goto out;
535
536
537
538 if (tmp_max == max && max_orig_node &&
539 batadv_compare_eth(candidate->orig, max_orig_node->orig))
540 goto out;
541
542 ret = true;
543out:
544 return ret;
545}
546
547
548
549
550
551
552
553
554
555static 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)
559{
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;
565 int i;
566
567
568
569
570 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
571
572
573
574
575 for (i = 0; i < hash->size; i++) {
576 head = &hash->table[i];
577
578 rcu_read_lock();
579 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
580
581 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
582 ip_key;
583
584 if (!batadv_is_orig_node_eligible(cands, select,
585 tmp_max, max,
586 *last_max, orig_node,
587 max_orig_node))
588 continue;
589
590 if (!kref_get_unless_zero(&orig_node->refcount))
591 continue;
592
593 max = tmp_max;
594 if (max_orig_node)
595 batadv_orig_node_put(max_orig_node);
596 max_orig_node = orig_node;
597 }
598 rcu_read_unlock();
599 }
600 if (max_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,
606 max);
607 }
608 *last_max = max;
609}
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624static struct batadv_dat_candidate *
625batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
626 unsigned short vid)
627{
628 int select;
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;
632
633 if (!bat_priv->orig_hash)
634 return NULL;
635
636 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
637 GFP_ATOMIC);
638 if (!res)
639 return NULL;
640
641 dat.ip = ip_dst;
642 dat.vid = vid;
643 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
644 BATADV_DAT_ADDR_MAX);
645
646 batadv_dbg(BATADV_DBG_DAT, bat_priv,
647 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
648 ip_key);
649
650 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
651 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
652 &last_max);
653
654 return res;
655}
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
672 struct sk_buff *skb, __be32 ip,
673 unsigned short vid, int packet_subtype)
674{
675 int i;
676 bool ret = false;
677 int send_status;
678 struct batadv_neigh_node *neigh_node = NULL;
679 struct sk_buff *tmp_skb;
680 struct batadv_dat_candidate *cand;
681
682 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
683 if (!cand)
684 goto out;
685
686 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
687
688 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
689 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
690 continue;
691
692 neigh_node = batadv_orig_router_get(cand[i].orig_node,
693 BATADV_IF_DEFAULT);
694 if (!neigh_node)
695 goto free_orig;
696
697 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
698 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
699 cand[i].orig_node,
700 packet_subtype)) {
701 kfree_skb(tmp_skb);
702 goto free_neigh;
703 }
704
705 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
706 if (send_status == NET_XMIT_SUCCESS) {
707
708 switch (packet_subtype) {
709 case BATADV_P_DAT_DHT_GET:
710 batadv_inc_counter(bat_priv,
711 BATADV_CNT_DAT_GET_TX);
712 break;
713 case BATADV_P_DAT_DHT_PUT:
714 batadv_inc_counter(bat_priv,
715 BATADV_CNT_DAT_PUT_TX);
716 break;
717 }
718
719
720 ret = true;
721 }
722free_neigh:
723 batadv_neigh_node_put(neigh_node);
724free_orig:
725 batadv_orig_node_put(cand[i].orig_node);
726 }
727
728out:
729 kfree(cand);
730 return ret;
731}
732
733
734
735
736
737
738static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
739{
740 char dat_mode;
741
742 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
743
744 switch (dat_mode) {
745 case 0:
746 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
747 break;
748 case 1:
749 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
750 NULL, 0);
751 break;
752 }
753}
754
755
756
757
758
759
760void batadv_dat_status_update(struct net_device *net_dev)
761{
762 struct batadv_priv *bat_priv = netdev_priv(net_dev);
763
764 batadv_dat_tvlv_container_update(bat_priv);
765}
766
767
768
769
770
771
772
773
774
775static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
776 struct batadv_orig_node *orig,
777 u8 flags,
778 void *tvlv_value, u16 tvlv_value_len)
779{
780 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
781 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
782 else
783 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
784}
785
786
787
788
789
790static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
791{
792 if (!bat_priv->dat.hash)
793 return;
794
795 __batadv_dat_purge(bat_priv, NULL);
796
797 batadv_hash_destroy(bat_priv->dat.hash);
798
799 bat_priv->dat.hash = NULL;
800}
801
802
803
804
805
806
807
808int batadv_dat_init(struct batadv_priv *bat_priv)
809{
810 if (bat_priv->dat.hash)
811 return 0;
812
813 bat_priv->dat.hash = batadv_hash_new(1024);
814
815 if (!bat_priv->dat.hash)
816 return -ENOMEM;
817
818 batadv_dat_start_timer(bat_priv);
819
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);
824 return 0;
825}
826
827
828
829
830
831void batadv_dat_free(struct batadv_priv *bat_priv)
832{
833 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
834 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
835
836 cancel_delayed_work_sync(&bat_priv->dat.work);
837
838 batadv_dat_hash_free(bat_priv);
839}
840
841#ifdef CONFIG_BATMAN_ADV_DEBUGFS
842
843
844
845
846
847
848
849int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
850{
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;
859 u32 i;
860
861 primary_if = batadv_seq_print_text_primary_if_get(seq);
862 if (!primary_if)
863 goto out;
864
865 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
866 seq_puts(seq,
867 " IPv4 MAC VID last-seen\n");
868
869 for (i = 0; i < hash->size; i++) {
870 head = &hash->table[i];
871
872 rcu_read_lock();
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;
879
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);
884 }
885 rcu_read_unlock();
886 }
887
888out:
889 if (primary_if)
890 batadv_hardif_put(primary_if);
891 return 0;
892}
893#endif
894
895
896
897
898
899
900
901
902
903
904
905static int
906batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
907 struct netlink_callback *cb,
908 struct batadv_dat_entry *dat_entry)
909{
910 int msecs;
911 void *hdr;
912
913 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
914 &batadv_netlink_family, NLM_F_MULTI,
915 BATADV_CMD_GET_DAT_CACHE);
916 if (!hdr)
917 return -ENOBUFS;
918
919 genl_dump_check_consistent(cb, hdr);
920
921 msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
922
923 if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
924 dat_entry->ip) ||
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);
930 return -EMSGSIZE;
931 }
932
933 genlmsg_end(msg, hdr);
934 return 0;
935}
936
937
938
939
940
941
942
943
944
945
946
947
948
949static int
950batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
951 struct netlink_callback *cb,
952 struct batadv_hashtable *hash, unsigned int bucket,
953 int *idx_skip)
954{
955 struct batadv_dat_entry *dat_entry;
956 int idx = 0;
957
958 spin_lock_bh(&hash->list_locks[bucket]);
959 cb->seq = atomic_read(&hash->generation) << 1 | 1;
960
961 hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
962 if (idx < *idx_skip)
963 goto skip;
964
965 if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
966 spin_unlock_bh(&hash->list_locks[bucket]);
967 *idx_skip = idx;
968
969 return -EMSGSIZE;
970 }
971
972skip:
973 idx++;
974 }
975 spin_unlock_bh(&hash->list_locks[bucket]);
976
977 return 0;
978}
979
980
981
982
983
984
985
986
987int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
988{
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];
997 int ifindex;
998 int ret = 0;
999
1000 ifindex = batadv_netlink_get_ifindex(cb->nlh,
1001 BATADV_ATTR_MESH_IFINDEX);
1002 if (!ifindex)
1003 return -EINVAL;
1004
1005 soft_iface = dev_get_by_index(net, ifindex);
1006 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1007 ret = -ENODEV;
1008 goto out;
1009 }
1010
1011 bat_priv = netdev_priv(soft_iface);
1012 hash = bat_priv->dat.hash;
1013
1014 primary_if = batadv_primary_if_get_selected(bat_priv);
1015 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1016 ret = -ENOENT;
1017 goto out;
1018 }
1019
1020 while (bucket < hash->size) {
1021 if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
1022 &idx))
1023 break;
1024
1025 bucket++;
1026 idx = 0;
1027 }
1028
1029 cb->args[0] = bucket;
1030 cb->args[1] = idx;
1031
1032 ret = msg->len;
1033
1034out:
1035 if (primary_if)
1036 batadv_hardif_put(primary_if);
1037
1038 if (soft_iface)
1039 dev_put(soft_iface);
1040
1041 return ret;
1042}
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1053 struct sk_buff *skb, int hdr_size)
1054{
1055 struct arphdr *arphdr;
1056 struct ethhdr *ethhdr;
1057 __be32 ip_src, ip_dst;
1058 u8 *hw_src, *hw_dst;
1059 u16 type = 0;
1060
1061
1062 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1063 goto out;
1064
1065 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1066
1067 if (ethhdr->h_proto != htons(ETH_P_ARP))
1068 goto out;
1069
1070
1071 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1072 arp_hdr_len(skb->dev))))
1073 goto out;
1074
1075 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1076
1077
1078 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1079 goto out;
1080
1081 if (arphdr->ar_pro != htons(ETH_P_IP))
1082 goto out;
1083
1084 if (arphdr->ar_hln != ETH_ALEN)
1085 goto out;
1086
1087 if (arphdr->ar_pln != 4)
1088 goto out;
1089
1090
1091
1092
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))
1099 goto out;
1100
1101 hw_src = batadv_arp_hw_src(skb, hdr_size);
1102 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1103 goto out;
1104
1105
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))
1110 goto out;
1111 }
1112
1113 type = ntohs(arphdr->ar_op);
1114out:
1115 return type;
1116}
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1128{
1129 unsigned short vid;
1130
1131 vid = batadv_get_vid(skb, *hdr_size);
1132
1133
1134
1135
1136
1137
1138 if (vid & BATADV_VLAN_HAS_TAG)
1139 *hdr_size += VLAN_HLEN;
1140
1141 return vid;
1142}
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158static struct sk_buff *
1159batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1160 __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1161 unsigned short vid)
1162{
1163 struct sk_buff *skb;
1164
1165 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
1166 ip_src, hw_dst, hw_src, hw_dst);
1167 if (!skb)
1168 return NULL;
1169
1170 skb_reset_mac_header(skb);
1171
1172 if (vid & BATADV_VLAN_HAS_TAG)
1173 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1174 vid & VLAN_VID_MASK);
1175
1176 return skb;
1177}
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1190 struct sk_buff *skb)
1191{
1192 u16 type = 0;
1193 __be32 ip_dst, ip_src;
1194 u8 *hw_src;
1195 bool ret = false;
1196 struct batadv_dat_entry *dat_entry = NULL;
1197 struct sk_buff *skb_new;
1198 struct net_device *soft_iface = bat_priv->soft_iface;
1199 int hdr_size = 0;
1200 unsigned short vid;
1201
1202 if (!atomic_read(&bat_priv->distributed_arp_table))
1203 goto out;
1204
1205 vid = batadv_dat_get_vid(skb, &hdr_size);
1206
1207 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1208
1209
1210
1211 if (type != ARPOP_REQUEST)
1212 goto out;
1213
1214 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
1215
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);
1219
1220 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1221
1222 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1223 if (dat_entry) {
1224
1225
1226
1227
1228
1229
1230
1231
1232 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
1233 ret = true;
1234 goto out;
1235 }
1236
1237
1238
1239
1240
1241
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);
1247 ret = true;
1248 goto out;
1249 }
1250
1251 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1252 dat_entry->mac_addr,
1253 hw_src, vid);
1254 if (!skb_new)
1255 goto out;
1256
1257 skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1258
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);
1262
1263 netif_rx(skb_new);
1264 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1265 ret = true;
1266 } else {
1267
1268 ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1269 BATADV_P_DAT_DHT_GET);
1270 }
1271out:
1272 if (dat_entry)
1273 batadv_dat_entry_put(dat_entry);
1274 return ret;
1275}
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1287 struct sk_buff *skb, int hdr_size)
1288{
1289 u16 type;
1290 __be32 ip_src, ip_dst;
1291 u8 *hw_src;
1292 struct sk_buff *skb_new;
1293 struct batadv_dat_entry *dat_entry = NULL;
1294 bool ret = false;
1295 unsigned short vid;
1296 int err;
1297
1298 if (!atomic_read(&bat_priv->distributed_arp_table))
1299 goto out;
1300
1301 vid = batadv_dat_get_vid(skb, &hdr_size);
1302
1303 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1304 if (type != ARPOP_REQUEST)
1305 goto out;
1306
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);
1310
1311 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
1312
1313 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1314
1315 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1316 if (!dat_entry)
1317 goto out;
1318
1319 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1320 dat_entry->mac_addr, hw_src, vid);
1321 if (!skb_new)
1322 goto out;
1323
1324
1325
1326
1327
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,
1331 NULL, vid);
1332 else
1333 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
1334
1335 if (err != NET_XMIT_DROP) {
1336 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
1337 ret = true;
1338 }
1339out:
1340 if (dat_entry)
1341 batadv_dat_entry_put(dat_entry);
1342 if (ret)
1343 kfree_skb(skb);
1344 return ret;
1345}
1346
1347
1348
1349
1350
1351
1352void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1353 struct sk_buff *skb)
1354{
1355 u16 type;
1356 __be32 ip_src, ip_dst;
1357 u8 *hw_src, *hw_dst;
1358 int hdr_size = 0;
1359 unsigned short vid;
1360
1361 if (!atomic_read(&bat_priv->distributed_arp_table))
1362 return;
1363
1364 vid = batadv_dat_get_vid(skb, &hdr_size);
1365
1366 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1367 if (type != ARPOP_REPLY)
1368 return;
1369
1370 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
1371
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);
1376
1377 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1378 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1379
1380
1381
1382
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);
1387}
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1400 struct sk_buff *skb, int hdr_size)
1401{
1402 struct batadv_dat_entry *dat_entry = NULL;
1403 u16 type;
1404 __be32 ip_src, ip_dst;
1405 u8 *hw_src, *hw_dst;
1406 bool dropped = false;
1407 unsigned short vid;
1408
1409 if (!atomic_read(&bat_priv->distributed_arp_table))
1410 goto out;
1411
1412 vid = batadv_dat_get_vid(skb, &hdr_size);
1413
1414 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1415 if (type != ARPOP_REPLY)
1416 goto out;
1417
1418 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
1419
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);
1424
1425
1426
1427
1428
1429
1430
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);
1436 dropped = true;
1437 }
1438
1439
1440
1441
1442 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1443 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1444
1445 if (dropped)
1446 goto out;
1447
1448
1449
1450
1451
1452
1453
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",
1457 hw_src);
1458 dropped = true;
1459 goto out;
1460 }
1461
1462
1463
1464
1465 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1466
1467
1468
1469
1470 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
1471out:
1472 if (dropped)
1473 kfree_skb(skb);
1474 if (dat_entry)
1475 batadv_dat_entry_put(dat_entry);
1476
1477 return dropped;
1478}
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491static bool
1492batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
1493{
1494 unsigned int offset = skb_network_offset(skb);
1495 struct udphdr *udphdr, _udphdr;
1496 struct iphdr *iphdr, _iphdr;
1497
1498 iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
1499 if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
1500 return false;
1501
1502 if (iphdr->protocol != IPPROTO_UDP)
1503 return false;
1504
1505 offset += iphdr->ihl * 4;
1506 skb_set_transport_header(skb, offset);
1507
1508 udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
1509 if (!udphdr || udphdr->source != htons(67))
1510 return false;
1511
1512 *ip_src = get_unaligned(&iphdr->saddr);
1513
1514 return true;
1515}
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531static int
1532batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
1533{
1534 __be32 *magic, _magic;
1535 unsigned int offset;
1536 struct {
1537 __u8 op;
1538 __u8 htype;
1539 __u8 hlen;
1540 __u8 hops;
1541 } *dhcp_h, _dhcp_h;
1542
1543 if (proto != htons(ETH_P_IP))
1544 return -EINVAL;
1545
1546 if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
1547 return -EINVAL;
1548
1549 offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1550 if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
1551 return -EINVAL;
1552
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)
1556 return -EINVAL;
1557
1558 offset += offsetof(struct batadv_dhcp_packet, magic);
1559
1560 magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
1561 if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
1562 return -EINVAL;
1563
1564 return dhcp_h->op;
1565}
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
1580{
1581 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1582 u8 *type, _type;
1583 struct {
1584 u8 type;
1585 u8 len;
1586 } *tl, _tl;
1587
1588 offset += sizeof(struct batadv_dhcp_packet);
1589
1590 while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
1591 if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
1592 break;
1593
1594 if (tl->type == BATADV_DHCP_OPT_END)
1595 break;
1596
1597 if (tl->type == BATADV_DHCP_OPT_PAD)
1598 offset++;
1599 else
1600 offset += tl->len + sizeof(_tl);
1601 }
1602
1603
1604 if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
1605 tl->len != sizeof(_type))
1606 return -EINVAL;
1607
1608 offset += sizeof(_tl);
1609
1610 type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
1611 if (!type)
1612 return -EINVAL;
1613
1614 return *type;
1615}
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
1628{
1629 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1630 __be32 *yiaddr;
1631
1632 offset += offsetof(struct batadv_dhcp_packet, yiaddr);
1633 yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
1634
1635 if (!yiaddr)
1636 return false;
1637
1638 if (yiaddr != buf)
1639 *buf = get_unaligned(yiaddr);
1640
1641 return true;
1642}
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
1655{
1656 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1657 u8 *chaddr;
1658
1659 offset += offsetof(struct batadv_dhcp_packet, chaddr);
1660 chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
1661
1662 if (!chaddr)
1663 return false;
1664
1665 if (chaddr != buf)
1666 memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
1667
1668 return true;
1669}
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
1688 __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
1689 unsigned short vid)
1690{
1691 struct sk_buff *skb;
1692
1693 skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
1694 hw_dst, vid);
1695 if (!skb)
1696 return;
1697
1698 skb_set_network_header(skb, ETH_HLEN);
1699
1700 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1701 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1702
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);
1707
1708 consume_skb(skb);
1709
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));
1716}
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734static bool
1735batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
1736 u8 *chaddr, __be32 *yiaddr)
1737{
1738 int type;
1739
1740 type = batadv_dat_check_dhcp(skb, proto, ip_src);
1741 if (type != BATADV_BOOTREPLY)
1742 return false;
1743
1744 type = batadv_dat_get_dhcp_message_type(skb);
1745 if (type != BATADV_DHCPACK)
1746 return false;
1747
1748 if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
1749 return false;
1750
1751 if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
1752 return false;
1753
1754 return true;
1755}
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
1773 struct sk_buff *skb,
1774 __be16 proto,
1775 unsigned short vid)
1776{
1777 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1778 __be32 ip_src, yiaddr;
1779
1780 if (!atomic_read(&bat_priv->distributed_arp_table))
1781 return;
1782
1783 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1784 return;
1785
1786 batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
1787 ip_src, vid);
1788}
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
1801 struct sk_buff *skb, int hdr_size)
1802{
1803 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1804 struct ethhdr *ethhdr;
1805 __be32 ip_src, yiaddr;
1806 unsigned short vid;
1807 __be16 proto;
1808 u8 *hw_src;
1809
1810 if (!atomic_read(&bat_priv->distributed_arp_table))
1811 return;
1812
1813 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1814 return;
1815
1816 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1817 skb_set_network_header(skb, hdr_size + ETH_HLEN);
1818 proto = ethhdr->h_proto;
1819
1820 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1821 return;
1822
1823 hw_src = ethhdr->h_source;
1824 vid = batadv_dat_get_vid(skb, &hdr_size);
1825
1826 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1827 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1828
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));
1835}
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1846 struct batadv_forw_packet *forw_packet)
1847{
1848 u16 type;
1849 __be32 ip_dst;
1850 struct batadv_dat_entry *dat_entry = NULL;
1851 bool ret = false;
1852 int hdr_size = sizeof(struct batadv_bcast_packet);
1853 unsigned short vid;
1854
1855 if (!atomic_read(&bat_priv->distributed_arp_table))
1856 goto out;
1857
1858
1859
1860
1861 if (batadv_forw_packet_is_rebroadcast(forw_packet))
1862 goto out;
1863
1864 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1865
1866 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1867 if (type != ARPOP_REQUEST)
1868 goto out;
1869
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
1873 if (!dat_entry) {
1874 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1875 "ARP Request for %pI4: fallback\n", &ip_dst);
1876 goto out;
1877 }
1878
1879 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1880 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1881 ret = true;
1882
1883out:
1884 if (dat_entry)
1885 batadv_dat_entry_put(dat_entry);
1886 return ret;
1887}
1888