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