0001
0002
0003
0004
0005
0006
0007 #include "distributed-arp-table.h"
0008 #include "main.h"
0009
0010 #include <asm/unaligned.h>
0011 #include <linux/atomic.h>
0012 #include <linux/bitops.h>
0013 #include <linux/byteorder/generic.h>
0014 #include <linux/container_of.h>
0015 #include <linux/errno.h>
0016 #include <linux/etherdevice.h>
0017 #include <linux/gfp.h>
0018 #include <linux/if_arp.h>
0019 #include <linux/if_ether.h>
0020 #include <linux/if_vlan.h>
0021 #include <linux/in.h>
0022 #include <linux/ip.h>
0023 #include <linux/jiffies.h>
0024 #include <linux/kref.h>
0025 #include <linux/list.h>
0026 #include <linux/netlink.h>
0027 #include <linux/rculist.h>
0028 #include <linux/rcupdate.h>
0029 #include <linux/skbuff.h>
0030 #include <linux/slab.h>
0031 #include <linux/spinlock.h>
0032 #include <linux/stddef.h>
0033 #include <linux/string.h>
0034 #include <linux/udp.h>
0035 #include <linux/workqueue.h>
0036 #include <net/arp.h>
0037 #include <net/genetlink.h>
0038 #include <net/netlink.h>
0039 #include <net/sock.h>
0040 #include <uapi/linux/batman_adv.h>
0041
0042 #include "bridge_loop_avoidance.h"
0043 #include "hard-interface.h"
0044 #include "hash.h"
0045 #include "log.h"
0046 #include "netlink.h"
0047 #include "originator.h"
0048 #include "send.h"
0049 #include "soft-interface.h"
0050 #include "translation-table.h"
0051 #include "tvlv.h"
0052
0053 enum batadv_bootpop {
0054 BATADV_BOOTREPLY = 2,
0055 };
0056
0057 enum batadv_boothtype {
0058 BATADV_HTYPE_ETHERNET = 1,
0059 };
0060
0061 enum batadv_dhcpoptioncode {
0062 BATADV_DHCP_OPT_PAD = 0,
0063 BATADV_DHCP_OPT_MSG_TYPE = 53,
0064 BATADV_DHCP_OPT_END = 255,
0065 };
0066
0067 enum batadv_dhcptype {
0068 BATADV_DHCPACK = 5,
0069 };
0070
0071
0072 #define BATADV_DHCP_MAGIC 1669485411
0073
0074 struct batadv_dhcp_packet {
0075 __u8 op;
0076 __u8 htype;
0077 __u8 hlen;
0078 __u8 hops;
0079 __be32 xid;
0080 __be16 secs;
0081 __be16 flags;
0082 __be32 ciaddr;
0083 __be32 yiaddr;
0084 __be32 siaddr;
0085 __be32 giaddr;
0086 __u8 chaddr[16];
0087 __u8 sname[64];
0088 __u8 file[128];
0089 __be32 magic;
0090
0091 };
0092
0093 #define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
0094 #define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
0095
0096 static void batadv_dat_purge(struct work_struct *work);
0097
0098
0099
0100
0101
0102 static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
0103 {
0104 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
0105 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
0106 msecs_to_jiffies(10000));
0107 }
0108
0109
0110
0111
0112
0113
0114 static void batadv_dat_entry_release(struct kref *ref)
0115 {
0116 struct batadv_dat_entry *dat_entry;
0117
0118 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
0119
0120 kfree_rcu(dat_entry, rcu);
0121 }
0122
0123
0124
0125
0126
0127
0128 static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
0129 {
0130 if (!dat_entry)
0131 return;
0132
0133 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
0134 }
0135
0136
0137
0138
0139
0140
0141
0142 static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
0143 {
0144 return batadv_has_timed_out(dat_entry->last_update,
0145 BATADV_DAT_ENTRY_TIMEOUT);
0146 }
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 static void __batadv_dat_purge(struct batadv_priv *bat_priv,
0160 bool (*to_purge)(struct batadv_dat_entry *))
0161 {
0162 spinlock_t *list_lock;
0163 struct batadv_dat_entry *dat_entry;
0164 struct hlist_node *node_tmp;
0165 struct hlist_head *head;
0166 u32 i;
0167
0168 if (!bat_priv->dat.hash)
0169 return;
0170
0171 for (i = 0; i < bat_priv->dat.hash->size; i++) {
0172 head = &bat_priv->dat.hash->table[i];
0173 list_lock = &bat_priv->dat.hash->list_locks[i];
0174
0175 spin_lock_bh(list_lock);
0176 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
0177 hash_entry) {
0178
0179
0180
0181 if (to_purge && !to_purge(dat_entry))
0182 continue;
0183
0184 hlist_del_rcu(&dat_entry->hash_entry);
0185 batadv_dat_entry_put(dat_entry);
0186 }
0187 spin_unlock_bh(list_lock);
0188 }
0189 }
0190
0191
0192
0193
0194
0195
0196 static void batadv_dat_purge(struct work_struct *work)
0197 {
0198 struct delayed_work *delayed_work;
0199 struct batadv_priv_dat *priv_dat;
0200 struct batadv_priv *bat_priv;
0201
0202 delayed_work = to_delayed_work(work);
0203 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
0204 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
0205
0206 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
0207 batadv_dat_start_timer(bat_priv);
0208 }
0209
0210
0211
0212
0213
0214
0215
0216
0217 static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
0218 {
0219 const void *data1 = container_of(node, struct batadv_dat_entry,
0220 hash_entry);
0221
0222 return memcmp(data1, data2, sizeof(__be32)) == 0;
0223 }
0224
0225
0226
0227
0228
0229
0230
0231
0232 static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
0233 {
0234 u8 *addr;
0235
0236 addr = (u8 *)(skb->data + hdr_size);
0237 addr += ETH_HLEN + sizeof(struct arphdr);
0238
0239 return addr;
0240 }
0241
0242
0243
0244
0245
0246
0247
0248
0249 static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
0250 {
0251 return *(__force __be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
0252 }
0253
0254
0255
0256
0257
0258
0259
0260
0261 static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
0262 {
0263 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
0264 }
0265
0266
0267
0268
0269
0270
0271
0272
0273 static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
0274 {
0275 u8 *dst = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4;
0276
0277 return *(__force __be32 *)dst;
0278 }
0279
0280
0281
0282
0283
0284
0285
0286
0287 static u32 batadv_hash_dat(const void *data, u32 size)
0288 {
0289 u32 hash = 0;
0290 const struct batadv_dat_entry *dat = data;
0291 const unsigned char *key;
0292 __be16 vid;
0293 u32 i;
0294
0295 key = (__force const unsigned char *)&dat->ip;
0296 for (i = 0; i < sizeof(dat->ip); i++) {
0297 hash += key[i];
0298 hash += (hash << 10);
0299 hash ^= (hash >> 6);
0300 }
0301
0302 vid = htons(dat->vid);
0303 key = (__force const unsigned char *)&vid;
0304 for (i = 0; i < sizeof(dat->vid); i++) {
0305 hash += key[i];
0306 hash += (hash << 10);
0307 hash ^= (hash >> 6);
0308 }
0309
0310 hash += (hash << 3);
0311 hash ^= (hash >> 11);
0312 hash += (hash << 15);
0313
0314 return hash % size;
0315 }
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326 static struct batadv_dat_entry *
0327 batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
0328 unsigned short vid)
0329 {
0330 struct hlist_head *head;
0331 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
0332 struct batadv_hashtable *hash = bat_priv->dat.hash;
0333 u32 index;
0334
0335 if (!hash)
0336 return NULL;
0337
0338 to_find.ip = ip;
0339 to_find.vid = vid;
0340
0341 index = batadv_hash_dat(&to_find, hash->size);
0342 head = &hash->table[index];
0343
0344 rcu_read_lock();
0345 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
0346 if (dat_entry->ip != ip)
0347 continue;
0348
0349 if (!kref_get_unless_zero(&dat_entry->refcount))
0350 continue;
0351
0352 dat_entry_tmp = dat_entry;
0353 break;
0354 }
0355 rcu_read_unlock();
0356
0357 return dat_entry_tmp;
0358 }
0359
0360
0361
0362
0363
0364
0365
0366
0367 static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
0368 u8 *mac_addr, unsigned short vid)
0369 {
0370 struct batadv_dat_entry *dat_entry;
0371 int hash_added;
0372
0373 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
0374
0375 if (dat_entry) {
0376 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
0377 ether_addr_copy(dat_entry->mac_addr, mac_addr);
0378 dat_entry->last_update = jiffies;
0379 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0380 "Entry updated: %pI4 %pM (vid: %d)\n",
0381 &dat_entry->ip, dat_entry->mac_addr,
0382 batadv_print_vid(vid));
0383 goto out;
0384 }
0385
0386 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
0387 if (!dat_entry)
0388 goto out;
0389
0390 dat_entry->ip = ip;
0391 dat_entry->vid = vid;
0392 ether_addr_copy(dat_entry->mac_addr, mac_addr);
0393 dat_entry->last_update = jiffies;
0394 kref_init(&dat_entry->refcount);
0395
0396 kref_get(&dat_entry->refcount);
0397 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
0398 batadv_hash_dat, dat_entry,
0399 &dat_entry->hash_entry);
0400
0401 if (unlikely(hash_added != 0)) {
0402
0403 batadv_dat_entry_put(dat_entry);
0404 goto out;
0405 }
0406
0407 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
0408 &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
0409
0410 out:
0411 batadv_dat_entry_put(dat_entry);
0412 }
0413
0414 #ifdef CONFIG_BATMAN_ADV_DEBUG
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
0425 int hdr_size, char *msg)
0426 {
0427 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
0428 struct batadv_bcast_packet *bcast_pkt;
0429 u8 *orig_addr;
0430 __be32 ip_src, ip_dst;
0431
0432 if (msg)
0433 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
0434
0435 ip_src = batadv_arp_ip_src(skb, hdr_size);
0436 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
0437 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0438 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
0439 batadv_arp_hw_src(skb, hdr_size), &ip_src,
0440 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
0441
0442 if (hdr_size < sizeof(struct batadv_unicast_packet))
0443 return;
0444
0445 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
0446
0447 switch (unicast_4addr_packet->u.packet_type) {
0448 case BATADV_UNICAST:
0449 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0450 "* encapsulated within a UNICAST packet\n");
0451 break;
0452 case BATADV_UNICAST_4ADDR:
0453 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0454 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
0455 unicast_4addr_packet->src);
0456 switch (unicast_4addr_packet->subtype) {
0457 case BATADV_P_DAT_DHT_PUT:
0458 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
0459 break;
0460 case BATADV_P_DAT_DHT_GET:
0461 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
0462 break;
0463 case BATADV_P_DAT_CACHE_REPLY:
0464 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0465 "* type: DAT_CACHE_REPLY\n");
0466 break;
0467 case BATADV_P_DATA:
0468 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
0469 break;
0470 default:
0471 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
0472 unicast_4addr_packet->u.packet_type);
0473 }
0474 break;
0475 case BATADV_BCAST:
0476 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
0477 orig_addr = bcast_pkt->orig;
0478 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0479 "* encapsulated within a BCAST packet (src: %pM)\n",
0480 orig_addr);
0481 break;
0482 default:
0483 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0484 "* encapsulated within an unknown packet type (0x%x)\n",
0485 unicast_4addr_packet->u.packet_type);
0486 }
0487 }
0488
0489 #else
0490
0491 static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
0492 int hdr_size, char *msg)
0493 {
0494 }
0495
0496 #endif
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511 static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
0512 int select, batadv_dat_addr_t tmp_max,
0513 batadv_dat_addr_t max,
0514 batadv_dat_addr_t last_max,
0515 struct batadv_orig_node *candidate,
0516 struct batadv_orig_node *max_orig_node)
0517 {
0518 bool ret = false;
0519 int j;
0520
0521
0522 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
0523 goto out;
0524
0525
0526 for (j = 0; j < select; j++)
0527 if (res[j].orig_node == candidate)
0528 break;
0529
0530 if (j < select)
0531 goto out;
0532
0533 if (tmp_max > last_max)
0534 goto out;
0535
0536
0537
0538 if (tmp_max < max)
0539 goto out;
0540
0541
0542
0543 if (tmp_max == max && max_orig_node &&
0544 batadv_compare_eth(candidate->orig, max_orig_node->orig))
0545 goto out;
0546
0547 ret = true;
0548 out:
0549 return ret;
0550 }
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560 static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
0561 struct batadv_dat_candidate *cands,
0562 int select, batadv_dat_addr_t ip_key,
0563 batadv_dat_addr_t *last_max)
0564 {
0565 batadv_dat_addr_t max = 0;
0566 batadv_dat_addr_t tmp_max = 0;
0567 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
0568 struct batadv_hashtable *hash = bat_priv->orig_hash;
0569 struct hlist_head *head;
0570 int i;
0571
0572
0573
0574
0575 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
0576
0577
0578
0579
0580 for (i = 0; i < hash->size; i++) {
0581 head = &hash->table[i];
0582
0583 rcu_read_lock();
0584 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
0585
0586 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
0587 ip_key;
0588
0589 if (!batadv_is_orig_node_eligible(cands, select,
0590 tmp_max, max,
0591 *last_max, orig_node,
0592 max_orig_node))
0593 continue;
0594
0595 if (!kref_get_unless_zero(&orig_node->refcount))
0596 continue;
0597
0598 max = tmp_max;
0599 batadv_orig_node_put(max_orig_node);
0600 max_orig_node = orig_node;
0601 }
0602 rcu_read_unlock();
0603 }
0604 if (max_orig_node) {
0605 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
0606 cands[select].orig_node = max_orig_node;
0607 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0608 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
0609 select, max_orig_node->orig, max_orig_node->dat_addr,
0610 max);
0611 }
0612 *last_max = max;
0613 }
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628 static struct batadv_dat_candidate *
0629 batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
0630 unsigned short vid)
0631 {
0632 int select;
0633 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
0634 struct batadv_dat_candidate *res;
0635 struct batadv_dat_entry dat;
0636
0637 if (!bat_priv->orig_hash)
0638 return NULL;
0639
0640 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
0641 GFP_ATOMIC);
0642 if (!res)
0643 return NULL;
0644
0645 dat.ip = ip_dst;
0646 dat.vid = vid;
0647 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
0648 BATADV_DAT_ADDR_MAX);
0649
0650 batadv_dbg(BATADV_DBG_DAT, bat_priv,
0651 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
0652 ip_key);
0653
0654 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
0655 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
0656 &last_max);
0657
0658 return res;
0659 }
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675 static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
0676 struct sk_buff *skb, __be32 ip,
0677 unsigned short vid, int packet_subtype)
0678 {
0679 int i;
0680 bool ret = false;
0681 int send_status;
0682 struct batadv_neigh_node *neigh_node = NULL;
0683 struct sk_buff *tmp_skb;
0684 struct batadv_dat_candidate *cand;
0685
0686 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
0687 if (!cand)
0688 goto out;
0689
0690 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
0691
0692 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
0693 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
0694 continue;
0695
0696 neigh_node = batadv_orig_router_get(cand[i].orig_node,
0697 BATADV_IF_DEFAULT);
0698 if (!neigh_node)
0699 goto free_orig;
0700
0701 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
0702 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
0703 cand[i].orig_node,
0704 packet_subtype)) {
0705 kfree_skb(tmp_skb);
0706 goto free_neigh;
0707 }
0708
0709 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
0710 if (send_status == NET_XMIT_SUCCESS) {
0711
0712 switch (packet_subtype) {
0713 case BATADV_P_DAT_DHT_GET:
0714 batadv_inc_counter(bat_priv,
0715 BATADV_CNT_DAT_GET_TX);
0716 break;
0717 case BATADV_P_DAT_DHT_PUT:
0718 batadv_inc_counter(bat_priv,
0719 BATADV_CNT_DAT_PUT_TX);
0720 break;
0721 }
0722
0723
0724 ret = true;
0725 }
0726 free_neigh:
0727 batadv_neigh_node_put(neigh_node);
0728 free_orig:
0729 batadv_orig_node_put(cand[i].orig_node);
0730 }
0731
0732 out:
0733 kfree(cand);
0734 return ret;
0735 }
0736
0737
0738
0739
0740
0741
0742 static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
0743 {
0744 char dat_mode;
0745
0746 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
0747
0748 switch (dat_mode) {
0749 case 0:
0750 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
0751 break;
0752 case 1:
0753 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
0754 NULL, 0);
0755 break;
0756 }
0757 }
0758
0759
0760
0761
0762
0763
0764 void batadv_dat_status_update(struct net_device *net_dev)
0765 {
0766 struct batadv_priv *bat_priv = netdev_priv(net_dev);
0767
0768 batadv_dat_tvlv_container_update(bat_priv);
0769 }
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779 static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
0780 struct batadv_orig_node *orig,
0781 u8 flags,
0782 void *tvlv_value, u16 tvlv_value_len)
0783 {
0784 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
0785 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
0786 else
0787 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
0788 }
0789
0790
0791
0792
0793
0794 static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
0795 {
0796 if (!bat_priv->dat.hash)
0797 return;
0798
0799 __batadv_dat_purge(bat_priv, NULL);
0800
0801 batadv_hash_destroy(bat_priv->dat.hash);
0802
0803 bat_priv->dat.hash = NULL;
0804 }
0805
0806
0807
0808
0809
0810
0811
0812 int batadv_dat_init(struct batadv_priv *bat_priv)
0813 {
0814 if (bat_priv->dat.hash)
0815 return 0;
0816
0817 bat_priv->dat.hash = batadv_hash_new(1024);
0818
0819 if (!bat_priv->dat.hash)
0820 return -ENOMEM;
0821
0822 batadv_dat_start_timer(bat_priv);
0823
0824 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
0825 NULL, BATADV_TVLV_DAT, 1,
0826 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
0827 batadv_dat_tvlv_container_update(bat_priv);
0828 return 0;
0829 }
0830
0831
0832
0833
0834
0835 void batadv_dat_free(struct batadv_priv *bat_priv)
0836 {
0837 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
0838 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
0839
0840 cancel_delayed_work_sync(&bat_priv->dat.work);
0841
0842 batadv_dat_hash_free(bat_priv);
0843 }
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855 static int
0856 batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
0857 struct netlink_callback *cb,
0858 struct batadv_dat_entry *dat_entry)
0859 {
0860 int msecs;
0861 void *hdr;
0862
0863 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
0864 &batadv_netlink_family, NLM_F_MULTI,
0865 BATADV_CMD_GET_DAT_CACHE);
0866 if (!hdr)
0867 return -ENOBUFS;
0868
0869 genl_dump_check_consistent(cb, hdr);
0870
0871 msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
0872
0873 if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
0874 dat_entry->ip) ||
0875 nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
0876 dat_entry->mac_addr) ||
0877 nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
0878 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
0879 genlmsg_cancel(msg, hdr);
0880 return -EMSGSIZE;
0881 }
0882
0883 genlmsg_end(msg, hdr);
0884 return 0;
0885 }
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899 static int
0900 batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
0901 struct netlink_callback *cb,
0902 struct batadv_hashtable *hash, unsigned int bucket,
0903 int *idx_skip)
0904 {
0905 struct batadv_dat_entry *dat_entry;
0906 int idx = 0;
0907
0908 spin_lock_bh(&hash->list_locks[bucket]);
0909 cb->seq = atomic_read(&hash->generation) << 1 | 1;
0910
0911 hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
0912 if (idx < *idx_skip)
0913 goto skip;
0914
0915 if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
0916 spin_unlock_bh(&hash->list_locks[bucket]);
0917 *idx_skip = idx;
0918
0919 return -EMSGSIZE;
0920 }
0921
0922 skip:
0923 idx++;
0924 }
0925 spin_unlock_bh(&hash->list_locks[bucket]);
0926
0927 return 0;
0928 }
0929
0930
0931
0932
0933
0934
0935
0936
0937 int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
0938 {
0939 struct batadv_hard_iface *primary_if = NULL;
0940 int portid = NETLINK_CB(cb->skb).portid;
0941 struct net *net = sock_net(cb->skb->sk);
0942 struct net_device *soft_iface;
0943 struct batadv_hashtable *hash;
0944 struct batadv_priv *bat_priv;
0945 int bucket = cb->args[0];
0946 int idx = cb->args[1];
0947 int ifindex;
0948 int ret = 0;
0949
0950 ifindex = batadv_netlink_get_ifindex(cb->nlh,
0951 BATADV_ATTR_MESH_IFINDEX);
0952 if (!ifindex)
0953 return -EINVAL;
0954
0955 soft_iface = dev_get_by_index(net, ifindex);
0956 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
0957 ret = -ENODEV;
0958 goto out;
0959 }
0960
0961 bat_priv = netdev_priv(soft_iface);
0962 hash = bat_priv->dat.hash;
0963
0964 primary_if = batadv_primary_if_get_selected(bat_priv);
0965 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
0966 ret = -ENOENT;
0967 goto out;
0968 }
0969
0970 while (bucket < hash->size) {
0971 if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
0972 &idx))
0973 break;
0974
0975 bucket++;
0976 idx = 0;
0977 }
0978
0979 cb->args[0] = bucket;
0980 cb->args[1] = idx;
0981
0982 ret = msg->len;
0983
0984 out:
0985 batadv_hardif_put(primary_if);
0986
0987 dev_put(soft_iface);
0988
0989 return ret;
0990 }
0991
0992
0993
0994
0995
0996
0997
0998
0999
1000 static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1001 struct sk_buff *skb, int hdr_size)
1002 {
1003 struct arphdr *arphdr;
1004 struct ethhdr *ethhdr;
1005 __be32 ip_src, ip_dst;
1006 u8 *hw_src, *hw_dst;
1007 u16 type = 0;
1008
1009
1010 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1011 goto out;
1012
1013 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1014
1015 if (ethhdr->h_proto != htons(ETH_P_ARP))
1016 goto out;
1017
1018
1019 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1020 arp_hdr_len(skb->dev))))
1021 goto out;
1022
1023 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1024
1025
1026 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1027 goto out;
1028
1029 if (arphdr->ar_pro != htons(ETH_P_IP))
1030 goto out;
1031
1032 if (arphdr->ar_hln != ETH_ALEN)
1033 goto out;
1034
1035 if (arphdr->ar_pln != 4)
1036 goto out;
1037
1038
1039
1040
1041 ip_src = batadv_arp_ip_src(skb, hdr_size);
1042 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1043 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
1044 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
1045 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
1046 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
1047 goto out;
1048
1049 hw_src = batadv_arp_hw_src(skb, hdr_size);
1050 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1051 goto out;
1052
1053
1054 if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
1055 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1056 if (is_zero_ether_addr(hw_dst) ||
1057 is_multicast_ether_addr(hw_dst))
1058 goto out;
1059 }
1060
1061 type = ntohs(arphdr->ar_op);
1062 out:
1063 return type;
1064 }
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075 static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1076 {
1077 unsigned short vid;
1078
1079 vid = batadv_get_vid(skb, *hdr_size);
1080
1081
1082
1083
1084
1085
1086 if (vid & BATADV_VLAN_HAS_TAG)
1087 *hdr_size += VLAN_HLEN;
1088
1089 return vid;
1090 }
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106 static struct sk_buff *
1107 batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1108 __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1109 unsigned short vid)
1110 {
1111 struct sk_buff *skb;
1112
1113 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
1114 ip_src, hw_dst, hw_src, hw_dst);
1115 if (!skb)
1116 return NULL;
1117
1118 skb_reset_mac_header(skb);
1119
1120 if (vid & BATADV_VLAN_HAS_TAG)
1121 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1122 vid & VLAN_VID_MASK);
1123
1124 return skb;
1125 }
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137 bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1138 struct sk_buff *skb)
1139 {
1140 u16 type = 0;
1141 __be32 ip_dst, ip_src;
1142 u8 *hw_src;
1143 bool ret = false;
1144 struct batadv_dat_entry *dat_entry = NULL;
1145 struct sk_buff *skb_new;
1146 struct net_device *soft_iface = bat_priv->soft_iface;
1147 int hdr_size = 0;
1148 unsigned short vid;
1149
1150 if (!atomic_read(&bat_priv->distributed_arp_table))
1151 goto out;
1152
1153 vid = batadv_dat_get_vid(skb, &hdr_size);
1154
1155 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1156
1157
1158
1159 if (type != ARPOP_REQUEST)
1160 goto out;
1161
1162 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
1163
1164 ip_src = batadv_arp_ip_src(skb, hdr_size);
1165 hw_src = batadv_arp_hw_src(skb, hdr_size);
1166 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1167
1168 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1169
1170 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1171 if (dat_entry) {
1172
1173
1174
1175
1176
1177
1178
1179
1180 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
1181 ret = true;
1182 goto out;
1183 }
1184
1185
1186
1187
1188
1189
1190 if (!batadv_bla_check_claim(bat_priv,
1191 dat_entry->mac_addr, vid)) {
1192 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1193 "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1194 dat_entry->mac_addr);
1195 ret = true;
1196 goto out;
1197 }
1198
1199 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1200 dat_entry->mac_addr,
1201 hw_src, vid);
1202 if (!skb_new)
1203 goto out;
1204
1205 skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1206
1207 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1208 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1209 skb->len + ETH_HLEN + hdr_size);
1210
1211 netif_rx(skb_new);
1212 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1213 ret = true;
1214 } else {
1215
1216 ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1217 BATADV_P_DAT_DHT_GET);
1218 }
1219 out:
1220 batadv_dat_entry_put(dat_entry);
1221 return ret;
1222 }
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233 bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1234 struct sk_buff *skb, int hdr_size)
1235 {
1236 u16 type;
1237 __be32 ip_src, ip_dst;
1238 u8 *hw_src;
1239 struct sk_buff *skb_new;
1240 struct batadv_dat_entry *dat_entry = NULL;
1241 bool ret = false;
1242 unsigned short vid;
1243 int err;
1244
1245 if (!atomic_read(&bat_priv->distributed_arp_table))
1246 goto out;
1247
1248 vid = batadv_dat_get_vid(skb, &hdr_size);
1249
1250 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1251 if (type != ARPOP_REQUEST)
1252 goto out;
1253
1254 hw_src = batadv_arp_hw_src(skb, hdr_size);
1255 ip_src = batadv_arp_ip_src(skb, hdr_size);
1256 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1257
1258 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
1259
1260 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1261
1262 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1263 if (!dat_entry)
1264 goto out;
1265
1266 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1267 dat_entry->mac_addr, hw_src, vid);
1268 if (!skb_new)
1269 goto out;
1270
1271
1272
1273
1274
1275 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
1276 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1277 BATADV_P_DAT_CACHE_REPLY,
1278 NULL, vid);
1279 else
1280 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
1281
1282 if (err != NET_XMIT_DROP) {
1283 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
1284 ret = true;
1285 }
1286 out:
1287 batadv_dat_entry_put(dat_entry);
1288 if (ret)
1289 kfree_skb(skb);
1290 return ret;
1291 }
1292
1293
1294
1295
1296
1297
1298 void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1299 struct sk_buff *skb)
1300 {
1301 u16 type;
1302 __be32 ip_src, ip_dst;
1303 u8 *hw_src, *hw_dst;
1304 int hdr_size = 0;
1305 unsigned short vid;
1306
1307 if (!atomic_read(&bat_priv->distributed_arp_table))
1308 return;
1309
1310 vid = batadv_dat_get_vid(skb, &hdr_size);
1311
1312 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1313 if (type != ARPOP_REPLY)
1314 return;
1315
1316 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
1317
1318 hw_src = batadv_arp_hw_src(skb, hdr_size);
1319 ip_src = batadv_arp_ip_src(skb, hdr_size);
1320 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1321 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1322
1323 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1324 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1325
1326
1327
1328
1329 batadv_dat_forward_data(bat_priv, skb, ip_src, vid,
1330 BATADV_P_DAT_DHT_PUT);
1331 batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1332 BATADV_P_DAT_DHT_PUT);
1333 }
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1346 struct sk_buff *skb, int hdr_size)
1347 {
1348 struct batadv_dat_entry *dat_entry = NULL;
1349 u16 type;
1350 __be32 ip_src, ip_dst;
1351 u8 *hw_src, *hw_dst;
1352 bool dropped = false;
1353 unsigned short vid;
1354
1355 if (!atomic_read(&bat_priv->distributed_arp_table))
1356 goto out;
1357
1358 vid = batadv_dat_get_vid(skb, &hdr_size);
1359
1360 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1361 if (type != ARPOP_REPLY)
1362 goto out;
1363
1364 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
1365
1366 hw_src = batadv_arp_hw_src(skb, hdr_size);
1367 ip_src = batadv_arp_ip_src(skb, hdr_size);
1368 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1369 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1370
1371
1372
1373
1374
1375
1376
1377 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1378 if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
1379 batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1380 hw_src, &ip_src, hw_dst, &ip_dst,
1381 dat_entry->mac_addr, &dat_entry->ip);
1382 dropped = true;
1383 }
1384
1385
1386
1387
1388 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1389 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1390
1391 if (dropped)
1392 goto out;
1393
1394
1395
1396
1397
1398
1399
1400 if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1401 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1402 "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1403 hw_src);
1404 dropped = true;
1405 goto out;
1406 }
1407
1408
1409
1410
1411 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1412
1413
1414
1415
1416 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
1417 out:
1418 if (dropped)
1419 kfree_skb(skb);
1420 batadv_dat_entry_put(dat_entry);
1421
1422 return dropped;
1423 }
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436 static bool
1437 batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
1438 {
1439 unsigned int offset = skb_network_offset(skb);
1440 struct udphdr *udphdr, _udphdr;
1441 struct iphdr *iphdr, _iphdr;
1442
1443 iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
1444 if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
1445 return false;
1446
1447 if (iphdr->protocol != IPPROTO_UDP)
1448 return false;
1449
1450 offset += iphdr->ihl * 4;
1451 skb_set_transport_header(skb, offset);
1452
1453 udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
1454 if (!udphdr || udphdr->source != htons(67))
1455 return false;
1456
1457 *ip_src = get_unaligned(&iphdr->saddr);
1458
1459 return true;
1460 }
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476 static int
1477 batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
1478 {
1479 __be32 *magic, _magic;
1480 unsigned int offset;
1481 struct {
1482 __u8 op;
1483 __u8 htype;
1484 __u8 hlen;
1485 __u8 hops;
1486 } *dhcp_h, _dhcp_h;
1487
1488 if (proto != htons(ETH_P_IP))
1489 return -EINVAL;
1490
1491 if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
1492 return -EINVAL;
1493
1494 offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1495 if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
1496 return -EINVAL;
1497
1498 dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
1499 if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
1500 dhcp_h->hlen != ETH_ALEN)
1501 return -EINVAL;
1502
1503 offset += offsetof(struct batadv_dhcp_packet, magic);
1504
1505 magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
1506 if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
1507 return -EINVAL;
1508
1509 return dhcp_h->op;
1510 }
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524 static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
1525 {
1526 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1527 u8 *type, _type;
1528 struct {
1529 u8 type;
1530 u8 len;
1531 } *tl, _tl;
1532
1533 offset += sizeof(struct batadv_dhcp_packet);
1534
1535 while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
1536 if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
1537 break;
1538
1539 if (tl->type == BATADV_DHCP_OPT_END)
1540 break;
1541
1542 if (tl->type == BATADV_DHCP_OPT_PAD)
1543 offset++;
1544 else
1545 offset += tl->len + sizeof(_tl);
1546 }
1547
1548
1549 if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
1550 tl->len != sizeof(_type))
1551 return -EINVAL;
1552
1553 offset += sizeof(_tl);
1554
1555 type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
1556 if (!type)
1557 return -EINVAL;
1558
1559 return *type;
1560 }
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572 static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
1573 {
1574 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1575 __be32 *yiaddr;
1576
1577 offset += offsetof(struct batadv_dhcp_packet, yiaddr);
1578 yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
1579
1580 if (!yiaddr)
1581 return false;
1582
1583 if (yiaddr != buf)
1584 *buf = get_unaligned(yiaddr);
1585
1586 return true;
1587 }
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599 static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
1600 {
1601 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1602 u8 *chaddr;
1603
1604 offset += offsetof(struct batadv_dhcp_packet, chaddr);
1605 chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
1606
1607 if (!chaddr)
1608 return false;
1609
1610 if (chaddr != buf)
1611 memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
1612
1613 return true;
1614 }
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632 static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
1633 __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
1634 unsigned short vid)
1635 {
1636 struct sk_buff *skb;
1637
1638 skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
1639 hw_dst, vid);
1640 if (!skb)
1641 return;
1642
1643 skb_set_network_header(skb, ETH_HLEN);
1644
1645 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1646 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1647
1648 batadv_dat_forward_data(bat_priv, skb, yiaddr, vid,
1649 BATADV_P_DAT_DHT_PUT);
1650 batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1651 BATADV_P_DAT_DHT_PUT);
1652
1653 consume_skb(skb);
1654
1655 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1656 "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1657 &ip_dst, hw_dst, batadv_print_vid(vid));
1658 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1659 "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1660 &yiaddr, chaddr, batadv_print_vid(vid));
1661 }
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679 static bool
1680 batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
1681 u8 *chaddr, __be32 *yiaddr)
1682 {
1683 int type;
1684
1685 type = batadv_dat_check_dhcp(skb, proto, ip_src);
1686 if (type != BATADV_BOOTREPLY)
1687 return false;
1688
1689 type = batadv_dat_get_dhcp_message_type(skb);
1690 if (type != BATADV_DHCPACK)
1691 return false;
1692
1693 if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
1694 return false;
1695
1696 if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
1697 return false;
1698
1699 return true;
1700 }
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717 void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
1718 struct sk_buff *skb,
1719 __be16 proto,
1720 unsigned short vid)
1721 {
1722 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1723 __be32 ip_src, yiaddr;
1724
1725 if (!atomic_read(&bat_priv->distributed_arp_table))
1726 return;
1727
1728 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1729 return;
1730
1731 batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
1732 ip_src, vid);
1733 }
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745 void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
1746 struct sk_buff *skb, int hdr_size)
1747 {
1748 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1749 struct ethhdr *ethhdr;
1750 __be32 ip_src, yiaddr;
1751 unsigned short vid;
1752 __be16 proto;
1753 u8 *hw_src;
1754
1755 if (!atomic_read(&bat_priv->distributed_arp_table))
1756 return;
1757
1758 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1759 return;
1760
1761 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1762 skb_set_network_header(skb, hdr_size + ETH_HLEN);
1763 proto = ethhdr->h_proto;
1764
1765 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1766 return;
1767
1768 hw_src = ethhdr->h_source;
1769 vid = batadv_dat_get_vid(skb, &hdr_size);
1770
1771 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1772 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1773
1774 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1775 "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1776 &ip_src, hw_src, batadv_print_vid(vid));
1777 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1778 "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1779 &yiaddr, chaddr, batadv_print_vid(vid));
1780 }
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790 bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1791 struct batadv_forw_packet *forw_packet)
1792 {
1793 u16 type;
1794 __be32 ip_dst;
1795 struct batadv_dat_entry *dat_entry = NULL;
1796 bool ret = false;
1797 int hdr_size = sizeof(struct batadv_bcast_packet);
1798 unsigned short vid;
1799
1800 if (!atomic_read(&bat_priv->distributed_arp_table))
1801 goto out;
1802
1803
1804
1805
1806 if (batadv_forw_packet_is_rebroadcast(forw_packet))
1807 goto out;
1808
1809 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1810
1811 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1812 if (type != ARPOP_REQUEST)
1813 goto out;
1814
1815 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1816 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1817
1818 if (!dat_entry) {
1819 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1820 "ARP Request for %pI4: fallback\n", &ip_dst);
1821 goto out;
1822 }
1823
1824 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1825 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1826 ret = true;
1827
1828 out:
1829 batadv_dat_entry_put(dat_entry);
1830 return ret;
1831 }