blob: 310a4f353008d55ddf5e64e1bd4d98b0daa6a3d8 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann7a79d712018-12-31 23:59:59 +01002/* Copyright (C) 2011-2019 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
Linus Lüssingb61ec312018-12-30 16:52:53 +010022#include <asm/unaligned.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020023#include <linux/atomic.h>
Linus Lüssing65d7d4602015-06-16 17:10:22 +020024#include <linux/bitops.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/byteorder/generic.h>
26#include <linux/errno.h>
27#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010028#include <linux/gfp.h>
Antonio Quartulli785ea112011-11-23 11:35:44 +010029#include <linux/if_arp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020030#include <linux/if_ether.h>
Antonio Quartullibe1db4f2013-06-04 12:11:43 +020031#include <linux/if_vlan.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032#include <linux/in.h>
Linus Lüssingb61ec312018-12-30 16:52:53 +010033#include <linux/ip.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020034#include <linux/jiffies.h>
35#include <linux/kernel.h>
Sven Eckelmann68a67222016-01-16 10:29:47 +010036#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037#include <linux/list.h>
Linus Lüssing41aeefc2018-03-13 11:41:12 +010038#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020039#include <linux/rculist.h>
40#include <linux/rcupdate.h>
41#include <linux/seq_file.h>
42#include <linux/skbuff.h>
43#include <linux/slab.h>
44#include <linux/spinlock.h>
45#include <linux/stddef.h>
46#include <linux/string.h>
Linus Lüssingb61ec312018-12-30 16:52:53 +010047#include <linux/udp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020048#include <linux/workqueue.h>
Antonio Quartullic384ea32011-06-26 03:37:18 +020049#include <net/arp.h>
Linus Lüssing41aeefc2018-03-13 11:41:12 +010050#include <net/genetlink.h>
51#include <net/netlink.h>
52#include <net/sock.h>
53#include <uapi/linux/batman_adv.h>
Antonio Quartulli785ea112011-11-23 11:35:44 +010054
Andreas Pape00311de2016-09-05 13:20:25 +020055#include "bridge_loop_avoidance.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010056#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020057#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020058#include "log.h"
Linus Lüssing41aeefc2018-03-13 11:41:12 +010059#include "netlink.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010060#include "originator.h"
61#include "send.h"
Linus Lüssing41aeefc2018-03-13 11:41:12 +010062#include "soft-interface.h"
Antonio Quartullic384ea32011-06-26 03:37:18 +020063#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020064#include "tvlv.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010065
Linus Lüssingb61ec312018-12-30 16:52:53 +010066enum batadv_bootpop {
67 BATADV_BOOTREPLY = 2,
68};
69
70enum batadv_boothtype {
71 BATADV_HTYPE_ETHERNET = 1,
72};
73
74enum batadv_dhcpoptioncode {
75 BATADV_DHCP_OPT_PAD = 0,
76 BATADV_DHCP_OPT_MSG_TYPE = 53,
77 BATADV_DHCP_OPT_END = 255,
78};
79
80enum batadv_dhcptype {
81 BATADV_DHCPACK = 5,
82};
83
84/* { 99, 130, 83, 99 } */
85#define BATADV_DHCP_MAGIC 1669485411
86
87struct batadv_dhcp_packet {
88 __u8 op;
89 __u8 htype;
90 __u8 hlen;
91 __u8 hops;
92 __be32 xid;
93 __be16 secs;
94 __be16 flags;
95 __be32 ciaddr;
96 __be32 yiaddr;
97 __be32 siaddr;
98 __be32 giaddr;
99 __u8 chaddr[16];
100 __u8 sname[64];
101 __u8 file[128];
102 __be32 magic;
103 __u8 options[0];
104};
105
106#define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
107#define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
108
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200109static void batadv_dat_purge(struct work_struct *work);
110
111/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100112 * batadv_dat_start_timer() - initialise the DAT periodic worker
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200113 * @bat_priv: the bat priv with all the soft interface information
114 */
115static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
116{
117 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
118 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
119 msecs_to_jiffies(10000));
120}
121
122/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100123 * batadv_dat_entry_release() - release dat_entry from lists and queue for free
Sven Eckelmann68a67222016-01-16 10:29:47 +0100124 * after rcu grace period
125 * @ref: kref pointer of the dat_entry
126 */
127static void batadv_dat_entry_release(struct kref *ref)
128{
129 struct batadv_dat_entry *dat_entry;
130
131 dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
132
133 kfree_rcu(dat_entry, rcu);
134}
135
136/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100137 * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
Sven Eckelmann68a67222016-01-16 10:29:47 +0100138 * release it
139 * @dat_entry: dat_entry to be free'd
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200140 */
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100141static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200142{
Sven Eckelmann68a67222016-01-16 10:29:47 +0100143 kref_put(&dat_entry->refcount, batadv_dat_entry_release);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200144}
145
146/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100147 * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200148 * @dat_entry: the entry to check
149 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200150 * Return: true if the entry has to be purged now, false otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200151 */
152static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
153{
154 return batadv_has_timed_out(dat_entry->last_update,
155 BATADV_DAT_ENTRY_TIMEOUT);
156}
157
158/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100159 * __batadv_dat_purge() - delete entries from the DAT local storage
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200160 * @bat_priv: the bat priv with all the soft interface information
161 * @to_purge: function in charge to decide whether an entry has to be purged or
162 * not. This function takes the dat_entry as argument and has to
163 * returns a boolean value: true is the entry has to be deleted,
164 * false otherwise
165 *
Marek Lindner93178012013-03-10 19:29:15 +0800166 * Loops over each entry in the DAT local storage and deletes it if and only if
167 * the to_purge function passed as argument returns true.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200168 */
169static void __batadv_dat_purge(struct batadv_priv *bat_priv,
170 bool (*to_purge)(struct batadv_dat_entry *))
171{
172 spinlock_t *list_lock; /* protects write access to the hash lists */
173 struct batadv_dat_entry *dat_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800174 struct hlist_node *node_tmp;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200175 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200176 u32 i;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200177
178 if (!bat_priv->dat.hash)
179 return;
180
181 for (i = 0; i < bat_priv->dat.hash->size; i++) {
182 head = &bat_priv->dat.hash->table[i];
183 list_lock = &bat_priv->dat.hash->list_locks[i];
184
185 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800186 hlist_for_each_entry_safe(dat_entry, node_tmp, head,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200187 hash_entry) {
Marek Lindner93178012013-03-10 19:29:15 +0800188 /* if a helper function has been passed as parameter,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200189 * ask it if the entry has to be purged or not
190 */
191 if (to_purge && !to_purge(dat_entry))
192 continue;
193
Sasha Levinb67bfe02013-02-27 17:06:00 -0800194 hlist_del_rcu(&dat_entry->hash_entry);
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100195 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200196 }
197 spin_unlock_bh(list_lock);
198 }
199}
200
201/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100202 * batadv_dat_purge() - periodic task that deletes old entries from the local
203 * DAT hash table
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200204 * @work: kernel work struct
205 */
206static void batadv_dat_purge(struct work_struct *work)
207{
208 struct delayed_work *delayed_work;
209 struct batadv_priv_dat *priv_dat;
210 struct batadv_priv *bat_priv;
211
Geliang Tang4ba4bc02015-12-28 23:43:37 +0800212 delayed_work = to_delayed_work(work);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200213 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
214 bat_priv = container_of(priv_dat, struct batadv_priv, dat);
215
216 __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
217 batadv_dat_start_timer(bat_priv);
218}
219
220/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100221 * batadv_compare_dat() - comparing function used in the local DAT hash table
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200222 * @node: node in the local table
223 * @data2: second object to compare the node to
224 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100225 * Return: true if the two entries are the same, false otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200226 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100227static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200228{
229 const void *data1 = container_of(node, struct batadv_dat_entry,
230 hash_entry);
231
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100232 return memcmp(data1, data2, sizeof(__be32)) == 0;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200233}
234
Antonio Quartulli785ea112011-11-23 11:35:44 +0100235/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100236 * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200237 * @skb: ARP packet
238 * @hdr_size: size of the possible header before the ARP packet
239 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200240 * Return: the value of the hw_src field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200241 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200242static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200243{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200244 u8 *addr;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200245
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200246 addr = (u8 *)(skb->data + hdr_size);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200247 addr += ETH_HLEN + sizeof(struct arphdr);
248
249 return addr;
250}
251
252/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100253 * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200254 * @skb: ARP packet
255 * @hdr_size: size of the possible header before the ARP packet
256 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200257 * Return: the value of the ip_src field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200258 */
259static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
260{
261 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
262}
263
264/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100265 * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200266 * @skb: ARP packet
267 * @hdr_size: size of the possible header before the ARP packet
268 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200269 * Return: the value of the hw_dst field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200270 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200271static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200272{
273 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
274}
275
276/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100277 * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200278 * @skb: ARP packet
279 * @hdr_size: size of the possible header before the ARP packet
280 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200281 * Return: the value of the ip_dst field in the ARP packet.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200282 */
283static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
284{
285 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
286}
287
288/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100289 * batadv_hash_dat() - compute the hash value for an IP address
Antonio Quartulli785ea112011-11-23 11:35:44 +0100290 * @data: data to hash
291 * @size: size of the hash table
292 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200293 * Return: the selected index in the hash table for the given data.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100294 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200295static u32 batadv_hash_dat(const void *data, u32 size)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100296{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200297 u32 hash = 0;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200298 const struct batadv_dat_entry *dat = data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100299 const unsigned char *key;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200300 u32 i;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100301
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100302 key = (const unsigned char *)&dat->ip;
303 for (i = 0; i < sizeof(dat->ip); i++) {
304 hash += key[i];
305 hash += (hash << 10);
306 hash ^= (hash >> 6);
307 }
308
309 key = (const unsigned char *)&dat->vid;
310 for (i = 0; i < sizeof(dat->vid); i++) {
311 hash += key[i];
312 hash += (hash << 10);
313 hash ^= (hash >> 6);
314 }
Antonio Quartulli785ea112011-11-23 11:35:44 +0100315
316 hash += (hash << 3);
317 hash ^= (hash >> 11);
318 hash += (hash << 15);
319
320 return hash % size;
321}
322
323/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100324 * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200325 * table
326 * @bat_priv: the bat priv with all the soft interface information
327 * @ip: search key
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200328 * @vid: VLAN identifier
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200329 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200330 * Return: the dat_entry if found, NULL otherwise.
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200331 */
332static struct batadv_dat_entry *
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200333batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
334 unsigned short vid)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200335{
336 struct hlist_head *head;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200337 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200338 struct batadv_hashtable *hash = bat_priv->dat.hash;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200339 u32 index;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200340
341 if (!hash)
342 return NULL;
343
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200344 to_find.ip = ip;
345 to_find.vid = vid;
346
347 index = batadv_hash_dat(&to_find, hash->size);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200348 head = &hash->table[index];
349
350 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800351 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200352 if (dat_entry->ip != ip)
353 continue;
354
Sven Eckelmann68a67222016-01-16 10:29:47 +0100355 if (!kref_get_unless_zero(&dat_entry->refcount))
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200356 continue;
357
358 dat_entry_tmp = dat_entry;
359 break;
360 }
361 rcu_read_unlock();
362
363 return dat_entry_tmp;
364}
365
366/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100367 * batadv_dat_entry_add() - add a new dat entry or update it if already exists
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200368 * @bat_priv: the bat priv with all the soft interface information
369 * @ip: ipv4 to add/edit
370 * @mac_addr: mac address to assign to the given ipv4
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200371 * @vid: VLAN identifier
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200372 */
373static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200374 u8 *mac_addr, unsigned short vid)
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200375{
376 struct batadv_dat_entry *dat_entry;
377 int hash_added;
378
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200379 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200380 /* if this entry is already known, just update it */
381 if (dat_entry) {
382 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100383 ether_addr_copy(dat_entry->mac_addr, mac_addr);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200384 dat_entry->last_update = jiffies;
385 batadv_dbg(BATADV_DBG_DAT, bat_priv,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200386 "Entry updated: %pI4 %pM (vid: %d)\n",
387 &dat_entry->ip, dat_entry->mac_addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100388 batadv_print_vid(vid));
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200389 goto out;
390 }
391
392 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
393 if (!dat_entry)
394 goto out;
395
396 dat_entry->ip = ip;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200397 dat_entry->vid = vid;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100398 ether_addr_copy(dat_entry->mac_addr, mac_addr);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200399 dat_entry->last_update = jiffies;
Sven Eckelmann68a67222016-01-16 10:29:47 +0100400 kref_init(&dat_entry->refcount);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200401
Sven Eckelmann6a51e092016-07-15 17:39:26 +0200402 kref_get(&dat_entry->refcount);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200403 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200404 batadv_hash_dat, dat_entry,
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200405 &dat_entry->hash_entry);
406
407 if (unlikely(hash_added != 0)) {
408 /* remove the reference for the hash */
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100409 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200410 goto out;
411 }
412
Antonio Quartullibe1db4f2013-06-04 12:11:43 +0200413 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100414 &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200415
416out:
417 if (dat_entry)
Sven Eckelmanna6416f92016-01-17 11:01:17 +0100418 batadv_dat_entry_put(dat_entry);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200419}
420
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200421#ifdef CONFIG_BATMAN_ADV_DEBUG
422
423/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100424 * batadv_dbg_arp() - print a debug message containing all the ARP packet
425 * details
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200426 * @bat_priv: the bat priv with all the soft interface information
427 * @skb: ARP packet
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200428 * @hdr_size: size of the possible header before the ARP packet
429 * @msg: message to print together with the debugging information
430 */
431static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
Antonio Quartulli843314c2016-09-25 08:46:36 +0800432 int hdr_size, char *msg)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200433{
434 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
435 struct batadv_bcast_packet *bcast_pkt;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200436 u8 *orig_addr;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200437 __be32 ip_src, ip_dst;
438
439 if (msg)
440 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
441
442 ip_src = batadv_arp_ip_src(skb, hdr_size);
443 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
444 batadv_dbg(BATADV_DBG_DAT, bat_priv,
445 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
446 batadv_arp_hw_src(skb, hdr_size), &ip_src,
447 batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
448
Matthias Schiffer6f27d2c2018-03-16 11:29:10 +0100449 if (hdr_size < sizeof(struct batadv_unicast_packet))
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200450 return;
451
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200452 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
453
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100454 switch (unicast_4addr_packet->u.packet_type) {
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200455 case BATADV_UNICAST:
456 batadv_dbg(BATADV_DBG_DAT, bat_priv,
457 "* encapsulated within a UNICAST packet\n");
458 break;
459 case BATADV_UNICAST_4ADDR:
460 batadv_dbg(BATADV_DBG_DAT, bat_priv,
461 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
462 unicast_4addr_packet->src);
463 switch (unicast_4addr_packet->subtype) {
464 case BATADV_P_DAT_DHT_PUT:
465 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
466 break;
467 case BATADV_P_DAT_DHT_GET:
468 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
469 break;
470 case BATADV_P_DAT_CACHE_REPLY:
471 batadv_dbg(BATADV_DBG_DAT, bat_priv,
472 "* type: DAT_CACHE_REPLY\n");
473 break;
474 case BATADV_P_DATA:
475 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
476 break;
477 default:
478 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100479 unicast_4addr_packet->u.packet_type);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200480 }
481 break;
482 case BATADV_BCAST:
483 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
484 orig_addr = bcast_pkt->orig;
485 batadv_dbg(BATADV_DBG_DAT, bat_priv,
486 "* encapsulated within a BCAST packet (src: %pM)\n",
487 orig_addr);
488 break;
489 default:
490 batadv_dbg(BATADV_DBG_DAT, bat_priv,
491 "* encapsulated within an unknown packet type (0x%x)\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100492 unicast_4addr_packet->u.packet_type);
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200493 }
494}
495
496#else
497
498static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
Antonio Quartulli843314c2016-09-25 08:46:36 +0800499 int hdr_size, char *msg)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200500{
501}
502
503#endif /* CONFIG_BATMAN_ADV_DEBUG */
504
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200505/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100506 * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
Antonio Quartulli785ea112011-11-23 11:35:44 +0100507 * @res: the array with the already selected candidates
508 * @select: number of already selected candidates
509 * @tmp_max: address of the currently evaluated node
510 * @max: current round max address
511 * @last_max: address of the last selected candidate
512 * @candidate: orig_node under evaluation
513 * @max_orig_node: last selected candidate
514 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200515 * Return: true if the node has been elected as next candidate or false
Marek Lindner93178012013-03-10 19:29:15 +0800516 * otherwise.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100517 */
518static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
519 int select, batadv_dat_addr_t tmp_max,
520 batadv_dat_addr_t max,
521 batadv_dat_addr_t last_max,
522 struct batadv_orig_node *candidate,
523 struct batadv_orig_node *max_orig_node)
524{
525 bool ret = false;
526 int j;
527
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800528 /* check if orig node candidate is running DAT */
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200529 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800530 goto out;
531
Antonio Quartulli785ea112011-11-23 11:35:44 +0100532 /* Check if this node has already been selected... */
533 for (j = 0; j < select; j++)
534 if (res[j].orig_node == candidate)
535 break;
536 /* ..and possibly skip it */
537 if (j < select)
538 goto out;
539 /* sanity check: has it already been selected? This should not happen */
540 if (tmp_max > last_max)
541 goto out;
542 /* check if during this iteration an originator with a closer dht
543 * address has already been found
544 */
545 if (tmp_max < max)
546 goto out;
547 /* this is an hash collision with the temporary selected node. Choose
548 * the one with the lowest address
549 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200550 if (tmp_max == max && max_orig_node &&
Sven Eckelmannd7625f92018-02-20 12:08:10 +0100551 batadv_compare_eth(candidate->orig, max_orig_node->orig))
Antonio Quartulli785ea112011-11-23 11:35:44 +0100552 goto out;
553
554 ret = true;
555out:
556 return ret;
557}
558
559/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100560 * batadv_choose_next_candidate() - select the next DHT candidate
Antonio Quartulli785ea112011-11-23 11:35:44 +0100561 * @bat_priv: the bat priv with all the soft interface information
562 * @cands: candidates array
563 * @select: number of candidates already present in the array
564 * @ip_key: key to look up in the DHT
565 * @last_max: pointer where the address of the selected candidate will be saved
566 */
567static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
568 struct batadv_dat_candidate *cands,
569 int select, batadv_dat_addr_t ip_key,
570 batadv_dat_addr_t *last_max)
571{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200572 batadv_dat_addr_t max = 0;
573 batadv_dat_addr_t tmp_max = 0;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100574 struct batadv_orig_node *orig_node, *max_orig_node = NULL;
575 struct batadv_hashtable *hash = bat_priv->orig_hash;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100576 struct hlist_head *head;
577 int i;
578
579 /* if no node is eligible as candidate, leave the candidate type as
580 * NOT_FOUND
581 */
582 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
583
Marek Lindner93178012013-03-10 19:29:15 +0800584 /* iterate over the originator list and find the node with the closest
Antonio Quartulli785ea112011-11-23 11:35:44 +0100585 * dat_address which has not been selected yet
586 */
587 for (i = 0; i < hash->size; i++) {
588 head = &hash->table[i];
589
590 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800591 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Marek Lindner93178012013-03-10 19:29:15 +0800592 /* the dht space is a ring using unsigned addresses */
Antonio Quartulli785ea112011-11-23 11:35:44 +0100593 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
594 ip_key;
595
596 if (!batadv_is_orig_node_eligible(cands, select,
597 tmp_max, max,
598 *last_max, orig_node,
599 max_orig_node))
600 continue;
601
Sven Eckelmann7c124392016-01-16 10:29:56 +0100602 if (!kref_get_unless_zero(&orig_node->refcount))
Antonio Quartulli785ea112011-11-23 11:35:44 +0100603 continue;
604
605 max = tmp_max;
606 if (max_orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100607 batadv_orig_node_put(max_orig_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100608 max_orig_node = orig_node;
609 }
610 rcu_read_unlock();
611 }
612 if (max_orig_node) {
613 cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
614 cands[select].orig_node = max_orig_node;
615 batadv_dbg(BATADV_DBG_DAT, bat_priv,
616 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
617 select, max_orig_node->orig, max_orig_node->dat_addr,
618 max);
619 }
620 *last_max = max;
621}
622
623/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100624 * batadv_dat_select_candidates() - select the nodes which the DHT message has
625 * to be sent to
Antonio Quartulli785ea112011-11-23 11:35:44 +0100626 * @bat_priv: the bat priv with all the soft interface information
627 * @ip_dst: ipv4 to look up in the DHT
Antonio Quartulli28717342016-03-12 11:12:59 +0100628 * @vid: VLAN identifier
Antonio Quartulli785ea112011-11-23 11:35:44 +0100629 *
630 * An originator O is selected if and only if its DHT_ID value is one of three
631 * closest values (from the LEFT, with wrap around if needed) then the hash
632 * value of the key. ip_dst is the key.
633 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200634 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100635 */
636static struct batadv_dat_candidate *
Antonio Quartulli28717342016-03-12 11:12:59 +0100637batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
638 unsigned short vid)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100639{
640 int select;
641 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
642 struct batadv_dat_candidate *res;
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100643 struct batadv_dat_entry dat;
Antonio Quartulli785ea112011-11-23 11:35:44 +0100644
645 if (!bat_priv->orig_hash)
646 return NULL;
647
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200648 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
649 GFP_ATOMIC);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100650 if (!res)
651 return NULL;
652
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100653 dat.ip = ip_dst;
Antonio Quartulli28717342016-03-12 11:12:59 +0100654 dat.vid = vid;
Sven Eckelmannb7fe3d42015-11-03 10:05:44 +0100655 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
Antonio Quartulli785ea112011-11-23 11:35:44 +0100656 BATADV_DAT_ADDR_MAX);
657
658 batadv_dbg(BATADV_DBG_DAT, bat_priv,
Sven Eckelmann22f05022017-05-19 13:02:00 +0200659 "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
Antonio Quartulli785ea112011-11-23 11:35:44 +0100660 ip_key);
661
662 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
663 batadv_choose_next_candidate(bat_priv, res, select, ip_key,
664 &last_max);
665
666 return res;
667}
668
669/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100670 * batadv_dat_send_data() - send a payload to the selected candidates
Antonio Quartulli785ea112011-11-23 11:35:44 +0100671 * @bat_priv: the bat priv with all the soft interface information
672 * @skb: payload to send
673 * @ip: the DHT key
Antonio Quartulli28717342016-03-12 11:12:59 +0100674 * @vid: VLAN identifier
Antonio Quartulli785ea112011-11-23 11:35:44 +0100675 * @packet_subtype: unicast4addr packet subtype to use
676 *
Marek Lindner93178012013-03-10 19:29:15 +0800677 * This function copies the skb with pskb_copy() and is sent as unicast packet
678 * to each of the selected candidates.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100679 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200680 * Return: true if the packet is sent to at least one candidate, false
Marek Lindner93178012013-03-10 19:29:15 +0800681 * otherwise.
Antonio Quartulli785ea112011-11-23 11:35:44 +0100682 */
683static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
684 struct sk_buff *skb, __be32 ip,
Antonio Quartulli28717342016-03-12 11:12:59 +0100685 unsigned short vid, int packet_subtype)
Antonio Quartulli785ea112011-11-23 11:35:44 +0100686{
687 int i;
688 bool ret = false;
689 int send_status;
690 struct batadv_neigh_node *neigh_node = NULL;
691 struct sk_buff *tmp_skb;
692 struct batadv_dat_candidate *cand;
693
Antonio Quartulli28717342016-03-12 11:12:59 +0100694 cand = batadv_dat_select_candidates(bat_priv, ip, vid);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100695 if (!cand)
696 goto out;
697
698 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
699
700 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
701 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
702 continue;
703
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100704 neigh_node = batadv_orig_router_get(cand[i].orig_node,
705 BATADV_IF_DEFAULT);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100706 if (!neigh_node)
707 goto free_orig;
708
Octavian Purdilabad93e92014-06-12 01:36:26 +0300709 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
Martin Hundebøllf097e252013-05-23 16:53:01 +0200710 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
711 cand[i].orig_node,
712 packet_subtype)) {
Antonio Quartulli785ea112011-11-23 11:35:44 +0100713 kfree_skb(tmp_skb);
714 goto free_neigh;
715 }
716
Antonio Quartulli95d39272016-01-16 16:40:15 +0800717 send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
Martin Hundebøll4046b242012-04-20 17:02:45 +0200718 if (send_status == NET_XMIT_SUCCESS) {
719 /* count the sent packet */
720 switch (packet_subtype) {
721 case BATADV_P_DAT_DHT_GET:
722 batadv_inc_counter(bat_priv,
723 BATADV_CNT_DAT_GET_TX);
724 break;
725 case BATADV_P_DAT_DHT_PUT:
726 batadv_inc_counter(bat_priv,
727 BATADV_CNT_DAT_PUT_TX);
728 break;
729 }
730
Antonio Quartulli785ea112011-11-23 11:35:44 +0100731 /* packet sent to a candidate: return true */
732 ret = true;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200733 }
Antonio Quartulli785ea112011-11-23 11:35:44 +0100734free_neigh:
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100735 batadv_neigh_node_put(neigh_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100736free_orig:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100737 batadv_orig_node_put(cand[i].orig_node);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100738 }
739
740out:
741 kfree(cand);
742 return ret;
743}
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200744
745/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100746 * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800747 * setting change
748 * @bat_priv: the bat priv with all the soft interface information
749 */
750static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
751{
752 char dat_mode;
753
754 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
755
756 switch (dat_mode) {
757 case 0:
758 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
759 break;
760 case 1:
761 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
762 NULL, 0);
763 break;
764 }
765}
766
767/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100768 * batadv_dat_status_update() - update the dat tvlv container after dat
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800769 * setting change
770 * @net_dev: the soft interface net device
771 */
772void batadv_dat_status_update(struct net_device *net_dev)
773{
774 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200775
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800776 batadv_dat_tvlv_container_update(bat_priv);
777}
778
779/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100780 * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800781 * @bat_priv: the bat priv with all the soft interface information
782 * @orig: the orig_node of the ogm
783 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
784 * @tvlv_value: tvlv buffer containing the gateway data
785 * @tvlv_value_len: tvlv buffer length
786 */
787static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
788 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200789 u8 flags,
790 void *tvlv_value, u16 tvlv_value_len)
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800791{
792 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200793 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800794 else
Linus Lüssing65d7d4602015-06-16 17:10:22 +0200795 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800796}
797
798/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100799 * batadv_dat_hash_free() - free the local DAT hash table
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200800 * @bat_priv: the bat priv with all the soft interface information
801 */
802static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
803{
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200804 if (!bat_priv->dat.hash)
805 return;
806
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200807 __batadv_dat_purge(bat_priv, NULL);
808
809 batadv_hash_destroy(bat_priv->dat.hash);
810
811 bat_priv->dat.hash = NULL;
812}
813
814/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100815 * batadv_dat_init() - initialise the DAT internals
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200816 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullife13c2a2015-11-17 16:40:51 +0800817 *
818 * Return: 0 in case of success, a negative error code otherwise
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200819 */
820int batadv_dat_init(struct batadv_priv *bat_priv)
821{
822 if (bat_priv->dat.hash)
823 return 0;
824
825 bat_priv->dat.hash = batadv_hash_new(1024);
826
827 if (!bat_priv->dat.hash)
828 return -ENOMEM;
829
830 batadv_dat_start_timer(bat_priv);
831
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800832 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
833 NULL, BATADV_TVLV_DAT, 1,
834 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
835 batadv_dat_tvlv_container_update(bat_priv);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200836 return 0;
837}
838
839/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100840 * batadv_dat_free() - free the DAT internals
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200841 * @bat_priv: the bat priv with all the soft interface information
842 */
843void batadv_dat_free(struct batadv_priv *bat_priv)
844{
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800845 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
846 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
847
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200848 cancel_delayed_work_sync(&bat_priv->dat.work);
849
850 batadv_dat_hash_free(bat_priv);
851}
852
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200853#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200854/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100855 * batadv_dat_cache_seq_print_text() - print the local DAT hash table
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200856 * @seq: seq file to print on
857 * @offset: not used
Antonio Quartullife13c2a2015-11-17 16:40:51 +0800858 *
859 * Return: always 0
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200860 */
861int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
862{
863 struct net_device *net_dev = (struct net_device *)seq->private;
864 struct batadv_priv *bat_priv = netdev_priv(net_dev);
865 struct batadv_hashtable *hash = bat_priv->dat.hash;
866 struct batadv_dat_entry *dat_entry;
867 struct batadv_hard_iface *primary_if;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200868 struct hlist_head *head;
869 unsigned long last_seen_jiffies;
870 int last_seen_msecs, last_seen_secs, last_seen_mins;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200871 u32 i;
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200872
873 primary_if = batadv_seq_print_text_primary_if_get(seq);
874 if (!primary_if)
875 goto out;
876
877 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
Antonio Quartulli925a6f32016-03-12 10:30:18 +0100878 seq_puts(seq,
879 " IPv4 MAC VID last-seen\n");
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200880
881 for (i = 0; i < hash->size; i++) {
882 head = &hash->table[i];
883
884 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800885 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200886 last_seen_jiffies = jiffies - dat_entry->last_update;
887 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
888 last_seen_mins = last_seen_msecs / 60000;
889 last_seen_msecs = last_seen_msecs % 60000;
890 last_seen_secs = last_seen_msecs / 1000;
891
Joe Perchescd0edf3a2017-06-14 02:33:52 -0700892 seq_printf(seq, " * %15pI4 %pM %4i %6i:%02i\n",
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200893 &dat_entry->ip, dat_entry->mac_addr,
Sven Eckelmannf7a2bd62017-02-22 17:16:42 +0100894 batadv_print_vid(dat_entry->vid),
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200895 last_seen_mins, last_seen_secs);
896 }
897 rcu_read_unlock();
898 }
899
900out:
901 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100902 batadv_hardif_put(primary_if);
Antonio Quartulli2f1dfbe2012-06-30 20:01:19 +0200903 return 0;
904}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200905#endif
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +0200906
907/**
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100908 * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
909 * netlink socket
910 * @msg: buffer for the message
911 * @portid: netlink port
Sven Eckelmann6f816522018-10-30 22:01:28 +0100912 * @cb: Control block containing additional options
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100913 * @dat_entry: entry to dump
914 *
915 * Return: 0 or error code.
916 */
917static int
Sven Eckelmann6f816522018-10-30 22:01:28 +0100918batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
919 struct netlink_callback *cb,
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100920 struct batadv_dat_entry *dat_entry)
921{
922 int msecs;
923 void *hdr;
924
Sven Eckelmann6f816522018-10-30 22:01:28 +0100925 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
926 &batadv_netlink_family, NLM_F_MULTI,
927 BATADV_CMD_GET_DAT_CACHE);
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100928 if (!hdr)
929 return -ENOBUFS;
930
Sven Eckelmann6f816522018-10-30 22:01:28 +0100931 genl_dump_check_consistent(cb, hdr);
932
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100933 msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
934
935 if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
936 dat_entry->ip) ||
937 nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
938 dat_entry->mac_addr) ||
939 nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
940 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
941 genlmsg_cancel(msg, hdr);
942 return -EMSGSIZE;
943 }
944
945 genlmsg_end(msg, hdr);
946 return 0;
947}
948
949/**
950 * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
951 * a netlink socket
952 * @msg: buffer for the message
953 * @portid: netlink port
Sven Eckelmann6f816522018-10-30 22:01:28 +0100954 * @cb: Control block containing additional options
955 * @hash: hash to dump
956 * @bucket: bucket index to dump
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100957 * @idx_skip: How many entries to skip
958 *
959 * Return: 0 or error code.
960 */
961static int
Sven Eckelmann6f816522018-10-30 22:01:28 +0100962batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
963 struct netlink_callback *cb,
964 struct batadv_hashtable *hash, unsigned int bucket,
965 int *idx_skip)
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100966{
967 struct batadv_dat_entry *dat_entry;
968 int idx = 0;
969
Sven Eckelmann6f816522018-10-30 22:01:28 +0100970 spin_lock_bh(&hash->list_locks[bucket]);
971 cb->seq = atomic_read(&hash->generation) << 1 | 1;
972
973 hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100974 if (idx < *idx_skip)
975 goto skip;
976
Sven Eckelmann6f816522018-10-30 22:01:28 +0100977 if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
978 spin_unlock_bh(&hash->list_locks[bucket]);
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100979 *idx_skip = idx;
980
981 return -EMSGSIZE;
982 }
983
984skip:
985 idx++;
986 }
Sven Eckelmann6f816522018-10-30 22:01:28 +0100987 spin_unlock_bh(&hash->list_locks[bucket]);
Linus Lüssing41aeefc2018-03-13 11:41:12 +0100988
989 return 0;
990}
991
992/**
993 * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
994 * @msg: buffer for the message
995 * @cb: callback structure containing arguments
996 *
997 * Return: message length.
998 */
999int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
1000{
1001 struct batadv_hard_iface *primary_if = NULL;
1002 int portid = NETLINK_CB(cb->skb).portid;
1003 struct net *net = sock_net(cb->skb->sk);
1004 struct net_device *soft_iface;
1005 struct batadv_hashtable *hash;
1006 struct batadv_priv *bat_priv;
1007 int bucket = cb->args[0];
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001008 int idx = cb->args[1];
1009 int ifindex;
1010 int ret = 0;
1011
1012 ifindex = batadv_netlink_get_ifindex(cb->nlh,
1013 BATADV_ATTR_MESH_IFINDEX);
1014 if (!ifindex)
1015 return -EINVAL;
1016
1017 soft_iface = dev_get_by_index(net, ifindex);
1018 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1019 ret = -ENODEV;
1020 goto out;
1021 }
1022
1023 bat_priv = netdev_priv(soft_iface);
1024 hash = bat_priv->dat.hash;
1025
1026 primary_if = batadv_primary_if_get_selected(bat_priv);
1027 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1028 ret = -ENOENT;
1029 goto out;
1030 }
1031
1032 while (bucket < hash->size) {
Sven Eckelmann6f816522018-10-30 22:01:28 +01001033 if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
Linus Lüssing41aeefc2018-03-13 11:41:12 +01001034 &idx))
1035 break;
1036
1037 bucket++;
1038 idx = 0;
1039 }
1040
1041 cb->args[0] = bucket;
1042 cb->args[1] = idx;
1043
1044 ret = msg->len;
1045
1046out:
1047 if (primary_if)
1048 batadv_hardif_put(primary_if);
1049
1050 if (soft_iface)
1051 dev_put(soft_iface);
1052
1053 return ret;
1054}
1055
1056/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001057 * batadv_arp_get_type() - parse an ARP packet and gets the type
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001058 * @bat_priv: the bat priv with all the soft interface information
1059 * @skb: packet to analyse
1060 * @hdr_size: size of the possible header before the ARP packet in the skb
1061 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001062 * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001063 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001064static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1065 struct sk_buff *skb, int hdr_size)
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001066{
1067 struct arphdr *arphdr;
1068 struct ethhdr *ethhdr;
1069 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001070 u8 *hw_src, *hw_dst;
1071 u16 type = 0;
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001072
1073 /* pull the ethernet header */
1074 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1075 goto out;
1076
1077 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1078
1079 if (ethhdr->h_proto != htons(ETH_P_ARP))
1080 goto out;
1081
1082 /* pull the ARP payload */
1083 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1084 arp_hdr_len(skb->dev))))
1085 goto out;
1086
1087 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1088
Marek Lindner93178012013-03-10 19:29:15 +08001089 /* check whether the ARP packet carries a valid IP information */
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001090 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1091 goto out;
1092
1093 if (arphdr->ar_pro != htons(ETH_P_IP))
1094 goto out;
1095
1096 if (arphdr->ar_hln != ETH_ALEN)
1097 goto out;
1098
1099 if (arphdr->ar_pln != 4)
1100 goto out;
1101
1102 /* Check for bad reply/request. If the ARP message is not sane, DAT
1103 * will simply ignore it
1104 */
1105 ip_src = batadv_arp_ip_src(skb, hdr_size);
1106 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1107 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
Matthias Schiffer757dd822013-01-24 18:18:26 +01001108 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
1109 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
1110 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001111 goto out;
1112
Matthias Schifferb618ad12013-01-24 18:18:27 +01001113 hw_src = batadv_arp_hw_src(skb, hdr_size);
1114 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1115 goto out;
1116
Marek Lindner93178012013-03-10 19:29:15 +08001117 /* don't care about the destination MAC address in ARP requests */
Matthias Schifferb618ad12013-01-24 18:18:27 +01001118 if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
1119 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1120 if (is_zero_ether_addr(hw_dst) ||
1121 is_multicast_ether_addr(hw_dst))
1122 goto out;
1123 }
1124
Antonio Quartulli5c3a0e52011-06-02 12:29:51 +02001125 type = ntohs(arphdr->ar_op);
1126out:
1127 return type;
1128}
Antonio Quartullic384ea32011-06-26 03:37:18 +02001129
1130/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001131 * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001132 * @skb: the buffer containing the packet to extract the VID from
1133 * @hdr_size: the size of the batman-adv header encapsulating the packet
1134 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001135 * Return: If the packet embedded in the skb is vlan tagged this function
1136 * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1137 * is returned.
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001138 */
1139static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1140{
1141 unsigned short vid;
1142
1143 vid = batadv_get_vid(skb, *hdr_size);
1144
1145 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1146 * If the header contained in the packet is a VLAN one (which is longer)
1147 * hdr_size is updated so that the functions will still skip the
1148 * correct amount of bytes.
1149 */
1150 if (vid & BATADV_VLAN_HAS_TAG)
1151 *hdr_size += VLAN_HLEN;
1152
1153 return vid;
1154}
1155
1156/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001157 * batadv_dat_arp_create_reply() - create an ARP Reply
Linus Lüssingcbfc16d2016-07-05 20:01:36 +02001158 * @bat_priv: the bat priv with all the soft interface information
1159 * @ip_src: ARP sender IP
1160 * @ip_dst: ARP target IP
1161 * @hw_src: Ethernet source and ARP sender MAC
1162 * @hw_dst: Ethernet destination and ARP target MAC
1163 * @vid: VLAN identifier (optional, set to zero otherwise)
1164 *
1165 * Creates an ARP Reply from the given values, optionally encapsulated in a
1166 * VLAN header.
1167 *
1168 * Return: An skb containing an ARP Reply.
1169 */
1170static struct sk_buff *
1171batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1172 __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1173 unsigned short vid)
1174{
1175 struct sk_buff *skb;
1176
1177 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
1178 ip_src, hw_dst, hw_src, hw_dst);
1179 if (!skb)
1180 return NULL;
1181
1182 skb_reset_mac_header(skb);
1183
1184 if (vid & BATADV_VLAN_HAS_TAG)
1185 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1186 vid & VLAN_VID_MASK);
1187
1188 return skb;
1189}
1190
1191/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001192 * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
Antonio Quartullic384ea32011-06-26 03:37:18 +02001193 * answer using DAT
1194 * @bat_priv: the bat priv with all the soft interface information
1195 * @skb: packet to check
1196 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001197 * Return: true if the message has been sent to the dht candidates, false
Marek Lindner93178012013-03-10 19:29:15 +08001198 * otherwise. In case of a positive return value the message has to be enqueued
1199 * to permit the fallback.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001200 */
1201bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1202 struct sk_buff *skb)
1203{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001204 u16 type = 0;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001205 __be32 ip_dst, ip_src;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001206 u8 *hw_src;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001207 bool ret = false;
1208 struct batadv_dat_entry *dat_entry = NULL;
1209 struct sk_buff *skb_new;
Tobias Klauserab044f82017-04-05 13:46:31 +02001210 struct net_device *soft_iface = bat_priv->soft_iface;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001211 int hdr_size = 0;
1212 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001213
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001214 if (!atomic_read(&bat_priv->distributed_arp_table))
1215 goto out;
1216
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001217 vid = batadv_dat_get_vid(skb, &hdr_size);
1218
1219 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001220 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1221 * message to the selected DHT candidates
1222 */
1223 if (type != ARPOP_REQUEST)
1224 goto out;
1225
Antonio Quartulli843314c2016-09-25 08:46:36 +08001226 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001227
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001228 ip_src = batadv_arp_ip_src(skb, hdr_size);
1229 hw_src = batadv_arp_hw_src(skb, hdr_size);
1230 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001231
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001232 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001233
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001234 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001235 if (dat_entry) {
Antonio Quartulli88e48d72013-05-09 09:35:45 +02001236 /* If the ARP request is destined for a local client the local
1237 * client will answer itself. DAT would only generate a
1238 * duplicate packet.
1239 *
1240 * Moreover, if the soft-interface is enslaved into a bridge, an
1241 * additional DAT answer may trigger kernel warnings about
1242 * a packet coming from the wrong port.
1243 */
Antonio Quartullicc2f3382014-03-29 17:27:38 +01001244 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
Antonio Quartulli88e48d72013-05-09 09:35:45 +02001245 ret = true;
1246 goto out;
1247 }
1248
Andreas Pape00311de2016-09-05 13:20:25 +02001249 /* If BLA is enabled, only send ARP replies if we have claimed
1250 * the destination for the ARP request or if no one else of
1251 * the backbone gws belonging to our backbone has claimed the
1252 * destination.
1253 */
1254 if (!batadv_bla_check_claim(bat_priv,
1255 dat_entry->mac_addr, vid)) {
1256 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1257 "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1258 dat_entry->mac_addr);
1259 ret = true;
1260 goto out;
1261 }
1262
Linus Lüssingcbfc16d2016-07-05 20:01:36 +02001263 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1264 dat_entry->mac_addr,
1265 hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001266 if (!skb_new)
1267 goto out;
1268
Tobias Klauserab044f82017-04-05 13:46:31 +02001269 skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1270
Sven Eckelmann36d4d682017-04-05 16:26:17 +02001271 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1272 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1273 skb->len + ETH_HLEN + hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001274
1275 netif_rx(skb_new);
1276 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1277 ret = true;
1278 } else {
Marek Lindner93178012013-03-10 19:29:15 +08001279 /* Send the request to the DHT */
Antonio Quartulli28717342016-03-12 11:12:59 +01001280 ret = batadv_dat_send_data(bat_priv, skb, ip_dst, vid,
Antonio Quartullic384ea32011-06-26 03:37:18 +02001281 BATADV_P_DAT_DHT_GET);
1282 }
1283out:
1284 if (dat_entry)
Sven Eckelmanna6416f92016-01-17 11:01:17 +01001285 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001286 return ret;
1287}
1288
1289/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001290 * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
Antonio Quartullic384ea32011-06-26 03:37:18 +02001291 * answer using the local DAT storage
1292 * @bat_priv: the bat priv with all the soft interface information
1293 * @skb: packet to check
1294 * @hdr_size: size of the encapsulation header
1295 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001296 * Return: true if the request has been answered, false otherwise.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001297 */
1298bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1299 struct sk_buff *skb, int hdr_size)
1300{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001301 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001302 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001303 u8 *hw_src;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001304 struct sk_buff *skb_new;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001305 struct batadv_dat_entry *dat_entry = NULL;
1306 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001307 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001308 int err;
1309
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001310 if (!atomic_read(&bat_priv->distributed_arp_table))
1311 goto out;
1312
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001313 vid = batadv_dat_get_vid(skb, &hdr_size);
1314
Antonio Quartullic384ea32011-06-26 03:37:18 +02001315 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1316 if (type != ARPOP_REQUEST)
1317 goto out;
1318
1319 hw_src = batadv_arp_hw_src(skb, hdr_size);
1320 ip_src = batadv_arp_ip_src(skb, hdr_size);
1321 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1322
Antonio Quartulli843314c2016-09-25 08:46:36 +08001323 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001324
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001325 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001326
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001327 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001328 if (!dat_entry)
1329 goto out;
1330
Linus Lüssingcbfc16d2016-07-05 20:01:36 +02001331 skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1332 dat_entry->mac_addr, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001333 if (!skb_new)
1334 goto out;
1335
Marek Lindner93178012013-03-10 19:29:15 +08001336 /* To preserve backwards compatibility, the node has choose the outgoing
1337 * format based on the incoming request packet type. The assumption is
1338 * that a node not using the 4addr packet format doesn't support it.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001339 */
1340 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
Linus Lüssinge300d312013-07-03 10:40:00 +02001341 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1342 BATADV_P_DAT_CACHE_REPLY,
Antonio Quartulli6c413b12013-11-05 19:31:08 +01001343 NULL, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001344 else
Antonio Quartulli6c413b12013-11-05 19:31:08 +01001345 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001346
Linus Lüssinge300d312013-07-03 10:40:00 +02001347 if (err != NET_XMIT_DROP) {
Martin Hundebøll4046b242012-04-20 17:02:45 +02001348 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001349 ret = true;
Martin Hundebøll4046b242012-04-20 17:02:45 +02001350 }
Antonio Quartullic384ea32011-06-26 03:37:18 +02001351out:
1352 if (dat_entry)
Sven Eckelmanna6416f92016-01-17 11:01:17 +01001353 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001354 if (ret)
1355 kfree_skb(skb);
1356 return ret;
1357}
1358
1359/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001360 * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
Antonio Quartullic384ea32011-06-26 03:37:18 +02001361 * @bat_priv: the bat priv with all the soft interface information
1362 * @skb: packet to check
1363 */
1364void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1365 struct sk_buff *skb)
1366{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001367 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001368 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001369 u8 *hw_src, *hw_dst;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001370 int hdr_size = 0;
1371 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001372
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001373 if (!atomic_read(&bat_priv->distributed_arp_table))
1374 return;
1375
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001376 vid = batadv_dat_get_vid(skb, &hdr_size);
1377
1378 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001379 if (type != ARPOP_REPLY)
1380 return;
1381
Antonio Quartulli843314c2016-09-25 08:46:36 +08001382 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001383
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001384 hw_src = batadv_arp_hw_src(skb, hdr_size);
1385 ip_src = batadv_arp_ip_src(skb, hdr_size);
1386 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1387 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001388
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001389 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1390 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001391
1392 /* Send the ARP reply to the candidates for both the IP addresses that
Marek Lindner93178012013-03-10 19:29:15 +08001393 * the node obtained from the ARP reply
Antonio Quartullic384ea32011-06-26 03:37:18 +02001394 */
Antonio Quartulli28717342016-03-12 11:12:59 +01001395 batadv_dat_send_data(bat_priv, skb, ip_src, vid, BATADV_P_DAT_DHT_PUT);
1396 batadv_dat_send_data(bat_priv, skb, ip_dst, vid, BATADV_P_DAT_DHT_PUT);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001397}
Antonio Quartulliaa143d22014-09-01 14:37:27 +02001398
Antonio Quartullic384ea32011-06-26 03:37:18 +02001399/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001400 * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1401 * local DAT storage only
Antonio Quartullic384ea32011-06-26 03:37:18 +02001402 * @bat_priv: the bat priv with all the soft interface information
1403 * @skb: packet to check
Marek Lindner93178012013-03-10 19:29:15 +08001404 * @hdr_size: size of the encapsulation header
Antonio Quartullif202a662015-06-16 21:06:24 +02001405 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001406 * Return: true if the packet was snooped and consumed by DAT. False if the
Antonio Quartullif202a662015-06-16 21:06:24 +02001407 * packet has to be delivered to the interface
Antonio Quartullic384ea32011-06-26 03:37:18 +02001408 */
1409bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1410 struct sk_buff *skb, int hdr_size)
1411{
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001412 struct batadv_dat_entry *dat_entry = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001413 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001414 __be32 ip_src, ip_dst;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001415 u8 *hw_src, *hw_dst;
Antonio Quartullif202a662015-06-16 21:06:24 +02001416 bool dropped = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001417 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001418
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001419 if (!atomic_read(&bat_priv->distributed_arp_table))
1420 goto out;
1421
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001422 vid = batadv_dat_get_vid(skb, &hdr_size);
1423
Antonio Quartullic384ea32011-06-26 03:37:18 +02001424 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1425 if (type != ARPOP_REPLY)
1426 goto out;
1427
Antonio Quartulli843314c2016-09-25 08:46:36 +08001428 batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
Antonio Quartullic384ea32011-06-26 03:37:18 +02001429
1430 hw_src = batadv_arp_hw_src(skb, hdr_size);
1431 ip_src = batadv_arp_ip_src(skb, hdr_size);
1432 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1433 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1434
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001435 /* If ip_dst is already in cache and has the right mac address,
1436 * drop this frame if this ARP reply is destined for us because it's
1437 * most probably an ARP reply generated by another node of the DHT.
1438 * We have most probably received already a reply earlier. Delivering
1439 * this frame would lead to doubled receive of an ARP reply.
1440 */
1441 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1442 if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
1443 batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1444 hw_src, &ip_src, hw_dst, &ip_dst,
1445 dat_entry->mac_addr, &dat_entry->ip);
1446 dropped = true;
1447 goto out;
1448 }
1449
Antonio Quartullic384ea32011-06-26 03:37:18 +02001450 /* Update our internal cache with both the IP addresses the node got
1451 * within the ARP reply
1452 */
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001453 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1454 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001455
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001456 /* If BLA is enabled, only forward ARP replies if we have claimed the
1457 * source of the ARP reply or if no one else of the same backbone has
1458 * already claimed that client. This prevents that different gateways
1459 * to the same backbone all forward the ARP reply leading to multiple
1460 * replies in the backbone.
1461 */
1462 if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1463 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1464 "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1465 hw_src);
1466 dropped = true;
1467 goto out;
1468 }
1469
Antonio Quartullic384ea32011-06-26 03:37:18 +02001470 /* if this REPLY is directed to a client of mine, let's deliver the
1471 * packet to the interface
1472 */
Antonio Quartullif202a662015-06-16 21:06:24 +02001473 dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1474
1475 /* if this REPLY is sent on behalf of a client of mine, let's drop the
1476 * packet because the client will reply by itself
1477 */
1478 dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001479out:
Antonio Quartullif202a662015-06-16 21:06:24 +02001480 if (dropped)
Matthias Schiffer0d15bec2013-01-23 18:11:53 +01001481 kfree_skb(skb);
Andreas Pape9aa5cd72016-09-05 13:20:26 +02001482 if (dat_entry)
1483 batadv_dat_entry_put(dat_entry);
Antonio Quartullif202a662015-06-16 21:06:24 +02001484 /* if dropped == false -> deliver to the interface */
1485 return dropped;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001486}
1487
1488/**
Linus Lüssingb61ec312018-12-30 16:52:53 +01001489 * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
1490 * @skb: the packet to check
1491 * @ip_src: a buffer to store the IPv4 source address in
1492 *
1493 * Checks whether the given skb has an IP and UDP header valid for a DHCP
1494 * message from a DHCP server. And if so, stores the IPv4 source address in
1495 * the provided buffer.
1496 *
1497 * Return: True if valid, false otherwise.
1498 */
1499static bool
1500batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
1501{
1502 unsigned int offset = skb_network_offset(skb);
1503 struct udphdr *udphdr, _udphdr;
1504 struct iphdr *iphdr, _iphdr;
1505
1506 iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
1507 if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
1508 return false;
1509
1510 if (iphdr->protocol != IPPROTO_UDP)
1511 return false;
1512
1513 offset += iphdr->ihl * 4;
1514 skb_set_transport_header(skb, offset);
1515
1516 udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
1517 if (!udphdr || udphdr->source != htons(67))
1518 return false;
1519
1520 *ip_src = get_unaligned(&iphdr->saddr);
1521
1522 return true;
1523}
1524
1525/**
1526 * batadv_dat_check_dhcp() - examine packet for valid DHCP message
1527 * @skb: the packet to check
1528 * @proto: ethernet protocol hint (behind a potential vlan)
1529 * @ip_src: a buffer to store the IPv4 source address in
1530 *
1531 * Checks whether the given skb is a valid DHCP packet. And if so, stores the
1532 * IPv4 source address in the provided buffer.
1533 *
1534 * Caller needs to ensure that the skb network header is set correctly.
1535 *
1536 * Return: If skb is a valid DHCP packet, then returns its op code
1537 * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
1538 */
1539static int
1540batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
1541{
1542 __be32 *magic, _magic;
1543 unsigned int offset;
1544 struct {
1545 __u8 op;
1546 __u8 htype;
1547 __u8 hlen;
1548 __u8 hops;
1549 } *dhcp_h, _dhcp_h;
1550
1551 if (proto != htons(ETH_P_IP))
1552 return -EINVAL;
1553
1554 if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
1555 return -EINVAL;
1556
1557 offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1558 if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
1559 return -EINVAL;
1560
1561 dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
1562 if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
1563 dhcp_h->hlen != ETH_ALEN)
1564 return -EINVAL;
1565
1566 offset += offsetof(struct batadv_dhcp_packet, magic);
1567
1568 magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
1569 if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
1570 return -EINVAL;
1571
1572 return dhcp_h->op;
1573}
1574
1575/**
1576 * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
1577 * @skb: the DHCP packet to parse
1578 *
1579 * Iterates over the DHCP options of the given DHCP packet to find a
1580 * DHCP Message Type option and parse it.
1581 *
1582 * Caller needs to ensure that the given skb is a valid DHCP packet and
1583 * that the skb transport header is set correctly.
1584 *
1585 * Return: The found DHCP message type value, if found. -EINVAL otherwise.
1586 */
1587static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
1588{
1589 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1590 u8 *type, _type;
1591 struct {
1592 u8 type;
1593 u8 len;
1594 } *tl, _tl;
1595
1596 offset += sizeof(struct batadv_dhcp_packet);
1597
1598 while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
1599 if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
1600 break;
1601
1602 if (tl->type == BATADV_DHCP_OPT_END)
1603 break;
1604
1605 if (tl->type == BATADV_DHCP_OPT_PAD)
1606 offset++;
1607 else
1608 offset += tl->len + sizeof(_tl);
1609 }
1610
1611 /* Option Overload Code not supported */
1612 if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
1613 tl->len != sizeof(_type))
1614 return -EINVAL;
1615
1616 offset += sizeof(_tl);
1617
1618 type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
1619 if (!type)
1620 return -EINVAL;
1621
1622 return *type;
1623}
1624
1625/**
1626 * batadv_dat_get_dhcp_yiaddr() - get yiaddr from a DHCP packet
1627 * @skb: the DHCP packet to parse
1628 * @buf: a buffer to store the yiaddr in
1629 *
1630 * Caller needs to ensure that the given skb is a valid DHCP packet and
1631 * that the skb transport header is set correctly.
1632 *
1633 * Return: True on success, false otherwise.
1634 */
1635static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
1636{
1637 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1638 __be32 *yiaddr;
1639
1640 offset += offsetof(struct batadv_dhcp_packet, yiaddr);
1641 yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
1642
1643 if (!yiaddr)
1644 return false;
1645
1646 if (yiaddr != buf)
1647 *buf = get_unaligned(yiaddr);
1648
1649 return true;
1650}
1651
1652/**
1653 * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
1654 * @skb: the DHCP packet to parse
1655 * @buf: a buffer to store the chaddr in
1656 *
1657 * Caller needs to ensure that the given skb is a valid DHCP packet and
1658 * that the skb transport header is set correctly.
1659 *
1660 * Return: True on success, false otherwise
1661 */
1662static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
1663{
1664 unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1665 u8 *chaddr;
1666
1667 offset += offsetof(struct batadv_dhcp_packet, chaddr);
1668 chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
1669
1670 if (!chaddr)
1671 return false;
1672
1673 if (chaddr != buf)
1674 memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
1675
1676 return true;
1677}
1678
1679/**
1680 * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
1681 * DAT cache
1682 * @bat_priv: the bat priv with all the soft interface information
1683 * @chaddr: the DHCP client MAC address
1684 * @yiaddr: the DHCP client IP address
1685 * @hw_dst: the DHCP server MAC address
1686 * @ip_dst: the DHCP server IP address
1687 * @vid: VLAN identifier
1688 *
1689 * Adds given MAC/IP pairs to the local DAT cache and propagates them further
1690 * into the DHT.
1691 *
1692 * For the DHT propagation, client MAC + IP will appear as the ARP Reply
1693 * transmitter (and hw_dst/ip_dst as the target).
1694 */
1695static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
1696 __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
1697 unsigned short vid)
1698{
1699 struct sk_buff *skb;
1700
1701 skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
1702 hw_dst, vid);
1703 if (!skb)
1704 return;
1705
1706 skb_set_network_header(skb, ETH_HLEN);
1707
1708 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1709 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1710
1711 batadv_dat_send_data(bat_priv, skb, yiaddr, vid, BATADV_P_DAT_DHT_PUT);
1712 batadv_dat_send_data(bat_priv, skb, ip_dst, vid, BATADV_P_DAT_DHT_PUT);
1713
Martin Weinelt5f320f02019-02-06 15:39:14 +01001714 consume_skb(skb);
1715
Linus Lüssingb61ec312018-12-30 16:52:53 +01001716 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1717 "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1718 &ip_dst, hw_dst, batadv_print_vid(vid));
1719 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1720 "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1721 &yiaddr, chaddr, batadv_print_vid(vid));
1722}
1723
1724/**
1725 * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
1726 * @skb: the packet to check
1727 * @proto: ethernet protocol hint (behind a potential vlan)
1728 * @ip_src: a buffer to store the IPv4 source address in
1729 * @chaddr: a buffer to store the DHCP Client Hardware Address in
1730 * @yiaddr: a buffer to store the DHCP Your IP Address in
1731 *
1732 * Checks whether the given skb is a valid DHCPACK. And if so, stores the
1733 * IPv4 server source address (ip_src), client MAC address (chaddr) and client
1734 * IPv4 address (yiaddr) in the provided buffers.
1735 *
1736 * Caller needs to ensure that the skb network header is set correctly.
1737 *
1738 * Return: True if the skb is a valid DHCPACK. False otherwise.
1739 */
1740static bool
1741batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
1742 u8 *chaddr, __be32 *yiaddr)
1743{
1744 int type;
1745
1746 type = batadv_dat_check_dhcp(skb, proto, ip_src);
1747 if (type != BATADV_BOOTREPLY)
1748 return false;
1749
1750 type = batadv_dat_get_dhcp_message_type(skb);
1751 if (type != BATADV_DHCPACK)
1752 return false;
1753
1754 if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
1755 return false;
1756
1757 if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
1758 return false;
1759
1760 return true;
1761}
1762
1763/**
1764 * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
1765 * @bat_priv: the bat priv with all the soft interface information
1766 * @skb: the packet to snoop
1767 * @proto: ethernet protocol hint (behind a potential vlan)
1768 * @vid: VLAN identifier
1769 *
1770 * This function first checks whether the given skb is a valid DHCPACK. If
1771 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1772 * field and DHCP Your IP Address field are added to the local DAT cache and
1773 * propagated into the DHT.
1774 *
1775 * Caller needs to ensure that the skb mac and network headers are set
1776 * correctly.
1777 */
1778void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
1779 struct sk_buff *skb,
1780 __be16 proto,
1781 unsigned short vid)
1782{
1783 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1784 __be32 ip_src, yiaddr;
1785
1786 if (!atomic_read(&bat_priv->distributed_arp_table))
1787 return;
1788
1789 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1790 return;
1791
1792 batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
1793 ip_src, vid);
1794}
1795
1796/**
1797 * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
1798 * @bat_priv: the bat priv with all the soft interface information
1799 * @skb: the packet to snoop
1800 * @hdr_size: header size, up to the tail of the batman-adv header
1801 *
1802 * This function first checks whether the given skb is a valid DHCPACK. If
1803 * so then its source MAC and IP as well as its DHCP Client Hardware Address
1804 * field and DHCP Your IP Address field are added to the local DAT cache.
1805 */
1806void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
1807 struct sk_buff *skb, int hdr_size)
1808{
1809 u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1810 struct ethhdr *ethhdr;
1811 __be32 ip_src, yiaddr;
1812 unsigned short vid;
1813 __be16 proto;
1814 u8 *hw_src;
1815
1816 if (!atomic_read(&bat_priv->distributed_arp_table))
1817 return;
1818
1819 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1820 return;
1821
1822 ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1823 skb_set_network_header(skb, hdr_size + ETH_HLEN);
1824 proto = ethhdr->h_proto;
1825
1826 if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1827 return;
1828
1829 hw_src = ethhdr->h_source;
1830 vid = batadv_dat_get_vid(skb, &hdr_size);
1831
1832 batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1833 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1834
1835 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1836 "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1837 &ip_src, hw_src, batadv_print_vid(vid));
1838 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1839 "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1840 &yiaddr, chaddr, batadv_print_vid(vid));
1841}
1842
1843/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001844 * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1845 * dropped (because the node has already obtained the reply via DAT) or not
Antonio Quartullic384ea32011-06-26 03:37:18 +02001846 * @bat_priv: the bat priv with all the soft interface information
1847 * @forw_packet: the broadcast packet
1848 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001849 * Return: true if the node can drop the packet, false otherwise.
Antonio Quartullic384ea32011-06-26 03:37:18 +02001850 */
1851bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1852 struct batadv_forw_packet *forw_packet)
1853{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001854 u16 type;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001855 __be32 ip_dst;
1856 struct batadv_dat_entry *dat_entry = NULL;
1857 bool ret = false;
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001858 int hdr_size = sizeof(struct batadv_bcast_packet);
1859 unsigned short vid;
Antonio Quartullic384ea32011-06-26 03:37:18 +02001860
Antonio Quartulli33af49a2012-08-08 18:50:57 +02001861 if (!atomic_read(&bat_priv->distributed_arp_table))
1862 goto out;
1863
Antonio Quartullic384ea32011-06-26 03:37:18 +02001864 /* If this packet is an ARP_REQUEST and the node already has the
1865 * information that it is going to ask, then the packet can be dropped
1866 */
Linus Lüssinge2d9ba42017-02-17 11:17:07 +01001867 if (batadv_forw_packet_is_rebroadcast(forw_packet))
Antonio Quartullic384ea32011-06-26 03:37:18 +02001868 goto out;
1869
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001870 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1871
1872 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001873 if (type != ARPOP_REQUEST)
1874 goto out;
1875
Antonio Quartullibe1db4f2013-06-04 12:11:43 +02001876 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1877 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001878 /* check if the node already got this entry */
1879 if (!dat_entry) {
1880 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1881 "ARP Request for %pI4: fallback\n", &ip_dst);
1882 goto out;
1883 }
1884
1885 batadv_dbg(BATADV_DBG_DAT, bat_priv,
1886 "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1887 ret = true;
1888
1889out:
1890 if (dat_entry)
Sven Eckelmanna6416f92016-01-17 11:01:17 +01001891 batadv_dat_entry_put(dat_entry);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001892 return ret;
1893}