blob: 2f008e32900794ccc7b4de02c4845e6ddcc18f35 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmanncfa55c62021-01-01 00:00:01 +01002/* Copyright (C) B.A.T.M.A.N. contributors:
Antonio Quartulli785ea112011-11-23 11:35:44 +01003 *
4 * Antonio Quartulli
Antonio Quartulli785ea112011-11-23 11:35:44 +01005 */
6
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02007#include "distributed-arp-table.h"
8#include "main.h"
9
Linus Lüssingb61ec312018-12-30 16:52:53 +010010#include <asm/unaligned.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020011#include <linux/atomic.h>
Linus Lüssing65d7d4602015-06-16 17:10:22 +020012#include <linux/bitops.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020013#include <linux/byteorder/generic.h>
14#include <linux/errno.h>
15#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010016#include <linux/gfp.h>
Antonio Quartulli785ea112011-11-23 11:35:44 +010017#include <linux/if_arp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020018#include <linux/if_ether.h>
Antonio Quartullibe1db4f2013-06-04 12:11:43 +020019#include <linux/if_vlan.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include <linux/in.h>
Linus Lüssingb61ec312018-12-30 16:52:53 +010021#include <linux/ip.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020022#include <linux/jiffies.h>
23#include <linux/kernel.h>
Sven Eckelmann68a67222016-01-16 10:29:47 +010024#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/list.h>
Linus Lüssing41aeefc2018-03-13 11:41:12 +010026#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/rculist.h>
28#include <linux/rcupdate.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/skbuff.h>
30#include <linux/slab.h>
31#include <linux/spinlock.h>
32#include <linux/stddef.h>
33#include <linux/string.h>
Linus Lüssingb61ec312018-12-30 16:52:53 +010034#include <linux/udp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/workqueue.h>
Antonio Quartullic384ea32011-06-26 03:37:18 +020036#include <net/arp.h>
Linus Lüssing41aeefc2018-03-13 11:41:12 +010037#include <net/genetlink.h>
38#include <net/netlink.h>
39#include <net/sock.h>
40#include <uapi/linux/batman_adv.h>
Antonio Quartulli785ea112011-11-23 11:35:44 +010041
Andreas Pape00311de2016-09-05 13:20:25 +020042#include "bridge_loop_avoidance.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010043#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020045#include "log.h"
Linus Lüssing41aeefc2018-03-13 11:41:12 +010046#include "netlink.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010047#include "originator.h"
48#include "send.h"
Linus Lüssing41aeefc2018-03-13 11:41:12 +010049#include "soft-interface.h"
Antonio Quartullic384ea32011-06-26 03:37:18 +020050#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020051#include "tvlv.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010052
Linus Lüssingb61ec312018-12-30 16:52:53 +010053enum batadv_bootpop {
54 BATADV_BOOTREPLY = 2,
55};
56
57enum batadv_boothtype {
58 BATADV_HTYPE_ETHERNET = 1,
59};
60
61enum batadv_dhcpoptioncode {
62 BATADV_DHCP_OPT_PAD = 0,
63 BATADV_DHCP_OPT_MSG_TYPE = 53,
64 BATADV_DHCP_OPT_END = 255,
65};
66
67enum batadv_dhcptype {
68 BATADV_DHCPACK = 5,
69};
70
71/* { 99, 130, 83, 99 } */
72#define BATADV_DHCP_MAGIC 1669485411
73
74struct batadv_dhcp_packet {
75 __u8 op;
76 __u8 htype;
77 __u8 hlen;
78 __u8 hops;
79 __be32 xid;
80 __be16 secs;
81 __be16 flags;
82 __be32 ciaddr;
83 __be32 yiaddr;
84 __be32 siaddr;
85 __be32 giaddr;
86 __u8 chaddr[16];
87 __u8 sname[64];
88 __u8 file[128];
89 __be32 magic;
Sven Eckelmann576fb672020-12-29 09:24:21 +010090 /* __u8 options[]; */
Linus Lüssingb61ec312018-12-30 16:52:53 +010091};
92
93#define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
94#define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
95
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020096static void batadv_dat_purge(struct work_struct *work);
97
98/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010099 * batadv_dat_start_timer() - initialise the DAT periodic worker
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200100 * @bat_priv: the bat priv with all the soft interface information
101 */
102static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
103{
104 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
105 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
106 msecs_to_jiffies(10000));
107}
108
109/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100110 * batadv_dat_entry_release() - release dat_entry from lists and queue for free
Sven Eckelmann68a67222016-01-16 10:29:47 +0100111 * after rcu grace period
112 * @ref: kref pointer of the dat_entry
113 */
114static void batadv_dat_entry_release(struct kref *ref)
115{
116 struct batadv_dat_entry *dat_entry;
117
118 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
119
120 kfree_rcu(dat_entry, rcu);
121}
122
123/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100124 * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
Sven Eckelmann68a67222016-01-16 10:29:47 +0100125 * release it
126 * @dat_entry: dat_entry to be free'd
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200127 */
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100128static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200129{
Sven Eckelmann6340dcb2021-08-08 19:56:17 +0200130 if (!dat_entry)
131 return;
132
Sven Eckelmann68a67222016-01-16 10:29:47 +0100133 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200134}
135
136/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100137 * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200138 * @dat_entry: the entry to check
139 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200140 * Return: true if the entry has to be purged now, false otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200141 */
142static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
143{
144 return batadv_has_timed_out(dat_entry->last_update,
145 BATADV_DAT_ENTRY_TIMEOUT);
146}
147
148/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100149 * __batadv_dat_purge() - delete entries from the DAT local storage
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200150 * @bat_priv: the bat priv with all the soft interface information
151 * @to_purge: function in charge to decide whether an entry has to be purged or
152 * not. This function takes the dat_entry as argument and has to
153 * returns a boolean value: true is the entry has to be deleted,
154 * false otherwise
155 *
Marek Lindner93178012013-03-10 19:29:15 +0800156 * Loops over each entry in the DAT local storage and deletes it if and only if
157 * the to_purge function passed as argument returns true.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200158 */
159static void __batadv_dat_purge(struct batadv_priv *bat_priv,
160 bool (*to_purge)(struct batadv_dat_entry *))
161{
162 spinlock_t *list_lock; /* protects write access to the hash lists */
163 struct batadv_dat_entry *dat_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800164 struct hlist_node *node_tmp;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200165 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200166 u32 i;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200167
168 if (!bat_priv->dat.hash)
169 return;
170
171 for (i = 0; i < bat_priv->dat.hash->size; i++) {
172 head = &bat_priv->dat.hash->table[i];
173 list_lock = &bat_priv->dat.hash->list_locks[i];
174
175 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800176 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200177 hash_entry) {
Marek Lindner93178012013-03-10 19:29:15 +0800178 /* if a helper function has been passed as parameter,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200179 * ask it if the entry has to be purged or not
180 */
181 if (to_purge && !to_purge(dat_entry))
182 continue;
183
Sasha Levinb67bfe02013-02-27 17:06:00 -0800184 hlist_del_rcu(&dat_entry->hash_entry);
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100185 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200186 }
187 spin_unlock_bh(list_lock);
188 }
189}
190
191/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100192 * batadv_dat_purge() - periodic task that deletes old entries from the local
193 * DAT hash table
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200194 * @work: kernel work struct
195 */
196static void batadv_dat_purge(struct work_struct *work)
197{
198 struct delayed_work *delayed_work;
199 struct batadv_priv_dat *priv_dat;
200 struct batadv_priv *bat_priv;
201
Geliang Tang4ba4bc02015-12-28 23:43:37 +0800202 delayed_work = to_delayed_work(work);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200203 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
204 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
205
206 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
207 batadv_dat_start_timer(bat_priv);
208}
209
210/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100211 * batadv_compare_dat() - comparing function used in the local DAT hash table
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200212 * @node: node in the local table
213 * @data2: second object to compare the node to
214 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100215 * Return: true if the two entries are the same, false otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200216 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100217static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200218{
219 const void *data1 = container_of(node, struct batadv_dat_entry,
220 hash_entry);
221
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100222 return memcmp(data1, data2, sizeof(__be32)) == 0;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200223}
224
Antonio Quartulli785ea112011-11-23 11:35:44 +0100225/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100226 * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200227 * @skb: ARP packet
228 * @hdr_size: size of the possible header before the ARP packet
229 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200230 * Return: the value of the hw_src field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200231 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200232static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200233{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200234 u8 *addr;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200235
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200236 addr = (u8 *)(skb->data + hdr_size);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200237 addr += ETH_HLEN + sizeof(struct arphdr);
238
239 return addr;
240}
241
242/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100243 * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200244 * @skb: ARP packet
245 * @hdr_size: size of the possible header before the ARP packet
246 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200247 * Return: the value of the ip_src field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200248 */
249static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
250{
Sven Eckelmann61a29282019-11-28 11:26:06 +0100251 return *(__force __be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200252}
253
254/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100255 * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200256 * @skb: ARP packet
257 * @hdr_size: size of the possible header before the ARP packet
258 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200259 * Return: the value of the hw_dst field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200260 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200261static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200262{
263 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
264}
265
266/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100267 * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200268 * @skb: ARP packet
269 * @hdr_size: size of the possible header before the ARP packet
270 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200271 * Return: the value of the ip_dst field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200272 */
273static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
274{
Sven Eckelmann61a29282019-11-28 11:26:06 +0100275 u8 *dst = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4;
276
277 return *(__force __be32 *)dst;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200278}
279
280/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100281 * batadv_hash_dat() - compute the hash value for an IP address
Antonio Quartulli785ea112011-11-23 11:35:44 +0100282 * @data: data to hash
283 * @size: size of the hash table
284 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200285 * Return: the selected index in the hash table for the given data.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100286 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200287static u32 batadv_hash_dat(const void *data, u32 size)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100288{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200289 u32 hash = 0;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200290 const struct batadv_dat_entry *dat = data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100291 const unsigned char *key;
Sven Eckelmann4cc4a172019-11-28 12:25:45 +0100292 __be16 vid;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200293 u32 i;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100294
Sven Eckelmann61a29282019-11-28 11:26:06 +0100295 key = (__force const unsigned char *)&dat->ip;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100296 for (i = 0; i < sizeof(dat->ip); i++) {
297 hash += key[i];
298 hash += (hash << 10);
299 hash ^= (hash >> 6);
300 }
301
Sven Eckelmann4cc4a172019-11-28 12:25:45 +0100302 vid = htons(dat->vid);
303 key = (__force const unsigned char *)&vid;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100304 for (i = 0; i < sizeof(dat->vid); i++) {
305 hash += key[i];
306 hash += (hash << 10);
307 hash ^= (hash >> 6);
308 }
Antonio Quartulli785ea112011-11-23 11:35:44 +0100309
310 hash += (hash << 3);
311 hash ^= (hash >> 11);
312 hash += (hash << 15);
313
314 return hash % size;
315}
316
317/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100318 * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200319 * table
320 * @bat_priv: the bat priv with all the soft interface information
321 * @ip: search key
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200322 * @vid: VLAN identifier
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200323 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200324 * Return: the dat_entry if found, NULL otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200325 */
326static struct batadv_dat_entry *
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200327batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
328 unsigned short vid)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200329{
330 struct hlist_head *head;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200331 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200332 struct batadv_hashtable *hash = bat_priv->dat.hash;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200333 u32 index;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200334
335 if (!hash)
336 return NULL;
337
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200338 to_find.ip = ip;
339 to_find.vid = vid;
340
341 index = batadv_hash_dat(&to_find, hash->size);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200342 head = &hash->table[index];
343
344 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800345 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200346 if (dat_entry->ip != ip)
347 continue;
348
Sven Eckelmann68a67222016-01-16 10:29:47 +0100349 if (!kref_get_unless_zero(&dat_entry->refcount))
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200350 continue;
351
352 dat_entry_tmp = dat_entry;
353 break;
354 }
355 rcu_read_unlock();
356
357 return dat_entry_tmp;
358}
359
360/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100361 * batadv_dat_entry_add() - add a new dat entry or update it if already exists
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200362 * @bat_priv: the bat priv with all the soft interface information
363 * @ip: ipv4 to add/edit
364 * @mac_addr: mac address to assign to the given ipv4
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200365 * @vid: VLAN identifier
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200366 */
367static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200368 u8 *mac_addr, unsigned short vid)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200369{
370 struct batadv_dat_entry *dat_entry;
371 int hash_added;
372
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200373 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200374 /* if this entry is already known, just update it */
375 if (dat_entry) {
376 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100377 ether_addr_copy(dat_entry->mac_addr, mac_addr);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200378 dat_entry->last_update = jiffies;
379 batadv_dbg(BATADV_DBG_DAT, bat_priv,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200380 "Entry updated: %pI4 %pM (vid: %d)\n",
381 &dat_entry->ip, dat_entry->mac_addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100382 batadv_print_vid(vid));
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200383 goto out;
384 }
385
386 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
387 if (!dat_entry)
388 goto out;
389
390 dat_entry->ip = ip;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200391 dat_entry->vid = vid;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100392 ether_addr_copy(dat_entry->mac_addr, mac_addr);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200393 dat_entry->last_update = jiffies;
Sven Eckelmann68a67222016-01-16 10:29:47 +0100394 kref_init(&dat_entry->refcount);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200395
Sven Eckelmann6a51e092016-07-15 17:39:26 +0200396 kref_get(&dat_entry->refcount);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200397 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200398 batadv_hash_dat, dat_entry,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200399 &dat_entry->hash_entry);
400
401 if (unlikely(hash_added != 0)) {
402 /* remove the reference for the hash */
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100403 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200404 goto out;
405 }
406
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200407 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100408 &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200409
410out:
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200411 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200412}
413
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200414#ifdef CONFIG_BATMAN_ADV_DEBUG
415
416/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100417 * batadv_dbg_arp() - print a debug message containing all the ARP packet
418 * details
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200419 * @bat_priv: the bat priv with all the soft interface information
420 * @skb: ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200421 * @hdr_size: size of the possible header before the ARP packet
422 * @msg: message to print together with the debugging information
423 */
424static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
Antonio Quartulli843314c2016-09-25 08:46:36 +0800425 int hdr_size, char *msg)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200426{
427 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
428 struct batadv_bcast_packet *bcast_pkt;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200429 u8 *orig_addr;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200430 __be32 ip_src, ip_dst;
431
432 if (msg)
433 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
434
435 ip_src = batadv_arp_ip_src(skb, hdr_size);
436 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
437 batadv_dbg(BATADV_DBG_DAT, bat_priv,
438 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
439 batadv_arp_hw_src(skb, hdr_size), &ip_src,
440 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
441
Matthias Schiffer6f27d2c2018-03-16 11:29:10 +0100442 if (hdr_size < sizeof(struct batadv_unicast_packet))
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200443 return;
444
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200445 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
446
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100447 switch (unicast_4addr_packet->u.packet_type) {
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200448 case BATADV_UNICAST:
449 batadv_dbg(BATADV_DBG_DAT, bat_priv,
450 "* encapsulated within a UNICAST packet\n");
451 break;
452 case BATADV_UNICAST_4ADDR:
453 batadv_dbg(BATADV_DBG_DAT, bat_priv,
454 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
455 unicast_4addr_packet->src);
456 switch (unicast_4addr_packet->subtype) {
457 case BATADV_P_DAT_DHT_PUT:
458 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
459 break;
460 case BATADV_P_DAT_DHT_GET:
461 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
462 break;
463 case BATADV_P_DAT_CACHE_REPLY:
464 batadv_dbg(BATADV_DBG_DAT, bat_priv,
465 "* type: DAT_CACHE_REPLY\n");
466 break;
467 case BATADV_P_DATA:
468 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
469 break;
470 default:
471 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100472 unicast_4addr_packet->u.packet_type);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200473 }
474 break;
475 case BATADV_BCAST:
476 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
477 orig_addr = bcast_pkt->orig;
478 batadv_dbg(BATADV_DBG_DAT, bat_priv,
479 "* encapsulated within a BCAST packet (src: %pM)\n",
480 orig_addr);
481 break;
482 default:
483 batadv_dbg(BATADV_DBG_DAT, bat_priv,
484 "* encapsulated within an unknown packet type (0x%x)\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100485 unicast_4addr_packet->u.packet_type);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200486 }
487}
488
489#else
490
491static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
Antonio Quartulli843314c2016-09-25 08:46:36 +0800492 int hdr_size, char *msg)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200493{
494}
495
496#endif /* CONFIG_BATMAN_ADV_DEBUG */
497
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200498/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100499 * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
Antonio Quartulli785ea112011-11-23 11:35:44 +0100500 * @res: the array with the already selected candidates
501 * @select: number of already selected candidates
502 * @tmp_max: address of the currently evaluated node
503 * @max: current round max address
504 * @last_max: address of the last selected candidate
505 * @candidate: orig_node under evaluation
506 * @max_orig_node: last selected candidate
507 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200508 * Return: true if the node has been elected as next candidate or false
Marek Lindner93178012013-03-10 19:29:15 +0800509 * otherwise.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100510 */
511static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
512 int select, batadv_dat_addr_t tmp_max,
513 batadv_dat_addr_t max,
514 batadv_dat_addr_t last_max,
515 struct batadv_orig_node *candidate,
516 struct batadv_orig_node *max_orig_node)
517{
518 bool ret = false;
519 int j;
520
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800521 /* check if orig node candidate is running DAT */
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200522 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800523 goto out;
524
Antonio Quartulli785ea112011-11-23 11:35:44 +0100525 /* Check if this node has already been selected... */
526 for (j = 0; j < select; j++)
527 if (res[j].orig_node == candidate)
528 break;
529 /* ..and possibly skip it */
530 if (j < select)
531 goto out;
532 /* sanity check: has it already been selected? This should not happen */
533 if (tmp_max > last_max)
534 goto out;
535 /* check if during this iteration an originator with a closer dht
536 * address has already been found
537 */
538 if (tmp_max < max)
539 goto out;
540 /* this is an hash collision with the temporary selected node. Choose
541 * the one with the lowest address
542 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200543 if (tmp_max == max && max_orig_node &&
Sven Eckelmannd7625f92018-02-20 12:08:10 +0100544 batadv_compare_eth(candidate->orig, max_orig_node->orig))
Antonio Quartulli785ea112011-11-23 11:35:44 +0100545 goto out;
546
547 ret = true;
548out:
549 return ret;
550}
551
552/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100553 * batadv_choose_next_candidate() - select the next DHT candidate
Antonio Quartulli785ea112011-11-23 11:35:44 +0100554 * @bat_priv: the bat priv with all the soft interface information
555 * @cands: candidates array
556 * @select: number of candidates already present in the array
557 * @ip_key: key to look up in the DHT
558 * @last_max: pointer where the address of the selected candidate will be saved
559 */
560static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
561 struct batadv_dat_candidate *cands,
562 int select, batadv_dat_addr_t ip_key,
563 batadv_dat_addr_t *last_max)
564{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200565 batadv_dat_addr_t max = 0;
566 batadv_dat_addr_t tmp_max = 0;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100567 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
568 struct batadv_hashtable *hash = bat_priv->orig_hash;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100569 struct hlist_head *head;
570 int i;
571
572 /* if no node is eligible as candidate, leave the candidate type as
573 * NOT_FOUND
574 */
575 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
576
Marek Lindner93178012013-03-10 19:29:15 +0800577 /* iterate over the originator list and find the node with the closest
Antonio Quartulli785ea112011-11-23 11:35:44 +0100578 * dat_address which has not been selected yet
579 */
580 for (i = 0; i < hash->size; i++) {
581 head = &hash->table[i];
582
583 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800584 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Marek Lindner93178012013-03-10 19:29:15 +0800585 /* the dht space is a ring using unsigned addresses */
Antonio Quartulli785ea112011-11-23 11:35:44 +0100586 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
587 ip_key;
588
589 if (!batadv_is_orig_node_eligible(cands, select,
590 tmp_max, max,
591 *last_max, orig_node,
592 max_orig_node))
593 continue;
594
Sven Eckelmann7c124392016-01-16 10:29:56 +0100595 if (!kref_get_unless_zero(&orig_node->refcount))
Antonio Quartulli785ea112011-11-23 11:35:44 +0100596 continue;
597
598 max = tmp_max;
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200599 batadv_orig_node_put(max_orig_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100600 max_orig_node = orig_node;
601 }
602 rcu_read_unlock();
603 }
604 if (max_orig_node) {
605 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
606 cands[select].orig_node = max_orig_node;
607 batadv_dbg(BATADV_DBG_DAT, bat_priv,
608 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
609 select, max_orig_node->orig, max_orig_node->dat_addr,
610 max);
611 }
612 *last_max = max;
613}
614
615/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100616 * batadv_dat_select_candidates() - select the nodes which the DHT message has
617 * to be sent to
Antonio Quartulli785ea112011-11-23 11:35:44 +0100618 * @bat_priv: the bat priv with all the soft interface information
619 * @ip_dst: ipv4 to look up in the DHT
Antonio Quartulli28717342016-03-12 11:12:59 +0100620 * @vid: VLAN identifier
Antonio Quartulli785ea112011-11-23 11:35:44 +0100621 *
622 * An originator O is selected if and only if its DHT_ID value is one of three
623 * closest values (from the LEFT, with wrap around if needed) then the hash
624 * value of the key. ip_dst is the key.
625 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200626 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100627 */
628static struct batadv_dat_candidate *
Antonio Quartulli28717342016-03-12 11:12:59 +0100629batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
630 unsigned short vid)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100631{
632 int select;
633 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
634 struct batadv_dat_candidate *res;
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100635 struct batadv_dat_entry dat;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100636
637 if (!bat_priv->orig_hash)
638 return NULL;
639
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200640 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
641 GFP_ATOMIC);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100642 if (!res)
643 return NULL;
644
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100645 dat.ip = ip_dst;
Antonio Quartulli28717342016-03-12 11:12:59 +0100646 dat.vid = vid;
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100647 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
Antonio Quartulli785ea112011-11-23 11:35:44 +0100648 BATADV_DAT_ADDR_MAX);
649
650 batadv_dbg(BATADV_DBG_DAT, bat_priv,
Sven Eckelmann22f05022017-05-19 13:02:00 +0200651 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
Antonio Quartulli785ea112011-11-23 11:35:44 +0100652 ip_key);
653
654 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
655 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
656 &last_max);
657
658 return res;
659}
660
661/**
Sven Eckelmannc2d8b9a2019-03-17 10:50:50 +0100662 * batadv_dat_forward_data() - copy and send payload to the selected candidates
Antonio Quartulli785ea112011-11-23 11:35:44 +0100663 * @bat_priv: the bat priv with all the soft interface information
664 * @skb: payload to send
665 * @ip: the DHT key
Antonio Quartulli28717342016-03-12 11:12:59 +0100666 * @vid: VLAN identifier
Antonio Quartulli785ea112011-11-23 11:35:44 +0100667 * @packet_subtype: unicast4addr packet subtype to use
668 *
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200669 * This function copies the skb with pskb_copy() and is sent as a unicast packet
Marek Lindner93178012013-03-10 19:29:15 +0800670 * to each of the selected candidates.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100671 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200672 * Return: true if the packet is sent to at least one candidate, false
Marek Lindner93178012013-03-10 19:29:15 +0800673 * otherwise.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100674 */
Sven Eckelmannc2d8b9a2019-03-17 10:50:50 +0100675static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
676 struct sk_buff *skb, __be32 ip,
677 unsigned short vid, int packet_subtype)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100678{
679 int i;
680 bool ret = false;
681 int send_status;
682 struct batadv_neigh_node *neigh_node = NULL;
683 struct sk_buff *tmp_skb;
684 struct batadv_dat_candidate *cand;
685
Antonio Quartulli28717342016-03-12 11:12:59 +0100686 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100687 if (!cand)
688 goto out;
689
690 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
691
692 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
693 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
694 continue;
695
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100696 neigh_node = batadv_orig_router_get(cand[i].orig_node,
697 BATADV_IF_DEFAULT);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100698 if (!neigh_node)
699 goto free_orig;
700
Octavian Purdilabad93e92014-06-12 01:36:26 +0300701 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
Martin Hundebøllf097e252013-05-23 16:53:01 +0200702 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
703 cand[i].orig_node,
704 packet_subtype)) {
Antonio Quartulli785ea112011-11-23 11:35:44 +0100705 kfree_skb(tmp_skb);
706 goto free_neigh;
707 }
708
Antonio Quartulli95d39272016-01-16 16:40:15 +0800709 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
Martin Hundebøll4046b242012-04-20 17:02:45 +0200710 if (send_status == NET_XMIT_SUCCESS) {
711 /* count the sent packet */
712 switch (packet_subtype) {
713 case BATADV_P_DAT_DHT_GET:
714 batadv_inc_counter(bat_priv,
715 BATADV_CNT_DAT_GET_TX);
716 break;
717 case BATADV_P_DAT_DHT_PUT:
718 batadv_inc_counter(bat_priv,
719 BATADV_CNT_DAT_PUT_TX);
720 break;
721 }
722
Antonio Quartulli785ea112011-11-23 11:35:44 +0100723 /* packet sent to a candidate: return true */
724 ret = true;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200725 }
Antonio Quartulli785ea112011-11-23 11:35:44 +0100726free_neigh:
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100727 batadv_neigh_node_put(neigh_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100728free_orig:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100729 batadv_orig_node_put(cand[i].orig_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100730 }
731
732out:
733 kfree(cand);
734 return ret;
735}
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200736
737/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100738 * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800739 * setting change
740 * @bat_priv: the bat priv with all the soft interface information
741 */
742static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
743{
744 char dat_mode;
745
746 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
747
748 switch (dat_mode) {
749 case 0:
750 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
751 break;
752 case 1:
753 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
754 NULL, 0);
755 break;
756 }
757}
758
759/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100760 * batadv_dat_status_update() - update the dat tvlv container after dat
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800761 * setting change
762 * @net_dev: the soft interface net device
763 */
764void batadv_dat_status_update(struct net_device *net_dev)
765{
766 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200767
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800768 batadv_dat_tvlv_container_update(bat_priv);
769}
770
771/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100772 * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800773 * @bat_priv: the bat priv with all the soft interface information
774 * @orig: the orig_node of the ogm
775 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
776 * @tvlv_value: tvlv buffer containing the gateway data
777 * @tvlv_value_len: tvlv buffer length
778 */
779static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
780 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200781 u8 flags,
782 void *tvlv_value, u16 tvlv_value_len)
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800783{
784 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200785 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800786 else
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200787 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800788}
789
790/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100791 * batadv_dat_hash_free() - free the local DAT hash table
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200792 * @bat_priv: the bat priv with all the soft interface information
793 */
794static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
795{
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200796 if (!bat_priv->dat.hash)
797 return;
798
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200799 __batadv_dat_purge(bat_priv, NULL);
800
801 batadv_hash_destroy(bat_priv->dat.hash);
802
803 bat_priv->dat.hash = NULL;
804}
805
806/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100807 * batadv_dat_init() - initialise the DAT internals
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200808 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullife13c2a2015-11-17 16:40:51 +0800809 *
810 * Return: 0 in case of success, a negative error code otherwise
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200811 */
812int batadv_dat_init(struct batadv_priv *bat_priv)
813{
814 if (bat_priv->dat.hash)
815 return 0;
816
817 bat_priv->dat.hash = batadv_hash_new(1024);
818
819 if (!bat_priv->dat.hash)
820 return -ENOMEM;
821
822 batadv_dat_start_timer(bat_priv);
823
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800824 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
825 NULL, BATADV_TVLV_DAT, 1,
826 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
827 batadv_dat_tvlv_container_update(bat_priv);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200828 return 0;
829}
830
831/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100832 * batadv_dat_free() - free the DAT internals
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200833 * @bat_priv: the bat priv with all the soft interface information
834 */
835void batadv_dat_free(struct batadv_priv *bat_priv)
836{
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800837 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
838 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
839
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200840 cancel_delayed_work_sync(&bat_priv->dat.work);
841
842 batadv_dat_hash_free(bat_priv);
843}
844
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200845/**
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100846 * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
847 * netlink socket
848 * @msg: buffer for the message
849 * @portid: netlink port
Sven Eckelmann6f816522018-10-30 22:01:28 +0100850 * @cb: Control block containing additional options
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100851 * @dat_entry: entry to dump
852 *
853 * Return: 0 or error code.
854 */
855static int
Sven Eckelmann6f816522018-10-30 22:01:28 +0100856batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
857 struct netlink_callback *cb,
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100858 struct batadv_dat_entry *dat_entry)
859{
860 int msecs;
861 void *hdr;
862
Sven Eckelmann6f816522018-10-30 22:01:28 +0100863 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
864 &batadv_netlink_family, NLM_F_MULTI,
865 BATADV_CMD_GET_DAT_CACHE);
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100866 if (!hdr)
867 return -ENOBUFS;
868
Sven Eckelmann6f816522018-10-30 22:01:28 +0100869 genl_dump_check_consistent(cb, hdr);
870
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100871 msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
872
873 if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
874 dat_entry->ip) ||
875 nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
876 dat_entry->mac_addr) ||
877 nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
878 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
879 genlmsg_cancel(msg, hdr);
880 return -EMSGSIZE;
881 }
882
883 genlmsg_end(msg, hdr);
884 return 0;
885}
886
887/**
888 * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
889 * a netlink socket
890 * @msg: buffer for the message
891 * @portid: netlink port
Sven Eckelmann6f816522018-10-30 22:01:28 +0100892 * @cb: Control block containing additional options
893 * @hash: hash to dump
894 * @bucket: bucket index to dump
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100895 * @idx_skip: How many entries to skip
896 *
897 * Return: 0 or error code.
898 */
899static int
Sven Eckelmann6f816522018-10-30 22:01:28 +0100900batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
901 struct netlink_callback *cb,
902 struct batadv_hashtable *hash, unsigned int bucket,
903 int *idx_skip)
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100904{
905 struct batadv_dat_entry *dat_entry;
906 int idx = 0;
907
Sven Eckelmann6f816522018-10-30 22:01:28 +0100908 spin_lock_bh(&hash->list_locks[bucket]);
909 cb->seq = atomic_read(&hash->generation) << 1 | 1;
910
911 hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100912 if (idx < *idx_skip)
913 goto skip;
914
Sven Eckelmann6f816522018-10-30 22:01:28 +0100915 if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
916 spin_unlock_bh(&hash->list_locks[bucket]);
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100917 *idx_skip = idx;
918
919 return -EMSGSIZE;
920 }
921
922skip:
923 idx++;
924 }
Sven Eckelmann6f816522018-10-30 22:01:28 +0100925 spin_unlock_bh(&hash->list_locks[bucket]);
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100926
927 return 0;
928}
929
930/**
931 * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
932 * @msg: buffer for the message
933 * @cb: callback structure containing arguments
934 *
935 * Return: message length.
936 */
937int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
938{
939 struct batadv_hard_iface *primary_if = NULL;
940 int portid = NETLINK_CB(cb->skb).portid;
941 struct net *net = sock_net(cb->skb->sk);
942 struct net_device *soft_iface;
943 struct batadv_hashtable *hash;
944 struct batadv_priv *bat_priv;
945 int bucket = cb->args[0];
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100946 int idx = cb->args[1];
947 int ifindex;
948 int ret = 0;
949
950 ifindex = batadv_netlink_get_ifindex(cb->nlh,
951 BATADV_ATTR_MESH_IFINDEX);
952 if (!ifindex)
953 return -EINVAL;
954
955 soft_iface = dev_get_by_index(net, ifindex);
956 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
957 ret = -ENODEV;
958 goto out;
959 }
960
961 bat_priv = netdev_priv(soft_iface);
962 hash = bat_priv->dat.hash;
963
964 primary_if = batadv_primary_if_get_selected(bat_priv);
965 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
966 ret = -ENOENT;
967 goto out;
968 }
969
970 while (bucket < hash->size) {
Sven Eckelmann6f816522018-10-30 22:01:28 +0100971 if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100972 &idx))
973 break;
974
975 bucket++;
976 idx = 0;
977 }
978
979 cb->args[0] = bucket;
980 cb->args[1] = idx;
981
982 ret = msg->len;
983
984out:
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200985 batadv_hardif_put(primary_if);
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100986
Yajun Deng1160dfa2021-08-05 19:55:27 +0800987 dev_put(soft_iface);
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100988
989 return ret;
990}
991
992/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100993 * batadv_arp_get_type() - parse an ARP packet and gets the type
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200994 * @bat_priv: the bat priv with all the soft interface information
995 * @skb: packet to analyse
996 * @hdr_size: size of the possible header before the ARP packet in the skb
997 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200998 * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200999 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001000static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1001 struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001002{
1003 struct arphdr *arphdr;
1004 struct ethhdr *ethhdr;
1005 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001006 u8 *hw_src, *hw_dst;
1007 u16 type = 0;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001008
1009 /* pull the ethernet header */
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 /* pull the ARP payload */
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
Marek Lindner93178012013-03-10 19:29:15 +08001025 /* check whether the ARP packet carries a valid IP information */
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001026 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 /* Check for bad reply/request. If the ARP message is not sane, DAT
1039 * will simply ignore it
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) ||
Matthias Schiffer757dd822013-01-24 18:18:26 +01001044 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))
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001047 goto out;
1048
Matthias Schifferb618ad12013-01-24 18:18:27 +01001049 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
Marek Lindner93178012013-03-10 19:29:15 +08001053 /* don't care about the destination MAC address in ARP requests */
Matthias Schifferb618ad12013-01-24 18:18:27 +01001054 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
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001061 type = ntohs(arphdr->ar_op);
1062out:
1063 return type;
1064}
Antonio Quartullic384ea32011-06-26 03:37:18 +02001065
1066/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001067 * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001068 * @skb: the buffer containing the packet to extract the VID from
1069 * @hdr_size: the size of the batman-adv header encapsulating the packet
1070 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001071 * Return: If the packet embedded in the skb is vlan tagged this function
1072 * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1073 * is returned.
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001074 */
1075static 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 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1082 * If the header contained in the packet is a VLAN one (which is longer)
1083 * hdr_size is updated so that the functions will still skip the
1084 * correct amount of bytes.
1085 */
1086 if (vid & BATADV_VLAN_HAS_TAG)
1087 *hdr_size += VLAN_HLEN;
1088
1089 return vid;
1090}
1091
1092/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001093 * batadv_dat_arp_create_reply() - create an ARP Reply
Linus Lüssingcbfc16d2016-07-05 20:01:36 +02001094 * @bat_priv: the bat priv with all the soft interface information
1095 * @ip_src: ARP sender IP
1096 * @ip_dst: ARP target IP
1097 * @hw_src: Ethernet source and ARP sender MAC
1098 * @hw_dst: Ethernet destination and ARP target MAC
1099 * @vid: VLAN identifier (optional, set to zero otherwise)
1100 *
1101 * Creates an ARP Reply from the given values, optionally encapsulated in a
1102 * VLAN header.
1103 *
1104 * Return: An skb containing an ARP Reply.
1105 */
1106static struct sk_buff *
1107batadv_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/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001128 * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
Antonio Quartullic384ea32011-06-26 03:37:18 +02001129 * answer using DAT
1130 * @bat_priv: the bat priv with all the soft interface information
1131 * @skb: packet to check
1132 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001133 * Return: true if the message has been sent to the dht candidates, false
Marek Lindner93178012013-03-10 19:29:15 +08001134 * otherwise. In case of a positive return value the message has to be enqueued
1135 * to permit the fallback.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001136 */
1137bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1138 struct sk_buff *skb)
1139{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001140 u16 type = 0;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001141 __be32 ip_dst, ip_src;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001142 u8 *hw_src;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001143 bool ret = false;
1144 struct batadv_dat_entry *dat_entry = NULL;
1145 struct sk_buff *skb_new;
Tobias Klauserab044f82017-04-05 13:46:31 +02001146 struct net_device *soft_iface = bat_priv->soft_iface;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001147 int hdr_size = 0;
1148 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001149
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001150 if (!atomic_read(&bat_priv->distributed_arp_table))
1151 goto out;
1152
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001153 vid = batadv_dat_get_vid(skb, &hdr_size);
1154
1155 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001156 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1157 * message to the selected DHT candidates
1158 */
1159 if (type != ARPOP_REQUEST)
1160 goto out;
1161
Antonio Quartulli843314c2016-09-25 08:46:36 +08001162 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001163
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001164 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);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001167
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001168 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001169
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001170 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001171 if (dat_entry) {
Antonio Quartulli88e48d72013-05-09 09:35:45 +02001172 /* If the ARP request is destined for a local client the local
1173 * client will answer itself. DAT would only generate a
1174 * duplicate packet.
1175 *
1176 * Moreover, if the soft-interface is enslaved into a bridge, an
1177 * additional DAT answer may trigger kernel warnings about
1178 * a packet coming from the wrong port.
1179 */
Antonio Quartullicc2f3382014-03-29 17:27:38 +01001180 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
Antonio Quartulli88e48d72013-05-09 09:35:45 +02001181 ret = true;
1182 goto out;
1183 }
1184
Andreas Pape00311de2016-09-05 13:20:25 +02001185 /* If BLA is enabled, only send ARP replies if we have claimed
1186 * the destination for the ARP request or if no one else of
1187 * the backbone gws belonging to our backbone has claimed the
1188 * destination.
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
Linus Lüssingcbfc16d2016-07-05 20:01:36 +02001199 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1200 dat_entry->mac_addr,
1201 hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001202 if (!skb_new)
1203 goto out;
1204
Tobias Klauserab044f82017-04-05 13:46:31 +02001205 skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1206
Sven Eckelmann36d4d682017-04-05 16:26:17 +02001207 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);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001210
1211 netif_rx(skb_new);
1212 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1213 ret = true;
1214 } else {
Marek Lindner93178012013-03-10 19:29:15 +08001215 /* Send the request to the DHT */
Sven Eckelmannc2d8b9a2019-03-17 10:50:50 +01001216 ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1217 BATADV_P_DAT_DHT_GET);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001218 }
1219out:
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001220 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001221 return ret;
1222}
1223
1224/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001225 * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
Antonio Quartullic384ea32011-06-26 03:37:18 +02001226 * answer using the local DAT storage
1227 * @bat_priv: the bat priv with all the soft interface information
1228 * @skb: packet to check
1229 * @hdr_size: size of the encapsulation header
1230 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001231 * Return: true if the request has been answered, false otherwise.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001232 */
1233bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1234 struct sk_buff *skb, int hdr_size)
1235{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001236 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001237 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001238 u8 *hw_src;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001239 struct sk_buff *skb_new;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001240 struct batadv_dat_entry *dat_entry = NULL;
1241 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001242 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001243 int err;
1244
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001245 if (!atomic_read(&bat_priv->distributed_arp_table))
1246 goto out;
1247
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001248 vid = batadv_dat_get_vid(skb, &hdr_size);
1249
Antonio Quartullic384ea32011-06-26 03:37:18 +02001250 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
Antonio Quartulli843314c2016-09-25 08:46:36 +08001258 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001259
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001260 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001261
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001262 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001263 if (!dat_entry)
1264 goto out;
1265
Linus Lüssingcbfc16d2016-07-05 20:01:36 +02001266 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1267 dat_entry->mac_addr, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001268 if (!skb_new)
1269 goto out;
1270
Marek Lindner93178012013-03-10 19:29:15 +08001271 /* To preserve backwards compatibility, the node has choose the outgoing
1272 * format based on the incoming request packet type. The assumption is
1273 * that a node not using the 4addr packet format doesn't support it.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001274 */
1275 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
Linus Lüssinge300d312013-07-03 10:40:00 +02001276 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1277 BATADV_P_DAT_CACHE_REPLY,
Antonio Quartulli6c413b12013-11-05 19:31:08 +01001278 NULL, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001279 else
Antonio Quartulli6c413b12013-11-05 19:31:08 +01001280 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001281
Linus Lüssinge300d312013-07-03 10:40:00 +02001282 if (err != NET_XMIT_DROP) {
Martin Hundebøll4046b242012-04-20 17:02:45 +02001283 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001284 ret = true;
Martin Hundebøll4046b242012-04-20 17:02:45 +02001285 }
Antonio Quartullic384ea32011-06-26 03:37:18 +02001286out:
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001287 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001288 if (ret)
1289 kfree_skb(skb);
1290 return ret;
1291}
1292
1293/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001294 * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
Antonio Quartullic384ea32011-06-26 03:37:18 +02001295 * @bat_priv: the bat priv with all the soft interface information
1296 * @skb: packet to check
1297 */
1298void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1299 struct sk_buff *skb)
1300{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001301 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001302 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001303 u8 *hw_src, *hw_dst;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001304 int hdr_size = 0;
1305 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001306
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001307 if (!atomic_read(&bat_priv->distributed_arp_table))
1308 return;
1309
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001310 vid = batadv_dat_get_vid(skb, &hdr_size);
1311
1312 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001313 if (type != ARPOP_REPLY)
1314 return;
1315
Antonio Quartulli843314c2016-09-25 08:46:36 +08001316 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001317
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001318 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);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001322
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001323 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1324 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001325
1326 /* Send the ARP reply to the candidates for both the IP addresses that
Marek Lindner93178012013-03-10 19:29:15 +08001327 * the node obtained from the ARP reply
Antonio Quartullic384ea32011-06-26 03:37:18 +02001328 */
Sven Eckelmannc2d8b9a2019-03-17 10:50:50 +01001329 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);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001333}
Antonio Quartulliaa143d22014-09-01 14:37:27 +02001334
Antonio Quartullic384ea32011-06-26 03:37:18 +02001335/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001336 * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1337 * local DAT storage only
Antonio Quartullic384ea32011-06-26 03:37:18 +02001338 * @bat_priv: the bat priv with all the soft interface information
1339 * @skb: packet to check
Marek Lindner93178012013-03-10 19:29:15 +08001340 * @hdr_size: size of the encapsulation header
Antonio Quartullif202a662015-06-16 21:06:24 +02001341 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001342 * Return: true if the packet was snooped and consumed by DAT. False if the
Antonio Quartullif202a662015-06-16 21:06:24 +02001343 * packet has to be delivered to the interface
Antonio Quartullic384ea32011-06-26 03:37:18 +02001344 */
1345bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1346 struct sk_buff *skb, int hdr_size)
1347{
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001348 struct batadv_dat_entry *dat_entry = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001349 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001350 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001351 u8 *hw_src, *hw_dst;
Antonio Quartullif202a662015-06-16 21:06:24 +02001352 bool dropped = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001353 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001354
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001355 if (!atomic_read(&bat_priv->distributed_arp_table))
1356 goto out;
1357
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001358 vid = batadv_dat_get_vid(skb, &hdr_size);
1359
Antonio Quartullic384ea32011-06-26 03:37:18 +02001360 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1361 if (type != ARPOP_REPLY)
1362 goto out;
1363
Antonio Quartulli843314c2016-09-25 08:46:36 +08001364 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001365
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
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001371 /* If ip_dst is already in cache and has the right mac address,
1372 * drop this frame if this ARP reply is destined for us because it's
1373 * most probably an ARP reply generated by another node of the DHT.
1374 * We have most probably received already a reply earlier. Delivering
1375 * this frame would lead to doubled receive of an ARP reply.
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;
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001383 }
1384
Antonio Quartullic384ea32011-06-26 03:37:18 +02001385 /* Update our internal cache with both the IP addresses the node got
1386 * within the ARP reply
1387 */
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001388 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1389 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001390
Linus Lüssing099e6cc2019-02-14 16:52:43 +01001391 if (dropped)
1392 goto out;
1393
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001394 /* If BLA is enabled, only forward ARP replies if we have claimed the
1395 * source of the ARP reply or if no one else of the same backbone has
1396 * already claimed that client. This prevents that different gateways
1397 * to the same backbone all forward the ARP reply leading to multiple
1398 * replies in the backbone.
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
Antonio Quartullic384ea32011-06-26 03:37:18 +02001408 /* if this REPLY is directed to a client of mine, let's deliver the
1409 * packet to the interface
1410 */
Antonio Quartullif202a662015-06-16 21:06:24 +02001411 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1412
1413 /* if this REPLY is sent on behalf of a client of mine, let's drop the
1414 * packet because the client will reply by itself
1415 */
1416 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001417out:
Antonio Quartullif202a662015-06-16 21:06:24 +02001418 if (dropped)
Matthias Schiffer0d15bec2013-01-23 18:11:53 +01001419 kfree_skb(skb);
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001420 batadv_dat_entry_put(dat_entry);
Antonio Quartullif202a662015-06-16 21:06:24 +02001421 /* if dropped == false -> deliver to the interface */
1422 return dropped;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001423}
1424
1425/**
Linus Lüssingb61ec312018-12-30 16:52:53 +01001426 * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
1427 * @skb: the packet to check
1428 * @ip_src: a buffer to store the IPv4 source address in
1429 *
1430 * Checks whether the given skb has an IP and UDP header valid for a DHCP
1431 * message from a DHCP server. And if so, stores the IPv4 source address in
1432 * the provided buffer.
1433 *
1434 * Return: True if valid, false otherwise.
1435 */
1436static bool
1437batadv_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 * batadv_dat_check_dhcp() - examine packet for valid DHCP message
1464 * @skb: the packet to check
1465 * @proto: ethernet protocol hint (behind a potential vlan)
1466 * @ip_src: a buffer to store the IPv4 source address in
1467 *
1468 * Checks whether the given skb is a valid DHCP packet. And if so, stores the
1469 * IPv4 source address in the provided buffer.
1470 *
1471 * Caller needs to ensure that the skb network header is set correctly.
1472 *
1473 * Return: If skb is a valid DHCP packet, then returns its op code
1474 * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
1475 */
1476static int
1477batadv_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 * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
1514 * @skb: the DHCP packet to parse
1515 *
1516 * Iterates over the DHCP options of the given DHCP packet to find a
1517 * DHCP Message Type option and parse it.
1518 *
1519 * Caller needs to ensure that the given skb is a valid DHCP packet and
1520 * that the skb transport header is set correctly.
1521 *
1522 * Return: The found DHCP message type value, if found. -EINVAL otherwise.
1523 */
1524static 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 /* Option Overload Code not supported */
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/**
Sven Eckelmann25d81f92021-01-20 20:50:35 +01001563 * batadv_dat_dhcp_get_yiaddr() - get yiaddr from a DHCP packet
Linus Lüssingb61ec312018-12-30 16:52:53 +01001564 * @skb: the DHCP packet to parse
1565 * @buf: a buffer to store the yiaddr in
1566 *
1567 * Caller needs to ensure that the given skb is a valid DHCP packet and
1568 * that the skb transport header is set correctly.
1569 *
1570 * Return: True on success, false otherwise.
1571 */
1572static 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 * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
1591 * @skb: the DHCP packet to parse
1592 * @buf: a buffer to store the chaddr in
1593 *
1594 * Caller needs to ensure that the given skb is a valid DHCP packet and
1595 * that the skb transport header is set correctly.
1596 *
1597 * Return: True on success, false otherwise
1598 */
1599static 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 * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
1618 * DAT cache
1619 * @bat_priv: the bat priv with all the soft interface information
1620 * @chaddr: the DHCP client MAC address
1621 * @yiaddr: the DHCP client IP address
1622 * @hw_dst: the DHCP server MAC address
1623 * @ip_dst: the DHCP server IP address
1624 * @vid: VLAN identifier
1625 *
1626 * Adds given MAC/IP pairs to the local DAT cache and propagates them further
1627 * into the DHT.
1628 *
1629 * For the DHT propagation, client MAC + IP will appear as the ARP Reply
1630 * transmitter (and hw_dst/ip_dst as the target).
1631 */
1632static 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
Sven Eckelmannc2d8b9a2019-03-17 10:50:50 +01001648 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);
Linus Lüssingb61ec312018-12-30 16:52:53 +01001652
Martin Weinelt5f320f02019-02-06 15:39:14 +01001653 consume_skb(skb);
1654
Linus Lüssingb61ec312018-12-30 16:52:53 +01001655 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 * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
1665 * @skb: the packet to check
1666 * @proto: ethernet protocol hint (behind a potential vlan)
1667 * @ip_src: a buffer to store the IPv4 source address in
1668 * @chaddr: a buffer to store the DHCP Client Hardware Address in
1669 * @yiaddr: a buffer to store the DHCP Your IP Address in
1670 *
1671 * Checks whether the given skb is a valid DHCPACK. And if so, stores the
1672 * IPv4 server source address (ip_src), client MAC address (chaddr) and client
1673 * IPv4 address (yiaddr) in the provided buffers.
1674 *
1675 * Caller needs to ensure that the skb network header is set correctly.
1676 *
1677 * Return: True if the skb is a valid DHCPACK. False otherwise.
1678 */
1679static bool
1680batadv_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 * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
1704 * @bat_priv: the bat priv with all the soft interface information
1705 * @skb: the packet to snoop
1706 * @proto: ethernet protocol hint (behind a potential vlan)
1707 * @vid: VLAN identifier
1708 *
1709 * This function first checks whether the given skb is a valid DHCPACK. If
1710 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1711 * field and DHCP Your IP Address field are added to the local DAT cache and
1712 * propagated into the DHT.
1713 *
1714 * Caller needs to ensure that the skb mac and network headers are set
1715 * correctly.
1716 */
1717void 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 * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
1737 * @bat_priv: the bat priv with all the soft interface information
1738 * @skb: the packet to snoop
1739 * @hdr_size: header size, up to the tail of the batman-adv header
1740 *
1741 * This function first checks whether the given skb is a valid DHCPACK. If
1742 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1743 * field and DHCP Your IP Address field are added to the local DAT cache.
1744 */
1745void 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/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001783 * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1784 * dropped (because the node has already obtained the reply via DAT) or not
Antonio Quartullic384ea32011-06-26 03:37:18 +02001785 * @bat_priv: the bat priv with all the soft interface information
1786 * @forw_packet: the broadcast packet
1787 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001788 * Return: true if the node can drop the packet, false otherwise.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001789 */
1790bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1791 struct batadv_forw_packet *forw_packet)
1792{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001793 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001794 __be32 ip_dst;
1795 struct batadv_dat_entry *dat_entry = NULL;
1796 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001797 int hdr_size = sizeof(struct batadv_bcast_packet);
1798 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001799
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001800 if (!atomic_read(&bat_priv->distributed_arp_table))
1801 goto out;
1802
Antonio Quartullic384ea32011-06-26 03:37:18 +02001803 /* If this packet is an ARP_REQUEST and the node already has the
1804 * information that it is going to ask, then the packet can be dropped
1805 */
Linus Lüssinge2d9ba42017-02-17 11:17:07 +01001806 if (batadv_forw_packet_is_rebroadcast(forw_packet))
Antonio Quartullic384ea32011-06-26 03:37:18 +02001807 goto out;
1808
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001809 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1810
1811 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001812 if (type != ARPOP_REQUEST)
1813 goto out;
1814
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001815 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1816 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001817 /* check if the node already got this entry */
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
1828out:
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001829 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001830 return ret;
1831}