blob: d3d37f3f99cf4f27afe13eeaffc8044d8c3df036 [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/bug.h>
23#include <linux/byteorder/generic.h>
24#include <linux/errno.h>
25#include <linux/fs.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/if.h>
29#include <linux/kernel.h>
Sven Eckelmann7a659d52016-01-16 10:29:54 +010030#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/list.h>
32#include <linux/netdevice.h>
33#include <linux/printk.h>
34#include <linux/rculist.h>
35#include <linux/rtnetlink.h>
36#include <linux/slab.h>
Marek Lindnercef63412015-08-04 21:09:55 +080037#include <linux/spinlock.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include <linux/workqueue.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020039
40#include "bridge_loop_avoidance.h"
41#include "debugfs.h"
42#include "distributed-arp-table.h"
43#include "gateway_client.h"
44#include "originator.h"
45#include "packet.h"
46#include "send.h"
47#include "soft-interface.h"
48#include "sysfs.h"
49#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010051/**
52 * batadv_hardif_release - release hard interface from lists and queue for
53 * free after rcu grace period
Sven Eckelmann7a659d52016-01-16 10:29:54 +010054 * @ref: kref pointer of the hard interface
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010055 */
Sven Eckelmann7a659d52016-01-16 10:29:54 +010056void batadv_hardif_release(struct kref *ref)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057{
Sven Eckelmann7a659d52016-01-16 10:29:54 +010058 struct batadv_hard_iface *hard_iface;
59
60 hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
Marek Lindnere6c10f42011-02-18 12:33:20 +000061 dev_put(hard_iface->net_dev);
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010062
63 kfree_rcu(hard_iface, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064}
65
Sven Eckelmann56303d32012-06-05 22:31:31 +020066struct batadv_hard_iface *
67batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000068{
Sven Eckelmann56303d32012-06-05 22:31:31 +020069 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070
71 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020072 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000073 if (hard_iface->net_dev == net_dev &&
Sven Eckelmann7a659d52016-01-16 10:29:54 +010074 kref_get_unless_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075 goto out;
76 }
77
Marek Lindnere6c10f42011-02-18 12:33:20 +000078 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079
80out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000082 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083}
84
Antonio Quartullib7eddd02012-09-09 10:46:46 +020085/**
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +010086 * batadv_mutual_parents - check if two devices are each others parent
87 * @dev1: 1st net_device
88 * @dev2: 2nd net_device
89 *
90 * veth devices come in pairs and each is the parent of the other!
91 *
92 * Return: true if the devices are each others parent, otherwise false
93 */
94static bool batadv_mutual_parents(const struct net_device *dev1,
95 const struct net_device *dev2)
96{
97 int dev1_parent_iflink = dev_get_iflink(dev1);
98 int dev2_parent_iflink = dev_get_iflink(dev2);
99
100 if (!dev1_parent_iflink || !dev2_parent_iflink)
101 return false;
102
103 return (dev1_parent_iflink == dev2->ifindex) &&
104 (dev2_parent_iflink == dev1->ifindex);
105}
106
107/**
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200108 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
109 * @net_dev: the device to check
110 *
111 * If the user creates any virtual device on top of a batman-adv interface, it
112 * is important to prevent this new interface to be used to create a new mesh
113 * network (this behaviour would lead to a batman-over-batman configuration).
114 * This function recursively checks all the fathers of the device passed as
115 * argument looking for a batman-adv soft interface.
116 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200117 * Return: true if the device is descendant of a batman-adv mesh interface (or
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200118 * if it is a batman-adv interface itself), false otherwise
119 */
120static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
121{
122 struct net_device *parent_dev;
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200123 struct net *net = dev_net(net_dev);
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200124 bool ret;
125
126 /* check if this is a batman-adv mesh interface */
127 if (batadv_softif_is_valid(net_dev))
128 return true;
129
130 /* no more parents..stop recursion */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200131 if (dev_get_iflink(net_dev) == 0 ||
132 dev_get_iflink(net_dev) == net_dev->ifindex)
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200133 return false;
134
135 /* recurse over the parent device */
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200136 parent_dev = __dev_get_by_index(net, dev_get_iflink(net_dev));
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200137 /* if we got a NULL parent_dev there is something broken.. */
138 if (WARN(!parent_dev, "Cannot find parent device"))
139 return false;
140
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100141 if (batadv_mutual_parents(net_dev, parent_dev))
142 return false;
143
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200144 ret = batadv_is_on_batman_iface(parent_dev);
145
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200146 return ret;
147}
148
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200149static int batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150{
151 if (net_dev->flags & IFF_LOOPBACK)
152 return 0;
153
154 if (net_dev->type != ARPHRD_ETHER)
155 return 0;
156
157 if (net_dev->addr_len != ETH_ALEN)
158 return 0;
159
160 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200161 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164 return 1;
165}
166
Matthias Schifferde68d102013-03-09 23:14:22 +0100167/**
168 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
169 * interface
170 * @net_device: the device to check
171 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200172 * Return: true if the net device is a 802.11 wireless device, false otherwise.
Matthias Schifferde68d102013-03-09 23:14:22 +0100173 */
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200174bool batadv_is_wifi_netdev(struct net_device *net_device)
Matthias Schifferde68d102013-03-09 23:14:22 +0100175{
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200176 if (!net_device)
177 return false;
178
Matthias Schifferde68d102013-03-09 23:14:22 +0100179#ifdef CONFIG_WIRELESS_EXT
180 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
181 * check for wireless_handlers != NULL
182 */
183 if (net_device->wireless_handlers)
184 return true;
185#endif
186
187 /* cfg80211 drivers have to set ieee80211_ptr */
188 if (net_device->ieee80211_ptr)
189 return true;
190
191 return false;
192}
193
Sven Eckelmann56303d32012-06-05 22:31:31 +0200194static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200195batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200197 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198
199 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200200 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000201 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202 continue;
203
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200204 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100205 kref_get_unless_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206 goto out;
207 }
208
Marek Lindnere6c10f42011-02-18 12:33:20 +0000209 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210
211out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000213 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214}
215
Sven Eckelmann56303d32012-06-05 22:31:31 +0200216static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
217 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200219 struct batadv_hard_iface *primary_if;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200220
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200221 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200222 if (!primary_if)
223 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224
Antonio Quartulli785ea112011-11-23 11:35:44 +0100225 batadv_dat_init_own_addr(bat_priv, primary_if);
Sven Eckelmann08adf152012-05-12 13:38:47 +0200226 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200227out:
228 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100229 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230}
231
Sven Eckelmann56303d32012-06-05 22:31:31 +0200232static void batadv_primary_if_select(struct batadv_priv *bat_priv,
233 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200235 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200237 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100239 if (new_hard_iface && !kref_get_unless_zero(&new_hard_iface->refcount))
Marek Lindner32ae9b22011-04-20 15:40:58 +0200240 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200242 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200243 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Marek Lindner32ae9b22011-04-20 15:40:58 +0200245 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100246 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200247
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800248 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200249 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100250
251out:
252 if (curr_hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100253 batadv_hardif_put(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254}
255
Sven Eckelmann56303d32012-06-05 22:31:31 +0200256static bool
257batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000259 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260 return true;
261
262 return false;
263}
264
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200265static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200267 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
269 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200270 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200271 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
272 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273 continue;
274
Marek Lindnere6c10f42011-02-18 12:33:20 +0000275 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276 continue;
277
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200278 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
279 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280 continue;
281
Sven Eckelmann67969582012-03-26 16:22:45 +0200282 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
283 net_dev->dev_addr, hard_iface->net_dev->name);
284 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285 }
286 rcu_read_unlock();
287}
288
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200289/**
290 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
291 * @soft_iface: netdev struct of the mesh interface
292 */
293static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
294{
295 const struct batadv_hard_iface *hard_iface;
296 unsigned short lower_header_len = ETH_HLEN;
297 unsigned short lower_headroom = 0;
298 unsigned short lower_tailroom = 0;
299 unsigned short needed_headroom;
300
301 rcu_read_lock();
302 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
303 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
304 continue;
305
306 if (hard_iface->soft_iface != soft_iface)
307 continue;
308
309 lower_header_len = max_t(unsigned short, lower_header_len,
310 hard_iface->net_dev->hard_header_len);
311
312 lower_headroom = max_t(unsigned short, lower_headroom,
313 hard_iface->net_dev->needed_headroom);
314
315 lower_tailroom = max_t(unsigned short, lower_tailroom,
316 hard_iface->net_dev->needed_tailroom);
317 }
318 rcu_read_unlock();
319
320 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
321 needed_headroom += batadv_max_header_len();
322
323 soft_iface->needed_headroom = needed_headroom;
324 soft_iface->needed_tailroom = lower_tailroom;
325}
326
Sven Eckelmann95638772012-05-12 02:09:31 +0200327int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800329 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200330 const struct batadv_hard_iface *hard_iface;
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100331 int min_mtu = INT_MAX;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332
333 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200334 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200335 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
336 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 continue;
338
Marek Lindnere6c10f42011-02-18 12:33:20 +0000339 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340 continue;
341
Marek Lindnera19d3d82013-05-27 15:33:25 +0800342 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 }
344 rcu_read_unlock();
Marek Lindnera19d3d82013-05-27 15:33:25 +0800345
Marek Lindnera19d3d82013-05-27 15:33:25 +0800346 if (atomic_read(&bat_priv->fragmentation) == 0)
347 goto out;
348
349 /* with fragmentation enabled the maximum size of internally generated
350 * packets such as translation table exchanges or tvlv containers, etc
351 * has to be calculated
352 */
353 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
354 min_mtu -= sizeof(struct batadv_frag_packet);
355 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800356
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357out:
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100358 /* report to the other components the maximum amount of bytes that
359 * batman-adv can send over the wire (without considering the payload
360 * overhead). For example, this value is used by TT to compute the
361 * maximum local table table size
362 */
363 atomic_set(&bat_priv->packet_size_max, min_mtu);
364
365 /* the real soft-interface MTU is computed by removing the payload
366 * overhead from the maximum amount of bytes that was just computed.
367 *
368 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
369 */
370 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371}
372
373/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200374void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800376 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Marek Lindnera19d3d82013-05-27 15:33:25 +0800378 /* Check if the local translate table should be cleaned up to match a
379 * new (and smaller) MTU.
380 */
381 batadv_tt_local_resize_to_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382}
383
Sven Eckelmann56303d32012-06-05 22:31:31 +0200384static void
385batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200387 struct batadv_priv *bat_priv;
388 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200390 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200391 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392
Marek Lindnere6c10f42011-02-18 12:33:20 +0000393 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394
Marek Lindnerc3229392012-03-11 06:17:50 +0800395 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200396 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200398 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200399 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200401 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200402 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200403 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404
Sven Eckelmann3e348192012-05-16 20:23:22 +0200405 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
406 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407
Sven Eckelmann95638772012-05-12 02:09:31 +0200408 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200409
Antonio Quartullib6cf5d42016-04-14 09:37:05 +0800410 if (bat_priv->bat_algo_ops->bat_iface_activate)
411 bat_priv->bat_algo_ops->bat_iface_activate(hard_iface);
412
Marek Lindner32ae9b22011-04-20 15:40:58 +0200413out:
414 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100415 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416}
417
Sven Eckelmann56303d32012-06-05 22:31:31 +0200418static void
419batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200421 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
422 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 return;
424
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200425 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426
Sven Eckelmann3e348192012-05-16 20:23:22 +0200427 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
428 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429
Sven Eckelmann95638772012-05-12 02:09:31 +0200430 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431}
432
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100433/**
434 * batadv_master_del_slave - remove hard_iface from the current master interface
435 * @slave: the interface enslaved in another master
436 * @master: the master from which slave has to be removed
437 *
438 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
439 * is free'd and master can correctly change its internal state.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200440 *
441 * Return: 0 on success, a negative value representing the error otherwise
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100442 */
443static int batadv_master_del_slave(struct batadv_hard_iface *slave,
444 struct net_device *master)
445{
446 int ret;
447
448 if (!master)
449 return 0;
450
451 ret = -EBUSY;
452 if (master->netdev_ops->ndo_del_slave)
453 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
454
455 return ret;
456}
457
Sven Eckelmann56303d32012-06-05 22:31:31 +0200458int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200459 struct net *net, const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200461 struct batadv_priv *bat_priv;
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100462 struct net_device *soft_iface, *master;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200463 __be16 ethertype = htons(ETH_P_BATMAN);
Marek Lindner411d6ed2013-05-08 13:31:59 +0800464 int max_header_len = batadv_max_header_len();
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000465 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200467 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468 goto out;
469
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100470 if (!kref_get_unless_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000471 goto out;
472
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200473 soft_iface = dev_get_by_name(net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000475 if (!soft_iface) {
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200476 soft_iface = batadv_softif_create(net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000478 if (!soft_iface) {
479 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000481 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482
483 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000484 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485 }
486
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200487 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100488 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000489 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000490 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800491 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000492 }
493
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100494 /* check if the interface is enslaved in another virtual one and
495 * in that case unlink it first
496 */
497 master = netdev_master_upper_dev_get(hard_iface->net_dev);
498 ret = batadv_master_del_slave(hard_iface, master);
499 if (ret)
500 goto err_dev;
501
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000502 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000503 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200504
Jiri Pirko6dffb042015-12-03 12:12:10 +0100505 ret = netdev_master_upper_dev_link(hard_iface->net_dev,
Jiri Pirko29bf24a2015-12-03 12:12:11 +0100506 soft_iface, NULL, NULL);
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800507 if (ret)
508 goto err_dev;
509
Marek Lindner77af7572012-02-07 17:20:48 +0800510 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200511 if (ret < 0)
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800512 goto err_upper;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513
Marek Lindnere6c10f42011-02-18 12:33:20 +0000514 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200516 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200517 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
518 if (ret < 0) {
519 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
520 bat_priv->num_ifaces--;
521 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800522 goto err_upper;
Simon Wunderlich62446302012-07-01 22:51:55 +0200523 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524
Sven Eckelmannd7d6de92016-03-05 16:09:18 +0100525 kref_get(&hard_iface->refcount);
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200526 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200527 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000528 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
529 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530
Sven Eckelmann3e348192012-05-16 20:23:22 +0200531 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
532 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200534 if (atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800535 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200536 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800537 "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 +0200538 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800539 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000540
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200541 if (!atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800542 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200543 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800544 "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 +0200545 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800546 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200548 if (batadv_hardif_is_iface_up(hard_iface))
549 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200551 batadv_err(hard_iface->soft_iface,
552 "Not using interface %s (retrying later): interface not active\n",
553 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200555 batadv_hardif_recalc_extra_skbroom(soft_iface);
556
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200558 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559
560out:
561 return 0;
562
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800563err_upper:
564 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
Marek Lindner77af7572012-02-07 17:20:48 +0800565err_dev:
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800566 hard_iface->soft_iface = NULL;
Marek Lindner77af7572012-02-07 17:20:48 +0800567 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568err:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100569 batadv_hardif_put(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000570 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571}
572
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800573void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
574 enum batadv_hard_if_cleanup autodel)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200576 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
577 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000578
Sven Eckelmannf2d23862016-03-19 13:55:21 +0100579 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200581 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200582 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583
Sven Eckelmann3e348192012-05-16 20:23:22 +0200584 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
585 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000586 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannd7d6de92016-03-05 16:09:18 +0100587 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588
589 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200590 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200592 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200593 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200594 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200596 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
597 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598
599 if (new_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100600 batadv_hardif_put(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601 }
602
Marek Lindner00a50072012-02-07 17:20:47 +0800603 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200604 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605
Marek Lindnere6c10f42011-02-18 12:33:20 +0000606 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200607 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200608 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000609 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610
Antonio Quartullia5256f72015-08-04 22:26:19 +0200611 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200612 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
Antonio Quartullia5256f72015-08-04 22:26:19 +0200613
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 /* nobody uses this interface anymore */
Antonio Quartulli8257f552013-08-19 18:39:59 +0200615 if (!bat_priv->num_ifaces) {
616 batadv_gw_check_client_stop(bat_priv);
617
618 if (autodel == BATADV_IF_CLEANUP_AUTO)
619 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
620 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621
Marek Lindnere6c10f42011-02-18 12:33:20 +0000622 hard_iface->soft_iface = NULL;
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100623 batadv_hardif_put(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200624
625out:
626 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100627 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628}
629
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100630/**
631 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
632 * @work: work queue item
633 *
634 * Free the parts of the hard interface which can not be removed under
635 * rtnl lock (to prevent deadlock situations).
636 */
637static void batadv_hardif_remove_interface_finish(struct work_struct *work)
638{
639 struct batadv_hard_iface *hard_iface;
640
641 hard_iface = container_of(work, struct batadv_hard_iface,
642 cleanup_work);
643
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100644 batadv_debugfs_del_hardif(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100645 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100646 batadv_hardif_put(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100647}
648
Sven Eckelmann56303d32012-06-05 22:31:31 +0200649static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200650batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200652 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653 int ret;
654
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200655 ASSERT_RTNL();
656
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200657 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658 if (ret != 1)
659 goto out;
660
661 dev_hold(net_dev);
662
Antonio Quartulli7db3fc22013-04-02 12:16:53 +0200663 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700664 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000665 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666
Sven Eckelmann5853e222012-05-12 02:09:24 +0200667 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668 if (ret)
669 goto free_if;
670
Marek Lindnere6c10f42011-02-18 12:33:20 +0000671 hard_iface->if_num = -1;
672 hard_iface->net_dev = net_dev;
673 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200674 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100675
676 ret = batadv_debugfs_add_hardif(hard_iface);
677 if (ret)
678 goto free_sysfs;
679
Marek Lindnere6c10f42011-02-18 12:33:20 +0000680 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnercef63412015-08-04 21:09:55 +0800681 INIT_HLIST_HEAD(&hard_iface->neigh_list);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100682 INIT_WORK(&hard_iface->cleanup_work,
683 batadv_hardif_remove_interface_finish);
684
Marek Lindnercef63412015-08-04 21:09:55 +0800685 spin_lock_init(&hard_iface->neigh_list_lock);
686
Matthias Schiffercaf65bf2013-03-09 23:14:23 +0100687 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
688 if (batadv_is_wifi_netdev(net_dev))
689 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
690
Marek Lindnered75ccb2011-02-10 14:33:51 +0000691 /* extra reference for return */
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100692 kref_init(&hard_iface->refcount);
693 kref_get(&hard_iface->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200695 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200696 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697
Marek Lindnere6c10f42011-02-18 12:33:20 +0000698 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000699
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100700free_sysfs:
701 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000703 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704release_dev:
705 dev_put(net_dev);
706out:
707 return NULL;
708}
709
Sven Eckelmann56303d32012-06-05 22:31:31 +0200710static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200712 ASSERT_RTNL();
713
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200715 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800716 batadv_hardif_disable_interface(hard_iface,
717 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200719 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720 return;
721
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200722 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100723 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724}
725
Sven Eckelmann95638772012-05-12 02:09:31 +0200726void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000727{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200728 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200730 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000731 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200732 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000733 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200734 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735 }
736 rtnl_unlock();
737}
738
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200739static int batadv_hard_if_event(struct notifier_block *this,
740 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741{
Jiri Pirko351638e2013-05-28 01:30:21 +0000742 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200743 struct batadv_hard_iface *hard_iface;
744 struct batadv_hard_iface *primary_if = NULL;
745 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746
Sven Eckelmann37130292013-02-11 17:10:22 +0800747 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
748 batadv_sysfs_add_meshif(net_dev);
Antonio Quartulli5d2c05b2013-07-02 11:04:34 +0200749 bat_priv = netdev_priv(net_dev);
750 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
Sven Eckelmann37130292013-02-11 17:10:22 +0800751 return NOTIFY_DONE;
752 }
753
Sven Eckelmann56303d32012-06-05 22:31:31 +0200754 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Andrew Lunna1a66b12015-12-03 21:12:33 +0100755 if (!hard_iface && (event == NETDEV_REGISTER ||
756 event == NETDEV_POST_TYPE_CHANGE))
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200757 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000758
Marek Lindnere6c10f42011-02-18 12:33:20 +0000759 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760 goto out;
761
762 switch (event) {
763 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200764 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765 break;
766 case NETDEV_GOING_DOWN:
767 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200768 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769 break;
770 case NETDEV_UNREGISTER:
Andrew Lunna1a66b12015-12-03 21:12:33 +0100771 case NETDEV_PRE_TYPE_CHANGE:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000772 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200774 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775 break;
776 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000777 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200778 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 break;
780 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200781 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782 goto hardif_put;
783
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200784 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785
Marek Lindnere6c10f42011-02-18 12:33:20 +0000786 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800787 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800788
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200789 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200790 if (!primary_if)
791 goto hardif_put;
792
793 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200794 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795 break;
796 default:
797 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000798 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799
800hardif_put:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100801 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200803 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100804 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000805 return NOTIFY_DONE;
806}
807
Sven Eckelmann95638772012-05-12 02:09:31 +0200808struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200809 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000810};