blob: 3696929e5692a1e49b7541c3a8e376457754b9f2 [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
Marek Lindner7db682d2016-05-10 22:31:59 +080040#include "bat_algo.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"
45#include "originator.h"
46#include "packet.h"
47#include "send.h"
48#include "soft-interface.h"
49#include "sysfs.h"
50#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010052/**
53 * batadv_hardif_release - release hard interface from lists and queue for
54 * free after rcu grace period
Sven Eckelmann7a659d52016-01-16 10:29:54 +010055 * @ref: kref pointer of the hard interface
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010056 */
Sven Eckelmann7a659d52016-01-16 10:29:54 +010057void batadv_hardif_release(struct kref *ref)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000058{
Sven Eckelmann7a659d52016-01-16 10:29:54 +010059 struct batadv_hard_iface *hard_iface;
60
61 hard_iface = container_of(ref, struct batadv_hard_iface, refcount);
Marek Lindnere6c10f42011-02-18 12:33:20 +000062 dev_put(hard_iface->net_dev);
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010063
64 kfree_rcu(hard_iface, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065}
66
Sven Eckelmann56303d32012-06-05 22:31:31 +020067struct batadv_hard_iface *
68batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069{
Sven Eckelmann56303d32012-06-05 22:31:31 +020070 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000071
72 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020073 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000074 if (hard_iface->net_dev == net_dev &&
Sven Eckelmann7a659d52016-01-16 10:29:54 +010075 kref_get_unless_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076 goto out;
77 }
78
Marek Lindnere6c10f42011-02-18 12:33:20 +000079 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080
81out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000083 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000084}
85
Antonio Quartullib7eddd02012-09-09 10:46:46 +020086/**
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +010087 * batadv_mutual_parents - check if two devices are each others parent
88 * @dev1: 1st net_device
89 * @dev2: 2nd net_device
90 *
91 * veth devices come in pairs and each is the parent of the other!
92 *
93 * Return: true if the devices are each others parent, otherwise false
94 */
95static bool batadv_mutual_parents(const struct net_device *dev1,
96 const struct net_device *dev2)
97{
98 int dev1_parent_iflink = dev_get_iflink(dev1);
99 int dev2_parent_iflink = dev_get_iflink(dev2);
100
101 if (!dev1_parent_iflink || !dev2_parent_iflink)
102 return false;
103
104 return (dev1_parent_iflink == dev2->ifindex) &&
105 (dev2_parent_iflink == dev1->ifindex);
106}
107
108/**
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200109 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
110 * @net_dev: the device to check
111 *
112 * If the user creates any virtual device on top of a batman-adv interface, it
113 * is important to prevent this new interface to be used to create a new mesh
114 * network (this behaviour would lead to a batman-over-batman configuration).
115 * This function recursively checks all the fathers of the device passed as
116 * argument looking for a batman-adv soft interface.
117 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200118 * Return: true if the device is descendant of a batman-adv mesh interface (or
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200119 * if it is a batman-adv interface itself), false otherwise
120 */
121static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
122{
123 struct net_device *parent_dev;
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200124 struct net *net = dev_net(net_dev);
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200125 bool ret;
126
127 /* check if this is a batman-adv mesh interface */
128 if (batadv_softif_is_valid(net_dev))
129 return true;
130
131 /* no more parents..stop recursion */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200132 if (dev_get_iflink(net_dev) == 0 ||
133 dev_get_iflink(net_dev) == net_dev->ifindex)
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200134 return false;
135
136 /* recurse over the parent device */
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200137 parent_dev = __dev_get_by_index(net, dev_get_iflink(net_dev));
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200138 /* if we got a NULL parent_dev there is something broken.. */
139 if (WARN(!parent_dev, "Cannot find parent device"))
140 return false;
141
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100142 if (batadv_mutual_parents(net_dev, parent_dev))
143 return false;
144
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200145 ret = batadv_is_on_batman_iface(parent_dev);
146
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200147 return ret;
148}
149
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100150static bool batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151{
152 if (net_dev->flags & IFF_LOOPBACK)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100153 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154
155 if (net_dev->type != ARPHRD_ETHER)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100156 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157
158 if (net_dev->addr_len != ETH_ALEN)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100159 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160
161 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200162 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100163 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100165 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166}
167
Matthias Schifferde68d102013-03-09 23:14:22 +0100168/**
169 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
170 * interface
171 * @net_device: the device to check
172 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200173 * Return: true if the net device is a 802.11 wireless device, false otherwise.
Matthias Schifferde68d102013-03-09 23:14:22 +0100174 */
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200175bool batadv_is_wifi_netdev(struct net_device *net_device)
Matthias Schifferde68d102013-03-09 23:14:22 +0100176{
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200177 if (!net_device)
178 return false;
179
Matthias Schifferde68d102013-03-09 23:14:22 +0100180#ifdef CONFIG_WIRELESS_EXT
181 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
182 * check for wireless_handlers != NULL
183 */
184 if (net_device->wireless_handlers)
185 return true;
186#endif
187
188 /* cfg80211 drivers have to set ieee80211_ptr */
189 if (net_device->ieee80211_ptr)
190 return true;
191
192 return false;
193}
194
Sven Eckelmann56303d32012-06-05 22:31:31 +0200195static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200196batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200198 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199
200 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200201 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000202 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203 continue;
204
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200205 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100206 kref_get_unless_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 goto out;
208 }
209
Marek Lindnere6c10f42011-02-18 12:33:20 +0000210 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
212out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000214 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215}
216
Sven Eckelmann56303d32012-06-05 22:31:31 +0200217static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
218 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200220 struct batadv_hard_iface *primary_if;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200221
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200222 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200223 if (!primary_if)
224 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225
Antonio Quartulli785ea112011-11-23 11:35:44 +0100226 batadv_dat_init_own_addr(bat_priv, primary_if);
Sven Eckelmann08adf152012-05-12 13:38:47 +0200227 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200228out:
229 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100230 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231}
232
Sven Eckelmann56303d32012-06-05 22:31:31 +0200233static void batadv_primary_if_select(struct batadv_priv *bat_priv,
234 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000235{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200236 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200238 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239
Sven Eckelmann17a86912016-04-11 13:06:40 +0200240 if (new_hard_iface)
241 kref_get(&new_hard_iface->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200243 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200244 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245
Marek Lindner32ae9b22011-04-20 15:40:58 +0200246 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100247 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200248
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800249 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200250 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100251
252out:
253 if (curr_hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100254 batadv_hardif_put(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255}
256
Sven Eckelmann56303d32012-06-05 22:31:31 +0200257static bool
258batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000260 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261 return true;
262
263 return false;
264}
265
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200266static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200268 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
270 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200271 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200272 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
273 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274 continue;
275
Marek Lindnere6c10f42011-02-18 12:33:20 +0000276 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277 continue;
278
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200279 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
280 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281 continue;
282
Sven Eckelmann67969582012-03-26 16:22:45 +0200283 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
284 net_dev->dev_addr, hard_iface->net_dev->name);
285 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286 }
287 rcu_read_unlock();
288}
289
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200290/**
291 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
292 * @soft_iface: netdev struct of the mesh interface
293 */
294static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
295{
296 const struct batadv_hard_iface *hard_iface;
297 unsigned short lower_header_len = ETH_HLEN;
298 unsigned short lower_headroom = 0;
299 unsigned short lower_tailroom = 0;
300 unsigned short needed_headroom;
301
302 rcu_read_lock();
303 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
304 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
305 continue;
306
307 if (hard_iface->soft_iface != soft_iface)
308 continue;
309
310 lower_header_len = max_t(unsigned short, lower_header_len,
311 hard_iface->net_dev->hard_header_len);
312
313 lower_headroom = max_t(unsigned short, lower_headroom,
314 hard_iface->net_dev->needed_headroom);
315
316 lower_tailroom = max_t(unsigned short, lower_tailroom,
317 hard_iface->net_dev->needed_tailroom);
318 }
319 rcu_read_unlock();
320
321 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
322 needed_headroom += batadv_max_header_len();
323
324 soft_iface->needed_headroom = needed_headroom;
325 soft_iface->needed_tailroom = lower_tailroom;
326}
327
Sven Eckelmann95638772012-05-12 02:09:31 +0200328int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800330 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200331 const struct batadv_hard_iface *hard_iface;
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100332 int min_mtu = INT_MAX;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333
334 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200335 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200336 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
337 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338 continue;
339
Marek Lindnere6c10f42011-02-18 12:33:20 +0000340 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341 continue;
342
Marek Lindnera19d3d82013-05-27 15:33:25 +0800343 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344 }
345 rcu_read_unlock();
Marek Lindnera19d3d82013-05-27 15:33:25 +0800346
Marek Lindnera19d3d82013-05-27 15:33:25 +0800347 if (atomic_read(&bat_priv->fragmentation) == 0)
348 goto out;
349
350 /* with fragmentation enabled the maximum size of internally generated
351 * packets such as translation table exchanges or tvlv containers, etc
352 * has to be calculated
353 */
354 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
355 min_mtu -= sizeof(struct batadv_frag_packet);
356 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800357
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358out:
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100359 /* report to the other components the maximum amount of bytes that
360 * batman-adv can send over the wire (without considering the payload
361 * overhead). For example, this value is used by TT to compute the
362 * maximum local table table size
363 */
364 atomic_set(&bat_priv->packet_size_max, min_mtu);
365
366 /* the real soft-interface MTU is computed by removing the payload
367 * overhead from the maximum amount of bytes that was just computed.
368 *
369 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
370 */
371 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372}
373
374/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200375void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800377 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378
Marek Lindnera19d3d82013-05-27 15:33:25 +0800379 /* Check if the local translate table should be cleaned up to match a
380 * new (and smaller) MTU.
381 */
382 batadv_tt_local_resize_to_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383}
384
Sven Eckelmann56303d32012-06-05 22:31:31 +0200385static void
386batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200388 struct batadv_priv *bat_priv;
389 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200391 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200392 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393
Marek Lindnere6c10f42011-02-18 12:33:20 +0000394 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395
Marek Lindnerc3229392012-03-11 06:17:50 +0800396 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200397 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200399 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200400 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200402 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200403 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200404 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
Sven Eckelmann3e348192012-05-16 20:23:22 +0200406 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
407 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Sven Eckelmann95638772012-05-12 02:09:31 +0200409 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200410
Antonio Quartullib6cf5d42016-04-14 09:37:05 +0800411 if (bat_priv->bat_algo_ops->bat_iface_activate)
412 bat_priv->bat_algo_ops->bat_iface_activate(hard_iface);
413
Marek Lindner32ae9b22011-04-20 15:40:58 +0200414out:
415 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100416 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417}
418
Sven Eckelmann56303d32012-06-05 22:31:31 +0200419static void
420batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200422 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
423 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 return;
425
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200426 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427
Sven Eckelmann3e348192012-05-16 20:23:22 +0200428 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
429 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430
Sven Eckelmann95638772012-05-12 02:09:31 +0200431 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432}
433
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100434/**
435 * batadv_master_del_slave - remove hard_iface from the current master interface
436 * @slave: the interface enslaved in another master
437 * @master: the master from which slave has to be removed
438 *
439 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
440 * is free'd and master can correctly change its internal state.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200441 *
442 * Return: 0 on success, a negative value representing the error otherwise
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100443 */
444static int batadv_master_del_slave(struct batadv_hard_iface *slave,
445 struct net_device *master)
446{
447 int ret;
448
449 if (!master)
450 return 0;
451
452 ret = -EBUSY;
453 if (master->netdev_ops->ndo_del_slave)
454 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
455
456 return ret;
457}
458
Sven Eckelmann56303d32012-06-05 22:31:31 +0200459int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200460 struct net *net, const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200462 struct batadv_priv *bat_priv;
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100463 struct net_device *soft_iface, *master;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200464 __be16 ethertype = htons(ETH_P_BATMAN);
Marek Lindner411d6ed2013-05-08 13:31:59 +0800465 int max_header_len = batadv_max_header_len();
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000466 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200468 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469 goto out;
470
Sven Eckelmann17a86912016-04-11 13:06:40 +0200471 kref_get(&hard_iface->refcount);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000472
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 +0000557out:
558 return 0;
559
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800560err_upper:
561 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
Marek Lindner77af7572012-02-07 17:20:48 +0800562err_dev:
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800563 hard_iface->soft_iface = NULL;
Marek Lindner77af7572012-02-07 17:20:48 +0800564 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565err:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100566 batadv_hardif_put(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000567 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568}
569
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800570void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
571 enum batadv_hard_if_cleanup autodel)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200573 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
574 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575
Sven Eckelmannf2d23862016-03-19 13:55:21 +0100576 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000577
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200578 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200579 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580
Sven Eckelmann3e348192012-05-16 20:23:22 +0200581 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
582 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000583 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannd7d6de92016-03-05 16:09:18 +0100584 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585
586 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200587 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200589 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200590 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200591 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200593 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
594 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595
596 if (new_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100597 batadv_hardif_put(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598 }
599
Marek Lindner00a50072012-02-07 17:20:47 +0800600 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200601 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602
Marek Lindnere6c10f42011-02-18 12:33:20 +0000603 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200604 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200605 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000606 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607
Antonio Quartullia5256f72015-08-04 22:26:19 +0200608 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200609 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
Antonio Quartullia5256f72015-08-04 22:26:19 +0200610
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611 /* nobody uses this interface anymore */
Antonio Quartulli8257f552013-08-19 18:39:59 +0200612 if (!bat_priv->num_ifaces) {
613 batadv_gw_check_client_stop(bat_priv);
614
615 if (autodel == BATADV_IF_CLEANUP_AUTO)
616 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
617 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618
Marek Lindnere6c10f42011-02-18 12:33:20 +0000619 hard_iface->soft_iface = NULL;
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100620 batadv_hardif_put(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200621
622out:
623 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100624 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625}
626
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100627/**
628 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
629 * @work: work queue item
630 *
631 * Free the parts of the hard interface which can not be removed under
632 * rtnl lock (to prevent deadlock situations).
633 */
634static void batadv_hardif_remove_interface_finish(struct work_struct *work)
635{
636 struct batadv_hard_iface *hard_iface;
637
638 hard_iface = container_of(work, struct batadv_hard_iface,
639 cleanup_work);
640
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100641 batadv_debugfs_del_hardif(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100642 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100643 batadv_hardif_put(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100644}
645
Sven Eckelmann56303d32012-06-05 22:31:31 +0200646static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200647batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000648{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200649 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650 int ret;
651
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200652 ASSERT_RTNL();
653
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100654 if (!batadv_is_valid_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655 goto out;
656
657 dev_hold(net_dev);
658
Antonio Quartulli7db3fc22013-04-02 12:16:53 +0200659 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700660 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662
Sven Eckelmann5853e222012-05-12 02:09:24 +0200663 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664 if (ret)
665 goto free_if;
666
Marek Lindnere6c10f42011-02-18 12:33:20 +0000667 hard_iface->if_num = -1;
668 hard_iface->net_dev = net_dev;
669 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200670 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100671
672 ret = batadv_debugfs_add_hardif(hard_iface);
673 if (ret)
674 goto free_sysfs;
675
Marek Lindnere6c10f42011-02-18 12:33:20 +0000676 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnercef63412015-08-04 21:09:55 +0800677 INIT_HLIST_HEAD(&hard_iface->neigh_list);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100678 INIT_WORK(&hard_iface->cleanup_work,
679 batadv_hardif_remove_interface_finish);
680
Marek Lindnercef63412015-08-04 21:09:55 +0800681 spin_lock_init(&hard_iface->neigh_list_lock);
682
Matthias Schiffercaf65bf2013-03-09 23:14:23 +0100683 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
684 if (batadv_is_wifi_netdev(net_dev))
685 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
686
Marek Lindner7db682d2016-05-10 22:31:59 +0800687 batadv_v_hardif_init(hard_iface);
688
Marek Lindnered75ccb2011-02-10 14:33:51 +0000689 /* extra reference for return */
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100690 kref_init(&hard_iface->refcount);
691 kref_get(&hard_iface->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200693 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200694 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695
Marek Lindnere6c10f42011-02-18 12:33:20 +0000696 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100698free_sysfs:
699 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000701 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702release_dev:
703 dev_put(net_dev);
704out:
705 return NULL;
706}
707
Sven Eckelmann56303d32012-06-05 22:31:31 +0200708static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200710 ASSERT_RTNL();
711
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000712 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200713 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800714 batadv_hardif_disable_interface(hard_iface,
715 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200717 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718 return;
719
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200720 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100721 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000722}
723
Sven Eckelmann95638772012-05-12 02:09:31 +0200724void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200726 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000727
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200728 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000729 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200730 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000731 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200732 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733 }
734 rtnl_unlock();
735}
736
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200737static int batadv_hard_if_event(struct notifier_block *this,
738 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000739{
Jiri Pirko351638e2013-05-28 01:30:21 +0000740 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200741 struct batadv_hard_iface *hard_iface;
742 struct batadv_hard_iface *primary_if = NULL;
743 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744
Sven Eckelmann37130292013-02-11 17:10:22 +0800745 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
746 batadv_sysfs_add_meshif(net_dev);
Antonio Quartulli5d2c05b2013-07-02 11:04:34 +0200747 bat_priv = netdev_priv(net_dev);
748 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
Sven Eckelmann37130292013-02-11 17:10:22 +0800749 return NOTIFY_DONE;
750 }
751
Sven Eckelmann56303d32012-06-05 22:31:31 +0200752 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Andrew Lunna1a66b12015-12-03 21:12:33 +0100753 if (!hard_iface && (event == NETDEV_REGISTER ||
754 event == NETDEV_POST_TYPE_CHANGE))
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200755 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000756
Marek Lindnere6c10f42011-02-18 12:33:20 +0000757 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000758 goto out;
759
760 switch (event) {
761 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200762 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000763 break;
764 case NETDEV_GOING_DOWN:
765 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200766 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767 break;
768 case NETDEV_UNREGISTER:
Andrew Lunna1a66b12015-12-03 21:12:33 +0100769 case NETDEV_PRE_TYPE_CHANGE:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000770 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200772 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773 break;
774 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000775 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200776 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777 break;
778 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200779 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780 goto hardif_put;
781
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200782 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783
Marek Lindnere6c10f42011-02-18 12:33:20 +0000784 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800785 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800786
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200787 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200788 if (!primary_if)
789 goto hardif_put;
790
791 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200792 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793 break;
794 default:
795 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000796 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797
798hardif_put:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100799 batadv_hardif_put(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000800out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200801 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100802 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000803 return NOTIFY_DONE;
804}
805
Sven Eckelmann95638772012-05-12 02:09:31 +0200806struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200807 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808};