Sven Eckelmann | 7db7d9f | 2017-11-19 15:05:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sven Eckelmann | 6b1aea8 | 2018-01-01 00:00:00 +0100 | [diff] [blame] | 2 | /* Copyright (C) 2009-2018 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Marek Lindner |
| 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/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 19 | #include "gateway_client.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 20 | #include "main.h" |
| 21 | |
| 22 | #include <linux/atomic.h> |
| 23 | #include <linux/byteorder/generic.h> |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 24 | #include <linux/errno.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 25 | #include <linux/etherdevice.h> |
Sven Eckelmann | b92b94a | 2017-11-19 17:12:02 +0100 | [diff] [blame] | 26 | #include <linux/gfp.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 27 | #include <linux/if_ether.h> |
| 28 | #include <linux/if_vlan.h> |
| 29 | #include <linux/in.h> |
| 30 | #include <linux/ip.h> |
| 31 | #include <linux/ipv6.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 32 | #include <linux/kernel.h> |
Sven Eckelmann | e7aed32 | 2016-01-16 10:29:41 +0100 | [diff] [blame] | 33 | #include <linux/kref.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 34 | #include <linux/list.h> |
Sven Eckelmann | dff9bc4 | 2018-08-12 21:04:41 +0200 | [diff] [blame] | 35 | #include <linux/lockdep.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 36 | #include <linux/netdevice.h> |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 37 | #include <linux/netlink.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 38 | #include <linux/rculist.h> |
| 39 | #include <linux/rcupdate.h> |
| 40 | #include <linux/seq_file.h> |
| 41 | #include <linux/skbuff.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <linux/spinlock.h> |
| 44 | #include <linux/stddef.h> |
| 45 | #include <linux/udp.h> |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 46 | #include <net/sock.h> |
Sven Eckelmann | fec149f | 2017-12-21 10:17:41 +0100 | [diff] [blame] | 47 | #include <uapi/linux/batadv_packet.h> |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 48 | #include <uapi/linux/batman_adv.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 49 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 50 | #include "gateway_common.h" |
| 51 | #include "hard-interface.h" |
Sven Eckelmann | ba41208 | 2016-05-15 23:48:31 +0200 | [diff] [blame] | 52 | #include "log.h" |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 53 | #include "netlink.h" |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 54 | #include "originator.h" |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 55 | #include "routing.h" |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 56 | #include "soft-interface.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 57 | #include "sysfs.h" |
| 58 | #include "translation-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 59 | |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 60 | /* These are the offsets of the "hw type" and "hw address length" in the dhcp |
| 61 | * packet starting at the beginning of the dhcp header |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 62 | */ |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 63 | #define BATADV_DHCP_HTYPE_OFFSET 1 |
| 64 | #define BATADV_DHCP_HLEN_OFFSET 2 |
| 65 | /* Value of htype representing Ethernet */ |
| 66 | #define BATADV_DHCP_HTYPE_ETHERNET 0x01 |
| 67 | /* This is the offset of the "chaddr" field in the dhcp packet starting at the |
| 68 | * beginning of the dhcp header |
| 69 | */ |
| 70 | #define BATADV_DHCP_CHADDR_OFFSET 28 |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 71 | |
Sven Eckelmann | e7aed32 | 2016-01-16 10:29:41 +0100 | [diff] [blame] | 72 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 73 | * batadv_gw_node_release() - release gw_node from lists and queue for free |
| 74 | * after rcu grace period |
Sven Eckelmann | e7aed32 | 2016-01-16 10:29:41 +0100 | [diff] [blame] | 75 | * @ref: kref pointer of the gw_node |
| 76 | */ |
| 77 | static void batadv_gw_node_release(struct kref *ref) |
| 78 | { |
| 79 | struct batadv_gw_node *gw_node; |
| 80 | |
| 81 | gw_node = container_of(ref, struct batadv_gw_node, refcount); |
| 82 | |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 83 | batadv_orig_node_put(gw_node->orig_node); |
Sven Eckelmann | e7aed32 | 2016-01-16 10:29:41 +0100 | [diff] [blame] | 84 | kfree_rcu(gw_node, rcu); |
| 85 | } |
| 86 | |
| 87 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 88 | * batadv_gw_node_put() - decrement the gw_node refcounter and possibly release |
| 89 | * it |
Sven Eckelmann | e7aed32 | 2016-01-16 10:29:41 +0100 | [diff] [blame] | 90 | * @gw_node: gateway node to free |
| 91 | */ |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 92 | void batadv_gw_node_put(struct batadv_gw_node *gw_node) |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 93 | { |
Sven Eckelmann | e7aed32 | 2016-01-16 10:29:41 +0100 | [diff] [blame] | 94 | kref_put(&gw_node->refcount, batadv_gw_node_release); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 97 | /** |
| 98 | * batadv_gw_get_selected_gw_node() - Get currently selected gateway |
| 99 | * @bat_priv: the bat priv with all the soft interface information |
| 100 | * |
| 101 | * Return: selected gateway (with increased refcnt), NULL on errors |
| 102 | */ |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 103 | struct batadv_gw_node * |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 104 | batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 105 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 106 | struct batadv_gw_node *gw_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 107 | |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 108 | rcu_read_lock(); |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 109 | gw_node = rcu_dereference(bat_priv->gw.curr_gw); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 110 | if (!gw_node) |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 111 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 112 | |
Sven Eckelmann | e7aed32 | 2016-01-16 10:29:41 +0100 | [diff] [blame] | 113 | if (!kref_get_unless_zero(&gw_node->refcount)) |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 114 | gw_node = NULL; |
| 115 | |
| 116 | out: |
| 117 | rcu_read_unlock(); |
| 118 | return gw_node; |
| 119 | } |
| 120 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 121 | /** |
| 122 | * batadv_gw_get_selected_orig() - Get originator of currently selected gateway |
| 123 | * @bat_priv: the bat priv with all the soft interface information |
| 124 | * |
| 125 | * Return: orig_node of selected gateway (with increased refcnt), NULL on errors |
| 126 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 127 | struct batadv_orig_node * |
| 128 | batadv_gw_get_selected_orig(struct batadv_priv *bat_priv) |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 129 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 130 | struct batadv_gw_node *gw_node; |
| 131 | struct batadv_orig_node *orig_node = NULL; |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 132 | |
Sven Eckelmann | 1409a83 | 2012-05-12 18:33:55 +0200 | [diff] [blame] | 133 | gw_node = batadv_gw_get_selected_gw_node(bat_priv); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 134 | if (!gw_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 135 | goto out; |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 136 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 137 | rcu_read_lock(); |
| 138 | orig_node = gw_node->orig_node; |
| 139 | if (!orig_node) |
| 140 | goto unlock; |
| 141 | |
Sven Eckelmann | 7c12439 | 2016-01-16 10:29:56 +0100 | [diff] [blame] | 142 | if (!kref_get_unless_zero(&orig_node->refcount)) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 143 | orig_node = NULL; |
Linus Lüssing | 43c70ad | 2011-02-13 21:13:04 +0000 | [diff] [blame] | 144 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 145 | unlock: |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 146 | rcu_read_unlock(); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 147 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 148 | if (gw_node) |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 149 | batadv_gw_node_put(gw_node); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 150 | return orig_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 153 | static void batadv_gw_select(struct batadv_priv *bat_priv, |
| 154 | struct batadv_gw_node *new_gw_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 155 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 156 | struct batadv_gw_node *curr_gw_node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 157 | |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 158 | spin_lock_bh(&bat_priv->gw.list_lock); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 159 | |
Sven Eckelmann | a08d497 | 2016-03-05 16:09:22 +0100 | [diff] [blame] | 160 | if (new_gw_node) |
| 161 | kref_get(&new_gw_node->refcount); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 162 | |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 163 | curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1); |
| 164 | rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node); |
Marek Lindner | 25b6d3c | 2011-02-10 14:33:49 +0000 | [diff] [blame] | 165 | |
| 166 | if (curr_gw_node) |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 167 | batadv_gw_node_put(curr_gw_node); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 168 | |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 169 | spin_unlock_bh(&bat_priv->gw.list_lock); |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Antonio Quartulli | 4e820e7 | 2013-11-04 20:59:41 +0100 | [diff] [blame] | 172 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 173 | * batadv_gw_reselect() - force a gateway reselection |
Antonio Quartulli | 4e820e7 | 2013-11-04 20:59:41 +0100 | [diff] [blame] | 174 | * @bat_priv: the bat priv with all the soft interface information |
| 175 | * |
| 176 | * Set a flag to remind the GW component to perform a new gateway reselection. |
| 177 | * However this function does not ensure that the current gateway is going to be |
| 178 | * deselected. The reselection mechanism may elect the same gateway once again. |
| 179 | * |
| 180 | * This means that invoking batadv_gw_reselect() does not guarantee a gateway |
| 181 | * change and therefore a uevent is not necessarily expected. |
| 182 | */ |
| 183 | void batadv_gw_reselect(struct batadv_priv *bat_priv) |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 184 | { |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 185 | atomic_set(&bat_priv->gw.reselect, 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Antonio Quartulli | c6eaa3f | 2013-07-13 00:06:00 +0200 | [diff] [blame] | 188 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 189 | * batadv_gw_check_client_stop() - check if client mode has been switched off |
Antonio Quartulli | c6eaa3f | 2013-07-13 00:06:00 +0200 | [diff] [blame] | 190 | * @bat_priv: the bat priv with all the soft interface information |
| 191 | * |
| 192 | * This function assumes the caller has checked that the gw state *is actually |
| 193 | * changing*. This function is not supposed to be called when there is no state |
| 194 | * change. |
| 195 | */ |
| 196 | void batadv_gw_check_client_stop(struct batadv_priv *bat_priv) |
| 197 | { |
| 198 | struct batadv_gw_node *curr_gw; |
| 199 | |
Antonio Quartulli | 3a24a63 | 2016-05-06 02:46:38 +0800 | [diff] [blame] | 200 | if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT) |
Antonio Quartulli | c6eaa3f | 2013-07-13 00:06:00 +0200 | [diff] [blame] | 201 | return; |
| 202 | |
| 203 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); |
| 204 | if (!curr_gw) |
| 205 | return; |
| 206 | |
Antonio Quartulli | f316318 | 2013-11-04 20:59:40 +0100 | [diff] [blame] | 207 | /* deselect the current gateway so that next time that client mode is |
| 208 | * enabled a proper GW_ADD event can be sent |
| 209 | */ |
| 210 | batadv_gw_select(bat_priv, NULL); |
| 211 | |
Antonio Quartulli | c6eaa3f | 2013-07-13 00:06:00 +0200 | [diff] [blame] | 212 | /* if batman-adv is switching the gw client mode off and a gateway was |
| 213 | * already selected, send a DEL uevent |
| 214 | */ |
| 215 | batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL); |
| 216 | |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 217 | batadv_gw_node_put(curr_gw); |
Antonio Quartulli | c6eaa3f | 2013-07-13 00:06:00 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 220 | /** |
| 221 | * batadv_gw_election() - Elect the best gateway |
| 222 | * @bat_priv: the bat priv with all the soft interface information |
| 223 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 224 | void batadv_gw_election(struct batadv_priv *bat_priv) |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 225 | { |
Sven Eckelmann | 4f248cf | 2015-06-09 20:50:49 +0200 | [diff] [blame] | 226 | struct batadv_gw_node *curr_gw = NULL; |
| 227 | struct batadv_gw_node *next_gw = NULL; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 228 | struct batadv_neigh_node *router = NULL; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 229 | struct batadv_neigh_ifinfo *router_ifinfo = NULL; |
Antonio Quartulli | 19595e0 | 2011-06-12 11:58:58 +0200 | [diff] [blame] | 230 | char gw_addr[18] = { '\0' }; |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 231 | |
Antonio Quartulli | 3a24a63 | 2016-05-06 02:46:38 +0800 | [diff] [blame] | 232 | if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT) |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 233 | goto out; |
| 234 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 235 | if (!bat_priv->algo_ops->gw.get_best_gw_node) |
| 236 | goto out; |
| 237 | |
Sven Eckelmann | 1409a83 | 2012-05-12 18:33:55 +0200 | [diff] [blame] | 238 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 239 | |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 240 | if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw) |
Marek Lindner | caa0bf6 | 2012-08-04 04:13:26 +0000 | [diff] [blame] | 241 | goto out; |
| 242 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 243 | /* if gw.reselect is set to 1 it means that a previous call to |
| 244 | * gw.is_eligible() said that we have a new best GW, therefore it can |
| 245 | * now be picked from the list and selected |
| 246 | */ |
| 247 | next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 248 | |
| 249 | if (curr_gw == next_gw) |
| 250 | goto out; |
| 251 | |
| 252 | if (next_gw) { |
Antonio Quartulli | 19595e0 | 2011-06-12 11:58:58 +0200 | [diff] [blame] | 253 | sprintf(gw_addr, "%pM", next_gw->orig_node->orig); |
| 254 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 255 | router = batadv_orig_router_get(next_gw->orig_node, |
| 256 | BATADV_IF_DEFAULT); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 257 | if (!router) { |
Antonio Quartulli | 4e820e7 | 2013-11-04 20:59:41 +0100 | [diff] [blame] | 258 | batadv_gw_reselect(bat_priv); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 259 | goto out; |
| 260 | } |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 261 | |
| 262 | router_ifinfo = batadv_neigh_ifinfo_get(router, |
| 263 | BATADV_IF_DEFAULT); |
| 264 | if (!router_ifinfo) { |
| 265 | batadv_gw_reselect(bat_priv); |
| 266 | goto out; |
| 267 | } |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Sven Eckelmann | 825ffe1 | 2017-08-23 21:52:13 +0200 | [diff] [blame] | 270 | if (curr_gw && !next_gw) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 271 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 272 | "Removing selected gateway - no gateway in range\n"); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 273 | batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, |
| 274 | NULL); |
Sven Eckelmann | 825ffe1 | 2017-08-23 21:52:13 +0200 | [diff] [blame] | 275 | } else if (!curr_gw && next_gw) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 276 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 277 | "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 278 | next_gw->orig_node->orig, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 279 | next_gw->bandwidth_down / 10, |
| 280 | next_gw->bandwidth_down % 10, |
| 281 | next_gw->bandwidth_up / 10, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 282 | next_gw->bandwidth_up % 10, |
| 283 | router_ifinfo->bat_iv.tq_avg); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 284 | batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, |
| 285 | gw_addr); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 286 | } else { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 287 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 288 | "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 289 | next_gw->orig_node->orig, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 290 | next_gw->bandwidth_down / 10, |
| 291 | next_gw->bandwidth_down % 10, |
| 292 | next_gw->bandwidth_up / 10, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 293 | next_gw->bandwidth_up % 10, |
| 294 | router_ifinfo->bat_iv.tq_avg); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 295 | batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, |
| 296 | gw_addr); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Sven Eckelmann | 1409a83 | 2012-05-12 18:33:55 +0200 | [diff] [blame] | 299 | batadv_gw_select(bat_priv, next_gw); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 300 | |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 301 | out: |
| 302 | if (curr_gw) |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 303 | batadv_gw_node_put(curr_gw); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 304 | if (next_gw) |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 305 | batadv_gw_node_put(next_gw); |
Antonio Quartulli | 2265c14 | 2011-04-27 00:22:00 +0200 | [diff] [blame] | 306 | if (router) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 307 | batadv_neigh_node_put(router); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 308 | if (router_ifinfo) |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 309 | batadv_neigh_ifinfo_put(router_ifinfo); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 312 | /** |
| 313 | * batadv_gw_check_election() - Elect orig node as best gateway when eligible |
| 314 | * @bat_priv: the bat priv with all the soft interface information |
| 315 | * @orig_node: orig node which is to be checked |
| 316 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 317 | void batadv_gw_check_election(struct batadv_priv *bat_priv, |
| 318 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 319 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 320 | struct batadv_orig_node *curr_gw_orig; |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 321 | |
| 322 | /* abort immediately if the routing algorithm does not support gateway |
| 323 | * election |
| 324 | */ |
| 325 | if (!bat_priv->algo_ops->gw.is_eligible) |
| 326 | return; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 327 | |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 328 | curr_gw_orig = batadv_gw_get_selected_orig(bat_priv); |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 329 | if (!curr_gw_orig) |
Antonio Quartulli | 4e820e7 | 2013-11-04 20:59:41 +0100 | [diff] [blame] | 330 | goto reselect; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 331 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 332 | /* this node already is the gateway */ |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 333 | if (curr_gw_orig == orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 334 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 335 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 336 | if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig, |
| 337 | orig_node)) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 338 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 339 | |
Antonio Quartulli | 4e820e7 | 2013-11-04 20:59:41 +0100 | [diff] [blame] | 340 | reselect: |
| 341 | batadv_gw_reselect(bat_priv); |
Linus Lüssing | 5d02b3c | 2011-02-13 21:13:02 +0000 | [diff] [blame] | 342 | out: |
Linus Lüssing | 57f0c07 | 2011-03-14 22:43:33 +0000 | [diff] [blame] | 343 | if (curr_gw_orig) |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 344 | batadv_orig_node_put(curr_gw_orig); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 347 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 348 | * batadv_gw_node_add() - add gateway node to list of available gateways |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 349 | * @bat_priv: the bat priv with all the soft interface information |
| 350 | * @orig_node: originator announcing gateway capabilities |
| 351 | * @gateway: announced bandwidth information |
Sven Eckelmann | dff9bc4 | 2018-08-12 21:04:41 +0200 | [diff] [blame] | 352 | * |
| 353 | * Has to be called with the appropriate locks being acquired |
| 354 | * (gw.list_lock). |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 355 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 356 | static void batadv_gw_node_add(struct batadv_priv *bat_priv, |
| 357 | struct batadv_orig_node *orig_node, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 358 | struct batadv_tvlv_gateway_data *gateway) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 359 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 360 | struct batadv_gw_node *gw_node; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 361 | |
Sven Eckelmann | dff9bc4 | 2018-08-12 21:04:41 +0200 | [diff] [blame] | 362 | lockdep_assert_held(&bat_priv->gw.list_lock); |
| 363 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 364 | if (gateway->bandwidth_down == 0) |
| 365 | return; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 366 | |
Antonio Quartulli | 377fe0f | 2014-05-02 01:35:13 +0200 | [diff] [blame] | 367 | gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); |
Sven Eckelmann | c3ba37a7 | 2016-03-05 16:09:23 +0100 | [diff] [blame] | 368 | if (!gw_node) |
Antonio Quartulli | 377fe0f | 2014-05-02 01:35:13 +0200 | [diff] [blame] | 369 | return; |
Antonio Quartulli | 377fe0f | 2014-05-02 01:35:13 +0200 | [diff] [blame] | 370 | |
Sven Eckelmann | f665fa7 | 2016-07-15 17:39:27 +0200 | [diff] [blame] | 371 | kref_init(&gw_node->refcount); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 372 | INIT_HLIST_NODE(&gw_node->list); |
Sven Eckelmann | 55db2d5 | 2016-07-15 17:39:21 +0200 | [diff] [blame] | 373 | kref_get(&orig_node->refcount); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 374 | gw_node->orig_node = orig_node; |
Simon Wunderlich | 27a4d5e | 2015-06-24 14:50:19 +0200 | [diff] [blame] | 375 | gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); |
| 376 | gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 377 | |
Sven Eckelmann | f665fa7 | 2016-07-15 17:39:27 +0200 | [diff] [blame] | 378 | kref_get(&gw_node->refcount); |
Sven Eckelmann | 70ea5ce | 2016-07-27 12:31:08 +0200 | [diff] [blame] | 379 | hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list); |
Sven Eckelmann | 9264c85 | 2018-10-30 22:01:23 +0100 | [diff] [blame] | 380 | bat_priv->gw.generation++; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 381 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 382 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 383 | "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", |
| 384 | orig_node->orig, |
| 385 | ntohl(gateway->bandwidth_down) / 10, |
| 386 | ntohl(gateway->bandwidth_down) % 10, |
| 387 | ntohl(gateway->bandwidth_up) / 10, |
| 388 | ntohl(gateway->bandwidth_up) % 10); |
Sven Eckelmann | f665fa7 | 2016-07-15 17:39:27 +0200 | [diff] [blame] | 389 | |
| 390 | /* don't return reference to new gw_node */ |
| 391 | batadv_gw_node_put(gw_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 394 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 395 | * batadv_gw_node_get() - retrieve gateway node from list of available gateways |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 396 | * @bat_priv: the bat priv with all the soft interface information |
| 397 | * @orig_node: originator announcing gateway capabilities |
| 398 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 399 | * Return: gateway node if found or NULL otherwise. |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 400 | */ |
Antonio Quartulli | 50164d8 | 2016-07-03 12:46:34 +0200 | [diff] [blame] | 401 | struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv, |
| 402 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 403 | { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 404 | struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 405 | |
| 406 | rcu_read_lock(); |
Sven Eckelmann | 70ea5ce | 2016-07-27 12:31:08 +0200 | [diff] [blame] | 407 | hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list, |
| 408 | list) { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 409 | if (gw_node_tmp->orig_node != orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 410 | continue; |
| 411 | |
Sven Eckelmann | e7aed32 | 2016-01-16 10:29:41 +0100 | [diff] [blame] | 412 | if (!kref_get_unless_zero(&gw_node_tmp->refcount)) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 413 | continue; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 414 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 415 | gw_node = gw_node_tmp; |
| 416 | break; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 417 | } |
Marek Lindner | c4aac1a | 2011-03-23 11:24:34 +0100 | [diff] [blame] | 418 | rcu_read_unlock(); |
Antonio Quartulli | 71e4aa9 | 2011-04-25 22:44:32 +0200 | [diff] [blame] | 419 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 420 | return gw_node; |
| 421 | } |
| 422 | |
| 423 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 424 | * batadv_gw_node_update() - update list of available gateways with changed |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 425 | * bandwidth information |
| 426 | * @bat_priv: the bat priv with all the soft interface information |
| 427 | * @orig_node: originator announcing gateway capabilities |
| 428 | * @gateway: announced bandwidth information |
| 429 | */ |
| 430 | void batadv_gw_node_update(struct batadv_priv *bat_priv, |
| 431 | struct batadv_orig_node *orig_node, |
| 432 | struct batadv_tvlv_gateway_data *gateway) |
| 433 | { |
| 434 | struct batadv_gw_node *gw_node, *curr_gw = NULL; |
| 435 | |
Sven Eckelmann | dff9bc4 | 2018-08-12 21:04:41 +0200 | [diff] [blame] | 436 | spin_lock_bh(&bat_priv->gw.list_lock); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 437 | gw_node = batadv_gw_node_get(bat_priv, orig_node); |
| 438 | if (!gw_node) { |
| 439 | batadv_gw_node_add(bat_priv, orig_node, gateway); |
Sven Eckelmann | dff9bc4 | 2018-08-12 21:04:41 +0200 | [diff] [blame] | 440 | spin_unlock_bh(&bat_priv->gw.list_lock); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 441 | goto out; |
| 442 | } |
Sven Eckelmann | dff9bc4 | 2018-08-12 21:04:41 +0200 | [diff] [blame] | 443 | spin_unlock_bh(&bat_priv->gw.list_lock); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 444 | |
Sven Eckelmann | 825ffe1 | 2017-08-23 21:52:13 +0200 | [diff] [blame] | 445 | if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) && |
| 446 | gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)) |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 447 | goto out; |
| 448 | |
| 449 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 450 | "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", |
| 451 | orig_node->orig, |
| 452 | gw_node->bandwidth_down / 10, |
| 453 | gw_node->bandwidth_down % 10, |
| 454 | gw_node->bandwidth_up / 10, |
| 455 | gw_node->bandwidth_up % 10, |
| 456 | ntohl(gateway->bandwidth_down) / 10, |
| 457 | ntohl(gateway->bandwidth_down) % 10, |
| 458 | ntohl(gateway->bandwidth_up) / 10, |
| 459 | ntohl(gateway->bandwidth_up) % 10); |
| 460 | |
| 461 | gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); |
| 462 | gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); |
| 463 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 464 | if (ntohl(gateway->bandwidth_down) == 0) { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 465 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 466 | "Gateway %pM removed from gateway list\n", |
| 467 | orig_node->orig); |
| 468 | |
| 469 | /* Note: We don't need a NULL check here, since curr_gw never |
| 470 | * gets dereferenced. |
| 471 | */ |
Simon Wunderlich | bd3524c | 2015-08-03 19:13:58 +0200 | [diff] [blame] | 472 | spin_lock_bh(&bat_priv->gw.list_lock); |
Sven Eckelmann | c18bdd0 | 2016-01-31 13:27:59 +0100 | [diff] [blame] | 473 | if (!hlist_unhashed(&gw_node->list)) { |
| 474 | hlist_del_init_rcu(&gw_node->list); |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 475 | batadv_gw_node_put(gw_node); |
Sven Eckelmann | 9264c85 | 2018-10-30 22:01:23 +0100 | [diff] [blame] | 476 | bat_priv->gw.generation++; |
Sven Eckelmann | c18bdd0 | 2016-01-31 13:27:59 +0100 | [diff] [blame] | 477 | } |
Simon Wunderlich | bd3524c | 2015-08-03 19:13:58 +0200 | [diff] [blame] | 478 | spin_unlock_bh(&bat_priv->gw.list_lock); |
| 479 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 480 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); |
| 481 | if (gw_node == curr_gw) |
Antonio Quartulli | 4e820e7 | 2013-11-04 20:59:41 +0100 | [diff] [blame] | 482 | batadv_gw_reselect(bat_priv); |
Simon Wunderlich | bd3524c | 2015-08-03 19:13:58 +0200 | [diff] [blame] | 483 | |
| 484 | if (curr_gw) |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 485 | batadv_gw_node_put(curr_gw); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | out: |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 489 | if (gw_node) |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 490 | batadv_gw_node_put(gw_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 493 | /** |
| 494 | * batadv_gw_node_delete() - Remove orig_node from gateway list |
| 495 | * @bat_priv: the bat priv with all the soft interface information |
| 496 | * @orig_node: orig node which is currently in process of being removed |
| 497 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 498 | void batadv_gw_node_delete(struct batadv_priv *bat_priv, |
| 499 | struct batadv_orig_node *orig_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 500 | { |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 501 | struct batadv_tvlv_gateway_data gateway; |
| 502 | |
| 503 | gateway.bandwidth_down = 0; |
| 504 | gateway.bandwidth_up = 0; |
| 505 | |
| 506 | batadv_gw_node_update(bat_priv, orig_node, &gateway); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 509 | /** |
| 510 | * batadv_gw_node_free() - Free gateway information from soft interface |
| 511 | * @bat_priv: the bat priv with all the soft interface information |
| 512 | */ |
Simon Wunderlich | bd3524c | 2015-08-03 19:13:58 +0200 | [diff] [blame] | 513 | void batadv_gw_node_free(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 514 | { |
Simon Wunderlich | bd3524c | 2015-08-03 19:13:58 +0200 | [diff] [blame] | 515 | struct batadv_gw_node *gw_node; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 516 | struct hlist_node *node_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 517 | |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 518 | spin_lock_bh(&bat_priv->gw.list_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 519 | hlist_for_each_entry_safe(gw_node, node_tmp, |
Sven Eckelmann | 70ea5ce | 2016-07-27 12:31:08 +0200 | [diff] [blame] | 520 | &bat_priv->gw.gateway_list, list) { |
Simon Wunderlich | bd3524c | 2015-08-03 19:13:58 +0200 | [diff] [blame] | 521 | hlist_del_init_rcu(&gw_node->list); |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 522 | batadv_gw_node_put(gw_node); |
Sven Eckelmann | 9264c85 | 2018-10-30 22:01:23 +0100 | [diff] [blame] | 523 | bat_priv->gw.generation++; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 524 | } |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 525 | spin_unlock_bh(&bat_priv->gw.list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Sven Eckelmann | dc1cbd1 | 2016-07-16 09:31:20 +0200 | [diff] [blame] | 528 | #ifdef CONFIG_BATMAN_ADV_DEBUGFS |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * batadv_gw_client_seq_print_text() - Print the gateway table in a seq file |
| 532 | * @seq: seq file to print on |
| 533 | * @offset: not used |
| 534 | * |
| 535 | * Return: always 0 |
| 536 | */ |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 537 | int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 538 | { |
| 539 | struct net_device *net_dev = (struct net_device *)seq->private; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 540 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
| 541 | struct batadv_hard_iface *primary_if; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 542 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 543 | primary_if = batadv_seq_print_text_primary_if_get(seq); |
| 544 | if (!primary_if) |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 545 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 546 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 547 | seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n", |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 548 | BATADV_SOURCE_VERSION, primary_if->net_dev->name, |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 549 | primary_if->net_dev->dev_addr, net_dev->name, |
| 550 | bat_priv->algo_ops->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 551 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 552 | batadv_hardif_put(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 553 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 554 | if (!bat_priv->algo_ops->gw.print) { |
| 555 | seq_puts(seq, |
| 556 | "No printing function for this routing protocol\n"); |
| 557 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 558 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 559 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 560 | bat_priv->algo_ops->gw.print(bat_priv, seq); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 561 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 562 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 563 | } |
Sven Eckelmann | dc1cbd1 | 2016-07-16 09:31:20 +0200 | [diff] [blame] | 564 | #endif |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 565 | |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 566 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 567 | * batadv_gw_dump() - Dump gateways into a message |
Sven Eckelmann | d7129da | 2016-07-03 13:31:42 +0200 | [diff] [blame] | 568 | * @msg: Netlink message to dump into |
| 569 | * @cb: Control block containing additional options |
| 570 | * |
| 571 | * Return: Error code, or length of message |
| 572 | */ |
| 573 | int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb) |
| 574 | { |
| 575 | struct batadv_hard_iface *primary_if = NULL; |
| 576 | struct net *net = sock_net(cb->skb->sk); |
| 577 | struct net_device *soft_iface; |
| 578 | struct batadv_priv *bat_priv; |
| 579 | int ifindex; |
| 580 | int ret; |
| 581 | |
| 582 | ifindex = batadv_netlink_get_ifindex(cb->nlh, |
| 583 | BATADV_ATTR_MESH_IFINDEX); |
| 584 | if (!ifindex) |
| 585 | return -EINVAL; |
| 586 | |
| 587 | soft_iface = dev_get_by_index(net, ifindex); |
| 588 | if (!soft_iface || !batadv_softif_is_valid(soft_iface)) { |
| 589 | ret = -ENODEV; |
| 590 | goto out; |
| 591 | } |
| 592 | |
| 593 | bat_priv = netdev_priv(soft_iface); |
| 594 | |
| 595 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 596 | if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) { |
| 597 | ret = -ENOENT; |
| 598 | goto out; |
| 599 | } |
| 600 | |
| 601 | if (!bat_priv->algo_ops->gw.dump) { |
| 602 | ret = -EOPNOTSUPP; |
| 603 | goto out; |
| 604 | } |
| 605 | |
| 606 | bat_priv->algo_ops->gw.dump(msg, cb, bat_priv); |
| 607 | |
| 608 | ret = msg->len; |
| 609 | |
| 610 | out: |
| 611 | if (primary_if) |
| 612 | batadv_hardif_put(primary_if); |
| 613 | if (soft_iface) |
| 614 | dev_put(soft_iface); |
| 615 | |
| 616 | return ret; |
| 617 | } |
| 618 | |
| 619 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 620 | * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 621 | * @skb: the packet to check |
| 622 | * @header_len: a pointer to the batman-adv header size |
| 623 | * @chaddr: buffer where the client address will be stored. Valid |
| 624 | * only if the function returns BATADV_DHCP_TO_CLIENT |
| 625 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 626 | * This function may re-allocate the data buffer of the skb passed as argument. |
| 627 | * |
| 628 | * Return: |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 629 | * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error |
| 630 | * while parsing it |
| 631 | * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server |
| 632 | * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 633 | */ |
| 634 | enum batadv_dhcp_recipient |
| 635 | batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 636 | u8 *chaddr) |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 637 | { |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 638 | enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 639 | struct ethhdr *ethhdr; |
| 640 | struct iphdr *iphdr; |
| 641 | struct ipv6hdr *ipv6hdr; |
| 642 | struct udphdr *udphdr; |
Antonio Quartulli | f7f8ed5 | 2013-05-12 21:51:15 +0200 | [diff] [blame] | 643 | struct vlan_ethhdr *vhdr; |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 644 | int chaddr_offset; |
Antonio Quartulli | f7f8ed5 | 2013-05-12 21:51:15 +0200 | [diff] [blame] | 645 | __be16 proto; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 646 | u8 *p; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 647 | |
| 648 | /* check for ethernet header */ |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 649 | if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 650 | return BATADV_DHCP_NO; |
| 651 | |
Linus Lüssing | 927c2ed | 2014-01-19 22:22:45 +0100 | [diff] [blame] | 652 | ethhdr = eth_hdr(skb); |
Antonio Quartulli | f7f8ed5 | 2013-05-12 21:51:15 +0200 | [diff] [blame] | 653 | proto = ethhdr->h_proto; |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 654 | *header_len += ETH_HLEN; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 655 | |
| 656 | /* check for initial vlan header */ |
Antonio Quartulli | f7f8ed5 | 2013-05-12 21:51:15 +0200 | [diff] [blame] | 657 | if (proto == htons(ETH_P_8021Q)) { |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 658 | if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 659 | return BATADV_DHCP_NO; |
Antonio Quartulli | f7f8ed5 | 2013-05-12 21:51:15 +0200 | [diff] [blame] | 660 | |
Linus Lüssing | 927c2ed | 2014-01-19 22:22:45 +0100 | [diff] [blame] | 661 | vhdr = vlan_eth_hdr(skb); |
Antonio Quartulli | f7f8ed5 | 2013-05-12 21:51:15 +0200 | [diff] [blame] | 662 | proto = vhdr->h_vlan_encapsulated_proto; |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 663 | *header_len += VLAN_HLEN; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* check for ip header */ |
Antonio Quartulli | f7f8ed5 | 2013-05-12 21:51:15 +0200 | [diff] [blame] | 667 | switch (proto) { |
| 668 | case htons(ETH_P_IP): |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 669 | if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 670 | return BATADV_DHCP_NO; |
| 671 | |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 672 | iphdr = (struct iphdr *)(skb->data + *header_len); |
| 673 | *header_len += iphdr->ihl * 4; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 674 | |
| 675 | /* check for udp header */ |
| 676 | if (iphdr->protocol != IPPROTO_UDP) |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 677 | return BATADV_DHCP_NO; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 678 | |
| 679 | break; |
Antonio Quartulli | f7f8ed5 | 2013-05-12 21:51:15 +0200 | [diff] [blame] | 680 | case htons(ETH_P_IPV6): |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 681 | if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 682 | return BATADV_DHCP_NO; |
| 683 | |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 684 | ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); |
| 685 | *header_len += sizeof(*ipv6hdr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 686 | |
| 687 | /* check for udp header */ |
| 688 | if (ipv6hdr->nexthdr != IPPROTO_UDP) |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 689 | return BATADV_DHCP_NO; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 690 | |
| 691 | break; |
| 692 | default: |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 693 | return BATADV_DHCP_NO; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 696 | if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 697 | return BATADV_DHCP_NO; |
Linus Lüssing | 9d2c948 | 2013-08-06 20:21:15 +0200 | [diff] [blame] | 698 | |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 699 | udphdr = (struct udphdr *)(skb->data + *header_len); |
| 700 | *header_len += sizeof(*udphdr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 701 | |
| 702 | /* check for bootp port */ |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 703 | switch (proto) { |
| 704 | case htons(ETH_P_IP): |
| 705 | if (udphdr->dest == htons(67)) |
| 706 | ret = BATADV_DHCP_TO_SERVER; |
| 707 | else if (udphdr->source == htons(67)) |
| 708 | ret = BATADV_DHCP_TO_CLIENT; |
| 709 | break; |
| 710 | case htons(ETH_P_IPV6): |
| 711 | if (udphdr->dest == htons(547)) |
| 712 | ret = BATADV_DHCP_TO_SERVER; |
| 713 | else if (udphdr->source == htons(547)) |
| 714 | ret = BATADV_DHCP_TO_CLIENT; |
| 715 | break; |
| 716 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 717 | |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 718 | chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; |
| 719 | /* store the client address if the message is going to a client */ |
| 720 | if (ret == BATADV_DHCP_TO_CLIENT && |
| 721 | pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) { |
| 722 | /* check if the DHCP packet carries an Ethernet DHCP */ |
| 723 | p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; |
| 724 | if (*p != BATADV_DHCP_HTYPE_ETHERNET) |
| 725 | return BATADV_DHCP_NO; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 726 | |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 727 | /* check if the DHCP packet carries a valid Ethernet address */ |
| 728 | p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; |
| 729 | if (*p != ETH_ALEN) |
| 730 | return BATADV_DHCP_NO; |
| 731 | |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 732 | ether_addr_copy(chaddr, skb->data + chaddr_offset); |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | return ret; |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 736 | } |
Antonio Quartulli | aa143d2 | 2014-09-01 14:37:27 +0200 | [diff] [blame] | 737 | |
Antonio Quartulli | bbb877e | 2013-06-04 12:11:42 +0200 | [diff] [blame] | 738 | /** |
Sven Eckelmann | 7e9a8c2 | 2017-12-02 19:51:47 +0100 | [diff] [blame] | 739 | * batadv_gw_out_of_range() - check if the dhcp request destination is the best |
| 740 | * gateway |
Antonio Quartulli | bbb877e | 2013-06-04 12:11:42 +0200 | [diff] [blame] | 741 | * @bat_priv: the bat priv with all the soft interface information |
| 742 | * @skb: the outgoing packet |
| 743 | * |
| 744 | * Check if the skb is a DHCP request and if it is sent to the current best GW |
| 745 | * server. Due to topology changes it may be the case that the GW server |
| 746 | * previously selected is not the best one anymore. |
| 747 | * |
Antonio Quartulli | bbb877e | 2013-06-04 12:11:42 +0200 | [diff] [blame] | 748 | * This call might reallocate skb data. |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 749 | * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 750 | * |
| 751 | * Return: true if the packet destination is unicast and it is not the best gw, |
| 752 | * false otherwise. |
Antonio Quartulli | bbb877e | 2013-06-04 12:11:42 +0200 | [diff] [blame] | 753 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 754 | bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, |
Linus Lüssing | 9d2c948 | 2013-08-06 20:21:15 +0200 | [diff] [blame] | 755 | struct sk_buff *skb) |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 756 | { |
Sven Eckelmann | 4f248cf | 2015-06-09 20:50:49 +0200 | [diff] [blame] | 757 | struct batadv_neigh_node *neigh_curr = NULL; |
| 758 | struct batadv_neigh_node *neigh_old = NULL; |
Linus Lüssing | a752c0a | 2018-03-22 00:21:32 +0100 | [diff] [blame] | 759 | struct batadv_orig_node *orig_dst_node = NULL; |
Sven Eckelmann | 4f248cf | 2015-06-09 20:50:49 +0200 | [diff] [blame] | 760 | struct batadv_gw_node *gw_node = NULL; |
| 761 | struct batadv_gw_node *curr_gw = NULL; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 762 | struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo; |
Antonio Quartulli | 6c413b1 | 2013-11-05 19:31:08 +0100 | [diff] [blame] | 763 | struct ethhdr *ethhdr = (struct ethhdr *)skb->data; |
| 764 | bool out_of_range = false; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 765 | u8 curr_tq_avg; |
Antonio Quartulli | bbb877e | 2013-06-04 12:11:42 +0200 | [diff] [blame] | 766 | unsigned short vid; |
| 767 | |
| 768 | vid = batadv_get_vid(skb, 0); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 769 | |
Linus Lüssing | a752c0a | 2018-03-22 00:21:32 +0100 | [diff] [blame] | 770 | if (is_multicast_ether_addr(ethhdr->h_dest)) |
| 771 | goto out; |
| 772 | |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 773 | orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, |
Antonio Quartulli | bbb877e | 2013-06-04 12:11:42 +0200 | [diff] [blame] | 774 | ethhdr->h_dest, vid); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 775 | if (!orig_dst_node) |
| 776 | goto out; |
| 777 | |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 778 | gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); |
Antonio Quartulli | 0d16449 | 2014-12-20 13:48:57 +0100 | [diff] [blame] | 779 | if (!gw_node) |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 780 | goto out; |
| 781 | |
Antonio Quartulli | 3a24a63 | 2016-05-06 02:46:38 +0800 | [diff] [blame] | 782 | switch (atomic_read(&bat_priv->gw.mode)) { |
Sven Eckelmann | cd646ab | 2012-06-03 22:19:18 +0200 | [diff] [blame] | 783 | case BATADV_GW_MODE_SERVER: |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 784 | /* If we are a GW then we are our best GW. We can artificially |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 785 | * set the tq towards ourself as the maximum value |
| 786 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 787 | curr_tq_avg = BATADV_TQ_MAX_VALUE; |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 788 | break; |
Sven Eckelmann | cd646ab | 2012-06-03 22:19:18 +0200 | [diff] [blame] | 789 | case BATADV_GW_MODE_CLIENT: |
Sven Eckelmann | 1409a83 | 2012-05-12 18:33:55 +0200 | [diff] [blame] | 790 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 791 | if (!curr_gw) |
| 792 | goto out; |
| 793 | |
| 794 | /* packet is going to our gateway */ |
| 795 | if (curr_gw->orig_node == orig_dst_node) |
| 796 | goto out; |
| 797 | |
| 798 | /* If the dhcp packet has been sent to a different gw, |
| 799 | * we have to evaluate whether the old gw is still |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 800 | * reliable enough |
| 801 | */ |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 802 | neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, |
| 803 | NULL); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 804 | if (!neigh_curr) |
| 805 | goto out; |
| 806 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 807 | curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr, |
| 808 | BATADV_IF_DEFAULT); |
| 809 | if (!curr_ifinfo) |
| 810 | goto out; |
| 811 | |
| 812 | curr_tq_avg = curr_ifinfo->bat_iv.tq_avg; |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 813 | batadv_neigh_ifinfo_put(curr_ifinfo); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 814 | |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 815 | break; |
Sven Eckelmann | cd646ab | 2012-06-03 22:19:18 +0200 | [diff] [blame] | 816 | case BATADV_GW_MODE_OFF: |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 817 | default: |
| 818 | goto out; |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 819 | } |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 820 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 821 | neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); |
Dan Carpenter | 2ef04f4 | 2011-11-29 09:09:09 +0300 | [diff] [blame] | 822 | if (!neigh_old) |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 823 | goto out; |
| 824 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 825 | old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT); |
| 826 | if (!old_ifinfo) |
| 827 | goto out; |
| 828 | |
| 829 | if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD) |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 830 | out_of_range = true; |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 831 | batadv_neigh_ifinfo_put(old_ifinfo); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 832 | |
| 833 | out: |
| 834 | if (orig_dst_node) |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 835 | batadv_orig_node_put(orig_dst_node); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 836 | if (curr_gw) |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 837 | batadv_gw_node_put(curr_gw); |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 838 | if (gw_node) |
Sven Eckelmann | 3a01743 | 2016-01-17 11:01:18 +0100 | [diff] [blame] | 839 | batadv_gw_node_put(gw_node); |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 840 | if (neigh_old) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 841 | batadv_neigh_node_put(neigh_old); |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 842 | if (neigh_curr) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 843 | batadv_neigh_node_put(neigh_curr); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 844 | return out_of_range; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 845 | } |