blob: 3c2faf77333574291e47043fa085db6b7b8ddabb [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01002/* Copyright (C) 2011-2017 B.A.T.M.A.N. contributors:
Antonio Quartulli785ea112011-11-23 11:35:44 +01003 *
4 * Antonio Quartulli
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Antonio Quartulli785ea112011-11-23 11:35:44 +010017 */
18
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "distributed-arp-table.h"
20#include "main.h"
21
22#include <linux/atomic.h>
Linus Lüssing65d7d4602015-06-16 17:10:22 +020023#include <linux/bitops.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020024#include <linux/byteorder/generic.h>
25#include <linux/errno.h>
26#include <linux/etherdevice.h>
27#include <linux/fs.h>
Antonio Quartulli785ea112011-11-23 11:35:44 +010028#include <linux/if_arp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/if_ether.h>
Antonio Quartullibe1db4f2013-06-04 12:11:43 +020030#include <linux/if_vlan.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/in.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
Sven Eckelmann68a67222016-01-16 10:29:47 +010034#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/list.h>
36#include <linux/rculist.h>
37#include <linux/rcupdate.h>
38#include <linux/seq_file.h>
39#include <linux/skbuff.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/stddef.h>
43#include <linux/string.h>
44#include <linux/workqueue.h>
Antonio Quartullic384ea32011-06-26 03:37:18 +020045#include <net/arp.h>
Antonio Quartulli785ea112011-11-23 11:35:44 +010046
Andreas Pape00311de2016-09-05 13:20:25 +020047#include "bridge_loop_avoidance.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010048#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020049#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020050#include "log.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010051#include "originator.h"
52#include "send.h"
Antonio Quartullic384ea32011-06-26 03:37:18 +020053#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020054#include "tvlv.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010055
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020056static void batadv_dat_purge(struct work_struct *work);
57
58/**
59 * batadv_dat_start_timer - initialise the DAT periodic worker
60 * @bat_priv: the bat priv with all the soft interface information
61 */
62static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
63{
64 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
65 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
66 msecs_to_jiffies(10000));
67}
68
69/**
Sven Eckelmann68a67222016-01-16 10:29:47 +010070 * batadv_dat_entry_release - release dat_entry from lists and queue for free
71 * after rcu grace period
72 * @ref: kref pointer of the dat_entry
73 */
74static void batadv_dat_entry_release(struct kref *ref)
75{
76 struct batadv_dat_entry *dat_entry;
77
78 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
79
80 kfree_rcu(dat_entry, rcu);
81}
82
83/**
Sven Eckelmanna6416f92016-01-17 11:01:17 +010084 * batadv_dat_entry_put - decrement the dat_entry refcounter and possibly
Sven Eckelmann68a67222016-01-16 10:29:47 +010085 * release it
86 * @dat_entry: dat_entry to be free'd
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020087 */
Sven Eckelmanna6416f92016-01-17 11:01:17 +010088static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020089{
Sven Eckelmann68a67222016-01-16 10:29:47 +010090 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020091}
92
93/**
Marek Lindner93178012013-03-10 19:29:15 +080094 * batadv_dat_to_purge - check whether a dat_entry has to be purged or not
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020095 * @dat_entry: the entry to check
96 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020097 * Return: true if the entry has to be purged now, false otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020098 */
99static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
100{
101 return batadv_has_timed_out(dat_entry->last_update,
102 BATADV_DAT_ENTRY_TIMEOUT);
103}
104
105/**
106 * __batadv_dat_purge - delete entries from the DAT local storage
107 * @bat_priv: the bat priv with all the soft interface information
108 * @to_purge: function in charge to decide whether an entry has to be purged or
109 * not. This function takes the dat_entry as argument and has to
110 * returns a boolean value: true is the entry has to be deleted,
111 * false otherwise
112 *
Marek Lindner93178012013-03-10 19:29:15 +0800113 * Loops over each entry in the DAT local storage and deletes it if and only if
114 * the to_purge function passed as argument returns true.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200115 */
116static void __batadv_dat_purge(struct batadv_priv *bat_priv,
117 bool (*to_purge)(struct batadv_dat_entry *))
118{
119 spinlock_t *list_lock; /* protects write access to the hash lists */
120 struct batadv_dat_entry *dat_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800121 struct hlist_node *node_tmp;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200122 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200123 u32 i;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200124
125 if (!bat_priv->dat.hash)
126 return;
127
128 for (i = 0; i < bat_priv->dat.hash->size; i++) {
129 head = &bat_priv->dat.hash->table[i];
130 list_lock = &bat_priv->dat.hash->list_locks[i];
131
132 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800133 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200134 hash_entry) {
Marek Lindner93178012013-03-10 19:29:15 +0800135 /* if a helper function has been passed as parameter,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200136 * ask it if the entry has to be purged or not
137 */
138 if (to_purge && !to_purge(dat_entry))
139 continue;
140
Sasha Levinb67bfe02013-02-27 17:06:00 -0800141 hlist_del_rcu(&dat_entry->hash_entry);
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100142 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200143 }
144 spin_unlock_bh(list_lock);
145 }
146}
147
148/**
149 * batadv_dat_purge - periodic task that deletes old entries from the local DAT
150 * hash table
151 * @work: kernel work struct
152 */
153static void batadv_dat_purge(struct work_struct *work)
154{
155 struct delayed_work *delayed_work;
156 struct batadv_priv_dat *priv_dat;
157 struct batadv_priv *bat_priv;
158
Geliang Tang4ba4bc02015-12-28 23:43:37 +0800159 delayed_work = to_delayed_work(work);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200160 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
161 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
162
163 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
164 batadv_dat_start_timer(bat_priv);
165}
166
167/**
168 * batadv_compare_dat - comparing function used in the local DAT hash table
169 * @node: node in the local table
170 * @data2: second object to compare the node to
171 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100172 * Return: true if the two entries are the same, false otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200173 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100174static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200175{
176 const void *data1 = container_of(node, struct batadv_dat_entry,
177 hash_entry);
178
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100179 return memcmp(data1, data2, sizeof(__be32)) == 0;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200180}
181
Antonio Quartulli785ea112011-11-23 11:35:44 +0100182/**
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200183 * batadv_arp_hw_src - extract the hw_src field from an ARP packet
184 * @skb: ARP packet
185 * @hdr_size: size of the possible header before the ARP packet
186 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200187 * Return: the value of the hw_src field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200188 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200189static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200190{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200191 u8 *addr;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200192
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200193 addr = (u8 *)(skb->data + hdr_size);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200194 addr += ETH_HLEN + sizeof(struct arphdr);
195
196 return addr;
197}
198
199/**
200 * batadv_arp_ip_src - extract the ip_src field from an ARP packet
201 * @skb: ARP packet
202 * @hdr_size: size of the possible header before the ARP packet
203 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200204 * Return: the value of the ip_src field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200205 */
206static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
207{
208 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
209}
210
211/**
212 * batadv_arp_hw_dst - extract the hw_dst field from an ARP packet
213 * @skb: ARP packet
214 * @hdr_size: size of the possible header before the ARP packet
215 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200216 * Return: the value of the hw_dst field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200217 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200218static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200219{
220 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
221}
222
223/**
224 * batadv_arp_ip_dst - extract the ip_dst field from an ARP packet
225 * @skb: ARP packet
226 * @hdr_size: size of the possible header before the ARP packet
227 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200228 * Return: the value of the ip_dst field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200229 */
230static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
231{
232 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
233}
234
235/**
Antonio Quartulli785ea112011-11-23 11:35:44 +0100236 * batadv_hash_dat - compute the hash value for an IP address
237 * @data: data to hash
238 * @size: size of the hash table
239 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200240 * Return: the selected index in the hash table for the given data.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100241 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200242static u32 batadv_hash_dat(const void *data, u32 size)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100243{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200244 u32 hash = 0;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200245 const struct batadv_dat_entry *dat = data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100246 const unsigned char *key;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200247 u32 i;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100248
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100249 key = (const unsigned char *)&dat->ip;
250 for (i = 0; i < sizeof(dat->ip); i++) {
251 hash += key[i];
252 hash += (hash << 10);
253 hash ^= (hash >> 6);
254 }
255
256 key = (const unsigned char *)&dat->vid;
257 for (i = 0; i < sizeof(dat->vid); i++) {
258 hash += key[i];
259 hash += (hash << 10);
260 hash ^= (hash >> 6);
261 }
Antonio Quartulli785ea112011-11-23 11:35:44 +0100262
263 hash += (hash << 3);
264 hash ^= (hash >> 11);
265 hash += (hash << 15);
266
267 return hash % size;
268}
269
270/**
Marek Lindner93178012013-03-10 19:29:15 +0800271 * batadv_dat_entry_hash_find - look for a given dat_entry in the local hash
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200272 * table
273 * @bat_priv: the bat priv with all the soft interface information
274 * @ip: search key
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200275 * @vid: VLAN identifier
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200276 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200277 * Return: the dat_entry if found, NULL otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200278 */
279static struct batadv_dat_entry *
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200280batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
281 unsigned short vid)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200282{
283 struct hlist_head *head;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200284 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200285 struct batadv_hashtable *hash = bat_priv->dat.hash;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200286 u32 index;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200287
288 if (!hash)
289 return NULL;
290
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200291 to_find.ip = ip;
292 to_find.vid = vid;
293
294 index = batadv_hash_dat(&to_find, hash->size);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200295 head = &hash->table[index];
296
297 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800298 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200299 if (dat_entry->ip != ip)
300 continue;
301
Sven Eckelmann68a67222016-01-16 10:29:47 +0100302 if (!kref_get_unless_zero(&dat_entry->refcount))
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200303 continue;
304
305 dat_entry_tmp = dat_entry;
306 break;
307 }
308 rcu_read_unlock();
309
310 return dat_entry_tmp;
311}
312
313/**
314 * batadv_dat_entry_add - add a new dat entry or update it if already exists
315 * @bat_priv: the bat priv with all the soft interface information
316 * @ip: ipv4 to add/edit
317 * @mac_addr: mac address to assign to the given ipv4
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200318 * @vid: VLAN identifier
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200319 */
320static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200321 u8 *mac_addr, unsigned short vid)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200322{
323 struct batadv_dat_entry *dat_entry;
324 int hash_added;
325
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200326 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200327 /* if this entry is already known, just update it */
328 if (dat_entry) {
329 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100330 ether_addr_copy(dat_entry->mac_addr, mac_addr);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200331 dat_entry->last_update = jiffies;
332 batadv_dbg(BATADV_DBG_DAT, bat_priv,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200333 "Entry updated: %pI4 %pM (vid: %d)\n",
334 &dat_entry->ip, dat_entry->mac_addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100335 batadv_print_vid(vid));
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200336 goto out;
337 }
338
339 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
340 if (!dat_entry)
341 goto out;
342
343 dat_entry->ip = ip;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200344 dat_entry->vid = vid;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100345 ether_addr_copy(dat_entry->mac_addr, mac_addr);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200346 dat_entry->last_update = jiffies;
Sven Eckelmann68a67222016-01-16 10:29:47 +0100347 kref_init(&dat_entry->refcount);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200348
Sven Eckelmann6a51e092016-07-15 17:39:26 +0200349 kref_get(&dat_entry->refcount);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200350 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200351 batadv_hash_dat, dat_entry,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200352 &dat_entry->hash_entry);
353
354 if (unlikely(hash_added != 0)) {
355 /* remove the reference for the hash */
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100356 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200357 goto out;
358 }
359
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200360 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100361 &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200362
363out:
364 if (dat_entry)
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100365 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200366}
367
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200368#ifdef CONFIG_BATMAN_ADV_DEBUG
369
370/**
371 * batadv_dbg_arp - print a debug message containing all the ARP packet details
372 * @bat_priv: the bat priv with all the soft interface information
373 * @skb: ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200374 * @hdr_size: size of the possible header before the ARP packet
375 * @msg: message to print together with the debugging information
376 */
377static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
Antonio Quartulli843314c2016-09-25 08:46:36 +0800378 int hdr_size, char *msg)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200379{
380 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
381 struct batadv_bcast_packet *bcast_pkt;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200382 u8 *orig_addr;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200383 __be32 ip_src, ip_dst;
384
385 if (msg)
386 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
387
388 ip_src = batadv_arp_ip_src(skb, hdr_size);
389 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
390 batadv_dbg(BATADV_DBG_DAT, bat_priv,
391 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
392 batadv_arp_hw_src(skb, hdr_size), &ip_src,
393 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
394
395 if (hdr_size == 0)
396 return;
397
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200398 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
399
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100400 switch (unicast_4addr_packet->u.packet_type) {
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200401 case BATADV_UNICAST:
402 batadv_dbg(BATADV_DBG_DAT, bat_priv,
403 "* encapsulated within a UNICAST packet\n");
404 break;
405 case BATADV_UNICAST_4ADDR:
406 batadv_dbg(BATADV_DBG_DAT, bat_priv,
407 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
408 unicast_4addr_packet->src);
409 switch (unicast_4addr_packet->subtype) {
410 case BATADV_P_DAT_DHT_PUT:
411 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
412 break;
413 case BATADV_P_DAT_DHT_GET:
414 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
415 break;
416 case BATADV_P_DAT_CACHE_REPLY:
417 batadv_dbg(BATADV_DBG_DAT, bat_priv,
418 "* type: DAT_CACHE_REPLY\n");
419 break;
420 case BATADV_P_DATA:
421 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
422 break;
423 default:
424 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100425 unicast_4addr_packet->u.packet_type);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200426 }
427 break;
428 case BATADV_BCAST:
429 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
430 orig_addr = bcast_pkt->orig;
431 batadv_dbg(BATADV_DBG_DAT, bat_priv,
432 "* encapsulated within a BCAST packet (src: %pM)\n",
433 orig_addr);
434 break;
435 default:
436 batadv_dbg(BATADV_DBG_DAT, bat_priv,
437 "* encapsulated within an unknown packet type (0x%x)\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100438 unicast_4addr_packet->u.packet_type);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200439 }
440}
441
442#else
443
444static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
Antonio Quartulli843314c2016-09-25 08:46:36 +0800445 int hdr_size, char *msg)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200446{
447}
448
449#endif /* CONFIG_BATMAN_ADV_DEBUG */
450
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200451/**
Antonio Quartulli785ea112011-11-23 11:35:44 +0100452 * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate
453 * @res: the array with the already selected candidates
454 * @select: number of already selected candidates
455 * @tmp_max: address of the currently evaluated node
456 * @max: current round max address
457 * @last_max: address of the last selected candidate
458 * @candidate: orig_node under evaluation
459 * @max_orig_node: last selected candidate
460 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200461 * Return: true if the node has been elected as next candidate or false
Marek Lindner93178012013-03-10 19:29:15 +0800462 * otherwise.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100463 */
464static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
465 int select, batadv_dat_addr_t tmp_max,
466 batadv_dat_addr_t max,
467 batadv_dat_addr_t last_max,
468 struct batadv_orig_node *candidate,
469 struct batadv_orig_node *max_orig_node)
470{
471 bool ret = false;
472 int j;
473
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800474 /* check if orig node candidate is running DAT */
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200475 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800476 goto out;
477
Antonio Quartulli785ea112011-11-23 11:35:44 +0100478 /* Check if this node has already been selected... */
479 for (j = 0; j < select; j++)
480 if (res[j].orig_node == candidate)
481 break;
482 /* ..and possibly skip it */
483 if (j < select)
484 goto out;
485 /* sanity check: has it already been selected? This should not happen */
486 if (tmp_max > last_max)
487 goto out;
488 /* check if during this iteration an originator with a closer dht
489 * address has already been found
490 */
491 if (tmp_max < max)
492 goto out;
493 /* this is an hash collision with the temporary selected node. Choose
494 * the one with the lowest address
495 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200496 if (tmp_max == max && max_orig_node &&
497 batadv_compare_eth(candidate->orig, max_orig_node->orig) > 0)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100498 goto out;
499
500 ret = true;
501out:
502 return ret;
503}
504
505/**
506 * batadv_choose_next_candidate - select the next DHT candidate
507 * @bat_priv: the bat priv with all the soft interface information
508 * @cands: candidates array
509 * @select: number of candidates already present in the array
510 * @ip_key: key to look up in the DHT
511 * @last_max: pointer where the address of the selected candidate will be saved
512 */
513static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
514 struct batadv_dat_candidate *cands,
515 int select, batadv_dat_addr_t ip_key,
516 batadv_dat_addr_t *last_max)
517{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200518 batadv_dat_addr_t max = 0;
519 batadv_dat_addr_t tmp_max = 0;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100520 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
521 struct batadv_hashtable *hash = bat_priv->orig_hash;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100522 struct hlist_head *head;
523 int i;
524
525 /* if no node is eligible as candidate, leave the candidate type as
526 * NOT_FOUND
527 */
528 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
529
Marek Lindner93178012013-03-10 19:29:15 +0800530 /* iterate over the originator list and find the node with the closest
Antonio Quartulli785ea112011-11-23 11:35:44 +0100531 * dat_address which has not been selected yet
532 */
533 for (i = 0; i < hash->size; i++) {
534 head = &hash->table[i];
535
536 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800537 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Marek Lindner93178012013-03-10 19:29:15 +0800538 /* the dht space is a ring using unsigned addresses */
Antonio Quartulli785ea112011-11-23 11:35:44 +0100539 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
540 ip_key;
541
542 if (!batadv_is_orig_node_eligible(cands, select,
543 tmp_max, max,
544 *last_max, orig_node,
545 max_orig_node))
546 continue;
547
Sven Eckelmann7c124392016-01-16 10:29:56 +0100548 if (!kref_get_unless_zero(&orig_node->refcount))
Antonio Quartulli785ea112011-11-23 11:35:44 +0100549 continue;
550
551 max = tmp_max;
552 if (max_orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100553 batadv_orig_node_put(max_orig_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100554 max_orig_node = orig_node;
555 }
556 rcu_read_unlock();
557 }
558 if (max_orig_node) {
559 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
560 cands[select].orig_node = max_orig_node;
561 batadv_dbg(BATADV_DBG_DAT, bat_priv,
562 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
563 select, max_orig_node->orig, max_orig_node->dat_addr,
564 max);
565 }
566 *last_max = max;
567}
568
569/**
Marek Lindner93178012013-03-10 19:29:15 +0800570 * batadv_dat_select_candidates - select the nodes which the DHT message has to
Antonio Quartulli785ea112011-11-23 11:35:44 +0100571 * be sent to
572 * @bat_priv: the bat priv with all the soft interface information
573 * @ip_dst: ipv4 to look up in the DHT
Antonio Quartulli28717342016-03-12 11:12:59 +0100574 * @vid: VLAN identifier
Antonio Quartulli785ea112011-11-23 11:35:44 +0100575 *
576 * An originator O is selected if and only if its DHT_ID value is one of three
577 * closest values (from the LEFT, with wrap around if needed) then the hash
578 * value of the key. ip_dst is the key.
579 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200580 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100581 */
582static struct batadv_dat_candidate *
Antonio Quartulli28717342016-03-12 11:12:59 +0100583batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
584 unsigned short vid)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100585{
586 int select;
587 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
588 struct batadv_dat_candidate *res;
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100589 struct batadv_dat_entry dat;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100590
591 if (!bat_priv->orig_hash)
592 return NULL;
593
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200594 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
595 GFP_ATOMIC);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100596 if (!res)
597 return NULL;
598
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100599 dat.ip = ip_dst;
Antonio Quartulli28717342016-03-12 11:12:59 +0100600 dat.vid = vid;
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100601 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
Antonio Quartulli785ea112011-11-23 11:35:44 +0100602 BATADV_DAT_ADDR_MAX);
603
604 batadv_dbg(BATADV_DBG_DAT, bat_priv,
Sven Eckelmann22f05022017-05-19 13:02:00 +0200605 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
Antonio Quartulli785ea112011-11-23 11:35:44 +0100606 ip_key);
607
608 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
609 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
610 &last_max);
611
612 return res;
613}
614
615/**
616 * batadv_dat_send_data - send a payload to the selected candidates
617 * @bat_priv: the bat priv with all the soft interface information
618 * @skb: payload to send
619 * @ip: the DHT key
Antonio Quartulli28717342016-03-12 11:12:59 +0100620 * @vid: VLAN identifier
Antonio Quartulli785ea112011-11-23 11:35:44 +0100621 * @packet_subtype: unicast4addr packet subtype to use
622 *
Marek Lindner93178012013-03-10 19:29:15 +0800623 * This function copies the skb with pskb_copy() and is sent as unicast packet
624 * to each of the selected candidates.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100625 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200626 * Return: true if the packet is sent to at least one candidate, false
Marek Lindner93178012013-03-10 19:29:15 +0800627 * otherwise.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100628 */
629static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
630 struct sk_buff *skb, __be32 ip,
Antonio Quartulli28717342016-03-12 11:12:59 +0100631 unsigned short vid, int packet_subtype)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100632{
633 int i;
634 bool ret = false;
635 int send_status;
636 struct batadv_neigh_node *neigh_node = NULL;
637 struct sk_buff *tmp_skb;
638 struct batadv_dat_candidate *cand;
639
Antonio Quartulli28717342016-03-12 11:12:59 +0100640 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100641 if (!cand)
642 goto out;
643
644 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
645
646 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
647 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
648 continue;
649
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100650 neigh_node = batadv_orig_router_get(cand[i].orig_node,
651 BATADV_IF_DEFAULT);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100652 if (!neigh_node)
653 goto free_orig;
654
Octavian Purdilabad93e92014-06-12 01:36:26 +0300655 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
Martin Hundebøllf097e252013-05-23 16:53:01 +0200656 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
657 cand[i].orig_node,
658 packet_subtype)) {
Antonio Quartulli785ea112011-11-23 11:35:44 +0100659 kfree_skb(tmp_skb);
660 goto free_neigh;
661 }
662
Antonio Quartulli95d39272016-01-16 16:40:15 +0800663 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
Martin Hundebøll4046b242012-04-20 17:02:45 +0200664 if (send_status == NET_XMIT_SUCCESS) {
665 /* count the sent packet */
666 switch (packet_subtype) {
667 case BATADV_P_DAT_DHT_GET:
668 batadv_inc_counter(bat_priv,
669 BATADV_CNT_DAT_GET_TX);
670 break;
671 case BATADV_P_DAT_DHT_PUT:
672 batadv_inc_counter(bat_priv,
673 BATADV_CNT_DAT_PUT_TX);
674 break;
675 }
676
Antonio Quartulli785ea112011-11-23 11:35:44 +0100677 /* packet sent to a candidate: return true */
678 ret = true;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200679 }
Antonio Quartulli785ea112011-11-23 11:35:44 +0100680free_neigh:
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100681 batadv_neigh_node_put(neigh_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100682free_orig:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100683 batadv_orig_node_put(cand[i].orig_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100684 }
685
686out:
687 kfree(cand);
688 return ret;
689}
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200690
691/**
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800692 * batadv_dat_tvlv_container_update - update the dat tvlv container after dat
693 * setting change
694 * @bat_priv: the bat priv with all the soft interface information
695 */
696static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
697{
698 char dat_mode;
699
700 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
701
702 switch (dat_mode) {
703 case 0:
704 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
705 break;
706 case 1:
707 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
708 NULL, 0);
709 break;
710 }
711}
712
713/**
714 * batadv_dat_status_update - update the dat tvlv container after dat
715 * setting change
716 * @net_dev: the soft interface net device
717 */
718void batadv_dat_status_update(struct net_device *net_dev)
719{
720 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200721
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800722 batadv_dat_tvlv_container_update(bat_priv);
723}
724
725/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100726 * batadv_dat_tvlv_ogm_handler_v1 - process incoming dat tvlv container
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800727 * @bat_priv: the bat priv with all the soft interface information
728 * @orig: the orig_node of the ogm
729 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
730 * @tvlv_value: tvlv buffer containing the gateway data
731 * @tvlv_value_len: tvlv buffer length
732 */
733static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
734 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200735 u8 flags,
736 void *tvlv_value, u16 tvlv_value_len)
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800737{
738 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200739 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800740 else
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200741 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800742}
743
744/**
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200745 * batadv_dat_hash_free - free the local DAT hash table
746 * @bat_priv: the bat priv with all the soft interface information
747 */
748static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
749{
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200750 if (!bat_priv->dat.hash)
751 return;
752
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200753 __batadv_dat_purge(bat_priv, NULL);
754
755 batadv_hash_destroy(bat_priv->dat.hash);
756
757 bat_priv->dat.hash = NULL;
758}
759
760/**
761 * batadv_dat_init - initialise the DAT internals
762 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullife13c2a2015-11-17 16:40:51 +0800763 *
764 * Return: 0 in case of success, a negative error code otherwise
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200765 */
766int batadv_dat_init(struct batadv_priv *bat_priv)
767{
768 if (bat_priv->dat.hash)
769 return 0;
770
771 bat_priv->dat.hash = batadv_hash_new(1024);
772
773 if (!bat_priv->dat.hash)
774 return -ENOMEM;
775
776 batadv_dat_start_timer(bat_priv);
777
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800778 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
779 NULL, BATADV_TVLV_DAT, 1,
780 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
781 batadv_dat_tvlv_container_update(bat_priv);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200782 return 0;
783}
784
785/**
786 * batadv_dat_free - free the DAT internals
787 * @bat_priv: the bat priv with all the soft interface information
788 */
789void batadv_dat_free(struct batadv_priv *bat_priv)
790{
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800791 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
792 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
793
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200794 cancel_delayed_work_sync(&bat_priv->dat.work);
795
796 batadv_dat_hash_free(bat_priv);
797}
798
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200799#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200800/**
801 * batadv_dat_cache_seq_print_text - print the local DAT hash table
802 * @seq: seq file to print on
803 * @offset: not used
Antonio Quartullife13c2a2015-11-17 16:40:51 +0800804 *
805 * Return: always 0
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200806 */
807int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
808{
809 struct net_device *net_dev = (struct net_device *)seq->private;
810 struct batadv_priv *bat_priv = netdev_priv(net_dev);
811 struct batadv_hashtable *hash = bat_priv->dat.hash;
812 struct batadv_dat_entry *dat_entry;
813 struct batadv_hard_iface *primary_if;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200814 struct hlist_head *head;
815 unsigned long last_seen_jiffies;
816 int last_seen_msecs, last_seen_secs, last_seen_mins;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200817 u32 i;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200818
819 primary_if = batadv_seq_print_text_primary_if_get(seq);
820 if (!primary_if)
821 goto out;
822
823 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
Antonio Quartulli925a6f32016-03-12 10:30:18 +0100824 seq_puts(seq,
825 " IPv4 MAC VID last-seen\n");
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200826
827 for (i = 0; i < hash->size; i++) {
828 head = &hash->table[i];
829
830 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800831 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200832 last_seen_jiffies = jiffies - dat_entry->last_update;
833 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
834 last_seen_mins = last_seen_msecs / 60000;
835 last_seen_msecs = last_seen_msecs % 60000;
836 last_seen_secs = last_seen_msecs / 1000;
837
Joe Perchescd0edf3a2017-06-14 02:33:52 -0700838 seq_printf(seq, " * %15pI4 %pM %4i %6i:%02i\n",
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200839 &dat_entry->ip, dat_entry->mac_addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100840 batadv_print_vid(dat_entry->vid),
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200841 last_seen_mins, last_seen_secs);
842 }
843 rcu_read_unlock();
844 }
845
846out:
847 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100848 batadv_hardif_put(primary_if);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200849 return 0;
850}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200851#endif
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200852
853/**
854 * batadv_arp_get_type - parse an ARP packet and gets the type
855 * @bat_priv: the bat priv with all the soft interface information
856 * @skb: packet to analyse
857 * @hdr_size: size of the possible header before the ARP packet in the skb
858 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200859 * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200860 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200861static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
862 struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200863{
864 struct arphdr *arphdr;
865 struct ethhdr *ethhdr;
866 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200867 u8 *hw_src, *hw_dst;
868 u16 type = 0;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200869
870 /* pull the ethernet header */
871 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
872 goto out;
873
874 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
875
876 if (ethhdr->h_proto != htons(ETH_P_ARP))
877 goto out;
878
879 /* pull the ARP payload */
880 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
881 arp_hdr_len(skb->dev))))
882 goto out;
883
884 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
885
Marek Lindner93178012013-03-10 19:29:15 +0800886 /* check whether the ARP packet carries a valid IP information */
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200887 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
888 goto out;
889
890 if (arphdr->ar_pro != htons(ETH_P_IP))
891 goto out;
892
893 if (arphdr->ar_hln != ETH_ALEN)
894 goto out;
895
896 if (arphdr->ar_pln != 4)
897 goto out;
898
899 /* Check for bad reply/request. If the ARP message is not sane, DAT
900 * will simply ignore it
901 */
902 ip_src = batadv_arp_ip_src(skb, hdr_size);
903 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
904 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
Matthias Schiffer757dd822013-01-24 18:18:26 +0100905 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
906 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
907 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200908 goto out;
909
Matthias Schifferb618ad12013-01-24 18:18:27 +0100910 hw_src = batadv_arp_hw_src(skb, hdr_size);
911 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
912 goto out;
913
Marek Lindner93178012013-03-10 19:29:15 +0800914 /* don't care about the destination MAC address in ARP requests */
Matthias Schifferb618ad12013-01-24 18:18:27 +0100915 if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
916 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
917 if (is_zero_ether_addr(hw_dst) ||
918 is_multicast_ether_addr(hw_dst))
919 goto out;
920 }
921
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200922 type = ntohs(arphdr->ar_op);
923out:
924 return type;
925}
Antonio Quartullic384ea32011-06-26 03:37:18 +0200926
927/**
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200928 * batadv_dat_get_vid - extract the VLAN identifier from skb if any
929 * @skb: the buffer containing the packet to extract the VID from
930 * @hdr_size: the size of the batman-adv header encapsulating the packet
931 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200932 * Return: If the packet embedded in the skb is vlan tagged this function
933 * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
934 * is returned.
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200935 */
936static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
937{
938 unsigned short vid;
939
940 vid = batadv_get_vid(skb, *hdr_size);
941
942 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
943 * If the header contained in the packet is a VLAN one (which is longer)
944 * hdr_size is updated so that the functions will still skip the
945 * correct amount of bytes.
946 */
947 if (vid & BATADV_VLAN_HAS_TAG)
948 *hdr_size += VLAN_HLEN;
949
950 return vid;
951}
952
953/**
Linus Lüssingcbfc16d2016-07-05 20:01:36 +0200954 * batadv_dat_arp_create_reply - create an ARP Reply
955 * @bat_priv: the bat priv with all the soft interface information
956 * @ip_src: ARP sender IP
957 * @ip_dst: ARP target IP
958 * @hw_src: Ethernet source and ARP sender MAC
959 * @hw_dst: Ethernet destination and ARP target MAC
960 * @vid: VLAN identifier (optional, set to zero otherwise)
961 *
962 * Creates an ARP Reply from the given values, optionally encapsulated in a
963 * VLAN header.
964 *
965 * Return: An skb containing an ARP Reply.
966 */
967static struct sk_buff *
968batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
969 __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
970 unsigned short vid)
971{
972 struct sk_buff *skb;
973
974 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
975 ip_src, hw_dst, hw_src, hw_dst);
976 if (!skb)
977 return NULL;
978
979 skb_reset_mac_header(skb);
980
981 if (vid & BATADV_VLAN_HAS_TAG)
982 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
983 vid & VLAN_VID_MASK);
984
985 return skb;
986}
987
988/**
Antonio Quartullic384ea32011-06-26 03:37:18 +0200989 * batadv_dat_snoop_outgoing_arp_request - snoop the ARP request and try to
990 * answer using DAT
991 * @bat_priv: the bat priv with all the soft interface information
992 * @skb: packet to check
993 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200994 * Return: true if the message has been sent to the dht candidates, false
Marek Lindner93178012013-03-10 19:29:15 +0800995 * otherwise. In case of a positive return value the message has to be enqueued
996 * to permit the fallback.
Antonio Quartullic384ea32011-06-26 03:37:18 +0200997 */
998bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
999 struct sk_buff *skb)
1000{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001001 u16 type = 0;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001002 __be32 ip_dst, ip_src;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001003 u8 *hw_src;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001004 bool ret = false;
1005 struct batadv_dat_entry *dat_entry = NULL;
1006 struct sk_buff *skb_new;
Tobias Klauserab044f82017-04-05 13:46:31 +02001007 struct net_device *soft_iface = bat_priv->soft_iface;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001008 int hdr_size = 0;
1009 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001010
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001011 if (!atomic_read(&bat_priv->distributed_arp_table))
1012 goto out;
1013
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001014 vid = batadv_dat_get_vid(skb, &hdr_size);
1015
1016 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001017 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1018 * message to the selected DHT candidates
1019 */
1020 if (type != ARPOP_REQUEST)
1021 goto out;
1022
Antonio Quartulli843314c2016-09-25 08:46:36 +08001023 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001024
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001025 ip_src = batadv_arp_ip_src(skb, hdr_size);
1026 hw_src = batadv_arp_hw_src(skb, hdr_size);
1027 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001028
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001029 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001030
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001031 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001032 if (dat_entry) {
Antonio Quartulli88e48d72013-05-09 09:35:45 +02001033 /* If the ARP request is destined for a local client the local
1034 * client will answer itself. DAT would only generate a
1035 * duplicate packet.
1036 *
1037 * Moreover, if the soft-interface is enslaved into a bridge, an
1038 * additional DAT answer may trigger kernel warnings about
1039 * a packet coming from the wrong port.
1040 */
Antonio Quartullicc2f3382014-03-29 17:27:38 +01001041 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
Antonio Quartulli88e48d72013-05-09 09:35:45 +02001042 ret = true;
1043 goto out;
1044 }
1045
Andreas Pape00311de2016-09-05 13:20:25 +02001046 /* If BLA is enabled, only send ARP replies if we have claimed
1047 * the destination for the ARP request or if no one else of
1048 * the backbone gws belonging to our backbone has claimed the
1049 * destination.
1050 */
1051 if (!batadv_bla_check_claim(bat_priv,
1052 dat_entry->mac_addr, vid)) {
1053 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1054 "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1055 dat_entry->mac_addr);
1056 ret = true;
1057 goto out;
1058 }
1059
Linus Lüssingcbfc16d2016-07-05 20:01:36 +02001060 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1061 dat_entry->mac_addr,
1062 hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001063 if (!skb_new)
1064 goto out;
1065
Tobias Klauserab044f82017-04-05 13:46:31 +02001066 skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1067
Sven Eckelmann36d4d682017-04-05 16:26:17 +02001068 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1069 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1070 skb->len + ETH_HLEN + hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001071
1072 netif_rx(skb_new);
1073 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1074 ret = true;
1075 } else {
Marek Lindner93178012013-03-10 19:29:15 +08001076 /* Send the request to the DHT */
Antonio Quartulli28717342016-03-12 11:12:59 +01001077 ret = batadv_dat_send_data(bat_priv, skb, ip_dst, vid,
Antonio Quartullic384ea32011-06-26 03:37:18 +02001078 BATADV_P_DAT_DHT_GET);
1079 }
1080out:
1081 if (dat_entry)
Sven Eckelmanna6416f92016-01-17 11:01:17 +01001082 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001083 return ret;
1084}
1085
1086/**
1087 * batadv_dat_snoop_incoming_arp_request - snoop the ARP request and try to
1088 * answer using the local DAT storage
1089 * @bat_priv: the bat priv with all the soft interface information
1090 * @skb: packet to check
1091 * @hdr_size: size of the encapsulation header
1092 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001093 * Return: true if the request has been answered, false otherwise.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001094 */
1095bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1096 struct sk_buff *skb, int hdr_size)
1097{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001098 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001099 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001100 u8 *hw_src;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001101 struct sk_buff *skb_new;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001102 struct batadv_dat_entry *dat_entry = NULL;
1103 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001104 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001105 int err;
1106
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001107 if (!atomic_read(&bat_priv->distributed_arp_table))
1108 goto out;
1109
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001110 vid = batadv_dat_get_vid(skb, &hdr_size);
1111
Antonio Quartullic384ea32011-06-26 03:37:18 +02001112 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1113 if (type != ARPOP_REQUEST)
1114 goto out;
1115
1116 hw_src = batadv_arp_hw_src(skb, hdr_size);
1117 ip_src = batadv_arp_ip_src(skb, hdr_size);
1118 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1119
Antonio Quartulli843314c2016-09-25 08:46:36 +08001120 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001121
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001122 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001123
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001124 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001125 if (!dat_entry)
1126 goto out;
1127
Linus Lüssingcbfc16d2016-07-05 20:01:36 +02001128 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1129 dat_entry->mac_addr, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001130 if (!skb_new)
1131 goto out;
1132
Marek Lindner93178012013-03-10 19:29:15 +08001133 /* To preserve backwards compatibility, the node has choose the outgoing
1134 * format based on the incoming request packet type. The assumption is
1135 * that a node not using the 4addr packet format doesn't support it.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001136 */
1137 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
Linus Lüssinge300d312013-07-03 10:40:00 +02001138 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1139 BATADV_P_DAT_CACHE_REPLY,
Antonio Quartulli6c413b12013-11-05 19:31:08 +01001140 NULL, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001141 else
Antonio Quartulli6c413b12013-11-05 19:31:08 +01001142 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001143
Linus Lüssinge300d312013-07-03 10:40:00 +02001144 if (err != NET_XMIT_DROP) {
Martin Hundebøll4046b242012-04-20 17:02:45 +02001145 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001146 ret = true;
Martin Hundebøll4046b242012-04-20 17:02:45 +02001147 }
Antonio Quartullic384ea32011-06-26 03:37:18 +02001148out:
1149 if (dat_entry)
Sven Eckelmanna6416f92016-01-17 11:01:17 +01001150 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001151 if (ret)
1152 kfree_skb(skb);
1153 return ret;
1154}
1155
1156/**
1157 * batadv_dat_snoop_outgoing_arp_reply - snoop the ARP reply and fill the DHT
1158 * @bat_priv: the bat priv with all the soft interface information
1159 * @skb: packet to check
1160 */
1161void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1162 struct sk_buff *skb)
1163{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001164 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001165 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001166 u8 *hw_src, *hw_dst;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001167 int hdr_size = 0;
1168 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001169
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001170 if (!atomic_read(&bat_priv->distributed_arp_table))
1171 return;
1172
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001173 vid = batadv_dat_get_vid(skb, &hdr_size);
1174
1175 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001176 if (type != ARPOP_REPLY)
1177 return;
1178
Antonio Quartulli843314c2016-09-25 08:46:36 +08001179 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001180
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001181 hw_src = batadv_arp_hw_src(skb, hdr_size);
1182 ip_src = batadv_arp_ip_src(skb, hdr_size);
1183 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1184 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001185
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001186 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1187 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001188
1189 /* Send the ARP reply to the candidates for both the IP addresses that
Marek Lindner93178012013-03-10 19:29:15 +08001190 * the node obtained from the ARP reply
Antonio Quartullic384ea32011-06-26 03:37:18 +02001191 */
Antonio Quartulli28717342016-03-12 11:12:59 +01001192 batadv_dat_send_data(bat_priv, skb, ip_src, vid, BATADV_P_DAT_DHT_PUT);
1193 batadv_dat_send_data(bat_priv, skb, ip_dst, vid, BATADV_P_DAT_DHT_PUT);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001194}
Antonio Quartulliaa143d22014-09-01 14:37:27 +02001195
Antonio Quartullic384ea32011-06-26 03:37:18 +02001196/**
1197 * batadv_dat_snoop_incoming_arp_reply - snoop the ARP reply and fill the local
1198 * DAT storage only
1199 * @bat_priv: the bat priv with all the soft interface information
1200 * @skb: packet to check
Marek Lindner93178012013-03-10 19:29:15 +08001201 * @hdr_size: size of the encapsulation header
Antonio Quartullif202a662015-06-16 21:06:24 +02001202 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001203 * Return: true if the packet was snooped and consumed by DAT. False if the
Antonio Quartullif202a662015-06-16 21:06:24 +02001204 * packet has to be delivered to the interface
Antonio Quartullic384ea32011-06-26 03:37:18 +02001205 */
1206bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1207 struct sk_buff *skb, int hdr_size)
1208{
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001209 struct batadv_dat_entry *dat_entry = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001210 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001211 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001212 u8 *hw_src, *hw_dst;
Antonio Quartullif202a662015-06-16 21:06:24 +02001213 bool dropped = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001214 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001215
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001216 if (!atomic_read(&bat_priv->distributed_arp_table))
1217 goto out;
1218
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001219 vid = batadv_dat_get_vid(skb, &hdr_size);
1220
Antonio Quartullic384ea32011-06-26 03:37:18 +02001221 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1222 if (type != ARPOP_REPLY)
1223 goto out;
1224
Antonio Quartulli843314c2016-09-25 08:46:36 +08001225 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001226
1227 hw_src = batadv_arp_hw_src(skb, hdr_size);
1228 ip_src = batadv_arp_ip_src(skb, hdr_size);
1229 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1230 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1231
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001232 /* If ip_dst is already in cache and has the right mac address,
1233 * drop this frame if this ARP reply is destined for us because it's
1234 * most probably an ARP reply generated by another node of the DHT.
1235 * We have most probably received already a reply earlier. Delivering
1236 * this frame would lead to doubled receive of an ARP reply.
1237 */
1238 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1239 if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
1240 batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1241 hw_src, &ip_src, hw_dst, &ip_dst,
1242 dat_entry->mac_addr, &dat_entry->ip);
1243 dropped = true;
1244 goto out;
1245 }
1246
Antonio Quartullic384ea32011-06-26 03:37:18 +02001247 /* Update our internal cache with both the IP addresses the node got
1248 * within the ARP reply
1249 */
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001250 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1251 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001252
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001253 /* If BLA is enabled, only forward ARP replies if we have claimed the
1254 * source of the ARP reply or if no one else of the same backbone has
1255 * already claimed that client. This prevents that different gateways
1256 * to the same backbone all forward the ARP reply leading to multiple
1257 * replies in the backbone.
1258 */
1259 if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1260 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1261 "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1262 hw_src);
1263 dropped = true;
1264 goto out;
1265 }
1266
Antonio Quartullic384ea32011-06-26 03:37:18 +02001267 /* if this REPLY is directed to a client of mine, let's deliver the
1268 * packet to the interface
1269 */
Antonio Quartullif202a662015-06-16 21:06:24 +02001270 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1271
1272 /* if this REPLY is sent on behalf of a client of mine, let's drop the
1273 * packet because the client will reply by itself
1274 */
1275 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001276out:
Antonio Quartullif202a662015-06-16 21:06:24 +02001277 if (dropped)
Matthias Schiffer0d15bec2013-01-23 18:11:53 +01001278 kfree_skb(skb);
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001279 if (dat_entry)
1280 batadv_dat_entry_put(dat_entry);
Antonio Quartullif202a662015-06-16 21:06:24 +02001281 /* if dropped == false -> deliver to the interface */
1282 return dropped;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001283}
1284
1285/**
1286 * batadv_dat_drop_broadcast_packet - check if an ARP request has to be dropped
Marek Lindner93178012013-03-10 19:29:15 +08001287 * (because the node has already obtained the reply via DAT) or not
Antonio Quartullic384ea32011-06-26 03:37:18 +02001288 * @bat_priv: the bat priv with all the soft interface information
1289 * @forw_packet: the broadcast packet
1290 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001291 * Return: true if the node can drop the packet, false otherwise.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001292 */
1293bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1294 struct batadv_forw_packet *forw_packet)
1295{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001296 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001297 __be32 ip_dst;
1298 struct batadv_dat_entry *dat_entry = NULL;
1299 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001300 int hdr_size = sizeof(struct batadv_bcast_packet);
1301 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001302
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001303 if (!atomic_read(&bat_priv->distributed_arp_table))
1304 goto out;
1305
Antonio Quartullic384ea32011-06-26 03:37:18 +02001306 /* If this packet is an ARP_REQUEST and the node already has the
1307 * information that it is going to ask, then the packet can be dropped
1308 */
Linus Lüssinge2d9ba42017-02-17 11:17:07 +01001309 if (batadv_forw_packet_is_rebroadcast(forw_packet))
Antonio Quartullic384ea32011-06-26 03:37:18 +02001310 goto out;
1311
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001312 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1313
1314 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001315 if (type != ARPOP_REQUEST)
1316 goto out;
1317
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001318 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1319 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001320 /* check if the node already got this entry */
1321 if (!dat_entry) {
1322 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1323 "ARP Request for %pI4: fallback\n", &ip_dst);
1324 goto out;
1325 }
1326
1327 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1328 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1329 ret = true;
1330
1331out:
1332 if (dat_entry)
Sven Eckelmanna6416f92016-01-17 11:01:17 +01001333 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001334 return ret;
1335}