blob: fb2d9c058ed047255ab8a1fc605d1bcb9591d60a [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>
39#include <net/net_namespace.h>
40
41#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/**
87 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
88 * @net_dev: the device to check
89 *
90 * If the user creates any virtual device on top of a batman-adv interface, it
91 * is important to prevent this new interface to be used to create a new mesh
92 * network (this behaviour would lead to a batman-over-batman configuration).
93 * This function recursively checks all the fathers of the device passed as
94 * argument looking for a batman-adv soft interface.
95 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020096 * Return: true if the device is descendant of a batman-adv mesh interface (or
Antonio Quartullib7eddd02012-09-09 10:46:46 +020097 * if it is a batman-adv interface itself), false otherwise
98 */
99static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
100{
101 struct net_device *parent_dev;
102 bool ret;
103
104 /* check if this is a batman-adv mesh interface */
105 if (batadv_softif_is_valid(net_dev))
106 return true;
107
108 /* no more parents..stop recursion */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200109 if (dev_get_iflink(net_dev) == 0 ||
110 dev_get_iflink(net_dev) == net_dev->ifindex)
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200111 return false;
112
113 /* recurse over the parent device */
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200114 parent_dev = __dev_get_by_index(&init_net, dev_get_iflink(net_dev));
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200115 /* if we got a NULL parent_dev there is something broken.. */
116 if (WARN(!parent_dev, "Cannot find parent device"))
117 return false;
118
119 ret = batadv_is_on_batman_iface(parent_dev);
120
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200121 return ret;
122}
123
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200124static int batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125{
126 if (net_dev->flags & IFF_LOOPBACK)
127 return 0;
128
129 if (net_dev->type != ARPHRD_ETHER)
130 return 0;
131
132 if (net_dev->addr_len != ETH_ALEN)
133 return 0;
134
135 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200136 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000139 return 1;
140}
141
Matthias Schifferde68d102013-03-09 23:14:22 +0100142/**
143 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
144 * interface
145 * @net_device: the device to check
146 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200147 * Return: true if the net device is a 802.11 wireless device, false otherwise.
Matthias Schifferde68d102013-03-09 23:14:22 +0100148 */
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200149bool batadv_is_wifi_netdev(struct net_device *net_device)
Matthias Schifferde68d102013-03-09 23:14:22 +0100150{
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200151 if (!net_device)
152 return false;
153
Matthias Schifferde68d102013-03-09 23:14:22 +0100154#ifdef CONFIG_WIRELESS_EXT
155 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
156 * check for wireless_handlers != NULL
157 */
158 if (net_device->wireless_handlers)
159 return true;
160#endif
161
162 /* cfg80211 drivers have to set ieee80211_ptr */
163 if (net_device->ieee80211_ptr)
164 return true;
165
166 return false;
167}
168
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200170batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200172 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173
174 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200175 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000176 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177 continue;
178
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200179 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100180 kref_get_unless_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 goto out;
182 }
183
Marek Lindnere6c10f42011-02-18 12:33:20 +0000184 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185
186out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000188 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189}
190
Sven Eckelmann56303d32012-06-05 22:31:31 +0200191static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
192 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200194 struct batadv_hard_iface *primary_if;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200195
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200196 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200197 if (!primary_if)
198 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199
Antonio Quartulli785ea112011-11-23 11:35:44 +0100200 batadv_dat_init_own_addr(bat_priv, primary_if);
Sven Eckelmann08adf152012-05-12 13:38:47 +0200201 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200202out:
203 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200204 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205}
206
Sven Eckelmann56303d32012-06-05 22:31:31 +0200207static void batadv_primary_if_select(struct batadv_priv *bat_priv,
208 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200210 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200212 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100214 if (new_hard_iface && !kref_get_unless_zero(&new_hard_iface->refcount))
Marek Lindner32ae9b22011-04-20 15:40:58 +0200215 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200217 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200218 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219
Marek Lindner32ae9b22011-04-20 15:40:58 +0200220 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100221 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200222
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800223 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200224 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100225
226out:
227 if (curr_hard_iface)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200228 batadv_hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229}
230
Sven Eckelmann56303d32012-06-05 22:31:31 +0200231static bool
232batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000234 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000235 return true;
236
237 return false;
238}
239
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200240static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200242 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243
244 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200245 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200246 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
247 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248 continue;
249
Marek Lindnere6c10f42011-02-18 12:33:20 +0000250 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251 continue;
252
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200253 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
254 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 continue;
256
Sven Eckelmann67969582012-03-26 16:22:45 +0200257 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
258 net_dev->dev_addr, hard_iface->net_dev->name);
259 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260 }
261 rcu_read_unlock();
262}
263
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200264/**
265 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom
266 * @soft_iface: netdev struct of the mesh interface
267 */
268static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface)
269{
270 const struct batadv_hard_iface *hard_iface;
271 unsigned short lower_header_len = ETH_HLEN;
272 unsigned short lower_headroom = 0;
273 unsigned short lower_tailroom = 0;
274 unsigned short needed_headroom;
275
276 rcu_read_lock();
277 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
278 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
279 continue;
280
281 if (hard_iface->soft_iface != soft_iface)
282 continue;
283
284 lower_header_len = max_t(unsigned short, lower_header_len,
285 hard_iface->net_dev->hard_header_len);
286
287 lower_headroom = max_t(unsigned short, lower_headroom,
288 hard_iface->net_dev->needed_headroom);
289
290 lower_tailroom = max_t(unsigned short, lower_tailroom,
291 hard_iface->net_dev->needed_tailroom);
292 }
293 rcu_read_unlock();
294
295 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN);
296 needed_headroom += batadv_max_header_len();
297
298 soft_iface->needed_headroom = needed_headroom;
299 soft_iface->needed_tailroom = lower_tailroom;
300}
301
Sven Eckelmann95638772012-05-12 02:09:31 +0200302int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800304 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200305 const struct batadv_hard_iface *hard_iface;
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100306 int min_mtu = INT_MAX;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
308 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200309 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200310 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
311 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312 continue;
313
Marek Lindnere6c10f42011-02-18 12:33:20 +0000314 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315 continue;
316
Marek Lindnera19d3d82013-05-27 15:33:25 +0800317 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 }
319 rcu_read_unlock();
Marek Lindnera19d3d82013-05-27 15:33:25 +0800320
Marek Lindnera19d3d82013-05-27 15:33:25 +0800321 if (atomic_read(&bat_priv->fragmentation) == 0)
322 goto out;
323
324 /* with fragmentation enabled the maximum size of internally generated
325 * packets such as translation table exchanges or tvlv containers, etc
326 * has to be calculated
327 */
328 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
329 min_mtu -= sizeof(struct batadv_frag_packet);
330 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800331
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332out:
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100333 /* report to the other components the maximum amount of bytes that
334 * batman-adv can send over the wire (without considering the payload
335 * overhead). For example, this value is used by TT to compute the
336 * maximum local table table size
337 */
338 atomic_set(&bat_priv->packet_size_max, min_mtu);
339
340 /* the real soft-interface MTU is computed by removing the payload
341 * overhead from the maximum amount of bytes that was just computed.
342 *
343 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
344 */
345 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346}
347
348/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200349void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800351 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352
Marek Lindnera19d3d82013-05-27 15:33:25 +0800353 /* Check if the local translate table should be cleaned up to match a
354 * new (and smaller) MTU.
355 */
356 batadv_tt_local_resize_to_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357}
358
Sven Eckelmann56303d32012-06-05 22:31:31 +0200359static void
360batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200362 struct batadv_priv *bat_priv;
363 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200365 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200366 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Marek Lindnere6c10f42011-02-18 12:33:20 +0000368 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369
Marek Lindnerc3229392012-03-11 06:17:50 +0800370 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200371 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200373 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200374 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200376 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200377 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200378 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379
Sven Eckelmann3e348192012-05-16 20:23:22 +0200380 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
381 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
Sven Eckelmann95638772012-05-12 02:09:31 +0200383 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200384
385out:
386 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200387 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388}
389
Sven Eckelmann56303d32012-06-05 22:31:31 +0200390static void
391batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200393 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
394 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395 return;
396
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200397 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398
Sven Eckelmann3e348192012-05-16 20:23:22 +0200399 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
400 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
Sven Eckelmann95638772012-05-12 02:09:31 +0200402 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403}
404
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100405/**
406 * batadv_master_del_slave - remove hard_iface from the current master interface
407 * @slave: the interface enslaved in another master
408 * @master: the master from which slave has to be removed
409 *
410 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
411 * is free'd and master can correctly change its internal state.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200412 *
413 * Return: 0 on success, a negative value representing the error otherwise
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100414 */
415static int batadv_master_del_slave(struct batadv_hard_iface *slave,
416 struct net_device *master)
417{
418 int ret;
419
420 if (!master)
421 return 0;
422
423 ret = -EBUSY;
424 if (master->netdev_ops->ndo_del_slave)
425 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
426
427 return ret;
428}
429
Sven Eckelmann56303d32012-06-05 22:31:31 +0200430int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Sven Eckelmann95638772012-05-12 02:09:31 +0200431 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200433 struct batadv_priv *bat_priv;
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100434 struct net_device *soft_iface, *master;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200435 __be16 ethertype = htons(ETH_P_BATMAN);
Marek Lindner411d6ed2013-05-08 13:31:59 +0800436 int max_header_len = batadv_max_header_len();
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000437 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200439 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440 goto out;
441
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100442 if (!kref_get_unless_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000443 goto out;
444
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000445 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000447 if (!soft_iface) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200448 soft_iface = batadv_softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000450 if (!soft_iface) {
451 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000453 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454
455 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000456 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457 }
458
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200459 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100460 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000461 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000462 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800463 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000464 }
465
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100466 /* check if the interface is enslaved in another virtual one and
467 * in that case unlink it first
468 */
469 master = netdev_master_upper_dev_get(hard_iface->net_dev);
470 ret = batadv_master_del_slave(hard_iface, master);
471 if (ret)
472 goto err_dev;
473
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000474 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000475 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200476
Jiri Pirko6dffb042015-12-03 12:12:10 +0100477 ret = netdev_master_upper_dev_link(hard_iface->net_dev,
Jiri Pirko29bf24a2015-12-03 12:12:11 +0100478 soft_iface, NULL, NULL);
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800479 if (ret)
480 goto err_dev;
481
Marek Lindner77af7572012-02-07 17:20:48 +0800482 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200483 if (ret < 0)
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800484 goto err_upper;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485
Marek Lindnere6c10f42011-02-18 12:33:20 +0000486 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200488 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200489 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
490 if (ret < 0) {
491 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
492 bat_priv->num_ifaces--;
493 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800494 goto err_upper;
Simon Wunderlich62446302012-07-01 22:51:55 +0200495 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200497 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200498 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000499 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
500 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501
Sven Eckelmann3e348192012-05-16 20:23:22 +0200502 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
503 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200505 if (atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800506 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200507 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800508 "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 +0200509 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800510 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200512 if (!atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800513 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200514 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800515 "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 +0200516 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800517 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200519 if (batadv_hardif_is_iface_up(hard_iface))
520 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200522 batadv_err(hard_iface->soft_iface,
523 "Not using interface %s (retrying later): interface not active\n",
524 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200526 batadv_hardif_recalc_extra_skbroom(soft_iface);
527
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200529 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530
531out:
532 return 0;
533
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800534err_upper:
535 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
Marek Lindner77af7572012-02-07 17:20:48 +0800536err_dev:
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800537 hard_iface->soft_iface = NULL;
Marek Lindner77af7572012-02-07 17:20:48 +0800538 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000539err:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200540 batadv_hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000541 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542}
543
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800544void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
545 enum batadv_hard_if_cleanup autodel)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200547 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
548 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200550 if (hard_iface->if_status == BATADV_IF_ACTIVE)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200551 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200553 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200554 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555
Sven Eckelmann3e348192012-05-16 20:23:22 +0200556 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
557 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000558 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559
560 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200561 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200563 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200564 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200565 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200567 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
568 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569
570 if (new_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200571 batadv_hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572 }
573
Marek Lindner00a50072012-02-07 17:20:47 +0800574 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200575 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576
Marek Lindnere6c10f42011-02-18 12:33:20 +0000577 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200578 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200579 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000580 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581
Antonio Quartullia5256f72015-08-04 22:26:19 +0200582 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
Sven Eckelmann7bca68c2015-08-07 19:28:42 +0200583 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface);
Antonio Quartullia5256f72015-08-04 22:26:19 +0200584
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585 /* nobody uses this interface anymore */
Antonio Quartulli8257f552013-08-19 18:39:59 +0200586 if (!bat_priv->num_ifaces) {
587 batadv_gw_check_client_stop(bat_priv);
588
589 if (autodel == BATADV_IF_CLEANUP_AUTO)
590 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
591 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592
Marek Lindnere6c10f42011-02-18 12:33:20 +0000593 hard_iface->soft_iface = NULL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200594 batadv_hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200595
596out:
597 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200598 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599}
600
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100601/**
602 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
603 * @work: work queue item
604 *
605 * Free the parts of the hard interface which can not be removed under
606 * rtnl lock (to prevent deadlock situations).
607 */
608static void batadv_hardif_remove_interface_finish(struct work_struct *work)
609{
610 struct batadv_hard_iface *hard_iface;
611
612 hard_iface = container_of(work, struct batadv_hard_iface,
613 cleanup_work);
614
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100615 batadv_debugfs_del_hardif(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100616 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
617 batadv_hardif_free_ref(hard_iface);
618}
619
Sven Eckelmann56303d32012-06-05 22:31:31 +0200620static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200621batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200623 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624 int ret;
625
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200626 ASSERT_RTNL();
627
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200628 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 if (ret != 1)
630 goto out;
631
632 dev_hold(net_dev);
633
Antonio Quartulli7db3fc22013-04-02 12:16:53 +0200634 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700635 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637
Sven Eckelmann5853e222012-05-12 02:09:24 +0200638 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639 if (ret)
640 goto free_if;
641
Marek Lindnere6c10f42011-02-18 12:33:20 +0000642 hard_iface->if_num = -1;
643 hard_iface->net_dev = net_dev;
644 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200645 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100646
647 ret = batadv_debugfs_add_hardif(hard_iface);
648 if (ret)
649 goto free_sysfs;
650
Marek Lindnere6c10f42011-02-18 12:33:20 +0000651 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnercef63412015-08-04 21:09:55 +0800652 INIT_HLIST_HEAD(&hard_iface->neigh_list);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100653 INIT_WORK(&hard_iface->cleanup_work,
654 batadv_hardif_remove_interface_finish);
655
Marek Lindnercef63412015-08-04 21:09:55 +0800656 spin_lock_init(&hard_iface->neigh_list_lock);
657
Matthias Schiffercaf65bf2013-03-09 23:14:23 +0100658 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
659 if (batadv_is_wifi_netdev(net_dev))
660 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
661
Marek Lindnered75ccb2011-02-10 14:33:51 +0000662 /* extra reference for return */
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100663 kref_init(&hard_iface->refcount);
664 kref_get(&hard_iface->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000665
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200666 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200667 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668
Marek Lindnere6c10f42011-02-18 12:33:20 +0000669 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000670
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100671free_sysfs:
672 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000674 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675release_dev:
676 dev_put(net_dev);
677out:
678 return NULL;
679}
680
Sven Eckelmann56303d32012-06-05 22:31:31 +0200681static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200683 ASSERT_RTNL();
684
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200686 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800687 batadv_hardif_disable_interface(hard_iface,
688 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200690 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000691 return;
692
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200693 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100694 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695}
696
Sven Eckelmann95638772012-05-12 02:09:31 +0200697void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200699 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200701 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000702 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200703 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000704 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200705 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706 }
707 rtnl_unlock();
708}
709
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200710static int batadv_hard_if_event(struct notifier_block *this,
711 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000712{
Jiri Pirko351638e2013-05-28 01:30:21 +0000713 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200714 struct batadv_hard_iface *hard_iface;
715 struct batadv_hard_iface *primary_if = NULL;
716 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000717
Sven Eckelmann37130292013-02-11 17:10:22 +0800718 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
719 batadv_sysfs_add_meshif(net_dev);
Antonio Quartulli5d2c05b2013-07-02 11:04:34 +0200720 bat_priv = netdev_priv(net_dev);
721 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
Sven Eckelmann37130292013-02-11 17:10:22 +0800722 return NOTIFY_DONE;
723 }
724
Sven Eckelmann56303d32012-06-05 22:31:31 +0200725 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Andrew Lunna1a66b12015-12-03 21:12:33 +0100726 if (!hard_iface && (event == NETDEV_REGISTER ||
727 event == NETDEV_POST_TYPE_CHANGE))
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200728 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729
Marek Lindnere6c10f42011-02-18 12:33:20 +0000730 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000731 goto out;
732
733 switch (event) {
734 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200735 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736 break;
737 case NETDEV_GOING_DOWN:
738 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200739 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740 break;
741 case NETDEV_UNREGISTER:
Andrew Lunna1a66b12015-12-03 21:12:33 +0100742 case NETDEV_PRE_TYPE_CHANGE:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000743 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200745 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746 break;
747 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000748 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200749 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750 break;
751 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200752 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000753 goto hardif_put;
754
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200755 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000756
Marek Lindnere6c10f42011-02-18 12:33:20 +0000757 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800758 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800759
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200760 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200761 if (!primary_if)
762 goto hardif_put;
763
764 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200765 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000766 break;
767 default:
768 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000769 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000770
771hardif_put:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200772 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200774 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200775 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776 return NOTIFY_DONE;
777}
778
Sven Eckelmann95638772012-05-12 02:09:31 +0200779struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200780 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781};