blob: 2e6198ecc07d569435d8c5353ba7fa98ebce6332 [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2011-2015 B.A.T.M.A.N. contributors:
Antonio Quartulli785ea112011-11-23 11:35:44 +01002 *
3 * Antonio Quartulli
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Antonio Quartulli785ea112011-11-23 11:35:44 +010016 */
17
18#include <linux/if_ether.h>
19#include <linux/if_arp.h>
Antonio Quartullibe1db4f2013-06-04 12:11:43 +020020#include <linux/if_vlan.h>
Antonio Quartullic384ea32011-06-26 03:37:18 +020021#include <net/arp.h>
Antonio Quartulli785ea112011-11-23 11:35:44 +010022
23#include "main.h"
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +020024#include "hash.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010025#include "distributed-arp-table.h"
26#include "hard-interface.h"
27#include "originator.h"
28#include "send.h"
29#include "types.h"
Antonio Quartullic384ea32011-06-26 03:37:18 +020030#include "translation-table.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010031
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020032static void batadv_dat_purge(struct work_struct *work);
33
34/**
35 * batadv_dat_start_timer - initialise the DAT periodic worker
36 * @bat_priv: the bat priv with all the soft interface information
37 */
38static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
39{
40 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
41 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
42 msecs_to_jiffies(10000));
43}
44
45/**
Marek Lindner93178012013-03-10 19:29:15 +080046 * batadv_dat_entry_free_ref - decrement the dat_entry refcounter and possibly
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020047 * free it
Marek Lindner93178012013-03-10 19:29:15 +080048 * @dat_entry: the entry to free
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020049 */
50static void batadv_dat_entry_free_ref(struct batadv_dat_entry *dat_entry)
51{
52 if (atomic_dec_and_test(&dat_entry->refcount))
53 kfree_rcu(dat_entry, rcu);
54}
55
56/**
Marek Lindner93178012013-03-10 19:29:15 +080057 * batadv_dat_to_purge - check whether a dat_entry has to be purged or not
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020058 * @dat_entry: the entry to check
59 *
Marek Lindner93178012013-03-10 19:29:15 +080060 * Returns true if the entry has to be purged now, false otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020061 */
62static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
63{
64 return batadv_has_timed_out(dat_entry->last_update,
65 BATADV_DAT_ENTRY_TIMEOUT);
66}
67
68/**
69 * __batadv_dat_purge - delete entries from the DAT local storage
70 * @bat_priv: the bat priv with all the soft interface information
71 * @to_purge: function in charge to decide whether an entry has to be purged or
72 * not. This function takes the dat_entry as argument and has to
73 * returns a boolean value: true is the entry has to be deleted,
74 * false otherwise
75 *
Marek Lindner93178012013-03-10 19:29:15 +080076 * Loops over each entry in the DAT local storage and deletes it if and only if
77 * the to_purge function passed as argument returns true.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020078 */
79static void __batadv_dat_purge(struct batadv_priv *bat_priv,
80 bool (*to_purge)(struct batadv_dat_entry *))
81{
82 spinlock_t *list_lock; /* protects write access to the hash lists */
83 struct batadv_dat_entry *dat_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -080084 struct hlist_node *node_tmp;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020085 struct hlist_head *head;
86 uint32_t i;
87
88 if (!bat_priv->dat.hash)
89 return;
90
91 for (i = 0; i < bat_priv->dat.hash->size; i++) {
92 head = &bat_priv->dat.hash->table[i];
93 list_lock = &bat_priv->dat.hash->list_locks[i];
94
95 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080096 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020097 hash_entry) {
Marek Lindner93178012013-03-10 19:29:15 +080098 /* if a helper function has been passed as parameter,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +020099 * ask it if the entry has to be purged or not
100 */
101 if (to_purge && !to_purge(dat_entry))
102 continue;
103
Sasha Levinb67bfe02013-02-27 17:06:00 -0800104 hlist_del_rcu(&dat_entry->hash_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200105 batadv_dat_entry_free_ref(dat_entry);
106 }
107 spin_unlock_bh(list_lock);
108 }
109}
110
111/**
112 * batadv_dat_purge - periodic task that deletes old entries from the local DAT
113 * hash table
114 * @work: kernel work struct
115 */
116static void batadv_dat_purge(struct work_struct *work)
117{
118 struct delayed_work *delayed_work;
119 struct batadv_priv_dat *priv_dat;
120 struct batadv_priv *bat_priv;
121
122 delayed_work = container_of(work, struct delayed_work, work);
123 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
124 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
125
126 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
127 batadv_dat_start_timer(bat_priv);
128}
129
130/**
131 * batadv_compare_dat - comparing function used in the local DAT hash table
132 * @node: node in the local table
133 * @data2: second object to compare the node to
134 *
Marek Lindner93178012013-03-10 19:29:15 +0800135 * Returns 1 if the two entries are the same, 0 otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200136 */
137static int batadv_compare_dat(const struct hlist_node *node, const void *data2)
138{
139 const void *data1 = container_of(node, struct batadv_dat_entry,
140 hash_entry);
141
Antonio Quartulli36484f82013-11-07 08:02:07 +0100142 return memcmp(data1, data2, sizeof(__be32)) == 0 ? 1 : 0;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200143}
144
Antonio Quartulli785ea112011-11-23 11:35:44 +0100145/**
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200146 * batadv_arp_hw_src - extract the hw_src field from an ARP packet
147 * @skb: ARP packet
148 * @hdr_size: size of the possible header before the ARP packet
149 *
Marek Lindner93178012013-03-10 19:29:15 +0800150 * Returns the value of the hw_src field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200151 */
152static uint8_t *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
153{
154 uint8_t *addr;
155
156 addr = (uint8_t *)(skb->data + hdr_size);
157 addr += ETH_HLEN + sizeof(struct arphdr);
158
159 return addr;
160}
161
162/**
163 * batadv_arp_ip_src - extract the ip_src field from an ARP packet
164 * @skb: ARP packet
165 * @hdr_size: size of the possible header before the ARP packet
166 *
Marek Lindner93178012013-03-10 19:29:15 +0800167 * Returns the value of the ip_src field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200168 */
169static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
170{
171 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
172}
173
174/**
175 * batadv_arp_hw_dst - extract the hw_dst field from an ARP packet
176 * @skb: ARP packet
177 * @hdr_size: size of the possible header before the ARP packet
178 *
Marek Lindner93178012013-03-10 19:29:15 +0800179 * Returns the value of the hw_dst field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200180 */
181static uint8_t *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
182{
183 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
184}
185
186/**
187 * batadv_arp_ip_dst - extract the ip_dst field from an ARP packet
188 * @skb: ARP packet
189 * @hdr_size: size of the possible header before the ARP packet
190 *
Marek Lindner93178012013-03-10 19:29:15 +0800191 * Returns the value of the ip_dst field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200192 */
193static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
194{
195 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
196}
197
198/**
Antonio Quartulli785ea112011-11-23 11:35:44 +0100199 * batadv_hash_dat - compute the hash value for an IP address
200 * @data: data to hash
201 * @size: size of the hash table
202 *
Marek Lindner93178012013-03-10 19:29:15 +0800203 * Returns the selected index in the hash table for the given data.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100204 */
205static uint32_t batadv_hash_dat(const void *data, uint32_t size)
206{
Antonio Quartulli785ea112011-11-23 11:35:44 +0100207 uint32_t hash = 0;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200208 const struct batadv_dat_entry *dat = data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100209 const unsigned char *key;
210 uint32_t i;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100211
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100212 key = (const unsigned char *)&dat->ip;
213 for (i = 0; i < sizeof(dat->ip); i++) {
214 hash += key[i];
215 hash += (hash << 10);
216 hash ^= (hash >> 6);
217 }
218
219 key = (const unsigned char *)&dat->vid;
220 for (i = 0; i < sizeof(dat->vid); i++) {
221 hash += key[i];
222 hash += (hash << 10);
223 hash ^= (hash >> 6);
224 }
Antonio Quartulli785ea112011-11-23 11:35:44 +0100225
226 hash += (hash << 3);
227 hash ^= (hash >> 11);
228 hash += (hash << 15);
229
230 return hash % size;
231}
232
233/**
Marek Lindner93178012013-03-10 19:29:15 +0800234 * batadv_dat_entry_hash_find - look for a given dat_entry in the local hash
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200235 * table
236 * @bat_priv: the bat priv with all the soft interface information
237 * @ip: search key
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200238 * @vid: VLAN identifier
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200239 *
Marek Lindner93178012013-03-10 19:29:15 +0800240 * Returns the dat_entry if found, NULL otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200241 */
242static struct batadv_dat_entry *
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200243batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
244 unsigned short vid)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200245{
246 struct hlist_head *head;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200247 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200248 struct batadv_hashtable *hash = bat_priv->dat.hash;
249 uint32_t index;
250
251 if (!hash)
252 return NULL;
253
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200254 to_find.ip = ip;
255 to_find.vid = vid;
256
257 index = batadv_hash_dat(&to_find, hash->size);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200258 head = &hash->table[index];
259
260 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800261 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200262 if (dat_entry->ip != ip)
263 continue;
264
265 if (!atomic_inc_not_zero(&dat_entry->refcount))
266 continue;
267
268 dat_entry_tmp = dat_entry;
269 break;
270 }
271 rcu_read_unlock();
272
273 return dat_entry_tmp;
274}
275
276/**
277 * batadv_dat_entry_add - add a new dat entry or update it if already exists
278 * @bat_priv: the bat priv with all the soft interface information
279 * @ip: ipv4 to add/edit
280 * @mac_addr: mac address to assign to the given ipv4
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200281 * @vid: VLAN identifier
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200282 */
283static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200284 uint8_t *mac_addr, unsigned short vid)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200285{
286 struct batadv_dat_entry *dat_entry;
287 int hash_added;
288
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200289 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200290 /* if this entry is already known, just update it */
291 if (dat_entry) {
292 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100293 ether_addr_copy(dat_entry->mac_addr, mac_addr);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200294 dat_entry->last_update = jiffies;
295 batadv_dbg(BATADV_DBG_DAT, bat_priv,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200296 "Entry updated: %pI4 %pM (vid: %d)\n",
297 &dat_entry->ip, dat_entry->mac_addr,
298 BATADV_PRINT_VID(vid));
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200299 goto out;
300 }
301
302 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
303 if (!dat_entry)
304 goto out;
305
306 dat_entry->ip = ip;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200307 dat_entry->vid = vid;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100308 ether_addr_copy(dat_entry->mac_addr, mac_addr);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200309 dat_entry->last_update = jiffies;
310 atomic_set(&dat_entry->refcount, 2);
311
312 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200313 batadv_hash_dat, dat_entry,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200314 &dat_entry->hash_entry);
315
316 if (unlikely(hash_added != 0)) {
317 /* remove the reference for the hash */
318 batadv_dat_entry_free_ref(dat_entry);
319 goto out;
320 }
321
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200322 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
323 &dat_entry->ip, dat_entry->mac_addr, BATADV_PRINT_VID(vid));
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200324
325out:
326 if (dat_entry)
327 batadv_dat_entry_free_ref(dat_entry);
328}
329
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200330#ifdef CONFIG_BATMAN_ADV_DEBUG
331
332/**
333 * batadv_dbg_arp - print a debug message containing all the ARP packet details
334 * @bat_priv: the bat priv with all the soft interface information
335 * @skb: ARP packet
336 * @type: ARP type
337 * @hdr_size: size of the possible header before the ARP packet
338 * @msg: message to print together with the debugging information
339 */
340static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
341 uint16_t type, int hdr_size, char *msg)
342{
343 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
344 struct batadv_bcast_packet *bcast_pkt;
345 uint8_t *orig_addr;
346 __be32 ip_src, ip_dst;
347
348 if (msg)
349 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
350
351 ip_src = batadv_arp_ip_src(skb, hdr_size);
352 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
353 batadv_dbg(BATADV_DBG_DAT, bat_priv,
354 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
355 batadv_arp_hw_src(skb, hdr_size), &ip_src,
356 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
357
358 if (hdr_size == 0)
359 return;
360
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200361 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
362
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100363 switch (unicast_4addr_packet->u.packet_type) {
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200364 case BATADV_UNICAST:
365 batadv_dbg(BATADV_DBG_DAT, bat_priv,
366 "* encapsulated within a UNICAST packet\n");
367 break;
368 case BATADV_UNICAST_4ADDR:
369 batadv_dbg(BATADV_DBG_DAT, bat_priv,
370 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
371 unicast_4addr_packet->src);
372 switch (unicast_4addr_packet->subtype) {
373 case BATADV_P_DAT_DHT_PUT:
374 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
375 break;
376 case BATADV_P_DAT_DHT_GET:
377 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
378 break;
379 case BATADV_P_DAT_CACHE_REPLY:
380 batadv_dbg(BATADV_DBG_DAT, bat_priv,
381 "* type: DAT_CACHE_REPLY\n");
382 break;
383 case BATADV_P_DATA:
384 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
385 break;
386 default:
387 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100388 unicast_4addr_packet->u.packet_type);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200389 }
390 break;
391 case BATADV_BCAST:
392 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
393 orig_addr = bcast_pkt->orig;
394 batadv_dbg(BATADV_DBG_DAT, bat_priv,
395 "* encapsulated within a BCAST packet (src: %pM)\n",
396 orig_addr);
397 break;
398 default:
399 batadv_dbg(BATADV_DBG_DAT, bat_priv,
400 "* encapsulated within an unknown packet type (0x%x)\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100401 unicast_4addr_packet->u.packet_type);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200402 }
403}
404
405#else
406
407static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
408 uint16_t type, int hdr_size, char *msg)
409{
410}
411
412#endif /* CONFIG_BATMAN_ADV_DEBUG */
413
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200414/**
Antonio Quartulli785ea112011-11-23 11:35:44 +0100415 * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate
416 * @res: the array with the already selected candidates
417 * @select: number of already selected candidates
418 * @tmp_max: address of the currently evaluated node
419 * @max: current round max address
420 * @last_max: address of the last selected candidate
421 * @candidate: orig_node under evaluation
422 * @max_orig_node: last selected candidate
423 *
Marek Lindner93178012013-03-10 19:29:15 +0800424 * Returns true if the node has been elected as next candidate or false
425 * otherwise.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100426 */
427static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
428 int select, batadv_dat_addr_t tmp_max,
429 batadv_dat_addr_t max,
430 batadv_dat_addr_t last_max,
431 struct batadv_orig_node *candidate,
432 struct batadv_orig_node *max_orig_node)
433{
434 bool ret = false;
435 int j;
436
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800437 /* check if orig node candidate is running DAT */
438 if (!(candidate->capabilities & BATADV_ORIG_CAPA_HAS_DAT))
439 goto out;
440
Antonio Quartulli785ea112011-11-23 11:35:44 +0100441 /* Check if this node has already been selected... */
442 for (j = 0; j < select; j++)
443 if (res[j].orig_node == candidate)
444 break;
445 /* ..and possibly skip it */
446 if (j < select)
447 goto out;
448 /* sanity check: has it already been selected? This should not happen */
449 if (tmp_max > last_max)
450 goto out;
451 /* check if during this iteration an originator with a closer dht
452 * address has already been found
453 */
454 if (tmp_max < max)
455 goto out;
456 /* this is an hash collision with the temporary selected node. Choose
457 * the one with the lowest address
458 */
Pau Koning816cd5b2013-02-12 00:18:45 +0000459 if ((tmp_max == max) && max_orig_node &&
Antonio Quartulli785ea112011-11-23 11:35:44 +0100460 (batadv_compare_eth(candidate->orig, max_orig_node->orig) > 0))
461 goto out;
462
463 ret = true;
464out:
465 return ret;
466}
467
468/**
469 * batadv_choose_next_candidate - select the next DHT candidate
470 * @bat_priv: the bat priv with all the soft interface information
471 * @cands: candidates array
472 * @select: number of candidates already present in the array
473 * @ip_key: key to look up in the DHT
474 * @last_max: pointer where the address of the selected candidate will be saved
475 */
476static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
477 struct batadv_dat_candidate *cands,
478 int select, batadv_dat_addr_t ip_key,
479 batadv_dat_addr_t *last_max)
480{
481 batadv_dat_addr_t max = 0, tmp_max = 0;
482 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
483 struct batadv_hashtable *hash = bat_priv->orig_hash;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100484 struct hlist_head *head;
485 int i;
486
487 /* if no node is eligible as candidate, leave the candidate type as
488 * NOT_FOUND
489 */
490 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
491
Marek Lindner93178012013-03-10 19:29:15 +0800492 /* iterate over the originator list and find the node with the closest
Antonio Quartulli785ea112011-11-23 11:35:44 +0100493 * dat_address which has not been selected yet
494 */
495 for (i = 0; i < hash->size; i++) {
496 head = &hash->table[i];
497
498 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800499 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Marek Lindner93178012013-03-10 19:29:15 +0800500 /* the dht space is a ring using unsigned addresses */
Antonio Quartulli785ea112011-11-23 11:35:44 +0100501 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
502 ip_key;
503
504 if (!batadv_is_orig_node_eligible(cands, select,
505 tmp_max, max,
506 *last_max, orig_node,
507 max_orig_node))
508 continue;
509
510 if (!atomic_inc_not_zero(&orig_node->refcount))
511 continue;
512
513 max = tmp_max;
514 if (max_orig_node)
515 batadv_orig_node_free_ref(max_orig_node);
516 max_orig_node = orig_node;
517 }
518 rcu_read_unlock();
519 }
520 if (max_orig_node) {
521 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
522 cands[select].orig_node = max_orig_node;
523 batadv_dbg(BATADV_DBG_DAT, bat_priv,
524 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
525 select, max_orig_node->orig, max_orig_node->dat_addr,
526 max);
527 }
528 *last_max = max;
529}
530
531/**
Marek Lindner93178012013-03-10 19:29:15 +0800532 * batadv_dat_select_candidates - select the nodes which the DHT message has to
Antonio Quartulli785ea112011-11-23 11:35:44 +0100533 * be sent to
534 * @bat_priv: the bat priv with all the soft interface information
535 * @ip_dst: ipv4 to look up in the DHT
536 *
537 * An originator O is selected if and only if its DHT_ID value is one of three
538 * closest values (from the LEFT, with wrap around if needed) then the hash
539 * value of the key. ip_dst is the key.
540 *
Marek Lindner93178012013-03-10 19:29:15 +0800541 * Returns the candidate array of size BATADV_DAT_CANDIDATE_NUM.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100542 */
543static struct batadv_dat_candidate *
544batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst)
545{
546 int select;
547 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
548 struct batadv_dat_candidate *res;
549
550 if (!bat_priv->orig_hash)
551 return NULL;
552
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200553 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
554 GFP_ATOMIC);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100555 if (!res)
556 return NULL;
557
558 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&ip_dst,
559 BATADV_DAT_ADDR_MAX);
560
561 batadv_dbg(BATADV_DBG_DAT, bat_priv,
562 "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &ip_dst,
563 ip_key);
564
565 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
566 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
567 &last_max);
568
569 return res;
570}
571
572/**
573 * batadv_dat_send_data - send a payload to the selected candidates
574 * @bat_priv: the bat priv with all the soft interface information
575 * @skb: payload to send
576 * @ip: the DHT key
577 * @packet_subtype: unicast4addr packet subtype to use
578 *
Marek Lindner93178012013-03-10 19:29:15 +0800579 * This function copies the skb with pskb_copy() and is sent as unicast packet
580 * to each of the selected candidates.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100581 *
Marek Lindner93178012013-03-10 19:29:15 +0800582 * Returns true if the packet is sent to at least one candidate, false
583 * otherwise.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100584 */
585static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
586 struct sk_buff *skb, __be32 ip,
587 int packet_subtype)
588{
589 int i;
590 bool ret = false;
591 int send_status;
592 struct batadv_neigh_node *neigh_node = NULL;
593 struct sk_buff *tmp_skb;
594 struct batadv_dat_candidate *cand;
595
596 cand = batadv_dat_select_candidates(bat_priv, ip);
597 if (!cand)
598 goto out;
599
600 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
601
602 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
603 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
604 continue;
605
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100606 neigh_node = batadv_orig_router_get(cand[i].orig_node,
607 BATADV_IF_DEFAULT);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100608 if (!neigh_node)
609 goto free_orig;
610
Octavian Purdilabad93e92014-06-12 01:36:26 +0300611 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
Martin Hundebøllf097e252013-05-23 16:53:01 +0200612 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
613 cand[i].orig_node,
614 packet_subtype)) {
Antonio Quartulli785ea112011-11-23 11:35:44 +0100615 kfree_skb(tmp_skb);
616 goto free_neigh;
617 }
618
619 send_status = batadv_send_skb_packet(tmp_skb,
620 neigh_node->if_incoming,
621 neigh_node->addr);
Martin Hundebøll4046b242012-04-20 17:02:45 +0200622 if (send_status == NET_XMIT_SUCCESS) {
623 /* count the sent packet */
624 switch (packet_subtype) {
625 case BATADV_P_DAT_DHT_GET:
626 batadv_inc_counter(bat_priv,
627 BATADV_CNT_DAT_GET_TX);
628 break;
629 case BATADV_P_DAT_DHT_PUT:
630 batadv_inc_counter(bat_priv,
631 BATADV_CNT_DAT_PUT_TX);
632 break;
633 }
634
Antonio Quartulli785ea112011-11-23 11:35:44 +0100635 /* packet sent to a candidate: return true */
636 ret = true;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200637 }
Antonio Quartulli785ea112011-11-23 11:35:44 +0100638free_neigh:
639 batadv_neigh_node_free_ref(neigh_node);
640free_orig:
641 batadv_orig_node_free_ref(cand[i].orig_node);
642 }
643
644out:
645 kfree(cand);
646 return ret;
647}
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200648
649/**
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800650 * batadv_dat_tvlv_container_update - update the dat tvlv container after dat
651 * setting change
652 * @bat_priv: the bat priv with all the soft interface information
653 */
654static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
655{
656 char dat_mode;
657
658 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
659
660 switch (dat_mode) {
661 case 0:
662 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
663 break;
664 case 1:
665 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
666 NULL, 0);
667 break;
668 }
669}
670
671/**
672 * batadv_dat_status_update - update the dat tvlv container after dat
673 * setting change
674 * @net_dev: the soft interface net device
675 */
676void batadv_dat_status_update(struct net_device *net_dev)
677{
678 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200679
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800680 batadv_dat_tvlv_container_update(bat_priv);
681}
682
683/**
684 * batadv_gw_tvlv_ogm_handler_v1 - process incoming dat tvlv container
685 * @bat_priv: the bat priv with all the soft interface information
686 * @orig: the orig_node of the ogm
687 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
688 * @tvlv_value: tvlv buffer containing the gateway data
689 * @tvlv_value_len: tvlv buffer length
690 */
691static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
692 struct batadv_orig_node *orig,
693 uint8_t flags,
694 void *tvlv_value,
695 uint16_t tvlv_value_len)
696{
697 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
698 orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_DAT;
699 else
700 orig->capabilities |= BATADV_ORIG_CAPA_HAS_DAT;
701}
702
703/**
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200704 * batadv_dat_hash_free - free the local DAT hash table
705 * @bat_priv: the bat priv with all the soft interface information
706 */
707static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
708{
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200709 if (!bat_priv->dat.hash)
710 return;
711
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200712 __batadv_dat_purge(bat_priv, NULL);
713
714 batadv_hash_destroy(bat_priv->dat.hash);
715
716 bat_priv->dat.hash = NULL;
717}
718
719/**
720 * batadv_dat_init - initialise the DAT internals
721 * @bat_priv: the bat priv with all the soft interface information
722 */
723int batadv_dat_init(struct batadv_priv *bat_priv)
724{
725 if (bat_priv->dat.hash)
726 return 0;
727
728 bat_priv->dat.hash = batadv_hash_new(1024);
729
730 if (!bat_priv->dat.hash)
731 return -ENOMEM;
732
733 batadv_dat_start_timer(bat_priv);
734
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800735 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
736 NULL, BATADV_TVLV_DAT, 1,
737 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
738 batadv_dat_tvlv_container_update(bat_priv);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200739 return 0;
740}
741
742/**
743 * batadv_dat_free - free the DAT internals
744 * @bat_priv: the bat priv with all the soft interface information
745 */
746void batadv_dat_free(struct batadv_priv *bat_priv)
747{
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800748 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
749 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
750
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200751 cancel_delayed_work_sync(&bat_priv->dat.work);
752
753 batadv_dat_hash_free(bat_priv);
754}
755
756/**
757 * batadv_dat_cache_seq_print_text - print the local DAT hash table
758 * @seq: seq file to print on
759 * @offset: not used
760 */
761int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
762{
763 struct net_device *net_dev = (struct net_device *)seq->private;
764 struct batadv_priv *bat_priv = netdev_priv(net_dev);
765 struct batadv_hashtable *hash = bat_priv->dat.hash;
766 struct batadv_dat_entry *dat_entry;
767 struct batadv_hard_iface *primary_if;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200768 struct hlist_head *head;
769 unsigned long last_seen_jiffies;
770 int last_seen_msecs, last_seen_secs, last_seen_mins;
771 uint32_t i;
772
773 primary_if = batadv_seq_print_text_primary_if_get(seq);
774 if (!primary_if)
775 goto out;
776
777 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200778 seq_printf(seq, " %-7s %-9s %4s %11s\n", "IPv4",
779 "MAC", "VID", "last-seen");
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200780
781 for (i = 0; i < hash->size; i++) {
782 head = &hash->table[i];
783
784 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800785 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200786 last_seen_jiffies = jiffies - dat_entry->last_update;
787 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
788 last_seen_mins = last_seen_msecs / 60000;
789 last_seen_msecs = last_seen_msecs % 60000;
790 last_seen_secs = last_seen_msecs / 1000;
791
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200792 seq_printf(seq, " * %15pI4 %14pM %4i %6i:%02i\n",
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200793 &dat_entry->ip, dat_entry->mac_addr,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200794 BATADV_PRINT_VID(dat_entry->vid),
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200795 last_seen_mins, last_seen_secs);
796 }
797 rcu_read_unlock();
798 }
799
800out:
801 if (primary_if)
802 batadv_hardif_free_ref(primary_if);
803 return 0;
804}
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200805
806/**
807 * batadv_arp_get_type - parse an ARP packet and gets the type
808 * @bat_priv: the bat priv with all the soft interface information
809 * @skb: packet to analyse
810 * @hdr_size: size of the possible header before the ARP packet in the skb
811 *
Marek Lindner93178012013-03-10 19:29:15 +0800812 * Returns the ARP type if the skb contains a valid ARP packet, 0 otherwise.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200813 */
814static uint16_t batadv_arp_get_type(struct batadv_priv *bat_priv,
815 struct sk_buff *skb, int hdr_size)
816{
817 struct arphdr *arphdr;
818 struct ethhdr *ethhdr;
819 __be32 ip_src, ip_dst;
Matthias Schifferb618ad12013-01-24 18:18:27 +0100820 uint8_t *hw_src, *hw_dst;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200821 uint16_t type = 0;
822
823 /* pull the ethernet header */
824 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
825 goto out;
826
827 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
828
829 if (ethhdr->h_proto != htons(ETH_P_ARP))
830 goto out;
831
832 /* pull the ARP payload */
833 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
834 arp_hdr_len(skb->dev))))
835 goto out;
836
837 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
838
Marek Lindner93178012013-03-10 19:29:15 +0800839 /* check whether the ARP packet carries a valid IP information */
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200840 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
841 goto out;
842
843 if (arphdr->ar_pro != htons(ETH_P_IP))
844 goto out;
845
846 if (arphdr->ar_hln != ETH_ALEN)
847 goto out;
848
849 if (arphdr->ar_pln != 4)
850 goto out;
851
852 /* Check for bad reply/request. If the ARP message is not sane, DAT
853 * will simply ignore it
854 */
855 ip_src = batadv_arp_ip_src(skb, hdr_size);
856 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
857 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
Matthias Schiffer757dd822013-01-24 18:18:26 +0100858 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
859 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
860 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200861 goto out;
862
Matthias Schifferb618ad12013-01-24 18:18:27 +0100863 hw_src = batadv_arp_hw_src(skb, hdr_size);
864 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
865 goto out;
866
Marek Lindner93178012013-03-10 19:29:15 +0800867 /* don't care about the destination MAC address in ARP requests */
Matthias Schifferb618ad12013-01-24 18:18:27 +0100868 if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
869 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
870 if (is_zero_ether_addr(hw_dst) ||
871 is_multicast_ether_addr(hw_dst))
872 goto out;
873 }
874
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200875 type = ntohs(arphdr->ar_op);
876out:
877 return type;
878}
Antonio Quartullic384ea32011-06-26 03:37:18 +0200879
880/**
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200881 * batadv_dat_get_vid - extract the VLAN identifier from skb if any
882 * @skb: the buffer containing the packet to extract the VID from
883 * @hdr_size: the size of the batman-adv header encapsulating the packet
884 *
885 * If the packet embedded in the skb is vlan tagged this function returns the
886 * VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS is returned.
887 */
888static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
889{
890 unsigned short vid;
891
892 vid = batadv_get_vid(skb, *hdr_size);
893
894 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
895 * If the header contained in the packet is a VLAN one (which is longer)
896 * hdr_size is updated so that the functions will still skip the
897 * correct amount of bytes.
898 */
899 if (vid & BATADV_VLAN_HAS_TAG)
900 *hdr_size += VLAN_HLEN;
901
902 return vid;
903}
904
905/**
Antonio Quartullic384ea32011-06-26 03:37:18 +0200906 * batadv_dat_snoop_outgoing_arp_request - snoop the ARP request and try to
907 * answer using DAT
908 * @bat_priv: the bat priv with all the soft interface information
909 * @skb: packet to check
910 *
911 * Returns true if the message has been sent to the dht candidates, false
Marek Lindner93178012013-03-10 19:29:15 +0800912 * otherwise. In case of a positive return value the message has to be enqueued
913 * to permit the fallback.
Antonio Quartullic384ea32011-06-26 03:37:18 +0200914 */
915bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
916 struct sk_buff *skb)
917{
918 uint16_t type = 0;
919 __be32 ip_dst, ip_src;
920 uint8_t *hw_src;
921 bool ret = false;
922 struct batadv_dat_entry *dat_entry = NULL;
923 struct sk_buff *skb_new;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200924 int hdr_size = 0;
925 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +0200926
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200927 if (!atomic_read(&bat_priv->distributed_arp_table))
928 goto out;
929
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200930 vid = batadv_dat_get_vid(skb, &hdr_size);
931
932 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200933 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
934 * message to the selected DHT candidates
935 */
936 if (type != ARPOP_REQUEST)
937 goto out;
938
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200939 batadv_dbg_arp(bat_priv, skb, type, hdr_size,
940 "Parsing outgoing ARP REQUEST");
Antonio Quartullic384ea32011-06-26 03:37:18 +0200941
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200942 ip_src = batadv_arp_ip_src(skb, hdr_size);
943 hw_src = batadv_arp_hw_src(skb, hdr_size);
944 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200945
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200946 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200947
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200948 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200949 if (dat_entry) {
Antonio Quartulli88e48d72013-05-09 09:35:45 +0200950 /* If the ARP request is destined for a local client the local
951 * client will answer itself. DAT would only generate a
952 * duplicate packet.
953 *
954 * Moreover, if the soft-interface is enslaved into a bridge, an
955 * additional DAT answer may trigger kernel warnings about
956 * a packet coming from the wrong port.
957 */
Antonio Quartullicc2f3382014-03-29 17:27:38 +0100958 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
Antonio Quartulli88e48d72013-05-09 09:35:45 +0200959 ret = true;
960 goto out;
961 }
962
Antonio Quartullic384ea32011-06-26 03:37:18 +0200963 skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
Marek Lindner736292c2013-01-12 19:19:06 +0800964 bat_priv->soft_iface, ip_dst, hw_src,
Antonio Quartullic384ea32011-06-26 03:37:18 +0200965 dat_entry->mac_addr, hw_src);
966 if (!skb_new)
967 goto out;
968
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200969 if (vid & BATADV_VLAN_HAS_TAG)
970 skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
971 vid & VLAN_VID_MASK);
972
Antonio Quartullic384ea32011-06-26 03:37:18 +0200973 skb_reset_mac_header(skb_new);
974 skb_new->protocol = eth_type_trans(skb_new,
Marek Lindner736292c2013-01-12 19:19:06 +0800975 bat_priv->soft_iface);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200976 bat_priv->stats.rx_packets++;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200977 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN + hdr_size;
Marek Lindner736292c2013-01-12 19:19:06 +0800978 bat_priv->soft_iface->last_rx = jiffies;
Antonio Quartullic384ea32011-06-26 03:37:18 +0200979
980 netif_rx(skb_new);
981 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
982 ret = true;
983 } else {
Marek Lindner93178012013-03-10 19:29:15 +0800984 /* Send the request to the DHT */
Antonio Quartullic384ea32011-06-26 03:37:18 +0200985 ret = batadv_dat_send_data(bat_priv, skb, ip_dst,
986 BATADV_P_DAT_DHT_GET);
987 }
988out:
989 if (dat_entry)
990 batadv_dat_entry_free_ref(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200991 return ret;
992}
993
994/**
995 * batadv_dat_snoop_incoming_arp_request - snoop the ARP request and try to
996 * answer using the local DAT storage
997 * @bat_priv: the bat priv with all the soft interface information
998 * @skb: packet to check
999 * @hdr_size: size of the encapsulation header
1000 *
Marek Lindner93178012013-03-10 19:29:15 +08001001 * Returns true if the request has been answered, false otherwise.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001002 */
1003bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1004 struct sk_buff *skb, int hdr_size)
1005{
1006 uint16_t type;
1007 __be32 ip_src, ip_dst;
1008 uint8_t *hw_src;
1009 struct sk_buff *skb_new;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001010 struct batadv_dat_entry *dat_entry = NULL;
1011 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001012 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001013 int err;
1014
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001015 if (!atomic_read(&bat_priv->distributed_arp_table))
1016 goto out;
1017
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001018 vid = batadv_dat_get_vid(skb, &hdr_size);
1019
Antonio Quartullic384ea32011-06-26 03:37:18 +02001020 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1021 if (type != ARPOP_REQUEST)
1022 goto out;
1023
1024 hw_src = batadv_arp_hw_src(skb, hdr_size);
1025 ip_src = batadv_arp_ip_src(skb, hdr_size);
1026 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1027
1028 batadv_dbg_arp(bat_priv, skb, type, hdr_size,
1029 "Parsing incoming ARP REQUEST");
1030
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001031 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001032
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001033 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001034 if (!dat_entry)
1035 goto out;
1036
Antonio Quartullic384ea32011-06-26 03:37:18 +02001037 skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
Marek Lindner736292c2013-01-12 19:19:06 +08001038 bat_priv->soft_iface, ip_dst, hw_src,
Antonio Quartullic384ea32011-06-26 03:37:18 +02001039 dat_entry->mac_addr, hw_src);
1040
1041 if (!skb_new)
1042 goto out;
1043
Linus Lüssing927c2ed2014-01-19 22:22:45 +01001044 /* the rest of the TX path assumes that the mac_header offset pointing
1045 * to the inner Ethernet header has been set, therefore reset it now.
1046 */
1047 skb_reset_mac_header(skb_new);
1048
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001049 if (vid & BATADV_VLAN_HAS_TAG)
1050 skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
1051 vid & VLAN_VID_MASK);
1052
Marek Lindner93178012013-03-10 19:29:15 +08001053 /* To preserve backwards compatibility, the node has choose the outgoing
1054 * format based on the incoming request packet type. The assumption is
1055 * that a node not using the 4addr packet format doesn't support it.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001056 */
1057 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
Linus Lüssinge300d312013-07-03 10:40:00 +02001058 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1059 BATADV_P_DAT_CACHE_REPLY,
Antonio Quartulli6c413b12013-11-05 19:31:08 +01001060 NULL, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001061 else
Antonio Quartulli6c413b12013-11-05 19:31:08 +01001062 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001063
Linus Lüssinge300d312013-07-03 10:40:00 +02001064 if (err != NET_XMIT_DROP) {
Martin Hundebøll4046b242012-04-20 17:02:45 +02001065 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001066 ret = true;
Martin Hundebøll4046b242012-04-20 17:02:45 +02001067 }
Antonio Quartullic384ea32011-06-26 03:37:18 +02001068out:
1069 if (dat_entry)
1070 batadv_dat_entry_free_ref(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001071 if (ret)
1072 kfree_skb(skb);
1073 return ret;
1074}
1075
1076/**
1077 * batadv_dat_snoop_outgoing_arp_reply - snoop the ARP reply and fill the DHT
1078 * @bat_priv: the bat priv with all the soft interface information
1079 * @skb: packet to check
1080 */
1081void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1082 struct sk_buff *skb)
1083{
1084 uint16_t type;
1085 __be32 ip_src, ip_dst;
1086 uint8_t *hw_src, *hw_dst;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001087 int hdr_size = 0;
1088 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001089
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001090 if (!atomic_read(&bat_priv->distributed_arp_table))
1091 return;
1092
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001093 vid = batadv_dat_get_vid(skb, &hdr_size);
1094
1095 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001096 if (type != ARPOP_REPLY)
1097 return;
1098
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001099 batadv_dbg_arp(bat_priv, skb, type, hdr_size,
1100 "Parsing outgoing ARP REPLY");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001101
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001102 hw_src = batadv_arp_hw_src(skb, hdr_size);
1103 ip_src = batadv_arp_ip_src(skb, hdr_size);
1104 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1105 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001106
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001107 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1108 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001109
1110 /* Send the ARP reply to the candidates for both the IP addresses that
Marek Lindner93178012013-03-10 19:29:15 +08001111 * the node obtained from the ARP reply
Antonio Quartullic384ea32011-06-26 03:37:18 +02001112 */
1113 batadv_dat_send_data(bat_priv, skb, ip_src, BATADV_P_DAT_DHT_PUT);
1114 batadv_dat_send_data(bat_priv, skb, ip_dst, BATADV_P_DAT_DHT_PUT);
1115}
Antonio Quartulliaa143d22014-09-01 14:37:27 +02001116
Antonio Quartullic384ea32011-06-26 03:37:18 +02001117/**
1118 * batadv_dat_snoop_incoming_arp_reply - snoop the ARP reply and fill the local
1119 * DAT storage only
1120 * @bat_priv: the bat priv with all the soft interface information
1121 * @skb: packet to check
Marek Lindner93178012013-03-10 19:29:15 +08001122 * @hdr_size: size of the encapsulation header
Antonio Quartullic384ea32011-06-26 03:37:18 +02001123 */
1124bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1125 struct sk_buff *skb, int hdr_size)
1126{
1127 uint16_t type;
1128 __be32 ip_src, ip_dst;
1129 uint8_t *hw_src, *hw_dst;
1130 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001131 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001132
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001133 if (!atomic_read(&bat_priv->distributed_arp_table))
1134 goto out;
1135
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001136 vid = batadv_dat_get_vid(skb, &hdr_size);
1137
Antonio Quartullic384ea32011-06-26 03:37:18 +02001138 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1139 if (type != ARPOP_REPLY)
1140 goto out;
1141
1142 batadv_dbg_arp(bat_priv, skb, type, hdr_size,
1143 "Parsing incoming ARP REPLY");
1144
1145 hw_src = batadv_arp_hw_src(skb, hdr_size);
1146 ip_src = batadv_arp_ip_src(skb, hdr_size);
1147 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1148 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1149
1150 /* Update our internal cache with both the IP addresses the node got
1151 * within the ARP reply
1152 */
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001153 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1154 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001155
1156 /* if this REPLY is directed to a client of mine, let's deliver the
1157 * packet to the interface
1158 */
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001159 ret = !batadv_is_my_client(bat_priv, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001160out:
Matthias Schiffer0d15bec2013-01-23 18:11:53 +01001161 if (ret)
1162 kfree_skb(skb);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001163 /* if ret == false -> packet has to be delivered to the interface */
1164 return ret;
1165}
1166
1167/**
1168 * batadv_dat_drop_broadcast_packet - check if an ARP request has to be dropped
Marek Lindner93178012013-03-10 19:29:15 +08001169 * (because the node has already obtained the reply via DAT) or not
Antonio Quartullic384ea32011-06-26 03:37:18 +02001170 * @bat_priv: the bat priv with all the soft interface information
1171 * @forw_packet: the broadcast packet
1172 *
Marek Lindner93178012013-03-10 19:29:15 +08001173 * Returns true if the node can drop the packet, false otherwise.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001174 */
1175bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1176 struct batadv_forw_packet *forw_packet)
1177{
1178 uint16_t type;
1179 __be32 ip_dst;
1180 struct batadv_dat_entry *dat_entry = NULL;
1181 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001182 int hdr_size = sizeof(struct batadv_bcast_packet);
1183 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001184
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001185 if (!atomic_read(&bat_priv->distributed_arp_table))
1186 goto out;
1187
Antonio Quartullic384ea32011-06-26 03:37:18 +02001188 /* If this packet is an ARP_REQUEST and the node already has the
1189 * information that it is going to ask, then the packet can be dropped
1190 */
1191 if (forw_packet->num_packets)
1192 goto out;
1193
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001194 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1195
1196 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001197 if (type != ARPOP_REQUEST)
1198 goto out;
1199
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001200 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1201 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001202 /* check if the node already got this entry */
1203 if (!dat_entry) {
1204 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1205 "ARP Request for %pI4: fallback\n", &ip_dst);
1206 goto out;
1207 }
1208
1209 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1210 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1211 ret = true;
1212
1213out:
1214 if (dat_entry)
1215 batadv_dat_entry_free_ref(dat_entry);
1216 return ret;
1217}