blob: 8f7883b7d717a94dc2f4de678b2744524e0f124d [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020
Sven Eckelmann7a659d52016-01-16 10:29:54 +010021#include <linux/atomic.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020022#include <linux/byteorder/generic.h>
23#include <linux/errno.h>
24#include <linux/fs.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020025#include <linux/if.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000026#include <linux/if_arp.h>
Antonio Quartulliaf5d4f72012-11-26 00:38:50 +010027#include <linux/if_ether.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020028#include <linux/kernel.h>
Sven Eckelmann7a659d52016-01-16 10:29:54 +010029#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020030#include <linux/list.h>
31#include <linux/netdevice.h>
32#include <linux/printk.h>
33#include <linux/rculist.h>
34#include <linux/rtnetlink.h>
35#include <linux/slab.h>
Marek Lindnercef63412015-08-04 21:09:55 +080036#include <linux/spinlock.h>
Andrew Lunn275019d2016-07-03 13:31:33 +020037#include <net/net_namespace.h>
38#include <net/rtnetlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020039
Sven Eckelmanna2d08162016-05-15 11:07:46 +020040#include "bat_v.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020041#include "bridge_loop_avoidance.h"
42#include "debugfs.h"
43#include "distributed-arp-table.h"
44#include "gateway_client.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020045#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020046#include "originator.h"
47#include "packet.h"
48#include "send.h"
49#include "soft-interface.h"
50#include "sysfs.h"
51#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010053/**
54 * batadv_hardif_release - release hard interface from lists and queue for
55 * free after rcu grace period
Sven Eckelmann7a659d52016-01-16 10:29:54 +010056 * @ref: kref pointer of the hard interface
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010057 */
Sven Eckelmann7a659d52016-01-16 10:29:54 +010058void batadv_hardif_release(struct kref *ref)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059{
Sven Eckelmann7a659d52016-01-16 10:29:54 +010060 struct batadv_hard_iface *hard_iface;
61
62 hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
Marek Lindnere6c10f42011-02-18 12:33:20 +000063 dev_put(hard_iface->net_dev);
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010064
65 kfree_rcu(hard_iface, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066}
67
Sven Eckelmann56303d32012-06-05 22:31:31 +020068struct batadv_hard_iface *
69batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070{
Sven Eckelmann56303d32012-06-05 22:31:31 +020071 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072
73 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020074 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000075 if (hard_iface->net_dev == net_dev &&
Sven Eckelmann7a659d52016-01-16 10:29:54 +010076 kref_get_unless_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000077 goto out;
78 }
79
Marek Lindnere6c10f42011-02-18 12:33:20 +000080 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081
82out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000084 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085}
86
Antonio Quartullib7eddd02012-09-09 10:46:46 +020087/**
Andrew Lunn275019d2016-07-03 13:31:33 +020088 * batadv_getlink_net - return link net namespace (of use fallback)
89 * @netdev: net_device to check
90 * @fallback_net: return in case get_link_net is not available for @netdev
91 *
92 * Return: result of rtnl_link_ops->get_link_net or @fallback_net
93 */
94static const struct net *batadv_getlink_net(const struct net_device *netdev,
95 const struct net *fallback_net)
96{
97 if (!netdev->rtnl_link_ops)
98 return fallback_net;
99
100 if (!netdev->rtnl_link_ops->get_link_net)
101 return fallback_net;
102
103 return netdev->rtnl_link_ops->get_link_net(netdev);
104}
105
106/**
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100107 * batadv_mutual_parents - check if two devices are each others parent
Andrew Lunn275019d2016-07-03 13:31:33 +0200108 * @dev1: 1st net dev
109 * @net1: 1st devices netns
110 * @dev2: 2nd net dev
111 * @net2: 2nd devices netns
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100112 *
113 * veth devices come in pairs and each is the parent of the other!
114 *
115 * Return: true if the devices are each others parent, otherwise false
116 */
117static bool batadv_mutual_parents(const struct net_device *dev1,
Andrew Lunn275019d2016-07-03 13:31:33 +0200118 const struct net *net1,
119 const struct net_device *dev2,
120 const struct net *net2)
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100121{
122 int dev1_parent_iflink = dev_get_iflink(dev1);
123 int dev2_parent_iflink = dev_get_iflink(dev2);
Andrew Lunn275019d2016-07-03 13:31:33 +0200124 const struct net *dev1_parent_net;
125 const struct net *dev2_parent_net;
126
127 dev1_parent_net = batadv_getlink_net(dev1, net1);
128 dev2_parent_net = batadv_getlink_net(dev2, net2);
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100129
130 if (!dev1_parent_iflink || !dev2_parent_iflink)
131 return false;
132
133 return (dev1_parent_iflink == dev2->ifindex) &&
Andrew Lunn275019d2016-07-03 13:31:33 +0200134 (dev2_parent_iflink == dev1->ifindex) &&
135 net_eq(dev1_parent_net, net2) &&
136 net_eq(dev2_parent_net, net1);
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100137}
138
139/**
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200140 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
141 * @net_dev: the device to check
142 *
143 * If the user creates any virtual device on top of a batman-adv interface, it
144 * is important to prevent this new interface to be used to create a new mesh
145 * network (this behaviour would lead to a batman-over-batman configuration).
146 * This function recursively checks all the fathers of the device passed as
147 * argument looking for a batman-adv soft interface.
148 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200149 * Return: true if the device is descendant of a batman-adv mesh interface (or
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200150 * if it is a batman-adv interface itself), false otherwise
151 */
152static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
153{
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200154 struct net *net = dev_net(net_dev);
Andrew Lunn275019d2016-07-03 13:31:33 +0200155 struct net_device *parent_dev;
156 const struct net *parent_net;
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200157 bool ret;
158
159 /* check if this is a batman-adv mesh interface */
160 if (batadv_softif_is_valid(net_dev))
161 return true;
162
163 /* no more parents..stop recursion */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200164 if (dev_get_iflink(net_dev) == 0 ||
165 dev_get_iflink(net_dev) == net_dev->ifindex)
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200166 return false;
167
Andrew Lunn275019d2016-07-03 13:31:33 +0200168 parent_net = batadv_getlink_net(net_dev, net);
169
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200170 /* recurse over the parent device */
Andrew Lunn275019d2016-07-03 13:31:33 +0200171 parent_dev = __dev_get_by_index((struct net *)parent_net,
172 dev_get_iflink(net_dev));
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200173 /* if we got a NULL parent_dev there is something broken.. */
Sven Eckelmanna5a1ce42018-12-30 12:46:01 +0100174 if (!parent_dev) {
175 pr_err("Cannot find parent device\n");
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200176 return false;
Sven Eckelmanna5a1ce42018-12-30 12:46:01 +0100177 }
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200178
Andrew Lunn275019d2016-07-03 13:31:33 +0200179 if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net))
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100180 return false;
181
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200182 ret = batadv_is_on_batman_iface(parent_dev);
183
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200184 return ret;
185}
186
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100187static bool batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188{
189 if (net_dev->flags & IFF_LOOPBACK)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100190 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191
192 if (net_dev->type != ARPHRD_ETHER)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100193 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194
195 if (net_dev->addr_len != ETH_ALEN)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100196 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197
198 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200199 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100200 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100202 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203}
204
Matthias Schifferde68d102013-03-09 23:14:22 +0100205/**
206 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
207 * interface
208 * @net_device: the device to check
209 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200210 * Return: true if the net device is a 802.11 wireless device, false otherwise.
Matthias Schifferde68d102013-03-09 23:14:22 +0100211 */
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200212bool batadv_is_wifi_netdev(struct net_device *net_device)
Matthias Schifferde68d102013-03-09 23:14:22 +0100213{
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200214 if (!net_device)
215 return false;
216
Matthias Schifferde68d102013-03-09 23:14:22 +0100217#ifdef CONFIG_WIRELESS_EXT
218 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
219 * check for wireless_handlers != NULL
220 */
221 if (net_device->wireless_handlers)
222 return true;
223#endif
224
225 /* cfg80211 drivers have to set ieee80211_ptr */
226 if (net_device->ieee80211_ptr)
227 return true;
228
229 return false;
230}
231
Sven Eckelmann56303d32012-06-05 22:31:31 +0200232static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200233batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200235 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236
237 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200238 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000239 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240 continue;
241
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200242 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100243 kref_get_unless_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244 goto out;
245 }
246
Marek Lindnere6c10f42011-02-18 12:33:20 +0000247 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248
249out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000251 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252}
253
Sven Eckelmann56303d32012-06-05 22:31:31 +0200254static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
255 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200257 struct batadv_hard_iface *primary_if;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200258
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200259 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200260 if (!primary_if)
261 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Antonio Quartulli785ea112011-11-23 11:35:44 +0100263 batadv_dat_init_own_addr(bat_priv, primary_if);
Sven Eckelmann08adf152012-05-12 13:38:47 +0200264 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200265out:
266 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100267 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268}
269
Sven Eckelmann56303d32012-06-05 22:31:31 +0200270static void batadv_primary_if_select(struct batadv_priv *bat_priv,
271 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200273 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200275 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
Sven Eckelmann17a86912016-04-11 13:06:40 +0200277 if (new_hard_iface)
278 kref_get(&new_hard_iface->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200280 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200281 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282
Marek Lindner32ae9b22011-04-20 15:40:58 +0200283 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100284 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200285
Antonio Quartulli29824a52016-05-25 23:27:31 +0800286 bat_priv->algo_ops->iface.primary_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200287 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100288
289out:
290 if (curr_hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100291 batadv_hardif_put(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292}
293
Sven Eckelmann56303d32012-06-05 22:31:31 +0200294static bool
295batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000297 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298 return true;
299
300 return false;
301}
302
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200303static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200305 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306
307 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200308 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200309 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
310 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311 continue;
312
Marek Lindnere6c10f42011-02-18 12:33:20 +0000313 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314 continue;
315
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200316 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
317 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 continue;
319
Sven Eckelmann67969582012-03-26 16:22:45 +0200320 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
321 net_dev->dev_addr, hard_iface->net_dev->name);
322 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 }
324 rcu_read_unlock();
325}
326
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200327/**
328 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
329 * @soft_iface: netdev struct of the mesh interface
330 */
331static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
332{
333 const struct batadv_hard_iface *hard_iface;
334 unsigned short lower_header_len = ETH_HLEN;
335 unsigned short lower_headroom = 0;
336 unsigned short lower_tailroom = 0;
337 unsigned short needed_headroom;
338
339 rcu_read_lock();
340 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
341 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
342 continue;
343
344 if (hard_iface->soft_iface != soft_iface)
345 continue;
346
347 lower_header_len = max_t(unsigned short, lower_header_len,
348 hard_iface->net_dev->hard_header_len);
349
350 lower_headroom = max_t(unsigned short, lower_headroom,
351 hard_iface->net_dev->needed_headroom);
352
353 lower_tailroom = max_t(unsigned short, lower_tailroom,
354 hard_iface->net_dev->needed_tailroom);
355 }
356 rcu_read_unlock();
357
358 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
359 needed_headroom += batadv_max_header_len();
360
361 soft_iface->needed_headroom = needed_headroom;
362 soft_iface->needed_tailroom = lower_tailroom;
363}
364
Sven Eckelmann95638772012-05-12 02:09:31 +0200365int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800367 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200368 const struct batadv_hard_iface *hard_iface;
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100369 int min_mtu = INT_MAX;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
371 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200372 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200373 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
374 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375 continue;
376
Marek Lindnere6c10f42011-02-18 12:33:20 +0000377 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378 continue;
379
Marek Lindnera19d3d82013-05-27 15:33:25 +0800380 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381 }
382 rcu_read_unlock();
Marek Lindnera19d3d82013-05-27 15:33:25 +0800383
Marek Lindnera19d3d82013-05-27 15:33:25 +0800384 if (atomic_read(&bat_priv->fragmentation) == 0)
385 goto out;
386
387 /* with fragmentation enabled the maximum size of internally generated
388 * packets such as translation table exchanges or tvlv containers, etc
389 * has to be calculated
390 */
391 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
392 min_mtu -= sizeof(struct batadv_frag_packet);
393 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800394
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395out:
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100396 /* report to the other components the maximum amount of bytes that
397 * batman-adv can send over the wire (without considering the payload
398 * overhead). For example, this value is used by TT to compute the
399 * maximum local table table size
400 */
401 atomic_set(&bat_priv->packet_size_max, min_mtu);
402
403 /* the real soft-interface MTU is computed by removing the payload
404 * overhead from the maximum amount of bytes that was just computed.
405 *
406 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
407 */
408 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409}
410
411/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200412void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800414 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Marek Lindnera19d3d82013-05-27 15:33:25 +0800416 /* Check if the local translate table should be cleaned up to match a
417 * new (and smaller) MTU.
418 */
419 batadv_tt_local_resize_to_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420}
421
Sven Eckelmann56303d32012-06-05 22:31:31 +0200422static void
423batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200425 struct batadv_priv *bat_priv;
426 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200428 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200429 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430
Marek Lindnere6c10f42011-02-18 12:33:20 +0000431 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432
Antonio Quartulli29824a52016-05-25 23:27:31 +0800433 bat_priv->algo_ops->iface.update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200434 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200436 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200437 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200439 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200440 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200441 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442
Sven Eckelmann3e348192012-05-16 20:23:22 +0200443 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
444 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445
Sven Eckelmann95638772012-05-12 02:09:31 +0200446 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200447
Antonio Quartulli29824a52016-05-25 23:27:31 +0800448 if (bat_priv->algo_ops->iface.activate)
449 bat_priv->algo_ops->iface.activate(hard_iface);
Antonio Quartullib6cf5d42016-04-14 09:37:05 +0800450
Marek Lindner32ae9b22011-04-20 15:40:58 +0200451out:
452 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100453 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454}
455
Sven Eckelmann56303d32012-06-05 22:31:31 +0200456static void
457batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200459 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
460 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461 return;
462
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200463 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464
Sven Eckelmann3e348192012-05-16 20:23:22 +0200465 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
466 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467
Sven Eckelmann95638772012-05-12 02:09:31 +0200468 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469}
470
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100471/**
472 * batadv_master_del_slave - remove hard_iface from the current master interface
473 * @slave: the interface enslaved in another master
474 * @master: the master from which slave has to be removed
475 *
476 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
477 * is free'd and master can correctly change its internal state.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200478 *
479 * Return: 0 on success, a negative value representing the error otherwise
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100480 */
481static int batadv_master_del_slave(struct batadv_hard_iface *slave,
482 struct net_device *master)
483{
484 int ret;
485
486 if (!master)
487 return 0;
488
489 ret = -EBUSY;
490 if (master->netdev_ops->ndo_del_slave)
491 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
492
493 return ret;
494}
495
Sven Eckelmann56303d32012-06-05 22:31:31 +0200496int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200497 struct net *net, const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200499 struct batadv_priv *bat_priv;
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100500 struct net_device *soft_iface, *master;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200501 __be16 ethertype = htons(ETH_P_BATMAN);
Marek Lindner411d6ed2013-05-08 13:31:59 +0800502 int max_header_len = batadv_max_header_len();
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000503 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200505 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 goto out;
507
Sven Eckelmann17a86912016-04-11 13:06:40 +0200508 kref_get(&hard_iface->refcount);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000509
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200510 soft_iface = dev_get_by_name(net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000512 if (!soft_iface) {
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200513 soft_iface = batadv_softif_create(net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000515 if (!soft_iface) {
516 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000518 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519
520 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000521 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 }
523
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200524 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100525 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000526 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000527 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800528 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000529 }
530
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100531 /* check if the interface is enslaved in another virtual one and
532 * in that case unlink it first
533 */
534 master = netdev_master_upper_dev_get(hard_iface->net_dev);
535 ret = batadv_master_del_slave(hard_iface, master);
536 if (ret)
537 goto err_dev;
538
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000539 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000540 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200541
Jiri Pirko6dffb042015-12-03 12:12:10 +0100542 ret = netdev_master_upper_dev_link(hard_iface->net_dev,
Jiri Pirko29bf24a2015-12-03 12:12:11 +0100543 soft_iface, NULL, NULL);
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800544 if (ret)
545 goto err_dev;
546
Antonio Quartulli29824a52016-05-25 23:27:31 +0800547 ret = bat_priv->algo_ops->iface.enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200548 if (ret < 0)
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800549 goto err_upper;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
Marek Lindnere6c10f42011-02-18 12:33:20 +0000551 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200553 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200554 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
555 if (ret < 0) {
Antonio Quartulli29824a52016-05-25 23:27:31 +0800556 bat_priv->algo_ops->iface.disable(hard_iface);
Simon Wunderlich62446302012-07-01 22:51:55 +0200557 bat_priv->num_ifaces--;
558 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800559 goto err_upper;
Simon Wunderlich62446302012-07-01 22:51:55 +0200560 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561
Sven Eckelmannd7d6de92016-03-05 16:09:18 +0100562 kref_get(&hard_iface->refcount);
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200563 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200564 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000565 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
566 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567
Sven Eckelmann3e348192012-05-16 20:23:22 +0200568 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
569 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200571 if (atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800572 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200573 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800574 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %i would solve the problem.\n",
Sven Eckelmann3e348192012-05-16 20:23:22 +0200575 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800576 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000577
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200578 if (!atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800579 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200580 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800581 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %i.\n",
Sven Eckelmann3e348192012-05-16 20:23:22 +0200582 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800583 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200585 if (batadv_hardif_is_iface_up(hard_iface))
586 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200588 batadv_err(hard_iface->soft_iface,
589 "Not using interface %s (retrying later): interface not active\n",
590 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200592 batadv_hardif_recalc_extra_skbroom(soft_iface);
593
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594out:
595 return 0;
596
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800597err_upper:
598 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
Marek Lindner77af7572012-02-07 17:20:48 +0800599err_dev:
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800600 hard_iface->soft_iface = NULL;
Marek Lindner77af7572012-02-07 17:20:48 +0800601 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602err:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100603 batadv_hardif_put(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000604 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605}
606
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800607void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
608 enum batadv_hard_if_cleanup autodel)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200610 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
611 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612
Sven Eckelmannf2d23862016-03-19 13:55:21 +0100613 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200615 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200616 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617
Sven Eckelmann3e348192012-05-16 20:23:22 +0200618 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
619 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000620 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannd7d6de92016-03-05 16:09:18 +0100621 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622
623 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200624 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200626 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200627 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200628 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200630 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
631 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
633 if (new_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100634 batadv_hardif_put(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000635 }
636
Antonio Quartulli29824a52016-05-25 23:27:31 +0800637 bat_priv->algo_ops->iface.disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200638 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639
Marek Lindnere6c10f42011-02-18 12:33:20 +0000640 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200641 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200642 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000643 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644
Antonio Quartullia5256f72015-08-04 22:26:19 +0200645 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200646 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
Antonio Quartullia5256f72015-08-04 22:26:19 +0200647
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000648 /* nobody uses this interface anymore */
Antonio Quartulli8257f552013-08-19 18:39:59 +0200649 if (!bat_priv->num_ifaces) {
650 batadv_gw_check_client_stop(bat_priv);
651
652 if (autodel == BATADV_IF_CLEANUP_AUTO)
653 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
654 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655
Sven Eckelmann27915aa2016-11-02 18:14:43 +0100656 hard_iface->soft_iface = NULL;
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100657 batadv_hardif_put(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200658
659out:
660 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100661 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662}
663
Sven Eckelmann56303d32012-06-05 22:31:31 +0200664static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200665batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200667 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668 int ret;
669
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200670 ASSERT_RTNL();
671
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100672 if (!batadv_is_valid_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673 goto out;
674
675 dev_hold(net_dev);
676
Antonio Quartulli7db3fc22013-04-02 12:16:53 +0200677 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700678 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680
Sven Eckelmann5853e222012-05-12 02:09:24 +0200681 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682 if (ret)
683 goto free_if;
684
Marek Lindnere6c10f42011-02-18 12:33:20 +0000685 hard_iface->if_num = -1;
686 hard_iface->net_dev = net_dev;
687 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200688 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100689
690 ret = batadv_debugfs_add_hardif(hard_iface);
691 if (ret)
692 goto free_sysfs;
693
Marek Lindnere6c10f42011-02-18 12:33:20 +0000694 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnercef63412015-08-04 21:09:55 +0800695 INIT_HLIST_HEAD(&hard_iface->neigh_list);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100696
Marek Lindnercef63412015-08-04 21:09:55 +0800697 spin_lock_init(&hard_iface->neigh_list_lock);
Sven Eckelmannb2367e42016-07-15 17:39:28 +0200698 kref_init(&hard_iface->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800699
Matthias Schiffercaf65bf2013-03-09 23:14:23 +0100700 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
701 if (batadv_is_wifi_netdev(net_dev))
702 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
703
Marek Lindner7db682d2016-05-10 22:31:59 +0800704 batadv_v_hardif_init(hard_iface);
705
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200706 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannb2367e42016-07-15 17:39:28 +0200707 kref_get(&hard_iface->refcount);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200708 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709
Marek Lindnere6c10f42011-02-18 12:33:20 +0000710 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100712free_sysfs:
713 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000715 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716release_dev:
717 dev_put(net_dev);
718out:
719 return NULL;
720}
721
Sven Eckelmann56303d32012-06-05 22:31:31 +0200722static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000723{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200724 ASSERT_RTNL();
725
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200727 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800728 batadv_hardif_disable_interface(hard_iface,
Sven Eckelmann06d640c2016-07-10 15:47:57 +0200729 BATADV_IF_CLEANUP_KEEP);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200731 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732 return;
733
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200734 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Sven Eckelmann569c9852016-06-13 07:41:31 +0200735 batadv_debugfs_del_hardif(hard_iface);
736 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
737 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738}
739
Sven Eckelmann95638772012-05-12 02:09:31 +0200740void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200742 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200744 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000745 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200746 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000747 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200748 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749 }
750 rtnl_unlock();
751}
752
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200753static int batadv_hard_if_event(struct notifier_block *this,
754 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755{
Jiri Pirko351638e2013-05-28 01:30:21 +0000756 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200757 struct batadv_hard_iface *hard_iface;
758 struct batadv_hard_iface *primary_if = NULL;
759 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760
Sven Eckelmann37130292013-02-11 17:10:22 +0800761 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
762 batadv_sysfs_add_meshif(net_dev);
Antonio Quartulli5d2c05b2013-07-02 11:04:34 +0200763 bat_priv = netdev_priv(net_dev);
764 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
Sven Eckelmann37130292013-02-11 17:10:22 +0800765 return NOTIFY_DONE;
766 }
767
Sven Eckelmann56303d32012-06-05 22:31:31 +0200768 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Andrew Lunna1a66b12015-12-03 21:12:33 +0100769 if (!hard_iface && (event == NETDEV_REGISTER ||
770 event == NETDEV_POST_TYPE_CHANGE))
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200771 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772
Marek Lindnere6c10f42011-02-18 12:33:20 +0000773 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774 goto out;
775
776 switch (event) {
777 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200778 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 break;
780 case NETDEV_GOING_DOWN:
781 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200782 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783 break;
784 case NETDEV_UNREGISTER:
Andrew Lunna1a66b12015-12-03 21:12:33 +0100785 case NETDEV_PRE_TYPE_CHANGE:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000786 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000787
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200788 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000789 break;
790 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000791 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200792 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793 break;
794 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200795 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000796 goto hardif_put;
797
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200798 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799
Marek Lindnere6c10f42011-02-18 12:33:20 +0000800 bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartulli29824a52016-05-25 23:27:31 +0800801 bat_priv->algo_ops->iface.update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800802
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200803 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200804 if (!primary_if)
805 goto hardif_put;
806
807 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200808 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000809 break;
810 default:
811 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000812 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813
814hardif_put:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100815 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200817 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100818 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819 return NOTIFY_DONE;
820}
821
Sven Eckelmann95638772012-05-12 02:09:31 +0200822struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200823 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824};