blob: 57f7107169f58c5e772887a6260d070b1eb9fc1e [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2007-2015 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 Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/bug.h>
22#include <linux/byteorder/generic.h>
23#include <linux/errno.h>
24#include <linux/fs.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include <linux/if_arp.h>
Antonio Quartulliaf5d4f72012-11-26 00:38:50 +010026#include <linux/if_ether.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/if.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/netdevice.h>
31#include <linux/printk.h>
32#include <linux/rculist.h>
33#include <linux/rtnetlink.h>
34#include <linux/slab.h>
Marek Lindnercef63412015-08-04 21:09:55 +080035#include <linux/spinlock.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020036#include <linux/workqueue.h>
37#include <net/net_namespace.h>
38
39#include "bridge_loop_avoidance.h"
40#include "debugfs.h"
41#include "distributed-arp-table.h"
42#include "gateway_client.h"
43#include "originator.h"
44#include "packet.h"
45#include "send.h"
46#include "soft-interface.h"
47#include "sysfs.h"
48#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049
Sven Eckelmann95638772012-05-12 02:09:31 +020050void batadv_hardif_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051{
Sven Eckelmann56303d32012-06-05 22:31:31 +020052 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053
Sven Eckelmann56303d32012-06-05 22:31:31 +020054 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
Marek Lindnere6c10f42011-02-18 12:33:20 +000055 dev_put(hard_iface->net_dev);
56 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057}
58
Sven Eckelmann56303d32012-06-05 22:31:31 +020059struct batadv_hard_iface *
60batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061{
Sven Eckelmann56303d32012-06-05 22:31:31 +020062 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063
64 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020065 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000066 if (hard_iface->net_dev == net_dev &&
67 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000068 goto out;
69 }
70
Marek Lindnere6c10f42011-02-18 12:33:20 +000071 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072
73out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000074 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000075 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076}
77
Antonio Quartullib7eddd02012-09-09 10:46:46 +020078/**
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +010079 * batadv_mutual_parents - check if two devices are each others parent
80 * @dev1: 1st net_device
81 * @dev2: 2nd net_device
82 *
83 * veth devices come in pairs and each is the parent of the other!
84 *
85 * Return: true if the devices are each others parent, otherwise false
86 */
87static bool batadv_mutual_parents(const struct net_device *dev1,
88 const struct net_device *dev2)
89{
90 int dev1_parent_iflink = dev_get_iflink(dev1);
91 int dev2_parent_iflink = dev_get_iflink(dev2);
92
93 if (!dev1_parent_iflink || !dev2_parent_iflink)
94 return false;
95
96 return (dev1_parent_iflink == dev2->ifindex) &&
97 (dev2_parent_iflink == dev1->ifindex);
98}
99
100/**
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200101 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
102 * @net_dev: the device to check
103 *
104 * If the user creates any virtual device on top of a batman-adv interface, it
105 * is important to prevent this new interface to be used to create a new mesh
106 * network (this behaviour would lead to a batman-over-batman configuration).
107 * This function recursively checks all the fathers of the device passed as
108 * argument looking for a batman-adv soft interface.
109 *
110 * Returns true if the device is descendant of a batman-adv mesh interface (or
111 * if it is a batman-adv interface itself), false otherwise
112 */
113static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
114{
115 struct net_device *parent_dev;
116 bool ret;
117
118 /* check if this is a batman-adv mesh interface */
119 if (batadv_softif_is_valid(net_dev))
120 return true;
121
122 /* no more parents..stop recursion */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200123 if (dev_get_iflink(net_dev) == 0 ||
124 dev_get_iflink(net_dev) == net_dev->ifindex)
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200125 return false;
126
127 /* recurse over the parent device */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200128 parent_dev = __dev_get_by_index(&init_net, dev_get_iflink(net_dev));
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200129 /* if we got a NULL parent_dev there is something broken.. */
130 if (WARN(!parent_dev, "Cannot find parent device"))
131 return false;
132
Andrew Lunn1bc4e2b2016-02-11 22:15:57 +0100133 if (batadv_mutual_parents(net_dev, parent_dev))
134 return false;
135
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200136 ret = batadv_is_on_batman_iface(parent_dev);
137
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200138 return ret;
139}
140
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200141static int batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000142{
143 if (net_dev->flags & IFF_LOOPBACK)
144 return 0;
145
146 if (net_dev->type != ARPHRD_ETHER)
147 return 0;
148
149 if (net_dev->addr_len != ETH_ALEN)
150 return 0;
151
152 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200153 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000156 return 1;
157}
158
Matthias Schifferde68d102013-03-09 23:14:22 +0100159/**
160 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
161 * interface
162 * @net_device: the device to check
163 *
164 * Returns true if the net device is a 802.11 wireless device, false otherwise.
165 */
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200166bool batadv_is_wifi_netdev(struct net_device *net_device)
Matthias Schifferde68d102013-03-09 23:14:22 +0100167{
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200168 if (!net_device)
169 return false;
170
Matthias Schifferde68d102013-03-09 23:14:22 +0100171#ifdef CONFIG_WIRELESS_EXT
172 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
173 * check for wireless_handlers != NULL
174 */
175 if (net_device->wireless_handlers)
176 return true;
177#endif
178
179 /* cfg80211 drivers have to set ieee80211_ptr */
180 if (net_device->ieee80211_ptr)
181 return true;
182
183 return false;
184}
185
Sven Eckelmann56303d32012-06-05 22:31:31 +0200186static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200187batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200189 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190
191 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200192 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000193 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194 continue;
195
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200196 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Marek Lindnere6c10f42011-02-18 12:33:20 +0000197 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198 goto out;
199 }
200
Marek Lindnere6c10f42011-02-18 12:33:20 +0000201 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202
203out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000205 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206}
207
Sven Eckelmann56303d32012-06-05 22:31:31 +0200208static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
209 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200211 struct batadv_hard_iface *primary_if;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200212
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200213 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200214 if (!primary_if)
215 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216
Antonio Quartulli785ea112011-11-23 11:35:44 +0100217 batadv_dat_init_own_addr(bat_priv, primary_if);
Sven Eckelmann08adf152012-05-12 13:38:47 +0200218 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200219out:
220 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200221 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222}
223
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224static void batadv_primary_if_select(struct batadv_priv *bat_priv,
225 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200227 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200229 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Marek Lindner32ae9b22011-04-20 15:40:58 +0200231 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
232 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200234 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200235 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236
Marek Lindner32ae9b22011-04-20 15:40:58 +0200237 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100238 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200239
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800240 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200241 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100242
243out:
244 if (curr_hard_iface)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200245 batadv_hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246}
247
Sven Eckelmann56303d32012-06-05 22:31:31 +0200248static bool
249batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000251 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252 return true;
253
254 return false;
255}
256
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200257static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200259 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260
261 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200262 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200263 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
264 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265 continue;
266
Marek Lindnere6c10f42011-02-18 12:33:20 +0000267 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268 continue;
269
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200270 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
271 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272 continue;
273
Sven Eckelmann67969582012-03-26 16:22:45 +0200274 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
275 net_dev->dev_addr, hard_iface->net_dev->name);
276 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277 }
278 rcu_read_unlock();
279}
280
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200281/**
282 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
283 * @soft_iface: netdev struct of the mesh interface
284 */
285static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
286{
287 const struct batadv_hard_iface *hard_iface;
288 unsigned short lower_header_len = ETH_HLEN;
289 unsigned short lower_headroom = 0;
290 unsigned short lower_tailroom = 0;
291 unsigned short needed_headroom;
292
293 rcu_read_lock();
294 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
295 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
296 continue;
297
298 if (hard_iface->soft_iface != soft_iface)
299 continue;
300
301 lower_header_len = max_t(unsigned short, lower_header_len,
302 hard_iface->net_dev->hard_header_len);
303
304 lower_headroom = max_t(unsigned short, lower_headroom,
305 hard_iface->net_dev->needed_headroom);
306
307 lower_tailroom = max_t(unsigned short, lower_tailroom,
308 hard_iface->net_dev->needed_tailroom);
309 }
310 rcu_read_unlock();
311
312 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
313 needed_headroom += batadv_max_header_len();
314
315 soft_iface->needed_headroom = needed_headroom;
316 soft_iface->needed_tailroom = lower_tailroom;
317}
318
Sven Eckelmann95638772012-05-12 02:09:31 +0200319int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800321 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200322 const struct batadv_hard_iface *hard_iface;
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100323 int min_mtu = INT_MAX;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324
325 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200326 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200327 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
328 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329 continue;
330
Marek Lindnere6c10f42011-02-18 12:33:20 +0000331 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332 continue;
333
Marek Lindnera19d3d82013-05-27 15:33:25 +0800334 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335 }
336 rcu_read_unlock();
Marek Lindnera19d3d82013-05-27 15:33:25 +0800337
Marek Lindnera19d3d82013-05-27 15:33:25 +0800338 if (atomic_read(&bat_priv->fragmentation) == 0)
339 goto out;
340
341 /* with fragmentation enabled the maximum size of internally generated
342 * packets such as translation table exchanges or tvlv containers, etc
343 * has to be calculated
344 */
345 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
346 min_mtu -= sizeof(struct batadv_frag_packet);
347 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800348
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349out:
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100350 /* report to the other components the maximum amount of bytes that
351 * batman-adv can send over the wire (without considering the payload
352 * overhead). For example, this value is used by TT to compute the
353 * maximum local table table size
354 */
355 atomic_set(&bat_priv->packet_size_max, min_mtu);
356
357 /* the real soft-interface MTU is computed by removing the payload
358 * overhead from the maximum amount of bytes that was just computed.
359 *
360 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
361 */
362 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363}
364
365/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200366void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800368 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369
Marek Lindnera19d3d82013-05-27 15:33:25 +0800370 /* Check if the local translate table should be cleaned up to match a
371 * new (and smaller) MTU.
372 */
373 batadv_tt_local_resize_to_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374}
375
Sven Eckelmann56303d32012-06-05 22:31:31 +0200376static void
377batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200379 struct batadv_priv *bat_priv;
380 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200382 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200383 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384
Marek Lindnere6c10f42011-02-18 12:33:20 +0000385 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386
Marek Lindnerc3229392012-03-11 06:17:50 +0800387 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200388 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200390 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200391 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200393 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200394 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200395 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
Sven Eckelmann3e348192012-05-16 20:23:22 +0200397 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
398 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399
Sven Eckelmann95638772012-05-12 02:09:31 +0200400 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200401
402out:
403 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200404 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405}
406
Sven Eckelmann56303d32012-06-05 22:31:31 +0200407static void
408batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200410 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
411 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412 return;
413
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200414 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Sven Eckelmann3e348192012-05-16 20:23:22 +0200416 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
417 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418
Sven Eckelmann95638772012-05-12 02:09:31 +0200419 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420}
421
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100422/**
423 * batadv_master_del_slave - remove hard_iface from the current master interface
424 * @slave: the interface enslaved in another master
425 * @master: the master from which slave has to be removed
426 *
427 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
428 * is free'd and master can correctly change its internal state.
429 * Return 0 on success, a negative value representing the error otherwise
430 */
431static int batadv_master_del_slave(struct batadv_hard_iface *slave,
432 struct net_device *master)
433{
434 int ret;
435
436 if (!master)
437 return 0;
438
439 ret = -EBUSY;
440 if (master->netdev_ops->ndo_del_slave)
441 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
442
443 return ret;
444}
445
Sven Eckelmann56303d32012-06-05 22:31:31 +0200446int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Sven Eckelmann95638772012-05-12 02:09:31 +0200447 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200449 struct batadv_priv *bat_priv;
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100450 struct net_device *soft_iface, *master;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200451 __be16 ethertype = htons(ETH_P_BATMAN);
Marek Lindner411d6ed2013-05-08 13:31:59 +0800452 int max_header_len = batadv_max_header_len();
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000453 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200455 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456 goto out;
457
Marek Lindnere6c10f42011-02-18 12:33:20 +0000458 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000459 goto out;
460
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000461 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000463 if (!soft_iface) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200464 soft_iface = batadv_softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000466 if (!soft_iface) {
467 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000469 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470
471 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000472 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473 }
474
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200475 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100476 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000477 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000478 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800479 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000480 }
481
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100482 /* check if the interface is enslaved in another virtual one and
483 * in that case unlink it first
484 */
485 master = netdev_master_upper_dev_get(hard_iface->net_dev);
486 ret = batadv_master_del_slave(hard_iface, master);
487 if (ret)
488 goto err_dev;
489
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000490 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000491 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200492
Jiri Pirko6dffb042015-12-03 12:12:10 +0100493 ret = netdev_master_upper_dev_link(hard_iface->net_dev,
Jiri Pirko29bf24a2015-12-03 12:12:11 +0100494 soft_iface, NULL, NULL);
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800495 if (ret)
496 goto err_dev;
497
Marek Lindner77af7572012-02-07 17:20:48 +0800498 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200499 if (ret < 0)
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800500 goto err_upper;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501
Marek Lindnere6c10f42011-02-18 12:33:20 +0000502 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200504 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200505 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
506 if (ret < 0) {
507 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
508 bat_priv->num_ifaces--;
509 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800510 goto err_upper;
Simon Wunderlich62446302012-07-01 22:51:55 +0200511 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200513 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200514 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000515 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
516 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517
Sven Eckelmann3e348192012-05-16 20:23:22 +0200518 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
519 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200521 if (atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800522 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200523 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800524 "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 +0200525 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800526 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200528 if (!atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800529 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200530 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800531 "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 +0200532 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800533 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200535 if (batadv_hardif_is_iface_up(hard_iface))
536 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200538 batadv_err(hard_iface->soft_iface,
539 "Not using interface %s (retrying later): interface not active\n",
540 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200542 batadv_hardif_recalc_extra_skbroom(soft_iface);
543
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200545 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546
547out:
548 return 0;
549
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800550err_upper:
551 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
Marek Lindner77af7572012-02-07 17:20:48 +0800552err_dev:
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800553 hard_iface->soft_iface = NULL;
Marek Lindner77af7572012-02-07 17:20:48 +0800554 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555err:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200556 batadv_hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000557 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558}
559
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800560void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
561 enum batadv_hard_if_cleanup autodel)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200563 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
564 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200566 if (hard_iface->if_status == BATADV_IF_ACTIVE)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200567 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200569 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200570 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571
Sven Eckelmann3e348192012-05-16 20:23:22 +0200572 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
573 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000574 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575
576 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200577 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000578
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200579 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200580 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200581 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200583 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
584 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585
586 if (new_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200587 batadv_hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588 }
589
Marek Lindner00a50072012-02-07 17:20:47 +0800590 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200591 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592
Marek Lindnere6c10f42011-02-18 12:33:20 +0000593 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200594 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200595 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000596 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597
Antonio Quartullia5256f72015-08-04 22:26:19 +0200598 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200599 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
Antonio Quartullia5256f72015-08-04 22:26:19 +0200600
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601 /* nobody uses this interface anymore */
Antonio Quartulli8257f552013-08-19 18:39:59 +0200602 if (!bat_priv->num_ifaces) {
603 batadv_gw_check_client_stop(bat_priv);
604
605 if (autodel == BATADV_IF_CLEANUP_AUTO)
606 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
607 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608
Marek Lindnere6c10f42011-02-18 12:33:20 +0000609 hard_iface->soft_iface = NULL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200610 batadv_hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200611
612out:
613 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200614 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615}
616
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100617/**
618 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
619 * @work: work queue item
620 *
621 * Free the parts of the hard interface which can not be removed under
622 * rtnl lock (to prevent deadlock situations).
623 */
624static void batadv_hardif_remove_interface_finish(struct work_struct *work)
625{
626 struct batadv_hard_iface *hard_iface;
627
628 hard_iface = container_of(work, struct batadv_hard_iface,
629 cleanup_work);
630
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100631 batadv_debugfs_del_hardif(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100632 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
633 batadv_hardif_free_ref(hard_iface);
634}
635
Sven Eckelmann56303d32012-06-05 22:31:31 +0200636static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200637batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200639 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640 int ret;
641
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200642 ASSERT_RTNL();
643
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200644 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645 if (ret != 1)
646 goto out;
647
648 dev_hold(net_dev);
649
Antonio Quartulli7db3fc22013-04-02 12:16:53 +0200650 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700651 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000652 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653
Sven Eckelmann5853e222012-05-12 02:09:24 +0200654 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655 if (ret)
656 goto free_if;
657
Marek Lindnere6c10f42011-02-18 12:33:20 +0000658 hard_iface->if_num = -1;
659 hard_iface->net_dev = net_dev;
660 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200661 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100662
663 ret = batadv_debugfs_add_hardif(hard_iface);
664 if (ret)
665 goto free_sysfs;
666
Marek Lindnere6c10f42011-02-18 12:33:20 +0000667 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnercef63412015-08-04 21:09:55 +0800668 INIT_HLIST_HEAD(&hard_iface->neigh_list);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100669 INIT_WORK(&hard_iface->cleanup_work,
670 batadv_hardif_remove_interface_finish);
671
Marek Lindnercef63412015-08-04 21:09:55 +0800672 spin_lock_init(&hard_iface->neigh_list_lock);
673
Matthias Schiffercaf65bf2013-03-09 23:14:23 +0100674 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
675 if (batadv_is_wifi_netdev(net_dev))
676 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
677
Marek Lindnered75ccb2011-02-10 14:33:51 +0000678 /* extra reference for return */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000679 atomic_set(&hard_iface->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200681 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200682 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683
Marek Lindnere6c10f42011-02-18 12:33:20 +0000684 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100686free_sysfs:
687 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000688free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000689 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690release_dev:
691 dev_put(net_dev);
692out:
693 return NULL;
694}
695
Sven Eckelmann56303d32012-06-05 22:31:31 +0200696static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200698 ASSERT_RTNL();
699
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200701 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800702 batadv_hardif_disable_interface(hard_iface,
703 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200705 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706 return;
707
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200708 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100709 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000710}
711
Sven Eckelmann95638772012-05-12 02:09:31 +0200712void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200714 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000715
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200716 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000717 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200718 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000719 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200720 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721 }
722 rtnl_unlock();
723}
724
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200725static int batadv_hard_if_event(struct notifier_block *this,
726 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000727{
Jiri Pirko351638e2013-05-28 01:30:21 +0000728 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200729 struct batadv_hard_iface *hard_iface;
730 struct batadv_hard_iface *primary_if = NULL;
731 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732
Sven Eckelmann37130292013-02-11 17:10:22 +0800733 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
734 batadv_sysfs_add_meshif(net_dev);
Antonio Quartulli5d2c05b2013-07-02 11:04:34 +0200735 bat_priv = netdev_priv(net_dev);
736 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
Sven Eckelmann37130292013-02-11 17:10:22 +0800737 return NOTIFY_DONE;
738 }
739
Sven Eckelmann56303d32012-06-05 22:31:31 +0200740 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Andrew Lunna1a66b12015-12-03 21:12:33 +0100741 if (!hard_iface && (event == NETDEV_REGISTER ||
742 event == NETDEV_POST_TYPE_CHANGE))
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200743 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744
Marek Lindnere6c10f42011-02-18 12:33:20 +0000745 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746 goto out;
747
748 switch (event) {
749 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200750 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751 break;
752 case NETDEV_GOING_DOWN:
753 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200754 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755 break;
756 case NETDEV_UNREGISTER:
Andrew Lunna1a66b12015-12-03 21:12:33 +0100757 case NETDEV_PRE_TYPE_CHANGE:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000758 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200760 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761 break;
762 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000763 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200764 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765 break;
766 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200767 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000768 goto hardif_put;
769
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200770 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771
Marek Lindnere6c10f42011-02-18 12:33:20 +0000772 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800773 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800774
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200775 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200776 if (!primary_if)
777 goto hardif_put;
778
779 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200780 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781 break;
782 default:
783 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000784 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785
786hardif_put:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200787 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200789 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200790 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 return NOTIFY_DONE;
792}
793
Sven Eckelmann95638772012-05-12 02:09:31 +0200794struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200795 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000796};