blob: b17e272b5cb3108bb60c9ff0f084b1dff69cdb54 [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 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 Eckelmann140ed8e2016-01-05 12:06:26 +010050/**
51 * batadv_hardif_release - release hard interface from lists and queue for
52 * free after rcu grace period
53 * @hard_iface: the hard interface to free
54 */
55void batadv_hardif_release(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056{
Marek Lindnere6c10f42011-02-18 12:33:20 +000057 dev_put(hard_iface->net_dev);
Sven Eckelmann140ed8e2016-01-05 12:06:26 +010058
59 kfree_rcu(hard_iface, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060}
61
Sven Eckelmann56303d32012-06-05 22:31:31 +020062struct batadv_hard_iface *
63batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064{
Sven Eckelmann56303d32012-06-05 22:31:31 +020065 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066
67 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020068 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000069 if (hard_iface->net_dev == net_dev &&
70 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000071 goto out;
72 }
73
Marek Lindnere6c10f42011-02-18 12:33:20 +000074 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075
76out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000077 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000078 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079}
80
Antonio Quartullib7eddd02012-09-09 10:46:46 +020081/**
82 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
83 * @net_dev: the device to check
84 *
85 * If the user creates any virtual device on top of a batman-adv interface, it
86 * is important to prevent this new interface to be used to create a new mesh
87 * network (this behaviour would lead to a batman-over-batman configuration).
88 * This function recursively checks all the fathers of the device passed as
89 * argument looking for a batman-adv soft interface.
90 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020091 * Return: true if the device is descendant of a batman-adv mesh interface (or
Antonio Quartullib7eddd02012-09-09 10:46:46 +020092 * if it is a batman-adv interface itself), false otherwise
93 */
94static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
95{
96 struct net_device *parent_dev;
97 bool ret;
98
99 /* check if this is a batman-adv mesh interface */
100 if (batadv_softif_is_valid(net_dev))
101 return true;
102
103 /* no more parents..stop recursion */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200104 if (dev_get_iflink(net_dev) == 0 ||
105 dev_get_iflink(net_dev) == net_dev->ifindex)
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200106 return false;
107
108 /* recurse over the parent device */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200109 parent_dev = __dev_get_by_index(&init_net, dev_get_iflink(net_dev));
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200110 /* if we got a NULL parent_dev there is something broken.. */
111 if (WARN(!parent_dev, "Cannot find parent device"))
112 return false;
113
114 ret = batadv_is_on_batman_iface(parent_dev);
115
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200116 return ret;
117}
118
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200119static int batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120{
121 if (net_dev->flags & IFF_LOOPBACK)
122 return 0;
123
124 if (net_dev->type != ARPHRD_ETHER)
125 return 0;
126
127 if (net_dev->addr_len != ETH_ALEN)
128 return 0;
129
130 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200131 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134 return 1;
135}
136
Matthias Schifferde68d102013-03-09 23:14:22 +0100137/**
138 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
139 * interface
140 * @net_device: the device to check
141 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200142 * Return: true if the net device is a 802.11 wireless device, false otherwise.
Matthias Schifferde68d102013-03-09 23:14:22 +0100143 */
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200144bool batadv_is_wifi_netdev(struct net_device *net_device)
Matthias Schifferde68d102013-03-09 23:14:22 +0100145{
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200146 if (!net_device)
147 return false;
148
Matthias Schifferde68d102013-03-09 23:14:22 +0100149#ifdef CONFIG_WIRELESS_EXT
150 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
151 * check for wireless_handlers != NULL
152 */
153 if (net_device->wireless_handlers)
154 return true;
155#endif
156
157 /* cfg80211 drivers have to set ieee80211_ptr */
158 if (net_device->ieee80211_ptr)
159 return true;
160
161 return false;
162}
163
Sven Eckelmann56303d32012-06-05 22:31:31 +0200164static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200165batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200167 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168
169 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200170 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000171 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172 continue;
173
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200174 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Marek Lindnere6c10f42011-02-18 12:33:20 +0000175 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176 goto out;
177 }
178
Marek Lindnere6c10f42011-02-18 12:33:20 +0000179 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180
181out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000182 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000183 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184}
185
Sven Eckelmann56303d32012-06-05 22:31:31 +0200186static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
187 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200189 struct batadv_hard_iface *primary_if;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200190
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200191 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200192 if (!primary_if)
193 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194
Antonio Quartulli785ea112011-11-23 11:35:44 +0100195 batadv_dat_init_own_addr(bat_priv, primary_if);
Sven Eckelmann08adf152012-05-12 13:38:47 +0200196 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200197out:
198 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200199 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200}
201
Sven Eckelmann56303d32012-06-05 22:31:31 +0200202static void batadv_primary_if_select(struct batadv_priv *bat_priv,
203 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200205 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200207 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208
Marek Lindner32ae9b22011-04-20 15:40:58 +0200209 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
210 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200212 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200213 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214
Marek Lindner32ae9b22011-04-20 15:40:58 +0200215 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100216 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200217
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800218 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200219 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100220
221out:
222 if (curr_hard_iface)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200223 batadv_hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224}
225
Sven Eckelmann56303d32012-06-05 22:31:31 +0200226static bool
227batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000229 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230 return true;
231
232 return false;
233}
234
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200235static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200237 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238
239 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200240 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200241 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
242 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243 continue;
244
Marek Lindnere6c10f42011-02-18 12:33:20 +0000245 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246 continue;
247
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200248 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
249 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250 continue;
251
Sven Eckelmann67969582012-03-26 16:22:45 +0200252 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
253 net_dev->dev_addr, hard_iface->net_dev->name);
254 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 }
256 rcu_read_unlock();
257}
258
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200259/**
260 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
261 * @soft_iface: netdev struct of the mesh interface
262 */
263static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
264{
265 const struct batadv_hard_iface *hard_iface;
266 unsigned short lower_header_len = ETH_HLEN;
267 unsigned short lower_headroom = 0;
268 unsigned short lower_tailroom = 0;
269 unsigned short needed_headroom;
270
271 rcu_read_lock();
272 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
273 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
274 continue;
275
276 if (hard_iface->soft_iface != soft_iface)
277 continue;
278
279 lower_header_len = max_t(unsigned short, lower_header_len,
280 hard_iface->net_dev->hard_header_len);
281
282 lower_headroom = max_t(unsigned short, lower_headroom,
283 hard_iface->net_dev->needed_headroom);
284
285 lower_tailroom = max_t(unsigned short, lower_tailroom,
286 hard_iface->net_dev->needed_tailroom);
287 }
288 rcu_read_unlock();
289
290 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
291 needed_headroom += batadv_max_header_len();
292
293 soft_iface->needed_headroom = needed_headroom;
294 soft_iface->needed_tailroom = lower_tailroom;
295}
296
Sven Eckelmann95638772012-05-12 02:09:31 +0200297int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800299 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200300 const struct batadv_hard_iface *hard_iface;
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100301 int min_mtu = INT_MAX;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
303 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200304 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200305 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
306 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307 continue;
308
Marek Lindnere6c10f42011-02-18 12:33:20 +0000309 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310 continue;
311
Marek Lindnera19d3d82013-05-27 15:33:25 +0800312 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313 }
314 rcu_read_unlock();
Marek Lindnera19d3d82013-05-27 15:33:25 +0800315
Marek Lindnera19d3d82013-05-27 15:33:25 +0800316 if (atomic_read(&bat_priv->fragmentation) == 0)
317 goto out;
318
319 /* with fragmentation enabled the maximum size of internally generated
320 * packets such as translation table exchanges or tvlv containers, etc
321 * has to be calculated
322 */
323 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
324 min_mtu -= sizeof(struct batadv_frag_packet);
325 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800326
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327out:
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100328 /* report to the other components the maximum amount of bytes that
329 * batman-adv can send over the wire (without considering the payload
330 * overhead). For example, this value is used by TT to compute the
331 * maximum local table table size
332 */
333 atomic_set(&bat_priv->packet_size_max, min_mtu);
334
335 /* the real soft-interface MTU is computed by removing the payload
336 * overhead from the maximum amount of bytes that was just computed.
337 *
338 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
339 */
340 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341}
342
343/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200344void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800346 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347
Marek Lindnera19d3d82013-05-27 15:33:25 +0800348 /* Check if the local translate table should be cleaned up to match a
349 * new (and smaller) MTU.
350 */
351 batadv_tt_local_resize_to_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352}
353
Sven Eckelmann56303d32012-06-05 22:31:31 +0200354static void
355batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200357 struct batadv_priv *bat_priv;
358 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200360 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200361 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362
Marek Lindnere6c10f42011-02-18 12:33:20 +0000363 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
Marek Lindnerc3229392012-03-11 06:17:50 +0800365 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200366 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200368 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200369 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200371 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200372 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200373 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374
Sven Eckelmann3e348192012-05-16 20:23:22 +0200375 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
376 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Sven Eckelmann95638772012-05-12 02:09:31 +0200378 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200379
380out:
381 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200382 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383}
384
Sven Eckelmann56303d32012-06-05 22:31:31 +0200385static void
386batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200388 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
389 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390 return;
391
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200392 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393
Sven Eckelmann3e348192012-05-16 20:23:22 +0200394 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
395 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
Sven Eckelmann95638772012-05-12 02:09:31 +0200397 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398}
399
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100400/**
401 * batadv_master_del_slave - remove hard_iface from the current master interface
402 * @slave: the interface enslaved in another master
403 * @master: the master from which slave has to be removed
404 *
405 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
406 * is free'd and master can correctly change its internal state.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200407 *
408 * Return: 0 on success, a negative value representing the error otherwise
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100409 */
410static int batadv_master_del_slave(struct batadv_hard_iface *slave,
411 struct net_device *master)
412{
413 int ret;
414
415 if (!master)
416 return 0;
417
418 ret = -EBUSY;
419 if (master->netdev_ops->ndo_del_slave)
420 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
421
422 return ret;
423}
424
Sven Eckelmann56303d32012-06-05 22:31:31 +0200425int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Sven Eckelmann95638772012-05-12 02:09:31 +0200426 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200428 struct batadv_priv *bat_priv;
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100429 struct net_device *soft_iface, *master;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200430 __be16 ethertype = htons(ETH_P_BATMAN);
Marek Lindner411d6ed2013-05-08 13:31:59 +0800431 int max_header_len = batadv_max_header_len();
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000432 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200434 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435 goto out;
436
Marek Lindnere6c10f42011-02-18 12:33:20 +0000437 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000438 goto out;
439
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000440 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000442 if (!soft_iface) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200443 soft_iface = batadv_softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000445 if (!soft_iface) {
446 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000448 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449
450 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000451 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 }
453
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200454 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100455 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000456 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000457 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800458 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000459 }
460
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100461 /* check if the interface is enslaved in another virtual one and
462 * in that case unlink it first
463 */
464 master = netdev_master_upper_dev_get(hard_iface->net_dev);
465 ret = batadv_master_del_slave(hard_iface, master);
466 if (ret)
467 goto err_dev;
468
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000469 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000470 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200471
Jiri Pirko6dffb042015-12-03 12:12:10 +0100472 ret = netdev_master_upper_dev_link(hard_iface->net_dev,
Jiri Pirko29bf24a2015-12-03 12:12:11 +0100473 soft_iface, NULL, NULL);
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800474 if (ret)
475 goto err_dev;
476
Marek Lindner77af7572012-02-07 17:20:48 +0800477 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200478 if (ret < 0)
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800479 goto err_upper;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480
Marek Lindnere6c10f42011-02-18 12:33:20 +0000481 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200483 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200484 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
485 if (ret < 0) {
486 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
487 bat_priv->num_ifaces--;
488 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800489 goto err_upper;
Simon Wunderlich62446302012-07-01 22:51:55 +0200490 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200492 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200493 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000494 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
495 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Sven Eckelmann3e348192012-05-16 20:23:22 +0200497 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
498 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200500 if (atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800501 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200502 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800503 "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 +0200504 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800505 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200507 if (!atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800508 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200509 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800510 "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 +0200511 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800512 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200514 if (batadv_hardif_is_iface_up(hard_iface))
515 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200517 batadv_err(hard_iface->soft_iface,
518 "Not using interface %s (retrying later): interface not active\n",
519 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200521 batadv_hardif_recalc_extra_skbroom(soft_iface);
522
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200524 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525
526out:
527 return 0;
528
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800529err_upper:
530 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
Marek Lindner77af7572012-02-07 17:20:48 +0800531err_dev:
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800532 hard_iface->soft_iface = NULL;
Marek Lindner77af7572012-02-07 17:20:48 +0800533 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534err:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200535 batadv_hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000536 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537}
538
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800539void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
540 enum batadv_hard_if_cleanup autodel)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200542 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
543 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200545 if (hard_iface->if_status == BATADV_IF_ACTIVE)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200546 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200548 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200549 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
Sven Eckelmann3e348192012-05-16 20:23:22 +0200551 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
552 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000553 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
555 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200556 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200558 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200559 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200560 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200562 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
563 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564
565 if (new_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200566 batadv_hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567 }
568
Marek Lindner00a50072012-02-07 17:20:47 +0800569 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200570 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571
Marek Lindnere6c10f42011-02-18 12:33:20 +0000572 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200573 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200574 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000575 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576
Antonio Quartullia5256f72015-08-04 22:26:19 +0200577 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200578 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
Antonio Quartullia5256f72015-08-04 22:26:19 +0200579
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580 /* nobody uses this interface anymore */
Antonio Quartulli8257f552013-08-19 18:39:59 +0200581 if (!bat_priv->num_ifaces) {
582 batadv_gw_check_client_stop(bat_priv);
583
584 if (autodel == BATADV_IF_CLEANUP_AUTO)
585 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
586 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587
Marek Lindnere6c10f42011-02-18 12:33:20 +0000588 hard_iface->soft_iface = NULL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200589 batadv_hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200590
591out:
592 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200593 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594}
595
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100596/**
597 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
598 * @work: work queue item
599 *
600 * Free the parts of the hard interface which can not be removed under
601 * rtnl lock (to prevent deadlock situations).
602 */
603static void batadv_hardif_remove_interface_finish(struct work_struct *work)
604{
605 struct batadv_hard_iface *hard_iface;
606
607 hard_iface = container_of(work, struct batadv_hard_iface,
608 cleanup_work);
609
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100610 batadv_debugfs_del_hardif(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100611 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
612 batadv_hardif_free_ref(hard_iface);
613}
614
Sven Eckelmann56303d32012-06-05 22:31:31 +0200615static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200616batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200618 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619 int ret;
620
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200621 ASSERT_RTNL();
622
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200623 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624 if (ret != 1)
625 goto out;
626
627 dev_hold(net_dev);
628
Antonio Quartulli7db3fc22013-04-02 12:16:53 +0200629 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700630 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Sven Eckelmann5853e222012-05-12 02:09:24 +0200633 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 if (ret)
635 goto free_if;
636
Marek Lindnere6c10f42011-02-18 12:33:20 +0000637 hard_iface->if_num = -1;
638 hard_iface->net_dev = net_dev;
639 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200640 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100641
642 ret = batadv_debugfs_add_hardif(hard_iface);
643 if (ret)
644 goto free_sysfs;
645
Marek Lindnere6c10f42011-02-18 12:33:20 +0000646 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnercef63412015-08-04 21:09:55 +0800647 INIT_HLIST_HEAD(&hard_iface->neigh_list);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100648 INIT_WORK(&hard_iface->cleanup_work,
649 batadv_hardif_remove_interface_finish);
650
Marek Lindnercef63412015-08-04 21:09:55 +0800651 spin_lock_init(&hard_iface->neigh_list_lock);
652
Matthias Schiffercaf65bf2013-03-09 23:14:23 +0100653 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
654 if (batadv_is_wifi_netdev(net_dev))
655 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
656
Marek Lindnered75ccb2011-02-10 14:33:51 +0000657 /* extra reference for return */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000658 atomic_set(&hard_iface->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200660 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200661 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662
Marek Lindnere6c10f42011-02-18 12:33:20 +0000663 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100665free_sysfs:
666 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000668 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669release_dev:
670 dev_put(net_dev);
671out:
672 return NULL;
673}
674
Sven Eckelmann56303d32012-06-05 22:31:31 +0200675static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200677 ASSERT_RTNL();
678
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200680 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800681 batadv_hardif_disable_interface(hard_iface,
682 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200684 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685 return;
686
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200687 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100688 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689}
690
Sven Eckelmann95638772012-05-12 02:09:31 +0200691void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200693 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200695 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000696 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200697 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000698 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200699 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700 }
701 rtnl_unlock();
702}
703
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200704static int batadv_hard_if_event(struct notifier_block *this,
705 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706{
Jiri Pirko351638e2013-05-28 01:30:21 +0000707 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200708 struct batadv_hard_iface *hard_iface;
709 struct batadv_hard_iface *primary_if = NULL;
710 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711
Sven Eckelmann37130292013-02-11 17:10:22 +0800712 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
713 batadv_sysfs_add_meshif(net_dev);
Antonio Quartulli5d2c05b2013-07-02 11:04:34 +0200714 bat_priv = netdev_priv(net_dev);
715 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
Sven Eckelmann37130292013-02-11 17:10:22 +0800716 return NOTIFY_DONE;
717 }
718
Sven Eckelmann56303d32012-06-05 22:31:31 +0200719 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Andrew Lunna1a66b12015-12-03 21:12:33 +0100720 if (!hard_iface && (event == NETDEV_REGISTER ||
721 event == NETDEV_POST_TYPE_CHANGE))
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200722 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000723
Marek Lindnere6c10f42011-02-18 12:33:20 +0000724 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725 goto out;
726
727 switch (event) {
728 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200729 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730 break;
731 case NETDEV_GOING_DOWN:
732 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200733 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734 break;
735 case NETDEV_UNREGISTER:
Andrew Lunna1a66b12015-12-03 21:12:33 +0100736 case NETDEV_PRE_TYPE_CHANGE:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000737 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200739 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740 break;
741 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000742 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200743 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744 break;
745 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200746 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747 goto hardif_put;
748
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200749 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750
Marek Lindnere6c10f42011-02-18 12:33:20 +0000751 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800752 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800753
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200754 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200755 if (!primary_if)
756 goto hardif_put;
757
758 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200759 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760 break;
761 default:
762 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000763 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764
765hardif_put:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200766 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200768 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200769 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000770 return NOTIFY_DONE;
771}
772
Sven Eckelmann95638772012-05-12 02:09:31 +0200773struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200774 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775};