blob: 1f9080840566720fdce2ddaff2a8e9c24084231e [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 Eckelmannfcafa5e2016-05-15 11:07:42 +020026#include <linux/if.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include <linux/if_arp.h>
Antonio Quartulliaf5d4f72012-11-26 00:38:50 +010028#include <linux/if_ether.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#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
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 Lunn1bc4e2b2016-02-11 22:15:57 +010088 * batadv_mutual_parents - check if two devices are each others parent
89 * @dev1: 1st net_device
90 * @dev2: 2nd net_device
91 *
92 * veth devices come in pairs and each is the parent of the other!
93 *
94 * Return: true if the devices are each others parent, otherwise false
95 */
96static bool batadv_mutual_parents(const struct net_device *dev1,
97 const struct net_device *dev2)
98{
99 int dev1_parent_iflink = dev_get_iflink(dev1);
100 int dev2_parent_iflink = dev_get_iflink(dev2);
101
102 if (!dev1_parent_iflink || !dev2_parent_iflink)
103 return false;
104
105 return (dev1_parent_iflink == dev2->ifindex) &&
106 (dev2_parent_iflink == dev1->ifindex);
107}
108
109/**
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200110 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
111 * @net_dev: the device to check
112 *
113 * If the user creates any virtual device on top of a batman-adv interface, it
114 * is important to prevent this new interface to be used to create a new mesh
115 * network (this behaviour would lead to a batman-over-batman configuration).
116 * This function recursively checks all the fathers of the device passed as
117 * argument looking for a batman-adv soft interface.
118 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200119 * Return: true if the device is descendant of a batman-adv mesh interface (or
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200120 * if it is a batman-adv interface itself), false otherwise
121 */
122static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
123{
124 struct net_device *parent_dev;
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200125 struct net *net = dev_net(net_dev);
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200126 bool ret;
127
128 /* check if this is a batman-adv mesh interface */
129 if (batadv_softif_is_valid(net_dev))
130 return true;
131
132 /* no more parents..stop recursion */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200133 if (dev_get_iflink(net_dev) == 0 ||
134 dev_get_iflink(net_dev) == net_dev->ifindex)
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200135 return false;
136
137 /* recurse over the parent device */
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200138 parent_dev = __dev_get_by_index(net, dev_get_iflink(net_dev));
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200139 /* if we got a NULL parent_dev there is something broken.. */
140 if (WARN(!parent_dev, "Cannot find parent device"))
141 return false;
142
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100143 if (batadv_mutual_parents(net_dev, parent_dev))
144 return false;
145
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200146 ret = batadv_is_on_batman_iface(parent_dev);
147
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200148 return ret;
149}
150
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100151static bool batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000152{
153 if (net_dev->flags & IFF_LOOPBACK)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100154 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155
156 if (net_dev->type != ARPHRD_ETHER)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100157 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158
159 if (net_dev->addr_len != ETH_ALEN)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100160 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161
162 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200163 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100164 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100166 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167}
168
Matthias Schifferde68d102013-03-09 23:14:22 +0100169/**
170 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
171 * interface
172 * @net_device: the device to check
173 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200174 * Return: true if the net device is a 802.11 wireless device, false otherwise.
Matthias Schifferde68d102013-03-09 23:14:22 +0100175 */
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200176bool batadv_is_wifi_netdev(struct net_device *net_device)
Matthias Schifferde68d102013-03-09 23:14:22 +0100177{
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200178 if (!net_device)
179 return false;
180
Matthias Schifferde68d102013-03-09 23:14:22 +0100181#ifdef CONFIG_WIRELESS_EXT
182 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
183 * check for wireless_handlers != NULL
184 */
185 if (net_device->wireless_handlers)
186 return true;
187#endif
188
189 /* cfg80211 drivers have to set ieee80211_ptr */
190 if (net_device->ieee80211_ptr)
191 return true;
192
193 return false;
194}
195
Sven Eckelmann56303d32012-06-05 22:31:31 +0200196static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200197batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200199 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200
201 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200202 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000203 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204 continue;
205
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200206 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100207 kref_get_unless_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 goto out;
209 }
210
Marek Lindnere6c10f42011-02-18 12:33:20 +0000211 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212
213out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000215 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216}
217
Sven Eckelmann56303d32012-06-05 22:31:31 +0200218static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
219 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200221 struct batadv_hard_iface *primary_if;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200222
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200223 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200224 if (!primary_if)
225 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
Antonio Quartulli785ea112011-11-23 11:35:44 +0100227 batadv_dat_init_own_addr(bat_priv, primary_if);
Sven Eckelmann08adf152012-05-12 13:38:47 +0200228 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200229out:
230 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100231 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232}
233
Sven Eckelmann56303d32012-06-05 22:31:31 +0200234static void batadv_primary_if_select(struct batadv_priv *bat_priv,
235 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200237 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200239 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240
Sven Eckelmann17a86912016-04-11 13:06:40 +0200241 if (new_hard_iface)
242 kref_get(&new_hard_iface->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200244 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200245 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246
Marek Lindner32ae9b22011-04-20 15:40:58 +0200247 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100248 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200249
Antonio Quartulli29824a52016-05-25 23:27:31 +0800250 bat_priv->algo_ops->iface.primary_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200251 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100252
253out:
254 if (curr_hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100255 batadv_hardif_put(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256}
257
Sven Eckelmann56303d32012-06-05 22:31:31 +0200258static bool
259batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000261 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262 return true;
263
264 return false;
265}
266
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200267static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200269 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270
271 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200272 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200273 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
274 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275 continue;
276
Marek Lindnere6c10f42011-02-18 12:33:20 +0000277 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278 continue;
279
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200280 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
281 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282 continue;
283
Sven Eckelmann67969582012-03-26 16:22:45 +0200284 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
285 net_dev->dev_addr, hard_iface->net_dev->name);
286 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 }
288 rcu_read_unlock();
289}
290
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200291/**
292 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
293 * @soft_iface: netdev struct of the mesh interface
294 */
295static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
296{
297 const struct batadv_hard_iface *hard_iface;
298 unsigned short lower_header_len = ETH_HLEN;
299 unsigned short lower_headroom = 0;
300 unsigned short lower_tailroom = 0;
301 unsigned short needed_headroom;
302
303 rcu_read_lock();
304 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
305 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
306 continue;
307
308 if (hard_iface->soft_iface != soft_iface)
309 continue;
310
311 lower_header_len = max_t(unsigned short, lower_header_len,
312 hard_iface->net_dev->hard_header_len);
313
314 lower_headroom = max_t(unsigned short, lower_headroom,
315 hard_iface->net_dev->needed_headroom);
316
317 lower_tailroom = max_t(unsigned short, lower_tailroom,
318 hard_iface->net_dev->needed_tailroom);
319 }
320 rcu_read_unlock();
321
322 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
323 needed_headroom += batadv_max_header_len();
324
325 soft_iface->needed_headroom = needed_headroom;
326 soft_iface->needed_tailroom = lower_tailroom;
327}
328
Sven Eckelmann95638772012-05-12 02:09:31 +0200329int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800331 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200332 const struct batadv_hard_iface *hard_iface;
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100333 int min_mtu = INT_MAX;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334
335 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200336 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200337 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
338 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339 continue;
340
Marek Lindnere6c10f42011-02-18 12:33:20 +0000341 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 continue;
343
Marek Lindnera19d3d82013-05-27 15:33:25 +0800344 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345 }
346 rcu_read_unlock();
Marek Lindnera19d3d82013-05-27 15:33:25 +0800347
Marek Lindnera19d3d82013-05-27 15:33:25 +0800348 if (atomic_read(&bat_priv->fragmentation) == 0)
349 goto out;
350
351 /* with fragmentation enabled the maximum size of internally generated
352 * packets such as translation table exchanges or tvlv containers, etc
353 * has to be calculated
354 */
355 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
356 min_mtu -= sizeof(struct batadv_frag_packet);
357 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800358
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359out:
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100360 /* report to the other components the maximum amount of bytes that
361 * batman-adv can send over the wire (without considering the payload
362 * overhead). For example, this value is used by TT to compute the
363 * maximum local table table size
364 */
365 atomic_set(&bat_priv->packet_size_max, min_mtu);
366
367 /* the real soft-interface MTU is computed by removing the payload
368 * overhead from the maximum amount of bytes that was just computed.
369 *
370 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
371 */
372 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373}
374
375/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200376void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800378 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379
Marek Lindnera19d3d82013-05-27 15:33:25 +0800380 /* Check if the local translate table should be cleaned up to match a
381 * new (and smaller) MTU.
382 */
383 batadv_tt_local_resize_to_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384}
385
Sven Eckelmann56303d32012-06-05 22:31:31 +0200386static void
387batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200389 struct batadv_priv *bat_priv;
390 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200392 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200393 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394
Marek Lindnere6c10f42011-02-18 12:33:20 +0000395 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
Antonio Quartulli29824a52016-05-25 23:27:31 +0800397 bat_priv->algo_ops->iface.update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200398 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200400 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200401 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200403 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200404 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200405 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406
Sven Eckelmann3e348192012-05-16 20:23:22 +0200407 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
408 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
Sven Eckelmann95638772012-05-12 02:09:31 +0200410 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200411
Antonio Quartulli29824a52016-05-25 23:27:31 +0800412 if (bat_priv->algo_ops->iface.activate)
413 bat_priv->algo_ops->iface.activate(hard_iface);
Antonio Quartullib6cf5d42016-04-14 09:37:05 +0800414
Marek Lindner32ae9b22011-04-20 15:40:58 +0200415out:
416 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100417 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418}
419
Sven Eckelmann56303d32012-06-05 22:31:31 +0200420static void
421batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200423 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
424 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425 return;
426
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200427 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428
Sven Eckelmann3e348192012-05-16 20:23:22 +0200429 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
430 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431
Sven Eckelmann95638772012-05-12 02:09:31 +0200432 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433}
434
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100435/**
436 * batadv_master_del_slave - remove hard_iface from the current master interface
437 * @slave: the interface enslaved in another master
438 * @master: the master from which slave has to be removed
439 *
440 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
441 * is free'd and master can correctly change its internal state.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200442 *
443 * Return: 0 on success, a negative value representing the error otherwise
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100444 */
445static int batadv_master_del_slave(struct batadv_hard_iface *slave,
446 struct net_device *master)
447{
448 int ret;
449
450 if (!master)
451 return 0;
452
453 ret = -EBUSY;
454 if (master->netdev_ops->ndo_del_slave)
455 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
456
457 return ret;
458}
459
Sven Eckelmann56303d32012-06-05 22:31:31 +0200460int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200461 struct net *net, const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200463 struct batadv_priv *bat_priv;
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100464 struct net_device *soft_iface, *master;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200465 __be16 ethertype = htons(ETH_P_BATMAN);
Marek Lindner411d6ed2013-05-08 13:31:59 +0800466 int max_header_len = batadv_max_header_len();
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000467 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200469 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 goto out;
471
Sven Eckelmann17a86912016-04-11 13:06:40 +0200472 kref_get(&hard_iface->refcount);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000473
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200474 soft_iface = dev_get_by_name(net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000476 if (!soft_iface) {
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200477 soft_iface = batadv_softif_create(net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000479 if (!soft_iface) {
480 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000481 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000482 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483
484 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000485 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486 }
487
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200488 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100489 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000490 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000491 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800492 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000493 }
494
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100495 /* check if the interface is enslaved in another virtual one and
496 * in that case unlink it first
497 */
498 master = netdev_master_upper_dev_get(hard_iface->net_dev);
499 ret = batadv_master_del_slave(hard_iface, master);
500 if (ret)
501 goto err_dev;
502
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000503 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000504 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200505
Jiri Pirko6dffb042015-12-03 12:12:10 +0100506 ret = netdev_master_upper_dev_link(hard_iface->net_dev,
Jiri Pirko29bf24a2015-12-03 12:12:11 +0100507 soft_iface, NULL, NULL);
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800508 if (ret)
509 goto err_dev;
510
Antonio Quartulli29824a52016-05-25 23:27:31 +0800511 ret = bat_priv->algo_ops->iface.enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200512 if (ret < 0)
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800513 goto err_upper;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514
Marek Lindnere6c10f42011-02-18 12:33:20 +0000515 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200517 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200518 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
519 if (ret < 0) {
Antonio Quartulli29824a52016-05-25 23:27:31 +0800520 bat_priv->algo_ops->iface.disable(hard_iface);
Simon Wunderlich62446302012-07-01 22:51:55 +0200521 bat_priv->num_ifaces--;
522 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800523 goto err_upper;
Simon Wunderlich62446302012-07-01 22:51:55 +0200524 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525
Sven Eckelmannd7d6de92016-03-05 16:09:18 +0100526 kref_get(&hard_iface->refcount);
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200527 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200528 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000529 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
530 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531
Sven Eckelmann3e348192012-05-16 20:23:22 +0200532 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
533 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200535 if (atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800536 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200537 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800538 "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 +0200539 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800540 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200542 if (!atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800543 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200544 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800545 "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 +0200546 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800547 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200549 if (batadv_hardif_is_iface_up(hard_iface))
550 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200552 batadv_err(hard_iface->soft_iface,
553 "Not using interface %s (retrying later): interface not active\n",
554 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200556 batadv_hardif_recalc_extra_skbroom(soft_iface);
557
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558out:
559 return 0;
560
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800561err_upper:
562 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
Marek Lindner77af7572012-02-07 17:20:48 +0800563err_dev:
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800564 hard_iface->soft_iface = NULL;
Marek Lindner77af7572012-02-07 17:20:48 +0800565 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566err:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100567 batadv_hardif_put(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000568 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569}
570
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800571void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
572 enum batadv_hard_if_cleanup autodel)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000573{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200574 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
575 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576
Sven Eckelmannf2d23862016-03-19 13:55:21 +0100577 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000578
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200579 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200580 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581
Sven Eckelmann3e348192012-05-16 20:23:22 +0200582 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
583 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000584 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannd7d6de92016-03-05 16:09:18 +0100585 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586
587 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200588 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200590 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200591 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200592 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200594 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
595 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596
597 if (new_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100598 batadv_hardif_put(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599 }
600
Antonio Quartulli29824a52016-05-25 23:27:31 +0800601 bat_priv->algo_ops->iface.disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200602 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603
Marek Lindnere6c10f42011-02-18 12:33:20 +0000604 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200605 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200606 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000607 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608
Antonio Quartullia5256f72015-08-04 22:26:19 +0200609 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200610 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
Antonio Quartullia5256f72015-08-04 22:26:19 +0200611
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612 /* nobody uses this interface anymore */
Antonio Quartulli8257f552013-08-19 18:39:59 +0200613 if (!bat_priv->num_ifaces) {
614 batadv_gw_check_client_stop(bat_priv);
615
616 if (autodel == BATADV_IF_CLEANUP_AUTO)
617 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
618 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619
Marek Lindnere6c10f42011-02-18 12:33:20 +0000620 hard_iface->soft_iface = NULL;
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100621 batadv_hardif_put(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200622
623out:
624 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100625 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626}
627
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100628/**
629 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
630 * @work: work queue item
631 *
632 * Free the parts of the hard interface which can not be removed under
633 * rtnl lock (to prevent deadlock situations).
634 */
635static void batadv_hardif_remove_interface_finish(struct work_struct *work)
636{
637 struct batadv_hard_iface *hard_iface;
638
639 hard_iface = container_of(work, struct batadv_hard_iface,
640 cleanup_work);
641
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100642 batadv_debugfs_del_hardif(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100643 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100644 batadv_hardif_put(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100645}
646
Sven Eckelmann56303d32012-06-05 22:31:31 +0200647static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200648batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000649{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200650 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651 int ret;
652
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200653 ASSERT_RTNL();
654
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100655 if (!batadv_is_valid_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656 goto out;
657
658 dev_hold(net_dev);
659
Antonio Quartulli7db3fc22013-04-02 12:16:53 +0200660 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700661 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663
Sven Eckelmann5853e222012-05-12 02:09:24 +0200664 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000665 if (ret)
666 goto free_if;
667
Marek Lindnere6c10f42011-02-18 12:33:20 +0000668 hard_iface->if_num = -1;
669 hard_iface->net_dev = net_dev;
670 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200671 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100672
673 ret = batadv_debugfs_add_hardif(hard_iface);
674 if (ret)
675 goto free_sysfs;
676
Marek Lindnere6c10f42011-02-18 12:33:20 +0000677 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnercef63412015-08-04 21:09:55 +0800678 INIT_HLIST_HEAD(&hard_iface->neigh_list);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100679 INIT_WORK(&hard_iface->cleanup_work,
680 batadv_hardif_remove_interface_finish);
681
Marek Lindnercef63412015-08-04 21:09:55 +0800682 spin_lock_init(&hard_iface->neigh_list_lock);
683
Matthias Schiffercaf65bf2013-03-09 23:14:23 +0100684 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
685 if (batadv_is_wifi_netdev(net_dev))
686 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
687
Marek Lindner7db682d2016-05-10 22:31:59 +0800688 batadv_v_hardif_init(hard_iface);
689
Marek Lindnered75ccb2011-02-10 14:33:51 +0000690 /* extra reference for return */
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100691 kref_init(&hard_iface->refcount);
692 kref_get(&hard_iface->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200694 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200695 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696
Marek Lindnere6c10f42011-02-18 12:33:20 +0000697 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100699free_sysfs:
700 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000701free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000702 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703release_dev:
704 dev_put(net_dev);
705out:
706 return NULL;
707}
708
Sven Eckelmann56303d32012-06-05 22:31:31 +0200709static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000710{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200711 ASSERT_RTNL();
712
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200714 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800715 batadv_hardif_disable_interface(hard_iface,
716 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000717
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200718 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719 return;
720
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200721 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100722 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000723}
724
Sven Eckelmann95638772012-05-12 02:09:31 +0200725void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200727 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000728
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200729 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000730 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200731 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000732 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200733 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734 }
735 rtnl_unlock();
736}
737
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200738static int batadv_hard_if_event(struct notifier_block *this,
739 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740{
Jiri Pirko351638e2013-05-28 01:30:21 +0000741 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200742 struct batadv_hard_iface *hard_iface;
743 struct batadv_hard_iface *primary_if = NULL;
744 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000745
Sven Eckelmann37130292013-02-11 17:10:22 +0800746 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
747 batadv_sysfs_add_meshif(net_dev);
Antonio Quartulli5d2c05b2013-07-02 11:04:34 +0200748 bat_priv = netdev_priv(net_dev);
749 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
Sven Eckelmann37130292013-02-11 17:10:22 +0800750 return NOTIFY_DONE;
751 }
752
Sven Eckelmann56303d32012-06-05 22:31:31 +0200753 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Andrew Lunna1a66b12015-12-03 21:12:33 +0100754 if (!hard_iface && (event == NETDEV_REGISTER ||
755 event == NETDEV_POST_TYPE_CHANGE))
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200756 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000757
Marek Lindnere6c10f42011-02-18 12:33:20 +0000758 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759 goto out;
760
761 switch (event) {
762 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200763 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764 break;
765 case NETDEV_GOING_DOWN:
766 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200767 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000768 break;
769 case NETDEV_UNREGISTER:
Andrew Lunna1a66b12015-12-03 21:12:33 +0100770 case NETDEV_PRE_TYPE_CHANGE:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000771 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200773 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774 break;
775 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000776 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200777 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778 break;
779 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200780 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781 goto hardif_put;
782
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200783 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784
Marek Lindnere6c10f42011-02-18 12:33:20 +0000785 bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartulli29824a52016-05-25 23:27:31 +0800786 bat_priv->algo_ops->iface.update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800787
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200788 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200789 if (!primary_if)
790 goto hardif_put;
791
792 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200793 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 break;
795 default:
796 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000797 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798
799hardif_put:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100800 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200802 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100803 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804 return NOTIFY_DONE;
805}
806
Sven Eckelmann95638772012-05-12 02:09:31 +0200807struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200808 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000809};