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