1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright (C) 2011-2020 B.A.T.M.A.N. contributors:
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Antonio Quartulli
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include "distributed-arp-table.h"
8*4882a593Smuzhiyun #include "main.h"
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <asm/unaligned.h>
11*4882a593Smuzhiyun #include <linux/atomic.h>
12*4882a593Smuzhiyun #include <linux/bitops.h>
13*4882a593Smuzhiyun #include <linux/byteorder/generic.h>
14*4882a593Smuzhiyun #include <linux/errno.h>
15*4882a593Smuzhiyun #include <linux/etherdevice.h>
16*4882a593Smuzhiyun #include <linux/gfp.h>
17*4882a593Smuzhiyun #include <linux/if_arp.h>
18*4882a593Smuzhiyun #include <linux/if_ether.h>
19*4882a593Smuzhiyun #include <linux/if_vlan.h>
20*4882a593Smuzhiyun #include <linux/in.h>
21*4882a593Smuzhiyun #include <linux/ip.h>
22*4882a593Smuzhiyun #include <linux/jiffies.h>
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/kref.h>
25*4882a593Smuzhiyun #include <linux/list.h>
26*4882a593Smuzhiyun #include <linux/netlink.h>
27*4882a593Smuzhiyun #include <linux/rculist.h>
28*4882a593Smuzhiyun #include <linux/rcupdate.h>
29*4882a593Smuzhiyun #include <linux/seq_file.h>
30*4882a593Smuzhiyun #include <linux/skbuff.h>
31*4882a593Smuzhiyun #include <linux/slab.h>
32*4882a593Smuzhiyun #include <linux/spinlock.h>
33*4882a593Smuzhiyun #include <linux/stddef.h>
34*4882a593Smuzhiyun #include <linux/string.h>
35*4882a593Smuzhiyun #include <linux/udp.h>
36*4882a593Smuzhiyun #include <linux/workqueue.h>
37*4882a593Smuzhiyun #include <net/arp.h>
38*4882a593Smuzhiyun #include <net/genetlink.h>
39*4882a593Smuzhiyun #include <net/netlink.h>
40*4882a593Smuzhiyun #include <net/sock.h>
41*4882a593Smuzhiyun #include <uapi/linux/batman_adv.h>
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #include "bridge_loop_avoidance.h"
44*4882a593Smuzhiyun #include "hard-interface.h"
45*4882a593Smuzhiyun #include "hash.h"
46*4882a593Smuzhiyun #include "log.h"
47*4882a593Smuzhiyun #include "netlink.h"
48*4882a593Smuzhiyun #include "originator.h"
49*4882a593Smuzhiyun #include "send.h"
50*4882a593Smuzhiyun #include "soft-interface.h"
51*4882a593Smuzhiyun #include "translation-table.h"
52*4882a593Smuzhiyun #include "tvlv.h"
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun enum batadv_bootpop {
55*4882a593Smuzhiyun BATADV_BOOTREPLY = 2,
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun enum batadv_boothtype {
59*4882a593Smuzhiyun BATADV_HTYPE_ETHERNET = 1,
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun enum batadv_dhcpoptioncode {
63*4882a593Smuzhiyun BATADV_DHCP_OPT_PAD = 0,
64*4882a593Smuzhiyun BATADV_DHCP_OPT_MSG_TYPE = 53,
65*4882a593Smuzhiyun BATADV_DHCP_OPT_END = 255,
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun enum batadv_dhcptype {
69*4882a593Smuzhiyun BATADV_DHCPACK = 5,
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* { 99, 130, 83, 99 } */
73*4882a593Smuzhiyun #define BATADV_DHCP_MAGIC 1669485411
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun struct batadv_dhcp_packet {
76*4882a593Smuzhiyun __u8 op;
77*4882a593Smuzhiyun __u8 htype;
78*4882a593Smuzhiyun __u8 hlen;
79*4882a593Smuzhiyun __u8 hops;
80*4882a593Smuzhiyun __be32 xid;
81*4882a593Smuzhiyun __be16 secs;
82*4882a593Smuzhiyun __be16 flags;
83*4882a593Smuzhiyun __be32 ciaddr;
84*4882a593Smuzhiyun __be32 yiaddr;
85*4882a593Smuzhiyun __be32 siaddr;
86*4882a593Smuzhiyun __be32 giaddr;
87*4882a593Smuzhiyun __u8 chaddr[16];
88*4882a593Smuzhiyun __u8 sname[64];
89*4882a593Smuzhiyun __u8 file[128];
90*4882a593Smuzhiyun __be32 magic;
91*4882a593Smuzhiyun __u8 options[];
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun #define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
95*4882a593Smuzhiyun #define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun static void batadv_dat_purge(struct work_struct *work);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun * batadv_dat_start_timer() - initialise the DAT periodic worker
101*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
102*4882a593Smuzhiyun */
batadv_dat_start_timer(struct batadv_priv * bat_priv)103*4882a593Smuzhiyun static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
106*4882a593Smuzhiyun queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
107*4882a593Smuzhiyun msecs_to_jiffies(10000));
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun * batadv_dat_entry_release() - release dat_entry from lists and queue for free
112*4882a593Smuzhiyun * after rcu grace period
113*4882a593Smuzhiyun * @ref: kref pointer of the dat_entry
114*4882a593Smuzhiyun */
batadv_dat_entry_release(struct kref * ref)115*4882a593Smuzhiyun static void batadv_dat_entry_release(struct kref *ref)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun kfree_rcu(dat_entry, rcu);
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /**
125*4882a593Smuzhiyun * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
126*4882a593Smuzhiyun * release it
127*4882a593Smuzhiyun * @dat_entry: dat_entry to be free'd
128*4882a593Smuzhiyun */
batadv_dat_entry_put(struct batadv_dat_entry * dat_entry)129*4882a593Smuzhiyun static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun if (!dat_entry)
132*4882a593Smuzhiyun return;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun kref_put(&dat_entry->refcount, batadv_dat_entry_release);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
139*4882a593Smuzhiyun * @dat_entry: the entry to check
140*4882a593Smuzhiyun *
141*4882a593Smuzhiyun * Return: true if the entry has to be purged now, false otherwise.
142*4882a593Smuzhiyun */
batadv_dat_to_purge(struct batadv_dat_entry * dat_entry)143*4882a593Smuzhiyun static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun return batadv_has_timed_out(dat_entry->last_update,
146*4882a593Smuzhiyun BATADV_DAT_ENTRY_TIMEOUT);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /**
150*4882a593Smuzhiyun * __batadv_dat_purge() - delete entries from the DAT local storage
151*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
152*4882a593Smuzhiyun * @to_purge: function in charge to decide whether an entry has to be purged or
153*4882a593Smuzhiyun * not. This function takes the dat_entry as argument and has to
154*4882a593Smuzhiyun * returns a boolean value: true is the entry has to be deleted,
155*4882a593Smuzhiyun * false otherwise
156*4882a593Smuzhiyun *
157*4882a593Smuzhiyun * Loops over each entry in the DAT local storage and deletes it if and only if
158*4882a593Smuzhiyun * the to_purge function passed as argument returns true.
159*4882a593Smuzhiyun */
__batadv_dat_purge(struct batadv_priv * bat_priv,bool (* to_purge)(struct batadv_dat_entry *))160*4882a593Smuzhiyun static void __batadv_dat_purge(struct batadv_priv *bat_priv,
161*4882a593Smuzhiyun bool (*to_purge)(struct batadv_dat_entry *))
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun spinlock_t *list_lock; /* protects write access to the hash lists */
164*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry;
165*4882a593Smuzhiyun struct hlist_node *node_tmp;
166*4882a593Smuzhiyun struct hlist_head *head;
167*4882a593Smuzhiyun u32 i;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (!bat_priv->dat.hash)
170*4882a593Smuzhiyun return;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun for (i = 0; i < bat_priv->dat.hash->size; i++) {
173*4882a593Smuzhiyun head = &bat_priv->dat.hash->table[i];
174*4882a593Smuzhiyun list_lock = &bat_priv->dat.hash->list_locks[i];
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun spin_lock_bh(list_lock);
177*4882a593Smuzhiyun hlist_for_each_entry_safe(dat_entry, node_tmp, head,
178*4882a593Smuzhiyun hash_entry) {
179*4882a593Smuzhiyun /* if a helper function has been passed as parameter,
180*4882a593Smuzhiyun * ask it if the entry has to be purged or not
181*4882a593Smuzhiyun */
182*4882a593Smuzhiyun if (to_purge && !to_purge(dat_entry))
183*4882a593Smuzhiyun continue;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun hlist_del_rcu(&dat_entry->hash_entry);
186*4882a593Smuzhiyun batadv_dat_entry_put(dat_entry);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun spin_unlock_bh(list_lock);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun * batadv_dat_purge() - periodic task that deletes old entries from the local
194*4882a593Smuzhiyun * DAT hash table
195*4882a593Smuzhiyun * @work: kernel work struct
196*4882a593Smuzhiyun */
batadv_dat_purge(struct work_struct * work)197*4882a593Smuzhiyun static void batadv_dat_purge(struct work_struct *work)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun struct delayed_work *delayed_work;
200*4882a593Smuzhiyun struct batadv_priv_dat *priv_dat;
201*4882a593Smuzhiyun struct batadv_priv *bat_priv;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun delayed_work = to_delayed_work(work);
204*4882a593Smuzhiyun priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
205*4882a593Smuzhiyun bat_priv = container_of(priv_dat, struct batadv_priv, dat);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
208*4882a593Smuzhiyun batadv_dat_start_timer(bat_priv);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /**
212*4882a593Smuzhiyun * batadv_compare_dat() - comparing function used in the local DAT hash table
213*4882a593Smuzhiyun * @node: node in the local table
214*4882a593Smuzhiyun * @data2: second object to compare the node to
215*4882a593Smuzhiyun *
216*4882a593Smuzhiyun * Return: true if the two entries are the same, false otherwise.
217*4882a593Smuzhiyun */
batadv_compare_dat(const struct hlist_node * node,const void * data2)218*4882a593Smuzhiyun static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun const void *data1 = container_of(node, struct batadv_dat_entry,
221*4882a593Smuzhiyun hash_entry);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun return memcmp(data1, data2, sizeof(__be32)) == 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /**
227*4882a593Smuzhiyun * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
228*4882a593Smuzhiyun * @skb: ARP packet
229*4882a593Smuzhiyun * @hdr_size: size of the possible header before the ARP packet
230*4882a593Smuzhiyun *
231*4882a593Smuzhiyun * Return: the value of the hw_src field in the ARP packet.
232*4882a593Smuzhiyun */
batadv_arp_hw_src(struct sk_buff * skb,int hdr_size)233*4882a593Smuzhiyun static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun u8 *addr;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun addr = (u8 *)(skb->data + hdr_size);
238*4882a593Smuzhiyun addr += ETH_HLEN + sizeof(struct arphdr);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun return addr;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /**
244*4882a593Smuzhiyun * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
245*4882a593Smuzhiyun * @skb: ARP packet
246*4882a593Smuzhiyun * @hdr_size: size of the possible header before the ARP packet
247*4882a593Smuzhiyun *
248*4882a593Smuzhiyun * Return: the value of the ip_src field in the ARP packet.
249*4882a593Smuzhiyun */
batadv_arp_ip_src(struct sk_buff * skb,int hdr_size)250*4882a593Smuzhiyun static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun return *(__force __be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /**
256*4882a593Smuzhiyun * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
257*4882a593Smuzhiyun * @skb: ARP packet
258*4882a593Smuzhiyun * @hdr_size: size of the possible header before the ARP packet
259*4882a593Smuzhiyun *
260*4882a593Smuzhiyun * Return: the value of the hw_dst field in the ARP packet.
261*4882a593Smuzhiyun */
batadv_arp_hw_dst(struct sk_buff * skb,int hdr_size)262*4882a593Smuzhiyun static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /**
268*4882a593Smuzhiyun * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
269*4882a593Smuzhiyun * @skb: ARP packet
270*4882a593Smuzhiyun * @hdr_size: size of the possible header before the ARP packet
271*4882a593Smuzhiyun *
272*4882a593Smuzhiyun * Return: the value of the ip_dst field in the ARP packet.
273*4882a593Smuzhiyun */
batadv_arp_ip_dst(struct sk_buff * skb,int hdr_size)274*4882a593Smuzhiyun static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun u8 *dst = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun return *(__force __be32 *)dst;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /**
282*4882a593Smuzhiyun * batadv_hash_dat() - compute the hash value for an IP address
283*4882a593Smuzhiyun * @data: data to hash
284*4882a593Smuzhiyun * @size: size of the hash table
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * Return: the selected index in the hash table for the given data.
287*4882a593Smuzhiyun */
batadv_hash_dat(const void * data,u32 size)288*4882a593Smuzhiyun static u32 batadv_hash_dat(const void *data, u32 size)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun u32 hash = 0;
291*4882a593Smuzhiyun const struct batadv_dat_entry *dat = data;
292*4882a593Smuzhiyun const unsigned char *key;
293*4882a593Smuzhiyun __be16 vid;
294*4882a593Smuzhiyun u32 i;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun key = (__force const unsigned char *)&dat->ip;
297*4882a593Smuzhiyun for (i = 0; i < sizeof(dat->ip); i++) {
298*4882a593Smuzhiyun hash += key[i];
299*4882a593Smuzhiyun hash += (hash << 10);
300*4882a593Smuzhiyun hash ^= (hash >> 6);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun vid = htons(dat->vid);
304*4882a593Smuzhiyun key = (__force const unsigned char *)&vid;
305*4882a593Smuzhiyun for (i = 0; i < sizeof(dat->vid); i++) {
306*4882a593Smuzhiyun hash += key[i];
307*4882a593Smuzhiyun hash += (hash << 10);
308*4882a593Smuzhiyun hash ^= (hash >> 6);
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun hash += (hash << 3);
312*4882a593Smuzhiyun hash ^= (hash >> 11);
313*4882a593Smuzhiyun hash += (hash << 15);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun return hash % size;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun /**
319*4882a593Smuzhiyun * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
320*4882a593Smuzhiyun * table
321*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
322*4882a593Smuzhiyun * @ip: search key
323*4882a593Smuzhiyun * @vid: VLAN identifier
324*4882a593Smuzhiyun *
325*4882a593Smuzhiyun * Return: the dat_entry if found, NULL otherwise.
326*4882a593Smuzhiyun */
327*4882a593Smuzhiyun static struct batadv_dat_entry *
batadv_dat_entry_hash_find(struct batadv_priv * bat_priv,__be32 ip,unsigned short vid)328*4882a593Smuzhiyun batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
329*4882a593Smuzhiyun unsigned short vid)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun struct hlist_head *head;
332*4882a593Smuzhiyun struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
333*4882a593Smuzhiyun struct batadv_hashtable *hash = bat_priv->dat.hash;
334*4882a593Smuzhiyun u32 index;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun if (!hash)
337*4882a593Smuzhiyun return NULL;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun to_find.ip = ip;
340*4882a593Smuzhiyun to_find.vid = vid;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun index = batadv_hash_dat(&to_find, hash->size);
343*4882a593Smuzhiyun head = &hash->table[index];
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun rcu_read_lock();
346*4882a593Smuzhiyun hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
347*4882a593Smuzhiyun if (dat_entry->ip != ip)
348*4882a593Smuzhiyun continue;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun if (!kref_get_unless_zero(&dat_entry->refcount))
351*4882a593Smuzhiyun continue;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun dat_entry_tmp = dat_entry;
354*4882a593Smuzhiyun break;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun rcu_read_unlock();
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun return dat_entry_tmp;
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun /**
362*4882a593Smuzhiyun * batadv_dat_entry_add() - add a new dat entry or update it if already exists
363*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
364*4882a593Smuzhiyun * @ip: ipv4 to add/edit
365*4882a593Smuzhiyun * @mac_addr: mac address to assign to the given ipv4
366*4882a593Smuzhiyun * @vid: VLAN identifier
367*4882a593Smuzhiyun */
batadv_dat_entry_add(struct batadv_priv * bat_priv,__be32 ip,u8 * mac_addr,unsigned short vid)368*4882a593Smuzhiyun static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
369*4882a593Smuzhiyun u8 *mac_addr, unsigned short vid)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry;
372*4882a593Smuzhiyun int hash_added;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
375*4882a593Smuzhiyun /* if this entry is already known, just update it */
376*4882a593Smuzhiyun if (dat_entry) {
377*4882a593Smuzhiyun if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
378*4882a593Smuzhiyun ether_addr_copy(dat_entry->mac_addr, mac_addr);
379*4882a593Smuzhiyun dat_entry->last_update = jiffies;
380*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
381*4882a593Smuzhiyun "Entry updated: %pI4 %pM (vid: %d)\n",
382*4882a593Smuzhiyun &dat_entry->ip, dat_entry->mac_addr,
383*4882a593Smuzhiyun batadv_print_vid(vid));
384*4882a593Smuzhiyun goto out;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
388*4882a593Smuzhiyun if (!dat_entry)
389*4882a593Smuzhiyun goto out;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun dat_entry->ip = ip;
392*4882a593Smuzhiyun dat_entry->vid = vid;
393*4882a593Smuzhiyun ether_addr_copy(dat_entry->mac_addr, mac_addr);
394*4882a593Smuzhiyun dat_entry->last_update = jiffies;
395*4882a593Smuzhiyun kref_init(&dat_entry->refcount);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun kref_get(&dat_entry->refcount);
398*4882a593Smuzhiyun hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
399*4882a593Smuzhiyun batadv_hash_dat, dat_entry,
400*4882a593Smuzhiyun &dat_entry->hash_entry);
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun if (unlikely(hash_added != 0)) {
403*4882a593Smuzhiyun /* remove the reference for the hash */
404*4882a593Smuzhiyun batadv_dat_entry_put(dat_entry);
405*4882a593Smuzhiyun goto out;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
409*4882a593Smuzhiyun &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun out:
412*4882a593Smuzhiyun if (dat_entry)
413*4882a593Smuzhiyun batadv_dat_entry_put(dat_entry);
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun #ifdef CONFIG_BATMAN_ADV_DEBUG
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun /**
419*4882a593Smuzhiyun * batadv_dbg_arp() - print a debug message containing all the ARP packet
420*4882a593Smuzhiyun * details
421*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
422*4882a593Smuzhiyun * @skb: ARP packet
423*4882a593Smuzhiyun * @hdr_size: size of the possible header before the ARP packet
424*4882a593Smuzhiyun * @msg: message to print together with the debugging information
425*4882a593Smuzhiyun */
batadv_dbg_arp(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size,char * msg)426*4882a593Smuzhiyun static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
427*4882a593Smuzhiyun int hdr_size, char *msg)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun struct batadv_unicast_4addr_packet *unicast_4addr_packet;
430*4882a593Smuzhiyun struct batadv_bcast_packet *bcast_pkt;
431*4882a593Smuzhiyun u8 *orig_addr;
432*4882a593Smuzhiyun __be32 ip_src, ip_dst;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun if (msg)
435*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun ip_src = batadv_arp_ip_src(skb, hdr_size);
438*4882a593Smuzhiyun ip_dst = batadv_arp_ip_dst(skb, hdr_size);
439*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
440*4882a593Smuzhiyun "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
441*4882a593Smuzhiyun batadv_arp_hw_src(skb, hdr_size), &ip_src,
442*4882a593Smuzhiyun batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun if (hdr_size < sizeof(struct batadv_unicast_packet))
445*4882a593Smuzhiyun return;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun switch (unicast_4addr_packet->u.packet_type) {
450*4882a593Smuzhiyun case BATADV_UNICAST:
451*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
452*4882a593Smuzhiyun "* encapsulated within a UNICAST packet\n");
453*4882a593Smuzhiyun break;
454*4882a593Smuzhiyun case BATADV_UNICAST_4ADDR:
455*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
456*4882a593Smuzhiyun "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
457*4882a593Smuzhiyun unicast_4addr_packet->src);
458*4882a593Smuzhiyun switch (unicast_4addr_packet->subtype) {
459*4882a593Smuzhiyun case BATADV_P_DAT_DHT_PUT:
460*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
461*4882a593Smuzhiyun break;
462*4882a593Smuzhiyun case BATADV_P_DAT_DHT_GET:
463*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
464*4882a593Smuzhiyun break;
465*4882a593Smuzhiyun case BATADV_P_DAT_CACHE_REPLY:
466*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
467*4882a593Smuzhiyun "* type: DAT_CACHE_REPLY\n");
468*4882a593Smuzhiyun break;
469*4882a593Smuzhiyun case BATADV_P_DATA:
470*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
471*4882a593Smuzhiyun break;
472*4882a593Smuzhiyun default:
473*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
474*4882a593Smuzhiyun unicast_4addr_packet->u.packet_type);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun break;
477*4882a593Smuzhiyun case BATADV_BCAST:
478*4882a593Smuzhiyun bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
479*4882a593Smuzhiyun orig_addr = bcast_pkt->orig;
480*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
481*4882a593Smuzhiyun "* encapsulated within a BCAST packet (src: %pM)\n",
482*4882a593Smuzhiyun orig_addr);
483*4882a593Smuzhiyun break;
484*4882a593Smuzhiyun default:
485*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
486*4882a593Smuzhiyun "* encapsulated within an unknown packet type (0x%x)\n",
487*4882a593Smuzhiyun unicast_4addr_packet->u.packet_type);
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun #else
492*4882a593Smuzhiyun
batadv_dbg_arp(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size,char * msg)493*4882a593Smuzhiyun static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
494*4882a593Smuzhiyun int hdr_size, char *msg)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun #endif /* CONFIG_BATMAN_ADV_DEBUG */
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /**
501*4882a593Smuzhiyun * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
502*4882a593Smuzhiyun * @res: the array with the already selected candidates
503*4882a593Smuzhiyun * @select: number of already selected candidates
504*4882a593Smuzhiyun * @tmp_max: address of the currently evaluated node
505*4882a593Smuzhiyun * @max: current round max address
506*4882a593Smuzhiyun * @last_max: address of the last selected candidate
507*4882a593Smuzhiyun * @candidate: orig_node under evaluation
508*4882a593Smuzhiyun * @max_orig_node: last selected candidate
509*4882a593Smuzhiyun *
510*4882a593Smuzhiyun * Return: true if the node has been elected as next candidate or false
511*4882a593Smuzhiyun * otherwise.
512*4882a593Smuzhiyun */
batadv_is_orig_node_eligible(struct batadv_dat_candidate * res,int select,batadv_dat_addr_t tmp_max,batadv_dat_addr_t max,batadv_dat_addr_t last_max,struct batadv_orig_node * candidate,struct batadv_orig_node * max_orig_node)513*4882a593Smuzhiyun static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
514*4882a593Smuzhiyun int select, batadv_dat_addr_t tmp_max,
515*4882a593Smuzhiyun batadv_dat_addr_t max,
516*4882a593Smuzhiyun batadv_dat_addr_t last_max,
517*4882a593Smuzhiyun struct batadv_orig_node *candidate,
518*4882a593Smuzhiyun struct batadv_orig_node *max_orig_node)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun bool ret = false;
521*4882a593Smuzhiyun int j;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun /* check if orig node candidate is running DAT */
524*4882a593Smuzhiyun if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
525*4882a593Smuzhiyun goto out;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun /* Check if this node has already been selected... */
528*4882a593Smuzhiyun for (j = 0; j < select; j++)
529*4882a593Smuzhiyun if (res[j].orig_node == candidate)
530*4882a593Smuzhiyun break;
531*4882a593Smuzhiyun /* ..and possibly skip it */
532*4882a593Smuzhiyun if (j < select)
533*4882a593Smuzhiyun goto out;
534*4882a593Smuzhiyun /* sanity check: has it already been selected? This should not happen */
535*4882a593Smuzhiyun if (tmp_max > last_max)
536*4882a593Smuzhiyun goto out;
537*4882a593Smuzhiyun /* check if during this iteration an originator with a closer dht
538*4882a593Smuzhiyun * address has already been found
539*4882a593Smuzhiyun */
540*4882a593Smuzhiyun if (tmp_max < max)
541*4882a593Smuzhiyun goto out;
542*4882a593Smuzhiyun /* this is an hash collision with the temporary selected node. Choose
543*4882a593Smuzhiyun * the one with the lowest address
544*4882a593Smuzhiyun */
545*4882a593Smuzhiyun if (tmp_max == max && max_orig_node &&
546*4882a593Smuzhiyun batadv_compare_eth(candidate->orig, max_orig_node->orig))
547*4882a593Smuzhiyun goto out;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun ret = true;
550*4882a593Smuzhiyun out:
551*4882a593Smuzhiyun return ret;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun /**
555*4882a593Smuzhiyun * batadv_choose_next_candidate() - select the next DHT candidate
556*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
557*4882a593Smuzhiyun * @cands: candidates array
558*4882a593Smuzhiyun * @select: number of candidates already present in the array
559*4882a593Smuzhiyun * @ip_key: key to look up in the DHT
560*4882a593Smuzhiyun * @last_max: pointer where the address of the selected candidate will be saved
561*4882a593Smuzhiyun */
batadv_choose_next_candidate(struct batadv_priv * bat_priv,struct batadv_dat_candidate * cands,int select,batadv_dat_addr_t ip_key,batadv_dat_addr_t * last_max)562*4882a593Smuzhiyun static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
563*4882a593Smuzhiyun struct batadv_dat_candidate *cands,
564*4882a593Smuzhiyun int select, batadv_dat_addr_t ip_key,
565*4882a593Smuzhiyun batadv_dat_addr_t *last_max)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun batadv_dat_addr_t max = 0;
568*4882a593Smuzhiyun batadv_dat_addr_t tmp_max = 0;
569*4882a593Smuzhiyun struct batadv_orig_node *orig_node, *max_orig_node = NULL;
570*4882a593Smuzhiyun struct batadv_hashtable *hash = bat_priv->orig_hash;
571*4882a593Smuzhiyun struct hlist_head *head;
572*4882a593Smuzhiyun int i;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun /* if no node is eligible as candidate, leave the candidate type as
575*4882a593Smuzhiyun * NOT_FOUND
576*4882a593Smuzhiyun */
577*4882a593Smuzhiyun cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun /* iterate over the originator list and find the node with the closest
580*4882a593Smuzhiyun * dat_address which has not been selected yet
581*4882a593Smuzhiyun */
582*4882a593Smuzhiyun for (i = 0; i < hash->size; i++) {
583*4882a593Smuzhiyun head = &hash->table[i];
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun rcu_read_lock();
586*4882a593Smuzhiyun hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
587*4882a593Smuzhiyun /* the dht space is a ring using unsigned addresses */
588*4882a593Smuzhiyun tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
589*4882a593Smuzhiyun ip_key;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun if (!batadv_is_orig_node_eligible(cands, select,
592*4882a593Smuzhiyun tmp_max, max,
593*4882a593Smuzhiyun *last_max, orig_node,
594*4882a593Smuzhiyun max_orig_node))
595*4882a593Smuzhiyun continue;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun if (!kref_get_unless_zero(&orig_node->refcount))
598*4882a593Smuzhiyun continue;
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun max = tmp_max;
601*4882a593Smuzhiyun if (max_orig_node)
602*4882a593Smuzhiyun batadv_orig_node_put(max_orig_node);
603*4882a593Smuzhiyun max_orig_node = orig_node;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun rcu_read_unlock();
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun if (max_orig_node) {
608*4882a593Smuzhiyun cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
609*4882a593Smuzhiyun cands[select].orig_node = max_orig_node;
610*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
611*4882a593Smuzhiyun "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
612*4882a593Smuzhiyun select, max_orig_node->orig, max_orig_node->dat_addr,
613*4882a593Smuzhiyun max);
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun *last_max = max;
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun /**
619*4882a593Smuzhiyun * batadv_dat_select_candidates() - select the nodes which the DHT message has
620*4882a593Smuzhiyun * to be sent to
621*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
622*4882a593Smuzhiyun * @ip_dst: ipv4 to look up in the DHT
623*4882a593Smuzhiyun * @vid: VLAN identifier
624*4882a593Smuzhiyun *
625*4882a593Smuzhiyun * An originator O is selected if and only if its DHT_ID value is one of three
626*4882a593Smuzhiyun * closest values (from the LEFT, with wrap around if needed) then the hash
627*4882a593Smuzhiyun * value of the key. ip_dst is the key.
628*4882a593Smuzhiyun *
629*4882a593Smuzhiyun * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
630*4882a593Smuzhiyun */
631*4882a593Smuzhiyun static struct batadv_dat_candidate *
batadv_dat_select_candidates(struct batadv_priv * bat_priv,__be32 ip_dst,unsigned short vid)632*4882a593Smuzhiyun batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
633*4882a593Smuzhiyun unsigned short vid)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun int select;
636*4882a593Smuzhiyun batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
637*4882a593Smuzhiyun struct batadv_dat_candidate *res;
638*4882a593Smuzhiyun struct batadv_dat_entry dat;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun if (!bat_priv->orig_hash)
641*4882a593Smuzhiyun return NULL;
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
644*4882a593Smuzhiyun GFP_ATOMIC);
645*4882a593Smuzhiyun if (!res)
646*4882a593Smuzhiyun return NULL;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun dat.ip = ip_dst;
649*4882a593Smuzhiyun dat.vid = vid;
650*4882a593Smuzhiyun ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
651*4882a593Smuzhiyun BATADV_DAT_ADDR_MAX);
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
654*4882a593Smuzhiyun "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
655*4882a593Smuzhiyun ip_key);
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
658*4882a593Smuzhiyun batadv_choose_next_candidate(bat_priv, res, select, ip_key,
659*4882a593Smuzhiyun &last_max);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun return res;
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun /**
665*4882a593Smuzhiyun * batadv_dat_forward_data() - copy and send payload to the selected candidates
666*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
667*4882a593Smuzhiyun * @skb: payload to send
668*4882a593Smuzhiyun * @ip: the DHT key
669*4882a593Smuzhiyun * @vid: VLAN identifier
670*4882a593Smuzhiyun * @packet_subtype: unicast4addr packet subtype to use
671*4882a593Smuzhiyun *
672*4882a593Smuzhiyun * This function copies the skb with pskb_copy() and is sent as a unicast packet
673*4882a593Smuzhiyun * to each of the selected candidates.
674*4882a593Smuzhiyun *
675*4882a593Smuzhiyun * Return: true if the packet is sent to at least one candidate, false
676*4882a593Smuzhiyun * otherwise.
677*4882a593Smuzhiyun */
batadv_dat_forward_data(struct batadv_priv * bat_priv,struct sk_buff * skb,__be32 ip,unsigned short vid,int packet_subtype)678*4882a593Smuzhiyun static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
679*4882a593Smuzhiyun struct sk_buff *skb, __be32 ip,
680*4882a593Smuzhiyun unsigned short vid, int packet_subtype)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun int i;
683*4882a593Smuzhiyun bool ret = false;
684*4882a593Smuzhiyun int send_status;
685*4882a593Smuzhiyun struct batadv_neigh_node *neigh_node = NULL;
686*4882a593Smuzhiyun struct sk_buff *tmp_skb;
687*4882a593Smuzhiyun struct batadv_dat_candidate *cand;
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun cand = batadv_dat_select_candidates(bat_priv, ip, vid);
690*4882a593Smuzhiyun if (!cand)
691*4882a593Smuzhiyun goto out;
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
696*4882a593Smuzhiyun if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
697*4882a593Smuzhiyun continue;
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun neigh_node = batadv_orig_router_get(cand[i].orig_node,
700*4882a593Smuzhiyun BATADV_IF_DEFAULT);
701*4882a593Smuzhiyun if (!neigh_node)
702*4882a593Smuzhiyun goto free_orig;
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
705*4882a593Smuzhiyun if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
706*4882a593Smuzhiyun cand[i].orig_node,
707*4882a593Smuzhiyun packet_subtype)) {
708*4882a593Smuzhiyun kfree_skb(tmp_skb);
709*4882a593Smuzhiyun goto free_neigh;
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
713*4882a593Smuzhiyun if (send_status == NET_XMIT_SUCCESS) {
714*4882a593Smuzhiyun /* count the sent packet */
715*4882a593Smuzhiyun switch (packet_subtype) {
716*4882a593Smuzhiyun case BATADV_P_DAT_DHT_GET:
717*4882a593Smuzhiyun batadv_inc_counter(bat_priv,
718*4882a593Smuzhiyun BATADV_CNT_DAT_GET_TX);
719*4882a593Smuzhiyun break;
720*4882a593Smuzhiyun case BATADV_P_DAT_DHT_PUT:
721*4882a593Smuzhiyun batadv_inc_counter(bat_priv,
722*4882a593Smuzhiyun BATADV_CNT_DAT_PUT_TX);
723*4882a593Smuzhiyun break;
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun /* packet sent to a candidate: return true */
727*4882a593Smuzhiyun ret = true;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun free_neigh:
730*4882a593Smuzhiyun batadv_neigh_node_put(neigh_node);
731*4882a593Smuzhiyun free_orig:
732*4882a593Smuzhiyun batadv_orig_node_put(cand[i].orig_node);
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun out:
736*4882a593Smuzhiyun kfree(cand);
737*4882a593Smuzhiyun return ret;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun /**
741*4882a593Smuzhiyun * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
742*4882a593Smuzhiyun * setting change
743*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
744*4882a593Smuzhiyun */
batadv_dat_tvlv_container_update(struct batadv_priv * bat_priv)745*4882a593Smuzhiyun static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun char dat_mode;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun dat_mode = atomic_read(&bat_priv->distributed_arp_table);
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun switch (dat_mode) {
752*4882a593Smuzhiyun case 0:
753*4882a593Smuzhiyun batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
754*4882a593Smuzhiyun break;
755*4882a593Smuzhiyun case 1:
756*4882a593Smuzhiyun batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
757*4882a593Smuzhiyun NULL, 0);
758*4882a593Smuzhiyun break;
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun /**
763*4882a593Smuzhiyun * batadv_dat_status_update() - update the dat tvlv container after dat
764*4882a593Smuzhiyun * setting change
765*4882a593Smuzhiyun * @net_dev: the soft interface net device
766*4882a593Smuzhiyun */
batadv_dat_status_update(struct net_device * net_dev)767*4882a593Smuzhiyun void batadv_dat_status_update(struct net_device *net_dev)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun struct batadv_priv *bat_priv = netdev_priv(net_dev);
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun batadv_dat_tvlv_container_update(bat_priv);
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun /**
775*4882a593Smuzhiyun * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
776*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
777*4882a593Smuzhiyun * @orig: the orig_node of the ogm
778*4882a593Smuzhiyun * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
779*4882a593Smuzhiyun * @tvlv_value: tvlv buffer containing the gateway data
780*4882a593Smuzhiyun * @tvlv_value_len: tvlv buffer length
781*4882a593Smuzhiyun */
batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv * bat_priv,struct batadv_orig_node * orig,u8 flags,void * tvlv_value,u16 tvlv_value_len)782*4882a593Smuzhiyun static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
783*4882a593Smuzhiyun struct batadv_orig_node *orig,
784*4882a593Smuzhiyun u8 flags,
785*4882a593Smuzhiyun void *tvlv_value, u16 tvlv_value_len)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
788*4882a593Smuzhiyun clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
789*4882a593Smuzhiyun else
790*4882a593Smuzhiyun set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun /**
794*4882a593Smuzhiyun * batadv_dat_hash_free() - free the local DAT hash table
795*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
796*4882a593Smuzhiyun */
batadv_dat_hash_free(struct batadv_priv * bat_priv)797*4882a593Smuzhiyun static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
798*4882a593Smuzhiyun {
799*4882a593Smuzhiyun if (!bat_priv->dat.hash)
800*4882a593Smuzhiyun return;
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun __batadv_dat_purge(bat_priv, NULL);
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun batadv_hash_destroy(bat_priv->dat.hash);
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun bat_priv->dat.hash = NULL;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun /**
810*4882a593Smuzhiyun * batadv_dat_init() - initialise the DAT internals
811*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
812*4882a593Smuzhiyun *
813*4882a593Smuzhiyun * Return: 0 in case of success, a negative error code otherwise
814*4882a593Smuzhiyun */
batadv_dat_init(struct batadv_priv * bat_priv)815*4882a593Smuzhiyun int batadv_dat_init(struct batadv_priv *bat_priv)
816*4882a593Smuzhiyun {
817*4882a593Smuzhiyun if (bat_priv->dat.hash)
818*4882a593Smuzhiyun return 0;
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun bat_priv->dat.hash = batadv_hash_new(1024);
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun if (!bat_priv->dat.hash)
823*4882a593Smuzhiyun return -ENOMEM;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun batadv_dat_start_timer(bat_priv);
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
828*4882a593Smuzhiyun NULL, BATADV_TVLV_DAT, 1,
829*4882a593Smuzhiyun BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
830*4882a593Smuzhiyun batadv_dat_tvlv_container_update(bat_priv);
831*4882a593Smuzhiyun return 0;
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun /**
835*4882a593Smuzhiyun * batadv_dat_free() - free the DAT internals
836*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
837*4882a593Smuzhiyun */
batadv_dat_free(struct batadv_priv * bat_priv)838*4882a593Smuzhiyun void batadv_dat_free(struct batadv_priv *bat_priv)
839*4882a593Smuzhiyun {
840*4882a593Smuzhiyun batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
841*4882a593Smuzhiyun batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun cancel_delayed_work_sync(&bat_priv->dat.work);
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun batadv_dat_hash_free(bat_priv);
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun #ifdef CONFIG_BATMAN_ADV_DEBUGFS
849*4882a593Smuzhiyun /**
850*4882a593Smuzhiyun * batadv_dat_cache_seq_print_text() - print the local DAT hash table
851*4882a593Smuzhiyun * @seq: seq file to print on
852*4882a593Smuzhiyun * @offset: not used
853*4882a593Smuzhiyun *
854*4882a593Smuzhiyun * Return: always 0
855*4882a593Smuzhiyun */
batadv_dat_cache_seq_print_text(struct seq_file * seq,void * offset)856*4882a593Smuzhiyun int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun struct net_device *net_dev = (struct net_device *)seq->private;
859*4882a593Smuzhiyun struct batadv_priv *bat_priv = netdev_priv(net_dev);
860*4882a593Smuzhiyun struct batadv_hashtable *hash = bat_priv->dat.hash;
861*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry;
862*4882a593Smuzhiyun struct batadv_hard_iface *primary_if;
863*4882a593Smuzhiyun struct hlist_head *head;
864*4882a593Smuzhiyun unsigned long last_seen_jiffies;
865*4882a593Smuzhiyun int last_seen_msecs, last_seen_secs, last_seen_mins;
866*4882a593Smuzhiyun u32 i;
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun primary_if = batadv_seq_print_text_primary_if_get(seq);
869*4882a593Smuzhiyun if (!primary_if)
870*4882a593Smuzhiyun goto out;
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
873*4882a593Smuzhiyun seq_puts(seq,
874*4882a593Smuzhiyun " IPv4 MAC VID last-seen\n");
875*4882a593Smuzhiyun
876*4882a593Smuzhiyun for (i = 0; i < hash->size; i++) {
877*4882a593Smuzhiyun head = &hash->table[i];
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun rcu_read_lock();
880*4882a593Smuzhiyun hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
881*4882a593Smuzhiyun last_seen_jiffies = jiffies - dat_entry->last_update;
882*4882a593Smuzhiyun last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
883*4882a593Smuzhiyun last_seen_mins = last_seen_msecs / 60000;
884*4882a593Smuzhiyun last_seen_msecs = last_seen_msecs % 60000;
885*4882a593Smuzhiyun last_seen_secs = last_seen_msecs / 1000;
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun seq_printf(seq, " * %15pI4 %pM %4i %6i:%02i\n",
888*4882a593Smuzhiyun &dat_entry->ip, dat_entry->mac_addr,
889*4882a593Smuzhiyun batadv_print_vid(dat_entry->vid),
890*4882a593Smuzhiyun last_seen_mins, last_seen_secs);
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun rcu_read_unlock();
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun out:
896*4882a593Smuzhiyun if (primary_if)
897*4882a593Smuzhiyun batadv_hardif_put(primary_if);
898*4882a593Smuzhiyun return 0;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun #endif
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun /**
903*4882a593Smuzhiyun * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
904*4882a593Smuzhiyun * netlink socket
905*4882a593Smuzhiyun * @msg: buffer for the message
906*4882a593Smuzhiyun * @portid: netlink port
907*4882a593Smuzhiyun * @cb: Control block containing additional options
908*4882a593Smuzhiyun * @dat_entry: entry to dump
909*4882a593Smuzhiyun *
910*4882a593Smuzhiyun * Return: 0 or error code.
911*4882a593Smuzhiyun */
912*4882a593Smuzhiyun static int
batadv_dat_cache_dump_entry(struct sk_buff * msg,u32 portid,struct netlink_callback * cb,struct batadv_dat_entry * dat_entry)913*4882a593Smuzhiyun batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
914*4882a593Smuzhiyun struct netlink_callback *cb,
915*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun int msecs;
918*4882a593Smuzhiyun void *hdr;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
921*4882a593Smuzhiyun &batadv_netlink_family, NLM_F_MULTI,
922*4882a593Smuzhiyun BATADV_CMD_GET_DAT_CACHE);
923*4882a593Smuzhiyun if (!hdr)
924*4882a593Smuzhiyun return -ENOBUFS;
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun genl_dump_check_consistent(cb, hdr);
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
931*4882a593Smuzhiyun dat_entry->ip) ||
932*4882a593Smuzhiyun nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
933*4882a593Smuzhiyun dat_entry->mac_addr) ||
934*4882a593Smuzhiyun nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
935*4882a593Smuzhiyun nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
936*4882a593Smuzhiyun genlmsg_cancel(msg, hdr);
937*4882a593Smuzhiyun return -EMSGSIZE;
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun genlmsg_end(msg, hdr);
941*4882a593Smuzhiyun return 0;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun /**
945*4882a593Smuzhiyun * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
946*4882a593Smuzhiyun * a netlink socket
947*4882a593Smuzhiyun * @msg: buffer for the message
948*4882a593Smuzhiyun * @portid: netlink port
949*4882a593Smuzhiyun * @cb: Control block containing additional options
950*4882a593Smuzhiyun * @hash: hash to dump
951*4882a593Smuzhiyun * @bucket: bucket index to dump
952*4882a593Smuzhiyun * @idx_skip: How many entries to skip
953*4882a593Smuzhiyun *
954*4882a593Smuzhiyun * Return: 0 or error code.
955*4882a593Smuzhiyun */
956*4882a593Smuzhiyun static int
batadv_dat_cache_dump_bucket(struct sk_buff * msg,u32 portid,struct netlink_callback * cb,struct batadv_hashtable * hash,unsigned int bucket,int * idx_skip)957*4882a593Smuzhiyun batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
958*4882a593Smuzhiyun struct netlink_callback *cb,
959*4882a593Smuzhiyun struct batadv_hashtable *hash, unsigned int bucket,
960*4882a593Smuzhiyun int *idx_skip)
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry;
963*4882a593Smuzhiyun int idx = 0;
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun spin_lock_bh(&hash->list_locks[bucket]);
966*4882a593Smuzhiyun cb->seq = atomic_read(&hash->generation) << 1 | 1;
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
969*4882a593Smuzhiyun if (idx < *idx_skip)
970*4882a593Smuzhiyun goto skip;
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
973*4882a593Smuzhiyun spin_unlock_bh(&hash->list_locks[bucket]);
974*4882a593Smuzhiyun *idx_skip = idx;
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun return -EMSGSIZE;
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun skip:
980*4882a593Smuzhiyun idx++;
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun spin_unlock_bh(&hash->list_locks[bucket]);
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun return 0;
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun /**
988*4882a593Smuzhiyun * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
989*4882a593Smuzhiyun * @msg: buffer for the message
990*4882a593Smuzhiyun * @cb: callback structure containing arguments
991*4882a593Smuzhiyun *
992*4882a593Smuzhiyun * Return: message length.
993*4882a593Smuzhiyun */
batadv_dat_cache_dump(struct sk_buff * msg,struct netlink_callback * cb)994*4882a593Smuzhiyun int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun struct batadv_hard_iface *primary_if = NULL;
997*4882a593Smuzhiyun int portid = NETLINK_CB(cb->skb).portid;
998*4882a593Smuzhiyun struct net *net = sock_net(cb->skb->sk);
999*4882a593Smuzhiyun struct net_device *soft_iface;
1000*4882a593Smuzhiyun struct batadv_hashtable *hash;
1001*4882a593Smuzhiyun struct batadv_priv *bat_priv;
1002*4882a593Smuzhiyun int bucket = cb->args[0];
1003*4882a593Smuzhiyun int idx = cb->args[1];
1004*4882a593Smuzhiyun int ifindex;
1005*4882a593Smuzhiyun int ret = 0;
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun ifindex = batadv_netlink_get_ifindex(cb->nlh,
1008*4882a593Smuzhiyun BATADV_ATTR_MESH_IFINDEX);
1009*4882a593Smuzhiyun if (!ifindex)
1010*4882a593Smuzhiyun return -EINVAL;
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun soft_iface = dev_get_by_index(net, ifindex);
1013*4882a593Smuzhiyun if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1014*4882a593Smuzhiyun ret = -ENODEV;
1015*4882a593Smuzhiyun goto out;
1016*4882a593Smuzhiyun }
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun bat_priv = netdev_priv(soft_iface);
1019*4882a593Smuzhiyun hash = bat_priv->dat.hash;
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun primary_if = batadv_primary_if_get_selected(bat_priv);
1022*4882a593Smuzhiyun if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1023*4882a593Smuzhiyun ret = -ENOENT;
1024*4882a593Smuzhiyun goto out;
1025*4882a593Smuzhiyun }
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun while (bucket < hash->size) {
1028*4882a593Smuzhiyun if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
1029*4882a593Smuzhiyun &idx))
1030*4882a593Smuzhiyun break;
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun bucket++;
1033*4882a593Smuzhiyun idx = 0;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun cb->args[0] = bucket;
1037*4882a593Smuzhiyun cb->args[1] = idx;
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun ret = msg->len;
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun out:
1042*4882a593Smuzhiyun if (primary_if)
1043*4882a593Smuzhiyun batadv_hardif_put(primary_if);
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun if (soft_iface)
1046*4882a593Smuzhiyun dev_put(soft_iface);
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun return ret;
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun /**
1052*4882a593Smuzhiyun * batadv_arp_get_type() - parse an ARP packet and gets the type
1053*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1054*4882a593Smuzhiyun * @skb: packet to analyse
1055*4882a593Smuzhiyun * @hdr_size: size of the possible header before the ARP packet in the skb
1056*4882a593Smuzhiyun *
1057*4882a593Smuzhiyun * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
1058*4882a593Smuzhiyun */
batadv_arp_get_type(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)1059*4882a593Smuzhiyun static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1060*4882a593Smuzhiyun struct sk_buff *skb, int hdr_size)
1061*4882a593Smuzhiyun {
1062*4882a593Smuzhiyun struct arphdr *arphdr;
1063*4882a593Smuzhiyun struct ethhdr *ethhdr;
1064*4882a593Smuzhiyun __be32 ip_src, ip_dst;
1065*4882a593Smuzhiyun u8 *hw_src, *hw_dst;
1066*4882a593Smuzhiyun u16 type = 0;
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun /* pull the ethernet header */
1069*4882a593Smuzhiyun if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1070*4882a593Smuzhiyun goto out;
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun if (ethhdr->h_proto != htons(ETH_P_ARP))
1075*4882a593Smuzhiyun goto out;
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun /* pull the ARP payload */
1078*4882a593Smuzhiyun if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1079*4882a593Smuzhiyun arp_hdr_len(skb->dev))))
1080*4882a593Smuzhiyun goto out;
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1083*4882a593Smuzhiyun
1084*4882a593Smuzhiyun /* check whether the ARP packet carries a valid IP information */
1085*4882a593Smuzhiyun if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1086*4882a593Smuzhiyun goto out;
1087*4882a593Smuzhiyun
1088*4882a593Smuzhiyun if (arphdr->ar_pro != htons(ETH_P_IP))
1089*4882a593Smuzhiyun goto out;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun if (arphdr->ar_hln != ETH_ALEN)
1092*4882a593Smuzhiyun goto out;
1093*4882a593Smuzhiyun
1094*4882a593Smuzhiyun if (arphdr->ar_pln != 4)
1095*4882a593Smuzhiyun goto out;
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun /* Check for bad reply/request. If the ARP message is not sane, DAT
1098*4882a593Smuzhiyun * will simply ignore it
1099*4882a593Smuzhiyun */
1100*4882a593Smuzhiyun ip_src = batadv_arp_ip_src(skb, hdr_size);
1101*4882a593Smuzhiyun ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1102*4882a593Smuzhiyun if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
1103*4882a593Smuzhiyun ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
1104*4882a593Smuzhiyun ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
1105*4882a593Smuzhiyun ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
1106*4882a593Smuzhiyun goto out;
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun hw_src = batadv_arp_hw_src(skb, hdr_size);
1109*4882a593Smuzhiyun if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1110*4882a593Smuzhiyun goto out;
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun /* don't care about the destination MAC address in ARP requests */
1113*4882a593Smuzhiyun if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
1114*4882a593Smuzhiyun hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1115*4882a593Smuzhiyun if (is_zero_ether_addr(hw_dst) ||
1116*4882a593Smuzhiyun is_multicast_ether_addr(hw_dst))
1117*4882a593Smuzhiyun goto out;
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun type = ntohs(arphdr->ar_op);
1121*4882a593Smuzhiyun out:
1122*4882a593Smuzhiyun return type;
1123*4882a593Smuzhiyun }
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun /**
1126*4882a593Smuzhiyun * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
1127*4882a593Smuzhiyun * @skb: the buffer containing the packet to extract the VID from
1128*4882a593Smuzhiyun * @hdr_size: the size of the batman-adv header encapsulating the packet
1129*4882a593Smuzhiyun *
1130*4882a593Smuzhiyun * Return: If the packet embedded in the skb is vlan tagged this function
1131*4882a593Smuzhiyun * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1132*4882a593Smuzhiyun * is returned.
1133*4882a593Smuzhiyun */
batadv_dat_get_vid(struct sk_buff * skb,int * hdr_size)1134*4882a593Smuzhiyun static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1135*4882a593Smuzhiyun {
1136*4882a593Smuzhiyun unsigned short vid;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun vid = batadv_get_vid(skb, *hdr_size);
1139*4882a593Smuzhiyun
1140*4882a593Smuzhiyun /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1141*4882a593Smuzhiyun * If the header contained in the packet is a VLAN one (which is longer)
1142*4882a593Smuzhiyun * hdr_size is updated so that the functions will still skip the
1143*4882a593Smuzhiyun * correct amount of bytes.
1144*4882a593Smuzhiyun */
1145*4882a593Smuzhiyun if (vid & BATADV_VLAN_HAS_TAG)
1146*4882a593Smuzhiyun *hdr_size += VLAN_HLEN;
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun return vid;
1149*4882a593Smuzhiyun }
1150*4882a593Smuzhiyun
1151*4882a593Smuzhiyun /**
1152*4882a593Smuzhiyun * batadv_dat_arp_create_reply() - create an ARP Reply
1153*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1154*4882a593Smuzhiyun * @ip_src: ARP sender IP
1155*4882a593Smuzhiyun * @ip_dst: ARP target IP
1156*4882a593Smuzhiyun * @hw_src: Ethernet source and ARP sender MAC
1157*4882a593Smuzhiyun * @hw_dst: Ethernet destination and ARP target MAC
1158*4882a593Smuzhiyun * @vid: VLAN identifier (optional, set to zero otherwise)
1159*4882a593Smuzhiyun *
1160*4882a593Smuzhiyun * Creates an ARP Reply from the given values, optionally encapsulated in a
1161*4882a593Smuzhiyun * VLAN header.
1162*4882a593Smuzhiyun *
1163*4882a593Smuzhiyun * Return: An skb containing an ARP Reply.
1164*4882a593Smuzhiyun */
1165*4882a593Smuzhiyun static struct sk_buff *
batadv_dat_arp_create_reply(struct batadv_priv * bat_priv,__be32 ip_src,__be32 ip_dst,u8 * hw_src,u8 * hw_dst,unsigned short vid)1166*4882a593Smuzhiyun batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1167*4882a593Smuzhiyun __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1168*4882a593Smuzhiyun unsigned short vid)
1169*4882a593Smuzhiyun {
1170*4882a593Smuzhiyun struct sk_buff *skb;
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
1173*4882a593Smuzhiyun ip_src, hw_dst, hw_src, hw_dst);
1174*4882a593Smuzhiyun if (!skb)
1175*4882a593Smuzhiyun return NULL;
1176*4882a593Smuzhiyun
1177*4882a593Smuzhiyun skb_reset_mac_header(skb);
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun if (vid & BATADV_VLAN_HAS_TAG)
1180*4882a593Smuzhiyun skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1181*4882a593Smuzhiyun vid & VLAN_VID_MASK);
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun return skb;
1184*4882a593Smuzhiyun }
1185*4882a593Smuzhiyun
1186*4882a593Smuzhiyun /**
1187*4882a593Smuzhiyun * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
1188*4882a593Smuzhiyun * answer using DAT
1189*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1190*4882a593Smuzhiyun * @skb: packet to check
1191*4882a593Smuzhiyun *
1192*4882a593Smuzhiyun * Return: true if the message has been sent to the dht candidates, false
1193*4882a593Smuzhiyun * otherwise. In case of a positive return value the message has to be enqueued
1194*4882a593Smuzhiyun * to permit the fallback.
1195*4882a593Smuzhiyun */
batadv_dat_snoop_outgoing_arp_request(struct batadv_priv * bat_priv,struct sk_buff * skb)1196*4882a593Smuzhiyun bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1197*4882a593Smuzhiyun struct sk_buff *skb)
1198*4882a593Smuzhiyun {
1199*4882a593Smuzhiyun u16 type = 0;
1200*4882a593Smuzhiyun __be32 ip_dst, ip_src;
1201*4882a593Smuzhiyun u8 *hw_src;
1202*4882a593Smuzhiyun bool ret = false;
1203*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry = NULL;
1204*4882a593Smuzhiyun struct sk_buff *skb_new;
1205*4882a593Smuzhiyun struct net_device *soft_iface = bat_priv->soft_iface;
1206*4882a593Smuzhiyun int hdr_size = 0;
1207*4882a593Smuzhiyun unsigned short vid;
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun if (!atomic_read(&bat_priv->distributed_arp_table))
1210*4882a593Smuzhiyun goto out;
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun vid = batadv_dat_get_vid(skb, &hdr_size);
1213*4882a593Smuzhiyun
1214*4882a593Smuzhiyun type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1215*4882a593Smuzhiyun /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1216*4882a593Smuzhiyun * message to the selected DHT candidates
1217*4882a593Smuzhiyun */
1218*4882a593Smuzhiyun if (type != ARPOP_REQUEST)
1219*4882a593Smuzhiyun goto out;
1220*4882a593Smuzhiyun
1221*4882a593Smuzhiyun batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun ip_src = batadv_arp_ip_src(skb, hdr_size);
1224*4882a593Smuzhiyun hw_src = batadv_arp_hw_src(skb, hdr_size);
1225*4882a593Smuzhiyun ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1230*4882a593Smuzhiyun if (dat_entry) {
1231*4882a593Smuzhiyun /* If the ARP request is destined for a local client the local
1232*4882a593Smuzhiyun * client will answer itself. DAT would only generate a
1233*4882a593Smuzhiyun * duplicate packet.
1234*4882a593Smuzhiyun *
1235*4882a593Smuzhiyun * Moreover, if the soft-interface is enslaved into a bridge, an
1236*4882a593Smuzhiyun * additional DAT answer may trigger kernel warnings about
1237*4882a593Smuzhiyun * a packet coming from the wrong port.
1238*4882a593Smuzhiyun */
1239*4882a593Smuzhiyun if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
1240*4882a593Smuzhiyun ret = true;
1241*4882a593Smuzhiyun goto out;
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun /* If BLA is enabled, only send ARP replies if we have claimed
1245*4882a593Smuzhiyun * the destination for the ARP request or if no one else of
1246*4882a593Smuzhiyun * the backbone gws belonging to our backbone has claimed the
1247*4882a593Smuzhiyun * destination.
1248*4882a593Smuzhiyun */
1249*4882a593Smuzhiyun if (!batadv_bla_check_claim(bat_priv,
1250*4882a593Smuzhiyun dat_entry->mac_addr, vid)) {
1251*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
1252*4882a593Smuzhiyun "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1253*4882a593Smuzhiyun dat_entry->mac_addr);
1254*4882a593Smuzhiyun ret = true;
1255*4882a593Smuzhiyun goto out;
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1259*4882a593Smuzhiyun dat_entry->mac_addr,
1260*4882a593Smuzhiyun hw_src, vid);
1261*4882a593Smuzhiyun if (!skb_new)
1262*4882a593Smuzhiyun goto out;
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun skb_new->protocol = eth_type_trans(skb_new, soft_iface);
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1267*4882a593Smuzhiyun batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1268*4882a593Smuzhiyun skb->len + ETH_HLEN + hdr_size);
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun netif_rx(skb_new);
1271*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1272*4882a593Smuzhiyun ret = true;
1273*4882a593Smuzhiyun } else {
1274*4882a593Smuzhiyun /* Send the request to the DHT */
1275*4882a593Smuzhiyun ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1276*4882a593Smuzhiyun BATADV_P_DAT_DHT_GET);
1277*4882a593Smuzhiyun }
1278*4882a593Smuzhiyun out:
1279*4882a593Smuzhiyun if (dat_entry)
1280*4882a593Smuzhiyun batadv_dat_entry_put(dat_entry);
1281*4882a593Smuzhiyun return ret;
1282*4882a593Smuzhiyun }
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun /**
1285*4882a593Smuzhiyun * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
1286*4882a593Smuzhiyun * answer using the local DAT storage
1287*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1288*4882a593Smuzhiyun * @skb: packet to check
1289*4882a593Smuzhiyun * @hdr_size: size of the encapsulation header
1290*4882a593Smuzhiyun *
1291*4882a593Smuzhiyun * Return: true if the request has been answered, false otherwise.
1292*4882a593Smuzhiyun */
batadv_dat_snoop_incoming_arp_request(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)1293*4882a593Smuzhiyun bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1294*4882a593Smuzhiyun struct sk_buff *skb, int hdr_size)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun u16 type;
1297*4882a593Smuzhiyun __be32 ip_src, ip_dst;
1298*4882a593Smuzhiyun u8 *hw_src;
1299*4882a593Smuzhiyun struct sk_buff *skb_new;
1300*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry = NULL;
1301*4882a593Smuzhiyun bool ret = false;
1302*4882a593Smuzhiyun unsigned short vid;
1303*4882a593Smuzhiyun int err;
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun if (!atomic_read(&bat_priv->distributed_arp_table))
1306*4882a593Smuzhiyun goto out;
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun vid = batadv_dat_get_vid(skb, &hdr_size);
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1311*4882a593Smuzhiyun if (type != ARPOP_REQUEST)
1312*4882a593Smuzhiyun goto out;
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun hw_src = batadv_arp_hw_src(skb, hdr_size);
1315*4882a593Smuzhiyun ip_src = batadv_arp_ip_src(skb, hdr_size);
1316*4882a593Smuzhiyun ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
1319*4882a593Smuzhiyun
1320*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1321*4882a593Smuzhiyun
1322*4882a593Smuzhiyun dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1323*4882a593Smuzhiyun if (!dat_entry)
1324*4882a593Smuzhiyun goto out;
1325*4882a593Smuzhiyun
1326*4882a593Smuzhiyun skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1327*4882a593Smuzhiyun dat_entry->mac_addr, hw_src, vid);
1328*4882a593Smuzhiyun if (!skb_new)
1329*4882a593Smuzhiyun goto out;
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun /* To preserve backwards compatibility, the node has choose the outgoing
1332*4882a593Smuzhiyun * format based on the incoming request packet type. The assumption is
1333*4882a593Smuzhiyun * that a node not using the 4addr packet format doesn't support it.
1334*4882a593Smuzhiyun */
1335*4882a593Smuzhiyun if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
1336*4882a593Smuzhiyun err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1337*4882a593Smuzhiyun BATADV_P_DAT_CACHE_REPLY,
1338*4882a593Smuzhiyun NULL, vid);
1339*4882a593Smuzhiyun else
1340*4882a593Smuzhiyun err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
1341*4882a593Smuzhiyun
1342*4882a593Smuzhiyun if (err != NET_XMIT_DROP) {
1343*4882a593Smuzhiyun batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
1344*4882a593Smuzhiyun ret = true;
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun out:
1347*4882a593Smuzhiyun if (dat_entry)
1348*4882a593Smuzhiyun batadv_dat_entry_put(dat_entry);
1349*4882a593Smuzhiyun if (ret)
1350*4882a593Smuzhiyun kfree_skb(skb);
1351*4882a593Smuzhiyun return ret;
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun /**
1355*4882a593Smuzhiyun * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
1356*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1357*4882a593Smuzhiyun * @skb: packet to check
1358*4882a593Smuzhiyun */
batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv * bat_priv,struct sk_buff * skb)1359*4882a593Smuzhiyun void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1360*4882a593Smuzhiyun struct sk_buff *skb)
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun u16 type;
1363*4882a593Smuzhiyun __be32 ip_src, ip_dst;
1364*4882a593Smuzhiyun u8 *hw_src, *hw_dst;
1365*4882a593Smuzhiyun int hdr_size = 0;
1366*4882a593Smuzhiyun unsigned short vid;
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun if (!atomic_read(&bat_priv->distributed_arp_table))
1369*4882a593Smuzhiyun return;
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun vid = batadv_dat_get_vid(skb, &hdr_size);
1372*4882a593Smuzhiyun
1373*4882a593Smuzhiyun type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1374*4882a593Smuzhiyun if (type != ARPOP_REPLY)
1375*4882a593Smuzhiyun return;
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
1378*4882a593Smuzhiyun
1379*4882a593Smuzhiyun hw_src = batadv_arp_hw_src(skb, hdr_size);
1380*4882a593Smuzhiyun ip_src = batadv_arp_ip_src(skb, hdr_size);
1381*4882a593Smuzhiyun hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1382*4882a593Smuzhiyun ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1385*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1386*4882a593Smuzhiyun
1387*4882a593Smuzhiyun /* Send the ARP reply to the candidates for both the IP addresses that
1388*4882a593Smuzhiyun * the node obtained from the ARP reply
1389*4882a593Smuzhiyun */
1390*4882a593Smuzhiyun batadv_dat_forward_data(bat_priv, skb, ip_src, vid,
1391*4882a593Smuzhiyun BATADV_P_DAT_DHT_PUT);
1392*4882a593Smuzhiyun batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1393*4882a593Smuzhiyun BATADV_P_DAT_DHT_PUT);
1394*4882a593Smuzhiyun }
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun /**
1397*4882a593Smuzhiyun * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1398*4882a593Smuzhiyun * local DAT storage only
1399*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1400*4882a593Smuzhiyun * @skb: packet to check
1401*4882a593Smuzhiyun * @hdr_size: size of the encapsulation header
1402*4882a593Smuzhiyun *
1403*4882a593Smuzhiyun * Return: true if the packet was snooped and consumed by DAT. False if the
1404*4882a593Smuzhiyun * packet has to be delivered to the interface
1405*4882a593Smuzhiyun */
batadv_dat_snoop_incoming_arp_reply(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)1406*4882a593Smuzhiyun bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1407*4882a593Smuzhiyun struct sk_buff *skb, int hdr_size)
1408*4882a593Smuzhiyun {
1409*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry = NULL;
1410*4882a593Smuzhiyun u16 type;
1411*4882a593Smuzhiyun __be32 ip_src, ip_dst;
1412*4882a593Smuzhiyun u8 *hw_src, *hw_dst;
1413*4882a593Smuzhiyun bool dropped = false;
1414*4882a593Smuzhiyun unsigned short vid;
1415*4882a593Smuzhiyun
1416*4882a593Smuzhiyun if (!atomic_read(&bat_priv->distributed_arp_table))
1417*4882a593Smuzhiyun goto out;
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun vid = batadv_dat_get_vid(skb, &hdr_size);
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1422*4882a593Smuzhiyun if (type != ARPOP_REPLY)
1423*4882a593Smuzhiyun goto out;
1424*4882a593Smuzhiyun
1425*4882a593Smuzhiyun batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun hw_src = batadv_arp_hw_src(skb, hdr_size);
1428*4882a593Smuzhiyun ip_src = batadv_arp_ip_src(skb, hdr_size);
1429*4882a593Smuzhiyun hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1430*4882a593Smuzhiyun ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun /* If ip_dst is already in cache and has the right mac address,
1433*4882a593Smuzhiyun * drop this frame if this ARP reply is destined for us because it's
1434*4882a593Smuzhiyun * most probably an ARP reply generated by another node of the DHT.
1435*4882a593Smuzhiyun * We have most probably received already a reply earlier. Delivering
1436*4882a593Smuzhiyun * this frame would lead to doubled receive of an ARP reply.
1437*4882a593Smuzhiyun */
1438*4882a593Smuzhiyun dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1439*4882a593Smuzhiyun if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
1440*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1441*4882a593Smuzhiyun hw_src, &ip_src, hw_dst, &ip_dst,
1442*4882a593Smuzhiyun dat_entry->mac_addr, &dat_entry->ip);
1443*4882a593Smuzhiyun dropped = true;
1444*4882a593Smuzhiyun }
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun /* Update our internal cache with both the IP addresses the node got
1447*4882a593Smuzhiyun * within the ARP reply
1448*4882a593Smuzhiyun */
1449*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1450*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1451*4882a593Smuzhiyun
1452*4882a593Smuzhiyun if (dropped)
1453*4882a593Smuzhiyun goto out;
1454*4882a593Smuzhiyun
1455*4882a593Smuzhiyun /* If BLA is enabled, only forward ARP replies if we have claimed the
1456*4882a593Smuzhiyun * source of the ARP reply or if no one else of the same backbone has
1457*4882a593Smuzhiyun * already claimed that client. This prevents that different gateways
1458*4882a593Smuzhiyun * to the same backbone all forward the ARP reply leading to multiple
1459*4882a593Smuzhiyun * replies in the backbone.
1460*4882a593Smuzhiyun */
1461*4882a593Smuzhiyun if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1462*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
1463*4882a593Smuzhiyun "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1464*4882a593Smuzhiyun hw_src);
1465*4882a593Smuzhiyun dropped = true;
1466*4882a593Smuzhiyun goto out;
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun
1469*4882a593Smuzhiyun /* if this REPLY is directed to a client of mine, let's deliver the
1470*4882a593Smuzhiyun * packet to the interface
1471*4882a593Smuzhiyun */
1472*4882a593Smuzhiyun dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun /* if this REPLY is sent on behalf of a client of mine, let's drop the
1475*4882a593Smuzhiyun * packet because the client will reply by itself
1476*4882a593Smuzhiyun */
1477*4882a593Smuzhiyun dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
1478*4882a593Smuzhiyun out:
1479*4882a593Smuzhiyun if (dropped)
1480*4882a593Smuzhiyun kfree_skb(skb);
1481*4882a593Smuzhiyun if (dat_entry)
1482*4882a593Smuzhiyun batadv_dat_entry_put(dat_entry);
1483*4882a593Smuzhiyun /* if dropped == false -> deliver to the interface */
1484*4882a593Smuzhiyun return dropped;
1485*4882a593Smuzhiyun }
1486*4882a593Smuzhiyun
1487*4882a593Smuzhiyun /**
1488*4882a593Smuzhiyun * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
1489*4882a593Smuzhiyun * @skb: the packet to check
1490*4882a593Smuzhiyun * @ip_src: a buffer to store the IPv4 source address in
1491*4882a593Smuzhiyun *
1492*4882a593Smuzhiyun * Checks whether the given skb has an IP and UDP header valid for a DHCP
1493*4882a593Smuzhiyun * message from a DHCP server. And if so, stores the IPv4 source address in
1494*4882a593Smuzhiyun * the provided buffer.
1495*4882a593Smuzhiyun *
1496*4882a593Smuzhiyun * Return: True if valid, false otherwise.
1497*4882a593Smuzhiyun */
1498*4882a593Smuzhiyun static bool
batadv_dat_check_dhcp_ipudp(struct sk_buff * skb,__be32 * ip_src)1499*4882a593Smuzhiyun batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
1500*4882a593Smuzhiyun {
1501*4882a593Smuzhiyun unsigned int offset = skb_network_offset(skb);
1502*4882a593Smuzhiyun struct udphdr *udphdr, _udphdr;
1503*4882a593Smuzhiyun struct iphdr *iphdr, _iphdr;
1504*4882a593Smuzhiyun
1505*4882a593Smuzhiyun iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
1506*4882a593Smuzhiyun if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
1507*4882a593Smuzhiyun return false;
1508*4882a593Smuzhiyun
1509*4882a593Smuzhiyun if (iphdr->protocol != IPPROTO_UDP)
1510*4882a593Smuzhiyun return false;
1511*4882a593Smuzhiyun
1512*4882a593Smuzhiyun offset += iphdr->ihl * 4;
1513*4882a593Smuzhiyun skb_set_transport_header(skb, offset);
1514*4882a593Smuzhiyun
1515*4882a593Smuzhiyun udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
1516*4882a593Smuzhiyun if (!udphdr || udphdr->source != htons(67))
1517*4882a593Smuzhiyun return false;
1518*4882a593Smuzhiyun
1519*4882a593Smuzhiyun *ip_src = get_unaligned(&iphdr->saddr);
1520*4882a593Smuzhiyun
1521*4882a593Smuzhiyun return true;
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun
1524*4882a593Smuzhiyun /**
1525*4882a593Smuzhiyun * batadv_dat_check_dhcp() - examine packet for valid DHCP message
1526*4882a593Smuzhiyun * @skb: the packet to check
1527*4882a593Smuzhiyun * @proto: ethernet protocol hint (behind a potential vlan)
1528*4882a593Smuzhiyun * @ip_src: a buffer to store the IPv4 source address in
1529*4882a593Smuzhiyun *
1530*4882a593Smuzhiyun * Checks whether the given skb is a valid DHCP packet. And if so, stores the
1531*4882a593Smuzhiyun * IPv4 source address in the provided buffer.
1532*4882a593Smuzhiyun *
1533*4882a593Smuzhiyun * Caller needs to ensure that the skb network header is set correctly.
1534*4882a593Smuzhiyun *
1535*4882a593Smuzhiyun * Return: If skb is a valid DHCP packet, then returns its op code
1536*4882a593Smuzhiyun * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
1537*4882a593Smuzhiyun */
1538*4882a593Smuzhiyun static int
batadv_dat_check_dhcp(struct sk_buff * skb,__be16 proto,__be32 * ip_src)1539*4882a593Smuzhiyun batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
1540*4882a593Smuzhiyun {
1541*4882a593Smuzhiyun __be32 *magic, _magic;
1542*4882a593Smuzhiyun unsigned int offset;
1543*4882a593Smuzhiyun struct {
1544*4882a593Smuzhiyun __u8 op;
1545*4882a593Smuzhiyun __u8 htype;
1546*4882a593Smuzhiyun __u8 hlen;
1547*4882a593Smuzhiyun __u8 hops;
1548*4882a593Smuzhiyun } *dhcp_h, _dhcp_h;
1549*4882a593Smuzhiyun
1550*4882a593Smuzhiyun if (proto != htons(ETH_P_IP))
1551*4882a593Smuzhiyun return -EINVAL;
1552*4882a593Smuzhiyun
1553*4882a593Smuzhiyun if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
1554*4882a593Smuzhiyun return -EINVAL;
1555*4882a593Smuzhiyun
1556*4882a593Smuzhiyun offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1557*4882a593Smuzhiyun if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
1558*4882a593Smuzhiyun return -EINVAL;
1559*4882a593Smuzhiyun
1560*4882a593Smuzhiyun dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
1561*4882a593Smuzhiyun if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
1562*4882a593Smuzhiyun dhcp_h->hlen != ETH_ALEN)
1563*4882a593Smuzhiyun return -EINVAL;
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun offset += offsetof(struct batadv_dhcp_packet, magic);
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
1568*4882a593Smuzhiyun if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
1569*4882a593Smuzhiyun return -EINVAL;
1570*4882a593Smuzhiyun
1571*4882a593Smuzhiyun return dhcp_h->op;
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun /**
1575*4882a593Smuzhiyun * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
1576*4882a593Smuzhiyun * @skb: the DHCP packet to parse
1577*4882a593Smuzhiyun *
1578*4882a593Smuzhiyun * Iterates over the DHCP options of the given DHCP packet to find a
1579*4882a593Smuzhiyun * DHCP Message Type option and parse it.
1580*4882a593Smuzhiyun *
1581*4882a593Smuzhiyun * Caller needs to ensure that the given skb is a valid DHCP packet and
1582*4882a593Smuzhiyun * that the skb transport header is set correctly.
1583*4882a593Smuzhiyun *
1584*4882a593Smuzhiyun * Return: The found DHCP message type value, if found. -EINVAL otherwise.
1585*4882a593Smuzhiyun */
batadv_dat_get_dhcp_message_type(struct sk_buff * skb)1586*4882a593Smuzhiyun static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
1587*4882a593Smuzhiyun {
1588*4882a593Smuzhiyun unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1589*4882a593Smuzhiyun u8 *type, _type;
1590*4882a593Smuzhiyun struct {
1591*4882a593Smuzhiyun u8 type;
1592*4882a593Smuzhiyun u8 len;
1593*4882a593Smuzhiyun } *tl, _tl;
1594*4882a593Smuzhiyun
1595*4882a593Smuzhiyun offset += sizeof(struct batadv_dhcp_packet);
1596*4882a593Smuzhiyun
1597*4882a593Smuzhiyun while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
1598*4882a593Smuzhiyun if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
1599*4882a593Smuzhiyun break;
1600*4882a593Smuzhiyun
1601*4882a593Smuzhiyun if (tl->type == BATADV_DHCP_OPT_END)
1602*4882a593Smuzhiyun break;
1603*4882a593Smuzhiyun
1604*4882a593Smuzhiyun if (tl->type == BATADV_DHCP_OPT_PAD)
1605*4882a593Smuzhiyun offset++;
1606*4882a593Smuzhiyun else
1607*4882a593Smuzhiyun offset += tl->len + sizeof(_tl);
1608*4882a593Smuzhiyun }
1609*4882a593Smuzhiyun
1610*4882a593Smuzhiyun /* Option Overload Code not supported */
1611*4882a593Smuzhiyun if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
1612*4882a593Smuzhiyun tl->len != sizeof(_type))
1613*4882a593Smuzhiyun return -EINVAL;
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun offset += sizeof(_tl);
1616*4882a593Smuzhiyun
1617*4882a593Smuzhiyun type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
1618*4882a593Smuzhiyun if (!type)
1619*4882a593Smuzhiyun return -EINVAL;
1620*4882a593Smuzhiyun
1621*4882a593Smuzhiyun return *type;
1622*4882a593Smuzhiyun }
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun /**
1625*4882a593Smuzhiyun * batadv_dat_get_dhcp_yiaddr() - get yiaddr from a DHCP packet
1626*4882a593Smuzhiyun * @skb: the DHCP packet to parse
1627*4882a593Smuzhiyun * @buf: a buffer to store the yiaddr in
1628*4882a593Smuzhiyun *
1629*4882a593Smuzhiyun * Caller needs to ensure that the given skb is a valid DHCP packet and
1630*4882a593Smuzhiyun * that the skb transport header is set correctly.
1631*4882a593Smuzhiyun *
1632*4882a593Smuzhiyun * Return: True on success, false otherwise.
1633*4882a593Smuzhiyun */
batadv_dat_dhcp_get_yiaddr(struct sk_buff * skb,__be32 * buf)1634*4882a593Smuzhiyun static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
1635*4882a593Smuzhiyun {
1636*4882a593Smuzhiyun unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1637*4882a593Smuzhiyun __be32 *yiaddr;
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun offset += offsetof(struct batadv_dhcp_packet, yiaddr);
1640*4882a593Smuzhiyun yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
1641*4882a593Smuzhiyun
1642*4882a593Smuzhiyun if (!yiaddr)
1643*4882a593Smuzhiyun return false;
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun if (yiaddr != buf)
1646*4882a593Smuzhiyun *buf = get_unaligned(yiaddr);
1647*4882a593Smuzhiyun
1648*4882a593Smuzhiyun return true;
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun
1651*4882a593Smuzhiyun /**
1652*4882a593Smuzhiyun * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
1653*4882a593Smuzhiyun * @skb: the DHCP packet to parse
1654*4882a593Smuzhiyun * @buf: a buffer to store the chaddr in
1655*4882a593Smuzhiyun *
1656*4882a593Smuzhiyun * Caller needs to ensure that the given skb is a valid DHCP packet and
1657*4882a593Smuzhiyun * that the skb transport header is set correctly.
1658*4882a593Smuzhiyun *
1659*4882a593Smuzhiyun * Return: True on success, false otherwise
1660*4882a593Smuzhiyun */
batadv_dat_get_dhcp_chaddr(struct sk_buff * skb,u8 * buf)1661*4882a593Smuzhiyun static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
1662*4882a593Smuzhiyun {
1663*4882a593Smuzhiyun unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1664*4882a593Smuzhiyun u8 *chaddr;
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun offset += offsetof(struct batadv_dhcp_packet, chaddr);
1667*4882a593Smuzhiyun chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun if (!chaddr)
1670*4882a593Smuzhiyun return false;
1671*4882a593Smuzhiyun
1672*4882a593Smuzhiyun if (chaddr != buf)
1673*4882a593Smuzhiyun memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
1674*4882a593Smuzhiyun
1675*4882a593Smuzhiyun return true;
1676*4882a593Smuzhiyun }
1677*4882a593Smuzhiyun
1678*4882a593Smuzhiyun /**
1679*4882a593Smuzhiyun * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
1680*4882a593Smuzhiyun * DAT cache
1681*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1682*4882a593Smuzhiyun * @chaddr: the DHCP client MAC address
1683*4882a593Smuzhiyun * @yiaddr: the DHCP client IP address
1684*4882a593Smuzhiyun * @hw_dst: the DHCP server MAC address
1685*4882a593Smuzhiyun * @ip_dst: the DHCP server IP address
1686*4882a593Smuzhiyun * @vid: VLAN identifier
1687*4882a593Smuzhiyun *
1688*4882a593Smuzhiyun * Adds given MAC/IP pairs to the local DAT cache and propagates them further
1689*4882a593Smuzhiyun * into the DHT.
1690*4882a593Smuzhiyun *
1691*4882a593Smuzhiyun * For the DHT propagation, client MAC + IP will appear as the ARP Reply
1692*4882a593Smuzhiyun * transmitter (and hw_dst/ip_dst as the target).
1693*4882a593Smuzhiyun */
batadv_dat_put_dhcp(struct batadv_priv * bat_priv,u8 * chaddr,__be32 yiaddr,u8 * hw_dst,__be32 ip_dst,unsigned short vid)1694*4882a593Smuzhiyun static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
1695*4882a593Smuzhiyun __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
1696*4882a593Smuzhiyun unsigned short vid)
1697*4882a593Smuzhiyun {
1698*4882a593Smuzhiyun struct sk_buff *skb;
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
1701*4882a593Smuzhiyun hw_dst, vid);
1702*4882a593Smuzhiyun if (!skb)
1703*4882a593Smuzhiyun return;
1704*4882a593Smuzhiyun
1705*4882a593Smuzhiyun skb_set_network_header(skb, ETH_HLEN);
1706*4882a593Smuzhiyun
1707*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1708*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1709*4882a593Smuzhiyun
1710*4882a593Smuzhiyun batadv_dat_forward_data(bat_priv, skb, yiaddr, vid,
1711*4882a593Smuzhiyun BATADV_P_DAT_DHT_PUT);
1712*4882a593Smuzhiyun batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1713*4882a593Smuzhiyun BATADV_P_DAT_DHT_PUT);
1714*4882a593Smuzhiyun
1715*4882a593Smuzhiyun consume_skb(skb);
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
1718*4882a593Smuzhiyun "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1719*4882a593Smuzhiyun &ip_dst, hw_dst, batadv_print_vid(vid));
1720*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
1721*4882a593Smuzhiyun "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1722*4882a593Smuzhiyun &yiaddr, chaddr, batadv_print_vid(vid));
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun
1725*4882a593Smuzhiyun /**
1726*4882a593Smuzhiyun * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
1727*4882a593Smuzhiyun * @skb: the packet to check
1728*4882a593Smuzhiyun * @proto: ethernet protocol hint (behind a potential vlan)
1729*4882a593Smuzhiyun * @ip_src: a buffer to store the IPv4 source address in
1730*4882a593Smuzhiyun * @chaddr: a buffer to store the DHCP Client Hardware Address in
1731*4882a593Smuzhiyun * @yiaddr: a buffer to store the DHCP Your IP Address in
1732*4882a593Smuzhiyun *
1733*4882a593Smuzhiyun * Checks whether the given skb is a valid DHCPACK. And if so, stores the
1734*4882a593Smuzhiyun * IPv4 server source address (ip_src), client MAC address (chaddr) and client
1735*4882a593Smuzhiyun * IPv4 address (yiaddr) in the provided buffers.
1736*4882a593Smuzhiyun *
1737*4882a593Smuzhiyun * Caller needs to ensure that the skb network header is set correctly.
1738*4882a593Smuzhiyun *
1739*4882a593Smuzhiyun * Return: True if the skb is a valid DHCPACK. False otherwise.
1740*4882a593Smuzhiyun */
1741*4882a593Smuzhiyun static bool
batadv_dat_check_dhcp_ack(struct sk_buff * skb,__be16 proto,__be32 * ip_src,u8 * chaddr,__be32 * yiaddr)1742*4882a593Smuzhiyun batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
1743*4882a593Smuzhiyun u8 *chaddr, __be32 *yiaddr)
1744*4882a593Smuzhiyun {
1745*4882a593Smuzhiyun int type;
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun type = batadv_dat_check_dhcp(skb, proto, ip_src);
1748*4882a593Smuzhiyun if (type != BATADV_BOOTREPLY)
1749*4882a593Smuzhiyun return false;
1750*4882a593Smuzhiyun
1751*4882a593Smuzhiyun type = batadv_dat_get_dhcp_message_type(skb);
1752*4882a593Smuzhiyun if (type != BATADV_DHCPACK)
1753*4882a593Smuzhiyun return false;
1754*4882a593Smuzhiyun
1755*4882a593Smuzhiyun if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
1756*4882a593Smuzhiyun return false;
1757*4882a593Smuzhiyun
1758*4882a593Smuzhiyun if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
1759*4882a593Smuzhiyun return false;
1760*4882a593Smuzhiyun
1761*4882a593Smuzhiyun return true;
1762*4882a593Smuzhiyun }
1763*4882a593Smuzhiyun
1764*4882a593Smuzhiyun /**
1765*4882a593Smuzhiyun * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
1766*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1767*4882a593Smuzhiyun * @skb: the packet to snoop
1768*4882a593Smuzhiyun * @proto: ethernet protocol hint (behind a potential vlan)
1769*4882a593Smuzhiyun * @vid: VLAN identifier
1770*4882a593Smuzhiyun *
1771*4882a593Smuzhiyun * This function first checks whether the given skb is a valid DHCPACK. If
1772*4882a593Smuzhiyun * so then its source MAC and IP as well as its DHCP Client Hardware Address
1773*4882a593Smuzhiyun * field and DHCP Your IP Address field are added to the local DAT cache and
1774*4882a593Smuzhiyun * propagated into the DHT.
1775*4882a593Smuzhiyun *
1776*4882a593Smuzhiyun * Caller needs to ensure that the skb mac and network headers are set
1777*4882a593Smuzhiyun * correctly.
1778*4882a593Smuzhiyun */
batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv * bat_priv,struct sk_buff * skb,__be16 proto,unsigned short vid)1779*4882a593Smuzhiyun void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
1780*4882a593Smuzhiyun struct sk_buff *skb,
1781*4882a593Smuzhiyun __be16 proto,
1782*4882a593Smuzhiyun unsigned short vid)
1783*4882a593Smuzhiyun {
1784*4882a593Smuzhiyun u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1785*4882a593Smuzhiyun __be32 ip_src, yiaddr;
1786*4882a593Smuzhiyun
1787*4882a593Smuzhiyun if (!atomic_read(&bat_priv->distributed_arp_table))
1788*4882a593Smuzhiyun return;
1789*4882a593Smuzhiyun
1790*4882a593Smuzhiyun if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1791*4882a593Smuzhiyun return;
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
1794*4882a593Smuzhiyun ip_src, vid);
1795*4882a593Smuzhiyun }
1796*4882a593Smuzhiyun
1797*4882a593Smuzhiyun /**
1798*4882a593Smuzhiyun * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
1799*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1800*4882a593Smuzhiyun * @skb: the packet to snoop
1801*4882a593Smuzhiyun * @hdr_size: header size, up to the tail of the batman-adv header
1802*4882a593Smuzhiyun *
1803*4882a593Smuzhiyun * This function first checks whether the given skb is a valid DHCPACK. If
1804*4882a593Smuzhiyun * so then its source MAC and IP as well as its DHCP Client Hardware Address
1805*4882a593Smuzhiyun * field and DHCP Your IP Address field are added to the local DAT cache.
1806*4882a593Smuzhiyun */
batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)1807*4882a593Smuzhiyun void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
1808*4882a593Smuzhiyun struct sk_buff *skb, int hdr_size)
1809*4882a593Smuzhiyun {
1810*4882a593Smuzhiyun u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1811*4882a593Smuzhiyun struct ethhdr *ethhdr;
1812*4882a593Smuzhiyun __be32 ip_src, yiaddr;
1813*4882a593Smuzhiyun unsigned short vid;
1814*4882a593Smuzhiyun __be16 proto;
1815*4882a593Smuzhiyun u8 *hw_src;
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun if (!atomic_read(&bat_priv->distributed_arp_table))
1818*4882a593Smuzhiyun return;
1819*4882a593Smuzhiyun
1820*4882a593Smuzhiyun if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1821*4882a593Smuzhiyun return;
1822*4882a593Smuzhiyun
1823*4882a593Smuzhiyun ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1824*4882a593Smuzhiyun skb_set_network_header(skb, hdr_size + ETH_HLEN);
1825*4882a593Smuzhiyun proto = ethhdr->h_proto;
1826*4882a593Smuzhiyun
1827*4882a593Smuzhiyun if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1828*4882a593Smuzhiyun return;
1829*4882a593Smuzhiyun
1830*4882a593Smuzhiyun hw_src = ethhdr->h_source;
1831*4882a593Smuzhiyun vid = batadv_dat_get_vid(skb, &hdr_size);
1832*4882a593Smuzhiyun
1833*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1834*4882a593Smuzhiyun batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
1837*4882a593Smuzhiyun "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1838*4882a593Smuzhiyun &ip_src, hw_src, batadv_print_vid(vid));
1839*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
1840*4882a593Smuzhiyun "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1841*4882a593Smuzhiyun &yiaddr, chaddr, batadv_print_vid(vid));
1842*4882a593Smuzhiyun }
1843*4882a593Smuzhiyun
1844*4882a593Smuzhiyun /**
1845*4882a593Smuzhiyun * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1846*4882a593Smuzhiyun * dropped (because the node has already obtained the reply via DAT) or not
1847*4882a593Smuzhiyun * @bat_priv: the bat priv with all the soft interface information
1848*4882a593Smuzhiyun * @forw_packet: the broadcast packet
1849*4882a593Smuzhiyun *
1850*4882a593Smuzhiyun * Return: true if the node can drop the packet, false otherwise.
1851*4882a593Smuzhiyun */
batadv_dat_drop_broadcast_packet(struct batadv_priv * bat_priv,struct batadv_forw_packet * forw_packet)1852*4882a593Smuzhiyun bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1853*4882a593Smuzhiyun struct batadv_forw_packet *forw_packet)
1854*4882a593Smuzhiyun {
1855*4882a593Smuzhiyun u16 type;
1856*4882a593Smuzhiyun __be32 ip_dst;
1857*4882a593Smuzhiyun struct batadv_dat_entry *dat_entry = NULL;
1858*4882a593Smuzhiyun bool ret = false;
1859*4882a593Smuzhiyun int hdr_size = sizeof(struct batadv_bcast_packet);
1860*4882a593Smuzhiyun unsigned short vid;
1861*4882a593Smuzhiyun
1862*4882a593Smuzhiyun if (!atomic_read(&bat_priv->distributed_arp_table))
1863*4882a593Smuzhiyun goto out;
1864*4882a593Smuzhiyun
1865*4882a593Smuzhiyun /* If this packet is an ARP_REQUEST and the node already has the
1866*4882a593Smuzhiyun * information that it is going to ask, then the packet can be dropped
1867*4882a593Smuzhiyun */
1868*4882a593Smuzhiyun if (batadv_forw_packet_is_rebroadcast(forw_packet))
1869*4882a593Smuzhiyun goto out;
1870*4882a593Smuzhiyun
1871*4882a593Smuzhiyun vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1872*4882a593Smuzhiyun
1873*4882a593Smuzhiyun type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1874*4882a593Smuzhiyun if (type != ARPOP_REQUEST)
1875*4882a593Smuzhiyun goto out;
1876*4882a593Smuzhiyun
1877*4882a593Smuzhiyun ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1878*4882a593Smuzhiyun dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1879*4882a593Smuzhiyun /* check if the node already got this entry */
1880*4882a593Smuzhiyun if (!dat_entry) {
1881*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
1882*4882a593Smuzhiyun "ARP Request for %pI4: fallback\n", &ip_dst);
1883*4882a593Smuzhiyun goto out;
1884*4882a593Smuzhiyun }
1885*4882a593Smuzhiyun
1886*4882a593Smuzhiyun batadv_dbg(BATADV_DBG_DAT, bat_priv,
1887*4882a593Smuzhiyun "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1888*4882a593Smuzhiyun ret = true;
1889*4882a593Smuzhiyun
1890*4882a593Smuzhiyun out:
1891*4882a593Smuzhiyun if (dat_entry)
1892*4882a593Smuzhiyun batadv_dat_entry_put(dat_entry);
1893*4882a593Smuzhiyun return ret;
1894*4882a593Smuzhiyun }
1895